def run_fft_filter( files, bnd_file, modes, suffix= 'fft_frames/normed_frames/', make_cubs=True, run_chomp=True ): """ For each frame in fdir, compute fft( frame ), then take the top <modes> percent of the frequencies. Threshold these filtered images at the mean (of pixels inside the cell boundary). Save the thresholded image to fft_frames/ directory. fdir : directory to frames modes : percentage of modes (low->high) to use for low-pass filter """ # grab all frames, skip the directories print "files", files if os.path.isdir( files ): fdir = files + '/' dlist = os.listdir( fdir ) if os.uname()[0] == 'Linux': frames = [] for f in dlist: if f.endswith('npy') and not os.path.isdir( fdir+f ): frames.append( f ) else: frames = [ f for f in dlist if not os.path.isdir( fdir+f ) ] else: frames = [ files ] fdir = files.rpartition( '/' )[0].rpartition( '/' )[0] +'/' if not fdir.endswith( '/' ): fdir += '/' savedir = fdir + suffix # create the directory if necessary make_dir( savedir ) # compute fft and filtered ifft on (normed) images for frame in frames: if frame.endswith( 'npy' ): savename = frame.rstrip( '.npy' ) elif frame.endswith( 'txt' ): savename = frame.rstrip( '.txt' ) else: savename = frame fft_data = {} # store the modes fft_data[ 'modes' ] = modes try: image = numpy.loadtxt( fdir+frame ) except ValueError: image = numpy.load( fdir+frame ) except IOError: raise bnd = numpy.loadtxt( bnd_file ) # normalize the images and shift so centered at zero image = normalize_image( image ) # FFT X = fft_image( image ) # Y = H * X Y = ideal_low_pass( X, r=modes ) Yinv = ifft_image( Y ) Ypow = log1p( numpy.abs( Yinv )**2 ) # crop the log of the low pass to get rid of the noise outside # of the cell (zero out external points using boundary array) crop_zeros = bnd * Ypow crop_zeros.resize( Ypow.shape ) # append the cell with zeros cropped [-4] #Y.append( crop_zeros ) m = crop_zeros[numpy.where( crop_zeros!=0 )].mean() # everything above the mean, [-3] #Ymean = numpy.ma.masked_less_equal( crop_zeros, m ) # just the boolean mask, [-2] #Ymask = numpy.ma.masked_less_equal( crop_zeros, m ).mask # now populate fft_data with the essentials and save to disk #fft_data['fft'] = X #fft_data['lowpass'] = Y #fft_data['ifft'] = Yinv #fft_data['fft_mag'] = Ypow fft_data['ifft_nonzero'] = crop_zeros fft_data['mean'] = m # masking is easy, so no need to save. #fft_data['ifft_mask_mean'] = Ymean #fft_data['ifft_mask'] = Ymask # create filename with chomp-readable name (remove the 'dot' # from mode) mode_part = str( modes ).partition( '.' ) mode_str = mode_part[0] + mode_part[2] # split files into subdirectories datadir = savedir + 'r'+mode_str save_suffix = savename+'_r'+mode_str make_dir( datadir ) dataname = datadir + '/' + savename+'_r'+mode_str+'.pkl' # save our thresholded data for later analysis save_fft( fft_data, dataname ) # additional work with chomp if make_cubs: fft2cub( dataname, datadir ) if run_chomp: if not make_cubs: pass else: cub_name = datadir +'_cub/' + save_suffix + '.cub' cbetti_prefix = datadir + '_cbetti/' + save_suffix betti_prefix = datadir + '_betti/' + save_suffix make_dir( datadir + '_cbetti/' ) make_dir( datadir + '_betti/' ) chomp_betti.run_chomp( cub_name, cbetti_prefix+'.cbetti' ) chomp_betti.extract_betti( cbetti_prefix, betti_prefix )
def run_chomp( cub_name ): """ """ save_prefix = cub_name.rstrip( '.cub' ) chomp_betti.run_chomp( cub_name, save_prefix+'.cbetti' ) chomp_betti.extract_betti( save_prefix )
#savedir = '/home/kellys/Dropbox/Shared-MSU-Kel/Research/RBC/' #files = '/home/kellys/RBC_Deriv_120125_r01/' #files = '/home/kellys/RBC_Deriv_120125_r005/' #grab files, skip directories if os.path.isdir (files): fdir = files + '/' dlist = os.listdir(fdir) frames = [] for f in dlist: if f.endswith('cbetti') and not os.path.isdir(fdir+f): frames.append(f) else: frames = [ files ] fdir = files.rpartition('/')[0].rpartition('/')[0]+'/' #savedir = '/home/kellys/RBC_Deriv_120125_r01/' #savedir = '/home/kellys/RBC_Deriv_110125_r01/' print 'extracting from chomp Bettie' frames.sort(key=R.natural_key) k = 0 for frame in frames: k = k+1 name = frame.rstrip('.cbetti') CB.extract_betti(fdir+name, fdir+name) barr = CB.read_betti_dir(fdir) CB.plot_betti(barr, 120125, savedir, 1, None, 5000)