def test_save_and_load_Astropy_model(self): save_model(self.model, 'bubu_model.p') b, kind, _ = load_model('bubu_model.p') assert kind == 'Astropy' assert isinstance(b, Model) assert np.all(self.model.parameters == b.parameters) assert np.all(self.model.bounds == b.bounds) assert np.all(self.model.fixed == b.fixed)
def test_save_and_load_callable_model(self): constraints0 = {'bounds': ()} save_model(_dummy, 'bubu_callable.p', constraints=constraints0) b, kind, constraints = load_model('bubu_callable.p') assert kind == 'callable' assert callable(b) assert np.all(_dummy.__code__.co_argcount == b.__code__.co_argcount) assert np.all(_dummy.__defaults__ == b.__defaults__) assert np.all(constraints == constraints0)
def test_load_python_model_Astropy(self): modelstring = ''' from astropy.modeling import models model = models.Const1D() ''' print(modelstring, file=open('bubu__model__2__.py', 'w')) b, kind, constraints = load_model('bubu__model__2__.py') assert isinstance(b, Model) assert kind == 'Astropy' assert b.amplitude == 1
def test_load_python_model_callable(self): modelstring = ''' def model(x, a=2, b=4): return x * a + b constraints = {'fixed': {'a': True}} ''' print(modelstring, file=open('bubu__model__.py', 'w')) b, kind, constraints = load_model('bubu__model__.py') assert kind == 'callable' assert callable(b) assert b.__code__.co_argcount == 3 assert b.__defaults__[0] == 2 assert b.__defaults__[1] == 4 assert np.all(constraints == {'fixed': {'a': True}})
def test_load_model_input_invalid_file_format(self): with open('bubu.txt', 'w') as fobj: print(1, file=fobj) with pytest.raises(TypeError) as record: b, kind, _ = load_model('bubu.txt') assert 'Unknown file type' in str(record.value)
def test_load_model_input_file_doesnt_exist(self): with pytest.raises(FileNotFoundError) as record: b, kind, _ = load_model('dfasjkdaslfj') assert 'Model file not found' in str(record.value)
def test_load_model_input_not_string(self): '''Input is not a string''' with pytest.raises(TypeError) as record: b, kind, _ = load_model(1) assert 'modelstring has to be an existing file name' \ in str(record.value)