easiest way to include square brackets in a parameter's name

Skip to first unread message

Kima wari

May 29, 2024, 8:29:33 AMMay 29
to lmfit-py
The issue is the following:
from lmfit import Parameters
params = Parameters()
KeyError Traceback (most recent call last)
/tmp/ipykernel_74236/312279628.py in <cell line: 2>() 
    1 params = Parameters()
    ----> 2 params.add(name="binwidth[s]")

/gpfs/exfel/sw/software/mambaforge/22.11/envs/202401/lib/python3.10/site-packages/lmfit/parameter.py in add(self, name, value, vary, min, max, expr, brute_step) 
    432 self.__setitem__(name.name, name) 
    433 else:
    --> 434 self.__setitem__(name, Parameter(value=value, name=name, vary=vary, 
    435 min=min, max=max, expr=expr, 
    436 brute_step=brute_step))

/gpfs/exfel/sw/software/mambaforge/22.11/envs/202401/lib/python3.10/site-packages/lmfit/parameter.py in __setitem__(self, key, par) 
    148 """Set items of Parameters object.""" 
    149 if key not in self and not valid_symbol_name(key):
    --> 150 raise KeyError(f"'{key}' is not a valid Parameters name") 
    151 if par is not None and not isinstance(par, Parameter): 
    152 raise ValueError(f"'{par}' is not a Parameter")

KeyError: "'binwidth[s]' is not a valid Parameters name"

As the `Parameters` class uses the asteval.Interpreter module by default, I was wondering how one can add square brackets to a parameter name, as it can be useful to put units on the name.


Matt Newville

May 29, 2024, 12:08:33 PMMay 29
to lmfi...@googlegroups.com
KeyError: "'binwidth[s]' is not a valid Parameters name"

That is the correct message:  "binwidth[s]" is not a valid Parameter name. 

Parameter names must match the requirements for Python variable names (without `.` or `[](){}` as those are really operations on the names).   It would be easy to state that as

but many unicode symbols (say, greek letters) are also supported, as with Python.  And Python keywords are not allowed.
Basically: "valid Python symbol".

You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/bddd7018-3841-4f79-84f2-38adf9d92322n%40googlegroups.com.

--Matt Newville <newville at cars.uchicago.edu630-327-7411
Reply all
Reply to author
0 new messages