def classifyWorm(labels, wid, d, s, nbins=256, verbose=True): XYwdata = XYdata[wid].copy() w = wd.WormData(XYwdata[:, 0:2], stage=XYwdata[:, -1], valid=XYwdata[:, 0] != 1, label=('x', 'y'), wid=wid) w.replaceInvalid() ds = w.calculateDistances(n=delays[d] + 1, stage=s) rs = w.calculateRotations(n=delays[d] + 1, stage=s) ddata = np.vstack([np.log(ds[:, -1]), (rs[:, -1])]).T #gmmdata = np.vstack([dists[:,j], (rots[:,j])]).T #ddata.shape nanids = np.logical_or(np.any(np.isnan(ddata), axis=1), np.any(np.isinf(ddata), axis=1)) ddata = ddata[~nanids, :] #ddata.shape pred2 = -np.ones(rs.shape[0]) pred2nn = pred2[~nanids] pred2nn.shape for i in range(2): ddata[:, i] = ddata[:, i] - ddata[:, i].min() ddata[:, i] = (ddata[:, i] / ddata[:, i].max()) * (nbins - 1) ddata = np.asarray(ddata, dtype=int) for i in range(2): ddata[ddata[:, i] > (nbins - 1), i] = nbins - 1 for i in xrange(ddata.shape[0]): pred2nn[i] = labels[ddata[i, 0], ddata[i, 1]] pred2[~nanids] = pred2nn #pred2nn.max(); if verbose: plt.figure(506) plt.clf() w.plotTrace(ids=shiftData(pred2, delays[d] / 2, nan=-1), stage=s) if verbose > 2: rds = w.calculateRotations(n=delays[d] + 1, stage=s) plt.figure(510) plt.clf() w.plotDataColor(data=shiftData(rds[:, -1], delays[d] / 2, nan=-1), c=pred2, lw=0, s=20, stage=s, cmap=cm.rainbow) dts = w.calculateDistances(n=delays[d] + 1, stage=s) plt.figure(510) plt.clf() w.plotDataColor(data=dts[:, -1], c=pred2, lw=0, s=20, stage=s, cmap=cm.rainbow) plt.figure(507) plt.clf() w.plotTrajectory(stage=s, colordata=shiftData(rds[:, -1] / (delays[d] + 1) / np.pi, delays[d] / 2, nan=-.1)) dist2 = w.calculateLengths(n=200) plt.figure(511) plt.clf() w.plotDataColor(data=dist2[:, -1], c=pred2, lw=0, s=20) return pred2
import numpy as np import matplotlib.pyplot as plt import wormdata as wd #make face path #random motion n1 = 400 n2 = 400 n = n1 + n2 path = np.zeros((n, 2)) path[:n1, :] = np.random.rand(n1, 2) path[n1:n, :] = 0.1 * np.random.rand(n1, 2) + np.outer(np.linspace(0, 100, n2), [0, 1]) w = wd.WormData(path, stage=np.zeros(n), label=('x', 'y'), wid=1) w.replaceInvalid() plt.figure(1) plt.clf() w.plotTrajectory(size=50) nn = 20 diffs = w.calculateDiffusionParameter(n=nn, parallel=False) plt.figure(2) plt.clf() diffsshift = np.zeros_like(diffs) diffsshift[nn / 2:, :] = diffs[:-nn / 2, :] diffsshift[:nn / 2.:] = np.NaN w.plotTrajectory(colordata=diffsshift[:, 0], size=diffsshift[:, -1] * 1000)
basedir = '/home/ckirst/Science/Projects/CElegansBehaviour/'; filename = os.path.join(basedir, 'Experiment/individuals_N2_X_Y.mat') data = io.loadmat(filename); XYdata = data['individual_X_Y'][0]; print XYdata.shape print XYdata[0].shape wid = 71; #wid = 1; XYwdata = XYdata[wid].copy(); reload(wd) w = wd.WormData(XYwdata[:,0:2], stage = XYwdata[:,-1], valid = XYwdata[:,0] != 1, label = ('x', 'y'), wid = wid); w.replaceInvalid(); plt.figure(1); plt.clf(); w.plotData(); plt.figure(2); plt.clf(); w.plotTrajectory() plt.figure(3); plt.clt(); w.plotTrace(ids = w.stage()) ############################################################################## # small test set reload(wd)
import wormdata as wd ## test work data reload(wd) wid = 1 xyd = np.array( [[0, 0, 0, 0, 1, 1, 1, 2, 2.1, 2.2, 2.3, 2.1, 2.4, 2.6], [0, 0.1, 1, 1.1, 1.2, 2, 3, 3.1, 3.2, 3.1, 3.3, 3.4, 3.1, 3.7]], dtype=float).T nn = xyd.shape[0] w = wd.WormData(xyd, stage=np.ones(nn), valid=np.ones(nn, dtype=bool), label=('x', 'y'), wid=wid) w.replaceInvalid() w.plotTrajectory() rads = [0.1, 0.5, 1., 2] npath = 4 pe = w.calculatePathEntropy(radius=rads, n=npath) print pe plt.clf() nr = pe.shape[1] for i in range(nr):