def test_load_v4(): data = """CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:20141017202435 GitHash:b261e94 ModuleCount:3 HasImagePlaneDetails:False MeasureTexture:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Hidden:2 Hidden:2 Hidden:2 Select an image to measure:rawDNA Select an image to measure:rawGFP Select objects to measure:Cells Select objects to measure:Nuclei Texture scale to measure:3 Angles to measure:Horizontal,Vertical Texture scale to measure:5 Angles to measure:Diagonal,Anti-diagonal Measure Gabor features?:Yes Number of angles to compute for Gabor:6 Measure images or objects?:Images MeasureTexture:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Hidden:2 Hidden:2 Hidden:2 Select an image to measure:rawDNA Select an image to measure:rawGFP Select objects to measure:Cells Select objects to measure:Nuclei Texture scale to measure:3 Angles to measure:Horizontal,Vertical Texture scale to measure:5 Angles to measure:Diagonal,Anti-diagonal Measure Gabor features?:No Number of angles to compute for Gabor:6 Measure images or objects?:Objects MeasureTexture:[module_num:3|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Hidden:2 Hidden:2 Hidden:2 Select an image to measure:rawDNA Select an image to measure:rawGFP Select objects to measure:Cells Select objects to measure:Nuclei Texture scale to measure:3 Angles to measure:Horizontal,Vertical Texture scale to measure:5 Angles to measure:Diagonal,Anti-diagonal Measure Gabor features?:No Number of angles to compute for Gabor:6 Measure images or objects?:Both """ pipeline = cellprofiler_core.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 3 for i, (wants_gabor, io_choice) in enumerate(( (True, cellprofiler.modules.measuretexture.IO_IMAGES), (False, cellprofiler.modules.measuretexture.IO_OBJECTS), (False, cellprofiler.modules.measuretexture.IO_BOTH), )): module = pipeline.modules()[i] assert isinstance(module, cellprofiler.modules.measuretexture.MeasureTexture) assert len(module.images_list.value) == 2 assert {"rawDNA", "rawGFP"}.issubset(module.images_list.value) assert len(module.objects_list.value) == 2 assert {"Cells", "Nuclei"}.issubset(module.objects_list.value) assert len(module.scale_groups) == 2 assert module.scale_groups[0].scale == 3 assert module.scale_groups[1].scale == 5
def test_load_v5(): with open( "./tests/resources/modules/measureobjectintensitydistribution/v5.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler_core.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 2 module = pipeline.modules()[0] assert isinstance( module, cellprofiler.modules.measureobjectintensitydistribution. MeasureObjectIntensityDistribution, ) assert (module.wants_zernikes == cellprofiler.modules. measureobjectintensitydistribution.Z_MAGNITUDES) assert module.zernike_degree == 7 assert len(module.images_list.value) == 2 assert {'CropGreen', 'CropRed'}.issubset(module.images_list.value) assert len(module.objects) == 2 for group, (object_name, center_choice, center_object_name) in zip( module.objects, ( ( "Nuclei", cellprofiler.modules.measureobjectintensitydistribution.C_SELF, "Ichthyosaurs", ), ( "Cells", cellprofiler.modules.measureobjectintensitydistribution. C_EDGES_OF_OTHER, "Nuclei", ), ), ): assert group.object_name.value == object_name assert group.center_choice.value == center_choice assert group.center_object_name == center_object_name assert len(module.bin_counts) == 2 for group, (bin_count, scale, max_radius) in zip(module.bin_counts, ((5, True, 100), (4, False, 100))): assert group.wants_scaled == scale assert group.bin_count == bin_count assert group.maximum_radius == max_radius for ( group, ( image_name, object_name, bin_count, measurement, colormap, wants_to_save, output_image_name, ), ) in zip( module.heatmaps, ( ( "CropRed", "Cells", 5, cellprofiler.modules.measureobjectintensitydistribution. A_FRAC_AT_D, "Default", True, "Heat", ), ( "CropGreen", "Nuclei", 4, cellprofiler.modules.measureobjectintensitydistribution. A_MEAN_FRAC, "Spectral", False, "A", ), ( "CropRed", "Nuclei", 5, cellprofiler.modules.measureobjectintensitydistribution. A_RADIAL_CV, "Default", False, "B", ), ), ): assert group.image_name.value == image_name assert group.object_name.value == object_name assert int(group.bin_count.value) == bin_count assert group.measurement == measurement assert group.colormap == colormap assert group.wants_to_save_display == wants_to_save assert group.display_name == output_image_name module = pipeline.modules()[1] assert (module.wants_zernikes == cellprofiler.modules. measureobjectintensitydistribution.Z_MAGNITUDES_AND_PHASE)
def test_load_v4(): file = tests.modules.test_resources_directory( "measureimagequality/v4.pipeline") with open(file, "r") as fd: data = fd.read() pipeline = cellprofiler_core.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 5 for module in pipeline.modules(): assert isinstance( module, cellprofiler.modules.measureimagequality.MeasureImageQuality) module = pipeline.modules()[0] assert len(module.image_groups) == 1 group = module.image_groups[0] assert group.threshold_groups == [] assert module.images_choice == cellprofiler.modules.measureimagequality.O_ALL_LOADED assert group.check_blur assert group.scale_groups[0].scale == 20 assert group.check_saturation assert group.check_intensity assert group.calculate_threshold assert group.use_all_threshold_methods module = pipeline.modules()[1] assert len(module.image_groups) == 1 group = module.image_groups[0] assert module.images_choice == cellprofiler.modules.measureimagequality.O_SELECT assert group.image_names.value_text == "Alpha" module = pipeline.modules()[2] assert len(module.image_groups) == 1 group = module.image_groups[0] assert module.images_choice == cellprofiler.modules.measureimagequality.O_SELECT assert "Delta" in group.image_names.value assert "Beta" in group.image_names.value assert len(group.image_names.value) == 2 module = pipeline.modules()[3] assert len(module.image_groups) == 2 group = module.image_groups[0] assert module.images_choice == cellprofiler.modules.measureimagequality.O_SELECT assert group.image_names.value_text == "Delta" assert group.check_intensity assert not group.use_all_threshold_methods thr = group.threshold_groups[0] assert thr.threshold_method == centrosome.threshold.TM_OTSU assert (thr.use_weighted_variance == O_WEIGHTED_VARIANCE) assert thr.two_class_otsu == O_TWO_CLASS group = module.image_groups[1] assert group.image_names.value_text == "Epsilon" module = pipeline.modules()[4] assert len(module.image_groups) == 1 group = module.image_groups[0] assert module.images_choice == cellprofiler.modules.measureimagequality.O_SELECT assert group.image_names.value_text == "Zeta" assert not group.use_all_threshold_methods thr = group.threshold_groups[0] assert thr.threshold_method == centrosome.threshold.TM_OTSU assert (thr.use_weighted_variance == O_WEIGHTED_VARIANCE) assert thr.two_class_otsu == O_TWO_CLASS thr = group.threshold_groups[1] assert thr.threshold_method == centrosome.threshold.TM_OTSU assert (thr.use_weighted_variance == O_WEIGHTED_VARIANCE) assert thr.two_class_otsu == O_THREE_CLASS assert (thr.assign_middle_to_foreground == O_FOREGROUND)
def test_load_v2(): file = tests.modules.get_test_resources_directory( "enhanceorsuppressfeatures/v2.pipeline") with open(file, "r") as fd: data = fd.read() pipeline = cellprofiler_core.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 5 for ( module, ( input_name, output_name, operation, feature_size, feature_type, min_range, max_range, ), ) in zip( pipeline.modules(), ( ( "Initial", "EnhancedSpeckles", cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE, 11, cellprofiler.modules.enhanceorsuppressfeatures.E_SPECKLES, 1, 10, ), ( "EnhancedSpeckles", "EnhancedNeurites", cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE, 9, cellprofiler.modules.enhanceorsuppressfeatures.E_NEURITES, 1, 10, ), ( "EnhancedNeurites", "EnhancedDarkHoles", cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE, 9, cellprofiler.modules.enhanceorsuppressfeatures.E_DARK_HOLES, 4, 11, ), ( "EnhancedDarkHoles", "EnhancedCircles", cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE, 9, cellprofiler.modules.enhanceorsuppressfeatures.E_CIRCLES, 4, 11, ), ( "EnhancedCircles", "Suppressed", cellprofiler.modules.enhanceorsuppressfeatures.SUPPRESS, 13, cellprofiler.modules.enhanceorsuppressfeatures.E_CIRCLES, 4, 11, ), ), ): assert module.module_name == "EnhanceOrSuppressFeatures" assert isinstance( module, cellprofiler.modules.enhanceorsuppressfeatures. EnhanceOrSuppressFeatures, ) assert module.x_name == input_name assert module.y_name == output_name assert module.method == operation assert module.enhance_method == feature_type assert module.object_size == feature_size assert module.hole_size.min == min_range assert module.hole_size.max == max_range
def test_load_v11(): pipeline_txt = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:300 GitHash: ModuleCount:4 HasImagePlaneDetails:False SaveImages:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:11|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Select the type of image to save:Image Select the image to save:DNA Select the objects to save:None Select the module display window to save:None Select method for constructing file names:From image filename Select image name for file prefix:DNA Enter single file name:OrigBlue Number of digits:4 Append a suffix to the image file name?:No Text to append to the image name: Saved file format:png Output file location:Default Output Folder\x7C Image bit depth:8-bit integer Overwrite existing files without warning?:No When to save:Every cycle Rescale the images? :No Save as grayscale or color image?:Grayscale Select colormap:gray Record the file and path information to the saved image?:No Create subfolders in the output folder?:No Base image folder:Elsewhere...\x7C Saved movie format:avi SaveImages:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:11|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Select the type of image to save:Image Select the image to save:DNA Select the objects to save:None Select the module display window to save:None Select method for constructing file names:From image filename Select image name for file prefix:DNA Enter single file name:OrigBlue Number of digits:4 Append a suffix to the image file name?:No Text to append to the image name: Saved file format:jpg Output file location:Default Output Folder\x7C Image bit depth:8-bit integer Overwrite existing files without warning?:No When to save:Every cycle Rescale the images? :No Save as grayscale or color image?:Grayscale Select colormap:gray Record the file and path information to the saved image?:No Create subfolders in the output folder?:No Base image folder:Elsewhere...\x7C Saved movie format:avi SaveImages:[module_num:3|svn_version:\'Unknown\'|variable_revision_number:11|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Select the type of image to save:Image Select the image to save:DNA Select the objects to save:None Select the module display window to save:None Select method for constructing file names:From image filename Select image name for file prefix:DNA Enter single file name:OrigBlue Number of digits:4 Append a suffix to the image file name?:No Text to append to the image name: Saved file format:tif Output file location:Default Output Folder\x7C Image bit depth:8-bit integer Overwrite existing files without warning?:No When to save:Every cycle Rescale the images? :No Save as grayscale or color image?:Grayscale Select colormap:gray Record the file and path information to the saved image?:No Create subfolders in the output folder?:No Base image folder:Elsewhere...\x7C Saved movie format:avi SaveImages:[module_num:4|svn_version:\'Unknown\'|variable_revision_number:11|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Select the type of image to save:Movie Select the image to save:DNA Select the objects to save:None Select the module display window to save:None Select method for constructing file names:From image filename Select image name for file prefix:DNA Enter single file name:OrigBlue Number of digits:4 Append a suffix to the image file name?:No Text to append to the image name: Saved file format:tif Output file location:Default Output Folder\x7C Image bit depth:8-bit integer Overwrite existing files without warning?:No When to save:Every cycle Rescale the images? :No Save as grayscale or color image?:Grayscale Select colormap:gray Record the file and path information to the saved image?:No Create subfolders in the output folder?:No Base image folder:Elsewhere...\x7C Saved movie format:avi """ def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline = cellprofiler_core.pipeline.Pipeline() pipeline.add_listener(callback) pipeline.load(io.StringIO(pipeline_txt)) module = pipeline.modules()[0] assert module.save_image_or_figure.value == cellprofiler.modules.saveimages.IF_IMAGE assert module.image_name.value == "DNA" assert (module.file_name_method.value == cellprofiler.modules.saveimages.FN_FROM_IMAGE) assert module.file_image_name.value == "DNA" assert module.single_file_name.value == "OrigBlue" assert module.number_of_digits.value == 4 assert module.wants_file_name_suffix.value == False assert module.file_format.value == "png" assert module.pathname.value == "Default Output Folder|" assert module.bit_depth.value == cellprofiler.modules.saveimages.BIT_DEPTH_8 assert module.overwrite.value == False assert module.when_to_save.value == cellprofiler.modules.saveimages.WS_EVERY_CYCLE assert module.update_file_names.value == False assert module.create_subdirectories.value == False assert module.root_dir.value == "Elsewhere...|" module = pipeline.modules()[1] assert module.file_format.value == "jpeg" module = pipeline.modules()[2] assert module.file_format.value == "tiff" module = pipeline.modules()[3] assert module.file_format.value == "tiff"
def test_load_v1(): with open("./tests/resources/modules/maskobjects/v1.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler_core.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline.add_listener(callback) pipeline.load(six.moves.StringIO(data)) assert len(pipeline.modules()) == 4 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.maskobjects.MaskObjects) assert module.object_name.value == "Nuclei" assert module.mask_choice.value == cellprofiler.modules.maskobjects.MC_OBJECTS assert module.masking_objects.value == "Wells" assert module.remaining_objects.value == "MaskedNuclei" assert ( module.retain_or_renumber.value == cellprofiler.modules.maskobjects.R_RENUMBER ) assert module.overlap_choice.value == cellprofiler.modules.maskobjects.P_MASK assert not module.wants_inverted_mask module = pipeline.modules()[1] assert isinstance(module, cellprofiler.modules.maskobjects.MaskObjects) assert module.object_name.value == "Cells" assert module.mask_choice.value == cellprofiler.modules.maskobjects.MC_IMAGE assert module.masking_image.value == "WellBoundary" assert module.remaining_objects.value == "MaskedCells" assert module.retain_or_renumber.value == cellprofiler.modules.maskobjects.R_RETAIN assert module.overlap_choice.value == cellprofiler.modules.maskobjects.P_KEEP assert not module.wants_inverted_mask module = pipeline.modules()[2] assert isinstance(module, cellprofiler.modules.maskobjects.MaskObjects) assert module.object_name.value == "Cytoplasm" assert module.mask_choice.value == cellprofiler.modules.maskobjects.MC_OBJECTS assert module.masking_objects.value == "Cells" assert module.remaining_objects.value == "MaskedCytoplasm" assert ( module.retain_or_renumber.value == cellprofiler.modules.maskobjects.R_RENUMBER ) assert module.overlap_choice.value == cellprofiler.modules.maskobjects.P_REMOVE assert not module.wants_inverted_mask module = pipeline.modules()[3] assert isinstance(module, cellprofiler.modules.maskobjects.MaskObjects) assert module.object_name.value == "Speckles" assert module.mask_choice.value == cellprofiler.modules.maskobjects.MC_OBJECTS assert module.masking_objects.value == "Cells" assert module.remaining_objects.value == "MaskedSpeckles" assert ( module.retain_or_renumber.value == cellprofiler.modules.maskobjects.R_RENUMBER ) assert ( module.overlap_choice.value == cellprofiler.modules.maskobjects.P_REMOVE_PERCENTAGE ) assert round(abs(module.overlap_fraction.value - 0.3), 7) == 0 assert not module.wants_inverted_mask
def test_load_v3(): file = tests.modules.get_test_resources_directory("flagimages/v3.pipeline") with open(file, "r") as fd: data = fd.read() pipeline = cellprofiler_core.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler_core.pipeline.event.LoadException) pipeline.add_listener(callback) pipeline.load(six.moves.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.flagimage.FlagImage) expected = ( ( "QCFlag", cellprofiler.modules.flagimage.C_ANY, False, ( ("Intensity_MaxIntensity_DNA", None, 0.95, "foo.txt"), ("Intensity_MinIntensity_Cytoplasm", 0.05, None, "bar.txt"), ("Intensity_MeanIntensity_DNA", 0.1, 0.9, "baz.txt"), ), ), ( "HighCytoplasmIntensity", None, True, (("Intensity_MeanIntensity_Cytoplasm", None, 0.8, "dunno.txt"), ), ), ) assert len(expected) == module.flag_count.value for flag, (feature_name, combine, skip, measurements) in zip(module.flags, expected): assert isinstance(flag, cellprofiler_core.setting.SettingsGroup) assert flag.category == "Metadata" assert flag.feature_name == feature_name assert flag.wants_skip == skip if combine is not None: assert flag.combination_choice == combine assert len(measurements) == flag.measurement_count.value for (measurement, (measurement_name, min_value, max_value, rules_file)) in zip(flag.measurement_settings, measurements): assert isinstance(measurement, cellprofiler_core.setting.SettingsGroup) assert measurement.source_choice == cellprofiler.modules.flagimage.S_IMAGE assert measurement.measurement == measurement_name assert measurement.wants_minimum.value == (min_value is not None) if measurement.wants_minimum.value: assert round(abs(measurement.minimum_value.value - min_value), 7) == 0 assert measurement.wants_maximum.value == (max_value is not None) if measurement.wants_maximum.value: assert round(abs(measurement.maximum_value.value - max_value), 7) == 0 assert measurement.rules_file_name == rules_file assert measurement.rules_class == "1"
def run_pipeline_headless(options, args): """ Run a CellProfiler pipeline in headless mode """ if options.first_image_set is not None: if not options.first_image_set.isdigit(): raise ValueError( "The --first-image-set option takes a numeric argument") else: image_set_start = int(options.first_image_set) else: image_set_start = 1 image_set_numbers = None if options.last_image_set is not None: if not options.last_image_set.isdigit(): raise ValueError( "The --last-image-set option takes a numeric argument") else: image_set_end = int(options.last_image_set) if image_set_start is None: image_set_numbers = numpy.arange(1, image_set_end + 1) else: image_set_numbers = numpy.arange(image_set_start, image_set_end + 1) else: image_set_end = None if (options.pipeline_filename is not None) and ( not options.pipeline_filename.lower().startswith("http")): options.pipeline_filename = os.path.expanduser( options.pipeline_filename) pipeline = cellprofiler_core.pipeline.Pipeline() initial_measurements = None try: if h5py.is_hdf5(options.pipeline_filename): initial_measurements = cellprofiler_core.measurement.load_measurements( options.pipeline_filename, image_numbers=image_set_numbers) except: logging.root.info("Failed to load measurements from pipeline") if initial_measurements is not None: pipeline_text = initial_measurements.get_experiment_measurement( cellprofiler_core.pipeline.M_PIPELINE) pipeline_text = pipeline_text pipeline.load(six.moves.StringIO(pipeline_text)) if not pipeline.in_batch_mode(): # # Need file list in order to call prepare_run # with h5py.File(options.pipeline_filename, "r") as src: if cellprofiler_core.utilities.hdf5_dict.HDF5FileList.has_file_list( src): cellprofiler_core.utilities.hdf5_dict.HDF5FileList.copy( src, initial_measurements.hdf5_dict.hdf5_file) else: pipeline.load(options.pipeline_filename) if options.groups is not None: kvs = [x.split("=") for x in options.groups.split(",")] groups = dict(kvs) else: groups = None file_list = cellprofiler_core.preferences.get_image_set_file() if file_list is not None: pipeline.read_file_list(file_list) elif options.image_directory is not None: pathnames = [] for dirname, _, fnames in os.walk( os.path.abspath(options.image_directory)): pathnames.append([ os.path.join(dirname, fname) for fname in fnames if os.path.isfile(os.path.join(dirname, fname)) ]) pathnames = sum(pathnames, []) pipeline.add_pathnames_to_file_list(pathnames) # # Fixup CreateBatchFiles with any command-line input or output directories # if pipeline.in_batch_mode(): create_batch_files = [ m for m in pipeline.modules() if m.is_create_batch_module() ] if len(create_batch_files) > 0: create_batch_files = create_batch_files[0] if options.output_directory is not None: create_batch_files.custom_output_directory.value = ( options.output_directory) if options.image_directory is not None: create_batch_files.default_image_directory.value = ( options.image_directory) use_hdf5 = len(args) > 0 and not args[0].lower().endswith(".mat") measurements = pipeline.run( image_set_start=image_set_start, image_set_end=image_set_end, grouping=groups, measurements_filename=None if not use_hdf5 else args[0], initial_measurements=initial_measurements, ) if len(args) > 0 and not use_hdf5: pipeline.save_measurements(args[0], measurements) if options.done_file is not None: if measurements is not None and measurements.has_feature( cellprofiler_core.measurement.EXPERIMENT, cellprofiler_core.pipeline.EXIT_STATUS, ): done_text = measurements.get_experiment_measurement( cellprofiler_core.pipeline.EXIT_STATUS) exit_code = 0 if done_text == "Complete" else -1 else: done_text = "Failure" exit_code = -1 fd = open(options.done_file, "wt") fd.write("%s\n" % done_text) fd.close() elif not measurements.has_feature(cellprofiler_core.measurement.EXPERIMENT, cellprofiler_core.pipeline.EXIT_STATUS): # The pipeline probably failed exit_code = 1 else: exit_code = 0 if measurements is not None: measurements.close() return exit_code