Skip to content

Commit

Permalink
Address Deprecation Warnings (#201)
Browse files Browse the repository at this point in the history
* Simplify 'pos()' in examples.
* Address deprecation warnings
  • Loading branch information
prisae authored Feb 20, 2024
1 parent 8a4e054 commit 0ee2107
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 54 deletions.
2 changes: 1 addition & 1 deletion empymod/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ def get_dlf_points(filt, inp, nr_per_dec):

# Get pts_per_dec and define number of out-values, depending on pts_per_dec
if nr_per_dec < 0: # Lagged Convolution DLF
pts_per_dec = 1/np.log(filt.factor)
pts_per_dec = 1/np.log(filt.factor.item())

# Calculate number of output values
nout = int(np.ceil(np.log(outmax/outmin)*pts_per_dec) + 1)
Expand Down
62 changes: 28 additions & 34 deletions examples/reproducing/ward1988.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,6 @@ def dhzdt(t, res, r, m=1.):
return s


###############################################################################

def pos(data):
"""Return positive data; set negative data to NaN."""
return np.array([x if x > 0 else np.nan for x in data])


def neg(data):
"""Return -negative data; set positive data to NaN."""
return np.array([-x if x < 0 else np.nan for x in data])


###############################################################################
# Survey parameters
# ~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -174,17 +162,23 @@ def neg(data):
# Plot the result
# ~~~~~~~~~~~~~~~


def pos(data):
"""Return positive data; set negative data to NaN."""
return np.where(data > 0, data, np.nan)


plt.figure(figsize=(5, 6))

plt.plot(time*1e3, pos(dhz_ana), 'k-', lw=2, label='Ward & Hohmann')
plt.plot(time*1e3, neg(dhz_ana), 'k--', lw=2)
plt.plot(time*1e3, pos(-dhz_ana), 'k--', lw=2)
plt.plot(time*1e3, pos(dhz_num), 'C1-', label='empymod; dHz/dt')
plt.plot(time*1e3, neg(dhz_num), 'C1--')
plt.plot(time*1e3, pos(-dhz_num), 'C1--')

plt.plot(time*1e3, pos(hz_ana), 'k-', lw=2)
plt.plot(time*1e3, neg(hz_ana), 'k--', lw=2)
plt.plot(time*1e3, pos(-hz_ana), 'k--', lw=2)
plt.plot(time*1e3, pos(hz_num), 'C0-', label='empymod; Hz')
plt.plot(time*1e3, neg(hz_num), 'C0--')
plt.plot(time*1e3, pos(-hz_num), 'C0--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -228,10 +222,10 @@ def neg(data):
plt.figure(figsize=(5, 5))

plt.plot(freq, pos(fhz_num.real), 'C0-', label='Real')
plt.plot(freq, neg(fhz_num.real), 'C0--')
plt.plot(freq, pos(-fhz_num.real), 'C0--')

plt.plot(freq, pos(fhz_num.imag), 'C1-', label='Imaginary')
plt.plot(freq, neg(fhz_num.imag), 'C1--')
plt.plot(freq, pos(-fhz_num.imag), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -270,10 +264,10 @@ def neg(data):
plt.figure(figsize=(5, 5))

plt.plot(freq, pos(fhz_num.real), 'C0-', label='Real')
plt.plot(freq, neg(fhz_num.real), 'C0--')
plt.plot(freq, pos(-fhz_num.real), 'C0--')

plt.plot(freq, pos(fhz_num.imag), 'C1-', label='Imaginary')
plt.plot(freq, neg(fhz_num.imag), 'C1--')
plt.plot(freq, pos(-fhz_num.imag), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -313,7 +307,7 @@ def neg(data):

ax1 = plt.subplot(111)
plt.plot(time*1e3, pos(fdhz_num), 'C0-', label='dHz/dt')
plt.plot(time*1e3, neg(fdhz_num), 'C0--')
plt.plot(time*1e3, pos(-fdhz_num), 'C0--')

plt.xscale('log')
plt.yscale('log')
Expand All @@ -327,7 +321,7 @@ def neg(data):
ax2 = ax1.twinx()

plt.plot(time*1e3, pos(fhz_num), 'C1-', label='Hz')
plt.plot(time*1e3, neg(fhz_num), 'C1--')
plt.plot(time*1e3, pos(-fhz_num), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -370,10 +364,10 @@ def neg(data):
plt.figure(figsize=(5, 5))

plt.plot(freq, pos(fhz_num.real), 'C0-', label='Real')
plt.plot(freq, neg(fhz_num.real), 'C0--')
plt.plot(freq, pos(-fhz_num.real), 'C0--')

plt.plot(freq, pos(fhz_num.imag), 'C1-', label='Imaginary')
plt.plot(freq, neg(fhz_num.imag), 'C1--')
plt.plot(freq, pos(-fhz_num.imag), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -420,10 +414,10 @@ def neg(data):

ax1 = plt.subplot(111)
plt.plot(time*1e3, pos(fdhz_num), 'C0-', label=r'dhz/dt (A/m-s)')
plt.plot(time*1e3, neg(fdhz_num), 'C0--')
plt.plot(time*1e3, pos(-fdhz_num), 'C0--')

plt.plot(time*1e3, pos(fhz_num), 'C1-', label='hz (A/m)')
plt.plot(time*1e3, neg(fhz_num), 'C1--')
plt.plot(time*1e3, pos(-fhz_num), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -462,10 +456,10 @@ def neg(data):
plt.figure(figsize=(5, 5))

plt.plot(freq, pos(fhz_num.real), 'C0-', label='Real')
plt.plot(freq, neg(fhz_num.real), 'C0--')
plt.plot(freq, pos(-fhz_num.real), 'C0--')

plt.plot(freq, pos(fhz_num.imag), 'C1-', label='Imaginary')
plt.plot(freq, neg(fhz_num.imag), 'C1--')
plt.plot(freq, pos(-fhz_num.imag), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -504,10 +498,10 @@ def neg(data):
plt.figure(figsize=(5, 5))

plt.plot(freq, pos(fhz_num.real), 'C0-', label='Real')
plt.plot(freq, neg(fhz_num.real), 'C0--')
plt.plot(freq, pos(-fhz_num.real), 'C0--')

plt.plot(freq, pos(fhz_num.imag), 'C1-', label='Imaginary')
plt.plot(freq, neg(fhz_num.imag), 'C1--')
plt.plot(freq, pos(-fhz_num.imag), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -548,7 +542,7 @@ def neg(data):
ax1 = plt.subplot(111)

plt.plot(time*1e3, pos(fdhz_num), 'C0-', label='dHz/dt')
plt.plot(time*1e3, neg(fdhz_num), 'C0--')
plt.plot(time*1e3, pos(-fdhz_num), 'C0--')

plt.xscale('log')
plt.yscale('log')
Expand All @@ -562,7 +556,7 @@ def neg(data):
ax2 = ax1.twinx()

plt.plot(time*1e3, pos(fhz_num), 'C1-', label='Hz')
plt.plot(time*1e3, neg(fhz_num), 'C1--')
plt.plot(time*1e3, pos(-fhz_num), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down Expand Up @@ -603,7 +597,7 @@ def neg(data):
ax1 = plt.subplot(111)

plt.plot(time*1e3, pos(fdhz_num), 'C0-', label='dHz/dt')
plt.plot(time*1e3, neg(fdhz_num), 'C0--')
plt.plot(time*1e3, pos(-fdhz_num), 'C0--')

plt.xscale('log')
plt.yscale('log')
Expand All @@ -617,7 +611,7 @@ def neg(data):
ax2 = ax1.twinx()

plt.plot(time*1e3, pos(fhz_num), 'C1-', label='Hz')
plt.plot(time*1e3, neg(fhz_num), 'C1--')
plt.plot(time*1e3, pos(-fhz_num), 'C1--')

plt.xscale('log')
plt.yscale('log')
Expand Down
26 changes: 12 additions & 14 deletions examples/time_domain/cole_cole_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@
plt.style.use('ggplot')


def pos(data):
"""Return positive data; set negative data to NaN."""
return np.array([x if x > 0 else np.nan for x in data])


def neg(data):
"""Return -negative data; set positive data to NaN."""
return np.array([-x if x < 0 else np.nan for x in data])


###############################################################################
# Use empymod with user-def. func. to adjust :math:`\eta` and :math:`\zeta`
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -258,21 +248,29 @@ def cole_perm(inp, p_dict):
out_cole_perm = empymod.bipole(res=cole_perm_model, **model)
out_pelton = empymod.bipole(res=pelton_model, **model)


# Plot


def pos(data):
"""Return positive data; set negative data to NaN."""
return np.where(data > 0, data, np.nan)


plt.figure()
plt.title('Switch-off')
plt.plot(times, pos(out_bipole), '-', label='Regular Bipole')
plt.plot(times, neg(out_bipole), '--', label='')
plt.plot(times, pos(-out_bipole), '--', label='')

plt.plot(times, pos(out_cole), '-', label='Cole and Cole (1941) Conductivity')
plt.plot(times, neg(out_cole), '--', label='')
plt.plot(times, pos(-out_cole), '--', label='')

plt.plot(times, pos(out_cole_perm), '-',
label='Cole and Cole (1941) Permittivity')
plt.plot(times, neg(out_cole_perm), '--', label='')
plt.plot(times, pos(-out_cole_perm), '--', label='')

plt.plot(times, pos(out_pelton), '-', label='Pelton et al. (1978) Resistivity')
plt.plot(times, neg(out_pelton), '--', label='')
plt.plot(times, pos(-out_pelton), '--', label='')

plt.legend()
plt.yscale('log')
Expand Down
10 changes: 5 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_main(script_runner):

# help
for inp in ['--help', '-h']:
ret = script_runner.run('empymod', inp)
ret = script_runner.run(['empymod', inp])
assert ret.success
assert "3D electromagnetic modeller for 1D VTI media" in ret.stdout

Expand All @@ -37,27 +37,27 @@ def test_main(script_runner):
assert "empymod v" in ret.stdout

# report
ret = script_runner.run('empymod', '--report')
ret = script_runner.run(['empymod', '--report'])
assert ret.success
# Exclude time to avoid errors.
# Exclude empymod-version (after 300), because if run locally without
# having empymod installed it will be "unknown" for the __main__ one.
assert empymod.utils.Report().__repr__()[115:300] in ret.stdout

# version -- VIA empymod/__main__.py by calling the folder empymod.
ret = script_runner.run('python', 'empymod', '--version')
ret = script_runner.run(['python', 'empymod', '--version'])
assert ret.success
assert "empymod v" in ret.stdout

# Wrong function -- VIA empymod/__main__.py by calling the file.
ret = script_runner.run(
'python', join('empymod', '__main__.py'), 'wrong')
['python', join('empymod', '__main__.py'), 'wrong'])
assert not ret.success
assert "error: argument routine: invalid choice: 'wrong'" in ret.stderr

# try to run
ret = script_runner.run(
'empymod', 'bipole', 'test.json', 'output.txt')
['empymod', 'bipole', 'test.json', 'output.txt'])
assert not ret.success
assert "No such file or directory" in ret.stderr

Expand Down

0 comments on commit 0ee2107

Please sign in to comment.