Ejemplo n.º 1
0
    def test_01_03_load_v2(self):
        data = r"""CellProfiler Pipeline: http://www.cellprofiler.org
Version:1
SVNRevision:8945

Align:[module_num:1|svn_version:'8942'|variable_revision_number:2|show_window:True|notes:\x5B\x5D]
Select the alignment method:Mutual Information
Crop output images to retain just the aligned regions?:Yes
Select the first input image:Image1
Name the first output image:AlignedImage1
Select the second input image:Image2
Name the second output image:AlignedImage2
"""
        pipeline = Pipeline()

        def callback(caller, event):
            assert not isinstance(event, LoadException)

        pipeline.add_listener(callback)
        pipeline.load(StringIO(data))
        assert len(pipeline.modules()) == 1
        module = pipeline.modules()[0]
        assert isinstance(module, Align)
        assert module.alignment_method == M_MUTUAL_INFORMATION
        assert module.crop_mode == C_CROP
        assert module.first_input_image == "Image1"
        assert module.second_input_image == "Image2"
        assert module.first_output_image == "AlignedImage1"
        assert module.second_output_image == "AlignedImage2"
Ejemplo n.º 2
0
 def make_workspace(self, images, masks):
     pipeline = Pipeline()
     object_set = ObjectSet()
     image_set_list = ImageSetList()
     image_set = image_set_list.get_image_set(0)
     module = Align()
     workspace = Workspace(pipeline, module, image_set, object_set,
                           Measurements(), image_set_list)
     for index, (pixels, mask) in enumerate(zip(images, masks)):
         if mask is None:
             image = Image(pixels)
         else:
             image = Image(pixels, mask=mask)
         input_name = "Channel%d" % index
         output_name = "Aligned%d" % index
         image_set.add(input_name, image)
         if index == 0:
             module.first_input_image.value = input_name
             module.first_output_image.value = output_name
         elif index == 1:
             module.second_input_image.value = input_name
             module.second_output_image.value = output_name
         else:
             module.add_image()
             ai = module.additional_images[-1]
             ai.input_image_name.value = input_name
             ai.output_image_name.value = output_name
     return workspace, module
Ejemplo n.º 3
0
def write_schema(pipeline_filename):
    if pipeline_filename is None:
        raise ValueError(
            "The --write-schema-and-exit switch must be used in conjunction\nwith the -p or --pipeline switch to load a pipeline with an\n"
            "ExportToDatabase module.")

    pipeline = Pipeline()

    pipeline.load(pipeline_filename)

    pipeline.turn_off_batch_mode()

    for module in pipeline.modules():
        if module.module_name == "ExportToDatabase":
            break
    else:
        raise ValueError(
            'The pipeline, "%s", does not have an ExportToDatabase module' %
            pipeline_filename)

    m = Measurements()

    workspace = Workspace(pipeline, module, m, ObjectSet, m, None)

    module.prepare_run(workspace)
Ejemplo n.º 4
0
    def test_01_04_load_v3(self):
        data = r"""CellProfiler Pipeline: http://www.cellprofiler.org
Version:1
SVNRevision:8945

Align:[module_num:1|svn_version:'8942'|variable_revision_number:3|show_window:True|notes:\x5B\x5D]
Select the alignment method:Mutual Information
Crop mode:Keep size
Select the first input image:Image1
Name the first output image:AlignedImage1
Select the second input image:Image2
Name the second output image:AlignedImage2

Align:[module_num:1|svn_version:'8942'|variable_revision_number:3|show_window:True|notes:\x5B\x5D]
Select the alignment method:Mutual Information
Crop mode:Crop to aligned region
Select the first input image:Image1
Name the first output image:AlignedImage1
Select the second input image:Image2
Name the second output image:AlignedImage2

Align:[module_num:1|svn_version:'8942'|variable_revision_number:3|show_window:True|notes:\x5B\x5D]
Select the alignment method:Mutual Information
Crop mode:Pad images
Select the first input image:Image1
Name the first output image:AlignedImage1
Select the second input image:Image2
Name the second output image:AlignedImage2
"""
        pipeline = Pipeline()

        def callback(caller, event):
            assert not isinstance(event, LoadException)

        pipeline.add_listener(callback)
        pipeline.load(StringIO(data))
        assert len(pipeline.modules()) == 3
        for module, crop_method in zip(pipeline.modules(),
                                       (C_SAME_SIZE, C_CROP, C_PAD)):
            assert isinstance(module, Align)
            assert module.alignment_method == M_MUTUAL_INFORMATION
            assert module.crop_mode == crop_method
            assert module.first_input_image == "Image1"
            assert module.second_input_image == "Image2"
            assert module.first_output_image == "AlignedImage1"
            assert module.second_output_image == "AlignedImage2"
Ejemplo n.º 5
0
 def make_workspace(self, image, mask=None, dimensions=2):
     '''Make a workspace for running Threshold. Taken from CellProfiler's test suite.'''
     module = Threshold()
     module.x_name.value = 'input'
     module.y_name.value = 'output'
     module.threshold_range.value = self.threshold_range.value
     module.threshold_correction_factor.value = self.threshold_correction_factor.value
     module.threshold_smoothing_scale.value = self.threshold_smoothing_scale.value
     pipeline = Pipeline()
     object_set = ObjectSet()
     image_set_list = cellprofiler_core.image.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     workspace = Workspace(pipeline, module, image_set, object_set,
                           cellprofiler_core.measurement.Measurements(),
                           image_set_list)
     image_set.add(
         'input',
         cellprofiler_core.image.Image(image, dimensions=dimensions)
         if mask is None else cellprofiler_core.image.Image(
             image, mask, dimensions=dimensions))
     return workspace, module
Ejemplo n.º 6
0
def print_measurements(options):
    """Print the measurements that would be output by a pipeline

    This function calls Pipeline.get_measurement_columns() to get the
    measurements that would be output by a pipeline. This can be used in
    a workflow tool or LIMS to find the outputs of a pipeline without
    running it. For instance, someone might want to integrate CellProfiler
    with Knime and write a Knime node that let the user specify a pipeline
    file. The node could then execute CellProfiler with the --measurements
    switch and display the measurements as node outputs.
    """

    if options.pipeline_filename is None:
        raise ValueError("Can't print measurements, no pipeline file")

    pipeline = Pipeline()

    def callback(pipeline, event):
        if isinstance(event, LoadException):
            raise ValueError("Failed to load %s" % options.pipeline_filename)

    pipeline.add_listener(callback)

    pipeline.load(os.path.expanduser(options.pipeline_filename))

    columns = pipeline.get_measurement_columns()

    print("--- begin measurements ---")

    print("Object,Feature,Type")

    for column in columns:
        object_name, feature, data_type = column[:3]

        print("%s,%s,%s" % (object_name, feature, data_type))

    print("--- end measurements ---")
Ejemplo n.º 7
0
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 = Pipeline()

    initial_measurements = None

    try:
        if h5py.is_hdf5(options.pipeline_filename):
            initial_measurements = 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(
            M_PIPELINE)

        pipeline_text = pipeline_text

        pipeline.load(io.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 HDF5FileList.has_file_list(src):
                    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 = 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(
                EXPERIMENT,
                EXIT_STATUS,
        ):
            done_text = measurements.get_experiment_measurement(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(EXPERIMENT, EXIT_STATUS):
        # The pipeline probably failed
        exit_code = 1
    else:
        exit_code = 0

    if measurements is not None:
        measurements.close()

    return exit_code