Commit ccc4cc35 authored by Damien Naudet's avatar Damien Naudet
Browse files

work on the fit widget

parent 077c44b0
......@@ -182,20 +182,23 @@ class FitWidget(Qt.QDialog):
fit_type=self.__fitType,
roiIndices=self.__roiIndices)
with FitH5Writer(self.__selectedFile, mode='w') as fitH5:
fitH5.set_x_fit(results.x_height,
results.x_center,
results.x_width)
fitH5.set_y_fit(results.y_height,
results.y_center,
results.y_width)
fitH5.set_z_fit(results.z_height,
results.z_center,
results.z_width)
fitH5.set_scan_positions(results.sample_x, results.sample_y)
fitH5.set_status(results.status)
fitH5.set_x_axis(results.q_x)
fitH5.set_y_axis(results.q_y)
fitH5.set_z_axis(results.q_z)
fitH5.create_entry(results.fit_name)
fitH5.create_process(results.fit_name)
fitH5.
# fitH5.set_x_fit(results.x_height,
# results.x_center,
# results.x_width)
# fitH5.set_y_fit(results.y_height,
# results.y_center,
# results.y_width)
# fitH5.set_z_fit(results.z_height,
# results.z_center,
# results.z_width)
# fitH5.set_scan_positions(results.sample_x, results.sample_y)
# fitH5.set_status(results.status)
# fitH5.set_x_axis(results.q_x)
# fitH5.set_y_axis(results.q_y)
# fitH5.set_z_axis(results.q_z)
self.__fitFile = self.__selectedFile
self._setStatus(FitWidget.StatusCompleted)
......
......@@ -37,132 +37,294 @@ import numpy as np
from .XsocsH5Base import XsocsH5Base
class FitH5(XsocsH5Base):
scan_positions_path = 'scan_positions'
q_fit_path = 'q{0}fit'
status_path = 'success'
axis_path = 'axis/{0}'
@property
def scan_positions(self):
with self:
return self.sample_x, self.sample_y
sample_x = property(lambda self:
self._get_array_data(FitH5.scan_positions_path + '/x'))
sample_y = property(lambda self:
self._get_array_data(FitH5.scan_positions_path + '/y'))
x_fit = property(lambda self: self.__get_fit('x'))
y_fit = property(lambda self: self.__get_fit('y'))
z_fit = property(lambda self: self.__get_fit('z'))
x_height = property(lambda self: self.__get_data('x', 'height'))
y_height = property(lambda self: self.__get_data('y', 'height'))
z_height = property(lambda self: self.__get_data('z', 'height'))
x_center = property(lambda self: self.__get_data('x', 'center'))
y_center = property(lambda self: self.__get_data('y', 'center'))
z_center = property(lambda self: self.__get_data('z', 'center'))
x_width = property(lambda self: self.__get_data('x', 'width'))
y_width = property(lambda self: self.__get_data('y', 'width'))
z_width = property(lambda self: self.__get_data('z', 'width'))
status = property(lambda self: self._get_array_data(FitH5.status_path))
x_axis = property(lambda self: self.__get_axis_values('x'))
y_axis = property(lambda self: self.__get_axis_values('y'))
z_axis = property(lambda self: self.__get_axis_values('z'))
def __get_fit(self, axis):
with self:
height = self.__get_data(axis, 'height')
center = self.__get_data(axis, 'center')
width = self.__get_data(axis, 'width')
return height, center, width
# class GaussianFitH5(FitH5):
def __get_data(self, axis, data):
data_path = FitH5.q_fit_path.format(axis) + '/{0}'.format(data)
return self._get_array_data(data_path)
def __get_axis_values(self, axis):
return self._get_array_data(FitH5.axis_path.format(axis))
def export_txt(self, filename):
with self:
with open(filename, 'w+') as res_f:
res_f.write('X Y '
'height_x center_x width_x '
'height_y center_y width_y '
'height_z center_z width_z '
'|q| status\n')
x_height, x_center, x_width = self.x_fit
y_height, y_center, y_width = self.y_fit
z_height, z_center, z_width = self.z_fit
q = np.sqrt(x_center ** 2 +
y_center ** 2 +
z_center ** 2)
status = self.status
x, y = self.scan_positions
for i, s in enumerate(status):
r = [x[i], y[i],
x_height[i], x_center[i], x_width[i],
y_height[i], y_center[i], y_width[i],
z_height[i], z_center[i], z_width[i],
q[i], s]
res_str = '{0}\n'.format(' '.join(str(e) for e in r))
res_f.write(res_str)
class FitH5(XsocsH5Base):
title_path = '{entry}/title'
start_time_path = '{entry}/start_time'
end_time_path = '{entry}/end_time'
date_path = '{entry}/{process}/date'
axis_path = '{entry}/{process}/axis'
configuration_path = '{entry}/{process}/configuration'
results_path = '{entry}/{process}/results'
qx_results_path = '{entry}/{process}/results/qx/{0}'
qy_results_path = '{entry}/{process}/results/qy/{0}'
qz_results_path = '{entry}/{process}/results/qz/{0}'
def __init__(self, h5_f, mode='r'):
super(XsocsH5Base, self).__init__(h5_f, mode=mode)
self.__entries = None
def title(self, entry):
with self._get_file() as h5_file:
path = entry + '/title'
return h5_file[path][()]
def _update_entries(self):
with self._get_file() as h5_file:
# TODO : this isnt pretty but for some reason the attrs.get() fails
# when there is no attribute NX_class (should return the default
# None)
self.__entries = sorted([key for key in h5_file
if ('NX_class' in h5_file[key].attrs and
h5_file[key].attrs['NX_class'] == 'NXentry')]) # noqa
def entries(self):
if self.__entries is None:
self._update_entries()
return self.__entries[:]
def processes(self, entry):
with self._get_file() as h5_file:
entry_grp = h5_file[entry]
processes = sorted([key for key in entry_grp
if ('NX_class' in entry_grp[key].attrs and
h5_file[key].attrs[
'NX_class'] == 'NXprocess')])
return processes
def results(self, entry, process):
with self._get_file() as h5_file:
results_path = FitH5.results_path.format(entry=entry,
process=process)
result_grp = h5_file[results_path]
results = sorted([key for key in result_grp])
return results
def result(self, entry, process, result):
result_path = FitH5.result_path.format(entry=entry,
process=process,
result=result)
return self._get_array_data(result_path)
class FitH5Writer(FitH5):
def __init__(self, h5_f, mode='a', **kwargs):
super(FitH5Writer, self).__init__(h5_f, mode=mode, **kwargs)
def set_scan_positions(self, x, y):
path_tpl = FitH5.scan_positions_path + '/{0}'
self._set_array_data(path_tpl.format('x'), x)
self._set_array_data(path_tpl.format('y'), y)
def set_x_fit(self, height, center, width):
self.__set_fit('x', height, center, width)
def set_y_fit(self, height, center, width):
self.__set_fit('y', height, center, width)
def set_z_fit(self, height, center, width):
self.__set_fit('z', height, center, width)
def __set_fit(self, axis, height, center, width):
path_tpl = FitH5.q_fit_path.format(axis) + '/{0}'
self._set_array_data(path_tpl.format('height'), height)
self._set_array_data(path_tpl.format('center'), center)
self._set_array_data(path_tpl.format('width'), width)
def set_status(self, status):
self._set_array_data(FitH5.status_path, status)
def create_entry(self, entry):
with self._get_file() as h5_file:
# TODO : check if it already exists
entry_grp = h5_file.require_group(entry)
entry_grp.attrs['NX_class'] = np.string_('NXentry')
self._update_entries()
def create_process(self, entry, process):
# TODO : check that there isn't already an existing process
with self._get_file() as h5_file:
if entry not in h5_file:
self.create_entry(entry)
entry_grp = h5_file[entry]
# TODO : check if it exists
process_grp = entry_grp.require_group[process]
process_grp.attrs['NX_class'] = np.string_('NXprocess')
results_grp = process_grp.require_group('results')
results_grp.attrs['NX_class'] = np.string_('NXcollection')
def set_title(self, entry, title):
self._set_scalar_data(FitH5.title_path.format(entry), title)
def set_result(self, entry, process, name, data):
result_path = FitH5.result_path.format(entry=entry,
process=process,
result=name)
self._set_array_data(result_path, data)
#
# def export_txt(self, filename):
# with self:
# with open(filename, 'w+') as res_f:
# res_f.write('X Y '
# 'height_x center_x width_x '
# 'height_y center_y width_y '
# 'height_z center_z width_z '
# '|q| status\n')
# x_height, x_center, x_width = self.x_fit
# y_height, y_center, y_width = self.y_fit
# z_height, z_center, z_width = self.z_fit
# q = np.sqrt(x_center ** 2 +
# y_center ** 2 +
# z_center ** 2)
# status = self.status
# x, y = self.scan_positions
#
# for i, s in enumerate(status):
# r = [x[i], y[i],
# x_height[i], x_center[i], x_width[i],
# y_height[i], y_center[i], y_width[i],
# z_height[i], z_center[i], z_width[i],
# q[i], s]
# res_str = '{0}\n'.format(' '.join(str(e) for e in r))
# res_f.write(res_str)
def __set_axis_values(self, axis, values):
self._set_array_data(FitH5.axis_path.format(axis), values)
def set_x_axis(self, values):
self.__set_axis_values('x', values)
#
# class FitH5Writer(FitH5):
#
# def __init__(self, h5_f, mode='a', **kwargs):
# super(FitH5Writer, self).__init__(h5_f, mode=mode, **kwargs)
#
# def set_scan_positions(self, x, y):
# path_tpl = FitH5.scan_positions_path + '/{0}'
# self._set_array_data(path_tpl.format('x'), x)
# self._set_array_data(path_tpl.format('y'), y)
#
# def set_x_fit(self, height, center, width):
# self.__set_fit('x', height, center, width)
#
# def set_y_fit(self, height, center, width):
# self.__set_fit('y', height, center, width)
#
# def set_z_fit(self, height, center, width):
# self.__set_fit('z', height, center, width)
#
# def __set_fit(self, axis, height, center, width):
# path_tpl = FitH5.q_fit_path.format(axis) + '/{0}'
# self._set_array_data(path_tpl.format('height'), height)
# self._set_array_data(path_tpl.format('center'), center)
# self._set_array_data(path_tpl.format('width'), width)
#
# def set_status(self, status):
# self._set_array_data(FitH5.status_path, status)
#
# def __set_axis_values(self, axis, values):
# self._set_array_data(FitH5.axis_path.format(axis), values)
#
# def set_x_axis(self, values):
# self.__set_axis_values('x', values)
#
# def set_y_axis(self, values):
# self.__set_axis_values('y', values)
#
# def set_z_axis(self, values):
# self.__set_axis_values('z', values)
def set_y_axis(self, values):
self.__set_axis_values('y', values)
def set_z_axis(self, values):
self.__set_axis_values('z', values)
# class FitH5(XsocsH5Base):
# scan_positions_path = 'scan_positions'
# q_fit_path = 'q{0}fit'
# status_path = 'success'
# axis_path = 'axis/{0}'
#
# @property
# def scan_positions(self):
# with self:
# return self.sample_x, self.sample_y
#
# sample_x = property(lambda self:
# self._get_array_data(FitH5.scan_positions_path + '/x'))
#
# sample_y = property(lambda self:
# self._get_array_data(FitH5.scan_positions_path + '/y'))
#
# x_fit = property(lambda self: self.__get_fit('x'))
#
# y_fit = property(lambda self: self.__get_fit('y'))
#
# z_fit = property(lambda self: self.__get_fit('z'))
#
# x_height = property(lambda self: self.__get_data('x', 'height'))
#
# y_height = property(lambda self: self.__get_data('y', 'height'))
#
# z_height = property(lambda self: self.__get_data('z', 'height'))
#
# x_center = property(lambda self: self.__get_data('x', 'center'))
#
# y_center = property(lambda self: self.__get_data('y', 'center'))
#
# z_center = property(lambda self: self.__get_data('z', 'center'))
#
# x_width = property(lambda self: self.__get_data('x', 'width'))
#
# y_width = property(lambda self: self.__get_data('y', 'width'))
#
# z_width = property(lambda self: self.__get_data('z', 'width'))
#
# status = property(lambda self: self._get_array_data(FitH5.status_path))
#
# x_axis = property(lambda self: self.__get_axis_values('x'))
#
# y_axis = property(lambda self: self.__get_axis_values('y'))
#
# z_axis = property(lambda self: self.__get_axis_values('z'))
#
# def __get_fit(self, axis):
# with self:
# height = self.__get_data(axis, 'height')
# center = self.__get_data(axis, 'center')
# width = self.__get_data(axis, 'width')
# return height, center, width
#
# def __get_data(self, axis, data):
# data_path = FitH5.q_fit_path.format(axis) + '/{0}'.format(data)
# return self._get_array_data(data_path)
#
# def __get_axis_values(self, axis):
# return self._get_array_data(FitH5.axis_path.format(axis))
#
# def export_txt(self, filename):
# with self:
# with open(filename, 'w+') as res_f:
# res_f.write('X Y '
# 'height_x center_x width_x '
# 'height_y center_y width_y '
# 'height_z center_z width_z '
# '|q| status\n')
# x_height, x_center, x_width = self.x_fit
# y_height, y_center, y_width = self.y_fit
# z_height, z_center, z_width = self.z_fit
# q = np.sqrt(x_center ** 2 +
# y_center ** 2 +
# z_center ** 2)
# status = self.status
# x, y = self.scan_positions
#
# for i, s in enumerate(status):
# r = [x[i], y[i],
# x_height[i], x_center[i], x_width[i],
# y_height[i], y_center[i], y_width[i],
# z_height[i], z_center[i], z_width[i],
# q[i], s]
# res_str = '{0}\n'.format(' '.join(str(e) for e in r))
# res_f.write(res_str)
#
#
# class FitH5Writer(FitH5):
#
# def __init__(self, h5_f, mode='a', **kwargs):
# super(FitH5Writer, self).__init__(h5_f, mode=mode, **kwargs)
#
# def set_scan_positions(self, x, y):
# path_tpl = FitH5.scan_positions_path + '/{0}'
# self._set_array_data(path_tpl.format('x'), x)
# self._set_array_data(path_tpl.format('y'), y)
#
# def set_x_fit(self, height, center, width):
# self.__set_fit('x', height, center, width)
#
# def set_y_fit(self, height, center, width):
# self.__set_fit('y', height, center, width)
#
# def set_z_fit(self, height, center, width):
# self.__set_fit('z', height, center, width)
#
# def __set_fit(self, axis, height, center, width):
# path_tpl = FitH5.q_fit_path.format(axis) + '/{0}'
# self._set_array_data(path_tpl.format('height'), height)
# self._set_array_data(path_tpl.format('center'), center)
# self._set_array_data(path_tpl.format('width'), width)
#
# def set_status(self, status):
# self._set_array_data(FitH5.status_path, status)
#
# def __set_axis_values(self, axis, values):
# self._set_array_data(FitH5.axis_path.format(axis), values)
#
# def set_x_axis(self, values):
# self.__set_axis_values('x', values)
#
# def set_y_axis(self, values):
# self.__set_axis_values('y', values)
#
# def set_z_axis(self, values):
# self.__set_axis_values('z', values)
......@@ -333,7 +333,7 @@ class XsocsH5Writer(XsocsH5):
grp['info'] = det_grp
det_grp['data'] = _h5py.SoftLink(self.img_data_tpl.format(entry))
self._update_entries()
self._update_entries()
class XsocsH5MasterWriter(XsocsH5Writer):
......
......@@ -55,7 +55,8 @@ FitResult = namedtuple('FitResult', ['sample_x', 'sample_y',
'x_height', 'x_center', 'x_width',
'y_height', 'y_center', 'y_width',
'z_height', 'z_center', 'z_width',
'status'])
'status',
'fit_name'])
_const_inv_2_pi_ = np.sqrt(2 * np.pi)
......@@ -261,6 +262,11 @@ def peak_fit(qspace_f,
q_y = q_y[ySlice]
q_z = q_z[zSlice]
if fit_type == FitTypes.LEASTSQ:
fit_name = 'LeastSq'
else:
fit_name = 'Centroid'
fit_results = FitResult(sample_x=x_pos,
sample_y=y_pos,
q_x=q_x,
......@@ -275,7 +281,8 @@ def peak_fit(qspace_f,
z_height=results_np[:, 6].ravel(),
z_center=results_np[:, 7].ravel(),
z_width=results_np[:, 8].ravel(),
status=status)
status=status,
fit_name=fit_name)
return fit_results
......
Supports Markdown
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