-
Notifications
You must be signed in to change notification settings - Fork 280
Improve interface for DC Dipole source #1393
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a21febc
Improve interface for DC Dipole source
santisoler ad559ec
Update docstring of Dipole DC sources
santisoler 1dd4251
Test error after location with invalid length
santisoler 20a11a0
Merge branch 'main' into dc-dipole-source
santisoler fd40ca5
Replace SimPEG for simpeg in test
santisoler 920aef0
Update tests/em/static/test_dc_sources_interface.py
santisoler 7c48f31
Take current as a float for the Dipole source
santisoler 2cfff18
Update tests/em/static/test_dc_sources_interface.py
santisoler 48bd6fb
Merge branch 'main' into dc-dipole-source
santisoler f25e152
Fix location argument in docstring
santisoler 46558db
Improve docstring
santisoler e20f057
Mark `location` as optional
santisoler 74b813d
Merge branch 'main' into dc-dipole-source
santisoler 721deab
Merge branch 'main' into dc-dipole-source
santisoler 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| """ | ||
| Test interface for some DC sources. | ||
| """ | ||
|
|
||
santisoler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import pytest | ||
santisoler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import numpy as np | ||
| from simpeg.electromagnetics.static import resistivity as dc | ||
|
|
||
|
|
||
| class TestDipoleLocations: | ||
| r""" | ||
| Test the location, location_a and location_b arguments for the Dipole | ||
|
|
||
| Considering that `location`, `location_a`, `location_b` can be None or not | ||
| None, then we have 8 different possible combinations. | ||
|
|
||
|
|
||
| .. code:: | ||
|
|
||
| | location | location_a | location_b | Result | | ||
| |----------|------------|------------|--------| | ||
| | None | None | None | Error | | ||
| | None | None | not None | Error | | ||
| | None | not None | None | Error | | ||
| | None | not None | not None | Run | | ||
| | not None | None | None | Run | | ||
| | not None | None | not None | Error | | ||
| | not None | not None | None | Error | | ||
| | not None | not None | not None | Error | | ||
| """ | ||
|
|
||
| @pytest.fixture | ||
| def receiver(self): | ||
| """Sample DC dipole receiver.""" | ||
| receiver = dc.receivers.Dipole( | ||
| locations_m=np.array([[-100, 0]]), | ||
| locations_n=np.array([[100, 0]]), | ||
| data_type="volt", | ||
| ) | ||
| return receiver | ||
|
|
||
| def test_all_nones(self, receiver): | ||
| """ | ||
| Test error being raised when passing all location as None | ||
| """ | ||
| msg = "Found 'location', 'location_a' and 'location_b' as None. " | ||
| with pytest.raises(TypeError, match=msg): | ||
| dc.sources.Dipole( | ||
| receiver_list=[receiver], | ||
| location_a=None, | ||
| location_b=None, | ||
| location=None, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("electrode", ("a", "b", "both")) | ||
| def test_not_nones(self, receiver, electrode): | ||
| """ | ||
| Test error after location as not None, and location_a and/or location_b | ||
| as not None | ||
| """ | ||
| msg = ( | ||
| "Found 'location_a' and/or 'location_b' as not None values. " | ||
| "When passing a not None value for 'location', 'location_a' and " | ||
| "'location_b' should be set to None." | ||
| ) | ||
| electrode_a = np.array([-1.0, 0.0]) | ||
| electrode_b = np.array([1.0, 0.0]) | ||
| if electrode == "a": | ||
| kwargs = dict(location_a=electrode_a, location_b=None) | ||
| elif electrode == "b": | ||
| kwargs = dict(location_a=None, location_b=electrode_b) | ||
| else: | ||
| kwargs = dict(location_a=electrode_a, location_b=electrode_b) | ||
| with pytest.raises(TypeError, match=msg): | ||
| dc.sources.Dipole( | ||
| receiver_list=[receiver], | ||
| location=[electrode_a, electrode_b], | ||
| **kwargs, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("none_electrode", ("a", "b")) | ||
| def test_single_location_as_none(self, receiver, none_electrode): | ||
| """ | ||
| Test error after location is None and one of location_a or location_b | ||
| is also None. | ||
| """ | ||
| msg = ( | ||
| f"Invalid 'location_{none_electrode}' set to None. " | ||
| "When 'location' is None, both 'location_a' and 'location_b' " | ||
| "should be set to a value different than None." | ||
| ) | ||
| electrode_a = np.array([-1.0, 0.0]) | ||
| electrode_b = np.array([1.0, 0.0]) | ||
| if none_electrode == "a": | ||
| kwargs = dict(location_a=None, location_b=electrode_b) | ||
| else: | ||
| kwargs = dict(location_a=electrode_a, location_b=None) | ||
| with pytest.raises(TypeError, match=msg): | ||
| dc.sources.Dipole( | ||
| receiver_list=[receiver], | ||
| location=None, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| def test_location_none(self, receiver): | ||
| """ | ||
| Test if object is correctly initialized with location set to None | ||
| """ | ||
| electrode_a = np.array([-1.0, 0.0]) | ||
| electrode_b = np.array([1.0, 0.0]) | ||
| source = dc.sources.Dipole( | ||
| receiver_list=[receiver], | ||
| location_a=electrode_a, | ||
| location_b=electrode_b, | ||
| location=None, | ||
| ) | ||
| assert isinstance(source.location, np.ndarray) | ||
| assert len(source.location) == 2 | ||
| np.testing.assert_allclose(source.location, [electrode_a, electrode_b]) | ||
|
|
||
| def test_location_not_none(self, receiver): | ||
| """ | ||
| Test if object is correctly initialized with location is set | ||
| """ | ||
| electrode_a = np.array([-1.0, 0.0]) | ||
| electrode_b = np.array([1.0, 0.0]) | ||
| source = dc.sources.Dipole( | ||
| receiver_list=[receiver], | ||
| location=[electrode_a, electrode_b], | ||
| ) | ||
| assert isinstance(source.location, np.ndarray) | ||
| assert len(source.location) == 2 | ||
| np.testing.assert_allclose(source.location, [electrode_a, electrode_b]) | ||
|
|
||
| @pytest.mark.parametrize("length", (0, 1, 3)) | ||
| def test_location_invalid_num_elements(self, length, receiver): | ||
| """ | ||
| Test error after passing location with invalid number of elements | ||
| """ | ||
| if length == 0: | ||
| location = () | ||
| elif length == 1: | ||
| location = (np.array([1.0, 0.0]),) | ||
| else: | ||
| location = ( | ||
| np.array([1.0, 0.0]), | ||
| np.array([1.0, 0.0]), | ||
| np.array([1.0, 0.0]), | ||
| ) | ||
| msg = "location must be a list or tuple of length 2" | ||
| with pytest.raises(ValueError, match=msg): | ||
| dc.sources.Dipole(receiver_list=[receiver], location=location) | ||
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.
🎉