rapidtide 3.0.10__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 (141) hide show
  1. rapidtide/Colortables.py +492 -27
  2. rapidtide/OrthoImageItem.py +1053 -47
  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 +30 -1
  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/data/reference/JHU-ArterialTerritoriesNoVent-LVL1_space-MNI152NLin2009cAsym_2mm.nii.gz +0 -0
  23. rapidtide/data/reference/JHU-ArterialTerritoriesNoVent-LVL1_space-MNI152NLin2009cAsym_2mm_mask.nii.gz +0 -0
  24. rapidtide/decorators.py +91 -0
  25. rapidtide/dlfilter.py +2225 -108
  26. rapidtide/dlfiltertorch.py +4843 -0
  27. rapidtide/externaltools.py +327 -12
  28. rapidtide/fMRIData_class.py +79 -40
  29. rapidtide/filter.py +1899 -810
  30. rapidtide/fit.py +2004 -574
  31. rapidtide/genericmultiproc.py +93 -18
  32. rapidtide/happy_supportfuncs.py +2044 -171
  33. rapidtide/helper_classes.py +584 -43
  34. rapidtide/io.py +2363 -370
  35. rapidtide/linfitfiltpass.py +341 -75
  36. rapidtide/makelaggedtcs.py +211 -20
  37. rapidtide/maskutil.py +423 -53
  38. rapidtide/miscmath.py +827 -121
  39. rapidtide/multiproc.py +210 -22
  40. rapidtide/patchmatch.py +234 -33
  41. rapidtide/peakeval.py +32 -30
  42. rapidtide/ppgproc.py +2203 -0
  43. rapidtide/qualitycheck.py +352 -39
  44. rapidtide/refinedelay.py +422 -57
  45. rapidtide/refineregressor.py +498 -184
  46. rapidtide/resample.py +671 -185
  47. rapidtide/scripts/applyppgproc.py +28 -0
  48. rapidtide/simFuncClasses.py +1052 -77
  49. rapidtide/simfuncfit.py +260 -46
  50. rapidtide/stats.py +540 -238
  51. rapidtide/tests/happycomp +9 -0
  52. rapidtide/tests/test_dlfiltertorch.py +627 -0
  53. rapidtide/tests/test_findmaxlag.py +24 -8
  54. rapidtide/tests/test_fullrunhappy_v1.py +0 -2
  55. rapidtide/tests/test_fullrunhappy_v2.py +0 -2
  56. rapidtide/tests/test_fullrunhappy_v3.py +1 -0
  57. rapidtide/tests/test_fullrunhappy_v4.py +2 -2
  58. rapidtide/tests/test_fullrunrapidtide_v7.py +1 -1
  59. rapidtide/tests/test_simroundtrip.py +8 -8
  60. rapidtide/tests/utils.py +9 -8
  61. rapidtide/tidepoolTemplate.py +142 -38
  62. rapidtide/tidepoolTemplate_alt.py +165 -44
  63. rapidtide/tidepoolTemplate_big.py +189 -52
  64. rapidtide/util.py +1217 -118
  65. rapidtide/voxelData.py +684 -37
  66. rapidtide/wiener.py +19 -12
  67. rapidtide/wiener2.py +113 -7
  68. rapidtide/wiener_doc.py +255 -0
  69. rapidtide/workflows/adjustoffset.py +105 -3
  70. rapidtide/workflows/aligntcs.py +85 -2
  71. rapidtide/workflows/applydlfilter.py +87 -10
  72. rapidtide/workflows/applyppgproc.py +522 -0
  73. rapidtide/workflows/atlasaverage.py +210 -47
  74. rapidtide/workflows/atlastool.py +100 -3
  75. rapidtide/workflows/calcSimFuncMap.py +294 -64
  76. rapidtide/workflows/calctexticc.py +201 -9
  77. rapidtide/workflows/ccorrica.py +97 -4
  78. rapidtide/workflows/cleanregressor.py +168 -29
  79. rapidtide/workflows/delayvar.py +163 -10
  80. rapidtide/workflows/diffrois.py +81 -3
  81. rapidtide/workflows/endtidalproc.py +144 -4
  82. rapidtide/workflows/fdica.py +195 -15
  83. rapidtide/workflows/filtnifti.py +70 -3
  84. rapidtide/workflows/filttc.py +74 -3
  85. rapidtide/workflows/fitSimFuncMap.py +206 -48
  86. rapidtide/workflows/fixtr.py +73 -3
  87. rapidtide/workflows/gmscalc.py +113 -3
  88. rapidtide/workflows/happy.py +813 -201
  89. rapidtide/workflows/happy2std.py +144 -12
  90. rapidtide/workflows/happy_parser.py +149 -8
  91. rapidtide/workflows/histnifti.py +118 -2
  92. rapidtide/workflows/histtc.py +84 -3
  93. rapidtide/workflows/linfitfilt.py +117 -4
  94. rapidtide/workflows/localflow.py +328 -28
  95. rapidtide/workflows/mergequality.py +79 -3
  96. rapidtide/workflows/niftidecomp.py +322 -18
  97. rapidtide/workflows/niftistats.py +174 -4
  98. rapidtide/workflows/pairproc.py +88 -2
  99. rapidtide/workflows/pairwisemergenifti.py +85 -2
  100. rapidtide/workflows/parser_funcs.py +1421 -40
  101. rapidtide/workflows/physiofreq.py +137 -11
  102. rapidtide/workflows/pixelcomp.py +208 -5
  103. rapidtide/workflows/plethquality.py +103 -21
  104. rapidtide/workflows/polyfitim.py +151 -11
  105. rapidtide/workflows/proj2flow.py +75 -2
  106. rapidtide/workflows/rankimage.py +111 -4
  107. rapidtide/workflows/rapidtide.py +272 -15
  108. rapidtide/workflows/rapidtide2std.py +98 -2
  109. rapidtide/workflows/rapidtide_parser.py +109 -9
  110. rapidtide/workflows/refineDelayMap.py +143 -33
  111. rapidtide/workflows/refineRegressor.py +682 -93
  112. rapidtide/workflows/regressfrommaps.py +152 -31
  113. rapidtide/workflows/resamplenifti.py +85 -3
  114. rapidtide/workflows/resampletc.py +91 -3
  115. rapidtide/workflows/retrolagtcs.py +98 -6
  116. rapidtide/workflows/retroregress.py +165 -9
  117. rapidtide/workflows/roisummarize.py +173 -5
  118. rapidtide/workflows/runqualitycheck.py +71 -3
  119. rapidtide/workflows/showarbcorr.py +147 -4
  120. rapidtide/workflows/showhist.py +86 -2
  121. rapidtide/workflows/showstxcorr.py +160 -3
  122. rapidtide/workflows/showtc.py +159 -3
  123. rapidtide/workflows/showxcorrx.py +184 -4
  124. rapidtide/workflows/showxy.py +185 -15
  125. rapidtide/workflows/simdata.py +262 -36
  126. rapidtide/workflows/spatialfit.py +77 -2
  127. rapidtide/workflows/spatialmi.py +251 -27
  128. rapidtide/workflows/spectrogram.py +305 -32
  129. rapidtide/workflows/synthASL.py +154 -3
  130. rapidtide/workflows/tcfrom2col.py +76 -2
  131. rapidtide/workflows/tcfrom3col.py +74 -2
  132. rapidtide/workflows/tidepool.py +2972 -133
  133. rapidtide/workflows/utils.py +19 -14
  134. rapidtide/workflows/utils_doc.py +293 -0
  135. rapidtide/workflows/variabilityizer.py +116 -3
  136. {rapidtide-3.0.10.dist-info → rapidtide-3.1.dist-info}/METADATA +10 -9
  137. {rapidtide-3.0.10.dist-info → rapidtide-3.1.dist-info}/RECORD +141 -122
  138. {rapidtide-3.0.10.dist-info → rapidtide-3.1.dist-info}/entry_points.txt +1 -0
  139. {rapidtide-3.0.10.dist-info → rapidtide-3.1.dist-info}/WHEEL +0 -0
  140. {rapidtide-3.0.10.dist-info → rapidtide-3.1.dist-info}/licenses/LICENSE +0 -0
  141. {rapidtide-3.0.10.dist-info → rapidtide-3.1.dist-info}/top_level.txt +0 -0
