Esempio n. 1
0
 def finalize(self, result):
     try:
         if self.temp_images is not None:
             import shutil
             shutil.rmtree(self.temp_images)
         if self.temp_exampleimages is not None:
             import shutil
             shutil.rmtree(self.temp_exampleimages)
     except:
         pass
     from cellprofiler.utilities.cpjvm import cp_stop_vm
     try:
         import wx
         app = wx.GetApp()
         if app is not None:
             app.ExitMainLoop()
             app.MainLoop()
         cp_stop_vm(kill=False)
     except:
         logging.root.warn("Failed to shut down AWT", exc_info=1)
     try:
         from ilastik.core.jobMachine import GLOBAL_WM
         GLOBAL_WM.stopWorkers()
     except:
         logging.root.warn("Failed to stop Ilastik")
     try:
         from cellprofiler.utilities.zmqrequest import join_to_the_boundary
         join_to_the_boundary()
     except:
         logging.root.warn("Failed to stop zmq boundary")
Esempio n. 2
0
    def begin(self):
        #
        # We monkey-patch javabridge.start_vm here in order to
        # set up the ImageJ event bus (actually 
        # org.bushe.swing.event.ThreadSafeEventService) to not start
        # its cleanup thread which semi-buggy hangs around forever
        # and prevents Java from exiting.
        #
        def patch_start_vm(*args, **kwargs):
            jvm_args = list(args[0]) +  [
                "-Dloci.bioformats.loaded=true",
                "-Djava.util.prefs.PreferencesFactory="+
                "org.cellprofiler.headlesspreferences"+
                ".HeadlessPreferencesFactory"]
            #
            # Find the ij1patcher
            #
            if hasattr(sys, 'frozen') and sys.platform == 'win32':
                root = os.path.dirname(sys.argv[0])
            else:
                root = os.path.dirname(__file__)
            jardir = os.path.join(root, "imagej", "jars")
            patchers = sorted([
                    x for x in os.listdir(jardir)
                    if x.startswith("ij1-patcher") and x.endswith(".jar")])
            if len(patchers) > 0:
                jvm_args.append(
                    "-javaagent:%s=init" % os.path.join(jardir, patchers[-1]))
            result = start_vm(jvm_args, *args[1:], **kwargs)
            if javabridge.get_env() is not None:
                try:
                    event_service_cls = javabridge.JClassWrapper(
                        "org.bushe.swing.event.ThreadSafeEventService")
                    event_service_cls.CLEANUP_PERIOD_MS_DEFAULT = None
                except:
                    pass
            return result
        patch_start_vm.func_globals["start_vm"] = javabridge.start_vm
        javabridge.start_vm = patch_start_vm
        if "CP_EXAMPLEIMAGES" in os.environ:
            self.temp_exampleimages = None
        else:
            self.temp_exampleimages = tempfile.mkdtemp(prefix="cpexampleimages")

        if "CP_TEMPIMAGES" in os.environ:
            self.temp_images = None
        else:
            self.temp_images = tempfile.mkdtemp(prefix="cptempimages")
        #
        # Set up matplotlib for WXAgg if in frozen mode
        # otherwise it looks for TK which isn't there
        #
        if hasattr(sys, 'frozen'):
            import matplotlib
            matplotlib.use("WXAgg")
        try:
            from ilastik.core.jobMachine import GLOBAL_WM
            GLOBAL_WM.set_thread_count(1)
        except:
            pass
Esempio n. 3
0
 def finalize(self, result):
     try:
         if self.temp_images is not None:
             import shutil
             shutil.rmtree(self.temp_images)
         if self.temp_exampleimages is not None:
             import shutil
             shutil.rmtree(self.temp_exampleimages)
     except:
         pass
     from cellprofiler.utilities.cpjvm import cp_stop_vm
     try:
         import wx
         app = wx.GetApp()
         if app is not None:
             app.ExitMainLoop()
             app.MainLoop()
         cp_stop_vm(kill=False)
     except:
         logging.root.warn("Failed to shut down AWT", exc_info=1)
     try:
         from ilastik.core.jobMachine import GLOBAL_WM
         GLOBAL_WM.stopWorkers()
     except:
         logging.root.warn("Failed to stop Ilastik")
     try:
         from cellprofiler.utilities.zmqrequest import join_to_the_boundary
         join_to_the_boundary()
     except:
         logging.root.warn("Failed to stop zmq boundary")
