DiadFit 0.0.81__py3-none-any.whl → 0.0.84__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
DiadFit/ne_lines.py CHANGED
@@ -19,6 +19,8 @@ import scipy.stats as stats
19
19
  import pickle
20
20
 
21
21
 
22
+ allowed_models = ["VoigtModel", "PseudoVoigtModel", "Pearson4Model", "SkewedVoigtModel"]
23
+
22
24
 
23
25
 
24
26
  encode="ISO-8859-1"
@@ -73,12 +75,16 @@ def calculate_Ne_splitting(wavelength=532.05, line1_shift=1117, line2_shift=1447
73
75
 
74
76
  closest1=find_closest(df_Ne, line1_shift).loc['Raman_shift (cm-1)']
75
77
  closest2=find_closest(df_Ne, line2_shift).loc['Raman_shift (cm-1)']
78
+ closest_1_int=find_closest(df_Ne, line1_shift).loc['Intensity']
79
+ closest_2_int=find_closest(df_Ne, line2_shift).loc['Intensity']
76
80
 
77
81
  diff=abs(closest1-closest2)
78
82
 
79
83
  df=pd.DataFrame(data={'Ne_Split': diff,
80
84
  'Line_1': closest1,
81
85
  'Line_2': closest2,
86
+ 'Line_1_int': closest_1_int,
87
+ 'Line_2_int': closest_2_int,
82
88
  'Entered Pos Line 1': line1_shift,
83
89
  'Entered Pos Line 2': line2_shift}, index=[0])
84
90
 
@@ -109,88 +115,229 @@ def calculate_Ne_line_positions(wavelength=532.05, cut_off_intensity=2000):
109
115
  Ne_emission_line_air=np.array([
110
116
 
111
117
 
112
- 541.85584,
113
- 542.009,
114
- 542.0155,
115
- 543.36513,
116
- 544.7120,
117
-
118
- 544.85091,
119
- 549.44158,
120
- 550.73442,
121
- 551.1176,
122
- 551.1485,
123
-
124
- 552.063,
125
- 553.36788,
126
- 553.86510,
127
- 555.90978,
128
- 556.24416,
129
-
130
- 556.27662,
131
- 556.30531,
132
-
133
-
134
-
135
- 556.244160,
136
- 556.276620,
137
- 556.305310,
138
- 557.603940,
139
- 558.590500,
140
- 558.934720,
141
- 559.115000,
142
- 565.256640,
143
- 565.602580,
144
- 565.665880,
145
- 566.220000,
146
- 566.254890,
147
- 568.464700,
148
- 568.981630,
149
- 571.534090,
150
- 571.887980,
151
- 571.922480,
152
- 571.953000,
153
- 574.243700,
154
- 574.829850,
155
- 574.864460,
156
- 576.058850,
157
- 576.405250,
158
- 576.441880,
159
- 577.030670,
160
- 580.409000,
161
- 580.444960,
162
- 581.140660,
163
- 581.662190,
164
- 582.015580,
165
- 582.890630
166
-
167
-
168
-
118
+ 541.85584,
119
+ 542.009,
120
+ 542.0155,
121
+ 543.36513,
122
+ 544.7120,
123
+
124
+ 544.85091,
125
+ 549.44158,
126
+ 550.73442,
127
+ 551.1176,
128
+ 551.1485,
129
+
130
+ 552.063,
131
+ 553.36788,
132
+ 553.86510,
133
+ 555.90978,
134
+ 556.24416,
135
+
136
+ 556.27662,
137
+ 556.30531,
138
+
139
+
140
+
141
+ 556.244160,
142
+ 556.276620,
143
+ 556.305310,
144
+ 557.603940,
145
+ 558.590500,
146
+ 558.934720,
147
+ 559.115000,
148
+ 565.256640,
149
+ 565.602580,
150
+ 565.665880,
151
+ 566.220000,
152
+ 566.254890,
153
+ 568.464700,
154
+ 568.981630,
155
+ 571.534090,
156
+ 571.887980,
157
+ 571.922480,
158
+ 571.953000,
159
+ 574.243700,
160
+ 574.829850,
161
+ 574.864460,
162
+ 576.058850,
163
+ 576.405250,
164
+ 576.441880,
165
+ 577.030670,
166
+ 580.409000,
167
+ 580.444960,
168
+ 581.140660,
169
+ 581.662190,
170
+ 582.015580,
171
+ 582.890630,
172
+ 585.24878,
173
+ 586.84165,
174
+ 587.2145,
175
+ 587.28275,
176
+ 588.1895,
177
+ 589.83287,
178
+ 590.20944,
179
+ 590.24623,
180
+ 590.27835,
181
+ 590.64294,
182
+ 591.3633,
183
+ 591.89068,
184
+ 591.9029,
185
+ 593.44522,
186
+ 593.93154,
187
+ 594.4834,
188
+ 596.16228,
189
+ 596.5471,
190
+ 596.6179,
191
+ 597.46273,
192
+ 597.55343,
193
+ 598.23753,
194
+ 598.79074,
195
+ 599.16477,
196
+ 600.09275,
197
+ 602.99968,
198
+ 604.2013,
199
+ 604.61348,
200
+ 606.45359,
201
+ 607.43376,
202
+ 609.6163,
203
+ 611.088,
204
+ 611.272,
205
+ 611.80187,
206
+ 612.84498,
207
+ 613.277,
208
+ 614.2508,
209
+ 614.30627,
210
+ 615.02985,
211
+ 615.6138,
212
+ 616.35937,
213
+ 616.609,
214
+ 617.28156,
215
+ 617.48829,
216
+ 617.52842,
217
+ 618.2146,
218
+ 618.31575,
219
+ 618.90649,
220
+ 619.30663,
221
+ 620.2974,
222
+ 620.57775,
223
+ 620.908,
224
+ 621.018,
225
+ 621.38758,
226
+ 621.72812,
227
+ 622.448,
228
+ 622.5735,
229
+ 623.9032,
230
+ 624.67294,
231
+ 624.9593,
232
+ 625.0925,
233
+ 625.2732,
234
+ 625.2905,
235
+ 625.869,
236
+ 625.87884,
237
+ 626.64952,
238
+ 627.0131,
239
+ 627.242,
240
+ 627.30141,
241
+ 627.60327,
242
+ 627.75,
243
+ 628.358,
244
+ 628.959,
245
+ 629.37447,
246
+ 629.784,
247
+ 630.47893,
248
+ 630.6113,
249
+ 630.766,
250
+ 631.36855,
251
+ 632.1116,
252
+ 632.81646,
253
+ 633.08894,
254
+ 633.44276,
255
+ 634.5653,
256
+ 635.18532,
257
+ 636.49963,
258
+ 638.29914,
259
+ 640.1076,
260
+ 640.2248,
261
+ 640.658,
262
+ 640.97469,
263
+ 642.17044,
264
+ 644.47118,
265
+ 650.65277,
266
+ 651.0877,
267
+ 651.1789,
268
+ 651.6789,
269
+ 651.7041,
270
+ 652.5584,
271
+ 652.603,
272
+ 653.28824,
273
+ 653.8685,
274
+ 653.9143,
275
+ 653.9492,
276
+ 653.9692,
277
+ 655.0653,
278
+ 655.0653,
279
+ 655.9024,
280
+ 655.9285,
281
+ 657.13,
282
+ 657.17,
283
+ 659.89528,
284
+ 660.29007,
285
+ 664.00095,
286
+ 664.08,
287
+ 665.20925,
288
+ 666.6892,
289
+ 667.82766,
290
+ 671.7043,
291
+ 672.11342,
292
+ 673.8032,
293
+ 675.95821,
294
+ 688.694,
295
+ 689.541,
296
+ 692.94672,
297
+ 702.405,
298
+ 703.24128,
299
+ 705.12922,
300
+ 705.91079,
301
+ 706.4762,
302
+ 706.7724,
303
+ 711.23075,
304
+ 713.336,
305
+ 713.854,
306
+ 717.3938,
307
+ 721.321,
308
+ 723.51978,
309
+ 724.51665
310
+
311
+
312
+
313
+
314
+
315
+
169
316
  ])
170
-
317
+
171
318
  Intensity=np.array([
172
319
 
173
- 1500,
174
- 12 ,
175
- 500,
176
- 2500,
177
- 80,
178
-
179
- 1500,
180
- 500,
181
- 250,
182
- 30,
183
- 150,
184
-
185
- 30,
186
- 750,
187
- 500,
188
- 350,
189
- 1500,
190
-
191
- 5000,
192
- 750,
193
-
320
+ 1500,
321
+ 12,
322
+ 500,
323
+ 2500,
324
+ 80,
325
+
326
+ 1500,
327
+ 500,
328
+ 250,
329
+ 30,
330
+ 150,
331
+
332
+ 30,
333
+ 750,
334
+ 500,
335
+ 350,
336
+ 1500,
337
+
338
+ 5000,
339
+ 750,
340
+
194
341
 
195
342
 
196
343
 
@@ -225,7 +372,149 @@ def calculate_Ne_line_positions(wavelength=532.05, cut_off_intensity=2000):
225
372
  3000.00,
226
373
  500.00,
227
374
  5000.00,
228
- 750.00])
375
+ 750.00,
376
+
377
+ 20000,
378
+ 750,
379
+ 750,
380
+ 5000,
381
+ 10000,
382
+ 200,
383
+ 30,
384
+ 500,
385
+ 50,
386
+ 500,
387
+ 2500,
388
+ 2500,
389
+ 80,
390
+ 750,
391
+ 500,
392
+ 5000,
393
+ 700,
394
+ 5000,
395
+ 350,
396
+ 5000,
397
+ 6000,
398
+ 80,
399
+ 1500,
400
+ 750,
401
+ 1000,
402
+ 10000,
403
+ 150,
404
+ 500,
405
+ 500,
406
+ 10000,
407
+ 3000,
408
+ 100,
409
+ 160,
410
+ 150,
411
+ 1000,
412
+ 100,
413
+ 1000,
414
+ 10000,
415
+ 1000,
416
+ 500,
417
+ 10000,
418
+ 80,
419
+ 150,
420
+ 700,
421
+ 500,
422
+ 1500,
423
+ 50,
424
+ 700,
425
+ 500,
426
+ 150,
427
+ 1000,
428
+ 70,
429
+ 70,
430
+ 1500,
431
+ 10000,
432
+ 160,
433
+ 500,
434
+ 120,
435
+ 1000,
436
+ 50,
437
+ 120,
438
+ 20,
439
+ 80,
440
+ 180,
441
+ 1000,
442
+ 10000,
443
+ 80,
444
+ 140,
445
+ 700,
446
+ 500,
447
+ 30,
448
+ 30,
449
+ 30,
450
+ 1000,
451
+ 80,
452
+ 1000,
453
+ 100,
454
+ 180,
455
+ 1500,
456
+ 60,
457
+ 3000,
458
+ 1500,
459
+ 10000,
460
+ 60,
461
+ 1000,
462
+ 1000,
463
+ 10000,
464
+ 1000,
465
+ 20000,
466
+ 90,
467
+ 1500,
468
+ 1000,
469
+ 1500,
470
+ 15000,
471
+ 60,
472
+ 140,
473
+ 120,
474
+ 100,
475
+ 22,
476
+ 120,
477
+ 1000,
478
+ 80,
479
+ 120,
480
+ 140,
481
+ 60,
482
+ 140,
483
+ 140,
484
+ 100,
485
+ 100,
486
+ 22,
487
+ 60,
488
+ 10000,
489
+ 1000,
490
+ 100,
491
+ 50,
492
+ 1500,
493
+ 1000,
494
+ 5000,
495
+ 700,
496
+ 20,
497
+ 700,
498
+ 150,
499
+ 70,
500
+ 100,
501
+ 100000,
502
+ 34000,
503
+ 85000,
504
+ 2200,
505
+ 10000,
506
+ 80,
507
+ 80,
508
+ 110,
509
+ 40,
510
+ 55,
511
+ 77000,
512
+ 300,
513
+ 300,
514
+ 77000
515
+
516
+
517
+ ])
229
518
 
