Ejemplo n.º 1
0
 def expt_detector_maker(self):
   """Construct the detector object for the experiments file. This function generates a monolithic flattening of the
   CSPAD detector if not supplied with an image file."""
   self.distance = self.data['distance']
   self.xbeam, self.ybeam = self.data['xbeam'], self.data['ybeam']
   if len(self.img_location) > 0 and not dxtbx.load(self.img_location[0])._image_file.endswith("_00000.pickle"):
     self.detector = dxtbx.load(self.img_location[0])._detector()
   else:
     self.detector = detector.detector_factory.simple('SENSOR_UNKNOWN',self.distance,(self.xbeam, self.ybeam),'+x','-y',
     (self.pixel_size, self.pixel_size),(1765,1765))
Ejemplo n.º 2
0
def main(filename, threshold, images):
  '''Read and sum all images, define those pixels which are POSITIVE but which
  come out below threshold as mask, write this mask in a format useful for
  DIALS.'''

  from dxtbx import load
  from dials.array_family import flex
  from cPickle import dump

  image_data = None

  for image in images:
    i = load(image)
    d = i.get_raw_data()
    if image_data is None:
      image_data = i.get_raw_data()
    else:
      image_data += i.get_raw_data()

  mask_inverse = (image_data >= 0) & (image_data < threshold)
  mask = ~ mask_inverse
  dump((mask,), open(filename, 'w'))

  print 'Mask written to %s' % filename
  return
Ejemplo n.º 3
0
def dumpImages(imagePaths):
	for path in imagePaths:
		print "Dumping image:", path
		rootname = splitext(basename(path))[0]
		imageName = rootname + '.img'
		
		if (os.path.exists(imageName)):
			print "Image", imageName, "already dumped - skipping."
			continue
		
		ext = splitext(basename(path))[1]
		
		if not ext == ".pickle":
			print "Warning: not a pickle file. Converting to pickle first."
			command = "cxi.image2pickle " + path
			os.system(command)
		
		db = dxtbx.load(rootname + ".pickle").get_detectorbase()
		data = db.get_raw_data()

		data_array = array.array('i')

		for i in range(0, len(data)):
			data_array.append(data[i])

		string = data_array.tostring()
		newFile = open(imageName, 'wb')
		newFile.write(string)
		newFile.close()
Ejemplo n.º 4
0
def to_imageset(input_filename, extra_filename=None):
  '''Get an image set from the xds input filename plus an extra filename

  Params:
      input_filename The XDS.INP file
      extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file

  Returns:
      The imageset

  '''
  from iotbx.xds import xds_inp
  from dxtbx.imageset import ImageSetFactory
  import dxtbx

  # Read the input filename
  handle = xds_inp.reader()
  handle.read_file(input_filename)

  # Get the template
  template = handle.name_template_of_data_frames[0].replace('?', '#')
  image_range = handle.data_range
  detector_name = handle.detector

  if extra_filename is not None:
    # we can get all the extra dxtbx models from extra_filename
    check_format = False
  else:
    # we need the image files present to get the dxtbx models
    check_format = True

  # Create the imageset
  imageset = ImageSetFactory.from_template(
    template, image_range=image_range, check_format=False)[0]

  # If an extra filename has been specified, try to load models
  if extra_filename:
    models = dxtbx.load(extra_filename)
    detector = models.get_detector()
    if detector_name.strip() == 'PILATUS':
      from dxtbx.model import ParallaxCorrectedPxMmStrategy
      from cctbx.eltbx import attenuation_coefficient
      table = attenuation_coefficient.get_table("Si")
      wavelength = models.get_beam().get_wavelength()
      mu = table.mu_at_angstrom(wavelength) / 10.0
      t0 = handle.sensor_thickness
      for panel in detector:
        panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
        panel.set_trusted_range(
          (handle.minimum_valid_pixel_value, handle.overload))
    imageset.set_beam(models.get_beam())
    imageset.set_detector(detector)
    imageset.set_goniometer(models.get_goniometer())
    # take the image range from XDS.INP
    scan = models.get_scan()
    scan.set_image_range(image_range)
    imageset.set_scan(scan)

  # Return the imageset
  return imageset
Ejemplo n.º 5
0
def read_image(image_name):
  import dxtbx
  from scitbx.array_family import flex
  data = dxtbx.load(image_name).get_raw_data()
  data.reshape(flex.grid(2527, 2463))
  subset = data[1055:1473,984:1478]
  return subset
Ejemplo n.º 6
0
    def run(self):
        params, options = self.parser.parse_args(show_diff_phil=True)
        assert params.input.single_img is not None

        filebase = os.path.splitext(params.input.single_img)[0]

        for item in dir(params.output):
            value = getattr(params.output, item)
            try:
                if "%s" in value:
                    setattr(params.output, item, value % filebase)
            except Exception:
                pass

        self.params = params
        self.options = options

        # load the image
        img = dxtbx.load(params.input.single_img)
        imgset = MemImageSet([img])
        datablock = DataBlockFactory.from_imageset(imgset)[0]

        # Cannot export MemImageSets
        # if self.params.output.datablock_filename:
        # from dxtbx.datablock import DataBlockDumper
        # dump = DataBlockDumper(datablock)
        # dump.as_json(self.params.output.datablock_filename)

        observed = self.find_spots(datablock)
        experiments, indexed = self.index(datablock, observed)
        experiments = self.refine(experiments, indexed)
        integrated = self.integrate(experiments, indexed)
Ejemplo n.º 7
0
def main():
  frame = sys.argv[1]
  powers = [2 ** n for n in range(20)]

  for j in range(powers[-1] + 1):
    i = dxtbx.load(frame)
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    if j in powers:
      print j, mem
Ejemplo n.º 8
0
def main(xparm_file, spot_file):

  import dxtbx
  from dxtbx.serialize.xds import to_crystal
  models = dxtbx.load(xparm_file)
  crystal_model = to_crystal(xparm_file)

  from dxtbx.model.experiment.experiment_list import Experiment, ExperimentList
  experiment = Experiment(beam=models.get_beam(),
                          detector=models.get_detector(),
                          goniometer=models.get_goniometer(),
                          scan=models.get_scan(),
                          crystal=crystal_model)

  detector = experiment.detector
  beam = experiment.beam
  goniometer = experiment.goniometer
  scan = experiment.scan

  from iotbx.xds import spot_xds
  spot_xds_handle = spot_xds.reader()
  spot_xds_handle.read_file(spot_file)

  from cctbx.array_family import flex
  centroids_px = flex.vec3_double(spot_xds_handle.centroid)
  miller_indices = flex.miller_index(spot_xds_handle.miller_index)

  # only those reflections that were actually indexed
  centroids_px = centroids_px.select(miller_indices != (0,0,0))
  miller_indices = miller_indices.select(miller_indices != (0,0,0))

  ub = crystal_model.get_A()

  d_spacings = [1.0 / (ub * mi).length() for mi in miller_indices]

  print max(d_spacings)

  # Convert Pixel coordinate into mm/rad
  x, y, z = centroids_px.parts()
  x_mm, y_mm = detector[0].pixel_to_millimeter(flex.vec2_double(x, y)).parts()
  z_rad = scan.get_angle_from_array_index(z, deg=False)
  centroids_mm = flex.vec3_double(x_mm, y_mm, z_rad)

  # then convert detector position to reciprocal space position

  # based on code in dials/algorithms/indexing/indexer2.py
  s1 = detector[0].get_lab_coord(flex.vec2_double(x_mm, y_mm))
  s1 = s1/s1.norms() * (1/beam.get_wavelength())
  S = s1 - beam.get_s0()
  reciprocal_space_points = S.rotate_around_origin(
    goniometer.get_rotation_axis(),
    -z_rad)

  d_spacings = 1/reciprocal_space_points.norms()
  dmax = flex.max(d_spacings)
  print dmax
  def load_pars_from_file(self, path=None) :
    """ Use the dxtbx object model to build a GeometryObject hierarchy
        @param path Path to the CSPAD CBF file
    """
    if path is not None : self.path = path

    if self.pbits & 32 : print 'Load file: %s' % self.path

    img = dxtbx.load(self.path)
    cbf = img._cbf_handle
    cbf.find_category("diffrn_source")
    cbf.find_column("type")

    self.dict_of_comments = {
      "TITLE"     : "Geometry parameters of CSPAD",
      "DATE_TIME" : evt_timestamp(),
      "AUTHOR"    : getpass.getuser(),
      "EXPERIMENT": cbf.get_value(),
      "DETECTOR"  : "CSPAD",
      "CALIB_TYPE": "geometry",
      "COMMENT:01": "Table contains the list of geometry parameters for alignment of 2x1 sensors, quads, CSPAD, etc",
      "COMMENT:02": " translation and rotation pars of the object are defined w.r.t. parent object Cartesian frame",
      "PARAM:01"  : "PARENT     - name and version of the parent object",
      "PARAM:02"  : "PARENT_IND - index of the parent object",
      "PARAM:03"  : "OBJECT     - name and version of the object",
      "PARAM:04"  : "OBJECT_IND - index of the new object",
      "PARAM:05"  : "X0         - x-coordinate [um] of the object origin in the parent frame",
      "PARAM:06"  : "Y0         - y-coordinate [um] of the object origin in the parent frame",
      "PARAM:07"  : "Z0         - z-coordinate [um] of the object origin in the parent frame",
      "PARAM:08"  : "ROT_Z      - object design rotation angle [deg] around Z axis of the parent frame",
      "PARAM:09"  : "ROT_Y      - object design rotation angle [deg] around Y axis of the parent frame",
      "PARAM:10"  : "ROT_X      - object design rotation angle [deg] around X axis of the parent frame",
      "PARAM:11"  : "TILT_Z     - object tilt angle [deg] around Z axis of the parent frame",
      "PARAM:12"  : "TILT_Y     - object tilt angle [deg] around Y axis of the parent frame",
      "PARAM:13"  : "TILT_X     - object tilt angle [deg] around X axis of the parent frame"
    }

    self.list_of_geos = []

    detector = img.get_detector()
    hierarchy = detector.hierarchy()

    for q, quad in enumerate(hierarchy):
      for s, sensor in enumerate(quad):
        self.list_of_geos.append(self._load_geo(q,"QUAD:V1",s,"SENS2X1:V1",sensor))

    for q, quad in enumerate(hierarchy):
      self.list_of_geos.append(self._load_geo(0,"CSPAD:V1",q,"QUAD:V1",quad))

    # Add placeholder RAIL and IP vectors, including the XY component of the hierarchy's d0 vector
    go = self._load_geo(0,'RAIL',0,'CSPAD:V1',hierarchy)
    go.move_geo(0,0,-go.z0+1000000) # Placeholder
    self.list_of_geos.append(go)
    self.list_of_geos.append(self._null_geo(0,"IP",0,"RAIL"))

    self._set_relations()
Ejemplo n.º 10
0
def overload(image_file):
  from dxtbx import load
  i = load(image_file)
  data = i.get_raw_data()
  if not isinstance(data, tuple):
    data = (data,)
  detector = i.get_detector()
  for pid, (d, p) in enumerate(zip(data, detector)):
    if max(d) > p.get_trusted_range()[1]:
      return True
  return False
Ejemplo n.º 11
0
def generate_dials_corrections(image_filename, sensor_thickness_mm,
                               energy_ev = None, method=compute_absolute_offset):
  '''Generate equivalent correction tables equivalent to those from XDS, but using
  the equations above.'''

  from dxtbx import load
  from scitbx import matrix
  from scitbx.array_family import flex
  import math
  import random
  import os

  image = load(image_filename)

  beam = matrix.col(image.get_beam().get_s0()).normalize()

  if energy_ev:
    energy_kev = energy_ev * 0.001
  else:
    wavelength = image.get_beam().get_wavelength()
    energy_kev = 12.3985 / wavelength

  mu = derive_absorption_coefficient_Si(energy_kev)
  d = image.get_detector()[0]
  fast = matrix.col(d.get_fast_axis())
  slow = matrix.col(d.get_slow_axis())
  normal = matrix.col(d.get_normal())
  origin = matrix.col(d.get_origin())
  distance = origin.dot(normal)
  offset = distance * normal - origin
  offset_fast = offset.dot(fast)
  offset_slow = offset.dot(slow)
  pixel_size = d.get_pixel_size()

  # this is in order slow, fast i.e. C order
  image_size = image.get_raw_data().focus()

  F = fast * pixel_size[0]
  S = slow * pixel_size[1]

  fast_parallax = flex.double(flex.grid(image_size))
  slow_parallax = flex.double(flex.grid(image_size))

  for i in range(image_size[0]):
    for j in range(image_size[1]):
      p = (origin + i * S + j * F).normalize()
      theta = p.angle(normal)
      dot_f = - p.dot(fast)
      dot_s = - p.dot(slow)
      offset = method(sensor_thickness_mm, theta, mu)
      fast_parallax[i, j] = dot_f * offset
      slow_parallax[i, j] = dot_s * offset

  return fast_parallax, slow_parallax
Ejemplo n.º 12
0
  def load_image(self):
    """ Reads raw image file and extracts data for conversion into pickle
        format. Also estimates gain if turned on."""
    # Load raw image or image pickle

    try:
      with misc.Capturing() as junk_output:
        loaded_img = dxtbx.load(self.raw_img)
    except IOError, e:
      loaded_img = None
      pass
Ejemplo n.º 13
0
def saturation(image_file):
  from dxtbx import load
  i = load(image_file)
  d = i.get_detector()
  raw_data = i.get_raw_data()
  if not isinstance(raw_data, tuple):
    raw_data = (raw_data,)
  if i.get_scan() is None:
    return 0, \
         max([max(raw_data[pid]) / d[pid].get_trusted_range()[1] for pid in xrange(len(d))])
  else:
    return i.get_scan().get_image_range()[0], \
         max([max(raw_data[pid]) / d[pid].get_trusted_range()[1] for pid in xrange(len(d))])
Ejemplo n.º 14
0
    def set_general_variables(self):

        self.frame = load(self.raw)
        self.detector = self.frame.get_detector()
        self.beam = self.frame.get_beam()
        self.s0 = self.beam.get_s0()
        self.gonio = self.frame.get_goniometer()
        self.scan = self.frame.get_scan()

        self.lab_coordinates = flex.vec3_double()
        for panel in self.detector:
            self.beam_center_mm_x, self.beam_center_mm_y = col(panel.get_beam_centre(self.s0))
            pixels = flex.vec2_double(panel.get_image_size())
            mms = panel.pixel_to_millimeter(pixels)
            self.lab_coordinates.extend(panel.get_lab_coord(mms))
            self.Isizex, self.Isizey = panel.get_image_size()
            self.beam_center_x, self.beam_center_y = col(panel.get_beam_centre_px(self.s0))
            self.detector_distance = panel.get_distance()
            thrshim_min, thrshim_max = panel.get_trusted_range()
            self.pixel_size = panel.get_pixel_size()[0]

        self.raw_data = self.frame.get_raw_data()

        if thrshim_min < 0 :
            self.thrshim_min = int(0)
        else:
            self.thrshim_min = thrshim_min

        if thrshim_max > 32767:
            self.thrshim_max = int(32767)
        else:
            self.thrshim_max = int(thrshim_max)

        self.polarization_fraction = self.beam.get_polarization_fraction()
        self.polarization_offset = 0.0
        self.cassette_x = 0.0
        self.cassette_y = 0.0
        self.windim_xmax     = int(self.Isizex)-100          # right border for processed image (pixels)
        self.windim_xmin     = 100          # left border for processed image (pixels)
        self.windim_ymax     = int(self.Isizey)-100     # top border for processed image (pixels)
        self.windim_ymin     = 100          # bottom border for processed image (pixels)
        ### beamstop borders
        self.punchim_xmax    = int(self.Isizex)          # right border of beam stop shadow (pixels)
        self.punchim_xmin    = int(self.beam_center_x)-80          # left border of beam stop shadow (pixels)
        self.punchim_ymax    = int(self.beam_center_y)+100          # top border of beam stop shadow (pixels)
        self.punchim_ymin    = int(self.beam_center_y)-40          # bottom border of beam stop shadow (pixels)
        self.mode_filter_footprint = int(20)

        return
