Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into support-uo-only
Browse files Browse the repository at this point in the history
  • Loading branch information
nllong committed Sep 3, 2024
2 parents 34be54c + 1611330 commit f282298
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 1,152 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repos:
types_or: [yaml, markdown, css, scss]
# https://docs.astral.sh/ruff/integrations/#pre-commit
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.1
rev: v0.6.3
hooks:
# Run the linter
- id: ruff
Expand Down
157 changes: 76 additions & 81 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions urbanopt_des/analysis_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def __init__(self) -> None:
# the machine name of the variable
self.variables: dict = {}

def add_variable_instance(
self, variable_name: str, variable_value: Any, **kwargs
) -> None:
def add_variable_instance(self, variable_name: str, variable_value: Any, **kwargs) -> None:
"""Store the variable instance and value in a dictionary. There can only
be one variable_name per instance of the analysis
Expand Down Expand Up @@ -71,9 +69,7 @@ def create_unique_variable_instance_name(self, prepend_str: str = "sim") -> str:

return result

def save_analysis_name_to_file(
self, filename: Path, override_name: Union[None, str] = None
) -> None:
def save_analysis_name_to_file(self, filename: Path, override_name: Union[None, str] = None) -> None:
"""Save off the analysis name to a file that can be used for
later reference and post processing. Right now this is a simple file
but ideally the instance of the analysis that is written should be the same
Expand All @@ -99,6 +95,4 @@ def save_variables_to_file(self, filename: Path) -> None:

