Skip to content

Commit

Permalink
Merge pull request #34 from nomad-coe/apply_ruff
Browse files Browse the repository at this point in the history
- Update Ruff linting
- Add Ruff formatter
- Remove old linters (`pycodestyle` and `pylint`)
  • Loading branch information
ndaelman-hu committed Feb 22, 2024
2 parents 80342a5 + 568f9b7 commit 3c1439d
Show file tree
Hide file tree
Showing 41 changed files with 4,253 additions and 2,464 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/python-actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ jobs:
pip install --upgrade pip
pip install .[tests]
pip install types-PyYAML
- name: pycodestyle
run: |
python -m pycodestyle --ignore=E501,E701,E731 *parsers tests
- name: pylint
run: |
python -m pylint *parsers tests
- name: mypy
run: |
python -m mypy --ignore-missing-imports --follow-imports=silent --no-strict-optional *parsers tests
- name: Test with pytest
run: |
python -m pytest -sv tests
ruff:
ruff-linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
with:
args: "check ."

ruff-formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
with:
args: "format ."
24 changes: 19 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ homepage = "https://github.com/nomad-coe/electronic-parsers"
[project.optional-dependencies]
tests = [
'mypy==1.0.1',
'pylint==2.13.9',
'pylint_plugin_utils==0.5',
'pycodestyle==2.8.0',
'pytest==3.10.0',
'pytest-timeout==1.4.2',
'pytest-cov==2.7.1',
Expand All @@ -39,11 +36,11 @@ tests = [
'phonopy==2.11.0',
'h5py==3.6.0',
'typing-extensions==4.4.0',
'ruff==0.1.4',
'ruff==0.1.8',
]

[tool.ruff]
include = ["electronicparsers/*.py", "tests/*.py"]
include = ["workflowparsers/*.py", "tests/*.py"]
select = [
"E", # pycodestyle
"W", # pycodestyle
Expand All @@ -66,6 +63,23 @@ ignore = [
]
fixable = ["ALL"]

# Same as Black.
line-length = 88
indent-width = 4

[tool.ruff.format]
# use single quotes for strings.
quote-style = "single"

# indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

[tool.setuptools.packages.find]
include = ["workflowparsers*"]

Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#
import os
import pytest

# Set up pytest to pass control to the debugger on an exception.
if os.getenv('_PYTEST_RAISE', "0") != "0":
if os.getenv('_PYTEST_RAISE', '0') != '0':

@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
Expand Down
26 changes: 18 additions & 8 deletions tests/test_aflowparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ def test_aflowlib(parser):
sec_calculation = archive.run[0].calculation[0]
assert sec_calculation.energy.total.value.magnitude == approx(-5.58872856e-18)
assert sec_calculation.forces.total.value[0][1].magnitude == approx(-2.14691669e-13)
assert sec_calculation.thermodynamics[0].enthalpy.magnitude == approx(-5.58872856e-18)
assert sec_calculation.thermodynamics[0].enthalpy.magnitude == approx(
-5.58872856e-18
)
assert sec_calculation.x_aflow_pressure_residual == approx(2.95)

run = archive.run[1]
assert len(run.system) == len(run.calculation) == 24
assert run.system[7].atoms.labels[6] == 'O'
assert run.system[21].atoms.lattice_vectors[0][1].magnitude == approx(-2.5241880209758e-10)
assert run.system[21].atoms.lattice_vectors[0][1].magnitude == approx(
-2.5241880209758e-10
)
assert run.calculation[15].energy.total.value.magnitude == approx(-6.9854741e-19)
assert run.calculation[9].stress.total.value[2][2].magnitude == approx(-1.594e+08)
assert run.calculation[9].stress.total.value[2][2].magnitude == approx(-1.594e08)
# TODO currently workflow is not a repeating section
# assert archive.workflow.results.n_deformations == 3
# assert archive.workflow.results.strain_maximum == pytest.approx(0.01)
Expand All @@ -68,7 +72,7 @@ def test_aflowlib(parser):
run = archive.run[2]
assert len(run.system) == len(run.calculation) == 28
assert run.system[3].atoms.positions[3][2].magnitude == approx(5.53515521e-10)
assert run.calculation[19].thermodynamics[0].pressure.magnitude == (-1.6886e+09)
assert run.calculation[19].thermodynamics[0].pressure.magnitude == (-1.6886e09)
sec_thermo = archive.workflow2.results
assert sec_thermo.temperature[12].magnitude == 120
# assert sec_thermo.thermal_conductivity[18].magnitude == approx(4.924586)
Expand All @@ -90,13 +94,19 @@ def test_aflowin(parser):

sec_scc = archive.run[1].calculation[0]
assert sec_scc.dos_phonon[0].energies[80].magnitude == approx(5.71011064e-22)
assert sec_scc.dos_phonon[0].total[0].value[1866].magnitude == approx(2.06688135e+20)
assert sec_scc.dos_phonon[0].total[0].value[1866].magnitude == approx(2.06688135e20)
assert len(sec_scc.band_structure_phonon[0].segment) == 10
assert sec_scc.band_structure_phonon[0].segment[3].kpoints[7][1] == approx(1.02984830)
assert sec_scc.band_structure_phonon[0].segment[9].energies[0][10][3].magnitude == approx(1.92480691e-21)
assert sec_scc.band_structure_phonon[0].segment[3].kpoints[7][1] == approx(
1.02984830
)
assert sec_scc.band_structure_phonon[0].segment[9].energies[0][10][
3
].magnitude == approx(1.92480691e-21)

assert archive.workflow2.results.qpoints[9249][0] == approx(-4.7619047619e-02)
assert archive.workflow2.results.group_velocity[234][2][0].magnitude == approx(-133.348333)
assert archive.workflow2.results.group_velocity[234][2][0].magnitude == approx(
-133.348333
)

# TODO currently workflow is not a repeating section
# assert archive.workflow.results.temperature[161].magnitude == approx(1610)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_asrparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ def test_database():
cwd = os.getcwd()
try:
os.chdir(tests_path)
subprocess.Popen(['asr', 'init'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.Popen(
['asr', 'init'], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
subprocess.Popen(
['asr', 'run', 'asr.c2db.relax -a Si.json -c {"name":"emt"}'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception:
pass
finally:
Expand Down
38 changes: 23 additions & 15 deletions tests/test_elasticparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,27 @@ def test_2nd(parser):
assert sec_strain[0].value[2][5] == approx(-3.30877062e-16)
assert sec_strain[3].type == 'cross-validation'
assert sec_strain[2].eta[1][2] == 0.03
assert sec_strain[6].value[2][4] == approx(6.8708895e+12)
assert sec_strain[6].value[2][4] == approx(6.8708895e12)
assert sec_strain[4].polynomial_fit_order == 6

assert results.elastic_constants_notation_matrix_second_order[1][2] == 'C12'
assert results.elastic_constants_matrix_second_order[0][2].magnitude == approx(1.008e+11)
assert results.elastic_constants_matrix_second_order[0][2].magnitude == approx(
1.008e11
)
assert results.compliance_matrix_second_order[3][3].magnitude == approx(1.75e-12)
assert results.bulk_modulus_voigt.magnitude == approx(4.4937e+11)
assert results.shear_modulus_voigt.magnitude == approx(5.3074e+11)
assert results.bulk_modulus_reuss.magnitude == approx(4.4937e+11)
assert results.shear_modulus_reuss.magnitude == approx(5.2574e+11)
assert results.bulk_modulus_hill.magnitude == approx(4.4937e+11)
assert results.shear_modulus_hill.magnitude == approx(5.2824e+11)
assert results.young_modulus_voigt.magnitude == approx(1.14245e+12)
assert results.bulk_modulus_voigt.magnitude == approx(4.4937e11)
assert results.shear_modulus_voigt.magnitude == approx(5.3074e11)
assert results.bulk_modulus_reuss.magnitude == approx(4.4937e11)
assert results.shear_modulus_reuss.magnitude == approx(5.2574e11)
assert results.bulk_modulus_hill.magnitude == approx(4.4937e11)
assert results.shear_modulus_hill.magnitude == approx(5.2824e11)
assert results.young_modulus_voigt.magnitude == approx(1.14245e12)
assert results.poisson_ratio_voigt == 0.08
assert results.young_modulus_reuss.magnitude == approx(1.1347e+12)
assert results.young_modulus_reuss.magnitude == approx(1.1347e12)
assert results.poisson_ratio_reuss == 0.08
assert results.young_modulus_hill.magnitude == approx(1.13858e+12)
assert results.young_modulus_hill.magnitude == approx(1.13858e12)
assert results.poisson_ratio_hill == 0.08
assert results.eigenvalues_elastic[1].magnitude == approx(1.3481e+12)
assert results.eigenvalues_elastic[1].magnitude == approx(1.3481e12)

sec_scc = archive.run[0].calculation[0]
assert len(sec_scc.calculations_path) == 33
Expand All @@ -94,9 +96,15 @@ def test_3rd(parser):
# assert sec_strain[3].value[8][7] == approx(2.06899957e-23)

results = archive.workflow2.results
assert results.elastic_constants_matrix_third_order[3][1][3].magnitude == approx(1.274e+10)
assert results.elastic_constants_matrix_third_order[5][2][5].magnitude == approx(1.2825e+10)
assert results.elastic_constants_matrix_third_order[0][0][1].magnitude == approx(-1.18334e+12)
assert results.elastic_constants_matrix_third_order[3][1][3].magnitude == approx(
1.274e10
)
assert results.elastic_constants_matrix_third_order[5][2][5].magnitude == approx(
1.2825e10
)
assert results.elastic_constants_matrix_third_order[0][0][1].magnitude == approx(
-1.18334e12
)


def test_stress(parser):
Expand Down
46 changes: 33 additions & 13 deletions tests/test_fhivibesparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,38 @@ def _test_singlepoint(parser):
assert len(sec_run) == 10
assert len(sec_run[2].calculation) == 1

assert sec_run[8].system[0].atoms.positions[3][2].magnitude == approx(5.30098546e-10)
assert sec_run[5].system[0].atoms.velocities[1][0].magnitude == approx(-2.18864066e+03)
assert sec_run[8].system[0].atoms.positions[3][2].magnitude == approx(
5.30098546e-10
)
assert sec_run[5].system[0].atoms.velocities[1][0].magnitude == approx(
-2.18864066e03
)

sec_scc = sec_run[9].calculation[0]
assert len(sec_scc.energy.contributions) == 2
assert sec_scc.energy.contributions[1].value.magnitude == approx(-1.00925367e-14)

sec_scc = sec_run[3].calculation[0]
assert sec_scc.stress.contributions[0].value[1][2].magnitude == approx(-1.42111377e+07)
assert sec_scc.stress.contributions[0].value[1][2].magnitude == approx(
-1.42111377e07
)

sec_scc = sec_run[1].calculation[0]
assert sec_scc.stress.total.value[1][1].magnitude == approx(1.49076266e+08)
assert sec_scc.stress.total.value[1][1].magnitude == approx(1.49076266e08)

sec_scc = sec_run[6].calculation[0]
assert sec_scc.forces.total.value[5][2].magnitude == approx(-3.47924808e-10)

sec_scc = sec_run[5].calculation[0]
assert sec_scc.thermodynamics[0].pressure.magnitude == approx(2.52108927e+07)
assert sec_scc.thermodynamics[0].pressure.magnitude == approx(2.52108927e07)

sec_scc = sec_run[2].calculation[0]
assert sec_scc.x_fhi_vibes_pressure_kinetic.magnitude == approx(2.08283962e+08)
assert sec_scc.x_fhi_vibes_pressure_kinetic.magnitude == approx(2.08283962e08)

sec_scc = sec_run[8].calculation[0]
assert sec_scc.x_fhi_vibes_energy_potential_harmonic.magnitude == approx(4.08242214e-20)
assert sec_scc.x_fhi_vibes_energy_potential_harmonic.magnitude == approx(
4.08242214e-20
)


def _test_relaxation(parser):
Expand All @@ -93,7 +101,9 @@ def _test_relaxation(parser):
sec_sccs = archive.run[0].calculation
assert len(sec_sccs) == 3
assert sec_sccs[2].thermodynamics[0].volume.magnitude == approx(3.97721030e-29)
assert sec_sccs[0].energy.contributions[1].value.magnitude == approx(-2.52313962e-15)
assert sec_sccs[0].energy.contributions[1].value.magnitude == approx(
-2.52313962e-15
)


def _test_molecular_dynamics(parser):
Expand All @@ -103,19 +113,27 @@ def _test_molecular_dynamics(parser):
assert archive.workflow2.m_def.name == 'MolecularDynamics'

sec_attrs = archive.run[0].method[0].x_fhi_vibes_section_attributes[0]
sec_md = sec_attrs.x_fhi_vibes_section_attributes_metadata[0].x_fhi_vibes_section_metadata_MD[0]
sec_md = sec_attrs.x_fhi_vibes_section_attributes_metadata[
0
].x_fhi_vibes_section_metadata_MD[0]
assert sec_md.x_fhi_vibes_MD_md_type == 'Langevin'
assert sec_md.x_fhi_vibes_MD_friction == 0.02

sec_systems = archive.run[0].system
assert len(sec_systems) == 11
assert sec_systems[3].atoms.positions[6][1].magnitude == approx(1.39537854e-10)
assert sec_systems[7].atoms.velocities[1][0].magnitude == approx(-249.97586102)
assert sec_systems[2].atoms.lattice_vectors[0][2].magnitude == approx(2.20004000e-21)
assert sec_systems[2].atoms.lattice_vectors[0][2].magnitude == approx(
2.20004000e-21
)

sec_sccs = archive.run[0].calculation
assert sec_sccs[4].x_fhi_vibes_heat_flux_0_harmonic[1].magnitude == approx(1.40863863e+13)
assert sec_sccs[5].x_fhi_vibes_atom_forces_harmonic[3][0].magnitude == approx(8.40976902e-10)
assert sec_sccs[4].x_fhi_vibes_heat_flux_0_harmonic[1].magnitude == approx(
1.40863863e13
)
assert sec_sccs[5].x_fhi_vibes_atom_forces_harmonic[3][0].magnitude == approx(
8.40976902e-10
)
assert sec_sccs[6].x_fhi_vibes_momenta[7][2].magnitude == approx(-1.18929315e-24)


Expand All @@ -126,7 +144,9 @@ def _test_phonon(parser):
assert archive.workflow2.m_def.name == 'Phonon'

sec_attrs = archive.run[0].method[0].x_fhi_vibes_section_attributes[0]
sec_phonon = sec_attrs.x_fhi_vibes_section_attributes_metadata[0].x_fhi_vibes_section_metadata_phonopy[0]
sec_phonon = sec_attrs.x_fhi_vibes_section_attributes_metadata[
0
].x_fhi_vibes_section_metadata_phonopy[0]
assert sec_phonon.x_fhi_vibes_phonopy_version == '2.6.1'
sec_atoms = sec_phonon.x_fhi_vibes_section_phonopy_primitive[0]
assert np.shape(sec_atoms.x_fhi_vibes_atoms_positions) == (2, 3)
Expand Down
Loading

0 comments on commit 3c1439d

Please sign in to comment.