-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Engine: add
CalcJobImporter
class and associated entry point group
The `CalcJobImporter` class is added, which defines a single abstract staticmethod `parse_remote_data`. The idea is that plugins can define an importer for a `CalcJob` implementation and implement this method. The method takes a `RemoteData` node that points to a path on the associated computer that contains the input and output files of a calculation that has been run outside of AiiDA, but by an executable that is normally run with this particular `CalcJob`. The `parse_remote_data` implementation should read the input files found in the remote data and parse their content into the input nodes that when used to launch the calculation job, would result in similar input files. These inputs, including the `RemoteData` as the `remote_folder` input, can then be used to run an instance of this particular `CalcJob`. The engine will recognize the `remote_folder` input, signalling an import job, and instead of running a normal job that creates the input files on the remote before submitting it to the scheduler, it passes straight to the retrieve step. This will retrieve the files from the `RemoteData` as if it would have been created by the job itself. If a parsers was defined in the inputs, the contents are parsed and the returned output nodes are attached. The `CalcJobImporter` can be loaded through its entry point name using the `CalcJobImporterFactory`, just like the entry points of all other entry point groups have their associated factory. As a shortcut, the `CalcJob` class, provides the `get_importer` class method which will attempt to load a `CalcJobImporter` class with the exact same entry point. Alternatively, the caller can specify the desired entry point name should it not correspond to that of the `CalcJob` class. To test the functionality, a `CalcJobImporter` is implemented for the `ArithmeticAddCalculation` class.
- Loading branch information
Showing
17 changed files
with
247 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Importer for the :class:`aiida.calculations.arithmetic.add.ArithmeticAddCalculation` plugin.""" | ||
from pathlib import Path | ||
from re import match | ||
from typing import Dict, Union | ||
from tempfile import NamedTemporaryFile | ||
|
||
from aiida.engine import CalcJobImporter | ||
from aiida.orm import Node, Int, RemoteData | ||
|
||
|
||
class ArithmeticAddCalculationImporter(CalcJobImporter): | ||
"""Importer for the :class:`aiida.calculations.arithmetic.add.ArithmeticAddCalculation` plugin.""" | ||
|
||
@staticmethod | ||
def parse_remote_data(remote_data: RemoteData, **kwargs) -> Dict[str, Union[Node, Dict]]: | ||
"""Parse the input nodes from the files in the provided ``RemoteData``. | ||
:param remote_data: the remote data node containing the raw input files. | ||
:param kwargs: additional keyword arguments to control the parsing process. | ||
:returns: a dictionary with the parsed inputs nodes that match the input spec of the associated ``CalcJob``. | ||
""" | ||
with NamedTemporaryFile('w+') as handle: | ||
with remote_data.get_authinfo().get_transport() as transport: | ||
filepath = Path(remote_data.get_remote_path()) / 'aiida.in' | ||
transport.getfile(filepath, handle.name) | ||
|
||
handle.seek(0) | ||
data = handle.read() | ||
|
||
matches = match(r'echo \$\(\(([0-9]+) \+ ([0-9]+)\)\).*', data.strip()) | ||
|
||
if matches is None: | ||
raise ValueError(f'failed to parse the integers `x` and `y` from the input content: {data}') | ||
|
||
return { | ||
'x': Int(matches.group(1)), | ||
'y': Int(matches.group(2)), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Abstract utility class that helps to import calculation jobs completed outside of AiiDA.""" | ||
from abc import abstractmethod | ||
from typing import Dict, Union | ||
|
||
from aiida.orm import Node, RemoteData | ||
|
||
__all__ = ('CalcJobImporter',) | ||
|
||
|
||
class CalcJobImporter: | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def parse_remote_data(remote_data: RemoteData, **kwargs) -> Dict[str, Union[Node, Dict]]: | ||
"""Parse the input nodes from the files in the provided ``RemoteData``. | ||
:param remote_data: the remote data node containing the raw input files. | ||
:param kwargs: additional keyword arguments to control the parsing process. | ||
:returns: a dictionary with the parsed inputs nodes that match the input spec of the associated ``CalcJob``. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Tests for the :mod:`aiida.calculations.importers.arithmetic.add` module.""" | ||
from aiida.calculations.importers.arithmetic.add import ArithmeticAddCalculationImporter | ||
from aiida.orm import Int, RemoteData | ||
|
||
|
||
def test_parse_remote_data(tmp_path, aiida_localhost): | ||
"""Test the ``ArithmeticAddCalculationImporter.parse_remote_data`` method.""" | ||
with (tmp_path / 'aiida.in').open('w+') as handle: | ||
handle.write('echo $((4 + 12))') | ||
handle.flush() | ||
|
||
remote_data = RemoteData(tmp_path, computer=aiida_localhost) | ||
inputs = ArithmeticAddCalculationImporter.parse_remote_data(remote_data) | ||
|
||
assert list(inputs.keys()) == ['x', 'y'] | ||
assert isinstance(inputs['x'], Int) | ||
assert isinstance(inputs['y'], Int) | ||
assert inputs['x'].value == 4 | ||
assert inputs['y'].value == 12 |
Oops, something went wrong.