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

Fix/generate physio #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 46 additions & 0 deletions physutils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 7 additions & 3 deletions physutils/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -45,16 +45,18 @@ 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."
)
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 {}:
Expand All @@ -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}")

Expand Down
6 changes: 6 additions & 0 deletions physutils/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down