Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# 1: Import relevant system packages that we will need...
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from bg_mpl_stylesheets.styles import all_styles
from scipy.optimize import least_squares

# ... and the relevant CMI packages
Expand All @@ -25,6 +25,7 @@
from diffpy.srfit.structure import constrainAsSpaceGroup
from diffpy.structure.parsers import getParser

plt.style.use(all_styles["bg-style"])
# Config
# 2: Give a file path to where your PDF (.gr) and
# structure (.cif) files are located.
Expand All @@ -33,7 +34,7 @@
# First we store the absolute directory of this script,
# then two directories above this,with the directory
# 'data' appended
PWD = Path(__file__).parent.absolute()
PWD = Path(__file__).parent.resolve()
DPATH = PWD.parent.parent / "data"

# 3: Give an identifying name for the refinement, similar
Expand Down Expand Up @@ -264,13 +265,6 @@ def plot_results(recipe, fig_name):
# Calculate the residual (difference) array and offset it vertically.
diff = g - gcalc + diffzero

# Change some style details of the plot
mpl.rcParams.update(mpl.rcParamsDefault)
if (PWD.parent.parent.parent / "utils" / "billinge.mplstyle").exists():
plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

# Create a figure and an axis on which to plot
fig, ax1 = plt.subplots(1, 1)

Expand Down
10 changes: 2 additions & 8 deletions docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitNPPt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# 1: Import relevant system packages that we will need...
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from bg_mpl_stylesheets.styles import all_styles
from scipy.optimize import least_squares

# ... and the relevant CMI packages
Expand All @@ -30,6 +30,7 @@
from diffpy.srfit.structure import constrainAsSpaceGroup
from diffpy.structure.parsers import getParser

plt.style.use(all_styles["bg-style"])
# Config ##############################
# 2: Give a file path to where your PDF (.gr) and structure (.cif) files
# are located.
Expand Down Expand Up @@ -241,13 +242,6 @@ def plot_results(recipe, fig_name):
# Calculate the residual (difference) array and offset it vertically.
diff = g - gcalc + diffzero

# Change some style details of the plot
mpl.rcParams.update(mpl.rcParamsDefault)
if (PWD.parent.parent.parent / "utils" / "billinge.mplstyle").exists():
plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

# Create a figure and an axis on which to plot
fig, ax1 = plt.subplots(1, 1)

Expand Down
10 changes: 2 additions & 8 deletions docs/examples/ch05Fit2Phase/solutions/diffpy-cmi/fit2P.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# 1: Import relevant system packages that we will need...
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from bg_mpl_stylesheets.styles import all_styles
from scipy.optimize import least_squares

# ... and the relevant CMI packages
Expand All @@ -26,6 +26,7 @@
from diffpy.srfit.structure import constrainAsSpaceGroup
from diffpy.structure.parsers import getParser

plt.style.use(all_styles["bg-style"])
# Config ##############################
# 2: Give a file path to where your pdf (.gr) and (.cif) files are located.
PWD = Path(__file__).parent.absolute()
Expand Down Expand Up @@ -271,13 +272,6 @@ def plot_results(recipe, fig_name):
ni_signal = ni_scale * recipe.crystal.G_Ni.profile.ycalc
ni_signal += min(si_signal) - np.abs(max(ni_signal))

# Change some style details of the plot
mpl.rcParams.update(mpl.rcParamsDefault)
if (PWD.parent.parent.parent / "utils" / "billinge.mplstyle").exists():
plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

