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_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_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_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_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_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_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_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_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_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_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_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)
def test_findmaxlag(display=False, fittype="gauss", debug=False): textfilename = op.join(get_examples_path(), "lt_rt.txt") # set default variable values searchfrac = 0.75 indata = tide_io.readvecs(textfilename, debug=debug) xvecs = indata[0, :] yvecs = indata[1, :] # set some fit parameters lagmin = -20.0 lagmax = 20.0 widthlimit = 1000.0 absmaxsigma = 1000.0 absminsigma = 0.1 absmaxval = 1.0 absminval = 0.0 # test over the lag range testmaxval = 0.8 testmaxsigma = 5.0 testlags = np.linspace(-25.0, 25.0, 50, endpoint=True) testsigmas = np.full((len(testlags)), testmaxsigma, dtype=np.float64) testvals = np.full((len(testlags)), testmaxval, dtype=np.float64) fml_maxlags = np.zeros(len(testlags), dtype=np.float64) fml_maxsigmas = np.zeros(len(testlags), dtype=np.float64) fml_maxvals = np.zeros(len(testlags), dtype=np.float64) fml_lfailreasons = np.zeros(len(testlags), dtype=np.uint16) fmlc_maxlags = np.zeros(len(testlags), dtype=np.float64) fmlc_maxsigmas = np.zeros(len(testlags), dtype=np.float64) fmlc_maxvals = np.zeros(len(testlags), dtype=np.float64) fmlc_lfailreasons = np.zeros(len(testlags), dtype=np.uint16) # initialize the correlation fitter thefitter = tide_classes.SimilarityFunctionFitter( corrtimeaxis=xvecs, lagmin=lagmin, lagmax=lagmax, absmaxsigma=absmaxsigma, absminsigma=absminsigma, peakfittype=fittype, debug=debug, searchfrac=searchfrac, zerooutbadfit=False, ) for i in range(len(testlags)): yvecs = tide_fit.gauss_eval( xvecs, np.array([testvals[i], testlags[i], testsigmas[i]])) print() print() print() ( maxindex, fml_maxlags[i], fml_maxvals[i], fml_maxsigmas[i], maskval, fml_lfailreasons[i], peakstart, peakend, ) = tide_fit.findmaxlag_gauss( xvecs, yvecs, lagmin, lagmax, widthlimit, tweaklims=False, refine=True, debug=debug, searchfrac=searchfrac, absmaxsigma=absmaxsigma, absminsigma=absminsigma, zerooutbadfit=False, ) print() print() print() ( maxindexc, fmlc_maxlags[i], fmlc_maxvals[i], fmlc_maxsigmas[i], maskvalc, fmlc_lfailreasons[i], peakstartc, peakendc, ) = thefitter.fit(yvecs) print( maxindexc, fmlc_maxlags[i], fmlc_maxvals[i], fmlc_maxsigmas[i], maskvalc, fmlc_lfailreasons[i], peakstartc, peakendc, ) if debug: print("findmaxlag_gauss results over lag range") for i in range(len(testlags)): print(testlags[i], fml_maxlags[i], fml_lfailreasons[i]) assert eval_fml_result(lagmin, lagmax, testlags, fml_maxlags, fml_lfailreasons) assert eval_fml_result(absminval, absmaxval, testvals, fml_maxvals, fml_lfailreasons) assert eval_fml_result(absminsigma, absmaxsigma, testsigmas, fml_maxsigmas, fml_lfailreasons) assert eval_fml_result(lagmin, lagmax, testlags, fmlc_maxlags, fmlc_lfailreasons) assert eval_fml_result(absminval, absmaxval, testvals, fmlc_maxvals, fmlc_lfailreasons) assert eval_fml_result(absminsigma, absmaxsigma, testsigmas, fmlc_maxsigmas, fmlc_lfailreasons) if display: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.plot(testlags, fml_maxlags, "r") ax.plot(testlags, fmlc_maxlags, "b") ax.legend(["findmaxlag_gauss", "classes"]) plt.show() # now test over range of sigmas testlag = 5.0 testsigmas = np.asarray([ 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, ]) testlags = np.full((len(testsigmas)), testlag, dtype=np.float64) testvals = np.full((len(testsigmas)), testmaxval, dtype=np.float64) fml_maxlags = np.zeros(len(testsigmas), dtype=np.float64) fml_maxsigmas = np.zeros(len(testsigmas), dtype=np.float64) fml_maxvals = np.zeros(len(testsigmas), dtype=np.float64) fml_wfailreasons = np.zeros(len(testsigmas), dtype=np.uint16) fmlc_maxlags = np.zeros(len(testsigmas), dtype=np.float64) fmlc_maxsigmas = np.zeros(len(testsigmas), dtype=np.float64) fmlc_maxvals = np.zeros(len(testsigmas), dtype=np.float64) fmlc_wfailreasons = np.zeros(len(testsigmas), dtype=np.uint16) peakstartc = np.zeros(len(testsigmas), dtype=np.int32) peakendc = np.zeros(len(testsigmas), dtype=np.int32) for i in range(len(testsigmas)): yvecs = tide_fit.gauss_eval( xvecs, np.array([testvals[i], testlags[i], testsigmas[i]])) print() print() print() ( maxindex, fml_maxlags[i], fml_maxvals[i], fml_maxsigmas[i], maskval, fml_wfailreasons[i], peakstart, peakend, ) = tide_fit.findmaxlag_gauss( xvecs, yvecs, lagmin, lagmax, widthlimit, tweaklims=False, refine=True, debug=debug, searchfrac=searchfrac, absmaxsigma=absmaxsigma, absminsigma=absminsigma, zerooutbadfit=False, ) print() print() print() ( maxindexc, fmlc_maxlags[i], fmlc_maxvals[i], fmlc_maxsigmas[i], maskvalc, fmlc_wfailreasons[i], peakstartc[i], peakendc[i], ) = thefitter.fit(yvecs) print( maxindexc, fmlc_maxlags[i], fmlc_maxvals[i], fmlc_maxsigmas[i], maskvalc, fmlc_wfailreasons[i], peakstartc[i], peakendc[i], ) if debug: print("findmaxlag_gauss results over sigma range") for i in range(len(testsigmas)): print( testsigmas[i], fml_maxsigmas[i], fmlc_maxlags[i], fmlc_maxvals[i], fml_wfailreasons[i], ) print("\nfitter class results over lag range") for i in range(len(testsigmas)): print( testsigmas[i], fmlc_maxsigmas[i], fmlc_maxlags[i], fmlc_maxvals[i], peakstartc[i], peakendc[i], fmlc_wfailreasons[i], thefitter.diagnosefail(np.uint32(fmlc_wfailreasons[i])), ) if display: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.loglog(testsigmas, fml_maxsigmas, "r") ax.loglog(testsigmas, fmlc_maxsigmas, "b") ax.legend(["findmaxlag_gauss", "classes"]) plt.show() assert eval_fml_result(lagmin, lagmax, testlags, fml_maxlags, fml_wfailreasons) # assert eval_fml_result(absminval, absmaxval, testvals, fml_maxvals, fml_wfailreasons) assert eval_fml_result(absminsigma, absmaxsigma, testsigmas, fml_maxsigmas, fml_wfailreasons) assert eval_fml_result(lagmin, lagmax, testlags, fmlc_maxlags, fmlc_wfailreasons) assert eval_fml_result(absminval, absmaxval, testvals, fmlc_maxvals, fmlc_wfailreasons) assert eval_fml_result(absminsigma, absmaxsigma, testsigmas, fmlc_maxsigmas, fmlc_wfailreasons)