230
519
 
231
520
 
@@ -236,7 +525,9 @@ def calculate_Ne_line_positions(wavelength=532.05, cut_off_intensity=2000):
236
525
  'Ne emission line in air': Ne_emission_line_air})
237
526
 
238
527
  df_Ne_r=df_Ne.loc[df_Ne['Intensity']>cut_off_intensity]
239
- return df_Ne_r
528
+ # Lets also only return positive numbers
529
+ df_Ne_p=df_Ne_r.loc[df_Ne_r['Raman_shift (cm-1)']>0]
530
+ return df_Ne_p
240
531
 
241
532
 
242
533
  @dataclass
@@ -500,7 +791,7 @@ Ne_array=None):
500
791
 
501
792
 
502
793
  ## Ne baselines
503
- def remove_Ne_baseline_pk1(Ne, N_poly_pk1_baseline=None, Ne_center_1=None,
794
+ def remove_Ne_baseline_pk(Ne, N_poly_pk1_baseline=None, Ne_center_1=None,
504
795
  lower_bck=None, upper_bck1=None, upper_bck2=None, sigma_baseline=None):
505
796
  """ This function uses a defined range of values to fit a baseline of Nth degree polynomial to the baseline
506
797
  around a specified peak
@@ -582,97 +873,10 @@ lower_bck=None, upper_bck1=None, upper_bck2=None, sigma_baseline=None):
582
873
 
583
874
  return y_corr, Py_base, x, Ne_short, Py_base, Baseline_y, Baseline_x
584
875
 
585
- def remove_Ne_baseline_pk2(Ne, N_poly_pk2_baseline=None, Ne_center_2=None, sigma_baseline=None,
586
- lower_bck=None, upper_bck1=None, upper_bck2=None):
587
-
588
- """ This function uses a defined range of values to fit a baseline of Nth degree polynomial to the baseline
589
- around a second selected peak
590
-
591
- Parameters
592
- -----------
593
-
594
- Ne: np.array
595
- np.array of x and y coordinates from the spectra
596
-
597
- N_poly_pk1_baseline: int
598
- Degree of polynomial used to fit the background
599
-
600
- Ne_center_1: float
601
- Center position for Ne line being fitted
602
-
603
- lower_bck: list (length 2) Default [-44.2, -22]
604
- position used for lower background relative to peak, so =[-50, -20] takes a
605
- background -50 and -20 from the peak center
606
-
607
- upper_bck1: list (length 2). Default [15, 50]
608
- position used for 1st upper background relative to peak, so =[8, 15] takes a
609
- background +8 and +15 from the peak center
610
-
611
- upper_bck2: list (length 2) Default [50, 51]
612
- position used for 2nd upper background relative to peak, so =[30, 50] takes a
613
- background +30 and +50 from the peak center
614
-
615
- Returns
616
- -----------
617
- y_corr, Py_base, x, Ne_short, Py_base, Baseline_y, Baseline_x
618
-
619
- y_corr (numpy.ndarray): The corrected y-values after subtracting the fitted polynomial baseline from the original data.
620
- Py_base (numpy.ndarray): The y-values of the fitted polynomial baseline.
621
- x (numpy.ndarray): The x-values of the trimmed data within the specified range.
622
- Ne_short (numpy.ndarray): The trimmed data within the specified range.
623
- Baseline_y (numpy.ndarray): The y-values of the baseline data points.
624
- Baseline_x (numpy.ndarray): The x-values of the baseline data points.
625
-
626
-
627
- """
628
-
629
-
630
-
631
- lower_0baseline_pk2=Ne_center_2+lower_bck[0]
632
- upper_0baseline_pk2=Ne_center_2+lower_bck[1]
633
- lower_1baseline_pk2=Ne_center_2+upper_bck1[0]
634
- upper_1baseline_pk2=Ne_center_2+upper_bck1[1]
635
- lower_2baseline_pk2=Ne_center_2+upper_bck2[0]
636
- upper_2baseline_pk2=Ne_center_2+upper_bck2[1]
637
-
638
- # Trim for entire range
639
- Ne_short=Ne[ (Ne[:,0]>lower_0baseline_pk2) & (Ne[:,0]<upper_2baseline_pk2) ]
640
-
641
- # Get actual baseline
642
- Baseline_with_outl=Ne_short[
643
- ((Ne_short[:, 0]<upper_0baseline_pk2) &(Ne_short[:, 0]>lower_0baseline_pk2))
644
- |
645
- ((Ne_short[:, 0]<upper_1baseline_pk2) &(Ne_short[:, 0]>lower_1baseline_pk2))
646
- |
647
- ((Ne_short[:, 0]<upper_2baseline_pk2) &(Ne_short[:, 0]>lower_2baseline_pk2))]
648
-
649
- # Calculates the median for the baseline and the standard deviation
650
- Median_Baseline=np.median(Baseline_with_outl[:, 1])
651
- Std_Baseline=np.std(Baseline_with_outl[:, 1])
652
-
653
- # Removes any points in the baseline outside of 2 sigma (helps remove cosmic rays etc).
654
- Baseline=Baseline_with_outl[(Baseline_with_outl[:, 1]<Median_Baseline+sigma_baseline*Std_Baseline)
655
- &
656
- (Baseline_with_outl[:, 1]>Median_Baseline-sigma_baseline*Std_Baseline)
657
- ]
658
-
659
- # Fits a polynomial to the baseline of degree
660
- Pf_baseline = np.poly1d(np.polyfit(Baseline[:, 0], Baseline[:, 1], N_poly_pk2_baseline))
661
- Py_base =Pf_baseline(Ne_short[:, 0])
662
- Baseline_ysub=Pf_baseline(Baseline[:, 0])
663
- Baseline_x=Baseline[:, 0]
664
- Baseline_y=Baseline[:, 1]
665
- y_corr= Ne_short[:, 1]- Py_base
666
- x=Ne_short[:, 0]
667
-
668
-
669
- return y_corr, Py_base, x, Ne_short, Py_base, Baseline_y, Baseline_x
670
-
671
876
 
672
877
 
673
878
 
674
-
675
- def fit_pk1(x, y_corr, x_span=[-10, 8], Ne_center=1117.1, amplitude=98.1, pk1_sigma=0.28,
879
+ def fit_Ne_pk(x, y_corr, x_span=[-10, 8], Ne_center=1117.1, amplitude=98.1, pk1_sigma=0.28,
676
880
  LH_offset_mini=[1.5, 3], peaks_pk1=2, model_name='PseudoVoigtModel', block_print=True,
677
881
  const_params=True, spec_res=0.4) :
678
882
  """ This function fits the 1117 Ne line as 1 or two voigt peaks
@@ -738,16 +942,12 @@ const_params=True, spec_res=0.4) :
738
942
  if peaks_pk1>1:
