rapidtide 3.0a13__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 (232) hide show
  1. rapidtide/Colortables.py +1 -1
  2. rapidtide/DerivativeDelay.py +4 -2
  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/testdelayvar +2 -1
  13. rapidtide/data/examples/src/testfmri +3 -2
  14. rapidtide/data/examples/src/testfuncs +1 -1
  15. rapidtide/data/examples/src/testhappy +12 -1
  16. rapidtide/data/examples/src/testmodels +33 -0
  17. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/loss.png +0 -0
  18. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/loss.txt +1 -0
  19. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/model.keras +0 -0
  20. rapidtide/data/models/model_cnn_w064_l13_fn20_fl08/model_meta.json +167 -0
  21. rapidtide/data/models/model_revised_tf2/model.keras +0 -0
  22. rapidtide/data/models/{model_serdar → model_revised_tf2}/model_meta.json +1 -1
  23. rapidtide/data/models/model_serdar2_tf2/model.keras +0 -0
  24. rapidtide/data/models/{model_serdar2 → model_serdar2_tf2}/model_meta.json +1 -1
  25. rapidtide/data/models/model_serdar_tf2/model.keras +0 -0
  26. rapidtide/data/models/{model_revised → model_serdar_tf2}/model_meta.json +1 -1
  27. rapidtide/dlfilter.py +325 -241
  28. rapidtide/externaltools.py +1 -1
  29. rapidtide/fMRIData_class.py +1 -1
  30. rapidtide/filter.py +1 -8
  31. rapidtide/fit.py +1 -8
  32. rapidtide/happy_supportfuncs.py +30 -1
  33. rapidtide/helper_classes.py +1 -1
  34. rapidtide/io.py +1 -1
  35. rapidtide/linfitfiltpass.py +3 -1
  36. rapidtide/makelaggedtcs.py +9 -6
  37. rapidtide/maskutil.py +1 -1
  38. rapidtide/miscmath.py +1 -8
  39. rapidtide/multiproc.py +9 -12
  40. rapidtide/patchmatch.py +1 -1
  41. rapidtide/peakeval.py +1 -1
  42. rapidtide/qualitycheck.py +1 -1
  43. rapidtide/refinedelay.py +174 -89
  44. rapidtide/refineregressor.py +1 -1
  45. rapidtide/resample.py +1 -1
  46. rapidtide/scripts/adjustoffset.py +1 -1
  47. rapidtide/scripts/aligntcs.py +1 -1
  48. rapidtide/scripts/applydlfilter.py +1 -1
  49. rapidtide/scripts/atlasaverage.py +1 -1
  50. rapidtide/scripts/atlastool.py +1 -1
  51. rapidtide/scripts/calcicc.py +1 -1
  52. rapidtide/scripts/calctexticc.py +1 -1
  53. rapidtide/scripts/calcttest.py +1 -1
  54. rapidtide/scripts/ccorrica.py +1 -1
  55. rapidtide/scripts/delayvar.py +2 -2
  56. rapidtide/scripts/diffrois.py +1 -1
  57. rapidtide/scripts/endtidalproc.py +1 -1
  58. rapidtide/scripts/fdica.py +1 -1
  59. rapidtide/scripts/filtnifti.py +1 -1
  60. rapidtide/scripts/filttc.py +1 -1
  61. rapidtide/scripts/fingerprint.py +1 -1
  62. rapidtide/scripts/fixtr.py +1 -1
  63. rapidtide/scripts/gmscalc.py +1 -1
  64. rapidtide/scripts/happy.py +1 -1
  65. rapidtide/scripts/happy2std.py +1 -1
  66. rapidtide/scripts/happywarp.py +1 -1
  67. rapidtide/scripts/histnifti.py +1 -1
  68. rapidtide/scripts/histtc.py +1 -1
  69. rapidtide/scripts/linfitfilt.py +1 -1
  70. rapidtide/scripts/localflow.py +1 -1
  71. rapidtide/scripts/mergequality.py +1 -1
  72. rapidtide/scripts/pairproc.py +1 -1
  73. rapidtide/scripts/pairwisemergenifti.py +1 -1
  74. rapidtide/scripts/physiofreq.py +1 -1
  75. rapidtide/scripts/pixelcomp.py +1 -1
  76. rapidtide/scripts/plethquality.py +1 -1
  77. rapidtide/scripts/polyfitim.py +1 -1
  78. rapidtide/scripts/proj2flow.py +1 -1
  79. rapidtide/scripts/rankimage.py +1 -1
  80. rapidtide/scripts/rapidtide.py +1 -1
  81. rapidtide/scripts/rapidtide2std.py +1 -1
  82. rapidtide/scripts/resamplenifti.py +1 -1
  83. rapidtide/scripts/resampletc.py +1 -1
  84. rapidtide/scripts/retrolagtcs.py +1 -1
  85. rapidtide/scripts/retroregress.py +1 -1
  86. rapidtide/scripts/roisummarize.py +1 -1
  87. rapidtide/scripts/runqualitycheck.py +1 -1
  88. rapidtide/scripts/showarbcorr.py +1 -1
  89. rapidtide/scripts/showhist.py +1 -1
  90. rapidtide/scripts/showstxcorr.py +1 -1
  91. rapidtide/scripts/showtc.py +1 -1
  92. rapidtide/scripts/showxcorr_legacy.py +1 -1
  93. rapidtide/scripts/showxcorrx.py +1 -1
  94. rapidtide/scripts/showxy.py +1 -1
  95. rapidtide/scripts/simdata.py +1 -1
  96. rapidtide/scripts/spatialdecomp.py +1 -1
  97. rapidtide/scripts/spatialfit.py +1 -1
  98. rapidtide/scripts/spatialmi.py +1 -1
  99. rapidtide/scripts/spectrogram.py +1 -1
  100. rapidtide/scripts/stupidramtricks.py +1 -1
  101. rapidtide/scripts/synthASL.py +1 -1
  102. rapidtide/scripts/tcfrom2col.py +1 -1
  103. rapidtide/scripts/tcfrom3col.py +1 -1
  104. rapidtide/scripts/temporaldecomp.py +1 -1
  105. rapidtide/scripts/testhrv.py +1 -1
  106. rapidtide/scripts/threeD.py +1 -1
  107. rapidtide/scripts/tidepool.py +1 -1
  108. rapidtide/scripts/variabilityizer.py +1 -1
  109. rapidtide/simfuncfit.py +1 -1
  110. rapidtide/stats.py +1 -8
  111. rapidtide/tests/cleanposttest +1 -1
  112. rapidtide/tests/resethappytargets +1 -1
  113. rapidtide/tests/resetrapidtidetargets +1 -1
  114. rapidtide/tests/resettargets +1 -1
  115. rapidtide/tests/runlocaltest +1 -1
  116. rapidtide/tests/showkernels +1 -1
  117. rapidtide/tests/test_aliasedcorrelate.py +1 -1
  118. rapidtide/tests/test_aligntcs.py +1 -1
  119. rapidtide/tests/test_calcicc.py +1 -1
  120. rapidtide/tests/test_congrid.py +1 -1
  121. rapidtide/tests/test_correlate.py +1 -1
  122. rapidtide/tests/test_corrpass.py +1 -1
  123. rapidtide/tests/test_delayestimation.py +1 -1
  124. rapidtide/tests/test_doresample.py +1 -1
  125. rapidtide/tests/test_fastresampler.py +1 -1
  126. rapidtide/tests/test_findmaxlag.py +1 -1
  127. rapidtide/tests/test_fullrunhappy_v1.py +15 -7
  128. rapidtide/tests/test_fullrunhappy_v2.py +18 -10
  129. rapidtide/tests/test_fullrunhappy_v3.py +18 -10
  130. rapidtide/tests/test_fullrunhappy_v4.py +18 -10
  131. rapidtide/tests/test_fullrunhappy_v5.py +15 -9
  132. rapidtide/tests/test_fullrunrapidtide_v1.py +19 -11
  133. rapidtide/tests/test_fullrunrapidtide_v2.py +24 -14
  134. rapidtide/tests/test_fullrunrapidtide_v3.py +15 -7
  135. rapidtide/tests/test_fullrunrapidtide_v4.py +15 -8
  136. rapidtide/tests/test_fullrunrapidtide_v5.py +13 -5
  137. rapidtide/tests/test_fullrunrapidtide_v6.py +24 -16
  138. rapidtide/tests/test_io.py +1 -1
  139. rapidtide/tests/test_linfitfiltpass.py +1 -1
  140. rapidtide/tests/test_mi.py +1 -1
  141. rapidtide/tests/test_miscmath.py +1 -1
  142. rapidtide/tests/test_motionregress.py +1 -1
  143. rapidtide/tests/test_nullcorr.py +1 -1
  144. rapidtide/tests/test_padvec.py +1 -1
  145. rapidtide/tests/test_phaseanalysis.py +1 -1
  146. rapidtide/tests/test_rapidtideparser.py +1 -1
  147. rapidtide/tests/test_refinedelay.py +4 -3
  148. rapidtide/tests/test_runmisc.py +1 -1
  149. rapidtide/tests/test_sharedmem.py +1 -1
  150. rapidtide/tests/test_simulate.py +1 -1
  151. rapidtide/tests/test_stcorrelate.py +1 -1
  152. rapidtide/tests/test_timeshift.py +1 -1
  153. rapidtide/tests/test_valtoindex.py +1 -1
  154. rapidtide/tests/test_zRapidtideDataset.py +1 -1
  155. rapidtide/tests/utils.py +1 -1
  156. rapidtide/transformerdlfilter.py +2 -4
  157. rapidtide/util.py +37 -14
  158. rapidtide/voxelData.py +278 -0
  159. rapidtide/wiener.py +1 -1
  160. rapidtide/wiener2.py +1 -1
  161. rapidtide/workflows/adjustoffset.py +1 -1
  162. rapidtide/workflows/aligntcs.py +1 -1
  163. rapidtide/workflows/applydlfilter.py +20 -65
  164. rapidtide/workflows/atlasaverage.py +41 -13
  165. rapidtide/workflows/atlastool.py +1 -1
  166. rapidtide/workflows/calctexticc.py +1 -1
  167. rapidtide/workflows/ccorrica.py +1 -1
  168. rapidtide/workflows/cleanregressor.py +243 -0
  169. rapidtide/workflows/delayestimation.py +488 -0
  170. rapidtide/workflows/delayvar.py +122 -34
  171. rapidtide/workflows/diffrois.py +1 -1
  172. rapidtide/workflows/endtidalproc.py +1 -1
  173. rapidtide/workflows/fdica.py +1 -1
  174. rapidtide/workflows/filtnifti.py +1 -1
  175. rapidtide/workflows/filttc.py +1 -1
  176. rapidtide/workflows/fixtr.py +1 -1
  177. rapidtide/workflows/gmscalc.py +1 -1
  178. rapidtide/workflows/happy.py +49 -3
  179. rapidtide/workflows/happy2std.py +1 -1
  180. rapidtide/workflows/happy_parser.py +30 -6
  181. rapidtide/workflows/histnifti.py +1 -1
  182. rapidtide/workflows/histtc.py +1 -1
  183. rapidtide/workflows/linfitfilt.py +1 -1
  184. rapidtide/workflows/localflow.py +1 -1
  185. rapidtide/workflows/mergequality.py +1 -1
  186. rapidtide/workflows/niftidecomp.py +1 -1
  187. rapidtide/workflows/niftistats.py +1 -1
  188. rapidtide/workflows/pairproc.py +1 -1
  189. rapidtide/workflows/pairwisemergenifti.py +1 -1
  190. rapidtide/workflows/parser_funcs.py +1 -1
  191. rapidtide/workflows/physiofreq.py +1 -1
  192. rapidtide/workflows/pixelcomp.py +1 -1
  193. rapidtide/workflows/plethquality.py +1 -1
  194. rapidtide/workflows/polyfitim.py +1 -1
  195. rapidtide/workflows/proj2flow.py +1 -1
  196. rapidtide/workflows/rankimage.py +1 -1
  197. rapidtide/workflows/rapidtide.py +174 -686
  198. rapidtide/workflows/rapidtide2std.py +1 -1
  199. rapidtide/workflows/rapidtide_parser.py +14 -113
  200. rapidtide/workflows/regressfrommaps.py +6 -48
  201. rapidtide/workflows/resamplenifti.py +1 -1
  202. rapidtide/workflows/resampletc.py +1 -1
  203. rapidtide/workflows/retrolagtcs.py +2 -1
  204. rapidtide/workflows/retroregress.py +26 -2
  205. rapidtide/workflows/roisummarize.py +1 -1
  206. rapidtide/workflows/runqualitycheck.py +1 -1
  207. rapidtide/workflows/showarbcorr.py +1 -1
  208. rapidtide/workflows/showhist.py +1 -1
  209. rapidtide/workflows/showstxcorr.py +1 -1
  210. rapidtide/workflows/showtc.py +1 -1
  211. rapidtide/workflows/showxcorrx.py +1 -1
  212. rapidtide/workflows/showxy.py +1 -1
  213. rapidtide/workflows/simdata.py +1 -1
  214. rapidtide/workflows/spatialfit.py +1 -1
  215. rapidtide/workflows/spatialmi.py +1 -1
  216. rapidtide/workflows/spectrogram.py +1 -1
  217. rapidtide/workflows/synthASL.py +1 -1
  218. rapidtide/workflows/tcfrom2col.py +1 -1
  219. rapidtide/workflows/tcfrom3col.py +1 -1
  220. rapidtide/workflows/tidepool.py +2 -1
  221. rapidtide/workflows/utils.py +1 -1
  222. rapidtide/workflows/variabilityizer.py +1 -1
  223. {rapidtide-3.0a13.dist-info → rapidtide-3.0a15.dist-info}/METADATA +1 -1
  224. rapidtide-3.0a15.dist-info/RECORD +353 -0
  225. {rapidtide-3.0a13.dist-info → rapidtide-3.0a15.dist-info}/WHEEL +1 -1
  226. rapidtide/data/models/model_revised/model.h5 +0 -0
  227. rapidtide/data/models/model_serdar/model.h5 +0 -0
  228. rapidtide/data/models/model_serdar2/model.h5 +0 -0
  229. rapidtide-3.0a13.dist-info/RECORD +0 -345
  230. {rapidtide-3.0a13.dist-info → rapidtide-3.0a15.dist-info}/entry_points.txt +0 -0
  231. {rapidtide-3.0a13.dist-info → rapidtide-3.0a15.dist-info}/licenses/LICENSE +0 -0
  232. {rapidtide-3.0a13.dist-info → rapidtide-3.0a15.dist-info}/top_level.txt +0 -0
