def openTets(fn, tets, t0=None, t1=None, lm=None, detectHash=True, chooseSpikes=_motion_): """ Open a file with marks and positions stets if I give """ if lm is None: with open(fn, "rb") as f: lm = _pkl.load(f) f.close() if type(tets) is int: tets = [tets] elif type(tets[0]) is str: inds = [] for i in xrange(len(tets)): inds.append(lm.tetlist.index(tets[i])) tets = inds t0 = 0 if (t0 is None) else t0 t1 = lm.marks.shape[0] if (t1 is None) else t1 if chooseSpikes == _motion_: minds = lm.minds elif chooseSpikes == _all_: minds = _N.array([[t0, t1]]) else: lminds = [] if lm.minds[0, 0] > 0: lminds.append([0, lm.minds[0, 0]]) for i in xrange(lm.minds.shape[0] - 1): lminds.append([lm.minds[i, 1], lm.minds[i+1, 0]]) if lm.minds[-1, 1] > 0: lminds.append([lm.minds[-1, 1], t1]) minds = _N.array(lminds) inds = _N.where((minds[:, 0] >= t0) & (minds[:, 0] <= t1))[0] if detectHash: allnhmks = [] allhmks = [] else: allmarks = [] for tet in tets: marks = [] pos = [] for t in inds: mksl = lm.marks[minds[t, 0]:minds[t, 1], tet] shsl = lm.pos[minds[t, 0]:minds[t, 1]] nn = _N.where(_N.equal(mksl, None) == False)[0] for n in nn: for l in xrange(len(mksl[n])): marks.append(mksl[n][l]) pos.append(shsl[n]) posmarks = _N.empty((len(marks), 5)) posmarks[:, 1:] = _N.array(marks) posmarks[:, 0] = _N.array(pos) if detectHash: # separate the hash nhid, hid, gmms = _fu.sepHashEM(posmarks) nhmks = posmarks[nhid] hmks = posmarks[hid] allnhmks.append(nhmks) allhmks.append(hmks) else: allmarks.append(posmarks) if detectHash: return allnhmks, allhmks, lm, gmms else: return allmarks, lm
def init0(self, pos, mk, n1, n2): """ M total number of clusters MS number of clusters assigned to signal MF number of clusters used for initial fit M - MF If doing touchup, number of clusters to assign to this """ print "init0" oo = self k = oo.k MF = oo.M ### # # parameters of cluster covariance. Becomes prior # priors oo.PR_cov_PSI = _N.tile(_N.eye(k)*0.5, oo.M).T.reshape(oo.M, k, k) oo.PR_cov_nu = _N.ones(oo.M, dtype=_N.int)*(k+1) # parameters of cluster mean. Becomes prior oo.PR_mu_mu = _N.zeros((oo.M, k)) oo.PR_mu_sg = _N.tile(_N.eye(k)*30, oo.M).T.reshape(oo.M, k, k) oo.iPR_mu_sg= _N.linalg.inv(oo.PR_mu_sg) # parameters of cluster weights oo.PR_m_alp = _N.ones(oo.M) * (1./oo.M) _x = _N.empty((n2-n1, k)) _x[:, 0] = pos _x[:, 1:] = mk N = n2-n1 # Gibbs sampling ################ init cluster centers ########################## BINS = 20 bins = _N.linspace(-6, 6, BINS+1) blksz = 20 unonhash, hashsp, gmms = _fu.sepHashEM(_x) #unonhash, hashsp, hashThr = _fu.sepHash(_x) if (len(unonhash) > 0) and (len(hashsp) > 0): labS, labH, clstrs = _fu.emMKPOS_sep(_x[unonhash], _x[hashsp]) elif len(unonhash) == 0: labS, labH, clstrs = _fu.emMKPOS_sep(None, _x[hashsp]) else: labS, labH, clstrs = _fu.emMKPOS_sep(_x[unonhash], None) print "**** clusters sig: %(1)d hash: %(2)d" % {"1" : clstrs[0], "2" : clstrs[1]} MS = 0 if len(labS) > 0: MS = _N.max(_N.unique(labS)) + 1 ################## lab = _N.array(labS.tolist() + (labH + clstrs[0]).tolist()) x = _N.empty((n2-n1, k)) strt = 0 if len(unonhash) > 0: x[0:len(unonhash)] = _x[unonhash] strt = len(unonhash) if len(hashsp) > 0: x[strt:] = _x[hashsp] # now assign the cluster we've found to Gaussian mixtures covAll = _N.cov(x.T) dcovMag= _N.diagonal(covAll)*0.005 print "look at the covariances" for im in xrange(MF): # if lab < 0, these marks not used for init kinds = _N.where(lab == im)[0] # inds if len(kinds) >= 6: # problem when cov is not positive def. oo.smu[0, im] = _N.mean(x[kinds], axis=0) oo.scov[0, im] = _N.cov(x[kinds], rowvar=0) #print "im %(im)d %(c).3e" % {"im" : im, "c" : _N.linalg.det(oo.scov[0, im])} if _N.linalg.det(oo.scov[0, im]) < 0: print len(kinds) print x[kinds] oo.sm[0, im] = float(len(kinds)+1) / (N+MF) else: if len(kinds) > 2: oo.smu[0, im] = _N.mean(x[kinds], axis=0) else: oo.smu[0, im] = _N.mean(x, axis=0) oo.scov[0, im] = covAll*0.125 oo.sm[0, im] = float(len(kinds)+1) / (N+MF)
def openTets(fn, tets, t0=None, t1=None, lm=None, detectHash=True, chooseSpikes=_motion_): """ Open a file with marks and positions stets if I give """ if lm is None: with open(fn, "rb") as f: lm = _pkl.load(f) f.close() if type(tets) is int: tets = [tets] elif type(tets[0]) is str: inds = [] for i in range(len(tets)): inds.append(lm.tetlist.index(tets[i])) tets = inds t0 = 0 if (t0 is None) else t0 t1 = lm.marks.shape[0] if (t1 is None) else t1 if chooseSpikes == _motion_: minds = lm.minds elif chooseSpikes == _all_: minds = _N.array([[t0, t1]]) else: lminds = [] if lm.minds[0, 0] > 0: lminds.append([0, lm.minds[0, 0]]) for i in range(lm.minds.shape[0] - 1): lminds.append([lm.minds[i, 1], lm.minds[i + 1, 0]]) if lm.minds[-1, 1] > 0: lminds.append([lm.minds[-1, 1], t1]) minds = _N.array(lminds) inds = _N.where((minds[:, 0] >= t0) & (minds[:, 0] <= t1))[0] if detectHash: allnhmks = [] allhmks = [] else: allmarks = [] for tet in tets: marks = [] pos = [] for t in inds: mksl = lm.marks[minds[t, 0]:minds[t, 1], tet] shsl = lm.pos[minds[t, 0]:minds[t, 1]] nn = _N.where(_N.equal(mksl, None) == False)[0] for n in nn: for l in range(len(mksl[n])): marks.append(mksl[n][l]) pos.append(shsl[n]) posmarks = _N.empty((len(marks), 5)) posmarks[:, 1:] = _N.array(marks) posmarks[:, 0] = _N.array(pos) if detectHash: # separate the hash nhid, hid, gmms = _fu.sepHashEM(posmarks) nhmks = posmarks[nhid] hmks = posmarks[hid] allnhmks.append(nhmks) allhmks.append(hmks) else: allmarks.append(posmarks) if detectHash: return allnhmks, allhmks, lm, gmms else: return allmarks, lm