Process directed graphs: nctx.directed¶
This submodule provides functionality for directed graphs. The examples on this page all build on the same graph:
>>> import nctx.directed as nctx
>>> g = nctx.Graph()
>>> name_map = nctx.PropertyMapStr(g, 'name')
>>> context_map = PropertyMapVecDouble(g, 'context')
>>> nctx.read_graphml(g, 'bcde.ctx.dir.graphml', [name_map, context_map])
If you load this module, all operations refer to directed graphs.
Up to now, there is no possibility to convert a directed graph into an undirected one or vice versa. This will be added in future versions.
Todo
Functionality to convert directed to undirected graphs.
The Graph Class¶
Graph |
This class represents a directed graph. |
__hash__ |
|
__init__ |
|
add_edge |
Add an edge that connects u and v. |
add_vertex |
Add a vertex to the graph. |
children |
Iterate over the children of a given vertex u. |
edges |
Iterate over the edges of the graph. |
in_edges |
Iterate over the incoming edges of a vertex. |
numEdges |
Retrieve the number of edges in the graph. |
numVertices |
Retrieve the number of vertices in the graph. |
out_edges |
Iterate over the outgoing edges of a vertex. |
parents |
Iterate over the parents of a given vertex u. |
source |
Retrieve the source vertex of an edge. |
target |
Retrieve the target vertex of an edge. |
vertices |
Iterate over the vertices of the graph. |
-
class
nctx.directed.
Graph
¶ This class represents a directed graph.
Example: >>> import nctx.directed as nctx >>> graph = nctx.Graph()
-
__hash__
() → int¶ Not commented yet
-
__init__
((object)arg1) → None¶ Not commented yet
-
add_edge
((int)u, (int)v) → bool :¶ Add an edge that connects u and v.
Returns: Status if adding the edge was successful.
Return type: bool
Example: >>> from collections import defaultdict >>> vertices = defaultdict(lambda: graph.add_vertex()) >>> for i in range(10): ... _ = vertices[i] >>> graph.add_edge(vertices[0], vertices[1]) True
-
add_vertex
() → int :¶ Add a vertex to the graph.
Returns: The ID of the added vertex.
Return type: int
Example: >>> from collections import defaultdict >>> name_map = nctx.PropertyMapStr(graph, 'name') >>> vertices = defaultdict(lambda: graph.add_vertex()) >>> for i in range(10): ... v = vertices[i] ... name_map.set_elem(v, f"vertex {i}")
-
children
((int)u) → a_iter_ids :¶ Iterate over the children of a given vertex u. In the case of a directed graph,
children
provides access to the ancestors of u.Parameters: u (int) – ID of the vertex whose children should be iterated.
Returns: A vertex iterator
Return type: Example: >>> some_vertex = 0 >>> for c in g.children(some_vertex): ... pass
-
edges
() → e_iter_ids :¶ Iterate over the edges of the graph. Use
source
to access the source vertex of an edge andtarget
to access its target. To handle incoming and outgoing edges separately, usein_edges
andout_edges
.Returns: The edge iterator
Return type: Example: >>> for e in g.edges(): ... source_vertex = g.source(e) ... target_vertex = g.target(e)
-
in_edges
((int)arg2) → e_iter_ids_in :¶ Iterate over the incoming edges of a vertex. Use
source
to access the source vertex of an edge.Parameters: v (int) – ID of the vertex whose in-edges should be iterated.
Returns: The in-edge iterator
Return type: Example: >>> v=4 >>> for e in g.in_edges(v): ... source_vertex = g.source(e)
-
numEdges
() → int :¶ Retrieve the number of edges in the graph.
Returns: The number of edges Return type: int
-
numVertices
() → int :¶ Retrieve the number of vertices in the graph.
Returns: The number of vertices Return type: int
-
out_edges
((int)arg2) → e_iter_ids_out :¶ Iterate over the outgoing edges of a vertex. Use
target
to access the target vertex of an edge.Parameters: v (int) – ID of the vertex whose out-edges should be iterated.
Returns: The out-edge iterator
Return type: Example: >>> v=4 >>> for e in g.in_edges(v): ... target_vertex = g.target(e)
-
parents
((int)u) → a_iter_ids_rev :¶ Iterate over the parents of a given vertex u. In the case of a directed graph,
parents
provides access to the predecessors of u.Parameters: u (int) – ID of the vertex whose parents should be iterated.
Returns: A vertex iterator
Return type: Example: >>> some_vertex = 0 >>> for p in g.parents(some_vertex): ... pass
-
source
((Edge)e) → int :¶ Retrieve the source vertex of an edge. See
edges
for an example.Parameters: e (Edge) – An edge. Returns: The ID of the source vertex Return type: int
-
target
((Edge)e) → int :¶ Retrieve the target vertex of an edge. See
edges
for an example.Parameters: e (Edge) – An edge. Returns: The ID of the target vertex Return type: int
-
vertices
() → v_iter_ids :¶ Iterate over the vertices of the graph.
Returns: A vertex iterator
Return type: Example: >>> for v in g.vertices(): ... pass
-
AlgPaths: Shortest-path discovery¶
AlgPaths |
|
dijkstra |
Conventional single-source shortest paths discovery using Dijkstra’s algorithm. |
dijkstra_apsp_ctx |
Obtain all-pairs-shortest-paths using Dijkstra’s algorithm taking into account contextual constraints. |
dijkstra_ctx |
Obtain shortest paths with dynamic contextual constraints. |
find_path |
Conventional shortest path discovery using Dijkstra’s algorithm. |
find_path_ctx |
Obtain a shortest paths from a start vertex to a target vertex using Dijkstra’s algorithm taking into account contextual constraints. |
-
class
nctx.directed.
AlgPaths
¶ -
dijkstra
((Graph)g, (int)start, (PropertyMapULong)distances) → None :
-
dijkstra
((Graph)g, (int)start) → PropertyMapULong :¶ Conventional single-source shortest paths discovery using Dijkstra’s algorithm.
Parameters: - g (Graph) – The Graph object
- start (int) – Source vertex
- distances (PropertyMapULong) – PropertyMap in which distances will be written.
Returns: PropertyMap with distances.
Return type:
-
dijkstra_apsp_ctx
((Graph)g, (object)decision_fct, (PropertyMapVecULong)distances) → None :
-
dijkstra_apsp_ctx
((Graph)g, (object)decision_fct) → PropertyMapVecULong :¶ Obtain all-pairs-shortest-paths using Dijkstra’s algorithm taking into account contextual constraints.
This is basically a wrapper around
dijkstra_ctx
. Please see the mentioned function for more information regarding the contextual constraints.Parameters: - g (Graph) – The Graph object
- decision_fct (function) – The decision function enforcing contextual constraints. The signature of the function is
(vertex index, vertex index, vertex index) -> Bool
- distances (PropertyMapVecULong) – matrix in which distances will be written
Returns: matrix of distances named apsp_constraint
Return type: Example: >>> distmap = PropertyMapVecULong(g, 'distances_ctx') >>> AlgPaths.dijkstra_apsp_ctx(g,lambda _start,_current,_next: (True), distmap)
Example: >>> distmap = AlgPaths.dijkstra_apsp_ctx(g,lambda _start,_current,_next: (True))
-
dijkstra_ctx
((Graph)g, (int)s, (object)decision_fct, (PropertyMapULong)distances) → None :
-
dijkstra_ctx
((Graph)g, (int)s, (object)decision_fct) → PropertyMapULong :¶ Obtain shortest paths with dynamic contextual constraints.
Single-source shortest paths using Dijkstra’s algorithm taking into account contextual constraints. Enforcement of constraints is the task of the given user-defined function.
The function enforcing contextual constraints is evaluated at each node during shortest path discovery. The function needs to evaluate to
true
orfalse
allowing an edge to be visited or not. As parameters, the function needs to accept the starting node of path traversal, the current node, and the descending node in question. If the function returns False, the descending node is not being visited. Passing the start vertex is unnecessary here. However, the signature is the same as for the other functions for usability reasons.The three nodes are passed as indices allowing for access of (external) attribute and other associated information.
Note that the decision function is evaluated more than once during path traversal. That means, there should not happen any resource-intense computation inside this function. Also, it does not allow to keep track of the status of calculation, e.g. by calculating the visited edges or something similar.
If the decision function simply returns True all the time, the set of shortest paths is the unaltered set of shortest paths expected by the classical Dijkstra-implementation.
Parameters: distances (PropertyMapULong) – map in which distances will be written
Returns: map containing distances named dijkstra_constraint
Return type: Example: >>> start = 3 >>> distmap = PropertyMapULong(g, 'distances_ctx') >>> AlgPaths.dijkstra_ctx(g, start, lambda _start,_current,_next: (True), distmap)
Example: >>> start = 3 >>> distmap = AlgPaths.dijkstra_ctx(g, start, lambda _start,_current,_next: (True))
-
find_path
((Graph)g, (int)start, (int)target) → list :¶ Conventional shortest path discovery using Dijkstra’s algorithm.
Parameters: - g (Graph) – The Graph object
- start (int) – Source vertex
- target (int) – Target vertex
Returns: A list of vertex indices describing the shortest path from start to target.
Return type: list
-
find_path_ctx
((Graph)g, (int)start, (int)target, (object)decision_fct) → list :¶ Obtain a shortest paths from a start vertex to a target vertex using Dijkstra’s algorithm taking into account contextual constraints.
This is basically a wrapper around
dijkstra_ctx
. Please see the mentioned function for more information regarding the contextual constraints.Parameters: - g (Graph) – The Graph object
- decision_fct (function) – The decision function enforcing contextual constraints. The signature of the function is
(vertex index, vertex index, vertex index) -> Bool
- start (int) – Start vertex
- target (int) – Target vertex
Returns: A list of vertex indices describing the shortest path from start to target.
Return type: list
Example: >>> start = 3 >>> target = 10 >>> path = AlgPaths.find_path_ctx(g,start,target,lambda _start,_current,_next: (True))
-
AlgCentralities: Centrality measures¶
AlgCentralities |
|
betweenness |
Obtain betweenness centrality for a graph using Brandes’ efficient algorithm. |
betweenness_ctx |
Betweenness centrality with dynamic contextual constraints. |
closeness |
Obtain closeness centrality for a graph. |
closeness_ctx |
Closeness centrality with dynamic contextual constraints. |
-
class
nctx.directed.
AlgCentralities
¶ -
betweenness
((Graph)g, (PropertyMapDouble)outmap) → None :
-
betweenness
((Graph)g) → PropertyMapDouble :¶ Obtain betweenness centrality for a graph using Brandes’ efficient algorithm.
Parameters: - g (Graph) – Graph object
- outmap (PropertyMapDouble) – map in which centrality values will be written
Returns: list of centrality values named betweenness
Return type: Example: >>> outmap = PropertyMapDouble(g, 'betw') >>> AlgCentralities.betweenness(g, outmap)
Example: >>> outmap = AlgCentralities.betweenness(g)
-
betweenness_ctx
((Graph)g, (object)betw_decision_fct, (PropertyMapDouble)outmap) → None :
-
betweenness_ctx
((Graph)g, (object)betw_decision_fct) → PropertyMapDouble :¶ Betweenness centrality with dynamic contextual constraints.
Using this function allows obtaining betweenness centrality under dynamic contextual constraints. Enforcement of constraints is the task of the given user-defined function.
The function enforcing contextual constraints is evaluated at each node during shortest path traversal. The function needs to evaluate to True or False allowing an edge to be visited or not. As parameters, the current state of the betweenness calculation is passed to the function, i.e. the starting node for which a centrality value is being calculated, the current node, and the descending node in question. If the function returns False, the descending node is not being visited.
The three nodes are passed as indices allowing for access of (external) attribute and other associated information.
Note that the decision function is evaluated more than once during path traversal. That means, there should not happen any resource-intense computation inside this function. Also, it does not allow to keep track of the status of calculation, e.g. by calculating the visited edges or something similar.
If the decision function simply returns True all the time, this function results in the unaltered betweenness centrality values.
Obtaining betweenness centrality is based on Brandes’ efficient algorithm. At the current stage, the implementation allows for single-core execution only.
Parameters: outmap (PropertyMapDouble) – map in which centrality values will be written
Returns: list of centrality values named betweenness_constraint
Return type: Example: >>> outmap = PropertyMapDouble(g, 'betw_ctx') >>> AlgCentralities.betweenness_ctx(g,lambda _start,_current,_next: (True),outmap)
Example: >>> outmap = AlgCentralities.betweenness_ctx(g,lambda _start,_current,_next: (True))
-
closeness
((Graph)g) → PropertyMapDouble :
-
closeness
((Graph)g, (PropertyMapDouble)outmap) → None :¶ Obtain closeness centrality for a graph.
Parameters: - g (Graph) – Graph object
- outmap (PropertyMapDouble) – map in which centrality values will be written
Returns: list of centrality values named betweenness
Return type: Example: >>> outmap = AlgCentralities.closeness(g)
Example: >>> outmap = PropertyMapDouble(g, 'clsn') >>> AlgCentralities.closeness(g, outmap)
-
closeness_ctx
((Graph)g, (object)clsn_decision_fct, (PropertyMapDouble)outmap) → None :
-
closeness_ctx
((Graph)g, (object)clsn_decision_fct) → PropertyMapDouble :¶ Closeness centrality with dynamic contextual constraints.
Using this function allows obtaining closeness centrality under dynamic contextual constraints. Enforcement of constraints is the task of the given user-defined function.
The function enforcing contextual constraints is evaluated at each node during shortest path traversal. The function needs to evaluate to True or False allowing an edge to be visited or not. As parameters, the current state of the centrality calculation is passed to the function, i.e. the starting node for which a centrality value is being calculated, the current node, and the descending node in question. If the function returns False, the descending node is not being visited.
The three nodes are passed as indices allowing for access of (external) attribute and other associated information.
Note that the decision function is evaluated more than once during path traversal. That means, there should not happen any resource-intense computation inside this function. Also, it does not allow to keep track of the status of calculation, e.g. by calculating the visited edges or something similar.
If the decision function simply returns True all the time, this function results in the unaltered betweenness centrality values.
Parameters: outmap (PropertyMapDouble) – map in which centrality values will be written
Returns: list of centrality values named closeness_constraint
Return type: Example: >>> outmap = PropertyMapDouble(g, 'clsn_ctx') >>> AlgCentralities.closeness_ctx(g,lambda _start,_current,_next: (True),outmap)
Example: >>> outmap = AlgCentralities.closeness_ctx(g,lambda _start,_current,_next: (True))
-
Graph Property Maps¶
Property maps allow to store attribute information together with the vertices. These are mainly used for importing and exporting graphs: read_graphml
and write_graphml
Todo
Support edge property maps
Property Maps containing atom elements¶
PropertyMapDouble |
PropertyMapDouble serves as a list of atom items of type float. |
PropertyMapInt |
PropertyMapInt serves as a list of atom items of type int. |
PropertyMapStr |
PropertyMapStr serves as a list of atom items of type str. |
PropertyMapULong |
PropertyMapULong serves as a list of atom items of type unsigned long - a number > 0. |
-
class
nctx.directed.
PropertyMapDouble
¶ PropertyMapDouble serves as a list of atom items of type float. The list contains values of a single data type only as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list, i.e. it allows iteration and supports built-in functions append and extend.
The contained data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (float) – initial value
Example: >>> my_map = PropertyMapDouble(g, 'context1') >>> my_map = PropertyMapDouble(g, 'context1', .0)
Example: >>> import numpy as np >>> np_arr = np.array(list(my_map))
-
__contains__
((float)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> .0 in my_map True >>> .1 in my_map False
-
__getitem__
((int)arg2) → float :¶ This allows to access items using square brackets. See
get_elem
for a detailed description of parameters.Example: >>> _ = my_map[2]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (float)arg4) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → float :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (float)arg3) → None :¶ This allows to modify items using square brackets. See
set_elem
for a detailed description of parameters.Example: >>> my_map[2] = .1
-
get_elem
((int)i) → float :¶ Retrieve the ith element of this list.n
Parameters: i (int) – Index of element. Returns: ith element. Return type: float
-
get_name
() → str :¶ Retrieve name of the property map.
The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → float :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (float)newval) → None :¶ Set the ith element of this list to be newval. Note that the type of newval needs to be float or a type that can be implicitly converted to float.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (float) – The new element.
Example: Note the type conversion (float to int) from the input list to the output of
get_elem
.>>> complex_map = PropertyMapDouble(g, 'context') >>> complex_map.set_elem(0, 3) >>> complex_map.get_elem(0) 3.0
-
class
nctx.directed.
PropertyMapInt
¶ PropertyMapInt serves as a list of atom items of type int. The list contains values of a single data type only as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list, i.e. it allows iteration and supports built-in functions append and extend.
The contained data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (int) – initial value
Example: >>> my_map = PropertyMapInt(g, 'context1') >>> my_map = PropertyMapInt(g, 'context1', 0)
Example: >>> import numpy as np >>> np_arr = np.array(list(my_map))
-
__contains__
((int)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> 0 in my_map True >>> 1 in my_map False
-
__getitem__
((int)arg2) → int :¶ This allows to access items using square brackets. See
get_elem
for a detailed description of parameters.Example: >>> _ = my_map[2]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (int)arg4) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → int :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (int)arg3) → None :¶ This allows to modify items using square brackets. See
set_elem
for a detailed description of parameters.Example: >>> my_map[2] = 1
-
get_elem
((int)i) → int :¶ Retrieve the ith element of this list.n
Parameters: i (int) – Index of element. Returns: ith element. Return type: int
-
get_name
() → str :¶ Retrieve name of the property map.
The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → int :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (int)newval) → None :¶ Set the ith element of this list to be newval. Note that the type of newval needs to be int or a type that can be implicitly converted to int. Please see
PropertyMapDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (int) – The new element.
-
class
nctx.directed.
PropertyMapStr
¶ PropertyMapStr serves as a list of atom items of type str. The list contains values of a single data type only as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list, i.e. it allows iteration and supports built-in functions append and extend.
The contained data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (str) – initial value
Example: >>> my_map = PropertyMapStr(g, 'context1') >>> my_map = PropertyMapStr(g, 'context1', '')
Example: >>> import numpy as np >>> np_arr = np.array(list(my_map))
-
__contains__
((str)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> '' in my_map True >>> '1' in my_map False
-
__getitem__
((int)arg2) → str :¶ This allows to access items using square brackets. See
get_elem
for a detailed description of parameters.Example: >>> _ = my_map[2]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (str)arg4) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → str :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (str)arg3) → None :¶ This allows to modify items using square brackets. See
set_elem
for a detailed description of parameters.Example: >>> my_map[2] = '1'
-
get_elem
((int)i) → str :¶ Retrieve the ith element of this list.n
Parameters: i (int) – Index of element. Returns: ith element. Return type: str
-
get_name
() → str :¶ Retrieve name of the property map.
The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → str :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (str)newval) → None :¶ Set the ith element of this list to be newval. Note that the type of newval needs to be str or a type that can be implicitly converted to str. Please see
PropertyMapDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (str) – The new element.
-
class
nctx.directed.
PropertyMapULong
¶ PropertyMapULong serves as a list of atom items of type unsigned long - a number > 0. The list contains values of a single data type only as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list, i.e. it allows iteration and supports built-in functions append and extend.
The contained data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (int) – initial value
Example: >>> my_map = PropertyMapULong(g, 'context1') >>> my_map = PropertyMapULong(g, 'context1', 0)
Example: >>> import numpy as np >>> np_arr = np.array(list(my_map))
-
__contains__
((int)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> 0 in my_map True >>> 1 in my_map False
-
__getitem__
((int)arg2) → int :¶ This allows to access items using square brackets. See
get_elem
for a detailed description of parameters.Example: >>> _ = my_map[2]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (int)arg4) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → int :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (int)arg3) → None :¶ This allows to modify items using square brackets. See
set_elem
for a detailed description of parameters.Example: >>> my_map[2] = 1
-
get_elem
((int)i) → int :¶ Retrieve the ith element of this list.n
Parameters: i (int) – Index of element. Returns: ith element. Return type: int
-
get_name
() → str :¶ Retrieve name of the property map.
The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → int :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (int)newval) → None :¶ Set the ith element of this list to be newval. Note that the type of newval needs to be int or a type that can be implicitly converted to int. Please see
PropertyMapDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (int) – The new element.
Property Maps containing list elements¶
PropertyMapVecDouble |
PropertyMapVecDouble serves as a container of lists, i. |
PropertyMapVecInt |
PropertyMapVecInt serves as a container of lists, i. |
PropertyMapVecStr |
PropertyMapVecStr serves as a container of lists, i. |
PropertyMapVecULong |
PropertyMapVecULong serves as a container of lists, i. |
-
class
nctx.directed.
PropertyMapVecDouble
¶ PropertyMapVecDouble serves as a container of lists, i.e. it is a list of lists of float. The nested lists all contain values of a single data type as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list returning lists as items when iterating over it.
The allowed data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (float) – initial value for each inner list
- init_size (int) – size of each inner list.
Example: >>> my_map = PropertyMapVecDouble(g, 'context1') >>> my_map = PropertyMapVecDouble(g, 'context1', 0.0, 10)
Example: >>> import numpy as np >>> np_arr = np.array([np.array(xi) for xi in my_map])
-
__contains__
((object)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> [.1,.2,.3] in my_map False
-
__getitem__
((int)arg2) → object :¶ This allows to access items using square brackets. See
get_list
for a detailed description of parameters.Example: >>> _ = my_map[2] >>> _ = my_map[2][0]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (float)arg4, (int)arg5) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (object)arg3) → None :¶ This allows to modify items using square brackets. See
set_list
for a detailed description of parameters.Example: >>> my_map[2] = [.1,.2,.3,.4,.5] >>> my_map[2][0] = 42.0
-
get_elem
((int)i, (int)j) → float :¶ Retrieve the jth element of ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: jth element in ith list.
Return type: float
-
get_list
((int)i) → object :¶ Retrieve the ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: The list saved at position i.
Return type:
-
get_name
() → str :¶ Retrieve name of the property map. The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (int)j, (float)newval) → None :¶ Set the jth element of ith list. Note that the type of the new element needs to be float or a type that can be implicitly converted to float.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (float) – The new element.
Example: Note the type conversion (float to int) from the input to the output of
get_elem
.>>> complex_map = PropertyMapVecDouble(g, 'context') >>> complex_map.set_elem(0, 1, 3) >>> complex_map.get_elem(0,1) 3.0
-
set_list
((int)i, (object)newlist) → None :¶ Set the ith list to be newlist.
Parameters: - i (int) – Index of List
- newlist (list) – List to be saved at position i. Note that all items of this list will be converted to float internally.
Example: Note the type conversion (float to int) from the input list to the output of
get_elem
.>>> complex_map = PropertyMapVecDouble(g, 'context') >>> complex_map.set_list(0, [1,2,3,4,5]) >>> complex_map.get_elem(0,2) 3.0
-
class
nctx.directed.
PropertyMapVecInt
¶ PropertyMapVecInt serves as a container of lists, i.e. it is a list of lists of int. The nested lists all contain values of a single data type as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list returning lists as items when iterating over it.
The allowed data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (int) – initial value for each inner list
- init_size (int) – size of each inner list.
Example: >>> my_map = PropertyMapVecInt(g, 'context1') >>> my_map = PropertyMapVecInt(g, 'context1', 0, 10)
Example: >>> import numpy as np >>> np_arr = np.array([np.array(xi) for xi in my_map])
-
__contains__
((object)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> [1,2,3] in my_map False
-
__getitem__
((int)arg2) → object :¶ This allows to access items using square brackets. See
get_list
for a detailed description of parameters.Example: >>> _ = my_map[2] >>> _ = my_map[2][0]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (int)arg4, (int)arg5) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (object)arg3) → None :¶ This allows to modify items using square brackets. See
set_list
for a detailed description of parameters.Example: >>> my_map[2] = [1,2,3,4,5] >>> my_map[2][0] = 42
-
get_elem
((int)i, (int)j) → int :¶ Retrieve the jth element of ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: jth element in ith list.
Return type: int
-
get_list
((int)i) → object :¶ Retrieve the ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: The list saved at position i.
Return type:
-
get_name
() → str :¶ Retrieve name of the property map. The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (int)j, (int)newval) → None :¶ Set the jth element of ith list. Note that the type of the new element needs to be int or a type that can be implicitly converted to int. Please see
PropertyMapVecDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (int) – The new element.
-
set_list
((int)i, (object)newlist) → None :¶ Set the ith list to be newlist. Please see
PropertyMapVecDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- newlist (list) – List to be saved at position i. Note that all items of this list will be converted to int internally.
-
class
nctx.directed.
PropertyMapVecStr
¶ PropertyMapVecStr serves as a container of lists, i.e. it is a list of lists of str. The nested lists all contain values of a single data type as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list returning lists as items when iterating over it.
The allowed data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (str) – initial value for each inner list
- init_size (int) – size of each inner list.
Example: >>> my_map = PropertyMapVecStr(g, 'context1') >>> my_map = PropertyMapVecStr(g, 'context1', '', 10)
Example: >>> import numpy as np >>> np_arr = np.array([np.array(xi) for xi in my_map])
-
__contains__
((object)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> ['1','2','3'] in my_map False
-
__getitem__
((int)arg2) → object :¶ This allows to access items using square brackets. See
get_list
for a detailed description of parameters.Example: >>> _ = my_map[2] >>> _ = my_map[2][0]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (str)arg4, (int)arg5) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (object)arg3) → None :¶ This allows to modify items using square brackets. See
set_list
for a detailed description of parameters.Example: >>> my_map[2] = ['1','2','3','4','5'] >>> my_map[2][0] = '42'
-
get_elem
((int)i, (int)j) → str :¶ Retrieve the jth element of ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: jth element in ith list.
Return type: str
-
get_list
((int)i) → object :¶ Retrieve the ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: The list saved at position i.
Return type:
-
get_name
() → str :¶ Retrieve name of the property map. The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (int)j, (str)newval) → None :¶ Set the jth element of ith list. Note that the type of the new element needs to be str or a type that can be implicitly converted to str. Please see
PropertyMapVecDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (str) – The new element.
-
set_list
((int)i, (object)newlist) → None :¶ Set the ith list to be newlist. Please see
PropertyMapVecDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- newlist (list) – List to be saved at position i. Note that all items of this list will be converted to str internally.
-
class
nctx.directed.
PropertyMapVecULong
¶ PropertyMapVecULong serves as a container of lists, i.e. it is a list of lists of unsigned long - numbers > 0. The nested lists all contain values of a single data type as this is a link to the C++ code base (and C++ is a strongly typed language). This PropertyMap behaves like a python list returning lists as items when iterating over it.
The allowed data type can be recognized from the class name of the PropertyMap.
The PropertyMap can be converted to a Python list or a numpy array easily.
Parameters: - g (Graph) – The Graph object
- name (str) – Name of the PropertyMap
- init_val (int) – initial value for each inner list
- init_size (int) – size of each inner list.
Example: >>> my_map = PropertyMapVecULong(g, 'context1') >>> my_map = PropertyMapVecULong(g, 'context1', 0, 10)
Example: >>> import numpy as np >>> np_arr = np.array([np.array(xi) for xi in my_map])
-
__contains__
((object)arg2) → bool :¶ Allows to check for the existence of elements in the PropertyMap.
Example: >>> [1,2,3] in my_map False
-
__getitem__
((int)arg2) → object :¶ This allows to access items using square brackets. See
get_list
for a detailed description of parameters.Example: >>> _ = my_map[2] >>> _ = my_map[2][0]
-
__init__
((object)arg1, (Graph)arg2, (str)arg3) → None
-
__init__
((object)arg1, (Graph)arg2, (str)arg3, (int)arg4, (int)arg5) → None¶ Not commented yet
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int :¶ Allows to retrieve the length of the PropertyMap.
Example: >>> len(my_map) 10
-
__next__
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
__setitem__
((int)arg2, (object)arg3) → None :¶ This allows to modify items using square brackets. See
set_list
for a detailed description of parameters.Example: >>> my_map[2] = [1,2,3,4,5] >>> my_map[2][0] = 42
-
get_elem
((int)i, (int)j) → int :¶ Retrieve the jth element of ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: jth element in ith list.
Return type: int
-
get_list
((int)i) → object :¶ Retrieve the ith list.
Parameters: - i (int) – Index of List
- j (int) – Index of element.
Returns: The list saved at position i.
Return type:
-
get_name
() → str :¶ Retrieve name of the property map. The name is mainly used when exporting the Graph to a file, e.g. using
write_graphml
.Returns: The name of the list Return type: string
-
next
() → object :¶ Allows to iterater over the PropertyMap.
Example: >>> for ctx in my_map: ... pass
-
set_elem
((int)i, (int)j, (int)newval) → None :¶ Set the jth element of ith list. Note that the type of the new element needs to be int or a type that can be implicitly converted to int. Please see
PropertyMapVecDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- j (int) – Index of element.
- newval (int) – The new element.
-
set_list
((int)i, (object)newlist) → None :¶ Set the ith list to be newlist. Please see
PropertyMapVecDouble.set_elem
for an example of type conversion.Parameters: - i (int) – Index of List
- newlist (list) – List to be saved at position i. Note that all items of this list will be converted to int internally.
Graph I/O: Importing and Exporting to files¶
read_graphml |
Reads graph specification from a GraphML file and fills a graph object accordingly. |
write_graphml |
Reads graph specification from a GraphML file and fills a graph object accordingly. |
-
nctx.directed.
read_graphml
((Graph)g, (str)filename, (list)pmaps) → None :¶ Reads graph specification from a GraphML file and fills a graph object accordingly. You can pass it a list of PropertyMaps to be filled. The names of the PropertyMaps must match the names in the graphml file. Graph properties that are present in the GraphML file but that do not have a matching PropertyMap in pmaps are ignored. See https://en.wikipedia.org/wiki/GraphML for more infos about the file format.
Parameters: - g (Graph) – The Graph object
- filename (str) – Path to a GraphML file
- pmaps (list) – A list of PropertyMaps that will be filled with the graph properties found in the file.
Example: >>> import nctx.directed as nctx >>> g = nctx.Graph() >>> name_map = nctx.PropertyMapStr(g, 'name') >>> context_map = PropertyMapVecDouble(g, 'context') >>> nctx.read_graphml(g, 'bcde.ctx.dir.graphml', [name_map, context_map])
-
nctx.directed.
write_graphml
((Graph)g, (str)filename, (list)pmaps) → None :¶ Reads graph specification from a GraphML file and fills a graph object accordingly. You can pass it a list of PropertyMaps to be filled. The names of the PropertyMaps must match the names in the graphml file. Graph properties that are present in the GraphML file but that do not have a matching PropertyMap in pmaps are ignored. See https://en.wikipedia.org/wiki/GraphML for more infos about the file format.
Parameters: - g (Graph) – The Graph object
- filename (str) – Path to a GraphML file
- pmaps (list) – A list of PropertyMaps that will be filled with the graph properties found in the file.
Example: >>> import nctx.directed as nctx >>> g = nctx.Graph() >>> name_map = nctx.PropertyMapStr(g, 'name') >>> context_map = PropertyMapVecDouble(g, 'context') >>> nctx.read_graphml(g, 'bcde.ctx.dir.graphml', [name_map, context_map]) >>> name_map[3] = 'X' >>> context_map[0][2] = .1337 >>> nctx.write_graphml(g, 'bcde.ctx.dir.modified.graphml', [name_map, context_map])
Helper Classes¶
-
class
nctx.directed.
Edge
¶ This is an internal description of an edge. It can’t be instantiated from Python. Just don’t bother with it.
-
class
nctx.directed.
a_iter_ids
¶ This is an internal description of the child-iterator.
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int¶ Not commented yet
-
__next__
() → int¶ Not commented yet
-
next
() → int :¶ See
Graph.children
how to use this to iterate over the children of a node.
-
-
class
nctx.directed.
a_iter_ids_rev
¶ This is an internal description of the parents-iterator.
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int¶ Not commented yet
-
__next__
() → int¶ Not commented yet
-
next
() → int :¶ See
Graph.parents
how to use this to iterate over the parents of a node.
-
-
class
nctx.directed.
e_iter_ids
¶ This is an internal description of the edges-iterator.
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int¶ Not commented yet
-
__next__
() → Edge¶ Not commented yet
-
next
() → Edge :¶ See
Graph.edges
how to use this to iterate over the edges of a graph.
-
-
class
nctx.directed.
e_iter_ids_in
¶ This is an internal description of the in-edges-iterator.
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int¶ Not commented yet
-
__next__
() → Edge¶ Not commented yet
-
next
() → Edge :¶ See
Graph.edges
how to use this to iterate over the edges of a graph.
-
-
class
nctx.directed.
e_iter_ids_out
¶ This is an internal description of the out-edges-iterator.
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int¶ Not commented yet
-
__next__
() → Edge¶ Not commented yet
-
next
() → Edge :¶ See
Graph.edges
how to use this to iterate over the edges of a graph.
-
-
class
nctx.directed.
v_iter_ids
¶ This is an internal description of the vertex-iterator.
-
__iter__
((object)arg1) → object¶ Not commented yet
-
__len__
() → int¶ Not commented yet
-
__next__
() → int¶ Not commented yet
-
next
() → int :¶ See
Graph.vertices
how to use this to iterate over the vertices of a graph.
-