rapidtide 3.0.8__py3-none-any.whl → 3.0.10__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 (38) hide show
  1. rapidtide/OrthoImageItem.py +15 -6
  2. rapidtide/RapidtideDataset.py +22 -7
  3. rapidtide/_version.py +3 -3
  4. rapidtide/data/examples/src/testfmri +26 -20
  5. rapidtide/data/examples/src/testhappy +0 -1
  6. rapidtide/filter.py +60 -111
  7. rapidtide/fit.py +501 -108
  8. rapidtide/io.py +19 -8
  9. rapidtide/linfitfiltpass.py +44 -25
  10. rapidtide/refinedelay.py +2 -2
  11. rapidtide/refineregressor.py +1 -1
  12. rapidtide/resample.py +8 -8
  13. rapidtide/simFuncClasses.py +13 -7
  14. rapidtide/tests/.coveragerc +17 -11
  15. rapidtide/tests/test_delayestimation.py +1 -1
  16. rapidtide/tests/test_findmaxlag.py +31 -16
  17. rapidtide/tests/test_fullrunrapidtide_v8.py +66 -0
  18. rapidtide/tests/test_padvec.py +19 -1
  19. rapidtide/tests/test_simroundtrip.py +124 -0
  20. rapidtide/tidepoolTemplate.py +37 -37
  21. rapidtide/tidepoolTemplate_alt.py +40 -40
  22. rapidtide/tidepoolTemplate_big.py +56 -56
  23. rapidtide/workflows/calcSimFuncMap.py +271 -0
  24. rapidtide/workflows/delayvar.py +1 -1
  25. rapidtide/workflows/fitSimFuncMap.py +427 -0
  26. rapidtide/workflows/happy.py +2 -2
  27. rapidtide/workflows/parser_funcs.py +1 -1
  28. rapidtide/workflows/rapidtide.py +197 -48
  29. rapidtide/workflows/rapidtide_parser.py +23 -2
  30. rapidtide/workflows/regressfrommaps.py +7 -7
  31. rapidtide/workflows/tidepool.py +51 -15
  32. {rapidtide-3.0.8.dist-info → rapidtide-3.0.10.dist-info}/METADATA +3 -3
  33. {rapidtide-3.0.8.dist-info → rapidtide-3.0.10.dist-info}/RECORD +37 -34
  34. rapidtide/workflows/estimateDelayMap.py +0 -536
  35. {rapidtide-3.0.8.dist-info → rapidtide-3.0.10.dist-info}/WHEEL +0 -0
  36. {rapidtide-3.0.8.dist-info → rapidtide-3.0.10.dist-info}/entry_points.txt +0 -0
  37. {rapidtide-3.0.8.dist-info → rapidtide-3.0.10.dist-info}/licenses/LICENSE +0 -0
  38. {rapidtide-3.0.8.dist-info → rapidtide-3.0.10.dist-info}/top_level.txt +0 -0
