Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Loguru #62

Merged
merged 32 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6dd3ce2
Add loguru exception catching
maestroque Apr 15, 2024
d33b5ac
Integrate loguru for warning and info logs
maestroque Apr 15, 2024
72c427b
Add loguru in requirements
maestroque Apr 15, 2024
a9b0c18
Add loguru to setup.cfg
maestroque May 13, 2024
acd31cd
Disable loguru logs for library users
maestroque May 22, 2024
537f48a
Add verbose option in CLI
maestroque May 22, 2024
bb7ce79
Fix loguru disabling for library users
maestroque May 23, 2024
7070d70
Add logger enabling and configuring
maestroque May 23, 2024
405254c
Leave logger.catch decorator only for the CLI workflow function
maestroque May 23, 2024
cbfc013
Remove unnecessary imports
maestroque May 23, 2024
4d8ffa3
Add loguru as a main dependency
maestroque May 27, 2024
3724928
Reformat using isort and black
maestroque May 27, 2024
333e94b
CLI: minor fixes for verbose argument
maestroque May 27, 2024
afcc380
Remove Gooey from draft CLI implementation
maestroque Jun 1, 2024
be82040
Minor comment deletion and reformatting
maestroque Jun 1, 2024
7fcb3ab
Add log level choice option in CLI
maestroque Jun 1, 2024
3253566
Add logfile sink
maestroque Jun 1, 2024
8dd3166
Add --verbose flag in the CLI
maestroque Jun 1, 2024
cd22a43
Add log level choosing for module usage
maestroque Jun 1, 2024
2ac91a3
Add info and debug logs physio.py and operations.py functions
maestroque Jun 1, 2024
81750e3
Add info logs on editor.py and io.py
maestroque Jun 2, 2024
7f8fd6a
Fix warning catching assertions in tests
maestroque Jun 4, 2024
86adcf3
Improve warning log assertions when there are multiple expected warni…
maestroque Jun 4, 2024
b52d13a
Style fix
maestroque Jun 4, 2024
5af076b
Updating from master
maestroque Jun 6, 2024
c568cb2
Remove unused imports
maestroque Jun 6, 2024
6c4dcde
Merge remote-tracking branch 'origin/master' into add-loguru
maestroque Jun 12, 2024
a78544c
Fix loguru not detecting module's name
maestroque Jun 13, 2024
a008c72
Add more logger utilities. Fix multiple logger instance bug
maestroque Jun 13, 2024
8f6b944
Remove commented out line
maestroque Jun 14, 2024
b865617
Fix imports in cli/run.py
maestroque Jun 14, 2024
145d48f
Make --quiet and --debug cli options mutually exclusive, remove matpl…
maestroque Jun 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions peakdet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"__version__",
]

from loguru import logger

from peakdet.analytics import HRV
from peakdet.external import load_rtpeaks
from peakdet.io import load_history, load_physio, save_history, save_physio
Expand All @@ -34,3 +36,5 @@

__version__ = get_versions()["version"]
del get_versions

logger.disable("peakdet")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I'm thick about this, but why are we importing and then disabling for peakdet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that the loguru logger is disabled by default for library users, so they can enable it explicitly if they want to. This is not the case for the CLI however

127 changes: 99 additions & 28 deletions peakdet/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import glob
import os
import sys
import warnings

import matplotlib

matplotlib.use("WXAgg")
from gooey import Gooey, GooeyParser
import argparse
import datetime

from loguru import logger
maestroque marked this conversation as resolved.
Show resolved Hide resolved

# from gooey import Gooey, GooeyParser
maestroque marked this conversation as resolved.
Show resolved Hide resolved
import peakdet

TARGET = "pythonw" if sys.platform == "darwin" else "python"
Expand Down Expand Up @@ -41,19 +44,11 @@
}