rapidtide/simfuncfit.py CHANGED
@@ -19,8 +19,10 @@
19
19
  import bisect
20
20
  import gc
21
21
  import logging
22
+ from typing import Any, Callable, Optional, Tuple, Union
22
23
 
23
24
  import numpy as np
25
+ from numpy.typing import ArrayLike, NDArray
24
26
  from tqdm import tqdm
25
27
 
26
28
  import rapidtide.fit as tide_fit
@@ -30,17 +32,77 @@ LGR = logging.getLogger("GENERAL")
30
32
 
31
33
 
32
34
  def onesimfuncfit(
33
- correlationfunc,
34
- thefitter,
35
- disablethresholds=False,
36
- initiallag=None,
37
- despeckle_thresh=5.0,
38
- lthreshval=0.0,
39
- fixdelay=False,
40
- initialdelayvalue=0.0,
41
- rt_floatset=np.float64,
42
- rt_floattype="float64",
43
- ):
35
+ correlationfunc: ArrayLike,
36
+ thefitter: Any,
37
+ disablethresholds: bool = False,
38
+ initiallag: Optional[float] = None,
39
+ despeckle_thresh: float = 5.0,
40
+ lthreshval: float = 0.0,
41
+ fixdelay: bool = False,
42
+ initialdelayvalue: float = 0.0,
43
+ rt_floatset: type = np.float64,
44
+ rt_floattype: str = "float64",
45
+ ) -> Tuple[int, float, float, float, int, int, int, int]:
46
+ """
47
+ Perform a single fit on a correlation function using the provided fitter.
48
+
49
+ This function sets up the fitter with initial parameters and thresholds,
50
+ then performs a fit on the provided correlation function. If `fixdelay` is
51
+ True, the fit is skipped and a fixed delay value is used instead.
52
+
53
+ Parameters
54
+ ----------
55
+ correlationfunc : ArrayLike
56
+ The correlation function data to be fitted.
57
+ thefitter : Any
58
+ An object with methods `setguess`, `setrange`, `setlthresh`, and `fit`.
59
+ disablethresholds : bool, optional
60
+ If True, disables the threshold setting in the fitter. Default is False.
61
+ initiallag : float, optional
62
+ Initial guess for the lag value. If None, no initial guess is set.
63
+ Default is None.
64
+ despeckle_thresh : float, optional
65
+ Threshold for despeckling. Default is 5.0.
66
+ lthreshval : float, optional
67
+ Low threshold value for the fitter. Default is 0.0.
68
+ fixdelay : bool, optional
69
+ If True, uses a fixed delay value instead of performing a fit.
70
+ Default is False.
71
+ initialdelayvalue : float, optional
72
+ The fixed delay value to use when `fixdelay=True`. Default is 0.0.
73
+ rt_floatset : type, optional
74
+ The data type to use for floating-point values. Default is `np.float64`.
75
+ rt_floattype : str, optional
76
+ String representation of the floating-point type. Default is "float64".
77
+
78
+ Returns
79
+ -------
80
+ tuple of (int, float, float, float, int, int, int, int)
81
+ A tuple containing:
82
+ - maxindex (int): Index of the maximum value in the correlation function.
83
+ - maxlag (float): The lag value at the maximum.
84
+ - maxval (float): The maximum value in the correlation function.
85
+ - maxsigma (float): The sigma (standard deviation) of the fit.
86
+ - maskval (int): A mask value indicating fit quality.
87
+ - peakstart (int): Start index of the fitted peak.
88
+ - peakend (int): End index of the fitted peak.
89
+ - failreason (int): Reason for fit failure (0 if successful).
90
+
91
+ Notes
92
+ -----
93
+ When `fixdelay=True`, the function bypasses the fitting process and returns
94
+ precomputed values based on `initialdelayvalue`.
95
+
96
+ Examples
97
+ --------
98
+ >>> import numpy as np
99
+ >>> from some_module import some_fitter_class
100
+ >>> corr_func = np.random.rand(100)
101
+ >>> fitter = some_fitter_class()
102
+ >>> result = onesimfuncfit(corr_func, fitter)
103
+ >>> print(result)
104
+ (50, 0.5, 0.95, 0.02, 1, 45, 55, 0)
105
+ """
44
106
  if initiallag is not None:
