rapidtide 3.0a14__py3-none-any.whl → 3.0a15__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 (231) hide show
  1. rapidtide/Colortables.py +1 -1
  2. rapidtide/DerivativeDelay.py +1 -1
  3. rapidtide/OrthoImageItem.py +1 -1
  4. rapidtide/RapidtideDataset.py +3 -1
  5. rapidtide/RegressorRefiner.py +1 -1
  6. rapidtide/calcandfitcorrpairs.py +1 -1
  7. rapidtide/calccoherence.py +1 -1
  8. rapidtide/calcnullsimfunc.py +1 -1
  9. rapidtide/calcsimfunc.py +1 -1
  10. rapidtide/correlate.py +1 -1
  11. rapidtide/data/examples/src/test_findmaxlag.py +1 -1
  12. rapidtide/data/examples/src/testfmri +3 -2
  13. rapidtide/data/examples/src/testfuncs +1 -1
  14. rapidtide/data/examples/src/testhappy +12 -1
  15. rapidtide/data/examples/src/testmodels +33 -0
  16. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/loss.png +0 -0
  17. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/loss.txt +1 -0
  18. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/model.keras +0 -0
  19. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/model_meta.json +167 -0
  20. rapidtide/data/models/model_revised_tf2/model.keras +0 -0
  21. rapidtide/data/models/{model_serdar → model_revised_tf2}/model_meta.json +1 -1
  22. rapidtide/data/models/model_serdar2_tf2/model.keras +0 -0
  23. rapidtide/data/models/{model_serdar2 → model_serdar2_tf2}/model_meta.json +1 -1
  24. rapidtide/data/models/model_serdar_tf2/model.keras +0 -0
  25. rapidtide/data/models/{model_revised → model_serdar_tf2}/model_meta.json +1 -1
  26. rapidtide/dlfilter.py +325 -241
  27. rapidtide/externaltools.py +1 -1
  28. rapidtide/fMRIData_class.py +1 -1
  29. rapidtide/filter.py +1 -8
  30. rapidtide/fit.py +1 -8
  31. rapidtide/happy_supportfuncs.py +30 -1
  32. rapidtide/helper_classes.py +1 -1
  33. rapidtide/io.py +1 -1
  34. rapidtide/linfitfiltpass.py +1 -1
  35. rapidtide/makelaggedtcs.py +1 -1
  36. rapidtide/maskutil.py +1 -1
  37. rapidtide/miscmath.py +1 -8
  38. rapidtide/multiproc.py +1 -1
  39. rapidtide/patchmatch.py +1 -1
  40. rapidtide/peakeval.py +1 -1
  41. rapidtide/qualitycheck.py +1 -1
  42. rapidtide/refinedelay.py +1 -3
  43. rapidtide/refineregressor.py +1 -1
  44. rapidtide/resample.py +1 -1
  45. rapidtide/scripts/adjustoffset.py +1 -1
  46. rapidtide/scripts/aligntcs.py +1 -1
  47. rapidtide/scripts/applydlfilter.py +1 -1
  48. rapidtide/scripts/atlasaverage.py +1 -1
  49. rapidtide/scripts/atlastool.py +1 -1
  50. rapidtide/scripts/calcicc.py +1 -1
  51. rapidtide/scripts/calctexticc.py +1 -1
  52. rapidtide/scripts/calcttest.py +1 -1
  53. rapidtide/scripts/ccorrica.py +1 -1
  54. rapidtide/scripts/delayvar.py +2 -2
  55. rapidtide/scripts/diffrois.py +1 -1
  56. rapidtide/scripts/endtidalproc.py +1 -1
  57. rapidtide/scripts/fdica.py +1 -1
  58. rapidtide/scripts/filtnifti.py +1 -1
  59. rapidtide/scripts/filttc.py +1 -1
  60. rapidtide/scripts/fingerprint.py +1 -1
  61. rapidtide/scripts/fixtr.py +1 -1
  62. rapidtide/scripts/gmscalc.py +1 -1
  63. rapidtide/scripts/happy.py +1 -1
  64. rapidtide/scripts/happy2std.py +1 -1
  65. rapidtide/scripts/happywarp.py +1 -1
  66. rapidtide/scripts/histnifti.py +1 -1
  67. rapidtide/scripts/histtc.py +1 -1
  68. rapidtide/scripts/linfitfilt.py +1 -1
  69. rapidtide/scripts/localflow.py +1 -1
  70. rapidtide/scripts/mergequality.py +1 -1
  71. rapidtide/scripts/pairproc.py +1 -1
  72. rapidtide/scripts/pairwisemergenifti.py +1 -1
  73. rapidtide/scripts/physiofreq.py +1 -1
  74. rapidtide/scripts/pixelcomp.py +1 -1
  75. rapidtide/scripts/plethquality.py +1 -1
  76. rapidtide/scripts/polyfitim.py +1 -1
  77. rapidtide/scripts/proj2flow.py +1 -1
  78. rapidtide/scripts/rankimage.py +1 -1
  79. rapidtide/scripts/rapidtide.py +1 -1
  80. rapidtide/scripts/rapidtide2std.py +1 -1
  81. rapidtide/scripts/resamplenifti.py +1 -1
  82. rapidtide/scripts/resampletc.py +1 -1
  83. rapidtide/scripts/retrolagtcs.py +1 -1
  84. rapidtide/scripts/retroregress.py +1 -1
  85. rapidtide/scripts/roisummarize.py +1 -1
  86. rapidtide/scripts/runqualitycheck.py +1 -1
  87. rapidtide/scripts/showarbcorr.py +1 -1
  88. rapidtide/scripts/showhist.py +1 -1
  89. rapidtide/scripts/showstxcorr.py +1 -1
  90. rapidtide/scripts/showtc.py +1 -1
  91. rapidtide/scripts/showxcorr_legacy.py +1 -1
  92. rapidtide/scripts/showxcorrx.py +1 -1
  93. rapidtide/scripts/showxy.py +1 -1
  94. rapidtide/scripts/simdata.py +1 -1
  95. rapidtide/scripts/spatialdecomp.py +1 -1
  96. rapidtide/scripts/spatialfit.py +1 -1
  97. rapidtide/scripts/spatialmi.py +1 -1
  98. rapidtide/scripts/spectrogram.py +1 -1
  99. rapidtide/scripts/stupidramtricks.py +1 -1
  100. rapidtide/scripts/synthASL.py +1 -1
  101. rapidtide/scripts/tcfrom2col.py +1 -1
  102. rapidtide/scripts/tcfrom3col.py +1 -1
  103. rapidtide/scripts/temporaldecomp.py +1 -1
  104. rapidtide/scripts/testhrv.py +1 -1
  105. rapidtide/scripts/threeD.py +1 -1
  106. rapidtide/scripts/tidepool.py +1 -1
  107. rapidtide/scripts/variabilityizer.py +1 -1
  108. rapidtide/simfuncfit.py +1 -1
  109. rapidtide/stats.py +1 -8
  110. rapidtide/tests/cleanposttest +1 -1
  111. rapidtide/tests/resethappytargets +1 -1
  112. rapidtide/tests/resetrapidtidetargets +1 -1
  113. rapidtide/tests/resettargets +1 -1
  114. rapidtide/tests/runlocaltest +1 -1
  115. rapidtide/tests/showkernels +1 -1
  116. rapidtide/tests/test_aliasedcorrelate.py +1 -1
  117. rapidtide/tests/test_aligntcs.py +1 -1
  118. rapidtide/tests/test_calcicc.py +1 -1
  119. rapidtide/tests/test_congrid.py +1 -1
  120. rapidtide/tests/test_correlate.py +1 -1
  121. rapidtide/tests/test_corrpass.py +1 -1
  122. rapidtide/tests/test_delayestimation.py +1 -1
  123. rapidtide/tests/test_doresample.py +1 -1
  124. rapidtide/tests/test_fastresampler.py +1 -1
  125. rapidtide/tests/test_findmaxlag.py +1 -1
  126. rapidtide/tests/test_fullrunhappy_v1.py +2 -2
  127. rapidtide/tests/test_fullrunhappy_v2.py +2 -2
  128. rapidtide/tests/test_fullrunhappy_v3.py +2 -2
  129. rapidtide/tests/test_fullrunhappy_v4.py +2 -2
  130. rapidtide/tests/test_fullrunhappy_v5.py +1 -3
  131. rapidtide/tests/test_fullrunrapidtide_v1.py +1 -1
  132. rapidtide/tests/test_fullrunrapidtide_v2.py +3 -1
  133. rapidtide/tests/test_fullrunrapidtide_v3.py +1 -1
  134. rapidtide/tests/test_fullrunrapidtide_v4.py +1 -1
  135. rapidtide/tests/test_fullrunrapidtide_v5.py +1 -1
  136. rapidtide/tests/test_fullrunrapidtide_v6.py +1 -1
  137. rapidtide/tests/test_io.py +1 -1
  138. rapidtide/tests/test_linfitfiltpass.py +1 -1
  139. rapidtide/tests/test_mi.py +1 -1
  140. rapidtide/tests/test_miscmath.py +1 -1
  141. rapidtide/tests/test_motionregress.py +1 -1
  142. rapidtide/tests/test_nullcorr.py +1 -1
  143. rapidtide/tests/test_padvec.py +1 -1
  144. rapidtide/tests/test_phaseanalysis.py +1 -1
  145. rapidtide/tests/test_rapidtideparser.py +1 -1
  146. rapidtide/tests/test_refinedelay.py +1 -2
  147. rapidtide/tests/test_runmisc.py +1 -1
  148. rapidtide/tests/test_sharedmem.py +1 -1
  149. rapidtide/tests/test_simulate.py +1 -1
  150. rapidtide/tests/test_stcorrelate.py +1 -1
  151. rapidtide/tests/test_timeshift.py +1 -1
  152. rapidtide/tests/test_valtoindex.py +1 -1
  153. rapidtide/tests/test_zRapidtideDataset.py +1 -1
  154. rapidtide/tests/utils.py +1 -1
  155. rapidtide/transformerdlfilter.py +2 -4
  156. rapidtide/util.py +37 -14
  157. rapidtide/voxelData.py +278 -0
  158. rapidtide/wiener.py +1 -1
  159. rapidtide/wiener2.py +1 -1
  160. rapidtide/workflows/adjustoffset.py +1 -1
  161. rapidtide/workflows/aligntcs.py +1 -1
  162. rapidtide/workflows/applydlfilter.py +20 -65
  163. rapidtide/workflows/atlasaverage.py +1 -1
  164. rapidtide/workflows/atlastool.py +1 -1
  165. rapidtide/workflows/calctexticc.py +1 -1
  166. rapidtide/workflows/ccorrica.py +1 -1
  167. rapidtide/workflows/cleanregressor.py +243 -0
  168. rapidtide/workflows/delayestimation.py +488 -0
  169. rapidtide/workflows/delayvar.py +1 -1
  170. rapidtide/workflows/diffrois.py +1 -1
  171. rapidtide/workflows/endtidalproc.py +1 -1
  172. rapidtide/workflows/fdica.py +1 -1
  173. rapidtide/workflows/filtnifti.py +1 -1
  174. rapidtide/workflows/filttc.py +1 -1
  175. rapidtide/workflows/fixtr.py +1 -1
  176. rapidtide/workflows/gmscalc.py +1 -1
  177. rapidtide/workflows/happy.py +49 -3
  178. rapidtide/workflows/happy2std.py +1 -1
  179. rapidtide/workflows/happy_parser.py +30 -6
  180. rapidtide/workflows/histnifti.py +1 -1
  181. rapidtide/workflows/histtc.py +1 -1
  182. rapidtide/workflows/linfitfilt.py +1 -1
  183. rapidtide/workflows/localflow.py +1 -1
  184. rapidtide/workflows/mergequality.py +1 -1
  185. rapidtide/workflows/niftidecomp.py +1 -1
  186. rapidtide/workflows/niftistats.py +1 -1
  187. rapidtide/workflows/pairproc.py +1 -1
  188. rapidtide/workflows/pairwisemergenifti.py +1 -1
  189. rapidtide/workflows/parser_funcs.py +1 -1
  190. rapidtide/workflows/physiofreq.py +1 -1
  191. rapidtide/workflows/pixelcomp.py +1 -1
  192. rapidtide/workflows/plethquality.py +1 -1
  193. rapidtide/workflows/polyfitim.py +1 -1
  194. rapidtide/workflows/proj2flow.py +1 -1
  195. rapidtide/workflows/rankimage.py +1 -1
  196. rapidtide/workflows/rapidtide.py +163 -679
  197. rapidtide/workflows/rapidtide2std.py +1 -1
  198. rapidtide/workflows/rapidtide_parser.py +1 -98
  199. rapidtide/workflows/regressfrommaps.py +4 -48
  200. rapidtide/workflows/resamplenifti.py +1 -1
  201. rapidtide/workflows/resampletc.py +1 -1
  202. rapidtide/workflows/retrolagtcs.py +1 -1
  203. rapidtide/workflows/retroregress.py +1 -1
  204. rapidtide/workflows/roisummarize.py +1 -1
  205. rapidtide/workflows/runqualitycheck.py +1 -1
  206. rapidtide/workflows/showarbcorr.py +1 -1
  207. rapidtide/workflows/showhist.py +1 -1
  208. rapidtide/workflows/showstxcorr.py +1 -1
  209. rapidtide/workflows/showtc.py +1 -1
  210. rapidtide/workflows/showxcorrx.py +1 -1
  211. rapidtide/workflows/showxy.py +1 -1
  212. rapidtide/workflows/simdata.py +1 -1
  213. rapidtide/workflows/spatialfit.py +1 -1
  214. rapidtide/workflows/spatialmi.py +1 -1
  215. rapidtide/workflows/spectrogram.py +1 -1
  216. rapidtide/workflows/synthASL.py +1 -1
  217. rapidtide/workflows/tcfrom2col.py +1 -1
  218. rapidtide/workflows/tcfrom3col.py +1 -1
  219. rapidtide/workflows/tidepool.py +2 -1
  220. rapidtide/workflows/utils.py +1 -1
  221. rapidtide/workflows/variabilityizer.py +1 -1
  222. {rapidtide-3.0a14.dist-info → rapidtide-3.0a15.dist-info}/METADATA +1 -1
  223. rapidtide-3.0a15.dist-info/RECORD +353 -0
  224. {rapidtide-3.0a14.dist-info → rapidtide-3.0a15.dist-info}/WHEEL +1 -1
  225. rapidtide/data/models/model_revised/model.h5 +0 -0
  226. rapidtide/data/models/model_serdar/model.h5 +0 -0
  227. rapidtide/data/models/model_serdar2/model.h5 +0 -0
  228. rapidtide-3.0a14.dist-info/RECORD +0 -345
  229. {rapidtide-3.0a14.dist-info → rapidtide-3.0a15.dist-info}/entry_points.txt +0 -0
  230. {rapidtide-3.0a14.dist-info → rapidtide-3.0a15.dist-info}/licenses/LICENSE +0 -0
  231. {rapidtide-3.0a14.dist-info → rapidtide-3.0a15.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,488 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # Copyright 2016-2025 Blaise Frederick
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ #
19
+ import argparse
20
+ import sys
21
+
22
+ import numpy as np
23
+ from scipy.stats import skew
24
+
25
+ import rapidtide.io as tide_io
26
+ import rapidtide.workflows.parser_funcs as pf
27
+
28
+ try:
29
+ import mkl
30
+
31
+ mklexists = True
32
+ except ImportError:
33
+ mklexists = False
34
+
35
+
36
+ def disablemkl(numprocs, debug=False):
37
+ if mklexists:
38
+ if numprocs > 1:
39
+ if debug:
40
+ print("disablemkl: setting threads to 1")
41
+ mkl.set_num_threads(1)
42
+
43
+
44
+ def enablemkl(numthreads, debug=False):
45
+ if mklexists:
46
+ if debug:
47
+ print(f"enablemkl: setting threads to {numthreads}")
48
+ mkl.set_num_threads(numthreads)
49
+
50
+
51
+ def estimateDelay(voxeldata, datatimeaxis, osdatatimeaxis, theMutualInformationator, thepass):
52
+ # Step 1 - Correlation step
53
+ if optiondict["similaritymetric"] == "mutualinfo":
54
+ similaritytype = "Mutual information"
55
+ elif optiondict["similaritymetric"] == "correlation":
56
+ similaritytype = "Correlation"
57
+ else:
58
+ similaritytype = "MI enhanced correlation"
59
+ LGR.info(f"\n\n{similaritytype} calculation, pass {thepass}")
60
+ TimingLGR.info(f"{similaritytype} calculation start, pass {thepass}")
61
+
62
+ disablemkl(optiondict["nprocs_calcsimilarity"], debug=threaddebug)
63
+ if optiondict["similaritymetric"] == "mutualinfo":
64
+ theMutualInformationator.setlimits(lagmininpts, lagmaxinpts)
65
+ (
66
+ voxelsprocessed_cp,
67
+ theglobalmaxlist,
68
+ trimmedcorrscale,
69
+ ) = tide_calcsimfunc.correlationpass(
70
+ voxeldata,
71
+ cleaned_referencetc,
72
+ theMutualInformationator,
73
+ datatimeaxis,
74
+ osdatatimeaxis,
75
+ lagmininpts,
76
+ lagmaxinpts,
77
+ corrout,
78
+ meanval,
79
+ nprocs=optiondict["nprocs_calcsimilarity"],
80
+ alwaysmultiproc=optiondict["alwaysmultiproc"],
81
+ oversampfactor=optiondict["oversampfactor"],
82
+ interptype=optiondict["interptype"],
83
+ showprogressbar=optiondict["showprogressbar"],
84
+ chunksize=optiondict["mp_chunksize"],
85
+ rt_floatset=rt_floatset,
86
+ rt_floattype=rt_floattype,
87
+ )
88
+ else:
89
+ (
90
+ voxelsprocessed_cp,
91
+ theglobalmaxlist,
92
+ trimmedcorrscale,
93
+ ) = tide_calcsimfunc.correlationpass(
94
+ voxeldata,
95
+ cleaned_referencetc,
96
+ theCorrelator,
97
+ datatimeaxis,
98
+ osdatatimeaxis,
99
+ lagmininpts,
100
+ lagmaxinpts,
101
+ corrout,
102
+ meanval,
103
+ nprocs=optiondict["nprocs_calcsimilarity"],
104
+ alwaysmultiproc=optiondict["alwaysmultiproc"],
105
+ oversampfactor=optiondict["oversampfactor"],
106
+ interptype=optiondict["interptype"],
107
+ showprogressbar=optiondict["showprogressbar"],
108
+ chunksize=optiondict["mp_chunksize"],
109
+ rt_floatset=rt_floatset,
110
+ rt_floattype=rt_floattype,
111
+ )
112
+ enablemkl(optiondict["mklthreads"], debug=threaddebug)
113
+
114
+ for i in range(len(theglobalmaxlist)):
115
+ theglobalmaxlist[i] = corrscale[theglobalmaxlist[i]] - optiondict["simcalcoffset"]
116
+ namesuffix = "_desc-globallag_hist"
117
+ tide_stats.makeandsavehistogram(
118
+ np.asarray(theglobalmaxlist),
119
+ len(corrscale),
120
+ 0,
121
+ outputname + namesuffix,
122
+ displaytitle="Histogram of lag times from global lag calculation",
123
+ therange=(corrscale[0], corrscale[-1]),
124
+ refine=False,
125
+ dictvarname="globallaghist_pass" + str(thepass),
126
+ append=(optiondict["echocancel"] or (thepass > 1)),
127
+ thedict=optiondict,
128
+ )
129
+
130
+ if optiondict["checkpoint"]:
131
+ outcorrarray[:, :] = 0.0
132
+ outcorrarray[validvoxels, :] = corrout[:, :]
133
+ if optiondict["textio"]:
134
+ tide_io.writenpvecs(
135
+ outcorrarray.reshape(nativecorrshape),
136
+ f"{outputname}_corrout_prefit_pass" + str(thepass) + ".txt",
137
+ )
138
+ else:
139
+ savename = f"{outputname}_desc-corroutprefit_pass-" + str(thepass)
140
+ tide_io.savetonifti(outcorrarray.reshape(nativecorrshape), theheader, savename)
141
+
142
+ TimingLGR.info(
143
+ f"{similaritytype} calculation end, pass {thepass}",
144
+ {
145
+ "message2": voxelsprocessed_cp,
146
+ "message3": "voxels",
147
+ },
148
+ )
149
+
150
+ # Step 1b. Do a peak prefit
151
+ if optiondict["similaritymetric"] == "hybrid":
152
+ LGR.info(f"\n\nPeak prefit calculation, pass {thepass}")
153
+ TimingLGR.info(f"Peak prefit calculation start, pass {thepass}")
154
+
155
+ disablemkl(optiondict["nprocs_peakeval"], debug=threaddebug)
156
+ voxelsprocessed_pe, thepeakdict = tide_peakeval.peakevalpass(
157
+ voxeldata,
158
+ cleaned_referencetc,
159
+ datatimeaxis,
160
+ osdatatimeaxis,
161
+ theMutualInformationator,
162
+ trimmedcorrscale,
163
+ corrout,
164
+ nprocs=optiondict["nprocs_peakeval"],
165
+ alwaysmultiproc=optiondict["alwaysmultiproc"],
166
+ bipolar=optiondict["bipolar"],
167
+ oversampfactor=optiondict["oversampfactor"],
168
+ interptype=optiondict["interptype"],
169
+ showprogressbar=optiondict["showprogressbar"],
170
+ chunksize=optiondict["mp_chunksize"],
171
+ rt_floatset=rt_floatset,
172
+ rt_floattype=rt_floattype,
173
+ )
174
+ enablemkl(optiondict["mklthreads"], debug=threaddebug)
175
+
176
+ TimingLGR.info(
177
+ f"Peak prefit end, pass {thepass}",
178
+ {
179
+ "message2": voxelsprocessed_pe,
180
+ "message3": "voxels",
181
+ },
182
+ )
183
+ mipeaks = lagtimes * 0.0
184
+ for i in range(numvalidspatiallocs):
185
+ if len(thepeakdict[str(i)]) > 0:
186
+ mipeaks[i] = thepeakdict[str(i)][0][0]
187
+ else:
188
+ thepeakdict = None
189
+
190
+ # Step 2 - similarity function fitting and time lag estimation
191
+ # write out the current version of the run options
192
+ optiondict["currentstage"] = f"presimfuncfit_pass{thepass}"
193
+ tide_io.writedicttojson(optiondict, f"{outputname}_desc-runoptions_info.json")
194
+ LGR.info(f"\n\nTime lag estimation pass {thepass}")
195
+ TimingLGR.info(f"Time lag estimation start, pass {thepass}")
196
+
197
+ theFitter.setfunctype(optiondict["similaritymetric"])
198
+ theFitter.setcorrtimeaxis(trimmedcorrscale)
199
+
200
+ # use initial lags if this is a hybrid fit
201
+ if optiondict["similaritymetric"] == "hybrid" and thepeakdict is not None:
202
+ initlags = mipeaks
203
+ else:
204
+ initlags = None
205
+
206
+ disablemkl(optiondict["nprocs_fitcorr"], debug=threaddebug)
207
+ voxelsprocessed_fc = tide_simfuncfit.fitcorr(
208
+ trimmedcorrscale,
209
+ theFitter,
210
+ corrout,
211
+ fitmask,
212
+ failreason,
213
+ lagtimes,
214
+ lagstrengths,
215
+ lagsigma,
216
+ gaussout,
217
+ windowout,
218
+ R2,
219
+ despeckling=False,
220
+ peakdict=thepeakdict,
221
+ nprocs=optiondict["nprocs_fitcorr"],
222
+ alwaysmultiproc=optiondict["alwaysmultiproc"],
223
+ fixdelay=optiondict["fixdelay"],
224
+ initialdelayvalue=theinitialdelay,
225
+ showprogressbar=optiondict["showprogressbar"],
226
+ chunksize=optiondict["mp_chunksize"],
227
+ despeckle_thresh=optiondict["despeckle_thresh"],
228
+ initiallags=initlags,
229
+ rt_floatset=rt_floatset,
230
+ rt_floattype=rt_floattype,
231
+ )
232
+ enablemkl(optiondict["mklthreads"], debug=threaddebug)
233
+
234
+ TimingLGR.info(
235
+ f"Time lag estimation end, pass {thepass}",
236
+ {
237
+ "message2": voxelsprocessed_fc,
238
+ "message3": "voxels",
239
+ },
240
+ )
241
+
242
+ # Step 2b - Correlation time despeckle
243
+ if optiondict["despeckle_passes"] > 0:
244
+ LGR.info(f"\n\n{similaritytype} despeckling pass {thepass}")
245
+ LGR.info(f"\tUsing despeckle_thresh = {optiondict['despeckle_thresh']:.3f}")
246
+ TimingLGR.info(f"{similaritytype} despeckle start, pass {thepass}")
247
+
248
+ # find lags that are very different from their neighbors, and refit starting at the median lag for the point
249
+ voxelsprocessed_fc_ds = 0
250
+ despecklingdone = False
251
+ lastnumdespeckled = 1000000
252
+ for despecklepass in range(optiondict["despeckle_passes"]):
253
+ LGR.info(f"\n\n{similaritytype} despeckling subpass {despecklepass + 1}")
254
+ outmaparray *= 0.0
255
+ outmaparray[validvoxels] = eval("lagtimes")[:]
256
+
257
+ # find voxels to despeckle
258
+ medianlags = ndimage.median_filter(outmaparray.reshape(nativespaceshape), 3).reshape(
259
+ numspatiallocs
260
+ )
261
+ # voxels that we're happy with have initlags set to -1000000.0
262
+ initlags = np.where(
263
+ np.abs(outmaparray - medianlags) > optiondict["despeckle_thresh"],
264
+ medianlags,
265
+ -1000000.0,
266
+ )[validvoxels]
267
+
268
+ if len(initlags) > 0:
269
+ numdespeckled = len(np.where(initlags != -1000000.0)[0])
270
+ if lastnumdespeckled > numdespeckled > 0:
271
+ lastnumdespeckled = numdespeckled
272
+ disablemkl(optiondict["nprocs_fitcorr"], debug=threaddebug)
273
+ voxelsprocessed_thispass = tide_simfuncfit.fitcorr(
274
+ trimmedcorrscale,
275
+ theFitter,
276
+ corrout,
277
+ fitmask,
278
+ failreason,
279
+ lagtimes,
280
+ lagstrengths,
281
+ lagsigma,
282
+ gaussout,
283
+ windowout,
284
+ R2,
285
+ despeckling=True,
286
+ peakdict=thepeakdict,
287
+ nprocs=optiondict["nprocs_fitcorr"],
288
+ alwaysmultiproc=optiondict["alwaysmultiproc"],
289
+ fixdelay=optiondict["fixdelay"],
290
+ initialdelayvalue=theinitialdelay,
291
+ showprogressbar=optiondict["showprogressbar"],
292
+ chunksize=optiondict["mp_chunksize"],
293
+ despeckle_thresh=optiondict["despeckle_thresh"],
294
+ initiallags=initlags,
295
+ rt_floatset=rt_floatset,
296
+ rt_floattype=rt_floattype,
297
+ )
298
+ enablemkl(optiondict["mklthreads"], debug=threaddebug)
299
+
300
+ voxelsprocessed_fc_ds += voxelsprocessed_thispass
301
+ optiondict[
302
+ "despecklemasksize_pass" + str(thepass) + "_d" + str(despecklepass + 1)
303
+ ] = voxelsprocessed_thispass
304
+ optiondict[
305
+ "despecklemaskpct_pass" + str(thepass) + "_d" + str(despecklepass + 1)
306
+ ] = (100.0 * voxelsprocessed_thispass / optiondict["corrmasksize"])
307
+ else:
308
+ despecklingdone = True
309
+ else:
310
+ despecklingdone = True
311
+ if despecklingdone:
312
+ LGR.info("Nothing left to do! Terminating despeckling")
313
+ break
314
+
315
+ internaldespeckleincludemask = np.where(
316
+ np.abs(outmaparray - medianlags) > optiondict["despeckle_thresh"],
317
+ medianlags,
318
+ 0.0,
319
+ )
320
+ if optiondict["savedespecklemasks"] and (optiondict["despeckle_passes"] > 0):
321
+ despecklesavemask = np.where(internaldespeckleincludemask[validvoxels] == 0.0, 0, 1)
322
+ if thepass == optiondict["passes"]:
323
+ if not optiondict["textio"]:
324
+ if fileiscifti:
325
+ timeindex = theheader["dim"][0] - 1
326
+ spaceindex = theheader["dim"][0]
327
+ theheader["dim"][timeindex] = 1
328
+ theheader["dim"][spaceindex] = numspatiallocs
329
+ else:
330
+ theheader["dim"][0] = 3
331
+ theheader["dim"][4] = 1
332
+ theheader["pixdim"][4] = 1.0
333
+ masklist = [
334
+ (
335
+ despecklesavemask,
336
+ "despeckle",
337
+ "mask",
338
+ None,
339
+ "Voxels that underwent despeckling in the final pass",
340
+ )
341
+ ]
342
+ tide_io.savemaplist(
343
+ outputname,
344
+ masklist,
345
+ validvoxels,
346
+ nativespaceshape,
347
+ theheader,
348
+ bidsbasedict,
349
+ textio=optiondict["textio"],
350
+ fileiscifti=fileiscifti,
351
+ rt_floattype=rt_floattype,
352
+ cifti_hdr=cifti_hdr,
353
+ )
354
+ LGR.info(
355
+ f"\n\n{voxelsprocessed_fc_ds} voxels despeckled in "
356
+ f"{optiondict['despeckle_passes']} passes"
357
+ )
358
+ TimingLGR.info(
359
+ f"{similaritytype} despeckle end, pass {thepass}",
360
+ {
361
+ "message2": voxelsprocessed_fc_ds,
362
+ "message3": "voxels",
363
+ },
364
+ )
365
+
366
+ # Step 2c - patch shifting
367
+ if optiondict["patchshift"]:
368
+ outmaparray *= 0.0
369
+ outmaparray[validvoxels] = eval("lagtimes")[:]
370
+ # new method
371
+ masklist = [
372
+ (
373
+ outmaparray[validvoxels],
374
+ f"lagtimes_prepatch_pass{thepass}",
375
+ "map",
376
+ None,
377
+ f"Input lagtimes map prior to patch map generation pass {thepass}",
378
+ ),
379
+ ]
380
+ tide_io.savemaplist(
381
+ outputname,
382
+ masklist,
383
+ validvoxels,
384
+ nativespaceshape,
385
+ theheader,
386
+ bidsbasedict,
387
+ textio=optiondict["textio"],
388
+ fileiscifti=fileiscifti,
389
+ rt_floattype=rt_floattype,
390
+ cifti_hdr=cifti_hdr,
391
+ )
392
+
393
+ # create list of anomalous 3D regions that don't match surroundings
394
+ if nim_affine is not None:
395
+ # make an atlas of anomalous patches - each patch shares the same integer value
396
+ step1 = tide_patch.calc_DoG(
397
+ outmaparray.reshape(nativespaceshape).copy(),
398
+ nim_affine,
399
+ thesizes,
400
+ fwhm=optiondict["patchfwhm"],
401
+ ratioopt=False,
402
+ debug=True,
403
+ )
404
+ masklist = [
405
+ (
406
+ step1.reshape(internalspaceshape)[validvoxels],
407
+ f"DoG_pass{thepass}",
408
+ "map",
409
+ None,
410
+ f"DoG map for pass {thepass}",
411
+ ),
412
+ ]
413
+ tide_io.savemaplist(
414
+ outputname,
415
+ masklist,
416
+ validvoxels,
417
+ nativespaceshape,
418
+ theheader,
419
+ bidsbasedict,
420
+ textio=optiondict["textio"],
421
+ fileiscifti=fileiscifti,
422
+ rt_floattype=rt_floattype,
423
+ cifti_hdr=cifti_hdr,
424
+ )
425
+ step2 = tide_patch.invertedflood3D(
426
+ step1,
427
+ 1,
428
+ )
429
+ masklist = [
430
+ (
431
+ step2.reshape(internalspaceshape)[validvoxels],
432
+ f"invertflood_pass{thepass}",
433
+ "map",
434
+ None,
435
+ f"Inverted flood map for pass {thepass}",
436
+ ),
437
+ ]
438
+ tide_io.savemaplist(
439
+ outputname,
440
+ masklist,
441
+ validvoxels,
442
+ nativespaceshape,
443
+ theheader,
444
+ bidsbasedict,
445
+ textio=optiondict["textio"],
446
+ fileiscifti=fileiscifti,
447
+ rt_floattype=rt_floattype,
448
+ cifti_hdr=cifti_hdr,
449
+ )
450
+
451
+ patchmap = tide_patch.separateclusters(
452
+ step2,
453
+ sizethresh=optiondict["patchminsize"],
454
+ debug=True,
455
+ )
456
+ # patchmap = tide_patch.getclusters(
457
+ # outmaparray.reshape(nativespaceshape),
458
+ # nim_affine,
459
+ # thesizes,
460
+ # fwhm=optiondict["patchfwhm"],
461
+ # ratioopt=True,
462
+ # sizethresh=optiondict["patchminsize"],
463
+ # debug=True,
464
+ # )
465
+ masklist = [
466
+ (
467
+ patchmap[validvoxels],
468
+ f"patch_pass{thepass}",
469
+ "map",
470
+ None,
471
+ f"Patch map for despeckling pass {thepass}",
472
+ ),
473
+ ]
474
+ tide_io.savemaplist(
475
+ outputname,
476
+ masklist,
477
+ validvoxels,
478
+ nativespaceshape,
479
+ theheader,
480
+ bidsbasedict,
481
+ textio=optiondict["textio"],
482
+ fileiscifti=fileiscifti,
483
+ rt_floattype=rt_floattype,
484
+ cifti_hdr=cifti_hdr,
485
+ )
486
+
487
+ # now shift the patches to align with the majority of the image
488
+ tide_patch.interppatch(lagtimes, patchmap[validvoxels])
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2016-2024 Blaise Frederick
4
+ # Copyright 2016-2025 Blaise Frederick
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.