Skip to content

mercury.graph.viz

mercury.graph.viz.Moebius(G)

Bases: MoebiusAnywidget

Moebius class for visualizing graphs using an anywidget.

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
453
454
455
456
457
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
125
126
127
128
129
130
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
117
118
119
120
121
122
def __str__(self):
    """
    Convert the object via str()
    """

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

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
133
134
135
136
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
467
468
469
470
471
472
473
474
475
476
477
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))