Example #1
0
def write_load_reduction_range(reduction_df, site):  # pragma: no cover
    final_cols = [
        "Total Suspended Solids",
        "Cadmium (Cd)",
        "Copper (Cu)",
        "Iron (Fe)",
        "Lead (Pb)",
        "Nickel (Ni)",
        "Zinc (Zn)",
        "Nitrate (N)",
        "Orthophosphate (P)",
        "Total Kjeldahl Nitrogen (TKN)",
        "Total Phosphorus",
    ]
    reduction_df = (
        reduction_df.applymap(lambda x: utils.sigFigs(x, n=2))
        .apply(lambda r: "{} - {}".format(r["load_red_lower"], r["load_red_upper"]), axis=1)
        .unstack(level="parameter")
    )[final_cols]

    reduction_df.xs(site, level="site").to_csv("{}_reduction.csv".format(site), quoting=csv.QUOTE_ALL)
Example #2
0
 def _res_with_units(res, units):
     if pandas.isnull(res):
         return '--'
     else:
         return '{} {}'.format(utils.sigFigs(res, 3), units)
Example #3
0
def load_summary_table(wq):  # pragma: no cover
    """ Produces a summary table of loads and confidence intervals for
    varying sample/event types (e.g., composite, unsampled w/ or w/o
    outflor) from tidy water quality data.
    """

    def set_column_name(df):
        df.columns.names = ["quantity"]
        return df

    def formatter(row, location):
        cols = "load_{0};load_{0}_lower;load_{0}_upper".format(location).split(";")
        row_str = "{} ({}; {})".format(*row[cols])
        return row_str

    def drop_unit_index(df):
        df = df.copy()
        df.index = df.index.droplevel("units")
        return df

    def set_no_outlow_zero(df):
        col = ("Effluent", "unsampled", "No")
        df[col] = 0
        return df

    def set_measured_effl(df):
        col = ("Effluent", "composite", "Yes")
        df[col] = df[col].apply(lambda x: str(x).split(" ")[0])
        return df

    def swal_col_levels(df, ii, jj):
        df.columns = df.columns.swaplevel(ii, jj)
        return df

    def pct_reduction(df, incol, outcol):
        return 100 * (df[incol] - df[outcol]) / df[incol]

    final_cols = [
        ("Influent", "unsampled", "No"),
        ("Effluent", "unsampled", "No"),
        ("Influent", "unsampled", "Yes"),
        ("Effluent", "unsampled", "Yes"),
        ("Influent", "composite", "Yes"),
        ("Effluent", "composite", "Yes"),
        "Reduction",
    ]

    final_params = [
        "Nitrate + Nitrite",
        "Nitrate (N)",
        "Orthophosphate (P)",
        "Cadmium (Cd)",
        "Copper (Cu)",
        "Iron (Fe)",
        "Total Kjeldahl Nitrogen (TKN)",
        "Lead (Pb)",
        "Nickel (Ni)",
        "Total Phosphorus",
        "Total Suspended Solids",
        "Zinc (Zn)",
    ]

    loads = (
        wq.groupby(by=["parameter", "units", "has_outflow", "sampletype"])
        .sum()
        .pipe(set_column_name)
        .select(lambda c: c.startswith("load_inflow") or c.startswith("load_outflow"), axis=1)
        .unstack(level="has_outflow")
        .pipe(swal_col_levels, "has_outflow", "quantity")
        .stack(level="has_outflow")
        .dropna()
        .pipe(drop_unit_index)
    )

    main = (
        loads.applymap(lambda x: utils.sigFigs(x, n=3, expthresh=7))
        .assign(Influent=lambda df: df.apply(formatter, args=("inflow",), axis=1))
        .assign(Effluent=lambda df: df.apply(formatter, args=("outflow",), axis=1))
        .unstack(level="sampletype")
        .unstack(level="has_outflow")[["Influent", "Effluent"]]
        .dropna(how="all", axis=1)
    )

    reduction = (
        loads.groupby(level="parameter")
        .sum()
        .assign(load_red=lambda df: pct_reduction(df, "load_inflow", "load_outflow"))
        .assign(load_red_upper=lambda df: pct_reduction(df, "load_inflow_upper", "load_outflow_lower"))
        .assign(load_red_lower=lambda df: pct_reduction(df, "load_inflow_lower", "load_outflow_upper"))
        .applymap(lambda x: utils.sigFigs(x, n=3, expthresh=7))
        .assign(Reduction=lambda df: df.apply(formatter, args=("red",), axis=1))
    )

    summary = main.join(reduction).loc[final_params, final_cols].pipe(set_no_outlow_zero).pipe(set_measured_effl)

    return summary