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

Standardize model identifier names; Support datastacks for plugins #1759

Open
wants to merge 24 commits into
base: feature/plugins
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a511ccc
rename and consolidate model identifiers
emlys Jan 31, 2025
81602a6
update placeholder name in workbench message catalog
emlys Feb 1, 2025
112b132
clean up
emlys Feb 1, 2025
18484c5
point model exporting functions to plugin server
emlys Feb 5, 2025
c083e44
get model title from investList when opening a datastack
emlys Feb 6, 2025
033b7a6
minor fix to a docstring
emlys Feb 6, 2025
f54f674
drop support for old logfiles; add support for logs/stacks with model_id
emlys Feb 6, 2025
0a839a9
invest logging: log model id instead of pyname
emlys Feb 6, 2025
5d4f628
look up modelTitle in investList on workbench side when loading runs
emlys Feb 6, 2025
5bda7a5
use model id rather than name in logfile name
emlys Feb 6, 2025
1142d32
don't mark up log messages from outside of natcap.invest
emlys Feb 6, 2025
b1d0043
fix hra test; move function from datastack to utils to avoid circular…
emlys Feb 7, 2025
8254e89
update a couple of workbench tests for new variable naming
emlys Feb 7, 2025
f6e8be7
fix a variable name in cli.py
emlys Feb 7, 2025
1d56c88
update variable naming in autovalidate script
emlys Feb 7, 2025
7afff1e
update variable naming in flask app tests
emlys Feb 7, 2025
77bb8cb
Merge branch 'feature/plugins' into bugfix/1700
emlys Feb 7, 2025
735563c
fix variable names in autovalidate script after merging
emlys Feb 7, 2025
48d35d9
filter out compiled modules earlier in autovalidate script
emlys Feb 7, 2025
582e657
trying again
emlys Feb 7, 2025
e0f9af7
debugging
emlys Feb 7, 2025
13efde1
use os.sep for cross system paths
emlys Feb 7, 2025
dd00685
apply warning markup only to warnings from natcap.invest
emlys Feb 10, 2025
5935723
datastack tests: update comments and remove unneeded mock
emlys Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions scripts/invest-autovalidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import unittest

from natcap.invest import datastack
from natcap.invest import models

logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger('invest-autovalidate.py')
Expand All @@ -33,7 +34,7 @@ def test_exception_on_invalid_data(self):
self.workspace, 'dummy.invs.json')
with open(datastack_path, 'w') as file:
file.write('"args": {"something": "else"},')
file.write('"model_name": natcap.invest.carbon')
file.write('"model_id": "carbon"')
with self.assertRaises(ValueError):
main(self.workspace)

Expand All @@ -43,7 +44,7 @@ def test_exception_on_workspace_dir(self):
self.workspace, 'dummy.invs.json')
with open(datastack_path, 'w') as file:
file.write('"args": {"workspace_dir": "/home/foo"},')
file.write('"model_name": natcap.invest.carbon')
file.write('"model_id": "carbon"')
with self.assertRaises(ValueError):
main(self.workspace)

Expand All @@ -67,25 +68,25 @@ def main(sampledatadir):
raise ValueError(f'no json files found in {sampledatadir}')

for datastack_path in datastacks:
paramset = datastack.extract_parameter_set(datastack_path)
if paramset.model_name.split(".")[-1] in {
'delineateit', 'ndr', 'scenic_quality', 'sdr',
'seasonal_water_yield', 'recreation'}:
if datastack_path.split(os.sep)[-2] in {'DelineateIt', 'NDR', 'ScenicQuality',
'SDR', 'Seasonal_Water_Yield', 'recreation'}:
continue # avoid compiled modles for devoloped of plugin feature branch
paramset = datastack.extract_parameter_set(datastack_path)
if 'workspace_dir' in paramset.args and \
paramset.args['workspace_dir'] != '':
msg = (
'%s : workspace_dir should not be defined '
'for sample datastacks' % datastack_path)
f'{datastack_path} : workspace_dir should not be defined '
'for sample datastacks' )
validation_messages += os.linesep + msg
LOGGER.error(msg)
else:
paramset.args['workspace_dir'] = tempfile.mkdtemp()
model_module = importlib.import_module(name=paramset.model_name)
model_module = importlib.import_module(
name=models.model_id_to_pyname[paramset.model_id])

model_warnings = [] # define here in case of uncaught exception.
try:
LOGGER.info('validating %s ', os.path.abspath(datastack_path))
LOGGER.info(f'validating {os.path.abspath(datastack_path)}')
model_warnings = getattr(
model_module, 'validate')(paramset.args)
except AttributeError as err:
Expand Down
2 changes: 1 addition & 1 deletion src/natcap/invest/annual_water_yield.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