@@ -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.
@@ -27,6 +27,7 @@ from pathlib import Path
27
27
  import numpy as np
28
28
  from scipy.stats import pearsonr
29
29
  from sklearn.decomposition import PCA
30
+ from tf_keras.src.dtensor.integration_test_utils import train_step
30
31
 
31
32
  import rapidtide.filter as tide_filt
32
33
  import rapidtide.io as tide_io
@@ -61,7 +62,8 @@ DEFAULT_DELAYOFFSETSPATIALFILT = -1
61
62
  DEFAULT_WINDOWSIZE = 30.0
62
63
  DEFAULT_SYSTEMICFITTYPE = "pca"
63
64
  DEFAULT_PCACOMPONENTS = 1
64
- DEFAULT_TRAINWIDTH = 0.0
65
+ DEFAULT_LAGMIN = 0.0
66
+ DEFAULT_LAGMAX = 0.0
65
67
  DEFAULT_TRAINSTEP = 0.5
66
68
 
67
69
 
@@ -146,19 +148,19 @@ def _get_parser():
146
148
  default=True,
147
149
  )
148
150
  parser.add_argument(
149
- "--trainwidth",
150
- dest="trainwidth",
151
- action="store",
151
+ "--trainrange",
152
+ dest="lag_extrema",
153
+ action=pf.IndicateSpecifiedAction,
154
+ nargs=2,
152
155
  type=float,
153
- metavar="WIDTH",
156
+ metavar=("LAGMIN", "LAGMAX"),
154
157
  help=(
155
- "Train the ratio offset function over this range of central delays (in seconds). The derivative "
158
+ "Set the range of delay offset center frequencies to span LAGMIN to LAGMAX. The derivative "
156
159
  "ratio calculation only works over a narrow range, so if the static offset is large, "
157
160
  "you need to train the ratio calculation with a central delay close to that value. "
158
- "Set negative to select the width automatically. "
159
- f"Default is {DEFAULT_TRAINWIDTH}"
161
+ f"LAGMAX. Default is {DEFAULT_LAGMIN} to {DEFAULT_LAGMAX} seconds. "
160
162
  ),
161
- default=DEFAULT_TRAINWIDTH,
163
+ default=(DEFAULT_LAGMIN, DEFAULT_LAGMAX),
162
164
  )
