def run(args):
  if len(args)==0: print Usage; exit()
  processed = iotbx.phil.process_command_line(
    args=args, master_string=master_phil_str)
  args = processed.remaining_args
  work_params = processed.work.extract().xes
  processed.work.show()
  assert len(args) == 1
  output_dirname = work_params.output_dirname
  roi = cspad_tbx.getOptROI(work_params.roi)
  bg_roi = cspad_tbx.getOptROI(work_params.bg_roi)
  gain_map_path = work_params.gain_map
  estimated_gain = work_params.estimated_gain

  print output_dirname
  if output_dirname is None:
    output_dirname = os.path.join(os.path.dirname(args[0]), "finalise")
    print output_dirname
  hist_d = easy_pickle.load(args[0])
  if len(hist_d.keys())==2:
    hist_d = hist_d['histogram']
  pixel_histograms = faster_methods_for_pixel_histograms(
    hist_d, work_params)

  result = xes_from_histograms(
    pixel_histograms, output_dirname=output_dirname,
    gain_map_path=gain_map_path, estimated_gain=estimated_gain,
    roi=roi, run=work_params.run)
Example #2
0
def run(args):
    if len(args) == 0:
        print Usage
        exit()
    processed = iotbx.phil.process_command_line(args=args,
                                                master_string=master_phil_str)
    args = processed.remaining_args
    work_params = processed.work.extract().xes
    processed.work.show()
    assert len(args) == 1
    output_dirname = work_params.output_dirname
    roi = cspad_tbx.getOptROI(work_params.roi)
    bg_roi = cspad_tbx.getOptROI(work_params.bg_roi)
    gain_map_path = work_params.gain_map
    estimated_gain = work_params.estimated_gain

    print output_dirname
    if output_dirname is None:
        output_dirname = os.path.join(os.path.dirname(args[0]), "finalise")
        print output_dirname
    hist_d = easy_pickle.load(args[0])
    if len(hist_d.keys()) == 2:
        hist_d = hist_d['histogram']
    pixel_histograms = faster_methods_for_pixel_histograms(hist_d, work_params)

    result = xes_from_histograms(pixel_histograms,
                                 output_dirname=output_dirname,
                                 gain_map_path=gain_map_path,
                                 estimated_gain=estimated_gain,
                                 roi=roi,
                                 run=work_params.run)
Example #3
0
def run(args):
  processed = iotbx.phil.process_command_line(
    args=args, master_string=master_phil_str)
  args = processed.remaining_args
  work_params = processed.work.extract().xes
  processed.work.show()
  assert len(args) == 1
  output_dirname = work_params.output_dirname
  roi = cspad_tbx.getOptROI(work_params.roi)
  bg_roi = cspad_tbx.getOptROI(work_params.bg_roi)
  gain_map_path = work_params.gain_map
  estimated_gain = work_params.estimated_gain
  nproc = work_params.nproc
  photon_threshold = work_params.photon_threshold
  method = work_params.method
  print output_dirname
  if output_dirname is None:
    output_dirname = os.path.join(os.path.dirname(args[0]), "finalise")
    print output_dirname
  hist_d = easy_pickle.load(args[0])
  if len(hist_d.keys())==2:
    hist_d = hist_d['histogram']
  pixel_histograms = view_pixel_histograms.pixel_histograms(
    hist_d, estimated_gain=estimated_gain)
  result = xes_from_histograms(
    pixel_histograms, output_dirname=output_dirname,
    gain_map_path=gain_map_path, estimated_gain=estimated_gain,
    method=method, nproc=nproc,
    photon_threshold=photon_threshold, roi=roi, run=work_params.run)

  if bg_roi is not None:
    bg_outdir = os.path.normpath(output_dirname)+"_bg"
    bg_result = xes_from_histograms(
      pixel_histograms, output_dirname=bg_outdir,
      gain_map_path=gain_map_path, estimated_gain=estimated_gain,
      method=method, nproc=nproc,
      photon_threshold=photon_threshold, roi=bg_roi)

    from xfel.command_line.subtract_background import subtract_background
    signal = result.spectrum
    background = bg_result.spectrum
    signal = (signal[0].as_double(), signal[1])
    background = (background[0].as_double(), background[1])
    signal_x, background_subtracted = subtract_background(signal, background, plot=True)
    f = open(os.path.join(output_dirname, "background_subtracted.txt"), "wb")
    print >> f, "\n".join(["%i %f" %(x, y)
                           for x, y in zip(signal_x, background_subtracted)])
    f.close()

  else:
    from xfel.command_line import smooth_spectrum
    from scitbx.smoothing import savitzky_golay_filter
    x, y = result.spectrum[0].as_double(), result.spectrum[1]
    x, y = smooth_spectrum.interpolate(x, y)
    x, y_smoothed = savitzky_golay_filter(
      x, y, 20, 4)
    smooth_spectrum.estimate_signal_to_noise(x, y, y_smoothed)