45
107
  thefitter.setguess(True, maxguess=initiallag)
46
108
  thefitter.setrange(-despeckle_thresh / 2.0, despeckle_thresh / 2.0)
@@ -78,17 +140,84 @@ def onesimfuncfit(
78
140
 
79
141
 
80
142
  def _procOneVoxelFitcorr(
81
- vox,
82
- corr_y,
83
- thefitter,
84
- disablethresholds=False,
85
- despeckle_thresh=5.0,
86
- initiallag=None,
87
- fixdelay=False,
88
- initialdelayvalue=0.0,
89
- rt_floatset=np.float64,
90
- rt_floattype="float64",
91
- ):
143
+ vox: int,
144
+ corr_y: ArrayLike,
145
+ thefitter: Any,
146
+ disablethresholds: bool = False,
147
+ despeckle_thresh: float = 5.0,
148
+ initiallag: Optional[float] = None,
149
+ fixdelay: bool = False,
150
+ initialdelayvalue: float = 0.0,
151
+ rt_floatset: type = np.float64,
152
+ rt_floattype: str = "float64",
153
+ ) -> Tuple[int, int, float, float, float, NDArray, NDArray, float, int, int]:
154
+ """
155
+ Process a single voxel for correlation fitting.
156
+
157
+ This function performs correlation fitting on a single voxel's data using the provided fitter.
158
+ It returns fitting results including time, strength, sigma, Gaussian fit, window mask, R² value,
159
+ and metadata such as mask value and failure reason.
160
+
161
+ Parameters
162
+ ----------
163
+ vox : int
164
+ Voxel index.
165
+ corr_y : ArrayLike
166
+ Correlation data for the voxel.
167
+ thefitter : Any
168
+ Fitter object containing fitting parameters and methods.
169
+ disablethresholds : bool, optional
170
+ If True, disables thresholding during fitting. Default is False.
171
+ despeckle_thresh : float, optional
172
+ Threshold for despeckling. Default is 5.0.
173
+ initiallag : float, optional
174
+ Initial lag value for fitting. Default is None.
175
+ fixdelay : bool, optional
176
+ If True, fixes the delay during fitting. Default is False.
177
+ initialdelayvalue : float, optional
178
+ Initial delay value if `fixdelay` is True. Default is 0.0.
179
+ rt_floatset : type, optional
180
+ Type to use for real-valued floating-point arrays. Default is `np.float64`.
181
+ rt_floattype : str, optional
182
+ String representation of the floating-point type. Default is "float64".
183
+
184
+ Returns
185
+ -------
186
+ tuple of (int, int, float, float, float, ndarray, ndarray, float, int, int)
187
+ A tuple containing:
188
+ - `vox`: Voxel index.
189
+ - `volumetotalinc`: 1 if fit was successful, 0 otherwise.
190
+ - `thetime`: Fitted time value.
191
+ - `thestrength`: Fitted strength value.
192
+ - `thesigma`: Fitted sigma value.
193
+ - `thegaussout`: Gaussian fit evaluated over the time axis.
194
+ - `thewindowout`: Binary window mask indicating the peak region.
195
+ - `theR2`: R-squared value of the fit.
196
+ - `maskval`: Mask value from the fitting process.
197
+ - `failreason`: Reason for failure, if any.
198
+
199
+ Notes
200
+ -----
201
+ - If `maxval > 0.3`, plotting is disabled.
202
+ - The function uses `onesimfuncfit` to perform the actual fitting.
203
+ - The `thefitter` object must have attributes like `zerooutbadfit`, `lagmod`, and `corrtimeaxis`.
204
+
205
+ Examples
206
+ --------
207
+ >>> result = _procOneVoxelFitcorr(
208
+ ... vox=10,
209
+ ... corr_y=corr_data,
210
+ ... thefitter=fitter_obj,
211
+ ... disablethresholds=False,
212
+ ... despeckle_thresh=5.0,
213
+ ... fixdelay=False,
214
+ ... initialdelayvalue=0.0,
215
+ ... rt_floatset=np.float64,
216
+ ... rt_floattype="float64"
217
+ ... )
218
+ >>> print(result)
219
+ (10, 1, 1.23, 0.95, 0.12, array([...]), array([...]), 0.90, 1, 0)
220
+ """
92
221
  (
93
222
  maxindex,
94
223
  maxlag,
@@ -154,30 +283,115 @@ def _procOneVoxelFitcorr(
154
283
 
155
284
 
156
285
  def fitcorr(
157
- corrtimescale,
158
- thefitter,
159
- corrout,
160
- lagmask,
161
- failimage,
162
- lagtimes,
163
- lagstrengths,
164
- lagsigma,
165
- gaussout,
166
- windowout,
167
- R2,
168
- despeckling=False,
169
- peakdict=None,
170
- nprocs=1,
171
- alwaysmultiproc=False,
172
- fixdelay=False,
173
- initialdelayvalue=0.0,
174
- showprogressbar=True,
175
- chunksize=1000,
176
- despeckle_thresh=5.0,
177
- initiallags=None,
178
- rt_floatset=np.float64,
179
- rt_floattype="float64",
180
- ):
286
+ corrtimescale: ArrayLike,
287
+ thefitter: Any,
288
+ corrout: NDArray,
289
+ lagmask: NDArray,
290
+ failimage: NDArray,
291
+ lagtimes: NDArray,
292
+ lagstrengths: NDArray,
293
+ lagsigma: NDArray,
294
+ gaussout: NDArray,
295
+ windowout: NDArray,
296
+ R2: NDArray,
297
+ despeckling: bool = False,
298
+ peakdict: Optional[dict] = None,
299
+ nprocs: int = 1,
300
+ alwaysmultiproc: bool = False,
301
+ fixdelay: bool = False,
302
+ initialdelayvalue: Union[float, NDArray] = 0.0,
303
+ showprogressbar: bool = True,
304
+ chunksize: int = 1000,
305
+ despeckle_thresh: float = 5.0,
306
+ initiallags: Optional[NDArray] = None,
307
+ rt_floatset: type = np.float64,
308
+ rt_floattype: str = "float64",
309
+ ) -> int:
310
+ """
311
+ Fit correlation data to extract lag parameters and related statistics for each voxel.
312
+
313
+ This function performs a fitting procedure on correlation data for each voxel,
314
+ extracting lag times, strengths, sigma values, Gaussian fits, window functions,
315
+ and R² values. It supports both single-threaded and multi-threaded processing.
316
+
317
+ Parameters
318
+ ----------
319
+ corrtimescale : ArrayLike
320
+ Time scale of the correlation data.
321
+ thefitter : Any
322
+ Fitter object used to perform the fitting. Must have methods like `setcorrtimeaxis`.
323
+ corrout : NDArray
324
+ Correlation data for all voxels, shape (n_voxels, n_timepoints).
325
+ lagmask : NDArray
326
+ Mask indicating valid lags for each voxel, shape (n_voxels,).
327
+ failimage : NDArray
328
+ Image to store failure flags for each voxel, shape (n_voxels,).
329
+ lagtimes : NDArray
330
+ Output array for lag times, shape (n_voxels,).
331
+ lagstrengths : NDArray
332
+ Output array for lag strengths, shape (n_voxels,).
333
+ lagsigma : NDArray
334
+ Output array for lag sigma values, shape (n_voxels,).
335
+ gaussout : NDArray
336
+ Output array for Gaussian fit parameters, shape (n_voxels, n_timepoints).
337
+ windowout : NDArray
338
+ Output array for window function values, shape (n_voxels, n_timepoints).
339
+ R2 : NDArray
340
+ Output array for R² values, shape (n_voxels,).
341
+ despeckling : bool, optional
342
+ If True, performs despeckling pass, only accepting successful fits, by default False.
343
+ peakdict : dict, optional
344
+ Dictionary of peak information, by default None.
345
+ nprocs : int, optional
346
+ Number of processes to use for multiprocessing, by default 1.
347
+ alwaysmultiproc : bool, optional
348
+ If True, always use multiprocessing even for single process, by default False.
349
+ fixdelay : bool, optional
350
+ If True, fix the delay value, by default False.
351
+ initialdelayvalue : Union[float, NDArray], optional
352
+ Initial delay value(s), by default 0.0.
353
+ showprogressbar : bool, optional
354
+ If True, show progress bar, by default True.
355
+ chunksize : int, optional
356
+ Size of chunks for multiprocessing, by default 1000.
357
+ despeckle_thresh : float, optional
358
+ Threshold for despeckling, by default 5.0.
359
+ initiallags : NDArray, optional
360
+ Initial lag values for each voxel, by default None.
361
+ rt_floatset : type, optional
362
+ Floating-point type for runtime, by default np.float64.
363
+ rt_floattype : str, optional
364
+ String representation of floating-point type, by default "float64".
365
+
366
+ Returns
367
+ -------
368
+ int
369
+ Total number of voxels successfully processed.
370
+
371
+ Notes
372
+ -----
373
+ The function modifies the input arrays (`lagtimes`, `lagstrengths`, `lagsigma`,
374
+ `gaussout`, `windowout`, `R2`, `lagmask`, `failimage`) in-place.
375
+
376
+ Examples
377
+ --------
378
+ >>> fitcorr(
379
+ ... corrtimescale=timescale,
380
+ ... thefitter=fitter,
381
+ ... corrout=correlation_data,
382
+ ... lagmask=lag_mask,
383
+ ... failimage=fail_image,
384
+ ... lagtimes=lag_times,
385
+ ... lagstrengths=lag_strengths,
386
+ ... lagsigma=lag_sigma,
387
+ ... gaussout=gaussian_out,
388
+ ... windowout=window_out,
389
+ ... R2=r2_values,
390
+ ... nprocs=4,
391
+ ... despeckling=True,
392
+ ... )
393
+ 12345
394
+ """
181
395
  thefitter.setcorrtimeaxis(corrtimescale)
182
396
  inputshape = np.shape(corrout)
183
397
  if initiallags is None: