Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
ToolBox
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
Model registry
Operate
Environments
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
SCS
ToolBox
Commits
71e16cc5
Commit
71e16cc5
authored
2 years ago
by
Johannes Niskanen
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit with alignment function draft
parent
4f96b0f8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!198
Alignment of spectra
,
!188
P2866 updates
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/toolbox_scs/detectors/hrixs.py
+97
-0
97 additions, 0 deletions
src/toolbox_scs/detectors/hrixs.py
with
97 additions
and
0 deletions
src/toolbox_scs/detectors/hrixs.py
+
97
−
0
View file @
71e16cc5
...
@@ -491,6 +491,103 @@ class hRIXS:
...
@@ -491,6 +491,103 @@ class hRIXS:
#**********************************************
#**********************************************
data
=
data
.
assign
(
spectrum
=
((
"
trainId
"
,
"
energy
"
),
ret
))
data
=
data
.
assign
(
spectrum
=
((
"
trainId
"
,
"
energy
"
),
ret
))
return
data
return
data
def
align_readouts
(
self
,
data
,
method
,
start
,
stop
):
import
scipy
as
sp
from
scipy
import
optimize
#********************************************
# aligns spectra in a given data xarray
# METHOD
# -max_value
# -autocorrelation
# -gauss_fit
# start and stop are values of data.energy
# that define the range of these operations
# RETURNS MAXIMUM POSITIONS AND data with
# shifted spectra
#********************************************
searchinds
=
(
data
.
energy
>=
start
)
*
(
data
.
energy
<=
stop
)
peak_posis
=
[]
#********************************************
# Simple maximum alignment
#********************************************
if
method
.
lower
()
==
'
max_value
'
:
#********************************************
# Find the max for each of the spectra
#********************************************
for
spec
in
data
.
spectrum
:
x
=
data
.
energy
.
to_numpy
()[
searchinds
]
y
=
spec
.
to_numpy
()[
searchinds
]
maxipos
=
np
.
argmax
(
y
)
peak_posis
.
append
(
x
[
maxipos
])
#********************************************
# Alignment based on autocorrelation
# this is a relative alignment method
#********************************************
elif
method
.
lower
()
==
'
autocorrelation
'
:
#********************************************
# Find the max for each of the spectra
#********************************************
for
ind
,
spec
in
enumerate
(
data
.
spectrum
):
if
ind
==
0
:
x0
=
data
.
energy
.
to_numpy
()[
searchinds
]
y0
=
spec
.
to_numpy
()[
searchinds
]
maxipos0
=
np
.
argmax
(
spec
.
to_numpy
()[
searchinds
])
peak_posis
.
append
(
x0
[
maxipos0
])
else
:
x
=
data
.
energy
.
to_numpy
()[
searchinds
]
y
=
spec
.
to_numpy
()[
searchinds
]
corr_len
=
np
.
sum
(
searchinds
)
corr
=
sp
.
signal
.
correlate
(
y
,
y0
,
mode
=
'
full
'
)
maxpos
=
np
.
argmax
(
corr
)
shift
=
maxpos
-
corr_len
peak_posis
.
append
(
x
[
maxipos0
+
shift
])
elif
method
.
lower
()
==
'
gauss_fit
'
:
#********************************************
# Define needed functions
#********************************************
def
Gauss
(
grid
,
x0
,
sigma
):
# Returns a normalized bell curve
# with center at x0 and sigma
# on grid
return
1.0
/
(
sigma
*
np
.
sqrt
(
2.0
*
np
.
pi
))
*
np
.
exp
(
-
0.5
*
(
grid
-
x0
)
**
2
/
sigma
**
2
)
def
Cost
(
p
,
grid
,
spec
):
return
np
.
sum
(
np
.
square
(
p
[
0
]
*
Gauss
(
grid
,
p
[
1
],
p
[
2
])
-
spec
))
#********************************************
# Find the max for each of the spectra
#********************************************
for
spec
in
data
.
spectrum
:
x
=
data
.
energy
.
to_numpy
()[
searchinds
]
y
=
spec
.
to_numpy
()[
searchinds
]
#********************************************
# Initial Guess and bounds
#********************************************
area
=
np
.
sum
(
y
)
mean
=
np
.
average
(
x
,
weights
=
y
)
std
=
np
.
sqrt
(
np
.
average
((
x
-
mean
)
**
2
,
weights
=
y
/
area
))
p0
=
[
area
,
mean
,
std
]
#********************************************
# Bounds
#********************************************
bnds
=
[[
0
,
None
],[
start
,
stop
],[
0
,
2
*
(
stop
-
start
)]]
#********************************************
# Fit by minimizing least squares error
#********************************************
p
=
optimize
.
minimize
(
Cost
,
p0
,
args
=
(
x
,
y
),
bounds
=
bnds
,
method
=
'
L-BFGS-B
'
,
tol
=
1e-6
,
options
=
{
'
disp
'
:
0
,
'
maxiter
'
:
1000000
})
if
p
.
success
:
peak_posis
.
append
(
p
.
x
[
1
])
else
:
plt
.
figure
()
plt
.
plot
(
x
,
y
,
'
.
'
)
plt
.
plot
(
x
,
p
.
x
[
0
]
*
Gauss
(
x
,
p
.
x
[
1
],
p
.
x
[
2
]))
raise
Exception
(
'
align_readouts(): can not fit a gaussian to the data.
'
)
else
:
raise
Exception
(
'
align_readouts() did recognize the method.
'
)
return
peak_posis
def
integrate
(
self
,
data
):
def
integrate
(
self
,
data
):
bins
=
self
.
Y_RANGE
.
stop
-
self
.
Y_RANGE
.
start
bins
=
self
.
Y_RANGE
.
stop
-
self
.
Y_RANGE
.
start
...
...
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