コード例 #1
0
def testSingleSubjectMultipleTimes(signal, times):
    print("Testing single subject, multiple {} times...".format(times))
    results = []
    for t in range(times):
        neuro_act = simulateFCD.computeSubjectSimulation(C, N)
        bds = simulateFCD.computeSubjectBOLD(neuro_act)
        bdsT = bds.T
        sim_filt = BOLDFilters.filterBrainArea(bdsT, 0)
        sim_filt /= np.std(sim_filt)
        error = errorMetrics.l2(signal, sim_filt)
        print("Trial: {} ({}) of {}".format(t+1, t, times), "Error:", error)
        results.append([error])
    avg = np.average(results)
    std = np.std(results)
    print("Average:", avg, "std:", std)

    # the histogram of the data
    n, bins, patches = plt.hist(results/avg*10, bins=10, facecolor='g', alpha=0.75)
    plt.xlabel('error')
    plt.ylabel('Probability')
    plt.title('Histogram of errors')
    plt.text(60, .025, '$\mu$={}, $\sigma$={}'.format(avg, std))
    #plt.xlim(40, 160)
    #plt.ylim(0, 0.03)
    #plt.grid(True)
    plt.show()
コード例 #2
0
ファイル: swFCD.py プロジェクト: dagush/WholeBrain
def from_fMRI(signal, applyFilters = True):  # Compute the FCD of an input BOLD signal
    (N, Tmax) = signal.shape
    lastWindow = Tmax - windowSize  # 190 = 220 - 30
    N_windows = calc_length(0, lastWindow, windowStep)  # N_windows = len(np.arange(0, lastWindow, windowStep))

    if not np.isnan(signal).any():  # No problems, go ahead!!!
        signal_filt = BOLDFilters.BandPassFilter(signal)  # Filters seem to be always applied...
        Isubdiag = np.tril_indices(N, k=-1)  # Indices of triangular lower part of matrix

        # For each pair of sliding windows calculate the FC at t and t2 and
        # compute the correlation between the two.
        cotsampling = np.zeros((int(N_windows*(N_windows-1)/2)))
        kk = 0
        ii2 = 0
        for t in range(0, lastWindow, windowStep):
            jj2 = 0
            sfilt = (signal_filt[:, t:t+windowSize+1]).T  # Extracts a (sliding) window between t and t+windowSize (included)
            cc = np.corrcoef(sfilt, rowvar=False)  # Pearson correlation coefficients
            for t2 in range(0, lastWindow, windowStep):
                sfilt2 = (signal_filt[:, t2:t2+windowSize+1]).T  # Extracts a (sliding) window between t2 and t2+windowSize (included)
                cc2 = np.corrcoef(sfilt2, rowvar=False)  # Pearson correlation coefficients
                ca = pearson_r(cc[Isubdiag],cc2[Isubdiag])  # Correlation between both FC
                if jj2 > ii2:  # Only keep the upper triangular part
                    cotsampling[kk] = ca
                    kk = kk+1
                jj2 = jj2+1
            ii2 = ii2+1

        return cotsampling
    else:
        warnings.warn('############ Warning!!! swFCD.from_fMRI: NAN found ############')
        return np.nan
コード例 #3
0
def filtPowSpetra(signal, TR):
    nNodes, Tmax = signal.shape  # Here we are assuming we receive only ONE subject...
    # idxMinFreq = np.argmin(np.abs(freqs-0.04))
    # idxMaxFreq = np.argmin(np.abs(freqs-0.07))
    # nFreqs = freqs.size
    
    # delt = 2                                   # sampling interval
    # fnq = 1/(2*delt)                           # Nyquist frequency
    # k = 2                                      # 2nd order butterworth filter

    # =================== WIDE BANDPASS
    # flp = .04                                  # lowpass frequency of filter
    # fhi = fnq-0.001  #.249                     # highpass needs to be limited by Nyquist frequency, which in turn depends on TR
    # ts_filt_wide =zscore(filtfilt(bfilt_wide,afilt_wide,x))
    # pw_filt_wide = abs(fft(ts_filt_wide))
    # PowSpect_filt_wide(:,seed) = pw_filt_wide[1:np.floor(TT/2)] ** 2 / (TT/2)

    # =================== NARROW LOW BANDPASS
    # print(f'BOLD Filters: low={BOLDFilters.flp}, hi={BOLDFilters.fhi}')
    # ts_filt_narrow = zscore(BOLDFilters.BandPassFilter(signal, removeStrongArtefacts=False), axis=0)  # Here we used the zscore to "normalize" the values... not really needed, but makes things easier to follow! ;-)
    ts_filt_narrow = BOLDFilters.BandPassFilter(signal, removeStrongArtefacts=False)
    pw_filt_narrow = np.abs(np.fft.fft(ts_filt_narrow, axis=1))
    PowSpect_filt_narrow = pw_filt_narrow[:, 0:int(np.floor(Tmax/2))].T**2 / (Tmax/TR)

    # Power_Areas_filt_narrow_unsmoothed = PowSpect_filt_narrow  # By now, do nothing...
    return PowSpect_filt_narrow
