forked from shcrela/frejus21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate.py
More file actions
executable file
·213 lines (185 loc) · 7.3 KB
/
simulate.py
File metadata and controls
executable file
·213 lines (185 loc) · 7.3 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 21:34:56 2021
@author: dejan
"""
import numpy as np
from skimage import transform, io
def pV(x: np.ndarray,
h: float, x0: float = None, w: float = None, factor: float = 0.5):
"""Create a pseudo-Voigt profile.
Parameters
----------
x : 1D ndarray
Independent variable (Raman shift for ex.)
h : float
The height of the peak
x0 : float
The position of the peak on the x-axis.
Default value is at the middle of the x
w : float
FWHM - The width
Default value is 1/3 of the x
factor : float
The ratio of Gauss vs Lorentz in the peak
Default value is 0.5
Returns
-------
y : np.ndarray :
1D-array of the same length as the input x-array
***************************
Example
-------
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(150, 1300, 1015)
>>> plt.plot(x, pV(x, 200))
"""
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))
if x0 is None:
x0 = x[int(len(x)/2)]
if w is None:
w = (x.max() - x.min()) / 3
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, peak_function=pV):
"""Create the spectra as the sum of the pseudo-Voigt peaks.
You need to provide the independent variable `x`
and a set of parameters for each peak.
(one sublist for each Pseudo-Voigt peak).
Parameters
----------
x : np.ndarray
1D ndarray - independent variable.
*params : list[list[float]]
The list of lists containing the peak parameters. For each infividual
peak to be created there should be a sublist of parameters to be
passed to the pV function. So that `params` list finally contains
one of these sublists for each Pseudo-Voigt peak to be created.
Look in the docstring of pV function for more info
on what to put in each sublist.
(h, x0, w, G/L) - only the first parameter is mandatory.
Returns
-------
y : np.ndarray
1D ndarray of the same length as the input x-array
Example
-------
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(150, 1300, 1015) # Create 1015 equally spaced points
>>> mpar = [[40, 220, 100], [122, 440, 80], [164, 550, 160], [40, 480, 340]]
>>> plt.plot(x, multi_pV(x, mpar))
"""
result = np.zeros_like(x, dtype=np.float)
for pp in params:
result += peak_function(x, *pp) # h, x0, w, r
return result
def create_multiple_spectra(x: np.ndarray, initial_peak_params: list,
defaults=None, N=10000, noise: float = 0.02,
spectrum_function=multi_pV,
noise_bias='linea', funny_peak='random'):
"""Create N different spectra using mutli_pV function.
Parameters
----------
x : np.ndarray
1D ndarray - independent variable
initial_peak_params: list[float]
The list of sublists containing individual peak parameters as
demanded by the `spectrum_function`.
defaults: list, optional
Default params for the sublists where not all params are set.
The function will try to come up with something if the defaults
are not provided.
N : int, optional
The number of spectra to create. Defaults to 1024 (32x32 :)
noise : float
Noisiness and how much you want the spectra to differ between them.
spectrum_function : function
The default is multi_pV.
You should be able to provide something else, but this is not yet
tested.
noise_bias: None or 'smiley' or 'linea'
Default is 'linea'.
The underlaying pattern of the differences between the spectra.
funny_peak: int or list of ints or 'random' or 'all'
Only applicable if `noise_bias` is 'smiley'.
Designates the peak on which you want the bias to appear.
If 'random', one peak is chosen randomly.
Returns
-------
y : np.ndarray :
2D ndarray of the shape (N, len(x))
Example
-------
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(150, 1300, 1015) # Create 1015 equally spaced points
>>> mpar = [[40, 220, 100], [122, 440, 80], [164, 550, 160], [40, 480, 340]]
>>> my_spectra = create_multiple_spectra(x, mpar)
"""
def binarization_load(f, shape=(132, 132)):
"""May be used if "linea" mode is active."""
im = io.imread(f, as_gray=True)
return transform.resize(im, shape, anti_aliasing=True)
# We need to make sure that all the sublists are of the same length.
# If that is not the case, we need to fill the sublists with the default
# values.
if defaults is None:
defaults = [np.median([pp[0] for pp in initial_peak_params]),
np.median(x),
np.ptp(x)/20,
0.5]
for i, par in enumerate(initial_peak_params):
while len(par) < len(defaults):
protektor = np.copy(len(par))
initial_peak_params[i].append(defaults[len(par)])
if len(par) == protektor:
print("!!!!!!!!!!!!!!!!!!!!!")
n_peaks = len(initial_peak_params) # Number of peaks
ponderation = 1 + (np.random.rand(N, n_peaks, 1) - 0.5) * noise
peaks_params = ponderation * np.asarray(initial_peak_params)
# -------- The funny part ----------------------------------
if noise_bias == 'smiley':
smile = io.imread('./misc/bibi.jpg')
x_dim = int(np.sqrt(N))
y_dim = N//x_dim
if x_dim*y_dim != N:
print(f"You'll end up with {x_dim}*{y_dim} = {x_dim*y_dim} points "
f"instead of initial {N}")
N = x_dim * y_dim
smile_resized = transform.resize(smile, (x_dim, y_dim))
noise_bias = smile_resized.ravel()
if funny_peak == 'random':
funny_peak = np.random.randint(0, n_peaks)
elif funny_peak == 'all':
funny_peak = list(range(n_peaks))
peaks_params[:, funny_peak, 0] *= noise_bias
elif noise_bias == 'linea':
x_dim = int(np.sqrt(N))
y_dim = N//x_dim
images = './misc/linea/*.jpg'
coll_all = io.ImageCollection(images, load_func=binarization_load,
shape=(x_dim, y_dim))
print(f"You'll end up with {x_dim}*{y_dim} = {x_dim*y_dim} points"
f"instead of initial {N}")
N = x_dim * y_dim
# -------- The End of the funny part ------------------------
additive_noise = peaks_params[:, :, 0].mean() *\
(0.5+np.random.rand(len(x))) / 5
spectra = np.asarray(
[multi_pV(x, peaks_params[i]) +
additive_noise[np.random.permutation(len(x))]
for i in range(N)])
if isinstance(noise_bias, str) and noise_bias == 'linea':
noise_bias = coll_all.concatenate().reshape(110, -1)
spectra[:, -110:] *= noise_bias.T
return spectra.reshape(N, -1)