cytoscape / cytoscape-jupyter-widget

Cytoscape.js based network visualizer for Jupyter Notebook
38 stars 14 forks source link

mapData not working for network produced by NiceCXNetwork.to_cx() #4

Open alex-wenzel opened 5 years ago

alex-wenzel commented 5 years ago

I have cyjupyter==0.2.0 and I am trying to follow the steps outlined in the example notebook for mapping data to node background colors. I have a simple network neighbor_net which is a ndex2.nice_cx_network.NiceCXNetwork. I have set a single attribute such that running the code

for nid in neighbor_net.nodes:
    print(neighbor_net.nodes[nid], neighbor_net.get_node_attribute_value(nid, 'in_top100'))

produces

image

I am defining a custom style using this code

my_style = [
    {
        'selector': 'node',
        'style': {
            'background-color': 'mapData(in_top100,' + str(0.0) + ',' + str(3.7) + ', blue, red)',
            'label': 'data(name)',
            'font-size': 2
        }
    },
    {
        'selector': 'edge',
        'style': {
            'width': 1,
            'opacity': 0.8,
            'line-color': 'green',
            'target-arrow-color': 'green',
            'target-arrow-shape': 'triangle'
        }
    }
]

and this code

Cytoscape(
    data=neighbor_net.to_cx(), 
    format='cx', 
    visual_style=my_style,
    layout={
        "height": "1200px",
        "width": "1200px"
    }
)

produces this visualization

image

I would expect GP1BA to be colored red, but that is not the case. I read in the docs that missing values will be replaced with the extrema, so my hypothesis is that something under the hood in Cytoscape() isn't reading the attribute I set in the way I expect it to.

I also tried to use networkx v2.3 to more closely mimic the example notebook

Cytoscape(
    data=cytoscape_data(neighbor_net.to_networkx()), 
    #format='cx', 
    visual_style=my_style,
    layout={
        "height": "1200px",
        "width": "1200px"
    }
)

and this code produces the output I expect:

image

Because Cytoscape() allows format=cx, its not clear to me why I had to convert to a networkx format first to get this to work.

ortega2247 commented 5 years ago

When you use the cx format the style of the graph (background-color, etc) is supposed to be included in the cx graph and cyjupyter tries to get that from the graph and ignore the visual_style that you pass to it. I don't have much experience with the cx format but I guess a workaround would be to include the style in you cx graph