예제 #1
0
    def getDataSlice(self,
                     con,
                     keys,
                     params=None,
                     sortby=None,
                     name='def',
                     fnc=lambda x: True,
                     fnc_sort=lambda x, y: cmp(x, y),
                     **kwargs):
        dH = m_dataHandler()
        dOs = []
        if params is not None:
            cons = self.lookup.getConditions(con, _asarray(params), fnc,
                                             **kwargs)
        else:
            cons = con

        for i, con in enumerate(cons):
            dat = LP_object.m_Dict()

            objs = self.getObjects(con, None, fnc, **kwargs)
            if objs is not None:
                objs = copy.copy(objs)
                if sortby is not None:
                    for key in reversed(_asarray(sortby)):
                        objs.sortObjects(key, fnc_sort, **kwargs)

                #name = ['%svs' %(d.replace('/','_')) for d in keys]
                #nadd = self.lookup.condition_to_string(con).replace('=','').replace(', ','_')
                #for key,value in kwargs.items():
                #    nadd=nadd.replace(key,value)
                objs_name = name
                if params is not None:
                    for param in _asarray(params):
                        objs_name = objs_name + '_%s%s' % (
                            param, str(objs.getSorted()[0].getHeader(param)))

                if objs.header.has_key(_asarray(keys)[0]):
                    dat = LP_object.m_Dict()
                    for d in _asarray(keys):
                        for obj in objs.getSorted():
                            if LP_object.isinstanceof(obj.header[d],
                                                      LP_object.m_Dict):
                                for k in obj.header[d].getKeys():
                                    if not dat.has_key(d + '/' + k):
                                        dat[d + '/' + k] = []
                                    dat[d + '/' + k].append(
                                        obj.header[d].get(k))
                            else:
                                if not dat.has_key(d):
                                    dat[d] = []
                                dat[d].append(obj.header.get(d))
                    dO = m_dataObject(m_Data(**dat),
                                      m_Header(**{'name': objs_name}))
                    for p in params:
                        dO.setHeader(p, objs.header[p][0])
                    dOs.append(dO)
        dH.add(dOs)
        return dH
예제 #2
0
 def __init__(self,fig, vis=False):
     import sys
     self.f=sys.stdout
     self.figure = fig
     self.data=LP_object.m_Dict()
     print(type(self.data))
     self.event=None
     self.vis = vis
     self.marker=LP_object.m_Dict()
     self.morder=[]
     self.cid1 = self.figure.canvas.mpl_connect('button_press_event',
                                               self._onclick)
     self.cid2 = self.figure.canvas.mpl_connect('pick_event',
                                               self._onpick)
예제 #3
0
    def _onpick(self,event):
        from numpy import mean
        if event.mouseevent.button == 2:
            self._release()
            return True
        self.event=event
        self.artist=event.artist
        self.mouse=event.mouseevent
        self.data.set('x',self.mouse.x)
        self.data.set('y',self.mouse.y)
        self.data.set('xcoord',self.mouse.xdata)
        self.data.set('ycoord',self.mouse.ydata)
        self.data.set('xdata',self.artist.get_xdata()[event.ind])
        self.data.set('ydata',self.artist.get_ydata()[event.ind])
        self.data.set('ind',event.ind)
        if event.mouseevent.button == 1:
            if len(self.marker)<4:
                self.figure.axes[0].hold(True)
                line=self.figure.axes[0].plot([mean(self.data.xdata)],[mean(self.data.ydata)],'r.',picker=2)[0]
                self.figure.canvas.draw()
                self.morder.append(line)
                self.marker[line]=LP_object.m_Dict(x=[mean(self.data.xdata)],y=[mean(self.data.ydata)])
            else:
                l=self.morder.pop(0)
                self.figure.axes[0].hold(True)
                line=self.figure.axes[0].plot([mean(self.data.xdata)],[mean(self.data.ydata)],'r.',picker=2)[0]
                self.figure.canvas.draw()
                self.morder.append(line)
                self.marker[line]=LP_object.m_Dict(x=[mean(self.data.xdata)],y=[mean(self.data.ydata)])
                l.remove()
                self.marker.pop(l)

        if event.mouseevent.button == 3:
            if self.marker.has_key(self.artist):
                self.morder.remove(self.artist)
                self.artist.remove()
                self.marker.pop(self.artist)
            else:
                l=self.morder.pop(-1)
                l.remove()
                self.marker.pop(l)
            self.figure.canvas.draw()
