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
@@ -21,9 +21,11 @@ import logging
21
21
  import os
22
22
  import sys
23
23
  from argparse import Namespace
24
+ from typing import Any, Callable, Dict, List, Optional, Tuple, Union
24
25
 
25
26
  import nibabel as nib
26
27
  import numpy as np
28
+ from numpy.typing import NDArray
27
29
 
28
30
  import rapidtide.io as tide_io
29
31
  import rapidtide.util as tide_util
@@ -108,10 +110,37 @@ DEFAULT_DELAYOFFSETSPATIALFILT = -1
108
110
  DEFAULT_PATCHMINSIZE = 10
109
111
  DEFAULT_PATCHFWHM = 5
110
112
 
113
+ DEFAULT_PREWHITEN_LAGS = -1
111
114
 
112
- def _get_parser():
115
+
116
+ def _get_parser() -> Any:
113
117
  """
114
- Argument parser for rapidtide
118
+ Set up the argument parser for rapidtide command-line interface.
119
+
120
+ This function configures all available command-line arguments for the rapidtide
121
+ tool, organizing them into logical groups for easy reference and use.
122
+
123
+ Returns
124
+ -------
125
+ argparse.ArgumentParser
126
+ Configured argument parser with all rapidtide options added
127
+
128
+ Notes
129
+ -----
130
+ The parser includes several groups of options:
131
+ - Input/Output options
132
+ - Processing options
133
+ - Performance options
134
+ - Miscellaneous options
135
+ - Experimental options (not fully tested)
136
+ - Deprecated options (will be removed in future versions)
137
+ - Debugging options (for development and troubleshooting)
138
+
139
+ Examples
140
+ --------
141
+ >>> parser = setup_parser()
142
+ >>> args = parser.parse_args()
143
+ >>> print(args.version)
115
144
  """
