Skip to content

Commit

Permalink
PyscfParser: Add unit tests for parsing of FCIDUMP and CUBE files
Browse files Browse the repository at this point in the history
  • Loading branch information
sphuber committed Apr 3, 2023
1 parent 31582fb commit aea0182
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/aiida_pyscf/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
31 changes: 22 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=''):
Expand All @@ -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}')

Expand All @@ -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

Expand Down Expand Up @@ -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]

Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/parsers/fixtures/base/cubegen/mo_5.cube
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy fcidump file
1 change: 1 addition & 0 deletions tests/parsers/fixtures/base/cubegen/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy fcidump file
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/parsers/fixtures/base/fcidump/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
28 changes: 28 additions & 0 deletions tests/parsers/test_base.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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())

0 comments on commit aea0182

Please sign in to comment.