Skip to content

Commit

Permalink
Merge pull request #189 from pynapple-org/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gviejo authored Oct 18, 2023
2 parents 4d64d54 + a46545d commit f551213
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
6 changes: 2 additions & 4 deletions pynapple/core/interval_set.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# @Date: 2022-01-25 21:50:48
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2023-09-21 16:01:25
# @Last Modified by: gviejo
# @Last Modified time: 2023-10-15 16:18:42

"""
"""
Expand All @@ -15,8 +15,6 @@
from numba import jit

from .jitted_functions import jitdiff, jitin_interval, jitintersect, jitunion

# from .time_units import format_timestamps, return_timestamps, sort_timestamps
from .time_index import TsIndex

all_warnings = np.array(
Expand Down
4 changes: 2 additions & 2 deletions pynapple/core/jitted_functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# @Author: guillaume
# @Date: 2022-10-31 16:44:31
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2023-09-21 18:00:13
# @Last Modified by: gviejo
# @Last Modified time: 2023-10-15 16:05:27
import numpy as np
from numba import jit

Expand Down
56 changes: 36 additions & 20 deletions pynapple/core/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: gviejo
# @Date: 2022-01-27 18:33:31
# @Last Modified by: gviejo
# @Last Modified time: 2023-10-13 11:19:12
# @Last Modified time: 2023-10-18 11:16:43

"""
Expand Down Expand Up @@ -171,6 +171,10 @@ def shape(self):
def ndim(self):
return self.values.ndim

@property
def size(self):
return self.values.size

def __repr__(self):
return str(self.__class__)

Expand Down Expand Up @@ -719,6 +723,37 @@ def copy(self):
t=self.index.copy(), d=self.values.copy(), time_support=self.time_support
)

def find_support(self, min_gap, time_units="s"):
"""
find the smallest (to a min_gap resolution) IntervalSet containing all the times in the Tsd
Parameters
----------
min_gap : float
minimal interval between timestamps
time_units : str, optional
Time units of min gap
Returns
-------
IntervalSet
Description
"""
assert isinstance(min_gap, Number), "min_gap should be a float or int"
min_gap = TsIndex.format_timestamps(np.array([min_gap]), time_units)[0]
time_array = self.index.values

starts = [time_array[0]]
ends = []
for i in range(len(time_array) - 1):
if (time_array[i + 1] - time_array[i]) > min_gap:
ends.append(time_array[i] + 1e-6)
starts.append(time_array[i + 1])

ends.append(time_array[-1] + 1e-6)

return IntervalSet(start=starts, end=ends)


class TsdTensor(NDArrayOperatorsMixin, _AbstractTsd):
"""
Expand Down Expand Up @@ -1779,22 +1814,3 @@ def save(self, filename):
# s, e = jitfind_gaps(time_array, starts, ends, min_gap)

# return nap.IntervalSet(s, e)

# def find_support(self, min_gap, method="absolute"):
# """
# find the smallest (to a min_gap resolution) IntervalSet containing all the times in the Tsd

# Parameters
# ----------
# min_gap : float
# Description
# method : str, optional
# Description

# Returns
# -------
# TYPE
# Description
# """
# print("TODO")
# return
36 changes: 34 additions & 2 deletions tests/test_time_series.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# @Author: gviejo
# @Date: 2022-04-01 09:57:55
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2023-09-25 16:01:32
# @Last Modified by: gviejo
# @Last Modified time: 2023-10-18 11:18:07
#!/usr/bin/env python

"""Tests of time series for `pynapple` package."""
Expand Down Expand Up @@ -213,6 +213,38 @@ def test_index_error():
with pytest.raises(IndexError):
ts[1000]

def test_find_support():
tsd = nap.Tsd(t=np.arange(100), d=np.arange(100))
ep = tsd.find_support(1.0)
assert ep.loc[0, 'start'] == 0
assert ep.loc[0, 'end'] == 99.0 + 1e-6

t = np.hstack((np.arange(10), np.arange(20, 30)))
tsd = nap.Tsd(t=t, d=np.arange(20))
ep = tsd.find_support(1.0)
np.testing.assert_array_equal(ep.start.values, np.array([0.0, 20.0]))
np.testing.assert_array_equal(ep.end.values, np.array([9.0+1e-6, 29+1e-6]))

def test_properties():
t = np.arange(100)
d = np.random.rand(100)
tsd = nap.Tsd(t=t, d = d)

assert hasattr(tsd, "t")
assert hasattr(tsd, "d")
assert hasattr(tsd, "start")
assert hasattr(tsd, "end")
assert hasattr(tsd, "shape")
assert hasattr(tsd, "ndim")
assert hasattr(tsd, "size")

np.testing.assert_array_equal(tsd.t, t)
np.testing.assert_array_equal(tsd.d, d)
assert tsd.start == 0.0
assert tsd.end == 99.0
assert tsd.shape == (100,)
assert tsd.ndim == 1
assert tsd.size == 100

####################################################
# General test for time series
Expand Down

0 comments on commit f551213

Please sign in to comment.