MODEL_SPEC = {
"model_id": "annual_water_yield",
"model_name": gettext("Annual Water Yield"),
"model_title": gettext("Annual Water Yield"),
"pyname": "natcap.invest.annual_water_yield",
"userguide": "annual_water_yield.html",
"aliases": ("hwy", "awy"),
Expand Down
2 changes: 1 addition & 1 deletion src/natcap/invest/carbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

MODEL_SPEC = {
"model_id": "carbon",
"model_name": gettext("Carbon Storage and Sequestration"),
"model_title": gettext("Carbon Storage and Sequestration"),
"pyname": "natcap.invest.carbon",
"userguide": "carbonstorage.html",
"aliases": (),
Expand Down
24 changes: 13 additions & 11 deletions src/natcap/invest/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def build_model_list_table(locale_code):
# Adding 3 to max alias name length for the parentheses plus some padding.
max_alias_name_length = max(len(', '.join(
spec['aliases'])) for spec in models.model_id_to_spec.values()) + 3
template_string = ' {model_id} {aliases} {model_name}'
template_string = ' {model_id} {aliases} {model_title}'
strings = [translation.gettext('Available models:')]
for model_id, model_spec in models.model_id_to_spec.items():

Expand All @@ -65,7 +65,7 @@ def build_model_list_table(locale_code):
strings.append(template_string.format(
model_id=model_id.ljust(max_model_id_length),
aliases=alias_string.ljust(max_alias_name_length),
model_name=translation.gettext(model_spec['model_name'])))
model_title=translation.gettext(model_spec['model_title'])))
return '\n'.join(strings) + '\n'


Expand Down Expand Up @@ -94,8 +94,8 @@ def build_model_list_json(locale_code):

json_object = {}
for model_id, model_spec in models.model_id_to_spec.items():
json_object[translation.gettext(model_spec['model_name'])] = {
'model_name': model_id,
json_object[model_id] = {
'model_title': translation.gettext(model_spec['model_title']),
'aliases': model_spec['aliases']
}

Expand Down Expand Up @@ -159,7 +159,7 @@ def export_to_python(target_filepath, model_id, args_dict=None):
py_file.write(script_template.format(
invest_version=natcap.invest.__version__,
today=datetime.datetime.now().strftime('%c'),
model_title=models.model_id_to_spec[model_id]['model_name'],
model_title=models.model_id_to_spec[model_id]['model_title'],
pyname=models.model_id_to_pyname[model_id],
model_args=args))

Expand Down Expand Up @@ -395,7 +395,7 @@ def main(user_args=None):
# reload validation module first so it's also in the correct language
importlib.reload(importlib.import_module('natcap.invest.validation'))
model_module = importlib.reload(importlib.import_module(
name=parsed_datastack.model_name))
name=models.model_id_to_pyname[parsed_datastack.model_id]))

try:
validation_result = model_module.validate(parsed_datastack.args)
Expand Down Expand Up @@ -470,12 +470,14 @@ def main(user_args=None):
model_module.__name__, model_module)

with utils.prepare_workspace(parsed_datastack.args['workspace_dir'],
name=parsed_datastack.model_name,
model_id=parsed_datastack.model_id,
logging_level=log_level):
LOGGER.log(datastack.ARGS_LOG_LEVEL,
'Starting model with parameters: \n%s',
datastack.format_args_dict(parsed_datastack.args,
parsed_datastack.model_name))
LOGGER.log(
datastack.ARGS_LOG_LEVEL,
'Starting model with parameters: \n%s',
datastack.format_args_dict(
parsed_datastack.args,
parsed_datastack.model_id))

# We're deliberately not validating here because the user
# can just call ``invest validate <datastack>`` to validate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@

MODEL_SPEC = {
"model_id": "coastal_blue_carbon",
"model_name": gettext("Coastal Blue Carbon"),
"model_title": gettext("Coastal Blue Carbon"),
"pyname": "natcap.invest.coastal_blue_carbon.coastal_blue_carbon",
"userguide": "coastal_blue_carbon.html",
"aliases": ("cbc",),
Expand Down
2 changes: 1 addition & 1 deletion src/natcap/invest/coastal_blue_carbon/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

MODEL_SPEC = {
"model_id": "coastal_blue_carbon_preprocessor",
"model_name": gettext("Coastal Blue Carbon Preprocessor"),
"model_title": gettext("Coastal Blue Carbon Preprocessor"),
"pyname": "natcap.invest.coastal_blue_carbon.preprocessor",
"userguide": "coastal_blue_carbon.html",
"aliases": ("cbc_pre",),
Expand Down
2 changes: 1 addition & 1 deletion src/natcap/invest/coastal_vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def get_vector_colnames(vector_path):

MODEL_SPEC = {
"model_id": "coastal_vulnerability",
"model_name": gettext("Coastal Vulnerability"),
"model_title": gettext("Coastal Vulnerability"),
"pyname": "natcap.invest.coastal_vulnerability",
"userguide": "coastal_vulnerability.html",
"aliases": ("cv",),
Expand Down
2 changes: 1 addition & 1 deletion src/natcap/invest/crop_production_percentile.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

MODEL_SPEC = {
"model_id": "crop_production_percentile",
"model_name": gettext("Crop Production: Percentile"),
"model_title": gettext("Crop Production: Percentile"),
"pyname": "natcap.invest.crop_production_percentile",
"userguide": "crop_production.html",
"aliases": ("cpp",),
Expand Down
2 changes: 1 addition & 1 deletion src/natcap/invest/crop_production_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

MODEL_SPEC = {
"model_id": "crop_production_regression",
"model_name": gettext("Crop Production: Regression"),
"model_title": gettext("Crop Production: Regression"),
"pyname": "natcap.invest.crop_production_regression",
"userguide": "crop_production.html",
"aliases": ("cpr",),
Expand Down
Loading
Loading