tree
phyllotaxis_analysis.tree Link
Rooted Tree Implementation
This module provides an implementation of a rooted tree graph for phyllotaxis analysis.
Acknowledgements:
This is a port of the original openalea.container.tree Python2 implementation by Christophe Pradal christophe.pradal@cirad.fr.
Key Features: - Tree class implementing basic tree operations - PropertyTree class extending Tree with property support - Support for tree traversal, manipulation, and property management - Methods to add, remove, and modify vertices and subtrees
Usage Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> # Create a simple tree
>>> tree = Tree()
>>> child1 = tree.add_child(tree.root)
>>> child2 = tree.add_child(tree.root)
>>> grandchild = tree.add_child(child1)
>>> # Check tree properties
>>> print(tree.nb_vertices()) # 4
4
>>> print(tree.is_leaf(grandchild)) # True
True
>>> print(tree.parent(child1)) # 0 (root)
0
GraphError Link
Bases: Exception
Base class for graph exceptions.
This exception is raised when a general graph operation fails.
Notes
All specific graph exceptions inherit from this class.
InvalidEdge Link
Bases: GraphError, KeyError
Exception raised when an edge is invalid.
This exception is raised when an operation is attempted on an edge that does not exist in the graph.
Examples:
>>> try:
... graph.edge_property(1, 2, 'weight') # Edge (1, 2) doesn't exist
... except InvalidEdge:
... print("Edge doesn't exist")
Edge doesn't exist
InvalidVertex Link
Bases: GraphError, KeyError
Exception raised when a vertex is invalid.
This exception is raised when an operation is attempted on a vertex that does not exist in the graph.
Examples:
>>> from phyllotaxis_analysis.tree import InvalidVertex
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
>>> try:
... tree.children(999) # Vertex 999 doesn't exist
... except InvalidVertex:
... print("Vertex doesn't exist")
Vertex doesn't exist
PropertyTree Link
PropertyTree(*args, **kwargs)
Bases: Tree
A tree with properties associated with vertices.
This class extends the Tree class by adding the ability to associate arbitrary properties (key-value pairs) with vertices in the tree.
Attributes:
| Name | Type | Description |
|---|---|---|
root |
int
|
The identifier of the root vertex. |
Notes
Properties are stored in dictionaries mapping vertex identifiers to values. Each property has its own dictionary.
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> child = tree.add_child(tree.root,child="child_value",properties=)
>>> tree.get_vertex_property(child)
>>> tree.property('color')[child]
Initialize a new PropertyTree instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Variable length argument list passed to the parent class constructor. |
()
|
|
**kwargs
|
Arbitrary keyword arguments passed to the parent class constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
PropertyTree
|
A new property tree with a single vertex (the root). |
Source code in src/phyllotaxis_analysis/tree.py
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 | |
root
property
writable
Link
root
Get the root vertex of the tree.
Returns:
| Type | Description |
|---|---|
int
|
The id of the root vertex of the tree. |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.root
0
__contains__ Link
__contains__(vid)
Check whether a vertex is present in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier to check for membership. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/phyllotaxis_analysis/tree.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
__iter__ Link
__iter__()
Iterate over the vertices of the tree.
This method returns an iterator over the vertices of the tree, allowing for easy traversal of the tree structure.
Yields:
| Type | Description |
|---|---|
Vertex
|
The next vertex in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
221 222 223 224 225 226 227 228 229 230 231 232 233 | |
__len__ Link
__len__()
Return the number of vertices in the tree.
This method returns the number of vertices in the tree by
delegating to the internal nb_vertices() method.
Returns:
| Type | Description |
|---|---|
int
|
The number of vertices in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
147 148 149 150 151 152 153 154 155 156 157 158 159 | |
add_child Link
add_child(parent, child=None, **properties)
Add a child vertex to the tree under the specified parent.
The child is appended to the end of the parent's children list. Additional properties can be assigned to the new vertex via keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
int
|
The identifier of the parent vertex to which the child will be attached. |
required |
child
|
int
|
The identifier of the child vertex to be added. If not provided, a new vertex identifier is generated automatically. |
None
|
**properties
|
dict[Any]
|
Keyword arguments representing additional properties to assign to the new vertex. Each key-value pair will be stored as a property of the vertex. |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
The identifier of the newly added child vertex. |
Notes
This method extends the base class's add_child by automatically
applying any provided properties to the new vertex using _add_vertex_properties.
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_child(0, ppty='value')
>>> tree.property_names()
ppty
>>> tree.property('ppty')
{1: 'value'}
Source code in src/phyllotaxis_analysis/tree.py
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 | |
add_child_tree Link
add_child_tree(parent, tree)
Add a tree after the children of the parent vertex.
The tree is inserted as a child of the specified parent vertex. If the tree being added is a subtree of the current tree, the operation has O(1) complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
int
|
The vertex identifier where the tree will be attached. |
required |
tree
|
PropertyTree
|
The rooted tree to be added as a child. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A mapping from tree IDs in the added tree to the corresponding vertex IDs in the parent tree. |
Notes
This method ensures that properties from the added tree are properly propagated to the new vertices in the parent tree. The operation is optimized for subtree insertion (O(1) complexity).
Source code in src/phyllotaxis_analysis/tree.py
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 | |
add_property Link
add_property(property_name)
Add a new map between vid and a data. Do not fill this property for any vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_name
|
str
|
Name of the property to add. |
required |
Notes
This method initializes an empty dictionary for the given property name in the _properties attribute.
The property is not populated with any vertex data at this stage.
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_property('ppty')
>>> tree.property_names()
ppty
Source code in src/phyllotaxis_analysis/tree.py
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 | |
add_vertex Link
add_vertex(vid=None)
Add a vertex to the graph.
Adds a new vertex to the graph with the specified identifier or auto-increments the identifier if not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The identifier for the new vertex. If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The identifier of the added vertex. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If the provided |
Examples:
>>> from phyllotaxis_analysis.tree import InvalidVertex
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_vertex()
1
>>> tree.add_vertex(vid=5)
5
>>> try:
>>> tree.add_vertex(vid=3)
>>> except InvalidVertex as e:
>>> print(f"ERROR: {e}")
ERROR: 'Vertex identifier already used: 3'
Source code in src/phyllotaxis_analysis/tree.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
children Link
children(vid)
Return the children of a vertex in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose children are to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
A list of vertex identifiers that are children of |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.children(0)
[5, 6]
>>> tree.children(5)
[]
Source code in src/phyllotaxis_analysis/tree.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
clear Link
clear()
Remove all vertices and edges from the graph.
This method clears the tree structure by removing all child and parent relationships.
Notes
This method does not delete the root vertex itself, only its relationships.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.nb_vertices()
3
>>> g.clear()
>>> g.nb_vertices()
1
Source code in src/phyllotaxis_analysis/tree.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
copy Link
copy()
Deep copy of the tree.
Returns:
| Type | Description |
|---|---|
tree
|
A new instance of the tree with all attributes deeply copied. |
Source code in src/phyllotaxis_analysis/tree.py
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 | |
edges_iter Link
edges_iter()
Iterate over the edges of the tree.
Yields:
| Type | Description |
|---|---|
tuple[int, int]
|
A tuple of (parent, child) vertices representing each edge in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
280 281 282 283 284 285 286 287 288 289 | |
get_vertex_property Link
get_vertex_property(vid)
Returns all the properties defined on a vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex ID for which to retrieve properties. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary mapping property names to their values for the specified vertex. |
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_child(0, ppty='A')
1
>>> tree.add_child(0, ppty='B')
2
>>> tree.get_vertex_property(2)
{'ppty': 'B'}
Source code in src/phyllotaxis_analysis/tree.py
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 | |
has_vertex Link
has_vertex(vid)
Determines whether a given vertex is in the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int or str
|
The identifier of the vertex to be checked for existence in the graph. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Returns |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.has_vertex(1)
True
>>> g.has_vertex(2)
False
>>> g.has_vertex(6)
True
Source code in src/phyllotaxis_analysis/tree.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
insert_parent Link
insert_parent(vid, parent_id=None, **properties)
Insert parent_id between vid and its actual parent. Inherit of the complex of the parent of vid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
A vertex identifier. |
required |
parent_id
|
int
|
A vertex identifier. Defaults to |
None
|
**properties
|
dict
|
Additional properties to assign to the new parent vertex. |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
The identifier of the newly inserted parent vertex. |
Source code in src/phyllotaxis_analysis/tree.py
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 | |
insert_sibling Link
insert_sibling(vid_1, vid_2=None, **properties)
Insert a new vertex as a sibling of an existing vertex in the tree.
The new vertex is inserted immediately before the specified vertex. Any additional properties provided are assigned to the new vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid_1
|
int
|
The vertex identifier of the existing vertex before which the new vertex will be inserted. |
required |
vid_2
|
int
|
The vertex identifier of the new vertex to insert. If not provided, a new vertex identifier is generated automatically. |
None
|
**properties
|
dict
|
Additional properties to assign to the new vertex. Keys are property names and values are the corresponding values. |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
The vertex identifier of the newly inserted vertex. |
Source code in src/phyllotaxis_analysis/tree.py
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 | |
insert_sibling_tree Link
insert_sibling_tree(vid, tree)
Insert a tree as a sibling to the vertex identified by vid.
The root of tree and the vertex vid become siblings. The operation
has O(1) complexity when tree is obtained via self.subtree().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
Vertex identifier in the current tree. |
required |
tree
|
PropertyTree
|
A rooted tree to insert as a sibling of |
required |
Returns:
| Type | Description |
|---|---|
dict[int, int]
|
Mapping from original tree vertex IDs to newly created vertex IDs. |
Notes
If tree is a subtree of the current tree (obtained via self.subtree()),
the operation is guaranteed to run in O(1) time.
Otherwise, the complexity depends on the size of tree.
Source code in src/phyllotaxis_analysis/tree.py
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 | |
is_leaf Link
is_leaf(vid)
Test if a vertex is a leaf in the tree.
A leaf vertex is defined as a vertex with no children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The ID of the vertex to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.is_leaf(5)
True
>>> tree.is_leaf(0)
False
Source code in src/phyllotaxis_analysis/tree.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | |
nb_children Link
nb_children(vid)
Return the number of children of a given vertex in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose children count is requested. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of children of the vertex. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.nb_children(0)
2
Source code in src/phyllotaxis_analysis/tree.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
nb_siblings Link
nb_siblings(vid)
Return the number of siblings of a given vertex identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The identifier of the vertex whose siblings are to be counted. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of siblings of the vertex with the identifier |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.nb_siblings(5)
1
Source code in src/phyllotaxis_analysis/tree.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
nb_vertices Link
nb_vertices()
Returns the number of vertices in the graph.
Returns:
| Type | Description |
|---|---|
int
|
The number of vertices in the graph. |
See Also
nb_edges : Returns the number of edges in the graph.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex() # add a second vertices
>>> g.nb_vertices()
3
Source code in src/phyllotaxis_analysis/tree.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
parent Link
parent(vid)
Return the parent of a vertex in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose parent is to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
The parent vertex identifier. Returns |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If the vertex identifier |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.parent(5)
0
Source code in src/phyllotaxis_analysis/tree.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
properties Link
properties()
Returns all the property maps contained in the graph.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary where keys are property names and values are the corresponding property maps. |
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_child(0,ppty='A')
1
>>> tree.add_child(0,ppty='B')
2
>>> tree.properties()
{'ppty': {1: 'A', 2: 'B'}}
Source code in src/phyllotaxis_analysis/tree.py
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 | |
property Link
property(name)
Returns the property map between the vid and the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the property to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary mapping vertex IDs (vid) to their corresponding data values. If the property does not exist, an empty dictionary is returned. |
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_child(0,ppty='value')
>>> tree.property('ppty')
{1: 'value'}
Source code in src/phyllotaxis_analysis/tree.py
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 | |
property_names Link
property_names()
Return the property names of all property maps.
Returns:
| Type | Description |
|---|---|
list
|
The list of the property names of all property maps. |
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_child(0,ppty='value')
>>> tree.property_names()
ppty
Source code in src/phyllotaxis_analysis/tree.py
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 | |
property_names_iter Link
property_names_iter()
Iterate over the names of all property maps.
Properties are defined only on vertices, even edge properties. This method returns an iterator over the property names stored in the tree.
Returns:
| Type | Description |
|---|---|
iter
|
An iterator over the names of all property maps in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 | |
remove_leaf Link
remove_leaf(vid)
Remove a leaf vertex from the tree.
The vertex identified by vid is removed only if it is a leaf (has no
children) and it is not the root of the tree. The parent–child
relationships stored in _children and _parent are updated
accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
Identifier of the vertex to be removed. |
required |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Notes
- The method mutates the internal
_childrenand_parentmappings of the tree instance. - Attempting to remove a vertex that does not exist in the internal structures has no effect.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0)
1
>>> tree.is_leaf(1)
True
>>> tree.remove_leaf(1) # removes leaf vertex 1
>>> tree.is_leaf(0)
True
>>> tree.remove_leaf(0) # cannot remove the root
phyllotaxis_analysis.tree.InvalidVertex: 'The root node 0 can not be removed.'
Source code in src/phyllotaxis_analysis/tree.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | |
remove_property Link
remove_property(property_name)
Remove the property map called property_name from the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_name
|
str
|
The name of the property map to remove. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_property('ppty')
>>> tree.property_names()
ppty
>>> tree.remove_property('ppty')
>>> tree.property_names()
[]
Source code in src/phyllotaxis_analysis/tree.py
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 | |
remove_tree Link
remove_tree(vid)
Remove the subtree rooted on vid.
Remove the subtree rooted at the vertex with the given ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex ID of the root of the subtree to remove. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The list of vertex IDs that were removed from the tree. |
Source code in src/phyllotaxis_analysis/tree.py
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 | |
remove_vertex Link
remove_vertex(vid, reparent_child=False)
Remove a specified vertex from the graph and all its attached edges.
Remove all edges connected to the vertex and clean up associated properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The ID of the vertex to remove. |
required |
reparent_child
|
bool
|
If |
False
|
Notes
This method also removes any properties associated with the vertex via _remove_vertex_properties.
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_vertex(1)
>>> tree.add_vertex(2)
>>> tree.vertices()
[0, 1, 2]
>>> tree.remove_vertex(1)
>>> tree.vertices()
[0, 2]
Source code in src/phyllotaxis_analysis/tree.py
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 | |
replace_parent Link
replace_parent(vid, new_parent_id)
Change the parent of a vertex to a new parent vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex ID whose parent will be changed. |
required |
new_parent_id
|
int
|
The new parent vertex ID. |
required |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Notes
The old parent relationship is removed when the new parent is assigned.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.children(0)
[5]
>>> tree.insert_parent(5, 6)
6
>>> tree.add_vertex(7)
7
>>> tree.replace_parent(5, 7)
>>> tree.parent(5)
7
>>> tree.children(0)
[6]
>>> tree.parent(7)
None
Source code in src/phyllotaxis_analysis/tree.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 | |
siblings Link
siblings(vid)
Return the siblings of a given vertex identifier in the tree.
Given a vertex identifier vid, this method returns a list of all sibling
vertices (i.e., vertices sharing the same parent). If the vertex has no
parent (i.e., it is the root), an empty list is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose siblings are to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The list of vertex identifiers that are siblings of |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.siblings(0)
[]
>>> tree.siblings(5)
[6]
Source code in src/phyllotaxis_analysis/tree.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
subtree Link
subtree(vid, copy=True)
Return the subtree containing all descendants of vid.
The induced subtree of the tree has the vertices in the subtree rooted at vid.
This creates a new tree that includes all vertices from vid and all its descendants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
A vertex of the original tree. |
required |
copy
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
PropertyTree
|
A subtree of the tree. If |
Examples:
>>> from phyllotaxis_analysis.tree import PropertyTree
>>> tree = PropertyTree()
>>> tree.add_child(0)
1
>>> tree.insert_parent(1)
2
>>> tree.insert_sibling(1)
3
>>> subtree = tree.subtree(2, copy=True)
>>> subtree.vertices()
[0, 1, 2]
>>> subtree.root
0
Source code in src/phyllotaxis_analysis/tree.py
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 | |
vertices Link
vertices()
Returns the list of vertices in the graph.
Returns:
| Type | Description |
|---|---|
list
|
The list of the vertices in the graph. |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.vertices()
[0, 1, 6]
Source code in src/phyllotaxis_analysis/tree.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
vertices_iter Link
vertices_iter()
Generates an iterator for the vertices in the graph.
This method is a generator that yields each vertex in the graph as a string, which corresponds to the key of the vertex in the internal dictionary representation of the graph. The order in which vertices are returned by this iterator is not guaranteed to be consistent between different calls or even different yields.
Yields:
| Type | Description |
|---|---|
str
|
A string representing the vertex in the graph. |
Source code in src/phyllotaxis_analysis/tree.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
Tree Link
Tree(root=0, tree=None)
Bases: object
Implementation of a rooted tree with vertex manipulation methods.
This class implements a tree data structure where each vertex has a unique identifier and can have multiple children but only one parent. The tree has a distinguished vertex called the root.
Attributes:
| Name | Type | Description |
|---|---|---|
_root |
int
|
The identifier of the root vertex. |
_id |
int
|
The value of the latest id used to register a vertex. |
_parent |
dict[int, int | None]
|
The dictionary of vertex id (key) and their parent vertex id (value). |
_children |
dict[int, int | list[int]]
|
The dictionary of vertex id (key) and their child(ren) vertex id(s) (value). |
Notes
The tree is implemented using dictionaries for efficient vertex lookup and parent-child relationship management.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> child1 = tree.add_child(tree.root)
>>> child2 = tree.add_child(tree.root)
>>> grandchild = tree.add_child(child1)
>>> tree.nb_vertices()
>>> tree.is_leaf(grandchild)
>>> tree.parent(child1) == tree.root
Initialize a new Tree instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
int
|
The identifier for the root vertex. Default is |
0
|
tree
|
tree
|
Not used in this implementation, kept for compatibility. |
None
|
Source code in src/phyllotaxis_analysis/tree.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
root
property
writable
Link
root
Get the root vertex of the tree.
Returns:
| Type | Description |
|---|---|
int
|
The id of the root vertex of the tree. |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.root
0
__contains__ Link
__contains__(vid)
Check whether a vertex is present in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier to check for membership. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/phyllotaxis_analysis/tree.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
__iter__ Link
__iter__()
Iterate over the vertices of the tree.
This method returns an iterator over the vertices of the tree, allowing for easy traversal of the tree structure.
Yields:
| Type | Description |
|---|---|
Vertex
|
The next vertex in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
221 222 223 224 225 226 227 228 229 230 231 232 233 | |
__len__ Link
__len__()
Return the number of vertices in the tree.
This method returns the number of vertices in the tree by
delegating to the internal nb_vertices() method.
Returns:
| Type | Description |
|---|---|
int
|
The number of vertices in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
147 148 149 150 151 152 153 154 155 156 157 158 159 | |
add_child Link
add_child(parent, child=None, **kwargs)
Add a child vertex to the tree under the specified parent.
Adds a new child vertex to the tree structure. If no child identifier is provided, a new vertex is automatically created. The child is appended to the parent's list of children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
int
|
The identifier of the parent vertex to which the child will be attached. |
required |
child
|
int
|
The identifier of the child vertex to attach. If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The identifier of the child vertex that was added. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If the specified |
Notes
- If the
childidentifier already exists in the tree, it is reused. - If the
childidentifier does not exist, a new vertex is created and added to the tree.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
Source code in src/phyllotaxis_analysis/tree.py
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 | |
add_child_tree Link
add_child_tree(parent, tree)
Add a tree after the children of the parent vertex.
The tree is inserted such that its root becomes a child of the parent vertex, and all other vertices are recursively added as children of their corresponding parent vertices in the new tree. The operation has O(1) complexity when the input tree is a subtree of the current tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
int
|
Vertex identifier in the current tree where the new tree will be attached. |
required |
tree
|
tree
|
A rooted tree to be inserted as a subtree under the parent vertex. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A mapping from vertex identifiers in the input tree to their corresponding vertex identifiers in the current tree after insertion. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Source code in src/phyllotaxis_analysis/tree.py
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 | |
add_vertex Link
add_vertex(vid=None)
Add a vertex to the graph.
Adds a new vertex to the graph with the specified identifier or auto-increments the identifier if not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The identifier for the new vertex. If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The identifier of the added vertex. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If the provided |
Examples:
>>> from phyllotaxis_analysis.tree import InvalidVertex
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_vertex()
1
>>> tree.add_vertex(vid=5)
5
>>> try:
>>> tree.add_vertex(vid=3)
>>> except InvalidVertex as e:
>>> print(f"ERROR: {e}")
ERROR: 'Vertex identifier already used: 3'
Source code in src/phyllotaxis_analysis/tree.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
children Link
children(vid)
Return the children of a vertex in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose children are to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
A list of vertex identifiers that are children of |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.children(0)
[5, 6]
>>> tree.children(5)
[]
Source code in src/phyllotaxis_analysis/tree.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
clear Link
clear()
Remove all vertices and edges from the graph.
This method clears the tree structure by removing all child and parent relationships.
Notes
This method does not delete the root vertex itself, only its relationships.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.nb_vertices()
3
>>> g.clear()
>>> g.nb_vertices()
1
Source code in src/phyllotaxis_analysis/tree.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
copy Link
copy()
Deep copy of the tree.
Returns:
| Type | Description |
|---|---|
tree
|
A new instance of the tree with all attributes deeply copied. |
Source code in src/phyllotaxis_analysis/tree.py
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 | |
edges_iter Link
edges_iter()
Iterate over the edges of the tree.
Yields:
| Type | Description |
|---|---|
tuple[int, int]
|
A tuple of (parent, child) vertices representing each edge in the tree. |
Source code in src/phyllotaxis_analysis/tree.py
280 281 282 283 284 285 286 287 288 289 | |
has_vertex Link
has_vertex(vid)
Determines whether a given vertex is in the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int or str
|
The identifier of the vertex to be checked for existence in the graph. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Returns |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.has_vertex(1)
True
>>> g.has_vertex(2)
False
>>> g.has_vertex(6)
True
Source code in src/phyllotaxis_analysis/tree.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
insert_parent Link
insert_parent(vid, parent_id=None)
Insert a new vertex between an existing vertex and its parent.
This method modifies the tree structure by inserting parent_id as the new parent
of vid, effectively making parent_id a child of vid's original parent and
vid a child of parent_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose parent will be modified. |
required |
parent_id
|
int
|
The vertex identifier to insert as the new parent.
If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The vertex identifier of the newly inserted parent (either the provided
|
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Notes
- This operation changes the tree structure by reparenting
vidand adjusting the children lists of the affected vertices. - If
parent_idis not provided, a new vertex is created and used as the parent.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.children(0)
[5]
>>> tree.insert_parent(5, 6)
6
>>> tree.children(0)
[6]
>>> tree.children(6)
[5]
Source code in src/phyllotaxis_analysis/tree.py
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 | |
insert_sibling Link
insert_sibling(vid_1, vid_2=None)
Insert a new vertex as a sibling of an existing vertex in the tree.
The new vertex is inserted immediately before the specified vertex in the parent's children list. If no vertex identifier is provided for the new vertex, a new one is automatically generated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid_1
|
int
|
The vertex identifier of the existing vertex before which the new vertex will be inserted. |
required |
vid_2
|
int
|
The vertex identifier for the new vertex. If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The vertex identifier of the newly inserted vertex. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.insert_sibling(5, 6)
6
>>> tree.children(0)
[6, 5]
Source code in src/phyllotaxis_analysis/tree.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 | |
insert_sibling_tree Link
insert_sibling_tree(vid, tree)
Insert a tree as a sibling to the specified vertex.
Inserts a rooted tree as a sibling to the vertex identified by vid.
The root of the inserted tree and the vertex vid share the same parent.
The operation has O(1) complexity when the tree is obtained via self.subtree().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
Vertex identifier of the sibling vertex. |
required |
tree
|
Tree
|
A rooted tree to be inserted as a sibling. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A mapping from vertex identifiers in the original |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Source code in src/phyllotaxis_analysis/tree.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 | |
is_leaf Link
is_leaf(vid)
Test if a vertex is a leaf in the tree.
A leaf vertex is defined as a vertex with no children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The ID of the vertex to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.is_leaf(5)
True
>>> tree.is_leaf(0)
False
Source code in src/phyllotaxis_analysis/tree.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | |
nb_children Link
nb_children(vid)
Return the number of children of a given vertex in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose children count is requested. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of children of the vertex. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.nb_children(0)
2
Source code in src/phyllotaxis_analysis/tree.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
nb_siblings Link
nb_siblings(vid)
Return the number of siblings of a given vertex identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The identifier of the vertex whose siblings are to be counted. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of siblings of the vertex with the identifier |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.nb_siblings(5)
1
Source code in src/phyllotaxis_analysis/tree.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
nb_vertices Link
nb_vertices()
Returns the number of vertices in the graph.
Returns:
| Type | Description |
|---|---|
int
|
The number of vertices in the graph. |
See Also
nb_edges : Returns the number of edges in the graph.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex() # add a second vertices
>>> g.nb_vertices()
3
Source code in src/phyllotaxis_analysis/tree.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
parent Link
parent(vid)
Return the parent of a vertex in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose parent is to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
The parent vertex identifier. Returns |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If the vertex identifier |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.parent(5)
0
Source code in src/phyllotaxis_analysis/tree.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
remove_leaf Link
remove_leaf(vid)
Remove a leaf vertex from the tree.
The vertex identified by vid is removed only if it is a leaf (has no
children) and it is not the root of the tree. The parent–child
relationships stored in _children and _parent are updated
accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
Identifier of the vertex to be removed. |
required |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Notes
- The method mutates the internal
_childrenand_parentmappings of the tree instance. - Attempting to remove a vertex that does not exist in the internal structures has no effect.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0)
1
>>> tree.is_leaf(1)
True
>>> tree.remove_leaf(1) # removes leaf vertex 1
>>> tree.is_leaf(0)
True
>>> tree.remove_leaf(0) # cannot remove the root
phyllotaxis_analysis.tree.InvalidVertex: 'The root node 0 can not be removed.'
Source code in src/phyllotaxis_analysis/tree.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | |
remove_tree Link
remove_tree(vid)
Remove the subtree rooted on vid.
Remove the subtree rooted at the vertex with ID vid from the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex ID of the root of the subtree to remove. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The list of vertex IDs that were removed from the tree. |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
TypeError
|
If |
Source code in src/phyllotaxis_analysis/tree.py
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 | |
remove_vertex Link
remove_vertex(vid, reparent_child=False)
Remove a vertex from the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The id of the vertex to remove. |
required |
reparent_child
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The id of the removed vertex if it had no children, otherwise raises |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If the vertex to be removed is the root vertex or if it has children. |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.remove_vertex(1)
1
>>> g.remove_vertex(2)
phyllotaxis_analysis.tree.interface.InvalidVertex: 2
Source code in src/phyllotaxis_analysis/tree.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
replace_parent Link
replace_parent(vid, new_parent_id)
Change the parent of a vertex to a new parent vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex ID whose parent will be changed. |
required |
new_parent_id
|
int
|
The new parent vertex ID. |
required |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Notes
The old parent relationship is removed when the new parent is assigned.
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.children(0)
[5]
>>> tree.insert_parent(5, 6)
6
>>> tree.add_vertex(7)
7
>>> tree.replace_parent(5, 7)
>>> tree.parent(5)
7
>>> tree.children(0)
[6]
>>> tree.parent(7)
None
Source code in src/phyllotaxis_analysis/tree.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 | |
siblings Link
siblings(vid)
Return the siblings of a given vertex identifier in the tree.
Given a vertex identifier vid, this method returns a list of all sibling
vertices (i.e., vertices sharing the same parent). If the vertex has no
parent (i.e., it is the root), an empty list is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
The vertex identifier whose siblings are to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The list of vertex identifiers that are siblings of |
Raises:
| Type | Description |
|---|---|
InvalidVertex
|
If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0, 5)
5
>>> tree.add_child(0)
6
>>> tree.siblings(0)
[]
>>> tree.siblings(5)
[6]
Source code in src/phyllotaxis_analysis/tree.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
subtree Link
subtree(vid, copy=True)
Return the subtree containing all descendants of vid.
The induced subtree of the tree has the vertices in the subtree rooted at vid.
This creates a new tree that includes all vertices from vid and all its descendants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid
|
int
|
A vertex of the original tree. |
required |
copy
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Tree
|
A subtree of the tree. If |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> tree = Tree()
>>> tree.add_child(0)
1
>>> tree.insert_parent(1)
2
>>> tree.insert_sibling(1)
3
>>> subtree = tree.subtree(2, copy=True)
>>> subtree.vertices()
[0, 1, 2]
>>> subtree.root
0
'leaf1'
Source code in src/phyllotaxis_analysis/tree.py
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 | |
vertices Link
vertices()
Returns the list of vertices in the graph.
Returns:
| Type | Description |
|---|---|
list
|
The list of the vertices in the graph. |
Examples:
>>> from phyllotaxis_analysis.tree import Tree
>>> g = Tree() # initialize a new tree with a single vertex (the root)
>>> g.add_vertex() # add a first vertices
>>> g.add_vertex(6) # add a second vertices
>>> g.vertices()
[0, 1, 6]
Source code in src/phyllotaxis_analysis/tree.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
vertices_iter Link
vertices_iter()
Generates an iterator for the vertices in the graph.
This method is a generator that yields each vertex in the graph as a string, which corresponds to the key of the vertex in the internal dictionary representation of the graph. The order in which vertices are returned by this iterator is not guaranteed to be consistent between different calls or even different yields.
Yields:
| Type | Description |
|---|---|
str
|
A string representing the vertex in the graph. |
Source code in src/phyllotaxis_analysis/tree.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |