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;
示例#2
0
def plotData(data, iters = [0,1,10,20], name = None):
  #plt.figure(10); plt.clf();
  #plt.plot(np.log(range(1,nn+1)), np.log(rots[3200:3300,:].T))  

  #fig = plt.figure(10); plt.clf();
  n = len(iters);
  n2 = int(np.ceil(n/2.));
  fig, axs = plt.subplots(2, n2, sharex=True, sharey=True);
  for k,i in enumerate(iters):
    #plt.subplot(2,2,k+1);
    w.plotTrajectory(colordata = shiftData(data, i/2, reverse = True)[:,i], ax = axs.flat[k]);
    axs.flat[k].set_title(str(i));
   
  if name is not None:
    fig = plt.gcf();
    fig.canvas.set_window_title(name);
  
  fig = plt.figure(); plt.clf();
  for k,i in enumerate(iters):
    plt.subplot(2,n2,k+1);
    plt.hist(data[~np.isnan(data[:,i]),i], bins = 50)
    plt.title(str(i));
  
  if name is not None:
    fig = plt.gcf();
    fig.canvas.set_window_title(name);
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
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):
  plt.subplot(nr,1,i+1);
  w.plotTrace(shiftData(pe[:,i], s = npath/2));
  plt.title('path entropy radius = %f' % rads[i])


## test on real data

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  
示例#5
0
angs = w.calculateAngles(n = nn);
#plt.figure(10); plt.clf();
#plt.plot(range(1,nn+1), angs[3200:3300,:].T)

plotData(angs, iters = nns, name = 'Angles')



##############
#diffusions
nn = 50;
diffs = w.calculateDiffusionParameter(n=nn, parallel = True)

plt.figure(20); plt.clf();
diffsshift = shiftData(diffs, nn/2, reverse = True);
w.plotTrajectory(colordata = diffsshift[:,0], size = 100)
plt.figure(21); plt.clf();
w.plotTrajectory(colordata = diffsshift[:,0], size = diffsshift[:,-1]* 1000)

plt.figure(22); plt.clf();
w.plotTrajectory(colordata = diffsshift[:,1], size = 60)
#w.plotTrajectory(colordata = -diffsshift[:,-1], size = diffsshift[:,0]* 1000)

#histogram
nbins = 50;
plt.figure(13); plt.clf();
plt.subplot(2,2,1);
plt.hist(diffs[~np.isnan(diffs[:,0]),0], bins = nbins)
plt.subplot(2,2,2);
plt.hist(diffs[~np.isnan(diffs[:,0]),-1], bins = nbins)
  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] = labelsws[ddata[i,0], ddata[i,1]];
  
pred2[~nanids] = pred2nn;
pred2nn.max();

plt.figure(506); plt.clf();
w.plotTrace(ids = shiftData(pred2, delays[d]/2, nan = -1), stage = s)


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))
                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):
    plt.subplot(nr, 1, i + 1)
    w.plotTrace(shiftData(pe[:, i], s=npath / 2))
    plt.title('path entropy radius = %f' % rads[i])

## test on real data

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

nworms = XYdata.shape[0]