def test_add_to_hierarchy(): # Not mocked: _relate_node_to_others mapp = MapGraph() # Add to an empty hierarchy. hierarchy = mapp._add_to_hierarchy('A-1', {}) nt.assert_equal(hierarchy, {'A-1': {}}) # RC = D. hierarchy = mapp._add_to_hierarchy('A-2', hierarchy) nt.assert_equal(hierarchy, {'A-1': {}, 'A-2': {}}) # RC = L. mapp.add_edges_from([('A-3', 'A-1', { 'RC': 'L' }), ('A-3', 'A-2', { 'RC': 'L' })]) hierarchy = mapp._add_to_hierarchy('A-3', hierarchy) nt.assert_equal(hierarchy, {'A-3': {'A-1': {}, 'A-2': {}}}) # RC = S for the first round, then RC = L. mapp.add_edges_from([('A-4', 'A-3', { 'RC': 'S' }), ('A-4', 'A-1', { 'RC': 'L' })]) hierarchy = mapp._add_to_hierarchy('A-4', hierarchy) nt.assert_equal(hierarchy, {'A-3': {'A-4': {'A-1': {}}, 'A-2': {}}})
def setUp(self): m = MapGraph() m.cong = DiGraph() m.add_edges_from([('A00-1', 'B00-1', { 'RC': 'L', 'PDC': 0 }), ('A00-2', 'B00-2', { 'RC': 'I', 'PDC': 0 }), ('A00-3', 'A00-1', { 'RC': 'L', 'PDC': 0 }), ('A00-3', 'A00-2', { 'RC': 'L', 'PDC': 0 }), ('A00-3', 'B00-3', { 'RC': 'I', 'PDC': 0 })]) m.cong.add_edges_from([('A00-1', 'C00-1', { 'Connection': 'Present' }), ('C00-2', 'A00-2', { 'Connection': 'Present' }), ('A00-3', 'C00-3', { 'Connection': 'Absent' }), ('C00-4', 'A00-3', { 'Connection': 'Absent' })]) self.m = m
def test_find_partial_coverage(): mapp = MapGraph() mapp.add_edges_from([('A-1', 'B-1', {'RC': 'S'}), ('B-1', 'A-1', {'RC': 'L'}), ('C-1', 'B-1', {'RC': 'I'}), ('B-1', 'C-1', {'RC': 'I'}), ('D-1', 'B-1', {'RC': 'S'}), ('B-1', 'D-1', {'RC': 'L'}), ('D-2', 'B-1', {'RC': 'O'}), ('B-1', 'D-2', {'RC': 'O'})]) # A has partial coverage of B, and B has partial coverage of D. nt.assert_equal(MapGraph.find_partial_coverage.im_func(mapp), [('B-1', 'D-2'), ('A-1', 'B-1')])
def setUp(self): m = MapGraph() m.cong = DiGraph() m.add_edges_from([('A00-1', 'B00-1', {'RC': 'L', 'PDC': 0}), ('A00-2', 'B00-2', {'RC': 'I', 'PDC': 0}), ('A00-3', 'A00-1', {'RC': 'L', 'PDC': 0}), ('A00-3', 'A00-2', {'RC': 'L', 'PDC': 0}), ('A00-3', 'B00-3', {'RC': 'I', 'PDC': 0})]) m.cong.add_edges_from([('A00-1', 'C00-1', {'Connection': 'Present'}), ('C00-2', 'A00-2', {'Connection': 'Present'}), ('A00-3', 'C00-3', {'Connection': 'Absent'}), ('C00-4', 'A00-3', {'Connection': 'Absent'})]) self.m = m
def test_merge_identical_nodes(): mock_conn = DiGraph() mock_conn.add_edges_from([('A-1', 'A-5'), ('A-4', 'A-1')]) mapp = MapGraph() # Here we aren't adding the reciprocals, because add_edges_from # has been mocked. And _merge_identical_nodes is designed only to # get a node's neighbors (i.e., its successors), assuming that # these are the same as its predecessors. mapp.add_edges_from([('A-1', 'A-3', { 'RC': 'S', 'PDC': 5 }), ('A-1', 'B-1', { 'RC': 'I', 'PDC': 7 }), ('A-1', 'C-1', { 'RC': 'L', 'PDC': 10 }), ('A-1', 'A-2', { 'RC': 'I', 'PDC': 12 })]) mapp.cong = mock_conn mapp._merge_identical_nodes('A-2', 'A-1') nt.assert_equal(mapp.cong.edges(), [('A-2', 'A-5'), ('A-4', 'A-2')]) nt.assert_equal(mapp.edges(), [('A-2', 'B-1'), ('A-2', 'C-1')])
def test_keep_one_level(): # Not mocked: _find_bottom_of_hierarchy, # _remove_level_from_hierarchy, _summate_connections. hierarchy = { 'A-J': { 'A-A': {} }, 'A-B': { 'A-I': { 'A-F': { 'A-K': {}, 'A-L': {} }, 'A-H': {} }, 'A-D': { 'A-E': {} } } } mock_conn = DiGraph() mock_conn.add_edges_from([('A-D', 'A-Z'), ('A-Y', 'A-F'), ('A-J', 'A-X')]) mapp = MapGraph() mapp.add_nodes_from( ['A-A', 'A-B', 'A-D', 'A-E', 'A-F', 'A-H', 'A-I', 'A-J', 'A-K', 'A-L']) mapp.cong = mock_conn mapp._keep_one_level(hierarchy, 'A') nt.assert_equal(mapp.cong.edges(), [('A-J', 'A-X'), ('A-D', 'A-Z'), ('A-Y', 'A-F')]) nt.assert_equal(mapp.nodes(), ['A-H', 'A-J', 'A-D', 'A-F'])
def test_find_bottom_of_hierarchy(): # The recursion in _find_bottom_of_hierarchy requires that we use an # actual MapGraph in this test. mock_mapp = MapGraph() hierarchy = {'J': {'A': {}}} path, bottom = mock_mapp._find_bottom_of_hierarchy(hierarchy, []) nt.assert_equal(path, []) nt.assert_equal(bottom, ['J', ['A']]) hierarchy = {'B': {'I': {'F': {'K': {}, 'L': {}}, 'H': {}}}} path, bottom = mock_mapp._find_bottom_of_hierarchy(hierarchy, []) nt.assert_equal(path, ['B', 'I']) nt.assert_equal(bottom, ['F', ['K', 'L']]) # Test what it returns when all regions are at the same level, the # goal state. hierarchy = {'A': {}, 'B': {}} path, bottom = mock_mapp._find_bottom_of_hierarchy(hierarchy, []) nt.assert_equal(path, []) nt.assert_equal(bottom, [])
def test_find_partial_coverage(): mapp = MapGraph() mapp.add_edges_from([('A-1', 'B-1', { 'RC': 'S' }), ('B-1', 'A-1', { 'RC': 'L' }), ('C-1', 'B-1', { 'RC': 'I' }), ('B-1', 'C-1', { 'RC': 'I' }), ('D-1', 'B-1', { 'RC': 'S' }), ('B-1', 'D-1', { 'RC': 'L' }), ('D-2', 'B-1', { 'RC': 'O' }), ('B-1', 'D-2', { 'RC': 'O' })]) # A has partial coverage of B, and B has partial coverage of D. nt.assert_equal(MapGraph.find_partial_coverage.im_func(mapp), [('B-1', 'D-2'), ('A-1', 'B-1')])
def test_add_to_hierarchy(): # Not mocked: _relate_node_to_others mapp = MapGraph() # Add to an empty hierarchy. hierarchy = mapp._add_to_hierarchy('A-1', {}) nt.assert_equal(hierarchy, {'A-1': {}}) # RC = D. hierarchy = mapp._add_to_hierarchy('A-2', hierarchy) nt.assert_equal(hierarchy, {'A-1': {}, 'A-2': {}}) # RC = L. mapp.add_edges_from([('A-3', 'A-1', {'RC': 'L'}), ('A-3', 'A-2', {'RC': 'L'})]) hierarchy = mapp._add_to_hierarchy('A-3', hierarchy) nt.assert_equal(hierarchy, {'A-3': {'A-1': {}, 'A-2': {}}}) # RC = S for the first round, then RC = L. mapp.add_edges_from([('A-4', 'A-3', {'RC': 'S'}), ('A-4', 'A-1', {'RC': 'L'})]) hierarchy = mapp._add_to_hierarchy('A-4', hierarchy) nt.assert_equal(hierarchy, {'A-3': {'A-4': {'A-1': {}}, 'A-2': {}}})
def test_determine_hierarchies(): intramap_nodes = set(['A-1', 'A-2', 'B-1', 'C-1']) mapp = MapGraph() nt.assert_equal( MapGraph._determine_hierarchies.im_func(mapp, intramap_nodes), { 'A': { 'A-1': {}, 'A-2': {} }, 'B': { 'B-1': {} }, 'C': { 'C-1': {} } })
def test_merge_identical_nodes(): mock_conn = DiGraph() mock_conn.add_edges_from([('A-1', 'A-5'), ('A-4', 'A-1')]) mapp = MapGraph() # Here we aren't adding the reciprocals, because add_edges_from # has been mocked. And _merge_identical_nodes is designed only to # get a node's neighbors (i.e., its successors), assuming that # these are the same as its predecessors. mapp.add_edges_from([('A-1', 'A-3', {'RC': 'S', 'PDC': 5}), ('A-1', 'B-1', {'RC': 'I', 'PDC': 7}), ('A-1', 'C-1', {'RC': 'L', 'PDC': 10}), ('A-1', 'A-2', {'RC': 'I', 'PDC': 12})]) mapp.cong = mock_conn mapp._merge_identical_nodes('A-2', 'A-1') nt.assert_equal(mapp.cong.edges(), [('A-2', 'A-5'), ('A-4', 'A-2')]) nt.assert_equal(mapp.edges(), [('A-2', 'B-1'), ('A-2', 'C-1')])
def test_edge_removal(): # Test for remove_edge and remove_edges_from. mock_mapp = MapGraph() mock_mapp.add_edges_from([('A-1', 'B-1', {'TP': []}), ('B-1', 'A-1', {'TP': []}), ('C-1', 'D-1', {'TP': ['E-1', 'A-1', 'B-1', 'I-1']}), ('D-1', 'C-1', {'TP': ['I-1', 'B-1', 'A-1', 'E-1']}), ('E-1', 'F-1', {'TP': ['D-1', 'C-1', 'G-1']}), ('F-1', 'E-1', {'TP': ['G-1', 'C-1', 'D-1']}), ('G-1', 'H-1', {'TP': []})]) mock_mapp.remove_edges_from([('A-1', 'B-1')]) nt.assert_equal(mock_mapp.edges(), [('G-1', 'H-1')])
def test_keep_one_level(): # Not mocked: _find_bottom_of_hierarchy, # _remove_level_from_hierarchy, _summate_connections. hierarchy = {'A-J': {'A-A': {}}, 'A-B': {'A-I': {'A-F': {'A-K': {}, 'A-L': {}}, 'A-H': {}}, 'A-D': {'A-E': {}}}} mock_conn = DiGraph() mock_conn.add_edges_from([('A-D', 'A-Z'), ('A-Y', 'A-F'), ('A-J', 'A-X')]) mapp = MapGraph() mapp.add_nodes_from(['A-A', 'A-B', 'A-D', 'A-E', 'A-F', 'A-H', 'A-I', 'A-J', 'A-K', 'A-L']) mapp.cong = mock_conn mapp._keep_one_level(hierarchy, 'A') nt.assert_equal(mapp.cong.edges(), [('A-J', 'A-X'), ('A-D', 'A-Z'), ('A-Y', 'A-F')]) nt.assert_equal(mapp.nodes(), ['A-H', 'A-J', 'A-D', 'A-F'])
def test_edge_removal(): # Test for remove_edge and remove_edges_from. mock_mapp = MapGraph() mock_mapp.add_edges_from([('A-1', 'B-1', { 'TP': [] }), ('B-1', 'A-1', { 'TP': [] }), ('C-1', 'D-1', { 'TP': ['E-1', 'A-1', 'B-1', 'I-1'] }), ('D-1', 'C-1', { 'TP': ['I-1', 'B-1', 'A-1', 'E-1'] }), ('E-1', 'F-1', { 'TP': ['D-1', 'C-1', 'G-1'] }), ('F-1', 'E-1', { 'TP': ['G-1', 'C-1', 'D-1'] }), ('G-1', 'H-1', { 'TP': [] })]) mock_mapp.remove_edges_from([('A-1', 'B-1')]) nt.assert_equal(mock_mapp.edges(), [('G-1', 'H-1')])
def test_clean_data(): mapg = MapGraph() edges = [] mapg.add_edges_from(edges) nt.assert_false(mapg.clean_data())