def make_workspace(self, scheme, images, adjustments): module = G.GrayToColor() module.scheme_choice.value = scheme image_names = [ "image%d" % i if images[i] is not None else G.LEAVE_THIS_BLACK for i in range(7) ] for image_name_setting, image_name, adjustment_setting, adjustment\ in zip((module.red_image_name, module.green_image_name, module.blue_image_name, module.cyan_image_name, module.magenta_image_name, module.yellow_image_name, module.gray_image_name), image_names, (module.red_adjustment_factor, module.green_adjustment_factor, module.blue_adjustment_factor, module.cyan_adjustment_factor, module.magenta_adjustment_factor, module.yellow_adjustment_factor, module.gray_adjustment_factor), adjustments): image_name_setting.value = image_name adjustment_setting.value = adjustment module.rgb_image_name.value = OUTPUT_IMAGE_NAME module.module_num = 1 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 image, image_name in zip(images, image_names): if image is not None: image_set.add(image_name, cpi.Image(image)) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) return workspace, module
def make_workspace(self, ground_truth, test): '''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 = C.CalculateImageOverlap() module.module_num = 1 module.obj_or_img.value = O_IMG module.ground_truth.value = GROUND_TRUTH_IMAGE_NAME module.test_img.value = TEST_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_IMAGE_NAME, ground_truth), (TEST_IMAGE_NAME, test)): image = cpi.Image(d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")) image_set.add(name, image) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) return workspace, module
def run_imagemath(self, 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 = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) module = I.ImageMath() module.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 = cpi.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 = cpp.Pipeline() pipeline.add_module(module) measurements = cpmeas.Measurements() if measurement is not None: measurements.add_image_measurement(MEASUREMENT_NAME, str(measurement)) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), measurements, image_set_list) module.run(workspace) return image_set.get_image('outputimage')
def run_module(self, image, mask=None, fn=None): '''Run the FlipAndRotate module image - pixel data to be transformed mask - optional mask on the pixel data fn - function with signature, "fn(module)" that will be called with the FlipAndRotate module returns an Image object containing the flipped/rotated/masked/cropped image and the angle measurement. ''' img = cpi.Image(image, mask) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, img) module = F.FlipAndRotate() module.image_name.value = IMAGE_NAME module.output_name.value = OUTPUT_IMAGE module.module_num = 1 if fn is not None: fn(module) pipeline = cpp.Pipeline() pipeline.add_module(module) def error_callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(error_callback) measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), measurements, image_set_list) module.run(workspace) feature = F.M_ROTATION_F % OUTPUT_IMAGE self.assertTrue( feature in measurements.get_feature_names(cpmeas.IMAGE)) angle = measurements.get_current_image_measurement(feature) output_image = image_set.get_image(OUTPUT_IMAGE) return (output_image, angle)
def make_workspace(self, image_measurements, object_measurements): '''Make a workspace with a FlagImage module and the given measurements image_measurements - a sequence of single image measurements. Use image_measurement_name(i) to get the name of the i th measurement object_measurements - a seequence of sequences of object measurements. These are stored under object, OBJECT_NAME with measurement name object_measurement_name(i) for the i th measurement. returns module, workspace ''' module = F.FlagImage() measurements = cpmeas.Measurements() for i in range(len(image_measurements)): measurements.add_image_measurement(image_measurement_name(i), image_measurements[i]) for i in range(len(object_measurements)): measurements.add_measurement(OBJECT_NAME, object_measurement_name(i), np.array(object_measurements[i])) flag = module.flags[0] self.assertTrue(isinstance(flag, cps.SettingsGroup)) flag.category.value = MEASUREMENT_CATEGORY flag.feature_name.value = MEASUREMENT_FEATURE module.module_num = 1 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) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), measurements, image_set_list) return module, workspace
def test_01_04_split_channels(self): np.random.seed(13) image = np.random.uniform(size=(20, 10, 5)) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cpi.Image(image)) module = cpm_ctg.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cpm_ctg.SPLIT module.rgb_or_channels.value = cpm_ctg.CH_CHANNELS module.add_channel() module.add_channel() channel_indexes = np.array([1, 4, 2]) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = module.channel_names[ channel_index] module.channels[i].image_name.value = OUTPUT_IMAGE_F % i pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpm.Measurements(), image_set_list) module.run(workspace) for i, channel_index in enumerate(channel_indexes): pixels = image_set.get_image( module.channels[i].image_name.value).pixel_data self.assertEqual(pixels.ndim, 2) self.assertEqual(tuple(pixels.shape), (20, 10)) np.testing.assert_almost_equal(image[:, :, channel_index], pixels)
def test_02_03_double_mask(self): labels = np.zeros((10, 15), int) labels[2:5, 3:8] = 1 labels[5:8, 10:14] = 2 object_set = cpo.ObjectSet() objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) np.random.seed(0) pixel_data = np.random.uniform(size=(10, 15)).astype(np.float32) mask = np.random.uniform(size=(10, 15)) > .5 image_set.add(IMAGE_NAME, cpi.Image(pixel_data, mask)) expected_mask = (mask & (labels > 0)) pipeline = cpp.Pipeline() module = M.MaskImage() module.source_choice.value = M.IO_OBJECTS module.object_name.value = OBJECTS_NAME module.image_name.value = IMAGE_NAME module.masked_image_name.value = MASKED_IMAGE_NAME module.invert_mask.value = False module.module_num = 1 workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) module.run(workspace) masked_image = workspace.image_set.get_image(MASKED_IMAGE_NAME) self.assertTrue(isinstance(masked_image, cpi.Image)) self.assertTrue(np.all(masked_image.pixel_data[expected_mask] == pixel_data[expected_mask])) self.assertTrue(np.all(masked_image.pixel_data[~ expected_mask] == 0)) self.assertTrue(np.all(masked_image.mask == expected_mask)) self.assertTrue(np.all(masked_image.masking_objects.segmented == labels))
def make_workspace(self, gridding, labels=None): module = I.IdentifyObjectsInGrid() module.module_num = 1 module.grid_name.value = GRID_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.guiding_object_name.value = GUIDING_OBJECTS_NAME module.outlines_name.value = OUTLINES_NAME image_set_list = cpi.ImageSetList() object_set = cpo.ObjectSet() if labels is not None: my_objects = cpo.Objects() my_objects.segmented = labels object_set.add_objects(my_objects, GUIDING_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_list.get_image_set(0), object_set, cpmeas.Measurements(), image_set_list) workspace.set_grid(GRID_NAME, gridding) return (workspace, module)
def make_tile_workspace(self, images): module = T.Tile() module.module_num = 1 module.tile_method.value = T.T_ACROSS_CYCLES module.input_image.value = INPUT_IMAGE_NAME module.output_image.value = OUTPUT_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() for i, image in enumerate(images): image_set = image_set_list.get_image_set(i) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image)) 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, pixel_data, mask=None, objects=None): image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) object_set = cpo.ObjectSet() image = cpi.Image(pixel_data) if not mask is None: image.mask = mask image_set.add(MY_IMAGE, image) if not objects is None: o = cpo.Objects() o.segmented = objects object_set.add_objects(o, MY_OBJECTS) module = miq.MeasureImageQuality() module.images_choice.value = miq.O_SELECT module.image_groups[0].include_image_scalings.value = False module.image_groups[0].image_names.value = MY_IMAGE module.image_groups[0].use_all_threshold_methods.value = False module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(),image_set_list) return workspace
def run_tteesstt(self, objects, level): module = instantiate_module(MODULE_NAME) module.objects_name.value = OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() object_set.add_objects(objects, OBJECTS_NAME) # # Make the workspace # measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, object_set, measurements, None) module.run(workspace) values = measurements.get_measurement(OBJECTS_NAME, "Example5_MeanDistance") self.assertEqual(len(values), objects.count) for labels, indices in objects.get_labels(): for l in np.unique(labels): # # Test very slowly = 1 object per pass # if l == 0: continue d = scipy.ndimage.distance_transform_edt(labels == l) value = np.sum(d) / np.sum(labels == l) if abs(value - values[l-1]) > .0001: if level == 1: self.fail("You got the wrong answer (label = %d, mine = %f, yours = %f" % l, value, values[l-1]) else: sys.stderr.write("Oh too bad, did not pass level %d\n" % level) return if level > 1: print "+%d for you!" % level
def make_workspace(self, pixel_data, mask=None): image = cpi.Image(pixel_data, mask) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, image) module = ID.IdentifyDeadWorms() module.module_num = 1 module.image_name.value = IMAGE_NAME module.object_name.value = OBJECTS_NAME 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, cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) return workspace, module
def test_01_02_run(self): module = instantiate_module(MODULE_NAME) module.input_objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() # # Pick a bunch of random points, dilate them using the distance # transform and then label the result. # r = np.random.RandomState() r.seed(12) bimg = np.ones((100, 100), bool) bimg[r.randint(0,100, 50), r.randint(0, 100, 50)] = False labels, count = label(distance_transform_edt(bimg) <= 5) # # Make the input objects # input_objects = cpo.Objects() input_objects.segmented = labels expected = skeletonize_labels(labels) object_set.add_objects(input_objects, INPUT_OBJECTS_NAME) # # Make the workspace # workspace = cpw.Workspace(pipeline, module, None, object_set, cpmeas.Measurements(), None) module.run(workspace) self.assertTrue(OUTPUT_OBJECTS_NAME in object_set.object_names, "Could not find the output objects in the object set") output_objects = object_set.get_objects(OUTPUT_OBJECTS_NAME) np.testing.assert_array_equal(expected, output_objects.segmented)
def make_workspace(self, scheme, images, adjustments=None, colors=None, weights=None): module = G.GrayToColor() module.scheme_choice.value = scheme if scheme not in (G.SCHEME_COMPOSITE, G.SCHEME_STACK): image_names = [ "image%d" % i if images[i] is not None else G.LEAVE_THIS_BLACK for i in range(7) ] for image_name_setting, image_name, adjustment_setting, adjustment\ in zip((module.red_image_name, module.green_image_name, module.blue_image_name, module.cyan_image_name, module.magenta_image_name, module.yellow_image_name, module.gray_image_name), image_names, (module.red_adjustment_factor, module.green_adjustment_factor, module.blue_adjustment_factor, module.cyan_adjustment_factor, module.magenta_adjustment_factor, module.yellow_adjustment_factor, module.gray_adjustment_factor), adjustments): image_name_setting.value = image_name adjustment_setting.value = adjustment else: while len(module.stack_channels) < len(images): module.add_stack_channel_cb() image_names = [] if weights is None: weights = [1.0] * len(images) if colors is None: colors = [ G.DEFAULT_COLORS[i % len(G.DEFAULT_COLORS)] for i in range(len(images)) ] for i, (image, color, weight) in enumerate(zip(images, colors, weights)): image_name = 'image%d' % (i + 1) image_names.append(image_name) module.stack_channels[i].image_name.value = image_name module.stack_channels[i].color.value = color module.stack_channels[i].weight.value = weight module.rgb_image_name.value = OUTPUT_IMAGE_NAME module.module_num = 1 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 image, image_name in zip(images, image_names): if image is not None: image_set.add(image_name, cpi.Image(image)) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpmeas.Measurements(), image_set_list) return workspace, module
def test_02_01_compare_to_matlab(self): expected = { 'EC50_DistCytoplasm_Correlation_Correlation_CorrGreenCorrBlue': 3.982812, 'EC50_DistCytoplasm_Intensity_LowerQuartileIntensity_CorrGreen': 4.139827, 'EC50_DistCytoplasm_Intensity_MedianIntensity_CorrGreen': 4.178600, 'EC50_DistCytoplasm_Intensity_MinIntensityEdge_CorrGreen': 4.059770, 'EC50_DistCytoplasm_Intensity_MinIntensity_CorrGreen': 4.066357, 'EC50_DistCytoplasm_Math_Ratio1': 4.491367, 'EC50_DistCytoplasm_Math_Ratio2': 3.848722, 'EC50_DistCytoplasm_Texture_AngularSecondMoment_CorrGreen_1': 4.948056, 'EC50_DistCytoplasm_Texture_Entropy_CorrGreen_1': 4.687104, 'EC50_DistCytoplasm_Texture_InfoMeas2_CorrGreen_1': 5.0285, 'EC50_DistCytoplasm_Texture_InverseDifferenceMoment_CorrGreen_1': 4.319017, 'EC50_DistCytoplasm_Texture_SumAverage_CorrGreen_1': 4.548876, 'EC50_DistCytoplasm_Texture_SumEntropy_CorrGreen_1': 4.779139, 'EC50_DistCytoplasm_Texture_Variance_CorrGreen_1': 4.218379, 'EC50_DistanceCells_Correlation_Correlation_CorrGreenCorrBlue': 3.708711, 'EC50_DistanceCells_Intensity_IntegratedIntensityEdge_CorrGreen': 4.135146, 'EC50_DistanceCells_Intensity_LowerQuartileIntensity_CorrGreen': 4.5372, 'EC50_DistanceCells_Intensity_MeanIntensityEdge_CorrGreen': 4.1371, 'EC50_DistanceCells_Intensity_MinIntensityEdge_CorrGreen': 4.033999, 'EC50_DistanceCells_Intensity_MinIntensity_CorrGreen': 4.079470, 'EC50_DistanceCells_Texture_AngularSecondMoment_CorrGreen_1': 5.118689, 'EC50_DistanceCells_Texture_Correlation_CorrGreen_1': 4.002074, 'EC50_DistanceCells_Texture_Entropy_CorrGreen_1': 5.008000, 'EC50_DistanceCells_Texture_InfoMeas1_CorrGreen_1': 3.883586, 'EC50_DistanceCells_Texture_InverseDifferenceMoment_CorrGreen_1': 3.977216, 'EC50_DistanceCells_Texture_SumAverage_CorrGreen_1': 4.9741, 'EC50_DistanceCells_Texture_SumEntropy_CorrGreen_1': 5.1455, 'EC50_DistanceCells_Texture_SumVariance_CorrGreen_1': 4.593041, 'EC50_DistanceCells_Texture_Variance_CorrGreen_1': 4.619517, 'EC50_Nuclei_Correlation_Correlation_CorrGreenCorrBlue': 3.751133, 'EC50_Nuclei_Math_Ratio1': 4.491367, 'EC50_Nuclei_Math_Ratio2': 3.848722, 'EC50_Nuclei_Texture_SumAverage_CorrGreen_1': 3.765297, 'EC50_PropCells_AreaShape_Area': 4.740853, 'EC50_PropCells_AreaShape_MajorAxisLength': 5.064460, 'EC50_PropCells_AreaShape_MinorAxisLength': 4.751471, 'EC50_PropCells_AreaShape_Perimeter': 4.949292, 'EC50_PropCells_Correlation_Correlation_CorrGreenCorrBlue': 3.772565, 'EC50_PropCells_Texture_GaborX_CorrGreen_1': 5.007167, 'EC50_PropCells_Texture_InfoMeas2_CorrBlue_1': 4.341353, 'EC50_PropCells_Texture_SumVariance_CorrBlue_1': 4.298359, 'EC50_PropCells_Texture_SumVariance_CorrGreen_1': 4.610826, 'EC50_PropCells_Texture_Variance_CorrBlue_1': 4.396352, 'EC50_PropCells_Texture_Variance_CorrGreen_1': 4.632468, 'EC50_PropCytoplasm_AreaShape_Area': 4.669679, 'EC50_PropCytoplasm_AreaShape_MinorAxisLength': 4.754476, 'EC50_PropCytoplasm_AreaShape_Perimeter': 4.949292, 'EC50_PropCytoplasm_Correlation_Correlation_CorrGreenCorrBlue': 4.072830, 'EC50_PropCytoplasm_Intensity_IntegratedIntensity_CorrGreen': 4.0934, 'EC50_PropCytoplasm_Intensity_LowerQuartileIntensity_CorrGreen': 3.925800, 'EC50_PropCytoplasm_Intensity_MedianIntensity_CorrGreen': 3.9252, 'EC50_PropCytoplasm_Texture_AngularSecondMoment_CorrGreen_1': 4.777481, 'EC50_PropCytoplasm_Texture_Entropy_CorrGreen_1': 4.4432, 'EC50_PropCytoplasm_Texture_GaborX_CorrGreen_1': 5.163371, 'EC50_PropCytoplasm_Texture_InfoMeas2_CorrGreen_1': 4.701046, 'EC50_PropCytoplasm_Texture_SumEntropy_CorrGreen_1': 4.510543, 'EC50_ThresholdedCells_Texture_AngularSecondMoment_CorrBlue_1': 4.560315, 'EC50_ThresholdedCells_Texture_AngularSecondMoment_CorrGreen_1': 4.966674, 'EC50_ThresholdedCells_Texture_Entropy_CorrBlue_1': 4.457866, 'EC50_ThresholdedCells_Texture_InfoMeas2_CorrBlue_1': 4.624049, 'EC50_ThresholdedCells_Texture_SumAverage_CorrBlue_1': 4.686706, 'EC50_ThresholdedCells_Texture_SumEntropy_CorrBlue_1': 4.537378, 'EC50_ThresholdedCells_Texture_SumVariance_CorrBlue_1': 4.322820, 'EC50_ThresholdedCells_Texture_SumVariance_CorrGreen_1': 4.742158, 'EC50_ThresholdedCells_Texture_Variance_CorrBlue_1': 4.265549, 'EC50_ThresholdedCells_Texture_Variance_CorrGreen_1': 4.860020, 'OneTailedZfactor_DistCytoplasm_Intensity_MedianIntensity_CorrGreen': -4.322503, 'OneTailedZfactor_DistCytoplasm_Intensity_MinIntensityEdge_CorrGreen': -4.322503, 'OneTailedZfactor_DistCytoplasm_Intensity_MinIntensity_CorrGreen': -4.322503, 'OneTailedZfactor_DistCytoplasm_Math_Ratio1': 0.622059, 'OneTailedZfactor_DistCytoplasm_Math_Ratio2': -4.508284, 'OneTailedZfactor_DistCytoplasm_Texture_Entropy_CorrGreen_1': -4.645887, 'OneTailedZfactor_DistCytoplasm_Texture_InfoMeas2_CorrGreen_1': -4.279118, 'OneTailedZfactor_DistCytoplasm_Texture_SumAverage_CorrGreen_1': -4.765570, 'OneTailedZfactor_DistCytoplasm_Texture_SumEntropy_CorrGreen_1': -4.682335, 'OneTailedZfactor_DistCytoplasm_Texture_Variance_CorrGreen_1': -4.415607, 'OneTailedZfactor_DistanceCells_Intensity_MeanIntensityEdge_CorrGreen': -4.200105, 'OneTailedZfactor_DistanceCells_Intensity_MinIntensityEdge_CorrGreen': -4.316452, 'OneTailedZfactor_DistanceCells_Intensity_MinIntensity_CorrGreen': -4.316452, 'OneTailedZfactor_DistanceCells_Texture_Correlation_CorrGreen_1': 0.202500, 'OneTailedZfactor_DistanceCells_Texture_Entropy_CorrGreen_1': -4.404815, 'OneTailedZfactor_DistanceCells_Texture_InfoMeas1_CorrGreen_1': -4.508513, 'OneTailedZfactor_DistanceCells_Texture_SumAverage_CorrGreen_1': -4.225356, 'OneTailedZfactor_DistanceCells_Texture_SumEntropy_CorrGreen_1': -4.382768, 'OneTailedZfactor_DistanceCells_Texture_SumVariance_CorrGreen_1': 0.492125, 'OneTailedZfactor_DistanceCells_Texture_Variance_CorrGreen_1': 0.477360, 'OneTailedZfactor_Nuclei_Correlation_Correlation_CorrGreenCorrBlue': 0.563780, 'OneTailedZfactor_Nuclei_Math_Ratio1': 0.622059, 'OneTailedZfactor_Nuclei_Math_Ratio2': -4.508284, 'OneTailedZfactor_Nuclei_Texture_SumAverage_CorrGreen_1': 0.426178, 'OneTailedZfactor_PropCells_AreaShape_Area': -4.216674, 'OneTailedZfactor_PropCells_AreaShape_MajorAxisLength': -4.119131, 'OneTailedZfactor_PropCells_AreaShape_MinorAxisLength': -4.109793, 'OneTailedZfactor_PropCells_AreaShape_Perimeter': -4.068050, 'OneTailedZfactor_PropCells_Correlation_Correlation_CorrGreenCorrBlue': 0.765440, 'OneTailedZfactor_PropCells_Texture_GaborX_CorrGreen_1': 0.114982, 'OneTailedZfactor_PropCells_Texture_InfoMeas2_CorrBlue_1': 0.108409, 'OneTailedZfactor_PropCells_Texture_SumVariance_CorrBlue_1': 0.191251, 'OneTailedZfactor_PropCells_Texture_SumVariance_CorrGreen_1': 0.559865, 'OneTailedZfactor_PropCells_Texture_Variance_CorrBlue_1': 0.254078, 'OneTailedZfactor_PropCells_Texture_Variance_CorrGreen_1': 0.556108, 'OneTailedZfactor_PropCytoplasm_AreaShape_Area': -4.223021, 'OneTailedZfactor_PropCytoplasm_AreaShape_MinorAxisLength': -4.095632, 'OneTailedZfactor_PropCytoplasm_AreaShape_Perimeter': -4.068050, 'OneTailedZfactor_PropCytoplasm_Intensity_MedianIntensity_CorrGreen': -4.194663, 'OneTailedZfactor_PropCytoplasm_Texture_Entropy_CorrGreen_1': -4.443338, 'OneTailedZfactor_PropCytoplasm_Texture_GaborX_CorrGreen_1': 0.207265, 'OneTailedZfactor_PropCytoplasm_Texture_InfoMeas2_CorrGreen_1': -4.297250, 'OneTailedZfactor_PropCytoplasm_Texture_SumEntropy_CorrGreen_1': -4.525324, 'OneTailedZfactor_ThresholdedCells_Texture_Entropy_CorrBlue_1': 0.167795, 'OneTailedZfactor_ThresholdedCells_Texture_InfoMeas2_CorrBlue_1': 0.067560, 'OneTailedZfactor_ThresholdedCells_Texture_SumAverage_CorrBlue_1': 0.478527, 'OneTailedZfactor_ThresholdedCells_Texture_SumEntropy_CorrBlue_1': 0.155119, 'OneTailedZfactor_ThresholdedCells_Texture_SumVariance_CorrBlue_1': 0.535907, 'OneTailedZfactor_ThresholdedCells_Texture_SumVariance_CorrGreen_1': 0.572801, 'OneTailedZfactor_ThresholdedCells_Texture_Variance_CorrBlue_1': 0.423454, 'OneTailedZfactor_ThresholdedCells_Texture_Variance_CorrGreen_1': 0.440500, 'Vfactor_DistCytoplasm_Correlation_Correlation_CorrGreenCorrBlue': 0.500429, 'Vfactor_DistCytoplasm_Intensity_LowerQuartileIntensity_CorrGreen': 0.325675, 'Vfactor_DistCytoplasm_Intensity_MedianIntensity_CorrGreen': 0.323524, 'Vfactor_DistCytoplasm_Intensity_MinIntensityEdge_CorrGreen': 0.138487, 'Vfactor_DistCytoplasm_Intensity_MinIntensity_CorrGreen': 0.128157, 'Vfactor_DistCytoplasm_Math_Ratio1': 0.503610, 'Vfactor_DistCytoplasm_Math_Ratio2': 0.319610, 'Vfactor_DistCytoplasm_Texture_AngularSecondMoment_CorrGreen_1': 0.522880, 'Vfactor_DistCytoplasm_Texture_Entropy_CorrGreen_1': 0.504303, 'Vfactor_DistCytoplasm_Texture_InfoMeas2_CorrGreen_1': 0.289432, 'Vfactor_DistCytoplasm_Texture_InverseDifferenceMoment_CorrGreen_1': 0.234123, 'Vfactor_DistCytoplasm_Texture_SumAverage_CorrGreen_1': 0.591687, 'Vfactor_DistCytoplasm_Texture_SumEntropy_CorrGreen_1': 0.520356, 'Vfactor_DistCytoplasm_Texture_Variance_CorrGreen_1': -0.007649, 'Vfactor_DistanceCells_Correlation_Correlation_CorrGreenCorrBlue': 0.761198, 'Vfactor_DistanceCells_Intensity_IntegratedIntensityEdge_CorrGreen': 0.234655, 'Vfactor_DistanceCells_Intensity_LowerQuartileIntensity_CorrGreen': 0.252240, 'Vfactor_DistanceCells_Intensity_MeanIntensityEdge_CorrGreen': 0.195125, 'Vfactor_DistanceCells_Intensity_MinIntensityEdge_CorrGreen': 0.138299, 'Vfactor_DistanceCells_Intensity_MinIntensity_CorrGreen': 0.126784, 'Vfactor_DistanceCells_Texture_AngularSecondMoment_CorrGreen_1': 0.342691, 'Vfactor_DistanceCells_Texture_Correlation_CorrGreen_1': 0.314396, 'Vfactor_DistanceCells_Texture_Entropy_CorrGreen_1': 0.311771, 'Vfactor_DistanceCells_Texture_InfoMeas1_CorrGreen_1': 0.410631, 'Vfactor_DistanceCells_Texture_InverseDifferenceMoment_CorrGreen_1': 0.170576, 'Vfactor_DistanceCells_Texture_SumAverage_CorrGreen_1': 0.223147, 'Vfactor_DistanceCells_Texture_SumEntropy_CorrGreen_1': 0.269519, 'Vfactor_DistanceCells_Texture_SumVariance_CorrGreen_1': 0.571528, 'Vfactor_DistanceCells_Texture_Variance_CorrGreen_1': 0.566272, 'Vfactor_Nuclei_Correlation_Correlation_CorrGreenCorrBlue': 0.705051, 'Vfactor_Nuclei_Math_Ratio1': 0.503610, 'Vfactor_Nuclei_Math_Ratio2': 0.319610, 'Vfactor_Nuclei_Texture_SumAverage_CorrGreen_1': 0.553708, 'Vfactor_PropCells_AreaShape_Area': 0.340093, 'Vfactor_PropCells_AreaShape_MajorAxisLength': 0.243838, 'Vfactor_PropCells_AreaShape_MinorAxisLength': 0.320691, 'Vfactor_PropCells_AreaShape_Perimeter': 0.238915, 'Vfactor_PropCells_Correlation_Correlation_CorrGreenCorrBlue': 0.723520, 'Vfactor_PropCells_Texture_GaborX_CorrGreen_1': 0.213161, 'Vfactor_PropCells_Texture_InfoMeas2_CorrBlue_1': 0.199791, 'Vfactor_PropCells_Texture_SumVariance_CorrBlue_1': 0.078959, 'Vfactor_PropCells_Texture_SumVariance_CorrGreen_1': 0.642844, 'Vfactor_PropCells_Texture_Variance_CorrBlue_1': 0.199105, 'Vfactor_PropCells_Texture_Variance_CorrGreen_1': 0.640818, 'Vfactor_PropCytoplasm_AreaShape_Area': 0.325845, 'Vfactor_PropCytoplasm_AreaShape_MinorAxisLength': 0.312258, 'Vfactor_PropCytoplasm_AreaShape_Perimeter': 0.238915, 'Vfactor_PropCytoplasm_Correlation_Correlation_CorrGreenCorrBlue': 0.337565, 'Vfactor_PropCytoplasm_Intensity_IntegratedIntensity_CorrGreen': 0.292900, 'Vfactor_PropCytoplasm_Intensity_LowerQuartileIntensity_CorrGreen': 0.175528, 'Vfactor_PropCytoplasm_Intensity_MedianIntensity_CorrGreen': 0.193308, 'Vfactor_PropCytoplasm_Texture_AngularSecondMoment_CorrGreen_1': 0.276152, 'Vfactor_PropCytoplasm_Texture_Entropy_CorrGreen_1': 0.239567, 'Vfactor_PropCytoplasm_Texture_GaborX_CorrGreen_1': 0.332380, 'Vfactor_PropCytoplasm_Texture_InfoMeas2_CorrGreen_1': 0.379141, 'Vfactor_PropCytoplasm_Texture_SumEntropy_CorrGreen_1': 0.337740, 'Vfactor_ThresholdedCells_Texture_AngularSecondMoment_CorrBlue_1': 0.334520, 'Vfactor_ThresholdedCells_Texture_AngularSecondMoment_CorrGreen_1': 0.192882, 'Vfactor_ThresholdedCells_Texture_Entropy_CorrBlue_1': 0.276245, 'Vfactor_ThresholdedCells_Texture_InfoMeas2_CorrBlue_1': 0.139166, 'Vfactor_ThresholdedCells_Texture_SumAverage_CorrBlue_1': 0.465237, 'Vfactor_ThresholdedCells_Texture_SumEntropy_CorrBlue_1': 0.355399, 'Vfactor_ThresholdedCells_Texture_SumVariance_CorrBlue_1': 0.453937, 'Vfactor_ThresholdedCells_Texture_SumVariance_CorrGreen_1': 0.564371, 'Vfactor_ThresholdedCells_Texture_Variance_CorrBlue_1': 0.360566, 'Vfactor_ThresholdedCells_Texture_Variance_CorrGreen_1': 0.548770, 'Zfactor_DistCytoplasm_Correlation_Correlation_CorrGreenCorrBlue': 0.531914, 'Zfactor_DistCytoplasm_Intensity_LowerQuartileIntensity_CorrGreen': 0.265558, 'Zfactor_DistCytoplasm_Intensity_MedianIntensity_CorrGreen': 0.178586, 'Zfactor_DistCytoplasm_Intensity_MinIntensityEdge_CorrGreen': 0.084566, 'Zfactor_DistCytoplasm_Intensity_MinIntensity_CorrGreen': 0.086476, 'Zfactor_DistCytoplasm_Math_Ratio1': 0.623284, 'Zfactor_DistCytoplasm_Math_Ratio2': 0.358916, 'Zfactor_DistCytoplasm_Texture_AngularSecondMoment_CorrGreen_1': 0.429510, 'Zfactor_DistCytoplasm_Texture_Entropy_CorrGreen_1': 0.508275, 'Zfactor_DistCytoplasm_Texture_InfoMeas2_CorrGreen_1': 0.068695, 'Zfactor_DistCytoplasm_Texture_InverseDifferenceMoment_CorrGreen_1': 0.347949, 'Zfactor_DistCytoplasm_Texture_SumAverage_CorrGreen_1': 0.646576, 'Zfactor_DistCytoplasm_Texture_SumEntropy_CorrGreen_1': 0.494276, 'Zfactor_DistCytoplasm_Texture_Variance_CorrGreen_1': 0.179011, 'Zfactor_DistanceCells_Correlation_Correlation_CorrGreenCorrBlue': 0.824686, 'Zfactor_DistanceCells_Intensity_IntegratedIntensityEdge_CorrGreen': 0.027644, 'Zfactor_DistanceCells_Intensity_LowerQuartileIntensity_CorrGreen': 0.088491, 'Zfactor_DistanceCells_Intensity_MeanIntensityEdge_CorrGreen': 0.065056, 'Zfactor_DistanceCells_Intensity_MinIntensityEdge_CorrGreen': 0.089658, 'Zfactor_DistanceCells_Intensity_MinIntensity_CorrGreen': 0.078017, 'Zfactor_DistanceCells_Texture_AngularSecondMoment_CorrGreen_1': 0.238131, 'Zfactor_DistanceCells_Texture_Correlation_CorrGreen_1': 0.301107, 'Zfactor_DistanceCells_Texture_Entropy_CorrGreen_1': 0.251143, 'Zfactor_DistanceCells_Texture_InfoMeas1_CorrGreen_1': 0.564957, 'Zfactor_DistanceCells_Texture_InverseDifferenceMoment_CorrGreen_1': 0.302767, 'Zfactor_DistanceCells_Texture_SumAverage_CorrGreen_1': 0.036459, 'Zfactor_DistanceCells_Texture_SumEntropy_CorrGreen_1': 0.159798, 'Zfactor_DistanceCells_Texture_SumVariance_CorrGreen_1': 0.516938, 'Zfactor_DistanceCells_Texture_Variance_CorrGreen_1': 0.501186, 'Zfactor_Nuclei_Correlation_Correlation_CorrGreenCorrBlue': 0.691408, 'Zfactor_Nuclei_Math_Ratio1': 0.623284, 'Zfactor_Nuclei_Math_Ratio2': 0.358916, 'Zfactor_Nuclei_Texture_SumAverage_CorrGreen_1': 0.587347, 'Zfactor_PropCells_AreaShape_Area': 0.132425, 'Zfactor_PropCells_AreaShape_MajorAxisLength': 0.034809, 'Zfactor_PropCells_AreaShape_MinorAxisLength': 0.113864, 'Zfactor_PropCells_AreaShape_Perimeter': 0.005984, 'Zfactor_PropCells_Correlation_Correlation_CorrGreenCorrBlue': 0.717632, 'Zfactor_PropCells_Texture_GaborX_CorrGreen_1': 0.251023, 'Zfactor_PropCells_Texture_InfoMeas2_CorrBlue_1': 0.149719, 'Zfactor_PropCells_Texture_SumVariance_CorrBlue_1': 0.102050, 'Zfactor_PropCells_Texture_SumVariance_CorrGreen_1': 0.611960, 'Zfactor_PropCells_Texture_Variance_CorrBlue_1': 0.197090, 'Zfactor_PropCells_Texture_Variance_CorrGreen_1': 0.614879, 'Zfactor_PropCytoplasm_AreaShape_Area': 0.205042, 'Zfactor_PropCytoplasm_AreaShape_MinorAxisLength': 0.072682, 'Zfactor_PropCytoplasm_AreaShape_Perimeter': 0.005984, 'Zfactor_PropCytoplasm_Correlation_Correlation_CorrGreenCorrBlue': 0.272017, 'Zfactor_PropCytoplasm_Intensity_IntegratedIntensity_CorrGreen': 0.115327, 'Zfactor_PropCytoplasm_Intensity_LowerQuartileIntensity_CorrGreen': 0.141850, 'Zfactor_PropCytoplasm_Intensity_MedianIntensity_CorrGreen': 0.105803, 'Zfactor_PropCytoplasm_Texture_AngularSecondMoment_CorrGreen_1': 0.107640, 'Zfactor_PropCytoplasm_Texture_Entropy_CorrGreen_1': 0.067896, 'Zfactor_PropCytoplasm_Texture_GaborX_CorrGreen_1': 0.136688, 'Zfactor_PropCytoplasm_Texture_InfoMeas2_CorrGreen_1': 0.334749, 'Zfactor_PropCytoplasm_Texture_SumEntropy_CorrGreen_1': 0.208829, 'Zfactor_ThresholdedCells_Texture_AngularSecondMoment_CorrBlue_1': 0.263467, 'Zfactor_ThresholdedCells_Texture_AngularSecondMoment_CorrGreen_1': 0.124355, 'Zfactor_ThresholdedCells_Texture_Entropy_CorrBlue_1': 0.236433, 'Zfactor_ThresholdedCells_Texture_InfoMeas2_CorrBlue_1': 0.125845, 'Zfactor_ThresholdedCells_Texture_SumAverage_CorrBlue_1': 0.449333, 'Zfactor_ThresholdedCells_Texture_SumEntropy_CorrBlue_1': 0.323243, 'Zfactor_ThresholdedCells_Texture_SumVariance_CorrBlue_1': 0.507477, 'Zfactor_ThresholdedCells_Texture_SumVariance_CorrGreen_1': 0.599000, 'Zfactor_ThresholdedCells_Texture_Variance_CorrBlue_1': 0.361424, 'Zfactor_ThresholdedCells_Texture_Variance_CorrGreen_1': 0.481393 } temp_dir = tempfile.mkdtemp() try: cpprefs.set_headless() cpprefs.set_default_output_directory(temp_dir) print "Writing output to %s" % temp_dir path = os.path.split(__file__)[0] measurements = loadmat(os.path.join(path, 'calculatestatistics.mat'), struct_as_record=True) measurements = measurements['m'] image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) m = cpmeas.Measurements() doses = [ 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] for i, dose in enumerate(doses): m.add_image_measurement("Dose", dose) for object_name in measurements.dtype.fields: omeasurements = measurements[object_name][0, 0] for feature_name in omeasurements.dtype.fields: data = omeasurements[feature_name][0, 0][0, i] m.add_measurement(object_name, feature_name, data) if i < len(doses) - 1: m.next_image_set() pipeline = cpp.Pipeline() module = C.CalculateStatistics() module.grouping_values.value = "Dose" module.dose_values[0].log_transform.value = False module.dose_values[0].measurement.value = "Dose" module.dose_values[0].wants_save_figure.value = True module.dose_values[0].figure_name.value = "EC49_" module.module_num = 1 pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), m, image_set_list) module.post_run(workspace) for feature_name in m.get_feature_names(cpmeas.EXPERIMENT): if not expected.has_key(feature_name): print "Missing measurement: %s" % feature_name continue value = m.get_experiment_measurement(feature_name) e_value = expected[feature_name] diff = abs(value - e_value) * 2 / abs(value + e_value) self.assertTrue( diff < .05, "%s: Matlab: %f, Python: %f diff: %f" % (feature_name, e_value, value, diff)) if diff > .01: print( "Warning: > 1%% difference for %s: Matlab: %f, Python: %f diff: %f" % (feature_name, e_value, value, diff)) if feature_name.startswith("EC50"): filename = "EC49_" + feature_name[5:] + ".pdf" self.assertTrue( os.path.isfile(os.path.join(temp_dir, filename))) finally: try: workspace.measurements.hdf5_dict.hdf5_file.close() except: pass for filename in os.listdir(temp_dir): path = os.path.join(temp_dir, filename) os.remove(path) os.rmdir(temp_dir)
return image_numbers = np.arange(1, n_image_sets + 1) for image_number in image_numbers: m[cpmeas.IMAGE, cpmeas.GROUP_NUMBER, image_number] = 1 m[cpmeas.IMAGE, cpmeas.GROUP_INDEX, image_number] = image_number input_modules, other_modules = self.split_pipeline(pipeline) workspace = cpw.Workspace(pipeline, None, m, None, m, None) logger.info("Preparing group") for module in other_modules: module.prepare_group( workspace, dict([("image_number", i) for i in image_numbers]), image_numbers) for image_index in range(n_image_sets): object_set = cpo.ObjectSet() m.next_image_set(image_index + 1) for channel_name in channel_names: dataset = image_group[channel_name] pixel_data = dataset[image_index] m.add(channel_name, cpi.Image(pixel_data)) for module in other_modules: workspace = cpw.Workspace(pipeline, module, m, object_set, m, None) try: logger.info("Running module # %d: %s" % (module.module_num, module.module_name)) pipeline.run_module(module, workspace) if workspace.disposition in \ (cpw.DISPOSITION_SKIP, cpw.DISPOSITION_CANCEL):
def run_create_webpage(self, image_paths, thumb_paths=None, metadata=None, alter_fn=None): '''Run the create_webpage module, returning the resulting HTML document image_paths - list of path / filename tuples. The function will write an image to each of these and put images and measurements into the workspace for each. thumb_paths - if present a list of path / filename tuples. Same as above metadata - a dictionary of feature / string values alter_fn - function taking a CreateWebPage module, for you to alter the module's settings ''' np.random.seed(0) module = C.CreateWebPage() module.module_num = 1 module.orig_image_name.value = IMAGE_NAME module.web_page_file_name.value = DEFAULT_HTML_FILE if alter_fn is not None: alter_fn(module) pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) images = [(IMAGE_NAME, image_paths)] if thumb_paths: images += [(THUMB_NAME, thumb_paths)] self.assertEqual(len(image_paths), len(thumb_paths)) module.wants_thumbnails.value = True module.thumbnail_image_name.value = THUMB_NAME else: module.wants_thumbnails.value = False measurements = cpmeas.Measurements() image_set_list = cpi.ImageSetList() workspace = cpw.Workspace(pipeline, module, None, None, measurements, image_set_list, None) self.assertTrue(module.prepare_run(workspace)) module.prepare_group(workspace, {}, np.arange(1, len(image_paths) + 1)) for i in range(len(image_paths)): image_set = image_set_list.get_image_set(i) if metadata is not None: for key in metadata.keys(): values = metadata[key] feature = cpmeas.C_METADATA + "_" + key measurements.add_image_measurement(feature, values[i]) for image_name, paths in images: pixel_data = np.random.uniform(size=(10, 13)) img = cpi.Image(pixel_data) image_set.add(image_name, img) path_name, file_name = paths[i] if path_name is None: path_name = cpprefs.get_default_image_directory() full_path = os.path.abspath( os.path.join(self.directory, path_name, file_name)) imsave(full_path, pixel_data) path_feature = '_'.join((C_PATH_NAME, image_name)) file_feature = '_'.join((C_FILE_NAME, image_name)) measurements.add_image_measurement(path_feature, os.path.split(full_path)[0]) measurements.add_image_measurement(file_feature, file_name) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), measurements, image_set_list) module.run(workspace) if i < len(image_paths) - 1: measurements.next_image_set() module.post_group(workspace, {}) module.post_run(workspace)
def test_12_01_load_unicode(self): base_directory = tempfile.mkdtemp() directory = u"\u2211\u03B1" filename = u"\u03B2.jpg" base_path = os.path.join(base_directory, directory) os.mkdir(base_path) path = os.path.join(base_path, filename) csv_filename = u"\u03b3.csv" csv_path = os.path.join(base_path, csv_filename) unicode_value = u"\u03b4.csv" try: r = np.random.RandomState() r.seed(1101) labels = r.randint(0, 10, size=(30, 20)).astype(np.uint8) img = PIL.Image.fromarray(labels, "L") img.save(path, "PNG") csv_text = ( "Image_FileName_MyFile,Image_PathName_MyFile,Metadata_Unicode\n" "%s,%s,%s\n" % (filename.encode('utf8'), base_path.encode('utf8'), unicode_value.encode('utf8'))) pipeline, module, _ = self.make_pipeline(csv_text, csv_path) image_set_list = cpi.ImageSetList() m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, None, m, image_set_list) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(len(m.get_image_numbers()), 1) key_names, group_list = pipeline.get_groupings(workspace) self.assertEqual(len(group_list), 1) group_keys, image_numbers = group_list[0] self.assertEqual(len(image_numbers), 1) module.prepare_group(workspace, group_keys, image_numbers) image_set = image_set_list.get_image_set(image_numbers[0] - 1) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), m, image_set_list) module.run(workspace) pixel_data = image_set.get_image("MyFile").pixel_data self.assertEqual(pixel_data.shape[0], 30) self.assertEqual(pixel_data.shape[1], 20) value = m.get_current_image_measurement("Metadata_Unicode") self.assertEqual(value, unicode_value) finally: if os.path.exists(path): try: os.unlink(path) except: pass if os.path.exists(csv_path): try: os.unlink(csv_path) except: pass if os.path.exists(base_path): try: os.rmdir(base_path) except: pass if os.path.exists(base_directory): try: os.rmdir(base_directory) except: pass
def make_workspace(self, control_points, lengths, radii, image, mask=None, auximage=None): '''Create a workspace containing the control point measurements control_points - an n x 2 x m array where n is the # of control points, and m is the number of objects. lengths - the length of each object radii - the radii_from_training defining the radius at each control pt image - the image to be straightened mask - the mask associated with the image (default = no mask) auximage - a second image to be straightnened (default = no second image) ''' module = S.StraightenWorms() module.objects_name.value = OBJECTS_NAME module.straightened_objects_name.value = STRAIGHTENED_OBJECTS_NAME module.images[0].image_name.value = IMAGE_NAME module.images[ 0].straightened_image_name.value = STRAIGHTENED_IMAGE_NAME module.flip_image.value = IMAGE_NAME module.module_num = 1 # Trick the module into thinking it's read the data file class P: def __init__(self): self.radii_from_training = radii module.training_set_directory.dir_choice = cps.URL_FOLDER_NAME module.training_set_directory.custom_path = "http://www.cellprofiler.org" module.training_set_file_name.value = "TrainingSet.xml" module.training_params = {"TrainingSet.xml": (P(), "URL")} pipeline = cpp.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) m = cpmeas.Measurements() for i, (y, x) in enumerate(control_points): for v, f in ((x, S.F_CONTROL_POINT_X), (y, S.F_CONTROL_POINT_Y)): feature = "_".join((S.C_WORM, f, str(i + 1))) m.add_measurement(OBJECTS_NAME, feature, v) feature = "_".join((S.C_WORM, S.F_LENGTH)) m.add_measurement(OBJECTS_NAME, feature, lengths) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cpi.Image(image, mask)) if auximage is not None: image_set.add(AUX_IMAGE_NAME, cpi.Image(auximage)) module.add_image() module.images[1].image_name.value = AUX_IMAGE_NAME module.images[ 1].straightened_image_name.value = AUX_STRAIGHTENED_IMAGE_NAME object_set = cpo.ObjectSet() objects = cpo.Objects() labels = np.zeros(image.shape, int) for i in range(control_points.shape[2]): if lengths[i] == 0: continue self.rebuild_worm_from_control_points_approx( control_points[:, :, i], radii, labels, i + 1) objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) workspace = cpw.Workspace(pipeline, module, image_set, object_set, m, image_set_list) return workspace, module
def test_01_02_run(self): """Run with a rectangle, cross and circle""" object_set = cpo.ObjectSet() labels = np.zeros((10,20),int) labels[1:9,1:5] = 1 labels[1:9,11] = 2 labels[4,6:19] = 2 objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") labels = np.zeros((115,115),int) x,y = np.mgrid[-50:51,-50:51] labels[:101, :101][x**2+y**2<=2500] = 1 objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, "OtherObjects") module = cpmoas.MeasureObjectAreaShape() settings = ["SomeObjects","OtherObjects","Yes"] module.set_settings_from_values(settings, 1, module.module_class()) module.module_num = 1 image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() pipeline = cpp.Pipeline() pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) self.features_and_columns_match(measurements, module) a = measurements.get_current_measurement('SomeObjects','AreaShape_Area') self.assertEqual(len(a),2) self.assertEqual(a[0],32) self.assertEqual(a[1],20) # # Mini-test of the form factor of a circle # ff = measurements.get_current_measurement('OtherObjects', 'AreaShape_FormFactor') self.assertEqual(len(ff),1) perim = measurements.get_current_measurement('OtherObjects', 'AreaShape_Perimeter') area = measurements.get_current_measurement('OtherObjects', 'AreaShape_Area') # The perimeter is obtained geometrically and is overestimated. expected = 100 * np.pi diff = abs((perim[0] - expected)/(perim[0] + expected)) self.assertTrue(diff < .05, "perimeter off by %f" % diff) wrongness = (perim[0] / expected)**2 # It's an approximate circle... expected = np.pi * 50.0 **2 diff = abs((area[0] - expected) / (area[0] + expected)) self.assertTrue(diff < .05, "area off by %f" %diff) wrongness *= expected / area[0] self.assertAlmostEqual(ff[0] * wrongness, 1.0) for object_name, object_count in (('SomeObjects',2), ('OtherObjects',1)): for measurement in module.get_measurements(pipeline,object_name, 'AreaShape'): feature_name = 'AreaShape_%s'%(measurement) m = measurements.get_current_measurement(object_name, feature_name) self.assertEqual(len(m),object_count)
def run_module(self, image, labels, center_labels=None, center_choice=M.C_CENTERS_OF_OTHER, bin_count=4, maximum_radius=100, wants_scaled=True, wants_workspace=False): '''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 = center_choice 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 module.add_heatmap() module.add_heatmap() module.add_heatmap() for i, (a, f) in enumerate( ((M.A_FRAC_AT_D, M.F_FRAC_AT_D), (M.A_MEAN_FRAC, M.F_MEAN_FRAC), (M.A_RADIAL_CV, M.F_RADIAL_CV))): module.heatmaps[i].image_name.value = IMAGE_NAME module.heatmaps[i].object_name.value = OBJECT_NAME module.heatmaps[i].bin_count.value = str(bin_count) module.heatmaps[i].wants_to_save_display.value = True display_name = HEAT_MAP_NAME + f module.heatmaps[i].display_name.value = display_name module.heatmaps[i].colormap.value = "gray" module.heatmaps[i].measurement.value = a pipeline = cpp.Pipeline() measurements = cpmeas.Measurements() image_set_list = cpi.ImageSetList() image_set = measurements 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) if wants_workspace: return measurements, workspace return measurements
def rruunn(self, input_labels, relabel_option, unify_option=R.UNIFY_DISTANCE, unify_method=R.UM_DISCONNECTED, distance_threshold=5, minimum_intensity_fraction=.9, where_algorithm=R.CA_CLOSEST_POINT, image=None, wants_outlines=False, outline_name="None", parent_object="Parent_object", parent_labels=np.zeros((10, 20), int)): '''Run the RelabelObjects module returns the labels matrix and the workspace. ''' module = R.RelabelObjects() module.module_num = 1 module.objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.relabel_option.value = relabel_option module.unify_option.value = unify_option module.unification_method.value = unify_method module.parent_object.value = parent_object module.distance_threshold.value = distance_threshold module.minimum_intensity_fraction.value = minimum_intensity_fraction module.wants_image.value = (image is not None) module.where_algorithm.value = where_algorithm module.wants_outlines.value = wants_outlines module.outlines_name.value = outline_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) if image is not None: img = cpi.Image(image) image_set.add(IMAGE_NAME, img) module.image_name.value = IMAGE_NAME object_set = cpo.ObjectSet() o = cpo.Objects() o.segmented = input_labels object_set.add_objects(o, INPUT_OBJECTS_NAME) parent_object_set = cpo.ObjectSet() p = cpo.Objects() p.segmented = parent_labels object_set.add_objects(p, parent_object) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) module.run(workspace) output_objects = workspace.object_set.get_objects(OUTPUT_OBJECTS_NAME) return output_objects.segmented, workspace
def test_01_02_run(self): module = instantiate_module(MODULE_NAME) module.input_objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() # # Pick a bunch of random points, dilate them using the distance # transform and then label the result. # r = np.random.RandomState() r.seed(12) bimg = np.ones((100, 100), bool) bimg[r.randint(0, 100, 50), r.randint(0, 100, 50)] = False labels, count = label(distance_transform_edt(bimg) <= 5) # # Make the input objects # input_objects = cpo.Objects() input_objects.segmented = labels expected = skeletonize_labels(labels) object_set.add_objects(input_objects, INPUT_OBJECTS_NAME) # # Make the workspace # measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, object_set, measurements, None) module.run(workspace) # # Calculate the centers using Numpy. Scipy can do this too. # But maybe it's instructive to show you how to go at the labels # matrix using Numpy. # # We're going to get the centroids by taking the average value # of x and y per object. # y, x = np.mgrid[0:labels.shape[0], 0:labels.shape[1]].astype(float) # # np.bincount counts the number of occurrences of each integer value. # You need to operate on a 1d array - if you flatten the labels # and weights, their pixels still align. # # We do [1:] to discard the background which is labeled 0 # # The optional second argument to np.bincount is the "weight". For # each label value, maintain a running sum of the weights. # areas = np.bincount(expected.flatten())[1:] total_x = np.bincount(expected.flatten(), weights=x.flatten())[1:] total_y = np.bincount(expected.flatten(), weights=y.flatten())[1:] expected_location_x = total_x / areas expected_location_y = total_y / areas # # Now check against the measurements. # count_feature = I.C_COUNT + "_" + OUTPUT_OBJECTS_NAME self.assertTrue( measurements.has_feature(cpmeas.IMAGE, count_feature), "Your module did not produce a %s measurement" % count_feature) count = measurements.get_measurement(cpmeas.IMAGE, count_feature) self.assertEqual(count, len(areas)) for ftr, expected in ((I.M_LOCATION_CENTER_X, expected_location_x), (I.M_LOCATION_CENTER_Y, expected_location_y)): self.assertTrue(measurements.has_feature(OUTPUT_OBJECTS_NAME, ftr)) location = measurements.get_measurement(OUTPUT_OBJECTS_NAME, ftr) np.testing.assert_almost_equal(location, expected)