Ejemplo n.º 1
0
def test_epoch_overlaps_case5_bounds():
    epoch_a = nept.Epoch([1.5], [2.5])
    epoch_b = nept.Epoch([1.5], [2.5])

    overlaps = epoch_a.overlaps(epoch_b)

    assert np.allclose(overlaps.starts, np.array([1.5]))
    assert np.allclose(overlaps.stops, np.array([2.5]))
Ejemplo n.º 2
0
def test_epoch_intersect_case5():
    epoch_a = nept.Epoch([1.5], [2.5])
    epoch_b = nept.Epoch([1.5], [2.5])

    intersects = epoch_a.intersect(epoch_b)

    assert np.allclose(intersects.starts, np.array([1.5]))
    assert np.allclose(intersects.stops, np.array([2.5]))
Ejemplo n.º 3
0
def test_epoch_join():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts, stops)

    epoch_2 = nept.Epoch([1.8], [2.5])

    union = epoch_1.join(epoch_2)

    assert np.allclose(union.starts, np.array([0.0, 0.9, 1.6, 1.8]))
    assert np.allclose(union.stops, np.array([1.0, 1.5, 2.0, 2.5]))
Ejemplo n.º 4
0
def test_epoch_no_intersect():
    starts1 = [0.0, 0.9, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    epoch_2 = nept.Epoch([1.5], [1.6])

    intersects = epoch_1.intersect(epoch_2)

    assert np.allclose(intersects.starts, np.array([]))
    assert np.allclose(intersects.stops, np.array([]))
Ejemplo n.º 5
0
def test_epoch_overlaps_empty():
    starts1 = [0.0, 1.1, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    starts2 = []
    stops2 = []
    epoch_2 = nept.Epoch(starts2, stops2)

    overlaps = epoch_1.overlaps(epoch_2)

    assert overlaps.isempty
Ejemplo n.º 6
0
def test_excludes_empty():
    starts1 = [0.0, 1.1, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    starts2 = [0.0]
    stops2 = [2.5]
    epoch_2 = nept.Epoch(starts2, stops2)

    excludes = epoch_1.excludes(epoch_2)

    assert excludes.time.size == 0
Ejemplo n.º 7
0
def test_epoch_intersect_empty():
    starts1 = [0.0, 1.1, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    starts2 = []
    stops2 = []
    epoch_2 = nept.Epoch(starts2, stops2)

    intersects = epoch_1.intersect(epoch_2)

    assert intersects.isempty
Ejemplo n.º 8
0
def test_epoch_overlaps_multiple_bounds():
    starts_a = [1.0, 4.0, 6.0, 8.0]
    stops_a = [2.0, 5.0, 7.0, 9.0]
    epoch_a = nept.Epoch(starts_a, stops_a)

    starts_b = [0.5, 4.3, 5.1, 8.2]
    stops_b = [1.7, 5.0, 7.2, 8.4]
    epoch_b = nept.Epoch(starts_b, stops_b)

    overlaps = epoch_a.overlaps(epoch_b)

    assert np.allclose(overlaps.starts, np.array([0.5, 4.3, 5.1, 8.2]))
    assert np.allclose(overlaps.stops, np.array([1.7, 5.0, 7.2, 8.4]))
Ejemplo n.º 9
0
def test_epoch_intersect_multiple():
    starts_a = [1.0, 4.0, 6.0, 8.0]
    stops_a = [2.0, 5.0, 7.0, 9.0]
    epoch_a = nept.Epoch(starts_a, stops_a)

    starts_b = [0.5, 4.3, 5.1, 8.2]
    stops_b = [1.7, 5.0, 7.2, 8.4]
    epoch_b = nept.Epoch(starts_b, stops_b)

    intersects = epoch_a.intersect(epoch_b)

    assert np.allclose(intersects.starts, np.array([1.0, 4.3, 6.0, 8.2]))
    assert np.allclose(intersects.stops, np.array([1.7, 5.0, 7.0, 8.4]))
Ejemplo n.º 10
0
def test_epoch_overlaps_case1_bounds():
    starts1 = [0.0, 1.1, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    starts2 = [1.55]
    stops2 = [1.8]
    epoch_2 = nept.Epoch(starts2, stops2)

    overlaps = epoch_1.overlaps(epoch_2)

    assert np.allclose(overlaps.starts, np.array([1.55]))
    assert np.allclose(overlaps.stops, np.array([1.8]))
Ejemplo n.º 11
0
def test_excludes_case6():
    starts1 = [0.0, 1.1, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    starts2 = [2.1]
    stops2 = [2.5]
    epoch_2 = nept.Epoch(starts2, stops2)

    excludes = epoch_1.excludes(epoch_2)

    assert np.allclose(excludes.starts, np.array([0.0, 1.1, 1.6]))
    assert np.allclose(excludes.stops, np.array([1.0, 1.5, 2.0]))
Ejemplo n.º 12
0
def test_epoch_intersect_multiple2():
    starts_a = [1.0, 4.0, 6.0, 8.0]
    stops_a = [2.0, 5.0, 7.0, 9.0]
    epoch_a = nept.Epoch(starts_a, stops_a)

    starts_b = [1.0, 4.0, 5.9, 7.8]
    stops_b = [2.0, 6.2, 6.2, 9.3]
    epoch_b = nept.Epoch(starts_b, stops_b)

    intersects = epoch_a.intersect(epoch_b)

    assert np.allclose(intersects.starts, np.array([1.0, 4.0, 6.0, 8.0]))
    assert np.allclose(intersects.stops, np.array([2.0, 5.0, 6.2, 9.0]))
Ejemplo n.º 13
0
def test_epoch_intersect_case1():
    starts1 = [0.0, 1.1, 1.6]
    stops1 = [1.0, 1.5, 2.0]
    epoch_1 = nept.Epoch(starts1, stops1)

    starts2 = [1.55]
    stops2 = [1.8]
    epoch_2 = nept.Epoch(starts2, stops2)

    intersects = epoch_1.intersect(epoch_2)

    assert np.allclose(intersects.starts, np.array([1.6]))
    assert np.allclose(intersects.stops, np.array([1.8]))
Ejemplo n.º 14
0
def spike_counts(spikes, epochs, window=None):
    """Get spike counts for specific interval.

    Parameters
    ----------
    spikes : list
        Containing nept.SpikeTrain for each neuron.
    interval_times : nept.Epoch
    window : float
        When window is set, takes the spike times for this window length
        around the center of the interval times. The default is set to None.
        Suggested window size is 0.1.

    Returns
    -------
    count_matrix : np.array
        num_neurons x num_bins

    """
    if window is not None:
        centers = epochs.centers - (window * 0.5)
        epochs = nept.Epoch(centers, np.array(centers) + window)

    n_neurons = len(spikes)
    count_matrix = np.zeros((n_neurons, epochs.n_epochs))

    for i, (start, stop) in enumerate(zip(epochs.starts, epochs.stops)):
        for neuron in range(n_neurons):
            count_matrix[neuron][i] = ((start <= spikes[neuron].time) &
                                       (spikes[neuron].time <= stop)).sum()

    return count_matrix
Ejemplo n.º 15
0
def test_epoch_sortlist():
    starts = [0.9, 0.0, 1.6]
    stops = [1.5, 1.0, 2.0]
    epoch = nept.Epoch(starts, stops)

    assert np.allclose(epoch.starts, np.array([0.0, 0.9, 1.6]))
    assert np.allclose(epoch.stops, np.array([1., 1.5, 2.]))
Ejemplo n.º 16
0
def speed_threshold(position, thresh, t_smooth, direction):
    """Finds the epochs where speed is greater or lesser than a threshold.

    Parameters
    ----------
    position: nept.Position
    thresh: float
    direction: str
        Must be "greater" or "lesser"

    Returns
    -------
    nept.Epoch
    """
    speed = position.speed(t_smooth)
    if direction == "greater":
        changes = np.diff(np.hstack(([0], (np.squeeze(speed.data) >= thresh).astype(int))))
    elif direction == "lesser":
        changes = np.diff(np.hstack(([0], (np.squeeze(speed.data) <= thresh).astype(int))))
    else:
        raise ValueError("Must be 'lesser' or 'greater'")

    starts = np.where(changes == 1)[0]
    stops = np.where(changes == -1)[0]

    if len(starts) != len(stops):
        assert len(starts) - len(stops) == 1
        stops = np.hstack((stops, position.n_samples - 1))

    if starts[-1] == stops[-1]:
        print("Last sample not included in speed thresholding")
        starts = starts[:-1]
        stops = stops[:-1]

    return nept.Epoch(position.time[starts], position.time[stops])
Ejemplo n.º 17
0
def find_multi_in_epochs(spikes, epochs, min_involved):
    """Finds epochs with minimum number of participating neurons.

    Parameters
    ----------
    spikes: np.array
        Of nept.SpikeTrain objects
    epochs: nept.Epoch
    min_involved: int

    Returns
    -------
    multi_epochs: nept.Epoch

    """
    multi_starts = []
    multi_stops = []

    for start, stop in zip(epochs.starts, epochs.stops):
        sliced_spikes = [spiketrain.time_slice(start, stop) for spiketrain in spikes]
        n_spikes = np.asarray([len(spiketrain.time) for spiketrain in sliced_spikes])

        n_active = len(n_spikes[n_spikes >= 1])

        if n_active >= min_involved:
            multi_starts.append(start)
            multi_stops.append(stop)

    return nept.Epoch(np.array(multi_starts), np.array(multi_stops))
Ejemplo n.º 18
0
def test_epoch_overlaps_multiple2_bounds():
    starts_a = [0.0, 4.0, 6.0, 8.0]
    stops_a = [2.0, 5.0, 7.0, 9.0]
    epoch_a = nept.Epoch(starts_a, stops_a)

    starts_b = [1.0, 4.0, 5.9, 7.8]
    stops_b = [2.0, 6.2, 6.2, 9.3]
    epoch_b = nept.Epoch(starts_b, stops_b)

    overlaps = epoch_a.overlaps(epoch_b)

    print(overlaps.starts)
    print(overlaps.stops)

    assert np.allclose(overlaps.starts, np.array([1.0, 4.0, 7.8]))
    assert np.allclose(overlaps.stops, np.array([2.0, 6.2, 9.3]))
Ejemplo n.º 19
0
def get_epoch_from_zscored_thresh(power_lfp, thresh, times_for_z):
    if times_for_z is not None:
        # Apply zscore thresh to restricted data to find power thresh
        sliced_power_lfp = power_lfp.time_slice(times_for_z.start, times_for_z.stop)
        zpower = scipy.stats.zscore(np.squeeze(sliced_power_lfp.data))

        zthresh_idx = (np.abs(zpower - thresh)).argmin()
        power_thresh = sliced_power_lfp.data[zthresh_idx][0]
    else:
        zpower = scipy.stats.zscore(np.squeeze(power_lfp.data))
        zthresh_idx = (np.abs(zpower - thresh)).argmin()
        power_thresh = power_lfp.data[zthresh_idx][0]

    # Finding locations where the power changes
    detect = np.squeeze(power_lfp.data) > power_thresh
    detect = np.hstack([0, detect, 0])  # pad to detect first or last element change
    signal_change = np.diff(detect.astype(int))

    start_swr_idx = np.where(signal_change == 1)[0]
    stop_swr_idx = np.where(signal_change == -1)[0]

    # Getting times associated with these power changes
    start_time = power_lfp.time[start_swr_idx]
    stop_time = power_lfp.time[stop_swr_idx]

    return nept.Epoch(start_time, stop_time)
Ejemplo n.º 20
0
def test_epoch_index():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 1.5, 2.0]
    epoch = nept.Epoch(starts, stops)
    sliced_epoch = epoch[:2]

    assert np.allclose(sliced_epoch.starts, np.array([0., 0.9]))
    assert np.allclose(sliced_epoch.stops, np.array([1., 1.5]))
Ejemplo n.º 21
0
def test_epoch_merge_overlap():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 1.5, 2.0]
    epoch = nept.Epoch(starts, stops)

    merged = epoch.merge()
    assert np.allclose(merged.starts, np.array([0., 1.6]))
    assert np.allclose(merged.stops, np.array([1.5, 2.0]))
Ejemplo n.º 22
0
def test_epoch_iter():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 1.5, 2.0]
    epoch = nept.Epoch(starts, stops)

    epoch_iter = iter(epoch)

    assert isinstance(epoch_iter, collections.Iterable)
Ejemplo n.º 23
0
def test_epoch_merge_with_gap():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 1.5, 2.0]
    epoch = nept.Epoch(starts, stops)

    merged = epoch.merge(gap=0.1)
    assert np.allclose(merged.starts, np.array([0.]))
    assert np.allclose(merged.stops, np.array([2.0]))
Ejemplo n.º 24
0
def test_epoch_stop_before_start():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 0.2, 2.0]

    with pytest.raises(ValueError) as excinfo:
        epoch = nept.Epoch(starts, stops)

    assert str(excinfo.value) == "start must be less than stop"
Ejemplo n.º 25
0
def test_epoch_ndim():
    starts = np.ones((2, 3, 4))
    stops = np.ones((2, 3, 4))*2

    with pytest.raises(ValueError) as excinfo:
        epoch = nept.Epoch(starts, stops)

    assert str(excinfo.value) == "time cannot have more than 1 dimension."
Ejemplo n.º 26
0
def test_epoch_merge_unordered_stops():
    starts = [-0.2, 0.0, 1.0, 2.2]
    stops = [0.8, 0.8, 3.6, 3.0]
    epoch = nept.Epoch(starts, stops)

    merged = epoch.merge()
    assert np.allclose(merged.starts, np.array([-0.2, 1.]))
    assert np.allclose(merged.stops, np.array([0.8, 3.6]))
Ejemplo n.º 27
0
def test_epoch_time_slice_cutoff():
    starts = [0.9, 1.5]
    stops = [1.6, 3.0]
    epoch = nept.Epoch(starts, stops)

    sliced_epoch = epoch.time_slice(0.1, 1.0)

    assert np.allclose(sliced_epoch.starts, np.array([0.9]))
    assert np.allclose(sliced_epoch.stops, np.array([1.0]))
Ejemplo n.º 28
0
def test_epoch_time_slice_empty():
    starts = [0.9, 1.6]
    stops = [1.5, 3.0]
    epoch = nept.Epoch(starts, stops)

    sliced_epoch = epoch.time_slice(0.1, 0.5)

    assert np.allclose(sliced_epoch.starts, np.array([]))
    assert np.allclose(sliced_epoch.stops, np.array([]))
Ejemplo n.º 29
0
def test_epoch_time_slice_overlap():
    starts = [0.0, 0.9, 1.6]
    stops = [1.1, 1.5, 3.0]
    epoch = nept.Epoch(starts, stops)

    sliced_epoch = epoch.time_slice(1, 2)

    assert np.allclose(sliced_epoch.starts, np.array([1., 1., 1.6]))
    assert np.allclose(sliced_epoch.stops, np.array([1.1, 1.5, 2.]))
Ejemplo n.º 30
0
def test_epoch_shrink_toobig_single():
    starts = [0.0, 0.9, 1.6]
    stops = [1.0, 1.5, 2.0]
    epoch = nept.Epoch(starts, stops)

    with pytest.raises(ValueError) as excinfo:
        shrinked = epoch.shrink(1., direction='start')

    assert str(excinfo.value) == "shrink amount too large"