Skip to content

Commit eaff564

Browse files
Configure label point order in GrapPA encoder
1 parent 3c1eb3f commit eaff564

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/spine/model/full_chain.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ def prepare_grappa_input(
12941294
clust_label,
12951295
coord_label,
12961296
ref_clusts,
1297+
random_order=model.node_encoder.random_order,
12971298
)
12981299

12991300
grappa_input["points"] = points

src/spine/model/layer/gnn/encode/geometric.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(
5656
add_value=False,
5757
add_shape=False,
5858
add_points=False,
59+
random_order=True,
5960
add_local_dirs=False,
6061
dir_max_dist=5.0,
6162
add_local_dedxs=False,
@@ -73,6 +74,9 @@ def __init__(
7374
Add the particle semantic type
7475
add_points : bool, default False
7576
Add the start/end points of the particles
77+
random_order : bool, default True
78+
If `True`, randomize the order of the start/end points fetched
79+
from labels
7680
add_local_dirs : bool, default False
7781
Add the local direction estimates at the start and end points
7882
dir_max_dist : float, default 5.
@@ -90,6 +94,7 @@ def __init__(
9094
self.add_value = add_value
9195
self.add_shape = add_shape
9296
self.add_points = add_points
97+
self.random_order = random_order
9398
self.add_local_dirs = add_local_dirs
9499
self.dir_max_dist = dir_max_dist
95100
self.add_local_dedxs = add_local_dedxs
@@ -175,7 +180,9 @@ def forward(
175180
# Add the points
176181
if self.add_points:
177182
if points is None:
178-
points = get_cluster_points_label_batch(data, coord_label, clusts)
183+
points = get_cluster_points_label_batch(
184+
data, coord_label, clusts, random_order=self.random_order
185+
)
179186

180187
feats = torch.cat((feats, points.tensor), dim=1)
181188

test/test_model/test_full_chain.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import torch
66

77
import spine.model.full_chain as full_chain_mod
8+
import spine.model.layer.gnn.encode.geometric as geometric_mod
89
from spine.constants import (
910
CLUST_COL,
1011
GHOST_SHP,
@@ -16,6 +17,7 @@
1617
)
1718
from spine.data import IndexBatch, TensorBatch
1819
from spine.model.full_chain import FullChain, FullChainLoss
20+
from spine.model.layer.gnn.encode.geometric import ClustGeoNodeEncoder
1921

2022

2123
class RecordingLoss:
@@ -182,7 +184,9 @@ def test_prepare_grappa_input_uses_label_points_without_ppn(monkeypatch):
182184
full_chain = object.__new__(FullChain)
183185
full_chain.result = {}
184186

185-
model = SimpleNamespace(node_encoder=SimpleNamespace(add_points=True))
187+
model = SimpleNamespace(
188+
node_encoder=SimpleNamespace(add_points=True, random_order=False)
189+
)
186190
data = TensorBatch(np.zeros((3, 4), dtype=np.float32), counts=np.array([3]))
187191
clust_label = TensorBatch(
188192
np.zeros((3, GROUP_COL + 1), dtype=np.float32), counts=np.array([3])
@@ -226,15 +230,50 @@ def fake_label_points(data_arg, coord_label_arg, clusts_arg, **kwargs):
226230
assert grappa_input["points"] is label_points
227231
assert "coord_label" not in grappa_input
228232
assert calls == [
229-
(clust_label, coord_label, primaries, {}),
233+
(clust_label, coord_label, primaries, {"random_order": False}),
234+
]
235+
236+
237+
def test_geo_node_encoder_forwards_random_order(monkeypatch):
238+
encoder = ClustGeoNodeEncoder(use_numpy=False, add_points=True, random_order=False)
239+
data = TensorBatch(torch.zeros((1, 5), dtype=torch.float32), counts=np.array([1]))
240+
clusts = IndexBatch(
241+
[np.array([0], dtype=np.int64)],
242+
spans=np.array([1]),
243+
counts=np.array([1]),
244+
single_counts=np.array([1]),
245+
)
246+
coord_label = TensorBatch(
247+
torch.zeros((1, 9), dtype=torch.float32), counts=np.array([1])
248+
)
249+
label_points = TensorBatch(
250+
torch.ones((1, 6), dtype=torch.float32), counts=np.array([1])
251+
)
252+
calls = []
253+
254+
def fake_label_points(data_arg, coord_label_arg, clusts_arg, **kwargs):
255+
calls.append((data_arg, coord_label_arg, clusts_arg, kwargs))
256+
return label_points
257+
258+
monkeypatch.setattr(
259+
geometric_mod, "get_cluster_points_label_batch", fake_label_points
260+
)
261+
262+
_, points = encoder(data, clusts, coord_label=coord_label)
263+
264+
assert points is label_points
265+
assert calls == [
266+
(data, coord_label, clusts, {"random_order": False}),
230267
]
231268

232269

233270
def test_prepare_grappa_input_requires_clust_label_for_label_points():
234271
full_chain = object.__new__(FullChain)
235272
full_chain.result = {}
236273

237-
model = SimpleNamespace(node_encoder=SimpleNamespace(add_points=True))
274+
model = SimpleNamespace(
275+
node_encoder=SimpleNamespace(add_points=True, random_order=True)
276+
)
238277
data = TensorBatch(np.zeros((1, 4), dtype=np.float32), counts=np.array([1]))
239278
clusts = IndexBatch(
240279
[np.array([0], dtype=np.int64)],

0 commit comments

Comments
 (0)