Example #4
0
    def __init__(self,
                 address,
                 pickle_dirname=".",
                 pickle_basename="hist",
                 roi=None,
                 hist_min=None,
                 hist_max=None,
                 n_slots=None,
                 **kwds):
        """

    @param address         Address string XXX Que?!
    @param pickle_dirname     Directory portion of output pickle file
                           XXX mean, mu?
    @param pickle_basename    Filename prefix of output pickle file
                           image XXX mean, mu?
    @param calib_dir       Directory with calibration information
    @param dark_path       Path to input dark image
    @param dark_stddev     Path to input dark standard deviation
    """
        super(pixel_histograms, self).__init__(address=address, **kwds)
        self.pickle_dirname = cspad_tbx.getOptString(pickle_dirname)
        self.pickle_basename = cspad_tbx.getOptString(pickle_basename)
        self.hist_min = cspad_tbx.getOptFloat(hist_min)
        self.hist_max = cspad_tbx.getOptFloat(hist_max)
        self.n_slots = cspad_tbx.getOptInteger(n_slots)
        self.histograms = {}
        self.dimensions = None
        self.roi = cspad_tbx.getOptROI(roi)
        self.values = flex.long()

        self.sigma_scaling = False
        if self.hist_min is None: self.hist_min = -50
        if self.hist_max is None: self.hist_max = 150
        if self.n_slots is None: self.n_slots = 200
Example #5
0
    def __init__(self,
                 address,
                 pickle_dirname=".",
                 pickle_basename="",
                 roi=None,
                 **kwds):
        """The mod_average class constructor stores the parameters passed
    from the pyana configuration file in instance variables.  All
    parameters, except @p address are optional, and hence need not be
    defined in pyana.cfg.

    @param address         Full data source address of the DAQ device
    @param pickle_dirname  Directory portion of output pickle file
                           XXX mean, mu?
    @param pickle_basename Filename prefix of output pickle file
                           image XXX mean, mu?
    """
        super(mod_xes, self).__init__(address=address, **kwds)
        self.pickle_dirname = cspad_tbx.getOptString(pickle_dirname)
        self.pickle_basename = cspad_tbx.getOptString(pickle_basename)
        self.roi = cspad_tbx.getOptROI(roi)
  def __init__(self,
               address,
               pickle_dirname=".",
               pickle_basename="hist",
               roi=None,
               hist_min=None,
               hist_max=None,
               n_slots=None,
               **kwds):
    """

    @param address         Address string XXX Que?!
    @param pickle_dirname     Directory portion of output pickle file
                           XXX mean, mu?
    @param pickle_basename    Filename prefix of output pickle file
                           image XXX mean, mu?
    @param calib_dir       Directory with calibration information
    @param dark_path       Path to input dark image
    @param dark_stddev     Path to input dark standard deviation
    """
    super(pixel_histograms, self).__init__(
      address=address,
      **kwds
    )
    self.pickle_dirname = cspad_tbx.getOptString(pickle_dirname)
    self.pickle_basename = cspad_tbx.getOptString(pickle_basename)
    self.hist_min = cspad_tbx.getOptFloat(hist_min)
    self.hist_max = cspad_tbx.getOptFloat(hist_max)
    self.n_slots = cspad_tbx.getOptInteger(n_slots)
    self.histograms = {}
    self.dimensions = None
    self.roi = cspad_tbx.getOptROI(roi)
    self.values = flex.long()

    self.sigma_scaling = False
    if self.hist_min is None: self.hist_min = -50
    if self.hist_max is None: self.hist_max = 150
    if self.n_slots is None: self.n_slots = 200
Example #7
0
  def __init__(self,
               address,
               pickle_dirname=".",
               pickle_basename="",
               roi=None,
               **kwds):
    """The mod_average class constructor stores the parameters passed
    from the pyana configuration file in instance variables.  All
    parameters, except @p address are optional, and hence need not be
    defined in pyana.cfg.

    @param address         Full data source address of the DAQ device
    @param pickle_dirname  Directory portion of output pickle file
                           XXX mean, mu?
    @param pickle_basename Filename prefix of output pickle file
                           image XXX mean, mu?
    """
    super(mod_xes, self).__init__(
      address=address,
      **kwds
    )
    self.pickle_dirname = cspad_tbx.getOptString(pickle_dirname)
    self.pickle_basename = cspad_tbx.getOptString(pickle_basename)
    self.roi = cspad_tbx.getOptROI(roi)
