Commit aaa91dda authored by Thomas Vincent's avatar Thomas Vincent

rework fit/com sample code

parent a876a057
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
# number of processes to use
# If None, will use the number of availble core (see multiprocessing.cpu_count)
# Number of processors to use
# If None, all available cores 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')
......@@ -119,7 +119,7 @@ class FitResult(object):
self._fit_results = numpy.transpose(fit_results)
@property
def available_results(self, dimension=None):
def available_result_names(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_names:
results = self.get_results(axis, name, copy=False)
if name == 'Status':
fitH5.set_status(fit_name, axis, results)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment