def setUp(self): self._test_x = np.arange(10) self._test_a = 2.0 self._test_b = -1.0 self._test_y = self.linear_model(self._test_x, self._test_a, self._test_b) self._model_function = HistModelFunction(self.linear_model) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_HIST)) self._testfile_stringstream_with_formatter = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_HIST_WITH_FORMATTER)) self._roundtrip_streamreader = ModelFunctionYamlReader( self._roundtrip_stringstream) self._roundtrip_streamwriter = ModelFunctionYamlWriter( self._model_function, self._roundtrip_stringstream) self._testfile_streamreader = ModelFunctionYamlReader( self._testfile_stringstream) self._testfile_streamreader_with_formatter = ModelFunctionYamlReader( self._testfile_stringstream_with_formatter) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_HIST_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_HIST_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = ModelFunctionYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = ModelFunctionYamlReader( self._testfile_stringstream_extra_keyword)
def setUp(self): _data = [1, 1, 4, 1, 1, 4, 2, 5, 5, 5, 2, 4, 2, 2, 18, 2, 8, 8, 9] _nbins = 3 self._container = HistContainer(n_bins=_nbins, bin_range=(0.0, 10.), fill_data=_data) self._container.add_error(err_val=0.1, name='SC5A_1', correlation=0.5, relative=False) self._container.add_error( err_val=[i / 10. for i in range(1, _nbins + 1)], name='SC5A_2', correlation=0.5, relative=False) self._container.add_matrix_error(err_matrix=np.eye(_nbins) * 0.1, name='MCov', relative=False, matrix_type='covariance') self._container.add_matrix_error(err_matrix=np.eye(_nbins), name='MCor', relative=False, matrix_type='correlation', err_val=0.03) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_DATASET_HIST)) self._roundtrip_streamreader = DataContainerYamlReader( self._roundtrip_stringstream) self._roundtrip_streamwriter = DataContainerYamlWriter( self._container, self._roundtrip_stringstream) self._testfile_streamreader = DataContainerYamlReader( self._testfile_stringstream) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_DATASET_HIST_MSSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_DATASET_HIST_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = DataContainerYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = DataContainerYamlReader( self._testfile_stringstream_extra_keyword) self._ref_testfile_n_bins = 2 self._ref_testfile_bin_range = [1.0, 3.0] self._ref_testfile_bin_edges = [1.0, 2.0, 3.0] self._ref_testfile_data = [1, 1] self._ref_testfile_err = [0.89442719, 1] self._ref_testfile_cov_mat = np.array([[0.8, 0.3], [0.3, 1.0]]) self._ref_testfile_error_names = { 'ErrorOne', 'ErrorTwo', 'ErrorThree', 'ErrorFour' }
def setup_streams(self): self._testfile_stringstream = IOStreamHandle(StringIO(self.TEST_FIT)) self._testfile_stringstream_simple = IOStreamHandle(StringIO(self.TEST_FIT_SIMPLE)) self._testfile_streamreader = FitYamlReader(self._testfile_stringstream) self._testfile_streamreader_simple = FitYamlReader(self._testfile_stringstream_simple) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._roundtrip_streamreader = FitYamlReader(self._roundtrip_stringstream) self._roundtrip_streamwriter = FitYamlWriter(self._fit, self._roundtrip_stringstream) self._testfile_stringstream_missing_keyword = IOStreamHandle(StringIO(self.TEST_FIT_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle(StringIO(self.TEST_FIT_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = FitYamlReader(self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = FitYamlReader(self._testfile_stringstream_extra_keyword)
def _get_model_from_roundtrip(model): _roundtrip_stringstream = IOStreamHandle(StringIO()) _roundtrip_streamreader = ParametricModelYamlReader( _roundtrip_stringstream) _roundtrip_streamwriter = ParametricModelYamlWriter( model, _roundtrip_stringstream) _roundtrip_streamwriter.write() _roundtrip_stringstream.seek(0) # return to beginning return _roundtrip_streamreader.read()
def setUp(self): self._model_function_formatter = IndexedModelFunctionFormatter( name='quadratic_model', latex_name='quadratic model', index_name='x', latex_index_name='X', arg_formatters=[ ParameterFormatter(arg_name='a', value=1.1, error=0.1, latex_name='A'), ParameterFormatter(arg_name='b', value=2.2, error=0.1, latex_name='B'), ParameterFormatter(arg_name='c', value=3.3, error=0.1, latex_name='C') ], expression_string='{0} * {x} ** 2 + {1} * {x} + {2}', latex_expression_string='{0}{x}^2 + {1}{x} + {2}') self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_FORMATTER_INDEXED)) self._roundtrip_streamreader = ModelFunctionFormatterYamlReader( self._roundtrip_stringstream) self._roundtrip_streamwriter = ModelFunctionFormatterYamlWriter( self._model_function_formatter, self._roundtrip_stringstream) self._testfile_streamreader = ModelFunctionFormatterYamlReader( self._testfile_stringstream) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_FORMATTER_INDEXED_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_MODEL_FUNCTION_FORMATTER_INDEXED_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = ModelFunctionFormatterYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = ModelFunctionFormatterYamlReader( self._testfile_stringstream_extra_keyword)
def setUp(self): self._model_parameter_formatter = ParameterFormatter( 'phi', name='ph', value=1.571, error=0.1, latex_name=r"\phi") self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_MODEL_PARAMETER_FORMATTER)) self._roundtrip_streamreader = ParameterFormatterYamlReader( self._roundtrip_stringstream) self._roundtrip_streamwriter = ParameterFormatterYamlWriter( self._model_parameter_formatter, self._roundtrip_stringstream) self._testfile_streamreader = ParameterFormatterYamlReader( self._testfile_stringstream) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_MODEL_PARAMETER_FORMATTER_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_MODEL_PARAMETER_FORMATTER_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = ParameterFormatterYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = ParameterFormatterYamlReader( self._testfile_stringstream_extra_keyword)
def setUp(self): self._test_n_bins = 5 self._test_bin_range = (0, 5) self._test_bin_edges = np.arange(6) self._test_model_parameters = np.array([0.0, 0.08]) self._test_x = np.linspace(start=0.0, stop=5.0, num=11, endpoint=True) self._test_parametric_model = HistParametricModel( n_bins=self._test_n_bins, bin_range=self._test_bin_range, model_density_func=HistModelFunction( TestHistParametricModelYamlRepresenter.linear_model), model_parameters=self._test_model_parameters) self._test_parametric_model_with_errors = HistParametricModel( n_bins=self._test_n_bins, bin_range=self._test_bin_range, model_density_func=HistModelFunction( TestHistParametricModelYamlRepresenter.linear_model), model_parameters=self._test_model_parameters) self._test_parametric_model_with_errors.add_error(err_val=0.1, name='test_error', correlation=0, relative=False) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._roundtrip_stringstream_with_errors = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_HIST)) self._testfile_stringstream_with_errors = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_HIST_WITH_ERRORS)) self._roundtrip_streamreader = ParametricModelYamlReader( self._roundtrip_stringstream) self._roundtrip_streamreader_with_errors = ParametricModelYamlReader( self._roundtrip_stringstream_with_errors) self._roundtrip_streamwriter = ParametricModelYamlWriter( self._test_parametric_model, self._roundtrip_stringstream) self._roundtrip_streamwriter_with_errors = ParametricModelYamlWriter( self._test_parametric_model_with_errors, self._roundtrip_stringstream_with_errors) self._testfile_streamreader = ParametricModelYamlReader( self._testfile_stringstream) self._testfile_streamreader_with_errors = ParametricModelYamlReader( self._testfile_stringstream_with_errors) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_HIST_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_HIST_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = ParametricModelYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = ParametricModelYamlReader( self._testfile_stringstream_extra_keyword)
def setUp(self): self._test_x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) self._test_model_parameters = np.array([1.1, -1.5]) self._test_parametric_model = XYParametricModel( self._test_x, ModelFunctionBase( TestXYParametricModelYamlRepresenter.linear_model), self._test_model_parameters) self._test_parametric_model_with_errors = XYParametricModel( self._test_x, ModelFunctionBase( TestXYParametricModelYamlRepresenter.linear_model), self._test_model_parameters) self._test_parametric_model_with_errors.add_error(axis='x', err_val=0.1, name='test_x_error', correlation=0, relative=False) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._roundtrip_stringstream_with_errors = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_XY)) self._testfile_stringstream_with_errors = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_XY_WITH_ERRORS)) self._roundtrip_streamreader = ParametricModelYamlReader( self._roundtrip_stringstream) self._roundtrip_streamreader_with_errors = ParametricModelYamlReader( self._roundtrip_stringstream_with_errors) self._roundtrip_streamwriter = ParametricModelYamlWriter( self._test_parametric_model, self._roundtrip_stringstream) self._roundtrip_streamwriter_with_errors = ParametricModelYamlWriter( self._test_parametric_model_with_errors, self._roundtrip_stringstream_with_errors) self._testfile_streamreader = ParametricModelYamlReader( self._testfile_stringstream) self._testfile_streamreader_with_errors = ParametricModelYamlReader( self._testfile_stringstream_with_errors) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_XY_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_XY_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = ParametricModelYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = ParametricModelYamlReader( self._testfile_stringstream_extra_keyword)
def setUp(self): self._test_model_parameters = np.array([1.1, -1.5]) self._test_parametric_model = IndexedParametricModel( IndexedModelFunction( TestIndexedParametricModelYamlRepresenter.linear_model), self._test_model_parameters) self._test_parametric_model_with_errors = IndexedParametricModel( IndexedModelFunction( TestIndexedParametricModelYamlRepresenter.linear_model), self._test_model_parameters) self._test_parametric_model_with_errors.add_error(err_val=0.1, name='test_x_error', correlation=0, relative=False) self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._roundtrip_stringstream_with_errors = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_INDEXED)) self._testfile_stringstream_with_errors = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_INDEXED_WITH_ERRORS)) self._roundtrip_streamreader = ParametricModelYamlReader( self._roundtrip_stringstream) self._roundtrip_streamreader_with_errors = ParametricModelYamlReader( self._roundtrip_stringstream_with_errors) self._roundtrip_streamwriter = ParametricModelYamlWriter( self._test_parametric_model, self._roundtrip_stringstream) self._roundtrip_streamwriter_with_errors = ParametricModelYamlWriter( self._test_parametric_model_with_errors, self._roundtrip_stringstream_with_errors) self._testfile_streamreader = ParametricModelYamlReader( self._testfile_stringstream) self._testfile_streamreader_with_errors = ParametricModelYamlReader( self._testfile_stringstream_with_errors) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_INDEXED_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_PARAMETRIC_MODEL_INDEXED_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = ParametricModelYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = ParametricModelYamlReader( self._testfile_stringstream_extra_keyword)
def _get_model_from_string(input_string): _testfile_stringstream = IOStreamHandle(StringIO(input_string)) return ParametricModelYamlReader(_testfile_stringstream).read()
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 setUp(self): _data = [[0.0, .1, .3], [10, 24, 44]] _ndat = len(_data[0]) self._container = XYContainer(x_data=_data[0], y_data=_data[1]) self._container.add_error(axis='y', name="ySUA", err_val=0.1, correlation=0.0, relative=False) self._container.add_error(axis='y', name="ySUR", err_val=0.1, correlation=0.0, relative=True) self._container.add_error(axis='y', name="ySCA", err_val=0.1, correlation=1.0, relative=False) self._container.add_matrix_error(axis='y', name="yMCov", err_matrix=np.eye(_ndat) * 0.1, relative=False, matrix_type='covariance') self._container.add_matrix_error(axis='y', name="yMCor", err_matrix=np.eye(_ndat), relative=False, matrix_type='correlation', err_val=0.1) self._container.add_error(axis='x', name="xSUA", err_val=0.1, correlation=0.0, relative=False) self._container.add_error(axis='x', name="xSUR", err_val=0.1, correlation=0.0, relative=True) self._container.add_matrix_error(axis='x', name="xMCov", err_matrix=np.eye(_ndat) * 0.1, relative=False, matrix_type='covariance') # 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') self._roundtrip_stringstream = IOStreamHandle(StringIO()) self._testfile_stringstream = IOStreamHandle(StringIO(TEST_DATASET_XY)) self._roundtrip_streamreader = DataContainerYamlReader( self._roundtrip_stringstream) self._roundtrip_streamwriter = DataContainerYamlWriter( self._container, self._roundtrip_stringstream) self._testfile_streamreader = DataContainerYamlReader( self._testfile_stringstream) self._testfile_stringstream_missing_keyword = IOStreamHandle( StringIO(TEST_DATASET_XY_MISSING_KEYWORD)) self._testfile_stringstream_extra_keyword = IOStreamHandle( StringIO(TEST_DATASET_XY_EXTRA_KEYWORD)) self._testfile_streamreader_missing_keyword = DataContainerYamlReader( self._testfile_stringstream_missing_keyword) self._testfile_streamreader_extra_keyword = DataContainerYamlReader( self._testfile_stringstream_extra_keyword) self._ref_testfile_x_data = [5, 17] self._ref_testfile_y_data = [80.429, 80.339] self._ref_testfile_x_err = [0.70710678, 0.70710678] self._ref_testfile_y_err = [0.54772256, 0.54772256] self._ref_testfile_y_cov_mat = np.array([[0.3, 0.1], [0.1, 0.3]]) self._ref_testfile_x_cov_mat = np.array([[0.5, 0.2], [0.2, 0.5]]) self._ref_testfile_error_names = { 'XErrorOne', 'XErrorTwo', 'YErrorOne', 'YErrorTwo' }