Skip to content

mercury.graph.viz

mercury.graph.viz.Moebius(G)

Bases: MoebiusAnywidget

Moebius class for visualizing graphs using an anywidget.

Important: This class requires: anywidget, traitlets and IPython.display!! These packages are not mandatory for the rest of the library, since you may not be intereseted in visualizing graphs. If you want to use this class, you need to install these packages.

Usage
from mercury.graph.viz import Moebius

G = ... # A graph object
moebius = Moebius(G)
moebius.show()

Parameters:

Name Type Description Default
G Graph

The graph to be visualized.

required
Source code in mercury/graph/viz/moebius.py
522
523
524
525
526
def __init__(self, G):
    if display is None or HTML is None:
        raise ImportError('IPython is not installed')

    self.G = G

__getitem__(item)

Add support for the [] operator.

Source code in mercury/graph/viz/moebius.py
129
130
131
132
133
134
def __getitem__(self, item):
    """
    Add support for the [] operator.
    """

    return self._get_adjacent_nodes_moebius(item)

__str__()

Convert the object via str()

Source code in mercury/graph/viz/moebius.py
121
122
123
124
125
126
def __str__(self):
    """
    Convert the object via str()
    """

    return 'Moebius(%s)' % str(self.G)

generate_color_palette(cats, hue=0, sat=0.7, light=0.5)

Generates a color palette for the given categories. This can be used in combination with node_or_edge_config to generate the dictionary expected by the colors argument with colors that cover the whole 0..1 hue range.

Parameters:

Name Type Description Default
cats iterable

An iterable of categories for which the color palette is to be generated.

required
hue float

The base hue that is added to all the colors in the color palette. It must be in range, 0..1, all the resulting hue values will be kept modulo 1.0. Default is 0 (no shift).

0
sat float

The saturation level for the colors. Default is 0.7. Range is 0..1.

0.7
light float

The lightness level for the colors. Default is 0.5. Range is 0..1.

0.5

Returns:

Type Description
dict

A dictionary where keys are categories and values are hex color codes.

Source code in mercury/graph/viz/moebius.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def generate_color_palette(self, cats, hue = 0, sat = 0.7, light = 0.5):
    """
    Generates a color palette for the given categories. This can be used in combination with `node_or_edge_config` to generate
    the dictionary expected by the `colors` argument with colors that cover the whole 0..1 hue range.

    Args:
        cats (iterable): An iterable of categories for which the color palette is to be generated.
        hue (float, optional): The base hue that is added to all the colors in the color palette. It must be in range, 0..1, all the
            resulting hue values will be kept modulo 1.0. Default is 0 (no shift).
        sat (float, optional): The saturation level for the colors. Default is 0.7. Range is 0..1.
        light (float, optional): The lightness level for the colors. Default is 0.5. Range is 0..1.

    Returns:
        (dict): A dictionary where keys are categories and values are hex color codes.
    """
    cats = set(cats)
    cols = {}
    N = len(cats)
    for i, cat in enumerate(cats):
        h = (i/N + hue) % 1.0
        s = sat
        l = light

        r, g, b = self._hsl_to_rgb(h, s, l)

        hex_color = '#%02x%02X%02x' % (r, g, b)
        cols[cat] = hex_color

    return cols

node_or_edge_config(text_is=None, color_is=None, colors=None, size_is=None, size_range=None, size_scale='linear')

Create a node_config or edge_config configuration dictionary for show() in an understandable way.

Parameters:

Name Type Description Default
text_is str

The node/edge attribute to be displayed as text. Use the string ìd to draw the node id (regardless of the column having another name) or any valid node attribute name.

None
color_is str

A categorical node/edge attribute that can be represented as a color. This will also enable a legend interface where categories can be individually shown or hidden.

None
colors dict

The colors for each category defined as a dictionary. The keys are possible outcomes of category. The values are html RGB strings. E.g., .draw(category = 'size', colors = {'big' : '#c0a080', 'small' : '#a0c080'}) where 'big' and 'small' are possible values of the category 'size'.

None
size_is str