Example #8
0
    def __init__(self, runs, output_dirname=".", roi=None):
        avg_basename = "avg_"
        stddev_basename = "stddev"
        self.sum_img = None
        self.sumsq_img = None
        self.nmemb = 0
        self.roi = cspad_tbx.getOptROI(roi)
        self.unbound_pixel_mask = cspad_unbound_pixel_mask()
        for i_run, run in enumerate(runs):
            run_scratch_dir = run
            result = finalise_one_run(run_scratch_dir)
            if result.sum_img is None: continue
            if self.sum_img is None:
                self.sum_img = result.sum_img
                self.sumsq_img = result.sumsq_img
            else:
                self.sum_img += result.sum_img
                self.sumsq_img += result.sumsq_img
            self.nmemb += result.nmemb

        self.avg_img = self.sum_img.as_double() / self.nmemb
        self.stddev_img = flex.sqrt(
            (self.sumsq_img.as_double() -
             self.sum_img.as_double() * self.avg_img) / (self.nmemb - 1))

        self.mask = flex.int(self.sum_img.accessor(), 0)
        self.mask.set_selected(self.sum_img == 0, 1)
        self.mask.set_selected(self.unbound_pixel_mask > 0, 1)

        if (output_dirname is not None and avg_basename is not None):
            if (not os.path.isdir(output_dirname)):
                os.makedirs(output_dirname)
            d = cspad_tbx.dpack(
                address='CxiSc1-0|Cspad2x2-0',
                data=self.avg_img,
                distance=1,
            )
            cspad_tbx.dwritef(d, output_dirname, avg_basename)
            d = cspad_tbx.dpack(
                address='CxiSc1-0|Cspad2x2-0',
                data=self.sum_img,
                distance=1,
            )
            cspad_tbx.dwritef(d, output_dirname, "sum_")
            if 1:
                output_image(self.avg_img, "%s/avg.png" % output_dirname)
                output_image(self.avg_img,
                             "%s/avg_inv.png" % output_dirname,
                             invert=True)

            if 1:
                output_matlab_form(self.sum_img, "%s/sum.m" % output_dirname)
                output_matlab_form(self.avg_img, "%s/avg.m" % output_dirname)
                output_matlab_form(self.stddev_img,
                                   "%s/stddev.m" % output_dirname)

        if (stddev_basename is not None):
            d = cspad_tbx.dpack(
                address='CxiSc1-0|Cspad2x2-0',
                data=self.stddev_img,
                distance=1,
            )
            cspad_tbx.dwritef(d, output_dirname, stddev_basename)

            # XXX we should really figure out automatically the area where the spectrum is
            #write an integrated spectrum from lines 186-227
            #spectrum_focus = self.sum_img.as_numpy_array()[186:228,:]
            img = self.sum_img
            if self.roi is None:
                spectrum_focus = img
                mask_focus = self.mask
            else:
                slices = (slice(self.roi[2],
                                self.roi[3]), slice(self.roi[0], self.roi[1]))
                spectrum_focus = img[slices]
                mask_focus = self.mask[slices]
            if False:
                from matplotlib import pylab
                pylab.imshow(spectrum_focus.as_numpy_array())
                pylab.show()

        output_spectrum(spectrum_focus,
                        mask_focus=mask_focus,
                        output_dirname=output_dirname)

        print "Total number of images used from %i runs: %i" % (i_run + 1,
                                                                self.nmemb)
