Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #194

Merged
merged 3 commits into from
Oct 30, 2023
Merged

Dev #194

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ Around 2016-2017, Luke Sjulson started *TSToolbox2*, still in Matlab and which i
In 2018, Francesco started neuroseries, a Python package built on Pandas. It was quickly adopted in Adrien's lab, especially by Guillaume Viejo, a postdoc in the lab. Gradually, the majority of the lab was using it and new functions were constantly added.
In 2021, Guillaume and other trainees in Adrien's lab decided to fork from neuroseries and started *pynapple*. The core of pynapple is largely built upon neuroseries. Some of the original changes to TSToolbox made by Luke were included in this package, especially the *time_support* property of all ts/tsd objects.

0.4.1 (2023-10-30)
------------------

- Implementing `get` method that return both an interval or the closest timepoint


0.4.0 (2023-10-11)
------------------

- Implementing the numpy array container approach within pynapple
- TsdTensor for objects larger than 2 dimensions is now available


0.3.6 (2023-09-11)
------------------

Expand Down
2 changes: 1 addition & 1 deletion pynapple/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.0"
__version__ = "0.4.1"
from .core import *
from .io import *
from .process import *
35 changes: 25 additions & 10 deletions pynapple/core/time_series.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# @Author: gviejo
# @Date: 2022-01-27 18:33:31
# @Last Modified by: gviejo
# @Last Modified time: 2023-10-27 14:26:55
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2023-10-30 16:43:22

"""

Expand Down Expand Up @@ -754,10 +754,11 @@ def find_support(self, min_gap, time_units="s"):

return IntervalSet(start=starts, end=ends)

def get(self, start, end, time_units="s"):
def get(self, start, end=None, time_units="s"):
"""Slice the time series from start to end such that all the timestamps satisfy start<=t<=end.
If end is None, only the timepoint closest to start is returned.

By default, the time support doesn't change. If you want to change the
By default, the time support doesn't change. If you want to change the time support, use the restrict function.

Parameters
----------
Expand All @@ -767,13 +768,27 @@ def get(self, start, end, time_units="s"):
The end
"""
assert isinstance(start, Number), "start should be a float or int"
assert isinstance(end, Number), "end should be a float or int"
assert start < end, "Start should not precede end"
start, end = TsIndex.format_timestamps(np.array([start, end]), time_units)
time_array = self.index.values
idx_start = np.searchsorted(time_array, start)
idx_end = np.searchsorted(time_array, end, side="right")
return self[idx_start:idx_end]

if end is None:
start = TsIndex.format_timestamps(np.array([start]), time_units)[0]
idx = np.searchsorted(time_array, start)
if idx == 0:
return self[idx]
elif idx >= self.shape[0]:
return self[-1]
else:
if start - time_array[idx - 1] < time_array[idx] - start:
return self[idx - 1]
else:
return self[idx]
else:
assert isinstance(end, Number), "end should be a float or int"
assert start < end, "Start should not precede end"
start, end = TsIndex.format_timestamps(np.array([start, end]), time_units)
idx_start = np.searchsorted(time_array, start)
idx_end = np.searchsorted(time_array, end, side="right")
return self[idx_start:idx_end]


class TsdTensor(NDArrayOperatorsMixin, _AbstractTsd):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pynapple"
version = "0.4.0"
version = "0.4.1"
description = "PYthon Neural Analysis Package Pour Laboratoires d’Excellence"
readme = "README.md"
authors = [{ name = "Guillaume Viejo", email = "[email protected]" }]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/pynapple-org/pynapple',
version='v0.4.0',
version='v0.4.1',
zip_safe=False,
long_description_content_type='text/markdown',
download_url='https://github.com/pynapple-org/pynapple/archive/refs/tags/v0.4.0.tar.gz'
download_url='https://github.com/pynapple-org/pynapple/archive/refs/tags/v0.4.1.tar.gz'
)
15 changes: 12 additions & 3 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: gviejo
# @Last Modified time: 2023-10-29 16:19:01
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2023-10-30 16:39:56
#!/usr/bin/env python

"""Tests of time series for `pynapple` package."""
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_restrict_inherit_time_support(self, tsd):
np.testing.assert_approx_equal(tsd2.time_support.start[0], ep.start[0])
np.testing.assert_approx_equal(tsd2.time_support.end[0], ep.end[0])

def test_get(self, tsd):
def test_get_interval(self, tsd):
tsd2 = tsd.get(10, 20)
assert len(tsd2) == 11
np.testing.assert_array_equal(tsd2.index.values, tsd.index.values[10:21])
Expand All @@ -383,6 +383,15 @@ def test_get(self, tsd):
with pytest.raises(Exception):
tsd.get([10], 20)

def test_get_timepoint(self, tsd):
if not isinstance(tsd, nap.Ts):
np.testing.assert_array_equal(tsd.get(-1), tsd[0])
np.testing.assert_array_equal(tsd.get(0), tsd[0])
np.testing.assert_array_equal(tsd.get(0.1), tsd[0])
np.testing.assert_array_equal(tsd.get(0.5), tsd[1])
np.testing.assert_array_equal(tsd.get(0.6), tsd[1])
np.testing.assert_array_equal(tsd.get(1), tsd[1])
np.testing.assert_array_equal(tsd.get(1000), tsd[-1])

####################################################
# Test for tsd
Expand Down
Loading