Skip to content

Randomize

pynapple.process.randomize

shift_timestamps

shift_timestamps(ts, min_shift=0.0, max_shift=None)

Shifts all the time stamps of a random amount between min_shift and max_shift, wrapping the end of the time support to the beginning.

Parameters:

Name Type Description Default
ts Ts or TsGroup

The timestamps to shift. If TsGroup, shifts all Ts in the group independently.

required
min_shift float

minimum shift (default: 0 )

0.0
max_shift float

maximum shift, (default: length of time support)

None

Returns:

Type Description
Ts or TsGroup

The randomly shifted timestamps

Source code in pynapple/process/randomize.py
def shift_timestamps(ts, min_shift=0.0, max_shift=None):
    """
    Shifts all the time stamps of a random amount between min_shift and max_shift, wrapping the
    end of the time support to the beginning.


    Parameters
    ----------
    ts : Ts or TsGroup
        The timestamps to shift. If TsGroup, shifts all Ts in the group independently.
    min_shift : float, optional
        minimum shift (default: 0 )
    max_shift : float, optional
        maximum shift, (default: length of time support)

    Returns
    -------
    Ts or TsGroup
        The randomly shifted timestamps
    """
    strategies = {
        nap.time_series.Ts: _shift_ts,
        nap.ts_group.TsGroup: _shift_tsgroup,
    }
    # checks input type
    if type(ts) not in strategies.keys():
        raise TypeError("Invalid input type, should be Ts or TsGroup")

    strategy = strategies[type(ts)]
    return strategy(ts, min_shift, max_shift)

shuffle_ts_intervals

shuffle_ts_intervals(ts, min_shift=0.0, max_shift=None)

Randomizes the timestamps by shuffling the intervals between them.

Parameters:

Name Type Description Default
ts Ts or TsGroup

The timestamps to randomize. If TsGroup, randomizes all Ts in the group independently.

required

Returns:

Type Description
Ts or TsGroup

The randomized timestamps, with shuffled intervals

Source code in pynapple/process/randomize.py
def shuffle_ts_intervals(ts, min_shift=0.0, max_shift=None):
    """
    Randomizes the timestamps by shuffling the intervals between them.


    Parameters
    ----------
    ts : Ts or TsGroup
        The timestamps to randomize. If TsGroup, randomizes all Ts in the group independently.

    Returns
    -------
    Ts or TsGroup
        The randomized timestamps, with shuffled intervals
    """
    strategies = {
        nap.time_series.Ts: _shuffle_intervals_ts,
        nap.ts_group.TsGroup: _shuffle_intervals_tsgroup,
    }
    # checks input type
    if type(ts) not in strategies.keys():
        raise TypeError("Invalid input type, should be Ts or TsGroup")

    strategy = strategies[type(ts)]
    return strategy(ts)

jitter_timestamps

jitter_timestamps(ts, max_jitter=None, keep_tsupport=False)

Jitters each time stamp independently of random amounts uniformly drawn between -max_jitter and max_jitter.

Parameters:

Name Type Description Default
ts Ts or TsGroup

The timestamps to jitter. If TsGroup, jitter is applied to each element of the group.

required
max_jitter float

maximum jitter

None
keep_tsupport

If True, keep time support of the input. The number of timestamps will not be conserved. If False, the time support is inferred from the jittered timestamps. The number of tmestamps is conserved. (default: False)

False

Returns:

Type Description
Ts or TsGroup

The jittered timestamps

Source code in pynapple/process/randomize.py
def jitter_timestamps(ts, max_jitter=None, keep_tsupport=False):
    """
    Jitters each time stamp independently of random amounts uniformly drawn between -max_jitter and max_jitter.


    Parameters
    ----------
    ts : Ts or TsGroup
        The timestamps to jitter. If TsGroup, jitter is applied to each element of the group.
    max_jitter : float
        maximum jitter
    keep_tsupport: bool, optional
        If True, keep time support of the input. The number of timestamps will not be conserved.
        If False, the time support is inferred from the jittered timestamps. The number of tmestamps
        is conserved. (default: False)

    Returns
    -------
    Ts or TsGroup
        The jittered timestamps
    """
    strategies = {
        nap.time_series.Ts: _jitter_ts,
        nap.ts_group.TsGroup: _jitter_tsgroup,
    }
    # checks input type
    if type(ts) not in strategies.keys():
        raise TypeError("Invalid input type, should be Ts or TsGroup")

    if max_jitter is None:
        raise TypeError("missing required argument: max_jitter ")

    strategy = strategies[type(ts)]
    return strategy(ts, max_jitter, keep_tsupport)

resample_timestamps

resample_timestamps(ts)

Resamples the timestamps in the time support, with uniform distribution.

Parameters:

Name Type Description Default
ts Ts or TsGroup

The timestamps to resample. If TsGroup, each Ts object in the group is independently resampled, in the time support of the whole group.

required

Returns:

Type Description
Ts or TsGroup

The resampled timestamps

Source code in pynapple/process/randomize.py
def resample_timestamps(ts):
    """
    Resamples the timestamps in the time support, with uniform distribution.


    Parameters
    ----------
    ts : Ts or TsGroup
        The timestamps to resample. If TsGroup, each Ts object in the group is independently
        resampled, in the time support of the whole group.


    Returns
    -------
    Ts or TsGroup
        The resampled timestamps
    """
    strategies = {
        nap.time_series.Ts: _resample_ts,
        nap.ts_group.TsGroup: _resample_tsgroup,
    }
    # checks input type
    if type(ts) not in strategies.keys():
        raise TypeError("Invalid input type, should be Ts or TsGroup")

    strategy = strategies[type(ts)]
    return strategy(ts)