Esempio n. 1
0
def find_or_retrieve_GMM_labels(dataset, eeg_date, eeg_gcoh_name, real_evs, iL, iH, fL, fH, armv_ver, gcoh_ver, which=0, try_K=[1, 2, 3, 4, 5, 6, 7], TRs=[1, 2, 4, 8, 16, 32, 64], manual_cluster=False, ignore_stored=False, do_pca=False, min_var_expld=0.95, dontsave=False, Bayesian=False):
    ###############
    if not dontsave:
        outdir = datconf.getResultFN(dataset, "%(rpsm)s/v%(av)d%(gv)d" % {"rpsm" : eeg_date, "w" : which, "av" : armv_ver, "gv" : gcoh_ver})

        if not os.access(outdir, os.F_OK):
            os.mkdir(outdir)

        fn = "%(od)s/%(eeg)s_%(fL)d-%(fH)d_GMM_labels%(w)d" % {"od" : outdir, "eeg" : eeg_gcoh_name, "w" : which, "fL" : fL, "fH" : fH}


    if (not ignore_stored) and os.access(fn, os.F_OK):
        rmpd_lab = _N.loadtxt(fn, dtype=_N.int)
        nStates  = len(_N.unique(rmpd_lab))
    else:
        print("doing GMM")
        minK    = _N.min(try_K)
        maxK    = _N.max(try_K)

        bics = _N.ones(((maxK-minK), _N.max(TRs)))*1000000
        labs = _N.empty((maxK-minK, _N.max(TRs), real_evs.shape[0]), dtype=_N.int)
        _features = _N.mean(real_evs[:, iL:iH], axis=1)

        if do_pca:
            pca = PCA()
            pca.fit(_features)
            
            proj = _N.einsum("ni,mi->nm", pca.components_, _features)
            print(pca.explained_variance_ratio_)
            maxC = _N.where(_N.cumsum(pca.explained_variance_ratio_) > min_var_expld)[0][0]
            features = proj[0:maxC].T
            print("Using %d features" % maxC)
        else:
            features = _features

        if not Bayesian:
            for K in range(minK, maxK):
                for tr in range(TRs[K]):
                    gmm = mixture.GaussianMixture(n_components=K, covariance_type="full")

                    gmm.fit(features)
                    bics[K-minK, tr] = gmm.bic(features)
                    labs[K-minK, tr] = gmm.predict(features)

            coords = _N.where(bics == _N.min(bics))
            bestLab = labs[coords[0][0], coords[1][0]]   #  indices in 2-D array
            nStates =  list(range(minK, maxK))[coords[0][0]]
            print("@!!!!! ")
            print(coords)
        else:
            gmm = mixture.BayesianGaussianMixture(n_components=15, weight_concentration_prior_type="dirichlet_process", covariance_type="full")
            gmm.fit(features)
            bestLab = gmm.predict(features)
            nStates = len(_N.unique(bestLab))

        rmpd_lab = remap_label_by_similarity(nStates, bestLab, _features) # raw features
        #rmpd_lab = increasing_labels_mapping(bestLab)

        if not dontsave:
            print("saving!!!!!!!!!!!!!!!!  %s" % fn)
            _N.savetxt(fn, rmpd_lab, fmt="%d")

    print("manual_clustering   %s" % str(manual_cluster))
    if manual_cluster:
        if os.access("%s_manual" % fn, os.F_OK):  #  open file of mapping
            print("found manual cluster for %s" % fn)
            mapping = _N.loadtxt("%s_manual" % fn, dtype=_N.int)
            #  col 0 is original cluster ID
            #  col 1 is remapped cluster ID
            where_origs = []
            for imaps in range(mapping.shape[0]):
                orig_ID = mapping[imaps, 0]
                new_ID  = mapping[imaps, 1]
                where_origs.append(_N.where(rmpd_lab == orig_ID)[0])
            for imaps in range(mapping.shape[0]):
                orig_ID = mapping[imaps, 0]
                new_ID  = mapping[imaps, 1]
                rmpd_lab[where_origs[imaps]] = new_ID
        else:
            print("!!  No manual cluster for %s.  Using original mapping" % fn)

    return nStates, rmpd_lab
Esempio n. 2
0
all_pwr2    = _N.zeros(N_psd)
all_pwr12   = _N.zeros(N_psd)        

fs_use = _N.linspace(0, 1.2, N_psd)

MULT   = 1.   #  kernel width
lags_sec=50

ii = -1
for partID in partIDs:
    ii += 1
    rpsm_key = rpsms.rpsm_partID_as_key[partID]

    sections = 4

    pikdir     = datconf.getResultFN(datconf._RPS, "%(dir)s/v%(av)d%(gv)d" % {"dir" : rpsm_key, "av" : armv_ver, "gv" : gcoh_ver})
    label          = 71
    outdir         = pikdir#"%(pd)s/%(lb)d_%(st)s_%(sec)d_%(toff)d" % {"pd" : pikdir, "lb" : label, "st" : shfl_type_str[shfl_type], "toff" : t_offset, "sec" : sections}

    lm       = depickle("%(od)s/%(rk)s_%(w)d_%(s)d_pkld_dat_v%(av)d%(gv)d_%(lb)d.dmp" % {"rk" : partID, "w" : win, "s" : slideby, "av" : armv_ver, "gv" : gcoh_ver, "od" : pikdir, "lb" : label})

    if not os.access(outdir, os.F_OK):
         os.mkdir(outdir)

    _behv_sigs_all   = _N.array(lm["behv"])
         
    bigchg_behv_sig_ts = lm["behv_ts"][0:_behv_sigs_all.shape[1]-stop_early] - t_offset         
    win_gcoh      = lm["win_gcoh"]
    slide_gcoh    = lm["slide_gcoh"]
    win_spec      = lm["win_spec"]
    slide_spec    = lm["slide_spec"]
Esempio n. 3
0
                "dsf": dat,
                "av": armv_ver,
                "gv": gcoh_ver,
                "wn": win,
                "sld": slideby,
                "tr2tr": str2tr
            }))
    # #lm         = depickle("../Neurable/DSi_dat/%(dat)s_gcoh_%(w)s_%(s)s.dmp" % {"dat" : dat, "w" : bin, "s" : slide})
    # #A_gcoh_mat = _scio.loadmat("DSi_dat/%(dat)s_gcoh_%(w)d_%(sl)d.mat" % {"dat" : dat, "w" : bin, "sl" : slide})
    # #A_gcoh     = A_gcoh_mat["Cs"]

    strt = 0  #  if start at middle of experiment
    A_gcoh = lm["Cs"][strt:]
    n_fs = lm["fs"]

    outdir1 = datconf.getResultFN(dataset, "%(dir)s" % {"dir": dat})
    if not os.access(outdir1, os.F_OK):
        os.mkdir(outdir1)
    outdir = datconf.getResultFN(
        dataset, "%(dir)s/v%(av)d%(gv)d" % {
            "dir": dat,
            "av": armv_ver,
            "gv": gcoh_ver
        })
    if not os.access(outdir, os.F_OK):
        os.mkdir(outdir)
    #  12.10.2020
    ################  egenvectors
    #imag_evs  = A_gcoh_mat["VEC"][0]

    imag_evs = lm["VEC"][strt:, :, ev_n]