Example #9
0
  def __init__(self,
               address,
               calib_dir=None,
               common_mode_correction="none",
               photon_threshold=None,
               two_photon_threshold=None,
               dark_path=None,
               dark_stddev=None,
               mask_path=None,
               gain_map_path=None,
               gain_map_level=None,
               cache_image=True,
               roi=None,
               laser_1_status=None,
               laser_4_status=None,
               laser_wait_time=None,
               override_beam_x=None,
               override_beam_y=None,
               bin_size=None,
               crop_rayonix=False,
               **kwds):
    """The common_mode_correction class constructor stores the
    parameters passed from the pyana configuration file in instance
    variables.

    @param address         Full data source address of the DAQ device
    @param calib_dir       Directory with calibration information
    @param common_mode_correction The type of common mode correction to apply
    @param dark_path       Path to input average dark image
    @param dark_stddev     Path to input standard deviation dark
                           image, required if @p dark_path is given
    @param mask_path       Path to input mask.  Pixels to mask out should be set to -2
    @param gain_map_path   Path to input gain map.  Multiplied times the image.
    @param gain_map_level  If set, all the '1' pixels in the gain_map are set to this multiplier
                           and all the '0' pixels in the gain_map are set to '1'.  If not set,
                           use the values in the gain_map directly
    @param laser_1_status  0 or 1 to indicate that the laser should be off or on respectively
    @param laser_4_status  0 or 1 to indicate that the laser should be off or on respectively
    @param laser_wait_time Length of time in milliseconds to wait after a laser
                           change of status to begin accepting images again.
                           (rejection of images occurs immediately after status
                           change).
    @param override_beam_x override value for x coordinate of beam center in pixels
    @param override_beam_y override value for y coordinate of beam center in pixels
    @param bin_size bin size for rayonix detector used to determin pixel size
    @param crop_rayonix    whether to crop rayonix images such that image center is the beam center
    """

    # Cannot use the super().__init__() construct here, because
    # common_mode_correction refers to the argument, and not the
    # class.
    mod_event_info.__init__(self, address=address, **kwds)

    # The paths will be substituted in beginjob(), where evt and env
    # are available.
    self._dark_path = cspad_tbx.getOptString(dark_path)
    self._dark_stddev_path = cspad_tbx.getOptString(dark_stddev)
    self._gain_map_path = cspad_tbx.getOptString(gain_map_path)
    self._mask_path = cspad_tbx.getOptString(mask_path)

    self.gain_map_level = cspad_tbx.getOptFloat(gain_map_level)
    self.common_mode_correction = cspad_tbx.getOptString(common_mode_correction)
    self.photon_threshold = cspad_tbx.getOptFloat(photon_threshold)
    self.two_photon_threshold = cspad_tbx.getOptFloat(two_photon_threshold)
    self.cache_image = cspad_tbx.getOptBool(cache_image)
    self.filter_laser_1_status = cspad_tbx.getOptInteger(laser_1_status)
    self.filter_laser_4_status = cspad_tbx.getOptInteger(laser_4_status)
    if self.filter_laser_1_status is not None:
      self.filter_laser_1_status = bool(self.filter_laser_1_status)
    if self.filter_laser_4_status is not None:
      self.filter_laser_4_status = bool(self.filter_laser_4_status)
    self.filter_laser_wait_time = cspad_tbx.getOptInteger(laser_wait_time)
    self.override_beam_x = cspad_tbx.getOptFloat(override_beam_x)
    self.override_beam_y = cspad_tbx.getOptFloat(override_beam_y)
    self.bin_size = cspad_tbx.getOptInteger(bin_size)
    self.crop_rayonix = cspad_tbx.getOptBool(crop_rayonix)

    self.cspad_img = None # The current image - set by self.event()
    self.sum_common_mode = 0
    self.sumsq_common_mode = 0
    self.roi = cspad_tbx.getOptROI(roi) # used to ignore the signal region in chebyshev fit

    assert self.common_mode_correction in \
        ("gaussian", "mean", "median", "mode", "none", "chebyshev")

    # Get and parse metrology.
    self.sections = None
    device = cspad_tbx.address_split(self.address)[2]
    if device == 'Andor':
      self.sections = [] # XXX FICTION
    elif device == 'Cspad':
      if self.address == 'XppGon-0|Cspad-0':
        self.sections = [] # Not used for XPP
      else:
        self.sections = calib2sections(cspad_tbx.getOptString(calib_dir))
    elif device == 'Cspad2x2':
      # There is no metrology information for the Sc1 detector, so
      # make it up.  The sections are rotated by 90 degrees with
      # respect to the "standing up" convention.
      self.sections = [[Section(90, (185 / 2 + 0,   (2 * 194 + 3) / 2)),
                        Section(90, (185 / 2 + 185, (2 * 194 + 3) / 2))]]
    elif device == 'marccd':
      self.sections = [] # XXX FICTION
    elif device == 'pnCCD':
      self.sections = [] # XXX FICTION
    elif device == 'Rayonix':
      self.sections = [] # XXX FICTION
    elif device == 'Opal1000':
      self.sections = [] # XXX FICTION
    if self.sections is None:
      raise RuntimeError("Failed to load metrology")
def run(args):
  assert len(args) > 0
  command_line = (option_parser()
                  .option("--roi",
                          type="string",
                          help="Region of interest for summing up histograms"
                          "from neighbouring pixels.")
                  .option("--log_scale",
                          action="store_true",
                          default=False,
                          help="Draw y-axis on a log scale.")
                  .option("--normalise",
                          action="store_true",
                          default=False,
                          help="Normalise by number of member images.")
                  .option("--save",
                          action="store_true",
                          default=False,
                          help="Save each plot as a png.")
                  .option("--start",
                          type="string",
                          help="Starting pixel coordinates")
                  .option("--fit_gaussians",
                          action="store_true",
                          default=False,
                          help="Fit gaussians to the peaks.")
                  .option("--n_gaussians",
                          type="int",
                          default=2,
                          help="Number of gaussians to fit.")
                  .option("--estimated_gain",
                          type="float",
                          default=30,
                          help="The approximate position of the one photon peak.")
                  ).process(args=args)
  log_scale = command_line.options.log_scale
  fit_gaussians = command_line.options.fit_gaussians
  roi = cspad_tbx.getOptROI(command_line.options.roi)
  normalise = command_line.options.normalise
  save_image = command_line.options.save
  starting_pixel = command_line.options.start
  n_gaussians = command_line.options.n_gaussians
  estimated_gain = command_line.options.estimated_gain
  if starting_pixel is not None:
    starting_pixel = eval(starting_pixel)
    assert isinstance(starting_pixel, tuple)
  args = command_line.args

  path = args[0]
  window_title = path
  d = easy_pickle.load(path)
  args = args[1:]
  pixels = None
  if len(args) > 0:
    pixels = [eval(arg) for arg in args]
    for pixel in pixels:
      assert isinstance(pixel, tuple)
      assert len(pixel) == 2
  if roi is not None:

    summed_hist = {}
    for i in range(roi[2], roi[3]):
      for j in range(roi[0], roi[1]):
        if (i,j) not in summed_hist:
          summed_hist[(0,0)] = d[(i,j)]
        else:
          summed_hist[(0,0)].update(d[(i,j)])
    d = summed_hist

  #if roi is not None:

    #summed_hist = None
    #for i in range(roi[2], roi[3]):
      #for j in range(roi[0], roi[1]):
        #if summed_hist is None:
          #summed_hist = d[(i,j)]
        #else:
          #summed_hist.update(d[(i,j)])

    #title = str(roi)
    #plot(hist, window_title=window_title, title=title,log_scale=log_scale,
         #normalise=normalise, save_image=save_image, fit_gaussians=fit_gaussians)
    #return


  histograms = pixel_histograms(d, estimated_gain=estimated_gain)
  histograms.plot(
     pixels=pixels, starting_pixel=starting_pixel, fit_gaussians=fit_gaussians,
    n_gaussians=n_gaussians, window_title=window_title, log_scale=log_scale,
    save_image=save_image)
