rapidtide 3.0a15__py3-none-any.whl → 3.0.2__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.
@@ -34,6 +34,7 @@ import rapidtide.refinedelay as tide_refinedelay
34
34
  import rapidtide.resample as tide_resample
35
35
  import rapidtide.stats as tide_stats
36
36
  import rapidtide.util as tide_util
37
+ import rapidtide.voxelData as tide_voxelData
37
38
  import rapidtide.workflows.parser_funcs as pf
38
39
  import rapidtide.workflows.regressfrommaps as tide_regressfrommaps
39
40
 
@@ -127,13 +128,14 @@ def _get_parser():
127
128
  dest="outputlevel",
128
129
  action="store",
129
130
  type=str,
130
- choices=["min", "less", "normal", "more", "max"],
131
+ choices=["min", "less", "normal", "more", "max", "onlyregressors"],
131
132
  help=(
132
133
  "The level of file output produced. 'min' produces only absolutely essential files, 'less' adds in "
133
134
  "the sLFO filtered data (rather than just filter efficacy metrics), 'normal' saves what you "
134
135
  "would typically want around for interactive data exploration, "
135
136
  "'more' adds files that are sometimes useful, and 'max' outputs anything you might possibly want. "
136
- "Selecting 'max' will produce ~3x your input datafile size as output. "
137
+ "Selecting 'max' will produce ~3x your input datafile size as output. 'onlyregressors' will ONLY generate the "
138
+ "voxelwise timecourses to filter and save them without actually running the filter. "
137
139
  f'Default is "normal".'
138
140
  ),
139
141
  default="normal",
@@ -271,28 +273,39 @@ def retroregress(args):
271
273
  args.savenormalsLFOfiltfiles = False
272
274
  args.savemovingsignal = False
273
275
  args.saveallsLFOfiltfiles = False
276
+ args.saveEVsandquit = False
274
277
  elif args.outputlevel == "less":
275
278
  args.saveminimumsLFOfiltfiles = True
276
279
  args.savenormalsLFOfiltfiles = False
277
280
  args.savemovingsignal = False
278
281
  args.saveallsLFOfiltfiles = False
282
+ args.saveEVsandquit = False
279
283
  elif args.outputlevel == "normal":
280
284
  args.saveminimumsLFOfiltfiles = True
281
285
  args.savenormalsLFOfiltfiles = True
282
286
  args.savemovingsignal = False
283
287
  args.saveallsLFOfiltfiles = False
288
+ args.saveEVsandquit = False
284
289
  elif args.outputlevel == "more":
285
290
  args.saveminimumsLFOfiltfiles = True
286
291
  args.savenormalsLFOfiltfiles = True
287
292
  args.savemovingsignal = True
288
293
  args.saveallsLFOfiltfiles = False
294
+ args.saveEVsandquit = False
289
295
  elif args.outputlevel == "max":
290
296
  args.saveminimumsLFOfiltfiles = True
291
297
  args.savenormalsLFOfiltfiles = True
292
298
  args.savemovingsignal = True
293
299
  args.saveallsLFOfiltfiles = True
300
+ args.saveEVsandquit = False
301
+ elif args.outputlevel == "onlyregressors":
302
+ args.saveminimumsLFOfiltfiles = False
303
+ args.savenormalsLFOfiltfiles = False
304
+ args.savemovingsignal = False
305
+ args.saveallsLFOfiltfiles = False
306
+ args.saveEVsandquit = True
294
307
  else:
295
- print(f"illegal output level {args['outputlevel']}")
308
+ print(f"illegal output level {args.outputlevel}")
296
309
  sys.exit()
297
310
 
298
311
  # save the raw and formatted command lines
@@ -370,19 +383,19 @@ def retroregress(args):
370
383
 
371
384
  # read the fmri input files
372
385
  print("reading fmrifile")
373
- fmri_input, fmri_data, fmri_header, fmri_dims, fmri_sizes = tide_io.readfromnifti(
374
- args.fmrifile
375
- )
386
+ theinputdata = tide_voxelData.VoxelData(args.fmrifile)
387
+ xsize, ysize, numslices, timepoints = theinputdata.getdims()
388
+ xdim, ydim, slicethickness, fmritr = theinputdata.getsizes()
389
+ fmri_header = theinputdata.copyheader()
390
+ fmri_data = theinputdata.byvol()
391
+ numspatiallocs = theinputdata.numspatiallocs
376
392
 
377
393
  # create the canary file
378
394
  Path(f"{outputname}_RETROISRUNNING.txt").touch()
379
395
 
380
396
  if args.debug:
381
397
  print(f"{fmri_data.shape=}")
