Skip to content

Common processing step: count_hourly_booleans #119

@ahobeost

Description

@ahobeost

When running simulations, booleans can be saved to timesteps, or as a mean hourly value.
The following code converts the mean hourly values to a total nr of hours in the True/on or False/off position.

import pandas as _pd
import pathlib as _pl
import pytest as _pt


def count_hourly_booleans(df_hourly: _pd.DataFrame, boolean_column: str) -> tuple[float, float]:
    """Helps convert averaged hourly boolean values to total hours True/on and False/off."""
    if boolean_column not in df_hourly.columns:
        raise ValueError(f"Incorrect boolean_column provided: {boolean_column}")

    count_series = df_hourly[boolean_column].value_counts()
    
    n_min_per_hour = 60
    n_minutes_on = 0
    n_minutes_off = 0
    for mean_boolean, n_hours in count_series.items():
        n_minutes_on += mean_boolean * n_min_per_hour * n_hours
        n_minutes_off += (1 - mean_boolean) * n_min_per_hour * n_hours

    total_hours_on = n_minutes_on / n_min_per_hour
    total_hours_off = n_minutes_off / n_min_per_hour

    return total_hours_on, total_hours_off


file_path = _pl.Path("T:\\ahobe\\Boolean_count") / "RESULTS_HR_cleaned.Prt"
df = _pd.read_csv(file_path, delimiter="\t")
df = df.rename(columns=lambda x: x.strip())

on, off = count_hourly_booleans(df, "Coll_TES_full")
print(f"\n\nOn: {on}")
print(f"Off: {off}")
# expected: 1795 times 1 and 6965 times 0

with _pt.raises(ValueError):
    count_hourly_booleans(df, "NO_SUCH_COLUMN")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions