-
Notifications
You must be signed in to change notification settings - Fork 81
Move preprocess
functions and tests new clean
and commongrid
subpackages
#993
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
930054e
Move noise functions and tests from preprocess to new filter subpackage
emiliom d85bb85
Fix docstring for new filter/api.py module
emiliom c328426
Add missing estimate_noise to filter __init__
emiliom a047558
Move remaining preprocess modules and test to commongrid; update all …
emiliom 6a1aa05
Update provenance attribute strings to replace preprocess with common…
emiliom 63c3ccd
Create stub preprocess subpackage so function calls still work, with …
emiliom 747c1f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 811d0d2
Rename new 'filter' subpackage to 'clean', to avoid Python reserved word
emiliom 3dc0f55
Solve merge conflicts with origin branch
emiliom 295111b
Merge branch 'dev' into preprocess-reorg
leewujung b5ab430
Update echopype/clean/api.py
emiliom b37636d
Update .ci_helpers/run-test.py
emiliom d35ae8f
Update echopype/commongrid/api.py
emiliom 9ee7ddc
Update echopype/__init__.py
emiliom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .api import remove_noise | ||
|
||
__all__ = [ | ||
"remove_noise", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Functions for enhancing the spatial and temporal coherence of data. | ||
""" | ||
|
||
from ..utils.prov import echopype_prov_attrs | ||
from .noise_est import NoiseEst | ||
|
||
|
||
def estimate_noise(ds_Sv, ping_num, range_sample_num, noise_max=None): | ||
""" | ||
Remove noise by using estimates of background noise | ||
from mean calibrated power of a collection of pings. | ||
|
||
See ``remove_noise`` for reference. | ||
|
||
Parameters | ||
---------- | ||
ds_Sv : xr.Dataset | ||
dataset containing ``Sv`` and ``echo_range`` [m] | ||
ping_num : int | ||
number of pings to obtain noise estimates | ||
range_sample_num : int | ||
number of samples along the ``range_sample`` dimension to obtain noise estimates | ||
noise_max : float | ||
the upper limit for background noise expected under the operating conditions | ||
|
||
Returns | ||
------- | ||
A DataArray containing noise estimated from the input ``ds_Sv`` | ||
""" | ||
noise_obj = NoiseEst(ds_Sv=ds_Sv.copy(), ping_num=ping_num, range_sample_num=range_sample_num) | ||
noise_obj.estimate_noise(noise_max=noise_max) | ||
return noise_obj.Sv_noise | ||
|
||
|
||
def remove_noise(ds_Sv, ping_num, range_sample_num, noise_max=None, SNR_threshold=3): | ||
""" | ||
Remove noise by using estimates of background noise | ||
from mean calibrated power of a collection of pings. | ||
|
||
Reference: De Robertis & Higginbottom. 2007. | ||
A post-processing technique to estimate the signal-to-noise ratio | ||
and remove echosounder background noise. | ||
ICES Journal of Marine Sciences 64(6): 1282–1291. | ||
|
||
Parameters | ||
---------- | ||
ds_Sv : xr.Dataset | ||
dataset containing ``Sv`` and ``echo_range`` [m] | ||
ping_num : int | ||
number of pings to obtain noise estimates | ||
range_sample_num : int | ||
number of samples along the ``range_sample`` dimension to obtain noise estimates | ||
noise_max : float | ||
the upper limit for background noise expected under the operating conditions | ||
SNR_threshold : float | ||
acceptable signal-to-noise ratio, default to 3 dB | ||
|
||
Returns | ||
------- | ||
The input dataset with additional variables, including | ||
the corrected Sv (``Sv_corrected``) and the noise estimates (``Sv_noise``) | ||
""" | ||
noise_obj = NoiseEst(ds_Sv=ds_Sv.copy(), ping_num=ping_num, range_sample_num=range_sample_num) | ||
noise_obj.remove_noise(noise_max=noise_max, SNR_threshold=SNR_threshold) | ||
ds_Sv = noise_obj.ds_Sv | ||
|
||
prov_dict = echopype_prov_attrs(process_type="processing") | ||
prov_dict["processing_function"] = "preprocess.remove_noise" | ||
ds_Sv = ds_Sv.assign_attrs(prov_dict) | ||
|
||
return ds_Sv |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from .api import compute_MVBS, compute_MVBS_index_binning, remove_noise | ||
from .api import compute_MVBS, compute_MVBS_index_binning | ||
|
||
__all__ = [ | ||
"compute_MVBS", | ||
"compute_MVBS_index_binning", | ||
"remove_noise", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import numpy as np | ||
import xarray as xr | ||
import echopype as ep | ||
import pytest | ||
from numpy.random import default_rng | ||
|
||
|
||
def test_remove_noise(): | ||
"""Test remove_noise on toy data""" | ||
|
||
# Parameters for fake data | ||
nchan, npings, nrange_samples = 1, 10, 100 | ||
chan = np.arange(nchan).astype(str) | ||
ping_index = np.arange(npings) | ||
range_sample = np.arange(nrange_samples) | ||
data = np.ones(nrange_samples) | ||
|
||
# Insert noise points | ||
np.put(data, 30, -30) | ||
np.put(data, 60, -30) | ||
# Add more pings | ||
data = np.array([data] * npings) | ||
# Make DataArray | ||
Sv = xr.DataArray( | ||
[data], | ||
coords=[ | ||
('channel', chan), | ||
('ping_time', ping_index), | ||
('range_sample', range_sample), | ||
], | ||
) | ||
Sv.name = "Sv" | ||
ds_Sv = Sv.to_dataset() | ||
|
||
ds_Sv = ds_Sv.assign( | ||
echo_range=xr.DataArray( | ||
np.array([[np.linspace(0, 10, nrange_samples)] * npings]), | ||
coords=Sv.coords, | ||
) | ||
) | ||
ds_Sv = ds_Sv.assign(sound_absorption=0.001) | ||
# Run noise removal | ||
ds_Sv = ep.filter.remove_noise( | ||
ds_Sv, ping_num=2, range_sample_num=5, SNR_threshold=0 | ||
) | ||
|
||
# Test if noise points are nan | ||
assert np.isnan( | ||
ds_Sv.Sv_corrected.isel(channel=0, ping_time=0, range_sample=30) | ||
) | ||
assert np.isnan( | ||
ds_Sv.Sv_corrected.isel(channel=0, ping_time=0, range_sample=60) | ||
) | ||
|
||
# Test remove noise on a normal distribution | ||
np.random.seed(1) | ||
data = np.random.normal( | ||
loc=-100, scale=2, size=(nchan, npings, nrange_samples) | ||
) | ||
# Make Dataset to pass into remove_noise | ||
Sv = xr.DataArray( | ||
data, | ||
coords=[ | ||
('channel', chan), | ||
('ping_time', ping_index), | ||
('range_sample', range_sample), | ||
], | ||
) | ||
Sv.name = "Sv" | ||
ds_Sv = Sv.to_dataset() | ||
# Attach required echo_range and sound_absorption values | ||
ds_Sv = ds_Sv.assign( | ||
echo_range=xr.DataArray( | ||
np.array([[np.linspace(0, 3, nrange_samples)] * npings]), | ||
coords=Sv.coords, | ||
) | ||
) | ||
ds_Sv = ds_Sv.assign(sound_absorption=0.001) | ||
# Run noise removal | ||
ds_Sv = ep.filter.remove_noise( | ||
ds_Sv, ping_num=2, range_sample_num=5, SNR_threshold=0 | ||
) | ||
null = ds_Sv.Sv_corrected.isnull() | ||
# Test to see if the right number of points are removed before the range gets too large | ||
assert ( | ||
np.count_nonzero(null.isel(channel=0, range_sample=slice(None, 50))) | ||
== 6 | ||
) | ||
|
||
|
||
def test_remove_noise_no_sound_absorption(): | ||
""" | ||
Tests remove_noise on toy data that does | ||
not have sound absorption as a variable. | ||
""" | ||
|
||
pytest.xfail(f"Tests for remove_noise have not been implemented" + | ||
" when no sound absorption is provided!") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.