Ejemplo n.º 1
0
    def __clustering(self, nodo, points, output):
        username = '******'
        password = '******'
        host = '172.16.162.128'
        dbname = 'demo'
        url = 'postgresql://%s:%s@%s/%s' % (username, password, host, dbname)
        dbconn = psycopg2.connect('dbname=%s user=%s password=%s host=%s' %
                                  (dbname, username, password, host))
        smod = np.float(2.5)

        connect = "dbname=demo host=172.16.162.128 user=postgres password=postgres"
        spikes_id = np.array(nodo, np.float64)

        nspikes = len(nodo)

        rho = np.empty(nspikes)
        delta = np.empty(nspikes)
        nneigh = np.empty(nspikes)
        centers = np.empty(nspikes)

        cluster_index = np.empty(nspikes)
        features = neurodb.features.getFeaturesFromSpikes(nodo,
                                                          connection=dbconn)

        dc = libcd.getDC(connect, features, spikes_id, nspikes, np.float(1.8),
                         points)
        libcd.dpClustering(features, nspikes, dc, points, "gaussian",
                           spikes_id, cluster_index, rho, delta, smod)

        #plt.plot(delta[rho.argsort()])
        #plt.show()
        #show_selection(rho, delta)
        #show_features(features, cluster_index)

        if (self.nnodos == 1):
            return spikes_id, cluster_index

        #plt.plot(rho, delta, 'o')
        #plt.show()
        templates = []
        spikes = []
        out = []

        for i in range(1, int(cluster_index.max()) + 1):
            template = np.zeros(64, np.float64)
            gspikes = []
            k = 0
            for j in range(nspikes):
                if cluster_index[j] == i:
                    spike = neurodb.neodb.core.spikedb.get_from_db(
                        dbconn, id=int(spikes_id[j]))
                    signal = spike[0].waveform
                    template = template + signal
                    gspikes.append(spikes_id[j])
                    k = k + 1
            template = template / k
            out.append((template, gspikes))

        dbconn.close()
        output.put(out)
Ejemplo n.º 2
0
    def fitSpikes(self, spike_ids=None, recordingchannel_id=None):
        results = []
        spike_ids_cp = np.copy(spike_ids)
        if spike_ids != None:
            if self.threading == "multi":
                results = self.__process_multi(spike_ids)

            if self.threading == "serial":
                if self.nnodos == 1:
                    spike_ids, labels = self.__process_serial(spike_ids)
                    #Lo que entra no es lo que sale de esta funcion, corregir

                    return labels
                else:
                    results = self.__process_serial(spike_ids)

        if results != []:
            templates = []
            ids = []
            for x in results:
                templates.append(x[0])
                ids.append(x[1])

            smod = np.float(1.5)

            features_ids = self.__insertFeaturesTemplate(templates, ids)

            features_ids = np.array(features_ids, np.float64)

            nspikes = len(features_ids)
            rho = np.empty(nspikes)
            delta = np.empty(nspikes)
            id_spikes = np.empty(nspikes)
            cluster_index = np.empty(len(features_ids))
            dc = libcd.getDC(self.connect, features_ids, id_spikes,
                             len(features_ids), np.float(2.0), self.points)
            libcd.dpClustering(features_ids, len(features_ids), dc,
                               self.points, "gaussian", id_spikes,
                               cluster_index, rho, delta, smod)
            #cent = show_selection(rho, delta, plot=False)
            #show_features(features_ids, np.ones(len(features_ids)), cent)
            # Cuando se hace una consulta a la base no se devuelve los ids ordenados segun la consulta

        spikes = neurodb.features.getFromDB(features_id=features_ids,
                                            column='extra')

        labels = []
        for j in range(len(spike_ids_cp)):
            flag = 0
            for k in range(len(spikes)):
                if spike_ids_cp[j] in spikes[k][1]:
                    labels.append(cluster_index[k])
                    flag = 1
            if flag == 0:
                labels.append(0)

        neurodb.features.removeOnDB(features_id=features_ids)
        self.__saveLabelsMulti(spike_ids, labels)
        return np.array(labels)
