Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pes_to_spec
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Machine Learning projects.
pes_to_spec
Commits
201a1568
Commit
201a1568
authored
2 years ago
by
Danilo Ferreira de Lima
Browse files
Options
Downloads
Patches
Plain Diff
Corrected smoothing normalization to always match the original normalization.
parent
10406ed5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pes_to_spec/model.py
+5
-5
5 additions, 5 deletions
pes_to_spec/model.py
scripts/test_analysis.py
+10
-5
10 additions, 5 deletions
scripts/test_analysis.py
with
15 additions
and
10 deletions
pes_to_spec/model.py
+
5
−
5
View file @
201a1568
...
@@ -4,6 +4,7 @@ from autograd import grad
...
@@ -4,6 +4,7 @@ from autograd import grad
import
joblib
import
joblib
import
h5py
import
h5py
from
scipy.signal
import
fftconvolve
from
scipy.signal
import
fftconvolve
from
scipy.signal.windows
import
gaussian
as
gaussian_window
from
scipy.optimize
import
fmin_l_bfgs_b
from
scipy.optimize
import
fmin_l_bfgs_b
from
sklearn.decomposition
import
PCA
from
sklearn.decomposition
import
PCA
from
sklearn.model_selection
import
train_test_split
from
sklearn.model_selection
import
train_test_split
...
@@ -96,11 +97,10 @@ class Model(object):
...
@@ -96,11 +97,10 @@ class Model(object):
"""
"""
# Apply smoothing
# Apply smoothing
n_features
=
high_res_data
.
shape
[
1
]
n_features
=
high_res_data
.
shape
[
1
]
mu
=
high_res_photon_energy
[
0
,
n_features
//
2
]
mu
=
high_res_photon_energy
[:,
n_features
//
2
,
np
.
newaxis
]
gaussian
=
np
.
exp
(
-
((
high_res_photon_energy
-
mu
)
/
self
.
high_res_sigma
)
**
2
/
2
)
/
np
.
sqrt
(
2
*
np
.
pi
*
self
.
high_res_sigma
**
2
)
gaussian
=
np
.
exp
(
-
0.5
*
(
high_res_photon_energy
-
mu
)
**
2
/
self
.
high_res_sigma
**
2
)
print
(
np
.
sum
(
gaussian
))
gaussian
/=
np
.
sum
(
gaussian
,
axis
=
1
,
keepdims
=
True
)
# 80 to match normalization (empirically taken)
high_res_gc
=
fftconvolve
(
high_res_data
,
gaussian
,
mode
=
"
same
"
,
axes
=
1
)
high_res_gc
=
fftconvolve
(
high_res_data
,
gaussian
,
mode
=
"
same
"
,
axes
=
1
)
/
80.0
return
high_res_gc
return
high_res_gc
def
fit
(
self
,
low_res_data
:
Dict
[
str
,
np
.
ndarray
],
high_res_data
:
np
.
ndarray
,
high_res_photon_energy
:
np
.
ndarray
)
->
np
.
ndarray
:
def
fit
(
self
,
low_res_data
:
Dict
[
str
,
np
.
ndarray
],
high_res_data
:
np
.
ndarray
,
high_res_photon_energy
:
np
.
ndarray
)
->
np
.
ndarray
:
...
...
This diff is collapsed.
Click to expand it.
scripts/test_analysis.py
+
10
−
5
View file @
201a1568
...
@@ -15,25 +15,30 @@ matplotlib.use('Agg')
...
@@ -15,25 +15,30 @@ matplotlib.use('Agg')
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
from
matplotlib.gridspec
import
GridSpec
from
matplotlib.gridspec
import
GridSpec
def
plot_result
(
filename
:
str
,
spec_pred
:
np
.
ndarray
,
spec_raw_int
:
np
.
ndarray
,
spec_raw_pe
:
np
.
ndarray
):
from
typing
import
Optional
def
plot_result
(
filename
:
str
,
spec_pred
:
np
.
ndarray
,
spec_smooth
:
np
.
ndarray
,
spec_raw_pe
:
np
.
ndarray
,
spec_raw_int
:
Optional
[
np
.
ndarray
]
=
None
):
"""
"""
Plot result with uncertainty band.
Plot result with uncertainty band.
Args:
Args:
filename: Output file name.
filename: Output file name.
spec_pred: Predicted result with uncertainty bands in a shape of (3, features).
spec_pred: Predicted result with uncertainty bands in a shape of (3, features).
spec_
raw_int: True
expected result with shape (features,).
spec_
smooth: Smoothened
expected result with shape (features,).
spec_raw_pe: x axis with the photon energy in eV.
spec_raw_pe: x axis with the photon energy in eV.
spec_raw_int: Original true expected result with shape (features,).
"""
"""
fig
=
plt
.
figure
(
figsize
=
(
1
0
,
10
))
fig
=
plt
.
figure
(
figsize
=
(
1
6
,
8
))
gs
=
GridSpec
(
1
,
1
)
gs
=
GridSpec
(
1
,
1
)
ax
=
fig
.
add_subplot
(
gs
[
0
,
0
])
ax
=
fig
.
add_subplot
(
gs
[
0
,
0
])
eps
=
np
.
mean
(
spec_pred
[:,
1
])
eps
=
np
.
mean
(
spec_pred
[:,
1
])
ax
.
plot
(
spec_raw_pe
,
spec_
raw_int
,
c
=
'
b
'
,
lw
=
3
,
label
=
"
High resolution measurement (smoothened)
"
)
ax
.
plot
(
spec_raw_pe
,
spec_
smooth
,
c
=
'
b
'
,
lw
=
3
,
label
=
"
High resolution measurement (smoothened)
"
)
ax
.
plot
(
spec_raw_pe
,
spec_pred
[:,
0
],
c
=
'
r
'
,
lw
=
3
,
label
=
"
High resolution prediction
"
)
ax
.
plot
(
spec_raw_pe
,
spec_pred
[:,
0
],
c
=
'
r
'
,
lw
=
3
,
label
=
"
High resolution prediction
"
)
ax
.
fill_between
(
spec_raw_pe
,
spec_pred
[:,
0
]
-
spec_pred
[:,
1
],
spec_pred
[:,
0
]
+
spec_pred
[:,
1
],
facecolor
=
'
red
'
,
alpha
=
0.6
,
label
=
"
68% unc. (stat.)
"
)
ax
.
fill_between
(
spec_raw_pe
,
spec_pred
[:,
0
]
-
spec_pred
[:,
1
],
spec_pred
[:,
0
]
+
spec_pred
[:,
1
],
facecolor
=
'
red
'
,
alpha
=
0.6
,
label
=
"
68% unc. (stat.)
"
)
ax
.
fill_between
(
spec_raw_pe
,
spec_pred
[:,
0
]
-
spec_pred
[:,
2
],
spec_pred
[:,
0
]
+
spec_pred
[:,
2
],
facecolor
=
'
magenta
'
,
alpha
=
0.6
,
label
=
"
68% unc. (syst., PCA)
"
)
ax
.
fill_between
(
spec_raw_pe
,
spec_pred
[:,
0
]
-
spec_pred
[:,
2
],
spec_pred
[:,
0
]
+
spec_pred
[:,
2
],
facecolor
=
'
magenta
'
,
alpha
=
0.6
,
label
=
"
68% unc. (syst., PCA)
"
)
if
spec_raw_int
is
not
None
:
ax
.
plot
(
spec_raw_pe
,
spec_raw_int
,
c
=
'
b
'
,
lw
=
1
,
ls
=
'
--
'
,
label
=
"
High resolution measurement
"
)
ax
.
legend
()
ax
.
legend
()
ax
.
set
(
title
=
f
"
avg(unc) =
{
eps
}
"
,
ax
.
set
(
title
=
f
"
avg(unc) =
{
eps
}
"
,
xlabel
=
"
Photon energy [eV]
"
,
xlabel
=
"
Photon energy [eV]
"
,
...
@@ -87,7 +92,7 @@ def main():
...
@@ -87,7 +92,7 @@ def main():
# plot
# plot
for
tid
in
test_tids
:
for
tid
in
test_tids
:
idx
=
np
.
where
(
tid
==
tids
)[
0
][
0
]
idx
=
np
.
where
(
tid
==
tids
)[
0
][
0
]
plot_result
(
f
"
test_
{
tid
}
.png
"
,
spec_pred
[
idx
,
:,
:],
spec_smooth
[
idx
,
:],
spec_raw_pe
[
idx
,
:])
plot_result
(
f
"
test_
{
tid
}
.png
"
,
spec_pred
[
idx
,
:,
:],
spec_smooth
[
idx
,
:],
spec_raw_pe
[
idx
,
:],
spec_raw_int
[
idx
,
:])
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
main
()
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment