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

Improves search to handle smaller search terms. #4735

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Added two more submodels (options) for the SEI: Lars von Kolzenberg (2020) model and Tunneling Limit model ([#4394](https://github.com/pybamm-team/PyBaMM/pull/4394))
- Automatically add state variables of the model to the output variables if they are not already present ([#4700](https://github.com/pybamm-team/PyBaMM/pull/4700))

## Optimizations

- Improved search to handle cases with shorter input strings and provide more relevant results.([#4735](https://github.com/pybamm-team/PyBaMM/pull/4735))

## Breaking changes

- Updated BPX to v0.5.0 and made changes for the switch to Pydantic V2 ([#4701](https://github.com/pybamm-team/PyBaMM/pull/4701))
Expand Down
52 changes: 42 additions & 10 deletions src/pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,31 @@ def _find_matches(self, search_key: str, known_keys: list[str]):
The list of known dictionary keys to search within.
"""
exact = [key for key in known_keys if search_key in key.lower()]
partial = difflib.get_close_matches(search_key, known_keys, n=5, cutoff=0.5)
return exact, partial
search_key = search_key.lower()
min_similarity = 0.4

exact_matches = []
partial_matches = []

for key in known_keys:
key_lower = key.lower()
if search_key in key_lower:
key_words = key_lower.split()
significant_match = False

for word in key_words:
similarity = difflib.SequenceMatcher(None, search_key, word).ratio()

if similarity >= min_similarity:
significant_match = True

if significant_match:
exact_matches.append(key)
else:
partial_matches = difflib.get_close_matches(
search_key, known_keys, n=5, cutoff=0.5
)
return exact_matches, partial_matches

def search(self, keys: str | list[str], print_values: bool = False):
"""
Expand Down Expand Up @@ -163,14 +185,24 @@ def search(self, keys: str | list[str], print_values: bool = False):
search_keys = [k.strip().lower() for k in keys if k.strip()]

known_keys = list(self.keys())
known_keys.sort()

min_similarity = 0.4
Copy link
Member

Choose a reason for hiding this comment

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

min_similarity is also defined above, so it gets defined twice. How about making it an argument for _find_matches()?

Follow-up question: do you think it would make sense to expose it publicly for users through search() as well?

# Check for exact matches where all search keys appear together in a key
exact_matches = [
key
for key in known_keys
if all(term in key.lower() for term in search_keys)
]
exact_matches = []
for key in known_keys:
key_lower = key.lower()
if all(term in key_lower for term in search_keys):
key_words = key_lower.split()

# Ensure all search terms match at least one word in the key
if all(
any(
difflib.SequenceMatcher(None, term, word).ratio()
>= min_similarity
for word in key_words
)
for term in search_keys
):
exact_matches.append(key)

if exact_matches:
print(
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,9 @@ def test_url_gets_to_stdout(self, mocker):
match="'keys' must be a string or a list of strings, got <class 'int'>",
):
model.variables.search(123)

# Test smaller strings
with mocker.patch("sys.stdout", new=StringIO()) as fake_out:
model.variables.search(["El", "co"], print_values=True)
out = "No matches found for 'El'\n" "No matches found for 'co'\n"
assert fake_out.getvalue() == out