Ejemplo n.º 15
0
def model(dials_regression):
    filename = os.path.join(dials_regression, "image_examples", "XDS",
                            "XPARM.XDS")

    models = dxtbx.load(filename)
    detector = models.get_detector()
    assert len(detector) == 1
    detector = detector[0]
    t0 = 0.320

    table = attenuation_coefficient.get_table("Si")
    mu = table.mu_at_angstrom(models.get_beam().get_wavelength()) / 10.0
    pixel_size = detector.get_pixel_size()

    return {"mu": mu, "t0": t0, "detector": detector, "pixel_size": pixel_size}
Ejemplo n.º 16
0
def run(argv=None):
  if argv is None:
    argv = sys.argv[1:]

  command_line = libtbx.option_parser.option_parser(
    usage="%s files" % libtbx.env.dispatcher_name).process(args=argv)

  paths = command_line.args
  if len(paths) <= 0:
    raise Usage("No files specified")

  for path in paths:
    # Load the metrology dictionary, containting basis shifts for each item in the hierarchy
    metro = cbf_file_to_basis_dict(path)

    # Remove from the hiearchy all but the central sensors (sensor 1 of each quadrant).
    # Need to remove the sesnor basis shifts and the corresponding asic shifts
    for quad in range(4):
      for sensor in [0,2,3,4,5,6,7]:
        metro.pop((0,quad,sensor))
        for asic in range(2):
          metro.pop((0,quad,sensor,asic))

    # Renumber the sensors to 0 instead of 1
    for key in metro:
      if len(key) == 3:
        detector, quad, sensor = key
        metro[(detector,quad,0)] = metro.pop(key)
      elif len(key) == 4:
        detector, quad, sensor, asic = key
        metro[(detector,quad,0,asic)] = metro.pop(key)

    # Build the tiles dictionary for only sensor 1 of each quadrant.  Rename that sensor to zero.
    img = dxtbx.load(path)
    tiles = {}
    for quad in range(4):
      src_sensor = 1
      dest_sensor = 0
      for asic in range(2):
        tiles[(0,quad,dest_sensor,asic)] = img.get_raw_data()[(quad*16)+(src_sensor*2)+asic] # FIXME get the panel ID from dxtbx

    destpath = os.path.splitext(path)[0] + "_pinwheel.cbf"

    hierarchy = img.get_detector().hierarchy()
    beam = img.get_beam()

    # write the result.  Have to call abs on the root distance because of a bug with the hierarchy matrix.
    write_cspad_cbf(tiles, metro, 'cbf', None, destpath, beam.get_wavelength(), hierarchy.get_distance())
Ejemplo n.º 17
0
def test_custom_scheme_handler(registry):
    class SchemeHandler(Format):
        schemes = ["scheme"]

        @classmethod
        def understand(cls, endpoint):
            assert endpoint.startswith("scheme://")
            return True

    assert not Registry.get_format_class_for_file("scheme://something")
    registry.register(SchemeHandler)
    assert Registry.get_format_class_for_file(
        "scheme://something") is SchemeHandler
    # Check dxtbx.load
    instance = dxtbx.load("scheme://something")
    assert isinstance(instance, SchemeHandler)
Ejemplo n.º 18
0
def read_image(in_image):
  from scitbx.array_family import flex
  import binascii
  import os
  from dxtbx import load

  assert(os.path.exists(in_image))

  start_tag = binascii.unhexlify('0c1a04d5')

  data = gz_open(in_image, 'rb').read()
  data_offset = data.find(start_tag)
  cbf_header = data[:data_offset]
  pixel_values = load(in_image).get_raw_data()

  return pixel_values, cbf_header
Ejemplo n.º 19
0
def read_image(in_image):
    from scitbx.array_family import flex
    import binascii
    import os
    from dxtbx import load

    assert (os.path.exists(in_image))

    start_tag = binascii.unhexlify('0c1a04d5')

    data = gz_open(in_image, 'rb').read()
    data_offset = data.find(start_tag)
    cbf_header = data[:data_offset]
    pixel_values = load(in_image).get_raw_data()

    return pixel_values, cbf_header
Ejemplo n.º 20
0
def read_image_to_flex_array(in_image):
    '''Looks like this works *only* for CBF images from a Pilatus detector;
  oh well - should still do something useful.'''
    from scitbx.array_family import flex
    import binascii
    import os
    from dxtbx import load

    assert (os.path.exists(in_image))

    start_tag = binascii.unhexlify('0c1a04d5')

    data = open(in_image, 'rb').read()
    data_offset = data.find(start_tag)
    cbf_header = data[:data_offset]
    pixel_values = load(in_image).get_raw_data()

    return pixel_values, cbf_header
Ejemplo n.º 21
0
def read_image_to_flex_array(in_image):
  '''Looks like this works *only* for CBF images from a Pilatus detector;
  oh well - should still do something useful.'''
  from scitbx.array_family import flex
  import binascii
  import os
  from dxtbx import load

  assert(os.path.exists(in_image))

  start_tag = binascii.unhexlify('0c1a04d5')

  data = open(in_image, 'rb').read()
  data_offset = data.find(start_tag)
  cbf_header = data[:data_offset]
  pixel_values = load(in_image).get_raw_data()

  return pixel_values, cbf_header
Ejemplo n.º 22
0
def test_cspad_cbf_in_memory(dials_regression, run_in_tmpdir,
                             composite_output):
    # Check the data files for this test exist
    image_path = os.path.join(
        dials_regression,
        "image_examples",
        "LCLS_cspad_nexus",
        "idx-20130301060858801.cbf",
    )
    assert os.path.isfile(image_path)

    with open("process_lcls.phil", "w") as f:
        f.write(cspad_cbf_in_memory_phil)

    params = phil_scope.fetch(parse(file_name="process_lcls.phil")).extract()
    params.output.experiments_filename = None
    params.output.composite_output = composite_output
    if composite_output:
        processor = Processor(params, composite_tag="memtest")
    else:
        processor = Processor(params)
    mem_img = dxtbx.load(image_path)
    raw_data = mem_img.get_raw_data(
    )  # cache the raw data to prevent swig errors
    mem_img = FormatCBFCspadInMemory(mem_img._cbf_handle)
    mem_img._raw_data = raw_data
    mem_img._cbf_handle = None  # drop the file handle to prevent swig errors
    imgset = ImageSet(ImageSetData(MemReader([mem_img]), None))
    imgset.set_beam(mem_img.get_beam())
    imgset.set_detector(mem_img.get_detector())
    experiments = ExperimentListFactory.from_imageset_and_crystal(imgset, None)
    processor.process_experiments("20130301060858801",
                                  experiments)  # index/integrate the image
    if composite_output:
        processor.finalize()
        result = "idx-memtest_integrated.refl"
    else:
        result = "idx-20130301060858801_integrated.refl"
    n_refls = list(range(
        140, 152))  # large ranges to handle platform-specific differences
    table = flex.reflection_table.from_file(result)
    assert len(table) in n_refls, len(table)
    assert "id" in table
    assert (table["id"] == 0).count(False) == 0
Ejemplo n.º 23
0
  def calculate_parameters(self, experiments=None):
    ''' Image modification for current cctbx.xfel '''

    if not experiments:
      # If data are given, apply modifications as specified below
      error = 'IOTA IMPORT ERROR: Experiment list not found!'
      return None, error
    else:
      error = []

      # Calculate auto-threshold
      # TODO: Revisit this; I REALLY don't like it.
      if self.auto_threshold:
        beamX = self.img_object.final['beamX']
        beamY = self.img_object.final['beamY']
        px_size = self.img_object.final['pixel']
        try:
          img = dxtbx.load(self.img_object.img_path)
          raw_data = img.get_raw_data()
          beam_x_px = int(beamX / px_size)
          beam_y_px = int(beamY / px_size)
          data_array = raw_data.as_numpy_array().astype(float)
          self.center_int = np.nanmax(data_array[beam_y_px - 20:beam_y_px + 20,
                                                 beam_x_px - 20:beam_x_px + 20])
        except Exception as e:
          error.append('IOTA IMPORT ERROR: Auto-threshold failed! {}'.format(e))

      # Estimate gain (or set gain to 1.00 if cannot calculate)
      if self.estimate_gain:
        with util.Capturing() as junk_output:
          try:
            assert self.img_object.experiments   # Must have experiments here
            imageset = self.img_object.experiments.extract_imagesets()[0]
            self.img_object.gain = estimate_gain(imageset)
          except Exception as e:
            error.append('IOTA IMPORT ERROR: Estimate gain failed! '.format(e))

      # Collect error messages for logging
      if error:
        error_message = '\n'.join(error)
      else:
        error_message = None

      return experiments, error_message
    def __init__(self):
        import os
        dials_regression = libtbx.env.dist_path('dials_regression')
        filename = os.path.join(dials_regression, 'image_examples', 'XDS',
                                'XPARM.XDS')

        import dxtbx
        models = dxtbx.load(filename)
        self.detector = models.get_detector()
        self.beam = models.get_beam()
        assert (len(self.detector) == 1)
        self.t0 = 0.320
        from cctbx.eltbx import attenuation_coefficient
        table = attenuation_coefficient.get_table("Si")
        self.mu = table.mu_at_angstrom(self.beam.get_wavelength()) / 10.0
        self.distance = self.detector[0].get_distance()
        self.origin = self.detector[0].get_ray_intersection(
            self.detector[0].get_normal())[1]
        self.pixel_size = self.detector[0].get_pixel_size()
Ejemplo n.º 25
0
def saturation(image_file):
    i = load(image_file)
    d = i.get_detector()
    raw_data = i.get_raw_data()
    if not isinstance(raw_data, tuple):
        raw_data = (raw_data, )
    if i.get_scan() is None:
        return (
            0,
            max(
                max(raw_data[pid]) / detector.get_trusted_range()[1]
                for pid, detector in enumerate(d)),
        )
    else:
        return (
            i.get_scan().get_image_range()[0],
            max(
                max(raw_data[pid]) / detector.get_trusted_range()[1]
                for pid, detector in enumerate(d)),
        )
Ejemplo n.º 26
0
def main(filename, threshold, images):
    """Read and sum all images, define those pixels which are POSITIVE but which
    come out below threshold as mask, write this mask in a format useful for
    DIALS."""

    image_data = None

    for image in images:
        i = load(image)
        if image_data is None:
            image_data = i.get_raw_data()
        else:
            image_data += i.get_raw_data()

    mask_inverse = (image_data >= 0) & (image_data < threshold)
    mask = ~mask_inverse
    with open(filename, "wb") as fh:
        pickle.dump((mask, ), fh, pickle.HIGHEST_PROTOCOL)

    print(f"Mask written to {filename}")
Ejemplo n.º 27
0
class Test(object):
    def __init__(self):
        import os
        import libtbx.load_env
        try:
            dials_regression = libtbx.env.dist_path('dials_regression')
        except KeyError, e:
            print 'FAIL: dials_regression not configured'
            exit(0)

        filename = os.path.join(dials_regression, 'image_examples', 'XDS',
                                'XPARM.XDS')

        import dxtbx
        models = dxtbx.load(filename)
        self.detector = models.get_detector()
        assert (len(self.detector) == 1)
        self.attlen = 0.252500934883
        self.distance = self.detector[0].get_distance()
        self.origin = self.detector[0].get_ray_intersection(
            self.detector[0].get_normal())
Ejemplo n.º 28
0
def test_run(dials_regression):
    filename = os.path.join(dials_regression, "image_examples", "XDS", "XPARM.XDS")

    models = dxtbx.load(filename)
    detector = models.get_detector()
    assert len(detector) == 1
    detector = detector[0]
    attlen = 0.252500934883
    distance = detector.get_distance()
    origin = detector.get_ray_intersection(detector.get_normal())

    for i in range(10000):
        # Generate some random coordinates
        xy = matrix.col((random.uniform(-1000, 1000), random.uniform(-1000, 1000)))

        # Do the forward and reverse corrections
        corr_gold = matrix.col(correct_gold(detector, attlen, xy))
        corr = matrix.col(parallax_correction(distance, attlen, origin, xy))
        corr_inv = matrix.col(parallax_correction_inv(distance, attlen, origin, corr))

        # Check the values
        assert abs(corr_gold - corr) < 1e-7
        assert abs(corr_inv - xy) < 1e-3
Ejemplo n.º 29
0
def saturation(image_file):
    from dxtbx import load

    i = load(image_file)
    d = i.get_detector()
    raw_data = i.get_raw_data()
    if not isinstance(raw_data, tuple):
        raw_data = (raw_data, )
    if i.get_scan() is None:
        return (
            0,
            max([
                max(raw_data[pid]) / d[pid].get_trusted_range()[1]
                for pid in xrange(len(d))
            ]),
        )
    else:
        return (
            i.get_scan().get_image_range()[0],
            max([
                max(raw_data[pid]) / d[pid].get_trusted_range()[1]
                for pid in xrange(len(d))
            ]),
        )
Ejemplo n.º 30
0
def main(filename, threshold, images):
    '''Read and sum all images, define those pixels which are POSITIVE but which
  come out below threshold as mask, write this mask in a format useful for
  DIALS.'''

    from dxtbx import load
    from dials.array_family import flex
    from cPickle import dump

    image_data = None

    for image in images:
        i = load(image)
        d = i.get_raw_data()
        if image_data is None:
            image_data = i.get_raw_data()
        else:
            image_data += i.get_raw_data()

    mask_inverse = (image_data >= 0) & (image_data < threshold)
    mask = ~mask_inverse
    dump((mask, ), open(filename, 'w'))

    print 'Mask written to %s' % filename
Ejemplo n.º 31
0
def resolution_corners(frame):
  '''Compute the resolution limit corresponding to the corners of the detector
  surface.'''

  import math
  from scitbx import matrix

  detector = frame.get_detector()
  beam = frame.get_beam()

  nfast, nslow = map(int, detector.get_image_size())
  dfast, dslow = detector.get_pixel_size()
  F = matrix.col(detector.get_fast_axis())
  S = matrix.col(detector.get_slow_axis())
  origin = matrix.col(detector.get_origin())

  s0 = -1 * matrix.col(beam.get_direction())

  for ds in 0, 1:
    for df in 0, 1:
      corner = origin + nfast * dfast * F * df + nslow * dslow * S * ds
      theta = 0.5 * corner.angle(s0)
      print '%.3f' % (beam.get_wavelength() / (2 * math.sin(theta)))

if __name__ == '__main__':

  import sys
  import dxtbx

  resolution_corners(dxtbx.load(sys.argv[1]))
