def test_baseobj_get_parameters(setup_pars: dict): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) pars = obj.get_fit_parameters() assert isinstance(pars, list) assert len(pars) == 2 par_names = [par.name for par in pars] assert "p2" in par_names assert "p3" in par_names
def test_baseobj__add_component(setup_pars): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) p = Parameter("added_par", 1) new_item_name = "Added" obj._add_component(new_item_name, p) assert hasattr(obj, new_item_name) a = getattr(obj, new_item_name) assert isinstance(a, Parameter)
def test_baseobj_set(setup_pars: dict): from copy import deepcopy name = setup_pars["name"] explicit_name1 = "par1" kwargs = { setup_pars[explicit_name1].name: setup_pars[explicit_name1], } obj = BaseObj(name, **kwargs) new_value = 5.0 with not_raises([AttributeError, ValueError]): obj.p1 = new_value assert obj.p1.raw_value == new_value
def test_baseobj_dir(setup_pars): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) expected = [ "REDIRECT", "as_dict", "constraints", "des1", "des2", "from_dict", "generate_bindings", "get_fit_parameters", "get_parameters", "interface", "name", "par1", "par2", "par3", "switch_interface", "to_data_dict", "to_json", "unsafe_hash", "user_data", ] obtained = dir(obj) assert len(obtained) == len(expected) assert obtained == sorted(obtained) for this_item, this_expect in zip(obtained, expected): assert this_item == this_expect
def test_baseCollection_get_fit_parameters_nested(cls, setup_pars): name = setup_pars['name'] del setup_pars['name'] obj = BaseObj(name, **setup_pars) name2 = name + '_2' obj2 = cls(name2, obj=obj, **setup_pars) pars = obj2.get_fit_parameters() assert len(pars) == 4
def test_baseobj_get(setup_pars: dict): name = setup_pars["name"] explicit_name1 = "par1" explicit_name2 = "par2" kwargs = { setup_pars[explicit_name1].name: setup_pars[explicit_name1], setup_pars[explicit_name2].name: setup_pars[explicit_name2], } obj = BaseObj(name, **kwargs) with not_raises(AttributeError): p1: Parameter = obj.p1 with not_raises(AttributeError): p2: Parameter = obj.p2
def test_baseobj_create(setup_pars: dict, a: List[str], kw: List[str]): name = setup_pars["name"] args = [] for key in a: args.append(setup_pars[key]) kwargs = {} for key in kw: kwargs[key] = setup_pars[key] base = BaseObj(name, *args, **kwargs) assert base.name == name for key in a: item = getattr(base, setup_pars[key].name) assert isinstance(item, setup_pars[key].__class__)
def test_BaseObjUndoRedo(): objs = { obj.name: obj for obj in [createSingleObjs(idx) for idx in range(5)] } name = 'test' obj = BaseObj(name, **objs) name2 = 'best' # Test name # assert not doUndoRedo(obj, 'name', name2) # Test setting value for b_obj in objs.values(): e = doUndoRedo(obj, b_obj.name, b_obj.raw_value + 1, 'raw_value') if e: raise e
def test_baseobj_get_fit_parameters_nested(setup_pars): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) name2 = name + "_2" obj2 = BaseObj(name2, obj=obj, **setup_pars) pars = obj2.get_fit_parameters() assert len(pars) == 4 pars = obj.get_fit_parameters() assert len(pars) == 2
def test_baseCollection_from_baseObj(cls, setup_pars: dict, value: int): name = setup_pars['name'] del setup_pars['name'] objs = {} prefix = 'obj' for idx in range(value): objs[prefix + str(idx)] = BaseObj(prefix + str(idx), **setup_pars) coll = cls(name, **objs) assert coll.name == name assert len(coll) == value assert coll.user_data == {} idx = 0 for item, key in zip(coll, objs.keys()): assert item.name == prefix + str(idx) assert isinstance(item, objs[key].__class__) idx += 1
__author__ = 'github.com/wardsimon' __version__ = '0.1.0' from easyCore import np from easyCore.Datasets.xarray import xr from easyCore.Objects.ObjectClasses import Parameter, BaseObj from easyCore.Fitting.Fitting import Fitter import matplotlib.pyplot as plt d = xr.Dataset() b = BaseObj('line', m=Parameter('m', 1), c=Parameter('c', 1)) def fit_fun(x, *args, **kwargs): # In the real case we would gust call the evaluation fn without reference to the BaseObj return b.c.raw_value + b.m.raw_value * x f = Fitter() f.initialize(b, fit_fun) nx = 1E3 x_min = 0 x_max = 100 x = np.linspace(x_min, x_max, num=int(nx)) y1 = 2 * x - 1 + 5 * (np.random.random(size=x.shape) - 0.5) x2 = x + 20 y2 = 2 * x2 - 1 + 5 * (np.random.random(size=x2.shape) - 0.5)
def test_baseObj_name(setup_pars): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) assert obj.name == name
def test_baseobj_get_fit_parameters(setup_pars): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) pars = obj.get_fit_parameters() assert len(pars) == 2
def test_baseobj_as_dict(setup_pars: dict): name = setup_pars["name"] del setup_pars["name"] obj = BaseObj(name, **setup_pars) obtained = obj.as_dict() assert isinstance(obtained, dict) expected = { "@module": "easyCore.Objects.ObjectClasses", "@class": "BaseObj", "@version": easyCore.__version__, "name": "test", "par1": { "@module": "easyCore.Objects.Variable", "@class": "Parameter", "@version": easyCore.__version__, "name": "p1", "value": 0.1, "error": 0.0, "min": -np.inf, "max": np.inf, "fixed": True, "units": "dimensionless", }, "des1": { "@module": "easyCore.Objects.Variable", "@class": "Descriptor", "@version": easyCore.__version__, "name": "d1", "value": 0.1, "units": "dimensionless", "description": "", "url": "", "display_name": "d1", }, "par2": { "@module": "easyCore.Objects.Variable", "@class": "Parameter", "@version": easyCore.__version__, "name": "p2", "value": 0.1, "error": 0.0, "min": -np.inf, "max": np.inf, "fixed": False, "units": "dimensionless", }, "des2": { "@module": "easyCore.Objects.Variable", "@class": "Descriptor", "@version": easyCore.__version__, "name": "d2", "value": 0.1, "units": "dimensionless", "description": "", "url": "", "display_name": "d2", }, "par3": { "@module": "easyCore.Objects.Variable", "@class": "Parameter", "@version": easyCore.__version__, "name": "p3", "value": 0.1, "error": 0.0, "min": -np.inf, "max": np.inf, "fixed": False, "units": "dimensionless", }, } def check_dict(check, item): if isinstance(check, dict) and isinstance(item, dict): if "@module" in item.keys(): with not_raises([ValueError, AttributeError]): this_obj = MontyDecoder().process_decoded(item) for this_check_key, this_item_key in zip(check.keys(), item.keys()): check_dict(check[this_check_key], item[this_item_key]) else: assert isinstance(item, type(check)) assert item == check check_dict(expected, obtained)
__version__ = '0.1.0' from easyCore import np from easyCore.Datasets.xarray import xr import matplotlib.pyplot as plt from easyCore.Objects.ObjectClasses import Parameter, BaseObj from easyCore.Fitting.Fitting import Fitter d = xr.Dataset() m_starting_point = 1 c_starting_point = 1 b = BaseObj('line', m=Parameter('m', m_starting_point), c=Parameter('c', c_starting_point)) def fit_fun(x, *args, **kwargs): # In the real case we would gust call the evaluation fn without reference to the BaseObj return b.c.raw_value + b.m.raw_value * x nx = 1E3 x_min = 0 x_max = 100 x = np.linspace(x_min, x_max, num=int(nx)) y = 2*x - 1 + 5*(np.random.random(size=x.shape) - 0.5)
x = np.linspace(x_min, x_max, num=int(nx)) d.easyCore.add_coordinate('x', x) d.easyCore.add_coordinate('y', x + 1) d.easyCore.add_variable( 'z', ['x', 'y'], np.sin(2 * np.pi * (x + sin_offest)).reshape( (-1, 1)) * np.cos(2 * np.pi * (x + cos_offset)).reshape( (1, -1)) + 2 * (0.5 - np.random.random(size=(int(nx), int(nx))))) s_off_start_point = 0 c_off_start_point = 0 b = BaseObj('line', s_off=Parameter('s_off', s_off_start_point), c_off=Parameter('c_off', c_off_start_point)) def fit_fun(x, *args, **kwargs): # In the real case we would gust call the evaluation fn without reference to the BaseObj return np.sin(2 * np.pi * (x[:, 0] + b.s_off.raw_value)) * np.cos( 2 * np.pi * (x[:, 1] + b.c_off.raw_value)) f = Fitter() f.initialize(b, fit_fun) print('Performing fit - No dask') t = time.time() f_res = d['z'].easyCore.fit(f)