def aliasedcorrelate( hiressignal, hires_Fs, lowressignal, lowres_Fs, timerange, hiresstarttime=0.0, lowresstarttime=0.0, padtime=30.0, ): """Perform an aliased correlation. This function is deprecated, and is retained here as a reference against which to test AliasedCorrelator. Parameters ---------- hiressignal: 1D array The unaliased waveform to match hires_Fs: float The sample rate of the unaliased waveform lowressignal: 1D array The aliased waveform to match lowres_Fs: float The sample rate of the aliased waveform timerange: 1D array The delays for which to calculate the correlation function Returns ------- corrfunc: 1D array The correlation function evaluated at timepoints of timerange """ highresaxis = np.arange( 0.0, len(hiressignal)) * (1.0 / hires_Fs) - hiresstarttime lowresaxis = np.arange( 0.0, len(lowressignal)) * (1.0 / lowres_Fs) - lowresstarttime tcgenerator = tide_resample.FastResampler(highresaxis, hiressignal, padtime=padtime) targetsignal = tide_math.corrnormalize(lowressignal) corrfunc = timerange * 0.0 for i in range(len(timerange)): aliasedhiressignal = tide_math.corrnormalize( tcgenerator.yfromx(lowresaxis + timerange[i])) corrfunc[i] = np.dot(aliasedhiressignal, targetsignal) return corrfunc
def __init__( self, hiressignal, hires_Fs, lores_Fs, timerange, hiresstarttime=0.0, loresstarttime=0.0, padtime=30.0, ): self.hiressignal = hiressignal self.hires_Fs = hires_Fs self.hiresstarttime = hiresstarttime self.lores_Fs = lores_Fs self.timerange = timerange self.loresstarttime = loresstarttime self.highresaxis = ( np.arange(0.0, len(self.hiressignal)) * (1.0 / self.hires_Fs) - self.hiresstarttime ) self.padtime = padtime self.tcgenerator = tide_resample.FastResampler( self.highresaxis, self.hiressignal, padtime=self.padtime ) self.aliasedsignals = {}
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_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_delayestimation(display=False, debug=False): # set the number of MKL threads to use if mklexists: print("disabling MKL") mkl.set_num_threads(1) # set parameters Fs = 10.0 numpoints = 5000 numlocs = 21 refnum = int(numlocs // 2) timestep = 0.228764 oversampfac = 2 detrendorder = 1 oversampfreq = Fs * oversampfac corrtr = 1.0 / oversampfreq smoothingtime = 1.0 bipolar = False interptype = "univariate" lagmod = 1000.0 lagmin = -20.0 lagmax = 20.0 lagmininpts = int((-lagmin / corrtr) - 0.5) lagmaxinpts = int((lagmax / corrtr) + 0.5) peakfittype = "gauss" corrweighting = "None" similaritymetric = "hybrid" windowfunc = "hamming" chunksize = 5 pedestal = 100.0 # set up the filter theprefilter = tide_filt.NoncausalFilter("arb", transferfunc="brickwall", debug=False) theprefilter.setfreqs(0.009, 0.01, 0.15, 0.16) # construct the various test waveforms timepoints = np.linspace(0.0, numpoints / Fs, num=numpoints, endpoint=False) oversamptimepoints = np.linspace(0.0, numpoints / Fs, num=oversampfac * numpoints, endpoint=False) waveforms = np.zeros((numlocs, numpoints), dtype=np.float64) paramlist = [ [0.314, 0.055457, 0.0], [-0.723, 0.08347856, np.pi], [-0.834, 0.1102947, 0.0], [1.0, 0.13425, 0.5], ] offsets = np.zeros(numlocs, dtype=np.float64) amplitudes = np.ones(numlocs, dtype=np.float64) for i in range(numlocs): offsets[i] = timestep * (i - refnum) waveforms[i, :] = multisine(timepoints - offsets[i], paramlist) + pedestal if display: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) for i in range(numlocs): ax.plot(timepoints, waveforms[i, :]) plt.show() threshval = pedestal / 4.0 waveforms = numpy2shared(waveforms, np.float64) referencetc = tide_resample.doresample(timepoints, waveforms[refnum, :], oversamptimepoints, method=interptype) referencetc = theprefilter.apply(oversampfreq, referencetc) referencetc = tide_math.corrnormalize(referencetc, detrendorder=detrendorder, windowfunc=windowfunc) # set up theCorrelator if debug: print("\n\nsetting up theCorrelator") theCorrelator = tide_classes.Correlator( Fs=oversampfreq, ncprefilter=theprefilter, detrendorder=detrendorder, windowfunc=windowfunc, corrweighting=corrweighting, debug=True, ) theCorrelator.setreftc( np.zeros((oversampfac * numpoints), dtype=np.float64)) theCorrelator.setlimits(lagmininpts, lagmaxinpts) dummy, trimmedcorrscale, dummy = theCorrelator.getfunction() corroutlen = np.shape(trimmedcorrscale)[0] internalvalidcorrshape = (numlocs, corroutlen) corrout, dummy, dummy = allocshared(internalvalidcorrshape, np.float64) meanval, dummy, dummy = allocshared((numlocs), np.float64) if debug: print("corrout shape:", corrout.shape) print("theCorrelator: corroutlen=", corroutlen) # set up theMutualInformationator if debug: print("\n\nsetting up theMutualInformationator") theMutualInformationator = tide_classes.MutualInformationator( Fs=oversampfreq, smoothingtime=smoothingtime, ncprefilter=theprefilter, detrendorder=detrendorder, windowfunc=windowfunc, madnorm=False, lagmininpts=lagmininpts, lagmaxinpts=lagmaxinpts, debug=False, ) theMutualInformationator.setreftc( np.zeros((oversampfac * numpoints), dtype=np.float64)) theMutualInformationator.setlimits(lagmininpts, lagmaxinpts) # set up thefitter if debug: print("\n\nsetting up thefitter") thefitter = tide_classes.SimilarityFunctionFitter( lagmod=lagmod, lthreshval=0.0, uthreshval=1.0, bipolar=bipolar, lagmin=lagmin, lagmax=lagmax, absmaxsigma=10000.0, absminsigma=0.01, debug=False, peakfittype=peakfittype, ) lagtc, dummy, dummy = allocshared(waveforms.shape, np.float64) fitmask, dummy, dummy = allocshared((numlocs), "uint16") failreason, dummy, dummy = allocshared((numlocs), "uint32") lagtimes, dummy, dummy = allocshared((numlocs), np.float64) lagstrengths, dummy, dummy = allocshared((numlocs), np.float64) lagsigma, dummy, dummy = allocshared((numlocs), np.float64) gaussout, dummy, dummy = allocshared(internalvalidcorrshape, np.float64) windowout, dummy, dummy = allocshared(internalvalidcorrshape, np.float64) rvalue, dummy, dummy = allocshared((numlocs), np.float64) r2value, dummy, dummy = allocshared((numlocs), np.float64) fitcoff, dummy, dummy = allocshared((numlocs), np.float64) fitNorm, dummy, dummy = allocshared((numlocs), np.float64) R2, dummy, dummy = allocshared((numlocs), np.float64) movingsignal, dummy, dummy = allocshared(waveforms.shape, np.float64) filtereddata, dummy, dummy = allocshared(waveforms.shape, np.float64) for nprocs in [4, 1]: # call correlationpass if debug: print("\n\ncalling correlationpass") print("waveforms shape:", waveforms.shape) ( voxelsprocessed_cp, theglobalmaxlist, trimmedcorrscale, ) = tide_calcsimfunc.correlationpass( waveforms[:, :], referencetc, theCorrelator, timepoints, oversamptimepoints, lagmininpts, lagmaxinpts, corrout, meanval, nprocs=nprocs, alwaysmultiproc=False, oversampfactor=oversampfac, interptype=interptype, showprogressbar=False, chunksize=chunksize, ) if debug: print(voxelsprocessed_cp, len(theglobalmaxlist), len(trimmedcorrscale)) if display: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) for i in range(numlocs): ax.plot(trimmedcorrscale, corrout[i, :]) plt.show() # call peakeval if debug: print("\n\ncalling peakeval") voxelsprocessed_pe, thepeakdict = tide_peakeval.peakevalpass( waveforms[:, :], referencetc, timepoints, oversamptimepoints, theMutualInformationator, trimmedcorrscale, corrout, nprocs=nprocs, alwaysmultiproc=False, bipolar=bipolar, oversampfactor=oversampfac, interptype=interptype, showprogressbar=False, chunksize=chunksize, ) if debug: for key in thepeakdict: print(key, thepeakdict[key]) # call thefitter if debug: print("\n\ncalling fitter") thefitter.setfunctype(similaritymetric) thefitter.setcorrtimeaxis(trimmedcorrscale) genlagtc = tide_resample.FastResampler(timepoints, waveforms[refnum, :]) if display: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) if nprocs == 1: proctype = "singleproc" else: proctype = "multiproc" for peakfittype in ["fastgauss", "quad", "fastquad", "gauss"]: thefitter.setpeakfittype(peakfittype) voxelsprocessed_fc = tide_simfuncfit.fitcorr( genlagtc, timepoints, lagtc, trimmedcorrscale, thefitter, corrout, fitmask, failreason, lagtimes, lagstrengths, lagsigma, gaussout, windowout, R2, peakdict=thepeakdict, nprocs=nprocs, alwaysmultiproc=False, fixdelay=None, showprogressbar=False, chunksize=chunksize, despeckle_thresh=100.0, initiallags=None, ) if debug: print(voxelsprocessed_fc) if debug: print("\npeakfittype:", peakfittype) for i in range(numlocs): print( "location", i, ":", offsets[i], lagtimes[i], lagtimes[i] - offsets[i], lagstrengths[i], lagsigma[i], ) if display: ax.plot(offsets, lagtimes, label=peakfittype) if checkfits(lagtimes, offsets, tolerance=0.01): print(proctype, peakfittype, " lagtime: pass") assert True else: print(proctype, peakfittype, " lagtime: fail") assert False if checkfits(lagstrengths, amplitudes, tolerance=0.05): print(proctype, peakfittype, " lagstrength: pass") assert True else: print(proctype, peakfittype, " lagstrength: fail") assert False if display: ax.legend() plt.show() filteredwaveforms, dummy, dummy = allocshared(waveforms.shape, np.float64) for i in range(numlocs): filteredwaveforms[i, :] = theprefilter.apply(Fs, waveforms[i, :]) for nprocs in [4, 1]: voxelsprocessed_glm = tide_glmpass.glmpass( numlocs, waveforms[:, :], threshval, lagtc, meanval, rvalue, r2value, fitcoff, fitNorm, movingsignal, filtereddata, nprocs=nprocs, alwaysmultiproc=False, showprogressbar=False, mp_chunksize=chunksize, ) if nprocs == 1: proctype = "singleproc" else: proctype = "multiproc" diffsignal = filtereddata fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # ax.plot(timepoints, filtereddata[refnum, :], label='filtereddata') ax.plot(oversamptimepoints, referencetc, label="referencetc") ax.plot(timepoints, movingsignal[refnum, :], label="movingsignal") ax.legend() plt.show() print(proctype, "glmpass", np.mean(diffsignal), np.max(np.fabs(diffsignal)))