def test_invert_binary_invert(): # # Regression for issue #1329 # r = numpy.random.RandomState() r.seed(1102) m = cellprofiler.measurement.Measurements() pixel_data = r.uniform(size=(20, 20)) > 0.5 m.add("inputimage", cellprofiler.image.Image(pixel_data)) module = cellprofiler.modules.imagemath.ImageMath() module.images[0].image_name.value = "inputimage" module.output_image_name.value = "intermediateimage" module.operation.value = cellprofiler.modules.imagemath.O_INVERT module.set_module_num(1) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) module = cellprofiler.modules.imagemath.ImageMath() module.images[0].image_name.value = "intermediateimage" module.output_image_name.value = "outputimage" module.operation.value = cellprofiler.modules.imagemath.O_INVERT module.set_module_num(2) pipeline = cellprofiler.pipeline.Pipeline() workspace = cellprofiler.workspace.Workspace(pipeline, module, m, None, m, None) for module in pipeline.modules(): module.run(workspace) numpy.testing.assert_array_equal( pixel_data, m.get_image("inputimage").pixel_data > 0.5)
def make_workspace(labels, operation, iterations=1, wants_outlines=False, wants_fill_holes=False): object_set = cellprofiler.object.ObjectSet() objects = cellprofiler.object.Objects() objects.segmented = labels object_set.add_objects(objects, INPUT_NAME) module = cellprofiler.modules.expandorshrinkobjects.ExpandOrShrink() module.object_name.value = INPUT_NAME module.output_object_name.value = OUTPUT_NAME module.operation.value = operation module.iterations.value = iterations module.wants_fill_holes.value = wants_fill_holes module.set_module_num(1) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set_list.get_image_set(0), object_set, cellprofiler.measurement.Measurements(), image_set_list, ) return workspace, module
def make_workspace(self, parents, children, fake_measurement=False): """Make a workspace for testing Relate""" pipeline = cellprofiler.pipeline.Pipeline() if fake_measurement: class FakeModule(cellprofiler.module.Module): def get_measurement_columns(self, pipeline): return [ ( CHILD_OBJECTS, MEASUREMENT, cellprofiler.measurement.COLTYPE_FLOAT, ), ( CHILD_OBJECTS, IGNORED_MEASUREMENT, cellprofiler.measurement.COLTYPE_INTEGER, ), ] module = FakeModule() module.set_module_num(1) pipeline.add_module(module) module = cellprofiler.modules.relateobjects.Relate() module.x_name.value = PARENT_OBJECTS module.y_name.value = CHILD_OBJECTS module.find_parent_child_distances.value = ( cellprofiler.modules.relateobjects.D_NONE) module.wants_child_objects_saved.value = False new_module_num = 2 if fake_measurement else 1 module.set_module_num(new_module_num) pipeline.add_module(module) object_set = cellprofiler.object.ObjectSet() image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) m = cellprofiler.measurement.Measurements() m.add_image_measurement(cellprofiler.measurement.GROUP_NUMBER, 1) m.add_image_measurement(cellprofiler.measurement.GROUP_INDEX, 1) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set, m, image_set_list) o = cellprofiler.object.Objects() if parents.shape[1] == 3: # IJV format o.ijv = parents else: o.segmented = parents object_set.add_objects(o, PARENT_OBJECTS) o = cellprofiler.object.Objects() if children.shape[1] == 3: o.ijv = children else: o.segmented = children object_set.add_objects(o, CHILD_OBJECTS) return workspace, module
def make_obj_workspace(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 = cellprofiler.modules.measureobjectoverlap.MeasureObjectOverlap() module.set_module_num(1) module.object_name_GT.value = GROUND_TRUTH_OBJ module.object_name_ID.value = ID_OBJ module.wants_emd.value = True pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.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 = cellprofiler.image.Image(d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")) image_set.add(name, image) object_set = cellprofiler.object.ObjectSet() for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ, id_obj)): object = cellprofiler.object.Objects() if d.shape[1] == 3: object.ijv = d else: object.segmented = d object_set.add_objects(object, name) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, object_set, cellprofiler.measurement.Measurements(), image_set_list, ) return workspace, module
def make_workspace(ground_truth, test, dimensions=2): """Make a workspace with a ground-truth image and a test image ground_truth and test are dictionaries with the following keys: image - the pixel data mask - (optional) the mask data crop_mask - (optional) a cropping mask returns a workspace and module """ module = cellprofiler.modules.measureimageoverlap.MeasureImageOverlap() module.set_module_num(1) module.ground_truth.value = GROUND_TRUTH_IMAGE_NAME module.test_img.value = TEST_IMAGE_NAME module.wants_emd.value = True pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) for name, d in ((GROUND_TRUTH_IMAGE_NAME, ground_truth), (TEST_IMAGE_NAME, test)): image = cellprofiler.image.Image( d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask"), dimensions=dimensions, ) image_set.add(name, image) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(), image_set_list, ) return workspace, module
def run_imagemath(images, modify_module_fn=None, measurement=None): """Run the ImageMath module, returning the image created images - a list of dictionaries. The dictionary has keys: pixel_data - image pixel data mask - mask for image cropping - cropping mask for image modify_module_fn - a function of the signature, fn(module) that allows the test to modify the module. measurement - an image measurement value """ image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) module = cellprofiler.modules.imagemath.ImageMath() module.set_module_num(1) for i, image in enumerate(images): pixel_data = image["pixel_data"] mask = image.get("mask", None) cropping = image.get("cropping", None) if i >= 2: module.add_image() name = "inputimage%s" % i module.images[i].image_name.value = name img = cellprofiler.image.Image(pixel_data, mask=mask, crop_mask=cropping) image_set.add(name, img) module.output_image_name.value = "outputimage" if modify_module_fn is not None: modify_module_fn(module) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) measurements = cellprofiler.measurement.Measurements() if measurement is not None: measurements.add_image_measurement(MEASUREMENT_NAME, str(measurement)) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), measurements, image_set_list, ) module.run(workspace) return image_set.get_image("outputimage")
def make_pipeline(csv_text, name=None): if name is None: handle, name = tempfile.mkstemp(".csv") fd = os.fdopen(handle, "w") else: fd = open(name, "w") fd.write(csv_text) fd.close() csv_path, csv_file = os.path.split(name) module = cellprofiler.modules.loaddata.LoadText() module.csv_directory.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME module.csv_directory.custom_path = csv_path module.csv_file_name.value = csv_file module.set_module_num(1) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) def error_callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(error_callback) return pipeline, module, name
def test_add_and_do_nothing(): # # Regression for issue #1333 - add one, do nothing, input image # is changed # r = numpy.random.RandomState() r.seed(1101) m = cellprofiler.measurement.Measurements() pixel_data = r.uniform(size=(20, 20)) m.add("inputimage", cellprofiler.image.Image(pixel_data)) module = cellprofiler.modules.imagemath.ImageMath() module.images[0].image_name.value = "inputimage" module.output_image_name.value = "outputimage" module.operation.value = cellprofiler.modules.imagemath.O_NONE module.addend.value = 0.5 module.set_module_num(1) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) workspace = cellprofiler.workspace.Workspace(pipeline, module, m, None, m, None) module.run(workspace) numpy.testing.assert_array_almost_equal( pixel_data, m.get_image("inputimage").pixel_data)
def test_module_must_be_last(): """Make sure that the pipeline is invalid if CreateBatchFiles is not last""" # # First, make sure that a naked CPModule tests valid # pipeline = cellprofiler.pipeline.Pipeline() module = cellprofiler.module.Module() module.set_module_num(len(pipeline.modules()) + 1) pipeline.add_module(module) pipeline.test_valid() # # Make sure that CreateBatchFiles on its own tests valid # pipeline = cellprofiler.pipeline.Pipeline() module = cellprofiler.modules.createbatchfiles.CreateBatchFiles() module.set_module_num(len(pipeline.modules()) + 1) pipeline.add_module(module) pipeline.test_valid() module = cellprofiler.module.Module() module.set_module_num(len(pipeline.modules()) + 1) pipeline.add_module(module) with pytest.raises(cellprofiler.setting.ValidationError): pipeline.test_valid()
def loader(module_num, mn=mn): module = cellprofiler.modules.instantiate_module(mn) module.set_module_num(module_num) return module