knit_graphs.directed_loop_graph module
Module containing directed loop graph class
- class Directed_Loop_Graph[source]
Bases:
Generic[LoopT,EdgeT]Wrapper for networkx.DiGraphs with directed edges between loops (i.e., floats in yarns, stitches in knitgraph).
- property contains_loops: bool
Returns: bool: True if the graph has at least one loop, False otherwise.
- property sorted_loops: list[LoopT]
Returns: list[Loop]: The list of loops in the graph sorted from the earliest formed loop to the latest formed loop.
- property edge_iter: Iterator[tuple[LoopT, LoopT, EdgeT]]
- Returns:
Iterator over the edges and edge-data in the graph.
- Return type:
Iterator[tuple[LoopT, LoopT, EdgeT]]
Notes
No guarantees about the order of the edges.
- property terminal_loops: Iterator[LoopT]
Returns: Iterator[Loop]: An iterator over all terminal loops in the graph.
- get_loop(loop_id: int) LoopT[source]
- Parameters:
loop_id (int) – The loop id of the loop to get from the graph.
- Returns:
The loop node in the graph.
- Return type:
LoopT
- is_terminal_loop(loop: LoopT) bool[source]
- Parameters:
loop (LoopT) – The loop to check for terminal status.
- Returns:
True if the loop has no child loops, False otherwise.
- Return type:
- get_child_loop(loop: LoopT) LoopT | None[source]
- Parameters:
loop (LoopT) – The loop to look for a child loop from.
- Returns:
The child loop if one exists, or None if no child loop is found.
- Return type:
LoopT | None
- ancestors(loop: LoopT) set[LoopT][source]
- Parameters:
loop (LoopT) – The loop to get the ancestors of from the graph.
- Returns:
The ancestors of the given loop.
- Return type:
set[LoopT]
- is_descendant(ancestor: LoopT, descendant: LoopT) bool[source]
- Parameters:
ancestor (LoopT) – The loop to check if it is an ancestor of the other loop.
descendant (LoopT) – The loop to check if it is a descendant of the other loop.
- Returns:
True if there is a directed path from the ancestor to the descendant, False otherwise.
- Return type:
- source_loops(loop: LoopT) set[LoopT][source]
- Parameters:
loop (LoopT) – The loop to get the sources of.
- Returns:
The source loops of the given loop. These are the loops that are the source of all ancestors to the given loop.
- Return type:
set[LoopT]
- dfs_edges(loop: LoopT) Iterator[tuple[LoopT, LoopT]][source]
- Parameters:
loop (LoopT) – The loop to start iteration over edges from.
- Returns:
The depth-first-search ordering of edges starting from the given loop.
- dfs_preorder_loops(loop: LoopT) Iterator[LoopT][source]
- Parameters:
loop (LoopT) – The loop to start iteration from.
- Returns:
The depth-first-search ordering of loops in the graph starting from the given loop.
- Return type:
Iterator[LoopT]
- add_loop(loop: LoopT) None[source]
Add the given loop to the loop graph. :param loop: The loop to add to the graph. :type loop: LoopT
- remove_loop(loop: LoopT | int) None[source]
Remove the given loop from the graph. :param loop: The loop or id of the loop to remove. :type loop: LoopT | int
- Raises:
KeyError – The given loop does not exist in the graph.
- add_edge(u: LoopT | int, v: LoopT | int, edge_data: EdgeT) None[source]
Connect the given loop u to the loop v with the given edge data. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int :param edge_data: The edge data to associate with the connection. :type edge_data: EdgeT
- Raises:
KeyError – Either of the given loops does not exist in the graph.
- remove_edge(u: LoopT | int, v: LoopT | int) None[source]
Removes the edge from loop u to the loop v from the graph. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int
- Raises:
KeyError – The given edge does not exist in the graph.
- __getitem__(item: int) LoopT[source]
- __getitem__(item: tuple[LoopT | int, LoopT | int]) EdgeT
- Parameters:
item (int | tuple[LoopT | int, LoopT | int]) – The id of the loop or the pair of loops that form an edge in the graph.
- Returns:
The loop associated with the given id. EdgeT: The edge data associated with the given pair of loops/ loop_ids.
- Return type:
LoopT
- Raises:
KeyError – The given loop or edge does not exist in the graph.
- __iter__() Iterator[LoopT][source]
- Returns:
Iterator over all the loops in the graph.
- Return type:
Iterator[LoopT]
Notes
No guarantees about order of loops. Expected to be insertion order.
- classmethod __class_getitem__(params)
Parameterizes a generic class.
At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.
However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….
- class Stitch_Edge(pull_direction: Pull_Direction)[source]
Bases:
objectCommon data about stitch edges.
- pull_direction: Pull_Direction
- __init__(pull_direction: Pull_Direction) None
- class Float_Edge(front_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>, back_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>)[source]
Bases:
Generic[LoopT]The edge data for float edges between loops on a yarn.
- loop_in_front_of_float(loop: LoopT) bool[source]
- Parameters:
loop (LoopT) – The loop to find relative to this float.
- Returns:
True if the loop is in the front of the float.
- Return type:
- loop_behind_float(loop: LoopT) bool[source]
- Parameters:
loop (LoopT) – The loop to find relative to this float.
- Returns:
True if the loop is behind the float.
- Return type:
- add_loop_in_front_of_float(loop: LoopT) None[source]
Adds the given loop to the set of loops in front of this float. If the loop was behind the float, it is swapped to be in front. :param loop: The loop to put in front of this float. :type loop: LoopT
- add_loop_behind_float(back_loop: LoopT) None[source]
Adds the given loop to the set of loops behind this float. If the loop was in front of this float, it is swapped to be behind. :param back_loop: The loop to put behind this float. :type back_loop: LoopT
- classmethod __class_getitem__(params)
Parameterizes a generic class.
At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.
However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….
- __init__(front_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>, back_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>) None