-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrum.py
152 lines (109 loc) · 4.52 KB
/
spectrum.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 19 13:55:23 2016
@author: gbaechle
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
import color_tools as ct
class Spectrum(object):
def __init__(self, wave_lengths, intensities):
self.wave_lengths = wave_lengths
self.intensities = intensities
def show(self, ax=None, title='', sqrt=False, y_max=0.):
if sqrt:
intensity = np.sqrt(np.abs(self.intensities))
y_max = np.sqrt(y_max)
else:
intensity = self.intensities
if ax is None:
plt.figure()
ax = plt.gca()
ax.clear()
# sort the wavelengths in order
idx = np.argsort(self.wave_lengths)
wl = self.wave_lengths[idx]
intensity = intensity[idx]
# select only the visible spectrum
idx2 = np.where((wl >= 390E-9) & (wl <= 700E-9))
wl = wl[idx2]
intensity = intensity[idx2]
l = len(wl)
colors = plt.cm.Spectral_r(np.linspace(0, 1, l))
cs = [colors[i] for i in range(l)]
ax.scatter(wl, intensity, color=cs)
ax.plot(wl, intensity, '--k', linewidth=1.0, zorder=-1, alpha=0.5)
ax.set_xlim([np.min(wl), np.max(wl)])
if y_max != 0.:
ax.set_ylim([0., y_max])
ax.set_xlabel('Wavelength (m)')
ax.set_title(title)
class Spectrum3D(object):
def __init__(self, wave_lengths, intensities):
self.wave_lengths = wave_lengths
self.intensities = intensities
self.xyz_colors = None
self.rgb_colors = None
def show(self, x=0, y=0, title='', sqrt=False):
if sqrt:
intensity = np.sqrt(np.abs(self.intensities[x, y, :]))
else:
intensity = self.intensities[x, y, :]
plt.figure()
idx = np.argsort(self.wave_lengths)
wl = self.wave_lengths[idx]
intensity = intensity[idx]
l = len(self.wave_lengths)
colors = plt.cm.Spectral_r(np.linspace(0, 1, l))
cs = [colors[i] for i in range(l)]
plt.scatter(wl, intensity, color=cs)
plt.plot(wl, intensity, '--k', linewidth=1.0, zorder=-1, alpha=0.5)
plt.gca().set_xlim([np.min(wl), np.max(wl)])
plt.gca().set_xlabel('Wavelength (m)')
plt.title(title)
plt.show()
def compute_xyz(self, sqrt=False):
if self.xyz_colors is None:
idx = np.argsort(self.wave_lengths)
if sqrt:
self.xyz_colors = ct.from_spectrum_to_xyz(self.wave_lengths[idx], np.sqrt(self.intensities[:, :, idx]),
integrate_nu=False)
else:
self.xyz_colors = ct.from_spectrum_to_xyz(self.wave_lengths[idx], self.intensities[:, :, idx],
integrate_nu=False)
return self.xyz_colors
def compute_rgb(self, sqrt=False):
if self.rgb_colors is None:
if self.xyz_colors is None:
self.compute_xyz(sqrt)
self.rgb_colors = ct.from_xyz_to_rgb(self.xyz_colors)
return self.rgb_colors
def blue_shift(self, factor, extrapolation='zero'):
f = interp1d(self.wave_lengths / factor, self.intensities, axis=2, kind='cubic', bounds_error=False,
fill_value=0.)
# f = interp1d(self.wave_lengths*np.cos(factor), self.intensities, axis=2, kind='cubic', bounds_error=False, fill_value=0.)
max_vals = self.intensities[:, :, 0]
self.intensities = np.maximum(f(self.wave_lengths), 0.)
max_wavelength = np.max(self.wave_lengths)
if extrapolation == 'cste':
idx = np.where(self.wave_lengths * factor > max_wavelength)[0]
self.intensities[:, :, idx] = max_vals[:, :, np.newaxis]
def __setitem__(self, key, value):
self.intensities[:, :, key] = value
def __getitem__(self, key):
return self.intensities[:, :, key]
def get_spectrum(self, x, y):
return Spectrum(self.wave_lengths, self.intensities[x, y, :])
def set_spectrum(self, x, y, value):
self.intensities[x, y, :] = value
class Scale(object):
def __init__(self, depths, intensities):
self.depths = depths
self.intensities = intensities
def show(self, title=''):
plt.figure()
plt.plot(self.depths, self.intensities)
plt.gca().set_xlim([np.min(self.depths), np.max(self.depths)])
plt.gca().set_xlabel('Depth (m)')
plt.title(title)