示例#1
0
def fc_visual(fcfile_pickle):

    with open(fcfile_pickle, 'rb') as handle:
        fc = pickle.load(handle)

    imcohs = fc['imcohs']
    pvals = fc['pvals']
    chnAreas = fc['chnAreas']

    # multiple comparison correction, get weights
    reject, pval_corr = fdr_correction(pvals, alpha=0.05, method='indep')
    [rows, cols] = np.where(reject == True)
    weight = np.zeros(imcohs.shape)
    if len(rows) > 0:
        weight[rows, cols] = imcohs[rows, cols]

    for co in ['normal', 'mild', 'moderate']:
        if co in fcfile_pickle:
            cond = co

    save_prefix = 'all'
    folder, filename = os.path.split(fcfile_pickle)[0], os.path.split(
        fcfile_pickle)[1]
    saveFCGraph = os.path.join(
        folder,
        'visual_' + filename[:-len('.pickle')] + '_' + save_prefix + '.png')
    texts = dict()
    texts[cond] = [-80, 40, 15]
    texts[animal] = [80, 20, 20]
    weight_visual_save(weight,
                       chnInf=assign_coord2chnArea(
                           area_coord_file=area_coord_file, chnAreas=chnAreas),
                       savefile=saveFCGraph,
                       texts=texts,
                       threds_edge=None)
示例#2
0
def fc_visual_subAreas(fcfile_pickle,
                       subareas=['M1', 'STN', 'GP'],
                       subtitle='M1DBS'):

    with open(fcfile_pickle, 'rb') as handle:
        fc = pickle.load(handle)

    imcohs = fc['imcohs']
    pvals = fc['pvals']
    chnAreas = fc['chnAreas']

    idxs_remain = []
    chnAreas_new = []
    for ci, carea in enumerate(chnAreas):
        for sarea in subareas:
            if sarea.lower() in carea.lower():
                idxs_remain.append(ci)
                chnAreas_new.append(carea)

    idxs_remain = np.array(idxs_remain)

    tmp = imcohs[idxs_remain, :]
    tmp = tmp[:, idxs_remain]
    imcohs = tmp
    tmp = pvals[idxs_remain, :]
    tmp = tmp[:, idxs_remain]
    pvals = tmp

    chnAreas = chnAreas_new

    # multiple comparison correction, get weights
    reject, pval_corr = fdr_correction(pvals, alpha=0.05, method='indep')
    [rows, cols] = np.where(reject == True)
    weight = np.zeros(imcohs.shape)
    if len(rows) > 0:
        weight[rows, cols] = imcohs[rows, cols]

    for co in ['normal', 'mild', 'moderate']:
        if co in fcfile_pickle:
            cond = co

    folder, filename = os.path.split(fcfile_pickle)[0], os.path.split(
        fcfile_pickle)[1]
    saveFCGraph = os.path.join(
        folder,
        'visual_' + filename[:-len('.pickle')] + '_' + subtitle + '.png')
    texts = dict()
    texts[cond] = [-80, 40, 15]
    texts[animal] = [80, 20, 20]
    weight_visual_save(weight,
                       chnInf=assign_coord2chnArea(
                           area_coord_file=area_coord_file, chnAreas=chnAreas),
                       savefile=saveFCGraph,
                       texts=texts,
                       threds_edge=None)
