draw
dsslab.net_bench.draw
Module for drawing network images through method chaining.
NOT_IMPLEMENTED = NotImplementedError('This feature is yet to be implemented.')
module-attribute
logger = get_logger(__name__)
module-attribute
Description()
Class containing description drawing preferences.
alpha = 0.5
instance-attribute
size = 8
instance-attribute
text = ''
instance-attribute
set_text(text)
Set the description setting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to set as description |
required |
Edges(labels=None)
Bases: GraphElement
alphas = fixed(1)
instance-attribute
arrow_size = 2
instance-attribute
colors = fixed('lightgrey')
instance-attribute
labels = labels
instance-attribute
sizes = fixed(0.5)
instance-attribute
set_alphas(arg)
Set alpha based on argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
set_colors(arg)
Set the colors of the displayed nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | str
|
Colors to set. String values will be mapped onto all nodes. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_sizes(arg)
Set size of labels for nodes as pt sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | int | float
|
Font size for text labels |
required |
Returns: self
set_transparency(arg)
Set transparency based on argument.
This is the same as set_alphas(). Alpha is the value for the transparency of a
color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
GraphElement
set_alphas(arg)
Set alpha based on argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
set_colors(arg)
Set the colors of the displayed nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | str
|
Colors to set. String values will be mapped onto all nodes. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_sizes(arg)
Set size of labels for nodes as pt sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | int | float
|
Font size for text labels |
required |
Returns: self
set_transparency(arg)
Set transparency based on argument.
This is the same as set_alphas(). Alpha is the value for the transparency of a
color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
ImageGenerator(graph)
Base class for setting up image generation.
axis = None
instance-attribute
axlimit = 1.05
instance-attribute
canvas_height = 10
instance-attribute
canvas_right = None
instance-attribute
canvas_width = 10
instance-attribute
continous_cmap = mpl.colormaps['viridis']
instance-attribute
description = Description()
instance-attribute
dpi = 200
instance-attribute
edges = Edges()
instance-attribute
graph = graph
instance-attribute
img_dir = Path('.')
instance-attribute
legends = LegendGenerator()
instance-attribute
nodes = Nodes(Labels())
instance-attribute
qualitative_cmap = mpl.colormaps['tab10']
instance-attribute
change_graph(graph)
Set the graph attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
A NetworkX graph object. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
deepcopy()
Create deep copy of the object.
This is the same as calling copy.deepcopy() on the object
draw()
draw_description()
Draw description below the image according to the settings.
draw_edges()
Draw edges according to the settings.
draw_labels()
Draw labels based on values.
draw_nodes()
Draw nodes according to the settings.
set_axis(axis)
Set an existing matplotlib axis object as ImageGenerator object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
Matplotlib axis |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_legend(mapping, graph_element=None, label=None)
Set a legend.
Legends consist of textboxes and colorbars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Sequential | Qualitative
|
mapping strategy for the basis of the legend |
required |
graph_element
|
Literal['edges', 'nodes', None]
|
the graph element to be modeled, necessary for generating textbox legends. |
None
|
label
|
str | None
|
optional label to be applied to the legend |
None
|
Returns:
| Type | Description |
|---|---|
|
self |
set_legends(legends)
Set the legends setting.
Legends consist of textboxes and colorbars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
legends
|
LegendGenerator
|
LegendGenerator object which contains all textboxes and/or colorbars to be drawn |
required |
Returns:
| Type | Description |
|---|---|
|
self |
write_file(path)
Write file to disk on the given path.
Will also close the internal figure object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
str | Path: Path to write the file to. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
write_json(path)
Write the graph data to a json file.
This is following nx.node_link_data format as shown here: https://networkx.org/documentation/stable/reference/readwrite/generated/networkx.readwrite.json_graph.node_link_data.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
saving location and name for the json-file |
required |
Returns:
| Type | Description |
|---|---|
ImageGenerator
|
self |
Labels()
Bases: GraphElement
alphas = None
instance-attribute
colors = None
instance-attribute
font_families = None
instance-attribute
labels = []
instance-attribute
show_labels = False
instance-attribute
sizes = None
instance-attribute
set_alphas(arg)
Set alpha based on argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
set_colors(arg)
Set the colors of the displayed nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | str
|
Colors to set. String values will be mapped onto all nodes. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_font_families(arg)
Set font family for all labels if single font is passed,.
Allows for multiple fonts to be set if an array of fonts is passed, allows for fonts to be individually set for labels based on the given node if a dictionary is passed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | str
|
Font family |
required |
set_labels(arg)
Set labels for nodes based on arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
dict
|
node identifier as the integer and the label as the string |
required |
set_sizes(arg)
Set size of labels for nodes as pt sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | int | float
|
Font size for text labels |
required |
Returns: self
set_transparency(arg)
Set transparency based on argument.
This is the same as set_alphas(). Alpha is the value for the transparency of a
color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
Legend(mapping, label=None, graph_element=None)
Class to create legend objects like colorbars or textboxs.
label = label
instance-attribute
mapping = mapping
instance-attribute
shape = None
instance-attribute
create_textbox()
LegendGenerator()
Base class for generating legends.
Legends include both colorbars and textboxes as a MatplotLib figure.
colorbars: list = []
instance-attribute
legends: list = []
instance-attribute
textboxes: list = []
instance-attribute
add_legend(legend)
draw_legends()
Determine the figure axis and draw the legends.
Colorbars have their own axis while 3 textboxes are drawn on 1 axis.
place_colorbar(colorbar, ax)
Generates then places the given colorbar on the given axis.
The colorbar is generated based on its mapping and the fallback is appended to the bottom of the generated colorbar axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colorbar
|
Legend
|
colorbar to be placed |
required |
ax
|
_AxesBase
|
column axis in which to place the legend |
required |
place_textbox(textbox, ax, index)
Places textbox on the given axis based on the index. Limit of 3 textboxes per axis, fills axis from top to bottom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
textbox
|
Legend
|
textbox to be placed |
required |
ax
|
_AxesBase
|
column axis in which to place the textbox |
required |
index
|
int
|
index of the textbox for determining placement within the given ax |
required |
sort_legends()
Add the legends to either self.colorbars or self.textboxes based on the attributes of the legends.
Nodes(labels=None)
Bases: GraphElement
alphas = fixed(1)
instance-attribute
colors = fixed('lightgrey')
instance-attribute
contour_colors = fixed('white')
instance-attribute
contour_sizes = fixed(0.5)
instance-attribute
labels = labels
instance-attribute
positions = None
instance-attribute
sizes = fixed(50)
instance-attribute
set_alphas(arg)
Set alpha based on argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
set_colors(arg)
Set the colors of the displayed nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | str
|
Colors to set. String values will be mapped onto all nodes. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_contour_colors(arg)
Set the contour color of the displayed nodes.
Contour means the outer border of a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | str
|
Colors to set. String values will be mapped onto all node contours. Additional options contain "node" and "edge" to automatically select the corresponding color. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_contour_sizes(arg)
Set the contour sizes of the displayed nodes.
Contour means the outer border of a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float | int
|
Sizes to set. Integer values will be mapped onto all node contours. String values will get mapped onto the corresponding data arrays or closeness values per node. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_positions(pos)
Set the node positions as a dict or list.
When using a file, use set_position_file() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
dict | list | Path | str
|
dict | list: Array of positions. Dicts should be keyed by node ID. |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_sizes(arg)
Set the sizes of the displayed nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float | int
|
Sizes to set. Scalar values will be mapped onto all nodes. String values |
required |
Returns:
| Type | Description |
|---|---|
|
self |
set_transparency(arg)
Set transparency based on argument.
This is the same as set_alphas(). Alpha is the value for the transparency of a
color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
GenericMapping | float
|
The text transparency as mapping between 0 and 1. |
required |
Returns: self
ensure_file_format(path, user_saving_format, *, default_format)
Ensure the provided path has a saving format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
the path that needs to be validated |
required |
user_saving_format
|
str | None
|
the saving format provided by the user |
required |
default_format
|
str
|
the format a programmer can set that will be used as default, if no format was provided at all |
required |
Returns:
| Type | Description |
|---|---|
tuple[Path, str]
|
the filepath and format (without leading dot) as an 2-Tuple |