From f83c079fdab33271c75bf9fc21b3a1cabb0c1b1b Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 10 Jun 2026 19:11:58 +0200 Subject: [PATCH 1/2] Update vensim.py --- ema_workbench/connectors/vensim.py | 50 +++++++++++++++++------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/ema_workbench/connectors/vensim.py b/ema_workbench/connectors/vensim.py index 95d0175fc..114002009 100644 --- a/ema_workbench/connectors/vensim.py +++ b/ema_workbench/connectors/vensim.py @@ -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: @@ -386,26 +386,32 @@ 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.encode("utf8")] = value.encode("utf8") + 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: @@ -414,7 +420,7 @@ def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: s for line in open(path_to_existing_model, "rb"): # noqa: SIM115 if skip_next_line: skip_next_line = False - lin_to_write = b"\n" + line_to_write = b"\n" elif line.find(b"=") != -1: variable = line.split(b"=")[0] variable = variable.strip() @@ -422,12 +428,14 @@ def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: s try: value = variables.pop(variable) except KeyError: - pass + line_to_write = line else: - lin_to_write = variable + b" = " + value + line_to_write = variable + b" = " + 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())) From c210b4726c55896241b62df4b315846908b9caf8 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 10 Jun 2026 20:03:45 +0200 Subject: [PATCH 2/2] remove bytes stuff --- ema_workbench/connectors/vensim.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ema_workbench/connectors/vensim.py b/ema_workbench/connectors/vensim.py index 114002009..b335bd34c 100644 --- a/ema_workbench/connectors/vensim.py +++ b/ema_workbench/connectors/vensim.py @@ -407,22 +407,22 @@ def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: s value = value.strip() # vensim model is in bytes, so we go from unicode to bytes - variables[variable.encode("utf8")] = value.encode("utf8") + 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 - line_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: @@ -430,7 +430,7 @@ def create_model_for_debugging(path_to_existing_model: str, path_to_new_model: s except KeyError: line_to_write = line else: - line_to_write = variable + b" = " + value + line_to_write = variable + " = " + str(value) skip_next_line = True else: line_to_write = line