-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageClass.py
144 lines (129 loc) · 5.03 KB
/
imageClass.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
from ncempy.io import dm
import numpy as np
import matplotlib.pyplot as plt
class Spectral_image():
def __init__(self, data, deltadeltaE, pixelsize=None, name=None):
self.data = data
self.ddeltaE = deltadeltaE
self.deltaE = self.determine_deltaE()
if pixelsize is not None:
self.pixelsize = pixelsize * 1E6
def determine_deltaE(self):
data_avg = np.average(self.data, axis=(0, 1))
ind_max = np.argmax(data_avg)
self.deltaE = np.linspace(-ind_max * self.ddeltaE, (self.l - ind_max - 1) * self.ddeltaE, self.l)
return self.deltaE
def plot_all(self, i, j, same_image=True, normalize=False, legend=False,
range_x=None, range_y=None, range_E=None, signal="EELS", log=False):
if range_x is None:
range_x = [0, self.image_shape[1]]
if range_y is None:
range_y = [0, self.image_shape[0]]
if same_image:
plt.figure()
plt.title("Spectrum image " + signal + " spectra")
plt.xlabel("[eV]")
if range_E is not None:
plt.xlim(range_E)
signal_pixel = self.get_pixel_signal(i, j, signal)
if normalize:
signal_pixel /= np.max(np.absolute(signal_pixel))
if log:
signal_pixel = np.log(signal_pixel)
plt.ylabel("log intensity")
plt.plot(self.deltaE, signal_pixel, label="[" + str(j) + "," + str(i) + "]")
plt.legend()
plt.show()
def get_pixel_signal(self, i, j, signal='EELS'):
"""
INPUT:
i: int, x-coordinate for the pixel
j: int, y-coordinate for the pixel
Keyword argument:
signal: str (default = 'EELS'), what signal is requested, should comply with defined names
OUTPUT:
signal: 1D numpy array, array with the requested signal from the requested pixel
"""
return self.data[i, j, :]
@property
def l(self):
"""returns length of spectra, i.e. num energy loss bins"""
return self.data.shape[2]
@property
def image_shape(self):
"""return 2D-shape of spectral image"""
return self.data.shape[:2]
@staticmethod
def get_prefix(unit, SIunit=None, numeric=True):
if SIunit is not None:
lenSI = len(SIunit)
if unit[-lenSI:] == SIunit:
prefix = unit[:-lenSI]
if len(prefix) == 0:
if numeric:
return 1
else:
return prefix
else:
print("provided unit not same as target unit: " + unit + ", and " + SIunit)
if numeric:
return 1
else:
return prefix
else:
prefix = unit[0]
if not numeric:
return prefix
if prefix == 'p':
return 1E-12
if prefix == 'n':
return 1E-9
if prefix in ['μ', 'µ', 'u', 'micron']:
return 1E-6
if prefix == 'm':
return 1E-3
if prefix == 'k':
return 1E3
if prefix == 'M':
return 1E6
if prefix == 'G':
return 1E9
if prefix == 'T':
return 1E12
else:
print("either no or unknown prefix in unit: " + unit + ", found prefix " + prefix + ", asuming no.")
return 1
@classmethod
def load_data(cls, path_to_dmfile, load_additional_data=False):
"""
INPUT:
path_to_dmfile: str, path to spectral image file (.dm3 or .dm4 extension)
OUTPUT:
image -- Spectral_image, object of Spectral_image class containing the data of the dm-file
"""
dmfile_tot = dm.fileDM(path_to_dmfile)
additional_data = []
for i in range(dmfile_tot.numObjects - dmfile_tot.thumbnail * 1):
dmfile = dmfile_tot.getDataset(i)
if dmfile['data'].ndim == 3:
dmfile = dmfile_tot.getDataset(i)
data = np.swapaxes(np.swapaxes(dmfile['data'], 0, 1), 1, 2)
if not load_additional_data:
break
elif load_additional_data:
additional_data.append(dmfile_tot.getDataset(i))
if i == dmfile_tot.numObjects - dmfile_tot.thumbnail * 1 - 1:
if (len(additional_data) == i + 1) or not load_additional_data:
print("No spectral image detected")
dmfile = dmfile_tot.getDataset(0)
data = dmfile['data']
ddeltaE = dmfile['pixelSize'][0]
pixelsize = np.array(dmfile['pixelSize'][1:])
energyUnit = dmfile['pixelUnit'][0]
ddeltaE *= cls.get_prefix(energyUnit, 'eV')
pixelUnit = dmfile['pixelUnit'][1]
pixelsize *= cls.get_prefix(pixelUnit, 'm')
image = cls(data, ddeltaE, pixelsize=pixelsize, name=path_to_dmfile[:-4])
if load_additional_data:
image.additional_data = additional_data
return image