Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_workspace(gridding, labels=None):
module = cellprofiler.modules.identifyobjectsingrid.IdentifyObjectsInGrid()
module.set_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
image_set_list = cellprofiler.image.ImageSetList()
object_set = cellprofiler.object.ObjectSet()
if labels is not None:
my_objects = cellprofiler.object.Objects()
my_objects.segmented = labels
object_set.add_objects(my_objects, GUIDING_OBJECTS_NAME)
pipeline = cellprofiler.pipeline.Pipeline()
def callback(caller, event):
assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)
pipeline.add_listener(callback)
pipeline.add_module(module)
workspace = cellprofiler.workspace.Workspace(
pipeline,
module,
image_set_list.get_image_set(0),
def make_place_workspace(images):
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
module = cellprofiler.modules.tile.Tile()
module.set_module_num(1)
module.tile_method.value = cellprofiler.modules.tile.T_WITHIN_CYCLES
module.output_image.value = OUTPUT_IMAGE_NAME
module.wants_automatic_rows.value = False
module.wants_automatic_columns.value = True
module.rows.value = 1
for i, image in enumerate(images):
image_name = input_image_name(i)
if i == 0:
module.input_image.value = image_name
else:
if len(module.additional_images) <= i:
module.add_image()
module.additional_images[i - 1].input_image_name.value = image_name
for feature in list(odict.keys()):
m.add_all_measurements(object_name, feature, odict[feature])
if nimages is None:
nimages = len(odict[feature])
else:
assert nimages == len(odict[feature])
if (
object_name == cellprofiler.measurement.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 = cellprofiler.image.ImageSetList()
for i in range(nimages):
image_set = image_set_list.get_image_set(i)
workspace = cellprofiler.workspace.Workspace(
pipeline, module, image_set, cellprofiler.object.ObjectSet(), m, image_set_list
)
return workspace, module
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:
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)
assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)
pipeline.add_listener(callback)
pipeline.add_module(module)
object_set = cellprofiler.object.ObjectSet()
io = cellprofiler.object.Objects()
io.segmented = labels
object_set.add_objects(io, INPUT_OBJECTS)
if masking_objects is not None:
oo = cellprofiler.object.Objects()
oo.segmented = masking_objects
object_set.add_objects(oo, MASKING_OBJECTS)
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
if masking_image is not None:
mi = cellprofiler.image.Image(masking_image)
image_set.add(MASKING_IMAGE, mi)
workspace = cellprofiler.workspace.Workspace(
pipeline,
module,
image_set,
object_set,
cellprofiler.measurement.Measurements(),
image_set_list,
)
return workspace, module
""".format(
**{
"cp_logo_url": tests.modules.cp_logo_url,
"cp_logo_url_filename": tests.modules.cp_logo_url_filename,
}
)
pipeline, module, filename = make_pipeline(csv_text)
assert isinstance(module, cellprofiler.modules.loaddata.LoadData)
m = cellprofiler.measurement.Measurements()
workspace = cellprofiler.workspace.Workspace(
pipeline,
module,
m,
cellprofiler.object.ObjectSet(),
m,
cellprofiler.image.ImageSetList(),
)
assert module.prepare_run(workspace)
assert (
m.get_measurement(cellprofiler.measurement.IMAGE, "FileName_DNA", 1)
== tests.modules.cp_logo_url_filename
)
path = m.get_measurement(cellprofiler.measurement.IMAGE, "PathName_DNA", 1)
assert path == tests.modules.cp_logo_url_folder
assert (
m.get_measurement(cellprofiler.measurement.IMAGE, "URL_DNA", 1)
== tests.modules.cp_logo_url
)
assert (
m[cellprofiler.measurement.IMAGE, "FileName_DNA", 2]
== tests.modules.cp_logo_url_filename
)
m.add_measurement(OBJECTS_NAME, MEASUREMENT_NAME, numpy.array(measurement))
y, x = centrosome.cpmorphology.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 = numpy.zeros(labels.shape)
module.measurement.value = MEASUREMENT_NAME
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)
image_set.add(INPUT_IMAGE_NAME, cellprofiler.image.Image(image))
workspace = cellprofiler.workspace.Workspace(
pipeline, module, image_set, object_set, m, image_set_list
)
return workspace, module
def test_add_measurements(self):
measurements = cellprofiler.measurement.Measurements()
module = cellprofiler.module.ImageSegmentation()
module.x_name.value = "Image"
image = cellprofiler.image.Image(image=numpy.zeros((30, 30)))
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
image_set.add("Image", image)
object_set = cellprofiler.object.ObjectSet()
labels = numpy.zeros((30, 30), dtype=numpy.uint8)
i, j = numpy.mgrid[-15:15, -7:23]
labels[i ** 2 + j ** 2 <= 25] = 1
i, j = numpy.mgrid[-15:15, -22:8]
labels[i ** 2 + j ** 2 <= 16] = 2
objects = cellprofiler.object.Objects()