annbatch.samplers.WeightedClassSampler#

class annbatch.samplers.WeightedClassSampler(chunk_size, preload_nchunks, batch_size, *, classes, num_samples, class_weights=None, mask=None, drop_last=False, rng=None)#

Sample batches whose class composition follows class_weights.

Chunks are read exactly as ClassSampler reads them – one class per chunk – but the rows of a whole preload window are shuffled together before being split into batches, so each batch mixes classes in expectation proportionally to their weights instead of being drawn from a single class.

Attributes table#

batch_size

The batch size for data loading.

mask

The observation range this sampler operates on.

rng

The random number generator used by this sampler.

shuffle

Whether data is shuffled.

Methods table#

n_batches(n_obs)

Return the number of batches.

sample(n_obs)

Sample load requests given the total number of observations.

validate(n_obs)

Validate that the codes describe exactly the loader's observations.

Attributes#

WeightedClassSampler.batch_size#
WeightedClassSampler.mask#
WeightedClassSampler.rng#

The random number generator used by this sampler.

WeightedClassSampler.shuffle#

Methods#

WeightedClassSampler.n_batches(n_obs)#

Return the number of batches.

Parameters:
n_obs int

The total number of observations available.

Return type:

int

Returns:

int The total number of batches this sampler will produce.

WeightedClassSampler.sample(n_obs)#

Sample load requests given the total number of observations.

Base implementation simply calls validate() and then yields via _sample().

Parameters:
n_obs int

The total number of observations available.

Yields:

LoadRequest – Load requests for batching data.

Return type:

Iterator[LoadRequest]

WeightedClassSampler.validate(n_obs)#

Validate that the codes describe exactly the loader’s observations.

Return type:

None