Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pipeline.add_listener(callback)
m = cellprofiler.measurement.Measurements()
for i, (y, x) in enumerate(control_points):
for v, f in (
(x, cellprofiler.modules.straightenworms.F_CONTROL_POINT_X),
(y, cellprofiler.modules.straightenworms.F_CONTROL_POINT_Y),
):
feature = "_".join(
(cellprofiler.modules.straightenworms.C_WORM, f, str(i + 1))
)
m.add_measurement(OBJECTS_NAME, feature, v)
feature = "_".join(
(
cellprofiler.modules.straightenworms.C_WORM,
cellprofiler.modules.straightenworms.F_LENGTH,
)
)
m.add_measurement(OBJECTS_NAME, feature, lengths)
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
image_set.add(IMAGE_NAME, cellprofiler.image.Image(image, mask))
if auximage is not None:
image_set.add(AUX_IMAGE_NAME, cellprofiler.image.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 = cellprofiler.object.ObjectSet()
objects = cellprofiler.object.Objects()
def test_test_overwrite_relationships_file(output_dir):
output_csv_filename = os.path.join(output_dir, "my_file.csv")
m = make_measurements()
pipeline = make_measurements_pipeline(m)
module = cellprofiler.modules.exporttospreadsheet.ExportToSpreadsheet()
module.directory.dir_choice = (
cellprofiler.modules.exporttospreadsheet.cps.ABSOLUTE_FOLDER_NAME
)
module.directory.custom_path = output_dir
module.wants_prefix.value = False
module.wants_everything.value = False
g = module.object_groups[0]
g.name.value = cellprofiler.modules.exporttospreadsheet.OBJECT_RELATIONSHIPS
g.wants_automatic_file_name.value = False
g.file_name.value = "my_file.csv"
module.set_module_num(1)
pipeline.add_module(module)
workspace = cellprofiler.workspace.Workspace(pipeline, module, m, None, m, None)
assert module.prepare_run(workspace)
with open(output_csv_filename, "w") as fd:
fd.write("Hello, world.\n")
assert not module.prepare_run(workspace)
def make_workspace(pixels, choices):
"""Make a workspace for running UnmixColors
pixels - input image
choices - a list of choice strings for the images desired
"""
pipeline = cellprofiler.pipeline.Pipeline()
def callback(caller, event):
assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)
pipeline.add_listener(callback)
module = cellprofiler.modules.unmixcolors.UnmixColors()
module.input_image_name.value = INPUT_IMAGE
module.outputs[0].image_name.value = output_image_name(0)
module.outputs[0].stain_choice.value = choices[0]
for i, choice in enumerate(choices[1:]):
module.add_image()
module.outputs[i + 1].image_name.value = output_image_name(i + 1)
module.outputs[i + 1].stain_choice.value = choice
module.set_module_num(1)
pipeline.add_module(module)
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
image = cellprofiler.image.Image(pixels)
image_set.add(INPUT_IMAGE, image)
import numpy.testing
import pytest
import skimage.data
import skimage.morphology
import cellprofiler.image
import cellprofiler.modules.morphologicalskeleton
instance = cellprofiler.modules.morphologicalskeleton.MorphologicalSkeleton()
@pytest.fixture(
scope="module",
params=[
(skimage.data.camera()[0:128, 0:128], 2),
(numpy.tile(skimage.data.camera()[0:32, 0:32], (2, 1)).reshape(2, 32, 32), 3),
],
ids=["grayscale_image", "grayscale_volume"],
)
def image(request):
data, dimensions = request.param
return cellprofiler.image.Image(image=data, dimensions=dimensions)
def run_module(
color_image=None, red_image=None, green_image=None, blue_image=None, fn=None
):
"""Run the InvertForPrinting module
Call this with Numpy arrays for the images and optionally
specify a function (fn) whose argument is an InvertForPrinting module.
You can specialize the module inside this function.
Returns a dictionary of the pixel data of the images in the image set
"""
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
module = cellprofiler.modules.invertforprinting.InvertForPrinting()
module.set_module_num(1)
for image, name, setting, check in (
(color_image, I_COLOR_IN, module.color_input_image, None),
(red_image, I_RED_IN, module.red_input_image, module.wants_red_input),
(green_image, I_GREEN_IN, module.green_input_image, module.wants_green_input),
(blue_image, I_BLUE_IN, module.blue_input_image, module.wants_blue_input),
):
if image is not None:
img = cellprofiler.image.Image(image)
image_set.add(name, img)
setting.value = name
if check is not None:
check.value = True
elif check is not None:
check.value = False
for name, setting in (
image,
mask,
subsample_size,
image_sample_size,
element_size,
granular_spectrum_length,
labels=None,
):
"""Make a pipeline with a MeasureGranularity module
image - measure granularity on this image
mask - exclude / include pixels from measurement. None = no mask
subsample_size, etc. - values for corresponding settings in the module
returns tuple of module & workspace
"""
module = cellprofiler.modules.measuregranularity.MeasureGranularity()
module.set_module_num(1)
image_setting = module.images[0]
# assert isinstance(image_setting, M.MeasureGranularity)
image_setting.image_name.value = IMAGE_NAME
image_setting.subsample_size.value = subsample_size
image_setting.image_sample_size.value = image_sample_size
image_setting.element_size.value = element_size
image_setting.granular_spectrum_length.value = granular_spectrum_length
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
img = cellprofiler.image.Image(image, mask)
image_set.add(IMAGE_NAME, img)
pipeline = cellprofiler.pipeline.Pipeline()
def error_callback(event, caller):
assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)
def test_display_objects_wrong_size():
labels = numpy.zeros((50, 120), int)
labels[10:20, 20:27] = 1
labels[30:35, 35:50] = 2
labels[5:18, 44:100] = 3
input_image = numpy.random.uniform(size=(60, 110))
for display in (
cellprofiler.modules.displaydataonimage.E_AXES,
cellprofiler.modules.displaydataonimage.E_FIGURE,
cellprofiler.modules.displaydataonimage.E_IMAGE,
):
workspace, module = make_workspace([0, 1, 2], labels, input_image)
module.saved_image_contents.value = display
module.run(workspace)
image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
im.image_name.value = image_name
im.wants_objects.value = True
im.object_name.value = object_name
expected_suffixes.append("%s_%s" % (image_name, object_name))
columns = module.get_measurement_columns(None)
assert all([column[0] == cellprofiler.measurement.IMAGE for column in columns])
for expected_suffix in expected_suffixes:
for feature, coltype in (
(
cellprofiler.modules.measureimageintensity.F_TOTAL_INTENSITY,
cellprofiler.measurement.COLTYPE_FLOAT,
),
(
cellprofiler.modules.measureimageintensity.F_MEAN_INTENSITY,
cellprofiler.measurement.COLTYPE_FLOAT,
),
(
cellprofiler.modules.measureimageintensity.F_MIN_INTENSITY,
cellprofiler.measurement.COLTYPE_FLOAT,
),
(
cellprofiler.modules.measureimageintensity.F_MAX_INTENSITY,
cellprofiler.measurement.COLTYPE_FLOAT,
),
(
cellprofiler.modules.measureimageintensity.F_TOTAL_AREA,
all_png_icons = glob.glob(os.path.join(icons_relpath, "*.png"))
icon_names = [os.path.basename(f)[:-4] for f in all_png_icons]
help_text = """
<h2>Help for CellProfiler Modules</h2>
<ul>\n"""
d = {}
module_path = webpage_path
if not (os.path.exists(module_path) and os.path.isdir(module_path)):
try:
os.mkdir(module_path)
except IOError:
raise ValueError("Could not create directory %s" % module_path)
for module_name in sorted(cellprofiler.modules.get_module_names()):
module = cellprofiler.modules.instantiate_module(module_name)
location = os.path.split(
module.create_settings.im_func.func_code.co_filename)[0]
if location == cellprofiler.preferences.get_plugin_directory():
continue
if isinstance(module.category, (str, unicode)):
module.category = [module.category]
for category in module.category:
if not d.has_key(category):
d[category] = {}
d[category][module_name] = module
result = module.get_help()
if result is None:
continue
result = result.replace('<h1>', '</h1><h1>Module: ')
# Replace refs to icons in memory with the relative path to the image dir (see above)</h1></ul>
def get_revision():
'''Return the maximum revision number from CellProfiler Python modules.
Starting with cellprofiler, find the maximum revision number by looking
at __version__ in all modules.
'''
version = 0
for module_name in sys.modules.keys():
if module_name.lower().startswith('cellprofiler') and ('plugins' not in module_name):
sub_version = __get_revision_of_module(sys.modules[module_name])
version = max(version, sub_version)
for module in cpmodules.pymodules:
sub_version = __get_revision_of_module(module)
version = max(version, sub_version)
return version