# Create a figure and an axis on which to plot
fig, ax1 = plt.subplots(1, 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# 1: Import relevant system packages that we will need...
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from bg_mpl_stylesheets.styles import all_styles
from scipy.optimize import least_squares

# ... and the relevant CMI packages
Expand All @@ -27,12 +27,7 @@
from diffpy.structure.atom import Atom
from diffpy.structure.parsers import getParser

try:
from bg_mpl_stylesheets.bg_mpl_stylesheet import bg_mpl_style

plt.style.use(bg_mpl_style)
except ImportError:
pass
plt.style.use(all_styles["bg-style"])

# Config ##############################
# 2: Give a file path to where your pdf (.gr) and (.cif) files are located.
Expand Down Expand Up @@ -246,13 +241,6 @@ def plot_results(recipe, fig_name):
# Calculate the residual (difference) array and offset it vertically.
diff = g - gcalc + diffzero

# Change some style details of the plot
mpl.rcParams.update(mpl.rcParamsDefault)
if (PWD.parent.parent.parent / "utils" / "billinge.mplstyle").exists():
plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

# Create a figure and an axis on which to plot
fig, ax1 = plt.subplots(1, 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# 1: Import relevant system packages that we will need...
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from bg_mpl_stylesheets.styles import all_styles
from scipy.optimize import least_squares

# ... and the relevant CMI packages
Expand All @@ -25,12 +25,7 @@
from diffpy.srfit.structure import constrainAsSpaceGroup
from diffpy.structure.parsers import getParser

try:
from bg_mpl_stylesheets.bg_mpl_stylesheet import bg_mpl_style

plt.style.use(bg_mpl_style)
except ImportError:
pass
plt.style.use(all_styles["bg-style"])

# Config ##############################
# 2: Give a file path to where your pdf (.gr) and (.cif) files are located.
Expand Down Expand Up @@ -221,13 +216,6 @@ def plot_results(recipe, fig_name):
# Calculate the residual (difference) array and offset it vertically.
diff = g - gcalc + diffzero

# Change some style details of the plot
mpl.rcParams.update(mpl.rcParamsDefault)
if (PWD.parent.parent.parent / "utils" / "billinge.mplstyle").exists():
plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

# Create a figure and an axis on which to plot
fig, ax1 = plt.subplots(1, 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,6 @@ def plot_results(recipe, fig_name):

# Change some style details of the plot
mpl.rcParams.update(mpl.rcParamsDefault)
if (PWD.parent.parent.parent / "utils" / "billinge.mplstyle").exists():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the group style added back in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbillinge I will have to add it to each file I think, I can do this!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better on another PR, but can we also do this work on the examples scripts also? Not just the solutions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbillinge Yeah I can do this. Do you want me to revert back then?

plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

# Create a figure and an axis on which to plot
fig, ax1 = plt.subplots(1, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ def plot_results(recipe, figname):
diff = g - gcalc + diffzero

mpl.rcParams.update(mpl.rcParamsDefault)
plt.style.use(
str(PWD.parent.parent.parent / "utils" / "billinge.mplstyle")
)

fig, ax1 = plt.subplots(1, 1)

Expand Down
23 changes: 23 additions & 0 deletions news/example-ci.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Add tests that run PDF example scripts

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
1 change: 1 addition & 0 deletions requirements/packs/plotting.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ipywidgets
matplotlib
ipympl
bg-mpl-stylesheets
py3dmol>=2.0.1
2 changes: 2 additions & 0 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pytest-env
pytest-mock
freezegun
DeepDiff
psutil
bg-mpl-stylesheets
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import json
import shutil
from pathlib import Path

import matplotlib
import pytest

PROJECT_ROOT = Path(__file__).resolve().parents[1]
EXAMPLES_ROOT = PROJECT_ROOT / "docs" / "examples"


@pytest.fixture(scope="session")
def tmp_examples(tmp_path_factory):
"""Copy the entire examples/ tree into a temp directory once per
test session.

Returns the path to that copy.
"""
tmpdir = tmp_path_factory.mktemp("examples")
tmp_examples = tmpdir / "examples"
shutil.copytree(EXAMPLES_ROOT, tmp_examples)
yield tmp_examples


@pytest.fixture(scope="session", autouse=True)
def use_headless_matplotlib():
"""Force matplotlib to use a headless backend during tests."""
matplotlib.use("Agg")


@pytest.fixture
def user_filesystem(tmp_path):
Expand Down
43 changes: 43 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import re
import runpy


def get_instrument_params(res_file):
"""Parse Qdamp and Qbroad from a .res file."""
qdamp = qbroad = None
with open(res_file, "r") as f:
for line in f:
if line.startswith("Calib_Qbroad"):
qbroad = float(re.split(r"\s+", line.strip())[1])
elif line.startswith("Calib_Qdamp"):
qdamp = float(re.split(r"\s+", line.strip())[1])
return qdamp, qbroad


def test_all_examples(tmp_examples):
"""Run all example scripts to ensure they execute without error."""
# Run Ni example first to produce .res file
ni_script = list(tmp_examples.rglob("**/FitBulkNi.py"))[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have turned something beautiful into something very ugly though. I think the figbulkNI runs first anyway so we probably don't need any of this. Do you want to have another crack at it, or should I try and see what I can come up with?

Also why are you recursively globbing a single file? I can see we need to work a bit on your coding skills......

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbillinge Let me give this another shot. Im just trying to figure out how to pull the results file from the temp dir.

runpy.run_path(str(ni_script), run_name="__main__")
res_file = ni_script.parent / "res" / "Fit_Ni_Bulk.res"
assert res_file.exists(), f"Ni results file not found: {res_file}"

qdamp_i, qbroad_i = get_instrument_params(res_file)
pt_script = list(tmp_examples.rglob("**/fitNPPt.py"))[0]
refined_Ni_params = {"QDAMP_I": qdamp_i, "QBROAD_I": qbroad_i}
# run the NPPt script with the refined Ni params
runpy.run_path(
str(pt_script), run_name="__main__", init_globals=refined_Ni_params
)

# Run all other example scripts, patching the instrument values
all_scripts = list(tmp_examples.rglob("**/solutions/diffpy-cmi/*.py"))
scripts = [
s for s in all_scripts if s.name not in ("fitBulkNi.py", "fitNPPt.py")
]
for script_path in scripts:
print(f"Testing {script_path.relative_to(tmp_examples)}")
mod_globals = {"QDAMP_I": qdamp_i, "QBROAD_I": qbroad_i}
runpy.run_path(
str(script_path), run_name="__main__", init_globals=mod_globals
)
Loading