739
943
 
740
944
  # Setting up lmfit
741
- if model_name == 'PseudoVoigtModel':
742
- model0 = PseudoVoigtModel(prefix='p0_')#+ ConstantModel(prefix='c0')
743
- if model_name=="VoigtModel":
744
- model0 = VoigtModel(prefix='p0_')#+ ConstantModel(prefix='c0')
745
- if model_name=='SkewedVoigtModel':
746
- model0=SkewedVoigtModel(prefix='p0_')
945
+ model0 = globals()[model_name](prefix='p0_')
946
+
747
947
 
748
948
  pars0 = model0.make_params()
749
949
  pars0['p0_center'].set(Ne_center, min=Ne_center-2*spec_res, max=Ne_center+2*spec_res)
750
- pars0['p0_amplitude'].set(amplitude)
950
+ pars0['p0_amplitude'].set(amplitude, min=amplitude*min_off, max=amplitude*max_off)
751
951
 
752
952
 
753
953
 
@@ -763,26 +963,7 @@ const_params=True, spec_res=0.4) :
763
963
  print('first iteration, peak Amplitude='+str(np.round(Amp_p0, 4)))
764
964
  fwhm_p0=result0.params.get('p0_fwhm')
765
965
 
766
-
767
-
768
- pattern = r"\+/-\s*([\d.]+)"
769
- match = re.search(pattern, str(Center_p0_error))
770
- if match:
771
- Center_p0_errorval = float(match.group(1))
772
- else:
773
- Center_p0_errorval=np.nan
774
-
775
-
776
-
777
- #Ne_center=Ne_center
778
- #rough_peak_positions=Ne_center-2
779
- if model_name == 'PseudoVoigtModel':
780
- model1 = PseudoVoigtModel(prefix='p1_')#+ ConstantModel(prefix='c0')
781
- if model_name=="VoigtModel":
782
- model1 = VoigtModel(prefix='p1_')#+ ConstantModel(prefix='c0')
783
-
784
- if model_name=='SkewedVoigtModel':
785
- model1=SkewedVoigtModel(prefix='p1_')
966
+ model1 = globals()[model_name](prefix='p1_')
786
967
 
