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

Functions to plot networkx graphs of topology objects #492

Merged
merged 81 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 80 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
23e9350
Include angle and dihedral information on conversion of topology to n…
CalCraven Jan 9, 2021
905263e
create gmso/formats/networkx.py
CalCraven Jan 10, 2021
07095b3
Add _get_angles_for and _get_dihedrals_for methods for a topology. Se…
CalCraven Jan 12, 2021
3d6e35b
Add functions to clean up networkx plotting, and plot missing angle a…
CalCraven Jan 14, 2021
3b7568e
Add unit tests for _get_x_for where x is bonds, angles, and dihedrals…
CalCraven Jan 15, 2021
c1ff20f
Merge branch 'convert_nx' of https://github.com/CalCraven/gmso into v…
CalCraven Jan 15, 2021
b573ebe
Fix import * and add soft import for matplotlib
CalCraven Jan 20, 2021
9a4d40c
Add unit tests for selecting atomtype parameters, and soft imports fo…
CalCraven Jan 21, 2021
db5933c
change to
CalCraven Jan 21, 2021
9870348
update to current master branch
CalCraven Jan 28, 2021
5cc1c0b
add interactive features to networkx.py
CalCraven Jan 28, 2021
7fe4440
update to master
CalCraven Jan 28, 2021
cc07db2
fix bugs with importing networkx.py functions
CalCraven Jan 28, 2021
b083f03
Merge branch 'master' into vis_atype
daico007 Feb 4, 2021
3bbab12
Convert charge units, clean up if statements for selecting angles and…
CalCraven Feb 4, 2021
6855593
Merge branch 'vis_atype' of https://github.com/CalCraven/gmso into vi…
CalCraven Feb 10, 2021
f77e3d6
Clean up layout for plotting molecules
CalCraven Feb 10, 2021
38cb18c
update charges
CalCraven Feb 10, 2021
960bf8a
Merge branch 'master' of https://github.com/mosdef-hub/gmso
CalCraven Feb 10, 2021
9f49e27
Merge branch 'master' of https://github.com/CalCraven/gmso into vis_a…
CalCraven Feb 10, 2021
842c458
remove Ipython.Display import
CalCraven Feb 10, 2021
a555323
Add messages and support for gmso.topology objects that are missing a…
CalCraven Feb 15, 2021
bc3b24a
Unit tests that are still failing
CalCraven Feb 22, 2021
efb299b
Passing test_highlight_edges
CalCraven Feb 22, 2021
0a5236f
unit tests passing for selecting_parameters
CalCraven Feb 22, 2021
2472a20
update to master
CalCraven Feb 24, 2021
f242e48
Fix unused import, and improve import of test_networkx.py functions
CalCraven Feb 24, 2021
25bd67f
increase unit test coverage
CalCraven Feb 24, 2021
ca9b1c3
Merge branch 'master' of https://github.com/mosdef-hub/gmso into vis_…
CalCraven Feb 24, 2021
5d9c4e3
Add ipywidgets to requirements-test.txt
CalCraven Feb 24, 2021
211917b
Fill out unit tests
CalCraven Feb 25, 2021
9b77799
Merge branch 'master' of https://github.com/mosdef-hub/gmso into vis_…
CalCraven Feb 25, 2021
dc4f323
parameter and get_edges unit tests
CalCraven Feb 26, 2021
cf44fa4
Update to master
CalCraven Mar 11, 2021
354544f
Fix merge issue, update test_return_labels_for_nodes
CalCraven Mar 11, 2021
c8fbae0
create gmso/formats/networkx.py
CalCraven Jan 10, 2021
2b37062
Add functions to clean up networkx plotting, and plot missing angle a…
CalCraven Jan 14, 2021
51e3962
Add _get_angles_for and _get_dihedrals_for methods for a topology. Se…
CalCraven Jan 12, 2021
4a85cd2
Add unit tests for _get_x_for where x is bonds, angles, and dihedrals…
CalCraven Jan 15, 2021
035c32b
Fix import * and add soft import for matplotlib
CalCraven Jan 20, 2021
b470440
Add unit tests for selecting atomtype parameters, and soft imports fo…
CalCraven Jan 21, 2021
c37e189
change to
CalCraven Jan 21, 2021
3582c9e
remove merge conflicts
CalCraven Mar 11, 2021
07606fa
rebase to master
CalCraven Mar 11, 2021
31b8eac
rebase to fee3df2
CalCraven Mar 11, 2021
29f9ed4
Add tests for select_params_on_networkx and select_angles_from_sites
CalCraven Mar 15, 2021
1a8bbf8
unit tests for select_parameters_on_networkx missing angles and dihed…
CalCraven Mar 15, 2021
2b245ce
remove interact fixtures to improve ease for writing unit tests
CalCraven Mar 18, 2021
857ddc0
unit tests for call_interactive_sites
CalCraven Mar 18, 2021
cceb0b8
Merge branch 'master' into vis_atype
daico007 Mar 24, 2021
353eeff
add tests for select_edges and show_bond_info
CalCraven Mar 25, 2021
303cae7
add tests to call_interactive sites
CalCraven Mar 25, 2021
55f1aa0
apply black to clean up code formatting
CalCraven Apr 2, 2021
fae8504
Update gmso/formats/networkx.py
CalCraven Apr 6, 2021
d471f5b
Update gmso/formats/networkx.py
CalCraven Apr 6, 2021
9fe8f32
Update gmso/formats/networkx.py
CalCraven Apr 6, 2021
55972aa
add selectable_dict_for_edges_from_labels function
CalCraven Apr 6, 2021
f0a69d2
fixed an issue with the widgets dropdown not being selectable as a li…
CalCraven Apr 6, 2021
3dde79b
fix behavior with missing atom_types so parameters can still be visua…
CalCraven Apr 7, 2021
d203888
update docstrings for interactive methods
CalCraven Apr 7, 2021
64e1a58
apply black
CalCraven Apr 7, 2021
c69539a
Update gmso/formats/networkx.py
CalCraven Apr 8, 2021
cc31c34
Merge branch 'master' into vis_atype
daico007 Apr 11, 2021
6d2e850
Merge branch 'master' into vis_atype
CalCraven Apr 15, 2021
4fd2dcf
Merge branch 'master' of https://github.com/mosdef-hub/gmso
CalCraven Apr 19, 2021
d0e0dbb
Merge branch 'master' of https://github.com/mosdef-hub/gmso
CalCraven Apr 28, 2021
45fc666
update to master
CalCraven Apr 28, 2021
bfbbc55
remove requirements-test.txt
CalCraven Apr 28, 2021
85c372a
checks for installation of matplotlib and ipywidgets
CalCraven Apr 28, 2021
75a5399
change ipywidgets to widgets
CalCraven Apr 28, 2021
2232aec
update testing with soft imports
CalCraven Apr 28, 2021
cbc9d68
put functions meant to be implemented by users on top, add checks for…
CalCraven Apr 28, 2021
70ef0dc
add matplotlib and ipywidgets for unit testing
CalCraven Apr 28, 2021
3c007d7
skip tests that need a jupyter notebook to run
CalCraven Apr 28, 2021
a9281cd
Merge branch 'master' into vis_atype
CalCraven Apr 28, 2021
334d5d0
switch interactive functions to not be covered by codecov, create gms…
CalCraven Apr 29, 2021
813f605
Merge branch 'vis_atype' of https://github.com/CalCraven/gmso into vi…
CalCraven Apr 29, 2021
fcd1611
Merge branch 'master' into vis_atype
CalCraven May 3, 2021
494553d
add calls to io/nx_utils for functions in formats/networkx.py
CalCraven May 3, 2021
1e7cd86
fix __init__.py for importing networkx functionality
CalCraven May 6, 2021
9f3ad31
improve logic for unit tests
CalCraven May 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ignore:
- "gmso/examples"
- "gmso/tests"
- "gmso/formats/networkx.py"
2 changes: 2 additions & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ dependencies:
- pytest-cov
- codecov
- bump2version
- matplotlib
- ipywidgets
- ele >= 0.2.0
1 change: 0 additions & 1 deletion gmso/core/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ def _get_bonds_for(self, site):

