Skip to content

Commit

Permalink
Merge branch 'docs_getting_started'
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoDiepers committed Sep 12, 2024
2 parents ff5c1dc + f865336 commit 567a729
Show file tree
Hide file tree
Showing 28 changed files with 8,040 additions and 92 deletions.
33 changes: 33 additions & 0 deletions bw_timex/timex_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,39 @@ def create_labelled_dynamic_biosphere_dataframe(self) -> pd.DataFrame:

return df

def create_labelled_dynamic_inventory_dataframe(self) -> pd.DataFrame:
"""
Returns the dynamic_inventory_df with comprehensible labels for flows and activities instead of ids.
Parameters
----------
None
Returns
-------
pd.DataFrame, dynamic inventory matrix as a pandas.DataFrame with comprehensible labels instead of ids.
"""

if not hasattr(self, "dynamic_inventory_df"):
warnings.warn(
"Dynamic inventory not yet calculated. Call TimexLCA.lci(build_dynamic_biosphere=True) first."
)

df = self.dynamic_inventory_df.copy()
df["flow"] = df["flow"].apply(lambda x: bd.get_node(id=x)["name"])

activity_name_cache = {}

for activity in df["activity"].unique():
if activity not in activity_name_cache:
activity_name_cache[activity] = resolve_temporalized_node_name(
self.activity_time_mapping_dict_reversed[activity][0][1]
)

df["activity"] = df["activity"].map(activity_name_cache)

return df

def plot_dynamic_inventory(self, bio_flows, cumulative=False) -> None:
"""
Simple plot of dynamic inventory of a biosphere flow over time, with optional cumulative plotting.
Expand Down
31 changes: 31 additions & 0 deletions bw_timex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,34 @@ def plot_characterized_inventory_as_waterfall(
ax.set_axisbelow(True)
plt.grid(True)
plt.show()

def get_exchange(**kwargs) -> Exchange:
"""
Get an exchange from the database.
"""
mapping = {
"input_code": ExchangeDataset.input_code,
"input_database": ExchangeDataset.input_database,
"output_code": ExchangeDataset.output_code,
"output_database": ExchangeDataset.output_database,
}
qs = ExchangeDataset.select()
for key, value in kwargs.items():
try:
qs = qs.where(mapping[key] == value)
except KeyError:
continue

candidates = [Exchange(obj) for obj in qs]
if len(candidates) > 1:
raise MultipleResults(
"Found {} results for the given search. Please be more specific or double-check your system model for duplicates.".format(len(candidates))
)
elif not candidates:
raise UnknownObject
return candidates[0]

def add_temporal_distribution_to_exchange(temporal_distribution: TemporalDistribution, **kwargs):
exchange = get_exchange(**kwargs)
exchange["temporal_distribution"] = temporal_distribution
exchange.save()
Loading

0 comments on commit 567a729

Please sign in to comment.