def setUp(self): from cellprofiler_core.preferences import set_headless, set_temporary_directory set_headless() set_temporary_directory(tempfile.gettempdir()) self.old_import = builtins.__import__ builtins.__import__ = import_all_but_wx
def aw_parse_args(): """Parse the application arguments into setup parameters""" from cellprofiler_core.preferences import ( set_headless, set_awt_headless, set_plugin_directory, ) import optparse global work_announce_address global knime_bridge_address set_headless() set_awt_headless(True) 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( "--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"), ) parser.add_option( "--knime-bridge-address", dest="knime_bridge_address", help="Open up a port to handle the Knime bridge protocol", 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 or options.knime_bridge_address): parser.print_help() sys.exit(1) work_announce_address = options.work_announce_address knime_bridge_address = options.knime_bridge_address # # Set up the headless plugins directories before doing # anything so loading will get them # if options.plugins_directory is not None: set_plugin_directory(options.plugins_directory, globally=False) else: logging.warning("Plugins directory not set")
import cellprofiler_core.image as cpi import cellprofiler_core.object as cpo import cellprofiler_core.pipeline as cpp import cellprofiler_core.measurement as cpmeas from cellprofiler_core.utilities.core import modules as cpmodules from centrosome.smooth import fit_polynomial, smooth_with_function_and_mask from centrosome.filter import median_filter, bilateral_filter import skimage.restoration from plugins import smoothmultichannel as S INPUT_IMAGE_NAME = "myimage" OUTPUT_IMAGE_NAME = "myfilteredimage" cppref.set_headless() # Initiate plugins and custom plugins cpmodules.fill_modules() cpmodules.add_module_for_tst(S.SmoothMultichannel) def make_workspace(image, mask): """Make a workspace for testing FilterByObjectMeasurement""" module = S.SmoothMultichannel() pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image, mask, scale=1))
"""test_stackimages.py - test the stackimages module """ import unittest import numpy as np from cellprofiler_core.preferences import set_headless set_headless() import cellprofiler_core.workspace as cpw import cellprofiler_core.image as cpi import cellprofiler_core.object as cpo import cellprofiler_core.pipeline as cpp import cellprofiler_core.measurement as cpmeas import plugins.stackimages as S INPUT_IMAGE_BASENAME = "myimage" OUTPUT_IMAGE_NAME = "mystackedimage" class TestStackImages(unittest.TestCase): def make_workspace(self, images): """Make a workspace """ module = S.StackImages() pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList()
import numpy as np import imageio from scipy.ndimage import gaussian_filter from scipy.sparse import csr_matrix, save_npz, load_npz import subprocess from cellprofiler_core import image, object, pipeline, preferences, workspace, measurement preferences.set_headless() from pathlib import Path, PurePath import multiprocessing as mp def segment_single_sample(sample, input_directory, output_directory): """ """ # TODO: add docstring sample_name = sample["name"] sample_input_subdirectory = input_directory / sample_name sample_output_subdirectory = output_directory / sample_name rounds = sample["rounds"] composite_filepaths, direct_filepaths = split_filepaths( rounds, sample_input_subdirectory) merged_composite_filepath = sample_input_subdirectory / "merged_composites.tif" merge_composites(composite_filepaths, merged_composite_filepath) # TODO: fix this to be better if possible?
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
# import os import tempfile import warnings from getpass import getpass # Import OMERO Python BlitzGateway from omero.gateway import BlitzGateway # Import Cell Profiler Dependencies import cellprofiler_core.preferences as cpprefs import cellprofiler_core.pipeline as cpp # Important to set when running headless cpprefs.set_headless() # noqa # module used to inject OMERO image planes into Cell Profiler Pipeline from cellprofiler_core.modules.injectimage import InjectImage # Connect to the server def connect(hostname, username, password): conn = BlitzGateway(username, password, host=hostname, secure=True) conn.connect() return conn # Load-plate def load_plate(conn, plate_id): return conn.getObject("Plate", plate_id)