class braindecode.samplers.BalancedSequenceSampler(metadata, n_windows, n_sequences=10, random_state=None)[source]#

Balanced sampling of sequences of consecutive windows with categorical targets.

Balanced sampling of sequences inspired by the approach of [Perslev2021]: 1. Uniformly sample a recording out of the available ones. 2. Uniformly sample one of the classes. 3. Sample a window of the corresponding class in the selected recording. 4. Extract a sequence of windows around the sampled window.

  • metadata (pd.DataFrame) – See RecordingSampler. Must contain a column target with categorical targets.

  • n_windows (int) – Number of consecutive windows in a sequence.

  • n_sequences (int) – Number of sequences to sample.

  • random_state (np.random.RandomState | int | None) – Random state.



Perslev M, Darkner S, Kempfner L, Nikolic M, Jennum PJ, Igel C. U-Sleep: resilient high-frequency sleep staging. npj Digit. Med. 4, 72 (2021). perslev/U-Time



Return a random class.


rec_ind (int | None) – Index to the recording to sample from. If None, the recording will be uniformly sampled across available recordings.


  • int – Sampled class.

  • int – Index to the recording the class was sampled from.