Skip to content

Commit

Permalink
Minor CLI optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
maestroque committed Aug 28, 2024
1 parent 7180576 commit c27e8d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
11 changes: 6 additions & 5 deletions phys2denoise/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _get_parser():
"--respiratory-pattern-variability",
dest="metrics",
action="append_const",
const=respiratory_pattern_variability,
const="respiratory_pattern_variability",
help="Respiratory pattern variability. Requires the following "
"input: window.",
default=[],
Expand All @@ -100,7 +100,7 @@ def _get_parser():
"--envelope",
dest="metrics",
action="append_const",
const=env,
const="env",
help="Respiratory pattern variability calculated across a sliding "
"window. Requires the following inputs: sample-rate, window and lags.",
default=[],
Expand Down Expand Up @@ -416,6 +416,7 @@ def _get_parser():
return parser


@logger.catch()
def main():
"""
Main function to run the parser.
Expand All @@ -434,8 +435,7 @@ def main():

logger.info(f"Running phys2denoise version: {__version__}")

LGR.debug(f"Arguments: {args}")
LGR.debug(f"Metrics to export: {args.metrics_to_export}")
LGR.debug(f"Arguments Provided: {args}")

if args.metrics_to_export is None or args.metrics_to_export == "all":
args.metrics_to_export = "all"
Expand All @@ -452,12 +452,13 @@ def main():
args.slice_timings = (
np.array(args.slice_timings) if args.slice_timings is not None else None
)
args.lags = np.array(args.lags) if args.lags is not None else None

metric_args = dict()
for metric in args.metrics:
metric_args[metric] = tasks.select_input_args(globals()[metric], vars(args))

logger.debug(f"Metric args: {metric_args}")
logger.debug(f"Metrics: {args.metrics}")

wf = workflow.build(
input_file=args.filename,
Expand Down
2 changes: 2 additions & 0 deletions phys2denoise/metrics/chest_belt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Denoising metrics for chest belt recordings."""
import numpy as np
import pandas as pd
from loguru import logger
from physutils import io, physio
from scipy.interpolate import interp1d
from scipy.stats import zscore
Expand Down Expand Up @@ -229,6 +230,7 @@ def _respiratory_pattern_variability(data, window):

# Convert window to Hertz
window = int(window * data.fs)
logger.debug(f"Window size in samples: {window}")

# Calculate RPV across a rolling window

Expand Down
14 changes: 14 additions & 0 deletions phys2denoise/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,17 @@ def export_metrics(
if metrics == "all":
LGR.info("Exporting all computed metrics")
for metric in phys.computed_metrics.keys():
if metric == "respiratory_pattern_variability":
LGR.warning(
f"respiratory_pattern_variability will not be exported, since it is a single value metric "
f"and not a timeseries. RPV value is {phys.computed_metrics[metric].data}"
)
continue
has_lags = (
True if "has_lags" in phys.computed_metrics[metric].args else False
)
prefix = outdir + f"/{metric}"
LGR.debug(f"Exporting {metric} to {prefix}")
export_metric(
phys.computed_metrics[metric], phys.fs, tr, prefix, has_lags=has_lags
)
Expand All @@ -131,10 +138,17 @@ def export_metrics(
if metric not in phys.computed_metrics.keys():
LGR.warning(f"Metric {metric} not computed. Skipping")
continue
if metric == "respiratory_pattern_variability":
LGR.warning(
f"respiratory_pattern_variability will not be exported, since it is a single value metric"
f"and not a timeseries. RPV value is {phys.computed_metrics[metric]}"
)
continue
has_lags = (
True if "has_lags" in phys.computed_metrics[metric].args else False
)
prefix = outdir + f"/{metric}"
LGR.debug(f"Exporting {metric} to {prefix}")
export_metric(
phys.computed_metrics[metric],
phys.fs,
Expand Down
Binary file modified phys2denoise/tests/data/fake_phys.phys
Binary file not shown.

0 comments on commit c27e8d5

Please sign in to comment.