def main(): #------------------------------ # MAIN #------------------------------ imp_name = imp.getTitle() imp_name, ext = os.path.splitext(imp_name) models, channel_names, prob_threshs, nms_threshs = [], [], [], [] if stardist_membrane_enabled: models.append(stardist_membrane) channel_names.append('Membrane') prob_threshs.append(prob_thresh_membrane) nms_threshs.append(nms_thresh_membrane) if stardist_dna_enabled: models.append(stardist_dna) channel_names.append('DNA') prob_threshs.append(prob_thresh_dna) nms_threshs.append(nms_thresh_dna) if len(models) == 0: return error("no stardist model enabled") if tracking_channel not in channel_names: return error("channel %s cannot be tracked, must be one of %s" % (tracking_channel, channel_names)) n_channels = imp.getNChannels() n_frames = imp.getNFrames() is_hyperstack = n_channels > 1 if n_frames < 2: return error("input must be a timelapse") if n_channels != len(models): return error( "input image has %d channels, but %d stardist model(s) enabled" % (n_channels, len(models))) export_calibration(imp, save_path(save_dir, imp_name, 'calibration.json')) channel_imps = ChannelSplitter.split(imp) args = zip(channel_names, channel_imps, models, prob_threshs, nms_threshs) if tracking_channel == 'Membrane': args = reversed(args) # tracking_channel must come last params = {} params['modelChoice'] = "Model (.zip) from File" params['outputType'] = "ROI Manager" # params['roiPosition'] = "Automatic" # doesn't work because single channels are fed to StarDist, but result may be displayed on hyperstack params['roiPosition'] = "Hyperstack" if n_channels > 1 else "Stack" print "\n===============================\n" for channel_name, channel, model, prob_thresh, nms_thresh in args: params['input'] = channel params['modelFile'] = model.getAbsolutePath() params['probThresh'] = prob_thresh params['nmsThresh'] = nms_thresh # print 'StarDist', channel_name, ':', params, '\n' command.run(StarDist2D, False, params).get() rename_rois(rm, is_hyperstack) rm.runCommand( "Save", save_path(save_dir, imp_name, 'rois_%s.zip' % channel_name.lower())) export_rois( rm, is_hyperstack, save_path(save_dir, imp_name, 'rois_%s.json' % channel_name.lower())) assert channel_name == tracking_channel # backup global user-chosen measurements measurements = Analyzer.getMeasurements() # set needed measurements Analyzer.setMeasurements(Measurements.AREA + Measurements.CENTER_OF_MASS + Measurements.STACK_POSITION) # create measurements table rm.runCommand(imp, "Measure") # restore global user-chosen measurements Analyzer.setMeasurements(measurements) # close/hide measurements table results_window = ResultsTable.getResultsWindow() results_window.close(False) # results_window.setVisible(False) # Remove overlay if any. imp.setOverlay(None) # Get results table. results_table = ResultsTable.getResultsTable() # print results_table # Create TrackMate instance. trackmate = create_trackmate(imp, results_table, frame_link_dist, gap_close_dist, seg_split_dist) #----------------------- # Process. #----------------------- ok = process(trackmate) if not ok: sys.exit(str(trackmate.getErrorMessage())) #----------------------- # Display results. #----------------------- # TODO: close trackmate gui? # Create the GUI and let it control display of results. display_results_in_GUI(trackmate, imp) color_and_export_rois_by_track( trackmate, rm, save_path(save_dir, imp_name, 'tracks_%s.csv' % tracking_channel.lower()))
def Measurements(channels, timelist, dirs, parameters): """ Takes measurements of weka selected ROIs in a generated aligned image stack. """ # Set desired measurements. an = Analyzer() an.setMeasurements(an.AREA + an.MEAN + an.MIN_MAX + an.SLICE) # Opens raw-projections as stack. test = IJ.run("Image Sequence...", "open=" + dirs["Aligned_All"] + " number=400 starting=1 increment=1 scale=400 file=.tif sort") # Calls roimanager. rm = RoiManager.getInstance() total_rois = rm.getCount() # Deletes artefact ROIs (too large or too small). imp = WindowManager.getCurrentImage() for roi in reversed(range(total_rois)): rm.select(roi) size = imp.getStatistics().area if size < int(float(parameters["cell_min"])): rm.select(roi) rm.runCommand('Delete') elif size > int(float(parameters["cell_max"])): rm.select(roi) rm.runCommand('Delete') else: rm.runCommand("Deselect") # Confirm that ROI selection is Ok (comment out for headless run). WaitForUserDialog("ROI check", "Control ROI selection, then click OK").show() # Measure each ROI for each channel. imp = WindowManager.getCurrentImage() rm.runCommand("Select All") rm.runCommand("multi-measure measure_all One row per slice") # Close. imp = WindowManager.getCurrentImage() imp.close() # Get measurement results. rt = ResultsTable.getResultsTable() Area = rt.getColumn(0) Mean = rt.getColumn(1) Slice = rt.getColumn(27) # Removes (and counts) artefact ROIs (redundant) # Area indices without outliers Area_indices = [index for (index, value) in enumerate(Area, start=0) if value > 0 and value < 9999999] # Mean without outliers from area (redundant) Filtered_mean = [Mean[index] for index in Area_indices] Filtered_slice = [Slice[index] for index in Area_indices] # Number of cell selections. Cell_number = Filtered_slice.count(1.0) rm = RoiManager.getInstance() print "Number of selected cells: ", Cell_number print "Total number of selections: ", rm.getCount() Cells = [ Filtered_mean [x : x + Cell_number] for x in xrange (0, len(Filtered_mean), Cell_number) ] Cells_indices = [ index for (index, value) in enumerate(Cells) ] time = [ x for item in timelist for x in repeat(item, Cell_number) ] time = [ time [x : x + Cell_number] for x in xrange (0, len(time), Cell_number) ] Slices = [ Filtered_slice [x : x + Cell_number] for x in xrange (0, len(Filtered_slice), Cell_number) ] # Lists IDD, IDA + IAA if 3ch. if channels == 3: IDD_list = [ Cells [index] for index in Cells_indices [0::int(channels)] ] IDA_list = [ Cells [index] for index in Cells_indices [1::int(channels)] ] IAA_list = [ Cells [index] for index in Cells_indices [2::int(channels)] ] raw_data = {"IDD" : IDD_list, "IDA" : IDA_list, "IAA" : IAA_list, "Cell_num" : Cell_number, "Slices" : Slices, "Time" : time } elif channels == 2: IDD_list = [ Cells [index] for index in Cells_indices [0::int(channels)] ] IDA_list = [ Cells [index] for index in Cells_indices [1::int(channels)] ] raw_data = {"IDD": IDD_list, "IDA" : IDA_list, "Cell_num" : Cell_number, "Slices" : Slices, "Time" : time } return raw_data
def setMeasurementInt(i) : """sets the measurements setting to the int value i, made as a hack to translate an number to its corresponding checkboxes""" a = Analyzer() a.setMeasurements(i)