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

Remove get_formatted_array #495

Merged
merged 16 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ User-facing changes

|changed| |backwards-incompatible| The `loc::tech` and `loc::tech::carrier` sets have been removed. Model components are now indexed separately over `node`, `tech`, and `carrier` (where applicable). Although primarily an internal change, this affects the xarray dataset structure and hence how users access data in `model.inputs` and `model.results`. For example, `model.inputs.energy_cap_max.loc[{"loc_techs": "X:pv"}]` in v0.6 needs to be changed to `model.inputs.energy_cap_max.loc[{"nodes": "X", "techs": "pv"}]` in v0.7. This is functionally equivalent to first calling `model.get_formatted_array("energy_cap_max")` in v0.6, which is no longer necessary in v0.7.

|changed| |backwards-incompatible| `get_formatted_array` has been removed in favour of directly accessing the data, since we no longer concatenate sets. E.g., `model.get_formatted_array("storage")` -> `model.results.storage`.

|changed| |backwards-incompatible| The dimensions of the model data no longer include all possible subsets. E.g. a user can no longer access `loc_techs_supply` to view the location/technology pairs which have defined `supply` as their top-level parent. Instead, the same subset can be supplied by calling `model.inputs.inheritance.str.endswith('supply')` to create a boolean array of technologies with `supply` as their top-level parent.

|changed| |backwards-incompatible| Group constraints have been removed. They will be replaced by `custom constraint` functionality.
Expand Down
8 changes: 4 additions & 4 deletions doc/_static/notebooks/calliope_model_object.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5279,7 +5279,7 @@
"source": [
"# Data can also be reformatted to be easier to read (removes dimension concatenation).\n",
"# Conversion to a pandas DataFrame is a good idea for greater readibility.\n",
"m.get_formatted_array(\"energy_cap\").to_pandas()"
"m.results.energy_cap.to_pandas()"
]
},
{
Expand Down Expand Up @@ -5313,9 +5313,9 @@
"source": [
"# >2 dimensions cannot be easily viewed in a pandas dataframe, unless a MultiIndex is used.\n",
"# To view a 4-dimensional result, we can use `to_series()`\n",
"m.get_formatted_array(\n",
" \"flow_out\"\n",
").to_series().dropna() # drop_na() removes all NaN values"
"\n",
"# drop_na() removes all NaN values\n",
"m.results.flow_out.to_series().dropna()"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/_static/notebooks/milp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,7 @@
],
"source": [
"# We can sum operating units of CHP over all locations and turn the result into a pandas DataFrame\n",
"df_units = model.get_formatted_array(\"operating_units\").sum(\"nodes\").to_series()\n",
"df_units = model.results.operating_units.sum(\"nodes\").to_series()\n",
"\n",
"# The information about the dataframe tells us about the amount of data it holds in the index and in each column\n",
"df_units.info()"
Expand Down
16 changes: 4 additions & 12 deletions doc/_static/notebooks/national_scale.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1273,9 +1273,8 @@
}
],
"source": [
"# To reformat the array, deconcatenating loc_techs / loc_tech_carriers, you can use model.get_formatted_array()\n",
"# You can then apply loc/tech/carrier only operations, like summing information over locations:\n",
"model.get_formatted_array(\"resource\").sum(\"nodes\").to_series().unstack(\"techs\")"
"# You can apply node/tech/carrier only operations, like summing information over locations:\n",
"model.inputs.resource.sum(\"nodes\").to_series().unstack(\"techs\")"
]
},
{
Expand Down Expand Up @@ -2366,12 +2365,7 @@
],
"source": [
"# We can sum power output over all locations and turn the result into a pandas DataFrame\n",
"df_power = (\n",
" model.get_formatted_array(\"carrier_prod\")\n",
" .loc[{\"carriers\": \"power\"}]\n",
" .sum(\"nodes\")\n",
" .to_series()\n",
")\n",
"df_power = model.results.flow_out.loc[{\"carriers\": \"power\"}].sum(\"nodes\").to_series()\n",
"\n",
"# The information about the dataframe tells us about the amount of data it holds in the index and in each column\n",
"df_power.info()"
Expand Down Expand Up @@ -8310,9 +8304,7 @@
"# notice all the NaN values which appear when seperating loc::techs to locs and techs.\n",
"# Any NaN value means we never considered that combination of `loc` and `tech` for the variable\n",
"\n",
"costs = (\n",
" model.get_formatted_array(\"cost\").loc[{\"costs\": \"monetary\"}].to_series().dropna()\n",
")\n",
"costs = model.results.cost.loc[{\"costs\": \"monetary\"}].to_series().dropna()\n",
"costs"
]
},
Expand Down
15 changes: 4 additions & 11 deletions doc/_static/notebooks/urban_scale.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1476,9 +1476,8 @@
}
],
"source": [
"# To reformat the array, deconcatenating loc_techs / loc_tech_carriers, you can use model.get_formatted_array()\n",
"# You can then apply loc/tech/carrier only operations, like summing information over locations:\n",
"model.get_formatted_array(\"resource\").sum(\"locs\").to_pandas()"
"# You can apply node/tech/carrier only operations, like summing information over locations:\n",
"model.inputs.source_max.sum(\"nodes\").to_pandas()"
]
},
{
Expand Down Expand Up @@ -2631,13 +2630,7 @@
],
"source": [
"# We can sum heat output over all locations and turn the result into a pandas DataFrame\n",
"df_heat = (\n",
" model.get_formatted_array(\"carrier_prod\")\n",
" .loc[{\"carriers\": \"heat\"}]\n",
" .sum(\"locs\")\n",
" .to_pandas()\n",
" .T\n",
")\n",
"df_heat = model.results.flow_out.loc[{\"carriers\": \"heat\"}].sum(\"locs\").to_pandas().T\n",
"\n",
"# The information about the dataframe tells us about the amount of data it holds in the index and in each column\n",
"df_heat.info()"
Expand Down Expand Up @@ -7166,7 +7159,7 @@
"# notice all the NaN values which appear when seperating loc::techs to locs and techs.\n",
"# Any NaN value means we never considered that combination of `loc` and `tech` for the variable\n",
"\n",
"costs = model.get_formatted_array(\"cost\").loc[{\"costs\": \"monetary\"}].to_pandas()\n",
"costs = model.results.cost.loc[{\"costs\": \"monetary\"}].to_pandas()\n",
"costs"
]
},
Expand Down
21 changes: 0 additions & 21 deletions src/calliope/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,27 +510,6 @@ def run(self, force_rerun=False, **kwargs):
self.build(force=force_rerun)
self.solve(force=force_rerun)

