forked from diffpy/diffpy.srmise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeakextraction.py
1340 lines (1135 loc) · 49.4 KB
/
peakextraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
##############################################################################
#
# SrMise by Luke Granlund
# (c) 2014 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Luke Granlund
#
# See LICENSE.txt for license information.
#
##############################################################################
import logging
import os.path
import re
import sys
import matplotlib.pyplot as plt
import numpy as np
from diffpy.srmise.baselines import Baseline
from diffpy.srmise.dataclusters import DataClusters
from diffpy.srmise.modelcluster import ModelCluster, ModelCovariance
from diffpy.srmise.modelparts import ModelPart, ModelParts
from diffpy.srmise.peaks import Peak, Peaks
from diffpy.srmise.srmiseerrors import *
logger = logging.getLogger("diffpy.srmise")
from diffpy.srmise import srmiselog
class PeakExtraction(object):
"""Class for peak extraction.
Data members
x: x coordinates of the data
y: y coordinates of the data
dx: uncertainties in the x coordinates (not used)
dy: uncertainties in the y coordinates
effective_dy: uncertainties in the y coordinates actually used during extraction
rng: [xmin, xmax] Range of x coordinates over which to extract peaks
pf: Sequence of peak functions that can be extracted
initial_peaks: Peaks present at start of extraction
baseline: Baseline for data
cres: Resolution of clustering
error_method: ErrorEvaluator class used to compare models
Calculated members
extracted: ModelCluster after extraction
extraction_type: Type of extraction
"""
def __init__(self, newvars=[]):
"""Initialize PeakExtraction object.
Parameters
newvars: Sequence of strings that represent additional extraction parameters."""
self.clear()
self.extractvars = dict.fromkeys(
(
"effective_dy",
"rng",
"pf",
"initial_peaks",
"baseline",
"cres",
"error_method",
)
)
for k in newvars:
if k not in self.extractvars:
self.extractvars[k] = None
else:
emsg = "Extraction variable %s conflicts with existing variable" % k
raise ValueError(emsg)
return
def clear(self):
"""Clear all members."""
self.x = None
self.y = None
self.dx = None
self.dy = None
self.effective_dy = None
self.cres = None
self.pf = None
self.baseline = None
self.error_method = None
self.initial_peaks = None
self.rng = None
self.clearcalc()
def clearcalc(self):
"""Clear all calculated members."""
self.extracted = None
self.extraction_type = None
def setdata(self, x, y, dx=None, dy=None):
if len(x) != len(y):
emsg = "Sequences x and y must have the same length."
raise ValueError(emsg)
self.x = np.array(x)
self.y = np.array(y)
if dx is None:
self.dx = np.zeros(len(x))
else:
self.dx = np.array(dx)
if dy is None:
self.dy = np.zeros(len(x))
else:
self.dy = np.array(dy)
if len(self.x) != len(self.dx) or len(self.x) != len(self.dy):
emsg = "Sequences dx and dy (if present) must have the same length as x"
raise ValueError(emsg)
# self.defaultvars()
return
def setvars(self, quiet=False, **kwds):
"""Set one or more extraction variables.
Variables
quiet: [False] Log changes quietly.
Keywords
cres: The clustering resolution, must be > 0.
effective_dy: The uncertainties actually used during extraction
pf: Sequence of PeakFunctionBase subclass instances.
baseline: Baseline instance or BaselineFunction instance (use built-in estimation)
error_method: ErrorEvaluator subclass instance used to compare models (default AIC)
initial_peaks: Peaks instance. These peaks are present at the start of extraction.
rng: Sequence specifying the least and greatest x-values over which to extract peaks.
"""
for k, v in kwds.iteritems():
if k in self.extractvars:
if quiet:
logger.debug("Setting variable %s=%s", k, v)
else:
logger.info("Setting variable %s=%s", k, v)
setattr(self, k, v)
else:
emsg = "Invalid extraction variable: %s=%s" % (k, v)
raise ValueError(emsg)
self.defaultvars()
def defaultvars(self, *args):
"""Set unset(=None) extraction variables to default values.
Certain variables may be partially set for convenience, and are transformed
appropriately. See 'Default values' below.
Parameters
Any number of strings corresponding to extraction variables. These
variables are reset to their default values even if another value
already exists.
Default values:
cres -> 4 times the average spacing between elements in x
effective_dy -> The data dy if all elements > 0, otherwise 5% of max(y)-min(y).
If effective_dy is a positive scalar, then an array of that
value of appropriate length.
pf -> [GaussianOverR(maxwidth=x[-1]-x[0])]
baseline -> Flat baseline located at y=0.
error_method -> AIC (Aikake Information Criterion)
initial_peaks -> No initial peaks
rng -> [x[0], x[-1]]. Partially set ranges like [None, 100.] replace None with
the appropriate limit in the data.
Note that the default values of very important parameters like the uncertainty
and clustering resolution are crude guesses at best.
"""
if self.cres is None or "cres" in args:
self.cres = 4 * (self.x[-1] - self.x[0]) / len(self.x)
if self.effective_dy is None or "effective_dy" in args:
if np.all(self.dy > 0):
# That is, all points positive uncertainty.
self.effective_dy = self.dy
else:
# A terribly crude guess
self.effective_dy = 0.05 * (np.max(self.y) - np.min(self.y)) * np.ones(len(self.x))
elif np.isscalar(self.effective_dy) and self.effective_dy > 0:
self.effective_dy = self.effective_dy * np.ones(len(self.x))
if self.pf is None or "pf" in args:
from diffpy.srmise.peaks import GaussianOverR
# TODO: Make a more useful default.
self.pf = [GaussianOverR(self.x[-1] - self.x[0])]
if self.rng is None or "rng" in args:
self.rng = [self.x[0], self.x[-1]]
else:
if self.rng[0] is None:
self.rng[0] = self.x[0]
if self.rng[1] is None:
self.rng[1] = self.x[-1]
# Set baseline where the type is given, but parameters must be estimated.
if hasattr(self.baseline, "estimate_parameters"):
try:
s = self.getrangeslice()
epars = self.baseline.estimate_parameters(self.x[s], self.y[s])
self.baseline = self.baseline.actualize(epars, "internal")
logger.info("Estimating baseline: %s" % self.baseline)
except (NotImplementedError, SrMiseEstimationError):
logger.error("Could not estimate baseline from provided BaselineFunction, trying default values.")
self.baseline = None
if self.baseline is None or "baseline" in args:
from diffpy.srmise.baselines import Polynomial
bl = Polynomial(degree=-1)
self.baseline = bl.actualize(np.array([]), "internal")
if self.error_method is None or "error_method" in args:
from diffpy.srmise.modelevaluators import AIC
self.error_method = AIC
if self.initial_peaks is None or "initial_peaks" in args:
self.initial_peaks = Peaks()
def __str__(self):
"""Return string summary of PeakExtraction."""
out = []
for k in self.extractvars:
out.append("%s: %s" % (k, getattr(self, k)))
if self.extracted is not None:
out.append("Extraction type: %s" % self.extraction_type)
out.append("--- Extracted ---")
out.append(str(self.extracted))
else:
out.append("No extracted peaks exist.")
return "\n".join(out) + "\n"
def plot(self, **kwds):
"""Convenience function to plot data and extracted peaks with matplotlib.
Uses initial peaks instead if no peaks have been extracted.
Takes same keywords as ModelCluster.plottable()"""
plt.clf()
if self.extracted is not None:
plt.plot(*self.extracted.plottable(kwds))
else:
# Make sure all required extraction variables have some value
self.defaultvars()
rangeslice = self.getrangeslice()
x = self.x[rangeslice]
y = self.y[rangeslice]
dy = self.dy[rangeslice]
mcluster = ModelCluster(
self.initial_peaks,
self.baseline,
x,
y,
dy,
None,
self.error_method,
self.pf,
)
plt.plot(*mcluster.plottable(kwds))
def read(self, filename):
"""load PeakExtraction object from file
filename -- file from which to read
returns self
"""
try:
self.readstr(open(filename, "rb").read())
except SrMiseDataFormatError as err:
logger.exception("")
basename = os.path.basename(filename)
emsg = ("Could not open '%s' due to unsupported file format " + "or corrupted data. [%s]") % (
basename,
err,
)
raise SrMiseFileError(emsg)
return self
def readstr(self, datastring):
"""Initialize members from string.
Parameters
datastring: The raw data to read"""
from diffpy.srmise.basefunction import BaseFunction
self.clear()
# The major components are:
# - Header
# - BaselineFunctions
# - PeakFunctions
# - BaselineObject
# - InitialPeaks
# - SrMiseMetaData
# - MetaData
# - StartData
# - Results
# Lists holding BaseFunctions as they are instantiated
safepf = []
safebf = []
# find where the results section starts
res = re.search(r"^#+ Results\s*(?:#.*\s+)*", datastring, re.M)
if res:
results = datastring[res.end() :].strip()
header = datastring[: res.start()]
# find data section, and what information it contains
res = re.search(r"^#+ start data\s*(?:#.*\s+)*", header, re.M)
if res:
start_data = header[res.end() :].strip()
start_data_info = header[res.start() : res.end()]
header = header[: res.start()]
res = re.search(r"^(#+L.*)$", start_data_info, re.M)
if res:
start_data_info = start_data_info[res.start() : res.end()].strip()
hasx = False
hasy = False
hasdx = False
hasdy = False
hasedy = False
res = re.search(r"\bx\b", start_data_info)
if res:
hasx = True
res = re.search(r"\by\b", start_data_info)
if res:
hasy = True
res = re.search(r"\bdx\b", start_data_info)
if res:
hasdx = True
res = re.search(r"\bdy\b", start_data_info)
if res:
hasdy = True
res = re.search(r"\edy\b", start_data_info)
if res:
hasedy = True
res = re.search(r"^#+ Metadata\s*(?:#.*\s+)*", header, re.M)
if res:
metadata = header[res.end() :].strip()
header = header[: res.start()]
res = re.search(r"^#+ SrMiseMetadata\s*(?:#.*\s+)*", header, re.M)
if res:
srmisemetadata = header[res.end() :].strip()
header = header[: res.start()]
res = re.search(r"^#+ InitialPeaks.*$", header, re.M)
if res:
initial_peaks = header[res.end() :].strip()
header = header[: res.start()]
res = re.search(r"^#+ BaselineObject\s*(?:#.*\s+)*", header, re.M)
if res:
baselineobject = header[res.end() :].strip()
header = header[: res.start()]
res = re.search(r"^#+ PeakFunctions.*$", header, re.M)
if res:
peakfunctions = header[res.end() :].strip()
header = header[: res.start()]
res = re.search(r"^#+ BaselineFunctions.*$", header, re.M)
if res:
baselinefunctions = header[res.end() :].strip()
header = header[: res.start()]
# Instantiating baseline functions
res = re.split(r"(?m)^#+ BaselineFunction \d+\s*(?:#.*\s+)*", baselinefunctions)
for s in res[1:]:
safebf.append(BaseFunction.factory(s, safebf))
# Instantiating peak functions
res = re.split(r"(?m)^#+ PeakFunction \d+\s*(?:#.*\s+)*", peakfunctions)
for s in res[1:]:
safepf.append(BaseFunction.factory(s, safepf))
# Instantiating Baseline object
if re.match(r"^None$", baselineobject):
self.baseline = None
elif re.match(r"^\d+$", baselineobject):
self.baseline = safebf[int(baselineobject)]
else:
self.baseline = Baseline.factory(baselineobject, safebf)
# Instantiating initial peaks
if re.match(r"^None$", initial_peaks):
self.initial_peaks = None
else:
self.initial_peaks = Peaks()
res = re.split(r"(?m)^#+ InitialPeak\s*(?:#.*\s+)*", initial_peaks)
for s in res[1:]:
self.initial_peaks.append(Peak.factory(s, safepf))
# Instantiating srmise metatdata
# pf
res = re.search(r"^pf=(.*)$", srmisemetadata, re.M)
self.pf = eval(res.groups()[0].strip())
if self.pf is not None:
self.pf = [safepf[i] for i in self.pf]
# cres
rx = {"f": r"[-+]?(\d+(\.\d*)?|\d*\.\d+)([eE][-+]?\d+)?"}
regexp = r"\bcres *= *(%(f)s)\b" % rx
res = re.search(regexp, srmisemetadata, re.I)
self.cres = float(res.groups()[0])
# error_method
res = re.search(r"^ModelEvaluator=(.*)$", srmisemetadata, re.M)
__import__("diffpy.srmise.modelevaluators")
module = sys.modules["diffpy.srmise.modelevaluators"]
self.error_method = getattr(module, res.groups()[0].strip())
# range
res = re.search(r"^Range=(.*)$", srmisemetadata, re.M)
self.rng = eval(res.groups()[0].strip())
# Instantiating other metadata
self.readmetadata(metadata)
# Instantiating start data
# read actual data - x, y, dx, dy, plus effective_dy
arrays = []
if hasx:
self.x = []
arrays.append(self.x)
else:
self.x = None
if hasy:
self.y = []
arrays.append(self.y)
else:
self.y = None
if hasdx:
self.dx = []
arrays.append(self.dx)
else:
self.dx = None
if hasdy:
self.dy = []
arrays.append(self.dy)
else:
self.dy = None
if hasedy:
self.effective_dy = []
arrays.append(self.effective_dy)
else:
self.effective_dy = None
# raise SrMiseDataFormatError if something goes wrong
try:
for line in start_data.split("\n"):
l = line.split()
if len(arrays) != len(l):
emsg = "Number of value fields does not match that given by '%s'" % start_data_info
for a, v in zip(arrays, line.split()):
a.append(float(v))
except (ValueError, IndexError) as err:
raise SrMiseDataFormatError(str(err))
if hasx:
self.x = np.array(self.x)
if hasy:
self.y = np.array(self.y)
if hasdx:
self.dx = np.array(self.dx)
if hasdy:
self.dy = np.array(self.dy)
if hasedy:
self.effective_dy = np.array(self.effective_dy)
# Instantiating results
res = re.search(r"^#+ ModelCluster\s*(?:#.*\s+)*", results, re.M)
if res:
mc = results[res.end() :].strip()
results = results[: res.start()]
# extraction type
res = re.search(r"^extraction_type=(.*)$", results, re.M)
if res:
self.extraction_type = eval(res.groups()[0].strip())
else:
emsg = "Required field 'extraction_type' not found."
raise SrMiseDataFormatError(emsg)
# extracted
if re.match(r"^None$", mc):
self.extracted = None
else:
self.extracted = ModelCluster.factory(mc, pfbaselist=safepf, blfbaselist=safebf)
def write(self, filename):
"""Write string representation of PeakExtraction instance to file.
Parameters
filename: the name of the file to write"""
bytes = self.writestr()
f = open(filename, "w")
f.write(bytes)
f.close()
return
def writestr(self):
"""Return string representation of PeakExtraction object."""
import time
from getpass import getuser
from diffpy.srmise import __version__
from diffpy.srmise.basefunction import BaseFunction
lines = []
# Header
lines.extend(
[
"History written: " + time.ctime(),
"produced by " + getuser(),
"diffpy.srmise version %s" % __version__,
"##### PDF Peak Extraction",
]
)
# Generate list of PeakFunctions and BaselineFunctions
# so I can refer to them by index when necessary.
allpf = []
allbf = []
if self.pf is not None:
allpf.extend(self.pf)
if self.initial_peaks is not None:
allpf.extend([i.owner() for i in self.initial_peaks])
if self.baseline is not None:
if isinstance(self.baseline, BaseFunction):
allbf.append(self.baseline)
else: # should be a ModelPart
allbf.append(self.baseline.owner())
if self.extracted is not None:
allpf.extend(self.extracted.peak_funcs)
allpf.extend([p.owner() for p in self.extracted.model])
if self.extracted.baseline is not None:
allbf.append(self.extracted.baseline.owner())
allpf = list(set(allpf))
allbf = list(set(allbf))
safepf = BaseFunction.safefunctionlist(allpf)
safebf = BaseFunction.safefunctionlist(allbf)
# Indexed baseline functions
lines.append("## BaselineFunctions")
for i, bf in enumerate(safebf):
lines.append("# BaselineFunction %s" % i)
lines.append(bf.writestr(safebf))
# Indexed peak functions
lines.append("## PeakFunctions")
for i, pf in enumerate(safepf):
lines.append("# PeakFunction %s" % i)
lines.append(pf.writestr(safepf))
# Baseline
lines.append("# BaselineObject")
if self.baseline is None:
lines.append("None")
elif self.baseline in safebf:
lines.append("%s" % repr(safebf.index(self.baseline)))
else:
lines.append(self.baseline.writestr(safebf))
# Initial peaks
lines.append("## InitialPeaks")
if self.initial_peaks is None:
lines.append("None")
else:
for ip in self.initial_peaks:
lines.append("# InitialPeak")
lines.append(ip.writestr(safepf))
lines.append("# SrMiseMetadata")
# Extractable peak types
if self.pf is None:
lines.append("pf=None")
else:
lines.append("pf=%s" % repr([safepf.index(p) for p in self.pf]))
# Clustering resolution
lines.append("cres=%g" % self.cres)
# Model evaluator
if self.error_method is None:
lines.append("ModelEvaluator=None")
else:
lines.append("ModelEvaluator=%s" % self.error_method.__name__)
# Extraction range
lines.append("Range=%s" % repr(self.rng))
# Everything not defined by PeakExtraction
lines.append("# Metadata")
lines.append(self.writemetadata())
# Raw data used in extraction.
lines.append("##### start data")
line = ["#L"]
numlines = 0
if self.x is not None:
line.append("x")
numlines = len(self.x)
if self.y is not None:
line.append("y")
numlines = len(self.y)
if self.dx is not None:
line.append("dx")
numlines = len(self.dx)
if self.dy is not None:
line.append("dy")
numlines = len(self.dy)
if self.effective_dy is not None:
line.append("edy")
numlines = len(self.effective_dy)
lines.append(" ".join(line))
for i in range(numlines):
line = []
if self.x is not None:
line.append("%g" % self.x[i])
if self.y is not None:
line.append("%g" % self.y[i])
if self.dx is not None:
line.append("%g" % self.dx[i])
if self.dy is not None:
line.append("%g" % self.dy[i])
if self.effective_dy is not None:
line.append("%g" % self.effective_dy[i])
lines.append(" ".join(line))
# Calculated members
lines.append("##### Results")
lines.append("extraction_type=%s" % repr(self.extraction_type))
lines.append("### ModelCluster")
if self.extracted is None:
lines.append("None")
else:
lines.append(self.extracted.writestr(pfbaselist=safepf, blfbaselist=safebf))
datastring = "\n".join(lines) + "\n"
return datastring
def writemetadata(self):
"""Return string for metadata not defined by srmise class."""
return
def readmetadata(self):
"""Return string for metadata not defined by srmise class."""
return
def writesummary(self):
"""Return summary of peak extraction results."""
pass
def getrangeslice(self):
"""Convert the ranges in terms of x-coordinates to a slice object."""
low_idx = 0
while self.x[low_idx] < max(self.x[0], self.rng[0]):
low_idx += 1
hi_idx = len(self.x)
while self.x[hi_idx - 1] > min(self.x[-1], self.rng[1]):
hi_idx -= 1
return slice(low_idx, hi_idx)
def estimate_peak(self, x, add=True):
"""Return new estimated peak near x.
Peaks already extracted, if any, are taken into account. If none exist,
use those specified by initial_peaks instead.
Parameters:
x: Coordinate of the point of interest
add: (True) Automatically add peak to extracted peaks or initial_peaks.
Return a Peak object, or None if estimation fails.
"""
# Make sure all required extraction variables have some value
self.defaultvars()
if self.extracted is not None:
# Determine clusters using existing peaks and baseline in extracted
x1 = self.extracted.r_cluster
y1 = self.extracted.y_cluster - self.extracted.value()
dy = self.extracted.error_cluster
else:
# Determine clusters using initial_peaks and pre-defined or estimated baseline
rangeslice = self.getrangeslice()
x1 = self.x[rangeslice]
y1 = self.y[rangeslice] - self.baseline.value(x1) - self.initial_peaks.value(x1)
dy = self.effective_dy[rangeslice]
if x < x1[0] or x > x1[-1]:
emsg = "Argument x=%s outside allowed range (%s, %s)." % (x, x1[0], x1[-1])
raise ValueError(emsg)
# Object performing clustering on data. Note that DataClusters
# provides an iterator that clusters the next point and returns
# itself. Thus, dclusters and step (below) refer to the same object.
dclusters = DataClusters(x1, y1, self.cres) # Cluster with baseline removed
dclusters.makeclusters()
cidx = dclusters.find_nearest_cluster2(x)[0]
cslice = dclusters.cut(cidx)
x1 = x1[cslice]
y1 = y1[cslice]
dy = dy[cslice]
mcluster = ModelCluster(None, None, x1, y1, dy, None, self.error_method, self.pf)
mcluster.fit()
if len(mcluster.model) > 0:
if add:
logger.info("Adding peak: %s" % mcluster.model[0])
self.add_peaks(mcluster.model)
else:
logger.info("Found peak: %s" % mcluster.model[0])
return mcluster.model[0]
else:
logger.info("No peaks found.")
return None
def add_peaks(self, peaks):
"""Add peaks to extracted peaks, or initial_peaks if no extracted peaks exist.
Parameters
peaks: A Peaks instance"""
if self.extracted is not None:
self.extracted.replacepeaks(peaks)
else:
if self.initial_peaks is None:
self.setvars("initial_peaks")
self.initial_peaks.extend(peaks)
self.initial_peaks.sort(key="position")
def extract_single(self, recursion_depth=1):
"""Find ModelCluster with peaks extracted from data. Return ModelCovariance instance at top level.
Every extracted peak is one of the peak functions supplied. All
comparisons of different peak models are performed with the class
specified by error_method.
Parameters
recursion_depth: (1) Tracks recursion with extract_single."""
self.clearcalc()
tracer = srmiselog.tracer
tracer.pushc()
tracer.pushr()
# Make sure all required extraction variables have some value
self.defaultvars()
bl = self.baseline
# Copy initial_peaks
# While it would be nice to integrate them into extracted model naturally
# as it progresses, this is fraught with difficulties. Thus, they will
# only be added back in before the final prune.
ip = self.initial_peaks.copy()
rangeslice = self.getrangeslice()
x = self.x[rangeslice]
y = self.y[rangeslice] - bl.value(x) - ip.value(x)
dy = self.effective_dy[rangeslice]
# Object performing clustering on data. Note that DataClusters
# provides an iterator that clusters the next point and returns
# itself. Thus, dclusters and step (below) refer to the same object.
dclusters = DataClusters(x, y, self.cres) # Cluster with baseline removed
# The data for model clusters includes the baseline
y = self.y[rangeslice] - ip.value(x)
# List of ModelClusters containing extracted peaks.
mclusters = [ModelCluster(None, bl, x, y, dy, dclusters.cut(0), self.error_method, self.pf)]
# The minimum number of points required to make a valid fit, as
# determined by the peak functions and error method used. This is a
# conservative estimate.
minpoints = max([self.error_method().minpoints(p.npars) for p in self.pf])
stepcounter = 0
# #########################
# Main extraction loop ###
for step in dclusters:
stepcounter += 1
msg = "\n\n------ Recursion: %s Step: %s Cluster: %s %s ------"
logger.debug(
msg,
recursion_depth,
stepcounter,
step.lastcluster_idx,
step.clusters[step.lastcluster_idx],
)
# Update mclusters
if len(step.clusters) > len(mclusters):
# Add a new cluster
mclusters.insert(
step.lastcluster_idx,
ModelCluster(
None,
bl,
x,
y,
dy,
step.cut(step.lastcluster_idx),
self.error_method,
self.pf,
),
)
else:
# Update an existing cluster
mclusters[step.lastcluster_idx].change_slice(step.cut(step.lastcluster_idx))
# Find newly adjacent clusters
adjacent = step.find_adjacent_clusters().ravel()
# Various assertions in case terrible things are afoot.
# These could save some gray hairs if they are needed.
# ------
# dclusters and mclusters should have consistent lengths
assert len(step.clusters) == len(mclusters)
# Clusters are always combined after becoming adjacent, so at most
# three clusters can become adjacent at any given step.
assert len(adjacent) <= 3
# Update cluster fits ###
# 1. Refit clusters adjacent to at least one other cluster.
for a in adjacent:
mclusters[a].fit(justify=True)
# 2. If necessary, update the fit of the cluster which has just
# had one or more points added. This occurs if the function
# has not been fit before but now contains enough data points
# to make a good estimate or if the size of the cluster has
# increased enough (e.g. doubled in size) since it was last
# fit.
mclusters[step.lastcluster_idx].contingent_fit(minpoints, 2.0)
# 3. Boundary recursion. If a cluster fills to the boundary of
# the data it should be recursively fit as though it were
# combining with an empty cluster at the boundary. This should
# reveal hidden peaks that might otherwise be improperly fit
# with just a single peak function.
#
# Note: If I later implement intra-cluster fitting, this
# section may become redundant...or the basis for doing it
# properly. Two if statements are required, in case the fit
# results in all peaks blowing up and being removed.
#
# Note: The operation here is very similar to combining
# clusters and recursing. Attempt to be be consistent with
# that section. The primary difference is no need to create an
# enlarged cluster ("new_cluster") or an intermediate cluster
# ("adj_cluster").
if step.lastpoint_idx == 0 or step.lastpoint_idx == len(step.x) - 1:
logger.debug("Boundary full: %s", step.lastpoint_idx)
full_cluster = ModelCluster(mclusters[step.lastcluster_idx])
full_cluster.fit(True)
# Estimate coordinate where clusters combine.
border_x = x[step.lastcluster_idx]
border_y = y[step.lastcluster_idx]
# Determine neighborhood appropriate for fitting (no larger than combined clusters)
if len(full_cluster.model) > 0:
peak_pos = np.array([p["position"] for p in full_cluster.model])
pivot = peak_pos.searchsorted(border_x)
else:
peak_pos = np.array([])
pivot = 0
# near_peaks: array containing the indices of two nearest peaks on either side of border_x
# other_peaks: all the other peaks in full_cluster
# left_data, right_data: indices defining the extent of the "interpeak range" for x, etc.
near_peaks = np.array([], dtype=np.int)
# interpeak range goes from peak to peak of next nearest peaks, although their contributions to the data are still removed.
if pivot == 0:
# No peaks left of border_x!
left_data = full_cluster.slice.indices(len(x))[0]
elif pivot == 1:
# One peak left
left_data = full_cluster.slice.indices(len(x))[0]
near_peaks = np.append(near_peaks, pivot - 1)
else:
# left_data -> one more peak to the left
left_data = max(0, x.searchsorted(peak_pos[pivot - 2]) - 1)
near_peaks = np.append(near_peaks, pivot - 1)
if pivot == len(peak_pos):
# No peaks right of border_x!
right_data = full_cluster.slice.indices(len(x))[1] - 1
elif pivot == len(peak_pos) - 1:
# One peak right
right_data = full_cluster.slice.indices(len(x))[1] - 1
near_peaks = np.append(near_peaks, pivot)
else:
# right_data -> one more peak to the right
right_data = min(len(x), x.searchsorted(peak_pos[pivot + 1]) + 1)
near_peaks = np.append(near_peaks, pivot)
other_peaks = np.concatenate([np.arange(0, pivot - 1), np.arange(pivot + 1, len(peak_pos))])
# Go from indices to lists of peaks.
near_peaks = Peaks([full_cluster.model[i] for i in near_peaks])
other_peaks = Peaks([full_cluster.model[i] for i in other_peaks])
# Remove contribution of peaks outside neighborhood
# Define range of fitting/recursion to the interpeak range
# The adjusted error is passed unchanged. This may introduce
# a few more peaks than is justified, but they can be pruned
# with the correct statistics at the top level of recursion.
adj_slice = slice(left_data, right_data + 1)
adj_x = x[adj_slice]
adj_y = y[adj_slice] - other_peaks.value(adj_x)
adj_error = dy[adj_slice]
adj_cluster = ModelCluster(
near_peaks,
bl,
adj_x,
adj_y,
adj_error,
slice(len(adj_x)),
self.error_method,
self.pf,
)
# Recursively cluster/fit the residual
rec_r = adj_x
rec_y = adj_y - near_peaks.value(rec_r)
rec_error = adj_error
# Quick check to see if there is anything to find
min_npars = min([p.npars for p in self.pf])
checkrec = ModelCluster(
None,
None,
rec_r,
rec_y,
rec_error,
None,
self.error_method,
self.pf,
)
recurse = len(near_peaks) > 0 and checkrec.quality().growth_justified(checkrec, min_npars)
if recurse and recursion_depth < 3:
logger.info(
"\n*********STARTING RECURSION level %s (full boundary)************"
% (recursion_depth + 1)
)
rec_search = PeakExtraction()
rec_search.setdata(rec_r, rec_y, None, rec_error)
rec_search.setvars(
quiet=True,
baseline=bl,
cres=self.cres,
pf=self.pf,
error_method=self.error_method,
)
rec_search.extract_single(recursion_depth + 1)
rec = rec_search.extracted
logger.info(
"*********ENDING RECURSION level %s (full boundary) ************\n" % (recursion_depth + 1)
)
# Incorporate best peaks from recursive search.
adj_cluster.augment(rec)
# Select which model to use
full_cluster.model = other_peaks
full_cluster.replacepeaks(adj_cluster.model)
full_cluster.fit(True)
msg = [
"---Result of full boundary---",
"Original cluster:",
"%s",
"Final cluster:",
"%s",
"---End of combining clusters---",
]