Ejemplo n.º 1
0
def test_run_ex_3_realistic():
    # given
    global this_dir, logging_conf_fpath
    sim_conf_fpath = 'test_sets/ex_3_full/run_realistic.ini'
    exp_log_fpath = 'test_sets/ex_3_full/exp_log_realistic.txt'
    exp_end_stats_fpath = os.path.normpath(
        'test_sets/ex_3_full/exp_end_stats_realistic.tsv')
    res_end_stats_fpath = os.path.normpath('test_sets/useless/useless_3.tsv')
    floader = fl.FileLoader()

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    cs.run(sim_conf_fpath, floader)

    # then
    assert sf.compare_files_by_line('log.txt', exp_log_fpath, False)
    assert sf.compare_files_by_line(res_end_stats_fpath, exp_end_stats_fpath,
                                    False)

    # tear down
    shutil.rmtree(
        os.path.join(this_dir,
                     os.path.normpath('test_sets/ex_3_full/res_realistic')))
    os.remove(
        os.path.join(this_dir,
                     os.path.normpath('test_sets/useless/useless_3.tsv')))
Ejemplo n.º 2
0
def test_run_ex_1_caching():
    # given
    global this_dir, logging_conf_fpath
    sim_conf_fpath = "test_sets/ex_1_full/run_realistic.ini"
    exp_log_fpath = "test_sets/ex_1_full/exp_log_realistic.txt"
    floader = fl.FileLoader()

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    cs.run(sim_conf_fpath, floader)

    # then
    assert sf.compare_files_by_line("log.txt", exp_log_fpath, False)

    # tear down
    shutil.rmtree(os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/res_realistic")))
    os.remove(os.path.join(this_dir, os.path.normpath("test_sets/useless/useless_1.tsv")))

    # when
    sf.setup_logging(logging_conf_fpath)
    cs.run(sim_conf_fpath, floader)

    # then
    assert sf.compare_files_by_line("log.txt", exp_log_fpath, False)

    # tear down
    shutil.rmtree(os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/res_realistic")))
    os.remove(os.path.join(this_dir, os.path.normpath("test_sets/useless/useless_1.tsv")))
Ejemplo n.º 3
0
def test_choose_most_intra_used_nodes():
    global this_dir, logging_conf_fpath
    netw_a_fpath = os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/A.graphml"))

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    A = nx.read_graphml(netw_a_fpath)
    chosen_nodes_1 = cs.choose_most_intra_used_nodes(A, 1, "transmission_substation", "sort_by_id")
    chosen_nodes_2 = cs.choose_most_intra_used_nodes(A, 2, "transmission_substation", "sort_by_id")
    chosen_nodes_3 = cs.choose_most_intra_used_nodes(A, 3, "transmission_substation", "sort_by_id")

    # then
    assert chosen_nodes_1 == ["T1"]
    assert sorted(chosen_nodes_2) == ["T1", "T3"]
    assert sorted(chosen_nodes_3) == ["T1", "T2", "T3"]
Ejemplo n.º 4
0
def test_find_uncontrolled_pow_nodes():
    global this_dir, logging_conf_fpath
    netw_a_fpath = os.path.join(
        this_dir, os.path.normpath('test_sets/ex_4_full/A.graphml'))
    netw_b_fpath = os.path.join(
        this_dir, os.path.normpath('test_sets/ex_4_full/B.graphml'))
    netw_inter_fpath = os.path.join(
        this_dir, os.path.normpath('test_sets/ex_4_full/Inter.graphml'))

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    A = nx.read_graphml(netw_a_fpath)
    B = nx.read_graphml(netw_b_fpath)
    I = nx.read_graphml(netw_inter_fpath)

    tmp_B = B.copy()
    tmp_I = I.copy()
    tmp_B.remove_node('R1')
    tmp_I.remove_node('R1')
    found_nodes_1 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)

    tmp_B = B.copy()
    tmp_I = I.copy()
    tmp_B.remove_node('R4')
    tmp_I.remove_node('R4')
    found_nodes_2 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)

    tmp_B = B.copy()
    tmp_I = I.copy()
    tmp_B.remove_node('C1')
    tmp_I.remove_node('C1')
    found_nodes_3 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)
    tmp_B.remove_node('C2')
    tmp_I.remove_node('C2')
    found_nodes_4 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)

    # then
    assert found_nodes_1.keys() == [
        'no_sup_relays', 'no_com_path', 'no_sup_ccs'
    ]
    assert sorted(found_nodes_1['no_sup_relays']) == ['D1', 'G1', 'T1']
    assert sorted(found_nodes_2['no_com_path']) == ['D2', 'G2', 'T2']
    assert found_nodes_3['no_sup_ccs'] == []
    assert sorted(
        found_nodes_4['no_sup_ccs']) == ['D1', 'D2', 'G1', 'G2', 'T1', 'T2']
