def test_parameter_fixed(value):
    d = Parameter("test", -np.inf)
    if isinstance(value, bool):
        d.fixed = value
        assert d.fixed == value
    else:
        with pytest.raises(ValueError):
            d.fixed = value
def test_parameter_max(value):
    d = Parameter("test", 2147483649)
    if d.raw_value > value:
        with pytest.raises(ValueError):
            d.max = value
    else:
        d.max = value
        assert d.max == value
def test_parameter_error(value):
    d = Parameter("test", 1)
    if value >= 0:
        d.error = value
        assert d.error == value
    else:
        with pytest.raises(ValueError):
            d.error = value
def test_parameter_min(value):
    d = Parameter("test", -0.1)
    if d.raw_value < value:
        with pytest.raises(ValueError):
            d.min = value
    else:
        d.min = value
        assert d.min == value
예제 #5
0
def test_Line_constructor(m, c):
    m_ = Parameter("m", m)
    c_ = Parameter("c", c)
    line = Line(m_, c_)

    assert line.m.raw_value == m
    assert line.c.raw_value == c

    x = np.linspace(0, 10, 100)
    y = line.m.raw_value * x + line.c.raw_value
    assert np.allclose(line(x), y)
def test_parameter_bounds(value):
    for fixed in (True, False):
        p = Parameter("test", 1, enabled=value, fixed=fixed)
        assert p.min == -np.inf
        assert p.max == np.inf
        assert p.fixed == fixed
        assert p.bounds == (-np.inf, np.inf)

        p.bounds = (0, 2)
        assert p.min == 0
        assert p.max == 2
        assert p.bounds == (0, 2)
        assert p.enabled is True
        assert p.fixed is False
def test_parameter_repr():
    d = Parameter("test", 1)
    assert repr(
        d) == f"<{d.__class__.__name__} 'test': 1.0+/-0, bounds=[-inf:inf]>"
    d = Parameter("test", 1, units="cm")
    assert repr(
        d) == f"<{d.__class__.__name__} 'test': 1.0+/-0 cm, bounds=[-inf:inf]>"

    d = Parameter("test", 1, fixed=True)
    assert (
        repr(d) ==
        f"<{d.__class__.__name__} 'test': 1.0+/-0 (fixed), bounds=[-inf:inf]>")
    d = Parameter("test", 1, units="cm", fixed=True)
    assert (
        repr(d) ==
        f"<{d.__class__.__name__} 'test': 1.0+/-0 cm (fixed), bounds=[-inf:inf]>"
    )
예제 #8
0
def createSingleObjs(idx):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    reps = math.floor(idx / len(alphabet)) + 1
    name = alphabet[idx % len(alphabet)] * reps
    if idx % 2:
        return Parameter(name, idx)
    else:
        return Descriptor(name, idx)
예제 #9
0
def test_Parameter_Bounds_UndoRedo(value):
    from easyCore import borg
    borg.stack.enabled = True
    p = Parameter("test", 1, enabled=value)
    assert p.min == -np.inf
    assert p.max == np.inf
    assert p.bounds == (-np.inf, np.inf)

    p.bounds = (0, 2)
    assert p.min == 0
    assert p.max == 2
    assert p.bounds == (0, 2)
    assert p.enabled is True

    borg.stack.undo()
    assert p.min == -np.inf
    assert p.max == np.inf
    assert p.bounds == (-np.inf, np.inf)
    assert p.enabled is value
def test_parameter_advanced_creation(element, expected):
    if len(element[0]) > 0:
        value = element[0][1]
    else:
        value = element[1]["value"]
    if "min" in element[1].keys():
        if element[1]["min"] > value:
            with pytest.raises(ValueError):
                d = Parameter(*element[0], **element[1])
    elif "max" in element[1].keys():
        if element[1]["max"] < value:
            with pytest.raises(ValueError):
                d = Parameter(*element[0], **element[1])
    else:
        d = Parameter(*element[0], **element[1])
        for field in expected.keys():
            ref = expected[field]
            obtained = getattr(d, field)
            if isinstance(obtained, (ureg.Unit, Q_)):
                obtained = str(obtained)
            assert obtained == ref
