2323
2424from pymove .utils .log import logger
2525
26- try :
27- pass
28- except ImportError :
29- pass
30-
3126
3227def reduce_mem_usage_automatic (df : DataFrame ):
3328 """
@@ -38,6 +33,22 @@ def reduce_mem_usage_automatic(df: DataFrame):
3833 df : dataframe
3934 The input data to which the operation will be performed.
4035
36+ Examples
37+ --------
38+ >>> import numpy as np
39+ >>> import pandas as pd
40+ >>> from pymove.utils.mem import reduce_mem_usage_automatic
41+ >>> df = pd.DataFrame({'col_1': np.arange(10000, dtype=np.float64)})
42+ >>> df.dtytes
43+ col_1 float64
44+ dtype: object
45+ >>> reduce_mem_usage_automatic(df)
46+ 'Memory usage of dataframe is 0.08 MB'
47+ 'Memory usage after optimization is: 0.02 MB'
48+ 'Decreased by 74.9 %'
49+ >>> df.dtytes
50+ col_1 float16
51+ dtype: object
4152 """
4253 start_mem = df .memory_usage ().sum () / 1024 ** 2
4354 logger .info ('Memory usage of dataframe is {:.2f} MB' .format (start_mem ))
@@ -138,6 +149,15 @@ def total_size(
138149 float
139150 The memory used by the given object
140151
152+ Examples
153+ --------
154+ >>> import numpy as np
155+ >>> from pymove.utils.mem import total_size
156+ >>> arr = np.arange(10000, dtype=np.float64)
157+ >>> sz = total_size(arr)
158+ 'Size in bytes: 80104, Type: <class 'numpy.ndarray'>'
159+ >>> sz
160+ 432
141161 """
142162 if handlers is None :
143163 handlers = {}
@@ -194,7 +214,17 @@ def begin_operation(name: Text) -> Dict:
194214 -------
195215 dict
196216 dictionary with the operation stats
197-
217+ Examples
218+ --------
219+ >>> from pymove.utils.mem import begin_operation
220+ >>> operation = begin_operation('operation')
221+ >>> operation
222+ {
223+ 'process': psutil.Process(
224+ pid=103401, name='python', status='running', started='21:48:11'
225+ ),
226+ 'init': 293732352, 'start': 1622082973.8825781, 'name': 'operation'
227+ }
198228 """
199229 process = psutil .Process (os .getpid ())
200230 init = process .memory_info ()[0 ]
@@ -216,6 +246,16 @@ def end_operation(operation: Dict) -> Dict:
216246 dict
217247 dictionary with the operation execution stats
218248
249+ Examples
250+ --------
251+ >>> import numpy as np
252+ >>> import time
253+ >>> from pymove.utils.mem import begin_operation, end_operation
254+ >>> operation = begin_operation('create_arr')
255+ >>> arr = np.arange(100000, dtype=np.float64)
256+ >>> time.sleep(1.2)
257+ >>> end_operation(operation)
258+ {'name': 'create_arr', 'time in seconds': 1.2022554874420166, 'memory': '752.0 KiB'}
219259 """
220260 finish = operation ['process' ].memory_info ()[0 ]
221261 last_operation_name = operation ['name' ]
@@ -244,7 +284,13 @@ def sizeof_fmt(mem_usage: int, suffix: Optional[Text] = 'B') -> Text:
244284 -------
245285 str
246286 A string of the memory usage in a more readable format
247-
287+ Examples
288+ --------
289+ >>> from pymove.utils.mem import sizeof_fmt
290+ >>> sizeof_fmt(1024)
291+ 1.0 KiB
292+ >>> sizeof_fmt(2e6)
293+ 1.9 MiB
248294 """
249295 for unit in ['' , 'Ki' , 'Mi' , 'Gi' , 'Ti' , 'Pi' , 'Ei' , 'Zi' ]:
250296 if abs (mem_usage ) < 1024.0 :
@@ -254,15 +300,15 @@ def sizeof_fmt(mem_usage: int, suffix: Optional[Text] = 'B') -> Text:
254300
255301
256302def top_mem_vars (
257- variables : Optional [ Callable ] = None , n : Optional [int ] = 10 , hide_private = True
303+ variables : Callable , n : Optional [int ] = 10 , hide_private = True
258304) -> DataFrame :
259305 """
260306 Shows the sizes of the active variables.
261307
262308 Parameters
263309 ----------
264- variables: locals() or globals(), optional
265- Whether to shows local or global variables, by default globals()
310+ variables: locals() or globals()
311+ Whether to shows local or global variables
266312 n: int, optional
267313 number of variables to show, by default
268314 hide_private: bool, optional
@@ -272,10 +318,20 @@ def top_mem_vars(
272318 -------
273319 DataFrame
274320 dataframe with variables names and sizes
275-
321+ Examples
322+ --------
323+ >>> import numpy as np
324+ >>> from pymove.utils.mem import top_mem_vars
325+ >>> arr = np.arange(100000, dtype=np.float64)
326+ >>> long_string = 'Hello World!' * 100
327+ >>> top_mem_vars(locals())
328+ var mem
329+ 0 arr 781.4 KiB
330+ 1 long_string 1.2 KiB
331+ 2 local 416.0 B
332+ 3 top_mem_vars 136.0 B
333+ 4 np 72.0 B
276334 """
277- if variables is None :
278- variables = globals ()
279335 vars_ = ((name , getsizeof (value )) for name , value in variables .items ())
280336 if hide_private :
281337 vars_ = filter (lambda x : not x [0 ].startswith ('_' ), vars_ )
0 commit comments