Esempio n. 4
0
 def finalize(self, result):
     try:
         javabridge.deactivate_awt()
         import imagej.imagej2
         if imagej.imagej2.the_imagej_context is not None:
             script = """
             new java.lang.Runnable () {
               run: function() {
                 ctx.getContext().dispose();
               }
             }"""
             runnable = javabridge.run_script(
                 script, dict(ctx=imagej.imagej2.the_imagej_context))
             javabridge.execute_runnable_in_main_thread(runnable, True)
             imagej.imagej2.the_imagej_context = None
             javabridge.static_call("java/lang/System", "gc", "()V")
     except:
         pass
     try:
         from ilastik.core.jobMachine import GLOBAL_WM
         GLOBAL_WM.stopWorkers()
     except:
         logging.root.warn("Failed to stop Ilastik")
     try:
         from cellprofiler.utilities.zmqrequest import join_to_the_boundary
         join_to_the_boundary()
     except:
         logging.root.warn("Failed to stop zmq boundary")
Esempio n. 5
0
 def tearDownClass(cls):
     try:
         from ilastik.core.jobMachine import GLOBAL_WM
         GLOBAL_WM.stopWorkers()
     except:
         pass
     cls.notify_pub_socket.close()
Esempio n. 6
0
def teardown_package():
    '''Shut down the Ilastik threads if they are up'''
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()

    except:
        pass
Esempio n. 7
0
def teardown_package():
    '''Shut down the Ilastik threads if they are up'''
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
        
    except:
        pass
Esempio n. 8
0
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
        J.javabridge.mac_run_loop_init()
    
    # Importing bioformats starts the JVM
    import bioformats
    # 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:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
Esempio n. 9
0
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
        J.javabridge.mac_run_loop_init()

    # Importing bioformats starts the JVM
    import bioformats
    # 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:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
Esempio n. 10
0
def shutdown_cellprofiler():
    '''Oh sadness and so many threads that won't die...
    
    '''
    try:
        import javabridge
        javabridge.kill_vm()
    except:
        pass
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        pass
Esempio n. 11
0
def shutdown_cellprofiler():
    '''Oh sadness and so many threads that won't die...
    
    '''
    try:
        import javabridge
        javabridge.kill_vm()
    except:
        pass
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        pass
Esempio n. 12
0
def stop_cellprofiler():
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        logging.root.warn("Failed to stop Ilastik")
    try:
        from cellprofiler.utilities.zmqrequest import join_to_the_boundary
        join_to_the_boundary()
    except:
        logging.root.warn("Failed to stop zmq boundary", exc_info=1)
    try:
        from cellprofiler.utilities.cpjvm import cp_stop_vm
        cp_stop_vm()
    except:
        logging.root.warn("Failed to stop the JVM", exc_info=1)
Esempio n. 13
0
def stop_cellprofiler():
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        logging.root.warn("Failed to stop Ilastik")
    try:
        from cellprofiler.utilities.zmqrequest import join_to_the_boundary
        join_to_the_boundary()
    except:
        logging.root.warn("Failed to stop zmq boundary", exc_info=1)
    try:
        from cellprofiler.utilities.cpjvm import cp_stop_vm
        cp_stop_vm()
    except:
        logging.root.warn("Failed to stop the JVM", exc_info=1)
Esempio n. 14
0
def main():
    # XXX - move all this to a class
    
    #
    # 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
        J.javabridge.mac_run_loop_init()
    
    # Importing bioformats starts the JVM
    import bioformats
    # 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:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
