Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial preprocessor implementation #52

Merged
merged 15 commits into from
Jun 9, 2020
92 changes: 60 additions & 32 deletions pyDeltaRCM/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def expand_yaml_matrix(self):
pass

def extract_timesteps(self):
if hasattr(self, 'cli_timesteps'):
if hasattr(self, 'arg_timesteps'):
# overrides everything else
self.timesteps = self.cli_timesteps
self.timesteps = self.arg_timesteps

if not hasattr(self, 'timesteps'):
if 'timesteps' in self.user_dict.keys():
Expand All @@ -66,19 +66,43 @@ def extract_timesteps(self):
'YAML configuration file or via the --timesteps '
'CLI flag, in order to use the high-level API.')

def construct_job_list(self):
self.job_list = []
if self._has_matrix:
self.expand_yaml_matrix()
else:
self.job_list.append(self._Job(self.input_file,
yaml_timesteps=self.yaml_timesteps,
arg_timesteps=self.arg_timesteps))

def run_jobs(self):

# check no mulitjobs, no implemented
if len(self.job_list) > 1:
raise NotImplementedError()
# 1. set up parallel pool if multiple jobs
# 2. run jobs in list
######

# run the job(s)
for job in self.job_list:
job.run_model()
job.finalize_model()

class _Job(object):

def __init__(self, input_file, yaml_timesteps, cli_timesteps):
def __init__(self, input_file, yaml_timesteps, arg_timesteps):

self.deltamodel = DeltaModel(input_file=input_file)

if yaml_timesteps:
_timesteps = yaml_timesteps
elif cli_timesteps:
_timesteps = cli_timesteps
elif arg_timesteps:
_timesteps = arg_timesteps
else:
raise ValueError('You must specify timesteps in either the '
'YAML configuration file or via the --timesteps '
'CLI flag, in order to use the high-level API.')
'YAML configuration file or via the timesteps '
'argument, in order to use the high-level API.')
self.timesteps = _timesteps

def run_model(self):
Expand Down Expand Up @@ -112,22 +136,16 @@ def __init__(self):
self._has_matrix = False

if self.args['timesteps']:
self.cli_timesteps = int(self.args['timesteps'])
self.arg_timesteps = int(self.args['timesteps'])
else:
self.cli_timesteps = None
self.arg_timesteps = None

if 'timesteps' in self.user_dict.keys():
self.yaml_timesteps = self.user_dict['timesteps']
else:
self.yaml_timesteps = None

self.job_list = []
if self._has_matrix:
self.expand_yaml_matrix()
else:
self.job_list.append(self._Job(self.input_file,
yaml_timesteps=self.yaml_timesteps,
cli_timesteps=self.cli_timesteps))
self.construct_job_list()

self.extract_timesteps()

Expand Down Expand Up @@ -159,28 +177,38 @@ class Preprocessor(BasePreprocessor):
as well as timestepping from script.
"""

def __init__(self):
raise NotImplementedError
super().__init__(self)
pass
def __init__(self, input_file=None, timesteps=None):

super().__init__()

if input_file:
self.input_file = input_file
self.extract_yaml_config()
else:
self.input_file = None
self.user_dict = {}
self._has_matrix = False

if timesteps:
self.arg_timesteps = int(timesteps)
else:
self.arg_timesteps = None

if 'timesteps' in self.user_dict.keys():
self.yaml_timesteps = self.user_dict['timesteps']
else:
self.yaml_timesteps = None

self.construct_job_list()

self.extract_timesteps()


def preprocessor_wrapper():
"""Wrapper for CLI interface.
"""
pp = PreprocessorCLI()

# check no mulitjobs, no implemented
if len(pp.job_list) > 1:
raise NotImplementedError
# 1. set up parallel pool if multiple jobs
# 2. run jobs in list
######

# run the job(s)
for job in pp.job_list:
job.run_model()
job.finalize_model()
pp.run_jobs()


if __name__ == '__main__':
Expand Down
Binary file removed test/pyDeltaRCM_output.nc
Binary file not shown.
12 changes: 6 additions & 6 deletions tests/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ def test_random_seed_settings_noaction_default(tmp_path):

# test the entry points

from pyDeltaRCM import preprocessor
import pyDeltaRCM as _pyimportedalias

def test_entry_point_installed_call(tmp_path):
"""
test calling the command line feature with a config file.
Expand Down Expand Up @@ -182,9 +179,6 @@ def test_entry_point_python_main_call(tmp_path):


def test_entry_point_python_main_call_dryrun(tmp_path):
"""
test calling the python hook command line feature with a config file.
"""
file_name = 'user_parameters.yaml'
p, f = utilities.create_temporary_file(tmp_path, file_name)
utilities.write_parameter_to_file(f, 'Length', 10.0)
Expand Down Expand Up @@ -256,6 +250,12 @@ def test_version_call():
from pyDeltaRCM import preprocessor



@pytest.mark.xfail(raises=ValueError, strict=True)
ericbarefoot marked this conversation as resolved.
Show resolved Hide resolved
def test_python_highlevelapi_call_without_args():
pp = preprocessor.Preprocessor()


@pytest.mark.xfail(raises=ValueError, strict=True)
def test_python_highlevelapi_call_without_timesteps(tmp_path):
file_name = 'user_parameters.yaml'
Expand Down