rapidtide 3.0.11__py3-none-any.whl → 3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. rapidtide/Colortables.py +492 -27
  2. rapidtide/OrthoImageItem.py +1049 -46
  3. rapidtide/RapidtideDataset.py +1533 -86
  4. rapidtide/_version.py +3 -3
  5. rapidtide/calccoherence.py +196 -29
  6. rapidtide/calcnullsimfunc.py +191 -40
  7. rapidtide/calcsimfunc.py +245 -42
  8. rapidtide/correlate.py +1210 -393
  9. rapidtide/data/examples/src/testLD +56 -0
  10. rapidtide/data/examples/src/testalign +1 -1
  11. rapidtide/data/examples/src/testdelayvar +0 -1
  12. rapidtide/data/examples/src/testfmri +19 -1
  13. rapidtide/data/examples/src/testglmfilt +5 -5
  14. rapidtide/data/examples/src/testhappy +25 -3
  15. rapidtide/data/examples/src/testppgproc +17 -0
  16. rapidtide/data/examples/src/testrolloff +11 -0
  17. rapidtide/data/models/model_cnn_pytorch/best_model.pth +0 -0
  18. rapidtide/data/models/model_cnn_pytorch/loss.png +0 -0
  19. rapidtide/data/models/model_cnn_pytorch/loss.txt +1 -0
  20. rapidtide/data/models/model_cnn_pytorch/model.pth +0 -0
  21. rapidtide/data/models/model_cnn_pytorch/model_meta.json +68 -0
  22. rapidtide/decorators.py +91 -0
  23. rapidtide/dlfilter.py +2225 -108
  24. rapidtide/dlfiltertorch.py +4843 -0
  25. rapidtide/externaltools.py +327 -12
  26. rapidtide/fMRIData_class.py +79 -40
  27. rapidtide/filter.py +1899 -810
  28. rapidtide/fit.py +2004 -574
  29. rapidtide/genericmultiproc.py +93 -18
  30. rapidtide/happy_supportfuncs.py +2044 -171
  31. rapidtide/helper_classes.py +584 -43
  32. rapidtide/io.py +2363 -370
  33. rapidtide/linfitfiltpass.py +341 -75
  34. rapidtide/makelaggedtcs.py +211 -20
  35. rapidtide/maskutil.py +423 -53
  36. rapidtide/miscmath.py +827 -121
  37. rapidtide/multiproc.py +210 -22
  38. rapidtide/patchmatch.py +234 -33
  39. rapidtide/peakeval.py +32 -30
  40. rapidtide/ppgproc.py +2203 -0
  41. rapidtide/qualitycheck.py +352 -39
  42. rapidtide/refinedelay.py +422 -57
  43. rapidtide/refineregressor.py +498 -184
  44. rapidtide/resample.py +671 -185
  45. rapidtide/scripts/applyppgproc.py +28 -0
  46. rapidtide/simFuncClasses.py +1052 -77
  47. rapidtide/simfuncfit.py +260 -46
  48. rapidtide/stats.py +540 -238
  49. rapidtide/tests/happycomp +9 -0
  50. rapidtide/tests/test_dlfiltertorch.py +627 -0
  51. rapidtide/tests/test_findmaxlag.py +24 -8
  52. rapidtide/tests/test_fullrunhappy_v1.py +0 -2
  53. rapidtide/tests/test_fullrunhappy_v2.py +0 -2
  54. rapidtide/tests/test_fullrunhappy_v3.py +1 -0
  55. rapidtide/tests/test_fullrunhappy_v4.py +2 -2
  56. rapidtide/tests/test_fullrunrapidtide_v7.py +1 -1
  57. rapidtide/tests/test_simroundtrip.py +8 -8
  58. rapidtide/tests/utils.py +9 -8
  59. rapidtide/tidepoolTemplate.py +142 -38
  60. rapidtide/tidepoolTemplate_alt.py +165 -44
  61. rapidtide/tidepoolTemplate_big.py +189 -52
  62. rapidtide/util.py +1217 -118
  63. rapidtide/voxelData.py +684 -37
  64. rapidtide/wiener.py +19 -12
  65. rapidtide/wiener2.py +113 -7
  66. rapidtide/wiener_doc.py +255 -0
  67. rapidtide/workflows/adjustoffset.py +105 -3
  68. rapidtide/workflows/aligntcs.py +85 -2
  69. rapidtide/workflows/applydlfilter.py +87 -10
  70. rapidtide/workflows/applyppgproc.py +522 -0
  71. rapidtide/workflows/atlasaverage.py +210 -47
  72. rapidtide/workflows/atlastool.py +100 -3
  73. rapidtide/workflows/calcSimFuncMap.py +294 -64
  74. rapidtide/workflows/calctexticc.py +201 -9
  75. rapidtide/workflows/ccorrica.py +97 -4
  76. rapidtide/workflows/cleanregressor.py +168 -29
  77. rapidtide/workflows/delayvar.py +163 -10
  78. rapidtide/workflows/diffrois.py +81 -3
  79. rapidtide/workflows/endtidalproc.py +144 -4
  80. rapidtide/workflows/fdica.py +195 -15
  81. rapidtide/workflows/filtnifti.py +70 -3
  82. rapidtide/workflows/filttc.py +74 -3
  83. rapidtide/workflows/fitSimFuncMap.py +206 -48
  84. rapidtide/workflows/fixtr.py +73 -3
  85. rapidtide/workflows/gmscalc.py +113 -3
  86. rapidtide/workflows/happy.py +801 -199
  87. rapidtide/workflows/happy2std.py +144 -12
  88. rapidtide/workflows/happy_parser.py +138 -9
  89. rapidtide/workflows/histnifti.py +118 -2
  90. rapidtide/workflows/histtc.py +84 -3
  91. rapidtide/workflows/linfitfilt.py +117 -4
  92. rapidtide/workflows/localflow.py +328 -28
  93. rapidtide/workflows/mergequality.py +79 -3
  94. rapidtide/workflows/niftidecomp.py +322 -18
  95. rapidtide/workflows/niftistats.py +174 -4
  96. rapidtide/workflows/pairproc.py +88 -2
  97. rapidtide/workflows/pairwisemergenifti.py +85 -2
  98. rapidtide/workflows/parser_funcs.py +1421 -40
  99. rapidtide/workflows/physiofreq.py +137 -11
  100. rapidtide/workflows/pixelcomp.py +208 -5
  101. rapidtide/workflows/plethquality.py +103 -21
  102. rapidtide/workflows/polyfitim.py +151 -11
  103. rapidtide/workflows/proj2flow.py +75 -2
  104. rapidtide/workflows/rankimage.py +111 -4
  105. rapidtide/workflows/rapidtide.py +272 -15
  106. rapidtide/workflows/rapidtide2std.py +98 -2
  107. rapidtide/workflows/rapidtide_parser.py +109 -9
  108. rapidtide/workflows/refineDelayMap.py +143 -33
  109. rapidtide/workflows/refineRegressor.py +682 -93
  110. rapidtide/workflows/regressfrommaps.py +152 -31
  111. rapidtide/workflows/resamplenifti.py +85 -3
  112. rapidtide/workflows/resampletc.py +91 -3
  113. rapidtide/workflows/retrolagtcs.py +98 -6
  114. rapidtide/workflows/retroregress.py +165 -9
  115. rapidtide/workflows/roisummarize.py +173 -5
  116. rapidtide/workflows/runqualitycheck.py +71 -3
  117. rapidtide/workflows/showarbcorr.py +147 -4
  118. rapidtide/workflows/showhist.py +86 -2
  119. rapidtide/workflows/showstxcorr.py +160 -3
  120. rapidtide/workflows/showtc.py +159 -3
  121. rapidtide/workflows/showxcorrx.py +184 -4
  122. rapidtide/workflows/showxy.py +185 -15
  123. rapidtide/workflows/simdata.py +262 -36
  124. rapidtide/workflows/spatialfit.py +77 -2
  125. rapidtide/workflows/spatialmi.py +251 -27
  126. rapidtide/workflows/spectrogram.py +305 -32
  127. rapidtide/workflows/synthASL.py +154 -3
  128. rapidtide/workflows/tcfrom2col.py +76 -2
  129. rapidtide/workflows/tcfrom3col.py +74 -2
  130. rapidtide/workflows/tidepool.py +2969 -130
  131. rapidtide/workflows/utils.py +19 -14
  132. rapidtide/workflows/utils_doc.py +293 -0
  133. rapidtide/workflows/variabilityizer.py +116 -3
  134. {rapidtide-3.0.11.dist-info → rapidtide-3.1.dist-info}/METADATA +3 -2
  135. {rapidtide-3.0.11.dist-info → rapidtide-3.1.dist-info}/RECORD +139 -122
  136. {rapidtide-3.0.11.dist-info → rapidtide-3.1.dist-info}/entry_points.txt +1 -0
  137. {rapidtide-3.0.11.dist-info → rapidtide-3.1.dist-info}/WHEEL +0 -0
  138. {rapidtide-3.0.11.dist-info → rapidtide-3.1.dist-info}/licenses/LICENSE +0 -0
  139. {rapidtide-3.0.11.dist-info → rapidtide-3.1.dist-info}/top_level.txt +0 -0
