Skip to content
Merged
Changes from all commits
Commits
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
58 changes: 33 additions & 25 deletions ema_workbench/connectors/vensim.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def check_data(result):
return results


def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: str, error: str) -> None:
def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: str, error: str|dict) -> None:
"""Create a vensim mdl file parameterized according to the experiment.

To be able to debug the Vensim model, a few steps are needed:
Expand Down Expand Up @@ -386,48 +386,56 @@ def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: s
path to the original mdl file
path_to_new_model : str
path for the new mdl file
error : str
error : str or dict
the case error, only containing the parameterization
or an experiment dictionary

"""
# we assume the case specification was copied from the logger
experiment = error.split(",")
variables = {}

# -1 because policy entry needs to be removed
for entry in experiment[0:-1]:
variable, value = entry.split(":")

# Delete the spaces and other rubish on the sides of the variable name
variable = variable.strip()
variable = variable.lstrip("'")
variable = variable.rstrip("'")
value = value.strip()

# vensim model is in bytes, so we go from unicode to bytes
variables[variable.encode("utf8")] = value.encode("utf8")
if isinstance(error, str):
experiment = error.split(",")
variables = {}

# -1 because policy entry needs to be removed
for entry in experiment[0:-1]:
variable, value = entry.split(":")

# Delete the spaces and other rubish on the sides of the variable name
variable = variable.strip()
variable = variable.lstrip("'")
variable = variable.rstrip("'")
value = value.strip()

# vensim model is in bytes, so we go from unicode to bytes
variables[variable] = value
elif isinstance(error, dict):
variables = error
else:
raise ValueError(f"error must be a string or dict, not of type{type(error)}")

# This generates a new (text-formatted) model
with open(path_to_new_model, "wb") as new_model:
with open(path_to_new_model, "w") as new_model:
skip_next_line = False

for line in open(path_to_existing_model, "rb"): # noqa: SIM115
for line in open(path_to_existing_model): # noqa: SIM115
if skip_next_line:
skip_next_line = False
lin_to_write = b"\n"
elif line.find(b"=") != -1:
variable = line.split(b"=")[0]
line_to_write = "\n"
elif line.find("=") != -1:
variable = line.split("=")[0]
variable = variable.strip()

try:
value = variables.pop(variable)
except KeyError:
pass
line_to_write = line
else:
lin_to_write = variable + b" = " + value
line_to_write = variable + " = " + str(value)
skip_next_line = True
else:
line_to_write = line

new_model.write(lin_to_write)
new_model.write(line_to_write)

_logger.info("parameters not set:")
_logger.info(set(variables.keys()))
Loading