def load_all(self, removedrift=False, showprogress=True): """Returns all positions from all trajectories""" trajpos = [np.zeros([len(tr), 3]) for tr in self.trajs] if showprogress: pro = ProgressBar(self.size) for t, name in self.enum(): pos = np.loadtxt(name, skiprows=2) p2tr = self.p2tr(t) for p, tr in enumerate(p2tr): trajpos[tr][t - self.starts[tr]] = pos[p] if showprogress: pro.animate(t) if removedrift: #compute drift using all trajectories drift = np.zeros((self.size,3)) nb = np.zeros((self.size), int) for tr, start in zip(trajpos, self.starts): drift[start+1:start + len(tr)] += tr[1:] - tr[:-1] nb[start+1:start + len(tr)] += 1 drift /= np.maximum(1, nb)[:,None] drift = np.cumsum(drift, axis=0) #remove drift from each trajectory for tr, start in enumerate(self.starts): trajpos[tr] -= drift[start:start + len(trajpos[tr])] return trajpos
def never_closer(self, L=3, mintrajlength=None, showprogress=True): """All the broken bonds (in term of trajectories) with a on graph distance that stays larger than L, associated with the last time they break""" try: ret = [] for t, name in self.enum('_L%d'%L, ext='nevc'): if os.stat(name).st_size: ret.append(np.loadtxt(name, dtype=int)) else: ret.append(np.array((0,2), dtype=int)) except IOError: pass if showprogress: pro = ProgressBar(self.size) result = dict() for t, name in self.enum(ext='bonds'): try: bonds1 = np.atleast_2d(np.loadtxt(name, dtype=int)) except UserWarning: bonds1 = np.zeros([0,2], int) p2tr1 = self.p2tr(t) if t>0: for path in broken_bonds_path(bonds0, bonds1, p2tr0, p2tr1): if len(path)<=L: continue a, b = sorted([path[0], path[-1]]) if mintrajlength is not None and min(len(self.trajs[u]) for u in [a,b]) <= mintrajlength: continue result[(a,b)] = t #create or replace if was already broken before #graph of the bonds between trajectories at t1 g = nx.Graph() g.add_nodes_from(p2tr1) g.add_edges_from(p2tr1[bonds1]) #remove all bonds that are closer than L at t1 existing = [[a,b] for a,b in result.iterkeys()] for path in shortest_path(g, existing): if len(path) <= L: a,b = sorted([path[0], path[-1]]) result.pop((a,b),-1) bonds0 = bonds1 p2tr0 = p2tr1 if showprogress: pro.animate(t) #translate as a list of arrays of bonds, an array per time tdl = [ np.zeros([nbb, 2], int) for nbb in np.histogram(result.values(), bins=np.arange(0, self.size+1))[0] ] ns = np.zeros(self.size, int) for bond, t in result.iteritems(): tdl[t][ns[t]] = bond ns[t] += 1 #save a cache for t, name in self.enum('_L%d'%L, ext='nevc'): np.savetxt(name, tdl[t], fmt='%d') return tdl
parser = argparse.ArgumentParser(description='Convert a HDF5 format compatible with trackpy.PandasHDFStoreSingleNode to coordinates and trajectory data.') parser.add_argument('h5name', help='HDF5 file name') parser.add_argument('--out', help='The folder where to save the data. By default the same name as HDF5 file, without extension.') args = parser.parse_args() h5name = args.h5name print(h5name) if args.out is None: args.out = os.path.splitext(h5name)[0] print('to %s'%args.out) os.mkdir(args.out) basename = os.path.join(args.out, os.path.split(os.path.basename(h5name))[0]) with tp.PandasHDFStoreSingleNode(h5name) as s: nbzeros = len('%d'%(len(s)-1)) datfile = +'_t%0{:d}d.dat'.format(nbzeros) p2trfile = +'_t%0{:d}d.p2tr'.format(nbzeros) pro = ProgressBar(len(s)) for t, frame in s: pos = np.vstack((np.zeros(2,3), frame.as_matrix(['x','y','z']))) pos[0] = 1, len(pos)-2, 1 np.savetxt(datfile%t, pos, fmt='%g') np.savetxt(p2trfile%t, frame.asmatrix(['particle'], fmt='%d') #if t==0: # linker = Linker(len(frame)) #else: # linker.loadFrame(frame.asmatrix(['particle']) pro.animate(t) #TODO save the linker to a traj file
'The file.h5 where to save the data. By default the same name as trajectory file, one level below in the directory tree.' ) args = parser.parse_args() trname = args.trname print(trname) x = xp.Experiment(trname) if args.out is None: args.out = os.path.join( os.path.split(x.path)[0], os.path.splitext(x.trajfile)[0] + '.h5') print('to %s' % args.out) assert x.size < 2**15, "Too many time steps, use larger data types" assert x.nb_trajs < 2**31, "Too many trajectories, use larger data types" pro = ProgressBar(x.size) with tp.PandasHDFStoreSingleNode(args.out) as s: for t, name in x.enum(): pro.animate(t) d = { x: c for x, c in zip( 'xyz', np.loadtxt(name, skiprows=2, unpack=True, dtype=np.float16)) } d['frame'] = np.full(d['x'].shape, t, dtype=np.int16) d['particle'] = x.p2tr(t).astype(np.int32) s.put(DataFrame(d)) pro.animate(x.size)