-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutilities.py
1145 lines (1014 loc) · 46 KB
/
utilities.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 python3
# -*- coding: utf-8 -*-
# %%
"""
Created on Tue Jun 11 15:28:47 2019
@author: dejan
"""
import numpy as np
from joblib import Parallel, delayed
from warnings import warn
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider, Button
# from cycler import cycler
import scipy
from scipy import sparse
from scipy.ndimage import median_filter
from scipy.optimize import minimize_scalar
from scipy.interpolate import interp1d
from tqdm import tqdm
from timeit import default_timer as time
import seaborn as sns
def remove_CRs(mock_sp3, sigma_kept, _n_x=0, _n_y=0, **initialization):
# a bit higher then median, or the area:
scaling_koeff = np.trapz(mock_sp3, x=sigma_kept, axis=-1)[:, np.newaxis]
mock_sp3 /= np.abs(scaling_koeff)
normalized_spectra = np.copy(mock_sp3)
# construct the footprint pointing to the pixels surrounding any given pixel:
kkk = np.zeros((2*(_n_x+1) + 1, 1))
# does this value change anything?
kkk[[0, 1, 2, _n_x-1, _n_x+1, -3, -2, -1]] = 1
# each pixel has the median value of its surrounding neighbours:
median_spectra3 = median_filter(mock_sp3, footprint=kkk)
# I will only take into account the positive values (CR):
coarsing_diff = (mock_sp3 - median_spectra3)
# find the highest differences between the spectra and its neighbours:
bad_neighbour = np.quantile(coarsing_diff, 0.99, axis=-1)
# The find the spectra where the bad neighbour is very bad:
# The "very bad" limit is set here at 30*standard deviation (why not?):
basic_candidates = np.nonzero(coarsing_diff > 40*np.std(bad_neighbour))
sind = basic_candidates[0] # the spectra containing very bad neighbours
rind = basic_candidates[1] # each element from the "very bad neighbour"
if len(sind) > 0:
# =====================================================================
# We want to extend the "very bad neighbour" label
# to ext_size adjecent family members in each such spectra:
# =====================================================================
npix = len(sigma_kept)
ext_size = int(npix/50)
if ext_size % 2 != 1:
ext_size += 1
extended_sind = np.stack((sind, )*ext_size, axis=-1).reshape(
len(sind)*ext_size,)
rind_stack = tuple()
for ii in np.arange(-(ext_size//2), ext_size//2+1):
rind_stack += (rind + ii, )
extended_rind = np.stack(rind_stack, axis=-1).reshape(
len(rind)*ext_size,)
# The mirror approach for family members close to the border:
extended_rind[np.nonzero(extended_rind < 0)] =\
-extended_rind[np.nonzero(extended_rind < 0)]
extended_rind[np.nonzero(extended_rind > len(sigma_kept)-1)] =\
(len(sigma_kept)-1)*2 -\
extended_rind[np.nonzero(extended_rind > len(sigma_kept)-1)]
# remove duplicates (https://stackoverflow.com/a/36237337/9368839):
_base = extended_sind.max()+1
_combi = extended_rind + _base * extended_sind
_vall, _indd = np.unique(_combi, return_index=True)
_indd.sort()
extended_sind = extended_sind[_indd]
extended_rind = extended_rind[_indd]
other_candidates = (extended_sind, extended_rind)
mock_sp3[other_candidates] = median_spectra3[other_candidates]
CR_cand_ind = np.unique(sind)
#CR_cand_ind = np.arange(len(spectra_kept))
_ss = np.stack((normalized_spectra[CR_cand_ind],
mock_sp3[CR_cand_ind]), axis=-1)
check_CR_candidates = NavigationButtons(sigma_kept, _ss,
autoscale_y=True,
title=[
f"indice={i}" for i in CR_cand_ind],
label=['normalized spectra',
'median correction'])
if len(CR_cand_ind) > 10:
plt.figure()
sns.violinplot(y=rind)
plt.title("Distribution of Cosmic Rays")
plt.ylabel("CCD pixel struck")
else:
print("No Cosmic Rays found!")
return mock_sp3
class AdjustCR_SearchSensitivity(object):
"""Visually set the sensitivity for the Cosmic Rays detection.
The graph shows the number and the distribution of CR candidates along the
Raman shifts' axis. You can manually adjust the sensitivity
(left=more sensitive, right=less sensitive)
The usage example is the following:
---------------------------------------
first you show the graph and set for the appropriate sensitivity value:
>>> my_class_instance = AdjustCR_SearchSensitivity(spectra, x_values=sigma)
Once you're satisfied with the result, you should recover the following
values:
>>> CR_spectra_ind = my_class_instance.CR_spectra_ind
>>> mask_CR_cand = my_class_instance.mask_CR_cand
>>> mask_whole = my_class_instance.mask_whole
The recovered values are:
CR_spectra_ind: 1D ndarray of ints: The indices of the spectra containing
the Cosmic Rays.
It's length is the number of CRs found.
mask_CR_cand: 2D ndarray of bools: Boolean mask of the same shape as the
spectra containing the CRs.
shape = (len(CR_spectra_ind), len(x_values))
Is True in the zone containing the CR.
mask_whole: 2D ndarray of bools:: Boolean mask of the same shape as the
input spectra. True where the CRs are.
"""
def __init__(self, spectra, x_values=None, gradient_axis=-1):
self.osa = gradient_axis
self.spectra = spectra
if x_values is None:
self.x_values = np.arange(self.spectra.shape[-1])
else:
self.x_values = x_values
assert len(x_values) == self.spectra.shape[-1], "wtf dude?"
self.fig, self.ax = plt.subplots()
# third gradient of the spectra (along the wavenumbers)
self.nabla = np.gradient(np.gradient(np.gradient(self.spectra,
axis=self.osa),
axis=self.osa),
axis=self.osa) # third gradient
self.nabla_dev = np.std(self.nabla, axis=self.osa)
# Create some space for the slider:
self.fig.subplots_adjust(bottom=0.19, right=0.89)
self.axcolor = 'lightgoldenrodyellow'
self.axframe = self.fig.add_axes([0.15, 0.1, 0.7, 0.03],
facecolor=self.axcolor)
self.sframe = Slider(self.axframe, 'Sensitivity',
1, 22,
valinit=8, valfmt='%.1f', valstep=0.1)
# calls the "update" function when changing the slider position
self.sframe.on_changed(self.update)
# Calling the "press" function on keypress event
# (only arrow keys left and right work)
self.fig.canvas.mpl_connect('key_press_event', self.press)
self.CR_spectra_ind, self.mask_whole, self.mask_CR_cand = \
self.calculate_mask(8)
self.line, = self.ax.plot(
self.x_values, np.sum(self.mask_whole, axis=-0))
self.ax.set_title(f"Found {len(self.CR_spectra_ind)} cosmic rays")
plt.show()
def calculate_mask(self, CR_coeff):
self.uslov = CR_coeff*self.nabla_dev[:, np.newaxis]
# find the indices of the potential CR candidates:
self.cand_spectra, self.cand_sigma =\
np.nonzero(np.abs(self.nabla) > self.uslov)
# indices of spectra containing the CR candidates:
self.CR_spectra_ind = np.unique(self.cand_spectra)
# we construct the mask with zeros everywhere except on the positions of CRs:
self.mask_whole = np.zeros_like(self.spectra, dtype=bool)
self.mask_whole[self.cand_spectra, self.cand_sigma] = True
# we now dilate the mask:
# the size of the window depends on resolution
self.ws = int(self.spectra.shape[-1]/10)
self.mask_CR_cand = scipy.ndimage.morphology.binary_dilation(
self.mask_whole[self.CR_spectra_ind],
structure=np.ones((1, self.ws)))
self.mask_whole[self.CR_spectra_ind] = self.mask_CR_cand
return self.CR_spectra_ind, self.mask_whole, self.mask_CR_cand
def update(self, val):
"""Scroll through frames with a slider."""
self.CR_coeff = self.sframe.val
self.CR_spectra_ind, self.mask_whole, self.mask_CR_cand =\
self.calculate_mask(self.CR_coeff)
self.line.set_ydata(np.sum(self.mask_whole, axis=-0))
self.ax.relim()
self.ax.autoscale_view()
self.ax.set_title(f"Found {len(self.CR_spectra_ind)} cosmic rays")
self.fig.canvas.draw_idle()
def press(self, event):
"""Use arrow keys left and right to scroll through frames one by one."""
frame = self.sframe.val
if event.key == 'left' and frame > 1:
new_frame = frame - 0.1
elif event.key == 'right' and frame < 22:
new_frame = frame + 0.1
else:
new_frame = frame
self.sframe.set_val(new_frame)
self.CR_coeff = new_frame
self.CR_spectra_ind, self.mask_whole, self.mask_CR_cand =\
self.calculate_mask(self.CR_coeff)
self.line.set_ydata(np.sum(self.mask_whole, axis=-0))
self.ax.relim()
self.ax.autoscale_view()
self.ax.set_title(f"Found {len(self.CR_spectra_ind)} cosmic rays")
self.fig.canvas.draw_idle()
def find_barycentre(x, y, method="trapz_minimize"):
"""Calculate the index of the barycentre value.
Parameters:
----------
x:1D ndarray: ndarray containing your raman shifts
y:1D ndarray: Ndarray containing your intensity (counts) values
method:string: only "trapz_minimize" for now
Returns:
---------
(x_value, y_value): the coordinates of the barycentre
"""
assert(method in ['trapz_minimize']) # , 'sum_minimize', 'trapz_list'])
# razlika = np.asarray(np.diff(x, append=x[-1]+x[-1]-x[-2]), dtype=np.float16)
# assert(np.all(razlika/razlika[np.random.randint(len(x))] == np.ones_like(x))),\
# "your points are not equidistant"
xx = np.linspace(x.min(), x.max(), 2*len(x))
f = interp1d(x, y, kind='quadratic')
yy = f(xx)
half = np.trapz(yy, x=xx)/2
if method in 'trapz_minimize':
def find_y(Y0, xx=xx, yy=yy, method=method):
'''Internal function to minimize
depending on the method chosen'''
# Calculate the area of the curve above the Y0 value:
part_up = np.trapz(yy[yy >= Y0]-Y0, x=xx[yy >= Y0])
# Calculate the area below Y0:
part_down = np.trapz(yy[yy <= Y0], x=xx[yy <= Y0])
# for the two parts to be the same
to_minimize_ud = np.abs(part_up - part_down)
# fto make the other part be close to half
to_minimize_uh = np.abs(part_up - half)
# to make the other part be close to half
to_minimize_dh = np.abs(part_down - half)
return to_minimize_ud**2+to_minimize_uh+to_minimize_dh
def find_x(X0, xx=xx, yy=yy, method=method):
part_left = np.trapz(yy[xx <= X0], x=xx[xx <= X0])
part_right = np.trapz(yy[xx >= X0], x=xx[xx >= X0])
to_minimize_lr = np.abs(part_left - part_right)
to_minimize_lh = np.abs(part_left - half)
to_minimize_rh = np.abs(part_right - half)
return to_minimize_lr**2+to_minimize_lh+to_minimize_rh
minimized_y = minimize_scalar(find_y, method='Bounded',
bounds=(np.quantile(y, 0.01),
np.quantile(y, 0.99)))
minimized_x = minimize_scalar(find_x, method='Bounded',
bounds=(np.quantile(x, 0.01),
np.quantile(x, 0.99)))
y_value = minimized_y.x
x_value = minimized_x.x
elif method == "list_minimize":
ys = np.sort(yy)
z2 = np.asarray(
[np.abs(np.trapz(yy[yy <= y_val], x=xx[yy <= y_val]) -
np.trapz(yy[yy >= y_val]-y_val, x=xx[yy >= y_val]))
for y_val in ys])
y_value = ys[np.argmin(z2)]
x_ind = np.argmin(np.abs(np.cumsum(yy) - np.sum(yy)/2)) + 1
x_value = xx[x_ind]
return x_value, y_value
def rolling_median(arr, w_size, ax=0, mode='nearest', *args):
'''Calculates the rolling median of an array
along the given axis on the given window size.
Parameters:
-------------
arr:ndarray: input array
w_size:int: the window size
(should be less then the dimension along the given axis)
ax:int: the axis along which to calculate the rolling median
mode:str: to choose from ['reflect', 'constant', 'nearest', 'mirror', 'wrap']
see the docstring of ndimage.median_filter for details
Returns:
------------
ndarray of same shape as the input array'''
shape = np.ones(np.ndim(arr), dtype=int)
shape[ax] = w_size
return median_filter(arr, size=shape, mode=mode, *args)
def baseline_als(y, lam=1e5, p=5e-5, niter=12):
'''Adapted from:
https://stackoverflow.com/questions/29156532/python-baseline-correction-library.
To get the feel on how the algorithm works, you can think of it as
if the rolling ball which comes from beneath the spectrum and thus sets
the baseline.
Then, to follow the image, schematic explanaton of the params would be:
Params:
----------
y: 1D or 2D ndarray: the spectra on which to find the baseline
lam:number: Can be viewed as the radius of the ball.
As a rule of thumb, this value should be around the
twice the width of the broadest feature you want to keep
(width is to be measured in number of points, since
for the moment no x values are taken into accound
in this algorithm)
p:number: Can be viewed as the measure of how much the ball
can penetrate into the spectra from below
niter:int: number of iterations
(the resulting baseline should stabilize after
some number of iterations)
Returns:
-----------
b_line:ndarray: the baseline (same shape as y)
Note:
----------
It takes around 2-3 sec per 1000 spectra with 10 iterations
on i7 4cores(8threads) @1,9GHz
'''
def _one_bl(yi, lam=lam, p=p, niter=niter, z=None):
if z is None:
L = yi.shape[-1]
D = sparse.diags([1, -2, 1], [0, -1, -2], shape=(L, L-2))
# Precompute this term since it does not depend on `w`
D = lam * D.dot(D.transpose())
w = np.ones(L)
W = sparse.spdiags(w, 0, L, L)
for i in range(niter):
# Do not create a new matrix, just update diagonal values
W.setdiag(w)
Z = W + D
z = sparse.linalg.spsolve(Z, w*yi)
w = p * (yi > z) + (1-p) * (yi < z)
return z
_start = time()
print("\nstarting the baseline correction..."
f"\n(be patient, this may take some time...)")
if y.ndim == 1:
b_line = _one_bl(y)
elif y.ndim == 2:
b_line = np.asarray(Parallel(n_jobs=-1)(delayed(_one_bl)(y[i])
for i in tqdm(range(y.shape[0]))))
else:
warn("This only works for 1D or 2D arrays")
_end = time()
print(f"baseline correction done in {_end - _start:.2f}s")
return b_line
def slice_lr(spectra, sigma=None, **kwargs):
'''
Several reasons may make you want to apply the slicing.
a) Your spectra might have been recorded with the dead pixels included.
It is normaly a parameter which should had been set at the spectrometer
configuration (Contact your spectros's manufacturer for assistance)
b) You might want to isolate only a part of the spectra which
interests you.
c) You might have made a poor choice of the spectral range at the
moment of recording the spectra.
Parameters:
---------------
spectra: N-D ndarray
your spectra. The last dimension corresponds
to one spectrum recorded at given position
sigma: 1D ndarray
your Raman shifts. Default is None, meaning
that the slicing will be applied based on the
indices of spectra, not Raman shift values
pos_left :int or float
position from which to start the slice. If sigma
is given, pos_left is the lower Raman shift value,
if not, it's the lower index of the spectra.
pos-right:int or float
same as for pos_left, but on the right side.
It can be negative (means you count from the end)
Returns:
---------------
spectra_kept: N-D ndarray
your spectra containing only the zone of interest.
spectra_kept.shape[:-1] = spectra_shape[:-1]
spectra_kept.shape[-1] <= spectra.shape[-1]
sigma_kept: 1D ndarray
if sigma is given: your Raman shift values for the isolated zone.
len(sigma_kept)=spectra_kept.shape[-1] <= len(sigma)=spectra.shape[-1]
if sigma is not given: indices of the zone of interest.
'''
if sigma is None:
sigma = np.arange(spectra.shape[-1])
pos_left, pos_right = kwargs.get("SliceValues", (None, None))
# If you pass a negative number as the right position:
if isinstance(pos_right, (int, float)):
if pos_right < 0:
pos_right = sigma[pos_right]
if pos_left is None:
pos_left = sigma.min()
if pos_right is None:
pos_right = sigma.max()
assert pos_left <= pos_right, "Check your initialization Slices!"
_condition = (sigma >= pos_left) & (sigma <= pos_right)
sigma_kept = sigma[_condition] # add np.copy if needed
spectra_kept = np.asarray(spectra[..., _condition], order='C')
return spectra_kept, sigma_kept
def skip_ud(spectra, _n_x=0, **kwargs):
"""Removing the lines from top and/or bottom of the map"""
skip_lines_up = kwargs.get("NumberOfLinesToSkip_Beggining", 0)
skip_lines_down = kwargs.get("NumberOfLinesToSkip_End", 0)
_start_pos = skip_lines_up * _n_x
if skip_lines_down == 0:
_end_pos = None
else:
_end_pos = -np.abs(skip_lines_down) * _n_x
return spectra[_start_pos:_end_pos]
def pV(x, h=30, x0=0, w=10, factor=0.5):
'''Manualy created pseudo-Voigt profile
Parameters:
------------
x: Independent variable
h: height
x0: The position of the peak on the x-axis
w: FWHM
factor: the ratio of lorentz vs gauss in the peak
Returns:
y-array of the same shape as the input x-array
'''
def Gauss(x, w):
return((2/w) * np.sqrt(np.log(2)/np.pi) * np.exp(
-(4*np.log(2)/w**2) * (x - x0)**2))
def Lorentz(x, w):
return((1/np.pi)*(w/2) / (
(x - x0)**2 + (w/2)**2))
intensity = h * np.pi * (w/2) / (
1 + factor * (np.sqrt(np.pi*np.log(2)) - 1))
return(intensity * (factor * Gauss(x, w)
+ (1-factor) * Lorentz(x, w)))
def multi_pV(x, *params):
'''
The function giving the sum of the pseudo-Voigt peaks.
Parameters:
x: independent variable
*params: is a list of parameters.
Its length is = 4 * "number of peaks",
where 4 is the number of parameters in the "pV" function.
Look in the docstring of pV function for more info on theese.
'''
# The following presupposes that the first argument of the function
# is the independent variable and that the subsequent parameters are
# the function parameters:
n = 4
nn = len(params)
if nn % n != 0:
raise Exception(f"You gave {nn} parameters and your basic function"
f"takes {n} parameters (The first one should be x"
"and the remainign ones the parameters of the funct.")
result = np.zeros_like(x, dtype=np.float)
for i in range(0, nn, n):
result += pV(x, *params[i:i+n]) # h, x0, w, r)
return result
def create_map_spectra(x=None, initial_peak_params=[171, 200, 8, 0.7], N=2000, ponderation=None):
'''Creates simulated spectra
Params:
x: independent variable
initial_peak_params: list of peak parameters
the number of parameters must be a multiple of number of parameters
demanded by peak_function (let's say that number is N)
So, then you can set-up M peaks, just by supplying M x N parameters
peak_function: default is pseudo-Voigt
N: the number of spectra to create
ponderation: How much you want the spectra to differ between them
'''
if x is None:
xmin = np.min(initial_peak_params[1::4])*0.8
xmax = np.max(initial_peak_params[1::4])*1.2
x = np.linspace(xmin, xmax, 300)
if not ponderation:
ponderation = np.asarray(initial_peak_params)/5 + 1
else:
ponderation = np.asarray(ponderation)
nn = len(initial_peak_params)
peaks_params = (1 + (np.random.rand(N, nn) - 0.5) / ponderation) \
* np.asarray(initial_peak_params)
spectra = np.asarray([(multi_pV(x, *peaks_params[i]) + (np.random.random(len(x))-0.5)*5)
* (1 + (np.random.random(len(x))-0.5)/20) for i in range(N)])
return spectra, x
# %%
class AllMaps(object):
'''
Allows one to rapidly visualize maps of Raman spectra.
You can also choose to visualize the map and plot the
corresponding component side by side if you set the
"components" parameter.
Parameters:
-----------
map_spectra: 3D ndarray
the spectra shaped as (n_lines, n_columns, n_wavenumbers)
sigma: 1D ndarray
an array of wavenumbers (len(sigma)=n_wavenumbers)
components: 2D ndarray
The most evident use-case would be to
help visualize the decomposition results from PCA or NMF.
In this case, the function will plot the component with
the corresponding map visualization of the given components'
presence in each of the points in the map.
So, in this case, your map_spectra would be for example
the matrix of components' contributions in each spectrum, while
the "components" array will be your actual components.
In this case you can ommit your sigma values or set them to
something like np.arange(n_components)
components_sigma: 1D ndarray
in the case explained above, this would be the actual wavenumbers
**kwargs: dict: can only take 'title' as a key for the moment
Returns:
--------
The interactive visualization (you can scroll through sigma values
with a slider, or using left/right keyboard arrows)
'''
def __init__(self, map_spectra, sigma=None, components=None, components_sigma=None, **kwargs):
self.map_spectra = map_spectra
if sigma is None:
self.sigma = np.arange(map_spectra.shape[-1])
else:
assert map_spectra.shape[-1] == len(
sigma), "Check your Ramans shifts array"
self.sigma = sigma
self.first_frame = 0
self.last_frame = len(self.sigma)-1
if components is not None:
#assert len(components) == map_spectra.shape[-1], "Check your components"
self.components = components
self.last_frame = len(self.components)-1
if components_sigma is None:
self.components_sigma = np.arange(components.shape[-1])
else:
self.components_sigma = components_sigma
else:
self.components = None
if components is not None:
self.fig, (self.ax2, self.ax, self.cbax) = plt.subplots(
ncols=3, gridspec_kw={'width_ratios': [40, 40, 1]})
self.cbax.set_box_aspect(
40*self.map_spectra.shape[0]/self.map_spectra.shape[1])
else:
self.fig, (self.ax, self.cbax) = plt.subplots(
ncols=2, gridspec_kw={'width_ratios': [40, 1]})
self.cbax.set_box_aspect(
40*self.map_spectra.shape[0]/self.map_spectra.shape[1])
#self.cbax = self.fig.add_axes([0.92, 0.3, 0.03, 0.48])
# Create some space for the slider:
self.fig.subplots_adjust(bottom=0.19, right=0.89)
self.title = kwargs.get('title', None)
self.im = self.ax.imshow(self.map_spectra[:, :, 0])
self.im.set_clim(np.percentile(self.map_spectra[:, :, 0], [1, 99]))
if self.components is not None:
self.line, = self.ax2.plot(
self.components_sigma, self.components[0])
self.ax2.set_box_aspect(
self.map_spectra.shape[0]/self.map_spectra.shape[1])
self.ax2.set_title(f"Component {0}")
self.titled(0)
self.axcolor = 'lightgoldenrodyellow'
self.axframe = self.fig.add_axes(
[0.15, 0.1, 0.7, 0.03], facecolor=self.axcolor)
self.sframe = Slider(self.axframe, 'Frame',
self.first_frame, self.last_frame,
valinit=self.first_frame, valfmt='%d', valstep=1)
self.my_cbar = mpl.colorbar.Colorbar(self.cbax, self.im)
# calls the "update" function when changing the slider position
self.sframe.on_changed(self.update)
# Calling the "press" function on keypress event
# (only arrow keys left and right work)
self.fig.canvas.mpl_connect('key_press_event', self.press)
plt.show()
def titled(self, frame):
if self.components is None:
if self.title is None:
self.ax.set_title(f"Raman shift = {self.sigma[frame]:.1f}cm⁻¹")
else:
self.ax.set_title(f"{self.title} n°{frame}")
else:
self.ax2.set_title(f"Component {frame}")
if self.title is None:
self.ax.set_title(f"Component n°{frame} contribution")
else:
self.ax.set_title(f"{self.title} n°{frame}")
def update(self, val):
'''This function is for using the slider to scroll through frames'''
frame = int(self.sframe.val)
img = self.map_spectra[:, :, frame]
self.im.set_data(img)
self.im.set_clim(np.percentile(img, [1, 99]))
if self.components is not None:
self.line.set_ydata(self.components[frame])
self.ax2.relim()
self.ax2.autoscale_view()
self.titled(frame)
self.fig.canvas.draw_idle()
def press(self, event):
'''This function is to use arrow keys left and right to scroll
through frames one by one'''
frame = int(self.sframe.val)
if event.key == 'left' and frame > 0:
new_frame = frame - 1
elif event.key == 'right' and frame < len(self.sigma)-1:
new_frame = frame + 1
else:
new_frame = frame
self.sframe.set_val(new_frame)
img = self.map_spectra[:, :, new_frame]
self.im.set_data(img)
self.im.set_clim(np.percentile(img, [1, 99]))
self.titled(new_frame)
if self.components is not None:
self.line.set_ydata(self.components[new_frame])
self.ax2.relim()
self.ax2.autoscale_view()
self.fig.canvas.draw_idle()
# %%
class NavigationButtons(object):
'''This class allows you to visualize multispectral data and
navigate trough your spectra simply by clicking on the
navigation buttons on the graph.
-------------------
Parameters:
sigma: 1D numpy array of your x-values (raman shifts, par ex.)
spectra: 3D or 2D ndarray of shape (n_spectra, len(sigma), n_curves).
The last dimension may be ommited it there is only one curve
to be plotted for each spectra),
autoscale: bool determining if you want to adjust the scale to each spectrum
title: The initial title describing where the spectra comes from
label: list: A list explaining each of the curves. len(label) = n_curves
Output:
matplotlib graph with navigation buttons to cycle through spectra
Example:
# Let's say you have a ndarray containing 10 spectra, each 500 points long
# base_spectras.shape should give (10, 500)
# your sigma.shape should be (500, )
# Then let's say you fitted each of your spectra with 3 gaussian peaks
# and you want to plot these as well. For each of your ten spectra,
# you will have something like:
>>>spectra_fitted[i] = multiple_gaussian_function(sigma, *params[i])
# your spectra_fitted should have the same shape as your spectra.
# Now, let's say you want also to plot each of the gaussian peaks as well
# for "i"th spectra you will have 3 gaussians
>>>for k in range(3):
>>>G[i][k] = single_gaussian_function(sigma, *params[i][k])
# At the end, you stack all of this in one ndarray :
>>>multiple_curves_to_plot = np.stack((
base_spectras, spectra_fitted, G1, G2, G3), axis=-1)
>>>NavigationButtons(sigma, multiple_curves_to_plot)
'''
ind = 0
def __init__(self, sigma, spectra, autoscale_y=False, title='Spectrum', label=False,
**kwargs):
self.y_autoscale = autoscale_y
if len(spectra.shape) == 2:
self.s = spectra[:, :, np.newaxis]
elif len(spectra.shape) == 3:
self.s = spectra
else:
raise ValueError("Check the shape of your spectra.\n"
"It should be (n_spectra, n_points, n_curves)\n"
"(this last dimension might be ommited if it's equal to one)")
self.n_spectra = self.s.shape[0]
if isinstance(title, list) or isinstance(title, np.ndarray):
if len(title) == spectra.shape[0]:
self.title = title
else:
raise ValueError(f"you have {len(title)} titles,\n"
f"but you have {len(spectra)} spectra")
else:
self.title = [title]*self.n_spectra
self.sigma = sigma
if label:
if len(label) == self.s.shape[2]:
self.label = label
else:
warn(
"You should check the length of your label list.\nFalling on to default labels...")
self.label = ["Curve n°"+str(numb)
for numb in range(self.s.shape[2])]
else:
self.label = ["Curve n°"+str(numb)
for numb in range(self.s.shape[2])]
self.figr, self.axr = plt.subplots(**kwargs)
self.axr.set_title(f'{title[0]}')
self.figr.subplots_adjust(bottom=0.2)
# l potentially contains multiple lines
self.l = self.axr.plot(self.sigma, self.s[0], lw=2, alpha=0.7)
self.axr.legend(self.l, self.label)
self.axprev1000 = plt.axes([0.097, 0.05, 0.1, 0.04])
self.axprev100 = plt.axes([0.198, 0.05, 0.1, 0.04])
self.axprev10 = plt.axes([0.299, 0.05, 0.1, 0.04])
self.axprev1 = plt.axes([0.4, 0.05, 0.1, 0.04])
self.axnext1 = plt.axes([0.501, 0.05, 0.1, 0.04])
self.axnext10 = plt.axes([0.602, 0.05, 0.1, 0.04])
self.axnext100 = plt.axes([0.703, 0.05, 0.1, 0.04])
self.axnext1000 = plt.axes([0.804, 0.05, 0.1, 0.04])
self.bprev1000 = Button(self.axprev1000, 'Prev.1000')
self.bprev1000.on_clicked(self.prev1000)
self.bprev100 = Button(self.axprev100, 'Prev.100')
self.bprev100.on_clicked(self.prev100)
self.bprev10 = Button(self.axprev10, 'Prev.10')
self.bprev10.on_clicked(self.prev10)
self.bprev = Button(self.axprev1, 'Prev.1')
self.bprev.on_clicked(self.prev1)
self.bnext = Button(self.axnext1, 'Next1')
self.bnext.on_clicked(self.next1)
self.bnext10 = Button(self.axnext10, 'Next10')
self.bnext10.on_clicked(self.next10)
self.bnext100 = Button(self.axnext100, 'Next100')
self.bnext100.on_clicked(self.next100)
self.bnext1000 = Button(self.axnext1000, 'Next1000')
self.bnext1000.on_clicked(self.next1000)
def update_data(self):
_i = self.ind % self.n_spectra
for ll in range(len(self.l)):
yl = self.s[_i][:, ll]
self.l[ll].set_ydata(yl)
self.axr.relim()
self.axr.autoscale_view(None, False, self.y_autoscale)
self.axr.set_title(f'{self.title[_i]}; N°{_i}')
self.figr.canvas.draw()
self.figr.canvas.flush_events()
def next1(self, event):
self.ind += 1
self.update_data()
def next10(self, event):
self.ind += 10
self.update_data()
def next100(self, event):
self.ind += 100
self.update_data()
def next1000(self, event):
self.ind += 1000
self.update_data()
def prev1(self, event):
self.ind -= 1
self.update_data()
def prev10(self, event):
self.ind -= 10
self.update_data()
def prev100(self, event):
self.ind -= 100
self.update_data()
def prev1000(self, event):
self.ind -= 1000
self.update_data()
# %%
class fitonclick(object):
'''This class is used to interactively draw pseudo-voigt (or other type)
peaks, on top of your data.
It was originaly created to help defining initial fit parameters to
pass on to SciPy CurveFit.
IMPORTANT! See the Example below, to see how to use the class
Parameters:
x: independent variable
y: your data
initial_GaussToLorentz_ratio:float between 0 and 1, default=0.5
Pseudo-Voigt peak is composed of a Gaussian and of a Laurentzian
part. This ratio defines the proportion of those parts.
scrolling_speed: float>0, default=1
defines how quickly your scroling widens peaks
initial_width: float>0, default=5
defines initial width of peaks
**kwargs: dictionary, for exemple {'figsize':(9,9)}
whatever you want to pass to plt.subplots(**kwargs)
Returns:
Nothing, but you can access the atributes using class instance, like
fitonclick.pic: dictionnary containing the parameters of each peak added
fitonclick.sum_peak: list containing cumulated graph line
to get the y-values, use sum_peak[-1][0].get_ydata()
fitonclick.peak_counter: int giving the number of peaks present
etc.
Example:
>>>my_class_instance = fitonclick(x, y)
>>>while my_class_instance.block:
>>> plt.waitforbuttonpress(timeout=-1)
'''
# Initiating variables to which we will atribute peak caractéristics:
pic = {}
pic['line'] = [] # List containing matplotlib.Line2D object for each peak
pic['h'] = [] # List that will contain heights of each peak
pic['x0'] = [] # List that will contain central positions of each peak
pic['w'] = [] # List containing widths
pic['GL'] = []
# List of cumulated graphs
# (used later for updating while removing previous one)
sum_peak = []
peak_counter: int = 0 # number of peaks on the graph
cum_graph_present: int = 0 # only 0 or 1
scroll_count = 0 # counter to store the cumulative values of scrolling
artists = [] # will be used to store the elipses on tops of the peaks
block = True
def __init__(self, x, y,
initial_GaussToLoretnz_ratio=0.5,
scrolling_speed=1,
initial_width=5,
**kwargs):
plt.ioff()
self.x = x
self.y = y
self.GL = initial_GaussToLoretnz_ratio
self.scrolling_speed = scrolling_speed
self.initial_width = initial_width
# Setting up the plot:
self.fig, self.ax = plt.subplots(**kwargs)
self.ax.plot(self.x, self.y,
linestyle='none', marker='o', c='k', ms=4, alpha=0.5)
self.ax.set_title('Left-click to add/remove peaks,'
'Scroll to adjust width, \nRight-click to draw sum,'
' Double-Right-Click when done')
self.x_size = self.set_size(self.x)
self.y_size = 2*self.set_size(self.y)
self.cid = self.fig.canvas.mpl_connect(
'button_press_event', self.onclick)
self.cid2 = self.fig.canvas.mpl_connect('scroll_event', self.onclick)
def set_size(self, variable, rapport=70):
return (variable.max() - variable.min())/rapport
def _add_peak(self, event):
self.peak_counter += 1
h = event.ydata
x0 = event.xdata
yy = pV(x=self.x, h=h,
x0=x0, w=self.x_size*self.initial_width, factor=self.GL)
one_elipsis = self.ax.add_artist(
Ellipse((x0, h),
self.x_size, self.y_size, alpha=0.5,
gid=str(self.peak_counter)))
self.artists.append(one_elipsis)
self.pic['line'].append(self.ax.plot(self.x, yy,
alpha=0.75, lw=2.5,
picker=5))
# ax.set_ylim(auto=True)
self.pic['h'].append(h)
self.pic['x0'].append(x0)
self.pic['w'].append(self.x_size*self.initial_width)
self.fig.canvas.draw_idle()
# return(self.artists, self.pic)
def _adjust_peak_width(self, event, peak_identifier=-1):
self.scroll_count += self.x_size * np.sign(event.step) *\
self.scrolling_speed/10
if self.scroll_count > -self.x_size*self.initial_width*0.999:
w2 = self.x_size*self.initial_width + self.scroll_count
else:
w2 = self.x_size * self.initial_width / 1000
# This doesn't allow you to sroll to negative values
# (basic width is x_size)
self.scroll_count = -self.x_size * self.initial_width * 0.999
center2 = self.pic['x0'][peak_identifier]
h2 = self.pic['h'][peak_identifier]
self.pic['w'][peak_identifier] = w2
yy = pV(x=self.x, x0=center2, h=h2, w=w2, factor=self.GL)
active_line = self.pic['line'][peak_identifier][0]
# This updates the values on the peak identified
active_line.set_ydata(yy)
self.ax.draw_artist(active_line)
self.fig.canvas.draw_idle()
# return(scroll_count, pic)
def _remove_peak(self, clicked_indice):
self.artists[clicked_indice].remove()
self.artists.pop(clicked_indice)
self.ax.lines.remove(self.pic['line'][clicked_indice][0])
self.pic['line'].pop(clicked_indice)
self.pic['x0'].pop(clicked_indice)
self.pic['h'].pop(clicked_indice)
self.pic['w'].pop(clicked_indice)
self.fig.canvas.draw_idle()
self.peak_counter -= 1
# return(artists, pic)
def _draw_peak_sum(self):
if self.peak_counter < 1:
return
def _remove_sum(self):
assert self.cum_graph_present == 1, "no sum drawn, nothing to remove"
self.ax.lines.remove(self.sum_peak[-1][0])
self.sum_peak.pop()
self.cum_graph_present -= 1
# return sum_peak
def _add_sum(self, sumy):
assert sumy.shape == self.x.shape, "something's wrong with your data"
self.sum_peak.append(self.ax.plot(self.x, sumy, '--',
color='lightgreen',
lw=3, alpha=0.6))
self.cum_graph_present += 1
# return sum_peak
# Sum all the y values from all the peaks:
sumy = np.sum(np.asarray(
[self.pic['line'][i][0].get_ydata() for i in range(self.peak_counter)]),
axis=0)
# Check if there is already a cumulated graph plotted:
if self.cum_graph_present == 1:
# Check if the sum of present peaks correponds to the cumulated graph
if np.array_equal(self.sum_peak[-1][0].get_ydata(), sumy):
pass
else: # if not, remove the last cumulated graph from the figure:
_remove_sum(self)
# and then plot the new cumulated graph:
_add_sum(self, sumy=sumy)
# No cumulated graph present:
elif self.cum_graph_present == 0:
# plot the new cumulated graph
_add_sum(self, sumy=sumy)
else:
raise("WTF?")
self.fig.canvas.draw_idle()
# return(cum_graph_present, sum_peak)