def __init__(self, *args, **kwargs): self.abort_initialization = False self.frame = None self.pipeline_path = kwargs.pop("pipeline_path", None) self.workspace_path = kwargs.pop("workspace_path", None) start_java() super(App, self).__init__(*args, **kwargs)
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_core.utilities.java import start_java, stop_java start_java() 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 Worker(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: stop_java() except: logging.warning("Failed to stop the JVM", exc_info=True)
def main(args=None): """Run CellProfiler args - command-line arguments, e.g., sys.argv """ if args is None: args = sys.argv 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]): set_headless() aw_parse_args() main() return exit_code options, args = parse_args(args) if options.temp_dir is not None: if not os.path.exists(options.temp_dir): os.makedirs(options.temp_dir) set_temporary_directory(options.temp_dir, globally=False) temp_dir = get_temporary_directory() to_clean = [] if options.pipeline_filename: o = urllib.parse.urlparse(options.pipeline_filename) if o[0] in ("ftp", "http", "https"): from urllib.request import urlopen temp_pipe_file = tempfile.NamedTemporaryFile(mode="w+b", suffix=".cppipe", dir=temp_dir, delete=False) downloaded_pipeline = urlopen(options.pipeline_filename) for line in downloaded_pipeline: temp_pipe_file.write(line) options.pipeline_filename = temp_pipe_file.name to_clean.append(os.path.join(temp_dir, temp_pipe_file.name)) if options.image_set_file: o = urllib.parse.urlparse(options.image_set_file) if o[0] in ("ftp", "http", "https"): from urllib.request import urlopen temp_set_file = tempfile.NamedTemporaryFile(mode="w+b", suffix=".csv", dir=temp_dir, delete=False) downloaded_set_csv = urlopen(options.image_set_file) for line in downloaded_set_csv: temp_set_file.write(line) options.image_set_file = temp_set_file.name to_clean.append(os.path.join(temp_dir, temp_set_file.name)) if options.data_file: o = urllib.parse.urlparse(options.data_file) if o[0] in ("ftp", "http", "https"): from urllib.request import urlopen temp_data_file = tempfile.NamedTemporaryFile(mode="w+b", suffix=".csv", dir=temp_dir, delete=False) downloaded_data_csv = urlopen(options.data_file) for line in downloaded_data_csv: temp_data_file.write(line) options.data_file = temp_data_file.name to_clean.append(os.path.join(temp_dir, temp_data_file.name)) if options.print_version: __version__(exit_code) if (not options.show_gui) or options.write_schema_and_exit: set_headless() options.run_pipeline = True if options.batch_commands_file: set_headless() options.run_pipeline = False options.show_gui = False set_log_level(options) if options.print_groups_file is not None: print_groups(options.print_groups_file) if options.batch_commands_file is not None: try: nr_per_batch = int(options.images_per_batch) except ValueError: logging.warning( "non-integer argument to --images-per-batch. Defaulting to 1.") nr_per_batch = 1 get_batch_commands(options.batch_commands_file, nr_per_batch) if options.omero_credentials is not None: set_omero_credentials_from_string(options.omero_credentials) if options.plugins_directory is not None: set_plugin_directory(options.plugins_directory, globally=False) if not options.allow_schema_write: set_allow_schema_write(False) if options.output_directory: if not os.path.exists(options.output_directory): os.makedirs(options.output_directory) set_default_output_directory(options.output_directory) if options.image_directory: set_default_image_directory(options.image_directory) if options.run_pipeline and not options.pipeline_filename: raise ValueError("You must specify a pipeline filename to run") if options.data_file is not None: set_data_file(os.path.abspath(options.data_file)) try: if not options.show_gui: start_java() if options.image_set_file is not None: set_image_set_file(options.image_set_file) # # Handle command-line tasks that that need to load the modules to run # if options.print_measurements: print_measurements(options) if options.write_schema_and_exit: write_schema(options.pipeline_filename) if options.show_gui: matplotlib.use("WXAgg") import cellprofiler.gui.app if options.pipeline_filename: if is_workspace_file(options.pipeline_filename): workspace_path = os.path.expanduser( options.pipeline_filename) pipeline_path = None else: pipeline_path = os.path.expanduser( options.pipeline_filename) workspace_path = None else: workspace_path = None pipeline_path = None app = cellprofiler.gui.app.App(0, workspace_path=workspace_path, pipeline_path=pipeline_path) if options.run_pipeline: app.frame.pipeline_controller.do_analyze_images() app.MainLoop() return elif options.run_pipeline: exit_code = run_pipeline_headless(options, args) finally: # Cleanup the temp files we made, if any if len(to_clean) > 0: for each_temp in to_clean: os.remove(each_temp) # If anything goes wrong during the startup sequence headlessly, the JVM needs # to be explicitly closed if not options.show_gui: stop_cellprofiler() return exit_code