163
165
  parser.add_argument(
164
166
  "--trainstep",
@@ -209,6 +211,13 @@ def _get_parser():
209
211
  help="Use NCOMP components for PCA fit of delay offset.",
210
212
  default=DEFAULT_PCACOMPONENTS,
211
213
  )
214
+ parser.add_argument(
215
+ "--verbose",
216
+ dest="verbose",
217
+ action="store_true",
218
+ help=("Be wicked chatty."),
219
+ default=False,
220
+ )
212
221
  parser.add_argument(
213
222
  "--debug",
214
223
  dest="debug",
@@ -259,6 +268,9 @@ def delayvar(args):
259
268
  # get the pid of the parent process
260
269
  args.pid = os.getpid()
261
270
 
271
+ args.lagmin = args.lag_extrema[0]
272
+ args.lagmax = args.lag_extrema[1]
273
+
262
274
  # specify the output name
263
275
  if args.alternateoutput is None:
264
276
  outputname = args.datafileroot
@@ -502,6 +514,16 @@ def delayvar(args):
502
514
  # windowed delay deviation estimation
503
515
  lagstouse_valid = lagtimes_valid
504
516
 
517
+ # find the robust range of the static delays
518
+ (
519
+ pct02,
520
+ pct98,
521
+ ) = tide_stats.getfracvals(lagstouse_valid, [0.02, 0.98], debug=args.debug)
522
+ if args.lagmin == -999:
523
+ args.lagmin = np.round(pct02 / args.trainstep, 0) * args.trainstep
524
+ if args.lagmax == -999:
525
+ args.lagmax = np.round(pct98 / args.trainstep, 0) * args.trainstep
526
+
505
527
  print("\n\nWindowed delay estimation")
506
528
  TimingLGR.info("Windowed delay estimation start")
507
529
  LGR.info("\n\nWindowed delay estimation")
@@ -537,7 +559,7 @@ def delayvar(args):
537
559
  thehpf.apply(genlagsamplerate, reference_y),
538
560
  padtime=thepadtime,
539
561
  )
