From e5116bab526a3058a3b7c621fc28fa0b28db8e74 Mon Sep 17 00:00:00 2001 From: Jakob Duerrwaechter Date: Wed, 27 Nov 2024 13:04:18 +0100 Subject: [PATCH 1/2] add option to to_df function to make one of the dims to columns; adapt examples --- examples/example3.py | 6 ++---- examples/example5.py | 10 +++------- sodym/named_dim_arrays.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/example3.py b/examples/example3.py index 186d277..7d4f5cd 100644 --- a/examples/example3.py +++ b/examples/example3.py @@ -163,8 +163,7 @@ def read_parameter_values(self, parameter: str, dims: DimensionSet) -> Parameter # First, we plot the steel stock in the different countries over time. # %% -stock_df = dynamic_stock.stock.to_df(index=False) -stock_df = stock_df.pivot(index="Time", columns="Region")["value"] +stock_df = dynamic_stock.stock.to_df(dim_to_columns="Region") fig = px.line(stock_df, title="In-use stocks of steel") fig.show() @@ -176,8 +175,7 @@ def read_parameter_values(self, parameter: str, dims: DimensionSet) -> Parameter inflow = dynamic_stock.inflow outflow = dynamic_stock.outflow with np.errstate(divide="ignore"): - ratio_df = (outflow / inflow).to_df(index=False) -ratio_df = ratio_df.pivot(index="Time", columns="Region")["value"] + ratio_df = (outflow / inflow).to_df(dim_to_columns="Region") fig = px.line(ratio_df, title="Ratio outflow:inflow") fig.show() diff --git a/examples/example5.py b/examples/example5.py index 6168e13..de134bf 100644 --- a/examples/example5.py +++ b/examples/example5.py @@ -135,8 +135,8 @@ def compute_stock(self): self.stocks["in use"].inflow[...] = self.flows["sysenv => market"] survival_model = NormalSurvival( dims=self.stocks["in use"].inflow.dims, - lifetime_mean=self.parameters["vehicle lifetime"].values, - lifetime_std=self.parameters["vehicle lifetime"].values * 0.3, + lifetime_mean=self.parameters["vehicle lifetime"], + lifetime_std=self.parameters["vehicle lifetime"] * 0.3, ) if not isinstance(self.stocks["in use"], InflowDrivenDSM): self.stocks["in use"] = self.stocks["in use"].to_stock_type( @@ -349,11 +349,7 @@ def vehicle_new_registration(self, data, dims): # %% np.nan_to_num(vehicle_mfa_2.flows["scrap => sysenv"].values, copy=False) scrap_outflow = vehicle_mfa_2.flows["scrap => sysenv"].sum_nda_over(sum_over_dims=("r", "m")) -outflow_df = pd.DataFrame( - data=scrap_outflow.values, - columns=scrap_outflow.dims["w"].items, - index=scrap_outflow.dims["t"].items, -) +outflow_df = scrap_outflow.to_df(dim_to_columns="waste") outflow_df = outflow_df[outflow_df.index > 2017] fig = px.line(outflow_df, title="Scrap outflow") fig.show() diff --git a/sodym/named_dim_arrays.py b/sodym/named_dim_arrays.py index 5961c6a..7d3e65c 100644 --- a/sodym/named_dim_arrays.py +++ b/sodym/named_dim_arrays.py @@ -237,12 +237,18 @@ def __setitem__(self, keys, item): self.values[slice_obj.ids] = item.sum_values_to(slice_obj.dim_letters) return - def to_df(self, index: bool = True): + def to_df(self, index: bool = True, dim_to_columns: str = None) -> pd.DataFrame: multiindex = pd.MultiIndex.from_product([d.items for d in self.dims], names=self.dims.names) df = pd.DataFrame({"value": self.values.flatten()}) df = df.set_index(multiindex) + if dim_to_columns is not None: + if dim_to_columns not in self.dims.names: + raise ValueError(f"Dimension name {dim_to_columns} not found in nda.dims.names") + df.reset_index(inplace=True) + index_names = [n for n in self.dims.names if n!=dim_to_columns] + df = df.pivot(index=index_names, columns=dim_to_columns, values="value") if not index: - df = df.reset_index() + df.reset_index(inplace=True) return df def split(self, dim_letter: str) -> dict: From c4182f2e4525e8a7c7a478c24ee6b96722c7c1a2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Nov 2024 12:08:07 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/example3.ipynb | 6 ++---- examples/example5.ipynb | 10 +++------- sodym/named_dim_arrays.py | 2 +- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/examples/example3.ipynb b/examples/example3.ipynb index b3804c0..46a1829 100644 --- a/examples/example3.ipynb +++ b/examples/example3.ipynb @@ -3299,8 +3299,7 @@ } ], "source": [ - "stock_df = dynamic_stock.stock.to_df(index=False)\n", - "stock_df = stock_df.pivot(index=\"Time\", columns=\"Region\")[\"value\"]\n", + "stock_df = dynamic_stock.stock.to_df(dim_to_columns=\"Region\")\n", "\n", "fig = px.line(stock_df, title=\"In-use stocks of steel\")\n", "fig.show()" @@ -6385,8 +6384,7 @@ "inflow = dynamic_stock.inflow\n", "outflow = dynamic_stock.outflow\n", "with np.errstate(divide=\"ignore\"):\n", - " ratio_df = (outflow / inflow).to_df(index=False)\n", - "ratio_df = ratio_df.pivot(index=\"Time\", columns=\"Region\")[\"value\"]\n", + " ratio_df = (outflow / inflow).to_df(dim_to_columns=\"Region\")\n", "\n", "fig = px.line(ratio_df, title=\"Ratio outflow:inflow\")\n", "fig.show()" diff --git a/examples/example5.ipynb b/examples/example5.ipynb index 10e75f3..0f3acb0 100644 --- a/examples/example5.ipynb +++ b/examples/example5.ipynb @@ -174,8 +174,8 @@ " self.stocks[\"in use\"].inflow[...] = self.flows[\"sysenv => market\"]\n", " survival_model = NormalSurvival(\n", " dims=self.stocks[\"in use\"].inflow.dims,\n", - " lifetime_mean=self.parameters[\"vehicle lifetime\"].values,\n", - " lifetime_std=self.parameters[\"vehicle lifetime\"].values * 0.3,\n", + " lifetime_mean=self.parameters[\"vehicle lifetime\"],\n", + " lifetime_std=self.parameters[\"vehicle lifetime\"] * 0.3,\n", " )\n", " if not isinstance(self.stocks[\"in use\"], InflowDrivenDSM):\n", " self.stocks[\"in use\"] = self.stocks[\"in use\"].to_stock_type(\n", @@ -4556,11 +4556,7 @@ "source": [ "np.nan_to_num(vehicle_mfa_2.flows[\"scrap => sysenv\"].values, copy=False)\n", "scrap_outflow = vehicle_mfa_2.flows[\"scrap => sysenv\"].sum_nda_over(sum_over_dims=(\"r\", \"m\"))\n", - "outflow_df = pd.DataFrame(\n", - " data=scrap_outflow.values,\n", - " columns=scrap_outflow.dims[\"w\"].items,\n", - " index=scrap_outflow.dims[\"t\"].items,\n", - ")\n", + "outflow_df = scrap_outflow.to_df(dim_to_columns=\"waste\")\n", "outflow_df = outflow_df[outflow_df.index > 2017]\n", "fig = px.line(outflow_df, title=\"Scrap outflow\")\n", "fig.show()\n", diff --git a/sodym/named_dim_arrays.py b/sodym/named_dim_arrays.py index 7d3e65c..47c8e85 100644 --- a/sodym/named_dim_arrays.py +++ b/sodym/named_dim_arrays.py @@ -245,7 +245,7 @@ def to_df(self, index: bool = True, dim_to_columns: str = None) -> pd.DataFrame: if dim_to_columns not in self.dims.names: raise ValueError(f"Dimension name {dim_to_columns} not found in nda.dims.names") df.reset_index(inplace=True) - index_names = [n for n in self.dims.names if n!=dim_to_columns] + index_names = [n for n in self.dims.names if n != dim_to_columns] df = df.pivot(index=index_names, columns=dim_to_columns, values="value") if not index: df.reset_index(inplace=True)