Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional replacing of feature mapping with identity #769

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/graphnet/models/detector/detector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Base detector-specific `Model` class(es)."""

from abc import abstractmethod
from typing import Dict, Callable, List
from typing import Dict, Callable, List, Optional

from torch_geometric.data import Data
import torch
Expand All @@ -14,10 +14,19 @@
class Detector(Model):
"""Base class for all detector-specific read-ins in graphnet."""

def __init__(self) -> None:
"""Construct `Detector`."""
def __init__(
self, replace_with_identity: Optional[List[str]] = None
) -> None:
"""Construct `Detector`.

Args:
replace_with_identity: A list of feature names from the
feature_map that should be replaced with the identity
function.
"""
# Base class constructor
super().__init__(name=__name__, class_name=self.__class__.__name__)
self._replace_with_identity = replace_with_identity

@abstractmethod
def feature_map(self) -> Dict[str, Callable]:
Expand Down Expand Up @@ -64,9 +73,13 @@ def sensor_index_name(self) -> str:
def _standardize(
self, input_features: torch.tensor, input_feature_names: List[str]
) -> Data:
feature_map = self.feature_map()
if self._replace_with_identity is not None:
for feature in self._replace_with_identity:
feature_map[feature] = self._identity
for idx, feature in enumerate(input_feature_names):
try:
input_features[:, idx] = self.feature_map()[
input_features[:, idx] = feature_map[
feature
]( # noqa: E501 # type: ignore
input_features[:, idx]
Expand Down
Loading