def clustdm(cmeth, dm, nclust, cargs={}): if cmeth == 'tree': t = clust.dtree(dm, **cargs) clsts = t.cut(nclust) elif cmeth == 'med': clsts = clust.mediods(dm, nclust, **cargs) else: raise StandardError("don't know clustering method %s" % cmeth) return clsts
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 show_cell_clust(cells, cn, mode='vdps', q=6000, fig=1, save="", suf=''): """ cell: i, mode: DistMode, q: DistQ, fig: i, save: DirName, suf: s -> None (draws in matplotlib figure fig, may write a file) Shows the clustering tree (as show_tree) for each stimulus in each condition, and also the clustering diagram. Draws a figure. If save is non-empty, then also writes a file in that directory, named: "c%i_clust_%s%i%s.png" % (cell, mode, int(q),suf) """ d = cells[cn] f = plt.figure(fig) plt.clf() plt.figtext(0, .98, "%s, mode=%s, q=%.2g" % (cn, mode, q), backgroundcolor='white') labs = stimnames(d) a1 = dist_discrim_incond(d['cond1'], mode, q, len(labs)) a2 = dist_discrim_incond(d['cond2'], mode, q, len(labs)) sb = plt.subplot(121) plt.title("Condition 1") im = vis.dot2img(clust.tree2dot(clust.dtree(a1), labs)) plt.imshow(im) sb.yaxis.set_visible(False) sb.xaxis.set_visible(False) sb = plt.subplot(122) plt.title("Condition 2") im = vis.dot2img(clust.tree2dot(clust.dtree(a2), labs)) plt.imshow(im) sb.yaxis.set_visible(False) sb.xaxis.set_visible(False) f.canvas.draw() if save: save = os.path.expanduser(save) fn = os.path.join(save, "%s_clust_%s%i%s.png" % (cn, mode, int(q), suf)) plt.savefig(fn)
def _tsort(a, l): """ a: DistMat(N), l: N-[ of s -> DistMat(N), N-[ of s creates a new matrix and list of names sorted using tree clustering """ t = clust.dtree(a) co = clust.treesort(t) l2 = [l[i] for i in co] a2 = np.zeros_like(a) for i in range(a.shape[0]): for j in range(a.shape[1]): a2[i, j] = a[co[i], co[j]] return (a2, l2)
def noise_tree(cond, nbs=20, noise=.1, mode='vdps', q=6000, N=14, ctree=True): """ Cond: IntIO Construct a set of <nbs> different clustering trees using a distance matrix calculated with distance function <mode> with parameter <q>. The trees differ because in each of the nbs trials, the DM is perturbed with independent Gaussian noise drawn from N(0, S), where S = <noise>*DM.std(). consensus tree of trees generated by adding noise values to the distance matrix. These values are drawn from a N(0, sd) where sd is noise*dm.std. <N> is the total number of stimuli available (used by the internal call to dist_discrim_incond. If <ctree> is True (default), returns a majority rule consensus tree across the various clusterings. Otherwise, returns a list of the cluster trees If 'mode' is "random_*", then the dm used is a random matrix where * specifies the distribution (it is passed as the first argument to random_dm). Can be used for testing. """ if mode.startswith('random_'): dm = random_dm(mode[7:], N) else: dm = dist_discrim_incond(cond, mode, q, N) nsd = noise * dm.std() trees = [] for _ in range(nbs): dm2 = dm + np.random.normal(0, nsd, dm.shape) dm2 = np.maximum(dm2, 0) trees.append(clust.dtree(dm2)) if ctree: return clust.mrtree(trees) else: return trees
def run(self, pars, out, messages): dm = np.array(pars['dm']) t = clust.dtree(dm) out['tree'] = clust.tree2tup(t)