#~
        if self.vis:
            self.f.write('\nid=%d, x=%d, y=%d,\nxcoord=%.4e, ycoord=%.4e,\nxdata=%.4e, ydata=%.4e'%(
            int(mean(event.idn)), self.data.x,self.data.y, self.data.xcoord,
                self.data.ycoord, mean(self.data.xdata), mean(self.data.ydata)))
            self.f.flush()
예제 #4
0
    def _store_leastsq(self, fitres):

        result = m_Result()
        result['xin'] = self.data.x
        result['yin'] = self.data.y
        result['xinfull'] = self.data.x_org
        result['yinfull'] = self.data.y_org
        result['yout'] = array(self.model.fcn(self.model.params, self.data.x,
                              self.model.const))
        result['yfull'] = array(self.model.fcn(list(self.model.params), self.data.x_org,
                              self.model.const))
        result['succeeded'] = (True if fitres[1] < 4 else False)
        result['params'] = self.model.params
        result['paramdict'] = LP_object.m_Dict()
        [result['paramdict'].set(name.split('.')[-1],v) for name,v in zip(self.model.names,
                                                                          self.model.params)]
        result['paramnames'] = tuple(name.split('.')[-1] for name in self.model.names)
        result['fixed'] = tuple(result['paramnames'][i] for i in xrange(len(result['paramnames']))
                                if self.model.x0[i].froozen)
        result['fitres'] = fitres[1]

        ret =   str(self.model)
        ret+='\n'

        result['pprint'] = lambda : sys.stdout.write(ret); sys.stdout.flush()
        result['format'] = ret

        self.result=result
예제 #5
0
    def _store_deSolver(self, fitres):

        result = m_Result()
        result['xin'] = self.data.x
        result['yin'] = self.data.y
        result['xinfull'] = self.data.x_org
        result['yinfull'] = self.data.y_org
        result['yout'] = array(self.model.fcn(self.model.params, self.data.x,
                              self.model.const))
        result['yfull'] = array(self.model.fcn(list(self.model.params), self.data.x_org,
                              self.model.const))
        result['succeeded'] = (True if fitres.goal_error > fitres.best_error
                               else False)
        result['paramnames'] = tuple(name.split('.')[-1] for name in self.model.names)
        result['params'] = self.model.params
        result['paramdict'] = LP_object.m_Dict()
        [result['paramdict'].set(name.split('.')[-1],v) for name,v in zip(self.model.names,self.model.params)]
        result['fixed'] = tuple(result['paramnames'][i] for i in xrange(len(result['paramnames']))
                                if self.model.x0[i].froozen)
        result['chi2'] = fitres.best_error
        result['nfev'] = fitres.best_individual
        result['method'] = fitres.method
        result['fitres'] = fitres

        ret = '\nNumber of generations: %s - Best generation: %s\n' %(fitres.generation, fitres.best_generation)
        ret +=   str(self.model)
        ret+='\n'

        result['pprint'] = lambda : sys.stdout.write(ret); sys.stdout.flush()
        result['format'] = ret

        self.result=result
예제 #6
0
    def _deSolverGet(self, keys):

        for key in LP_object._asarray(keys):
            try:
                exec('return self.solver.%s'%(key))
            except:
                pass
예제 #7
0
        def _sherpaGet(self, keys):

            for key in LP_object._asarray(keys):
                try:
                    exec('return sherpa.get_%s'%(key))
                except:
                    pass
예제 #8
0
    def export(self):
        header=LP_object.m_Dict()
        data=LP_object.m_Dict()
        for key,val in self.getItems():

            if LP_object._ndim(val) == 0:
                header.set(key, val)
            else:
                if LP_object.isinstanceof('dict', val):
                    header.set(key, val)
                elif LP_object.isinstanceof('numpy.ndarray', val):
                    data.set(key,val)
        try:
            del header['format']
            del header['pprint']
            del data['modelvals']
        except:
            pass
        return header, data
예제 #9
0
 def __init__(self,
              objects=None,
              derived_class1=LP_object.m_Object,
              derived_class2=LP_saveload.m_IgorSave,
              **kwargs):
     derived_class1.__init__(self, **kwargs)
     derived_class2.__init__(self)
     self.objects = None
     self.objects = LP_object.m_Dict()
     self.header = m_Header(**kwargs)
     self.lookup = LP_lookup.m_Lookup()
     self.order = []
     if objects is not None:
         for object in objects:
             object.parent = None
         self.add(copy.deepcopy(objects))
