def run(self, workspace): import matplotlib import matplotlib.cm import matplotlib.backends.backend_agg import matplotlib.transforms from cellprofiler.gui.tools import figure_to_image, only_display_image # # Get the image # image = workspace.image_set.get_image(self.image_name.value) if self.wants_image: pixel_data = image.pixel_data else: pixel_data = np.zeros(image.pixel_data.shape[:2]) object_set = workspace.object_set if self.objects_or_image == OI_OBJECTS: if self.objects_name.value in object_set.get_object_names(): objects = object_set.get_objects(self.objects_name.value) else: objects = None workspace.display_data.pixel_data = pixel_data if self.use_color_map(): workspace.display_data.labels = objects.segmented # # Get the measurements and positions # measurements = workspace.measurements if self.objects_or_image == OI_IMAGE: value = measurements.get_current_image_measurement(self.measurement.value) values = [value] x = [pixel_data.shape[1] / 2] x_offset = np.random.uniform(high=1.0, low=-1.0) x[0] += x_offset y = [pixel_data.shape[0] / 2] y_offset = np.sqrt(1 - x_offset ** 2) y[0] += y_offset else: values = measurements.get_current_measurement(self.objects_name.value, self.measurement.value) if objects is not None and len(values) < objects.count: temp = np.zeros(objects.count, values.dtype) temp[: len(values)] = values temp[len(values) :] = np.nan values = temp x = measurements.get_current_measurement(self.objects_name.value, M_LOCATION_CENTER_X) x_offset = np.random.uniform(high=1.0, low=-1.0, size=x.shape) y_offset = np.sqrt(1 - x_offset ** 2) x += self.offset.value * x_offset y = measurements.get_current_measurement(self.objects_name.value, M_LOCATION_CENTER_Y) y += self.offset.value * y_offset mask = ~(np.isnan(values) | np.isnan(x) | np.isnan(y)) values = values[mask] x = x[mask] y = y[mask] workspace.display_data.mask = mask workspace.display_data.values = values workspace.display_data.x = x workspace.display_data.y = y fig = matplotlib.figure.Figure() axes = fig.add_subplot(1, 1, 1) def imshow_fn(pixel_data): # Note: requires typecast to avoid failure during # figure_to_image (IMG-764) img = pixel_data * 255 img[img < 0] = 0 img[img > 255] = 255 img = img.astype(np.uint8) axes.imshow(img, cmap=matplotlib.cm.Greys_r) self.display_on_figure(workspace, axes, imshow_fn) canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) if self.saved_image_contents == E_AXES: fig.set_frameon(False) if not self.use_color_map(): fig.subplots_adjust(0.1, 0.1, 0.9, 0.9, 0, 0) shape = pixel_data.shape width = float(shape[1]) / fig.dpi height = float(shape[0]) / fig.dpi fig.set_figheight(height) fig.set_figwidth(width) elif self.saved_image_contents == E_IMAGE: if self.use_color_map(): fig.axes[1].set_visible(False) only_display_image(fig, pixel_data.shape) else: if not self.use_color_map(): fig.subplots_adjust(0.1, 0.1, 0.9, 0.9, 0, 0) pixel_data = figure_to_image(fig, dpi=fig.dpi) image = cpi.Image(pixel_data) workspace.image_set.add(self.display_image.value, image)
def run(self, workspace): import matplotlib import matplotlib.cm import matplotlib.backends.backend_agg import matplotlib.transforms from cellprofiler.gui.tools import figure_to_image, only_display_image # # Get the image # image = workspace.image_set.get_image(self.image_name.value) if self.wants_image: pixel_data = image.pixel_data else: pixel_data = np.zeros(image.pixel_data.shape[:2]) object_set = workspace.object_set if self.objects_or_image == OI_OBJECTS: if self.objects_name.value in object_set.get_object_names(): objects = object_set.get_objects(self.objects_name.value) else: objects = None workspace.display_data.pixel_data = pixel_data if self.use_color_map(): workspace.display_data.labels = objects.segmented # # Get the measurements and positions # measurements = workspace.measurements if self.objects_or_image == OI_IMAGE: value = measurements.get_current_image_measurement( self.measurement.value) values = [value] x = [pixel_data.shape[1] / 2] x_offset = np.random.uniform(high=1.0, low=-1.0) x[0] += x_offset y = [pixel_data.shape[0] / 2] y_offset = np.sqrt(1 - x_offset**2) y[0] += y_offset else: values = measurements.get_current_measurement( self.objects_name.value, self.measurement.value) if objects is not None: if len(values) < objects.count: temp = np.zeros(objects.count, values.dtype) temp[:len(values)] = values temp[len(values):] = np.nan values = temp elif len(values) > objects.count: # If the values for something (say, object number) are greater # than the actual number of objects we have, some might have been # filtered out/removed. We'll need to diff the arrays to figure out # what objects to remove indices = objects.indices diff = np.setdiff1d(indices, np.unique(objects.segmented)) values = np.delete(values, diff) x = measurements.get_current_measurement(self.objects_name.value, M_LOCATION_CENTER_X) x_offset = np.random.uniform(high=1.0, low=-1.0, size=x.shape) y_offset = np.sqrt(1 - x_offset**2) x += self.offset.value * x_offset y = measurements.get_current_measurement(self.objects_name.value, M_LOCATION_CENTER_Y) y += self.offset.value * y_offset mask = ~(np.isnan(values) | np.isnan(x) | np.isnan(y)) values = values[mask] x = x[mask] y = y[mask] workspace.display_data.mask = mask workspace.display_data.values = values workspace.display_data.x = x workspace.display_data.y = y fig = matplotlib.figure.Figure() axes = fig.add_subplot(1, 1, 1) def imshow_fn(pixel_data): # Note: requires typecast to avoid failure during # figure_to_image (IMG-764) img = pixel_data * 255 img[img < 0] = 0 img[img > 255] = 255 img = img.astype(np.uint8) axes.imshow(img, cmap=matplotlib.cm.Greys_r) self.display_on_figure(workspace, axes, imshow_fn) canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) if self.saved_image_contents == E_AXES: fig.set_frameon(False) if not self.use_color_map(): fig.subplots_adjust(0.1, .1, .9, .9, 0, 0) shape = pixel_data.shape width = float(shape[1]) / fig.dpi height = float(shape[0]) / fig.dpi fig.set_figheight(height) fig.set_figwidth(width) elif self.saved_image_contents == E_IMAGE: if self.use_color_map(): fig.axes[1].set_visible(False) only_display_image(fig, pixel_data.shape) else: if not self.use_color_map(): fig.subplots_adjust(.1, .1, .9, .9, 0, 0) pixel_data = figure_to_image(fig, dpi=fig.dpi) image = cpi.Image(pixel_data) workspace.image_set.add(self.display_image.value, image)
def run(self, workspace): """Run the module workspace - The workspace contains pipeline - instance of cpp for this run image_set - the images in the image set being processed object_set - the objects (labeled masks) in this image set measurements - the measurements for this run frame - the parent frame to whatever frame is created. None means don't draw. """ background_image = self.get_background_image(workspace, None) if (self.each_or_once == EO_ONCE and self.get_good_gridding(workspace) is not None): gridding = self.get_good_gridding(workspace) if self.auto_or_manual == AM_AUTOMATIC: gridding = self.run_automatic(workspace) elif self.manual_choice == MAN_COORDINATES: gridding = self.run_coordinates(workspace) elif self.manual_choice == MAN_MOUSE: gridding = workspace.interaction_request( self, background_image, workspace.measurements.image_set_number) self.set_good_gridding(workspace, gridding) workspace.set_grid(self.grid_image.value, gridding) # # Save measurements # self.add_measurement( workspace, F_X_LOCATION_OF_LOWEST_X_SPOT, gridding.x_location_of_lowest_x_spot, ) self.add_measurement( workspace, F_Y_LOCATION_OF_LOWEST_Y_SPOT, gridding.y_location_of_lowest_y_spot, ) self.add_measurement(workspace, F_ROWS, gridding.rows) self.add_measurement(workspace, F_COLUMNS, gridding.columns) self.add_measurement(workspace, F_X_SPACING, gridding.x_spacing) self.add_measurement(workspace, F_Y_SPACING, gridding.y_spacing) # update background image background_image = self.get_background_image(workspace, gridding) workspace.display_data.gridding = gridding.serialize() workspace.display_data.background_image = background_image workspace.display_data.image_set_number = ( workspace.measurements.image_set_number) if self.wants_image: import matplotlib.transforms import matplotlib.figure import matplotlib.backends.backend_agg from cellprofiler.gui.tools import figure_to_image figure = matplotlib.figure.Figure() canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure) ax = figure.add_subplot(1, 1, 1) self.display_grid(background_image, gridding, workspace.measurements.image_set_number, ax) # # This is the recipe for just showing the axis # figure.set_frameon(False) ax.set_axis_off() figure.subplots_adjust(0, 0, 1, 1, 0, 0) ai = ax.images[0] shape = ai.get_size() dpi = figure.dpi width = float(shape[1]) / dpi height = float(shape[0]) / dpi figure.set_figheight(height) figure.set_figwidth(width) bbox = matplotlib.transforms.Bbox( np.array([[0.0, 0.0], [width, height]])) transform = matplotlib.transforms.Affine2D( np.array([[dpi, 0, 0], [0, dpi, 0], [0, 0, 1]])) figure.bbox = matplotlib.transforms.TransformedBbox( bbox, transform) image_pixels = figure_to_image(figure, dpi=dpi) image = cpi.Image(image_pixels) workspace.image_set.add(self.save_image_name.value, image)
def run(self, workspace): """Run the module workspace - The workspace contains pipeline - instance of cpp for this run image_set - the images in the image set being processed object_set - the objects (labeled masks) in this image set measurements - the measurements for this run frame - the parent frame to whatever frame is created. None means don't draw. """ background_image = self.get_background_image(workspace, None) if (self.each_or_once == EO_ONCE and self.get_good_gridding(workspace) is not None): gridding = self.get_good_gridding(workspace) if self.auto_or_manual == AM_AUTOMATIC: gridding = self.run_automatic(workspace) elif self.manual_choice == MAN_COORDINATES: gridding = self.run_coordinates(workspace) elif self.manual_choice == MAN_MOUSE: gridding = workspace.interaction_request(self, background_image, workspace.measurements.image_set_number) self.set_good_gridding(workspace, gridding) workspace.set_grid(self.grid_image.value, gridding) # # Save measurements # self.add_measurement(workspace, F_X_LOCATION_OF_LOWEST_X_SPOT, gridding.x_location_of_lowest_x_spot) self.add_measurement(workspace, F_Y_LOCATION_OF_LOWEST_Y_SPOT, gridding.y_location_of_lowest_y_spot) self.add_measurement(workspace, F_ROWS, gridding.rows) self.add_measurement(workspace, F_COLUMNS, gridding.columns) self.add_measurement(workspace, F_X_SPACING, gridding.x_spacing) self.add_measurement(workspace, F_Y_SPACING, gridding.y_spacing) # update background image background_image = self.get_background_image(workspace, gridding) workspace.display_data.gridding = gridding.serialize() workspace.display_data.background_image = background_image workspace.display_data.image_set_number = workspace.measurements.image_set_number if self.wants_image: import matplotlib.transforms import matplotlib.figure import matplotlib.backends.backend_agg from cellprofiler.gui.tools import figure_to_image figure = matplotlib.figure.Figure() canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure) ax = figure.add_subplot(1, 1, 1) self.display_grid(background_image, gridding, workspace.measurements.image_set_number, ax) # # This is the recipe for just showing the axis # figure.set_frameon(False) ax.set_axis_off() figure.subplots_adjust(0, 0, 1, 1, 0, 0) ai = ax.images[0] shape = ai.get_size() dpi = figure.dpi width = float(shape[1]) / dpi height = float(shape[0]) / dpi figure.set_figheight(height) figure.set_figwidth(width) bbox = matplotlib.transforms.Bbox( np.array([[0.0, 0.0], [width, height]])) transform = matplotlib.transforms.Affine2D( np.array([[dpi, 0, 0], [0, dpi, 0], [0, 0, 1]])) figure.bbox = matplotlib.transforms.TransformedBbox(bbox, transform) image_pixels = figure_to_image(figure, dpi=dpi) image = cpi.Image(image_pixels) workspace.image_set.add(self.save_image_name.value, image)