540
- genlagtc.save(f"{outputname}_desc-lagtcgenerator_timeseries")
562
+ genlagtc.save(f"{outputname}_desc-hpflagtcgenerator_timeseries")
541
563
 
542
564
  # and filter the data if necessary
543
565
  if args.hpf:
@@ -582,7 +604,7 @@ def delayvar(args):
582
604
  numwins,
583
605
  )
584
606
  internalwinfmrishape = (numvalidspatiallocs, wintrs)
585
- if args.debug or args.focaldebug:
607
+ if args.debug:
586
608
  print(f"window space shape = {internalwinspaceshape}")
587
609
  print(f"internalwindowfmrishape shape = {internalwinfmrishape}")
588
610
 
@@ -591,6 +613,7 @@ def delayvar(args):
591
613
  windowedmedfiltregressderivratios = np.zeros(internalwinspaceshape, dtype=float)
592
614
  windowedfilteredregressderivratios = np.zeros(internalwinspaceshape, dtype=float)
593
615
  windoweddelayoffset = np.zeros(internalwinspaceshape, dtype=float)
616
+ windowedclosestoffset = np.zeros(internalwinspaceshape, dtype=float)
594
617
  if usesharedmem:
595
618
  if args.debug:
596
619
  print("allocating shared memory")
@@ -626,17 +649,30 @@ def delayvar(args):
626
649
  if args.debug:
