Esempio n. 1
0
 def doGrid(self, method=None):
   if method is None:
     feal = self.E.all_feal()
   else:
     self.space = method
     feal = self.E.all_feal(force=True, method=method)
   self.kdg = KDGrid(K=10, split=5)
   self.kdg.insert([i[10:] for i in feal])
Esempio n. 2
0
class Reweight:
  def __init__(self, expr, space='cw'):
    self.E = expr
    self.space = 'cw'
    self.last_method = None
    self.kdg = None
    self.hc = None
    self.fealcov = None
    self.covar = None

  def doGrid(self, method=None):
    if method is None:
      feal = self.E.all_feal()
    else:
      self.space = method
      feal = self.E.all_feal(force=True, method=method)
    self.kdg = KDGrid(K=10, split=5)
    self.kdg.insert([i[10:] for i in feal])

  def doCovar(self, winsize=.1, slide=.05):
    # Calculate Covariance matrices over all temporal data IAW given window param
    self.covar = []
    self.fealcov  = []
    for i, tr in self.E.trlist.items():
      if i % 100 == 0:
        print(i)
      cov = dr.calc_covar(tr.xyz, winsize, 1, slide=slide)
      if self.space in ['ds', 'dsw']:
        X, C = dr.distance_space(tr), self.E.cent_ds
      else:
        X, C = tr.xyz, self.E.cent_c
      wgt = self.E.cw if self.space in ['cw', 'dsw'] else [1,1,1,1,1]
      rms = calc_rmsd(X, C, weights=wgt)
      W = int(winsize * 1000)
      S = int(slide * 1000)
      feal = [np.mean([FL.feal.atemporal(i) for i in rms[st:st+W]], axis=0) for st in range(0, len(tr.xyz), S)]
      for n in range(min(len(cov), len(feal))):
        self.covar.append(cov[n])
        self.fealcov.append(feal[n])

    # Extract diagonals as a vector and solve Kernel PCA

  def doTree(self, pc=4, ktype='rbf', leafsize=100, maxdepth=6, split='middle'):
    diag = np.array([np.diag(i) for i in self.covar])
    kpca = PCAKernel(pc, ktype)
    kpca.solve(diag)
    gdata = kpca.project(diag)

    # Create KDTree over reduced-dim subspace (4-PCs)
    gtree  = KDTree(leafsize, maxdepth, gdata, split) # Or Max Gap
    self.hc = gtree.getleaves()

  def doHeatmap(self, title, DS=True):

    # Save grid hcubes which actually have sufficient data points
    nodeA_size = [len(i) for i in self.kdg.grid]
    nodeA = [i for i, size in enumerate(nodeA_size) if size > 10]
    nodeB = list(self.hc.keys())
    idxB  = {k: i for i, k in enumerate(nodeB)}

    # For each HCube (among clustered temporal data):
    for k in nodeB:
      self.hc[k]['reweight'] = {}
      for cov in self.hc[k]['elm']:
        probe = self.kdg.toindex(self.fealcov[cov][10:])
        if probe not in self.hc[k]['reweight']:
          self.hc[k]['reweight'][probe] = 0
        self.hc[k]['reweight'][probe] += 1
        if probe not in nodeA:
          nodeA.append(probe)

    idxA  = {k: i for i, k in enumerate(nodeA)}

    # Map projection to edges (and subsequent heatmap chart)
    edge = []
    projmap = np.zeros(shape=(len(nodeA), len(nodeB)))
    for kB in nodeB:
      for kA, proj_cnt in self.hc[kB]['reweight'].items():
        edge.append((kA, kB, proj_cnt))
        A = idxA[kA]
        B = idxB[kB]
        projmap[A][B] = proj_cnt

    xlabel = 'SOURCE: Temporal Windows (Covariance -> KPCA -> KDTree)'
    ylabel = 'DEST: ATemporal Data (K-D Grid w/feature Landscape, 0-1..3-4 vals)'

    Asize = [nodeA_size[i] for i in nodeA]
    proj_total = [int(i) for i in np.sum(projmap, axis=1)]
    Bsize = [self.hc[k]['count'] for k in nodeB]
    alabel = ['#%04d/ %5d/ %3d' % x for x in zip(nodeA, Asize, proj_total)]
    blabel = ['%6s/ %d' % x for x in zip(nodeB, Bsize)]
    pmap_bal_row_norm = np.nan_to_num(projmap / np.linalg.norm(projmap, axis=-1)[:, np.newaxis]).T
    pmap_bal_col_norm = np.nan_to_num(projmap.T / np.linalg.norm(projmap.T, axis=-1)[:, np.newaxis])


    P.heatmap(projmap, alabel, blabel, title, ylabel=ylabel, xlabel=xlabel)
    P.heatmap(pmap_bal_col_norm.T, alabel, blabel, title+'_NormCol', ylabel=ylabel, xlabel=xlabel)
    P.heatmap(pmap_bal_row_norm.T, alabel, blabel, title+'_NormRow', ylabel=ylabel, xlabel=xlabel)  

  def drawLandscape(self, idxList, title):
    feal = np.mean([self.E.feal_list[i] for i in idxList], axis=0)
    var  = np.std([self.E.feal_list[i] for i in idxList], axis=0)
    P.feadist(feal, title, err=var, pcount=len(idxList), norm=10, negval=True)

  def drawIndex(self, idxNum):
    self.drawLandscape(self.kdg.index[idxNum], 'FeatLand_%d'%idxNum)

  def drawHC(self, hcKey):
    self.drawLandscape(self.hc[hcKey]['elm'], 'FeatLand_%s'%hcKey)