示例#3
0
def subArea_dailyfc_visual(files):
    
    for onefile in files:
        lfpdata, chnAreas, fs = lfp_extract([onefile])

        if lfpdata.shape[2] < 80:
            continue


        print(onefile)
        ciCOHs = calc_ciCOHs_rest(lfpdata)




        # permutation test: use the lfp data whose ciCOHs are the largest to get  distribution
        [i, j] = np.unravel_index(np.argmax(ciCOHs), shape = ciCOHs.shape)
        lfp1, lfp2 = lfpdata[i, :, :], lfpdata[j, :, :]
        _, mu, std = pval_permciCOH_rest(lfp1, lfp2, ciCOHs[i, j], shuffleN = 1000)


        cond = re.search('_[a-z]*_[0-9]{8}', files[0]).group()[1:-9]
        datestr = re.search('[0-9]{8}', os.path.basename(onefile)).group()


        ### left thalamus and SMA/M1 ###
        save_prefix = 'leftThaCor_' 
        areas_used = ['lVA', 'lVLo/VPLo', 'lSMA', 'rSMA','M1']

        # subareas selection
        ciCOH_new, chnAreas_new = ciCOH_select(ciCOHs, chnAreas, areas_used)
        
        
        # multiple comparison correction, get weight matrix
        pvals = norm.sf(abs(ciCOH_new), loc = mu, scale = std) * 2
        reject, pval_corr = fdr_correction(pvals, alpha = 0.05, method='indep')
        [rows, cols]= np.where(reject == True)
        weight = np.zeros(ciCOH_new.shape)
        if len(rows) > 0:
            weight[rows, cols] = ciCOH_new[rows, cols]

        # visual and save
        saveFCGraph = os.path.join(savefolder, cond + '_' + save_prefix + '_' + datestr + '.png')
        texts = dict()
        texts[datestr] = [80, 50, 15]
        weight_visual_save(weight, chnInf = assign_coord2chnArea(area_coord_file, chnAreas_new), 
                            savefile = saveFCGraph, texts = None, threds_edge = None)
        del ciCOH_new, chnAreas_new, save_prefix, areas_used
        del saveFCGraph, weight
示例#4
0
def dailyfc_visual(files):

    for onefile in files:
        lfpdata, chnAreas, fs = lfp_extract([onefile])

        if lfpdata.shape[2] < 80:
            continue

        print(onefile)
        ciCOHs = calc_ciCOHs_rest(lfpdata)

        # permutation test: use the lfp data whose ciCOHs are the largest to get  distribution
        [i, j] = np.unravel_index(np.argmax(ciCOHs), shape=ciCOHs.shape)
        lfp1, lfp2 = lfpdata[i, :, :], lfpdata[j, :, :]
        _, mu, std = pval_permciCOH_rest(lfp1,
                                         lfp2,
                                         ciCOHs[i, j],
                                         shuffleN=1000)
        pvals = norm.sf(abs(ciCOHs), loc=mu, scale=std) * 2

        # multiple comparison correction, get weights
        reject, pval_corr = fdr_correction(pvals, alpha=0.05, method='indep')
        [rows, cols] = np.where(reject == True)
        weight = np.zeros(ciCOHs.shape)
        if len(rows) > 0:
            weight[rows, cols] = ciCOHs[rows, cols]

        # visual and save
        filename = os.path.basename(onefile)
        datestr = re.search('[0-9]{8}', filename).group()
        cond = re.search('_[a-z]*_[0-9]{8}', filename).group()[1:-9]
        freqstr = 'freq' + re.search('_filtered[0-9]*_[0-9]*',
                                     filename).group()[len('_filtered'):]

        save_prefix = 'all'
        saveFCGraph = os.path.join(
            savefolder,
            freqstr + '_' + cond + '_' + save_prefix + '_' + datestr + '.png')
        texts = dict()
        texts[cond + ',' + datestr] = [-80, 50, 15]
        weight_visual_save(weight,
                           chnInf=assign_coord2chnArea(
                               area_coord_file=area_coord_file,
                               chnAreas=chnAreas),
                           savefile=saveFCGraph,
                           texts=texts,
                           threds_edge=None)

        del texts, datestr, cond, weight
