Example #1
0
def test_plotly_tabs(document, comm):
    trace = go.Scatter(x=[0, 1], y=[2, 3])

    pane1 = Plotly(dict(data=[trace], layout={'autosize': True}))
    pane2 = Plotly(dict(data=[trace], layout={'autosize': True}))

    tabs = Tabs(pane1, pane2)

    root = tabs.get_root(document, comm)

    model1 = pane1._models[root.id][0]
    model2 = pane2._models[root.id][0]

    cb1, cb2 = root.js_property_callbacks['change:active']
    if cb1.args['model'] is model2:
        cb1, cb2 = cb2, cb1
    assert model1.visible
    assert cb1.args['model'] is model1
    assert cb1.args['i'] == 0
    assert not model2.visible
    assert cb2.args['model'] is model2
    assert cb2.args['i'] == 1

    tabs.insert(0, 'Blah')
    assert cb1.args['i'] == 1
    assert cb2.args['i'] == 2
Example #2
0
def test_plotly_pane_datetime64_list_transform(document, comm):
    index = [np.datetime64(dt.datetime(2019, 1, i)) for i in range(1, 11)]
    data = np.random.randn(10)
    traces = [go.Scatter(x=index, y=data)]
    fig = go.Figure(traces)
    pane = Plotly(fig)

    model = pane.get_root(document, comm)
    assert all(isinstance(v, str) for v in model.data[0]['x'])
Example #3
0
def test_plotly_pane_datetime_array_transform(document, comm):
    index = np.array([dt.datetime(2019, 1, i) for i in range(1, 11)])
    data = np.random.randn(10)
    traces = [go.Scatter(x=index, y=data)]
    fig = go.Figure(traces)
    pane = Plotly(fig)

    model = pane.get_root(document, comm)
    assert model.data_sources[0].data['x'][0].dtype.kind in 'SU'
def test_clean_relayout_data():
    relayout_data = {
        "mapbox.center": {
            "lon": -73.59183434290809,
            "lat": 45.52341668343991
        },
        "mapbox.zoom": 10,
        "mapbox.bearing": 0,
        "mapbox.pitch": 0,
        "mapbox._derived": {
            "coordinates": [
                [-73.92279747767401, 45.597934047192865],
                [-73.26087120814279, 45.597934047192865],
                [-73.26087120814279, 45.44880048640681],
                [-73.92279747767401, 45.44880048640681],
            ]
        },
    }
    result = Plotly._clean_relayout_data(relayout_data)
    assert result == {
        "mapbox.center": {
            "lon": -73.59183434290809,
            "lat": 45.52341668343991
        },
        "mapbox.zoom": 10,
        "mapbox.bearing": 0,
        "mapbox.pitch": 0,
    }
Example #5
0
def test_plotly_pane_single_trace(document, comm):
    trace = go.Scatter(x=[0, 1], y=[2, 3], uid='Test')
    pane = Plotly({'data': [trace], 'layout': {'width': 350}})

    # Create pane
    model = pane.get_root(document, comm=comm)
    assert isinstance(model, PlotlyPlot)
    assert pane._models[model.ref['id']][0] is model
    assert len(model.data) == 1
    assert model.data[0]['type'] == 'scatter'
    assert model.data[0]['x'] == [0, 1]
    assert model.data[0]['y'] == [2, 3]
    assert model.layout == {'width': 350}
    assert len(model.data_sources) == 1
    assert model.data_sources[0].data == {}

    # Replace Pane.object
    new_trace = go.Bar(x=[2, 3], y=[4, 5])
    pane.object = {'data': new_trace, 'layout': {'width': 350}}
    assert len(model.data) == 1
    assert model.data[0]['type'] == 'bar'
    assert model.data[0]['x'] == [2, 3]
    assert model.data[0]['y'] == [4, 5]
    assert model.layout == {'width': 350}
    assert len(model.data_sources) == 1
    assert model.data_sources[0].data == {}
    assert pane._models[model.ref['id']][0] is model

    # Cleanup
    pane._cleanup(model)
    assert pane._models == {}