382
- xdim, ydim, slicedim, fmritr = tide_io.parseniftisizes(fmri_sizes)
383
- xsize, ysize, numslices, timepoints = tide_io.parseniftidims(fmri_dims)
384
- numspatiallocs = int(xsize) * int(ysize) * int(numslices)
385
- fmri_data_spacebytime = fmri_data.reshape((numspatiallocs, timepoints))
398
+ fmri_data_spacebytime = theinputdata.byvoxel()
386
399
  if args.debug:
387
400
  print(f"{fmri_data_spacebytime.shape=}")
388
401
 
@@ -584,9 +597,7 @@ def retroregress(args):
584
597
 
585
598
  if args.debug:
586
599
  # dump the fmri input file going to glm
587
- theheader = copy.deepcopy(fmri_header)
588
- theheader["dim"][4] = validtimepoints
589
- theheader["pixdim"][4] = fmritr
600
+ theheader = theinputdata.copyheader(numtimepoints=validtimepoints, tr=fmritr)
590
601
 
591
602
  maplist = [
592
603
  (
@@ -604,8 +615,7 @@ def retroregress(args):
604
615
  (xsize, ysize, numslices, validtimepoints),
605
616
  theheader,
606
617
  bidsbasedict,
607
- textio=therunoptions["textio"],
608
- fileiscifti=False,
618
+ filetype=theinputdata.filetype,
609
619
  rt_floattype=rt_floattype,
610
620
  cifti_hdr=None,
611
621
  )
@@ -618,7 +628,7 @@ def retroregress(args):
618
628
 
619
629
  if args.delayoffsetgausssigma < 0.0:
620
630
  # set gausssigma automatically
621
- args.delayoffsetgausssigma = np.mean([xdim, ydim, slicedim]) / 2.0
631
+ args.delayoffsetgausssigma = np.mean([xdim, ydim, slicethickness]) / 2.0
622
632
 
623
633
  TimingLGR.info("Refinement calibration start")
624
634
  regressderivratios, regressrvalues = tide_refinedelay.getderivratios(
@@ -652,11 +662,9 @@ def retroregress(args):
652
662
  regressderivratios,
653
663
  (xsize, ysize, numslices),
654
664
  validvoxels,
655
- (xdim, ydim, slicedim),
665
+ (xdim, ydim, slicethickness),
656
666
  gausssigma=args.delayoffsetgausssigma,
657
667
  patchthresh=args.delaypatchthresh,
658
- fileiscifti=False,
659
- textio=False,
660
668
  rt_floattype=rt_floattype,
661
669
  debug=args.debug,
662
670
  )
@@ -704,11 +712,9 @@ def retroregress(args):
704
712
  regressderivratios[i, :],
705
713
  (xsize, ysize, numslices),
706
714
  validvoxels,
707
- (xdim, ydim, slicedim),
715
+ (xdim, ydim, slicethickness),
708
716
  gausssigma=args.delayoffsetgausssigma,
709
717
  patchthresh=args.delaypatchthresh,
710
- fileiscifti=False,
711
- textio=False,
712
718
  rt_floattype=rt_floattype,
713
719
  debug=args.debug,
714
720
  )
@@ -725,16 +731,17 @@ def retroregress(args):
725
731
  )
726
732
  refinedvoxelstoreport = filteredregressderivratios.shape[1]
727
733
 
728
- namesuffix = "_desc-delayoffset_hist"
729
- tide_stats.makeandsavehistogram(
730
- delayoffset,
731
- therunoptions["histlen"],
732
- 1,
733
- outputname + namesuffix,
734
- displaytitle="Histogram of delay offsets calculated from GLM",
735
- dictvarname="delayoffsethist",
736
- thedict=None,
737
- )
734
+ if not args.saveEVsandquit:
735
+ namesuffix = "_desc-delayoffset_hist"
736
+ tide_stats.makeandsavehistogram(
737
+ delayoffset,
738
+ therunoptions["histlen"],
739
+ 1,
740
+ outputname + namesuffix,
741
+ displaytitle="Histogram of delay offsets calculated from GLM",
742
+ dictvarname="delayoffsethist",
743
+ thedict=None,
744
+ )
738
745
  lagtimesrefined_valid = lagtimes_valid + delayoffset
739
746
 
740
747
  TimingLGR.info(
@@ -782,248 +789,410 @@ def retroregress(args):
782
789
  nprocs_regressionfilt=args.nprocs,
783
790
  regressderivs=args.regressderivs,
784
791
  showprogressbar=args.showprogressbar,
792
+ saveEVsandquit=args.saveEVsandquit,
785
793
  debug=args.debug,
786
794
  )