コード例 #4
0
ファイル: optimBOLD.py プロジェクト: dagush/WholeBrain
def fitBOLDBrainArea(neuronal_act, BOLDSignal, area, lowerBounds = initialLowerBounds):
    def errorFunc(neuro_act, epsilon, alpha, tau, gamma, kappa):
        if Verbose:
            global evalCounter
            evalCounter += 1
            print("Test:", evalCounter)
        BOLDModel.epsilon = epsilon
        BOLDModel.alpha = alpha
        BOLDModel.tau = tau
        BOLDModel.gamma = gamma
        BOLDModel.kappa = kappa
        bds = simulateFCD.computeSubjectBOLD(neuro_act, areasToSimulate=[area])
        bdsT = bds.T
        sim_filt = BOLDFilters.filterBrainArea(bdsT, 0)
        sim_filt /= np.std(sim_filt)
        return sim_filt

    # init...
    from scipy.optimize import curve_fit
    emp_filt = BOLDFilters.filterBrainArea(BOLDSignal, area)
    emp_filt /= np.std(emp_filt)

    # Now, fit it !!!
    # Note: Be careful with max_nfev, evaluations include numerical derivatives, so the actual number will be around 4.6 (or more) * this number before stopping!!!
    if numMethod == 'trf':
        popt, pcov = curve_fit(errorFunc, neuronal_act, emp_filt, method='trf', bounds=(lowerBounds, 3.0), p0 = initialValues, max_nfev = 100)
    else: # use 'lm'
        popt, pcov = curve_fit(errorFunc, neuronal_act, emp_filt, method='lm', p0 = initialValues)
    final_values = errorFunc(neuronal_act, popt[0], popt[1], popt[2], popt[3], popt[4])
    finalError = errorMetrics.l2(emp_filt, final_values)
    return popt, pcov, finalError
コード例 #5
0
def from_fMRI(ts_emp,
              applyFilters=True
              ):  # Compute the Metastability of an input BOLD signal
    # --------------------------------------------------------------------------
    # for isub=1:nsub
    #     for inode=1:nnodes
    #         ts_emp_sub(inode,:)=detrend(ts_emp(inode,:,isub)-mean(ts_emp(inode,:,isub)));
    #         ts_emp_filt(inode,:,isub)=filtfilt(bfilt,afilt,ts_emp_sub(inode,:));
    #         % pw = abs(fft(ts_emp_filt(inode,:,isub)));
    #         % PowSpect(:,inode,isub) = pw(1:floor(Tmax/2)).^2/(Tmax/Cfg.TRsec);
    #         Xanalytic(inode,:) = hilbert(demean(ts_emp_filt(inode,:,isub)));
    #         phases_emp(inode,:,isub) = angle(Xanalytic(inode,:));              % <--
    #     end
    #
    #     T=10:Tmax-10;
    #     sync = zeros(1, numel(T));
    #     for t=T
    #         ku=sum(complex(cos(phases_emp(:,t,isub)),sin(phases_emp(:,t,isub))))/nnodes;
    #         sync(t-9)=abs(ku);
    #     end
    #     % empirical metastability
    #     meta_emp_all(isub)=std(sync(:));
    #     % fc_emp_all(:,:,isub) = corrcoef(ts_emp_filt(:,:,isub)');
    #     % phfcd_emp_all(:,isub)=patternCons(phases_emp(:,:,isub),nnodes,Tmax);
    # end
    # --------------------------------------------------------------------------
    (N, Tmax) = ts_emp.shape
    npattmax = Tmax - 19  # calculates the size of phfcd vector
    # size_kk3 = int((npattmax - 3) * (npattmax - 2) / 2)  # The int() is not needed because N*(N-1) is always even, but "it will produce an error in the future"...

    if not np.isnan(ts_emp).any():  # No problems, go ahead!!!
        # Data structures we are going to need...
        phases_emp = np.zeros([N, Tmax])
        # sync = np.zeros(npattmax)

        # Filters seem to be always applied...
        ts_emp_filt = BOLDFilters.BandPassFilter(
            ts_emp)  # zero phase filter the data
        for n in range(N):
            Xanalytic = signal.hilbert(demean.demean(ts_emp_filt[n, :]))
            phases_emp[n, :] = np.angle(Xanalytic)

        T = np.arange(10, Tmax - 10 + 1)
        sync = np.zeros(T.size)
        for t in T:
            ku = np.sum(
                np.cos(phases_emp[:, t - 1]) +
                1j * np.sin(phases_emp[:, t - 1])) / N
            sync[t - 10] = abs(ku)

        # empirical metastability
        meta_emp_all = np.std(sync)
    else:
        warnings.warn(
            f'############ Warning!!! Metastability.from_fMRI: NAN found ############'
        )
        meta_emp_all = np.nan
    return meta_emp_all