예제 #10
0
    def add(self, dOs, **kwargs):
        from string import join
        for key, val in kwargs.items():
            self.header.set(key, val)
        if LP_object.isinstanceof(_asarray(dOs)[0], m_dataObject):
            for d in _asarray(dOs):
                counter = len(self.objects.keys())
                d.setHeader('counter', counter)
                if not d.header.has_key('name'):
                    d.setHeader('name', 'def_%d' % (counter))
                # Add an extra counter to the names to avoid
                # compatibility problems with IGOR makros

                if not self.header.has_key('name'):
                    self.setHeader('name', [])
                c = 0
                name = copy.copy(d.header['name'])
                while True:
                    if name not in self.header['name']:

                        self.header['name'].append(name)
                        break

                    name = d.header['name'] + '_' + str(c)
                    c += 1

                d.setParent(self)
                d.setHeader('name', name)
                self.objects.set(d.header['name'], d)
                self.order.append(d.header['name'])
                self.__dict__['o%d' % counter] = self.objects[d.header['name']]

            self._makeHeader(_asarray(dOs))
            self._appendToHeader(_asarray(dOs))
            self.updateHeader()
        else:
            print('%s kann nicht durch m_dataHandler verwaltet werden.')
            print(
                'Objekte muessen von der Klasse "LP_data.m_dataObject" sein.' %
                str(_asarray(dOs)[0].__class__).split("'")[1])
예제 #11
0
    def _store_odr(self, fitres):

        result = m_Result()
        result['xin'] = self.data.x
        result['yin'] = self.data.y
        result['xinfull'] = self.data.x_org
        result['yinfull'] = self.data.y_org
        result['yout'] = array(self.model.fcn(list(self.model.params), self.data.x,
                              self.model.const))
        result['yfull'] = array(self.model.fcn(list(self.model.params), self.data.x_org,
                              self.model.const))
        result['params'] = self.model.params
        result['paramdict'] = LP_object.m_Dict()
        [result['paramdict'].set(name.split('.')[-1],v) for name,v in zip(self.model.names,self.model.params)]
        result['paramnames'] = tuple(name.split('.')[-1] for name in self.model.names)
        result['parmaxes'] = tuple(cov for cov in fitres.cov_beta)
        result['parmins'] = tuple(-1*cov for cov in fitres.cov_beta)
        result['fixed'] = tuple(result['paramnames'][i] for i in xrange(len(result['paramnames']))
                                if self.model.x0[i].froozen)
        result['info'] = fitres.info
        result['stopreason'] = fitres.stopreason
        result['fitres'] = fitres

        ret =   str(self.model)
        ret+='\n'
        if hasattr(fitres, 'info'):
            ret += 'Residual Variance: %s\n' %fitres.res_var
            ret += 'Inverse Condition #: %s\n' %fitres.inv_condnum
            ret += 'Reason(s) for Halting:\n'
            for r in fitres.stopreason:
                ret += '  %s\n' % r
        ret+='\n\n'

        result['pprint'] = lambda : sys.stdout.write(ret); sys.stdout.flush()
        result['format'] = ret

        self.result=result
예제 #12
0
    def getObjects(self, con, keys=None, fnc=lambda x: True, **kwargs):
        if keys is None:
            oH = m_dataHandler()
            Os = []

            vals = self.lookup.getWhere(con, fnc, **kwargs)
            for val in vals:
                self.objects[val].parent = None
                Os.append(copy.deepcopy(self.objects[val]))
            if len(Os) > 0:
                oH.add(Os)
                return oH
        else:
            oHs = {}
            for key in reversed(_asarray(keys)):
                if LP_func.isAscii(_asarray(self.header.get(key))[0]):
                    self.sortObjects(key, LP_func.strSort_cmp, **kwargs)
                else:
                    self.sortObjects(key, cmp, **kwargs)
            cons = self.lookup.getConditions(con, keys, fnc, **kwargs)
            for i, con in enumerate(cons):
                dat = LP_object.m_Dict()
                conParts = _asarray(con.replace(' ', '').split('&'))
                for conp in conParts:
                    if conp.count('==') > 0:
                        if not conp.split('==')[0].lstrip('(') in keys:
                            con += '&(' + conp + ')'
                #~ con += '&'+con
                objs = self.getObjects(con, None, fnc, **kwargs)
                if objs is not None:
                    objs = copy.copy(objs)
                    name = (con.replace('(','').replace(')','')\
                            .replace('&','_').replace('==','').replace(' ',''))
                    oHs[name] = objs

            return oHs