787
- print(f"filtered {voxelsprocessed_regressionfilt} voxels")
788
- TimingLGR.info(
789
- "sLFO filtering done",
790
- {
791
- "message2": voxelsprocessed_regressionfilt,
792
- "message3": "voxels",
793
- },
794
- )
795
- # finalrawvariance = tide_math.imagevariance(filtereddata, None, 1.0 / fmritr)
796
- finalvariance = tide_math.imagevariance(filtereddata, theprefilter, 1.0 / fmritr)
797
795
 
798
- divlocs = np.where(finalvariance > 0.0)
799
- varchange = initialvariance * 0.0
800
- varchange[divlocs] = 100.0 * (finalvariance[divlocs] / initialvariance[divlocs] - 1.0)
796
+ if not args.saveEVsandquit:
797
+ print(f"filtered {voxelsprocessed_regressionfilt} voxels")
798
+ TimingLGR.info(
799
+ "sLFO filtering done",
800
+ {
801
+ "message2": voxelsprocessed_regressionfilt,
802
+ "message3": "voxels",
803
+ },
804
+ )
805
+ # finalrawvariance = tide_math.imagevariance(filtereddata, None, 1.0 / fmritr)
806
+ finalvariance = tide_math.imagevariance(filtereddata, theprefilter, 1.0 / fmritr)
807
+
808
+ divlocs = np.where(finalvariance > 0.0)
809
+ varchange = initialvariance * 0.0
810
+ varchange[divlocs] = 100.0 * (finalvariance[divlocs] / initialvariance[divlocs] - 1.0)
801
811
 
802
- """divlocs = np.where(finalrawvariance > 0.0)
803
- rawvarchange = initialrawvariance * 0.0
804
- rawvarchange[divlocs] = 100.0 * (finalrawvariance[divlocs] / initialrawvariance[divlocs] - 1.0)"""
812
+ """divlocs = np.where(finalrawvariance > 0.0)
813
+ rawvarchange = initialrawvariance * 0.0
814
+ rawvarchange[divlocs] = 100.0 * (finalrawvariance[divlocs] / initialrawvariance[divlocs] - 1.0)"""
805
815
 
