Exemple #1
0
def clusterTrains2(ds, cost=.1, avoid=.2, require=.2, simthresh=.5, dpath='/', newpath='/cluster'):
	'''attempts to cluster spikes in the labeledevent instance at dpath. Cost is the cost per ms to move a spike and is required because spike correspondance is determined by converting between spike trains using a Victor algorithm. "avoid" is a fraction of spike trains comparisons that are not considered. These are the comparisons with the longest pairwise distances. require is a fraction of the total presentations in which a spike must exist, and simthresh is the level of group similarity required to assign spikes to the same event (high values will result in more unique events, lower values in more permissive event groups).'''
	d=ds.getSubData(dpath)
	cost=cost*1000.0/d.fs()
	dat=d.getData()
	trainIDs=unique1d(dat[:,1])
	nst=trainIDs.shape[0]
	dist=zeros((trainIDs.shape[0],trainIDs.shape[0]), float32)
	st=[]
	trans={}
	ntr =0
	for i, l in enumerate(trainIDs):
		st.append(take(dat[:,0], nonzero1d(dat[:,1]==l)))
		for j in range(0,i):
			dist[j,i], trans[(j,i)]=sc.transformSpikes(st[j], st[i], cost)
			ntr+=trans[(j,i)].shape[0]
	td=dist.sum(1)
	order=argsort(td)
	trainIDs=trainIDs[order]
	if avoid:
		uw=dist.min()
		mw=dist.max()
		avoid=mw-(mw-uw)*avoid
	ia=0
#	foo={'order':order, 'Labels':trainIDs, 'spikes': st, 'avoid':avoid, 'trans':trans, 'dist':dist}
# 	import cPickle
# 	f=file('foo.pickle', 'wb')
# 	cPickle.dump(foo, f)	
	if require:
		require=round(require*nst)
	b=clust.combine(trans, dist, avoid, nst)
	c=clust.getequiv(b, require)
	#c2=clust.fixcor(c, require)
	sev=lambda x, y, z: clust.simgrp(x, y, z, simthresh)
	id=clust.group(c,sev)
	id=clust.unravel(id, nst)
	clust.resolve(id, st)
	head={'SampleType':'labeledevents', 'Source':dpath, 'Cost':cost, 'SamplesPerSecond':ds.getSubData(dpath).fs(), 'Labels':trainIDs, 'avoid':avoid, 'require':require, 'simthresh':simthresh} 
	ds.createSubData(newpath, id, head, True)
Exemple #2
0
def clusterTrains1(ds, dpath='/distances', newpath='/cluster', mode='cluster'):
	'''attempts to caluculate the minimal perturbations required to convert a single most representative spike train in the labeled event data at <dpath> into every other spike train. These transformations are then used to relabel the spikes in each spike train. The data element written at newpath is a labeledevents set with two label columns'''
	dists = ds.getSubData(dpath)
	cost=float(dists.attrib('Cost'))
	dpathSpikes=str(dists.attrib('Source'))
	labs=array(map(int, dists.attrib('Labels')))
	dists=dists.getData()
	m=argmin(dists.sum(1))
	print m
	spikes=ds.getSubData(dpathSpikes).getData()
	#labs=unique1d(spikes[:,1])
	rows=range(dists.shape[0])
	st=spikes[nonzero1d(spikes[:,1]==labs[m]),0]
	spikelabs=[]
	traverse=[m]
	rows.remove(m)
	spikelabs.append(column_stack([st, arange(st.shape[0])]))
	maxlab=st.shape[0]-1
	while rows:
		#get best transform
		#print dists.shape, rows, traverse
		if mode=='cluster':
			sd=take(take(dists, array(rows), 0), array(traverse),1)
			mi=argmin(sd)
			mi=unravel_index(mi, sd.shape)
			if len(mi)==1:
				mi=(mi[0], 0)
		else:
			sd=take(dists[traverse[0]], array(rows))
			mi=argmin(sd)
			mi=(mi,0)
		print mi, traverse[mi[1]], rows[mi[0]]
		#best transform is traverse[mi[1]] to rows[mi[0]]
		st= spikes[nonzero1d(spikes[:,1]==labs[traverse[mi[1]]]),0]
		tt= spikes[nonzero1d(spikes[:,1]==labs[rows[mi[0]]]),0]
		#calculate transform
		c, m=sc.transformSpikes(st, tt, cost)
		mf=st[m[:,0]]
		mt=tt[m[:,1]]
		a=setdiff1d(tt, mt)
		if a.shape[0]!=0:
			nl=arange(maxlab+1, maxlab+1+a.shape[0]).astype(tt.dtype)
			maxlab=nl.max()
		sl=zeros_like(tt)-1
		sl[tt.searchsorted(a)]=nl
		ii=tt.searchsorted(mt)
		#print sl.shape, m.shape, ii.shape, ii.max(), ii.min()
		jj=st.searchsorted(mf)
		sl[ii]=spikelabs[mi[1]][jj,1]
		#store event labels 
		traverse.append(rows[mi[0]])
		rows.remove(rows[mi[0]])
		spikelabs.append(transpose(vstack([tt, sl])))
	newlabs=[]
	spikes=zeros((spikes.shape[0], 3), spikes.dtype)
	index=0
	for i in range(len(spikelabs)):
		newlabs.append(labs[traverse[i]])
		ns=spikelabs[i].shape[0]
		spikes[index:index+ns,0]=spikelabs[i][:,0]
		spikes[index:index+ns,1]=i
		spikes[index:index+ns,2]=spikelabs[i][:,1]
		index+=ns
	head={'SampleType':'labeledevents', 'Source':dpathSpikes, 'Cost':cost, 'SamplesPerSecond':ds.getSubData(dpathSpikes).fs(), 'Labels':newlabs} 
	ds.createSubData(newpath, spikes, head, True)