From c336365d14057656ed47c66861bf59fd62b83d9b Mon Sep 17 00:00:00 2001
From: Luc Giffon <luc.giffon@lif.univ-mrs.fr>
Date: Thu, 26 Jul 2018 14:09:25 +0200
Subject: [PATCH] add memory usage function

---
 skluc/utils.py | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/skluc/utils.py b/skluc/utils.py
index 525d7c9..e9e93de 100644
--- a/skluc/utils.py
+++ b/skluc/utils.py
@@ -214,6 +214,26 @@ def replace_nan(tensor):
     return np.where(np.isnan(tensor), np.zeros_like(tensor), tensor)
 
 
+def memory_usage():
+    """
+    Copy pasted from https://airbrake.io/blog/python-exception-handling/memoryerror
+
+    Prints current memory usage stats.
+    See: https://stackoverflow.com/a/15495136
+
+    :return: None
+    """
+    PROCESS = psutil.Process(os.getpid())
+    GIGA = 10 ** 9
+    # MEGA_STR = ' ' * MEGA
+
+    mem = psutil.virtual_memory()
+    total, available, used, free = mem.total / GIGA, mem.available / GIGA, mem.used / GIGA, mem.free / GIGA
+    proc = PROCESS.memory_info()[1] / GIGA
+    return 'process = %s total = %s available = %s used = %s free = %s' \
+          % (proc, total, available, used, free)
+
+
 def compute_euristic_sigma_chi2(dataset_full, slice_size=100):
     """
     Given a dataset, return the gamma that should be used (euristically) when using a rbf kernel on this dataset.
-- 
GitLab