示例#5
0
def segfc_visual(onefile):

    # lfpdata: nchns * ntemp * nsegs
    lfpdata, chnAreas, fs = lfp_extract([onefile])

    nchns, _, nsegs = lfpdata.shape
    seg_ciCOHs = np.zeros(shape=(nchns, nchns, nsegs))
    for segi in range(nsegs):
        seg_ciCOHs[:, :, segi] = calc_ciCOHs_rest(
            np.expand_dims(lfpdata[:, :, segi], axis=2))

    # permutation test: use the lfp data whose ciCOHs are the largest to get  distribution
    [i, j] = np.unravel_index(np.argmax(ciCOHs), shape=ciCOHs.shape)
    lfp1, lfp2 = lfpdata[i, :, :], lfpdata[j, :, :]
    _, mu, std = pval_permciCOH_rest(lfp1, lfp2, ciCOHs[i, j], shuffleN=1000)
    pvals = norm.sf(abs(ciCOHs), loc=mu, scale=std) * 2

    # multiple comparison correction, get weights
    reject, pval_corr = fdr_correction(pvals, alpha=0.05, method='indep')
    [rows, cols] = np.where(reject == True)
    weight = np.zeros(ciCOHs.shape)
    if len(rows) > 0:
        weight[rows, cols] = ciCOHs[rows, cols]

    # visual and save
    filename = os.path.basename(onefile)
    datestr = re.search('[0-9]{8}', filename).group()
    cond = re.search('_[a-z]*_[0-9]{8}', filename).group()[1:-9]

    save_prefix = 'all'
    saveFCGraph = os.path.join(
        savefolder, cond + '_' + save_prefix + '_' + datestr + '.png')
    weight_visual_save(weight,
                       chnInf=assign_coord2chnArea(
                           area_coord_file=area_coord_file, chnAreas=chnAreas),
                       savefile=saveFCGraph,
                       texts=None,
                       threds_edge=None)
