コード例 #1
0
ファイル: test_initiate.py プロジェクト: lawilby/infSim
def test_lambda_value_degree():

    from infSim.initiate import lambda_value_degree
    from infSim.settings import make_settings_file
    from infSim.db import create_db

    ## TEST SETUP ##

    directory_name = os.getcwd()
    params = dict()
    make_settings_file(directory_name, directory_name, params)

    settings_config = configparser.ConfigParser()
    settings_config.read(directory_name + '/settings.ini')
    conn = sqlite3.connect(settings_config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    create_db(conn)

    dummy_nodes = list()
    dummy_nodes.append((1, 5, 0, 0))
    dummy_nodes.append((2, 5, 0, 0))
    dummy_nodes.append((3, 4, 0, 0))
    dummy_nodes.append((4, 1, 0, 0))

    dummy_edges = list()
    dummy_edges.append((1, 2))
    dummy_edges.append((2, 1))
    dummy_edges.append((1, 3))
    dummy_edges.append((3, 1))

    lambda_value_degree(settings_config, conn)

    updated_nodes_query = conn.execute('SELECT * FROM nodes')

    for node in updated_nodes_query:

        if node['nodeID'] == 1:

            assert node['lambda'] == 2

        if node['nodeID'] == 2:

            assert node['lambda'] == 1

        if node['nodeID'] == 3:

            assert node['lambda'] == 1

        if node['nodeID'] == 4:

            assert node['lambda'] == 0

    conn.close()
    os.remove(settings_config['FILES']['DB'])
    os.remove('settings.ini')
コード例 #2
0
ファイル: test_initiate.py プロジェクト: lawilby/infSim
def test_thresholds_proportion():

    from infSim.initiate import thresholds_proportion
    from infSim.settings import make_settings_file
    from infSim.db import create_db

    ## TEST SETUP ##

    directory_name = os.getcwd()
    # Paramaters except threshold proportion don't matter because test doesn't use them
    params = dict(thresh_prop=0.5)
    make_settings_file(directory_name, directory_name, params)

    config = configparser.ConfigParser()
    config.read(directory_name + '/settings.ini')
    conn = sqlite3.connect(config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    create_db(conn)

    dummy_nodes = [(i + 1, 0, 0, 0) for i in range(4)]
    '''
    Test: Min threshold is 1
    Test: Non-integer set to ceiling
    Test: All threshs are set
    Test: Integer is set to integer  
    '''
    dummy_edges = ([(1, 2), (2, 1), (2, 3), (2, 4), (3, 2), (3, 4), (4, 2),
                    (4, 3)])

    conn.executemany('INSERT INTO nodes VALUES (?, ?, ?, ?)', dummy_nodes)
    conn.executemany('INSERT INTO edges VALUES (?, ?)', dummy_edges)

    conn.commit()

    thresholds_proportion(config, conn)

    updated_nodes = conn.execute('SELECT threshold, nodeID FROM nodes')

    row_count = 0
    for row in updated_nodes:

        if row['nodeID'] == 1:

            assert row['threshold'] == 1

        if row['nodeID'] == 2:

            assert row['threshold'] == 2

        if row['nodeID'] == 3:

            assert row['threshold'] == 1

        if row['nodeID'] == 4:

            assert row['threshold'] == 1

        row_count += 1

    assert row_count == 4

    conn.close()
    os.remove(config['FILES']['DB'])
    os.remove('settings.ini')
コード例 #3
0
ファイル: test_initiate.py プロジェクト: lawilby/infSim
def test_incentivize_partial():

    from infSim.settings import make_settings_file
    from infSim.db import create_db
    from infSim.initiate import incentivize

    ## TEST SETUP ##

    directory_name = os.getcwd()
    params = dict(thresh_prop=1, budget=3, incentive_prop=.5)
    make_settings_file(directory_name, directory_name, params)

    settings_config = configparser.ConfigParser()
    results_config = configparser.ConfigParser()
    settings_config.read(directory_name + '/settings.ini')
    results_config['RESULTS'] = dict()
    conn = sqlite3.connect(settings_config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    create_db(conn)

    dummy_nodes = list()
    dummy_nodes.append((1, 5, 0, 0))
    dummy_nodes.append((2, 5, 0, 0))
    dummy_nodes.append((3, 1, 0, 0))
    dummy_nodes.append((4, 1, 0, 0))

    dummy_edges = list()
    dummy_edges.append((1, 2))
    dummy_edges.append((1, 3))
    dummy_edges.append((1, 4))
    dummy_edges.append((2, 1))
    dummy_edges.append((2, 3))
    dummy_edges.append((3, 1))
    dummy_edges.append((3, 2))
    dummy_edges.append((4, 1))

    conn.executemany('INSERT INTO nodes VALUES (?, ?, ?, ?)', dummy_nodes)
    conn.executemany('INSERT INTO edges VALUES (?,?)', dummy_edges)

    conn.commit()

    incentivize(settings_config, results_config, conn)

    nodes_influenced = conn.execute('SELECT * FROM nodes WHERE inf=1')

    influenced_ids = list()
    for node in nodes_influenced:

        assert node['threshold'] == 0
        influenced_ids.append(node['nodeID'])

    assert len(influenced_ids) == 2

    nodes_activated = conn.execute('SELECT * FROM activeNodes')

    activeNodes = 0
    for activeNode in nodes_activated:

        assert activeNode['round'] == 0
        assert activeNode['nodeID'] in influenced_ids

        if activeNode['nodeID'] == 1:

            assert activeNode['power'] == 3.0

        if activeNode['nodeID'] == 2 or activeNode['nodeID'] == 3:

            assert activeNode['power'] == 2.0

        if activeNode['nodeID'] == 4:

            assert activeNode['power'] == 1.0

        activeNodes = activeNodes + 1

    assert activeNodes == len(influenced_ids)

    conn.close()
    os.remove(settings_config['FILES']['DB'])
    os.remove('settings.ini')
コード例 #4
0
def test_run_sim_decay_lambda_2():

    from infSim.sim import run_sim
    from infSim.settings import make_settings_file
    from infSim.db import create_db
    '''
    In this test the dummy data from lambda two test above is the same as above and now node 4 will not meet the threshold value because of decay on 1 and 3. 
    '''

    ## DUMMY DATA ##

    dummy_nodes = list()
    dummy_nodes.append((1, 0, 2, 1))
    dummy_nodes.append((2, 2, 2, 0))
    dummy_nodes.append((3, 2, 2, 0))
    dummy_nodes.append((4, 7, 2, 0))
    dummy_nodes.append((5, 5, 2, 0))
    dummy_nodes.append((6, 1, 2, 0))
    dummy_nodes.append((7, 1, 2, 0))

    dummy_edges = list()
    dummy_edges.append((1, 2))
    dummy_edges.append((1, 3))
    dummy_edges.append((1, 4))
    dummy_edges.append((2, 1))
    dummy_edges.append((2, 4))
    dummy_edges.append((2, 6))
    dummy_edges.append((3, 1))
    dummy_edges.append((3, 4))
    dummy_edges.append((4, 1))
    dummy_edges.append((4, 2))
    dummy_edges.append((4, 3))
    dummy_edges.append((4, 5))
    dummy_edges.append((5, 4))
    dummy_edges.append((6, 2))
    dummy_edges.append((6, 7))
    dummy_edges.append((7, 6))

    dummy_active = list()
    dummy_active.append((1, 0, 3))

    directory_name = os.getcwd()

    params = dict(lambda_val=2, decay=1)
    make_settings_file(directory_name, directory_name, params)

    config = configparser.ConfigParser()
    config.read(directory_name + '/settings.ini')
    conn = sqlite3.connect(config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    create_db(conn)

    conn.executemany('INSERT INTO nodes VALUES (?, ?, ?, ?)', dummy_nodes)
    conn.executemany('INSERT INTO edges VALUES (?, ?)', dummy_edges)
    conn.executemany('INSERT INTO activeNodes VALUES (?, ?, ?)', dummy_active)

    conn.commit()

    run_sim(config, conn)

    influenced_nodes = conn.execute('SELECT nodeID FROM nodes WHERE inf=1')

    influenced_count = 0
    for node in influenced_nodes:

        if node['nodeID'] == 4:

            pytest.fail('Node 4 Should Not Be Influenced')

        if node['nodeID'] == 5:

            pytest.fail('Node 5 Should Not Be Influenced')

        influenced_count += 1

    assert influenced_count == 5

    active_nodes = conn.execute('SELECT * FROM activeNodes')

    active_count = 0
    for node in active_nodes:

        if node['nodeID'] == 4:

            pytest.fail('Node 4 Should Not Have Been Activated')

        if node['nodeID'] == 5:

            pytest.fail('Node 5 Should Not Have Been Activated')

        if node['nodeID'] == 1:

            if node['round'] != 0 and node['round'] != 1:

                pytest.fail(
                    'Node 1 Should Only Be Active First and Second Round')

        if node['nodeID'] == 3:

            if node['round'] != 1 and node['round'] != 2:

                pytest.fail(
                    'Node 3 Should Only Be Active Second and Third Round')

        if node['nodeID'] == 2:

            if node['round'] != 1 and node['round'] != 2:

                pytest.fail(
                    'Node 2 Should Only Be Active Second and Third Round')

        if node['nodeID'] == 6:

            if node['round'] != 2 and node['round'] != 3:

                pytest.fail(
                    'Node 2 Should Only Be Active Second and Third Round')

        if node['nodeID'] == 7:

            if node['round'] != 3 and node['round'] != 4:

                pytest.fail(
                    'Node 2 Should Only Be Active Second and Third Round')

        active_count += 1

    assert active_count == 10

    conn.close()
    os.remove(config['FILES']['DB'])
    os.remove('settings.ini')
    os.remove('simulation-details.csv')
コード例 #5
0
def test_run_sim_lambda_degree():

    from infSim.sim import run_sim
    from infSim.settings import make_settings_file
    from infSim.db import create_db
    from infSim.initiate import lambda_value_degree

    ## DUMMY DATA ##

    dummy_nodes = list()
    dummy_nodes.append((1, 0, 4, 1))
    dummy_nodes.append((2, 4, 5, 0))
    dummy_nodes.append((3, 1, 3, 0))
    dummy_nodes.append((4, 1, 2, 0))
    dummy_nodes.append((5, 1, 3, 0))
    dummy_nodes.append((6, 2, 2, 0))
    dummy_nodes.append((7, 1, 2, 0))
    dummy_nodes.append((8, 2, 2, 0))

    dummy_edges = list()
    dummy_edges.append((1, 2))
    dummy_edges.append((1, 3))
    dummy_edges.append((1, 7))
    dummy_edges.append((1, 8))
    dummy_edges.append((2, 1))
    dummy_edges.append((2, 3))
    dummy_edges.append((2, 4))
    dummy_edges.append((2, 5))
    dummy_edges.append((2, 8))
    dummy_edges.append((3, 1))
    dummy_edges.append((3, 2))
    dummy_edges.append((3, 4))
    dummy_edges.append((4, 2))
    dummy_edges.append((4, 3))
    dummy_edges.append((4, 5))
    dummy_edges.append((5, 2))
    dummy_edges.append((5, 4))
    dummy_edges.append((5, 6))
    dummy_edges.append((6, 5))
    dummy_edges.append((6, 7))
    dummy_edges.append((7, 1))
    dummy_edges.append((7, 6))
    dummy_edges.append((8, 1))
    dummy_edges.append((8, 2))

    dummy_active = list()
    dummy_active.append((1, 0, 1))

    directory_name = os.getcwd()

    params = dict(lambda_val=2, decay=0)
    make_settings_file(directory_name, directory_name, params)

    config = configparser.ConfigParser()
    config.read(directory_name + '/settings.ini')
    conn = sqlite3.connect(config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    create_db(conn)

    conn.executemany('INSERT INTO nodes VALUES (?, ?, ?, ?)', dummy_nodes)
    conn.executemany('INSERT INTO edges VALUES (?, ?)', dummy_edges)
    conn.executemany('INSERT INTO activeNodes VALUES (?, ?, ?)', dummy_active)

    conn.commit()

    run_sim(config, conn)

    influenced_nodes = conn.execute('SELECT nodeID FROM nodes WHERE inf=1')

    influenced_count = 0
    for node in influenced_nodes:

        if node['nodeID'] == 6:

            pytest.fail('Node 6 Should Not Be Influenced')

        if node['nodeID'] == 8:

            pytest.fail('Node 8 Should Not Be Influenced')

        influenced_count += 1

    assert influenced_count == 6

    active_nodes = conn.execute('SELECT * FROM activeNodes')

    active_count = 0
    for node in active_nodes:

        if node['nodeID'] == 6:

            pytest.fail('Node 6 Should Not Have Been Activated')

        if node['nodeID'] == 8:

            pytest.fail('Node 8 Should Not Have Been Activated')

        if node['nodeID'] == 4:

            if node['round'] != 2 and node['round'] != 3:

                pytest.fail(
                    'Node 4 Should Only Be Activated Third, Fourth Rounds')

        if node['nodeID'] == 1:

            if node['round'] != 0 and node['round'] != 1 and node[
                    'round'] != 2 and node['round'] != 3:

                pytest.fail(
                    'Node 1 Should Only Be Active First, Second, Third, Fourth Round'
                )

        if node['nodeID'] == 3:

            if node['round'] != 1 and node['round'] != 2 and node['round'] != 3:

                pytest.fail(
                    'Node 3 Should Only Be Active Second, Third, Fourth Round')

        if node['nodeID'] == 2:

            if node['round'] != 4 and node['round'] != 5:

                pytest.fail('Node 2 Should Only Be Active Rounds 5, 6')

        if node['nodeID'] == 7:

            if node['round'] != 1 and node['round'] != 2:

                pytest.fail(
                    'Node 7 Should Only Be Active Second, Third Rounds')

        if node['nodeID'] == 5:

            if node['round'] != 3 and node['round'] != 4 and node['round'] != 5:

                pytest.fail(
                    'Node 5 Should Only Be Active Second, Third, Fourth Round')

        active_count += 1

    assert active_count == 16  # There is no round 6

    conn.close()
    os.remove(config['FILES']['DB'])
    os.remove('settings.ini')
    os.remove('simulation-details.csv')
コード例 #6
0
def test_run_sim():

    from infSim.sim import run_sim
    from infSim.settings import make_settings_file
    from infSim.db import create_db

    ## DUMMY DATA ##

    dummy_nodes = list()
    dummy_nodes.append((1, 0, 1, 1))
    dummy_nodes.append((2, 2, 1, 0))
    dummy_nodes.append((3, 0, 1, 1))
    dummy_nodes.append((4, 3, 1, 0))
    dummy_nodes.append((5, 2, 1, 0))
    dummy_nodes.append((6, 2, 1, 0))

    dummy_edges = list()
    dummy_edges.append((1, 2))
    dummy_edges.append((1, 4))
    dummy_edges.append((1, 5))
    dummy_edges.append((2, 1))
    dummy_edges.append((2, 3))
    dummy_edges.append((2, 4))
    dummy_edges.append((2, 6))
    dummy_edges.append((3, 2))
    dummy_edges.append((3, 4))
    dummy_edges.append((4, 1))
    dummy_edges.append((4, 2))
    dummy_edges.append((4, 3))
    dummy_edges.append((5, 1))
    dummy_edges.append((5, 6))
    dummy_edges.append((6, 2))
    dummy_edges.append((6, 5))

    dummy_active = list()
    dummy_active.append((1, 0, 1))
    dummy_active.append((3, 0, 1))

    ## TEST SETUP - Lambda One ##
    directory_name = os.getcwd()

    params = dict(lambda_val=1)
    make_settings_file(directory_name, directory_name, params)

    config = configparser.ConfigParser()
    config.read(directory_name + '/settings.ini')
    conn = sqlite3.connect(config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    create_db(conn)

    conn.executemany('INSERT INTO nodes VALUES (?, ?, ?, ?)', dummy_nodes)
    conn.executemany('INSERT INTO edges VALUES (?, ?)', dummy_edges)
    conn.executemany('INSERT INTO activeNodes VALUES (?, ?, ?)', dummy_active)

    conn.commit()

    run_sim(config, conn)

    influenced_nodes = conn.execute('SELECT nodeID FROM nodes WHERE inf=1')

    influenced_count = 0
    for node in influenced_nodes:

        if node['nodeID'] == 5:

            pytest.fail('Node 5 Should Not Be Influenced')

        if node['nodeID'] == 6:

            pytest.fail('Node 6 Should Not Be Influenced')

        if node['nodeID'] == 4:

            pytest.fail('Node 4 Should Only Be Influenced When Lambda > 1')

        influenced_count += 1

    assert influenced_count == 3

    active_nodes = conn.execute('SELECT * FROM activeNodes')

    active_count = 0
    for node in active_nodes:

        if node['nodeID'] == 5:

            pytest.fail('Node 5 Should Not Have Been Activated')

        if node['nodeID'] == 6:

            pytest.fail('Node 6 Should Not Have Been Activated')

        if node['nodeID'] == 4:

            pytest.fail('Node 4 Should Only Be Activated When Lambda > 1')

        if node['nodeID'] == 1:

            if node['round'] != 0:

                pytest.fail('Node 1 Should Only Be Active First Round')

        if node['nodeID'] == 3:

            if node['round'] != 0:

                pytest.fail('Node 3 Should Only Be Active First Round')

        if node['nodeID'] == 2:

            if node['round'] != 1:

                pytest.fail('Node 2 Should Only Be Active Second Round')

        active_count += 1

    assert active_count == 3

    conn.close()
    os.remove(config['FILES']['DB'])
    os.remove('settings.ini')

    ## TEST SETUP - Lambda Two ##
    directory_name = os.getcwd()

    params = dict(lambda_val=2)
    make_settings_file(directory_name, directory_name, params)

    config = configparser.ConfigParser()
    config.read(directory_name + '/settings.ini')
    conn = sqlite3.connect(config['FILES']['DB'])
    conn.row_factory = sqlite3.Row

    dummy_nodes = list()
    dummy_nodes.append((1, 0, 2, 1))
    dummy_nodes.append((2, 2, 2, 0))
    dummy_nodes.append((3, 0, 2, 1))
    dummy_nodes.append((4, 3, 2, 0))
    dummy_nodes.append((5, 2, 2, 0))
    dummy_nodes.append((6, 2, 2, 0))

    create_db(conn)

    conn.executemany('INSERT INTO nodes VALUES (?, ?, ?, ?)', dummy_nodes)
    conn.executemany('INSERT INTO edges VALUES (?, ?)', dummy_edges)
    conn.executemany('INSERT INTO activeNodes VALUES (?, ?, ?)', dummy_active)

    conn.commit()

    run_sim(config, conn)

    influenced_nodes = conn.execute('SELECT nodeID FROM nodes WHERE inf=1')

    influenced_count = 0
    for node in influenced_nodes:

        if node['nodeID'] == 5:

            pytest.fail('Node 5 Should Not Be Influenced')

        if node['nodeID'] == 6:

            pytest.fail('Node 6 Should Not Be Influenced')

        influenced_count += 1

    assert influenced_count == 4

    active_nodes = conn.execute('SELECT * FROM activeNodes')

    active_count = 0
    for node in active_nodes:

        if node['nodeID'] == 5:

            pytest.fail('Node 5 Should Not Have Been Activated')

        if node['nodeID'] == 6:

            pytest.fail('Node 6 Should Not Have Been Activated')

        if node['nodeID'] == 4:

            if node['round'] != 2 and node['round'] != 3:

                pytest.fail(
                    'Node 4 Should Only Be Activated Third and Fourth Round')

        if node['nodeID'] == 1:

            if node['round'] != 0 and node['round'] != 1:

                pytest.fail(
                    'Node 1 Should Only Be Active First and Second Round')

        if node['nodeID'] == 3:

            if node['round'] != 0 and node['round'] != 1:

                pytest.fail(
                    'Node 3 Should Only Be Active First and Second Round')

        if node['nodeID'] == 2:

            if node['round'] != 1 and node['round'] != 2:

                pytest.fail(
                    'Node 2 Should Only Be Active Second and Third Round')

        active_count += 1

    assert active_count == 8

    conn.close()
    os.remove(config['FILES']['DB'])
    os.remove('settings.ini')
    os.remove('simulation-details.csv')