def run(args):
    assert len(args) > 0
    command_line = (
        option_parser().option(
            "--roi",
            type="string",
            help="Region of interest for summing up histograms"
            "from neighbouring pixels.").option(
                "--log_scale",
                action="store_true",
                default=False,
                help="Draw y-axis on a log scale.").option(
                    "--normalise",
                    action="store_true",
                    default=False,
                    help="Normalise by number of member images.").option(
                        "--save",
                        action="store_true",
                        default=False,
                        help="Save each plot as a png.").option(
                            "--start",
                            type="string",
                            help="Starting pixel coordinates").option(
                                "--fit_gaussians",
                                action="store_true",
                                default=False,
                                help="Fit gaussians to the peaks.").option(
                                    "--n_gaussians",
                                    type="int",
                                    default=2,
                                    help="Number of gaussians to fit.").
        option(
            "--estimated_gain",
            type="float",
            default=30,
            help="The approximate position of the one photon peak.")).process(
                args=args)
    log_scale = command_line.options.log_scale
    fit_gaussians = command_line.options.fit_gaussians
    roi = cspad_tbx.getOptROI(command_line.options.roi)
    normalise = command_line.options.normalise
    save_image = command_line.options.save
    starting_pixel = command_line.options.start
    n_gaussians = command_line.options.n_gaussians
    estimated_gain = command_line.options.estimated_gain
    if starting_pixel is not None:
        starting_pixel = eval(starting_pixel)
        assert isinstance(starting_pixel, tuple)
    args = command_line.args

    path = args[0]
    window_title = path
    d = easy_pickle.load(path)
    args = args[1:]
    pixels = None
    if len(args) > 0:
        pixels = [eval(arg) for arg in args]
        for pixel in pixels:
            assert isinstance(pixel, tuple)
            assert len(pixel) == 2
    if roi is not None:

        summed_hist = {}
        for i in range(roi[2], roi[3]):
            for j in range(roi[0], roi[1]):
                if (i, j) not in summed_hist:
                    summed_hist[(0, 0)] = d[(i, j)]
                else:
                    summed_hist[(0, 0)].update(d[(i, j)])
        d = summed_hist

    #if roi is not None:

    #summed_hist = None
    #for i in range(roi[2], roi[3]):
    #for j in range(roi[0], roi[1]):
    #if summed_hist is None:
    #summed_hist = d[(i,j)]
    #else:
    #summed_hist.update(d[(i,j)])

    #title = str(roi)
    #plot(hist, window_title=window_title, title=title,log_scale=log_scale,
    #normalise=normalise, save_image=save_image, fit_gaussians=fit_gaussians)
    #return

    histograms = pixel_histograms(d, estimated_gain=estimated_gain)
    histograms.plot(pixels=pixels,
                    starting_pixel=starting_pixel,
                    fit_gaussians=fit_gaussians,
                    n_gaussians=n_gaussians,
                    window_title=window_title,
                    log_scale=log_scale,
                    save_image=save_image)
