cytoscape / ipycytoscape

A Cytoscape Jupyter widget
https://ipycytoscape.readthedocs.io/en/master/
BSD 3-Clause "New" or "Revised" License
269 stars 62 forks source link

classes nomenclature #187

Closed joseberlines closed 3 years ago

joseberlines commented 3 years ago

In the file example gridData.json we can see:

{
   "nodes":[
      {
         "data":{
            "id":"n40",
            "weight":53
         },
         "position":{
            "x":50,
            "y":45
         },
         "group":"nodes",
         "removed":false,
         "selected":false,
         "selectable":true,
         "locked":false,
         "grabbable":true,
         "classes":""
      },

but in the documentation we can see: https://ipycytoscape.readthedocs.io/en/latest/examples/labels.html?highlight=classes#Add-labels-to-Nodes

...
{'data': {'id': '2', 'label': 'top center', 'classes': 'top-center' }},
...

So in the first example classes IS NOT inside data, whereas in the second case it is. 1.- What is the way to go? What is better? Are both ok? 2.- Are those ALL the attributes of a node or are there more? 3.- Can someone link me to a notebook where it is possible to see the different use of group attribute and classes attribute. 6.- in ipycytosacpe I see classes as a spaces separated string where they are an array (classes: ['foo', 'bar'], // an array) (see link above). Would that not be better to allow a list instead of a space separated string for different classes? If the classes are a list you can easily loop, append, pop, etc.

SvenSchiffner commented 3 years ago

These are to different things in the first example you set the attribute classes of the node instance to something (only interesting for edges in the moment) and in the second example you only add am additional key value pair to the data dict.

You can take a look in the source code for the different attributes in most cases everything is done automatically and only the data attribute is relevant to store some informations. https://github.com/QuantStack/ipycytoscape/blob/26fed38ae6aa5c3bad375cab3ddcf937d84de72b/ipycytoscape/cytoscape.py#L161 https://github.com/QuantStack/ipycytoscape/blob/26fed38ae6aa5c3bad375cab3ddcf937d84de72b/ipycytoscape/cytoscape.py#L185 https://github.com/QuantStack/ipycytoscape/blob/26fed38ae6aa5c3bad375cab3ddcf937d84de72b/ipycytoscape/cytoscape.py#L201

Sorry, I don't know. But I think that I would be very helpful.

Basically, for this use case lists and strings aren't that different but I also asked for this in my PR #162.

marimeireles commented 3 years ago

So in the first example classes IS NOT inside data, whereas in the second case it is. 1.- What is the way to go? What is better? Are both ok?

The reason why I wrote like in the first example is simply because I was following cytoscape style see: https://github.com/cytoscape/cytoscape.js/blob/master/documentation/demos/colajs-graph/data.json. I think the general idea is to have generic attributes in the graph, attributes that should be manipulated by the library and offer functionalities to the user outside the data attr. data IMO should be used to add the data relevant to the node or edge, the input data of the user, but I might be wrong. I'm happy to hear user's input on this :) so we can possibly change it, if it doesn't make sense.

2.- Are those ALL the attributes of a node or are there more?

Yes. Check https://js.cytoscape.org/'s doc or the code where sven5s' pointed out.

3.- Can someone link me to a notebook where it is possible to see the different use of group attribute and classes attribute.

I'm not sure if I understood you well, but: https://ipycytoscape.readthedocs.io/en/latest/examples/networkx.html#Custom-networkx-Node-Objects-that-inherit-from-ipycytoscape.Node Maybe this answers your question?

6.- in ipycytosacpe I see classes as a spaces separated string where they are an array (classes: ['foo', 'bar'], // an array) (see link above). Would that not be better to allow a list instead of a space separated string for different classes? If the classes are a list you can easily loop, append, pop, etc.

sven5s is working on it :) Thanks for the input Jose.

If I answered your questions we can close this one too?

joseberlines commented 3 years ago

Ok, so basically all is based in cytoscape. So if I am explaining ipycytoscape to someone and they asked "why is the json structure for every node not a simple flat dictionary" the answer is basically because it is base in cytoscape.

And only the attributes within the data key are really necessary for the graph.

I see within the code:

data = MutableDict().tag(sync=True)
    pannable = Bool().tag(sync=True)
    _base_cyto_attrs = [
        "removed",
        "selected",
        "selectable",
        "classes",
        "data",
        "pannable",
    ]

Data is hence a dict, but not restricted to a particular number of keys.

You can close this issue if this above is right. (NOTE1: why that is so relevant becomes more apparent when you think that pandas provides a function df.to_json that basically could provide in one line a graph if it would not be because the dictionary of the node is not flat, and we are here all pandas freaks) (NOTE2: I am adding this info in a second medium article I am writing. And it is very relevant for "selectively" let the user manipulate the appearrance of the graph see #194 )