Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add constructor to main widget #258

Merged
merged 8 commits into from Nov 27, 2021
Merged

add constructor to main widget #258

merged 8 commits into from Nov 27, 2021

Conversation

vaniisgh
Copy link
Contributor

@vaniisgh vaniisgh commented May 6, 2021

Hey,
This issue ( #94 ) looked interesting and beginner-friendly,
I just wanted to know if this was a good start.

@github-actions
Copy link

github-actions bot commented May 6, 2021

Binder 👈 Launch a binder notebook on branch vaniisgh/ipycytoscape/master

@ianhi
Copy link
Collaborator

ianhi commented May 6, 2021

Hi @vaniisgh thanks for taking this on! This looks like a good start but there are a few things to clean up.

TODO:

  1. Add some examples documenting this
  2. Add a docstring to __init__ with parameters section
  3. Cleanup checking for string in the from_json method (I'll add an inline review) (edit I think you did this correctly and I was just reading the code too fast - my bad!)
  4. Add a check for neo4j graphs
  5. Change the name from graph_file to something more general.
    • It's not necessarily a file that user will be passing in.
  6. Make the formatter happy by running black .

Also looking at the test failures there are unfortunately several due to neo4j which aren't your fault. This makes it trickier to see if your are causing issues but you can look more closely by clicking on the details next to the failing test.

ipycytoscape/cytoscape.py Outdated Show resolved Hide resolved
directed : bool
If True all edges will be given 'directed' as a class if
they do not already have it.
"""
if path.isfile(str(json_file)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fully protect us if the user passes in a dict?

ipycytoscape/cytoscape.py Outdated Show resolved Hide resolved

from spectate import mvc
from traitlets import TraitType, TraitError

from pandas import DataFrame
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently don't require pandas so this need to either be an optional import or the check below needs to check for pandas in the graph.__class__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a change to fix this, but I am not sure I passed the kwargs correctly to the add_graph_from_df correctly ... cold you please let me know what the correct way to do that would be? :)

ipycytoscape/cytoscape.py Outdated Show resolved Hide resolved
@ianhi ianhi linked an issue May 6, 2021 that may be closed by this pull request
@vaniisgh
Copy link
Contributor Author

vaniisgh commented May 12, 2021

hey thank you so much for the feedback!
I tried to take the things you mentioned into account and made a few changes. Though, I still think I might need to revise my work a bit.
Also, I apologize forgot to rebase my work on the current master I hope I did it correctly now.

Copy link
Collaborator

@ianhi ianhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good! I'll try look more closely (e.g. at the pandas import) in the coming days. For now I left a quick comment on the docstring and approved the github actions.

Comment on lines 834 to 838
- graph:
type: string|dict|pandas.DataFrame|networkx.Graph|neo4j|Graph|None
description: graph passes is checked to be of one of the declared
types, and the corresponding object is added to the
graph attribute of the DOM object.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! Can you update this to be in the numpydoc style as described here: https://numpydoc.readthedocs.io/en/latest/format.html#parameters

You can see an example here:

Parameters
----------
g : networkx graph
receives a generic NetworkX graph. more info in
https://networkx.github.io/documentation/
directed : bool
If true all edges will be given directed as class if
they do not already have it. Equivalent to adding
'directed' to the 'classes' attribute of edge.data for all edges

Comment on lines 845 to 850
if isinstance(graph, nx.Graph):
self.graph.add_graph_from_networkx(graph)
elif isinstance(graph, (dict, str)):
self.graph.add_graph_from_json(graph)
elif graph.__class__.__name__ == "DataFrame":
self.graph.add_graph_from_df(graph, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the approach I've always taken to optional imports in the past, but looking at it now and reading around on the internet a bit I think this is probably not the best way. This is brittle to changes in class names and also to other objects that have a similar classname. I think a better approach would be to try to import pandas at the top level like this:

try:
    import pandas as pd
except ImportError:
    pd = None

and then for this checks do:

elif pd and isinstance(graph, pd.DataFrame)

what do you think?

Copy link
Contributor Author

@vaniisgh vaniisgh May 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a good solution really. :) Though then should we do something similar for the neo4j graphs (because Graph is a far more common class name than DataFrame)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that's the best way forward!

Comment on lines 851 to 853
else:
if isinstance(graph, Graph):
self.graph = graph
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to make these elifs at the same level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was thinking was, that the graph.class.name is "Graph" for both objects, and I didn't want to add the optional import, I will recheck though.

@vaniisgh
Copy link
Contributor Author

I think it should be better now :)

@ianhi
Copy link
Collaborator

ianhi commented May 27, 2021

Thanks for the ping! This slipped through the cracks for me - can look later

ipycytoscape/cytoscape.py Outdated Show resolved Hide resolved
ipycytoscape/cytoscape.py Outdated Show resolved Hide resolved
Comment on lines 850 to 852
Checked to be of one of the declared types, and graph object
corresponding to it is added as a Graph attribute to the
CytoscapeWidget DOM.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Checked to be of one of the declared types, and graph object
corresponding to it is added as a Graph attribute to the
CytoscapeWidget DOM.
The graph to initialize with. Equivalent to calling the appropriate ``CytoscapeWidget.graph.add_graph_from_` method.

Is this clearer? We maybe should stay away from using words like DOM as I suspect that isn't a super clear term to most python users.

@vaniisgh
Copy link
Contributor Author

@ianhi should i close this ?

@ianhi
Copy link
Collaborator

ianhi commented Nov 27, 2021

Thank you @vaniisgh !! This is a great addition and we can always iterate more as needed.

@ianhi ianhi merged commit 79a4000 into cytoscape:master Nov 27, 2021
@ianhi
Copy link
Collaborator

ianhi commented Nov 27, 2021

@ianhi should i close this ?

Definitely not that - I'm really sorry about the long periods of non-responsiveness that's totally on me. I really appreciate you coming and putting the effort to make a positive change. I can't promise to be perfect, but I can promise that I will at least try to be more honest myself about what I can claim to do. Also don't be scared to ping more and more often (though ideally course you shouldn't have to :/ )

@ianhi
Copy link
Collaborator

ianhi commented Nov 27, 2021

This should be live in version 1.3.0 soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a constructor in the main widget
2 participants