Example #1
0
    def test_files(self):
        print('\n########################################')
        print('Test File vs File')
        print('\n########################################')
        print('  Loading data...')
        nw_1 = load_nd_from_pickle(filename='ding0_tests_grids_1.pkl')
        nw_2 = load_nd_from_pickle(filename='ding0_tests_grids_2.pkl')
        #test equality
        print('\n########################################')
        print('  Testing equality...')
        equals_e, msg = dataframe_equal(nw_1, nw_1)
        print('  ...' + msg)
        #test difference
        print('\n########################################')
        print('  Testing difference...')
        equals_d, msg = dataframe_equal(nw_1, nw_2)
        print('  ...' + msg)

        #compare results
        if equals_e and not equals_d:
            msg = 'No failure'
            passed = True
        elif equals_e and equals_d:
            msg = 'Only difference failed'
            passed = False
        elif not equals_e and not equals_d:
            msg = 'Only equality failed'
            passed = False
        elif not equals_e and equals_d:
            msg = 'Both failed'
            passed = False
        print('    ' + msg)
        self.assertTrue(passed, msg=msg)
def example_stats(filename):
    """
    Obtain statistics from create grid topology

    Prints some statistical numbers and produces exemplary figures
    """

    nd = results.load_nd_from_pickle(filename=filename)

    nodes_df, edges_df = nd.to_dataframe()

    # get statistical numbers about grid
    stats = results.calculate_mvgd_stats(nd)

    # plot distribution of load/generation of subjacent LV grids
    stations = nodes_df[nodes_df['type'] == 'LV Station']
    f, axarr = plt.subplots(2, sharex=True)
    f.suptitle("Peak load (top) / peak generation capacity (bottom) at LV "
               "substations in kW")
    stations['peak_load'].hist(bins=20, alpha=0.5, ax=axarr[0])
    axarr[0].set_title("Peak load in kW")
    stations['generation_capacity'].hist(bins=20, alpha=0.5, ax=axarr[1])
    axarr[1].set_title("Peak generation capacity in kW")
    plt.show()

    # Introduction of report
    print("You are analyzing MV grid district {mvgd}\n".format(
        mvgd=int(stats.index.values)))

    # print all the calculated stats
    # this isn't a particularly beautiful format but it is
    # information rich
    with option_context('display.max_rows', None, 'display.max_columns', None,
                        'display.max_colwidth', -1):
        print(stats.T)
Example #3
0
def manual_ding0_test(mv_grid_districts=[3545],
                      filename='ding0_tests_grids_1.pkl'):
    ''' Compares a new run of ding0 over districts and an old one saved in
    filename.
    
    Parameters
    ----------
    mv_grid_districts: :obj:`list` of :obj:`int`
        Districts IDs: Defaults to [3545]
    filename: :obj:`str`
        Defaults to 'ding0_tests_grids_1.pkl'
    '''
    print('\n########################################')
    print('Test ding0 vs File')
    print('\n########################################')
    print('  Loading file', filename, '...')
    nw_1 = load_nd_from_pickle(filename=filename)

    print('\n########################################')
    print('  Running ding0 for district', mv_grid_districts, '...')

    # database connection/ session
    engine = db.connection(readonly=True)
    session = sessionmaker(bind=engine)()

    nw_2 = NetworkDing0(name='network')
    nw_2.run_ding0(session=session, mv_grid_districts_no=mv_grid_districts)

    # test equality
    print('\n########################################')
    print('  Testing equality...')
    passed, msg = dataframe_equal(nw_1, nw_2)
    print('    ...' + msg)