def _get_angles_for(self, site):
"""Return a list of angles in this Topology that the site is a part of"""

angles = []
for angle in self.angles:
if site in angle.connection_members:
Expand Down
2 changes: 2 additions & 0 deletions gmso/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
from .gsd import write_gsd
from .xyz import read_xyz, write_xyz
from .lammpsdata import write_lammpsdata
from .networkx import (interactive_networkx_atomtypes, interactive_networkx_bonds,
interactive_networkx_angles, interactive_networkx_dihedrals)
319 changes: 319 additions & 0 deletions gmso/formats/networkx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
from gmso.utils.io import import_, has_ipywidgets, run_from_ipython
from gmso.utils import nx_utils
widgets = import_('ipywidgets')
plt = import_('matplotlib.pyplot')
if has_ipywidgets:
from ipywidgets import interact, fixed

from gmso.external.convert_networkx import to_networkx

def interactive_networkx_atomtypes(topology, list_of_labels=None):
"""Get an interactive networkx plot showing the atom types of a topology object.

Parameters
----------
topology : gmso.Topology
This should be a gmso topology object that you want to visualize the atom types
that have been parameterized.
list_of_labels : List[str]
Default labels are ['atom_type.name','charge','mass','element','label','position'].
Any additonal labels can be appended from the list of:
`dir(list(topology.sites)[0])` or `dir(list(topology.sites)[0].atom_type)`

Notes
-------
This function will output interactive ipywidgets. An ipywidgets dropdown object will
select which atomtypes that should be shown on the networkx graph object below. This
graph object is a visual representation for the connections within a gmso.Topology
object.
Select from a list of labels labels to be included on the graph.
See:
gmso.formats.networkx.interactive_networkx_bonds, gmso.formats.networkx.interactive_networkx_angles,
gmso.formats.networkx.interactive_networkx_dihedrals
for other interactive visualization methods.

ipywidgets.Dropdown()
Two dropdown options, corresponding to:
Label = What labels are shown on the networkx graph.
Atom_Name = Which sites will show these labels.
"""

