class TestParameterConstraintInIndexedFit(unittest.TestCase): def _expected_profile_diff(self, res, cov_mat_inv): return res.dot(cov_mat_inv).dot(res) def _test_consistency(self, constrained_fit, par_cov_mat_inv): constrained_fit.do_fit() _cost_function = constrained_fit._fitter._fcn_wrapper for _i in range(4): for _j in range(9): _profile_constrained = _cost_function(self._test_par_values[_i, 0, _j], self._test_par_values[_i, 1, _j]) _diff = _profile_constrained - self._profile_no_constraints[_i, _j] _expected_profile_diff = self._expected_profile_diff(self._test_par_res[_i, _j], par_cov_mat_inv) self.assertTrue(np.abs(_diff - _expected_profile_diff) < 1e-12) @staticmethod def _model(a, b): return a * np.arange(5) + b def setUp(self): _data = [-2.1, 0.2, 1.9, 3.8, 6.1] self._means = np.array([3.654, 7.789]) self._vars = np.array([2.467, 1.543]) self._cov_mat_uncor = np.array([[self._vars[0], 0.0], [0.0, self._vars[1]]]) self._cov_mat_uncor_inv = np.linalg.inv(self._cov_mat_uncor) self._cov_mat_cor = np.array([[self._vars[0], 0.1], [0.1, self._vars[1]]]) self._cov_mat_cor_inv = np.linalg.inv(self._cov_mat_cor) self._cov_mat_simple_a_inv = np.array([[1.0 / self._vars[0], 0.0], [0.0, 0.0]]) self._cov_mat_simple_b_inv = np.array([[0.0, 0.0], [0.0, 1.0 / self._vars[1]]]) self._data_container = IndexedContainer(data=_data) self._data_container.add_error(err_val=1.0) _a_test = np.linspace(start=0, stop=4, num=9, endpoint=True) _b_test = np.linspace(start=-4, stop=0, num=9, endpoint=True) self._test_par_values = np.zeros((4, 2, 9)) self._test_par_values[0, 0] = _a_test self._test_par_values[1, 1] = _b_test self._test_par_values[2, 0] = _a_test self._test_par_values[2, 1] = _b_test self._test_par_values[3, 0] = _a_test self._test_par_values[3, 1] = -_b_test self._test_par_res = self._test_par_values - self._means.reshape((1, 2, 1)) self._test_par_res = np.transpose(self._test_par_res, axes=(0, 2, 1)) self._fit_no_constraints = IndexedFit(self._data_container, model_function=self._model) self._fit_no_constraints.do_fit() _cost_function = self._fit_no_constraints._fitter._fcn_wrapper self._profile_no_constraints = np.zeros((4, 9)) for _i in range(4): for _j in range(9): self._profile_no_constraints[_i, _j] = _cost_function( self._test_par_values[_i, 0, _j], self._test_par_values[_i, 1, _j]) def test_bad_input_exception(self): _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model) with self.assertRaises(IndexedFitException): _fit_with_constraint.add_parameter_constraint('c', 1.0, 1.0) with self.assertRaises(IndexedFitException): _fit_with_constraint.add_matrix_parameter_constraint(['a', 'c'], [1.0, 2.0], [[0.2, 0.0], [0.0, 0.1]]) with self.assertRaises(IndexedFitException): _fit_with_constraint.add_matrix_parameter_constraint(['a'], [1.0, 2.0], [[0.2, 0.0], [0.0, 0.1]]) def test_fit_profile_cov_mat_uncorrelated(self): _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model) _fit_with_constraint.add_matrix_parameter_constraint(['a', 'b'], self._means, self._cov_mat_uncor) self._test_consistency(_fit_with_constraint, self._cov_mat_uncor_inv) _fit_with_constraint_alt = IndexedFit(self._data_container, model_function=self._model) _fit_with_constraint_alt.add_parameter_constraint('a', self._means[0], np.sqrt(self._vars[0])) _fit_with_constraint_alt.add_parameter_constraint('b', self._means[1], np.sqrt(self._vars[1])) self._test_consistency(_fit_with_constraint_alt, self._cov_mat_uncor_inv) def test_fit_profile_cov_mat_correlated(self): _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model) _fit_with_constraint.add_matrix_parameter_constraint(['a', 'b'], self._means, self._cov_mat_cor) self._test_consistency(_fit_with_constraint, self._cov_mat_cor_inv) def test_fit_profile_simple_a(self): _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model) _fit_with_constraint.add_parameter_constraint('a', self._means[0], np.sqrt(self._vars[0])) self._test_consistency(_fit_with_constraint, self._cov_mat_simple_a_inv) def test_fit_profile_simple_b(self): _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model) _fit_with_constraint.add_parameter_constraint('b', self._means[1], np.sqrt(self._vars[1])) self._test_consistency(_fit_with_constraint, self._cov_mat_simple_b_inv)
class TestDatastoreIndexed(unittest.TestCase): def setUp(self): self._ref_data = [3.3, 5.5, 2.2, 8.5, 10.2] self.idx_cont = IndexedContainer(data=self._ref_data) self._ref_err_abs_singlevalue = 1.2 self._ref_err_abs_valuearray = [1.2, 1.1, 1.2, 1.2, 1.1] self._ref_err_corr_coeff = 0.23 self.idx_cont.add_error(self._ref_err_abs_valuearray, name='MyError', correlation=self._ref_err_corr_coeff, relative=False) self._ref_cov_mat = cov_mat_from_float_list( self._ref_err_abs_valuearray, correlation=self._ref_err_corr_coeff).mat def test_get_matching_error_all_empty_dict(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=dict()) self.assertEqual(len(_errs), 1) self.assertIs(self.idx_cont._error_dicts['MyError']['err'], _errs['MyError']) def test_get_matching_error_all_None(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=None) self.assertEqual(len(_errs), 1) self.assertIs(self.idx_cont._error_dicts['MyError']['err'], _errs['MyError']) def test_get_matching_error_name(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=dict( name='MyError')) self.assertEqual(len(_errs), 1) self.assertIs(self.idx_cont._error_dicts['MyError']['err'], _errs['MyError']) def test_get_matching_error_type_simple(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=dict( type='simple')) self.assertEqual(len(_errs), 1) self.assertIs(self.idx_cont._error_dicts['MyError']['err'], _errs['MyError']) def test_get_matching_error_type_matrix(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=dict( type='matrix')) self.assertEqual(len(_errs), 0) def test_get_matching_error_uncorrelated(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=dict( correlated=False)) self.assertEqual(len(_errs), 0) def test_get_matching_error_correlated(self): _errs = self.idx_cont.get_matching_errors(matching_criteria=dict( correlated=True)) self.assertEqual(len(_errs), 1) self.assertIs(self.idx_cont._error_dicts['MyError']['err'], _errs['MyError']) def test_compare_error_reference(self): for _err_dict in self.idx_cont._error_dicts.values(): _err_ref_vals = _err_dict['err'].reference self.assertTrue(np.all(_err_ref_vals == self._ref_data)) def test_compare_ref_data(self): self.assertTrue(np.all(self.idx_cont.data == self._ref_data)) def test_compare_ref_err(self): self.assertTrue( np.allclose(self.idx_cont.err, self._ref_err_abs_valuearray, atol=1e-10)) def test_compare_ref_total_cov_mat(self): _err = self.idx_cont.get_total_error() _mat = _err.cov_mat self.assertTrue(np.allclose(_mat, self._ref_cov_mat, atol=1e-5)) def test_compare_ref_total_err_for_add_err_twice(self): self.idx_cont.add_error(self._ref_err_abs_valuearray, name='MyNewError', correlation=self._ref_err_corr_coeff, relative=False) _err = self.idx_cont.get_total_error() _mat = _err.cov_mat self.assertTrue( np.allclose(_mat, self._ref_cov_mat + self._ref_cov_mat, atol=1e-5)) def test_compare_ref_total_err_for_add_disable_new_err(self): self.idx_cont.add_error(self._ref_err_abs_valuearray, name='MyNewError', correlation=self._ref_err_corr_coeff, relative=False) self.idx_cont.disable_error("MyNewError") _err = self.idx_cont.get_total_error() _mat = _err.cov_mat self.assertTrue(np.allclose(_mat, self._ref_cov_mat, atol=1e-5)) def test_compare_ref_total_err_for_add_disable_reenable_new_err(self): self.idx_cont.add_error(self._ref_err_abs_valuearray, name='MyNewError', correlation=self._ref_err_corr_coeff, relative=False) self.idx_cont.disable_error("MyNewError") _err = self.idx_cont.get_total_error() self.idx_cont.enable_error("MyNewError") _err = self.idx_cont.get_total_error() _mat = _err.cov_mat self.assertTrue( np.allclose(_mat, self._ref_cov_mat + self._ref_cov_mat, atol=1e-5)) def test_raise_add_same_error_name_twice(self): self.idx_cont.add_error(0.1, name="MyNewError", correlation=0, relative=False) with self.assertRaises(DataContainerException): self.idx_cont.add_error(0.1, name="MyNewError", correlation=0, relative=False) def test_raise_get_inexistent_error(self): with self.assertRaises(DataContainerException): self.idx_cont.get_error("MyInexistentError")
class TestIndexedContainerYamlRepresentation(unittest.TestCase): def setUp(self): _data = [4, 5, 6, 2, 4, 5., 3.01e-5, 4.4e+45] _ndat = len(_data) self._container = IndexedContainer(data=_data) self._container.add_error(err_val=0.1, name="SUA", correlation=0.0, relative=False) self._container.add_error(err_val=0.1, name="SUR", correlation=0.0, relative=True) self._container.add_error(err_val=0.1, name="SCA", correlation=1.0, relative=False) self._container.add_matrix_error(err_matrix=np.eye(_ndat) * 0.1, name="MCov", relative=False, matrix_type='covariance') self._container.add_matrix_error(err_matrix=np.eye(_ndat), name="MCor", relative=False, matrix_type='correlation', err_val=0.1) # # xy # self._container.add_simple_error(axis='x', err_val=0.1, correlation=0.0, relative=False) # self._container.add_simple_error(axis='y', err_val=0.2, correlation=0.0, relative=False) # self._container.add_matrix_error(axis='y', err_matrix=np.eye(8) * 0.1, relative=False, matrix_type='covariance') # # hist # self._container.add_simple_error(err_val=2, correlation=0.5, relative=False) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_DATASET_INDEXED)) self._roundtrip_streamreader = DataContainerYamlReader( self._roundtrip_stringstream) self._roundtrip_streamwriter = DataContainerYamlWriter( self._container, self._roundtrip_stringstream) self._testfile_streamreader = DataContainerYamlReader( self._testfile_stringstream) self._ref_testfile_data = [80.429, 80.339] self._ref_testfile_err = [0.89442719, 0.89442719] self._ref_testfile_cov_mat = np.array([[0.8, 0.3], [0.3, 0.8]]) self._ref_testfile_error_names = { 'ErrorOne', 'ErrorTwo', 'ErrorThree', 'ErrorFour' } self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_DATASET_INDEXED_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_DATASET_INDEXED_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = DataContainerYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = DataContainerYamlReader( self._testfile_stringstream_extra_keyword) def test_write_to_roundtrip_stringstream(self): self._roundtrip_streamwriter.write() def test_read_from_testfile_stream(self): _read_container = self._testfile_streamreader.read() self.assertTrue(isinstance(_read_container, IndexedContainer)) self.assertTrue( np.allclose(_read_container.data, self._ref_testfile_data)) self.assertTrue( np.allclose(_read_container.err, self._ref_testfile_err)) self.assertTrue( np.allclose( _read_container.cov_mat, self._ref_testfile_cov_mat, )) # check that the error names are the same self.assertEqual(set(_read_container._error_dicts.keys()), self._ref_testfile_error_names) def test_read_from_testfile_stream_missing_keyword(self): with self.assertRaises(YamlReaderException): self._testfile_streamreader_missing_keyword.read() def test_read_from_testfile_stream_extra_keyword(self): with self.assertRaises(YamlReaderException): self._testfile_streamreader_extra_keyword.read() def test_round_trip_with_stringstream(self): self._roundtrip_streamwriter.write() self._roundtrip_stringstream.seek(0) # return to beginning _read_container = self._roundtrip_streamreader.read() self.assertTrue(isinstance(_read_container, IndexedContainer)) # compare data members self.assertTrue(np.allclose(self._container.data, _read_container.data)) # compare (total) errors and cov mats # TODO: compare individual error components -> nontrivial because error ids change... self.assertTrue(np.allclose(self._container.err, _read_container.err)) self.assertTrue( np.allclose(self._container.cov_mat, _read_container.cov_mat)) # check that the error names are the same self.assertEqual(set(self._container._error_dicts.keys()), set(_read_container._error_dicts.keys()))