"""
Adapters for fitting module
"""
import copy
import numpy
import math
from sas.data_util.uncertainty import Uncertainty
from sas.plottools.plottables import Data1D as PlotData1D
from sas.plottools.plottables import Data2D as PlotData2D
from sas.plottools.plottables import Theory1D as PlotTheory1D
from sas.dataloader.data_info import Data1D as LoadData1D
from sas.dataloader.data_info import Data2D as LoadData2D
[docs]class Data1D(PlotData1D, LoadData1D):
"""
"""
def __init__(self, x=None, y=None, dx=None, dy=None):
"""
"""
if x is None:
x = []
if y is None:
y = []
PlotData1D.__init__(self, x, y, dx, dy)
LoadData1D.__init__(self, x, y, dx, dy)
self.id = None
self.list_group_id = []
self.group_id = None
self.is_data = True
self.path = None
self.xtransform = None
self.ytransform = None
self.title = ""
self.scale = None
[docs] def copy_from_datainfo(self, data1d):
"""
copy values of Data1D of type DataLaoder.Data_info
"""
self.x = copy.deepcopy(data1d.x)
self.y = copy.deepcopy(data1d.y)
self.dy = copy.deepcopy(data1d.dy)
if hasattr(data1d, "dx"):
self.dx = copy.deepcopy(data1d.dx)
if hasattr(data1d, "dxl"):
self.dxl = copy.deepcopy(data1d.dxl)
if hasattr(data1d, "dxw"):
self.dxw = copy.deepcopy(data1d.dxw)
self.xaxis(data1d._xaxis, data1d._xunit)
self.yaxis(data1d._yaxis, data1d._yunit)
self.title = data1d.title
def __str__(self):
"""
print data
"""
_str = "%s\n" % LoadData1D.__str__(self)
return _str
def _perform_operation(self, other, operation):
"""
"""
# First, check the data compatibility
dy, dy_other = self._validity_check(other)
result = Data1D(x=[], y=[], dx=None, dy=None)
result.clone_without_data(length=len(self.x), clone=self)
result.copy_from_datainfo(data1d=self)
if self.dxw == None:
result.dxw = None
else:
result.dxw = numpy.zeros(len(self.x))
if self.dxl == None:
result.dxl = None
else:
result.dxl = numpy.zeros(len(self.x))
for i in range(len(self.x)):
result.x[i] = self.x[i]
if self.dx is not None and len(self.x) == len(self.dx):
result.dx[i] = self.dx[i]
if self.dxw is not None and len(self.x) == len(self.dxw):
result.dxw[i] = self.dxw[i]
if self.dxl is not None and len(self.x) == len(self.dxl):
result.dxl[i] = self.dxl[i]
a = Uncertainty(self.y[i], dy[i]**2)
if isinstance(other, Data1D):
b = Uncertainty(other.y[i], dy_other[i]**2)
if other.dx is not None:
result.dx[i] *= self.dx[i]
result.dx[i] += (other.dx[i]**2)
result.dx[i] /= 2
result.dx[i] = math.sqrt(result.dx[i])
if result.dxl is not None and other.dxl is not None:
result.dxl[i] *= self.dxl[i]
result.dxl[i] += (other.dxl[i]**2)
result.dxl[i] /= 2
result.dxl[i] = math.sqrt(result.dxl[i])
else:
b = other
output = operation(a, b)
result.y[i] = output.x
result.dy[i] = math.sqrt(math.fabs(output.variance))
return result
def _perform_union(self, other):
"""
"""
# First, check the data compatibility
self._validity_check_union(other)
result = Data1D(x=[], y=[], dx=None, dy=None)
tot_length = len(self.x) + len(other.x)
result = self.clone_without_data(length=tot_length, clone=result)
if self.dy == None or other.dy is None:
result.dy = None
else:
result.dy = numpy.zeros(tot_length)
if self.dx == None or other.dx is None:
result.dx = None
else:
result.dx = numpy.zeros(tot_length)
if self.dxw == None or other.dxw is None:
result.dxw = None
else:
result.dxw = numpy.zeros(tot_length)
if self.dxl == None or other.dxl is None:
result.dxl = None
else:
result.dxl = numpy.zeros(tot_length)
result.x = numpy.append(self.x, other.x)
#argsorting
ind = numpy.argsort(result.x)
result.x = result.x[ind]
result.y = numpy.append(self.y, other.y)
result.y = result.y[ind]
if result.dy != None:
result.dy = numpy.append(self.dy, other.dy)
result.dy = result.dy[ind]
if result.dx is not None:
result.dx = numpy.append(self.dx, other.dx)
result.dx = result.dx[ind]
if result.dxw is not None:
result.dxw = numpy.append(self.dxw, other.dxw)
result.dxw = result.dxw[ind]
if result.dxl is not None:
result.dxl = numpy.append(self.dxl, other.dxl)
result.dxl = result.dxl[ind]
return result
[docs]class Theory1D(PlotTheory1D, LoadData1D):
"""
"""
def __init__(self, x=None, y=None, dy=None):
"""
"""
if x is None:
x = []
if y is None:
y = []
PlotTheory1D.__init__(self, x, y, dy)
LoadData1D.__init__(self, x, y, dy)
self.id = None
self.list_group_id = []
self.group_id = None
self.is_data = True
self.path = None
self.xtransform = None
self.ytransform = None
self.title = ""
self.scale = None
[docs] def copy_from_datainfo(self, data1d):
"""
copy values of Data1D of type DataLaoder.Data_info
"""
self.x = copy.deepcopy(data1d.x)
self.y = copy.deepcopy(data1d.y)
self.dy = copy.deepcopy(data1d.dy)
if hasattr(data1d, "dx"):
self.dx = copy.deepcopy(data1d.dx)
if hasattr(data1d, "dxl"):
self.dxl = copy.deepcopy(data1d.dxl)
if hasattr(data1d, "dxw"):
self.dxw = copy.deepcopy(data1d.dxw)
self.xaxis(data1d._xaxis, data1d._xunit)
self.yaxis(data1d._yaxis, data1d._yunit)
self.title = data1d.title
def __str__(self):
"""
print data
"""
_str = "%s\n" % LoadData1D.__str__(self)
return _str
def _perform_operation(self, other, operation):
"""
"""
# First, check the data compatibility
dy, dy_other = self._validity_check(other)
result = self.clone_without_data(len(self.x))
result.copy_from_datainfo(data1d=self)
if self.dxw == None:
result.dxw = None
else:
result.dxw = numpy.zeros(len(self.x))
if self.dxl == None:
result.dxl = None
else:
result.dxl = numpy.zeros(len(self.x))
for i in range(numpy.size(self.x)):
result.x[i] = self.x[i]
if self.dx is not None and len(self.x) == len(self.dx):
result.dx[i] = self.dx[i]
if self.dxw is not None and len(self.x) == len(self.dxw):
result.dxw[i] = self.dxw[i]
if self.dxl is not None and len(self.x) == len(self.dxl):
result.dxl[i] = self.dxl[i]
a = Uncertainty(self.y[i], dy[i]**2)
if isinstance(other, Data1D):
b = Uncertainty(other.y[i], dy_other[i]**2)
if other.dx is not None:
result.dx[i] *= self.dx[i]
result.dx[i] += (other.dx[i]**2)
result.dx[i] /= 2
result.dx[i] = math.sqrt(result.dx[i])
if result.dxl is not None and other.dxl is not None:
result.dxl[i] *= self.dxl[i]
other.dxl[i] += (other.dxl[i]**2)
result.dxl[i] /= 2
result.dxl[i] = math.sqrt(result.dxl[i])
if result.dxw is not None and self.dxw is not None:
result.dxw[i] *= self.dxw[i]
other.dxw[i] += (other.dxw[i]**2)
result.dxw[i] /= 2
result.dxw[i] = math.sqrt(result.dxw[i])
else:
b = other
output = operation(a, b)
result.y[i] = output.x
result.dy[i] = math.sqrt(math.fabs(output.variance))
return result
def _perform_union(self, other):
"""
"""
# First, check the data compatibility
self._validity_check_union(other)
result = Data1D(x=[], y=[], dx=None, dy=None)
tot_length = len(self.x)+len(other.x)
result.clone_without_data(length=tot_length, clone=self)
if self.dy == None or other.dy is None:
result.dy = None
else:
result.dy = numpy.zeros(tot_length)
if self.dx == None or other.dx is None:
result.dx = None
else:
result.dx = numpy.zeros(tot_length)
if self.dxw == None or other.dxw is None:
result.dxw = None
else:
result.dxw = numpy.zeros(tot_length)
if self.dxl == None or other.dxl is None:
result.dxl = None
else:
result.dxl = numpy.zeros(tot_length)
result.x = numpy.append(self.x, other.x)
#argsorting
ind = numpy.argsort(result.x)
result.x = result.x[ind]
result.y = numpy.append(self.y, other.y)
result.y = result.y[ind]
if result.dy != None:
result.dy = numpy.append(self.dy, other.dy)
result.dy = result.dy[ind]
if result.dx is not None:
result.dx = numpy.append(self.dx, other.dx)
result.dx = result.dx[ind]
if result.dxw is not None:
result.dxw = numpy.append(self.dxw, other.dxw)
result.dxw = result.dxw[ind]
if result.dxl is not None:
result.dxl = numpy.append(self.dxl, other.dxl)
result.dxl = result.dxl[ind]
return result
[docs]class Data2D(PlotData2D, LoadData2D):
"""
"""
def __init__(self, image=None, err_image=None,
qx_data=None, qy_data=None, q_data=None,
mask=None, dqx_data=None, dqy_data=None,
xmin=None, xmax=None, ymin=None, ymax=None,
zmin=None, zmax=None):
"""
"""
PlotData2D.__init__(self, image=image, err_image=err_image,
xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
zmin=zmin, zmax=zmax, qx_data=qx_data,
qy_data=qy_data)
LoadData2D.__init__(self, data=image, err_data=err_image,
qx_data=qx_data, qy_data=qy_data,
dqx_data=dqx_data, dqy_data=dqy_data,
q_data=q_data, mask=mask)
self.id = None
self.list_group_id = []
self.group_id = None
self.is_data = True
self.path = None
self.xtransform = None
self.ytransform = None
self.title = ""
self.scale = None
[docs] def copy_from_datainfo(self, data2d):
"""
copy value of Data2D of type DataLoader.data_info
"""
self.data = copy.deepcopy(data2d.data)
self.qx_data = copy.deepcopy(data2d.qx_data)
self.qy_data = copy.deepcopy(data2d.qy_data)
self.q_data = copy.deepcopy(data2d.q_data)
self.mask = copy.deepcopy(data2d.mask)
self.err_data = copy.deepcopy(data2d.err_data)
self.x_bins = copy.deepcopy(data2d.x_bins)
self.y_bins = copy.deepcopy(data2d.y_bins)
if data2d.dqx_data is not None:
self.dqx_data = copy.deepcopy(data2d.dqx_data)
if data2d.dqy_data is not None:
self.dqy_data = copy.deepcopy(data2d.dqy_data)
self.xmin = data2d.xmin
self.xmax = data2d.xmax
self.ymin = data2d.ymin
self.ymax = data2d.ymax
if hasattr(data2d, "zmin"):
self.zmin = data2d.zmin
if hasattr(data2d, "zmax"):
self.zmax = data2d.zmax
self.xaxis(data2d._xaxis, data2d._xunit)
self.yaxis(data2d._yaxis, data2d._yunit)
self.title = data2d.title
def __str__(self):
"""
print data
"""
_str = "%s\n" % LoadData2D.__str__(self)
return _str
def _validity_check(self, other):
"""
Checks that the data lengths are compatible.
Checks that the x vectors are compatible.
Returns errors vectors equal to original
errors vectors if they were present or vectors
of zeros when none was found.
:param other: other data set for operation
:return: dy for self, dy for other [numpy arrays]
:raise ValueError: when lengths are not compatible
"""
err_other = None
if isinstance(other, Data2D):
# Check that data lengths are the same
if len(self.data) != len(other.data) or \
len(self.qx_data) != len(other.qx_data) or \
len(self.qy_data) != len(other.qy_data):
msg = "Unable to perform operation: data length are not equal"
raise ValueError, msg
#if len(self.data) < 1:
# msg = "Incompatible data sets: I-values do not match"
# raise ValueError, msg
for ind in range(len(self.data)):
if self.qx_data[ind] != other.qx_data[ind]:
msg = "Incompatible data sets: qx-values do not match"
raise ValueError, msg
if self.qy_data[ind] != other.qy_data[ind]:
msg = "Incompatible data sets: qy-values do not match"
raise ValueError, msg
# Check that the scales match
err_other = other.err_data
if other.err_data == None or \
(len(other.err_data) != len(other.data)):
err_other = numpy.zeros(len(other.data))
# Check that we have errors, otherwise create zero vector
err = self.err_data
if self.err_data == None or \
(len(self.err_data) != len(self.data)):
err = numpy.zeros(len(other.data))
return err, err_other
def _validity_check_union(self, other):
"""
Checks that the data lengths are compatible.
Checks that the x vectors are compatible.
Returns errors vectors equal to original
errors vectors if they were present or vectors
of zeros when none was found.
:param other: other data set for operation
:return: bool
:raise ValueError: when data types are not compatible
"""
if not isinstance(other, Data2D):
msg = "Unable to perform operation: different types of data set"
raise ValueError, msg
return True
def _perform_operation(self, other, operation):
"""
Perform 2D operations between data sets
:param other: other data set
:param operation: function defining the operation
"""
# First, check the data compatibility
dy, dy_other = self._validity_check(other)
result = Data2D(image=None, qx_data=None, qy_data=None,
q_data=None, err_image=None, xmin=None, xmax=None,
ymin=None, ymax=None, zmin=None, zmax=None)
result.clone_without_data(len(self.data))
result.copy_from_datainfo(data2d=self)
result.xmin = self.xmin
result.xmax = self.xmax
result.ymin = self.ymin
result.ymax = self.ymax
if self.dqx_data == None or self.dqy_data == None:
result.dqx_data = None
result.dqy_data = None
else:
result.dqx_data = numpy.zeros(len(self.data))
result.dqy_data = numpy.zeros(len(self.data))
for i in range(numpy.size(self.data)):
result.data[i] = self.data[i]
if self.err_data is not None and \
numpy.size(self.data) == numpy.size(self.err_data):
result.err_data[i] = self.err_data[i]
if self.dqx_data is not None:
result.dqx_data[i] = self.dqx_data[i]
if self.dqy_data is not None:
result.dqy_data[i] = self.dqy_data[i]
result.qx_data[i] = self.qx_data[i]
result.qy_data[i] = self.qy_data[i]
result.q_data[i] = self.q_data[i]
result.mask[i] = self.mask[i]
a = Uncertainty(self.data[i], dy[i]**2)
if isinstance(other, Data2D):
b = Uncertainty(other.data[i], dy_other[i]**2)
if other.dqx_data is not None and \
result.dqx_data is not None:
result.dqx_data[i] *= self.dqx_data[i]
result.dqx_data[i] += (other.dqx_data[i]**2)
result.dqx_data[i] /= 2
result.dqx_data[i] = math.sqrt(result.dqx_data[i])
if other.dqy_data is not None and \
result.dqy_data is not None:
result.dqy_data[i] *= self.dqy_data[i]
result.dqy_data[i] += (other.dqy_data[i]**2)
result.dqy_data[i] /= 2
result.dqy_data[i] = math.sqrt(result.dqy_data[i])
else:
b = other
output = operation(a, b)
result.data[i] = output.x
result.err_data[i] = math.sqrt(math.fabs(output.variance))
return result
def _perform_union(self, other):
"""
Perform 2D operations between data sets
:param other: other data set
:param operation: function defining the operation
"""
# First, check the data compatibility
self._validity_check_union(other)
result = Data2D(image=None, qx_data=None, qy_data=None,
q_data=None, err_image=None, xmin=None, xmax=None,
ymin=None, ymax=None, zmin=None, zmax=None)
length = len(self.data)
tot_length = length + len(other.data)
result.clone_without_data(tot_length)
result.xmin = self.xmin
result.xmax = self.xmax
result.ymin = self.ymin
result.ymax = self.ymax
if self.dqx_data == None or self.dqy_data == None or \
other.dqx_data == None or other.dqy_data == None :
result.dqx_data = None
result.dqy_data = None
else:
result.dqx_data = numpy.zeros(len(self.data) + \
numpy.size(other.data))
result.dqy_data = numpy.zeros(len(self.data) + \
numpy.size(other.data))
result.data = numpy.append(self.data, other.data)
result.qx_data = numpy.append(self.qx_data, other.qx_data)
result.qy_data = numpy.append(self.qy_data, other.qy_data)
result.q_data = numpy.append(self.q_data, other.q_data)
result.mask = numpy.append(self.mask, other.mask)
if result.err_data is not None:
result.err_data = numpy.append(self.err_data, other.err_data)
if self.dqx_data is not None:
result.dqx_data = numpy.append(self.dqx_data, other.dqx_data)
if self.dqy_data is not None:
result.dqy_data = numpy.append(self.dqy_data, other.dqy_data)
return result
[docs]def check_data_validity(data):
"""
Return True is data is valid enough to compute chisqr, else False
"""
flag = True
if data is not None:
if issubclass(data.__class__, Data2D):
if (data.data is None) or (len(data.data) == 0)\
or (len(data.err_data) == 0):
flag = False
else:
if (data.y is None) or (len(data.y) == 0):
flag = False
if not data.is_data:
flag = False
else:
flag = False
return flag