Example #1
0
def main(args=None):
    import cellprofiler.preferences
    """Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    """
    if args is None:
        args = sys.argv

    cellprofiler.preferences.set_awt_headless(True)

    exit_code = 0

    switches = ('--work-announce', '--knime-bridge-address')

    if any(
        [any([arg.startswith(switch) for switch in switches])
         for arg in args]):
        cellprofiler.preferences.set_headless()

        cellprofiler.worker.aw_parse_args()

        cellprofiler.worker.main()

        sys.exit(exit_code)

    options, args = parse_args(args)

    if options.print_version:
        __version__(exit_code)

    if (not options.show_gui) or options.write_schema_and_exit:
        cellprofiler.preferences.set_headless()

        options.run_pipeline = True

    set_log_level(options)

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)

    if options.plugins_directory is not None:
        cellprofiler.preferences.set_plugin_directory(
            options.plugins_directory, globally=False)

    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cellprofiler.preferences.set_temporary_directory(options.temp_dir,
                                                         globally=False)

    if not options.allow_schema_write:
        cellprofiler.preferences.set_allow_schema_write(False)

    if options.output_directory:
        if not os.path.exists(options.output_directory):
            os.makedirs(options.output_directory)

        cellprofiler.preferences.set_default_output_directory(
            options.output_directory)

    if options.image_directory:
        cellprofiler.preferences.set_default_image_directory(
            options.image_directory)

    if options.run_pipeline and not options.pipeline_filename:
        raise ValueError("You must specify a pipeline filename to run")

    if options.data_file is not None:
        cellprofiler.preferences.set_data_file(
            os.path.abspath(options.data_file))

    cellprofiler.utilities.cpjvm.cp_start_vm()

    if options.image_set_file is not None:
        cellprofiler.preferences.set_image_set_file(options.image_set_file)

    #
    # Handle command-line tasks that that need to load the modules to run
    #
    if options.print_measurements:
        print_measurements(options)

    if options.write_schema_and_exit:
        write_schema(options.pipeline_filename)

    if options.show_gui:
        matplotlib.use('WXAgg')

        import cellprofiler.gui.app

        if options.pipeline_filename:
            if cellprofiler.workspace.is_workspace_file(
                    options.pipeline_filename):
                workspace_path = os.path.expanduser(options.pipeline_filename)

                pipeline_path = None
            else:
                pipeline_path = os.path.expanduser(options.pipeline_filename)

                workspace_path = None
        else:
            workspace_path = None

            pipeline_path = None

        app = cellprofiler.gui.app.App(0,
                                       workspace_path=workspace_path,
                                       pipeline_path=pipeline_path)

        if options.run_pipeline:
            app.frame.pipeline_controller.do_analyze_images()

        app.MainLoop()

        return
    elif options.run_pipeline:
        run_pipeline_headless(options, args)

    stop_cellprofiler()
