mirror of
https://github.com/alicevision/Meshroom.git
synced 2025-06-06 04:41:58 +02:00
[graph] add helper methods to get nodes by type and sort them by name
This commit is contained in:
parent
e391d76a82
commit
1bc6168814
2 changed files with 58 additions and 0 deletions
|
@ -1250,6 +1250,46 @@ class Graph(BaseObject):
|
||||||
node, attribute = fullName.split('.', 1)
|
node, attribute = fullName.split('.', 1)
|
||||||
return self.node(node).attribute(attribute)
|
return self.node(node).attribute(attribute)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getNodeIndexFromName(name):
|
||||||
|
""" Nodes are created with a suffix index; returns this index by parsing node name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): the node name
|
||||||
|
Returns:
|
||||||
|
int: the index retrieved from node name (-1 if not found)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return int(name.split('_')[-1])
|
||||||
|
except:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def sortNodesByIndex(nodes):
|
||||||
|
"""
|
||||||
|
Sort the given list of Nodes using the suffix index in their names.
|
||||||
|
[NodeName_1, NodeName_0] => [NodeName_0, NodeName_1]
|
||||||
|
|
||||||
|
Args:
|
||||||
|
nodes (list[Node]): the list of Nodes to sort
|
||||||
|
Returns:
|
||||||
|
list[Node]: the sorted list of Nodes based on their index
|
||||||
|
"""
|
||||||
|
return sorted(nodes, key=lambda x: Graph.getNodeIndexFromName(x.name))
|
||||||
|
|
||||||
|
def nodesByType(self, nodeType, sortedByIndex=True):
|
||||||
|
"""
|
||||||
|
Returns all Nodes of the given nodeType.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
nodeType (str): the node type name to consider.
|
||||||
|
sortedByIndex (bool): whether to sort the nodes by their index (see Graph.sortNodesByIndex)
|
||||||
|
Returns:
|
||||||
|
list[Node]: the list of nodes matching the given nodeType.
|
||||||
|
"""
|
||||||
|
nodes = [n for n in self._nodes.values() if n.nodeType == nodeType]
|
||||||
|
return self.sortNodesByIndex(nodes) if sortedByIndex else nodes
|
||||||
|
|
||||||
def findNodeCandidates(self, nodeNameExpr):
|
def findNodeCandidates(self, nodeNameExpr):
|
||||||
pattern = re.compile(nodeNameExpr)
|
pattern = re.compile(nodeNameExpr)
|
||||||
return [v for k, v in self._nodes.objects.items() if pattern.match(k)]
|
return [v for k, v in self._nodes.objects.items() if pattern.match(k)]
|
||||||
|
|
|
@ -186,3 +186,21 @@ def test_graph_reverse_dfs():
|
||||||
# Get all nodes from C (order guaranteed)
|
# Get all nodes from C (order guaranteed)
|
||||||
nodes = graph.nodesFromNode(C)[0]
|
nodes = graph.nodesFromNode(C)[0]
|
||||||
assert nodes == [C, E, F]
|
assert nodes == [C, E, F]
|
||||||
|
|
||||||
|
|
||||||
|
def test_graph_nodes_sorting():
|
||||||
|
graph = Graph('')
|
||||||
|
|
||||||
|
ls0 = graph.addNewNode('Ls')
|
||||||
|
ls1 = graph.addNewNode('Ls')
|
||||||
|
ls2 = graph.addNewNode('Ls')
|
||||||
|
|
||||||
|
assert graph.nodesByType('Ls', sortedByIndex=True) == [ls0, ls1, ls2]
|
||||||
|
|
||||||
|
graph = Graph('')
|
||||||
|
# 'Random' creation order (what happens when loading a file)
|
||||||
|
ls2 = graph.addNewNode('Ls', name='Ls_2')
|
||||||
|
ls0 = graph.addNewNode('Ls', name='Ls_0')
|
||||||
|
ls1 = graph.addNewNode('Ls', name='Ls_1')
|
||||||
|
|
||||||
|
assert graph.nodesByType('Ls', sortedByIndex=True) == [ls0, ls1, ls2]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue