1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """The R1 and R2 exponential relaxation curve fitting parameter functions."""
24
25
26 from numpy import array, float64, identity, zeros
27 from re import search
28
29
30 from lib.mathematics import round_to_next_order
31
32
34 """Assemble the exponential curve parameter vector (as a numpy array).
35
36 @keyword spin: The spin data container.
37 @type spin: SpinContainer instance
38 @keyword sim_index: The optional MC simulation index.
39 @type sim_index: int
40 @return: An array of the parameter values of the exponential model.
41 @rtype: numpy array
42 """
43
44
45 param_vector = []
46
47
48 for i in range(len(spin.params)):
49
50 if spin.params[i] == 'rx':
51 if sim_index != None:
52 param_vector.append(spin.rx_sim[sim_index])
53 elif spin.rx == None:
54 param_vector.append(0.0)
55 else:
56 param_vector.append(spin.rx)
57
58
59 elif spin.params[i] == 'i0':
60 if sim_index != None:
61 param_vector.append(spin.i0_sim[sim_index])
62 elif spin.i0 == None:
63 param_vector.append(0.0)
64 else:
65 param_vector.append(spin.i0)
66
67
68 elif spin.params[i] == 'iinf':
69 if sim_index != None:
70 param_vector.append(spin.iinf_sim[sim_index])
71 elif spin.iinf == None:
72 param_vector.append(0.0)
73 else:
74 param_vector.append(spin.iinf)
75
76
77 return array(param_vector, float64)
78
79
81 """Create and return the scaling matrix.
82
83 @keyword spin: The spin data container.
84 @type spin: SpinContainer instance
85 @keyword scaling: A flag which if false will cause the identity matrix to be returned.
86 @type scaling: bool
87 @return: The diagonal and square scaling matrix.
88 @rtype: numpy diagonal matrix
89 """
90
91
92 scaling_matrix = identity(len(spin.params), float64)
93 i = 0
94
95
96 if not scaling:
97 return scaling_matrix
98
99
100 for i in range(len(spin.params)):
101
102 if spin.params[i] == 'rx':
103 pass
104
105
106 elif search('^i', spin.params[i]):
107 scaling_matrix[i, i] = round_to_next_order(max(spin.peak_intensity.values()))
108
109
110 return scaling_matrix
111
112
114 """Disassemble the parameter vector.
115
116 @keyword param_vector: The parameter vector.
117 @type param_vector: numpy array
118 @keyword spin: The spin data container.
119 @type spin: SpinContainer instance
120 @keyword sim_index: The optional MC simulation index.
121 @type sim_index: int
122 """
123
124
125 if sim_index != None:
126
127 spin.rx_sim[sim_index] = param_vector[0]
128
129
130 spin.i0_sim[sim_index] = param_vector[1]
131
132
133 if cdp.curve_type == 'inv':
134 spin.iinf_sim[sim_index] = param_vector[2]
135
136
137 else:
138
139 spin.rx = param_vector[0]
140
141
142 spin.i0 = param_vector[1]
143
144
145 if cdp.curve_type == 'inv':
146 spin.iinf = param_vector[2]
147
148
150 """Set up the relaxation curve fitting linear constraint matrices A and b.
151
152 Standard notation
153 =================
154
155 The relaxation rate constraints are::
156
157 Rx >= 0
158
159 The intensity constraints are::
160
161 I0 >= 0
162 Iinf >= 0
163
164
165 Matrix notation
166 ===============
167
168 In the notation A.x >= b, where A is an matrix of coefficients, x is an array of parameter values, and b is a vector of scalars, these inequality constraints are::
169
170 | 1 0 0 | | Rx | | 0 |
171 | | | | | |
172 | 1 0 0 | . | I0 | >= | 0 |
173 | | | | | |
174 | 1 0 0 | | Iinf | | 0 |
175
176
177 @keyword spin: The spin data container.
178 @type spin: SpinContainer instance
179 @keyword scaling_matrix: The diagonal, square scaling matrix.
180 @type scaling_matrix: numpy diagonal matrix
181 """
182
183
184 A = []
185 b = []
186 n = len(spin.params)
187 zero_array = zeros(n, float64)
188 i = 0
189 j = 0
190
191
192 for k in range(len(spin.params)):
193
194 if spin.params[k] == 'rx':
195
196 A.append(zero_array * 0.0)
197 A[j][i] = 1.0
198 b.append(0.0)
199 j = j + 1
200
201
202 elif search('^i', spin.params[k]):
203
204 A.append(zero_array * 0.0)
205 A[j][i] = 1.0
206 b.append(0.0)
207 j = j + 1
208
209
210 i = i + 1
211
212
213 A = array(A, float64)
214 b = array(b, float64)
215
216 return A, b
217