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

Issue 3361 - Improve print_parameter_info functionality #3584

Merged
merged 18 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
562815b
Added "parameter_info" and modified "print_parameter_info"
cringeyburger Nov 29, 2023
d9a1d94
Added docstrings and exception handling to parameter_info and print_p…
cringeyburger Dec 3, 2023
b86f58c
Added Issue 3361 to features' section of CHANGELOG.md
cringeyburger Dec 3, 2023
e2f8a43
Implemented "get_parameter_info" to return a dictionary of parameters…
cringeyburger Dec 5, 2023
d38a8b5
Improved readability
cringeyburger Dec 5, 2023
6bcc50c
Added docstrings and modified "get_parameter_info" to store the entir…
cringeyburger Dec 6, 2023
5fb4ed2
Implemented a parameter table to print parameter info, from"print_par…
cringeyburger Dec 6, 2023
01723a5
parameter table size optimisation and code formatting
cringeyburger Dec 7, 2023
292086d
Made the parameter table dynamic and modified docstrings of "get_para…
cringeyburger Dec 7, 2023
3ab36c0
Added "get_parameter_info" and modified information about "print_para…
cringeyburger Dec 7, 2023
9c46caf
Merge branch 'develop' into issue-3361-improve-print_parameter_info-f…
cringeyburger Dec 7, 2023
0da48b2
Update pybamm/models/base_model.py
valentinsulzer Dec 7, 2023
af26526
Modified "get_parameter_info" and "print_parameter_info" to store and…
cringeyburger Dec 10, 2023
1618199
Merge remote-tracking branch 'origin/issue-3361-improve-print_paramet…
cringeyburger Dec 10, 2023
5ccd185
Modified "get_parameter_info" and "print_parameter_info" to store and…
cringeyburger Dec 10, 2023
d365023
Updated all instances of "_parameter_info" with "get_parameter_info" …
cringeyburger Dec 10, 2023
53bedfd
Merge branch 'develop' into issue-3361-improve-print_parameter_info-f…
cringeyburger Dec 10, 2023
55a70da
Style and docstring modifications
cringeyburger Dec 12, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- Added a new unary operator, `EvaluateAt`, that evaluates a spatial variable at a given position ([#3573](https://github.com/pybamm-team/PyBaMM/pull/3573))
- Added a method, `insert_reference_electrode`, to `pybamm.lithium_ion.BaseModel` that insert a reference electrode to measure the electrolyte potential at a given position in space and adds new variables that mimic a 3E cell setup. ([#3573](https://github.com/pybamm-team/PyBaMM/pull/3573))
- Serialisation added so models can be written to/read from JSON ([#3397](https://github.com/pybamm-team/PyBaMM/pull/3397))
- Added a `get_parameter_info` method for models and modified "print_parameter_info" functionality to extract all parameters and their type in a tabular and readable format ([#3361](https://github.com/pybamm-team/PyBaMM/pull/3584))
cringeyburger marked this conversation as resolved.
Show resolved Hide resolved
- Mechanical parameters are now a function of stoichiometry and temperature ([#3576](https://github.com/pybamm-team/PyBaMM/pull/3576))


## Bug fixes

Expand Down
664 changes: 209 additions & 455 deletions docs/source/examples/notebooks/parameterization/parameterization.ipynb

Large diffs are not rendered by default.

66 changes: 49 additions & 17 deletions pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,31 +421,63 @@ def input_parameters(self):
self._input_parameters = self._find_symbols(pybamm.InputParameter)
return self._input_parameters

def print_parameter_info(self):
"""Returns parameters used in the model"""
self._parameter_info = ""
def get_parameter_info(self):
cringeyburger marked this conversation as resolved.
Show resolved Hide resolved
"""
Extract the parameter information and returns it as a dictionary.
To get a list of all parameter-like objects without extra information,
use `model.parameters`.
cringeyburger marked this conversation as resolved.
Show resolved Hide resolved
"""
parameter_info = {}
parameters = self._find_symbols(pybamm.Parameter)
for param in parameters:
self._parameter_info += f"{param.name} (Parameter)\n"
parameter_info[param.name] = (param, "Parameter")

input_parameters = self._find_symbols(pybamm.InputParameter)
for input_param in input_parameters:
if input_param.domain == []:
self._parameter_info += f"{input_param.name} (InputParameter)\n"
if not input_param.domain:
parameter_info[input_param.name] = (input_param, "InputParameter")
else:
self._parameter_info += (
f"{input_param.name} (InputParameter in {input_param.domain})\n"
)
parameter_info[input_param.name] = (input_param, f"InputParameter in {input_param.domain}")

function_parameters = self._find_symbols(pybamm.FunctionParameter)
for func_param in function_parameters:
# don't double count function parameters
if func_param.name not in self._parameter_info:
input_names = "'" + "', '".join(func_param.input_names) + "'"
self._parameter_info += (
f"{func_param.name} (FunctionParameter "
f"with input(s) {input_names})\n"
)
if func_param.name not in parameter_info:
input_names = "', '".join(func_param.input_names)
parameter_info[func_param.name] = (func_param, f"FunctionParameter with inputs(s) '{input_names}'")

print(self._parameter_info)
return parameter_info

def print_parameter_info(self):
cringeyburger marked this conversation as resolved.
Show resolved Hide resolved
"""Print parameter information in a formatted table from the list of tuples"""
cringeyburger marked this conversation as resolved.
Show resolved Hide resolved
info = self.get_parameter_info()
max_param_name_length = 0
max_param_type_length = 0

for param, param_type in info.values():
param_name_length = len(getattr(param, 'name', str(param)))
param_type_length = len(param_type)
max_param_name_length = max(max_param_name_length, param_name_length)
max_param_type_length = max(max_param_type_length, param_type_length)

header_format = f"| {{:<{max_param_name_length}}} | {{:<{max_param_type_length}}} |"
row_format = f"| {{:<{max_param_name_length}}} | {{:<{max_param_type_length}}} |"

table = [header_format.format("Parameter", "Type of parameter"),
header_format.format("=" * max_param_name_length, "=" * max_param_type_length)]

for param, param_type in info.values():
param_name = getattr(param, 'name', str(param))
param_name_lines = [param_name[i:i + max_param_name_length] for i in range(0, len(param_name), max_param_name_length)]
param_type_lines = [param_type[i:i + max_param_type_length] for i in range(0, len(param_type), max_param_type_length)]
max_lines = max(len(param_name_lines), len(param_type_lines))

for i in range(max_lines):
param_line = param_name_lines[i] if i < len(param_name_lines) else ""
type_line = param_type_lines[i] if i < len(param_type_lines) else ""
table.append(row_format.format(param_line, type_line))

for line in table:
print(line)

def _find_symbols(self, typ):
"""Find all the instances of `typ` in the model"""
Expand Down
Loading