@@ -17,8 +17,10 @@
17
17
  #
18
18
  #
19
19
  import sys
20
+ from typing import Any, Callable, Dict, List, Optional, Tuple, Union
20
21
 
21
22
  import numpy as np
23
+ from numpy.typing import NDArray
22
24
 
23
25
  import rapidtide.fit as tide_fit
24
26
  import rapidtide.io as tide_io
@@ -40,52 +42,220 @@ class RegressorRefiner:
40
42
 
41
43
  def __init__(
42
44
  self,
43
- internalvalidfmrishape,
44
- internalvalidpaddedfmrishape,
45
- pid,
46
- outputname,
47
- initial_fmri_x,
48
- paddedinitial_fmri_x,
49
- os_fmri_x,
50
- sharedmem=False,
51
- offsettime=0.0,
52
- ampthresh=0.3,
53
- lagminthresh=0.25,
54
- lagmaxthresh=3.0,
55
- sigmathresh=1000.0,
56
- cleanrefined=False,
57
- bipolar=False,
58
- fixdelay=False,
59
- includemask=None,
60
- excludemask=None,
61
- LGR=None,
62
- nprocs=1,
63
- detrendorder=1,
64
- alwaysmultiproc=False,
65
- showprogressbar=True,
66
- chunksize=50000,
67
- padtrs=10,
68
- refineprenorm="var",
69
- refineweighting=None,
70
- refinetype="pca",
71
- pcacomponents=0.8,
72
- dodispersioncalc=False,
73
- dispersioncalc_lower=-5.0,
74
- dispersioncalc_upper=5.0,
75
- dispersioncalc_step=0.5,
76
- windowfunc="hamming",
77
- passes=3,
78
- maxpasses=15,
79
- convergencethresh=None,
80
- interptype="univariate",
81
- usetmask=False,
82
- tmask_y=None,
83
- tmaskos_y=None,
84
- fastresamplerpadtime=45.0,
85
- debug=False,
86
- rt_floattype="float64",
87
- rt_floatset=np.float64,
88
- ):
45
+ internalvalidfmrishape: Any,
46
+ internalvalidpaddedfmrishape: Any,
47
+ pid: Any,
48
+ outputname: Any,
49
+ initial_fmri_x: Any,
50
+ paddedinitial_fmri_x: Any,
51
+ os_fmri_x: Any,
52
+ sharedmem: bool = False,
53
+ offsettime: float = 0.0,
54
+ ampthresh: float = 0.3,
55
+ lagminthresh: float = 0.25,
56
+ lagmaxthresh: float = 3.0,
57
+ sigmathresh: float = 1000.0,
58
+ cleanrefined: bool = False,
59
+ bipolar: bool = False,
60
+ fixdelay: bool = False,
61
+ includemask: Optional[Any] = None,
62
+ excludemask: Optional[Any] = None,
63
+ LGR: Optional[Any] = None,
64
+ nprocs: int = 1,
65
+ detrendorder: int = 1,
66
+ alwaysmultiproc: bool = False,
67
+ showprogressbar: bool = True,
68
+ chunksize: int = 50000,
69
+ padtrs: int = 10,
70
+ refineprenorm: str = "var",
71
+ refineweighting: Optional[Any] = None,
72
+ refinetype: str = "pca",
73
+ pcacomponents: float = 0.8,
74
+ dodispersioncalc: bool = False,
75
+ dispersioncalc_lower: float = -5.0,
76
+ dispersioncalc_upper: float = 5.0,
77
+ dispersioncalc_step: float = 0.5,
78
+ windowfunc: str = "hamming",
79
+ passes: int = 3,
80
+ maxpasses: int = 15,
81
+ convergencethresh: Optional[Any] = None,
82
+ interptype: str = "univariate",
83
+ usetmask: bool = False,
84
+ tmask_y: Optional[Any] = None,
85
+ tmaskos_y: Optional[Any] = None,
86
+ fastresamplerpadtime: float = 45.0,
87
+ prewhitenregressor: bool = False,
88
+ prewhitenlags: int = 10,
89
+ debug: bool = False,
90
+ rt_floattype: str = "float64",
91
+ rt_floatset: Any = np.float64,
92
+ ) -> None:
93
+ """
94
+ Initialize the object with configuration parameters for fMRI data processing.
95
+
96
+ This constructor sets up internal attributes and performs initial setup tasks such as
97
+ allocating memory and applying masks based on provided parameters.
98
+
99
+ Parameters
100
+ ----------
101
+ internalvalidfmrishape : Any
102
+ Shape of the internal valid fMRI data.
103
+ internalvalidpaddedfmrishape : Any
104
+ Shape of the padded internal valid fMRI data.
105
+ pid : Any
106
+ Process identifier used for memory allocation.
107
+ outputname : Any
108
+ Name of the output file or dataset.
109
+ initial_fmri_x : Any
110
+ Initial fMRI data array.
111
+ paddedinitial_fmri_x : Any
112
+ Padded version of the initial fMRI data.
113
+ os_fmri_x : Any
114
+ Oversampled fMRI data array.
115
+ sharedmem : bool, optional
116
+ Whether to use shared memory for processing (default is False).
117
+ offsettime : float, optional
118
+ Time offset in seconds (default is 0.0).
119
+ ampthresh : float, optional
120
+ Amplitude threshold for filtering (default is 0.3).
121
+ lagminthresh : float, optional
122
+ Minimum lag threshold for correlation analysis (default is 0.25).
123
+ lagmaxthresh : float, optional
124
+ Maximum lag threshold for correlation analysis (default is 3.0).
125
+ sigmathresh : float, optional
126
+ Significance threshold for statistical tests (default is 1000.0).
127
+ cleanrefined : bool, optional
128
+ Whether to apply refined cleaning steps (default is False).
129
+ bipolar : bool, optional
130
+ Whether to use bipolar filtering (default is False).
131
+ fixdelay : bool, optional
132
+ Whether to fix delay in the processing pipeline (default is False).
133
+ includemask : Optional[Any], optional
134
+ Mask to include specific regions (default is None).
135
+ excludemask : Optional[Any], optional
136
+ Mask to exclude specific regions (default is None).
137
+ LGR : Optional[Any], optional
138
+ Logarithmic gradient or related parameter (default is None).
139
+ nprocs : int, optional
140
+ Number of processes to use (default is 1).
141
+ detrendorder : int, optional
142
+ Order of detrending polynomial (default is 1).
143
+ alwaysmultiproc : bool, optional
144
+ Force multiprocessing even for small tasks (default is False).
145
+ showprogressbar : bool, optional
146
+ Show progress bar during processing (default is True).
147
+ chunksize : int, optional
148
+ Size of data chunks for processing (default is 50000).
149
+ padtrs : int, optional
150
+ Number of TRs to pad (default is 10).
151
+ refineprenorm : str, optional
152
+ Pre-normalization method for refinement ("var", "mean", etc.) (default is "var").
153
+ refineweighting : Optional[Any], optional
154
+ Weighting scheme for refinement (default is None).
155
+ refinetype : str, optional
156
+ Type of refinement to perform ("pca", "ica", etc.) (default is "pca").
157
+ pcacomponents : float, optional
158
+ Fraction of PCA components to retain (default is 0.8).
159
+ dodispersioncalc : bool, optional
160
+ Whether to perform dispersion calculation (default is False).
161
+ dispersioncalc_lower : float, optional
162
+ Lower bound for dispersion calculation (default is -5.0).
163
+ dispersioncalc_upper : float, optional
164
+ Upper bound for dispersion calculation (default is 5.0).
165
+ dispersioncalc_step : float, optional
166
+ Step size for dispersion calculation (default is 0.5).
167
+ windowfunc : str, optional
168
+ Window function used in spectral analysis (default is "hamming").
169
+ passes : int, optional
170
+ Number of filter passes (default is 3).
171
+ maxpasses : int, optional
172
+ Maximum allowed number of passes (default is 15).
173
+ convergencethresh : Optional[Any], optional
174
+ Convergence threshold for iterative algorithms (default is None).
175
+ interptype : str, optional
176
+ Interpolation type for resampling ("univariate", "multivariate") (default is "univariate").
177
+ usetmask : bool, optional
178
+ Whether to use temporal mask (default is False).
179
+ tmask_y : Optional[Any], optional
180
+ Temporal mask for y-axis (default is None).
181
+ tmaskos_y : Optional[Any], optional
182
+ Oversampled temporal mask for y-axis (default is None).
183
+ fastresamplerpadtime : float, optional
184
+ Padding time for fast resampling (default is 45.0).
185
+ prewhitenregressor : bool, optional
186
+ Apply pre-whitening to regressors (default is False).
187
+ prewhitenlags : int, optional
188
+ Number of lags for pre-whitening (default is 10).
189
+ debug : bool, optional
190
+ Enable debug mode (default is False).
191
+ rt_floattype : str, optional
192
+ Real-time floating-point data type (default is "float64").
193
+ rt_floatset : Any, optional
194
+ Real-time float type setting (default is np.float64).
195
+
196
+ Returns
197
+ -------
198
+ None
199
+ This method initializes the object and does not return any value.
200
+
201
+ Notes
202
+ -----
203
+ - The function internally calls `setmasks` and `_allocatemem` to initialize
204
+ masks and allocate memory respectively.
205
+ - The parameters are stored as instance attributes for use in subsequent processing steps.
206
+
207
+ Examples
208
+ --------
209
+ >>> obj = MyClass(
210
+ ... internalvalidfmrishape=(64, 64, 30),
211
+ ... internalvalidpaddedfmrishape=(64, 64, 35),
212
+ ... pid=12345,
213
+ ... outputname="output.nii",
214
+ ... initial_fmri_x=np.random.rand(64, 64, 30),
215
+ ... paddedinitial_fmri_x=np.random.rand(64, 64, 35),
216
+ ... os_fmri_x=np.random.rand(64, 64, 60),
217
+ ... sharedmem=True,
218
+ ... offsettime=0.5,
219
+ ... ampthresh=0.5,
220
+ ... lagminthresh=0.3,
221
+ ... lagmaxthresh=2.0,
222
+ ... sigmathresh=500.0,
223
+ ... cleanrefined=True,
224
+ ... bipolar=False,
225
+ ... fixdelay=False,
226
+ ... includemask=None,
227
+ ... excludemask=None,
228
+ ... LGR=None,
229
+ ... nprocs=4,
230
+ ... detrendorder=2,
231
+ ... alwaysmultiproc=False,
232
+ ... showprogressbar=True,
233
+ ... chunksize=10000,
234
+ ... padtrs=5,
235
+ ... refineprenorm="mean",
236
+ ... refineweighting=None,
237
+ ... refinetype="pca",
238
+ ... pcacomponents=0.9,
239
+ ... dodispersioncalc=True,
240
+ ... dispersioncalc_lower=-4.0,
241
+ ... dispersioncalc_upper=4.0,
242
+ ... dispersioncalc_step=0.25,
243
+ ... windowfunc="hann",
244
+ ... passes=2,
245
+ ... maxpasses=10,
246
+ ... convergencethresh=None,
247
+ ... interptype="multivariate",
248
+ ... usetmask=True,
249
+ ... tmask_y=np.ones((64, 64)),
250
+ ... tmaskos_y=np.ones((64, 64)),
251
+ ... fastresamplerpadtime=30.0,
252
+ ... prewhitenregressor=True,
253
+ ... prewhitenlags=5,
254
+ ... debug=False,
255
+ ... rt_floattype="float32",
256
+ ... rt_floatset=np.float32,
257
+ ... )
258
+ """
89
259
  self.internalvalidfmrishape = internalvalidfmrishape
