Example #1
0
def exercise_miller_export_as_shelx_hklf():
  s = """\
   1   2  -1   23.34    4.56
   2  -3   9   12.45    6.12
99999999999999999.9999999.99
-999-999-999-9999.99-9999.99
   3   4   5999999.99999999.
   3   4   5-99999.9-999999.
"""
  ma = hklf.reader(file_object=StringIO(s)).as_miller_arrays()[0]
  sio = StringIO()
  ma.export_as_shelx_hklf(file_object=sio)
  ma2 = hklf.reader(file_object=StringIO(sio.getvalue())).as_miller_arrays()[0]
  assert approx_equal(ma.indices(), ma2.indices())
  assert approx_equal(ma.data(), ma2.data())
  assert approx_equal(ma.sigmas(), ma2.sigmas())
  #
  ma = ma.select(flex.size_t([0]))
  def check(d, s, f):
    if (s is not None): s = flex.double([s])
    ma2 = ma.array(data=flex.double([d]), sigmas=s)
    sio = StringIO()
    ma2.export_as_shelx_hklf(sio, normalise_if_format_overflow=True)
    assert not show_diff(sio.getvalue(), """\
   1   2  -1%s
   0   0   0    0.00    0.00
""" % f)
    try: ma2.export_as_shelx_hklf(sio)
    except RuntimeError: pass
    else: raise Exception_expected
  check(-12345678, 1, "-999999.    0.08")
  check(-12345678, None, "-999999.    0.00")
  check(2, -12345678, "    0.16-999999.")
  check(123456789, 30, "9999999.    2.43")
  check(123456789, None, "9999999.    0.00")
  check(40, 123456789, "    3.249999999.")
  check(-23456789, 123456789, "-999999.5263153.")
  check(123456789, -23456789, "5263153.-999999.")
  #
  ma = hklf.reader(file_object=StringIO(s)).as_miller_arrays()[0]
  ma = ma.select(flex.size_t([0,1]))
  ma2 = ma.array(data=flex.double([123456789, -23456789]))
  sio = StringIO()
  ma2.export_as_shelx_hklf(sio, normalise_if_format_overflow=True)
  assert not show_diff(sio.getvalue(), """\
   1   2  -15263153.    0.00
   2  -3   9-999999.    0.00
   0   0   0    0.00    0.00
""")
  ma2 = ma.array(data=flex.double([-23456789, 823456789]))
  sio = StringIO()
  ma2.export_as_shelx_hklf(sio, normalise_if_format_overflow=True)
  assert not show_diff(sio.getvalue(), """\
   1   2  -1-284858.    0.00
   2  -3   99999999.    0.00
   0   0   0    0.00    0.00
""")
Example #2
0
 def _set_res(self, event):
     if len(self._ref_files) > 0 and len(self._in_files) > 0:        
         ref = self._ref_files[self._reflections.GetCurrentSelection()]
         ins = self._in_files[self._structure.GetCurrentSelection()]
         
         
         if os.path.exists(ref) and os.path.exists(ins):
             hkl = hklf.reader(open(ref))
             try:
                 ma = hkl.as_miller_arrays(crystal_symmetry=crystal_symmetry_from_ins.extract_from(ins))
             except:
                 return
                 
             rr = ma[0].resolution_range()
             
             self._res_high.SetValue(str(round(rr[1], 2)))
             self._res_low.SetValue(str(round(rr[0], 2)))
             
             ins_obj = Shelx(ins)
             self._hydrogens.SetValue(ins_obj.has_hydrogen())
             
             self._residues.Clear()
             self._residue_list = []
             for r in sorted(ins_obj.residue_list()):
                 if r not in (ShelxData._r + ['HOH', 'DOD', 'CL', 'MG']) or (r in ['ASP', 'GLU', 'HIS', 'ARG']):
                     self._residues.Append(r)
                     self._residue_list.append(r)