Ejemplo n.º 32
0
def run(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    command_line = (
        libtbx.option_parser.option_parser(
            usage="%s [-v] [-c] [-s] [-w wavelength] [-d distance] [-p pixel_size] [-x beam_x] [-y beam_y] [-o overload] files"
            % libtbx.env.dispatcher_name
        )
        .option(
            None,
            "--verbose",
            "-v",
            action="store_true",
            default=False,
            dest="verbose",
            help="Print more information about progress",
        )
        .option(
            None,
            "--crop",
            "-c",
            action="store_true",
            default=False,
            dest="crop",
            help="Crop the image such that the beam center is in the middle",
        )
        .option(
            None,
            "--skip_converted",
            "-s",
            action="store_true",
            default=False,
            dest="skip_converted",
            help="Skip converting if an image already exist that matches the destination file name",
        )
        .option(
            None,
            "--wavelength",
            "-w",
            type="float",
            default=None,
            dest="wavelength",
            help="Override the image's wavelength (angstroms)",
        )
        .option(
            None,
            "--distance",
            "-d",
            type="float",
            default=None,
            dest="distance",
            help="Override the detector distance (mm)",
        )
        .option(
            None,
            "--pixel_size",
            "-p",
            type="float",
            default=None,
            dest="pixel_size",
            help="Override the detector pixel size (mm)",
        )
        .option(
            None,
            "--beam_x",
            "-x",
            type="float",
            default=None,
            dest="beam_center_x",
            help="Override the beam x position (pixels)",
        )
        .option(
            None,
            "--beam_y",
            "-y",
            type="float",
            default=None,
            dest="beam_center_y",
            help="Override the beam y position (pixels)",
        )
        .option(
            None,
            "--overload",
            "-o",
            type="float",
            default=None,
            dest="overload",
            help="Override the detector overload value (ADU)",
        )
    ).process(args=argv)

    paths = command_line.args
    if len(paths) <= 0:
        raise Usage("No files specified")

    for imgpath in paths:
        destpath = os.path.join(os.path.dirname(imgpath), os.path.splitext(os.path.basename(imgpath))[0] + ".pickle")
        if command_line.options.skip_converted and os.path.isfile(destpath):
            if command_line.options.verbose:
                print "Skipping %s, file exists" % imgpath
                continue

        if command_line.options.verbose:
            print "Converting %s to %s..." % (imgpath, destpath)

        try:
            img = dxtbx.load(imgpath)
        except IOError:
            img = None
            pass

        if img is None:
            import numpy as np

            try:
                raw_data = np.loadtxt(imgpath)

                from scitbx.array_family import flex

                raw_data = flex.double(raw_data.astype(np.double))
            except ValueError:
                raise Usage("Couldn't load %s, no supported readers" % imgpath)

            detector = None
            beam = None
            scan = None
        else:
            raw_data = img.get_raw_data()
            detector = img.get_detector()
            beam = img.get_beam()
            scan = img.get_scan()

        if detector is None:
            if command_line.options.distance is None:
                raise Usage("Can't get distance from image. Override with -d")
            if command_line.options.pixel_size is None:
                raise Usage("Can't get pixel size from image. Override with -p")
            if command_line.options.overload is None:
                raise Usage("Can't get overload value from image. Override with -o")
            distance = command_line.options.distance
            pixel_size = command_line.options.pixel_size
            overload = command_line.options.overload
        else:
            detector = detector[0]
            if command_line.options.distance is None:
                distance = detector.get_distance()
            else:
                distance = command_line.options.distance

            if command_line.options.pixel_size is None:
                pixel_size = detector.get_pixel_size()[0]
            else:
                pixel_size = command_line.options.pixel_size

            if command_line.options.overload is None:
                overload = detector.get_trusted_range()[1]
            else:
                overload = command_line.options.overload

        if beam is None:
            if command_line.options.wavelength is None:
                raise Usage("Can't get wavelength from image. Override with -w")
            wavelength = command_line.options.wavelength
        else:
            if command_line.options.wavelength is None:
                wavelength = beam.get_wavelength()
            else:
                wavelength = command_line.options.wavelength

        if beam is None and detector is None:
            if command_line.options.beam_center_x is None:
                print "Can't get beam x position from image. Using image center. Override with -x"
                beam_x = raw_data.focus()[0] * pixel_size
            else:
                beam_x = command_line.options.beam_center_x * pixel_size

            if command_line.options.beam_center_y is None:
                print "Can't get beam y position from image. Using image center. Override with -y"
                beam_y = raw_data.focus()[1] * pixel_size
            else:
                beam_y = command_line.options.beam_center_y * pixel_size
        else:
            if command_line.options.beam_center_x is None:
                beam_x = detector.get_beam_centre(beam.get_s0())[0]
            else:
                beam_x = command_line.options.beam_center_x * pixel_size

            if command_line.options.beam_center_y is None:
                beam_y = detector.get_beam_centre(beam.get_s0())[1]
            else:
                beam_y = command_line.options.beam_center_y * pixel_size

        if scan is None:
            timestamp = None
        else:
            msec, sec = math.modf(scan.get_epochs()[0])
            timestamp = evt_timestamp((sec, msec))

        data = dpack(
            data=raw_data,
            distance=distance,
            pixel_size=pixel_size,
            wavelength=wavelength,
            beam_center_x=beam_x,
            beam_center_y=beam_y,
            ccd_image_saturation=overload,
            saturated_value=overload,
            timestamp=timestamp,
        )

        if scan is not None:
            osc_start, osc_range = scan.get_oscillation()
            if osc_start != osc_range:
                data["OSC_START"] = osc_start
                data["OSC_RANGE"] = osc_range

                data["TIME"] = scan.get_exposure_times()[0]

        if command_line.options.crop:
            data = crop_image_pickle(data)
        easy_pickle.dump(destpath, data)
Ejemplo n.º 33
0
spot_par.spotfinder.threshold.dispersion.sigma_background = 6.
spot_par.spotfinder.filter.min_spot_size = 2
spot_par.spotfinder.force_2d = True
spot_par.spotfinder.lookup.mask = mask_file

#spot_par_moder.spotfinder.threshold.dispersion.global_threshold = 56.
#spot_par_moder.spotfinder.threshold.dispersion.gain = 28.
#spot_par_moder.spotfinder.threshold.dispersion.kernel_size = [1,1]
#spot_par_moder.spotfinder.threshold.dispersion.sigma_strong = 2.5
#spot_par_moder.spotfinder.threshold.dispersion.sigma_background = 2.5
#spot_par_moder.spotfinder.filter.min_spot_size = 1
#spot_par_moder.spotfinder.force_2d = True
#spot_par_moder.spotfinder.lookup.mask = mask_file
#try_fft1d = False

loader = dxtbx.load(img_f)

#info_f = utils.open_flex("../index/run62_idx_processed.pkl")
#hit_idx = info_f.keys()

ENERGIES = [parameters.ENERGY_LOW,
            parameters.ENERGY_HIGH]  # colors of the beams
FF = [5000, None]  # Setting structure factors takes long time in nanoBragg, so
# unless you want energy-dependent structure factors
# you need only provide one number -or- one structure factor flex miller table
# and the computer will know to preserve that for all beam colors
FLUX = [1e14, 1e14]  # fluxes of the beams

#from cxid9114.sim import scattering_factors
#Fcalcs = scattering_factors.get_scattF( parameters.WAVELEN_LOW,
#                                     pdb_name="../sim/4bs7.pdb",
Ejemplo n.º 34
0
from __future__ import division
import sys, os, dxtbx
from xfel.cxi.cspad_ana import cspad_tbx
from libtbx import easy_pickle
from libtbx import easy_mp
from xfel.command_line.cxi_image2pickle import crop_image_pickle

# Jiffy script to dump SACLA data processed by Cheetah into image pickles.  Usage:
# libtbx.python dump_sacla_data.py <path to h5 file> <destination directory for pickles>.
# Uses 4 processors, hardcoded at the end of the file

data_path = sys.argv[1]
dest_dir = sys.argv[2]

data = dxtbx.load(data_path)
detector = data.get_detector()
distance = detector[0].get_directed_distance()
beam = data.get_beam()
wavelength = beam.get_wavelength()
pixel_size = detector[0].get_pixel_size()[0]
beam_x, beam_y = detector[0].get_beam_centre_px(beam.get_s0())
beam_x *= pixel_size
beam_y += 0  #earlier work required a 3 pixel shift based on powder pattern/fit to unit cell
beam_y *= pixel_size
overload = detector[0].get_trusted_range()[1]
from xfel.cxi.cspad_ana.cspad_tbx import xpp_active_areas
active_areas = xpp_active_areas["Sacla.MPCCD.8tile"]["active_areas"]
# the active areas are already determined for the cropped size
# (ran once without active areas, then measured cropped active areas on image viewer)

dest_base = os.path.basename(os.path.splitext(data_path)[0])
Ejemplo n.º 35
0
def generate_xds_corrections(image_filename, sensor_thickness_mm,
                             energy_ev=None):
  '''Generate an XYCORR input file from an image header via dxtbx, noting
  well that this will *tell lies* as the image is rescaled to give a 1:1
  correction table in 0.025 rather than 0.1 (original) pixel increments.'''

  from dxtbx import load
  from scitbx import matrix

  image = load(image_filename)

  beam = matrix.col(image.get_beam().get_s0()).normalize()
  if energy_ev:
    wavelength = 12398.5 / energy_ev
  else:
    wavelength = image.get_beam().get_wavelength()
    energy_ev = 12398.5 / wavelength

  from mu_Si import derive_absorption_coefficient_Si
  silicon = derive_absorption_coefficient_Si(0.001 * energy_ev)
  d = image.get_detector()[0]
  fast = matrix.col(d.get_fast_axis())
  slow = matrix.col(d.get_slow_axis())
  normal = matrix.col(d.get_normal())
  origin = matrix.col(d.get_origin())
  distance = origin.dot(normal)
  offset = distance * normal - origin
  offset_fast = offset.dot(fast)
  offset_slow = offset.dot(slow)

  trusted = d.get_trusted_range()

  pixel_size = d.get_pixel_size()

  # this is in order slow, fast i.e. C order
  image_size = image.get_raw_data().focus()

  open('XDS.INP', 'w').write(xds_template % {
    'overload':trusted[1],
    'fast_x':fast.elems[0],
    'fast_y':fast.elems[1],
    'fast_z':fast.elems[2],
    'slow_x':slow.elems[0],
    'slow_y':slow.elems[1],
    'slow_z':slow.elems[2],
    'n_fast':image_size[1] * 4,
    'n_slow':image_size[0] * 4,
    'pixel_fast':pixel_size[0] / 4.0,
    'pixel_slow':pixel_size[1] / 4.0,
    'distance':distance,
    'wavelength':wavelength,
    'beam_x':beam.elems[0],
    'beam_y':beam.elems[1],
    'beam_z':beam.elems[2],
    'silicon':silicon,
    'thickness':sensor_thickness_mm,
    'origin_fast':offset_fast / (pixel_size[0] / 4.0),
    'origin_slow':offset_slow / (pixel_size[1] / 4.0)
    })

  output = run_job('xds_par')

  # now read the correction tables in and scale to mm - recall pixel size / 4
  # above..

  x_corrections_parallax = read_xds_calibration_file(
    'X-CORRECTIONS.cbf').as_double() * (pixel_size[1] / 40.0)
  y_corrections_parallax = read_xds_calibration_file(
    'Y-CORRECTIONS.cbf').as_double() * (pixel_size[0] / 40.0)

  return x_corrections_parallax, y_corrections_parallax
Ejemplo n.º 36
0
def run():
  parser = OptionParser(
    phil = phil_scope)

  params, options = parser.parse_args(show_diff_phil=True)
  assert params.input.single_img is not None
  assert params.output_dir is not None

  # load the image
  img = dxtbx.load(params.input.single_img)
  imgset = MemImageSet([img])
  datablock = DataBlockFactory.from_imageset(imgset)[0]

  spotfinder = SpotFinderFactory.from_parameters(params)
  reflections = spotfinder(datablock)

  base_name = os.path.splitext(params.input.single_img)[0]
  reflections.as_pickle(os.path.join(params.output_dir, base_name + "_strong.pickle"))

  # DGW commented out as reflections.minimum_number_of_reflections no longer exists
  #if len(reflections) < params.refinement.reflections.minimum_number_of_reflections:
  #  print "Not enough spots to index"
  #  return

  # create the spot finder

  print "Spotfinder spots found:", len(reflections)

  if params.indexing.method == "fft3d":
    from dials.algorithms.indexing.fft3d import indexer_fft3d as indexer
  elif params.indexing.method == "fft1d":
    from dials.algorithms.indexing.fft1d import indexer_fft1d as indexer
  elif params.method == "real_space_grid_search":
    from dials.algorithms.indexing.real_space_grid_search \
         import indexer_real_space_grid_search as indexer
  try:
    idxr = indexer(reflections, [imgset], params=params.indexing)
  except (RuntimeError, Sorry) as e:
    print str(e)
    return

  indexed = idxr.refined_reflections
  experiments = idxr.refined_experiments
  #from dxtbx.model.experiment.experiment_list import ExperimentListDumper
  #dump = ExperimentListDumper(experiments)
  #dump.as_json(os.path.join(params.output_dir, base_name + "_experiments.json"))
  indexed.as_pickle(os.path.join(params.output_dir, base_name + "_indexed.pickle"))

  refiner = RefinerFactory.from_parameters_data_experiments(
    params, indexed, experiments)

  refiner.run()
  refined_experiments = refiner.get_experiments()
  #dump = ExperimentListDumper(refined_experiments)
  #dump.as_json(os.path.join(params.output_dir, base_name + "_refined.json"))

  # Compute the profile model
  # Predict the reflections
  # Match the predictions with the reference
  # Create the integrator
  reference = indexed

  reference = process_reference(reference)
  profile_model = ProfileModelFactory.create(params, refined_experiments, reference)
  predicted = flex.reflection_table.from_predictions_multi(
    refined_experiments,
    dmin=params.prediction.dmin,
    dmax=params.prediction.dmax,
    margin=params.prediction.margin,
    force_static=params.prediction.force_static)
  predicted.match_with_reference(reference)
  integrator = IntegratorFactory.create(params, experiments, profile_model, predicted)

  # Integrate the reflections
  integrated = integrator.integrate()
  integrated.as_pickle(os.path.join(params.output_dir, base_name + "_integrated.pickle"))
Ejemplo n.º 37
0
  pool = Pool(processes=nproc)

  for line in lines:

    # parse the input file line into a diffuse image file name and scale factor
    words = line.split()

    imgname = os.path.join(procpath+"/"+words[1])
    scale = float(words[2])

    print "processing file %s with scale factor %f"%(imgname,scale)
#    I = QuickImage(imgname)
#    I.read()
#    DATA = I.linearintdata
    import dxtbx
    img = dxtbx.load(imgname)
    detector = img.get_detector()
    beam = img.get_beam()
    scan = img.get_scan()
    gonio = img.get_goniometer()
 
    print "transform pixel numbers to mm positions and rotational degrees"
#    from iotbx.detectors.context.spot_xy_convention import spot_xy_convention
#    SF = results.spotfinder_results
#    SXYC = spot_xy_convention(SF.pixel_size*SF.size1,SF.pixel_size*SF.size2)
#    from spotfinder.math_support import pixels_to_mmPos
#    this_frame_phi_deg = I.deltaphi/2.0+I.osc_start

    print "Creating pixel map..."
    t0 = time()
    """
Ejemplo n.º 38
0
if (__name__ == "__main__"):
  if len(sys.argv) == 1 or '-h' in sys.argv or '--help' in sys.argv or '-c' in sys.argv:
    raise Usage("%s files"%libtbx.env.dispatcher_name)

  files = [arg for arg in sys.argv[1:] if os.path.isfile(arg)]
  arguments = [arg for arg in sys.argv[1:] if not os.path.isfile(arg)]

  for file in files:
    message="""Based on the file %s,
    this program will compute incremental quadrant translations to circularize
    powder rings on the inner four sensors.  The algorithm treats each quadrant
    independently and scores based on self-correlation upon 45-degree rotation.
    """%file
    print message

    image = dxtbx.load(file)
    detector = image.get_detector()
    beam = image.get_beam()

    from xfel.metrology.quadrant import one_panel
    for i_quad, quad in enumerate(detector.hierarchy()):
      # find panel closest to the beam center
      panels = []
      def recursive_get_panels(group):
        if hasattr(group, "children"):
          for child in group:
            recursive_get_panels(child)
        else:
          panels.append(group)
      recursive_get_panels(quad)
Ejemplo n.º 39
0
def run(argv=None):
  if argv is None:
    argv = sys.argv[1:]

  command_line = (libtbx.option_parser.option_parser(
    usage="%s [-v] [-c] [-s] [-w wavelength] [-d distance] [-p pixel_size] [-x beam_x] [-y beam_y] [-o overload] files" % libtbx.env.dispatcher_name)
                  .option(None, "--verbose", "-v",
                          action="store_true",
                          default=False,
                          dest="verbose",
                          help="Print more information about progress")
                  .option(None, "--crop", "-c",
                          action="store_true",
                          default=False,
                          dest="crop",
                          help="Crop the image such that the beam center is in the middle")
                  .option(None, "--skip_converted", "-s",
                          action="store_true",
                          default=False,
                          dest="skip_converted",
                          help="Skip converting if an image already exist that matches the destination file name")
                  .option(None, "--wavelength", "-w",
                          type="float",
                          default=None,
                          dest="wavelength",
                          help="Override the image's wavelength (angstroms)")
                  .option(None, "--distance", "-d",
                          type="float",
                          default=None,
                          dest="distance",
                          help="Override the detector distance (mm)")
                  .option(None, "--pixel_size", "-p",
                          type="float",
                          default=None,
                          dest="pixel_size",
                          help="Override the detector pixel size (mm)")
                  .option(None, "--beam_x", "-x",
                          type="float",
                          default=None,
                          dest="beam_center_x",
                          help="Override the beam x position (pixels)")
                  .option(None, "--beam_y", "-y",
                          type="float",
                          default=None,
                          dest="beam_center_y",
                          help="Override the beam y position (pixels)")
                  .option(None, "--overload", "-o",
                          type="float",
                          default=None,
                          dest="overload",
                          help="Override the detector overload value (ADU)")
                  ).process(args=argv)

  paths = command_line.args
  if len(paths) <= 0:
    raise Usage("No files specified")

  for imgpath in paths:
    if command_line.options.verbose:
      print "Reading %s"%(imgpath)

    try:
      img = dxtbx.load(imgpath)
    except IOError:
      img = None
      pass

    if img is None:
      import numpy as np
      try:
        raw_data = np.loadtxt(imgpath)

        from scitbx.array_family import flex
        raw_data = flex.double(raw_data.astype(np.double))
      except ValueError:
        raise Usage("Couldn't load %s, no supported readers"%imgpath)

      detector = None
      beam = None
      scan = None
      is_multi_image = False
    else:
      try:
        raw_data = img.get_raw_data()
        is_multi_image = False
      except TypeError:
        raw_data = img.get_raw_data(0)
        is_multi_image = True
      detector = img.get_detector()
      beam = img.get_beam()
      scan = img.get_scan()


    if detector is None:
      if command_line.options.distance is None:
        raise Usage("Can't get distance from image. Override with -d")
      if command_line.options.pixel_size is None:
        raise Usage("Can't get pixel size from image. Override with -p")
      if command_line.options.overload is None:
        raise Usage("Can't get overload value from image. Override with -o")
      distance = command_line.options.distance
      pixel_size = command_line.options.pixel_size
      overload = command_line.options.overload
    else:
      detector = detector[0]
      if command_line.options.distance is None:
        distance   = detector.get_distance()
      else:
        distance = command_line.options.distance

      if command_line.options.pixel_size is None:
        pixel_size = detector.get_pixel_size()[0]
      else:
        pixel_size = command_line.options.pixel_size

      if command_line.options.overload is None:
        overload   = detector.get_trusted_range()[1]
      else:
        overload = command_line.options.overload

    if beam is None:
      if command_line.options.wavelength is None:
        raise Usage("Can't get wavelength from image. Override with -w")
      wavelength = command_line.options.wavelength
    else:
      if command_line.options.wavelength is None:
        wavelength = beam.get_wavelength()
      else:
        wavelength = command_line.options.wavelength

    if beam is None and detector is None:
      if command_line.options.beam_center_x is None:
        print "Can't get beam x position from image. Using image center. Override with -x"
        beam_x = raw_data.focus()[0] * pixel_size
      else:
        beam_x = command_line.options.beam_center_x * pixel_size

      if command_line.options.beam_center_y is None:
        print "Can't get beam y position from image. Using image center. Override with -y"
        beam_y = raw_data.focus()[1] * pixel_size
      else:
        beam_y = command_line.options.beam_center_y * pixel_size
    else:
      if command_line.options.beam_center_x is None:
        beam_x = detector.get_beam_centre(beam.get_s0())[0]
      else:
        beam_x = command_line.options.beam_center_x * pixel_size

      if command_line.options.beam_center_y is None:
        beam_y = detector.get_beam_centre(beam.get_s0())[1]
      else:
        beam_y = command_line.options.beam_center_y * pixel_size

    if scan is None:
      timestamp = None
    else:
      msec, sec = math.modf(scan.get_epochs()[0])
      timestamp = evt_timestamp((sec,msec))

    if is_multi_image:
      for i in xrange(img.get_num_images()):
        save_image(command_line, imgpath, scan, img.get_raw_data(i), distance, pixel_size, wavelength, beam_x, beam_y, overload, timestamp, image_number = i)
    else:
      save_image(command_line, imgpath, scan, raw_data, distance, pixel_size, wavelength, beam_x, beam_y, overload, timestamp)
Ejemplo n.º 40
0
def xds_check_indexer_solution(xparm_file,
                               spot_file):
  '''Read XPARM file from XDS IDXREF (assumes that this is in the putative
  correct symmetry, not P1! and test centring operations if present. Note
  that a future version will boost to the putative correct symmetry (or
  an estimate of it) and try this if it is centred. Returns tuple
  (space_group_number, cell).'''

  from dxtbx.serialize.xds import to_crystal as xparm_to_crystal
  cm = xparm_to_crystal(xparm_file)
  sg = cm.get_space_group()
  spacegroup = sg.type().hall_symbol()
  space_group_number = sg.type().number()
  A_inv = cm.get_A().inverse()
  cell = cm.get_unit_cell().parameters()

  import dxtbx
  models = dxtbx.load(xparm_file)
  detector = models.get_detector()
  beam = models.get_beam()
  goniometer = models.get_goniometer()
  scan = models.get_scan()

  from iotbx.xds import spot_xds
  spot_xds_handle = spot_xds.reader()
  spot_xds_handle.read_file(spot_file)

  from cctbx.array_family import flex
  centroids_px = flex.vec3_double(spot_xds_handle.centroid)
  miller_indices = flex.miller_index(spot_xds_handle.miller_index)

  # Convert Pixel coordinate into mm/rad
  x, y, z = centroids_px.parts()
  x_mm, y_mm = detector[0].pixel_to_millimeter(flex.vec2_double(x, y)).parts()
  z_rad = scan.get_angle_from_array_index(z, deg=False)
  centroids_mm = flex.vec3_double(x_mm, y_mm, z_rad)

  # then convert detector position to reciprocal space position

  # based on code in dials/algorithms/indexing/indexer2.py
  s1 = detector[0].get_lab_coord(flex.vec2_double(x_mm, y_mm))
  s1 = s1/s1.norms() * (1/beam.get_wavelength())
  S = s1 - beam.get_s0()
  # XXX what about if goniometer fixed rotation is not identity?
  reciprocal_space_points = S.rotate_around_origin(
    goniometer.get_rotation_axis(),
    -z_rad)

  # now index the reflections
  hkl_float = tuple(A_inv) * reciprocal_space_points
  hkl_int = hkl_float.iround()

  # check if we are within 0.1 lattice spacings of the closest
  # lattice point - a for a random point this will be about 0.8% of
  # the time...
  differences = hkl_float - hkl_int.as_vec3_double()
  dh, dk, dl = [flex.abs(d) for d in differences.parts()]
  tolerance = 0.1
  sel = (dh < tolerance) and (dk < tolerance) and (dl < tolerance)

  is_sys_absent = sg.is_sys_absent(
    flex.miller_index(list(hkl_int.select(sel))))

  total = is_sys_absent.size()
  absent = is_sys_absent.count(True)
  present = total - absent

  # now, if the number of absences is substantial, need to consider
  # transforming this to a primitive basis

  Debug.write('Absent: %d  vs.  Present: %d Total: %d' % \
              (absent, present, total))

  # now see if this is compatible with a centred lattice or suggests
  # a primitive basis is correct

  sd = math.sqrt(absent)

  if (absent - 3 * sd) / total < 0.008:
    # everything is peachy

    return s2l(space_group_number), tuple(cell)

  # ok if we are here things are not peachy, so need to calculate the
  # spacegroup number without the translation operators

  sg_new = sg.build_derived_group(True, False)
  space_group_number_primitive = sg_new.type().number()

  # also determine the best setting for the new cell ...

  symm = crystal.symmetry(unit_cell = cell,
                          space_group = sg_new)

  rdx = symm.change_of_basis_op_to_best_cell()
  symm_new = symm.change_basis(rdx)
  cell_new = symm_new.unit_cell().parameters()

  return s2l(space_group_number_primitive), tuple(cell_new)
Ejemplo n.º 41
0
  def load_image(self):
    """ Reads raw image file and extracts data for conversion into pickle
        format. Also estimates gain if turned on."""
    # Load raw image or image pickle
    try:
      with misc.Capturing() as junk_output:
        loaded_img = dxtbx.load(self.raw_img)
    except IOError:
      loaded_img = None
      pass

    # Extract image information
    if loaded_img is not None:
      raw_data   = loaded_img.get_raw_data()
      detector   = loaded_img.get_detector()[0]
      beam       = loaded_img.get_beam()
      scan       = loaded_img.get_scan()
      distance   = detector.get_distance()
      pixel_size = detector.get_pixel_size()[0]
      overload   = detector.get_trusted_range()[1]
      wavelength = beam.get_wavelength()
      beam_x     = detector.get_beam_centre(beam.get_s0())[0]
      beam_y     = detector.get_beam_centre(beam.get_s0())[1]

      if scan is None:
        timestamp = None
        if abs(beam_x - beam_y) <= 0.1 or self.params.image_conversion.square_mode == "None":
          img_type = 'converted'
        else:
          img_type = 'unconverted'
      else:
        msec, sec = math.modf(scan.get_epochs()[0])
        timestamp = evt_timestamp((sec,msec))

      if self.params.image_conversion.beamstop != 0 or\
         self.params.image_conversion.beam_center.x != 0 or\
         self.params.image_conversion.beam_center.y != 0 or\
         self.params.image_conversion.rename_pickle_prefix != 'Auto' or\
         self.params.image_conversion.rename_pickle_prefix != None:
        img_type = 'unconverted'

      # Assemble datapack
      data = dpack(data=raw_data,
                   distance=distance,
                   pixel_size=pixel_size,
                   wavelength=wavelength,
                   beam_center_x=beam_x,
                   beam_center_y=beam_y,
                   ccd_image_saturation=overload,
                   saturated_value=overload,
                   timestamp=timestamp
                   )

      #print "data: ", type(raw_data)
      #print "pixel size: ", type(pixel_size)
      #print 'wavelength: ', type(wavelength)
      #print "beamX: ", type(beam_x)
      #print "saturation: ", type(overload)
      #print "timestamp: ", type(timestamp)

      #for i in dir(raw_data): print i

      #exit()

      if scan is not None:
        osc_start, osc_range = scan.get_oscillation()
        img_type = 'unconverted'
        if osc_start != osc_range:
          data['OSC_START'] = osc_start
          data['OSC_RANGE'] = osc_range
          data['TIME'] = scan.get_exposure_times()[0]

      # Estimate gain (or set gain to 1.00 if cannot calculate)
      # Cribbed from estimate_gain.py by Richard Gildea
      if self.params.advanced.estimate_gain:
        try:
          from dials.algorithms.image.threshold import KabschDebug
          raw_data = [raw_data]

          gain_value = 1
          kernel_size=(10,10)
          gain_map = [flex.double(raw_data[i].accessor(), gain_value)
                      for i in range(len(loaded_img.get_detector()))]
          mask = loaded_img.get_mask()
          min_local = 0

          # dummy values, shouldn't affect results
          nsigma_b = 6
          nsigma_s = 3
          global_threshold = 0

          kabsch_debug_list = []
          for i_panel in range(len(loaded_img.get_detector())):
            kabsch_debug_list.append(
              KabschDebug(
                raw_data[i_panel].as_double(), mask[i_panel], gain_map[i_panel],
                kernel_size, nsigma_b, nsigma_s, global_threshold, min_local))

          dispersion = flex.double()
          for kabsch in kabsch_debug_list:
            dispersion.extend(kabsch.coefficient_of_variation().as_1d())

          sorted_dispersion = flex.sorted(dispersion)
          from libtbx.math_utils import nearest_integer as nint

          q1 = sorted_dispersion[nint(len(sorted_dispersion)/4)]
          q2 = sorted_dispersion[nint(len(sorted_dispersion)/2)]
          q3 = sorted_dispersion[nint(len(sorted_dispersion)*3/4)]
          iqr = q3-q1

          inlier_sel = (sorted_dispersion > (q1 - 1.5*iqr)) & (sorted_dispersion < (q3 + 1.5*iqr))
          sorted_dispersion = sorted_dispersion.select(inlier_sel)
          self.gain = sorted_dispersion[nint(len(sorted_dispersion)/2)]
        except IndexError:
          self.gain = 1.0
      else:
        self.gain = 1.0

    else:
      data = None

    return data, img_type
Ejemplo n.º 42
0
    f = open(ifname, "r")
    lines = []
    linenum = 1
    framenum = -1
    for line in f:
        if ((line.strip() != "") and (line[0] != '.')):
            if ((framenum == -1) or (framenum == linenum)):
                lines.append(line)
            linenum = linenum + 1
    f.close()

    imgname = filenames[0]
    #  import dxtbx
    t0 = time()
    #  img = FormatSMVADSCNoDateStamp(imgname)
    img = dxtbx.load(imgname)
    detector = img.get_detector()
    beam = img.get_beam()
    scan = img.get_scan()
    gonio = img.get_goniometer()

    print "transform pixel numbers to mm positions and rotational degrees"

    print "Creating pixel map..."
    t0 = time()
    lab_coordinates = flex.vec3_double()
    for panel in detector:
        pixels = flex.vec2_double(panel.get_image_size())
        mms = panel.pixel_to_millimeter(pixels)
        lab_coordinates.extend(panel.get_lab_coord(mms))
Ejemplo n.º 43
0
def run():
  if not have_dials_regression:
    print "Skipping test: dials_regression not available."
    return

  from scitbx import matrix
  from iotbx.xds import xparm, integrate_hkl
  from dials.util import ioutil
  from math import ceil
  from dials.algorithms.spot_prediction import RotationAngles
  import dxtbx
  from rstbx.cftbx.coordinate_frame_converter import \
      coordinate_frame_converter

  # The XDS files to read from
  integrate_filename = join(dials_regression, 'data/sim_mx/INTEGRATE.HKL')
  gxparm_filename = join(dials_regression, 'data/sim_mx/GXPARM.XDS')

  # Read the XDS files
  integrate_handle = integrate_hkl.reader()
  integrate_handle.read_file(integrate_filename)
  gxparm_handle = xparm.reader()
  gxparm_handle.read_file(gxparm_filename)

  # Get the parameters we need from the GXPARM file
  models = dxtbx.load(gxparm_filename)
  beam = models.get_beam()
  gonio = models.get_goniometer()
  detector = models.get_detector()
  scan = models.get_scan()

  # Get the crystal parameters
  space_group_type = ioutil.get_space_group_type_from_xparm(gxparm_handle)
  cfc = coordinate_frame_converter(gxparm_filename)
  a_vec = cfc.get('real_space_a')
  b_vec = cfc.get('real_space_b')
  c_vec = cfc.get('real_space_c')
  unit_cell = cfc.get_unit_cell()
  UB = matrix.sqr(a_vec + b_vec + c_vec).inverse()
  ub_matrix = UB

  # Get the minimum resolution in the integrate file
  d = [unit_cell.d(h) for h in integrate_handle.hkl]
  d_min = min(d)

  # Get the number of frames from the max z value
  xcal, ycal, zcal = zip(*integrate_handle.xyzcal)
  num_frames = int(ceil(max(zcal)))
  scan.set_image_range((scan.get_image_range()[0],
                      scan.get_image_range()[0] + num_frames - 1))

  # Create the rotation angle object
  ra = RotationAngles(beam.get_s0(), gonio.get_rotation_axis())

  # Setup the matrices
  ub = matrix.sqr(ub_matrix)
  s0 = matrix.col(beam.get_s0())
  m2 = matrix.col(gonio.get_rotation_axis())

  # For all the miller indices
  for h in integrate_handle.hkl:
    h = matrix.col(h)

    # Calculate the angles
    angles = ra(h, ub)

    # For all the angles
    for phi in angles:
      r = m2.axis_and_angle_as_r3_rotation_matrix(angle=phi)
      pstar = r * ub * h
      s1 = s0 + pstar
      assert(abs(s1.length() - s0.length()) < 1e-7)

  print "OK"

  # Create a dict of lists of xy for each hkl
  gen_phi = {}
  for h in integrate_handle.hkl:

    # Calculate the angles
    angles = ra(h, ub)
    gen_phi[h] = angles
#        for phi in angles:
#            try:
#                a = gen_phi[h]
#                a.append(phi)
#                gen_phi[h] = a
#            except KeyError:
#                gen_phi[h] = [phi]

  # For each hkl in the xds file
  for hkl, xyz in zip(integrate_handle.hkl,
                      integrate_handle.xyzcal):

    # Calculate the XDS phi value
    xds_phi = scan.get_oscillation(deg=False)[0] + \
              xyz[2]*scan.get_oscillation(deg=False)[1]

    # Select the nearest xy to use if there are 2
    my_phi = gen_phi[hkl]
    if len(my_phi) == 2:
      my_phi0 = my_phi[0]
      my_phi1 = my_phi[1]
      diff0 = abs(xds_phi - my_phi0)
      diff1 = abs(xds_phi - my_phi1)
      if diff0 < diff1:
        my_phi = my_phi0
      else:
        my_phi = my_phi1
    else:
      my_phi = my_phi[0]

    # Check the Phi values are the same
    assert(abs(xds_phi - my_phi) < 0.1)

  # Test Passed
  print "OK"
Ejemplo n.º 44
0
dq_min = 0.005
nom_gain = 28  # nominal photon gain for corrected CSPAD
# ------------------

fnames = glob.glob("results/run%d/*resid.pkl" % run)
mask = [
    m.as_numpy_array() for m in utils.open_flex("dials_mask_64panels_2.pkl")
]

spec_df = pandas.read_pickle('ana_result/run%d/run%d_overview_wspec.pdpkl' %
                             (run, run))

all_spec_hist = []
all_raw_spec = []

loader = dxtbx.load("image_files/_autogen_run%d.loc" % run)
PSANA_ENV = loader.run_mapping[run][2].env()  # important to use this env!

# these values in the dataframe  represent nominal values, but sometimes
# attenuators were pulled or inserted mid-run, hence
# these are not trustworthy, and we resort to per-event attenuation checks
atten = pandas.read_pickle("atten_cxid9114.pdpkl")
thick, trans = atten.loc[atten.run == run,
                         ['thickness', 'transmission']].iloc[0].values
det_ids = range(2, 12)  # need reference to the motors themselves
atten_dets = {
    det_id: psana.Detector("XRT:DIA:MMS:%02d.RBV" % det_id, PSANA_ENV)
    for det_id in det_ids
}
# each motor represents a piece of Silicon foil, of varying thickness
atten_vals = {det_id: 20 * 2**i
Ejemplo n.º 45
0
def test(dials_regression, run_in_tmpdir):
    import dxtbx
    from iotbx.xds import integrate_hkl, xparm
    from rstbx.cftbx.coordinate_frame_converter import coordinate_frame_converter

    from dials.algorithms.spot_prediction import RotationAngles

    # The XDS files to read from
    integrate_filename = os.path.join(dials_regression,
                                      "data/sim_mx/INTEGRATE.HKL")
    gxparm_filename = os.path.join(dials_regression, "data/sim_mx/GXPARM.XDS")

    # Read the XDS files
    integrate_handle = integrate_hkl.reader()
    integrate_handle.read_file(integrate_filename)
    gxparm_handle = xparm.reader()
    gxparm_handle.read_file(gxparm_filename)

    # Get the parameters we need from the GXPARM file
    models = dxtbx.load(gxparm_filename)
    beam = models.get_beam()
    gonio = models.get_goniometer()
    scan = models.get_scan()

    # Get the crystal parameters
    cfc = coordinate_frame_converter(gxparm_filename)
    a_vec = cfc.get("real_space_a")
    b_vec = cfc.get("real_space_b")
    c_vec = cfc.get("real_space_c")
    UB = matrix.sqr(a_vec + b_vec + c_vec).inverse()
    ub_matrix = UB

    # Get the number of frames from the max z value
    xcal, ycal, zcal = zip(*integrate_handle.xyzcal)
    num_frames = int(math.ceil(max(zcal)))
    scan.set_image_range((scan.get_image_range()[0],
                          scan.get_image_range()[0] + num_frames - 1))

    # Create the rotation angle object
    ra = RotationAngles(beam.get_s0(), gonio.get_rotation_axis())

    # Setup the matrices
    ub = matrix.sqr(ub_matrix)
    s0 = matrix.col(beam.get_s0())
    m2 = matrix.col(gonio.get_rotation_axis())

    # For all the miller indices
    for h in integrate_handle.hkl:
        h = matrix.col(h)

        # Calculate the angles
        angles = ra(h, ub)

        # For all the angles
        for phi in angles:
            r = m2.axis_and_angle_as_r3_rotation_matrix(angle=phi)
            pstar = r * ub * h
            s1 = s0 + pstar
            assert s1.length() == pytest.approx(s0.length(), abs=1e-7)

    # Create a dict of lists of xy for each hkl
    gen_phi = {}
    for h in integrate_handle.hkl:

        # Calculate the angles
        angles = ra(h, ub)
        gen_phi[h] = angles
    #        for phi in angles:
    #            try:
    #                a = gen_phi[h]
    #                a.append(phi)
    #                gen_phi[h] = a
    #            except KeyError:
    #                gen_phi[h] = [phi]

    # For each hkl in the xds file
    for hkl, xyz in zip(integrate_handle.hkl, integrate_handle.xyzcal):

        # Calculate the XDS phi value
        xds_phi = (scan.get_oscillation(deg=False)[0] +
                   xyz[2] * scan.get_oscillation(deg=False)[1])

        # Select the nearest xy to use if there are 2
        my_phi = gen_phi[hkl]
        if len(my_phi) == 2:
            my_phi0 = my_phi[0]
            my_phi1 = my_phi[1]
            diff0 = abs(xds_phi - my_phi0)
            diff1 = abs(xds_phi - my_phi1)
            if diff0 < diff1:
                my_phi = my_phi0
            else:
                my_phi = my_phi1
        else:
            my_phi = my_phi[0]

        # Check the Phi values are the same
        assert xds_phi == pytest.approx(my_phi, abs=0.1)
Ejemplo n.º 46
0
        logger = open(params.output_file, 'w')
        logger.write("%s " % params.output_file)

    if params.show_plots:
        from matplotlib import pyplot as plt
        import numpy as np
        colormap = plt.cm.gist_ncar
        plt.gca().set_color_cycle(
            [colormap(i) for i in np.linspace(0, 0.9, len(params.file_path))])

    if params.mask is not None:
        params.mask = easy_pickle.load(params.mask)

    if image is None:
        iterable = params.file_path
        load_func = lambda x: dxtbx.load(x)
    else:
        iterable = [image]
        load_func = lambda x: x

    # Iterate over each file provided
    for item in iterable:
        img = load_func(item)
        beam = img.get_beam()
        detector = img.get_detector()

        # Search the detector for the panel farthest from the beam. The number of bins in the radial average will be
        # equal to the farthest point from the beam on the detector, in pixels, unless overridden at the command line
        extent = 0
        extent_two_theta = 0
        for panel in detector:
Ejemplo n.º 47
0
    def __init__(self, dials_regression):
        import dxtbx
        from iotbx.xds import integrate_hkl, xparm
        from rstbx.cftbx.coordinate_frame_converter import coordinate_frame_converter

        from dials.algorithms.spot_prediction import (
            IndexGenerator,
            ScanStaticRayPredictor,
        )
        from dials.util import ioutil

        # The XDS files to read from
        integrate_filename = os.path.join(dials_regression, "data", "sim_mx",
                                          "INTEGRATE.HKL")
        gxparm_filename = os.path.join(dials_regression, "data", "sim_mx",
                                       "GXPARM.XDS")

        # Read the XDS files
        self.integrate_handle = integrate_hkl.reader()
        self.integrate_handle.read_file(integrate_filename)
        self.gxparm_handle = xparm.reader()
        self.gxparm_handle.read_file(gxparm_filename)

        # Get the parameters we need from the GXPARM file
        models = dxtbx.load(gxparm_filename)
        self.beam = models.get_beam()
        self.gonio = models.get_goniometer()
        self.detector = models.get_detector()
        self.scan = models.get_scan()

        # Get crystal parameters
        self.space_group_type = ioutil.get_space_group_type_from_xparm(
            self.gxparm_handle)
        cfc = coordinate_frame_converter(gxparm_filename)
        a_vec = cfc.get("real_space_a")
        b_vec = cfc.get("real_space_b")
        c_vec = cfc.get("real_space_c")
        self.unit_cell = cfc.get_unit_cell()
        self.ub_matrix = matrix.sqr(a_vec + b_vec + c_vec).inverse()

        # Get the minimum resolution in the integrate file
        d = [self.unit_cell.d(h) for h in self.integrate_handle.hkl]
        self.d_min = min(d)
        # extend the resolution shell by epsilon>0
        # to account for rounding artifacts on 32-bit platforms
        self.d_min = self.d_min - 1e-15

        # Get the number of frames from the max z value
        xcal, ycal, zcal = zip(*self.integrate_handle.xyzcal)
        self.scan.set_image_range((
            self.scan.get_image_range()[0],
            self.scan.get_image_range()[0] + int(math.ceil(max(zcal))),
        ))

        # Print stuff
        #        print self.beam
        #        print self.gonio
        #        print self.detector
        #        print self.scan

        # Create the index generator
        self.generate_indices = IndexGenerator(self.unit_cell,
                                               self.space_group_type,
                                               self.d_min)

        s0 = self.beam.get_s0()
        m2 = self.gonio.get_rotation_axis()
        fixed_rotation = self.gonio.get_fixed_rotation()
        setting_rotation = self.gonio.get_setting_rotation()
        UB = self.ub_matrix
        dphi = self.scan.get_oscillation_range(deg=False)

        # Create the ray predictor
        self.predict_rays = ScanStaticRayPredictor(s0, m2, fixed_rotation,
                                                   setting_rotation, dphi)

        # Predict the spot locations
        self.reflections = self.predict_rays(self.generate_indices.to_array(),
                                             UB)
Ejemplo n.º 48
0
from scitbx import matrix

import dxtbx


def resolution_corners(frame):
    """Compute the resolution limit corresponding to the corners of the detector
    surface."""

    detector = frame.get_detector()
    beam = frame.get_beam()

    nfast, nslow = map(int, detector.get_image_size())
    dfast, dslow = detector.get_pixel_size()
    F = matrix.col(detector.get_fast_axis())
    S = matrix.col(detector.get_slow_axis())
    origin = matrix.col(detector.get_origin())

    s0 = -1 * matrix.col(beam.get_sample_to_source_direction())

    for ds in 0, 1:
        for df in 0, 1:
            corner = origin + nfast * dfast * F * df + nslow * dslow * S * ds
            theta = 0.5 * corner.angle(s0)
            print("%.3f" % (beam.get_wavelength() / (2 * math.sin(theta))))


if __name__ == "__main__":
    resolution_corners(dxtbx.load(sys.argv[1]))
Ejemplo n.º 49
0
    def __init__(self):
        from dials.algorithms.spot_prediction import IndexGenerator
        from dials.algorithms.spot_prediction import ScanStaticRayPredictor
        from dials.algorithms.spot_prediction import ray_intersection
        from iotbx.xds import xparm, integrate_hkl
        from dials.util import ioutil
        from math import ceil
        import dxtbx
        from rstbx.cftbx.coordinate_frame_converter import \
            coordinate_frame_converter
        from scitbx import matrix

        # The XDS files to read from
        integrate_filename = join(dials_regression,
                                  'data/sim_mx/INTEGRATE.HKL')
        gxparm_filename = join(dials_regression, 'data/sim_mx/GXPARM.XDS')

        # Read the XDS files
        self.integrate_handle = integrate_hkl.reader()
        self.integrate_handle.read_file(integrate_filename)
        self.gxparm_handle = xparm.reader()
        self.gxparm_handle.read_file(gxparm_filename)

        # Get the parameters we need from the GXPARM file
        models = dxtbx.load(gxparm_filename)
        self.beam = models.get_beam()
        self.gonio = models.get_goniometer()
        self.detector = models.get_detector()
        self.scan = models.get_scan()

        assert (len(self.detector) == 1)

        #print self.detector

        # Get crystal parameters
        self.space_group_type = ioutil.get_space_group_type_from_xparm(
            self.gxparm_handle)
        cfc = coordinate_frame_converter(gxparm_filename)
        a_vec = cfc.get('real_space_a')
        b_vec = cfc.get('real_space_b')
        c_vec = cfc.get('real_space_c')
        self.unit_cell = cfc.get_unit_cell()
        self.ub_matrix = matrix.sqr(a_vec + b_vec + c_vec).inverse()

        # Get the minimum resolution in the integrate file
        self.d_min = self.detector[0].get_max_resolution_at_corners(
            self.beam.get_s0())

        # Get the number of frames from the max z value
        xcal, ycal, zcal = zip(*self.integrate_handle.xyzcal)
        self.scan.set_image_range(
            (self.scan.get_image_range()[0],
             self.scan.get_image_range()[0] + int(ceil(max(zcal)))))

        # Create the index generator
        generate_indices = IndexGenerator(self.unit_cell,
                                          self.space_group_type, self.d_min)

        s0 = self.beam.get_s0()
        m2 = self.gonio.get_rotation_axis()
        fixed_rotation = self.gonio.get_fixed_rotation()
        setting_rotation = self.gonio.get_setting_rotation()
        UB = self.ub_matrix
        dphi = self.scan.get_oscillation_range(deg=False)

        # Create the ray predictor
        self.predict_rays = ScanStaticRayPredictor(s0, m2, fixed_rotation,
                                                   setting_rotation, dphi)

        # Predict the spot locations
        self.reflections = self.predict_rays(generate_indices.to_array(), UB)

        # Calculate the intersection of the detector and reflection frames
        success = ray_intersection(self.detector, self.reflections)
        self.reflections.select(success)
Ejemplo n.º 50
0
def xds_check_indexer_solution(xparm_file, spot_file):
    '''Read XPARM file from XDS IDXREF (assumes that this is in the putative
  correct symmetry, not P1! and test centring operations if present. Note
  that a future version will boost to the putative correct symmetry (or
  an estimate of it) and try this if it is centred. Returns tuple
  (space_group_number, cell).'''

    from scitbx import matrix
    from dxtbx.serialize.xds import to_crystal as xparm_to_crystal
    cm = xparm_to_crystal(xparm_file)
    sg = cm.get_space_group()
    spacegroup = sg.type().hall_symbol()
    space_group_number = sg.type().number()
    A_inv = matrix.sqr(cm.get_A()).inverse()
    cell = cm.get_unit_cell().parameters()

    import dxtbx
    models = dxtbx.load(xparm_file)
    detector = models.get_detector()
    beam = models.get_beam()
    goniometer = models.get_goniometer()
    scan = models.get_scan()

    from iotbx.xds import spot_xds
    spot_xds_handle = spot_xds.reader()
    spot_xds_handle.read_file(spot_file)

    from cctbx.array_family import flex
    centroids_px = flex.vec3_double(spot_xds_handle.centroid)
    miller_indices = flex.miller_index(spot_xds_handle.miller_index)

    # Convert Pixel coordinate into mm/rad
    x, y, z = centroids_px.parts()
    x_mm, y_mm = detector[0].pixel_to_millimeter(flex.vec2_double(x,
                                                                  y)).parts()
    z_rad = scan.get_angle_from_array_index(z, deg=False)
    centroids_mm = flex.vec3_double(x_mm, y_mm, z_rad)

    # then convert detector position to reciprocal space position

    # based on code in dials/algorithms/indexing/indexer2.py
    s1 = detector[0].get_lab_coord(flex.vec2_double(x_mm, y_mm))
    s1 = s1 / s1.norms() * (1 / beam.get_wavelength())
    S = s1 - beam.get_s0()
    # XXX what about if goniometer fixed rotation is not identity?
    reciprocal_space_points = S.rotate_around_origin(
        goniometer.get_rotation_axis(), -z_rad)

    # now index the reflections
    hkl_float = tuple(A_inv) * reciprocal_space_points
    hkl_int = hkl_float.iround()

    # check if we are within 0.1 lattice spacings of the closest
    # lattice point - a for a random point this will be about 0.8% of
    # the time...
    differences = hkl_float - hkl_int.as_vec3_double()
    dh, dk, dl = [flex.abs(d) for d in differences.parts()]
    tolerance = 0.1
    sel = (dh < tolerance) and (dk < tolerance) and (dl < tolerance)

    is_sys_absent = sg.is_sys_absent(
        flex.miller_index(list(hkl_int.select(sel))))

    total = is_sys_absent.size()
    absent = is_sys_absent.count(True)
    present = total - absent

    # now, if the number of absences is substantial, need to consider
    # transforming this to a primitive basis

    Debug.write('Absent: %d  vs.  Present: %d Total: %d' % \
                (absent, present, total))

    # now see if this is compatible with a centred lattice or suggests
    # a primitive basis is correct

    sd = math.sqrt(absent)

    if (absent - 3 * sd) / total < 0.008:
        # everything is peachy

        return s2l(space_group_number), tuple(cell)

    # ok if we are here things are not peachy, so need to calculate the
    # spacegroup number without the translation operators

    sg_new = sg.build_derived_group(True, False)
    space_group_number_primitive = sg_new.type().number()

    # also determine the best setting for the new cell ...

    symm = crystal.symmetry(unit_cell=cell, space_group=sg_new)

    rdx = symm.change_of_basis_op_to_best_cell()
    symm_new = symm.change_basis(rdx)
    cell_new = symm_new.unit_cell().parameters()

    return s2l(space_group_number_primitive), tuple(cell_new)
Ejemplo n.º 51
0
 def __init__(self, filename):
     self.image = dxtbx.load(filename)
Ejemplo n.º 52
0
if (__name__ == "__main__"):
  files = [arg for arg in sys.argv[1:] if os.path.isfile(arg)]
  arguments = [libtbx.phil.parse(arg) for arg in sys.argv[1:] if not os.path.isfile(arg)]
  params = master_phil.fetch(sources=arguments).extract()

  rot45 = sqr((sin(pi/4.),-cos(pi/4.),cos(pi/4.),sin(pi/4.)))

  for file in files:
    message="""Based on the file %s, this program will compute incremental translations to circularize
    powder rings.  The algorithm  scores based on self-correlation upon 45-degree rotation.
    Increments are determined ON TOP OF beam center value in image header.  Output is given in the form of
    delta to that value."""%file
    print message

    img = dxtbx.load(file)
    beam = img.get_beam()
    s0 = beam.get_s0()

    raw_data = img.get_raw_data()
    if not isinstance(raw_data, tuple):
      raw_data = (raw_data,)

    for panel_id, panel in enumerate(img.get_detector()):
      beam_center = col(panel.get_beam_centre_px(s0))
      data = raw_data[panel_id]

      print "Assembling mask...",; sys.stdout.flush()
      mask = panel.get_trusted_range_mask(data)
      trusted_min = panel.get_trusted_range()[0]
Ejemplo n.º 53
0
import matplotlib.pyplot as plt


def cost(params):
    x0, y0, r = params
    coords = draw.circle(y0, x0, r, shape=image.shape)
    template = np.zeros_like(image)
    template[coords] = mean_i
    print x0, y0, r
    return np.sum((template[coords] - image[coords])**2)


if (__name__ == "__main__"):
    imgpath = sys.argv[1]

    img = dxtbx.load('mccd/E1_0_00006_106.mccd')
    raw_data = img.get_raw_data()
    image = raw_data.as_numpy_array()
    detector = img.get_detector()
    detector = detector[0]
    beam = img.get_beam()
    pixel_size = detector.get_pixel_size()[0]
    x0 = int(round(detector.get_beam_centre(beam.get_s0())[0] / pixel_size))
    y0 = int(round(detector.get_beam_centre(beam.get_s0())[1] / pixel_size))
    r = 1400

    coords = draw.circle(y0, x0, r, shape=image.shape)
    mean_i = np.mean(image[coords])
    print x0, y0, r, mean_i
    """
  img = io.imread(imgpath)
Ejemplo n.º 54
0
  def __init__(self):
    from dials.algorithms.spot_prediction import IndexGenerator
    from dials.algorithms.spot_prediction import ScanStaticRayPredictor
    from iotbx.xds import xparm, integrate_hkl
    from dials.util import ioutil
    from math import ceil
    import dxtbx
    from rstbx.cftbx.coordinate_frame_converter import \
        coordinate_frame_converter
    from scitbx import matrix

    # The XDS files to read from
    integrate_filename = join(dials_regression, 'data/sim_mx/INTEGRATE.HKL')
    gxparm_filename = join(dials_regression, 'data/sim_mx/GXPARM.XDS')

    # Read the XDS files
    self.integrate_handle = integrate_hkl.reader()
    self.integrate_handle.read_file(integrate_filename)
    self.gxparm_handle = xparm.reader()
    self.gxparm_handle.read_file(gxparm_filename)

    # Get the parameters we need from the GXPARM file
    models = dxtbx.load(gxparm_filename)
    self.beam = models.get_beam()
    self.gonio = models.get_goniometer()
    self.detector = models.get_detector()
    self.scan = models.get_scan()

    # Get crystal parameters
    self.space_group_type = ioutil.get_space_group_type_from_xparm(
        self.gxparm_handle)
    cfc = coordinate_frame_converter(gxparm_filename)
    a_vec = cfc.get('real_space_a')
    b_vec = cfc.get('real_space_b')
    c_vec = cfc.get('real_space_c')
    self.unit_cell = cfc.get_unit_cell()
    self.ub_matrix = matrix.sqr(a_vec + b_vec + c_vec).inverse()

    # Get the minimum resolution in the integrate file
    d = [self.unit_cell.d(h) for h in self.integrate_handle.hkl]
    self.d_min = min(d)
    # extend the resolution shell by epsilon>0
    # to account for rounding artifacts on 32-bit platforms
    self.d_min = self.d_min - 1e-15

    # Get the number of frames from the max z value
    xcal, ycal, zcal = zip(*self.integrate_handle.xyzcal)
    self.scan.set_image_range((self.scan.get_image_range()[0],
                             self.scan.get_image_range()[0] +
                                int(ceil(max(zcal)))))

    # Print stuff
#        print self.beam
#        print self.gonio
#        print self.detector
#        print self.scan

    # Create the index generator
    self.generate_indices = IndexGenerator(self.unit_cell,
        self.space_group_type, self.d_min)

    s0 = self.beam.get_s0()
    m2 = self.gonio.get_rotation_axis()
    fixed_rotation = self.gonio.get_fixed_rotation()
    setting_rotation = self.gonio.get_setting_rotation()
    UB = self.ub_matrix
    dphi = self.scan.get_oscillation_range(deg=False)

    # Create the ray predictor
    self.predict_rays = ScanStaticRayPredictor(s0, m2, fixed_rotation,
                                               setting_rotation, dphi)

    # Predict the spot locations
    self.reflections = self.predict_rays(
                            self.generate_indices.to_array(), UB)
Ejemplo n.º 55
0
    def _index(self):
        """Actually do the autoindexing using the data prepared by the
        previous method."""

        self._index_remove_masked_regions()

        if self._i_or_ii is None:
            self._i_or_ii = self.decide_i_or_ii()
            logger.debug("Selecting I or II, chose %s", self._i_or_ii)

        idxref = self.Idxref()

        for file in ["SPOT.XDS"]:
            idxref.set_input_data_file(file, self._indxr_payload[file])

        # set the phi start etc correctly

        idxref.set_data_range(self._indxr_images[0][0],
                              self._indxr_images[0][1])
        idxref.set_background_range(self._indxr_images[0][0],
                                    self._indxr_images[0][1])

        if self._i_or_ii == "i":
            blocks = self._index_select_images_i()
            for block in blocks[:1]:
                starting_frame = block[0]
                starting_angle = self.get_scan().get_angle_from_image_index(
                    starting_frame)

                idxref.set_starting_frame(starting_frame)
                idxref.set_starting_angle(starting_angle)

                idxref.add_spot_range(block[0], block[1])

            for block in blocks[1:]:
                idxref.add_spot_range(block[0], block[1])
        else:
            for block in self._indxr_images[:1]:
                starting_frame = block[0]
                starting_angle = self.get_scan().get_angle_from_image_index(
                    starting_frame)

                idxref.set_starting_frame(starting_frame)
                idxref.set_starting_angle(starting_angle)

                idxref.add_spot_range(block[0], block[1])

            for block in self._indxr_images[1:]:
                idxref.add_spot_range(block[0], block[1])

        # FIXME need to also be able to pass in the known unit
        # cell and lattice if already available e.g. from
        # the helper... indirectly

        if self._indxr_user_input_lattice:
            idxref.set_indexer_user_input_lattice(True)

        if self._indxr_input_lattice and self._indxr_input_cell:
            idxref.set_indexer_input_lattice(self._indxr_input_lattice)
            idxref.set_indexer_input_cell(self._indxr_input_cell)

            logger.debug("Set lattice: %s", self._indxr_input_lattice)
            logger.debug("Set cell: %f %f %f %f %f %f" %
                         self._indxr_input_cell)

            original_cell = self._indxr_input_cell
        elif self._indxr_input_lattice:
            idxref.set_indexer_input_lattice(self._indxr_input_lattice)
            original_cell = None
        else:
            original_cell = None

        # FIXED need to set the beam centre here - this needs to come
        # from the input .xinfo object or header, and be converted
        # to the XDS frame... done.

        from dxtbx.serialize.xds import to_xds

        converter = to_xds(self.get_imageset())
        xds_beam_centre = converter.detector_origin

        idxref.set_beam_centre(xds_beam_centre[0], xds_beam_centre[1])

        # fixme need to check if the lattice, cell have been set already,
        # and if they have, pass these in as input to the indexing job.

        done = False

        while not done:
            try:
                done = idxref.run()

                # N.B. in here if the IDXREF step was being run in the first
                # pass done is FALSE however there should be a refined
                # P1 orientation matrix etc. available - so keep it!

            except XDSException as e:
                # inspect this - if we have complaints about not
                # enough reflections indexed, and we have a target
                # unit cell, and they are the same, well ignore it

                if "solution is inaccurate" in str(e):
                    logger.debug(
                        "XDS complains solution inaccurate - ignoring")
                    done = idxref.continue_from_error()
                elif ("insufficient percentage (< 70%)" in str(e)
                      or "insufficient percentage (< 50%)"
                      in str(e)) and original_cell:
                    done = idxref.continue_from_error()
                    lattice, cell, mosaic = idxref.get_indexing_solution()
                    # compare solutions
                    check = PhilIndex.params.xia2.settings.xds_check_cell_deviation
                    for j in range(3):
                        # allow two percent variation in unit cell length
                        if (math.fabs(
                            (cell[j] - original_cell[j]) / original_cell[j]) >
                                0.02 and check):
                            logger.debug("XDS unhappy and solution wrong")
                            raise e
                        # and two degree difference in angle
                        if (math.fabs(cell[j + 3] - original_cell[j + 3]) > 2.0
                                and check):
                            logger.debug("XDS unhappy and solution wrong")
                            raise e
                    logger.debug("XDS unhappy but solution ok")
                elif "insufficient percentage (< 70%)" in str(
                        e) or "insufficient percentage (< 50%)" in str(e):
                    logger.debug("XDS unhappy but solution probably ok")
                    done = idxref.continue_from_error()
                else:
                    raise e

        FileHandler.record_log_file(
            "%s INDEX" % self.get_indexer_full_name(),
            os.path.join(self.get_working_directory(), "IDXREF.LP"),
        )

        for file in ["SPOT.XDS", "XPARM.XDS"]:
            self._indxr_payload[file] = idxref.get_output_data_file(file)

        # need to get the indexing solutions out somehow...

        self._indxr_other_lattice_cell = idxref.get_indexing_solutions()

        (
            self._indxr_lattice,
            self._indxr_cell,
            self._indxr_mosaic,
        ) = idxref.get_indexing_solution()

        xparm_file = os.path.join(self.get_working_directory(), "XPARM.XDS")
        models = dxtbx.load(xparm_file)
        crystal_model = to_crystal(xparm_file)

        # this information gets lost when re-creating the models from the
        # XDS results - however is not refined so can simply copy from the
        # input - https://github.com/xia2/xia2/issues/372
        models.get_detector()[0].set_thickness(
            converter.get_detector()[0].get_thickness())

        experiment = Experiment(
            beam=models.get_beam(),
            detector=models.get_detector(),
            goniometer=models.get_goniometer(),
            scan=models.get_scan(),
            crystal=crystal_model,
            # imageset=self.get_imageset(),
        )

        experiment_list = ExperimentList([experiment])
        self.set_indexer_experiment_list(experiment_list)

        # I will want this later on to check that the lattice was ok
        self._idxref_subtree_problem = idxref.get_index_tree_problem()
Ejemplo n.º 56
0
def run_sim2smv(fileout):
    SIM = nanoBragg(detpixels_slowfast=(1000, 1000),
                    pixel_size_mm=0.1,
                    Ncells_abc=(5, 5, 5),
                    verbose=9)
    import sys
    if len(sys.argv) > 2:
        SIM.seed = -int(sys.argv[2])
        print "GOTHERE seed=", SIM.seed
    if len(sys.argv) > 1:
        if sys.argv[1] == "random": SIM.randomize_orientation()
    SIM.distance_mm = 100
    #SIM = nanoBragg(detpixels_slowfast=(2527,2463),pixel_size_mm=0.172,Ncells_abc=(5,5,5),verbose=9)
    #SIM.distance_mm=200
    # get same noise each time this test is run
    SIM.seed = 1
    SIM.oversample = 1
    SIM.wavelength_A = 1
    SIM.polarization = 1
    #SIM.unit_cell_tuple=(50,50,50,90,90,90)
    print "unit_cell_Adeg=", SIM.unit_cell_Adeg
    print "unit_cell_tuple=", SIM.unit_cell_tuple
    # this will become F000, marking the beam center
    SIM.default_F = 100
    #SIM.missets_deg= (10,20,30)
    print "mosaic_seed=", SIM.mosaic_seed
    print "seed=", SIM.seed
    print "calib_seed=", SIM.calib_seed
    print "missets_deg =", SIM.missets_deg
    sfall = fcalc_from_pdb(resolution=1.6,
                           algorithm="direct",
                           wavelength=SIM.wavelength_A)
    # use crystal structure to initialize Fhkl array
    SIM.Fhkl = sfall
    # fastest option, least realistic
    SIM.xtal_shape = shapetype.Tophat
    # only really useful for long runs
    SIM.progress_meter = False
    # prints out value of one pixel only.  will not render full image!
    #SIM.printout_pixel_fastslow=(500,500)
    #SIM.printout=True
    SIM.show_params()
    # flux is always in photons/s
    SIM.flux = 1e12
    # assumes round beam
    SIM.beamsize_mm = 0.1
    SIM.exposure_s = 0.1
    temp = SIM.Ncells_abc
    print "Ncells_abc=", SIM.Ncells_abc
    SIM.Ncells_abc = temp
    print "Ncells_abc=", SIM.Ncells_abc
    print "xtal_size_mm=", SIM.xtal_size_mm
    print "unit_cell_Adeg=", SIM.unit_cell_Adeg
    print "unit_cell_tuple=", SIM.unit_cell_tuple
    print "missets_deg=", SIM.missets_deg
    print "Amatrix=", SIM.Amatrix
    print "beam_center_mm=", SIM.beam_center_mm
    print "XDS_ORGXY=", SIM.XDS_ORGXY
    print "detector_pivot=", SIM.detector_pivot
    print "xtal_shape=", SIM.xtal_shape
    print "beamcenter_convention=", SIM.beamcenter_convention
    print "fdet_vector=", SIM.fdet_vector
    print "sdet_vector=", SIM.sdet_vector
    print "odet_vector=", SIM.odet_vector
    print "beam_vector=", SIM.beam_vector
    print "polar_vector=", SIM.polar_vector
    print "spindle_axis=", SIM.spindle_axis
    print "twotheta_axis=", SIM.twotheta_axis
    print "distance_meters=", SIM.distance_meters
    print "distance_mm=", SIM.distance_mm
    print "close_distance_mm=", SIM.close_distance_mm
    print "detector_twotheta_deg=", SIM.detector_twotheta_deg
    print "detsize_fastslow_mm=", SIM.detsize_fastslow_mm
    print "detpixels_fastslow=", SIM.detpixels_fastslow
    print "detector_rot_deg=", SIM.detector_rot_deg
    print "curved_detector=", SIM.curved_detector
    print "pixel_size_mm=", SIM.pixel_size_mm
    print "point_pixel=", SIM.point_pixel
    print "polarization=", SIM.polarization
    print "nopolar=", SIM.nopolar
    print "oversample=", SIM.oversample
    print "region_of_interest=", SIM.region_of_interest
    print "wavelength_A=", SIM.wavelength_A
    print "energy_eV=", SIM.energy_eV
    print "fluence=", SIM.fluence
    print "flux=", SIM.flux
    print "exposure_s=", SIM.exposure_s
    print "beamsize_mm=", SIM.beamsize_mm
    print "dispersion_pct=", SIM.dispersion_pct
    print "dispsteps=", SIM.dispsteps
    print "divergence_hv_mrad=", SIM.divergence_hv_mrad
    print "divsteps_hv=", SIM.divsteps_hv
    print "divstep_hv_mrad=", SIM.divstep_hv_mrad
    print "round_div=", SIM.round_div
    print "phi_deg=", SIM.phi_deg
    print "osc_deg=", SIM.osc_deg
    print "phisteps=", SIM.phisteps
    print "phistep_deg=", SIM.phistep_deg
    print "detector_thick_mm=", SIM.detector_thick_mm
    print "detector_thicksteps=", SIM.detector_thicksteps
    print "detector_thickstep_mm=", SIM.detector_thickstep_mm
    print "mosaic_spread_deg=", SIM.mosaic_spread_deg
    print "mosaic_domains=", SIM.mosaic_domains
    print "indices=", SIM.indices
    print "amplitudes=", SIM.amplitudes
    print "Fhkl_tuple=", SIM.Fhkl_tuple
    print "default_F=", SIM.default_F
    print "interpolate=", SIM.interpolate
    print "integral_form=", SIM.integral_form
    # now actually burn up some CPU
    SIM.add_nanoBragg_spots()
    # simulated crystal is only 125 unit cells (25 nm wide)
    # amplify spot signal to simulate physical crystal of 4000x larger: 100 um (64e9 x the volume)
    SIM.raw_pixels *= 64e9
    SIM.to_smv_format(fileout="intimage_001.img")
    # rough approximation to water: interpolation points for sin(theta/lambda) vs structure factor
    bg = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8), (0.12, 5),
                           (0.162, 8), (0.2, 6.75),
                           (0.18, 7.32), (0.216, 6.75), (0.236, 6.5),
                           (0.28, 4.5), (0.3, 4.3), (0.345, 4.36),
                           (0.436, 3.77), (0.5, 3.17)])
    SIM.Fbg_vs_stol = bg
    SIM.amorphous_sample_thick_mm = 0.1
    SIM.amorphous_density_gcm3 = 1
    SIM.amorphous_molecular_weight_Da = 18
    SIM.flux = 1e12
    SIM.beamsize_mm = 0.1
    SIM.exposure_s = 0.1
    SIM.add_background()
    SIM.to_smv_format(fileout="intimage_002.img")
    # rough approximation to air
    bg = flex.vec2_double([(0, 14.1), (0.045, 13.5), (0.174, 8.35),
                           (0.35, 4.78), (0.5, 4.22)])
    SIM.Fbg_vs_stol = bg
    SIM.amorphous_sample_thick_mm = 35  # between beamstop and collimator
    SIM.amorphous_density_gcm3 = 1.2e-3
    SIM.amorphous_sample_molecular_weight_Da = 28  # nitrogen = N2
    print "amorphous_sample_size_mm=", SIM.amorphous_sample_size_mm
    print "amorphous_sample_thick_mm=", SIM.amorphous_sample_thick_mm
    print "amorphous_density_gcm3=", SIM.amorphous_density_gcm3
    print "amorphous_molecular_weight_Da=", SIM.amorphous_molecular_weight_Da
    SIM.add_background()
    # set this to 0 or -1 to trigger automatic radius.  could be very slow with bright images
    SIM.detector_psf_kernel_radius_pixels = 5
    SIM.detector_psf_fwhm_mm = 0.08
    SIM.detector_psf_type = shapetype.Fiber
    #SIM.apply_psf()
    print SIM.raw_pixels[500000]
    SIM.to_smv_format(fileout="intimage_003.img")
    #SIM.detector_psf_fwhm_mm=0
    print "quantum_gain=", SIM.quantum_gain
    print "adc_offset_adu=", SIM.adc_offset_adu
    print "detector_calibration_noise_pct=", SIM.detector_calibration_noise_pct
    print "flicker_noise_pct=", SIM.flicker_noise_pct
    print "readout_noise_adu=", SIM.readout_noise_adu
    print "detector_psf_type=", SIM.detector_psf_type
    print "detector_psf_fwhm_mm=", SIM.detector_psf_fwhm_mm
    print "detector_psf_kernel_radius_pixels=", SIM.detector_psf_kernel_radius_pixels
    SIM.add_noise()

    #fileout = "intimage_001.img"
    print "raw_pixels=", SIM.raw_pixels
    SIM.to_smv_format(fileout="noiseimage_001.img", intfile_scale=1)

    # try to write as CBF
    import dxtbx
    from dxtbx.format.FormatCBFMini import FormatCBFMini
    img = dxtbx.load("noiseimage_001.img")
    print img
    FormatCBFMini.as_file(detector=img.get_detector(),
                          beam=img.get_beam(),
                          gonio=img.get_goniometer(),
                          scan=img.get_scan(),
                          data=img.get_raw_data(),
                          path=fileout)
    SIM.free_all()
