def test_unrestrained_ScalingRestraints( mock_unrestrained_apm, mock_multi_unrestrained_apm ): """Test the case of unrestrained components. None should be returned in each case.""" assert ( SingleScalingRestraintsCalculator.calculate_restraints(mock_unrestrained_apm) is None ) assert ( SingleScalingRestraintsCalculator.calculate_jacobian_restraints( mock_unrestrained_apm ) is None ) assert ( ScalingRestraintsCalculator.calculate_restraints(mock_multi_unrestrained_apm) is None ) assert ( ScalingRestraintsCalculator.calculate_jacobian_restraints( mock_multi_unrestrained_apm ) is None )
def compute_restraints_residuals_and_gradients(self, apm): """Return the restraints for the residuals and jacobian.""" if self.param_restraints: restr = ScalingRestraintsCalculator.calculate_jacobian_restraints(apm) if not restr: self.param_restraints = False return restr return None
def compute_restraints_functional_gradients(self, apm): """Return the restrains for functional and gradients.""" restraints = None if self.param_restraints: restr = ScalingRestraintsCalculator.calculate_restraints(apm) if restr: resid_restr = flex.sum(restr[0]) # add to total functional here grad_restr = restr[1] restraints = [resid_restr, grad_restr] else: self.param_restraints = False return restraints # list of restraints to add to resid, grads and curvs
def test_MultiScalingRestraints( mock_multi_apm, mock_restrained_component, mock_unrestrained_component ): """Test for the multi-dataset scaling restraints manager.""" # Test the call to calculate restraints. Expected return is the individual # dataset vectors joined together. restraints = ScalingRestraintsCalculator.calculate_restraints(mock_multi_apm) abs_restraints = mock_restrained_component.calculate_restraints() assert list(restraints[0]) == (list(abs_restraints[0]) + list(abs_restraints[0])) assert list(restraints[1]) == ( list(abs_restraints[1]) + [0.0] * mock_unrestrained_component.n_params + list(abs_restraints[1]) + [0.0] * mock_unrestrained_component.n_params ) # Test the call to calculate jacobian restraints. Again, the expected return # is the individual dataset vectors joined together. jacobian_restraints = ScalingRestraintsCalculator.calculate_jacobian_restraints( mock_multi_apm ) abs_restraints = mock_restrained_component.calculate_jacobian_restraints() assert list(jacobian_restraints[0]) == ( list(abs_restraints[0]) + list(abs_restraints[0]) ) n_abs_params = mock_restrained_component.n_params n_total_params = mock_multi_apm.apm_data[0]["end_idx"] assert jacobian_restraints[1].n_cols == mock_multi_apm.n_active_params assert jacobian_restraints[1].n_rows == n_abs_params * 2 # Check that both restraints jacobians were set in correct location. for i in range(mock_restrained_component.n_params): for j in range(mock_restrained_component.n_params): assert jacobian_restraints[1][i, j] == abs_restraints[1][i, j] assert jacobian_restraints[1][i + n_abs_params, j + n_total_params] == ( abs_restraints[1][i, j] ) assert abs_restraints[1].non_zeroes * 2 == jacobian_restraints[1].non_zeroes
def rmsds(self, Ih_table, apm): """Calculate RMSDs for the matches. Also calculate R-factors.""" R = flex.double([]) n = 0 for block in Ih_table.blocked_data_list: R.extend(flex.pow2(self.calculate_residuals(block)) * block.weights) n += block.size if self.param_restraints: restraints = ScalingRestraintsCalculator.calculate_restraints(apm) if restraints: R.extend(restraints[0]) else: self.param_restraints = False self._rmsds = [(flex.sum(R) / n) ** 0.5] return self._rmsds
def rmsds(self, Ih_table, apm): """Calculate RMSDs for the matches. Also calculate R-factors.""" R = 0 n = 0 for block in Ih_table.blocked_data_list: R += np.sum( np.square(self.calculate_residuals(block)) * block.weights) n += block.size if self.param_restraints: restraints = ScalingRestraintsCalculator.calculate_restraints(apm) if restraints: R += np.sum(restraints[0]) else: self.param_restraints = False self._rmsds = [(R / n)**0.5] return self._rmsds