with open(filename, "w") as f:
for variable_name in self.variables:
f.write(
f"{variable_name},{self.variables[variable_name]['value']},{self.variables[variable_name]['short_name']}\n"
)
f.write(f"{variable_name},{self.variables[variable_name]['value']},{self.variables[variable_name]['short_name']}\n")
37 changes: 9 additions & 28 deletions urbanopt_des/emissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,30 @@ def __init__(
path = path / "with_distribution_losses"
else:
path = path / "without_distribution_losses"
path = (
path
/ "future"
/ "hourly"
/ f"future_hourly_{emissions_type}_co2e_{future_year}.csv"
)
path = path / "future" / "hourly" / f"future_hourly_{emissions_type}_co2e_{future_year}.csv"

if not path.exists():
raise Exception(f"Future emissions data file does not exist: {path}")

# verify that the eGRID subregion is valid
if egrid_subregion not in self.region_names():
raise Exception(
f"Invalid eGRID subregion: {egrid_subregion}, expected one of {self.region_names()}"
)
raise Exception(f"Invalid eGRID subregion: {egrid_subregion}, expected one of {self.region_names()}")

if analysis_year is None:
analysis_year = future_year

self.data = pd.read_csv(path, header=0)

# create two new columns, one for the datetime based on the future_year and one based on the analysis_year.
self.data["datetime"] = datetime.datetime(future_year, 1, 1) + pd.to_timedelta(
self.data["hour"], unit="h"
)
self.data["datetime"] = datetime.datetime(future_year, 1, 1) + pd.to_timedelta(self.data["hour"], unit="h")
# If the year is a leap year, then shift the datetime by one day, effectively eliminating the leap day.
# This isn't working yet, moving on...
# if self.data['datetime'][0].is_leap_year:
# after 2/28/future_year, shift all hours back by 24 hours
# self.data.loc[self.data['datetime'] > datetime.datetime(future_year, 3, 1), 'datetime'] = self.data.loc[self.data['datetime'] > datetime.datetime(future_year, 3, 1), 'datetime'] - pd.to_timedelta(1, unit='d')

self.data["analysis_datetime_end"] = datetime.datetime(
analysis_year, 1, 1
) + pd.to_timedelta(self.data["hour"], unit="h")
self.data["analysis_datetime_start"] = self.data[
"analysis_datetime_end"
] - pd.to_timedelta(1, unit="h")
self.data["analysis_datetime_end"] = datetime.datetime(analysis_year, 1, 1) + pd.to_timedelta(self.data["hour"], unit="h")
self.data["analysis_datetime_start"] = self.data["analysis_datetime_end"] - pd.to_timedelta(1, unit="h")

# move the datetime columns to the front
cols = self.data.columns.tolist()
Expand All @@ -98,26 +85,20 @@ def __init__(
# remove the MBtu column, and then transpose
# if the column exists, then drop it
if "emission_kg_per_mbtu" in self.other_fuel_data.columns:
self.other_fuel_data = self.other_fuel_data.drop(
columns=["emission_kg_per_mbtu"]
)
self.other_fuel_data = self.other_fuel_data.drop(columns=["emission_kg_per_mbtu"])

self.other_fuel_data = self.other_fuel_data.T
# make the first row the column names
self.other_fuel_data.columns = self.other_fuel_data.iloc[0]
# drop the first row
self.other_fuel_data = self.other_fuel_data.drop(self.other_fuel_data.index[0])
self.other_fuel_data.reset_index(inplace=True)
self.other_fuel_data.drop(columns=["index"], inplace=True)
self.other_fuel_data = self.other_fuel_data.reset_index()
self.other_fuel_data = self.other_fuel_data.drop(columns=["index"])

# copy the self.data and remove all the columns except
self.other_fuels = self.data.copy()
# drop all columns except 'analysis_datetime_start', 'analysis_datetime_end', 'hour'
to_drop = [
col
for col in self.other_fuels.columns
if col not in ["analysis_datetime_start", "analysis_datetime_end", "hour"]
]
to_drop = [col for col in self.other_fuels.columns if col not in ["analysis_datetime_start", "analysis_datetime_end", "hour"]]
self.other_fuels = self.other_fuels.drop(columns=to_drop)

# merge in the other_fuels_data with the self.other_fuels and fill down
Expand Down
28 changes: 7 additions & 21 deletions urbanopt_des/measures/set_argument_value/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,10 @@ def arguments(self):
"The model_name in the ModelicaProject passed into the run method.",
units="string",
)
self.measure_args.add_argument(
"type", "Name of the type being modified", units="string"
)
self.measure_args.add_argument(
"identifier", "Name of the type identifier being modified", units="string"
)
self.measure_args.add_argument(
"argument_name", "Name of the argument to set", units="string"
)
self.measure_args.add_argument(
"value", "Value to set the argument to", units="float"
)
self.measure_args.add_argument("type", "Name of the type being modified", units="string")
self.measure_args.add_argument("identifier", "Name of the type identifier being modified", units="string")
self.measure_args.add_argument("argument_name", "Name of the argument to set", units="string")
self.measure_args.add_argument("value", "Value to set the argument to", units="float")
return self.measure_args

def run(self, project: ModelicaProject, user_arguments: list[dict]):
Expand All @@ -61,25 +53,19 @@ def run(self, project: ModelicaProject, user_arguments: list[dict]):

# add actions to manipulate the model
# store the previous value, for fun
previous_value = model.get_component_argument_value(
type_, identifier, argument_name
)
previous_value = model.get_component_argument_value(type_, identifier, argument_name)
self.measure_attributes.register_value(
model_name,
self.unique_name,
f"{argument_name}_previous_value",
previous_value,
)

model.update_component_modifications(
type_, identifier, {argument_name: new_value}
)
model.update_component_modifications(type_, identifier, {argument_name: new_value})

for args in user_arguments.get_args_with_register_values():
# register the value that was set after the fact
self.measure_attributes.register_value(
model_name, self.unique_name, args["name"], args["value"]
)
self.measure_attributes.register_value(model_name, self.unique_name, args["name"], args["value"])

# execute the actions
model.execute()
Expand Down
32 changes: 8 additions & 24 deletions urbanopt_des/measures/set_extended_type_argument_value/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,12 @@ def arguments(self):
"The model_name in the ModelicaProject passed into the run method.",
units="string",
)
self.measure_args.add_argument(
"extended_type", "Name of the extended type being modified", units="string"
)
self.measure_args.add_argument(
"type", "Name of the type being modified", units="string"
)
self.measure_args.add_argument(
"identifier", "Name of the type identifier being modified", units="string"
)
self.measure_args.add_argument(
"object_name", "Name of the data object being modified", units="string"
)
self.measure_args.add_argument(
"argument_name", "Name of the argument to set", units="string"
)
self.measure_args.add_argument(
"value", "Value to set the argument to", units="float"
)
self.measure_args.add_argument("extended_type", "Name of the extended type being modified", units="string")
self.measure_args.add_argument("type", "Name of the type being modified", units="string")
self.measure_args.add_argument("identifier", "Name of the type identifier being modified", units="string")
self.measure_args.add_argument("object_name", "Name of the data object being modified", units="string")
self.measure_args.add_argument("argument_name", "Name of the argument to set", units="string")
self.measure_args.add_argument("value", "Value to set the argument to", units="float")
return self.measure_args

