def find_nuclear_nucleoli(pixels: np.array): """ Detect nuclear in nucleoli stain images :param pixels: np.array, nucleoli stain image :return: label_nuclear_sort: np.array, grey scale labeled nuclear image """ bg_int = ana.get_bg_int([pixels])[0] # nuclear detection markers = np.zeros_like(pixels) markers[pixels < (1.7 * bg_int)] = 1 markers[pixels > (3 * bg_int)] = 2 seg = random_walker(pixels, markers) # nuclear binary mask nuclear = np.zeros_like(pixels) nuclear[seg == 2] = 1 # fill holes nuclear_fill = ndimage.binary_fill_holes(nuclear) # separate touching nuclei label_nuclear = obj.label_watershed(nuclear_fill, 1) # filter out: # 1) nuclear size < 1500 # 2) nuclear touches boundary label_nuclear_ft = clear_border(label_nuclear) label_nuclear_ft = obj.label_remove_small(label_nuclear_ft, 1500) label_nuclear_sort = obj.label_resort(label_nuclear_ft) return label_nuclear_sort
def find_nuclear(pixels: np.array, local_thresholding_size: int, min_size: int, max_size: int): """ Detect nuclear in nuclear stain image :param pixels: np.array, nuclear stain image :param local_thresholding_size: int, running size for local thresholding, varies for different magnification etc. :param min_size: minimum allowable nuclear size :param max_size: maximum allowable nuclear size :return: nuclear: np.array, 0-and-1, nuclear mask with nuclei identified label_nuclear: np.array, labeled image of nuclear mask """ # nuclear identification using local thresholding local = threshold_local(pixels, local_thresholding_size) # for Jose data under 60x objective, local_thresholding_size = 701 nuclei_local = pixels > local for i in range(2): nuclei_local = binary_erosion(nuclei_local) for i in range(2): nuclei_local = binary_dilation(nuclei_local) nuclei_local = ndimage.binary_fill_holes(nuclei_local) nuclei_local = remove_small(nuclei_local, min_size) nuclei_local = remove_large(nuclei_local, max_size) # for Jose data under 60x objective, min_size = 15000 # nuclear identification using background intensity bg_int = ana.get_bg_int([pixels])[0] nuclei_bg = pixels > 1.05 * bg_int nuclei_bg = ndimage.binary_fill_holes(nuclei_bg) nuclei_bg = binary_erosion(nuclei_bg) nuclei_bg = binary_dilation(nuclei_bg) nuclei_bg = remove_small(nuclei_bg, min_size) nuclei_bg = remove_large(nuclei_bg, max_size) # nuclear mask nuclear = np.zeros_like(pixels) nuclear[nuclei_local == 1] = 1 nuclear[nuclei_bg == 1] = 1 label_nuclear = label(nuclear) return nuclear, label_nuclear
pointer_pd['num_ctrl_spots'] = [num_ctrl_spots] * len(pointer_pd) # get raw intensities for bleach spots and control spots pointer_pd['raw_int'] = ana.get_intensity( label(bleach_spots, connectivity=1), pixels_tseries) ctrl_pd['raw_int'] = ana.get_intensity( label(ctrl_spots, connectivity=1), pixels_tseries) ctrl_pd['pos'] = [pos] * num_ctrl_spots # link ctrl spots with corresponding organelle ctrl_pd['%s' % analyze_organelle] = obj.points_in_objects( label_organelle, ctrl_pd['x'], ctrl_pd['y']) print("### Image analysis: background correction ...") # background intensity measurement bg_int_tseries = ana.get_bg_int(pixels_tseries) pointer_pd['bg_int'] = [bg_int_tseries] * len(pointer_pd) # background intensity fitting bg_fit = mat.fitting_linear(np.arange(0, len(bg_int_tseries), 1), bg_int_tseries) pointer_pd = dat.add_columns( pointer_pd, ['bg_linear_fit', 'bg_linear_r2', 'bg_linear_a', 'bg_linear_b'], [[bg_fit[0]] * len(pointer_pd), [bg_fit[1]] * len(pointer_pd), [bg_fit[2]] * len(pointer_pd), [bg_fit[3]] * len(pointer_pd)]) # background correction # use original measurement if fitting does not exist if np.isnan(bg_fit[2]): bg = bg_int_tseries
sg_tseries.append(sg) if 1 in sg: label_sg = label(sg, connectivity=1) sg_props = regionprops_table(label_sg, pix, properties=('label', 'area', 'mean_intensity')) sg_props1 = regionprops_table(label_sg, pix1, properties=('label', 'area', 'mean_intensity')) sg_pd = pd.DataFrame(sg_props) sg_pd1 = pd.DataFrame(sg_props1) num_temp = len(sg_pd) num.append(num_temp) size_temp = np.mean(sg_pd['area']) size.append(size_temp) raw_int_G3BP1_temp = np.mean(sg_pd['mean_intensity']) raw_int_G3BP1.append(raw_int_G3BP1_temp) bg_G3BP1_temp = ana.get_bg_int([pix])[0] bg_G3BP1.append(bg_G3BP1_temp) raw_int_sample_temp = np.mean(sg_pd1['mean_intensity']) raw_int_sample.append(raw_int_sample_temp) bg_sample_temp = ana.get_bg_int([pix1])[0] bg_sample.append(bg_sample_temp) raw_int_G3BP1_full = raw_int_G3BP1_full + sg_pd['mean_intensity'].tolist() raw_int_sample_full = raw_int_sample_full + sg_pd1['mean_intensity'].tolist() x_frame = x_frame + [i] * len(sg_pd) bg_G3BP1_full = bg_G3BP1_full + [bg_G3BP1_temp] * len(sg_pd) bg_sample_full = bg_sample_full + [bg_sample_temp] * len(sg_pd1) else: num.append(0) size.append(0) raw_int_G3BP1.append(0)
# measure stress granule properties in both channel sg_props = regionprops_table(label_sg, pix, properties=('label', 'area', 'mean_intensity')) sg_props1 = regionprops_table(label_sg, pix1, properties=('label', 'area', 'mean_intensity')) sg_pd = pd.DataFrame(sg_props) sg_pd1 = pd.DataFrame(sg_props1) # measure total intensity fov_G3BP1 = regionprops(fov, pix)[0].mean_intensity fov_sample = regionprops(fov, pix1)[0].mean_intensity # measure background intensity bg_G3BP1 = ana.get_bg_int([pix])[0] bg_sample = ana.get_bg_int([pix1])[0] # add information into list pos_lst = pos_lst + [pos] * len(sg_pd) well_lst = well_lst + [well] * len(sg_pd) frame_lst = frame_lst + [frame + frame_offset] * len(sg_pd) label_lst = label_lst + sg_pd['label'].tolist() size_lst = size_lst + sg_pd['area'].tolist() raw_int_G3BP1_lst = raw_int_G3BP1_lst + sg_pd['mean_intensity'].tolist( ) bg_G3BP1_lst = bg_G3BP1_lst + [bg_G3BP1] * len(sg_pd) fov_G3BP1_lst = fov_G3BP1_lst + [fov_G3BP1] * len(sg_pd) raw_int_sample_lst = raw_int_sample_lst + sg_pd1[ 'mean_intensity'].tolist() bg_sample_lst = bg_sample_lst + [bg_sample] * len(sg_pd) fov_sample_lst = fov_sample_lst + [fov_sample] * len(sg_pd)