diff --git a/src/graphnet/models/data_representation/__init__.py b/src/graphnet/models/data_representation/__init__.py index 84dd64331..52c5502e8 100644 --- a/src/graphnet/models/data_representation/__init__.py +++ b/src/graphnet/models/data_representation/__init__.py @@ -17,4 +17,5 @@ PercentileClusters, NodeAsDOMTimeSeries, IceMixNodes, + ClusterSummaryFeatures, ) diff --git a/src/graphnet/models/data_representation/data_representation.py b/src/graphnet/models/data_representation/data_representation.py index 09cd43293..40015288f 100644 --- a/src/graphnet/models/data_representation/data_representation.py +++ b/src/graphnet/models/data_representation/data_representation.py @@ -202,6 +202,25 @@ def forward( # type: ignore # Add data representation Stamp data["data_representation"] = self.__class__.__name__ + + # Warn on empty events; the representation will hold no nodes/pulses, + # which is rarely what the user intends. + if data.x.shape[0] == 0: + event_no = getattr(data, "event_no", None) + if isinstance(event_no, torch.Tensor): + event_no = event_no.tolist() + msg = ( + f"{self.__class__.__name__} received an empty input tensor " + f"(no pulses) for event_no={event_no}" + ) + if data_path is not None: + msg += f" in dataset '{data_path}'" + msg += ( + ". This event will be turned into an empty data " + "representation; check your input data if this is unexpected." + ) + self.warning(msg) + return data def _resolve_masks(self) -> None: diff --git a/src/graphnet/models/data_representation/graphs/nodes/nodes.py b/src/graphnet/models/data_representation/graphs/nodes/nodes.py index 8fa4b74cb..0e0b216f6 100644 --- a/src/graphnet/models/data_representation/graphs/nodes/nodes.py +++ b/src/graphnet/models/data_representation/graphs/nodes/nodes.py @@ -1,22 +1,20 @@ """Class(es) for building/connecting graphs.""" -from typing import List, Tuple, Optional, Dict, Union from abc import abstractmethod +from copy import deepcopy +from typing import Dict, List, Optional, Tuple, Union +import numpy as np import torch -from torch_geometric.data import Data -from graphnet.utilities.decorators import final from graphnet.models import Model from graphnet.models.data_representation.graphs.utils import ( cluster_and_pad, + ice_transparency, identify_indices, lex_sort, - ice_transparency, ) -from copy import deepcopy - -import numpy as np +from graphnet.utilities.decorators import final class NodeDefinition(Model): # pylint: disable=too-few-public-methods @@ -34,7 +32,7 @@ def __init__( ) @final - def forward(self, x: torch.tensor) -> torch.tensor: + def forward(self, x: torch.Tensor) -> torch.Tensor: """Construct nodes from raw node features. Args: @@ -43,11 +41,9 @@ def forward(self, x: torch.tensor) -> torch.tensor: node_feature_names: list of names for each column in ´x´. Returns: - graph: a graph without edges + Node feature tensor of shape ´[num_nodes, num_features]´. """ - data = self._construct_nodes(x=x) - - return data + return self._construct_nodes(x=x) @property def _output_feature_names(self) -> List[str]: @@ -109,7 +105,7 @@ def _define_output_feature_names( """ @abstractmethod - def _construct_nodes(self, x: torch.tensor) -> torch.tensor: + def _construct_nodes(self, x: torch.Tensor) -> torch.Tensor: """Construct nodes from raw node features ´x´. Args: @@ -119,8 +115,7 @@ def _construct_nodes(self, x: torch.tensor) -> torch.tensor: order of appearance. Length `d`. Returns: - graph: graph without edges. - new_node_features: A list of node features names. + Node feature tensor of shape ´[num_nodes, num_features]´. """ @@ -269,10 +264,16 @@ def _define_output_feature_names( def _construct_nodes(self, x: torch.Tensor) -> torch.Tensor: """Construct nodes from raw node features ´x´.""" + if x.shape[0] == 0: + n_features = len(self._keys) + ( + 1 if self._charge_index is None else 0 + ) + # `new_node_col` appended below for non-empty events + n_features += 1 + return torch.empty((0, n_features), dtype=x.dtype, device=x.device) + # Cast to Numpy x = x.numpy() - if x.shape[0] == 0: - return Data(x=torch.tensor(np.column_stack([x, []]))) # if there is no charge column add a dummy column # of zeros with the same shape as the time column if self._charge_index is None: @@ -495,9 +496,13 @@ class ClusterSummaryFeatures(NodeDefinition): - number of pulses per clusters feature name: `counts` - For more details on some of the features see - Theo Glauchs thesis (chapter 5.3): + For more details on most of the listed features see + Theo Glauch's thesis (chapter 5.3): https://mediatum.ub.tum.de/node?id=1584755 + + NOTE: The `counts` feature (number of pulses per cluster) is an + addition introduced in this implementation and is not part of + the feature set described in the referenced thesis. """ def __init__( @@ -602,7 +607,7 @@ def _define_output_feature_names( new_feature_names.append("counts") return new_feature_names - def _construct_nodes(self, x: torch.Tensor) -> Data: + def _construct_nodes(self, x: torch.Tensor) -> torch.Tensor: """Construct nodes from raw node features ´x´.""" # Cast to Numpy x = x.numpy() @@ -720,9 +725,7 @@ def _standardize_features( f"but got {standardization}" ) - def _verify_standardization( - self, - ) -> torch.Tensor: + def _verify_standardization(self) -> None: """Verify settings of standardization of the features.""" if not isinstance(self._charge_standardization, float): if isinstance(self._charge_standardization, str):