def run(self, project: ModelicaProject, user_arguments: list[dict]):
Expand All @@ -68,15 +56,11 @@ def run(self, project: ModelicaProject, user_arguments: list[dict]):
model = project.get_model(model_name)

# add actions to manipulate the model
model.update_extended_component_modification(
extended_type, type_, identifier, object_name, argument_name, str(new_value)
)
model.update_extended_component_modification(extended_type, type_, identifier, object_name, argument_name, str(new_value))

for args in user_arguments.get_args_with_register_values():
# register the value that was set after the fact
self.measure_attributes.register_value(
model_name, self.unique_name, args["name"], args["value"]
)
self.measure_attributes.register_value(model_name, self.unique_name, args["name"], args["value"])

# execute the actions
model.execute()
Expand Down
20 changes: 5 additions & 15 deletions urbanopt_des/measures/set_parameter_value/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ def arguments(self):
"The model_name in the ModelicaProject passed into the run method.",
units="string",
)
self.measure_args.add_argument(
"type", "Name of the type being modified", units="string"
)
self.measure_args.add_argument(
"identifier", "Name of the type identifier being modified", units="string"
)
self.measure_args.add_argument(
"value", "Value to set the argument to", units="float"
)
self.measure_args.add_argument("type", "Name of the type being modified", units="string")
self.measure_args.add_argument("identifier", "Name of the type identifier being modified", units="string")
self.measure_args.add_argument("value", "Value to set the argument to", units="float")
return self.measure_args

def run(self, project: ModelicaProject, user_arguments: list[dict]):
Expand All @@ -59,15 +53,11 @@ def run(self, project: ModelicaProject, user_arguments: list[dict]):

model.update_parameter(type_, identifier, new_value)

self.measure_attributes.register_value(
model_name, self.unique_name, f"{identifier}_previous_value", previous_value
)
self.measure_attributes.register_value(model_name, self.unique_name, f"{identifier}_previous_value", previous_value)

for args in user_arguments.get_args_with_register_values():
# register the value that was set after the fact
self.measure_attributes.register_value(
model_name, self.unique_name, args["name"], args["value"]
)
self.measure_attributes.register_value(model_name, self.unique_name, args["name"], args["value"])

# execute the actions
model.execute()
Expand Down
20 changes: 5 additions & 15 deletions urbanopt_des/measures/set_parameter_value_multiplier/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ def arguments(self):
"The model_name in the ModelicaProject passed into the run method.",
units="string",
)
self.measure_args.add_argument(
"type", "Name of the type being modified", units="string"
)
self.measure_args.add_argument(
"identifier", "Name of the type identifier being modified", units="string"
)
self.measure_args.add_argument(
"value", "Value to multiply the argument by", units="float"
)
self.measure_args.add_argument("type", "Name of the type being modified", units="string")
self.measure_args.add_argument("identifier", "Name of the type identifier being modified", units="string")
self.measure_args.add_argument("value", "Value to multiply the argument by", units="float")
return self.measure_args

def run(self, project: ModelicaProject, user_arguments: list[dict]):
Expand All @@ -60,15 +54,11 @@ def run(self, project: ModelicaProject, user_arguments: list[dict]):
to_set_value = float(previous_value) * float(new_value)
model.update_parameter(type_, identifier, str(to_set_value))

self.measure_attributes.register_value(
model_name, self.unique_name, f"{identifier}_previous_value", previous_value
)
self.measure_attributes.register_value(model_name, self.unique_name, f"{identifier}_previous_value", previous_value)

for args in user_arguments.get_args_with_register_values():
# register the value that was set after the fact
self.measure_attributes.register_value(
model_name, self.unique_name, args["name"], str(to_set_value)
)
self.measure_attributes.register_value(model_name, self.unique_name, args["name"], str(to_set_value))

# execute the actions
model.execute()
Expand Down
Loading

0 comments on commit f282298

Please sign in to comment.