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
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)
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"]))
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"]))
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))
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))
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)
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)
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)
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]
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)
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)
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]
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)
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)
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)
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)
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"])
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"])
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"])
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)
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)
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))
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)
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)
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)
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)
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)
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
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)