Example #12
0
def run(args):
    processed = iotbx.phil.process_command_line(args=args,
                                                master_string=master_phil_str)
    args = processed.remaining_args
    work_params = processed.work.extract().xes
    processed.work.show()
    assert len(args) == 1
    output_dirname = work_params.output_dirname
    roi = cspad_tbx.getOptROI(work_params.roi)
    bg_roi = cspad_tbx.getOptROI(work_params.bg_roi)
    gain_map_path = work_params.gain_map
    estimated_gain = work_params.estimated_gain
    nproc = work_params.nproc
    photon_threshold = work_params.photon_threshold
    method = work_params.method
    print output_dirname
    if output_dirname is None:
        output_dirname = os.path.join(os.path.dirname(args[0]), "finalise")
        print output_dirname
    hist_d = easy_pickle.load(args[0])
    if len(hist_d.keys()) == 2:
        hist_d = hist_d['histogram']
    pixel_histograms = view_pixel_histograms.pixel_histograms(
        hist_d, estimated_gain=estimated_gain)
    result = xes_from_histograms(pixel_histograms,
                                 output_dirname=output_dirname,
                                 gain_map_path=gain_map_path,
                                 estimated_gain=estimated_gain,
                                 method=method,
                                 nproc=nproc,
                                 photon_threshold=photon_threshold,
                                 roi=roi,
                                 run=work_params.run)

    if bg_roi is not None:
        bg_outdir = os.path.normpath(output_dirname) + "_bg"
        bg_result = xes_from_histograms(pixel_histograms,
                                        output_dirname=bg_outdir,
                                        gain_map_path=gain_map_path,
                                        estimated_gain=estimated_gain,
                                        method=method,
                                        nproc=nproc,
                                        photon_threshold=photon_threshold,
                                        roi=bg_roi)

        from xfel.command_line.subtract_background import subtract_background
        signal = result.spectrum
        background = bg_result.spectrum
        signal = (signal[0].as_double(), signal[1])
        background = (background[0].as_double(), background[1])
        signal_x, background_subtracted = subtract_background(signal,
                                                              background,
                                                              plot=True)
        f = open(os.path.join(output_dirname, "background_subtracted.txt"),
                 "wb")
        print >> f, "\n".join([
            "%i %f" % (x, y) for x, y in zip(signal_x, background_subtracted)
        ])
        f.close()

    else:
        from xfel.command_line import smooth_spectrum
        from scitbx.smoothing import savitzky_golay_filter
        x, y = result.spectrum[0].as_double(), result.spectrum[1]
        x, y = smooth_spectrum.interpolate(x, y)
        x, y_smoothed = savitzky_golay_filter(x, y, 20, 4)
        smooth_spectrum.estimate_signal_to_noise(x, y, y_smoothed)
Example #13
0
    def __init__(self,
                 address,
                 dispatch=None,
                 integration_dirname=None,
                 integration_basename=None,
                 out_dirname=None,
                 out_basename=None,
                 roi=None,
                 distl_min_peaks=None,
                 distl_flags=None,
                 threshold=None,
                 xtal_target=None,
                 negate_hits=False,
                 trial_id=None,
                 db_logging=False,
                 progress_logging=False,
                 sql_buffer_size=1,
                 db_host=None,
                 db_name=None,
                 db_table_name=None,
                 db_experiment_tag=None,
                 db_user=None,
                 db_password=None,
                 db_tags=None,
                 trial=None,
                 rungroup_id=None,
                 db_version='v1',
                 **kwds):
        """The mod_hitfind class constructor stores the parameters passed
    from the pyana configuration file in instance variables.  All
    parameters, except @p address are optional, and hence need not be
    defined in pyana.cfg.

    @param address      Full data source address of the DAQ device
    @param dispatch     Function to call
    @param integration_dirname
                        Directory portion of output integration file
                        pathname
    @param integration_basename
                        Filename prefix of output integration file
                        pathname
    @param out_dirname  Directory portion of output image pathname
    @param out_basename Filename prefix of output image pathname
    @param roi          Region of interest for thresholding, on the
                        form fast_low:fast_high,slow_low:slow_high
    @param threshold    Minimum value in region of interest to pass
    """

        super(mod_hitfind, self).__init__(address=address, **kwds)

        self.m_dispatch = cspad_tbx.getOptString(dispatch)
        self.m_integration_basename = cspad_tbx.getOptString(
            integration_basename)
        self.m_integration_dirname = cspad_tbx.getOptString(
            integration_dirname)
        self.m_out_basename = cspad_tbx.getOptString(out_basename)
        self.m_out_dirname = cspad_tbx.getOptString(out_dirname)
        self.m_distl_min_peaks = cspad_tbx.getOptInteger(distl_min_peaks)
        self.m_distl_flags = cspad_tbx.getOptStrings(distl_flags)
        self.m_threshold = cspad_tbx.getOptInteger(threshold)
        self.m_xtal_target = cspad_tbx.getOptString(xtal_target)
        self.m_negate_hits = cspad_tbx.getOptBool(negate_hits)
        self.m_trial_id = cspad_tbx.getOptInteger(trial_id)
        self.m_db_logging = cspad_tbx.getOptBool(db_logging)
        self.m_progress_logging = cspad_tbx.getOptBool(progress_logging)
        self.m_sql_buffer_size = cspad_tbx.getOptInteger(sql_buffer_size)
        self.m_db_host = cspad_tbx.getOptString(db_host)
        self.m_db_name = cspad_tbx.getOptString(db_name)
        self.m_db_table_name = cspad_tbx.getOptString(db_table_name)
        self.m_db_experiment_tag = cspad_tbx.getOptString(db_experiment_tag)
        self.m_db_user = cspad_tbx.getOptString(db_user)
        self.m_db_password = cspad_tbx.getOptString(db_password)
        self.m_db_tags = cspad_tbx.getOptString(db_tags)
        self.m_trial = cspad_tbx.getOptInteger(trial)
        self.m_rungroup_id = cspad_tbx.getOptInteger(rungroup_id)
        self.m_db_version = cspad_tbx.getOptString(db_version)
        # A ROI should not contain any ASIC boundaries, as these are
        # noisy.  Hence circular ROI:s around the beam centre are probably
        # not such a grand idea.
        self.m_roi = cspad_tbx.getOptROI(roi)

        # Verify that dist_min_peaks is either "restrictive" or
        # "permissive", but not both.  ^ is the logical xor operator
        if self.m_distl_min_peaks is not None:
            if (not (('permissive' in self.m_distl_flags) ^
                     ('restrictive' in self.m_distl_flags))):
                raise RuntimeError("""Sorry, with the distl_min_peaks option,
          distl_flags must be set to 'permissive' or 'restrictive'.""")
            if (self.m_roi is not None):
                raise RuntimeError("""Sorry, either specify region of interest
          (roi) or distl_min_peaks, but not both.""")

        if self.m_db_logging:
            self.buffered_sql_entries = []
            assert self.m_sql_buffer_size >= 1

        if self.m_progress_logging:
            if self.m_db_version == 'v1':
                self.buffered_progress_entries = []
                assert self.m_sql_buffer_size >= 1
                self.isoforms = {}
            elif self.m_db_version == 'v2':
                from xfel.ui import db_phil_str
                from libtbx.phil import parse
                extra_phil = """
        input {
          trial = None
            .type = int
          trial_id = None
            .type = int
          rungroup = None
            .type = int
        }
        """
                self.db_params = parse(db_phil_str + extra_phil).extract()
                self.db_params.experiment_tag = self.m_db_experiment_tag
                self.db_params.db.host = self.m_db_host
                self.db_params.db.name = self.m_db_name
                self.db_params.db.user = self.m_db_user
                self.db_params.db.password = self.m_db_password
                self.db_params.input.trial = self.m_trial
                self.db_params.input.rungroup = self.m_rungroup_id

        if self.m_db_tags is None:
            self.m_db_tags = ""
