File size: 1,882 Bytes
04ae9bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """Torch dataset and dataloader helpers."""
from __future__ import annotations
from typing import List, Sequence
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader, Dataset
class AMPDataset(Dataset):
def __init__(
self,
X: np.ndarray,
samples: pd.DataFrame,
target_cols: Sequence[str],
mask_cols: Sequence[str],
n_classes: int | None = None,
):
self.X = np.array(X, dtype=np.float32, copy=True)
self.samples = samples.reset_index(drop=True)
self.target_cols = list(target_cols)
self.mask_cols = list(mask_cols)
self.n_classes = n_classes
Y = np.array(self.samples[self.target_cols].to_numpy(dtype=np.float32), copy=True)
M = np.array(self.samples[self.mask_cols].to_numpy(dtype=np.float32), copy=True)
Y = np.nan_to_num(Y, nan=0.0)
if n_classes is not None:
Y = np.clip(Y, 0, n_classes - 1)
self.Y = Y
self.M = M
def __len__(self) -> int:
return len(self.samples)
def __getitem__(self, idx: int):
return {
"x": torch.from_numpy(self.X[idx]),
"y": torch.from_numpy(self.Y[idx]),
"mask": torch.from_numpy(self.M[idx]),
"idx": torch.tensor(idx, dtype=torch.long),
}
def make_loader(
X: np.ndarray,
samples: pd.DataFrame,
target_cols: Sequence[str],
mask_cols: Sequence[str],
batch_size: int = 64,
shuffle: bool = False,
num_workers: int = 0,
n_classes: int | None = None,
) -> DataLoader:
ds = AMPDataset(X, samples, target_cols, mask_cols, n_classes=n_classes)
return DataLoader(
ds,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
pin_memory=torch.cuda.is_available(),
drop_last=False,
)
|