calpit.nn.data

PyTorch data sets of (alpha, x, target) rows for training Cal-PIT networks.

Every item is a tuple (alpha, x, target): the coverage level, shape (); the features, shape (n_features,); and the target 1.0 if the PIT is at most alpha and 0.0 otherwise, shape (). A DataLoader batches them into the (alpha, x) inputs of the Cal-PIT network contract (see calpit.nn.models).

Classes

CoverageDataset

Training set that draws a fresh coverage level for every item.

CoverageGridDataset

Validation set of every object at every coverage level of a fixed grid.

PhotometryDataset

Training set read lazily from an HDF5 file of photometric features.

Module Contents

class CoverageDataset(x: numpy.typing.ArrayLike, pit: numpy.typing.ArrayLike, oversample: float = 1)[source]

Bases: torch.utils.data.Dataset

Training set that draws a fresh coverage level for every item.

Each time an item is read, alpha is drawn from Uniform(0, 1) with the torch random number generator, so every epoch sees new (alpha, x) pairs.

Indexing with a list of indices returns a whole batch at once, which is much faster than collating single items; batches() makes a DataLoader that does this. The batch draws the same alpha as reading its items one by one.

Parameters:
  • x – The features, shape (n_objects, n_features).

  • pit – The PIT values, shape (n_objects,).

  • oversample – How many items each object yields per epoch; the data set has int(n_objects * oversample) items.

Variables:
  • x – The features as float32, shape (n_objects, n_features).

  • pit – The PIT values, shape (n_objects,).

  • oversample – The oversampling factor.

x[source]
pit[source]
oversample = 1[source]
__len__() → int[source]

Returns the number of items per epoch.

__getitem__(index: int | list[int]) → tuple[torch.Tensor, torch.Tensor, torch.Tensor][source]

Returns the item (alpha, x, target) of object index % n_objects, or a batch for a list.

batches(batch_size: int, shuffle: bool = True, num_workers: int = 0) → torch.utils.data.DataLoader[source]

Returns a DataLoader that reads whole batches of shuffled items.

It yields the same batches, with the same alpha, as DataLoader(self, batch_size=batch_size, shuffle=shuffle), but builds each batch at once.

Parameters:
  • batch_size – The number of items per batch.

  • shuffle – Whether to shuffle the items every epoch.

  • num_workers – The number of worker processes.

Returns:

The DataLoader.

class CoverageGridDataset(x: numpy.typing.ArrayLike, pit: numpy.typing.ArrayLike, alpha: numpy.typing.ArrayLike, batch_size: int = 2048)[source]

Bases: torch.utils.data.Dataset

Validation set of every object at every coverage level of a fixed grid.

The rows run through the grid alpha-major: row i pairs alpha[i // n_objects] with object i % n_objects. Each item is a whole batch of consecutive rows, built on the fly, so memory does not grow with the grid. Iterate over it directly, or wrap it in DataLoader(dataset, batch_size=None).

Parameters:
  • x – The features, shape (n_objects, n_features).

  • pit – The PIT values, shape (n_objects,).

  • alpha – The coverage levels, shape (n_alpha,).

  • batch_size – The number of rows per item.

Variables:
  • x – The features as float32, shape (n_objects, n_features).

  • pit – The PIT values, shape (n_objects,).

  • alpha – The coverage levels, shape (n_alpha,). The network gets them as float32, but the targets compare the PIT with these values.

  • batch_size – The number of rows per item.

x[source]
pit[source]
alpha[source]
batch_size = 2048[source]
n_rows() → int[source]

Returns the number of (alpha, object) rows, n_alpha * n_objects.

__len__() → int[source]

Returns the number of batches.

__getitem__(index: int) → tuple[torch.Tensor, torch.Tensor, torch.Tensor][source]

Returns batch index as (alpha, x, target), each with up to batch_size rows.

class PhotometryDataset(file_path: str | pathlib.Path, pit: numpy.typing.ArrayLike, scaler: object | None = None, key: str = 'dered_color_features')[source]

Bases: torch.utils.data.Dataset

Training set read lazily from an HDF5 file of photometric features.

Like CoverageDataset, alpha is drawn from Uniform(0, 1) for every item. The features are read row by row, so the file need not fit in memory. The file stays open for the life of the data set; call close() when done with it.

Parameters:
  • file_path – The path to an .hdf5 file.

  • pit – The PIT values, shape (n_objects,).

  • scaler – An object with a scikit-learn style transform method applied to each row of features, or None to leave them unscaled.

  • key – The HDF5 data set of features, shape (n_objects, n_features).

Variables:
  • file – The open h5py.File.

  • pit – The PIT values, shape (n_objects,).

  • scaler – The scaler, or None.

  • key – The HDF5 data set of features.

Raises:

ImportError – If h5py is not installed.

pit[source]
scaler = None[source]
key = 'dered_color_features'[source]
file[source]
__len__() → int[source]

Returns the number of objects.

__getitem__(index: int) → tuple[torch.Tensor, torch.Tensor, torch.Tensor][source]

Returns the item (alpha, x, target) of object index.

close() → None[source]

Closes the HDF5 file.