Example #14
0
  def __init__(self,
               address,
               dispatch               = None,
               integration_dirname    = None,
               integration_basename   = None,
               out_dirname            = None,
               out_basename           = None,
               roi                    = None,
               distl_min_peaks        = None,
               distl_flags            = None,
               threshold              = None,
               xtal_target            = None,
               negate_hits            = False,
               trial_id               = None,
               db_logging             = False,
               progress_logging       = False,
               sql_buffer_size        = 1,
               db_host                = None,
               db_name                = None,
               db_table_name          = None,
               db_experiment_tag      = None,
               db_user                = None,
               db_password            = None,
               db_tags                = None,
               rungroup_id            = None,
               **kwds):
    """The mod_hitfind class constructor stores the parameters passed
    from the pyana configuration file in instance variables.  All
    parameters, except @p address are optional, and hence need not be
    defined in pyana.cfg.

    @param address      Full data source address of the DAQ device
    @param dispatch     Function to call
    @param integration_dirname
                        Directory portion of output integration file
                        pathname
    @param integration_basename
                        Filename prefix of output integration file
                        pathname
    @param out_dirname  Directory portion of output image pathname
    @param out_basename Filename prefix of output image pathname
    @param roi          Region of interest for thresholding, on the
                        form fast_low:fast_high,slow_low:slow_high
    @param threshold    Minimum value in region of interest to pass
    """

    super(mod_hitfind, self).__init__(address=address, **kwds)

    self.m_dispatch             = cspad_tbx.getOptString(dispatch)
    self.m_integration_basename = cspad_tbx.getOptString(integration_basename)
    self.m_integration_dirname  = cspad_tbx.getOptString(integration_dirname)
    self.m_out_basename         = cspad_tbx.getOptString(out_basename)
    self.m_out_dirname          = cspad_tbx.getOptString(out_dirname)
    self.m_distl_min_peaks      = cspad_tbx.getOptInteger(distl_min_peaks)
    self.m_distl_flags          = cspad_tbx.getOptStrings(distl_flags)
    self.m_threshold            = cspad_tbx.getOptInteger(threshold)
    self.m_xtal_target          = cspad_tbx.getOptString(xtal_target)
    self.m_negate_hits          = cspad_tbx.getOptBool(negate_hits)
    self.m_trial_id             = cspad_tbx.getOptInteger(trial_id)
    self.m_db_logging           = cspad_tbx.getOptBool(db_logging)
    self.m_progress_logging     = cspad_tbx.getOptBool(progress_logging)
    self.m_sql_buffer_size      = cspad_tbx.getOptInteger(sql_buffer_size)
    self.m_db_host              = cspad_tbx.getOptString(db_host)
    self.m_db_name              = cspad_tbx.getOptString(db_name)
    self.m_db_table_name        = cspad_tbx.getOptString(db_table_name)
    self.m_db_experiment_tag    = cspad_tbx.getOptString(db_experiment_tag)
    self.m_db_user              = cspad_tbx.getOptString(db_user)
    self.m_db_password          = cspad_tbx.getOptString(db_password)
    self.m_db_tags              = cspad_tbx.getOptString(db_tags)
    self.m_rungroup_id          = cspad_tbx.getOptInteger(rungroup_id)
    # A ROI should not contain any ASIC boundaries, as these are
    # noisy.  Hence circular ROI:s around the beam centre are probably
    # not such a grand idea.
    self.m_roi = cspad_tbx.getOptROI(roi)

    # Verify that dist_min_peaks is either "restrictive" or
    # "permissive", but not both.  ^ is the logical xor operator
    if self.m_distl_min_peaks is not None:
      if (not (('permissive'  in self.m_distl_flags) ^
               ('restrictive' in self.m_distl_flags))):
        raise RuntimeError("""Sorry, with the distl_min_peaks option,
          distl_flags must be set to 'permissive' or 'restrictive'.""")
      if (self.m_roi is not None):
        raise RuntimeError("""Sorry, either specify region of interest
          (roi) or distl_min_peaks, but not both.""")

    if self.m_db_logging:
      self.buffered_sql_entries = []
      assert self.m_sql_buffer_size >= 1

    if self.m_progress_logging:
      self.buffered_progress_entries = []
      assert self.m_sql_buffer_size >= 1
      self.isoforms = {}

    if self.m_db_tags is None:
      self.m_db_tags = ""
