knit_graphs.Knit_Graph module

The graph structure used to represent knitted objects.

This module contains the main Knit_Graph class which serves as the central data structure for representing knitted fabrics. It manages the relationships between loops, yarns, and structural elements like courses and wales.

class Knit_Graph[source]

Bases: object

A representation of knitted structures as connections between loops on yarns.

The Knit_Graph class is the main data structure for representing knitted fabrics. It maintains a directed graph of loops connected by stitch edges, manages yarn relationships, and provides methods for analyzing the structure of the knitted fabric including courses, wales, and cable crossings.

__init__()[source]

Initialize an empty knit graph with no loops or yarns.

property last_loop: None | Loop

Get the most recently added loop in the graph.

Returns:

The last loop added to the graph, or None if no loops have been added.

Return type:

None | Loop

property has_loop: bool

Check if the graph contains any loops.

Returns:

True if the graph has at least one loop, False otherwise.

Return type:

bool

add_crossing(left_loop, right_loop, crossing_direction)[source]

Add a cable crossing between two loops with the specified crossing direction.

Parameters:
  • left_loop (Loop) – The loop on the left side of the crossing.

  • right_loop (Loop) – The loop on the right side of the crossing.

  • crossing_direction (Crossing_Direction) – The direction of the crossing (over, under, or none) between the loops.

Return type:

None

add_loop(loop)[source]

Add a loop to the knit graph as a node.

Parameters:

loop (Loop) – The loop to be added as a node in the graph. If the loop’s yarn is not already in the graph, it will be added automatically.

Return type:

None

add_yarn(yarn)[source]

Add a yarn to the graph without adding its loops.

Parameters:

yarn (Yarn) – The yarn to be added to the graph structure. This method assumes that loops do not need to be added separately.

Return type:

None

connect_loops(parent_loop, child_loop, pull_direction=BtF, stack_position=None)[source]

Create a stitch edge by connecting a parent and child loop.

Parameters:
  • parent_loop (Loop) – The parent loop to connect to the child loop.

  • child_loop (Loop) – The child loop to connect to the parent loop.

  • pull_direction (Pull_Direction) – The direction the child is pulled through the parent. Defaults to Pull_Direction.BtF (knit stitch).

  • stack_position (int | None, optional) – The position to insert the parent into the child’s parent stack. If None, adds on top of the stack. Defaults to None.

Raises:

KeyError – If either the parent_loop or child_loop is not already in the knit graph.

Return type:

None

get_wale_starting_with_loop(first_loop)[source]

Get a wale (vertical column of stitches) starting from the specified loop.

Parameters:

first_loop (Loop) – The loop at the start of the wale to be constructed.

Returns:

A wale object representing the vertical column of stitches starting from the given loop.

Return type:

Wale

get_wales_ending_with_loop(last_loop)[source]

Get all wales (vertical columns of stitches) that end at the specified loop.

Parameters:

last_loop (Loop) – The last loop of the joined set of wales.

Returns:

The set of wales that end at this loop. Only returns multiple wales if this loop is a child of a decrease stitch.

Return type:

list[Wale]

get_courses()[source]

Get all courses (horizontal rows) in the knit graph in chronological order.

Returns:

A list of courses representing horizontal rows of loops. The first course contains the initial set of loops. A course change occurs when a loop has a parent loop in the previous course.

Return type:

list[Course]

get_wale_groups()[source]

Get wale groups organized by their terminal loops.

Returns:

Dictionary mapping terminal loops to the wale groups they terminate. Each wale group represents a collection of wales that end at the same terminal loop.

Return type:

dict[Loop, Wale_Group]

__contains__(item)[source]

Check if a loop is contained in the knit graph.

Parameters:

item (Loop | tuple[Loop, Loop]) – The loop being checked for in the graph or the parent-child stitch edge to check for in the knit graph.

Returns:

True if the given loop or stitch edge is in the graph, False otherwise.

Return type:

bool

__iter__()[source]
Returns:

An iterator over all loops in the knit graph.

Return type:

Iterator[Loop]

sorted_loops()[source]
Returns:

The list of loops in the stitch graph sorted from the earliest formed loop to the latest formed loop.

Return type:

list[Loop]

get_pull_direction(parent, child)[source]

Get the pull direction of the stitch edge between parent and child loops.

Parameters:
  • parent (Loop) – The parent loop of the stitch edge.

  • child (Loop) – The child loop of the stitch edge.

Returns:

The pull direction of the stitch-edge between the parent and child, or None if there is no edge between these loops.

Return type:

Pull_Direction | None

get_stitch_edge(parent, child)[source]

Get the stitch edge data between two loops.

Parameters:
  • parent (Loop) – The parent loop of the stitch edge.

  • child (Loop) – The child loop of the stitch edge.

Returns:

The edge data dictionary for this stitch edge, or None if no edge exists between these loops.

Return type:

dict[str, Any] | None

get_child_loop(loop)[source]

Get the child loop of the specified parent loop.

Parameters:

loop (Loop) – 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:

Loop | None

has_child_loop(loop)[source]

Check if a loop has a child loop connected to it.

Parameters:

loop (Loop) – The loop to check for child connections.

Returns:

True if the loop has a child loop, False otherwise.

Return type:

bool

is_terminal_loop(loop)[source]

Check if a loop is terminal (has no child loops and terminates a wale).

Parameters:

loop (Loop) – The loop to check for terminal status.

Returns:

True if the loop has no child loops and terminates a wale, False otherwise.

Return type:

bool