806
- # save outputs
807
- TimingLGR.info("Starting output save")
808
- theheader = copy.deepcopy(lagtimes_header)
809
- if mode == "glm":
810
- maplist = [
811
- (
812
- initialvariance,
813
- "lfofilterInbandVarianceBefore",
814
- "map",
815
- None,
816
- "Inband variance prior to filtering",
817
- ),
818
- (
819
- finalvariance,
820
- "lfofilterInbandVarianceAfter",
821
- "map",
822
- None,
823
- "Inband variance after filtering",
824
- ),
825
- (
826
- varchange,
827
- "lfofilterInbandVarianceChange",
828
- "map",
829
- "percent",
830
- "Change in inband variance after filtering, in percent",
831
- ),
832
- # (
833
- # initialrawvariance,
834
- # "lfofilterTotalVarianceBefore",
835
- # "map",
836
- # None,
837
- # "Total variance prior to filtering",
838
- # ),
839
- # (
840
- # finalrawvariance,
841
- # "lfofilterTotalVarianceAfter",
842
- # "map",
843
- # None,
844
- # "Total variance after filtering",
845
- # ),
846
- # (
847
- # rawvarchange,
848
- # "lfofilterTotalVarianceChange",
849
- # "map",
850
- # "percent",
851
- # "Change in total variance after filtering, in percent",
852
- # ),
853
- ]
854
- if args.saveminimumsLFOfiltfiles:
855
- maplist += [
816
+ # save outputs
817
+ TimingLGR.info("Starting output save")
818
+ theheader = copy.deepcopy(lagtimes_header)
819
+ if mode == "glm":
820
+ maplist = [
856
821
  (
857
- r2value,
858
- "lfofilterR2",
822
+ initialvariance,
823
+ "lfofilterInbandVarianceBefore",
859
824
  "map",
860
825
  None,
861
- "Squared R value of the GLM fit (proportion of variance explained)",
826
+ "Inband variance prior to filtering",
862
827
  ),
828
+ (
829
+ finalvariance,
830
+ "lfofilterInbandVarianceAfter",
831
+ "map",
832
+ None,
833
+ "Inband variance after filtering",
834
+ ),
835
+ (
836
+ varchange,
837
+ "lfofilterInbandVarianceChange",
838
+ "map",
839
+ "percent",
840
+ "Change in inband variance after filtering, in percent",
841
+ ),
842
+ # (
843
+ # initialrawvariance,
844
+ # "lfofilterTotalVarianceBefore",
845
+ # "map",
846
+ # None,
847
+ # "Total variance prior to filtering",
848
+ # ),
849
+ # (
850
+ # finalrawvariance,
851
+ # "lfofilterTotalVarianceAfter",
852
+ # "map",
853
+ # None,
854
+ # "Total variance after filtering",
855
+ # ),
856
+ # (
857
+ # rawvarchange,
858
+ # "lfofilterTotalVarianceChange",
859
+ # "map",
860
+ # "percent",
861
+ # "Change in total variance after filtering, in percent",
862
+ # ),
863
863
  ]
864
- if args.savenormalsLFOfiltfiles:
865
- maplist += [
866
- (rvalue, "lfofilterR", "map", None, "R value of the GLM fit"),
867
- (sLFOfitmean, "lfofilterMean", "map", None, "Intercept from GLM fit"),
868
- ]
869
- else:
870
- maplist = [
871
- (initialvariance, "lfofilterInbandVarianceBefore", "map", None),
872
- (finalvariance, "lfofilterInbandVarianceAfter", "map", None),
873
- (varchange, "CVRVariance", "map", None),
874
- ]
875
- if args.savenormalsLFOfiltfiles:
876
- maplist += [
877
- (rvalue, "CVRR", "map", None),
878
- (r2value, "CVRR2", "map", None),
879
- (fitcoeff, "CVR", "map", "percent"),
880
- ]
881
- bidsdict = bidsbasedict.copy()
882
-
883
- if args.debug or args.focaldebug:
884
- maplist += [
885
- (
886
- lagtimes_valid,
887
- "maxtimeREAD",
888
- "map",
889
- "second",
890
- "Lag time in seconds used for calculation",
891
- ),
892
- (corrmask_valid, "corrfitREAD", "mask", None, "Correlation mask used for calculation"),
893
- (procmask_valid, "processedREAD", "mask", None, "Processed mask used for calculation"),
894
- ]
895
- if args.savenormalsLFOfiltfiles:
896
- if args.regressderivs > 0 or args.refinedelay:
897
- maplist += [
898
- (fitcoeff[:, 0], "lfofilterCoeff", "map", None, "Fit coefficient"),
899
- (fitNorm[:, 0], "lfofilterNorm", "map", None, "Normalized fit coefficient"),
900
- ]
901
- for thederiv in range(1, args.regressderivs + 1):
864
+ if args.saveminimumsLFOfiltfiles:
902
865
  maplist += [
903
866
  (
904
- fitcoeff[:, thederiv],
905
- f"lfofilterCoeffDeriv{thederiv}",
867
+ r2value,
868
+ "lfofilterR2",
906
869
  "map",
907
870
  None,
908
- f"Fit coefficient for temporal derivative {thederiv}",
909
- ),
910
- (
911
- fitNorm[:, thederiv],
912
- f"lfofilterNormDeriv{thederiv}",
913
- "map",
914
- None,
915
- f"Normalized fit coefficient for temporal derivative {thederiv}",
871
+ "Squared R value of the GLM fit (proportion of variance explained)",
916
872
  ),
917
873
  ]
874
+ if args.savenormalsLFOfiltfiles:
875
+ maplist += [
876
+ (rvalue, "lfofilterR", "map", None, "R value of the GLM fit"),
877
+ (sLFOfitmean, "lfofilterMean", "map", None, "Intercept from GLM fit"),
878
+ ]
918
879
  else:
880
+ maplist = [
881
+ (initialvariance, "lfofilterInbandVarianceBefore", "map", None),
882
+ (finalvariance, "lfofilterInbandVarianceAfter", "map", None),
883
+ (varchange, "CVRVariance", "map", None),
884
+ ]
885
+ if args.savenormalsLFOfiltfiles:
886
+ maplist += [
887
+ (rvalue, "CVRR", "map", None),
888
+ (r2value, "CVRR2", "map", None),
889
+ (fitcoeff, "CVR", "map", "percent"),
890
+ ]
891
+ bidsdict = bidsbasedict.copy()
892
+
893
+ if args.debug or args.focaldebug:
919
894
  maplist += [
920
- (fitcoeff, "lfofilterCoeff", "map", None, "Fit coefficient"),
921
- (fitNorm, "lfofilterNorm", "map", None, "Normalized fit coefficient"),
895
+ (
896
+ lagtimes_valid,
897
+ "maxtimeREAD",
898
+ "map",
899
+ "second",
900
+ "Lag time in seconds used for calculation",
901
+ ),
902
+ (corrmask_valid, "corrfitREAD", "mask", None, "Correlation mask used for calculation"),
903
+ (procmask_valid, "processedREAD", "mask", None, "Processed mask used for calculation"),
922
904
  ]
905
+ if args.savenormalsLFOfiltfiles:
906
+ if args.regressderivs > 0 or args.refinedelay:
907
+ maplist += [
908
+ (fitcoeff[:, 0], "lfofilterCoeff", "map", None, "Fit coefficient"),
909
+ (fitNorm[:, 0], "lfofilterNorm", "map", None, "Normalized fit coefficient"),
910
+ ]
911
+ for thederiv in range(1, args.regressderivs + 1):
912
+ maplist += [
913
+ (
914
+ fitcoeff[:, thederiv],
915
+ f"lfofilterCoeffDeriv{thederiv}",
916
+ "map",
917
+ None,
918
+ f"Fit coefficient for temporal derivative {thederiv}",
919
+ ),
920
+ (
921
+ fitNorm[:, thederiv],
922
+ f"lfofilterNormDeriv{thederiv}",
923
+ "map",
924
+ None,
925
+ f"Normalized fit coefficient for temporal derivative {thederiv}",
926
+ ),
927
+ ]
928
+ else:
929
+ maplist += [
930
+ (fitcoeff, "lfofilterCoeff", "map", None, "Fit coefficient"),
931
+ (fitNorm, "lfofilterNorm", "map", None, "Normalized fit coefficient"),
932
+ ]
923
933
 
924
- if args.refinedelay:
925
- if args.refineregressderivs > 1:
926
- for i in range(args.refineregressderivs):
934
+ if args.refinedelay:
935
+ if args.refineregressderivs > 1:
936
+ for i in range(args.refineregressderivs):
937
+ maplist += [
938
+ (
939
+ regressderivratios[i, :],
940
+ f"regressderivratios_{i}",
941
+ "map",
942
+ None,
943
+ f"Ratio of derivative {i+1} of delayed sLFO to the delayed sLFO",
944
+ ),
945
+ (
946
+ medfiltregressderivratios[i, :],
947
+ f"medfiltregressderivratios_{i}",
948
+ "map",
949
+ None,
950
+ f"Median filtered version of the regressderivratios_{i} map",
951
+ ),
952
+ (
953
+ filteredregressderivratios[i, :],
954
+ f"filteredregressderivratios_{i}",
955
+ "map",
956
+ None,
957
+ f"regressderivratios_{i}, with outliers patched using median filtered data",
958
+ ),
959
+ ]
960
+ else:
927
961
  maplist += [
928
962
  (
929
- regressderivratios[i, :],
930
- f"regressderivratios_{i}",
963
+ regressderivratios,
964
+ "regressderivratios",
931
965
  "map",
932
966
  None,
933
- f"Ratio of derivative {i+1} of delayed sLFO to the delayed sLFO",
967
+ "Ratio of the first derivative of delayed sLFO to the delayed sLFO",
934
968
  ),
935
969
  (
936
- medfiltregressderivratios[i, :],
937
- f"medfiltregressderivratios_{i}",
970
+ medfiltregressderivratios,
971
+ "medfiltregressderivratios",
938
972
  "map",
939
973
  None,
940
- f"Median filtered version of the regressderivratios_{i} map",
974
+ "Median filtered version of the regressderivratios map",
941
975
  ),
942
976
  (
943
- filteredregressderivratios[i, :],
944
- f"filteredregressderivratios_{i}",
977
+ filteredregressderivratios,
978
+ "filteredregressderivratios",
945
979
  "map",
946
980
  None,
947
- f"regressderivratios_{i}, with outliers patched using median filtered data",
981
+ "regressderivratios, with outliers patched using median filtered data",
948
982
  ),
949
983
  ]
950
- else:
951
984
  maplist += [
952
985
  (
953
- regressderivratios,
954
- "regressderivratios",
986
+ delayoffset,
987
+ "delayoffset",
955
988
  "map",
956
- None,
957
- "Ratio of the first derivative of delayed sLFO to the delayed sLFO",
989
+ "second",
990
+ "Delay offset correction from delay refinement",
958
991
  ),
959
992
  (
960
- medfiltregressderivratios,
961
- "medfiltregressderivratios",
993
+ lagtimesrefined_valid,
994
+ "maxtimerefined",
962
995
  "map",
963
- None,
964
- "Median filtered version of the regressderivratios map",
996
+ "second",
997
+ "Lag time in seconds, refined",
965
998
  ),
999
+ ]
1000
+
1001
+ # write the 3D maps
1002
+ tide_io.savemaplist(
1003
+ outputname,
1004
+ maplist,
1005
+ validvoxels,
1006
+ (xsize, ysize, numslices),
1007
+ theheader,
1008
+ bidsdict,
1009
+ debug=args.debug,
1010
+ )
1011
+
1012
+ # write the 4D maps
1013
+ theheader = theinputdata.copyheader()
1014
+ maplist = []
1015
+ if args.saveminimumsLFOfiltfiles:
1016
+ maplist = [
966
1017
  (
967
- filteredregressderivratios,
968
- "filteredregressderivratios",
969
- "map",
1018
+ filtereddata,
1019
+ "lfofilterCleaned",
1020
+ "bold",
970
1021
  None,
971
- "regressderivratios, with outliers patched using median filtered data",
1022
+ "fMRI data with sLFO signal filtered out",
972
1023
  ),
973
1024
  ]
974
- maplist += [
975
- (
976
- delayoffset,
977
- "delayoffset",
978
- "map",
979
- "second",
980
- "Delay offset correction from delay refinement",
981
- ),
982
- (
983
- lagtimesrefined_valid,
984
- "maxtimerefined",
985
- "map",
986
- "second",
987
- "Lag time in seconds, refined",
988
- ),
989
- ]
990
-
991
- # write the 3D maps
992
- tide_io.savemaplist(
993
- outputname,
994
- maplist,
995
- validvoxels,
996
- (xsize, ysize, numslices),
997
- theheader,
998
- bidsdict,
999
- debug=args.debug,
1000
- )
1025
+ if args.savemovingsignal:
1026
+ maplist += [
1027
+ (
1028
+ movingsignal,
1029
+ "lfofilterRemoved",
1030
+ "bold",
1031
+ None,
1032
+ "sLFO signal filtered out of this voxel",
1033
+ )
1034
+ ]
1001
1035
 
1002
- # write the 4D maps
1003
- theheader = copy.deepcopy(fmri_header)
1004
- maplist = []
1005
- if args.saveminimumsLFOfiltfiles:
1006
- maplist = [
1007
- (
1008
- filtereddata,
1009
- "lfofilterCleaned",
1010
- "bold",
1011
- None,
1012
- "fMRI data with sLFO signal filtered out",
1013
- ),
1014
- ]
1015
- if args.savemovingsignal:
1016
- maplist += [
1036
+ if args.saveallsLFOfiltfiles:
1037
+ if args.regressderivs > 0:
1038
+ if args.debug:
1039
+ print("going down the multiple EV path")
1040
+ print(f"{regressorset[:, :, 0].shape=}")
1041
+ maplist += [
1042
+ (
1043
+ regressorset[:, :, 0],
1044
+ "lfofilterEV",
1045
+ "bold",
1046
+ None,
1047
+ "Shifted sLFO regressor to filter",
1048
+ ),
1049
+ ]
1050
+ for thederiv in range(1, args.regressderivs + 1):
1051
+ if args.debug:
1052
+ print(f"{regressorset[:, :, thederiv].shape=}")
1053
+ maplist += [
1054
+ (
1055
+ regressorset[:, :, thederiv],
1056
+ f"lfofilterEVDeriv{thederiv}",
1057
+ "bold",
1058
+ None,
1059
+ f"Time derivative {thederiv} of shifted sLFO regressor",
1060
+ ),
1061
+ ]
1062
+ else:
1063
+ if args.debug:
1064
+ print("going down the single EV path")
1065
+ maplist += [
1066
+ (
1067
+ regressorset,
1068
+ "lfofilterEV",
1069
+ "bold",
1070
+ None,
1071
+ "Shifted sLFO regressor to filter",
1072
+ ),
1073
+ ]
1074
+ if args.makepseudofile:
1075
+ print("reading mean image")
1076
+ meanfile = f"{args.datafileroot}_desc-mean_map.nii.gz"
1017
1077
  (
1078
+ mean_input,
1079
+ mean,
1080
+ mean_header,
1081
+ mean_dims,
1082
+ mean_sizes,
1083
+ ) = tide_io.readfromnifti(meanfile)
1084
+ if not tide_io.checkspacematch(fmri_header, mean_header):
1085
+ raise ValueError("mean dimensions do not match fmri dimensions")
1086
+ if args.debug:
1087
+ print(f"{mean.shape=}")
1088
+ mean_spacebytime = mean.reshape((numspatiallocs))
1089
+ if args.debug:
1090
+ print(f"{mean_spacebytime.shape=}")
1091
+ pseudofile = mean_spacebytime[validvoxels, None] + movingsignal[:, :]
1092
+ maplist.append((pseudofile, "pseudofile", "bold", None, None))
1093
+ tide_io.savemaplist(
1094
+ outputname,
1095
+ maplist,
1096
+ validvoxels,
1097
+ (xsize, ysize, numslices, validtimepoints),
1098
+ theheader,
1099
+ bidsdict,
1100
+ debug=args.debug,
1101
+ )
1102
+ TimingLGR.info("Finishing output save")
1103
+
1104
+ if args.refinecorr:
1105
+ TimingLGR.info("Filtering for maxcorralt calculation start")
1106
+ for thevoxel in range(fmri_data_valid.shape[0]):
1107
+ fmri_data_valid[thevoxel, :] = theprefilter.apply(
1108
+ 1.0 / fmritr, fmri_data_valid[thevoxel, :]
1109
+ )
1110
+ TimingLGR.info("Filtering for maxcorralt calculation complete")
1111
+ TimingLGR.info("GLM for maxcorralt calculation start")
1112
+ voxelsprocessed_regressionfilt, regressorset, evset = tide_regressfrommaps.regressfrommaps(
1113
+ fmri_data_valid,
1114
+ validvoxels,
1115
+ initial_fmri_x,
1116
+ lagstouse_valid,
1117
+ corrmask_valid,
1118
+ genlagtc,
1119
+ mode,
1120
+ outputname,
1121
+ oversamptr,
1122
+ sLFOfitmean,
1123
+ rvalue,
1124
+ r2value,
1125
+ fitNorm[:, : args.regressderivs + 1],
1126
+ fitcoeff[:, : args.regressderivs + 1],
1018
1127
  movingsignal,
1019
- "lfofilterRemoved",
1020
- "bold",
1021
- None,
1022
- "sLFO signal filtered out of this voxel",
1128
+ lagtc,
1129
+ filtereddata,
1130
+ LGR,
1131
+ TimingLGR,
1132
+ threshval,
1133
+ args.saveminimumsLFOfiltfiles,
1134
+ nprocs_makelaggedtcs=args.nprocs,
1135
+ nprocs_regressionfilt=args.nprocs,
1136
+ regressderivs=args.regressderivs,
1137
+ showprogressbar=args.showprogressbar,
1138
+ debug=args.debug,
1139
+ )
1140
+ TimingLGR.info(
1141
+ "GLM for maxcorralt calculation done",
1142
+ {
1143
+ "message2": voxelsprocessed_regressionfilt,
1144
+ "message3": "voxels",
1145
+ },
1023
1146
  )
1024
- ]
1025
1147
 
1026
- if args.saveallsLFOfiltfiles:
1148
+ maplist = [
1149
+ (
1150
+ rvalue,
1151
+ "maxcorralt",
1152
+ "map",
1153
+ None,
1154
+ "R value for the lfo component of the delayed regressor, with sign",
1155
+ ),
1156
+ ]
1157
+ theheader = copy.deepcopy(lagtimes_header)
1158
+ tide_io.savemaplist(
1159
+ outputname,
1160
+ maplist,
1161
+ validvoxels,
1162
+ (xsize, ysize, numslices),
1163
+ theheader,
1164
+ bidsdict,
1165
+ debug=args.debug,
1166
+ )
1167
+ if args.debug:
1168
+ # dump the fmri input file going to glm
1169
+ theheader = theinputdata.copyheader(numtimepoints=validtimepoints, tr=fmritr)
1170
+
1171
+ maplist = [
1172
+ (
1173
+ fmri_data_valid,
1174
+ "prefilteredinputdata",
1175
+ "bold",
1176
+ None,
1177
+ "fMRI data after temporal filtering",
1178
+ ),
1179
+ ]
1180
+ tide_io.savemaplist(
1181
+ outputname,
1182
+ maplist,
1183
+ validvoxels,
1184
+ (xsize, ysize, numslices, validtimepoints),
1185
+ theheader,
1186
+ bidsbasedict,
1187
+ filetype=theinputdata.filetype,
1188
+ rt_floattype=rt_floattype,
1189
+ cifti_hdr=None,
1190
+ )
1191
+ else:
1192
+ # We are terminating early because we only want the regressors
1193
+ # write the EVs
1194
+ theheader = theinputdata.copyheader()
1195
+ maplist = []
1027
1196
  if args.regressderivs > 0:
1028
1197
  if args.debug:
1029
1198
  print("going down the multiple EV path")
@@ -1061,128 +1230,19 @@ def retroregress(args):
1061
1230
  "Shifted sLFO regressor to filter",
1062
1231
  ),
