mirror of
https://github.com/Athemis/pyKinetics.git
synced 2025-04-05 14:56:02 +00:00
Adding logger
This commit is contained in:
parent
7a943cb945
commit
c90c18ee84
2 changed files with 73 additions and 12 deletions
|
@ -1,4 +1,5 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
|
@ -31,9 +32,39 @@ def parse_arguments():
|
|||
return args
|
||||
|
||||
|
||||
def initialize_logger():
|
||||
"""
|
||||
Initialization of logging subsystem. Two logging handlers are brought up:
|
||||
'fh' which logs to a log file and 'ch' which logs to standard output.
|
||||
:return logger: returns a logger instance
|
||||
"""
|
||||
logger = logging.getLogger('pyKinetics-cli')
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(logging.INFO)
|
||||
logger.addHandler(ch)
|
||||
|
||||
try:
|
||||
log_filename = 'pyKinetics-cli.log'
|
||||
fh = logging.FileHandler(log_filename, 'w')
|
||||
fh.setLevel(logging.INFO)
|
||||
logger.addHandler(fh)
|
||||
except IOError as error:
|
||||
logger.warning('WARNING: Cannot create log file! Run pyKinetics-cli'
|
||||
'from a directory to which you have write access.')
|
||||
logger.warning(error.msg)
|
||||
pass
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
def main():
|
||||
# parse command line arguments
|
||||
args = parse_arguments()
|
||||
# initialize logger
|
||||
logger = initialize_logger()
|
||||
|
||||
if args.with_hill:
|
||||
do_hill = args.with_hill
|
||||
else:
|
||||
|
@ -41,25 +72,38 @@ def main():
|
|||
try:
|
||||
input_path = Path(args.input).resolve()
|
||||
except FileNotFoundError:
|
||||
print('Path containing input data not found: {}'.format(args.input))
|
||||
logger.critical('CRITICAL: Path containing input data '
|
||||
'not found: {}'.format(args.input))
|
||||
raise
|
||||
try:
|
||||
output_path = Path(args.output).resolve()
|
||||
except FileNotFoundError:
|
||||
print('Path for writing results not found: {}'.format(args.output))
|
||||
logger.critical('CRITICAL: Path for writing results '
|
||||
'not found: {}'.format(args.output))
|
||||
raise
|
||||
|
||||
if output_path.is_dir():
|
||||
if input_path.is_dir():
|
||||
logger.info('INFO: Collecting data files')
|
||||
data_files = sorted(input_path.glob('**/*.csv'))
|
||||
msg = 'Calculating kinetics'
|
||||
if do_hill:
|
||||
msg = '{} including Hill kinetics'.format(msg)
|
||||
logger.info('INFO: {}'.format(msg))
|
||||
exp = libkinetics.Experiment(data_files, (10, 25), do_hill)
|
||||
logger.info('INFO: Plotting linear fits to data and kinetics')
|
||||
exp.plot_data(str(output_path))
|
||||
exp.plot_kinetics(str(output_path))
|
||||
logger.info('INFO: Writing results to results.csv')
|
||||
exp.write_data(str(output_path))
|
||||
else:
|
||||
raise ValueError('{} is not a directory!'.format(input_path))
|
||||
msg = '{} is not a directory!'.format(input_path)
|
||||
logger.critical('CRITICAL: '.format(msg))
|
||||
raise ValueError(msg)
|
||||
else:
|
||||
raise ValueError('{} is not a directory!'.format(output_path))
|
||||
msg = '{} is not a directory!'.format(output_path)
|
||||
logger.critical('CRITICAL: '.format(msg))
|
||||
raise ValueError(msg)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from scipy import stats, optimize
|
||||
import numpy as np
|
||||
|
@ -9,6 +10,7 @@ import csv
|
|||
class Replicate():
|
||||
|
||||
def __init__(self, x, y, owner):
|
||||
self.logger = owner.logger
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.owner = owner
|
||||
|
@ -36,6 +38,7 @@ class Replicate():
|
|||
class Measurement():
|
||||
|
||||
def __init__(self, x, y, conc, conc_unit, owner):
|
||||
self.logger = owner.logger
|
||||
self.concentration = float(conc)
|
||||
self.concentration_unit = conc_unit
|
||||
self.x = x
|
||||
|
@ -89,7 +92,9 @@ class Measurement():
|
|||
|
||||
class Experiment():
|
||||
|
||||
def __init__(self, data_files, xlim, do_hill=False):
|
||||
def __init__(self, data_files, xlim, do_hill=False, logger=None):
|
||||
|
||||
self.logger = logger
|
||||
|
||||
# collction of indepentend measurements
|
||||
self.measurements = []
|
||||
|
@ -163,7 +168,11 @@ class Experiment():
|
|||
'perr': perr,
|
||||
'x': x}
|
||||
except:
|
||||
print('Calculation of Hill kinetics failed!')
|
||||
msg = 'Calculation of Michaelis-Menten kinetics failed!'
|
||||
if self.logger:
|
||||
self.logger.error('ERROR: {}'.format(msg))
|
||||
else:
|
||||
print(msg)
|
||||
return None
|
||||
|
||||
def do_hill_kinetics(self):
|
||||
|
@ -185,7 +194,11 @@ class Experiment():
|
|||
'h': h,
|
||||
'x': x}
|
||||
except:
|
||||
print('Calculation of Hill kinetics failed!')
|
||||
msg = 'Calculation of Hill kinetics failed!'
|
||||
if self.logger:
|
||||
self.logger.error('ERROR: {}'.format(msg))
|
||||
else:
|
||||
print(msg)
|
||||
return None
|
||||
|
||||
def plot_kinetics(self, outpath):
|
||||
|
@ -200,12 +213,16 @@ class Experiment():
|
|||
fmt='ok', ms=3, fillstyle='none', label="Data with error")
|
||||
|
||||
if self.mm:
|
||||
ax.plot(self.mm['x'],
|
||||
(self.mm['vmax']*self.mm['x'])/(self.mm['Km']+self.mm['x']), 'b-', label="Michaelis-Menten")
|
||||
y = self.mm_kinetics_function(self.mm['x'],
|
||||
self.mm['vmax'],
|
||||
self.mm['Km'])
|
||||
ax.plot(self.mm['x'], y, 'b-', label="Michaelis-Menten")
|
||||
if self.hill:
|
||||
ax.plot(self.hill['x'],
|
||||
((self.hill['vmax']*(self.hill['x']**self.hill['h'])) /
|
||||
(self.hill['Kprime'] + (self.hill['x']**self.hill['h']))), 'g-', label="Hill")
|
||||
y = self.hill_kinetics_function(self.hill['x'],
|
||||
self.hill['vmax'],
|
||||
self.hill['Kprime'],
|
||||
self.hill['h'])
|
||||
ax.plot(self.hill['x'], y, 'g-', label="Hill")
|
||||
|
||||
ax.legend(loc='best', fancybox=True)
|
||||
plt.savefig('{}/kinetics.png'.format(outpath), bbox_inches='tight')
|
||||
|
|
Loading…
Add table
Reference in a new issue