The node attribute to be displayed as the size of the nodes. Use the string id to set the node id (regardless of the column having another name) or any valid node attribute name. See the options in the Moebius configuration menu to set minimum, maximum sizes, linear or logarithmic scale, etc.

None
size_range List of two numbers

Combined with edge_label, this parameter controls the values in the variable that correspond to the minimum and maximum displayed sizes. The values below or equal the first value will be displayed with the base radius (that depends on the zoom) and the values above or equal to the second value will be shown with the maximum radius.

None
size_scale (linear, power, sqrt or log)

Combined with edge_label, the scale used to convert the value in the variable to the displayed radius.

'linear'

Returns:

Type Description
dict

The node configuration dictionary

Source code in mercury/graph/viz/moebius.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def node_or_edge_config(self, text_is = None, color_is = None, colors = None, size_is = None, size_range = None, size_scale = 'linear'):
    """
    Create a `node_config` or `edge_config` configuration dictionary for `show()` in an understandable way.

    Args:
        text_is (str): The node/edge attribute to be displayed as text. Use the string `ìd` to draw the node id (regardless of the
            column having another name) or any valid node attribute name.
        color_is (str): A categorical node/edge attribute that can be represented as a color. This will also enable a legend interface
            where categories can be individually shown or hidden.
        colors (dict): The colors for each category defined as a dictionary. The keys are possible outcomes of category.
            The values are html RGB strings. E.g., .draw(category = 'size', colors = {'big' : '#c0a080', 'small' : '#a0c080'})
            where 'big' and 'small' are possible values of the category 'size'.
        size_is (str): The node attribute to be displayed as the size of the nodes. Use the string `id` to set the node id (regardless
            of the column having another name) or any valid node attribute name. See the options in the Moebius configuration menu to
            set minimum, maximum sizes, linear or logarithmic scale, etc.
        size_range (List of two numbers): Combined with edge_label, this parameter controls the values in the variable that
            correspond to the minimum and maximum displayed sizes. The values below or equal the first value will be displayed with the
            base radius (that depends on the zoom) and the values above or equal to the second value will be shown with the maximum
            radius.
        size_scale ('linear', 'power', 'sqrt' or 'log'): Combined with edge_label, the scale used to convert the value in the variable
            to the displayed radius.

    Returns:
        (dict): The node configuration dictionary
    """

    config = {}

    if text_is is not None:
        config['label'] = text_is

    if color_is is not None:
        config['color'] = color_is

    if colors is not None:
        config['color_palette'] = colors
    else:
        config['color_palette'] = {}

    if size_is is None:
        config['size_thresholds'] = []
    else:
        config['size'] = size_is

        if size_range is None:
            config['size_thresholds'] = []
        else:
            assert type(size_range) == list and len(size_range) == 2
            config['size_thresholds'] = size_range

        if size_scale != 'linear':
            assert size_scale in {'power', 'sqrt', 'log'}

        config['scale'] = size_scale

    return config

show(initial_id=None, initial_depth=1, node_config=None, edge_config=None)

Start the interactive graph visualization using an anywidget.

Parameters:

Name Type Description Default
initial_id str

The id of the node to start the visualization.

None
initial_depth int

The initial depth of the graph (starting with initial_id as 0) to be shown.

1
node_config dict

A node configuration dictionary created by node_config().

None
edge_config dict

An edge configuration dictionary created by edge_config().

None
Source code in mercury/graph/viz/moebius.py
536
537
538
539
540
541
542
543
544
545
546
def show(self, initial_id = None, initial_depth = 1, node_config = None, edge_config = None):
    """
    Start the interactive graph visualization using an anywidget.

    Args:
        initial_id (str): The id of the node to start the visualization.
        initial_depth (int): The initial depth of the graph (starting with `initial_id` as 0) to be shown.
        node_config (dict): A node configuration dictionary created by `node_config()`.
        edge_config (dict): An edge configuration dictionary created by `edge_config()`.
    """
    display(MoebiusAnywidget(self.G, initial_id, initial_depth, node_config, edge_config))