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)