1+ #!/usr/bin/env python3
2+ # -*- coding: utf-8 -*-
3+
4+ """Helper class for holding physiological data and associated metadata information."""
5+
16import 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
1315try :
14- import pydra
15-
16- pydra_imported = True
16+ from pydra import task
1717except 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
0 commit comments