# Path: edisgo/flex_opt/check_tech_constraints.py
# def mv_line_load(edisgo_obj):
# def lv_line_load(edisgo_obj):
# def lines_allowed_load(edisgo_obj, voltage_level):
# def lines_relative_load(edisgo_obj, lines_allowed_load):
# def _line_load(edisgo_obj, voltage_level):
# def hv_mv_station_load(edisgo_obj):
# def mv_lv_station_load(edisgo_obj):
# def _station_load(edisgo_obj, grid):
# def mv_voltage_deviation(edisgo_obj, voltage_levels="mv_lv"):
# def lv_voltage_deviation(edisgo_obj, mode=None, voltage_levels="mv_lv"):
# def _mv_allowed_voltage_limits(edisgo_obj, voltage_levels):
# def _lv_allowed_voltage_limits(edisgo_obj, lv_grid, mode):
# def voltage_diff(edisgo_obj, buses, v_dev_allowed_upper, v_dev_allowed_lower):
# def _voltage_deviation(edisgo_obj, buses, v_limits_upper, v_limits_lower):
#     def _append_crit_buses(df):
# def check_ten_percent_voltage_deviation(edisgo_obj):


import logging
import pandas as pd
import numpy as np
from edisgo.flex_opt import check_tech_constraints
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans


def _scored_critical_current(edisgo_obj, grid):
    # Get allowed current per line per time step
    i_lines_allowed = check_tech_constraints.lines_allowed_load(
        edisgo_obj, "mv"
    )
    i_lines_pfa = edisgo_obj.results.i_res[grid.lines_df.index]

    # Get current relative to allowed current
    relative_i_res = i_lines_pfa / i_lines_allowed

    # Get lines that have violations
    crit_lines_score = relative_i_res[relative_i_res > 1]

    # Remove time steps with no violations
    crit_lines_score = crit_lines_score.dropna(how="all", axis=0)

    # Cumulate violations over all lines per time step
    crit_lines_score = crit_lines_score.sum(axis=1)

    return crit_lines_score.sort_values(ascending=False)


def _scored_critical_overvoltage(edisgo_obj, grid):
    nodes = grid.buses_df.index

    # Get allowed deviations per time step
    (
        v_dev_allowed_upper,

# Path: edisgo/flex_opt/check_tech_constraints.py
# def mv_line_load(edisgo_obj):
# def lv_line_load(edisgo_obj):
# def lines_allowed_load(edisgo_obj, voltage_level):
# def lines_relative_load(edisgo_obj, lines_allowed_load):
# def _line_load(edisgo_obj, voltage_level):
# def hv_mv_station_load(edisgo_obj):
# def mv_lv_station_load(edisgo_obj):
# def _station_load(edisgo_obj, grid):
# def mv_voltage_deviation(edisgo_obj, voltage_levels="mv_lv"):
# def lv_voltage_deviation(edisgo_obj, mode=None, voltage_levels="mv_lv"):
# def _mv_allowed_voltage_limits(edisgo_obj, voltage_levels):
# def _lv_allowed_voltage_limits(edisgo_obj, lv_grid, mode):
# def voltage_diff(edisgo_obj, buses, v_dev_allowed_upper, v_dev_allowed_lower):
# def _voltage_deviation(edisgo_obj, buses, v_limits_upper, v_limits_lower):
#     def _append_crit_buses(df):
# def check_ten_percent_voltage_deviation(edisgo_obj):


# Path: edisgo/opf/timeseries_reduction.py
import logging
import pandas as pd
import numpy as np
from edisgo.flex_opt import check_tech_constraints
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans



def _scored_critical_current(edisgo_obj, grid):
    # Get allowed current per line per time step
    i_lines_allowed = check_tech_constraints.lines_allowed_load(
        edisgo_obj, "mv"
    )
    i_lines_pfa = edisgo_obj.results.i_res[grid.lines_df.index]

    # Get current relative to allowed current
    relative_i_res = i_lines_pfa / i_lines_allowed

    # Get lines that have violations
    crit_lines_score = relative_i_res[relative_i_res > 1]

    # Remove time steps with no violations
    crit_lines_score = crit_lines_score.dropna(how="all", axis=0)

    # Cumulate violations over all lines per time step
    crit_lines_score = crit_lines_score.sum(axis=1)

    return crit_lines_score.sort_values(ascending=False)


def _scored_critical_overvoltage(edisgo_obj, grid):
    nodes = grid.buses_df.index

    # Get allowed deviations per time step
    (
        v_dev_allowed_upper,

        v_dev_allowed_lower,