Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
kmap
xsocs
Commits
aaa91dda
Commit
aaa91dda
authored
Nov 21, 2018
by
Thomas Vincent
Browse files
rework fit/com sample code
parent
a876a057
Changes
2
Hide whitespace changes
Inline
Side-by-side
xsocs/examples/id01_peak.py
View file @
aaa91dda
import
numpy
as
np
import
os
#!/usr/bin/python
# coding: utf-8
"""
This scripts illustrates the API of the gaussian/center-of-mass processing
"""
from
xsocs.process.fit
import
peak_fit
import
sys
import
numpy
from
xsocs.process.fit
import
PeakFitter
,
FitTypes
,
BackgroundTypes
# output directory (some temporary files will also be written there)
workdir
=
'/path/to/workdir/'
# path to the hdf5 file written by the img_to_qspace function
# path to the hdf5 file written by the kmap_2_qspace function
qspace_f
=
'/path/to/qspace.h5'
#
result file
result_file
=
os
.
path
.
join
(
workdir
,
'results.txt'
)
#
Name of the text file where to store the resuls
result_file
=
'results.txt'
# positions (on the sample) to convert to qspace
# indices = array with indices (of the sample positions array)
# List of QSpace indices to process
# This selects sample positions for which QSpace are processed.
# If None, all QSpace are processed
indices
=
None
#
n
umber of process
e
s to use
# If None,
wi
ll
use the number of
availble core
(see multiprocessing.cpu_count)
#
N
umber of process
or
s to use
# If None,
a
ll avail
a
ble core
s are used.
n_proc
=
None
results
,
success
=
peak_fit
.
peak_fit
(
qspace_f
,
indices
=
indice
,
fit_type
=
peak_fit
.
FitTypes
.
GAUSSIAN
,
n_proc
=
n_proc
)
with
open
(
result_file
,
'w+'
)
as
res_f
:
res_f
.
write
(
'# X Y qx qy qz q I valid
\n
'
)
for
i
,
s
in
enumerate
(
success
):
x_pos
=
results
[
i
,
0
]
y_pos
=
results
[
i
,
1
]
xpeak
=
results
[
i
,
3
]
ypeak
=
results
[
i
,
6
]
zpeak
=
results
[
i
,
9
]
xpeak_max
=
results
[
i
,
2
]
q
=
np
.
sqrt
(
xpeak
**
2
+
ypeak
**
2
+
zpeak
**
2
)
r
=
(
x_pos
,
y_pos
,
xpeak
,
ypeak
,
zpeak
,
q
,
xpeak_max
,
s
)
res_str
=
'{0} {1} {2} {3} {4} {5} {6} {7} ({8})
\n
'
.
format
(
i
,
*
r
)
res_f
.
write
(
res_str
)
# Set-up the processing for a gaussian fit without background subtraction
fitter
=
PeakFitter
(
qspace_f
,
fit_type
=
FitTypes
.
GAUSSIAN
,
indices
=
indices
,
n_proc
=
n_proc
,
roi_indices
=
None
,
# QSpace ROI
background
=
BackgroundTypes
.
NONE
)
# Run the processing and get the results
results
=
fitter
.
peak_fit
()
# Check for errors
if
fitter
.
status
!=
fitter
.
DONE
:
print
(
"Fit process failed"
)
sys
.
exit
()
# Prepare columns header and data
# Start with position (x, y) on the sample
headers
=
[
'sample_x'
,
'sample_y'
]
values
=
[
results
.
sample_x
,
results
.
sample_y
]
# Add one column for each parameter of the fit/COM result
# For each axis of the QSpace
for
dimension
,
axis_name
in
enumerate
(
results
.
qspace_dimension_names
):
# For each fitted/computed parameter of the result
for
parameter_name
in
results
.
available_result_names
:
# Add its name to the header and its data to the values
headers
.
append
(
axis_name
+
'_'
+
parameter_name
)
values
.
append
(
results
.
get_results
(
dimension
,
parameter_name
))
# Transpose values from (parameters x points) to (points x parameters)
values
=
numpy
.
transpose
(
numpy
.
array
(
values
))
# Write results to text file
with
open
(
result_file
,
'w'
)
as
res_f
:
res_f
.
write
(
'
\t
'
.
join
(
headers
)
+
'
\n
'
)
for
row_values
in
values
:
res_f
.
write
(
'
\t
'
.
join
(
str
(
v
)
for
v
in
row_values
)
+
'
\n
'
)
xsocs/process/fit/peak_fit.py
View file @
aaa91dda
...
...
@@ -119,7 +119,7 @@ class FitResult(object):
self
.
_fit_results
=
numpy
.
transpose
(
fit_results
)
@
property
def
available_results
(
self
,
dimension
=
None
):
def
available_result
_name
s
(
self
,
dimension
=
None
):
"""Returns the available result names
:param Union[int,None] dimension:
...
...
@@ -173,7 +173,7 @@ class FitResult(object):
self
.
_fit_results
,
(
fitH5
.
set_qx_result
,
fitH5
.
set_qy_result
,
fitH5
.
set_qz_result
),
(
0
,
1
,
2
)):
for
name
in
self
.
available_results
:
for
name
in
self
.
available_result
_name
s
:
results
=
self
.
get_results
(
axis
,
name
,
copy
=
False
)
if
name
==
'Status'
:
fitH5
.
set_status
(
fit_name
,
axis
,
results
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment