knit_graphs.Knit_Graph_Visualizer module

Module used to visualize a Knit graph as an SVG file.

This module provides comprehensive visualization capabilities for knit graphs by rendering them as SVG. It handles the positioning of loops, rendering of yarn paths, stitch edges, and cable crossings to create 2D visualizations of knitted structures.

class TraceData[source]

Bases: TypedDict, Generic[LoopT]

Typing specification for stitch edge data.

x: list[float | None]
y: list[float | None]
edge: list[tuple[LoopT, LoopT]]
is_start: list[bool]
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__(*args, **kwargs)
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
class Knit_Graph_Visualizer(knit_graph: Knit_Graph[LoopT], first_course_index: int = 0, top_course_index: int | None = None, start_on_left: bool = True, balance_by_base_width: bool = False, left_zero_align: bool = True)[source]

Bases: Generic[LoopT]

A class used to visualize a knit graph by rendering it as an SVG file.

This class converts knit graph data structures into SVG visualizations by calculating loop positions, rendering yarn paths as Bézier curves, and displaying stitch relationships with appropriate styling for different stitch types and cable crossings.

knit_graph

The knit graph to visualize.

Type:

Knit_Graph

courses

List of courses (horizontal rows) in the knit graph.

Type:

list[Course]

base_width

The width of the base course used for scaling.

Type:

float

base_left

The leftmost position of the base course.

Type:

float

loops_to_course

Mapping from loops to their containing courses.

Type:

dict[Loop, Course]

data_graph

Internal graph for storing loop positions and visualization data.

Type:

DiGraph

left_zero_align

Whether to align the left edge of courses to zero.

Type:

bool

balance_by_base_width

Whether to scale course widths to match the base course.

Type:

bool

start_on_left

Whether to start knitting visualization from the left side.

Type:

bool

top_course_index

The index of the topmost course to visualize.

Type:

int

first_course_index

The index of the first (bottom) course to visualize.

Type:

int

__init__(knit_graph: Knit_Graph[LoopT], first_course_index: int = 0, top_course_index: int | None = None, start_on_left: bool = True, balance_by_base_width: bool = False, left_zero_align: bool = True)[source]

Initialize the knit graph SVG visualizer with specified configuration options.

Parameters:
  • knit_graph – The knit graph to be visualized.

  • first_course_index – The index of the first course to include. Defaults to 0.

  • top_course_index – The index of the last course to include. If None, includes all.

  • start_on_left – Whether to position the first loop on the left side. Defaults to True.

  • balance_by_base_width – Whether to scale all course widths to match the base. Defaults to False.

  • left_zero_align – Whether to align the leftmost loop of each course to x=0. Defaults to True.

make_svg(graph_title: str = 'Knit Graph', scale: float = 80.0, loop_radius: float = 0.15, padding: float = 0.6, knit_color: str = 'blue', purl_color: str = 'red', font_size: float = 0.14, loop_border_width: float = 0.02, yarn_line_width: float = 0.02, background_color: str | None = None) str[source]

Generate an SVG string visualizing this knit graph.

Parameters:
  • graph_title – Title text rendered at the top of the SVG.

  • scale – Pixels per graph-coordinate unit. Controls overall image size.

  • loop_radius – Radius of loop circles in graph-coordinate units.

  • padding – Extra space around the content in graph-coordinate units.

  • knit_color – Stroke color for knit-stitch edges.

  • purl_color – Stroke color for purl-stitch edges.

  • font_size – Font size for loop id labels in graph-coordinate units.

  • loop_border_width – Stroke width of loop circle borders in graph-coordinate units.

  • yarn_line_width – Stroke width of yarn traces in graph-coordinate units.

  • background_color – Optional background rectangle color. None for transparent.

Returns:

The SVG document as a string.

save_svg(filepath: str | Path = 'knit_graph.svg', **kwargs: Any) Path[source]

Generate and save the SVG visualization to a file.

Parameters:
  • filepath – The path to write the SVG file. Defaults to “knit_graph.svg”.

  • **kwargs – Additional keyword arguments forwarded to make_svg().

Returns:

The resolved Path where the file was written.

x_coordinate_differences(other: Knit_Graph_Visualizer[LoopT]) dict[LoopT, tuple[float | None, float | None]][source]

Find the differences in x-coordinates between two visualizations.

Parameters:

other – The visualization to compare to.

Returns:

A dict mapping loops with coordinate differences to (self_x, other_x) tuples.

y_coordinate_differences(other: Knit_Graph_Visualizer[LoopT]) dict[LoopT, tuple[float | None, float | None]][source]

Find the differences in y-coordinates between two visualizations.

Parameters:

other – The visualization to compare to.

Returns:

A dict mapping loops with coordinate differences to (self_y, other_y) tuples.

__eq__(other: object) bool[source]

Two visualizations are equal if they share the same coordinates for all loops.

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]): ….

visualize_knit_graph(knit_graph: Knit_Graph, first_course_index: int = 0, top_course_index: int | None = None, start_on_left: bool = True, balance_by_base_width: bool = False, left_zero_align: bool = True, graph_title: str = 'knit_graph', filepath: str | Path = 'knit_graph.svg', **svg_kwargs: Any) Path[source]

Generate and save an SVG visualization of the given knit graph.

Parameters:
  • knit_graph – The knit graph to visualize.

  • first_course_index – Index of the first (bottom) course. Defaults to 0.

  • top_course_index – Index of the last (top) course. If None, visualizes all.

  • start_on_left – Whether the first loop is on the left. Defaults to True.

  • balance_by_base_width – Whether to scale course widths to match the base. Defaults to False.

  • left_zero_align – Whether to left-align each course to x=0. Defaults to True.

  • graph_title – Title displayed on the SVG. Defaults to “knit_graph”.

  • filepath – Output file path. Defaults to “knit_graph.svg”.

  • **svg_kwargs – Additional keyword arguments forwarded to Knit_Graph_SVG_Visualizer.make_svg().

Returns:

The resolved Path where the SVG file was written.