Example #2
0
def main(args=None):
    import cellprofiler.preferences
    """Run CellProfiler

    args - command-line arguments, e.g., sys.argv
    """
    if args is None:
        args = sys.argv

    cellprofiler.preferences.set_awt_headless(True)

    exit_code = 0

    switches = ("--work-announce", "--knime-bridge-address")

    if any(
        [any([arg.startswith(switch) for switch in switches])
         for arg in args]):
        cellprofiler.preferences.set_headless()
        cellprofiler.worker.aw_parse_args()
        cellprofiler.worker.main()
        return exit_code

    options, args = parse_args(args)

    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cellprofiler.preferences.set_temporary_directory(options.temp_dir,
                                                         globally=False)

    to_clean = []

    if options.pipeline_filename:
        o = urlparse(options.pipeline_filename)
        if o[0] in ("ftp", "http", "https"):
            import urllib2
            temp_pipe_file = tempfile.NamedTemporaryFile(mode='w+b',
                                                         suffix='.cppipe',
                                                         dir=temp_dir,
                                                         delete=False)
            downloaded_pipeline = urllib2.urlopen(options.pipeline_filename)
            for line in downloaded_pipeline:
                temp_pipe_file.write(line)
            options.pipeline_filename = temp_pipe_file.name
            to_clean.append(os.path.join(temp_dir, temp_pipe_file.name))

    if options.image_set_file:
        o = urlparse(options.image_set_file)
        if o[0] in ("ftp", "http", "https"):
            import urllib2
            temp_set_file = tempfile.NamedTemporaryFile(mode='w+b',
                                                        suffix='.csv',
                                                        dir=temp_dir,
                                                        delete=False)
            downloaded_set_csv = urllib2.urlopen(options.image_set_file)
            for line in downloaded_set_csv:
                temp_set_file.write(line)
            options.image_set_file = temp_set_file.name
            to_clean.append(os.path.join(temp_dir, temp_set_file.name))

    if options.data_file:
        o = urlparse(options.data_file)
        if o[0] in ("ftp", "http", "https"):
            import urllib2
            temp_data_file = tempfile.NamedTemporaryFile(mode='w+b',
                                                         suffix='.csv',
                                                         dir=temp_dir,
                                                         delete=False)
            downloaded_data_csv = urllib2.urlopen(options.data_file)
            for line in downloaded_data_csv:
                temp_data_file.write(line)
            options.data_file = temp_data_file.name
            to_clean.append(os.path.join(temp_dir, temp_data_file.name))

    if options.print_version:
        __version__(exit_code)

    if (not options.show_gui) or options.write_schema_and_exit:
        cellprofiler.preferences.set_headless()

        options.run_pipeline = True

    if options.batch_commands_file:
        cellprofiler.preferences.set_headless()
        options.run_pipeline = False
        options.show_gui = False

    set_log_level(options)

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)

    if options.batch_commands_file is not None:
        try:
            nr_per_batch = int(options.images_per_batch)
        except ValueError:
            logging.warning(
                "non-integer argument to --images-per-batch. Defaulting to 1.")
            nr_per_batch = 1
        get_batch_commands(options.batch_commands_file, nr_per_batch)

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)

    if options.plugins_directory is not None:
        cellprofiler.preferences.set_plugin_directory(
            options.plugins_directory, globally=False)

    if not options.allow_schema_write:
        cellprofiler.preferences.set_allow_schema_write(False)

    if options.output_directory:
        if not os.path.exists(options.output_directory):
            os.makedirs(options.output_directory)

        cellprofiler.preferences.set_default_output_directory(
            options.output_directory)

    if options.image_directory:
        cellprofiler.preferences.set_default_image_directory(
            options.image_directory)

    if options.run_pipeline and not options.pipeline_filename:
        raise ValueError("You must specify a pipeline filename to run")

    if options.data_file is not None:
        cellprofiler.preferences.set_data_file(
            os.path.abspath(options.data_file))

    try:
        if not options.show_gui:
            cellprofiler.utilities.cpjvm.cp_start_vm()

        if options.image_set_file is not None:
            cellprofiler.preferences.set_image_set_file(options.image_set_file)

        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.print_measurements:
            print_measurements(options)

        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)

        if options.show_gui:
            matplotlib.use("WXAgg")

            import cellprofiler.gui.app

            if options.pipeline_filename:
                if cellprofiler.workspace.is_workspace_file(
                        options.pipeline_filename):
                    workspace_path = os.path.expanduser(
                        options.pipeline_filename)

                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(
                        options.pipeline_filename)

                    workspace_path = None
            else:
                workspace_path = None

                pipeline_path = None

            app = cellprofiler.gui.app.App(0,
                                           workspace_path=workspace_path,
                                           pipeline_path=pipeline_path)

            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()

            app.MainLoop()

            return
        elif options.run_pipeline:
            exit_code = run_pipeline_headless(options, args)

    finally:
        # Cleanup the temp files we made, if any
        if len(to_clean) > 0:
            for each_temp in to_clean:
                os.remove(each_temp)
        # If anything goes wrong during the startup sequence headlessly, the JVM needs
        # to be explicitly closed
        if not options.show_gui:
            stop_cellprofiler()

    return exit_code