def test_parameter_as_dict():
    d = Parameter("test", 1)
    result = d.as_dict()
    expected = {
        "@module": "easyCore.Objects.Variable",
        "@class": "Parameter",
        "@version": easyCore.__version__,
        "name": "test",
        "value": 1.0,
        "error": 0.0,
        "min": -np.inf,
        "max": np.inf,
        "fixed": False,
        "units": "dimensionless",
    }
    for key in expected.keys():
        if key == "callback":
            continue
        assert result[key] == expected[key]

    # Check that additional arguments work
    d = Parameter("test", 1, units="km", url="https://www.boo.com")
    result = d.as_dict()
    expected = {
        "@module": "easyCore.Objects.Variable",
        "@class": "Parameter",
        "@version": easyCore.__version__,
        "name": "test",
        "units": "kilometer",
        "value": 1.0,
        "error": 0.0,
        "min": -np.inf,
        "max": np.inf,
        "fixed": False,
        "url": "https://www.boo.com",
    }
    for key in expected.keys():
        if key == "callback":
            continue
        assert result[key] == expected[key]
예제 #12
0
def test_ItemHolder_fixed(fixed, value, precision, expected):
    p = Parameter("p", value, fixed=fixed)
    s = ItemHolder(p, decimal_places=precision)
    if not p.fixed:
        expected += "()"
    assert str(s) == expected
예제 #13
0
def test_ItemHolder_with_error(value, error, precision, expected):
    p = Parameter("p", value, error=error)
    s = ItemHolder(p, decimal_places=precision)
    assert str(s) == expected
예제 #14
0
def threePars(twoPars) -> Tuple[List[Parameter], List[int]]:
    ps, vs = twoPars
    ps.append(Parameter('c', 3))
    vs.append(3)
    return ps, vs
예제 #15
0
def twoPars() -> Tuple[List[Parameter], List[int]]:
    return [Parameter('a', 1), Parameter('b', 2)], [1, 2]
def test_parameter_display_name(value):
    p = Parameter("test", 1, display_name=value)
    assert p.display_name == value

    p = Descriptor("test", 1, display_name=value)
    assert p.display_name == value
예제 #17
0
 def default(cls):
     m = Parameter('m', m_value)
     c = Parameter('c', c_value)
     return cls(m=m, c=c)
예제 #18
0
 def from_pars(cls, m_value: float, c_value: float):
     m = Parameter('m', m_value)
     c = Parameter('c', c_value)
     return cls(m=m, c=c)
예제 #19
0
def test_BaseCollectionUndoRedo():
    objs = [createSingleObjs(idx) for idx in range(5)]
    name = 'test'
    obj = BaseCollection(name, *objs)
    name2 = 'best'

    # assert not doUndoRedo(obj, 'name', name2)

    from easyCore import borg
    borg.stack.enabled = True

    original_length = len(obj)
    p = Parameter('slip_in', 50)
    idx = 2
    obj.insert(idx, p)
    assert len(obj) == original_length + 1
    objs.insert(idx, p)
    for item, obj_r in zip(obj, objs):
        assert item == obj_r

    # Test inserting items
    borg.stack.undo()
    assert len(obj) == original_length
    _ = objs.pop(idx)
    for item, obj_r in zip(obj, objs):
        assert item == obj_r
    borg.stack.redo()
    assert len(obj) == original_length + 1
    objs.insert(idx, p)
    for item, obj_r in zip(obj, objs):
        assert item == obj_r

    # Test Del Items
    del obj[idx]
    del objs[idx]
    assert len(obj) == original_length
    for item, obj_r in zip(obj, objs):
        assert item == obj_r
    borg.stack.undo()
    assert len(obj) == original_length + 1
    objs.insert(idx, p)
    for item, obj_r in zip(obj, objs):
        assert item == obj_r
    del objs[idx]
    borg.stack.redo()
    assert len(obj) == original_length
    for item, obj_r in zip(obj, objs):
        assert item == obj_r

    # Test Place Item
    old_item = objs[idx]
    objs[idx] = p
    obj[idx] = p
    assert len(obj) == original_length
    for item, obj_r in zip(obj, objs):
        assert item == obj_r
    borg.stack.undo()
    for i in range(len(obj)):
        if i == idx:
            item = old_item
        else:
            item = objs[i]
        assert obj[i] == item
    borg.stack.redo()
    for item, obj_r in zip(obj, objs):
        assert item == obj_r

    borg.stack.enabled = False
def test_parameter_advanced_convert_unit(conv_unit: str, data_in: dict,
                                         result: dict):
    d = Parameter("test", 273, units="kelvin", **data_in)
    d.convert_unit(conv_unit)
    for key in result.keys():
        assert result[key] == pytest.approx(getattr(d, key))
def test_Parameter_value_get(element, expected):
    d = Parameter("test", 1, units=element)
    assert str(d.value) == expected