コード例 #6
0
def simBrainAreaForOptimVars(N, area, keysAndValues):
    global neuro_act
    simulateActivitySubject(C, N, we)
    for key, value in keysAndValues.items():
        exec('BOLDHemModel2.'+key+' = '+str(value))
    bds = simulateFCD.computeSubjectBOLD(neuro_act, areasToSimulate=[area])
    bdsT = bds.T
    sim_filt = BOLDFilters.filterBrainArea(bdsT, 0)
    sim_filt /= np.std(sim_filt)
    return sim_filt
コード例 #7
0
def from_fMRI(signal, applyFilters=True):
    if not np.isnan(signal).any():  # No problems, go ahead!!!
        if applyFilters:
            signal_filt = BOLDFilters.BandPassFilter(signal)
            sfiltT = signal_filt.T
        else:
            sfiltT = signal.T
        cc = np.corrcoef(sfiltT,
                         rowvar=False)  # Pearson correlation coefficients
        return cc
    else:
        n = signal.shape[0]
        return np.nan
コード例 #8
0
ファイル: optimBOLD.py プロジェクト: dagush/WholeBrain
 def errorFunc(neuro_act, epsilon, alpha, tau, gamma, kappa):
     if Verbose:
         global evalCounter
         evalCounter += 1
         print("Test:", evalCounter)
     BOLDModel.epsilon = epsilon
     BOLDModel.alpha = alpha
     BOLDModel.tau = tau
     BOLDModel.gamma = gamma
     BOLDModel.kappa = kappa
     bds = simulateFCD.computeSubjectBOLD(neuro_act, areasToSimulate=[area])
     bdsT = bds.T
     sim_filt = BOLDFilters.filterBrainArea(bdsT, 0)
     sim_filt /= np.std(sim_filt)
     return sim_filt
コード例 #9
0
def fitPltErrorForBrainArea(BOLDSignal, area):
    N,T = BOLDSignal.shape
    simulateActivitySubject(C, N, we)
    # popt, pcov, finalError = optimBOLD.fitBOLDBrainAreaCatchingErrors(neuro_act, BOLDSignal, area)
    # perr = np.sqrt(np.diag(pcov))
    # print("Computed minimum:", popt, "value:", finalError, "Std Dev:", perr)

    interval=range(T)
    emp_filt = BOLDFilters.filterBrainArea(BOLDSignal, area)
    emp_filt /= np.std(emp_filt)
    empSignal, = plt.plot(interval, emp_filt, 'r--', label='empirical')
    sim_filtOptim = simBrainAreaForOptimVars(N, area, {'epsilon': 0.5}) # popt[0])
    simSignalOptim, = plt.plot(interval, sim_filtOptim, 'b-', label='simulated(epsilon={})'.format(0.5)) #popt[0]))
    plt.suptitle('Optimal Sim and Empirical BOLD for area {}'.format(area))
    plt.legend(handles=[empSignal, simSignalOptim])
    plt.show()