Example #15
0
  def __init__(self,
               runs,
               output_dirname=".",
               roi=None):
    avg_basename="avg_"
    stddev_basename="stddev"
    self.sum_img = None
    self.sumsq_img = None
    self.nmemb = 0
    self.roi = cspad_tbx.getOptROI(roi)
    self.unbound_pixel_mask = cspad_unbound_pixel_mask()
    for i_run, run in enumerate(runs):
      run_scratch_dir = run
      result = finalise_one_run(run_scratch_dir)
      if result.sum_img is None: continue
      if self.sum_img is None:
        self.sum_img = result.sum_img
        self.sumsq_img = result.sumsq_img
      else:
        self.sum_img += result.sum_img
        self.sumsq_img += result.sumsq_img
      self.nmemb += result.nmemb

    self.avg_img = self.sum_img.as_double() / self.nmemb
    self.stddev_img = flex.sqrt((self.sumsq_img.as_double() - self.sum_img.as_double() * self.avg_img) / (self.nmemb - 1))

    self.mask = flex.int(self.sum_img.accessor(), 0)
    self.mask.set_selected(self.sum_img == 0, 1)
    self.mask.set_selected(self.unbound_pixel_mask > 0, 1)

    if (output_dirname is not None and
        avg_basename is not None):
      if (not os.path.isdir(output_dirname)):
        os.makedirs(output_dirname)
      d = cspad_tbx.dpack(
        address='CxiSc1-0|Cspad2x2-0',
        data=self.avg_img,
        distance=1,
      )
      cspad_tbx.dwritef(d, output_dirname, avg_basename)
      d = cspad_tbx.dpack(
        address='CxiSc1-0|Cspad2x2-0',
        data=self.sum_img,
        distance=1,
      )
      cspad_tbx.dwritef(d, output_dirname, "sum_")
      if 1:
        output_image(self.avg_img, "%s/avg.png" %output_dirname)
        output_image(self.avg_img, "%s/avg_inv.png" %output_dirname, invert=True)

      if 1:
        output_matlab_form(self.sum_img, "%s/sum.m" %output_dirname)
        output_matlab_form(self.avg_img, "%s/avg.m" %output_dirname)
        output_matlab_form(self.stddev_img, "%s/stddev.m" %output_dirname)

    if (stddev_basename is not None):
      d = cspad_tbx.dpack(
        address='CxiSc1-0|Cspad2x2-0',
        data=self.stddev_img,
        distance=1,
      )
      cspad_tbx.dwritef(d, output_dirname, stddev_basename)

      # XXX we should really figure out automatically the area where the spectrum is
      #write an integrated spectrum from lines 186-227
      #spectrum_focus = self.sum_img.as_numpy_array()[186:228,:]
      img = self.sum_img
      if self.roi is None:
        spectrum_focus = img
        mask_focus = self.mask
      else:
        slices = (slice(self.roi[2],self.roi[3]), slice(self.roi[0],self.roi[1]))
        spectrum_focus = img[slices]
        mask_focus = self.mask[slices]
      if False:
        from matplotlib import pylab
        pylab.imshow(spectrum_focus.as_numpy_array())
        pylab.show()

    output_spectrum(spectrum_focus, mask_focus=mask_focus,
                    output_dirname=output_dirname)

    print "Total number of images used from %i runs: %i" %(i_run+1, self.nmemb)