<commit_before>import pandas as pd


def one_storage_per_feeder(edisgo):

    def feeder_ranking(grid_expansion_costs):
        """
        Get feeder ranking from grid expansion costs DataFrame.

        Parameters
        ----------
        grid_expansion_costs : :pandas:`pandas.DataFrame<dataframe>`
            grid_expansion_costs DataFrame from :class:`~.grid.network.Results`

        Returns
        -------
        :pandas:`pandas.Series<series>`
            Series with ranked MV feeders. Feeders are ranked by total grid
            expansion costs of all measures conducted in the feeder. The
            feeder with the highest costs is in the first row and the feeder
            with the lowest costs in the last row.

        """
        return grid_expansion_costs.groupby(['mv_feeder'], sort=False).sum().\
            reset_index().sort_values(by=['total_costs'], ascending=False)[
            'mv_feeder']

    # rank MV feeders by grid expansion costs calculated with feed-in case
    if edisgo.network.results.grid_expansion_costs is not None:
        ranked_feeders = feeder_ranking(
            edisgo.network.results.grid_expansion_costs)
    else:
        # conduct grid reinforcement
        edisgo.reinforce()
        # rank feeders
        ranked_feeders = feeder_ranking(
            edisgo.network.results.grid_expansion_costs)
        # reset edisgo object (new grid -> moved to reinforce_grid)
        #edisgo.import_from_ding0(kwargs.get('ding0_grid', None))
    # for every feeder:
    # ** install storage at the node of the overloaded lines farthest away from the HV/MV transformer
    # ** estimate residual line load (neglect grid losses) to identify all time steps where line is over-loaded
    # ** determine storage capacity by adding storage dispatch time series to residual line load and checking if line load can be reduced; maximum needed reduction to avoid all line over-loadings determines the storage capacity
    # ** if there is storage capacity left, rerun the past steps for the next feeder in the ranking list<commit_msg>Add random feeder ranking<commit_after>import pandas as pd


def one_storage_per_feeder(edisgo):

    def feeder_ranking(grid_expansion_costs):
        """
        Get feeder ranking from grid expansion costs DataFrame.

        Parameters
        ----------
        grid_expansion_costs : :pandas:`pandas.DataFrame<dataframe>`
            grid_expansion_costs DataFrame from :class:`~.grid.network.Results`

        Returns
        -------
        :pandas:`pandas.Series<series>`
            Series with ranked MV feeders. Feeders are ranked by total grid
            expansion costs of all measures conducted in the feeder. The
            feeder with the highest costs is in the first row and the feeder
            with the lowest costs in the last row.

        """
        return grid_expansion_costs.groupby(['mv_feeder'], sort=False).sum().\
            reset_index().sort_values(by=['total_costs'], ascending=False)[
            'mv_feeder']

    # # rank MV feeders by grid expansion costs calculated with feed-in case
    # if edisgo.network.results.grid_expansion_costs is not None:
    #     ranked_feeders = feeder_ranking(
    #         edisgo.network.results.grid_expansion_costs)
    # else:
    #     # conduct grid reinforcement
    #     edisgo.reinforce()
    #     # rank feeders
    #     ranked_feeders = feeder_ranking(
    #         edisgo.network.results.grid_expansion_costs)
    #     # reset edisgo object (new grid -> moved to reinforce_grid)
    #     #edisgo.import_from_ding0(kwargs.get('ding0_grid', None))

    # work around for now: random feeder ranking
    # get nodes adjacent to HV/MV station
    station_adj_nodes = list(edisgo.network.mv_grid.graph.edge[
        edisgo.network.mv_grid.station].keys())
    # for each adjacent node check if
    ranked_feeders = []
    for node in station_adj_nodes:
        if len(edisgo.network.mv_grid.graph.edge[node])  > 1:
            ranked_feeders.append(edisgo.network.mv_grid.graph.line_from_nodes(
                edisgo.network.mv_grid.station, node))

    # for every feeder:
    # ** install storage at the node of the overloaded lines farthest away from the HV/MV transformer
    # ** estimate residual line load (neglect grid losses) to identify all time steps where line is over-loaded
    # ** determine storage capacity by adding storage dispatch time series to residual line load and checking if line load can be reduced; maximum needed reduction to avoid all line over-loadings determines the storage capacity
    # ** if there is storage capacity left, rerun the past steps for the next feeder in the ranking list
{'fraction_non_alphanumeric': 0.07616812459995732, 'fraction_numerical': 0.0010667804565820354, 'mean_word_length': 2.7444089456869007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '1352146', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1254, 'n_words': 429}