787
968
  pars1 = model1.make_params()
788
969
  pars1['p1_'+ 'amplitude'].set(Amp_p0, min=min_off*Amp_p0, max=max_off*Amp_p0)
@@ -791,13 +972,7 @@ const_params=True, spec_res=0.4) :
791
972
 
792
973
 
793
974
  # Second wee peak
794
- prefix='p2_'
795
- if model_name == 'PseudoVoigtModel':
796
- peak = PseudoVoigtModel(prefix='p2_')#+ ConstantModel(prefix='c0')
797
- if model_name=="VoigtModel":
798
- peak = VoigtModel(prefix='p2_')#+ ConstantModel(prefix='c0')
799
- if model_name=='SkewedVoigtModel':
800
- peak=SkewedVoigtModel(prefix='p2_')
975
+ peak = globals()[model_name](prefix='p2_')
801
976
 
802
977
 
803
978
  pars = peak.make_params()
@@ -847,28 +1022,13 @@ const_params=True, spec_res=0.4) :
847
1022
 
848
1023
 
849
1024
  if peaks_pk1==1:
850
-
851
- if model_name == 'PseudoVoigtModel':
852
- model_combo = PseudoVoigtModel(prefix='p1_')#+ ConstantModel(prefix='c0')
853
- if model_name=="VoigtModel":
854
- model_combo= VoigtModel(prefix='p1_')#+ ConstantModel(prefix='c0')
855
- if model_name=='SkewedVoigtModel':
856
- model_combo=SkewedVoigtModel(prefix='p1_')
857
-
858
-
859
-
1025
+ model_combo = globals()[model_name](prefix='p1_')
860
1026
  # create parameters with initial values
