<commit_before>"""Some helper funcs for MNE-python"""
import mne
import numpy as np
import pandas as pd


def create_random_events(nev, ntimes, nclasses):
    """Creates a random set of nev events for each of
    nclasses, randomly interspersed in ntimes."""
    ixs = np.random.permutation(range(ntimes))
    classes = np.tile(range(nclasses), (nev, 1)).ravel()
    ixs_ev = ixs[:nev*nclasses]
    events = np.zeros([nev*nclasses, 3])
    for i, (ix, evclass) in enumerate(zip(ixs_ev, classes)):
        events[i, :] = [ix, 0, evclass]
    return events.astype(int)


def create_random_epochs(nep, nchan, ntime, sfreq,
                         nclasses=2, ch_types='eeg'):
    data = np.random.randn(nep*nclasses, nchan, ntime*sfreq)
    ev = create_random_events(nep, ntime*sfreq, nclasses)
    info = mne.create_info([str(i) for i in range(nchan)], sfreq, ch_types)
    ep = mne.epochs.EpochsArray(data, info, ev)
    return ep


def create_random_raw(nchan, ntime, sfreq, ch_types='eeg'):
    data = np.random.randn(nchan, ntime*sfreq)
    info = mne.create_info([str(i) for i in range(nchan)], sfreq, ch_types)
    raw = mne.io.RawArray(data, info)
    return raw


class Events(object):
    """Container for events metadata"""
    def __init__(self, time_start, sfreq, time_stop=None, meta=None):
        self.time_start = np.array(time_start)
        if time_stop is not None:
            time_stop = np.array(time_stop)
            if time_stop.shape != time_start.shape:
                raise ValueError('start/stop mismatch')
            self.length = time_stop - self.time_start
        if meta is not None:
            if not isinstance(meta, pd.DataFrame):
                raise AssertionError('metadata must be a dataframe')
            if meta.shape[0] != len(time_start):
                raise ValueError('metadata / time_start length mismatch')
            if np.sum(meta.isnull().values) > 0:
                raise ValueError('Found NaNs in metadata')
        self.meta = meta
        self.sfreq = sfreq
        self.ids = np.arange(len(time_start))

    def to_mne(self, meta_columns=None):
        times = self.time_start * self.sfreq
        zeros = np.zeros_like(times)

        if meta_columns is not None:
            m_str = ['/'.join([str(i) for i in rw[meta_columns].values])
                     for _, rw in self.meta.iterrows()]
            ev_id = {str(i_mstr): i + 1
                     for i, i_mstr in enumerate(np.unique(m_str))}
            events = np.array([ev_id[i_mstr] for i_mstr in m_str])
        else:
            events = np.arange(times.shape[0])
            ev_id = {'event_{0}'.format(i): i for i in self.ids}
        events = np.vstack([times, zeros, events]).T.astype(int)
        return events, ev_id
<commit_msg>Fixing the indexing bug in Events<commit_after>"""Some helper funcs for MNE-python"""
import mne
import numpy as np
import pandas as pd


def create_random_events(nev, ntimes, nclasses):
    """Creates a random set of nev events for each of
    nclasses, randomly interspersed in ntimes."""
    ixs = np.random.permutation(range(ntimes))
    classes = np.tile(range(nclasses), (nev, 1)).ravel()
    ixs_ev = ixs[:nev*nclasses]
    events = np.zeros([nev*nclasses, 3])
    for i, (ix, evclass) in enumerate(zip(ixs_ev, classes)):
        events[i, :] = [ix, 0, evclass]
    return events.astype(int)


def create_random_epochs(nep, nchan, ntime, sfreq,
                         nclasses=2, ch_types='eeg'):
    data = np.random.randn(nep*nclasses, nchan, ntime*sfreq)
    ev = create_random_events(nep, ntime*sfreq, nclasses)
    info = mne.create_info([str(i) for i in range(nchan)], sfreq, ch_types)
    ep = mne.epochs.EpochsArray(data, info, ev)
    return ep


def create_random_raw(nchan, ntime, sfreq, ch_types='eeg'):
    data = np.random.randn(nchan, ntime*sfreq)
    info = mne.create_info([str(i) for i in range(nchan)], sfreq, ch_types)
    raw = mne.io.RawArray(data, info)
    return raw


class Events(object):
    """Container for events metadata"""
    def __init__(self, time_start, sfreq, time_stop=None, meta=None):
        self.time_start = np.array(time_start)
        if time_stop is not None:
            time_stop = np.array(time_stop)
            if time_stop.shape != time_start.shape:
                raise ValueError('start/stop mismatch')
            self.length = time_stop - self.time_start
        if meta is not None:
            if not isinstance(meta, pd.DataFrame):
                raise AssertionError('metadata must be a dataframe')
            if meta.shape[0] != len(time_start):
                raise ValueError('metadata / time_start length mismatch')
            if np.sum(meta.isnull().values) > 0:
                raise ValueError('Found NaNs in metadata')
            meta = meta.reset_index(drop=True)
        self.meta = meta
        self.sfreq = sfreq
        self.ids = np.arange(len(time_start))

    def to_mne(self, meta_columns=None):
        times = self.time_start * self.sfreq
        zeros = np.zeros_like(times)

        if meta_columns is not None:
            m_str = ['/'.join([str(i) for i in rw[meta_columns].values])
                     for _, rw in self.meta.iterrows()]
            ev_id = {str(i_mstr): i + 1
                     for i, i_mstr in enumerate(np.unique(m_str))}
            events = np.array([ev_id[i_mstr] for i_mstr in m_str])
        else:
            events = np.arange(times.shape[0])
            ev_id = {'event_{0}'.format(i): i for i in self.ids}
        events = np.vstack([times, zeros, events]).T.astype(int)
        return events, ev_id

{'fraction_non_alphanumeric': 0.12095765588708236, 'fraction_numerical': 0.0032160085760228694, 'mean_word_length': 2.556543837357052, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '854657', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1862, 'n_words': 467}