individual_N2_X_Y_cluster_stage_watershed.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:CElegansBehaviour 作者: ChristophKirst 项目源码 文件源码
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;


#assume classes to be 0...N
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号