Skip to content

Commit

Permalink
Merge pull request #2 from myokit/imin-imax
Browse files Browse the repository at this point in the history
Added imin and imax methods.
  • Loading branch information
MichaelClerx authored Dec 6, 2024
2 parents 895c24d + 25fb0e5 commit 0cf75bc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
2 changes: 2 additions & 0 deletions datkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

from ._points import ( # noqa
abs_max_on,
imax_on,
imin_on,
index,
index_near,
index_on,
Expand Down
26 changes: 25 additions & 1 deletion datkit/_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def abs_max_on(times, values, t0=None, t1=None, include_left=True,
include_right=False):
"""
Returns a tuple ``(t_max, v_max)`` corresponding to the maximum value in
``values`` on the interval from ``t0`` to ``t1``.
``abs(values)`` on the interval from ``t0`` to ``t1``.
See also :meth:`index_on`.
"""
Expand All @@ -22,6 +22,30 @@ def abs_max_on(times, values, t0=None, t1=None, include_left=True,
return times[i], values[i]


def imax_on(times, values, t0=None, t1=None, include_left=True,
include_right=False):
"""
Returns the index ``i`` corresponding to the maximum value on the interval
from ``t0`` to ``t1``.
See also :meth:`index_on`.
"""
i, j = index_on(times, t0, t1, include_left, include_right)
return i + np.argmax(values[i:j])


def imin_on(times, values, t0=None, t1=None, include_left=True,
include_right=False):
"""
Returns the index ``i`` corresponding to the maximum value on the interval
from ``t0`` to ``t1``.
See also :meth:`index_on`.
"""
i, j = index_on(times, t0, t1, include_left, include_right)
return i + np.argmin(values[i:j])


def index(times, t, ttol=1e-9):
"""
Returns the index of time ``t`` in ``times``.
Expand Down
5 changes: 3 additions & 2 deletions datkit/_smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def haar_downsample(times, values, repeats=1):
- If the signal length is odd, the final sample is omitted
- The first two samples are averaged, then the next two, etc.
Returns a new and downsampled time series ``(times_2, values_2)``.
Returns a new and downsampled time series ``(times_2, values_2)`` of length
``len(times) // 2**repeats``.
"""
times, values = np.asarray(times), np.asarray(values)
if len(times) != len(values):
Expand Down Expand Up @@ -98,7 +99,7 @@ def window_size(times, w=None, t=None):
dt = d.sampling_interval(times)
if t / dt < 2.9:
raise ValueError(
'Invalid window size: must be at least 3 times the samplig'
'Invalid window size: must be at least 3 times the sampling'
f' interval {dt}.')

w = int(1 + 2 * ((t / dt) // 2))
Expand Down
19 changes: 18 additions & 1 deletion datkit/tests/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ def test_abs_max_on(self):
self.assertEqual(d.abs_max_on(t, v, 1.5, 2), (t[99], v[99]))
self.assertEqual(d.abs_max_on(t, v, 1.5, 2, False, True), (2, 1))

def test_imax_on(self):
t = np.linspace(0, 2, 101)
v = np.cos(t * np.pi)
self.assertEqual(d.imax_on(t, v, 0, 1), 0)
self.assertEqual(d.imax_on(t, v, 0.5, 1), 25)
self.assertEqual(d.imax_on(t, v, 0.6, 1.5), 74)
self.assertEqual(d.imax_on(t, v, 1.5, 2), 99)
self.assertEqual(d.imax_on(t, v, 1.5, 2, False, True), 100)

def test_imin_on(self):
t = np.linspace(0, 2, 101)
v = np.cos(t * np.pi)
self.assertEqual(d.imin_on(t, v, 0, 1), 49)
self.assertEqual(d.imin_on(t, v, 0.5, 2), 50)
self.assertEqual(d.imin_on(t, v, 0.5, 1.5), 50)
self.assertEqual(d.imin_on(t, v, 1.5, 2), 75)
self.assertEqual(d.imin_on(t, v, 1.5, 2, False), 76)

def test_index(self):

# Simple tests
Expand Down Expand Up @@ -164,7 +182,6 @@ def test_index_on(self):

# Values not specified
t = np.arange(0, 20, 2)
print(t)
self.assertEqual(d.index_on(t), (0, 10))
self.assertEqual(d.index_on(t, include_left=False), (0, 10))
self.assertEqual(d.index_on(t, include_right=True), (0, 10))
Expand Down
4 changes: 4 additions & 0 deletions docs/source/finding_points.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Finding points

.. autofunction:: abs_max_on

.. autofunction:: imax_on

.. autofunction:: imin_on

9 changes: 5 additions & 4 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
************
Introduction
************
*****************
Table of contents
*****************

.. module:: datkit

Some silly scripts to do stuff I do all the time.
This module contains some simple methods that are useful when analysing time
series data.

.. toctree::

Expand Down

0 comments on commit 0cf75bc

Please sign in to comment.