def _extract_phi6_values(phi6_key,conn,fr_num): '''Takes in a phi6 computation number, assumed to be a length one tuple as comes out of the sql function the sql connection, and a frame number''' (fname,iden_key,dset_key) = conn.execute("select fout,iden_key,dset_key from phi6" + " where comp_key = ?", phi6_key).fetchone() f = h5py.File(fname,'r') ns = f[ff(fr_num)][fd('neighborhood_size',phi6_key[0])][:] x = f[ff(fr_num)][fd('x',iden_key)][:] # hack to deal with bug in size of output vectors, now fixed in the c++ if(x.shape != ns.shape): if(x.shape > ns.shape and len(x.shape) ==1): ns_full = np.append(ns,-1*np.ones(np.array(x.shape) - np.array(ns.shape))) else: ns_full = ns x = x[ns>0] y = f[ff(fr_num)][fd('y',iden_key)][:] y = y[ns_full>0] phi = f[ff(fr_num)][fd('scaler_order_parameter',phi6_key[0])] phi = phi[ns>0] f.close() del f return x,y,phi,ns[ns>0]
def _plot_file_frame_nsize(key,conn,fr_num,fnameg=None): ''' ''' (fname,p_comp) = conn.execute("select fout,comp_key from comps where function = 'phi6' and dset_key = ?;",(key,)).fetchone() (comp_number,) = conn.execute("select comp_key from comps where function = 'Iden' and fout = ?",(fname,)).fetchone() (sname,stype,temp) = conn.execute("select sname,dtype,temp from dsets where key = ?",(key,)).fetchone() f = h5py.File(fname,'r') x = f[ff(fr_num)][fd('x',iden_key)] x = f[ff(fr_num)][fd('y',iden_key)] ns = f[ff(fr_num)][fd('neighborhood_size',iden_key)] print np.mean(ns) # check interactive plotting and turn it off istatus = plt.isinteractive(); print istatus if istatus:plt.ioff() leg_hands = [] leg_strs = [] fig = plt.figure() ax = fig.add_axes([.1,.1,.8,.8]) ax.hold(True) #ax.set_aspect('equal') # ax.scatter(x,y,c=ns) ax.hist(ns,bins=range(0,10)) ax.set_title(sname + " temp: " + str(temp)) # plt.plot(x,y,'ro') if not fnameg == None: f_path = '/home/tcaswell/python/figures/' + sname + '/' if not os.path.isdir(f_path): os.makedirs(f_path) fnameg = f_path + str(key) + '_phi6.eps' fig.savefig(fnameg) if istatus: print "displaying figure" plt.ion() plt.show() else: print "closing figure" plt.close(fig)
def testing(phi6_key,conn,fr_num): '''Takes in a phi6 computation number, assumed to be a length one tuple as comes out of the sql function the sql connection, and a frame number''' (fname,iden_key,dset_key) = conn.execute("select fout,iden_key,dset_key from phi6" + " where comp_key = ?", phi6_key).fetchone() f = h5py.File(fname,'r') ns = f[ff(fr_num)][fd('neighborhood_size',phi6_key[0])][:] x = f[ff(fr_num)][fd('x',iden_key)][:] f.close() del f return (x.shape,ns.shape)
def new_track_density(track_key,hist_dims,conn): """This makes a 2D histogram of where new tracks are found in the sample. This is useful to check if there are regions of the sample that are tracking badly""" # extract all of the md needed to look up the data (fname,iden_key,track_key,dset_key) = conn.execute("select fout,iden_key,comp_key,dset_key from tracking where comp_key = ?", track_key).fetchone() print fname F = h5py.File(fname,'r') print F.keys()[:5] try: start_plane = F[fd('tracking',track_key)]['start_plane'][:] start_part = F[fd('tracking',track_key)]['start_particle'][:] print len(start_plane) # figure out the right size to make the array dims = F.attrs['dims'] print dims # make data collection object hist2D_ac = Hist2D_accumlator(dims,hist_dims) # loop over the heads of track index and hash result cur_plane = None cur_x = None cur_y = None temp = 0 fr_count = 0 for plane,part in zip(start_plane,start_part): if not plane == cur_plane: cur_plane = plane cp = F[ff(cur_plane)] cur_x = cp[fd('x',iden_key)] cur_y = cp[fd('y',iden_key)] temp += cp.attrs['temperature'] fr_count += 1 hist2D_ac.add_point( (cur_x[part], cur_y[part]) ) pass except ValueError,er: print ff(cur_plane)
def extract_region(track_comp_key,region,init_frame,conn): ''' Extracts the tracks in a given region starting from the frame init_frame region = [x_offset y_offset x_len y_len] ''' # make sure track id is valid # extract comp_id (fin,dset_key,iden_key) = conn.execute("select fout,dset_key,iden_key from tracking where" + " comp_key=?", (track_comp_key,)).fetchone() # open file F = h5py.File(fin,'r') tc = Track_shelf() try: # extract list of particles in ROI x = F[ff(init_frame)][fd('x',iden_key)][:] y = F[ff(init_frame)][fd('y',iden_key)][:] tc.dtime = F[ff(init_frame)].attrs['dtime'] tc.sp_scale = gen.extract_spatial_calibration(F[ff(init_frame)]) ind = matplotlib.mlab.find((x>region[0]) * (y>region[1]) \ * (x<(region[0] + region[2] ) )*( y<(region[1] + region[3]))) DW = data_wrapper(F,iden_key,track_comp_key) # loop over particles in region and extract tracks tc.tracks = [extract_track(DW,init_frame,i,track(init_frame)) for i in ind] return tc finally: # close hdf file and clean up F.close() del F
def _extract_tracks(F,start_plane,start_indx,iden_key,track_key): frame_num = start_plane g_x = F[ff(frame_num)][fd('x',iden_key)] g_y = F[ff(frame_num)][fd('y',iden_key)] g_nxt = F[ff(frame_num)][fd('next_part',track_key)] trks = [[(g_x[s],g_y[s],g_nxt[s])] for s in start_indx] frame_num += 1 while ff(frame_num) in F.keys(): g_x = F[ff(frame_num)][fd('x',iden_key)] g_y = F[ff(frame_num)][fd('y',iden_key)] g_nxt = F[ff(frame_num)][fd('next_part',track_key)] for t in trks: s = t[-1][-1] if s != -1: t.append((g_x[s],g_y[s],g_nxt[s])) frame_num += 1 return trks
def get_cords(self,frame_num): return (self.F[gen.ff(frame_num)][gen.fd('x',self.cnum)], self.F[gen.ff(frame_num)][gen.fd('y',self.cnum)])
def coarse_grain_dedrift(track_key,conn,frame_start,forward_step,grid_size): """Coarse grains tracking data to detect anisotropic drift. This version is slower because it computes the cumulative displacement as it goes instead of using the MD in the tracking file. This needs to be done because the was a bug that did not compute the displacement.""" # sql stuff (iden_key,fout) = conn.execute("select iden_key,fout from tracking inner join comps on tracking.comp_key = comps.comp_key where " + "tracking.comp_key = ?", track_key).fetchone() try: # open file F = h5py.File(fout,'r') # extract the data dimensions dims = F.attrs['dims'][:] hash_dims = dims//grid_size +1 # define a local has function def hash_fun(x,y): '''Local hash function ''' return int(x//grid_size + (y//grid_size )*hash_dims[0]) # set up data structures cg_vectors = [np.array([0,0]) for i in range(0,hash_dims.prod())] cg_count = np.zeros(hash_dims.prod()) # extract all of the relevant data try: start_frame = F[ff(frame_start)] end_frame = F[ff(frame_start+forward_step)] init_pos = zip(start_frame[fd('x',iden_key)][:], start_frame[fd('y',iden_key)][:], ) init_next_part = start_frame[fd('next_part',track_key[0])][:] final_pos = zip(end_frame[fd('x',iden_key)][:], end_frame[fd('y',iden_key)][:], ) drift_val = (start_frame.attrs[fd('cumulative_displacement',track_key[0])][:] - end_frame.attrs[fd('cumulative_displacement',track_key[0])][:]) except Exception: raise finally: del start_frame del end_frame print F next_part = [] poses = [] for j in range(0,forward_step): frame_tmp = None try: frame_tmp = F[ff(frame_start + j + 1)] next_part.append(frame_tmp[fd('next_part',track_key[0])][:]) poses.append(zip(frame_tmp[fd('x',iden_key)][:], frame_tmp[fd('y',iden_key)][:], )) del frame_tmp except Exception: print ff(frame_start + j + 1) print F print F.keys() raise finally: # clean up hdf F.close() del F # loop over particles in start frame disp_sum = [(0,0) for j in range(0,forward_step-1)] plane_count = np.zeros(forward_step-1) for pos,nxt_p in zip(init_pos,init_next_part): die_flag = False prev_pos = pos if nxt_p == -1: continue for j in range(0,forward_step-1): if nxt_p == -1: die_flag = True break try: tp = poses[j] tmp_pos = tp[nxt_p] disp_sum[j] = disp_sum[j] + ( np.array(tmp_pos) -np.array(prev_pos)) plane_count[j] +=1 prev_pos = tmp_pos except Exception: print j,nxt_p nxt_p = next_part[j][nxt_p] if die_flag or nxt_p == -1: continue fn_pos = final_pos[nxt_p] disp = np.array(fn_pos) - np.array(pos) n = hash_fun(*pos) try: cg_vectors[n] = cg_vectors[n] + disp cg_count[n] += 1 except Exception: print n drift_val = np.sum([v/c for (v,c) in zip(disp_sum,plane_count)],0) print drift_val print np.mean([(v)/c for (v,c) in zip( cg_vectors,cg_count)],0) # average the vectors cg_avg = [(v)/c-drift_val for (v,c) in zip( cg_vectors,cg_count)] # split up the results u,v = zip(*cg_avg) x,y = np.meshgrid(grid_size*(np.arange(0,hash_dims[0]) + .5),grid_size*(np.arange(0,hash_dims[1]) + .5)) return x,y,np.array(u),np.array(v),cg_count
def coarse_grain(track_key,conn,frame_start,forward_step,grid_size): """Coarse grains tracking data to detect anisotropic drift """ # sql stuff (iden_key,fout) = conn.execute("select iden_key,fout from tracking inner join comps on tracking.comp_key = comps.comp_key where " + "tracking.comp_key = ?", track_key).fetchone() # open file F = h5py.File(fout,'r') # extract the data dimensions dims = F.attrs['dims'][:] hash_dims = dims//grid_size +1 # define a local has function def hash_fun(x,y): '''Local hash function ''' return int(x//grid_size + (y//grid_size )*hash_dims[0]) # set up data structures cg_vectors = [np.array([0,0]) for i in range(0,hash_dims.prod())] cg_count = np.zeros(hash_dims.prod()) try: # extract all of the relevant data start_frame = F[ff(frame_start)] init_pos = zip(start_frame[fd('x',iden_key)][:], start_frame[fd('y',iden_key)][:], ) init_next_part = start_frame[fd('next_part',track_key[0])][:] end_frame = F[ff(frame_start+forward_step)] final_pos = zip(end_frame[fd('x',iden_key)][:], end_frame[fd('y',iden_key)][:], ) next_part = [] for j in range(0,forward_step-1): frame_tmp = F[ff(frame_start + j + 1)] next_part.append(frame_tmp[fd('next_part',track_key[0])][:]) del frame_tmp drift_val = (-start_frame.attrs[fd('cumulative_displacement',track_key[0])][:] + end_frame.attrs[fd('cumulative_displacement',track_key[0])][:]) del start_frame del end_frame finally: # clean up hdf F.close() del F # loop over particles in start frame for pos,nxt_p in zip(init_pos,init_next_part): die_flag = False if nxt_p == -1: continue for j in range(0,forward_step-1): if nxt_p == -1: die_flag = True break nxt_p = next_part[j][nxt_p] if die_flag or nxt_p == -1: continue fn_pos = final_pos[nxt_p] disp = np.array(fn_pos) - np.array(pos) n = hash_fun(*pos) try: cg_vectors[n] = cg_vectors[n] + disp cg_count[n] += 1 except Exception: print n # average the vectors cg_avg = [v for (v,c) in zip( cg_vectors,cg_count)] print drift_val print cg_vectors[:10] cg_avg = [(v )/c - drift_val for (v,c) in zip( cg_vectors,cg_count)] print cg_avg[:10] # split up the results u,v = zip(*cg_avg) x,y = np.meshgrid(grid_size*(np.arange(0,hash_dims[0]) + .5), grid_size*(np.arange(0,hash_dims[1]) + .5)) return x,y,u,v,cg_count
def population_sort_tracks(track_key,conn,frame_start,frame_step, cut_off): """This function takes in a track_key, a connection, and a displacement cut off. Three lists are return: two lists of tuples that pair initial position, final position, and displacement of each track that is present in frame frame_start and lasts until at least frame_start + frame_step corresponding to above and below the cut off. A list of the particles that are present in frame_start and do not last long enough is ruterns""" # defs short_list = [] long_list = [] die_list = [] # sql stuff (iden_key,fout) = conn.execute("select iden_key,fout from tracking inner join comps on tracking.comp_key = comps.comp_key where " + "tracking.comp_key = ?", track_key).fetchone() # open file F = h5py.File(fout,'r') try: # extract the initial position data frame_tmp = F[ff(frame_start)] init_pos = zip(frame_tmp[fd('x',iden_key)][:], frame_tmp[fd('y',iden_key)][:], ) track_id = frame_tmp[fd('track_id',track_key[0])][:] init_next_part = frame_tmp[fd('next_part',track_key[0])][:] del frame_tmp frame_tmp = F[ff(frame_start + frame_step)] # extract the final position data final_pos = zip(frame_tmp[fd('x',iden_key)][:], frame_tmp[fd('y',iden_key)][:]) del frame_tmp # extract the next_particle data from all the frames between the two next_part = [] for j in range(0,frame_step-1): next_part.append(F[ff(frame_start + j + 1)][fd('next_part',track_key[0])][:]) pass finally: # make sure we clean up the hdf stuff F.close() print 'cleaned up' del F # walk along tracks to sort in to lists for pos,nxt_p,tid in zip(init_pos,init_next_part,track_id): die_flag = False if tid == -1: continue for j in range(0,frame_step-1): if nxt_p == -1: die_flag = True break nxt_p = next_part[j][nxt_p] if die_flag or nxt_p == -1: die_list.append(pos) continue fn_pos = final_pos[nxt_p] disp = np.array(pos) - np.array(fn_pos) if np.abs(disp[0]) > cut_off or np.abs(disp[1]) > cut_off: long_list.append((pos,fn_pos)) else: short_list.append((pos,fn_pos)) return short_list,long_list,die_list
def print_info(F,frame_num,part_num,comp_num): """Prints returns (prev_part,next_part,track_id) for the given particle and computation""" return (F[ff(frame_num)][fd('prev_part',comp_num)][part_num], F[ff(frame_num)][fd('next_part',comp_num)][part_num], F[ff(frame_num)][fd('track_id',comp_num)][part_num])
def __init__(self,F,iden_key,track_key): self.iden_key = iden_key self.track_key = track_key self.planes = [plane_wrapper(F[ff(f)],iden_key,track_key) for f in range(0,F.attrs['number-of-planes'])] pass
def _plot_file_frame_phi6(key,conn,fr_num,fnameg=None): ''' ''' (fname,p_comp) = conn.execute("select fout,comp_key from comps where function = 'phi6' and dset_key = ?;",(key,)).fetchone() (comp_number,) = conn.execute("select comp_key from comps where function = 'Iden' and fout = ?",(fname,)).fetchone() (sname,stype,temp) = conn.execute("select sname,dtype,temp from dsets where key = ?",(key,)).fetchone() f = h5py.File(fname,'r') fc = f.attrs['number-of-planes'] if fr_num <fc: fr_num = fc-1 x = f[ff(fr_num)][fd('x',iden_key)][:] x = f[ff(fr_num)][fd('y',iden_key)][:] phi = f[ff(fr_num)][fd('scaler_order_parameter',iden_key)] phir = np.zeros(phi.shape[0]) phii = np.zeros(phi.shape[0]) for j in range(0,phi.shape[0]): phir[j] = phi[j][0] phii[j] = phi[j][1] print np.mean(abs(phir)) print np.mean(abs(phii)) print np.mean((phir)) print np.mean((phii)) # check interactive plotting and turn it off istatus = plt.isinteractive(); print istatus if istatus:plt.ioff() leg_hands = [] leg_strs = [] fig = plt.figure() ax = fig.add_axes([.1,.1,.8,.8]) ax.hold(True) ax.set_aspect('equal') ax.quiver(x,y,phir,phii,scale = 100,headlength = 2,headwidth= 1.5,headaxislength=2) ax.set_title(sname + " temp: " + str(temp)) # plt.plot(x,y,'ro') if not fnameg == None: f_path = '/home/tcaswell/python/figures/' + sname + '/' if not os.path.isdir(f_path): os.makedirs(f_path) fnameg = f_path + str(key) + '_phi6.png' fig.savefig(fnameg,dpi = 500) if istatus: print "displaying figure" plt.ion() plt.show() else: print "closing figure" plt.close(fig)