示例#1
0
def extract_fit_displacement(dsfile, expttype='size_contrast_opto_0'):
    displacement = {}
    with ut.hdf5read(dsfile) as f:
        keylist = [key for key in f.keys()]
        for ikey in range(len(keylist)):
            session = f[keylist[ikey]]
            if expttype in session:
                sc0 = session[expttype]
                if 'rf_displacement_deg' in sc0:
                    pval = sc0['rf_mapping_pval'][:]
                    sigma = session['retinotopy_0']['rf_sigma'][:]
                    X = session['cell_center'][:]
                    y = sc0['rf_displacement_deg'][:].T
                    if sigma.max() > 0:
                        lkat = ut.k_and(pval < 0.05, ~np.isnan(X[:, 0]),
                                        ~np.isnan(y[:, 0]), sigma > 5)
                    else:
                        lkat = ut.k_and(pval < 0.05, ~np.isnan(X[:, 0]),
                                        ~np.isnan(y[:, 0]))
                    linreg = sklearn.linear_model.LinearRegression().fit(
                        X[lkat], y[lkat])
                    displacement[keylist[ikey]] = np.zeros_like(y)
                    displacement[
                        keylist[ikey]][~np.isnan(X[:, 0])] = linreg.predict(
                            X[~np.isnan(X[:, 0])])
    return displacement
def extract_image_data(dsname, keylist=None):
    with ut.hdf5read(dsname) as ds:
        if keylist is None:
            keylist = list(ds.keys())
        image_datas = {}
        for ikey in range(len(keylist)):
            print(keylist[ikey])
            image_data = {}
            source_keys = [
                'mean_green_channel', 'mean_red_channel',
                'mean_green_channel_enhanced', 'mean_red_channel_corrected',
                'cell_mask', 'cell_depth'
            ]
            target_keys = ['g', 'r', 'g2', 'r2', 'msk', 'depth']
            for skey, tkey in zip(source_keys, target_keys):
                if skey in ds[keylist[ikey]]:
                    if tkey == 'depth':
                        image_data[tkey] = ds[keylist[ikey]][skey][:].astype(
                            'int')
                    else:
                        image_data[tkey] = ds[keylist[ikey]][skey][:]
                else:
                    print('%s not in dsfile' % skey)
            #image_data['g'] = ds[keylist[ikey]]['mean_green_channel'][:]
            #image_data['r'] = ds[keylist[ikey]]['mean_red_channel'][:]
            #image_data['g2'] = ds[keylist[ikey]]['mean_green_channel_enhanced'][:]
            #image_data['r2'] = ds[keylist[ikey]]['mean_red_channel_corrected'][:]
            #image_data['msk'] = ds[keylist[ikey]]['cell_mask'][:]
            #image_data['depth'] = ds[keylist[ikey]]['cell_depth'][:].astype('int')
            image_datas[keylist[ikey]] = image_data
    return image_datas
示例#3
0
def look_up_nroi(dsname):
    # compute tuning as above, for each of a list of HDF5 files each corresponding to a particular cell type
    with ut.hdf5read(dsname) as ds:
        keylist = list(ds.keys())
        nkey = len(keylist)
        nroi = [None for i in range(nkey)]
        for ikey in range(nkey):
            nroi[ikey] = len(ds[keylist[ikey]]['cell_id'][:])
    return nroi
示例#4
0
def compute_tuning(dsfile,
                   running=True,
                   center=True,
                   fieldname='decon',
                   keylist=None,
                   pval_cutoff=np.inf,
                   dcutoff=np.inf,
                   nbefore=8,
                   nafter=8,
                   run_cutoff=10):
    with ut.hdf5read(dsfile) as f:
        if keylist is None:
            keylist = [key for key in f.keys()]
        tuning = [None for ikey in range(len(keylist))]
        cell_criteria = [None for ikey in range(len(keylist))]
        uparam = [None for ikey in range(len(keylist))]
        for ikey in range(len(keylist)):
            session = f[keylist[ikey]]
            if 'size_contrast_opto_0' in session:
                sc0 = session['size_contrast_opto_0']
                #                 print([key for key in session.keys()])
                data = sc0[fieldname][:]
                stim_id = sc0['stimulus_id'][:]
                paramnames = params = sc0['stimulus_parameters']
                uparam[ikey] = [sc0[paramname][:] for paramname in paramnames]
                trialrun = sc0['running_speed_cm_s'][:, nbefore:-nafter].mean(
                    -1) > run_cutoff
                if not running:
                    trialrun = ~trialrun
                if 'rf_displacement_deg' in sc0:
                    pval = sc0['rf_mapping_pval'][:]
                    X = session['cell_center'][:]
                    y = sc0['rf_displacement_deg'][:].T
                    lkat = ut.k_and(pval < 0.05, ~np.isnan(X[:, 0]),
                                    ~np.isnan(y[:, 0]))
                    linreg = sklearn.linear_model.LinearRegression().fit(
                        X[lkat], y[lkat])
                    displacement = np.zeros_like(y)
                    displacement[~np.isnan(X[:, 0])] = linreg.predict(
                        X[~np.isnan(X[:, 0])])
                if center:
                    cell_criteria[ikey] = (np.sqrt((displacement**2).sum(1)) <
                                           dcutoff) & (pval < pval_cutoff)
                else:
                    cell_criteria[ikey] = (np.sqrt((displacement**2).sum(1)) >
                                           dcutoff) & (pval < pval_cutoff)
                tuning[ikey] = ut.compute_tuning(
                    data,
                    stim_id,
                    cell_criteria=cell_criteria[ikey],
                    trial_criteria=trialrun)  #cell_criteria
                print('%s: %.1f' % (keylist[ikey], trialrun.mean()))
            else:
                print('could not do ' + keylist[ikey])
    return tuning, cell_criteria, uparam
