1
1
"""Logging and Profiling."""
2
+ from collections .abc import Iterable
2
3
from datetime import datetime
3
4
from platform import python_version
4
5
from sys import stdout
5
6
from time import time as get_time
6
7
7
- from packaging .version import parse
8
-
9
8
from anndata .logging import get_memory_usage
10
9
11
10
from scvelo import settings
@@ -114,9 +113,6 @@ def msg(
114
113
_write_log (get_memory_usage (), end = end )
115
114
116
115
117
- m = msg
118
-
119
-
120
116
# TODO: Add docstrings
121
117
def _write_log (* msg , end = "\n " ):
122
118
"""Write message to log output, ignoring the verbosity level.
@@ -128,7 +124,7 @@ def _write_log(*msg, end="\n"):
128
124
msg
129
125
One or more arguments to be formatted as string. Same behavior as print function.
130
126
"""
131
- from .settings import logfile
127
+ from scvelo .settings import logfile
132
128
133
129
if logfile == "" :
134
130
print (* msg , end = end )
@@ -166,12 +162,6 @@ def get_passed_time():
166
162
return elapsed
167
163
168
164
169
- # TODO: Add docstrings
170
- def print_passed_time ():
171
- """TODO."""
172
- return _sec_to_str (get_passed_time ())
173
-
174
-
175
165
# TODO: Finish docstrings
176
166
def timeout (func , args = (), timeout_duration = 2 , default = None , ** kwargs ):
177
167
"""Spwans thread and runs the given function using the args, kwargs, and return default value on timeout."""
@@ -191,77 +181,56 @@ def run(self):
191
181
return it .result
192
182
193
183
194
- # TODO: Add docstrings
195
- def get_latest_pypi_version ():
196
- """TODO."""
197
- from subprocess import CalledProcessError , check_output
198
-
199
- try : # needs to work offline as well
200
- result = check_output (["pip" , "search" , "scvelo" ])
201
- return f"{ result .split ()[- 1 ]} " [2 :- 1 ]
202
- except CalledProcessError :
203
- return "0.0.0"
204
-
205
-
206
- # TODO: Add docstrings
207
- def check_if_latest_version ():
208
- """TODO."""
209
- from . import __version__
210
-
211
- latest_version = timeout (
212
- get_latest_pypi_version , timeout_duration = 2 , default = "0.0.0"
213
- )
214
- if parse (__version__ .rsplit (".dev" )[0 ]) < parse (latest_version .rsplit (".dev" )[0 ]):
215
- warn (
216
- "There is a newer scvelo version available on PyPI:\n " ,
217
- "Your version: \t \t " ,
218
- __version__ ,
219
- "\n Latest version: \t " ,
220
- latest_version ,
221
- )
222
-
223
-
224
184
# TODO: Add docstrings
225
185
def print_version ():
226
186
"""TODO."""
227
187
from . import __version__
228
188
229
189
_write_log (
230
190
f"Running scvelo { __version__ } "
231
- f"(python { python_version ()} ) on { get_date_string ( )} ." ,
191
+ f"(Python { python_version ()} ) on { datetime . now (). strftime ( '%Y-%m-%d %H:%M' )} ." ,
232
192
)
233
- check_if_latest_version ()
234
193
235
194
236
- # TODO: Add docstrings
237
- def print_versions ():
238
- """TODO."""
239
- for mod in [
240
- "scvelo" ,
241
- "scanpy" ,
242
- "anndata" ,
243
- "loompy" ,
244
- "numpy" ,
245
- "scipy" ,
246
- "matplotlib" ,
247
- "sklearn" ,
248
- "pandas" ,
249
- ]:
250
- mod_name = mod [0 ] if isinstance (mod , tuple ) else mod
251
- mod_install = mod [1 ] if isinstance (mod , tuple ) else mod
195
+ _DEPENDENCIES_NUMERICS = [
196
+ "anndata" ,
197
+ "loompy" ,
198
+ "numba" ,
199
+ "numpy" ,
200
+ "pandas" ,
201
+ "scanpy" ,
202
+ "scipy" ,
203
+ ("sklearn" , "scikit-learn" ),
204
+ ]
205
+
206
+
207
+ _DEPENDENCIES_PLOTTING = ["matplotlib" ]
208
+
209
+
210
+ # Adapted from https://github.com/theislab/cellrank/blob/main/src/cellrank/logging/_logging.py#L98-L128
211
+ def _versions_dependencies (dependencies : Iterable [str ]):
212
+ # this is not the same as the requirements!
213
+ for mod in dependencies :
214
+ mod_name , dist_name = mod if isinstance (mod , tuple ) else (mod , mod )
252
215
try :
253
- mod_version = __import__ (mod_name ). __version__
254
- _write_log ( f" { mod_install } == { mod_version } " , end = " " )
216
+ imp = __import__ (mod_name )
217
+ yield dist_name , imp . __version__
255
218
except (ImportError , AttributeError ):
256
219
pass
257
- _write_log ("" )
258
- check_if_latest_version ()
259
220
260
221
261
- # TODO: Add docstrings
262
- def get_date_string ():
263
- """TODO."""
264
- return datetime .now ().strftime ("%Y-%m-%d %H:%M" )
222
+ def print_versions ():
223
+ """Print versions of relevant packages."""
224
+ from scvelo import settings
225
+
226
+ modules = ["scvelo" ] + _DEPENDENCIES_NUMERICS + _DEPENDENCIES_PLOTTING
227
+ print (
228
+ " " .join (
229
+ f"{ module } =={ version } "
230
+ for module , version in _versions_dependencies (modules )
231
+ ),
232
+ file = settings .logfile ,
233
+ )
265
234
266
235
267
236
# TODO: Add docstrings
@@ -315,28 +284,3 @@ def finish(self):
315
284
if settings .verbosity > 1 :
316
285
stdout .write ("\r " )
317
286
stdout .flush ()
318
-
319
-
320
- def profiler (command , filename = "profile.stats" , n_stats = 10 ):
321
- """Profiler for a python program.
322
-
323
- Runs cProfile and outputs ordered statistics that describe
324
- how often and for how long various parts of the program are executed.
325
-
326
- Stats can be visualized with `!snakeviz profile.stats`.
327
-
328
- Parameters
329
- ----------
330
- command: str
331
- Command string to be executed.
332
- filename: str
333
- Name under which to store the stats.
334
- n_stats: int or None
335
- Number of top stats to show.
336
- """
337
- import cProfile
338
- import pstats
339
-
340
- cProfile .run (command , filename )
341
- stats = pstats .Stats (filename ).strip_dirs ().sort_stats ("time" )
342
- return stats .print_stats (n_stats or {})
0 commit comments