Skip to content

Commit

Permalink
Testing: Add coverage for simulation classes
Browse files Browse the repository at this point in the history
  • Loading branch information
CSSFrancis committed Apr 17, 2024
1 parent d1839a5 commit 3567308
Show file tree
Hide file tree
Showing 4 changed files with 551 additions and 1 deletion.
Expand Up @@ -177,7 +177,7 @@ def test_appropriate_intensities(self, diffraction_calculator, local_structure):
"""Tests the central beam is strongest."""
diffraction = diffraction_calculator.calculate_diffraction2d(
local_structure, reciprocal_radius=0.5, with_direct_beam=True
) # direct beam doesn't work
)
indices = [tuple(np.round(i).astype(int)) for i in diffraction.coordinates.hkl]
central_beam = indices.index((0, 0, 0))

Expand All @@ -187,6 +187,14 @@ def test_appropriate_intensities(self, diffraction_calculator, local_structure):
)
assert np.all(smaller)

def test_direct_beam(self, diffraction_calculator, local_structure):
diffraction = diffraction_calculator.calculate_diffraction2d(
local_structure, reciprocal_radius=0.5, with_direct_beam=False
)
indices = [tuple(np.round(i).astype(int)) for i in diffraction.coordinates.hkl]
with pytest.raises(ValueError):
indices.index((0, 0, 0))

def test_shape_factor_strings(self, diffraction_calculator, local_structure):
_ = diffraction_calculator.calculate_diffraction2d(
local_structure,
Expand Down
17 changes: 17 additions & 0 deletions diffsims/tests/simulations/__init__.py
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2024 The diffsims developers
#
# This file is part of diffsims.
#
# diffsims is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffsims is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.
69 changes: 69 additions & 0 deletions diffsims/tests/simulations/test_simulations1d.py
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2024 The diffsims developers
#
# This file is part of diffsims.
#
# diffsims is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffsims is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.
import matplotlib.pyplot as plt
import pytest

from orix.crystal_map import Phase
import numpy as np

from diffsims.tests.generators.test_simulation_generator import make_phase
from diffsims.simulations import Simulation1D


class TestSingleSimulation:
@pytest.fixture
def simulation1d(self):
al_phase = make_phase()
al_phase.name = "Al"
hkls = np.array(["100", "110", "111"])
magnitudes = np.array([1, 2, 3])
inten = np.array([1, 2, 3])
recip = 4.0

return Simulation1D(
phase=al_phase,
hkl=hkls,
reciprocal_spacing=magnitudes,
intensities=inten,
reciprocal_radius=recip,
wavelength=0.025,
)

def test_init(self, simulation1d):
assert isinstance(simulation1d, Simulation1D)
assert isinstance(simulation1d.phase, Phase)
assert isinstance(simulation1d.hkl, np.ndarray)
assert isinstance(simulation1d.reciprocal_spacing, np.ndarray)
assert isinstance(simulation1d.intensities, np.ndarray)
assert isinstance(simulation1d.reciprocal_radius, float)

@pytest.mark.parametrize("annotate", [True, False])
@pytest.mark.parametrize("ax", [None, "new"])
@pytest.mark.parametrize("with_labels", [True, False])
def test_plot(self, simulation1d, annotate, ax, with_labels):
if ax == "new":
fig, ax = plt.subplots()
fig = simulation1d.plot(annotate_peaks=annotate, ax=ax, with_labels=with_labels)

def test_repr(self, simulation1d):
assert simulation1d.__repr__() == "Simulation1D(name: Al, wavelength: 0.025)"

def test_theta(self, simulation1d):
np.testing.assert_almost_equal(
simulation1d.theta, np.array([0.02499479, 0.0499584, 0.07485985])
)

0 comments on commit 3567308

Please sign in to comment.