1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
|CI| |coverall| |Using Ruff| |Python 3.9+|
.. |CI| image:: https://github.com/HuttleyLab/scitrack/actions/workflows/testing_develop.yml/badge.svg
:target: https://github.com/HuttleyLab/scitrack/actions/workflows/testing_develop.yml
.. |coverall| image:: https://coveralls.io/repos/github/GavinHuttley/scitrack/badge.svg?branch=develop
:target: https://coveralls.io/github/GavinHuttley/scitrack?branch=develop
.. |Using Ruff| image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
:target: https://github.com/astral-sh/ruff
.. |Python 3.9+| image:: https://img.shields.io/badge/python-3.9+-blue.svg
:target: https://www.python.org/downloads/release/python-390/
##################
About ``scitrack``
##################
One of the critical challenges in scientific analysis is to track all the elements involved. This includes the arguments provided to a specific application (including default values), input data files referenced by those arguments and output data generated by the application. In addition to this, tracking a minimal set of system specific information.
``scitrack`` is a simple package aimed at researchers writing scripts, or more substantial scientific software, to support the tracking of scientific computation. The package provides elementary functionality to support logging. The primary capabilities concern generating checksums on input and output files and facilitating logging of the computational environment.
To see some projects using ``scitrack``, see the "Used by" link at the top of the `project GitHub page <https://github.com/HuttleyLab/scitrack>`_.
**********
Installing
**********
For the released version::
$ pip install scitrack
For the very latest version::
$ pip install git+https://github.com/HuttleyLab/scitrack
Or clone it::
$ git clone git@github.com:HuttleyLab/scitrack.git
And then install::
$ pip install ~/path/to/scitrack
*****************
``CachingLogger``
*****************
There is a single object provided by ``scitrack``, ``CachingLogger``. This object is basically a wrapper around the Python standard library ``logging`` module. On invocation, ``CachingLogger`` captures basic information regarding the system and the command line call that was made to invoke the application.
In addition, the class provides convenience methods for logging both the path and the md5 hexdigest checksum [1]_ of input/output files. A method is also provided for producing checksums of text data. The latter is useful for the case when data are from a stream or a database, for instance.
All logging calls are cached until a path for a logfile is provided. The logger can also, optionally, create directories.
**********************************
Simple instantiation of the logger
**********************************
Creating the logger. Setting ``create_dir=True`` means on creation of the logfile, the directory path will be created also.
.. code:: python
from scitrack import CachingLogger
LOGGER = CachingLogger(create_dir=True)
LOGGER.log_file_path = "somedir/some_path.log"
The last assignment triggers creation of ``somedir/some_path.log``.
.. warning::
Once set, a loggers ``.log_file_path`` cannot be changed.
******************************************
Capturing a programs arguments and options
******************************************
``scitrack`` will write the contents of ``sys.argv`` to the log file, prefixed by ``command_string``. However, this only captures arguments specified on the command line. Tracking the value of optional arguments not specified, which may have default values, is critical to tracking the full command set. Doing this is now easy with the simple statement ``LOGGER.log_args()``. The logger can also record the versions of named dependencies.
Here's one approach to incorporating ``scitrack`` into a command line application built using the ``click`` `command line interface library <http://click.pocoo.org/>`_. Below we create a simple ``click`` app and capture the required and optional argument values.
.. note::
``LOGGER.log_args()`` should be called immediately after the function definition, or after "true" default values have been configured.
.. code:: python
import click
from scitrack import CachingLogger
LOGGER = CachingLogger()
@click.command()
@click.option("-i", "--infile", type=click.Path(exists=True))
@click.option("-t", "--test", is_flag=True, help="Run test.")
def main(infile, test):
# capture the local variables, at this point just provided arguments
LOGGER.log_args()
LOGGER.log_versions("numpy")
LOGGER.input_file(infile)
LOGGER.log_file_path = "some_path.log"
if __name__ == "__main__":
main()
The ``CachingLogger.write()`` method takes a message and a label. All other logging methods wrap ``log_message()``, providing a specific label. For instance, the method ``input_file()`` writes out two lines in the log.
- ``input_file_path``, the absolute path to the intput file
- ``input_file_path md5sum``, the hex digest of the file
``output_file()`` behaves analogously. An additional method ``text_data()`` is useful for other data input/output sources (e.g. records from a database). For this to have value for arbitrary data types requires a systematic approach to ensuring the text conversion is robust across platforms.
The ``log_args()`` method captures all local variables within a scope.
The ``log_versions()`` method captures versions for the current file and that of a list of named packages, e.g. ``LOGGER.log_versions(['numpy', 'sklearn'])``.
Some sample output
==================
::
2020-05-25 13:32:07 Eratosthenes:98447 INFO system_details : system=Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64
2020-05-25 13:32:07 Eratosthenes:98447 INFO python : 3.8.2
2020-05-25 13:32:07 Eratosthenes:98447 INFO user : gavin
2020-05-25 13:32:07 Eratosthenes:98447 INFO command_string : ./demo.py -i /Users/gavin/repos/SciTrack/tests/sample-lf.fasta
2020-05-25 13:32:07 Eratosthenes:98447 INFO params : {'infile': '/Users/gavin/repos/SciTrack/tests/sample-lf.fasta', 'test': False}
2020-05-25 13:32:07 Eratosthenes:98447 INFO version : __main__==None
2020-05-25 13:32:07 Eratosthenes:98447 INFO version : numpy==1.18.4
2020-05-25 13:32:07 Eratosthenes:98447 INFO input_file_path : /Users/gavin/repos/SciTrack/tests/sample-lf.fasta
2020-05-25 13:32:07 Eratosthenes:98447 INFO input_file_path md5sum : 96eb2c2632bae19eb65ea9224aaafdad
**********************
Other useful functions
**********************
Two other useful functions are ``get_file_hexdigest`` and ``get_text_hexdigest``.
****************
Reporting issues
****************
Use the project `issue tracker <https://github.com/HuttleyLab/scitrack/issues>`_.
**************
For Developers
**************
We use flit_ for package building. Having cloned the repository onto your machine. Install ``flit``::
$ python3 -m pip install flit
Do a developer install of ``scitrack`` using flit as::
$ cd path/to/cloned/repo
$ flit install -s --python `which python`
.. note:: This installs a symlink into ``site-packages`` of the python identified by ``which python``.
.. [1] The hexdigest serves as a unique signature of a files contents.
.. _flit: https://flit.readthedocs.io/en/latest/
|