3. dataList

dataList contain a list of dataArray.

  • Subclass of list as lists of dataArrays (allowing variable sizes).
  • Basic list routines as read/save, appending, selection, filter, sort, prune, interpolate, spline…
  • Multidimensional least square fit that uses the attributes of the dataArray elements.
  • Read/Write in human readable ASCII text of multiple files in one run (gzip possible) or pickle.
  • A file may contain several datasets and several files can be read.

For Beginners:

  • Create a dataList and use the methods from this object in point notations.:

    data=js.dL('filename.dat').
    data.prune(number=100)
    data.attr
    data.save('newfilename.dat')
    
  • The dataList methods should not be used directly from this module.

See dataList for details.

Example:

p=js.grace()
dlist2=js.dL()
x=np.r_[0:10:0.5]
D,A,q=0.45,0.99,1.2
for q in np.r_[0.1:2:0.2]:
   dlist2.append(js.dA(np.vstack([x,np.exp(-q**2*D*x),np.random.rand(len(x))*0.05])) )
   dlist2[-1].q=q
p.clear()
p.plot(dlist2,legend='Q=$q')
p.legend()
dlist2.save('test.dat.gz')

The dataarray module can be run standalone in a new project.

3.1. dataList Class

dataList([objekt, block, usecols, …]) A list of dataArrays with attributes for analysis, fitting and plotting.
  • dataList creating by dataL=js.dL(‘filename.dat’) or from numpy arrays.
  • List columns can be accessed as automatic generated attributes like .X,.Y,.eY (see protectedNames). or by indexing as *dataL[:,0] -> .X * for all list elements.
  • Corresponding column indices are set by setColumnIndex() (default X,Y,eY = 0,1,2).
  • Multidimensional fitting of 1D,2D,3D (.X,.Z,.W) data including additional attributes. .Y (scalar) are used as function values at coordinates.
  • Attributes can be set like: dataL.aName= 1.2345 or dataL[2].aName= 1.2345
  • Individual elements and dataArray methods can be accessed by indexing data[2].bName
  • Methods are used as dataL.methodname(arguments)

3.2. Attribute Methods

attr Returns all attribute names (including commonAttr of elements) of the dataList.
commonAttr Returns list of attribute names existing in elements.
dtype return dtype of elements
names List of element names.
whoHasAttributes Lists which attribute is found in which element.
showattr([maxlength, exclude]) Show data specific attributes for all elements.

3.3. Fit Methods

fit(model[, freepar, fixpar, mapNames, …]) Least square fit of model that minimizes \chi^2 (uses scipy.optimize methods).
modelValues(**kwargs) Calculates modelValues of model after a fit.
setLimit(**kwargs) Set upper and lower limits for parameters in least square fit.
hasLimit Return existing limits
setConstrain(*args) Set inequality constrains for constrained minimization in fit.
hasConstrain Return list with defined constrained source code.
makeErrPlot([title, showfixpar]) Creates a GracePlot for intermediate output from fit with residuals.
makeNewErrPlot(**kwargs) Creates a NEW ErrPlot without destroying the last.
detachErrPlot() Detaches ErrPlot without killing it and returns a reference to it.
killErrPlot([filename]) Kills ErrPlot
showlastErrPlot([title, modelValues]) Shows last ErrPlot as created by makeErrPlot with last fit result.
errPlot(*args, **kwargs) Plot into an existing ErrPlot.
savelastErrPlot(filename[, format, size, dpi]) Saves errplot to file with filename.
interpolate([func, invfunc, deg]) 2D interpolation at new attribute and .X values.
polyfit([func, invfunc, xfunc, invxfunc, exfunc]) Inter/Extrapolated values along attribut for all given X values using a polyfit.
extrapolate([func, invfunc, xfunc, …]) Inter/Extrapolated values along attribut for all given X values using a polyfit.
bispline([func, invfunc, tx, ta, deg, eps, …]) Weighted least-squares bivariate spline approximation for interpolation of Y at given attribute values for X values.

3.4. Housekeeping Methods

setColumnIndex(*arg, **kwargs) Set the columnIndex where to find X,Y,Z,W eY, eX, eZ…..
append([objekt, index, usecols, skiplines, …]) Reads/creates new dataArrays and appends to dataList.
extend([objekt, index, usecols, skiplines, …]) Reads/creates new dataArrays and appends to dataList.
insert(i[, objekt, index, usecols, …]) Reads/creates new dataArrays and inserts in dataList.
prune(*args, **kwargs) Reduce number of values between upper and lower limits.
savetxt([name, exclude, fmt]) Saves dataList as ASCII text file, optional compressed (gzip).
sort([key, reverse]) Sort dataList -> INPLACE!!!
reverse() Reverse dataList -> INPLACE!!! original doc from list L.reverse() – reverse IN PLACE
delete(index) Delete element at index
extractAttribut(parName[, func, newParName]) Extract a simpler attribute from a complex attribute in each element of dataList.
filter([filterfunction]) Filter elements according to filterfunction and kwargs.
index(value[, start, stop]) original doc from list L.index(value, [start, [stop]]) -> integer – return first index of value.
merge(indices[, isort]) Merges elements of dataList.
mergeAttribut(parName[, limit, isort, func]) Merges elements of dataList if attribute values are closer than limit (in place).
pop([i]) original doc from list L.pop([index]) -> item – remove and return item at index (default last).
copyattr2elements([maxndim, exclude]) Copy dataList specific attributes to all elements.
getfromcomment(attrname) Extract a non number parameter from comment with attrname in front
transposeAttribute(attr) Use attribute as new X axis (like transpose .X and attribute).

class jscatter.dataarray.dataList(objekt=None, block=None, usecols=None, delimiter=None, takeline=None, index=slice(None, None, None), replace=None, skiplines=None, ignore='#', XYeYeX=None, lines2parameter=None, encoding=None)[source]

Bases: jscatter.dataarray.dataListBase

A list of dataArrays with attributes for analysis, fitting and plotting.

  • Allows reading, appending, selection, filter, sort, prune, least square fitting, ….
  • Saves to human readable ASCII text format (possibly gziped). For file format see dataArray.
  • The dataList allows simultaneous fit of all dataArrays dependent on attributes.
  • and with different parameters for the dataArrays (see fit).
  • dataList creation parameters (below) mainly determine how a file is read from file.
  • .Y are used as function values at coordinates [.X,.Z,.W] in fitting.
Parameters:
objekt : strings, list of array or dataArray
Objects or filename(s) to read.
  • Filenames with extension ‘.gz’ are decompressed (gzip).
  • Filenames with asterisk like exda=dataList(objekt=’aa12*’) as input for multiple files.
  • An in-memory stream for text I/O (Python3 -> io.StringIO, Python2.7 -> StringIO ).
lines2parameter : list of integer

List of lines i which to prepend with ‘line_i’ to be found as parameter line_i. Used to mark lines with parameters without name (only numbers in a line as in .pdh files in the header). E.g. to skip the first lines.

ignore : string, default ‘#’

Ignore lines starting with string e.g. ‘#’.

takeline : string,list of string, function

Filter lines to be included (all lines) e.g. to select line starting with ‘ATOM’. Should be combined with: replace (replace starting word by number {‘ATOM’:1} to be detected as data) and usecols to select the needed columns. Examples (function gets words in line):

  • lambda words: any(w in words for w in [‘ATOM’,’CA’]) # one of both words somewhere in line
  • lambda w: (w[0]==’ATOM’) & (w[2]==’CA’) # starts with ‘ATOM’ and third is ‘CA’

For word or list of words first example is generated automatically.

replace : dictionary of [string,regular expression object]:string

String replacement in read lines as {‘old’:’new’,…} (after takeline). String pairs in this dictionary are replaced in each line. This is done prior to determining line type and can be used to convert strings to number or ‘,’:’.’. If dict key is a regular expression object (e.g. rH=re.compile(‘Hd+’) ),it is replaced by string. See python module re for syntax.

skiplines : boolean function, list of string or single string

