def __call__(self, downscale=False): markers = ['avanti', 'Rutenium Red'] si = SequenceImporter(markers) try: img, channel_names = si(self.settings.input_folder) except: si = SequenceImporter() img, channel_names = si(self.settings.input_folder) output = np.zeros((img.shape[0], img.shape[1])) return output image = np.mean(img, axis=2) pdb.set_trace() if downscale: image_downscale = rescale(image, .25) else: image_downscale = image perc = np.percentile(image_downscale, [80, 99]) low_val = perc[0] high_val = perc[1] image = 255 * (image_downscale - low_val) / (high_val - low_val) image[image>255] = 255 image[image<0] = 0 image = image.astype(np.uint8) pref1 = median(image, disk(3)) pref2 = gaussian(pref1, .5) thresh = threshold_isodata(pref2) binary = pref2 > thresh print(np.sum(binary)) binary_closed = closing(binary, square(8)) clean_binary = remove_small_objects(binary_closed, 30) print(np.sum(binary_closed)) temp = dilation(clean_binary, disk(2)) output_downscale = closing(temp, square(15)) print(np.sum(output_downscale)) if downscale: output = rescale(output_downscale, 4) else: output = output_downscale print(np.sum(output)) return output
def read_region_images(self): """ reads the region images and does some postfiltering. """ filename = self.settings.ilastik_filename im_small = skimage.io.imread(filename) img = rescale(im_small, 4, preserve_range=True) # get the right size si = SequenceImporter(['CD3']) original_img, channel_names = si(self.settings.input_folder) nb_rows = original_img.shape[0] nb_cols = original_img.shape[1] img = self.adjust(img, nb_rows, nb_cols, fill_value=255) background = np.zeros(img.shape, dtype=np.uint8) background[img > 250] = 255 background = remove_small_holes(background.astype(np.bool), 400, connectivity=2) bcell = np.zeros(img.shape, dtype=np.uint8) bcell[img > 124] = 255 bcell[background > 0] = 0 bcell = remove_small_holes(bcell.astype(np.bool), 400, connectivity=2) tcell = np.zeros(img.shape, dtype=np.uint8) tcell[img < 5] = 255 tcell = remove_small_holes(tcell.astype(np.bool), 400, connectivity=2) return background, bcell, tcell
def prepare(self): """ prepare loads three images to make an RGB image which can then be used in Ilastik for region annotation and segmentation. """ rgb_folder = self.settings.ilastik_input_rgb_folder #prep_folder = self.settings.ilastik_input_folder for folder in [rgb_folder]: #, prep_folder]: if not os.path.isdir(folder): os.makedirs(folder) si = SequenceImporter(['CD3', 'CD19', 'E-Cadherin']) img, channel_names = si(self.settings.input_folder) img_downscale = rescale(img, .25) rgb_image = np.zeros( (img_downscale.shape[0], img_downscale.shape[1], 3), dtype=np.float64) for i in range(img.shape[2]): perc = np.percentile(img_downscale[:, :, i], [10, 99.9]) minval = perc[0] maxval = perc[1] normalized = (img_downscale[:, :, i] - minval) / (maxval - minval) normalized[normalized > 1.0] = 1.0 normalized[normalized < 0.0] = 0.0 rgb_image[:, :, i] = 255 * normalized skimage.io.imsave(self.settings.ilastik_input_rgb_filename, rgb_image.astype(np.uint8)) return
def get_data(self, force=False): if not force: print('new calculation not forced ... ') print('Reading data ... ') si = SequenceImporter(self.markers) img, channel_names = si(self.settings.input_folder) self.channel_names = channel_names # get the individual cells. ws = self.cell_detector.get_image(False) # number of samples N = ws.max() # number of features P = len(channel_names) X = np.zeros((N, P)) for j in range(P): props = regionprops(ws, img[:, :, j]) intensities = np.array( [props[k]['mean_intensity'] for k in range(len(props))]) # centers = np.array([props[k]['centroid'] for k in range(len(props))]) X[:, j] = intensities print('read X: %i x %i' % (N, P)) return X
def test_routine(self): markers = ['avanti', 'Rutenium Red'] si = SequenceImporter(markers) img, channel_names = si(self.settings.input_folder) image = np.mean(img, axis=2) image_downscale = rescale(image, .25) perc = np.percentile(image_downscale, [80, 99]) low_val = perc[0] high_val = perc[1] image = 255 * (image_downscale - low_val) / (high_val - low_val) image[image>255] = 255 image[image<0] = 0 image = image.astype(np.uint8) pref1 = median(image, disk(3)) pref2 = gaussian(pref1, .5) thresh = threshold_isodata(pref2) binary = pref2 > thresh binary_closed = closing(binary, square(8)) clean_binary = remove_small_objects(binary_closed, 30) temp = dilation(clean_binary, disk(2)) output = closing(temp, square(15)) if self.settings.debug: out_folder = os.path.join(self.settings.debug_folder, 'fissure') if not os.path.isdir(out_folder): os.makedirs(out_folder) filename = os.path.join(out_folder, 'debug_fissure_median.png') skimage.io.imsave(filename, pref1) filename = os.path.join(out_folder, 'debug_fissure_gauss.png') skimage.io.imsave(filename, pref2) filename = os.path.join(out_folder, 'debug_fissure_binary_closed.png') skimage.io.imsave(filename, 255 * binary_closed) filename = os.path.join(out_folder, 'debug_fissure_normalized.png') skimage.io.imsave(filename, image) filename = os.path.join(out_folder, 'debug_thresh_isodata.png') skimage.io.imsave(filename, 255 * binary) filename = os.path.join(out_folder, 'debug_thresh_clean.png') skimage.io.imsave(filename, 255 * clean_binary) filename = os.path.join(out_folder, 'debug_thresh_dilated.png') skimage.io.imsave(filename, 255 * temp) filename = os.path.join(out_folder, 'debug_thresh_output.png') skimage.io.imsave(filename, 255 * output) return output
def projection(self): si = SequenceImporter() img, channel_names = si(self.settings.input_folder) projection_markers = list(filter(lambda x: x[:2] == 'CD', list(channel_names.values() ) )) si = SequenceImporter(projection_markers) img, channel_names = si(self.settings.input_folder) avg_image = np.average(img, axis=2) projection_folder = os.path.join(self.settings.debug_folder, 'projection_folder') if not os.path.isdir(projection_folder): os.makedirs(projection_folder) print('made %s' % projection_folder) filename = os.path.join(projection_folder, 'projection_%s.png' % self.settings.dataset) image_max = np.percentile(avg_image, [99])[0] image_min = np.percentile(avg_image, [5])[0] avg_image_norm = 255.0 * (avg_image - image_min) / (image_max - image_min) avg_image_norm[avg_image_norm > 255] = 255 skimage.io.imsave(filename, avg_image_norm.astype(np.uint8)) return avg_image_norm.astype(np.uint8)
def normalize_images(self): si = SequenceImporter() img, channel_names = si(self.settings.input_folder) for i in range(len(channel_names)): image = img[:,:,i] perc = np.percentile(image, [1, 99]) image_norm = 255.0 * (image - perc[0]) / (perc[1] - perc[0]) image_norm[image_norm>255.0] = 255.0 image_norm[image_norm<0] = 0.0 out_folder = os.path.join(self.settings.output_folder, 'normalized_images') if not os.path.isdir(out_folder): print('made %s' % out_folder) os.makedirs(out_folder) filename = os.path.join(out_folder, 'normalized_%s.png' % channel_names[i]) print('writing %s' % filename) skimage.io.imsave(filename, image_norm.astype(np.uint8)) return
def __call__(self): print('Reading data ... ') si = SequenceImporter(['DNA1']) img, channel_names = si(self.settings.input_folder) image = img[:,:,0] print('Detecting spots ... ') start_time = time.time() coordinates = self.spot_detection_dna_raw(image) diff_time = time.time() - start_time print('\ttime elapsed, spot detection: %.2f s' % diff_time) print('Fissure image ... ') start_time = time.time() fissure_image = self.fissure.get_image(False) filtered_coordinates = list(filter(lambda x: fissure_image[x] == 0, coordinates)) print(len(coordinates), ' --> ', len(filtered_coordinates)) coordinates = filtered_coordinates diff_time = time.time() - start_time print('\ttime elapsed, fissure exclusion: %.2f s' % diff_time) print('get membrane image ... ') start_time = time.time() membrane_img = self.projection() diff_time = time.time() - start_time print('\ttime elapsed, membrane extraction: %.2f s' % diff_time) print('Voronoi diagram ... ') start_time = time.time() ws = self.get_voronoi_regions(image, coordinates, radius=8, cd_image=membrane_img, alpha=0.1) diff_time = time.time() - start_time print('\ttime elapsed, voronoi: %.2f s' % diff_time) print('Writing result image ... ') self.write_image(ws) return ws
def export_cluster_galleries(self, cluster_filename, nb_rows=20, window_size=51, alpha=0.4): filename = os.path.join( self.cluster_folder, 'cluster_assignment%s.pickle' % cluster_filename) if not os.path.isfile(filename): filename = cluster_filename cluster_filename = '' if not os.path.isfile(filename): raise ValueError( "A filename or filename extension must be given." "First, we interpret cluster_filename as a filename extension and look for" "the corresponding pickle file in %s" % self.cluster_folder) output_folder = os.path.join(self.cluster_folder, 'clustergalleries') if not os.path.isdir(output_folder): os.makedirs(output_folder) # import cluster results fp = open(filename, 'rb') full_res = pickle.load(fp) fp.close() cluster_assignment = full_res['res'] col_pal = full_res['colors'] nb_rows_max = nb_rows # read label image with cell contours ws = self.cell_detector.get_image(False) ws_random = make_random_colors(ws) # read image data si = SequenceImporter(self.markers) img, channel_names = si(self.settings.input_folder) nb_channels = len(channel_names) # normalize image data for visualization perc_for_vis = [1, 99.9] percentiles = np.percentile(img, perc_for_vis, axis=(0, 1)) image = 255 * (img.astype(np.float) - percentiles[0]) / (percentiles[1] - percentiles[0]) image[image > 255] = 255 image[image < 0] = 0 image = image.astype(np.uint8) # geometry settings if window_size % 2 == 0: window_size = window_size + 1 radius = (window_size - 1) / 2 nb_cols = len(channel_names) nb_samples = nb_rows * nb_cols # find the centers of the labels props = regionprops(ws, image[:, :, 0]) centers = np.array([props[k]['centroid'] for k in range(len(props))]) ws_labels = dict( zip([props[k]['label'] for k in range(len(props))], [props[k]['centroid'] for k in range(len(props))])) # look over cluster labels for cluster_label in cluster_assignment: labels = np.array(cluster_assignment[cluster_label][0]) if not full_res['indices'] is None: labels = full_res['indices'][labels] else: labels = labels + 1 if nb_rows < len(labels): labels_sample = resample(labels, replace=False, n_samples=nb_rows) else: labels_sample = labels nb_rows_adjusted = len(labels_sample) # output image rgb_image = 255 * np.ones((nb_rows_adjusted * window_size, (nb_channels + 1) * window_size, 3)) for t_row, lab in enumerate(labels_sample): row_, col_ = ws_labels[lab] row = np.rint(row_).astype(np.int) col = np.rint(col_).astype(np.int) ws_label = ws[row, col] # coordinates and dimensions of the vignette y1 = int(max(0, row - radius)) y2 = int(min(row + radius, ws.shape[0])) x1 = int(max(0, col - radius)) x2 = int(min(col + radius, ws.shape[1])) height = np.rint(y2 - y1).astype(np.int) width = np.rint(x2 - x1).astype(np.int) # get contour mask_img = ws[y1:y2, x1:x2] == ws_label mask_img = mask_img.astype(np.uint8) grad_img = dilation(mask_img, disk(1)) - mask_img indices = np.where(grad_img) sample_image = image[y1:y2, x1:x2, :] for t_col in range(nb_channels): color_sample_image = grey2rgb(sample_image[:, :, t_col]) color_sample_image[indices] = ( 1 - alpha) * color_sample_image[ indices] + alpha * np.array([255, 0, 0]) offset_y = t_row * window_size offset_x = t_col * window_size #pdb.set_trace() rgb_image[offset_y:(offset_y + height), offset_x:(offset_x + width), :] = color_sample_image offset_x = nb_channels * window_size rgb_image[offset_y:(offset_y + height), offset_x:(offset_x + width), :] = ws_random[y1:y2, x1:x2] filename = os.path.join( output_folder, 'clustergallery%s_clusterid_%i.png' % (cluster_filename, cluster_label)) print('write %s' % filename) skimage.io.imsave(filename, rgb_image.astype(np.uint8)) # write with matplotlib filename = os.path.join( output_folder, 'mpl_clustergallery%s_clusterid_%i.pdf' % (cluster_filename, cluster_label)) my_dpi = 200 h = rgb_image.shape[0] w = rgb_image.shape[1] h = np.ceil(h / my_dpi).astype(np.int) w = np.ceil(w / my_dpi).astype(np.int) fig = plt.figure(frameon=False) fig.set_size_inches(w, h) #fig.set_size_inches(w,h) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) ax.imshow(rgb_image.astype(np.uint8), aspect='auto', interpolation='none') title_row = self.markers + ['WS'] for t_col in range(nb_channels + 1): offset_y = 0.12 * window_size offset_x = t_col * window_size + 0.02 * window_size plt.text(offset_x, offset_y, title_row[t_col], color='orange', fontsize=2) for t_row, lab in enumerate(labels_sample): row_, col_ = ws_labels[lab] row = np.rint(row_).astype(np.int) col = np.rint(col_).astype(np.int) ws_label = ws[row, col] plt.text(2, (t_row + .5) * window_size, '%i' % ws_label, rotation=90, fontsize=2, color='orange') fig.savefig(filename, dpi=my_dpi) return
def test_segmentation(self): RADIUS = 8 xmin = 656 xmax = 956 ymin = 1020 ymax = 1320 print('Reading data ... ') si = SequenceImporter(['DNA1']) img, channel_names = si(self.settings.input_folder) image = img[xmin:xmax,ymin:ymax,0] print('Detecting spots ... ') start_time = time.time() coordinates = self.spot_detection_dna_raw(image) diff_time = time.time() - start_time print('\ttime elapsed, spot detection: %.2f s' % diff_time) print('Fissure image ... ') start_time = time.time() fissure_image = self.fissure.get_image(False) fissure_image = fissure_image[xmin:xmax,ymin:ymax] filtered_coordinates = list(filter(lambda x: fissure_image[x] == 0, coordinates)) print(len(coordinates), ' --> ', len(filtered_coordinates)) coordinates = filtered_coordinates diff_time = time.time() - start_time print('\ttime elapsed, fissure exclusion: %.2f s' % diff_time) print('get membrane image ... ') membrane_img = self.projection() membrane_img = membrane_img[xmin:xmax,ymin:ymax] print('Voronoi diagram ... ') start_time = time.time() ws = self.get_voronoi_regions(image, coordinates, radius=RADIUS, cd_image=membrane_img, alpha=0.1) diff_time = time.time() - start_time print('\ttime elapsed, voronoi: %.2f s' % diff_time) print('Writing result image ... ') self.write_image_debug(ws) # write a few debug images. ov = Overlays() colors = {1: (255, 0, 0), 2:(40, 120, 255)} out_folder = os.path.join(self.settings.debug_folder, 'cell_segmentation_%s' % self.settings.dataset) spot_img = np.zeros(ws.shape, dtype=np.uint8) col_indices = [x[1] for x in coordinates] row_indices = [x[0] for x in coordinates] spot_img[row_indices, col_indices] = 2 image = self.sp.remove_salt_noise(image) perc = np.percentile(image, [10, 98]) dna_min = np.float(perc[0]) dna_max = np.float(perc[1]) #pdb.set_trace() image = 255.0 * (image - dna_min) / (dna_max - dna_min) image[image>255] = 255 image[image < 0] = 0 image = image.astype(np.uint8) img_overlay = ov.overlay_grey_img(membrane_img, spot_img, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'nuclei_detection_1.png'), img_overlay) img_overlay = ov.overlay_grey_img(image, spot_img, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'nuclei_detection_2.png'), img_overlay) wsl = dilation(ws, disk(1)) - erosion(ws, disk(1)) wsl[wsl>0] = 1 wsl[row_indices, col_indices] = 2 wsl_membrane = wsl img_overlay = ov.overlay_grey_img(membrane_img, wsl, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'cell_contours_with_membrane_1.png'), img_overlay) img_overlay = ov.overlay_grey_img(image, wsl, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'cell_contours_with_membrane_2.png'), img_overlay) ws = self.get_voronoi_regions(image, coordinates, radius=RADIUS) wsl = dilation(ws, disk(1)) - erosion(ws, disk(1)) wsl[wsl>0] = 1 wsl[row_indices, col_indices] = 2 wsl_distance = wsl img_overlay = ov.overlay_grey_img(membrane_img, wsl, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'cell_contours_with_distance_1.png'), img_overlay) img_overlay = ov.overlay_grey_img(image, wsl, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'cell_contours_with_distance_2.png'), img_overlay) skimage.io.imsave(os.path.join(out_folder, 'raw_membrane.png'), membrane_img) skimage.io.imsave(os.path.join(out_folder, 'raw_dna.png'), image) colorimage = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8) colorimage[:,:,0] = image colorimage[:,:,1] = membrane_img skimage.io.imsave(os.path.join(out_folder, 'raw_rgb.png'), colorimage) colors = {1: (30, 150, 255), 2:(30, 150, 255)} img_overlay = ov.overlay_rgb_img(colorimage, wsl_membrane, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'rgb_all_membrane.png'), img_overlay) colors = {1: (30, 150, 255), 2:(30, 150, 255)} img_overlay = ov.overlay_rgb_img(colorimage, wsl_distance, colors=colors, contour=False) skimage.io.imsave(os.path.join(out_folder, 'rgb_all_distance.png'), img_overlay) wsl_distance[wsl_distance>1] = 0 wsl_distance[wsl_distance>0] = 255 skimage.io.imsave(os.path.join(out_folder, 'wsl_distance.png'), wsl_distance) wsl_membrane[wsl_membrane!=1] = 0 wsl_membrane[wsl_membrane>0] = 255 skimage.io.imsave(os.path.join(out_folder, 'wsl_membrane.png'), wsl_membrane) return