<commit_before>import os
import pandas as pd
import numpy as np
import pytest

from edisgo import EDisGo
from edisgo.flex_opt import check_tech_constraints


class TestCheckTechConstraints:
    @classmethod
    def setup_class(self):
        """Setup default values"""
        dirname = os.path.realpath(os.path.dirname(__file__) + "/..")
        test_network_directory = os.path.join(dirname, "ding0_test_network")
        self.edisgo = EDisGo(
            ding0_grid=test_network_directory, worst_case_analysis="worst-case"
        )
        self.timesteps = pd.date_range("1/1/1970", periods=2, freq="H")

    @pytest.fixture(autouse=True)
    def run_power_flow(self):
        """
        Fixture to run new power flow before each test.

        """
        self.edisgo.analyze()

    @pytest.fixture()
    def mv_voltage_issues(self):
        """
        Fixture to create voltage issues in MV grid.

        """
        bus0 = "Bus_Generator_1"
        bus1 = "Bus_GeneratorFluctuating_2"
        bus2 = "Bus_GeneratorFluctuating_3"

        # create over- and undervoltage at bus0, with higher undervoltage
        # deviation
        self.edisgo.results._v_res.loc[self.timesteps[0], bus0] = 1.11
        self.edisgo.results._v_res.loc[self.timesteps[1], bus0] = 0.88
        # create overvoltage at bus1
        self.edisgo.results._v_res.loc[self.timesteps[0], bus1] = 1.11
        # create undervoltage at bus0
        self.edisgo.results._v_res.loc[self.timesteps[0], bus2] = 0.895

    def test_voltage_diff(self, mv_voltage_issues):

        uv_violations, ov_violations = check_tech_constraints.voltage_diff(
            self.edisgo,
            self.edisgo.topology.mv_grid.buses_df.index,
            pd.Series(data=1.1, index=self.timesteps),
            pd.Series(data=0.9, index=self.timesteps),
        )

        # check shapes of under- and overvoltage dataframes
        assert (2, 2) == uv_violations.shape
        assert (1, 2) == ov_violations.shape
        # check under- and overvoltage deviation values
        assert np.isclose(
            uv_violations.at["Bus_Generator_1", self.timesteps[1]], 0.02
        )
        assert np.isclose(
            uv_violations.at["Bus_GeneratorFluctuating_3", self.timesteps[0]],
            0.005,
        )
        assert np.isclose(
            ov_violations.at["Bus_GeneratorFluctuating_2", self.timesteps[0]],
            0.01,
        )
        assert np.isclose(
            uv_violations.at["Bus_Generator_1", self.timesteps[0]], -0.21
        )

    def test__voltage_deviation(self, mv_voltage_issues):

        v_violations = check_tech_constraints._voltage_deviation(
            self.edisgo,
            self.edisgo.topology.mv_grid.buses_df.index,
            pd.Series(data=1.1, index=self.timesteps),
            pd.Series(data=0.9, index=self.timesteps),
        )

        # check shape of dataframe
        assert (3, 2) == v_violations.shape<commit_msg>Make fixture creating voltage issues a function<commit_after>import os
import pandas as pd
import numpy as np
import pytest

from edisgo import EDisGo
from edisgo.flex_opt import check_tech_constraints


class TestCheckTechConstraints:
    @classmethod
    def setup_class(self):
        """Setup default values"""
        dirname = os.path.realpath(os.path.dirname(__file__) + "/..")
        test_network_directory = os.path.join(dirname, "ding0_test_network")
        self.edisgo = EDisGo(
            ding0_grid=test_network_directory, worst_case_analysis="worst-case"
        )
        self.timesteps = pd.date_range("1/1/1970", periods=2, freq="H")

    @pytest.fixture(autouse=True)
    def run_power_flow(self):
        """
        Fixture to run new power flow before each test.

        """
        self.edisgo.analyze()

    def mv_voltage_issues(self):
        """
        Fixture to create voltage issues in MV grid.

        """
        bus0 = "Bus_Generator_1"
        bus1 = "Bus_GeneratorFluctuating_2"
        bus2 = "Bus_GeneratorFluctuating_3"

        # create over- and undervoltage at bus0, with higher undervoltage
        # deviation
        self.edisgo.results._v_res.loc[self.timesteps[0], bus0] = 1.11
        self.edisgo.results._v_res.loc[self.timesteps[1], bus0] = 0.88
        # create overvoltage at bus1
        self.edisgo.results._v_res.loc[self.timesteps[0], bus1] = 1.11
        # create undervoltage at bus0
        self.edisgo.results._v_res.loc[self.timesteps[0], bus2] = 0.895

    def test_voltage_diff(self):

        # create voltage issues
        self.mv_voltage_issues()

        uv_violations, ov_violations = check_tech_constraints.voltage_diff(
            self.edisgo,
            self.edisgo.topology.mv_grid.buses_df.index,
            pd.Series(data=1.1, index=self.timesteps),
            pd.Series(data=0.9, index=self.timesteps),
        )

        # check shapes of under- and overvoltage dataframes
        assert (2, 2) == uv_violations.shape
        assert (1, 2) == ov_violations.shape
        # check under- and overvoltage deviation values
        assert np.isclose(
            uv_violations.at["Bus_Generator_1", self.timesteps[1]], 0.02
        )
        assert np.isclose(
            uv_violations.at["Bus_GeneratorFluctuating_3", self.timesteps[0]],
            0.005,
        )
        assert np.isclose(
            ov_violations.at["Bus_GeneratorFluctuating_2", self.timesteps[0]],
            0.01,
        )
        assert np.isclose(
            uv_violations.at["Bus_Generator_1", self.timesteps[0]], -0.21
        )

    def test__voltage_deviation(self):

        # create voltage issues
        self.mv_voltage_issues()

        v_violations = check_tech_constraints._voltage_deviation(
            self.edisgo,
            self.edisgo.topology.mv_grid.buses_df.index,
            pd.Series(data=1.1, index=self.timesteps),
            pd.Series(data=0.9, index=self.timesteps),
        )

        # check shape of dataframe
        assert (3, 2) == v_violations.shape
{'fraction_non_alphanumeric': 0.10383973288814691, 'fraction_numerical': 0.024707846410684475, 'mean_word_length': 2.375211267605634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '925375', 'n_tokens_mistral': 2080, 'n_tokens_neox': 1934, 'n_words': 356}