class any_reflection_file(object):
    """
  Proxy object for reading a reflection file of unspecified format, and
  extracting the Miller arrays contained therein.

  Examples
  --------
  >>> from iotbx.reflection_file_reader import any_reflection_file
  >>> hkl_file = any_reflection_file("data.mtz")
  >>> print hkl_file.file_type()
  'ccp4_mtz'
  >>> print type(hkl_file.file_content())
  <class 'iotbx_mtz_ext.object'>
  >>> miller_arrays = hkl_file.as_miller_arrays()
  """
    def __init__(self, file_name, ensure_read_access=True, strict=True):
        # strict is no longer used
        if (file_name.startswith("amplitudes=")
                or file_name.startswith("hklf3=")
                or file_name.startswith("intensities=")
                or file_name.startswith("hklf4=")
                or file_name.startswith("hklf+ins/res=")):
            self._observation_type, self._file_name = file_name.split("=", 1)
        elif (file_name.endswith("=amplitudes") or file_name.endswith("=hklf3")
              or file_name.endswith("=intensities")
              or file_name.endswith("=hklf4")
              or file_name.endswith("=hklf+ins/res")):
            self._file_name, self._observation_type = file_name.split("=", 1)
        else:
            self._file_name = file_name
            self._observation_type = None
        if (self._observation_type == "hklf3"):
            self._observation_type = "amplitudes"
        elif (self._observation_type == "hklf4"):
            self._observation_type = "intensities"
        self._file_type = None
        file_name = self._file_name
        try:
            open(file_name)  # test read access
        except IOError, e:
            if (ensure_read_access):
                raise Sorry(str(e))
            return
        if (self._observation_type is not None):
            try:
                self._file_content = shelx_hklf.reader(file_name=file_name)
            except KeyboardInterrupt:
                raise
            except Exception:
                raise Sorry(
                    "Not a SHELX reflection file: %s\n"
                    "  =%s can only be used for SHELX reflection files." %
                    (file_name, self._observation_type))
            else:
                self._file_type = "shelx_hklf"
        else:
            self._file_type, self._file_content = try_all_readers(
                file_name=file_name)
Example #4
0
    def load_refinement(self, ref, stats):
        hkl = hklf.reader(open(ref.replace('.dat', '.hkl')))
        pdbf = pdb.input(file_name=ref.replace('.dat', '.pdb'))
        pdbt = PDBTools(file_name=ref.replace('.dat', '.pdb'))
        
        cs = crystal_symmetry_from_ins.extract_from(ref.replace('.dat', '.ins'))
        ma = hkl.as_miller_arrays(crystal_symmetry=cs)
        
        mp = pdbf.xray_structure_simple().scatterers()[0].multiplicity()
        s = pdbt.get_s()
        vm = cs.unit_cell().volume() / (pdbt.get_rmm() * mp)
        
        c = ma[0].completeness(d_max=stats['res'])
        dmin = stats['res']
        r = stats['r']
        rfree = stats['rfree']

        self._mv.SetLabel('%.2f' % vm)
        self._s.SetLabel('%.2f' % s)
        self._dmin.SetLabel('%.2f' % dmin)
        self._c.SetLabel('%.1f' % (c*100))
        self._r.SetLabel('%.3f' % r)
        self._rfree.SetLabel('%.3f' % rfree)
        
        sigxr = self._sigx_r(s, vm, c, r, dmin)
        self._sigxr.SetLabel('%.4f' % sigxr)
        sigxrf = self._sigx_rfree(s, vm, c, rfree, dmin)
        self._sigxrf.SetLabel('%.4f' % sigxrf)
        
        x = np.linspace(dmin+0.5,dmin-0.5, 20)
        ys = [[],[],[],[],[],[]]
        for i in x:
            sr = self._sigx_r(s, vm, c, r, i)
            ys[0].append(sr)
            ys[1].append(sr*1.15)
            ys[2].append(sr*0.85)
            
            sr = self._sigx_rfree(s, vm, c, rfree, i)
            ys[3].append(sr)
            ys[4].append(sr*1.15)
            ys[5].append(sr*0.85)
    
        self.ax1.plot(x, ys[0], 'b-', label='R')
        self.ax1.fill_between(x, ys[1], ys[2], alpha=0.1, facecolor='blue')

        self.ax1.plot(x, ys[3], 'g-', label='Rfree')
        self.ax1.fill_between(x, ys[4], ys[5], alpha=0.1, facecolor='green')
                
        al = self.ax1.axis()
        yam = al[3]-al[2]
        xam = al[1]-al[0]

        self.ax1.axhline(y=0.0236, color='purple', label='3sig (ASP)')
                
        self.ax1.axvline(x=dmin, ymin=0, ymax=(sigxr-al[2])/yam, color='0.7', label='Actual')
        self.ax1.axhline(y=sigxr, xmin=0, xmax=(dmin-al[0])/xam, color='0.7')
        self.ax1.legend(prop={'size':8}, numpoints=1, loc='best', fancybox=True)