861
1027
  pars1 = model_combo.make_params()
862
- pars1['p1_amplitude'].set(amplitude)
1028
+ pars1['p1_amplitude'].set(amplitude, min=amplitude*min_off, max=amplitude*max_off)
863
1029
  pars1['p1_' + 'center'].set(Ne_center, min=Ne_center-2*spec_res,max=Ne_center+2*spec_res)
864
1030
  pars1['p1_'+ 'sigma'].set(pk1_sigma, min=pk1_sigma*min_off, max=pk1_sigma*max_off)
865
1031
 
866
-
867
-
868
-
869
-
870
-
871
-
872
1032
 
873
1033
  result = model_combo.fit(ydat, pars1, x=xdat)
874
1034
 
@@ -876,52 +1036,18 @@ const_params=True, spec_res=0.4) :
876
1036
  # Need to check errors output
877
1037
  Error_bars=result.errorbars
878
1038
 
879
-
880
-
881
1039
  # Get center value
882
1040
  Center_p1=result.best_values.get('p1_center')
883
-
884
-
885
-
886
1041
  error_pk1 = result.params['p1_center'].stderr
887
-
888
1042
 
1043
+ Center_pk2_error=result.params.get('p1_center')
889
1044
 
1045
+
890
1046
  # Get mix of lorenz
891
1047
  Peak1_Prop_Lor=result.best_values.get('p1_fraction')
892
1048
 
893
1049
 
894
-
895
-
896
- if peaks_pk1>1:
897
- Center_p2=result.best_values.get('p2_center')
898
- Center_p2_error=result.params.get('p2_center')
899
-
900
-
901
1050
 
902
- # # Check if nonsense, e.g. if center 2 miles away, just use center 0
903
- # if Center_p2 is not None:
904
- # if Center_p2>Center_p0 or Center_p2<1112:
905
- # Center_pk1=Center_p0
906
- # error_pk1=Center_p0_errorval
907
- # if block_print is False:
908
- # print('No meaningful second peak found')
909
- #
910
- # elif Center_p1 is None and Center_p2 is None:
911
- # if block_print is False:
912
- # print('No peaks found')
913
- # elif Center_p1 is None and Center_p2>0:
914
- # Center_pk1=Center_p2
915
- # error_pk1=Center_p2_errorval
916
- # elif Center_p2 is None and Center_p1>0:
917
- # Center_pk1=Center_p1
918
- # error_pk1=Center_p1_errorval
919
- # elif Center_p1>Center_p2:
920
- # Center_pk1=Center_p1
921
- #
922
- # elif Center_p1<Center_p2:
923
- # Center_pk1=Center_p2
924
-
925
1051
 
926
1052
  Area_pk1=result.best_values.get('p1_amplitude')
927
1053
  sigma_pk1=result.best_values.get('p1_sigma')
@@ -943,115 +1069,7 @@ const_params=True, spec_res=0.4) :
943
1069
  return Center_pk1, Area_pk1, sigma_pk1, gamma_pk1, Ne_pk1_reg_x_plot, Ne_pk1_reg_y_plot, Ne_pk1_reg_x, Ne_pk1_reg_y, xx_pk1, result_pk1, error_pk1, result_pk1_origx, comps, Peak1_Prop_Lor
944
1070
 
945
1071
 