Example #6
0
def test_plotly_pane_numpy_to_cds_traces(document, comm):
    trace = go.Scatter(x=np.array([1, 2]), y=np.array([2, 3]))
    pane = Plotly({'data': [trace], 'layout': {'width': 350}})

    # Create pane
    model = pane.get_root(document, comm=comm)
    assert isinstance(model, PlotlyPlot)
    assert len(model.data) == 1
    assert model.data[0]['type'] == 'scatter'
    assert 'x' not in model.data[0]
    assert 'y' not in model.data[0]
    assert model.layout == {'width': 350}
    assert len(model.data_sources) == 1
    cds = model.data_sources[0]
    assert np.array_equal(cds.data['x'][0], np.array([1, 2]))
    assert np.array_equal(cds.data['y'][0], np.array([2, 3]))

    # Replace Pane.object
    new_trace = [
        go.Scatter(x=np.array([5, 6]), y=np.array([6, 7])),
        go.Bar(x=np.array([2, 3]), y=np.array([4, 5]))
    ]
    pane.object = {'data': new_trace, 'layout': {'width': 350}}
    assert len(model.data) == 2
    assert model.data[0]['type'] == 'scatter'
    assert 'x' not in model.data[0]
    assert 'y' not in model.data[0]
    assert model.data[1]['type'] == 'bar'
    assert 'x' not in model.data[1]
    assert 'y' not in model.data[1]
    assert model.layout == {'width': 350}
    assert len(model.data_sources) == 2
    cds = model.data_sources[0]
    assert np.array_equal(cds.data['x'][0], np.array([5, 6]))
    assert np.array_equal(cds.data['y'][0], np.array([6, 7]))
    cds2 = model.data_sources[1]
    assert np.array_equal(cds2.data['x'][0], np.array([2, 3]))
    assert np.array_equal(cds2.data['y'][0], np.array([4, 5]))

    # Cleanup
    pane._cleanup(model)
    assert pane._models == {}
Example #7
0
def test_plotly_autosize(document, comm):
    trace = go.Scatter(x=[0, 1], y=[2, 3])

    pane = Plotly(dict(data=[trace], layout={'autosize': True}))

    model = pane.get_root(document, comm=comm)
    model.sizing_mode == 'stretch_both'

    pane.object['layout']['autosize'] = False
    pane.param.trigger('object')
    model.sizing_mode == 'fixed'

    pane._cleanup(model)

    pane = Plotly(dict(data=[trace], layout={'autosize': True}),
                  sizing_mode='fixed')
    model = pane.get_root(document, comm=comm)
    model.sizing_mode == 'fixed'

    pane._cleanup(model)
Example #8
0
def generate3DDiagram(file, sid, df=False):
    if not df:
        df = decreaseDiagramSize(file)
    else:
        df = file

    names = df.columns.tolist()
    N = len(names)

    G = from_pandas_adjacency(df)
    G = convert_node_labels_to_integers(G)
    # 3d spring layout
    pos = spring_layout(G, dim=3)
    # numpy array of x,y,z positions in sorted node order
    layt = array([pos[v] for v in sorted(G)])
    # scalar colors
    scalars = array(list(G.nodes())) + 5
    # edges

    maximum = 0
    for (u, v, d) in G.edges(data=True):
        w = d['weight']
        if w > maximum:
            maximum = w

    Edges = array([(int(u), int(v), {
        'weight': d['weight'] / maximum
    }) for (u, v, d) in G.edges(data=True) if d['weight'] > 0])

    def make_edge(x, y, z, weight):
        return Scatter3d(
            x=x,
            y=y,
            z=z,
            # line=dict(color='rgb(' + str(int(100 + (weight ** 2 - 0.25) * 100)) + ',100,100)', width=(weight * 3) ** 2),
            line=dict(color='rgb(' + str(int(weight) * 180) + ', 0, 0)',
                      width=(weight * 3)**2),
            hoverinfo='none',
            mode='lines')

    Xn = [layt[k][0] for k in range(N)]  # x-coordinates of nodes
    Yn = [layt[k][1] for k in range(N)]  # y-coordinates
    Zn = [layt[k][2] for k in range(N)]  # z-coordinates
    edge_traces = []

    for e in Edges:
        x_edge_ends = [layt[e[0]][0], layt[e[1]][0],
                       None]  # x-coordinates of edge ends
        y_edge_ends = [layt[e[0]][1], layt[e[1]][1], None]
        z_edge_ends = [layt[e[0]][2], layt[e[1]][2], None]
        edge_traces.append(
            make_edge(x_edge_ends, y_edge_ends, z_edge_ends, e[2]['weight']))

    trace2 = Scatter3d(x=Xn,
                       y=Yn,
                       z=Zn,
                       mode='markers',
                       marker=dict(symbol='circle',
                                   size=6,
                                   color=scalars,
                                   colorscale='Viridis',
                                   line=dict(color='rgb(50,50,50)',
                                             width=0.5)),
                       text=names,
                       hoverinfo='text')

    axis = dict(showbackground=False,
                showline=False,
                zeroline=False,
                showgrid=False,
                showticklabels=False,
                title='')

    from graphion.session.handler import calculate_plot_size
    psize = calculate_plot_size(sid)

    layout = Layout(title="Force-directed layout",
                    width=psize,
                    height=psize,
                    showlegend=False,
                    scene=dict(
                        xaxis=dict(axis),
                        yaxis=dict(axis),
                        zaxis=dict(axis),
                    ),
                    margin=dict(t=100),
                    hovermode='closest',
                    paper_bgcolor='rgba(0,0,0,0)',
                    plot_bgcolor='rgba(0,0,0,0)')

    data = [trace2] + edge_traces
    fig = Figure(data=data, layout=layout)
    extension('plotly')
    painful = Plotly(fig)
    return painful