Esempio n. 15
0
    def begin(self):
        #
        # We monkey-patch javabridge.start_vm here in order to
        # set up the ImageJ event bus (actually
        # org.bushe.swing.event.ThreadSafeEventService) to not start
        # its cleanup thread which semi-buggy hangs around forever
        # and prevents Java from exiting.
        #
        def patch_start_vm(*args, **kwargs):
            result = start_vm(*args, **kwargs)
            if javabridge.get_env() is not None:
                try:
                    event_service_cls = javabridge.JClassWrapper(
                        "org.bushe.swing.event.ThreadSafeEventService")
                    event_service_cls.CLEANUP_PERIOD_MS_DEFAULT = None
                except:
                    pass
            return result

        patch_start_vm.func_globals["start_vm"] = javabridge.start_vm
        javabridge.start_vm = patch_start_vm
        if "CP_EXAMPLEIMAGES" in os.environ:
            self.temp_exampleimages = None
        else:
            self.temp_exampleimages = tempfile.mkdtemp(
                prefix="cpexampleimages")

        if "CP_TEMPIMAGES" in os.environ:
            self.temp_images = None
        else:
            self.temp_images = tempfile.mkdtemp(prefix="cptempimages")
        #
        # Set up matplotlib for WXAgg if in frozen mode
        # otherwise it looks for TK which isn't there
        #
        if hasattr(sys, 'frozen'):
            import matplotlib
            matplotlib.use("WXAgg")
        try:
            from ilastik.core.jobMachine import GLOBAL_WM
            GLOBAL_WM.set_thread_count(1)
        except:
            pass
Esempio n. 16
0
def main():
    # XXX - move all this to a class
    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=logging.INFO)
    options, args = parser.parse_args()
    logging.root.setLevel(options.log_level)

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    # Start the deadman switch thread.
    notify_started_cv = threading.Condition()
    start_daemon_thread(target=exit_on_stdin_close, 
                        name="exit_on_stdin_close",
                        args = (notify_started_cv, ))
    with notify_started_cv:
        notify_started_cv.wait()

    try:
        with AnalysisWorker(options.work_announce_address) as worker:
            worker.run()
    except CancelledException:
        logger.debug("Exiting after cancellation")
    finally:
        #
        # 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:
            J.kill_vm()
        except:
            logger.warn("Failed to stop the Java VM")
Esempio n. 17
0
    def begin(self):
        #
        # We monkey-patch javabridge.start_vm here in order to
        # set up the ImageJ event bus (actually 
        # org.bushe.swing.event.ThreadSafeEventService) to not start
        # its cleanup thread which semi-buggy hangs around forever
        # and prevents Java from exiting.
        #
        def patch_start_vm(*args, **kwargs):
            result = start_vm(*args, **kwargs)
            if javabridge.get_env() is not None:
                try:
                    event_service_cls = javabridge.JClassWrapper(
                        "org.bushe.swing.event.ThreadSafeEventService")
                    event_service_cls.CLEANUP_PERIOD_MS_DEFAULT = None
                except:
                    pass
            return result
        patch_start_vm.func_globals["start_vm"] = javabridge.start_vm
        javabridge.start_vm = patch_start_vm
        if "CP_EXAMPLEIMAGES" in os.environ:
            self.temp_exampleimages = None
        else:
            self.temp_exampleimages = tempfile.mkdtemp(prefix="cpexampleimages")

        if "CP_TEMPIMAGES" in os.environ:
            self.temp_images = None
        else:
            self.temp_images = tempfile.mkdtemp(prefix="cptempimages")
        #
        # Set up matplotlib for WXAgg if in frozen mode
        # otherwise it looks for TK which isn't there
        #
        if hasattr(sys, 'frozen'):
            import matplotlib
            matplotlib.use("WXAgg")
        try:
            from ilastik.core.jobMachine import GLOBAL_WM
            GLOBAL_WM.set_thread_count(1)
        except:
            pass
