def make_workspace(self, file_names): module = L.LoadSingleImage() module.module_num = 1 for i, file_name in enumerate(file_names): if i > 0: module.add_file() module.file_settings[i].image_name.value = self.get_image_name(i) module.file_settings[i].file_name.value = file_name pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cpi.ImageSetList() workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) return workspace, module
def test_03_03_MultipleImageSets(self): np.random.seed(0) x = cpmeas.Measurements() x.add_measurement("Image", "Feature", "Value1") m1 = np.random.rand(10) x.add_measurement("Nuclei", "Feature", m1) x.next_image_set() x.add_measurement("Image", "Feature", "Value2") m2 = np.random.rand(10) x.add_measurement("Nuclei", "Feature", m2) self.assertEqual(x.get_current_measurement("Image", "Feature"), "Value2") self.assertTrue((x.get_current_measurement("Nuclei", "Feature") == m2).all()) for a, b in zip(x.get_all_measurements("Image", "Feature"), ["Value1", "Value2"]): self.assertEqual(a, b) for a, b in zip(x.get_all_measurements("Nuclei", "Feature"), [m1, m2]): self.assertTrue((a == b).all())
def make_workspace(self, labels, parent_image=None): object_set = cpo.ObjectSet() objects = cpo.Objects() objects.segmented = labels objects.parent_image = parent_image object_set.add_objects(objects, OBJECTS_NAME) pipeline = cpp.Pipeline() module = mia.MeasureImageAreaOccupied() module.module_num = 1 module.operands[0].operand_objects.value = OBJECTS_NAME pipeline.add_module(module) image_set_list = cpi.ImageSetList() workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, cpmm.Measurements(), image_set_list) return workspace
def make_workspace(self, parents, children, fake_measurement=False): '''Make a workspace for testing Relate''' pipeline = cpp.Pipeline() if fake_measurement: class FakeModule(cpm.CPModule): def get_measurement_columns(self, pipeline): return [(CHILD_OBJECTS, MEASUREMENT, cpmeas.COLTYPE_FLOAT), (CHILD_OBJECTS, IGNORED_MEASUREMENT, cpmeas.COLTYPE_INTEGER)] module = FakeModule() module.module_num = 1 pipeline.add_module(module) module = R.Relate() module.parent_name.value = PARENT_OBJECTS module.sub_object_name.value = CHILD_OBJECTS module.find_parent_child_distances.value = R.D_NONE module.module_num = 2 if fake_measurement else 1 pipeline.add_module(module) object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) m = cpmeas.Measurements() m.add_image_measurement(cpmeas.GROUP_NUMBER, 1) m.add_image_measurement(cpmeas.GROUP_INDEX, 1) workspace = cpw.Workspace(pipeline, module, image_set, object_set, m, image_set_list) o = cpo.Objects() if parents.shape[1] == 3: # IJV format o.ijv = parents else: o.segmented = parents object_set.add_objects(o, PARENT_OBJECTS) o = cpo.Objects() if children.shape[1] == 3: o.ijv = children else: o.segmented = children object_set.add_objects(o, CHILD_OBJECTS) return workspace, module
def test_04_02_object_outlines(self): labels = np.zeros((30,40), int) labels[10:15, 20:30] = 1 expected_outlines = labels != 0 expected_outlines[11:14,21:29] = False filename = "myobjects.png" directory = tempfile.mkdtemp() cpprefs.set_default_image_directory(directory) pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L") pilimage.save(os.path.join(directory, filename)) del pilimage try: module = L.LoadSingleImage() module.module_num = 1 module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME) fs = module.file_settings[0] fs.file_name.value = filename fs.image_objects_choice.value = L.IO_OBJECTS fs.objects_name.value = OBJECTS_NAME fs.wants_outlines.value = True fs.outlines_name.value = OUTLINES_NAME pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) m = cpmeas.Measurements() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace( pipeline, module, image_set, object_set, m, image_set_list) module.prepare_run(workspace) module.run(workspace) outlines = image_set.get_image(OUTLINES_NAME) np.testing.assert_equal(outlines.pixel_data, expected_outlines) finally: try: os.remove(os.path.join(directory, filename)) os.rmdir(directory) except: print "Failed to delete directory " + directory
def make_workspace(self, labels, pixel_data, mask=None): image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cpi.Image(pixel_data, mask)) object_set = cpo.ObjectSet() o = cpo.Objects() o.segmented = labels object_set.add_objects(o, OBJECT_NAME) pipeline = P.Pipeline() module = MOI.MeasureObjectIntensity() module.images[0].name.value = IMAGE_NAME module.objects[0].name.value = OBJECT_NAME module.module_num = 1 pipeline.add_listener(self.error_callback) pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) return workspace, module
def make_workspace(self, image_set_count): image_set_list = cpi.ImageSetList() for i in range(image_set_count): image_set = image_set_list.get_image_set(i) module = L.LabelImages() pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) module.module_num = 1 pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) return workspace, module
def make_workspace(self, labels): image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) object_set = cpo.ObjectSet() objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) m = cpmeas.Measurements() module = cpmoas.MeasureObjectAreaShape() module.module_num = 1 module.object_groups[0].name.value = OBJECTS_NAME pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set, object_set, m, image_set_list) return workspace, module
def make_workspace(self, image, mask=None): '''Make a workspace for testing FindEdges''' module = F.FindEdges() module.image_name.value = INPUT_IMAGE_NAME module.output_image_name.value = OUTPUT_IMAGE_NAME pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image) if mask is None else cpi.Image(image,mask)) return workspace, module
def test_01_02_run(self): measurements = cpmeas.Measurements() image_set_list = cpi.ImageSetList() workspace = cpw.Workspace(self.pipeline, self.module, None, None, measurements, image_set_list) self.assertTrue(self.module.prepare_run(workspace)) for i, expected in enumerate((IMAGE_1, IMAGE_2)): image_number = i + 1 if hasattr(measurements, "get_image"): measurements.next_image_set(image_number) image_set = measurements else: image_set = image_set_list.get_image_set(image_number) workspace = cpw.Workspace(self.pipeline, self.module, image_set, None, measurements, image_set_list) self.module.run(workspace) image = image_set.get_image(IMAGE_NAME) pixel_data = (image.pixel_data * 255).astype(int) np.testing.assert_array_equal(pixel_data, expected)
def make_workspace(self): module = C.ConvertToImage() labels = np.reshape(np.arange(256),(16,16)) pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) module.image_name.value = IMAGE_NAME module.object_name.value = OBJECTS_NAME return (workspace,module)
def run_group_request(self, session_id, message_type, message): '''Handle a run-group request message''' pipeline = cpp.Pipeline() m = cpmeas.Measurements() image_group = m.hdf5_dict.hdf5_file.create_group("ImageData") if len(message) < 2: self.raise_cellprofiler_exception(session_id, "Missing run request sections") return pipeline_txt = message.pop(0).bytes image_metadata = message.pop(0).bytes n_image_sets = None try: image_metadata = json.loads(image_metadata) channel_names = [] for channel_name, channel_metadata in image_metadata: channel_names.append(channel_name) if len(message) < 1: self.raise_cellprofiler_exception( session_id, "Missing binary data for channel %s" % channel_name) return None, None, None pixel_data = self.decode_image(channel_metadata, message.pop(0).bytes, grouping_allowed=True) if pixel_data.ndim < 3: self.raise_cellprofiler_exception( session_id, "The image for channel %s does not have a Z or T dimension" ) return if n_image_sets is None: n_image_sets = pixel_data.shape[0] elif n_image_sets != pixel_data.shape[0]: self.raise_cellprofiler_exception( session_id, "The images passed have different numbers of Z or T planes" ) return image_group.create_dataset(channel_name, data=pixel_data) except Exception, e: self.raise_cellprofiler_exception(session_id, e.message) return None, None, None
def run_module(self, image, labels, center_labels=None, bin_count=4, maximum_radius=100, wants_scaled=True): '''Run the module, returning the measurements image - matrix representing the image to be analyzed labels - labels matrix of objects to be analyzed center_labels - labels matrix of alternate centers or None for self centers bin_count - # of radial bins ''' module = M.MeasureObjectRadialDistribution() module.images[0].image_name.value = IMAGE_NAME module.objects[0].object_name.value = OBJECT_NAME object_set = cpo.ObjectSet() main_objects = cpo.Objects() main_objects.segmented = labels object_set.add_objects(main_objects, OBJECT_NAME) if center_labels is None: module.objects[0].center_choice.value = M.C_SELF else: module.objects[0].center_choice.value = M.C_OTHER module.objects[0].center_object_name.value = CENTER_NAME center_objects = cpo.Objects() center_objects.segmented = center_labels object_set.add_objects(center_objects, CENTER_NAME) module.bin_counts[0].bin_count.value = bin_count module.bin_counts[0].wants_scaled.value = wants_scaled module.bin_counts[0].maximum_radius.value = maximum_radius pipeline = cpp.Pipeline() measurements = cpmeas.Measurements() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) img = cpi.Image(image) image_set.add(IMAGE_NAME, img) workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) module.run(workspace) return measurements
def make_workspace(self, labels, image, mask=None, intensity_image=None, wants_graph=False): m = cpmeas.Measurements() image_set_list = cpi.ImageSetList() m.add_measurement(cpmeas.IMAGE, cpmeas.GROUP_NUMBER, 1) m.add_measurement(cpmeas.IMAGE, cpmeas.GROUP_INDEX, 1) image_set = m img = cpi.Image(image, mask) image_set.add(IMAGE_NAME, img) object_set = cpo.ObjectSet() o = cpo.Objects() o.segmented = labels object_set.add_objects(o, OBJECT_NAME) module = M.MeasureNeurons() module.image_name.value = IMAGE_NAME module.seed_objects_name.value = OBJECT_NAME if intensity_image is not None: img = cpi.Image(intensity_image) image_set.add(INTENSITY_IMAGE_NAME, img) module.intensity_image_name.value = INTENSITY_IMAGE_NAME if wants_graph: module.wants_neuron_graph.value = True module.directory.dir_choice = cps.ABSOLUTE_FOLDER_NAME module.directory.custom_path = self.temp_dir module.edge_file_name.value = EDGE_FILE module.vertex_file_name.value = VERTEX_FILE module.module_num = 1 pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.LoadExceptionEvent)) self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set, object_set, m, image_set_list) return workspace, module
def load(self, filename, load_pipeline): '''Load a workspace from a .cpi file filename - path to file to load load_pipeline - true to load the pipeline from the file, false to use the current pipeline. ''' import shutil from .pipeline import M_PIPELINE import cellprofiler.measurements as cpmeas if self.__measurements is not None: self.close() # # Copy the file to a temporary location before opening # fd, self.__filename = cpmeas.make_temporary_file() os.close(fd) shutil.copyfile(filename, self.__filename) self.__measurements = cpmeas.Measurements(filename=self.__filename, mode="r+") if self.__file_list is not None: self.__file_list.remove_notification_callback( self.__on_file_list_changed) self.__file_list = HDF5FileList(self.measurements.hdf5_dict.hdf5_file) self.__file_list.add_notification_callback(self.__on_file_list_changed) if load_pipeline and self.__measurements.has_feature( cpmeas.EXPERIMENT, M_PIPELINE): pipeline_txt = self.__measurements.get_experiment_measurement( M_PIPELINE).encode("utf-8") self.pipeline.load(StringIO(pipeline_txt)) elif load_pipeline: self.pipeline.clear() else: fd = StringIO() self.pipeline.savetxt(fd, save_image_plane_details=False) self.__measurements.add_experiment_measurement( M_PIPELINE, fd.getvalue()) self.notify(self.WorkspaceLoadedEvent(self))
def make_workspace(self, measurement, labels=None, image=None): object_set = cpo.ObjectSet() module = D.DisplayDataOnImage() module.module_num = 1 module.image_name.value = INPUT_IMAGE_NAME module.display_image.value = OUTPUT_IMAGE_NAME module.objects_name.value = OBJECTS_NAME m = cpmeas.Measurements() if labels is None: module.objects_or_image.value = D.OI_IMAGE m.add_image_measurement(MEASUREMENT_NAME, measurement) if image is None: image = np.zeros((50, 120)) else: module.objects_or_image.value = D.OI_OBJECTS o = cpo.Objects() o.segmented = labels object_set.add_objects(o, OBJECTS_NAME) m.add_measurement(OBJECTS_NAME, MEASUREMENT_NAME, np.array(measurement)) y, x = centers_of_labels(labels) m.add_measurement(OBJECTS_NAME, "Location_Center_X", x) m.add_measurement(OBJECTS_NAME, "Location_Center_Y", y) if image is None: image = np.zeros(labels.shape) module.measurement.value = MEASUREMENT_NAME pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image)) workspace = cpw.Workspace(pipeline, module, image_set, object_set, m, image_set_list) return workspace, module
def make_obj_workspace(self, ground_truth_obj, id_obj, ground_truth, id): '''make a workspace to test comparing objects''' ''' ground truth object and ID object are dictionaires w/ the following keys''' '''i - i component of pixel coordinates j - j component of pixel coordinates l - label ''' module = C.CalculateImageOverlap() module.module_num = 1 module.obj_or_img.value = O_OBJ module.object_name_GT.value = GROUND_TRUTH_OBJ module.img_obj_found_in_GT.value = GROUND_TRUTH_OBJ_IMAGE_NAME module.object_name_ID.value = ID_OBJ module.img_obj_found_in_ID.value = ID_OBJ_IMAGE_NAME pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) for name, d in ((GROUND_TRUTH_OBJ_IMAGE_NAME, ground_truth), (ID_OBJ_IMAGE_NAME, id)): image = cpi.Image(d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")) image_set.add(name, image) object_set = cpo.ObjectSet() for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ, id_obj)): object = cpo.Objects() if d.shape[1] == 3: object.ijv = d else: object.segmented = d object_set.add_objects(object, name) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) return workspace, module
def test_03_02_zernikes_are_different(self): '''Regression test of IMG-773''' np.random.seed(32) labels = np.zeros((40, 20), int) # # Make two "objects" composed of random foreground/background # labels[1:19, 1:19] = (np.random.uniform(size=(18, 18)) > .5).astype(int) labels[21:39, 1:19] = (np.random.uniform(size=(18, 18)) > .5).astype(int) * 2 objects = cpo.Objects() objects.segmented = labels object_set = cpo.ObjectSet() object_set.add_objects(objects, "SomeObjects") module = cpmoas.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True module.module_num = 1 image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() pipeline = cpp.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) features = [ x[1] for x in module.get_measurement_columns(pipeline) if x[0] == "SomeObjects" and x[1].startswith("AreaShape_Zernike") ] for feature in features: values = measurements.get_current_measurement( "SomeObjects", feature) self.assertEqual(len(values), 2) self.assertNotEqual(values[0], values[1])
def make_workspace(self, labels, overlap_choice, masking_objects = None, masking_image = None, renumber = True, wants_outlines = False): module = M.MaskObjects() module.module_num = 1 module.object_name.value = INPUT_OBJECTS module.remaining_objects.value = OUTPUT_OBJECTS module.mask_choice.value = (M.MC_OBJECTS if masking_objects is not None else M.MC_IMAGE) module.masking_image.value = MASKING_IMAGE module.masking_objects.value = MASKING_OBJECTS module.retain_or_renumber.value = (M.R_RENUMBER if renumber else M.R_RETAIN) module.overlap_choice.value = overlap_choice module.wants_outlines.value = wants_outlines module.outlines_name.value = OUTPUT_OUTLINES pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) object_set = cpo.ObjectSet() io = cpo.Objects() io.segmented = labels object_set.add_objects(io, INPUT_OBJECTS) if masking_objects is not None: oo = cpo.Objects() oo.segmented = masking_objects object_set.add_objects(oo, MASKING_OBJECTS) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) if masking_image is not None: mi = cpi.Image(masking_image) image_set.add(MASKING_IMAGE, mi) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) return workspace, module
def save_pipeline(self, workspace, outf=None): '''Save the pipeline in Batch_data.mat Save the pickled image_set_list state in a setting and put this module in batch mode. if outf is not None, it is used as a file object destination. ''' from cellprofiler.utilities.get_revision import version if outf is None: if self.wants_default_output_directory.value: path = cpprefs.get_default_output_directory() else: path = cpprefs.get_absolute_path(self.custom_output_directory.value) h5_path = os.path.join(path, F_BATCH_DATA_H5) else: h5_path = outf image_set_list = workspace.image_set_list pipeline = workspace.pipeline m = cpmeas.Measurements(copy = workspace.measurements, filename = h5_path) assert isinstance(image_set_list, cpi.ImageSetList) assert isinstance(pipeline, cpp.Pipeline) assert isinstance(m, cpmeas.Measurements) pipeline = pipeline.copy() target_workspace = cpw.Workspace(pipeline, None, None, None, m, image_set_list, workspace.frame) pipeline.prepare_to_create_batch(target_workspace, self.alter_path) bizarro_self = pipeline.module(self.module_num) bizarro_self.revision.value = version if self.wants_default_output_directory: bizarro_self.custom_output_directory.value = \ self.alter_path(cpprefs.get_default_output_directory()) bizarro_self.default_image_directory.value = \ self.alter_path(cpprefs.get_default_image_directory()) bizarro_self.batch_mode.value = True pipeline.write_pipeline_measurement(m) del m
def make_workspace(self, object_dict={}, image_dict={}): '''Make a workspace for testing MeasureImageIntensity''' module = M.MeasureImageIntensity() pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) for key in image_dict.keys(): image_set.add(key, cpi.Image(image_dict[key])) for key in object_dict.keys(): o = cpo.Objects() o.segmented = object_dict[key] object_set.add_objects(o, key) return workspace, module
def test_01_02_two(self): np.random.seed(12) mm = [] for i in range(2): m = cpmeas.Measurements() self.write_image_measurements(m, "foo", 5) self.write_object_measurements(m, "myobjects", "bar", [3, 6, 2, 9, 16]) self.write_experiment_measurement(m, "baz") mm.append(m) result = self.execute_merge_files(mm) self.assertAlmostEqual(result.get_experiment_measurement("baz"), mm[0].get_experiment_measurement("baz")) ro = result.get_all_measurements("myobjects", "bar") moo = [m.get_all_measurements("myobjects", "bar") for m in mm] for i in range(5): for j in range(2): np.testing.assert_almost_equal(ro[i + j * 5], moo[j][i]) self.assertEqual(len(ro[i + j * 5]), len(moo[j][i])) np.testing.assert_almost_equal(ro[i + j * 5], moo[j][i])
def make_workspace(self, mdict, controls_measurement, dose_measurements=[]): '''Make a workspace and module for running CalculateStatistics mdict - a two-level dictionary that mimics the measurements structure for instance: mdict = { cpmeas.Image: { "M1": [ 1,2,3] }} for the measurement M1 with values for 3 image sets controls_measurement - the name of the controls measurement ''' module = C.CalculateStatistics() module.module_num = 1 module.grouping_values.value = controls_measurement pipeline = cpp.Pipeline() pipeline.add_module(module) m = cpmeas.Measurements() nimages = None for object_name in mdict.keys(): odict = mdict[object_name] for feature in odict.keys(): m.add_all_measurements(object_name, feature, odict[feature]) if nimages is None: nimages = len(odict[feature]) else: self.assertEqual(nimages, len(odict[feature])) if object_name == cpmeas.IMAGE and feature in dose_measurements: if len(module.dose_values) > 1: module.add_dose_value() dv = module.dose_values[-1] dv.measurement.value = feature m.image_set_number = nimages image_set_list = cpi.ImageSetList() for i in range(nimages): image_set = image_set_list.get_image_set(i) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), m, image_set_list) return workspace, module
def make_workspace(self, input_pixels, crop_image = None, cropping = None, crop_objects = None ): """Return a workspace with the given images installed and the crop module""" image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) module = cpmc.Crop() module.module_num = 1 image_set.add(INPUT_IMAGE, cpi.Image(input_pixels)) module.image_name.value = INPUT_IMAGE module.cropped_image_name.value = OUTPUT_IMAGE if crop_image is not None: image_set.add(CROP_IMAGE, cpi.Image(crop_image)) module.image_mask_source.value = CROP_IMAGE if cropping is not None: image_set.add(CROPPING, cpi.Image(np.zeros(cropping.shape), crop_mask = cropping)) module.cropping_mask_source.value = CROPPING object_set = cpo.ObjectSet() if crop_objects is not None: objects = cpo.Objects() objects.segmented = crop_objects object_set.add_objects(objects, CROP_OBJECTS) pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) m = cpm.Measurements() workspace = cpw.Workspace(pipeline, module, image_set, object_set, m, image_set_list) m.add_measurement(cpm.IMAGE, cpm.GROUP_INDEX, 0, image_set_number = 1) m.add_measurement(cpm.IMAGE, cpm.GROUP_NUMBER, 1, image_set_number = 1) return workspace, module
def make_pipeline(self, image, mask, subsample_size, image_sample_size, element_size, granular_spectrum_length, labels=None): '''Make a pipeline with a MeasureGranularity module image - measure granularity on this image mask - exclude / include pixels from measurement. None = no mask subsample_size, etc. - values for corresponding settings in the module returns tuple of module & workspace ''' module = M.MeasureGranularity() module.module_num = 1 image_setting = module.images[0] # assert isinstance(image_setting, M.MeasureGranularity) image_setting.image_name.value = IMAGE_NAME image_setting.subsample_size.value = subsample_size image_setting.image_sample_size.value = image_sample_size image_setting.element_size.value = element_size image_setting.granular_spectrum_length.value = granular_spectrum_length image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) img = cpi.Image(image, mask) image_set.add(IMAGE_NAME, img) pipeline = cpp.Pipeline() def error_callback(event, caller): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(error_callback) pipeline.add_module(module) object_set = cpo.ObjectSet() if labels is not None: objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) image_setting.add_objects() image_setting.objects[0].objects_name.value = OBJECTS_NAME workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) return module, workspace
def test_04_03_load_planes(self): file_name = "RLM1 SSN3 300308 008015000.flex" path = testimages_directory() pathname = os.path.join(path, file_name) url = pathname2url(pathname) ftrs = (cpmeas.C_URL, cpmeas.C_SERIES, cpmeas.C_FRAME) channels = ("Channel1", "Channel2") header = ",".join([",".join(["_".join((ftr, channel)) for ftr in ftrs]) for channel in channels]) csv_lines = [header] for series in range(4): csv_lines.append(",".join(['"%s","%d","%d"' % (url, series, frame) for frame in range(2)])) csv_text = "\n".join(csv_lines) pipeline, module, filename = self.make_pipeline(csv_text) assert isinstance(module, L.LoadData) m = cpmeas.Measurements() image_set_list = cpi.ImageSetList() try: workspace = cpw.Workspace(pipeline, module, m, None, m, image_set_list) self.assertTrue(module.prepare_run(workspace)) pixel_hashes = [] for i in range(4): m.next_image_set(i+1) module.run(workspace) chashes = [] for channel in channels: pixel_data = m.get_image(channel).pixel_data h = hashlib.md5() h.update(pixel_data) chashes.append(h.digest()) self.assertNotEqual(chashes[0], chashes[1]) for j, ph in enumerate(pixel_hashes): for k, phh in enumerate(ph): for l, phd in enumerate(chashes): self.assertNotEqual(phh, phd) pixel_hashes.append(chashes) finally: os.remove(filename)
def test_03_07_metadata(self): m = cpmeas.Measurements() m.add_image_measurement("Metadata_Path", "2") s = cps.DirectoryPath("whatever", allow_metadata = True) for dir_choice, expected in ( ( cps.DEFAULT_INPUT_SUBFOLDER_NAME, os.path.join(cpprefs.get_default_image_directory(), "0", "2")), ( cps.DEFAULT_OUTPUT_SUBFOLDER_NAME, os.path.join(cpprefs.get_default_output_directory(), "0", "2")), ( cps.ABSOLUTE_FOLDER_NAME, os.path.join(self.root_directory, "2")), ( cps.URL_FOLDER_NAME, "http://www.cellprofiler.org/2")): s.dir_choice = dir_choice if dir_choice in (cps.DEFAULT_INPUT_SUBFOLDER_NAME, cps.DEFAULT_OUTPUT_SUBFOLDER_NAME): s.custom_path = "0" + os.path.sep.replace('\\','\\\\') + "\\g<Path>" elif dir_choice == cps.ABSOLUTE_FOLDER_NAME: s.custom_path = self.root_directory + os.path.sep.replace('\\','\\\\') + "\\g<Path>" else: s.custom_path = "http://www.cellprofiler.org/\\g<Path>" self.assertEqual(s.get_absolute_path(m), expected)
def make_workspace(self, image1, image2, objects=None): '''Make a workspace for testing ApplyThreshold''' module = M.MeasureCorrelation() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) for image_group, name, image in zip(module.image_groups, (IMAGE1_NAME, IMAGE2_NAME), (image1, image2)): image_group.image_name.value = name image_set.add(name, image) object_set = cpo.ObjectSet() if objects is None: module.images_or_objects.value = M.M_IMAGES else: module.images_or_objects.value = M.M_IMAGES_AND_OBJECTS module.object_groups[0].object_name.value = OBJECTS_NAME object_set.add_objects(objects, OBJECTS_NAME) pipeline = cpp.Pipeline() workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) return workspace, module
def test_02_02_erase_keep(self): module = S.SpeedUpCellProfiler() module.how_to_remove.value = S.C_KEEP module.image_names[0].image_name.value = "Image1" module.module_num = 1 image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add("Image1", cpi.Image(np.zeros((10,10)))) image_set.add("Image2", cpi.Image(np.zeros((10,10)))) pipeline = cpp.Pipeline() def callback(caller,event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) module.run(workspace) image = image_set.get_image("Image2") self.assertFalse(isinstance(image, cpi.Image)) image = image_set.get_image("Image1") self.assertTrue(isinstance(image, cpi.Image))
def make_workspace(self, image, labels, convert = True, mask = None): '''Make a workspace for testing MeasureTexture''' module = M.MeasureTexture() module.image_groups[0].image_name.value = INPUT_IMAGE_NAME module.object_groups[0].object_name.value = INPUT_OBJECTS_NAME pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image, convert=convert, mask = mask)) objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, INPUT_OBJECTS_NAME) return workspace, module