Example #4
0
def nd_load_and_stats(filenames, base_path=BASEPATH):
    """
    Load multiple files from disk and generate stats

    Passes the list of files assuming the ding0 data structure as default in
    :code:`~/.ding0`.
    Data will concatenated and key indicators for each grid district are
    returned in table and graphic format

    Parameters
    ----------
    filenames : list of str
        Provide list of files you want to analyze
    base_path : str
        Root directory of Ding0 data structure, i.e. '~/.ding0' (which is
        default).
    Returns
    -------
    stats : pandas.DataFrame
        Statistics of each MV grid districts
    """

    # load Ding0 data
    nds = []
    for filename in filenames:
        try:
            nd_load = results.load_nd_from_pickle(filename=
                                             os.path.join(base_path,
                                                          'results',
                                                          filename))

            nds.append(nd_load)
        except:
            print("File {mvgd} not found. It was maybe excluded by Ding0 or "
                  "just forgotten to generate by you...".format(mvgd=filename))

    nd = nds[0]

    for n in nds[1:]:
        nd.add_mv_grid_district(n._mv_grid_districts[0])

    nodes_df, edges_df = nd.to_dataframe()

    # get statistical numbers about grid
    stats = results.calculate_mvgd_stats(nodes_df, edges_df)

    # TODO: correct LV peak load/ generation capacity. Same in all LV GD
    return stats
Example #5
0
    def test_ding0_file(self):
        print('\n########################################')
        print('Test ding0 vs File')
        print('\n########################################')
        print('  Loading data...')
        nw_1 = load_nd_from_pickle(filename='ding0_tests_grids_1.pkl')

        print('\n########################################')
        print('  Running ding0 for the same configuration...')

        # database connection/ session
        engine = db.connection(readonly=True)
        session = sessionmaker(bind=engine)()
        mv_grid_districts = [3545]

        nw_2 = NetworkDing0(name='network')
        nw_2.run_ding0(session=session, mv_grid_districts_no=mv_grid_districts)

        #test equality
        print('  Testing equality...')
        passed, msg = dataframe_equal(nw_1, nw_2)
        print('    ...' + msg)

        self.assertTrue(passed, msg=msg)
Example #6
0
from ding0.tools.results import export_network, load_nd_from_pickle, export_data_tocsv
import json

grid_id = '76'
base_path = "/home/local/RL-INSTITUT/inga.loeser/ding0/20170922152523/ding0_grids__"
path = ''.join([base_path, grid_id])

# Load network and run export function
nw = load_nd_from_pickle(filename=''.join([path, '.pkl']))
run_id, lv_grid, lv_gen, lv_cd, lv_stations, lv_trafos, lv_loads, mv_grid, mv_gen, mv_cb, mv_cd, mv_stations, mv_trafos, mv_loads, edges, mapping = export_network(
    nw)

# Create directory for exported data
import os
if not os.path.exists(os.path.join(path, run_id)):
    os.makedirs(os.path.join(path, run_id))

export_data_tocsv(path, run_id, lv_grid, lv_gen, lv_cd, lv_stations, lv_trafos,
                  lv_loads, mv_grid, mv_gen, mv_cb, mv_cd, mv_stations,
                  mv_trafos, mv_loads, edges, mapping)

# Save metadata

metadata = nw.metadata  #keys: version, mv_grid_districts, database_tables, data_version, assumptions, run_id
with open(os.path.join(path, run_id, 'Ding0_{}.meta'.format(run_id)),
          'w') as f:  #'Ding0.meta'),'w') as f: #
    json.dump(metadata, f)