Esempio n. 18
0
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")
Esempio n. 19
0
    def begin(self):
        #
        # We monkey-patch javabridge.start_vm here in order to
        # set up the ImageJ event bus (actually
        # org.bushe.swing.event.ThreadSafeEventService) to not start
        # its cleanup thread which semi-buggy hangs around forever
        # and prevents Java from exiting.
        #
        def patch_start_vm(*args, **kwargs):
            jvm_args = list(args[0]) + [
                "-Dloci.bioformats.loaded=true",
                "-Djava.util.prefs.PreferencesFactory=" +
                "org.cellprofiler.headlesspreferences" +
                ".HeadlessPreferencesFactory"
            ]
            add_logback_xml_arg(jvm_args)
            #
            # Find the ij1patcher
            #
            if hasattr(sys, 'frozen') and sys.platform == 'win32':
                root = os.path.dirname(sys.argv[0])
            else:
                root = os.path.dirname(__file__)
            jardir = os.path.join(root, "imagej", "jars")
            patchers = sorted([
                x for x in os.listdir(jardir)
                if x.startswith("ij1-patcher") and x.endswith(".jar")
            ])
            if len(patchers) > 0:
                jvm_args.append("-javaagent:%s=init" %
                                os.path.join(jardir, patchers[-1]))
            result = start_vm(jvm_args, *args[1:], **kwargs)
            if javabridge.get_env() is not None:
                try:
                    event_service_cls = javabridge.JClassWrapper(
                        "org.bushe.swing.event.ThreadSafeEventService")
                    event_service_cls.CLEANUP_PERIOD_MS_DEFAULT = None
                except:
                    pass
            return result

        patch_start_vm.func_globals["start_vm"] = javabridge.start_vm
        javabridge.start_vm = patch_start_vm
        if "CP_EXAMPLEIMAGES" in os.environ:
            self.temp_exampleimages = None
        else:
            self.temp_exampleimages = tempfile.mkdtemp(
                prefix="cpexampleimages")
            os.environ["CP_EXAMPLEIMAGES"] = self.temp_exampleimages

        if "CP_TEMPIMAGES" in os.environ:
            self.temp_images = None
        else:
            self.temp_images = tempfile.mkdtemp(prefix="cptempimages")
        #
        # Set up matplotlib for WXAgg if in frozen mode
        # otherwise it looks for TK which isn't there
        #
        if hasattr(sys, 'frozen'):
            import matplotlib
            matplotlib.use("WXAgg")
        try:
            from ilastik.core.jobMachine import GLOBAL_WM
            GLOBAL_WM.set_thread_count(1)
        except:
            pass
Esempio n. 20
0
def main():
    # XXX - move all this to a class
    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=logging.INFO)
    options, args = parser.parse_args()
    logging.root.setLevel(options.log_level)

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    #
    # 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
        J.javabridge.mac_run_loop_init()

    # Importing bioformats starts the JVM
    import bioformats

    # 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(options.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:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
Esempio n. 21
0
def run_pipeline_headless(options, args):
    '''Run a CellProfiler pipeline in headless mode'''
    #
    # Start Ilastik's workers
    #
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.set_thread_count(1)
    except:
        logging.root.warn("Failed to stop Ilastik")

    if sys.platform == 'darwin':
        if options.start_awt:
            import bioformats
            from javabridge import activate_awt
            activate_awt()

    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")
        else:
            image_set_start = int(options.first_image_set)
    else:
        image_set_start = None

    image_set_numbers = None
    if not options.last_image_set is 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 = np.arange(1, image_set_end + 1)
            else:
                image_set_numbers = np.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)
    from cellprofiler.pipeline import Pipeline, EXIT_STATUS, M_PIPELINE
    import cellprofiler.measurements as cpmeas
    import cellprofiler.preferences as cpprefs
    pipeline = Pipeline()
    initial_measurements = None
    try:
        if h5py.is_hdf5(options.pipeline_filename):
            initial_measurements = cpmeas.load_measurements(
                options.pipeline_filename, image_numbers=image_set_numbers)
    except:
        logging.root.info("Failed to load measurements from pipeline")
    if initial_measurements is not None:
        pipeline_text = \
            initial_measurements.get_experiment_measurement(
                M_PIPELINE)
        pipeline_text = pipeline_text.encode('us-ascii')
        pipeline.load(StringIO(pipeline_text))
        if not pipeline.in_batch_mode():
            #
            # Need file list in order to call prepare_run
            #
            from cellprofiler.utilities.hdf5_dict import HDF5FileList
            with h5py.File(options.pipeline_filename, "r") as src:
                if HDF5FileList.has_file_list(src):
                    HDF5FileList.copy(src,
                                      initial_measurements.hdf5_dict.hdf5_file)
    else:
        pipeline.load(options.pipeline_filename)
    if options.groups is not None:
        kvs = [x.split('=') for x in options.groups.split(',')]
        groups = dict(kvs)
    else:
        groups = None
    file_list = cpprefs.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(cpmeas.EXPERIMENT, EXIT_STATUS)):
            done_text = measurements.get_experiment_measurement(EXIT_STATUS)
        else:
            done_text = "Failure"
        fd = open(options.done_file, "wt")
        fd.write("%s\n" % done_text)
        fd.close()
    if measurements is not None:
        measurements.close()
