Skip to content

Commit

Permalink
Merge pull request #1308 from pyiron/flxjob_no_function_required
Browse files Browse the repository at this point in the history
No longer require functions for create_job_factory()
  • Loading branch information
jan-janssen authored Feb 3, 2024
2 parents 80fd565 + 061e3e4 commit 59597fd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
43 changes: 25 additions & 18 deletions pyiron_base/jobs/flex/executablecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,45 @@ def __init__(self, project, job_name):

def set_job_type(
self,
write_input_funct,
executable_str,
collect_output_funct,
write_input_funct=None,
collect_output_funct=None,
default_input_dict=None,
):
"""
Set the pre-defined write_input() and collect_output() function plus a dictionary of default inputs and an
executable string.
Args:
write_input_funct (callable): The write input function write_input(input_dict, working_directory)
executable_str (str): Call to an external executable
write_input_funct (callable): The write input function write_input(input_dict, working_directory)
collect_output_funct (callable): The collect output function collect_output(working_directory)
default_input_dict (dict/None): Default input for the newly created job class
Returns:
callable: Function which requires a project and a job_name as input and returns a job object
"""
self._write_input_funct = write_input_funct
self.executable = executable_str
self._collect_output_funct = collect_output_funct
if write_input_funct is not None:
self._write_input_funct = write_input_funct
if collect_output_funct is not None:
self._collect_output_funct = collect_output_funct
if default_input_dict is not None:
self.input.update(default_input_dict)

def write_input(self):
self._write_input_funct(
input_dict=self.input.to_builtin(), working_directory=self.working_directory
)
if self._write_input_funct is not None:
self._write_input_funct(
input_dict=self.input.to_builtin(),
working_directory=self.working_directory,
)

def collect_output(self):
self.output.update(
self._collect_output_funct(working_directory=self.working_directory)
)
self.to_hdf()
if self._collect_output_funct is not None:
self.output.update(
self._collect_output_funct(working_directory=self.working_directory)
)
self.to_hdf()

def to_hdf(self, hdf=None, group_name=None):
super().to_hdf(hdf=hdf, group_name=group_name)
Expand All @@ -94,9 +99,11 @@ def to_hdf(self, hdf=None, group_name=None):

def from_hdf(self, hdf=None, group_name=None):
super().from_hdf(hdf=hdf, group_name=group_name)
self._write_input_funct = cloudpickle.loads(
self.project_hdf5["write_input_function"]
)
self._collect_output_funct = cloudpickle.loads(
self.project_hdf5["collect_output_function"]
)
if "write_input_function" in self.project_hdf5.list_nodes():
self._write_input_funct = cloudpickle.loads(
self.project_hdf5["write_input_function"]
)
if "write_input_function" in self.project_hdf5.list_nodes():
self._collect_output_funct = cloudpickle.loads(
self.project_hdf5["collect_output_function"]
)
6 changes: 3 additions & 3 deletions pyiron_base/jobs/flex/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@


def create_job_factory(
write_input_funct,
executable_str,
collect_output_funct,
write_input_funct=None,
collect_output_funct=None,
default_input_dict=None,
):
"""
Create a new job class based on pre-defined write_input() and collect_output() function plus a dictionary of
default inputs and an executable string.
Args:
write_input_funct (callable): The write input function write_input(input_dict, working_directory)
executable_str (str): Call to an external executable
write_input_funct (callable): The write input function write_input(input_dict, working_directory)
collect_output_funct (callable): The collect output function collect_output(working_directory)
default_input_dict (dict/None): Default input for the newly created job class
Expand Down
8 changes: 4 additions & 4 deletions pyiron_base/project/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,21 +335,21 @@ def create_group(self, group):
@staticmethod
def create_job_class(
class_name,
write_input_funct,
collect_output_funct,
default_input_dict,
executable_str,
write_input_funct=None,
collect_output_funct=None,
default_input_dict=None,
):
"""
Create a new job class based on pre-defined write_input() and collect_output() function plus a dictionary of
default inputs and an executable string.
Args:
class_name (str): A name for the newly created job class, so it is accessible via pr.create.job.<class_name>
executable_str (str): Call to an external executable
write_input_funct (callable): The write input function write_input(input_dict, working_directory)
collect_output_funct (callable): The collect output function collect_output(working_directory)
default_input_dict (dict): Default input for the newly created job class
executable_str (str): Call to an external executable
Example:
Expand Down
15 changes: 15 additions & 0 deletions tests/flex/test_executablecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ def test_create_job_factory_typeerror(self):
project="project",
job_name="job_test"
)

def test_create_job_factory_no_functions(self):
create_catjob = create_job_factory(
executable_str="python --version",
)
job = create_catjob(
project=ProjectHDFio(project=self.project, file_name="any.h5", h5_path=None, mode=None),
job_name="job_no"
)
job.run()
self.assertTrue(job.status.finished)
self.assertEqual(os.listdir(job.working_directory), ['error.out'])
with open(os.path.join(job.working_directory, 'error.out'), "r") as f:
content = f.readlines()
self.assertEqual(content[0].split()[0], "Python")

0 comments on commit 59597fd

Please sign in to comment.