@@ -1,536 +0,0 @@
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 numpy as np
20
- from scipy import ndimage
21
-
22
- import rapidtide.calcsimfunc as tide_calcsimfunc
23
- import rapidtide.io as tide_io
24
- import rapidtide.patchmatch as tide_patch
25
- import rapidtide.peakeval as tide_peakeval
26
- import rapidtide.simfuncfit as tide_simfuncfit
27
- import rapidtide.stats as tide_stats
28
- import rapidtide.util as tide_util
29
-
30
- try:
31
- import mkl
32
-
33
- mklexists = True
34
- except ImportError:
35
- mklexists = False
36
-
37
-
38
- def disablemkl(numprocs, debug=False):
39
- if mklexists:
40
- if numprocs > 1:
41
- if debug:
42
- print("disablemkl: setting threads to 1")
43
- mkl.set_num_threads(1)
44
-
45
-
46
- def enablemkl(numthreads, debug=False):
47
- if mklexists:
48
- if debug:
49
- print(f"enablemkl: setting threads to {numthreads}")
50
- mkl.set_num_threads(numthreads)
51
-
52
-
53
- def estimateDelay(
54
- fmri_data_valid,
55
- validsimcalcstart,
56
- validsimcalcend,
57
- osvalidsimcalcstart,
58
- osvalidsimcalcend,
59
- initial_fmri_x,
60
- os_fmri_x,
61
- theCorrelator,
62
- theMutualInformationator,
63
- cleaned_referencetc,
64
- corrout,
65
- meanval,
66
- corrscale,
67
- outputname,
68
- outcorrarray,
69
- validvoxels,
70
- nativecorrshape,
71
- nativespaceshape,
72
- bidsbasedict,
73
- numspatiallocs,
74
- gaussout,
75
- theinitialdelay,
76
- windowout,
77
- R2,
78
- thesizes,
79
- internalspaceshape,
80
- numvalidspatiallocs,
81
- theinputdata,
82
- theheader,
83
- theFitter,
84
- fitmask,
85
- lagtimes,
86
- lagstrengths,
87
- lagsigma,
88
- failreason,
89
- outmaparray,
90
- lagmininpts,
91
- lagmaxinpts,
92
- thepass,
93
- optiondict,
94
- LGR,
95
- TimingLGR,
96
- rt_floatset=np.float64,
97
- rt_floattype="float64",
98
- ):
99
- # Step 1 - Correlation step
100
- if optiondict["similaritymetric"] == "mutualinfo":
101
- similaritytype = "Mutual information"
102
- elif optiondict["similaritymetric"] == "correlation":
103
- similaritytype = "Correlation"
104
- else:
105
- similaritytype = "MI enhanced correlation"
106
- LGR.info(f"\n\n{similaritytype} calculation, pass {thepass}")
107
- TimingLGR.info(f"{similaritytype} calculation start, pass {thepass}")
108
-
109
- tide_util.disablemkl(optiondict["nprocs_calcsimilarity"], debug=optiondict["threaddebug"])
110
- if optiondict["similaritymetric"] == "mutualinfo":
111
- theMutualInformationator.setlimits(lagmininpts, lagmaxinpts)
112
- (
113
- voxelsprocessed_cp,
114
- theglobalmaxlist,
115
- trimmedcorrscale,
116
- ) = tide_calcsimfunc.correlationpass(
117
- fmri_data_valid[:, validsimcalcstart : validsimcalcend + 1],
118
- cleaned_referencetc,
119
- theMutualInformationator,
120
- initial_fmri_x[validsimcalcstart : validsimcalcend + 1],
121
- os_fmri_x[osvalidsimcalcstart : osvalidsimcalcend + 1],
122
- lagmininpts,
123
- lagmaxinpts,
124
- corrout,
125
- meanval,
126
- nprocs=optiondict["nprocs_calcsimilarity"],
127
- alwaysmultiproc=optiondict["alwaysmultiproc"],
128
- oversampfactor=optiondict["oversampfactor"],
129
- interptype=optiondict["interptype"],
130
- showprogressbar=optiondict["showprogressbar"],
131
- chunksize=optiondict["mp_chunksize"],
132
- rt_floatset=rt_floatset,
133
- rt_floattype=rt_floattype,
134
- debug=optiondict["focaldebug"],
135
- )
136
- else:
137
- (
138
- voxelsprocessed_cp,
139
- theglobalmaxlist,
140
- trimmedcorrscale,
141
- ) = tide_calcsimfunc.correlationpass(
142
- fmri_data_valid[:, validsimcalcstart : validsimcalcend + 1],
143
- cleaned_referencetc,
144
- theCorrelator,
145
- initial_fmri_x[validsimcalcstart : validsimcalcend + 1],
146
- os_fmri_x[osvalidsimcalcstart : osvalidsimcalcend + 1],
147
- lagmininpts,
148
- lagmaxinpts,
149
- corrout,
150
- meanval,
151
- nprocs=optiondict["nprocs_calcsimilarity"],
152
- alwaysmultiproc=optiondict["alwaysmultiproc"],
153
- oversampfactor=optiondict["oversampfactor"],
154
- interptype=optiondict["interptype"],
155
- showprogressbar=optiondict["showprogressbar"],
156
- chunksize=optiondict["mp_chunksize"],
157
- rt_floatset=rt_floatset,
158
- rt_floattype=rt_floattype,
159
- debug=optiondict["focaldebug"],
160
- )
161
- tide_util.enablemkl(optiondict["mklthreads"], debug=optiondict["threaddebug"])
162
-
163
- for i in range(len(theglobalmaxlist)):
164
- theglobalmaxlist[i] = corrscale[theglobalmaxlist[i]] - optiondict["simcalcoffset"]
165
- namesuffix = "_desc-globallag_hist"
166
- tide_stats.makeandsavehistogram(
167
- np.asarray(theglobalmaxlist),
168
- len(corrscale),
169
- 0,
170
- outputname + namesuffix,
171
- displaytitle="Histogram of lag times from global lag calculation",
172
- therange=(corrscale[0], corrscale[-1]),
173
- refine=False,
174
- dictvarname="globallaghist_pass" + str(thepass),
175
- append=(optiondict["echocancel"] or (thepass > 1)),
176
- thedict=optiondict,
177
- )
178
-
179
- if optiondict["checkpoint"]:
180
- outcorrarray[:, :] = 0.0
181
- outcorrarray[validvoxels, :] = corrout[:, :]
182
- if theinputdata.filetype == "text":
183
- tide_io.writenpvecs(
184
- outcorrarray.reshape(nativecorrshape),
185
- f"{outputname}_corrout_prefit_pass" + str(thepass) + ".txt",
186
- )
187
- else:
188
- savename = f"{outputname}_desc-corroutprefit_pass-" + str(thepass)
189
- tide_io.savetonifti(outcorrarray.reshape(nativecorrshape), theheader, savename)
190
-
191
- TimingLGR.info(
192
- f"{similaritytype} calculation end, pass {thepass}",
193
- {
194
- "message2": voxelsprocessed_cp,
195
- "message3": "voxels",
196
- },
197
- )
198
-
199
- # Step 1b. Do a peak prefit
200
- if optiondict["similaritymetric"] == "hybrid":
201
- LGR.info(f"\n\nPeak prefit calculation, pass {thepass}")
202
- TimingLGR.info(f"Peak prefit calculation start, pass {thepass}")
203
-
204
- tide_util.disablemkl(optiondict["nprocs_peakeval"], debug=optiondict["threaddebug"])
205
- voxelsprocessed_pe, thepeakdict = tide_peakeval.peakevalpass(
206
- fmri_data_valid[:, validsimcalcstart : validsimcalcend + 1],
207
- cleaned_referencetc,
208
- initial_fmri_x[validsimcalcstart : validsimcalcend + 1],
209
- os_fmri_x[osvalidsimcalcstart : osvalidsimcalcend + 1],
210
- theMutualInformationator,
211
- trimmedcorrscale,
212
- corrout,
213
- nprocs=optiondict["nprocs_peakeval"],
214
- alwaysmultiproc=optiondict["alwaysmultiproc"],
215
- bipolar=optiondict["bipolar"],
216
- oversampfactor=optiondict["oversampfactor"],
217
- interptype=optiondict["interptype"],
218
- showprogressbar=optiondict["showprogressbar"],
219
- chunksize=optiondict["mp_chunksize"],
220
- rt_floatset=rt_floatset,
221
- rt_floattype=rt_floattype,
222
- )
223
- tide_util.enablemkl(optiondict["mklthreads"], debug=optiondict["threaddebug"])
224
-
225
- TimingLGR.info(
226
- f"Peak prefit end, pass {thepass}",
227
- {
228
- "message2": voxelsprocessed_pe,
229
- "message3": "voxels",
230
- },
231
- )
232
- mipeaks = lagtimes * 0.0
233
- for i in range(numvalidspatiallocs):
234
- if len(thepeakdict[str(i)]) > 0:
235
- mipeaks[i] = thepeakdict[str(i)][0][0]
236
- else:
237
- thepeakdict = None
238
-
239
- # Step 2 - similarity function fitting and time lag estimation
240
- # write out the current version of the run options
241
- optiondict["currentstage"] = f"presimfuncfit_pass{thepass}"
242
- tide_io.writedicttojson(optiondict, f"{outputname}_desc-runoptions_info.json")
243
- LGR.info(f"\n\nTime lag estimation pass {thepass}")
244
- TimingLGR.info(f"Time lag estimation start, pass {thepass}")
245
-
246
- theFitter.setfunctype(optiondict["similaritymetric"])
247
- theFitter.setcorrtimeaxis(trimmedcorrscale)
248
-
249
- # use initial lags if this is a hybrid fit
250
- if optiondict["similaritymetric"] == "hybrid" and thepeakdict is not None:
251
- initlags = mipeaks
252
- else:
253
- initlags = None
254
-
255
- tide_util.disablemkl(optiondict["nprocs_fitcorr"], debug=optiondict["threaddebug"])
256
- voxelsprocessed_fc = tide_simfuncfit.fitcorr(
257
- trimmedcorrscale,
258
- theFitter,
259
- corrout,
260
- fitmask,
261
- failreason,
262
- lagtimes,
263
- lagstrengths,
264
- lagsigma,
265
- gaussout,
266
- windowout,
267
- R2,
268
- despeckling=False,
269
- peakdict=thepeakdict,
270
- nprocs=optiondict["nprocs_fitcorr"],
271
- alwaysmultiproc=optiondict["alwaysmultiproc"],
272
- fixdelay=optiondict["fixdelay"],
273
- initialdelayvalue=theinitialdelay,
274
- showprogressbar=optiondict["showprogressbar"],
275
- chunksize=optiondict["mp_chunksize"],
276
- despeckle_thresh=optiondict["despeckle_thresh"],
277
- initiallags=initlags,
278
- rt_floatset=rt_floatset,
279
- rt_floattype=rt_floattype,
280
- )
281
- tide_util.enablemkl(optiondict["mklthreads"], debug=optiondict["threaddebug"])
282
-
283
- TimingLGR.info(
284
- f"Time lag estimation end, pass {thepass}",
285
- {
286
- "message2": voxelsprocessed_fc,
287
- "message3": "voxels",
288
- },
289
- )
290
-
291
- # Step 2b - Correlation time despeckle
292
- if optiondict["despeckle_passes"] > 0:
293
- LGR.info(f"\n\n{similaritytype} despeckling pass {thepass}")
294
- LGR.info(f"\tUsing despeckle_thresh = {optiondict['despeckle_thresh']:.3f}")
295
- TimingLGR.info(f"{similaritytype} despeckle start, pass {thepass}")
296
-
297
- # find lags that are very different from their neighbors, and refit starting at the median lag for the point
298
- voxelsprocessed_fc_ds = 0
299
- despecklingdone = False
300
- lastnumdespeckled = 1000000
301
- for despecklepass in range(optiondict["despeckle_passes"]):
302
- LGR.info(f"\n\n{similaritytype} despeckling subpass {despecklepass + 1}")
303
- outmaparray *= 0.0
304
- outmaparray[validvoxels] = eval("lagtimes")[:]
305
-
306
- # find voxels to despeckle
307
- medianlags = ndimage.median_filter(outmaparray.reshape(nativespaceshape), 3).reshape(
308
- numspatiallocs
309
- )
310
- # voxels that we're happy with have initlags set to -1000000.0
311
- initlags = np.where(
312
- np.abs(outmaparray - medianlags) > optiondict["despeckle_thresh"],
313
- medianlags,
314
- -1000000.0,
315
- )[validvoxels]
316
-
317
- if len(initlags) > 0:
318
- numdespeckled = len(np.where(initlags != -1000000.0)[0])
319
- if lastnumdespeckled > numdespeckled > 0:
320
- lastnumdespeckled = numdespeckled
321
- tide_util.disablemkl(
322
- optiondict["nprocs_fitcorr"], debug=optiondict["threaddebug"]
323
- )
324
- voxelsprocessed_thispass = tide_simfuncfit.fitcorr(
325
- trimmedcorrscale,
326
- theFitter,
327
- corrout,
328
- fitmask,
329
- failreason,
330
- lagtimes,
331
- lagstrengths,
332
- lagsigma,
333
- gaussout,
334
- windowout,
335
- R2,
336
- despeckling=True,
337
- peakdict=thepeakdict,
338
- nprocs=optiondict["nprocs_fitcorr"],
339
- alwaysmultiproc=optiondict["alwaysmultiproc"],
340
- fixdelay=optiondict["fixdelay"],
341
- initialdelayvalue=theinitialdelay,
342
- showprogressbar=optiondict["showprogressbar"],
343
- chunksize=optiondict["mp_chunksize"],
344
- despeckle_thresh=optiondict["despeckle_thresh"],
345
- initiallags=initlags,
346
- rt_floatset=rt_floatset,
347
- rt_floattype=rt_floattype,
348
- )
349
- tide_util.enablemkl(optiondict["mklthreads"], debug=optiondict["threaddebug"])
350
-
351
- voxelsprocessed_fc_ds += voxelsprocessed_thispass
352
- optiondict[
353
- "despecklemasksize_pass" + str(thepass) + "_d" + str(despecklepass + 1)
354
- ] = voxelsprocessed_thispass
355
- optiondict[
356
- "despecklemaskpct_pass" + str(thepass) + "_d" + str(despecklepass + 1)
357
- ] = (100.0 * voxelsprocessed_thispass / optiondict["corrmasksize"])
358
- else:
359
- despecklingdone = True
360
- else:
361
- despecklingdone = True
362
- if despecklingdone:
363
- LGR.info("Nothing left to do! Terminating despeckling")
364
- break
365
-
366
- internaldespeckleincludemask = np.where(
367
- np.abs(outmaparray - medianlags) > optiondict["despeckle_thresh"],
368
- medianlags,
369
- 0.0,
370
- )
371
- if optiondict["savedespecklemasks"] and (optiondict["despeckle_passes"] > 0):
372
- despecklesavemask = np.where(internaldespeckleincludemask[validvoxels] == 0.0, 0, 1)
373
- if thepass == optiondict["passes"]:
374
- if theinputdata.filetype != "text":
375
- if theinputdata.filetype == "cifti":
376
- timeindex = theheader["dim"][0] - 1
377
- spaceindex = theheader["dim"][0]
378
- theheader["dim"][timeindex] = 1
379
- theheader["dim"][spaceindex] = numspatiallocs
380
- else:
381
- theheader["dim"][0] = 3
382
- theheader["dim"][4] = 1
383
- theheader["pixdim"][4] = 1.0
384
- masklist = [
385
- (
386
- despecklesavemask,
387
- "despeckle",
388
- "mask",
389
- None,
390
- "Voxels that underwent despeckling in the final pass",
391
- )
392
- ]
393
- tide_io.savemaplist(
394
- outputname,
395
- masklist,
396
- validvoxels,
397
- nativespaceshape,
398
- theheader,
399
- bidsbasedict,
400
- filetype=theinputdata.filetype,
401
- rt_floattype=rt_floattype,
402
- cifti_hdr=theinputdata.cifti_hdr,
403
- )
404
- LGR.info(
405
- f"\n\n{voxelsprocessed_fc_ds} voxels despeckled in "
406
- f"{optiondict['despeckle_passes']} passes"
407
- )
408
- TimingLGR.info(
409
- f"{similaritytype} despeckle end, pass {thepass}",
410
- {
411
- "message2": voxelsprocessed_fc_ds,
412
- "message3": "voxels",
413
- },
414
- )
415
-
416
- # Step 2c - patch shifting
417
- if optiondict["patchshift"]:
418
- outmaparray *= 0.0
419
- outmaparray[validvoxels] = eval("lagtimes")[:]
420
- # new method
421
- masklist = [
422
- (
423
- outmaparray[validvoxels],
424
- f"lagtimes_prepatch_pass{thepass}",
425
- "map",
426
- None,
427
- f"Input lagtimes map prior to patch map generation pass {thepass}",
428
- ),
429
- ]
430
- tide_io.savemaplist(
431
- outputname,
432
- masklist,
433
- validvoxels,
434
- nativespaceshape,
435
- theheader,
436
- bidsbasedict,
437
- filetype=theinputdata.filetype,
438
- rt_floattype=rt_floattype,
439
- cifti_hdr=theinputdata.cifti_hdr,
440
- )
441
-
442
- # create list of anomalous 3D regions that don't match surroundings
443
- if theinputdata.nim_affine is not None:
444
- # make an atlas of anomalous patches - each patch shares the same integer value
445
- step1 = tide_patch.calc_DoG(
446
- outmaparray.reshape(nativespaceshape).copy(),
447
- theinputdata.nim_affine,
448
- thesizes,
449
- fwhm=optiondict["patchfwhm"],
450
- ratioopt=False,
451
- debug=True,
452
- )
453
- masklist = [
454
- (
455
- step1.reshape(internalspaceshape)[validvoxels],
456
- f"DoG_pass{thepass}",
457
- "map",
458
- None,
459
- f"DoG map for pass {thepass}",
460
- ),
461
- ]
462
- tide_io.savemaplist(
463
- outputname,
464
- masklist,
465
- validvoxels,
466
- nativespaceshape,
467
- theheader,
468
- bidsbasedict,
469
- filetype=theinputdata.filetype,
470
- rt_floattype=rt_floattype,
471
- cifti_hdr=theinputdata.cifti_hdr,
472
- )
473
- step2 = tide_patch.invertedflood3D(
474
- step1,
475
- 1,
476
- )
477
- masklist = [
478
- (
479
- step2.reshape(internalspaceshape)[validvoxels],
480
- f"invertflood_pass{thepass}",
481
- "map",
482
- None,
483
- f"Inverted flood map for pass {thepass}",
484
- ),
485
- ]
486
- tide_io.savemaplist(
487
- outputname,
488
- masklist,
489
- validvoxels,
490
- nativespaceshape,
491
- theheader,
492
- bidsbasedict,
493
- filetype=theinputdata.filetype,
494
- rt_floattype=rt_floattype,
495
- cifti_hdr=theinputdata.cifti_hdr,
496
- )
497
-
498
- patchmap = tide_patch.separateclusters(
499
- step2,
500
- sizethresh=optiondict["patchminsize"],
501
- debug=True,
502
- )
503
- # patchmap = tide_patch.getclusters(
504
- # outmaparray.reshape(nativespaceshape),
505
- # theinputdata.nim_affine,
506
- # thesizes,
507
- # fwhm=optiondict["patchfwhm"],
508
- # ratioopt=True,
509
- # sizethresh=optiondict["patchminsize"],
510
- # debug=True,
511
- # )
512
- masklist = [
513
- (
514
- patchmap[validvoxels],
515
- f"patch_pass{thepass}",
516
- "map",
517
- None,
518
- f"Patch map for despeckling pass {thepass}",
519
- ),
520
- ]
521
- tide_io.savemaplist(
522
- outputname,
523
- masklist,
524
- validvoxels,
525
- nativespaceshape,
526
- theheader,
527
- bidsbasedict,
528
- filetype=theinputdata.filetype,
529
- rt_floattype=rt_floattype,
530
- cifti_hdr=theinputdata.cifti_hdr,
531
- )
532
-
533
- # now shift the patches to align with the majority of the image
534
- tide_patch.interppatch(lagtimes, patchmap[validvoxels])
535
-
536
- return internaldespeckleincludemask