def structure_factors_from_hkl(hkl_path, xs, weighting_scheme=None):
  fo2 = hklf.reader(file_name=hkl_path).as_miller_arrays(
    crystal_symmetry=xs)[0]
  fo2.set_observation_type_xray_intensity()
  merging = fo2.merge_equivalents(algorithm="shelx")
  fo2 = merging.array()
  xs.scattering_type_registry(table="it1992", d_min=fo2.d_min())
  fc = fo2.structure_factors_from_scatterers(xs, algorithm="direct").f_calc()
  scale = fo2.scale_factor(fc)
  if weighting_scheme is not None:
    weights = weighting_scheme(
      fo2.data(), fo2.sigmas(), fc.as_intensity_array().data(), scale)
    scale = fo2.scale_factor(fc, weights=weights)
  return xs, fo2, fc, scale
def structure_factors_from_hkl(hkl_path, xs, weighting_scheme=None):
    fo2 = hklf.reader(file_name=hkl_path).as_miller_arrays(
        crystal_symmetry=xs)[0]
    fo2.set_observation_type_xray_intensity()
    merging = fo2.merge_equivalents(algorithm="shelx")
    fo2 = merging.array()
    xs.scattering_type_registry(table="it1992", d_min=fo2.d_min())
    fc = fo2.structure_factors_from_scatterers(xs, algorithm="direct").f_calc()
    scale = fo2.scale_factor(fc)
    if weighting_scheme is not None:
        weights = weighting_scheme(fo2.data(), fo2.sigmas(),
                                   fc.as_intensity_array().data(), scale)
        scale = fo2.scale_factor(fc, weights=weights)
    return xs, fo2, fc, scale
Example #7
0
def shelxd_read_hklf(fa_file, fa_ins, d_min, n_bins):
    '''Normalise Fa values output by SHELX'''

    fa_data = hklf.reader(file_name=fa_file)
    c = crystal_symmetry_from_ins.extract_from(file_name=fa_ins)

    fsigf_all = fa_data.as_miller_arrays(crystal_symmetry=c)[0]
    fsigf = fsigf_all.select(fsigf_all.d_spacings().data()>d_min)
    fsigf.setup_binner(n_bins=n_bins)

    ea = fsigf.quasi_normalize_structure_factors()
    ea.setup_binner(n_bins=n_bins)
    ea_weak = ea.select((abs(ea.data())<1.5))
    ea_weak.setup_binner(n_bins=n_bins)
    return ea, ea_weak
 def __init__(self, file_name, ensure_read_access=True, strict=True):
     # strict is no longer used
     if (file_name.startswith("amplitudes=")
             or file_name.startswith("hklf3=")
             or file_name.startswith("intensities=")
             or file_name.startswith("hklf4=")
             or file_name.startswith("hklf+ins/res=")):
         self._observation_type, self._file_name = file_name.split("=", 1)
     elif (file_name.endswith("=amplitudes") or file_name.endswith("=hklf3")
           or file_name.endswith("=intensities")
           or file_name.endswith("=hklf4")
           or file_name.endswith("=hklf+ins/res")):
         self._file_name, self._observation_type = file_name.split("=", 1)
     else:
         self._file_name = file_name
         self._observation_type = None
     if (self._observation_type == "hklf3"):
         self._observation_type = "amplitudes"
     elif (self._observation_type == "hklf4"):
         self._observation_type = "intensities"
     self._file_type = None
     file_name = self._file_name
     try:
         with open(file_name):  # test read access
             pass
     except IOError as e:
         if (ensure_read_access):
             raise Sorry(str(e))
         return
     if (self._observation_type is not None):
         try:
             self._file_content = shelx_hklf.reader(file_name=file_name)
         except KeyboardInterrupt:
             raise
         except Exception:
             raise Sorry(
                 "Not a SHELX reflection file: %s\n"
                 "  =%s can only be used for SHELX reflection files." %
                 (file_name, self._observation_type))
         else:
             self._file_type = "shelx_hklf"
     else:
         self._file_type, self._file_content = try_all_readers(
             file_name=file_name)
