Example #1
0
    def __init__(self,
                 xray_structure,
                 obs_,
                 exti=None,
                 connectivity_table=None):
        if exti is None:
            exti = xray.dummy_extinction_correction()
        adopt_init_args(self, locals())
        assert obs_.fo_sq.anomalous_flag()
        assert not (obs_.twin_fractions and obs_.merohedral_components)

        xray_structure = xray_structure.deep_copy_scatterers()
        for sc in xray_structure.scatterers():
            f = xray.scatterer_flags()
            f.set_use_u_aniso(sc.flags.use_u_aniso())
            f.set_use_u_iso(sc.flags.use_u_iso())
            f.set_use_fp_fdp(True)
            sc.flags = f

        twin_fractions = ()
        it = xray.twin_component(sgtbx.rot_mx((-1, 0, 0, 0, -1, 0, 0, 0, -1)),
                                 0.2, True)
        twin_components = (it, )
        obs = observations.customized_copy(obs_, twin_fractions,
                                           twin_components)
        # reparameterisation needs all fractions
        twin_fractions += twin_components
        if connectivity_table is None:
            connectivity_table = smtbx.utils.connectivity_table(xray_structure)
        reparametrisation = constraints.reparametrisation(
            xray_structure, [],
            connectivity_table,
            twin_fractions=twin_fractions,
            extinction=exti)
        normal_eqns = least_squares.crystallographic_ls(obs, reparametrisation)
        cycles = normal_eqns_solving.naive_iterations(normal_eqns,
                                                      n_max_iterations=10,
                                                      gradient_threshold=1e-7,
                                                      step_threshold=1e-4)
        self.flack_x = it.value
        self.sigma_x = math.sqrt(
            normal_eqns.covariance_matrix(
                jacobian_transpose=reparametrisation.
                jacobian_transpose_matching(
                    reparametrisation.mapping_to_grad_fc_independent_scalars))
            [0])
Example #2
0
  def __init__(self, xray_structure, obs_, exti=None, connectivity_table=None):
    if exti is None:
      exti = xray.dummy_extinction_correction()
    adopt_init_args(self, locals())
    assert obs_.fo_sq.anomalous_flag()
    xray_structure = xray_structure.deep_copy_scatterers()
    flags = xray_structure.scatterer_flags()
    for sc in xray_structure.scatterers():
      f = xray.scatterer_flags()
      f.set_use_u_aniso(sc.flags.use_u_aniso())
      f.set_use_u_iso(sc.flags.use_u_iso())
      f.set_use_fp_fdp(True)
      sc.flags = f

    twin_fractions = obs_.twin_fractions
    twin_components = obs_.merohedral_components
    for tw in twin_fractions: tw.grad = False
    for tc in twin_components: tc.grad = False

    it = xray.twin_component(sgtbx.rot_mx((-1,0,0,0,-1,0,0,0,-1)), 0.2, True)
    twin_components += (it,)
    obs = observations.customized_copy(obs_, twin_fractions, twin_components)
    # reparameterisation needs all fractions
    twin_fractions += twin_components
    if connectivity_table is None:
      connectivity_table = smtbx.utils.connectivity_table(xray_structure)
    reparametrisation = constraints.reparametrisation(
      xray_structure, [], connectivity_table,
      twin_fractions=twin_fractions,
      extinction=exti
    )
    normal_eqns = least_squares.crystallographic_ls(obs,
      reparametrisation)
    cycles = normal_eqns_solving.naive_iterations(
      normal_eqns, n_max_iterations=10,
      gradient_threshold=1e-7,
      step_threshold=1e-4)
    self.flack_x = it.value
    self.sigma_x = math.sqrt(normal_eqns.covariance_matrix(
      jacobian_transpose=reparametrisation.jacobian_transpose_matching(
        reparametrisation.mapping_to_grad_fc_independent_scalars))[0])
