From cfdb706e6a23dca320d8414339c38764eb1ba7d0 Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:01:05 +0100 Subject: [PATCH 1/8] Drop max pylint Python version to 3.11 3.12 can have trouble installing euphonic dependency --- .github/workflows/pylint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index e06c4e4..4f15587 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.9", "3.12"] + python-version: ["3.9", "3.11"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} From 554347282c593ef8906a309b9290447a06adc707 Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:11:30 +0100 Subject: [PATCH 2/8] Drop license header, shebang from get_energy. Project LICENSE is ok --- mctools/generic/get_energy.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/mctools/generic/get_energy.py b/mctools/generic/get_energy.py index 32d171f..dc3267a 100644 --- a/mctools/generic/get_energy.py +++ b/mctools/generic/get_energy.py @@ -1,22 +1,3 @@ -#! /usr/bin/env python - -############################################################################### -# Copyright 2017 Adam Jackson -############################################################################### -# This program 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. -# -# This program 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 this program. If not, see . -############################################################################### - from argparse import ArgumentParser import ase.io From 57f7c83130bab8834ad735152095701108b2fc79 Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:12:12 +0100 Subject: [PATCH 3/8] Add a simple unit test for get_energy --- tests/test_get_energy.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/test_get_energy.py diff --git a/tests/test_get_energy.py b/tests/test_get_energy.py new file mode 100644 index 0000000..9c80d69 --- /dev/null +++ b/tests/test_get_energy.py @@ -0,0 +1,23 @@ +import ase.build +from ase.calculators.singlepoint import SinglePointCalculator +import ase.io +import pytest + +from mctools.generic.get_energy import get_energy + + +ENERGY = 3.141 +FILENAME = "methane.extxyz" + + +@pytest.fixture +def methane_with_energy() -> ase.Atoms: + atoms = ase.build.molecule("CH4") + atoms.calc = SinglePointCalculator(atoms, energy=ENERGY) + return atoms + + +def test_get_energy(methane_with_energy, tmp_path) -> None: + ase.io.write(tmp_path / FILENAME, methane_with_energy) + + assert get_energy(tmp_path / FILENAME) == pytest.approx(ENERGY) From f6e5c28ce9bbc503e07b16864165853d1b780921 Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:17:37 +0100 Subject: [PATCH 4/8] Pytest workflow --- .github/workflows/pytest.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/pytest.yml diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..33416b6 --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,24 @@ +name: Pytest + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9", "3.11"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install pytest + pip install . + - name: Test with pytest + run: | + pytest From fcb16fc6ea6fd267913aa28df0925964964efd7f Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:20:45 +0100 Subject: [PATCH 5/8] Pylint the main code, not the unit tests --- .github/workflows/pylint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 4f15587..ecef92a 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -21,4 +21,4 @@ jobs: pip install . - name: Analysing the code with pylint run: | - pylint $(git ls-files '*.py') + pylint $(git ls-files 'mctools/*.py') From 18fe07f47d2003e82e612c2bdf432c6d142f4a6d Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:23:52 +0100 Subject: [PATCH 6/8] Lint the tests with flake8-pytest-style This is a bit less strict overall than pylint, but enforces pytest idioms --- .github/workflows/pytest.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 33416b6..e0e15cb 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -17,8 +17,11 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install pytest + python -m pip install pytest flake8 flake8-pytest-style pip install . + - name: Lint the test files with flake8-pytest-style + run: | + flake8 tests/ - name: Test with pytest run: | pytest From 5095fa321e200eb7316d274b61b3f0f35fc1010d Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:41:31 +0100 Subject: [PATCH 7/8] Test get_energy main() call --- mctools/generic/get_energy.py | 9 +++++++-- tests/test_get_energy.py | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/mctools/generic/get_energy.py b/mctools/generic/get_energy.py index dc3267a..bf64a92 100644 --- a/mctools/generic/get_energy.py +++ b/mctools/generic/get_energy.py @@ -1,4 +1,6 @@ from argparse import ArgumentParser +from typing import List, Optional + import ase.io @@ -8,7 +10,7 @@ def get_energy(filename): return atoms.get_total_energy() -def main(): +def main(params: Optional[List[str]] = None): """Get calculated energy from output file using ASE""" parser = ArgumentParser(description="Read energy from output") @@ -16,7 +18,10 @@ def main(): default="vasprun.xml", help="Path to ab initio output file") - args = parser.parse_args() + if params: + args = parser.parse_args(params) + else: + args = parser.parse_args() energy = get_energy(args.filename) print(energy) diff --git a/tests/test_get_energy.py b/tests/test_get_energy.py index 9c80d69..5ef60da 100644 --- a/tests/test_get_energy.py +++ b/tests/test_get_energy.py @@ -3,7 +3,7 @@ import ase.io import pytest -from mctools.generic.get_energy import get_energy +from mctools.generic.get_energy import get_energy, main ENERGY = 3.141 @@ -21,3 +21,11 @@ def test_get_energy(methane_with_energy, tmp_path) -> None: ase.io.write(tmp_path / FILENAME, methane_with_energy) assert get_energy(tmp_path / FILENAME) == pytest.approx(ENERGY) + + +def test_main(methane_with_energy, tmp_path, capsys) -> None: + ase.io.write(tmp_path / FILENAME, methane_with_energy) + + main([str(tmp_path / FILENAME)]) + captured = capsys.readouterr() + assert float(captured.out) == pytest.approx(ENERGY) From 10ba81192766ac32b9bccc7d215192307898d102 Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Tue, 28 May 2024 16:56:47 +0100 Subject: [PATCH 8/8] Update README: mention unit tests, drop ase-convert --- README.md | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cc0ac94..b495a48 100644 --- a/README.md +++ b/README.md @@ -6,22 +6,15 @@ and developed further while at [Scanlon Materials Theory Group](https://github.c The recommended way of installing is to clone the repository and make a local installation using pip: -``` shell +```bash git clone https://github.com/ajjackson/mctools.git cd mctools pip install --user -e . ``` -ase-convert (Convert structure files) -------------------------------------- -Use ASE to read a crystal structure file and write out to target format. Call with `-h` flag for usage information. ASE can also get this information from some output file formats, which is useful. - -``` -# EXAMPLE: read relaxed structure from aims output and write VASP input -bash> ase_convert.py aims.out POSCAR - -# EXAMPLE: Convert between files with non-standard names -bash> ase_convert.py -f vasp MY_SUPER_POSCAR -t cif MY_SUPER_CIF +To run unit tests, install pytest and run with +```bash +python -m pytest ``` get-spacegroup (Spacegroup tolerances)