Conditions Tree: first blueprint using anytree (iteratively)

This commit is contained in:
Jean Sirmai 2022-01-06 12:27:11 +01:00
parent 5f70690871
commit 9e327a2643
Signed by: jean
GPG Key ID: FB3115C340E057E3
1 changed files with 32 additions and 54 deletions

View File

@ -1,69 +1,47 @@
# https://anytree.readthedocs.io/en/latest/_modules/anytree/node/nodemixin.html
from anytree import NodeMixin, RenderTree
import model import model
from anytree import Node, RenderTree
# from anytree import Node, AnyNode, NodeMixin, RenderTree, find_by_attr
# from anytree.exporter import DotExporter from ete3 import Tree
# https://stackoverflow.com/
# read-data-from-a-file-and-create-a-tree-using-anytree-in-python
# https://www.delftstack.com/howto/python/trees-in-python/
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html
# https://anytree.readthedocs.io/en/latest/
# TODO Contrôles de cohérence des données # TODO Contrôles de cohérence des données
# 1) node_id <> pas de doublon # 1) node_id <> pas de doublons
# 2) pas de node sans un node_parent_id # 2) tout node doit avoir un node_parent_id
conditions_set = set() conditions_set = set()
conditions_list = []
# TODO cnd.get_coord("y"),...
for i in range(0, len(model.testmodel.get_conditions().get_conditions_array())): for i in range(0, len(model.testmodel.get_conditions().get_conditions_array())):
cnd = model.testmodel.get_conditions().get_condition(i) condition = model.testmodel.get_conditions().get_condition(i)
conditions_set.add((cnd.get_node_self_id(), conditions_set.add((condition.get_node_self_id(),
cnd.get_node_parent_id(), condition.get_node_parent_id(),
cnd.get_coord("x"), condition.get_coord("x"), # TODO y,z,...
cnd.get_site(), condition.get_site(),
cnd.get_weight())) condition.get_weight()))
class CondTreeNode(NodeMixin):
class TreeNode(object): def __init__(self, id, x, site, weight, parent_node_id):
super(CondTreeNode, self).__init__()
# TODO self.y = y,...
def __init__(self, id, parent, x, site, weight):
self.id = id self.id = id
self.parent = parent self.parent_node_id = parent_node_id
self.children = [] self.parent = None
self.x = x self.x = x # TODO self.y = y,z...
self.site = site self.site = site
self.weight = weight self.weight = weight
def add_child(self, children_id): def add_parent_node(self, parent_node):
self.children.append(children_id) self.parent = parent_node
self.children = sorted(self.children, key=id) print('orphan node ', self.id, ' is adopted by node ', parent_node.id)
for i in conditions_set: # sorted(conditions_set, key=i.id):
condition = CondTreeNode(i[0], i[2], i[3], i[4], i[1]) # TODO y,z,...
conditions_list.append(condition)
cnd_list = [] for i in conditions_list:
nodes_list = [] # [Node("0")] if (i.parent == None):
for j in conditions_list:
if (i.parent_node_id == j.id):
i.add_parent_node(j)
for i in conditions_set: # sorted(conditions_set, key=id): for pre, fill, node in RenderTree(conditions_list[3]):
cndt = TreeNode(i[0], i[1], i[2], i[3], i[4]) # TODO y,z,... treestr = u"%s%s" % (pre, node.id)
cnd_list.append(cndt) print(treestr.ljust(16), ' n =', node.weight,
' at (site', node.site, 'in cell', node.x, ')')
for i in cnd_list:
for j in cnd_list:
if (i.id == j.parent):
i.add_child(j)
nodes_list.append(Node(str(i))) # (Node(str(i), parent=str(i.parent))) #(i)
n0 = Node("0")
n1 = Node("1", parent=n0)
n2 = Node("2", parent=n1)
n3 = Node("3", parent=n2)
n4 = Node("4", parent=n2)
for pre, fill, node in RenderTree(n0):
print("%s%s" % (pre, node.name))
for i in nodes_list:
print(i) # print(i.id, ' ', i.parent)