-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaps.py
More file actions
1304 lines (952 loc) · 48.2 KB
/
maps.py
File metadata and controls
1304 lines (952 loc) · 48.2 KB
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
# import PyQt5 elements
from PyQt5.QtWidgets import QApplication
# general imports
from os import path
import numpy
import pickle
# datatype imports
from datatypes.maps1d.qtlab import QtLab1D
from datatypes.maps1d.qcodes import QCoDeS1D
from datatypes.maps2d.qtlab import QtLab2D
from datatypes.maps2d.qcodes import QCoDeS2D
from datatypes.maps2d.horiba import Horiba2D
from datatypes.maps2d.vuckovic import Vuckovic2D
import time
class Map:
"""
Map
This class defines the basic functions of all maps.
The Map1D and Map2D classes inherit from this class.
"""
def __init__(self):
# create all general variables
self._app = None
self._data_names = {} # dictionary for all data names
self._dimension = 0 # the dimension of the map (1D, 2D)
self._focus = [] # the currently focused pixel
self._id = 0 # the map id
self._interval = [0, 0] # integration interval for energy
self._map_name = '' # the map name
self._resolution = 0 # the pixels on the CCD
self._selected_data = 0 # a flag for the currently selected data
def get_data_names(self):
# return data names
return self._data_names
def get_dimension(self):
# return map dimension
return self._dimension
def get_focus(self):
# return focus
return self._focus
def get_id(self):
# return the map ID
return self._id
def get_interval(self):
# return interval
return self._interval
def get_map_name(self):
# return map name
return self._map_name
def get_resolution(self):
# return pixels on CCD
return self._resolution
def get_selected_data(self):
# return the currently selected data
return self._selected_data
def set_app(self, app):
# set map list
self._app = app
def set_id(self, map_id):
# set map id
self._id = map_id
def set_selected_data(self, selected_data):
# update data selection if the new data is different from the old one
if selected_data != self._selected_data:
self._selected_data = selected_data
# emit signal
self._app.selected_data_changed.emit(self._id)
class Map1D(Map):
"""
Map1D
Class for one-dimensional maps such as gate-dependent measurements.
"""
def __init__(self, map_id, file_name):
# call super init
super(Map1D, self).__init__()
# set dimension of the map
self._dimension = 1
# set the map id
self._id = map_id
# get the directory from path
dir_name = path.dirname(file_name)
# check for the file type of the map
# .dat files are acquired in the PGI9 (FZJ) lab using QTLab
if file_name[-4:] == '.dat':
# define map loader
map_loader = QtLab1D(file_name)
elif file_name[-4:] == '.pck':
# define map loader
map_loader = QCoDeS1D(file_name)
# load data
self._map_name, self._spectra, self._data_names, self._data = map_loader.load_data()
# set map size
self._nx = self._spectra.shape[0]
# set spectral resolution
self._resolution = self._spectra.shape[1]
# set initial interval for the integration of the spectra
self._interval = [0, self._resolution-1]
# get integrated counts, average energy and maximum energy
self._int_counts = numpy.sum(self._spectra[:, :, 1], axis=1)
self._mean_energies = numpy.sum(self._spectra[:, :, 0]*self._spectra[:, :, 1], axis=1)/self._int_counts
max_pixels = numpy.argmax(self._spectra[:, :, 1], axis=1)
self._max_energies = numpy.zeros((self._nx))
for ix in range(self._nx):
self._max_energies[ix] = self._spectra[ix, max_pixels[ix], 0]
# create variables for the fit data
self._fit_functions = numpy.zeros((self._nx, 6))
self._fit_initial_parameters = numpy.zeros((self._nx, 6, 4))
self._fit_initial_parameters[:, :, :] = numpy.NAN
self._fit_optimized_parameters = numpy.zeros((self._nx, 6, 4))
self._fit_optimized_parameters[:, :, :] = numpy.NAN
# set focus to the center of the map
self._focus = [int(self._nx / 2)]
def clear_fit(self, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
else:
px = kwargs['pixel'][0]
# clear fit
self._fit_functions[px, :] = numpy.zeros(6)
self._fit_initial_parameters[px, :, :] = numpy.NAN
self._fit_optimized_parameters[px, :, :] = numpy.NAN
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
# TODO: Flip for 1D
def get_data(self, **kwargs):
# if no data index is given, return the currently selected data
if 'data_index' in kwargs.keys():
data_index = kwargs['data_index']
else:
data_index = self._selected_data
# return a data
if data_index == 0:
return self._spectra[:, :, 1]
elif data_index == 1:
# check if the whole map data (pixel = -1) or the focussed pixel (pixel = -2)
# or a specific pixel (pixel = [x,y]) are requested
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
# return whole map data
return self._int_counts
elif kwargs['pixel'] == -2:
# return data at focused pixel
return self._int_counts[self._focus[0]]
else:
# return data at requested pixel
return self._int_counts[kwargs['pixel'][0]]
elif data_index == 2:
# check if the whole map data (pixel = -1) or the focussed pixel (pixel = -2)
# or a specific pixel (pixel = [x,y]) are requested
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
# return whole map data
return self._mean_energies
elif kwargs['pixel'] == -2:
# return data at focused pixel
return self._mean_energies[self._focus[0]]
else:
# return data at requested pixel
return self._mean_energies[kwargs['pixel'][0]]
elif data_index == 3:
# check if the whole map data (pixel = -1) or the focussed pixel (pixel = -2)
# or a specific pixel (pixel = [x,y]) are requested
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
# return whole map data
return self._max_energies
elif kwargs['pixel'] == -2:
# return data at focused pixel
return self._max_energies[self._focus[0]]
else:
# return data at requested pixel
return self._max_energies[kwargs['pixel'][0]]
elif 3 < data_index < 4 + len(self._data):
data_index -= 4
# check if the whole map data (pixel = -1) or the focussed pixel (pixel = -2)
# or a specific pixel (pixel = [x,y]) are requested
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
# return whole map data
return self._data[data_index]
elif kwargs['pixel'] == -2:
# return data at focused pixel
return self._data[data_index][self._focus[0]]
else:
# return data at requested pixel
return self._data[data_index][kwargs['pixel'][0]]
# return a fit data
else:
# check which fit parameters are there
data_index -= 4 + len(self._data)
# get fit functions and fit parameters once
fit_functions = self._fit_functions
fit_optimized_parameters = self._fit_optimized_parameters
parameters = []
for i_peak in range(6):
if numpy.sum(numpy.int_(fit_functions[:, i_peak] > 0)) > 0:
parameters.append([i_peak, 0])
parameters.append([i_peak, 1])
if numpy.sum(numpy.int_(fit_functions[:, i_peak] == 2)) > 0 or \
numpy.sum(numpy.int_(fit_functions[:, i_peak] == 3)) > 0:
parameters.append([i_peak, 2])
if numpy.sum(numpy.int_(fit_functions[:, i_peak] == 1)) > 0 or \
numpy.sum(numpy.int_(fit_functions[:, i_peak] == 3)) > 0:
parameters.append([i_peak, 3])
if numpy.sum(numpy.int_(fit_functions[:, i_peak] > 0)) > 0:
parameters.append([i_peak, 4])
# return intensities
if parameters[data_index][1] == 0:
return fit_optimized_parameters[:, parameters[data_index][0], 0]
# return central energies
elif parameters[data_index][1] == 1:
return fit_optimized_parameters[:, parameters[data_index][0], 1]
# return sigma
elif parameters[data_index][1] == 2:
sigma = numpy.zeros((self._nx))
sigma[:] = numpy.NAN
sigma_from_gaussian = fit_optimized_parameters[:, parameters[data_index][0], 2][fit_functions[:, parameters[data_index][0]] == 2]
sigma_from_voigt = fit_optimized_parameters[:, parameters[data_index][0], 2][fit_functions[:, parameters[data_index][0]] == 3]
sigma[fit_functions[:, parameters[data_index][0]] == 2] = sigma_from_gaussian
sigma[fit_functions[:, parameters[data_index][0]] == 3] = sigma_from_voigt
return 1000*sigma
# return gamma
elif parameters[data_index][1] == 3:
gamma = numpy.zeros((self._nx))
gamma[:] = numpy.NAN
gamma_from_lorentzian = fit_optimized_parameters[:, parameters[data_index][0], 2][fit_functions[:, parameters[data_index][0]] == 1]
gamma_from_voigt = fit_optimized_parameters[:, parameters[data_index][0], 3][fit_functions[:, parameters[data_index][0]] == 3]
gamma[fit_functions[:, parameters[data_index][0]] == 1] = gamma_from_lorentzian
gamma[fit_functions[:, parameters[data_index][0]] == 3] = gamma_from_voigt
return 1000*gamma
# return FWHM
elif parameters[data_index][1] == 4:
fwhm = numpy.zeros((self._nx))
fwhm[:] = numpy.NAN
sigma_from_gaussian = fit_optimized_parameters[:, parameters[data_index][0], 2][fit_functions[:, parameters[data_index][0]] == 2]
gamma_from_lorentzian = fit_optimized_parameters[:, parameters[data_index][0], 2][fit_functions[:, parameters[data_index][0]] == 1]
gamma_from_voigt = fit_optimized_parameters[:, parameters[data_index][0], 3][fit_functions[:, parameters[data_index][0]] == 3]
sigma_from_voigt = fit_optimized_parameters[:, parameters[data_index][0], 2][fit_functions[:, parameters[data_index][0]] == 3]
fwhm[fit_functions[:, parameters[data_index][0]] == 1] = 2*gamma_from_lorentzian
fwhm[fit_functions[:, parameters[data_index][0]] == 2] = 2.35482*sigma_from_gaussian
fwhm[fit_functions[:, parameters[data_index][0]] == 3] = 0.5346*2.*gamma_from_voigt+numpy.sqrt(0.2166*4.*gamma_from_voigt**2.+2.35482**2.*sigma_from_voigt**2.)
return 1000*fwhm
def get_data_name(self, **kwargs):
# if no data index is given, return the currently selected data name
if 'data_index' in kwargs.keys():
data_index = kwargs['data_index']
else:
data_index = self._selected_data
# check whether a data or a micrograph or a fit parameter is selected
if data_index == 0:
return 'spectra'
elif data_index == 1:
return 'spectra --integral'
elif data_index == 2:
return 'spectra --mean'
elif data_index == 3:
return 'spectra --maximum'
elif 3 < data_index < 4 + len(self._data):
data_index -= 4
# return data name
return self._data_names[data_index]
else:
data_index -= 4 + len(self._data)
# return parameter name
parameters = []
subscripts = [u'\u2081', u'\u2082', u'\u2083', u'\u2084', u'\u2085', u'\u2086']
for i_peak in range(6):
if numpy.sum(numpy.int_(self._fit_functions[:, i_peak] > 0)) > 0:
parameters.append('I'+subscripts[i_peak])
parameters.append('ε'+subscripts[i_peak])
if numpy.sum(numpy.int_(self._fit_functions[:, i_peak] == 1)) > 0 or \
numpy.sum(numpy.int_(self._fit_functions[:, i_peak] == 3)) > 0:
parameters.append('σ'+subscripts[i_peak])
if numpy.sum(numpy.int_(self._fit_functions[:, i_peak] == 2)) > 0 or \
numpy.sum(numpy.int_(self._fit_functions[:, i_peak] == 3)) > 0:
parameters.append('γ'+subscripts[i_peak])
parameters.append('FWHM'+subscripts[i_peak])
return parameters[data_index]
def get_fit(self, **kwargs):
# if no pixel was provided the current pixel is returned
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
else:
px = kwargs['pixel'][0]
return self._fit_functions[px, :], self._fit_initial_parameters[px, :, :], self._fit_optimized_parameters[px, :, :]
def get_fit_functions(self, **kwargs):
# if no pixel was provided return the whole fit functions array
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._fit_functions[:, :]
# if pixel is set to -2 return the fit functions for the focused pixel
elif kwargs['pixel'] == -2:
return self._fit_functions[self._focus[0], :]
# return the fit functions for the desired pixel
else:
return self._fit_functions[kwargs['pixel'][0], :]
def get_fit_parameters(self, **kwargs):
# if no pixel was provided return the whole fit parameter array
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._fit_optimized_parameters[:, :, :]
# if pixel is set to -2 return the fit parameters for the focused pixel
elif kwargs['pixel'] == -2:
return self._fit_optimized_parameters[self._focus[0], :, :]
# return the fit parameters for the desired pixel
else:
return self._fit_optimized_parameters[kwargs['pixel'][0], :, :]
def get_size(self):
# return map size
return [self._nx]
def get_spectrum(self, **kwargs):
# if no pixel is given, return the focused pixel's spectrum
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._spectra[self._focus[0]]
else:
return self._spectra[kwargs['pixel'][0], :, :]
def set_fit(self, fit_functions, fit_initial_parameters, fit_optimized_parameters, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
else:
px = kwargs['pixel'][0]
# clear the old fit data
self._fit_functions[px, :] = numpy.zeros(6)
self._fit_initial_parameters[px, :, :] = numpy.NAN
self._fit_optimized_parameters[px, :, :] = numpy.NAN
# set new fit data
i_parameter = 0
for i_peak in range(len(fit_functions)):
if fit_functions[i_peak] == 1:
self._fit_functions[px, i_peak] = 1
self._fit_initial_parameters[px, i_peak, :3] = fit_initial_parameters[i_parameter:i_parameter+3]
self._fit_initial_parameters[px, i_peak, 3] = 0
self._fit_optimized_parameters[px, i_peak, :3] = fit_optimized_parameters[i_parameter:i_parameter+3]
self._fit_optimized_parameters[px, i_peak, 3] = 0
i_parameter += 3
elif fit_functions[i_peak] == 2:
self._fit_functions[px, i_peak] = 2
self._fit_initial_parameters[px, i_peak, :3] = fit_initial_parameters[i_parameter:i_parameter+3]
self._fit_initial_parameters[px, i_peak, 3] = 0
self._fit_optimized_parameters[px, i_peak, :3] = fit_optimized_parameters[i_parameter:i_parameter+3]
self._fit_optimized_parameters[px, i_peak, 3] = 0
i_parameter += 3
elif fit_functions[i_peak] == 3:
self._fit_functions[px, i_peak] = 3
self._fit_initial_parameters[px, i_peak, :] = fit_initial_parameters[i_parameter:i_parameter+4]
self._fit_optimized_parameters[px, i_peak, :] = fit_optimized_parameters[i_parameter:i_parameter+4]
i_parameter += 4
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
def set_focus(self, focus):
# set focus if it is within the map
if 0 <= numpy.round(focus[0]) < self._nx:
self._focus = [int(numpy.round(focus[0]))]
# emit signal
self._app.focus_changed.emit(self._id)
def set_interval(self, side, value):
# check if a good interval has been given
if side == 'left' and value < 0:
return
if side == 'right' and value >= self._resolution:
return
if side == 'left' and self._interval[1] <= value:
return
if side == 'right' and self._interval[0] >= value:
return
# set interval
if side == 'left':
self._interval[0] = value
elif side == 'right':
self._interval[1] = value
# recalculate intensities
self._int_counts = numpy.sum(self._spectra[:, self._interval[0]:self._interval[1], 1], axis=1)
self._mean_energies = numpy.sum(self._spectra[:, self._interval[0]:self._interval[1], 0]*self._spectra[:, self._interval[0]:self._interval[1], 1], axis=1)/self._int_counts
max_pixels = numpy.argmax(self._spectra[:, self._interval[0]:self._interval[1], 1], axis=1)
for ix in range(self._nx):
self._max_energies[ix] = self._spectra[ix, max_pixels[ix], 0]
# emit signal
self._app.interval_changed.emit(self._id)
def set_spectrum(self, spectrum, **kwargs):
# if no pixel was provided update the focused pixel
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
else:
px = kwargs['pixel'][0]
# update spectrum
self._spectra[px, :, :] = spectrum
self._int_counts[px] = numpy.sum(self._spectra[px, self._interval[0]:self._interval[1], 1])
self._mean_energies[px] = numpy.sum(self._spectra[px, self._interval[0]:self._interval[1], 0]*self._spectra[px, self._interval[0]:self._interval[1], 1])/self._int_counts[px]
max_pixel = numpy.argmax(self._spectra[px, self._interval[0]:self._interval[1], 1])
self._max_energies[px] = self._spectra[px, self._interval[0]+max_pixel, 0]
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.spectrum_changed.emit(self._id)
class Map2D(Map):
"""
Map2D
Class for two-dimensional maps which are mainly spatial maps.
"""
def __init__(self, map_id, file_name):
# call super init
super(Map2D, self).__init__()
# set dimension of the map
self._dimension = 2
# set the map id
self._id = map_id
# check for the file type of the map
# .dat files are acquired in the PGI9 (FZJ) lab using QTLab
if file_name[-4:] == '.dat':
# define map loader
map_loader = QtLab2D(file_name)
# .txt files are acquired by the Horiba machine in the Heinz Group at Stanford
elif file_name[-4:] == '.pck':
# define map loader
map_loader = QCoDeS2D(file_name)
# .txt files are acquired by the Horiba machine in the Heinz Group at Stanford
elif file_name[-4:] == '.txt':
# define map loader
map_loader = Horiba2D(file_name)
# .dat2 files are acquired in the J. Vuckovic group at Stanford
elif file_name[-5:] == '.dat2':
# define map loader
map_loader = Vuckovic2D(file_name)
# load data
self._map_name, self._spectra, self._data_names, self._data = map_loader.load_data()
# set map size
self._nx = self._spectra.shape[0]
self._ny = self._spectra.shape[1]
# set spectral resolution
self._resolution = self._spectra.shape[2]
# set initial interval for the integration of the spectra
self._interval = [0, self._resolution-1]
# get integrated counts, average energy and maximum energy
self._int_counts = numpy.sum(self._spectra[:, :, :, 1], axis=2)
self._mean_energies = numpy.sum(self._spectra[:, :, :, 0]*self._spectra[:, :, :, 1], axis=2)/self._int_counts
max_pixels = numpy.argmax(self._spectra[:, :, :, 1], axis=2)
self._max_energies = numpy.zeros((self._nx, self._ny))
for ix in range(self._nx):
for iy in range(self._ny):
self._max_energies[ix, iy] = self._spectra[ix, iy, max_pixels[ix, iy], 0]
# create variables for the fit data
self._fit_functions = numpy.zeros((self._nx, self._ny, 6))
self._fit_initial_parameters = numpy.zeros((self._nx, self._ny, 6, 4))
self._fit_initial_parameters[:, :, :, :] = numpy.NAN
self._fit_optimized_parameters = numpy.zeros((self._nx, self._ny, 6, 4))
self._fit_optimized_parameters[:, :, :, :] = numpy.NAN
# set focus to the center of the map
self._focus = [int(self._nx / 2), int(self._ny / 2)]
# dictionary for micrographs
self._micrographs = {}
self._micrograph_names = {}
# set selected data to integral
self._selected_data = 1
def add_micrograph(self, file_name, micrograph):
# obtain maximum key so far
if len(self._micrographs) == 0:
max_key = -1
else:
max_key = max(self._micrographs.keys())
# add micrograph and micrograph name
self._micrographs[int(max_key) + 1] = micrograph
self._micrograph_names[int(max_key) + 1] = file_name
# return the data id of the new micrograph
return 5 + len(self._data_names) + int(max_key)
def clear_fit(self, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
py = self._focus[1]
else:
px = kwargs['pixel'][0]
py = kwargs['pixel'][1]
# clear fit
self._fit_functions[px, py, :] = numpy.zeros(6)
self._fit_initial_parameters[px, py, :, :] = numpy.NAN
self._fit_optimized_parameters[px, py, :, :] = numpy.NAN
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
def flip(self, direction):
# flip the map horizontally
if direction == 'horizontally':
# flip data
self._data = numpy.flip(self._data, 1)
# flip spectra
self._spectra = numpy.flip(self._spectra, 0)
# flip data derived from spectra
self._int_counts = numpy.flip(self._int_counts, 0)
self._mean_energies = numpy.flip(self._mean_energies, 0)
self._max_energies = numpy.flip(self._max_energies, 0)
# flip micrographs
for i_micrograph in range(len(self._micrographs)):
self._micrographs[i_micrograph] = numpy.flip(self._micrographs[i_micrograph], 1)
# flip fit data
self._fit_functions = numpy.flip(self._fit_functions, 0)
self._fit_initial_parameters = numpy.flip(self._fit_initial_parameters, 0)
self._fit_optimized_parameters = numpy.flip(self._fit_optimized_parameters, 0)
# update focus
self._focus[0] = self._nx - self._focus[0]
# emit signal
self._app.geometry_changed.emit(self._id)
# flip the map vertically
elif direction == 'vertically':
# flip data
self._data = numpy.flip(self._data, 2)
# flip spectra
self._spectra = numpy.flip(self._spectra, 1)
# flip data derived from spectra
self._int_counts = numpy.flip(self._int_counts, 1)
self._mean_energies = numpy.flip(self._mean_energies, 1)
self._max_energies = numpy.flip(self._max_energies, 1)
# flip micrographs
for i_micrograph in range(len(self._micrographs)):
self._micrographs[i_micrograph] = numpy.flip(self._micrographs[i_micrograph], 0)
# flip fit data
self._fit_functions = numpy.flip(self._fit_functions, 1)
self._fit_initial_parameters = numpy.flip(self._fit_initial_parameters, 1)
self._fit_optimized_parameters = numpy.flip(self._fit_optimized_parameters, 1)
# update focus
self._focus[1] = self._ny - self._focus[1]
# emit signal
self._app.geometry_changed.emit(self._id)
def get_data(self, **kwargs):
# if no data index is given, return the currently selected data
if 'data_index' in kwargs.keys():
data_index = kwargs['data_index']
else:
data_index = self._selected_data
# return a data
if data_index == 1:
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._int_counts
elif kwargs['pixel'] == -2:
return self._int_counts[self._focus[0], self._focus[1]]
else:
return self._int_counts[kwargs['pixel'][0], kwargs['pixel'][1]]
elif data_index == 2:
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._mean_energies
elif kwargs['pixel'] == -2:
return self._mean_energies[self._focus[0], self._focus[1]]
else:
return self._mean_energies[kwargs['pixel'][0], kwargs['pixel'][1]]
elif data_index == 3:
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._max_energies
elif kwargs['pixel'] == -2:
return self._max_energies[self._focus[0], self._focus[1]]
else:
return self._max_energies[kwargs['pixel'][0], kwargs['pixel'][1]]
elif 3 < data_index < 4 + len(self._data):
data_index -= 4
# check if the whole map data (pixel = -1) or the focussed pixel (pixel = -2)
# or a specific pixel (pixel = [x,y]) are requested
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
# return whole map data
return self._data[data_index]
elif kwargs['pixel'] == -2:
# return data at focused pixel
return self._data[data_index][self._focus[0], self._focus[1]]
else:
# return data at requested pixel
return self._data[data_index][kwargs['pixel'][0], kwargs['pixel'][1]]
# return a micrograph
elif 3 + len(self._data) < data_index < 4 + len(self._data) + len(self._micrographs):
# return micrograph
data_index -= 4 + len(self._data)
return self._micrographs[data_index]
# return a fit data
else:
# check which fit parameters are there
data_index -= 4 + len(self._data) + len(self._micrographs)
# get fit functions and fit parameters once
fit_functions = self._fit_functions
fit_optimized_parameters = self._fit_optimized_parameters
parameters = []
for i_peak in range(6):
if numpy.sum(numpy.int_(fit_functions[:, :, i_peak] > 0)) > 0:
parameters.append([i_peak, 0])
parameters.append([i_peak, 1])
if numpy.sum(numpy.int_(fit_functions[:, :, i_peak] == 2)) > 0 or \
numpy.sum(numpy.int_(fit_functions[:, :, i_peak] == 3)) > 0:
parameters.append([i_peak, 2])
if numpy.sum(numpy.int_(fit_functions[:, :, i_peak] == 1)) > 0 or \
numpy.sum(numpy.int_(fit_functions[:, :, i_peak] == 3)) > 0:
parameters.append([i_peak, 3])
if numpy.sum(numpy.int_(fit_functions[:, :, i_peak] > 0)) > 0:
parameters.append([i_peak, 4])
# return intensities
if parameters[data_index][1] == 0:
return fit_optimized_parameters[:, :, parameters[data_index][0], 0]
# return central energies
elif parameters[data_index][1] == 1:
return fit_optimized_parameters[:, :, parameters[data_index][0], 1]
# return sigma
elif parameters[data_index][1] == 2:
sigma = numpy.zeros((self._nx, self._ny))
sigma[:] = numpy.NAN
sigma_from_gaussian = fit_optimized_parameters[:, :, parameters[data_index][0], 2][fit_functions[:, :, parameters[data_index][0]] == 2]
sigma_from_voigt = fit_optimized_parameters[:, :, parameters[data_index][0], 2][fit_functions[:, :, parameters[data_index][0]] == 3]
sigma[fit_functions[:, :, parameters[data_index][0]] == 2] = sigma_from_gaussian
sigma[fit_functions[:, :, parameters[data_index][0]] == 3] = sigma_from_voigt
return 1000*sigma
# return gamma
elif parameters[data_index][1] == 3:
gamma = numpy.zeros((self._nx, self._ny))
gamma[:] = numpy.NAN
gamma_from_lorentzian = fit_optimized_parameters[:, :, parameters[data_index][0], 2][fit_functions[:, :, parameters[data_index][0]] == 1]
gamma_from_voigt = fit_optimized_parameters[:, :, parameters[data_index][0], 3][fit_functions[:, :, parameters[data_index][0]] == 3]
gamma[fit_functions[:, :, parameters[data_index][0]] == 1] = gamma_from_lorentzian
gamma[fit_functions[:, :, parameters[data_index][0]] == 3] = gamma_from_voigt
return 1000*gamma
# return FWHM
elif parameters[data_index][1] == 4:
fwhm = numpy.zeros((self._nx, self._ny))
fwhm[:] = numpy.NAN
sigma_from_gaussian = fit_optimized_parameters[:, :, parameters[data_index][0], 2][fit_functions[:, :, parameters[data_index][0]] == 2]
gamma_from_lorentzian = fit_optimized_parameters[:, :, parameters[data_index][0], 2][fit_functions[:, :, parameters[data_index][0]] == 1]
gamma_from_voigt = fit_optimized_parameters[:, :, parameters[data_index][0], 3][fit_functions[:, :, parameters[data_index][0]] == 3]
sigma_from_voigt = fit_optimized_parameters[:, :, parameters[data_index][0], 2][fit_functions[:, :, parameters[data_index][0]] == 3]
fwhm[fit_functions[:, :, parameters[data_index][0]] == 1] = 2*gamma_from_lorentzian
fwhm[fit_functions[:, :, parameters[data_index][0]] == 2] = 2.35482*sigma_from_gaussian
fwhm[fit_functions[:, :, parameters[data_index][0]] == 3] = 0.5346*2.*gamma_from_voigt+numpy.sqrt(0.2166*4.*gamma_from_voigt**2.+2.35482**2.*sigma_from_voigt**2.)
return 1000*fwhm
def get_data_name(self, **kwargs):
# if no data index is given, return the currently selected data name
if 'data_index' in kwargs.keys():
data_index = kwargs['data_index']
else:
data_index = self._selected_data
if data_index == 1:
return 'spectra --integral'
elif data_index == 2:
return 'spectra --mean'
elif data_index == 3:
return 'spectra --maximum'
elif 3 < data_index < 4 + len(self._data):
data_index -= 4
return self._data_names[data_index]
elif 3 + len(self._data) < data_index < 4 + len(self._data) + len(self._micrographs):
data_index -= 4 + len(self._data)
return self._micrograph_names[data_index]
else:
# check which fit parameters are there
data_index -= 4 + len(self._data) + len(self._micrographs)
parameters = []
subscripts = [u'\u2081', u'\u2082', u'\u2083', u'\u2084', u'\u2085', u'\u2086']
for i_peak in range(6):
if numpy.sum(numpy.int_(self._fit_functions[:, :, i_peak] > 0)) > 0:
parameters.append('I'+subscripts[i_peak])
parameters.append('ε'+subscripts[i_peak])
if numpy.sum(numpy.int_(self._fit_functions[:, :, i_peak] == 1)) > 0 or \
numpy.sum(numpy.int_(self._fit_functions[:, :, i_peak] == 3)) > 0:
parameters.append('σ'+subscripts[i_peak])
if numpy.sum(numpy.int_(self._fit_functions[:, :, i_peak] == 2)) > 0 or \
numpy.sum(numpy.int_(self._fit_functions[:, :, i_peak] == 3)) > 0:
parameters.append('γ'+subscripts[i_peak])
parameters.append('FWHM'+subscripts[i_peak])
return parameters[data_index]
def get_fit(self, **kwargs):
# if no pixel was provided the current pixel is returned
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
py = self._focus[1]
else:
px = kwargs['pixel'][0]
py = kwargs['pixel'][1]
return self._fit_functions[px, py, :], self._fit_initial_parameters[px, py, :, :], self._fit_optimized_parameters[px, py, :, :]
def get_fit_functions(self, **kwargs):
# if no pixel was provided return the whole fit functions array
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._fit_functions[:, :, :]
# if pixel is set to -2 return the fit functions for the focused pixel
elif kwargs['pixel'] == -2:
return self._fit_functions[self._focus[0], self._focus[1], :]
# return the fit functions for the desired pixel
else:
return self._fit_functions[kwargs['pixel'][0], kwargs['pixel'][1], :]
def get_fit_parameters(self, **kwargs):
# if no pixel was provided return the whole fit parameter array
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._fit_optimized_parameters[:, :, :, :]
# if pixel is set to -2 return the fit parameters for the focused pixel
elif kwargs['pixel'] == -2:
return self._fit_optimized_parameters[self._focus[0], self._focus[1], :, :]
# return the fit parameters for the desired pixel
else:
return self._fit_optimized_parameters[kwargs['pixel'][0], kwargs['pixel'][1], :, :]
def get_micrographs(self):
# return micrographs
return self._micrographs
def get_micrograph_names(self):
# return micrograph names
return self._micrograph_names
def get_size(self):
# return map size
return [self._nx, self._ny]
def get_spectrum(self, **kwargs):
# if no pixel is given, return the focused pixel's spectrum
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
return self._spectra[self._focus[0], self._focus[1]]
else:
return self._spectra[kwargs['pixel'][0], kwargs['pixel'][1], :, :]
def rotate(self, direction):
if direction == 'clockwise':
# rotate data
self._data = numpy.swapaxes(self._data, 1, 2)
self._data = numpy.flip(self._data, 2)
# rotate spectra
self._spectra = numpy.swapaxes(self._spectra, 0, 1)
self._spectra = numpy.flip(self._spectra, 1)
# rotate data derived from spectra
self._int_counts = numpy.swapaxes(self._int_counts, 0, 1)
self._int_counts = numpy.flip(self._int_counts, 1)
self._mean_energies = numpy.swapaxes(self._mean_energies, 0, 1)
self._mean_energies = numpy.flip(self._mean_energies, 1)
self._max_energies = numpy.swapaxes(self._max_energies, 0, 1)
self._max_energies = numpy.flip(self._max_energies, 1)
# rotate micrographs
for i_micrograph in range(len(self._micrographs)):
self._micrographs[i_micrograph] = numpy.swapaxes(self._micrographs[i_micrograph], 0, 1)
self._micrographs[i_micrograph] = numpy.flip(self._micrographs[i_micrograph], 0)
# rotate fit data
self._fit_functions = numpy.swapaxes(self._fit_functions, 0, 1)
self._fit_initial_parameters = numpy.swapaxes(self._fit_initial_parameters, 0, 1)
self._fit_optimized_parameters = numpy.swapaxes(self._fit_optimized_parameters, 0, 1)