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 """
@@ -40,22 +35,20 @@ def reduce_mem_usage_automatic(df: DataFrame):
4035
4136 Examples
4237 --------
38+ >>> import numpy as np
39+ >>> import pandas as pd
4340 >>> from pymove.utils.mem import reduce_mem_usage_automatic
44- >>> df
45- lat lon datetime id
46- 0 39.984094 116.319236 2008-10-23 05:53:05 1
47- 1 39.984198 116.319322 2008-10-23 05:53:06 1
48- 2 39.984224 116.319402 2008-10-23 05:53:11 1
49- 3 39.984211 116.319389 2008-10-23 05:53:16 1
50- 4 39.984217 116.319422 2008-10-23 05:53:21 1
51- 5 39.984710 116.319865 2008-10-23 05:53:23 1
52- 6 39.984674 116.319810 2008-10-23 05:53:28 1
53- 7 39.984623 116.319773 2008-10-23 05:53:33 1
54- 8 39.984606 116.319732 2008-10-23 05:53:38 1
41+ >>> df = pd.DataFrame({'col_1': np.arange(10000, dtype=np.float64)})
42+ >>> df.dtytes
43+ col_1 float64
44+ dtype: object
5545 >>> reduce_mem_usage_automatic(df)
56- Memory usage of dataframe is 0.00 MB
57- Memory usage after optimization is: 0.00 MB
58- Decreased by 26.0 %
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
5952 """
6053 start_mem = df .memory_usage ().sum () / 1024 ** 2
6154 logger .info ('Memory usage of dataframe is {:.2f} MB' .format (start_mem ))
@@ -158,20 +151,12 @@ def total_size(
158151
159152 Examples
160153 --------
154+ >>> import numpy as np
161155 >>> from pymove.utils.mem import total_size
162- >>> df
163- lat lon datetime id
164- 0 39.984094 116.319236 2008-10-23 05:53:05 1
165- 1 39.984198 116.319322 2008-10-23 05:53:06 1
166- 2 39.984224 116.319402 2008-10-23 05:53:11 1
167- 3 39.984211 116.319389 2008-10-23 05:53:16 1
168- 4 39.984217 116.319422 2008-10-23 05:53:21 1
169- 5 39.984710 116.319865 2008-10-23 05:53:23 1
170- 6 39.984674 116.319810 2008-10-23 05:53:28 1
171- 7 39.984623 116.319773 2008-10-23 05:53:33 1
172- 8 39.984606 116.319732 2008-10-23 05:53:38 1
173- >>> total_size(df)
174- Size in bytes: 432, Type: <class 'pandas.core.frame.DataFrame'>
156+ >>> arr = np.arange(10000, dtype=np.float64)
157+ >>> sz = total_size(arr)
158+ 'Size in bytes: 80104, Type: <class 'numpy.ndarray'>'
159+ >>> sz
175160 432
176161 """
177162 if handlers is None :
@@ -232,16 +217,14 @@ def begin_operation(name: Text) -> Dict:
232217 Examples
233218 --------
234219 >>> from pymove.utils.mem import begin_operation
235- >>> print(begin_operation('move_data'), type(begin_operation('move_data')))
236- {'process': psutil.Process(pid=103401, name='python',
237- status='running', started='21:48:11'),
238- 'init': 293732352, 'start': 1622082973.8825781, 'name': 'move_data'}
239- <class 'dict'>
240- >>> print(begin_operation('mdf'), type(begin_operation('mdf')))
241- {'process': psutil.Process(pid=103401, name='python',
242- status='running', started='21:48:11'),
243- 'init': 293732352, 'start': 1622082973.8850513, 'name': 'mdf'}
244- <class 'dict'>
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+ }
245228 """
246229 process = psutil .Process (os .getpid ())
247230 init = process .memory_info ()[0 ]
@@ -265,15 +248,14 @@ def end_operation(operation: Dict) -> Dict:
265248
266249 Examples
267250 --------
268- >>> from pymove.utils.mem import end_operation
269- >>> stats = {'process': psutil.Process(pid=103401, name='python',
270- status='running', started='21:48:11'),
271- 'init': 293732352,
272- 'start': 1622083075.4811873,
273- 'name': 'move_data'}
274- >>> print(end_operation(stats), type(end_operation(stats)))
275- {'name': 'move_data', 'time in seconds': 0.0014350414276123047,
276- 'memory': '0.0 B'} <class 'dict'>
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'}
277259 """
278260 finish = operation ['process' ].memory_info ()[0 ]
279261 last_operation_name = operation ['name' ]
@@ -305,8 +287,10 @@ def sizeof_fmt(mem_usage: int, suffix: Optional[Text] = 'B') -> Text:
305287 Examples
306288 --------
307289 >>> from pymove.utils.mem import sizeof_fmt
308- >>> print(sizeof_fmt(6.64,'MB'), type(sizeof_fmt(6.64,'MB')))
309- 6.6 MB <class 'str'>
290+ >>> sizeof_fmt(1024)
291+ 1.0 KiB
292+ >>> sizeof_fmt(2e6)
293+ 1.9 MiB
310294 """
311295 for unit in ['' , 'Ki' , 'Mi' , 'Gi' , 'Ti' , 'Pi' , 'Ei' , 'Zi' ]:
312296 if abs (mem_usage ) < 1024.0 :
@@ -316,15 +300,15 @@ def sizeof_fmt(mem_usage: int, suffix: Optional[Text] = 'B') -> Text:
316300
317301
318302def top_mem_vars (
319- variables : Optional [ Callable ] = None , n : Optional [int ] = 10 , hide_private = True
303+ variables : Callable , n : Optional [int ] = 10 , hide_private = True
320304) -> DataFrame :
321305 """
322306 Shows the sizes of the active variables.
323307
324308 Parameters
325309 ----------
326- variables: locals() or globals(), optional
327- Whether to shows local or global variables, by default globals()
310+ variables: locals() or globals()
311+ Whether to shows local or global variables
328312 n: int, optional
329313 number of variables to show, by default
330314 hide_private: bool, optional
@@ -336,23 +320,18 @@ def top_mem_vars(
336320 dataframe with variables names and sizes
337321 Examples
338322 --------
323+ >>> import numpy as np
339324 >>> from pymove.utils.mem import top_mem_vars
340- >>> print(top_mem_vars(globals()), type(top_mem_vars(globals())))
341- var mem
342- 0 Out 1.1 KiB
343- 1 In 776.0 B
344- 2 df2 432.0 B
345- 3 df 304.0 B
346- 4 stats 232.0 B
347- 5 reduce_mem_usage_automatic 136.0 B
348- 6 total_size 136.0 B
349- 7 begin_operation 136.0 B
350- 8 end_operation 136.0 B
351- 9 sizeof_fmt 136.0 B
352- <class 'pandas.core.frame.DataFrame'>
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
353334 """
354- if variables is None :
355- variables = globals ()
356335 vars_ = ((name , getsizeof (value )) for name , value in variables .items ())
357336 if hide_private :
358337 vars_ = filter (lambda x : not x [0 ].startswith ('_' ), vars_ )
0 commit comments