627
650
  print(f"wintrs={wintrs}, winskip={winskip}, numtrs={numtrs}, numwins={numwins}")
628
651
  thewindowprocoptions = therunoptions
652
+ if args.verbose:
653
+ thewindowprocoptions["showprogressbar"] = True
654
+ else:
655
+ thewindowprocoptions["showprogressbar"] = False
629
656
  if args.focaldebug:
630
657
  thewindowprocoptions["saveminimumsLFOfiltfiles"] = True
631
658
  winoutputlevel = "max"
632
659
  else:
633
660
  thewindowprocoptions["saveminimumsLFOfiltfiles"] = False
634
661
  winoutputlevel = "min"
662
+
663
+ # Now get the derivative ratios the individual windows
664
+ print("Finding derivative ratios:")
635
665
  for thewin in range(numwins):
636
- print(f"Processing window {thewin + 1} of {numwins}")
666
+ print(f"\tProcessing window {thewin + 1} of {numwins}")
637
667
  starttr = thewin * winskip
638
668
  endtr = starttr + wintrs
639
669
  winlabel = f"_win-{str(thewin + 1).zfill(3)}"
670
+ if args.verbose:
671
+ thisLGR = LGR
672
+ thisTimingLGR = TimingLGR
673
+ else:
674
+ thisLGR = None
675
+ thisTimingLGR = None
640
676
 
641
677
  windowedregressderivratios[:, thewin], windowedregressrvalues[:, thewin] = (
642
678
  tide_refinedelay.getderivratios(
@@ -657,8 +693,8 @@ def delayvar(args):
657
693
  winmovingsignal,
658
694
  winlagtc,
659
695
  winfiltereddata,
660
- LGR,
661
- TimingLGR,
696
+ thisLGR,
697
+ thisTimingLGR,
662
698
  thewindowprocoptions,
663
699
  regressderivs=1,
664
700
  starttr=starttr,
@@ -666,7 +702,33 @@ def delayvar(args):
666
702
  debug=args.debug,
667
703
  )
668
704
  )
705
+ if args.focaldebug:
706
+ theheader = copy.deepcopy(fmri_header)
707
+ theheader["dim"][4] = wintrs
708
+ theheader["toffset"] = winwidth / 2.0
709
+ maplist = [
710
+ (
711
+ winlagtc,
712
+ "windowedlagtcs",
713
+ "bold",
714
+ None,
715
+ f"Lagtcs in each {winspace} second window",
716
+ ),
717
+ ]
718
+ tide_io.savemaplist(
719
+ outputname + winlabel,
720
+ maplist,
721
+ validvoxels,
722
+ (xsize, ysize, numslices, wintrs),
723
+ theheader,
724
+ bidsbasedict,
725
+ debug=args.debug,
726
+ )
669
727
 
728
+ # Filter the derivative ratios
729
+ print("Filtering derivative ratios:")
730
+ for thewin in range(numwins):
731
+ print(f"\tProcessing window {thewin + 1} of {numwins}")
670
732
  (
671
733
  windowedmedfiltregressderivratios[:, thewin],
672
734
  windowedfilteredregressderivratios[:, thewin],
@@ -681,34 +743,62 @@ def delayvar(args):
681
743
  fileiscifti=False,
682
744
  textio=False,
683
745
  rt_floattype=rt_floattype,
746
+ verbose=args.verbose,
684
747
  debug=args.debug,
685
748
  )
686
749
 
750
+ # Train the ratio offsets
751
+ print("Training ratio offsets:")
752
+ for thewin in range(numwins):
753
+ print(f"\tProcessing window {thewin + 1} of {numwins}")
754
+ starttr = thewin * winskip
755
+ endtr = starttr + wintrs
756
+ winlabel = f"_win-{str(thewin + 1).zfill(3)}"
687
757
  # find the mapping of glm ratios to delays
688
758
  tide_refinedelay.trainratiotooffset(
689
759
  genlagtc,
690
760
  initial_fmri_x[starttr:endtr],
691
761
  outputname + winlabel,
692
762
  winoutputlevel,
693
- trainwidth=args.trainwidth,
694
- trainstep=args.trainstep,
763
+ trainlagmin=args.lagmin,
764
+ trainlagmax=args.lagmax,
765
+ trainlagstep=args.trainstep,
695
766
  mindelay=args.mindelay,
696
767
  maxdelay=args.maxdelay,
697
768
  numpoints=args.numpoints,
698
- debug=args.debug,
769
+ verbose=args.verbose,
770
+ debug=args.focaldebug,
699
771
  )
700
772
  TimingLGR.info("Refinement calibration end")
701
773
 