print(mapping)
Example #7
0
def example_stats(filename, plotpath=''):
    """
    Obtain statistics from create grid topology

    Prints some statistical numbers and produces exemplary figures
    """

    nd = results.load_nd_from_pickle(filename=filename)

    nodes_df, edges_df = nd.to_dataframe()

    # get statistical numbers about grid
    stats = results.calculate_mvgd_stats(nodes_df, edges_df)

    # plot distribution of load/generation of subjacent LV grids
    stations = nodes_df[nodes_df['type'] == 'LV Station']
    f, axarr = plt.subplots(2, sharex=True)
    f.suptitle("Peak load (top)/ peak generation capacity (bottom) at LV "
              "substation in kW")
    stations.hist(column=['peak_load'], bins=20, alpha=0.5, ax=axarr[0])
    axarr[0].set_title("Peak load in kW")
    stations.hist(column=['generation_capacity'], bins=20, alpha=0.5, ax=axarr[1])
    axarr[1].set_title("Peak generation capacity in kW")
    plt.show()

    # Introduction of report
    print("You are analyzing MV grid district {mvgd}\n".format(
        mvgd=int(stats.index.values)))

    # Print peak load
    print("Total peak load: {load:.0f} kW".format(load=float(stats['peak_load'])))
    print("\t thereof MV: {load:.0f} kW".format(load=0))
    print("\t thereof LV: {load:.0f} kW".format(load=float(stats['LV peak load'])))

    # Print generation capacity
    print("\nTotal generation capacity: {gen:.0f} kW".format(
        gen=float(stats['generation_capacity'])))
    print("\t thereof MV: {gen:.0f} kW".format(
        gen=float(stats['MV generation capacity'])))
    print("\t thereof LV: {gen:.0f} kW".format(
        gen=float(stats['LV generation capacity'])))

    # print total length of cables/overhead lines
    print("\nTotal cable length: {length:.1f} km".format(length=float(stats['km_cable'])))
    print("Total line length: {length:.1f} km".format(length=float(stats['km_line'])))

    # Other predefined functions bring extra information for example the number
    # of generators directly connected to the  bus bar of LV station
    stations_generators = results.lv_grid_generators_bus_bar(nd)
    print('\nGenerators directly connected to the substation')
    for k, v in stations_generators.items():
        if v:
            print("{station}: {gens}".format(station=k, gens=len(v)))

    # Number of line/cable equipment type
    print("\n")
    for t, cnt in dict(edges_df.groupby('type_name').size()).items():
        print("Line/cable of type {type} occurs {cnt} times".format(type=t,
                                                                    cnt=cnt))

    # Access results directly from nd-object if they are not available in stats/
    # nodes_df or edges_df
    # One example: print length of each half ring in this MVGD
    print("\n")
    root = nd._mv_grid_districts[0].mv_grid.station()
    for circ_breaker in nd._mv_grid_districts[0].mv_grid.circuit_breakers():
        for half_ring in [0, 1]:
            half_ring_length = nd._mv_grid_districts[
                                   0].mv_grid.graph_path_length(
                circ_breaker.branch_nodes[half_ring], root) / 1000
            print('Length to circuit breaker', repr(circ_breaker),
                  ', half-ring', str(half_ring), ':', str(half_ring_length),
                  'km')
Example #8
0
        == load_effective_lv_load
    ],
                                axis=1)
    compare_by_load.columns = ['table', 'ding0', 'equal?']
    compare_by_load.index.names = ['sector']

    return compare_by_district, compare_by_load


########################################################
if __name__ == "__main__":
    # database connection/ session
    engine = db.connection(section='oedb')
    session = sessionmaker(bind=engine)()

    nw = load_nd_from_pickle(filename='ding0_tests_grids_1.pkl')

    compare_by_level, compare_by_type = validate_generation(session, nw)
    print('\nCompare Generation by Level')
    print(compare_by_level)
    print('\nCompare Generation by Type/Subtype')
    print(compare_by_type)

    compare_by_la, compare_la_ids = validate_load_areas(session, nw)
    print('\nCompare Load by Load Areas')
    print(compare_by_la)
    #print(compare_la_ids)

    compare_by_district, compare_by_load = validate_lv_districts(session, nw)
    print('\nCompare Load by LV Districts')
    print(compare_by_district)
        # run DING0 on selected MV Grid District
        nd.run_ding0(session=session, mv_grid_districts_no=mv_grid_districts)

        # export grid to file (pickle)
        save_nd_to_pickle(nd,
                          path=test_path,
                          filename='ding0_grids_example{}.pkl'.format(i))
#endregion

#region EXTRACT DATAFRAMES
components_for_comparison = {}
lines_for_comparison = {}
for i in range(nr_test_runs):
    try:
        if not is_load_from_csv:
            nw = load_nd_from_pickle(
                path=test_path, filename='ding0_grids_example{}.pkl'.format(i))

            if not is_alternative_lv_line_naming:
                # extract component dataframes from network
                components = create_grid_component_dataframes_from_network(nw)
                if is_save_to_csv:
                    # save dataframes as csv
                    dir = test_path + "/" + str(i)
                    if not os.path.exists(dir):
                        os.makedirs(dir)
                    components['mv_grids'].to_csv(os.path.join(
                        dir, '{}_mv_grids.csv'.format(i)),
                                                  sep=';')
                    components['lv_grids'].to_csv(os.path.join(
                        dir, '{}_lv_grids.csv'.format(i)),
                                                  sep=';')