90
260
  self.internalvalidpaddedfmrishape = internalvalidpaddedfmrishape
91
261
  self.sharedmem = sharedmem
@@ -126,6 +296,8 @@ class RegressorRefiner:
126
296
  self.tmask_y = tmask_y
127
297
  self.tmaskos_y = tmaskos_y
128
298
  self.fastresamplerpadtime = fastresamplerpadtime
299
+ self.prewhitenregressor = prewhitenregressor
300
+ self.prewhitenlags = prewhitenlags
129
301
  self.debug = debug
130
302
  self.rt_floattype = rt_floattype
131
303
  self.rt_floatset = rt_floatset
@@ -133,11 +305,74 @@ class RegressorRefiner:
133
305
  self.setmasks(includemask, excludemask)
134
306
  self.totalrefinementbytes = self._allocatemem(pid)
135
307
 
136
- def setmasks(self, includemask, excludemask):
308
+ def setmasks(self, includemask: Any, excludemask: Any) -> None:
309
+ """
310
+ Set the include and exclude masks for the object.
311
+
312
+ Parameters
313
+ ----------
314
+ includemask : Any
315
+ The mask to be used for including elements. Type and structure depends
316
+ on the specific implementation and usage context.
317
+ excludemask : Any
318
+ The mask to be used for excluding elements. Type and structure depends
319
+ on the specific implementation and usage context.
320
+
321
+ Returns
322
+ -------
323
+ None
324
+ This method does not return any value.
325
+
326
+ Notes
327
+ -----
328
+ This method assigns the provided masks to instance attributes `includemask`
329
+ and `excludemask`. The masks are typically used for filtering or selection
330
+ operations in data processing workflows.
331
+
332
+ Examples
333
+ --------
334
+ >>> obj = MyClass()
335
+ >>> obj.setmasks([1, 0, 1], [0, 1, 0])
336
+ >>> print(obj.includemask)
337
+ [1, 0, 1]
338
+ >>> print(obj.excludemask)
339
+ [0, 1, 0]
340
+ """
137
341
  self.includemask = includemask