Skip if line meets condition (only data lines). Function gets the list of words in a data line. Examples:

  • lambda words: any(w in words for w in [‘’,’ ‘,’NAN’,’‘*]) #with exact match
  • lambda words: any(float(w)>3.1411 for w in words)
  • lambda words: len(words)==1

If a list is given, the lambda function is generated automatically as in above example. If single string is given, it is tested if string is a substring of a word ( ‘abc’ in ‘123abc456’)

usecols : list of integer

Use only given columns and ignore others (after skiplines).

block : string,slice (or slice indices), default None
Indicates separation of dataArray in file if multiple are present.
  • None : Auto detection of blocks according to change between datalines and non-datalines.
    A new dataArray is created if data and attributes are present.
  • string : If block is found at beginning of line a new dataArray is created and appended. block can be something like “next” or the first parameter name of a new block as block=’Temp’
  • slice or slice indices : block=slice(2,100,3) slices the filelines in file as lines[i:j:k] . If only indices are given these are converted to slice.
index : integer, slice list of integer, default is a slice for all.

Selects which dataArray to use from read file if multiple are found. Can be integer , list of integer or slice notation.

XYeYeX : list integers, default=[0,1,2,None,None,None]

Sets columns for X, Y, eY, eX, Z, eZ. This is ignored for dataList and dataArray objects as these have defined columns. Change later by: data.setColumnIndex .

delimiter : string, default any whitespace

Separator between words (data fields) in a line. E.g. ‘ ‘ tabulator

encoding : None, ‘utf-8’, ‘cp1252’, ‘ascii’,…

The encoding of the files read. By default the system default encoding is used. Others: python2.7 ‘ascii’, python3 ‘utf-8’ For files written on Microsoft Windows use ‘cp1252’ (US),’cp1251’ (with German öäüß) ‘latin-1’ codes also the first 256 ascii characters correctly.

Returns:
dataList : list of dataArray

Notes

Attribute access as atlist
Attributes of the dataArray elements can be accessed like in dataArrays by .name notation. The difference is that a dataList returns atlist -a subclass of list- with some additional methods as the list of attributes in the dataList elements. This is necessary as it is allowed that dataList elements miss an attribute (indicated as None) or have different type. An numpy ndarray can be retrieved by the array property (as .name.array).
Global attributes
We have to discriminate attributes stored individual in each dataArray and in the dataList as a kind of global attribute. dataArray attributes belong to a dataArray and are saved with the dataArray, while global dataList attributes are only saved with the whole dataList at the beginning of a file. If dataArrays are saved as single files global attributes are lost.

Examples

import jscatter as js
ex=js.dL('aa12*')        #read aa files
ex.extend('bb12*')      #extend with other bb files
ex.sort(...)            #sort by attribute "q"
ex.prune(number=100)    # reduce number of points; default is to calc the mean in an interval
ex.filter(lambda a:a.Temperature>273)  to filter for an attribute "Temperature" or .X.mean() value
# do linear fit
ex.fit(model=lambda a,b,t:a*t+b,freepar={'a':1,'b':0},mapNames={'t':'X'})
# fit using parameters in example the Temperature stored as parameter.
ex.fit(model=lambda Temperature,b,x:Temperature*x+b,freepar={'b':0},mapNames={'x':'X'})
import jscatter as js
import numpy as np
t=np.r_[1:100:5];D=0.05;amp=1
# using list comprehension creating a numpy array
i5=js.dL([np.c_[t,amp*np.exp(-q*q*D*t),np.ones_like(t)*0.05].T for q in np.r_[0.2:2:0.4]])
# calling a function returning dataArrays
i5=js.dL([js.dynamic.simpleDiffusion(q,t,amp,D) for q in np.r_[0.2:2:0.4]])
# define a function and add dataArrays to dataList
ff=lambda q,D,t,amp:np.c_[t,amp*np.exp(-q*q*D*t),np.ones_like(t)*0.05].T
i5=js.dL()  # empty list
for q in np.r_[0.2:2:0.4]:
   i5.append(ff(q,D,t,amp))

Get elements of dataList with specific attribute values.

i5=js.dL([js.dynamic.simpleDiffusion(q,t,amp,D) for q in np.r_[0.2:2:0.4]])
# get q=0.6
i5[i5.q.array==0.6]
# get q > 0.5
i5[i5.q.array > 0.5]

Rules for reading of ASCII files

How files are interpreted :

Reads simple formats as tables with rows and columns like numpy.loadtxt.
The difference is how to treat additional information like attributes or comments and non float data.

Line format rules: A dataset consists of comments, attributes and data (and optional other datasets).

First two words in a line decide what it is:
  • string + value -> attribute with attribute name and list of values
  • string + string -> comment ignore or convert to attribute by getfromcomment
  • value + value -> data line of an array; in sequence without break, input for the ndarray
  • single words -> are appended to comment
  • string+@unique_name-> link to other dataArray with unique_name

Even complex ASCII file can be read with a few changes as options.

Datasets are given as blocks of attributes and data.

A new dataArray is created if:

  • a data block with a parameter block (preceded or appended) is found.
  • a keyword as first word in line is found: - Keyword can be eg. the name of the first parameter. - Blocks are separated as start or end of a number data block (like a matrix). - It is checked if parameters are prepended or append to the datablock. - If both is used, set block to the first keyword in first line of new block (name of the first parameter).

Example of an ASCII file with attributes temp, pressure, name:

this is just a comment or description of the data
temp     293
pressure 1013 14
name     temp1bsa
0.854979E-01  0.178301E+03  0.383044E+02
0.882382E-01  0.156139E+03  0.135279E+02
0.909785E-01  0.150313E+03  0.110681E+02
0.937188E-01  0.147430E+03  0.954762E+01
0.964591E-01  0.141615E+03  0.846613E+01
0.991995E-01  0.141024E+03  0.750891E+01
0.101940E+00  0.135792E+03  0.685011E+01
0.104680E+00  0.140996E+03  0.607993E+01

this is just a second comment
temp     393
pressure 1011 12
name     temp2bsa
0.236215E+00  0.107017E+03  0.741353E+00
0.238955E+00  0.104532E+03  0.749095E+00
0.241696E+00  0.104861E+03  0.730935E+00
0.244436E+00  0.104052E+03  0.725260E+00
0.247176E+00  0.103076E+03  0.728606E+00
0.249916E+00  0.101828E+03  0.694907E+00
0.252657E+00  0.102275E+03  0.712851E+00
0.255397E+00  0.102052E+03  0.702520E+00
0.258137E+00  0.100898E+03  0.690019E+00

optional:

  • string + @name: Link to other data in same file with name given as “name”. Content of @name is used as identifier. Think of an attribute with 2dim data.

Reading of complex files with filtering of specific information To read something like a pdb structure file with lines like

...
ATOM      1  N   LYS A   1       3.246  10.041  10.379  1.00  5.28           N
ATOM      2  CA  LYS A   1       2.386  10.407   9.247  1.00  7.90           C
ATOM      3  C   LYS A   1       2.462  11.927   9.098  1.00  7.93           C
ATOM      4  O   LYS A   1       2.582  12.668  10.097  1.00  6.28           O
ATOM      5  CB  LYS A   1       0.946   9.964   9.482  1.00  3.54           C
ATOM      6  CG  LYS A   1      -0.045  10.455   8.444  1.00  3.75           C
ATOM      7  CD  LYS A   1      -1.470  10.062   8.818  1.00  2.85           C
ATOM      8  CE  LYS A   1      -2.354   9.922   7.589  1.00  3.83           C
ATOM      9  NZ  LYS A   1      -3.681   9.377   7.952  1.00  1.78           N
...

combine takeline, replace and usecols.

usecols=[6,7,8] selects the columns as x,y,z positions