946
- def fit_pk2(x, y_corr, x_span=[-5, 5], Ne_center=1447.5, amplitude=1000, pk2_sigma=0.4,
947
- model_name='PseudoVoigtModel', print_report=False, const_params=True, spec_res=0.4) :
948
- """ This function fits the 1447 Ne line as a single Voigt
949
-
950
- Parameters
951
- -----------
952
-
953
- x: np.array
954
- x coordinate (wavenumber)
955
-
956
- y: np.array
957
- Background corrected intensiy
958
-
959
- x_span: list length 2. Default [-5, 5]
960
- Span either side of peak center used for fitting,
961
- e.g. by default, fits to 5 wavenumbers below peak, 5 above.
962
-
963
- Ne_center: float (default=1447.5)
964
- Center position for Ne line being fitted
965
-
966
- amplitude: integer (default = 1000)
967
- peak amplitude
968
-
969
- sigma: float (default =0.28)
970
- sigma of the voigt peak
971
-
972
-
973
- print_report: bool
974
- if True, prints fit report.
975
-
976
-
977
- """
978
- if const_params is True:
979
- min_off=0.8
980
- max_off=1.2
981
- if const_params is False:
982
- min_off=0
983
- max_off=100
984
-
985
-
986
- # This defines the range you want to fit (e.g. how big the tails are)
987
- lower_pk2=Ne_center+x_span[0]
988
- upper_pk2=Ne_center+x_span[1]
989
-
990
- # This segments into the x and y variable, and variables to plot, which are a bit bigger.
991
- Ne_pk2_reg_x=x[(x>lower_pk2)&(x<upper_pk2)]
992
- Ne_pk2_reg_y=y_corr[(x>lower_pk2)&(x<upper_pk2)]
993
- Ne_pk2_reg_x_plot=x[(x>(lower_pk2-3))&(x<(upper_pk2+3))]
994
- Ne_pk2_reg_y_plot=y_corr[(x>(lower_pk2-3))&(x<(upper_pk2+3))]
995
-
996
- if model_name == 'PseudoVoigtModel':
997
- model = PseudoVoigtModel()#+ ConstantModel(prefix='c0')
998
- if model_name=="VoigtModel":
999
- model = VoigtModel()#+ ConstantModel(prefix='c0')
1000
- if model_name=="SkewedVoigtModel":
1001
- model = SkewedVoigtModel()#+ ConstantModel(prefix='c0')
1002
-
1003
-
1004
-
1005
- # create parameters with initial values
1006
- params = model.make_params()
1007
-
1008
- params['center'].set(Ne_center, min=Ne_center+x_span[0], max=Ne_center+x_span[1])
1009
- params['amplitude'].set(amplitude, min=amplitude*min_off, max=amplitude*max_off)
1010
- params['sigma'].set(pk2_sigma, min=pk2_sigma*min_off, max=pk2_sigma*max_off)
1011
-
1012
-
1013
- result = model.fit(Ne_pk2_reg_y.flatten(), params, x=Ne_pk2_reg_x.flatten())
1014
-
1015
- # Get center value
1016
- Center_pk2=result.best_values.get('center')
1017
- Center_pk2_error=result.params.get('center')
1018
-
1019
- Peak2_Prop_Lor=result.best_values.get('fraction')
1020
-
1021
-
1022
-
1023
- #print(result.best_values)
1024
-
1025
- Area_pk2=result.best_values.get('amplitude')
1026
- sigma_pk2=result.best_values.get('sigma')
1027
- gamma_pk2=result.best_values.get('gamma')
1028
- # Have to strip away the rest of the string, as center + error
1029
- # print('debug:')
1030
- # print(Center_pk2_error)
1031
- # Center_pk2_errorval=float(str(Center_pk2_error).split()[4].replace(",", ""))
1032
- # error_pk2=Center_pk2_errorval
1033
-
1034
-
1035
- error_pk2=np.nan
1036
-
1037
- try:
1038
- error_pk2_str = str(Center_pk2_error).split('+/-')[1].split(' bounds')[0].strip()
1039
- error_pk2 = float(error_pk2_str.replace(",", ""))
1040
- except IndexError:
1041
- pass
1042
-
1043
-
1044
-
1045
- # Evaluate the peak at 100 values for pretty plotting
1046
- xx_pk2=np.linspace(lower_pk2, upper_pk2, 2000)
1047
-
1048
- result_pk2=result.eval(x=xx_pk2)
1049
- result_pk2_origx=result.eval(x=Ne_pk2_reg_x)
1050
-
1051
- if print_report is True:
1052
- print(result.fit_report(min_correl=0.5))
1053
1072
 
1054
- return Center_pk2,Area_pk2, sigma_pk2, gamma_pk2, Ne_pk2_reg_x_plot, Ne_pk2_reg_y_plot, Ne_pk2_reg_x, Ne_pk2_reg_y, xx_pk2, result_pk2, error_pk2, result_pk2_origx, Peak2_Prop_Lor
1055
1073
 
1056
1074
  ## Setting default Ne fitting parameters
1057
1075
  @dataclass
@@ -1071,6 +1089,7 @@ class Ne_peak_config:
1071
1089
 
1072
1090
  # Whether you want a secondary peak
1073
1091
  peaks_1: float=2
1092
+ peaks_2: float=1
1074
1093
 
1075
1094
  # SPlitting
1076
1095
  DeltaNe_ideal: float= 330.477634
@@ -1092,8 +1111,12 @@ class Ne_peak_config:
1092
1111
  # Things for plotting the residual
1093
1112
  x_range_residual: float=7 # Shows how many x units to left and right is shown on residual plot
1094
1113
 
1095
- # Things for fitting a secondary peak on 1117
1114
+ # Things for fitting a secondary peak on pk1
1096
1115
  LH_offset_mini: Tuple[float, float] = (1.5, 3)
1116
+ # Same for Pk2
1117
+ LH_offset_mini2: Tuple[float, float] = None
1118
+
1119
+
1097
1120
 
1098
1121
  # Optional, by default, fits to the points inside the baseline. Can also specify as values to make a smaller peak fit.
1099
1122
  x_span_pk1: Optional [Tuple[float, float]] = None # Tuple[float, float] = (-10, 8)
@@ -1124,77 +1147,86 @@ plot_figure=True, loop=True,
1124
1147
 
1125
1148
  filename and path: str
1126
1149
  used to save filename in datatable, and to make a new folder.
1150
+
1151
+ prefix: bool
1152
+ Whether or not the filename has a prefix
1153
+
1154
+ Ne_center_1, Ne_center_2 : float
1155
+ Approximate peak position of the 1st and 2nd Ne line
1127
1156
 
1128
- filetype: str
1129
- choose from 'Witec_ASCII', 'headless_txt', 'headless_csv', 'head_csv', 'Witec_ASCII',
1130
- 'HORIBA_txt', 'Renishaw_txt'
1131
-
1132
- amplitude: int or float
1133
- first guess of peak amplitude
1134
-
1157
+ Ne_prom_1, Ne_prom_2 : float
1158
+ Approximate prominance of the 1st and 2nd Ne line
1159
+
1135
1160
  plot_figure: bool
