tidy up old code
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import numpy as np
|
||||
from backend.data_types import DataField
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
|
||||
|
||||
def test_crop_resize_field():
|
||||
@@ -19,42 +20,38 @@ def test_crop_resize_field():
|
||||
)
|
||||
|
||||
overlays = []
|
||||
CropResizeField._broadcast_overlay_fn = lambda nid, data: overlays.append(data)
|
||||
CropResizeField._current_node_id = "test"
|
||||
with execution_callbacks(overlay=lambda nid, data: overlays.append(data)), active_node("test"):
|
||||
cropped, = node.process(field, x1=0.25, y1=0.25, x2=0.75, y2=1.0, target_width=0, target_height=0, interpolation="bilinear")
|
||||
assert cropped.data.shape == (3, 4)
|
||||
assert np.array_equal(cropped.data, data[1:4, 2:6])
|
||||
assert cropped.xreal == 4.0
|
||||
assert cropped.yreal == 3.0
|
||||
assert cropped.xoff == 12.0
|
||||
assert cropped.yoff == 21.0
|
||||
assert cropped.si_unit_xy == field.si_unit_xy
|
||||
assert cropped.si_unit_z == field.si_unit_z
|
||||
assert cropped.overlays == []
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "crop_box"
|
||||
assert overlays[0]["image"].startswith("data:image/png;base64,")
|
||||
assert overlays[0]["a_locked"] is False
|
||||
assert overlays[0]["b_locked"] is False
|
||||
|
||||
cropped, = node.process(field, x1=0.25, y1=0.25, x2=0.75, y2=1.0, target_width=0, target_height=0, interpolation="bilinear")
|
||||
assert cropped.data.shape == (3, 4)
|
||||
assert np.array_equal(cropped.data, data[1:4, 2:6])
|
||||
assert cropped.xreal == 4.0
|
||||
assert cropped.yreal == 3.0
|
||||
assert cropped.xoff == 12.0
|
||||
assert cropped.yoff == 21.0
|
||||
assert cropped.si_unit_xy == field.si_unit_xy
|
||||
assert cropped.si_unit_z == field.si_unit_z
|
||||
assert cropped.overlays == []
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "crop_box"
|
||||
assert overlays[0]["image"].startswith("data:image/png;base64,")
|
||||
assert overlays[0]["a_locked"] is False
|
||||
assert overlays[0]["b_locked"] is False
|
||||
resized, = node.process(field, x1=0.0, y1=0.0, x2=1.0, y2=1.0, target_width=8, target_height=0, interpolation="bilinear", corner_a=(0.25, 0.25), corner_b=(0.75, 1.0))
|
||||
assert resized.data.shape == (6, 8)
|
||||
assert resized.xreal == cropped.xreal
|
||||
assert resized.yreal == cropped.yreal
|
||||
assert resized.xoff == cropped.xoff
|
||||
assert resized.yoff == cropped.yoff
|
||||
assert resized.domain == field.domain
|
||||
assert overlays[-1]["a_locked"] is True
|
||||
assert overlays[-1]["b_locked"] is True
|
||||
|
||||
resized, = node.process(field, x1=0.0, y1=0.0, x2=1.0, y2=1.0, target_width=8, target_height=0, interpolation="bilinear", corner_a=(0.25, 0.25), corner_b=(0.75, 1.0))
|
||||
assert resized.data.shape == (6, 8)
|
||||
assert resized.xreal == cropped.xreal
|
||||
assert resized.yreal == cropped.yreal
|
||||
assert resized.xoff == cropped.xoff
|
||||
assert resized.yoff == cropped.yoff
|
||||
assert resized.domain == field.domain
|
||||
assert overlays[-1]["a_locked"] is True
|
||||
assert overlays[-1]["b_locked"] is True
|
||||
reversed_crop, = node.process(field, x1=0.75, y1=1.0, x2=0.25, y2=0.25, target_width=0, target_height=0, interpolation="nearest")
|
||||
assert np.array_equal(reversed_crop.data, cropped.data)
|
||||
|
||||
reversed_crop, = node.process(field, x1=0.75, y1=1.0, x2=0.25, y2=0.25, target_width=0, target_height=0, interpolation="nearest")
|
||||
assert np.array_equal(reversed_crop.data, cropped.data)
|
||||
|
||||
try:
|
||||
node.process(field, x1=0.9, y1=0.0, x2=0.9, y2=1.0, target_width=0, target_height=0, interpolation="nearest")
|
||||
raise AssertionError("Expected invalid crop bounds to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
CropResizeField._broadcast_overlay_fn = None
|
||||
try:
|
||||
node.process(field, x1=0.9, y1=0.0, x2=0.9, y2=1.0, target_width=0, target_height=0, interpolation="nearest")
|
||||
raise AssertionError("Expected invalid crop bounds to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
@@ -42,11 +42,10 @@ def test_cross_section():
|
||||
assert rows["dx"]["unit"] == field.si_unit_xy
|
||||
assert rows["dy"]["unit"] == field.si_unit_z
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
captured = []
|
||||
Stats._broadcast_value_fn = lambda nid, payload: captured.append(payload)
|
||||
Stats._current_node_id = "test"
|
||||
stats = Stats()
|
||||
mean_value, = stats.process(profile, operation="mean", column="value")
|
||||
assert mean_value > 0
|
||||
assert captured[-1]["unit"] == field.si_unit_z
|
||||
Stats._broadcast_value_fn = None
|
||||
with execution_callbacks(value=lambda nid, payload: captured.append(payload)), active_node("test"):
|
||||
stats = Stats()
|
||||
mean_value, = stats.process(profile, operation="mean", column="value")
|
||||
assert mean_value > 0
|
||||
assert captured[-1]["unit"] == field.si_unit_z
|
||||
|
||||
@@ -12,53 +12,50 @@ def test_line_cursors():
|
||||
|
||||
line = np.linspace(0, 10, 100).astype(np.float64)
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
overlays = []
|
||||
Cursors._broadcast_overlay_fn = lambda nid, data: overlays.append(data)
|
||||
Cursors._current_node_id = "test"
|
||||
with execution_callbacks(overlay=lambda nid, data: overlays.append(data)), active_node("test"):
|
||||
table, coord_pair = node.process(line, x1=0.25, y1=0.5, x2=0.75, y2=0.5)
|
||||
assert isinstance(coord_pair, tuple) and len(coord_pair) == 2
|
||||
assert len(table) == 7
|
||||
quantities = {row["quantity"] for row in table}
|
||||
assert "Length" in quantities
|
||||
assert "A x" in quantities
|
||||
assert "B x" in quantities
|
||||
assert "dx" in quantities
|
||||
assert "dy" in quantities
|
||||
|
||||
table, coord_pair = node.process(line, x1=0.25, y1=0.5, x2=0.75, y2=0.5)
|
||||
assert isinstance(coord_pair, tuple) and len(coord_pair) == 2
|
||||
assert len(table) == 7
|
||||
quantities = {row["quantity"] for row in table}
|
||||
assert "Length" in quantities
|
||||
assert "A x" in quantities
|
||||
assert "B x" in quantities
|
||||
assert "dx" in quantities
|
||||
assert "dy" in quantities
|
||||
a_pos = next(r["value"] for r in table if r["quantity"] == "A x")
|
||||
b_pos = next(r["value"] for r in table if r["quantity"] == "B x")
|
||||
assert b_pos > a_pos
|
||||
|
||||
a_pos = next(r["value"] for r in table if r["quantity"] == "A x")
|
||||
b_pos = next(r["value"] for r in table if r["quantity"] == "B x")
|
||||
assert b_pos > a_pos
|
||||
dy = next(r["value"] for r in table if r["quantity"] == "dy")
|
||||
assert dy > 0
|
||||
|
||||
dy = next(r["value"] for r in table if r["quantity"] == "dy")
|
||||
assert dy > 0
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "line_plot"
|
||||
assert len(overlays[0]["line"]) == len(line)
|
||||
assert len(overlays[0]["x_axis"]) == len(line)
|
||||
assert 0.0 <= overlays[0]["x1"] <= 1.0
|
||||
assert 0.0 <= overlays[0]["x2"] <= 1.0
|
||||
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "line_plot"
|
||||
assert len(overlays[0]["line"]) == len(line)
|
||||
assert len(overlays[0]["x_axis"]) == len(line)
|
||||
assert 0.0 <= overlays[0]["x1"] <= 1.0
|
||||
assert 0.0 <= overlays[0]["x2"] <= 1.0
|
||||
line_data = LineData(data=line, x_axis=np.linspace(0, 1, 100))
|
||||
table2, _ = node.process(line_data, x1=0.25, y1=0.5, x2=0.75, y2=0.5)
|
||||
assert len(table2) == 7
|
||||
|
||||
line_data = LineData(data=line, x_axis=np.linspace(0, 1, 100))
|
||||
table2, _ = node.process(line_data, x1=0.25, y1=0.5, x2=0.75, y2=0.5)
|
||||
assert len(table2) == 7
|
||||
|
||||
field = DataField(
|
||||
data=np.arange(100, dtype=np.float64).reshape(10, 10),
|
||||
xreal=2.0, yreal=4.0, si_unit_xy="um", si_unit_z="nm",
|
||||
)
|
||||
overlays.clear()
|
||||
table3, _ = node.process(field, x1=0.2, y1=0.25, x2=0.7, y2=0.75)
|
||||
assert len(table3) == 9
|
||||
field_rows = {row["quantity"]: row for row in table3}
|
||||
assert field_rows["dx"]["unit"] == "um"
|
||||
assert field_rows["dy"]["unit"] == "um"
|
||||
assert field_rows["dz"]["unit"] == "nm"
|
||||
assert np.isclose(field_rows["dx"]["value"], 1.0)
|
||||
assert np.isclose(field_rows["dy"]["value"], 2.0)
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "cursor_points"
|
||||
assert overlays[0]["image"].startswith("data:image/png;base64,")
|
||||
|
||||
Cursors._broadcast_overlay_fn = None
|
||||
field = DataField(
|
||||
data=np.arange(100, dtype=np.float64).reshape(10, 10),
|
||||
xreal=2.0, yreal=4.0, si_unit_xy="um", si_unit_z="nm",
|
||||
)
|
||||
overlays.clear()
|
||||
table3, _ = node.process(field, x1=0.2, y1=0.25, x2=0.7, y2=0.75)
|
||||
assert len(table3) == 9
|
||||
field_rows = {row["quantity"]: row for row in table3}
|
||||
assert field_rows["dx"]["unit"] == "um"
|
||||
assert field_rows["dy"]["unit"] == "um"
|
||||
assert field_rows["dz"]["unit"] == "nm"
|
||||
assert np.isclose(field_rows["dx"]["value"], 1.0)
|
||||
assert np.isclose(field_rows["dy"]["value"], 2.0)
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "cursor_points"
|
||||
assert overlays[0]["image"].startswith("data:image/png;base64,")
|
||||
|
||||
@@ -109,7 +109,7 @@ def test_measurement_value_errors():
|
||||
|
||||
|
||||
def test_format_with_unit():
|
||||
from backend.nodes.helpers import _format_with_unit, _format_numeric
|
||||
from backend.data_types import _format_with_unit, _format_numeric
|
||||
|
||||
assert _format_numeric(0.0) == "0"
|
||||
assert not np.isfinite(float('inf')) or _format_numeric(float('inf')) is not None
|
||||
@@ -182,7 +182,7 @@ def test_square_unit_and_apply():
|
||||
|
||||
|
||||
def test_nice_length():
|
||||
from backend.nodes.helpers import _nice_length
|
||||
from backend.data_types import _nice_length
|
||||
|
||||
assert _nice_length(0.0) == 0.0
|
||||
assert _nice_length(float('inf')) == 0.0
|
||||
|
||||
@@ -9,32 +9,29 @@ def test_height_histogram():
|
||||
data = np.linspace(0, 1, 1000).reshape(25, 40)
|
||||
field = make_field(data=data)
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
overlays = []
|
||||
Histogram._broadcast_overlay_fn = lambda nid, data: overlays.append(data)
|
||||
Histogram._current_node_id = "test"
|
||||
|
||||
table, coord_pair = node.process(field, n_bins=10, y_scale="linear", x1=0.2, y1=0.5, x2=0.8, y2=0.5)
|
||||
assert isinstance(coord_pair, tuple) and len(coord_pair) == 2
|
||||
measurements = {row["quantity"]: row for row in table}
|
||||
assert "A position" in measurements
|
||||
assert "A count" in measurements
|
||||
assert "B position" in measurements
|
||||
assert "B count" in measurements
|
||||
assert "delta X" in measurements
|
||||
assert "delta Y" in measurements
|
||||
assert measurements["A count"]["unit"] == "count"
|
||||
assert measurements["B count"]["unit"] == "count"
|
||||
assert measurements["B position"]["value"] > measurements["A position"]["value"]
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "line_plot"
|
||||
assert overlays[0]["section_title"] == "Histogram"
|
||||
assert len(overlays[0]["line"]) == 10
|
||||
assert len(overlays[0]["x_axis"]) == 10
|
||||
assert np.isclose(overlays[0]["x1"], 0.2)
|
||||
assert np.isclose(overlays[0]["x2"], 0.8)
|
||||
assert np.isclose(
|
||||
measurements["delta Y"]["value"],
|
||||
measurements["B count"]["value"] - measurements["A count"]["value"],
|
||||
)
|
||||
|
||||
Histogram._broadcast_overlay_fn = None
|
||||
with execution_callbacks(overlay=lambda nid, data: overlays.append(data)), active_node("test"):
|
||||
table, coord_pair = node.process(field, n_bins=10, y_scale="linear", x1=0.2, y1=0.5, x2=0.8, y2=0.5)
|
||||
assert isinstance(coord_pair, tuple) and len(coord_pair) == 2
|
||||
measurements = {row["quantity"]: row for row in table}
|
||||
assert "A position" in measurements
|
||||
assert "A count" in measurements
|
||||
assert "B position" in measurements
|
||||
assert "B count" in measurements
|
||||
assert "delta X" in measurements
|
||||
assert "delta Y" in measurements
|
||||
assert measurements["A count"]["unit"] == "count"
|
||||
assert measurements["B count"]["unit"] == "count"
|
||||
assert measurements["B position"]["value"] > measurements["A position"]["value"]
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "line_plot"
|
||||
assert overlays[0]["section_title"] == "Histogram"
|
||||
assert len(overlays[0]["line"]) == 10
|
||||
assert len(overlays[0]["x_axis"]) == 10
|
||||
assert np.isclose(overlays[0]["x1"], 0.2)
|
||||
assert np.isclose(overlays[0]["x2"], 0.8)
|
||||
assert np.isclose(
|
||||
measurements["delta Y"]["value"],
|
||||
measurements["B count"]["value"] - measurements["A count"]["value"],
|
||||
)
|
||||
|
||||
@@ -126,12 +126,13 @@ def test_load_file_unsupported():
|
||||
|
||||
def test_load_file_warning():
|
||||
from backend.nodes.image import Image as ImageNode
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
node = ImageNode()
|
||||
warnings = []
|
||||
ImageNode._broadcast_warning_fn = lambda nid, msg: warnings.append(msg)
|
||||
ImageNode._current_node_id = "test"
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
with tempfile.TemporaryDirectory() as tmpdir, \
|
||||
execution_callbacks(warning=lambda nid, msg: warnings.append(msg)), \
|
||||
active_node("test"):
|
||||
arr = np.random.default_rng(10).integers(0, 256, (16, 16), dtype=np.uint8)
|
||||
img = PILImage.fromarray(arr)
|
||||
path = os.path.join(tmpdir, "test.png")
|
||||
@@ -142,8 +143,6 @@ def test_load_file_warning():
|
||||
assert len(warnings) == 1
|
||||
assert "Uncalibrated" in warnings[0]
|
||||
|
||||
ImageNode._broadcast_warning_fn = None
|
||||
|
||||
|
||||
def test_load_file_ibw():
|
||||
from backend.nodes.image import Image
|
||||
|
||||
@@ -8,33 +8,30 @@ def test_draw_mask():
|
||||
node = DrawMask()
|
||||
|
||||
field = make_field(data=np.zeros((32, 32), dtype=np.float64))
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
overlays = []
|
||||
DrawMask._broadcast_overlay_fn = lambda nid, data: overlays.append(data)
|
||||
DrawMask._current_node_id = "test"
|
||||
with execution_callbacks(overlay=lambda nid, data: overlays.append(data)), active_node("test"):
|
||||
mask_paths = [{"size": 5, "points": [{"x": 0.2, "y": 0.5}, {"x": 0.8, "y": 0.5}]}]
|
||||
|
||||
mask_paths = [{"size": 5, "points": [{"x": 0.2, "y": 0.5}, {"x": 0.8, "y": 0.5}]}]
|
||||
mask, = node.process(field, pen_size=2, invert=False, mask_paths=json.dumps(mask_paths))
|
||||
assert mask.dtype == np.uint8
|
||||
assert mask.shape == (32, 32)
|
||||
assert mask[16, 16] == 255
|
||||
assert mask[14, 16] == 255
|
||||
assert mask[0, 0] == 0
|
||||
|
||||
mask, = node.process(field, pen_size=2, invert=False, mask_paths=json.dumps(mask_paths))
|
||||
assert mask.dtype == np.uint8
|
||||
assert mask.shape == (32, 32)
|
||||
assert mask[16, 16] == 255
|
||||
assert mask[14, 16] == 255
|
||||
assert mask[0, 0] == 0
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "mask_paint"
|
||||
assert overlays[0]["section_title"] == "Mask"
|
||||
assert overlays[0]["image"].startswith("data:image/png;base64,")
|
||||
assert overlays[0]["image_width"] == field.xres
|
||||
assert overlays[0]["image_height"] == field.yres
|
||||
assert overlays[0]["invert"] is False
|
||||
|
||||
assert len(overlays) == 1
|
||||
assert overlays[0]["kind"] == "mask_paint"
|
||||
assert overlays[0]["section_title"] == "Mask"
|
||||
assert overlays[0]["image"].startswith("data:image/png;base64,")
|
||||
assert overlays[0]["image_width"] == field.xres
|
||||
assert overlays[0]["image_height"] == field.yres
|
||||
assert overlays[0]["invert"] is False
|
||||
inverted, = node.process(field, pen_size=2, invert=True, mask_paths=json.dumps(mask_paths))
|
||||
assert inverted[16, 16] == 0
|
||||
assert inverted[0, 0] == 255
|
||||
assert overlays[-1]["invert"] is True
|
||||
|
||||
inverted, = node.process(field, pen_size=2, invert=True, mask_paths=json.dumps(mask_paths))
|
||||
assert inverted[16, 16] == 0
|
||||
assert inverted[0, 0] == 255
|
||||
assert overlays[-1]["invert"] is True
|
||||
|
||||
cleared, = node.process(field, pen_size=12, invert=False, mask_paths="[]")
|
||||
assert np.count_nonzero(cleared) == 0
|
||||
|
||||
DrawMask._broadcast_overlay_fn = None
|
||||
cleared, = node.process(field, pen_size=12, invert=False, mask_paths="[]")
|
||||
assert np.count_nonzero(cleared) == 0
|
||||
|
||||
@@ -10,30 +10,27 @@ def test_threshold_mask():
|
||||
data[:, 32:] = 1.0
|
||||
field = make_field(data=data)
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
previews = []
|
||||
ThresholdMask._broadcast_fn = lambda nid, uri: previews.append(uri)
|
||||
ThresholdMask._current_node_id = "test"
|
||||
with execution_callbacks(preview=lambda nid, uri: previews.append(uri)), active_node("test"):
|
||||
mask, table = node.process(field, method="absolute", threshold=0.5, direction="above")
|
||||
assert mask.dtype == np.uint8
|
||||
assert mask.shape == (64, 64)
|
||||
assert np.all(mask[:, :32] == 0)
|
||||
assert np.all(mask[:, 32:] == 255)
|
||||
|
||||
mask, table = node.process(field, method="absolute", threshold=0.5, direction="above")
|
||||
assert mask.dtype == np.uint8
|
||||
assert mask.shape == (64, 64)
|
||||
assert np.all(mask[:, :32] == 0)
|
||||
assert np.all(mask[:, 32:] == 255)
|
||||
assert len(previews) == 1
|
||||
assert previews[0].startswith("data:image/png;base64,")
|
||||
|
||||
assert len(previews) == 1
|
||||
assert previews[0].startswith("data:image/png;base64,")
|
||||
mask_below, _ = node.process(field, method="absolute", threshold=0.5, direction="below")
|
||||
assert np.all(mask_below[:, :32] == 255)
|
||||
assert np.all(mask_below[:, 32:] == 0)
|
||||
|
||||
mask_below, _ = node.process(field, method="absolute", threshold=0.5, direction="below")
|
||||
assert np.all(mask_below[:, :32] == 255)
|
||||
assert np.all(mask_below[:, 32:] == 0)
|
||||
mask_rel, _ = node.process(field, method="relative", threshold=0.5, direction="above")
|
||||
assert np.all(mask_rel[:, 32:] == 255)
|
||||
|
||||
mask_rel, _ = node.process(field, method="relative", threshold=0.5, direction="above")
|
||||
assert np.all(mask_rel[:, 32:] == 255)
|
||||
|
||||
mask_otsu, _ = node.process(field, method="otsu", threshold=0.0, direction="above")
|
||||
assert mask_otsu[:, 32:].sum() > mask_otsu[:, :32].sum()
|
||||
|
||||
ThresholdMask._broadcast_fn = None
|
||||
mask_otsu, _ = node.process(field, method="otsu", threshold=0.0, direction="above")
|
||||
assert mask_otsu[:, 32:].sum() > mask_otsu[:, :32].sum()
|
||||
|
||||
|
||||
def test_threshold_mask_unknown_method():
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
def test_print_table():
|
||||
from backend.nodes.print_table import PrintTable
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
node = PrintTable()
|
||||
|
||||
table_spec = PrintTable.INPUT_TYPES()["required"]["table"]
|
||||
@@ -7,12 +8,8 @@ def test_print_table():
|
||||
assert table_spec[1]["accepted_types"] == ["DATA_TABLE"]
|
||||
|
||||
captured = []
|
||||
PrintTable._broadcast_table_fn = lambda node_id, rows: captured.append(rows)
|
||||
PrintTable._current_node_id = "test"
|
||||
|
||||
table = [{"quantity": "test", "value": 42.0, "unit": "m"}]
|
||||
node.print_table(table=table)
|
||||
assert len(captured) == 1
|
||||
assert captured[0] == table
|
||||
|
||||
PrintTable._broadcast_table_fn = None
|
||||
with execution_callbacks(table=lambda nid, rows: captured.append(rows)), active_node("test"):
|
||||
table = [{"quantity": "test", "value": 42.0, "unit": "m"}]
|
||||
node.print_table(table=table)
|
||||
assert len(captured) == 1
|
||||
assert captured[0] == table
|
||||
|
||||
@@ -42,22 +42,20 @@ def test_rotate_field():
|
||||
def test_rotate_field_overlay_warning():
|
||||
from backend.nodes.rotate import RotateField
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
node = RotateField()
|
||||
warnings = []
|
||||
RotateField._broadcast_warning_fn = lambda nid, msg: warnings.append(msg)
|
||||
RotateField._current_node_id = "test"
|
||||
|
||||
field = DataField(
|
||||
data=np.arange(16, dtype=np.float64).reshape(4, 4),
|
||||
overlays=[{"kind": "markup", "shapes": [{"kind": "line", "x1": 0.1, "y1": 0.1, "x2": 0.9, "y2": 0.9, "width": 2, "color": "#ffffff"}]}],
|
||||
)
|
||||
|
||||
rotated, = node.process(field, angle=30.0, interpolation="bilinear", expand_canvas=True)
|
||||
assert rotated.overlays == []
|
||||
assert len(warnings) == 1
|
||||
assert "clears annotation/markup overlays" in warnings[0]
|
||||
|
||||
RotateField._broadcast_warning_fn = None
|
||||
with execution_callbacks(warning=lambda nid, msg: warnings.append(msg)), active_node("test"):
|
||||
rotated, = node.process(field, angle=30.0, interpolation="bilinear", expand_canvas=True)
|
||||
assert rotated.overlays == []
|
||||
assert len(warnings) == 1
|
||||
assert "clears annotation/markup overlays" in warnings[0]
|
||||
|
||||
|
||||
def test_rotate_unknown_interpolation():
|
||||
|
||||
@@ -11,61 +11,58 @@ def test_stats():
|
||||
assert input_spec[0] == "DATA_FIELD"
|
||||
assert input_spec[1]["accepted_types"] == ["IMAGE", "LINE", "DATA_TABLE"]
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
captured = []
|
||||
Stats._broadcast_value_fn = lambda node_id, payload: captured.append((node_id, payload))
|
||||
Stats._current_node_id = "test"
|
||||
with execution_callbacks(value=lambda nid, payload: captured.append((nid, payload))), active_node("test"):
|
||||
line = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64)
|
||||
result, = node.process(line, operation="mean", column="value")
|
||||
assert np.isclose(result, 2.5)
|
||||
assert captured[-1] == ("test", {"value": result})
|
||||
roughness, = node.process(line, operation="Rq", column="value")
|
||||
assert np.isclose(roughness, np.sqrt(np.mean((line - line.mean()) ** 2)))
|
||||
|
||||
line = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64)
|
||||
result, = node.process(line, operation="mean", column="value")
|
||||
assert np.isclose(result, 2.5)
|
||||
assert captured[-1] == ("test", {"value": result})
|
||||
roughness, = node.process(line, operation="Rq", column="value")
|
||||
assert np.isclose(roughness, np.sqrt(np.mean((line - line.mean()) ** 2)))
|
||||
table = DataTable([
|
||||
{"name": "a", "value": 3.0, "unit": "m", "other": 10.0},
|
||||
{"name": "b", "value": 7.0, "unit": "m", "other": 20.0},
|
||||
])
|
||||
result, = node.process(table, operation="max", column="value")
|
||||
assert result == 7.0
|
||||
assert captured[-1] == ("test", {"value": 7.0, "unit": "m"})
|
||||
count, = node.process(table, operation="count", column="other")
|
||||
assert count == 2.0
|
||||
auto_column_range, = node.process(table, operation="range", column="")
|
||||
assert auto_column_range == 4.0
|
||||
|
||||
table = DataTable([
|
||||
{"name": "a", "value": 3.0, "unit": "m", "other": 10.0},
|
||||
{"name": "b", "value": 7.0, "unit": "m", "other": 20.0},
|
||||
])
|
||||
result, = node.process(table, operation="max", column="value")
|
||||
assert result == 7.0
|
||||
assert captured[-1] == ("test", {"value": 7.0, "unit": "m"})
|
||||
count, = node.process(table, operation="count", column="other")
|
||||
assert count == 2.0
|
||||
auto_column_range, = node.process(table, operation="range", column="")
|
||||
assert auto_column_range == 4.0
|
||||
field = make_field(data=np.array([[1.0, 5.0], [2.0, 4.0]], dtype=np.float64))
|
||||
result, = node.process(field, operation="range", column="value")
|
||||
assert result == 4.0
|
||||
assert captured[-1] == ("test", {"value": 4.0, "unit": "m"})
|
||||
|
||||
field = make_field(data=np.array([[1.0, 5.0], [2.0, 4.0]], dtype=np.float64))
|
||||
result, = node.process(field, operation="range", column="value")
|
||||
assert result == 4.0
|
||||
assert captured[-1] == ("test", {"value": 4.0, "unit": "m"})
|
||||
image = np.array([[0, 10], [20, 30]], dtype=np.uint8)
|
||||
result, = node.process(image, operation="avg", column="value")
|
||||
assert np.isclose(result, 15.0)
|
||||
assert captured[-1] == ("test", {"value": 15.0})
|
||||
|
||||
image = np.array([[0, 10], [20, 30]], dtype=np.uint8)
|
||||
result, = node.process(image, operation="avg", column="value")
|
||||
assert np.isclose(result, 15.0)
|
||||
assert captured[-1] == ("test", {"value": 15.0})
|
||||
try:
|
||||
node.process(table, operation="Rq", column="value")
|
||||
raise AssertionError("Expected invalid TABLE operation to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
node.process(table, operation="Rq", column="value")
|
||||
raise AssertionError("Expected invalid TABLE operation to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
node.process([{"label": "only text"}], operation="max", column="label")
|
||||
raise AssertionError("Expected non-numeric record-table input to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
node.process([{"label": "only text"}], operation="max", column="label")
|
||||
raise AssertionError("Expected non-numeric record-table input to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
node.process(
|
||||
RecordTable([{"quantity": "min", "value": 1.0, "unit": "m"}]),
|
||||
operation="max", column="value",
|
||||
)
|
||||
raise AssertionError("Expected measurement table input to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
Stats._broadcast_value_fn = None
|
||||
try:
|
||||
node.process(
|
||||
RecordTable([{"quantity": "min", "value": 1.0, "unit": "m"}]),
|
||||
operation="max", column="value",
|
||||
)
|
||||
raise AssertionError("Expected measurement table input to raise ValueError")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
def test_stats_empty_inputs():
|
||||
|
||||
@@ -11,23 +11,20 @@ def test_value_display():
|
||||
assert value_spec[0] == "FLOAT"
|
||||
assert value_spec[1]["accepted_types"] == ["RECORD_TABLE"]
|
||||
|
||||
from backend.execution_context import execution_callbacks, active_node
|
||||
captured = []
|
||||
ValueIO._broadcast_value_fn = lambda node_id, payload: captured.append((node_id, payload))
|
||||
ValueIO._current_node_id = "test"
|
||||
with execution_callbacks(value=lambda nid, payload: captured.append((nid, payload))), active_node("test"):
|
||||
result = node.display_value(value=3.25)
|
||||
assert result == (3.25,)
|
||||
assert captured == [("test", {"value": 3.25})]
|
||||
|
||||
result = node.display_value(value=3.25)
|
||||
assert result == (3.25,)
|
||||
assert captured == [("test", {"value": 3.25})]
|
||||
|
||||
measurements = RecordTable([
|
||||
{"quantity": "delta X", "value": 1.7e-7, "unit": "m"},
|
||||
{"quantity": "delta Y", "value": 463, "unit": "count"},
|
||||
])
|
||||
result = node.display_value(value=measurements, measurement="delta X")
|
||||
assert result == (1.7e-7,)
|
||||
assert captured[-1] == ("test", {"value": 1.7e-7, "unit": "m"})
|
||||
|
||||
ValueIO._broadcast_value_fn = None
|
||||
measurements = RecordTable([
|
||||
{"quantity": "delta X", "value": 1.7e-7, "unit": "m"},
|
||||
{"quantity": "delta Y", "value": 463, "unit": "count"},
|
||||
])
|
||||
result = node.display_value(value=measurements, measurement="delta X")
|
||||
assert result == (1.7e-7,)
|
||||
assert captured[-1] == ("test", {"value": 1.7e-7, "unit": "m"})
|
||||
|
||||
|
||||
def test_value_display_string_input():
|
||||
|
||||
Reference in New Issue
Block a user