Skip to content

Commit 36e89fa

Browse files
committed
Export metric utility
1 parent 4fe505c commit 36e89fa

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

phys2denoise/metrics/utils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import numpy as np
55
from numpy.lib.stride_tricks import sliding_window_view as swv
6+
from scipy.interpolate import interp1d
67
from scipy.stats import zscore
78

89
LGR = logging.getLogger(__name__)
@@ -250,3 +251,68 @@ def convolve_and_rescale(array, func, rescale="rescale", pad=False):
250251
pass
251252

252253
return array_combined
254+
255+
256+
def export_metric(
257+
metric, sample_rate, tr, fileprefix, ext=".1D", is_convolved=True, has_lags=False
258+
):
259+
"""
260+
Export the metric content, both in original sampling rate and resampled at the TR.
261+
262+
Parameters
263+
----------
264+
metric : list or numpy.ndarray
265+
Metric to be exported
266+
sample_rate : int or float
267+
Original sampling rate of the metric
268+
tr : int or float
269+
TR of functional data. Output will be also resampled to this value
270+
fileprefix : str
271+
Filename prefix, including path where files should be stored
272+
ext : str, optional
273+
Extension of file, default "1D"
274+
is_convolved : bool, optional.
275+
If True, `metric` contains convolved version already - default is True
276+
has_lags : bool, optional.
277+
If True, `metric` contains lagged versions of itself - default is False
278+
"""
279+
# Start resampling
280+
len_tp = metric.shape[0]
281+
len_newtp = int(np.around(metric.shape[0] * (1 / (sample_rate * tr))))
282+
len_s = len_tp / sample_rate
283+
orig_t = np.linspace(0, len_s, len_tp)
284+
interp_t = np.linspace(0, len_s, len_newtp)
285+
f = interp1d(orig_t, metric, fill_value="extrapolate", axis=0)
286+
287+
resampled_metric = f(interp_t)
288+
289+
# Export metrics
290+
if metric.ndim == 1:
291+
np.savetxt(f"{fileprefix}_orig{ext}", metric, fmt="%.6f")
292+
np.savetxt(f"{fileprefix}_resampled{ext}", resampled_metric, fmt="%.6f")
293+
elif metric.ndim == 2:
294+
cols = metric.shape[1]
295+
if cols == 1:
296+
np.savetxt(f"{fileprefix}_orig{ext}", metric, fmt="%.6f")
297+
np.savetxt(f"{fileprefix}_resampled{ext}", resampled_metric, fmt="%.6f")
298+
elif is_convolved:
299+
np.savetxt(f"{fileprefix}_orig_raw{ext}", metric[:, 0], fmt="%.6f")
300+
np.savetxt(
301+
f"{fileprefix}_resampled_raw{ext}", resampled_metric[:, 0], fmt="%.6f"
302+
)
303+
np.savetxt(f"{fileprefix}_orig_convolved{ext}", metric[:, 1], fmt="%.6f")
304+
np.savetxt(
305+
f"{fileprefix}_resampled_convolved{ext}",
306+
resampled_metric[:, 1],
307+
fmt="%.6f",
308+
)
309+
elif has_lags:
310+
for c in range(cols):
311+
np.savetxt(f"{fileprefix}_orig_lag-{c}{ext}", metric[:, c], fmt="%.6f")
312+
np.savetxt(
313+
f"{fileprefix}_resampled_lag-{c}{ext}",
314+
resampled_metric[:, c],
315+
fmt="%.6f",
316+
)
317+
318+
return fileprefix

0 commit comments

Comments
 (0)