11"""Logging and Profiling."""
2+ from collections .abc import Iterable
23from datetime import datetime
34from platform import python_version
45from sys import stdout
56from time import time as get_time
67
7- from packaging .version import parse
8-
98from anndata .logging import get_memory_usage
109
1110from scvelo import settings
@@ -114,9 +113,6 @@ def msg(
114113 _write_log (get_memory_usage (), end = end )
115114
116115
117- m = msg
118-
119-
120116# TODO: Add docstrings
121117def _write_log (* msg , end = "\n " ):
122118 """Write message to log output, ignoring the verbosity level.
@@ -128,7 +124,7 @@ def _write_log(*msg, end="\n"):
128124 msg
129125 One or more arguments to be formatted as string. Same behavior as print function.
130126 """
131- from .settings import logfile
127+ from scvelo .settings import logfile
132128
133129 if logfile == "" :
134130 print (* msg , end = end )
@@ -166,12 +162,6 @@ def get_passed_time():
166162 return elapsed
167163
168164
169- # TODO: Add docstrings
170- def print_passed_time ():
171- """TODO."""
172- return _sec_to_str (get_passed_time ())
173-
174-
175165# TODO: Finish docstrings
176166def timeout (func , args = (), timeout_duration = 2 , default = None , ** kwargs ):
177167 """Spwans thread and runs the given function using the args, kwargs, and return default value on timeout."""
@@ -191,77 +181,56 @@ def run(self):
191181 return it .result
192182
193183
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-
224184# TODO: Add docstrings
225185def print_version ():
226186 """TODO."""
227187 from . import __version__
228188
229189 _write_log (
230190 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' )} ." ,
232192 )
233- check_if_latest_version ()
234193
235194
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 )
252215 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__
255218 except (ImportError , AttributeError ):
256219 pass
257- _write_log ("" )
258- check_if_latest_version ()
259220
260221
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+ )
265234
266235
267236# TODO: Add docstrings
@@ -315,28 +284,3 @@ def finish(self):
315284 if settings .verbosity > 1 :
316285 stdout .write ("\r " )
317286 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