Skip to content

Commit 3c1eb3f

Browse files
Restore part-based truth point selection
1 parent d9324a3 commit 3c1eb3f

4 files changed

Lines changed: 18 additions & 86 deletions

File tree

src/spine/model/full_chain.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,7 @@ def prepare_grappa_input(
12651265
grappa_input["data"] = data
12661266
grappa_input["clusts"] = clusts
12671267
grappa_input["shapes"] = clust_shapes
1268+
12681269
# Get the particle end points, if requested
12691270
if hasattr(model.node_encoder, "add_points") and model.node_encoder.add_points:
12701271
# Fetch the cluster list to use to get points
@@ -1289,12 +1290,10 @@ def prepare_grappa_input(
12891290
"Must provide `clust_label` with `coord_label` to add "
12901291
"label points to the GrapPA input."
12911292
)
1292-
point_clusts = clusts if point_use_primaries else ref_clusts
12931293
points = get_cluster_points_label_batch(
12941294
clust_label,
12951295
coord_label,
1296-
point_clusts,
1297-
use_group=point_use_primaries,
1296+
ref_clusts,
12981297
)
12991298

13001299
grappa_input["points"] = points

src/spine/utils/gnn/cluster.py

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ def get_cluster_closest_primary_label_batch(data, coord_label, clusts, primary_i
171171
return TensorBatch(labels, clusts.counts)
172172

173173

174-
def get_cluster_points_label_batch(
175-
data, coord_label, clusts, random_order=True, use_group=False
176-
):
174+
def get_cluster_points_label_batch(data, coord_label, clusts, random_order=True):
177175
"""Batched version of :func:`get_cluster_points_label`
178176
179177
Parameters
@@ -187,11 +185,6 @@ def get_cluster_points_label_batch(
187185
random_order : bool, default True
188186
If `True`, randomize the order in which the start en end points of
189187
a track are stored in the output
190-
use_group : bool, default False
191-
If `True`, use the true group ID to fetch the particle end point
192-
labels. Otherwise, use particle IDs and select the earliest particle
193-
in time.
194-
195188
Returns
196189
-------
197190
np.ndarray
@@ -210,7 +203,6 @@ def get_cluster_points_label_batch(
210203
coord_label[b],
211204
clusts[b],
212205
random_order,
213-
use_group,
214206
)
215207

216208
return TensorBatch(points, clusts.counts, coord_cols=points.shape[1])
@@ -982,9 +974,7 @@ def _get_cluster_features_extended(
982974
keep_torch=True,
983975
ref_arg="data",
984976
)
985-
def get_cluster_points_label(
986-
data, coord_label, clusts, random_order=True, use_group=False
987-
):
977+
def get_cluster_points_label(data, coord_label, clusts, random_order=True):
988978
"""Gets label points for each cluster.
989979
990980
Returns start point of primary shower fragment twice if shower, delta or
@@ -1002,11 +992,6 @@ def get_cluster_points_label(
1002992
random_order : bool, default True
1003993
If `True`, randomize the order in which the start en end points of
1004994
a track are stored in the output
1005-
use_group : bool, default False
1006-
If `True`, use the true group ID to fetch the particle end point
1007-
labels. Otherwise, use particle IDs and select the earliest particle
1008-
in time.
1009-
1010995
Returns
1011996
-------
1012997
np.ndarray
@@ -1015,7 +1000,7 @@ def get_cluster_points_label(
10151000
if len(clusts) == 0:
10161001
return np.empty((0, 6), dtype=data.dtype)
10171002

1018-
return _get_cluster_points_label(data, coord_label, clusts, random_order, use_group)
1003+
return _get_cluster_points_label(data, coord_label, clusts, random_order)
10191004

10201005

10211006
@nb.njit(cache=True)
@@ -1024,28 +1009,22 @@ def _get_cluster_points_label(
10241009
coord_label: nb.float64[:, :],
10251010
clusts: nb.types.List(nb.int64[:]),
10261011
random_order: nb.boolean = True,
1027-
use_group: nb.boolean = False,
10281012
) -> nb.float64[:, :]:
10291013

10301014
# Get start and end points (one and the same for all but track class)
10311015
points = np.empty((len(clusts), 6), dtype=data.dtype)
10321016
for i, c in enumerate(clusts):
1033-
if use_group:
1034-
# Use the true aggregate particle identity directly.
1035-
label_ids = np.unique(data[c, GROUP_COL]).astype(np.int64)
1036-
label_id = label_ids[0]
1037-
else:
1038-
# Use the first constituent particle in time.
1039-
part_ids = np.unique(data[c, PART_COL]).astype(np.int64)
1040-
label_id = -1
1041-
min_time = np.inf
1042-
for part_id in part_ids:
1043-
if part_id < 0 or part_id >= len(coord_label):
1044-
raise IndexError("Invalid label index for coord_label.")
1045-
time = coord_label[part_id, COORD_TIME_COL]
1046-
if time < min_time:
1047-
min_time = time
1048-
label_id = part_id
1017+
# Use the first constituent particle in time.
1018+
part_ids = np.unique(data[c, PART_COL]).astype(np.int64)
1019+
label_id = -1
1020+
min_time = np.inf
1021+
for part_id in part_ids:
1022+
if part_id < 0 or part_id >= len(coord_label):
1023+
raise IndexError("Invalid label index for coord_label.")
1024+
time = coord_label[part_id, COORD_TIME_COL]
1025+
if time < min_time:
1026+
min_time = time
1027+
label_id = part_id
10491028

10501029
if label_id < 0 or label_id >= len(coord_label):
10511030
raise IndexError("Invalid label index for coord_label.")

test/test_model/test_full_chain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def fake_label_points(data_arg, coord_label_arg, clusts_arg, **kwargs):
226226
assert grappa_input["points"] is label_points
227227
assert "coord_label" not in grappa_input
228228
assert calls == [
229-
(clust_label, coord_label, clusts, {"use_group": True}),
229+
(clust_label, coord_label, primaries, {}),
230230
]
231231

232232

test/test_utils/test_gnn_cluster.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from spine.constants import GROUP_COL, PART_COL
6+
from spine.constants import PART_COL
77
from spine.utils.gnn.cluster import (
88
cluster_dedx,
99
get_cluster_features_base,
@@ -50,37 +50,6 @@ def test_cluster_features_base_accepts_indexed_float32_coordinates():
5050
np.testing.assert_allclose(feats[1, :3], [1.0 / 3.0, 0.0, 5.0])
5151

5252

53-
def test_cluster_points_label_can_use_group_identity():
54-
data = np.zeros((2, GROUP_COL + 1), dtype=np.float32)
55-
data[:, 1:4] = np.array(
56-
[
57-
[10.0, 0.0, 0.0],
58-
[20.0, 0.0, 0.0],
59-
],
60-
dtype=np.float32,
61-
)
62-
data[:, PART_COL] = [0, 1]
63-
data[:, GROUP_COL] = 1
64-
coord_label = np.array(
65-
[
66-
[0.0, 10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0],
67-
[0.0, 20.0, 0.0, 0.0, 20.0, 0.0, 0.0, 5.0, 0.0],
68-
],
69-
dtype=np.float32,
70-
)
71-
72-
points = get_cluster_points_label(
73-
data,
74-
coord_label,
75-
[np.array([0, 1], dtype=np.int64)],
76-
random_order=False,
77-
use_group=True,
78-
)
79-
80-
np.testing.assert_allclose(points[0, :3], [20.0, 0.0, 0.0])
81-
np.testing.assert_allclose(points[0, 3:], [20.0, 0.0, 0.0])
82-
83-
8453
def test_cluster_points_label_rejects_invalid_particle_id():
8554
data = np.zeros((1, PART_COL + 1), dtype=np.float32)
8655
data[:, PART_COL] = 1
@@ -93,18 +62,3 @@ def test_cluster_points_label_rejects_invalid_particle_id():
9362
[np.array([0], dtype=np.int64)],
9463
random_order=False,
9564
)
96-
97-
98-
def test_cluster_points_label_rejects_invalid_group_id():
99-
data = np.zeros((1, GROUP_COL + 1), dtype=np.float32)
100-
data[:, GROUP_COL] = 1
101-
coord_label = np.zeros((1, 9), dtype=np.float32)
102-
103-
with pytest.raises(IndexError, match="Invalid label index"):
104-
get_cluster_points_label(
105-
data,
106-
coord_label,
107-
[np.array([0], dtype=np.int64)],
108-
random_order=False,
109-
use_group=True,
110-
)

0 commit comments

Comments
 (0)