Esempio n. 1
0
def test_bad_add(test_data):
    item1 = XPadDataItem(test_data)
    item2 = XPadDataItem(test_data)
    item2.dim = [XPadDataDim()]

    # This won't work due to different dimensions
    with pytest.raises(ValueError):
        item3 = item1 + item2
Esempio n. 2
0
def test_init_no_dims(test_data):
    # Removing the dimensions should still let us construct an XPadDataItem
    test_data.pop("dim")
    item = XPadDataItem(test_data)

    assert np.all(item.data == test_data["data"])
    assert item.name == test_data["name"]
    assert np.all(item.dim == XPadDataItem().dim)
Esempio n. 3
0
def test_inplace_subtract(test_data):
    item1 = XPadDataItem(test_data)
    item2 = XPadDataItem(test_data)

    item1 -= item2

    assert np.all(item1.data == np.zeros(len(test_data["data"])))
    assert "test data - test data" in str(item1)
    assert np.all(item1.errl == np.hypot(test_data["errl"], test_data["errh"]))
    assert np.all(item1.errh == np.hypot(test_data["errh"], test_data["errl"]))
Esempio n. 4
0
def test_inplace_add(test_data):
    item1 = XPadDataItem(test_data)
    item2 = XPadDataItem(test_data)

    item1 += item2

    assert np.all(item1.data == test_data["data"] * 2)
    assert "test data + test data" in str(item1)
    assert np.all(item1.errl == np.hypot(test_data["errl"], test_data["errl"]))
    assert np.all(item1.errh == np.hypot(test_data["errh"], test_data["errh"]))
Esempio n. 5
0
def test_inplace_multiply(test_data):
    item1 = XPadDataItem(test_data)
    item2 = XPadDataItem(test_data)

    item1 *= item2

    assert np.all(item1.data == test_data["data"]**2)
    assert "test data * test data" in str(item1)
    data_times_errl = test_data["data"] * test_data["errl"]
    data_times_errh = test_data["data"] * test_data["errh"]
    assert np.all(item1.errl == np.hypot(data_times_errl, data_times_errl))
    assert np.all(item1.errh == np.hypot(data_times_errh, data_times_errh))
Esempio n. 6
0
def test_inplace_divide(test_data):
    item1 = XPadDataItem(test_data)
    item2 = XPadDataItem(test_data)

    item1 /= item2

    assert np.all(item1.data == np.ones(len(test_data["data"])))
    assert "test data / test data" in str(item1)
    data = test_data["data"]
    errl = test_data["errl"]
    errh = test_data["errh"]
    assert np.all(item1.errl == np.hypot(errl / data, data * errh / data**2))
    assert np.all(item1.errh == np.hypot(errh / data, data * errl / data**2))
Esempio n. 7
0
def test_init_from_other(test_data):
    # Easy way to create an other object that doesn't have all the fields
    item1 = XPadDataItem(test_data)
    del item1.errl
    del item1.order
    del item1.time

    item2 = XPadDataItem(item1)

    assert np.all(item2.data == test_data["data"])
    assert item2.name == test_data["name"]
    assert np.all(item2.dim == test_data["dim"])

    assert str(test_data["dim"][0]) in str(item2)
Esempio n. 8
0
def test_right_multiply(test_data):
    item1 = XPadDataItem(test_data)

    item2 = 3.3 * item1

    assert np.all(item2.data == 3.3 * test_data["data"])
    assert "test data * 3.3" in str(item2)
Esempio n. 9
0
def test_right_subtract(test_data):
    item1 = XPadDataItem(test_data)

    item2 = 2.2 - item1

    assert np.all(item2.data == 2.2 - test_data["data"])
    assert "2.2 - test data" in str(item2)
Esempio n. 10
0
def test_chop(test_data):
    item = XPadDataItem(test_data)

    result = user_functions.chop(item, 1.5, 2.5)

    assert len(result.data) == 1
    assert result.data == test_data["data"][1]