Ejemplo n.º 5
0
def test_calc_stats_on_centrality():
    global this_dir, logging_conf_fpath
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    file_loader = fl.FileLoader()
    centrality_fpath = os.path.join(
        this_dir,
        os.path.normpath('test_sets/ex_centralities/centralities.json'))
    centrality_info = file_loader.fetch_json(centrality_fpath)

    centrality_name = 'centrality_a'
    result_key_suffix = 'centr_a'
    attacked_nodes = ['A1', 'A4', 'A8', 'A9']
    exp_centr_stats = {
        'p_q_1_centr_a': 0.5,
        'p_q_2_centr_a': 0.0,
        'p_q_3_centr_a': 0.5,
        'p_q_4_centr_a': 0.0,
        'p_q_5_centr_a': 1.0,
        'p_tot_centr_a': 0.4,
        'sum_centr_a': 4.0
    }

    centr_stats = cs.calc_atk_centrality_stats(attacked_nodes, centrality_name,
                                               result_key_suffix,
                                               centrality_info)
    assert centr_stats == exp_centr_stats

    centrality_name = 'centrality_b'
    result_key_suffix = 'centr_b'
    attacked_nodes = ['A1', 'A4', 'A8', 'A9']
    exp_centr_stats = {
        'p_q_1_centr_b': 0.5,
        'p_q_2_centr_b': 0.0,
        'p_q_3_centr_b': 0.5,
        'p_q_4_centr_b': 0.0,
        'p_q_5_centr_b': 1.0,
        'p_tot_centr_b': (22.0 / 45.0),
        'sum_centr_b': 22.0
    }

    centr_stats = cs.calc_atk_centrality_stats(attacked_nodes, centrality_name,
                                               result_key_suffix,
                                               centrality_info)
    assert centr_stats == exp_centr_stats
Ejemplo n.º 6
0
def test_choose_most_inter_used_nodes():
    global this_dir, logging_conf_fpath
    netw_a_fpath = os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/A.graphml"))
    netw_inter_fpath = os.path.join(this_dir, os.path.normpath("test_sets/ex_1_full/Inter.graphml"))

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    A = nx.read_graphml(netw_a_fpath)
    I = nx.read_graphml(netw_inter_fpath)
    chosen_nodes_1 = cs.choose_most_inter_used_nodes(A, I, 1, "distribution_substation", "sort_by_id")
    chosen_nodes_2 = cs.choose_most_inter_used_nodes(A, I, 2, "distribution_substation", "sort_by_id")
    chosen_nodes_3 = cs.choose_most_inter_used_nodes(A, I, 3, "distribution_substation", "sort_by_id")

    # then
    assert chosen_nodes_1 == ["D2"]
    assert sorted(chosen_nodes_2) == ["D2", "D3"]
    assert sorted(chosen_nodes_3) == ["D1", "D2", "D3"]
Ejemplo n.º 7
0
def test_run_ex_unstable_2():
    # given
    global this_dir, logging_conf_fpath
    sim_conf_fpath = "test_sets/ex_unstable_2/run_uniform.ini"
    exp_log_fpath = "test_sets/ex_unstable_2/exp_log_uniform.txt"
    floader = fl.FileLoader()

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    cs.run(sim_conf_fpath, floader)

    # then
    assert sf.compare_files_by_line("log.txt", exp_log_fpath, False)

    # tear down
    shutil.rmtree(os.path.join(this_dir, os.path.normpath("test_sets/ex_unstable_2/res_uniform")))
    os.remove(os.path.join(this_dir, os.path.normpath("test_sets/useless/useless_4.tsv")))
