Commit 151a1869 authored by Damien Naudet's avatar Damien Naudet

Added the MaxFitter.

parent bcaa0ad2
import numpy as np
import os
from xsocs.process import peak_fit
import numpy as np
from xsocs.process.fit import peak_fit
# output directory (some temporary files will also be written there)
workdir = '/path/to/workdir/'
......
......@@ -35,8 +35,10 @@ import numpy as np
from .Plotter import Plotter
from ..process.Fitter import Fitter
from ..process.fitresults import FitStatus
from ..process.fit.Fitter import Fitter
from ..process.fit.fitresults import FitStatus
from ..process.fit.sharedresults import FitSharedResults
from ..process.fit.fitresults import FitResult
class CentroidFitter(Fitter):
......@@ -82,12 +84,59 @@ class CentroidFitter(Fitter):
FitStatus.FAILED)
class CentroidPlotter(Plotter):
class CentroidResults(FitSharedResults):
def __init__(self,
n_points=None,
shared_results=None,
shared_status=None,
**kwargs):
super(CentroidResults, self).__init__(n_points=n_points,
n_params=3,
n_peaks=1,
shared_results=shared_results,
shared_status=shared_status)
def fit_results(self, *args, **kwargs):
qx_results = self._npy_qx_results
qy_results = self._npy_qy_results
qz_results = self._npy_qz_results
qx_status = self._npy_qx_status
qy_status = self._npy_qy_status
qz_status = self._npy_qz_status
fit_name = 'Centroid'
results = FitResult(fit_name, *args, **kwargs)
results.add_qx_result('centroid', 'I', qx_results[:, 0].ravel())
results.add_qx_result('centroid', 'COM',
qx_results[:, 1].ravel())
results.add_qx_result('centroid', 'Max',
qx_results[:, 2].ravel())
results.set_qx_status(qx_status)
results.add_qy_result('centroid', 'I', qy_results[:, 0].ravel())
results.add_qy_result('centroid',
'COM',
qy_results[:, 1].ravel())
results.add_qy_result('centroid', 'Max',
qy_results[:, 2].ravel())
results.set_qy_status(qy_status)
results.add_qz_result('centroid', 'I', qz_results[:, 0].ravel())
results.add_qz_result('centroid',
'COM',
qz_results[:, 1].ravel())
results.add_qz_result('centroid', 'Max',
qz_results[:, 2].ravel())
results.set_qz_status(qz_status)
return results
class CentroidPlotter(Plotter):
def plotFit(self, plot, x, peakParams):
plot.setGraphTitle('QX center of mass')
plot.setGraphTitle('center of mass')
for peakName, peak in peakParams.items():
center = peak.get('COM')
......
......@@ -37,8 +37,10 @@ from scipy.optimize import leastsq
from .Plotter import Plotter
from ..process.Fitter import Fitter
from ..process.fitresults import FitStatus
from ..process.fit.Fitter import Fitter
from ..process.fit.fitresults import FitStatus
from ..process.fit.sharedresults import FitSharedResults
from ..process.fit.fitresults import FitResult
# Some constants
......@@ -69,6 +71,63 @@ class GaussianFitter(Fitter):
self._shared_results.set_qx_results(i_fit, x_fit, success_x)
class GaussianResults(FitSharedResults):
def __init__(self,
n_points=None,
n_peaks=1,
shared_results=None,
shared_status=None):
super(GaussianResults, self).__init__(n_points=n_points,
n_params=3,
n_peaks=n_peaks,
shared_results=shared_results,
shared_status=shared_status)
def fit_results(self, *args, **kwargs):
qx_results = self._npy_qx_results
qy_results = self._npy_qy_results
qz_results = self._npy_qz_results
qx_status = self._npy_qx_status
qy_status = self._npy_qy_status
qz_status = self._npy_qz_status
fit_name = 'Gaussian'
results = FitResult(fit_name, *args, **kwargs)
for i_peak in range(self._n_peaks):
peak_name = 'gauss_{0}'.format(i_peak)
i_start = i_peak * 3
results.add_qx_result(peak_name, 'intensity',
qx_results[:, i_start].ravel())
results.add_qx_result(peak_name, 'position',
qx_results[:, i_start + 1].ravel())
results.add_qx_result(peak_name, 'width',
qx_results[:, i_start + 2].ravel())
results.add_qy_result(peak_name, 'intensity',
qy_results[:, i_start].ravel())
results.add_qy_result(peak_name, 'position',
qy_results[:, i_start + 1].ravel())
results.add_qy_result(peak_name, 'width',
qy_results[:, i_start + 2].ravel())
results.add_qz_result(peak_name, 'intensity',
qz_results[:, i_start].ravel())
results.add_qz_result(peak_name, 'position',
qz_results[:, i_start + 1].ravel())
results.add_qz_result(peak_name, 'width',
qz_results[:, i_start + 2].ravel())
results.set_qy_status(qy_status)
results.set_qx_status(qx_status)
results.set_qz_status(qz_status)
return results
# 1d Gaussian func
# TODO : optimize
def gaussian(x, a, c, s):
......
#!/usr/bin/python
# coding: utf8
# /*##########################################################################
#
# Copyright (c) 2015-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
from __future__ import absolute_import
__authors__ = ["D. Naudet"]
__date__ = "01/01/2017"
__license__ = "MIT"
from .Plotter import Plotter
from ..process.fit.Fitter import Fitter
from ..process.fit.fitresults import FitStatus
from ..process.fit.sharedresults import FitSharedResults
from ..process.fit.fitresults import FitResult
class MaxFitter(Fitter):
"""
Fitter that returns the maximum and its position.
"""
def fit(self, i_fit, i_cube, qx_profile, qy_profile, qz_profile):
idx_max = qx_profile.argmax()
self._shared_results.set_qx_results(i_fit,
[qx_profile[idx_max],
self._qx[idx_max]],
FitStatus.OK)
idx_max = qy_profile.argmax()
self._shared_results.set_qy_results(i_fit,
[qy_profile[idx_max],
self._qy[idx_max]],
FitStatus.OK)
idx_max = qz_profile.argmax()
self._shared_results.set_qz_results(i_fit,
[qz_profile[idx_max],
self._qz[idx_max]],
FitStatus.OK)
class MaxResults(FitSharedResults):
def __init__(self,
n_points=None,
shared_results=None,
shared_status=None):
super(MaxResults, self).__init__(n_points=n_points,
n_params=2,
n_peaks=1,
shared_results=shared_results,
shared_status=shared_status)
def fit_results(self, *args, **kwargs):
qx_results = self._npy_qx_results
qy_results = self._npy_qy_results
qz_results = self._npy_qz_results
qx_status = self._npy_qx_status
qy_status = self._npy_qy_status
qz_status = self._npy_qz_status
fit_name = 'Max'
results = FitResult(fit_name, *args, **kwargs)
results.add_qx_result('max', 'max', qx_results[:, 0].ravel())
results.add_qx_result('max', 'position',
qx_results[:, 1].ravel())
results.set_qx_status(qx_status)
results.add_qy_result('max', 'max', qy_results[:, 0].ravel())
results.add_qy_result('max', 'position',
qy_results[:, 1].ravel())
results.set_qy_status(qy_status)
results.add_qz_result('max', 'max', qz_results[:, 0].ravel())
results.add_qz_result('max', 'position',
qz_results[:, 1].ravel())
results.set_qz_status(qz_status)
return results
class MaxPlotter(Plotter):
def plotFit(self, plot, x, peakParams):
plot.setGraphTitle('Maximum')
for peakName, peak in peakParams.items():
maximum = peak.get('max')
position = peak.get('position')
plot.addXMarker(position, legend='max_position')
def getPlotTitle(self):
return 'Maximum'
if __name__ == '__main__':
pass
......@@ -31,13 +31,6 @@ __date__ = "01/01/2017"
__license__ = "MIT"
import numpy as np
from scipy.optimize import leastsq
from ..process.Fitter import Fitter
from ..process.fitresults import FitStatus
class Plotter(object):
def __init__(self):
super(Plotter, self).__init__()
......
......@@ -38,8 +38,10 @@ from silx.math.fit.fitmanager import FitManager
from .Plotter import Plotter
from ..process.Fitter import Fitter
from ..process.fitresults import FitStatus
from ..process.fit.Fitter import Fitter
from ..process.fit.fitresults import FitStatus
from ..process.fit.sharedresults import FitSharedResults
from ..process.fit.fitresults import FitResult
class SilxFitter(Fitter):
......@@ -148,6 +150,64 @@ class SilxFitter(Fitter):
FitStatus.FAILED)
class SilxResults(FitSharedResults):
def __init__(self,
n_points=None,
n_peaks=1,
shared_results=None,
shared_status=None):
super(SilxResults, self).__init__(n_points=n_points,
n_params=3,
n_peaks=n_peaks,
shared_results=shared_results,
shared_status=shared_status)
def fit_results(self, *args, **kwargs):
qx_results = self._npy_qx_results
qy_results = self._npy_qy_results
qz_results = self._npy_qz_results
qx_status = self._npy_qx_status
qy_status = self._npy_qy_status
qz_status = self._npy_qz_status
fit_name = 'SilxFit'
results = FitResult(fit_name, *args, **kwargs)
for i_peak in range(self._n_peaks):
peak_name = 'gauss_{0}'.format(i_peak)
i_start = i_peak * 3
results.add_qx_result(peak_name, 'area',
qx_results[:, i_start].ravel())
results.add_qx_result(peak_name, 'position',
qx_results[:, i_start + 1].ravel())
results.add_qx_result(peak_name, 'fwhm',
qx_results[:, i_start + 2].ravel())
results.add_qy_result(peak_name, 'area',
qy_results[:, i_start].ravel())
results.add_qy_result(peak_name, 'position',
qy_results[:, i_start + 1].ravel())
results.add_qy_result(peak_name, 'fwhm',
qy_results[:, i_start + 2].ravel())
results.add_qz_result(peak_name, 'area',
qz_results[:, i_start].ravel())
results.add_qz_result(peak_name, 'position',
qz_results[:, i_start + 1].ravel())
results.add_qz_result(peak_name, 'fwhm',
qz_results[:, i_start + 2].ravel())
results.set_qy_status(qy_status)
results.set_qx_status(qx_status)
results.set_qz_status(qz_status)
return results
class SilxPlotter(Plotter):
def plotFit(self, plot, x, peakParams):
for peakName, peak in peakParams.items():
......
......@@ -31,6 +31,7 @@ __date__ = "01/01/2017"
__license__ = "MIT"
from .Gaussian import GaussianFitter, GaussianPlotter
from .Centroid import CentroidFitter, CentroidPlotter
from .SilxFitter import SilxFitter, SilxPlotter
from .Gaussian import GaussianFitter, GaussianResults, GaussianPlotter
from .Centroid import CentroidFitter, CentroidResults, CentroidPlotter
from .MaxFitter import MaxFitter, MaxResults, MaxPlotter
from .SilxFitter import SilxFitter, SilxResults, SilxPlotter
......@@ -42,7 +42,7 @@ from ..widgets.Containers import GroupBox
from ..widgets.RoiAxisWidget import RoiAxisWidget
from ..widgets.Input import StyledLineEdit
from ...process.peak_fit import PeakFitter, FitTypes
from ...process.fit.peak_fit import PeakFitter, FitTypes
class Roi3DSelectorWidget(Qt.QWidget):
......@@ -162,7 +162,8 @@ class FitWidget(Qt.QWidget):
"""
FitTypes = OrderedDict([('Gaussian', FitTypes.GAUSSIAN),
('Centroid', FitTypes.CENTROID)])\
('Centroid', FitTypes.CENTROID),
('Maximum', FitTypes.MAX)])\
# ,
# ('Silx', FitTypes.SILX)])
......
......@@ -44,7 +44,7 @@ from ..project.QSpaceGroup import QSpaceItem
from .fitview.FitModel import FitModel, FitH5Node
from .fitview.DropPlotWidget import DropPlotWidget
from ...fit import GaussianPlotter, CentroidPlotter, SilxPlotter
from ...fit import GaussianPlotter, CentroidPlotter, SilxPlotter, MaxPlotter
class FitView(Qt.QMainWindow):
......@@ -302,6 +302,8 @@ class FitView(Qt.QMainWindow):
plotterKlass = GaussianPlotter
elif entry == 'Centroid':
plotterKlass = CentroidPlotter
elif entry == 'Max':
plotterKlass = MaxPlotter
elif entry == 'SilxFit':
plotterKlass = SilxPlotter
......
......@@ -33,8 +33,8 @@ import numpy as np
from silx.gui import qt as Qt
from xsocs.io.FitH5 import FitH5, FitH5QAxis
from ....process.fitresults import FitStatus
from ....io.FitH5 import FitH5, FitH5QAxis
from ....process.fit.fitresults import FitStatus
from ...widgets.XsocsPlot2D import XsocsPlot2D
......
......@@ -43,7 +43,7 @@ from ...project.Hdf5Nodes import H5File
from ...project.Hdf5Nodes import H5Base, H5NodeClassDef
from ....io.FitH5 import FitH5, FitH5QAxis
from ....process.fitresults import FitStatus
from ....process.fit.fitresults import FitStatus
......@@ -259,6 +259,15 @@ class FitResultNode(FitProcessNode):
legend='__border',
z=-1)
plot.setPlotData(x, y, data)
# WARNING, DO NOT REMOVE
# for some reason the first thumbnail is empty if we dont call
# replot.
# ============
plot.replot()
# ============
# WARNING END
pixmap = plot.toPixmap()
self._setDataInternal(1, pixmap, Qt.Qt.DecorationRole)
qApp.processEvents()
......
......@@ -33,9 +33,9 @@ __date__ = "01/11/2016"
from silx.gui import qt as Qt
from xsocs.gui.model.Node import Node
from xsocs.gui.model.TreeView import TreeView
from xsocs.gui.model.Model import Model, ModelColumns
from ....gui.model.Node import Node
from ....gui.model.TreeView import TreeView
from ....gui.model.Model import Model, ModelColumns
class CurveGroup(Node):
......
......@@ -222,42 +222,6 @@ class ShiftNode(Node):
centerY = origin['y'] + shiftY
plot.selectPoint(centerX, centerY)
# shape = shiftSubject.getRoiSize()
# halfX = shape[0] / 2.
# halfY = shape[1] / 2.
# xBorder = np.array([centerX - halfX,
# centerX - halfX,
# centerX + halfX,
# centerX + halfX,
# centerX - halfX])
# yBorder = np.array([centerY - halfY,
# centerY + halfY,
# centerY + halfY,
# centerY - halfY,
# centerY - halfY])
#
# plot.addCurve(xBorder,
# yBorder,
# color='#f442eeaa',
# linestyle='-',
# linewidth=2,
# legend='__borders',
# z=-1)
#
# plot.addCurve(np.array([centerX, centerX]),
# np.array([centerY - halfY, centerY + halfY]),
# color='#f442eeaa',
# linestyle='-',
# linewidth=4,
# legend='__crosshairV')
#
# plot.addCurve(np.array([centerX - halfX, centerX + halfX]),
# np.array([centerY, centerY]),
# color='#f442eeaa',
# linestyle='-',
# linewidth=4,
# legend='__crosshairH')
plot.setPlotData(pos_0, pos_1, data)
plot.replot()
......
......@@ -43,7 +43,7 @@ from silx.io.utils import savetxt
from silx.gui.icons import getQIcon
from silx.gui.plot import PlotWindow
from silx.math.histogram import Histogramnd
from silx.gui.plot.MPLColormap import viridis
from silx.gui.plot.matplotlib.Colormap import viridis
from ..widgets.Containers import GroupBox
from ..widgets.RangeSlider import RangeSlider
......
......@@ -32,6 +32,19 @@ __license__ = "MIT"
class Fitter(object):
qx = property(lambda self: self._qx)
""" qx axis values """
qy = property(lambda self: self._qy)
""" qy axis values """
qz = property(lambda self: self._qz)
""" qz axis values """
shared_results = property(lambda self: self._shared_results)
""" FitSharedResults instance use by this fitter """
def __init__(self, qx, qy, qz,
shared_results):
super(Fitter, self).__init__()
......@@ -42,6 +55,18 @@ class Fitter(object):
self._qz = qz
def fit(self, i_fit, i_cube, qx_profile, qy_profile, qz_profile):
"""
Performs the fit and stores the result in this instance's
FitSharedResult object.
:param i_fit: index of the fit result (in the FitH5 file). This
index is to be used when storing the result in the FitSharedResult
instance.
:param i_cube: index of the cube (in the QSpaceH5 file)
:param qx_profile: qx profile
:param qy_profile: qy profile
:param qz_profile: qz profile
:return:
"""
raise NotImplementedError('Not implemented.')
......
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__authors__ = ["D. Naudet"]
__license__ = "MIT"
__date__ = "01/05/2017"
......@@ -35,7 +35,7 @@ from collections import OrderedDict
import numpy as np
from ..io.FitH5 import FitH5Writer, FitH5QAxis
from ...io.FitH5 import FitH5Writer, FitH5QAxis
class FitStatus(object):
......
......@@ -39,10 +39,12 @@ import multiprocessing.sharedctypes as mp_sharedctypes
import numpy as np
# from silx.math import curve_fit
from ..io import QSpaceH5
from ..fit import GaussianFitter, CentroidFitter, SilxFitter
from .sharedresults import (FitTypes, GaussianResults,
CentroidResults, SilxResults)
from ...io import QSpaceH5
from ...fit import (GaussianFitter, GaussianResults,
CentroidFitter, CentroidResults,
SilxFitter, SilxResults,
MaxFitter, MaxResults)
from .sharedresults import FitTypes
disp_times = False
......@@ -194,6 +196,9 @@ class PeakFitter(Thread):
elif fit_type == FitTypes.CENTROID:
fit_class = CentroidFitter
shared_results = CentroidResults(n_points=n_indices)
elif fit_type == FitTypes.MAX:
fit_class = MaxFitter
shared_results = MaxResults(n_points=n_indices)
elif fit_type == FitTypes.SILX:
fit_class = SilxFitter
n_peaks = n_peaks if n_peaks >= 1 else 1
......
......@@ -40,8 +40,8 @@ from .fitresults import FitResult
class FitTypes(object):
ALLOWED = range(3)
GAUSSIAN, CENTROID, SILX = ALLOWED
ALLOWED = range(4)
GAUSSIAN, CENTROID, MAX, SILX = ALLOWED
class FitSharedResults(object):
......@@ -138,18 +138,44 @@ class FitSharedResults(object):
dtype=np.int8)
def set_qx_results(self, fit_idx, results, status):
"""
Sets a result value for qx at index fit_idx
:param fit_idx:
:param results:
:param status:
:return:
"""
self._npy_qx_results[fit_idx] = results
self._npy_qx_status[fit_idx] = status
def set_qy_results(self, fit_idx, results, status):
"""
Sets a result value for qy at index fit_idx
:param fit_idx:
:param results:
:param status:
:return:
"""
self._npy_qy_results[fit_idx] = results
self._npy_qy_status[fit_idx] = status
def set_qz_results(self, fit_idx, results, status):
"""
Sets a result value for qz at index fit_idx
:param fit_idx:
:param results:
:param status:
:return:
"""
self._npy_qz_results[fit_idx] = results
self._npy_qz_status[fit_idx] = status
def local_copy(self):
"""
Returns a local (local to the thread this instance in running in) copy
of FitSharedResults.
:return:
"""
shared_results = (self._shared_qx_results,
self._shared_qy_results,
self._shared_qz_results)
......@@ -163,171 +189,14 @@ class FitSharedResults(object):
shared_status=shared_status)
def fit_results(self, *args, **kwargs):
"""
Returns the fit results.
:param args:
:param kwargs:
:return: FitResult instance
"""
raise NotImplementedError('')
class GaussianResults(FitSharedResults):
def __init__(self,
n_points=None,
n_peaks=1,
shared_results=None,
shared_status=None):
super(GaussianResults, self).__init__(n_points=n_points,
n_params=3,
n_peaks=n_peaks,
shared_results=shared_results,
shared_status=shared_status)
def fit_results(self, *args, **kwargs):
qx_results = self._npy_qx_results
qy_results = self._npy_qy_results
qz_results = self._npy_qz_results
qx_status = self._npy_qx_status
qy_status = self._npy_qy_status
qz_status = self._npy_qz_status
fit_name = 'Gaussian'
results = FitResult(fit_name, *args, **kwargs)
for i_peak in range(self._n_peaks):
peak_name = 'gauss_{0}'.format(i_peak)