Esempio n. 11
0
def test_right_divide(test_data):
    item1 = XPadDataItem(test_data)

    item2 = 4.4 / item1

    assert np.all(item2.data == 4.4 / test_data["data"])
    assert "4.4 / test data" in str(item2)
Esempio n. 12
0
def test_right_add(test_data):
    item1 = XPadDataItem(test_data)

    item2 = 1.0 + item1

    assert np.all(item2.data == test_data["data"] + 1.0)
    assert "test data + 1." in str(item2)
Esempio n. 13
0
def test_clip(test_data):
    item = XPadDataItem(test_data)

    result = user_functions.clip(item, 0.0, 5.5)

    assert min(result.data) == 5.0
    assert max(result.data) == 5.0
    assert result.dim[0].data == [2.0]
Esempio n. 14
0
def test_bad_clip(test_data):
    item = XPadDataItem(test_data)

    with pytest.raises(ValueError):
        user_functions.clip(item, 3.0, 0.0)

    with pytest.raises(ValueError):
        user_functions.clip(item, 3.0, 3.0)
Esempio n. 15
0
def test_invert(test_data):
    item = XPadDataItem(test_data)

    invert_item = user_functions.invert(item)
    np_invert = -test_data["data"]

    assert "-test data" in str(invert_item)
    assert np.allclose(invert_item.data, np_invert, equal_nan=True)
Esempio n. 16
0
def test_init_from_dict(test_data):
    item = XPadDataItem(test_data)

    assert np.all(item.data == test_data["data"])
    assert item.name == test_data["name"]
    assert np.all(item.dim == test_data["dim"])

    assert str(test_data["dim"][0]) in str(item)
Esempio n. 17
0
def test_time_offset(test_data, qtbot, monkeypatch):
    item = XPadDataItem(test_data)

    offset = 1.1
    monkeypatch.setattr(QInputDialog, "getDouble", lambda *args: (offset, True))
    result = user_functions.timeOffset(item)

    assert np.allclose(result.dim[0].data, test_data["dim"][0].data + offset)
Esempio n. 18
0
def test_absolute(test_data):
    item1 = XPadDataItem(test_data)

    item2 = abs(item1)

    assert np.all(item2.data == abs(test_data["data"]))
    assert "abs(test data)" in str(item2)
    assert np.all(item2.errl == np.zeros(len(test_data["errl"])))
    assert np.all(item2.errh == test_data["errl"])
Esempio n. 19
0
def test_unary_positive(test_data):
    item1 = XPadDataItem(test_data)

    item2 = +item1

    assert np.all(item2.data == test_data["data"])
    assert "test data" in str(item2)
    assert np.all(item2.errl == test_data["errl"])
    assert np.all(item2.errh == test_data["errh"])
Esempio n. 20
0
def test_unary_negation(test_data):
    item1 = XPadDataItem(test_data)

    item2 = -item1

    assert np.all(item2.data == -test_data["data"])
    assert "-test data" in str(item2)
    assert np.all(item2.errh == test_data["errl"])
    assert np.all(item2.errl == test_data["errh"])
Esempio n. 21
0
def test_add_constant(test_data, qtbot, monkeypatch):
    item = XPadDataItem(test_data)

    constant = 3.14
    monkeypatch.setattr(QInputDialog, "getDouble", lambda *args: (constant, True))
    result = user_functions.addcon(item)

    assert f"test data + {constant}" in str(result)
    assert np.allclose(result.data, test_data["data"] + constant)
Esempio n. 22
0
def test_reciprocal(test_data):
    item = XPadDataItem(test_data)

    reciprocal_item = user_functions.reciprocal(item)
    np_reciprocal = np.reciprocal(test_data["data"])

    assert "recip(test data)" in str(reciprocal_item)
    assert chr(0x207B) + chr(0x00B9) in reciprocal_item.units
    assert np.allclose(reciprocal_item.data, np_reciprocal, equal_nan=True)
