Skip to content

Commit a4ea65e

Browse files
authored
v1.7.0 CLI-clean; deprecations (#288)
* v1.7.0 Changelog * Deprecate noise-parameters in simulation section of config file * Deprecate expand in simulation * Bump change of center_on_edge to v1.9.0
1 parent 46d8be3 commit a4ea65e

File tree

8 files changed

+116
-22
lines changed

8 files changed

+116
-22
lines changed

CHANGELOG.rst

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ Changelog
66
""""""""""
77

88

9-
*latest*
10-
--------
9+
v1.7.0 : CLI-clean
10+
------------------
11+
12+
**2022-05-21**
1113

1214
- CLI:
1315

@@ -18,16 +20,27 @@ Changelog
1820
- New command-line argument ``--cache`` (or as parameter ``cache`` in the
1921
configuration file under ``[files]``): Acts as a shortcut for ``--load
2022
--save`` using the same file name.
23+
- Parameters for noise generation should new be provided under their own
24+
section ``[noise_opts]``; providing them under ``[simulation]`` is
25+
deprecated and will be removed in v1.9.0.
2126

2227
- Simulation:
2328

2429
- ``'all'`` is now the same as ``'computed'`` in ``to_file`` and ``to_dict``,
2530
meaning the grids are stored as well.
31+
- Deprecation: The ``'expand'``-functionality in the gridding options is
32+
deprecated and will be removed in v1.9.0. A property-complete model has to
33+
be provided.
34+
35+
- Meshes: Bumped the change of the default value for ``center_on_edge`` from
36+
``True`` to ``False`` to v1.9.0, coinciding with the above deprecations.
2637

2738

2839
v1.6.1 : Max offset
2940
-------------------
3041

42+
**2022-05-11**
43+
3144
- Survey: ``add_noise`` takes new a ``max_offset`` argument; receivers
3245
responses at offsets greater than maximum offset are set to NaN (also
3346
available through the CLI).

docs/manual/cli.rst

+11-6
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,10 @@ remove the comment signs to use them.
5454
# Simulation parameters
5555
# ---------------------
5656
# Input parameters for the `Simulation` class, except for `solver_opts`
57-
# (defined in their own section), but including the parameter `min_offset`
58-
# for `compute()`.
57+
# (defined in their own section).
5958
[simulation]
6059
# max_workers = 4 # Also via `-n` or `--nproc`.
6160
# gridding = single
62-
# min_offset = 0.0 # off < min_off set to NaN; only if `--forward`.
63-
# max_offset = np.infty # off > max_off set to NaN; only if `--forward`.
64-
# mean_noise = 0.0 # Mean of the noise; only if `--forward`.
65-
# ntype = white_noise # Type of the noise; only if `--forward`.
6661
# name = MyTestSimulation
6762
# file_dir = None # For file-based comp; absolute or relative path.
6863
# receiver_interpolation = cubic # Set it to <linear> for the gradient.
@@ -121,6 +116,16 @@ remove the comment signs to use them.
121116
# verb = # int, e.g.: 0
122117
# lambda_from_center = # bool, e.g.: False
123118

119+
# Noise options
120+
# -------------
121+
# Only if `--forward`, the noise options are passed to
122+
# `Simulation.compute(observed=True, **noise_opts)`.
123+
[noise_opts]
124+
# min_offset = 0.0 # off < min_off set to NaN.
125+
# max_offset = np.infty # off > max_off set to NaN.
126+
# mean_noise = 0.0 # Mean of the noise.
127+
# ntype = white_noise # Type of the noise.
128+
124129
# Data
125130
# ----
126131
# Select which sources, receivers, and frequencies of the survey are used. By

emg3d/cli/parser.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# the License.
1919

2020
import os
21+
import warnings
2122
import configparser
2223
from pathlib import Path
2324

@@ -197,7 +198,7 @@ def parse_config_file(args_dict):
197198
# Default is 'cubic' - gradient needs 'linear'
198199
simulation[key] = 'linear'
199200

200-
# Check noise parameters
201+
# Check noise parameters => deprecated in ``simulation``, see [noise_opts]!
201202
noise_kwargs = {}
202203
keys = ['min_offset', 'mean_noise', 'max_offset']
203204
for key in keys:
@@ -215,6 +216,38 @@ def parse_config_file(args_dict):
215216
f"Unexpected parameter in [simulation]: {list(all_sim.keys())}."
216217
)
217218

219+
# # Noise parameters # #
220+
221+
if len(noise_kwargs) > 0:
222+
msg = ("emg3d: `min_offset`, `max_offset`, `mean_noise`, and `ntype` "
223+
"belong since v1.7.0 in their own section [noise_opts]; "
224+
"providing them in [simulation] is deprecated and will be "
225+
"removed in v1.9.0.")
226+
warnings.warn(msg, FutureWarning)
227+
228+
# Check if parameter-file has a noise-section, add it otherwise.
229+
# Note: values in noise_opts overwrite values from simulation.
230+
if 'noise_opts' in cfg.sections():
231+
232+
all_noise = dict(cfg.items('noise_opts'))
233+
234+
keys = ['min_offset', 'max_offset', 'mean_noise']
235+
for key in keys:
236+
if cfg.has_option('noise_opts', key):
237+
_ = all_noise.pop(key)
238+
noise_kwargs[key] = cfg.getfloat('noise_opts', key)
239+
key = 'ntype'
240+
if cfg.has_option('noise_opts', key):
241+
_ = all_noise.pop(key)
242+
noise_kwargs[key] = cfg.get('noise_opts', key)
243+
244+
# Ensure no keys are left.
245+
if all_noise:
246+
raise TypeError(
247+
f"Unexpected parameter in [noise_opts]: "
248+
f"{list(all_noise.keys())}."
249+
)
250+
218251
# # Solver parameters # #
219252

220253
# Check if parameter-file has a solver-section, add it otherwise.

emg3d/cli/run.py

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def simulation(args_dict):
9595
sim.model = model['model']
9696

9797
# Expand model if necessary.
98+
# Deprecated, will be removed in v1.9.0.
9899
gopts = cfg['simulation_options']['gridding_opts']
99100
expand = gopts.pop('expand', None)
100101
if expand is not None:

emg3d/meshes.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ def construct_mesh(frequency, properties, center, domain=None, vector=None,
347347
center : array_like
348348
Center coordinates (x, y, z). The mesh is centered around this point,
349349
which means that here is the smallest cell. Usually this is the source
350-
location. Note that from v1.7.0 the default will change: until then,
351-
the center is assumed to be at the edge; from v1.7.0 onwards, it is
350+
location. Note that from v1.9.0 the default will change: until then,
351+
the center is assumed to be at the edge; from v1.9.0 onwards, it is
352352
assumed to be at the cell center. It can be changed via the parameter
353353
``center_on_edge``.
354354
@@ -633,12 +633,12 @@ def origin_and_widths(frequency, properties, center, domain=None, vector=None,
633633
if vector is None:
634634
msg = (
635635
"emg3d: `center` will change from being at an edge to "
636-
"being at the cell center in v1.7.0. This behaviour can "
636+
"being at the cell center in v1.9.0. This behaviour can "
637637
"be set via at `center_on_edge`. Set `center_on_edge` "
638638
"specifically to suppress this warning."
639639
)
640640
warnings.warn(msg, FutureWarning)
641-
center_on_edge = True # Backwards compatible, until v1.7.0.
641+
center_on_edge = True # Backwards compatible, until v1.9.0.
642642

643643
# Get property map from string.
644644
if isinstance(pmap, str):
@@ -1378,6 +1378,7 @@ def estimate_gridding_opts(gridding_opts, model, survey, input_sc2=None):
13781378
input_sc2 : int, default: None
13791379
If :func:`emg3d.models.expand_grid_model` was used, ``input_sc2``
13801380
corresponds to the original ``grid.shape_cells[2]``.
1381+
NOTE: Will be removed in v1.9.0.
13811382
13821383
13831384
Returns

emg3d/simulations.py

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class Simulation:
137137
water, and an air layer is added. The actual height of the seasurface
138138
can be defined with the key ``seasurface``. See
139139
:func:`emg3d.models.expand_grid_model`.
140+
NOTE: ``expand`` is deprecated in v1.7.0, and will be removed in
141+
v1.9.0. A property-complete model has to be provided.
140142
141143
solver_opts : dict, default: {'verb': 1'}
142144
Passed through to :func:`emg3d.solver.solve`. The dict can contain any
@@ -1456,6 +1458,10 @@ def _set_model(self, model, kwargs):
14561458
# Expand model by water and air if required.
14571459
expand = g_opts.pop('expand', None)
14581460
if expand is not None:
1461+
msg = ("emg3d: `expand` is deprecated and will be removed in "
1462+
"v1.9.0. A property-complete model has to be provided.")
1463+
warnings.warn(msg, FutureWarning)
1464+
14591465
try:
14601466
interface = g_opts['seasurface']
14611467
except KeyError as e:

tests/test_cli.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ def test_simulation(self, tmpdir):
261261

262262
args_dict = self.args_dict.copy()
263263
args_dict['config'] = config
264-
cfg, term = cli.parser.parse_config_file(args_dict)
264+
with pytest.warns(FutureWarning, match='in their own section'):
265+
cfg, term = cli.parser.parse_config_file(args_dict)
265266
sim_opts = cfg['simulation_options']
266267
noise_kwargs = cfg['noise_kwargs']
267268
assert sim_opts['max_workers'] == 5
@@ -302,6 +303,33 @@ def test_simulation(self, tmpdir):
302303
sim_opts = cfg['simulation_options']
303304
assert sim_opts['receiver_interpolation'] == 'cubic'
304305

306+
def test_noise(self, tmpdir):
307+
308+
# Write a config file.
309+
config = os.path.join(tmpdir, 'emg3d.cfg')
310+
with open(config, 'w') as f:
311+
f.write("[noise_opts]\n")
312+
f.write("min_offset=1320\n")
313+
f.write("max_offset=5320\n")
314+
f.write("mean_noise=1.0\n")
315+
f.write("ntype=gaussian_uncorrelated")
316+
317+
args_dict = self.args_dict.copy()
318+
args_dict['config'] = config
319+
cfg, term = cli.parser.parse_config_file(args_dict)
320+
noise_kwargs = cfg['noise_kwargs']
321+
assert noise_kwargs['min_offset'] == 1320.0
322+
assert noise_kwargs['max_offset'] == 5320.0
323+
assert noise_kwargs['mean_noise'] == 1.0
324+
assert noise_kwargs['ntype'] == 'gaussian_uncorrelated'
325+
326+
with pytest.raises(TypeError, match="Unexpected parameter in"):
327+
with open(config, 'a') as f:
328+
f.write("\nanother=True")
329+
args_dict = self.args_dict.copy()
330+
args_dict['config'] = config
331+
_ = cli.parser.parse_config_file(args_dict)
332+
305333
def test_solver(self, tmpdir):
306334

307335
# Write a config file.
@@ -692,7 +720,8 @@ def test_expand(self, tmpdir):
692720
args_dict['config'] = os.path.join(tmpdir, 'emg3d.cfg')
693721
args_dict['path'] = tmpdir
694722
args_dict['save'] = 'simulation1.h5'
695-
cli.run.simulation(args_dict.copy())
723+
with pytest.warns(FutureWarning, match='A property-complete'):
724+
cli.run.simulation(args_dict.copy())
696725

697726
# Replace / add dicts
698727
s = emg3d.Simulation.from_file(os.path.join(tmpdir, 'simulation1.h5'))

tests/test_simulations.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ def test_errors(self):
149149

150150
# expand without seasurface
151151
with pytest.raises(KeyError, match="is required if"):
152-
simulations.Simulation(
153-
self.survey, self.model, name='Test',
154-
gridding='single', gridding_opts={'expand': [1, 2]})
152+
with pytest.warns(FutureWarning, match='A property-complete'):
153+
simulations.Simulation(
154+
self.survey, self.model, name='Test',
155+
gridding='single', gridding_opts={'expand': [1, 2]})
155156

156157
def test_reprs(self):
157158
test = self.simulation.__repr__()
@@ -324,11 +325,16 @@ def test_simulation_automatic(self, capsys):
324325
},
325326
}
326327

327-
b_sim = simulations.Simulation(name='both', gridding='both', **inp)
328-
f_sim = simulations.Simulation(
328+
with pytest.warns(FutureWarning, match='A property-complete'):
329+
b_sim = simulations.Simulation(name='both', gridding='both', **inp)
330+
with pytest.warns(FutureWarning, match='A property-complete'):
331+
f_sim = simulations.Simulation(
329332
name='freq', gridding='frequency', **inp)
330-
t_sim = simulations.Simulation(name='src', gridding='source', **inp)
331-
s_sim = simulations.Simulation(
333+
with pytest.warns(FutureWarning, match='A property-complete'):
334+
t_sim = simulations.Simulation(
335+
name='src', gridding='source', **inp)
336+
with pytest.warns(FutureWarning, match='A property-complete'):
337+
s_sim = simulations.Simulation(
332338
name='single', gridding='single', **inp)
333339

334340
# Quick repr test.

0 commit comments

Comments
 (0)