Skip to content

Commit

Permalink
[DOC,MNT,BUG] Documentation and testing for downsample.py (#45)
Browse files Browse the repository at this point in the history
* 1.Fixes a bug in downsample.py when
n_timepoints / new_ratio is an integer
2.complete a testing for downsample.py
in transformations/tests/test_downsample.py
3.write documentation for transformations/downsample.py

* 1.Fixes a bug in downsample.py
when n_timepoints / new_ratio is an integer
2.complete a testing for downsample.py
in transformations/tests/test_downsample.py
3.write documentation for transformations/downsample.py

* modify the variable names
both in the code and comments

---------

Co-authored-by: futuer <[email protected]>
  • Loading branch information
futuer-szd and futuer committed Jun 14, 2024
1 parent 7e4966e commit f051bff
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
29 changes: 24 additions & 5 deletions aeon_neuro/transformations/downsample.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
"""Downsample series by frequency."""

import numpy as np


def downsample_series(X, sfreq, target_sample_rate):
"""Downsample a time series.
Parameters
----------
X : a numpy array of shape = [n_cases, n_channels, n_timepoints]
The input time series data to be downsampled.
sfreq : int
The original sampling frequency of the time series data.
target_sample_rate : int
The target sampling frequency after downsampling.
Returns
-------
downsampled : a numpy array of
shape = [n_cases, n_channels, updated_timepoints]
The downsampled time series data.
"""
new_ratio = int(sfreq / target_sample_rate)
n_instances, n_dimensions, n_timepoints = np.shape(X)
updated_timepoints = int(n_timepoints / new_ratio)
downsampled_data = np.zeros((n_instances, n_dimensions, updated_timepoints + 1))
for i in range(n_instances):
for j in range(n_dimensions):
n_cases, n_channels, n_timepoints = np.shape(X)
updated_timepoints = int(np.ceil(n_timepoints / new_ratio))
downsampled_data = np.zeros((n_cases, n_channels, updated_timepoints))
for i in range(n_cases):
for j in range(n_channels):
updated_index = 0
for k in range(0, n_timepoints, new_ratio):
downsampled_data[i][j][updated_index] = X[i][j][k]
Expand Down
1 change: 1 addition & 0 deletions aeon_neuro/transformations/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit tests of transformations."""
46 changes: 46 additions & 0 deletions aeon_neuro/transformations/tests/test_downsample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Tests for downsample_series."""

import numpy as np
import pytest

from aeon_neuro.transformations.downsample import downsample_series

X = np.array(
[
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]],
[
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10],
],
]
)

testdata = [
(
X,
10,
5,
np.array(
[
[[1, 3, 5, 7, 9], [10, 8, 6, 4, 2]],
[[10, 30, 50, 70, 90], [100, 80, 60, 40, 20]],
]
),
),
(
X,
10,
3,
np.array(
[[[1, 4, 7, 10], [10, 7, 4, 1]], [[10, 40, 70, 100], [100, 70, 40, 10]]]
),
),
(X, 10, 2, np.array([[[1, 6], [10, 5]], [[10, 60], [100, 50]]])),
]


@pytest.mark.parametrize("X, sfreq, target_sample_rate, expected", testdata)
def test_downsample_series(X, sfreq, target_sample_rate, expected):
"""Test the downsampling of a time series."""
result = downsample_series(X, sfreq, target_sample_rate)
np.testing.assert_array_equal(result, expected)

0 comments on commit f051bff

Please sign in to comment.