Skip to content

Commit

Permalink
Further adapted start_end_array_to_bool_array to fit Zupt detector us…
Browse files Browse the repository at this point in the history
…ecase
  • Loading branch information
AKuederle committed Feb 10, 2023
1 parent c88bd5e commit c02294f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ project.
- The util method `start_end_array_to_bool_array` now assumes that the end index of all regions is inclusive.
This enables roundtrip conversion with the `bool_array_to_start_end_array` method and is in line with the definitions
used for strides, ROIs, and ZUPTs in gaitmap.
Further, the method now supports to output arrays that are shorter than the largest input index.
Before, this resulted in an error.
Both changes might require some user facing code changes, if this function is used.
However, as it was not used internally, it is likely that no one was using it anyway.
(https://github.com/mad-lab-fau/gaitmap/pull/14)


Expand Down
15 changes: 8 additions & 7 deletions gaitmap/utils/array_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ def start_end_array_to_bool_array(start_end_array: np.ndarray, pad_to_length: in
This is in line with the definitions of stride and roi lists in gaitmap.
pad_to_length: int
define length of resulting array if None is given the array will have the length of the last element of the
initial start_end_array
Define the length of the resulting array.
If None, the array will have the length of the largest index.
Otherwise, the final array will either be padded with False or truncated to the specified length.
Returns
-------
Expand All @@ -167,11 +168,11 @@ def start_end_array_to_bool_array(start_end_array: np.ndarray, pad_to_length: in
"""
start_end_array = np.atleast_2d(start_end_array)

n_elements = start_end_array.max()

if pad_to_length:
if pad_to_length <= n_elements:
raise ValueError("Padding length must be larger than last element of start end array!")
if pad_to_length is None:
n_elements = start_end_array.max()
else:
if pad_to_length < 0:
raise ValueError("pad_to_length must be positive!")
n_elements = pad_to_length

bool_array = np.zeros(n_elements)
Expand Down
3 changes: 2 additions & 1 deletion gaitmap/zupt_detection/_combo_zupt_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def detect(
Returns
-------
self
The fitted instance
The class instance with all result attributes populated
"""
if not self.detectors:
raise ValueError("No detectors have been set.")
Expand Down
8 changes: 7 additions & 1 deletion tests/test_utils/test_array_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,13 @@ def test_invalid_padding(self):
with pytest.raises(ValueError) as e:
start_end_array_to_bool_array(input_array, pad_to_length=-1)

assert "Padding length must be larger than" in str(e)
assert "pad_to_length must be positive" in str(e)

def test_short_padding(self):
input_array = np.array([[2, 3], [5, 9]])
output_array = start_end_array_to_bool_array(input_array, pad_to_length=7)
expected_output = np.array([0, 0, 1, 0, 0, 1, 1]).astype(bool)
assert_array_equal(expected_output, output_array)

def test_correct_output_dtype(self):
input_array = np.array([[2, 3], [5, 9]])
Expand Down

0 comments on commit c02294f

Please sign in to comment.