Skip to content

Commit 44e2431

Browse files
committed
Refactor TR_string_to_float
1 parent 90dfaa8 commit 44e2431

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

CPAC/nuisance/utils/compcor.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,34 @@ def fallback_svd(a, full_matrices=True, compute_uv=True):
181181

182182

183183
def TR_string_to_float(tr):
184-
if 'ms' in tr:
185-
tr = float(tr.replace('ms', '')) / 1000
186-
else:
187-
tr = float(tr.replace('s', ''))
184+
"""
185+
Convert TR string to seconds (float). Suffixes 's' or 'ms' to indicate
186+
seconds or milliseconds.
187+
188+
Parameters
189+
----------
190+
tr : TR string representation. May use suffixes 's' or 'ms' to indicate
191+
seconds or milliseconds.
192+
193+
Returns
194+
-------
195+
tr in seconds (float)
196+
"""
197+
if not isinstance(tr, str):
198+
raise TypeError(f'Improper type for TR_string_to_float ({tr}).')
199+
200+
tr_str = tr.replace(' ', '')
201+
factor = 1.
202+
203+
if tr_str.endswith('ms'):
204+
tr_str = tr_str[:-2]
205+
factor = 0.001
206+
elif tr.endswith('s'):
207+
tr_str = tr_str[:-1]
208+
209+
try:
210+
tr_numeric = float(tr_str)
211+
except Exception as exc:
212+
raise ValueError(f'Can not convert TR string to float: "{tr}".') from exc
188213

189-
return tr
214+
return tr_numeric * factor

0 commit comments

Comments
 (0)