def _procOneVoxelCorrelation(vox, thetc, optiondict, fmri_x, fmritc, os_fmri_x, oversampfreq, corrorigin, lagmininpts, lagmaxinpts, ncprefilter, referencetc, rt_floatset=np.float64, rt_floattype='float64' ): if optiondict['oversampfactor'] >= 1: thetc[:] = tide_resample.doresample(fmri_x, fmritc, os_fmri_x, method=optiondict['interptype']) else: thetc[:] = fmritc thexcorr, theglobalmax = onecorrelation(thetc, oversampfreq, corrorigin, lagmininpts, lagmaxinpts, ncprefilter, referencetc, usewindowfunc=optiondict['usewindowfunc'], detrendorder=optiondict['detrendorder'], windowfunc=optiondict['windowfunc'], corrweighting=optiondict['corrweighting']) return vox, np.mean(thetc), thexcorr, theglobalmax
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 _procOneVoxelCorrelation(vox, thetc, thecorrelator, fmri_x, fmritc, os_fmri_x, oversampfactor=1, interptype='univariate', rt_floatset=np.float64, rt_floattype='float64' ): if oversampfactor >= 1: thetc[:] = tide_resample.doresample(fmri_x, fmritc, os_fmri_x, method=interptype) else: thetc[:] = fmritc thexcorr_y, thexcorr_x, theglobalmax = thecorrelator.run(thetc) return vox, np.mean(thetc), thexcorr_y, thexcorr_x, theglobalmax
def _procOneVoxelPeaks( vox, thetc, theMutualInformationator, fmri_x, fmritc, os_fmri_x, xcorr_x, thexcorr, bipolar=False, oversampfactor=1, sort=True, interptype="univariate", ): if oversampfactor >= 1: thetc[:] = tide_resample.doresample(fmri_x, fmritc, os_fmri_x, method=interptype) else: thetc[:] = fmritc thepeaks = tide_fit.getpeaks(xcorr_x, thexcorr, bipolar=bipolar, display=False) peaklocs = [] for thepeak in thepeaks: peaklocs.append(int(round(thepeak[2], 0))) theMI_list = theMutualInformationator.run(thetc, locs=peaklocs) hybridpeaks = [] for i in range(len(thepeaks)): hybridpeaks.append([thepeaks[i][0], thepeaks[i][1], theMI_list[i]]) if sort: hybridpeaks.sort(key=lambda x: x[2], reverse=True) return vox, hybridpeaks
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)))