qumin.lattice package

Submodules

qumin.lattice.lattice module

class qumin.lattice.lattice.ICLattice(onehot_encoding, microclasses, annotate=None, comp_prefix=None, aoc=False)[source]

Bases: object

Inflection class Lattice representation.

This is a wrapper around (concepts.Context). It builds on concepts’ lattice class, but adopts a custom representation of lattices, using Qumin’s :class:`qumin.clustering.node.Node class.

Variables:
  • context (concepts.Context) – stores the underlying Context representation.

  • lattice (concepts.lattices.Lattice) – shortcut to the underlying lattice representation.

  • nodes (qumin.clustering.node.Node) – Qumin’s lattice is represented as a root node. Each node contains its children. This is the lattice we will draw.

  • comp (bool) – Whether there are two sets of properties to represent.

__init__(onehot_encoding, microclasses, annotate=None, comp_prefix=None, aoc=False)[source]

Creates a lattice from a onehot_encoding and microclasses, that is a dictionary which maps one lexeme to lexemes belonging to the same class.

A simpler interface to convert patterns into an ICLattice is available through PatternStore.to_lattice().

Parameters:
  • onehot_encoding (pandas.DataFrame) – onehot encoding of objects (rows) and attributes (columns). Filled with 1/0, True/False, “X”/””.

  • annotate (dict) – Extra annotations to add on lattice. Of the form: {<object label>: <annotation>}

  • aoc (bool) – Whether to limit ourselves to Attribute or Object Concepts.

  • kwargs (dict) – all other keyword arguments are passed to table_to_context

ancestors(identifier)[source]

Return all ancestors of a node which corresponds to the identifier.

draw_nodes(styler, figsize=(24, 12), scale=False, layout='qumin', seed=0, colormap='Blues', **kwargs)[source]

Draw the root node using qumin.clustering.node.Node’s drawing function.

Parameters:
  • styler (qumin.lattice.stylers.LatticeStyler) – Styling object for advanced plot customization.

  • figsize (tuple) – Size of the figure.

  • scale (bool) – Whether to display a colorbar. Defaults to False.

  • colormap (str) – Colormap to use for the scale (if any).

  • **kwargs – All keyword arguments will be passed to qumin.clustering.node.Node.draw().

parents(identifier)[source]

Return all direct parents of a node which corresponds to the identifier.

stats()[source]

Returns some stats about the classification size and shape. Based on self.nodes, not self.lattice: stats are different depending on AOC/not AOC.

to_figure(filename=None, styler=None, title=None, save_kws=None, layout='qumin', **kwargs)[source]

Default drawing function.

Wrapper around the low_level :method:`ICLattice.draw_nodes`.

Parameters:
  • styler (qumin.lattice.stylers.LatticeStyler) – Styling object for advanced plot customization.

  • filename (str) – If provided, the plot will be saved to the given file.

  • save_kws (dict) – Keyword arguments for matplotlib.pyplot.savefig(). Used only when a filename is provided.

  • layout (str) – Drawing layout. Supported values are “qumin” for Qumin’s internal algorithm, or ‘dot’ for the graphviz ‘dot’ layout. Ignored if pos is given.

  • title (str) – title of the plot.

  • **kwargs – keyword arguments are passed to ÌCLattice.draw_nodes().

Returns:

A matplotlib plot.

Return type:

matplotlib.Figure

to_html(**kwargs)[source]
qumin.lattice.lattice.context_from_pandas(df)[source]

Converts a onehot encoding stored as a pandas.DataFrame to a concepts.Context. Uses the following recipe: https://concepts.readthedocs.io/en/stable/advanced.html#context-from-pandas-dataframe

Parameters:

df (pandas.DataFrame) – onehot encoding of objects (rows) and attributes (columns). Filled with 1/0, True/False or “X”/””.

Returns:

a Context representing the input data.

Return type:

concepts.Context

qumin.lattice.lattice.to_html_disabled(*args, **kwargs)[source]

qumin.lattice.stylers module

class qumin.lattice.stylers.LatticeStyler(*args, label_attributes=False, max_label_number=3, attribute_label_kws=None, show_size=True, **kwargs)[source]

Bases: NodeStyler

Instances of this class can be passed to drawing functions of the qumin.lattice.lattice.ICLattice class.

All properties from the qumin.lattice.stylers.NodeStyler class are available.

__init__(*args, label_attributes=False, max_label_number=3, attribute_label_kws=None, show_size=True, **kwargs)[source]
Parameters:
  • max_label_number (int) – Maximum number of object names shown.

  • label_attributes (bool) – Whether to label attribute nodes too.

  • show_size (bool) – Whether to show the size of inflection classes in node labels.

  • attribute_label_kws (dict) – Additional default keyword arguments for attribute labels.

  • kwargs (dict) – Additional keyword arguments are passed to qumin.lattice.stylers.NodeStyler.__init__().

get_edge_color(node, child)[source]
Parameters:
Returns:

the colors to use.

Return type:

tuple[str]

get_html_tooltip(node, patterns_map, comp=None)[source]

Render node tooltips on HTML visualizations (with mpld3).

Parameters:
  • patterns_map (dict[str, List]) – a dictionary that maps a pattern to the list of patterns that have the same distribution.

  • node (qumin.clustering.node.Node) – the node whose label we render.

Returns:

HTML tooltip that will render for each node on hover.

Return type:

str

get_label_properties(node)[source]

Returns the text label and properties for a given lattice node.

Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

A list of tuples containing 2 items each: a label (str) and a dictionary of matplotlib options (position & style).

Return type:

List[Tuple[str, Dict]]

get_marker_properties(node)[source]

Returns the marker properties for a given lattice node.

This method is able to handle up to three colors, using matplotlib’s utility for two color markers + the marker edge color.

The output of this function should be a dictionary of keyword arguments for matplotlib.pyplot.scatter().

Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

A list of keyword options for:func:matplotlib.pyplot.scatter.

Return type:

dict

get_marker_shape(node)[source]
Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

the marker to use for a given node.

Return type:

str or matplotlib.pyplot.Marker

class qumin.lattice.stylers.NodeStyler(colors=None, min_size=0, max_size=0, min_markersize=20, max_markersize=50, square=False, markers=None, textoffset=0.08, edge_kws=None, marker_kws=None, label_kws=None, draw_nodes=True, layout_y_factor=1, layout_x_factor=15, topdown=True, horizontal=False)[source]

Bases: object

Instances of this class can be passed to drawing functions of the qumin.clustering.node.Node class.

There are two levels of customization:

  • Use one of the default NodeStyler’s: DefaultStyler.

  • Using this class as is, many options are available to change the default colormap, fontsize, etc. You simply have to instanciate an object and pass it to further scripts.

  • You can inherit methods from this class and rewrite some of them. Just be sure that you implement an interface for each property, as in the default LatticeStyler (markers, labels, colors). Then, you instanciate an object and pass it to further scripts.

Variables:
  • colors (List) – the colorscheme to use.

  • edge_kws (dict) – options that will be passed to draw edges.

  • marker_kws (dict) – options that will be passed to draw nodes.

  • label_kws (dict) – options that will be passed to draw labels.

__init__(colors=None, min_size=0, max_size=0, min_markersize=20, max_markersize=50, square=False, markers=None, textoffset=0.08, edge_kws=None, marker_kws=None, label_kws=None, draw_nodes=True, layout_y_factor=1, layout_x_factor=15, topdown=True, horizontal=False)[source]
Parameters:
  • draw_nodes (dict) – Whether to draw the nodes or not.

  • colors (list[str]) – Color scheme to use. Two items.

  • textoffset (int) – Offset for text labels.

  • min_size (int) – Minimum size of a node.

  • max_size (int) – Maximum size of a node.

  • min_markersize (int) – Minimum size of a marker.

  • max_markersize (int) – Maximum size of a marker.

  • marker (str) – The marker shape. Defaults to ‘o’.

  • edge_kws (dict) – Additional default keyword arguments for edges.

  • marker_kws (dict) – Additional default keyword arguments for markers.

  • label_kws (dict) – Additional default keyword arguments for labels.

  • layout_y_factor (float) – Scaling on the y-axis.

  • layout_x_factor (float) – Scaling on the x-axis.

  • horizontal (bool) – Orientation of the lattice (vertical or horizontal).

  • topdown (bool) – Direction of the lattice (top->down or right->left when horizontal=True).

  • square (bool) –

    Controls whether the lattice is drawn with straight lines or squared.

    Example:

                            square=True        square=False
    
                            │  ┌──┴──┐         │    ╱╲
    horizontal=False        │  │   ┌─┴─┐       │   ╱  ╲
    topdown=True            │  │   │   │       │  ╱   ╱╲
                            │  │   │   │       │ ╱   ╱  ╲
                            │__│___│___│       │╱___╱____╲
    
                            │─────┐             │⟍
                            │───┐ ├             │  ⟍
    horizontal=True         │   ├─┘             │⟍ ⟋
    topdown=True            │───┘               │⟋
                            │____________       │____________
    

colors = ['#004f00', 'indigo']
edge_kws = {'linewidth': 0.5}
get_edge_color(node, child)[source]
get_edge_properties(node, child)[source]

Returns the edge properties for a pair of parent - child nodes.

This method is able to handle up to two colors, using multicolor dashes.

Parameters:
Returns:

A list of matplotlib options for the marker..

Return type:

dict

get_label_position(node)[source]

Returns keywords for the positioning of the labels in matplotlib.

get_label_properties(node)[source]

Returns the text label and properties for a given node.

Parameters:

node (qumin.clustering.node.Node) – A Node to draw.

Returns:

A list of tuples containing 2 items each: a label (str) and a dictionary of matplotlib options (position & style).

Return type:

List[Tuple[str, Dict]]

get_marker_properties(node)[source]

Returns the marker properties for a given node.

This method is able to handle up to two colors, using the marker edge color.

The output of this function should be a dictionary of keyword arguments for matplotlib.pyplot.scatter().

Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

A list of keyword options for:func:matplotlib.pyplot.scatter.

Return type:

dict

get_marker_shape(node)[source]
Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

the marker to use for a given node.

Return type:

str or matplotlib.pyplot.Marker

get_marker_size(node)[source]

Normalizes the size of the node for the visualization.

Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

the size of the node in the visualization.

Return type:

float

get_node_color(node)[source]
Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

The colors to use for a given node.

Return type:

iterable

label_kws = {'family': 'DejaVu Sans', 'size': 9, 'weight': 'normal'}
marker_kws = {}
markers = ['o', 's']
set_size_scale(root)[source]

Initializes the size norming function. This cannot be done when the instance is created (as we don’t know yet the size scale to use), so this function is called by the lattice drawing method.

Parameters:

root (qumin.clustering.node.Node) – Top level lattice node.

class qumin.lattice.stylers.TreeStyler(*args, onlymacroclasses=False, min_markersize=15, max_markersize=50, **kwargs)[source]

Bases: NodeStyler

Instances of this class can be passed to drawing functions for macroclass trees.

All properties from the qumin.lattice.stylers.NodeStyler class are available.

__init__(*args, onlymacroclasses=False, min_markersize=15, max_markersize=50, **kwargs)[source]

Arguments:

get_label_properties(node)[source]

Returns the text label and properties for a given node.

Parameters:

node (qumin.clustering.node.Node) – A Node to draw.

Returns:

A list of tuples containing 2 items each: a label (str) and a dictionary of matplotlib options (position & style).

Return type:

List[Tuple[str, Dict]]

get_node_color(node)[source]
Parameters:

node (qumin.clustering.node.Node) – A lattice Node to draw.

Returns:

The colors to use for a given node.

Return type:

iterable

Module contents