def try_all_readers(file_name):
  try: content = mtz.object(file_name=file_name)
  except RuntimeError: pass
  else: return ("ccp4_mtz", content)
  if (detect_binary_file.from_initial_block(file_name=file_name)):
    try: content = unpickle_miller_arrays(file_name=file_name)
    except KeyboardInterrupt: raise
    except Exception: pass
    else: return ("cctbx.miller.array", content)
    return (None, None)
  try: content = cns_reflection_reader.cns_reflection_file(
    open(file_name))
  except cns_reflection_reader.CNS_input_Error: pass
  else: return ("cns_reflection_file", content)
  try: content = cns_index_fobs_sigma_reader.reader(
    file_name=file_name)
  except RuntimeError: pass
  else: return ("cns_index_fobs_sigma", content)
  try: content = scalepack_merge.reader(
    open(file_name))
  except scalepack_merge.FormatError: pass
  else: return ("scalepack_merge", content)
  try: content = scalepack_no_merge.reader(file_name)
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("scalepack_no_merge_original_index", content)
  try: content = dtrek_reflnlist_reader.reflnlist(
    open(file_name))
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("dtrek_reflnlist", content)
  try: content = shelx_hklf.reader(
    file_name=file_name)
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("shelx_hklf", content)
  try:
    content = cif_reader(file_path=file_name)
    looks_like_a_reflection_file = False
    for block in content.model().values():
      if '_refln_index_h' in block or '_refln.index_h' in block:
        looks_like_a_reflection_file = True
        break
    if not looks_like_a_reflection_file:
      raise RuntimeError
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("cif", content)
  try: content = xds_ascii_reader(
    open(file_name))
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("xds_ascii", content)
  try:
    content = xds_integrate_hkl_reader()
    content.read_file(file_name)
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("xds_integrate_hkl", content)
  try: content = solve_fpfm_reader(file_name=file_name)
  except KeyboardInterrupt: raise
  except Exception: pass
  else: return ("solve_fpfm", content)
  return (None, None)
Example #10
0
def excersise():
    s = """\
  -1  -6   9624.8650 54.9190   1
  -1   6   9749.5660 52.1870  -2
   1   6  -9749.5660 52.1870   1
   1  -6  -8695.6020 53.8100  -2
  -1  -6   8695.6020 53.8100   1
  -1   6   8746.7970 51.3980  -2
   1   6  -8746.7970 51.3980   1
  -1  -6 -12281.3590 49.5060   1
   1   6  12185.9370 47.3950   1
  -1  -6 -1316.0700044.01400   1
   1   6  13 4.7500043.54900   1
  -1  -6 -1479.6380048.66400   2
   1   6  1432.7830051.51700   2
  """
    sg = sgtbx.space_group("P 1")
    uc = uctbx.unit_cell((11, 11, 13, 90, 90, 120))
    cs = crystal.symmetry(unit_cell=uc, space_group=sg)
    ma = hklf.reader(file_object=StringIO(s))\
           .as_miller_arrays(crystal_symmetry=cs, merge_equivalents=False)
    fo_sq = ma[0]
    batch_numbers = ma[1]
    obs = fo_sq.as_xray_observations(scale_indices=batch_numbers.data(),
                                     twin_fractions=(xray.twin_fraction(
                                         0.4, True), ))
    measured_cnt = 0
    measured_scale_indices = obs.measured_scale_indices
    for bn in batch_numbers.data():
        if bn > 0:
            assert (measured_scale_indices[measured_cnt] == bn)
            measured_cnt = measured_cnt + 1
    assert (measured_cnt == obs.indices.size())

    itr = obs.iterator(0)
    assert (not itr.has_next())
    itr = obs.iterator(1)
    assert (itr.next().h == (-1, 6, 9))
    itr = obs.iterator(2)
    assert (itr.next().h == (1, -6, -8))

    # this is supposed to fail for now until somebody re-implements
    # mixed HKLF 5 + merohedral twinning in a meaningful way...
    try:
        obs = observations.customized_copy(
            obs,
            twin_fractions=(xray.twin_fraction(0.7, True), ),
            twin_components=(xray.twin_component(
                sgtbx.rot_mx((-1, 0, 0, 0, -1, 0, 0, 0, -1)), 0.25, True), ))

        itr = obs.iterator(0)
        assert (itr.has_next())
        assert (itr.next().h == (1, 6, -9))
        assert (not itr.has_next())
        itr = obs.iterator(1)
        assert (itr.next().h == (-1, -6, 9))
        assert (itr.next().h == (-1, 6, 9))
        assert (itr.next().h == (1, -6, -9))
        assert (not itr.has_next())

        ts = 1 - obs.ref_twin_components[0].value
        ps = 1 - obs.ref_twin_fractions[0].value
        itr = obs.iterator(0)
        assert obs.scale(0) == ts * ps
        nv = next(itr)
        assert nv.scale == obs.ref_twin_components[0].value * ps
    except RuntimeError as e:
        pass
