def clustm(d, nc, mode='med', npass=1000): for c in dconds(d): dm = d[c]['dm'] if mode == 'med': cd = clust.kmedoids(dm, nc, npass)[0] elif mode == 'tree': cd = clust.dtree(dm).cut(nc) cids = list(set(cd)) cid = [] for id in cd: cid.append(cids.index(id)) d[c]['cid'] = cid
def clustmmr_med(d, q, ncr=(1, 5), bits=8, dmeth='vdps', npass=1000, ponly=False): ''' Check the response encoded in d for multiple modes. d should be a "condition document": e.g. it should have keys "evts" and "stims", and will typically be equivalent to doc[condX] for some X, if doc is a document returned by one of the test data generators in this module, such as nburst or nscatter. The test is performed by clustering the "evts" (this pays no attention to the values in "stims"), using the kmedoids approach applied to a distance matrix. The distance matrix is calculated with a precision of "bits", using a distance function specified by "dmeth", and using the precision parameter q "ncr" specifies a range of number of clusters to test (it is a tuple of (min, max), the centers tested will be range(ncr[0], ncr[1]+1)). "npass" is used internally by the clustering algorithm. The return value is a list of len(range(ncr[0], ncr[1]+1)) tuples. Each tuple contains (L, P, E), where L is a list of the cluster centers, P is an array giving the partition (the integer ID of the cluster center associated to each response in "evts"), and E is the expectation value of the distance from an element to its associated center. If ponly is True, the return value is only a list of partitions, rather than (L, P, E) tuples. ''' dm = distance(d['evts'], None, q, dmeth) if bits: dm = bitenc(dm, bits) out = [] for nc in range(ncr[0], ncr[1] + 1): part, err = clust.kmedoids(dm, nc, npass)[:2] cents = [d['evts'][i] for i in getmedoids(part, dm)] out.append((cents, part, err)) if ponly: return [o[1] for o in out] else: return out