# select all atoms
xyz = js.dA('3rn3.pdb',takeline=lambda w:w[0]=='ATOM',replace={'ATOM':1},usecols=[6,7,8])
# select only CA atoms
xyz = js.dA('3rn3.pdb',takeline=lambda w:(w[0]=='ATOM') & (w[2]=='CA'),replace={'ATOM':1},usecols=[6,7,8])
# in PDB files different atomic structures are separate my "MODEL","ENDMODEL" lines.
# We might load all by using block
xyz = js.dA('3rn3.pdb',takeline=lambda w:(w[0]=='ATOM') & (w[2]=='CA'),
                       replace={'ATOM':1},usecols=[6,7,8],block='MODEL')
append(objekt=None, index=slice(None, None, None), usecols=None, skiplines=None, replace=None, ignore='#', XYeYeX=None, delimiter=None, takeline=None, lines2parameter=None, encoding=None)

Reads/creates new dataArrays and appends to dataList.

See dataList for description of all keywords. If objekt is dataArray or dataList all options except XYeYeX,index are ignored.

Parameters:
objekt,index,usecols,skiplines,replace,ignore,delimiter,takeline,lines2parameter : options

See dataArray or dataList

original doc from list
L.append(object) – append object to end
aslist

Return as simple list.

attr

Returns all attribute names (including commonAttr of elements) of the dataList.

bispline(func=None, invfunc=None, tx=None, ta=None, deg=[3, 3], eps=None, addErr=False, **kwargs)

Weighted least-squares bivariate spline approximation for interpolation of Y at given attribute values for X values.

Uses scipy.interpolate.LSQBivariateSpline eY values are used as weights (1/eY**2) if present.

Parameters:
kwargs :

Keyword arguments The first keyword argument found as attribute is used for interpolation. E.g. conc=0.12 defines the attribute ‘conc’ to be interpolated to 0.12 Special kwargs see below.

X : array

List of X values were to evaluate. If X not given the .X of first element are used as default.

func : numpy ufunction or lambda

Simple function to be used on Y values before interpolation. see dataArray.polyfit

invfunc : numpy ufunction or lambda

To invert func after extrapolation again.

tx,ta : array like, None, int

Strictly ordered 1-D sequences of knots coordinates for X and attribute. If None the X or attribute values are used. If integer<len(X or attribute) the respective number of equidistant points in the interval between min and max are used.

deg : [int,int], optional

Degrees of the bivariate spline for X and attribute. Default is 3. If single integer given this is used for both.

eps : float, optional

A threshold for determining the effective rank of an over-determined linear system of equations. eps should have a value between 0 and 1, the default is 1e-16.

addErr : bool

If errors are present spline the error column and add it to the result.

Returns:
dataArray

Notes

  • The spline interpolation results in a good approximation if the data are narrow. Around peaks values are underestimated if the data are not dense enough as the flank values are included in the spline between the maxima. See Examples.
  • Without peaks there should be no artifacts.
  • To estimate new errors for the spline data use .setColumnIndex(iy=ii,iey=None) with ii as index of errors. Then spline the errors and add these as new column.
  • Interpolation can not be as good as fitting with a prior known model and use this for extrapolating.

Examples

import jscatter as js
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax1 = fig.add_subplot(211, projection='3d')
ax2 = fig.add_subplot(212, projection='3d')

i5=js.dL([js.formel.gauss(np.r_[-50:50:5],mean,10) for mean in np.r_[-15:15.1:3]])
i5b=i5.bispline(mean=np.r_[-15:15:1],X=np.r_[-25:25:1],tx=10,ta=5)

