def aw_parse_args():
    '''Parse the application arguments into setup parameters'''
    from cellprofiler.preferences import set_headless, set_plugin_directory
    from cellprofiler.preferences import set_ij_plugin_directory
    import optparse
    global work_announce_address
    set_headless()
    parser = optparse.OptionParser()
    parser.add_option("--work-announce",
                      dest="work_announce_address",
                      help="ZMQ port where work announcements are published",
                      default=None)
    parser.add_option(
        "--log-level",
        dest="log_level",
        help="Logging level for logger: DEBUG, INFO, WARNING, ERROR",
        default=os.environ.get(AW_LOG_LEVEL, logging.INFO))
    parser.add_option(
        "--plugins-directory",
        dest="plugins_directory",
        help=
        "Folder containing the CellProfiler plugin modules needed by client pipelines",
        default=None)
    parser.add_option(
        "--ij-plugins-directory",
        dest="ij_plugins_directory",
        help=
        "Folder containing the ImageJ plugin .jar and .class files needed bby client pipelines",
        default=None)
    parser.add_option(
        "--jvm-heap-size",
        dest="jvm_heap_size",
        default="512m",
        help=("This is the amount of memory reserved for the "
              "Java Virtual Machine (similar to the java -Xmx switch)."
              "Example formats: 512000k, 512m, 1g"))

    options, args = parser.parse_args()
    logging.root.setLevel(options.log_level)
    if len(logging.root.handlers) == 0:
        logging.root.addHandler(logging.StreamHandler())

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    work_announce_address = options.work_announce_address
    #
    # Set up the headless plugins and ij plugins directories before doing
    # anything so loading will get them
    #
    if options.plugins_directory is not None:
        set_plugin_directory(options.plugins_directory)
    else:
        logger.warning("Plugins directory not set")
    if options.ij_plugins_directory is not None:
        logger.debug("Using %s as IJ plugins directory" %
                     options.ij_plugins_directory)
        set_ij_plugin_directory(options.ij_plugins_directory)
    else:
        logger.debug("IJ plugins directory not set")
def aw_parse_args():
    '''Parse the application arguments into setup parameters'''
    from cellprofiler.preferences import \
         set_headless, set_awt_headless, \
         set_plugin_directory, set_ij_plugin_directory
    import optparse
    global work_announce_address
    set_headless()
    set_awt_headless(False)
    parser = optparse.OptionParser()
    parser.add_option("--work-announce",
                      dest="work_announce_address",
                      help="ZMQ port where work announcements are published",
                      default=None)
    parser.add_option("--log-level",
                      dest="log_level",
                      help="Logging level for logger: DEBUG, INFO, WARNING, ERROR",
                      default=os.environ.get(AW_LOG_LEVEL, logging.INFO))
    parser.add_option("--plugins-directory",
                      dest="plugins_directory",
                      help="Folder containing the CellProfiler plugin modules needed by client pipelines",
                      default=None)
    parser.add_option("--ij-plugins-directory",
                      dest="ij_plugins_directory",
                      help="Folder containing the ImageJ plugin .jar and .class files needed bby client pipelines",
                      default=None)
    parser.add_option("--jvm-heap-size",
                      dest="jvm_heap_size",
                      default=None,
                      help=("This is the amount of memory reserved for the "
                            "Java Virtual Machine (similar to the java -Xmx switch)."
                            "Example formats: 512000k, 512m, 1g"))

    options, args = parser.parse_args()
    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    logging.root.setLevel(options.log_level)
    if len(logging.root.handlers) == 0:
        logging.root.addHandler(logging.StreamHandler())

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    work_announce_address = options.work_announce_address
    #
    # Set up the headless plugins and ij plugins directories before doing
    # anything so loading will get them
    #
    if options.plugins_directory is not None:
        set_plugin_directory(options.plugins_directory)
    else:
        logger.warning("Plugins directory not set")
    if options.ij_plugins_directory is not None:
        logger.debug("Using %s as IJ plugins directory" % options.ij_plugins_directory)
        set_ij_plugin_directory(options.ij_plugins_directory)
    else:
        logger.debug("IJ plugins directory not set")
