Skip to content

Commit

Permalink
update logger documentation; update doc site
Browse files Browse the repository at this point in the history
  • Loading branch information
haozhu233 committed Mar 22, 2024
1 parent 0030511 commit bba42ce
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 52 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_build/
.DS_Store
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'renku'
html_static_path = ['_static']
html_theme = 'sphinx_book_theme'
html_static_path = []
34 changes: 20 additions & 14 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
.. regdiffusion documentation master file, created by
sphinx-quickstart on Thu Mar 21 15:45:33 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
RegDiffusion
=============

Welcome to regdiffusion's documentation!
========================================
RegDiffusion is a very fast regulatory network inference algorithm based on
probabilistic diffusion model. It works well on genes and is capable to predict
biologically verifiable links from single cell RNA-seq data.

Inference on networks with 15,000 genes takes under 5 minutes with modern
accelerated computing.

Installation
============
``regdiffusion`` is available on pypi:

pip install regdiffusion

.. toctree::
:maxdepth: 2
:caption: Contents:
:caption: Get Started:

modules
quick_tour

Indices and tables
==================
.. toctree::
:caption: References:
:titlesonly:

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
modules
7 changes: 7 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
regdiffusion
============

.. toctree::
:maxdepth: 4

regdiffusion
2 changes: 2 additions & 0 deletions docs/quick_tour.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Getting Startted with GRN inference using diffusion model
=========================================================
37 changes: 37 additions & 0 deletions docs/regdiffusion.data.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
regdiffusion.data package
=========================

Submodules
----------

regdiffusion.data.beeline module
--------------------------------

.. automodule:: regdiffusion.data.beeline
:members:
:undoc-members:
:show-inheritance:

regdiffusion.data.microglia module
----------------------------------

.. automodule:: regdiffusion.data.microglia
:members:
:undoc-members:
:show-inheritance:

regdiffusion.data.utils module
------------------------------

.. automodule:: regdiffusion.data.utils
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: regdiffusion.data
:members:
:undoc-members:
:show-inheritance:
21 changes: 21 additions & 0 deletions docs/regdiffusion.models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
regdiffusion.models package
===========================

Submodules
----------

regdiffusion.models.regdiffusion module
---------------------------------------

.. automodule:: regdiffusion.models.regdiffusion
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: regdiffusion.models
:members:
:undoc-members:
:show-inheritance:
54 changes: 54 additions & 0 deletions docs/regdiffusion.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
regdiffusion package
====================

Subpackages
-----------

.. toctree::
:maxdepth: 4

regdiffusion.data
regdiffusion.models

Submodules
----------

regdiffusion.evaluator module
-----------------------------

.. automodule:: regdiffusion.evaluator
:members:
:undoc-members:
:show-inheritance:

regdiffusion.grn module
-----------------------

.. automodule:: regdiffusion.grn
:members:
:undoc-members:
:show-inheritance:

regdiffusion.logger module
--------------------------

.. automodule:: regdiffusion.logger
:members:
:undoc-members:
:show-inheritance:

regdiffusion.trainer module
---------------------------

.. automodule:: regdiffusion.trainer
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: regdiffusion
:members:
:undoc-members:
:show-inheritance:
97 changes: 61 additions & 36 deletions regdiffusion/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,24 @@
import datetime
import pandas as pd
import numpy as np
from typing import List, Dict, Union

class LightLogger:
''' A lightweight logger that runs completely in local
'''
A lightweight logger that runs completely in local
This logger takes inspirations from w&b but runs completely in local
environment. Also, it supports logging multiple separated runs in
a single experiment.
Parameters
----------
result_dir: str
Path to the dir to save all the logging files
log_date: str
Within result_dir, logs from each date will be saved in each
subdirectory. This log_date variable provides a way to customize
this setting
Methods
-------
set_configs(configs)
Save experiment configurations (a python dictionary) to memory for
future exportation
start(note=None)
Start the logging of a new run within an experiment
log(log_dict, step=None)
Log `log_dict` (a dictionary containing performance) at each step
finish(save_now=True)
End the logging of a run and save to a local file if `save_now` is
True
to_df(tidy=True)
Convert saved logs to a pandas dataframe
save(path)
Save all the logs to path
Args:
result_dir (str): Path to the dir to save all the logging files
log_date (str): Within result_dir, logs from each date will be saved in
each subdirectory. This log_date variable provides a way to customize
this setting.
'''

def __init__(self, result_dir='result_logs', log_date=None):
def __init__(self, result_dir: str = 'result_logs',
log_date: str = None):
if log_date is None:
log_date = str(datetime.date.today())
self.result_dir = result_dir
Expand All @@ -53,12 +35,27 @@ def __init__(self, result_dir='result_logs', log_date=None):
self.logging_vars = set()
self.early_stopping_min = None

def set_configs(self, configs):
def set_configs(self, configs: Dict):
"""
Save experiment configurations (a python dictionary) to memory for
future exportation
Args:
configs (dict): A python dictionary saving all the experimental
details. For example, the hyper parameters.
"""
self.configs = configs

def start(self, note=None):
def start(self, note: str = None):
"""
Start the logging of a new run within an experiment
Args:
note (str): A name for a new log stream.
"""
if note is None:
note = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f") + str(np.random.choice(100000))
note = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
note += str(np.random.choice(100000))
self.current_log = note
self.mem[note] = {'time': note}
for k in self.configs.keys():
Expand All @@ -68,7 +65,14 @@ def start(self, note=None):
return note


def log(self, log_dict, step=None):
def log(self, log_dict: Dict, step: int = None):
"""
Log `log_dict` (a dictionary containing performance) at each step
Args:
log_dict (dict): A python dictionary (with performances) to log
step (int): Which step to log on.
"""
if step is None:
step = self.mem[self.current_log]['current_step'] + 1
self.mem[self.current_log]['current_step'] = step
Expand All @@ -77,7 +81,7 @@ def log(self, log_dict, step=None):
self.mem[self.current_log]['log'][step][k] = log_dict[k]
self.logging_vars.add(k)

def check_early_stopping(self, item, k=10):
def check_early_stopping(self, item: str, k: int = 10):
end_idx = self.mem[self.current_log]['current_step']
window = []
start_idx = max(end_idx-k, 0)
Expand All @@ -94,13 +98,28 @@ def check_early_stopping(self, item, k=10):
self.early_stopping_min = min(window)
return False

def finish(self, save_now=True):
def finish(self, save_now: bool = True):
"""
End the logging of a run and save to a local file if `save_now` is
True.
Args:
save_now (bool): whether to dump the current log stream's memory
to a local memory file.
"""
if save_now:
with open(f'{self.log_dir}/{self.current_log}.json', 'w') as f:
json.dump(self.mem[self.current_log], f)
self.current_log = None

def to_df(self, tidy=True):
def to_df(self, tidy: bool = True):
"""
Convert saved logs to a pandas dataframe
Args:
tidy (bool): Whether to convert the df to a tidy format. Default
is true.
"""
export_df = pd.DataFrame(self.mem).transpose().reset_index()
if tidy:
export_df['steps'] = export_df['log'].map(lambda x: list(x.keys()))
Expand All @@ -113,7 +132,13 @@ def to_df(self, tidy=True):
['steps'] + list(self.logging_vars), ignore_index=True)
return export_df

def save(self, path):
def save(self, path: str):
"""
Save all the logs to path
Args:
path (str): The file path used to save the logs.
"""
export = {}
export['result_dir'] = self.result_dir
export['log_dir'] = self.log_dir
Expand Down

0 comments on commit bba42ce

Please sign in to comment.