# if decide to use other image as ref_image # be sure to check photobleaching correction for all reported intensities temp = store.get_image(cb.c(data_c).t(0).build()) pix = np.reshape(temp.get_raw_pixels(), newshape=[temp.get_height(), temp.get_width()]) if analyze_organelle == 'nucleoli': # nuclear detection (currently only doable for nucleoli staining image) label_nuclear, _ = find_nuclear_nucleoli(pix) data_log['num_nuclei_detected'] = [np.amax(label_nuclear)] print("Found %d nuclei." % data_log['num_nuclei_detected'][0]) # organelle detection organelle_before_filter, organelle = find_organelle(pix, thresholding, min_size=min_size, max_size=max_size) label_organelle = label(organelle, connectivity=1) data_log['num_%s_detected' % analyze_organelle] = [obj.object_count(organelle)] print("Found %d %s." % (data_log['num_%s_detected' % analyze_organelle][0], analyze_organelle)) # organelle pd dataset organelle_pd = organelle_analysis(pix, organelle, '%s' % analyze_organelle, pos) if analyze_organelle == 'nucleoli': # link nucleoli with corresponding nuclear round_x = [round(num) for num in organelle_pd['x']]
if count >= nr_between_projector_checks: calibrated, error = snap_and_get_bleach_location( cal_exposure, cal_offset) if error < 0: count -= 1 else: count = 0 if calibrated: continue count += 1 img = mm.live().snap(False).get(0) pixels = np.reshape(img.get_raw_pixels(), newshape=[img.get_height(), img.get_width()]) # find organelles using a combination of thresholding and watershed _, segmented = find_organelle(pixels, 'local-nucleoli', 500, 200, 10, 1000) label_img = label(segmented) label_img = morphology.remove_small_objects(label_img, 5) blobs = regionprops(label_img) centered = select(blobs, 'centroid', img.get_width() / 10, 0.9 * img.get_width()) if len(centered) > (nr // 2): projector.enable_point_and_shoot_mode(True) ssb = mm.acquisitions().get_acquisition_settings().copy_builder() mm.acquisitions().set_acquisition_settings( ssb.prefix(pos.get_label()).build()) ds = mm.acquisitions().run_acquisition_nonblocking() # Trick to get timing right. Wait for Core to report that Sequence is running while not mmc.is_sequence_running(mmc.get_camera_device()): time.sleep(0.1)
num_dir = len(dirs) for s in range(len(dirs)): name = dirs[s][:-4] print("### DATA PROCESSING: %s (%d / %d)" % (name, s + 1, num_dir)) imgs = nd2.ND2Reader('%s/%s.nd2' % (data_path, name)) # 0: SG channel # 1: cell boundary channel # 2: nuclei channel # identify SG print("### SG IDENTIFICATION ...") sg = find_organelle(imgs[0], thresholding, 3000, 200, min_size_sg, max_size_sg) # identify cell print("### CELL SEGMENTATION ...") cell, cell_ft = find_cell(imgs[0], imgs[1], imgs[2], nuclear_local_thresholding_size, min_size_nuclear, max_size_nuclear, min_size_cell, max_size_cell) # figures storage_path = save_path if not os.path.exists(storage_path): os.makedirs(storage_path) print("### FIGURE EXPORTATION ...") fig = Figure(figsize=(15, 10), dpi=100)
# ------------------------------ # IMAGE ANALYSIS # ------------------------------ print("### Image analysis: calculate SG mask/pd for each FOV ...") sg_fov_pd = pd.DataFrame() # SG dataFrame of all FOVs based on each single FOV row_lst = [] # row location of given FOV in multi-FOV-grid col_lst = [] # column location of given FOV in multi-FOV-grid pix_lst = [] # list of t(0).p(0) image for multi-FOV-display sg_lst = [] # list of t(0).p(0) SG mask for multi-FOV-display for i in range(max_p + 1): temp = store.get_image(cb.t(data_t).c(data_c).z(0).p(i).build()) temp_pix = np.reshape(temp.get_raw_pixels(), newshape=[temp.get_height(), temp.get_width()]) temp_sg = find_organelle(temp_pix, thresholding, min_size=min_size, max_size=max_size) row, col = dat.get_grid_pos(i, num_grid) row_lst.append(row) col_lst.append(col) pix_lst.append(temp_pix) sg_lst.append(temp_sg) if export_pd_pre_stitch == 'Y': temp_sg_pd = organelle_analysis(temp_pix, temp_sg, 'sg', i) sg_fov_pd = pd.concat([sg_fov_pd, temp_sg_pd], ignore_index=True) print("### Image analysis: Stitch image ...") pix_pd = pd.DataFrame({ 'row': row_lst, 'col': col_lst, 'pix': pix_lst
# reference image of time 0 # if decide to use other image as ref_image # be sure to check photobleaching correction for all reported intensities temp = store.get_image(cb.c(data_c).t(0).build()) pix = np.reshape(temp.get_raw_pixels(), newshape=[temp.get_height(), temp.get_width()]) # nuclear detection label_nuclear = find_nuclear_nucleoli(pix) data_log['num_nuclei_detected'] = [np.amax(label_nuclear)] print("Found %d nuclei." % data_log['num_nuclei_detected'][0]) # nucleoli detection nucleoli = find_organelle(pix, thresholding, min_size=min_size, max_size=max_size) label_nucleoli = label(nucleoli, connectivity=1) data_log['num_nucleoli_detected'] = [obj.object_count(nucleoli)] print("Found %d nucleoli." % data_log['num_nucleoli_detected'][0]) # nucleoli pd dataset nucleoli_pd = organelle_analysis(pix, nucleoli, 'nucleoli', pos) # link nucleoli with corresponding nuclear round_x = [round(num) for num in nucleoli_pd['x']] round_y = [round(num) for num in nucleoli_pd['y']] nucleoli_pd['nuclear'] = obj.points_in_objects(label_nuclear, round_y, round_x) data_log['num_nucleoli_in_nuclei'] = [ len(nucleoli_pd[nucleoli_pd['nuclear'] != 0]) ]
if error < 0: count -= 1 else: count = 0 if calibrated: continue count += 1 # set analyze channel mmc.set_config("Channels", analyze_channel) img = mm.live().snap(False).get(0) pixels = np.reshape(img.get_raw_pixels(), newshape=[img.get_height(), img.get_width()]) # find organelles using a combination of thresholding and watershed if analyze_organelle == 'sg': segmented = find_organelle(pixels, 'na', 500, 200, 5, 200) # stress granule else: segmented = find_organelle(pixels, 'local-nucleoli', 500, 200, 10, 1000) # nucleoli label_img = label(segmented) blobs = regionprops(label_img) centered = select(blobs, 'centroid', img.get_width() / 10, 0.9 * img.get_width()) if len(centered) > (nr // 2): projector.enable_point_and_shoot_mode(True) # set analyze channel mmc.set_config("Channels", acquisition_channel) ssb = mm.acquisitions().get_acquisition_settings().copy_builder() mm.acquisitions().set_acquisition_settings(ssb.prefix(pos.get_label()).build()) ds = mm.acquisitions().run_acquisition_nonblocking()
# load time series data store = mm.data().load_data(data_path, True) max_t = store.get_max_indices().get_t() cb = mm.data().get_coords_builder() cb.t(0).p(0).c(0).z(0) # ------------------------------ # IMAGE ANALYSIS based on position # ------------------------------ print("### Image analysis: calculate SG mask/pd ...") # test image of position temp = store.get_image(cb.t(data_t).c(data_c).z(0).p(data_p).build()) pix = np.reshape(temp.get_raw_pixels(), newshape=[temp.get_height(), temp.get_width()]) sg = find_organelle(pix, thresholding, min_size=min_size, max_size=max_size, local_param=21) label_sg = label(sg, connectivity=1) sg_pd = organelle_analysis(pix, sg, 'sg', 0) # -------------------------- # COLOR CODED IMAGES # -------------------------- print("### Calculate color coded circ/ecce/int image ...") # circ image cmap1 = 'YlOrRd' cmap1_napari = dis.num_color_colormap(cmap1, 255)[0] cmap1_plt = dis.num_color_colormap(cmap1, 255)[1] sg_circ = obj.obj_display_in_circularity(label_sg) # ecce image cmap2 = 'Blues'