knit-graphs 0.0.6__py3-none-any.whl → 0.0.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. knit_graphs-0.0.6.dist-info/licenses/LICENSE → LICENSE +21 -21
  2. README.md +75 -0
  3. docs/Makefile +20 -0
  4. docs/make.bat +35 -0
  5. docs/source/api/knit_graphs.artin_wale_braids.rst +58 -0
  6. docs/source/api/knit_graphs.rst +74 -0
  7. docs/source/conf.py +335 -0
  8. docs/source/index.rst +71 -0
  9. docs/source/installation.rst +67 -0
  10. knit_graphs/Course.py +156 -104
  11. knit_graphs/Knit_Graph.py +249 -186
  12. knit_graphs/Knit_Graph_Visualizer.py +680 -0
  13. knit_graphs/Loop.py +141 -155
  14. knit_graphs/Pull_Direction.py +68 -23
  15. knit_graphs/Yarn.py +424 -267
  16. knit_graphs/__init__.py +3 -3
  17. knit_graphs/_base_classes.py +173 -0
  18. knit_graphs/artin_wale_braids/Crossing_Direction.py +74 -15
  19. knit_graphs/artin_wale_braids/Loop_Braid_Graph.py +95 -62
  20. knit_graphs/artin_wale_braids/Wale.py +169 -93
  21. knit_graphs/artin_wale_braids/Wale_Braid.py +50 -30
  22. knit_graphs/artin_wale_braids/Wale_Braid_Word.py +99 -54
  23. knit_graphs/artin_wale_braids/Wale_Group.py +136 -88
  24. knit_graphs/{knit_graph_generators/basic_knit_graph_generators.py → basic_knit_graph_generators.py} +302 -248
  25. knit_graphs-0.0.7.dist-info/LICENSE +21 -0
  26. {knit_graphs-0.0.6.dist-info → knit_graphs-0.0.7.dist-info}/METADATA +33 -24
  27. knit_graphs-0.0.7.dist-info/RECORD +29 -0
  28. {knit_graphs-0.0.6.dist-info → knit_graphs-0.0.7.dist-info}/WHEEL +1 -1
  29. knit_graphs/__about__.py +0 -4
  30. knit_graphs/knit_graph_generators/__init__.py +0 -0
  31. knit_graphs/knit_graph_visualizer/Stitch_Visualizer.py +0 -427
  32. knit_graphs/knit_graph_visualizer/__init__.py +0 -0
  33. knit_graphs-0.0.6.dist-info/RECORD +0 -22