fig.subtitle('Spline comparison with different spacing of data')
ax1.set_title("Narrow spacing result in good interpolation")
ax1.scatter3D(i5.X.flatten, np.repeat(i5.mean,[x.shape[0] for x in i5.X]), i5.Y.flatten,s=20,c='red')
ax1.scatter3D(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
ax1.tricontour(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)

i5=js.dL([js.formel.gauss(np.r_[-50:50:5],mean,10) for mean in np.r_[-15:15.1:15]])
i5b=i5.bispline(mean=np.r_[-15:15:1],X=np.r_[-25:25:1])

ax2.set_title("Wide spacing result in artifacts between peaks")
ax2.scatter3D(i5.X.flatten, np.repeat(i5.mean,[x.shape[0] for x in i5.X]), i5.Y.flatten,s=20,c='red')
ax2.scatter3D(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
ax2.tricontour(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
plt.show(block=False)
commonAttr

Returns list of attribute names existing in elements.

copy()

Deepcopy of dataList

To make a normal shallow copy use copy.copy

copyattr2elements(maxndim=1, exclude=['comment'])

Copy dataList specific attributes to all elements.

Parameters:
exclude : list of str

List of attr names to exclude from show

maxndim : int, default 2

Maximum dimension e.g. to prevent copy of 2d arrays like covariance matrix

Notes

Main use is for copying fit parameters.

count(value) → integer -- return number of occurrences of value
delete(index)

Delete element at index

detachErrPlot()[source]

Detaches ErrPlot without killing it and returns a reference to it.

dtype

return dtype of elements

errPlot(*args, **kwargs)[source]

Plot into an existing ErrPlot. See Graceplot.plot for details.

errPlotTitle(title)[source]

dummy

errplot

Errplot handle

extend(objekt=None, index=slice(None, None, None), usecols=None, skiplines=None, replace=None, ignore='#', XYeYeX=None, delimiter=None, takeline=None, lines2parameter=None, encoding=None)

Reads/creates new dataArrays and appends to dataList.

See dataList for description of all keywords. If objekt is dataArray or dataList all options except XYeYeX,index are ignored.

Parameters:
objekt,index,usecols,skiplines,replace,ignore,delimiter,takeline,lines2parameter : options

See dataArray or dataList

original doc from list
L.append(object) – append object to end
extractAttribut(parName, func=None, newParName=None)

Extract a simpler attribute from a complex attribute in each element of dataList.

eg. extract the mean value from a list in an attribute

Parameters:
parName : string

Name of the parameter to process

func : function or lambda

A function (eg lambda ) that creates a new content for the parameter from the original content eg lambda a:np.mean(a)*5.123 The function gets the content of parameter whatever it is

newParName :string

New parname, if None old parameter is overwritten.

extrapolate(func=None, invfunc=None, xfunc=None, invxfunc=None, exfunc=None, **kwargs)

Inter/Extrapolated values along attribut for all given X values using a polyfit.

To extrapolate along an attribute using twice a polyfit (first along X then along attribute). E.g. from a concentration series to extrapolate to concentration zero.

Parameters:
**kwargs :

Keyword arguments The first keyword argument found as attribute is used for extrapolation e.g. q=0.01 attribute with values where to extrapolate to Special kwargs see below.

X : arraylike

list of X values were to evaluate

func : function or lambda

Function to be used in Y values before extrapolating. See Notes.

invfunc : function or lambda

To invert function after extrapolation again.

xfunc : function or lambda

Function to be used for X values before interpolating along X.

invxfunc : function or lambda

To invert xfunction again.

exfunc : function or lambda

Weight for extrapolating along X

degx,degy : integer default degx=0, degy=1

polynom degree for extrapolation in x,y If degx=0 (default) no extrapolation for X is done and values are linear interpolated.

Returns:
dataArray

Notes

funct/invfunc is used to transfer the data to a simpler smoother or polynominal form.

  • Think about data describing diffusion like I=exp(-q^2Dt) and we want to interpolate along attribute q. If funct is np.log we interpolate on a simpler parabolic q**2 and linear in t.
  • Same can be done with X axis e.g for subdiffusion I=exp(-q^2Dt^a) \ with \ a < 1.

Examples

# Task: Extrapolate to zero q for 3 X values for an exp decaying function.
# Here first log(Y) is used (problem linearized), then linear extrapolate and exp function used for the result.
# This is like lin extrapolation of the exponent.
#
i5.polyfit(q=0,X=[0,1,11],func=lambda y:np.log(y),invfunc=lambda y:np.exp(y),deg=1)
#
# Concentration data with conc and extrapolate to conc=0.
data.polyfit(conc=0,X=data[0].X,deg=1)
#
# Interpolate for specified X and a list of attributes. ::
i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')
i5.polyfit(X=np.r_[1:5.1],q=i5.q)
filter(filterfunction=None, **kwargs)

Filter elements according to filterfunction and kwargs.

Parameters:
filterfunction : function or lambda function returning boolean

Return those items of sequence for which function(item) is true.

kwargs :

Any given keyword with value is combined with filterfunction (logical AND).

Examples

i5=js.dL('exampleData/iqt_1hho.dat')
i1=i5.filter(lambda a:a.q>0.1)
i1=i5.filter(lambda a:(a.q>0.1) )
i5.filter(lambda a:(a.q>0.1) & (a.average[0]>1)).average
i5.filter(lambda a:(max(a.q*a.X)>0.1) & (a.average[0]>1))
# with kwargs
i5.filter(q=0.5,conc=1)
fit(model, freepar={}, fixpar={}, mapNames={}, method='leastsq', xslice=slice(None, None, None), condition=None, output=True, **kw)

Least square fit of model that minimizes \chi^2 (uses scipy.optimize methods).

  • A least square fit of scalar .Y values dependent on coordinates (X,Z,W) and attributes (multidimensional fitting).
  • Data attributes are used automatically in model if they have the same name as a parameter.
  • Resulting errors are 1-sigma errors as estimated from covariance matrix diagonal.
  • Results can be simulated with changed parameters in .modelValues or .showlastErrPlot.
Parameters:
model : function or lambda
Model function, should accept arrays as input (use numpy ufunctions).
  • Return value should be dataArray (.Y is used) or only Y values.
  • Failed model evaluation should return single negative integer.

Example ( see How to build simple models ):

diffusion=lambda A,D,t,wavevector: A*np.exp(-wavevector**2*D*t)
freepar : dictionary
Fit parameter names with start values.
  • {'D':2.56,..} float, one common value for all
  • {'D':[1,2.3,4.5,...],..} list of float, individual parameters for independent fit.
  • [..] is extended with missing values equal to last given value. [2,1] -> [2,1,1,1,1,1]
  • It is sufficient to add [] around a float to switch between common value and independent fit values.
fixpar : dictionary

Fixed parameters (see freepar for syntax). Overwrites data attributes with same name.

mapNames : dictionary

Map parameter names from model to attribute names in data e.g. {‘t’:’X’,’wavevector’:’q’,}

method : default ‘leastsq’, ‘differential_evolution’, ‘BFGS’, ‘Nelder-Mead’ or from scipy.optimize.minimize

Type of solver for minimization, See last example below for a speed comparison and usage.

  • ‘leastsq’ is fastest and what you typically expect for fitting. It is a wrapper around MINPACK’s lmdif and lmder algorithms which are a modification of the Levenberg-Marquardt algorithm.
  • Only ‘leastsq’ and ‘BFGS’ return errors for the fit parameters.
  • ‘differential_evolution’ generates automatic bounds around start value x0 as [x0/10**0.5,x0*10**0.5] if no bounds are set for a freepar.
  • All methods use bounds set by .setlimits to allow bounds as described there.
  • For detailed options see scipy.optimize. For some methods the Jacobian is required.
xslice : slice object

Select values by slicing

xslice=slice(2,-3,2)       To skip first 2,last 3 and take each second.
condition : function or lambda
A lambda function to determine which datapoints to include.
  • The function should evaluate to boolean with dataArray as input and combines with xslice used on full set (first xslice then the condition is used)

  • local operation on numpy arrays as “&”(and), “|”(or), “^”(xor)

    lambda a:(a.X>1) & (a.Y<1)
    lambda a:(a.X>1) & (a.X<100)
    lambda a: a.X>a.q * a.X
    
output : ‘last’,’best’, False, default True
By default write some text messages (fit progress).
  • ‘last’ return lastfit and text messages
  • ‘best’ return best (parameters,errors) and text messages
  • False : No printed output.
debug : 1,2,3
Debug modus returns:
  • 1 Free and fixed parameters but not mappedNames.
  • 2 Fitparameters in modelValues as dict to call model as model(**kwargs) with mappedNames.
  • >2 Prints parameters sent to model and returns the output of model without fitting.
kw : additional keyword arguments

Forwarded to minimizer as given in specified method. See scipy optimize for details.

Returns:
By default no return value.
  • Final results with errors are in .lastfit
  • Fitparameters are additional in dataList object as .parname and corresponding errors as .parname_err.
  • If the fit fails a exception is raised and last parameters are printed. These are not a fit result.

Notes

  • We minimise the unbiased estimate \chi^2 = \frac{1}{n-p} \sum_i \frac{[X_i-f(X_i,a)]^2}{\sigma_i^2} with number of datapoints n, number of parameters p and function f(X_i,a) dependent on parameters a_i.

  • The concept is to use data attributes as fixed parameters for the fit (multidimensional fit). This is realized by using data attribute with same name as fixed parameters if not given in freepar or fixpar.

  • Fit parameters can be set equal for all elements 'par':1 or independent 'par':[1] just by writing the start value as a single float or as a list of float. The same for fixed parameters.

  • Changes between free and fixed parameters is easy done by moving 'par':[1] between freepar and fixpar.

  • Limits for parameters can be set prior to the fit as .setlimit(D=[1,4,0,10]). The first two numbers (min,max) are softlimits (increase \chi^2) and second are hardlimits to avoid extreme values. (hard set to these values if outside interval and increasing \chi^2).

  • If errors exist (.eY) and are not zero, the error weighted \chi^2 is minimized. Without error or with single errors equal zero an unweighted \chi^2 is minimized (equal weights).

  • Resulting parameter errors are 1-sigma errors as determined from the covariance matrix (see [1]). This holds under the assumption that we have a well-behaved likelihood function which is asymptotically Gaussian near its maximum (equal to minimum in \chi^2). Practically, this error is independent of the absolut value of errors in the data as long as the relative contributions are ok. Thus for equal weights error weighted \chi^2 and unweighted \chi^2 the result is a 1- sigma error.

  • The change of parameters can be simulated by .modelValues(D=3) which overrides attributes and fit parameters.

  • .makeErrPlot() creates an errorplot with residuals prior to the fit for intermediate output.

  • The last errPlot can be recreated after the fit with .showlastErrPlot().

  • The simulated data can be shown in errPlot with .showlastErrPlot(D=3).

  • Each dataArray in a dataList can be fit individually (same model function) like this

    # see Examples for dataList creation
    data[3].fit(model,freepar,fixpar,.....)
    #or
    for dat in data:
        dat.fit(model,freepar,fixpar,.....)
    

Additional kwargs for ‘leastsq’

some additional optional arguments passed to leastsq (see scipy.optimize.leastsq)
col_deriv    default  0
ftol         default  1.49e-08    Relative error desired in the sum of squares
xtol         default  1.49e-08    Relative error desired in the approximate solution.
gtol         default  0.0O        Orthogonality desired between function vector and Jacobian.
maxfev       default  200*(N+1)   Maximum model evaluations
factor       default  100         A parameter determining the initial step bound

Fit result attributes

# exda are fitted example data
exda.D                    freepar 'D' ; same for fixpar but no error.
                          use exda.lastfit.attr to see all attributes of model
exda.D_err                1-sigma error of freepar 'D'
# full result in lastfit
exda.lastfit.X            X values in fit model
exda.lastfit.Y            Y values in fit model
exda.lastfit[i].D         free parameter D result in best fit
exda.lastfit[i].D_err     error of free parameter D as 1-sigma error from diagonal in covariance matrix.
exda.lastfit.chi2         chi2 = sum(((.Y-model(.X,best))/.eY)**2)/dof; should be around 1 with proper weight.
exda.lastfit.cov          covariance matrix = hessian**-1 * chi2
exda.lastfit.dof          degrees of freedom = len(y)-len(best)
exda.lastfit.func_name    name of used model
exda.lastfit.func_code    where to find code of used model

References

About error estimate from covariance Matrix with the Fischer matrix F cM_{i,j}=(\frac{\partial^2 log(F) }{ \partial x_i\partial x_j})^{-1}

[1](1, 2) https://arxiv.org/pdf/1009.2755.pdf

Examples

How to make a model: The model function gets .X (.Z, .W .eY, ….) as ndarray and parameters (from attributes and freepar and fixpar) as scalar input. It should return an ndarray as output (only Y values) or dataArray (.Y is used automatically). Therefore it is advised to use numpy ufunctions in the model. Instead of math.sin use numpy.sin, which is achieved by import numpy as np and use np.sin see http://docs.scipy.org/doc/numpy/reference/ufuncs.html

See How to build simple models and How to build a more complex model

A bunch of examples can be found in formel.py, formfactor.py, stucturefactor.py.

Basic examples with synthetic data.

Usually data are loaded from a file. For the following also see 1D fits with attributes and 2D fitting .

  • An error plot with residuals can be created for intermediate output. The model is here a lambda function

    import jscatter as js
    import numpy as np
    data=js.dL(js.examples.datapath+'/iqt_1hho.dat')
    diffusion=lambda t,wavevector,A,D,b:A*np.exp(-wavevector**2*D*t)+b
    data.setlimit(D=(0,2))               # set a limit for diffusion values
    data.makeErrPlot()                   # create errorplot which is updated
    data.fit(model=diffusion ,
         freepar={'D':0.1,               # one value for all (as a first try)
                  'A':[1,2,3]},          # extended to [1,2,3,3,3,3,...3] independent parameters
         fixpar={'b':0.} ,               # fixed parameters here, [1,2,3] possible
         mapNames= {'t':'X',             # maps time t of the model as .X column for the fit.
                    'wavevector':'q'},   # and map model parameter 'wavevector' to data attribute .q
         condition=lambda a:(a.Y>0.1) )  # set a condition
    
  • Fit sine to simulated data. The model is inline lambda function.

    import jscatter as js
    import numpy as np
    x=np.r_[0:10:0.1]
    data=js.dA(np.c_[x,np.sin(x)+0.2*np.random.randn(len(x)),x*0+0.2].T)           # simulate data with error
    data.fit(lambda x,A,a,B:A*np.sin(a*x)+B,{'A':1.2,'a':1.2,'B':0},{},{'x':'X'})  # fit data
    data.showlastErrPlot()                                                         # show fit
    print(  data.A,data.A_err)                                                        # access A and error
    
  • Fit sine to simulated data using an attribute in data with same name

    x=np.r_[0:10:0.1]
    data=js.dA(np.c_[x,1.234*np.sin(x)+0.1*np.random.randn(len(x)),x*0+0.1].T)     # create data
    data.A=1.234                                                                   # add attribute
    data.makeErrPlot()                                                             # makes errorPlot prior to fit
    data.fit(lambda x,A,a,B:A*np.sin(a*x)+B,{'a':1.2,'B':0},{},{'x':'X'})          # fit using .A
    
  • Fit sine to simulated data using an attribute in data with different name and fixed B

    x=np.r_[0:10:0.1]
    data=js.dA(np.c_[x,1.234*np.sin(x)+0.1*np.random.randn(len(x)),x*0+0.1].T)       # create data
    data.dd=1.234                                                                    # add attribute
    data.fit(lambda x,A,a,B:A*np.sin(a*x)+B,{'a':1.2,},{'B':0},{'x':'X','A':'dd'})   # fit data
    data.showlastErrPlot()                                                           # show fit
    
  • Fit sine to simulated dataList using an attribute in data with different name and fixed B from data. first one common parameter then as parameter list in [].

    x=np.r_[0:10:0.1]
    data=js.dL()
    ef=0.1  # increase this to increase error bars of final result
    for ff in [0.001,0.4,0.8,1.2,1.6]:                                                      # create data
        data.append( js.dA(np.c_[x,(1.234+ff)*np.sin(x+ff)+ef*ff*np.random.randn(len(x)),x*0+ef*ff].T) )
        data[-1].B=0.2*ff/2                                                                 # add attributes
    #
    # fit with a single parameter for all data, obviously wrong result
    data.fit(lambda x,A,a,B,p:A*np.sin(a*x+p)+B,{'a':1.2,'p':0,'A':1.2},{},{'x':'X'})
    data.showlastErrPlot()                                                                 # show fit
    #
    # now allowing multiple p,A,B as indicated by the list starting value
    data.fit(lambda x,A,a,B,p:A*np.sin(a*x+p)+B,{'a':1.2,'p':[0],'B':[0,0.1],'A':[1]},{},{'x':'X'})
    # plot p against A , just as demonstration
    p=js.grace()
    p.plot(data.A,data.p,data.p_err)
    
  • 2D/3D fit for scalar Y

    For 2D fit we calc Y values from X,Z coordinates, for 3D fits we use X,Z,W coordinates. For 2D plotting of the result we need data in X,Z,Y column format.

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    #
    # create 2D data with X,Z axes and Y values as Y=f(X,Z)
    x,z=np.mgrid[-5:5:0.25,-5:5:0.25]
    xyz=js.dA(np.c_[x.flatten(),
                    z.flatten(),
                    0.3*np.sin(x*z/np.pi).flatten()+0.01*np.random.randn(len(x.flatten())),
                    0.01*np.ones_like(x).flatten() ].T)
    # set columns where to find X,Y,Z )
    xyz.setColumnIndex(ix=0,iz=1,iy=2,iey=3)
    #
    ff=lambda x,z,a,b:a*np.sin(b*x*z)
    xyz.fit(ff,{'a':1,'b':1/3.},{},{'x':'X','z':'Z'})
    #
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    # matplotlib wants values as last
    ax.scatter(xyz.X,xyz.Z,xyz.Y)
    ax.tricontour(xyz.lastfit.X,xyz.lastfit.Z,xyz.lastfit.Y, cmap=cm.coolwarm, antialiased=False)
    plt.show(block=False)
    
  • Comparison of fit methods

    import numpy as np
    import jscatter as js
    diffusion=lambda A,D,t,elastic,wavevector=0:A*np.exp(-wavevector**2*D*t)+elastic
    
    i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')
    i5.makeErrPlot(title='diffusion model residual plot')
    i5.fit(model=diffusion,freepar={'D':[0.2],'A':1}, fixpar={'elastic':0.0},
           mapNames= {'t':'X','wavevector':'q'},  condition=lambda a:a.X>0.01  )
    # 22 evaluations; error YES -> 'leastsq'
    #with D=[0.2]  130 evaluations and chi2 = 0.992
    
    i5.fit(model=diffusion,freepar={'D':0.2,'A':1}, fixpar={'elastic':0.0},
           mapNames= {'t':'X','wavevector':'q'},
           condition=lambda a:a.X>0.01 ,method='BFGS' )
    # 52 evaluations, error YES
    
    i5.fit(model=diffusion,freepar={'D':0.2,'A':1}, fixpar={'elastic':0.0},
           mapNames= {'t':'X','wavevector':'q'},
           condition=lambda a:a.X>0.01 ,method='differential_evolution' )
    # 477 evaluations, error YES ; needs >20000 evaluations using D=[0.2]
    # use only with low number of parameters
    
    i5.fit(model=diffusion,freepar={'D':0.2,'A':1}, fixpar={'elastic':0.0},
           mapNames= {'t':'X','wavevector':'q'},
           condition=lambda a:a.X>0.01 ,method='Powell' )
    # 121 evaluations; error NO
    
    i5.fit(model=diffusion,freepar={'D':0.2,'A':1}, fixpar={'elastic':0.0},
           mapNames= {'t':'X','wavevector':'q'},
           condition=lambda a:a.X>0.01 ,method='SLSQP' )
    # 37 evaluations, error NO
    
    i5.fit(model=diffusion,freepar={'D':0.2,'A':1}, fixpar={'elastic':0.0},
           mapNames= {'t':'X','wavevector':'q'},
           condition=lambda a:a.X>0.01 ,method='COBYLA' )
    # 269 evaluations, error NO
    
getfromcomment(attrname)

Extract a non number parameter from comment with attrname in front

If multiple names start with parname first one is used. Used comment line is deleted from comments

Parameters:
attrname : string

Name of the parameter in first place.

hasConstrain

Return list with defined constrained source code.

hasLimit

Return existing limits

without limits returns None

has_limit

Return existing limits

without limits returns None

index(value, start=0, stop=-1)

original doc from list L.index(value, [start, [stop]]) -> integer – return first index of value. Raises ValueError if the value is not present.

insert(i, objekt=None, index=0, usecols=None, skiplines=None, replace=None, ignore='#', XYeYeX=None, delimiter=None, takeline=None, lines2parameter=None, encoding=None)

Reads/creates new dataArrays and inserts in dataList.

If objekt is dataArray or dataList all options except XYeYeX,index are ignored.

Parameters:
i : int, default 0

Position where to insert.

objekt,index,usecols,skiplines,replace,ignore,delimiter,takeline,lines2parameter : options

See dataArray or dataList

original doc from list
L.insert(index, object) – insert object before index
interpolate(func=None, invfunc=None, deg=1, **kwargs)

2D interpolation at new attribute and .X values.

Used e.g. to extrapolate a concentration series to zero. Uses twice a linear interpolation (first along .X then along attribute).

Parameters:
**kwargs :

Keyword arguments as float or array-like the first keyword argument found as attribute is used for interpolation. E.g. conc=0.12 defines the attribute ‘conc’ to be interpolated to 0.12 Special kwargs see below.

X : array

List of new X values were to evaluate (linear interpolation for X). If X < or > self.X the corresponding min/max border is used. If X not given the .X of first dataList element are used as default.

func : function or lambda

Function to be used on Y values before interpolation. See dataArray.polyfit.

invfunc : function or lambda

To invert func after extrapolation again.

deg : integer, default =1

Polynom degree for interpolation along attribute. Outliers result in Nan.

Returns:
dataArray

Notes

  • This interpolation results in a good approximation if the data are narrow. Around peaks values are underestimated if the data are not dense enough. See Examples.
  • To estimate new errors for the spline data use .setColumnIndex(iy=ii,iey=None) with ii as index of errors. Then spline the errors and add these as new column.
  • Interpolation can not be as good as fitting with a prior known model and use this for extrapolating.

Examples

import jscatter as js
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax1 = fig.add_subplot(211, projection='3d')
ax2 = fig.add_subplot(212, projection='3d')
# try different kinds of polynominal degree
deg=2
# generate some data (gaussian with mean)
i5=js.dL([js.formel.gauss(np.r_[-50:50:5],mean,10) for mean in np.r_[-15:15.1:3]])
# interpolate for several new mean values and new X values
i5b=i5.interpolate(mean=np.r_[-15:15:1],X=np.r_[-25:25:1],deg=deg)
#
fig.suptitle('Interpolation comparison with different spacing of data')
ax1.set_title("Narrow spacing result in good interpolation")
ax1.scatter3D(i5.X.flatten, np.repeat(i5.mean,[x.shape[0] for x in i5.X]), i5.Y.flatten,s=20,c='red')
ax1.scatter3D(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
ax1.tricontour(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
#
i5=js.dL([js.formel.gauss(np.r_[-50:50:5],mean,10) for mean in np.r_[-15:15.1:15]])
i5b=i5.interpolate(mean=np.r_[-15:15:1],X=np.r_[-25:25:1],deg=deg)
#
ax2.set_title("Wide spacing result in artifacts between peaks")
ax2.scatter3D(i5.X.flatten, np.repeat(i5.mean,[x.shape[0] for x in i5.X]), i5.Y.flatten,s=20,c='red')
ax2.scatter3D(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
ax2.tricontour(i5b.X.flatten,np.repeat(i5b.mean,[x.shape[0] for x in i5b.X]), i5b.Y.flatten,s=2)
plt.show(block=False)
killErrPlot(filename=None)[source]

Kills ErrPlot

If filename given the plot is saved.

makeErrPlot(title=None, showfixpar=True, **kwargs)[source]

Creates a GracePlot for intermediate output from fit with residuals.

ErrPlot is updated only if consecutive steps need more than 2 seconds. The plot can be accessed later as .errplot .

Parameters:
title : string

Title of plot.

residuals : string
Plot type of residuals.
  • ‘absolut’ or ‘a’ absolute residuals
  • ‘relative’ or ‘r’ relative =res/y
showfixpar : boolean (None,False,0 or True,Yes,1)

Show the fixed parameters in errplot.

yscale,xscale : ‘n’,’l’ for ‘normal’, ‘logarithmic’

Y scale, log or normal (linear)

fitlinecolor : int, [int,int,int]

Color for fit lines (or line style as in plot). If not given same color as data.

makeNewErrPlot(**kwargs)[source]

Creates a NEW ErrPlot without destroying the last. See makeErrPlot for details.

Parameters:
**kwargs

Keyword arguments passed to makeErrPlot.

merge(indices, isort=None)

Merges elements of dataList.

The merged dataArray is stored in the lowest indices. Others are removed.

Parameters:
indices : integer,’all’

List of indices to merge ‘all’ merges all elements into one.

isort : integer, default None

Sort after merge along specified column eg isort=’X’, ‘Y’, or 0,1,2

Notes

Attributes are copied as lists in the merged dataArray.

mergeAttribut(parName, limit=None, isort=None, func=<function mean>)

Merges elements of dataList if attribute values are closer than limit (in place).

If attribute is list the average is taken for comparison. For special needs create new parameter and merge along this.

Parameters:
parName : string

name of a parameter

limit : float

The relative limit value. If limit is None limit is determined as standard deviation of sorted differences as limit=np.std(np.array(data.q[:-1])-np.array(data.q[1:]))/np.mean(np.array(self.q)

isort : ‘X’, ‘Y’ or 0,1,2…, default None

Column for sort.

func : function or lambda, default np.mean

A function to create a new value for parameter. see extractAttribut stored as .parName+str(func.func_name)

Examples

i5=js.dL('exampleData/iqt_1hho.dat')
i5.mergeAttribut('q',0.1)
# use qmean instead of q or calc the new value
print(i5.qmean)
modelValues(**kwargs)

Calculates modelValues of model after a fit.

Model parameters are used from dataArray attributes or last fit parameters. Given arguments overwrite parameters and attributes to simulate modelValues e.g. to extend X range.

Parameters:
**kwargs : parname=value

Overwrite parname with value in the dataList attributes or fit results e.g. to extend the parameter range or simulate changed parameters.

debug : internal usage documented for completes

dictionary passed to model to allow calling model as model(**kwargs) for debugging

Returns:
dataList of modelValues with parameters as attributes.

Notes

Example: extend time range

data=js.dL('iqt_1hho.dat')
diffusion=lambda A,D,t,wavevector: A*np.exp(-wavevector**2*D*t)
data.fit(diffusion,{'D':[2],'amplitude':[1]},{},{'t':'X'})    # do fit
# overwrite t to extend range
newmodelvalues=data.modelValues(t=numpy.r_[0:100])   #with more t

Example: 1-sigma interval for D

data=js.dL('exampleData/iqt_1hho.dat')
diffusion=lambda A,D,t,q: A*np.exp(-q**2*D*t)
data.fit(diffusion,{'D':[0.1],'A':[1]},{},{'t':'X'})    # do fit
# add errors of D for confidence limits
upper=data.modelValues(D=data.D+data.D_err)
lower=data.modelValues(D=data.D-data.D_err)
data.showlastErrPlot()
data.errPlot(upper,sy=0,li=[2,1,1])
data.errPlot(lower,sy=0,li=[2,1,1])
nakedcopy()

Returns copy without attributes, thus only the data.

names

List of element names.

polyfit(func=None, invfunc=None, xfunc=None, invxfunc=None, exfunc=None, **kwargs)

Inter/Extrapolated values along attribut for all given X values using a polyfit.

To extrapolate along an attribute using twice a polyfit (first along X then along attribute). E.g. from a concentration series to extrapolate to concentration zero.

Parameters:
**kwargs :

Keyword arguments The first keyword argument found as attribute is used for extrapolation e.g. q=0.01 attribute with values where to extrapolate to Special kwargs see below.

X : arraylike

list of X values were to evaluate

func : function or lambda

Function to be used in Y values before extrapolating. See Notes.

invfunc : function or lambda

To invert function after extrapolation again.

xfunc : function or lambda

Function to be used for X values before interpolating along X.

invxfunc : function or lambda

To invert xfunction again.

exfunc : function or lambda

Weight for extrapolating along X

degx,degy : integer default degx=0, degy=1

polynom degree for extrapolation in x,y If degx=0 (default) no extrapolation for X is done and values are linear interpolated.

Returns:
dataArray

Notes

funct/invfunc is used to transfer the data to a simpler smoother or polynominal form.

  • Think about data describing diffusion like I=exp(-q^2Dt) and we want to interpolate along attribute q. If funct is np.log we interpolate on a simpler parabolic q**2 and linear in t.
  • Same can be done with X axis e.g for subdiffusion I=exp(-q^2Dt^a) \ with \ a < 1.

Examples

# Task: Extrapolate to zero q for 3 X values for an exp decaying function.
# Here first log(Y) is used (problem linearized), then linear extrapolate and exp function used for the result.
# This is like lin extrapolation of the exponent.
#
i5.polyfit(q=0,X=[0,1,11],func=lambda y:np.log(y),invfunc=lambda y:np.exp(y),deg=1)
#
# Concentration data with conc and extrapolate to conc=0.
data.polyfit(conc=0,X=data[0].X,deg=1)
#
# Interpolate for specified X and a list of attributes. ::
i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')
i5.polyfit(X=np.r_[1:5.1],q=i5.q)
pop(i=-1)

original doc from list L.pop([index]) -> item – remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.

prune(*args, **kwargs)

Reduce number of values between upper and lower limits.

Prune reduces a dataset to reduced number of data points in an interval between lower and upper by selection or by averaging including errors.

Parameters:
*args,**kwargs :

arguments and keyword arguments see below

lower : float

Lower bound

upper : float

Upper bound

number : int

Number of points in [lower,upper] resulting in number intervals.

kind : {‘log’,’lin’}, default ‘lin’
Kind of the new point distribution.
  • ‘log’ closest values in log distribution with number points in [lower,upper]
  • ‘lin’ closest values in lin distribution with number points in [lower,upper]
  • If number is None all points are used.
type : {None,’mean’,’error’,’mean+error’} default ‘mean’
How to determine the value for a point.
  • None next original value closest to column col value.
  • ‘mean’ mean values in interval between 2 points;
  • ‘mean+std’ calcs mean and adds error columns as standard deviation in intervals (no weight). Can be used if no errors are present to generate errors as std in intervals. For single values the error is interpolated from neighbouring values. ! For less pruned data error may be bad defined if only a few points are averaged.
col : ‘X’,’Y’….., or int, default ‘X’

Column to prune along X,Y,Z or index of column.

weight : None, protectedNames as ‘eY’ or int
Column for weight as 1/err**2 in ‘mean’ calculation, weight column gets new error sqrt(1/sum_i(1/err_i**2))
  • None is equal weight
  • If weight not existing or contains zeros equal weights are used.
keep : list of int

List of indices to keep in any case e.g. keep=np.r_[0:10,90:101]

Returns:
dataArray with values pruned to number of values

Notes

Attention !!!!
dependent on the distribution of original data a lower number of points can be the result
eg think of noisy data between 4 and 5 and a lin distribution from 1 to 10 of 9 points
as there are no data between 5 and 10 these will all result in 5 and be set to 5 to be unique

Examples

i5.prune(number=13,col='X',type='mean',weight='eY')
i5.prune(number=13)
remove()

L.remove(value) – remove first occurrence of value. Raises ValueError if the value is not present.

reverse()

Reverse dataList -> INPLACE!!! original doc from list L.reverse() – reverse IN PLACE

sCI(*arg, **kwargs)

Set the columnIndex where to find X,Y,Z,W eY, eX, eZ…..

Default is ix=0,iy=1,iey=2,iz=None,iex=None,iez=None,iw=None,iew=None as it is the most used. There is no limitation and each dataArray can have different ones.

Parameters:
ix,iy,iey,iex,iz,iez,iw,iew : integer, None, default= [0,1,2,None,None,None,None,None]
Set column index, where to find X, Y, eY.
  • Default from initialisation is ix,iy,iey,iex,iz,iez,iw,iew=0,1,2,None,None,None,None,None. (Usability wins iey=2!!)
  • If first ix is dataArray the ColumnIndex is copied, others are ignored.
  • If first ix is list [0,1,3] these are used as [ix,iy,iey,iex,iz,iez,iw,iew].

Remember that negative indices always are counted from back, which changes the column when adding a new column.

Notes

  • integer column index as 0,1,2,-1 , should be in range
  • None as not used eg iex=None -> no errors for x
  • anything else does not change

Shortcut sCI

Examples

data.setColumnIndex(ix=2,iy=3,iey=0,iex=None)
# remove y error in (only needed if 3rd column present)
data.setColumnIndex(iey=None)
# add Z, W column  for 3D data
data.setColumnIndex(ix=0, iz=1, iw=2, iy=3)
save(name=None, exclude=['comment', 'lastfit'], fmt='%.5e')

Saves dataList as ASCII text file, optional compressed (gzip).

Saves dataList with attributes to one file that can be reread. Dynamic created attributes as e.g. X, Y, eY, are not saved. If name extension is ‘.gz’ the file is compressed (gzip).

Parameters:
name : string

Filename

exclude : list of str, default [‘comment’,’lastfit’]

List of dataList attribut names to exclude from being saved.

fmt : string, default ‘%.5e’

Format specifier for writing float as e.g. ‘%.5e’ is exponential with 5 digits precision.

Notes

Saves a sequence of the dataArray elements.

Format rules:

Dataset consists of tabulated data with optional attributes and comments. Datasets are separated by empty lines, attributes and comments come before data.

First two strings decide for a line:
  • string + value -> attribute as attribute name + list of values
  • string + string -> comment line
  • value + value -> data (line of an array; in sequence without break)
  • single words -> are appended to comments
optional:
  • string + @name -> as attribute but links to other dataArray with .name=”name”
    stored in the same file after this dataset.
  • internal parameters starting with underscore (‘_’) are ignored for writing, also X,Y,Z,eX,eY,eZ,
  • only ndarray content is stored; no dictionaries in parameters.
  • @name is used as identifier or filename can be accessed as name.
  • attributes of dataList are saved as common attributes marked with a line “@name header_of_common_parameters”
savelastErrPlot(filename, format='agr', size=(1012, 760), dpi=300, **kwargs)[source]

Saves errplot to file with filename.

savetext(name=None, exclude=['comment', 'lastfit'], fmt='%.5e')

Saves dataList as ASCII text file, optional compressed (gzip).

Saves dataList with attributes to one file that can be reread. Dynamic created attributes as e.g. X, Y, eY, are not saved. If name extension is ‘.gz’ the file is compressed (gzip).

Parameters:
name : string

Filename

exclude : list of str, default [‘comment’,’lastfit’]

List of dataList attribut names to exclude from being saved.

fmt : string, default ‘%.5e’

Format specifier for writing float as e.g. ‘%.5e’ is exponential with 5 digits precision.

Notes

Saves a sequence of the dataArray elements.

Format rules:

Dataset consists of tabulated data with optional attributes and comments. Datasets are separated by empty lines, attributes and comments come before data.

First two strings decide for a line:
  • string + value -> attribute as attribute name + list of values
  • string + string -> comment line
  • value + value -> data (line of an array; in sequence without break)
  • single words -> are appended to comments
optional:
  • string + @name -> as attribute but links to other dataArray with .name=”name”
    stored in the same file after this dataset.
  • internal parameters starting with underscore (‘_’) are ignored for writing, also X,Y,Z,eX,eY,eZ,
  • only ndarray content is stored; no dictionaries in parameters.
  • @name is used as identifier or filename can be accessed as name.
  • attributes of dataList are saved as common attributes marked with a line “@name header_of_common_parameters”
savetxt(name=None, exclude=['comment', 'lastfit'], fmt='%.5e')

Saves dataList as ASCII text file, optional compressed (gzip).

Saves dataList with attributes to one file that can be reread. Dynamic created attributes as e.g. X, Y, eY, are not saved. If name extension is ‘.gz’ the file is compressed (gzip).

Parameters:
name : string

Filename

exclude : list of str, default [‘comment’,’lastfit’]

List of dataList attribut names to exclude from being saved.

fmt : string, default ‘%.5e’

Format specifier for writing float as e.g. ‘%.5e’ is exponential with 5 digits precision.

Notes

Saves a sequence of the dataArray elements.

Format rules:

Dataset consists of tabulated data with optional attributes and comments. Datasets are separated by empty lines, attributes and comments come before data.

First two strings decide for a line:
  • string + value -> attribute as attribute name + list of values
  • string + string -> comment line
  • value + value -> data (line of an array; in sequence without break)
  • single words -> are appended to comments
optional:
  • string + @name -> as attribute but links to other dataArray with .name=”name”
    stored in the same file after this dataset.
  • internal parameters starting with underscore (‘_’) are ignored for writing, also X,Y,Z,eX,eY,eZ,
  • only ndarray content is stored; no dictionaries in parameters.
  • @name is used as identifier or filename can be accessed as name.
  • attributes of dataList are saved as common attributes marked with a line “@name header_of_common_parameters”
setColumnIndex(*arg, **kwargs)

Set the columnIndex where to find X,Y,Z,W eY, eX, eZ…..

Default is ix=0,iy=1,iey=2,iz=None,iex=None,iez=None,iw=None,iew=None as it is the most used. There is no limitation and each dataArray can have different ones.

Parameters:
ix,iy,iey,iex,iz,iez,iw,iew : integer, None, default= [0,1,2,None,None,None,None,None]
Set column index, where to find X, Y, eY.
  • Default from initialisation is ix,iy,iey,iex,iz,iez,iw,iew=0,1,2,None,None,None,None,None. (Usability wins iey=2!!)
  • If first ix is dataArray the ColumnIndex is copied, others are ignored.
  • If first ix is list [0,1,3] these are used as [ix,iy,iey,iex,iz,iez,iw,iew].

Remember that negative indices always are counted from back, which changes the column when adding a new column.

Notes

  • integer column index as 0,1,2,-1 , should be in range
  • None as not used eg iex=None -> no errors for x
  • anything else does not change

Shortcut sCI

Examples

data.setColumnIndex(ix=2,iy=3,iey=0,iex=None)
# remove y error in (only needed if 3rd column present)
data.setColumnIndex(iey=None)
# add Z, W column  for 3D data
data.setColumnIndex(ix=0, iz=1, iw=2, iy=3)
setConstrain(*args)

Set inequality constrains for constrained minimization in fit.

Inequality constrains are accounted by an exterior penalty function increasing chi2. Equality constrains should be incorporated in the model function to reduce the number of parameters.

Parameters:
args : function or lambda function

Function that defines constrains by returning boolean with free and fixed parameters as input. The constrain function should return True in the accepted region and return False otherwise. Without function all constrains are removed.

Notes

Warning:
The fit will find a best solution with violated constrains if the constrains forbid to find a good solution.

A 3 component model with fractional contributions n1,n2,n3 Constrains are:

  • n1+n2+n3=1
  • 0=<ni<=1 for i=1, 2, 3

Use n3=1-n1-n2 to reduce number of parameters in model function.

Set constrain:

data.setconstrain(lambda n1,n2:(0<=n1<=1) & (0<=n2<=1) & (0<=1-n1-n2<=1))
setLimit(**kwargs)

Set upper and lower limits for parameters in least square fit.

Use as .setlimit(parname=(lowlimit, uplimit,lowhardlimit, uphardlimit))

Parameters:
parname : [value x 4] , list of 4 x (float/None), default None
  • lowlimit, uplimit : float, default None soft limits: chi2 increased with distance from limit, non-float resets limit
  • lowhardlimit, uphardlimit: hardlimit float, None values are set to border , chi2 is increased strongly

Notes

Penalty methods are a certain class of algorithms for solving constrained optimization problems. Here the penalty function increases chi2 by a factor chi*f_constrain.

  • no limit overrun : 1
  • softlimits : + 1+abs(val-limit)*10 per limit
  • hardlimits : +10+abs(val-limit)*10 per limit

Examples

setlimit(D=(1,100),A=(0.2,0.8,0.0001))  # to set low=1 and up=100
                                        # A with a hard limit to avoid zero
setlimit(D=(None,100))                  # to reset lower and set upper=100
setlimit(D=(1,'thisisnotfloat','',))    # to set low=1 and reset up
setlimit(**kwargs)

Set upper and lower limits for parameters in least square fit.

Use as .setlimit(parname=(lowlimit, uplimit,lowhardlimit, uphardlimit))

Parameters:
parname : [value x 4] , list of 4 x (float/None), default None
  • lowlimit, uplimit : float, default None soft limits: chi2 increased with distance from limit, non-float resets limit
  • lowhardlimit, uphardlimit: hardlimit float, None values are set to border , chi2 is increased strongly

Notes

Penalty methods are a certain class of algorithms for solving constrained optimization problems. Here the penalty function increases chi2 by a factor chi*f_constrain.

  • no limit overrun : 1
  • softlimits : + 1+abs(val-limit)*10 per limit
  • hardlimits : +10+abs(val-limit)*10 per limit

Examples

setlimit(D=(1,100),A=(0.2,0.8,0.0001))  # to set low=1 and up=100
                                        # A with a hard limit to avoid zero
setlimit(D=(None,100))                  # to reset lower and set upper=100
setlimit(D=(1,'thisisnotfloat','',))    # to set low=1 and reset up
shape

Tuple with shapes of dataList elements.

showattr(maxlength=75, exclude=None)

Show data specific attributes for all elements.

Parameters:
maxlength : integer

Truncate string representation

exclude : list of str

List of attribute names to exclude from show

showlastErrPlot(title=None, modelValues=None, **kwargs)[source]

Shows last ErrPlot as created by makeErrPlot with last fit result.

Same arguments as in makeErrPlot.

Additional keyword arguments are passed as in modelValues and simulate changes in the parameters. Without parameters the last fit is retrieved.

sort(key=None, reverse=False)

Sort dataList -> INPLACE!!!

Parameters:
key : function

A function that is applied to all elements and the output is used for sorting. e.g. ‘Temp’ or lambda a:a.Temp convenience: If key is attribut name this attribute is used

reverse : True, False

Normal or reverse order.

Examples

dlist.sort('q',True)
dlist.sort(key=lambda ee:ee.X.mean() )
dlist.sort(key=lambda ee:ee.temperatur )
dlist.sort(key=lambda ee:ee.Y.mean() )
dlist.sort(key=lambda ee:ee[:,0].sum() )
dlist.sort(key=lambda ee:getattr(ee,parname))
dlist.sort(key='parname')
transposeAttribute(attr)

Use attribute as new X axis (like transpose .X and attribute).

It is necessary that all X have same values and length. This can be achieved by polyfit, interpolate or prune to shape the dataList.

Parameters:
attr : str

Attribute to use

Returns:
dataList with attribute x as old X values

Examples

i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')
# polyfit and interpolate produce the same .X with control over used values
i6=i5.polyfit(X=np.r_[1:5.1],q=i5.q).transposeAttribute('q')
i7=i5.interpolate(X=i5[-1].X,q=i5.q).transposeAttribute('q')
# .prune allows to use X borders to cut X range
i5.prune(lower=1,upper=5).transposeAttribute('q')
whoHasAttributes

Lists which attribute is found in which element.

Returns:
dictionary of attributes names: list of indices

keys are the attribute names values are indices of dataList where attr is existent