@Gooey(
program_name="Physio pipeline",
program_description="Physiological processing pipeline",
default_size=(800, 600),
target=TARGET,
)
def get_parser():
"""Parser for GUI and command-line arguments"""
parser = GooeyParser()
parser = argparse.ArgumentParser()
parser.add_argument(
"file_template",
metavar="Filename template",
widget="FileChooser",
help="Select a representative file and replace all "
'subject-specific information with a "?" symbol.'
"\nFor example, subject_001_data.txt should "
Expand All @@ -67,28 +62,24 @@ def get_parser():
)
inp_group.add_argument(
"--modality",
metavar="Modality",
default="ECG",
choices=list(MODALITIES.keys()),
help="Modality of input data.",
)
inp_group.add_argument(
"--fs",
metavar="Sampling rate",
default=1000.0,
type=float,
help="Sampling rate of input data.",
)
inp_group.add_argument(
"--source",
metavar="Source",
default="rtpeaks",
choices=list(LOADERS.keys()),
help="Program used to collect the data.",
)
inp_group.add_argument(
"--channel",
metavar="Channel",
default=1,
type=int,
help="Which channel of data to read from data "
Expand All @@ -102,7 +93,6 @@ def get_parser():
out_group.add_argument(
"-o",
"--output",
metavar="Filename",
default="peakdet.csv",
help="Output filename for generated measurements.",
)
Expand All @@ -111,16 +101,13 @@ def get_parser():
"--measurements",
metavar="Measurements",
nargs="+",
widget="Listbox",
choices=list(ATTR_CONV.keys()),
default=["Average NN intervals", "Standard deviation of NN intervals"],
help="Desired physiological measurements.\nChoose "
"multiple with shift+click or ctrl+click.",
help="Desired physiological measurements",
)
out_group.add_argument(
"-s",
"--savehistory",
metavar="Save history",
action="store_true",
help="Whether to save history of data processing " "for each file.",
)
Expand All @@ -132,22 +119,45 @@ def get_parser():
edit_group.add_argument(
"-n",
"--noedit",
metavar="Editing",
action="store_true",
help="Turn off interactive editing.",
)
edit_group.add_argument(
"-t",
"--thresh",
metavar="Threshold",
default=0.2,
type=float,
help="Threshold for peak detection algorithm.",
)
edit_group.add_argument(
"-debug",
"--debug",
dest="debug",
action="store_true",
help="Only print debugging info to log file. Default is False.",
default=False,
)
edit_group.add_argument(
"-quiet",
"--quiet",
dest="quiet",
action="store_true",
help="Only print warnings to log file. Default is False.",
default=False,
)
edit_group.add_argument(
"-verbose",
"--verbose",
dest="verbose",
action="store_true",
help="Print verbose error logs with diagnostics",
default=False,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add these three into a mutually exclusive group of arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be good now

image

image


return parser


@logger.catch
def workflow(
*,
file_template,
Expand All @@ -159,7 +169,10 @@ def workflow(
savehistory=True,
noedit=False,
thresh=0.2,
measurements=ATTR_CONV.keys()
measurements=ATTR_CONV.keys(),
verbose=False,
debug=False,
quiet=False,
):
"""
Basic workflow for physiological data
Expand Down Expand Up @@ -190,17 +203,74 @@ def workflow(
measurements : list, optional
Which HRV-related measurements to save from data. See ``peakdet.HRV``
for available measurements. Default: all available measurements.
verbose : bool, optional
Whether to include verbose logs when catching exceptions that include diagnostics
"""
outdir = os.path.dirname(output)
logger.info(f"Current path is {outdir}")

# Create logfile name
basename = "peakdet"
extension = "log"
isotime = datetime.datetime.now().strftime("%Y-%m-%dT%H%M%S")
logname = os.path.join(outdir, (basename + isotime + "." + extension))

logger.remove(0)
if quiet:
logger.add(
sys.stderr,
level="WARNING",
colorize=True,
backtrace=verbose,
diagnose=verbose,
)
logger.add(
logname,
level="WARNING",
colorize=False,
backtrace=verbose,
diagnose=verbose,
)
elif debug:
logger.add(
sys.stderr,
level="DEBUG",
colorize=True,
backtrace=verbose,
diagnose=verbose,
)
logger.add(
logname,
level="DEBUG",
colorize=False,
backtrace=verbose,
diagnose=verbose,
)
else:
logger.add(
sys.stderr,
level="INFO",
colorize=True,
backtrace=verbose,
diagnose=verbose,
)
logger.add(
logname,
level="INFO",
colorize=False,
backtrace=verbose,
diagnose=verbose,
)

# output file
print("OUTPUT FILE:\t\t{}\n".format(output))
logger.info("OUTPUT FILE:\t\t{}".format(output))
# grab files from file template
print("FILE TEMPLATE:\t{}\n".format(file_template))
logger.info("FILE TEMPLATE:\t{}".format(file_template))
files = glob.glob(file_template, recursive=True)

# convert measurements to peakdet.HRV attribute friendly names
try:
print("REQUESTED MEASUREMENTS: {}\n".format(", ".join(measurements)))
logger.info("REQUESTED MEASUREMENTS: {}\n".format(", ".join(measurements)))
except TypeError:
raise TypeError(
"It looks like you didn't select any of the options "
Expand All @@ -221,7 +291,7 @@ def workflow(
# requested on command line, warn and use existing measurements so
# as not to totally fork up existing file
if eheader != head:
warnings.warn(
logger.warning(
"Desired output file already exists and requested "
"measurements do not match with measurements in "
"existing output file. Using the pre-existing "
Expand All @@ -238,7 +308,7 @@ def workflow(
# iterate through all files and do peak detection with manual editing
for fname in files:
fname = os.path.relpath(fname)
print("Currently processing {}".format(fname))
logger.info("Currently processing {}".format(fname))

# if we want to save history, this is the output name it would take
outname = os.path.join(
Expand Down Expand Up @@ -281,6 +351,7 @@ def workflow(


def main():
logger.enable("")
opts = get_parser().parse_args()
workflow(**vars(opts))

Expand Down
3 changes: 3 additions & 0 deletions peakdet/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import matplotlib.pyplot as plt
import numpy as np
from loguru import logger
from matplotlib.widgets import SpanSelector

from peakdet import operations, utils
Expand Down Expand Up @@ -142,6 +143,7 @@ def on_edit(self, xmin, xmax, *, method):

method accepts 'insert', 'reject', 'delete'
"""
logger.debug("Edited peaks with action: {}", method)
if method not in ["insert", "reject", "delete"]:
raise ValueError(f'Action "{method}" not supported.')

Expand Down Expand Up @@ -184,6 +186,7 @@ def undo(self):

# pop off last edit and delete
func, peaks = self.data._history.pop()
logger.debug(f"Undo previous action: {func}")

if func == "reject_peaks":
self.data._metadata["reject"] = np.setdiff1d(
Expand Down
5 changes: 2 additions & 3 deletions peakdet/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
Functions for interacting with physiological data acquired by external packages
"""

import warnings

import numpy as np
from loguru import logger

from peakdet import physio, utils

Expand Down Expand Up @@ -40,7 +39,7 @@ def load_rtpeaks(fname, channel, fs):
"""

if fname.startswith("/"):
warnings.warn(
logger.warning(
"Provided file seems to be an absolute path. In order "
"to ensure full reproducibility it is recommended that "
"a relative path is provided."
Expand Down
18 changes: 13 additions & 5 deletions peakdet/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import json
import os.path as op
import warnings

import numpy as np
from loguru import logger

from peakdet import physio, utils

Expand Down Expand Up @@ -61,18 +61,23 @@ def load_physio(data, *, fs=None, dtype=None, history=None, allow_pickle=False):
inp["history"] = list(map(tuple, inp["history"]))
except (IOError, OSError, ValueError):
inp = dict(data=np.loadtxt(data), history=[utils._get_call(exclude=[])])
logger.debug("Instantiating Physio object from a file")
phys = physio.Physio(**inp)
# if we got a numpy array, load that into a Physio object
elif isinstance(data, np.ndarray):
logger.debug("Instantiating Physio object from numpy array")
if history is None:
warnings.warn(
logger.warning(
"Loading data from a numpy array without providing a"
"history will render reproducibility functions "
"useless! Continuing anyways."
)
phys = physio.Physio(np.asarray(data, dtype=dtype), fs=fs, history=history)
# create a new Physio object out of a provided Physio object
elif isinstance(data, physio.Physio):
logger.debug(
"Instantiating a new Physio object from the provided Physio object"
)
phys = utils.new_physio_like(data, data.data, fs=fs, dtype=dtype)
phys._history += [utils._get_call()]
else:
Expand All @@ -81,7 +86,7 @@ def load_physio(data, *, fs=None, dtype=None, history=None, allow_pickle=False):
# reset sampling rate, as requested
if fs is not None and fs != phys.fs:
if not np.isnan(phys.fs):
warnings.warn(
logger.warning(
"Provided sampling rate does not match loaded rate. "
"Resetting loaded sampling rate {} to provided {}".format(phys.fs, fs)
)
Expand Down Expand Up @@ -119,6 +124,7 @@ def save_physio(fname, data):
np.savez_compressed(
dest, data=data.data, fs=data.fs, history=hist, metadata=data._metadata
)
logger.info(f"Saved {data} in {fname}")

return fname

Expand Down Expand Up @@ -149,10 +155,11 @@ def load_history(file, verbose=False):
history = json.load(src)

# replay history from beginning and return resultant Physio object
logger.info(f"Replaying history from {file}")
data = None
for func, kwargs in history:
if verbose:
print("Rerunning {}".format(func))
logger.info("Rerunning {}".format(func))
# loading functions don't have `data` input because it should be the
# first thing in `history` (when the data was originally loaded!).
# for safety, check if `data` is None; someone could have potentially
Expand Down Expand Up @@ -203,13 +210,14 @@ def save_history(file, data):

data = check_physio(data)
if len(data.history) == 0:
warnings.warn(
logger.warning(
"History of provided Physio object is empty. Saving "
"anyway, but reloading this file will result in an "
"error."
)
file += ".json" if not file.endswith(".json") else ""
with open(file, "w") as dest:
json.dump(data.history, dest, indent=4)
logger.info(f"Saved {data} history in {file}")

return file
Loading