Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -10,7 +10,6 @@
# 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 scipy.optimize import least_squares
Expand Down Expand Up @@ -445,13 +444,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():
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
13 changes: 13 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import runpy


def test_all_examples(tmp_examples):
"""Run all example scripts to ensure they execute without error."""
scripts = list(tmp_examples.rglob("**/solutions/diffpy-cmi/*.py"))
# sort list so that fitBulkNi.py runs first
scripts.sort(key=lambda s: 0 if s.name == "fitBulkNi.py" else 1)
for script in scripts:
script_relative_path = script.relative_to(tmp_examples).as_posix()
Copy link
Contributor

Choose a reason for hiding this comment

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

what is your reason for the posix? this is likely to make it not work on windows.

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 This is to make it compatible across operating systems when converting the path to a string. Maybe it can go either way here (use str() or posix).

Copy link
Contributor

Choose a reason for hiding this comment

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

I would just delete it tbh

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Will do

print("hello", script_relative_path)
print(f"Testing {script_relative_path}")
runpy.run_path(str(script), run_name="__main__")
Loading