def eval_phaseanalysis(phasestep=0.01, amplitude=1.0, numpoints=100, display=False): # read in some data phases = np.linspace(0.0, numpoints * phasestep, num=numpoints, endpoint=False) testwaveform = amplitude * np.cos(phases) if display: plt.figure() plt.plot(phases) plt.show() plt.figure() plt.plot(testwaveform) plt.show() # now calculate the phase waveform instantaneous_phase, amplitude_envelope = tide_fit.phaseanalysis( testwaveform) filtered_phase = tide_math.trendfilt(instantaneous_phase, order=3, ndevs=2.0) initialphase = instantaneous_phase[0] if display: plt.figure() plt.plot(instantaneous_phase) plt.plot(filtered_phase) plt.plot(phases) plt.show() return mse(phases, instantaneous_phase), mse(phases, filtered_phase)
def test_timeshift(debug=False): tr = 1.0 padvalue = 300.0 testlen = 1000 shiftdist = 30 timeaxis = np.arange(0.0, 1.0 * testlen) * tr #timecoursein = np.zeros((testlen), dtype='float64') timecoursein = np.float64(timeaxis * 0.0) midpoint = int(testlen // 2) + 1 timecoursein[midpoint - 1] = np.float64(1.0) timecoursein[midpoint] = np.float64(1.0) timecoursein[midpoint + 1] = np.float64(1.0) timecoursein -= 0.5 butterorder = 4 timecoursein = 0.5 * dolpfiltfilt(1.0, 0.25, timecoursein, butterorder) + 0.5 shiftlist = [-30, -20, -10, 0, 10, 20, 30] shiftlist = [-100] if debug: plt.figure() plt.ylim([-1.0, 2.0 * len(shiftlist) + 1.0]) plt.plot(timecoursein) legend = ['Original'] offset = 0.0 for shiftdist in shiftlist: # generate the ground truth rolled regressor tcrolled = np.float64(np.roll(timecoursein, int(shiftdist))) # generate the fast resampled regressor #tcshifted = genlaggedtc.yfromx(timeaxis - shiftdist, debug=debug) tcshifted, weights, alltc, allweights = timeshift(timecoursein, 1.0 * shiftdist, int(padvalue // tr), doplot=False) # print out all elements print("len tcrolled=", len(tcrolled), "len tcshifted=", len(tcshifted)) for i in range(0, len(tcrolled)): print(i, tcrolled[i], tcshifted[i], tcshifted[i] - tcrolled[i]) # plot if we are doing that if debug: offset += 1.0 plt.plot(tcrolled + offset) legend.append('Roll ' + str(shiftdist)) offset += 1.0 plt.plot(tcshifted + offset) legend.append('Timeshift ' + str(shiftdist)) plt.plot(weights + offset) legend.append('Weights ' + str(shiftdist)) # do the tests msethresh = 1e-6 aethresh = 2 assert mse(tcrolled, tcshifted) < msethresh np.testing.assert_almost_equal(tcrolled, tcshifted, aethresh) if debug: plt.legend(legend) plt.show()
def test_fastresampler(debug=False): tr = 1.0 padvalue = 30.0 testlen = 1000 shiftdist = 30 timeaxis = np.arange(0.0, 1.0 * testlen) * tr #timecoursein = np.zeros((testlen), dtype='float64') timecoursein = np.float64(timeaxis * 0.0) midpoint = int(testlen // 2) + 1 timecoursein[midpoint - 1] = np.float64(1.0) timecoursein[midpoint] = np.float64(1.0) timecoursein[midpoint + 1] = np.float64(1.0) timecoursein -= 0.5 shiftlist = [-30, -20, -10, 0, 10, 20, 30] # generate the fast resampled regressor genlaggedtc = fastresampler(timeaxis, timecoursein, padvalue=padvalue) if debug: plt.figure() plt.ylim([-1.0, 2.0 * len(shiftlist) + 1.0]) plt.hold(True) plt.plot(timecoursein) legend = ['Original'] offset = 0.0 for shiftdist in shiftlist: # generate the ground truth rolled regressor tcrolled = np.float64(np.roll(timecoursein, shiftdist)) # generate the fast resampled regressor tcshifted = genlaggedtc.yfromx(timeaxis - shiftdist, debug=debug) tcshifted = doresample(timeaxis, timecoursein, timeaxis - shiftdist, method='univariate') # print out all elements for i in range(0, len(tcrolled)): print(i, tcrolled[i], tcshifted[i], tcshifted[i] - tcrolled[i]) # plot if we are doing that if debug: offset += 1.0 plt.plot(tcrolled + offset) legend.append('Roll ' + str(shiftdist)) offset += 1.0 plt.plot(tcshifted + offset) legend.append('Fastresampler ' + str(shiftdist)) # do the tests msethresh = 1e-6 aethresh = 2 assert mse(tcrolled, tcshifted) < msethresh np.testing.assert_almost_equal(tcrolled, tcshifted, aethresh) if debug: plt.legend(legend) plt.show()
def test_doresample(debug=False): tr = 1.0 padtime = 30.0 padlen = int(padtime // tr) testlen = 1000 shiftdist = 30 timeaxis = np.arange(0.0, 1.0 * testlen) * tr # timecoursein = np.zeros((testlen), dtype='float64') timecoursein = np.float64(timeaxis * 0.0) midpoint = int(testlen // 2) + 1 timecoursein[midpoint - 1] = np.float64(1.0) timecoursein[midpoint] = np.float64(1.0) timecoursein[midpoint + 1] = np.float64(1.0) timecoursein -= 0.5 shiftlist = [-30, -20, -10, 0, 10, 20, 30] if debug: plt.figure() plt.ylim([-1.0, 2.0 * len(shiftlist) + 1.0]) plt.plot(timecoursein) legend = ["Original"] offset = 0.0 for shiftdist in shiftlist: # generate the ground truth rolled regressor tcrolled = np.float64(np.roll(timecoursein, shiftdist)) # generate the fast resampled regressor tcshifted = doresample( timeaxis, timecoursein, timeaxis - shiftdist, method="univariate", padlen=padlen, ) # print out all elements for i in range(0, len(tcrolled)): # print(i, tcrolled[i], tcshifted[i], tcshifted[i] - tcrolled[i]) pass # plot if we are doing that if debug: offset += 1.0 plt.plot(tcrolled + offset) legend.append("Roll " + str(shiftdist)) offset += 1.0 plt.plot(tcshifted + offset) legend.append("doresample " + str(shiftdist)) # do the tests msethresh = 1e-6 aethresh = 2 assert mse(tcrolled, tcshifted) < msethresh np.testing.assert_almost_equal(tcrolled, tcshifted, aethresh) if debug: plt.legend(legend) plt.show()
def test_FastResampler(debug=False): tr = 1.0 padtime = 50.0 testlen = 1000 timeaxis = np.arange(0.0, 1.0 * testlen) * tr timecoursein = np.float64(timeaxis * 0.0) midpoint = int(testlen // 2) + 1 timecoursein[midpoint - 1] = np.float64(1.0) timecoursein[midpoint] = np.float64(1.0) timecoursein[midpoint + 1] = np.float64(1.0) timecoursein -= 0.5 shiftlist = [-40, -30, -20, -10, 0, 10, 20, 30, 40] # generate the fast resampled regressor genlaggedtc = FastResampler(timeaxis, timecoursein, padtime=padtime) if debug: plt.figure() plt.ylim([-1.0, 2.0 * len(shiftlist) + 1.0]) plt.plot(timecoursein) legend = ["Original"] offset = 0.0 for shiftdist in shiftlist: # generate the ground truth rolled regressor tcrolled = np.float64(np.roll(timecoursein, shiftdist)) # generate the fast resampled regressor tcshifted = genlaggedtc.yfromx(timeaxis - shiftdist, debug=debug) # print out all elements for i in range(0, len(tcrolled)): print(i, tcrolled[i], tcshifted[i], tcshifted[i] - tcrolled[i]) # plot if we are doing that if debug: offset += 1.0 plt.plot(tcrolled + offset) legend.append("Roll " + str(shiftdist)) offset += 1.0 plt.plot(tcshifted + offset) legend.append("Fastresampler " + str(shiftdist)) # do the tests msethresh = 1e-6 aethresh = 2 assert mse(tcrolled, tcshifted) < msethresh np.testing.assert_almost_equal(tcrolled, tcshifted, aethresh) if debug: plt.legend(legend) plt.show()
def test_simulate(display=False): fmritr = 1.5 numtrs = 260 fmriskip = 0 oversampfac = 10 inputfreq = oversampfac / fmritr inputstarttime = 0.0 timecourse = np.zeros((oversampfac * numtrs), dtype="float") timecourse[500:600] = 1.0 timecourse[700:750] = 1.0 # read in the timecourse to resample inputvec = tide_math.stdnormalize(timecourse) simregressorpts = len(inputvec) # prepare the input data for interpolation print("Input regressor has ", simregressorpts, " points") inputstep = 1.0 / inputfreq nirs_x = np.r_[0.0 : 1.0 * simregressorpts] * inputstep - inputstarttime nirs_y = inputvec[0:simregressorpts] print("nirs regressor runs from ", nirs_x[0], " to ", nirs_x[-1]) # prepare the output timepoints fmrifreq = 1.0 / fmritr initial_fmri_x = np.r_[0 : fmritr * (numtrs - fmriskip) : fmritr] + fmritr * fmriskip print("length of fmri after removing skip:", len(initial_fmri_x)) print("fmri time runs from ", initial_fmri_x[0], " to ", initial_fmri_x[-1]) # set the sim parameters immean = 1.0 boldpc = 1.0 lag = 10.0 * fmritr noiselevel = 0.0 simdata = np.zeros((len(initial_fmri_x)), dtype="float") fmrilcut = 0.0 fmriucut = fmrifreq / 2.0 # set up fast resampling padtime = 60.0 numpadtrs = int(padtime / fmritr) padtime = fmritr * numpadtrs genlagtc = tide_res.FastResampler(nirs_x, nirs_y, padtime=padtime, doplot=False) initial_fmri_y = genlagtc.yfromx(initial_fmri_x) if display: fig = plt.figure() ax = fig.add_subplot(111) ax.set_title("Regressors") plt.plot(nirs_x, nirs_y, initial_fmri_x, initial_fmri_y) plt.show() # loop over space sliceoffsettime = 0.0 fmri_x = initial_fmri_x - lag - sliceoffsettime print(fmri_x[0], initial_fmri_x[0], lag, sliceoffsettime) fmri_y = genlagtc.yfromx(fmri_x) thenoise = noiselevel * np.random.standard_normal(len(fmri_y)) simdata[:] = immean * (1.0 + (boldpc / 100.0) * fmri_y) + thenoise if display: plt.plot(initial_fmri_x, simdata, initial_fmri_x, initial_fmri_y) plt.show() # tests msethresh = 1e-6 aethresh = 2 assert mse(simdata, initial_fmri_y) < aethresh
def test_congrid(debug=False, display=False): # make the source axis starttime = 0.0 endtime = 1.0 sourcelen = 1000 sourceaxis = np.linspace(starttime, endtime, num=sourcelen, endpoint=False) if debug: print("sourceaxis range:", sourceaxis[0], sourceaxis[-1]) # now make the destination gridlen = 32 gridaxis = np.linspace(starttime, endtime, num=gridlen, endpoint=False) if debug: print("gridaxis range:", gridaxis[0], gridaxis[-1]) cycles = 1.0 if debug: outputlines = [] if debug: cyclist = [1.0, 2.0, 3.0] kernellist = ["gauss", "kaiser"] binslist = [1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] else: cyclist = [1.0] kernellist = ["gauss", "kaiser"] binslist = [1.5, 2.0, 2.5, 3.0] for cycles in cyclist: timecoursein = np.float64(sourceaxis * 0.0) for i in range(len(sourceaxis)): timecoursein[i] = funcvalue2(sourceaxis[i], frequency=cycles) # define the gridding congridbins = 1.5 gridkernel = "gauss" # initialize the test points numsamples = 200 testvals = np.zeros((numsamples), dtype=np.float64) for i in range(numsamples): testvals[i] = np.random.uniform() * (endtime - starttime) + starttime weights = np.zeros((gridlen), dtype=float) griddeddata = np.zeros((gridlen), dtype=float) for gridkernel in kernellist: for congridbins in binslist: print("about to grid") # reinitialize grid outputs weights *= 0.0 griddeddata *= 0.0 for i in range(numsamples): thevals, theweights, theindices = congrid( gridaxis, testvals[i], funcvalue2(testvals[i], frequency=cycles), congridbins, kernel=gridkernel, debug=False, ) for i in range(len(theindices)): weights[theindices[i]] += theweights[i] griddeddata[theindices[i]] += thevals[i] griddeddata = np.where(weights > 0.0, griddeddata / weights, 0.0) target = np.float64(gridaxis * 0.0) for i in range(len(gridaxis)): target[i] = funcvalue2(gridaxis[i], frequency=cycles) print("gridding done") print("debug:", debug) # plot if we are doing that if display: offset = 0.0 legend = [] plt.plot(sourceaxis, timecoursein) legend.append("Original") # offset += 1.0 plt.plot(gridaxis, target + offset) legend.append("Target") # offset += 1.0 plt.plot(gridaxis, griddeddata + offset) legend.append("Gridded") plt.plot(gridaxis, weights) legend.append("Weights") plt.legend(legend) plt.show() # do the tests msethresh = 1.5e-2 themse = mse(target, griddeddata) if debug: if themse >= msethresh: extra = "FAIL" else: extra = "" print( "mse for", cycles, "cycles:", gridkernel, str(congridbins), ":", themse, extra, ) outputlines.append(" ".join([ "mse for", str(cycles), "cycles:", gridkernel, str(congridbins), ":", str(themse), ])) if not debug: assert themse < msethresh if debug: for theline in outputlines: print(theline)
def test_calcsimfunc(debug=False, display=False): # make the lfo filter lfofilter = tide_filt.NoncausalFilter(filtertype="lfo") # make some data oversampfactor = 2 numvoxels = 100 numtimepoints = 500 tr = 0.72 Fs = 1.0 / tr init_fmri_x = np.linspace( 0.0, numtimepoints, numtimepoints, endpoint=False) * tr oversampfreq = oversampfactor * Fs os_fmri_x = np.linspace(0.0, numtimepoints * oversampfactor, numtimepoints * oversampfactor) * (1.0 / oversampfreq) theinputdata = np.zeros((numvoxels, numtimepoints), dtype=np.float64) meanval = np.zeros((numvoxels), dtype=np.float64) testfreq = 0.075 msethresh = 1e-3 # make the starting regressor sourcedata = np.sin(2.0 * np.pi * testfreq * os_fmri_x) numpasses = 1 # make the timeshifted data shiftstart = -5.0 shiftend = 5.0 voxelshifts = np.linspace(shiftstart, shiftend, numvoxels, endpoint=False) for i in range(numvoxels): theinputdata[i, :] = np.sin(2.0 * np.pi * testfreq * (init_fmri_x - voxelshifts[i])) if display: plt.figure() plt.plot(sourcedata) plt.show() genlagtc = tide_resample.FastResampler(os_fmri_x, sourcedata) thexcorr = tide_corr.fastcorrelate(sourcedata, sourcedata) xcorrlen = len(thexcorr) xcorr_x = (np.linspace(0.0, xcorrlen, xcorrlen, endpoint=False) * tr - (xcorrlen * tr) / 2.0 + tr / 2.0) if display: plt.figure() plt.plot(xcorr_x, thexcorr) plt.show() corrzero = xcorrlen // 2 lagmin = -10.0 lagmax = 10.0 lagmininpts = int((-lagmin * oversampfreq) - 0.5) lagmaxinpts = int((lagmax * oversampfreq) + 0.5) searchstart = int(np.round(corrzero + lagmin / tr)) searchend = int(np.round(corrzero + lagmax / tr)) numcorrpoints = lagmaxinpts + lagmininpts corrout = np.zeros((numvoxels, numcorrpoints), dtype=np.float64) lagmask = np.zeros((numvoxels), dtype=np.float64) failimage = np.zeros((numvoxels), dtype=np.float64) lagtimes = np.zeros((numvoxels), dtype=np.float64) lagstrengths = np.zeros((numvoxels), dtype=np.float64) lagsigma = np.zeros((numvoxels), dtype=np.float64) gaussout = np.zeros((numvoxels, numcorrpoints), dtype=np.float64) windowout = np.zeros((numvoxels, numcorrpoints), dtype=np.float64) R2 = np.zeros((numvoxels), dtype=np.float64) lagtc = np.zeros((numvoxels, numtimepoints), dtype=np.float64) optiondict = { "numestreps": 10000, "interptype": "univariate", "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.1, "debug": False, "enforcethresh": True, "lagmod": 1000.0, "searchfrac": 0.5, "mp_chunksize": 1000, "oversampfactor": oversampfactor, "despeckle_thresh": 5.0, "zerooutbadfit": False, "permutationmethod": "shuffle", "hardlimit": True, } theprefilter = tide_filt.NoncausalFilter("lfo") theCorrelator = tide_classes.Correlator( Fs=oversampfreq, 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"], zerooutbadfit=optiondict["zerooutbadfit"], 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) for thenprocs in [1, -1]: for i in range(numpasses): ( voxelsprocessed_cp, theglobalmaxlist, trimmedcorrscale, ) = tide_calcsimfunc.correlationpass( theinputdata, sourcedata, theCorrelator, init_fmri_x, os_fmri_x, lagmininpts, lagmaxinpts, corrout, meanval, nprocs=thenprocs, oversampfactor=optiondict["oversampfactor"], interptype=optiondict["interptype"], showprogressbar=optiondict["showprogressbar"], chunksize=optiondict["mp_chunksize"], ) if display: plt.figure() plt.plot(trimmedcorrscale, corrout[numvoxels // 2, :], "k") plt.show() voxelsprocessed_fc = tide_simfuncfit.fitcorr( genlagtc, init_fmri_x, lagtc, trimmedcorrscale, thefitter, corrout, lagmask, failimage, lagtimes, lagstrengths, lagsigma, gaussout, windowout, R2, nprocs=optiondict["nprocs"], fixdelay=optiondict["fixdelay"], showprogressbar=optiondict["showprogressbar"], chunksize=optiondict["mp_chunksize"], despeckle_thresh=optiondict["despeckle_thresh"], ) if display: plt.figure() plt.plot(voxelshifts, "k") plt.plot(lagtimes, "r") plt.show() if debug: for i in range(numvoxels): print( voxelshifts[i], lagtimes[i], lagstrengths[i], lagsigma[i], failimage[i], ) assert mse(voxelshifts, lagtimes) < msethresh
def test_timeshift(debug=False): tr = 1.0 padtime = 300.0 testlen = 1000 shiftdist = 30 timeaxis = np.arange(0.0, 1.0 * testlen) * tr # timecoursein = np.zeros((testlen), dtype='float64') timecoursein = np.float64(timeaxis * 0.0) midpoint = int(testlen // 2) + 1 timecoursein[midpoint - 1] = np.float64(1.0) timecoursein[midpoint] = np.float64(1.0) timecoursein[midpoint + 1] = np.float64(1.0) timecoursein -= 0.5 butterorder = 4 timecoursein = 0.5 * dolpfiltfilt(1.0, 0.25, timecoursein, butterorder) + 0.5 shiftlist = [-30, -20, -10, 0, 10, 20, 30] shiftlist = [-100] if debug: plt.figure() plt.ylim([-1.0, 2.0 * len(shiftlist) + 1.0]) plt.plot(timecoursein) legend = ["Original"] offset = 0.0 for shiftdist in shiftlist: # generate the ground truth rolled regressor tcrolled = np.float64(np.roll(timecoursein, int(shiftdist))) # generate the fast resampled regressor # tcshifted = genlaggedtc.yfromx(timeaxis - shiftdist, debug=debug) tcshifted, weights, alltc, allweights = timeshift(timecoursein, 1.0 * shiftdist, int(padtime // tr), doplot=False) # print out all elements print("len tcrolled=", len(tcrolled), "len tcshifted=", len(tcshifted)) for i in range(0, len(tcrolled)): print(i, tcrolled[i], tcshifted[i], tcshifted[i] - tcrolled[i]) # plot if we are doing that if debug: offset += 1.0 plt.plot(tcrolled + offset) legend.append("Roll " + str(shiftdist)) offset += 1.0 plt.plot(tcshifted + offset) legend.append("Timeshift " + str(shiftdist)) plt.plot(weights + offset) legend.append("Weights " + str(shiftdist)) # do the tests msethresh = 1e-6 aethresh = 2 assert mse(tcrolled, tcshifted) < msethresh np.testing.assert_almost_equal(tcrolled, tcshifted, aethresh) if debug: plt.legend(legend) plt.show()
def test_glmpass(debug=True, display=False): xsize = 150 xcycles = 7 tsize = 200 tcycles = 23 mean = 100.0 noiselevel = 5.0 targetarray, xwaveforms, twaveforms = gen2d(xsize=xsize, xcycles=xcycles, tsize=tsize, tcycles=tcycles) testarray = targetarray + np.random.random((xsize, tsize)) + mean if display: plt.figure() plt.imshow(targetarray) plt.show() filtereddata = 0.0 * testarray datatoremove = 0.0 * testarray threshval = 0.0 meanvals_t = np.zeros(tsize, dtype=np.float64) rvals_t = np.zeros(tsize, dtype=np.float64) r2vals_t = np.zeros(tsize, dtype=np.float64) fitcoffs_t = np.zeros(tsize, dtype=np.float64) fitNorm_t = np.zeros(tsize, dtype=np.float64) meanvals_x = np.zeros(xsize, dtype=np.float64) rvals_x = np.zeros(xsize, dtype=np.float64) r2vals_x = np.zeros(xsize, dtype=np.float64) fitcoffs_x = np.zeros(xsize, dtype=np.float64) fitNorm_x = np.zeros(xsize, dtype=np.float64) # run along spatial direction # no multiproc tide_glmpass.glmpass( xsize, testarray, threshval, twaveforms, meanvals_x, rvals_x, r2vals_x, fitcoffs_x, fitNorm_x, datatoremove, filtereddata, showprogressbar=False, procbyvoxel=True, nprocs=1, ) if display: plt.figure() plt.imshow(datatoremove) plt.show() plt.imshow(filtereddata) plt.show() if debug: print("proc by space, single proc:", mse(datatoremove, targetarray)) assert mse(datatoremove, targetarray) < 1e-3 # multiproc tide_glmpass.glmpass( xsize, testarray, threshval, twaveforms, meanvals_x, rvals_x, r2vals_x, fitcoffs_x, fitNorm_x, datatoremove, filtereddata, showprogressbar=False, procbyvoxel=True, nprocs=2, ) if display: plt.figure() plt.imshow(datatoremove) plt.show() plt.imshow(filtereddata) plt.show() if debug: print("proc by space, multi proc:", mse(datatoremove, targetarray)) assert mse(datatoremove, targetarray) < 1e-3 # run along time direction # no multiproc tide_glmpass.glmpass( tsize, testarray, threshval, xwaveforms, meanvals_t, rvals_t, r2vals_t, fitcoffs_t, fitNorm_t, datatoremove, filtereddata, showprogressbar=False, procbyvoxel=False, nprocs=1, ) if display: plt.figure() plt.imshow(datatoremove) plt.show() plt.imshow(filtereddata) plt.show() if debug: print("proc by time, single proc:", mse(datatoremove, targetarray)) assert mse(datatoremove, targetarray) < 1e-3 # multiproc tide_glmpass.glmpass( tsize, testarray, threshval, xwaveforms, meanvals_t, rvals_t, r2vals_t, fitcoffs_t, fitNorm_t, datatoremove, filtereddata, showprogressbar=False, procbyvoxel=False, nprocs=2, ) if display: plt.figure() plt.imshow(datatoremove) plt.show() plt.imshow(filtereddata) plt.show() if debug: print("proc by time, multi proc:", mse(datatoremove, targetarray)) assert mse(datatoremove, targetarray) < 1e-3 # no mask tide_glmpass.glmpass( tsize, testarray, None, xwaveforms, meanvals_t, rvals_t, r2vals_t, fitcoffs_t, fitNorm_t, datatoremove, filtereddata, showprogressbar=False, procbyvoxel=False, nprocs=1, ) if display: plt.figure() plt.imshow(datatoremove) plt.show() plt.imshow(filtereddata) plt.show() if debug: print("proc by time, single proc, no mask:", mse(datatoremove, targetarray)) assert mse(datatoremove, targetarray) < 1e-3
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_simulate(display=False): fmritr = 1.5 numtrs = 260 fmriskip = 0 oversampfac = 10 inputfreq = oversampfac / fmritr inputstarttime = 0.0 timecourse = np.zeros((oversampfac * numtrs), dtype='float') timecourse[500:600] = 1.0 timecourse[700:750] = 1.0 # read in the timecourse to resample inputvec = tide_math.stdnormalize(timecourse) simregressorpts = len(inputvec) # prepare the input data for interpolation print("Input regressor has ", simregressorpts, " points") inputstep = 1.0 / inputfreq nirs_x = np.r_[0.0:1.0 * simregressorpts] * inputstep - inputstarttime nirs_y = inputvec[0:simregressorpts] print('nirs regressor runs from ', nirs_x[0], ' to ', nirs_x[-1]) # prepare the output timepoints fmrifreq = 1.0 / fmritr initial_fmri_x = np.r_[0:fmritr * (numtrs - fmriskip):fmritr] + fmritr * fmriskip print('length of fmri after removing skip:', len(initial_fmri_x)) print('fmri time runs from ', initial_fmri_x[0], ' to ', initial_fmri_x[-1]) # set the sim parameters immean = 1.0 boldpc = 1.0 lag = 10.0 * fmritr noiselevel = 0.0 simdata = np.zeros((len(initial_fmri_x)), dtype='float') fmrilcut = 0.0 fmriucut = fmrifreq / 2.0 # set up fast resampling padvalue = 60.0 numpadtrs = int(padvalue / fmritr) padvalue = fmritr * numpadtrs genlagtc = tide_res.fastresampler(nirs_x, nirs_y, padvalue=padvalue, doplot=False) initial_fmri_y = genlagtc.yfromx(initial_fmri_x) if display: fig = figure() ax = fig.add_subplot(111) ax.set_title('Regressors') plt.plot(nirs_x, nirs_y, initial_fmri_x, initial_fmri_y) plt.show() # loop over space sliceoffsettime = 0.0 fmri_x = initial_fmri_x - lag - sliceoffsettime print(fmri_x[0], initial_fmri_x[0], lag, sliceoffsettime) fmri_y = genlagtc.yfromx(fmri_x) thenoise = noiselevel * np.random.standard_normal(len(fmri_y)) simdata[:] = immean * (1.0 + (boldpc / 100.0) * fmri_y) + thenoise if display: plt.plot(initial_fmri_x, simdata, initial_fmri_x, initial_fmri_y) plt.show() # tests msethresh = 1e-6 aethresh = 2 assert mse(simdata, initial_fmri_y) < aethresh