Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@
print("The Ni example refines instrument parameters\n")
print("The instrument parameters are necessary to run this fit\n")
print("Please run the Ni example first\n")
print("Setting Q_damp and Q_broad to the refined values\n")
QDAMP_I = 0.045298
QBROAD_I = 0.016809

# If we want to run using multiprocessors, we can switch this to 'True'.
# This requires that the 'psutil' python package installed.
RUN_PARALLEL = False
Expand Down
39 changes: 37 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,8 +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):
scripts = list(tmp_examples.rglob("**/solutions/diffpy-cmi/*.py"))
"""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)}")
runpy.run_path(str(script_path), run_name="__main__")
mod_globals = {"QDAMP_I": qdamp_i, "QBROAD_I": qbroad_i}
runpy.run_path(
str(script_path), run_name="__main__", init_globals=mod_globals
)
Loading