702
- # now calculate the delay offsets
774
+ # now calculate the delay offsets
775
+ print("Calculating delay offsets:")
776
+ for thewin in range(numwins):
777
+ print(f"\tProcessing window {thewin + 1} of {numwins}")
778
+ winlabel = f"_win-{str(thewin + 1).zfill(3)}"
703
779
  TimingLGR.info("Calculating delay offsets")
704
- if args.focaldebug:
780
+ if args.debug:
705
781
  print(
706
782
  f"calculating delayoffsets for {windowedfilteredregressderivratios.shape[0]} voxels"
707
783
  )
708
784
  for i in range(windowedfilteredregressderivratios.shape[0]):
709
- windoweddelayoffset[i, thewin] = tide_refinedelay.ratiotodelay(
710
- windowedfilteredregressderivratios[i, thewin]
785
+ (windoweddelayoffset[i, thewin], windowedclosestoffset[i, thewin]) = (
786
+ tide_refinedelay.ratiotodelay(
787
+ windowedfilteredregressderivratios[i, thewin],
788
+ offset=lagstouse_valid[i],
789
+ debug=args.focaldebug,
790
+ )
711
791
  )
792
+ namesuffix = "_desc-delayoffset_hist"
793
+ tide_stats.makeandsavehistogram(
794
+ windoweddelayoffset[:, thewin],
795
+ therunoptions["histlen"],
796
+ 1,
797
+ outputname + winlabel + namesuffix,
798
+ displaytitle="Histogram of delay offsets calculated from GLM",
799
+ dictvarname="delayoffsethist",
800
+ thedict=None,
801
+ )
712
802
 
713
803
  # now see if there are common timecourses in the delay offsets
714
804
  themean = np.mean(windoweddelayoffset, axis=1)
@@ -749,7 +839,7 @@ def delayvar(args):
749
839
  # unscale the PCA cleaned data
750
840
  for vox in range(0, windoweddelayoffset.shape[0]):
751
841
  reduceddata[vox, :] = reduceddata[vox, :] * thevar[vox] + themean[vox]
752
- if args.focaldebug:
842
+ if args.debug:
753
843
  print("complex processing: reduceddata.shape =", scaledvoxels.shape)
754
844
  # pcadata = np.mean(reduceddata, axis=0)
755
845
  pcadata = thefit.components_[0]
@@ -850,22 +940,13 @@ def delayvar(args):
850
940
  nprocs_regressionfilt=args.nprocs,
851
941
  regressderivs=1,
852
942
  showprogressbar=args.showprogressbar,
853
- debug=args.focaldebug,
943
+ debug=args.debug,
854
944
  )
855
945
 
856
- namesuffix = f"_desc-delayoffsetwin{thewin}_hist"
857
- tide_stats.makeandsavehistogram(
858
- windoweddelayoffset[:, thewin],
859
- therunoptions["histlen"],
860
- 1,
861
- outputname + namesuffix,
862
- displaytitle="Histogram of delay offsets calculated from GLM",
863
- dictvarname="delayoffsethist",
864
- thedict=None,
865
- )
866
946
  theheader = copy.deepcopy(fmri_header)
867
947
  theheader["dim"][4] = numwins
868
948
  theheader["pixdim"][4] = winspace
