From b8cbea0a17f587f6f2e72405c6ad0f1463fbe39d Mon Sep 17 00:00:00 2001 From: Marie-Eve Picard Date: Mon, 28 Oct 2024 12:06:59 -0400 Subject: [PATCH 1/2] rm unecessary params specification --- physutils/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physutils/tasks.py b/physutils/tasks.py index 451ac89..d52ea7e 100644 --- a/physutils/tasks.py +++ b/physutils/tasks.py @@ -54,7 +54,7 @@ def generate_physio( "Could not determine input mode automatically. Please specify it manually." ) if mode == "physio": - physio_obj = load_physio(input_file, fs=fs, allow_pickle=True) + physio_obj = load_physio(input_file, allow_pickle=True) elif mode == "bids": if bids_parameters is {}: From 9ac509516efbe0a71e37a66717c355a5495ed33c Mon Sep 17 00:00:00 2001 From: Marie-Eve Picard Date: Mon, 28 Oct 2024 21:17:41 -0400 Subject: [PATCH 2/2] separate loading functions --- physutils/io.py | 46 ++++++++++++++++++++++++++++++++++++++ physutils/tasks.py | 8 +++++-- physutils/tests/test_io.py | 6 +++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/physutils/io.py b/physutils/io.py index 3cb8f79..41c0dbf 100644 --- a/physutils/io.py +++ b/physutils/io.py @@ -224,6 +224,52 @@ def load_physio(data, *, fs=None, dtype=None, history=None, allow_pickle=False): return phys +def load_misc(data, *, fs=None): + """ + Returns `Physio` object with provided data + + Parameters + ---------- + data : str, os.path.PathLike or array_like + Input physiological data. If array_like, should be one-dimensional + fs : float, optional + Sampling rate of `data`. Default: None + + Returns + ------- + phys: :class:`physutils.Physio` + Loaded physiological data + + Raises + ------ + TypeError + If provided `data` is unable to be loaded + """ + # load data + if isinstance(data, str) or isinstance(data, os.PathLike): + if os.path.exists(data): + ext = os.path.splitext(data)[-1] + + if ext == '.gz': + ext == os.path.splitext(os.path.splitext(data)[0])[-1] + + if ext == '.tsv': + data = np.genfromtxt(data, delimiter='\t') + elif ext == '.csv': + data = np.genfromtxt(data, delimiter=',') + else: + data = np.genfromtxt(data) + else: + raise IOError(f'Cannot find {data}') + else: + raise TypeError(f'{type(data)} is not a supported type') + + # instantiate physio object + phys = physio.Physio(data, fs=fs) + + return phys + + def save_physio(fname, data): """ Saves `data` to `fname` diff --git a/physutils/tasks.py b/physutils/tasks.py index d52ea7e..b0011e3 100644 --- a/physutils/tasks.py +++ b/physutils/tasks.py @@ -5,7 +5,7 @@ import logging -from .io import load_from_bids, load_physio +from .io import load_from_bids, load_physio, load_misc from .physio import Physio from .utils import is_bids_directory @@ -45,10 +45,12 @@ def generate_physio( LGR.info(f"Loading physio object from {input_file}") if mode == "auto": - if input_file.endswith((".phys", ".physio", ".1D", ".txt", ".tsv", ".csv")): + if input_file.endswith((".phys")): mode = "physio" elif is_bids_directory(input_file): mode = "bids" + elif input_file.endswith((".txt", ".tsv", ".csv")): + mode = "misc" else: raise ValueError( "Could not determine input mode automatically. Please specify it manually." @@ -64,6 +66,8 @@ def generate_physio( physio_obj = ( physio_array[col_physio_type] if col_physio_type else physio_array ) + elif mode == "misc": + physio_obj = load_misc(input_file, fs=fs) else: raise ValueError(f"Invalid generate_physio mode: {mode}") diff --git a/physutils/tests/test_io.py b/physutils/tests/test_io.py index b64078a..5f5b71c 100644 --- a/physutils/tests/test_io.py +++ b/physutils/tests/test_io.py @@ -87,6 +87,12 @@ def test_load_from_bids_no_rec(): assert phys_array[col].history[0][0] == "physutils.io.load_from_bids" +def test_load_misc(): + csv = io.load_physio(get_test_data_path("ECG.csv")) + assert isinstance(csv, physio.Physio) + assert np.isnan(csv.fs) + + def test_save_physio(tmpdir): pckl = io.load_physio(get_test_data_path("ECG.phys"), allow_pickle=True) out = io.save_physio(tmpdir.join("tmp").purebasename, pckl)