From bfa847c2ae2887c99893876fc0230279b77ce76b Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Thu, 13 Feb 2025 23:12:50 +0530 Subject: [PATCH 1/9] Add Doc-String for FuzzyDict class --- src/pybamm/util.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 11580967ff..3579cbce24 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -21,8 +21,42 @@ def root_dir(): """return the root directory of the PyBaMM install directory""" return str(pathlib.Path(pybamm.__path__[0]).parent.parent) - class FuzzyDict(dict): + """ + A dictionary with fuzzy matching capabilities for key retrieval and search. + + 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. + + 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. + + Methods + ------- + get_best_matches(key) + Returns a list of the best-matching keys for a given input key. + + __getitem__(key) + 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) + Finds exact and partial matches for a given search term in the dictionary keys. + + 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. + + """ + def get_best_matches(self, key): """Get best matches from keys""" return difflib.get_close_matches(key, list(self.keys()), n=3, cutoff=0.5) From 5c896d63b56f0f7b730e398d36084542a814f722 Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Fri, 14 Feb 2025 00:22:55 +0530 Subject: [PATCH 2/9] Add doc-string for fuzzyDict.copy func --- src/pybamm/util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 3579cbce24..3c15b7768d 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -216,7 +216,21 @@ def search(self, keys: str | list[str], print_values: bool = False): else: print(f"No matches found for '{original_key}'") + def copy(self): + """ + Create and return a copy of the FuzzyDict instance. + + 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. + + Returns + ------- + FuzzyDict + A new FuzzyDict instance with the same data as the original. + """ + return FuzzyDict(super().copy()) From 58e937e45ebdbe8f72f25d2b4bd5a43beae32015 Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Fri, 14 Feb 2025 00:30:16 +0530 Subject: [PATCH 3/9] Fix the indentation --- src/pybamm/util.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 3c15b7768d..3f83f48f37 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -36,8 +36,7 @@ class FuzzyDict(dict): - Search functionality to find keys containing specific terms. - Custom warnings for deprecated key names. - Methods - ------- + Methods: get_best_matches(key) Returns a list of the best-matching keys for a given input key. @@ -130,7 +129,6 @@ 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 - ---------- search_key : str The term to search for in the keys. known_keys : list of str @@ -149,7 +147,6 @@ def search(self, keys: str | list[str], print_values: bool = False): the best matches are printed. Parameters - ---------- keys : str or list of str Search term(s) print_values : bool, optional @@ -225,8 +222,7 @@ def copy(self): as the original dictionary. It ensures that the copied dictionary retains the fuzzy matching behavior. - Returns - ------- + Returns: FuzzyDict A new FuzzyDict instance with the same data as the original. """ From 21942317131defc30d231cf83de5c46c7dc6cd1b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Feb 2025 19:07:25 +0000 Subject: [PATCH 4/9] style: pre-commit fixes --- src/pybamm/util.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 3f83f48f37..ee8fb8f904 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -21,12 +21,13 @@ def root_dir(): """return the root directory of the PyBaMM install directory""" return str(pathlib.Path(pybamm.__path__[0]).parent.parent) + class FuzzyDict(dict): """ A dictionary with fuzzy matching capabilities for key retrieval and search. This class extends the built-in `dict` to provide intelligent key lookup, - including approximate string matching and handling of renamed terms. It is + 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. @@ -35,22 +36,22 @@ class FuzzyDict(dict): - Custom error handling for specific key renamings with informative messages. - Search functionality to find keys containing specific terms. - Custom warnings for deprecated key names. - + Methods: get_best_matches(key) Returns a list of the best-matching keys for a given input key. - + __getitem__(key) - Retrieves the value associated with a key, handling renamed terms and + 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) Finds exact and partial matches for a given search term in the dictionary keys. - + 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. @@ -213,20 +214,19 @@ def search(self, keys: str | list[str], print_values: bool = False): else: print(f"No matches found for '{original_key}'") - def copy(self): """ Create and return a copy of the FuzzyDict instance. - This method returns a new FuzzyDict object containing the same key-value pairs - as the original dictionary. It ensures that the copied dictionary retains + 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. Returns: FuzzyDict A new FuzzyDict instance with the same data as the original. """ - + return FuzzyDict(super().copy()) From 577260aea5b6e48cef0b803ee227f43723b67875 Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Fri, 14 Feb 2025 01:10:10 +0530 Subject: [PATCH 5/9] Remove the private func --- src/pybamm/util.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index ee8fb8f904..27507da48e 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -41,13 +41,6 @@ class FuzzyDict(dict): get_best_matches(key) Returns a list of the best-matching keys for a given input key. - __getitem__(key) - 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) - Finds exact and partial matches for a given search term in the dictionary keys. - search(keys, print_values=False) Searches the dictionary for keys containing all specified terms. Prints the results and, optionally, the corresponding values. From b8abf29e2d185ece670f64c52d5faad77b8391f2 Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Fri, 14 Feb 2025 10:09:44 +0530 Subject: [PATCH 6/9] Docs: Re-worked on the FuzzyDict class doc-string --- src/pybamm/util.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 27507da48e..46a77ad5a9 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -26,17 +26,6 @@ class FuzzyDict(dict): """ A dictionary with fuzzy matching capabilities for key retrieval and search. - 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. - - 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. - Methods: get_best_matches(key) Returns a list of the best-matching keys for a given input key. @@ -48,6 +37,23 @@ class FuzzyDict(dict): copy() Returns a copy of the FuzzyDict instance. + 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 + ``` """ def get_best_matches(self, key): From f26253466b037a8896d5af85dd1fd26d3888f35d Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Fri, 14 Feb 2025 10:14:36 +0530 Subject: [PATCH 7/9] Docs: Improve formatting --- src/pybamm/util.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 46a77ad5a9..1fd37753dc 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -26,7 +26,8 @@ class FuzzyDict(dict): """ A dictionary with fuzzy matching capabilities for key retrieval and search. - Methods: + Methods + ------- get_best_matches(key) Returns a list of the best-matching keys for a given input key. @@ -37,8 +38,8 @@ class FuzzyDict(dict): copy() Returns a copy of the FuzzyDict instance. - Example usage: - + Example usage + ------- ```python >>> params = FuzzyDict({ >>> "electrode diffusivity": 1.2, @@ -129,6 +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 + ------- search_key : str The term to search for in the keys. known_keys : list of str @@ -147,6 +149,7 @@ def search(self, keys: str | list[str], print_values: bool = False): the best matches are printed. Parameters + ------- keys : str or list of str Search term(s) print_values : bool, optional @@ -221,7 +224,8 @@ def copy(self): as the original dictionary. It ensures that the copied dictionary retains the fuzzy matching behavior. - Returns: + Returns + ------- FuzzyDict A new FuzzyDict instance with the same data as the original. """ From b3d8262b56e8a711a5c3b5f03916fb67b86f30d3 Mon Sep 17 00:00:00 2001 From: AKHIL SHARMA Date: Mon, 17 Feb 2025 14:35:46 +0530 Subject: [PATCH 8/9] Add example in the doc-string --- src/pybamm/util.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index 1fd37753dc..f4bc7e66ef 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -38,23 +38,21 @@ class FuzzyDict(dict): copy() Returns a copy of the FuzzyDict instance. - 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 + """ def get_best_matches(self, key): From a352e8f9065ca900ee2eada446278f4fc7a042f5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Feb 2025 09:06:18 +0000 Subject: [PATCH 9/9] style: pre-commit fixes --- src/pybamm/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pybamm/util.py b/src/pybamm/util.py index f4bc7e66ef..14de3a841d 100644 --- a/src/pybamm/util.py +++ b/src/pybamm/util.py @@ -52,7 +52,7 @@ class FuzzyDict(dict): 0.8 >>> params.search("open circuit voltage") >>> params_copy = params.copy() # Creates a new FuzzyDict instance - + """ def get_best_matches(self, key):