Esempio n. 22
0
            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
    
    finally:
        if __name__ == "__main__":
            try:
                from ilastik.core.jobMachine import GLOBAL_WM
                GLOBAL_WM.stopWorkers()
            except:
                logging.root.warn("Failed to stop Ilastik")
            try:
                from cellprofiler.utilities.zmqrequest import join_to_the_boundary
                join_to_the_boundary()
            except:
                logging.root.warn("Failed to stop zmq boundary")
            try:
                from cellprofiler.utilities.jutil import kill_vm
                kill_vm()
            except:
                logging.root.warn("Failed to stop the JVM")
            os._exit(0)

def parse_args(args):
Esempio n. 23
0
def main():
    # XXX - move all this to a class
    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=logging.INFO)
    options, args = parser.parse_args()
    logging.root.setLevel(options.log_level)

    if not options.work_announce_address:
        parser.print_help()
        sys.exit(1)
    #
    # 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
        J.javabridge.mac_run_loop_init()
     
    # Importing bioformats starts the JVM
    import bioformats
    
    # 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(options.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:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
Esempio n. 24
0
def run_pipeline_headless(options, args):
    '''Run a CellProfiler pipeline in headless mode'''
    #
    # Start Ilastik's workers
    #
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.set_thread_count(1)
    except:
        logging.root.warn("Failed to stop Ilastik")
    
    if sys.platform == 'darwin':
        if options.start_awt:
            import bioformats
            from javabridge import activate_awt
            activate_awt()
        
    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")
        else:
            image_set_start = int(options.first_image_set)
    else:
        image_set_start = None
    
    image_set_numbers = None
    if not options.last_image_set is 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 = np.arange(1, image_set_end+1)
            else:
                image_set_numbers = np.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)
    from cellprofiler.pipeline import Pipeline, EXIT_STATUS, M_PIPELINE
    import cellprofiler.measurements as cpmeas
    import cellprofiler.preferences as cpprefs
    pipeline = Pipeline()
    initial_measurements = None
    try:
        if h5py.is_hdf5(options.pipeline_filename):
            initial_measurements = cpmeas.load_measurements(
                options.pipeline_filename,
                image_numbers=image_set_numbers)
    except:
        logging.root.info("Failed to load measurements from pipeline")
    if initial_measurements is not None:
        pipeline_text = \
            initial_measurements.get_experiment_measurement(
                M_PIPELINE)
        pipeline_text = pipeline_text.encode('us-ascii')
        pipeline.load(StringIO(pipeline_text))
        if not pipeline.in_batch_mode():
            #
            # Need file list in order to call prepare_run
            #
            from cellprofiler.utilities.hdf5_dict import HDF5FileList
            with h5py.File(options.pipeline_filename, "r") as src:
                if HDF5FileList.has_file_list(src):
                    HDF5FileList.copy(
                        src, initial_measurements.hdf5_dict.hdf5_file)
    else:
        pipeline.load(options.pipeline_filename)
    if options.groups is not None:
        kvs = [x.split('=') for x in options.groups.split(',')]
        groups = dict(kvs)
    else:
        groups = None
    file_list = cpprefs.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(cpmeas.EXPERIMENT, EXIT_STATUS)):
            done_text = measurements.get_experiment_measurement(EXIT_STATUS)
        else:
            done_text = "Failure"
        fd = open(options.done_file, "wt")
        fd.write("%s\n"%done_text)
        fd.close()
    if measurements is not None:
        measurements.close()
Esempio n. 25
0
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)