Example #11
0
def try_all_readers(file_name):
    try:
        content = mtz.object(file_name=file_name)
    except RuntimeError:
        pass
    else:
        return ("ccp4_mtz", content)
    if (detect_binary_file.from_initial_block(file_name=file_name)):
        try:
            content = unpickle_miller_arrays(file_name=file_name)
        except KeyboardInterrupt:
            raise
        except Exception:
            pass
        else:
            return ("cctbx.miller.array", content)
        return (None, None)
    try:
        content = cns_reflection_reader.cns_reflection_file(open(file_name))
    except cns_reflection_reader.CNS_input_Error:
        pass
    else:
        return ("cns_reflection_file", content)
    try:
        content = cns_index_fobs_sigma_reader.reader(file_name=file_name)
    except RuntimeError:
        pass
    else:
        return ("cns_index_fobs_sigma", content)
    try:
        content = scalepack_merge.reader(open(file_name))
    except scalepack_merge.FormatError:
        pass
    else:
        return ("scalepack_merge", content)
    try:
        content = scalepack_no_merge.reader(file_name)
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("scalepack_no_merge_original_index", content)
    try:
        content = dtrek_reflnlist_reader.reflnlist(open(file_name))
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("dtrek_reflnlist", content)
    try:
        content = shelx_hklf.reader(file_name=file_name)
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("shelx_hklf", content)
    try:
        content = cif_reader(file_path=file_name)
        looks_like_a_reflection_file = False
        for block in content.model().values():
            if '_refln_index_h' in block or '_refln.index_h' in block:
                looks_like_a_reflection_file = True
                break
        if not looks_like_a_reflection_file:
            raise RuntimeError
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("cif", content)
    try:
        content = xds_ascii_reader(open(file_name))
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("xds_ascii", content)
    try:
        content = xds_integrate_hkl_reader()
        content.read_file(file_name)
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("xds_integrate_hkl", content)
    try:
        content = solve_fpfm_reader(file_name=file_name)
    except KeyboardInterrupt:
        raise
    except Exception:
        pass
    else:
        return ("solve_fpfm", content)
    return (None, None)
