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 Fuzzydict docstring #4846

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

Akhil-Sharma30
Copy link
Contributor

Description

In this PR fixed the following bugs in pybamm.util.py:

  • pybamm.FuzzyDict.copy. needs docstring
  • pybamm.FuzzyDict. get_best_matches. document key arg
  • pybamm.FuzzyDict. search. document args. format code properly

Fixes #4845

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@Akhil-Sharma30 Akhil-Sharma30 requested a review from a team as a code owner February 13, 2025 19:07
Retrieves the value associated with a key, handling renamed terms and
suggesting closest matches if the key is not found.

_find_matches(search_key, known_keys)
Copy link
Contributor

Choose a reason for hiding this comment

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

Private methods should not be documented

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay I have removed the private methods.

Comment on lines 44 to 46
__getitem__(key)
Retrieves the value associated with a key, handling renamed terms and
suggesting closest matches if the key is not found.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably better documented as an example rather then the method itself

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.70%. Comparing base (9ec472d) to head (f262534).
Report is 7 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4846   +/-   ##
========================================
  Coverage    98.70%   98.70%           
========================================
  Files          303      303           
  Lines        23335    23335           
========================================
  Hits         23032    23032           
  Misses         303      303           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +214 to +216
This method returns a new FuzzyDict object containing the same key-value pairs
as the original dictionary. It ensures that the copied dictionary retains
the fuzzy matching behavior.
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an AI generated text 🙁

Anyways, it is redundant. The first sentence makes everything clear.

Suggested change
This method returns a new FuzzyDict object containing the same key-value pairs
as the original dictionary. It ensures that the copied dictionary retains
the fuzzy matching behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extremely sorry, I have re-written the doc-string and added an example case.

Comment on lines 34 to 38
Features:
- Fuzzy matching using `difflib.get_close_matches` to suggest the closest keys.
- Custom error handling for specific key renamings with informative messages.
- Search functionality to find keys containing specific terms.
- Custom warnings for deprecated key names.
Copy link
Member

Choose a reason for hiding this comment

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

Does not add anything. I am not against AI, but it is always important to check the results before adding them in.

Suggested change
Features:
- Fuzzy matching using `difflib.get_close_matches` to suggest the closest keys.
- Custom error handling for specific key renamings with informative messages.
- Search functionality to find keys containing specific terms.
- Custom warnings for deprecated key names.

Comment on lines 40 to 49
Methods:
get_best_matches(key)
Returns a list of the best-matching keys for a given input key.

search(keys, print_values=False)
Searches the dictionary for keys containing all specified terms. Prints
the results and, optionally, the corresponding values.

copy()
Returns a copy of the FuzzyDict instance.
Copy link
Member

Choose a reason for hiding this comment

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

Methods and their respective docstrings are automatically rendered in the docs

Suggested change
Methods:
get_best_matches(key)
Returns a list of the best-matching keys for a given input key.
search(keys, print_values=False)
Searches the dictionary for keys containing all specified terms. Prints
the results and, optionally, the corresponding values.
copy()
Returns a copy of the FuzzyDict instance.

Comment on lines 29 to 32
This class extends the built-in `dict` to provide intelligent key lookup,
including approximate string matching and handling of renamed terms. It is
useful when working with parameter dictionaries where keys might have slight
variations, renamings, or formatting differences.
Copy link
Member

Choose a reason for hiding this comment

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

The first sentence captures the gist here too, so these lines look redundant to me.

as the original dictionary. It ensures that the copied dictionary retains
the fuzzy matching behavior.

Returns:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Returns:
Returns
-------

@@ -96,7 +130,7 @@ def _find_matches(self, search_key: str, known_keys: list[str]):
Helper method to find exact and partial matches for a given search key.

Parameters
----------
-------
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
-------
----------

@@ -115,7 +149,7 @@ def search(self, keys: str | list[str], print_values: bool = False):
the best matches are printed.

Parameters
----------
-------
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
-------
----------

Comment on lines +29 to +40
Methods
-------
get_best_matches(key)
Returns a list of the best-matching keys for a given input key.

search(keys, print_values=False)
Searches the dictionary for keys containing all specified terms. Prints
the results and, optionally, the corresponding values.

copy()
Returns a copy of the FuzzyDict instance.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Methods
-------
get_best_matches(key)
Returns a list of the best-matching keys for a given input key.
search(keys, print_values=False)
Searches the dictionary for keys containing all specified terms. Prints
the results and, optionally, the corresponding values.
copy()
Returns a copy of the FuzzyDict instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove this section?

Comment on lines 41 to 58
Example usage
-------
```python
>>> params = FuzzyDict({
>>> "electrode diffusivity": 1.2,
>>> "particle diffusivity": 0.8,
>>> "Negative electrode SOC": 0.5,
>>> "Open-circuit voltage at 100% SOC [V]": 4.2,
>>> })

>>> print(params["electrode diffusivity"]) # 1.2 (direct access)
>>> print(params["particle diffusivity"]) # 0.8 (handles renaming)

>>> params.search("open circuit voltage")

>>> params_copy = params.copy() # Creates a new FuzzyDict instance
```
"""
Copy link
Member

Choose a reason for hiding this comment

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

You will need to fix this examples as the doctests will fail

Suggested change
Example usage
-------
```python
>>> params = FuzzyDict({
>>> "electrode diffusivity": 1.2,
>>> "particle diffusivity": 0.8,
>>> "Negative electrode SOC": 0.5,
>>> "Open-circuit voltage at 100% SOC [V]": 4.2,
>>> })
>>> print(params["electrode diffusivity"]) # 1.2 (direct access)
>>> print(params["particle diffusivity"]) # 0.8 (handles renaming)
>>> params.search("open circuit voltage")
>>> params_copy = params.copy() # Creates a new FuzzyDict instance
```
"""
Examples
--------
>>> params = FuzzyDict({
>>> "electrode diffusivity": 1.2,
>>> "particle diffusivity": 0.8,
>>> "Negative electrode SOC": 0.5,
>>> "Open-circuit voltage at 100% SOC [V]": 4.2,
>>> })
>>> print(params["electrode diffusivity"])
1.2
>>> print(params["particle diffusivity"])
0.8
>>> params.search("open circuit voltage")
>>> params_copy = params.copy() # Creates a new FuzzyDict instance
"""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified the example.

@agriyakhetarpal agriyakhetarpal marked this pull request as draft February 17, 2025 02:32
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.

Fix the docs in pybamm.FuzzyDict class
3 participants