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

automatic attribute length adjustment #584

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
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
29 changes: 22 additions & 7 deletions src/graphnet/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ def predict_as_dataframe(
dataloader: DataLoader,
prediction_columns: List[str],
*,
node_level: bool = False,
additional_attributes: Optional[List[str]] = None,
index_column: str = "event_no",
gpus: Optional[Union[List[int], int]] = None,
distribution_strategy: Optional[str] = "auto",
) -> pd.DataFrame:
Expand Down Expand Up @@ -231,12 +229,29 @@ def predict_as_dataframe(
)
for batch in dataloader:
for attr in attributes:
attribute = batch[attr].detach().cpu().numpy()
if node_level:
if attr == index_column:
attribute = np.repeat(
attribute, batch.n_pulses.detach().cpu().numpy()
attribute = batch[attr]
if isinstance(attribute, torch.Tensor):
attribute = attribute.detach().cpu().numpy()

# Check if node level predictions
# If true, additional attributes are repeated
# to make dimensions fit
if len(attribute) < np.sum(
batch.n_pulses.detach().cpu().numpy()
):
attribute = np.repeat(
attribute, batch.n_pulses.detach().cpu().numpy()
)
try:
assert len(attribute) == len(batch.x)
except AssertionError:
self.warning_once(
"Could not automatically adjust length"
f"of additional attribute {attr} to match length of"
f"predictions. Make sure {attr} is a graph-level or"
"node-level attribute. Attribute skipped."
)
pass
attributes[attr].extend(attribute)

data = np.concatenate(
Expand Down
4 changes: 0 additions & 4 deletions src/graphnet/models/standard_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ def predict_as_dataframe(
dataloader: DataLoader,
prediction_columns: Optional[List[str]] = None,
*,
node_level: bool = False,
additional_attributes: Optional[List[str]] = None,
index_column: str = "event_no",
gpus: Optional[Union[List[int], int]] = None,
distribution_strategy: Optional[str] = "auto",
) -> pd.DataFrame:
Expand All @@ -207,9 +205,7 @@ def predict_as_dataframe(
return super().predict_as_dataframe(
dataloader=dataloader,
prediction_columns=prediction_columns,
node_level=node_level,
additional_attributes=additional_attributes,
index_column=index_column,
gpus=gpus,
distribution_strategy=distribution_strategy,
)