def test_plot_networkx_nodes(self, typed_ethane): graph = to_networkx(typed_ethane) fig, ax = plt.subplots(1, 1) plot_networkx_nodes(graph, ax, edge_weights={1: 5}, edge_colors={1: "r"})
def test_select_params_on_networkx_output(self, typed_ethane, capsys): graph = to_networkx(typed_ethane) select_params_on_networkx(graph, [None, None, None]) captured, err = capsys.readouterr() assert captured.startswith("All angles") select_params_on_networkx(graph, [None, None, None, None]) captured, err = capsys.readouterr() assert captured.startswith("All dihedrals") for node, angles in graph.nodes(data="angles"): if angles[0]: angles[0].angle_type = None result = select_params_on_networkx(graph, [None, None, None]) captured, err = capsys.readouterr() assert captured.startswith("Since no sites are input, angles") for node, dihedrals in graph.nodes(data="dihedrals"): if dihedrals[0]: dihedrals[0].dihedral_type = None result = select_params_on_networkx(graph, [None, None, None, None]) captured, err = capsys.readouterr() assert captured.startswith("Since no sites are input, dihedrals") nx.set_node_attributes(graph, None, name="angles") result = select_params_on_networkx(graph, [None, None, None]) captured, err = capsys.readouterr() assert captured.startswith("No angle") nx.set_node_attributes(graph, None, name="dihedrals") result = select_params_on_networkx(graph, [None, None, None, None]) captured, err = capsys.readouterr() assert captured.startswith("No dihedral") result = select_params_on_networkx(graph, [None, None]) captured, err = capsys.readouterr() assert captured.startswith("invalid")
def test_to_networkx_ethane(self, ethane): ethane_to_nx = to_networkx(ethane) assert ethane.n_sites == ethane_to_nx.number_of_nodes() assert ethane.n_bonds == ethane_to_nx.number_of_edges() assert set(ethane.sites) == set(ethane_to_nx.nodes)
def test_from_networkx_ethane(self, ethane): ethane_to_nx = to_networkx(ethane) ethane_from_nx = from_networkx(ethane_to_nx) assert ethane.n_sites == ethane_from_nx.n_sites assert ethane.n_bonds == ethane_from_nx.n_bonds assert set(ethane.sites) == set(ethane_from_nx.sites) assert set(ethane.bonds) == set(ethane_from_nx.bonds)
def test_from_networkx_argon(self, ar_system): ar_to_nx = to_networkx(ar_system) ar_from_nx = from_networkx(ar_to_nx) assert ar_system.n_sites == ar_from_nx.n_sites assert ar_system.n_bonds == ar_from_nx.n_bonds assert set(ar_system.sites) == set(ar_from_nx.sites) assert set(ar_system.bonds) == set(ar_from_nx.bonds)
def test_return_labels_for_nodes(self, typed_ethane): graph = to_networkx(typed_ethane) assert (len( return_labels_for_nodes( graph.nodes, ["atom_type.name", "charge", "positions"])) == 8) assert (list( return_labels_for_nodes( graph.nodes, ["atom_type.error"]).values())[0][-8:] == "NoneType") assert (list(return_labels_for_nodes( graph.nodes, ["error"]).values())[0][-8:] == "NoneType")
def test_plot_networkx_params(self, typed_ethane): graph = to_networkx(typed_ethane) list_of_edges = get_networkx_edges(list_of_params=[ typed_ethane.dihedrals[0].connection_members, typed_ethane.dihedrals[1].connection_members, ]) fig, ax = plot_networkx_params(graph, list_of_edges) test_fig, test_ax = plt.subplots(1) assert isinstance(fig, test_fig.__class__) assert isinstance(ax, test_ax.__class__)
def test_to_networkx_ethane(self, ethane): ethane_to_nx = to_networkx(ethane) assert ethane.n_sites == ethane_to_nx.number_of_nodes() assert ethane.n_bonds == ethane_to_nx.number_of_edges() assert set(ethane.sites) == set(ethane_to_nx.nodes) for site in ethane.sites: assert set(ethane_to_nx.nodes[site]['angles']) == set( ethane._get_angles_for(site)) assert set(ethane_to_nx.nodes[site]['dihedrals']) == set( ethane._get_dihedrals_for(site))
def test_highlight_networkx_edges(self, typed_ethane): list(typed_ethane.angles)[0].angle_type = None list(typed_ethane.dihedrals)[0].dihedral_type = None graph = to_networkx(typed_ethane) list_edges = list(graph.edges)[0:3] test_edge_weights, test_edge_colors = highlight_networkx_edges( graph, list_edges[0:2]) assert test_edge_weights[list_edges[0]] == 5 assert test_edge_weights[list_edges[1]] == 5 assert test_edge_weights[list_edges[2]] == 1 assert test_edge_colors[list_edges[0]] == "r" assert test_edge_colors[list_edges[1]] == "r" assert test_edge_colors[list_edges[2]] == "k"
def test_select_params_on_networkx(self, typed_ethane, capsys): graph = to_networkx(typed_ethane) assert (len(select_params_on_networkx(graph, [None, None, None, None])) == 0) assert len(select_params_on_networkx(graph, ["C", "H", "H"])) == 1 assert len(select_params_on_networkx(graph, ["C", "C", "H", "H"])) == 1 assert len(select_params_on_networkx(graph, [None, None, None])) == 0 assert len(select_params_on_networkx(graph, ["C", "H", None])) == 3 assert len(select_params_on_networkx(graph, ["C", None, None])) == 3 assert len(select_params_on_networkx(graph, ["C", "C", "H", None])) == 1 assert (len(select_params_on_networkx(graph, ["C", "C", None, None])) == 1)
def test_select_angles_from_sites(self, typed_ethane, capsys): graph = to_networkx(typed_ethane) select_angles_from_sites(graph, typed_ethane, Atom1="C", Atom2="H", Atom3="C") select_angles_from_sites(graph, typed_ethane, Atom1="O", Atom2="H", Atom3="C") captured, err = capsys.readouterr() assert isinstance(err, str)
def test_from_networkx_water_box(self, water_system): water_to_nx = to_networkx(water_system) water_from_nx = from_networkx(water_to_nx) assert water_system.n_sites == water_from_nx.n_sites assert water_system.n_bonds == water_from_nx.n_bonds assert set(water_system.sites) == set(water_from_nx.sites) assert set(water_system.bonds) == set(water_from_nx.bonds) # The number fragments in the networkX representation == n_subtops # TODO: create subtops for each fragment in `from_networkx()` assert (nx.number_connected_components(water_to_nx) == water_system.n_subtops)
def plot_networkx_atomtypes(topology, atom_name=None, list_of_labels=["atom_type.name"]): """Get a networkx plot showing the atom types in 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. atom_name : The atom name which will have larger node sizes. When drawing the networkx graph, all atoms with this name will be 3X as large. This input will be of type string. To see what atom names are available, use for site in topology.sites: print(site.name) Returns ------- matplotlib.pyplot.figure The drawn networkx plot of the topology on a matplotlib editable figures matplotlib.pyplot.axis The axis information that corresponds to that figure. This output can be shown using matplotlib.pyplot.show() """ fig, ax = plt.subplots(1, 1, figsize=(8, 8)) networkx_graph = to_networkx(topology) # larger nodes for the atom selected node_sizes = [] for node in networkx_graph.nodes: if node.name == atom_name: node_sizes.append(900) else: node_sizes.append(300) ax = plot_networkx_nodes( networkx_graph, ax, edge_weights=None, edge_colors=None, node_sizes=node_sizes, list_of_labels=list_of_labels, atom_name=atom_name, ) return (fig, ax)
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 Also -------- 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 test_plot_networkx_bonds(self, typed_ethane): graph = to_networkx(typed_ethane) fig, ax = plot_networkx_bonds(graph) test_fig, test_ax = plt.subplots(1) assert isinstance(fig, test_fig.__class__) assert isinstance(ax, test_ax.__class__)
def test_identify_labels(self, typed_ethane): graph = to_networkx(typed_ethane) assert len(identify_labels(graph, ["name"], atom_name="C")) == 2
def test_show_bond_info(self, typed_ethane): graph = to_networkx(typed_ethane) list_of_edges = get_edges(graph, "C", "C") assert not show_bond_info(True, typed_ethane, list_of_edges) assert not show_bond_info(False, typed_ethane, list_of_edges)
def test_select_dihedrals_from_sites(self, typed_ethane, capsys): graph = to_networkx(typed_ethane) select_dihedrals_from_sites(graph, typed_ethane) captured, err = capsys.readouterr() assert isinstance(err, str)
def test_call_interactive_sites(self, typed_ethane): graph = to_networkx(typed_ethane) assert not call_interactive_sites( "C", "C", graph, typed_ethane, list_of_labels=["name"]) assert not call_interactive_sites( "C", "O", graph, typed_ethane, list_of_labels=["name"])
def test_select_edges(self, typed_ethane): graph = to_networkx(typed_ethane) list_of_edges = get_edges(graph, "C", "C") assert not select_edges(graph, typed_ethane, list_of_edges, ["name"])
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 Also -------- 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
def test__get_formatted_atom_types_names_for(self, typed_ethane): graph = to_networkx(typed_ethane) for node, dihedrals in graph.nodes(data="angles"): assert isinstance( _get_formatted_atom_types_names_for(dihedrals[0]), str)
def test_get_edges(self, typed_ethane): graph = to_networkx(typed_ethane) assert len(get_edges(graph, "C", "C")[0][1]) == 1 assert len(get_edges(graph, "H", None)[0][1]) == 6 assert not get_edges(graph, None, None)
def test_select_edges_on_networkx(self, typed_ethane, capsys): graph = to_networkx(typed_ethane) edges = select_params_on_networkx(graph, ["C", "C", "H"]) select_edges_on_networkx(graph, typed_ethane, edges[0][1]) captured, err = capsys.readouterr() assert isinstance(err, str)
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 Also -------- 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): """Return the interactive sites.""" # Plot atom types for the widget inputs nx_utils.plot_networkx_atomtypes(topology, Atom_Name, [Label]) return return