Skip to content

Add Fuzzydict docstring#4846

Closed
Akhil-Sharma30 wants to merge 9 commits intopybamm-team:developfrom
Akhil-Sharma30:fuzzydict_docstring
Closed

Add Fuzzydict docstring#4846
Akhil-Sharma30 wants to merge 9 commits intopybamm-team:developfrom
Akhil-Sharma30:fuzzydict_docstring

Conversation

@Akhil-Sharma30
Copy link
Copy Markdown
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
Comment thread src/pybamm/util.py Outdated
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
Copy Markdown
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
Copy Markdown
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 thread src/pybamm/util.py Outdated
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
Copy Markdown
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

Comment thread src/pybamm/util.py
@codecov
Copy link
Copy Markdown

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 thread src/pybamm/util.py
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
Copy Markdown
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
Copy Markdown
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 thread src/pybamm/util.py Outdated
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
Copy Markdown
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 thread src/pybamm/util.py Outdated
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
Copy Markdown
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 thread src/pybamm/util.py Outdated
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
Copy Markdown
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.

Comment thread src/pybamm/util.py Outdated
as the original dictionary. It ensures that the copied dictionary retains
the fuzzy matching behavior.

Returns:
Copy link
Copy Markdown
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
-------

Comment thread src/pybamm/util.py

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

Choose a reason for hiding this comment

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

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

Comment thread src/pybamm/util.py

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

Choose a reason for hiding this comment

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

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

Comment thread src/pybamm/util.py
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
Copy Markdown
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
Copy Markdown
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 thread src/pybamm/util.py Outdated
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
Copy Markdown
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
Copy Markdown
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
@kratman
Copy link
Copy Markdown
Contributor

kratman commented Mar 18, 2025

@Akhil-Sharma30 Are you still working on this?

@kratman kratman added the needs-reply Needs further information from the author and may be closed if no response is received label Mar 18, 2025
@Akhil-Sharma30
Copy link
Copy Markdown
Contributor Author

Akhil-Sharma30 commented Mar 20, 2025

@Akhil-Sharma30 Are you still working on this?

But I have already made the changes asked for, waiting for the review from your end.

@github-actions github-actions Bot removed the needs-reply Needs further information from the author and may be closed if no response is received label Mar 20, 2025
@Saransh-cpp
Copy link
Copy Markdown
Member

@Akhil-Sharma30 did you forget to push your changes? The comments above are still unresolved.

@Akhil-Sharma30
Copy link
Copy Markdown
Contributor Author

Akhil-Sharma30 commented Mar 20, 2025

@Akhil-Sharma30 did you forget to push your changes? The comments above are still unresolved.

Could you please verify reflected as per #4846.

@Saransh-cpp
Copy link
Copy Markdown
Member

I don't see the new commits :(

Please make sure your local branch is synced with the remote branch.

@kratman
Copy link
Copy Markdown
Contributor

kratman commented May 12, 2025

@Akhil-Sharma30 Are you still working on this?

@kratman kratman added the needs-reply Needs further information from the author and may be closed if no response is received label May 12, 2025
@kratman kratman closed this Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-reply Needs further information from the author and may be closed if no response is received

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix the docs in pybamm.FuzzyDict class

3 participants