Skip to content

Better interface for setParameters#326

Closed
SengerM wants to merge 1 commit into
OpenModelica:masterfrom
SengerM:setParameters-pythonic
Closed

Better interface for setParameters#326
SengerM wants to merge 1 commit into
OpenModelica:masterfrom
SengerM:setParameters-pythonic

Conversation

@SengerM

@SengerM SengerM commented Aug 1, 2025

Copy link
Copy Markdown

Purpose

setParameters(Name=value)
setParameters(Name1=value1, Name2=value2)

are more clean and natural than

setParameters("Name=value")
setParameters(["Name1=value1","Name2=value2"])

It should be backwards compatible, meaning that it still accepts the strings.

@CLAassistant

CLAassistant commented Aug 1, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@syntron

syntron commented Aug 3, 2025

Copy link
Copy Markdown
Contributor

@SengerM would this work on top of PR #314? it creates the option to use dictionaries for all set*() functions ...

@SengerM

SengerM commented Aug 4, 2025

Copy link
Copy Markdown
Author

@syntron I think PR #314 is an improvement with respect to the current interface, but the interface from this PR is superior and I would suggest to implement it like this in all the set methods. It is the most natural way.

For quick comparison:

model.setParameters(['Name1=Value1', 'Name2=Value2', ...]) # Current interface (cumbersome, error prone, have to deal with conversion to strings, etc).
model.SetParameters(dict(Name1=Value1, Name2=Value2, ...)) # PR #314 (boilerplate `dict()`, other than that it is acceptable).
model.SetParameters(Name1=Value1, Name2=Value2, ...) # This PR (as good as it gets).

I would also only keep the current interface with strings for backwards compatibility, and add a warning that it will be deprecated in the future.

@syntron

syntron commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

I see the dict based interface as preferable if lots of options are handled which are defined before as dictionary. My idea is to add an additional function which just translates the argument key based variant to the dict based version.

I would also only keep the current interface with strings for backwards compatibility, and add a warning that it will be deprecated in the future.

This is done in PR #314; I also prepared a commit which would remove the depreciated / old way of setting the options.

@syntron

syntron commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

Please see commit 6cd65ea of PR #314 ...

@SengerM

SengerM commented Aug 5, 2025

Copy link
Copy Markdown
Author

I see the dict based interface as preferable if lots of options are handled which are defined before as dictionary.

The "normal Python interface" from this PR can naturally do that (if I understood correctly what you mean):

parameters = {f'Name{i}': i**2 for i in range(999)} # 999 different parameters.
model.setParameters(**parameters)

My advice is to keep it simple and to offer the user an interface he would expect in Python. Less maintenance and more user friendly.

This is also the way it is done in many other packages, e.g. matplotlyb.pyplot.plot, which is defined as matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs) instead of matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, kwargs). This allows you to do plot(x, y, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12) instead of plot(x, y, dict(color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12)), and you can of course do

plot_settings = dict(color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12)
plot(x, y, **plot_settings)

@syntron

syntron commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

@SengerM I learned something and will check this out - thanks for the explanation!

syntron added a commit to syntron/OMPython that referenced this pull request Aug 15, 2025
* use a Pythonic way for input:
  setParameters(a=123)

  param = {'a': 123}
  setParameters(**param)

see input by SengerM in PR OpenModelica#326
syntron added a commit to syntron/OMPython that referenced this pull request Aug 15, 2025
* use a Pythonic way for input:
  setParameters(a=123)

  param = {'a': 123}
  setParameters(**param)

see input by SengerM in PR OpenModelica#326
@syntron

syntron commented Aug 15, 2025

Copy link
Copy Markdown
Contributor

@SengerM new version as PR #345

syntron added a commit to syntron/OMPython that referenced this pull request Aug 23, 2025
* use a Pythonic way for input:
  setParameters(a=123)

  param = {'a': 123}
  setParameters(**param)

see input by SengerM in PR OpenModelica#326
syntron added a commit to syntron/OMPython that referenced this pull request Oct 15, 2025
* use a Pythonic way for input:
  setParameters(a=123)

  param = {'a': 123}
  setParameters(**param)

see input by SengerM in PR OpenModelica#326
adeas31 added a commit that referenced this pull request Nov 3, 2025
* [ModelicaSystem] update input handling for set*() functions

* use a Pythonic way for input:
  setParameters(a=123)

  param = {'a': 123}
  setParameters(**param)

see input by SengerM in PR #326

* [test_optimization] update due to changes in set*() functions

* [test_ModelicaSystem] update due to changes in set*() functions

* [test_linearization] update due to changes in set*() functions

* [ModelicaSystem] consider dict input for set*() functions

---------

Co-authored-by: Adeel Asghar <adeel.asghar@liu.se>
@adeas31

adeas31 commented Nov 3, 2025

Copy link
Copy Markdown
Member

Closing this as #345 is merged now.

@adeas31 adeas31 closed this Nov 3, 2025
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.

4 participants