knit_graphs.Knit_Graph_Visualizer module

Module used to visualize a Knit graph with the plotly graph object library.

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

configure_plotly_environment()[source]

Auto-configure Plotly based on environment detection to avoid socket issues.

Return type:

None

class Knit_Graph_Visualizer(knit_graph, first_course_index=0, top_course_index=None, start_on_left=True, balance_by_base_width=False, left_zero_align=True)[source]

Bases: object

A class used to visualize a knit graph using the plotly graph objects library.

This class converts knit graph data structures into interactive 2D visualizations by calculating loop positions, rendering yarn paths, 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

Parameters:
__init__(knit_graph, first_course_index=0, top_course_index=None, start_on_left=True, balance_by_base_width=False, left_zero_align=True)[source]

Initialize the knit graph visualizer with specified configuration options.

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

  • first_course_index (int, optional) – The index of the first course to include in the visualization. Defaults to 0.

  • top_course_index (int | None, optional) – The index of the last course to include in the visualization. If None, includes all courses up to the top.

  • start_on_left (bool, optional) – Whether to position the first loop on the left side of the visualization. Defaults to True.

  • balance_by_base_width (bool, optional) – Whether to scale all course widths to match the base course width. Defaults to False.

  • left_zero_align (bool, optional) – Whether to align the leftmost loop of each course to x=0. Defaults to True.

make_figure(graph_title='Knit Graph')[source]

Generate the interactive figure to visualize this knit graph.

Parameters:

graph_title (str, optional) – The title to display on the figure. Defaults to “Knit Graph”.

Returns:

The plotly figure object.

Return type:

Figure

show_figure(graph_title='Knit Graph', renderer=None)[source]

Generate and display the interactive figure to visualize this knit graph.

Parameters:
  • graph_title (str, optional) – The title to display on the figure. Defaults to “Knit Graph”.

  • renderer (str, optional) – Plotly renderer to use. If None, uses the default configured renderer.

Return type:

None

x_coordinate_differences(other)[source]

Find the differences in x-coordinates between two knitgraph visualizations. Used for testing and comparing visualization results.

Parameters:

other (Knit_Graph_Visualizer) – The knitgraph visualization to compare to.

Returns:

tuple[float | None, float | None]]: The differences in x-coordinates. * The keys of the dictionary are loops with differences between the two visualizations. * The values of the dictionary are tuples of floats or None. ** The first value in each tuple is the x-coordinate of the loop in this knitgraph visualization or None if the loop is not in this visualization. ** The second value of each tuple is the x-coordinate of the loop in the other visualization or NOne if the loop is not in that visualization.

Return type:

dict[Loop

y_coordinate_differences(other)[source]

Find the differences in y-coordinates between two knitgraph visualizations. Used for testing and comparing visualization results.

Parameters:

other (Knit_Graph_Visualizer) – The knitgraph visualization to compare to.

Returns:

tuple[float | None, float | None]]: The differences in y-coordinates. * The keys of the dictionary are loops with differences between the two visualizations. * The values of the dictionary are tuples of floats or None. ** The first value in each tuple is the y-coordinate of the loop in this knitgraph visualization or None if the loop is not in this visualization. ** The second value of each tuple is the y-coordinate of the loop in the other visualization or NOne if the loop is not in that visualization.

Return type:

dict[Loop

__eq__(other)[source]

Two visualizations are equal if share the same x,y coordinates for all loops in the visualization and both contain the same set of loop nodes. :type other: Knit_Graph_Visualizer :param other: The knitgraph visualization to compare to. :type other: Knit_Graph_Visualizer

Returns:

True if the knitgraph visualizations are equal, False otherwise.

Return type:

bool

visualize_knit_graph(knit_graph, first_course_index=0, top_course_index=None, start_on_left=True, balance_by_base_width=False, left_zero_align=True, graph_title='knit_graph', show_figure=True, renderer=None)[source]

Generate and optionally display a plotly visualization of the given knit graph with specified configuration.

Parameters:
  • knit_graph (Knit_Graph) – The knit graph to visualize.

  • first_course_index (int) – Index of the first (bottom) course to include in the visualization. Defaults to 0.

  • top_course_index (int | None) – Index of the last (top) course to include in the visualization. If None, visualizes up to the top of the knit graph.

  • start_on_left (bool) – Whether the first loop knit is presumed to be positioned on the left of the pattern. Defaults to True.

  • balance_by_base_width (bool) – Whether to scale all course widths to match the first course width. Defaults to False.

  • left_zero_align (bool) – Whether to align the leftmost position of each course with x=0 in the figure. Defaults to True.

  • graph_title (str) – The title to display on the generated figure. Defaults to “knit_graph”.

  • show_figure (bool, optional) – If True, the visualization will be shown. Defaults to True.

  • renderer (str, optional) – Plotly renderer to use. Common options: ‘browser’, ‘notebook’. Defaults to None (uses configured default).

Returns:

The plotly figure object.

Return type:

Figure

visualize_knit_graph_safe(knit_graph, first_course_index=0, top_course_index=None, start_on_left=True, balance_by_base_width=False, left_zero_align=True, graph_title='knit_graph')[source]

Generate a plotly visualization of the given knit graph with specified configuration. This function is safe for UnitTest and other headless environments because it does not attempt to show the visualization.

Parameters:
  • knit_graph (Knit_Graph) – The knit graph to visualize.

  • first_course_index (int) – Index of the first (bottom) course to include in the visualization. Defaults to 0.

  • top_course_index (int | None) – Index of the last (top) course to include in the visualization. If None, visualizes up to the top of the knit graph.

  • start_on_left (bool) – Whether the first loop knit is presumed to be positioned on the left of the pattern. Defaults to True.

  • balance_by_base_width (bool) – Whether to scale all course widths to match the first course width. Defaults to False.

  • left_zero_align (bool) – Whether to align the leftmost position of each course with x=0 in the figure. Defaults to True.

  • graph_title (str) – The title to display on the generated figure. Defaults to “knit_graph”.

Returns:

The plotly figure object.

Return type:

Figure