code: def run_pipeline_headless(options, args):
      """
      Run a CellProfiler pipeline in headless mode
      """
      if options.first_image_set is not 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 = 1
  
      image_set_numbers = None
  
      if options.last_image_set is not 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 = numpy.arange(1, image_set_end + 1)
              else:
                  image_set_numbers = numpy.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
          )
  
      pipeline = Pipeline()
  
      initial_measurements = None
  
      try:
          if h5py.is_hdf5(options.pipeline_filename):
              initial_measurements = 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
  
          pipeline.load(io.StringIO(pipeline_text))
  
          if not pipeline.in_batch_mode():
              #
              # Need file list in order to call prepare_run
              #
  
              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 = get_image_set_file()
  
      if file_list is not None:
          pipeline.read_file_list(file_list)
      elif options.image_directory is not None:
          pathnames = []
  
          for dirname, _, fnames in os.walk(
              os.path.abspath(options.image_directory)
          ):
              pathnames.append(
                  [
                      os.path.join(dirname, fname)
                      for fname in fnames
                      if os.path.isfile(os.path.join(dirname, fname))
                  ]
              )
  
          pathnames = sum(pathnames, [])
  
          pipeline.add_pathnames_to_file_list(pathnames)
  
      #
      # 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
                  )
  
      measurements = pipeline.run(
          image_set_start=image_set_start,
          image_set_end=image_set_end,
          grouping=groups,
          measurements_filename=None,
          initial_measurements=initial_measurements,
      )
  
      if options.done_file is not None:
          if measurements is not None and measurements.has_feature(
              EXPERIMENT,
              EXIT_STATUS,
          ):
              done_text = measurements.get_experiment_measurement(
                  EXIT_STATUS
              )
  
              exit_code = 0 if done_text == "Complete" else -1
          else:
              done_text = "Failure"
  
              exit_code = -1
  
          fd = open(options.done_file, "wt")
          fd.write("%s\n" % done_text)
          fd.close()
      elif not measurements.has_feature(EXPERIMENT, EXIT_STATUS):
          # The pipeline probably failed
          exit_code = 1
      else:
          exit_code = 0
  
      if measurements is not None:
          measurements.close()
  
      return exit_code
  
imports: import io
  import json
  import logging
  import logging.config
  import optparse
  import os
  import os.path
  import re
  import sys
  import tempfile
  import urllib.parse
  import h5py
  import matplotlib
  import numpy
  import pkg_resources
  from cellprofiler_core.constants.measurement import EXPERIMENT
  from cellprofiler_core.constants.measurement import GROUP_INDEX
  from cellprofiler_core.constants.measurement import GROUP_NUMBER
  from cellprofiler_core.constants.measurement import IMAGE
  from cellprofiler_core.constants.pipeline import M_PIPELINE, EXIT_STATUS
  from cellprofiler_core.measurement import Measurements
  from cellprofiler_core.object import ObjectSet
  from cellprofiler_core.pipeline import LoadException
  from cellprofiler_core.pipeline import Pipeline
  from cellprofiler_core.preferences import get_image_set_file
  from cellprofiler_core.preferences import get_temporary_directory
  from cellprofiler_core.preferences import set_conserve_memory
  from cellprofiler_core.preferences import set_force_bioformats
  from cellprofiler_core.preferences import set_allow_schema_write
  from cellprofiler_core.preferences import set_always_continue
  from cellprofiler_core.preferences import set_awt_headless
  from cellprofiler_core.preferences import set_data_file
  from cellprofiler_core.preferences import set_default_image_directory
  from cellprofiler_core.preferences import set_default_output_directory
  from cellprofiler_core.preferences import set_headless
  from cellprofiler_core.preferences import set_image_set_file
  from cellprofiler_core.preferences import set_plugin_directory
  from cellprofiler_core.preferences import set_temporary_directory
  from cellprofiler_core.preferences import set_widget_inspector
  from cellprofiler_core.utilities.core.workspace import is_workspace_file
  from cellprofiler_core.utilities.hdf5_dict import HDF5FileList
  from cellprofiler_core.utilities.java import start_java, stop_java
  from cellprofiler_core.utilities.measurement import load_measurements
  from cellprofiler_core.utilities.zmq import join_to_the_boundary
  from cellprofiler_core.worker import aw_parse_args
  from cellprofiler_core.worker import main
  from cellprofiler_core.workspace import Workspace
  from cellprofiler_core.reader import activate_readers
label_desc: Use image node_id 11 for calling a global function with example usage: run_pipeline_headless(options, args) and returns: exit_code