-
Notifications
You must be signed in to change notification settings - Fork 146
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
Node and Edge Filtering #886
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b88741d
Implementing filter_nodes and filter_edges funcs
danielleodigie a7de5ad
Merge branch 'Qiskit:main' into main
danielleodigie ca01739
Running fmt and clippy
danielleodigie 06119be
Merge branch 'main' of https://github.com/danielleodigie/rustworkx
danielleodigie 1a0f219
Fixed issue where errors were not being propagated up to Python. Crea…
danielleodigie a5301ef
Ran fmt, clippy, and tox
danielleodigie 8ed4220
Fixing release notes
danielleodigie 7f8b655
Merge branch 'Qiskit:main' into main
danielleodigie 363dcb4
Fixing release notes again
danielleodigie 9e13637
Fixing release notes again again
danielleodigie 496e1e8
Fixed release notes
danielleodigie ece5717
Fixed release notes. Changed Vec allocation. Expanded on documentation.
danielleodigie 193e55a
ran cargo fmt and clippy
danielleodigie 284b4d0
Fixing docs for filter functions
danielleodigie f3ba571
Merge branch 'main' into main
danielleodigie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
features: | ||
- | | ||
The :class:`~rustworkx.PyGraph` and the :class:`~rustworkx.PyDiGraph` classes have a new method | ||
:meth:`~rustworkx.PyGraph.filter_nodes` (or :meth:`~rustworkx.PyDiGraph.filter_nodes`). | ||
This method returns a :class:`~.NodeIndices` object with the resulting nodes that fit some abstract criteria indicated by a filter function. | ||
For example: | ||
|
||
.. jupyter-execute:: | ||
|
||
from rustworkx import PyGraph | ||
|
||
graph = PyGraph() | ||
graph.add_nodes_from(list(range(5))) # Adds nodes from 0 to 5 | ||
|
||
def my_filter_function(node): | ||
return node > 2 | ||
|
||
indices = graph.filter_nodes(my_filter_function) | ||
print(indices) | ||
|
||
- | | ||
The :class:`~rustworkx.PyGraph` and the :class:`~rustworkx.PyDiGraph` classes have a new method | ||
:meth:`~rustworkx.PyGraph.filter_edges` (or :meth:`~rustworkx.PyDiGraph.filter_edges`). | ||
This method returns a :class:`~.EdgeIndices` object with the resulting edges that fit some abstract criteria indicated by a filter function. | ||
For example: | ||
|
||
.. jupyter-execute:: | ||
|
||
from rustworkx import PyGraph | ||
from rustworkx.generators import complete_graph | ||
|
||
graph = PyGraph() | ||
graph.add_nodes_from(range(3)) | ||
graph.add_edges_from([(0, 1, 'A'), (0, 1, 'B'), (1, 2, 'C')]) | ||
|
||
def my_filter_function(edge): | ||
if edge: | ||
return edge == 'B' | ||
return False | ||
|
||
indices = graph.filter_edges(my_filter_function) | ||
print(indices) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import unittest | ||
|
||
import rustworkx as rx | ||
|
||
|
||
class TestFilter(unittest.TestCase): | ||
def test_filter_nodes(self): | ||
def my_filter_function1(node): | ||
return node == "cat" | ||
|
||
def my_filter_function2(node): | ||
return node == "lizard" | ||
|
||
def my_filter_function3(node): | ||
return node == "human" | ||
|
||
graph = rx.PyDiGraph() | ||
graph.add_node("cat") | ||
graph.add_node("cat") | ||
graph.add_node("dog") | ||
graph.add_node("lizard") | ||
graph.add_node("cat") | ||
cat_indices = graph.filter_nodes(my_filter_function1) | ||
lizard_indices = graph.filter_nodes(my_filter_function2) | ||
human_indices = graph.filter_nodes(my_filter_function3) | ||
self.assertEqual(list(cat_indices), [0, 1, 4]) | ||
self.assertEqual(list(lizard_indices), [3]) | ||
self.assertEqual(list(human_indices), []) | ||
|
||
def test_filter_edges(self): | ||
def my_filter_function1(edge): | ||
return edge == "friends" | ||
|
||
def my_filter_function2(edge): | ||
return edge == "enemies" | ||
|
||
def my_filter_function3(node): | ||
return node == "frenemies" | ||
|
||
graph = rx.PyDiGraph() | ||
graph.add_node("cat") | ||
graph.add_node("cat") | ||
graph.add_node("dog") | ||
graph.add_node("lizard") | ||
graph.add_node("cat") | ||
graph.add_edge(0, 2, "friends") | ||
graph.add_edge(0, 1, "friends") | ||
graph.add_edge(0, 3, "enemies") | ||
friends_indices = graph.filter_edges(my_filter_function1) | ||
enemies_indices = graph.filter_edges(my_filter_function2) | ||
frenemies_indices = graph.filter_edges(my_filter_function3) | ||
self.assertEqual(list(friends_indices), [0, 1]) | ||
self.assertEqual(list(enemies_indices), [2]) | ||
self.assertEqual(list(frenemies_indices), []) | ||
|
||
def test_filter_errors(self): | ||
def my_filter_function1(node): | ||
raise TypeError("error!") | ||
|
||
graph = rx.PyDiGraph() | ||
graph.add_node("cat") | ||
graph.add_node("cat") | ||
graph.add_node("dog") | ||
graph.add_edge(0, 1, "friends") | ||
graph.add_edge(1, 2, "enemies") | ||
with self.assertRaises(TypeError): | ||
graph.filter_nodes(my_filter_function1) | ||
with self.assertRaises(TypeError): | ||
graph.filter_edges(my_filter_function1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import unittest | ||
|
||
import rustworkx as rx | ||
|
||
|
||
class TestFilter(unittest.TestCase): | ||
def test_filter_nodes(self): | ||
def my_filter_function1(node): | ||
return node == "cat" | ||
|
||
def my_filter_function2(node): | ||
return node == "lizard" | ||
|
||
def my_filter_function3(node): | ||
return node == "human" | ||
|
||
graph = rx.PyGraph() | ||
graph.add_node("cat") | ||
graph.add_node("cat") | ||
graph.add_node("dog") | ||
graph.add_node("lizard") | ||
graph.add_node("cat") | ||
cat_indices = graph.filter_nodes(my_filter_function1) | ||
lizard_indices = graph.filter_nodes(my_filter_function2) | ||
human_indices = graph.filter_nodes(my_filter_function3) | ||
self.assertEqual(list(cat_indices), [0, 1, 4]) | ||
self.assertEqual(list(lizard_indices), [3]) | ||
self.assertEqual(list(human_indices), []) | ||
|
||
def test_filter_edges(self): | ||
def my_filter_function1(edge): | ||
return edge == "friends" | ||
|
||
def my_filter_function2(edge): | ||
return edge == "enemies" | ||
|
||
def my_filter_function3(node): | ||
return node == "frenemies" | ||
|
||
graph = rx.PyGraph() | ||
graph.add_node("cat") | ||
graph.add_node("cat") | ||
graph.add_node("dog") | ||
graph.add_node("lizard") | ||
graph.add_node("cat") | ||
graph.add_edge(0, 2, "friends") | ||
graph.add_edge(0, 1, "friends") | ||
graph.add_edge(0, 3, "enemies") | ||
friends_indices = graph.filter_edges(my_filter_function1) | ||
enemies_indices = graph.filter_edges(my_filter_function2) | ||
frenemies_indices = graph.filter_edges(my_filter_function3) | ||
self.assertEqual(list(friends_indices), [0, 1]) | ||
self.assertEqual(list(enemies_indices), [2]) | ||
self.assertEqual(list(frenemies_indices), []) | ||
|
||
def test_filter_errors(self): | ||
def my_filter_function1(node): | ||
raise TypeError("error!") | ||
|
||
graph = rx.PyGraph() | ||
graph.add_node("cat") | ||
graph.add_node("cat") | ||
graph.add_node("dog") | ||
graph.add_edge(0, 1, "friends") | ||
graph.add_edge(1, 2, "enemies") | ||
with self.assertRaises(TypeError): | ||
graph.filter_nodes(my_filter_function1) | ||
with self.assertRaises(TypeError): | ||
graph.filter_edges(my_filter_function1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs a little more detail on how this is used. I think we should include what the format is for the filter function (what it gets passed and the required return type). Maybe having an example in the docs here would be useful too since it might be unclear even with an English description of the behavior.
This comment applies to all the functions being added in both graph and digraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the example goes a long way for clarity thanks for adding that. But I still think we should explicitly document the inputs and required output from
filter_function
so that it's clear to users that it will be passed a node weight and is required to return a boolean value (this applies to all the docstrings for the other 3 methods too).