예제 #13
0
        def add(self, obj):
            """Function add:
            Fügt ein Objekt oder eine Liste von Objekten der Klassen
                - m_FileHandler
                - m_dataHandler
                - m_Plot
            hinzu.

            Parameter:
            ----------
            obj:            Objekt oder eine Liste von Objekten der Klassen
                                - m_FileHandler
                                - m_dataHandler
                                - m_Plot"""

            if asarray(obj).ndim == 0:
                if LP_object.isinstanceof(obj, LP_files.m_fileHandler) or \
                   LP_object.isinstanceof(obj, LPlot.LP_files.m_fileHandler):
                    self.fileHandler = obj
                elif LP_object.isinstanceof(obj,LP_data.m_dataHandler) or \
                   LP_object.isinstanceof(obj, LPlot.LP_data.m_dataHandler):
                    self.dataHandlers.append(obj)
            else:
                if LP_object.isinstanceof(obj[0], LP_files.m_fileData) or \
                   LP_object.isinstanceof(obj, LPlot.LP_files.m_fileData):
                    self.fileHandler = LP_files.m_fileHandler(obj)
                elif LP_object.isinstanceof(obj[0],LP_data.m_Data) or \
                   LP_object.isinstanceof(obj, LPlot.LP_data.m_Data):
                    self.dataHandlers.append(LP_data.m_dataHandler(obj))
                else:
                    for o in asarray(obj):
                        if LP_object.isinstanceof(o,LP_data.m_dataHandler) or \
                           LP_object.isinstanceof(obj, LPlot.LP_data.m_dataHandler):
                            self.dataHandlers.append(o)
                        elif LP_object.isinstanceof(o,LP_plot.m_Plot) or \
                            LP_object.isinstanceof(obj, LPlot.LP_plot.m_Plot) :
                            self.plotHandlers.append(o)
                        else:
                            print '%s kann nicht im Projekt verwaltet werden.\n Objekte muessen von der Klasse "LP_files.m_fileHandler", "m_dataHandler" oder "m_Plot" sein.' % str(
                                o.__class__).split("'")[1]
예제 #14
0
        def _fit_sherpa(self):
            #
            # Fitting function
            #
            def fcn(params, x):
                return self.model.fcn(params, x, self.model.const)
            #
            # set data
            #
            self.solver=sherpa
            self.solver.set_data(self.solver.Data1D('Default', self.data.x, self.data.y))
            #
            # create model
            #
            self.solver.load_user_model(fcn, "fmdl")
            #
            # set startparameter
            #
            self.solver.add_user_pars("fmdl", self.model.names, self.model.params,
                                        self.model.mins, self.model.maxs,
                                        self.model.units, self.model.froozen)
            self.solver.set_model(fmdl)

            if len(self.kwargs) > 0:
                self.set(**self.kwargs)
            #
            # set method for fitting (levmar, moncar, simplex)
            # if simplex or moncar is used try first to get good
            # starting values with levmar
            fitres,errestres = (None,None)
            for method in LP_object._asarray(self.method):
                if method in ('levmar','simplex','moncar'):
                    self.set(set_method=method)
                    if method=='moncar':
                        self.set(set_method_opt=("maxfev",self.maxfev/100))
                    elif method == 'levmar':
                        self.set(set_method_opt=("maxfev",self.maxfev*50))
                    elif method == 'simplex':
                        self.set(set_method_opt=("maxfev",self.maxfev))
                    else: print '! Fit method unknown: %s' %method
                    fitres=self.solver.run()
                elif method in ('proj','covar','conf') and self.stat <> 'leastsq':

                    #~ try:
                        if method == 'proj':
                            self.solver.set_proj_opt ('tol',1e-1)
                            self.solver.set_proj_opt ('eps',1e-2)
                            self.solver.set_proj_opt ('sigma',3)
                            errestres = self.solver.proj()
                        elif method == 'conf':
                            self.solver.set_conf_opt ('tol',1e-1)
                            self.solver.set_conf_opt ('eps',1e-2)
                            self.solver.set_conf_opt ('sigma',3)
                            errestres = self.solver.conf()
                        elif method == 'covar':
                            self.solver.set_covar_opt ('eps',1e-2)
                            self.solver.set_covar_opt ('sigma',3)
                            errestres = self.solver.covar()
                        else: print '! Error estimation method unknown: %s' %method
                    #~ except:
                       #~ pass

            j=0
            for i in xrange(self.model.get_paramCount()):
                if not self.model.froozen[i]\
                    and self.model.params[i] <> fitres.parvals[j]:
                    self.model.params[i] = fitres.parvals[j]
                    j+=1

            self.storeResult(fitres)

            return self.result