if not run_from_ipython():
raise RuntimeError("Unsupported visualization outside of jupyter notebooks.")
networkx_graph = to_networkx(topology)
# get a unique list of site names
site_names = []
for node in networkx_graph.nodes:
site_names.append(node.name)
site_names = set(site_names)

# create a tuple of selectable names for each site
names_tuple = []
names_tuple.append(("All Sites", None))
for name in site_names:
names_tuple.append((name, name))

# Create list of labels to put on plot
if not list_of_labels:
list_of_labels = []
base_list = ["atom_type.name", "charge", "mass", "element", "label", "position"]
list_of_labels += base_list

@interact
def get_interactive_sites(Label=list_of_labels, Atom_Name=names_tuple):
# Plot atom types for the widget inputs
nx_utils.plot_networkx_atomtypes(topology, Atom_Name, [Label])

return

return


def interactive_networkx_bonds(topology, additional_labels=None):
"""Get an interactive networkx plot showing the bond types of a topology object.

Parameters
----------
topology : A gmso.core.topology.Topology object
This should be a gmso topology object that you want to visualize the atom types
that have been parameterized.
additonal_labels : Labels at each site to be included on the plot.
Default labels are ['atom_type.name'].
Any additonal labels can be appended from the list of:
`dir(list(topology.sites)[0])` or `dir(list(topology.sites)[0].atom_type)`

Notes
-------
This function will output interactive ipywidgets. An ipywidgets dropdown object will
select which atom names that will make up the bonds shown on the networkx graph object below.
This graph object is a visual representation for the connections within a gmso.Topology
object, and the bonds that makeup that bondtype are highlighted in red.
Select from a list of available sites, the sites that make up bonds to be highlighted on the graph.
See:
gmso.formats.networkx.interactive_networkx_atomtypes, gmso.formats.networkx.interactive_networkx_angles,
gmso.formats.networkx.interactive_networkx_dihedrals
for other interactive visualization methods.

ipywidgets.Dropdown()
Two dropdown options, corresponding to:
Atom1 (req) = Filters through all bonds to find bonds that contain this site. If not specified,
only bonds with missing bondtypes will be highlighted (in red).
Atom2 (opt) = A second site to specify which bonds can be selected on the graph.
A third dropdown option:
list_of_bonds = a list of the bonds labeled by the types that makeup the two sites.

matplotlib.pyplot.figure()
A figure showing the networkx.graph object of the molecule with highlighted edges corresponding to the
selected bonds

ipywidgets.checkbox()
A checkbox that will allow you to print the parameters associated with the selected bond in the list_of_bonds
dropdown option.
"""
if not run_from_ipython():
raise RuntimeError("Unsupported visualization outside of jupyter notebooks.")
networkx_graph = to_networkx(topology)

