-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a temporary dataset class (#10)
- Loading branch information
1 parent
5b18376
commit bc8437f
Showing
11 changed files
with
1,303 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .dataset import Dataset, collate_fn # noqa: F401 | ||
from .readers import read_structures, read_targets # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import Dict, List | ||
|
||
import metatensor.torch | ||
import rascaline.torch | ||
import torch | ||
from metatensor.torch import Labels, TensorMap | ||
|
||
|
||
class Dataset(torch.utils.data.Dataset): | ||
def __init__( | ||
self, structures: List[rascaline.torch.System], targets: Dict[str, TensorMap] | ||
): | ||
"""Creates a dataset from a list of `rascaline.torch.System`s and | ||
a list of dictionaries of `TensorMap`s.""" | ||
|
||
for tensor_map in targets.values(): | ||
n_structures = ( | ||
torch.max(tensor_map.block(0).samples["structure"]).item() + 1 | ||
) | ||
if n_structures != len(structures): | ||
raise ValueError( | ||
f"Number of structures in input ({len(structures)}) and " | ||
f"output ({n_structures}) must be the same" | ||
) | ||
|
||
self.structures = structures | ||
self.targets = targets | ||
|
||
def __len__(self): | ||
""" | ||
Return the total number of samples in the dataset. | ||
""" | ||
return len(self.structures) | ||
|
||
def __getitem__(self, index): | ||
""" | ||
Generates one sample of data. | ||
Args: | ||
index: The index of the item in the dataset. | ||
Returns: | ||
A tuple containing the structure and targets for the given index. | ||
""" | ||
structure = self.structures[index] | ||
|
||
structure_index_samples = Labels( | ||
names=["structure"], | ||
values=torch.tensor([[index]]), # must be a 2D-array | ||
) | ||
|
||
targets = {} | ||
for name, tensor_map in self.targets.items(): | ||
targets[name] = metatensor.torch.slice( | ||
tensor_map, "samples", structure_index_samples | ||
) | ||
|
||
return structure, targets | ||
|
||
|
||
def collate_fn(batch): | ||
""" | ||
Creates a batch from a list of samples. | ||
Args: | ||
batch: A list of samples, where each sample is a tuple containing a | ||
structure and targets. | ||
Returns: | ||
A tuple containing the structures and targets for the batch. | ||
""" | ||
|
||
structures = [sample[0] for sample in batch] | ||
targets = {} | ||
for name in batch[0][1].keys(): | ||
targets[name] = metatensor.torch.join( | ||
[sample[1][name] for sample in batch], "samples" | ||
) | ||
|
||
return structures, targets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
import torch | ||
|
||
from metatensor_models.utils.data import Dataset, collate_fn, read_structures, read_targets | ||
|
||
|
||
def test_dataset(): | ||
"""Tests the readers and the dataset class.""" | ||
|
||
dataset_path = os.path.join(os.path.dirname(__file__), "data/qm9_reduced_100.xyz") | ||
|
||
structures = read_structures(dataset_path) | ||
targets = read_targets(dataset_path, "U0") | ||
|
||
dataset = Dataset(structures, targets) | ||
dataloader = torch.utils.data.DataLoader(dataset, batch_size=10, collate_fn=collate_fn) | ||
|
||
for batch in dataloader: | ||
assert batch[1]["U0"].block().values.shape == (10, 1) |
Large diffs are not rendered by default.
Oops, something went wrong.