Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data type check for padding short complex pings #1353

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
leewujung marked this conversation as resolved.
Show resolved Hide resolved

# 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
)