def get_params_locate(frame,diameter=15,minmass_percentile=92,out_fh=None,test=True,figsize=None): f = tp.locate(frame, diameter, invert=False) minmass=np.percentile(f['mass'],minmass_percentile) logging.info('feature count= %s, %spercentile= %s' % (len(f),minmass_percentile,minmass)) f = tp.locate(frame, diameter, invert=False, minmass=np.percentile(f['mass'],minmass_percentile)) logging.info('feature count= %s, %spercentile= %s' % (len(f),minmass_percentile, np.percentile(f['mass'],minmass_percentile))) if test: logging.info('getting plots annotate') # plt.clf() fig=plt.figure(figsize=figsize) ax=plt.subplot(111) ax=tp.annotate(f, frame,ax=ax) if not out_fh is None: # plt.savefig('%s.annotate.pdf' % out_fh,format='pdf') plt.savefig('%s.annotate.svg' % out_fh,format='svg') # plt.clf() logging.info('getting plots hist') cols=['mass','size','ecc','signal','raw_mass','ep'] fig=plt.figure() ax=plt.subplot(111) _=f.loc[:,cols].hist(ax=ax) if not out_fh is None: plt.savefig('%s.feature_props.svg' % out_fh,format='svg') # plt.clf() logging.info('getting plots bias') fig=plt.figure() tp.subpx_bias(f); if not out_fh is None: plt.savefig('%s.subpx_bias.svg' % out_fh,format='svg') # plt.clf() params_locate={'diameter':diameter, 'minmass':minmass} return params_locate
def SpotSize_manual(rawframes_pre, settings, AutoIteration=True): """ Optimize the diameter of the Particles """ separation = settings["Find"]["Separation data"] minmass = settings["Find"]["Minimal bead brightness"] UserSatisfied = False if AutoIteration == True: try_diameter = [3.0, 3.0] else: try_diameter = settings["Find"]["Estimated particle size"] if len(rawframes_pre) > 100: rawframes_pre = rawframes_pre[0:100, :, :] while UserSatisfied == False: print('UserSatisfied? : ', UserSatisfied) print('Try diameter:', np.asarray(try_diameter)) # obj_all = nd.get_trajectorie.batch_np(rawframes_rot, ParameterJsonFile, UseLog = False, diameter = try_diameter) obj_all = tp.batch(rawframes_pre, diameter=try_diameter, minmass=minmass, separation=separation) if obj_all.empty == True: UserSatisfied = False else: tp.subpx_bias(obj_all) plt.draw() plt.title("y, spot sizes = {}".format(try_diameter)) plt.show() if AutoIteration == True: plt.pause(3) UserSatisfied = AskIfUserSatisfied( 'The histogramm should be flat. \ They should not have a dip in the middle! \ Particles should be detected. Are you satisfied?' ) else: UserSatisfied = True print( "\n >>> The histogramm should be flat. They should not have a dip in the middle! <<< \n" ) if UserSatisfied == False: try_diameter = list(np.asarray(try_diameter) + 2) # settings["Find"]["Estimated particle size"] = try_diameter # nd.handle_data.WriteJson(ParameterJsonFile, settings) if AutoIteration == True: print('Your diameter should be:', np.asarray(try_diameter)) print( "WARNING: IF YOUR BEADSIZE CHANGED YOU MIGHT HAVE TO UPDATE YOUR MINIMAL BRIGHTNESS!" ) return try_diameter
while (flag == 0): features = tp.locate(frames[frameNum], particleSize, invert=False) features.head() # shows the first few rows of data plt.figure(2) # make a new figure tp.annotate(features, frames[frameNum]) fig, ax = plt.subplots() ax.hist(features['mass'], bins=20) # Optionally, label the axes. ax.set(xlabel='mass', ylabel='count') plt.figure(3) tp.subpx_bias(features) flag = int( input('Press 1 to use these settings, Press 0 to Enter new settings')) if (flag == 0): frameNum = int( input('Enter new frame number. Prev value : {}'.format(frameNum))) minMass = int( input('Enter new min mass. Prev value: {}'.format(minMass))) blobSize = int( input('Enter new blob size (odd integer). Prev value: {}'.format( blobSize))) if (not os.path.exists(FilePath) or computeNew == 1):
# let's see a histogram of total brightness of blobs # fig, ax = plt.subplots() # ax.hist(f['mass'], bins=20) # # label axes: # ax.set(xlabel='mass', ylabel='count') # plt.show() # now we're filtering by brightness (minmass): f = tp.locate(frames[63], 11, minmass=49000, max_iterations=20) plt.figure() tp.annotate(f, frames[63]) plt.show() # check for subpixel bias (are decimals evenly distributed) tp.subpx_bias(f) plt.show() # locate features in all frames # TODO return here time_cutoff = 419 f = tp.batch(frames[:time_cutoff], 11, minmass=49000, max_iterations=20) print("f length is:", time_cutoff - 2) # link features into particle trajectories # define max displacement max_disp = 5 # define frame memory for feature drop-out frame_memory = 5 t = tp.link_df(f, max_disp, memory=frame_memory)
### USER INPUT FOR FEATURE FINDING ### mass=input("Enter particle brightness: ") ##minimum brightness to track (~1500) mass=float(mass) #print("Min. Brightness="+str(mass)) ###VISUAL FEATURE FINDING ROUTINE R=tp.locate(frames[0],diameter,minmass=mass,separation=6,invert=True) ##user define feature params plt.figure() tp.annotate(R,frames[0]) ##circle the 'features' in the frame tp.subpx_bias(R) ##check x,y displacement distrobution plt.show() ##Batch Processes ## #perform same feature finding but on frames 0:last L=tp.batch(frames[:last],diameter,minmass=mass,separation=5,invert=True) ##link trajectories using memory tra=tp.link_df(L,5,memory=2) ##max displacement b/w frames, memory b/w frames print(tra.head()) print(tra.tail()) memory=float(last*(.95)) ##set memory to be 80% of total frames
def FindDiameter_manual(rawframes_pre, settings, AutoIteration=True): """ Optimize the diameter of the Particles Start with a very low diameter (3px) and run Trackpy on the first 100 frames. A good choosen diameter leads to a even (flat) distributed decimal place of the localized particle position If the distribution is not flat, the diameter is increase till its ok. """ separation = settings["Find"]["tp_separation"] minmass = settings["Find"]["tp_minmass"] UserSatisfied = False if AutoIteration == True: try_diameter = [3.0, 3.0] else: try_diameter = settings["Find"]["tp_diameter"] if len(rawframes_pre) > 100: rawframes_pre = rawframes_pre[0:100, :, :] while UserSatisfied == False: nd.logger.debug('UserSatisfied? : %i', UserSatisfied) nd.logger.info("Try diameter: %i", try_diameter[0]) # obj_all = nd.get_trajectorie.batch_np(rawframes_rot, ParameterJsonFile, UseLog = False, diameter = try_diameter) obj_all = tp.batch(rawframes_pre, diameter=try_diameter, minmass=minmass, separation=separation) if obj_all.empty == True: nd.logger.warning("No Object found.") UserSatisfied = False else: tp.subpx_bias(obj_all) plt.draw() plt.title("y, spot sizes = {}".format(try_diameter)) plt.show() if AutoIteration == True: plt.pause(3) UserSatisfied = AskIfUserSatisfied( 'The histogramm should be flat. They should not have a dip in the middle! Particles should be detected. \n Are you satisfied?' ) else: UserSatisfied = True print( "\n >>> The histogramm should be flat. They should not have a dip in the middle! <<< \n" ) if UserSatisfied == False: try_diameter = list(np.asarray(try_diameter) + 2) # settings["Find"]["tp_diameter"] = try_diameter # nd.handle_data.WriteJson(ParameterJsonFile, settings) if AutoIteration == True: print('Your diameter should be:', np.asarray(try_diameter)) print( "WARNING: IF YOUR BEADSIZE CHANGED YOU MIGHT HAVE TO UPDATE YOUR MINIMAL BRIGHTNESS!" ) return try_diameter
def OptimizeMinmassInTrackpyMain(settings, img1, num_particles_zncc, pos_particles, DoDiameter): # prepare calling OptimizeMinmassInTrackpy # load settings diameter = settings["Find"]["tp_diameter"] separation = settings["Find"]["tp_separation"] percentile = settings["Find"]["tp_percentile"] ImgConvolvedWithPSF = settings["PreProcessing"]["EnhanceSNR"] # Trackpy does bandpass filtering as "preprocessing". If the rawdata is already convolved by the PSF this additional bandpass does not make any sense. Switch of the preprocessing if rawdata is already convolved by the PSF DoPreProcessing = (ImgConvolvedWithPSF == False) if DoDiameter == False: nd.logger.info("Optimize trackpy parameter MINMASS: starting...") minmass, num_particles_trackpy, not_needed = OptimizeMinmassInTrackpy( img1, diameter, separation, num_particles_zncc, pos_particles, minmass_start=1, DoPreProcessing=DoPreProcessing, percentile=percentile) settings["Find"]["tp_minmass"] = minmass nd.logger.info("Optimize trackpy parameter MINMASS: ...finished") else: nd.logger.info( "Optimize trackpy parameter MINMASS and DIAMETER: starting...") # try several diameters. ideal job for doing this in parallal num_cores = multiprocessing.cpu_count() nd.logger.info("This is done in parallel (Number of cores: %s)", num_cores) # set the diameter worth trying # theoreical value of diameter diam_theory = nd.ParameterEstimation.DiameterForTrackpy(settings) # minium diameter min_diam = diam_theory - 2 if min_diam < 3: min_diam = 3 # maximum diameter max_diam = diam_theory + 8 + 1 # max_diam = diam_theory + 12 + 1 inputs = range(min_diam, max_diam, 2) num_verbose = nd.handle_data.GetNumberVerbose() # Do it parallel output_list = Parallel(n_jobs=num_cores, verbose=num_verbose)( delayed(OptimizeMinmassInTrackpy)(img1, loop_diameter, separation, num_particles_zncc, pos_particles, minmass_start=1, DoPreProcessing=DoPreProcessing, percentile=percentile, DoLog=False) for loop_diameter in inputs) for ii, jj in enumerate(inputs): tp.subpx_bias(output_list[ii][2]) plt.title("y - diameter = %i" % (jj)) #Pause sothat python can show the plots before waiting for an input plt.pause(3) valid_input = False # ask the user which diameter is best while valid_input == False: nd.logger.info( "The histogramm should be flat. They should not have a dip in the middle! " ) try: diameter = input("Which diameter is best [dtype interger]: ") diameter = int(diameter) # position of best diameter inside range range_pos = int(np.where(np.asarray(inputs) == diameter)[0]) # select ideal minmass from list minmass = output_list[range_pos][0] valid_input = True except: nd.logger.error( "Try again. Dtype must be int and one of the available frames from %i to %i", inputs[0], inputs[-1]) # save result settings["Find"]["tp_minmass"] = minmass settings["Find"]["tp_diameter"] = diameter nd.logger.info("Minmass for trackpy estimate to: %i", minmass) nd.logger.info("Diameter for trackpy estimate to: %i", diameter) nd.logger.info( "Optimize trackpy parameter MINMASS and DIAMETER: ...finished") #make the plotting of some internal functions in case of debug if nd.logger.getEffectiveLevel() < 20: nd.logger.debug("Plot some additional stuff for information") OptimizeMinmassInTrackpy(img1, diameter, separation, num_particles_zncc, pos_particles, minmass_start=1, DoPreProcessing=DoPreProcessing, percentile=percentile, DoLog=True) else: nd.logger.info( "If you want to see some extra plots, change to the debug mode" ) return settings