Esempio n. 3
0
def aw_parse_args():
    '''Parse the application arguments into setup parameters'''
    from cellprofiler.preferences import set_headless, set_plugin_directory
    from cellprofiler.preferences import set_ij_plugin_directory
    import optparse
    global work_announce_address
    set_headless()
    parser = optparse.OptionParser()
    parser.add_option("--work-announce",
                      dest="work_announce_address",
                      help="ZMQ port where work announcements are published",
                      default=None)
    parser.add_option(
        "--log-level",
        dest="log_level",
        help="Logging level for logger: DEBUG, INFO, WARNING, ERROR",
        default=os.environ.get(AW_LOG_LEVEL, logging.INFO))
    parser.add_option(
        "--plugins-directory",
        dest="plugins_directory",
        help=
        "Folder containing the CellProfiler plugin modules needed by client pipelines",
        default=None)
    parser.add_option(
        "--ij-plugins-directory",
        dest="ij_plugins_directory",
        help=
        "Folder containing the ImageJ plugin .jar and .class files needed bby client pipelines",
        default=None)
    options, args = parser.parse_args()
    logging.root.setLevel(options.log_level)

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    work_announce_address = options.work_announce_address
    #
    # Set up the headless plugins and ij plugins directories before doing
    # anything so loading will get them
    #
    if options.plugins_directory is not None:
        set_plugin_directory(options.plugins_directory)
    else:
        logger.warning("Plugins directory not set")
    if options.ij_plugins_directory is not None:
        logger.debug("Using %s as IJ plugins directory" %
                     options.ij_plugins_directory)
        set_ij_plugin_directory(options.ij_plugins_directory)
    else:
        logger.debug("IJ plugins directory not set")
def aw_parse_args():
    '''Parse the application arguments into setup parameters'''
    from cellprofiler.preferences import set_headless, set_plugin_directory
    from cellprofiler.preferences import set_ij_plugin_directory
    import optparse
    global work_announce_address
    set_headless()
    parser = optparse.OptionParser()
    parser.add_option("--work-announce",
                      dest="work_announce_address",
                      help="ZMQ port where work announcements are published",
                      default=None)
    parser.add_option("--log-level",
                      dest="log_level",
                      help="Logging level for logger: DEBUG, INFO, WARNING, ERROR",
                      default=os.environ.get(AW_LOG_LEVEL, logging.INFO))
    parser.add_option("--plugins-directory",
                      dest="plugins_directory",
                      help="Folder containing the CellProfiler plugin modules needed by client pipelines",
                      default=None)
    parser.add_option("--ij-plugins-directory",
                      dest="ij_plugins_directory",
                      help="Folder containing the ImageJ plugin .jar and .class files needed bby client pipelines",
                      default=None)
    options, args = parser.parse_args()
    logging.root.setLevel(options.log_level)
    if len(logging.root.handlers) == 0:
        logging.root.addHandler(logging.StreamHandler())

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    work_announce_address = options.work_announce_address
    #
    # Set up the headless plugins and ij plugins directories before doing
    # anything so loading will get them
    #
    if options.plugins_directory is not None:
        set_plugin_directory(options.plugins_directory)
    else:
        logger.warning("Plugins directory not set")
    if options.ij_plugins_directory is not None:
        logger.debug("Using %s as IJ plugins directory" % options.ij_plugins_directory)
        set_ij_plugin_directory(options.ij_plugins_directory)
    else:
        logger.debug("IJ plugins directory not set")
