or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builtin-models.mdconfidence.mdindex.mdminimization.mdmodels.mdparameters.mdreporting.md

parameters.mddocs/

0

# Parameters and Constraints

1

2

LMFIT replaces plain float values with Parameter objects that can be varied, fixed, bounded, or constrained by mathematical expressions. The Parameters class provides a dictionary-like container for managing collections of Parameter objects with constraint evaluation.

3

4

## Capabilities

5

6

### Parameter Class

7

8

Individual parameter objects that form the foundation of all fitting operations in LMFIT.

9

10

```python { .api }

11

class Parameter:

12

"""Individual parameter that can be varied in fits"""

13

14

def __init__(self, name=None, value=None, vary=True, min=-inf, max=inf,

15

expr=None, brute_step=None, user_data=None):

16

"""

17

Create a Parameter.

18

19

Args:

20

name (str): Parameter name

21

value (float): Parameter value

22

vary (bool): Whether parameter should be varied in fit

23

min (float): Lower bound for parameter value

24

max (float): Upper bound for parameter value

25

expr (str): Mathematical expression to constrain parameter

26

brute_step (float): Step size for brute force method

27

user_data: User-defined data associated with parameter

28

"""

29

30

def set(self, value=None, vary=None, min=None, max=None, expr=None,

31

brute_step=None):

32

"""

33

Set parameter attributes.

34

35

Args:

36

value (float): Parameter value

37

vary (bool): Whether to vary in fit

38

min (float): Lower bound

39

max (float): Upper bound

40

expr (str): Constraint expression

41

brute_step (float): Brute force step size

42

"""

43

```

44

45

**Key Attributes (set after fitting):**

46

- `stderr`: Standard error in parameter value

47

- `correl`: Dictionary of correlation coefficients with other parameters

48

49

### Parameters Class

50

51

Dictionary-like container for Parameter objects with constraint evaluation capabilities.

52

53

```python { .api }

54

class Parameters(dict):

55

"""Dictionary of Parameter objects with constraint evaluation"""

56

57

def __init__(self, usersyms=None):

58

"""

59

Create Parameters container.

60

61

Args:

62

usersyms (dict): Additional symbols for expression evaluation

63

"""

64

65

def add(self, name, value=None, vary=None, min=-inf, max=inf,

66

expr=None, brute_step=None):

67

"""

68

Add a parameter.

69

70

Args:

71

name (str): Parameter name (must be valid Python identifier)

72

value (float): Initial value

73

vary (bool): Whether to vary in fit (default is True)

74

min (float): Lower bound

75

max (float): Upper bound

76

expr (str): Constraint expression using other parameter names

77

brute_step (float): Step size for brute force optimization

78

"""

79

80

def add_many(self, *parlist):

81

"""

82

Add multiple parameters from tuples.

83

84

Args:

85

*parlist: Tuples of (name, value, vary, min, max, expr, brute_step)

86

"""

87

88

def valuesdict(self):

89

"""

90

Get parameter values as dictionary.

91

92

Returns:

93

dict: Parameter names and their current values

94

"""

95

96

def pretty_print(self, oneline=False, colwidth=8, precision=4, fmt='g',

97

columns=['value', 'min', 'max', 'stderr', 'vary', 'expr', 'brute_step']):

98

"""

99

Print formatted parameter table.

100

101

Args:

102

oneline (bool): Print in single line format

103

colwidth (int): Column width for display

104

precision (int): Number precision for display

105

fmt (str): Number format string

106

columns (list): Columns to display

107

"""

108

109

def dumps(self, sort_keys=True, **kws):

110

"""

111

Serialize parameters to JSON string.

112

113

Args:

114

sort_keys (bool): Sort parameter names

115

**kws: Additional arguments for json.dumps

116

117

Returns:

118

str: JSON string representation

119

"""

120

121

def loads(self, s, **kws):

122

"""

123

Load parameters from JSON string.

124

125

Args:

126

s (str): JSON string

127

**kws: Additional arguments for json.loads

128

"""

129

130

def dump(self, fp, **kws):

131

"""

132

Serialize parameters to file.

133

134

Args:

135

fp: File-like object to write to

136

**kws: Additional arguments for json.dump

137

"""

138

139

def load(self, fp, **kws):

140

"""

141

Load parameters from file.

142

143

Args:

144

fp: File-like object to read from

145

**kws: Additional arguments for json.load

146

"""

147

148

def update_constraints(self):

149

"""Update all parameter constraint expressions"""

150

151

def create_uvars(self):

152

"""

153

Create uncertainties variables for error propagation.

154

155

Returns:

156

dict: Parameter names mapped to uncertainties.ufloat objects

157

"""

158

```

159

160

### Helper Functions

161

162

```python { .api }

163

def create_params(**kws):

164

"""

165

Create Parameters instance with initial values.

166

167

Args:

168

**kws: Parameter names and initial values

169

170

Returns:

171

Parameters: Container with initialized parameters

172

173

Example:

174

params = create_params(x=1.0, y=2.0, z=3.0)

175

"""

176

```

177

178

## Usage Examples

179

180

### Basic Parameter Creation

181

182

```python

183

from lmfit import Parameters, Parameter

184

185

# Create individual parameters

186

param1 = Parameter('amplitude', value=10, min=0, max=100)

187

param2 = Parameter('center', value=5, vary=False) # fixed parameter

188

189

# Create Parameters container

190

params = Parameters()

191

params.add('amp', value=10, min=0)

192

params.add('cen', value=5)

193

params.add('wid', value=1, min=0.01, max=10)

194

```

195

196

### Parameter Constraints

197

198

```python

199

# Constrained parameters using expressions

200

params = Parameters()

201

params.add('a', value=10, min=0)

202

params.add('b', value=5, min=0)

203

params.add('c', expr='2*a + sqrt(b)') # c depends on a and b

204

params.add('d', expr='a - b', min=0) # constrained difference

205

206

# Update constraint expressions

207

params.update_constraints()

208

```

209

210

### Bulk Parameter Addition

211

212

```python

213

# Add multiple parameters at once

214

params = Parameters()

215

params.add_many(

216

('amp1', 10, True, 0, 50), # name, value, vary, min, max

217

('cen1', 5, True, 0, 10),

218

('wid1', 1, True, 0.01, 5),

219

('amp2', 8, True, 0, 50),

220

('cen2', 7, True, 0, 10),

221

('wid2', 1.5, True, 0.01, 5)

222

)

223

```

224

225

### Serialization

226

227

```python

228

# Save parameters to JSON

229

json_str = params.dumps()

230

231

# Load parameters from JSON

232

new_params = Parameters()

233

new_params.loads(json_str)

234

235

# Save/load to/from file

236

with open('params.json', 'w') as f:

237

params.dump(f)

238

239

with open('params.json', 'r') as f:

240

params.load(f)

241

```