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
616428fb
Commit
616428fb
authored
5 years ago
by
Laurent Mercadier
Browse files
Options
Downloads
Patches
Plain Diff
More description of fitting parameters
parent
28554868
No related branches found
No related tags found
1 merge request
!36
Knife edge
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
knife_edge.py
+23
-12
23 additions, 12 deletions
knife_edge.py
with
23 additions
and
12 deletions
knife_edge.py
+
23
−
12
View file @
616428fb
...
@@ -9,20 +9,26 @@ import numpy as np
...
@@ -9,20 +9,26 @@ import numpy as np
from
scipy.special
import
erfc
from
scipy.special
import
erfc
from
scipy.optimize
import
curve_fit
from
scipy.optimize
import
curve_fit
def
knife_edge
(
nrun
,
axisKey
=
'
scannerX
'
,
signalKey
=
'
FastADC4peaks
'
,
p0
=
None
,
plot
=
False
):
def
knife_edge
(
nrun
,
axisKey
=
'
scannerX
'
,
signalKey
=
'
FastADC4peaks
'
,
p0
=
None
,
full
=
False
,
plot
=
False
):
'''
Calculates the beam radius at 1/e^2 from a knife-edge scan by fitting with
erfc
'''
Calculates the beam radius at 1/e^2 from a knife-edge scan by fitting with
function: f(a, u) = a*erfc(u) or f(a, u) = a*erfc(-u) where
u = sqrt(2)*(x-x0)/w0
erfc
function: f(a, u) = a*erfc(u) or f(a, u) = a*erfc(-u) where
with w0 the beam radius at 1/e^2 and x0 the beam center.
u = sqrt(2)*(x-x0)/w0
with w0 the beam radius at 1/e^2 and x0 the beam center.
Inputs:
Inputs:
nrun: xarray Dataset containing the detector signal and the motor position.
nrun: xarray Dataset containing the detector signal and the motor
axisKey: string, key of the axis against which the knife-edge is performed.
position.
axisKey: string, key of the axis against which the knife-edge is
performed.
signalKey: string, key of the detector signal.
signalKey: string, key of the detector signal.
p0: list, initial parameters used for the fit: x0, w0, a. If None, a beam
p0: list, initial parameters used for the fit: x0, w0, a. If None, a beam
radius of 100 um is assumed.
radius of 100 um is assumed.
full: bool: If False, returns the beam radius and standard error. If True,
returns the popt, pcov list of parameters and covariance matrix from
curve_fit.
plot: bool: If True, plots the data and the result of the fit.
plot: bool: If True, plots the data and the result of the fit.
Outputs:
Outputs:
ndarray with beam radius at 1/e^2 in mm and standard error from the fit
If full is False, ndarray with beam radius at 1/e^2 in mm and standard
in mm.
error from the fit in mm. If full is True, returns popt and pcov from
curve_fit function.
'''
'''
def
integPowerUp
(
x
,
x0
,
w0
,
a
):
def
integPowerUp
(
x
,
x0
,
w0
,
a
):
return
a
*
erfc
(
-
np
.
sqrt
(
2
)
*
(
x
-
x0
)
/
w0
)
return
a
*
erfc
(
-
np
.
sqrt
(
2
)
*
(
x
-
x0
)
/
w0
)
...
@@ -42,14 +48,16 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', p0=None, plo
...
@@ -42,14 +48,16 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', p0=None, plo
intensities
=
nrun
[
signalKey
].
values
.
flatten
()[
sortIdx
]
intensities
=
nrun
[
signalKey
].
values
.
flatten
()[
sortIdx
]
if
intensities
[
0
]
>
intensities
[
-
1
]:
if
intensities
[
0
]
>
intensities
[
-
1
]:
func
=
integPowerDown
func
=
integPowerDown
funcStr
=
'
a*erfc(np.sqrt(2)*(x-x0)/w0)
'
else
:
else
:
func
=
integPowerUp
func
=
integPowerUp
funcStr
=
'
a*erfc(-np.sqrt(2)*(x-x0)/w0)
'
if
p0
is
None
:
if
p0
is
None
:
p0
=
[
np
.
mean
(
positions
),
0.1
,
np
.
max
(
intensities
)
/
2
]
p0
=
[
np
.
mean
(
positions
),
0.1
,
np
.
max
(
intensities
)
/
2
]
popt
,
pcov
=
curve_fit
(
func
,
positions
,
intensities
,
p0
=
p0
)
popt
,
pcov
=
curve_fit
(
func
,
positions
,
intensities
,
p0
=
p0
)
print
(
'
fitting function:
'
,
funcStr
)
print
(
'
w0 = (%.1f +/- %.1f) um
'
%
(
popt
[
1
]
*
1e3
,
pcov
[
1
,
1
]
**
0.5
*
1e3
))
print
(
'
w0 = (%.1f +/- %.1f) um
'
%
(
popt
[
1
]
*
1e3
,
pcov
[
1
,
1
]
**
0.5
*
1e3
))
print
(
'
x0 = (%.3f +/- %.
1
f) mm
'
%
(
popt
[
0
],
pcov
[
0
,
0
]
**
0.5
*
1e3
))
print
(
'
x0 = (%.3f +/- %.
3
f) mm
'
%
(
popt
[
0
],
pcov
[
0
,
0
]
**
0.5
*
1e3
))
print
(
'
a = %e +/- %e
'
%
(
popt
[
2
],
pcov
[
2
,
2
]
**
0.5
*
1e3
))
print
(
'
a = %e +/- %e
'
%
(
popt
[
2
],
pcov
[
2
,
2
]
**
0.5
*
1e3
))
if
plot
:
if
plot
:
...
@@ -63,6 +71,9 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', p0=None, plo
...
@@ -63,6 +71,9 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', p0=None, plo
for
lh
in
leg
.
legendHandles
:
for
lh
in
leg
.
legendHandles
:
lh
.
set_alpha
(
1
)
lh
.
set_alpha
(
1
)
plt
.
ylabel
(
signalKey
)
plt
.
ylabel
(
signalKey
)
plt
.
xlabel
(
axisKey
+
'
-
position [mm]
'
)
plt
.
xlabel
(
axisKey
+
'
position [mm]
'
)
plt
.
tight_layout
()
plt
.
tight_layout
()
return
np
.
array
([
popt
[
1
],
pcov
[
1
,
1
]
**
0.5
])
if
full
:
\ No newline at end of file
return
popt
,
pcov
else
:
return
np
.
array
([
popt
[
1
],
pcov
[
1
,
1
]
**
0.5
])
\ No newline at end of file
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