Commit e4719bfe authored by McDonnell, Marshall's avatar McDonnell, Marshall
Browse files

Fixes for tests + added test fixtures

parent 187c2678
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
Dockerfile
*.sh
**/output/

run_gsas2.sh

deleted100755 → 0
+0 −28
Original line number Diff line number Diff line
CIF=177063_P2_R-3m_NaNiFeMn111O2_CollCode177063.cif
INSTPRM=Aeris_Si_Exported_03062025.instprm
XYE=GSAS_Sim_NaNiFeMnO2_111.xye

TEST_DIR=${PWD}/tests/data

#IMAGE=savannah.ornl.gov/asrp/gsas2_refinement@sha256:b0c351a9b9624e12dba5653a84c4c84e5246ee9e807228635eb0e89da67eb11c
IMAGE=asrp-gsas2-refinement

docker build -t ${IMAGE} .

docker run \
    -v ${TEST_DIR}/${CIF}:/refinement/input.cif \
    -v ${TEST_DIR}/${XYE}:/refinement/input.xye \
    -v ${TEST_DIR}/${INSTPRM}:/refinement/input.instprm \
    -v ${PWD}/output:/app/portal \
    ${IMAGE} \
        pixi run python -m gsas2_refinement.gsas2_refinement \
        --cif-filenames /refinement/input.cif \
        --cif-filenames /refinement/input.cif /refinement/input.cif \
        --gsas-filename /refinement/input.xye \
        --instrument-params-filename /refinement/input.instprm \
        --bank-ids 0 \
        --xmin 30.0 \
        --xmax 110.0 \
        --refine-scale-off \
        --scale-factor 20. \
        --num-cycles 0
+3 −6
Original line number Diff line number Diff line
import matplotlib.pyplot as plt
import numpy as np
import os
import time
from typing import Union, Dict, Any

import GSASIIscriptable as G2sc
import numpy as np

# Callback functions
from GSASII import GSASIIscriptable as G2sc


@@ -35,7 +32,7 @@ def print_stats(gpx: G2sc.G2Project):

    print("")

def plot_refinement(gpx: G2sc.G2Project):
def plot_refinement(gpx: G2sc.G2Project, directory="/app/portal"):
    for hist in gpx.histograms():
        x = np.array(hist.getdata("X"))
        y = np.array(hist.getdata("Yobs"))
@@ -43,7 +40,7 @@ def plot_refinement(gpx: G2sc.G2Project):
        dy = np.array(hist.getdata("Residual"))
        bkg = np.array(hist.getdata("Background"))

    filename=f"/app/portal/out_{time.time()}.png"
    filename=os.path.join(directory, f"out_{time.time()}.png")
    fig,ax = plt.subplots()
    ax.plot(x, y, label="exp", color='black', marker='x')
    ax.plot(x, ycalc, label="refinement", color='red')
+17 −4
Original line number Diff line number Diff line
@@ -18,13 +18,26 @@ def fixture__si_cif(test_data_dir: Path) -> Path:
    return test_data_dir / "Si.cif"


@pytest.fixture(name="si_gsas")
def fixture__si_gsas(test_data_dir: Path) -> Path:
@pytest.fixture(name="si_neutron_gsas")
def fixture__si_neutron_gsas(test_data_dir: Path) -> Path:
    return test_data_dir / "NOMAD_Si.gsa"


@pytest.fixture(name="nomad_instrument_parameters")
def fixture__nomad_instrument_parameters(test_data_dir: Path) -> Path:
@pytest.fixture(name="nomad_neutron_instrument_parameters")
def fixture__nomad_neutron_instrument_parameters(test_data_dir: Path) -> Path:
    return test_data_dir / "NOMAD_2021A_sixbanks_Shifter_Si_640e.instprm"

@pytest.fixture(name="nanifemno2_cif")
def fixture__nanifemno2_cif(test_data_dir: Path) -> Path:
    return test_data_dir / "177063_P2_R-3m_NaNiFeMn111O2_CollCode177063.cif"

@pytest.fixture(name="nanifemno2_xray_gsas")
def fixture__nanifemno2_xray_gsas(test_data_dir: Path) -> Path:
    return test_data_dir / "GSAS_Sim_NaNiFeMnO2_111.xye"

@pytest.fixture(name="nanifemno2_xray_instrument_parameters")
def fixture__aeris_xray_instrument_parameters(test_data_dir: Path) -> Path:
    return test_data_dir / "Aeris_Si_Exported_03062025.instprm"


+14 −20
Original line number Diff line number Diff line
import logging
import os
from pathlib import Path

from gsas2_refinement.run_gsas2_fit import run_gsas2_fit

def _assert_outputs(output_path: Path, stem: str):
    assert (output_path / f"{stem}_initial.gpx").exists()
    assert (output_path / f"{stem}_refined.bak0.gpx").exists()
    assert (output_path / f"{stem}_refined.cif").exists()
    assert (output_path / f"{stem}_refined.gpx").exists()
    assert (output_path / f"{stem}_refined.lst").exists()

def test_gsas2_fit_single_phase(si_cif, si_gsas, nomad_instrument_parameters):

def test_gsas2_fit_nomad_neutron_si_single_phase(si_cif, si_neutron_gsas, nomad_neutron_instrument_parameters):
    output_stem = "si_output"
    output_path = Path(__file__).parent.resolve()
    stype = "N"
    bank = 5
    xmin = 5000
    xmax = 15000

    run_gsas2_fit(
        structure_paths = [si_cif],
        gsas_input_path = si_gsas,
        structure_path = [si_cif],
        gsas_input_path = si_neutron_gsas,
        instrument_params_path = nomad_instrument_parameters,
        output_stem_fn = output_stem,
        banks = bank,
@@ -24,23 +28,17 @@ def test_gsas2_fit_single_phase(si_cif, si_gsas, nomad_instrument_parameters):
        output_path = output_path,
    )

    assert os.path.join(output_path, output_stem + "_initial.gpx")
    assert os.path.join(output_path, output_stem + "_refined.bak0.gpx")
    assert os.path.join(output_path, output_stem + "_refined.cif")
    assert os.path.join(output_path, output_stem + "_refined.gpx")
    assert os.path.join(output_path, output_stem + "_refined.lst")

    _assert_outputs(output_path, output_stem)

def test_gsas2_fit_multi_phase(si_cif, si_gsas, nomad_instrument_parameters):
def test_gsas2_fit_nomad_neutron_si_multi_phase(si_cif, si_neutron_gsas, nomad_neutron_instrument_parameters):
    output_stem = "si_output"
    output_path = Path(__file__).parent.resolve()
    stype = "N"
    bank = 5
    xmin = 5000
    xmax = 15000

    run_gsas2_fit(
        structure_paths = [si_cif, si_cif],
        structure_path = [si_cif, si_cif],
        gsas_input_path = si_gsas,
        instrument_params_path = nomad_instrument_parameters,
        output_stem_fn = output_stem,
@@ -50,8 +48,4 @@ def test_gsas2_fit_multi_phase(si_cif, si_gsas, nomad_instrument_parameters):
        output_path = output_path,
    )

    assert os.path.join(output_path, output_stem + "_initial.gpx")
    assert os.path.join(output_path, output_stem + "_refined.bak0.gpx")
    assert os.path.join(output_path, output_stem + "_refined.cif")
    assert os.path.join(output_path, output_stem + "_refined.gpx")
    assert os.path.join(output_path, output_stem + "_refined.lst")
 No newline at end of file
    _assert_outputs(output_path, output_stem)