@@ -1,88 +1,136 @@
1
- """Module containing the Wale_Group class and its methods."""
2
- import networkx
3
-
4
- from knit_graphs.Loop import Loop
5
- from knit_graphs.artin_wale_braids.Wale import Wale
6
-
7
-
8
- class Wale_Group:
9
- """
10
- A graphs structure maintaining the relationship between connected wales through decreases
11
- """
12
-
13
- def __init__(self, terminal_wale: Wale, knit_graph):
14
- self.wale_graph: networkx.DiGraph = networkx.DiGraph()
15
- self.stitch_graph: networkx.DiGraph = networkx.DiGraph()
16
- self.terminal_wale: Wale | None = terminal_wale
17
- self.top_loops: dict[Loop, Wale] = {}
18
- self.bottom_loops: dict[Loop, Wale] = {}
19
- self.build_group_from_top_wale(terminal_wale, knit_graph)
20
-
21
- def add_wale(self, wale: Wale):
22
- """
23
- :param wale: Adds wale to group and connects by di-graph by position of shared loops.
24
- """
25
- self.wale_graph.add_node(wale)
26
- for u, v in wale.stitches.edges:
27
- self.stitch_graph.add_edge(u, v, pull_direction=wale.stitches.get_edge_data(u, v, "pull_direction"))
28
- for top_loop, other_wale in self.top_loops.items():
29
- if top_loop == wale.first_loop:
30
- self.wale_graph.add_edge(other_wale, wale)
31
- for bot_loop, other_wale in self.bottom_loops.items():
32
- if bot_loop == wale.last_loop:
33
- self.wale_graph.add_edge(wale, other_wale)
34
- self.top_loops[wale.last_loop] = wale
35
- self.bottom_loops[wale.first_loop] = wale
36
-
37
- def add_parent_wales(self, wale: Wale, knit_graph) -> list[Wale]:
38
- """
39
- Add parent wales that created the given wale to a wale group.
40
- :param wale: Wale to find parents from.
41
- :param knit_graph: Knit graph used to derive stitch relationships.
42
- :return: List of wales that were added.
43
- """
44
- added_wales = []
45
- for parent_loop in wale.first_loop.parent_loops:
46
- parent_wales = knit_graph.get_wales_ending_with_loop(parent_loop)
47
- for parent_wale in parent_wales:
48
- self.add_wale(parent_wale)
49
- added_wales.extend(parent_wales)
50
- return added_wales
51
-
52
- def build_group_from_top_wale(self, top_wale: Wale, knit_graph):
53
- """
54
- Builds out a wale group by finding parent wales from top wale provided
55
- :param top_wale: top of a wale tree.
56
- :param knit_graph: Knit graph used to derive stitch relationships.
57
- """
58
- self.add_wale(top_wale)
59
- added_wales = self.add_parent_wales(top_wale, knit_graph)
60
- while len(added_wales) > 0:
61
- next_wale = added_wales.pop()
62
- more_wales = self.add_parent_wales(next_wale, knit_graph)
63
- added_wales.extend(more_wales)
64
-
65
- def get_loops_over_courses(self) -> list[list[Loop]]:
66
- """
67
- :return: List of lists of loops that are in the same course by wales
68
- """
69
- top_loop = self.terminal_wale.last_loop
70
- courses = []
71
- cur_course = [top_loop]
72
- while len(cur_course) > 0:
73
- courses.append(cur_course)
74
- next_course = []
75
- for loop in cur_course:
76
- next_course.extend(self.stitch_graph.predecessors(loop))
77
- cur_course = next_course
78
- return courses
79
-
80
- def __len__(self):
81
- """
82
- :return: height of the wale group from base loop to the tallest terminal
83
- """
84
- max_len = 0
85
- for bot_loop, wale in self.bottom_loops.items():
86
- path_len = sum(len(successor) for successor in networkx.dfs_preorder_nodes(self.wale_graph, wale))
87
- max_len = max(max_len, path_len)
88
- return max_len
1
+ """Module containing the Wale_Group class and its methods.
2
+
3
+ This module provides the Wale_Group class which represents a collection of interconnected wales that are joined through decrease operations, forming a tree-like structure of vertical stitch columns.
4
+ """
5
+ from __future__ import annotations
6
+
7
+ from typing import cast
8
+
9
+ from networkx import DiGraph, dfs_preorder_nodes
10
+
11
+ from knit_graphs._base_classes import _Base_Knit_Graph
12
+ from knit_graphs.artin_wale_braids.Wale import Wale
13
+ from knit_graphs.Loop import Loop
14
+
15
+
16
+ class Wale_Group:
17
+ """A graph structure maintaining relationships between connected wales through decrease operations.
18
+
19
+ This class represents a collection of wales that are connected through decrease stitches, where multiple wales merge into fewer wales as the knitting progresses upward.
20
+ It maintains both the wale-to-wale relationships and the individual stitch connections within the group.
21
+
22
+ Attributes:
23
+ wale_graph (DiGraph): A directed graph representing the relationships between wales in this group.
24
+ stitch_graph (DiGraph): A directed graph of all individual stitch connections within this wale group.
25
+ terminal_wale (Wale | None): The topmost wale in this group, typically where multiple wales converge.
26
+ top_loops (dict[Loop, Wale]): Mapping from the last (top) loop of each wale to the wale itself.
27
+ bottom_loops (dict[Loop, Wale]): Mapping from the first (bottom) loop of each wale to the wale itself.
28
+ """
29
+
30
+ def __init__(self, terminal_wale: Wale, knit_graph: _Base_Knit_Graph):
31
+ """Initialize a wale group starting from a terminal wale and building downward.
32
+
33
+ Args:
34
+ terminal_wale (Wale): The topmost wale in the group, used as the starting point for building the complete group structure.
35
+ knit_graph (Knit_Graph): The parent knit graph that contains this wale group.
36
+ """
37
+ self.wale_graph: DiGraph = DiGraph()
38
+ self.stitch_graph: DiGraph = DiGraph()
39
+ self._knit_graph: _Base_Knit_Graph = knit_graph
40
+ self.terminal_wale: Wale | None = terminal_wale
41
+ self.top_loops: dict[Loop, Wale] = {}
42
+ self.bottom_loops: dict[Loop, Wale] = {}
43
+ self.build_group_from_top_wale(terminal_wale)
44
+
45
+ def add_wale(self, wale: Wale) -> None:
46
+ """Add a wale to the group and connect it to existing wales through shared loops.
47
+
48
+ This method adds the wale to the group's graphs and establishes connections with other wales based on shared loops at their endpoints.
49
+
50
+ Args:
51
+ wale (Wale): The wale to add to this group. Empty wales are ignored and not added.
52
+ """
53
+ if len(wale) == 0:
54
+ return # This wale is empty and therefore there is nothing to add to the wale group
55
+ self.wale_graph.add_node(wale)
56
+ for u, v in wale.stitches.edges:
57
+ self.stitch_graph.add_edge(u, v, pull_direction=wale.get_stitch_pull_direction(u, v))
58
+ for top_loop, other_wale in self.top_loops.items():
59
+ if top_loop == wale.first_loop:
60
+ self.wale_graph.add_edge(other_wale, wale)
61
+ for bot_loop, other_wale in self.bottom_loops.items():
62
+ if bot_loop == wale.last_loop:
63
+ self.wale_graph.add_edge(wale, other_wale)
64
+ assert isinstance(wale.last_loop, Loop)
65
+ self.top_loops[wale.last_loop] = wale
66
+ assert isinstance(wale.first_loop, Loop)
67
+ self.bottom_loops[wale.first_loop] = wale
68
+
69
+ def add_parent_wales(self, wale: Wale) -> list[Wale]:
70
+ """Find and add all parent wales that created the given wale through decrease operations.
71
+
72
+ This method identifies wales that end at the loops that are parents of this wale's first loop, representing the wales that were decreased together to form the given wale.
73
+
74
+ Args:
75
+ wale (Wale): The wale to find and add parent wales for.
76
+
77
+ Returns:
78
+ list[Wale]: The list of parent wales that were found and added to the group.
79
+ """
80
+ added_wales = []
81
+ for parent_loop in cast(Loop, wale.first_loop).parent_loops:
82
+ parent_wales = cast(list[Wale], self._knit_graph.get_wales_ending_with_loop(parent_loop))
83
+ for parent_wale in parent_wales:
84
+ self.add_wale(parent_wale)
85
+ added_wales.extend(parent_wales)
86
+ return added_wales
87
+
88
+ def build_group_from_top_wale(self, top_wale: Wale) -> None:
89
+ """Build the complete wale group by recursively finding all parent wales from the terminal wale.
90
+
91
+ This method starts with the terminal wale and recursively adds all parent wales, building the complete tree structure of wales that contribute to the terminal wale through decrease operations.
92
+
93
+ Args:
94
+ top_wale (Wale): The terminal wale at the top of the group structure.
95
+ """
96
+ self.add_wale(top_wale)
97
+ added_wales = self.add_parent_wales(top_wale)
98
+ while len(added_wales) > 0:
99
+ next_wale = added_wales.pop()
100
+ more_wales = self.add_parent_wales(next_wale)
101
+ added_wales.extend(more_wales)
102
+
103
+ def get_loops_over_courses(self) -> list[list[Loop]]:
104
+ """Get loops organized by their course (horizontal row) within this wale group.
105
+
106
+ This method traces through the stitch connections starting from the terminal wale's top loop and groups loops by their vertical position (course) in the knitted structure.
107
+
108
+ Returns:
109
+ list[list[Loop]]: A list where each inner list contains all loops that belong to the same course, ordered from top to bottom courses. Returns empty list if there is no terminal wale.
110
+ """
111
+ if self.terminal_wale is None:
112
+ return []
113
+ top_loop: Loop = cast(Loop, self.terminal_wale.last_loop)
114
+ courses: list[list[Loop]] = []
115
+ cur_course: list[Loop] = [top_loop]
116
+ while len(cur_course) > 0:
117
+ courses.append(cur_course)
118
+ next_course = []
119
+ for loop in cur_course:
120
+ next_course.extend(self.stitch_graph.predecessors(loop))
121
+ cur_course = next_course
122
+ return courses
123
+
124
+ def __len__(self) -> int:
125
+ """Get the height of the wale group measured as the maximum number of loops from base to terminal.
126
+
127
+ This method calculates the total length by summing all loops in all wales that can be reached from each bottom wale, returning the maximum total length found.
128
+
129
+ Returns:
130
+ int: The height of the wale group from the base loops to the tallest terminal, measured in total number of loops.
131
+ """
132
+ max_len = 0
133
+ for bot_loop, wale in self.bottom_loops.items():
134
+ path_len = sum(len(successor) for successor in dfs_preorder_nodes(self.wale_graph, wale))
135
+ max_len = max(max_len, path_len)
136
+ return max_len