def getValues(self, field): try: i=self._fields.index(field) except: vlog.error('Field {} not found in list of fields'.format(field)) raise return self._contents[i]
def __call__(self, fvalue): ''' Evalue the interpolators for the particular value of the external parameter ''' if self.range[0] <= fvalue <= self.range[1]: return self.y(fvalue), float(self.e(fvalue)) else: vlog.error( 'Value outside of interpolating bounds' ) return ( float( 'inf' ), float( 'inf' ) )
def detectVersion(self, mfile): '''Find parser with latest version that will read the file ''' try: pf=open(mfile) except IOError, e: vlog.error(e) raise
def SetErrors(self,errors): if self.intensities is None: vlog.error('Error: Set intensities before setting errors') return if numpy.array(errors).shape != self.intensities.shape: vlog.error('Error: shape of errors different than shape of intensities') return self.errors = numpy.array(errors)
def __init__(self, angletype): ''' Attributys: type domainlength ''' if angletype in Unit.domainlenghts.keys(): self.type = angletype else: vlog.error( 'Angle unit not recognized. Implemented units are : {0}'.format( ', '.join(Unit.domainlenghts.keys()) ) ) self.domainlenght = Unit.domainlenghts[ angletype ]
def test_LoadMantidWorkspace2D(self): ''' Load intensities and errors from a Mantid Workspace2D ''' try: from mantid.simpleapi import LoadNexus except ImportError: vlog.error('mantid library not found!') raise ImportError workspace = LoadNexus('./data/exp100K.nxs') dsf = Dsf() dsf.Load(workspace) self.assertEqual(dsf.shape, (9,700)) self.assertEqual(dsf.errors.shape, (9,700))
def Save(self, dsf, ws): ''' Save the dynamics structure factor into the workspace dsf: the dynamics structure factor to save ws: workspace where Y and E will be overwritten with dsf contents ''' dimension = len(dsf.shape) if dimension != 2: vlog.error('Dimension of the dynamics structure factor is not 2') return nhist = dsf.shape[0] size = dsf.shape[1] try: mhist = ws.getNumberHistograms() if nhist != mhist: vlog.error('Number of histograms in the worskpace does not match the dynamics structure factor first dimension') return for ihist in range(nhist): if ws.dataY(ihist).size != size: vlog.error('second dimension of the dynamics structure factor has different size than that of histogram with index '+str(ihist)) return ws.dataY(ihist)[:] = dsf.intensities[ihist] ws.dataE(ihist)[:] = dsf.errors[ihist] except TypeError: vlog.error('the workspace is not of type '+self.datatype) return
def Load(self): ''' Load a trajectory of angles from a file. Will also try to guess the units (radian, degree,...) as well as range ([0,360], [-pi,pi], ..) ''' if type == 'ASCII text': options = {'comments':'#', 'delimiter':None, 'converters':None, 'skiprows':0, 'usecols':None, 'unpack':False, 'ndmin':0} options.update( {key:value for key, value in self.__dict__.items() if key in self.__dict__} ) #update with passed arguments self.series = numpy.loadtxt(self.filenamed, type='float', **options) if len(self.series) > 1: vlog.error('Error in trajectoryLoader.Load: Multi-column file. Please specify "usecols" argument. Example: usecols = 0 will read the first column. See numpy.loadtxt for more information on allowed arguments') self.series=None else: vlog.error( 'Loading trajectory of type {0} not implemented yet'.format(self.type)) self.GuessUnit()
def Load(self, filename): try: from mantid.simpleapi import LoadNexus except ImportError: vlog.error('mantid library not found!') raise ImportError try: workspace = LoadNexus(filename) except: raise TypeError loader = DsfLoaderMantidWorkspace2D() return loader.Load(workspace)
def Instantiate(self, datatype): ''' Instantiate a dynamic structure factor saver of appropriate type ''' if datatype not in self.datatypes: vlog.error('No dynamic structure factor loader for type {0}'.format(datatype)) return DsfSaveFactory.savers[datatype]()
def __init__(self, fseries, signalseries, errorseries=None, running_regr_type = 'linear', windowlength=3): ''' Arguments: [running_regr_type]: method for the local, runnig regression Attributes: range: minimum and maximum of the fseries fitted: values of the structure factor at the external parameter values after the running regression errors: errorseries or estimated errors at the external parameter values from the running regression y: interpolator object for the struc ture factor (cubic spline) e: interpolator object for the error (linear) running_regr_type: type of running regression windowlength: length of window where local regression is done. Select zero for no regression ''' # Deal with possible errors if len( fseries ) != len( signalseries ): vlog.error( 'signal and external parameter series have different lenght!' ) self.running_regr_type = running_regr_type self.windowlength = windowlength self.range = ( fseries[ 0 ], fseries[ -1 ] ) # Do running regression, and if necessary estimate errors if self.windowlength and running_regr_type == 'linear': if self.windowlength < 3: message = 'Linear regression requires a window length bigger than 2' vlog.error(message) raise ValueError(message) from scipy.stats import linregress if len( fseries ) < self.windowlength: vlog.error( 'series has to contain at least {0} members'.format( windowlength ) ) else: # Lower boundary, the first self.windowlength/2 values x = fseries[ : self.windowlength ] y = signalseries[ : self.windowlength ] slope, intercept, r_value, p_value, std_err = linregress( x, y ) linF = lambda xx: intercept + slope * xx self.fitted = [] for i in range(0, 1+self.windowlength/2): self.fitted.append(linF(x[i])) residuals = numpy.square(numpy.vectorize(linF)(x) - y) residual = numpy.sqrt( numpy.mean(residuals)) #average residual self.errors = [residual,] * (1+self.windowlength/2) # Continue until hitting the upper boundary index = 1 # lower bound of the regression window while ( index + self.windowlength <= len( fseries ) ): x = fseries[ index : index + self.windowlength ] y = signalseries[ index : index + self.windowlength ] slope, intercept, r_value, p_value, std_err = linregress( x, y ) linF = lambda xx: intercept + slope * xx self.fitted.append(linF(x[self.windowlength/2])) residuals = numpy.square(numpy.vectorize(linF)(x) - y) residual = numpy.sqrt( numpy.mean(residuals)) #average residual self.errors.append(residual) # Resolve the upper boundary if index + self.windowlength == len( fseries ): for i in range(1+self.windowlength/2, self.windowlength): self.fitted.append(linF(x[i])) self.errors.append(residual) index += 1 elif self.windowlength and running_regr_type == 'quadratic': if self.windowlength < 4: message = 'Quadratic regression requires a window length bigger than 3' vlog.error(message) raise ValueError(message) from numpy import polyfit if len( fseries ) < self.windowlength: vlog.error( 'series has to contain at least {0} members'.format( self.windowlength ) ) else: # Lower boundary, the first three values x = fseries[ : self.windowlength ] y = signalseries[ : self.windowlength ] coeffs, residuals, rank, singular_values, rcond= polyfit(x,y,2, full=True) #second order polynomial quadF = lambda xx: coeffs[0]*xx*xx + coeffs[1]*xx + coeffs[2] self.fitted = [] for i in range(0, 1+self.windowlength/2): self.fitted.append(quadF(x[i])) residual = numpy.sqrt(numpy.mean( residuals )) #average residual self.errors = [residual,] * (1+self.windowlength/2) # Continue until hitting the upper boundary index = 1 # lower bound of the regression window while ( index + self.windowlength <= len( fseries ) ): x = fseries[ index : index + self.windowlength ] y = signalseries[ index : index + self.windowlength ] coeffs, residuals, rank, singular_values, rcond = polyfit(x,y,2, full=True) #second order polynomial quadF = lambda xx: coeffs[0]*xx*xx + coeffs[1]*xx + coeffs[2] self.fitted.append(quadF(x[self.windowlength/2])) residuals = numpy.square(numpy.vectorize(quadF)(x) - y) residual = numpy.sqrt( numpy.mean(residuals)) #average residual self.errors.append(residual) # Resolve the upper boundary if index + self.windowlength == len( fseries ): for i in range(1+self.windowlength/2, self.windowlength): self.fitted.append(quadF(x[i])) self.errors.append(residual) index += 1 else: if self.windowlength == 0: self.fitted = copy.copy(signalseries) self.errors = [0.,] * len( fseries ) else: vlog.warning( 'Requested regression type not recogized' ) # Passed errors take precedence over calculated errors if errorseries is not None: self.errors = errorseries # Interpolators for fitted and errors from scipy.interpolate import interp1d, UnivariateSpline x = numpy.array( fseries ) y = numpy.array( self.fitted ) e = numpy.array( self.errors ) if e.any(): min_nonzero_error = numpy.min(e[numpy.nonzero(e)]) # smallest non-zero error e = numpy.where(e >=min_nonzero_error, e, min_nonzero_error) # substitute zero errors with the smallest non-zero error w = 1.0 / e s = len( fseries ) else: # in the case of no errors, force the spline to pass through all points w = numpy.ones(len(fseries)) s = 0 self.y = UnivariateSpline( x, y, w=w, s=s ) self.e = interp1d(x, e, kind='linear')
except Exception, e: pass vlog.error('Appropriate loader not found for supplied data') raise TypeError def Save(self, container, datatype=None): ''' Save intensities and errors to a container Iterates over all data savers until it finds appropiate saver Arguments: [datatype]: one of the DsfSave types registered in DsfSaverFactory Returns: first successful datatype Exceptions: TypeError if container and datatype don't agree ''' from dsfsave import DsfSaveFactory save_factory = DsfSaveFactory() datatypes = [datatype,] if datatype else save_factory.datatypes for datatype in datatypes: try: saver = save_factory.Instantiate(datatype) saver.Save(self, container) return datatype except Exception, e: pass vlog.error('Appropriate loader not found for supplied data') raise TypeError
''' Created on Jan 10, 2014 @author: jbq ''' import numpy from unit import Unit from logger import vlog try: from magic import from_file except TypeError: vlog.error('Please install the python-magic package ("sudo pip install python-magic")') raise class Trajectory(object): ''' This class implements a trajectory of dihedral angles ''' def __init__( self, ): ''' Attributes: series: the actual values, a numpy.array unit: degree, radian, grad range: [0,360], or [-pi,pi], etc ''' self.series = None self.unit = None