Example #1
0
def test_gen_xrf_spectrum_2():
    r"""Failing test of ``gen_xrf_spectrum``: invalid types of parameter ``element_line_groups``"""

    # 'element_groups' == None is valid
    element_groups = None
    spectrum_expected = np.zeros(shape=(4096, ), dtype=float)
    spectrum, xx = gen_xrf_spectrum(element_groups, incident_energy=12.0)
    npt.assert_array_equal(
        spectrum,
        spectrum_expected,
        err_msg="Values in the spectrum array are not all zeros")

    # Try submit a list instead of dict
    with pytest.raises(
            RuntimeError,
            match="Parameter 'element_line_groups' has invalid type"):
        gen_xrf_spectrum([1, 2, 3], incident_energy=12.0)
Example #2
0
def test_gen_xrf_spectrum_3():
    r"""Failing test of ``gen_xrf_spectrum``: unsupported emission line in the list"""

    # 'Fe_L' is not supported
    element_groups = {
        "Fe_L": {
            "area": 800
        },
        "Se_L": {
            "area": 900
        },
        "W_M": {
            "area": 1000
        }
    }

    with pytest.raises(RuntimeError):
        gen_xrf_spectrum(element_groups, incident_energy=12.0)
Example #3
0
def test_gen_xrf_spectrum_1():
    r"""Successful operation of ``gen_xrf_spectrum``. Some tests are empirical and don't guarantee
    accurate operation of the function."""

    element_groups = {
        "Fe_K": {
            "area": 800
        },
        "Se_L": {
            "area": 900
        },
        "W_M": {
            "area": 1000
        }
    }
    total_area = sum([_["area"] for _ in element_groups.values()])
    # Expected (known) number of peaks due to emission lines in the spectrum
    #   The number of peak is not equal to the number of emission lines (some peaks overlap)
    n_peaks_expected = 5

    spectrum, xx = gen_xrf_spectrum(element_groups, incident_energy=12.0)

    npt.assert_almost_equal(sum(spectrum),
                            total_area,
                            err_msg="Total spectrum area is incorrect")

    # Compute the number of peaks (the number of lines) using primitive algorithm
    #   (there is no noise in simulated data)
    n_peaks = 0
    for n in range(1, len(spectrum) - 1):
        if (spectrum[n] > 1e-20) and (spectrum[n] == max(
                spectrum[n - 1:n + 2])):
            n_peaks += 1

    assert n_peaks == n_peaks_expected, "The number of peaks in simulated data is incorrect"

    # Test if the energy axis has correct values (for default parameters)
    xx_expected = 0.01 * np.arange(4096)
    npt.assert_array_almost_equal(
        xx,
        xx_expected,
        err_msg="The returned energy axis values are incorrect")