示例#6
0
def dailyfc_visual(files):

    ### fc extract ###
    for onefile in files:
        
        filename = os.path.basename(onefile)
        datestr = re.search('[0-9]{8}', filename).group()

        lfpdata, chnAreas, fs = lfp_extract([onefile])

        if 'cond' not in locals():
            cond = re.search('_[a-z]*_[0-9]{8}', filename).group()[1:-9]
        
        if 'lfpdatas' not in locals():
            lfpdatas = lfpdata
            datestrs = datestr
        else:
            lfpdatas = np.concatenate((lfpdatas, lfpdata), axis = 2)
            datestrs = datestrs + '_' + datestr 


        del lfpdata, datestr
        

        ### if enough lfpdatas
        if lfpdatas.shape[2] >= 500 / 5:

            lfp1, lfp2 = lfpdatas[:, 0:500, :], lfpdatas[:, 125:625, :]
            lfp3, lfp4 = lfpdatas[:, 250:750, :], lfpdatas[:, 375:875, :]
            lfp5= lfpdatas[:, 500:, :]
            lfpdatas = np.concatenate((lfp1, lfp2, lfp3, lfp4, lfp5), axis=2)

            idx_ntrials = np.random.randint(lfpdatas.shape[2], size = 500)
            lfpdatas = lfpdatas[:, :, idx_ntrials]
            nchns, ntemp, ntrials = lfpdatas.shape
            
            
            ### calc ciCOH for each cond ###
            ciCOH = calc_ciCOHs_rest(lfpdatas)
            ciCOH = abs(ciCOH)

            
            ### all ##
            save_prefix = 'all'
            
            # get weight matrix
            pvals = pvals_fc_overtime(ciCOH = ciCOH, ntrials = ntrials, ntemp = ntemp, f = (freq[0] + freq[1])/2, t = ntemp/fs)
            reject, pval_corr = fdr_correction(pvals, alpha = 0.1, method='indep')
            [rows, cols]= np.where(reject)
            weight = np.zeros(ciCOH.shape)
            if len(rows) > 0:
                weight[rows, cols] = ciCOH[rows, cols]

            # visual and save
            saveFCname = cond + '_'  + save_prefix + '_' + datestrs + '.png'
            saveFCGraph = os.path.join(savefolder, saveFCname)
            weight_visual_save(weight, chnInf = assign_coord2chnArea(area_coord_file, chnAreas), 
                                savefile = saveFCGraph, texts = None, threds_edge = None)


            # network metric
            avg_CC = graph_metrics(weight)
            d = {saveFCname: avg_CC}
            with open(os.path.join(savefolder, 'avgCC.csv'), 'a+') as f:
                for key in d.keys():
                    f.write("%s,%s\n"%(key,d[key]))
            
            del avg_CC, d
            del pvals, reject, pval_corr, rows, cols
            del saveFCGraph, weight, save_prefix, saveFCname

            
            
            ### left thalamus and SMA/M1 ###
            save_prefix = 'leftThaCor_' 
            areas_used = ['lVA', 'lVLo/VPLo', 'lSMA', 'rSMA','M1']

            # subareas selection
            ciCOH_new, chnAreas_new = ciCOH_select(ciCOH, chnAreas, areas_used)
            
            
            # get weight matrix
            pvals = pvals_fc_overtime(ciCOH = ciCOH_new, ntrials = ntrials, ntemp = ntemp, f = (freq[0] + freq[1])/2, t = ntemp/fs)
            reject, pval_corr = fdr_correction(pvals, alpha = 0.1, method='indep')
            [rows, cols]= np.where(reject)
            weight = np.zeros(ciCOH.shape)
            if len(rows) > 0:
                weight[rows, cols] = ciCOH[rows, cols]

            # visual and save
            saveFCGraph = os.path.join(savefolder, cond + '_' + save_prefix + '_' + datestrs + '.png')
            weight_visual_save(weight, chnInf = assign_coord2chnArea(area_coord_file, chnAreas_new), 
                                savefile = saveFCGraph, texts = None, threds_edge = None)
            del ciCOH_new, chnAreas_new, save_prefix, areas_used
            del saveFCGraph, weight




            ### right thalamus and SMA/M1 ###
            save_prefix = 'rightThaCor'
            areas_used = ['rVA', 'rVLo/VPLo', 'lSMA', 'rSMA','M1']
            
            # subareas selection
            ciCOH_new, chnAreas_new = ciCOH_select(ciCOH, chnAreas, areas_used)

            # get weight matrix
            pvals = pvals_fc_overtime(ciCOH = ciCOH_new, ntrials = ntrials, ntemp = ntemp, f = (freq[0] + freq[1])/2, t = ntemp/fs)
            reject, pval_corr = fdr_correction(pvals, alpha = 0.1, method='indep')
            [rows, cols]= np.where(reject)
            weight = np.zeros(ciCOH.shape)
            if len(rows) > 0:
                weight[rows, cols] = ciCOH[rows, cols]

            # visual and save
            saveFCGraph = os.path.join(savefolder, cond + '_' + save_prefix + '_' + datestrs + '.png')
            weight_visual_save(weight, chnInf = assign_coord2chnArea(area_coord_file, chnAreas_new), 
                                savefile = saveFCGraph, texts = None, threds_edge = None)
            del ciCOH_new, chnAreas_new, save_prefix, areas_used
            del saveFCGraph, weight
           

            
            ### right thalamus and GP ###
            save_prefix = 'gpRightTha'
            areas_used = ['rVA', 'rVLo/VPLo', 'GP']
            
            # subareas selection
            ciCOH_new, chnAreas_new = ciCOH_select(ciCOH, chnAreas, areas_used)

            # get weight matrix
            pvals = pvals_fc_overtime(ciCOH = ciCOH_new, ntrials = ntrials, ntemp = ntemp, f = (freq[0] + freq[1])/2, t = ntemp/fs)
            reject, pval_corr = fdr_correction(pvals, alpha = 0.1, method='indep')
            [rows, cols]= np.where(reject)
            weight = np.zeros(ciCOH.shape)
            if len(rows) > 0:
                weight[rows, cols] = ciCOH[rows, cols]

            # visual and save
            saveFCGraph = os.path.join(savefolder, cond + '_' + save_prefix + '_' + datestrs + '.png')
            weight_visual_save(weight, chnInf = assign_coord2chnArea(area_coord_file, chnAreas_new), 
                                savefile = saveFCGraph, texts = None, threds_edge = None)
            del ciCOH_new, chnAreas_new, save_prefix, areas_used
            del saveFCGraph, weight



            ### left thalamus and GP ###
            save_prefix = 'gpLeftTha'
            areas_used = ['lVA', 'lVLo/VPLo', 'GP']
            
            # subareas selection
            ciCOH_new, chnAreas_new = ciCOH_select(ciCOH, chnAreas, areas_used)

            # get weight matrix
            pvals = pvals_fc_overtime(ciCOH = ciCOH_new, ntrials = ntrials, ntemp = ntemp, f = (freq[0] + freq[1])/2, t = ntemp/fs)
            reject, pval_corr = fdr_correction(pvals, alpha = 0.1, method='indep')
            [rows, cols]= np.where(reject)
            weight = np.zeros(ciCOH.shape)
            if len(rows) > 0:
                weight[rows, cols] = ciCOH[rows, cols]

            # visual and save
            saveFCGraph = os.path.join(savefolder, cond + '_' + save_prefix + '_' + datestrs + '.png')
            weight_visual_save(weight, chnInf = assign_coord2chnArea(area_coord_file, chnAreas_new), 
                                savefile = saveFCGraph, texts = None, threds_edge = None)
            del ciCOH_new, chnAreas_new, save_prefix, areas_used
            del saveFCGraph, weight



            del lfpdatas, idx_ntrials, datestrs
            del ciCOH
