def execute_merge_files(mm):
    input_files = []
    output_fd, output_file = tempfile.mkstemp(".mat")
    pipeline = cellprofiler.pipeline.Pipeline()
    li = cellprofiler.modules.loadimages.LoadImages()
    li.module_num = 1
    pipeline.add_module(li)

    for m in mm:
        input_fd, input_file = tempfile.mkstemp(".mat")
        pipeline.save_measurements(input_file, m)
        input_files.append((input_fd, input_file))

    cellprofiler.modules.mergeoutputfiles.MergeOutputFiles.merge_files(
        output_file, [x[1] for x in input_files]
    )
    m = cellprofiler.measurement.load_measurements(output_file)
    os.close(output_fd)
    os.remove(output_file)
    for fd, filename in input_files:
        os.close(fd)
        os.remove(filename)
    return m
Esempio n. 2
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 = None

    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.pipeline.Pipeline()

    initial_measurements = None

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

        pipeline_text = pipeline_text.encode('us-ascii')

        pipeline.load(cStringIO.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.utilities.hdf5_dict.HDF5FileList.has_file_list(
                        src):
                    cellprofiler.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.preferences.get_image_set_file()

    if file_list is not None:
        pipeline.read_file_list(file_list)

    #
    # 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.measurement.EXPERIMENT,
                cellprofiler.pipeline.EXIT_STATUS):
            done_text = measurements.get_experiment_measurement(
                cellprofiler.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()
    else:
        exit_code = 0

    if measurements is not None:
        measurements.close()

    return exit_code
Esempio n. 3
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 = None

    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.pipeline.Pipeline()

    initial_measurements = None

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

        pipeline_text = pipeline_text.encode('us-ascii')

        pipeline.load(cStringIO.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.utilities.hdf5_dict.HDF5FileList.has_file_list(src):
                    cellprofiler.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.preferences.get_image_set_file()

    if file_list is not None:
        pipeline.read_file_list(file_list)

    #
    # 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.measurement.EXPERIMENT, cellprofiler.pipeline.EXIT_STATUS):
            done_text = measurements.get_experiment_measurement(cellprofiler.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()
    else:
        exit_code = 0

    if measurements is not None:
        measurements.close()

    return exit_code