Skip to content

Commit 34fc89d

Browse files
committed
Clean code, remove moved functions, remove pydra mask check, add docstrings, rename transform_to_physio function
1 parent a19bc19 commit 34fc89d

File tree

2 files changed

+50
-64
lines changed

2 files changed

+50
-64
lines changed

physutils/tasks.py

+40-54
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,49 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""Helper class for holding physiological data and associated metadata information."""
5+
16
import logging
2-
from functools import wraps
37

4-
from bids import BIDSLayout
5-
from loguru import logger
8+
from .io import load_from_bids, load_physio
9+
from .physio import Physio
10+
from .utils import is_bids_directory
611

7-
from physutils.io import load_from_bids, load_physio
8-
from physutils.physio import Physio
12+
# from loguru import logger
913

10-
LGR = logging.getLogger(__name__)
11-
LGR.setLevel(logging.DEBUG)
1214

1315
try:
14-
import pydra
15-
16-
pydra_imported = True
16+
from pydra import task
1717
except ImportError:
18-
pydra_imported = False
19-
20-
21-
def mark_task(pydra_imported=pydra_imported):
22-
def decorator(func):
23-
if pydra_imported:
24-
# If the decorator exists, apply it
25-
@wraps(func)
26-
def wrapped_func(*args, **kwargs):
27-
logger.debug(f"Creating pydra task for {func.__name__}")
28-
return pydra.mark.task(func)(*args, **kwargs)
18+
from .utils import task
2919

30-
return wrapped_func
31-
# Otherwise, return the original function
32-
return func
3320

34-
return decorator
21+
LGR = logging.getLogger(__name__)
22+
LGR.setLevel(logging.DEBUG)
3523

3624

37-
def is_bids_directory(directory):
38-
try:
39-
# Attempt to create a BIDSLayout object
40-
_ = BIDSLayout(directory)
41-
return True
42-
except Exception as e:
43-
# Catch other exceptions that might indicate the directory isn't BIDS compliant
44-
logger.error(
45-
f"An error occurred while trying to load {directory} as a BIDS Layout object: {e}"
46-
)
47-
return False
25+
@task
26+
def generate_physio(
27+
input_file: str, mode="auto", fs=None, bids_parameters=dict(), col_physio_type=None
28+
) -> Physio:
29+
"""
30+
Load a physio object from either a BIDS directory or an exported physio object.
4831
32+
Parameters
33+
----------
34+
input_file : str
35+
Path to input file
36+
mode : 'auto', 'physio', or 'bids', optional
37+
Mode to operate with
38+
fs : None, optional
39+
Set or force set sapmling frequency (Hz).
40+
bids_parameters : dictionary, optional
41+
Dictionary containing BIDS parameters
42+
col_physio_type : int or None, optional
43+
Object to pick up in a BIDS array of physio objects.
4944
50-
@mark_task(pydra_imported=pydra_imported)
51-
def transform_to_physio(
52-
input_file: str, mode="physio", fs=None, bids_parameters=dict(), bids_channel=None
53-
) -> Physio:
54-
if not pydra_imported:
55-
LGR.warning(
56-
"Pydra is not installed, thus transform_to_physio is not available as a pydra task. Using the function directly"
57-
)
58-
LGR.debug(f"Loading physio object from {input_file}")
59-
if not fs:
60-
fs = None
45+
"""
46+
LGR.info(f"Loading physio object from {input_file}")
6147

6248
if mode == "auto":
6349
if input_file.endswith((".phys", ".physio", ".1D", ".txt", ".tsv", ".csv")):
@@ -66,20 +52,20 @@ def transform_to_physio(
6652
mode = "bids"
6753
else:
6854
raise ValueError(
69-
"Could not determine mode automatically, please specify mode"
55+
"Could not determine input mode automatically. Please specify it manually."
7056
)
7157
if mode == "physio":
72-
if fs is not None:
73-
physio_obj = load_physio(input_file, fs=fs, allow_pickle=True)
74-
else:
75-
physio_obj = load_physio(input_file, allow_pickle=True)
58+
physio_obj = load_physio(input_file, fs=fs, allow_pickle=True)
7659

7760
elif mode == "bids":
7861
if bids_parameters is {}:
7962
raise ValueError("BIDS parameters must be provided when loading from BIDS")
8063
else:
8164
physio_array = load_from_bids(input_file, **bids_parameters)
82-
physio_obj = physio_array[bids_channel]
65+
physio_obj = (
66+
physio_array[col_physio_type] if col_physio_type else physio_array
67+
)
8368
else:
84-
raise ValueError(f"Invalid transform_to_physio mode: {mode}")
69+
raise ValueError(f"Invalid generate_physio mode: {mode}")
70+
8571
return physio_obj

physutils/tests/test_tasks.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from physutils.tests.utils import create_random_bids_structure
88

99

10-
def test_transform_to_physio_phys_file():
11-
"""Test transform_to_physio task."""
10+
def test_generate_physio_phys_file():
11+
"""Test generate_physio task."""
1212
physio_file = os.path.abspath("physutils/tests/data/ECG.phys")
13-
task = tasks.transform_to_physio(input_file=physio_file, mode="physio")
13+
task = tasks.generate_physio(input_file=physio_file, mode="physio")
1414
assert task.inputs.input_file == physio_file
1515
assert task.inputs.mode == "physio"
1616
assert task.inputs.fs is None
@@ -23,8 +23,8 @@ def test_transform_to_physio_phys_file():
2323
assert physio_obj.data.shape == (44611,)
2424

2525

26-
def test_transform_to_physio_bids_file():
27-
"""Test transform_to_physio task."""
26+
def test_generate_physio_bids_file():
27+
"""Test generate_physio task."""
2828
create_random_bids_structure("physutils/tests/data", recording_id="cardiac")
2929
bids_parameters = {
3030
"subject": "01",
@@ -34,7 +34,7 @@ def test_transform_to_physio_bids_file():
3434
"recording": "cardiac",
3535
}
3636
bids_dir = os.path.abspath("physutils/tests/data/bids-dir")
37-
task = tasks.transform_to_physio(
37+
task = tasks.generate_physio(
3838
input_file=bids_dir,
3939
mode="bids",
4040
bids_parameters=bids_parameters,
@@ -53,7 +53,7 @@ def test_transform_to_physio_bids_file():
5353
assert isinstance(physio_obj, physio.Physio)
5454

5555

56-
def test_transform_to_physio_auto():
56+
def test_generate_physio_auto():
5757
create_random_bids_structure("physutils/tests/data", recording_id="cardiac")
5858
bids_parameters = {
5959
"subject": "01",
@@ -63,7 +63,7 @@ def test_transform_to_physio_auto():
6363
"recording": "cardiac",
6464
}
6565
bids_dir = os.path.abspath("physutils/tests/data/bids-dir")
66-
task = tasks.transform_to_physio(
66+
task = tasks.generate_physio(
6767
input_file=bids_dir,
6868
mode="auto",
6969
bids_parameters=bids_parameters,
@@ -82,9 +82,9 @@ def test_transform_to_physio_auto():
8282
assert isinstance(physio_obj, physio.Physio)
8383

8484

85-
def test_transform_to_physio_auto_error(caplog):
85+
def test_generate_physio_auto_error(caplog):
8686
bids_dir = os.path.abspath("physutils/tests/data/non-bids-dir")
87-
task = tasks.transform_to_physio(
87+
task = tasks.generate_physio(
8888
input_file=bids_dir,
8989
mode="auto",
9090
bids_channel="cardiac",

0 commit comments

Comments
 (0)