1063
1232
  ]
1064
- if args.makepseudofile:
1065
- print("reading mean image")
1066
- meanfile = f"{args.datafileroot}_desc-mean_map.nii.gz"
1067
- (
1068
- mean_input,
1069
- mean,
1070
- mean_header,
1071
- mean_dims,
1072
- mean_sizes,
1073
- ) = tide_io.readfromnifti(meanfile)
1074
- if not tide_io.checkspacematch(fmri_header, mean_header):
1075
- raise ValueError("mean dimensions do not match fmri dimensions")
1076
- if args.debug:
1077
- print(f"{mean.shape=}")
1078
- mean_spacebytime = mean.reshape((numspatiallocs))
1079
- if args.debug:
1080
- print(f"{mean_spacebytime.shape=}")
1081
- pseudofile = mean_spacebytime[validvoxels, None] + movingsignal[:, :]
1082
- maplist.append((pseudofile, "pseudofile", "bold", None, None))
1083
- tide_io.savemaplist(
1084
- outputname,
1085
- maplist,
1086
- validvoxels,
1087
- (xsize, ysize, numslices, validtimepoints),
1088
- theheader,
1089
- bidsdict,
1090
- debug=args.debug,
1091
- )
1092
- TimingLGR.info("Finishing output save")
1093
-
1094
- if args.refinecorr:
1095
- TimingLGR.info("Filtering for maxcorralt calculation start")
1096
- for thevoxel in range(fmri_data_valid.shape[0]):
1097
- fmri_data_valid[thevoxel, :] = theprefilter.apply(
1098
- 1.0 / fmritr, fmri_data_valid[thevoxel, :]
1099
- )
1100
- TimingLGR.info("Filtering for maxcorralt calculation complete")
1101
- TimingLGR.info("GLM for maxcorralt calculation start")
1102
- voxelsprocessed_regressionfilt, regressorset, evset = tide_regressfrommaps.regressfrommaps(
1103
- fmri_data_valid,
1104
- validvoxels,
1105
- initial_fmri_x,
1106
- lagstouse_valid,
1107
- corrmask_valid,
1108
- genlagtc,
1109
- mode,
1110
- outputname,
1111
- oversamptr,
1112
- sLFOfitmean,
1113
- rvalue,
1114
- r2value,
1115
- fitNorm[:, : args.regressderivs + 1],
1116
- fitcoeff[:, : args.regressderivs + 1],
1117
- movingsignal,
1118
- lagtc,
1119
- filtereddata,
1120
- LGR,
1121
- TimingLGR,
1122
- threshval,
1123
- args.saveminimumsLFOfiltfiles,
1124
- nprocs_makelaggedtcs=args.nprocs,
1125
- nprocs_regressionfilt=args.nprocs,
1126
- regressderivs=args.regressderivs,
1127
- showprogressbar=args.showprogressbar,
1128
- debug=args.debug,
1129
- )
1130
- TimingLGR.info(
1131
- "GLM for maxcorralt calculation done",
1132
- {
1133
- "message2": voxelsprocessed_regressionfilt,
1134
- "message3": "voxels",
1135
- },
1136
- )
1137
-
1138
- maplist = [
1139
- (
1140
- rvalue,
1141
- "maxcorralt",
1142
- "map",
1143
- None,
1144
- "R value for the lfo component of the delayed regressor, with sign",
1145
- ),
1146
- ]
1147
- theheader = copy.deepcopy(lagtimes_header)
1233
+ bidsdict = bidsbasedict.copy()
1148
1234
  tide_io.savemaplist(
1149
1235
  outputname,
1150
1236
  maplist,
1151
1237
  validvoxels,
1152
- (xsize, ysize, numslices),
1238
+ (xsize, ysize, numslices, validtimepoints),
1153
1239
  theheader,
1154
1240
  bidsdict,
1155
1241
  debug=args.debug,
1156
1242
  )
1157
- if args.debug:
1158
- # dump the fmri input file going to glm
1159
- theheader = copy.deepcopy(fmri_header)
1160
- theheader["dim"][4] = validtimepoints
1161
- theheader["pixdim"][4] = fmritr
1162
1243
 
1163
- maplist = [
1164
- (
1165
- fmri_data_valid,
1166
- "prefilteredinputdata",
1167
- "bold",
1168
- None,
1169
- "fMRI data after temporal filtering",
1170
- ),
1171
- ]
1172
- tide_io.savemaplist(
1173
- outputname,
1174
- maplist,
1175
- validvoxels,
1176
- (xsize, ysize, numslices, validtimepoints),
1177
- theheader,
1178
- bidsbasedict,
1179
- textio=therunoptions["textio"],
1180
- fileiscifti=False,
1181
- rt_floattype=rt_floattype,
1182
- cifti_hdr=None,
1183
- )
1184
1244
 
1185
- # read the runoptions file
1245
+ # write the runoptions file
1186
1246
  print("writing runoptions")
1187
1247
  if args.refinedelay:
1188
1248
  therunoptions["retroregress_delayoffsetMAD"] = delayoffsetMAD