def extract_tuning_data(dsname, iexpt, running=False):
    ikey = iexpt
    with ut.hdf5read(dsname) as ds:
        keylist = list(ds.keys())
    tuning, cell_criteria, uparam = opto_utils.compute_tuning(
        dsname, running=False, dcutoff=np.inf, keylist=[keylist[ikey]])
    tuning_data = {}
    tuning_data['tuning'] = tuning[0]
    tuning_data['cell_criteria'] = cell_criteria[0]
    tuning_data['uparam'] = uparam[0]
    return tuning_data
示例#6
0
def look_up_params(dsnames, expttype, paramnames):
    # compute tuning as above, for each of a list of HDF5 files each corresponding to a particular cell type
    params = []
    for dsname in dsnames:
        print(dsname)
        with ut.hdf5read(dsname) as ds:
            keylist = list(ds.keys())
            nkey = len(keylist)
            this_param = [None for i in range(nkey)]
            for ikey in range(nkey):
                if expttype in ds[keylist[ikey]]:
                    this_param[ikey] = np.array([
                        ds[keylist[ikey]][expttype][paramname][:].flatten()
                        for paramname in paramnames
                    ]).T
        params.append(this_param)
    return params
示例#7
0
def compute_tuning(dsfile,exptname='retinotopy_0',run_cutoff=-np.inf,criterion_cutoff=0.4):
    with ut.hdf5read(dsfile) as f:
        keylist = [key for key in f.keys()]
        tuning = [None for i in range(len(keylist))]
        uparam = [None for i in range(len(keylist))]
        for ikey in range(len(keylist)):
            session = f[keylist[ikey]]
            if exptname in session:
                sc0 = session[exptname]
                data = sc0['decon'][:]
                stim_id = sc0['stimulus_id'][:]
                nbefore = sc0['nbefore'][()]
                nafter = sc0['nafter'][()]
                trialrun = np.nanmean(sc0['running_speed_cm_s'][:,nbefore:-nafter],-1)>run_cutoff
                if np.nanmean(trialrun)>criterion_cutoff:
                    tuning[ikey] = ut.compute_tuning(data,stim_id,trial_criteria=trialrun)[:]
                stim_params = sc0['stimulus_parameters']
                uparam[ikey] = [sc0[x][:] for x in stim_params]
        relevant_list = [key for key in keylist if exptname in f[key]]    
    return tuning,uparam,relevant_list
