Skip to content

Commit e1b75f8

Browse files
committed
_helpers: no longer depend on global conf
1 parent 66ecff7 commit e1b75f8

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

stagpy/_helpers.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import matplotlib.pyplot as plt
99

10-
from . import conf
11-
1210
if typing.TYPE_CHECKING:
1311
from typing import Any, Optional
1412

@@ -31,7 +29,7 @@ def walk(sdat: StagyyData, conf: Config) -> _StepsView:
3129
return sdat.snaps[conf.core.snapshots]
3230

3331

34-
def out_name(stem: str, timestep: Optional[int] = None) -> str:
32+
def out_name(conf: Config, stem: str, timestep: Optional[int] = None) -> str:
3533
"""Return StagPy out file name.
3634
3735
Args:
@@ -40,9 +38,6 @@ def out_name(stem: str, timestep: Optional[int] = None) -> str:
4038
4139
Returns:
4240
the output file name.
43-
44-
Other Parameters:
45-
conf.core.outname: the generic name stem, defaults to ``'stagpy'``.
4641
"""
4742
if conf.core.shortname:
4843
return conf.core.outname
@@ -69,6 +64,7 @@ def scilabel(value: float, precision: int = 2) -> str:
6964

7065

7166
def saveplot(
67+
conf: Config,
7268
fig: Figure,
7369
stem: str,
7470
timestep: Optional[int] = None,
@@ -85,7 +81,7 @@ def saveplot(
8581
timestep: timestep if relevant.
8682
close: whether to close the figure.
8783
"""
88-
oname = out_name(stem, timestep)
84+
oname = out_name(conf, stem, timestep)
8985
fig.savefig(
9086
f"{oname}.{conf.plot.format}", format=conf.plot.format, bbox_inches="tight"
9187
)

stagpy/field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,4 @@ def cmd() -> None:
399399
)
400400
oname = "_".join(chain.from_iterable(vfig))
401401
plt.tight_layout(w_pad=3)
402-
_helpers.saveplot(fig, oname, step.isnap)
402+
_helpers.saveplot(conf, fig, oname, step.isnap)

stagpy/plates.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def plot_at_surface(
262262
axis.legend()
263263
axes[-1].set_xlabel(r"$\phi$")
264264
axes[-1].set_xlim(snap.geom.p_walls[[0, -1]])
265-
saveplot(fig, fname, snap.isnap)
265+
saveplot(conf, fig, fname, snap.isnap)
266266

267267

268268
def _write_trench_diagnostics(
@@ -367,7 +367,13 @@ def plot_scalar_field(
367367
# Put arrow where ridges and trenches are
368368
_plot_plate_limits_field(axis, snap, conf)
369369

370-
saveplot(fig, f"plates_{fieldname}", snap.isnap, close=conf.plates.zoom is None)
370+
saveplot(
371+
conf,
372+
fig,
373+
f"plates_{fieldname}",
374+
snap.isnap,
375+
close=conf.plates.zoom is None,
376+
)
371377

372378
# Zoom
373379
if conf.plates.zoom is not None:
@@ -385,7 +391,7 @@ def plot_scalar_field(
385391
yzoom = (snap.geom.rcmb + 1) * np.sin(np.radians(conf.plates.zoom))
386392
axis.set_xlim(xzoom - ladd, xzoom + radd)
387393
axis.set_ylim(yzoom - dadd, yzoom + uadd)
388-
saveplot(fig, f"plates_zoom_{fieldname}", snap.isnap)
394+
saveplot(conf, fig, f"plates_zoom_{fieldname}", snap.isnap)
389395

390396

391397
def cmd() -> None:
@@ -408,7 +414,7 @@ def cmd() -> None:
408414
time = []
409415
istart, iend = None, None
410416

411-
oname = _helpers.out_name(f"plates_trenches_{view.stepstr}")
417+
oname = _helpers.out_name(conf, f"plates_trenches_{view.stepstr}")
412418
with open(f"{oname}.dat", "w") as fid:
413419
fid.write(
414420
"# istep time time_My phi_trench vel_trench "
@@ -436,11 +442,11 @@ def cmd() -> None:
436442
axis.hist(plate_sizes, 10, (0, np.pi))
437443
axis.set_ylabel("Number of plates")
438444
axis.set_xlabel(r"$\phi$-span")
439-
saveplot(fig, "plates_size_distribution", step.isnap)
445+
saveplot(conf, fig, "plates_size_distribution", step.isnap)
440446

441447
if conf.plates.nbplates:
442448
figt, axis = plt.subplots()
443449
axis.plot(time, nb_plates)
444450
axis.set_xlabel("Time")
445451
axis.set_ylabel("Number of plates")
446-
saveplot(figt, f"plates_{istart}_{iend}")
452+
saveplot(conf, figt, f"plates_{istart}_{iend}")

stagpy/refstate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def plot_ref(sdat: StagyyData, var: str, conf: Optional[Config] = None) -> None:
3939
axis.set_ylabel("z Position")
4040
if len(adbts) > 2:
4141
axis.legend()
42-
_helpers.saveplot(fig, f"refstate_{var}")
42+
_helpers.saveplot(conf, fig, f"refstate_{var}")
4343

4444

4545
def cmd() -> None:

stagpy/rprof.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def plot_rprofs(
6666
axes[0].invert_yaxis()
6767
ylabel = "Depth" if conf.rprof.depth else "Radius"
6868
axes[0].set_ylabel(ylabel)
69-
_helpers.saveplot(fig, fname + stepstr)
69+
_helpers.saveplot(conf, fig, fname + stepstr)
7070

7171

72-
def plot_grid(step: Step) -> None:
72+
def plot_grid(step: Step, conf: Optional[Config] = None) -> None:
7373
"""Plot cell position and thickness.
7474
7575
The figure is call grid_N.pdf where N is replace by the step index.
@@ -78,6 +78,8 @@ def plot_grid(step: Step) -> None:
7878
step (:class:`~stagpy._step.Step`): a step of a StagyyData
7979
instance.
8080
"""
81+
if conf is None:
82+
conf = Config.default_()
8183
drprof = step.rprofs["dr"]
8284
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
8385
ax1.plot(drprof.rad, "-ko")
@@ -86,7 +88,7 @@ def plot_grid(step: Step) -> None:
8688
ax2.set_ylabel("$dr$")
8789
ax2.set_xlim([-0.5, len(drprof.rad) - 0.5])
8890
ax2.set_xlabel("Cell number")
89-
_helpers.saveplot(fig, "grid", step.istep)
91+
_helpers.saveplot(conf, fig, "grid", step.istep)
9092

9193

9294
def cmd() -> None:
@@ -103,7 +105,7 @@ def cmd() -> None:
103105

104106
if conf.rprof.grid:
105107
for step in view.filter(rprofs=True):
106-
plot_grid(step)
108+
plot_grid(step, conf)
107109

108110
if conf.rprof.average:
109111
plot_rprofs(view.rprofs_averaged, conf.rprof.plot, conf)

stagpy/time_series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def plot_time_series(
9191
axes[-1].set_xlabel("Time")
9292
axes[-1].set_xlim(tstart, tend)
9393
axes[-1].tick_params()
94-
_helpers.saveplot(fig, "_".join(fname))
94+
_helpers.saveplot(conf, fig, "_".join(fname))
9595

9696

9797
def compstat(

tests/test_helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
import stagpy
43
from stagpy import _helpers
54
from stagpy.config import Config
65
from stagpy.stagyydata import StagyyData
@@ -15,15 +14,16 @@ def test_walk_dflt(sdat: StagyyData) -> None:
1514

1615

1716
def test_out_name_conf() -> None:
17+
conf = Config.default_()
1818
oname = "something_fancy"
19-
stagpy.conf.core.outname = oname
19+
conf.core.outname = oname
2020
stem = "teapot"
21-
assert _helpers.out_name(stem) == oname + "_" + stem
22-
del stagpy.conf.core.outname
21+
assert _helpers.out_name(conf, stem) == oname + "_" + stem
2322

2423

2524
def test_out_name_number() -> None:
26-
assert _helpers.out_name("T", 123) == "stagpy_T00123"
25+
conf = Config.default_()
26+
assert _helpers.out_name(conf, "T", 123) == "stagpy_T00123"
2727

2828

2929
def test_baredoc() -> None:

0 commit comments

Comments
 (0)