def _create_windows_from_events(
    ds,
    infer_mapping,
    infer_window_size_stride,
    trial_start_offset_samples,
    trial_stop_offset_samples,
    window_size_samples=None,
    window_stride_samples=None,
    drop_last_window=False,
    mapping=None,
    preload=False,
    drop_bad_windows=True,
    picks=None,
    reject=None,
    flat=None,
    on_missing="error",
    accepted_bads_ratio=0.0,
    verbose="error",
    use_mne_epochs=False,
):
    """Create WindowsDataset from BaseDataset based on events.

    Parameters
    ----------
    ds : BaseDataset
        Dataset containing continuous data and description.
    infer_mapping : bool
        If True, extract all events from all datasets and map them to
        increasing integers starting from 0.
    infer_window_size_stride : bool
        If True, infer the stride from the original trial size of the first
        trial and trial_start_offset_samples and trial_stop_offset_samples.

    See `create_windows_from_events` for description of other parameters.

    Returns
    -------
    EEGWindowsDataset :
        Windowed dataset.
    """
    # catch window_kwargs to store to dataset
    window_kwargs = [
        (create_windows_from_events.__name__, _get_windowing_kwargs(locals())),
    ]
    if infer_mapping:
        unique_events = np.unique(ds.raw.annotations.description)
        new_unique_events = [x for x in unique_events if x not in mapping]
        # mapping event descriptions to integers from 0 on
        max_id_existing_mapping = len(mapping)
        mapping.update(
            {
                event_name: i_event_type + max_id_existing_mapping
                for i_event_type, event_name in enumerate(new_unique_events)
            }
        )

    events, events_id = mne.events_from_annotations(ds.raw, mapping)
    onsets = events[:, 0]
    # Onsets are relative to the beginning of the recording
    filtered_durations = np.array(
        [a["duration"] for a in ds.raw.annotations if a["description"] in events_id]
    )

    stops = onsets + (filtered_durations * ds.raw.info["sfreq"]).astype(int)
    # XXX This could probably be simplified by using chunk_duration in
    #     `events_from_annotations`

    last_samp = ds.raw.first_samp + ds.raw.n_times - 1
    # `stops` is used exclusively (i.e. `start:stop`), so add back 1
    if stops[-1] + trial_stop_offset_samples > last_samp + 1:
        raise ValueError(
            '"trial_stop_offset_samples" too large. Stop of last trial '
            f'({stops[-1]}) + "trial_stop_offset_samples" '
            f"({trial_stop_offset_samples}) must be smaller than length of"
            f" recording ({len(ds)})."
        )

    if infer_window_size_stride:
        # window size is trial size
        if window_size_samples is None:
            window_size_samples = (
                stops[0]
                + trial_stop_offset_samples
                - (onsets[0] + trial_start_offset_samples)
            )
            window_stride_samples = window_size_samples
        this_trial_sizes = (stops + trial_stop_offset_samples) - (
            onsets + trial_start_offset_samples
        )
        # Maybe actually this is not necessary?
        # We could also just say we just assume window size=trial size
        # in case not given, without this condition...
        # but then would have to change functions overall
        checker_trials_size = this_trial_sizes == window_size_samples

        if not np.all(checker_trials_size):
            trials_drops = int(len(this_trial_sizes) - sum(checker_trials_size))
            warnings.warn(
                f"Dropping trials with different windows size {trials_drops}",
            )
            bads_size_trials = checker_trials_size
            events = events[checker_trials_size]
            onsets = onsets[checker_trials_size]
            stops = stops[checker_trials_size]

    description = events[:, -1]

    if not use_mne_epochs:
        onsets = onsets - ds.raw.first_samp
        stops = stops - ds.raw.first_samp
    i_trials, i_window_in_trials, starts, stops = _compute_window_inds(
        onsets,
        stops,
        trial_start_offset_samples,
        trial_stop_offset_samples,
        window_size_samples,
        window_stride_samples,
        drop_last_window,
        accepted_bads_ratio,
    )

    if any(np.diff(starts) <= 0):
        raise NotImplementedError("Trial overlap not implemented.")

    events = [
        [start, window_size_samples, description[i_trials[i_start]]]
        for i_start, start in enumerate(starts)
    ]
    events = np.array(events)

    description = events[:, -1]

    metadata = pd.DataFrame(
        {
            "i_window_in_trial": i_window_in_trials,
            "i_start_in_trial": starts,
            "i_stop_in_trial": stops,
            "target": description,
        }
    )
    if use_mne_epochs:
        # window size - 1, since tmax is inclusive
        mne_epochs = mne.Epochs(
            ds.raw,
            events,
            events_id,
            baseline=None,
            tmin=0,
            tmax=(window_size_samples - 1) / ds.raw.info["sfreq"],
            metadata=metadata,
            preload=preload,
            picks=picks,
            reject=reject,
            flat=flat,
            on_missing=on_missing,
            verbose=verbose,
        )
        if drop_bad_windows:
            mne_epochs.drop_bad()
        windows_ds = WindowsDataset(
            mne_epochs,
            ds.description,
        )
    else:
        windows_ds = EEGWindowsDataset(
            ds.raw,
            metadata=metadata,
            description=ds.description,
        )
    # add window_kwargs and raw_preproc_kwargs to windows dataset
    setattr(windows_ds, "window_kwargs", window_kwargs)
    kwargs_name = "raw_preproc_kwargs"
    if hasattr(ds, kwargs_name):
        setattr(windows_ds, kwargs_name, getattr(ds, kwargs_name))
    return windows_ds
Create WindowsDataset from BaseDataset based on events.

    Parameters
    ----------
    ds : BaseDataset
        Dataset containing continuous data and description.
    infer_mapping : bool
        If True, extract all events from all datasets and map them to
        increasing integers starting from 0.
    infer_window_size_stride : bool
        If True, infer the stride from the original trial size of the first
        trial and trial_start_offset_samples and trial_stop_offset_samples.

    See `create_windows_from_events` for description of other parameters.

    Returns
    -------
    EEGWindowsDataset :
        Windowed dataset.
    
braindecode/preprocessing/windowers.py