def __init__(self, ctx):
     super(Mapper, self).__init__(ctx)
     self.img_set = []
     self.cwd = os.getcwd()
     self.pipeline_filename = ctx.job_conf.get(PIPELINE_BN)
     cp_start_vm()
     self.ctx = ctx
 def __init__(self, ctx):
     super(Mapper, self).__init__(ctx)
     self.img_set = []
     self.cwd = os.getcwd()
     self.pipeline_filename = ctx.job_conf.get(PIPELINE_BN)
     cp_start_vm()
     self.ctx = ctx
Exemple #3
0
def main():
    #
    # For OS/X set up the UI elements that users expect from
    # an app.
    #
    if sys.platform == "darwin":
        import os.path

        icon_path = pkg_resources.resource_filename(
            "cellprofiler", os.path.join("data", "icons", "CellProfiler.png"))
        os.environ["APP_NAME_%d" % os.getpid()] = "CellProfilerWorker"
        os.environ["APP_ICON_%d" % os.getpid()] = icon_path

    # Start the JVM
    from cellprofiler.utilities.cpjvm import cp_start_vm, cp_stop_vm

    cp_start_vm()

    deadman_start_socket = the_zmq_context.socket(zmq.PAIR)
    deadman_start_socket.bind(DEADMAN_START_ADDR)

    # Start the deadman switch thread.
    start_daemon_thread(target=exit_on_stdin_close, name="exit_on_stdin_close")
    deadman_start_socket.recv()
    deadman_start_socket.close()

    from cellprofiler.knime_bridge import KnimeBridgeServer

    with AnalysisWorker(work_announce_address) as worker:
        worker_thread = threading.Thread(target=worker.run,
                                         name="WorkerThread")
        worker_thread.setDaemon(True)
        worker_thread.start()
        with KnimeBridgeServer(the_zmq_context, knime_bridge_address,
                               NOTIFY_ADDR, NOTIFY_STOP):
            worker_thread.join()

    #
    # Shutdown - need to handle some global cleanup here
    #
    try:
        cp_stop_vm()
    except:
        logger.warn("Failed to stop the JVM", exc_info=1)
Exemple #4
0
def main():
    #
    # For OS/X set up the UI elements that users expect from
    # an app.
    #
    if sys.platform == "darwin":
        from cellprofiler.icons import get_builtin_images_path
        import os.path
        icon_path = pkg_resources.resource_filename("cellprofiler", os.path.join("data", "icons", "CellProfiler.png"))
        os.environ["APP_NAME_%d" % os.getpid()] = "CellProfilerWorker"
        os.environ["APP_ICON_%d" % os.getpid()] = icon_path

    # Start the JVM
    from cellprofiler.utilities.cpjvm import cp_start_vm, cp_stop_vm
    cp_start_vm()

    deadman_start_socket = the_zmq_context.socket(zmq.PAIR)
    deadman_start_socket.bind(DEADMAN_START_ADDR)

    # Start the deadman switch thread.
    start_daemon_thread(target=exit_on_stdin_close,
                        name="exit_on_stdin_close")
    deadman_start_socket.recv()
    deadman_start_socket.close()

    from cellprofiler.knime_bridge import KnimeBridgeServer
    with AnalysisWorker(work_announce_address) as worker:
        worker_thread = threading.Thread(target=worker.run,
                                         name="WorkerThread")
        worker_thread.setDaemon(True)
        worker_thread.start()
        with KnimeBridgeServer(the_zmq_context,
                               knime_bridge_address,
                               NOTIFY_ADDR, NOTIFY_STOP):
            worker_thread.join()

    #
    # Shutdown - need to handle some global cleanup here
    #
    try:
        cp_stop_vm()
    except:
        logger.warn("Failed to stop the JVM", exc_info=1)
