def get_step_parents(self, pipeline): """Return the possible step-parents associated with the parent""" step_parents = set() parent_name = self.x_name.value for module in pipeline.modules(): if module.module_num == self.module_num: return list(step_parents) # Objects that are the parent of the parents grandparents = module.get_measurements( pipeline, parent_name, cellprofiler.measurement.C_PARENT) step_parents.update(grandparents) # Objects that are the children of the parents siblings = module.get_measurements( pipeline, parent_name, cellprofiler.measurement.C_CHILDREN) for sibling in siblings: match = re.match("^([^_]+)_Count", sibling) if match is not None: sibling_name = match.groups()[0] if parent_name in module.get_measurements( pipeline, sibling_name, cellprofiler.measurement.C_PARENT): step_parents.add(sibling_name) return list(step_parents)
def test_get_measurements(): workspace, module = make_workspace(dict(image=numpy.zeros((20, 10), bool)), dict(image=numpy.zeros((20, 10), bool))) for wants_emd, features in ( ( True, list(cellprofiler.modules.measureimageoverlap.FTR_ALL) + [ cellprofiler.modules.measureimageoverlap. FTR_EARTH_MOVERS_DISTANCE ], ), (False, cellprofiler.modules.measureimageoverlap.FTR_ALL), ): module.wants_emd.value = wants_emd mnames = module.get_measurements( workspace.pipeline, cellprofiler.measurement.IMAGE, cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, ) assert len(mnames) == len(features) assert all(n in features for n in mnames) assert all(f in mnames for f in features) mnames = module.get_measurements( workspace.pipeline, "Foo", cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, ) assert len(mnames) == 0 mnames = module.get_measurements(workspace.pipeline, cellprofiler.measurement.IMAGE, "Foo") assert len(mnames) == 0
def test_03_03_metadata_row_and_column_and_well(self): csv_text = '''"Metadata_Row","Metadata_Column","Metadata_Well" "C","03","B14" ''' pipeline, module, filename = self.make_pipeline(csv_text) columns = module.get_measurement_columns(pipeline) self.assertTrue( any([ c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Row" and c[2] == "varchar(1)" for c in columns ])) self.assertTrue( any([ c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Column" and c[2] == "varchar(2)" for c in columns ])) self.assertTrue( any([ c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Well" and c[2] == "varchar(3)" for c in columns ])) m = pipeline.run() features = module.get_measurements(pipeline, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_METADATA) for feature, expected in (("Row", "C"), ("Column", "03"), ("Well", "B14")): self.assertTrue(feature in features) value = m.get_current_image_measurement('_'.join( (cellprofiler.measurement.C_METADATA, feature))) self.assertEqual(value, expected)
def validate_module(self, pipeline): '''Validate the module's settings Relate will complain if the children and parents are related by a prior module or if a step-parent is named twice''' for module in pipeline.modules(): if module == self: break parent_features = module.get_measurements(pipeline, self.y_name.value, "Parent") if self.x_name.value in parent_features: raise cellprofiler.setting.ValidationError( "{} and {} were related by the {} module".format( self.y_name.value, self.x_name.value, module.module_name ), self.x_name ) if self.has_step_parents and self.wants_step_parent_distances: step_parents = set() for group in self.step_parent_names: if group.step_parent_name.value in step_parents: raise cellprofiler.setting.ValidationError( u"{} has already been chosen".format( group.step_parent_name.value ), group.step_parent_name ) step_parents.add(group.step_parent_name.value)
def test_metadata_row_and_column_and_well(): csv_text = """"Metadata_Row","Metadata_Column","Metadata_Well" "C","03","B14" """ pipeline, module, filename = make_pipeline(csv_text) columns = module.get_measurement_columns(pipeline) assert any([ c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Row" and c[2] == "varchar(1)" for c in columns ]) assert any([ c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Column" and c[2] == "varchar(2)" for c in columns ]) assert any([ c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Well" and c[2] == "varchar(3)" for c in columns ]) m = pipeline.run() features = module.get_measurements(pipeline, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_METADATA) for feature, expected in (("Row", "C"), ("Column", "03"), ("Well", "B14")): assert feature in features value = m.get_current_image_measurement("_".join( (cellprofiler.measurement.C_METADATA, feature))) assert value == expected
def validate_module(self, pipeline): """Validate the module's settings Relate will complain if the children and parents are related by a prior module or if a step-parent is named twice""" for module in pipeline.modules(): if module == self: break parent_features = module.get_measurements(pipeline, self.y_name.value, "Parent") if self.x_name.value in parent_features: raise cellprofiler.setting.ValidationError( "{} and {} were related by the {} module".format( self.y_name.value, self.x_name.value, module.module_name), self.x_name, ) if self.has_step_parents and self.wants_step_parent_distances: step_parents = set() for group in self.step_parent_names: if group.step_parent_name.value in step_parents: raise cellprofiler.setting.ValidationError( "{} has already been chosen".format( group.step_parent_name.value), group.step_parent_name, ) step_parents.add(group.step_parent_name.value)
def test_04_03_get_measurements(self): workspace, module = self.make_workspace( dict(image=numpy.zeros((20, 10), bool)), dict(image=numpy.zeros((20, 10), bool))) for wants_emd, features in ( (True, list(cellprofiler.modules.measureimageoverlap.FTR_ALL) + [cellprofiler.modules.measureimageoverlap.FTR_EARTH_MOVERS_DISTANCE]), (False, cellprofiler.modules.measureimageoverlap.FTR_ALL)): module.wants_emd.value = wants_emd mnames = module.get_measurements(workspace.pipeline, cellprofiler.measurement.IMAGE, cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP) self.assertEqual(len(mnames), len(features)) self.assertTrue(all(n in features for n in mnames)) self.assertTrue(all(f in mnames for f in features)) mnames = module.get_measurements(workspace.pipeline, "Foo", cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP) self.assertEqual(len(mnames), 0) mnames = module.get_measurements(workspace.pipeline, cellprofiler.measurement.IMAGE, "Foo") self.assertEqual(len(mnames), 0)
def test_get_measurements_other_other(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "foo", "bar") expected = [] assert actual == expected
def test_get_measurements_other_other(self): module = cellprofiler.module.ImageSegmentation() module.x_name.value = "Image" actual = module.get_measurements(None, "foo", "bar") expected = [] assert actual == expected
def test_get_measurements_input_object_children(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "Objects", cellprofiler.measurement.C_CHILDREN) expected = [cellprofiler.measurement.FF_COUNT % "ObjectProcessing"] assert actual == expected
def test_get_measurements_image_count(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_COUNT) expected = ["ObjectProcessing"] assert actual == expected
def test_get_measurements_output_object_number(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "ObjectProcessing", cellprofiler.measurement.C_NUMBER) expected = [cellprofiler.measurement.FTR_OBJECT_NUMBER] assert actual == expected
def test_get_measurements_output_object_parent(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "ObjectProcessing", cellprofiler.measurement.C_PARENT) expected = ["Objects"] assert actual == expected
def test_get_measurements_output_object_number(self): module = cellprofiler.module.ImageSegmentation() module.x_name.value = "Image" actual = module.get_measurements(None, "ImageSegmentation", cellprofiler.measurement.C_NUMBER) expected = [cellprofiler.measurement.FTR_OBJECT_NUMBER] assert actual == expected
def test_get_measurements_image_count(self): module = cellprofiler.module.ImageSegmentation() module.x_name.value = "Image" actual = module.get_measurements(None, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_COUNT) expected = ["ImageSegmentation"] assert actual == expected
def test_get_measurements_image_count(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_COUNT) expected = [ "ObjectProcessing" ] assert actual == expected
def test_get_measurements_input_object_children(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "Objects", cellprofiler.measurement.C_CHILDREN) expected = [ cellprofiler.measurement.FF_COUNT % "ObjectProcessing" ] assert actual == expected
def test_get_measurements_output_object_parent(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "ObjectProcessing", cellprofiler.measurement.C_PARENT) expected = [ "Objects" ] assert actual == expected
def test_get_measurements_image_count(self): module = cellprofiler.module.ImageSegmentation() module.x_name.value = "Image" actual = module.get_measurements(None, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_COUNT) expected = [ "ImageSegmentation" ] assert actual == expected
def test_get_measurements_output_object_number(self): module = cellprofiler.module.ImageSegmentation() module.x_name.value = "Image" actual = module.get_measurements(None, "ImageSegmentation", cellprofiler.measurement.C_NUMBER) expected = [ cellprofiler.measurement.FTR_OBJECT_NUMBER ] assert actual == expected
def test_get_measurements_output_object_number(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "ObjectProcessing", cellprofiler.measurement.C_NUMBER) expected = [ cellprofiler.measurement.FTR_OBJECT_NUMBER ] assert actual == expected
def get_step_parents(self, pipeline): '''Return the possible step-parents associated with the parent''' step_parents = set() parent_name = self.x_name.value for module in pipeline.modules(): if module.module_num == self.module_num: return list(step_parents) # Objects that are the parent of the parents grandparents = module.get_measurements( pipeline, parent_name, cellprofiler.measurement.C_PARENT ) step_parents.update(grandparents) # Objects that are the children of the parents siblings = module.get_measurements( pipeline, parent_name, cellprofiler.measurement.C_CHILDREN ) for sibling in siblings: match = re.match("^([^_]+)_Count", sibling) if match is not None: sibling_name = match.groups()[0] if parent_name in module.get_measurements( pipeline, sibling_name, cellprofiler.measurement.C_PARENT ): step_parents.add(sibling_name) return list(step_parents)
def test_04_03_means_of_distances(self): # # Regression test of issue #1409 # # Make sure means of minimum and mean distances of children # are recorded properly # i, j = numpy.mgrid[0:14, 0:30] # # Make the objects different sizes to exercise more code # parent_labels = (i >= 7) * 1 + (j >= 15) * 2 + 1 child_labels = numpy.zeros(i.shape) numpy.random.seed(0) # Take 12 random points and label them child_centers = numpy.random.permutation(numpy.prod(i.shape))[:12] child_centers = numpy.vstack((i.flatten()[child_centers], j.flatten()[child_centers])) child_labels[child_centers[0], child_centers[1]] = numpy.arange(1, 13) parent_centers = numpy.array([[3, 7], [10, 7], [3, 22], [10, 22]], float) parent_indexes = parent_labels[child_centers[0], child_centers[1]] - 1 expected = numpy.sqrt(numpy.sum((parent_centers[parent_indexes, :] - child_centers.transpose()) ** 2, 1)) workspace, module = self.make_workspace(parent_labels, child_labels) self.assertTrue(isinstance(module, cellprofiler.modules.relateobjects.Relate)) module.find_parent_child_distances.value = cellprofiler.modules.relateobjects.D_CENTROID module.wants_per_parent_means.value = True mnames = module.get_measurements(workspace.pipeline, PARENT_OBJECTS, "_".join((cellprofiler.modules.relateobjects.C_MEAN, CHILD_OBJECTS))) self.assertTrue(cellprofiler.modules.relateobjects.FF_CENTROID % PARENT_OBJECTS in mnames) feat_mean = cellprofiler.modules.relateobjects.FF_MEAN % (CHILD_OBJECTS, cellprofiler.modules.relateobjects.FF_CENTROID % PARENT_OBJECTS) mcolumns = module.get_measurement_columns(workspace.pipeline) self.assertTrue(any([c[0] == PARENT_OBJECTS and c[1] == feat_mean for c in mcolumns])) m = workspace.measurements m[CHILD_OBJECTS, cellprofiler.measurement.M_LOCATION_CENTER_X, 1] = child_centers[1] m[CHILD_OBJECTS, cellprofiler.measurement.M_LOCATION_CENTER_Y, 1] = child_centers[0] module.run(workspace) v = m[PARENT_OBJECTS, feat_mean, 1] plabel = m[CHILD_OBJECTS, "_".join((cellprofiler.measurement.C_PARENT, PARENT_OBJECTS)), 1] self.assertEqual(len(v), 4) for idx in range(4): if numpy.any(plabel == idx + 1): self.assertAlmostEqual( v[idx], numpy.mean(expected[plabel == idx + 1]), 4)
def test_get_measurements_output_object_location(self): module = cellprofiler.module.ImageSegmentation() module.x_name.value = "Image" actual = module.get_measurements(None, "ImageSegmentation", cellprofiler.measurement.C_LOCATION) expected = [ cellprofiler.measurement.FTR_CENTER_X, cellprofiler.measurement.FTR_CENTER_Y, cellprofiler.measurement.FTR_CENTER_Z ] assert actual == expected
def test_get_measurements_output_object_location(self): module = cellprofiler.module.ObjectProcessing() module.x_name.value = "Objects" actual = module.get_measurements(None, "ObjectProcessing", cellprofiler.measurement.C_LOCATION) expected = [ cellprofiler.measurement.FTR_CENTER_X, cellprofiler.measurement.FTR_CENTER_Y, cellprofiler.measurement.FTR_CENTER_Z ] assert actual == expected
def test_03_03_metadata_row_and_column_and_well(self): csv_text = '''"Metadata_Row","Metadata_Column","Metadata_Well" "C","03","B14" ''' pipeline, module, filename = self.make_pipeline(csv_text) columns = module.get_measurement_columns(pipeline) self.assertTrue(any([c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Row" and c[2] == "varchar(1)" for c in columns])) self.assertTrue(any([c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Column" and c[2] == "varchar(2)" for c in columns])) self.assertTrue(any([c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Well" and c[2] == "varchar(3)" for c in columns])) m = pipeline.run() features = module.get_measurements(pipeline, cellprofiler.measurement.IMAGE, cellprofiler.measurement.C_METADATA) for feature, expected in (("Row", "C"), ("Column", "03"), ("Well", "B14")): self.assertTrue(feature in features) value = m.get_current_image_measurement('_'.join((cellprofiler.measurement.C_METADATA, feature))) self.assertEqual(value, expected)
def test_01_get_measurements(): module = (cellprofiler.modules.measureobjectintensitydistribution. MeasureObjectIntensityDistribution()) for i, image_name in ((0, "DNA"), (1, "Cytoplasm"), (2, "Actin")): if i: module.add_image() module.images[i].image_name.value = image_name for i, object_name, center_name in ( (0, "Nucleii", None), (1, "Cells", "Nucleii"), (2, "Cytoplasm", "Nucleii"), ): if i: module.add_object() module.objects[i].object_name.value = object_name if center_name is None: module.objects[i].center_choice.value = ( cellprofiler.modules.measureobjectintensitydistribution.C_SELF) else: module.objects[i].center_choice.value = ( cellprofiler.modules.measureobjectintensitydistribution. C_CENTERS_OF_OTHER) module.objects[i].center_object_name.value = center_name for i, bin_count in ((0, 4), (0, 5), (0, 6)): if i: module.add_bin_count() module.bin_counts[i].bin_count.value = bin_count for object_name in [x.object_name.value for x in module.objects]: assert tuple(module.get_categories(None, object_name)) == ( cellprofiler.modules.measureobjectintensitydistribution.M_CATEGORY, ) for feature in cellprofiler.modules.measureobjectintensitydistribution.F_ALL: assert feature in module.get_measurements( None, object_name, cellprofiler.modules.measureobjectintensitydistribution. M_CATEGORY, ) for image_name in [x.image_name.value for x in module.images]: for ( feature ) in cellprofiler.modules.measureobjectintensitydistribution.F_ALL: assert image_name in module.get_measurement_images( None, object_name, cellprofiler.modules.measureobjectintensitydistribution. M_CATEGORY, feature, ) for bin_count in [x.bin_count.value for x in module.bin_counts]: for bin in range(1, bin_count + 1): for ( feature ) in cellprofiler.modules.measureobjectintensitydistribution.F_ALL: assert "%dof%d" % ( bin, bin_count, ) in module.get_measurement_scales( None, object_name, cellprofiler.modules. measureobjectintensitydistribution.M_CATEGORY, feature, image_name, )
def test_02_get_zernike_measurements(): module = (cellprofiler.modules.measureobjectintensitydistribution. MeasureObjectIntensityDistribution()) for wants_zernikes, ftrs in ( ( cellprofiler.modules.measureobjectintensitydistribution. Z_MAGNITUDES, (cellprofiler.modules.measureobjectintensitydistribution. FF_ZERNIKE_MAGNITUDE, ), ), ( cellprofiler.modules.measureobjectintensitydistribution. Z_MAGNITUDES_AND_PHASE, ( cellprofiler.modules.measureobjectintensitydistribution. FF_ZERNIKE_MAGNITUDE, cellprofiler.modules.measureobjectintensitydistribution. FF_ZERNIKE_PHASE, ), ), ): module.wants_zernikes.value = wants_zernikes module.zernike_degree.value = 2 for i, image_name in ((0, "DNA"), (1, "Cytoplasm"), (2, "Actin")): if i: module.add_image() module.images[i].image_name.value = image_name for i, object_name, center_name in ( (0, "Nucleii", None), (1, "Cells", "Nucleii"), (2, "Cytoplasm", "Nucleii"), ): if i: module.add_object() module.objects[i].object_name.value = object_name if center_name is None: module.objects[i].center_choice.value = ( cellprofiler.modules.measureobjectintensitydistribution. C_SELF) else: module.objects[i].center_choice.value = ( cellprofiler.modules.measureobjectintensitydistribution. C_CENTERS_OF_OTHER) module.objects[i].center_object_name.value = center_name for object_name in "Nucleii", "Cells", "Cytoplasm": result = module.get_measurements( None, object_name, cellprofiler.modules.measureobjectintensitydistribution. M_CATEGORY, ) for ftr in ftrs: assert ftr in result iresult = module.get_measurement_images( None, object_name, cellprofiler.modules.measureobjectintensitydistribution. M_CATEGORY, ftr, ) for image in "DNA", "Cytoplasm", "Actin": assert image in iresult sresult = module.get_measurement_scales( None, object_name, cellprofiler.modules. measureobjectintensitydistribution.M_CATEGORY, ftr, image, ) for n, m in ((0, 0), (1, 1), (2, 0), (2, 2)): assert "%d_%d" % (n, m) in sresult