Example #3
0
        def build_up(self, objective_only=False):
            if self.f_mask is not None:
                f_mask = self.f_mask.data()
            else:
                f_mask = flex.complex_double()

            extinction_correction = self.reparametrisation.extinction
            if extinction_correction is None:
                extinction_correction = xray.dummy_extinction_correction()

            def build_normal_eqns(scale_factor, weighting_scheme,
                                  objective_only):
                return ext.build_normal_equations(
                    self, self.observations, f_mask, weighting_scheme,
                    scale_factor, self.one_h_linearisation,
                    self.reparametrisation.jacobian_transpose_matching_grad_fc(
                    ), extinction_correction, objective_only,
                    self.may_parallelise)

            if not self.finalised:  #i.e. never been called
                self.reparametrisation.linearise()
                self.reparametrisation.store()
                scale_factor = self.initial_scale_factor
                if scale_factor is None:  # we haven't got one from previous refinement
                    result = build_normal_eqns(
                        scale_factor=None,
                        weighting_scheme=sigma_weighting(),
                        objective_only=True)
                    scale_factor = self.scale_factor()
            else:  # use scale factor from the previous step
                scale_factor = self.scale_factor()

            self.reset()
            result = build_normal_eqns(scale_factor, self.weighting_scheme,
                                       objective_only)
            self.f_calc = self.observations.fo_sq.array(data=result.f_calc(),
                                                        sigmas=None)
            self.fc_sq = self.observations.fo_sq.array(
              data=result.observables(), sigmas=None)\
                .set_observation_type_xray_intensity()
            self.weights = result.weights()
            self.objective_data_only = self.objective()
            self.chi_sq_data_only = self.chi_sq()
            if self.restraints_manager is not None:
                # Here we determine a normalisation factor to place the restraints on the
                # same scale as the average residual. This is the normalisation
                # factor suggested in Giacovazzo and similar to that used by shelxl.
                # (shelx manual, page 5-1).
                # The factor 2 comes from the fact that we minimize 1/2 sum w delta^2
                if self.restraints_normalisation_factor is None:
                    self.restraints_normalisation_factor \
                        = 2 * self.objective_data_only/(self.n_equations-self.n_parameters)
                linearised_eqns = self.restraints_manager.build_linearised_eqns(
                    self.xray_structure,
                    self.reparametrisation.parameter_map())
                jacobian = \
                  self.reparametrisation.jacobian_transpose_matching(
                    self.reparametrisation.mapping_to_grad_fc_all).transpose()
                self.reduced_problem().add_equations(
                    linearised_eqns.deltas,
                    linearised_eqns.design_matrix * jacobian,
                    linearised_eqns.weights *
                    self.restraints_normalisation_factor,
                    optimise_for_tall_matrix=False)
                self.n_restraints = linearised_eqns.n_restraints()
                self.chi_sq_data_and_restraints = self.chi_sq()
            if not objective_only:
                self.origin_fixing_restraint.add_to(
                    self.step_equations(),
                    self.reparametrisation.jacobian_transpose_matching_grad_fc(
                    ), self.reparametrisation.asu_scatterer_parameters)
Example #4
0
  def build_up(self, objective_only=False):
    if self.f_mask is not None:
      f_mask = self.f_mask.data()
    else:
      f_mask = flex.complex_double()

    extinction_correction = self.reparametrisation.extinction
    if extinction_correction is None:
      extinction_correction = xray.dummy_extinction_correction()

    def args(scale_factor, weighting_scheme, objective_only):
      args = (self,
              self.observations,
              f_mask,
              weighting_scheme,
              scale_factor,
              self.one_h_linearisation,
              self.reparametrisation.jacobian_transpose_matching_grad_fc(),
              extinction_correction
              )
      if objective_only:
        args += (True,)
      return args

    if not self.finalised: #i.e. never been called
      self.reparametrisation.linearise()
      self.reparametrisation.store()
      scale_factor = self.initial_scale_factor
      if scale_factor is None: # we haven't got one from previous refinement
        result = ext.build_normal_equations(
          *args(scale_factor=None, weighting_scheme=sigma_weighting(),
                objective_only=True))
        scale_factor = self.scale_factor()
    else: # use scale factor from the previous step
      scale_factor = self.scale_factor()

    self.reset()
    result = ext.build_normal_equations(*args(scale_factor,
                                              self.weighting_scheme,
                                              objective_only))
    self.f_calc = self.observations.fo_sq.array(
      data=result.f_calc(), sigmas=None)
    self.fc_sq = self.observations.fo_sq.array(
      data=result.observables(), sigmas=None)\
        .set_observation_type_xray_intensity()
    self.weights = result.weights()
    self.objective_data_only = self.objective()
    self.chi_sq_data_only = self.chi_sq()
    if self.restraints_manager is not None:
      # Here we determine a normalisation factor to place the restraints on the
      # same scale as the average residual. This is the normalisation
      # factor suggested in Giacovazzo and similar to that used by shelxl.
      # (shelx manual, page 5-1).
      # The factor 2 comes from the fact that we minimize 1/2 sum w delta^2
      if self.restraints_normalisation_factor is None:
        self.restraints_normalisation_factor \
            = 2 * self.objective_data_only/(self.n_equations-self.n_parameters)
      linearised_eqns = self.restraints_manager.build_linearised_eqns(
        self.xray_structure, self.reparametrisation.parameter_map())
      jacobian = \
        self.reparametrisation.jacobian_transpose_matching(
          self.reparametrisation.mapping_to_grad_fc_all).transpose()
      self.reduced_problem().add_equations(
        linearised_eqns.deltas,
        linearised_eqns.design_matrix * jacobian,
        linearised_eqns.weights * self.restraints_normalisation_factor)
      self.n_restraints = linearised_eqns.n_restraints()
      self.chi_sq_data_and_restraints = self.chi_sq()
    if not objective_only:
      self.origin_fixing_restraint.add_to(
        self.step_equations(),
        self.reparametrisation.jacobian_transpose_matching_grad_fc(),
        self.reparametrisation.asu_scatterer_parameters)