116
145
  parser = argparse.ArgumentParser(
117
146
  prog="rapidtide",
@@ -1445,7 +1474,7 @@ def _get_parser():
1445
1474
  experimental = parser.add_argument_group(
1446
1475
  "Experimental options (not fully tested, or not tested at all, may not work). Beware!"
1447
1476
  )
1448
- output.add_argument(
1477
+ experimental.add_argument(
1449
1478
  "--riptidestep", # was -h
1450
1479
  dest="riptidestep",
1451
1480
  action="store",
@@ -1572,6 +1601,24 @@ def _get_parser():
1572
1601
  help=("Perform patch shift correction."),
1573
1602
  default=False,
1574
1603
  )
1604
+ experimental.add_argument(
1605
+ "--prewhitenregressor",
1606
+ dest="prewhitenregressor",
1607
+ action="store_true",
1608
+ help=("Prewhiten probe regressor prior to calculating correlations."),
1609
+ default=False,
1610
+ )
1611
+ experimental.add_argument(
1612
+ "--prewhitenlags",
1613
+ dest="prewhitenlags",
1614
+ action="store",
1615
+ type=lambda x: pf.is_int(parser, x, minval=0),
1616
+ metavar="LAGS",
1617
+ help=(
1618
+ f"Set number of TRs to use in prewhitening. Set to -1 to calculate automatically. Default is {DEFAULT_PREWHITEN_LAGS}."
1619
+ ),
1620
+ default=DEFAULT_PREWHITEN_LAGS,
1621
+ )
1575
1622
 
1576
1623
  # Deprecated options
1577
1624
  deprecated = parser.add_argument_group(
@@ -1731,9 +1778,51 @@ def _get_parser():
1731
1778
  return parser
1732
1779
 
1733
1780
 
1734
- def process_args(inputargs=None):
1781
+ def process_args(inputargs: Optional[Any] = None) -> Tuple[Any, object]:
1735
1782
  """
1736
1783
  Compile arguments for rapidtide workflow.
1784
+
1785
+ This function processes command-line arguments and sets up the configuration
1786
+ for the rapidtide workflow. It handles argument parsing, logging setup,
1787
+ command-line saving, and various parameter defaults and overrides based on
1788
+ analysis modes and macros.
1789
+
1790
+ Parameters
1791
+ ----------
1792
+ inputargs : optional
1793
+ Input arguments to be processed. If None, arguments are parsed from
1794
+ sys.argv. Default is None.
1795
+
1796
+ Returns
1797
+ -------
1798
+ tuple
1799
+ A tuple containing:
1800
+ - args : dict
1801
+ Dictionary of processed arguments.
1802
+ - theprefilter : object
1803
+ Preprocessing filter object.
1804
+
1805
+ Notes
1806
+ -----
1807
+ The function performs the following key operations:
1808
+ 1. Parses command-line arguments using `_get_parser`
1809
+ 2. Sets up logging based on debug flag
1810
+ 3. Saves raw and formatted command lines to files
1811
+ 4. Applies default values and overrides for various parameters
1812
+ 5. Handles analysis modes (delaymapping, denoising, cvrmap, etc.)
1813
+ 6. Processes macros (venousrefine, nirs)
1814
+ 7. Configures output options based on `outputlevel`
1815
+ 8. Sets up pass options and dispersion calculation parameters
1816
+ 9. Handles mask specifications and file processing
1817
+ 10. Initializes filter options
1818
+
1819
+ Examples
1820
+ --------
1821
+ >>> args, prefilter = process_args()
1822
+ >>> print(args['passes'])
1823
+ 1
1824
+ >>> print(args['outputlevel'])
1825
+ 'normal'
1737
1826
  """
1738
1827
  inargs, argstowrite = pf.setargs(_get_parser, inputargs=inputargs)
1739
1828
  args = vars(inargs)
@@ -1747,7 +1836,7 @@ def process_args(inputargs=None):
1747
1836
  # save the raw and formatted command lines
1748
1837
  args["commandlineargs"] = argstowrite[1:]
1749
1838
  thecommandline = " ".join(argstowrite)
1750
- tide_io.writevec([thecommandline], args["outputname"] + "_commandline.txt")
1839
+ tide_io.writevec(np.array([thecommandline]), args["outputname"] + "_commandline.txt")
1751
1840
  formattedcommandline = []
1752
1841
  for thetoken in argstowrite[0:3]:
1753
1842
  formattedcommandline.append(thetoken)
@@ -1766,7 +1855,9 @@ def process_args(inputargs=None):
1766
1855
  else:
1767
1856
  suffix = ""
1768
1857
  formattedcommandline[i] = prefix + formattedcommandline[i] + suffix
1769
- tide_io.writevec(formattedcommandline, args["outputname"] + "_formattedcommandline.txt")
1858
+ tide_io.writevec(
1859
+ np.array(formattedcommandline), args["outputname"] + "_formattedcommandline.txt"
1860
+ )
1770
1861
 
1771
1862
  LGR.debug("\nbefore postprocessing:\n{}".format(args))
1772
1863
 
@@ -1847,6 +1938,9 @@ def process_args(inputargs=None):
1847
1938
  args["lagmin"] = args["lag_extrema"][0]
1848
1939
  args["lagmax"] = args["lag_extrema"][1]
1849
1940
 
1941
+ if args["prewhitenlags"] == -1:
1942
+ args["prewhitenlags"] = int(np.max([np.fabs(args["lagmin"]), np.fabs(args["lagmax"])]))
1943
+
1850
1944
  # set startpoint and endpoint
1851
1945
  args["startpoint"], args["endpoint"] = pf.parserange(args["timerange"], descriptor="timerange")
1852
1946
 
@@ -2222,9 +2316,15 @@ def process_args(inputargs=None):
2222
2316
 
2223
2317
  # make the pass options dictionary
2224
2318
  args["passoptions"] = [
2225
- {"similaritymetric": "riptide",},
2226
- {"similaritymetric": "correlation", },
2227
- {"similaritymetric": "correlation", },
2319
+ {
2320
+ "similaritymetric": "riptide",
2321
+ },
2322
+ {
2323
+ "similaritymetric": "correlation",
2324
+ },
2325
+ {
2326
+ "similaritymetric": "correlation",
2327
+ },
2228
2328
  ]
2229
2329
 
2230
2330
  # dispersion calculation
@@ -16,46 +16,156 @@
16
16
  # limitations under the License.
17
17
  #
18
18
  #
19
+ from typing import Any, Callable, Dict, List, Optional, Tuple, Union
20
+
19
21
  import numpy as np
22
+ from numpy.typing import NDArray
20
23
 
21
24
  import rapidtide.refinedelay as tide_refinedelay
22
25
  import rapidtide.stats as tide_stats
23
26
 
24
27
 
25
28
  def refineDelay(
26
- fmri_data_valid,
27
- initial_fmri_x,
28
- xdim,
29
- ydim,
30
- slicethickness,
31
- sLFOfiltmask,
32
- genlagtc,
33
- oversamptr,
34
- sLFOfitmean,
35
- rvalue,
36
- r2value,
37
- fitNorm,
38
- fitcoeff,
39
- lagtc,
40
- outputname,
41
- validvoxels,
42
- nativespaceshape,
43
- theinputdata,
44
- lagtimes,
45
- optiondict,
46
- LGR,
47
- TimingLGR,
48
- outputlevel="normal",
49
- gausssigma=-1,
50
- patchthresh=3.0,
51
- mindelay=-5.0,
52
- maxdelay=5.0,
53
- numpoints=501,
54
- histlen=101,
55
- rt_floatset=np.float64,
56
- rt_floattype="float64",
57
- debug=False,
58
- ):
29
+ fmri_data_valid: Any,
30
+ initial_fmri_x: Any,
31
+ xdim: Any,
32
+ ydim: Any,
33
+ slicethickness: Any,
34
+ sLFOfiltmask: Any,
35
+ genlagtc: Any,
36
+ oversamptr: Any,
37
+ sLFOfitmean: Any,
38
+ rvalue: Any,
39
+ r2value: Any,
40
+ fitNorm: Any,
41
+ fitcoeff: Any,
42
+ lagtc: Any,
43
+ outputname: Any,
44
+ validvoxels: Any,
45
+ nativespaceshape: Any,
46
+ theinputdata: Any,
47
+ lagtimes: Any,
48
+ optiondict: Any,
49
+ LGR: Any,
50
+ TimingLGR: Any,
51
+ outputlevel: str = "normal",
52
+ gausssigma: int = -1,
53
+ patchthresh: float = 3.0,
54
+ mindelay: float = -5.0,
55
+ maxdelay: float = 5.0,
56
+ numpoints: int = 501,
57
+ histlen: int = 101,
58
+ rt_floatset: Any = np.float64,
59
+ rt_floattype: str = "float64",
60
+ debug: bool = False,
61
+ ) -> None:
62
+ """
63
+ Refine delay estimates using regression derivative ratios and histogram-based calibration.
64
+
65
+ This function performs calibration of delay estimates by computing regression derivative
66
+ ratios, filtering them, training a mapping from ratios to delays, and finally calculating
67
+ delay offsets for each voxel. It also generates a histogram of the computed delay offsets.
68
+
69
+ Parameters
70
+ ----------
71
+ fmri_data_valid : Any
72
+ Valid fMRI data used for delay refinement.
73
+ initial_fmri_x : Any
74
+ Initial fMRI design matrix.
75
+ xdim : Any
76
+ X dimension of the data.
77
+ ydim : Any
78
+ Y dimension of the data.
79
+ slicethickness : Any
80
+ Thickness of the slices in the data.
81
+ sLFOfiltmask : Any
82
+ Mask for filtering based on SLOF (slice timing) effects.
83
+ genlagtc : Any
84
+ Generated lag time course.
85
+ oversamptr : Any
86
+ Oversampling time resolution.
87
+ sLFOfitmean : Any
88
+ Mean SLOF fit values.
89
+ rvalue : Any
90
+ R-values from regression.
91
+ r2value : Any
92
+ R-squared values from regression.
93
+ fitNorm : Any
94
+ Normalized fit coefficients.
95
+ fitcoeff : Any
96
+ Fit coefficients.
97
+ lagtc : Any
98
+ Lag time course.
99
+ outputname : Any
100
+ Base name for output files.
101
+ validvoxels : Any
102
+ Indices of valid voxels.
103
+ nativespaceshape : Any
104
+ Shape of the native space.
105
+ theinputdata : Any
106
+ Input data object.
107
+ lagtimes : Any
108
+ Time lags used for analysis.
109
+ optiondict : Any
110
+ Dictionary of options for processing.
111
+ LGR : Any
112
+ Logger for general messages.
113
+ TimingLGR : Any
114
+ Logger for timing-related messages.
115
+ outputlevel : str, optional
116
+ Level of output verbosity, default is "normal".
117
+ gausssigma : int, optional
118
+ Sigma for Gaussian filtering, default is -1 (no filtering).
119
+ patchthresh : float, optional
120
+ Threshold for patch-based filtering, default is 3.0.
121
+ mindelay : float, optional
122
+ Minimum delay value, default is -5.0.
123
+ maxdelay : float, optional
124
+ Maximum delay value, default is 5.0.
125
+ numpoints : int, optional
126
+ Number of points for delay interpolation, default is 501.
127
+ histlen : int, optional
128
+ Length of histogram bins, default is 101.
129
+ rt_floatset : Any, optional
130
+ Data type for real-time float operations, default is np.float64.
131
+ rt_floattype : str, optional
132
+ String representation of float type, default is "float64".
133
+ debug : bool, optional
134
+ Enable debug mode, default is False.
135
+
136
+ Returns
137
+ -------
138
+ tuple
139
+ A tuple containing:
140
+ - delayoffset : ndarray
141
+ Calculated delay offsets for each voxel.
142
+ - regressderivratios : ndarray
143
+ Raw regression derivative ratios.
144
+ - medfiltregressderivratios : ndarray
145
+ Median-filtered regression derivative ratios.
146
+ - filteredregressderivratios : ndarray
147
+ Final filtered regression derivative ratios.
148
+ - delayoffsetMAD : ndarray
149
+ Median absolute deviation of delay offsets.
150
+
151
+ Notes
152
+ -----
153
+ The function uses the `tide_refinedelay` module to perform various steps:
154
+ 1. Computes regression derivative ratios using `getderivratios`.
155
+ 2. Filters these ratios using `filterderivratios`.
156
+ 3. Trains a ratio-to-delay mapping using `trainratiotooffset`.
157
+ 4. Converts ratios to delay offsets using `ratiotodelay`.
158
+ 5. Saves a histogram of delay offsets to disk.
159
+
160
+ Examples
161
+ --------
162
+ >>> delayoffset, regressderivratios, medfiltregressderivratios, filteredregressderivratios, delayoffsetMAD = refineDelay(
163
+ ... fmri_data_valid, initial_fmri_x, xdim, ydim, slicethickness,
164
+ ... sLFOfiltmask, genlagtc, oversamptr, sLFOfitmean, rvalue, r2value,
165
+ ... fitNorm, fitcoeff, lagtc, outputname, validvoxels, nativespaceshape,
166
+ ... theinputdata, lagtimes, optiondict, LGR, TimingLGR
167
+ ... )
168
+ """
59
169
  # do the calibration
60
170
  TimingLGR.info("Refinement calibration start")
61
171
  regressderivratios, regressrvalues = tide_refinedelay.getderivratios(