Exemple #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
def main():
    #
    # For Windows build with Ilastik, look for site-packages
    # in order to find Ilastik sources.
    #
    if hasattr(sys, 'frozen') and sys.platform == "win32":
        root = os.path.split(sys.argv[0])[0]
        if len(root) == 0:
            root = os.curdir
        root = os.path.abspath(root)
        site_packages = os.path.join(root, 'site-packages').encode('utf-8')
        if os.path.exists(site_packages) and os.path.isdir(site_packages):
            import site
            site.addsitedir(site_packages)
    #
    # For OS/X set up the UI elements that users expect from
    # an app.
    #
    if sys.platform == "darwin":
        from cellprofiler.icons import get_builtin_images_path
        
        icon_path = os.path.join(get_builtin_images_path(), "CellProfilerIcon.png")
        os.environ["APP_NAME_%d" % os.getpid()] = "CellProfilerWorker"
        os.environ["APP_ICON_%d" % os.getpid()] = icon_path
    
    # Start the JVM
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    
    deadman_start_socket = the_zmq_context.socket(zmq.PAIR)
    deadman_start_socket.bind(DEADMAN_START_ADDR)
    
    # Start the deadman switch thread.
    start_daemon_thread(target=exit_on_stdin_close, 
                        name="exit_on_stdin_close")
    deadman_start_socket.recv()
    deadman_start_socket.close()
    
    from cellprofiler.knime_bridge import KnimeBridgeServer
    with AnalysisWorker(work_announce_address) as worker:
        worker_thread = threading.Thread(target = worker.run, 
                                         name="WorkerThread")
        worker_thread.setDaemon(True)
        worker_thread.start()
        with KnimeBridgeServer(the_zmq_context,
                               knime_bridge_address,
                               NOTIFY_ADDR, NOTIFY_STOP):
            enter_run_loop()
            worker_thread.join()
            
    #
    # Shutdown - need to handle some global cleanup here
    #
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        logger.warn("Failed to stop Ilastik")
    try:
        from imagej.imagej2 import allow_quit
        allow_quit()
    except:
        logger.warn("Failed to signal ImageJ to stop")
    try:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
                    image = image[:, :(megapicture.shape[1] - offs[1]), :]
                megapicture[offs[0]:(offs[0] + image.shape[0]),
                            offs[1]:(offs[1] + image.shape[1]), :] += image
        self.axes.cla()
        self.axes.imshow(megapicture)
        self.canvas.draw()
        self.navtoolbar.update()


if __name__ == "__main__":
    import os
    import re
    import bioformats
    import javabridge
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    app = wx.PySimpleApp(True)
    dlg = wx.Dialog(None,
                    size=(1024, 768),
                    style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
                    | wx.THICK_FRAME)
    data = PlateData()
    root = r"\\iodine-cifs\imaging_analysis\2007_09_24_BBBC_ImagingPlatform\Fibroblasts"
    paths = [
        pathname2url(os.path.join(root, filename))
        for filename in os.listdir(root) if filename.startswith("plate")
    ]
    filenames = []
    plates = []
    wells = []
    sites = []
                    offs[0] : (offs[0] + image.shape[0]), offs[1] : (offs[1] + image.shape[1]), :
                ] += image.astype(megapicture.dtype)
        self.axes.cla()
        self.axes.imshow(megapicture)
        self.canvas.draw()
        self.navtoolbar.update()


if __name__ == "__main__":
    import os
    import re
    import bioformats
    import javabridge
    from cellprofiler.utilities.cpjvm import cp_start_vm

    cp_start_vm()
    app = wx.PySimpleApp(True)
    dlg = wx.Dialog(None, size=(1024, 768), style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.THICK_FRAME)
    data = PlateData()
    root = r"\\iodine-cifs\imaging_analysis\2007_09_24_BBBC_ImagingPlatform\Fibroblasts"
    paths = [
        pathname2url(os.path.join(root, filename)) for filename in os.listdir(root) if filename.startswith("plate")
    ]
    filenames = []
    plates = []
    wells = []
    sites = []
    channels = []
    # example file name: HDFa030510P6hiP6loP20hiP20lo_A11_s1_w2E387A0AC-E9DE-42FA-8BBC-73F9BA938085.tif
    pattern = "^(?P<Plate>.+)_(?P<Well>[A-P][0-9]{2})_(?P<Site>s[0-9])_(?P<Channel>w[0-9])[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}.tif$"
    for path in paths:
Exemple #9
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    import cellprofiler.preferences as cpprefs
    cpprefs.set_awt_headless(True)
    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(0)
        
    if any([arg.startswith('--xml-test-file=') for arg in sys.argv]):
        import cpnose
        cpnose.main(*sys.argv)
        return
    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(0)
    #
    # 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 (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.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_stop_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.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)

        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)
        raise
