-
Notifications
You must be signed in to change notification settings - Fork 49
Improve flexibility and robustness of Acoustics Module #433
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
Open
jmcvey3
wants to merge
6
commits into
MHKiT-Software:develop
Choose a base branch
from
jmcvey3:acoustics-options
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−62
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f3405b
Change hardwired variables to function inputs, improve fmin/fmax checks
jmcvey3 085f56a
Use n_bin to define time now that n_fft can be adjusted
jmcvey3 593e198
Change 'nbin' to 'bin_length' and specify time interval in SPL attrib…
jmcvey3 c222670
Use 'time_resolution' instead of 'time_interval' for time over which …
jmcvey3 c514cda
Make sure test gets updated
jmcvey3 9cd0857
Fix one instance of check_numeric call input
jmcvey3 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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,8 @@ | |||||||||||||||||||||||||
| import xarray as xr | ||||||||||||||||||||||||||
| from scipy.io import wavfile | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from mhkit.acoustics.analysis import _check_numeric | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _read_wav_metadata(f: BinaryIO) -> dict: | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
|
|
@@ -132,10 +134,9 @@ def _calculate_voltage_and_time( | |||||||||||||||||||||||||
| raise TypeError("Raw audio data 'raw' must be a numpy.ndarray.") | ||||||||||||||||||||||||||
| if not isinstance(bits_per_sample, int): | ||||||||||||||||||||||||||
| raise TypeError("'bits_per_sample' must be an integer.") | ||||||||||||||||||||||||||
| if not isinstance(peak_voltage, (int, float)): | ||||||||||||||||||||||||||
| raise TypeError("'peak_voltage' must be numeric (int or float).") | ||||||||||||||||||||||||||
| if not isinstance(start_time, (str, np.datetime64)): | ||||||||||||||||||||||||||
| raise TypeError("'start_time' must be a string or np.datetime64.") | ||||||||||||||||||||||||||
| _check_numeric(peak_voltage, "peak_voltage") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| length = raw.shape[0] // fs # length of recording in seconds | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -196,15 +197,12 @@ def read_hydrophone( | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not isinstance(filename, (str, Path)): | ||||||||||||||||||||||||||
| raise TypeError("Filename must be a string or a pathlib.Path object.") | ||||||||||||||||||||||||||
| if not isinstance(peak_voltage, (int, float)): | ||||||||||||||||||||||||||
| raise TypeError("'peak_voltage' must be numeric (int or float).") | ||||||||||||||||||||||||||
| if sensitivity is not None and not isinstance(sensitivity, (int, float)): | ||||||||||||||||||||||||||
| raise TypeError("'sensitivity' must be numeric (int, float) or None.") | ||||||||||||||||||||||||||
| if not isinstance(gain, (int, float)): | ||||||||||||||||||||||||||
| raise TypeError("'gain' must be numeric (int or float).") | ||||||||||||||||||||||||||
| if sensitivity is not None: | ||||||||||||||||||||||||||
| _check_numeric(sensitivity, "sensitivity") | ||||||||||||||||||||||||||
| _check_numeric(peak_voltage, "peak_voltage") | ||||||||||||||||||||||||||
| _check_numeric(gain, "gain") | ||||||||||||||||||||||||||
| if not isinstance(start_time, (str, np.datetime64)): | ||||||||||||||||||||||||||
| raise TypeError("'start_time' must be a string or np.datetime64") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (sensitivity is not None) and (sensitivity > 0): | ||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||
| "Hydrophone calibrated sensitivity should be entered as a negative number." | ||||||||||||||||||||||||||
|
|
@@ -225,9 +223,9 @@ def read_hydrophone( | |||||||||||||||||||||||||
| # If sensitivity is provided, convert to sound pressure | ||||||||||||||||||||||||||
| if sensitivity is not None: | ||||||||||||||||||||||||||
| # Subtract gain | ||||||||||||||||||||||||||
| # Hydrophone with sensitivity of -177 dB and gain of -3 dB = sensitivity of -174 dB | ||||||||||||||||||||||||||
| # Hydrophone with sensitivity of -177 dB and gain of 3 dB = sensitivity of -174 dB | ||||||||||||||||||||||||||
| if gain: | ||||||||||||||||||||||||||
| sensitivity -= gain | ||||||||||||||||||||||||||
| sensitivity += gain | ||||||||||||||||||||||||||
| # Convert calibration from dB rel 1 V/uPa into ratio | ||||||||||||||||||||||||||
| sensitivity = 10 ** (sensitivity / 20) # V/uPa | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -511,25 +509,18 @@ def export_audio( | |||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||
| None | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not isinstance(filename, str): | ||||||||||||||||||||||||||
| raise TypeError("'filename' must be a string.") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not isinstance(pressure, xr.DataArray): | ||||||||||||||||||||||||||
| raise TypeError("'pressure' must be an xarray.DataArray.") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not hasattr(pressure, "values") or not isinstance(pressure.values, np.ndarray): | ||||||||||||||||||||||||||
| raise TypeError("'pressure.values' must be a numpy.ndarray.") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not hasattr(pressure, "sensitivity") or not isinstance( | ||||||||||||||||||||||||||
| pressure.sensitivity, (int, float) | ||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||
| raise TypeError("'pressure.sensitivity' must be a numeric type (int or float).") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not hasattr(pressure, "fs") or not isinstance(pressure.fs, (int, float)): | ||||||||||||||||||||||||||
| raise TypeError("'pressure.fs' must be a numeric type (int or float).") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not isinstance(gain, (int, float)): | ||||||||||||||||||||||||||
| raise TypeError("'gain' must be a numeric type (int or float).") | ||||||||||||||||||||||||||
| if not hasattr(pressure, "sensitivity"): | ||||||||||||||||||||||||||
| _check_numeric(pressure.sensitivity, "pressure.sensitivity") | ||||||||||||||||||||||||||
| if not hasattr(pressure, "fs"): | ||||||||||||||||||||||||||
| _check_numeric(pressure.fs, "pressure.fs") | ||||||||||||||||||||||||||
|
Comment on lines
+519
to
+522
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should only check that the values are numeric if they exist. If the values are given then the type is not checked:
Suggested change
|
||||||||||||||||||||||||||
| _check_numeric(gain, "gain") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Convert from Pascals to UPa | ||||||||||||||||||||||||||
| upa = pressure.values.T * 1e6 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add call to
_check_numericin place of the removed error check