コード例 #10
0
def pltSimAndEmpiricalBrainAreaForVariousVariableValues(BOLDSignal, area, varKey, subplot=False):
    N,T = BOLDSignal.shape
    interval=range(T)
    plt.rcParams.update({'font.size': 22})

    if subplot:
        plt.subplot(2, 1, 1)
        plt.title("Empirical BOLD for area {}".format(area))
    emp_filt = BOLDFilters.filterBrainArea(BOLDSignal, area)
    emp_filt /= np.std(emp_filt)
    empSignal, = plt.plot(interval, emp_filt, 'r--', label='empirical')

    if subplot:
        plt.subplot(2, 1, 2)
        plt.title("Simulated BOLD for area {}".format(area))
    sim_filt00 = simBrainAreaForOptimVars(N, area, {varKey: 0.05})  # Some vars cannot be just 0.0...
    simSignal00, = plt.plot(interval, sim_filt00, 'b-', label='simulated('+varKey+'=0.05)')

    sim_filt05 = simBrainAreaForOptimVars(N, area, {varKey: 0.5})
    simSignal05, = plt.plot(interval, sim_filt05, 'g-', label='simulated('+varKey+'=0.5)')

    sim_filt10 = simBrainAreaForOptimVars(N, area, {varKey: 1.0})
    simSignal10, = plt.plot(interval, sim_filt10, 'c-', label='simulated('+varKey+'=1.0)')

    sim_filt15 = simBrainAreaForOptimVars(N, area, {varKey: 1.5})
    simSignal15, = plt.plot(interval, sim_filt15, 'm-', label='simulated('+varKey+'=1.5)')

    plt.suptitle('Sim and Empirical BOLD for single area, '+varKey)
    if not subplot:
        plt.legend(handles=[empSignal, simSignal00, simSignal05, simSignal10, simSignal15])
    else:
        plt.legend(handles=[simSignal00, simSignal05, simSignal10, simSignal15])
        plt.tight_layout()
    # plt.legend(handles=[simSignal00, simSignal05, simSignal10, simSignal15])
    # plt.legend(handles=[empSignal, simSignal05])
    plt.show()

    # just some computations to know a little bit better this stuff
    print("l^2 Error (0.05):", errorMetrics.l2(emp_filt, sim_filt00))
    print("l^2 Error (0.5):", errorMetrics.l2(emp_filt, sim_filt05))
    print("l^2 Error (1.0):", errorMetrics.l2(emp_filt, sim_filt10))
    print("l^2 Error (1.5):", errorMetrics.l2(emp_filt, sim_filt15))
    from scipy.stats.stats import pearsonr
    print("Pearson.r (0.05):", pearsonr(emp_filt, sim_filt00))
    print("Pearson.r (0.5):", pearsonr(emp_filt, sim_filt05))
    print("Pearson.r (1.0):", pearsonr(emp_filt, sim_filt10))
    print("Pearson.r (1.5):", pearsonr(emp_filt, sim_filt15))
コード例 #11
0
def checkAveragedValuesOverSubjects(tc_aal, area, condition):
    print("function checkAveragedValuesOverSubjects ({} Subjects)".format(Subjects))
    print("======================================================")
    allErrors = np.zeros([Subjects])
    avgResult, avgError = averageSingleBrainAreaForAllSubjects(tc_aal, area, condition)
    print("Average error (area by area):", avgError)
    for subject in range(Subjects):
        print("Checking area {} from subject {} !!!".format(area,subject))
        BOLDData = tc_aal[subject, condition]
        emp_filt = BOLDFilters.filterBrainArea(BOLDData, area)
        emp_filt /= np.std(emp_filt)
        sim_filtOptim = simBrainAreaForOptimVars(N, area, optimBOLD.pairVarsAndValues(avgResult))
        error = errorMetrics.l2(emp_filt, sim_filtOptim)
        print("Error computed:", error)
        allErrors[subject] = error
    finalAvgError = np.mean(allErrors)
    print("Final avg error (all areas same parms):", finalAvgError)