Esempio n. 5
0
def main(args=None):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if args is None:
        args = sys.argv
    import cellprofiler.preferences as cpprefs
    cpprefs.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]):
        #
        # Go headless ASAP
        #
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i + 1:
                cpprefs.set_ij_plugin_directory(args[i + 1])
                break
        import cellprofiler.worker
        cellprofiler.worker.aw_parse_args()
        cellprofiler.worker.main()
        sys.exit(exit_code)

    options, args = parse_args(args)
    if options.print_version:
        from cellprofiler.utilities.version import \
            dotted_version, version_string, git_hash, version_number
        print "CellProfiler %s" % dotted_version
        print "Git %s" % git_hash
        print "Version %s" % version_number
        print "Built %s" % version_string.split(" ")[0]
        sys.exit(exit_code)
    #
    # Important to go headless ASAP
    #
    if (not options.show_gui) or options.write_schema_and_exit:
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        # What's there to do but run if you're running headless?
        # Might want to change later if there's some headless setup
        options.run_pipeline = True

    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)

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

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

    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write(
                "CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n"
            )
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write(
                "    <message> - the message displayed inside the message box\n"
            )
            sys.stderr.write(
                "    <pipeline-or-project> - the path to the pipeline or project file to modify\n"
            )
            return
        caption = args[0]
        message = args[1]
        path = args[2]

        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurement as cpmeas
            m = cpmeas.Measurements(filename=path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
                        ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory, globally=False)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory,
                                        globally=False)
    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cpprefs.set_temporary_directory(options.temp_dir, globally=False)
    if not options.allow_schema_write:
        cpprefs.set_allow_schema_write(False)
    #
    # After the crucial preferences are established, we can start the VM
    #
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    #
    # Not so crucial preferences...
    #
    if options.image_set_file is not None:
        cpprefs.set_image_set_file(options.image_set_file)
    try:
        # ---------------------------------------
        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.output_html:
            from cellprofiler.gui.html.manual import generate_html
            webpage_path = options.output_directory if options.output_directory else None
            generate_html(webpage_path)
            return
        if options.print_measurements:
            print_measurements(options)
            return
        if not hasattr(sys, "frozen") and options.code_statistics:
            print_code_statistics()
            return
        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)
            return
        #
        # ------------------------------------------
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.gui.app import App
            from cellprofiler.workspace import is_workspace_file

            if options.pipeline_filename:
                if 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
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None

            app = App(0,
                      workspace_path=workspace_path,
                      pipeline_path=pipeline_path)

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

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" %
                          (version_string, version_number))

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

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

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()
            app.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py",
                           exc_info=True)
        exit_code = -1
Esempio n. 6
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i+1:
                cpprefs.set_ij_plugin_directory(args[i+1])
                break
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.aw_parse_args()
        cellprofiler.analysis_worker.main()
        sys.exit(0)
        
    options, args = parse_args(args)
    if options.jvm_heap_size != None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)
    
    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return
    
    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return
    
    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return
        
    if options.run_ilastik:
        run_ilastik()
        return
    
    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write("CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n")
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write("    <message> - the message displayed inside the message box\n")
            sys.stderr.write("    <pipeline-or-project> - the path to the pipeline or project file to modify\n")
            return
        caption = args[0]
        message = args[1]
        path = args[2]
        
        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurements as cpmeas
            m = cpmeas.Measurements(
                filename = path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
            ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return
    
    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')
    
    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(options.overwrite_external_dependencies)
    
    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return
    
    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return
    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            from cellprofiler.workspace import is_workspace_file
            show_splashbox = (options.pipeline_filename is None and
                              (not options.new_project) and
                              options.show_splashbox)
            
            if options.pipeline_filename:
                if 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
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None
            App = CellProfilerApp(
                0, 
                check_for_new_version = (options.pipeline_filename is None),
                show_splashbox = show_splashbox,
                workspace_path = workspace_path,
                pipeline_path = pipeline_path)
    
        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup 
            options.run_pipeline = True
    
            
        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            if not os.path.exists(options.temp_dir):
                os.makedirs(options.temp_dir)
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)
            
        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))
    
        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")
    
        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)
        
        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)
    
        if options.show_gui:
            if options.run_pipeline:
                App.frame.pipeline_controller.do_analyze_images()
            App.MainLoop()
            return
        
        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        raise
Esempio n. 7
0
# 
# Please see the AUTHORS file for credits.
#
# Website: http://www.cellprofiler.org
#
__version__="$Revision$"

import numpy as np
from StringIO import StringIO
import os
import sys
import unittest

import cellprofiler.preferences as cpprefs
cpprefs.set_headless()
cpprefs.set_ij_plugin_directory(os.path.split(__file__)[0])