示例#7
0
            for ni in range(ts.shape[0]):
                saveFCGraph = os.path.join(tmpfolder, str(ni) + '.png')
                texts = dict()
                texts['t = ' + str(round(ts[ni], 3)) + 'ms'] = [-80, 40, 15]
                texts[animal + ': ' + cond + ',' + event_name] = [300, 40, 15]

                texts['Segi = ' + str(segi)] = [-80, 240, 15]

                # add change point marker if it is
                if ni + 1 in cpoints and (ni + 1) != ts.shape[0]:
                    segi = segi + 1

                weight_visual_save(trun_dynfc[:, :, ni],
                                   chnInf=assign_coord2chnArea(
                                       area_coord_file=area_coord_file,
                                       chnAreas=dynfc['chnAreas']),
                                   savefile=saveFCGraph,
                                   texts=texts,
                                   threds_edge=None)

                images.append(saveFCGraph)

            generate_video(genvideofile=os.path.join(
                savesubfolder, savename_prefix + '_wCP_' + cond + '.avi'),
                           fps=fps,
                           images=images)
            shutil.rmtree(tmpfolder)
            del images, segi
            """ end plot dyn_FC video """
            """ Summary FC during segments """
            for ci in range(len(cpoints)):
示例#8
0
plt.ylabel('cos diff')


plt.savefig(os.path.join(savefolder, prefix + '.png'))
plt.clf()
print('save' +  prefix + ' at ' + os.path.join(savefolder))


# plot dyn_FC video
images = []
fps = fs
for ni in range(ntemp):
    saveFCGraph = os.path.join(savefolder, str(ni) + '.png')
    texts = dict()
    texts['t = ' + str((round(ni/fs + tdur_trial[0], 3)) * 1000) + 'ms'] = [-80, 40, 15]
    weight_visual_save(trun_dynfc_normal[:, :, ni], chnInf = assign_coord2chnArea(area_coord_file = area_coord_file, chnAreas = chnAreas), 
                            savefile = saveFCGraph, texts = texts, threds_edge = None)

    images.append(saveFCGraph)

generate_video(genvideofile = os.path.join(savefolder, animal + '_normal.mp4'), fps = fps, images = images)
for ni in range(ntemp):
    os.remove(os.path.join(savefolder, str(ni) + '.png'))



images = []
for ni in range(ntemp):
    saveFCGraph = os.path.join(savefolder, str(ni) + '.png')
    texts = dict()
    texts['t = ' + str((round(ni/fs + tdur_trial[0], 3)) * 1000) + 'ms'] = [-80, 40, 15]
    weight_visual_save(trun_dynfc_mild[:, :, ni], chnInf = assign_coord2chnArea(area_coord_file = area_coord_file, chnAreas = chnAreas),