def test_rapidtide2x_phase3(debug=False): recalculate = True if recalculate: # create outputdir if it doesn't exist create_dir(get_test_temp_path()) # trigger the usage function rapidtide2x_workflow.usage() # and launch the processing theargs = ["rapidtide2x"] theargs += [os.path.join(get_examples_path(), "fmri.nii.gz")] theargs += [ os.path.join(get_test_temp_path(), "rapidtide2x_phase3output") ] theargs += ["--nowindow"] theargs += ["--windowfunc=hamming"] theargs += ["--liang", "--eckart", "--phat"] theargs += ["--usesp"] theargs += ["--preservefiltering"] theargs += ["--corrmaskthresh=0.25"] theargs += ["-I", "-B", "-a", "-M", "-m"] theargs += ["-C", "-R", "-L", "-V", "-F", "0.01,0.08"] theargs += ["-v", "--debug"] theargs += ["--globalmaskmethod=mean"] theargs += ["--mklthreads=1"] theargs += ["--nosharedmem"] theargs += ["-S"] rapidtide2x_workflow.rapidtide_main(theargs) assert True
def test_rapidtide2x_phase3(debug=False): recalculate = True if recalculate: # create outputdir if it doesn't exist create_dir(get_test_temp_path()) # trigger the usage function rapidtide2x_workflow.usage() # and launch the processing theargs = ['rapidtide2x'] theargs += [os.path.join(get_examples_path(), 'fmri.nii.gz')] theargs += [ os.path.join(get_test_temp_path(), 'rapidtide2x_phase3output') ] theargs += ['--nowindow'] theargs += ['--windowfunc=hamming'] theargs += ['--liang', '--eckart', '--phat'] theargs += ['--usesp'] theargs += ['--preservefiltering'] theargs += ['--corrmaskthresh=0.25'] theargs += ['-I', '-B', '-a', '-M', '-m'] theargs += ['-C', '-R', '-L', '-V', '-F', '0.01,0.08'] theargs += ['-v', '--debug'] theargs += ['--globalmaskmethod=mean'] theargs += ['--mklthreads=1'] theargs += ['--nosharedmem'] theargs += ['-S'] rapidtide2x_workflow.rapidtide_main(theargs) assert True
def test_rapidtide2x(debug=False): recalculate = True if recalculate: # create outputdir if it doesn't exist create_dir(get_test_temp_path()) # and launch the processing theargs = ["rapidtide2x"] theargs += [os.path.join(get_examples_path(), "fmri.nii.gz")] theargs += [ os.path.join(get_test_temp_path(), "rapidtide2x_phase2output") ] theargs += [ "--regressor=" + os.path.join(get_test_data_path(), "rapidtide2x_phase1target_reference_fmrires.txt") ] theargs += ["--regressortstep=1.5"] theargs += ["--limitoutput"] theargs += ["-s", "25.0"] theargs += ["-L"] theargs += ["-r", "-20,20"] theargs += ["-f", "2"] theargs += ["--passes=3"] theargs += ["--refineoffset"] theargs += ["--despecklepasses=4"] theargs += ["--accheck"] theargs += ["--nprocs=2"] theargs += ["--saveoptionsasjson"] theargs += ["--detrendorder=3"] theargs += ["--pickleft"] theargs += [ "--corrmask=" + os.path.join(get_examples_path(), "quartermask.nii.gz") ] theargs += ["--noglm"] rapidtide2x_workflow.rapidtide_main(theargs) diffmaps = tide_util.comparerapidtideruns( os.path.join(get_test_temp_path(), "rapidtide2x_phase2output"), os.path.join(get_test_target_path(), "rapidtide2x_phase2target"), ) for mapname, maps in diffmaps.items(): print("checking", mapname) print("\trelmindiff", maps["relmindiff"]) print("\trelmaxdiff", maps["relmaxdiff"]) print("\trelmeandiff", maps["relmeandiff"]) print("\trelmse", maps["relmse"]) assert maps["relmindiff"] < 1e2 assert maps["relmaxdiff"] < 1e2 assert maps["relmeandiff"] < 1e-2 assert maps["relmse"] < 1e2
def test_stcorrelate(debug=False): tr = 0.72 testlen = 800 shiftdist = 5 windowtime = 30.0 stepsize = 5.0 corrweighting = "None" outfilename = op.join(get_test_temp_path(), "stcorrtest") # create outputdir if it doesn't exist create_dir(get_test_temp_path()) dodetrend = True timeaxis = np.arange(0.0, 1.0 * testlen) * tr testfilter = NoncausalFilter(filtertype="lfo") sig1 = testfilter.apply(1.0 / tr, np.random.random(testlen)) sig2 = np.float64(np.roll(sig1, int(shiftdist))) if debug: plt.figure() plt.plot(sig1) plt.plot(sig2) legend = ["Original", "Shifted"] plt.show() times, corrpertime, ppertime = shorttermcorr_1D(sig1, sig2, tr, windowtime, samplestep=int(stepsize // tr), detrendorder=0) plength = len(times) times, xcorrpertime, Rvals, delayvals, valid = shorttermcorr_2D( sig1, sig2, tr, windowtime, samplestep=int(stepsize // tr), weighting=corrweighting, detrendorder=0, display=False, ) xlength = len(times) writenpvecs(corrpertime, outfilename + "_pearson.txt") writenpvecs(ppertime, outfilename + "_pvalue.txt") writenpvecs(Rvals, outfilename + "_Rvalue.txt") writenpvecs(delayvals, outfilename + "_delay.txt") writenpvecs(valid, outfilename + "_mask.txt")
def test_fullrunrapidtide_v3(debug=False, display=False): # run rapidtide inputargs = [ os.path.join(get_examples_path(), "sub-RAPIDTIDETEST.nii.gz"), os.path.join(get_test_temp_path(), "sub-RAPIDTIDETEST"), "--corrmask", os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_restrictedmask.nii.gz:1"), "--legacyoutput", "--maxpasses", "2", "--numnull", "100", "--refinetype", "pca", "--convergencethresh", "0.5", "--noglm", "--nprocs", "-1", "--similaritymetric", "mutualinfo", "--dpoutput", "--spcalculation", "--globalsignalmethod", "pca", "--regressor", os.path.join( get_examples_path(), "sub-RAPIDTIDETEST_desc-oversampledmovingregressor_timeseries.json:pass3", ), ] rapidtide_workflow.rapidtide_main( rapidtide_parser.process_args(inputargs=inputargs))
def test_fullrunhappy_v2(debug=False, display=False): # run happy inputargs = [ os.path.join(get_examples_path(), "sub-HAPPYTEST.nii.gz"), os.path.join(get_examples_path(), "sub-HAPPYTEST.json"), os.path.join(get_test_temp_path(), "happyout"), "--estmask", os.path.join(get_examples_path(), "sub-HAPPYTEST_smallmask.nii.gz"), "--projmask", os.path.join(get_examples_path(), "sub-HAPPYTEST_smallmask.nii.gz"), "--mklthreads", "-1", "--model", "model_revised", "--fliparteries", "--temporalglm", "--cardiacfile", os.path.join( get_examples_path(), "sub-HAPPYTEST_desc-slicerescardfromfmri_timeseries.json:cardiacfromfmri_dlfiltered", ), "--increaseoutputlevel", "--increaseoutputlevel", ] happy_workflow.happy_main(happy_parser.process_args(inputargs=inputargs))
def test_rapidtide2x(debug=False): recalculate = True if recalculate: # create outputdir if it doesn't exist create_dir(get_test_temp_path()) # and launch the processing theargs = ['rapidtide2x'] theargs += [os.path.join(get_examples_path(), 'fmri.nii.gz')] theargs += [os.path.join(get_test_temp_path(), 'rapidtide2x_phase2output')] theargs += ['--regressor=' + os.path.join(get_test_data_path(), 'rapidtide2x_phase1target_reference_fmrires.txt')] theargs += ['--regressortstep=1.5'] theargs += ['--limitoutput'] theargs += ['-s', '25.0'] theargs += ['-L'] theargs += ['-r', '-20,20'] theargs += ['-f', '2'] theargs += ['--passes=3'] theargs += ['--refineoffset'] theargs += ['--despecklepasses=4'] theargs += ['--accheck'] theargs += ['--nprocs=2'] theargs += ['--saveoptionsasjson'] theargs += ['--detrendorder=3'] theargs += ['--pickleft'] theargs += ['--corrmask=' + os.path.join(get_examples_path(), 'quartermask.nii.gz')] theargs += ['--noglm'] rapidtide2x_workflow.rapidtide_main(theargs) diffmaps = tide_util.comparerapidtideruns(os.path.join(get_test_temp_path(), 'rapidtide2x_phase2output'), os.path.join(get_test_target_path(), 'rapidtide2x_phase2target')) for mapname, maps in diffmaps.items(): print('checking', mapname) print('\trelmindiff', maps['relmindiff']) print('\trelmaxdiff', maps['relmaxdiff']) print('\trelmeandiff', maps['relmeandiff']) print('\trelmse', maps['relmse']) assert maps['relmindiff'] < 1e2 assert maps['relmaxdiff'] < 1e2 assert maps['relmeandiff'] < 1e-2 assert maps['relmse'] < 1e2
def test_fullrunrapidtide_v5(debug=False, display=False): # run rapidtide inputargs = [ os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_cifti.ptseries.nii"), os.path.join(get_test_temp_path(), "sub-RAPIDTIDETEST"), "--nprocs", "-1", "--passes", "2", "--numnull", "0", ] rapidtide_workflow.rapidtide_main(rapidtide_parser.process_args(inputargs=inputargs))
def test_fullrunhappy_v1(debug=False, display=False): # run happy inputargs = [ os.path.join(get_examples_path(), "sub-HAPPYTEST.nii.gz"), os.path.join(get_examples_path(), "sub-HAPPYTEST.json"), os.path.join(get_test_temp_path(), "happyout"), "--mklthreads", "-1", "--spatialglm", "--model", "model_revised", "--aliasedcorrelation", ] happy_workflow.happy_main(happy_parser.process_args(inputargs=inputargs))
def test_happy_phase1(debug=False): recalculate = True if recalculate: # create outputdir if it doesn't exist create_dir(get_test_temp_path()) # and launch the processing theargs = ["happy"] theargs += [os.path.join(get_examples_path(), "happyfmri.nii.gz")] theargs += [os.path.join(get_examples_path(), "happyfmri.json")] theargs += [os.path.join(get_test_temp_path(), "happy_output")] theargs += ["--dodlfilter"] theargs += ["--saveinfoasjson"] theargs += ["--glm"] theargs += ["--numskip=0"] theargs += ["--gridbins=2.0"] theargs += ["--gridkernel=kaiser"] theargs += ["--model=model_revised"] print(" ".join(theargs)) happy_workflow.happy_main(theargs) diffmaps = tide_util.comparehappyruns( os.path.join(get_test_temp_path(), "happy_output"), os.path.join(get_test_target_path(), "happy_target"), debug=debug, ) for mapname, maps in diffmaps.items(): print("checking", mapname) print("\trelmindiff", maps["relmindiff"]) print("\trelmaxdiff", maps["relmaxdiff"]) print("\trelmeandiff", maps["relmeandiff"]) print("\trelmse", maps["relmse"]) assert maps["relmindiff"] < 1e2 assert maps["relmaxdiff"] < 1e2 assert maps["relmeandiff"] < 1e-2 assert maps["relmse"] < 1e2
def test_fullrunrapidtide_v1(debug=False, display=False): # run rapidtide inputargs = [ os.path.join(get_examples_path(), "sub-RAPIDTIDETEST.nii.gz"), os.path.join(get_test_temp_path(), "sub-RAPIDTIDETEST"), "--spatialfilt", "2", "--nprocs", "-1", "--passes", "1", "--despecklepasses", "3", ] rapidtide_workflow.rapidtide_main( rapidtide_parser.process_args(inputargs=inputargs))
def test_fullrunhappy_v4(debug=False, display=False): # run happy inputargs = [ os.path.join(get_examples_path(), "sub-HAPPYTEST.nii.gz"), os.path.join(get_examples_path(), "sub-HAPPYTEST.json"), os.path.join(get_test_temp_path(), "happyout"), "--estmask", os.path.join(get_examples_path(), "sub-HAPPYTEST_smallmask.nii.gz"), "--projmask", os.path.join(get_examples_path(), "sub-HAPPYTEST_smallmask.nii.gz"), "--legacyoutput", "--mklthreads", "-1", "--model", "model_revised", ] happy_workflow.happy_main(happy_parser.process_args(inputargs=inputargs))
def test_fullrunrapidtide_v4(debug=False, display=False): # run rapidtide inputargs = [ os.path.join(get_examples_path(), "sub-NIRSRAPIDTIDETEST.txt"), os.path.join(get_test_temp_path(), "sub-NIRSRAPIDTIDETEST"), "--globalmeaninclude", os.path.join(get_examples_path(), "sub-NIRSRAPIDTIDETEST_mask.txt"), "--nirs", "--datatstep", "0.2560", "--globalmaskmethod", "variance", "--despecklepasses", "0", "--numnull", "1000", "--respdelete", "--echocancel", ] rapidtide_workflow.rapidtide_main( rapidtide_parser.process_args(inputargs=inputargs))
def test_fullrunrapidtide_v2(debug=False, display=False): # run rapidtide inputargs = [ os.path.join(get_examples_path(), "sub-RAPIDTIDETEST.nii.gz"), os.path.join(get_test_temp_path(), "sub-RAPIDTIDETEST"), "--tmask", os.path.join(get_examples_path(), "tmask3.txt"), "--corrmask", os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_restrictedmask.nii.gz"), "--globalmeaninclude", os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_brainmask.nii.gz"), "--globalmeanexclude", os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_nullmask.nii.gz"), "--refineinclude", os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_brainmask.nii.gz"), "--refineexclude", os.path.join(get_examples_path(), "sub-RAPIDTIDETEST_nullmask.nii.gz"), "--spatialfilt", "-1", "--savelags", "--checkpoint", "--saveintermediatemaps", "--nolimitoutput", "--calccoherence", "--nprocs", "1", "--passes", "2", "--numnull", "0", "--similaritymetric", "hybrid", "--globalsignalmethod", "meanscale", ] rapidtide_workflow.rapidtide_main( rapidtide_parser.process_args(inputargs=inputargs))
def test_nullsimfunc(debug=False, display=False): # make the lfo filter lfofilter = tide_filt.NoncausalFilter(filtertype="lfo") # make the starting regressor timestep = 1.5 Fs = 1.0 / timestep # sourcelen = 1200 # sourcedata = lfofilter.apply(Fs, np.random.rand(sourcelen)) sourcedata = tide_io.readvecs( os.path.join(get_test_data_path(), "fmri_globalmean.txt"))[0] sourcelen = len(sourcedata) numpasses = 1 if display: plt.figure() plt.plot(sourcedata) plt.show() thexcorr = tide_corr.fastcorrelate(sourcedata, sourcedata) xcorrlen = len(thexcorr) xcorr_x = ( np.linspace(0.0, xcorrlen, xcorrlen, endpoint=False) * timestep - (xcorrlen * timestep) / 2.0 + timestep / 2.0) if display: plt.figure() plt.plot(xcorr_x, thexcorr) plt.show() corrzero = xcorrlen // 2 lagmin = -10 lagmax = 10 lagmininpts = int((-lagmin / timestep) - 0.5) lagmaxinpts = int((lagmax / timestep) + 0.5) searchstart = int(np.round(corrzero + lagmin / timestep)) searchend = int(np.round(corrzero + lagmax / timestep)) optiondict = { "numestreps": 10000, "showprogressbar": debug, "detrendorder": 3, "windowfunc": "hamming", "corrweighting": "None", "nprocs": 1, "widthlimit": 1000.0, "bipolar": False, "fixdelay": False, "peakfittype": "gauss", "lagmin": lagmin, "lagmax": lagmax, "absminsigma": 0.25, "absmaxsigma": 25.0, "edgebufferfrac": 0.0, "lthreshval": 0.0, "uthreshval": 1.0, "debug": False, "enforcethresh": True, "lagmod": 1000.0, "searchfrac": 0.5, "permutationmethod": "shuffle", "hardlimit": True, } theprefilter = tide_filt.NoncausalFilter("lfo") theCorrelator = tide_classes.Correlator( Fs=Fs, ncprefilter=theprefilter, detrendorder=optiondict["detrendorder"], windowfunc=optiondict["windowfunc"], corrweighting=optiondict["corrweighting"], ) thefitter = tide_classes.SimilarityFunctionFitter( lagmod=optiondict["lagmod"], lthreshval=optiondict["lthreshval"], uthreshval=optiondict["uthreshval"], bipolar=optiondict["bipolar"], lagmin=optiondict["lagmin"], lagmax=optiondict["lagmax"], absmaxsigma=optiondict["absmaxsigma"], absminsigma=optiondict["absminsigma"], debug=optiondict["debug"], peakfittype=optiondict["peakfittype"], searchfrac=optiondict["searchfrac"], enforcethresh=optiondict["enforcethresh"], hardlimit=optiondict["hardlimit"], ) if debug: print(optiondict) theCorrelator.setlimits(lagmininpts, lagmaxinpts) theCorrelator.setreftc(sourcedata) dummy, trimmedcorrscale, dummy = theCorrelator.getfunction() thefitter.setcorrtimeaxis(trimmedcorrscale) histograms = [] for thenprocs in [1, -1]: for i in range(numpasses): corrlist = tide_nullsimfunc.getNullDistributionDatax( sourcedata, Fs, theCorrelator, thefitter, despeckle_thresh=5.0, fixdelay=False, fixeddelayvalue=0.0, numestreps=optiondict["numestreps"], nprocs=thenprocs, showprogressbar=optiondict["showprogressbar"], chunksize=1000, permutationmethod=optiondict["permutationmethod"], ) tide_io.writenpvecs( corrlist, os.path.join(get_test_temp_path(), "corrdistdata.txt")) # calculate percentiles for the crosscorrelation from the distribution data histlen = 250 thepercentiles = [0.95, 0.99, 0.995] pcts, pcts_fit, histfit = tide_stats.sigFromDistributionData( corrlist, histlen, thepercentiles) if debug: tide_stats.printthresholds( pcts, thepercentiles, "Crosscorrelation significance thresholds from data:", ) tide_stats.printthresholds( pcts_fit, thepercentiles, "Crosscorrelation significance thresholds from fit:", ) ( thehist, peakheight, peakloc, peakwidth, centerofmass, ) = tide_stats.makehistogram(np.abs(corrlist), histlen, therange=[0.0, 1.0]) histograms.append(thehist) thestore = np.zeros((2, len(thehist[0])), dtype="float64") thestore[0, :] = (thehist[1][1:] + thehist[1][0:-1]) / 2.0 thestore[1, :] = thehist[0][-histlen:] if display: plt.figure() plt.plot(thestore[0, :], thestore[1, :]) plt.show() # tide_stats.makeandsavehistogram(corrlist, histlen, 0, # os.path.join(get_test_temp_path(), 'correlationhist'), # displaytitle='Null correlation histogram', # displayplots=display, refine=False) assert True
def test_io(debug=True, display=False): # create outputdir if it doesn't exist create_dir(get_test_temp_path()) # test checkifnifti assert tide_io.checkifnifti("test.nii") == True assert tide_io.checkifnifti("test.nii.gz") == True assert tide_io.checkifnifti("test.txt") == False # test checkiftext assert tide_io.checkiftext("test.nii") == False assert tide_io.checkiftext("test.nii.gz") == False assert tide_io.checkiftext("test.txt") == True # test getniftiroot assert tide_io.getniftiroot("test.nii") == "test" assert tide_io.getniftiroot("test.nii.gz") == "test" assert tide_io.getniftiroot("test.txt") == "test.txt" # test fmritimeinfo fmritimeinfothresh = 1e-2 tr, timepoints = tide_io.fmritimeinfo( os.path.join(get_examples_path(), "sub-HAPPYTEST.nii.gz")) assert np.fabs(tr - 1.16) < fmritimeinfothresh assert timepoints == 110 tr, timepoints = tide_io.fmritimeinfo( os.path.join(get_examples_path(), "sub-RAPIDTIDETEST.nii.gz")) assert np.fabs(tr - 1.5) < fmritimeinfothresh assert timepoints == 260 # test niftifile reading sizethresh = 1e-3 happy_img, happy_data, happy_hdr, happydims, happysizes = tide_io.readfromnifti( os.path.join(get_examples_path(), "sub-HAPPYTEST.nii.gz")) fmri_img, fmri_data, fmri_hdr, fmridims, fmrisizes = tide_io.readfromnifti( os.path.join(get_examples_path(), "sub-RAPIDTIDETEST.nii.gz")) targetdims = [4, 65, 89, 64, 110, 1, 1, 1] targetsizes = [-1.00, 2.39583, 2.395830, 2.4, 1.16, 0.00, 0.00, 0.00] if debug: print("happydims:", happydims) print("targetdims:", targetdims) print("happysizes:", happysizes) print("targetsizes:", targetsizes) for i in range(len(targetdims)): assert targetdims[i] == happydims[i] assert mse(np.array(targetsizes), np.array(happysizes)) < sizethresh # test file writing datathresh = 2e-3 # relaxed threshold because sub-RAPIDTIDETEST has been converted to INT16 tide_io.savetonifti( fmri_data, fmri_hdr, os.path.join(get_test_temp_path(), "sub-RAPIDTIDETEST_copy.nii.gz")) ( fmricopy_img, fmricopy_data, fmricopy_hdr, fmricopydims, fmricopysizes, ) = tide_io.readfromnifti( os.path.join(get_test_temp_path(), "sub-RAPIDTIDETEST_copy.nii.gz")) assert tide_io.checkspacematch(fmri_hdr, fmricopy_hdr) assert tide_io.checktimematch(fmridims, fmridims) assert mse(fmri_data, fmricopy_data) < datathresh # test file header comparisons assert tide_io.checkspacematch(happy_hdr, happy_hdr) assert not tide_io.checkspacematch(happy_hdr, fmri_hdr) assert tide_io.checktimematch(happydims, happydims) assert not tide_io.checktimematch(happydims, fmridims)