def transform_file(f, model=None, test=False, tri=False): print(f, 'starting') tic = time.time() s = cranium.brain() df = cranium.read_psi(f) if 'ac' not in df.columns: # if tri==True: # s.df_align = df[['x','y','z']] # pt1x = np.max(s.df_align.x) # pt1z = s.df_align[s.df_align.x == pt1x].z.values[0] # pt2x = np.min(s.df_align.x) # pt2z = s.df_align[s.df_align.x == pt2x].z.values[0] # # s.mm = cranium.math_model(np.polyfit([pt1x,0,pt2x],[pt1z,0,pt2z],2)) # # if test==True: # return(s,[pt1x,pt2x],[pt1z,pt2z]) # if tri==False: s.df_align = df[['x', 'y', 'z']] snum = re.findall(r'\d+', f.split('.')[0])[0] s.mm = cranium.math_model(model.loc[int(snum)].values) num = int(snum) # return(s) # if test==False: s.transform_coordinates() cranium.write_data(f, s.df_align) print(f, 'complete', time.time() - tic)
def save_zrf(k,df,outdir,expname): ''' Save dataframe as psi file in outdir :param str k: Sample number :param pd.DataFrame df: Dataframe containing 'x','y','z' :param str outdir: Filepath to destination directory for psi :param str expname: Descriptive name for experiment to be used in filename ''' cranium.write_data(os.path.join(outdir,'ZRF_'+k+'_'+expname+'.psi'),df)
def transform_file(f,model=None): print(f,'starting') tic = time.time() s = ds.brain() df = ds.read_psi(f) # Transform coordinates if cylindrical column not present if 'ac' not in df.columns: # Add psi data to brain object s.df_align = df[['x','y','z']] #snum = re.findall(r'\d+',f.split('.')[0])[0] snum = f.split('_')[1] num = int(snum) # Extract math model and add to brain object try: mm_val = model.loc[num].values s.mm = ds.math_model(mm_val) except: s.mm = s.fit_model(s.df_align,deg,fit_dim) # Try to transform and report errors try: s.transform_coordinates() except: print(f,'failed on transform coordinates',time.time()-tic) traceback.print_exc() # Try to save data and report errors try: ds.write_data(f,s.df_align) except: print(f,'failed on write data',time.time()-tic) traceback.print_exc() print(f,'complete',time.time()-tic) else: # Report if file already appears transformed print(f,'already transformed')
def process(num, P=None): ''' Run through the processing steps for a single sample through saving psi files :param int num: Index of the file that is currently being processed :param :class:`paramClass` P: Object containing all variables from config file ''' tic = time.time() print(num, 'Starting sample') try: e = cranium.embryo(P.expname, num, P.outdir) #Add channels and preprocess data e.add_channel(os.path.join(P.c1_dir, P.c1_files[num]), P.c1_key) e.chnls[P.c1_key].preprocess_data(P.genthresh, P.scale, P.microns) for i in range(len(P.Lcdir)): e.add_channel(os.path.join(P.Lcdir[i], P.Lcfiles[i][num]), P.Lckey[i]) e.chnls[P.Lckey[i]].preprocess_data(P.genthresh, P.scale, P.microns) #Calculate PCA transformation for structural channel, c1 if P.twoD == True: e.chnls[P.c1_key].calculate_pca_median_2d( e.chnls[P.c1_key].raw_data, P.medthresh, P.radius, P.microns) pca = e.chnls[P.c1_key].pcamed e.chnls[P.c1_key].pca_transform_2d(e.chnls[P.c1_key].df_thresh, pca, P.comporder, P.fitdim, deg=P.deg) #Transform additional channels for i in range(len(P.Lcdir)): e.chnls[P.Lckey[i]].pca_transform_2d( e.chnls[P.Lckey[i]].df_thresh, pca, P.comporder, P.fitdim, deg=P.deg) else: e.chnls[P.c1_key].calculate_pca_median(e.chnls[P.c1_key].raw_data, P.medthresh, P.radius, P.microns) pca = e.chnls[P.c1_key].pcamed e.chnls[P.c1_key].pca_transform_3d(e.chnls[P.c1_key].df_thresh, pca, P.comporder, P.fitdim, deg=P.deg) #Transform additional channels for i in range(len(P.Lcdir)): e.chnls[P.Lckey[i]].pca_transform_3d( e.chnls[P.Lckey[i]].df_thresh, pca, P.comporder, P.fitdim, deg=P.deg) # print(num,'Starting coordinate transformation') # e.chnls[P.c1_key].transform_coordinates() # for i in range(len(P.Lcdir)): # e.chnls[P.Lckey[i]].transform_coordinates() #e.save_psi() columns = ['x', 'y', 'z'] for ch in e.chnls.keys(): cranium.write_data( os.path.join(e.outdir, e.name + '_' + str(e.number) + '_' + ch + '.psi'), e.chnls[ch].df_align[columns]) toc = time.time() print(num, 'Complete', toc - tic) except: toc = time.time() print(num, 'Failed', toc - tic, exc_info())
def save_caax(k,df,outdir,expname): cranium.write_data(os.path.join(outdir,'caax_'+k+'_'+expname+'.psi'),df)