Skip to content

Commit

Permalink
Merge pull request #232 from pynapple-org/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gviejo authored Feb 27, 2024
2 parents f441ec5 + 84ccfbb commit 8302918
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 245 deletions.
34 changes: 34 additions & 0 deletions pynapple/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,27 @@ class PynappleConfig:
array-like objects to NumPy arrays. This is useful for users who frequently work with array-like objects from other
libraries (e.g., JAX, TensorFlow) and prefer not to receive warnings for automatic
conversions. Defaults to False, which means warnings will be shown.
suppress_time_index_sorting_warnings : boolean
Control the warning raised when passing a non-sorted array for time index.
It can be useful to catch data where timestamps are not properly sorted before using pynapple.
time_index_precision : int
Precision for the time index is set to nanoseconds. It's a fixed parameter in pynapple and cannot be changed.
"""

def __init__(self):
self.suppress_conversion_warnings = False
self.suppress_time_index_sorting_warnings = False

@property
def time_index_precision(self):
"""Precision for the time index
Returns
-------
Int
Parameter for the `numpy.around` function when rounding time index
"""
return 9

@property
def suppress_conversion_warnings(self):
Expand All @@ -65,6 +82,22 @@ def suppress_conversion_warnings(self, value):
raise ValueError("suppress_conversion_warnings must be a boolean value.")
self._suppress_conversion_warnings = value

@property
def suppress_time_index_sorting_warnings(self):
"""
Gets or sets the suppression state for sorting time index. When set to True,
warnings for sorting are suppressed. Ensures that only boolean values are assigned.
"""
return self._suppress_time_index_sorting_warnings

@suppress_time_index_sorting_warnings.setter
def suppress_time_index_sorting_warnings(self, value):
if not isinstance(value, bool):
raise ValueError(
"suppress_time_index_sorting_warnings must be a boolean value."
)
self._suppress_time_index_sorting_warnings = value

def restore_defaults(self):
"""
Set all configuration settings to their default values.
Expand All @@ -73,6 +106,7 @@ def restore_defaults(self):
to its initial, default configuration.
"""
self.suppress_conversion_warnings = False
self.suppress_time_index_sorting_warnings = False


# Initialize a config instance
Expand Down
24 changes: 10 additions & 14 deletions pynapple/core/time_index.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# -*- coding: utf-8 -*-
# @Author: Guillaume Viejo
# @Date: 2023-09-21 13:32:03
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2024-02-13 16:55:44

"""
Similar to pandas.Index, TsIndex holds the timestamps associated with the data of a time series.
Similar to pandas.Index, `TsIndex` holds the timestamps associated with the data of a time series.
This class deals with conversion between different time units for all pynapple objects as well
as making sure that timestamps are property sorted before initializing any objects.
- `us`: microseconds
Expand All @@ -18,6 +12,8 @@

import numpy as np

from .config import nap_config


class TsIndex(np.ndarray):
"""
Expand Down Expand Up @@ -47,11 +43,11 @@ def format_timestamps(t, units="s"):
Description
"""
if units == "s":
t = np.around(t, 9)
t = np.around(t, nap_config.time_index_precision)
elif units == "ms":
t = np.around(t / 1.0e3, 9)
t = np.around(t / 1.0e3, nap_config.time_index_precision)
elif units == "us":
t = np.around(t / 1.0e6, 9)
t = np.around(t / 1.0e6, nap_config.time_index_precision)
else:
raise ValueError("unrecognized time units type")

Expand Down Expand Up @@ -80,11 +76,11 @@ def return_timestamps(t, units="s"):
IF units is not in ['s', 'ms', 'us']
"""
if units == "s":
t = np.around(t, 9)
t = np.around(t, nap_config.time_index_precision)
elif units == "ms":
t = np.around(t * 1.0e3, 9)
t = np.around(t * 1.0e3, nap_config.time_index_precision)
elif units == "us":
t = np.around(t * 1.0e6, 9)
t = np.around(t * 1.0e6, nap_config.time_index_precision)
else:
raise ValueError("unrecognized time units type")

Expand All @@ -108,7 +104,7 @@ def sort_timestamps(t, give_warning=True):
Description
"""
if not (np.diff(t) >= 0).all():
if give_warning:
if give_warning and not nap_config.suppress_time_index_sorting_warnings:
warn("timestamps are not sorted", UserWarning)
t = np.sort(t)
return t
Expand Down
225 changes: 0 additions & 225 deletions pynapple/core/time_units.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_abstract_tsd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# @Author: Guillaume Viejo
# @Date: 2023-09-25 11:53:30
# @Last Modified by: Guillaume Viejo
# @Last Modified time: 2024-02-13 17:33:25
# @Last Modified by: gviejo
# @Last Modified time: 2024-02-26 13:06:55

import pynapple as nap
import numpy as np
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ def test_config_supress_warning_d(cls, t, d, conf, expectation):
cls(t=t, d=MockArray(d))
finally:
nap.config.nap_config.restore_defaults()


def test_get_time_index_precision():
assert nap.config.nap_config.time_index_precision == 9
8 changes: 4 additions & 4 deletions tests/test_time_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: gviejo
# @Date: 2022-11-30 09:29:21
# @Last Modified by: gviejo
# @Last Modified time: 2023-11-08 17:16:26
# @Last Modified time: 2024-02-26 13:14:41
"""Tests of time units for `pynapple` package."""

import pynapple as nap
Expand All @@ -27,13 +27,13 @@ def test_return_timestamps():
t = np.random.rand(100)

np.testing.assert_array_almost_equal(t, nap.TsIndex.return_timestamps(t))
np.testing.assert_array_almost_equal(t/1e3, nap.TsIndex.return_timestamps(t, 'ms'))
np.testing.assert_array_almost_equal(t/1e6, nap.TsIndex.return_timestamps(t, 'us'))
np.testing.assert_array_almost_equal(t*1e3, nap.TsIndex.return_timestamps(t, 'ms'))
np.testing.assert_array_almost_equal(t*1e6, nap.TsIndex.return_timestamps(t, 'us'))

with pytest.raises(ValueError, match="unrecognized time units type"):
nap.TsIndex.return_timestamps(t, units='aaaa')

def test_return_timestamps():
def test_sort_timestamps():
t = np.random.rand(100)

np.testing.assert_array_almost_equal(np.sort(t), nap.TsIndex.sort_timestamps(t, False))
Expand Down

0 comments on commit 8302918

Please sign in to comment.