def main():
    #
    # For Windows build with Ilastik, look for site-packages
    # in order to find Ilastik sources.
    #
    if hasattr(sys, 'frozen') and sys.platform == "win32":
        root = os.path.split(sys.argv[0])[0]
        if len(root) == 0:
            root = os.curdir
        root = os.path.abspath(root)
        site_packages = os.path.join(root, 'site-packages').encode('utf-8')
        if os.path.exists(site_packages) and os.path.isdir(site_packages):
            import site
            site.addsitedir(site_packages)
    #
    # For OS/X set up the UI elements that users expect from
    # an app.
    #
    if sys.platform == "darwin":
        from cellprofiler.icons import get_builtin_images_path

        icon_path = os.path.join(get_builtin_images_path(),
                                 "artwork/CellProfilerIcon.png")
        os.environ["APP_NAME_%d" % os.getpid()] = "CellProfilerWorker"
        os.environ["APP_ICON_%d" % os.getpid()] = icon_path

    # Start the JVM
    from cellprofiler.utilities.cpjvm import cp_start_vm, cp_stop_vm
    cp_start_vm()

    deadman_start_socket = the_zmq_context.socket(zmq.PAIR)
    deadman_start_socket.bind(DEADMAN_START_ADDR)

    # Start the deadman switch thread.
    start_daemon_thread(target=exit_on_stdin_close, name="exit_on_stdin_close")
    deadman_start_socket.recv()
    deadman_start_socket.close()
    # Limit Ilastik to one job thread.
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        try:
            GLOBAL_WM.set_thread_count(1)
        except:
            GLOBAL_WM.setThreadCount(1)
    except:
        pass

    from cellprofiler.knime_bridge import KnimeBridgeServer
    with AnalysisWorker(work_announce_address) as worker:
        worker_thread = threading.Thread(target=worker.run,
                                         name="WorkerThread")
        worker_thread.setDaemon(True)
        worker_thread.start()
        with KnimeBridgeServer(the_zmq_context, knime_bridge_address,
                               NOTIFY_ADDR, NOTIFY_STOP):
            enter_run_loop()
            worker_thread.join()

    #
    # Shutdown - need to handle some global cleanup here
    #
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        logger.warn("Failed to stop Ilastik")
    try:
        cp_stop_vm()
    except:
        logger.warn("Failed to stop the JVM", exc_info=1)
def main():
    #
    # For Windows build with Ilastik, look for site-packages
    # in order to find Ilastik sources.
    #
    if hasattr(sys, 'frozen') and sys.platform == "win32":
        root = os.path.split(sys.argv[0])[0]
        if len(root) == 0:
            root = os.curdir
        root = os.path.abspath(root)
        site_packages = os.path.join(root, 'site-packages').encode('utf-8')
        if os.path.exists(site_packages) and os.path.isdir(site_packages):
            import site
            site.addsitedir(site_packages)
    #
    # For OS/X set up the UI elements that users expect from
    # an app.
    #
    if sys.platform == "darwin":
        from cellprofiler.icons import get_builtin_images_path

        icon_path = os.path.join(get_builtin_images_path(),
                                 "CellProfilerIcon.png")
        os.environ["APP_NAME_%d" % os.getpid()] = "CellProfilerWorker"
        os.environ["APP_ICON_%d" % os.getpid()] = icon_path

    # Start the JVM
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()

    # Start the deadman switch thread.
    start_daemon_thread(target=exit_on_stdin_close, name="exit_on_stdin_close")
    with stdin_monitor_lock:
        while not stdin_monitor_started:
            stdin_monitor_cv.wait()

    with AnalysisWorker(work_announce_address) as worker:
        worker_thread = threading.Thread(target=worker.run,
                                         name="WorkerThread")
        worker_thread.setDaemon(True)
        worker_thread.start()
        print "Entering run loop"
        enter_run_loop()
        print "Exiting run loop"
        worker_thread.join()

    #
    # Shutdown - need to handle some global cleanup here
    #
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        logger.warn("Failed to stop Ilastik")
    try:
        from imagej.imagej2 import allow_quit
        allow_quit()
    except:
        logger.warn("Failed to signal ImageJ to stop")
    try:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")