示例#1
0
    def __init__(self,x,y,func,*args,**kwargs):
    
        '''
        Create an instance of the Profiler2D() class. Requires 2 coordinate 
        arrays and a function object for profile.
       
        The function can also be given as an interpolation object.
        
        The optional args and kwargs give the additional arguments for the 
        function, which are ignored in case func is an interpolation object.
        
        The default coordinate grids are both evaluated for the function. 
        They are saved in self.z, as an array of dimensions (x.size,y.size). 
        Alternatively, new evaluations can be attained through eval and diff.
        
        @param x: The default coordinates of the primary independent variable. 
                  Minimum three points.
        @type x: array
        @param y: The default coordinates of the secondary independent variable. 
                  Minimum three points.
        @type y: array
        @param func: The function that describes the profile with respect to x 
                     and y. Can be given as a 2D interpolation object.
        @type func: function/interpolation object
        
        @keyword args: Additional parameters passed to the functions when eval
                       or diff are called. 
                       
                       (default: [])
        @type args: tuple
        @keyword kwargs: Additional keywords passed to the functions when eval
                         or diff are called. 
                       
                         (default: {})
        @type kwargs: dict
        
        '''

        #-- If the function is given as a string, retrieve it from the local 
        #   child module, or from the Profiler module (as parent). If the latter
        #   check if the function comes from one of Profiler's attributes, such
        #   as the loaded DataIO module.
        if isinstance(func,str):
            try:
                #-- Note that self.module refers to the child, not Profiler
                func = getattr(sys.modules[self.__module__],func)
            except AttributeError:
                #-- Recursively find the function of loaded modules in Profiler
                #   or a function of the Profiler module itself if no '.' 
                func = DataIO.read(sys.modules[__name__],return_func=1)
                
        #-- set functional args, remember spline order. args/kwargs for 
        #   func are saved separately. They are removed if either are
        #   interpolation objects. They are always accessible, the _** variables
        #   are passed to the evaluation.
        self.args = args
        self.kwargs = kwargs
        self._args = args
        self._kwargs = kwargs
        self.func = func

        if isinstance(self.func,interp2d) \
                or isinstance(self.func,BivariateSpline):
            self.interp_func = 1
            self._args = []
            self._kwargs = {}
        else:
            self.interp_func = 0
        
        #-- Evaluate the default grid with function. Set x as None first so it
        #   can actually evaluate.
        self.x = None
        self.y = None
        self.z = self.func(x,y,*self._args,**self._kwargs)
        
        #-- Now set x and y.
        self.x = x
        self.y = y 
示例#2
0
    def __init__(self, x, y, func, *args, **kwargs):
        '''
        Create an instance of the Profiler2D() class. Requires 2 coordinate 
        arrays and a function object for profile.
       
        The function can also be given as an interpolation object.
        
        The optional args and kwargs give the additional arguments for the 
        function, which are ignored in case func is an interpolation object.
        
        The default coordinate grids are both evaluated for the function. 
        They are saved in self.z, as an array of dimensions (x.size,y.size). 
        Alternatively, new evaluations can be attained through eval and diff.
        
        @param x: The default coordinates of the primary independent variable. 
                  Minimum three points.
        @type x: array
        @param y: The default coordinates of the secondary independent variable. 
                  Minimum three points.
        @type y: array
        @param func: The function that describes the profile with respect to x 
                     and y. Can be given as a 2D interpolation object.
        @type func: function/interpolation object
        
        @keyword args: Additional parameters passed to the functions when eval
                       or diff are called. 
                       
                       (default: [])
        @type args: tuple
        @keyword kwargs: Additional keywords passed to the functions when eval
                         or diff are called. 
                       
                         (default: {})
        @type kwargs: dict
        
        '''

        #-- If the function is given as a string, retrieve it from the local
        #   child module, or from the Profiler module (as parent). If the latter
        #   check if the function comes from one of Profiler's attributes, such
        #   as the loaded DataIO module.
        if isinstance(func, str):
            try:
                #-- Note that self.module refers to the child, not Profiler
                func = getattr(sys.modules[self.__module__], func)
            except AttributeError:
                #-- Recursively find the function of loaded modules in Profiler
                #   or a function of the Profiler module itself if no '.'
                func = DataIO.read(sys.modules[__name__], return_func=1)

        #-- set functional args, remember spline order. args/kwargs for
        #   func are saved separately. They are removed if either are
        #   interpolation objects. They are always accessible, the _** variables
        #   are passed to the evaluation.
        self.args = args
        self.kwargs = kwargs
        self._args = args
        self._kwargs = kwargs
        self.func = func

        if isinstance(self.func,interp2d) \
                or isinstance(self.func,BivariateSpline):
            self.interp_func = 1
            self._args = []
            self._kwargs = {}
        else:
            self.interp_func = 0

        #-- Evaluate the default grid with function. Set x as None first so it
        #   can actually evaluate.
        self.x = None
        self.y = None
        self.z = self.func(x, y, *self._args, **self._kwargs)

        #-- Now set x and y. Make sure they are arrays.
        self.x = Data.arrayify(x)
        self.y = Data.arrayify(y)
