Skip to content

Commit

Permalink
Fixes to tests and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastonero committed Feb 22, 2024
1 parent 686de86 commit c5104fd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 23 deletions.
18 changes: 9 additions & 9 deletions src/aiida_vibroscopy/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"""Validation function utilities."""
from __future__ import annotations

__all__ = ('validate_tot_magnetization', 'validate_matrix', 'validate_positive', 'validate_nac')
__all__ = ('validate_tot_magnetization', 'validate_matrix', 'validate_positive')


def validate_tot_magnetization(tot_magnetization: float, thr: float = 0.2) -> bool:
"""Round the total magnetization input and return true if within threshold.
"""Round the total magnetization input and return true if outside the threshold.
This is needed because 'tot_magnetization' must be an integer in the aiida-quantumespresso input parameters.
"""
Expand Down Expand Up @@ -53,10 +53,10 @@ def validate_positive(value, _):
return 'specified value is negative.'


def validate_nac(value, _):
"""Validate that `value` is a valid non-analytical ArrayData input."""
try:
value.get_array('dielectric')
value.get_array('born_charges')
except KeyError:
return 'data does not contain `dieletric` and/or `born_charges` arraynames.'
# def validate_nac(value, _):
# """Validate that `value` is a valid non-analytical ArrayData input."""
# try:
# value.get_array('dielectric')
# value.get_array('born_charges')
# except KeyError:
# return 'data does not contain `dieletric` and/or `born_charges` arraynames.'
25 changes: 13 additions & 12 deletions tests/calculations/test_spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,33 +166,34 @@ def test_compute_methods(generate_phonopy_instance, generate_third_rank_tensors,
ph.symmetrize_force_constants()
raman, chi2 = generate_third_rank_tensors()

freqs, eigenvectors, _ = compute_active_modes(phonopy_instance=ph)
freqs, _, _ = compute_active_modes(phonopy_instance=ph)
results['active_modes_freqs'] = freqs
results['active_modes_eigvecs'] = eigenvectors
# results['active_modes_eigvecs'] = eigenvectors

freqs, eigenvectors , _ = compute_active_modes(phonopy_instance=ph, nac_direction=[0,0,1])
freqs, _ , _ = compute_active_modes(phonopy_instance=ph, nac_direction=[0,0,1])
results['active_modes_nac_freqs'] = freqs
results['active_modes_nac_eigvecs'] = eigenvectors
# results['active_modes_nac_eigvecs'] = eigenvectors

alpha, _, _ = compute_raman_susceptibility_tensors(ph, raman, chi2)
ints_hh, ints_hv = compute_raman_space_average(alpha)
results['raman_susceptibility_tensors'] = alpha
results['intensities_hh'] = ints_hh
results['intensities_hv'] = ints_hv

alpha, _, _ = compute_raman_susceptibility_tensors(ph, raman, chi2, nac_direction=[0,0,1])
results['raman_susceptibility_tensors_nac'] = alpha
# alpha, _, _ = compute_raman_susceptibility_tensors(ph, raman, chi2, nac_direction=[0,0,1])
# results['raman_susceptibility_tensors_nac'] = alpha

pols, _, _ = compute_polarization_vectors(ph)
results['polarization_vectors'] = pols
# pols, _, _ = compute_polarization_vectors(ph)
# results['polarization_vectors'] = pols

pols, _, _ = compute_polarization_vectors(ph, nac_direction=[0,0,1])
results['polarization_vectors_nac'] = pols
# pols, _, _ = compute_polarization_vectors(ph, nac_direction=[0,0,1])
# results['polarization_vectors_nac'] = pols

eps = compute_complex_dielectric(ph)
freq_range = np.linspace(10,1000,900)
eps = compute_complex_dielectric(ph, freq_range=freq_range)
results['complex_dielectric'] = eps

eps = compute_complex_dielectric(ph, nac_direction=[0,0,1])
eps = compute_complex_dielectric(ph, nac_direction=[0,0,1], freq_range=freq_range)
results['complex_dielectric_nac'] = eps

ndarrays_regression.check(results, default_tolerance=dict(atol=1e-4, rtol=1e-4))
Expand Down
Binary file modified tests/calculations/test_spectra/test_compute_methods.npz
Binary file not shown.
9 changes: 7 additions & 2 deletions tests/data/test_vibro.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@pytest.mark.usefixtures('aiida_profile')
def test_methods(generate_vibrational_data_from_forces, ndarrays_regression):
"""Test `VibrationalMixin` methods."""
import numpy as np

vibrational_data = generate_vibrational_data_from_forces()

results = {}
Expand All @@ -23,8 +25,11 @@ def test_methods(generate_vibrational_data_from_forces, ndarrays_regression):
results['powder_raman_intensities'] = vibrational_data.run_powder_raman_intensities()[0]
results['single_crystal_ir_intensities'] = vibrational_data.run_single_crystal_ir_intensities([1, 0, 0])[0]
results['powder_ir_intensities'] = vibrational_data.run_powder_ir_intensities()[0]
results['complex_dielectric_function'] = vibrational_data.run_complex_dielectric_function()
results['normal_reflectivity_spectrum'] = vibrational_data.run_normal_reflectivity_spectrum([0, 0, 1])
freq_range = np.arange(10, 1000, 10)
results['complex_dielectric_function'] = vibrational_data.run_complex_dielectric_function(freq_range=freq_range)
results['normal_reflectivity_spectrum'] = vibrational_data.run_normal_reflectivity_spectrum([
0, 0, 1
], **dict(freq_range=freq_range))

ndarrays_regression.check(results)

Expand Down
Binary file modified tests/data/test_vibro/test_methods.npz
Binary file not shown.
48 changes: 48 additions & 0 deletions tests/utils/test_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
#################################################################################
# Copyright (c), All rights reserved. #
# This file is part of the AiiDA-Vibroscopy code. #
# #
# The code is hosted on GitHub at https://github.com/bastonero/aiida-vibroscopy #
# For further information on the license, see the LICENSE.txt file #
#################################################################################
"""Test the :mod:`utils.validation`."""
import numpy as np
import pytest


def test_validate_tot_magnetization():
"""Test `validate_tot_magnetization`."""
from aiida_vibroscopy.utils.validation import validate_tot_magnetization

magnetization = 1.1
assert not validate_tot_magnetization(magnetization)

magnetization = 1.3
assert validate_tot_magnetization(magnetization)


@pytest.mark.parametrize(('value', 'message'), (
(5.0, 'value is not of the right type; only `list`, `aiida.orm.List` and `numpy.ndarray`'),
([0, 0], 'need exactly 3 diagonal elements or 3x3 arrays.'),
([[0, 0, 0], [0], [0, 0, 0]], 'matrix need to have 3x1 or 3x3 shape.'),
(np.ones((3)), None),
(np.ones((3, 3)), None),
))
def test_validate_matrix(
value,
message,
):
"""Test `validate_tot_magnetization`."""
from aiida_vibroscopy.utils.validation import validate_matrix
assert validate_matrix(value, None) == message


def test_validate_positive():
"""Test `validate_positive`."""
from aiida.orm import Float

from aiida_vibroscopy.utils.validation import validate_positive

assert validate_positive(Float(1.0), None) is None
assert validate_positive(Float(-1.0), None) == 'specified value is negative.'

0 comments on commit c5104fd

Please sign in to comment.