示例#8
0
def compute_encoding_axes(dsname,
                          expttype='size_contrast_0',
                          cutoffs=(20, ),
                          alphas=np.logspace(-2, 2, 50),
                          running_trials=False,
                          training_set=None):
    na = len(alphas)
    with ut.hdf5read(dsname) as ds:
        keylist = list(ds.keys())
        nkey = len(keylist)
        R = [None for k in range(nkey)]
        reg = [None for k in range(nkey)]
        #         pred = [None for k in range(nkey)]
        top_score = [None for k in range(nkey)]
        proc = [{} for k in range(nkey)]
        auroc = [None for k in range(nkey)]
        for k in range(len(keylist)):
            if expttype in ds[keylist[k]]:
                R[k] = [None for icutoff in range(len(cutoffs))]
                reg[k] = [None for icutoff in range(len(cutoffs))]
                #                 pred[k] = [None for icutoff in range(len(cutoffs))]
                sc0 = ds[keylist[k]][expttype]
                nbefore = sc0['nbefore'][()]
                nafter = sc0['nafter'][()]
                decon = np.nanmean(sc0['decon'][()][:, :, nbefore:-nafter], -1)
                data = sst.zscore(decon, axis=1)
                data[np.isnan(data)] = 0
                if training_set is None:
                    train = np.ones((decon.shape[1], ), dtype='bool')
                else:
                    if isinstance(training_set[keylist[k]], list):
                        train = training_set[keylist[k]][0]
                    else:
                        train = training_set[keylist[k]]

                pval_ret = sc0['rf_mapping_pval'][()]
                dist_ret = sc0['rf_distance_deg'][()]

                ontarget_ret_lax = np.logical_and(dist_ret < 40,
                                                  pval_ret < 0.05)
                #ntokeep = 20
                #if ontarget_ret_lax.sum() > ntokeep:
                #    ot = np.where(ontarget_ret_lax)[0]
                #    throw_out = ot[np.random.choice(len(ot),len(ot)-ntokeep,replace=False)]
                #    ontarget_ret_lax[throw_out] = False
                ntokeep = -np.inf

                data = data[ontarget_ret_lax]
                print(data.shape[0])
                u, sigma, v = np.linalg.svd(data)

                running_speed_cm_s = sc0['running_speed_cm_s'][()]
                running = np.nanmean(running_speed_cm_s[:, nbefore:-nafter],
                                     1) > 7
                if not running_trials:
                    running = ~running
                size = sc0['stimulus_id'][()][0]
                contrast = sc0['stimulus_id'][()][1]
                angle = sc0['stimulus_id'][()][2]
                light = sc0['stimulus_id'][()][3]

                proc[k]['u'] = u
                proc[k]['sigma'] = sigma
                proc[k]['v'] = v
                proc[k]['pval_ret'] = pval_ret
                proc[k]['dist_ret'] = dist_ret
                proc[k]['ontarget_ret_lax'] = ontarget_ret_lax
                proc[k]['running_speed_cm_s'] = running_speed_cm_s
                proc[k]['running'] = running
                proc[k]['size'] = size
                proc[k]['contrast'] = contrast
                proc[k]['angle'] = angle
                proc[k]['light'] = light
                proc[k]['cutoffs'] = cutoffs
                proc[k]['train'] = train

                uangle, usize, ucontrast, ulight = [
                    sc0[key][()] for key in [
                        'stimulus_direction_deg', 'stimulus_size_deg',
                        'stimulus_contrast', 'stimulus_light'
                    ]
                ]

                proc[k]['uangle'], proc[k]['usize'], proc[k][
                    'ucontrast'], proc[k][
                        'ulight'] = uangle, usize, ucontrast, ulight

                if np.logical_and(ontarget_ret_lax.sum() >= ntokeep,
                                  running.mean() > 0.5):
                    #uangle,usize,ucontrast = [np.unique(arr) for arr in [angle,size,contrast]]
                    nlight = len(ulight)
                    nsize = len(usize)
                    ncontrast = len(ucontrast)
                    nangle = len(uangle)
                    top_score[k] = np.zeros(
                        (len(cutoffs), nlight, nsize, nangle))
                    auroc[k] = np.zeros((nlight, nsize, ncontrast - 1, nangle))
                    for icutoff, cutoff in enumerate(cutoffs):
                        R[k][icutoff] = np.zeros(
                            (nlight, nsize, nangle, cutoff))
                        reg[k][icutoff] = [None for ilight in range(nlight)]
                        #                         pred[k][icutoff] = [None for ilight in range(nlight)]
                        #                         actual[k][icutoff][ilight] = [None for ilight in range(nlight)]
                        for ilight in range(nlight):
                            reg[k][icutoff][ilight] = [
                                None for s in range(nsize)
                            ]
                            #                             pred[k][icutoff][ilight] = [None for s in range(nsize)]
                            #                             actual[k][icutoff][ilight] = [None for s in range(nsize)]
                            for s in range(nsize):
                                reg[k][icutoff][ilight][s] = [
                                    None for i in range(nangle)
                                ]
                                #                                 pred[k][icutoff][ilight][s] = [None for i in range(nangle)]
                                #                                 actual[k][icutoff][ilight][s] = [None for i in range(nangle)]
                                for i in range(nangle):
                                    stim_of_interest_all_contrast = ut.k_and(
                                        np.logical_or(
                                            np.logical_and(
                                                angle == i, size == s),
                                            contrast == 0), running,
                                        light == ilight, train
                                    )  #,eye_dist < np.nanpercentile(eye_dist,50))
                                    X = (
                                        np.diag(sigma[:cutoff]) @ v[:cutoff, :]
                                    ).T[stim_of_interest_all_contrast]
                                    y = contrast[
                                        stim_of_interest_all_contrast]  #>0

                                    sc = np.zeros((na, ))
                                    for ia, alpha in enumerate(alphas):
                                        linreg = sklearn.linear_model.Ridge(
                                            alpha=alpha, normalize=True)
                                        reg1 = linreg.fit(X, y)
                                        scores = sklearn.model_selection.cross_validate(
                                            linreg, X, y, cv=5)
                                        sc[ia] = scores['test_score'].mean()
                                    best_alpha = np.argmax(sc)
                                    top_score[k][icutoff, ilight, s,
                                                 i] = sc.max()
                                    linreg = sklearn.linear_model.Ridge(
                                        alpha=alphas[best_alpha],
                                        normalize=True)
                                    pred = sklearn.model_selection.cross_val_predict(
                                        linreg, X, y, cv=5)
                                    actual = y  # [k][icutoff][ilight][s][i]
                                    auroc[k][ilight, s, :,
                                             i] = compute_detection_auroc(
                                                 pred,
                                                 actual,
                                                 nvals=contrast.max())
                                    reg[k][icutoff][ilight][s][i] = linreg.fit(
                                        X, y)
    return reg, proc, top_score, auroc