Skip to content
Open
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
18 changes: 15 additions & 3 deletions chgnet/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def get_train_val_test_loader(
batch_size=32,
num_workers=0,
pin_memory=True,
seed: int | None = None,
) -> tuple[DataLoader, DataLoader, DataLoader]:
"""Partition the GraphData using materials id,
randomly select the train_keys, val_keys, test_keys by train val test ratio,
Expand All @@ -465,14 +466,17 @@ def get_train_val_test_loader(
Default = 0
pin_memory (bool): Whether to pin the memory of the data loaders
Default: True
seed (int, optional): seed for the random train/val/test split
Default = None (use the global random state)

Returns:
train_loader, val_loader, test_loader
"""
train_labels, val_labels, test_labels = {}, {}, {}
if train_key is None:
mp_ids = list(self.labels)
random.shuffle(mp_ids)
rng = random if seed is None else random.Random(seed)
rng.shuffle(mp_ids)
n_train = int(train_ratio * len(mp_ids))
n_val = int(val_ratio * len(mp_ids))
train_key = mp_ids[:n_train]
Expand Down Expand Up @@ -673,6 +677,7 @@ def get_train_val_test_loader(
batch_size=32,
num_workers=0,
pin_memory=True,
seed: int | None = None,
) -> tuple[DataLoader, DataLoader, DataLoader]:
"""Partition the Dataset using materials id,
randomly select the train_keys, val_keys, test_keys by train val test ratio,
Expand All @@ -694,14 +699,17 @@ def get_train_val_test_loader(
Default = 0
pin_memory (bool): Whether to pin the memory of the data loaders
Default: True
seed (int, optional): seed for the random train/val/test split
Default = None (use the global random state)

Returns:
train_loader, val_loader, test_loader
"""
train_data, val_data, test_data = {}, {}, {}
if train_key is None:
mp_ids = list(self.data)
random.shuffle(mp_ids)
rng = random if seed is None else random.Random(seed)
rng.shuffle(mp_ids)
n_train = int(train_ratio * len(mp_ids))
n_val = int(val_ratio * len(mp_ids))
train_key = mp_ids[:n_train]
Expand Down Expand Up @@ -797,6 +805,7 @@ def get_train_val_test_loader(
return_test: bool = True,
num_workers: int = 0,
pin_memory: bool = True,
seed: int | None = None,
) -> tuple[DataLoader, DataLoader, DataLoader]:
"""Randomly partition a dataset into train, val, test loaders.

Expand All @@ -815,13 +824,16 @@ def get_train_val_test_loader(
Default = 0
pin_memory (bool): Whether to pin the memory of the data loaders
Default: True
seed (int, optional): seed for the random train/val/test split
Default = None (use the global random state)

Returns:
train_loader, val_loader and optionally test_loader
"""
total_size = len(dataset)
indices = list(range(total_size))
random.shuffle(indices)
rng = random if seed is None else random.Random(seed)
rng.shuffle(indices)
train_size = int(train_ratio * total_size)
val_size = int(val_ratio * total_size)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def test_data_loader(structure_data: StructureData) -> None:
assert targets["m"][0].shape == (2, 1)


def test_data_loader_seed(structure_data: StructureData) -> None:
def split(seed: int) -> list[list[int]]:
loaders = get_train_val_test_loader(structure_data, seed=seed)
return [list(loader.sampler.indices) for loader in loaders]

assert split(0) == split(0)
assert split(0) != split(1)


def test_structure_data_inconsistent_length():
# https://github.com/CederGroupHub/chgnet/pull/69
structures = [NaCl.copy() for _ in range(5)]
Expand Down