1136
- if True, saves figure of fit in a new folder
1137
-
1138
- Loop: bool
1139
- If True, only returns df.
1140
-
1141
- x_range_baseline: flt, int
1142
- How much x range outside selected baseline the baseline selection plot shows.
1143
-
1144
- y_range_baseline: flt, int
1145
- How much above the baseline position is shown on the y axis.
1146
-
1147
- x_range_peak: flt, int, or None
1148
- How much to either side of the peak to show on the final peak fitting plot
1149
-
1150
-
1151
- DeltaNe_ideal: float
1152
- Theoretical distance between the two peaks you have selected. Default is 330.477634 for
1153
- the 1117 and 1447 Neon for the Cornell Raman. You can calculate this using the calculate_Ne_line_positions
1154
-
1155
-
1156
- Things for Neon 1 (~1117):
1157
-
1158
- N_poly_pk1_baseline: int
1159
- Degree of polynomial used to fit the background
1160
-
1161
- Ne_center_1: float
1162
- Center position for Ne line being fitted
1163
-
1164
- lower_bck_1, upper_bck1, upper_bck1: 3 lists of length 2:
1165
- Positions used for background relative to peak.[-50, -20] takes a
1166
- background -50 and -20 from the peak center
1167
-
1168
- x_span_pk1: list length 2. Default [-10, 8]
1169
- Span either side of peak center used for fitting,
1170
- e.g. by default, fits to 10 wavenumbers below peak, 8 above.
1171
-
1172
-
1173
- peaks_1: int
1174
- How many peaks to fit to the 1117 Neon, if 2, tries to put a shoulder peak
1175
-
1176
- LH_offset_mini: list
1177
- If peaks>1, puts second peak within this range left of the main peak
1178
-
1179
-
1180
-
1181
-
1182
- Things for Neon 2 (~1447):
1183
- N_poly_pk2_baseline: int
1184
- Degree of polynomial used to fit the background
1185
-
1186
- Ne_center_2: float
1187
- Center position for Ne line being fitted
1188
-
1189
- lower_bck_2, upper_bck2, upper_bck2: 3 lists of length 2:
1190
- Positions used for background relative to peak.[-50, -20] takes a
1191
- background -50 and -20 from the peak center
1161
+ Plots a figure, nice to inspect fits, makes it slower
1162
+
1163
+ loop: bool
1164
+
1165
+ save_clipboard: bool
1166
+ Saves results to clipboard if true
1167
+
1168
+ close_figure: bool
1169
+ Closes figure if True (useful in some editors)
1170
+
1171
+ const_params: bool
1172
+ If true, means amplitude and peak sigma have to be closer to the guessed parameters (e.g. used after initial fit).
1173
+ E.g. forced within +-20% of estimated peak parameters if True, if false, +-100%.
1174
+
1192
1175
 
1193
- x_span_pk2: list length 2. Default [-10, 8]
1194
- Span either side of peak center used for fitting,
1195
- e.g. by default, fits to 10 wavenumbers below peak, 8 above.
1176
+ config parameters from Ne_peak_config():
1177
+
1178
+ model_name: str
1179
+ allowed_models = "VoigtModel", "PseudoVoigtModel", "Pearson4Model", "SkewedVoigtModel"
1180
+
1181
+ N_poly_pk1_baseline, N_poly_pk1_baseline: int (default 1)
1182
+ Degree of polynomial to fit to baseline around pk
1196
1183
 