def get_formatted_array(self, var):
"""
Return an xarray.DataArray with nodes, techs, and carriers as
separate dimensions.

Parameters
----------
var : str
Decision variable for which to return a DataArray.

"""
warnings.warn(
"get_formatted_array() is deprecated and will be removed in a "
"future version. Use `model.results[var]` instead.",
DeprecationWarning,
)
if var not in self._model_data.data_vars:
raise KeyError("Variable {} not in Model data".format(var))

return self._model_data[var]

def to_netcdf(self, path):
"""
Save complete model data (inputs and, if available, results)
Expand Down
4 changes: 1 addition & 3 deletions src/calliope/postprocess/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def subset_sum_squeeze(data, subset={}, sum_dims=None, squeeze=False):
Parameters
----------
data : xarray DataArray
A Calliope model data variable, either input or output, which has been
reformatted to deconcatenate loc_techs (or loc_tech_carriers/loc_carriers)
using calliope.Model().get_formatted_array(original_data)
A Calliope model data variable.
subset : dict, default {}
key:value pairs for indexing data. Uses xarray `loc[]` to index.
sum_dims : str or list of strings, default None
Expand Down
8 changes: 0 additions & 8 deletions tests/test_core_future_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@


class TestDeprecationWarnings:
def test_get_formatted_array_deprecationwarning(self):
model = build_model(scenario="simple_supply,one_day,investment_costs")

with pytest.warns(DeprecationWarning) as warning:
model.get_formatted_array("source_max")

assert check_error_or_warning(warning, "get_formatted_array() is deprecated")

def test_run_deprecationwarning(self):
model = build_model(scenario="simple_supply,two_hours,investment_costs")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_example_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def test_nationalscale_example_results_glpk(self):

def test_considers_supply_generation_only_in_total_levelised_cost(self):
# calculation of expected value:
# costs = model.get_formatted_array("cost").sum(dim="locs")
# gen = model.get_formatted_array("flow_out").sum(dim=["timesteps", "locs"])
# costs = model.inputs.cost.sum(dim="locs")
# gen = model.inputs.flow_out.sum(dim=["timesteps", "locs"])
# lcoe = costs.sum(dim="techs") / gen.sel(techs=["ccgt", "csp"]).sum(dim="techs")
model = calliope.examples.national_scale()
model.build()
Expand Down