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