949
+ theheader["toffset"] = winwidth / 2.0
869
950
  maplist = [
870
951
  (
871
952
  windoweddelayoffset,
@@ -874,6 +955,13 @@ def delayvar(args):
874
955
  None,
875
956
  f"Delay offsets in each {winspace} second window",
876
957
  ),
958
+ (
959
+ windowedclosestoffset,
960
+ "windowedclosestoffset",
961
+ "info",
962
+ None,
963
+ f"Closest delay offsets in each {winspace} second window",
964
+ ),
877
965
  (
878
966
  np.square(windowedregressrvalues),
879
967
  "windowedregressr2values",
@@ -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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
  #
4
- # Copyright 2018-2024 Blaise Frederick
4
+ # Copyright 2018-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.
@@ -205,7 +205,6 @@ def happy_main(argparsingfunc):
205
205
  tide_util.logmem("before mask creation")
206
206
  # mask = np.uint16(masking.compute_epi_mask(nim).dataobj.reshape(numspatiallocs))
207
207
  mask = np.uint16(tide_mask.makeepimask(nim).dataobj.reshape(numspatiallocs))
208
- validvoxels = np.where(mask > 0)[0]
209
208
  theheader = copy.deepcopy(nim_hdr)
210
209
  theheader["dim"][4] = 1
211
210
  timings.append(["Mask created", time.time(), None, None])
@@ -236,13 +235,14 @@ def happy_main(argparsingfunc):
236
235
  # filter out motion regressors here
237
236
  if args.motionfilename is not None:
238
237
  timings.append(["Motion filtering start", time.time(), None, None])
239
- motiondict = tide_io.readmotion(args.motionfilename, tr=tr)
238
+ motiondict = tide_io.readmotion(args.motionfilename, tr=tr, colspec=args.motionfilecolspec)
240
239
  confoundregressors, confoundregressorlabels = tide_fit.calcexpandedregressors(
241
240
  motiondict,
242
241
  labels=["xtrans", "ytrans", "ztrans", "xrot", "yrot", "zrot"],
243
242
  deriv=args.motfilt_deriv,
244
243
  order=args.motfilt_order,
245
244
  )
245
+ tide_util.disablemkl(args.nprocs)
246
246
  (motionregressors, motionregressorlabels, filtereddata, confoundr2) = (
247
247
  tide_linfitfiltpass.confoundregress(
248
248
  confoundregressors,
@@ -253,8 +253,10 @@ def happy_main(argparsingfunc):
253
253
  tcstart=args.motskip,
254
254
  tchp=args.motionhp,
255
255
  tclp=args.motionlp,
256
+ nprocs=args.nprocs,
256
257
  )
257
258
  )
259
+ tide_util.enablemkl(args.mklthreads)
258
260
  if confoundr2 is None:
259
261
  print("There are no nonzero confound regressors - exiting")
260
262
  sys.exit()
@@ -273,6 +275,17 @@ def happy_main(argparsingfunc):
273
275
  append=False,
274
276
  debug=args.debug,
275
277
  )
278
+ # save motionr2 map
279
+ theheader = copy.deepcopy(nim_hdr)
280
+ theheader["dim"][4] = 1
281
+ motionr2filename = outputroot + "_desc-motionr2_map"
282
+ bidsdict = bidsbasedict.copy()
283
+ tide_io.writedicttojson(bidsdict, motionr2filename + ".json")
284
+ outarray = np.zeros((xsize, ysize, numslices), dtype=float)
285
+ outarray.reshape(numspatiallocs)[validprojvoxels] = confoundr2
286
+ tide_io.savetonifti(
287
+ outarray.reshape((xsize, ysize, numslices)), theheader, motionr2filename
288
+ )
276
289
  if args.savemotionglmfilt:
277
290
  motionfilteredfilename = outputroot + "_desc-motionfiltered_bold"
278
291
  bidsdict = bidsbasedict.copy()
@@ -372,6 +385,7 @@ def happy_main(argparsingfunc):
372
385
  # now get an estimate of the cardiac signal
373
386
  print("estimating cardiac signal from fmri data")
374
387
  tide_util.logmem("before cardiacfromimage")
388
+ tide_util.disablemkl(args.nprocs)
375
389
  (
376
390
  cardfromfmri_sliceres,
377
391
  cardfromfmri_normfac,
@@ -402,6 +416,7 @@ def happy_main(argparsingfunc):
402
416
  debug=args.debug,
403
417
  verbose=args.verbose,
404
418
  )
419
+ tide_util.enablemkl(args.mklthreads)
405
420
  timings.append(
406
421
  [
407
422
  "Cardiac signal generated from image data" + passstring,
@@ -602,6 +617,15 @@ def happy_main(argparsingfunc):
602
617
  )
603
618
  thedlfilter = tide_dlfilt.DeepLearningFilter(modelpath=modelpath)
604
619
  thedlfilter.loadmodel(args.modelname)
620
+ updatemodels = False
621
+ if updatemodels:
622
+ updatedmodelname = f"{args.modelname}_tf2"
623
+ newmodeldir = os.path.join(
624
+ "/Users/frederic/code/rapidtide/rapidtide/data/models", updatedmodelname
625
+ )
626
+ print(f"creating {newmodeldir}")
627
+ tide_util.makeadir(newmodeldir)
628
+ thedlfilter.savemodel(altname=newmodeldir)
605
629
  infodict["dlfiltermodel"] = args.modelname
606
630
  normdlfilteredcard_stdres = thedlfilter.apply(normcardfromfmri_stdres)
607
631
  dlfilteredcard_stdres = thedlfilter.apply(cardfromfmri_stdres)
@@ -966,6 +990,24 @@ def happy_main(argparsingfunc):
966
990
  append=True,
967
991
  debug=args.debug,
968
992
  )
993
+ badpointlist_stdres = np.round(
994
+ tide_resample.arbresample(
995
+ badpointlist,
996
+ slicesamplerate,
997
+ args.stdfreq,
998
+ decimate=True,
999
+ debug=False,
1000
+ ),
1001
+ 0,
1002
+ )
1003
+ tide_io.writebidstsv(
1004
+ outputroot + "_desc-stdrescardfromfmri_timeseries",
1005
+ badpointlist_stdres,
1006
+ args.stdfreq,
1007
+ columns=["badpts"],
1008
+ append=True,
1009
+ debug=args.debug,
1010
+ )
969
1011
 
970
1012
  # extract the fundamental
971
1013
  if args.forcedhr is not None:
@@ -1755,6 +1797,7 @@ def happy_main(argparsingfunc):
1755
1797
  fitNorm = np.zeros(timepoints, dtype=np.float64)
1756
1798
  datatoremove = 0.0 * fmri_data
1757
1799
  print("Running spatial regression on", timepoints, "timepoints")
1800
+ tide_util.disablemkl(args.nprocs)
1758
1801
  tide_linfitfiltpass.linfitfiltpass(
1759
1802
  timepoints,
1760
1803
  fmri_data[validlocs, :],
@@ -1771,6 +1814,7 @@ def happy_main(argparsingfunc):
1771
1814
  procbyvoxel=False,
1772
1815
  nprocs=args.nprocs,
1773
1816
  )
1817
+ tide_util.enablemkl(args.mklthreads)
1774
1818
  print(datatoremove.shape, cardiacnoise.shape, fitcoffs.shape)
1775
1819
  # datatoremove[validlocs, :] = np.multiply(cardiacnoise[validlocs, :], fitcoffs[:, None])
1776
1820
  filtereddata = fmri_data - datatoremove
@@ -1815,6 +1859,7 @@ def happy_main(argparsingfunc):
1815
1859
  fitNorm = np.zeros(numspatiallocs, dtype=np.float64)
1816
1860
  datatoremove = 0.0 * fmri_data
1817
1861
  print("Running temporal regression on", numvalidspatiallocs, "voxels")
1862
+ tide_util.disablemkl(args.nprocs)
1818
1863
  tide_linfitfiltpass.linfitfiltpass(
1819
1864
  numvalidspatiallocs,
1820
1865
  fmri_data[validlocs, :],
@@ -1830,6 +1875,7 @@ def happy_main(argparsingfunc):
1830
1875
  procbyvoxel=True,
1831
1876
  nprocs=args.nprocs,
1832
1877
  )
1878
+ tide_util.enablemkl(args.mklthreads)
1833
1879
  datatoremove[validlocs, :] = np.multiply(
1834
1880
  cardiacnoise[validlocs, :], fitcoffs[validlocs, None]
1835
1881
  )
@@ -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 2019-2024 Blaise Frederick
4
+ # Copyright 2019-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.
@@ -21,9 +21,11 @@ import argparse
21
21
  import numpy as np
22
22
 
23
23
  import rapidtide.io as tide_io
24
+ import rapidtide.multiproc as tide_multiproc
24
25
  import rapidtide.workflows.parser_funcs as pf
25
26
 
26
27
  DEFAULT_ALIASEDCORRELATIONWIDTH = 5.0
28
+ DEFAULT_DL_MODEL = "model_revised_tf2"
27
29
 
28
30
 
29
31
  def _get_parser():
@@ -99,10 +101,10 @@ def _get_parser():
99
101
  dest="modelname",
100
102
  metavar="MODELNAME",
101
103
  help=(
102
- "Use model MODELNAME for dl filter (default is model_revised - "
104
+ f"Use model MODELNAME for dl filter (default is {DEFAULT_DL_MODEL} - "
103
105
  "from the revised NeuroImage paper.) "
104
106
  ),
105
- default="model_revised",
107
+ default=DEFAULT_DL_MODEL,
106
108
  )
