Skip to content

Commit

Permalink
Padding Short Pings (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctuguinay authored Jul 11, 2024
1 parent 872dd03 commit a2a98b6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
10 changes: 6 additions & 4 deletions echopype/convert/parse_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,15 @@ def pad_shorter_ping(data_list) -> np.ndarray:
# Create output array from mask
out_array = np.full(mask.shape, np.nan)

# Concatenate short pings
concat_short_pings = np.concatenate(data_list).reshape(-1) # reshape in case data > 1D

# Take care of problem of np.nan being implicitly "real"
arr_dtype = data_list[0].dtype
if np.issubdtype(arr_dtype, np.complex_):
out_array = out_array.astype(arr_dtype)
if concat_short_pings.dtype == np.complex64:
out_array = out_array.astype(np.complex64)

# Fill in values
out_array[mask] = np.concatenate(data_list).reshape(-1) # reshape in case data > 1D
out_array[mask] = concat_short_pings
else:
out_array = np.array(data_list)
return out_array
57 changes: 57 additions & 0 deletions echopype/tests/convert/test_convert_ek.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from echopype import open_raw
from echopype.convert.utils.ek_raw_io import RawSimradFile, SimradEOF
from echopype.convert.parse_base import ParseEK


def expected_array_shape(file, datagram_type, datagram_item):
Expand Down Expand Up @@ -151,3 +152,59 @@ def test_convert_ek_with_idx_file(file, sonar_model):
+ "This is different from longitude from the NMEA datagram.",
}
)


@pytest.mark.unit
def test_pad_short_complex_pings():
"""
Test padding of short complex pings.
"""
# Create empty parser
empty_parser = ParseEK(None, None, None, None, None)

# Create ping list with sizes 3, 2, 1 samples per ping
data_list = [
np.array(
[
1.0 + 2.5j,
7.3 - 3.2j,
3.2 - 9.1j,
],
dtype=np.complex64
),
np.array(
[
1.8 - 4.1j,
1.2 - 8.9j,
],
dtype=np.complex64
),
np.array(
[
6.1 - 4.8j,
],
dtype=np.complex64
),
]

# Pad shorter pings
output = empty_parser.pad_shorter_ping(data_list)

# Set expected output
expected_output = np.array(
[
[1. +2.5j, 7.3-3.2j, 3.2-9.1j],
[1.8-4.1j, 1.2-8.9j, np.nan+0.j ],
[6.1-4.8j, np.nan+0.j , np.nan+0.j ]
],
dtype=np.complex64
)
# Check dtype
assert output.dtype == np.complex64

# Check output against expected
assert np.allclose(
output,
expected_output,
equal_nan=True
)

0 comments on commit a2a98b6

Please sign in to comment.