示例#3
0
    def __init__(self,x,func=interp_file,dfunc=None,order=3,*args,\
                 **kwargs):
    
        '''
        Create an instance of the Profiler() class. Requires a coordinate grid 
        and a function object for the profile. A function for the derivative is
        optional. The functions can also be given as an interpolation object.
        
        The optional args and kwargs give the additional arguments for the 
        two function, which are ignored in case func is an interpolation object.
        
        The default coordinate grid is evaluated for both the function and the
        derivative. They are saved in self.y and self.dydx. Alternatively, new
        evaluations can be attained through eval and diff.
        
        Note that if func is an interpolator object, the original input x and y
        grids can be passed as additional keywords xin and yin, which would then
        be arrays. Otherwise, the x and the interpolator(x) are set as xin and 
        yin. xin and yin are ignored if func is a function, even if it returns
        an interpolator (in which case the original grids are known)
        
        @param x: The default coordinate points, minimum three points. In the 
                  case of an interpolation function, this is the default grid
                  returned by the instance. The original x/y of the 
                  interpolated profile are saved as xori/yori in the object.
        @type x: array
        
        @keyword func: The function that describes the profile with respect to 
                       x. Can be given as an interp1d object. Default is a read
                       function that interpolates data and returns the 
                       interpolator object. If interpolation object, x and 
                       eval(x) are assumed to be the original grids, unless
                       xin and yin are given as keywords with arrays as values
                       for the original grids.
                       
                       (default: interp_file)
        @type func: function/interp1d object
        @keyword dfunc: Function that describes the derivative of the profile 
                        with respect to x. Can be given as an interpolation 
                        object. If None, a generic central difference is taken & 
                        interpolated with a spline of which the order can be 
                        chosen.
        
                        (default: None)
        @type dfunc: function/interpolation object
        @keyword order: Order of the spline interpolation of the derivative. 
                        Default is cubic. Not used for the interpolation if func
                        returns an interpolation object. Use read_order in that
                        case.
                        
                        (default: 3)
        @type order: int
        
        @keyword args: Additional parameters passed to the functions when eval
                       or diff are called. 
                       
                       (default: [])
        @type args: tuple
        @keyword kwargs: Additional keywords passed to the functions when eval
                         or diff are called. 
                       
                         (default: {})
        @type kwargs: dict
        
        '''
        
        #-- Check len of coordinate input. Cannot be less than three for the 
        #   derivative.
        if len(x) < 3:
            raise ValueError('Coordinate grid must have more than 2 elements.')
        
        #-- If the function is given as a string, retrieve it from the local 
        #   child module, or from the Profiler module (as parent). If the latter
        #   case then check if the function comes from one of Profiler's 
        #   attributes, such as the loaded DataIO module.
        #   Can also simply be a constant value, so try making a float first.
        if isinstance(func,str):
            try:
                #-- Check if a constant was given, rather than a function
                kwargs['c'] = float(func)
                func = constant

            except ValueError:
                #-- Note that self.module refers to the child, not Profiler
                if hasattr(sys.modules[self.__module__],func):
                    func = getattr(sys.modules[self.__module__],func)

                #-- Recursively find the function of loaded modules in Profiler
                #   or a function of the Profiler module itself if no '.'         
                else:
                    
                    func = DataIO.read(func=func,module=sys.modules[__name__],\
                                       return_func=1)
        
        #-- set functional args, remember spline order. args/kwargs for 
        #   func and dfunc are saved separately. They are removed if either are
        #   interpolation objects. They are always accessible, the _** variables
        #   are passed to the evaluation.
        self.args = args
        self.kwargs = kwargs
        self._args = self.args
        self._dargs = self.args
        self._dkwargs = self.kwargs
        self._kwargs = self.kwargs
        self.order = order
        
        #-- Grab default xin and yin keys in case they are included, and remove
        #   from the arguments dictionary. None if not available. 
        xin = self.kwargs.pop('xin',None)
        yin = self.kwargs.pop('yin',None)
        
        #-- By default no interpolation, so leave this off.
        self.interp_func = 0
        self.interp_dfunc = 0
        
        #-- Defaults for xori/yori, not used in case of normal functions
        self.xin = np.empty(0)
        self.yin = np.empty(0)
        
        #-- Evaluate the default grid with function. Set x as None first so it
        #   can actually evaluate. Set x once derivative has been evaluated. 
        self.x = None
        if not (isinstance(func,interp1d) or isinstance(func,UnivariateSpline)):
            #-- Evaluate the function, and check what is returned: array or 
            #   interpolation object
            y = func(x,*args,**kwargs)
            
            #-- Interpolation object: so set that as this instance's func. In 
            #   this case, the variable x passed to the class is the default x
            #   grid of this instance. The original x/y-grid is saved as xi, yi
            if isinstance(y,tuple):
                self.func = y[2]
                self.xin = y[0]
                self.yin = y[1]
                self._args = []
                self._kwargs = {}
                self.interp_func = 1
                self.y = self.func(x)
            else: 
                self.func = func
                self.y = y
                
        #-- func is an interpolation object, so just run the normal evaluation.
        #   Set _args/_kwargs to empty, so none are ever passed to the interpol
        else:
            self.func = func
            self._args = []
            self._kwargs = {}
            self.interp_func = 1
            self.y = self.func(x)
            self.yin = yin if not yin is None else self.y
            self.xin = xin if not xin is None else x
                                                    
        #-- Set the derivative function, resorting to default if needed
        if not dfunc is None:
            self.dfunc = dfunc
        elif self.func == constant:
            self.dfunc = zero
        else: 
            #-- Extend array slightly to allow odeint to succeed.
            #   Need better fix for this.
            #x0 = x[0]-(x[1]-x[0])#*0.5
            #xn = x[-1]+(x[-1]-x[-2])#*0.5
            #x_ext = np.hstack([[x0],x,[xn]])
            
            #-- Evaluate the function, and set up an interpolator for 
            #   central difference. The interpolator will extrapolate 
            #   beyond the given x range. This is necessary for odeint to work.
            #   Usually x-range is not exceeded much. 
            self.dfunc = spline1d(x=x,y=op.diff_central(self.y,x),\
                                  k=self.order)
        
        if (isinstance(self.dfunc,interp1d) \
                or isinstance(self.dfunc,UnivariateSpline)):
            self._dargs = []
            self._dkwargs = {}
            self.interp_dfunc = 1
        
        #-- Evaluate the derivative with the default grid
        self.dydx = self.dfunc(x,*self._dargs,**self._dkwargs)
        
        #-- Now set x.
        self.x = x
