Source code for orgmatt.agrifood.biowaste

# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2022-2023 Tanguy Fardet
# SPDX-License-Identifier: GPL-3.0-or-later
# orgmatt/agrifood/biowaste.py

"""
Functions to compute bio and food waste generated in households.
"""

from typing import Optional, Union

from pint import Quantity

from .._utils import _filter_dataframe, return_values, auto_format
from ..data import food
from ..typing import NumericArrayLike, NumericOrArray
from ..units import check_dim, ureg


[docs] @auto_format @check_dim(arglist=('duration', 1, '[time]'), result='[mass]') def biowaste_from_population( pop: NumericOrArray, duration: Quantity, ci: int = 0, q: Optional[NumericArrayLike] = None, waste_type: str = "BiodegradableWaste", **kwargs ) -> Quantity: ''' Amount of biowaste generated in households for a given population `pop` over a certain `duration` using the ``food.csv`` database. Can be: - biodegradable waste (for any putrescible matter) - green waste (from gardens, etc) - kitchen waste (for organic components thrown during meal preparation) - food waste (for edible leftovers) - food residues (combining kitchen and food waste) Parameters ---------- pop : float or list-like object Number of inhabitants. duration : float [time] Time interval over which the nitrogen is excreted. ci : int (optional, default: 0) Confidence interval (CI). If non-zero, the function will also return the low and high expected values corresponding to that CI. q : array-like of floats (optional: default: None) Percentiles to compute (supersedes `ci`). waste_type : str, optional (default: "BiodegradableWaste") Type of waste that should be considered among "BiodegradableWaste", "KitchenWaste", and "FoodWaste". **kwargs : arguments to use a subset of the database Additional arguments can be use to restrict the results to a subset of the full database. E.g. one can add `region="Europe"` to use only values obtained from studies in European countries. Returns ------- biowaste : float or array of dimension D [length]**3 Mass of generated biodegradable waste if `q` is None or `ci` is 0. If `q` is provided, returns the values associated to each percentile in `q` for each entry in `pop`; otherwise one (low, mean, high) result per entry in `pop`, giving the masses expected for the requested confidence interval. The dimension D is either 3, len(q), (len(pop), 3), or (len(pop), len(q)) depending on `ci` and `q`. ''' valid = ("BiodegradableWaste", "KitchenWaste", "FoodWaste", "FoodResidues") assert waste_type in valid, \ f"Invalid `waste_type` '{waste_type}'; accepted values are {valid}." # remove potential duplicate entry for property/type argument kwargs = kwargs.copy() if "property" in kwargs: del kwargs["property"] if "type" in kwargs: del kwargs["type"] bw = food[food.property == "generation"] bw = bw[bw.type == waste_type] bw = _filter_dataframe(bw, kwargs) res = return_values(bw, "value", pop, ci, q) return ureg("kg/week") * duration * res