Esempio n. 23
0
def test_integrate(test_data):
    x = np.linspace(0.0, 1.0)
    test_data["dim"] = [XPadDataDim({"data": x})]
    test_data["data"] = x

    item = XPadDataItem(test_data)

    result = calculus.integrate(item)

    assert np.allclose(result.data, 0.5 * (x**2))
Esempio n. 24
0
def test_normalise(test_data):
    test_data["data"] = 4.0 * np.ones((4))
    test_data["dim"] = [XPadDataDim({"data": np.linspace(0.0, 1.0, 4)})]
    item = XPadDataItem(test_data)

    norm_item = user_functions.normalise(item)
    np_norm = np.ones((4))

    assert "Norm(test data)" in str(norm_item)
    assert np.allclose(norm_item.data, np_norm)
Esempio n. 25
0
def test_differentiate(test_data):
    x = np.linspace(0.0, 2.0 * np.pi, 128)
    test_data["dim"] = [XPadDataDim({"data": x})]
    test_data["data"] = np.sin(x)

    item = XPadDataItem(test_data)

    result = calculus.differentiate(item)

    assert np.allclose(result.data[1:-1], np.cos(x[1:-1]), 1e-2)
Esempio n. 26
0
def test_change_units(test_data, qtbot, monkeypatch):
    item = XPadDataItem(test_data)

    unit_factor = 2.0
    new_unit_name = "new unit"
    monkeypatch.setattr(QInputDialog, "getDouble", lambda *args: (unit_factor, True))
    monkeypatch.setattr(QInputDialog, "getText", lambda *args: (new_unit_name, True))
    result = user_functions.changeunits(item)

    assert np.allclose(result.data, test_data["data"] * unit_factor)
    assert f"test data({new_unit_name})" in str(result)
Esempio n. 27
0
def test_xpadfunction(test_data):
    def identity(data):
        return data

    identity_func = user_functions.XPadFunction(identity, name="identity")

    item = XPadDataItem(test_data)
    identity_item = identity_func(item)

    assert np.all(item.data == identity_item.data)
    assert np.all(item.dim == identity_item.dim)
    assert "identity(test data)" in str(identity_item)
Esempio n. 28
0
def test_bad_chop(test_data):
    item = XPadDataItem(test_data)

    with pytest.raises(ValueError):
        user_functions.chop(item, 2.5, 1.5)

    with pytest.raises(ValueError):
        user_functions.chop(item, 3.5, 4.5)

    with pytest.raises(ValueError):
        user_functions.chop(item, 0.5, 0.75)

    with pytest.raises(ValueError):
        user_functions.chop(item, 1.5, 1.75)
Esempio n. 29
0
 def get(name: str, shot: Union[int, str]):
     if name == "lastshot":
         child = MockSignalData()
         child.lastshot = int(MOCK_LAST_SHOT)
         signal = MockSignalData()
         signal.children = [child]
         return signal
     if name.startswith("meta::list"):
         signal = MockSignalData()
         shot_data = MOCK_SHOTS[str(shot).strip()]
         signal.signal_name = [item[0] for item in shot_data]
         signal.description = [item[1] for item in shot_data]
         return {"data": signal}
     x = np.linspace(0.0, 1.0)
     dim = XPadDataDim({"name": "time", "data": x})
     data = XPadDataItem({"name": name, "data": np.sin(x), "dim": [dim]})
     return data
Esempio n. 30
0
def test_basic_arithmetic_functions(test_data, func):
    item = XPadDataItem(test_data)

    func_names = {
        "absolute": "abs",
        "exponential": "exp",
        "nlog": "log",
    }
    func_name = func_names.get(func, func)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        user_func = getattr(user_functions, func)
        func_item = user_func(item)
        np_func = getattr(np, func_name)
        np_data = np_func(test_data["data"])

    assert f"{func_name}(test data)" in str(func_item)
    assert np.allclose(func_item.data, np_data, equal_nan=True)