Ejemplo n.º 8
0
def test_choose_most_intra_used_nodes():
    global this_dir, logging_conf_fpath
    netw_a_fpath = os.path.join(
        this_dir, os.path.normpath('test_sets/ex_1_full/A.graphml'))

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    A = nx.read_graphml(netw_a_fpath)
    chosen_nodes_1 = cs.choose_most_intra_used_nodes(
        A, 1, 'transmission_substation', 'sort_by_id')
    chosen_nodes_2 = cs.choose_most_intra_used_nodes(
        A, 2, 'transmission_substation', 'sort_by_id')
    chosen_nodes_3 = cs.choose_most_intra_used_nodes(
        A, 3, 'transmission_substation', 'sort_by_id')

    # then
    assert chosen_nodes_1 == ['T1']
    assert sorted(chosen_nodes_2) == ['T1', 'T3']
    assert sorted(chosen_nodes_3) == ['T1', 'T2', 'T3']
Ejemplo n.º 9
0
def test_find_uncontrolled_pow_nodes():
    global this_dir, logging_conf_fpath
    netw_a_fpath = os.path.join(this_dir, os.path.normpath("test_sets/ex_4_full/A.graphml"))
    netw_b_fpath = os.path.join(this_dir, os.path.normpath("test_sets/ex_4_full/B.graphml"))
    netw_inter_fpath = os.path.join(this_dir, os.path.normpath("test_sets/ex_4_full/Inter.graphml"))

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    A = nx.read_graphml(netw_a_fpath)
    B = nx.read_graphml(netw_b_fpath)
    I = nx.read_graphml(netw_inter_fpath)

    tmp_B = B.copy()
    tmp_I = I.copy()
    tmp_B.remove_node("R1")
    tmp_I.remove_node("R1")
    found_nodes_1 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)

    tmp_B = B.copy()
    tmp_I = I.copy()
    tmp_B.remove_node("R4")
    tmp_I.remove_node("R4")
    found_nodes_2 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)

    tmp_B = B.copy()
    tmp_I = I.copy()
    tmp_B.remove_node("C1")
    tmp_I.remove_node("C1")
    found_nodes_3 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)
    tmp_B.remove_node("C2")
    tmp_I.remove_node("C2")
    found_nodes_4 = cs.find_uncontrolled_pow_nodes(A, tmp_B, tmp_I, True)

    # then
    assert found_nodes_1.keys() == ["no_sup_relays", "no_com_path", "no_sup_ccs"]
    assert sorted(found_nodes_1["no_sup_relays"]) == ["D1", "G1", "T1"]
    assert sorted(found_nodes_2["no_com_path"]) == ["D2", "G2", "T2"]
    assert found_nodes_3["no_sup_ccs"] == []
    assert sorted(found_nodes_4["no_sup_ccs"]) == ["D1", "D2", "G1", "G2", "T1", "T2"]
Ejemplo n.º 10
0
def test_run_ex_unstable_2():
    # given
    global this_dir, logging_conf_fpath
    sim_conf_fpath = 'test_sets/ex_unstable_2/run_uniform.ini'
    exp_log_fpath = 'test_sets/ex_unstable_2/exp_log_uniform.txt'
    floader = fl.FileLoader()

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    cs.run(sim_conf_fpath, floader)

    # then
    assert sf.compare_files_by_line('log.txt', exp_log_fpath, False)

    # tear down
    shutil.rmtree(
        os.path.join(this_dir,
                     os.path.normpath('test_sets/ex_unstable_2/res_uniform')))
    os.remove(
        os.path.join(this_dir,
                     os.path.normpath('test_sets/useless/useless_4.tsv')))
