1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """Helper class for holding physiological data and associated metadata information."""
5
+
1
6
import logging
2
- from functools import wraps
3
7
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
6
11
7
- from physutils .io import load_from_bids , load_physio
8
- from physutils .physio import Physio
12
+ # from loguru import logger
9
13
10
- LGR = logging .getLogger (__name__ )
11
- LGR .setLevel (logging .DEBUG )
12
14
13
15
try :
14
- import pydra
15
-
16
- pydra_imported = True
16
+ from pydra import task
17
17
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
29
19
30
- return wrapped_func
31
- # Otherwise, return the original function
32
- return func
33
20
34
- return decorator
21
+ LGR = logging .getLogger (__name__ )
22
+ LGR .setLevel (logging .DEBUG )
35
23
36
24
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.
48
31
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.
49
44
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 } " )
61
47
62
48
if mode == "auto" :
63
49
if input_file .endswith ((".phys" , ".physio" , ".1D" , ".txt" , ".tsv" , ".csv" )):
@@ -66,20 +52,20 @@ def transform_to_physio(
66
52
mode = "bids"
67
53
else :
68
54
raise ValueError (
69
- "Could not determine mode automatically, please specify mode "
55
+ "Could not determine input mode automatically. Please specify it manually. "
70
56
)
71
57
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 )
76
59
77
60
elif mode == "bids" :
78
61
if bids_parameters is {}:
79
62
raise ValueError ("BIDS parameters must be provided when loading from BIDS" )
80
63
else :
81
64
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
+ )
83
68
else :
84
- raise ValueError (f"Invalid transform_to_physio mode: { mode } " )
69
+ raise ValueError (f"Invalid generate_physio mode: { mode } " )
70
+
85
71
return physio_obj
0 commit comments