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
4 changes: 2 additions & 2 deletions physioex/data/datamodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
sequence_length: int = 21,
target_transform: Callable = None,
task: str = "sleep",
folds: Union[int, List[int]] = -1,
folds: Union[int, List[int]] = 0,
data_folder: str = None,
num_workers: int = 0,
data_prefetch: bool = True,
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(

if isinstance(eval_datasets, list):
self.eval_dataset = PhysioExDataset(
datasets=datasets,
datasets=eval_datasets,
preprocessing=preprocessing,
selected_channels=selected_channels,
sequence_length=-1,
Expand Down
44 changes: 19 additions & 25 deletions physioex/data/dataset.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When no specific fold is specified (fold=-1), a fold is sampled when the training dataset is created, and again when eval_dataset is created, leading to different folds in each.

I’ve changed the default fold to fold 0 when no fold is specified. I’ve done this everywhere in the code and removed random fold selection.

Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def __init__(
self.mean, self.std = self.readers[0].reader.mean, self.readers[0].reader.std

self.dataset_idx = np.array(self.dataset_idx, dtype=np.int8)
# set the table fold to a random fold by default
self.split()
# set the table fold to fold 0 by default
self.split(0)
self.target_transform = target_transform

self.len = offset
Expand All @@ -107,36 +107,30 @@ def set_scaling(self, mean : torch.Tensor, std : torch.Tensor ):
def get_scaling(self):
return self.mean, self.std

def split(self, fold: int = -1, dataset_idx: int = -1):
def split(self, fold: int = 0, dataset_idx: int = -1):
assert fold >= 0, "ERR: fold must be >= 0. fold=-1 (randomly selected fold) is deprecated."
assert dataset_idx < len(self.tables), "ERR: dataset_idx out of range"

# if fold is -1, set the split to a random fold for each dataset
if fold == -1 and dataset_idx == -1:
for i, table in enumerate(self.tables):
num_folds = [col for col in table.columns if "fold_" in col]
num_folds = len(num_folds)
selected_fold = np.random.randint(0, num_folds)

self.tables[i]["split"] = self.tables[i][f"fold_{selected_fold}"].map(
{"train": 0, "valid": 1, "test": 2}
)
elif fold == -1 and dataset_idx != -1:
num_folds = [
col for col in self.tables[dataset_idx].columns if "fold_" in col
]
num_folds = len(num_folds)
selcted_fold = np.random.randint(0, num_folds)

self.tables[dataset_idx]["split"] = table[f"fold_{selcted_fold}"].map(
{"train": 0, "valid": 1, "test": 2}
)
elif fold != -1 and dataset_idx == -1:
if dataset_idx == -1:
# Apply to all datasets
for i, table in enumerate(self.tables):
fold_columns = [col for col in table.columns if "fold_" in col]
num_folds = len(fold_columns)
if fold >= num_folds:
raise ValueError(f"ERR: fold {fold} is out of range for dataset {i}. Available folds: 0-{num_folds-1} (total: {num_folds} folds)")

self.tables[i]["split"] = table[f"fold_{fold}"].map(
{"train": 0, "valid": 1, "test": 2}
)
else:
self.tables[dataset_idx]["split"] = self.tables[dataset_idx][f"fold_{fold}"].map(
# Apply to specific dataset
table = self.tables[dataset_idx]
fold_columns = [col for col in table.columns if "fold_" in col]
num_folds = len(fold_columns)
if fold >= num_folds:
raise ValueError(f"ERR: fold {fold} is out of range for dataset {dataset_idx}. Available folds: 0-{num_folds-1} (total: {num_folds} folds)")

self.tables[dataset_idx]["split"] = table[f"fold_{fold}"].map(
{"train": 0, "valid": 1, "test": 2}
)

Expand Down
Loading
Loading