138
342
  self.excludemask = excludemask
139
343
 
140
- def _allocatemem(self, pid):
344
+ def _allocatemem(self, pid: Any) -> None:
345
+ """
346
+ Allocate memory for refinement arrays using shared memory if specified.
347
+
348
+ This function allocates four arrays used in the refinement process:
349
+ `shiftedtcs`, `weights`, `paddedshiftedtcs`, and `paddedweights`. These
350
+ arrays are allocated with shapes determined by `internalvalidfmrishape` and
351
+ `internalvalidpaddedfmrishape`, using the specified data type and memory
352
+ sharing settings.
353
+
354
+ Parameters
355
+ ----------
356
+ pid : Any
357
+ Process identifier used to name shared memory segments.
358
+
359
+ Returns
360
+ -------
361
+ int
362
+ Total number of bytes allocated for the refinement arrays.
363
+
364
+ Notes
365
+ -----
366
+ If `sharedmem` is True, the arrays are allocated in shared memory; otherwise,
367
+ they are allocated locally. The function prints information about the
368
+ allocation size and location, and logs memory usage after allocation.
369
+
370
+ Examples
371
+ --------
372
+ >>> self._allocatemem(pid=1234)
373
+ allocated 10.500 MB in shared memory for refinement
374
+ 11010048
375
+ """
141
376
  self.shiftedtcs, self.shiftedtcs_shm = tide_util.allocarray(
142
377
  self.internalvalidfmrishape,
143
378
  self.rt_floattype,
@@ -177,7 +412,34 @@ class RegressorRefiner:
177
412
  tide_util.logmem("after refinement array allocation")
178
413
  return totalrefinementbytes
179
414
 
180
- def cleanup(self):
415
+ def cleanup(self) -> None:
416
+ """
417
+ Clean up memory resources by deleting internal attributes and shared memory segments.
418
+
419
+ This method removes all internal arrays and their corresponding shared memory segments
420
+ when shared memory is being used. It's designed to free up memory resources that were
421
+ allocated during processing.
422
+
423
+ Returns
424
+ -------
425
+ None
426
+ This method does not return any value.
427
+
428
+ Notes
429
+ -----
430
+ - Deletes the following internal attributes: paddedshiftedtcs, paddedweights,
431
+ shiftedtcs, weights
432
+ - If shared memory is enabled (sharedmem=True), also cleans up the corresponding
433
+ shared memory segments using tide_util.cleanup_shm()
434
+ - This method should be called when the object is no longer needed to prevent
435
+ memory leaks
436
+
437
+ Examples
438
+ --------
439
+ >>> obj = MyClass()
440
+ >>> obj.cleanup()
441
+ >>> # All internal memory resources are now freed
442
+ """
181
443
  del self.paddedshiftedtcs
182
444
  del self.paddedweights
183
445
  del self.shiftedtcs
@@ -188,7 +450,54 @@ class RegressorRefiner:
188
450
  tide_util.cleanup_shm(self.shiftedtcs_shm)
189
451
  tide_util.cleanup_shm(self.weights_shm)
190
452
 
191
- def makemask(self, lagstrengths, lagtimes, lagsigma, fitmask):
453
+ def makemask(self, lagstrengths: Any, lagtimes: Any, lagsigma: Any, fitmask: Any) -> None:
454
+ """
455
+ Create a refinement mask based on lag strength, lag time, and sigma thresholds.
456
+
457
+ This function generates a mask for refining regressor parameters by evaluating
458
+ the quality of lag estimates against specified thresholds. The mask determines
459
+ which voxels should be included in the refinement process based on their
460
+ lag strength, lag time, and sigma values.
461
+
462
+ Parameters
463
+ ----------
464
+ lagstrengths : array-like
465
+ Array containing lag strength values for each voxel
466
+ lagtimes : array-like
467
+ Array containing lag time values for each voxel
468
+ lagsigma : array-like
469
+ Array containing sigma values for each voxel
470
+ fitmask : array-like
471
+ Boolean mask indicating which voxels to consider for fitting
472
+
473
+ Returns
474
+ -------
475
+ bool
476
+ True if voxels are included in the refine mask, False if no voxels
477
+ meet the refinement criteria
478
+
479
+ Notes
480
+ -----
481
+ The function uses internal threshold parameters to determine which voxels
482
+ should be included in the refinement process. These include:
483
+
484
+ - ampthresh: amplitude threshold
485
+ - lagminthresh: minimum lag threshold
486
+ - lagmaxthresh: maximum lag threshold
487
+ - sigmathresh: sigma threshold
488
+
489
+ If no voxels meet the criteria, a critical log message is generated and
490
+ the function returns False.
491
+
492
+ Examples
493
+ --------
494
+ >>> # Assuming self is an instance of a class with the required attributes
495
+ >>> result = self.makemask(lagstrengths, lagtimes, lagsigma, fitmask)
496
+ >>> if result:
497
+ ... print("Refinement mask created successfully")
498
+ ... else:
499
+ ... print("No voxels in refine mask")
500
+ """
192
501
  # create the refinement mask
193
502
  (
194
503
  self.refinemaskvoxels,
@@ -222,13 +531,95 @@ class RegressorRefiner:
222
531
  else:
223
532
  return True
224
533
 
225
- def getrefinemask(self):
534
+ def getrefinemask(self) -> None:
535
+ """
536
+ Return the refinement mask.
537
+
538
+ Returns
539
+ -------
540
+ None
541
+ The refinement mask stored in the instance.
542
+
543
+ Notes
544
+ -----
545
+ This method provides access to the refinement mask attribute. The refinement
546
+ mask is typically used in computational physics or data analysis workflows
547
+ to identify regions of interest or apply specific processing to certain data
548
+ points.
549
+
550
+ Examples
551
+ --------
552
+ >>> mask = obj.getrefinemask()
553
+ >>> print(mask)
554
+ [True, False, True, False]
555
+ """
226
556
  return self.refinemask
227
557
 
228
- def getpaddedshiftedtcs(self):
558
+ def getpaddedshiftedtcs(self) -> None:
559
+ """
560
+ Return the padded and shifted time-course data.
561
+
562
+ This method retrieves the pre-computed padded and shifted time-course data
563
+ that has been processed for analysis. The data is typically used in
564
+ time-series analysis or signal processing applications where temporal
565
+ alignment and padding are required.
566
+
567
+ Returns
568
+ -------
569
+ array-like
570
+ The padded and shifted time-course data stored in the instance variable
571
+ `paddedshiftedtcs`. The exact format depends on the data processing
572
+ pipeline that generated this data.
573
+
574
+ Notes
575
+ -----
576
+ This method serves as a simple getter function for the `paddedshiftedtcs`
577
+ attribute. The actual computation of padded and shifted time-course data
578
+ should be performed prior to calling this method.
579
+
580
+ Examples
581
+ --------
582
+ >>> processor = TimeCourseProcessor()
583
+ >>> processor.compute_padded_shifted_tcs()
584
+ >>> result = processor.getpaddedshiftedtcs()
585
+ >>> print(result.shape)
586
+ (1000, 50)
587
+ """
229
588
  return self.paddedshiftedtcs
230
589
 
231
- def alignvoxels(self, fmri_data_valid, fmritr, lagtimes):
590
+ def alignvoxels(self, fmri_data_valid: Any, fmritr: Any, lagtimes: Any) -> None:
591
+ """
592
+ Align timecourses to prepare for refinement.
593
+
594
+ This function aligns voxel timecourses for further refinement processing by
595
+ coordinating the alignment of fMRI data with specified lag times and processing
596
+ parameters.
597
+
598
+ Parameters
599
+ ----------
600
+ fmri_data_valid : Any
601
+ Valid fMRI data to be aligned
602
+ fmritr : Any
603
+ fMRI temporal resolution information
604
+ lagtimes : Any
605
+ Lag times to be used for alignment
606
+
607
+ Returns
608
+ -------
609
+ None
610
+ This function does not return a value but updates internal state
611
+
612
+ Notes
613
+ -----
614
+ The function utilizes the `tide_refineregressor.alignvoxels` function internally
615
+ and passes all relevant processing parameters including multiprocessing settings,
616
+ detrending options, and padding parameters.
617
+
618
+ Examples
619
+ --------
620
+ >>> alignvoxels(fmri_data_valid, fmritr, lagtimes)
621
+ >>> # Function processes data and updates internal state
622
+ """
232
623
  # align timecourses to prepare for refinement
233
624
  self.LGR.info("aligning timecourses")
234
625
  voxelsprocessed_rra = tide_refineregressor.alignvoxels(
@@ -247,14 +638,49 @@ class RegressorRefiner:
247
638
  showprogressbar=self.showprogressbar,
248
639
  chunksize=self.chunksize,
249
640
  padtrs=self.padtrs,
250
- debug=self.debug,
251
641
  rt_floatset=self.rt_floatset,
252
642
  rt_floattype=self.rt_floattype,
253
643
  )
254
644
  return voxelsprocessed_rra
255
645
  # self.LGR.info(f"align complete: {voxelsprocessed_rra=}")
256
646
 
257
- def prenormalize(self, lagtimes, lagstrengths, R2):
647
+ def prenormalize(self, lagtimes: Any, lagstrengths: Any, R2: Any) -> None:
648
+ """
649
+ Pre-normalize time series data for refinement regression.
650
+
651
+ This function applies pre-normalization to the padded and shifted time series
652
+ data using the specified lag times, lag strengths, and R2 values. The
653
+ normalization is performed through the underlying tide_refineregressor.prenorm
654
+ function with the appropriate internal parameters.
655
+
656
+ Parameters
657
+ ----------
658
+ lagtimes : Any
659
+ Array or list of lag times to be used in the pre-normalization process.
660
+ lagstrengths : Any
661
+ Array or list of lag strengths corresponding to the lag times.
662
+ R2 : Any
663
+ Array or list of R2 values used for the pre-normalization calculation.
664
+
665
+ Returns
666
+ -------
667
+ None
668
+ This function does not return any value. It modifies internal attributes
669
+ in-place.
670
+
671
+ Notes
672
+ -----
673
+ The function internally uses:
674
+ - self.paddedshiftedtcs: padded and shifted time series data
675
+ - self.refinemask: refinement mask for the normalization process
676
+ - self.lagmaxthresh: maximum lag threshold
677
+ - self.refineprenorm: refinement pre-normalization parameters
678
+ - self.refineweighting: refinement weighting parameters
679
+
680
+ Examples
681
+ --------
682
+ >>> prenormalize(lagtimes=[1, 2, 3], lagstrengths=[0.5, 0.3, 0.8], R2=[0.9, 0.85, 0.92])
683
+ """
258
684
  tide_refineregressor.prenorm(
259
685
  self.paddedshiftedtcs,
260
686
  self.refinemask,
@@ -268,14 +694,69 @@ class RegressorRefiner:
268
694
 
269
695
  def refine(
270
696
  self,
271
- theprefilter,
272
- fmritr,
273
- thepass,
274
- lagstrengths,
275
- lagtimes,
276
- previousnormoutputdata,
277
- corrmasksize,
278
- ):
697
+ theprefilter: Any,
698
+ fmritr: Any,
699
+ thepass: Any,
700
+ lagstrengths: Any,
701
+ lagtimes: Any,
702
+ previousnormoutputdata: Any,
703
+ corrmasksize: Any,
704
+ ) -> None:
705
+ """
706
+ Refine the regressor by iteratively applying filtering and resampling operations.
707
+
708
+ This method performs iterative refinement of a regressor using a series of
709
+ filtering, resampling, and normalization steps. It tracks convergence and
710
+ updates internal state variables accordingly.
711
+
712
+ Parameters
713
+ ----------
714
+ theprefilter : Any
715
+ The prefilter to be applied to the data.
716
+ fmritr : Any
717
+ The fMRI temporal resolution (TR).
718
+ thepass : Any
719
+ The current pass number in the refinement process.
720
+ lagstrengths : Any
721
+ The lag strengths used in the refinement.
722
+ lagtimes : Any
723
+ The lag times used in the refinement.
724
+ previousnormoutputdata : Any
725
+ The normalized output data from the previous pass.
726
+ corrmasksize : Any
727
+ The size of the correlation mask.
728
+
729
+ Returns
730
+ -------
731
+ tuple
732
+ A tuple containing:
733
+ - voxelsprocessed_rr : int
734
+ Number of voxels processed in this pass.
735
+ - outputdict : dict
736
+ Dictionary of output statistics for this pass.
737
+ - previousnormoutputdata : ndarray
738
+ Updated normalized output data for the next pass.
739
+ - resampref_y : ndarray
740
+ Resampled refined regressor at oversampled frequency.
741
+ - resampnonosref_y : ndarray
742
+ Resampled refined regressor at original frequency.
743
+ - stoprefining : bool
744
+ Flag indicating whether refinement should stop.
745
+ - refinestopreason : str or None
746
+ Reason for stopping refinement.
747
+ - genlagtc : FastResampler
748
+ Generator for lag time courses.
749
+
750
+ Notes
751
+ -----
752
+ This function modifies internal attributes of the class, such as `paddedoutputdata`,
753
+ `locationfails`, `ampfails`, `lagfails`, and `sigmafails`. It also writes output
754
+ files using `tide_io.writebidstsv`.
755
+
756
+ Examples
757
+ --------
758
+ >>> refine(prefilter, fmritr, 1, lagstrengths, lagtimes, prev_data, mask_size)
759
+ """
279
760
  (
280
761
  voxelsprocessed_rr,
281
762
  self.paddedoutputdata,
@@ -300,7 +781,6 @@ class RegressorRefiner:
300
781
  windowfunc=self.windowfunc,
301
782
  cleanrefined=self.cleanrefined,
302
783
  bipolar=self.bipolar,
303
- debug=self.debug,
304
784
  rt_floatset=self.rt_floatset,
305
785
  rt_floattype=self.rt_floattype,
306
786
  )
@@ -312,6 +792,11 @@ class RegressorRefiner:
312
792
  outputdict["refinelagfails_pass" + str(thepass)] = self.lagfails
313
793
  outputdict["refinesigmafails_pass" + str(thepass)] = self.sigmafails
314
794
 
795
+ if self.prewhitenregressor:
796
+ self.paddedoutputdata = tide_fit.prewhiten(
797
+ self.paddedoutputdata, self.prewhitenlags, debug=self.debug
798
+ )
799
+
315
800
  fmrifreq = 1.0 / fmritr
316
801
  if voxelsprocessed_rr > 0:
317
802
  paddednormoutputdata = tide_math.stdnormalize(
@@ -466,34 +951,138 @@ class RegressorRefiner:
466
951
 
467
952
 
468
953
  def refineRegressor(
469
- LGR,
470
- TimingLGR,
471
- thepass,
472
- optiondict,
473
- fitmask,
474
- internaloffsetincludemask_valid,
475
- internaloffsetexcludemask_valid,
476
- internalrefineincludemask_valid,
477
- internalrefineexcludemask_valid,
478
- internaldespeckleincludemask,
479
- validvoxels,
480
- theRegressorRefiner,
481
- lagtimes,
482
- lagstrengths,
483
- lagsigma,
484
- fmri_data_valid,
485
- fmritr,
486
- R2,
487
- theprefilter,
488
- previousnormoutputdata,
489
- theinputdata,
490
- numpadtrs,
491
- outputname,
492
- nativefmrishape,
493
- bidsbasedict,
494
- rt_floatset=np.float64,
495
- rt_floattype="float64",
496
- ):
954
+ LGR: Any,
955
+ TimingLGR: Any,
956
+ thepass: Any,
957
+ optiondict: Any,
958
+ fitmask: Any,
959
+ internaloffsetincludemask_valid: Any,
960
+ internaloffsetexcludemask_valid: Any,
961
+ internalrefineincludemask_valid: Any,
962
+ internalrefineexcludemask_valid: Any,
963
+ internaldespeckleincludemask: Any,
964
+ validvoxels: Any,
965
+ theRegressorRefiner: Any,
966
+ lagtimes: Any,
967
+ lagstrengths: Any,
968
+ lagsigma: Any,
969
+ fmri_data_valid: Any,
970
+ fmritr: Any,
971
+ R2: Any,
972
+ theprefilter: Any,
973
+ previousnormoutputdata: Any,
974
+ theinputdata: Any,
975
+ numpadtrs: Any,
976
+ outputname: Any,
977
+ nativefmrishape: Any,
978
+ bidsbasedict: Any,
979
+ rt_floatset: Any = np.float64,
980
+ rt_floattype: str = "float64",
981
+ debug: bool = False,
982
+ ) -> None:
983
+ """
984
+ Refine the regressor by adjusting masks, aligning timecourses, and performing refinement steps.
985
+
986
+ This function performs regressor refinement during a specified pass, including:
987
+ - Updating offset time based on lag properties
988
+ - Managing masks for refinement and despeckling
989
+ - Aligning timecourses
990
+ - Pre-normalizing data
991
+ - Executing the refinement step using a regressor refiner object
992
+
993
+ Parameters
994
+ ----------
995
+ LGR : logging.Logger
996
+ Logger instance for general logging.
997
+ TimingLGR : logging.Logger
998
+ Logger instance for timing-related messages.
999
+ thepass : int
1000
+ Current pass number in the refinement process.
1001
+ optiondict : dict
1002
+ Dictionary containing various options and settings for the refinement process.
1003
+ fitmask : array_like
1004
+ Boolean mask indicating voxels to be considered in fitting.
1005
+ internaloffsetincludemask_valid : array_like or None
1006
+ Mask for including voxels in offset calculation.
1007
+ internaloffsetexcludemask_valid : array_like or None
1008
+ Mask for excluding voxels from offset calculation.
1009
+ internalrefineincludemask_valid : array_like or None
1010
+ Mask for including voxels in refinement.
1011
+ internalrefineexcludemask_valid : array_like or None
1012
+ Mask for excluding voxels from refinement.
1013
+ internaldespeckleincludemask : array_like
1014
+ Mask for including voxels in despeckling.
1015
+ validvoxels : array_like
1016
+ Indices of valid voxels.
1017
+ theRegressorRefiner : object
1018
+ Regressor refiner object with methods for mask setting, masking, alignment, etc.
1019
+ lagtimes : array_like
1020
+ Array of lag times.
1021
+ lagstrengths : array_like
1022
+ Array of lag strengths.
1023
+ lagsigma : array_like
1024
+ Array of lag sigma values.
1025
+ fmri_data_valid : array_like
1026
+ Valid fMRI data.
1027
+ fmritr : float
1028
+ fMRI repetition time.
1029
+ R2 : array_like
1030
+ R2 values.
1031
+ theprefilter : object
1032
+ Filter object used for preprocessing.
1033
+ previousnormoutputdata : array_like
1034
+ Previously normalized output data.
1035
+ theinputdata : object
1036
+ Input data object (e.g., Nifti1Image).
1037
+ numpadtrs : int
1038
+ Number of padded timepoints.
1039
+ outputname : str
1040
+ Base name for output files.
1041
+ nativefmrishape : tuple
1042
+ Shape of the native fMRI data.
1043
+ bidsbasedict : dict
1044
+ Dictionary for BIDS metadata.
1045
+ rt_floatset : numpy.dtype, optional
1046
+ Data type for floating-point operations, default is np.float64.
1047
+ rt_floattype : str, optional
1048
+ String representation of floating-point data type, default is "float64".
1049
+ debug : bool, optional
1050
+ Enable debug mode, default is False.
1051
+
1052
+ Returns
1053
+ -------
1054
+ tuple
1055
+ A tuple containing:
1056
+ - resampref_y : array_like
1057
+ Resampled refined y values.
1058
+ - resampnonosref_y : array_like
1059
+ Resampled non-oscillatory refined y values.
1060
+ - stoprefining : bool
1061
+ Flag indicating whether refinement should stop.
1062
+ - refinestopreason : str
1063
+ Reason for stopping refinement.
1064
+ - genlagtc : array_like
1065
+ Generated lag timecourses.
1066
+
1067
+ Notes
1068
+ -----
1069
+ - This function modifies `optiondict` in-place, updating offset times and other parameters.
1070
+ - The function uses `theRegressorRefiner` to perform various refinement steps including:
1071
+ `setmasks`, `makemask`, `alignvoxels`, `prenormalize`, and `refine`.
1072
+ - If `refinedespeckled` is False and `despeckle_passes` > 0, the exclude mask for refinement
1073
+ is adjusted to include voxels being despeckled.
1074
+ - The function may exit early if no voxels qualify for refinement.
1075
+
1076
+ Examples
1077
+ --------
1078
+ >>> refineRegressor(
1079
+ ... LGR, TimingLGR, 1, optiondict, fitmask, offset_incl, offset_excl,
1080
+ ... refine_incl, refine_excl, despeckle_incl, valid_voxels, refiner,
1081
+ ... lagtimes, lagstrengths, lagsigma, fmri_data, fmritr, R2, prefilter,
1082
+ ... prev_norm_data, input_data, numpadtrs, output_name, native_shape,
1083
+ ... bids_dict, debug=True
1084
+ ... )
1085
+ """
497
1086
  LGR.info(f"\n\nRegressor refinement, pass {thepass}")
498
1087
  TimingLGR.info(f"Regressor refinement start, pass {thepass}")
499
1088
  if optiondict["refineoffset"]:
@@ -630,7 +1219,7 @@ def refineRegressor(
630
1219
  filetype=theinputdata.filetype,
631
1220
  rt_floattype=rt_floattype,
632
1221
  cifti_hdr=theinputdata.cifti_hdr,
633
- debug=True,
1222
+ debug=debug,
634
1223
  )
635
1224
 
636
1225
  return resampref_y, resampnonosref_y, stoprefining, refinestopreason, genlagtc