Ejemplo n.º 11
0
def test_choose_most_inter_used_nodes():
    global this_dir, logging_conf_fpath
    netw_a_fpath = os.path.join(
        this_dir, os.path.normpath('test_sets/ex_1_full/A.graphml'))
    netw_inter_fpath = os.path.join(
        this_dir, os.path.normpath('test_sets/ex_1_full/Inter.graphml'))

    # when
    os.chdir(this_dir)
    sf.setup_logging(logging_conf_fpath)
    A = nx.read_graphml(netw_a_fpath)
    I = nx.read_graphml(netw_inter_fpath)
    chosen_nodes_1 = cs.choose_most_inter_used_nodes(
        A, I, 1, 'distribution_substation', 'sort_by_id')
    chosen_nodes_2 = cs.choose_most_inter_used_nodes(
        A, I, 2, 'distribution_substation', 'sort_by_id')
    chosen_nodes_3 = cs.choose_most_inter_used_nodes(
        A, I, 3, 'distribution_substation', 'sort_by_id')

    # then
    assert chosen_nodes_1 == ['D2']
    assert sorted(chosen_nodes_2) == ['D2', 'D3']
    assert sorted(chosen_nodes_3) == ['D1', 'D2', 'D3']
Ejemplo n.º 12
0
__author__ = 'Agostino Sturaro'

import os
import netw_creator as nc
import shared_functions as sf

# This is a simple file to run custom tests

this_dir = os.path.normpath(os.path.dirname(__file__))
os.chdir(this_dir)

logging_conf_fpath = os.path.normpath('logging_base_conf.json')
sf.setup_logging(logging_conf_fpath)

for i in range(0, 10):
    netw_conf_fpath = os.path.normpath(
        '../Simulations/temp/config_{}.ini'.format(i))
    conf_path = os.path.abspath(netw_conf_fpath)
    nc.run(conf_path)
Ejemplo n.º 13
0
__author__ = 'Agostino Sturaro'

import os
import netw_creator as nc
import shared_functions as sf

# This is a simple file to run custom tests

this_dir = os.path.normpath(os.path.dirname(__file__))
os.chdir(this_dir)

logging_conf_fpath = os.path.normpath('logging_base_conf.json')
netw_conf_fpath = os.path.normpath('../Simulations/test_2/example_gen_2.ini')

sf.setup_logging(logging_conf_fpath)
conf_path = os.path.abspath(netw_conf_fpath)
nc.run(conf_path)
Ejemplo n.º 14
0
    config.add_section('build_inter')
    for opt_name in inter_options:
        config.set('build_inter', opt_name, inter_options[opt_name])

    config.add_section('misc')
    for opt_name in misc_options:
        config.set('misc', opt_name, misc_options[opt_name])

    with open(conf_fpath, 'w') as configfile:
        config.write(configfile)


this_dir = os.path.normpath(os.path.dirname(__file__))
os.chdir(this_dir)
sf.setup_logging('logging_base_conf.json')
logger = logging.getLogger(__name__)
# base_dir = os.path.normpath('../Simulations/test_mp/1cc_1ap')
base_dir = os.path.normpath('../Simulations/test_mp/2cc_2ap')

build_a_options = [{
    'name': 'A',
    'model': 'rt_nested_smallworld',
    'nodes': 1000,
    'subnets': 20,
    'beta': 0.2,
    'alpha': 0.2,
    'd_0': 7,
    'avg_k': 4,
    'q_rw': 0.5,
    'roles': 'subnet_gen_transm_distr',
Ejemplo n.º 15
0
import os
import csv
import logging
import numpy as np
import shared_functions as sf

__author__ = "Agostino Sturaro"

this_dir = os.path.normpath(os.path.dirname(__file__))
os.chdir(this_dir)
sf.setup_logging("logging_base_conf.json")
logger = logging.getLogger(__name__)


def run(index_fpath, aggregate_fpath, instance_type_names=None, cols_to_ignore=None):
    with open(index_fpath, "r") as index_file, open(aggregate_fpath, "wb") as aggregate_file:
        first_it = True
        # the index file contains the paths of the files with the actual statistics
        # open the index file as a list of dictionaries (kinda, each file line can be accessed as a dictionary)
        index = csv.DictReader(index_file, delimiter="\t", quoting=csv.QUOTE_MINIMAL)

        for line in index:
            stats_fpath = line["Results_file"]
            # using NumPy, open a statistics file as a dictionary of vectors (kinda, it's indexed by column)
            # NumPy guesses the type of the cells
            stats = np.genfromtxt(stats_fpath, delimiter="\t", names=True, dtype=None)
            stat_names = stats.dtype.names
            avgs = dict()
            std_devs = dict()

            # assuming all stats files have the same header