class TestRegression: @pytest.fixture def inject_mocks(self, mocker): # Prevent calls to show so we can get the current figure using # gcf() mocker.patch('matplotlib.pyplot.show') mocker.resetall() def teardown_method(self): # Ensure figures are closed matplotlib.pyplot.close('all') @pytest.mark.parametrize('dispersion_args', disp_params) def test_plots_contain_expected_data(self, inject_mocks, dispersion_args): euphonic.cli.dispersion.main(dispersion_args) line_data = get_current_plot_line_data() with open(disp_output_file, 'r') as f: expected_line_data = json.load(f)[args_to_key(dispersion_args)] # Increase tolerance if asr present - can give slightly # different results with different libs if any(['--asr' in arg for arg in dispersion_args]): atol = 1.5e-6 else: atol = sys.float_info.epsilon for key, value in line_data.items(): if key == 'xy_data': # numpy can only auto convert 2D lists - xy_data has # dimensions (n_lines, 2, n_points) so check in a loop for idx, line in enumerate(value): npt.assert_allclose(line, expected_line_data[key][idx], atol=atol) else: assert value == expected_line_data[key] @pytest.mark.parametrize( 'dispersion_args', [[quartz_phonon_file, '--save-to'], [quartz_phonon_file, '-s']]) def test_plot_save_to_file(self, inject_mocks, tmpdir, dispersion_args): output_file = str(tmpdir.join('test.png')) euphonic.cli.dispersion.main(dispersion_args + [output_file]) assert os.path.exists(output_file) @pytest.mark.parametrize( 'dispersion_args', [[os.path.join(get_data_path(), 'crystal', 'crystal_LZO.json')], [ os.path.join(get_data_path(), 'force_constants', 'NaCl', 'FORCE_CONSTANTS') ]]) def test_invalid_file_raises_value_error(self, dispersion_args): with pytest.raises(ValueError): euphonic.cli.dispersion.main(dispersion_args)
def get_qpt_freqs_dir(material): return os.path.join(get_data_path(), 'qpoint_frequencies', material)
def get_dw_dir(material): return os.path.join(get_data_path(), 'debye_waller', material)
class TestRegression: @pytest.fixture def inject_mocks(self, mocker): # Prevent calls to show so we can get the current figure using # gcf() mocker.patch('matplotlib.pyplot.show') mocker.resetall() def teardown_method(self): # Ensure figures are closed matplotlib.pyplot.close('all') @pytest.mark.parametrize('powder_map_args', powder_map_params) def test_plots_produce_expected_image(self, inject_mocks, powder_map_args): euphonic.cli.powder_map.main(powder_map_args) image_data = get_current_plot_image_data() with open(powder_map_output_file, 'r') as expected_data_file: expected_image_data = json.load(expected_data_file)[args_to_key( powder_map_args)] for key, value in image_data.items(): if key == 'extent': # Lower bound of y-data (energy) varies by up to ~2e-6 on # different systems when --asr is used, compared to # the upper bound of 100s of meV this is effectively zero, # so increase tolerance to allow for this npt.assert_allclose(value, expected_image_data[key], atol=2e-6) elif isinstance(value, list) and isinstance(value[0], float): # Errors of 2-4 epsilon seem to be common when using # broadening, so slightly increase tolerance npt.assert_allclose(value, expected_image_data[key], atol=1e-14) else: assert value == expected_image_data[key] @pytest.mark.parametrize( 'powder_map_args', [[nacl_prim_fc_file, '--save-to'], [nacl_prim_fc_file, '-s']]) def test_plot_save_to_file(self, inject_mocks, tmpdir, powder_map_args): output_file = str(tmpdir.join('test.png')) euphonic.cli.powder_map.main(powder_map_args + [output_file] + quick_calc_params) assert os.path.exists(output_file) @pytest.mark.parametrize( 'powder_map_args', [[os.path.join(get_data_path(), 'util', 'qgrid_444.txt')]]) def test_invalid_file_raises_value_error(self, powder_map_args): with pytest.raises(ValueError): euphonic.cli.powder_map.main(powder_map_args) @pytest.mark.parametrize('powder_map_args', [[nacl_prim_fc_file, '-w=incoherent']]) def test_invalid_weights_raises_causes_exit(self, powder_map_args): # Argparse should call sys.exit on invalid choices with pytest.raises(SystemExit) as err: euphonic.cli.powder_map.main(powder_map_args) assert err.type == SystemExit assert err.value.code == 2
def get_spectrum_dir(): return os.path.join(get_data_path(), 'spectrum1dcollection')
def get_sf_dir(material): return os.path.join(get_data_path(), 'structure_factor', material)
def get_filepath(filename): return os.path.join(get_data_path(), 'readers', filename)
def get_spectrum2d_dir(): return os.path.join(get_data_path(), 'spectrum2d')
def get_qpt_ph_modes_dir(material): return os.path.join(get_data_path(), 'qpoint_phonon_modes', material)
import os import json from unittest.mock import patch import pytest import numpy.testing as npt # Required for mocking import matplotlib.pyplot from tests_and_analysis.test.utils import get_data_path, get_castep_path from tests_and_analysis.test.script_tests.utils import ( get_script_test_data_path, get_current_plot_image_data, args_to_key) import euphonic.cli.intensity_map quartz_phonon_file = os.path.join( get_data_path(), 'qpoint_phonon_modes', 'quartz', 'quartz_bandstructure_qpoint_phonon_modes.json') lzo_phonon_file = get_castep_path('LZO', 'La2Zr2O7.phonon') graphite_fc_file = get_castep_path('graphite', 'graphite.castep_bin') intensity_map_output_file = os.path.join(get_script_test_data_path(), 'intensity-map.json') intensity_map_params = [ [graphite_fc_file], [graphite_fc_file, '--v-min=0', '--v-max=1e-10'], [graphite_fc_file, '--energy-unit=meV'], [graphite_fc_file, '--weights=coherent', '--cmap=bone'], [graphite_fc_file, '--weights=coherent', '--temperature=800'], [graphite_fc_file, '-w', 'dos', '--y-label=DOS', '--title=DOS TITLE'], [graphite_fc_file, '--e-min=50', '-u=cm^-1', '--x-label=wavenumber'], [ graphite_fc_file, '--e-min=-100', '--e-max=1000', '--ebins=100', '--energy-unit=cm^-1'
def get_filepath(filename): return os.path.join(get_data_path(), 'crystal', filename)
def get_fc_dir(): return os.path.join(get_data_path(), 'force_constants')
import json from unittest.mock import patch import pytest import numpy.testing as npt # Required for mocking import matplotlib.pyplot from tests_and_analysis.test.utils import (get_data_path, get_castep_path, get_phonopy_path) from tests_and_analysis.test.script_tests.utils import ( get_script_test_data_path, get_current_plot_line_data, args_to_key) import euphonic.cli.dispersion cahgo2_fc_file = get_phonopy_path('CaHgO2', 'mp-7041-20180417.yaml') lzo_fc_file = os.path.join(get_data_path(), 'force_constants', 'LZO_force_constants.json') nacl_fc_file = get_phonopy_path('NaCl_cli_test', 'force_constants.hdf5') nacl_phonon_file = os.path.join(get_phonopy_path('NaCl', 'band'), 'band.yaml') nacl_phonon_hdf5_file = os.path.join(get_phonopy_path('NaCl', 'band'), 'band.hdf5') quartz_phonon_file = os.path.join( get_data_path(), 'qpoint_phonon_modes', 'quartz', 'quartz_bandstructure_qpoint_phonon_modes.json') disp_output_file = os.path.join(get_script_test_data_path(), 'dispersion.json') disp_params = [[cahgo2_fc_file], [cahgo2_fc_file, '--energy-unit=hartree'], [ cahgo2_fc_file, '--x-label=wavenumber', '--y-label=Energy (meV)', '--title=CaHgO2' ], [cahgo2_fc_file, '--reorder'], [cahgo2_fc_file, '-u=1/cm', '--e-min=200'],