def test_extract_graph_links_with_flat_condition(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) links = graph_operations.extract_on_attributes( n.links(), conditions={'attributes': 'yes'}, ) assert links == ['2']
def test_extract_graph_links_with_flat_condition_and_list_value_specifying_to_ignore_mixed_types( ): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('2', 3, 4, attribs={'attributes': ['yes', 'no', 'bobby']}) links = graph_operations.extract_on_attributes( n.links(), conditions={'attributes': 'yes'}, mixed_dtypes=False) assert links == []
def test_extract_graph_links_with_callable_condition_and_list_value_specifying_to_ignore_mixed_types( ): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': {9, 10} } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': [1, 2] } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) def condition(val): return val == 9 links = graph_operations.extract_on_attributes( n.links(), conditions={'attributes': { 'osm:way:highway': { 'text': condition } }}, mixed_dtypes=False) assert links == []
def test_extract_graph_links_with_callable_condition(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 9 } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 1 } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) def condition(val): return val == 9 links = graph_operations.extract_on_attributes( n.links(), conditions={'attributes': { 'osm:way:highway': { 'text': condition } }}) assert links == ['0']
def test_extract_graph_links_with_list_condition_and_list_value(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': ['primary', 'secondary'] } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': {'primary', 'other'} } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) links = graph_operations.extract_on_attributes( n.links(), conditions={ 'attributes': { 'osm:way:highway': { 'text': ['primary', 'some_other_highway'] } } }) assert links == ['0', '1']
def test_extract_graph_links_with_bound_condition_and_list_value(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': [9, 1] } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': [0, 1] } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) links = graph_operations.extract_on_attributes( n.links(), conditions={'attributes': { 'osm:way:highway': { 'text': (2, 10) } }}) assert links == ['0']
def generate_problem_graph(self): # build the problem graph # build up the nodes, edges and shortest path lengths (will be used to compute weight coefficient later on) self.nodes.loc[:, 'problem_nodes'] = self.nodes.loc[:, 'id'] + '.link:' + self.nodes.loc[:, 'link_id'] self.edges = self.edges.merge( self.nodes[['id', 'link_id', 'problem_nodes' ]].rename(columns={ 'link_id': 'link_id_u', 'problem_nodes': 'problem_nodes_u' }), left_on='u', right_on='id') self.edges = self.edges.merge( self.nodes[['id', 'link_id', 'problem_nodes' ]].rename(columns={ 'link_id': 'link_id_v', 'problem_nodes': 'problem_nodes_v' }), left_on='v', right_on='id') self.edges = self.network_spatial_tree.shortest_path_lengths( df_pt_edges=self.edges, from_col='link_id_u', to_col='link_id_v', weight='length') # build the problem graph problem_graph = nx.DiGraph() problem_nodes = self.nodes.set_index('problem_nodes').T.to_dict() problem_graph.add_nodes_from(problem_nodes) path_length_coeff = self.edges.loc[self.edges['path_lengths'].notna( ), :].groupby('problem_nodes_u').mean() path_length_coeff = path_length_coeff.rename( columns={ 'path_lengths': 'path_lengths_u' }).merge( self.edges.loc[self.edges['path_lengths'].notna(), :].rename( columns={ 'path_lengths': 'path_lengths_v' }).groupby('problem_nodes_v').mean(), how='outer', left_index=True, right_index=True) path_length_coeff = ( 1 / path_length_coeff[['path_lengths_u', 'path_lengths_v']].mean( axis=1, skipna=True)).to_dict() nx.set_node_attributes(problem_graph, problem_nodes) nx.set_node_attributes(problem_graph, path_length_coeff, 'coeff') problem_edges = self.edges.loc[self.edges['path_lengths'].isna(), :] problem_graph.add_edges_from( zip(problem_edges['problem_nodes_u'], problem_edges['problem_nodes_v'])) # connect all catchment pools [ problem_graph.add_edges_from( itertools.combinations(group['problem_nodes'], 2)) for name, group in self.nodes.groupby('id') ] nodes_without_paths = set(problem_graph.nodes()) - set( graph_operations.extract_on_attributes( problem_graph.nodes(data=True), conditions={'coeff': has_attrib})) problem_graph.remove_nodes_from(nodes_without_paths) return problem_graph