<commit_before>'''
stimulustools.py

Tools for basic manipulation of stimulus arrays.

(C) 2014 bnaecker, nirum
'''

import numpy as np

def upsamplestim(time, stim, upfact):
    '''

    Upsample the given stimulus by the given factor.

    Input
    -----

    time:
        The time axis of the original stimulus.

    stim:
        The actual stimulus to be upsampled.

    upfact:
        The upsample factor.

    Output

    time_us, stim_us:
        The upsampled time vector and stimulus array

    '''

    # Compute old and new sizes
    oldsz = stim.shape
    newsz = oldsz[:-1] + (upfact * oldsz[-1],)

    # Upsample the stimulus array
    stim_us = (stim.reshape((-1, 1)) * np.ones((1, upfact))).reshape(newsz)

    # Upsample the time vecctor
    x = np.arange(0, upfact * time.size)
    xp = np.arange(0, upfact * time.size, 2)
    time_us = np.interp(x, xp, time)

    return time_us, stim_us

def downsamplestim(time, stim, downfact):
    '''

    Downsample the given stimulus by the given factor.

    Input
    -----

    time:
        The time axis of the original stimulus

    stim:
        The original stimulus array

    downfact:
        The factor by which the stimulus will be downsampled

    Output
    ------

    time_ds, stim_ds:
        The downsampled time vector and stimulus array

    '''

    # Downsample the stimulus array
    stim_ds = np.take(stim, np.arange(0, stim.shape[-1], downfact), axis=-1)
    
    # Downsample the time vector
    time_ds = time[::downfact]

    return time_ds, stim_ds

def slicestim(stim, history, locations=None):
    '''

    Slices a spatiotemporal stimulus array (over time) into overlapping frames.

    Input
    -----

    stim:
        The spatiotemporal or temporal stimulus to slices. Should have shape
        (n, n, t) or (t,).

    history:
        Integer number of time points to keep in each slice.

    locations [optional]:
        Boolean array of temporal locations at which slices are taken. If unspecified,
        use all time points.

    Output
    ------

    slices:
        Array of stimulus slices, with all stimulus dimensions collapsed into one. 
        That is, it has shape (np.prod(stim.shape), `history`)

    '''

    # Converty purely temporal stimuli into spatiotemporal
    if stim.ndim == 1:
        stim = stim.reshape(1, 1, -1)

    # Compute spatial locations to take
    if locations is None:
        locations = np.ones(stim.shape[-1])

    # Preallocate array to hold all slices
    slices = np.empty((history * np.prod(stim.shape[:2]), np.sum(locations[history:])))
    sidx = 0

    # Loop over locations (can't use np.take, since we need to keep `history`)
    for idx in range(history, locations.size):
        if locations[idx]:
            slices[:, sidx] = stim[:, :, idx - history :idx].ravel()

    return slices<commit_msg>added function in stimulustools to compute the stimulus covariance matrix<commit_after>'''
stimulustools.py

Tools for basic manipulation of stimulus arrays.

(C) 2014 bnaecker, nirum
'''

import numpy as np

def upsamplestim(time, stim, upfact):
    '''

    Upsample the given stimulus by the given factor.

    Input
    -----

    time:
        The time axis of the original stimulus.

    stim:
        The actual stimulus to be upsampled.

    upfact:
        The upsample factor.

    Output

    time_us, stim_us:
        The upsampled time vector and stimulus array

    '''

    # Compute old and new sizes
    oldsz = stim.shape
    newsz = oldsz[:-1] + (upfact * oldsz[-1],)

    # Upsample the stimulus array
    stim_us = (stim.reshape((-1, 1)) * np.ones((1, upfact))).reshape(newsz)

    # Upsample the time vecctor
    x = np.arange(0, upfact * time.size)
    xp = np.arange(0, upfact * time.size, 2)
    time_us = np.interp(x, xp, time)

    return time_us, stim_us

def downsamplestim(time, stim, downfact):
    '''

    Downsample the given stimulus by the given factor.

    Input
    -----

    time:
        The time axis of the original stimulus

    stim:
        The original stimulus array

    downfact:
        The factor by which the stimulus will be downsampled

    Output
    ------

    time_ds, stim_ds:
        The downsampled time vector and stimulus array

    '''

    # Downsample the stimulus array
    stim_ds = np.take(stim, np.arange(0, stim.shape[-1], downfact), axis=-1)
    
    # Downsample the time vector
    time_ds = time[::downfact]

    return time_ds, stim_ds

def slicestim(stim, history, locations=None):
    '''

    Slices a spatiotemporal stimulus array (over time) into overlapping frames.

    Input
    -----

    stim:
        The spatiotemporal or temporal stimulus to slices. Should have shape
        (n, n, t) or (t,).

    history:
        Integer number of time points to keep in each slice.

    locations [optional]:
        Boolean array of temporal locations at which slices are taken. If unspecified,
        use all time points.

    Output
    ------

    slices:
        Array of stimulus slices, with all stimulus dimensions collapsed into one. 
        That is, it has shape (np.prod(stim.shape), `history`)

    '''

    # Converty purely temporal stimuli into spatiotemporal
    if stim.ndim == 1:
        stim = stim.reshape(1, 1, -1)

    # Compute spatial locations to take
    if locations is None:
        locations = np.ones(stim.shape[-1])

    # Preallocate array to hold all slices
    slices = np.empty((history * np.prod(stim.shape[:2]), np.sum(locations[history:])))
    sidx = 0

    # Loop over locations (can't use np.take, since we need to keep `history`)
    for idx in range(history, locations.size):
        if locations[idx]:
            slices[:, sidx] = stim[:, :, idx - history :idx].ravel()

    return slices

def getcov(stim, history):
    '''

    Computes a stimulus covariance matrix
    ** Warning: this is computationally expensive for large stimuli **

    Input
    -----

    stim:
        The spatiotemporal or temporal stimulus to slices. Should have shape
        (n, n, t) or (t,).

    history:
        Integer number of time points to keep in each slice.

    Output
    ------

    cov:
        (n*n*t by n*n*t) Covariance matrix

    covinv:
        (n*n*t by n*n*t) Inverse covariance matrix (computed using the pseudoinverse)

    '''

    cov    = np.cov(slicestim(stim, history))
    covinv = np.linalg.inv(cov)

    return cov, covinv
{'fraction_non_alphanumeric': 0.09959476309226932, 'fraction_numerical': 0.00685785536159601, 'mean_word_length': 2.3300467047223665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1254897', 'n_tokens_mistral': 2107, 'n_tokens_neox': 1903, 'n_words': 692}