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

Allow users to modify dataset summary table format #9408

Merged
merged 4 commits into from
Jun 14, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Changed

- Added the `fmt` arg to `Dataset.get_summary` ([#9408](https://github.com/pyg-team/pytorch_geometric/pull/9408))
- Skipped zero atom molecules in `MoleculeNet` ([#9318](https://github.com/pyg-team/pytorch_geometric/pull/9318))
- Ensure proper parallelism in `OnDiskDataset` for multi-threaded `get` calls ([#9140](https://github.com/pyg-team/pytorch_geometric/pull/9140))
- Allow `None` outputs in `FeatureStore.get_tensor()` - `KeyError` should now be raised based on the implementation in `FeatureStore._get_tensor()` ([#9102](https://github.com/pyg-team/pytorch_geometric/pull/9102))
Expand Down
12 changes: 9 additions & 3 deletions torch_geometric/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,15 @@ def get_summary(self) -> Any:
from torch_geometric.data.summary import Summary
return Summary.from_dataset(self)

def print_summary(self) -> None:
r"""Prints summary statistics of the dataset to the console."""
print(str(self.get_summary()))
def print_summary(self, fmt: str = "psql") -> None:
r"""Prints summary statistics of the dataset to the console.

Args:
fmt (str, optional): Summary tables format. Available table formats
can be found `here <https://github.com/astanin/python-tabulate?
tab=readme-ov-file#table-format>`__. (default: :obj:`"psql"`)
"""
print(self.get_summary().format(fmt=fmt))

def to_datapipe(self) -> Any:
r"""Converts the dataset into a :class:`torch.utils.data.DataPipe`.
Expand Down
18 changes: 14 additions & 4 deletions torch_geometric/data/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ def from_dataset(
num_edges_per_type=num_edges_per_type,
)

def __repr__(self) -> str:
def format(self, fmt: str = "psql") -> str:
r"""Formats summary statistics of the dataset.

Args:
fmt (str, optional): Summary tables format. Available table formats
can be found `here <https://github.com/astanin/python-tabulate?
tab=readme-ov-file#table-format>`__. (default: :obj:`"psql"`)
"""
from tabulate import tabulate

body = f'{self.name} (#graphs={self.num_graphs}):\n'
Expand All @@ -127,7 +134,7 @@ def __repr__(self) -> str:
for field in Stats.__dataclass_fields__:
row = [field] + [f'{getattr(s, field):.1f}' for s in stats]
content.append(row)
body += tabulate(content, headers='firstrow', tablefmt='psql')
body += tabulate(content, headers='firstrow', tablefmt=fmt)

if self.num_nodes_per_type is not None:
content = [['']]
Expand All @@ -140,7 +147,7 @@ def __repr__(self) -> str:
]
content.append(row)
body += "\nNumber of nodes per node type:\n"
body += tabulate(content, headers='firstrow', tablefmt='psql')
body += tabulate(content, headers='firstrow', tablefmt=fmt)

if self.num_edges_per_type is not None:
content = [['']]
Expand All @@ -156,6 +163,9 @@ def __repr__(self) -> str:
]
content.append(row)
body += "\nNumber of edges per edge type:\n"
body += tabulate(content, headers='firstrow', tablefmt='psql')
body += tabulate(content, headers='firstrow', tablefmt=fmt)

return body

def __repr__(self) -> str:
return self.format()
Loading