1184
+ lower_bck_pk1, upper_bck1_pk1, upper_bck2_pk1: tuple
1185
+ lower_bck_pk2, upper_bck1_pk2, upper_bck2_pk2: tuple
1186
+ Background positions relative to estimated peak center.
1187
+
1188
+ peaks_1, peaks_2: int
1189
+ Number of peaks to fit to peak 1. If you need 2 overlapping peaks, put this Ne line as pk1
1190
+ if not 1, you also need LH_offset_mini=(1.5, 3). Means second peak put between 1.5 and 3 units left of the 1st peak.
1191
+
1192
+ DeltaNe_ideal: float
1193
+ Ideal distance between two lines calculated for your laser wavelength.
1194
+
1195
+ x_range_baseline_pk1, x_range_baseline_pk2: int, float
1196
+ test
1197
+
1198
+ y_range_baseline_pk1, y_range_baseline_pk2: int, float
1199
+ test
1200
+
1201
+ pk1_sigma, pk2_sigma: float (Default 0.4)
1202
+ Estimated sigma of each peak
1203
+
1204
+ x_range_peak: flt, int, or None
1205
+ How much to either side of the peak to show on the final peak fitting plot
1206
+
1207
+ x_range_residual: flt
1208
+ How much either side of the peak is used for calculating residual
1209
+
1210
+ x_span_pk1, x_span_pk2: float, default None
1211
+ By default, function fits up to background, but can shrink that range here.
1212
+
1213
+ Returns
1214
+ ------------
1215
+ if loop is False:
1216
+ return df, Ne_pk1_reg_x_plot, Ne_pk1_reg_y_plot
1217
+ if loop is True:
1218
+ return df
1219
+
1220
+ Also returns figure.
1221
+
1197
1222
  """
1223
+ # Check model is supported
1224
+ if config.model_name not in allowed_models:
1225
+ raise ValueError(f"Unsupported model: {config.model_name}. Supported models are: {', '.join(allowed_models)}")
1226
+
1227
+
1228
+
1229
+
1198
1230
 
1199
1231
  # check they havent messed up background
1200
1232
  if config.lower_bck_pk1[0]>config.lower_bck_pk1[1]:
@@ -1217,6 +1249,7 @@ plot_figure=True, loop=True,
1217
1249
  spec_res=np.abs(x[1]-x[0])
1218
1250
  # Getting things from config file
1219
1251
  peaks_1=config.peaks_1
1252
+ peaks_2=config.peaks_2
1220
1253
  DeltaNe_ideal=config.DeltaNe_ideal
1221
1254
 
1222
1255
  # Estimate amplitude from prominence and sigma you entered
@@ -1225,11 +1258,11 @@ plot_figure=True, loop=True,
1225
1258
 
1226
1259
 
1227
1260
  #Remove the baselines
1228
- y_corr_pk1, Py_base_pk1, x_pk1, Ne_short_pk1, Py_base_pk1, Baseline_ysub_pk1, Baseline_x_pk1=remove_Ne_baseline_pk1(Ne,
1261
+ y_corr_pk1, Py_base_pk1, x_pk1, Ne_short_pk1, Py_base_pk1, Baseline_ysub_pk1, Baseline_x_pk1=remove_Ne_baseline_pk(Ne,
1229
1262
  N_poly_pk1_baseline=config.N_poly_pk1_baseline, Ne_center_1=Ne_center_1, sigma_baseline=config.sigma_baseline,
1230
1263
  lower_bck=config.lower_bck_pk1, upper_bck1=config.upper_bck1_pk1, upper_bck2=config.upper_bck2_pk1)
1231
1264
 
1232
- y_corr_pk2, Py_base_pk2, x_pk2, Ne_short_pk2, Py_base_pk2, Baseline_ysub_pk2, Baseline_x_pk2=remove_Ne_baseline_pk2(Ne, Ne_center_2=Ne_center_2, N_poly_pk2_baseline=config.N_poly_pk2_baseline, sigma_baseline=config.sigma_baseline,
1265
+ y_corr_pk2, Py_base_pk2, x_pk2, Ne_short_pk2, Py_base_pk2, Baseline_ysub_pk2, Baseline_x_pk2=remove_Ne_baseline_pk(Ne, Ne_center_1=Ne_center_2, N_poly_pk1_baseline=config.N_poly_pk2_baseline, sigma_baseline=config.sigma_baseline,
1233
1266
  lower_bck=config.lower_bck_pk2, upper_bck1=config.upper_bck1_pk2, upper_bck2=config.upper_bck2_pk2)
1234
1267
 
1235
1268
 
@@ -1249,14 +1282,12 @@ plot_figure=True, loop=True,
1249
1282
  x_span_pk2=config.x_span_pk2
1250
1283
  x_span_pk2_dist=abs(config.x_span_pk2[1]-config.x_span_pk2[0])
1251
1284
 
1252
- # Fit the 1117 peak
1253
- cent_pk1, Area_pk1, sigma_pk1, gamma_pk1, Ne_pk1_reg_x_plot, Ne_pk1_reg_y_plot, Ne_pk1_reg_x, Ne_pk1_reg_y, xx_pk1, result_pk1, error_pk1, result_pk1_origx, comps, Peak1_Prop_Lor = fit_pk1(x_pk1, y_corr_pk1, x_span=x_span_pk1, Ne_center=Ne_center_1,model_name=config.model_name, LH_offset_mini=config.LH_offset_mini, peaks_pk1=peaks_1, amplitude=Pk1_Amp, pk1_sigma=config.pk1_sigma,
1285
+ # Fit Pk1
1286
+ cent_pk1, Area_pk1, sigma_pk1, gamma_pk1, Ne_pk1_reg_x_plot, Ne_pk1_reg_y_plot, Ne_pk1_reg_x, Ne_pk1_reg_y, xx_pk1, result_pk1, error_pk1, result_pk1_origx, comps, Peak1_Prop_Lor = fit_Ne_pk(x_pk1, y_corr_pk1, x_span=x_span_pk1, Ne_center=Ne_center_1, model_name=config.model_name, LH_offset_mini=config.LH_offset_mini, peaks_pk1=peaks_1, amplitude=Pk1_Amp, pk1_sigma=config.pk1_sigma,
1254
1287
  const_params=const_params, spec_res=spec_res)
1255
1288
 
1256
-
1257
-
1258
- # Fit the 1447 peak
1259
- cent_pk2,Area_pk2, sigma_pk2, gamma_pk2, Ne_pk2_reg_x_plot, Ne_pk2_reg_y_plot, Ne_pk2_reg_x, Ne_pk2_reg_y, xx_pk2, result_pk2, error_pk2, result_pk2_origx, Peak2_Prop_Lor = fit_pk2( x_pk2, y_corr_pk2, x_span=x_span_pk2, Ne_center=Ne_center_2, model_name=config.model_name, amplitude=Pk2_Amp, pk2_sigma=config.pk2_sigma, const_params=const_params,spec_res=spec_res)
1289
+ # Fit pk2
1290
+ cent_pk2,Area_pk2, sigma_pk2, gamma_pk2, Ne_pk2_reg_x_plot, Ne_pk2_reg_y_plot, Ne_pk2_reg_x, Ne_pk2_reg_y, xx_pk2, result_pk2, error_pk2, result_pk2_origx, comps2, Peak2_Prop_Lor = fit_Ne_pk( x_pk2, y_corr_pk2, x_span=x_span_pk2, Ne_center=Ne_center_2, model_name=config.model_name, LH_offset_mini=config.LH_offset_mini2, peaks_pk1=peaks_2, amplitude=Pk2_Amp, pk1_sigma=config.pk2_sigma, const_params=const_params,spec_res=spec_res)
1260
1291
 
1261
1292
 
1262
1293
  # Calculate difference between peak centers, and Delta Ne