Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
labels - if thresholding per-object, the labels matrix needed
'''
module = cellprofiler.modules.identifyprimaryobjects.IdentifyPrimaryObjects()
module.module_num = 1
module.image_name.value = IMAGE_NAME
module.object_name.value = OBJECTS_NAME
pipeline = cellprofiler.pipeline.Pipeline()
pipeline.add_module(module)
m = cellprofiler.measurement.Measurements()
cpimage = cellprofiler.image.Image(image, mask=mask)
m.add(IMAGE_NAME, cpimage)
object_set = cellprofiler.object.ObjectSet()
if labels is not None:
o = cellprofiler.object.Objects()
o.segmented = labels
object_set.add_objects(o, MASKING_OBJECTS_NAME)
workspace = cellprofiler.workspace.Workspace(
pipeline, module, m, object_set, m, None)
return workspace, module
def test_05_02_relate_zero_parents_one_child(self):
x = cellprofiler.object.Objects()
x.segmented = numpy.zeros((10, 10), int)
y = cellprofiler.object.Objects()
labels = numpy.zeros((10, 10), int)
labels[3:6, 3:6] = 1
y.segmented = labels
children_per_parent, parents_of_children = x.relate_children(y)
self.assertEqual(numpy.product(children_per_parent.shape), 0)
self.assertEqual(numpy.product(parents_of_children.shape), 1)
self.assertEqual(parents_of_children[0], 0)
def make_workspace(labels, parent_image=None):
object_set = cellprofiler.object.ObjectSet()
objects = cellprofiler.object.Objects()
objects.segmented = labels
objects.parent_image = parent_image
object_set.add_objects(objects, OBJECTS_NAME)
pipeline = cellprofiler.pipeline.Pipeline()
module = cellprofiler.modules.measureimageareaoccupied.MeasureImageAreaOccupied()
module.set_module_num(1)
module.operands[0].operand_objects.value = OBJECTS_NAME
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 - 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 = (
cellprofiler.modules.measureobjectintensitydistribution.MeasureObjectIntensityDistribution()
)
module.wants_zernikes.value = wants_zernikes
module.zernike_degree.value = zernike_degree
module.images[0].image_name.value = IMAGE_NAME
module.objects[0].object_name.value = OBJECT_NAME
object_set = cellprofiler.object.ObjectSet()
main_objects = cellprofiler.object.Objects()
main_objects.segmented = labels
object_set.add_objects(main_objects, OBJECT_NAME)
if center_labels is None:
module.objects[
0
].center_choice.value = (
cellprofiler.modules.measureobjectintensitydistribution.C_SELF
)
else:
module.objects[0].center_choice.value = center_choice
module.objects[0].center_object_name.value = CENTER_NAME
center_objects = cellprofiler.object.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
def test_05_03_relate_one_parent_no_children(self):
x = cellprofiler.object.Objects()
labels = numpy.zeros((10, 10), int)
labels[3:6, 3:6] = 1
x.segmented = labels
y = cellprofiler.object.Objects()
y.segmented = numpy.zeros((10, 10), int)
children_per_parent, parents_of_children = x.relate_children(y)
self.assertEqual(numpy.product(children_per_parent.shape), 1)
self.assertEqual(children_per_parent[0], 0)
self.assertEqual(numpy.product(parents_of_children.shape), 0)
module.module_num = 1
module.input_image_name.value = IMAGE_NAME
module.object_name.value = OBJECTS_NAME
module.binary_image.value = BINARY_IMAGE_NAME
module.masking_objects.value = MASKING_OBJECTS_NAME
pipeline = cellprofiler.pipeline.Pipeline()
pipeline.add_module(module)
m = cpmeas.Measurements()
cpimage = cpi.Image(image, mask=mask)
m.add(IMAGE_NAME, cpimage)
if binary_image is not None:
m.add(BINARY_IMAGE_NAME, cpi.Image(binary_image))
object_set = cpo.ObjectSet()
if labels is not None:
o = cpo.Objects()
o.segmented = labels
object_set.add_objects(o, MASKING_OBJECTS_NAME)
workspace = cellprofiler.workspace.Workspace(
pipeline, module, m, object_set, m, None)
return workspace, module
module.object_groups[0].file_name.value = path
module.object_groups[0].wants_automatic_file_name.value = False
m = cellprofiler.measurement.Measurements()
numpy.random.seed(0)
mvalues = numpy.random.uniform(size=(4,))
image_set_list = cellprofiler.image.ImageSetList()
for index, measurement, metadata in zip(
list(range(4)), mvalues, ("foo", "bar", "bar", "foo")
):
image_set = image_set_list.get_image_set(index)
m.add_image_measurement("my_measurement", measurement)
m.add_image_measurement("Metadata_tag", metadata)
if index < 3:
m.next_image_set()
object_set = cellprofiler.object.ObjectSet()
object_set.add_objects(cellprofiler.object.Objects(), "my_objects")
workspace = cellprofiler.workspace.Workspace(
make_measurements_pipeline(m), module, image_set, object_set, m, image_set_list
)
for i in range(4):
module.post_run(workspace)
for file_name, value_indexes in (("foo.csv", (0, 3)), ("bar.csv", (1, 2))):
path = os.path.join(output_dir, file_name)
fd = open(path, "r")
try:
reader = csv.reader(fd, delimiter=module.delimiter_char)
header = next(reader)
assert len(header) == 3
d = {}
assert "ImageNumber" in header
assert "my_measurement" in header
assert "Metadata_tag" in header
def test_05_05_relate_two_parents_one_child(self):
x = cellprofiler.object.Objects()
labels = numpy.zeros((10, 10), int)
labels[3:6, 3:6] = 1
labels[3:6, 7:9] = 2
x.segmented = labels
y = cellprofiler.object.Objects()
labels = numpy.zeros((10, 10), int)
labels[3:6, 5:9] = 1
y.segmented = labels
children_per_parent, parents_of_children = x.relate_children(y)
self.assertEqual(numpy.product(children_per_parent.shape), 2)
self.assertEqual(children_per_parent[0], 0)
self.assertEqual(children_per_parent[1], 1)
self.assertEqual(numpy.product(parents_of_children.shape), 1)
self.assertEqual(parents_of_children[0], 2)
mask = images.get_image(mask_name)
mask_data = mask.pixel_data
y_data = skimage.morphology.watershed(
image=x_data,
markers=markers_data,
mask=mask_data,
connectivity=self.connectivity.value,
compactness=self.compactness.value,
watershed_line=self.watershed_line.value,
)
y_data = skimage.measure.label(y_data)
objects = cellprofiler.object.Objects()
objects.segmented = y_data
objects.parent_image = x
workspace.object_set.add_objects(objects, y_name)
self.add_measurements(workspace)
if self.show_window:
workspace.display_data.x_data = x.pixel_data
workspace.display_data.y_data = y_data
workspace.display_data.dimensions = dimensions
del graph
del paths
all_path_coords += paths_selected
ijv, all_lengths, all_angles, all_control_coords_x, all_control_coords_y = self.worm_descriptor_building(
all_path_coords, params, labels.shape
)
if self.show_window:
workspace.display_data.input_image = image.pixel_data
object_set = workspace.object_set
assert isinstance(object_set, cpo.ObjectSet)
measurements = workspace.measurements
assert isinstance(measurements, cpmeas.Measurements)
object_names = []
if self.overlap in (OO_WITH_OVERLAP, OO_BOTH):
o = cpo.Objects()
o.ijv = ijv
o.parent_image = image
name = self.overlap_objects.value
object_names.append(name)
object_set.add_objects(o, name)
I.add_object_count_measurements(measurements, name, o.count)
if self.show_window:
workspace.display_data.overlapping_labels = [
l for l, idx in o.get_labels()
]
if o.count == 0:
center_x = np.zeros(0)
center_y = np.zeros(0)
else:
center_x = np.bincount(ijv[:, 2], ijv[:, 1])[o.indices] / o.areas