コード例 #12
0
def pltErrorForBrainAreaFitOptimVariable(BOLDSignal, area, key, minValue = -0.5, maxValue=2.6, stepValue=0.1):
    from scipy.stats.stats import pearsonr
    emp_filt = BOLDFilters.filterBrainArea(BOLDSignal, area)
    emp_filt /= np.std(emp_filt)

    N,T = BOLDSignal.shape
    global neuro_act
    simulateActivitySubject(C, N, we)
    values = np.arange(minValue,maxValue,stepValue)
    results = [] #np.zeros([len(epsilons), len(areas)])
    resultsPearson = []
    minVar=-1
    minValue=1e99
    for value in values:
        print("Var value:", value, end=' ')
        sim_filt = simBrainAreaForOptimVars(N, area, {key: value})
        # simSignal, = plt.plot(interval, sim_filt, label='simulated(epsilon={}})'.format(epsilon))
        error = errorMetrics.l2(emp_filt, sim_filt)
        errorPearson = (1-pearsonr(emp_filt, sim_filt)[0])*20
        if error < minValue:
            minVar = value
            minValue = error
        results.append([error])
        resultsPearson.append([errorPearson])
        print("Error:", error, "Pearson.r:", errorPearson)
    print("'Manual' minimum:", minValue, "at:", minVar,)
    plt.rcParams.update({'font.size': 22})
    plt.suptitle('BOLD Error for '+ key +', area ({})'.format(area))
    plt.plot(values, results, 'r-')
    plt.plot(values, resultsPearson, 'b-')
    #popt, pcov, finalError = optimBOLD.fitBOLDBrainAreaCatchingErrors(neuro_act, BOLDSignal, area)
    #perr = np.sqrt(np.diag(pcov))
    #print("Computed minimum:", popt, "value:", finalError,"Std Dev:", perr)
    lineAt = minVar  # popt[0]   or   minVar
    plt.axvline(x=lineAt, color='g', linestyle='--')
    plt.show()

    interval=range(T)
    empSignal, = plt.plot(interval, emp_filt, 'r--', label='empirical')
    sim_filtOptim = simBrainAreaForOptimVars(N, area, {key: lineAt})
    simSignalOptim, = plt.plot(interval, sim_filtOptim, 'b-', label='simulated(epsilon={})'.format(lineAt))
    plt.suptitle('Optimal Sim and Empirical ('+key+') BOLD for area {}'.format(area))
    plt.legend(handles=[empSignal, simSignalOptim])
    plt.show()
コード例 #13
0
def fitAndPltBrainArea(BOLDSignal, area):
    # init...
    N,T = BOLDSignal.shape
    simulateActivitySubject(C, N, we)
    # fit !!!
    t = time.time()
    popt, perr, finalError = fitBrainArea(BOLDSignal, area)
    print(np.round_(time.time() - t, 3), 'sec elapsed')
    # print("Area {} processed:".format(area))
    # print("Computed minimum:", pairVarsAndValues(popt), "\n         Std Dev:", pairVarsAndValues(perr, "\n         value:", finalError))
    # and plot.
    interval=range(T)
    emp_filt = BOLDFilters.filterBrainArea(BOLDSignal, area)
    emp_filt /= np.std(emp_filt)
    empSignal, = plt.plot(interval, emp_filt, 'r--', label='Empirical')
    sim_filtOptim = simBrainAreaForOptimVars(N, area, optimBOLD.pairVarsAndValues(popt))
    simSignalOptim, = plt.plot(interval, sim_filtOptim, 'b-', label='Simulated')
    plt.suptitle('Optimal Sim and Empirical BOLD for area {}'.format(area))
    plt.legend(handles=[empSignal, simSignalOptim])
    plt.show()
コード例 #14
0
def from_fMRI(
    ts,
    applyFilters=True
):  # Compute the Phase-Interaction Matrix of an input BOLD signal
    (N, Tmax) = ts.shape
    npattmax = Tmax - (2 * discardOffset - 1
                       )  # calculates the size of phfcd matrix

    if not np.isnan(ts).any():  # No problems, go ahead!!!
        # Data structures we are going to need...
        phases = np.zeros((N, Tmax))
        dFC = np.zeros((N, N))
        # PhIntMatr = np.zeros((npattmax, int(N * (N - 1) / 2)))  # The int() is not needed, but... (see above)
        PhIntMatr = np.zeros((npattmax, N, N))
        # syncdata = np.zeros(npattmax)

        # Filters seem to be always applied...
        ts_filt = BOLDFilters.BandPassFilter(ts)  # zero phase filter the data
        for n in range(N):
            Xanalytic = signal.hilbert(demean.demean(ts_filt[n, :]))
            phases[n, :] = np.angle(Xanalytic)

        # Isubdiag = tril_indices_column(N, k=-1)  # Indices of triangular lower part of matrix
        T = np.arange(discardOffset, Tmax - discardOffset + 1)
        for t in T:
            # kudata = np.sum(np.cos(phases[:, t - 1]) + 1j * np.sin(phases[:, t - 1])) / N
            # syncdata[t - 10] = abs(kudata)
            for i in range(N):
                for j in range(N):
                    # print(f'processing {t}: ({i}, {j})')
                    dFC[i, j] = np.cos(adif(phases[i, t - 1], phases[j,
                                                                     t - 1]))
            PhIntMatr[t - discardOffset] = dFC
    else:
        warnings.warn(
            '############ Warning!!! PhaseInteractionMatrix.from_fMRI: NAN found ############'
        )
        PhIntMatr = np.array([np.nan])
    return PhIntMatr