# Create a list of labels to go on the nodes
if not additional_labels:
additional_labels = []
base_list = ["atom_type.name"]
list_of_labels = base_list + additional_labels

# Create list of nodes to plot
site_names = []
for node in networkx_graph.nodes:
site_names.append(node.name)
site_names = set(site_names)

# Create a tuple of keys for each selected site
names_tuple = []
names_tuple.append(("All Sites", None))
for name in site_names:
names_tuple.append((name, name))
atom_selection = []
descriptions = ["Atom1 (req)", "Atom2 (opt)"]
for i in [0, 1]:
atom_selection.append(
widgets.Dropdown(
options=names_tuple,
layout=widgets.Layout(width="30%"),
style=dict(description_width="initial"),
description=descriptions[i],
)
)
interact(
nx_utils.call_interactive_sites,
Atom1=atom_selection[0],
Atom2=atom_selection[1],
list_of_labels=fixed(list_of_labels),
networkx_graph=fixed(networkx_graph),
topology=fixed(topology),
)

return


def interactive_networkx_angles(topology):
"""Get an interactive networkx plot showing the angle types of a topology object.

Parameters
----------
topology : A gmso.core.topology.Topology object
This should be a gmso topology object that you want to visualize the angle types
that have been parameterized.

Notes
-------
This function will output interactive ipywidgets. An ipywidgets dropdown object will
select which atom names that will make up the angles shown on the networkx graph object below.
This graph object is a visual representation for the connections within a gmso.Topology
object.
Select from a list of available sites, the sites that make up angles to be highlighted (in red) on the graph.
See:
gmso.formats.networkx.interactive_networkx_atomtypes, gmso.formats.networkx.interactive_networkx_bonds,
gmso.formats.networkx.interactive_networkx_dihedrals
for other interactive visualization methods.

ipywidgets.Dropdown()
Three dropdown options, corresponding to:
Central Atom1 (req) = Filters through all angles to find angles that contain this site as the center of the angle.
If not specified only angles with missing angletypes will be highlighted.
Atom2 (opt) = A second site to filter which angles can be selected on the graph.
Atom3 (opt) = A third site to filter which angles can be selected on the graph.
A fourth dropdown option:
Selected Edge = a list of the angles labeled by the types that makeup the three sites.

matplotlib.pyplot.figure()
A figure showing the networkx.graph object of the molecule with highlighted (in red) edges corresponding to the
selected angles.

ipywidgets.checkbox()
A checkbox that will allow you to print the parameters associated with the selected angle in the Selected Edge
dropdown option.
"""
if not run_from_ipython():
raise RuntimeError("Unsupported visualization outside of jupyter notebooks.")

networkx_graph = to_networkx(topology)