107
109
 
108
110
  # Performance
@@ -120,7 +122,20 @@ def _get_parser():
120
122
  ),
121
123
  default=1,
122
124
  )
123
-
125
+ performance_opts.add_argument(
126
+ "--nprocs",
127
+ dest="nprocs",
128
+ action="store",
129
+ metavar="NPROCS",
130
+ type=lambda x: pf.is_int(parser, x),
131
+ help=(
132
+ "Use NPROCS CPUs to accelerate processing (defaults to 1 - more "
133
+ "CPUs up to the number of cores can accelerate processing a lot, but "
134
+ "you need to remember to ask for this many CPUs on clusters.) Entering a mulitprocessor "
135
+ "routine disables mklthreads (otherwise there's chaos)."
136
+ ),
137
+ default=1,
138
+ )
124
139
  # Preprocessing
125
140
  preprocessing_opts = parser.add_argument_group("Preprocessing")
126
141
  preprocessing_opts.add_argument(
@@ -143,7 +158,7 @@ def _get_parser():
143
158
  )
144
159
  preprocessing_opts.add_argument(
145
160
  "--motionfile",
146
- dest="motionfilename",
161
+ dest="motionfilespec",
147
162
  metavar="MOTFILE",
148
163
  help=(
149
164
  "Read 6 columns of motion regressors out of MOTFILE file (.par or BIDS .json) "
@@ -719,7 +734,6 @@ def process_args(inputargs=None):
719
734
  args.outputlevel = 1
720
735
  args.maskthreshpct = 10.0
721
736
  args.domadnorm = True
722
- args.nprocs = 1
723
737
  args.verbose = False
724
738
  args.smoothlen = 101
725
739
  args.envthresh = 0.2
@@ -738,6 +752,16 @@ def process_args(inputargs=None):
738
752
  if args.disablenotch:
739
753
  args.notchpct = None
740
754
 
755
+ # process motionfile information
756
+ if args.motionfilespec is not None:
757
+ (args.motionfilename, args.motionfilecolspec) = tide_io.parsefilespec(args.motionfilespec)
758
+ else:
759
+ args.motionfilename = None
760
+
761
+ # set the number of worker processes if multiprocessing
762
+ if args.nprocs < 1:
763
+ args.nprocs = tide_multiproc.maxcpus()
764
+
741
765
  # process infotags
742
766
  args = pf.postprocesstagopts(args)
743
767
 
@@ -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 2024-2024 Blaise Frederick
4
+ # Copyright 2024-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.