Ejemplo n.º 3
0
def clustering(nodo, points, output):
    username = '******'
    password = '******'
    host = '172.16.162.128'
    dbname = 'demo'
    url = 'postgresql://%s:%s@%s/%s' % (username, password, host, dbname)
    dbconn = psycopg2.connect('dbname=%s user=%s password=%s host=%s' %
                              (dbname, username, password, host))

    connect = "dbname=demo host=172.16.162.128 user=postgres password=postgres"
    spikes_id = np.array(nodo, np.float64)
    #spikes_id1= np.copy(spikes_id)

    nspikes = len(nodo)

    rho = np.empty(nspikes)
    delta = np.empty(nspikes)
    nneigh = np.empty(nspikes)
    centers = np.empty(nspikes)

    cluster_index = np.empty(nspikes)
    features = get_features(nodo)

    #dc = libcd.get_dc(connect, spikes_id, nspikes, np.float(1.8), points)
    dc = libcd.getDC(connect, features, spikes_id, nspikes, np.float(1.8),
                     points)

    #libcd.cluster_dp(connect, rho, delta, spikes_id,
    #                     cluster_index1, nneigh, centers, dc, points, nspikes, "gaussian")
    libcd.dpClustering(features, nspikes, dc, points, "gaussian", spikes_id,
                       cluster_index, rho, delta)

    templates = []
    spikes = []
    gspikes = []

    for i in range(1, int(cluster_index.max()) + 1):
        template = np.zeros(64, np.float64)
        k = 0
        for j in range(nspikes):
            if cluster_index[j] == i:
                spike = neurodb.neodb.core.spikedb.get_from_db(
                    dbconn,
                    id_block=id_block,
                    channel=channel,
                    id=int(spikes_id[j]))
                signal = spike[0].waveform
                template = template + signal
                gspikes.append(nodo[j])
                k = k + 1
        template = template / k
        templates.append(template)
        spikes.append(gspikes)

    output.put((templates, spikes))

    pass
Ejemplo n.º 4
0
 n_nodos = 1
 
 color = ['bo', 'ro', 'go', 'co', 'ko', 'mo', 'b^', 'r^', 'g^', 'c^', 'k^', 'm^']
 
 nodos = select_nodes(id_project, id_session, channel, n_nodos)
 n = len(nodos[0])
 
 local_density = np.empty(n)
 distance_to_higher_density = np.empty(n)
 cluster_index = np.empty(n)
 nneigh = np.empty(n)
 centers = np.empty(n)
 
 nodo = np.array(nodos[0], np.float64)
 dc = libcd.get_dc(connect, nodo, n, np.float(2), points)
 libcd.getDC(connect, features, spikes_id, nspikes, np.float(1.8), points)
 #dc = 5.83778190613
 #dc = 4.82201528549
 print dc
 
 f = open('distance_to_higher_density.p','rb')
 distance_to_higher_density = np.load(f)
 f.close()
    
 f = open('local_density.p','rb')
 local_density = np.load(f)
 f.close()
 
 rho = local_density
 delta = distance_to_higher_density
 
Ejemplo n.º 5
0
    for x in results:
        for y in x[0]:
            templates.append(y)
        for y in x[1]:
            ids.append(y)

    features_ids = insertFeaturesTemplate(templates, ids)

    features_ids = np.array(features_ids, np.float64)

    nspikes = len(features_ids)
    rho = np.empty(nspikes)
    delta = np.empty(nspikes)
    id_spikes = np.empty(nspikes)
    cluster_index = np.empty(len(features_ids))
    dc = libcd.getDC(connect, features_ids, id_spikes, len(features_ids),
                     np.float(2.0), points)
    libcd.dpClustering(features_ids, len(features_ids), dc, points, "gaussian",
                       id_spikes, cluster_index, rho, delta)

    res = neurodb.features.getFromDB(features_ids, "extra")
    neurodb.features.removeOnDB(features_ids)

    n = len(features_ids)
    max = rho.max()
    max = int(max * 0.06)
    print max
    for j in range(len(delta)):
        if (rho[j] < max):
            delta[j] = 0

    coeficientes1, stats1 = np.polynomial.polynomial.polyfit(rho,