Skip to content

Commit

Permalink
Don't set _finite if no data (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
prisae authored Aug 30, 2024
1 parent 5658962 commit e64358b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 6 additions & 2 deletions docs/manual/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ References
.. [JoOM06] Jönsthövel, T. B., C. W. Oosterlee, and W. A. Mulder, 2006,
Improving multigrid for 3-D electro-magnetic diffusion on stretched grids:
European Conference on Computational Fluid Dynamics; UUID:
`df65da5c-e43f-47ab-b80d-2f8ee7f35464
<http://resolver.tudelft.nl/uuid:df65da5c-e43f-47ab-b80d-2f8ee7f35464>`_.
`632b503b-149a-47c1-a6b2-2f98f8703b48
<https://resolver.tudelft.nl/uuid:632b503b-149a-47c1-a6b2-2f98f8703b48>`_.
..
df65da5c-e43f-47ab-b80d-2f8ee7f35464 # Old uuid
.. [Hack85] Hackbusch, W., 1985, Multi-grid methods and applications: Springer,
Berlin, Heidelberg, Volume 4 of Springer Series in Computational
Mathematics; DOI: `10.1007/978- 3-662-02427-0 <https://doi.org/10.1007/978-
Expand Down
10 changes: 8 additions & 2 deletions emg3d/surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,14 @@ def _rec_types_coord(self, source):
def isfinite(self):
"""Return indices of the finite data."""
if not hasattr(self, '_isfinite'):
self._isfinite = np.isfinite(self.data.observed.data)
return self._isfinite
finite = np.isfinite(self.data.observed.data)
# Only set the attribute if there is any data.
# Otherwise, this was called before observations were added.
if finite.sum() > 0:
self._isfinite = finite
else:
finite = self._isfinite
return finite

def finite_data(self, data='observed'):
"""Return finite elements of selected `data`."""
Expand Down
11 changes: 8 additions & 3 deletions tests/test_surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,23 @@ def test_add_noise(self):
)

data = np.logspace(0, -20, offs.size)+1j*np.logspace(0, -20, offs.size)
data = data[None, :, None]

survey = surveys.Survey(
sources=emg3d.electrodes.TxElectricDipole((0, 0, 0, 0, 0)),
receivers=rec,
frequencies=1.0,
data=data,
relative_error=0.01,
noise_floor=1e-15
)

assert survey.isfinite.sum() == 0
assert not hasattr(survey, '_isfinite')
survey.data['observed'] = survey.data.observed.copy(data=data)
assert survey.isfinite.sum() == 21
assert hasattr(survey, '_isfinite')
assert survey.finite_data().shape == (21,)

# Defined cutting
survey.add_noise(min_offset=1000, min_amplitude=1e-19, add_to='test1')
# Ensure short offsets are NaN
Expand All @@ -284,8 +291,6 @@ def test_add_noise(self):
assert np.all(np.isnan(survey.data.test1.data[:, -1:, :]))
# Ensure no others are none
assert np.sum(np.isnan(survey.data.test1.data)) == 3
assert survey.isfinite.sum() == 21
assert survey.finite_data().shape == (21,)

# No cutting
survey.add_noise(min_offset=0, min_amplitude=10e-50, add_to='test2')
Expand Down

0 comments on commit e64358b

Please sign in to comment.