示例#4
0
    def __init__(self,x,func=interp_file,dfunc=None,order=3,*args,\
                 **kwargs):
        '''
        Create an instance of the Profiler() class. Requires a coordinate grid 
        and a function object for the profile. A function for the derivative is
        optional. The functions can also be given as an interpolation object.
        
        The optional args and kwargs give the additional arguments for the 
        two function, which are ignored in case func is an interpolation object.
        
        The default coordinate grid is evaluated for both the function and the
        derivative. They are saved in self.y and self.dydx. Alternatively, new
        evaluations can be attained through eval and diff.
        
        Note that if func is an interpolator object, the original input x and y
        grids can be passed as additional keywords xin and yin, which would then
        be arrays. Otherwise, the x and the interpolator(x) are set as xin and 
        yin. xin and yin are ignored if func is a function, even if it returns
        an interpolator (in which case the original grids are known)
        
        @param x: The default coordinate points, minimum three points. In the 
                  case of an interpolation function, this is the default grid
                  returned by the instance. The original x/y of the 
                  interpolated profile are saved as xori/yori in the object.
        @type x: array
        
        @keyword func: The function that describes the profile with respect to 
                       x. Can be given as an interp1d object. Default is a read
                       function that interpolates data and returns the 
                       interpolator object. If interpolation object, x and 
                       eval(x) are assumed to be the original grids, unless
                       xin and yin are given as keywords with arrays as values
                       for the original grids.
                       
                       (default: interp_file)
        @type func: function/interp1d object
        @keyword dfunc: Function that describes the derivative of the profile 
                        with respect to x. Can be given as an interpolation 
                        object. If None, a generic central difference is taken & 
                        interpolated with a spline of which the order can be 
                        chosen.
        
                        (default: None)
        @type dfunc: function/interpolation object
        @keyword order: Order of the spline interpolation of the derivative. 
                        Default is cubic. Not used for the interpolation if func
                        returns an interpolation object. Use read_order in that
                        case.
                        
                        (default: 3)
        @type order: int
        
        @keyword args: Additional parameters passed to the functions when eval
                       or diff are called. 
                       
                       (default: [])
        @type args: tuple
        @keyword kwargs: Additional keywords passed to the functions when eval
                         or diff are called. 
                       
                         (default: {})
        @type kwargs: dict
        
        '''

        #-- Check len of coordinate input. Cannot be less than three for the
        #   derivative.
        if len(x) < 3:
            raise ValueError('Coordinate grid must have more than 2 elements.')

        #-- If the function is given as a string, retrieve it from the local
        #   child module, or from the Profiler module (as parent). If the latter
        #   case then check if the function comes from one of Profiler's
        #   attributes, such as the loaded DataIO module.
        #   Can also simply be a constant value, so try making a float first.
        if isinstance(func, str):
            try:
                #-- Check if a constant was given, rather than a function
                kwargs['c'] = float(func)
                func = constant

            except ValueError:
                #-- Note that self.module refers to the child, not Profiler
                if hasattr(sys.modules[self.__module__], func):
                    func = getattr(sys.modules[self.__module__], func)

                #-- Recursively find the function of loaded modules in Profiler
                #   or a function of the Profiler module itself if no '.'
                else:

                    func = DataIO.read(func=func,module=sys.modules[__name__],\
                                       return_func=1)

        #-- set functional args, remember spline order. args/kwargs for
        #   func and dfunc are saved separately. They are removed if either are
        #   interpolation objects. They are always accessible, the _** variables
        #   are passed to the evaluation.
        self.args = args
        self.kwargs = kwargs
        self._args = self.args
        self._dargs = self.args
        self._dkwargs = self.kwargs
        self._kwargs = self.kwargs
        self.order = order

        #-- Grab default xin and yin keys in case they are included, and remove
        #   from the arguments dictionary. None if not available.
        xin = self.kwargs.pop('xin', None)
        yin = self.kwargs.pop('yin', None)

        #-- By default no interpolation, so leave this off.
        self.interp_func = 0
        self.interp_dfunc = 0

        #-- Defaults for xori/yori, not used in case of normal functions
        self.xin = np.empty(0)
        self.yin = np.empty(0)

        #-- Evaluate the default grid with function. Set x as None first so it
        #   can actually evaluate. Set x once derivative has been evaluated.
        self.x = None
        if not (isinstance(func, interp1d)
                or isinstance(func, UnivariateSpline)):
            #-- Evaluate the function, and check what is returned: array or
            #   interpolation object
            y = func(x, *args, **kwargs)

            #-- Interpolation object: so set that as this instance's func. In
            #   this case, the variable x passed to the class is the default x
            #   grid of this instance. The original x/y-grid is saved as xi, yi
            if isinstance(y, tuple):
                self.func = y[2]
                self.xin = y[0]
                self.yin = y[1]
                self._args = []
                self._kwargs = {}
                self.interp_func = 1
                self.y = self.func(x)
            else:
                self.func = func
                self.y = y

        #-- func is an interpolation object, so just run the normal evaluation.
        #   Set _args/_kwargs to empty, so none are ever passed to the interpol
        else:
            self.func = func
            self._args = []
            self._kwargs = {}
            self.interp_func = 1
            self.y = self.func(x)
            self.yin = yin if not yin is None else self.y
            self.xin = xin if not xin is None else x

        #-- Set the derivative function, resorting to default if needed
        if not dfunc is None:
            self.dfunc = dfunc
        elif self.func == constant:
            self.dfunc = zero
        else:
            #-- Extend array slightly to allow odeint to succeed.
            #   Need better fix for this.
            #x0 = x[0]-(x[1]-x[0])#*0.5
            #xn = x[-1]+(x[-1]-x[-2])#*0.5
            #x_ext = np.hstack([[x0],x,[xn]])

            #-- Evaluate the function, and set up an interpolator for
            #   central difference. The interpolator will extrapolate
            #   beyond the given x range. This is necessary for odeint to work.
            #   Usually x-range is not exceeded much.
            self.dfunc = spline1d(x=x,y=op.diff_central(self.y,x),\
                                  k=self.order)

        if (isinstance(self.dfunc,interp1d) \
                or isinstance(self.dfunc,UnivariateSpline)):
            self._dargs = []
            self._dkwargs = {}
            self.interp_dfunc = 1

        #-- Evaluate the derivative with the default grid
        self.dydx = self.dfunc(x, *self._dargs, **self._dkwargs)

        #-- Now set x.
        self.x = Data.arrayify(x)