import cellprofiler.cpimage as cpi
import cellprofiler.objects as cpo
import cellprofiler.measurements as cpm
import cellprofiler.pipeline as cpp
import cellprofiler.settings as cps
import cellprofiler.workspace as cpw

import cellprofiler.modules.run_imagej as R

run_tests = (sys.platform.startswith("win"))

INPUT_IMAGE_NAME = "inputimage"
OUTPUT_IMAGE_NAME = "outputimage"
Esempio n. 8
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.main()
        sys.exit(0)

    options, args = parse_args(args)
    set_log_level(options)

    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return

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

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

    if options.run_ilastik:
        run_ilastik()
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(
            options.overwrite_external_dependencies)

    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return

    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return

    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            show_splashbox = (options.pipeline_filename is None
                              and options.workspace_filename is None
                              and (not options.new_workspace)
                              and options.show_splashbox)
            if options.workspace_filename:
                workspace_path = os.path.expanduser(options.workspace_filename)
            elif options.new_workspace:
                workspace_path = False
            else:
                workspace_path = None
            App = CellProfilerApp(
                0,
                check_for_new_version=(options.pipeline_filename is None),
                show_splashbox=show_splashbox,
                workspace_path=workspace_path)

        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup
            options.run_pipeline = True

        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" %
                          (version_string, version_number))

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

        if options.output_directory:
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            import cellprofiler.gui.cpframe as cpgframe
            if options.pipeline_filename:
                pipeline_path = os.path.expanduser(options.pipeline_filename)
                try:
                    App.frame.pipeline.load(pipeline_path)
                    if options.run_pipeline:
                        App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
                except:
                    import wx
                    wx.MessageBox(
                        'CellProfiler was unable to load the pipeline file, "%s"'
                        % options.pipeline_filename,
                        "Error loading pipeline",
                        style=wx.OK | wx.ICON_ERROR)
                    logging.root.error("Unable to load pipeline",
                                       exc_info=True)
            App.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py",
                           exc_info=True)
        raise
Esempio n. 9
0
            if isinstance(event, cpp.LoadExceptionEvent):
                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 ---"
    
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
    if options.data_file is not None:
        cpprefs.set_data_file(os.path.abspath(options.data_file))
        
    from cellprofiler.utilities.version import version_string, version_number
    logging.root.info("Version: %s / %d" % (version_string, version_number))

    if options.output_html:
        sys.exit(0) 
    
    if options.run_pipeline and not (options.pipeline_filename or options.worker_mode_URL):
        raise ValueError("You must specify a pipeline filename to run")
    
    if not options.first_image_set is None:
        if not options.first_image_set.isdigit():
            raise ValueError("The --first-image-set option takes a numeric argument")
Esempio n. 10
0
                                 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 ---"

    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
    if options.data_file is not None:
        cpprefs.set_data_file(os.path.abspath(options.data_file))

    from cellprofiler.utilities.version import version_string, version_number
    logging.root.info("Version: %s / %d" % (version_string, version_number))

    if options.output_html:
        sys.exit(0)

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

    if not options.first_image_set is None:
        if not options.first_image_set.isdigit():
