Skip to content

Commit

Permalink
[ENH] convert list of 1D numpy to 2D (#2378)
Browse files Browse the repository at this point in the history
* convert 1D numpy to 2D

* add further test
  • Loading branch information
TonyBagnall authored Nov 22, 2024
1 parent ee4fa27 commit e31eddf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aeon/base/_base_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Base class for estimators that fit collections of time series."""

import numpy as np

from aeon.base._base import BaseAeonEstimator
from aeon.utils.conversion import (
convert_collection,
Expand Down Expand Up @@ -79,6 +81,8 @@ def _preprocess_collection(self, X, store_metadata=True):
>>> X2.shape
(10, 1, 20)
"""
if isinstance(X, list) and isinstance(X[0], np.ndarray):
X = self._reshape_np_list(X)
meta = self._check_X(X)
if len(self.metadata_) == 0 and store_metadata:
self.metadata_ = meta
Expand Down Expand Up @@ -267,3 +271,20 @@ def _get_X_metadata(X):
None if metadata["unequal_length"] else get_n_timepoints(X)
)
return metadata

@staticmethod
def _reshape_np_list(X):
"""Reshape 1D numpy to be 2D."""
reshape = False
for x in X:
if x.ndim == 1:
reshape = True
break
if reshape:
X2 = []
for x in X:
if x.ndim == 1:
x = x.reshape(1, -1)
X2.append(x)
return X2
return X
21 changes: 21 additions & 0 deletions aeon/base/tests/test_base_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@ def test_preprocess_collection(data):
meta = cls.metadata_
cls._preprocess_collection(data2)
assert meta == cls.metadata_


def test_convert_np_list():
"""Test np-list of 1D numpy converted to 2D."""
x1 = np.random.random(size=(1, 10))
x2 = np.random.rand(20)
x3 = np.random.rand(30)
np_list = [x1, x2, x3]
np2 = BaseCollectionEstimator._reshape_np_list(np_list)
assert len(np2) == len(np_list)
assert np2[0].shape == (1, 10)
assert np2[1].shape == (1, 20)
assert np2[2].shape == (1, 30)
dummy1 = BaseCollectionEstimator()
x1 = np.random.random(size=(1, 10))
x2 = np.random.rand(10)
x3 = np.random.rand(10)
np_list = [x1, x2, x3]
np3 = dummy1._preprocess_collection(np_list)
assert isinstance(np3, np.ndarray)
assert np3.shape == (3, 1, 10)

0 comments on commit e31eddf

Please sign in to comment.