r""" Fitting of multiple spectra using ADMM method. Parameters ---------- data : ndarray(float), 2D array holding multiple observed spectra, shape (K, N), where K is the number of energy points, and N is the number of spectra absorption_refs : ndarray(float), 2
(data, ref_spectra, *, rate=0.2, maxiter=100, epsilon=1e-30, non_negative=True)
| 294 | |
| 295 | |
| 296 | def _fitting_admm(data, ref_spectra, *, rate=0.2, maxiter=100, epsilon=1e-30, non_negative=True): |
| 297 | r""" |
| 298 | Fitting of multiple spectra using ADMM method. |
| 299 | |
| 300 | Parameters |
| 301 | ---------- |
| 302 | |
| 303 | data : ndarray(float), 2D |
| 304 | array holding multiple observed spectra, shape (K, N), where K is the number of energy points, |
| 305 | and N is the number of spectra |
| 306 | |
| 307 | absorption_refs : ndarray(float), 2D |
| 308 | array of references, shape (K, Q), where Q is the number of references. |
| 309 | |
| 310 | maxiter : int |
| 311 | maximum number of iterations. Optimization may stop prematurely if convergence criteria are met. |
| 312 | |
| 313 | rate : float |
| 314 | descent rate for optimization algorithm. Currently is used only for ADMM fitting (1/lambda). |
| 315 | |
| 316 | epsilon : float |
| 317 | small value used in stopping criterion of ADMM optimization algorithm. |
| 318 | |
| 319 | non_negative : bool |
| 320 | if True, then the solution is guaranteed to be non-negative |
| 321 | |
| 322 | Returns |
| 323 | ------- |
| 324 | |
| 325 | map_data_fitted : ndarray(float), 2D |
| 326 | fitting results, shape (Q, N), where Q is the number of references and N is the number of spectra. |
| 327 | |
| 328 | map_rfactor : ndarray(float), 2D |
| 329 | map that represents R-factor for the fitting, shape (M,N). |
| 330 | |
| 331 | convergence : ndarray(float), 1D |
| 332 | convergence data returned by ADMM algorithm |
| 333 | |
| 334 | feasibility : ndarray(float), 1D |
| 335 | feasibility data returned by ADMM algorithm |
| 336 | |
| 337 | The prototype for the ADMM fitting function was implemented by Hanfei Yan in Matlab. |
| 338 | """ |
| 339 | assert data.ndim == 2, "Data array 'data' must have 2 dimensions" |
| 340 | assert ref_spectra.ndim == 2, "Data array 'ref_spectra' must have 2 dimensions" |
| 341 | |
| 342 | n_pts = data.shape[0] |
| 343 | n_pixels = data.shape[1] |
| 344 | n_pts_2 = ref_spectra.shape[0] |
| 345 | n_refs = ref_spectra.shape[1] |
| 346 | |
| 347 | assert ( |
| 348 | n_pts == n_pts_2 |
| 349 | ), f"ADMM fitting: number of spectrum points in data ({n_pts}) and references ({n_pts_2}) do not match." |
| 350 | |
| 351 | assert rate > 0.0, f"ADMM fitting: parameter 'rate' is zero or negative ({rate:.6g})" |
| 352 | |
| 353 | assert maxiter > 0, f"ADMM fitting: parameter 'maxiter' is zero or negative ({rate})" |