# Create list of nodes to plot
site_names = []
for node in networkx_graph.nodes:
site_names.append(node.name)
site_names = set(site_names)

# Create a tuple of keys for each selected site
names_tuple = []
names_tuple.append(("All Sites", None))
for name in site_names:
names_tuple.append((name, name))

# Call recursive interacts. The top level determines what bonds can be selected
atom_selection = []
descriptions = ["Central Atom1 (req)", "Atom2 (opt)", "Atom3 (opt)"]
for i in [0, 1, 2]:
atom_selection.append(
widgets.Dropdown(
options=names_tuple,
layout=widgets.Layout(width="30%"),
style=dict(description_width="initial"),
description=descriptions[i],
)
)
interact(
nx_utils.select_angles_from_sites,
networkx_graph=fixed(networkx_graph),
top=fixed(topology),
Atom1=atom_selection[0],
Atom2=atom_selection[1],
Atom3=atom_selection[2],
)

return


def interactive_networkx_dihedrals(topology):
"""Get an interactive networkx plot showing the dihedral types of a topology object.

Parameters
----------
topology : A gmso.core.topology.Topology object
This should be a gmso topology object that you want to visualize the dihedral types
that have been parameterized.

Notes
-------
This function will output interactive ipywidgets. An ipywidgets dropdown object will
select which atom names that will make up the dihedrals shown on the networkx graph object below.
This graph object is a visual representation for the connections within a gmso.Topology
object.
Select from a list of available sites, the sites that make up dihedrals to be highlighted (in red) on the graph.
See:
gmso.formats.networkx.interactive_networkx_atomtypes, gmso.formats.networkx.interactive_networkx_bonds,
gmso.formats.networkx.interactive_networkx_angles
for other interactive visualization methods.

ipywidgets.Dropdown()
Four dropdown options, corresponding to:
Central Atom1 (req) = Filters through all bonds to find dihedrals that contain this site in one of its central
two positions. If not specified, only dihedrals with missing dihedraltypes will be highlighted.
Central Atom2 (req) = Filters through all bonds to find dihedrals that contain this site in one of its central
two positions. If not specified, only dihedrals with missing dihedraltypes will be highlighted.
Atom3 (opt) = A third site to filter which dihedrals can be selected on the graph.
Atom4 (opt) = A fourth site to filter which dihedrals can be selected on the graph.
A fourth dropdown option:
Selected Edge = a list of the angles labeled by the types that makeup the three sites.

matplotlib.pyplot.figure()
A figure showing the networkx.graph object of the molecule with highlighted (in red) edges corresponding to the
selected dihedrals.

ipywidgets.checkbox()
A checkbox that will allow you to print the parameters associated with the selected dihedral in the Selected Edge
dropdown option.
"""
if not run_from_ipython():
raise RuntimeError("Unsupported visualization outside of jupyter notebooks.")

networkx_graph = to_networkx(topology)
# Create list of nodes to plot
site_names = []
for node in networkx_graph.nodes:
site_names.append(node.name)
site_names = set(site_names)

# Create a tuple of keys for each selected site
names_tuple = []
names_tuple.append(("All Sites", None))
for name in site_names:
names_tuple.append((name, name))

# Call recursive interacts. The top level determines what bonds can be selected
atom_selection = []
descriptions = [
"Central Atom1 (req)",
"Central Atom2 (req)",
"Atom3 (opt)",
"Atom4 (opt)",
]
for i in [0, 1, 2, 3]:
atom_selection.append(
widgets.Dropdown(
options=(names_tuple),
layout=widgets.Layout(width="30%"),
style=dict(description_width="initial"),
description=descriptions[i],
)
)
interact(
nx_utils.select_dihedrals_from_sites,
networkx_graph=fixed(networkx_graph),
top=fixed(topology),
Atom1=atom_selection[0],
Atom2=atom_selection[1],
Atom3=atom_selection[2],
Atom4=atom_selection[3],
)

return
Loading