From aea01823b82121dc114a2b1cdd8de0a0c95a2abe Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Sun, 2 Apr 2023 15:14:41 +0200 Subject: [PATCH] `PyscfParser`: Add unit tests for parsing of FCIDUMP and CUBE files --- src/aiida_pyscf/parsers/base.py | 5 +++ tests/conftest.py | 31 +++++++++++++------ tests/parsers/fixtures/base/cubegen/aiida.err | 0 tests/parsers/fixtures/base/cubegen/aiida.out | 0 tests/parsers/fixtures/base/cubegen/mo_5.cube | 1 + .../fixtures/base/cubegen/results.json | 1 + .../base/fcidump/active_space_5_6_8_9.fcidump | 1 + tests/parsers/fixtures/base/fcidump/aiida.err | 0 tests/parsers/fixtures/base/fcidump/aiida.out | 0 .../fixtures/base/fcidump/results.json | 1 + tests/parsers/test_base.py | 28 +++++++++++++++++ 11 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 tests/parsers/fixtures/base/cubegen/aiida.err create mode 100644 tests/parsers/fixtures/base/cubegen/aiida.out create mode 100644 tests/parsers/fixtures/base/cubegen/mo_5.cube create mode 100644 tests/parsers/fixtures/base/cubegen/results.json create mode 100644 tests/parsers/fixtures/base/fcidump/active_space_5_6_8_9.fcidump create mode 100644 tests/parsers/fixtures/base/fcidump/aiida.err create mode 100644 tests/parsers/fixtures/base/fcidump/aiida.out create mode 100644 tests/parsers/fixtures/base/fcidump/results.json diff --git a/src/aiida_pyscf/parsers/base.py b/src/aiida_pyscf/parsers/base.py index 9b90354..71e214d 100644 --- a/src/aiida_pyscf/parsers/base.py +++ b/src/aiida_pyscf/parsers/base.py @@ -58,7 +58,12 @@ def parse(self, retrieved_temporary_folder: str | None = None, **kwargs): # pyl for filepath_cubegen in dirpath_temporary.glob('*.cube'): self.out(f'cubegen.{filepath_cubegen.stem}', SinglefileData(filepath_cubegen)) + print(list(dirpath_temporary.iterdir())) for filepath_fcidump in dirpath_temporary.glob('*.fcidump'): + print(filepath_fcidump) + print('KEY', f'fcidump.{filepath_fcidump.stem}') + print(self.node.process_class) + print(self.node.process_class.spec().outputs) self.out(f'fcidump.{filepath_fcidump.stem}', SinglefileData(filepath_fcidump)) self.out('parameters', Dict(parsed_json)) diff --git a/tests/conftest.py b/tests/conftest.py index 17b4992..a4689fd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -69,7 +69,7 @@ def factory(process_class, inputs=None, return_process=False): @pytest.fixture -def generate_calc_job_node(filepath_tests, aiida_computer_local): +def generate_calc_job_node(filepath_tests, aiida_computer_local, tmp_path): """Create and return a :class:`aiida.orm.CalcJobNode` instance.""" def flatten_inputs(inputs, prefix=''): @@ -82,7 +82,9 @@ def flatten_inputs(inputs, prefix=''): flat_inputs.append((prefix + key, value)) return flat_inputs - def factory(entry_point: str, test_name: str | None = None, inputs: dict = None): + def factory( + entry_point: str, test_name: str, inputs: dict = None, retrieve_temporary_list: list[str] | None = None + ): """Create and return a :class:`aiida.orm.CalcJobNode` instance.""" node = CalcJobNode(computer=aiida_computer_local(), process_type=f'aiida.calculations:{entry_point}') @@ -93,13 +95,24 @@ def factory(entry_point: str, test_name: str | None = None, inputs: dict = None) node.store() - if test_name: - filepath_retrieved = filepath_tests / 'parsers' / 'fixtures' / entry_point.split('.')[-1] / test_name + filepath_retrieved = filepath_tests / 'parsers' / 'fixtures' / entry_point.split('.')[-1] / test_name - retrieved = FolderData() - retrieved.base.repository.put_object_from_tree(filepath_retrieved) - retrieved.base.links.add_incoming(node, link_type=LinkType.CREATE, link_label='retrieved') - retrieved.store() + retrieved = FolderData() + retrieved.base.repository.put_object_from_tree(filepath_retrieved) + retrieved.base.links.add_incoming(node, link_type=LinkType.CREATE, link_label='retrieved') + retrieved.store() + + if retrieve_temporary_list: + for pattern in retrieve_temporary_list: + for filename in filepath_retrieved.glob(pattern): + filepath = tmp_path / filename.relative_to(filepath_retrieved) + print(tmp_path) + print(filename) + print(filename.relative_to(filepath_retrieved)) + print(filepath) + filepath.write_bytes(filename.read_bytes()) + + return node, tmp_path return node @@ -144,7 +157,7 @@ def factory(inputs=None, exit_code=None): :param exit_code: exit code for the ``PyscfCalculation``. """ process = generate_workchain('pyscf.base', {'pyscf': inputs or generate_inputs_pyscf()}) - node = generate_calc_job_node('pyscf.base', inputs={'parameters': Dict()}) + node = generate_calc_job_node('pyscf.base', 'default', inputs={'parameters': Dict()}) process.ctx.iteration = 1 process.ctx.children = [node] diff --git a/tests/parsers/fixtures/base/cubegen/aiida.err b/tests/parsers/fixtures/base/cubegen/aiida.err new file mode 100644 index 0000000..e69de29 diff --git a/tests/parsers/fixtures/base/cubegen/aiida.out b/tests/parsers/fixtures/base/cubegen/aiida.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/parsers/fixtures/base/cubegen/mo_5.cube b/tests/parsers/fixtures/base/cubegen/mo_5.cube new file mode 100644 index 0000000..03cc3ac --- /dev/null +++ b/tests/parsers/fixtures/base/cubegen/mo_5.cube @@ -0,0 +1 @@ +dummy fcidump file diff --git a/tests/parsers/fixtures/base/cubegen/results.json b/tests/parsers/fixtures/base/cubegen/results.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/parsers/fixtures/base/cubegen/results.json @@ -0,0 +1 @@ +{} diff --git a/tests/parsers/fixtures/base/fcidump/active_space_5_6_8_9.fcidump b/tests/parsers/fixtures/base/fcidump/active_space_5_6_8_9.fcidump new file mode 100644 index 0000000..03cc3ac --- /dev/null +++ b/tests/parsers/fixtures/base/fcidump/active_space_5_6_8_9.fcidump @@ -0,0 +1 @@ +dummy fcidump file diff --git a/tests/parsers/fixtures/base/fcidump/aiida.err b/tests/parsers/fixtures/base/fcidump/aiida.err new file mode 100644 index 0000000..e69de29 diff --git a/tests/parsers/fixtures/base/fcidump/aiida.out b/tests/parsers/fixtures/base/fcidump/aiida.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/parsers/fixtures/base/fcidump/results.json b/tests/parsers/fixtures/base/fcidump/results.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/parsers/fixtures/base/fcidump/results.json @@ -0,0 +1 @@ +{} diff --git a/tests/parsers/test_base.py b/tests/parsers/test_base.py index 09ab0a4..4dbcfb1 100644 --- a/tests/parsers/test_base.py +++ b/tests/parsers/test_base.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- """Tests for the :mod:`aiida_pyscf.parsers.base` module.""" # pylint: disable=redefined-outer-name +from aiida.orm import SinglefileData + from aiida_pyscf.calculations.base import PyscfCalculation @@ -36,3 +38,29 @@ def test_failed_missing_result(generate_calc_job_node, generate_parser): assert calcfunction.is_failed assert calcfunction.exit_status == PyscfCalculation.exit_codes.ERROR_OUTPUT_RESULTS_MISSING.status + + +def test_cubegen(generate_calc_job_node, generate_parser, generate_structure): + """Test parsing the outputs of a job that computed CUBE files.""" + inputs = {'structure': generate_structure('N2')} + node, tmp_path = generate_calc_job_node('pyscf.base', 'cubegen', inputs, retrieve_temporary_list=['*.cube']) + parser = generate_parser('pyscf.base') + results, calcfunction = parser.parse_from_node(node, retrieved_temporary_folder=tmp_path, store_provenance=False) + + assert calcfunction.is_finished, calcfunction.exception + assert calcfunction.is_finished_ok, calcfunction.exit_message + assert 'cubegen' in results + assert all(isinstance(node, SinglefileData) for node in results['cubegen'].values()) + + +def test_fcidump(generate_calc_job_node, generate_parser, generate_structure): + """Test parsing the outputs of a job that computed FCIDUMP files.""" + inputs = {'structure': generate_structure('N2')} + node, tmp_path = generate_calc_job_node('pyscf.base', 'fcidump', inputs, retrieve_temporary_list=['*.fcidump']) + parser = generate_parser('pyscf.base') + results, calcfunction = parser.parse_from_node(node, retrieved_temporary_folder=tmp_path, store_provenance=False) + + assert calcfunction.is_finished, calcfunction.exception + assert calcfunction.is_finished_ok, calcfunction.exit_message + assert 'fcidump' in results + assert all(isinstance(node, SinglefileData) for node in results['fcidump'].values())