Example #12
0
def exercise_hklf_reader():
  s = ('   1   2  -1  -23.34    4.56   1\n'
       '   2  -3   9   12.45    6.12   2\r\n'
       '99999999999999999.9999999.999999\n'
       '-999-999-999-9999.99-9999.99-999\r\n'
       '   0   0   0    0.00    0.00   0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [
    (1, 2, -1), (2, -3, 9), (9999, 9999, 9999), (-999, -999, -999), ]
  assert approx_equal(r.data(), [-23.34, 12.45, 99999.99, -9999.99, ])
  assert approx_equal(r.sigmas(), [4.56, 6.12, 99999.99, -9999.99, ])
  assert approx_equal(r.batch_numbers(), [1, 2, 9999, -999, ])
  assert approx_equal(r.alphas(), [1, 2, 9999, -999, ])
  for ma in r.as_miller_arrays():
    assert ma.indices().all_eq(r.indices())
    assert ma.anomalous_flag() is False
  ma = r.as_miller_arrays()[0]
  assert ma.data().all_approx_equal(r.data())
  assert ma.sigmas().all_approx_equal(r.sigmas())
  ma = r.as_miller_arrays()[1]
  assert ma.data().all_eq(r.alphas())
  assert ma.sigmas() is None

  s = ('   0   2   3 1816.00   20.00\n'
       '   0   2   415508.00  138.00\n'
       '   0   2   5 4776.00   40.00\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [ (0,2,3), (0,2,4), (0,2,5) ]
  assert r.batch_numbers() is None
  assert r.alphas() is None
  assert r.wavelengths() is None

  for end_line in (True, False):
    for something_after_end_line in (True, False):
      s = ('   1   2  -1   23.34    4.56\n'
           '   2  -3   9   12.45    6.12\r\n'
           '99999999999999999.9999999.99\n'
           '-999-999-999-9999.99-9999.99\n')
      if end_line:
        s += '   0   0   0    0.00    0.00\n'
        if something_after_end_line:
          s += '  -5   1   0  123.45   66.12\n'
      s = (s)
      r = hklf.reader(file_object=StringIO(s))
      assert approx_equal(r.sigmas(), [4.56, 6.12, 99999.99, -9999.99, ])
      assert r.batch_numbers() is None
      assert r.alphas() is None
      assert r.wavelengths() is None

  s = ''
  try: r = hklf.reader(file_object=StringIO(s))
  except RuntimeError: pass
  else: raise Exception_expected

  s = ('   1   2  -1   23.34    4.56   1\n'
       '   2  -3  a9   12.45    6.12   2\n'
       '   0   0   0    0.00    0.00   0\n')
  try: r = hklf.reader(file_object=StringIO(s))
  except Exception: pass
  else: raise Exception_expected

  s = ('   1   2  -1   23.34    4.56   1   45.36\n'
       '   2  -3   9  -12.45   -6.12   2   54.63\n'
       '   0   0   0    0.00    0.00   0       0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [ (1, 2, -1), (2, -3, 9)]
  assert approx_equal(r.data(), [23.34, -12.45])
  assert approx_equal(r.sigmas(), [4.56, -6.12])
  assert approx_equal(r.batch_numbers(), [1, 2])
  assert approx_equal(r.wavelengths(), [45.36, 54.63])

  s = ('   1   2  -1     23.      4.   2\n'
       '  -2   1   3     -1.      3.   1\n'
       '   0   0   0      0.      0.   0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [(1, 2, -1), (-2, 1, 3)]
  assert approx_equal(r.data(), [23, -1])
  assert approx_equal(r.sigmas(), [4, 3])
  assert approx_equal(r.alphas(), [2, 1])
  assert r.wavelengths() is None

  s = ('   3   2  -1     32.      5.\n'
       '   0   0   0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [(3, 2, -1)]
  assert approx_equal(r.data(), [32])
  assert approx_equal(r.sigmas(), [5])
  assert r.alphas() is None
  assert r.wavelengths() is None

  s = (
"""King Arthur: [after Arthur's cut off both of the Black Knight's arms]
  Look, you stupid Bastard. You've got no arms left.
Black Knight: Yes I have.
King Arthur: *Look*!
Black Knight: It's just a flesh wound.""")
  try: r = hklf.reader(file_object=StringIO(s))
  except RuntimeError: pass
  else: raise Exception_expected
def excersise():
  s = """\
  -1  -6   9624.8650 54.9190   1
  -1   6   9749.5660 52.1870  -2
   1   6  -9749.5660 52.1870   1
   1  -6  -8695.6020 53.8100  -2
  -1  -6   8695.6020 53.8100   1
  -1   6   8746.7970 51.3980  -2
   1   6  -8746.7970 51.3980   1
  -1  -6 -12281.3590 49.5060   1
   1   6  12185.9370 47.3950   1
  -1  -6 -1316.0700044.01400   1
   1   6  13 4.7500043.54900   1
  -1  -6 -1479.6380048.66400   2
   1   6  1432.7830051.51700   2
  """
  sg = sgtbx.space_group("P 1")
  uc = uctbx.unit_cell((11,11,13,90,90,120))
  cs = crystal.symmetry(unit_cell=uc, space_group=sg)
  ma = hklf.reader(file_object=StringIO(s))\
         .as_miller_arrays(crystal_symmetry=cs, merge_equivalents=False)
  fo_sq = ma[0]
  batch_numbers = ma[1]
  obs = fo_sq.as_xray_observations(
          scale_indices=batch_numbers.data(),
          twin_fractions=(xray.twin_fraction(0.4,True),))
  measured_cnt = 0
  measured_scale_indices = obs.measured_scale_indices
  for bn in batch_numbers.data():
    if bn > 0:
      assert(measured_scale_indices[measured_cnt]==bn)
      measured_cnt = measured_cnt+1
  assert(measured_cnt == obs.indices.size())

  itr = obs.iterator(0)
  assert(not itr.has_next())
  itr = obs.iterator(1)
  assert(itr.next().h==(-1,6,9))
  itr = obs.iterator(2)
  assert(itr.next().h==(1,-6,-8))

  obs = observations.customized_copy(obs,
          twin_fractions=(xray.twin_fraction(0.7,True),),
          twin_components=(xray.twin_component(
              sgtbx.rot_mx((-1,0,0,0,-1,0,0,0,-1)), 0.25, True),))

  itr = obs.iterator(0)
  assert(itr.has_next())
  assert(itr.next().h==(1,6,-9))
  assert(not itr.has_next())
  itr = obs.iterator(1)
  assert(itr.next().h==(-1,-6,9))
  assert(itr.next().h==(-1,6,9))
  assert(itr.next().h==(1,-6,-9))
  assert(not itr.has_next())

  ts = 1-obs.ref_twin_components[0].value
  ps = 1-obs.ref_twin_fractions[0].value
  itr = obs.iterator(0)
  assert obs.scale(0) == ts*ps
  nv = itr.next()
  assert nv.scale == obs.ref_twin_components[0].value*ps
Example #14
0
def exercise_hklf_reader():
  s = ('   1   2  -1  -23.34    4.56   1\n'
       '   2  -3   9   12.45    6.12   2\r\n'
       '99999999999999999.9999999.999999\n'
       '-999-999-999-9999.99-9999.99-999\r\n'
       '   0   0   0    0.00    0.00   0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [
    (1, 2, -1), (2, -3, 9), (9999, 9999, 9999), (-999, -999, -999), ]
  assert approx_equal(r.data(), [-23.34, 12.45, 99999.99, -9999.99, ])
  assert approx_equal(r.sigmas(), [4.56, 6.12, 99999.99, -9999.99, ])
  assert approx_equal(r.batch_numbers(), [1, 2, 9999, -999, ])
  assert approx_equal(r.alphas(), [1, 2, 9999, -999, ])
  for ma in r.as_miller_arrays():
    assert ma.indices().all_eq(r.indices())
  ma = r.as_miller_arrays()[0]
  assert ma.data().all_approx_equal(r.data())
  assert ma.sigmas().all_approx_equal(r.sigmas())
  ma = r.as_miller_arrays()[1]
  assert ma.data().all_eq(r.alphas())
  assert ma.sigmas() is None

  s = ('   0   2   3 1816.00   20.00\n'
       '   0   2   415508.00  138.00\n'
       '   0   2   5 4776.00   40.00\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [ (0,2,3), (0,2,4), (0,2,5) ]
  assert r.batch_numbers() is None
  assert r.alphas() is None
  assert r.wavelengths() is None

  for end_line in (True, False):
    for something_after_end_line in (True, False):
      s = ('   1   2  -1   23.34    4.56\n'
           '   2  -3   9   12.45    6.12\r\n'
           '99999999999999999.9999999.99\n'
           '-999-999-999-9999.99-9999.99\n')
      if end_line:
        s += '   0   0   0    0.00    0.00\n'
        if something_after_end_line:
          s += '  -5   1   0  123.45   66.12\n'
      s = (s)
      r = hklf.reader(file_object=StringIO(s))
      assert approx_equal(r.sigmas(), [4.56, 6.12, 99999.99, -9999.99, ])
      assert r.batch_numbers() is None
      assert r.alphas() is None
      assert r.wavelengths() is None

  s = ''
  try: r = hklf.reader(file_object=StringIO(s))
  except RuntimeError: pass
  else: raise Exception_expected

  s = ('   1   2  -1   23.34    4.56   1\n'
       '   2  -3  a9   12.45    6.12   2\n'
       '   0   0   0    0.00    0.00   0\n')
  try: r = hklf.reader(file_object=StringIO(s))
  except Exception: pass
  else: raise Exception_expected

  s = ('   1   2  -1   23.34    4.56   1   45.36\n'
       '   2  -3   9  -12.45   -6.12   2   54.63\n'
       '   0   0   0    0.00    0.00   0       0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [ (1, 2, -1), (2, -3, 9)]
  assert approx_equal(r.data(), [23.34, -12.45])
  assert approx_equal(r.sigmas(), [4.56, -6.12])
  assert approx_equal(r.batch_numbers(), [1, 2])
  assert approx_equal(r.wavelengths(), [45.36, 54.63])

  s = ('   1   2  -1     23.      4.   2\n'
       '  -2   1   3     -1.      3.   1\n'
       '   0   0   0      0.      0.   0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [(1, 2, -1), (-2, 1, 3)]
  assert approx_equal(r.data(), [23, -1])
  assert approx_equal(r.sigmas(), [4, 3])
  assert approx_equal(r.alphas(), [2, 1])
  assert r.wavelengths() is None

  s = ('   3   2  -1     32.      5.\n'
       '   0   0   0\n')
  r = hklf.reader(file_object=StringIO(s))
  assert list(r.indices()) == [(3, 2, -1)]
  assert approx_equal(r.data(), [32])
  assert approx_equal(r.sigmas(), [5])
  assert r.alphas() is None
  assert r.wavelengths() is None

  s = (
"""King Arthur: [after Arthur's cut off both of the Black Knight's arms]
  Look, you stupid Bastard. You've got no arms left.
Black Knight: Yes I have.
King Arthur: *Look*!
Black Knight: It's just a flesh wound.""")
  try: r = hklf.reader(file_object=StringIO(s))
  except RuntimeError: pass
  else: raise Exception_expected
Example #15
0
def exercise_miller_export_as_shelx_hklf():
    s = """\
   1   2  -1   23.34    4.56
   2  -3   9   12.45    6.12
99999999999999999.9999999.99
-999-999-999-9999.99-9999.99
   3   4   5999999.99999999.
   3   4   5-99999.9-999999.
"""
    ma = hklf.reader(file_object=StringIO(s)).as_miller_arrays()[0]
    sio = StringIO()
    ma.export_as_shelx_hklf(file_object=sio)
    ma2 = hklf.reader(
        file_object=StringIO(sio.getvalue())).as_miller_arrays()[0]
    assert approx_equal(ma.indices(), ma2.indices())
    assert approx_equal(ma.data(), ma2.data())
    assert approx_equal(ma.sigmas(), ma2.sigmas())
    assert ma.anomalous_flag() is False
    assert ma2.anomalous_flag() is False
    #
    ma = ma.select(flex.size_t([0]))

    def check(d, s, f, exception_expected=True):
        if (s is not None): s = flex.double([s])
        ma2 = ma.array(data=flex.double([d]), sigmas=s)
        sio = StringIO()
        ma2.export_as_shelx_hklf(sio, normalise_if_format_overflow=True)
        assert not show_diff(
            sio.getvalue(), """\
   1   2  -1%s
   0   0   0    0.00    0.00
""" % f)
        if exception_expected:
            try:
                ma2.export_as_shelx_hklf(sio)
            except RuntimeError:
                pass
            else:
                raise Exception_expected
        else:
            try:
                ma2.export_as_shelx_hklf(sio)
            except RuntimeError:
                raise Exception_not_expected

    check(-12345678, 1, "-999999.    0.08")
    check(-12345678, None, "-999999.    0.00")
    check(2, -12345678,
          "    0.16-999999.")  # Should negative sigma be allowed?
    check(123456789, 30, "9999999.    2.43")
    check(123456789, None, "9999999.    0.00")
    check(40, 123456789, "    3.249999999.")
    check(-23456789, 123456789, "-999999.5263153.")
    check(123456789, -23456789, "5263153.-999999.")
    # Numbers that would be formatted as integers should end with "."
    check(-123456, 1, "-123456.    1.00", exception_expected=False)
    check(1234567, 10, "1234567.   10.00", exception_expected=False)
    #
    ma = hklf.reader(file_object=StringIO(s)).as_miller_arrays()[0]
    ma = ma.select(flex.size_t([0, 1]))
    ma2 = ma.array(data=flex.double([123456789, -23456789]))
    sio = StringIO()
    ma2.export_as_shelx_hklf(sio, normalise_if_format_overflow=True)
    assert not show_diff(
        sio.getvalue(), """\
   1   2  -15263153.    0.00
   2  -3   9-999999.    0.00
   0   0   0    0.00    0.00
""")
    ma2 = ma.array(data=flex.double([-23456789, 823456789]))
    sio = StringIO()
    ma2.export_as_shelx_hklf(sio, normalise_if_format_overflow=True)
    assert not show_diff(
        sio.getvalue(), """\
   1   2  -1-284858.    0.00
   2  -3   99999999.    0.00
   0   0   0    0.00    0.00
""")
    # Test that setting the scale range works.
    ma2.export_as_shelx_hklf(sio,
                             scale_range=(-9999., 9999.),
                             normalise_if_format_overflow=True)
    # Test that ignoring the scale range and normalising out-of-range values anyway works.
    ma2.export_as_shelx_hklf(sio, full_dynamic_range=True)