Esempio n. 11
0
def main(args=None):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if args is None:
        args = sys.argv
    import cellprofiler.preferences as cpprefs
    cpprefs.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]):
        #
        # Go headless ASAP
        #
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i + 1:
                cpprefs.set_ij_plugin_directory(args[i + 1])
                break
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.aw_parse_args()
        cellprofiler.analysis_worker.main()
        sys.exit(exit_code)

    options, args = parse_args(args)
    if options.print_version:
        from cellprofiler.utilities.version import \
            dotted_version, version_string, git_hash, version_number
        print "CellProfiler %s" % dotted_version
        print "Git %s" % git_hash
        print "Version %s" % version_number
        print "Built %s" % version_string.split(" ")[0]
        sys.exit(exit_code)
    #
    # Important to go headless ASAP
    #
    if (not options.show_gui) or options.write_schema_and_exit:
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        # What's there to do but run if you're running headless?
        # Might want to change later if there's some headless setup
        options.run_pipeline = True

    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)

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

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

    if options.run_ilastik:
        run_ilastik()
        return

    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write("CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n")
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write("    <message> - the message displayed inside the message box\n")
            sys.stderr.write("    <pipeline-or-project> - the path to the pipeline or project file to modify\n")
            return
        caption = args[0]
        message = args[1]
        path = args[2]

        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurements as cpmeas
            m = cpmeas.Measurements(
                    filename=path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
                        ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory,
                                     globally=False)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory,
                                        globally=False)
    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cpprefs.set_temporary_directory(options.temp_dir, globally=False)
    if not options.allow_schema_write:
        cpprefs.set_allow_schema_write(False)
    #
    # After the crucial preferences are established, we can start the VM
    #
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    #
    # Not so crucial preferences...
    #
    if options.image_set_file is not None:
        cpprefs.set_image_set_file(options.image_set_file)
    try:
        # ---------------------------------------
        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.output_html:
            from cellprofiler.gui.html.manual import generate_html
            webpage_path = options.output_directory if options.output_directory else None
            generate_html(webpage_path)
            return
        if options.print_measurements:
            print_measurements(options)
            return
        if not hasattr(sys, "frozen") and options.code_statistics:
            print_code_statistics()
            return
        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)
            return
        #
        # ------------------------------------------
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.gui.app import App
            from cellprofiler.workspace import is_workspace_file

            if options.pipeline_filename:
                if 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
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None

            app = App(0, workspace_path=workspace_path, pipeline_path=pipeline_path)

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

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))

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

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

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()
            app.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        exit_code = -1
Esempio n. 12
0
#
# Please see the AUTHORS file for credits.
#
# Website: http://www.cellprofiler.org
#
__version__ = "$Revision$"

import numpy as np
from StringIO import StringIO
import os
import sys
import unittest

import cellprofiler.preferences as cpprefs
cpprefs.set_headless()
cpprefs.set_ij_plugin_directory(os.path.split(__file__)[0])
cpprefs.set_ij_version(cpprefs.IJ_1)

import cellprofiler.cpimage as cpi
import cellprofiler.objects as cpo
import cellprofiler.measurements as cpm
import cellprofiler.pipeline as cpp
import cellprofiler.settings as cps
import cellprofiler.workspace as cpw

import cellprofiler.modules.run_imagej as R
from bioformats import USE_IJ2

run_tests = (sys.platform.startswith("win") and not USE_IJ2)

INPUT_IMAGE_NAME = "inputimage"
Esempio n. 13
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.main()
        sys.exit(0)
        
    options, args = parse_args(args)
    set_log_level(options)
    
    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return
    
    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return
    
    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return
        
    if options.run_ilastik:
        run_ilastik()
        return
    
    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')
    
    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(options.overwrite_external_dependencies)
    
    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return
    
    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return
    
    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            show_splashbox = (options.pipeline_filename is None and
                              options.workspace_filename is None and
                              (not options.new_workspace) and
                              options.show_splashbox)
            if options.workspace_filename:
                workspace_path = os.path.expanduser(options.workspace_filename)
            elif options.new_workspace:
                workspace_path = False
            else:
                workspace_path = None
            App = CellProfilerApp(
                0, 
                check_for_new_version = (options.pipeline_filename is None),
                show_splashbox = show_splashbox,
                workspace_path = workspace_path)
    
        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup 
            options.run_pipeline = True
    
            
        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)
            
        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))
    
        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")
    
        if options.output_directory:
            cpprefs.set_default_output_directory(options.output_directory)
        
        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)
    
        if options.show_gui:
            import cellprofiler.gui.cpframe as cpgframe
            if options.pipeline_filename:
                pipeline_path = os.path.expanduser(options.pipeline_filename)
                try:
                    App.frame.pipeline.load(pipeline_path)
                    if options.run_pipeline:
                        App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
                except:
                    import wx
                    wx.MessageBox(
                        'CellProfiler was unable to load the pipeline file, "%s"' %
                        options.pipeline_filename, "Error loading pipeline",
                        style = wx.OK | wx.ICON_ERROR)
                    logging.root.error("Unable to load pipeline", exc_info=True)
            App.MainLoop()
            return
        
        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        raise