Skip to content

Commit d87137e

Browse files
committed
fix(loop-link): apply the PR 406 review fixes
- Viewer: the tool connection uses the address of the data connection, so select, selection and fetch go to the same viewer as send. - Viewer: a failed send or heartbeat no longer blocks later sends; only close() does. A later send opens a new connection. - ToolConnection: an error in on_frame closes the connection, so the viewer stops the stream and a later request does not read it. - pyvista adapter: refuse PolyData that mixes lines with polygons or strips; convert line cells with numpy, not a Python loop. - loopstructural adapter: flatten grid properties in x-fastest order and split (n, k) properties into one property for each column. - pandas adapter: accept 'category' columns. - Selection: the index arrays are writeable copies. - WebSocket mask: make the key pattern only as long as the frame. - Read __version__ from the package metadata (version.py), so that release-please bumps keep it correct.
1 parent 0744caa commit d87137e

16 files changed

Lines changed: 284 additions & 50 deletions

‎packages/loop_link/src/loop_link/__init__.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
__version__ = "0.1.0"
6-
75
from .commands import ColourSettings, FilterResult
86
from .errors import (
97
BusyError,
@@ -25,6 +23,7 @@
2523
from .objects import BlockModel, Categorical, Drillhole, Lines, Points, Surface, TetraMesh, Vectors
2624
from .push import PushResult, push_objects
2725
from .selection import Selection, SelectionWatcher
26+
from .version import __version__
2827
from .viewer import InventoryEntry, Viewer
2928

3029
__all__ = [

‎packages/loop_link/src/loop_link/_websocket.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def _mask(data: bytes | bytearray, key: bytes) -> bytes | bytearray:
7777
target = np.frombuffer(result, dtype=np.uint8)
7878
key_array = np.frombuffer(key, dtype=np.uint8)
7979
# The chunk size is a multiple of 4, so the key stays aligned between chunks.
80-
pattern = np.tile(key_array, _MASK_CHUNK_BYTES // 4)
80+
# Make the pattern only as long as the data needs, so small frames stay cheap.
81+
pattern = np.tile(key_array, (min(size, _MASK_CHUNK_BYTES) + 3) // 4)
8182
for start in range(0, size, _MASK_CHUNK_BYTES):
8283
end = min(start + _MASK_CHUNK_BYTES, size)
8384
np.bitwise_xor(source[start:end], pattern[: end - start], out=target[start:end])

‎packages/loop_link/src/loop_link/adapters/loopstructural.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import sys
66
from typing import Any
77

8+
import numpy as np
9+
810
from loop_link.errors import InvalidInputError, MissingExtraError
911
from loop_link.objects import BlockModel, SceneObject, Surface
1012

@@ -51,7 +53,31 @@ def from_loopstructural(obj: Any) -> SceneObject: # noqa: ANN401
5153
origin=obj.origin,
5254
cell_sizes=obj.step_vector,
5355
cells=obj.nsteps_cells,
54-
properties=dict(obj.properties or {}),
55-
cell_properties=dict(obj.cell_properties or {}),
56+
properties=_flatten(obj.properties),
57+
cell_properties=_flatten(obj.cell_properties),
5658
)
5759
raise InvalidInputError(_ACCEPTED_INPUTS)
60+
61+
62+
def _flatten(properties: dict[str, Any] | None) -> dict[str, Any]:
63+
"""Return the grid properties as 1-D arrays in x-fastest order.
64+
65+
``StructuredGrid.vtk()`` reshapes each property to ``(n, -1)`` with ``order="F"``,
66+
so a property can be a grid of shape ``(nx, ny, nz)`` or have more than one value
67+
for each node, for example a vector of shape ``(n, 3)``. A grid is flattened in
68+
x-fastest order. Each column of a 2-D array becomes one property with the name
69+
``"<name>_<column>"``. A 2-D array with one column keeps its name.
70+
"""
71+
result: dict[str, Any] = {}
72+
for name, values in (properties or {}).items():
73+
array = np.asarray(values)
74+
if array.ndim == 3:
75+
array = array.ravel(order="F")
76+
if array.ndim == 2 and array.shape[1] == 1:
77+
array = array[:, 0]
78+
if array.ndim == 2:
79+
for column in range(array.shape[1]):
80+
result[f"{name}_{column}"] = array[:, column]
81+
continue
82+
result[name] = array
83+
return result

‎packages/loop_link/src/loop_link/adapters/pandas.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def _column_array(values: pd.Series, name: str) -> np.ndarray:
3838
f"The column '{name}' has missing values. "
3939
"Fill or remove the missing values, then try again."
4040
)
41+
if str(values.dtype) == "category":
42+
# A category column, for example a lithology log: use the values of the
43+
# categories. A string column then becomes a Categorical in the viewer.
44+
return _column_array(values.astype(values.cat.categories.dtype), name)
4145
if values.dtype.kind in "biuf" and values.dtype != object:
4246
return values.to_numpy()
4347
if values.dtype.kind in "US":

‎packages/loop_link/src/loop_link/adapters/pyvista.py‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import itertools
65
from typing import Any
76

87
import numpy as np
@@ -75,20 +74,22 @@ def _collect(data: Any, where: str) -> dict[str, np.ndarray]: # noqa: ANN401
7574

7675
def _polydata_lines(mesh: Any) -> tuple[np.ndarray, np.ndarray]: # noqa: ANN401
7776
"""Return the segments of a PolyData and the source line of each segment."""
78-
flat = np.asarray(mesh.lines)
79-
segments: list[list[int]] = []
80-
owners: list[int] = []
81-
position = 0
82-
line = 0
83-
while position < len(flat):
84-
count = int(flat[position])
85-
ids = flat[position + 1 : position + 1 + count]
86-
for first, second in itertools.pairwise(ids):
87-
segments.append([int(first), int(second)])
88-
owners.append(line)
89-
position += count + 1
90-
line += 1
91-
return np.array(segments, dtype=np.int64).reshape(-1, 2), np.array(owners, dtype=np.int64)
77+
from vtkmodules.util.numpy_support import vtk_to_numpy
78+
79+
cells = mesh.GetLines()
80+
offsets = vtk_to_numpy(cells.GetOffsetsArray()).astype(np.int64)
81+
connectivity = vtk_to_numpy(cells.GetConnectivityArray()).astype(np.int64)
82+
counts = np.diff(offsets)
83+
if connectivity.size < 2:
84+
return np.empty((0, 2), dtype=np.int64), np.empty(0, dtype=np.int64)
85+
# Each point of a line, except the last one, starts a segment.
86+
starts = np.ones(connectivity.size - 1, dtype=bool)
87+
ends = offsets[1:] - 1
88+
starts[ends[(ends >= 0) & (ends < connectivity.size - 1)]] = False
89+
first = np.flatnonzero(starts)
90+
segments = np.column_stack((connectivity[first], connectivity[first + 1]))
91+
owners = np.repeat(np.arange(counts.size, dtype=np.int64), np.maximum(counts - 1, 0))
92+
return segments, owners
9293

9394

9495
def _convert_polydata(
@@ -98,6 +99,11 @@ def _convert_polydata(
9899
) -> SceneObject:
99100
"""Convert a PolyData to Lines, Surface, Points or Vectors."""
100101
if mesh.n_lines > 0:
102+
if len(mesh.faces) > 0 or mesh.n_strips > 0:
103+
raise InvalidInputError(
104+
"The PolyData mixes lines with polygons or strips. "
105+
"Split the mesh into one mesh for each kind of cell."
106+
)
101107
segments, owners = _polydata_lines(mesh)
102108
# Cell data is ordered: vertices, lines, polygons, strips.
103109
first = int(mesh.n_verts)

‎packages/loop_link/src/loop_link/selection.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def parse_selection_result(frame: Frame) -> Selection:
117117
f"The selection indices '{name}' have shape {array.shape}. "
118118
f"The entry count is {count}."
119119
)
120-
arrays[field] = array
120+
# Copy, so the array is writeable and does not keep the frame bytes in memory.
121+
arrays[field] = array.copy()
121122
return Selection(
122123
header.get("object"), header.get("revision"), arrays["cells"], arrays["vertices"]
123124
)

‎packages/loop_link/src/loop_link/tool.py‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class ToolConnection:
8383
The settings folder of the viewer.
8484
timeout : float
8585
The time in seconds to wait for the connection and for each reply frame.
86+
resolve : callable, optional
87+
Returns the address of the viewer. When it is given, the connection uses it in
88+
place of its own search, so that it connects to the same viewer as another
89+
connection. The other address parameters are then not used.
8690
"""
8791

8892
def __init__(
@@ -93,7 +97,9 @@ def __init__(
9397
host: str = "127.0.0.1",
9498
settings_dir: Path | None = None,
9599
timeout: float = 5.0,
100+
resolve: Callable[[], discovery.ViewerAddress] | None = None,
96101
) -> None:
102+
self._resolve = resolve
97103
self._port = port
98104
self._token = token
99105
self._host = host
@@ -243,12 +249,15 @@ def _connect_locked(self) -> WebSocketClient:
243249
"""Return the open client, or open one. The caller holds the lock."""
244250
if self._client is not None:
245251
return self._client
246-
address = discovery.find_viewer(
247-
port=self._port,
248-
token=self._token,
249-
host=self._host,
250-
settings=self._settings_dir,
251-
)
252+
if self._resolve is not None:
253+
address = self._resolve()
254+
else:
255+
address = discovery.find_viewer(
256+
port=self._port,
257+
token=self._token,
258+
host=self._host,
259+
settings=self._settings_dir,
260+
)
252261
self._client = WebSocketClient.connect(
253262
address.host, address.port, token=address.token, timeout=self._timeout, path="/tool"
254263
)
@@ -312,7 +321,14 @@ def _read_locked(
312321
progress(float(fraction))
313322
continue
314323
if on_frame is not None:
315-
on_frame(frame)
324+
try:
325+
on_frame(frame)
326+
except BaseException:
327+
# The viewer can continue to send the frames of this request. Close
328+
# the connection so that they stop and a later request does not
329+
# read them.
330+
self._drop_locked()
331+
raise
316332
if kind in until:
317333
return frame
318334

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""The version of loop_link, read from the installed package metadata."""
2+
3+
# The version is set in pyproject.toml. Do not write a version string here:
4+
# release-please changes each "__version__ = ..." string in every version.py
5+
# file in the repository to the version of the release it makes.
6+
from importlib.metadata import PackageNotFoundError, version
7+
8+
try:
9+
__version__ = version("loop-link")
10+
except PackageNotFoundError:
11+
# the package is imported from source and is not installed
12+
__version__ = "unknown"

‎packages/loop_link/src/loop_link/viewer.py‎

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
BusyError,
3232
InvalidInputError,
3333
MissingFeatureError,
34+
NoViewerError,
3435
Refusal,
3536
ViewerClosedError,
3637
ViewerRefusedError,
@@ -116,7 +117,12 @@ def __init__(
116117
self._shared: dict[str, Any] = {"client": None, "stop": self._stop}
117118
weakref.finalize(self, _release_shared, self._shared)
118119
self._thread: threading.Thread | None = None
120+
# _dead: the data connection failed. A later send opens a new connection.
121+
# _closed: close() was called. Only connect() opens the viewer again.
119122
self._dead = False
123+
self._closed = False
124+
self._address_lock = threading.Lock()
125+
self._address: discovery.ViewerAddress | None = None
120126
self._last_send = 0.0
121127
self._seq = 0
122128
self._tool_lock = threading.Lock()
@@ -157,7 +163,7 @@ def connected(self) -> bool:
157163
def tool_connected(self) -> bool:
158164
"""True while the tool connection is open."""
159165
tool = self._tool_connection
160-
return tool is not None and tool.connected and not self._dead
166+
return tool is not None and tool.connected and not self._closed
161167

162168
# -- context manager ------------------------------------------------------
163169

@@ -204,17 +210,21 @@ def connect(self) -> Viewer:
204210

205211
if self.connected:
206212
return self
213+
self._closed = False
207214
self._stop_thread()
208215
self._drop_client()
209-
address = discovery.find_viewer(
210-
port=self._port,
211-
token=self._token,
212-
host=self._host,
213-
settings=self._settings_dir,
214-
)
215-
client = WebSocketClient.connect(
216-
address.host, address.port, token=address.token, timeout=self._timeout, path="/"
217-
)
216+
address = self._resolve_address()
217+
try:
218+
client = WebSocketClient.connect(
219+
address.host, address.port, token=address.token, timeout=self._timeout, path="/"
220+
)
221+
except NoViewerError:
222+
# The viewer at the known address is gone. Search again.
223+
self._forget_address()
224+
address = self._resolve_address()
225+
client = WebSocketClient.connect(
226+
address.host, address.port, token=address.token, timeout=self._timeout, path="/"
227+
)
218228
try:
219229
_read_early(client)
220230
client.send_binary(encode.hello_frame(self._crs, self._proposed_origin, __version__))
@@ -1168,7 +1178,8 @@ def release_data(self) -> None:
11681178
"""Close the connection so another client can own the scene.
11691179
11701180
A later ``send`` opens a new connection. The revision numbers stay. The
1171-
tool connection stays open.
1181+
tool connection stays open. A later ``send`` also opens a new connection
1182+
after the connection fails.
11721183
"""
11731184
self._stop_thread()
11741185
self._drop_client()
@@ -1179,38 +1190,68 @@ def close(self) -> None:
11791190
for watcher in watchers:
11801191
watcher.stop()
11811192
with self._tool_lock:
1182-
self._dead = True
1193+
self._closed = True
11831194
tool, self._tool_connection = self._tool_connection, None
11841195
self._colour_map_names = None
11851196
if tool is not None:
11861197
tool.close()
11871198
self._stop_thread()
11881199
self._drop_client()
11891200
self._dead = True
1201+
with self._address_lock:
1202+
self._address = None
11901203

11911204
# -- helpers --------------------------------------------------------------
11921205

11931206
def _tool(self) -> ToolConnection:
11941207
"""Return the tool connection. Make it at the first call."""
1195-
if self._dead:
1208+
if self._closed:
11961209
raise ViewerClosedError
11971210
with self._tool_lock:
1198-
if self._dead:
1211+
if self._closed:
11991212
raise ViewerClosedError
12001213
if self._tool_connection is None:
12011214
self._tool_connection = ToolConnection(
1215+
timeout=self._timeout, resolve=self._resolve_address
1216+
)
1217+
return self._tool_connection
1218+
1219+
def _resolve_address(self) -> discovery.ViewerAddress:
1220+
"""Return the address of the viewer.
1221+
1222+
While the data connection or the tool connection is open, the known address
1223+
is used again, so that both connections go to the same viewer. Otherwise the
1224+
viewer is found again.
1225+
"""
1226+
with self._address_lock:
1227+
tool = self._tool_connection
1228+
in_use = (self._client is not None and not self._dead) or (
1229+
tool is not None and tool.connected
1230+
)
1231+
if self._address is None or not in_use:
1232+
self._address = discovery.find_viewer(
12021233
port=self._port,
12031234
token=self._token,
12041235
host=self._host,
1205-
settings_dir=self._settings_dir,
1206-
timeout=self._timeout,
1236+
settings=self._settings_dir,
12071237
)
1208-
return self._tool_connection
1238+
return self._address
1239+
1240+
def _forget_address(self) -> None:
1241+
"""Forget the known address and close the tool connection that uses it."""
1242+
with self._tool_lock:
1243+
tool = self._tool_connection
1244+
self._colour_map_names = None
1245+
if tool is not None:
1246+
tool.close()
1247+
with self._address_lock:
1248+
self._address = None
12091249

12101250
def _ensure_connected(self) -> None:
1211-
if self._dead:
1251+
if self._closed:
12121252
raise ViewerClosedError
1213-
if self._client is None:
1253+
if self._client is None or self._dead:
1254+
# No connection yet, or the last one failed: open a new one.
12141255
self.connect()
12151256

12161257
def _drop_client(self) -> None:

‎packages/loop_link/tests/test_adapter_loopstructural.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,22 @@ def test_send_other_loop_common_class_is_refused(
156156
viewer = viewer_factory()
157157
with pytest.raises(InvalidInputError, match="Accepted inputs:"):
158158
viewer.send(points, id="pts")
159+
160+
161+
def test_grid_properties_are_flattened_in_x_fastest_order() -> None:
162+
"""A property of shape (nx, ny, nz) is flattened with order='F'; a vector is split."""
163+
nsteps = np.array([3, 4, 5])
164+
values = np.arange(60, dtype=float).reshape(nsteps, order="F")
165+
vector = np.arange(180, dtype=float).reshape(60, 3)
166+
grid = geometry.StructuredGrid(
167+
origin=[0, 0, 0],
168+
step_vector=[1, 1, 1],
169+
nsteps=nsteps,
170+
properties={"v": values, "column": values.reshape(-1, 1, order="F"), "g": vector},
171+
)
172+
result = from_loopstructural(grid)
173+
np.testing.assert_array_equal(result.properties["v"], np.arange(60, dtype=float))
174+
np.testing.assert_array_equal(result.properties["column"], np.arange(60, dtype=float))
175+
assert "g" not in result.properties
176+
for column in range(3):
177+
np.testing.assert_array_equal(result.properties[f"g_{column}"], vector[:, column])

0 commit comments

Comments
 (0)