Ejemplo n.º 57
0
to full reflection equivalents.  Therefore it reports an absolute
minimum number of images when the reality is almost certainly higher.
"""

# Set up dxtbx models, starting with a detector. Here we simulate
# the gaps in a CSPAD by using a CSPAD CBF
import dxtbx, libtbx.load_env, os

img_root = libtbx.env.find_in_repositories("dials_regression")
if img_root is not None:
    img_path = os.path.join(img_root, "spotfinding_test_data/idx-s00-20131106040304531.cbf")
else:
    img_path = None

if img_path is not None and os.path.exists(img_path):
    img = dxtbx.load(img_path)
    detector = img.get_detector()
    # Manually push the detector in to 100 mm
    h = detector.hierarchy()
    h.set_local_frame(h.get_fast_axis(), h.get_slow_axis(), (0, 0, -100))
else:
    # If dials_regression not present, use a simple detector with a single panel, similar to a CSPAD
    detector = detector_factory.simple("SENSOR_UNKNOWN", 100, (97.075, 97.075), "+x", "-y", (0.11, 0.11), (1765, 1765))

# Here's the MarCCD at XPP if desired.
# detector=detector_factory.simple('SENSOR_UNKNOWN',150,(162.5,162.5),'+x','-y',(0.079346,0.079346),(4096,4096))


# Beam model
wavelength = 1.32  # from Boutet 2012
beam = beam_factory.simple_directional((0, 0, 1), wavelength)
Ejemplo n.º 58
0
def run_sim2smv(prefix, crystal, spectra, rotation, rank, quick=False):
    local_data = data()
    smv_fileout = prefix + ".img"
    if quick is not True:
        if not write_safe(smv_fileout):
            print("File %s already exists, skipping in rank %d" %
                  (smv_fileout, rank))
            return

    direct_algo_res_limit = 1.7

    wavlen, flux, wavelength_A = next(
        spectra)  # list of lambdas, list of fluxes, average wavelength
    if quick:
        wavlen = flex.double([wavelength_A])
        flux = flex.double([flex.sum(flux)])
        print("Quick sim, lambda=%f, flux=%f" % (wavelength_A, flux[0]))

    GF = gen_fmodel(resolution=direct_algo_res_limit,
                    pdb_text=local_data.get("pdb_lines"),
                    algorithm="fft",
                    wavelength=wavelength_A)
    GF.set_k_sol(0.435)
    GF.make_P1_primitive()
    sfall_main = GF.get_amplitudes()

    # use crystal structure to initialize Fhkl array
    sfall_main.show_summary(prefix="Amplitudes used ")
    N = crystal.number_of_cells(sfall_main.unit_cell())

    #SIM = nanoBragg(detpixels_slowfast=(2000,2000),pixel_size_mm=0.11,Ncells_abc=(5,5,5),verbose=0)
    SIM = nanoBragg(
        detpixels_slowfast=(3000, 3000),
        pixel_size_mm=0.11,
        Ncells_abc=(N, N, N),
        # workaround for problem with wavelength array, specify it separately in constructor.
        wavelength_A=wavelength_A,
        verbose=0)
    SIM.adc_offset_adu = 0  # Do not offset by 40
    SIM.adc_offset_adu = 10  # Do not offset by 40
    import sys
    if len(sys.argv) > 2:
        SIM.seed = -int(sys.argv[2])
        print("GOTHERE seed=", SIM.seed)
    if len(sys.argv) > 1:
        if sys.argv[1] == "random": SIM.randomize_orientation()
    SIM.mosaic_spread_deg = 0.05  # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = n_mosaic_domains  # 77 seconds.  With 100 energy points, 7700 seconds (2 hours) per image
    # 3000000 images would be 100000 hours on a 60-core machine (dials), or 11.4 years
    # using 2 nodes, 5.7 years.  Do this at SLAC? NERSC? combination of all?
    # SLAC downtimes: Tues Dec 5 (24 hrs), Mon Dec 11 (72 hrs), Mon Dec 18 light use, 24 days
    # mosaic_domains setter must come after mosaic_spread_deg setter
    SIM.distance_mm = 141.7

    UMAT_nm = flex.mat3_double()
    mersenne_twister = flex.mersenne_twister(seed=0)
    scitbx.random.set_random_seed(1234)
    rand_norm = scitbx.random.normal_distribution(mean=0,
                                                  sigma=SIM.mosaic_spread_deg *
                                                  math.pi / 180.)
    g = scitbx.random.variate(rand_norm)
    mosaic_rotation = g(SIM.mosaic_domains)
    for m in mosaic_rotation:
        site = col(mersenne_twister.random_double_point_on_sphere())
        UMAT_nm.append(site.axis_and_angle_as_r3_rotation_matrix(m, deg=False))
    SIM.set_mosaic_blocks(UMAT_nm)

    #SIM.detector_thick_mm = 0.5 # = 0 for Rayonix
    #SIM.detector_thicksteps = 1 # should default to 1 for Rayonix, but set to 5 for CSPAD
    #SIM.detector_attenuation_length_mm = default is silicon

    # get same noise each time this test is run
    SIM.seed = 1
    SIM.oversample = 1
    SIM.wavelength_A = wavelength_A
    SIM.polarization = 1
    # this will become F000, marking the beam center
    SIM.default_F = 0
    #SIM.missets_deg= (10,20,30)
    print("mosaic_seed=", SIM.mosaic_seed)
    print("seed=", SIM.seed)
    print("calib_seed=", SIM.calib_seed)
    print("missets_deg =", SIM.missets_deg)
    SIM.Fhkl = sfall_main
    print("Determinant", rotation.determinant())
    Amatrix_rot = (
        rotation *
        sqr(sfall_main.unit_cell().orthogonalization_matrix())).transpose()
    print("RAND_ORI", prefix, end=' ')
    for i in Amatrix_rot:
        print(i, end=' ')
    print()

    SIM.Amatrix_RUB = Amatrix_rot
    #workaround for failing init_cell, use custom written Amatrix setter
    print("unit_cell_Adeg=", SIM.unit_cell_Adeg)
    print("unit_cell_tuple=", SIM.unit_cell_tuple)
    Amat = sqr(SIM.Amatrix).transpose()  # recovered Amatrix from SIM
    from cctbx import crystal_orientation
    Ori = crystal_orientation.crystal_orientation(
        Amat, crystal_orientation.basis_type.reciprocal)
    print("Python unit cell from SIM state", Ori.unit_cell())

    # fastest option, least realistic
    #SIM.xtal_shape=shapetype.Tophat # RLP = hard sphere
    #SIM.xtal_shape=shapetype.Square # gives fringes
    SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
    #SIM.xtal_shape=shapetype.Round # Crystal is a hard sphere
    # only really useful for long runs
    SIM.progress_meter = False
    # prints out value of one pixel only.  will not render full image!
    #SIM.printout_pixel_fastslow=(500,500)
    #SIM.printout=True
    SIM.show_params()
    # flux is always in photons/s
    SIM.flux = 1e12
    SIM.exposure_s = 1.0  # so total fluence is e12
    # assumes round beam
    SIM.beamsize_mm = 0.003  #cannot make this 3 microns; spots are too intense
    temp = SIM.Ncells_abc
    print("Ncells_abc=", SIM.Ncells_abc)
    SIM.Ncells_abc = temp
    print("Ncells_abc=", SIM.Ncells_abc)
    print("xtal_size_mm=", SIM.xtal_size_mm)
    print("unit_cell_Adeg=", SIM.unit_cell_Adeg)
    print("unit_cell_tuple=", SIM.unit_cell_tuple)
    print("missets_deg=", SIM.missets_deg)
    print("Amatrix=", SIM.Amatrix)
    print("beam_center_mm=", SIM.beam_center_mm)
    print("XDS_ORGXY=", SIM.XDS_ORGXY)
    print("detector_pivot=", SIM.detector_pivot)
    print("xtal_shape=", SIM.xtal_shape)
    print("beamcenter_convention=", SIM.beamcenter_convention)
    print("fdet_vector=", SIM.fdet_vector)
    print("sdet_vector=", SIM.sdet_vector)
    print("odet_vector=", SIM.odet_vector)
    print("beam_vector=", SIM.beam_vector)
    print("polar_vector=", SIM.polar_vector)
    print("spindle_axis=", SIM.spindle_axis)
    print("twotheta_axis=", SIM.twotheta_axis)
    print("distance_meters=", SIM.distance_meters)
    print("distance_mm=", SIM.distance_mm)
    print("close_distance_mm=", SIM.close_distance_mm)
    print("detector_twotheta_deg=", SIM.detector_twotheta_deg)
    print("detsize_fastslow_mm=", SIM.detsize_fastslow_mm)
    print("detpixels_fastslow=", SIM.detpixels_fastslow)
    print("detector_rot_deg=", SIM.detector_rot_deg)
    print("curved_detector=", SIM.curved_detector)
    print("pixel_size_mm=", SIM.pixel_size_mm)
    print("point_pixel=", SIM.point_pixel)
    print("polarization=", SIM.polarization)
    print("nopolar=", SIM.nopolar)
    print("oversample=", SIM.oversample)
    print("region_of_interest=", SIM.region_of_interest)
    print("wavelength_A=", SIM.wavelength_A)
    print("energy_eV=", SIM.energy_eV)
    print("fluence=", SIM.fluence)
    print("flux=", SIM.flux)
    print("exposure_s=", SIM.exposure_s)
    print("beamsize_mm=", SIM.beamsize_mm)
    print("dispersion_pct=", SIM.dispersion_pct)
    print("dispsteps=", SIM.dispsteps)
    print("divergence_hv_mrad=", SIM.divergence_hv_mrad)
    print("divsteps_hv=", SIM.divsteps_hv)
    print("divstep_hv_mrad=", SIM.divstep_hv_mrad)
    print("round_div=", SIM.round_div)
    print("phi_deg=", SIM.phi_deg)
    print("osc_deg=", SIM.osc_deg)
    print("phisteps=", SIM.phisteps)
    print("phistep_deg=", SIM.phistep_deg)
    print("detector_thick_mm=", SIM.detector_thick_mm)
    print("detector_thicksteps=", SIM.detector_thicksteps)
    print("detector_thickstep_mm=", SIM.detector_thickstep_mm)
    print("***mosaic_spread_deg=", SIM.mosaic_spread_deg)
    print("***mosaic_domains=", SIM.mosaic_domains)
    print("indices=", SIM.indices)
    print("amplitudes=", SIM.amplitudes)
    print("Fhkl_tuple=", SIM.Fhkl_tuple)
    print("default_F=", SIM.default_F)
    print("interpolate=", SIM.interpolate)
    print("integral_form=", SIM.integral_form)

    from libtbx.development.timers import Profiler
    P = Profiler("nanoBragg")
    # now actually burn up some CPU
    #SIM.add_nanoBragg_spots()
    del P

    # simulated crystal is only 125 unit cells (25 nm wide)
    # amplify spot signal to simulate physical crystal of 4000x larger: 100 um (64e9 x the volume)
    print(crystal.domains_per_crystal)
    SIM.raw_pixels *= crystal.domains_per_crystal
    # must calculate the correct scale!

    # Use single wavelength for all energy channels for the purpose of Fcalc
    wavelength_hi_remote = wavlen[-1]
    GF.reset_wavelength(wavelength_hi_remote)
    GF.reset_specific_at_wavelength(label_has="FE1",
                                    tables=local_data.get("Fe_oxidized_model"),
                                    newvalue=wavelength_hi_remote)
    GF.reset_specific_at_wavelength(label_has="FE2",
                                    tables=local_data.get("Fe_reduced_model"),
                                    newvalue=wavelength_hi_remote)
    sfall_channel = GF.get_amplitudes()

    # sources
    channel_source_XYZ = flex.vec3_double(len(flux), SIM.xray_source_XYZ[0])

    CP = channel_pixels(
        wavlen, flux,
        channel_source_XYZ)  # class interface for multi-wavelength
    print("+++++++++++++++++++++++++++++++++++++++ Multiwavelength call")
    CH = CP(N=N,
            UMAT_nm=UMAT_nm,
            Amatrix_rot=Amatrix_rot,
            sfall_channel=sfall_channel)
    SIM.raw_pixels += CH.raw_pixels * crystal.domains_per_crystal
    CH.free_all()
    if quick: SIM.to_smv_format(fileout=prefix + "_intimage_001.img")

    # rough approximation to water: interpolation points for sin(theta/lambda) vs structure factor
    bg = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8), (0.12, 5),
                           (0.162, 8), (0.2, 6.75),
                           (0.18, 7.32), (0.216, 6.75), (0.236, 6.5),
                           (0.28, 4.5), (0.3, 4.3), (0.345, 4.36),
                           (0.436, 3.77), (0.5, 3.17)])
    SIM.Fbg_vs_stol = bg
    SIM.amorphous_sample_thick_mm = 0.1
    SIM.amorphous_density_gcm3 = 1
    SIM.amorphous_molecular_weight_Da = 18
    SIM.flux = 1e12
    SIM.beamsize_mm = 0.003  # square (not user specified)
    SIM.exposure_s = 1.0  # multiplies flux x exposure
    SIM.add_background()
    if quick: SIM.to_smv_format(fileout=prefix + "_intimage_002.img")

    # rough approximation to air
    bg = flex.vec2_double([(0, 14.1), (0.045, 13.5), (0.174, 8.35),
                           (0.35, 4.78), (0.5, 4.22)])
    SIM.Fbg_vs_stol = bg
    #SIM.amorphous_sample_thick_mm = 35 # between beamstop and collimator
    SIM.amorphous_sample_thick_mm = 10  # between beamstop and collimator
    SIM.amorphous_density_gcm3 = 1.2e-3
    SIM.amorphous_sample_molecular_weight_Da = 28  # nitrogen = N2
    print("amorphous_sample_size_mm=", SIM.amorphous_sample_size_mm)
    print("amorphous_sample_thick_mm=", SIM.amorphous_sample_thick_mm)
    print("amorphous_density_gcm3=", SIM.amorphous_density_gcm3)
    print("amorphous_molecular_weight_Da=", SIM.amorphous_molecular_weight_Da)
    SIM.add_background()

    #apply beamstop mask here

    # set this to 0 or -1 to trigger automatic radius.  could be very slow with bright images
    # settings for CCD
    SIM.detector_psf_kernel_radius_pixels = 5
    #SIM.detector_psf_fwhm_mm=0.08;
    #SIM.detector_psf_type=shapetype.Fiber # rayonix=Fiber, CSPAD=None (or small Gaussian)
    SIM.detector_psf_type = shapetype.Unknown  # for CSPAD
    SIM.detector_psf_fwhm_mm = 0
    #SIM.apply_psf()
    print("One pixel-->", SIM.raw_pixels[500000])

    # at this point we scale the raw pixels so that the output array is on an scale from 0 to 50000.
    # that is the default behavior (intfile_scale<=0), otherwise it applies intfile_scale as a multiplier on an abs scale.
    if quick: SIM.to_smv_format(fileout=prefix + "_intimage_003.img")

    print("quantum_gain=",
          SIM.quantum_gain)  #defaults to 1. converts photons to ADU
    print("adc_offset_adu=", SIM.adc_offset_adu)
    print("detector_calibration_noise_pct=",
          SIM.detector_calibration_noise_pct)
    print("flicker_noise_pct=", SIM.flicker_noise_pct)
    print("readout_noise_adu=", SIM.readout_noise_adu
          )  # gaussian random number to add to every pixel (0 for PAD)
    # apply Poissonion correction, then scale to ADU, then adc_offset.
    # should be 10 for most Rayonix, Pilatus should be 0, CSPAD should be 0.

    print("detector_psf_type=", SIM.detector_psf_type)
    print("detector_psf_fwhm_mm=", SIM.detector_psf_fwhm_mm)
    print("detector_psf_kernel_radius_pixels=",
          SIM.detector_psf_kernel_radius_pixels)
    SIM.add_noise()  #converts phtons to ADU.

    print("raw_pixels=", SIM.raw_pixels)
    extra = "PREFIX=%s;\nRANK=%d;\n" % (prefix, rank)
    SIM.to_smv_format_py(fileout=smv_fileout,
                         intfile_scale=1,
                         rotmat=True,
                         extra=extra,
                         gz=True)

    # try to write as CBF
    if False:
        import dxtbx
        from dxtbx.format.FormatCBFMiniPilatus import FormatCBFMiniPilatus
        img = dxtbx.load(prefix + ".img")
        print(img)
        FormatCBFMiniPilatus.as_file(detector=img.get_detector(),
                                     beam=img.get_beam(),
                                     gonio=img.get_goniometer(),
                                     scan=img.get_scan(),
                                     data=img.get_raw_data(),
                                     path=prefix + ".cbf")
    SIM.free_all()
Ejemplo n.º 59
0
def run (args, image = None):
  from xfel import radial_average
  from scitbx.array_family import flex
  import os, sys
  import dxtbx

  # Parse input
  try:
    n = len(args)
  except Exception:
    params = args
  else:
    user_phil = []
    for arg in args:
      if (not "=" in arg):
        try :
          user_phil.append(libtbx.phil.parse("""file_path=%s""" % arg))
        except ValueError:
          raise Sorry("Unrecognized argument '%s'" % arg)
      else:
        try:
          user_phil.append(libtbx.phil.parse(arg))
        except RuntimeError as e:
          raise Sorry("Unrecognized argument '%s' (error: %s)" % (arg, str(e)))
    params = master_phil.fetch(sources=user_phil).extract()
  if image is None:
    if params.file_path is None or len(params.file_path) == 0 or not all([os.path.isfile(f) for f in params.file_path]):
      master_phil.show()
      raise Usage("file_path must be defined (either file_path=XXX, or the path alone).")
  assert params.n_bins is not None
  assert params.verbose is not None
  assert params.output_bins is not None

  # Allow writing to a file instead of stdout
  if params.output_file is None:
    logger = sys.stdout
  else:
    logger = open(params.output_file, 'w')
    logger.write("%s "%params.output_file)

  if params.show_plots:
    from matplotlib import pyplot as plt
    import numpy as np
    colormap = plt.cm.gist_ncar
    plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, len(params.file_path))])

  if params.mask is not None:
    params.mask = easy_pickle.load(params.mask)

  if image is None:
    iterable = params.file_path
    load_func = lambda x: dxtbx.load(x)
  else:
    iterable = [image]
    load_func = lambda x: x

  # Iterate over each file provided
  for item in iterable:
    img = load_func(item)
    try:
      n_images = img.get_num_images()
      subiterable = xrange(n_images)
    except AttributeError:
      n_images = None
      subiterable = [0]
    for image_number in subiterable:
      if n_images is None:
        beam = img.get_beam()
        detector = img.get_detector()
      else:
        beam = img.get_beam(image_number)
        detector = img.get_detector(image_number)
      s0 = col(beam.get_s0())

      # Search the detector for the panel farthest from the beam. The number of bins in the radial average will be
      # equal to the farthest point from the beam on the detector, in pixels, unless overridden at the command line
      panel_res = [p.get_max_resolution_at_corners(s0) for p in detector]
      farthest_panel = detector[panel_res.index(min(panel_res))]
      size2, size1 = farthest_panel.get_image_size()
      corners = [(0,0), (size1-1,0), (0,size2-1), (size1-1,size2-1)]
      corners_lab = [col(farthest_panel.get_pixel_lab_coord(c)) for c in corners]
      corner_two_thetas = [farthest_panel.get_two_theta_at_pixel(s0, c) for c in corners]
      extent_two_theta = max(corner_two_thetas)
      max_corner = corners_lab[corner_two_thetas.index(extent_two_theta)]
      extent = int(math.ceil(max_corner.length()*math.sin(extent_two_theta)/max(farthest_panel.get_pixel_size())))
      extent_two_theta *= 180/math.pi

      if params.n_bins < extent:
        params.n_bins = extent

      # These arrays will store the radial average info
      sums    = flex.double(params.n_bins) * 0
      sums_sq = flex.double(params.n_bins) * 0
      counts  = flex.int(params.n_bins) * 0

      if n_images is None:
        all_data = img.get_raw_data()
      else:
        all_data = img.get_raw_data(image_number)

      if not isinstance(all_data, tuple):
        all_data = (all_data,)

      for tile, (panel, data) in enumerate(zip(detector, all_data)):
        if params.mask is None:
          mask = flex.bool(flex.grid(data.focus()), True)
        else:
          mask = params.mask[tile]

        if hasattr(data,"as_double"):
          data = data.as_double()

        logger.flush()
        if params.verbose:
          logger.write("Average intensity tile %d: %9.3f\n"%(tile, flex.mean(data)))
          logger.write("N bins: %d\n"%params.n_bins)
          logger.flush()

        x1,y1,x2,y2 = 0,0,panel.get_image_size()[1],panel.get_image_size()[0]
        bc = panel.get_beam_centre_px(beam.get_s0())
        bc = int(round(bc[1])), int(round(bc[0]))

        # compute the average
        radial_average(data,mask,bc,sums,sums_sq,counts,panel.get_pixel_size()[0],panel.get_distance(),
                       (x1,y1),(x2,y2))

      # average the results, avoiding division by zero
      results = sums.set_selected(counts <= 0, 0)
      results /= counts.set_selected(counts <= 0, 1).as_double()

      if params.median_filter_size is not None:
        logger.write("WARNING, the median filter is not fully propogated to the variances\n")
        from scipy.ndimage.filters import median_filter
        results = flex.double(median_filter(results.as_numpy_array(), size = params.median_filter_size))

      # calculate standard devations
      stddev_sel = ((sums_sq-sums*results) >= 0) & (counts > 0)
      std_devs = flex.double(len(sums), 0)
      std_devs.set_selected(stddev_sel,
                           (sums_sq.select(stddev_sel)-sums.select(stddev_sel)* \
                            results.select(stddev_sel))/counts.select(stddev_sel).as_double())
      std_devs = flex.sqrt(std_devs)

      twotheta = flex.double(xrange(len(results)))*extent_two_theta/params.n_bins
      q_vals = 4*math.pi*flex.sin(math.pi*twotheta/360)/beam.get_wavelength()

      if params.low_max_two_theta_limit is None:
        subset = results
      else:
        subset = results.select(twotheta >= params.low_max_two_theta_limit)

      max_result = flex.max(subset)

      if params.x_axis == 'two_theta':
        xvals = twotheta
        max_x = twotheta[flex.first_index(results, max_result)]
      elif params.x_axis == 'q':
        xvals = q_vals
        max_x = q_vals[flex.first_index(results, max_result)]

      for i in xrange(len(results)):
        val = xvals[i]
        if params.output_bins and "%.3f"%results[i] != "nan":
         #logger.write("%9.3f %9.3f\n"%     (val,results[i]))        #.xy  format for Rex.cell.
          logger.write("%9.3f %9.3f %9.3f\n"%(val,results[i],std_devs[i])) #.xye format for GSASII
         #logger.write("%.3f %.3f %.3f\n"%(val,results[i],ds[i]))  # include calculated d spacings
      logger.write("Maximum %s: %f, value: %f\n"%(params.x_axis, max_x, max_result))

      if params.show_plots:
        if params.plot_x_max is not None:
          results = results.select(xvals <= params.plot_x_max)
          xvals = xvals.select(xvals <= params.plot_x_max)
        if params.normalize:
          plt.plot(xvals.as_numpy_array(),(results/flex.max(results)).as_numpy_array(),'-')
        else:
          plt.plot(xvals.as_numpy_array(),results.as_numpy_array(),'-')
        if params.x_axis == 'two_theta':
          plt.xlabel("2 theta")
        elif params.x_axis == 'q':
          plt.xlabel("q")
        plt.ylabel("Avg ADUs")
        if params.plot_y_max is not None:
          plt.ylim(0, params.plot_y_max)

    if params.show_plots:
      #plt.legend([os.path.basename(os.path.splitext(f)[0]) for f in params.file_path], ncol=2)
      plt.show()

  return xvals, results
Ejemplo n.º 60
0
from cxid9114 import utils

MIN_SPOT_PER_HIT = 30
output_dir = "."

pickle_fname = sys.argv[1]
image_fname = sys.argv[2]
output_tag = sys.argv[3]

print('Loading reflections')
with open(pickle_fname, 'r') as f:
    found_refl = cPickle.load(f)
refl_select = count_spots.ReflectionSelect(found_refl)

print('Loading format')
loader = dxtbx.load(image_fname)
imgset = loader.get_imageset(loader.get_image_file())

print('Counting spots')
idx, Nspot_at_idx = count_spots.count_spots(pickle_fname)
where_hits = np.where(Nspot_at_idx > MIN_SPOT_PER_HIT)[0]
Nhits = where_hits.shape[0]

# ============
output_h5_name = os.path.join(
    output_dir, "run%d_hits_%s.h5" % (loader.run_number, output_tag))

with h5py.File(output_h5_name, "w") as out_h5:

    out_h5.create_dataset("panel_masks", data=loader.cspad_mask, dtype=np.bool)
    out_h5.create_dataset("panel_gainmasks", data=loader.gain)