repo_name
stringlengths
7
60
path
stringlengths
6
134
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
1.04k
149k
license
stringclasses
12 values
postvakje/sympy
sympy/plotting/plot.py
7
65097
"""Plotting module for Sympy. A plot is represented by the ``Plot`` class that contains a reference to the backend and a list of the data series to be plotted. The data series are instances of classes meant to simplify getting points and meshes from sympy expressions. ``plot_backends`` is a dictionary with all the backends. This module gives only the essential. For all the fancy stuff use directly the backend. You can get the backend wrapper for every plot from the ``_backend`` attribute. Moreover the data series classes have various useful methods like ``get_points``, ``get_segments``, ``get_meshes``, etc, that may be useful if you wish to use another plotting library. Especially if you need publication ready graphs and this module is not enough for you - just get the ``_backend`` attribute and add whatever you want directly to it. In the case of matplotlib (the common way to graph data in python) just copy ``_backend.fig`` which is the figure and ``_backend.ax`` which is the axis and work on them as you would on any other matplotlib object. Simplicity of code takes much greater importance than performance. Don't use it if you care at all about performance. A new backend instance is initialized every time you call ``show()`` and the old one is left to the garbage collector. """ from __future__ import print_function, division import inspect from collections import Callable import warnings import sys from sympy import sympify, Expr, Tuple, Dummy, Symbol from sympy.external import import_module from sympy.core.compatibility import range from sympy.utilities.decorator import doctest_depends_on from sympy.utilities.iterables import is_sequence from .experimental_lambdify import (vectorized_lambdify, lambdify) # N.B. # When changing the minimum module version for matplotlib, please change # the same in the `SymPyDocTestFinder`` in `sympy/utilities/runtests.py` # Backend specific imports - textplot from sympy.plotting.textplot import textplot # Global variable # Set to False when running tests / doctests so that the plots don't show. _show = True def unset_show(): global _show _show = False ############################################################################## # The public interface ############################################################################## def _arity(f): """ Python 2 and 3 compatible version that do not raise a Deprecation warning. """ if sys.version_info < (3,): return len(inspect.getargspec(f)[0]) else: param = inspect.signature(f).parameters.values() return len([p for p in param if p.kind == p.POSITIONAL_OR_KEYWORD]) class Plot(object): """The central class of the plotting module. For interactive work the function ``plot`` is better suited. This class permits the plotting of sympy expressions using numerous backends (matplotlib, textplot, the old pyglet module for sympy, Google charts api, etc). The figure can contain an arbitrary number of plots of sympy expressions, lists of coordinates of points, etc. Plot has a private attribute _series that contains all data series to be plotted (expressions for lines or surfaces, lists of points, etc (all subclasses of BaseSeries)). Those data series are instances of classes not imported by ``from sympy import *``. The customization of the figure is on two levels. Global options that concern the figure as a whole (eg title, xlabel, scale, etc) and per-data series options (eg name) and aesthetics (eg. color, point shape, line type, etc.). The difference between options and aesthetics is that an aesthetic can be a function of the coordinates (or parameters in a parametric plot). The supported values for an aesthetic are: - None (the backend uses default values) - a constant - a function of one variable (the first coordinate or parameter) - a function of two variables (the first and second coordinate or parameters) - a function of three variables (only in nonparametric 3D plots) Their implementation depends on the backend so they may not work in some backends. If the plot is parametric and the arity of the aesthetic function permits it the aesthetic is calculated over parameters and not over coordinates. If the arity does not permit calculation over parameters the calculation is done over coordinates. Only cartesian coordinates are supported for the moment, but you can use the parametric plots to plot in polar, spherical and cylindrical coordinates. The arguments for the constructor Plot must be subclasses of BaseSeries. Any global option can be specified as a keyword argument. The global options for a figure are: - title : str - xlabel : str - ylabel : str - legend : bool - xscale : {'linear', 'log'} - yscale : {'linear', 'log'} - axis : bool - axis_center : tuple of two floats or {'center', 'auto'} - xlim : tuple of two floats - ylim : tuple of two floats - aspect_ratio : tuple of two floats or {'auto'} - autoscale : bool - margin : float in [0, 1] The per data series options and aesthetics are: There are none in the base series. See below for options for subclasses. Some data series support additional aesthetics or options: ListSeries, LineOver1DRangeSeries, Parametric2DLineSeries, Parametric3DLineSeries support the following: Aesthetics: - line_color : function which returns a float. options: - label : str - steps : bool - integers_only : bool SurfaceOver2DRangeSeries, ParametricSurfaceSeries support the following: aesthetics: - surface_color : function which returns a float. """ def __init__(self, *args, **kwargs): super(Plot, self).__init__() # Options for the graph as a whole. # The possible values for each option are described in the docstring of # Plot. They are based purely on convention, no checking is done. self.title = None self.xlabel = None self.ylabel = None self.aspect_ratio = 'auto' self.xlim = None self.ylim = None self.axis_center = 'auto' self.axis = True self.xscale = 'linear' self.yscale = 'linear' self.legend = False self.autoscale = True self.margin = 0 # Contains the data objects to be plotted. The backend should be smart # enough to iterate over this list. self._series = [] self._series.extend(args) # The backend type. On every show() a new backend instance is created # in self._backend which is tightly coupled to the Plot instance # (thanks to the parent attribute of the backend). self.backend = DefaultBackend # The keyword arguments should only contain options for the plot. for key, val in kwargs.items(): if hasattr(self, key): setattr(self, key, val) def show(self): # TODO move this to the backend (also for save) if hasattr(self, '_backend'): self._backend.close() self._backend = self.backend(self) self._backend.show() def save(self, path): if hasattr(self, '_backend'): self._backend.close() self._backend = self.backend(self) self._backend.save(path) def __str__(self): series_strs = [('[%d]: ' % i) + str(s) for i, s in enumerate(self._series)] return 'Plot object containing:\n' + '\n'.join(series_strs) def __getitem__(self, index): return self._series[index] def __setitem__(self, index, *args): if len(args) == 1 and isinstance(args[0], BaseSeries): self._series[index] = args def __delitem__(self, index): del self._series[index] @doctest_depends_on(modules=('numpy', 'matplotlib',)) def append(self, arg): """Adds an element from a plot's series to an existing plot. Examples ======== Consider two ``Plot`` objects, ``p1`` and ``p2``. To add the second plot's first series object to the first, use the ``append`` method, like so: >>> from sympy import symbols >>> from sympy.plotting import plot >>> x = symbols('x') >>> p1 = plot(x*x) >>> p2 = plot(x) >>> p1.append(p2[0]) >>> p1 Plot object containing: [0]: cartesian line: x**2 for x over (-10.0, 10.0) [1]: cartesian line: x for x over (-10.0, 10.0) See Also ======== extend """ if isinstance(arg, BaseSeries): self._series.append(arg) else: raise TypeError('Must specify element of plot to append.') @doctest_depends_on(modules=('numpy', 'matplotlib',)) def extend(self, arg): """Adds all series from another plot. Examples ======== Consider two ``Plot`` objects, ``p1`` and ``p2``. To add the second plot to the first, use the ``extend`` method, like so: >>> from sympy import symbols >>> from sympy.plotting import plot >>> x = symbols('x') >>> p1 = plot(x*x) >>> p2 = plot(x) >>> p1.extend(p2) >>> p1 Plot object containing: [0]: cartesian line: x**2 for x over (-10.0, 10.0) [1]: cartesian line: x for x over (-10.0, 10.0) """ if isinstance(arg, Plot): self._series.extend(arg._series) elif is_sequence(arg): self._series.extend(arg) else: raise TypeError('Expecting Plot or sequence of BaseSeries') ############################################################################## # Data Series ############################################################################## #TODO more general way to calculate aesthetics (see get_color_array) ### The base class for all series class BaseSeries(object): """Base class for the data objects containing stuff to be plotted. The backend should check if it supports the data series that it's given. (eg TextBackend supports only LineOver1DRange). It's the backend responsibility to know how to use the class of data series that it's given. Some data series classes are grouped (using a class attribute like is_2Dline) according to the api they present (based only on convention). The backend is not obliged to use that api (eg. The LineOver1DRange belongs to the is_2Dline group and presents the get_points method, but the TextBackend does not use the get_points method). """ # Some flags follow. The rationale for using flags instead of checking base # classes is that setting multiple flags is simpler than multiple # inheritance. is_2Dline = False # Some of the backends expect: # - get_points returning 1D np.arrays list_x, list_y # - get_segments returning np.array (done in Line2DBaseSeries) # - get_color_array returning 1D np.array (done in Line2DBaseSeries) # with the colors calculated at the points from get_points is_3Dline = False # Some of the backends expect: # - get_points returning 1D np.arrays list_x, list_y, list_y # - get_segments returning np.array (done in Line2DBaseSeries) # - get_color_array returning 1D np.array (done in Line2DBaseSeries) # with the colors calculated at the points from get_points is_3Dsurface = False # Some of the backends expect: # - get_meshes returning mesh_x, mesh_y, mesh_z (2D np.arrays) # - get_points an alias for get_meshes is_contour = False # Some of the backends expect: # - get_meshes returning mesh_x, mesh_y, mesh_z (2D np.arrays) # - get_points an alias for get_meshes is_implicit = False # Some of the backends expect: # - get_meshes returning mesh_x (1D array), mesh_y(1D array, # mesh_z (2D np.arrays) # - get_points an alias for get_meshes #Different from is_contour as the colormap in backend will be #different is_parametric = False # The calculation of aesthetics expects: # - get_parameter_points returning one or two np.arrays (1D or 2D) # used for calculation aesthetics def __init__(self): super(BaseSeries, self).__init__() @property def is_3D(self): flags3D = [ self.is_3Dline, self.is_3Dsurface ] return any(flags3D) @property def is_line(self): flagslines = [ self.is_2Dline, self.is_3Dline ] return any(flagslines) ### 2D lines class Line2DBaseSeries(BaseSeries): """A base class for 2D lines. - adding the label, steps and only_integers options - making is_2Dline true - defining get_segments and get_color_array """ is_2Dline = True _dim = 2 def __init__(self): super(Line2DBaseSeries, self).__init__() self.label = None self.steps = False self.only_integers = False self.line_color = None def get_segments(self): np = import_module('numpy') points = self.get_points() if self.steps is True: x = np.array((points[0], points[0])).T.flatten()[1:] y = np.array((points[1], points[1])).T.flatten()[:-1] points = (x, y) points = np.ma.array(points).T.reshape(-1, 1, self._dim) return np.ma.concatenate([points[:-1], points[1:]], axis=1) def get_color_array(self): np = import_module('numpy') c = self.line_color if hasattr(c, '__call__'): f = np.vectorize(c) arity = _arity(c) if arity == 1 and self.is_parametric: x = self.get_parameter_points() return f(centers_of_segments(x)) else: variables = list(map(centers_of_segments, self.get_points())) if arity == 1: return f(variables[0]) elif arity == 2: return f(*variables[:2]) else: # only if the line is 3D (otherwise raises an error) return f(*variables) else: return c*np.ones(self.nb_of_points) class List2DSeries(Line2DBaseSeries): """Representation for a line consisting of list of points.""" def __init__(self, list_x, list_y): np = import_module('numpy') super(List2DSeries, self).__init__() self.list_x = np.array(list_x) self.list_y = np.array(list_y) self.label = 'list' def __str__(self): return 'list plot' def get_points(self): return (self.list_x, self.list_y) class LineOver1DRangeSeries(Line2DBaseSeries): """Representation for a line consisting of a SymPy expression over a range.""" def __init__(self, expr, var_start_end, **kwargs): super(LineOver1DRangeSeries, self).__init__() self.expr = sympify(expr) self.label = str(self.expr) self.var = sympify(var_start_end[0]) self.start = float(var_start_end[1]) self.end = float(var_start_end[2]) self.nb_of_points = kwargs.get('nb_of_points', 300) self.adaptive = kwargs.get('adaptive', True) self.depth = kwargs.get('depth', 12) self.line_color = kwargs.get('line_color', None) def __str__(self): return 'cartesian line: %s for %s over %s' % ( str(self.expr), str(self.var), str((self.start, self.end))) def get_segments(self): """ Adaptively gets segments for plotting. The adaptive sampling is done by recursively checking if three points are almost collinear. If they are not collinear, then more points are added between those points. References ========== [1] Adaptive polygonal approximation of parametric curves, Luiz Henrique de Figueiredo. """ if self.only_integers or not self.adaptive: return super(LineOver1DRangeSeries, self).get_segments() else: f = lambdify([self.var], self.expr) list_segments = [] def sample(p, q, depth): """ Samples recursively if three points are almost collinear. For depth < 6, points are added irrespective of whether they satisfy the collinearity condition or not. The maximum depth allowed is 12. """ np = import_module('numpy') #Randomly sample to avoid aliasing. random = 0.45 + np.random.rand() * 0.1 xnew = p[0] + random * (q[0] - p[0]) ynew = f(xnew) new_point = np.array([xnew, ynew]) #Maximum depth if depth > self.depth: list_segments.append([p, q]) #Sample irrespective of whether the line is flat till the #depth of 6. We are not using linspace to avoid aliasing. elif depth < 6: sample(p, new_point, depth + 1) sample(new_point, q, depth + 1) #Sample ten points if complex values are encountered #at both ends. If there is a real value in between, then #sample those points further. elif p[1] is None and q[1] is None: xarray = np.linspace(p[0], q[0], 10) yarray = list(map(f, xarray)) if any(y is not None for y in yarray): for i in range(len(yarray) - 1): if yarray[i] is not None or yarray[i + 1] is not None: sample([xarray[i], yarray[i]], [xarray[i + 1], yarray[i + 1]], depth + 1) #Sample further if one of the end points in None( i.e. a complex #value) or the three points are not almost collinear. elif (p[1] is None or q[1] is None or new_point[1] is None or not flat(p, new_point, q)): sample(p, new_point, depth + 1) sample(new_point, q, depth + 1) else: list_segments.append([p, q]) f_start = f(self.start) f_end = f(self.end) sample([self.start, f_start], [self.end, f_end], 0) return list_segments def get_points(self): np = import_module('numpy') if self.only_integers is True: list_x = np.linspace(int(self.start), int(self.end), num=int(self.end) - int(self.start) + 1) else: list_x = np.linspace(self.start, self.end, num=self.nb_of_points) f = vectorized_lambdify([self.var], self.expr) list_y = f(list_x) return (list_x, list_y) class Parametric2DLineSeries(Line2DBaseSeries): """Representation for a line consisting of two parametric sympy expressions over a range.""" is_parametric = True def __init__(self, expr_x, expr_y, var_start_end, **kwargs): super(Parametric2DLineSeries, self).__init__() self.expr_x = sympify(expr_x) self.expr_y = sympify(expr_y) self.label = "(%s, %s)" % (str(self.expr_x), str(self.expr_y)) self.var = sympify(var_start_end[0]) self.start = float(var_start_end[1]) self.end = float(var_start_end[2]) self.nb_of_points = kwargs.get('nb_of_points', 300) self.adaptive = kwargs.get('adaptive', True) self.depth = kwargs.get('depth', 12) self.line_color = kwargs.get('line_color', None) def __str__(self): return 'parametric cartesian line: (%s, %s) for %s over %s' % ( str(self.expr_x), str(self.expr_y), str(self.var), str((self.start, self.end))) def get_parameter_points(self): np = import_module('numpy') return np.linspace(self.start, self.end, num=self.nb_of_points) def get_points(self): param = self.get_parameter_points() fx = vectorized_lambdify([self.var], self.expr_x) fy = vectorized_lambdify([self.var], self.expr_y) list_x = fx(param) list_y = fy(param) return (list_x, list_y) def get_segments(self): """ Adaptively gets segments for plotting. The adaptive sampling is done by recursively checking if three points are almost collinear. If they are not collinear, then more points are added between those points. References ========== [1] Adaptive polygonal approximation of parametric curves, Luiz Henrique de Figueiredo. """ if not self.adaptive: return super(Parametric2DLineSeries, self).get_segments() f_x = lambdify([self.var], self.expr_x) f_y = lambdify([self.var], self.expr_y) list_segments = [] def sample(param_p, param_q, p, q, depth): """ Samples recursively if three points are almost collinear. For depth < 6, points are added irrespective of whether they satisfy the collinearity condition or not. The maximum depth allowed is 12. """ #Randomly sample to avoid aliasing. np = import_module('numpy') random = 0.45 + np.random.rand() * 0.1 param_new = param_p + random * (param_q - param_p) xnew = f_x(param_new) ynew = f_y(param_new) new_point = np.array([xnew, ynew]) #Maximum depth if depth > self.depth: list_segments.append([p, q]) #Sample irrespective of whether the line is flat till the #depth of 6. We are not using linspace to avoid aliasing. elif depth < 6: sample(param_p, param_new, p, new_point, depth + 1) sample(param_new, param_q, new_point, q, depth + 1) #Sample ten points if complex values are encountered #at both ends. If there is a real value in between, then #sample those points further. elif ((p[0] is None and q[1] is None) or (p[1] is None and q[1] is None)): param_array = np.linspace(param_p, param_q, 10) x_array = list(map(f_x, param_array)) y_array = list(map(f_y, param_array)) if any(x is not None and y is not None for x, y in zip(x_array, y_array)): for i in range(len(y_array) - 1): if ((x_array[i] is not None and y_array[i] is not None) or (x_array[i + 1] is not None and y_array[i + 1] is not None)): point_a = [x_array[i], y_array[i]] point_b = [x_array[i + 1], y_array[i + 1]] sample(param_array[i], param_array[i], point_a, point_b, depth + 1) #Sample further if one of the end points in None( ie a complex #value) or the three points are not almost collinear. elif (p[0] is None or p[1] is None or q[1] is None or q[0] is None or not flat(p, new_point, q)): sample(param_p, param_new, p, new_point, depth + 1) sample(param_new, param_q, new_point, q, depth + 1) else: list_segments.append([p, q]) f_start_x = f_x(self.start) f_start_y = f_y(self.start) start = [f_start_x, f_start_y] f_end_x = f_x(self.end) f_end_y = f_y(self.end) end = [f_end_x, f_end_y] sample(self.start, self.end, start, end, 0) return list_segments ### 3D lines class Line3DBaseSeries(Line2DBaseSeries): """A base class for 3D lines. Most of the stuff is derived from Line2DBaseSeries.""" is_2Dline = False is_3Dline = True _dim = 3 def __init__(self): super(Line3DBaseSeries, self).__init__() class Parametric3DLineSeries(Line3DBaseSeries): """Representation for a 3D line consisting of two parametric sympy expressions and a range.""" def __init__(self, expr_x, expr_y, expr_z, var_start_end, **kwargs): super(Parametric3DLineSeries, self).__init__() self.expr_x = sympify(expr_x) self.expr_y = sympify(expr_y) self.expr_z = sympify(expr_z) self.label = "(%s, %s)" % (str(self.expr_x), str(self.expr_y)) self.var = sympify(var_start_end[0]) self.start = float(var_start_end[1]) self.end = float(var_start_end[2]) self.nb_of_points = kwargs.get('nb_of_points', 300) self.line_color = kwargs.get('line_color', None) def __str__(self): return '3D parametric cartesian line: (%s, %s, %s) for %s over %s' % ( str(self.expr_x), str(self.expr_y), str(self.expr_z), str(self.var), str((self.start, self.end))) def get_parameter_points(self): np = import_module('numpy') return np.linspace(self.start, self.end, num=self.nb_of_points) def get_points(self): param = self.get_parameter_points() fx = vectorized_lambdify([self.var], self.expr_x) fy = vectorized_lambdify([self.var], self.expr_y) fz = vectorized_lambdify([self.var], self.expr_z) list_x = fx(param) list_y = fy(param) list_z = fz(param) return (list_x, list_y, list_z) ### Surfaces class SurfaceBaseSeries(BaseSeries): """A base class for 3D surfaces.""" is_3Dsurface = True def __init__(self): super(SurfaceBaseSeries, self).__init__() self.surface_color = None def get_color_array(self): np = import_module('numpy') c = self.surface_color if isinstance(c, Callable): f = np.vectorize(c) arity = _arity(c) if self.is_parametric: variables = list(map(centers_of_faces, self.get_parameter_meshes())) if arity == 1: return f(variables[0]) elif arity == 2: return f(*variables) variables = list(map(centers_of_faces, self.get_meshes())) if arity == 1: return f(variables[0]) elif arity == 2: return f(*variables[:2]) else: return f(*variables) else: return c*np.ones(self.nb_of_points) class SurfaceOver2DRangeSeries(SurfaceBaseSeries): """Representation for a 3D surface consisting of a sympy expression and 2D range.""" def __init__(self, expr, var_start_end_x, var_start_end_y, **kwargs): super(SurfaceOver2DRangeSeries, self).__init__() self.expr = sympify(expr) self.var_x = sympify(var_start_end_x[0]) self.start_x = float(var_start_end_x[1]) self.end_x = float(var_start_end_x[2]) self.var_y = sympify(var_start_end_y[0]) self.start_y = float(var_start_end_y[1]) self.end_y = float(var_start_end_y[2]) self.nb_of_points_x = kwargs.get('nb_of_points_x', 50) self.nb_of_points_y = kwargs.get('nb_of_points_y', 50) self.surface_color = kwargs.get('surface_color', None) def __str__(self): return ('cartesian surface: %s for' ' %s over %s and %s over %s') % ( str(self.expr), str(self.var_x), str((self.start_x, self.end_x)), str(self.var_y), str((self.start_y, self.end_y))) def get_meshes(self): np = import_module('numpy') mesh_x, mesh_y = np.meshgrid(np.linspace(self.start_x, self.end_x, num=self.nb_of_points_x), np.linspace(self.start_y, self.end_y, num=self.nb_of_points_y)) f = vectorized_lambdify((self.var_x, self.var_y), self.expr) return (mesh_x, mesh_y, f(mesh_x, mesh_y)) class ParametricSurfaceSeries(SurfaceBaseSeries): """Representation for a 3D surface consisting of three parametric sympy expressions and a range.""" is_parametric = True def __init__( self, expr_x, expr_y, expr_z, var_start_end_u, var_start_end_v, **kwargs): super(ParametricSurfaceSeries, self).__init__() self.expr_x = sympify(expr_x) self.expr_y = sympify(expr_y) self.expr_z = sympify(expr_z) self.var_u = sympify(var_start_end_u[0]) self.start_u = float(var_start_end_u[1]) self.end_u = float(var_start_end_u[2]) self.var_v = sympify(var_start_end_v[0]) self.start_v = float(var_start_end_v[1]) self.end_v = float(var_start_end_v[2]) self.nb_of_points_u = kwargs.get('nb_of_points_u', 50) self.nb_of_points_v = kwargs.get('nb_of_points_v', 50) self.surface_color = kwargs.get('surface_color', None) def __str__(self): return ('parametric cartesian surface: (%s, %s, %s) for' ' %s over %s and %s over %s') % ( str(self.expr_x), str(self.expr_y), str(self.expr_z), str(self.var_u), str((self.start_u, self.end_u)), str(self.var_v), str((self.start_v, self.end_v))) def get_parameter_meshes(self): np = import_module('numpy') return np.meshgrid(np.linspace(self.start_u, self.end_u, num=self.nb_of_points_u), np.linspace(self.start_v, self.end_v, num=self.nb_of_points_v)) def get_meshes(self): mesh_u, mesh_v = self.get_parameter_meshes() fx = vectorized_lambdify((self.var_u, self.var_v), self.expr_x) fy = vectorized_lambdify((self.var_u, self.var_v), self.expr_y) fz = vectorized_lambdify((self.var_u, self.var_v), self.expr_z) return (fx(mesh_u, mesh_v), fy(mesh_u, mesh_v), fz(mesh_u, mesh_v)) ### Contours class ContourSeries(BaseSeries): """Representation for a contour plot.""" #The code is mostly repetition of SurfaceOver2DRange. #XXX: Presently not used in any of those functions. #XXX: Add contour plot and use this seties. is_contour = True def __init__(self, expr, var_start_end_x, var_start_end_y): super(ContourSeries, self).__init__() self.nb_of_points_x = 50 self.nb_of_points_y = 50 self.expr = sympify(expr) self.var_x = sympify(var_start_end_x[0]) self.start_x = float(var_start_end_x[1]) self.end_x = float(var_start_end_x[2]) self.var_y = sympify(var_start_end_y[0]) self.start_y = float(var_start_end_y[1]) self.end_y = float(var_start_end_y[2]) self.get_points = self.get_meshes def __str__(self): return ('contour: %s for ' '%s over %s and %s over %s') % ( str(self.expr), str(self.var_x), str((self.start_x, self.end_x)), str(self.var_y), str((self.start_y, self.end_y))) def get_meshes(self): np = import_module('numpy') mesh_x, mesh_y = np.meshgrid(np.linspace(self.start_x, self.end_x, num=self.nb_of_points_x), np.linspace(self.start_y, self.end_y, num=self.nb_of_points_y)) f = vectorized_lambdify((self.var_x, self.var_y), self.expr) return (mesh_x, mesh_y, f(mesh_x, mesh_y)) ############################################################################## # Backends ############################################################################## class BaseBackend(object): def __init__(self, parent): super(BaseBackend, self).__init__() self.parent = parent ## don't have to check for the success of importing matplotlib in each case; ## we will only be using this backend if we can successfully import matploblib class MatplotlibBackend(BaseBackend): def __init__(self, parent): super(MatplotlibBackend, self).__init__(parent) are_3D = [s.is_3D for s in self.parent._series] self.matplotlib = import_module('matplotlib', __import__kwargs={'fromlist': ['pyplot', 'cm', 'collections']}, min_module_version='1.1.0', catch=(RuntimeError,)) self.plt = self.matplotlib.pyplot self.cm = self.matplotlib.cm self.LineCollection = self.matplotlib.collections.LineCollection if any(are_3D) and not all(are_3D): raise ValueError('The matplotlib backend can not mix 2D and 3D.') elif not any(are_3D): self.fig = self.plt.figure() self.ax = self.fig.add_subplot(111) self.ax.spines['left'].set_position('zero') self.ax.spines['right'].set_color('none') self.ax.spines['bottom'].set_position('zero') self.ax.spines['top'].set_color('none') self.ax.spines['left'].set_smart_bounds(True) self.ax.spines['bottom'].set_smart_bounds(False) self.ax.xaxis.set_ticks_position('bottom') self.ax.yaxis.set_ticks_position('left') elif all(are_3D): ## mpl_toolkits.mplot3d is necessary for ## projection='3d' mpl_toolkits = import_module('mpl_toolkits', __import__kwargs={'fromlist': ['mplot3d']}) self.fig = self.plt.figure() self.ax = self.fig.add_subplot(111, projection='3d') def process_series(self): parent = self.parent for s in self.parent._series: # Create the collections if s.is_2Dline: collection = self.LineCollection(s.get_segments()) self.ax.add_collection(collection) elif s.is_contour: self.ax.contour(*s.get_meshes()) elif s.is_3Dline: # TODO too complicated, I blame matplotlib mpl_toolkits = import_module('mpl_toolkits', __import__kwargs={'fromlist': ['mplot3d']}) art3d = mpl_toolkits.mplot3d.art3d collection = art3d.Line3DCollection(s.get_segments()) self.ax.add_collection(collection) x, y, z = s.get_points() self.ax.set_xlim((min(x), max(x))) self.ax.set_ylim((min(y), max(y))) self.ax.set_zlim((min(z), max(z))) elif s.is_3Dsurface: x, y, z = s.get_meshes() collection = self.ax.plot_surface(x, y, z, cmap=self.cm.jet, rstride=1, cstride=1, linewidth=0.1) elif s.is_implicit: #Smart bounds have to be set to False for implicit plots. self.ax.spines['left'].set_smart_bounds(False) self.ax.spines['bottom'].set_smart_bounds(False) points = s.get_raster() if len(points) == 2: #interval math plotting x, y = _matplotlib_list(points[0]) self.ax.fill(x, y, facecolor=s.line_color, edgecolor='None') else: # use contourf or contour depending on whether it is # an inequality or equality. #XXX: ``contour`` plots multiple lines. Should be fixed. ListedColormap = self.matplotlib.colors.ListedColormap colormap = ListedColormap(["white", s.line_color]) xarray, yarray, zarray, plot_type = points if plot_type == 'contour': self.ax.contour(xarray, yarray, zarray, contours=(0, 0), fill=False, cmap=colormap) else: self.ax.contourf(xarray, yarray, zarray, cmap=colormap) else: raise ValueError('The matplotlib backend supports only ' 'is_2Dline, is_3Dline, is_3Dsurface and ' 'is_contour objects.') # Customise the collections with the corresponding per-series # options. if hasattr(s, 'label'): collection.set_label(s.label) if s.is_line and s.line_color: if isinstance(s.line_color, (float, int)) or isinstance(s.line_color, Callable): color_array = s.get_color_array() collection.set_array(color_array) else: collection.set_color(s.line_color) if s.is_3Dsurface and s.surface_color: if self.matplotlib.__version__ < "1.2.0": # TODO in the distant future remove this check warnings.warn('The version of matplotlib is too old to use surface coloring.') elif isinstance(s.surface_color, (float, int)) or isinstance(s.surface_color, Callable): color_array = s.get_color_array() color_array = color_array.reshape(color_array.size) collection.set_array(color_array) else: collection.set_color(s.surface_color) # Set global options. # TODO The 3D stuff # XXX The order of those is important. mpl_toolkits = import_module('mpl_toolkits', __import__kwargs={'fromlist': ['mplot3d']}) Axes3D = mpl_toolkits.mplot3d.Axes3D if parent.xscale and not isinstance(self.ax, Axes3D): self.ax.set_xscale(parent.xscale) if parent.yscale and not isinstance(self.ax, Axes3D): self.ax.set_yscale(parent.yscale) if parent.xlim: self.ax.set_xlim(parent.xlim) else: if all(isinstance(s, LineOver1DRangeSeries) for s in parent._series): starts = [s.start for s in parent._series] ends = [s.end for s in parent._series] self.ax.set_xlim(min(starts), max(ends)) if parent.ylim: self.ax.set_ylim(parent.ylim) if not isinstance(self.ax, Axes3D) or self.matplotlib.__version__ >= '1.2.0': # XXX in the distant future remove this check self.ax.set_autoscale_on(parent.autoscale) if parent.axis_center: val = parent.axis_center if isinstance(self.ax, Axes3D): pass elif val == 'center': self.ax.spines['left'].set_position('center') self.ax.spines['bottom'].set_position('center') elif val == 'auto': xl, xh = self.ax.get_xlim() yl, yh = self.ax.get_ylim() pos_left = ('data', 0) if xl*xh <= 0 else 'center' pos_bottom = ('data', 0) if yl*yh <= 0 else 'center' self.ax.spines['left'].set_position(pos_left) self.ax.spines['bottom'].set_position(pos_bottom) else: self.ax.spines['left'].set_position(('data', val[0])) self.ax.spines['bottom'].set_position(('data', val[1])) if not parent.axis: self.ax.set_axis_off() if parent.legend: if self.ax.legend(): self.ax.legend_.set_visible(parent.legend) if parent.margin: self.ax.set_xmargin(parent.margin) self.ax.set_ymargin(parent.margin) if parent.title: self.ax.set_title(parent.title) if parent.xlabel: self.ax.set_xlabel(parent.xlabel, position=(1, 0)) if parent.ylabel: self.ax.set_ylabel(parent.ylabel, position=(0, 1)) def show(self): self.process_series() #TODO after fixing https://github.com/ipython/ipython/issues/1255 # you can uncomment the next line and remove the pyplot.show() call #self.fig.show() if _show: self.plt.show() def save(self, path): self.process_series() self.fig.savefig(path) def close(self): self.plt.close(self.fig) class TextBackend(BaseBackend): def __init__(self, parent): super(TextBackend, self).__init__(parent) def show(self): if len(self.parent._series) != 1: raise ValueError( 'The TextBackend supports only one graph per Plot.') elif not isinstance(self.parent._series[0], LineOver1DRangeSeries): raise ValueError( 'The TextBackend supports only expressions over a 1D range') else: ser = self.parent._series[0] textplot(ser.expr, ser.start, ser.end) def close(self): pass class DefaultBackend(BaseBackend): def __new__(cls, parent): matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,)) if matplotlib: return MatplotlibBackend(parent) else: return TextBackend(parent) plot_backends = { 'matplotlib': MatplotlibBackend, 'text': TextBackend, 'default': DefaultBackend } ############################################################################## # Finding the centers of line segments or mesh faces ############################################################################## def centers_of_segments(array): np = import_module('numpy') return np.average(np.vstack((array[:-1], array[1:])), 0) def centers_of_faces(array): np = import_module('numpy') return np.average(np.dstack((array[:-1, :-1], array[1:, :-1], array[:-1, 1: ], array[:-1, :-1], )), 2) def flat(x, y, z, eps=1e-3): """Checks whether three points are almost collinear""" np = import_module('numpy') # Workaround plotting piecewise (#8577): # workaround for `lambdify` in `.experimental_lambdify` fails # to return numerical values in some cases. Lower-level fix # in `lambdify` is possible. vector_a = (x - y).astype(np.float) vector_b = (z - y).astype(np.float) dot_product = np.dot(vector_a, vector_b) vector_a_norm = np.linalg.norm(vector_a) vector_b_norm = np.linalg.norm(vector_b) cos_theta = dot_product / (vector_a_norm * vector_b_norm) return abs(cos_theta + 1) < eps def _matplotlib_list(interval_list): """ Returns lists for matplotlib ``fill`` command from a list of bounding rectangular intervals """ xlist = [] ylist = [] if len(interval_list): for intervals in interval_list: intervalx = intervals[0] intervaly = intervals[1] xlist.extend([intervalx.start, intervalx.start, intervalx.end, intervalx.end, None]) ylist.extend([intervaly.start, intervaly.end, intervaly.end, intervaly.start, None]) else: #XXX Ugly hack. Matplotlib does not accept empty lists for ``fill`` xlist.extend([None, None, None, None]) ylist.extend([None, None, None, None]) return xlist, ylist ####New API for plotting module #### # TODO: Add color arrays for plots. # TODO: Add more plotting options for 3d plots. # TODO: Adaptive sampling for 3D plots. @doctest_depends_on(modules=('numpy', 'matplotlib',)) def plot(*args, **kwargs): """ Plots a function of a single variable and returns an instance of the ``Plot`` class (also, see the description of the ``show`` keyword argument below). The plotting uses an adaptive algorithm which samples recursively to accurately plot the plot. The adaptive algorithm uses a random point near the midpoint of two points that has to be further sampled. Hence the same plots can appear slightly different. Usage ===== Single Plot ``plot(expr, range, **kwargs)`` If the range is not specified, then a default range of (-10, 10) is used. Multiple plots with same range. ``plot(expr1, expr2, ..., range, **kwargs)`` If the range is not specified, then a default range of (-10, 10) is used. Multiple plots with different ranges. ``plot((expr1, range), (expr2, range), ..., **kwargs)`` Range has to be specified for every expression. Default range may change in the future if a more advanced default range detection algorithm is implemented. Arguments ========= ``expr`` : Expression representing the function of single variable ``range``: (x, 0, 5), A 3-tuple denoting the range of the free variable. Keyword Arguments ================= Arguments for ``plot`` function: ``show``: Boolean. The default value is set to ``True``. Set show to ``False`` and the function will not display the plot. The returned instance of the ``Plot`` class can then be used to save or display the plot by calling the ``save()`` and ``show()`` methods respectively. Arguments for ``LineOver1DRangeSeries`` class: ``adaptive``: Boolean. The default value is set to True. Set adaptive to False and specify ``nb_of_points`` if uniform sampling is required. ``depth``: int Recursion depth of the adaptive algorithm. A depth of value ``n`` samples a maximum of `2^{n}` points. ``nb_of_points``: int. Used when the ``adaptive`` is set to False. The function is uniformly sampled at ``nb_of_points`` number of points. Aesthetics options: ``line_color``: float. Specifies the color for the plot. See ``Plot`` to see how to set color for the plots. If there are multiple plots, then the same series series are applied to all the plots. If you want to set these options separately, you can index the ``Plot`` object returned and set it. Arguments for ``Plot`` class: ``title`` : str. Title of the plot. It is set to the latex representation of the expression, if the plot has only one expression. ``xlabel`` : str. Label for the x-axis. ``ylabel`` : str. Label for the y-axis. ``xscale``: {'linear', 'log'} Sets the scaling of the x-axis. ``yscale``: {'linear', 'log'} Sets the scaling if the y-axis. ``axis_center``: tuple of two floats denoting the coordinates of the center or {'center', 'auto'} ``xlim`` : tuple of two floats, denoting the x-axis limits. ``ylim`` : tuple of two floats, denoting the y-axis limits. Examples ======== >>> from sympy import symbols >>> from sympy.plotting import plot >>> x = symbols('x') Single Plot >>> plot(x**2, (x, -5, 5)) Plot object containing: [0]: cartesian line: x**2 for x over (-5.0, 5.0) Multiple plots with single range. >>> plot(x, x**2, x**3, (x, -5, 5)) Plot object containing: [0]: cartesian line: x for x over (-5.0, 5.0) [1]: cartesian line: x**2 for x over (-5.0, 5.0) [2]: cartesian line: x**3 for x over (-5.0, 5.0) Multiple plots with different ranges. >>> plot((x**2, (x, -6, 6)), (x, (x, -5, 5))) Plot object containing: [0]: cartesian line: x**2 for x over (-6.0, 6.0) [1]: cartesian line: x for x over (-5.0, 5.0) No adaptive sampling. >>> plot(x**2, adaptive=False, nb_of_points=400) Plot object containing: [0]: cartesian line: x**2 for x over (-10.0, 10.0) See Also ======== Plot, LineOver1DRangeSeries. """ args = list(map(sympify, args)) free = set() for a in args: if isinstance(a, Expr): free |= a.free_symbols if len(free) > 1: raise ValueError( 'The same variable should be used in all ' 'univariate expressions being plotted.') x = free.pop() if free else Symbol('x') kwargs.setdefault('xlabel', x.name) kwargs.setdefault('ylabel', 'f(%s)' % x.name) show = kwargs.pop('show', True) series = [] plot_expr = check_arguments(args, 1, 1) series = [LineOver1DRangeSeries(*arg, **kwargs) for arg in plot_expr] plots = Plot(*series, **kwargs) if show: plots.show() return plots @doctest_depends_on(modules=('numpy', 'matplotlib',)) def plot_parametric(*args, **kwargs): """ Plots a 2D parametric plot. The plotting uses an adaptive algorithm which samples recursively to accurately plot the plot. The adaptive algorithm uses a random point near the midpoint of two points that has to be further sampled. Hence the same plots can appear slightly different. Usage ===== Single plot. ``plot_parametric(expr_x, expr_y, range, **kwargs)`` If the range is not specified, then a default range of (-10, 10) is used. Multiple plots with same range. ``plot_parametric((expr1_x, expr1_y), (expr2_x, expr2_y), range, **kwargs)`` If the range is not specified, then a default range of (-10, 10) is used. Multiple plots with different ranges. ``plot_parametric((expr_x, expr_y, range), ..., **kwargs)`` Range has to be specified for every expression. Default range may change in the future if a more advanced default range detection algorithm is implemented. Arguments ========= ``expr_x`` : Expression representing the function along x. ``expr_y`` : Expression representing the function along y. ``range``: (u, 0, 5), A 3-tuple denoting the range of the parameter variable. Keyword Arguments ================= Arguments for ``Parametric2DLineSeries`` class: ``adaptive``: Boolean. The default value is set to True. Set adaptive to False and specify ``nb_of_points`` if uniform sampling is required. ``depth``: int Recursion depth of the adaptive algorithm. A depth of value ``n`` samples a maximum of `2^{n}` points. ``nb_of_points``: int. Used when the ``adaptive`` is set to False. The function is uniformly sampled at ``nb_of_points`` number of points. Aesthetics ---------- ``line_color``: function which returns a float. Specifies the color for the plot. See ``sympy.plotting.Plot`` for more details. If there are multiple plots, then the same Series arguments are applied to all the plots. If you want to set these options separately, you can index the returned ``Plot`` object and set it. Arguments for ``Plot`` class: ``xlabel`` : str. Label for the x-axis. ``ylabel`` : str. Label for the y-axis. ``xscale``: {'linear', 'log'} Sets the scaling of the x-axis. ``yscale``: {'linear', 'log'} Sets the scaling if the y-axis. ``axis_center``: tuple of two floats denoting the coordinates of the center or {'center', 'auto'} ``xlim`` : tuple of two floats, denoting the x-axis limits. ``ylim`` : tuple of two floats, denoting the y-axis limits. Examples ======== >>> from sympy import symbols, cos, sin >>> from sympy.plotting import plot_parametric >>> u = symbols('u') Single Parametric plot >>> plot_parametric(cos(u), sin(u), (u, -5, 5)) Plot object containing: [0]: parametric cartesian line: (cos(u), sin(u)) for u over (-5.0, 5.0) Multiple parametric plot with single range. >>> plot_parametric((cos(u), sin(u)), (u, cos(u))) Plot object containing: [0]: parametric cartesian line: (cos(u), sin(u)) for u over (-10.0, 10.0) [1]: parametric cartesian line: (u, cos(u)) for u over (-10.0, 10.0) Multiple parametric plots. >>> plot_parametric((cos(u), sin(u), (u, -5, 5)), ... (cos(u), u, (u, -5, 5))) Plot object containing: [0]: parametric cartesian line: (cos(u), sin(u)) for u over (-5.0, 5.0) [1]: parametric cartesian line: (cos(u), u) for u over (-5.0, 5.0) See Also ======== Plot, Parametric2DLineSeries """ args = list(map(sympify, args)) show = kwargs.pop('show', True) series = [] plot_expr = check_arguments(args, 2, 1) series = [Parametric2DLineSeries(*arg, **kwargs) for arg in plot_expr] plots = Plot(*series, **kwargs) if show: plots.show() return plots @doctest_depends_on(modules=('numpy', 'matplotlib',)) def plot3d_parametric_line(*args, **kwargs): """ Plots a 3D parametric line plot. Usage ===== Single plot: ``plot3d_parametric_line(expr_x, expr_y, expr_z, range, **kwargs)`` If the range is not specified, then a default range of (-10, 10) is used. Multiple plots. ``plot3d_parametric_line((expr_x, expr_y, expr_z, range), ..., **kwargs)`` Ranges have to be specified for every expression. Default range may change in the future if a more advanced default range detection algorithm is implemented. Arguments ========= ``expr_x`` : Expression representing the function along x. ``expr_y`` : Expression representing the function along y. ``expr_z`` : Expression representing the function along z. ``range``: ``(u, 0, 5)``, A 3-tuple denoting the range of the parameter variable. Keyword Arguments ================= Arguments for ``Parametric3DLineSeries`` class. ``nb_of_points``: The range is uniformly sampled at ``nb_of_points`` number of points. Aesthetics: ``line_color``: function which returns a float. Specifies the color for the plot. See ``sympy.plotting.Plot`` for more details. If there are multiple plots, then the same series arguments are applied to all the plots. If you want to set these options separately, you can index the returned ``Plot`` object and set it. Arguments for ``Plot`` class. ``title`` : str. Title of the plot. Examples ======== >>> from sympy import symbols, cos, sin >>> from sympy.plotting import plot3d_parametric_line >>> u = symbols('u') Single plot. >>> plot3d_parametric_line(cos(u), sin(u), u, (u, -5, 5)) Plot object containing: [0]: 3D parametric cartesian line: (cos(u), sin(u), u) for u over (-5.0, 5.0) Multiple plots. >>> plot3d_parametric_line((cos(u), sin(u), u, (u, -5, 5)), ... (sin(u), u**2, u, (u, -5, 5))) Plot object containing: [0]: 3D parametric cartesian line: (cos(u), sin(u), u) for u over (-5.0, 5.0) [1]: 3D parametric cartesian line: (sin(u), u**2, u) for u over (-5.0, 5.0) See Also ======== Plot, Parametric3DLineSeries """ args = list(map(sympify, args)) show = kwargs.pop('show', True) series = [] plot_expr = check_arguments(args, 3, 1) series = [Parametric3DLineSeries(*arg, **kwargs) for arg in plot_expr] plots = Plot(*series, **kwargs) if show: plots.show() return plots @doctest_depends_on(modules=('numpy', 'matplotlib',)) def plot3d(*args, **kwargs): """ Plots a 3D surface plot. Usage ===== Single plot ``plot3d(expr, range_x, range_y, **kwargs)`` If the ranges are not specified, then a default range of (-10, 10) is used. Multiple plot with the same range. ``plot3d(expr1, expr2, range_x, range_y, **kwargs)`` If the ranges are not specified, then a default range of (-10, 10) is used. Multiple plots with different ranges. ``plot3d((expr1, range_x, range_y), (expr2, range_x, range_y), ..., **kwargs)`` Ranges have to be specified for every expression. Default range may change in the future if a more advanced default range detection algorithm is implemented. Arguments ========= ``expr`` : Expression representing the function along x. ``range_x``: (x, 0, 5), A 3-tuple denoting the range of the x variable. ``range_y``: (y, 0, 5), A 3-tuple denoting the range of the y variable. Keyword Arguments ================= Arguments for ``SurfaceOver2DRangeSeries`` class: ``nb_of_points_x``: int. The x range is sampled uniformly at ``nb_of_points_x`` of points. ``nb_of_points_y``: int. The y range is sampled uniformly at ``nb_of_points_y`` of points. Aesthetics: ``surface_color``: Function which returns a float. Specifies the color for the surface of the plot. See ``sympy.plotting.Plot`` for more details. If there are multiple plots, then the same series arguments are applied to all the plots. If you want to set these options separately, you can index the returned ``Plot`` object and set it. Arguments for ``Plot`` class: ``title`` : str. Title of the plot. Examples ======== >>> from sympy import symbols >>> from sympy.plotting import plot3d >>> x, y = symbols('x y') Single plot >>> plot3d(x*y, (x, -5, 5), (y, -5, 5)) Plot object containing: [0]: cartesian surface: x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0) Multiple plots with same range >>> plot3d(x*y, -x*y, (x, -5, 5), (y, -5, 5)) Plot object containing: [0]: cartesian surface: x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0) [1]: cartesian surface: -x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0) Multiple plots with different ranges. >>> plot3d((x**2 + y**2, (x, -5, 5), (y, -5, 5)), ... (x*y, (x, -3, 3), (y, -3, 3))) Plot object containing: [0]: cartesian surface: x**2 + y**2 for x over (-5.0, 5.0) and y over (-5.0, 5.0) [1]: cartesian surface: x*y for x over (-3.0, 3.0) and y over (-3.0, 3.0) See Also ======== Plot, SurfaceOver2DRangeSeries """ args = list(map(sympify, args)) show = kwargs.pop('show', True) series = [] plot_expr = check_arguments(args, 1, 2) series = [SurfaceOver2DRangeSeries(*arg, **kwargs) for arg in plot_expr] plots = Plot(*series, **kwargs) if show: plots.show() return plots @doctest_depends_on(modules=('numpy', 'matplotlib',)) def plot3d_parametric_surface(*args, **kwargs): """ Plots a 3D parametric surface plot. Usage ===== Single plot. ``plot3d_parametric_surface(expr_x, expr_y, expr_z, range_u, range_v, **kwargs)`` If the ranges is not specified, then a default range of (-10, 10) is used. Multiple plots. ``plot3d_parametric_surface((expr_x, expr_y, expr_z, range_u, range_v), ..., **kwargs)`` Ranges have to be specified for every expression. Default range may change in the future if a more advanced default range detection algorithm is implemented. Arguments ========= ``expr_x``: Expression representing the function along ``x``. ``expr_y``: Expression representing the function along ``y``. ``expr_z``: Expression representing the function along ``z``. ``range_u``: ``(u, 0, 5)``, A 3-tuple denoting the range of the ``u`` variable. ``range_v``: ``(v, 0, 5)``, A 3-tuple denoting the range of the v variable. Keyword Arguments ================= Arguments for ``ParametricSurfaceSeries`` class: ``nb_of_points_u``: int. The ``u`` range is sampled uniformly at ``nb_of_points_v`` of points ``nb_of_points_y``: int. The ``v`` range is sampled uniformly at ``nb_of_points_y`` of points Aesthetics: ``surface_color``: Function which returns a float. Specifies the color for the surface of the plot. See ``sympy.plotting.Plot`` for more details. If there are multiple plots, then the same series arguments are applied for all the plots. If you want to set these options separately, you can index the returned ``Plot`` object and set it. Arguments for ``Plot`` class: ``title`` : str. Title of the plot. Examples ======== >>> from sympy import symbols, cos, sin >>> from sympy.plotting import plot3d_parametric_surface >>> u, v = symbols('u v') Single plot. >>> plot3d_parametric_surface(cos(u + v), sin(u - v), u - v, ... (u, -5, 5), (v, -5, 5)) Plot object containing: [0]: parametric cartesian surface: (cos(u + v), sin(u - v), u - v) for u over (-5.0, 5.0) and v over (-5.0, 5.0) See Also ======== Plot, ParametricSurfaceSeries """ args = list(map(sympify, args)) show = kwargs.pop('show', True) series = [] plot_expr = check_arguments(args, 3, 2) series = [ParametricSurfaceSeries(*arg, **kwargs) for arg in plot_expr] plots = Plot(*series, **kwargs) if show: plots.show() return plots def check_arguments(args, expr_len, nb_of_free_symbols): """ Checks the arguments and converts into tuples of the form (exprs, ranges) Examples ======== >>> from sympy import plot, cos, sin, symbols >>> from sympy.plotting.plot import check_arguments >>> x = symbols('x') >>> check_arguments([cos(x), sin(x)], 2, 1) [(cos(x), sin(x), (x, -10, 10))] >>> check_arguments([x, x**2], 1, 1) [(x, (x, -10, 10)), (x**2, (x, -10, 10))] """ if expr_len > 1 and isinstance(args[0], Expr): # Multiple expressions same range. # The arguments are tuples when the expression length is # greater than 1. if len(args) < expr_len: raise ValueError("len(args) should not be less than expr_len") for i in range(len(args)): if isinstance(args[i], Tuple): break else: i = len(args) + 1 exprs = Tuple(*args[:i]) free_symbols = list(set().union(*[e.free_symbols for e in exprs])) if len(args) == expr_len + nb_of_free_symbols: #Ranges given plots = [exprs + Tuple(*args[expr_len:])] else: default_range = Tuple(-10, 10) ranges = [] for symbol in free_symbols: ranges.append(Tuple(symbol) + default_range) for i in range(len(free_symbols) - nb_of_free_symbols): ranges.append(Tuple(Dummy()) + default_range) plots = [exprs + Tuple(*ranges)] return plots if isinstance(args[0], Expr) or (isinstance(args[0], Tuple) and len(args[0]) == expr_len and expr_len != 3): # Cannot handle expressions with number of expression = 3. It is # not possible to differentiate between expressions and ranges. #Series of plots with same range for i in range(len(args)): if isinstance(args[i], Tuple) and len(args[i]) != expr_len: break if not isinstance(args[i], Tuple): args[i] = Tuple(args[i]) else: i = len(args) + 1 exprs = args[:i] assert all(isinstance(e, Expr) for expr in exprs for e in expr) free_symbols = list(set().union(*[e.free_symbols for expr in exprs for e in expr])) if len(free_symbols) > nb_of_free_symbols: raise ValueError("The number of free_symbols in the expression " "is greater than %d" % nb_of_free_symbols) if len(args) == i + nb_of_free_symbols and isinstance(args[i], Tuple): ranges = Tuple(*[range_expr for range_expr in args[ i:i + nb_of_free_symbols]]) plots = [expr + ranges for expr in exprs] return plots else: #Use default ranges. default_range = Tuple(-10, 10) ranges = [] for symbol in free_symbols: ranges.append(Tuple(symbol) + default_range) for i in range(len(free_symbols) - nb_of_free_symbols): ranges.append(Tuple(Dummy()) + default_range) ranges = Tuple(*ranges) plots = [expr + ranges for expr in exprs] return plots elif isinstance(args[0], Tuple) and len(args[0]) == expr_len + nb_of_free_symbols: #Multiple plots with different ranges. for arg in args: for i in range(expr_len): if not isinstance(arg[i], Expr): raise ValueError("Expected an expression, given %s" % str(arg[i])) for i in range(nb_of_free_symbols): if not len(arg[i + expr_len]) == 3: raise ValueError("The ranges should be a tuple of " "length 3, got %s" % str(arg[i + expr_len])) return args
bsd-3-clause
jeremyclover/airflow
airflow/hooks/base_hook.py
20
1812
from builtins import object import logging import os import random from airflow import settings from airflow.models import Connection from airflow.utils import AirflowException CONN_ENV_PREFIX = 'AIRFLOW_CONN_' class BaseHook(object): """ Abstract base class for hooks, hooks are meant as an interface to interact with external systems. MySqlHook, HiveHook, PigHook return object that can handle the connection and interaction to specific instances of these systems, and expose consistent methods to interact with them. """ def __init__(self, source): pass @classmethod def get_connections(cls, conn_id): session = settings.Session() db = ( session.query(Connection) .filter(Connection.conn_id == conn_id) .all() ) if not db: raise AirflowException( "The conn_id `{0}` isn't defined".format(conn_id)) session.expunge_all() session.close() return db @classmethod def get_connection(cls, conn_id): environment_uri = os.environ.get(CONN_ENV_PREFIX + conn_id.upper()) conn = None if environment_uri: conn = Connection(uri=environment_uri) else: conn = random.choice(cls.get_connections(conn_id)) if conn.host: logging.info("Using connection to: " + conn.host) return conn @classmethod def get_hook(cls, conn_id): connection = cls.get_connection(conn_id) return connection.get_hook() def get_conn(self): raise NotImplemented() def get_records(self, sql): raise NotImplemented() def get_pandas_df(self, sql): raise NotImplemented() def run(self, sql): raise NotImplemented()
apache-2.0
spbguru/repo1
external/linux32/lib/python2.6/site-packages/matplotlib/backends/backend_wxagg.py
70
9051
from __future__ import division """ backend_wxagg.py A wxPython backend for Agg. This uses the GUI widgets written by Jeremy O'Donoghue (jeremy@o-donoghue.com) and the Agg backend by John Hunter (jdhunter@ace.bsd.uchicago.edu) Copyright (C) 2003-5 Jeremy O'Donoghue, John Hunter, Illinois Institute of Technology License: This work is licensed under the matplotlib license( PSF compatible). A copy should be included with this source code. """ import wx import matplotlib from matplotlib.figure import Figure from backend_agg import FigureCanvasAgg import backend_wx from backend_wx import FigureManager, FigureManagerWx, FigureCanvasWx, \ FigureFrameWx, DEBUG_MSG, NavigationToolbar2Wx, error_msg_wx, \ draw_if_interactive, show, Toolbar, backend_version class FigureFrameWxAgg(FigureFrameWx): def get_canvas(self, fig): return FigureCanvasWxAgg(self, -1, fig) def _get_toolbar(self, statbar): if matplotlib.rcParams['toolbar']=='classic': toolbar = NavigationToolbarWx(self.canvas, True) elif matplotlib.rcParams['toolbar']=='toolbar2': toolbar = NavigationToolbar2WxAgg(self.canvas) toolbar.set_status_bar(statbar) else: toolbar = None return toolbar class FigureCanvasWxAgg(FigureCanvasAgg, FigureCanvasWx): """ The FigureCanvas contains the figure and does event handling. In the wxPython backend, it is derived from wxPanel, and (usually) lives inside a frame instantiated by a FigureManagerWx. The parent window probably implements a wxSizer to control the displayed control size - but we give a hint as to our preferred minimum size. """ def draw(self, drawDC=None): """ Render the figure using agg. """ DEBUG_MSG("draw()", 1, self) FigureCanvasAgg.draw(self) self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) self._isDrawn = True self.gui_repaint(drawDC=drawDC) def blit(self, bbox=None): """ Transfer the region of the agg buffer defined by bbox to the display. If bbox is None, the entire buffer is transferred. """ if bbox is None: self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) self.gui_repaint() return l, b, w, h = bbox.bounds r = l + w t = b + h x = int(l) y = int(self.bitmap.GetHeight() - t) srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None) srcDC = wx.MemoryDC() srcDC.SelectObject(srcBmp) destDC = wx.MemoryDC() destDC.SelectObject(self.bitmap) destDC.BeginDrawing() destDC.Blit(x, y, int(w), int(h), srcDC, x, y) destDC.EndDrawing() destDC.SelectObject(wx.NullBitmap) srcDC.SelectObject(wx.NullBitmap) self.gui_repaint() filetypes = FigureCanvasAgg.filetypes def print_figure(self, filename, *args, **kwargs): # Use pure Agg renderer to draw FigureCanvasAgg.print_figure(self, filename, *args, **kwargs) # Restore the current view; this is needed because the # artist contains methods rely on particular attributes # of the rendered figure for determining things like # bounding boxes. if self._isDrawn: self.draw() class NavigationToolbar2WxAgg(NavigationToolbar2Wx): def get_canvas(self, frame, fig): return FigureCanvasWxAgg(frame, -1, fig) def new_figure_manager(num, *args, **kwargs): """ Create a new figure manager instance """ # in order to expose the Figure constructor to the pylab # interface we need to create the figure here DEBUG_MSG("new_figure_manager()", 3, None) backend_wx._create_wx_app() FigureClass = kwargs.pop('FigureClass', Figure) fig = FigureClass(*args, **kwargs) frame = FigureFrameWxAgg(num, fig) figmgr = frame.get_figure_manager() if matplotlib.is_interactive(): figmgr.frame.Show() return figmgr # # agg/wxPython image conversion functions (wxPython <= 2.6) # def _py_convert_agg_to_wx_image(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Image. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ image = wx.EmptyImage(int(agg.width), int(agg.height)) image.SetData(agg.tostring_rgb()) if bbox is None: # agg => rgb -> image return image else: # agg => rgb -> image => bitmap => clipped bitmap => image return wx.ImageFromBitmap(_clipped_image_as_bitmap(image, bbox)) def _py_convert_agg_to_wx_bitmap(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ if bbox is None: # agg => rgb -> image => bitmap return wx.BitmapFromImage(_py_convert_agg_to_wx_image(agg, None)) else: # agg => rgb -> image => bitmap => clipped bitmap return _clipped_image_as_bitmap( _py_convert_agg_to_wx_image(agg, None), bbox) def _clipped_image_as_bitmap(image, bbox): """ Convert the region of a wx.Image bounded by bbox to a wx.Bitmap. """ l, b, width, height = bbox.get_bounds() r = l + width t = b + height srcBmp = wx.BitmapFromImage(image) srcDC = wx.MemoryDC() srcDC.SelectObject(srcBmp) destBmp = wx.EmptyBitmap(int(width), int(height)) destDC = wx.MemoryDC() destDC.SelectObject(destBmp) destDC.BeginDrawing() x = int(l) y = int(image.GetHeight() - t) destDC.Blit(0, 0, int(width), int(height), srcDC, x, y) destDC.EndDrawing() srcDC.SelectObject(wx.NullBitmap) destDC.SelectObject(wx.NullBitmap) return destBmp # # agg/wxPython image conversion functions (wxPython >= 2.8) # def _py_WX28_convert_agg_to_wx_image(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Image. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ if bbox is None: # agg => rgb -> image image = wx.EmptyImage(int(agg.width), int(agg.height)) image.SetData(agg.tostring_rgb()) return image else: # agg => rgba buffer -> bitmap => clipped bitmap => image return wx.ImageFromBitmap(_WX28_clipped_agg_as_bitmap(agg, bbox)) def _py_WX28_convert_agg_to_wx_bitmap(agg, bbox): """ Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If bbox is None, the entire buffer is converted. Note: agg must be a backend_agg.RendererAgg instance. """ if bbox is None: # agg => rgba buffer -> bitmap return wx.BitmapFromBufferRGBA(int(agg.width), int(agg.height), agg.buffer_rgba(0, 0)) else: # agg => rgba buffer -> bitmap => clipped bitmap return _WX28_clipped_agg_as_bitmap(agg, bbox) def _WX28_clipped_agg_as_bitmap(agg, bbox): """ Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap. Note: agg must be a backend_agg.RendererAgg instance. """ l, b, width, height = bbox.get_bounds() r = l + width t = b + height srcBmp = wx.BitmapFromBufferRGBA(int(agg.width), int(agg.height), agg.buffer_rgba(0, 0)) srcDC = wx.MemoryDC() srcDC.SelectObject(srcBmp) destBmp = wx.EmptyBitmap(int(width), int(height)) destDC = wx.MemoryDC() destDC.SelectObject(destBmp) destDC.BeginDrawing() x = int(l) y = int(int(agg.height) - t) destDC.Blit(0, 0, int(width), int(height), srcDC, x, y) destDC.EndDrawing() srcDC.SelectObject(wx.NullBitmap) destDC.SelectObject(wx.NullBitmap) return destBmp def _use_accelerator(state): """ Enable or disable the WXAgg accelerator, if it is present and is also compatible with whatever version of wxPython is in use. """ global _convert_agg_to_wx_image global _convert_agg_to_wx_bitmap if getattr(wx, '__version__', '0.0')[0:3] < '2.8': # wxPython < 2.8, so use the C++ accelerator or the Python routines if state and _wxagg is not None: _convert_agg_to_wx_image = _wxagg.convert_agg_to_wx_image _convert_agg_to_wx_bitmap = _wxagg.convert_agg_to_wx_bitmap else: _convert_agg_to_wx_image = _py_convert_agg_to_wx_image _convert_agg_to_wx_bitmap = _py_convert_agg_to_wx_bitmap else: # wxPython >= 2.8, so use the accelerated Python routines _convert_agg_to_wx_image = _py_WX28_convert_agg_to_wx_image _convert_agg_to_wx_bitmap = _py_WX28_convert_agg_to_wx_bitmap # try to load the WXAgg accelerator try: import _wxagg except ImportError: _wxagg = None # if it's present, use it _use_accelerator(True)
gpl-3.0
elijah513/scikit-learn
examples/model_selection/plot_validation_curve.py
229
1823
""" ========================== Plotting Validation Curves ========================== In this plot you can see the training scores and validation scores of an SVM for different values of the kernel parameter gamma. For very low values of gamma, you can see that both the training score and the validation score are low. This is called underfitting. Medium values of gamma will result in high values for both scores, i.e. the classifier is performing fairly well. If gamma is too high, the classifier will overfit, which means that the training score is good but the validation score is poor. """ print(__doc__) import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_digits from sklearn.svm import SVC from sklearn.learning_curve import validation_curve digits = load_digits() X, y = digits.data, digits.target param_range = np.logspace(-6, -1, 5) train_scores, test_scores = validation_curve( SVC(), X, y, param_name="gamma", param_range=param_range, cv=10, scoring="accuracy", n_jobs=1) train_scores_mean = np.mean(train_scores, axis=1) train_scores_std = np.std(train_scores, axis=1) test_scores_mean = np.mean(test_scores, axis=1) test_scores_std = np.std(test_scores, axis=1) plt.title("Validation Curve with SVM") plt.xlabel("$\gamma$") plt.ylabel("Score") plt.ylim(0.0, 1.1) plt.semilogx(param_range, train_scores_mean, label="Training score", color="r") plt.fill_between(param_range, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.2, color="r") plt.semilogx(param_range, test_scores_mean, label="Cross-validation score", color="g") plt.fill_between(param_range, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.2, color="g") plt.legend(loc="best") plt.show()
bsd-3-clause
sonusz/PhasorToolBox
examples/freq_meter.py
1
1820
#!/usr/bin/env python3 """ This is an real-time frequency meter of two PMUs. This code connects to two PMUs, plot the frequency of the past 300 time-stamps and update the plot in real-time. """ from phasortoolbox import PDC,Client import matplotlib.pyplot as plt import numpy as np import gc import logging logging.basicConfig(level=logging.DEBUG) class FreqMeter(object): def __init__(self): x = np.linspace(-10.0, 0.0, num=300, endpoint=False) y = [60.0]*300 plt.ion() self.fig = plt.figure() self.ax1 = self.fig.add_subplot(211) self.line1, = self.ax1.plot(x, y) plt.title('PMU1 Frequency Plot') plt.xlabel('Time (s)') plt.ylabel('Freq (Hz)') self.ax2 = self.fig.add_subplot(212) self.line2, = self.ax2.plot(x, y) plt.title('PMU2 Frequency Plot') plt.xlabel('Time (s)') plt.ylabel('Freq (Hz)') plt.tight_layout() def update_plot(self, synchrophasors): y_data = [[],[]] for synchrophasor in synchrophasors: for i, msg in enumerate(synchrophasor): y_data[i].append(msg.data.pmu_data[0].freq) self.line1.set_ydata(y_data[0]) self.line2.set_ydata(y_data[1]) self.ax1.set_ylim(min(y_data[0]),max(y_data[0])) self.ax2.set_ylim(min(y_data[1]),max(y_data[1])) self.fig.canvas.draw() self.fig.canvas.flush_events() del(synchrophasors) gc.collect() if __name__ == '__main__': pmu_client1 = Client(remote_ip='10.0.0.1', remote_port=4722, idcode=1, mode='TCP') pmu_client2 = Client(remote_ip='10.0.0.2', remote_port=4722, idcode=2, mode='TCP') fm = FreqMeter() pdc = PDC(clients=[pmu_client1,pmu_client2],history=300) pdc.callback = fm.update_plot pdc.run()
mit
iulian787/spack
var/spack/repos/builtin/packages/py-sncosmo/package.py
5
1133
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other # Spack Project Developers. See the top-level COPYRIGHT file for details. # # SPDX-License-Identifier: (Apache-2.0 OR MIT) from spack import * class PySncosmo(PythonPackage): """SNCosmo is a Python library for high-level supernova cosmology analysis.""" homepage = "http://sncosmo.readthedocs.io/" url = "https://pypi.io/packages/source/s/sncosmo/sncosmo-1.2.0.tar.gz" version('1.2.0', sha256='f3969eec5b25f60c70418dbd64765a2b4735bb53c210c61d0aab68916daea588') # Required dependencies # py-sncosmo binaries are duplicates of those from py-astropy extends('python', ignore=r'bin/.*') depends_on('py-setuptools', type='build') depends_on('py-numpy', type=('build', 'run')) depends_on('py-scipy', type=('build', 'run')) depends_on('py-astropy', type=('build', 'run')) # Recommended dependencies depends_on('py-matplotlib', type=('build', 'run')) depends_on('py-iminuit', type=('build', 'run')) depends_on('py-emcee', type=('build', 'run')) depends_on('py-nestle', type=('build', 'run'))
lgpl-2.1
mediaProduct2017/learn_NeuralNet
neural_network_design.py
1
1568
""" In order to decide how many hidden nodes the hidden layer should have, split up the data set into training and testing data and create networks with various hidden node counts (5, 10, 15, ... 45), testing the performance for each. The best-performing node count is used in the actual system. If multiple counts perform similarly, choose the smallest count for a smaller network with fewer computations. """ import numpy as np from ocr import OCRNeuralNetwork from sklearn.cross_validation import train_test_split def test(data_matrix, data_labels, test_indices, nn): avg_sum = 0 for j in xrange(100): correct_guess_count = 0 for i in test_indices: test = data_matrix[i] prediction = nn.predict(test) if data_labels[i] == prediction: correct_guess_count += 1 avg_sum += (correct_guess_count / float(len(test_indices))) return avg_sum / 100 # Load data samples and labels into matrix data_matrix = np.loadtxt(open('data.csv', 'rb'), delimiter = ',').tolist() data_labels = np.loadtxt(open('dataLabels.csv', 'rb')).tolist() # Create training and testing sets. train_indices, test_indices = train_test_split(list(range(5000))) print "PERFORMANCE" print "-----------" # Try various number of hidden nodes and see what performs best for i in xrange(5, 50, 5): nn = OCRNeuralNetwork(i, data_matrix, data_labels, train_indices, False) performance = str(test(data_matrix, data_labels, test_indices, nn)) print "{i} Hidden Nodes: {val}".format(i=i, val=performance)
mit
chvogl/tardis
tardis/io/config_reader.py
1
40145
# Module to read the rather complex config data import logging import os import pprint from astropy import constants, units as u import numpy as np import pandas as pd import yaml import tardis from tardis.io.model_reader import read_density_file, \ calculate_density_after_time, read_abundances_file from tardis.io.config_validator import ConfigurationValidator from tardis import atomic from tardis.util import species_string_to_tuple, parse_quantity, \ element_symbol2atomic_number import copy pp = pprint.PrettyPrinter(indent=4) logger = logging.getLogger(__name__) data_dir = os.path.join(tardis.__path__[0], 'data') default_config_definition_file = os.path.join(data_dir, 'tardis_config_definition.yml') #File parsers for different file formats: density_structure_fileparser = {} inv_ni56_efolding_time = 1 / (8.8 * u.day) inv_co56_efolding_time = 1 / (113.7 * u.day) inv_cr48_efolding_time = 1 / (1.29602 * u.day) inv_v48_efolding_time = 1 / (23.0442 * u.day) inv_fe52_efolding_time = 1 / (0.497429 * u.day) inv_mn52_efolding_time = 1 / (0.0211395 * u.day) class ConfigurationError(ValueError): pass def parse_quantity_linspace(quantity_linspace_dictionary, add_one=True): """ parse a dictionary of the following kind {'start': 5000 km/s, 'stop': 10000 km/s, 'num': 1000} Parameters ---------- quantity_linspace_dictionary: ~dict add_one: boolean, default: True Returns ------- ~np.array """ start = parse_quantity(quantity_linspace_dictionary['start']) stop = parse_quantity(quantity_linspace_dictionary['stop']) try: stop = stop.to(start.unit) except u.UnitsError: raise ConfigurationError('"start" and "stop" keyword must be compatible quantities') num = quantity_linspace_dictionary['num'] if add_one: num += 1 return np.linspace(start.value, stop.value, num=num) * start.unit def parse_spectral_bin(spectral_bin_boundary_1, spectral_bin_boundary_2): spectral_bin_boundary_1 = parse_quantity(spectral_bin_boundary_1).to('Angstrom', u.spectral()) spectral_bin_boundary_2 = parse_quantity(spectral_bin_boundary_2).to('Angstrom', u.spectral()) spectrum_start_wavelength = min(spectral_bin_boundary_1, spectral_bin_boundary_2) spectrum_end_wavelength = max(spectral_bin_boundary_1, spectral_bin_boundary_2) return spectrum_start_wavelength, spectrum_end_wavelength def calculate_exponential_density(velocities, v_0, rho0): """ This function computes the exponential density profile. :math:`\\rho = \\rho_0 \\times \\exp \\left( -\\frac{v}{v_0} \\right)` Parameters ---------- velocities : ~astropy.Quantity Array like velocity profile velocity_0 : ~astropy.Quantity reference velocity rho0 : ~astropy.Quantity reference density Returns ------- densities : ~astropy.Quantity """ densities = rho0 * np.exp(-(velocities / v_0)) return densities def calculate_power_law_density(velocities, velocity_0, rho_0, exponent): """ This function computes a descret exponential density profile. :math:`\\rho = \\rho_0 \\times \\left( \\frac{v}{v_0} \\right)^n` Parameters ---------- velocities : ~astropy.Quantity Array like velocity profile velocity_0 : ~astropy.Quantity reference velocity rho0 : ~astropy.Quantity reference density exponent : ~float exponent used in the powerlaw Returns ------- densities : ~astropy.Quantity """ densities = rho_0 * np.power((velocities / velocity_0), exponent) return densities def parse_model_file_section(model_setup_file_dict, time_explosion): def parse_artis_model_setup_files(model_file_section_dict, time_explosion): ###### Reading the structure part of the ARTIS file pair structure_fname = model_file_section_dict['structure_fname'] for i, line in enumerate(file(structure_fname)): if i == 0: no_of_shells = np.int64(line.strip()) elif i == 1: time_of_model = u.Quantity(float(line.strip()), 'day').to('s') elif i == 2: break artis_model_columns = ['velocities', 'mean_densities_0', 'ni56_fraction', 'co56_fraction', 'fe52_fraction', 'cr48_fraction'] artis_model = np.recfromtxt(structure_fname, skip_header=2, usecols=(1, 2, 4, 5, 6, 7), unpack=True, dtype=[(item, np.float64) for item in artis_model_columns]) #converting densities from log(g/cm^3) to g/cm^3 and stretching it to the current ti velocities = u.Quantity(np.append([0], artis_model['velocities']), 'km/s').to('cm/s') mean_densities_0 = u.Quantity(10 ** artis_model['mean_densities_0'], 'g/cm^3') mean_densities = calculate_density_after_time(mean_densities_0, time_of_model, time_explosion) #Verifying information if len(mean_densities) == no_of_shells: logger.debug('Verified ARTIS model structure file %s (no_of_shells=length of dataset)', structure_fname) else: raise ConfigurationError( 'Error in ARTIS file %s - Number of shells not the same as dataset length' % structure_fname) v_inner = velocities[:-1] v_outer = velocities[1:] volumes = (4 * np.pi / 3) * (time_of_model ** 3) * ( v_outer ** 3 - v_inner ** 3) masses = (volumes * mean_densities_0 / constants.M_sun).to(1) logger.info('Read ARTIS configuration file %s - found %d zones with total mass %g Msun', structure_fname, no_of_shells, sum(masses.value)) if 'v_lowest' in model_file_section_dict: v_lowest = parse_quantity(model_file_section_dict['v_lowest']).to('cm/s').value min_shell = v_inner.value.searchsorted(v_lowest) else: min_shell = 1 if 'v_highest' in model_file_section_dict: v_highest = parse_quantity(model_file_section_dict['v_highest']).to('cm/s').value max_shell = v_outer.value.searchsorted(v_highest) else: max_shell = no_of_shells artis_model = artis_model[min_shell:max_shell] v_inner = v_inner[min_shell:max_shell] v_outer = v_outer[min_shell:max_shell] mean_densities = mean_densities[min_shell:max_shell] ###### Reading the abundance part of the ARTIS file pair abundances_fname = model_file_section_dict['abundances_fname'] abundances = pd.DataFrame(np.loadtxt(abundances_fname)[min_shell:max_shell, 1:].transpose(), index=np.arange(1, 31)) ni_stable = abundances.ix[28] - artis_model['ni56_fraction'] co_stable = abundances.ix[27] - artis_model['co56_fraction'] fe_stable = abundances.ix[26] - artis_model['fe52_fraction'] mn_stable = abundances.ix[25] - 0.0 cr_stable = abundances.ix[24] - artis_model['cr48_fraction'] v_stable = abundances.ix[23] - 0.0 ti_stable = abundances.ix[22] - 0.0 abundances.ix[28] = ni_stable abundances.ix[28] += artis_model['ni56_fraction'] * np.exp( -(time_explosion * inv_ni56_efolding_time).to(1).value) abundances.ix[27] = co_stable abundances.ix[27] += artis_model['co56_fraction'] * np.exp( -(time_explosion * inv_co56_efolding_time).to(1).value) abundances.ix[27] += (inv_ni56_efolding_time * artis_model['ni56_fraction'] / (inv_ni56_efolding_time - inv_co56_efolding_time)) * \ (np.exp(-(inv_co56_efolding_time * time_explosion).to(1).value) - np.exp( -(inv_ni56_efolding_time * time_explosion).to(1).value)) abundances.ix[26] = fe_stable abundances.ix[26] += artis_model['fe52_fraction'] * np.exp( -(time_explosion * inv_fe52_efolding_time).to(1).value) abundances.ix[26] += ((artis_model['co56_fraction'] * inv_ni56_efolding_time - artis_model['co56_fraction'] * inv_co56_efolding_time + artis_model['ni56_fraction'] * inv_ni56_efolding_time - artis_model['ni56_fraction'] * inv_co56_efolding_time - artis_model['co56_fraction'] * inv_ni56_efolding_time * np.exp( -(inv_co56_efolding_time * time_explosion).to(1).value) + artis_model['co56_fraction'] * inv_co56_efolding_time * np.exp( -(inv_co56_efolding_time * time_explosion).to(1).value) - artis_model['ni56_fraction'] * inv_ni56_efolding_time * np.exp( -(inv_co56_efolding_time * time_explosion).to(1).value) + artis_model['ni56_fraction'] * inv_co56_efolding_time * np.exp( -(inv_ni56_efolding_time * time_explosion).to(1).value)) / (inv_ni56_efolding_time - inv_co56_efolding_time)) abundances.ix[25] = mn_stable abundances.ix[25] += (inv_fe52_efolding_time * artis_model['fe52_fraction'] / (inv_fe52_efolding_time - inv_mn52_efolding_time)) * \ (np.exp(-(inv_mn52_efolding_time * time_explosion).to(1).value) - np.exp( -(inv_fe52_efolding_time * time_explosion).to(1).value)) abundances.ix[24] = cr_stable abundances.ix[24] += artis_model['cr48_fraction'] * np.exp( -(time_explosion * inv_cr48_efolding_time).to(1).value) abundances.ix[24] += ((artis_model['fe52_fraction'] * inv_fe52_efolding_time - artis_model['fe52_fraction'] * inv_mn52_efolding_time - artis_model['fe52_fraction'] * inv_fe52_efolding_time * np.exp( -(inv_mn52_efolding_time * time_explosion).to(1).value) + artis_model['fe52_fraction'] * inv_mn52_efolding_time * np.exp( -(inv_fe52_efolding_time * time_explosion).to(1).value)) / (inv_fe52_efolding_time - inv_mn52_efolding_time)) abundances.ix[23] = v_stable abundances.ix[23] += (inv_cr48_efolding_time * artis_model['cr48_fraction'] / (inv_cr48_efolding_time - inv_v48_efolding_time)) * \ (np.exp(-(inv_v48_efolding_time * time_explosion).to(1).value) - np.exp( -(inv_cr48_efolding_time * time_explosion).to(1).value)) abundances.ix[22] = ti_stable abundances.ix[22] += ((artis_model['cr48_fraction'] * inv_cr48_efolding_time - artis_model['cr48_fraction'] * inv_v48_efolding_time - artis_model['cr48_fraction'] * inv_cr48_efolding_time * np.exp( -(inv_v48_efolding_time * time_explosion).to(1).value) + artis_model['cr48_fraction'] * inv_v48_efolding_time * np.exp( -(inv_cr48_efolding_time * time_explosion).to(1).value)) / (inv_cr48_efolding_time - inv_v48_efolding_time)) if 'split_shells' in model_file_section_dict: split_shells = int(model_file_section_dict['split_shells']) else: split_shells = 1 if split_shells > 1: logger.info('Increasing the number of shells by a factor of %s' % split_shells) no_of_shells = len(v_inner) velocities = np.linspace(v_inner[0], v_outer[-1], no_of_shells * split_shells + 1) v_inner = velocities[:-1] v_outer = velocities[1:] old_mean_densities = mean_densities mean_densities = np.empty(no_of_shells * split_shells) * old_mean_densities.unit new_abundance_data = np.empty((abundances.values.shape[0], no_of_shells * split_shells)) for i in xrange(split_shells): mean_densities[i::split_shells] = old_mean_densities new_abundance_data[:, i::split_shells] = abundances.values abundances = pd.DataFrame(new_abundance_data, index=abundances.index) #def parser_simple_ascii_model return v_inner, v_outer, mean_densities, abundances model_file_section_parser = {} model_file_section_parser['artis'] = parse_artis_model_setup_files try: parser = model_file_section_parser[model_setup_file_dict['type']] except KeyError: raise ConfigurationError('In abundance file section only types %s are allowed (supplied %s) ' % (model_file_section_parser.keys(), model_file_section_parser['type'])) return parser(model_setup_file_dict, time_explosion) def parse_density_file_section(density_file_dict, time_explosion): density_file_parser = {} def parse_artis_density(density_file_dict, time_explosion): density_file = density_file_dict['name'] for i, line in enumerate(file(density_file)): if i == 0: no_of_shells = np.int64(line.strip()) elif i == 1: time_of_model = u.Quantity(float(line.strip()), 'day').to('s') elif i == 2: break velocities, mean_densities_0 = np.recfromtxt(density_file, skip_header=2, usecols=(1, 2), unpack=True) #converting densities from log(g/cm^3) to g/cm^3 and stretching it to the current ti velocities = u.Quantity(np.append([0], velocities), 'km/s').to('cm/s') mean_densities_0 = u.Quantity(10 ** mean_densities_0, 'g/cm^3') mean_densities = calculate_density_after_time(mean_densities_0, time_of_model, time_explosion) #Verifying information if len(mean_densities) == no_of_shells: logger.debug('Verified ARTIS file %s (no_of_shells=length of dataset)', density_file) else: raise ConfigurationError( 'Error in ARTIS file %s - Number of shells not the same as dataset length' % density_file) min_shell = 1 max_shell = no_of_shells v_inner = velocities[:-1] v_outer = velocities[1:] volumes = (4 * np.pi / 3) * (time_of_model ** 3) * ( v_outer ** 3 - v_inner ** 3) masses = (volumes * mean_densities_0 / constants.M_sun).to(1) logger.info('Read ARTIS configuration file %s - found %d zones with total mass %g Msun', density_file, no_of_shells, sum(masses.value)) if 'v_lowest' in density_file_dict: v_lowest = parse_quantity(density_file_dict['v_lowest']).to('cm/s').value min_shell = v_inner.value.searchsorted(v_lowest) else: min_shell = 1 if 'v_highest' in density_file_dict: v_highest = parse_quantity(density_file_dict['v_highest']).to('cm/s').value max_shell = v_outer.value.searchsorted(v_highest) else: max_shell = no_of_shells v_inner = v_inner[min_shell:max_shell] v_outer = v_outer[min_shell:max_shell] mean_densities = mean_densities[min_shell:max_shell] return v_inner, v_outer, mean_densities, min_shell, max_shell density_file_parser['artis'] = parse_artis_density try: parser = density_file_parser[density_file_dict['type']] except KeyError: raise ConfigurationError('In abundance file section only types %s are allowed (supplied %s) ' % (density_file_parser.keys(), density_file_dict['type'])) return parser(density_file_dict, time_explosion) def parse_density_section(density_dict, v_inner, v_outer, time_explosion): density_parser = {} #Parse density uniform def parse_uniform(density_dict, v_inner, v_outer, time_explosion): no_of_shells = len(v_inner) return density_dict['value'].to('g cm^-3') * np.ones(no_of_shells) density_parser['uniform'] = parse_uniform #Parse density branch85 w7 def parse_branch85(density_dict, v_inner, v_outer, time_explosion): velocities = 0.5 * (v_inner + v_outer) densities = calculate_power_law_density(velocities, density_dict['w7_v_0'], density_dict['w7_rho_0'], -7) densities = calculate_density_after_time(densities, density_dict['w7_time_0'], time_explosion) return densities density_parser['branch85_w7'] = parse_branch85 def parse_power_law(density_dict, v_inner, v_outer, time_explosion): time_0 = density_dict.pop('time_0') rho_0 = density_dict.pop('rho_0') v_0 = density_dict.pop('v_0') exponent = density_dict.pop('exponent') velocities = 0.5 * (v_inner + v_outer) densities = calculate_power_law_density(velocities, v_0, rho_0, exponent) densities = calculate_density_after_time(densities, time_0, time_explosion) return densities density_parser['power_law'] = parse_power_law def parse_exponential(density_dict, v_inner, v_outer, time_explosion): time_0 = density_dict.pop('time_0') rho_0 = density_dict.pop('rho_0') v_0 = density_dict.pop('v_0') velocities = 0.5 * (v_inner + v_outer) densities = calculate_exponential_density(velocities, v_0, rho_0) densities = calculate_density_after_time(densities, time_0, time_explosion) return densities density_parser['exponential'] = parse_exponential try: parser = density_parser[density_dict['type']] except KeyError: raise ConfigurationError('In density section only types %s are allowed (supplied %s) ' % (density_parser.keys(), density_dict['type'])) return parser(density_dict, v_inner, v_outer, time_explosion) def parse_abundance_file_section(abundance_file_dict, abundances, min_shell, max_shell): abundance_file_parser = {} def parse_artis(abundance_file_dict, abundances, min_shell, max_shell): #### ---- debug ---- time_of_model = 0.0 #### fname = abundance_file_dict['name'] max_atom = 30 logger.info("Parsing ARTIS Abundance section from shell %d to %d", min_shell, max_shell) abundances.values[:max_atom, :] = np.loadtxt(fname)[min_shell:max_shell, 1:].transpose() return abundances abundance_file_parser['artis'] = parse_artis try: parser = abundance_file_parser[abundance_file_dict['type']] except KeyError: raise ConfigurationError('In abundance file section only types %s are allowed (supplied %s) ' % (abundance_file_parser.keys(), abundance_file_dict['type'])) return parser(abundance_file_dict, abundances, min_shell, max_shell) def parse_supernova_section(supernova_dict): """ Parse the supernova section Parameters ---------- supernova_dict: dict YAML parsed supernova dict Returns ------- config_dict: dict """ config_dict = {} #parse luminosity luminosity_value, luminosity_unit = supernova_dict['luminosity_requested'].strip().split() if luminosity_unit == 'log_lsun': config_dict['luminosity_requested'] = 10 ** ( float(luminosity_value) + np.log10(constants.L_sun.cgs.value)) * u.erg / u.s else: config_dict['luminosity_requested'] = (float(luminosity_value) * u.Unit(luminosity_unit)).to('erg/s') config_dict['time_explosion'] = parse_quantity(supernova_dict['time_explosion']).to('s') if 'distance' in supernova_dict: config_dict['distance'] = parse_quantity(supernova_dict['distance']) else: config_dict['distance'] = None if 'luminosity_wavelength_start' in supernova_dict: config_dict['luminosity_nu_end'] = parse_quantity(supernova_dict['luminosity_wavelength_start']). \ to('Hz', u.spectral()) else: config_dict['luminosity_nu_end'] = np.inf * u.Hz if 'luminosity_wavelength_end' in supernova_dict: config_dict['luminosity_nu_start'] = parse_quantity(supernova_dict['luminosity_wavelength_end']). \ to('Hz', u.spectral()) else: config_dict['luminosity_nu_start'] = 0.0 * u.Hz return config_dict def parse_spectrum_list2dict(spectrum_list): """ Parse the spectrum list [start, stop, num] to a list """ if spectrum_list[0].unit.physical_type != 'length' and \ spectrum_list[1].unit.physical_type != 'length': raise ValueError('start and end of spectrum need to be a length') spectrum_config_dict = {} spectrum_config_dict['start'] = spectrum_list[0] spectrum_config_dict['end'] = spectrum_list[1] spectrum_config_dict['bins'] = spectrum_list[2] spectrum_frequency = np.linspace( spectrum_config_dict['end'].to('Hz', u.spectral()), spectrum_config_dict['start'].to('Hz', u.spectral()), num=spectrum_config_dict['bins'] + 1) spectrum_config_dict['frequency'] = spectrum_frequency return spectrum_config_dict def parse_convergence_section(convergence_section_dict): """ Parse the convergence section dictionary Parameters ---------- convergence_section_dict: ~dict dictionary """ convergence_parameters = ['damping_constant', 'threshold', 'fraction', 'hold_iterations'] for convergence_variable in ['t_inner', 't_rad', 'w']: if convergence_variable not in convergence_section_dict: convergence_section_dict[convergence_variable] = {} convergence_variable_section = convergence_section_dict[convergence_variable] for param in convergence_parameters: if convergence_variable_section.get(param, None) is None: if param in convergence_section_dict: convergence_section_dict[convergence_variable][param] = ( convergence_section_dict[param]) return convergence_section_dict def calculate_w7_branch85_densities(velocities, time_explosion, time_0=19.9999584, density_coefficient=3e29): """ Generated densities from the fit to W7 in Branch 85 page 620 (citation missing) Parameters ---------- velocities : `~numpy.ndarray` velocities in cm/s time_explosion : `float` time since explosion needed to descale density with expansion time_0 : `float` time in seconds of the w7 model - default 19.999, no reason to change density_coefficient : `float` coefficient for the polynomial - obtained by fitting to W7, no reason to change """ densities = density_coefficient * (velocities * 1e-5) ** -7 densities = calculate_density_after_time(densities, time_0, time_explosion) return densities[1:] class ConfigurationNameSpace(dict): """ The configuration name space class allows to wrap a dictionary and adds utility functions for easy access. Accesses like a.b.c are then possible Code from http://goo.gl/KIaq8I Parameters ---------- config_dict: ~dict configuration dictionary Returns ------- config_ns: ConfigurationNameSpace """ @classmethod def from_yaml(cls, fname): """ Read a configuration from a YAML file Parameters ---------- fname: str filename or path """ try: yaml_dict = yaml.load(file(fname)) except IOError as e: logger.critical('No config file named: %s', fname) raise e return cls.from_config_dict(yaml_dict) @classmethod def from_config_dict(cls, config_dict, config_definition_file=None): """ Validating a config file. Parameters ---------- config_dict : ~dict dictionary of a raw unvalidated config file Returns ------- `tardis.config_reader.Configuration` """ if config_definition_file is None: config_definition_file = default_config_definition_file config_definition = yaml.load(open(config_definition_file)) return cls(ConfigurationValidator(config_definition, config_dict).get_config()) marker = object() def __init__(self, value=None): if value is None: pass elif isinstance(value, dict): for key in value: self.__setitem__(key, value[key]) else: raise TypeError, 'expected dict' def __setitem__(self, key, value): if isinstance(value, dict) and not isinstance(value, ConfigurationNameSpace): value = ConfigurationNameSpace(value) if key in self and hasattr(self[key], 'unit'): value = u.Quantity(value, self[key].unit) dict.__setitem__(self, key, value) def __getitem__(self, key): return super(ConfigurationNameSpace, self).__getitem__(key) def __getattr__(self, item): if item in self: return self[item] else: super(ConfigurationNameSpace, self).__getattribute__(item) __setattr__ = __setitem__ def __dir__(self): return self.keys() def get_config_item(self, config_item_string): """ Get configuration items using a string of type 'a.b.param' Parameters ---------- config_item_string: ~str string of shape 'section1.sectionb.param1' """ config_item_path = config_item_string.split('.') if len(config_item_path) == 1: config_item = config_item_path[0] if config_item.startswith('item'): return self[config_item_path[0]] else: return self[config_item] elif len(config_item_path) == 2 and\ config_item_path[1].startswith('item'): return self[config_item_path[0]][ int(config_item_path[1].replace('item', ''))] else: return self[config_item_path[0]].get_config_item( '.'.join(config_item_path[1:])) def set_config_item(self, config_item_string, value): """ set configuration items using a string of type 'a.b.param' Parameters ---------- config_item_string: ~str string of shape 'section1.sectionb.param1' value: value to set the parameter with it """ config_item_path = config_item_string.split('.') if len(config_item_path) == 1: self[config_item_path[0]] = value elif len(config_item_path) == 2 and \ config_item_path[1].startswith('item'): current_value = self[config_item_path[0]][ int(config_item_path[1].replace('item', ''))] if hasattr(current_value, 'unit'): self[config_item_path[0]][ int(config_item_path[1].replace('item', ''))] =\ u.Quantity(value, current_value.unit) else: self[config_item_path[0]][ int(config_item_path[1].replace('item', ''))] = value else: self[config_item_path[0]].set_config_item( '.'.join(config_item_path[1:]), value) def deepcopy(self): return ConfigurationNameSpace(copy.deepcopy(dict(self))) class Configuration(ConfigurationNameSpace): """ Tardis configuration class """ @classmethod def from_yaml(cls, fname, test_parser=False): try: yaml_dict = yaml.load(open(fname)) except IOError as e: logger.critical('No config file named: %s', fname) raise e tardis_config_version = yaml_dict.get('tardis_config_version', None) if tardis_config_version != 'v1.0': raise ConfigurationError('Currently only tardis_config_version v1.0 supported') return cls.from_config_dict(yaml_dict, test_parser=test_parser) @classmethod def from_config_dict(cls, config_dict, atom_data=None, test_parser=False, config_definition_file=None, validate=True): """ Validating and subsequently parsing a config file. Parameters ---------- config_dict : ~dict dictionary of a raw unvalidated config file atom_data: ~tardis.atomic.AtomData atom data object. if `None` will be tried to be read from atom data file path in the config_dict [default=None] test_parser: ~bool switch on to ignore a working atom_data, mainly useful for testing this reader config_definition_file: ~str path to config definition file, if `None` will be set to the default in the `data` directory that ships with TARDIS validate: ~bool Turn validation on or off. Returns ------- `tardis.config_reader.Configuration` """ if config_definition_file is None: config_definition_file = default_config_definition_file config_definition = yaml.load(open(config_definition_file)) if validate: validated_config_dict = ConfigurationValidator(config_definition, config_dict).get_config() else: validated_config_dict = config_dict #First let's see if we can find an atom_db anywhere: if test_parser: atom_data = None elif 'atom_data' in validated_config_dict.keys(): atom_data_fname = validated_config_dict['atom_data'] validated_config_dict['atom_data_fname'] = atom_data_fname else: raise ConfigurationError('No atom_data key found in config or command line') if atom_data is None and not test_parser: logger.info('Reading Atomic Data from %s', atom_data_fname) atom_data = atomic.AtomData.from_hdf5(atom_data_fname) else: atom_data = atom_data #Parsing supernova dictionary validated_config_dict['supernova']['luminosity_nu_start'] = \ validated_config_dict['supernova']['luminosity_wavelength_end'].to( u.Hz, u.spectral()) try: validated_config_dict['supernova']['luminosity_nu_end'] = \ (validated_config_dict['supernova'] ['luminosity_wavelength_start'].to(u.Hz, u.spectral())) except ZeroDivisionError: validated_config_dict['supernova']['luminosity_nu_end'] = ( np.inf * u.Hz) validated_config_dict['supernova']['time_explosion'] = ( validated_config_dict['supernova']['time_explosion'].cgs) validated_config_dict['supernova']['luminosity_requested'] = ( validated_config_dict['supernova']['luminosity_requested'].cgs) #Parsing the model section model_section = validated_config_dict['model'] v_inner = None v_outer = None mean_densities = None abundances = None structure_section = model_section['structure'] if structure_section['type'] == 'specific': start, stop, num = model_section['structure']['velocity'] num += 1 velocities = np.linspace(start, stop, num) v_inner, v_outer = velocities[:-1], velocities[1:] mean_densities = parse_density_section( model_section['structure']['density'], v_inner, v_outer, validated_config_dict['supernova']['time_explosion']).cgs elif structure_section['type'] == 'file': v_inner, v_outer, mean_densities, inner_boundary_index, \ outer_boundary_index = read_density_file( structure_section['filename'], structure_section['filetype'], validated_config_dict['supernova']['time_explosion'], structure_section['v_inner_boundary'], structure_section['v_outer_boundary']) r_inner = validated_config_dict['supernova']['time_explosion'] * v_inner r_outer = validated_config_dict['supernova']['time_explosion'] * v_outer r_middle = 0.5 * (r_inner + r_outer) structure_validated_config_dict = {} structure_section['v_inner'] = v_inner.cgs structure_section['v_outer'] = v_outer.cgs structure_section['mean_densities'] = mean_densities.cgs no_of_shells = len(v_inner) structure_section['no_of_shells'] = no_of_shells structure_section['r_inner'] = r_inner.cgs structure_section['r_outer'] = r_outer.cgs structure_section['r_middle'] = r_middle.cgs structure_section['volumes'] = ((4. / 3) * np.pi * \ (r_outer ** 3 - r_inner ** 3)).cgs #### TODO the following is legacy code and should be removed validated_config_dict['structure'] = \ validated_config_dict['model']['structure'] # ^^^^^^^^^^^^^^^^ abundances_section = model_section['abundances'] if abundances_section['type'] == 'uniform': abundances = pd.DataFrame(columns=np.arange(no_of_shells), index=pd.Index(np.arange(1, 120), name='atomic_number'), dtype=np.float64) for element_symbol_string in abundances_section: if element_symbol_string == 'type': continue z = element_symbol2atomic_number(element_symbol_string) abundances.ix[z] = float(abundances_section[element_symbol_string]) elif abundances_section['type'] == 'file': index, abundances = read_abundances_file(abundances_section['filename'], abundances_section['filetype'], inner_boundary_index, outer_boundary_index) if len(index) != no_of_shells: raise ConfigurationError('The abundance file specified has not the same number of cells' 'as the specified density profile') abundances = abundances.replace(np.nan, 0.0) abundances = abundances[abundances.sum(axis=1) > 0] norm_factor = abundances.sum(axis=0) if np.any(np.abs(norm_factor - 1) > 1e-12): logger.warning("Abundances have not been normalized to 1. - normalizing") abundances /= norm_factor validated_config_dict['abundances'] = abundances ########### DOING PLASMA SECTION ############### plasma_section = validated_config_dict['plasma'] if plasma_section['initial_t_inner'] < 0.0 * u.K: luminosity_requested = validated_config_dict['supernova']['luminosity_requested'] plasma_section['t_inner'] = ((luminosity_requested / (4 * np.pi * r_inner[0] ** 2 * constants.sigma_sb)) ** .25).to('K') logger.info('"initial_t_inner" is not specified in the plasma ' 'section - initializing to %s with given luminosity', plasma_section['t_inner']) else: plasma_section['t_inner'] = plasma_section['initial_t_inner'] plasma_section['t_rads'] = np.ones(no_of_shells) * \ plasma_section['initial_t_rad'] if plasma_section['disable_electron_scattering'] is False: logger.debug("Electron scattering switched on") validated_config_dict['montecarlo']['sigma_thomson'] = 6.652486e-25 / (u.cm ** 2) else: logger.warn('Disabling electron scattering - this is not physical') validated_config_dict['montecarlo']['sigma_thomson'] = 1e-200 / (u.cm ** 2) ##### NLTE subsection of Plasma start nlte_validated_config_dict = {} nlte_species = [] nlte_section = plasma_section['nlte'] nlte_species_list = nlte_section.pop('species') for species_string in nlte_species_list: nlte_species.append(species_string_to_tuple(species_string)) nlte_validated_config_dict['species'] = nlte_species nlte_validated_config_dict['species_string'] = nlte_species_list nlte_validated_config_dict.update(nlte_section) if 'coronal_approximation' not in nlte_section: logger.debug('NLTE "coronal_approximation" not specified in NLTE section - defaulting to False') nlte_validated_config_dict['coronal_approximation'] = False if 'classical_nebular' not in nlte_section: logger.debug('NLTE "classical_nebular" not specified in NLTE section - defaulting to False') nlte_validated_config_dict['classical_nebular'] = False elif nlte_section: #checks that the dictionary is not empty logger.warn('No "species" given - ignoring other NLTE options given:\n%s', pp.pformat(nlte_section)) if not nlte_validated_config_dict: nlte_validated_config_dict['species'] = [] plasma_section['nlte'] = nlte_validated_config_dict #^^^^^^^^^^^^^^ End of Plasma Section ##### Monte Carlo Section montecarlo_section = validated_config_dict['montecarlo'] if montecarlo_section['last_no_of_packets'] < 0: montecarlo_section['last_no_of_packets'] = \ montecarlo_section['no_of_packets'] default_convergence_section = {'type': 'damped', 'lock_t_inner_cycles': 1, 't_inner_update_exponent': -0.5, 'damping_constant': 0.5} if montecarlo_section['convergence_strategy'] is None: logger.warning('No convergence criteria selected - ' 'just damping by 0.5 for w, t_rad and t_inner') montecarlo_section['convergence_strategy'] = ( parse_convergence_section(default_convergence_section)) else: montecarlo_section['convergence_strategy'] = ( parse_convergence_section( montecarlo_section['convergence_strategy'])) black_body_section = montecarlo_section['black_body_sampling'] montecarlo_section['black_body_sampling'] = {} montecarlo_section['black_body_sampling']['start'] = \ black_body_section[0] montecarlo_section['black_body_sampling']['end'] = \ black_body_section[1] montecarlo_section['black_body_sampling']['samples'] = \ black_body_section[2] ###### END of convergence section reading validated_config_dict['spectrum'] = parse_spectrum_list2dict( validated_config_dict['spectrum']) return cls(validated_config_dict, atom_data) def __init__(self, config_dict, atom_data): super(Configuration, self).__init__(config_dict) self.atom_data = atom_data selected_atomic_numbers = self.abundances.index if atom_data is not None: self.number_densities = (self.abundances * self.structure.mean_densities.to('g/cm^3').value) self.number_densities = self.number_densities.div(self.atom_data.atom_data.mass.ix[selected_atomic_numbers], axis=0) else: logger.critical('atom_data is None, only sensible for testing the parser')
bsd-3-clause
panmari/tensorflow
tensorflow/examples/skflow/boston.py
1
1485
# Copyright 2015-present Scikit Flow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from sklearn import datasets, cross_validation, metrics from sklearn import preprocessing from tensorflow.contrib import skflow # Load dataset boston = datasets.load_boston() X, y = boston.data, boston.target # Split dataset into train / test X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2, random_state=42) # scale data (training set) to 0 mean and unit Std. dev scaler = preprocessing.StandardScaler() X_train = scaler.fit_transform(X_train) # Build 2 layer fully connected DNN with 10, 10 units respecitvely. regressor = skflow.TensorFlowDNNRegressor(hidden_units=[10, 10], steps=5000, learning_rate=0.1, batch_size=1) # Fit regressor.fit(X_train, y_train) # Predict and score score = metrics.mean_squared_error(regressor.predict(scaler.fit_transform(X_test)), y_test) print('MSE: {0:f}'.format(score))
apache-2.0
Titan-C/scikit-learn
examples/cluster/plot_ward_structured_vs_unstructured.py
1
3369
""" =========================================================== Hierarchical clustering: structured vs unstructured ward =========================================================== Example builds a swiss roll dataset and runs hierarchical clustering on their position. For more information, see :ref:`hierarchical_clustering`. In a first step, the hierarchical clustering is performed without connectivity constraints on the structure and is solely based on distance, whereas in a second step the clustering is restricted to the k-Nearest Neighbors graph: it's a hierarchical clustering with structure prior. Some of the clusters learned without connectivity constraints do not respect the structure of the swiss roll and extend across different folds of the manifolds. On the opposite, when opposing connectivity constraints, the clusters form a nice parcellation of the swiss roll. """ # Authors : Vincent Michel, 2010 # Alexandre Gramfort, 2010 # Gael Varoquaux, 2010 # License: BSD 3 clause print(__doc__) import time as time import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mplot3d.axes3d as p3 from sklearn.cluster import AgglomerativeClustering from sklearn.datasets.samples_generator import make_swiss_roll # ############################################################################# # Generate data (swiss roll dataset) n_samples = 1500 noise = 0.05 X, _ = make_swiss_roll(n_samples, noise) # Make it thinner X[:, 1] *= .5 # ############################################################################# # Compute clustering print("Compute unstructured hierarchical clustering...") st = time.time() ward = AgglomerativeClustering(n_clusters=6, linkage='ward').fit(X) elapsed_time = time.time() - st label = ward.labels_ print("Elapsed time: %.2fs" % elapsed_time) print("Number of points: %i" % label.size) # ############################################################################# # Plot result fig = plt.figure() ax = p3.Axes3D(fig) ax.view_init(7, -80) for l in np.unique(label): ax.plot3D(X[label == l, 0], X[label == l, 1], X[label == l, 2], 'o', color=plt.cm.jet(np.float(l) / np.max(label + 1))) plt.title('Without connectivity constraints (time %.2fs)' % elapsed_time) # ############################################################################# # Define the structure A of the data. Here a 10 nearest neighbors from sklearn.neighbors import kneighbors_graph connectivity = kneighbors_graph(X, n_neighbors=10, include_self=False) # ############################################################################# # Compute clustering print("Compute structured hierarchical clustering...") st = time.time() ward = AgglomerativeClustering(n_clusters=6, connectivity=connectivity, linkage='ward').fit(X) elapsed_time = time.time() - st label = ward.labels_ print("Elapsed time: %.2fs" % elapsed_time) print("Number of points: %i" % label.size) # ############################################################################# # Plot result fig = plt.figure() ax = p3.Axes3D(fig) ax.view_init(7, -80) for l in np.unique(label): ax.plot3D(X[label == l, 0], X[label == l, 1], X[label == l, 2], 'o', color=plt.cm.jet(float(l) / np.max(label + 1))) plt.title('With connectivity constraints (time %.2fs)' % elapsed_time) plt.show()
bsd-3-clause
hep-gc/panda-autopyfactory
bin/factory.py
1
6335
#! /usr/bin/env python # # Simple(ish) python condor_g factory for panda pilots # # $Id$ # # # Copyright (C) 2007,2008,2009 Graeme Andrew Stewart # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from optparse import OptionParser import logging import logging.handlers import time import os import sys import traceback # Need to set PANDA_URL_MAP before the Client module is loaded (which happens # when the Factory module is loaded). Unfortunately this means that logging # is not yet available. if not 'APF_NOSQUID' in os.environ: if not 'PANDA_URL_MAP' in os.environ: os.environ['PANDA_URL_MAP'] = 'CERN,http://pandaserver.cern.ch:25085/server/panda,https://pandaserver.cern.ch:25443/server/panda' print >>sys.stderr, 'FACTORY DEBUG: Set PANDA_URL_MAP to %s' % os.environ['PANDA_URL_MAP'] else: print >>sys.stderr, 'FACTORY DEBUG: Found PANDA_URL_MAP set to %s. Not changed.' % os.environ['PANDA_URL_MAP'] if not 'PANDA_URL' in os.environ: os.environ['PANDA_URL'] = 'http://pandaserver.cern.ch:25085/server/panda' print >>sys.stderr, 'FACTORY DEBUG: Set PANDA_URL to %s' % os.environ['PANDA_URL'] else: print >>sys.stderr, 'FACTORY DEBUG: Found PANDA_URL set to %s. Not changed.' % os.environ['PANDA_URL'] else: print >>sys.stderr, 'FACTORY DEBUG: Found APF_NOSQUID set. Not changing/setting panda client environment.' from autopyfactory.Factory import factory from autopyfactory.Exceptions import FactoryConfigurationFailure def main(): parser = OptionParser(usage='''%prog [OPTIONS] autopyfactory is an ATLAS pilot factory. This program is licenced under the GPL, as set out in LICENSE file. Author(s): Graeme A Stewart <g.stewart@physics.gla.ac.uk>, Peter Love <p.love@lancaster.ac.uk> ''', version="%prog $Id$") parser.add_option("--verbose", "--debug", dest="logLevel", default=logging.INFO, action="store_const", const=logging.DEBUG, help="Set logging level to DEBUG [default INFO]") parser.add_option("--quiet", dest="logLevel", action="store_const", const=logging.WARNING, help="Set logging level to WARNING [default INFO]") parser.add_option("--test", "--dry-run", dest="dryRun", default=False, action="store_true", help="Dry run - supress job submission") parser.add_option("--oneshot", "--one-shot", dest="cyclesToDo", default=0, action="store_const", const=1, help="Run one cycle only") parser.add_option("--cycles", dest="cyclesToDo", action="store", type="int", metavar="CYCLES", help="Run CYCLES times, then exit [default infinite]") parser.add_option("--sleep", dest="sleepTime", default=120, action="store", type="int", metavar="TIME", help="Sleep TIME seconds between cycles [default %default]") parser.add_option("--conf", dest="confFiles", default="factory.conf", action="store", metavar="FILE1[,FILE2,FILE3]", help="Load configuration from FILEs (comma separated list)") parser.add_option("--log", dest="logfile", default="syslog", metavar="LOGFILE", action="store", help="Send logging output to LOGFILE or SYSLOG or stdout [default <syslog>]") (options, args) = parser.parse_args() options.confFiles = options.confFiles.split(',') # Setup logging factoryLogger = logging.getLogger('main') if options.logfile == "stdout": logStream = logging.StreamHandler() elif options.logfile == 'syslog': logStream = logging.handlers.SysLogHandler('/dev/log') else: logStream = logging.handlers.RotatingFileHandler(filename=options.logfile, maxBytes=10000000, backupCount=5) formatter = logging.Formatter('%(asctime)s - %(name)s: %(levelname)s %(message)s') logStream.setFormatter(formatter) factoryLogger.addHandler(logStream) factoryLogger.setLevel(options.logLevel) factoryLogger.debug('logging initialised') # Main loop try: f = factory(factoryLogger, options.dryRun, options.confFiles) cyclesDone = 0 while True: factoryLogger.info('\nStarting factory cycle %d at %s', cyclesDone, time.asctime(time.localtime())) f.factorySubmitCycle(cyclesDone) factoryLogger.info('Factory cycle %d done' % cyclesDone) cyclesDone += 1 if cyclesDone == options.cyclesToDo: break factoryLogger.info('Sleeping %ds' % options.sleepTime) time.sleep(options.sleepTime) f.updateConfig(cyclesDone) except KeyboardInterrupt: factoryLogger.info('Caught keyboard interrupt - exiting') except FactoryConfigurationFailure, errMsg: factoryLogger.error('Factory configuration failure: %s', errMsg) except ImportError, errorMsg: factoryLogger.error('Failed to import necessary python module: %s' % errorMsg) except: # TODO - make this a logger.exception() call factoryLogger.error('''Unexpected exception! There was an exception raised which the factory was not expecting and did not know how to handle. You may have discovered a new bug or an unforseen error condition. Please report this exception to Graeme <g.stewart@physics.gla.ac.uk>. The factory will now re-raise this exception so that the python stack trace is printed, which will allow it to be debugged - please send output from this message onwards. Exploding in 5...4...3...2...1... Have a nice day!''') # The following line prints the exception to the logging module factoryLogger.error(traceback.format_exc(None)) raise if __name__ == "__main__": main()
gpl-3.0
bibarz/bibarz.github.io
dabble/ab/auth_algorithms.py
1
17145
# Import any required libraries or modules. import numpy as np from sklearn import svm from sklearn.ensemble import RandomForestClassifier from sklearn.neighbors import KNeighborsClassifier import csv import sys class MetaParams: n_lda_ensemble = 101 lda_ensemble_feature_fraction = 0.4 mode = 'lda_ensemble' # The following is a hacky container for Statistics computed from the # whole training set; we don't want to have to recompute them again at every call # to build_template (it becomes slow for parameter searches with cross validation), # so we preserve it here between calls. The proper place to # do this would be in main.py, but we don't want to touch that. Global = lambda: None Global.ready = False def pca_converter(data, feature_discriminabilities, explained_variance): ''' PCA conversion of the data. The PCA is based on the complete dataset, but each feature is normalized to a std dev proportional to the given discriminability. :param data: n_samples x n_features matrix with all data to do PCA on :param feature_discriminabilities: n_features length vector :param explained_variance: ratio of explained variance (between 0 and 1) that will determine how many components are kept :return: function transforming data into pca components, and covariance matrix of transformed data ''' mu = np.mean(data, axis=0) std = np.std(data, axis=0) / feature_discriminabilities normalized_data = (data - mu) / std u, s, vt = np.linalg.svd(normalized_data) cut_idx = np.argmin(np.abs(np.cumsum(s * s) / np.sum(s * s) - explained_variance)) vt = vt[:cut_idx + 1] return (lambda x, mu=mu, std=std, vt=vt: np.dot((x - mu) / std, vt.T)),\ np.diag(s[:cut_idx + 1] ** 2 / (len(data) - 1)) def preprocess_data(data): ''' Turn raw data into an array of hand-picked features useful for classification :param data: n_samples x n_raw_features numpy array :return: n_samples x n_processed_features array ''' keypress_dt = data[:, 8::10] - data[:, 3::10] # duration of each keystroke key_to_key_dt = data[:, 13::10] - data[:, 3:-10:10] # interval between keystrokes x_down = data[:, 4::10].astype(np.float) / data[:, 1][:, None].astype(np.float) # x relative to screen width y_down = data[:, 5::10].astype(np.float) / data[:, 0][:, None].astype(np.float) # y relative to screen height x_up = data[:, 9::10].astype(np.float) / data[:, 1][:, None].astype(np.float) # x relative to screen width y_up = data[:, 10::10].astype(np.float) / data[:, 0][:, None].astype(np.float) # y relative to screen height size_down = data[:, 6::10] size_up = data[:, 11::10] pressure_down = data[:, 7::10] pressure_up = data[:, 12::10] assert np.all((x_down >= 0) & (x_down <= 1) & (y_down >= 0) & (y_down <= 1)) assert np.all((x_up >= 0) & (x_up <= 1) & (y_up >= 0) & (y_up <= 1)) touch_d = np.hypot(x_down - x_up, y_down - y_up) collected_data = np.hstack((keypress_dt, key_to_key_dt, np.diff(x_down, axis=1), np.diff(y_down, axis=1), touch_d, size_down, size_up, pressure_down, pressure_up, )) return collected_data def get_random_feature_selector(n_all_features, feature_fraction, seed): ''' Return a selector of random features from a data array :param n_all_features: total number of features :param feature_fraction: desired fraction of selected features :param seed: random seed for repeatable experiments :return: a function taking in full data and returning only the random features from it ''' n_features = int(np.round(feature_fraction * n_all_features)) rng = np.random.RandomState(seed) p = rng.permutation(n_all_features)[:n_features] return lambda x, p=p: x[..., p] def simple_gaussian(user_pca): # template will consist of mean and std dev of each feature in pca space mean_pca = np.mean(user_pca, axis=0) std_pca = np.std(user_pca, axis=0) return mean_pca, std_pca def scikit_classifier(user, training_dataset, generator=lambda:KNeighborsClassifier(5)): ''' Train a given classifier on user vs others :param generator: a function creating a scikit classifier with fit and predict functions :return: the trained classifier ''' all_users = training_dataset.keys() others_raw = np.vstack([training_dataset[u] for u in all_users if u != user]) others_pca = Global.pca(preprocess_data(others_raw)) user_raw = training_dataset[user] user_pca = Global.pca(preprocess_data(user_raw)) clf = generator() clf.fit(np.vstack((user_pca, others_pca)), np.hstack((np.zeros(len(user_pca)), np.ones(len(others_pca))))) return clf def lda(user_pca, all_pca_cov, n_all): ''' Compute the Fisher discriminant vector and threshold to classify user vs others. :param user_pca: n_samples x n_pca_features array of user instances :param all_pca_cov: covariance matrix of the complete dataset; it is assumed that the user data was part of the dataset, and that the mean of the whole dataset is 0 for every feature :param n_all: number of samples that formed the complete dataset :return: Fisher discriminant vector, threshold ''' n_user = len(user_pca) assert n_user < n_all - 1 # make sure the complete dataset has more than just the current user # We compute mean and variance for the user data directly, and infer the mean # and variance of the rest of the dataset from the covariance of the complete set # (and its mean, which is assumed zero) user_mu = np.mean(user_pca, axis=0) others_mu = - n_user * user_mu / (n_all - n_user) user_sigma = np.cov(user_pca.T) def sq_(x): return x[:, None] * x[None, :] others_sigma = ((n_all - 1) * all_pca_cov - (n_user - 1) * user_sigma\ - n_user * sq_(user_mu) - (n_all - n_user) * sq_(others_mu)) / (n_all - n_user - 1) ld_vector = np.dot(np.linalg.inv(user_sigma + others_sigma), user_mu - others_mu) # order determines sign of criterion ld_vector /= np.linalg.norm(ld_vector) # find the threshold for equal false positives and false negatives user_proj_mu = np.dot(user_mu, ld_vector) others_proj_mu = np.dot(others_mu, ld_vector) user_proj_std = np.sqrt(np.dot(ld_vector, np.dot(user_sigma, ld_vector))) others_proj_std = np.sqrt(np.dot(ld_vector, np.dot(others_sigma, ld_vector))) ld_threshold = (others_proj_std * user_proj_mu + user_proj_std * others_proj_mu) / (user_proj_std + others_proj_std) return ld_vector, ld_threshold def compute_feature_discriminabilities(each_preprocessed): ''' Return a vector of discriminability for each feature :param each_preprocessed: list with one n_samples x n_features data matrix for each user :return: vector of discriminabilities (sqrt of the square of the difference of means divided by the sum of variances) for each feature ''' n_users = len(each_preprocessed) each_mu = np.array([np.mean(m, axis=0) for m in each_preprocessed]) # n_users x n_features each_var = np.array([np.var(m, axis=0) for m in each_preprocessed]) # n_users x n_features # compute discriminability for each feature and pair of users pairwise_discriminability = (each_mu[:, None, :] - each_mu[None :, :]) ** 2 / (1e-6 + each_var[:, None, :] + each_var[None :, :]) # compute discriminability of each feature as the average over pairs of users return np.sqrt(np.sum(pairwise_discriminability, axis=(0, 1)) / (n_users * (n_users - 1))) def _prepare_global(training_dataset): ''' Processing of the complete dataset, to be reused for each user - feature preprocessing - pca converter - selection of features and computation of covariances for ensemble lda :param training_dataset: the complete dataset :return: None. The Global container is initialized with all necessary data ''' each_preprocessed = [preprocess_data(training_dataset[u]) for u in training_dataset] Global.feature_discriminabilities = compute_feature_discriminabilities(each_preprocessed) all_preprocessed = np.vstack(each_preprocessed) Global.n_all = len(all_preprocessed) Global.pca, Global.all_pca_cov = pca_converter(all_preprocessed, Global.feature_discriminabilities, explained_variance=0.98) if MetaParams.mode == 'lda_ensemble': Global.lda_ensemble = [] for i in range(MetaParams.n_lda_ensemble): seed = np.random.randint(200000) feature_selector = get_random_feature_selector(all_preprocessed.shape[1], feature_fraction=MetaParams.lda_ensemble_feature_fraction, seed=seed) selected_pca, selected_pca_cov = pca_converter(feature_selector(all_preprocessed), feature_selector(Global.feature_discriminabilities), explained_variance=0.99) Global.lda_ensemble.append({'selector': feature_selector, 'pca': selected_pca, 'pca_cov': selected_pca_cov}) Global.ready = True # Implement template building here. Feel free to write any helper classes or functions required. # Return the generated template for that user. def build_template(user, training_dataset): if not Global.ready: _prepare_global(training_dataset) user_raw = training_dataset[user] user_preprocessed = preprocess_data(user_raw) template = {} if MetaParams.mode in ['lda', 'simple', 'combined']: user_pca = Global.pca(user_preprocessed) template['mean_pca'], template['std_pca'] = simple_gaussian(user_pca) template['ld_vector'], template['ld_threshold'] =\ lda(user_pca, all_pca_cov=Global.all_pca_cov, n_all=Global.n_all) if MetaParams.mode == 'lda_ensemble': lda_ensemble = [] for lda_item in Global.lda_ensemble: user_selected_pca = lda_item['pca'](lda_item['selector'](user_preprocessed)) ld_vector, ld_threshold = lda(user_selected_pca, n_all=Global.n_all, all_pca_cov=lda_item['pca_cov']) lda_ensemble.append({'ld_vector': ld_vector, 'ld_threshold': ld_threshold}) template['lda_ensemble'] = lda_ensemble if MetaParams.mode in ['nonlinear', 'combined']: template['clf_1'] = scikit_classifier(user, training_dataset, generator=lambda: KNeighborsClassifier(5)) template['clf_2'] = scikit_classifier(user, training_dataset, generator=lambda: svm.LinearSVC(C=0.05, class_weight='balanced')) return template # Implement authentication method here. Feel free to write any helper classes or functions required. # Return the authenttication score and threshold above which you consider it being a correct user. def authenticate(instance, user, templates): mode = MetaParams.mode assert mode in ['lda', 'combined', 'lda_ensemble', 'nonlinear', 'simple'], ("Unrecognized mode: %s" % mode) t = templates[user] batch_mode = instance.ndim > 1 if not batch_mode: instance = instance[None, :] preprocessed_instance = preprocess_data(instance) if mode in ['lda', 'combined']: user_pca = Global.pca(preprocessed_instance) user_lda_proj = np.dot(user_pca, t['ld_vector']) lda_score, lda_thr = user_lda_proj - t['ld_threshold'], np.zeros(len(user_lda_proj)) if mode in ['nonlinear', 'combined']: user_pca = Global.pca(preprocessed_instance) clf_score_1, clf_thr_1 = (t['clf_1'].predict(user_pca) == 0).astype(np.float), 0.5 * np.ones(len(user_pca)) clf_score_2, clf_thr_2 = (t['clf_2'].predict(user_pca) == 0).astype(np.float), 0.5 * np.ones(len(user_pca)) if mode == 'simple': user_pca = Global.pca(preprocessed_instance) z = (user_pca - t['mean_pca']) / t['std_pca'] distance = np.mean(np.abs(z) ** 2, axis=1) ** 0.5 score, thr = distance, 1.2 * np.ones(len(distance)) if mode == 'lda_ensemble': ensemble_scores = np.empty((len(preprocessed_instance), len(t['lda_ensemble']))) for i, sub_t in enumerate(t['lda_ensemble']): g_item = Global.lda_ensemble[i] user_selected_pca = g_item['pca'](g_item['selector'](preprocessed_instance)) user_thinned_lda_proj = np.dot(user_selected_pca, sub_t['ld_vector']) ensemble_scores[:, i] = user_thinned_lda_proj - sub_t['ld_threshold'] score = np.mean(ensemble_scores > 0, axis=1) thr = 0.5 * np.ones(len(score)) if mode == 'lda': score, thr = lda_score, lda_thr elif mode == 'nonlinear': score, thr = clf_score_1, clf_thr_1 elif mode == 'combined': score = np.mean(np.vstack((lda_score > lda_thr, clf_score_1 > clf_thr_1, clf_score_2 > clf_thr_2)), axis=0) thr = 0.5 * np.ones(len(score)) if not batch_mode: assert score.shape == (1, ) assert thr.shape == (1, ) score, thr = score[0], thr[0] return score, thr def cross_validate(full_dataset, print_results=False): ''' n-fold cross-validation of given dataset :param full_dataset: dictionary of raw data for each user :param print_results: if True, print progress messages and results :return: (percentage of false rejects, percentage of false accepts) ''' n_folds = 5 # for cross-validation all_false_accept = 0 all_false_reject = 0 all_true_accept = 0 all_true_reject = 0 for i in range(n_folds): # split full dataset into training and validation training_dataset = dict() validation_dataset = dict() for u in full_dataset.keys(): n = len(full_dataset[u]) idx = np.round(float(n) / n_folds * np.arange(n_folds + 1)).astype(np.int) n_validation = np.diff(idx) rolled_set = np.roll(full_dataset[u], -idx[i], axis=0) training_dataset[u] = rolled_set[n_validation[i]:, :] validation_dataset[u] = rolled_set[:n_validation[i], :] # reset global data Global.ready = False templates = {u: build_template(u, training_dataset) for u in training_dataset} # For each user test authentication. true_accept = 0 false_reject = 0 true_reject = 0 false_accept = 0 for u in training_dataset: # Test false rejections. (score, threshold) = authenticate(validation_dataset[u], u, templates) true_accept += np.sum(score > threshold) false_reject += np.sum(score <= threshold) # Test false acceptance. for u_attacker in validation_dataset: if u == u_attacker: continue (score, threshold) = authenticate(validation_dataset[u_attacker], u, templates) false_accept += np.sum(score > threshold) true_reject += np.sum(score <= threshold) if print_results: print "fold %i: false reject rate: %.1f%%, false accept rate: %.1f%%" %\ (i, 100. * float(false_reject) / (false_reject + true_accept), 100. * float(false_accept) / (false_accept + true_reject)) all_false_accept += false_accept all_false_reject += false_reject all_true_accept += true_accept all_true_reject += true_reject false_reject_percent = 100. * float(all_false_reject) / (all_false_reject + all_true_accept) false_accept_percent = 100. * float(all_false_accept) / (all_false_accept + all_true_reject) if print_results: print "Total: false reject rate: %.1f%%, false accept rate: %.1f%%" % (false_reject_percent, false_accept_percent) return false_reject_percent, false_accept_percent if __name__ == "__main__": # Reading the data into the training dataset separated by user. data_training_file = open('dataset_training.csv', 'rb') csv_training_reader = csv.reader(data_training_file, delimiter=',', quotechar='"') csv_training_reader.next() full_dataset = dict() for row in csv_training_reader: if row[0] not in full_dataset: full_dataset[row[0]] = np.array([]).reshape((0, len(row[1:]))) full_dataset[row[0]] = np.vstack([full_dataset[row[0]], np.array(row[1:]).astype(float)]) for feature_fraction in [0.4]: for n_lda_ensemble in [51]: n_trials = 10 tot_rej = 0 tot_acc = 0 for _ in range(n_trials): MetaParams.feature_fraction = feature_fraction MetaParams.n_lda_ensemble = n_lda_ensemble rej, acc = cross_validate(full_dataset) tot_rej += rej tot_acc += acc print "feature fraction=%.2f, ensemble size=%i, false_rej=%.2f%%, false_acc=%.2f%%" % (feature_fraction, n_lda_ensemble, tot_rej / n_trials, tot_acc / n_trials)
mit
AxelTLarsson/robot-localisation
robot_localisation/main.py
1
6009
""" This module contains the logic to run the simulation. """ import sys import os import argparse import numpy as np sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from robot_localisation.grid import Grid, build_transition_matrix from robot_localisation.robot import Robot, Sensor from robot_localisation.hmm_filter import FilterState def help_text(): """ Return a helpful text explaining usage of the program. """ return """ ------------------------------- HMM Filtering --------------------------------- Type a command to get started. Type 'quit' or 'q' to quit. Valid commands (all commands are case insensitive): ENTER move the robot one step further in the simulation, will also output current pose and estimated position of the robot help show this help text show T show the transition matrix T show f show the filter column vector show O show the observation matrix quit | q quit the program ------------------------------------------------------------------------------- """ def main(): parser = argparse.ArgumentParser(description='Robot localisation with HMM') parser.add_argument( '-r', '--rows', type=int, help='the number of rows on the grid, default is 4', default=4) parser.add_argument( '-c', '--columns', type=int, help='the number of columns on the grid, default is 4', default=4) args = parser.parse_args() # Initialise the program size = (args.rows, args.columns) the_T_matrix = build_transition_matrix(*size) the_filter = FilterState(transition=the_T_matrix) the_sensor = Sensor() the_grid = Grid(*size) the_robot = Robot(the_grid, the_T_matrix) sensor_value = None obs = None print(help_text()) print("Grid size is {} x {}".format(size[0], size[1])) print(the_robot) print("The sensor says: {}".format(sensor_value)) filter_est = the_grid.index_to_pose(the_filter.belief_state) pos_est = (filter_est[0], filter_est[1]) print("The HMM filter thinks the robot is at {}".format(filter_est)) print("The Manhattan distance is: {}".format( manhattan(the_robot.get_position(), pos_est))) np.set_printoptions(linewidth=1000) # Main loop while True: user_command = str(input('> ')) if user_command.upper() == 'QUIT' or user_command.upper() == 'Q': break elif user_command.upper() == 'HELP': print(help_text()) elif user_command.upper() == 'SHOW T': print(the_T_matrix) elif user_command.upper() == 'SHOW F': print(the_filter.belief_matrix) elif user_command.upper() == 'SHOW O': print(obs) elif not user_command: # take a step then approximate etc. the_robot.step() sensor_value = the_sensor.get_position(the_robot) obs = the_sensor.get_obs_matrix(sensor_value, size) the_filter.forward(obs) print(the_robot) print("The sensor says: {}".format(sensor_value)) filter_est = the_grid.index_to_pose(the_filter.belief_state) pos_est = (filter_est[0], filter_est[1]) print("The HMM filter thinks the robot is at {}".format(filter_est)) print("The Manhattan distance is: {}".format( manhattan(the_robot.get_position(), pos_est))) else: print("Unknown command!") def manhattan(pos1, pos2): """ Calculate the Manhattan distance between pos1 and pos2. """ x1, y1 = pos1 x2, y2 = pos2 return abs(x1-x2) + abs(y1-y2) def automated_run(): import matplotlib.pyplot as plt fig = plt.figure(figsize=(10, 7)) navg = 20 nsteps = 10 for size in (2, 2), (3, 3), (4, 4), (5, 5), (10, 10): avg_distances = np.zeros(shape=(nsteps+1,)) for n in range(navg): distances = list() none_values = list() the_T_matrix = build_transition_matrix(*size) the_filter = FilterState(transition=the_T_matrix) the_sensor = Sensor() the_grid = Grid(*size) the_robot = Robot(the_grid, the_T_matrix) # get the manhattan distance at the start filter_est = the_grid.index_to_pose(the_filter.belief_state) pos_est = (filter_est[0], filter_est[1]) distances.append(manhattan(the_robot.get_position(), pos_est)) for i in range(nsteps): # take a step then approximate etc. the_robot.step() sensor_value = the_sensor.get_position(the_robot) if sensor_value is None: none_values.append(i) # keep track of where None was returned obs = the_sensor.get_obs_matrix(sensor_value, size) the_filter.forward(obs) filter_est = the_grid.index_to_pose(the_filter.belief_state) pos_est = (filter_est[0], filter_est[1]) distances.append(manhattan(the_robot.get_position(), pos_est)) avg_distances += np.array(distances) avg_distances /= navg base_line, = plt.plot(avg_distances, label="Grid size {}".format(size)) # for point in none_values: # plt.scatter(point, distances[point], marker='o', # color=base_line.get_color(), s=40) plt.legend() plt.xlim(0, nsteps) plt.ylim(0,) plt.ylabel("Manhattan distance") plt.xlabel("Steps") plt.title("Manhattan distance from true position and inferred position \n" "from the hidden Markov model (average over %s runs)" % navg) fig.savefig("automated_run.png") plt.show() if __name__ == '__main__': main() # automated_run()
mit
zfrenchee/pandas
pandas/tests/indexes/datetimes/test_arithmetic.py
1
21153
# -*- coding: utf-8 -*- import warnings from datetime import datetime, timedelta import pytest import numpy as np import pandas as pd import pandas.util.testing as tm from pandas.errors import PerformanceWarning from pandas import (Timestamp, Timedelta, Series, DatetimeIndex, TimedeltaIndex, date_range) @pytest.fixture(params=[None, 'UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/Asia/Singapore', 'dateutil/US/Pacific']) def tz(request): return request.param @pytest.fixture(params=[pd.offsets.Hour(2), timedelta(hours=2), np.timedelta64(2, 'h'), Timedelta(hours=2)], ids=str) def delta(request): # Several ways of representing two hours return request.param @pytest.fixture( params=[ datetime(2011, 1, 1), DatetimeIndex(['2011-01-01', '2011-01-02']), DatetimeIndex(['2011-01-01', '2011-01-02']).tz_localize('US/Eastern'), np.datetime64('2011-01-01'), Timestamp('2011-01-01')], ids=lambda x: type(x).__name__) def addend(request): return request.param class TestDatetimeIndexArithmetic(object): def test_dti_add_timestamp_raises(self): idx = DatetimeIndex(['2011-01-01', '2011-01-02']) msg = "cannot add DatetimeIndex and Timestamp" with tm.assert_raises_regex(TypeError, msg): idx + Timestamp('2011-01-01') def test_dti_radd_timestamp_raises(self): idx = DatetimeIndex(['2011-01-01', '2011-01-02']) msg = "cannot add DatetimeIndex and Timestamp" with tm.assert_raises_regex(TypeError, msg): Timestamp('2011-01-01') + idx # ------------------------------------------------------------- # Binary operations DatetimeIndex and int def test_dti_add_int(self, tz, one): # Variants of `one` for #19012 rng = pd.date_range('2000-01-01 09:00', freq='H', periods=10, tz=tz) result = rng + one expected = pd.date_range('2000-01-01 10:00', freq='H', periods=10, tz=tz) tm.assert_index_equal(result, expected) def test_dti_iadd_int(self, tz, one): rng = pd.date_range('2000-01-01 09:00', freq='H', periods=10, tz=tz) expected = pd.date_range('2000-01-01 10:00', freq='H', periods=10, tz=tz) rng += one tm.assert_index_equal(rng, expected) def test_dti_sub_int(self, tz, one): rng = pd.date_range('2000-01-01 09:00', freq='H', periods=10, tz=tz) result = rng - one expected = pd.date_range('2000-01-01 08:00', freq='H', periods=10, tz=tz) tm.assert_index_equal(result, expected) def test_dti_isub_int(self, tz, one): rng = pd.date_range('2000-01-01 09:00', freq='H', periods=10, tz=tz) expected = pd.date_range('2000-01-01 08:00', freq='H', periods=10, tz=tz) rng -= one tm.assert_index_equal(rng, expected) # ------------------------------------------------------------- # Binary operations DatetimeIndex and timedelta-like def test_dti_add_timedeltalike(self, tz, delta): rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) result = rng + delta expected = pd.date_range('2000-01-01 02:00', '2000-02-01 02:00', tz=tz) tm.assert_index_equal(result, expected) def test_dti_iadd_timedeltalike(self, tz, delta): rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('2000-01-01 02:00', '2000-02-01 02:00', tz=tz) rng += delta tm.assert_index_equal(rng, expected) def test_dti_sub_timedeltalike(self, tz, delta): rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('1999-12-31 22:00', '2000-01-31 22:00', tz=tz) result = rng - delta tm.assert_index_equal(result, expected) def test_dti_isub_timedeltalike(self, tz, delta): rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('1999-12-31 22:00', '2000-01-31 22:00', tz=tz) rng -= delta tm.assert_index_equal(rng, expected) # ------------------------------------------------------------- # Binary operations DatetimeIndex and TimedeltaIndex/array def test_dti_add_tdi(self, tz): # GH 17558 dti = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) tdi = pd.timedelta_range('0 days', periods=10) expected = pd.date_range('2017-01-01', periods=10, tz=tz) # add with TimdeltaIndex result = dti + tdi tm.assert_index_equal(result, expected) result = tdi + dti tm.assert_index_equal(result, expected) # add with timedelta64 array result = dti + tdi.values tm.assert_index_equal(result, expected) result = tdi.values + dti tm.assert_index_equal(result, expected) def test_dti_iadd_tdi(self, tz): # GH 17558 dti = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) tdi = pd.timedelta_range('0 days', periods=10) expected = pd.date_range('2017-01-01', periods=10, tz=tz) # iadd with TimdeltaIndex result = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) result += tdi tm.assert_index_equal(result, expected) result = pd.timedelta_range('0 days', periods=10) result += dti tm.assert_index_equal(result, expected) # iadd with timedelta64 array result = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) result += tdi.values tm.assert_index_equal(result, expected) result = pd.timedelta_range('0 days', periods=10) result += dti tm.assert_index_equal(result, expected) def test_dti_sub_tdi(self, tz): # GH 17558 dti = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) tdi = pd.timedelta_range('0 days', periods=10) expected = pd.date_range('2017-01-01', periods=10, tz=tz, freq='-1D') # sub with TimedeltaIndex result = dti - tdi tm.assert_index_equal(result, expected) msg = 'cannot subtract TimedeltaIndex and DatetimeIndex' with tm.assert_raises_regex(TypeError, msg): tdi - dti # sub with timedelta64 array result = dti - tdi.values tm.assert_index_equal(result, expected) msg = 'cannot perform __neg__ with this index type:' with tm.assert_raises_regex(TypeError, msg): tdi.values - dti def test_dti_isub_tdi(self, tz): # GH 17558 dti = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) tdi = pd.timedelta_range('0 days', periods=10) expected = pd.date_range('2017-01-01', periods=10, tz=tz, freq='-1D') # isub with TimedeltaIndex result = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) result -= tdi tm.assert_index_equal(result, expected) msg = 'cannot subtract TimedeltaIndex and DatetimeIndex' with tm.assert_raises_regex(TypeError, msg): tdi -= dti # isub with timedelta64 array result = DatetimeIndex([Timestamp('2017-01-01', tz=tz)] * 10) result -= tdi.values tm.assert_index_equal(result, expected) msg = '|'.join(['cannot perform __neg__ with this index type:', 'ufunc subtract cannot use operands with types']) with tm.assert_raises_regex(TypeError, msg): tdi.values -= dti # ------------------------------------------------------------- # Binary Operations DatetimeIndex and datetime-like # TODO: A couple other tests belong in this section. Move them in # A PR where there isn't already a giant diff. def test_add_datetimelike_and_dti(self, addend): # GH#9631 dti = DatetimeIndex(['2011-01-01', '2011-01-02']) msg = 'cannot add DatetimeIndex and {0}'.format( type(addend).__name__) with tm.assert_raises_regex(TypeError, msg): dti + addend with tm.assert_raises_regex(TypeError, msg): addend + dti def test_add_datetimelike_and_dti_tz(self, addend): # GH#9631 dti_tz = DatetimeIndex(['2011-01-01', '2011-01-02']).tz_localize('US/Eastern') msg = 'cannot add DatetimeIndex and {0}'.format( type(addend).__name__) with tm.assert_raises_regex(TypeError, msg): dti_tz + addend with tm.assert_raises_regex(TypeError, msg): addend + dti_tz # ------------------------------------------------------------- def test_sub_dti_dti(self): # previously performed setop (deprecated in 0.16.0), now changed to # return subtraction -> TimeDeltaIndex (GH ...) dti = date_range('20130101', periods=3) dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern') dti_tz2 = date_range('20130101', periods=3).tz_localize('UTC') expected = TimedeltaIndex([0, 0, 0]) result = dti - dti tm.assert_index_equal(result, expected) result = dti_tz - dti_tz tm.assert_index_equal(result, expected) with pytest.raises(TypeError): dti_tz - dti with pytest.raises(TypeError): dti - dti_tz with pytest.raises(TypeError): dti_tz - dti_tz2 # isub dti -= dti tm.assert_index_equal(dti, expected) # different length raises ValueError dti1 = date_range('20130101', periods=3) dti2 = date_range('20130101', periods=4) with pytest.raises(ValueError): dti1 - dti2 # NaN propagation dti1 = DatetimeIndex(['2012-01-01', np.nan, '2012-01-03']) dti2 = DatetimeIndex(['2012-01-02', '2012-01-03', np.nan]) expected = TimedeltaIndex(['1 days', np.nan, np.nan]) result = dti2 - dti1 tm.assert_index_equal(result, expected) def test_sub_period(self): # GH 13078 # not supported, check TypeError p = pd.Period('2011-01-01', freq='D') for freq in [None, 'D']: idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=freq) with pytest.raises(TypeError): idx - p with pytest.raises(TypeError): p - idx def test_ufunc_coercions(self): idx = date_range('2011-01-01', periods=3, freq='2D', name='x') delta = np.timedelta64(1, 'D') for result in [idx + delta, np.add(idx, delta)]: assert isinstance(result, DatetimeIndex) exp = date_range('2011-01-02', periods=3, freq='2D', name='x') tm.assert_index_equal(result, exp) assert result.freq == '2D' for result in [idx - delta, np.subtract(idx, delta)]: assert isinstance(result, DatetimeIndex) exp = date_range('2010-12-31', periods=3, freq='2D', name='x') tm.assert_index_equal(result, exp) assert result.freq == '2D' delta = np.array([np.timedelta64(1, 'D'), np.timedelta64(2, 'D'), np.timedelta64(3, 'D')]) for result in [idx + delta, np.add(idx, delta)]: assert isinstance(result, DatetimeIndex) exp = DatetimeIndex(['2011-01-02', '2011-01-05', '2011-01-08'], freq='3D', name='x') tm.assert_index_equal(result, exp) assert result.freq == '3D' for result in [idx - delta, np.subtract(idx, delta)]: assert isinstance(result, DatetimeIndex) exp = DatetimeIndex(['2010-12-31', '2011-01-01', '2011-01-02'], freq='D', name='x') tm.assert_index_equal(result, exp) assert result.freq == 'D' def test_datetimeindex_sub_timestamp_overflow(self): dtimax = pd.to_datetime(['now', pd.Timestamp.max]) dtimin = pd.to_datetime(['now', pd.Timestamp.min]) tsneg = Timestamp('1950-01-01') ts_neg_variants = [tsneg, tsneg.to_pydatetime(), tsneg.to_datetime64().astype('datetime64[ns]'), tsneg.to_datetime64().astype('datetime64[D]')] tspos = Timestamp('1980-01-01') ts_pos_variants = [tspos, tspos.to_pydatetime(), tspos.to_datetime64().astype('datetime64[ns]'), tspos.to_datetime64().astype('datetime64[D]')] for variant in ts_neg_variants: with pytest.raises(OverflowError): dtimax - variant expected = pd.Timestamp.max.value - tspos.value for variant in ts_pos_variants: res = dtimax - variant assert res[1].value == expected expected = pd.Timestamp.min.value - tsneg.value for variant in ts_neg_variants: res = dtimin - variant assert res[1].value == expected for variant in ts_pos_variants: with pytest.raises(OverflowError): dtimin - variant @pytest.mark.parametrize('box', [np.array, pd.Index]) def test_dti_add_offset_array(self, tz, box): # GH#18849 dti = pd.date_range('2017-01-01', periods=2, tz=tz) other = box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) with tm.assert_produces_warning(PerformanceWarning): res = dti + other expected = DatetimeIndex([dti[n] + other[n] for n in range(len(dti))], name=dti.name, freq='infer') tm.assert_index_equal(res, expected) with tm.assert_produces_warning(PerformanceWarning): res2 = other + dti tm.assert_index_equal(res2, expected) @pytest.mark.parametrize('box', [np.array, pd.Index]) def test_dti_sub_offset_array(self, tz, box): # GH#18824 dti = pd.date_range('2017-01-01', periods=2, tz=tz) other = box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) with tm.assert_produces_warning(PerformanceWarning): res = dti - other expected = DatetimeIndex([dti[n] - other[n] for n in range(len(dti))], name=dti.name, freq='infer') tm.assert_index_equal(res, expected) @pytest.mark.parametrize('names', [(None, None, None), ('foo', 'bar', None), ('foo', 'foo', 'foo')]) def test_dti_with_offset_series(self, tz, names): # GH#18849 dti = pd.date_range('2017-01-01', periods=2, tz=tz, name=names[0]) other = Series([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], name=names[1]) expected_add = Series([dti[n] + other[n] for n in range(len(dti))], name=names[2]) with tm.assert_produces_warning(PerformanceWarning): res = dti + other tm.assert_series_equal(res, expected_add) with tm.assert_produces_warning(PerformanceWarning): res2 = other + dti tm.assert_series_equal(res2, expected_add) expected_sub = Series([dti[n] - other[n] for n in range(len(dti))], name=names[2]) with tm.assert_produces_warning(PerformanceWarning): res3 = dti - other tm.assert_series_equal(res3, expected_sub) # GH 10699 @pytest.mark.parametrize('klass,assert_func', zip([Series, DatetimeIndex], [tm.assert_series_equal, tm.assert_index_equal])) def test_datetime64_with_DateOffset(klass, assert_func): s = klass(date_range('2000-01-01', '2000-01-31'), name='a') result = s + pd.DateOffset(years=1) result2 = pd.DateOffset(years=1) + s exp = klass(date_range('2001-01-01', '2001-01-31'), name='a') assert_func(result, exp) assert_func(result2, exp) result = s - pd.DateOffset(years=1) exp = klass(date_range('1999-01-01', '1999-01-31'), name='a') assert_func(result, exp) s = klass([Timestamp('2000-01-15 00:15:00', tz='US/Central'), pd.Timestamp('2000-02-15', tz='US/Central')], name='a') result = s + pd.offsets.Day() result2 = pd.offsets.Day() + s exp = klass([Timestamp('2000-01-16 00:15:00', tz='US/Central'), Timestamp('2000-02-16', tz='US/Central')], name='a') assert_func(result, exp) assert_func(result2, exp) s = klass([Timestamp('2000-01-15 00:15:00', tz='US/Central'), pd.Timestamp('2000-02-15', tz='US/Central')], name='a') result = s + pd.offsets.MonthEnd() result2 = pd.offsets.MonthEnd() + s exp = klass([Timestamp('2000-01-31 00:15:00', tz='US/Central'), Timestamp('2000-02-29', tz='US/Central')], name='a') assert_func(result, exp) assert_func(result2, exp) # array of offsets - valid for Series only if klass is Series: with tm.assert_produces_warning(PerformanceWarning): s = klass([Timestamp('2000-1-1'), Timestamp('2000-2-1')]) result = s + Series([pd.offsets.DateOffset(years=1), pd.offsets.MonthEnd()]) exp = klass([Timestamp('2001-1-1'), Timestamp('2000-2-29') ]) assert_func(result, exp) # same offset result = s + Series([pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(years=1)]) exp = klass([Timestamp('2001-1-1'), Timestamp('2001-2-1')]) assert_func(result, exp) s = klass([Timestamp('2000-01-05 00:15:00'), Timestamp('2000-01-31 00:23:00'), Timestamp('2000-01-01'), Timestamp('2000-03-31'), Timestamp('2000-02-29'), Timestamp('2000-12-31'), Timestamp('2000-05-15'), Timestamp('2001-06-15')]) # DateOffset relativedelta fastpath relative_kwargs = [('years', 2), ('months', 5), ('days', 3), ('hours', 5), ('minutes', 10), ('seconds', 2), ('microseconds', 5)] for i, kwd in enumerate(relative_kwargs): op = pd.DateOffset(**dict([kwd])) assert_func(klass([x + op for x in s]), s + op) assert_func(klass([x - op for x in s]), s - op) op = pd.DateOffset(**dict(relative_kwargs[:i + 1])) assert_func(klass([x + op for x in s]), s + op) assert_func(klass([x - op for x in s]), s - op) # assert these are equal on a piecewise basis offsets = ['YearBegin', ('YearBegin', {'month': 5}), 'YearEnd', ('YearEnd', {'month': 5}), 'MonthBegin', 'MonthEnd', 'SemiMonthEnd', 'SemiMonthBegin', 'Week', ('Week', {'weekday': 3}), 'BusinessDay', 'BDay', 'QuarterEnd', 'QuarterBegin', 'CustomBusinessDay', 'CDay', 'CBMonthEnd', 'CBMonthBegin', 'BMonthBegin', 'BMonthEnd', 'BusinessHour', 'BYearBegin', 'BYearEnd', 'BQuarterBegin', ('LastWeekOfMonth', {'weekday': 2}), ('FY5253Quarter', {'qtr_with_extra_week': 1, 'startingMonth': 1, 'weekday': 2, 'variation': 'nearest'}), ('FY5253', {'weekday': 0, 'startingMonth': 2, 'variation': 'nearest'}), ('WeekOfMonth', {'weekday': 2, 'week': 2}), 'Easter', ('DateOffset', {'day': 4}), ('DateOffset', {'month': 5})] with warnings.catch_warnings(record=True): for normalize in (True, False): for do in offsets: if isinstance(do, tuple): do, kwargs = do else: do = do kwargs = {} for n in [0, 5]: if (do in ['WeekOfMonth', 'LastWeekOfMonth', 'FY5253Quarter', 'FY5253'] and n == 0): continue op = getattr(pd.offsets, do)(n, normalize=normalize, **kwargs) assert_func(klass([x + op for x in s]), s + op) assert_func(klass([x - op for x in s]), s - op) assert_func(klass([op + x for x in s]), op + s)
bsd-3-clause
ahye/FYS2140-Resources
examples/animation/func_animate_sin.py
1
1284
#!/usr/bin/env python """ Created on Mon 2 Dec 2013 Eksempelscript som viser hvordan en sinusboelge kan animeres med funksjonsanimasjon. @author Benedicte Emilie Braekken """ from numpy import * from matplotlib.pyplot import * from matplotlib import animation def wave( x, t ): ''' Funksjonen beskriver en sinusboelge ved tiden t og punktet x. ''' omega = 1 # Vinkelhastighet k = 1 # Boelgetall return sin( k * x - omega * t ) T = 10 dt = 0.01 nx = 1e3 nt = int( T / dt ) # Antall tidssteg t = 0 all_waves = [] # Tom liste for aa ta vare paa boelgetilstandene x = linspace( -pi, pi, nx ) while t < T: # Legger til en ny boelgetilstand for hver kjoering all_waves.append( wave( x, t ) ) t += dt # Tegner initialtilstanden fig = figure() # Passer paa aa ta vare paa figuren line, = plot( x, all_waves[0] ) draw() # Konstanter til animasjonen FPS = 60 # Bilder i sekundet inter = 1. / FPS # Tid mellom hvert bilde def init(): ''' ''' line.set_data( [], [] ) return line, def get_frame( frame ): ''' ''' line.set_data( x, all_waves[ frame ] ) return line, anim = animation.FuncAnimation( fig, get_frame, init_func=init, frames=nt, interval=inter, blit=True ) show()
mit
theandygross/Figures
src/Figures/Boxplots.py
1
11851
""" Created on Apr 24, 2013 @author: agross """ import numpy as np import pandas as pd import matplotlib.pylab as plt import Stats.Scipy as Stats from Figures.FigureHelpers import latex_float, init_ax from Figures.FigureHelpers import prettify_ax from Helpers.Pandas import match_series, true_index colors = plt.rcParams['axes.color_cycle'] * 10 def _violin_plot(ax, data, pos=[], bp=False): """ http://pyinsci.blogspot.com/2009/09/violin-plot-with-matplotlib.html Create violin plots on an axis. Internal to module as it does not use Pandas data-structures. This is split off due to it's being a reuse of the code from the blog-post linked above, and I wanted to keep the original code untouched. """ from scipy.stats import gaussian_kde from numpy import arange # dist = max(pos)-min(pos) dist = len(pos) w = min(0.25 * max(dist, 1.0), 0.5) for p, d in enumerate(data): try: k = gaussian_kde(d) # calculates the kernel density m = k.dataset.min() # lower bound of violin M = k.dataset.max() # upper bound of violin x = arange(m, M, (M - m) / 100.) # support for violin v = k.evaluate(x) # violin profile (density curve) v = v / v.max() * w # scaling the violin to the available space ax.fill_betweenx(x, p, v + p, facecolor='y', alpha=0.1) ax.fill_betweenx(x, p, -v + p, facecolor='y', alpha=0.1) except: pass if bp: box_plot = ax.boxplot(data, notch=1, positions=range(len(pos)), vert=1, widths=.25) return box_plot def box_plot_pandas(bin_vec, real_vec, ax=None, order=None): """ Wrapper around matplotlib's boxplot function. Inputs bin_vec: Series of labels real_vec: Series of measurements to be grouped according to bin_vec """ _, ax = init_ax(ax) bin_vec, real_vec = match_series(bin_vec, real_vec) if order is not None: categories = order else: categories = bin_vec.value_counts().index data = [real_vec[bin_vec == num] for num in categories] bp = ax.boxplot(data, positions=range(len(categories)), widths=.3, patch_artist=True) if real_vec.name: ax.set_ylabel(real_vec.name) if bin_vec.name: ax.set_xlabel(bin_vec.name) ax.set_xticklabels(categories) [p.set_visible(False) for p in bp['fliers']] [p.set_visible(False) for p in bp['caps']] [p.set_visible(False) for p in bp['whiskers']] for p in bp['medians']: p.set_color(colors[0]) p.set_lw(3) p.set_alpha(.8) for i, p in enumerate(bp['boxes']): p.set_color('grey') p.set_lw(3) p.set_alpha(.7) if len(data[i]) < 3: p.set_alpha(0) def violin_plot_pandas(bin_vec, real_vec, ann='p', order=None, ax=None, filename=None): """ http://pyinsci.blogspot.com/2009/09/violin-plot-with-matplotlib.html Wrapper around matplotlib's boxplot function to add violin profile. Inputs bin_vec: Series of labels real_vec: Series of measurements to be grouped according to bin_vec """ fig, ax = init_ax(ax) ax.set_ylabel(real_vec.name) ax.set_xlabel(bin_vec.name) bin_vec, real_vec = match_series(bin_vec, real_vec) try: if order is None: categories = bin_vec.value_counts().index else: categories = order _violin_plot(ax, [real_vec[bin_vec == num] for num in categories], pos=categories, bp=True) ax.set_xticklabels([str(c) + '\n(n=%i)' % sum(bin_vec == c) for c in categories]) except: box_plot_pandas(bin_vec, real_vec, ax=ax) #if type(bin_vec.name) == str: # ax.set_title(str(bin_vec.name) + ' x ' + str(real_vec.name)) p_value = Stats.kruskal_pandas(bin_vec, real_vec)['p'] if ann == 'p_fancy': ax.annotate('$p = {}$'.format(latex_float(p_value)), (.95, -.02), xycoords='axes fraction', ha='right', va='bottom', size=14) if ann == 'p': ax.annotate('p = {0:.1e}'.format(p_value), (.95, .02), xycoords='axes fraction', ha='right', va='bottom', size=12) elif ann is not None: ax.annotate(ann, (.95, .02), xycoords='axes fraction', ha='right', va='bottom', size=12) if filename is not None: fig.savefig(filename) return def violin_plot_series(s, **kw_args): """ Wrapper for drawing a violin plot on a series with a multi-index. The second level of the index is used as the binning variable. """ assert s.index.levshape[1] > 1 violin_plot_pandas(pd.Series(s.index.get_level_values(1), s.index), s, **kw_args) def paired_boxplot_o(boxes): """ Wrapper around plt.boxplot to draw paired boxplots for a set of boxes. Input is the same as plt.boxplot: Array or a sequence of vectors. """ fig = plt.figure(figsize=(len(boxes) / 2.5, 4)) ax1 = fig.add_subplot(111) plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25) bp = ax1.boxplot(boxes, notch=0, positions=np.arange(len(boxes)) + 1.5 * (np.arange(len(boxes)) / 2), patch_artist=True) [p.set_color(colors[0]) for p in bp['boxes'][::2]] [p.set_color('black') for p in bp['whiskers']] [p.set_color('black') for p in bp['fliers']] [p.set_alpha(.4) for p in bp['fliers']] [p.set_alpha(.6) for p in bp['boxes']] [p.set_edgecolor('black') for p in bp['boxes']] ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) # Hide these grid behind plot objects ax1.set_axisbelow(True) ax1.set_ylabel('$Log_{2}$ RNA Expression') ax1.set_xticks(3.5 * np.arange(len(boxes) / 2) + .5) return ax1, bp def paired_boxplot(boxes, ax1=None): if not ax1: fig = plt.figure(figsize=(len(boxes) / 2.5, 4)) ax1 = fig.add_subplot(111) plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25) bp = ax1.boxplot(boxes, notch=0, positions=np.arange(len(boxes)) + 1.5 * (np.arange(len(boxes)) / 2), patch_artist=True) [p.set_color(colors[0]) for p in bp['boxes'][::2]] [p.set_color(colors[1]) for p in bp['boxes'][1::2]] [p.set_color('black') for p in bp['whiskers']] [p.set_color('black') for p in bp['fliers']] [p.set_alpha(.4) for p in bp['fliers']] [p.set_alpha(.8) for p in bp['boxes']] [p.set_edgecolor('black') for p in bp['boxes']] ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) # Hide these grid behind plot objects ax1.set_axisbelow(True) ax1.set_ylabel('$Log_{2}$ RNA Expression') ax1.set_xticks(3.5 * np.arange(len(boxes) / 2) + .5) return ax1, bp def paired_boxplot_tumor_normal(df, sig=True, cutoffs=[.01, .00001], order=None, ax=None): """ Draws a paired boxplot given a DataFrame with both tumor and normal samples on the index. '01' and '11' are hard-coded as the ids for tumor/normal. """ n = df.groupby(level=0).size() == 2 df = df.ix[n[n].index] if order is None: o = df.xs('11', level=1).median().order().index df = df[o[::-1]] else: df = df[order] l1 = list(df.xs('01', level=1).as_matrix().T) l2 = list(df.xs('11', level=1).as_matrix().T) boxes = [x for t in zip(l1, l2) for x in t] ax1, bp = paired_boxplot(boxes, ax) test = lambda v: Stats.ttest_rel(v.unstack()['01'], v.unstack()['11']) res = df.apply(test).T p = res.p if sig: pts = [(i * 3.5 + .5, 18) for i, n in enumerate(p) if n < cutoffs[1]] if len(pts) > 0: s1 = ax1.scatter(*zip(*pts), marker='$**$', label='$p<10^{-5}$', s=200) else: s1 = None pts = [(i * 3.5 + .5, 18) for i, n in enumerate(p) if (n < cutoffs[0]) and (n > cutoffs[1])] if len(pts) > 0: s2 = ax1.scatter(*zip(*pts), marker='$*$', label='$p<10^{-2}$', s=30) else: s2 = None ax1.legend(bp['boxes'][:2] + [s2, s1], ('Tumor', 'Normal', '$p<10^{-2}$', '$p<10^{-5}$'), loc='best', scatterpoints=1) else: ax1.legend(bp['boxes'][:2], ('Tumor', 'Normal'), loc='best') ax1.set_xticklabels(df.columns) def boxplot_panel(hit_vec, response_df): """ Draws a series of paired boxplots with the rows of the response_df split according to hit_vec. """ b = response_df.copy() b.columns = pd.MultiIndex.from_arrays([b.columns, hit_vec.ix[b.columns]]) b = b.T v1, v2 = hit_vec.unique() test = lambda v: Stats.anova(v.reset_index(level=1)[v.index.names[1]], v.reset_index(level=1)[v.name]) res = b.apply(test).T p = res.p.order() b = b.ix[:, p.index] l1 = list(b.xs(v1, level=1).as_matrix().T) l2 = list(b.xs(v2, level=1).as_matrix().T) boxes = [x for t in zip(l1, l2) for x in t] ax1, bp = paired_boxplot(boxes) y_lim = (response_df.T.quantile(.9).max()) * 1.2 pts = [(i * 3.5 + .5, y_lim) for i, n in enumerate(p) if n < .00001] if len(pts) > 0: s1 = ax1.scatter(*zip(*pts), marker='$**$', label='$p<10^{-5}$', s=200) else: s1 = None pts = [(i * 3.5 + .5, y_lim) for i, n in enumerate(p) if (n < .01) and (n > .00001)] if len(pts) > 0: s2 = ax1.scatter(*zip(*pts), marker='$*$', label='$p<10^{-2}$', s=30) else: s2 = None ax1.set_xticklabels(b.columns) ax1.legend(bp['boxes'][:2] + [s2, s1], (v1, v2, '$p<10^{-2}$', '$p<10^{-5}$'), loc='best', scatterpoints=1) def paired_bp_tn_split(vec, assignment, ax=None, split_vals=('01', '11'), data_type='gene expression'): """ Paired boxplot for a single Series, with splitting on the index, grouped by assignment. I.E. Tumor-Normal gene expression split by cancer. vec: vector of values to plot. assignment: vector mapping keys to group assignment ax (None): matplotlib axis to plot on or None split_vals ('01','11'): Values to split the boxplot pairing on. The default of ('01','11') indicates tumor vs. normal in the standard TCGA barcode nomenclature. This should coorespond to values on the second level of the index for vec and assignment. **both vec and assignment should have an overlapping index with multiple levels** """ _, ax = init_ax(ax, figsize=(8, 3)) if vec.name != None: label = vec.name # lose label in manipulation else: label = '' g1 = split_vals[0] g2 = split_vals[1] vec = pd.concat([vec[:, g1], vec[:, g2]], keys=[g1, g2], axis=1) vec = vec.dropna().stack() counts = vec.unstack().groupby(assignment).size() groups = list(true_index(counts > 5)) groups = vec.unstack().groupby(assignment).median()[g1].ix[groups] groups = groups.order().index[::-1] l1 = [np.array(vec[:, g1].ix[true_index(assignment == c)].dropna()) for c in groups] l2 = [np.array(vec[:, g2].ix[true_index(assignment == c)].dropna()) for c in groups] boxes = [x for t in zip(l1, l2) for x in t if len(t[1]) > 5] ax, bp = paired_boxplot(boxes, ax) labels = ['{}\n({})'.format(c, counts[c]) for c in groups] ax.set_xticklabels(labels) prettify_ax(ax) ax.set_ylabel('{} {}'.format(label, data_type))
mit
bnoi/scikit-tracker
sktracker/tracker/cost_function/tests/test_abstract_cost_functions.py
1
1500
# -*- coding: utf-8 -*- from __future__ import unicode_literals from __future__ import division from __future__ import absolute_import from __future__ import print_function from nose.tools import assert_raises import sys import pandas as pd import numpy as np from sktracker.tracker.cost_function import AbstractCostFunction def test_abstract_cost_function(): cost_func = AbstractCostFunction(context={}, parameters={}) assert cost_func.get_block() == None def test_abstract_cost_function_check_context(): cost_func = AbstractCostFunction(context={'cost': 1}, parameters={}) assert_raises(ValueError, cost_func.check_context, 'test_string', str) cost_func.context['test_string'] = 5 assert_raises(TypeError, cost_func.check_context, 'test_string', str) cost_func.context['test_string'] = "i am a string" ### This fails in py2.7 if sys.version_info[0] > 2: cost_func.check_context('test_string', str) assert True def test_abstract_cost_function_check_columns(): cost_func = AbstractCostFunction(context={}, parameters={}) df = pd.DataFrame([np.arange(0, 5), np.arange(20, 25)], columns=['x', 'y', 'z', 'w', 't']) cost_func.check_columns(df, ['t', 'z', 'y']) cost_func.check_columns([df], ['t', 'z', 'y']) df = pd.DataFrame([np.arange(0, 4), np.arange(20, 24)], columns=['x', 'y', 'w', 't']) assert_raises(ValueError, cost_func.check_columns, df, ['t', 'z', 'y'])
bsd-3-clause
belkinsky/SFXbot
src/pyAudioAnalysis/audioTrainTest.py
1
46228
import sys import numpy import time import os import glob import pickle import shutil import audioop import signal import csv import ntpath from . import audioFeatureExtraction as aF from . import audioBasicIO from matplotlib.mlab import find import matplotlib.pyplot as plt import scipy.io as sIO from scipy import linalg as la from scipy.spatial import distance import sklearn.svm import sklearn.decomposition import sklearn.ensemble def signal_handler(signal, frame): print('You pressed Ctrl+C! - EXIT') os.system("stty -cbreak echo") sys.exit(0) signal.signal(signal.SIGINT, signal_handler) shortTermWindow = 0.050 shortTermStep = 0.050 eps = 0.00000001 class kNN: def __init__(self, X, Y, k): self.X = X self.Y = Y self.k = k def classify(self, testSample): nClasses = numpy.unique(self.Y).shape[0] YDist = (distance.cdist(self.X, testSample.reshape(1, testSample.shape[0]), 'euclidean')).T iSort = numpy.argsort(YDist) P = numpy.zeros((nClasses,)) for i in range(nClasses): P[i] = numpy.nonzero(self.Y[iSort[0][0:self.k]] == i)[0].shape[0] / float(self.k) return (numpy.argmax(P), P) def classifierWrapper(classifier, classifierType, testSample): ''' This function is used as a wrapper to pattern classification. ARGUMENTS: - classifier: a classifier object of type sklearn.svm.SVC or kNN (defined in this library) or sklearn.ensemble.RandomForestClassifier or sklearn.ensemble.GradientBoostingClassifier or sklearn.ensemble.ExtraTreesClassifier - classifierType: "svm" or "knn" or "randomforests" or "gradientboosting" or "extratrees" - testSample: a feature vector (numpy array) RETURNS: - R: class ID - P: probability estimate EXAMPLE (for some audio signal stored in array x): import audioFeatureExtraction as aF import audioTrainTest as aT # load the classifier (here SVM, for kNN use loadKNNModel instead): [Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep] = aT.loadSVModel(modelName) # mid-term feature extraction: [MidTermFeatures, _] = aF.mtFeatureExtraction(x, Fs, mtWin * Fs, mtStep * Fs, round(Fs*stWin), round(Fs*stStep)); # feature normalization: curFV = (MidTermFeatures[:, i] - MEAN) / STD; # classification [Result, P] = classifierWrapper(Classifier, modelType, curFV) ''' R = -1 P = -1 if classifierType == "knn": [R, P] = classifier.classify(testSample) elif classifierType == "svm" or classifierType == "randomforest" or classifierType == "gradientboosting" or "extratrees": R = classifier.predict(testSample.reshape(1,-1))[0] P = classifier.predict_proba(testSample.reshape(1,-1))[0] return [R, P] def regressionWrapper(model, modelType, testSample): ''' This function is used as a wrapper to pattern classification. ARGUMENTS: - model: regression model - modelType: "svm" or "knn" (TODO) - testSample: a feature vector (numpy array) RETURNS: - R: regression result (estimated value) EXAMPLE (for some audio signal stored in array x): TODO ''' if modelType == "svm" or modelType == "randomforest": return (model.predict(testSample.reshape(1,-1))[0]) # elif classifierType == "knn": # TODO return None def randSplitFeatures(features, partTrain): ''' def randSplitFeatures(features): This function splits a feature set for training and testing. ARGUMENTS: - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. each matrix features[i] of class i is [numOfSamples x numOfDimensions] - partTrain: percentage RETURNS: - featuresTrains: a list of training data for each class - featuresTest: a list of testing data for each class ''' featuresTrain = [] featuresTest = [] for i, f in enumerate(features): [numOfSamples, numOfDims] = f.shape randperm = numpy.random.permutation(list(range(numOfSamples))) nTrainSamples = int(round(partTrain * numOfSamples)) featuresTrain.append(f[randperm[0:nTrainSamples]]) featuresTest.append(f[randperm[nTrainSamples::]]) return (featuresTrain, featuresTest) def trainKNN(features, K): ''' Train a kNN classifier. ARGUMENTS: - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. each matrix features[i] of class i is [numOfSamples x numOfDimensions] - K: parameter K RETURNS: - kNN: the trained kNN variable ''' [Xt, Yt] = listOfFeatures2Matrix(features) knn = kNN(Xt, Yt, K) return knn def trainSVM(features, Cparam): ''' Train a multi-class probabilitistic SVM classifier. Note: This function is simply a wrapper to the sklearn functionality for SVM training See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes. ARGUMENTS: - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features each matrix features[i] of class i is [numOfSamples x numOfDimensions] - Cparam: SVM parameter C (cost of constraints violation) RETURNS: - svm: the trained SVM variable NOTE: This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided. ''' [X, Y] = listOfFeatures2Matrix(features) svm = sklearn.svm.SVC(C = Cparam, kernel = 'linear', probability = True) svm.fit(X,Y) return svm def trainRandomForest(features, n_estimators): ''' Train a multi-class decision tree classifier. Note: This function is simply a wrapper to the sklearn functionality for SVM training See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes. ARGUMENTS: - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features each matrix features[i] of class i is [numOfSamples x numOfDimensions] - n_estimators: number of trees in the forest RETURNS: - svm: the trained SVM variable NOTE: This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided. ''' [X, Y] = listOfFeatures2Matrix(features) rf = sklearn.ensemble.RandomForestClassifier(n_estimators = n_estimators) rf.fit(X,Y) return rf def trainGradientBoosting(features, n_estimators): ''' Train a gradient boosting classifier Note: This function is simply a wrapper to the sklearn functionality for SVM training See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes. ARGUMENTS: - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features each matrix features[i] of class i is [numOfSamples x numOfDimensions] - n_estimators: number of trees in the forest RETURNS: - svm: the trained SVM variable NOTE: This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided. ''' [X, Y] = listOfFeatures2Matrix(features) rf = sklearn.ensemble.GradientBoostingClassifier(n_estimators = n_estimators) rf.fit(X,Y) return rf def trainExtraTrees(features, n_estimators): ''' Train a gradient boosting classifier Note: This function is simply a wrapper to the sklearn functionality for extra tree classifiers See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes. ARGUMENTS: - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features each matrix features[i] of class i is [numOfSamples x numOfDimensions] - n_estimators: number of trees in the forest RETURNS: - svm: the trained SVM variable NOTE: This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided. ''' [X, Y] = listOfFeatures2Matrix(features) et = sklearn.ensemble.ExtraTreesClassifier(n_estimators = n_estimators) et.fit(X,Y) return et def trainSVMregression(Features, Y, Cparam): svm = sklearn.svm.SVR(C = Cparam, kernel = 'linear') print(Features.shape, Y) svm.fit(Features,Y) trainError = numpy.mean(numpy.abs(svm.predict(Features) - Y)) return svm, trainError # TODO (not avaiable for regression?) #def trainRandomForestRegression(Features, Y, n_estimators): # rf = sklearn.ensemble.RandomForestClassifier(n_estimators = n_estimators) # print Features.shape, Y # rf.fit(Features,Y) # trainError = numpy.mean(numpy.abs(rf.predict(Features) - Y)) # return rf, trainError def featureAndTrain(listOfDirs, mtWin, mtStep, stWin, stStep, classifierType, modelName, computeBEAT=False, perTrain=0.90): ''' This function is used as a wrapper to segment-based audio feature extraction and classifier training. ARGUMENTS: listOfDirs: list of paths of directories. Each directory contains a signle audio class whose samples are stored in seperate WAV files. mtWin, mtStep: mid-term window length and step stWin, stStep: short-term window and step classifierType: "svm" or "knn" or "randomforest" or "gradientboosting" or "extratrees" modelName: name of the model to be saved RETURNS: None. Resulting classifier along with the respective model parameters are saved on files. ''' # STEP A: Feature Extraction: [features, classNames, _] = aF.dirsWavFeatureExtraction(listOfDirs, mtWin, mtStep, stWin, stStep, computeBEAT=computeBEAT) if len(features) == 0: print("trainSVM_feature ERROR: No data found in any input folder!") return numOfFeatures = features[0].shape[1] featureNames = ["features" + str(d + 1) for d in range(numOfFeatures)] writeTrainDataToARFF(modelName, features, classNames, featureNames) for i, f in enumerate(features): if len(f) == 0: print("trainSVM_feature ERROR: " + listOfDirs[i] + " folder is empty or non-existing!") return # STEP B: Classifier Evaluation and Parameter Selection: if classifierType == "svm": classifierParams = numpy.array([0.001, 0.01, 0.5, 1.0, 5.0, 10.0]) elif classifierType == "randomforest": classifierParams = numpy.array([10, 25, 50, 100,200,500]) elif classifierType == "knn": classifierParams = numpy.array([1, 3, 5, 7, 9, 11, 13, 15]) elif classifierType == "gradientboosting": classifierParams = numpy.array([10, 25, 50, 100,200,500]) elif classifierType == "extratrees": classifierParams = numpy.array([10, 25, 50, 100,200,500]) # get optimal classifeir parameter: bestParam = evaluateClassifier(features, classNames, 100, classifierType, classifierParams, 0, perTrain) print("Selected params: {0:.5f}".format(bestParam)) C = len(classNames) [featuresNorm, MEAN, STD] = normalizeFeatures(features) # normalize features MEAN = MEAN.tolist() STD = STD.tolist() featuresNew = featuresNorm # STEP C: Save the classifier to file if classifierType == "svm": Classifier = trainSVM(featuresNew, bestParam) with open(modelName, 'wb') as fid: # save to file pickle.dump(Classifier, fid) fo = open(modelName + "MEANS", "wb") pickle.dump(MEAN, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(STD, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(classNames, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(computeBEAT, fo, protocol=pickle.HIGHEST_PROTOCOL) fo.close() elif classifierType == "randomforest": Classifier = trainRandomForest(featuresNew, bestParam) with open(modelName, 'wb') as fid: # save to file pickle.dump(Classifier, fid) fo = open(modelName + "MEANS", "wb") pickle.dump(MEAN, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(STD, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(classNames, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(computeBEAT, fo, protocol=pickle.HIGHEST_PROTOCOL) fo.close() elif classifierType == "gradientboosting": Classifier = trainGradientBoosting(featuresNew, bestParam) with open(modelName, 'wb') as fid: # save to file pickle.dump(Classifier, fid) fo = open(modelName + "MEANS", "wb") pickle.dump(MEAN, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(STD, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(classNames, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(computeBEAT, fo, protocol=pickle.HIGHEST_PROTOCOL) fo.close() elif classifierType == "extratrees": Classifier = trainExtraTrees(featuresNew, bestParam) with open(modelName, 'wb') as fid: # save to file pickle.dump(Classifier, fid) fo = open(modelName + "MEANS", "wb") pickle.dump(MEAN, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(STD, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(classNames, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(computeBEAT, fo, protocol=pickle.HIGHEST_PROTOCOL) fo.close() elif classifierType == "knn": [X, Y] = listOfFeatures2Matrix(featuresNew) X = X.tolist() Y = Y.tolist() fo = open(modelName, "wb") pickle.dump(X, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(Y, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(MEAN, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(STD, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(classNames, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(bestParam, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(computeBEAT, fo, protocol=pickle.HIGHEST_PROTOCOL) fo.close() def featureAndTrainRegression(dirName, mtWin, mtStep, stWin, stStep, modelType, modelName, computeBEAT=False): ''' This function is used as a wrapper to segment-based audio feature extraction and classifier training. ARGUMENTS: dirName: path of directory containing the WAV files and Regression CSVs mtWin, mtStep: mid-term window length and step stWin, stStep: short-term window and step modelType: "svm" or "knn" or "randomforest" modelName: name of the model to be saved RETURNS: None. Resulting regression model along with the respective model parameters are saved on files. ''' # STEP A: Feature Extraction: [features, _, fileNames] = aF.dirsWavFeatureExtraction([dirName], mtWin, mtStep, stWin, stStep, computeBEAT=computeBEAT) features = features[0] fileNames = [ntpath.basename(f) for f in fileNames[0]] # Read CSVs: CSVs = glob.glob(dirName + os.sep + "*.csv") regressionLabels = [] regressionNames = [] for c in CSVs: # for each CSV curRegressionLabels = numpy.zeros((len(fileNames, ))) # read filenames, map to "fileNames" and append respective values in the regressionLabels with open(c, 'rb') as csvfile: CSVreader = csv.reader(csvfile, delimiter=',', quotechar='|') for row in CSVreader: if len(row) == 2: if row[0]+".wav" in fileNames: index = fileNames.index(row[0]+".wav") curRegressionLabels[index] = float(row[1]) regressionLabels.append(curRegressionLabels) # curRegressionLabels is the list of values for the current regression problem regressionNames.append(ntpath.basename(c).replace(".csv", "")) # regression task name if len(features) == 0: print("ERROR: No data found in any input folder!") return numOfFeatures = features.shape[1] # TODO: ARRF WRITE???? # STEP B: Classifier Evaluation and Parameter Selection: if modelType == "svm": modelParams = numpy.array([0.001, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 5.0, 10.0]) elif modelType == "randomforest": modelParams = numpy.array([5, 10, 25, 50, 100]) # elif modelType == "knn": # modelParams = numpy.array([1, 3, 5, 7, 9, 11, 13, 15]); for iRegression, r in enumerate(regressionNames): # get optimal classifeir parameter: print("Regression task " + r) bestParam = evaluateRegression(features, regressionLabels[iRegression], 100, modelType, modelParams) print("Selected params: {0:.5f}".format(bestParam)) [featuresNorm, MEAN, STD] = normalizeFeatures([features]) # normalize features # STEP C: Save the model to file if modelType == "svm": Classifier, _ = trainSVMregression(featuresNorm[0], regressionLabels[iRegression], bestParam) with open(modelName + "_" + r, 'wb') as fid: # save to file pickle.dump(Classifier, fid) fo = open(modelName + "_" + r + "MEANS", "wb") pickle.dump(MEAN, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(STD, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(mtStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stWin, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(stStep, fo, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(computeBEAT, fo, protocol=pickle.HIGHEST_PROTOCOL) fo.close() ''' TODO elif modelType == "randomforest": Classifier, _ = trainRandomForestRegression(featuresNorm[0], regressionLabels[iRegression], bestParam) with open(modelName + "_" + r, 'wb') as fid: # save to file cPickle.dump(Classifier, fid) fo = open(modelName + "_" + r + "MEANS", "wb") cPickle.dump(MEAN, fo, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.dump(STD, fo, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.dump(mtWin, fo, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.dump(mtStep, fo, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.dump(stWin, fo, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.dump(stStep, fo, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.dump(computeBEAT, fo, protocol=cPickle.HIGHEST_PROTOCOL) fo.close() ''' # elif classifierType == "knn": def loadKNNModel(kNNModelName, isRegression=False): try: fo = open(kNNModelName, "rb") except IOError: print("didn't find file") return try: X = pickle.load(fo) Y = pickle.load(fo) MEAN = pickle.load(fo) STD = pickle.load(fo) if not isRegression: classNames = pickle.load(fo) K = pickle.load(fo) mtWin = pickle.load(fo) mtStep = pickle.load(fo) stWin = pickle.load(fo) stStep = pickle.load(fo) computeBEAT = pickle.load(fo) except: fo.close() fo.close() X = numpy.array(X) Y = numpy.array(Y) MEAN = numpy.array(MEAN) STD = numpy.array(STD) Classifier = kNN(X, Y, K) # Note: a direct call to the kNN constructor is used here if isRegression: return(Classifier, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT) else: return(Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT) def loadSVModel(SVMmodelName, isRegression=False): ''' This function loads an SVM model either for classification or training. ARGMUMENTS: - SVMmodelName: the path of the model to be loaded - isRegression: a flag indigating whereas this model is regression or not ''' try: fo = open(SVMmodelName+"MEANS", "rb") except IOError: print("Load SVM Model: Didn't find file") return try: MEAN = pickle.load(fo) STD = pickle.load(fo) if not isRegression: classNames = pickle.load(fo) mtWin = pickle.load(fo) mtStep = pickle.load(fo) stWin = pickle.load(fo) stStep = pickle.load(fo) computeBEAT = pickle.load(fo) except: fo.close() fo.close() MEAN = numpy.array(MEAN) STD = numpy.array(STD) COEFF = [] with open(SVMmodelName, 'rb') as fid: SVM = pickle.load(fid) if isRegression: return(SVM, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT) else: return(SVM, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT) def loadRandomForestModel(RFmodelName, isRegression=False): ''' This function loads an SVM model either for classification or training. ARGMUMENTS: - SVMmodelName: the path of the model to be loaded - isRegression: a flag indigating whereas this model is regression or not ''' try: fo = open(RFmodelName+"MEANS", "rb") except IOError: print("Load Random Forest Model: Didn't find file") return try: MEAN = pickle.load(fo) STD = pickle.load(fo) if not isRegression: classNames = pickle.load(fo) mtWin = pickle.load(fo) mtStep = pickle.load(fo) stWin = pickle.load(fo) stStep = pickle.load(fo) computeBEAT = pickle.load(fo) except: fo.close() fo.close() MEAN = numpy.array(MEAN) STD = numpy.array(STD) COEFF = [] with open(RFmodelName, 'rb') as fid: RF = pickle.load(fid) if isRegression: return(RF, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT) else: return(RF, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT) def loadGradientBoostingModel(GBModelName, isRegression=False): ''' This function loads gradient boosting either for classification or training. ARGMUMENTS: - SVMmodelName: the path of the model to be loaded - isRegression: a flag indigating whereas this model is regression or not ''' try: fo = open(GBModelName+"MEANS", "rb") except IOError: print("Load Random Forest Model: Didn't find file") return try: MEAN = pickle.load(fo) STD = pickle.load(fo) if not isRegression: classNames = pickle.load(fo) mtWin = pickle.load(fo) mtStep = pickle.load(fo) stWin = pickle.load(fo) stStep = pickle.load(fo) computeBEAT = pickle.load(fo) except: fo.close() fo.close() MEAN = numpy.array(MEAN) STD = numpy.array(STD) COEFF = [] with open(GBModelName, 'rb') as fid: GB = pickle.load(fid) if isRegression: return(GB, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT) else: return(GB, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT) def loadExtraTreesModel(ETmodelName, isRegression=False): ''' This function loads extra trees either for classification or training. ARGMUMENTS: - SVMmodelName: the path of the model to be loaded - isRegression: a flag indigating whereas this model is regression or not ''' try: fo = open(ETmodelName+"MEANS", "rb") except IOError: print("Load Random Forest Model: Didn't find file") return try: MEAN = pickle.load(fo) STD = pickle.load(fo) if not isRegression: classNames = pickle.load(fo) mtWin = pickle.load(fo) mtStep = pickle.load(fo) stWin = pickle.load(fo) stStep = pickle.load(fo) computeBEAT = pickle.load(fo) except: fo.close() fo.close() MEAN = numpy.array(MEAN) STD = numpy.array(STD) COEFF = [] with open(ETmodelName, 'rb') as fid: GB = pickle.load(fid) if isRegression: return(GB, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT) else: return(GB, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT) def evaluateClassifier(features, ClassNames, nExp, ClassifierName, Params, parameterMode, perTrain=0.90): ''' ARGUMENTS: features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. each matrix features[i] of class i is [numOfSamples x numOfDimensions] ClassNames: list of class names (strings) nExp: number of cross-validation experiments ClassifierName: svm or knn or randomforest Params: list of classifier parameters (for parameter tuning during cross-validation) parameterMode: 0: choose parameters that lead to maximum overall classification ACCURACY 1: choose parameters that lead to maximum overall F1 MEASURE RETURNS: bestParam: the value of the input parameter that optimizes the selected performance measure ''' # feature normalization: (featuresNorm, MEAN, STD) = normalizeFeatures(features) #featuresNorm = features; nClasses = len(features) CAll = [] acAll = [] F1All = [] PrecisionClassesAll = [] RecallClassesAll = [] ClassesAll = [] F1ClassesAll = [] CMsAll = [] # compute total number of samples: nSamplesTotal = 0 for f in features: nSamplesTotal += f.shape[0] if nSamplesTotal > 1000 and nExp > 50: nExp = 50 print("Number of training experiments changed to 50 due to high number of samples") if nSamplesTotal > 2000 and nExp > 10: nExp = 10 print("Number of training experiments changed to 10 due to high number of samples") for Ci, C in enumerate(Params): # for each param value CM = numpy.zeros((nClasses, nClasses)) for e in range(nExp): # for each cross-validation iteration: print("Param = {0:.5f} - Classifier Evaluation Experiment {1:d} of {2:d}".format(C, e+1, nExp)) # split features: featuresTrain, featuresTest = randSplitFeatures(featuresNorm, perTrain) # train multi-class svms: if ClassifierName == "svm": Classifier = trainSVM(featuresTrain, C) elif ClassifierName == "knn": Classifier = trainKNN(featuresTrain, C) elif ClassifierName == "randomforest": Classifier = trainRandomForest(featuresTrain, C) elif ClassifierName == "gradientboosting": Classifier = trainGradientBoosting(featuresTrain, C) elif ClassifierName == "extratrees": Classifier = trainExtraTrees(featuresTrain, C) CMt = numpy.zeros((nClasses, nClasses)) for c1 in range(nClasses): #Results = Classifier.pred(featuresTest[c1]) nTestSamples = len(featuresTest[c1]) Results = numpy.zeros((nTestSamples, 1)) for ss in range(nTestSamples): [Results[ss], _] = classifierWrapper(Classifier, ClassifierName, featuresTest[c1][ss]) for c2 in range(nClasses): CMt[c1][c2] = float(len(numpy.nonzero(Results == c2)[0])) CM = CM + CMt CM = CM + 0.0000000010 Rec = numpy.zeros((CM.shape[0], )) Pre = numpy.zeros((CM.shape[0], )) for ci in range(CM.shape[0]): Rec[ci] = CM[ci, ci] / numpy.sum(CM[ci, :]) Pre[ci] = CM[ci, ci] / numpy.sum(CM[:, ci]) PrecisionClassesAll.append(Pre) RecallClassesAll.append(Rec) F1 = 2 * Rec * Pre / (Rec + Pre) F1ClassesAll.append(F1) acAll.append(numpy.sum(numpy.diagonal(CM)) / numpy.sum(CM)) CMsAll.append(CM) F1All.append(numpy.mean(F1)) # print "{0:6.4f}{1:6.4f}{2:6.1f}{3:6.1f}".format(nu, g, 100.0*acAll[-1], 100.0*F1All[-1]) print(("\t\t"), end=' ') for i, c in enumerate(ClassNames): if i == len(ClassNames)-1: print("{0:s}\t\t".format(c), end=' ') else: print("{0:s}\t\t\t".format(c), end=' ') print ("OVERALL") print(("\tC"), end=' ') for c in ClassNames: print("\tPRE\tREC\tF1", end=' ') print("\t{0:s}\t{1:s}".format("ACC", "F1")) bestAcInd = numpy.argmax(acAll) bestF1Ind = numpy.argmax(F1All) for i in range(len(PrecisionClassesAll)): print("\t{0:.3f}".format(Params[i]), end=' ') for c in range(len(PrecisionClassesAll[i])): print("\t{0:.1f}\t{1:.1f}\t{2:.1f}".format(100.0 * PrecisionClassesAll[i][c], 100.0 * RecallClassesAll[i][c], 100.0 * F1ClassesAll[i][c]), end=' ') print("\t{0:.1f}\t{1:.1f}".format(100.0 * acAll[i], 100.0 * F1All[i]), end=' ') if i == bestF1Ind: print("\t best F1", end=' ') if i == bestAcInd: print("\t best Acc", end=' ') print() if parameterMode == 0: # keep parameters that maximize overall classification accuracy: print("Confusion Matrix:") printConfusionMatrix(CMsAll[bestAcInd], ClassNames) return Params[bestAcInd] elif parameterMode == 1: # keep parameters that maximize overall F1 measure: print("Confusion Matrix:") printConfusionMatrix(CMsAll[bestF1Ind], ClassNames) return Params[bestF1Ind] def evaluateRegression(features, labels, nExp, MethodName, Params): ''' ARGUMENTS: features: numpy matrices of features [numOfSamples x numOfDimensions] labels: list of sample labels nExp: number of cross-validation experiments MethodName: "svm" or "randomforest" Params: list of classifier params to be evaluated RETURNS: bestParam: the value of the input parameter that optimizes the selected performance measure ''' # feature normalization: (featuresNorm, MEAN, STD) = normalizeFeatures([features]) featuresNorm = featuresNorm[0] nSamples = labels.shape[0] partTrain = 0.9 ErrorsAll = [] ErrorsTrainAll = [] ErrorsBaselineAll = [] for Ci, C in enumerate(Params): # for each param value Errors = [] ErrorsTrain = [] ErrorsBaseline = [] for e in range(nExp): # for each cross-validation iteration: # split features: randperm = numpy.random.permutation(list(range(nSamples))) nTrain = int(round(partTrain * nSamples)) featuresTrain = [featuresNorm[randperm[i]] for i in range(nTrain)] featuresTest = [featuresNorm[randperm[i+nTrain]] for i in range(nSamples - nTrain)] labelsTrain = [labels[randperm[i]] for i in range(nTrain)] labelsTest = [labels[randperm[i + nTrain]] for i in range(nSamples - nTrain)] # train multi-class svms: featuresTrain = numpy.matrix(featuresTrain) if MethodName == "svm": [Classifier, trainError] = trainSVMregression(featuresTrain, labelsTrain, C) # TODO #elif MethodName == "randomforest": # [Classifier, trainError] = trainRandomForestRegression(featuresTrain, labelsTrain, C) # TODO KNN # elif ClassifierName=="knn": # Classifier = trainKNN(featuresTrain, C) ErrorTest = [] ErrorTestBaseline = [] for itest, fTest in enumerate(featuresTest): R = regressionWrapper(Classifier, MethodName, fTest) Rbaseline = numpy.mean(labelsTrain) ErrorTest.append((R - labelsTest[itest]) * (R - labelsTest[itest])) ErrorTestBaseline.append((Rbaseline - labelsTest[itest]) * (Rbaseline - labelsTest[itest])) Error = numpy.array(ErrorTest).mean() ErrorBaseline = numpy.array(ErrorTestBaseline).mean() Errors.append(Error) ErrorsTrain.append(trainError) ErrorsBaseline.append(ErrorBaseline) ErrorsAll.append(numpy.array(Errors).mean()) ErrorsTrainAll.append(numpy.array(ErrorsTrain).mean()) ErrorsBaselineAll.append(numpy.array(ErrorsBaseline).mean()) bestInd = numpy.argmin(ErrorsAll) print("{0:s}\t\t{1:s}\t\t{2:s}\t\t{3:s}".format("Param", "MSE", "T-MSE", "R-MSE")) for i in range(len(ErrorsAll)): print("{0:.4f}\t\t{1:.2f}\t\t{2:.2f}\t\t{3:.2f}".format(Params[i], ErrorsAll[i], ErrorsTrainAll[i], ErrorsBaselineAll[i]), end=' ') if i == bestInd: print("\t\t best", end=' ') print() return Params[bestInd] def printConfusionMatrix(CM, ClassNames): ''' This function prints a confusion matrix for a particular classification task. ARGUMENTS: CM: a 2-D numpy array of the confusion matrix (CM[i,j] is the number of times a sample from class i was classified in class j) ClassNames: a list that contains the names of the classes ''' if CM.shape[0] != len(ClassNames): print("printConfusionMatrix: Wrong argument sizes\n") return for c in ClassNames: if len(c) > 4: c = c[0:3] print("\t{0:s}".format(c), end=' ') print() for i, c in enumerate(ClassNames): if len(c) > 4: c = c[0:3] print("{0:s}".format(c), end=' ') for j in range(len(ClassNames)): print("\t{0:.1f}".format(100.0 * CM[i][j] / numpy.sum(CM)), end=' ') print() def normalizeFeatures(features): ''' This function normalizes a feature set to 0-mean and 1-std. Used in most classifier trainning cases. ARGUMENTS: - features: list of feature matrices (each one of them is a numpy matrix) RETURNS: - featuresNorm: list of NORMALIZED feature matrices - MEAN: mean vector - STD: std vector ''' X = numpy.array([]) for count, f in enumerate(features): if f.shape[0] > 0: if count == 0: X = f else: X = numpy.vstack((X, f)) count += 1 MEAN = numpy.mean(X, axis=0) STD = numpy.std(X, axis=0) featuresNorm = [] for f in features: ft = f.copy() for nSamples in range(f.shape[0]): ft[nSamples, :] = (ft[nSamples, :] - MEAN) / STD featuresNorm.append(ft) return (featuresNorm, MEAN, STD) def listOfFeatures2Matrix(features): ''' listOfFeatures2Matrix(features) This function takes a list of feature matrices as argument and returns a single concatenated feature matrix and the respective class labels. ARGUMENTS: - features: a list of feature matrices RETURNS: - X: a concatenated matrix of features - Y: a vector of class indeces ''' X = numpy.array([]) Y = numpy.array([]) for i, f in enumerate(features): if i == 0: X = f Y = i * numpy.ones((len(f), 1)) else: X = numpy.vstack((X, f)) Y = numpy.append(Y, i * numpy.ones((len(f), 1))) return (X, Y) def pcaDimRed(features, nDims): [X, Y] = listOfFeatures2Matrix(features) pca = sklearn.decomposition.PCA(n_components = nDims) pca.fit(X) coeff = pca.components_ coeff = coeff[:, 0:nDims] featuresNew = [] for f in features: ft = f.copy() # ft = pca.transform(ft, k=nDims) ft = numpy.dot(f, coeff) featuresNew.append(ft) return (featuresNew, coeff) def fileClassification(inputFile, modelName, modelType): # Load classifier: if not os.path.isfile(inputFile): print("fileClassification: wav file not found!") return (-1, -1, -1) [Fs, x] = audioBasicIO.readAudioFile(inputFile) # read audio file and convert to mono x = audioBasicIO.stereo2mono(x) return fragmentClassification(Fs, x, modelName, modelType) def fragmentClassification(Fs, x, modelName, modelType): if not os.path.isfile(modelName): print("fileClassification: input modelName not found!") return (-1, -1, -1) if modelType == 'svm': [Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadSVModel(modelName) elif modelType == 'knn': [Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadKNNModel(modelName) elif modelType == 'randomforest': [Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadRandomForestModel(modelName) elif modelType == 'gradientboosting': [Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadGradientBoostingModel(modelName) elif modelType == 'extratrees': [Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadExtraTreesModel(modelName) # feature extraction: [MidTermFeatures, s] = aF.mtFeatureExtraction(x, Fs, mtWin * Fs, mtStep * Fs, round(Fs * stWin), round(Fs * stStep)) MidTermFeatures = MidTermFeatures.mean(axis=1) # long term averaging of mid-term statistics if computeBEAT: [beat, beatConf] = aF.beatExtraction(s, stStep) MidTermFeatures = numpy.append(MidTermFeatures, beat) MidTermFeatures = numpy.append(MidTermFeatures, beatConf) curFV = (MidTermFeatures - MEAN) / STD # normalization [Result, P] = classifierWrapper(Classifier, modelType, curFV) # classification return Result, P, classNames def fileRegression(inputFile, modelName, modelType): # Load classifier: if not os.path.isfile(inputFile): print("fileClassification: wav file not found!") return (-1, -1, -1) regressionModels = glob.glob(modelName + "_*") regressionModels2 = [] for r in regressionModels: if r[-5::] != "MEANS": regressionModels2.append(r) regressionModels = regressionModels2 regressionNames = [] for r in regressionModels: regressionNames.append(r[r.rfind("_")+1::]) # FEATURE EXTRACTION # LOAD ONLY THE FIRST MODEL (for mtWin, etc) if modelType == 'svm': [_, _, _, mtWin, mtStep, stWin, stStep, computeBEAT] = loadSVModel(regressionModels[0], True) elif modelType == 'knn': [_, _, _, mtWin, mtStep, stWin, stStep, computeBEAT] = loadKNNModel(regressionModels[0], True) [Fs, x] = audioBasicIO.readAudioFile(inputFile) # read audio file and convert to mono x = audioBasicIO.stereo2mono(x) # feature extraction: [MidTermFeatures, s] = aF.mtFeatureExtraction(x, Fs, mtWin * Fs, mtStep * Fs, round(Fs * stWin), round(Fs * stStep)) MidTermFeatures = MidTermFeatures.mean(axis=1) # long term averaging of mid-term statistics if computeBEAT: [beat, beatConf] = aF.beatExtraction(s, stStep) MidTermFeatures = numpy.append(MidTermFeatures, beat) MidTermFeatures = numpy.append(MidTermFeatures, beatConf) # REGRESSION R = [] for ir, r in enumerate(regressionModels): if not os.path.isfile(r): print("fileClassification: input modelName not found!") return (-1, -1, -1) if modelType == 'svm': [Model, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT] = loadSVModel(r, True) elif modelType == 'knn': [Model, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT] = loadKNNModel(r, True) curFV = (MidTermFeatures - MEAN) / STD # normalization R.append(regressionWrapper(Model, modelType, curFV)) # classification return R, regressionNames def lda(data, labels, redDim): # Centre data data -= data.mean(axis=0) nData = numpy.shape(data)[0] nDim = numpy.shape(data)[1] print(nData, nDim) Sw = numpy.zeros((nDim, nDim)) Sb = numpy.zeros((nDim, nDim)) C = numpy.cov((data.T)) # Loop over classes classes = numpy.unique(labels) for i in range(len(classes)): # Find relevant datapoints indices = (numpy.where(labels == classes[i])) d = numpy.squeeze(data[indices, :]) classcov = numpy.cov((d.T)) Sw += float(numpy.shape(indices)[0])/nData * classcov Sb = C - Sw # Now solve for W # Compute eigenvalues, eigenvectors and sort into order #evals,evecs = linalg.eig(dot(linalg.pinv(Sw),sqrt(Sb))) evals, evecs = la.eig(Sw, Sb) indices = numpy.argsort(evals) indices = indices[::-1] evecs = evecs[:, indices] evals = evals[indices] w = evecs[:, :redDim] #print evals, w newData = numpy.dot(data, w) #for i in range(newData.shape[0]): # plt.text(newData[i,0],newData[i,1],str(labels[i])) #plt.xlim([newData[:,0].min(), newData[:,0].max()]) #plt.ylim([newData[:,1].min(), newData[:,1].max()]) #plt.show() return newData, w def writeTrainDataToARFF(modelName, features, classNames, featureNames): f = open(modelName + ".arff", 'w') f.write('@RELATION ' + modelName + '\n') for fn in featureNames: f.write('@ATTRIBUTE ' + fn + ' NUMERIC\n') f.write('@ATTRIBUTE class {') for c in range(len(classNames)-1): f.write(classNames[c] + ',') f.write(classNames[-1] + '}\n\n') f.write('@DATA\n') for c, fe in enumerate(features): for i in range(fe.shape[0]): for j in range(fe.shape[1]): f.write("{0:f},".format(fe[i, j])) f.write(classNames[c]+"\n") f.close() def trainSpeakerModelsScript(): ''' This script is used to train the speaker-related models (NOTE: data paths are hard-coded and NOT included in the library, the models are, however included) import audioTrainTest as aT aT.trainSpeakerModelsScript() ''' mtWin = 2.0 mtStep = 2.0 stWin = 0.020 stStep = 0.020 dirName = "DIARIZATION_ALL/all" listOfDirs = [os.path.join(dirName, name) for name in os.listdir(dirName) if os.path.isdir(os.path.join(dirName, name))] featureAndTrain(listOfDirs, mtWin, mtStep, stWin, stStep, "knn", "data/knnSpeakerAll", computeBEAT=False, perTrain=0.50) dirName = "DIARIZATION_ALL/female_male" listOfDirs = [os.path.join(dirName, name) for name in os.listdir(dirName) if os.path.isdir(os.path.join(dirName, name))] featureAndTrain(listOfDirs, mtWin, mtStep, stWin, stStep, "knn", "data/knnSpeakerFemaleMale", computeBEAT=False, perTrain=0.50) def main(argv): return 0 if __name__ == '__main__': main(sys.argv)
mit
mrcslws/htmresearch
projects/thing_classification/thing_convergence.py
3
13625
# Numenta Platform for Intelligent Computing (NuPIC) # Copyright (C) 2016, Numenta, Inc. Unless you have an agreement # with Numenta, Inc., for a separate license for this software code, the # following terms and conditions apply: # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero Public License version 3 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Affero Public License for more details. # # You should have received a copy of the GNU Affero Public License # along with this program. If not, see http://www.gnu.org/licenses. # # http://numenta.org/licenses/ # ---------------------------------------------------------------------- """ This file is used to run Thing experiments using simulated sensations. """ import random import os from math import ceil import numpy as np import pprint import matplotlib.pyplot as plt from sklearn import manifold, random_projection from htmresearch.frameworks.layers.l2_l4_inference import ( L4L2Experiment, rerunExperimentFromLogfile) from htmresearch.frameworks.layers.object_machine_factory import ( createObjectMachine ) def getL4Params(): """ Returns a good default set of parameters to use in the L4 region. """ return { "columnCount": 256, "cellsPerColumn": 16, "learn": True, "learnOnOneCell": False, "initialPermanence": 0.51, "connectedPermanence": 0.6, "permanenceIncrement": 0.1, "permanenceDecrement": 0.01, "minThreshold": 19, "predictedSegmentDecrement": 0.0, "activationThreshold": 19, "sampleSize": 20, "implementation": "etm", } def getL2Params(): """ Returns a good default set of parameters to use in the L4 region. """ return { "inputWidth": 256 * 16, "cellCount": 4096, "sdrSize": 40, "synPermProximalInc": 0.5, "synPermProximalDec": 0.0, "initialProximalPermanence": 0.6, "minThresholdProximal": 9, "sampleSizeProximal": 10, "connectedPermanenceProximal": 0.5, "synPermDistalInc": 0.1, "synPermDistalDec": 0.001, "initialDistalPermanence": 0.41, "activationThresholdDistal": 13, "sampleSizeDistal": 30, "connectedPermanenceDistal": 0.5, "distalSegmentInhibitionFactor": 1.001, "learningMode": True, } def locateConvergencePoint(stats, minOverlap, maxOverlap): """ Walk backwards through stats until you locate the first point that diverges from target overlap values. We need this to handle cases where it might get to target values, diverge, and then get back again. We want the last convergence point. """ for i,v in enumerate(stats[::-1]): if not (v >= minOverlap and v <= maxOverlap): return len(stats)-i + 1 # Never differs - converged in one iteration return 1 def averageConvergencePoint(inferenceStats, prefix, minOverlap, maxOverlap, settlingTime): """ inferenceStats contains activity traces while the system visits each object. Given the i'th object, inferenceStats[i] contains activity statistics for each column for each region for the entire sequence of sensations. For each object, compute the convergence time - the first point when all L2 columns have converged. Return the average convergence time across all objects. Given inference statistics for a bunch of runs, locate all traces with the given prefix. For each trace locate the iteration where it finally settles on targetValue. Return the average settling iteration across all runs. """ convergenceSum = 0.0 # For each object for stats in inferenceStats: # For each L2 column locate convergence time convergencePoint = 0.0 for key in stats.iterkeys(): if prefix in key: columnConvergence = locateConvergencePoint( stats[key], minOverlap, maxOverlap) # Ensure this column has converged by the last iteration # assert(columnConvergence <= len(stats[key])) convergencePoint = max(convergencePoint, columnConvergence) convergenceSum += ceil(float(convergencePoint)/settlingTime) return convergenceSum/len(inferenceStats) def loadThingObjects(numCorticalColumns=1, objDataPath='./data/'): """ Load simulated sensation data on a number of different objects There is one file per object, each row contains one feature, location pairs The format is as follows [(-33.6705, 75.5003, 2.4207)/10] => [[list of active bits of location], [list of active bits of feature]] The content before "=>" is the true 3D location / sensation The number of active bits in the location and feature is listed after "=>". @return A simple object machine """ # create empty simple object machine objects = createObjectMachine( machineType="simple", numInputBits=20, sensorInputSize=1024, externalInputSize=1024, numCorticalColumns=numCorticalColumns, numFeatures=0, numLocations=0, ) for _ in range(numCorticalColumns): objects.locations.append([]) objects.features.append([]) objFiles = [] for f in os.listdir(objDataPath): if os.path.isfile(os.path.join(objDataPath, f)): if '.log' in f: objFiles.append(f) idx = 0 OnBitsList = [] for f in objFiles: objName = f.split('.')[0] objName = objName[4:] objFile = open('{}/{}'.format(objDataPath, f)) sensationList = [] for line in objFile.readlines(): # parse thing data file and extract feature/location vectors sense = line.split('=>')[1].strip(' ').strip('\n') OnBitsList.append(float(line.split('] =>')[0].split('/')[1])) location = sense.split('],[')[0].strip('[') feature = sense.split('],[')[1].strip(']') location = np.fromstring(location, sep=',', dtype=np.uint8) feature = np.fromstring(feature, sep=',', dtype=np.uint8) # add the current sensation to object Machine sensationList.append((idx, idx)) for c in range(numCorticalColumns): objects.locations[c].append(set(location.tolist())) objects.features[c].append(set(feature.tolist())) idx += 1 objects.addObject(sensationList, objName) print "load object file: {} object name: {} sensation # {}".format( f, objName, len(sensationList)) OnBitsList OnBitsList = np.array(OnBitsList) plt.figure() plt.hist(OnBitsList) return objects, OnBitsList def trainNetwork(objects, numColumns, l4Params, l2Params, verbose=False): print " Training sensorimotor network ..." objectNames = objects.objects.keys() numObjects = len(objectNames) exp = L4L2Experiment("shared_features", L2Overrides=l2Params, L4Overrides=l4Params, numCorticalColumns=numColumns) exp.learnObjects(objects.provideObjectsToLearn()) settlingTime = 1 L2Representations = exp.objectL2Representations # if verbose: # print "Learned object representations:" # pprint.pprint(L2Representations, width=400) # print "==========================" # For inference, we will check and plot convergence for each object. For each # object, we create a sequence of random sensations for each column. We will # present each sensation for settlingTime time steps to let it settle and # ensure it converges. maxSensationNumber = 30 overlapMat = np.zeros((numObjects, numObjects, maxSensationNumber)) numL2ActiveCells = np.zeros((numObjects, maxSensationNumber)) for objectIdx in range(numObjects): objectId = objectNames[objectIdx] obj = objects[objectId] # Create sequence of sensations for this object for one column. The total # number of sensations is equal to the number of points on the object. No # point should be visited more than once. objectCopy = [pair for pair in obj] random.shuffle(objectCopy) exp.sendReset() for sensationNumber in range(maxSensationNumber): objectSensations = {} for c in range(numColumns): objectSensations[c] = [] if sensationNumber >= len(objectCopy): pair = objectCopy[-1] else: pair = objectCopy[sensationNumber] if numColumns > 1: raise NotImplementedError else: # stay multiple steps on each sensation for _ in xrange(settlingTime): objectSensations[0].append(pair) inferConfig = { "object": objectId, "numSteps": len(objectSensations[0]), "pairs": objectSensations, "includeRandomLocation": False, } inferenceSDRs = objects.provideObjectToInfer(inferConfig) exp.infer(inferenceSDRs, objectName=objectId, reset=False) for i in range(numObjects): overlapMat[objectIdx, i, sensationNumber] = len( exp.getL2Representations()[0] & L2Representations[objects.objects.keys()[i]][0]) # if verbose: # print "Intersection with {}:{}".format( # objectNames[i], overlapMat[objectIdx, i]) for c in range(numColumns): numL2ActiveCells[objectIdx, sensationNumber] += len( exp.getL2Representations()[c]) print "{} # L2 active cells {}: ".format(sensationNumber, numL2ActiveCells[ objectIdx, sensationNumber]) if verbose: print "Output for {}: {}".format(objectId, exp.getL2Representations()) print "Final L2 active cells {}: ".format( numL2ActiveCells[objectIdx, sensationNumber]) print exp.sendReset() expResult = {'overlapMat': overlapMat, 'numL2ActiveCells': numL2ActiveCells} return expResult def computeAccuracy(expResult, objects): objectNames = objects.objects.keys() overlapMat = expResult['overlapMat'][:, :, -1] numL2ActiveCells = expResult['numL2ActiveCells'][:, -1] numCorrect = 0 numObjects = overlapMat.shape[0] numFound = 0 percentOverlap = np.zeros(overlapMat.shape) for i in range(numObjects): for j in range(i, numObjects): percentOverlap[i, j] = overlapMat[i, j] # / np.min([numL2ActiveCells[i], numL2ActiveCells[j]]) objectNames = np.array(objectNames) for i in range(numObjects): # idx = np.where(overlapMat[i, :]>confuseThresh)[0] idx = np.where(percentOverlap[i, :] == np.max(percentOverlap[i, :]))[0] print " {}, # sensations {}, best match is {}".format( objectNames[i], len(objects[objectNames[i]]), objectNames[idx]) found = len(np.where(idx == i)[0]) > 0 numFound += found if not found: print "<=========== {} was not detected ! ===========>".format(objectNames[i]) if len(idx) > 1: continue if idx[0] == i: numCorrect += 1 accuracy = float(numCorrect)/numObjects numPerfect = len(np.where(numL2ActiveCells<=40)[0]) print "accuracy: {} ({}/{}) ".format(accuracy, numCorrect, numObjects) print "perfect retrival ratio: {} ({}/{}) ".format( float(numPerfect)/numObjects, numPerfect, numObjects) print "Object detection ratio {}/{} ".format(numFound, numObjects) return accuracy def runExperimentAccuracyVsL4Thresh(): accuracyVsThresh = [] threshList = np.arange(13, 20) for thresh in threshList: numColumns = 1 l2Params = getL2Params() l4Params = getL4Params() l4Params['minThreshold'] = thresh l4Params['activationThreshold'] = thresh objects = loadThingObjects(1, './data') expResult = trainNetwork(objects, numColumns, l4Params, l2Params, True) accuracy = computeAccuracy(expResult, objects) accuracyVsThresh.append(accuracy) plt.figure() plt.plot(threshList, accuracyVsThresh, '-o') plt.xlabel('L4 distal Threshold') plt.ylabel('Classification Accuracy') plt.savefig('accuracyVsL4Thresh.pdf') return threshList, accuracyVsThresh if __name__ == "__main__": # uncomment to plot accuracy as a function of L4 threshold # threshList, accuracyVsThresh = runExperimentAccuracyVsL4Thresh() numColumns = 1 l2Params = getL2Params() l4Params = getL4Params() verbose = 1 objects, OnBitsList = loadThingObjects(numColumns, './data') expResult = trainNetwork(objects, numColumns, l4Params, l2Params, True) accuracy = computeAccuracy(expResult, objects) objectNames = objects.objects.keys() numObjects = len(objectNames) overlapMat = expResult['overlapMat'] numL2ActiveCells = expResult['numL2ActiveCells'] objectNames = objects.objects.keys() numObjects = len(objectNames) plt.figure() for sensationNumber in range(10): plt.imshow(overlapMat[:, :, sensationNumber]) plt.xticks(range(numObjects), objectNames, rotation='vertical', fontsize=4) plt.yticks(range(numObjects), objectNames, fontsize=4) plt.title('pairwise overlap at step {}'.format(sensationNumber)) plt.xlabel('target representation') plt.ylabel('inferred representation') plt.tight_layout() plt.savefig('plots/overlap_matrix_step_{}.png'.format(sensationNumber)) # plot number of active cells for each object plt.figure() objectNamesSort = [] idx = np.argsort(expResult['numL2ActiveCells'][:, -1]) for i in idx: objectNamesSort.append(objectNames[i]) plt.plot(numL2ActiveCells[idx, -1]) plt.xticks(range(numObjects), objectNamesSort, rotation='vertical', fontsize=5) plt.tight_layout() plt.ylabel('Number of active L2 cells') plt.savefig('plots/number_of_active_l2_cells.pdf') #
agpl-3.0
cajal/pipeline
python/pipeline/utils/galvo_corrections.py
5
13668
""" Utilities for motion and raster correction of resonant scans. """ import numpy as np from scipy import interpolate as interp from scipy import signal from scipy import ndimage from ..exceptions import PipelineException from ..utils.signal import mirrconv def compute_raster_phase(image, temporal_fill_fraction): """ Compute raster correction for bidirectional resonant scanners. It shifts the even and odd rows of the image in the x axis to find the scan angle that aligns them better. Positive raster phase will shift even rows to the right and odd rows to the left (assuming first row is row 0). :param np.array image: The image to be corrected. :param float temporal_fill_fraction: Fraction of time during which the scan is recording a line against the total time per line. :return: An angle (in radians). Estimate of the mismatch angle between the expected initial angle and the one recorded. :rtype: float """ # Make sure image has even number of rows (so number of even and odd rows is the same) image = image[:-1] if image.shape[0] % 2 == 1 else image # Get some params image_height, image_width = image.shape skip_rows = round(image_height * 0.05) # rows near the top or bottom have artifacts skip_cols = round(image_width * 0.10) # so do columns # Create images with even and odd rows even_rows = image[::2][skip_rows: -skip_rows] odd_rows = image[1::2][skip_rows: -skip_rows] # Scan angle at which each pixel was recorded. max_angle = (np.pi / 2) * temporal_fill_fraction scan_angles = np.linspace(-max_angle, max_angle, image_width + 2)[1:-1] #sin_index = np.sin(scan_angles) # Greedy search for the best raster phase: starts at coarse estimates and refines them even_interp = interp.interp1d(scan_angles, even_rows, fill_value='extrapolate') odd_interp = interp.interp1d(scan_angles, odd_rows, fill_value='extrapolate') angle_shift = 0 for scale in [1e-2, 1e-3, 1e-4, 1e-5, 1e-6]: angle_shifts = angle_shift + scale * np.linspace(-9, 9, 19) match_values = [] for new_angle_shift in angle_shifts: shifted_evens = even_interp(scan_angles + new_angle_shift) shifted_odds = odd_interp(scan_angles - new_angle_shift) match_values.append(np.sum(shifted_evens[:, skip_cols: -skip_cols] * shifted_odds[:, skip_cols: -skip_cols])) angle_shift = angle_shifts[np.argmax(match_values)] return angle_shift def compute_motion_shifts(scan, template, in_place=True, num_threads=8): """ Compute shifts in y and x for rigid subpixel motion correction. Returns the number of pixels that each image in the scan was to the right (x_shift) or below (y_shift) the template. Negative shifts mean the image was to the left or above the template. :param np.array scan: 2 or 3-dimensional scan (image_height, image_width[, num_frames]). :param np.array template: 2-d template image. Each frame in scan is aligned to this. :param bool in_place: Whether the scan can be overwritten. :param int num_threads: Number of threads used for the ffts. :returns: (y_shifts, x_shifts) Two arrays (num_frames) with the y, x motion shifts. ..note:: Based in imreg_dft.translation(). """ import pyfftw from imreg_dft import utils # Add third dimension if scan is a single image if scan.ndim == 2: scan = np.expand_dims(scan, -1) # Get some params image_height, image_width, num_frames = scan.shape taper = np.outer(signal.tukey(image_height, 0.2), signal.tukey(image_width, 0.2)) # Prepare fftw frame = pyfftw.empty_aligned((image_height, image_width), dtype='complex64') fft = pyfftw.builders.fft2(frame, threads=num_threads, overwrite_input=in_place, avoid_copy=True) ifft = pyfftw.builders.ifft2(frame, threads=num_threads, overwrite_input=in_place, avoid_copy=True) # Get fourier transform of template template_freq = fft(template * taper).conj() # we only need the conjugate abs_template_freq = abs(template_freq) eps = abs_template_freq.max() * 1e-15 # Compute subpixel shifts per image y_shifts = np.empty(num_frames) x_shifts = np.empty(num_frames) for i in range(num_frames): # Compute correlation via cross power spectrum image_freq = fft(scan[:, :, i] * taper) cross_power = (image_freq * template_freq) / (abs(image_freq) * abs_template_freq + eps) shifted_cross_power = np.fft.fftshift(abs(ifft(cross_power))) # Get best shift shifts = np.unravel_index(np.argmax(shifted_cross_power), shifted_cross_power.shape) shifts = utils._interpolate(shifted_cross_power, shifts, rad=3) # Map back to deviations from center y_shifts[i] = shifts[0] - image_height // 2 x_shifts[i] = shifts[1] - image_width // 2 return y_shifts, x_shifts def fix_outliers(y_shifts, x_shifts, max_y_shift=20, max_x_shift=20, method='median'): """ Look for spikes in motion shifts and set them to a sensible value. Reject any shift whose y or x shift is higher than max_y_shift/max_x_shift pixels from the median/linear estimate/moving average. Outliers filled by interpolating valid points; in the edges filled with the median/linear estimate/moving average. :param np.array y_shifts/x_shifts: Shifts in y, x. :param float max_y_shift/max_x_shifts: Number of pixels used as threshold to classify a point as an outlier in y, x. :param string method: One of 'mean' or 'trend'. 'median': Detect outliers as deviations from the median of the shifts. 'linear': Detect outliers as deviations from a line estimated from the shifts. 'trend': Detect outliers as deviations from the shift trend computed as a moving average over the entire scan. :returns: (y_shifts, x_shifts) Two arrays (num_frames) with the fixed motion shifts. :returns: (outliers) A boolean array (num_frames) with True for outlier frames. """ # Basic checks num_frames = len(y_shifts) if num_frames < 5: return y_shifts, x_shifts, np.full(num_frames, False) # Copy shifts to avoid changing originals y_shifts, x_shifts = y_shifts.copy(), x_shifts.copy() # Detrend shifts if method == 'median': y_trend = np.median(y_shifts) x_trend = np.median(x_shifts) elif method == 'linear': x_trend = _fit_robust_line(x_shifts) y_trend = _fit_robust_line(y_shifts) else: # trend window_size = min(101, num_frames) window_size -= 1 if window_size % 2 == 0 else 0 y_trend = mirrconv(y_shifts, np.ones(window_size) / window_size) x_trend = mirrconv(x_shifts, np.ones(window_size) / window_size) # Subtract trend from shifts y_shifts -= y_trend x_shifts -= x_trend # Get outliers outliers = np.logical_or(abs(y_shifts) > max_y_shift, abs(x_shifts) > max_x_shift) # Interpolate outliers num_outliers = np.sum(outliers) if num_outliers < num_frames - 1: # at least two good points needed for interpolation #indices = np.arange(len(x_shifts)) #y_shifts = np.interp(indices, indices[~outliers], y_shifts[~outliers], left=0, right=0) #x_shifts = np.interp(indices, indices[~outliers], x_shifts[~outliers], left=0, right=0) y_shifts[outliers] = 0 x_shifts[outliers] = 0 else: print('Warning: {} out of {} frames were outliers.'.format(num_outliers, num_frames)) y_shifts = 0 x_shifts = 0 # Add trend back to shifts y_shifts += y_trend x_shifts += x_trend return y_shifts, x_shifts, outliers def _fit_robust_line(shifts): """ Use a robust linear regression algorithm to fit a line to the data.""" from sklearn.linear_model import TheilSenRegressor X = np.arange(len(shifts)).reshape(-1, 1) y = shifts model = TheilSenRegressor() # robust regression model.fit(X, y) line = model.predict(X) return line def correct_raster(scan, raster_phase, temporal_fill_fraction, in_place=True): """ Raster correction for resonant scans. Corrects multi-photon images in n-dimensional scans. Positive raster phase shifts even lines to the left and odd lines to the right. Negative raster phase shifts even lines to the right and odd lines to the left. :param np.array scan: Volume with images to be corrected in the first two dimensions. Works for 2-dimensions and up, usually (image_height, image_width, num_frames). :param float raster_phase: Angle difference between expected and recorded scan angle. :param float temporal_fill_fraction: Ratio between active acquisition and total length of the scan line. :param bool in_place: If True (default), the original array is modified in place. :return: Raster-corrected scan. :rtype: Same as scan if scan.dtype is subtype of np.float, else np.float32. :raises: PipelineException """ # Basic checks if not isinstance(scan, np.ndarray): raise PipelineException('Scan needs to be a numpy array.') if scan.ndim < 2: raise PipelineException('Scan with less than 2 dimensions.') # Assert scan is float if not np.issubdtype(scan.dtype, np.floating): print('Warning: Changing scan type from', str(scan.dtype), 'to np.float32') scan = scan.astype(np.float32, copy=(not in_place)) elif not in_place: scan = scan.copy() # copy it anyway preserving the original float dtype # Get some dimensions original_shape = scan.shape image_height = original_shape[0] image_width = original_shape[1] # Scan angle at which each pixel was recorded. max_angle = (np.pi / 2) * temporal_fill_fraction scan_angles = np.linspace(-max_angle, max_angle, image_width + 2)[1:-1] # We iterate over every image in the scan (first 2 dimensions). Same correction # regardless of what channel, slice or frame they belong to. reshaped_scan = np.reshape(scan, (image_height, image_width, -1)) num_images = reshaped_scan.shape[-1] for i in range(num_images): # Get current image image = reshaped_scan[:, :, i] # Correct even rows of the image (0, 2, ...) interp_function = interp.interp1d(scan_angles, image[::2, :], bounds_error=False, fill_value=0, copy=(not in_place)) reshaped_scan[::2, :, i] = interp_function(scan_angles + raster_phase) # Correct odd rows of the image (1, 3, ...) interp_function = interp.interp1d(scan_angles, image[1::2, :], bounds_error=False, fill_value=0, copy=(not in_place)) reshaped_scan[1::2, :, i] = interp_function(scan_angles - raster_phase) scan = np.reshape(reshaped_scan, original_shape) return scan def correct_motion(scan, x_shifts, y_shifts, in_place=True): """ Motion correction for multi-photon scans. Shifts each image in the scan x_shift pixels to the left and y_shift pixels up. :param np.array scan: Volume with images to be corrected in the first two dimensions. Works for 2-dimensions and up, usually (image_height, image_width, num_frames). :param list/np.array x_shifts: 1-d array with x motion shifts for each image. :param list/np.array y_shifts: 1-d array with x motion shifts for each image. :param bool in_place: If True (default), the original array is modified in place. :return: Motion corrected scan :rtype: Same as scan if scan.dtype is subtype of np.float, else np.float32. :raises: PipelineException """ # Basic checks if not isinstance(scan, np.ndarray): raise PipelineException('Scan needs to be a numpy array.') if scan.ndim < 2: raise PipelineException('Scan with less than 2 dimensions.') if np.ndim(y_shifts) != 1 or np.ndim(x_shifts) != 1: raise PipelineException('Dimension of one or both motion arrays differs from 1.') if len(x_shifts) != len(y_shifts): raise PipelineException('Length of motion arrays differ.') # Assert scan is float (integer precision is not good enough) if not np.issubdtype(scan.dtype, np.floating): print('Warning: Changing scan type from', str(scan.dtype), 'to np.float32') scan = scan.astype(np.float32, copy=(not in_place)) elif not in_place: scan = scan.copy() # copy it anyway preserving the original dtype # Get some dimensions original_shape = scan.shape image_height = original_shape[0] image_width = original_shape[1] # Reshape input (to deal with more than 2-D volumes) reshaped_scan = np.reshape(scan, (image_height, image_width, -1)) if reshaped_scan.shape[-1] != len(x_shifts): raise PipelineException('Scan and motion arrays have different dimensions') # Ignore NaN values (present in some older data) y_clean, x_clean = y_shifts.copy(), x_shifts.copy() y_clean[np.logical_or(np.isnan(y_shifts), np.isnan(x_shifts))] = 0 x_clean[np.logical_or(np.isnan(y_shifts), np.isnan(x_shifts))] = 0 # Shift each frame for i, (y_shift, x_shift) in enumerate(zip(y_clean, x_clean)): image = reshaped_scan[:, :, i].copy() ndimage.interpolation.shift(image, (-y_shift, -x_shift), order=1, output=reshaped_scan[:, :, i]) scan = np.reshape(reshaped_scan, original_shape) return scan
lgpl-3.0
billy-inn/scikit-learn
examples/linear_model/lasso_dense_vs_sparse_data.py
348
1862
""" ============================== Lasso on dense and sparse data ============================== We show that linear_model.Lasso provides the same results for dense and sparse data and that in the case of sparse data the speed is improved. """ print(__doc__) from time import time from scipy import sparse from scipy import linalg from sklearn.datasets.samples_generator import make_regression from sklearn.linear_model import Lasso ############################################################################### # The two Lasso implementations on Dense data print("--- Dense matrices") X, y = make_regression(n_samples=200, n_features=5000, random_state=0) X_sp = sparse.coo_matrix(X) alpha = 1 sparse_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=1000) dense_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=1000) t0 = time() sparse_lasso.fit(X_sp, y) print("Sparse Lasso done in %fs" % (time() - t0)) t0 = time() dense_lasso.fit(X, y) print("Dense Lasso done in %fs" % (time() - t0)) print("Distance between coefficients : %s" % linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_)) ############################################################################### # The two Lasso implementations on Sparse data print("--- Sparse matrices") Xs = X.copy() Xs[Xs < 2.5] = 0.0 Xs = sparse.coo_matrix(Xs) Xs = Xs.tocsc() print("Matrix density : %s %%" % (Xs.nnz / float(X.size) * 100)) alpha = 0.1 sparse_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=10000) dense_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=10000) t0 = time() sparse_lasso.fit(Xs, y) print("Sparse Lasso done in %fs" % (time() - t0)) t0 = time() dense_lasso.fit(Xs.toarray(), y) print("Dense Lasso done in %fs" % (time() - t0)) print("Distance between coefficients : %s" % linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_))
bsd-3-clause
jtwhite79/pyemu
pyemu/utils/gw_utils.py
1
110032
"""MODFLOW support utilities""" import os from datetime import datetime import shutil import warnings import numpy as np import pandas as pd import re pd.options.display.max_colwidth = 100 from pyemu.pst.pst_utils import ( SFMT, IFMT, FFMT, pst_config, parse_tpl_file, try_process_output_file, ) from pyemu.utils.os_utils import run from pyemu.utils.helpers import _write_df_tpl from ..pyemu_warnings import PyemuWarning PP_FMT = { "name": SFMT, "x": FFMT, "y": FFMT, "zone": IFMT, "tpl": SFMT, "parval1": FFMT, } PP_NAMES = ["name", "x", "y", "zone", "parval1"] def modflow_pval_to_template_file(pval_file, tpl_file=None): """write a template file for a modflow parameter value file. Args: pval_file (`str`): the path and name of the existing modflow pval file tpl_file (`str`, optional): template file to write. If None, use `pval_file` +".tpl". Default is None Note: Uses names in the first column in the pval file as par names. Returns: **pandas.DataFrame**: a dataFrame with control file parameter information """ if tpl_file is None: tpl_file = pval_file + ".tpl" pval_df = pd.read_csv( pval_file, delim_whitespace=True, header=None, skiprows=2, names=["parnme", "parval1"], ) pval_df.index = pval_df.parnme pval_df.loc[:, "tpl"] = pval_df.parnme.apply(lambda x: " ~ {0:15s} ~".format(x)) with open(tpl_file, "w") as f: f.write("ptf ~\n#pval template file from pyemu\n") f.write("{0:10d} #NP\n".format(pval_df.shape[0])) f.write( pval_df.loc[:, ["parnme", "tpl"]].to_string( col_space=0, formatters=[SFMT, SFMT], index=False, header=False, justify="left", ) ) return pval_df def modflow_hob_to_instruction_file(hob_file, ins_file=None): """write an instruction file for a modflow head observation file Args: hob_file (`str`): the path and name of the existing modflow hob file ins_file (`str`, optional): the name of the instruction file to write. If `None`, `hob_file` +".ins" is used. Default is `None`. Returns: **pandas.DataFrame**: a dataFrame with control file observation information """ hob_df = pd.read_csv( hob_file, delim_whitespace=True, skiprows=1, header=None, names=["simval", "obsval", "obsnme"], ) hob_df.loc[:, "obsnme"] = hob_df.obsnme.apply(str.lower) hob_df.loc[:, "ins_line"] = hob_df.obsnme.apply(lambda x: "l1 !{0:s}!".format(x)) hob_df.loc[0, "ins_line"] = hob_df.loc[0, "ins_line"].replace("l1", "l2") if ins_file is None: ins_file = hob_file + ".ins" f_ins = open(ins_file, "w") f_ins.write("pif ~\n") f_ins.write( hob_df.loc[:, ["ins_line"]].to_string( col_space=0, columns=["ins_line"], header=False, index=False, formatters=[SFMT], ) + "\n" ) hob_df.loc[:, "weight"] = 1.0 hob_df.loc[:, "obgnme"] = "obgnme" f_ins.close() return hob_df def modflow_hydmod_to_instruction_file(hydmod_file, ins_file=None): """write an instruction file for a modflow hydmod file Args: hydmod_file (`str`): the path and name of the existing modflow hob file ins_file (`str`, optional): the name of the instruction file to write. If `None`, `hydmod_file` +".ins" is used. Default is `None`. Returns: **pandas.DataFrame**: a dataFrame with control file observation information Note: calls `pyemu.gw_utils.modflow_read_hydmod_file()` """ hydmod_df, hydmod_outfile = modflow_read_hydmod_file(hydmod_file) hydmod_df.loc[:, "ins_line"] = hydmod_df.obsnme.apply( lambda x: "l1 w !{0:s}!".format(x) ) if ins_file is None: ins_file = hydmod_outfile + ".ins" with open(ins_file, "w") as f_ins: f_ins.write("pif ~\nl1\n") f_ins.write( hydmod_df.loc[:, ["ins_line"]].to_string( col_space=0, columns=["ins_line"], header=False, index=False, formatters=[SFMT], ) + "\n" ) hydmod_df.loc[:, "weight"] = 1.0 hydmod_df.loc[:, "obgnme"] = "obgnme" df = try_process_output_file(hydmod_outfile + ".ins") if df is not None: df.loc[:, "obsnme"] = df.index.values df.loc[:, "obgnme"] = df.obsnme.apply(lambda x: x[:-9]) df.to_csv("_setup_" + os.path.split(hydmod_outfile)[-1] + ".csv", index=False) return df return hydmod_df def modflow_read_hydmod_file(hydmod_file, hydmod_outfile=None): """read a binary hydmod file and return a dataframe of the results Args: hydmod_file (`str`): The path and name of the existing modflow hydmod binary file hydmod_outfile (`str`, optional): output file to write. If `None`, use `hydmod_file` +".dat". Default is `None`. Returns: **pandas.DataFrame**: a dataFrame with hymod_file values """ try: import flopy.utils as fu except Exception as e: print("flopy is not installed - cannot read {0}\n{1}".format(hydmod_file, e)) return obs = fu.HydmodObs(hydmod_file) hyd_df = obs.get_dataframe() hyd_df.columns = [i[2:] if i.lower() != "totim" else i for i in hyd_df.columns] # hyd_df.loc[:,"datetime"] = hyd_df.index hyd_df["totim"] = hyd_df.index.map(lambda x: x.strftime("%Y%m%d")) hyd_df.rename(columns={"totim": "datestamp"}, inplace=True) # reshape into a single column hyd_df = pd.melt(hyd_df, id_vars="datestamp") hyd_df.rename(columns={"value": "obsval"}, inplace=True) hyd_df["obsnme"] = [ i.lower() + "_" + j.lower() for i, j in zip(hyd_df.variable, hyd_df.datestamp) ] vc = hyd_df.obsnme.value_counts().sort_values() vc = list(vc.loc[vc > 1].index.values) if len(vc) > 0: hyd_df.to_csv("hyd_df.duplciates.csv") obs.get_dataframe().to_csv("hyd_org.duplicates.csv") raise Exception("duplicates in obsnme:{0}".format(vc)) # assert hyd_df.obsnme.value_counts().max() == 1,"duplicates in obsnme" if not hydmod_outfile: hydmod_outfile = hydmod_file + ".dat" hyd_df.to_csv(hydmod_outfile, columns=["obsnme", "obsval"], sep=" ", index=False) # hyd_df = hyd_df[['obsnme','obsval']] return hyd_df[["obsnme", "obsval"]], hydmod_outfile def setup_mtlist_budget_obs( list_filename, gw_filename="mtlist_gw.dat", sw_filename="mtlist_sw.dat", start_datetime="1-1-1970", gw_prefix="gw", sw_prefix="sw", save_setup_file=False, ): """setup observations of gw (and optionally sw) mass budgets from mt3dusgs list file. Args: list_filename (`str`): path and name of existing modflow list file gw_filename (`str`, optional): output filename that will contain the gw budget observations. Default is "mtlist_gw.dat" sw_filename (`str`, optional): output filename that will contain the sw budget observations. Default is "mtlist_sw.dat" start_datetime (`str`, optional): an str that can be parsed into a `pandas.TimeStamp`. used to give budget observations meaningful names. Default is "1-1-1970". gw_prefix (`str`, optional): a prefix to add to the GW budget observations. Useful if processing more than one list file as part of the forward run process. Default is 'gw'. sw_prefix (`str`, optional): a prefix to add to the SW budget observations. Useful if processing more than one list file as part of the forward run process. Default is 'sw'. save_setup_file (`bool`, optional): a flag to save "_setup_"+ `list_filename` +".csv" file that contains useful control file information. Default is `False`. Returns: tuple containing - **str**: the command to add to the forward run script - **str**: the names of the instruction files that were created - **pandas.DataFrame**: a dataframe with information for constructing a control file Note: writes an instruction file and also a _setup_.csv to use when constructing a pest control file The instruction files are named `out_filename` +".ins" It is recommended to use the default value for `gw_filename` or `sw_filename`. This is the companion function of `gw_utils.apply_mtlist_budget_obs()`. """ gw, sw = apply_mtlist_budget_obs( list_filename, gw_filename, sw_filename, start_datetime ) gw_ins = gw_filename + ".ins" _write_mtlist_ins(gw_ins, gw, gw_prefix) ins_files = [gw_ins] df_gw = try_process_output_file(gw_ins, gw_filename) if df_gw is None: raise Exception("error processing groundwater instruction file") if sw is not None: sw_ins = sw_filename + ".ins" _write_mtlist_ins(sw_ins, sw, sw_prefix) ins_files.append(sw_ins) df_sw = try_process_output_file(sw_ins, sw_filename) if df_sw is None: raise Exception("error processing surface water instruction file") df_gw = df_gw.append(df_sw) df_gw.loc[:, "obsnme"] = df_gw.index.values if save_setup_file: df_gw.to_csv("_setup_" + os.path.split(list_filename)[-1] + ".csv", index=False) frun_line = "pyemu.gw_utils.apply_mtlist_budget_obs('{0}')".format(list_filename) return frun_line, ins_files, df_gw def _write_mtlist_ins(ins_filename, df, prefix): """write an instruction file for a MT3D-USGS list file""" try: dt_str = df.index.map(lambda x: x.strftime("%Y%m%d")) except: dt_str = df.index.map(lambda x: "{0:08.1f}".format(x).strip()) with open(ins_filename, "w") as f: f.write("pif ~\nl1\n") for dt in dt_str: f.write("l1 ") for col in df.columns.str.translate( {ord(s): None for s in ["(", ")", "/", "="]} ): if prefix == "": obsnme = "{0}_{1}".format(col, dt) else: obsnme = "{0}_{1}_{2}".format(prefix, col, dt) f.write(" w !{0}!".format(obsnme)) f.write("\n") def apply_mtlist_budget_obs( list_filename, gw_filename="mtlist_gw.dat", sw_filename="mtlist_sw.dat", start_datetime="1-1-1970", ): """process an MT3D-USGS list file to extract mass budget entries. Args: list_filename (`str`): the path and name of an existing MT3D-USGS list file gw_filename (`str`, optional): the name of the output file with gw mass budget information. Default is "mtlist_gw.dat" sw_filename (`str`): the name of the output file with sw mass budget information. Default is "mtlist_sw.dat" start_datatime (`str`): an str that can be cast to a pandas.TimeStamp. Used to give observations a meaningful name Returns: 2-element tuple containing - **pandas.DataFrame**: the gw mass budget dataframe - **pandas.DataFrame**: (optional) the sw mass budget dataframe. If the SFT process is not active, this returned value is `None`. Note: This is the companion function of `gw_utils.setup_mtlist_budget_obs()`. """ try: import flopy except Exception as e: raise Exception("error import flopy: {0}".format(str(e))) mt = flopy.utils.MtListBudget(list_filename) gw, sw = mt.parse(start_datetime=start_datetime, diff=True) gw = gw.drop( [ col for col in gw.columns for drop_col in ["kper", "kstp", "tkstp"] if (col.lower().startswith(drop_col)) ], axis=1, ) gw.to_csv(gw_filename, sep=" ", index_label="datetime", date_format="%Y%m%d") if sw is not None: sw = sw.drop( [ col for col in sw.columns for drop_col in ["kper", "kstp", "tkstp"] if (col.lower().startswith(drop_col)) ], axis=1, ) sw.to_csv(sw_filename, sep=" ", index_label="datetime", date_format="%Y%m%d") return gw, sw def setup_mflist_budget_obs( list_filename, flx_filename="flux.dat", vol_filename="vol.dat", start_datetime="1-1'1970", prefix="", save_setup_file=False, specify_times=None, ): """setup observations of budget volume and flux from modflow list file. Args: list_filename (`str`): path and name of the existing modflow list file flx_filename (`str`, optional): output filename that will contain the budget flux observations. Default is "flux.dat" vol_filename (`str`, optional): output filename that will contain the budget volume observations. Default is "vol.dat" start_datetime (`str`, optional): a string that can be parsed into a pandas.TimeStamp. This is used to give budget observations meaningful names. Default is "1-1-1970". prefix (`str`, optional): a prefix to add to the water budget observations. Useful if processing more than one list file as part of the forward run process. Default is ''. save_setup_file (`bool`): a flag to save "_setup_"+ `list_filename` +".csv" file that contains useful control file information specify_times (`np.ndarray`-like, optional): An array of times to extract from the budget dataframes returned by the flopy MfListBudget(list_filename).get_dataframe() method. This can be useful to ensure consistent observation times for PEST. Array needs to be alignable with index of dataframe return by flopy method, care should be take to ensure that this is the case. If passed will be written to "budget_times.config" file as strings to be read by the companion `apply_mflist_budget_obs()` method at run time. Returns: **pandas.DataFrame**: a dataframe with information for constructing a control file. Note: This method writes instruction files and also a _setup_.csv to use when constructing a pest control file. The instruction files are named <flux_file>.ins and <vol_file>.ins, respectively It is recommended to use the default values for flux_file and vol_file. This is the companion function of `gw_utils.apply_mflist_budget_obs()`. """ flx, vol = apply_mflist_budget_obs( list_filename, flx_filename, vol_filename, start_datetime, times=specify_times ) _write_mflist_ins(flx_filename + ".ins", flx, prefix + "flx") _write_mflist_ins(vol_filename + ".ins", vol, prefix + "vol") df = try_process_output_file(flx_filename + ".ins") if df is None: raise Exception("error processing flux instruction file") df2 = try_process_output_file(vol_filename + ".ins") if df2 is None: raise Exception("error processing volume instruction file") df = df.append(df2) df.loc[:, "obsnme"] = df.index.values if save_setup_file: df.to_csv("_setup_" + os.path.split(list_filename)[-1] + ".csv", index=False) if specify_times is not None: np.savetxt( os.path.join(os.path.dirname(flx_filename), "budget_times.config"), specify_times, fmt="%s", ) return df def apply_mflist_budget_obs( list_filename, flx_filename="flux.dat", vol_filename="vol.dat", start_datetime="1-1-1970", times=None, ): """process a MODFLOW list file to extract flux and volume water budget entries. Args: list_filename (`str`): path and name of the existing modflow list file flx_filename (`str`, optional): output filename that will contain the budget flux observations. Default is "flux.dat" vol_filename (`str`, optional): output filename that will contain the budget volume observations. Default is "vol.dat" start_datetime (`str`, optional): a string that can be parsed into a pandas.TimeStamp. This is used to give budget observations meaningful names. Default is "1-1-1970". times (`np.ndarray`-like or `str`, optional): An array of times to extract from the budget dataframes returned by the flopy MfListBudget(list_filename).get_dataframe() method. This can be useful to ensure consistent observation times for PEST. If type `str`, will assume `times=filename` and attempt to read single vector (no header or index) from file, parsing datetime using pandas. Array needs to be alignable with index of dataframe return by flopy method, care should be take to ensure that this is the case. If setup with `setup_mflist_budget_obs()` specifying `specify_times` argument `times` should be set to "budget_times.config". Note: This is the companion function of `gw_utils.setup_mflist_budget_obs()`. Returns: tuple containing - **pandas.DataFrame**: a dataframe with flux budget information - **pandas.DataFrame**: a dataframe with cumulative budget information """ try: import flopy except Exception as e: raise Exception("error import flopy: {0}".format(str(e))) mlf = flopy.utils.MfListBudget(list_filename) flx, vol = mlf.get_dataframes(start_datetime=start_datetime, diff=True) if times is not None: if isinstance(times, str): if vol.index.tzinfo: parse_date = {"t": [0]} names = [None] else: parse_date = False names = ["t"] times = pd.read_csv( times, header=None, names=names, parse_dates=parse_date )["t"].values flx = flx.loc[times] vol = vol.loc[times] flx.to_csv(flx_filename, sep=" ", index_label="datetime", date_format="%Y%m%d") vol.to_csv(vol_filename, sep=" ", index_label="datetime", date_format="%Y%m%d") return flx, vol def _write_mflist_ins(ins_filename, df, prefix): """write an instruction file for a MODFLOW list file""" dt_str = df.index.map(lambda x: x.strftime("%Y%m%d")) with open(ins_filename, "w") as f: f.write("pif ~\nl1\n") for dt in dt_str: f.write("l1 ") for col in df.columns: obsnme = "{0}_{1}_{2}".format(prefix, col, dt) f.write(" w !{0}!".format(obsnme)) f.write("\n") def setup_hds_timeseries( bin_file, kij_dict, prefix=None, include_path=False, model=None, postprocess_inact=None, text=None, fill=None, precision="single", ): """a function to setup a forward process to extract time-series style values from a binary modflow binary file (or equivalent format - hds, ucn, sub, cbb, etc). Args: bin_file (`str`): path and name of existing modflow binary file - headsave, cell budget and MT3D UCN supported. kij_dict (`dict`): dictionary of site_name: [k,i,j] pairs. For example: `{"wel1":[0,1,1]}`. prefix (`str`, optional): string to prepend to site_name when forming observation names. Default is None include_path (`bool`, optional): flag to setup the binary file processing in directory where the hds_file is located (if different from where python is running). This is useful for setting up the process in separate directory for where python is running. model (`flopy.mbase`, optional): a `flopy.basemodel` instance. If passed, the observation names will have the datetime of the observation appended to them (using the flopy `start_datetime` attribute. If None, the observation names will have the zero-based stress period appended to them. Default is None. postprocess_inact (`float`, optional): Inactive value in heads/ucn file e.g. mt.btn.cinit. If `None`, no inactive value processing happens. Default is `None`. text (`str`): the text record entry in the binary file (e.g. "constant_head"). Used to indicate that the binary file is a MODFLOW cell-by-cell budget file. If None, headsave or MT3D unformatted concentration file is assummed. Default is None fill (`float`): fill value for NaNs in the extracted timeseries dataframe. If `None`, no filling is done, which may yield model run failures as the resulting processed timeseries CSV file (produced at runtime) may have missing values and can't be processed with the cooresponding instruction file. Default is `None`. precision (`str`): the precision of the binary file. Can be "single" or "double". Default is "single". Returns: tuple containing - **str**: the forward run command to execute the binary file process during model runs. - **pandas.DataFrame**: a dataframe of observation information for use in the pest control file Note: This function writes hds_timeseries.config that must be in the same dir where `apply_hds_timeseries()` is called during the forward run Assumes model time units are days This is the companion function of `gw_utils.apply_hds_timeseries()`. """ try: import flopy except Exception as e: print("error importing flopy, returning {0}".format(str(e))) return assert os.path.exists(bin_file), "binary file not found" iscbc = False if text is not None: text = text.upper() try: # hack: if model is passed and its None, it trips up CellBudgetFile... if model is not None: bf = flopy.utils.CellBudgetFile( bin_file, precision=precision, model=model ) iscbc = True else: bf = flopy.utils.CellBudgetFile(bin_file, precision=precision) iscbc = True except Exception as e: try: if model is not None: bf = flopy.utils.HeadFile( bin_file, precision=precision, model=model, text=text ) else: bf = flopy.utils.HeadFile(bin_file, precision=precision, text=text) except Exception as e1: raise Exception( "error instantiating binary file as either CellBudgetFile:{0} or as HeadFile with text arg: {1}".format( str(e), str(e1) ) ) if iscbc: tl = [t.decode().strip() for t in bf.textlist] if text not in tl: raise Exception( "'text' {0} not found in CellBudgetFile.textlist:{1}".format( text, tl ) ) elif bin_file.lower().endswith(".ucn"): try: bf = flopy.utils.UcnFile(bin_file, precision=precision) except Exception as e: raise Exception("error instantiating UcnFile:{0}".format(str(e))) else: try: bf = flopy.utils.HeadFile(bin_file, precision=precision) except Exception as e: raise Exception("error instantiating HeadFile:{0}".format(str(e))) if text is None: text = "none" nlay, nrow, ncol = bf.nlay, bf.nrow, bf.ncol # if include_path: # pth = os.path.join(*[p for p in os.path.split(hds_file)[:-1]]) # config_file = os.path.join(pth,"{0}_timeseries.config".format(hds_file)) # else: config_file = "{0}_timeseries.config".format(bin_file) print("writing config file to {0}".format(config_file)) if fill is None: fill = "none" f_config = open(config_file, "w") if model is not None: if model.dis.itmuni != 4: warnings.warn( "setup_hds_timeseries only supports 'days' time units...", PyemuWarning ) f_config.write( "{0},{1},d,{2},{3},{4},{5}\n".format( os.path.split(bin_file)[-1], model.start_datetime, text, fill, precision, iscbc, ) ) start = pd.to_datetime(model.start_datetime) else: f_config.write( "{0},none,none,{1},{2},{3},{4}\n".format( os.path.split(bin_file)[-1], text, fill, precision, iscbc ) ) f_config.write("site,k,i,j\n") dfs = [] for site, (k, i, j) in kij_dict.items(): assert k >= 0 and k < nlay, k assert i >= 0 and i < nrow, i assert j >= 0 and j < ncol, j site = site.lower().replace(" ", "") if iscbc: ts = bf.get_ts((k, i, j), text=text) # print(ts) df = pd.DataFrame(data=ts, columns=["totim", site]) else: df = pd.DataFrame(data=bf.get_ts((k, i, j)), columns=["totim", site]) if model is not None: dts = start + pd.to_timedelta(df.totim, unit="d") df.loc[:, "totim"] = dts # print(df) f_config.write("{0},{1},{2},{3}\n".format(site, k, i, j)) df.index = df.pop("totim") dfs.append(df) f_config.close() df = pd.concat(dfs, axis=1).T df.to_csv(bin_file + "_timeseries.processed", sep=" ") if model is not None: t_str = df.columns.map(lambda x: x.strftime("%Y%m%d")) else: t_str = df.columns.map(lambda x: "{0:08.2f}".format(x)) ins_file = bin_file + "_timeseries.processed.ins" print("writing instruction file to {0}".format(ins_file)) with open(ins_file, "w") as f: f.write("pif ~\n") f.write("l1 \n") for site in df.index: # for t in t_str: f.write("l1 w ") # for site in df.columns: for t in t_str: if prefix is not None: obsnme = "{0}_{1}_{2}".format(prefix, site, t) else: obsnme = "{0}_{1}".format(site, t) f.write(" !{0}!".format(obsnme)) f.write("\n") if postprocess_inact is not None: _setup_postprocess_hds_timeseries( bin_file, df, config_file, prefix=prefix, model=model ) bd = "." if include_path: bd = os.getcwd() pth = os.path.join(*[p for p in os.path.split(bin_file)[:-1]]) os.chdir(pth) config_file = os.path.split(config_file)[-1] try: df = apply_hds_timeseries(config_file, postprocess_inact=postprocess_inact) except Exception as e: os.chdir(bd) raise Exception("error in apply_hds_timeseries(): {0}".format(str(e))) os.chdir(bd) df = try_process_output_file(ins_file) if df is None: raise Exception("error processing {0} instruction file".format(ins_file)) df.loc[:, "weight"] = 0.0 if prefix is not None: df.loc[:, "obgnme"] = df.index.map(lambda x: "_".join(x.split("_")[:2])) else: df.loc[:, "obgnme"] = df.index.map(lambda x: x.split("_")[0]) frun_line = "pyemu.gw_utils.apply_hds_timeseries('{0}',{1})\n".format( config_file, postprocess_inact ) return frun_line, df def apply_hds_timeseries(config_file=None, postprocess_inact=None): """process a modflow binary file using a previously written configuration file Args: config_file (`str`, optional): configuration file written by `pyemu.gw_utils.setup_hds_timeseries`. If `None`, looks for `hds_timeseries.config` postprocess_inact (`float`, optional): Inactive value in heads/ucn file e.g. mt.btn.cinit. If `None`, no inactive value processing happens. Default is `None`. Note: This is the companion function of `gw_utils.setup_hds_timeseries()`. """ import flopy if config_file is None: config_file = "hds_timeseries.config" assert os.path.exists(config_file), config_file with open(config_file, "r") as f: line = f.readline() ( bf_file, start_datetime, time_units, text, fill, precision, _iscbc, ) = line.strip().split(",") if len(line.strip().split(",")) == 6: ( bf_file, start_datetime, time_units, text, fill, precision, ) = line.strip().split(",") _iscbc = "false" else: ( bf_file, start_datetime, time_units, text, fill, precision, _iscbc, ) = line.strip().split(",") site_df = pd.read_csv(f) text = text.upper() if _iscbc.lower().strip() == "false": iscbc = False elif _iscbc.lower().strip() == "true": iscbc = True else: raise Exception( "apply_hds_timeseries() error: unrecognized 'iscbc' string in config file: {0}".format( _iscbc ) ) assert os.path.exists(bf_file), "head save file not found" if iscbc: try: bf = flopy.utils.CellBudgetFile(bf_file, precision=precision) except Exception as e: raise Exception("error instantiating CellBudgetFile:{0}".format(str(e))) elif bf_file.lower().endswith(".ucn"): try: bf = flopy.utils.UcnFile(bf_file, precision=precision) except Exception as e: raise Exception("error instantiating UcnFile:{0}".format(str(e))) else: try: if text != "NONE": bf = flopy.utils.HeadFile(bf_file, text=text, precision=precision) else: bf = flopy.utils.HeadFile(bf_file, precision=precision) except Exception as e: raise Exception("error instantiating HeadFile:{0}".format(str(e))) nlay, nrow, ncol = bf.nlay, bf.nrow, bf.ncol dfs = [] for site, k, i, j in zip(site_df.site, site_df.k, site_df.i, site_df.j): assert k >= 0 and k < nlay assert i >= 0 and i < nrow assert j >= 0 and j < ncol if iscbc: df = pd.DataFrame( data=bf.get_ts((k, i, j), text=text), columns=["totim", site] ) else: df = pd.DataFrame(data=bf.get_ts((k, i, j)), columns=["totim", site]) df.index = df.pop("totim") dfs.append(df) df = pd.concat(dfs, axis=1).T if df.shape != df.dropna().shape: warnings.warn("NANs in processed timeseries file", PyemuWarning) if fill.upper() != "NONE": fill = float(fill) df.fillna(fill, inplace=True) # print(df) df.to_csv(bf_file + "_timeseries.processed", sep=" ") if postprocess_inact is not None: _apply_postprocess_hds_timeseries(config_file, postprocess_inact) return df def _setup_postprocess_hds_timeseries( hds_file, df, config_file, prefix=None, model=None ): """Dirty function to setup post processing concentrations in inactive/dry cells""" warnings.warn( "Setting up post processing of hds or ucn timeseries obs. " "Prepending 'pp' to obs name may cause length to exceed 20 chars", PyemuWarning, ) if model is not None: t_str = df.columns.map(lambda x: x.strftime("%Y%m%d")) else: t_str = df.columns.map(lambda x: "{0:08.2f}".format(x)) if prefix is not None: prefix = "pp{0}".format(prefix) else: prefix = "pp" ins_file = hds_file + "_timeseries.post_processed.ins" print("writing instruction file to {0}".format(ins_file)) with open(ins_file, "w") as f: f.write("pif ~\n") f.write("l1 \n") for site in df.index: f.write("l1 w ") # for site in df.columns: for t in t_str: obsnme = "{0}{1}_{2}".format(prefix, site, t) f.write(" !{0}!".format(obsnme)) f.write("\n") frun_line = "pyemu.gw_utils._apply_postprocess_hds_timeseries('{0}')\n".format( config_file ) return frun_line def _apply_postprocess_hds_timeseries(config_file=None, cinact=1e30): """private function to post processing binary files""" import flopy if config_file is None: config_file = "hds_timeseries.config" assert os.path.exists(config_file), config_file with open(config_file, "r") as f: line = f.readline() ( hds_file, start_datetime, time_units, text, fill, precision, _iscbc, ) = line.strip().split(",") if len(line.strip().split(",")) == 6: ( hds_file, start_datetime, time_units, text, fill, precision, ) = line.strip().split(",") _iscbc = "false" else: ( hds_file, start_datetime, time_units, text, fill, precision, _iscbc, ) = line.strip().split(",") site_df = pd.read_csv(f) # print(site_df) text = text.upper() assert os.path.exists(hds_file), "head save file not found" if hds_file.lower().endswith(".ucn"): try: hds = flopy.utils.UcnFile(hds_file, precision=precision) except Exception as e: raise Exception("error instantiating UcnFile:{0}".format(str(e))) else: try: if text != "NONE": hds = flopy.utils.HeadFile(hds_file, text=text, precision=precision) else: hds = flopy.utils.HeadFile(hds_file, precision=precision) except Exception as e: raise Exception("error instantiating HeadFile:{0}".format(str(e))) nlay, nrow, ncol = hds.nlay, hds.nrow, hds.ncol dfs = [] for site, k, i, j in zip(site_df.site, site_df.k, site_df.i, site_df.j): assert k >= 0 and k < nlay assert i >= 0 and i < nrow assert j >= 0 and j < ncol if text.upper() != "NONE": df = pd.DataFrame(data=hds.get_ts((k, i, j)), columns=["totim", site]) else: df = pd.DataFrame(data=hds.get_ts((k, i, j)), columns=["totim", site]) df.index = df.pop("totim") inact_obs = df[site].apply(lambda x: np.isclose(x, cinact)) if inact_obs.sum() > 0: assert k + 1 < nlay, "Inactive observation in lowest layer" df_lower = pd.DataFrame( data=hds.get_ts((k + 1, i, j)), columns=["totim", site] ) df_lower.index = df_lower.pop("totim") df.loc[inact_obs] = df_lower.loc[inact_obs] print( "{0} observation(s) post-processed for site {1} at kij ({2},{3},{4})".format( inact_obs.sum(), site, k, i, j ) ) dfs.append(df) df = pd.concat(dfs, axis=1).T # print(df) df.to_csv(hds_file + "_timeseries.post_processed", sep=" ") return df def setup_hds_obs( hds_file, kperk_pairs=None, skip=None, prefix="hds", text="head", precision="single", include_path=False, ): """a function to setup using all values from a layer-stress period pair for observations. Args: hds_file (`str`): path and name of an existing MODFLOW head-save file. If the hds_file endswith 'ucn', then the file is treated as a UcnFile type. kperk_pairs ([(int,int)]): a list of len two tuples which are pairs of kper (zero-based stress period index) and k (zero-based layer index) to setup observations for. If None, then all layers and stress period records found in the file will be used. Caution: a shit-ton of observations may be produced! skip (variable, optional): a value or function used to determine which values to skip when setting up observations. If np.scalar(skip) is True, then values equal to skip will not be used. If skip can also be a np.ndarry with dimensions equal to the model. Observations are set up only for cells with Non-zero values in the array. If not np.ndarray or np.scalar(skip), then skip will be treated as a lambda function that returns np.NaN if the value should be skipped. prefix (`str`): the prefix to use for the observation names. default is "hds". text (`str`): the text tag the flopy HeadFile instance. Default is "head" precison (`str`): the precision string for the flopy HeadFile instance. Default is "single" include_path (`bool`, optional): flag to setup the binary file processing in directory where the hds_file is located (if different from where python is running). This is useful for setting up the process in separate directory for where python is running. Returns: tuple containing - **str**: the forward run script line needed to execute the headsave file observation operation - **pandas.DataFrame**: a dataframe of pest control file information Note: Writes an instruction file and a _setup_ csv used construct a control file. This is the companion function to `gw_utils.apply_hds_obs()`. """ try: import flopy except Exception as e: print("error importing flopy, returning {0}".format(str(e))) return assert os.path.exists(hds_file), "head save file not found" if hds_file.lower().endswith(".ucn"): try: hds = flopy.utils.UcnFile(hds_file) except Exception as e: raise Exception("error instantiating UcnFile:{0}".format(str(e))) elif text.lower() == "headu": try: hds = flopy.utils.HeadUFile(hds_file, text=text, precision=precision) except Exception as e: raise Exception("error instantiating HeadFile:{0}".format(str(e))) else: try: hds = flopy.utils.HeadFile(hds_file, text=text, precision=precision) except Exception as e: raise Exception("error instantiating HeadFile:{0}".format(str(e))) if kperk_pairs is None: kperk_pairs = [] for kstp, kper in hds.kstpkper: kperk_pairs.extend([(kper - 1, k) for k in range(hds.nlay)]) if len(kperk_pairs) == 2: try: if len(kperk_pairs[0]) == 2: pass except: kperk_pairs = [kperk_pairs] # if start_datetime is not None: # start_datetime = pd.to_datetime(start_datetime) # dts = start_datetime + pd.to_timedelta(hds.times,unit='d') data = {} kpers = [kper - 1 for kstp, kper in hds.kstpkper] for kperk_pair in kperk_pairs: kper, k = kperk_pair assert kper in kpers, "kper not in hds:{0}".format(kper) assert k in range(hds.nlay), "k not in hds:{0}".format(k) kstp = last_kstp_from_kper(hds, kper) d = hds.get_data(kstpkper=(kstp, kper))[k] data["{0}_{1}".format(kper, k)] = d.flatten() # data[(kper,k)] = d.flatten() idx, iidx, jidx = [], [], [] for _ in range(len(data)): for i in range(hds.nrow): iidx.extend([i for _ in range(hds.ncol)]) jidx.extend([j for j in range(hds.ncol)]) idx.extend(["i{0:04d}_j{1:04d}".format(i, j) for j in range(hds.ncol)]) idx = idx[: hds.nrow * hds.ncol] df = pd.DataFrame(data, index=idx) data_cols = list(df.columns) data_cols.sort() # df.loc[:,"iidx"] = iidx # df.loc[:,"jidx"] = jidx if skip is not None: for col in data_cols: if np.isscalar(skip): df.loc[df.loc[:, col] == skip, col] = np.NaN elif isinstance(skip, np.ndarray): assert ( skip.ndim >= 2 ), "skip passed as {}D array, At least 2D (<= 4D) array required".format( skip.ndim ) assert skip.shape[-2:] == ( hds.nrow, hds.ncol, ), "Array dimensions of arg. skip needs to match model dimensions ({0},{1}). ({2},{3}) passed".format( hds.nrow, hds.ncol, skip.shape[-2], skip.shape[-1] ) if skip.ndim == 2: print( "2D array passed for skip, assuming constant for all layers and kper" ) skip = np.tile(skip, (len(kpers), hds.nlay, 1, 1)) if skip.ndim == 3: print("3D array passed for skip, assuming constant for all kper") skip = np.tile(skip, (len(kpers), 1, 1, 1)) kper, k = [int(c) for c in col.split("_")] df.loc[ df.index.map( lambda x: skip[ kper, k, int(x.split("_")[0].strip("i")), int(x.split("_")[1].strip("j")), ] == 0 ), col, ] = np.NaN else: df.loc[:, col] = df.loc[:, col].apply(skip) # melt to long form df = df.melt(var_name="kperk", value_name="obsval") # set row and col identifies df.loc[:, "iidx"] = iidx df.loc[:, "jidx"] = jidx # drop nans from skip df = df.dropna() # set some additional identifiers df.loc[:, "kper"] = df.kperk.apply(lambda x: int(x.split("_")[0])) df.loc[:, "kidx"] = df.pop("kperk").apply(lambda x: int(x.split("_")[1])) # form obs names # def get_kper_str(kper): # if start_datetime is not None: # return dts[int(kper)].strftime("%Y%m%d") # else: # return "kper{0:04.0f}".format(kper) fmt = prefix + "_{0:02.0f}_{1:03.0f}_{2:03.0f}_{3:03.0f}" # df.loc[:,"obsnme"] = df.apply(lambda x: fmt.format(x.kidx,x.iidx,x.jidx, # get_kper_str(x.kper)),axis=1) df.loc[:, "obsnme"] = df.apply( lambda x: fmt.format(x.kidx, x.iidx, x.jidx, x.kper), axis=1 ) df.loc[:, "ins_str"] = df.obsnme.apply(lambda x: "l1 w !{0}!".format(x)) df.loc[:, "obgnme"] = prefix # write the instruction file with open(hds_file + ".dat.ins", "w") as f: f.write("pif ~\nl1\n") df.ins_str.to_string(f, index=False, header=False) # write the corresponding output file df.loc[:, ["obsnme", "obsval"]].to_csv(hds_file + ".dat", sep=" ", index=False) hds_path = os.path.dirname(hds_file) setup_file = os.path.join( hds_path, "_setup_{0}.csv".format(os.path.split(hds_file)[-1]) ) df.to_csv(setup_file) if not include_path: hds_file = os.path.split(hds_file)[-1] fwd_run_line = ( "pyemu.gw_utils.apply_hds_obs('{0}',precision='{1}',text='{2}')\n".format( hds_file, precision, text ) ) df.index = df.obsnme return fwd_run_line, df def last_kstp_from_kper(hds, kper): """function to find the last time step (kstp) for a give stress period (kper) in a modflow head save file. Args: hds (`flopy.utils.HeadFile`): head save file kper (`int`): the zero-index stress period number Returns: **int**: the zero-based last time step during stress period kper in the head save file """ # find the last kstp with this kper kstp = -1 for kkstp, kkper in hds.kstpkper: if kkper == kper + 1 and kkstp > kstp: kstp = kkstp if kstp == -1: raise Exception("kstp not found for kper {0}".format(kper)) kstp -= 1 return kstp def apply_hds_obs(hds_file, inact_abs_val=1.0e20, precision="single", text="head"): """process a modflow head save file. A companion function to `gw_utils.setup_hds_obs()` that is called during the forward run process Args: hds_file (`str`): a modflow head save filename. if hds_file ends with 'ucn', then the file is treated as a UcnFile type. inact_abs_val (`float`, optional): the value that marks the mininum and maximum active value. values in the headsave file greater than `inact_abs_val` or less than -`inact_abs_val` are reset to `inact_abs_val` Returns: **pandas.DataFrame**: a dataframe with extracted simulated values. Note: This is the companion function to `gw_utils.setup_hds_obs()`. """ try: import flopy except Exception as e: raise Exception("apply_hds_obs(): error importing flopy: {0}".format(str(e))) from .. import pst_utils assert os.path.exists(hds_file) out_file = hds_file + ".dat" ins_file = out_file + ".ins" assert os.path.exists(ins_file) df = pd.DataFrame({"obsnme": pst_utils.parse_ins_file(ins_file)}) df.index = df.obsnme # populate metdata items = ["k", "i", "j", "kper"] for i, item in enumerate(items): df.loc[:, item] = df.obsnme.apply(lambda x: int(x.split("_")[i + 1])) if hds_file.lower().endswith("ucn"): hds = flopy.utils.UcnFile(hds_file) elif text.lower() == "headu": hds = flopy.utils.HeadUFile(hds_file) else: hds = flopy.utils.HeadFile(hds_file, precision=precision, text=text) kpers = df.kper.unique() df.loc[:, "obsval"] = np.NaN for kper in kpers: kstp = last_kstp_from_kper(hds, kper) data = hds.get_data(kstpkper=(kstp, kper)) # jwhite 15jan2018 fix for really large values that are getting some # trash added to them... if text.lower() != "headu": data[np.isnan(data)] = 0.0 data[data > np.abs(inact_abs_val)] = np.abs(inact_abs_val) data[data < -np.abs(inact_abs_val)] = -np.abs(inact_abs_val) df_kper = df.loc[df.kper == kper, :] df.loc[df_kper.index, "obsval"] = data[df_kper.k, df_kper.i, df_kper.j] else: df_kper = df.loc[df.kper == kper, :] for k, d in enumerate(data): d[np.isnan(d)] = 0.0 d[d > np.abs(inact_abs_val)] = np.abs(inact_abs_val) d[d < -np.abs(inact_abs_val)] = -np.abs(inact_abs_val) df_kperk = df_kper.loc[df_kper.k == k, :] df.loc[df_kperk.index, "obsval"] = d[df_kperk.i] assert df.dropna().shape[0] == df.shape[0] df.loc[:, ["obsnme", "obsval"]].to_csv(out_file, index=False, sep=" ") return df def setup_sft_obs(sft_file, ins_file=None, start_datetime=None, times=None, ncomp=1): """writes a post-processor and instruction file for a mt3d-usgs sft output file Args: sft_file (`str`): path and name of an existing sft output file (ASCII) ins_file (`str`, optional): the name of the instruction file to create. If None, the name is `sft_file`+".ins". Default is `None`. start_datetime (`str`): a pandas.to_datetime() compatible str. If not None, then the resulting observation names have the datetime suffix. If None, the suffix is the output totim. Default is `None`. times ([`float`]): a list of times to make observations for. If None, all times found in the file are used. Default is None. ncomp (`int`): number of components in transport model. Default is 1. Returns: **pandas.DataFrame**: a dataframe with observation names and values for the sft simulated concentrations. Note: This is the companion function to `gw_utils.apply_sft_obs()`. """ df = pd.read_csv(sft_file, skiprows=1, delim_whitespace=True) df.columns = [c.lower().replace("-", "_") for c in df.columns] if times is None: times = df.time.unique() missing = [] utimes = df.time.unique() for t in times: if t not in utimes: missing.append(str(t)) if len(missing) > 0: print(df.time) raise Exception("the following times are missing:{0}".format(",".join(missing))) with open("sft_obs.config", "w") as f: f.write(sft_file + "\n") [f.write("{0:15.6E}\n".format(t)) for t in times] df = apply_sft_obs() utimes = df.time.unique() for t in times: assert t in utimes, "time {0} missing in processed dataframe".format(t) idx = df.time.apply(lambda x: x in times) if start_datetime is not None: start_datetime = pd.to_datetime(start_datetime) df.loc[:, "time_str"] = pd.to_timedelta(df.time, unit="d") + start_datetime df.loc[:, "time_str"] = df.time_str.apply( lambda x: datetime.strftime(x, "%Y%m%d") ) else: df.loc[:, "time_str"] = df.time.apply(lambda x: "{0:08.2f}".format(x)) df.loc[:, "ins_str"] = "l1\n" # check for multiple components df_times = df.loc[idx, :] df.loc[:, "icomp"] = 1 icomp_idx = list(df.columns).index("icomp") for t in times: df_time = df.loc[df.time == t, :].copy() vc = df_time.sfr_node.value_counts() ncomp = vc.max() assert np.all(vc.values == ncomp) nstrm = df_time.shape[0] / ncomp for icomp in range(ncomp): s = int(nstrm * (icomp)) e = int(nstrm * (icomp + 1)) idxs = df_time.iloc[s:e, :].index # df_time.iloc[nstrm*(icomp):nstrm*(icomp+1),icomp_idx.loc["icomp"] = int(icomp+1) df_time.loc[idxs, "icomp"] = int(icomp + 1) # df.loc[df_time.index,"ins_str"] = df_time.apply(lambda x: "l1 w w !sfrc{0}_{1}_{2}! !swgw{0}_{1}_{2}! !gwcn{0}_{1}_{2}!\n".\ # format(x.sfr_node,x.icomp,x.time_str),axis=1) df.loc[df_time.index, "ins_str"] = df_time.apply( lambda x: "l1 w w !sfrc{0}_{1}_{2}!\n".format( x.sfr_node, x.icomp, x.time_str ), axis=1, ) df.index = np.arange(df.shape[0]) if ins_file is None: ins_file = sft_file + ".processed.ins" with open(ins_file, "w") as f: f.write("pif ~\nl1\n") [f.write(i) for i in df.ins_str] # df = try_process_ins_file(ins_file,sft_file+".processed") df = try_process_output_file(ins_file, sft_file + ".processed") return df def apply_sft_obs(): """process an mt3d-usgs sft ASCII output file using a previous-written config file Returns: **pandas.DataFrame**: a dataframe of extracted simulated outputs Note: This is the companion function to `gw_utils.setup_sft_obs()`. """ # this is for dealing with the missing 'e' problem def try_cast(x): try: return float(x) except: return 0.0 times = [] with open("sft_obs.config") as f: sft_file = f.readline().strip() for line in f: times.append(float(line.strip())) df = pd.read_csv(sft_file, skiprows=1, delim_whitespace=True) # ,nrows=10000000) df.columns = [c.lower().replace("-", "_") for c in df.columns] df = df.loc[df.time.apply(lambda x: x in times), :] # print(df.dtypes) # normalize for c in df.columns: # print(c) if not "node" in c: df.loc[:, c] = df.loc[:, c].apply(try_cast) # print(df.loc[df.loc[:,c].apply(lambda x : type(x) == str),:]) if df.dtypes[c] == float: df.loc[df.loc[:, c] < 1e-30, c] = 0.0 df.loc[df.loc[:, c] > 1e30, c] = 1.0e30 df.loc[:, "sfr_node"] = df.sfr_node.apply(np.int) df.to_csv(sft_file + ".processed", sep=" ", index=False) return df def setup_sfr_seg_parameters( nam_file, model_ws=".", par_cols=None, tie_hcond=True, include_temporal_pars=None ): """Setup multiplier parameters for SFR segment data. Args: nam_file (`str`): MODFLOw name file. DIS, BAS, and SFR must be available as pathed in the nam_file. Optionally, `nam_file` can be an existing `flopy.modflow.Modflow`. model_ws (`str`): model workspace for flopy to load the MODFLOW model from par_cols ([`str`]): a list of segment data entires to parameterize tie_hcond (`bool`): flag to use same mult par for hcond1 and hcond2 for a given segment. Default is `True`. include_temporal_pars ([`str`]): list of spatially-global multipliers to set up for each stress period. Default is None Returns: **pandas.DataFrame**: a dataframe with useful parameter setup information Note: This function handles the standard input case, not all the cryptic SFR options. Loads the dis, bas, and sfr files with flopy using model_ws. This is the companion function to `gw_utils.apply_sfr_seg_parameters()` . The number (and numbering) of segment data entries must consistent across all stress periods. Writes `nam_file` +"_backup_.sfr" as the backup of the original sfr file Skips values = 0.0 since multipliers don't work for these """ try: import flopy except Exception as e: return if par_cols is None: par_cols = ["flow", "runoff", "hcond1", "pptsw"] if tie_hcond: if "hcond1" not in par_cols or "hcond2" not in par_cols: tie_hcond = False if isinstance(nam_file, flopy.modflow.mf.Modflow) and nam_file.sfr is not None: m = nam_file nam_file = m.namefile model_ws = m.model_ws else: # load MODFLOW model # is this needed? could we just pass the model if it has already been read in? m = flopy.modflow.Modflow.load( nam_file, load_only=["sfr"], model_ws=model_ws, check=False, forgive=False ) if include_temporal_pars: if include_temporal_pars is True: tmp_par_cols = {col: range(m.dis.nper) for col in par_cols} elif isinstance(include_temporal_pars, str): tmp_par_cols = {include_temporal_pars: range(m.dis.nper)} elif isinstance(include_temporal_pars, list): tmp_par_cols = {col: range(m.dis.nper) for col in include_temporal_pars} elif isinstance(include_temporal_pars, dict): tmp_par_cols = include_temporal_pars include_temporal_pars = True else: tmp_par_cols = {} include_temporal_pars = False # make backup copy of sfr file shutil.copy( os.path.join(model_ws, m.sfr.file_name[0]), os.path.join(model_ws, nam_file + "_backup_.sfr"), ) # get the segment data (dict) segment_data = m.sfr.segment_data shape = segment_data[list(segment_data.keys())[0]].shape # check for kper, seg_data in m.sfr.segment_data.items(): assert ( seg_data.shape == shape ), "cannot use: seg data must have the same number of entires for all kpers" seg_data_col_order = list(seg_data.dtype.names) # convert segment_data dictionary to multi index df - this could get ugly reform = { (k, c): segment_data[k][c] for k in segment_data.keys() for c in segment_data[k].dtype.names } seg_data_all_kper = pd.DataFrame.from_dict(reform) seg_data_all_kper.columns.names = ["kper", "col"] # extract the first seg data kper to a dataframe seg_data = seg_data_all_kper[0].copy() # pd.DataFrame.from_records(seg_data) # make sure all par cols are found and search of any data in kpers missing = [] cols = par_cols.copy() for par_col in set(par_cols + list(tmp_par_cols.keys())): if par_col not in seg_data.columns: if par_col in cols: missing.append(cols.pop(cols.index(par_col))) if par_col in tmp_par_cols.keys(): _ = tmp_par_cols.pop(par_col) # look across all kper in multiindex df to check for values entry - fill with absmax should capture entries else: seg_data.loc[:, par_col] = ( seg_data_all_kper.loc[:, (slice(None), par_col)] .abs() .max(level=1, axis=1) ) if len(missing) > 0: warnings.warn( "the following par_cols were not found in segment data: {0}".format( ",".join(missing) ), PyemuWarning, ) if len(missing) >= len(par_cols): warnings.warn( "None of the passed par_cols ({0}) were found in segment data.".format( ",".join(par_cols) ), PyemuWarning, ) seg_data = seg_data[seg_data_col_order] # reset column orders to inital seg_data_org = seg_data.copy() seg_data.to_csv(os.path.join(model_ws, "sfr_seg_pars.dat"), sep=",") # the data cols not to parameterize # better than a column indexer as pandas can change column orders idx_cols = ["nseg", "icalc", "outseg", "iupseg", "iprior", "nstrpts"] notpar_cols = [c for c in seg_data.columns if c not in cols + idx_cols] # process par cols tpl_str, pvals = [], [] if include_temporal_pars: tmp_pnames, tmp_tpl_str = [], [] tmp_df = pd.DataFrame( data={c: 1.0 for c in tmp_par_cols.keys()}, index=list(m.sfr.segment_data.keys()), ) tmp_df.sort_index(inplace=True) tmp_df.to_csv(os.path.join(model_ws, "sfr_seg_temporal_pars.dat")) for par_col in set(cols + list(tmp_par_cols.keys())): print(par_col) prefix = par_col if tie_hcond and par_col == "hcond2": prefix = "hcond1" if seg_data.loc[:, par_col].sum() == 0.0: print("all zeros for {0}...skipping...".format(par_col)) # seg_data.loc[:,par_col] = 1 # all zero so no need to set up if par_col in cols: # - add to notpar notpar_cols.append(cols.pop(cols.index(par_col))) if par_col in tmp_par_cols.keys(): _ = tmp_par_cols.pop(par_col) if par_col in cols: seg_data.loc[:, par_col] = seg_data.apply( lambda x: "~ {0}_{1:04d} ~".format(prefix, int(x.nseg)) if float(x[par_col]) != 0.0 else "1.0", axis=1, ) org_vals = seg_data_org.loc[seg_data_org.loc[:, par_col] != 0.0, par_col] pnames = seg_data.loc[org_vals.index, par_col] pvals.extend(list(org_vals.values)) tpl_str.extend(list(pnames.values)) if par_col in tmp_par_cols.keys(): parnme = tmp_df.index.map( lambda x: "{0}_{1:04d}_tmp".format(par_col, int(x)) if x in tmp_par_cols[par_col] else 1.0 ) sel = parnme != 1.0 tmp_df.loc[sel, par_col] = parnme[sel].map(lambda x: "~ {0} ~".format(x)) tmp_tpl_str.extend(list(tmp_df.loc[sel, par_col].values)) tmp_pnames.extend(list(parnme[sel].values)) pnames = [t.replace("~", "").strip() for t in tpl_str] df = pd.DataFrame( {"parnme": pnames, "org_value": pvals, "tpl_str": tpl_str}, index=pnames ) df.drop_duplicates(inplace=True) if df.empty: warnings.warn( "No spatial sfr segment parameters have been set up, " "either none of {0} were found or all were zero.".format( ",".join(par_cols) ), PyemuWarning, ) # return df # set not par cols to 1.0 seg_data.loc[:, notpar_cols] = "1.0" # write the template file _write_df_tpl(os.path.join(model_ws, "sfr_seg_pars.dat.tpl"), seg_data, sep=",") # make sure the tpl file exists and has the same num of pars parnme = parse_tpl_file(os.path.join(model_ws, "sfr_seg_pars.dat.tpl")) assert len(parnme) == df.shape[0] # set some useful par info df["pargp"] = df.parnme.apply(lambda x: x.split("_")[0]) if include_temporal_pars: _write_df_tpl( filename=os.path.join(model_ws, "sfr_seg_temporal_pars.dat.tpl"), df=tmp_df ) pargp = [pname.split("_")[0] + "_tmp" for pname in tmp_pnames] tmp_df = pd.DataFrame( data={"parnme": tmp_pnames, "pargp": pargp}, index=tmp_pnames ) if not tmp_df.empty: tmp_df.loc[:, "org_value"] = 1.0 tmp_df.loc[:, "tpl_str"] = tmp_tpl_str df = df.append(tmp_df[df.columns]) if df.empty: warnings.warn( "No sfr segment parameters have been set up, " "either none of {0} were found or all were zero.".format( ",".join(set(par_cols + list(tmp_par_cols.keys()))) ), PyemuWarning, ) return df # write the config file used by apply_sfr_pars() with open(os.path.join(model_ws, "sfr_seg_pars.config"), "w") as f: f.write("nam_file {0}\n".format(nam_file)) f.write("model_ws {0}\n".format(model_ws)) f.write("mult_file sfr_seg_pars.dat\n") f.write("sfr_filename {0}\n".format(m.sfr.file_name[0])) if include_temporal_pars: f.write("time_mult_file sfr_seg_temporal_pars.dat\n") # set some useful par info df.loc[:, "parubnd"] = 1.25 df.loc[:, "parlbnd"] = 0.75 hpars = df.loc[df.pargp.apply(lambda x: x.startswith("hcond")), "parnme"] df.loc[hpars, "parubnd"] = 100.0 df.loc[hpars, "parlbnd"] = 0.01 return df def setup_sfr_reach_parameters(nam_file, model_ws=".", par_cols=["strhc1"]): """Setup multiplier paramters for reach data, when reachinput option is specififed in sfr. Args: nam_file (`str`): MODFLOw name file. DIS, BAS, and SFR must be available as pathed in the nam_file. Optionally, `nam_file` can be an existing `flopy.modflow.Modflow`. model_ws (`str`): model workspace for flopy to load the MODFLOW model from par_cols ([`str`]): a list of segment data entires to parameterize tie_hcond (`bool`): flag to use same mult par for hcond1 and hcond2 for a given segment. Default is `True`. include_temporal_pars ([`str`]): list of spatially-global multipliers to set up for each stress period. Default is None Returns: **pandas.DataFrame**: a dataframe with useful parameter setup information Note: Similar to `gw_utils.setup_sfr_seg_parameters()`, method will apply params to sfr reachdata Can load the dis, bas, and sfr files with flopy using model_ws. Or can pass a model object (SFR loading can be slow) This is the companion function of `gw_utils.apply_sfr_reach_parameters()` Skips values = 0.0 since multipliers don't work for these """ try: import flopy except Exception as e: return if par_cols is None: par_cols = ["strhc1"] if isinstance(nam_file, flopy.modflow.mf.Modflow) and nam_file.sfr is not None: # flopy MODFLOW model has been passed and has SFR loaded m = nam_file nam_file = m.namefile model_ws = m.model_ws else: # if model has not been passed or SFR not loaded # load MODFLOW model m = flopy.modflow.Modflow.load( nam_file, load_only=["sfr"], model_ws=model_ws, check=False, forgive=False ) # get reachdata as dataframe reach_data = pd.DataFrame.from_records(m.sfr.reach_data) # write inital reach_data as csv reach_data_orig = reach_data.copy() reach_data.to_csv(os.path.join(m.model_ws, "sfr_reach_pars.dat"), sep=",") # generate template file with pars in par_cols # process par cols tpl_str, pvals = [], [] # par_cols=["strhc1"] idx_cols = ["node", "k", "i", "j", "iseg", "ireach", "reachID", "outreach"] # the data cols not to parameterize notpar_cols = [c for c in reach_data.columns if c not in par_cols + idx_cols] # make sure all par cols are found and search of any data in kpers missing = [] cols = par_cols.copy() for par_col in par_cols: if par_col not in reach_data.columns: missing.append(par_col) cols.remove(par_col) if len(missing) > 0: warnings.warn( "the following par_cols were not found in reach data: {0}".format( ",".join(missing) ), PyemuWarning, ) if len(missing) >= len(par_cols): warnings.warn( "None of the passed par_cols ({0}) were found in reach data.".format( ",".join(par_cols) ), PyemuWarning, ) for par_col in cols: if par_col == "strhc1": prefix = "strk" # shorten par else: prefix = par_col reach_data.loc[:, par_col] = reach_data.apply( lambda x: "~ {0}_{1:04d} ~".format(prefix, int(x.reachID)) if float(x[par_col]) != 0.0 else "1.0", axis=1, ) org_vals = reach_data_orig.loc[reach_data_orig.loc[:, par_col] != 0.0, par_col] pnames = reach_data.loc[org_vals.index, par_col] pvals.extend(list(org_vals.values)) tpl_str.extend(list(pnames.values)) pnames = [t.replace("~", "").strip() for t in tpl_str] df = pd.DataFrame( {"parnme": pnames, "org_value": pvals, "tpl_str": tpl_str}, index=pnames ) df.drop_duplicates(inplace=True) if df.empty: warnings.warn( "No sfr reach parameters have been set up, either none of {0} were found or all were zero.".format( ",".join(par_cols) ), PyemuWarning, ) else: # set not par cols to 1.0 reach_data.loc[:, notpar_cols] = "1.0" # write the template file _write_df_tpl( os.path.join(model_ws, "sfr_reach_pars.dat.tpl"), reach_data, sep="," ) # write the config file used by apply_sfr_pars() with open(os.path.join(model_ws, "sfr_reach_pars.config"), "w") as f: f.write("nam_file {0}\n".format(nam_file)) f.write("model_ws {0}\n".format(model_ws)) f.write("mult_file sfr_reach_pars.dat\n") f.write("sfr_filename {0}".format(m.sfr.file_name[0])) # make sure the tpl file exists and has the same num of pars parnme = parse_tpl_file(os.path.join(model_ws, "sfr_reach_pars.dat.tpl")) assert len(parnme) == df.shape[0] # set some useful par info df.loc[:, "pargp"] = df.parnme.apply(lambda x: x.split("_")[0]) df.loc[:, "parubnd"] = 1.25 df.loc[:, "parlbnd"] = 0.75 hpars = df.loc[df.pargp.apply(lambda x: x.startswith("strk")), "parnme"] df.loc[hpars, "parubnd"] = 100.0 df.loc[hpars, "parlbnd"] = 0.01 return df def apply_sfr_seg_parameters(seg_pars=True, reach_pars=False): """apply the SFR segement multiplier parameters. Args: seg_pars (`bool`, optional): flag to apply segment-based parameters. Default is True reach_pars (`bool`, optional): flag to apply reach-based parameters. Default is False Returns: **flopy.modflow.ModflowSfr**: the modified SFR package instance Note: Expects "sfr_seg_pars.config" to exist Expects `nam_file` +"_backup_.sfr" to exist """ if not seg_pars and not reach_pars: raise Exception( "gw_utils.apply_sfr_pars() error: both seg_pars and reach_pars are False" ) # if seg_pars and reach_pars: # raise Exception("gw_utils.apply_sfr_pars() error: both seg_pars and reach_pars are True") import flopy bak_sfr_file, pars = None, None if seg_pars: assert os.path.exists("sfr_seg_pars.config") with open("sfr_seg_pars.config", "r") as f: pars = {} for line in f: line = line.strip().split() pars[line[0]] = line[1] bak_sfr_file = pars["nam_file"] + "_backup_.sfr" # m = flopy.modflow.Modflow.load(pars["nam_file"],model_ws=pars["model_ws"],load_only=["sfr"],check=False) m = flopy.modflow.Modflow.load(pars["nam_file"], load_only=[], check=False) sfr = flopy.modflow.ModflowSfr2.load(os.path.join(bak_sfr_file), m) sfrfile = pars["sfr_filename"] mlt_df = pd.read_csv(pars["mult_file"], delim_whitespace=False, index_col=0) # time_mlt_df = None # if "time_mult_file" in pars: # time_mult_file = pars["time_mult_file"] # time_mlt_df = pd.read_csv(pars["time_mult_file"], delim_whitespace=False,index_col=0) idx_cols = ["nseg", "icalc", "outseg", "iupseg", "iprior", "nstrpts"] present_cols = [c for c in idx_cols if c in mlt_df.columns] mlt_cols = mlt_df.columns.drop(present_cols) for key, val in m.sfr.segment_data.items(): df = pd.DataFrame.from_records(val) df.loc[:, mlt_cols] *= mlt_df.loc[:, mlt_cols] val = df.to_records(index=False) sfr.segment_data[key] = val if reach_pars: assert os.path.exists("sfr_reach_pars.config") with open("sfr_reach_pars.config", "r") as f: r_pars = {} for line in f: line = line.strip().split() r_pars[line[0]] = line[1] if bak_sfr_file is None: # will be the case is seg_pars is false bak_sfr_file = r_pars["nam_file"] + "_backup_.sfr" # m = flopy.modflow.Modflow.load(pars["nam_file"],model_ws=pars["model_ws"],load_only=["sfr"],check=False) m = flopy.modflow.Modflow.load( r_pars["nam_file"], load_only=[], check=False ) sfr = flopy.modflow.ModflowSfr2.load(os.path.join(bak_sfr_file), m) sfrfile = r_pars["sfr_filename"] r_mlt_df = pd.read_csv(r_pars["mult_file"], sep=",", index_col=0) r_idx_cols = ["node", "k", "i", "j", "iseg", "ireach", "reachID", "outreach"] r_mlt_cols = r_mlt_df.columns.drop(r_idx_cols) r_df = pd.DataFrame.from_records(m.sfr.reach_data) r_df.loc[:, r_mlt_cols] *= r_mlt_df.loc[:, r_mlt_cols] sfr.reach_data = r_df.to_records(index=False) # m.remove_package("sfr") if pars is not None and "time_mult_file" in pars: time_mult_file = pars["time_mult_file"] time_mlt_df = pd.read_csv(time_mult_file, delim_whitespace=False, index_col=0) for kper, sdata in m.sfr.segment_data.items(): assert kper in time_mlt_df.index, ( "gw_utils.apply_sfr_seg_parameters() error: kper " + "{0} not in time_mlt_df index".format(kper) ) for col in time_mlt_df.columns: sdata[col] *= time_mlt_df.loc[kper, col] sfr.write_file(filename=sfrfile) return sfr def apply_sfr_parameters(seg_pars=True, reach_pars=False): """thin wrapper around `gw_utils.apply_sfr_seg_parameters()` Args: seg_pars (`bool`, optional): flag to apply segment-based parameters. Default is True reach_pars (`bool`, optional): flag to apply reach-based parameters. Default is False Returns: **flopy.modflow.ModflowSfr**: the modified SFR package instance Note: Expects "sfr_seg_pars.config" to exist Expects `nam_file` +"_backup_.sfr" to exist """ sfr = apply_sfr_seg_parameters(seg_pars=seg_pars, reach_pars=reach_pars) return sfr def setup_sfr_obs( sfr_out_file, seg_group_dict=None, ins_file=None, model=None, include_path=False ): """setup observations using the sfr ASCII output file. Setups the ability to aggregate flows for groups of segments. Applies only flow to aquier and flow out. Args: sft_out_file (`str`): the name and path to an existing SFR output file seg_group_dict (`dict`): a dictionary of SFR segements to aggregate together for a single obs. the key value in the dict is the base observation name. If None, all segments are used as individual observations. Default is None model (`flopy.mbase`): a flopy model. If passed, the observation names will have the datetime of the observation appended to them. If None, the observation names will have the stress period appended to them. Default is None. include_path (`bool`): flag to prepend sfr_out_file path to sfr_obs.config. Useful for setting up process in separate directory for where python is running. Returns: **pandas.DataFrame**: dataframe of observation name, simulated value and group. Note: This is the companion function of `gw_utils.apply_sfr_obs()`. This function writes "sfr_obs.config" which must be kept in the dir where "gw_utils.apply_sfr_obs()" is being called during the forward run """ sfr_dict = load_sfr_out(sfr_out_file) kpers = list(sfr_dict.keys()) kpers.sort() if seg_group_dict is None: seg_group_dict = {"seg{0:04d}".format(s): s for s in sfr_dict[kpers[0]].segment} else: warnings.warn( "Flow out (flout) of grouped segments will be aggregated... ", PyemuWarning ) sfr_segs = set(sfr_dict[list(sfr_dict.keys())[0]].segment) keys = ["sfr_out_file"] if include_path: values = [os.path.split(sfr_out_file)[-1]] else: values = [sfr_out_file] for oname, segs in seg_group_dict.items(): if np.isscalar(segs): segs_set = {segs} segs = [segs] else: segs_set = set(segs) diff = segs_set.difference(sfr_segs) if len(diff) > 0: raise Exception( "the following segs listed with oname {0} where not found: {1}".format( oname, ",".join([str(s) for s in diff]) ) ) for seg in segs: keys.append(oname) values.append(seg) df_key = pd.DataFrame({"obs_base": keys, "segment": values}) if include_path: pth = os.path.join(*[p for p in os.path.split(sfr_out_file)[:-1]]) config_file = os.path.join(pth, "sfr_obs.config") else: config_file = "sfr_obs.config" print("writing 'sfr_obs.config' to {0}".format(config_file)) df_key.to_csv(config_file) bd = "." if include_path: bd = os.getcwd() os.chdir(pth) try: df = apply_sfr_obs() except Exception as e: os.chdir(bd) raise Exception("error in apply_sfr_obs(): {0}".format(str(e))) os.chdir(bd) if model is not None: dts = ( pd.to_datetime(model.start_datetime) + pd.to_timedelta(np.cumsum(model.dis.perlen.array), unit="d") ).date df.loc[:, "datetime"] = df.kper.apply(lambda x: dts[x]) df.loc[:, "time_str"] = df.datetime.apply(lambda x: x.strftime("%Y%m%d")) else: df.loc[:, "time_str"] = df.kper.apply(lambda x: "{0:04d}".format(x)) df.loc[:, "flaqx_obsnme"] = df.apply( lambda x: "{0}_{1}_{2}".format("fa", x.obs_base, x.time_str), axis=1 ) df.loc[:, "flout_obsnme"] = df.apply( lambda x: "{0}_{1}_{2}".format("fo", x.obs_base, x.time_str), axis=1 ) if ins_file is None: ins_file = sfr_out_file + ".processed.ins" with open(ins_file, "w") as f: f.write("pif ~\nl1\n") for fla, flo in zip(df.flaqx_obsnme, df.flout_obsnme): f.write("l1 w w !{0}! !{1}!\n".format(fla, flo)) df = None pth = os.path.split(ins_file)[:-1] pth = os.path.join(*pth) if pth == "": pth = "." bd = os.getcwd() os.chdir(pth) df = try_process_output_file( os.path.split(ins_file)[-1], os.path.split(sfr_out_file + ".processed")[-1] ) os.chdir(bd) if df is not None: df.loc[:, "obsnme"] = df.index.values df.loc[:, "obgnme"] = df.obsnme.apply( lambda x: "flaqx" if x.startswith("fa") else "flout" ) return df def apply_sfr_obs(): """apply the sfr observation process Args: None Returns: **pandas.DataFrame**: a dataframe of aggregrated sfr segment aquifer and outflow Note: This is the companion function of `gw_utils.setup_sfr_obs()`. Requires `sfr_obs.config`. Writes `sfr_out_file`+".processed", where `sfr_out_file` is defined in "sfr_obs.config" """ assert os.path.exists("sfr_obs.config") df_key = pd.read_csv("sfr_obs.config", index_col=0) assert df_key.iloc[0, 0] == "sfr_out_file", df_key.iloc[0, :] sfr_out_file = df_key.iloc[0, 1] df_key = df_key.iloc[1:, :] df_key.loc[:, "segment"] = df_key.segment.apply(np.int) df_key.index = df_key.segment seg_group_dict = df_key.groupby(df_key.obs_base).groups sfr_kper = load_sfr_out(sfr_out_file) kpers = list(sfr_kper.keys()) kpers.sort() # results = {o:[] for o in seg_group_dict.keys()} results = [] for kper in kpers: df = sfr_kper[kper] for obs_base, segs in seg_group_dict.items(): agg = df.loc[ segs.values, : ].sum() # still agg flout where seg groups are passed! # print(obs_base,agg) results.append([kper, obs_base, agg["flaqx"], agg["flout"]]) df = pd.DataFrame(data=results, columns=["kper", "obs_base", "flaqx", "flout"]) df.sort_values(by=["kper", "obs_base"], inplace=True) df.to_csv(sfr_out_file + ".processed", sep=" ", index=False) return df def load_sfr_out(sfr_out_file, selection=None): """load an ASCII SFR output file into a dictionary of kper: dataframes. Args: sfr_out_file (`str`): SFR ASCII output file selection (`pandas.DataFrame`): a dataframe of `reach` and `segment` pairs to load. If `None`, all reach-segment pairs are loaded. Default is `None`. Returns: **dict**: dictionary of {kper:`pandas.DataFrame`} of SFR output. Note: Aggregates flow to aquifer for segments and returns and flow out at downstream end of segment. """ assert os.path.exists(sfr_out_file), "couldn't find sfr out file {0}".format( sfr_out_file ) tag = " stream listing" lcount = 0 sfr_dict = {} if selection is None: pass elif isinstance(selection, str): assert ( selection == "all" ), "If string passed as selection only 'all' allowed: " "{}".format(selection) else: assert isinstance( selection, pd.DataFrame ), "'selection needs to be pandas Dataframe. " "Type {} passed.".format( type(selection) ) assert np.all( [sr in selection.columns for sr in ["segment", "reach"]] ), "Either 'segment' or 'reach' not in selection columns" with open(sfr_out_file) as f: while True: line = f.readline().lower() lcount += 1 if line == "": break if line.startswith(tag): raw = line.strip().split() kper = int(raw[3]) - 1 kstp = int(raw[5]) - 1 [f.readline() for _ in range(4)] # skip to where the data starts lcount += 4 dlines = [] while True: dline = f.readline() lcount += 1 if dline.strip() == "": break draw = dline.strip().split() dlines.append(draw) df = pd.DataFrame(data=np.array(dlines)).iloc[:, [3, 4, 6, 7]] df.columns = ["segment", "reach", "flaqx", "flout"] df["segment"] = df.segment.astype(np.int) df["reach"] = df.reach.astype(np.int) df["flaqx"] = df.flaqx.astype(np.float) df["flout"] = df.flout.astype(np.float) df.index = [ "{0:03d}_{1:03d}".format(s, r) for s, r in np.array([df.segment.values, df.reach.values]).T ] # df.index = df.apply( # lambda x: "{0:03d}_{1:03d}".format( # int(x.segment), int(x.reach)), axis=1) if selection is None: # setup for all segs, aggregate gp = df.groupby(df.segment) bot_reaches = ( gp[["reach"]] .max() .apply( lambda x: "{0:03d}_{1:03d}".format( int(x.name), int(x.reach) ), axis=1, ) ) # only sum distributed output # take flow out of seg df2 = pd.DataFrame( { "flaqx": gp.flaqx.sum(), "flout": df.loc[bot_reaches, "flout"].values, }, index=gp.groups.keys(), ) # df = df.groupby(df.segment).sum() df2["segment"] = df2.index elif isinstance(selection, str) and selection == "all": df2 = df else: seg_reach_id = selection.apply( lambda x: "{0:03d}_{1:03d}".format( int(x.segment), int(x.reach) ), axis=1, ).values for sr in seg_reach_id: if sr not in df.index: s, r = [x.lstrip("0") for x in sr.split("_")] warnings.warn( "Requested segment reach pair ({0},{1}) " "is not in sfr output. Dropping...".format( int(r), int(s) ), PyemuWarning, ) seg_reach_id = np.delete( seg_reach_id, np.where(seg_reach_id == sr), axis=0 ) df2 = df.loc[seg_reach_id].copy() if kper in sfr_dict.keys(): print( "multiple entries found for kper {0}, " "replacing...".format(kper) ) sfr_dict[kper] = df2 return sfr_dict def setup_sfr_reach_obs( sfr_out_file, seg_reach=None, ins_file=None, model=None, include_path=False ): """setup observations using the sfr ASCII output file. Setups sfr point observations using segment and reach numbers. Args: sft_out_file (`str`): the path and name of an existing SFR output file seg_reach (varies): a dict, or list of SFR [segment,reach] pairs identifying locations of interest. If `dict`, the key value in the dict is the base observation name. If None, all reaches are used as individual observations. Default is None - THIS MAY SET UP A LOT OF OBS! model (`flopy.mbase`): a flopy model. If passed, the observation names will have the datetime of the observation appended to them. If None, the observation names will have the stress period appended to them. Default is None. include_path (`bool`): a flag to prepend sfr_out_file path to sfr_obs.config. Useful for setting up process in separate directory for where python is running. Returns: `pd.DataFrame`: a dataframe of observation names, values, and groups Note: This is the companion function of `gw_utils.apply_sfr_reach_obs()`. This function writes "sfr_reach_obs.config" which must be kept in the dir where "apply_sfr_reach_obs()" is being called during the forward run """ if seg_reach is None: warnings.warn("Obs will be set up for every reach", PyemuWarning) seg_reach = "all" elif isinstance(seg_reach, list) or isinstance(seg_reach, np.ndarray): if np.ndim(seg_reach) == 1: seg_reach = [seg_reach] assert ( np.shape(seg_reach)[1] == 2 ), "varible seg_reach expected shape (n,2), received {0}".format( np.shape(seg_reach) ) seg_reach = pd.DataFrame(seg_reach, columns=["segment", "reach"]) seg_reach.index = seg_reach.apply( lambda x: "s{0:03d}r{1:03d}".format(int(x.segment), int(x.reach)), axis=1 ) elif isinstance(seg_reach, dict): seg_reach = pd.DataFrame.from_dict( seg_reach, orient="index", columns=["segment", "reach"] ) else: assert isinstance( seg_reach, pd.DataFrame ), "'selection needs to be pandas Dataframe. Type {} passed.".format( type(seg_reach) ) assert np.all( [sr in seg_reach.columns for sr in ["segment", "reach"]] ), "Either 'segment' or 'reach' not in selection columns" sfr_dict = load_sfr_out(sfr_out_file, selection=seg_reach) kpers = list(sfr_dict.keys()) kpers.sort() if isinstance(seg_reach, str) and seg_reach == "all": seg_reach = sfr_dict[kpers[0]][["segment", "reach"]] seg_reach.index = seg_reach.apply( lambda x: "s{0:03d}r{1:03d}".format(int(x.segment), int(x.reach)), axis=1 ) keys = ["sfr_out_file"] if include_path: values = [os.path.split(sfr_out_file)[-1]] else: values = [sfr_out_file] diff = seg_reach.loc[ seg_reach.apply( lambda x: "{0:03d}_{1:03d}".format(int(x.segment), int(x.reach)) not in sfr_dict[list(sfr_dict.keys())[0]].index, axis=1, ) ] if len(diff) > 0: for ob in diff.itertuples(): warnings.warn( "segs,reach pair listed with onames {0} was not found: {1}".format( ob.Index, "({},{})".format(ob.segment, ob.reach) ), PyemuWarning, ) seg_reach = seg_reach.drop(diff.index) seg_reach["obs_base"] = seg_reach.index df_key = pd.DataFrame({"obs_base": keys, "segment": 0, "reach": values}) df_key = pd.concat([df_key, seg_reach], sort=True).reset_index(drop=True) if include_path: pth = os.path.join(*[p for p in os.path.split(sfr_out_file)[:-1]]) config_file = os.path.join(pth, "sfr_reach_obs.config") else: config_file = "sfr_reach_obs.config" print("writing 'sfr_reach_obs.config' to {0}".format(config_file)) df_key.to_csv(config_file) bd = "." if include_path: bd = os.getcwd() os.chdir(pth) try: df = apply_sfr_reach_obs() except Exception as e: os.chdir(bd) raise Exception("error in apply_sfr_reach_obs(): {0}".format(str(e))) os.chdir(bd) if model is not None: dts = ( pd.to_datetime(model.start_datetime) + pd.to_timedelta(np.cumsum(model.dis.perlen.array), unit="d") ).date df.loc[:, "datetime"] = df.kper.apply(lambda x: dts[x]) df.loc[:, "time_str"] = df.datetime.apply(lambda x: x.strftime("%Y%m%d")) else: df.loc[:, "time_str"] = df.kper.apply(lambda x: "{0:04d}".format(x)) df.loc[:, "flaqx_obsnme"] = df.apply( lambda x: "{0}_{1}_{2}".format("fa", x.obs_base, x.time_str), axis=1 ) df.loc[:, "flout_obsnme"] = df.apply( lambda x: "{0}_{1}_{2}".format("fo", x.obs_base, x.time_str), axis=1 ) if ins_file is None: ins_file = sfr_out_file + ".reach_processed.ins" with open(ins_file, "w") as f: f.write("pif ~\nl1\n") for fla, flo in zip(df.flaqx_obsnme, df.flout_obsnme): f.write("l1 w w !{0}! !{1}!\n".format(fla, flo)) df = None pth = os.path.split(ins_file)[:-1] pth = os.path.join(*pth) if pth == "": pth = "." bd = os.getcwd() os.chdir(pth) try: df = try_process_output_file( os.path.split(ins_file)[-1], os.path.split(sfr_out_file + ".processed")[-1] ) except Exception as e: pass os.chdir(bd) if df is not None: df.loc[:, "obsnme"] = df.index.values df.loc[:, "obgnme"] = df.obsnme.apply( lambda x: "flaqx" if x.startswith("fa") else "flout" ) return df def apply_sfr_reach_obs(): """apply the sfr reach observation process. Returns: `pd.DataFrame`: a dataframe of sfr aquifer and outflow ad segment,reach locations Note: This is the companion function of `gw_utils.setup_sfr_reach_obs()`. Requires sfr_reach_obs.config. Writes <sfr_out_file>.processed, where <sfr_out_file> is defined in "sfr_reach_obs.config" """ assert os.path.exists("sfr_reach_obs.config") df_key = pd.read_csv("sfr_reach_obs.config", index_col=0) assert df_key.iloc[0, 0] == "sfr_out_file", df_key.iloc[0, :] sfr_out_file = df_key.iloc[0].reach df_key = df_key.iloc[1:, :].copy() df_key.loc[:, "segment"] = df_key.segment.apply(np.int) df_key.loc[:, "reach"] = df_key.reach.apply(np.int) df_key = df_key.set_index("obs_base") sfr_kper = load_sfr_out(sfr_out_file, df_key) kpers = list(sfr_kper.keys()) kpers.sort() results = [] for kper in kpers: df = sfr_kper[kper] for sr in df_key.itertuples(): ob = df.loc["{0:03d}_{1:03d}".format(sr.segment, sr.reach), :] results.append([kper, sr.Index, ob["flaqx"], ob["flout"]]) df = pd.DataFrame(data=results, columns=["kper", "obs_base", "flaqx", "flout"]) df.sort_values(by=["kper", "obs_base"], inplace=True) df.to_csv(sfr_out_file + ".reach_processed", sep=" ", index=False) return df def modflow_sfr_gag_to_instruction_file( gage_output_file, ins_file=None, parse_filename=False ): """writes an instruction file for an SFR gage output file to read Flow only at all times Args: gage_output_file (`str`): the gage output filename (ASCII). ins_file (`str`, optional): the name of the instruction file to create. If None, the name is `gage_output_file` +".ins". Default is None parse_filename (`bool`): if True, get the gage_num parameter by parsing the gage output file filename if False, get the gage number from the file itself Returns: tuple containing - **pandas.DataFrame**: a dataframe with obsnme and obsval for the sfr simulated flows. - **str**: file name of instructions file relating to gage output. - **str**: file name of processed gage output for all times Note: Sets up observations for gage outputs only for the Flow column. If `parse_namefile` is true, only text up to first '.' is used as the gage_num """ if ins_file is None: ins_file = gage_output_file + ".ins" # navigate the file to be sure the header makes sense indat = [line.strip() for line in open(gage_output_file, "r").readlines()] header = [i for i in indat if i.startswith('"')] # yank out the gage number to identify the observation names if parse_filename: gage_num = os.path.basename(gage_output_file).split(".")[0] else: gage_num = re.sub( "[^0-9]", "", indat[0].lower().split("gage no.")[-1].strip().split()[0] ) # get the column names cols = ( [i.lower() for i in header if "data" in i.lower()][0] .lower() .replace('"', "") .replace("data:", "") .split() ) # make sure "Flow" is included in the columns if "flow" not in cols: raise Exception('Requested field "Flow" not in gage output columns') # find which column is for "Flow" flowidx = np.where(np.array(cols) == "flow")[0][0] # write out the instruction file lines inslines = [ "l1 " + (flowidx + 1) * "w " + "!g{0}_{1:d}!".format(gage_num, j) for j in range(len(indat) - len(header)) ] inslines[0] = inslines[0].replace("l1", "l{0:d}".format(len(header) + 1)) # write the instruction file with open(ins_file, "w") as ofp: ofp.write("pif ~\n") [ofp.write("{0}\n".format(line)) for line in inslines] df = try_process_output_file(ins_file, gage_output_file) return df, ins_file, gage_output_file def setup_gage_obs(gage_file, ins_file=None, start_datetime=None, times=None): """setup a forward run post processor routine for the modflow gage file Args: gage_file (`str`): the gage output file (ASCII) ins_file (`str`, optional): the name of the instruction file to create. If None, the name is `gage_file`+".processed.ins". Default is `None` start_datetime (`str`): a `pandas.to_datetime()` compatible `str`. If not `None`, then the resulting observation names have the datetime suffix. If `None`, the suffix is the output totim. Default is `None`. times ([`float`]): a container of times to make observations for. If None, all times are used. Default is None. Returns: tuple containing - **pandas.DataFrame**: a dataframe with observation name and simulated values for the values in the gage file. - **str**: file name of instructions file that was created relating to gage output. - **str**: file name of processed gage output (processed according to times passed above.) Note: Setups up observations for gage outputs (all columns). This is the companion function of `gw_utils.apply_gage_obs()` """ with open(gage_file, "r") as f: line1 = f.readline() gage_num = int( re.sub("[^0-9]", "", line1.split("GAGE No.")[-1].strip().split()[0]) ) gage_type = line1.split("GAGE No.")[-1].strip().split()[1].lower() obj_num = int(line1.replace('"', "").strip().split()[-1]) line2 = f.readline() df = pd.read_csv( f, delim_whitespace=True, names=line2.replace('"', "").split()[1:] ) df.columns = [ c.lower().replace("-", "_").replace(".", "_").strip("_") for c in df.columns ] # get unique observation ids obs_ids = { col: "" for col in df.columns[1:] } # empty dictionary for observation ids for col in df.columns[1:]: # exclude column 1 (TIME) colspl = col.split("_") if len(colspl) > 1: # obs name built out of "g"(for gage) "s" or "l"(for gage type) 2 chars from column name - date added later obs_ids[col] = "g{0}{1}{2}".format( gage_type[0], colspl[0][0], colspl[-1][0] ) else: obs_ids[col] = "g{0}{1}".format(gage_type[0], col[0:2]) with open( "_gage_obs_ids.csv", "w" ) as f: # write file relating obs names to meaningfull keys! [f.write("{0},{1}\n".format(key, obs)) for key, obs in obs_ids.items()] # find passed times in df if times is None: times = df.time.unique() missing = [] utimes = df.time.unique() for t in times: if not np.isclose(t, utimes).any(): missing.append(str(t)) if len(missing) > 0: print(df.time) raise Exception("the following times are missing:{0}".format(",".join(missing))) # write output times to config file with open("gage_obs.config", "w") as f: f.write(gage_file + "\n") [f.write("{0:15.10E}\n".format(t)) for t in times] # extract data for times: returns dataframe and saves a processed df - read by pest df, obs_file = apply_gage_obs(return_obs_file=True) utimes = df.time.unique() for t in times: assert np.isclose( t, utimes ).any(), "time {0} missing in processed dataframe".format(t) idx = df.time.apply( lambda x: np.isclose(x, times).any() ) # boolean selector of desired times in df if start_datetime is not None: # convert times to usable observation times start_datetime = pd.to_datetime(start_datetime) df.loc[:, "time_str"] = pd.to_timedelta(df.time, unit="d") + start_datetime df.loc[:, "time_str"] = df.time_str.apply( lambda x: datetime.strftime(x, "%Y%m%d") ) else: df.loc[:, "time_str"] = df.time.apply(lambda x: "{0:08.2f}".format(x)) # set up instructions (line feed for lines without obs (not in time) df.loc[:, "ins_str"] = "l1\n" df_times = df.loc[idx, :] # Slice by desired times # TODO include GAGE No. in obs name (if permissible) df.loc[df_times.index, "ins_str"] = df_times.apply( lambda x: "l1 w {}\n".format( " w ".join( ["!{0}{1}!".format(obs, x.time_str) for key, obs in obs_ids.items()] ) ), axis=1, ) df.index = np.arange(df.shape[0]) if ins_file is None: ins_file = gage_file + ".processed.ins" with open(ins_file, "w") as f: f.write("pif ~\nl1\n") [f.write(i) for i in df.ins_str] df = try_process_output_file(ins_file, gage_file + ".processed") return df, ins_file, obs_file def apply_gage_obs(return_obs_file=False): """apply the modflow gage obs post-processor Args: return_obs_file (`bool`): flag to return the processed observation file. Default is `False`. Note: This is the companion function of `gw_utils.setup_gage_obs()` """ times = [] with open("gage_obs.config") as f: gage_file = f.readline().strip() for line in f: times.append(float(line.strip())) obs_file = gage_file + ".processed" with open(gage_file, "r") as f: line1 = f.readline() gage_num = int( re.sub("[^0-9]", "", line1.split("GAGE No.")[-1].strip().split()[0]) ) gage_type = line1.split("GAGE No.")[-1].strip().split()[1].lower() obj_num = int(line1.replace('"', "").strip().split()[-1]) line2 = f.readline() df = pd.read_csv( f, delim_whitespace=True, names=line2.replace('"', "").split()[1:] ) df.columns = [c.lower().replace("-", "_").replace(".", "_") for c in df.columns] df = df.loc[df.time.apply(lambda x: np.isclose(x, times).any()), :] df.to_csv(obs_file, sep=" ", index=False) if return_obs_file: return df, obs_file else: return df def apply_hfb_pars(par_file="hfb6_pars.csv"): """a function to apply HFB multiplier parameters. Args: par_file (`str`): the HFB parameter info file. Default is `hfb_pars.csv` Note: This is the companion function to `gw_utils.write_hfb_zone_multipliers_template()` This is to account for the horrible HFB6 format that differs from other BCs making this a special case Requires "hfb_pars.csv" Should be added to the forward_run.py script """ hfb_pars = pd.read_csv(par_file) hfb_mults_contents = open(hfb_pars.mlt_file.values[0], "r").readlines() skiprows = ( sum([1 if i.strip().startswith("#") else 0 for i in hfb_mults_contents]) + 1 ) header = hfb_mults_contents[:skiprows] # read in the multipliers names = ["lay", "irow1", "icol1", "irow2", "icol2", "hydchr"] hfb_mults = pd.read_csv( hfb_pars.mlt_file.values[0], skiprows=skiprows, delim_whitespace=True, names=names, ).dropna() # read in the original file hfb_org = pd.read_csv( hfb_pars.org_file.values[0], skiprows=skiprows, delim_whitespace=True, names=names, ).dropna() # multiply it out hfb_org.hydchr *= hfb_mults.hydchr for cn in names[:-1]: hfb_mults[cn] = hfb_mults[cn].astype(np.int) hfb_org[cn] = hfb_org[cn].astype(np.int) # write the results with open(hfb_pars.model_file.values[0], "w", newline="") as ofp: [ofp.write("{0}\n".format(line.strip())) for line in header] ofp.flush() hfb_org[["lay", "irow1", "icol1", "irow2", "icol2", "hydchr"]].to_csv( ofp, sep=" ", header=None, index=None ) def write_hfb_zone_multipliers_template(m): """write a template file for an hfb using multipliers per zone (double yuck!) Args: m (`flopy.modflow.Modflow`): a model instance with an HFB package Returns: tuple containing - **dict**: a dictionary with original unique HFB conductivity values and their corresponding parameter names - **str**: the template filename that was created """ if m.hfb6 is None: raise Exception("no HFB package found") # find the model file hfb_file = os.path.join(m.model_ws, m.hfb6.file_name[0]) # this will use multipliers, so need to copy down the original if not os.path.exists(os.path.join(m.model_ws, "hfb6_org")): os.mkdir(os.path.join(m.model_ws, "hfb6_org")) # copy down the original file shutil.copy2( os.path.join(m.model_ws, m.hfb6.file_name[0]), os.path.join(m.model_ws, "hfb6_org", m.hfb6.file_name[0]), ) if not os.path.exists(os.path.join(m.model_ws, "hfb6_mlt")): os.mkdir(os.path.join(m.model_ws, "hfb6_mlt")) # read in the model file hfb_file_contents = open(hfb_file, "r").readlines() # navigate the header skiprows = ( sum([1 if i.strip().startswith("#") else 0 for i in hfb_file_contents]) + 1 ) header = hfb_file_contents[:skiprows] # read in the data names = ["lay", "irow1", "icol1", "irow2", "icol2", "hydchr"] hfb_in = pd.read_csv( hfb_file, skiprows=skiprows, delim_whitespace=True, names=names ).dropna() for cn in names[:-1]: hfb_in[cn] = hfb_in[cn].astype(np.int) # set up a multiplier for each unique conductivity value unique_cond = hfb_in.hydchr.unique() hfb_mults = dict( zip(unique_cond, ["hbz_{0:04d}".format(i) for i in range(len(unique_cond))]) ) # set up the TPL line for each parameter and assign hfb_in["tpl"] = "blank" for cn, cg in hfb_in.groupby("hydchr"): hfb_in.loc[hfb_in.hydchr == cn, "tpl"] = "~{0:^10s}~".format(hfb_mults[cn]) assert "blank" not in hfb_in.tpl # write out the TPL file tpl_file = os.path.join(m.model_ws, "hfb6.mlt.tpl") with open(tpl_file, "w", newline="") as ofp: ofp.write("ptf ~\n") [ofp.write("{0}\n".format(line.strip())) for line in header] ofp.flush() hfb_in[["lay", "irow1", "icol1", "irow2", "icol2", "tpl"]].to_csv( ofp, sep=" ", quotechar=" ", header=None, index=None, mode="a" ) # make a lookup for lining up the necessary files to # perform multiplication with the helpers.apply_hfb_pars() function # which must be added to the forward run script with open(os.path.join(m.model_ws, "hfb6_pars.csv"), "w") as ofp: ofp.write("org_file,mlt_file,model_file\n") ofp.write( "{0},{1},{2}\n".format( os.path.join(m.model_ws, "hfb6_org", m.hfb6.file_name[0]), os.path.join( m.model_ws, "hfb6_mlt", os.path.basename(tpl_file).replace(".tpl", ""), ), hfb_file, ) ) return hfb_mults, tpl_file def write_hfb_template(m): """write a template file for an hfb (yuck!) Args: m (`flopy.modflow.Modflow`): a model instance with an HFB package Returns: tuple containing - **str**: name of the template file that was created - **pandas.DataFrame**: a dataframe with use control file info for the HFB parameters """ assert m.hfb6 is not None hfb_file = os.path.join(m.model_ws, m.hfb6.file_name[0]) assert os.path.exists(hfb_file), "couldn't find hfb_file {0}".format(hfb_file) f_in = open(hfb_file, "r") tpl_file = hfb_file + ".tpl" f_tpl = open(tpl_file, "w") f_tpl.write("ptf ~\n") parnme, parval1, xs, ys = [], [], [], [] iis, jjs, kks = [], [], [] xc = m.sr.xcentergrid yc = m.sr.ycentergrid while True: line = f_in.readline() if line == "": break f_tpl.write(line) if not line.startswith("#"): raw = line.strip().split() nphfb = int(raw[0]) mxfb = int(raw[1]) nhfbnp = int(raw[2]) if nphfb > 0 or mxfb > 0: raise Exception("not supporting terrible HFB pars") for i in range(nhfbnp): line = f_in.readline() if line == "": raise Exception("EOF") raw = line.strip().split() k = int(raw[0]) - 1 i = int(raw[1]) - 1 j = int(raw[2]) - 1 pn = "hb{0:02}{1:04d}{2:04}".format(k, i, j) pv = float(raw[5]) raw[5] = "~ {0} ~".format(pn) line = " ".join(raw) + "\n" f_tpl.write(line) parnme.append(pn) parval1.append(pv) xs.append(xc[i, j]) ys.append(yc[i, j]) iis.append(i) jjs.append(j) kks.append(k) break f_tpl.close() f_in.close() df = pd.DataFrame( { "parnme": parnme, "parval1": parval1, "x": xs, "y": ys, "i": iis, "j": jjs, "k": kks, }, index=parnme, ) df.loc[:, "pargp"] = "hfb_hydfac" df.loc[:, "parubnd"] = df.parval1.max() * 10.0 df.loc[:, "parlbnd"] = df.parval1.min() * 0.1 return tpl_file, df class GsfReader: """ a helper class to read a standard modflow-usg gsf file Args: gsffilename (`str`): filename """ def __init__(self, gsffilename): with open(gsffilename, "r") as f: self.read_data = f.readlines() self.nnode, self.nlay, self.iz, self.ic = [ int(n) for n in self.read_data[1].split() ] self.nvertex = int(self.read_data[2]) def get_vertex_coordinates(self): """ Returns: Dictionary containing list of x, y and z coordinates for each vertex """ # vdata = self.read_data[3:self.nvertex+3] vertex_coords = {} for vert in range(self.nvertex): x, y, z = self.read_data[3 + vert].split() vertex_coords[vert + 1] = [float(x), float(y), float(z)] return vertex_coords def get_node_data(self): """ Returns: nodedf: a pd.DataFrame containing Node information; Node, X, Y, Z, layer, numverts, vertidx """ node_data = [] for node in range(self.nnode): nid, x, y, z, lay, numverts = self.read_data[ self.nvertex + 3 + node ].split()[:6] # vertidx = {'ivertex': [int(n) for n in self.read_data[self.nvertex+3 + node].split()[6:]]} vertidx = [ int(n) for n in self.read_data[self.nvertex + 3 + node].split()[6:] ] node_data.append( [ int(nid), float(x), float(y), float(z), int(lay), int(numverts), vertidx, ] ) nodedf = pd.DataFrame( node_data, columns=["node", "x", "y", "z", "layer", "numverts", "vertidx"] ) return nodedf def get_node_coordinates(self, zcoord=False, zero_based=False): """ Args: zcoord (`bool`): flag to add z coord to coordinates. Default is False zero_based (`bool`): flag to subtract one from the node numbers in the returned node_coords dict. This is needed to support PstFrom. Default is False Returns: node_coords: Dictionary containing x and y coordinates for each node """ node_coords = {} for node in range(self.nnode): nid, x, y, z, lay, numverts = self.read_data[ self.nvertex + 3 + node ].split()[:6] nid = int(nid) if zero_based: nid -= 1 node_coords[nid] = [float(x), float(y)] if zcoord: node_coords[nid] += [float(z)] return node_coords
bsd-3-clause
DonBeo/scikit-learn
sklearn/utils/tests/test_class_weight.py
14
6559
import numpy as np from sklearn.utils.class_weight import compute_class_weight from sklearn.utils.class_weight import compute_sample_weight from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_equal def test_compute_class_weight(): # Test (and demo) compute_class_weight. y = np.asarray([2, 2, 2, 3, 3, 4]) classes = np.unique(y) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_true(cw[0] < cw[1] < cw[2]) def test_compute_class_weight_not_present(): # Raise error when y does not contain all class labels classes = np.arange(4) y = np.asarray([0, 0, 0, 1, 1, 2]) assert_raises(ValueError, compute_class_weight, "auto", classes, y) def test_compute_class_weight_auto_negative(): # Test compute_class_weight when labels are negative # Test with balanced class labels. classes = np.array([-2, -1, 0]) y = np.asarray([-1, -1, 0, 0, -2, -2]) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_equal(len(cw), len(classes)) assert_array_almost_equal(cw, np.array([1., 1., 1.])) # Test with unbalanced class labels. y = np.asarray([-1, 0, 0, -2, -2, -2]) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_equal(len(cw), len(classes)) assert_array_almost_equal(cw, np.array([0.545, 1.636, 0.818]), decimal=3) def test_compute_class_weight_auto_unordered(): # Test compute_class_weight when classes are unordered classes = np.array([1, 0, 3]) y = np.asarray([1, 0, 0, 3, 3, 3]) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_equal(len(cw), len(classes)) assert_array_almost_equal(cw, np.array([1.636, 0.818, 0.545]), decimal=3) def test_compute_sample_weight(): # Test (and demo) compute_sample_weight. # Test with balanced classes y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with user-defined weights sample_weight = compute_sample_weight({1: 2, 2: 1}, y) assert_array_almost_equal(sample_weight, [2., 2., 2., 1., 1., 1.]) # Test with column vector of balanced classes y = np.asarray([[1], [1], [1], [2], [2], [2]]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with unbalanced classes y = np.asarray([1, 1, 1, 2, 2, 2, 3]) sample_weight = compute_sample_weight("auto", y) expected = np.asarray([.6, .6, .6, .6, .6, .6, 1.8]) assert_array_almost_equal(sample_weight, expected) # Test with `None` weights sample_weight = compute_sample_weight(None, y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 1.]) # Test with multi-output of balanced classes y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with multi-output with user-defined weights y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) sample_weight = compute_sample_weight([{1: 2, 2: 1}, {0: 1, 1: 2}], y) assert_array_almost_equal(sample_weight, [2., 2., 2., 2., 2., 2.]) # Test with multi-output of unbalanced classes y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [3, -1]]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, expected ** 2) def test_compute_sample_weight_with_subsample(): # Test compute_sample_weight with subsamples specified. # Test with balanced classes and all samples present y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with column vector of balanced classes and all samples present y = np.asarray([[1], [1], [1], [2], [2], [2]]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with a subsample y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y, range(4)) assert_array_almost_equal(sample_weight, [.5, .5, .5, 1.5, 1.5, 1.5]) # Test with a bootstrap subsample y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y, [0, 1, 1, 2, 2, 3]) expected = np.asarray([1/3., 1/3., 1/3., 5/3., 5/3., 5/3.]) assert_array_almost_equal(sample_weight, expected) # Test with a bootstrap subsample for multi-output y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) sample_weight = compute_sample_weight("auto", y, [0, 1, 1, 2, 2, 3]) assert_array_almost_equal(sample_weight, expected ** 2) # Test with a missing class y = np.asarray([1, 1, 1, 2, 2, 2, 3]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.]) # Test with a missing class for multi-output y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [2, 2]]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.]) def test_compute_sample_weight_errors(): # Test compute_sample_weight raises errors expected. # Invalid preset string y = np.asarray([1, 1, 1, 2, 2, 2]) y_ = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) assert_raises(ValueError, compute_sample_weight, "ni", y) assert_raises(ValueError, compute_sample_weight, "ni", y, range(4)) assert_raises(ValueError, compute_sample_weight, "ni", y_) assert_raises(ValueError, compute_sample_weight, "ni", y_, range(4)) # Not "auto" for subsample assert_raises(ValueError, compute_sample_weight, {1: 2, 2: 1}, y, range(4)) # Not a list or preset for multi-output assert_raises(ValueError, compute_sample_weight, {1: 2, 2: 1}, y_) # Incorrect length list for multi-output assert_raises(ValueError, compute_sample_weight, [{1: 2, 2: 1}], y_)
bsd-3-clause
balazssimon/ml-playground
udemy/lazyprogrammer/reinforcement-learning-python/approx_mc_prediction.py
1
2661
import numpy as np import matplotlib.pyplot as plt from grid_world import standard_grid, negative_grid from iterative_policy_evaluation import print_values, print_policy # NOTE: this is only policy evaluation, not optimization # we'll try to obtain the same result as our other MC script from monte_carlo_random import random_action, play_game, SMALL_ENOUGH, GAMMA, ALL_POSSIBLE_ACTIONS LEARNING_RATE = 0.001 if __name__ == '__main__': # use the standard grid again (0 for every step) so that we can compare # to iterative policy evaluation grid = standard_grid() # print rewards print("rewards:") print_values(grid.rewards, grid) # state -> action # found by policy_iteration_random on standard_grid # MC method won't get exactly this, but should be close # values: # --------------------------- # 0.43| 0.56| 0.72| 0.00| # --------------------------- # 0.33| 0.00| 0.21| 0.00| # --------------------------- # 0.25| 0.18| 0.11| -0.17| # policy: # --------------------------- # R | R | R | | # --------------------------- # U | | U | | # --------------------------- # U | L | U | L | policy = { (2, 0): 'U', (1, 0): 'U', (0, 0): 'R', (0, 1): 'R', (0, 2): 'R', (1, 2): 'U', (2, 1): 'L', (2, 2): 'U', (2, 3): 'L', } # initialize theta # our model is V_hat = theta.dot(x) # where x = [row, col, row*col, 1] - 1 for bias term theta = np.random.randn(4) / 2 def s2x(s): return np.array([s[0] - 1, s[1] - 1.5, s[0]*s[1] - 3, 1]) # repeat until convergence deltas = [] t = 1.0 for it in range(20000): if it % 100 == 0: t += 0.01 alpha = LEARNING_RATE/t # generate an episode using pi biggest_change = 0 states_and_returns = play_game(grid, policy) seen_states = set() for s, G in states_and_returns: # check if we have already seen s # called "first-visit" MC policy evaluation if s not in seen_states: old_theta = theta.copy() x = s2x(s) V_hat = theta.dot(x) # grad(V_hat) wrt theta = x theta += alpha*(G - V_hat)*x biggest_change = max(biggest_change, np.abs(old_theta - theta).sum()) seen_states.add(s) deltas.append(biggest_change) plt.plot(deltas) plt.show() # obtain predicted values V = {} states = grid.all_states() for s in states: if s in grid.actions: V[s] = theta.dot(s2x(s)) else: # terminal state or state we can't otherwise get to V[s] = 0 print("values:") print_values(V, grid) print("policy:") print_policy(policy, grid)
apache-2.0
mblondel/scikit-learn
sklearn/utils/tests/test_utils.py
23
6045
import warnings import numpy as np import scipy.sparse as sp from scipy.linalg import pinv2 from sklearn.utils.testing import (assert_equal, assert_raises, assert_true, assert_almost_equal, assert_array_equal, SkipTest) from sklearn.utils import check_random_state from sklearn.utils import deprecated from sklearn.utils import resample from sklearn.utils import safe_mask from sklearn.utils import column_or_1d from sklearn.utils import safe_indexing from sklearn.utils import shuffle from sklearn.utils.extmath import pinvh from sklearn.utils.mocking import MockDataFrame def test_make_rng(): """Check the check_random_state utility function behavior""" assert_true(check_random_state(None) is np.random.mtrand._rand) assert_true(check_random_state(np.random) is np.random.mtrand._rand) rng_42 = np.random.RandomState(42) assert_true(check_random_state(42).randint(100) == rng_42.randint(100)) rng_42 = np.random.RandomState(42) assert_true(check_random_state(rng_42) is rng_42) rng_42 = np.random.RandomState(42) assert_true(check_random_state(43).randint(100) != rng_42.randint(100)) assert_raises(ValueError, check_random_state, "some invalid seed") def test_resample_noarg(): """Border case not worth mentioning in doctests""" assert_true(resample() is None) def test_deprecated(): """Test whether the deprecated decorator issues appropriate warnings""" # Copied almost verbatim from http://docs.python.org/library/warnings.html # First a function... with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") @deprecated() def ham(): return "spam" spam = ham() assert_equal(spam, "spam") # function must remain usable assert_equal(len(w), 1) assert_true(issubclass(w[0].category, DeprecationWarning)) assert_true("deprecated" in str(w[0].message).lower()) # ... then a class. with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") @deprecated("don't use this") class Ham(object): SPAM = 1 ham = Ham() assert_true(hasattr(ham, "SPAM")) assert_equal(len(w), 1) assert_true(issubclass(w[0].category, DeprecationWarning)) assert_true("deprecated" in str(w[0].message).lower()) def test_resample_value_errors(): """Check that invalid arguments yield ValueError""" assert_raises(ValueError, resample, [0], [0, 1]) assert_raises(ValueError, resample, [0, 1], [0, 1], n_samples=3) assert_raises(ValueError, resample, [0, 1], [0, 1], meaning_of_life=42) def test_safe_mask(): random_state = check_random_state(0) X = random_state.rand(5, 4) X_csr = sp.csr_matrix(X) mask = [False, False, True, True, True] mask = safe_mask(X, mask) assert_equal(X[mask].shape[0], 3) mask = safe_mask(X_csr, mask) assert_equal(X_csr[mask].shape[0], 3) def test_pinvh_simple_real(): a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 10]], dtype=np.float64) a = np.dot(a, a.T) a_pinv = pinvh(a) assert_almost_equal(np.dot(a, a_pinv), np.eye(3)) def test_pinvh_nonpositive(): a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float64) a = np.dot(a, a.T) u, s, vt = np.linalg.svd(a) s[0] *= -1 a = np.dot(u * s, vt) # a is now symmetric non-positive and singular a_pinv = pinv2(a) a_pinvh = pinvh(a) assert_almost_equal(a_pinv, a_pinvh) def test_pinvh_simple_complex(): a = (np.array([[1, 2, 3], [4, 5, 6], [7, 8, 10]]) + 1j * np.array([[10, 8, 7], [6, 5, 4], [3, 2, 1]])) a = np.dot(a, a.conj().T) a_pinv = pinvh(a) assert_almost_equal(np.dot(a, a_pinv), np.eye(3)) def test_column_or_1d(): EXAMPLES = [ ("binary", ["spam", "egg", "spam"]), ("binary", [0, 1, 0, 1]), ("continuous", np.arange(10) / 20.), ("multiclass", [1, 2, 3]), ("multiclass", [0, 1, 2, 2, 0]), ("multiclass", [[1], [2], [3]]), ("multilabel-indicator", [[0, 1, 0], [0, 0, 1]]), ("multiclass-multioutput", [[1, 2, 3]]), ("multiclass-multioutput", [[1, 1], [2, 2], [3, 1]]), ("multiclass-multioutput", [[5, 1], [4, 2], [3, 1]]), ("multiclass-multioutput", [[1, 2, 3]]), ("continuous-multioutput", np.arange(30).reshape((-1, 3))), ] for y_type, y in EXAMPLES: if y_type in ["binary", 'multiclass', "continuous"]: assert_array_equal(column_or_1d(y), np.ravel(y)) else: assert_raises(ValueError, column_or_1d, y) def test_safe_indexing(): X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] inds = np.array([1, 2]) X_inds = safe_indexing(X, inds) X_arrays = safe_indexing(np.array(X), inds) assert_array_equal(np.array(X_inds), X_arrays) assert_array_equal(np.array(X_inds), np.array(X)[inds]) def test_safe_indexing_pandas(): try: import pandas as pd except ImportError: raise SkipTest("Pandas not found") X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) X_df = pd.DataFrame(X) inds = np.array([1, 2]) X_df_indexed = safe_indexing(X_df, inds) X_indexed = safe_indexing(X_df, inds) assert_array_equal(np.array(X_df_indexed), X_indexed) def test_safe_indexing_mock_pandas(): X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) X_df = MockDataFrame(X) inds = np.array([1, 2]) X_df_indexed = safe_indexing(X_df, inds) X_indexed = safe_indexing(X_df, inds) assert_array_equal(np.array(X_df_indexed), X_indexed) def test_shuffle_on_ndim_equals_three(): def to_tuple(A): # to make the inner arrays hashable return tuple(tuple(tuple(C) for C in B) for B in A) A = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # A.shape = (2,2,2) S = set(to_tuple(A)) shuffle(A) # shouldn't raise a ValueError for dim = 3 assert_equal(set(to_tuple(A)), S)
bsd-3-clause
balazssimon/ml-playground
udemy/lazyprogrammer/reinforcement-learning-python/comparing_explore_exploit_methods.py
1
2913
import numpy as np import matplotlib.pyplot as plt from comparing_epsilons import Bandit from optimistic_initial_values import run_experiment as run_experiment_oiv from ucb1 import run_experiment as run_experiment_ucb class BayesianBandit: def __init__(self, true_mean): self.true_mean = true_mean # parameters for mu - prior is N(0,1) self.predicted_mean = 0 self.lambda_ = 1 self.sum_x = 0 # for convenience self.tau = 1 def pull(self): return np.random.randn() + self.true_mean def sample(self): return np.random.randn() / np.sqrt(self.lambda_) + self.predicted_mean def update(self, x): self.lambda_ += self.tau self.sum_x += x self.predicted_mean = self.tau*self.sum_x / self.lambda_ def run_experiment_decaying_epsilon(m1, m2, m3, N): bandits = [Bandit(m1), Bandit(m2), Bandit(m3)] data = np.empty(N) for i in range(N): # epsilon greedy p = np.random.random() if p < 1.0/(i+1): j = np.random.choice(3) else: j = np.argmax([b.mean for b in bandits]) x = bandits[j].pull() bandits[j].update(x) # for the plot data[i] = x cumulative_average = np.cumsum(data) / (np.arange(N) + 1) # plot moving average ctr plt.plot(cumulative_average) plt.plot(np.ones(N)*m1) plt.plot(np.ones(N)*m2) plt.plot(np.ones(N)*m3) plt.xscale('log') plt.show() for b in bandits: print(b.mean) return cumulative_average def run_experiment(m1, m2, m3, N): bandits = [BayesianBandit(m1), BayesianBandit(m2), BayesianBandit(m3)] data = np.empty(N) for i in range(N): # optimistic initial values j = np.argmax([b.sample() for b in bandits]) x = bandits[j].pull() bandits[j].update(x) # for the plot data[i] = x cumulative_average = np.cumsum(data) / (np.arange(N) + 1) # plot moving average ctr plt.plot(cumulative_average) plt.plot(np.ones(N)*m1) plt.plot(np.ones(N)*m2) plt.plot(np.ones(N)*m3) plt.xscale('log') plt.show() return cumulative_average if __name__ == '__main__': m1 = 1.0 m2 = 2.0 m3 = 3.0 eps = run_experiment_decaying_epsilon(m1, m2, m3, 100000) oiv = run_experiment_oiv(m1, m2, m3, 100000) ucb = run_experiment_ucb(m1, m2, m3, 100000) bayes = run_experiment(m1, m2, m3, 100000) # log scale plot plt.plot(eps, label='decaying-epsilon-greedy') plt.plot(oiv, label='optimistic') plt.plot(ucb, label='ucb1') plt.plot(bayes, label='bayesian') plt.legend() plt.xscale('log') plt.show() # linear plot plt.plot(eps, label='decaying-epsilon-greedy') plt.plot(oiv, label='optimistic') plt.plot(ucb, label='ucb1') plt.plot(bayes, label='bayesian') plt.legend() plt.show()
apache-2.0
neuropoly/spinalcordtoolbox
spinalcordtoolbox/scripts/sct_maths.py
1
20433
#!/usr/bin/env python ######################################################################################### # # Perform mathematical operations on images # # --------------------------------------------------------------------------------------- # Copyright (c) 2015 Polytechnique Montreal <www.neuro.polymtl.ca> # Authors: Julien Cohen-Adad, Sara Dupont # # About the license: see the file LICENSE.TXT ######################################################################################### import os import sys import pickle import gzip import numpy as np import matplotlib import matplotlib.pyplot as plt import spinalcordtoolbox.math as sct_math from spinalcordtoolbox.image import Image from spinalcordtoolbox.utils.shell import SCTArgumentParser, Metavar, list_type, display_viewer_syntax from spinalcordtoolbox.utils.sys import init_sct, printv, set_global_loglevel from spinalcordtoolbox.utils.fs import extract_fname def get_parser(): parser = SCTArgumentParser( description='Perform mathematical operations on images. Some inputs can be either a number or a 4d image or ' 'several 3d images separated with ","' ) mandatory = parser.add_argument_group("MANDATORY ARGUMENTS") mandatory.add_argument( "-i", metavar=Metavar.file, help="Input file. Example: data.nii.gz", required=True) mandatory.add_argument( "-o", metavar=Metavar.file, help='Output file. Example: data_mean.nii.gz', required=True) optional = parser.add_argument_group("OPTIONAL ARGUMENTS") optional.add_argument( "-h", "--help", action="help", help="Show this help message and exit") basic = parser.add_argument_group('BASIC OPERATIONS') basic.add_argument( "-add", metavar='', nargs="+", help='Add following input. Can be a number or multiple images (separated with space).', required=False) basic.add_argument( "-sub", metavar='', nargs="+", help='Subtract following input. Can be a number or an image.', required=False) basic.add_argument( "-mul", metavar='', nargs="+", help='Multiply by following input. Can be a number or multiple images (separated with space).', required=False) basic.add_argument( "-div", metavar='', nargs="+", help='Divide by following input. Can be a number or an image.', required=False) basic.add_argument( '-mean', help='Average data across dimension.', required=False, choices=('x', 'y', 'z', 't')) basic.add_argument( '-rms', help='Compute root-mean-squared across dimension.', required=False, choices=('x', 'y', 'z', 't')) basic.add_argument( '-std', help='Compute STD across dimension.', required=False, choices=('x', 'y', 'z', 't')) basic.add_argument( "-bin", type=float, metavar=Metavar.float, help='Binarize image using specified threshold. Example: 0.5', required=False) thresholding = parser.add_argument_group("THRESHOLDING METHODS") thresholding.add_argument( '-otsu', type=int, metavar=Metavar.int, help='Threshold image using Otsu algorithm (from skimage). Specify the number of bins (e.g. 16, 64, 128)', required=False) thresholding.add_argument( "-adap", metavar=Metavar.list, type=list_type(',', int), help="R|Threshold image using Adaptive algorithm (from skimage). Provide 2 values separated by ',' that " "correspond to the parameters below. For example, '-adap 7,0' corresponds to a block size of 7 and an " "offset of 0.\n" " - Block size: Odd size of pixel neighborhood which is used to calculate the threshold value. \n" " - Offset: Constant subtracted from weighted mean of neighborhood to calculate the local threshold " "value. Suggested offset is 0.", required=False) thresholding.add_argument( "-otsu-median", metavar=Metavar.list, type=list_type(',', int), help="R|Threshold image using Median Otsu algorithm (from dipy). Provide 2 values separated by ',' that " "correspond to the parameters below. For example, '-otsu-median 3,5' corresponds to a filter size of 3 " "repeated over 5 iterations.\n" " - Size: Radius (in voxels) of the applied median filter.\n" " - Iterations: Number of passes of the median filter.", required=False) thresholding.add_argument( '-percent', type=int, help="Threshold image using percentile of its histogram.", metavar=Metavar.int, required=False) thresholding.add_argument( "-thr", type=float, help='Use following number to threshold image (zero below number).', metavar=Metavar.float, required=False) mathematical = parser.add_argument_group("MATHEMATICAL MORPHOLOGY") mathematical.add_argument( '-dilate', type=int, metavar=Metavar.int, help="Dilate binary or greyscale image with specified size. If shape={'square', 'cube'}: size corresponds to the length of " "an edge (size=1 has no effect). If shape={'disk', 'ball'}: size corresponds to the radius, not including " "the center element (size=0 has no effect).", required=False) mathematical.add_argument( '-erode', type=int, metavar=Metavar.int, help="Erode binary or greyscale image with specified size. If shape={'square', 'cube'}: size corresponds to the length of " "an edge (size=1 has no effect). If shape={'disk', 'ball'}: size corresponds to the radius, not including " "the center element (size=0 has no effect).", required=False) mathematical.add_argument( '-shape', help="R|Shape of the structuring element for the mathematical morphology operation. Default: ball.\n" "If a 2D shape {'disk', 'square'} is selected, -dim must be specified.", required=False, choices=('square', 'cube', 'disk', 'ball'), default='ball') mathematical.add_argument( '-dim', type=int, help="Dimension of the array which 2D structural element will be orthogonal to. For example, if you wish to " "apply a 2D disk kernel in the X-Y plane, leaving Z unaffected, parameters will be: shape=disk, dim=2.", required=False, choices=(0, 1, 2)) filtering = parser.add_argument_group("FILTERING METHODS") filtering.add_argument( "-smooth", metavar=Metavar.list, type=list_type(',', float), help='Gaussian smoothing filtering. Supply values for standard deviations in mm. If a single value is provided, ' 'it will be applied to each axis of the image. If multiple values are provided, there must be one value ' 'per image axis. (Examples: "-smooth 2.0,3.0,2.0" (3D image), "-smooth 2.0" (any-D image)).', required=False) filtering.add_argument( '-laplacian', metavar=Metavar.list, type=list_type(',', float), help='Laplacian filtering. Supply values for standard deviations in mm. If a single value is provided, it will ' 'be applied to each axis of the image. If multiple values are provided, there must be one value per ' 'image axis. (Examples: "-laplacian 2.0,3.0,2.0" (3D image), "-laplacian 2.0" (any-D image)).', required=False) filtering.add_argument( '-denoise', help='R|Non-local means adaptative denoising from P. Coupe et al. as implemented in dipy. Separate with ". Example: p=1,b=3\n' ' p: (patch radius) similar patches in the non-local means are searched for locally, inside a cube of side 2*p+1 centered at each voxel of interest. Default: p=1\n' ' b: (block radius) the size of the block to be used (2*b+1) in the blockwise non-local means implementation. Default: b=5 ' ' Note, block radius must be smaller than the smaller image dimension: default value is lowered for small images)\n' 'To use default parameters, write -denoise 1', required=False) similarity = parser.add_argument_group("SIMILARITY METRIC") similarity.add_argument( '-mi', metavar=Metavar.file, help='Compute the mutual information (MI) between both input files (-i and -mi) as in: ' 'http://scikit-learn.org/stable/modules/generated/sklearn.metrics.mutual_info_score.html', required=False) similarity.add_argument( '-minorm', metavar=Metavar.file, help='Compute the normalized mutual information (MI) between both input files (-i and -mi) as in: ' 'http://scikit-learn.org/stable/modules/generated/sklearn.metrics.normalized_mutual_info_score.html', required=False) similarity.add_argument( '-corr', metavar=Metavar.file, help='Compute the cross correlation (CC) between both input files (-i and -cc).', required=False) misc = parser.add_argument_group("MISC") misc.add_argument( '-symmetrize', type=int, help='Symmetrize data along the specified dimension.', required=False, choices=(0, 1, 2)) misc.add_argument( '-type', required=False, help='Output type.', choices=('uint8', 'int16', 'int32', 'float32', 'complex64', 'float64', 'int8', 'uint16', 'uint32', 'int64', 'uint64')) optional.add_argument( '-v', metavar=Metavar.int, type=int, choices=[0, 1, 2], default=1, # Values [0, 1, 2] map to logging levels [WARNING, INFO, DEBUG], but are also used as "if verbose == #" in API help="Verbosity. 0: Display only errors/warnings, 1: Errors/warnings + info messages, 2: Debug mode") return parser # MAIN # ========================================================================================== def main(argv=None): """ Main function :param argv: :return: """ parser = get_parser() arguments = parser.parse_args(argv) verbose = arguments.v set_global_loglevel(verbose=verbose) dim_list = ['x', 'y', 'z', 't'] fname_in = arguments.i fname_out = arguments.o output_type = arguments.type # Open file(s) im = Image(fname_in) data = im.data # 3d or 4d numpy array dim = im.dim # run command if arguments.otsu is not None: param = arguments.otsu data_out = sct_math.otsu(data, param) elif arguments.adap is not None: param = arguments.adap data_out = sct_math.adap(data, param[0], param[1]) elif arguments.otsu_median is not None: param = arguments.otsu_median data_out = sct_math.otsu_median(data, param[0], param[1]) elif arguments.thr is not None: param = arguments.thr data_out = sct_math.threshold(data, param) elif arguments.percent is not None: param = arguments.percent data_out = sct_math.perc(data, param) elif arguments.bin is not None: bin_thr = arguments.bin data_out = sct_math.binarize(data, bin_thr=bin_thr) elif arguments.add is not None: data2 = get_data_or_scalar(arguments.add, data) data_concat = sct_math.concatenate_along_4th_dimension(data, data2) data_out = np.sum(data_concat, axis=3) elif arguments.sub is not None: data2 = get_data_or_scalar(arguments.sub, data) data_out = data - data2 elif arguments.laplacian is not None: sigmas = arguments.laplacian if len(sigmas) == 1: sigmas = [sigmas for i in range(len(data.shape))] elif len(sigmas) != len(data.shape): printv(parser.error('ERROR: -laplacian need the same number of inputs as the number of image dimension OR only one input')) # adjust sigma based on voxel size sigmas = [sigmas[i] / dim[i + 4] for i in range(3)] # smooth data data_out = sct_math.laplacian(data, sigmas) elif arguments.mul is not None: data2 = get_data_or_scalar(arguments.mul, data) data_concat = sct_math.concatenate_along_4th_dimension(data, data2) data_out = np.prod(data_concat, axis=3) elif arguments.div is not None: data2 = get_data_or_scalar(arguments.div, data) data_out = np.divide(data, data2) elif arguments.mean is not None: dim = dim_list.index(arguments.mean) if dim + 1 > len(np.shape(data)): # in case input volume is 3d and dim=t data = data[..., np.newaxis] data_out = np.mean(data, dim) elif arguments.rms is not None: dim = dim_list.index(arguments.rms) if dim + 1 > len(np.shape(data)): # in case input volume is 3d and dim=t data = data[..., np.newaxis] data_out = np.sqrt(np.mean(np.square(data.astype(float)), dim)) elif arguments.std is not None: dim = dim_list.index(arguments.std) if dim + 1 > len(np.shape(data)): # in case input volume is 3d and dim=t data = data[..., np.newaxis] data_out = np.std(data, dim, ddof=1) elif arguments.smooth is not None: sigmas = arguments.smooth if len(sigmas) == 1: sigmas = [sigmas[0] for i in range(len(data.shape))] elif len(sigmas) != len(data.shape): printv(parser.error('ERROR: -smooth need the same number of inputs as the number of image dimension OR only one input')) # adjust sigma based on voxel size sigmas = [sigmas[i] / dim[i + 4] for i in range(3)] # smooth data data_out = sct_math.smooth(data, sigmas) elif arguments.dilate is not None: if arguments.shape in ['disk', 'square'] and arguments.dim is None: printv(parser.error('ERROR: -dim is required for -dilate with 2D morphological kernel')) data_out = sct_math.dilate(data, size=arguments.dilate, shape=arguments.shape, dim=arguments.dim) elif arguments.erode is not None: if arguments.shape in ['disk', 'square'] and arguments.dim is None: printv(parser.error('ERROR: -dim is required for -erode with 2D morphological kernel')) data_out = sct_math.erode(data, size=arguments.erode, shape=arguments.shape, dim=arguments.dim) elif arguments.denoise is not None: # parse denoising arguments p, b = 1, 5 # default arguments list_denoise = (arguments.denoise).split(",") for i in list_denoise: if 'p' in i: p = int(i.split('=')[1]) if 'b' in i: b = int(i.split('=')[1]) data_out = sct_math.denoise_nlmeans(data, patch_radius=p, block_radius=b) elif arguments.symmetrize is not None: data_out = (data + data[list(range(data.shape[0] - 1, -1, -1)), :, :]) / float(2) elif arguments.mi is not None: # input 1 = from flag -i --> im # input 2 = from flag -mi im_2 = Image(arguments.mi) compute_similarity(im, im_2, fname_out, metric='mi', metric_full='Mutual information', verbose=verbose) data_out = None elif arguments.minorm is not None: im_2 = Image(arguments.minorm) compute_similarity(im, im_2, fname_out, metric='minorm', metric_full='Normalized Mutual information', verbose=verbose) data_out = None elif arguments.corr is not None: # input 1 = from flag -i --> im # input 2 = from flag -mi im_2 = Image(arguments.corr) compute_similarity(im, im_2, fname_out, metric='corr', metric_full='Pearson correlation coefficient', verbose=verbose) data_out = None # if no flag is set else: data_out = None printv(parser.error('ERROR: you need to specify an operation to do on the input image')) if data_out is not None: # Write output nii_out = Image(fname_in) # use header of input file nii_out.data = data_out nii_out.save(fname_out, dtype=output_type) # TODO: case of multiple outputs # assert len(data_out) == n_out # if n_in == n_out: # for im_in, d_out, fn_out in zip(nii, data_out, fname_out): # im_in.data = d_out # im_in.absolutepath = fn_out # if arguments.w is not None: # im_in.hdr.set_intent('vector', (), '') # im_in.save() # elif n_out == 1: # nii[0].data = data_out[0] # nii[0].absolutepath = fname_out[0] # if arguments.w is not None: # nii[0].hdr.set_intent('vector', (), '') # nii[0].save() # elif n_out > n_in: # for dat_out, name_out in zip(data_out, fname_out): # im_out = nii[0].copy() # im_out.data = dat_out # im_out.absolutepath = name_out # if arguments.w is not None: # im_out.hdr.set_intent('vector', (), '') # im_out.save() # else: # printv(parser.usage.generate(error='ERROR: not the correct numbers of inputs and outputs')) # display message if data_out is not None: display_viewer_syntax([fname_out], verbose=verbose) else: printv('\nDone! File created: ' + fname_out, verbose, 'info') def get_data(list_fname): """ Get data from list of file names :param list_fname: :return: 3D or 4D numpy array. """ try: nii = [Image(f_in) for f_in in list_fname] except Exception as e: printv(str(e), 1, 'error') # file does not exist, exit program data0 = nii[0].data data = nii[0].data # check that every images have same shape for i in range(1, len(nii)): if not np.shape(nii[i].data) == np.shape(data0): printv('\nWARNING: shape(' + list_fname[i] + ')=' + str(np.shape(nii[i].data)) + ' incompatible with shape(' + list_fname[0] + ')=' + str(np.shape(data0)), 1, 'warning') printv('\nERROR: All input images must have same dimensions.', 1, 'error') else: data = sct_math.concatenate_along_4th_dimension(data, nii[i].data) return data def get_data_or_scalar(argument, data_in): """ Get data from list of file names (scenario 1) or scalar (scenario 2) :param argument: list of file names of scalar :param data_in: if argument is scalar, use data to get np.shape :return: 3d or 4d numpy array """ # try to convert argument in float try: # build data2 with same shape as data data_out = data_in[:, :, :] * 0 + float(argument[0]) # if conversion fails, it should be a string (i.e. file name) except ValueError: data_out = get_data(argument) return data_out def compute_similarity(img1: Image, img2: Image, fname_out: str, metric: str, metric_full: str, verbose): """ Sanitize input and compute similarity metric between two images data. """ if img1.data.size != img2.data.size: raise ValueError(f"Input images don't have the same size! \nPlease use \"sct_register_multimodal -i im1.nii.gz -d im2.nii.gz -identity 1\" to put the input images in the same space") res, data1_1d, data2_1d = sct_math.compute_similarity(img1.data, img2.data, metric=metric) if verbose > 1: matplotlib.use('Agg') plt.plot(data1_1d, 'b') plt.plot(data2_1d, 'r') plt.title('Similarity: ' + metric_full + ' = ' + str(res)) plt.savefig('fig_similarity.png') path_out, filename_out, ext_out = extract_fname(fname_out) if ext_out not in ['.txt', '.pkl', '.pklz', '.pickle']: raise ValueError(f"The output file should a text file or a pickle file. Received extension: {ext_out}") if ext_out == '.txt': with open(fname_out, 'w') as f: f.write(metric_full + ': \n' + str(res)) elif ext_out == '.pklz': pickle.dump(res, gzip.open(fname_out, 'wb'), protocol=2) else: pickle.dump(res, open(fname_out, 'w'), protocol=2) if __name__ == "__main__": init_sct() main(sys.argv[1:])
mit
cdek11/PLS
Code/PLS_Algorithm_Optimized.py
2
5817
# coding: utf-8 # In[2]: # Code to implement the optimized version of the PLS Algorithm import pandas as pd import numpy as np import numba from numba import jit @jit def mean_center_scale(dataframe): '''Scale dataframe by subtracting mean and dividing by standard deviation''' dataframe = dataframe - dataframe.mean() dataframe = dataframe/dataframe.std() return dataframe @jit def y_pred(Y_pred, i,b_dictionary,t_hat_dictionary,q_new_dictionary): '''Find prediction for Y based on the number of components in this iteration''' for j in range(1,i+1): Y_pred = Y_pred + (b_dictionary[j]*t_hat_dictionary[j]).dot(q_new_dictionary[j].T) return Y_pred @jit def rmse(i,Y_true, Y_pred, response_std, RMSE_dictionary): '''Find training RMSE''' RMSE = np.sqrt(sum((Y_true - Y_pred)**2)/Y_true.shape[0]) RMSE_scaled = RMSE * response_std RMSE_dictionary[i] = RMSE_scaled return RMSE_dictionary @jit def core_pls(i,Y, X, q_new_dictionary, b_dictionary, t_hat_dictionary) : '''Core PLS algorithm''' #Here we have one variable in the Y block so q = 1 #and omit steps 5-8 q = 1 #For the X block, u = Y u = Y #random y column from Y #Step 1 w_old = np.dot(u.T,X)/np.dot(u.T,u) #Step 2 w_new = w_old/np.linalg.norm(w_old) #Step 3 t = np.dot(X,w_new.T)/np.dot(w_new,w_new.T) #Step 4 #For the Y block can be omitted if Y only has one variable q_old = np.dot(t.T,Y)/np.dot(t.T,t) #Step 5 q_new = q_old/np.linalg.norm(q_old) #Step 6 q_new_dictionary[i] = q_new u = np.dot(Y,q_new.T)/np.dot(q_new,q_new.T) #Step 7 #Step 8: Check convergence #Calculate the X loadings and rescale the scores and weights accordingly p = np.dot(t.T,X)/np.dot(t.T,t) #Step 9 p_new = p.T/np.linalg.norm(p.T) #Step 10 t_new = t/np.linalg.norm(p.T) #Step 11 w_new = w_old/np.linalg.norm(p) #Step 12 #Find the regression coefficient for b for th inner relation b = np.dot(u.T,t_new)/np.dot(t.T,t) #Step 13 b_dictionary[i] = b #Calculation of the residuals E_h = X - np.dot(t_new,p_new.T) F_h = Y - b.dot(t_new.T).T.dot(q) #WORKS BUT IS THIS RIGHT? #Set outer relation for the X block #Xres_dictionary[i] = E_h #MAYBE REMOVE X = E_h #Set the mixed relation for the Y block #Yres_dictionary[i] = F_h 3MAYBE REMOVE Y = F_h #Find estimated t hat t_hat = np.dot(E_h,w_new.T) t_hat_dictionary[i] = t_hat E_h = E_h - np.dot(t_hat,p_new.T) return X,Y, u, w_new, q_new, t_new, p_new, q_new_dictionary, t_hat_dictionary, b_dictionary,E_h, F_h def pls_optimized(path, path_test, predictors, response): '''Function that takes a dataframe and runs partial least squares on numeric predictors for a numeric response. Returns the residuals of the predictor (X block), response (Y block), and traininig RMSE''' ###TRAINING DATA combined = predictors #Load data data = pd.DataFrame.from_csv(path) combined.append(response) data = data[combined] response_std = data[response].std() #Subtract the mean and scale each column data = mean_center_scale(data) #Separate in to design matrix (X block) and response column vector (Y block) predictors.pop() X = data[predictors].as_matrix() Y = data[[response]].as_matrix() Y_true = Y #For prediction #Get rank of matrix rank = np.linalg.matrix_rank(X) u = Y #set initial u as Y Xres_dictionary = {} Yres_dictionary = {} q_new_dictionary ={} b_dictionary = {} t_hat_dictionary = {} t_hat_train_dictionary = {} t_hat_test_dictionary = {} RMSE_dictionary = {} RMSE_test_dictionary = {} ###TEST DATA #Load data data_test = pd.DataFrame.from_csv(path_test) combined.append(response) data_test = data_test[combined] response_std_test = data_test[response].std() #Subtract the mean and scale each column data_test = mean_center_scale(data_test) #Separate in to design matrix (X block) and response column vector (Y block) predictors.pop() X_test = data[predictors].as_matrix() Y_test = data[[response]].as_matrix() Y_true_test = Y_test #For prediction #Get rank of matrix rank_test = np.linalg.matrix_rank(X_test) #Iterate through each component for i in range(1,(rank+1)): Y_pred = np.zeros((Y_true.shape[0],1)) Y_pred_test = np.zeros((Y_true_test.shape[0],1)) #Core algo X,Y, u, w_new, q_new, t_new, p_new, q_new_dictionary, t_hat_dictionary, b_dictionary,E_h, F_h = core_pls(i,Y, X, q_new_dictionary, b_dictionary, t_hat_dictionary) #NEW Sum over different compenents for g in range(1,i+1): t_hat_train = np.dot(E_h,w_new.T) t_hat_train_dictionary[g] = t_hat_train E_h = E_h - np.dot(t_hat_train, p_new.T) Y_pred = y_pred(Y_pred, g,b_dictionary,t_hat_dictionary,q_new_dictionary) #Find training RMSE RMSE_dictionary = rmse(i,Y_true, Y_pred, response_std, RMSE_dictionary) #Set initial E_h as X_test data E_h_test = X_test #Sum over different compenents for k in range(1,i+1): t_hat_test = np.dot(E_h_test,w_new.T) t_hat_test_dictionary[k] = t_hat_test E_h_test = E_h_test - np.dot(t_hat_test, p_new.T) Y_pred_test = y_pred(Y_pred_test, k,b_dictionary,t_hat_test_dictionary,q_new_dictionary) #Find test RMSE RMSE_test_dictionary = rmse(i,Y_true_test, Y_pred_test, response_std_test, RMSE_test_dictionary) return RMSE_dictionary, RMSE_test_dictionary
mit
kaichogami/scikit-learn
sklearn/utils/multiclass.py
40
12966
# Author: Arnaud Joly, Joel Nothman, Hamzeh Alsalhi # # License: BSD 3 clause """ Multi-class / multi-label utility function ========================================== """ from __future__ import division from collections import Sequence from itertools import chain from scipy.sparse import issparse from scipy.sparse.base import spmatrix from scipy.sparse import dok_matrix from scipy.sparse import lil_matrix import numpy as np from ..externals.six import string_types from .validation import check_array from ..utils.fixes import bincount from ..utils.fixes import array_equal def _unique_multiclass(y): if hasattr(y, '__array__'): return np.unique(np.asarray(y)) else: return set(y) def _unique_indicator(y): return np.arange(check_array(y, ['csr', 'csc', 'coo']).shape[1]) _FN_UNIQUE_LABELS = { 'binary': _unique_multiclass, 'multiclass': _unique_multiclass, 'multilabel-indicator': _unique_indicator, } def unique_labels(*ys): """Extract an ordered array of unique labels We don't allow: - mix of multilabel and multiclass (single label) targets - mix of label indicator matrix and anything else, because there are no explicit labels) - mix of label indicator matrices of different sizes - mix of string and integer labels At the moment, we also don't allow "multiclass-multioutput" input type. Parameters ---------- *ys : array-likes, Returns ------- out : numpy array of shape [n_unique_labels] An ordered array of unique labels. Examples -------- >>> from sklearn.utils.multiclass import unique_labels >>> unique_labels([3, 5, 5, 5, 7, 7]) array([3, 5, 7]) >>> unique_labels([1, 2, 3, 4], [2, 2, 3, 4]) array([1, 2, 3, 4]) >>> unique_labels([1, 2, 10], [5, 11]) array([ 1, 2, 5, 10, 11]) """ if not ys: raise ValueError('No argument has been passed.') # Check that we don't mix label format ys_types = set(type_of_target(x) for x in ys) if ys_types == set(["binary", "multiclass"]): ys_types = set(["multiclass"]) if len(ys_types) > 1: raise ValueError("Mix type of y not allowed, got types %s" % ys_types) label_type = ys_types.pop() # Check consistency for the indicator format if (label_type == "multilabel-indicator" and len(set(check_array(y, ['csr', 'csc', 'coo']).shape[1] for y in ys)) > 1): raise ValueError("Multi-label binary indicator input with " "different numbers of labels") # Get the unique set of labels _unique_labels = _FN_UNIQUE_LABELS.get(label_type, None) if not _unique_labels: raise ValueError("Unknown label type: %s" % repr(ys)) ys_labels = set(chain.from_iterable(_unique_labels(y) for y in ys)) # Check that we don't mix string type with number type if (len(set(isinstance(label, string_types) for label in ys_labels)) > 1): raise ValueError("Mix of label input types (string and number)") return np.array(sorted(ys_labels)) def _is_integral_float(y): return y.dtype.kind == 'f' and np.all(y.astype(int) == y) def is_multilabel(y): """ Check if ``y`` is in a multilabel format. Parameters ---------- y : numpy array of shape [n_samples] Target values. Returns ------- out : bool, Return ``True``, if ``y`` is in a multilabel format, else ```False``. Examples -------- >>> import numpy as np >>> from sklearn.utils.multiclass import is_multilabel >>> is_multilabel([0, 1, 0, 1]) False >>> is_multilabel([[1], [0, 2], []]) False >>> is_multilabel(np.array([[1, 0], [0, 0]])) True >>> is_multilabel(np.array([[1], [0], [0]])) False >>> is_multilabel(np.array([[1, 0, 0]])) True """ if hasattr(y, '__array__'): y = np.asarray(y) if not (hasattr(y, "shape") and y.ndim == 2 and y.shape[1] > 1): return False if issparse(y): if isinstance(y, (dok_matrix, lil_matrix)): y = y.tocsr() return (len(y.data) == 0 or np.unique(y.data).size == 1 and (y.dtype.kind in 'biu' or # bool, int, uint _is_integral_float(np.unique(y.data)))) else: labels = np.unique(y) return len(labels) < 3 and (y.dtype.kind in 'biu' or # bool, int, uint _is_integral_float(labels)) def check_classification_targets(y): """Ensure that target y is of a non-regression type. Only the following target types (as defined in type_of_target) are allowed: 'binary', 'multiclass', 'multiclass-multioutput', 'multilabel-indicator', 'multilabel-sequences' Parameters ---------- y : array-like """ y_type = type_of_target(y) if y_type not in ['binary', 'multiclass', 'multiclass-multioutput', 'multilabel-indicator', 'multilabel-sequences']: raise ValueError("Unknown label type: %r" % y_type) def type_of_target(y): """Determine the type of data indicated by target `y` Parameters ---------- y : array-like Returns ------- target_type : string One of: * 'continuous': `y` is an array-like of floats that are not all integers, and is 1d or a column vector. * 'continuous-multioutput': `y` is a 2d array of floats that are not all integers, and both dimensions are of size > 1. * 'binary': `y` contains <= 2 discrete values and is 1d or a column vector. * 'multiclass': `y` contains more than two discrete values, is not a sequence of sequences, and is 1d or a column vector. * 'multiclass-multioutput': `y` is a 2d array that contains more than two discrete values, is not a sequence of sequences, and both dimensions are of size > 1. * 'multilabel-indicator': `y` is a label indicator matrix, an array of two dimensions with at least two columns, and at most 2 unique values. * 'unknown': `y` is array-like but none of the above, such as a 3d array, sequence of sequences, or an array of non-sequence objects. Examples -------- >>> import numpy as np >>> type_of_target([0.1, 0.6]) 'continuous' >>> type_of_target([1, -1, -1, 1]) 'binary' >>> type_of_target(['a', 'b', 'a']) 'binary' >>> type_of_target([1.0, 2.0]) 'binary' >>> type_of_target([1, 0, 2]) 'multiclass' >>> type_of_target([1.0, 0.0, 3.0]) 'multiclass' >>> type_of_target(['a', 'b', 'c']) 'multiclass' >>> type_of_target(np.array([[1, 2], [3, 1]])) 'multiclass-multioutput' >>> type_of_target([[1, 2]]) 'multiclass-multioutput' >>> type_of_target(np.array([[1.5, 2.0], [3.0, 1.6]])) 'continuous-multioutput' >>> type_of_target(np.array([[0, 1], [1, 1]])) 'multilabel-indicator' """ valid = ((isinstance(y, (Sequence, spmatrix)) or hasattr(y, '__array__')) and not isinstance(y, string_types)) if not valid: raise ValueError('Expected array-like (array or non-string sequence), ' 'got %r' % y) if is_multilabel(y): return 'multilabel-indicator' try: y = np.asarray(y) except ValueError: # Known to fail in numpy 1.3 for array of arrays return 'unknown' # The old sequence of sequences format try: if (not hasattr(y[0], '__array__') and isinstance(y[0], Sequence) and not isinstance(y[0], string_types)): raise ValueError('You appear to be using a legacy multi-label data' ' representation. Sequence of sequences are no' ' longer supported; use a binary array or sparse' ' matrix instead.') except IndexError: pass # Invalid inputs if y.ndim > 2 or (y.dtype == object and len(y) and not isinstance(y.flat[0], string_types)): return 'unknown' # [[[1, 2]]] or [obj_1] and not ["label_1"] if y.ndim == 2 and y.shape[1] == 0: return 'unknown' # [[]] if y.ndim == 2 and y.shape[1] > 1: suffix = "-multioutput" # [[1, 2], [1, 2]] else: suffix = "" # [1, 2, 3] or [[1], [2], [3]] # check float and contains non-integer float values if y.dtype.kind == 'f' and np.any(y != y.astype(int)): # [.1, .2, 3] or [[.1, .2, 3]] or [[1., .2]] and not [1., 2., 3.] return 'continuous' + suffix if (len(np.unique(y)) > 2) or (y.ndim >= 2 and len(y[0]) > 1): return 'multiclass' + suffix # [1, 2, 3] or [[1., 2., 3]] or [[1, 2]] else: return 'binary' # [1, 2] or [["a"], ["b"]] def _check_partial_fit_first_call(clf, classes=None): """Private helper function for factorizing common classes param logic Estimators that implement the ``partial_fit`` API need to be provided with the list of possible classes at the first call to partial_fit. Subsequent calls to partial_fit should check that ``classes`` is still consistent with a previous value of ``clf.classes_`` when provided. This function returns True if it detects that this was the first call to ``partial_fit`` on ``clf``. In that case the ``classes_`` attribute is also set on ``clf``. """ if getattr(clf, 'classes_', None) is None and classes is None: raise ValueError("classes must be passed on the first call " "to partial_fit.") elif classes is not None: if getattr(clf, 'classes_', None) is not None: if not array_equal(clf.classes_, unique_labels(classes)): raise ValueError( "`classes=%r` is not the same as on last call " "to partial_fit, was: %r" % (classes, clf.classes_)) else: # This is the first call to partial_fit clf.classes_ = unique_labels(classes) return True # classes is None and clf.classes_ has already previously been set: # nothing to do return False def class_distribution(y, sample_weight=None): """Compute class priors from multioutput-multiclass target data Parameters ---------- y : array like or sparse matrix of size (n_samples, n_outputs) The labels for each example. sample_weight : array-like of shape = (n_samples,), optional Sample weights. Returns ------- classes : list of size n_outputs of arrays of size (n_classes,) List of classes for each column. n_classes : list of integers of size n_outputs Number of classes in each column class_prior : list of size n_outputs of arrays of size (n_classes,) Class distribution of each column. """ classes = [] n_classes = [] class_prior = [] n_samples, n_outputs = y.shape if issparse(y): y = y.tocsc() y_nnz = np.diff(y.indptr) for k in range(n_outputs): col_nonzero = y.indices[y.indptr[k]:y.indptr[k + 1]] # separate sample weights for zero and non-zero elements if sample_weight is not None: nz_samp_weight = np.asarray(sample_weight)[col_nonzero] zeros_samp_weight_sum = (np.sum(sample_weight) - np.sum(nz_samp_weight)) else: nz_samp_weight = None zeros_samp_weight_sum = y.shape[0] - y_nnz[k] classes_k, y_k = np.unique(y.data[y.indptr[k]:y.indptr[k + 1]], return_inverse=True) class_prior_k = bincount(y_k, weights=nz_samp_weight) # An explicit zero was found, combine its weight with the weight # of the implicit zeros if 0 in classes_k: class_prior_k[classes_k == 0] += zeros_samp_weight_sum # If an there is an implicit zero and it is not in classes and # class_prior, make an entry for it if 0 not in classes_k and y_nnz[k] < y.shape[0]: classes_k = np.insert(classes_k, 0, 0) class_prior_k = np.insert(class_prior_k, 0, zeros_samp_weight_sum) classes.append(classes_k) n_classes.append(classes_k.shape[0]) class_prior.append(class_prior_k / class_prior_k.sum()) else: for k in range(n_outputs): classes_k, y_k = np.unique(y[:, k], return_inverse=True) classes.append(classes_k) n_classes.append(classes_k.shape[0]) class_prior_k = bincount(y_k, weights=sample_weight) class_prior.append(class_prior_k / class_prior_k.sum()) return (classes, n_classes, class_prior)
bsd-3-clause
dtkav/naclports
ports/ipython-ppapi/kernel.py
7
12026
# Copyright (c) 2014 Google Inc. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """A simple shell that uses the IPython messaging system.""" # Override platform information. import platform platform.system = lambda: "pnacl" platform.release = lambda: "chrome" import time import json import logging import sys import Queue import thread stdin_input = Queue.Queue() shell_input = Queue.Queue() stdin_output = Queue.Queue() shell_output = Queue.Queue() iopub_output = Queue.Queue() sys_stdout = sys.stdout sys_stderr = sys.stderr def emit(s): print >> sys_stderr, "EMITTING: %s" % (s) time.sleep(1) import IPython from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC from IPython.utils.traitlets import Type, Dict, Instance from IPython.core.displayhook import DisplayHook from IPython.utils import py3compat from IPython.utils.py3compat import builtin_mod from IPython.utils.jsonutil import json_clean, encode_images from IPython.core.displaypub import DisplayPublisher from IPython.config.configurable import Configurable # module defined in shell.cc for communicating via pepper API from pyppapi import nacl_instance def CreateMessage(msg_type, parent_header=None, content=None): if parent_header is None: parent_header = {} if content is None: content = {} return { 'header': {'msg_type': msg_type}, 'parent_header': parent_header, 'content': content, 'msg_type': msg_type, } class MsgOutStream(object): """Class to overrides stderr and stdout.""" def __init__(self, stream_name): self._stream_name = stream_name self._parent_header = {} def SetParentHeader(self, parent_header): self._parent_header = parent_header def close(self): pass def flush(self): pass def write(self, string): iopub_output.put(CreateMessage('stream', parent_header=self._parent_header, content={'name': self._stream_name, 'data': string})) def writelines(self, sequence): for string in sequence: self.write(string) # override sys.stdout and sys.stderr to broadcast on iopub stdout_stream = MsgOutStream('stdout') stderr_stream = MsgOutStream('stderr') sys.stdout = stdout_stream sys.stderr = stderr_stream class PepperShellDisplayHook(DisplayHook): parent_header = Dict({}) def set_parent_header(self, parent_header): """Set the parent for outbound messages.""" self.parent_header = parent_header def start_displayhook(self): self.content = {} def write_output_prompt(self): self.content['execution_count'] = self.prompt_count def write_format_data(self, format_dict, md_dict=None): self.content['data'] = encode_images(format_dict) self.content['metadata'] = md_dict def finish_displayhook(self): sys.stdout.flush() sys.stderr.flush() iopub_output.put(CreateMessage('pyout', parent_header=self.parent_header, content=self.content)) self.content = None class PepperDisplayPublisher(DisplayPublisher): parent_header = Dict({}) def set_parent_header(self, parent_header): self.parent_header = parent_header def _flush_streams(self): """flush IO Streams prior to display""" sys.stdout.flush() sys.stderr.flush() def publish(self, source, data, metadata=None): self._flush_streams() if metadata is None: metadata = {} self._validate_data(source, data, metadata) content = {} content['source'] = source content['data'] = encode_images(data) content['metadata'] = metadata iopub_output.put(CreateMessage('display_data', content=json_clean(content), parent_header=self.parent_header)) def clear_output(self, stdout=True, stderr=True, other=True): content = dict(stdout=stdout, stderr=stderr, other=other) if stdout: sys.stdout.write('\r') if stderr: sys.stderr.write('\r') self._flush_streams() iopub_output.put(CreateMessage('clear_output', content=content, parent_header=self.parent_header)) class PepperInteractiveShell(InteractiveShell): """A subclass of InteractiveShell for the Pepper Messagin API.""" displayhook_class = Type(PepperShellDisplayHook) display_pub_class = Type(PepperDisplayPublisher) @staticmethod def enable_gui(gui): pass InteractiveShellABC.register(PepperInteractiveShell) class PepperKernel(Configurable): shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') shell_class = Type(PepperInteractiveShell) def __init__(self): self.shell = self.shell_class.instance(parent=self) self.shell.run_cell(""" import os matplotlib_config_dir = '/mplconfigdir' os.environ['XDG_CONFIG_HOME'] = matplotlib_config_dir os.environ['TMP'] = '' import matplotlib import matplotlib.cbook """) shell = PepperKernel().shell # Taken from IPython 2.x branch, IPython/kernel/zmq/ipykernel.py def _complete(msg): c = msg['content'] try: cpos = int(c['cursor_pos']) except: # If we don't get something that we can convert to an integer, at # least attempt the completion guessing the cursor is at the end of # the text, if there's any, and otherwise of the line cpos = len(c['text']) if cpos==0: cpos = len(c['line']) return shell.complete(c['text'], c['line'], cpos) # Special message to indicate the NaCl kernel is ready. iopub_output.put(CreateMessage('status', content={'execution_state': 'nacl_ready'})) def _no_raw_input(self): """Raise StdinNotImplentedError if active frontend doesn't support stdin.""" raise StdinNotImplementedError("raw_input was called, but this " "frontend does not support stdin.") def _raw_input(prompt, parent_header): # Flush output before making the request. sys.stderr.flush() sys.stdout.flush() # flush the stdin socket, to purge stale replies while True: try: stdin_input.get_nowait() except Queue.Empty: break # Send the input request. content = json_clean(dict(prompt=prompt)) stdin_output.put(CreateMessage('input_request', content=content, parent_header=parent_header)) # Await a response. while True: try: reply = stdin_input.get() except Exception: print "Invalid Message" except KeyboardInterrupt: # re-raise KeyboardInterrupt, to truncate traceback raise KeyboardInterrupt else: break try: value = py3compat.unicode_to_str(reply['content']['value']) except: print "Got bad raw_input reply: " print reply value = '' if value == '\x04': # EOF raise EOFError return value def main_loop(): execution_count = 1 while 1: iopub_output.put(CreateMessage('status', content={'execution_state': 'idle'})) msg = shell_input.get() iopub_output.put(CreateMessage('status', content={'execution_state': 'busy'})) if not 'header' in msg: continue request_header = msg['header'] if not 'msg_type' in request_header: continue msg_type = request_header['msg_type'] if msg_type == 'execute_request': try: content = msg[u'content'] code = content[u'code'] silent = content[u'silent'] store_history = content.get(u'store_history', not silent) except: self.log.error("Got bad msg: ") self.log.error("%s", msg) continue # Replace raw_input. Note that is not sufficient to replace # raw_input in the user namespace. if content.get('allow_stdin', False): raw_input = lambda prompt='': _raw_input(prompt, request_header) input = lambda prompt='': eval(raw_input(prompt)) else: raw_input = input = lambda prompt='' : _no_raw_input() if py3compat.PY3: _sys_raw_input = builtin_mod.input builtin_mod.input = raw_input else: _sys_raw_input = builtin_mod.raw_input _sys_eval_input = builtin_mod.input builtin_mod.raw_input = raw_input builtin_mod.input = input # Let output streams know which message the output is for stdout_stream.SetParentHeader(request_header) stderr_stream.SetParentHeader(request_header) shell.displayhook.set_parent_header(request_header) shell.display_pub.set_parent_header(request_header) status = 'ok' content = {} try: shell.run_cell(msg['content']['code'], store_history=store_history, silent=silent) except Exception, ex: status = 'error' logging.exception('Exception occured while running cell') finally: # Restore raw_input. if py3compat.PY3: builtin_mod.input = _sys_raw_input else: builtin_mod.raw_input = _sys_raw_input builtin_mod.input = _sys_eval_input content = {'status': status, 'execution_count': execution_count} if status == 'ok': content['payload'] = [] content['user_variables'] = {} content['user_expressions'] = {} elif status == 'error': content['ename'] = type(ex).__name__ content['evalue'] = str(ex) content['traceback'] = [] execution_count += 1 if status == 'error': iopub_output.put(CreateMessage('pyerr', parent_header=request_header, content={ 'execution_count': execution_count, 'ename': type(ex).__name__, 'evalue': str(ex), 'traceback': [] } )) shell_output.put(CreateMessage('execute_reply', parent_header=request_header, content=content)) elif msg_type == 'complete_request': # Taken from IPython 2.x branch, IPython/kernel/zmq/ipykernel.py txt, matches = _complete(msg) matches = {'matches' : matches, 'matched_text' : txt, 'status' : 'ok'} matches = json_clean(matches) shell_output.put(CreateMessage('complete_reply', parent_header = request_header, content = matches)) elif msg_type == 'object_info_request': # Taken from IPython 2.x branch, IPython/kernel/zmq/ipykernel.py content = msg['content'] object_info = shell.object_inspect(content['oname'], detail_level = content.get('detail_level', 0)) # Before we send this object over, we scrub it for JSON usage oinfo = json_clean(object_info) shell_output.put(CreateMessage('object_info_reply', parent_header = request_header, content = oinfo)) elif msg_type == 'restart': # break out of this loop, ending this program. # The main event loop in shell.cc will then # run this program again. break elif msg_type == 'kill': # Raise an exception so that the function # running this script will return -1, resulting # in no restart of this script. raise RuntimeError thread.start_new_thread(main_loop, ()) def deal_message(msg): channel = msg['stream'] content = json.loads(msg['json']) queues = {'shell': shell_input, 'stdin': stdin_input} queue = queues[channel] queue.put(content) def send_message(stream, msg): nacl_instance.send_raw_object({ 'stream': stream, 'json': json.dumps(msg) }) while 1: msg = nacl_instance.wait_for_message(timeout=1, sleeptime=10000) try: deal_message(msg) except: pass output_streams = [ (stdin_output, 'stdin'), (shell_output, 'shell'), (iopub_output, 'iopub') ] for msg_queue, stream in output_streams: msg = None try: msg = msg_queue.get_nowait() send_message(stream, msg) except Queue.Empty: pass
bsd-3-clause
RegulatoryGenomicsUPF/pyicoteo
pyicoteolib/enrichment.py
1
40209
""" Pyicoteo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ import sys, os import math import random from core import Cluster, Region, InvalidLine, InsufficientData, ConversionNotSupported from defaults import * import utils import bam from regions import AnnotationGene, AnnotationTranscript, AnnotationExon, RegionWriter, read_gff_file, get_exons, get_introns, gene_slide import warnings try: from shutil import move except: from os import rename as move """ Differential expression and MA plot visualization module. """ def _region_from_dual(self, line): try: self.cluster_aux.clear() self.cluster_aux.read_line(line) strand = None if self.stranded_analysis: strand = self.cluster_aux.strand ret = Region(self.cluster_aux.name, self.cluster_aux.start, self.cluster_aux.end, name2=self.cluster_aux.name2, strand=strand) self.cluster_aux.clear() return ret except ValueError: pass #discarding header def __calc_reg_write(self, region_file, count, calculated_region): if count > self.region_mintags: region_file.write(calculated_region.write()) def calculate_region(self): """ Calculate a region file using the reads present in the both main files to analyze. """ self.logger.info('Generating regions...') self.sorted_region_path = '%s/calcregion_%s.bed'%(self._output_dir(), os.path.basename(self.current_output_path)) region_file = open(self.sorted_region_path, 'wb') if self.region_magic: regwriter = RegionWriter(self.gff_file, region_file, self.region_magic, no_sort=self.no_sort, logger=self.logger, write_as=BED, galaxy_workarounds=self.galaxy_workarounds) regwriter.write_regions() dual_reader = utils.DualSortedReader(self.current_experiment_path, self.current_control_path, self.experiment_format, self.logger) if self.stranded_analysis: calculate_region_stranded(self, dual_reader, region_file) else: calculate_region_notstranded(self, dual_reader, region_file) region_file.flush() def __cr_append(self, regions, region): regions.append(region) def calculate_region_notstranded(self, dual_reader, region_file): calculated_region = Region() readcount = 1 for line in dual_reader: if not calculated_region: #first region only calculated_region = _region_from_dual(self, line) calculated_region.end += self.proximity else: new_region = _region_from_dual(self, line) new_region.end += self.proximity if calculated_region.overlap(new_region): calculated_region.join(new_region) readcount += 1 else: calculated_region.end -= self.proximity __calc_reg_write(self, region_file, readcount, calculated_region) calculated_region = new_region.copy() readcount = 1 if calculated_region: calculated_region.end -= self.proximity __calc_reg_write(self, region_file, readcount, calculated_region) def calculate_region_stranded(self, dual_reader, region_file): temp_region_file = open(self.sorted_region_path, 'wb') region_plus = Region() region_minus = Region() regions = [] numreads_plus = 1 numreads_minus = 1 dual_reader = utils.DualSortedReader(self.current_experiment_path, self.current_control_path, self.experiment_format, self.logger) for line in dual_reader: new_region = _region_from_dual(self, line) new_region.end += self.proximity if not (region_plus and new_region.strand == PLUS_STRAND): region_plus = _region_from_dual(self, line) elif not (region_plus and new_region.strand == PLUS_STRAND): region_minus = _region_from_dual(self, line) else: if region_plus.overlap(new_region) and region_plus.strand == new_region.strand: region_plus.join(new_region) numreads_plus += 1 elif region_minus.overlap(new_region) and region_minus.strand == new_region.strand: region_minus.join(new_region) numreads_minus += 1 else: if new_region.strand == region_plus.strand: region_plus.end -= self.proximity self.__calc_reg_write(region_file, numreads_plus, region_plus) region_plus = new_region.copy() numreads_plus = 1 else: region_minus.end -= self.proximity self.__calc_reg_write(region_file, numreads_minus, region_minus) region_minus = new_region.copy() numreads_minus = 1 if region_plus: region_plus.end -= self.proximity regions.append(region_plus) if region_minus: region_minus.end -= self.proximity regions.append(region_minus) regions.sort(key=lambda x:(x.name, x.start, x.end, x.strand)) for region in regions: region_file.write(region.write()) def get_zscore(x, mean, sd): if sd > 0: return float(x-mean)/sd else: return 0 #This points are weird anyway def read_interesting_regions(self, file_path): regs = [] try: regs_file = open(file_path, 'r') for line in regs_file: regs.append(line.strip()) except IOError as ioerror: self.logger.warning("Interesting regions file not found") return regs # memory inefficient if there's a large number of interesting regions def plot_enrichment(self, file_path): with warnings.catch_warnings(): warnings.simplefilter("ignore") try: if self.postscript: import matplotlib matplotlib.use("PS") from matplotlib.pyplot import * from matplotlib import rcParams rcParams.update({'font.size': 22}) rcParams['legend.fontsize'] = 14 #decide labels if self.label1: label_main = self.label1 else: if self.real_control_path and self.real_experiment_path: label_main = '%s VS %s'%(os.path.basename(self.real_experiment_path), os.path.basename(self.real_control_path)) else: label_main = "A VS B" if self.label2: label_control = self.label2 else: if self.replica_path: label_control = '%s(A) VS %s(A)'%(os.path.basename(self.real_experiment_path), os.path.basename(self.replica_path)) else: label_control = 'Background distribution' #self.logger.info("Interesting regions path: %s" % (self.interesting_regions)) interesting_regs = [] if self.interesting_regions: self.logger.info("Reading interesting regions...") interesting_regs = read_interesting_regions(self, self.interesting_regions) #self.logger.info("Interesting regions: %s" % (interesting_regs)) #self.logger.info("Plot path: %s" % (file_path)) interesting_A = [] interesting_M = [] #self.logger.info("disable_significant: %s" % (self.disable_significant_color)) A = [] A_prime = [] M = [] M_significant = [] A_significant = [] M_prime = [] A_medians = [] points = [] minus_points = [] all_points = [] figure(figsize=(14,22)) biggest_A = -sys.maxint #for drawing smallest_A = sys.maxint #for drawing biggest_M = 0 #for drawing self.logger.info("Loading table...") for line in open(file_path): sline = line.split() try: enrich = dict(zip(enrichment_keys, sline)) # WARNING: for slide inter and slide intra: name2 = 'start:end' (no gene_id, FIXME?) name2 = enrich['name2'].split(':') gene_id = name2[0] if len(name2) >= 2: transcript_id = name2[1] # consider transcript_id? (exons) else: transcript_id = None if gene_id in interesting_regs or transcript_id in interesting_regs: interesting_M.append(float(enrich["M"])) interesting_A.append(float(enrich["A"])) biggest_A = max(biggest_A, float(enrich["A"])) smallest_A = min(smallest_A, float(enrich["A"])) biggest_M = max(biggest_M, abs(float(enrich["M"]))) biggest_A = max(biggest_A, float(enrich["A_prime"])) smallest_A = min(smallest_A, float(enrich["A_prime"])) biggest_M = max(biggest_M, abs(float(enrich["M_prime"]))) positive_point = self.zscore*float(enrich["sd"])+float(enrich["mean"]) negative_point = -self.zscore*float(enrich["sd"])+float(enrich["mean"]) A_median = float(enrich["A_median"]) all_points.append((A_median, positive_point, negative_point)) if abs(float(enrich["zscore"])) < self.zscore: M.append(float(enrich["M"])) A.append(float(enrich["A"])) else: M_significant.append(float(enrich["M"])) A_significant.append(float(enrich["A"])) M_prime.append(float(enrich["M_prime"])) A_prime.append(float(enrich["A_prime"])) except ValueError: pass #to skip the header all_points.sort(key= lambda x:x[0]) for t in all_points: (A_medians.append(t[0]), points.append(t[1]), minus_points.append(t[2])) if points: margin = 1.1 A_medians.append(biggest_A*margin) points.append(points[-1]) minus_points.append(minus_points[-1]) A_medians.insert(0, smallest_A) points.insert(0, points[0]) minus_points.insert(0, minus_points[0]) self.logger.info("Plotting points...") #Background plot subplot(211, axisbg="lightyellow") xlabel('Average', fontsize=30) ylabel('Log2 ratio', fontsize=30) axis([smallest_A*margin, biggest_A*margin, -biggest_M*margin, biggest_M*margin]) plot(A_prime, M_prime, '.', label=label_control, color = '#666666') plot(A_medians, points, 'r--', label="Z-score (%s)"%self.zscore) plot(A_medians, minus_points, 'r--') axhline(0, linestyle='--', color="grey", alpha=0.75) leg = legend(fancybox=True, scatterpoints=1, numpoints=1, loc=2, ncol=4, mode="expand") leg.get_frame().set_alpha(0.5) #Experiment plot subplot(212, axisbg="lightyellow") axis([smallest_A*margin, biggest_A*margin, -biggest_M*margin, biggest_M*margin]) plot(A, M, 'k.', label=label_main) if self.disable_significant_color: significant_marker = 'ko' else: significant_marker = 'ro' plot(A_significant, M_significant, significant_marker, label="%s (significant)"%label_main) plot(A_medians, points, 'r--', label="Z-score (%s)"%self.zscore) plot(A_medians, minus_points, 'r--') if self.interesting_regions: interesting_label = label_main + ' (interesting)' plot(interesting_A, interesting_M, 'H', label=interesting_label, color='#00EE00') # plotting "interesting" regions axhline(0, linestyle='--', color="grey", alpha=0.75) xlabel('Average', fontsize=30) ylabel('Log2 ratio', fontsize=30) leg2 = legend(fancybox=True, scatterpoints=1, numpoints=1, loc=2, ncol=4) leg2.get_frame().set_alpha(0.7) self._save_figure("enrichment_MA", width=500, height=2800) else: self.logger.warning("Nothing to plot.") except ImportError: if self.debug: raise __matplotlibwarn(self) def __matplotlibwarn(self): #FIXME move to utils.py or plotting module self.logger.warning('Pyicos can not find an installation of matplotlib, so no plot will be drawn. If you want to get a plot with the correlation values, install the matplotlib library.') def __calc_M(signal_a, signal_b): return math.log(float(signal_a)/float(signal_b), 2) def __calc_A(signal_a, signal_b): return (math.log(float(signal_a), 2)+math.log(float(signal_b), 2))/2 def _calculate_MA(self, region_path, read_counts, factor = 1, replica_factor = 1, file_a_reader=None, file_b_reader=None, replica_reader=None): tags_a = [] tags_b = [] numreads_background_1 = 0 numreads_background_2 = 0 total_reads_background_1 = 0 total_reads_background_2 = 0 self.logger.debug("Inside _calculate_MA") self.regions_analyzed_count = 0 enrichment_result = [] #This will hold the name, start and end of the region, plus the A, M, 'A and 'M if NOWRITE not in self.operations: out_file = open(self.current_output_path, 'wb') for region_line in open(region_path): sline = region_line.split() region_of_interest = self._region_from_sline(sline) if region_of_interest: region_a = None replica = None replica_tags = None signal_a = -1 signal_b = -1 signal_background_1 = -1 signal_background_2 = -1 swap1 = Region() swap2 = Region() if read_counts: signal_a = float(sline[6]) signal_b = float(sline[7])*factor signal_background_1 = float(sline[8]) signal_background_2 = float(sline[9])*replica_factor if CHECK_REPLICAS in self.operations: self.experiment_values.append(signal_background_1) self.replica_values.append(signal_background_2) else: self.logger.debug("Reading tags for %s ..."%region_of_interest) if self.experiment_format == BAM: tags_a = len(file_a_reader.get_overlaping_clusters(region_of_interest, overlap=self.overlap)) tags_b = len(file_b_reader.get_overlaping_clusters(region_of_interest, overlap=self.overlap)) else: tags_a = file_a_reader.get_overlaping_counts(region_of_interest, overlap=self.overlap) tags_b = file_b_reader.get_overlaping_counts(region_of_interest, overlap=self.overlap) if self.use_replica: if self.experiment_format == BAM: replica_tags = len(replica_reader.get_overlaping_clusters(region_of_interest, overlap=self.overlap)) else: replica_tags = replica_reader.get_overlaping_counts(region_of_interest, overlap=self.overlap) self.logger.debug("... done. tags_a: %s tags_b: %s"%(tags_a, tags_b)) #if we are using pseudocounts, use the union, use the intersection otherwise if (self.pseudocount and (tags_a or tags_b)) or (not self.pseudocount and tags_a and tags_b): signal_a = region_of_interest.normalized_counts(self.len_norm, self.n_norm, self.total_regions, self.pseudocount, factor, self.total_reads_a, tags_a) signal_b = region_of_interest.normalized_counts(self.len_norm, self.n_norm, self.total_regions, self.pseudocount, factor, self.total_reads_b, tags_b) self.already_norm = True if not self.counts_file: if (self.pseudocount and (tags_a or tags_b)) or (not self.pseudocount and tags_a and tags_b): if self.use_replica: replica = region_of_interest.copy() #replica.add_tags(replica_tags) numreads_background_1 = tags_a numreads_background_2 = replica_tags total_reads_background_1 = self.total_reads_a total_reads_background_2 = self.total_reads_replica signal_background_1 = signal_a signal_background_2 = region_of_interest.normalized_counts(self.len_norm, self.n_norm, self.total_regions, self.pseudocount, replica_factor, self.total_reads_replica, replica_tags) else: numreads_background_1 = 0 numreads_background_2 = 0 for i in range(0, tags_a+tags_b): if random.uniform(0,2) > 1: numreads_background_1 += 1 else: numreads_background_2 += 1 total_reads_background_1 = total_reads_background_2 = self.average_total_reads signal_background_1 = region_of_interest.normalized_counts(self.len_norm, self.n_norm, self.total_regions, self.pseudocount, replica_factor, self.average_total_reads, numreads_background_1) signal_background_2 = region_of_interest.normalized_counts(self.len_norm, self.n_norm, self.total_regions, self.pseudocount, replica_factor, self.average_total_reads, numreads_background_2) #if there is no data in the replica or in the swap and we are not using pseudocounts, dont write the data if signal_a > 0 and signal_b > 0 and signal_background_1 > 0 and signal_background_2 > 0 or self.use_MA: if self.use_MA and not self.already_norm: A = float(sline[10]) M = float(sline[11]) A_prime = float(sline[16]) M_prime = float(sline[17]) else: if not self.already_norm: #TODO refractor if self.len_norm: #read per kilobase in region signal_a = 1e3*(float(signal_a)/len(region_of_interest)) signal_b = 1e3*(float(signal_b)/len(region_of_interest)) signal_background_1 = 1e3*(float(signal_background_1)/len(region_of_interest)) signal_background_2 = 1e3*(float(signal_background_2)/len(region_of_interest)) if self.n_norm: #per million reads in the sample signal_a = 1e6*(float(signal_a)/self.total_reads_a) signal_b = 1e6*(float(signal_b)/self.total_reads_b) if self.use_replica: signal_background_1 = signal_a signal_background_2 = 1e6*(float(signal_background_2)/self.total_reads_replica) else: signal_background_1 = 1e6*(float(signal_background_1)/self.average_total_reads) signal_background_2 = 1e6*(float(signal_background_2)/self.average_total_reads) A = __calc_A(signal_a, signal_b) M = __calc_M(signal_a, signal_b) A_prime = __calc_A(signal_background_1, signal_background_2) M_prime = __calc_M(signal_background_1, signal_background_2) if CHECK_REPLICAS in self.operations: self.experiment_values.append(signal_background_1) self.replica_values.append(signal_background_2) if NOWRITE not in self.operations: out_file.write("%s\n"%("\t".join([region_of_interest.write().rstrip("\n"), str(signal_a), str(signal_b), str(signal_background_1), str(signal_background_2), str(A), str(M), str(self.total_reads_a), str(self.total_reads_b), str(tags_a), str(tags_b), str(A_prime), str(M_prime), str(total_reads_background_1), str(total_reads_background_2), str(numreads_background_1), str(numreads_background_2)]))) self.regions_analyzed_count += 1 self.logger.debug("LEAVING _calculate_MA") if NOWRITE in self.operations: return "" else: out_file.flush() out_file.close() # Outputting to HTML (if specified) if self.html_output is not None: self.logger.info("Generating HTML") try: from jinja2 import Environment, PackageLoader, Markup except: self.logger.error("Could not find the jinja2 library") return out_file.name loadr = PackageLoader('pyicoteolib', 'templates') env = Environment(loader=loadr) template = env.get_template('enrich_html.html') def jinja_read_file(filename): f = open(filename, 'r') #for line in f: # print line txt = ''.join(f.readlines()) f.close() return txt env.globals['jinja_read_file'] = jinja_read_file if self.galaxy_workarounds: # Galaxy changes the working directory when outputting multiple files parent_dir = "./" else: parent_dir = os.sep.join(out_file.name.split(os.sep)[0:-1]) + "/" plot_path = parent_dir + "enrichment_MA_" + out_file.name.split(os.sep)[-1] + ".png" bed_path = parent_dir + out_file.name.split(os.sep)[-1] html_file = open(self.html_output, 'w') html_file.write(template.render({'page_title': 'Enrichment results', 'results_output': jinja_read_file(out_file.name), 'plot_path': plot_path, 'bed_path': bed_path})) html_file.flush() html_file.close() return out_file.name def _calculate_total_lengths(self): msg = "Calculating enrichment in regions" if self.counts_file: self.sorted_region_path = self.counts_file if (not self.total_reads_a or not self.total_reads_b or (not self.total_reads_replica and self.use_replica)) and not self.use_MA: self.logger.info("... counting from counts file...") self.total_reads_a = 0 self.total_reads_b = 0 if self.total_reads_replica: self.total_reads_replica = 0 else: self.total_reads_replica = 1 for line in open(self.counts_file): try: enrich = dict(zip(enrichment_keys, line.split())) self.total_reads_a += float(enrich["signal_a"]) self.total_reads_b += float(enrich["signal_b"]) if self.use_replica: self.total_reads_replica += float(enrich["signal_prime_2"]) except ValueError: self.logger.debug("(Counting) skip header...") else: self.logger.info("... counting number of lines in files...") if not self.total_reads_a: if self.experiment_format == BAM: self.total_reads_a = bam.size(self.current_experiment_path) else: self.total_reads_a = sum(1 for line in utils.open_file(self.current_experiment_path, self.experiment_format, logger=self.logger)) if not self.total_reads_b: if self.experiment_format == BAM: self.total_reads_b = bam.size(self.current_control_path) else: self.total_reads_b = sum(1 for line in utils.open_file(self.current_control_path, self.control_format, logger=self.logger)) if self.use_replica and not self.total_reads_replica: if self.experiment_format == BAM: self.total_reads_replica = bam.size(self.replica_path) else: self.total_reads_replica = sum(1 for line in utils.open_file(self.replica_path, self.experiment_format, logger=self.logger)) self.logger.debug("Number lines in experiment A: %s Experiment B: %s"%(self.total_reads_a, self.total_reads_b)) if self.use_replica: msg = "%s using replicas..."%msg else: msg = "%s using swap..."%msg self.logger.info(msg) self.average_total_reads = (self.total_reads_a+self.total_reads_b)/2 def enrichment(self): file_a_reader = file_b_reader = replica_reader = None self.use_replica = (bool(self.replica_path) or (bool(self.counts_file) and self.use_replica_flag)) self.logger.debug("Use replica: %s"%self.use_replica) if not USE_MA in self.operations: _calculate_total_lengths(self) if not self.counts_file: file_a_reader = utils.read_fetcher(self.current_experiment_path, self.experiment_format, cached=self.cached, logger=self.logger, use_samtools=self.use_samtools, access_sequential=self.access_sequential, only_counts=True) file_b_reader = utils.read_fetcher(self.current_control_path, self.experiment_format, cached=self.cached, logger=self.logger, use_samtools=self.use_samtools, access_sequential=self.access_sequential, only_counts=True) if self.use_replica: replica_reader = utils.read_fetcher(self.current_replica_path, self.experiment_format, cached=self.cached, logger=self.logger, use_samtools=self.use_samtools, access_sequential=self.access_sequential, only_counts=True) if self.sorted_region_path: self.logger.info('Using region file %s (%s)'%(self.region_path, self.region_format)) else: calculate_region(self) #create region file semi automatically self.total_regions = sum(1 for line in open(self.sorted_region_path)) self.logger.info("... analyzing regions, calculating normalized counts, A / M and replica or swap...") self.already_norm = False if self.use_MA: ma_path = self.counts_file else: ma_path = self.sorted_region_path out_path = _calculate_MA(self, ma_path, bool(self.counts_file), 1, 1, file_a_reader, file_b_reader, replica_reader) self.already_norm = True self.logger.debug("Already normalized: %s"%self.already_norm) if self.tmm_norm: if CHECK_REPLICAS in self.operations: self.experiment_values = [] self.replica_values = [] self.logger.info("TMM Normalizing...") tmm_factor = calc_tmm_factor(self, out_path, self.regions_analyzed_count, False) replica_tmm_factor = 1 if self.use_replica: replica_tmm_factor = calc_tmm_factor(self, out_path, self.regions_analyzed_count, True) #move output file to old output #use as input old_output = '%s/notnormalized_%s'%(self._current_directory(), os.path.basename(self.current_output_path)) move(os.path.abspath(self.current_output_path), old_output) out_path = _calculate_MA(self, old_output, True, tmm_factor, replica_tmm_factor, True) #recalculate with the new factor, using the counts again if self.quant_norm: self.logger.info("Full quantile normalization...") signal_a = [] signal_prime_1 = [] enrich = [] for line in open(out_path): sline = line.split() enrich_line = dict(zip(enrichment_keys, sline)) enrich.append(enrich_line) signal_a.append(float(enrich_line['signal_a'])) signal_prime_1.append(float(enrich_line['signal_prime_1'])) #full quantile normalization signal_a.sort() enrich.sort(key=lambda x:float(x['signal_b'])) quant_counts = open('%s/quantcounts_%s'%(self._current_directory(), os.path.basename(self.current_output_path)), 'w') for i in range(len(enrich)): enrich[i]['signal_b'] = signal_a[i] self.logger.info("Full quantile normalization replica...") #full quantile normalization of the replica signal_prime_1.sort() enrich.sort(key=lambda x:float(x['signal_prime_2'])) for i in range(len(enrich)): enrich[i]['signal_prime_2'] = signal_prime_1[i] quant_counts.write("%s\n"%"\t".join(str(enrich[i][key]) for key in enrichment_keys[:20])) #write the lines quant_counts.flush() out_path = _calculate_MA(self, quant_counts.name, True, 1, 1, True) #recalculate with the new factor, using the counts again self._manage_temp_file(quant_counts.name) self.logger.info("%s regions analyzed."%self.regions_analyzed_count) if not NOWRITE in self.operations: self.logger.info("Enrichment result saved to %s"%self.current_output_path) if CHECK_REPLICAS in self.operations: check_replica(self) return out_path def _sub_tmm(counts_a, counts_b, reads_a, reads_b): return (counts_a-reads_a)/(counts_a*reads_a) + (counts_b-reads_b)/(counts_b*reads_b) def calc_tmm_factor(self, file_counts, total_regions, replica): if replica: signal_1 = "signal_prime_1" signal_2 = "signal_prime_2" M = "M_prime" reads_2 = self.total_reads_replica else: signal_1 = "signal_a" signal_2 = "signal_b" M = "M" reads_2 = self.total_reads_b values_list = [] #read the file inside the values_list for line in open(file_counts): sline = line.split() values_list.append(dict(zip(enrichment_keys, sline))) a_trim_number = int(round(total_regions*self.a_trim)) #discard the bad A self.logger.debug("Removing the worst A (%s regions, %s percent)"%(a_trim_number, self.a_trim*100)) values_list.sort(key=lambda x:float(x["A"])) #sort by A for i in range (0, a_trim_number): values_list.pop(0) values_list.sort(key=lambda x:float(x[M])) #sort by M m_trim_number = int(round(total_regions*(self.m_trim/2))) #this number is half the value of the flag, because we will trim half below, and half over #remove on the left for i in range(0, m_trim_number): values_list.pop(0) #remove on the right for i in range(0, m_trim_number): values_list.pop(-1) #now calculate the normalization factor arriba = 0 abajo = 0 for value in values_list: w = _sub_tmm(float(value[signal_1]), float(value[signal_2]), self.total_reads_a, reads_2) arriba += w*float(value[M]) abajo += w try: factor = 2**(arriba/abajo) except ZeroDivisionError: self.logger.warning("Division by zero, TMM factor could not be calculated.") factor = 1 if replica: self.logger.info("Replica TMM Normalization Factor: %s"%factor) else: self.logger.info("TMM Normalization Factor: %s"%factor) return factor def __load_enrichment_result(values_path): ret = [] for line in open(values_path): sline = line.split() try: float(sline[1]) ret.append(dict(zip(enrichment_keys, sline))) except ValueError: pass return ret def calculate_zscore(self, values_path): num_regions = sum(1 for line in open(values_path)) bin_size = int(self.binsize*num_regions) if bin_size < 50: self.logger.warning("The bin size results in a sliding window smaller than 50, adjusting window to 50 in order to get statistically meaningful results.") bin_size = 50 bin_step = max(1, int(round(self.bin_step*bin_size))) self.logger.info("Enrichment window calculation using a sliding window size of %s, sliding with a step of %s"%(bin_size, bin_step)) self.logger.info("... calculating zscore...") enrichment_result = __load_enrichment_result(values_path) enrichment_result.sort(key= lambda x:(float(x["A_prime"]))) self.logger.debug("Number of loaded counts: %s"%len(enrichment_result)) self.points = [] #get the standard deviations for i in range(0, num_regions-bin_size+bin_step, bin_step): #get the slice if i+bin_size < num_regions: result_chunk = enrichment_result[i:i+bin_size] else: result_chunk = enrichment_result[i:] #last chunk #retrieve the values mean_acum = 0 a_acum = 0 Ms_replica = [] for entry in result_chunk: mean_acum += float(entry["M_prime"]) a_acum += float(entry["A_prime"]) Ms_replica.append(float(entry["M_prime"])) #add them to the points of mean and sd mean = mean_acum/len(result_chunk) sd = math.sqrt((sum((x - mean)**2 for x in Ms_replica))/len(Ms_replica)) #the A median A_median = a_acum / len(result_chunk) self.points.append([A_median, mean, sd]) #The A asigned to the window, the mean and the standard deviation #self.logger.debug("Window of %s length, with A median: %s mean: %s sd: %s"%(len(result_chunk), self.points[-1][0], self.points[-1][1], self.points[-1][2], len(self.points))) #update z scores for entry in enrichment_result: entry["A_median"] = 0 entry["mean"] = 0 entry["sd"] = 0 entry["zscore"] = 0 closest_A = sys.maxint sd_position = 0 for i in range(0, len(self.points)): new_A = self.points[i][0] if new_A != closest_A: #skip repeated points if abs(closest_A - float(entry["A"])) >= abs(new_A - float(entry["A"])): closest_A = new_A sd_position = i else: break #already found, no need to go further since the points are ordered entry["A_median"] = closest_A if self.points: #only calculate if there where windows... __sub_zscore(self.sdfold, entry, self.points[sd_position]) if not self.points: # ... otherwise give a warning self.logger.warning("Insufficient number of regions analyzed (%s), z-score values could not be calculated"%num_regions) enrichment_result.sort(key=lambda x:(x["name"], int(x["start"]), int(x["end"]))) old_file_path = '%s/before_zscore_%s'%(self._current_directory(), os.path.basename(values_path)) #create path for the outdated file move(os.path.abspath(values_path), old_file_path) #move the file new_file = file(values_path, 'w') #open a new file in the now empty file space if not self.skip_header: new_file.write('\t'.join(enrichment_keys)) new_file.write('\n') for entry in enrichment_result: new_file.write("\t".join(str(entry[key]) for key in enrichment_keys)+"\n") self._manage_temp_file(old_file_path) return values_path def __sub_zscore(sdfold, entry, point): entry["mean"] = str(point[1]) entry["sd"] = str(point[2]) entry["zscore"] = str(get_zscore(float(entry["M"]), float(entry["mean"]), sdfold*float(entry["sd"]))) def check_replica(self): #discard everything below the flag new_experiment = [] new_replica = [] min_value = sys.maxint max_value = -sys.maxint for i in range(len(self.replica_values)): if self.experiment_values[i] > self.count_filter and self.replica_values[i] > self.count_filter: new_experiment.append(math.log(self.experiment_values[i], 2)) new_replica.append(math.log(self.replica_values[i], 2)) min_value = min(min_value, math.log(self.experiment_values[i], 2), math.log(self.replica_values[i], 2)) max_value = max(max_value, math.log(self.experiment_values[i], 2), math.log(self.replica_values[i], 2)) #print self.replica_values self.experiment_values = new_experiment self.replica_values = new_replica try: if self.postscript: import matplotlib matplotlib.use("PS") from matplotlib.pyplot import plot, show, xlabel, ylabel, axhline, axis, clf, text, title, xlim, ylim except: __matplotlibwarn(self) return 0 clf() r_squared = utils.pearson(self.experiment_values, self.replica_values)**2 text(min_value+abs(max_value)*0.1, max_value-abs(max_value)*0.2, r'Pearson $R^2$= %s'%round(r_squared, 3), fontsize=18, bbox={'facecolor':'yellow', 'alpha':0.5, 'pad':10}) xlabel("log2(%s)"%self.experiment_label, fontsize=18) ylabel("log2(%s)"%self.replica_label, fontsize=18) xlim(min_value, max_value) ylim(min_value, max_value) title(self.title_label, fontsize=24) plot(self.experiment_values, self.replica_values, '.') self._save_figure("check_replica") def check_replica_correlation(self): "No usado, de momento" min_tags = 20 experiment_reader = utils.read_fetcher(self.current_experiment_path, self.experiment_format, cached=self.cached, logger=self.logger, use_samtools=self.use_samtools, access_sequential=self.access_sequential) replica_reader = utils.read_fetcher(self.current_replica_path, self.experiment_format, cached=self.cached, logger=self.logger, use_samtools=self.use_samtools, access_sequential=self.access_sequential) correlations_acum = 0 num_correlations = 0 for region_line in open(self.region_path): sline = region_line.split() region_experiment = self._region_from_sline(sline) region_replica = region_experiment.copy() tags_experiment = experiment_reader.get_overlaping_clusters(region_experiment, overlap=1) tags_replica = replica_reader.get_overlaping_clusters(region_experiment, overlap=1) count_experiment = len(tags_experiment) count_replica = len(tags_replica) correlations = [] if count_experiment+count_replica > min_tags: region_experiment.add_tags(tags_experiment, clusterize=True) region_replica.add_tags(tags_replica, clusterize=True) num_correlations += 1 correlation = utils.pearson(region_experiment.get_array(), region_replica.get_array()) correlations_acum += max(0, correlation) correlations.append(correlation) print correlations_acum/num_correlations try: if self.postscript: import matplotlib matplotlib.use("PS") from matplotlib.pyplot import plot, boxplot, show, legend, figure, xlabel, ylabel, subplot, axhline, axis except: __matplotlibwarn(self) return 0 print correlations boxplot(correlations) self._save_figure("check_replica")
gpl-3.0
francisco-dlp/hyperspy
hyperspy/drawing/utils.py
1
57321
# -*- coding: utf-8 -*- # Copyright 2007-2016 The HyperSpy developers # # This file is part of HyperSpy. # # HyperSpy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # HyperSpy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with HyperSpy. If not, see <http://www.gnu.org/licenses/>. import copy import itertools import textwrap from traits import trait_base import matplotlib.pyplot as plt import matplotlib as mpl from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.backend_bases import key_press_handler import warnings import numpy as np from distutils.version import LooseVersion import logging import hyperspy as hs _logger = logging.getLogger(__name__) def contrast_stretching(data, saturated_pixels): """Calculate bounds that leaves out a given percentage of the data. Parameters ---------- data: numpy array saturated_pixels: scalar, None The percentage of pixels that are left out of the bounds. For example, the low and high bounds of a value of 1 are the 0.5% and 99.5% percentiles. It must be in the [0, 100] range. If None, set the value to 0. Returns ------- vmin, vmax: scalar The low and high bounds Raises ------ ValueError if the value of `saturated_pixels` is out of the valid range. """ # Sanity check if saturated_pixels is None: saturated_pixels = 0 if not 0 <= saturated_pixels <= 100: raise ValueError( "saturated_pixels must be a scalar in the range[0, 100]") vmin = np.nanpercentile(data, saturated_pixels / 2.) vmax = np.nanpercentile(data, 100 - saturated_pixels / 2.) return vmin, vmax MPL_DIVERGING_COLORMAPS = [ "BrBG", "bwr", "coolwarm", "PiYG", "PRGn", "PuOr", "RdBu", "RdGy", "RdYIBu", "RdYIGn", "seismic", "Spectral", ] # Add reversed colormaps MPL_DIVERGING_COLORMAPS += [cmap + "_r" for cmap in MPL_DIVERGING_COLORMAPS] def centre_colormap_values(vmin, vmax): """Calculate vmin and vmax to set the colormap midpoint to zero. Parameters ---------- vmin, vmax : scalar The range of data to display. Returns ------- cvmin, cvmax : scalar The values to obtain a centre colormap. """ absmax = max(abs(vmin), abs(vmax)) return -absmax, absmax def create_figure(window_title=None, _on_figure_window_close=None, disable_xyscale_keys=False, **kwargs): """Create a matplotlib figure. This function adds the possibility to execute another function when the figure is closed and to easily set the window title. Any keyword argument is passed to the plt.figure function Parameters ---------- window_title : string _on_figure_window_close : function disable_xyscale_keys : bool, disable the `k`, `l` and `L` shortcuts which toggle the x or y axis between linear and log scale. Returns ------- fig : plt.figure """ fig = plt.figure(**kwargs) if window_title is not None: # remove non-alphanumeric characters to prevent file saving problems # This is a workaround for: # https://github.com/matplotlib/matplotlib/issues/9056 reserved_characters = r'<>"/\|?*' for c in reserved_characters: window_title = window_title.replace(c, '') window_title = window_title.replace('\n', ' ') window_title = window_title.replace(':', ' -') fig.canvas.set_window_title(window_title) if disable_xyscale_keys and hasattr(fig.canvas, 'toolbar'): # hack the `key_press_handler` to disable the `k`, `l`, `L` shortcuts manager = fig.canvas.manager fig.canvas.mpl_disconnect(manager.key_press_handler_id) manager.key_press_handler_id = manager.canvas.mpl_connect( 'key_press_event', lambda event: key_press_handler_custom(event, manager.canvas)) if _on_figure_window_close is not None: on_figure_window_close(fig, _on_figure_window_close) return fig def key_press_handler_custom(event, canvas): if event.key not in ['k', 'l', 'L']: key_press_handler(event, canvas, canvas.manager.toolbar) def on_figure_window_close(figure, function): """Connects a close figure signal to a given function. Parameters ---------- figure : mpl figure instance function : function """ def function_wrapper(evt): function() figure.canvas.mpl_connect('close_event', function_wrapper) def plot_RGB_map(im_list, normalization='single', dont_plot=False): """Plot 2 or 3 maps in RGB. Parameters ---------- im_list : list of Signal2D instances normalization : {'single', 'global'} dont_plot : bool Returns ------- array: RGB matrix """ # from widgets import cursors height, width = im_list[0].data.shape[:2] rgb = np.zeros((height, width, 3)) rgb[:, :, 0] = im_list[0].data.squeeze() rgb[:, :, 1] = im_list[1].data.squeeze() if len(im_list) == 3: rgb[:, :, 2] = im_list[2].data.squeeze() if normalization == 'single': for i in range(len(im_list)): rgb[:, :, i] /= rgb[:, :, i].max() elif normalization == 'global': rgb /= rgb.max() rgb = rgb.clip(0, rgb.max()) if not dont_plot: figure = plt.figure() ax = figure.add_subplot(111) ax.frameon = False ax.set_axis_off() ax.imshow(rgb, interpolation='nearest') # cursors.set_mpl_ax(ax) figure.canvas.draw_idle() else: return rgb def subplot_parameters(fig): """Returns a list of the subplot parameters of a mpl figure. Parameters ---------- fig : mpl figure Returns ------- tuple : (left, bottom, right, top, wspace, hspace) """ wspace = fig.subplotpars.wspace hspace = fig.subplotpars.hspace left = fig.subplotpars.left right = fig.subplotpars.right top = fig.subplotpars.top bottom = fig.subplotpars.bottom return left, bottom, right, top, wspace, hspace class ColorCycle: _color_cycle = [mpl.colors.colorConverter.to_rgba(color) for color in ('b', 'g', 'r', 'c', 'm', 'y', 'k')] def __init__(self): self.color_cycle = copy.copy(self._color_cycle) def __call__(self): if not self.color_cycle: self.color_cycle = copy.copy(self._color_cycle) return self.color_cycle.pop(0) def plot_signals(signal_list, sync=True, navigator="auto", navigator_list=None, **kwargs): """Plot several signals at the same time. Parameters ---------- signal_list : list of BaseSignal instances If sync is set to True, the signals must have the same navigation shape, but not necessarily the same signal shape. sync : True or False, default "True" If True: the signals will share navigation, all the signals must have the same navigation shape for this to work, but not necessarily the same signal shape. navigator : {"auto", None, "spectrum", "slider", BaseSignal}, default "auto" See signal.plot docstring for full description navigator_list : {List of navigator arguments, None}, default None Set different navigator options for the signals. Must use valid navigator arguments: "auto", None, "spectrum", "slider", or a hyperspy Signal. The list must have the same size as signal_list. If None, the argument specified in navigator will be used. **kwargs Any extra keyword arguments are passed to each signal `plot` method. Example ------- >>> s_cl = hs.load("coreloss.dm3") >>> s_ll = hs.load("lowloss.dm3") >>> hs.plot.plot_signals([s_cl, s_ll]) Specifying the navigator: >>> s_cl = hs.load("coreloss.dm3") >>> s_ll = hs.load("lowloss.dm3") >>> hs.plot.plot_signals([s_cl, s_ll], navigator="slider") Specifying the navigator for each signal: >>> s_cl = hs.load("coreloss.dm3") >>> s_ll = hs.load("lowloss.dm3") >>> s_edx = hs.load("edx.dm3") >>> s_adf = hs.load("adf.dm3") >>> hs.plot.plot_signals( [s_cl, s_ll, s_edx], navigator_list=["slider",None,s_adf]) """ import hyperspy.signal if navigator_list: if not (len(signal_list) == len(navigator_list)): raise ValueError( "signal_list and navigator_list must" " have the same size") if sync: axes_manager_list = [] for signal in signal_list: axes_manager_list.append(signal.axes_manager) if not navigator_list: navigator_list = [] if navigator is None: navigator_list.extend([None] * len(signal_list)) elif isinstance(navigator, hyperspy.signal.BaseSignal): navigator_list.append(navigator) navigator_list.extend([None] * (len(signal_list) - 1)) elif navigator == "slider": navigator_list.append("slider") navigator_list.extend([None] * (len(signal_list) - 1)) elif navigator == "spectrum": navigator_list.extend(["spectrum"] * len(signal_list)) elif navigator == "auto": navigator_list.extend(["auto"] * len(signal_list)) else: raise ValueError( "navigator must be one of \"spectrum\",\"auto\"," " \"slider\", None, a Signal instance") # Check to see if the spectra have the same navigational shapes temp_shape_first = axes_manager_list[0].navigation_shape for i, axes_manager in enumerate(axes_manager_list): temp_shape = axes_manager.navigation_shape if not (temp_shape_first == temp_shape): raise ValueError( "The spectra does not have the same navigation shape") axes_manager_list[i] = axes_manager.deepcopy() if i > 0: for axis0, axisn in zip(axes_manager_list[0].navigation_axes, axes_manager_list[i].navigation_axes): axes_manager_list[i]._axes[axisn.index_in_array] = axis0 del axes_manager for signal, navigator, axes_manager in zip(signal_list, navigator_list, axes_manager_list): signal.plot(axes_manager=axes_manager, navigator=navigator, **kwargs) # If sync is False else: if not navigator_list: navigator_list = [] navigator_list.extend([navigator] * len(signal_list)) for signal, navigator in zip(signal_list, navigator_list): signal.plot(navigator=navigator, **kwargs) def _make_heatmap_subplot(spectra): from hyperspy._signals.signal2d import Signal2D im = Signal2D(spectra.data, axes=spectra.axes_manager._get_axes_dicts()) im.metadata.General.title = spectra.metadata.General.title im.plot() return im._plot.signal_plot.ax def set_xaxis_lims(mpl_ax, hs_axis): """ Set the matplotlib axis limits to match that of a HyperSpy axis Parameters ---------- mpl_ax : :class:`matplotlib.axis.Axis` The ``matplotlib`` axis to change hs_axis : :class:`~hyperspy.axes.DataAxis` The data axis that contains the values that control the scaling """ x_axis_lower_lim = hs_axis.axis[0] x_axis_upper_lim = hs_axis.axis[-1] mpl_ax.set_xlim(x_axis_lower_lim, x_axis_upper_lim) def _make_overlap_plot(spectra, ax, color="blue", line_style='-'): if isinstance(color, str): color = [color] * len(spectra) if isinstance(line_style, str): line_style = [line_style] * len(spectra) for spectrum_index, (spectrum, color, line_style) in enumerate( zip(spectra, color, line_style)): x_axis = spectrum.axes_manager.signal_axes[0] spectrum = _transpose_if_required(spectrum, 1) ax.plot(x_axis.axis, spectrum.data, color=color, ls=line_style) set_xaxis_lims(ax, x_axis) _set_spectrum_xlabel(spectra if isinstance(spectra, hs.signals.BaseSignal) else spectra[-1], ax) ax.set_ylabel('Intensity') ax.autoscale(tight=True) def _make_cascade_subplot( spectra, ax, color="blue", line_style='-', padding=1): max_value = 0 for spectrum in spectra: spectrum_yrange = (np.nanmax(spectrum.data) - np.nanmin(spectrum.data)) if spectrum_yrange > max_value: max_value = spectrum_yrange if isinstance(color, str): color = [color] * len(spectra) if isinstance(line_style, str): line_style = [line_style] * len(spectra) for spectrum_index, (spectrum, color, line_style) in enumerate( zip(spectra, color, line_style)): x_axis = spectrum.axes_manager.signal_axes[0] spectrum = _transpose_if_required(spectrum, 1) data_to_plot = ((spectrum.data - spectrum.data.min()) / float(max_value) + spectrum_index * padding) ax.plot(x_axis.axis, data_to_plot, color=color, ls=line_style) set_xaxis_lims(ax, x_axis) _set_spectrum_xlabel(spectra if isinstance(spectra, hs.signals.BaseSignal) else spectra[-1], ax) ax.set_yticks([]) ax.autoscale(tight=True) def _plot_spectrum(spectrum, ax, color="blue", line_style='-'): x_axis = spectrum.axes_manager.signal_axes[0] ax.plot(x_axis.axis, spectrum.data, color=color, ls=line_style) set_xaxis_lims(ax, x_axis) def _set_spectrum_xlabel(spectrum, ax): x_axis = spectrum.axes_manager.signal_axes[0] ax.set_xlabel("%s (%s)" % (x_axis.name, x_axis.units)) def _transpose_if_required(signal, expected_dimension): # EDS profiles or maps have signal dimension = 0 and navigation dimension # 1 or 2. For convenience transpose the signal if possible if (signal.axes_manager.signal_dimension == 0 and signal.axes_manager.navigation_dimension == expected_dimension): return signal.T else: return signal def plot_images(images, cmap=None, no_nans=False, per_row=3, label='auto', labelwrap=30, suptitle=None, suptitle_fontsize=18, colorbar='multi', centre_colormap="auto", saturated_pixels=0, scalebar=None, scalebar_color='white', axes_decor='all', padding=None, tight_layout=False, aspect='auto', min_asp=0.1, namefrac_thresh=0.4, fig=None, vmin=None, vmax=None, *args, **kwargs): """Plot multiple images as sub-images in one figure. Extra keyword arguments are passed to `matplotlib.figure`. Parameters ---------- images : list of Signal2D or BaseSignal `images` should be a list of Signals to plot. For `BaseSignal` with navigation dimensions 2 and signal dimension 0, the signal will be tranposed to form a `Signal2D`. Multi-dimensional images will have each plane plotted as a separate image. If any signal shape is not suitable, a ValueError will be raised. cmap : matplotlib colormap, list, or ``'mpl_colors'``, *optional* The colormap used for the images, by default read from ``pyplot``. A list of colormaps can also be provided, and the images will cycle through them. Optionally, the value ``'mpl_colors'`` will cause the cmap to loop through the default ``matplotlib`` colors (to match with the default output of the :py:func:`~.drawing.utils.plot_spectra` method. Note: if using more than one colormap, using the ``'single'`` option for ``colorbar`` is disallowed. no_nans : bool, optional If True, set nans to zero for plotting. per_row : int, optional The number of plots in each row label : None, str, or list of str, optional Control the title labeling of the plotted images. If None, no titles will be shown. If 'auto' (default), function will try to determine suitable titles using Signal2D titles, falling back to the 'titles' option if no good short titles are detected. Works best if all images to be plotted have the same beginning to their titles. If 'titles', the title from each image's metadata.General.title will be used. If any other single str, images will be labeled in sequence using that str as a prefix. If a list of str, the list elements will be used to determine the labels (repeated, if necessary). labelwrap : int, optional integer specifying the number of characters that will be used on one line If the function returns an unexpected blank figure, lower this value to reduce overlap of the labels between each figure suptitle : str, optional Title to use at the top of the figure. If called with label='auto', this parameter will override the automatically determined title. suptitle_fontsize : int, optional Font size to use for super title at top of figure colorbar : {'multi', None, 'single'} Controls the type of colorbars that are plotted. If None, no colorbar is plotted. If 'multi' (default), individual colorbars are plotted for each (non-RGB) image If 'single', all (non-RGB) images are plotted on the same scale, and one colorbar is shown for all centre_colormap : {"auto", True, False} If True the centre of the color scheme is set to zero. This is specially useful when using diverging color schemes. If "auto" (default), diverging color schemes are automatically centred. saturated_pixels: None, scalar or list of scalar, optional, default: 0 If list of scalar, the length should match the number of images to show. If provide in the list, set the value to 0. The percentage of pixels that are left out of the bounds. For example, the low and high bounds of a value of 1 are the 0.5% and 99.5% percentiles. It must be in the [0, 100] range. scalebar : {None, 'all', list of ints}, optional If None (or False), no scalebars will be added to the images. If 'all', scalebars will be added to all images. If list of ints, scalebars will be added to each image specified. scalebar_color : str, optional A valid MPL color string; will be used as the scalebar color axes_decor : {'all', 'ticks', 'off', None}, optional Controls how the axes are displayed on each image; default is 'all' If 'all', both ticks and axis labels will be shown If 'ticks', no axis labels will be shown, but ticks/labels will If 'off', all decorations and frame will be disabled If None, no axis decorations will be shown, but ticks/frame will padding : None or dict, optional This parameter controls the spacing between images. If None, default options will be used Otherwise, supply a dictionary with the spacing options as keywords and desired values as values Values should be supplied as used in pyplot.subplots_adjust(), and can be: 'left', 'bottom', 'right', 'top', 'wspace' (width), and 'hspace' (height) tight_layout : bool, optional If true, hyperspy will attempt to improve image placement in figure using matplotlib's tight_layout If false, repositioning images inside the figure will be left as an exercise for the user. aspect : str or numeric, optional If 'auto', aspect ratio is auto determined, subject to min_asp. If 'square', image will be forced onto square display. If 'equal', aspect ratio of 1 will be enforced. If float (or int/long), given value will be used. min_asp : float, optional Minimum aspect ratio to be used when plotting images namefrac_thresh : float, optional Threshold to use for auto-labeling. This parameter controls how much of the titles must be the same for the auto-shortening of labels to activate. Can vary from 0 to 1. Smaller values encourage shortening of titles by auto-labeling, while larger values will require more overlap in titles before activing the auto-label code. fig : mpl figure, optional If set, the images will be plotted to an existing MPL figure vmin, vmax : scalar or list of scalar, optional, default: None If list of scalar, the length should match the number of images to show. A list of scalar is not compatible with a single colorbar. See vmin, vmax of matplotlib.imshow() for more details. *args, **kwargs, optional Additional arguments passed to matplotlib.imshow() Returns ------- axes_list : list a list of subplot axes that hold the images See Also -------- plot_spectra : Plotting of multiple spectra plot_signals : Plotting of multiple signals plot_histograms : Compare signal histograms Notes ----- `interpolation` is a useful parameter to provide as a keyword argument to control how the space between pixels is interpolated. A value of ``'nearest'`` will cause no interpolation between pixels. `tight_layout` is known to be quite brittle, so an option is provided to disable it. Turn this option off if output is not as expected, or try adjusting `label`, `labelwrap`, or `per_row` """ def __check_single_colorbar(cbar): if cbar == 'single': raise ValueError('Cannot use a single colorbar with multiple ' 'colormaps. Please check for compatible ' 'arguments.') from hyperspy.drawing.widgets import ScaleBar from hyperspy.misc import rgb_tools from hyperspy.signal import BaseSignal # Check that we have a hyperspy signal im = [images] if not isinstance(images, (list, tuple)) else images for image in im: if not isinstance(image, BaseSignal): raise ValueError("`images` must be a list of image signals or a " "multi-dimensional signal." " " + repr(type(images)) + " was given.") # For list of EDS maps, transpose the BaseSignal if isinstance(images, (list, tuple)): images = [_transpose_if_required(image, 2) for image in images] # If input is >= 1D signal (e.g. for multi-dimensional plotting), # copy it and put it in a list so labeling works out as (x,y) when plotting if isinstance(images, BaseSignal) and images.axes_manager.navigation_dimension > 0: images = [images._deepcopy_with_new_data(images.data)] n = 0 for i, sig in enumerate(images): if sig.axes_manager.signal_dimension != 2: raise ValueError("This method only plots signals that are images. " "The signal dimension must be equal to 2. " "The signal at position " + repr(i) + " was " + repr(sig) + ".") # increment n by the navigation size, or by 1 if the navigation size is # <= 0 n += (sig.axes_manager.navigation_size if sig.axes_manager.navigation_size > 0 else 1) # If no cmap given, get default colormap from pyplot: if cmap is None: cmap = [plt.get_cmap().name] elif cmap == 'mpl_colors': for n_color, c in enumerate(mpl.rcParams['axes.prop_cycle']): make_cmap(colors=['#000000', c['color']], name='mpl{}'.format(n_color)) cmap = ['mpl{}'.format(i) for i in range(len(mpl.rcParams['axes.prop_cycle']))] __check_single_colorbar(colorbar) # cmap is list, tuple, or something else iterable (but not string): elif hasattr(cmap, '__iter__') and not isinstance(cmap, str): try: cmap = [c.name for c in cmap] # convert colormap to string except AttributeError: cmap = [c for c in cmap] # c should be string if not colormap __check_single_colorbar(colorbar) elif isinstance(cmap, mpl.colors.Colormap): cmap = [cmap.name] # convert single colormap to list with string elif isinstance(cmap, str): cmap = [cmap] # cmap is single string, so make it a list else: # Didn't understand cmap input, so raise error raise ValueError('The provided cmap value was not understood. Please ' 'check input values.') # If any of the cmaps given are diverging, and auto-centering, set the # appropriate flag: if centre_colormap == "auto": centre_colormaps = [] for c in cmap: if c in MPL_DIVERGING_COLORMAPS: centre_colormaps.append(True) else: centre_colormaps.append(False) # if it was True, just convert to list elif centre_colormap: centre_colormaps = [True] # likewise for false elif not centre_colormap: centre_colormaps = [False] # finally, convert lists to cycle generators for adaptive length: centre_colormaps = itertools.cycle(centre_colormaps) cmap = itertools.cycle(cmap) def _check_arg(arg, default_value, arg_name): if isinstance(arg, list): if len(arg) != n: _logger.warning('The provided {} values are ignored because the ' 'length of the list does not match the number of ' 'images'.format(arg_name)) arg = [default_value] * n else: arg = [arg] * n return arg vmin = _check_arg(vmin, None, 'vmin') vmax = _check_arg(vmax, None, 'vmax') saturated_pixels = _check_arg(saturated_pixels, 0, 'saturated_pixels') # Sort out the labeling: div_num = 0 all_match = False shared_titles = False user_labels = False if label is None: pass elif label == 'auto': # Use some heuristics to try to get base string of similar titles label_list = [x.metadata.General.title for x in images] # Find the shortest common string between the image titles # and pull that out as the base title for the sequence of images # array in which to store arrays res = np.zeros((len(label_list), len(label_list[0]) + 1)) res[:, 0] = 1 # j iterates the strings for j in range(len(label_list)): # i iterates length of substring test for i in range(1, len(label_list[0]) + 1): # stores whether or not characters in title match res[j, i] = label_list[0][:i] in label_list[j] # sum up the results (1 is True, 0 is False) and create # a substring based on the minimum value (this will be # the "smallest common string" between all the titles if res.all(): basename = label_list[0] div_num = len(label_list[0]) all_match = True else: div_num = int(min(np.sum(res, 1))) basename = label_list[0][:div_num - 1] all_match = False # trim off any '(' or ' ' characters at end of basename if div_num > 1: while True: if basename[len(basename) - 1] == '(': basename = basename[:-1] elif basename[len(basename) - 1] == ' ': basename = basename[:-1] else: break # namefrac is ratio of length of basename to the image name # if it is high (e.g. over 0.5), we can assume that all images # share the same base if len(label_list[0]) > 0: namefrac = float(len(basename)) / len(label_list[0]) else: # If label_list[0] is empty, it means there was probably no # title set originally, so nothing to share namefrac = 0 if namefrac > namefrac_thresh: # there was a significant overlap of label beginnings shared_titles = True # only use new suptitle if one isn't specified already if suptitle is None: suptitle = basename else: # there was not much overlap, so default back to 'titles' mode shared_titles = False label = 'titles' div_num = 0 elif label == 'titles': # Set label_list to each image's pre-defined title label_list = [x.metadata.General.title for x in images] elif isinstance(label, str): # Set label_list to an indexed list, based off of label label_list = [label + " " + repr(num) for num in range(n)] elif isinstance(label, list) and all( isinstance(x, str) for x in label): label_list = label user_labels = True # If list of labels is longer than the number of images, just use the # first n elements if len(label_list) > n: del label_list[n:] if len(label_list) < n: label_list *= (n // len(label_list)) + 1 del label_list[n:] else: raise ValueError("Did not understand input of labels.") # Determine appropriate number of images per row rows = int(np.ceil(n / float(per_row))) if n < per_row: per_row = n # Set overall figure size and define figure (if not pre-existing) if fig is None: k = max(plt.rcParams['figure.figsize']) / max(per_row, rows) f = plt.figure(figsize=(tuple(k * i for i in (per_row, rows)))) else: f = fig # Initialize list to hold subplot axes axes_list = [] # Initialize list of rgb tags isrgb = [False] * len(images) # Check to see if there are any rgb images in list # and tag them using the isrgb list for i, img in enumerate(images): if rgb_tools.is_rgbx(img.data): isrgb[i] = True # Determine how many non-rgb Images there are non_rgb = list(itertools.compress(images, [not j for j in isrgb])) if len(non_rgb) == 0 and colorbar is not None: colorbar = None warnings.warn("Sorry, colorbar is not implemented for RGB images.") # Find global min and max values of all the non-rgb images for use with # 'single' scalebar if colorbar == 'single': # get a g_saturated_pixels from saturated_pixels if isinstance(saturated_pixels, list): g_saturated_pixels = min(np.array([v for v in saturated_pixels])) else: g_saturated_pixels = saturated_pixels # estimate a g_vmin and g_max from saturated_pixels g_vmin, g_vmax = contrast_stretching(np.concatenate( [i.data.flatten() for i in non_rgb]), g_saturated_pixels) # if vmin and vmax are provided, override g_min and g_max if isinstance(vmin, list): _logger.warning('vmin have to be a scalar to be compatible with a ' 'single colorbar') else: g_vmin = vmin if vmin is not None else g_vmin if isinstance(vmax, list): _logger.warning('vmax have to be a scalar to be compatible with a ' 'single colorbar') else: g_vmax = vmax if vmax is not None else g_vmax if next(centre_colormaps): g_vmin, g_vmax = centre_colormap_values(g_vmin, g_vmax) # Check if we need to add a scalebar for some of the images if isinstance(scalebar, list) and all(isinstance(x, int) for x in scalebar): scalelist = True else: scalelist = False idx = 0 ax_im_list = [0] * len(isrgb) # Replot: create a list to store references to the images replot_ims = [] # Loop through each image, adding subplot for each one for i, ims in enumerate(images): # Get handles for the signal axes and axes_manager axes_manager = ims.axes_manager if axes_manager.navigation_dimension > 0: ims = ims._deepcopy_with_new_data(ims.data) for j, im in enumerate(ims): ax = f.add_subplot(rows, per_row, idx + 1) axes_list.append(ax) data = im.data centre = next(centre_colormaps) # get next value for centreing # Enable RGB plotting if rgb_tools.is_rgbx(data): data = rgb_tools.rgbx2regular_array(data, plot_friendly=True) l_vmin, l_vmax = None, None else: data = im.data # Find min and max for contrast l_vmin, l_vmax = contrast_stretching( data, saturated_pixels[idx]) l_vmin = vmin[idx] if vmin[idx] is not None else l_vmin l_vmax = vmax[idx] if vmax[idx] is not None else l_vmax if centre: l_vmin, l_vmax = centre_colormap_values(l_vmin, l_vmax) # Remove NaNs (if requested) if no_nans: data = np.nan_to_num(data) # Get handles for the signal axes and axes_manager axes_manager = im.axes_manager axes = axes_manager.signal_axes # Set dimensions of images xaxis = axes[0] yaxis = axes[1] extent = ( xaxis.low_value, xaxis.high_value, yaxis.high_value, yaxis.low_value, ) if not isinstance(aspect, (int, float)) and aspect not in [ 'auto', 'square', 'equal']: _logger.warning("Did not understand aspect ratio input. " "Using 'auto' as default.") aspect = 'auto' if aspect == 'auto': if float(yaxis.size) / xaxis.size < min_asp: factor = min_asp * float(xaxis.size) / yaxis.size elif float(yaxis.size) / xaxis.size > min_asp ** -1: factor = min_asp ** -1 * float(xaxis.size) / yaxis.size else: factor = 1 asp = np.abs(factor * float(xaxis.scale) / yaxis.scale) elif aspect == 'square': asp = abs(extent[1] - extent[0]) / abs(extent[3] - extent[2]) elif aspect == 'equal': asp = 1 elif isinstance(aspect, (int, float)): asp = aspect if 'interpolation' not in kwargs.keys(): kwargs['interpolation'] = 'nearest' # Get colormap for this image: cm = next(cmap) # Plot image data, using vmin and vmax to set bounds, # or allowing them to be set automatically if using individual # colorbars if colorbar == 'single' and not isrgb[i]: axes_im = ax.imshow(data, cmap=cm, extent=extent, vmin=g_vmin, vmax=g_vmax, aspect=asp, *args, **kwargs) ax_im_list[i] = axes_im else: axes_im = ax.imshow(data, cmap=cm, extent=extent, vmin=l_vmin, vmax=l_vmax, aspect=asp, *args, **kwargs) ax_im_list[i] = axes_im # If an axis trait is undefined, shut off : if isinstance(xaxis.units, trait_base._Undefined) or \ isinstance(yaxis.units, trait_base._Undefined) or \ isinstance(xaxis.name, trait_base._Undefined) or \ isinstance(yaxis.name, trait_base._Undefined): if axes_decor == 'all': _logger.warning( 'Axes labels were requested, but one ' 'or both of the ' 'axes units and/or name are undefined. ' 'Axes decorations have been set to ' '\'ticks\' instead.') axes_decor = 'ticks' # If all traits are defined, set labels as appropriate: else: ax.set_xlabel(axes[0].name + " axis (" + axes[0].units + ")") ax.set_ylabel(axes[1].name + " axis (" + axes[1].units + ")") if label: if all_match: title = '' elif shared_titles: title = label_list[i][div_num - 1:] else: if len(ims) == n: # This is true if we are plotting just 1 # multi-dimensional Signal2D title = label_list[idx] elif user_labels: title = label_list[idx] else: title = label_list[i] if ims.axes_manager.navigation_size > 1 and not user_labels: title += " %s" % str(ims.axes_manager.indices) ax.set_title(textwrap.fill(title, labelwrap)) # Set axes decorations based on user input set_axes_decor(ax, axes_decor) # If using independent colorbars, add them if colorbar == 'multi' and not isrgb[i]: div = make_axes_locatable(ax) cax = div.append_axes("right", size="5%", pad=0.05) plt.colorbar(axes_im, cax=cax) # Add scalebars as necessary if (scalelist and idx in scalebar) or scalebar == 'all': ax.scalebar = ScaleBar( ax=ax, units=axes[0].units, color=scalebar_color, ) # Replot: store references to the images replot_ims.append(im) idx += 1 # If using a single colorbar, add it, and do tight_layout, ensuring that # a colorbar is only added based off of non-rgb Images: if colorbar == 'single': foundim = None for i in range(len(isrgb)): if (not isrgb[i]) and foundim is None: foundim = i if foundim is not None: f.subplots_adjust(right=0.8) cbar_ax = f.add_axes([0.9, 0.1, 0.03, 0.8]) f.colorbar(ax_im_list[foundim], cax=cbar_ax) if tight_layout: # tight_layout, leaving room for the colorbar plt.tight_layout(rect=[0, 0, 0.9, 1]) elif tight_layout: plt.tight_layout() elif tight_layout: plt.tight_layout() # Set top bounds for shared titles and add suptitle if suptitle: f.subplots_adjust(top=0.85) f.suptitle(suptitle, fontsize=suptitle_fontsize) # If we want to plot scalebars, loop through the list of axes and add them if scalebar is None or scalebar is False: # Do nothing if no scalebars are called for pass elif scalebar == 'all': # scalebars were taken care of in the plotting loop pass elif scalelist: # scalebars were taken care of in the plotting loop pass else: raise ValueError("Did not understand scalebar input. Must be None, " "\'all\', or list of ints.") # Adjust subplot spacing according to user's specification if padding is not None: plt.subplots_adjust(**padding) # Replot: connect function def on_dblclick(event): # On the event of a double click, replot the selected subplot if not event.inaxes: return if not event.dblclick: return subplots = [axi for axi in f.axes if isinstance(axi, mpl.axes.Subplot)] inx = list(subplots).index(event.inaxes) im = replot_ims[inx] # Use some of the info in the subplot cm = subplots[inx].images[0].get_cmap() clim = subplots[inx].images[0].get_clim() sbar = False if (scalelist and inx in scalebar) or scalebar == 'all': sbar = True im.plot(colorbar=bool(colorbar), vmin=clim[0], vmax=clim[1], no_nans=no_nans, aspect=asp, scalebar=sbar, scalebar_color=scalebar_color, cmap=cm) f.canvas.mpl_connect('button_press_event', on_dblclick) return axes_list def set_axes_decor(ax, axes_decor): if axes_decor == 'off': ax.axis('off') elif axes_decor == 'ticks': ax.set_xlabel('') ax.set_ylabel('') elif axes_decor == 'all': pass elif axes_decor is None: ax.set_xlabel('') ax.set_ylabel('') ax.set_xticklabels([]) ax.set_yticklabels([]) def make_cmap(colors, name='my_colormap', position=None, bit=False, register=True): """ Create a matplotlib colormap with customized colors, optionally registering it with matplotlib for simplified use. Adapted from Chris Slocum's code at: https://github.com/CSlocumWX/custom_colormap/blob/master/custom_colormaps.py and used under the terms of that code's BSD-3 license Parameters ---------- colors : iterable list of either tuples containing rgb values, or html strings Colors should be arranged so that the first color is the lowest value for the colorbar and the last is the highest. name : str name of colormap to use when registering with matplotlib position : None or iterable list containing the values (from [0,1]) that dictate the position of each color within the colormap. If None (default), the colors will be equally-spaced within the colorbar. bit : boolean True if RGB colors are given in 8-bit [0 to 255] or False if given in arithmetic basis [0 to 1] (default) register : boolean switch to control whether or not to register the custom colormap with matplotlib in order to enable use by just the name string """ def _html_color_to_rgb(color_string): """ convert #RRGGBB to an (R, G, B) tuple """ color_string = color_string.strip() if color_string[0] == '#': color_string = color_string[1:] if len(color_string) != 6: raise ValueError( "input #{} is not in #RRGGBB format".format(color_string)) r, g, b = color_string[:2], color_string[2:4], color_string[4:] r, g, b = [int(n, 16) / 255 for n in (r, g, b)] return r, g, b bit_rgb = np.linspace(0, 1, 256) if position is None: position = np.linspace(0, 1, len(colors)) else: if len(position) != len(colors): raise ValueError("position length must be the same as colors") elif position[0] != 0 or position[-1] != 1: raise ValueError("position must start with 0 and end with 1") cdict = {'red': [], 'green': [], 'blue': []} for pos, color in zip(position, colors): if isinstance(color, str): color = _html_color_to_rgb(color) elif bit: color = (bit_rgb[color[0]], bit_rgb[color[1]], bit_rgb[color[2]]) cdict['red'].append((pos, color[0], color[0])) cdict['green'].append((pos, color[1], color[1])) cdict['blue'].append((pos, color[2], color[2])) cmap = mpl.colors.LinearSegmentedColormap(name, cdict, 256) if register: mpl.cm.register_cmap(name, cmap) return cmap def plot_spectra( spectra, style='overlap', color=None, line_style=None, padding=1., legend=None, legend_picking=True, legend_loc='upper right', fig=None, ax=None, **kwargs): """Plot several spectra in the same figure. Extra keyword arguments are passed to `matplotlib.figure`. Parameters ---------- spectra : list of Signal1D or BaseSignal Ordered spectra list of signal to plot. If `style` is "cascade" or "mosaic" the spectra can have different size and axes. For `BaseSignal` with navigation dimensions 1 and signal dimension 0, the signal will be tranposed to form a `Signal1D`. style : {'overlap', 'cascade', 'mosaic', 'heatmap'} The style of the plot. color : matplotlib color or a list of them or `None` Sets the color of the lines of the plots (no action on 'heatmap'). If a list, if its length is less than the number of spectra to plot, the colors will be cycled. If `None`, use default matplotlib color cycle. line_style: matplotlib line style or a list of them or `None` Sets the line style of the plots (no action on 'heatmap'). The main line style are '-','--','steps','-.',':'. If a list, if its length is less than the number of spectra to plot, line_style will be cycled. If If `None`, use continuous lines, eg: ('-','--','steps','-.',':') padding : float, optional, default 0.1 Option for "cascade". 1 guarantees that there is not overlapping. However, in many cases a value between 0 and 1 can produce a tighter plot without overlapping. Negative values have the same effect but reverse the order of the spectra without reversing the order of the colors. legend: None or list of str or 'auto' If list of string, legend for "cascade" or title for "mosaic" is displayed. If 'auto', the title of each spectra (metadata.General.title) is used. legend_picking: bool If true, a spectrum can be toggle on and off by clicking on the legended line. legend_loc : str or int This parameter controls where the legend is placed on the figure; see the pyplot.legend docstring for valid values fig : matplotlib figure or None If None, a default figure will be created. Specifying fig will not work for the 'heatmap' style. ax : matplotlib ax (subplot) or None If None, a default ax will be created. Will not work for 'mosaic' or 'heatmap' style. **kwargs remaining keyword arguments are passed to matplotlib.figure() or matplotlib.subplots(). Has no effect on 'heatmap' style. Example ------- >>> s = hs.load("some_spectra") >>> hs.plot.plot_spectra(s, style='cascade', color='red', padding=0.5) To save the plot as a png-file >>> hs.plot.plot_spectra(s).figure.savefig("test.png") Returns ------- ax: matplotlib axes or list of matplotlib axes An array is returned when `style` is "mosaic". """ import hyperspy.signal def _reverse_legend(ax_, legend_loc_): """ Reverse the ordering of a matplotlib legend (to be more consistent with the default ordering of plots in the 'cascade' and 'overlap' styles Parameters ---------- ax_: matplotlib axes legend_loc_: str or int This parameter controls where the legend is placed on the figure; see the pyplot.legend docstring for valid values """ l = ax_.get_legend() labels = [lb.get_text() for lb in list(l.get_texts())] handles = l.legendHandles ax_.legend(handles[::-1], labels[::-1], loc=legend_loc_) # Before v1.3 default would read the value from prefereces. if style == "default": style = "overlap" if color is not None: if isinstance(color, str): color = itertools.cycle([color]) elif hasattr(color, "__iter__"): color = itertools.cycle(color) else: raise ValueError("Color must be None, a valid matplotlib color " "string or a list of valid matplotlib colors.") else: if LooseVersion(mpl.__version__) >= "1.5.3": color = itertools.cycle( plt.rcParams['axes.prop_cycle'].by_key()["color"]) else: color = itertools.cycle(plt.rcParams['axes.color_cycle']) if line_style is not None: if isinstance(line_style, str): line_style = itertools.cycle([line_style]) elif hasattr(line_style, "__iter__"): line_style = itertools.cycle(line_style) else: raise ValueError("line_style must be None, a valid matplotlib" " line_style string or a list of valid matplotlib" " line_style.") else: line_style = ['-'] * len(spectra) if legend is not None: if isinstance(legend, str): if legend == 'auto': legend = [spec.metadata.General.title for spec in spectra] else: raise ValueError("legend must be None, 'auto' or a list of" " string") elif hasattr(legend, "__iter__"): legend = itertools.cycle(legend) if style == 'overlap': if fig is None: fig = plt.figure(**kwargs) if ax is None: ax = fig.add_subplot(111) _make_overlap_plot(spectra, ax, color=color, line_style=line_style,) if legend is not None: ax.legend(legend, loc=legend_loc) _reverse_legend(ax, legend_loc) if legend_picking is True: animate_legend(fig=fig, ax=ax) elif style == 'cascade': if fig is None: fig = plt.figure(**kwargs) if ax is None: ax = fig.add_subplot(111) _make_cascade_subplot(spectra, ax, color=color, line_style=line_style, padding=padding) if legend is not None: plt.legend(legend, loc=legend_loc) _reverse_legend(ax, legend_loc) elif style == 'mosaic': default_fsize = plt.rcParams["figure.figsize"] figsize = (default_fsize[0], default_fsize[1] * len(spectra)) fig, subplots = plt.subplots( len(spectra), 1, figsize=figsize, **kwargs) if legend is None: legend = [legend] * len(spectra) for spectrum, ax, color, line_style, legend in zip( spectra, subplots, color, line_style, legend): spectrum = _transpose_if_required(spectrum, 1) _plot_spectrum(spectrum, ax, color=color, line_style=line_style) ax.set_ylabel('Intensity') if legend is not None: ax.set_title(legend) if not isinstance(spectra, hyperspy.signal.BaseSignal): _set_spectrum_xlabel(spectrum, ax) if isinstance(spectra, hyperspy.signal.BaseSignal): _set_spectrum_xlabel(spectrum, ax) fig.tight_layout() elif style == 'heatmap': if not isinstance(spectra, hyperspy.signal.BaseSignal): import hyperspy.utils spectra = [_transpose_if_required(spectrum, 1) for spectrum in spectra] spectra = hyperspy.utils.stack(spectra) with spectra.unfolded(): ax = _make_heatmap_subplot(spectra) ax.set_ylabel('Spectra') ax = ax if style != "mosaic" else subplots return ax def animate_legend(fig=None, ax=None): """Animate the legend of a figure. A spectrum can be toggle on and off by clicking on the legended line. Parameters ---------- fig: None | matplotlib.figure If None pick the current figure using "plt.gcf" ax: None | matplotlib.axes If None pick the current axes using "plt.gca". Note ---- Code inspired from legend_picking.py in the matplotlib gallery """ if fig is None: fig = plt.gcf() if ax is None: ax = plt.gca() lines = ax.lines[::-1] lined = dict() leg = ax.get_legend() for legline, origline in zip(leg.get_lines(), lines): legline.set_picker(5) # 5 pts tolerance lined[legline] = origline def onpick(event): # on the pick event, find the orig line corresponding to the # legend proxy line, and toggle the visibility legline = event.artist if legline.axes == ax: origline = lined[legline] vis = not origline.get_visible() origline.set_visible(vis) # Change the alpha on the line in the legend so we can see what lines # have been toggled if vis: legline.set_alpha(1.0) else: legline.set_alpha(0.2) fig.canvas.draw_idle() fig.canvas.mpl_connect('pick_event', onpick) def plot_histograms(signal_list, bins='freedman', range_bins=None, color=None, line_style=None, legend='auto', fig=None, **kwargs): """Plot the histogram of every signal in the list in the same figure. This function creates a histogram for each signal and plot the list with the `utils.plot.plot_spectra` function. Parameters ---------- signal_list : iterable Ordered spectra list to plot. If `style` is "cascade" or "mosaic" the spectra can have different size and axes. bins : int or list or str, optional If bins is a string, then it must be one of: 'knuth' : use Knuth's rule to determine bins 'scotts' : use Scott's rule to determine bins 'freedman' : use the Freedman-diaconis rule to determine bins 'blocks' : use bayesian blocks for dynamic bin widths range_bins : tuple or None, optional. the minimum and maximum range for the histogram. If not specified, it will be (x.min(), x.max()) color : valid matplotlib color or a list of them or `None`, optional. Sets the color of the lines of the plots. If a list, if its length is less than the number of spectra to plot, the colors will be cycled. If If `None`, use default matplotlib color cycle. line_style: valid matplotlib line style or a list of them or `None`, optional. The main line style are '-','--','steps','-.',':'. If a list, if its length is less than the number of spectra to plot, line_style will be cycled. If If `None`, use continuous lines, eg: ('-','--','steps','-.',':') legend: None or list of str or 'auto', optional. Display a legend. If 'auto', the title of each spectra (metadata.General.title) is used. legend_picking: bool, optional. If true, a spectrum can be toggle on and off by clicking on the legended line. fig : matplotlib figure or None, optional. If None, a default figure will be created. **kwargs other keyword arguments (weight and density) are described in np.histogram(). Example ------- Histograms of two random chi-square distributions >>> img = hs.signals.Signal2D(np.random.chisquare(1,[10,10,100])) >>> img2 = hs.signals.Signal2D(np.random.chisquare(2,[10,10,100])) >>> hs.plot.plot_histograms([img,img2],legend=['hist1','hist2']) Returns ------- ax: matplotlib axes or list of matplotlib axes An array is returned when `style` is "mosaic". """ hists = [] for obj in signal_list: hists.append(obj.get_histogram(bins=bins, range_bins=range_bins, **kwargs)) if line_style is None: line_style = 'steps' return plot_spectra(hists, style='overlap', color=color, line_style=line_style, legend=legend, fig=fig)
gpl-3.0
shangwuhencc/scikit-learn
sklearn/decomposition/tests/test_incremental_pca.py
297
8265
"""Tests for Incremental PCA.""" import numpy as np from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_raises from sklearn import datasets from sklearn.decomposition import PCA, IncrementalPCA iris = datasets.load_iris() def test_incremental_pca(): # Incremental PCA on dense arrays. X = iris.data batch_size = X.shape[0] // 3 ipca = IncrementalPCA(n_components=2, batch_size=batch_size) pca = PCA(n_components=2) pca.fit_transform(X) X_transformed = ipca.fit_transform(X) np.testing.assert_equal(X_transformed.shape, (X.shape[0], 2)) assert_almost_equal(ipca.explained_variance_ratio_.sum(), pca.explained_variance_ratio_.sum(), 1) for n_components in [1, 2, X.shape[1]]: ipca = IncrementalPCA(n_components, batch_size=batch_size) ipca.fit(X) cov = ipca.get_covariance() precision = ipca.get_precision() assert_array_almost_equal(np.dot(cov, precision), np.eye(X.shape[1])) def test_incremental_pca_check_projection(): # Test that the projection of data is correct. rng = np.random.RandomState(1999) n, p = 100, 3 X = rng.randn(n, p) * .1 X[:10] += np.array([3, 4, 5]) Xt = 0.1 * rng.randn(1, p) + np.array([3, 4, 5]) # Get the reconstruction of the generated data X # Note that Xt has the same "components" as X, just separated # This is what we want to ensure is recreated correctly Yt = IncrementalPCA(n_components=2).fit(X).transform(Xt) # Normalize Yt /= np.sqrt((Yt ** 2).sum()) # Make sure that the first element of Yt is ~1, this means # the reconstruction worked as expected assert_almost_equal(np.abs(Yt[0][0]), 1., 1) def test_incremental_pca_inverse(): # Test that the projection of data can be inverted. rng = np.random.RandomState(1999) n, p = 50, 3 X = rng.randn(n, p) # spherical data X[:, 1] *= .00001 # make middle component relatively small X += [5, 4, 3] # make a large mean # same check that we can find the original data from the transformed # signal (since the data is almost of rank n_components) ipca = IncrementalPCA(n_components=2, batch_size=10).fit(X) Y = ipca.transform(X) Y_inverse = ipca.inverse_transform(Y) assert_almost_equal(X, Y_inverse, decimal=3) def test_incremental_pca_validation(): # Test that n_components is >=1 and <= n_features. X = [[0, 1], [1, 0]] for n_components in [-1, 0, .99, 3]: assert_raises(ValueError, IncrementalPCA(n_components, batch_size=10).fit, X) def test_incremental_pca_set_params(): # Test that components_ sign is stable over batch sizes. rng = np.random.RandomState(1999) n_samples = 100 n_features = 20 X = rng.randn(n_samples, n_features) X2 = rng.randn(n_samples, n_features) X3 = rng.randn(n_samples, n_features) ipca = IncrementalPCA(n_components=20) ipca.fit(X) # Decreasing number of components ipca.set_params(n_components=10) assert_raises(ValueError, ipca.partial_fit, X2) # Increasing number of components ipca.set_params(n_components=15) assert_raises(ValueError, ipca.partial_fit, X3) # Returning to original setting ipca.set_params(n_components=20) ipca.partial_fit(X) def test_incremental_pca_num_features_change(): # Test that changing n_components will raise an error. rng = np.random.RandomState(1999) n_samples = 100 X = rng.randn(n_samples, 20) X2 = rng.randn(n_samples, 50) ipca = IncrementalPCA(n_components=None) ipca.fit(X) assert_raises(ValueError, ipca.partial_fit, X2) def test_incremental_pca_batch_signs(): # Test that components_ sign is stable over batch sizes. rng = np.random.RandomState(1999) n_samples = 100 n_features = 3 X = rng.randn(n_samples, n_features) all_components = [] batch_sizes = np.arange(10, 20) for batch_size in batch_sizes: ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X) all_components.append(ipca.components_) for i, j in zip(all_components[:-1], all_components[1:]): assert_almost_equal(np.sign(i), np.sign(j), decimal=6) def test_incremental_pca_batch_values(): # Test that components_ values are stable over batch sizes. rng = np.random.RandomState(1999) n_samples = 100 n_features = 3 X = rng.randn(n_samples, n_features) all_components = [] batch_sizes = np.arange(20, 40, 3) for batch_size in batch_sizes: ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X) all_components.append(ipca.components_) for i, j in zip(all_components[:-1], all_components[1:]): assert_almost_equal(i, j, decimal=1) def test_incremental_pca_partial_fit(): # Test that fit and partial_fit get equivalent results. rng = np.random.RandomState(1999) n, p = 50, 3 X = rng.randn(n, p) # spherical data X[:, 1] *= .00001 # make middle component relatively small X += [5, 4, 3] # make a large mean # same check that we can find the original data from the transformed # signal (since the data is almost of rank n_components) batch_size = 10 ipca = IncrementalPCA(n_components=2, batch_size=batch_size).fit(X) pipca = IncrementalPCA(n_components=2, batch_size=batch_size) # Add one to make sure endpoint is included batch_itr = np.arange(0, n + 1, batch_size) for i, j in zip(batch_itr[:-1], batch_itr[1:]): pipca.partial_fit(X[i:j, :]) assert_almost_equal(ipca.components_, pipca.components_, decimal=3) def test_incremental_pca_against_pca_iris(): # Test that IncrementalPCA and PCA are approximate (to a sign flip). X = iris.data Y_pca = PCA(n_components=2).fit_transform(X) Y_ipca = IncrementalPCA(n_components=2, batch_size=25).fit_transform(X) assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1) def test_incremental_pca_against_pca_random_data(): # Test that IncrementalPCA and PCA are approximate (to a sign flip). rng = np.random.RandomState(1999) n_samples = 100 n_features = 3 X = rng.randn(n_samples, n_features) + 5 * rng.rand(1, n_features) Y_pca = PCA(n_components=3).fit_transform(X) Y_ipca = IncrementalPCA(n_components=3, batch_size=25).fit_transform(X) assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1) def test_explained_variances(): # Test that PCA and IncrementalPCA calculations match X = datasets.make_low_rank_matrix(1000, 100, tail_strength=0., effective_rank=10, random_state=1999) prec = 3 n_samples, n_features = X.shape for nc in [None, 99]: pca = PCA(n_components=nc).fit(X) ipca = IncrementalPCA(n_components=nc, batch_size=100).fit(X) assert_almost_equal(pca.explained_variance_, ipca.explained_variance_, decimal=prec) assert_almost_equal(pca.explained_variance_ratio_, ipca.explained_variance_ratio_, decimal=prec) assert_almost_equal(pca.noise_variance_, ipca.noise_variance_, decimal=prec) def test_whitening(): # Test that PCA and IncrementalPCA transforms match to sign flip. X = datasets.make_low_rank_matrix(1000, 10, tail_strength=0., effective_rank=2, random_state=1999) prec = 3 n_samples, n_features = X.shape for nc in [None, 9]: pca = PCA(whiten=True, n_components=nc).fit(X) ipca = IncrementalPCA(whiten=True, n_components=nc, batch_size=250).fit(X) Xt_pca = pca.transform(X) Xt_ipca = ipca.transform(X) assert_almost_equal(np.abs(Xt_pca), np.abs(Xt_ipca), decimal=prec) Xinv_ipca = ipca.inverse_transform(Xt_ipca) Xinv_pca = pca.inverse_transform(Xt_pca) assert_almost_equal(X, Xinv_ipca, decimal=prec) assert_almost_equal(X, Xinv_pca, decimal=prec) assert_almost_equal(Xinv_pca, Xinv_ipca, decimal=prec)
bsd-3-clause
petosegan/scikit-learn
sklearn/calibration.py
137
18876
"""Calibration of predicted probabilities.""" # Author: Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr> # Balazs Kegl <balazs.kegl@gmail.com> # Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # Mathieu Blondel <mathieu@mblondel.org> # # License: BSD 3 clause from __future__ import division import inspect import warnings from math import log import numpy as np from scipy.optimize import fmin_bfgs from .base import BaseEstimator, ClassifierMixin, RegressorMixin, clone from .preprocessing import LabelBinarizer from .utils import check_X_y, check_array, indexable, column_or_1d from .utils.validation import check_is_fitted from .isotonic import IsotonicRegression from .svm import LinearSVC from .cross_validation import check_cv from .metrics.classification import _check_binary_probabilistic_predictions class CalibratedClassifierCV(BaseEstimator, ClassifierMixin): """Probability calibration with isotonic regression or sigmoid. With this class, the base_estimator is fit on the train set of the cross-validation generator and the test set is used for calibration. The probabilities for each of the folds are then averaged for prediction. In case that cv="prefit" is passed to __init__, it is it is assumed that base_estimator has been fitted already and all data is used for calibration. Note that data for fitting the classifier and for calibrating it must be disjpint. Read more in the :ref:`User Guide <calibration>`. Parameters ---------- base_estimator : instance BaseEstimator The classifier whose output decision function needs to be calibrated to offer more accurate predict_proba outputs. If cv=prefit, the classifier must have been fit already on data. method : 'sigmoid' | 'isotonic' The method to use for calibration. Can be 'sigmoid' which corresponds to Platt's method or 'isotonic' which is a non-parameteric approach. It is not advised to use isotonic calibration with too few calibration samples (<<1000) since it tends to overfit. Use sigmoids (Platt's calibration) in this case. cv : integer or cross-validation generator or "prefit", optional If an integer is passed, it is the number of folds (default 3). Specific cross-validation objects can be passed, see sklearn.cross_validation module for the list of possible objects. If "prefit" is passed, it is assumed that base_estimator has been fitted already and all data is used for calibration. Attributes ---------- classes_ : array, shape (n_classes) The class labels. calibrated_classifiers_: list (len() equal to cv or 1 if cv == "prefit") The list of calibrated classifiers, one for each crossvalidation fold, which has been fitted on all but the validation fold and calibrated on the validation fold. References ---------- .. [1] Obtaining calibrated probability estimates from decision trees and naive Bayesian classifiers, B. Zadrozny & C. Elkan, ICML 2001 .. [2] Transforming Classifier Scores into Accurate Multiclass Probability Estimates, B. Zadrozny & C. Elkan, (KDD 2002) .. [3] Probabilistic Outputs for Support Vector Machines and Comparisons to Regularized Likelihood Methods, J. Platt, (1999) .. [4] Predicting Good Probabilities with Supervised Learning, A. Niculescu-Mizil & R. Caruana, ICML 2005 """ def __init__(self, base_estimator=None, method='sigmoid', cv=3): self.base_estimator = base_estimator self.method = method self.cv = cv def fit(self, X, y, sample_weight=None): """Fit the calibrated model Parameters ---------- X : array-like, shape (n_samples, n_features) Training data. y : array-like, shape (n_samples,) Target values. sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Returns ------- self : object Returns an instance of self. """ X, y = check_X_y(X, y, accept_sparse=['csc', 'csr', 'coo'], force_all_finite=False) X, y = indexable(X, y) lb = LabelBinarizer().fit(y) self.classes_ = lb.classes_ # Check that we each cross-validation fold can have at least one # example per class n_folds = self.cv if isinstance(self.cv, int) \ else self.cv.n_folds if hasattr(self.cv, "n_folds") else None if n_folds and \ np.any([np.sum(y == class_) < n_folds for class_ in self.classes_]): raise ValueError("Requesting %d-fold cross-validation but provided" " less than %d examples for at least one class." % (n_folds, n_folds)) self.calibrated_classifiers_ = [] if self.base_estimator is None: # we want all classifiers that don't expose a random_state # to be deterministic (and we don't want to expose this one). base_estimator = LinearSVC(random_state=0) else: base_estimator = self.base_estimator if self.cv == "prefit": calibrated_classifier = _CalibratedClassifier( base_estimator, method=self.method) if sample_weight is not None: calibrated_classifier.fit(X, y, sample_weight) else: calibrated_classifier.fit(X, y) self.calibrated_classifiers_.append(calibrated_classifier) else: cv = check_cv(self.cv, X, y, classifier=True) arg_names = inspect.getargspec(base_estimator.fit)[0] estimator_name = type(base_estimator).__name__ if (sample_weight is not None and "sample_weight" not in arg_names): warnings.warn("%s does not support sample_weight. Samples" " weights are only used for the calibration" " itself." % estimator_name) base_estimator_sample_weight = None else: base_estimator_sample_weight = sample_weight for train, test in cv: this_estimator = clone(base_estimator) if base_estimator_sample_weight is not None: this_estimator.fit( X[train], y[train], sample_weight=base_estimator_sample_weight[train]) else: this_estimator.fit(X[train], y[train]) calibrated_classifier = _CalibratedClassifier( this_estimator, method=self.method) if sample_weight is not None: calibrated_classifier.fit(X[test], y[test], sample_weight[test]) else: calibrated_classifier.fit(X[test], y[test]) self.calibrated_classifiers_.append(calibrated_classifier) return self def predict_proba(self, X): """Posterior probabilities of classification This function returns posterior probabilities of classification according to each class on an array of test vectors X. Parameters ---------- X : array-like, shape (n_samples, n_features) The samples. Returns ------- C : array, shape (n_samples, n_classes) The predicted probas. """ check_is_fitted(self, ["classes_", "calibrated_classifiers_"]) X = check_array(X, accept_sparse=['csc', 'csr', 'coo'], force_all_finite=False) # Compute the arithmetic mean of the predictions of the calibrated # classfiers mean_proba = np.zeros((X.shape[0], len(self.classes_))) for calibrated_classifier in self.calibrated_classifiers_: proba = calibrated_classifier.predict_proba(X) mean_proba += proba mean_proba /= len(self.calibrated_classifiers_) return mean_proba def predict(self, X): """Predict the target of new samples. Can be different from the prediction of the uncalibrated classifier. Parameters ---------- X : array-like, shape (n_samples, n_features) The samples. Returns ------- C : array, shape (n_samples,) The predicted class. """ check_is_fitted(self, ["classes_", "calibrated_classifiers_"]) return self.classes_[np.argmax(self.predict_proba(X), axis=1)] class _CalibratedClassifier(object): """Probability calibration with isotonic regression or sigmoid. It assumes that base_estimator has already been fit, and trains the calibration on the input set of the fit function. Note that this class should not be used as an estimator directly. Use CalibratedClassifierCV with cv="prefit" instead. Parameters ---------- base_estimator : instance BaseEstimator The classifier whose output decision function needs to be calibrated to offer more accurate predict_proba outputs. No default value since it has to be an already fitted estimator. method : 'sigmoid' | 'isotonic' The method to use for calibration. Can be 'sigmoid' which corresponds to Platt's method or 'isotonic' which is a non-parameteric approach based on isotonic regression. References ---------- .. [1] Obtaining calibrated probability estimates from decision trees and naive Bayesian classifiers, B. Zadrozny & C. Elkan, ICML 2001 .. [2] Transforming Classifier Scores into Accurate Multiclass Probability Estimates, B. Zadrozny & C. Elkan, (KDD 2002) .. [3] Probabilistic Outputs for Support Vector Machines and Comparisons to Regularized Likelihood Methods, J. Platt, (1999) .. [4] Predicting Good Probabilities with Supervised Learning, A. Niculescu-Mizil & R. Caruana, ICML 2005 """ def __init__(self, base_estimator, method='sigmoid'): self.base_estimator = base_estimator self.method = method def _preproc(self, X): n_classes = len(self.classes_) if hasattr(self.base_estimator, "decision_function"): df = self.base_estimator.decision_function(X) if df.ndim == 1: df = df[:, np.newaxis] elif hasattr(self.base_estimator, "predict_proba"): df = self.base_estimator.predict_proba(X) if n_classes == 2: df = df[:, 1:] else: raise RuntimeError('classifier has no decision_function or ' 'predict_proba method.') idx_pos_class = np.arange(df.shape[1]) return df, idx_pos_class def fit(self, X, y, sample_weight=None): """Calibrate the fitted model Parameters ---------- X : array-like, shape (n_samples, n_features) Training data. y : array-like, shape (n_samples,) Target values. sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Returns ------- self : object Returns an instance of self. """ lb = LabelBinarizer() Y = lb.fit_transform(y) self.classes_ = lb.classes_ df, idx_pos_class = self._preproc(X) self.calibrators_ = [] for k, this_df in zip(idx_pos_class, df.T): if self.method == 'isotonic': calibrator = IsotonicRegression(out_of_bounds='clip') elif self.method == 'sigmoid': calibrator = _SigmoidCalibration() else: raise ValueError('method should be "sigmoid" or ' '"isotonic". Got %s.' % self.method) calibrator.fit(this_df, Y[:, k], sample_weight) self.calibrators_.append(calibrator) return self def predict_proba(self, X): """Posterior probabilities of classification This function returns posterior probabilities of classification according to each class on an array of test vectors X. Parameters ---------- X : array-like, shape (n_samples, n_features) The samples. Returns ------- C : array, shape (n_samples, n_classes) The predicted probas. Can be exact zeros. """ n_classes = len(self.classes_) proba = np.zeros((X.shape[0], n_classes)) df, idx_pos_class = self._preproc(X) for k, this_df, calibrator in \ zip(idx_pos_class, df.T, self.calibrators_): if n_classes == 2: k += 1 proba[:, k] = calibrator.predict(this_df) # Normalize the probabilities if n_classes == 2: proba[:, 0] = 1. - proba[:, 1] else: proba /= np.sum(proba, axis=1)[:, np.newaxis] # XXX : for some reason all probas can be 0 proba[np.isnan(proba)] = 1. / n_classes # Deal with cases where the predicted probability minimally exceeds 1.0 proba[(1.0 < proba) & (proba <= 1.0 + 1e-5)] = 1.0 return proba def _sigmoid_calibration(df, y, sample_weight=None): """Probability Calibration with sigmoid method (Platt 2000) Parameters ---------- df : ndarray, shape (n_samples,) The decision function or predict proba for the samples. y : ndarray, shape (n_samples,) The targets. sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Returns ------- a : float The slope. b : float The intercept. References ---------- Platt, "Probabilistic Outputs for Support Vector Machines" """ df = column_or_1d(df) y = column_or_1d(y) F = df # F follows Platt's notations tiny = np.finfo(np.float).tiny # to avoid division by 0 warning # Bayesian priors (see Platt end of section 2.2) prior0 = float(np.sum(y <= 0)) prior1 = y.shape[0] - prior0 T = np.zeros(y.shape) T[y > 0] = (prior1 + 1.) / (prior1 + 2.) T[y <= 0] = 1. / (prior0 + 2.) T1 = 1. - T def objective(AB): # From Platt (beginning of Section 2.2) E = np.exp(AB[0] * F + AB[1]) P = 1. / (1. + E) l = -(T * np.log(P + tiny) + T1 * np.log(1. - P + tiny)) if sample_weight is not None: return (sample_weight * l).sum() else: return l.sum() def grad(AB): # gradient of the objective function E = np.exp(AB[0] * F + AB[1]) P = 1. / (1. + E) TEP_minus_T1P = P * (T * E - T1) if sample_weight is not None: TEP_minus_T1P *= sample_weight dA = np.dot(TEP_minus_T1P, F) dB = np.sum(TEP_minus_T1P) return np.array([dA, dB]) AB0 = np.array([0., log((prior0 + 1.) / (prior1 + 1.))]) AB_ = fmin_bfgs(objective, AB0, fprime=grad, disp=False) return AB_[0], AB_[1] class _SigmoidCalibration(BaseEstimator, RegressorMixin): """Sigmoid regression model. Attributes ---------- a_ : float The slope. b_ : float The intercept. """ def fit(self, X, y, sample_weight=None): """Fit the model using X, y as training data. Parameters ---------- X : array-like, shape (n_samples,) Training data. y : array-like, shape (n_samples,) Training target. sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Returns ------- self : object Returns an instance of self. """ X = column_or_1d(X) y = column_or_1d(y) X, y = indexable(X, y) self.a_, self.b_ = _sigmoid_calibration(X, y, sample_weight) return self def predict(self, T): """Predict new data by linear interpolation. Parameters ---------- T : array-like, shape (n_samples,) Data to predict from. Returns ------- T_ : array, shape (n_samples,) The predicted data. """ T = column_or_1d(T) return 1. / (1. + np.exp(self.a_ * T + self.b_)) def calibration_curve(y_true, y_prob, normalize=False, n_bins=5): """Compute true and predicted probabilities for a calibration curve. Read more in the :ref:`User Guide <calibration>`. Parameters ---------- y_true : array, shape (n_samples,) True targets. y_prob : array, shape (n_samples,) Probabilities of the positive class. normalize : bool, optional, default=False Whether y_prob needs to be normalized into the bin [0, 1], i.e. is not a proper probability. If True, the smallest value in y_prob is mapped onto 0 and the largest one onto 1. n_bins : int Number of bins. A bigger number requires more data. Returns ------- prob_true : array, shape (n_bins,) The true probability in each bin (fraction of positives). prob_pred : array, shape (n_bins,) The mean predicted probability in each bin. References ---------- Alexandru Niculescu-Mizil and Rich Caruana (2005) Predicting Good Probabilities With Supervised Learning, in Proceedings of the 22nd International Conference on Machine Learning (ICML). See section 4 (Qualitative Analysis of Predictions). """ y_true = column_or_1d(y_true) y_prob = column_or_1d(y_prob) if normalize: # Normalize predicted values into interval [0, 1] y_prob = (y_prob - y_prob.min()) / (y_prob.max() - y_prob.min()) elif y_prob.min() < 0 or y_prob.max() > 1: raise ValueError("y_prob has values outside [0, 1] and normalize is " "set to False.") y_true = _check_binary_probabilistic_predictions(y_true, y_prob) bins = np.linspace(0., 1. + 1e-8, n_bins + 1) binids = np.digitize(y_prob, bins) - 1 bin_sums = np.bincount(binids, weights=y_prob, minlength=len(bins)) bin_true = np.bincount(binids, weights=y_true, minlength=len(bins)) bin_total = np.bincount(binids, minlength=len(bins)) nonzero = bin_total != 0 prob_true = (bin_true[nonzero] / bin_total[nonzero]) prob_pred = (bin_sums[nonzero] / bin_total[nonzero]) return prob_true, prob_pred
bsd-3-clause
lthurlow/Network-Grapher
proj/external/matplotlib-1.2.1/build/lib.linux-i686-2.7/matplotlib/table.py
2
17111
""" Place a table below the x-axis at location loc. The table consists of a grid of cells. The grid need not be rectangular and can have holes. Cells are added by specifying their row and column. For the purposes of positioning the cell at (0, 0) is assumed to be at the top left and the cell at (max_row, max_col) is assumed to be at bottom right. You can add additional cells outside this range to have convenient ways of positioning more interesting grids. Author : John Gill <jng@europe.renre.com> Copyright : 2004 John Gill and John Hunter License : matplotlib license """ from __future__ import division, print_function import warnings import artist from artist import Artist, allow_rasterization from patches import Rectangle from cbook import is_string_like from matplotlib import docstring from text import Text from transforms import Bbox class Cell(Rectangle): """ A cell is a Rectangle with some associated text. """ PAD = 0.1 # padding between text and rectangle def __init__(self, xy, width, height, edgecolor='k', facecolor='w', fill=True, text='', loc=None, fontproperties=None ): # Call base Rectangle.__init__(self, xy, width=width, height=height, edgecolor=edgecolor, facecolor=facecolor) self.set_clip_on(False) # Create text object if loc is None: loc = 'right' self._loc = loc self._text = Text(x=xy[0], y=xy[1], text=text, fontproperties=fontproperties) self._text.set_clip_on(False) def set_transform(self, trans): Rectangle.set_transform(self, trans) # the text does not get the transform! def set_figure(self, fig): Rectangle.set_figure(self, fig) self._text.set_figure(fig) def get_text(self): 'Return the cell Text intance' return self._text def set_fontsize(self, size): self._text.set_fontsize(size) def get_fontsize(self): 'Return the cell fontsize' return self._text.get_fontsize() def auto_set_font_size(self, renderer): """ Shrink font size until text fits. """ fontsize = self.get_fontsize() required = self.get_required_width(renderer) while fontsize > 1 and required > self.get_width(): fontsize -= 1 self.set_fontsize(fontsize) required = self.get_required_width(renderer) return fontsize @allow_rasterization def draw(self, renderer): if not self.get_visible(): return # draw the rectangle Rectangle.draw(self, renderer) # position the text self._set_text_position(renderer) self._text.draw(renderer) def _set_text_position(self, renderer): """ Set text up so it draws in the right place. Currently support 'left', 'center' and 'right' """ bbox = self.get_window_extent(renderer) l, b, w, h = bbox.bounds # draw in center vertically self._text.set_verticalalignment('center') y = b + (h / 2.0) # now position horizontally if self._loc == 'center': self._text.set_horizontalalignment('center') x = l + (w / 2.0) elif self._loc == 'left': self._text.set_horizontalalignment('left') x = l + (w * self.PAD) else: self._text.set_horizontalalignment('right') x = l + (w * (1.0 - self.PAD)) self._text.set_position((x, y)) def get_text_bounds(self, renderer): """ Get text bounds in axes co-ordinates. """ bbox = self._text.get_window_extent(renderer) bboxa = bbox.inverse_transformed(self.get_data_transform()) return bboxa.bounds def get_required_width(self, renderer): """ Get width required for this cell. """ l, b, w, h = self.get_text_bounds(renderer) return w * (1.0 + (2.0 * self.PAD)) def set_text_props(self, **kwargs): 'update the text properties with kwargs' self._text.update(kwargs) class Table(Artist): """ Create a table of cells. Table can have (optional) row and column headers. Each entry in the table can be either text or patches. Column widths and row heights for the table can be specifified. Return value is a sequence of text, line and patch instances that make up the table """ codes = {'best': 0, 'upper right': 1, # default 'upper left': 2, 'lower left': 3, 'lower right': 4, 'center left': 5, 'center right': 6, 'lower center': 7, 'upper center': 8, 'center': 9, 'top right': 10, 'top left': 11, 'bottom left': 12, 'bottom right': 13, 'right': 14, 'left': 15, 'top': 16, 'bottom': 17, } FONTSIZE = 10 AXESPAD = 0.02 # the border between the axes and table edge def __init__(self, ax, loc=None, bbox=None): Artist.__init__(self) if is_string_like(loc) and loc not in self.codes: warnings.warn('Unrecognized location %s. Falling back on ' 'bottom; valid locations are\n%s\t' % (loc, '\n\t'.join(self.codes.iterkeys()))) loc = 'bottom' if is_string_like(loc): loc = self.codes.get(loc, 1) self.set_figure(ax.figure) self._axes = ax self._loc = loc self._bbox = bbox # use axes coords self.set_transform(ax.transAxes) self._texts = [] self._cells = {} self._autoRows = [] self._autoColumns = [] self._autoFontsize = True self._cachedRenderer = None def add_cell(self, row, col, *args, **kwargs): """ Add a cell to the table. """ xy = (0, 0) cell = Cell(xy, *args, **kwargs) cell.set_figure(self.figure) cell.set_transform(self.get_transform()) cell.set_clip_on(False) self._cells[(row, col)] = cell def _approx_text_height(self): return (self.FONTSIZE / 72.0 * self.figure.dpi / self._axes.bbox.height * 1.2) @allow_rasterization def draw(self, renderer): # Need a renderer to do hit tests on mouseevent; assume the last one # will do if renderer is None: renderer = self._cachedRenderer if renderer is None: raise RuntimeError('No renderer defined') self._cachedRenderer = renderer if not self.get_visible(): return renderer.open_group('table') self._update_positions(renderer) keys = self._cells.keys() keys.sort() for key in keys: self._cells[key].draw(renderer) #for c in self._cells.itervalues(): # c.draw(renderer) renderer.close_group('table') def _get_grid_bbox(self, renderer): """Get a bbox, in axes co-ordinates for the cells. Only include those in the range (0,0) to (maxRow, maxCol)""" boxes = [self._cells[pos].get_window_extent(renderer) for pos in self._cells.iterkeys() if pos[0] >= 0 and pos[1] >= 0] bbox = Bbox.union(boxes) return bbox.inverse_transformed(self.get_transform()) def contains(self, mouseevent): """Test whether the mouse event occurred in the table. Returns T/F, {} """ if callable(self._contains): return self._contains(self, mouseevent) # TODO: Return index of the cell containing the cursor so that the user # doesn't have to bind to each one individually. if self._cachedRenderer is not None: boxes = [self._cells[pos].get_window_extent(self._cachedRenderer) for pos in self._cells.iterkeys() if pos[0] >= 0 and pos[1] >= 0] bbox = Bbox.union(boxes) return bbox.contains(mouseevent.x, mouseevent.y), {} else: return False, {} def get_children(self): 'Return the Artists contained by the table' return self._cells.values() get_child_artists = get_children # backward compatibility def get_window_extent(self, renderer): 'Return the bounding box of the table in window coords' boxes = [cell.get_window_extent(renderer) for cell in self._cells.values()] return Bbox.union(boxes) def _do_cell_alignment(self): """ Calculate row heights and column widths. Position cells accordingly. """ # Calculate row/column widths widths = {} heights = {} for (row, col), cell in self._cells.iteritems(): height = heights.setdefault(row, 0.0) heights[row] = max(height, cell.get_height()) width = widths.setdefault(col, 0.0) widths[col] = max(width, cell.get_width()) # work out left position for each column xpos = 0 lefts = {} cols = widths.keys() cols.sort() for col in cols: lefts[col] = xpos xpos += widths[col] ypos = 0 bottoms = {} rows = heights.keys() rows.sort() rows.reverse() for row in rows: bottoms[row] = ypos ypos += heights[row] # set cell positions for (row, col), cell in self._cells.iteritems(): cell.set_x(lefts[col]) cell.set_y(bottoms[row]) def auto_set_column_width(self, col): self._autoColumns.append(col) def _auto_set_column_width(self, col, renderer): """ Automagically set width for column. """ cells = [key for key in self._cells if key[1] == col] # find max width width = 0 for cell in cells: c = self._cells[cell] width = max(c.get_required_width(renderer), width) # Now set the widths for cell in cells: self._cells[cell].set_width(width) def auto_set_font_size(self, value=True): """ Automatically set font size. """ self._autoFontsize = value def _auto_set_font_size(self, renderer): if len(self._cells) == 0: return fontsize = self._cells.values()[0].get_fontsize() cells = [] for key, cell in self._cells.iteritems(): # ignore auto-sized columns if key[1] in self._autoColumns: continue size = cell.auto_set_font_size(renderer) fontsize = min(fontsize, size) cells.append(cell) # now set all fontsizes equal for cell in self._cells.itervalues(): cell.set_fontsize(fontsize) def scale(self, xscale, yscale): """ Scale column widths by xscale and row heights by yscale. """ for c in self._cells.itervalues(): c.set_width(c.get_width() * xscale) c.set_height(c.get_height() * yscale) def set_fontsize(self, size): """ Set the fontsize of the cell text ACCEPTS: a float in points """ for cell in self._cells.itervalues(): cell.set_fontsize(size) def _offset(self, ox, oy): 'Move all the artists by ox,oy (axes coords)' for c in self._cells.itervalues(): x, y = c.get_x(), c.get_y() c.set_x(x + ox) c.set_y(y + oy) def _update_positions(self, renderer): # called from renderer to allow more precise estimates of # widths and heights with get_window_extent # Do any auto width setting for col in self._autoColumns: self._auto_set_column_width(col, renderer) if self._autoFontsize: self._auto_set_font_size(renderer) # Align all the cells self._do_cell_alignment() bbox = self._get_grid_bbox(renderer) l, b, w, h = bbox.bounds if self._bbox is not None: # Position according to bbox rl, rb, rw, rh = self._bbox self.scale(rw / w, rh / h) ox = rl - l oy = rb - b self._do_cell_alignment() else: # Position using loc (BEST, UR, UL, LL, LR, CL, CR, LC, UC, C, TR, TL, BL, BR, R, L, T, B) = range(len(self.codes)) # defaults for center ox = (0.5 - w / 2) - l oy = (0.5 - h / 2) - b if self._loc in (UL, LL, CL): # left ox = self.AXESPAD - l if self._loc in (BEST, UR, LR, R, CR): # right ox = 1 - (l + w + self.AXESPAD) if self._loc in (BEST, UR, UL, UC): # upper oy = 1 - (b + h + self.AXESPAD) if self._loc in (LL, LR, LC): # lower oy = self.AXESPAD - b if self._loc in (LC, UC, C): # center x ox = (0.5 - w / 2) - l if self._loc in (CL, CR, C): # center y oy = (0.5 - h / 2) - b if self._loc in (TL, BL, L): # out left ox = - (l + w) if self._loc in (TR, BR, R): # out right ox = 1.0 - l if self._loc in (TR, TL, T): # out top oy = 1.0 - b if self._loc in (BL, BR, B): # out bottom oy = - (b + h) self._offset(ox, oy) def get_celld(self): 'return a dict of cells in the table' return self._cells def table(ax, cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None): """ TABLE(cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None) Factory function to generate a Table instance. Thanks to John Gill for providing the class and table. """ # Check we have some cellText if cellText is None: # assume just colours are needed rows = len(cellColours) cols = len(cellColours[0]) cellText = [[''] * rows] * cols rows = len(cellText) cols = len(cellText[0]) for row in cellText: assert len(row) == cols if cellColours is not None: assert len(cellColours) == rows for row in cellColours: assert len(row) == cols else: cellColours = ['w' * cols] * rows # Set colwidths if not given if colWidths is None: colWidths = [1.0 / cols] * cols # Check row and column labels rowLabelWidth = 0 if rowLabels is None: if rowColours is not None: rowLabels = [''] * cols rowLabelWidth = colWidths[0] elif rowColours is None: rowColours = 'w' * rows if rowLabels is not None: assert len(rowLabels) == rows offset = 0 if colLabels is None: if colColours is not None: colLabels = [''] * rows offset = 1 elif colColours is None: colColours = 'w' * cols offset = 1 if rowLabels is not None: assert len(rowLabels) == rows # Set up cell colours if not given if cellColours is None: cellColours = ['w' * cols] * rows # Now create the table table = Table(ax, loc, bbox) height = table._approx_text_height() # Add the cells for row in xrange(rows): for col in xrange(cols): table.add_cell(row + offset, col, width=colWidths[col], height=height, text=cellText[row][col], facecolor=cellColours[row][col], loc=cellLoc) # Do column labels if colLabels is not None: for col in xrange(cols): table.add_cell(0, col, width=colWidths[col], height=height, text=colLabels[col], facecolor=colColours[col], loc=colLoc) # Do row labels if rowLabels is not None: for row in xrange(rows): table.add_cell(row + offset, -1, width=rowLabelWidth or 1e-15, height=height, text=rowLabels[row], facecolor=rowColours[row], loc=rowLoc) if rowLabelWidth == 0: table.auto_set_column_width(-1) ax.add_table(table) return table docstring.interpd.update(Table=artist.kwdoc(Table))
mit
andrewgiessel/folium
folium/utilities.py
1
19979
# -*- coding: utf-8 -*- """ Utilities ------- Utility module for Folium helper functions. """ from __future__ import absolute_import from __future__ import print_function from __future__ import division import time import math import zlib import struct import json import base64 from jinja2 import Environment, PackageLoader try: import pandas as pd except ImportError: pd = None try: import numpy as np except ImportError: np = None from folium.six import iteritems, text_type, binary_type def get_templates(): """Get Jinja templates.""" return Environment(loader=PackageLoader('folium', 'templates')) def legend_scaler(legend_values, max_labels=10.0): """ Downsamples the number of legend values so that there isn't a collision of text on the legend colorbar (within reason). The colorbar seems to support ~10 entries as a maximum. """ if len(legend_values) < max_labels: legend_ticks = legend_values else: spacer = int(math.ceil(len(legend_values)/max_labels)) legend_ticks = [] for i in legend_values[::spacer]: legend_ticks += [i] legend_ticks += ['']*(spacer-1) return legend_ticks def linear_gradient(hexList, nColors): """ Given a list of hexcode values, will return a list of length nColors where the colors are linearly interpolated between the (r, g, b) tuples that are given. Example: linear_gradient([(0, 0, 0), (255, 0, 0), (255, 255, 0)], 100) """ def _scale(start, finish, length, i): """ Return the value correct value of a number that is in between start and finish, for use in a loop of length *length*. """ base = 16 fraction = float(i) / (length - 1) raynge = int(finish, base) - int(start, base) thex = hex(int(int(start, base) + fraction * raynge)).split('x')[-1] if len(thex) != 2: thex = '0' + thex return thex allColors = [] # Separate (R, G, B) pairs. for start, end in zip(hexList[:-1], hexList[1:]): # Linearly intepolate between pair of hex ###### values and # add to list. nInterpolate = 765 for index in range(nInterpolate): r = _scale(start[1:3], end[1:3], nInterpolate, index) g = _scale(start[3:5], end[3:5], nInterpolate, index) b = _scale(start[5:7], end[5:7], nInterpolate, index) allColors.append(''.join(['#', r, g, b])) # Pick only nColors colors from the total list. result = [] for counter in range(nColors): fraction = float(counter) / (nColors - 1) index = int(fraction * (len(allColors) - 1)) result.append(allColors[index]) return result def color_brewer(color_code, n=6): """ Generate a colorbrewer color scheme of length 'len', type 'scheme. Live examples can be seen at http://colorbrewer2.org/ """ maximum_n = 253 scheme_info = {'BuGn': 'Sequential', 'BuPu': 'Sequential', 'GnBu': 'Sequential', 'OrRd': 'Sequential', 'PuBu': 'Sequential', 'PuBuGn': 'Sequential', 'PuRd': 'Sequential', 'RdPu': 'Sequential', 'YlGn': 'Sequential', 'YlGnBu': 'Sequential', 'YlOrBr': 'Sequential', 'YlOrRd': 'Sequential', 'BrBg': 'Diverging', 'PiYG': 'Diverging', 'PRGn': 'Diverging', 'PuOr': 'Diverging', 'RdBu': 'Diverging', 'RdGy': 'Diverging', 'RdYlBu': 'Diverging', 'RdYlGn': 'Diverging', 'Spectral': 'Diverging', 'Accent': 'Qualitative', 'Dark2': 'Qualitative', 'Paired': 'Qualitative', 'Pastel1': 'Qualitative', 'Pastel2': 'Qualitative', 'Set1': 'Qualitative', 'Set2': 'Qualitative', 'Set3': 'Qualitative', } schemes = {'BuGn': ['#EDF8FB', '#CCECE6', '#CCECE6', '#66C2A4', '#41AE76', '#238B45', '#005824'], 'BuPu': ['#EDF8FB', '#BFD3E6', '#9EBCDA', '#8C96C6', '#8C6BB1', '#88419D', '#6E016B'], 'GnBu': ['#F0F9E8', '#CCEBC5', '#A8DDB5', '#7BCCC4', '#4EB3D3', '#2B8CBE', '#08589E'], 'OrRd': ['#FEF0D9', '#FDD49E', '#FDBB84', '#FC8D59', '#EF6548', '#D7301F', '#990000'], 'PuBu': ['#F1EEF6', '#D0D1E6', '#A6BDDB', '#74A9CF', '#3690C0', '#0570B0', '#034E7B'], 'PuBuGn': ['#F6EFF7', '#D0D1E6', '#A6BDDB', '#67A9CF', '#3690C0', '#02818A', '#016450'], 'PuRd': ['#F1EEF6', '#D4B9DA', '#C994C7', '#DF65B0', '#E7298A', '#CE1256', '#91003F'], 'RdPu': ['#FEEBE2', '#FCC5C0', '#FA9FB5', '#F768A1', '#DD3497', '#AE017E', '#7A0177'], 'YlGn': ['#FFFFCC', '#D9F0A3', '#ADDD8E', '#78C679', '#41AB5D', '#238443', '#005A32'], 'YlGnBu': ['#FFFFCC', '#C7E9B4', '#7FCDBB', '#41B6C4', '#1D91C0', '#225EA8', '#0C2C84'], 'YlOrBr': ['#FFFFD4', '#FEE391', '#FEC44F', '#FE9929', '#EC7014', '#CC4C02', '#8C2D04'], 'YlOrRd': ['#FFFFB2', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A', '#E31A1C', '#B10026'], 'BrBg': ['#8c510a', '#d8b365', '#f6e8c3', '#c7eae5', '#5ab4ac', '#01665e'], 'PiYG': ['#c51b7d', '#e9a3c9', '#fde0ef', '#e6f5d0', '#a1d76a', '#4d9221'], 'PRGn': ['#762a83', '#af8dc3', '#e7d4e8', '#d9f0d3', '#7fbf7b', '#1b7837'], 'PuOr': ['#b35806', '#f1a340', '#fee0b6', '#d8daeb', '#998ec3', '#542788'], 'RdBu': ['#b2182b', '#ef8a62', '#fddbc7', '#d1e5f0', '#67a9cf', '#2166ac'], 'RdGy': ['#b2182b', '#ef8a62', '#fddbc7', '#e0e0e0', '#999999', '#4d4d4d'], 'RdYlBu': ['#d73027', '#fc8d59', '#fee090', '#e0f3f8', '#91bfdb', '#4575b4'], 'RdYlGn': ['#d73027', '#fc8d59', '#fee08b', '#d9ef8b', '#91cf60', '#1a9850'], 'Spectral': ['#d53e4f', '#fc8d59', '#fee08b', '#e6f598', '#99d594', '#3288bd'], 'Accent': ['#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f'], 'Dark2': ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02'], 'Paired': ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c'], 'Pastel1': ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc'], 'Pastel2': ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae'], 'Set1': ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33'], 'Set2': ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854', '#ffd92f'], 'Set3': ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462'], } # Raise an error if the n requested is greater than the maximum. if n > maximum_n: raise ValueError("The maximum number of colors in a" " ColorBrewer sequential color series is 253") # Only if n is greater than six do we interpolate values. if n > 6: if color_code not in schemes: color_scheme = None else: # Check to make sure that it is not a qualitative scheme. if scheme_info[color_code] == 'Qualitative': raise ValueError("Expanded color support is not available" " for Qualitative schemes, restrict" " number of colors to 6") else: color_scheme = linear_gradient(schemes.get(color_code), n) else: color_scheme = schemes.get(color_code, None) return color_scheme def transform_data(data): """ Transform Pandas DataFrame into JSON format. Parameters ---------- data: DataFrame or Series Pandas DataFrame or Series Returns ------- JSON compatible dict Example ------- >>> transform_data(df) """ if pd is None: raise ImportError("The Pandas package is required" " for this functionality") if np is None: raise ImportError("The NumPy package is required" " for this functionality") def type_check(value): """ Type check values for JSON serialization. Native Python JSON serialization will not recognize some Numpy data types properly, so they must be explicitly converted. """ if pd.isnull(value): return None elif (isinstance(value, pd.tslib.Timestamp) or isinstance(value, pd.Period)): return time.mktime(value.timetuple()) elif isinstance(value, (int, np.integer)): return int(value) elif isinstance(value, (float, np.float_)): return float(value) elif isinstance(value, str): return str(value) else: return value if isinstance(data, pd.Series): json_data = [{type_check(x): type_check(y) for x, y in iteritems(data)}] elif isinstance(data, pd.DataFrame): json_data = [{type_check(y): type_check(z) for x, y, z in data.itertuples()}] return json_data def split_six(series=None): """ Given a Pandas Series, get a domain of values from zero to the 90% quantile rounded to the nearest order-of-magnitude integer. For example, 2100 is rounded to 2000, 2790 to 3000. Parameters ---------- series: Pandas series, default None Returns ------- list """ if pd is None: raise ImportError("The Pandas package is required" " for this functionality") if np is None: raise ImportError("The NumPy package is required" " for this functionality") def base(x): if x > 0: base = pow(10, math.floor(math.log10(x))) return round(x/base)*base else: return 0 quants = [0, 50, 75, 85, 90] # Some weirdness in series quantiles a la 0.13. arr = series.values return [base(np.percentile(arr, x)) for x in quants] def mercator_transform(data, lat_bounds, origin='upper', height_out=None): """Transforms an image computed in (longitude,latitude) coordinates into the a Mercator projection image. Parameters ---------- data: numpy array or equivalent list-like object. Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) lat_bounds : length 2 tuple Minimal and maximal value of the latitude of the image. origin : ['upper' | 'lower'], optional, default 'upper' Place the [0,0] index of the array in the upper left or lower left corner of the axes. height_out : int, default None The expected height of the output. If None, the height of the input is used. """ if np is None: raise ImportError("The NumPy package is required" " for this functionality") mercator = lambda x: np.arcsinh(np.tan(x*np.pi/180.))*180./np.pi array = np.atleast_3d(data).copy() height, width, nblayers = array.shape lat_min, lat_max = lat_bounds if height_out is None: height_out = height # Eventually flip the image if origin == 'upper': array = array[::-1, :, :] lats = (lat_min + np.linspace(0.5/height, 1.-0.5/height, height) * (lat_max-lat_min)) latslats = (mercator(lat_min) + np.linspace(0.5/height_out, 1.-0.5/height_out, height_out) * (mercator(lat_max)-mercator(lat_min))) out = np.zeros((height_out, width, nblayers)) for i in range(width): for j in range(4): out[:, i, j] = np.interp(latslats, mercator(lats), array[:, i, j]) # Eventually flip the image. if origin == 'upper': out = out[::-1, :, :] return out def image_to_url(image, mercator_project=False, colormap=None, origin='upper', bounds=((-90, -180), (90, 180))): """Infers the type of an image argument and transforms it into a URL. Parameters ---------- image: string, file or array-like object * If string, it will be written directly in the output file. * If file, it's content will be converted as embedded in the output file. * If array-like, it will be converted to PNG base64 string and embedded in the output. origin : ['upper' | 'lower'], optional, default 'upper' Place the [0, 0] index of the array in the upper left or lower left corner of the axes. colormap : callable, used only for `mono` image. Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] for transforming a mono image into RGB. It must output iterables of length 3 or 4, with values between 0. and 1. Hint : you can use colormaps from `matplotlib.cm`. mercator_project : bool, default False, used for array-like image. Transforms the data to project (longitude,latitude) coordinates to the Mercator projection. bounds: list-like, default ((-90, -180), (90, 180)) Image bounds on the map in the form [[lat_min, lon_min], [lat_max, lon_max]]. Only used if mercator_project is True. """ if hasattr(image, 'read'): # We got an image file. if hasattr(image, 'name'): # We try to get the image format from the file name. fileformat = image.name.lower().split('.')[-1] else: fileformat = 'png' url = "data:image/{};base64,{}".format( fileformat, base64.b64encode(image.read()).decode('utf-8')) elif (not (isinstance(image, text_type) or isinstance(image, binary_type))) and hasattr(image, '__iter__'): # We got an array-like object. if mercator_project: data = mercator_transform(image, [bounds[0][0], bounds[1][0]], origin=origin) else: data = image png = write_png(data, origin=origin, colormap=colormap) url = "data:image/png;base64," + base64.b64encode(png).decode('utf-8') else: # We got an URL. url = json.loads(json.dumps(image)) return url.replace('\n', ' ') def write_png(data, origin='upper', colormap=None): """ Transform an array of data into a PNG string. This can be written to disk using binary I/O, or encoded using base64 for an inline PNG like this: >>> png_str = write_png(array) >>> "data:image/png;base64,"+png_str.encode('base64') Inspired from http://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image Parameters ---------- data: numpy array or equivalent list-like object. Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) origin : ['upper' | 'lower'], optional, default 'upper' Place the [0,0] index of the array in the upper left or lower left corner of the axes. colormap : callable, used only for `mono` image. Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] for transforming a mono image into RGB. It must output iterables of length 3 or 4, with values between 0. and 1. Hint: you can use colormaps from `matplotlib.cm`. Returns ------- PNG formatted byte string """ if np is None: raise ImportError("The NumPy package is required" " for this functionality") if colormap is None: colormap = lambda x: (x, x, x, 1) array = np.atleast_3d(data) height, width, nblayers = array.shape if nblayers not in [1, 3, 4]: raise ValueError("Data must be NxM (mono), " "NxMx3 (RGB), or NxMx4 (RGBA)") assert array.shape == (height, width, nblayers) if nblayers == 1: array = np.array(list(map(colormap, array.ravel()))) nblayers = array.shape[1] if nblayers not in [3, 4]: raise ValueError("colormap must provide colors of" "length 3 (RGB) or 4 (RGBA)") array = array.reshape((height, width, nblayers)) assert array.shape == (height, width, nblayers) if nblayers == 3: array = np.concatenate((array, np.ones((height, width, 1))), axis=2) nblayers = 4 assert array.shape == (height, width, nblayers) assert nblayers == 4 # Normalize to uint8 if it isn't already. if array.dtype != 'uint8': array = array * 255./array.max(axis=(0, 1)).reshape((1, 1, 4)) array = array.astype('uint8') # Eventually flip the image. if origin == 'lower': array = array[::-1, :, :] # Transform the array to bytes. raw_data = b''.join([b'\x00' + array[i, :, :].tobytes() for i in range(height)]) def png_pack(png_tag, data): chunk_head = png_tag + data return (struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))) return b''.join([ b'\x89PNG\r\n\x1a\n', png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)), png_pack(b'IDAT', zlib.compress(raw_data, 9)), png_pack(b'IEND', b'')]) def _camelify(out): return (''.join(["_" + x.lower() if i < len(out)-1 and x.isupper() and out[i+1].islower() # noqa else x.lower() + "_" if i < len(out)-1 and x.islower() and out[i+1].isupper() # noqa else x.lower() for i, x in enumerate(list(out))])).lstrip('_').replace('__', '_') # noqa def _parse_size(value): try: if isinstance(value, int) or isinstance(value, float): value_type = 'px' value = float(value) assert value > 0 else: value_type = '%' value = float(value.strip('%')) assert 0 <= value <= 100 except: msg = "Cannot parse value {!r} as {!r}".format raise ValueError(msg(value, value_type)) return value, value_type def _locations_mirror(x): """Mirrors the points in a list-of-list-of-...-of-list-of-points. For example: >>> _locations_mirror([[[1, 2], [3, 4]], [5, 6], [7, 8]]) [[[2, 1], [4, 3]], [6, 5], [8, 7]] """ if hasattr(x, '__iter__'): if hasattr(x[0], '__iter__'): return list(map(_locations_mirror, x)) else: return list(x[::-1]) else: return x def _locations_tolist(x): """Transforms recursively a list of iterables into a list of list. """ if hasattr(x, '__iter__'): return list(map(_locations_tolist, x)) else: return x
mit
anacode/anacode-toolkit
anacode/api/writers.py
1
20217
# -*- coding: utf-8 -*- import os import csv import datetime import pandas as pd from itertools import chain from functools import partial from anacode import codes def backup(root, files): """Backs up `files` from `root` directory and return list of backed up file names. Backed up files will have datetime suffix appended to original file name. :param root: Absolute path to folder where files to backup are located :type root: str :param files: Names of files that needs backing up :type files: str :return: list -- List of backed up file names """ backed_up = [] join = os.path.join root_contents = os.listdir(root) dt_str = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S') for file_name in files: if file_name not in root_contents: continue new_name = file_name + '_' + dt_str os.rename(join(root, file_name), join(root, new_name)) backed_up.append(new_name) return backed_up HEADERS = { 'categories': [u'doc_id', u'text_order', u'category', u'probability'], 'concepts': [u'doc_id', u'text_order', u'concept', u'freq', u'relevance_score', u'concept_type'], 'concepts_surface_strings': [u'doc_id', u'text_order', u'concept', u'surface_string', u'text_span'], 'sentiments': [u'doc_id', u'text_order', u'sentiment_value'], 'absa_entities': [u'doc_id', u'text_order', u'entity_name', u'entity_type', u'surface_string', u'text_span'], 'absa_normalized_texts': [u'doc_id', u'text_order', u'normalized_text'], 'absa_relations': [u'doc_id', u'text_order', u'relation_id', u'opinion_holder', u'restriction', u'sentiment_value', u'is_external', u'surface_string', u'text_span'], 'absa_relations_entities': [u'doc_id', u'text_order', u'relation_id', u'entity_type', u'entity_name'], 'absa_evaluations': [u'doc_id', u'text_order', u'evaluation_id', u'sentiment_value', u'surface_string', u'text_span'], 'absa_evaluations_entities': [u'doc_id', u'text_order', u'evaluation_id', u'entity_type', u'entity_name'], } # `anacode.agg.aggregations.ApiDataset.from_path` depends # on ordering of files defined in values here CSV_FILES = { 'categories': ['categories.csv'], 'concepts': ['concepts.csv', 'concepts_surface_strings.csv'], 'sentiments': ['sentiments.csv'], 'absa': [ 'absa_entities.csv', 'absa_normalized_texts.csv', 'absa_relations.csv', 'absa_relations_entities.csv', 'absa_evaluations.csv', 'absa_evaluations_entities.csv' ] } def categories_to_list(doc_id, analyzed, single_document=False): """Converts categories response to flat list with doc_id included. :param doc_id: Will be inserted to each row as first element :param analyzed: Response json from anacode api for categories call :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool :return: dict -- Dictionary with one key 'categories' pointing to flat list of categories """ cat_list = [] for order, text_analyzed in enumerate(analyzed): for result_dict in text_analyzed: row = [doc_id, 0, result_dict.get('label'), result_dict.get('probability')] if single_document: row[1] += order else: row[0] += order cat_list.append(row) return {'categories': cat_list} def concepts_to_list(doc_id, analyzed, single_document=False): """Converts concepts response to flat lists with doc_id included :param doc_id: Will be inserted to each row as first element :param analyzed: Response json from anacode api for concepts call :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool :return: dict -- Dictionary with two keys: 'concepts' pointing to flat list of found concepts and their metadata and 'concepts_surface_strings' pointing to flat list of strings realizing found concepts """ con_list, exp_list = [], [] for order, text_analyzed in enumerate(analyzed): for concept in text_analyzed or []: row = [doc_id, 0, concept.get('concept'), concept.get('freq'), concept.get('relevance_score'), concept.get('type')] if single_document: row[1] += order else: row[0] += order con_list.append(row) for string in concept.get('surface', []): surface_str, span = string['surface_string'], string['span'] exp_list.append([row[0], row[1], concept.get('concept'), surface_str, '-'.join(map(str, span))]) return {'concepts': con_list, 'concepts_surface_strings': exp_list} def sentiments_to_list(doc_id, analyzed, single_document=False): """Converts sentiments response to flat lists with doc_id included :param doc_id: Will be inserted to each row as first element :param analyzed: Response json from anacode api for sentiment call :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool :return: dict -- Dictionary with one key 'sentiments' pointing to flat list of sentiment probabilities """ sen_list = [] for order, sentiment in enumerate(analyzed): row = [doc_id, 0, sentiment['sentiment_value']] if single_document: # this should not happen row[1] += order else: row[0] += order sen_list.append(row) return {'sentiments': sen_list} def _absa_entities_to_list(doc_id, order, entities): ent_list = [] for entity_dict in entities: text_span = '-'.join(map(str, entity_dict['surface']['span'])) surface_string = entity_dict['surface']['surface_string'] for semantics in entity_dict['semantics']: row = [doc_id, order, semantics['value'], semantics['type'], surface_string, text_span] ent_list.append(row) return ent_list def _absa_normalized_text_to_list(doc_id, order, normalized_text): return [[doc_id, order, normalized_text]] def _absa_relations_to_list(doc_id, order, relations): rel_list, ent_list = [], [] for rel_index, rel in enumerate(relations): rel_row = [doc_id, order, rel_index, rel['semantics']['opinion_holder'], rel['semantics']['restriction'], rel['semantics']['sentiment_value'], rel['external_entity'], rel['surface']['surface_string'], '-'.join(map(str, rel['surface']['span']))] rel_list.append(rel_row) for ent in rel['semantics'].get('entity', []): ent_row = [doc_id, order, rel_index, ent['type'], ent['value']] ent_list.append(ent_row) return rel_list, ent_list def _absa_evaluations_to_list(doc_id, order, evaluations): eval_list, ent_list = [], [] for eval_index, evaluation in enumerate(evaluations): eval_row = [doc_id, order, eval_index, evaluation['semantics']['sentiment_value'], evaluation['surface']['surface_string'], '-'.join(map(str, evaluation['surface']['span']))] eval_list.append(eval_row) for ent in evaluation['semantics'].get('entity', []): ent_row = [doc_id, order, eval_index, ent['type'], ent['value']] ent_list.append(ent_row) return eval_list, ent_list def absa_to_list(doc_id, analyzed, single_document=False): """Converts ABSA response to flat lists with doc_id included :param doc_id: Will be inserted to each row as first element :param analyzed: Response json from anacode api for ABSA call :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool :return: dict -- Dictionary with six keys: 'absa_entities' pointing to flat list of found entities with metadata, 'absa_normalized_texts' pointing to flat list of normalized chinese texts, 'absa_relations' pointing to found entity relations with metadata, 'absa_relations_entities' pointing to flat list of entities that belong to absa relations, 'absa_evaluations' pointing to flat list of entity evaluations with metadata and 'absa_evaluations_entities' specifying entities in absa_evaluations """ absa = { 'absa_entities': [], 'absa_normalized_texts': [], 'absa_relations': [], 'absa_relations_entities': [], 'absa_evaluations': [], 'absa_evaluations_entities': [] } for order, text_analyzed in enumerate(analyzed): if single_document: current_id = doc_id text_order = order else: current_id = doc_id + order text_order = 0 entities = text_analyzed['entities'] ents = _absa_entities_to_list(current_id, text_order, entities) text = text_analyzed['normalized_text'] texts = _absa_normalized_text_to_list(current_id, text_order, text) relations = text_analyzed['relations'] rels, rel_ents = _absa_relations_to_list(current_id, text_order, relations) evaluations = text_analyzed['evaluations'] evals, eval_ents = _absa_evaluations_to_list(current_id, text_order, evaluations) absa['absa_entities'].extend(ents) absa['absa_normalized_texts'].extend(texts) absa['absa_relations'].extend(rels) absa['absa_relations_entities'].extend(rel_ents) absa['absa_evaluations'].extend(evals) absa['absa_evaluations_entities'].extend(eval_ents) return absa class Writer(object): """Base "abstract" class containing common methods that are needed by all implementations of Writer interface. The writer interface consists of init, close and write_bulk methods. """ def __init__(self): self.ids = {'scrape': 0, 'analyze': 0} def write_row(self, call_type, call_result): """Decides what kind of data it got and calls appropriate write method. :param call_type: Library's ID of anacode call :type call_type: int :param call_result: JSON response from Anacode API :type call_result: list """ if call_type == codes.SCRAPE: self.write_scrape(call_result) if call_type == codes.ANALYZE: self.write_analysis(call_result) def _add_new_data_from_dict(self, new_data): """Not implemented here! Used by write methods to submit new Anacode API response data for storage. :param new_data: dict; keys are data sets names and values are flat lists of rows :type new_data: dict """ pass def write_scrape(self, scraped): self.ids['scrape'] += 1 def write_analysis(self, analyzed): """Inspects analysis result for performed analysis and delegates persisting of results to appropriate write methods. :param analyzed: JSON object analysis response :type: dict """ single_document = analyzed.get('single_document', False) analyzed_length = 1 if 'categories' in analyzed: categories = analyzed['categories'] self.write_categories(categories, single_document=single_document) if not single_document: analyzed_length = len(categories) if 'concepts' in analyzed: concepts = analyzed['concepts'] self.write_concepts(concepts, single_document=single_document) if not single_document: analyzed_length = len(concepts) if 'sentiment' in analyzed: sentiment = analyzed['sentiment'] self.write_sentiment(sentiment, single_document=single_document) if not single_document: analyzed_length = len(sentiment) if 'absa' in analyzed: absa = analyzed['absa'] self.write_absa(analyzed['absa'], single_document=single_document) if not single_document: analyzed_length = len(absa) self.ids['analyze'] += analyzed_length def write_categories(self, analyzed, single_document=False): """Converts categories analysis result to flat lists and stores them. :param analyzed: JSON categories analysis result :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool """ doc_id = self.ids['analyze'] new_data = categories_to_list(doc_id, analyzed, single_document) self._add_new_data_from_dict(new_data) def write_concepts(self, analyzed, single_document=False): """Converts concepts analysis result to flat lists and stores them. :param analyzed: JSON concepts analysis result :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool """ doc_id = self.ids['analyze'] new_data = concepts_to_list(doc_id, analyzed, single_document) self._add_new_data_from_dict(new_data) def write_sentiment(self, analyzed, single_document=False): """Converts sentiment analysis result to flat lists and stores them. :param analyzed: JSON sentiment analysis result :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool """ doc_id = self.ids['analyze'] new_data = sentiments_to_list(doc_id, analyzed, single_document) self._add_new_data_from_dict(new_data) def write_absa(self, analyzed, single_document=False): """Converts absa analysis result to flat lists and stores them. :param analyzed: JSON absa analysis result :type analyzed: list :param single_document: Is analysis describing just one document :type single_document: bool """ doc_id = self.ids['analyze'] new_data = absa_to_list(doc_id, analyzed, single_document) self._add_new_data_from_dict(new_data) def write_bulk(self, results): """Stores multiple anacode api's JSON responses marked with call IDs as tuples (call_id, call_result). Both scrape and analyze call IDs are defined in anacode.codes module. :param results: List of anacode responses with IDs of calls used :type results: list """ for call_type, call_result in results: self.write_row(call_type, call_result) def init(self): """Not implemented here! Each subclass should decide what to do here.""" pass def close(self): """Not implemented here! Each subclass should decide what to do here.""" pass def __enter__(self): self.init() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() class DataFrameWriter(Writer): """Writes Anacode API output into pandas.DataFrame instances.""" def __init__(self, frames=None): """Initializes dictionary of result frames. Alternatively uses given frames dict for storage. :param frames: Might be specified to use this instead of new dict :type frames: dict """ super(DataFrameWriter, self).__init__() self.frames = {} if frames is None else frames self._row_data = {} def init(self): """Initialized empty lists for each possible data frame.""" self._row_data = { 'categories': [], 'concepts': [], 'concepts_surface_strings': [], 'sentiments': [], 'absa_entities': [], 'absa_normalized_texts': [], 'absa_relations': [], 'absa_relations_entities': [], 'absa_evaluations': [], 'absa_evaluations_entities': [], } def close(self): """Creates pandas data frames to self.frames dict and clears internal state. """ for name, row in self._row_data.items(): if len(row) > 0: self.frames[name] = pd.DataFrame(row, columns=HEADERS[name]) self._row_data = {} def _add_new_data_from_dict(self, new_data): """Stores anacode api result converted to flat lists. :param new_data: Anacode api result :param new_data: list """ for name, row_list in new_data.items(): self._row_data[name].extend(row_list) class CSVWriter(Writer): def __init__(self, target_dir='.'): """Initializes Writer to store Anacode API analysis results in target_dir in csv files. :param target_dir: Path to directory where to store csv files :type target_dir: str """ super(CSVWriter, self).__init__() self.target_dir = os.path.abspath(os.path.expanduser(target_dir)) self._files = {} self.csv = {} def _open_csv(self, csv_name): path = partial(os.path.join, self.target_dir) try: return open(path(csv_name), 'w', newline='') except TypeError: return open(path(csv_name), 'wb') def init(self): """Opens all csv files for writing and writes headers to them.""" self.close() backup(self.target_dir, chain.from_iterable(CSV_FILES.values())) self._files = { 'categories': self._open_csv('categories.csv'), 'concepts': self._open_csv('concepts.csv'), 'concepts_surface_strings': self._open_csv( 'concepts_surface_strings.csv' ), 'sentiments': self._open_csv('sentiments.csv'), 'absa_entities': self._open_csv('absa_entities.csv'), 'absa_normalized_texts': self._open_csv( 'absa_normalized_texts.csv' ), 'absa_relations': self._open_csv('absa_relations.csv'), 'absa_relations_entities': self._open_csv( 'absa_relations_entities.csv' ), 'absa_evaluations': self._open_csv('absa_evaluations.csv'), 'absa_evaluations_entities': self._open_csv( 'absa_evaluations_entities.csv' ), } self.csv = {name: csv.writer(fp) for name, fp in self._files.items()} for name, writer in self.csv.items(): writer.writerow(HEADERS[name]) def _csv_has_content(self, csv_path): if not os.path.isfile(csv_path): return False with open(csv_path) as fp: for line_count, line in enumerate(fp): if line_count == 1 and len(line.strip()) != '': return True return False def close(self): """Closes all csv files and removes empty ones.""" for name, file in self._files.items(): try: file.close() except (IOError, AttributeError): print('Problem closing "{}"'.format(name)) for file_list in CSV_FILES.values(): for file_name in file_list: path = os.path.join(self.target_dir, file_name) if os.path.isfile(path) and not self._csv_has_content(path): os.unlink(path) self._files = {} self.csv = {} def _add_new_data_from_dict(self, new_data): """Stores anacode api result converted to flat lists. :param new_data: Anacode api result :param new_data: list """ for name, row_list in new_data.items(): self.csv[name].writerows(row_list)
bsd-3-clause
ischwabacher/seaborn
seaborn/algorithms.py
35
6889
"""Algorithms to support fitting routines in seaborn plotting functions.""" from __future__ import division import numpy as np from scipy import stats from .external.six.moves import range def bootstrap(*args, **kwargs): """Resample one or more arrays with replacement and store aggregate values. Positional arguments are a sequence of arrays to bootstrap along the first axis and pass to a summary function. Keyword arguments: n_boot : int, default 10000 Number of iterations axis : int, default None Will pass axis to ``func`` as a keyword argument. units : array, default None Array of sampling unit IDs. When used the bootstrap resamples units and then observations within units instead of individual datapoints. smooth : bool, default False If True, performs a smoothed bootstrap (draws samples from a kernel destiny estimate); only works for one-dimensional inputs and cannot be used `units` is present. func : callable, default np.mean Function to call on the args that are passed in. random_seed : int | None, default None Seed for the random number generator; useful if you want reproducible resamples. Returns ------- boot_dist: array array of bootstrapped statistic values """ # Ensure list of arrays are same length if len(np.unique(list(map(len, args)))) > 1: raise ValueError("All input arrays must have the same length") n = len(args[0]) # Default keyword arguments n_boot = kwargs.get("n_boot", 10000) func = kwargs.get("func", np.mean) axis = kwargs.get("axis", None) units = kwargs.get("units", None) smooth = kwargs.get("smooth", False) random_seed = kwargs.get("random_seed", None) if axis is None: func_kwargs = dict() else: func_kwargs = dict(axis=axis) # Initialize the resampler rs = np.random.RandomState(random_seed) # Coerce to arrays args = list(map(np.asarray, args)) if units is not None: units = np.asarray(units) # Do the bootstrap if smooth: return _smooth_bootstrap(args, n_boot, func, func_kwargs) if units is not None: return _structured_bootstrap(args, n_boot, units, func, func_kwargs, rs) boot_dist = [] for i in range(int(n_boot)): resampler = rs.randint(0, n, n) sample = [a.take(resampler, axis=0) for a in args] boot_dist.append(func(*sample, **func_kwargs)) return np.array(boot_dist) def _structured_bootstrap(args, n_boot, units, func, func_kwargs, rs): """Resample units instead of datapoints.""" unique_units = np.unique(units) n_units = len(unique_units) args = [[a[units == unit] for unit in unique_units] for a in args] boot_dist = [] for i in range(int(n_boot)): resampler = rs.randint(0, n_units, n_units) sample = [np.take(a, resampler, axis=0) for a in args] lengths = map(len, sample[0]) resampler = [rs.randint(0, n, n) for n in lengths] sample = [[c.take(r, axis=0) for c, r in zip(a, resampler)] for a in sample] sample = list(map(np.concatenate, sample)) boot_dist.append(func(*sample, **func_kwargs)) return np.array(boot_dist) def _smooth_bootstrap(args, n_boot, func, func_kwargs): """Bootstrap by resampling from a kernel density estimate.""" n = len(args[0]) boot_dist = [] kde = [stats.gaussian_kde(np.transpose(a)) for a in args] for i in range(int(n_boot)): sample = [a.resample(n).T for a in kde] boot_dist.append(func(*sample, **func_kwargs)) return np.array(boot_dist) def randomize_corrmat(a, tail="both", corrected=True, n_iter=1000, random_seed=None, return_dist=False): """Test the significance of set of correlations with permutations. By default this corrects for multiple comparisons across one side of the matrix. Parameters ---------- a : n_vars x n_obs array array with variables as rows tail : both | upper | lower whether test should be two-tailed, or which tail to integrate over corrected : boolean if True reports p values with respect to the max stat distribution n_iter : int number of permutation iterations random_seed : int or None seed for RNG return_dist : bool if True, return n_vars x n_vars x n_iter Returns ------- p_mat : float array of probabilites for actual correlation from null CDF """ if tail not in ["upper", "lower", "both"]: raise ValueError("'tail' must be 'upper', 'lower', or 'both'") rs = np.random.RandomState(random_seed) a = np.asarray(a, np.float) flat_a = a.ravel() n_vars, n_obs = a.shape # Do the permutations to establish a null distribution null_dist = np.empty((n_vars, n_vars, n_iter)) for i_i in range(n_iter): perm_i = np.concatenate([rs.permutation(n_obs) + (v * n_obs) for v in range(n_vars)]) a_i = flat_a[perm_i].reshape(n_vars, n_obs) null_dist[..., i_i] = np.corrcoef(a_i) # Get the observed correlation values real_corr = np.corrcoef(a) # Figure out p values based on the permutation distribution p_mat = np.zeros((n_vars, n_vars)) upper_tri = np.triu_indices(n_vars, 1) if corrected: if tail == "both": max_dist = np.abs(null_dist[upper_tri]).max(axis=0) elif tail == "lower": max_dist = null_dist[upper_tri].min(axis=0) elif tail == "upper": max_dist = null_dist[upper_tri].max(axis=0) cdf = lambda x: stats.percentileofscore(max_dist, x) / 100. for i, j in zip(*upper_tri): observed = real_corr[i, j] if tail == "both": p_ij = 1 - cdf(abs(observed)) elif tail == "lower": p_ij = cdf(observed) elif tail == "upper": p_ij = 1 - cdf(observed) p_mat[i, j] = p_ij else: for i, j in zip(*upper_tri): null_corrs = null_dist[i, j] cdf = lambda x: stats.percentileofscore(null_corrs, x) / 100. observed = real_corr[i, j] if tail == "both": p_ij = 2 * (1 - cdf(abs(observed))) elif tail == "lower": p_ij = cdf(observed) elif tail == "upper": p_ij = 1 - cdf(observed) p_mat[i, j] = p_ij # Make p matrix symettrical with nans on the diagonal p_mat += p_mat.T p_mat[np.diag_indices(n_vars)] = np.nan if return_dist: return p_mat, null_dist return p_mat
bsd-3-clause
sgenoud/scikit-learn
sklearn/cluster/tests/test_dbscan.py
3
2890
""" Tests for DBSCAN clustering algorithm """ import pickle import numpy as np from numpy.testing import assert_equal from scipy.spatial import distance from sklearn.cluster.dbscan_ import DBSCAN, dbscan from .common import generate_clustered_data n_clusters = 3 X = generate_clustered_data(n_clusters=n_clusters) def test_dbscan_similarity(): """Tests the DBSCAN algorithm with a similarity array.""" # Parameters chosen specifically for this task. eps = 0.15 min_samples = 10 # Compute similarities D = distance.squareform(distance.pdist(X)) D /= np.max(D) # Compute DBSCAN core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - (1 if -1 in labels else 0) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric="precomputed", eps=eps, min_samples=min_samples) labels = db.fit(D).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_feature(): """Tests the DBSCAN algorithm with a feature vector array.""" # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 metric = 'euclidean' # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples) labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_callable(): """Tests the DBSCAN algorithm with a callable metric.""" # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 # metric is the function reference, not the string key. metric = distance.euclidean # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples) labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_pickle(): obj = DBSCAN() s = pickle.dumps(obj) assert_equal(type(pickle.loads(s)), obj.__class__)
bsd-3-clause
LaRiffle/axa_challenge
fonction_py/train.py
1
12400
from fonction_py.tools import * from fonction_py.preprocess import * from sklearn import linear_model import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn import cross_validation from sklearn.linear_model import LogisticRegression from sklearn import tree from sklearn import svm from sklearn import decomposition from sklearn.naive_bayes import GaussianNB from sklearn.ensemble import RandomForestRegressor from sklearn.ensemble import GradientBoostingRegressor from sklearn.grid_search import GridSearchCV from sklearn.grid_search import RandomizedSearchCV from scipy.stats import uniform as sp_randint from sklearn import datasets from sklearn.linear_model import Ridge from fonction_py.tim import * import time def faireTout(): fields = ['DATE', 'DAY_OFF', 'WEEK_END', 'DAY_WE_DS', 'ASS_ASSIGNMENT', 'CSPL_RECEIVED_CALLS' ] # selectionne les colonnes à lire c = pd.DataFrame() <<<<<<< HEAD listmodel = faireListModel()#recupere le nom et les modeles de chaque truc data=pd.read_csv("data/trainPure.csv", sep=";", usecols=fields) # LECTURE du fichier de train, resultat = pd.read_csv("data/submission.txt", sep="\t") # LECTURE dufichier de test res=[] model = listmodel[0] for model in listmodel: print(model[0]) #affiche le ass assignment (xTest, x, souvenir, y)=preprocessTOTAL(model[0]) # ajuste le nombre et le nom de feature pour que xTest et x aient les memes mod= GradientBoostingRegressor(loss='huber', alpha=0.9,n_estimators=100, max_depth=3,learning_rate=.1, min_samples_leaf=9,min_samples_split=9) mod.fit(x, y) #s'entraine pred = mod.predict(xTest) # predit pred[pred>max(y)*1.05]=max(y)*1.05 # pour pas predire trop grand pred[pred<0]=0 # pas de negatif pred =np.round(pred).astype(int) # to int souvenir['prediction']=pred # on l'ajoute a souvenir qui garde le format standard et la date pour qu'on remette tout a la bonne place a la fin resultat=pd.merge(resultat, souvenir, how='left',on=['DATE', 'ASS_ASSIGNMENT']) # on remet chaque prediction à la bonne ligne -> il cree prediction_x et prediction_y car l'ancienne prediction et la nouvelle colonne de prediction resultat=resultat.fillna(0) # on remplit les endroits ou on a pas predit avec des 0 resultat['prediction'] = resultat['prediction_x']+resultat['prediction_y'] # merge les deux colonnes del resultat['prediction_x'] del resultat['prediction_y'] ======= listmodel = faireListModel() #'Evenements', 'Gestion Amex' #setFields = set(pd.read_csv("data/fields.txt", sep=";")['0'].values) # resultat = pd.read_csv("data/submission.txt", sep="\t") i=0 # res = [] start_time = time.time() model = listmodel[24] data=pd.read_csv("data/trainPure.csv", sep=";", usecols=fields) # LECTURE resultat = pd.read_csv("data/submission.txt", sep="\t") # LECTURE res=[] for model in listmodel: i = i+1 print(model[0]) x,y = preprocess(data.copy(), model[0]) # rajoute les features model[1].fit(x, y) #model.score(xTrain, yTrain) (xTest, souvenir)=preprocessFINAL(x,model[0]) pred = model[1].predict(xTest) pred[pred>max(y)*1.05]=max(y)*1.05 pred[pred<0]=0 pred =np.round(pred) souvenir['prediction']=int(pred) resultat=pd.merge(resultat, souvenir, how='left',on=['DATE', 'ASS_ASSIGNMENT']) resultat=resultat.fillna(0) resultat['prediction'] = resultat['prediction_x']+resultat['prediction_y'] del resultat['prediction_x'] del resultat['prediction_y'] x,y = preprocess(data.copy(), 'Téléphonie') # rajoute les features #model.score(xTrain, yTrain) (xTest, souvenir)=preprocessFINAL(x,'Téléphonie') pred=telephoniePred(x,y,xTest) pred[pred>max(y)*1.05]=max(y)*1.05 pred[pred<0]=0 pred =np.round(pred) souvenir['prediction']=int(pred) resultat=pd.merge(resultat, souvenir, how='left',on=['DATE', 'ASS_ASSIGNMENT']) resultat=resultat.fillna(0) resultat['prediction'] = resultat['prediction_x']+resultat['prediction_y'] del resultat['prediction_x'] del resultat['prediction_y'] <<<<<<< HEAD pd.DataFrame(res).to_csv("reslist.csv", sep=";", decimal=",") resultat.to_csv("vraipred.txt", sep="\t", index =False) ======= >>>>>>> origin/master resultat['prediction']=resultat['prediction'].astype(int) resultat.to_csv("pouranalyse.txt", sep="\t", index =False, encoding='utf-8') >>>>>>> origin/master return resultat def faireListModel(): return [('CAT', linear_model.LinearRegression()), ('CMS', RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=5, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Crises',linear_model.LinearRegression()), ('Domicile', RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=30, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=90, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=30, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion - Accueil Telephonique',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=20, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=70, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion Assurances',RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=20, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=20, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion Clients', RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=10, max_features=90, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=50, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion DZ', RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=5, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion Relation Clienteles',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=10, max_features=90, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=110, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Gestion Renault', RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=30, max_features=50, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Japon',RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=10, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Manager',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=10, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Mécanicien',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=20, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Médical',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=30, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Nuit', RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=20, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Prestataires',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=20, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('RENAULT',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=80, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('RTC',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=20, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Regulation Medicale',linear_model.LinearRegression()), ('SAP',RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=20, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Services',RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=30, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Tech. Axa',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=20, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Tech. Inter',RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=30, max_features=30, max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=30, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Tech. Total',RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=70, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False)), ('Téléphonie',GradientBoostingRegressor(loss='huber', alpha=0.9,n_estimators=100, max_depth=3,learning_rate=.1, min_samples_leaf=9,min_samples_split=9) )]
mit
phobson/wqio
wqio/tests/test_datacollections.py
2
28761
from distutils.version import LooseVersion from textwrap import dedent from io import StringIO import numpy import scipy from scipy import stats import pandas from unittest import mock import pytest import pandas.testing as pdtest from wqio.tests import helpers from wqio.features import Location, Dataset from wqio.datacollections import DataCollection, _dist_compare OLD_SCIPY = LooseVersion(scipy.version.version) < LooseVersion("0.19") def check_stat(expected_csv, result, comp=False): index_col = [0] if comp: index_col += [1] file_obj = StringIO(dedent(expected_csv)) expected = pandas.read_csv(file_obj, header=[0, 1], index_col=index_col) if comp: expected = expected.stack(level=-1) pdtest.assert_frame_equal( expected.sort_index(axis="columns"), result.sort_index(axis="columns").round(6), atol=1e-5, ) def remove_g_and_h(group): return group.name[1] not in ["G", "H"] @pytest.fixture def dc(): df = helpers.make_dc_data_complex() dc = DataCollection( df, rescol="res", qualcol="qual", stationcol="loc", paramcol="param", ndval="<", othergroups=None, pairgroups=["state", "bmp"], useros=True, filterfxn=remove_g_and_h, bsiter=10000, ) return dc @pytest.fixture def dc_noNDs(): df = helpers.make_dc_data_complex() dc = DataCollection( df, rescol="res", qualcol="qual", stationcol="loc", paramcol="param", ndval="junk", othergroups=None, pairgroups=["state", "bmp"], useros=True, filterfxn=remove_g_and_h, bsiter=10000, ) return dc def test_basic_attr(dc): assert dc._raw_rescol == "res" assert isinstance(dc.data, pandas.DataFrame) assert dc.roscol == "ros_res" assert dc.rescol == "ros_res" assert dc.qualcol == "qual" assert dc.stationcol == "loc" assert dc.paramcol == "param" assert dc.ndval == ["<"] assert dc.bsiter == 10000 assert dc.groupcols == ["loc", "param"] assert dc.tidy_columns == ["loc", "param", "res", "__censorship"] assert hasattr(dc, "filterfxn") def test_data(dc): assert isinstance(dc.data, pandas.DataFrame) assert dc.data.shape == (519, 8) assert "G" in dc.data["param"].unique() assert "H" in dc.data["param"].unique() @pytest.mark.parametrize("useros", [True, False]) def test_tidy(dc, useros): assert isinstance(dc.tidy, pandas.DataFrame) assert dc.tidy.shape == (388, 5) assert "G" not in dc.tidy["param"].unique() assert "H" not in dc.tidy["param"].unique() collist = ["loc", "param", "res", "__censorship", "ros_res"] assert dc.tidy.columns.tolist() == collist def test_paired(dc): assert isinstance(dc.paired, pandas.DataFrame) assert dc.paired.shape == (164, 6) assert "G" not in dc.paired.index.get_level_values("param").unique() assert "H" not in dc.paired.index.get_level_values("param").unique() dc.paired.columns.tolist() == [ ("res", "Inflow"), ("res", "Outflow"), ("res", "Reference"), ("__censorship", "Inflow"), ("__censorship", "Outflow"), ("__censorship", "Reference"), ] def test_count(dc): known_csv = """\ station,Inflow,Outflow,Reference result,Count,Count,Count param,,, A,21,22,20 B,24,22,19 C,24,24,25 D,24,25,21 E,19,16,20 F,21,24,17 """ check_stat(known_csv, dc.count) def test_n_unique(dc): known_csv = """\ loc,Inflow,Outflow,Reference result,bmp,bmp,bmp param,,, A,7,7,7 B,7,7,7 C,7,7,7 D,7,7,7 E,7,7,7 F,7,7,7 G,7,7,7 H,7,7,7 """ check_stat(known_csv, dc.n_unique("bmp")) @helpers.seed def test_median(dc): known_csv = """\ station,Inflow,Inflow,Inflow,Outflow,Outflow,Outflow,Reference,Reference,Reference result,lower,median,upper,lower,median,upper,lower,median,upper param,,,,,,,,, A,0.334506,1.197251,2.013994,0.860493,2.231058,2.626023,1.073386,1.639472,1.717293 B,1.366948,2.773989,3.297147,0.23201,1.546499,2.579206,0.204164,1.565076,2.196367 C,0.17351,0.525957,0.68024,0.247769,0.396984,0.540742,0.136462,0.412693,0.559458 D,0.374122,1.201892,2.098846,0.516989,1.362759,1.827087,0.314655,0.882695,1.24545 E,0.276095,1.070858,1.152887,0.287914,0.516746,1.456859,0.366824,0.80716,2.040739 F,0.05667,0.832488,1.310575,0.425237,1.510942,2.193997,0.162327,0.745993,1.992513 """ check_stat(known_csv, dc.median) @helpers.seed def test_mean(dc): known_csv = """\ station,Inflow,Inflow,Inflow,Outflow,Outflow,Outflow,Reference,Reference,Reference result,lower,mean,upper,lower,mean,upper,lower,mean,upper param,,,,,,,,, A,1.231607,2.646682,4.204054,1.930601,5.249281,9.081952,1.540167,3.777974,6.389439 B,2.99031,7.647175,12.810844,1.545539,6.863835,12.705913,1.010374,4.504255,9.592572 C,0.37496,0.513248,0.65948,0.411501,1.004637,1.706317,0.35779,0.541962,0.734751 D,1.29141,3.021235,4.987855,1.285899,2.318808,3.451824,1.008364,1.945828,2.924812 E,0.818641,1.914696,3.049554,0.584826,1.098241,1.640807,1.113589,2.283292,3.581946 F,0.8379,9.825404,25.289933,1.497825,3.450184,5.61929,0.939917,2.491708,4.094258 """ check_stat(known_csv, dc.mean) @helpers.seed def test_std_dev(dc): known_csv = """\ station,Inflow,Outflow,Reference result,std. dev.,std. dev.,std. dev. param,,, A,3.58649,8.719371,5.527633 B,12.360099,13.60243,10.759285 C,0.353755,1.691208,0.493325 D,4.811938,2.849393,2.248178 E,2.55038,1.096698,2.789238 F,34.447565,5.361033,3.398367 """ check_stat(known_csv, dc.std_dev) @helpers.seed def test_percentile_25(dc): known_csv = """\ station,Inflow,Outflow,Reference result,pctl 25,pctl 25,pctl 25 param,,, A,0.522601,0.906029,1.094721 B,1.472541,0.251126,0.314226 C,0.164015,0.267521,0.136462 D,0.35688,0.516989,0.383895 E,0.364748,0.311508,0.394658 F,0.120068,0.406132,0.224429 """ check_stat(known_csv, dc.percentile(25)) @helpers.seed def test_percentile_75(dc): known_csv = """\ station,Inflow,Outflow,Reference result,pctl 75,pctl 75,pctl 75 param,,, A,2.563541,3.838021,2.650648 B,4.728871,2.849948,2.261847 C,0.776388,0.853535,0.792612 D,3.04268,2.79341,3.611793 E,1.532775,1.59183,3.201534 F,1.792985,2.80979,2.742249 """ check_stat(known_csv, dc.percentile(75)) @helpers.seed def test_logmean(dc): known_csv = """\ station,Inflow,Inflow,Inflow,Outflow,Outflow,Outflow,Reference,Reference,Reference result,Log-mean,lower,upper,Log-mean,lower,upper,Log-mean,lower,upper param,,,,,,,,, A,0.140559,-0.55112,0.644202,0.733004,0.047053,1.22099,0.545205,-0.057683,1.029948 B,1.026473,0.368659,1.541241,0.105106,-0.939789,0.860244,0.068638,-0.932357,0.661203 C,-0.963004,-1.304115,-0.638446,-0.83221,-1.464092,-0.414379,-1.088377,-1.556795,-0.720706 D,0.062317,-0.663241,0.58349,0.185757,-0.325074,0.598432,-0.063507,-0.670456,0.434214 E,-0.103655,-0.751075,0.385909,-0.456202,-1.08692,0.029967,-0.068135,-0.787007,0.51226 F,-0.442721,-1.874677,0.344704,0.211658,-0.504166,0.734283,-0.253352,-1.175917,0.467231 """ check_stat(known_csv, dc.logmean) @helpers.seed def test_logstd_dev(dc): known_csv = """\ station,Inflow,Outflow,Reference result,Log-std. dev.,Log-std. dev.,Log-std. dev. param,,, A,1.374026,1.343662,1.225352 B,1.430381,2.07646,1.662001 C,0.818504,1.263631,1.057177 D,1.530871,1.187246,1.277927 E,1.264403,1.121038,1.474431 F,2.324063,1.516331,1.701596 """ check_stat(known_csv, dc.logstd_dev) @helpers.seed def test_geomean(dc): known_csv = """\ station,Inflow,Inflow,Inflow,Outflow,Outflow,Outflow,Reference,Reference,Reference Geo-mean,Log-mean,lower,upper,Log-mean,lower,upper,Log-mean,lower,upper param,,,,,,,,, A,1.150917,0.576304,1.904467,2.081323,1.048178,3.390543,1.724962,0.943949,2.800919 B,2.791205,1.445795,4.670381,1.110829,0.39071,2.363737,1.071049,0.393625,1.937121 C,0.381744,0.271413,0.528113,0.435087,0.231288,0.66075,0.336763,0.210811,0.486409 D,1.064299,0.515179,1.792283,1.204129,0.722474,1.819264,0.938467,0.511475,1.543749 E,0.901536,0.471859,1.470951,0.633686,0.337254,1.03042,0.934134,0.455205,1.66906 F,0.642286,0.153405,1.411572,1.235726,0.604009,2.083988,0.776195,0.308536,1.595571 """ check_stat(known_csv, dc.geomean) @helpers.seed def test_geostd_dev(dc): known_csv = """\ station,Inflow,Outflow,Reference Geo-std. dev.,Log-std. dev.,Log-std. dev.,Log-std. dev. param,,, A,3.951225,3.833055,3.405365 B,4.180294,7.976181,5.269843 C,2.267105,3.538244,2.878234 D,4.622199,3.278041,3.589191 E,3.540977,3.068036,4.368548 F,10.217099,4.55548,5.48269 """ check_stat(known_csv, dc.geostd_dev) @helpers.seed def test_shapiro(dc): known_csv = """\ station,Inflow,Inflow,Outflow,Outflow,Reference,Reference result,pvalue,statistic,pvalue,statistic,pvalue,statistic param,,,,,, A,1.8e-05,0.685783,1e-06,0.576069,4e-06,0.61735 B,1e-06,0.594411,0.0,0.530962,0.0,0.41471 C,0.028774,0.905906,0.0,0.546626,0.00279,0.860373 D,1e-06,0.622915,1.5e-05,0.722374,0.000202,0.76518 E,1.7e-05,0.654137,0.004896,0.818813,0.000165,0.74917 F,0.0,0.292916,2e-06,0.634671,0.000167,0.713968 """ check_stat(known_csv, dc.shapiro) @helpers.seed def test_shapiro_log(dc): known_csv = """\ station,Inflow,Inflow,Outflow,Outflow,Reference,Reference result,statistic,pvalue,statistic,pvalue,statistic,pvalue param,,,,,, A,0.983521938,0.96662426,0.979861856,0.913820148,0.939460814,0.234214202 B,0.957531095,0.390856266,0.97048676,0.722278714,0.967978418,0.735424638 C,0.906479359,0.029602444,0.974698305,0.78197974,0.967106879,0.572929323 D,0.989704251,0.995502174,0.990663111,0.997093379,0.964812279,0.617747009 E,0.955088913,0.479993254,0.95211035,0.523841977,0.963425279,0.61430341 F,0.97542423,0.847370088,0.982230783,0.933124721,0.966197193,0.749036908 """ check_stat(known_csv, dc.shapiro_log) @helpers.seed def test_lilliefors(dc): known_csv = """\ station,Inflow,Inflow,Outflow,Outflow,Reference,Reference result,lilliefors,pvalue,lilliefors,pvalue,lilliefors,pvalue param,,,,,, A,0.308131,1.4e-05,0.340594,0.0,0.364453,0.0 B,0.36764,0.0,0.420343,0.0,0.417165,0.0 C,0.166799,0.082737,0.324733,0.0,0.161753,0.090455 D,0.273012,6.7e-05,0.240311,0.000665,0.296919,3.7e-05 E,0.341398,3e-06,0.239314,0.014862,0.233773,0.005474 F,0.419545,0.0,0.331315,0.0,0.284249,0.000741 """ check_stat(known_csv, dc.lilliefors) @helpers.seed def test_lilliefors_log(dc): known_csv = """\ station,Inflow,Inflow,Outflow,Outflow,Reference,Reference result,log-lilliefors,pvalue,log-lilliefors,pvalue,log-lilliefors,pvalue param,,,,,, A,0.08548109,0.95458004,0.15443943,0.19715747,0.20141389,0.03268737 B,0.16162839,0.10505016,0.12447902,0.49697902,0.15934334,0.22969362 C,0.16957278,0.07248915,0.12388174,0.44379732,0.11746642,0.48915671 D,0.06885549,0.99,0.06067356,0.99,0.13401954,0.41967483 E,0.13506577,0.47186822,0.14552341,0.47797919,0.09164876,0.92860794 F,0.14420794,0.30694533,0.08463267,0.92741885,0.08586933,0.9800294 """ check_stat(known_csv, dc.lilliefors_log) @helpers.seed def test_anderson_darling(dc): with helpers.raises(NotImplementedError): _ = dc.anderson_darling @helpers.seed def test_anderson_darling_log(dc): with helpers.raises(NotImplementedError): _ = dc.anderson_darling_log @helpers.seed def test_mann_whitney(dc): known_csv = """\ ,,mann_whitney,mann_whitney,mann_whitney,pvalue,pvalue,pvalue loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,180.0,179.0,,0.2198330905,0.4263216587 A,Outflow,282.0,,248.0,0.2198330905,,0.488580368 A,Reference,241.0,192.0,,0.4263216587,0.488580368, B,Inflow,,345.0,317.0,,0.0766949991,0.0304383994 B,Outflow,183.0,,216.0,0.0766949991,,0.8650586835 B,Reference,139.0,202.0,,0.0304383994,0.8650586835, C,Inflow,,282.0,323.0,,0.9097070273,0.6527104406 C,Outflow,294.0,,323.0,0.9097070273,,0.6527104406 C,Reference,277.0,277.0,,0.6527104406,0.6527104406, D,Inflow,,285.0,263.0,,0.7718162376,0.8111960975 D,Outflow,315.0,,293.0,0.7718162376,,0.5082395211 D,Reference,241.0,232.0,,0.8111960975,0.5082395211, E,Inflow,,164.0,188.0,,0.7033493939,0.9663820218 E,Outflow,140.0,,132.0,0.7033493939,,0.3813114322 E,Reference,192.0,188.0,,0.9663820218,0.3813114322, F,Inflow,,201.0,172.0,,0.2505911218,0.8601783903 F,Outflow,303.0,,236.0,0.2505911218,,0.4045186043 F,Reference,185.0,172.0,,0.8601783903,0.4045186043 """ check_stat(known_csv, dc.mann_whitney, comp=True) @helpers.seed def test_t_test(dc): known_csv = """\ ,,pvalue,pvalue,pvalue,t_test,t_test,t_test loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,0.2178424157,0.4563196599,,-1.2604458127,-0.7539785777 A,Outflow,0.2178424157,,0.5240147979,1.2604458127,,0.643450194 A,Reference,0.4563196599,0.5240147979,,0.7539785777,-0.643450194, B,Inflow,,0.8430007638,0.3898358794,,0.1992705833,0.869235357 B,Outflow,0.8430007638,,0.5491097882,-0.1992705833,,0.6043850808 B,Reference,0.3898358794,0.5491097882,,-0.869235357,-0.6043850808, C,Inflow,,0.1847386316,0.8191392537,,-1.3639360123,-0.2300373632 C,Outflow,0.1847386316,,0.2179907667,1.3639360123,,1.2615982727 C,Reference,0.8191392537,0.2179907667,,0.2300373632,-1.2615982727, D,Inflow,,0.5484265023,0.344783812,,0.6056706932,0.9582600001 D,Outflow,0.5484265023,,0.6299742693,-0.6056706932,,0.4851636024 D,Reference,0.344783812,0.6299742693,,-0.9582600001,-0.4851636024, E,Inflow,,0.2304569921,0.6770414622,,1.2287029977,-0.4198288251 E,Outflow,0.2304569921,,0.1023435465,-1.2287029977,,-1.6935358498 E,Reference,0.6770414622,0.1023435465,,0.4198288251,1.6935358498, F,Inflow,,0.422008391,0.3549979666,,0.8190789273,0.9463539528 F,Outflow,0.422008391,,0.4988994144,-0.8190789273,,0.6826435968 F,Reference,0.3549979666,0.4988994144,,-0.9463539528,-0.6826435968 """ check_stat(known_csv, dc.t_test, comp=True) @helpers.seed def test_levene(dc): known_csv = """\ ,,levene,levene,levene,pvalue,pvalue,pvalue loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,1.176282059,0.293152155,,0.284450688,0.591287419 A,Outflow,1.176282059,,0.397705309,0.284450688,,0.531863542 A,Reference,0.293152155,0.397705309,,0.591287419,0.531863542, B,Inflow,,0.003559637,0.402002411,,0.952694449,0.529578712 B,Outflow,0.003559637,,0.408938588,0.952694449,,0.526247443 B,Reference,0.402002411,0.408938588,,0.529578712,0.526247443, C,Inflow,,1.965613561,0.679535532,,0.167626459,0.413910674 C,Outflow,1.965613561,,1.462364363,0.167626459,,0.232602352 C,Reference,0.679535532,1.462364363,,0.413910674,0.232602352, D,Inflow,,0.643364813,0.983777911,,0.426532092,0.32681669 D,Outflow,0.643364813,,0.116830634,0.426532092,,0.734124856 D,Reference,0.983777911,0.116830634,,0.32681669,0.734124856, E,Inflow,,0.961616536,0.410491665,,0.333914902,0.525668596 E,Outflow,0.961616536,,2.726351564,0.333914902,,0.107912818 E,Reference,0.410491665,2.726351564,,0.525668596,0.107912818, F,Inflow,,0.841984453,0.734809611,,0.363948105,0.396999375 F,Outflow,0.841984453,,0.25881357,0.363948105,,0.613802541 F,Reference,0.734809611,0.25881357,,0.396999375,0.613802541, """ check_stat(known_csv, dc.levene, comp=True) @helpers.seed def test_wilcoxon(dc): known_csv = """\ ,,wilcoxon,wilcoxon,wilcoxon,pvalue,pvalue,pvalue loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,32.0,59.0,,0.03479,0.430679 A,Outflow,32.0,,46.0,0.03479,,0.274445 A,Reference,59.0,46.0,,0.430679,0.274445, B,Inflow,,38.0,22.0,,0.600179,0.182338 B,Outflow,38.0,,31.0,0.600179,,0.858863 B,Reference,22.0,31.0,,0.182338,0.858863, C,Inflow,,75.0,120.0,,0.167807,0.601046 C,Outflow,75.0,,113.0,0.167807,,0.463381 C,Reference,120.0,113.0,,0.601046,0.463381, D,Inflow,,44.0,31.0,,0.593618,0.530285 D,Outflow,44.0,,45.0,0.593618,,0.972125 D,Reference,31.0,45.0,,0.530285,0.972125, E,Inflow,,21.0,19.0,,0.910156,0.386271 E,Outflow,21.0,,16.0,0.910156,,0.077148 E,Reference,19.0,16.0,,0.386271,0.077148, F,Inflow,,62.0,22.0,,0.492459,0.952765 F,Outflow,62.0,,28.0,0.492459,,0.656642 F,Reference,22.0,28.0,,0.952765,0.656642, """ with pytest.warns(UserWarning): check_stat(known_csv, dc.wilcoxon, comp=True) @helpers.seed def test_ranksums(dc): known_csv = """\ ,,pvalue,pvalue,pvalue,rank_sums,rank_sums,rank_sums loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,0.2153009,0.4187782,,-1.2391203,-0.8085428 A,Outflow,0.2153009,,0.4807102,1.2391203,,0.7051607 A,Reference,0.4187782,0.4807102,,0.8085428,-0.7051607, B,Inflow,,0.0748817,0.029513,,1.781188,2.1765661 B,Outflow,0.0748817,,0.8547898,-1.781188,,0.1830104 B,Reference,0.029513,0.8547898,,-2.1765661,-0.1830104, C,Inflow,,0.9015386,0.6455162,,-0.1237179,0.46 C,Outflow,0.9015386,,0.6455162,0.1237179,,0.46 C,Reference,0.6455162,0.6455162,,-0.46,-0.46, D,Inflow,,0.7641772,0.8023873,,-0.3,0.2502587 D,Outflow,0.7641772,,0.5011969,0.3,,0.6726078 D,Reference,0.8023873,0.5011969,,-0.2502587,-0.6726078, E,Inflow,,0.6911022,0.9551863,,0.3973597,-0.0561951 E,Outflow,0.6911022,,0.3727144,-0.3973597,,-0.8914004 E,Reference,0.9551863,0.3727144,,0.0561951,0.8914004, F,Inflow,,0.2459307,0.8486619,,-1.1602902,-0.190826 F,Outflow,0.2459307,,0.3971011,1.1602902,,0.8468098 F,Reference,0.8486619,0.3971011,,0.190826,-0.8468098, """ check_stat(known_csv, dc.ranksums, comp=True) @helpers.seed @pytest.mark.xfail(OLD_SCIPY, reason="Scipy < 0.19") def test_kendall(dc): known_csv = """\ ,,kendalltau,kendalltau,kendalltau,pvalue,pvalue,pvalue loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,-0.051661,-0.00738,,0.772893,0.967114 A,Outflow,-0.051661,,-0.083333,0.772893,,0.690095 A,Reference,-0.00738,-0.083333,,0.967114,0.690095, B,Inflow,,0.441351,0.298246,,0.015267,0.119265 B,Outflow,0.441351,,0.559855,0.015267,,0.004202 B,Reference,0.298246,0.559855,,0.119265,0.004202, C,Inflow,,0.280223,0.084006,,0.078682,0.578003 C,Outflow,0.280223,,-0.1417,0.078682,,0.352394 C,Reference,0.084006,-0.1417,,0.578003,0.352394, D,Inflow,,0.403469,0.095299,,0.020143,0.634826 D,Outflow,0.403469,,0.318337,0.020143,,0.094723 D,Reference,0.095299,0.318337,,0.634826,0.094723, E,Inflow,,0.114286,0.640703,,0.673337,0.004476 E,Outflow,0.114286,,0.167944,0.673337,,0.449603 E,Reference,0.640703,0.167944,,0.004476,0.449603, F,Inflow,,0.0,0.07231,,1.0,0.763851 F,Outflow,0.0,,0.388889,1.0,,0.063 F,Reference,0.07231,0.388889,,0.763851,0.063, """ check_stat(known_csv, dc.kendall, comp=True) @helpers.seed def test_spearman(dc): known_csv = """\ ,,pvalue,pvalue,pvalue,spearmanrho,spearmanrho,spearmanrho loc_2,,Inflow,Outflow,Reference,Inflow,Outflow,Reference param,loc_1,,,,,, A,Inflow,,0.7574884491,0.9627447553,,-0.0809319588,0.012262418 A,Outflow,0.7574884491,,0.7617330788,-0.0809319588,,-0.0823529412 A,Reference,0.9627447553,0.7617330788,,0.012262418,-0.0823529412, B,Inflow,,0.0110829791,0.0775159774,,0.5831305575,0.4537313433 B,Outflow,0.0110829791,,0.0024069317,0.5831305575,,0.6850916941 B,Reference,0.0775159774,0.0024069317,,0.4537313433,0.6850916941, C,Inflow,,0.1330504059,0.6063501968,,0.3387640122,0.1134228342 C,Outflow,0.1330504059,,0.3431640379,0.3387640122,,-0.2070506455 C,Reference,0.6063501968,0.3431640379,,0.1134228342,-0.2070506455, D,Inflow,,0.0195715066,0.4751861062,,0.4935814032,0.1858231711 D,Outflow,0.0195715066,,0.1263974782,0.4935814032,,0.363209462 D,Reference,0.4751861062,0.1263974782,,0.1858231711,0.363209462, E,Inflow,,0.9828818202,0.0013596162,,0.0084033613,0.8112988341 E,Outflow,0.9828818202,,0.3413722947,0.0084033613,,0.3012263814 E,Reference,0.0013596162,0.3413722947,,0.8112988341,0.3012263814, F,Inflow,,0.9645303744,0.6759971848,,-0.0106277141,0.1348767061 F,Outflow,0.9645303744,,0.0560590794,-0.0106277141,,0.5028571429 F,Reference,0.6759971848,0.0560590794,,0.1348767061,0.5028571429 """ check_stat(known_csv, dc.spearman, comp=True) @helpers.seed def test_theilslopes(dc): with helpers.raises(NotImplementedError): _ = dc.theilslopes def test_inventory(dc): known_csv = StringIO( dedent( """\ loc,param,Count,Non-Detect Inflow,A,21,3 Inflow,B,24,6 Inflow,C,24,0 Inflow,D,24,11 Inflow,E,19,4 Inflow,F,21,8 Outflow,A,22,1 Outflow,B,22,9 Outflow,C,24,4 Outflow,D,25,12 Outflow,E,16,2 Outflow,F,24,8 Reference,A,20,2 Reference,B,19,6 Reference,C,25,4 Reference,D,21,12 Reference,E,20,3 Reference,F,17,7 """ ) ) expected = pandas.read_csv(known_csv, index_col=[0, 1]).astype(int) pdtest.assert_frame_equal(expected, dc.inventory.astype(int), check_names=False) def test_inventory_noNDs(dc_noNDs): known_csv = StringIO( dedent( """\ loc,param,Count,Non-Detect Inflow,A,21,0 Inflow,B,24,0 Inflow,C,24,0 Inflow,D,24,0 Inflow,E,19,0 Inflow,F,21,0 Outflow,A,22,0 Outflow,B,22,0 Outflow,C,24,0 Outflow,D,25,0 Outflow,E,16,0 Outflow,F,24,0 Reference,A,20,0 Reference,B,19,0 Reference,C,25,0 Reference,D,21,0 Reference,E,20,0 Reference,F,17,0 """ ) ) expected = pandas.read_csv(known_csv, index_col=[0, 1]).astype(int) pdtest.assert_frame_equal( expected, dc_noNDs.inventory.astype(int), check_names=False, ) @helpers.seed def test_stat_summary(dc): known_csv = StringIO( dedent( """\ ros_res,loc,A,B,C,D,E,F Count,Inflow,21,24,24,24,19,21 Count,Outflow,22,22,24,25,16,24 Count,Reference,20,19,25,21,20,17 Non-Detect,Inflow,3.0,6.0,0.0,11.0,4.0,8.0 Non-Detect,Outflow,1.0,9.0,4.0,12.0,2.0,8.0 Non-Detect,Reference,2.0,6.0,4.0,12.0,3.0,7.0 mean,Inflow,2.64668,7.64717,0.51325,3.02124,1.9147,9.8254 mean,Outflow,5.24928,6.86384,1.00464,2.31881,1.09824,3.45018 mean,Reference,3.77797,4.50425,0.54196,1.94583,2.28329,2.49171 std,Inflow,3.67506,12.62594,0.36136,4.91543,2.62027,35.29825 std,Outflow,8.92456,13.92253,1.72758,2.90815,1.13267,5.47634 std,Reference,5.67123,11.05411,0.5035,2.3037,2.8617,3.50296 min,Inflow,0.0756,0.17404,0.10213,0.05365,0.08312,0.00803 min,Outflow,0.11177,0.02106,0.03578,0.11678,0.07425,0.06377 min,Reference,0.15575,0.04909,0.04046,0.08437,0.05237,0.03445 10%,Inflow,0.1772,0.45233,0.13467,0.15495,0.1763,0.03548 10%,Outflow,0.44852,0.08297,0.08222,0.26949,0.19903,0.18008 10%,Reference,0.38448,0.13467,0.08241,0.19355,0.12777,0.09457 25%,Inflow,0.5226,1.47254,0.16401,0.35688,0.36475,0.12007 25%,Outflow,0.90603,0.25113,0.26752,0.51699,0.31151,0.40613 25%,Reference,1.09472,0.31423,0.13646,0.3839,0.39466,0.22443 50%,Inflow,1.19725,2.77399,0.52596,1.20189,1.07086,0.83249 50%,Outflow,2.23106,1.5465,0.39698,1.36276,0.51675,1.51094 50%,Reference,1.63947,1.56508,0.41269,0.8827,0.80716,0.74599 75%,Inflow,2.56354,4.72887,0.77639,3.04268,1.53278,1.79299 75%,Outflow,3.83802,2.84995,0.85354,2.79341,1.59183,2.80979 75%,Reference,2.65065,2.26185,0.79261,3.61179,3.20153,2.74225 90%,Inflow,6.02835,24.40655,0.99293,8.00691,6.28345,8.51706 90%,Outflow,12.43052,23.90022,2.43829,5.66731,2.30348,10.32829 90%,Reference,12.58278,6.67125,1.2205,4.78255,7.72012,8.57303 max,Inflow,13.87664,45.97893,1.26657,21.75505,8.88365,163.01001 max,Outflow,36.58941,47.49381,8.04948,12.39894,4.19118,23.29367 max,Reference,21.22363,48.23615,1.94442,7.67751,8.75609,10.5095 """ ) ) expected = pandas.read_csv(known_csv, index_col=[0, 1]).T pdtest.assert_frame_equal( expected.round(5), dc.stat_summary().round(5), check_names=False, check_dtype=False, rtol=1e-4, ) def test_locations(dc): for loc in dc.locations: assert isinstance(loc, Location) assert len(dc.locations) == 18 assert dc.locations[0].definition == {"loc": "Inflow", "param": "A"} assert dc.locations[1].definition == {"loc": "Inflow", "param": "B"} def test_datasets(dc): _ds = [] for d in dc.datasets("Inflow", "Outflow"): assert isinstance(d, Dataset) _ds.append(d) assert len(_ds) == 6 assert _ds[0].definition == {"param": "A"} assert _ds[1].definition == {"param": "B"} # this sufficiently tests dc._filter_collection def test_selectLocations(dc): locs = dc.selectLocations(param="A", loc=["Inflow", "Outflow"]) assert len(locs) == 2 for n, (loc, loctype) in enumerate(zip(locs, ["Inflow", "Outflow"])): assert isinstance(loc, Location) assert loc.definition["param"] == "A" assert loc.definition["loc"] == loctype def test_selectLocations_squeeze_False(dc): locs = dc.selectLocations(param="A", loc=["Inflow"], squeeze=False) assert len(locs) == 1 for n, loc in enumerate(locs): assert isinstance(loc, Location) assert loc.definition["param"] == "A" assert loc.definition["loc"] == "Inflow" def test_selectLocations_squeeze_True(dc): loc = dc.selectLocations(param="A", loc=["Inflow"], squeeze=True) assert isinstance(loc, Location) assert loc.definition["param"] == "A" assert loc.definition["loc"] == "Inflow" def test_selectLocations_squeeze_True_None(dc): loc = dc.selectLocations(param="A", loc=["Junk"], squeeze=True) assert loc is None # since the test_selectLocations* tests stress _filter_collection # enough, we'll mock it out for datasets: def test_selectDatasets(dc): with mock.patch.object(dc, "_filter_collection") as _fc: with mock.patch.object(dc, "datasets", return_value=["A", "B"]) as _ds: dc.selectDatasets("Inflow", "Reference", foo="A", bar="C") _ds.assert_called_once_with("Inflow", "Reference") _fc.assert_called_once_with(["A", "B"], foo="A", bar="C", squeeze=False) @pytest.mark.parametrize("func", [stats.mannwhitneyu, stats.wilcoxon]) @pytest.mark.parametrize( ("x", "all_same"), [([5, 5, 5, 5, 5], True), ([5, 6, 7, 7, 8], False)] ) def test_dist_compare_wrapper(x, all_same, func): y = [5, 5, 5, 5, 5] with mock.patch.object(stats, func.__name__) as _test: result = _dist_compare(x, y, _test) if all_same: assert numpy.isnan(result.stat) assert numpy.isnan(result.pvalue) assert _test.call_count == 0 else: # assert result == (0, 0) _test.assert_called_once_with(x, y, alternative="two-sided")
bsd-3-clause
cgrima/rsr
rsr/fit.py
1
4401
""" Various tools for extracting signal components from a fit of the amplitude distribution """ from . import pdf from .Classdef import Statfit import numpy as np import time import random import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from lmfit import minimize, Parameters, report_fit def param0(sample, method='basic'): """Estimate initial parameters for HK fitting Arguments --------- sample : sequence amplitudes Keywords -------- method : string method to compute the initial parameters """ if method == 'basic': a = np.nanmean(sample) s = np.nanstd(sample) mu = 1. return {'a':a, 's':s, 'mu':mu} def lmfit(sample, fit_model='hk', bins='auto', p0 = None, xtol=1e-4, ftol=1e-4): """Lmfit Arguments --------- sample : sequence amplitudes between 0 and 1. Keywords -------- fit_model : string name of the function (in pdf module) to use for the fit bins : string method to compute the bin width (inherited from numpy.histogram) p0 : dict Initial parameters. If None, estimated automatically. xtol : float ?? ftol : float ?? Return ------ A Statfit Class """ start = time.time() winsize = len(sample) bad = False #-------------------------------------------------------------------------- # Clean sample #-------------------------------------------------------------------------- sample = np.array(sample) sample = sample[np.isfinite(sample)] if len(sample) == 0: bad = True sample = np.zeros(10)+1 #-------------------------------------------------------------------------- # Make the histogram #-------------------------------------------------------------------------- # n, edges, patches = hist(sample, bins=bins, normed=True) n, edges = np.histogram(sample, bins=bins, density=True) # plt.clf() x = ((np.roll(edges, -1) + edges)/2.)[0:-1] #-------------------------------------------------------------------------- # Initial Parameters for the fit #-------------------------------------------------------------------------- if p0 is None: p0 = param0(sample) prm0 = Parameters() # (Name, Value, Vary, Min, Max, Expr) prm0.add('a', p0['a'], True, 0, 1, None) prm0.add('s', p0['s'], True, 0, 1, None) prm0.add('mu', p0['mu'], True, .5, 10, None) prm0.add('pt', np.average(sample)**2,False, 0, 1, 'a**2+2*s**2*mu') #if fit_model == 'hk': # # From [Dutt and Greenleaf. 1994, eq.14] # prm0.add('a4', np.average(sample)**4,False, 0, 1, # '8*(1+1/mu)*s**4 + 8*s**2*s**2 + a**4') #-------------------------------------------------------------------------- # Fit #-------------------------------------------------------------------------- pdf2use = getattr(pdf, fit_model) # use 'lbfgs' fit if error with 'leastsq' fit try: p = minimize(pdf2use, prm0, args=(x, n), method='leastsq', xtol=xtol, ftol=ftol) except KeyboardInterrupt: raise except: print('!! Error with LEASTSQ fit, use L-BFGS-B instead') p = minimize(pdf2use, prm0, args=(x, n), method='lbfgs') #-------------------------------------------------------------------------- # Output #-------------------------------------------------------------------------- elapsed = time.time() - start values = {} # Create values dict For lmfit >0.9.0 compatibility since it is no longer # in the minimize output for i in p.params.keys(): values[i] = p.params[i].value # Results result = Statfit(sample, pdf2use, values, p.params, p.chisqr, p.redchi, elapsed, p.nfev, p.message, p.success, p.residual, x, n, edges, bins=bins) # Identify bad results if bad is True: result.success = False result.values['a'] = 0 result.values['s'] = 0 result.values['mu'] = 0 result.values['pt'] = 0 result.chisqr = 0 result.redchi = 0 result.message = 'No valid data in the sample' result.residual = 0 return result
mit
jseabold/scikit-learn
sklearn/manifold/tests/test_locally_linear.py
232
4761
from itertools import product from nose.tools import assert_true import numpy as np from numpy.testing import assert_almost_equal, assert_array_almost_equal from scipy import linalg from sklearn import neighbors, manifold from sklearn.manifold.locally_linear import barycenter_kneighbors_graph from sklearn.utils.testing import assert_less from sklearn.utils.testing import ignore_warnings eigen_solvers = ['dense', 'arpack'] #---------------------------------------------------------------------- # Test utility routines def test_barycenter_kneighbors_graph(): X = np.array([[0, 1], [1.01, 1.], [2, 0]]) A = barycenter_kneighbors_graph(X, 1) assert_array_almost_equal( A.toarray(), [[0., 1., 0.], [1., 0., 0.], [0., 1., 0.]]) A = barycenter_kneighbors_graph(X, 2) # check that columns sum to one assert_array_almost_equal(np.sum(A.toarray(), 1), np.ones(3)) pred = np.dot(A.toarray(), X) assert_less(linalg.norm(pred - X) / X.shape[0], 1) #---------------------------------------------------------------------- # Test LLE by computing the reconstruction error on some manifolds. def test_lle_simple_grid(): # note: ARPACK is numerically unstable, so this test will fail for # some random seeds. We choose 2 because the tests pass. rng = np.random.RandomState(2) tol = 0.1 # grid of equidistant points in 2D, n_components = n_dim X = np.array(list(product(range(5), repeat=2))) X = X + 1e-10 * rng.uniform(size=X.shape) n_components = 2 clf = manifold.LocallyLinearEmbedding(n_neighbors=5, n_components=n_components, random_state=rng) tol = 0.1 N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray() reconstruction_error = linalg.norm(np.dot(N, X) - X, 'fro') assert_less(reconstruction_error, tol) for solver in eigen_solvers: clf.set_params(eigen_solver=solver) clf.fit(X) assert_true(clf.embedding_.shape[1] == n_components) reconstruction_error = linalg.norm( np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2 assert_less(reconstruction_error, tol) assert_almost_equal(clf.reconstruction_error_, reconstruction_error, decimal=1) # re-embed a noisy version of X using the transform method noise = rng.randn(*X.shape) / 100 X_reembedded = clf.transform(X + noise) assert_less(linalg.norm(X_reembedded - clf.embedding_), tol) def test_lle_manifold(): rng = np.random.RandomState(0) # similar test on a slightly more complex manifold X = np.array(list(product(np.arange(18), repeat=2))) X = np.c_[X, X[:, 0] ** 2 / 18] X = X + 1e-10 * rng.uniform(size=X.shape) n_components = 2 for method in ["standard", "hessian", "modified", "ltsa"]: clf = manifold.LocallyLinearEmbedding(n_neighbors=6, n_components=n_components, method=method, random_state=0) tol = 1.5 if method == "standard" else 3 N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray() reconstruction_error = linalg.norm(np.dot(N, X) - X) assert_less(reconstruction_error, tol) for solver in eigen_solvers: clf.set_params(eigen_solver=solver) clf.fit(X) assert_true(clf.embedding_.shape[1] == n_components) reconstruction_error = linalg.norm( np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2 details = ("solver: %s, method: %s" % (solver, method)) assert_less(reconstruction_error, tol, msg=details) assert_less(np.abs(clf.reconstruction_error_ - reconstruction_error), tol * reconstruction_error, msg=details) def test_pipeline(): # check that LocallyLinearEmbedding works fine as a Pipeline # only checks that no error is raised. # TODO check that it actually does something useful from sklearn import pipeline, datasets X, y = datasets.make_blobs(random_state=0) clf = pipeline.Pipeline( [('filter', manifold.LocallyLinearEmbedding(random_state=0)), ('clf', neighbors.KNeighborsClassifier())]) clf.fit(X, y) assert_less(.9, clf.score(X, y)) # Test the error raised when the weight matrix is singular def test_singular_matrix(): from nose.tools import assert_raises M = np.ones((10, 3)) f = ignore_warnings assert_raises(ValueError, f(manifold.locally_linear_embedding), M, 2, 1, method='standard', eigen_solver='arpack')
bsd-3-clause
pdamodaran/yellowbrick
yellowbrick/text/dispersion.py
1
10916
# yellowbrick.text.dispersion # Implementations of lexical dispersions for text visualization. # # Author: Larry Gray # Created: 2018-06-21 10:06 # # Copyright (C) 2018 District Data Labs # For license information, see LICENSE.txt # # ID: dispersion.py [] lwgray@gmail.com $ """ Implementation of lexical dispersion for text visualization """ ########################################################################## ## Imports ########################################################################## from collections import defaultdict import itertools from yellowbrick.text.base import TextVisualizer from yellowbrick.style.colors import resolve_colors from yellowbrick.exceptions import YellowbrickValueError import numpy as np ########################################################################## ## Dispersion Plot Visualizer ########################################################################## class DispersionPlot(TextVisualizer): """ DispersionPlotVisualizer allows for visualization of the lexical dispersion of words in a corpus. Lexical dispersion is a measure of a word's homeogeneity across the parts of a corpus. This plot notes the occurences of a word and how many words from the beginning it appears. Parameters ---------- target_words : list A list of target words whose dispersion across a corpus passed at fit will be visualized. ax : matplotlib axes, default: None The axes to plot the figure on. labels : list of strings The names of the classes in the target, used to create a legend. Labels must match names of classes in sorted order. colors : list or tuple of colors Specify the colors for each individual class colormap : string or matplotlib cmap Qualitative colormap for discrete target ignore_case : boolean, default: False Specify whether input will be case-sensitive. annotate_docs : boolean, default: False Specify whether document boundaries will be displayed. Vertical lines are positioned at the end of each document. kwargs : dict Pass any additional keyword arguments to the super class. These parameters can be influenced later on in the visualization process, but can and should be set as early as possible. """ # NOTE: cannot be np.nan NULL_CLASS = None def __init__(self, target_words, ax=None, colors=None, ignore_case=False, annotate_docs=False, labels=None, colormap=None, **kwargs): super(DispersionPlot, self).__init__(ax=ax, **kwargs) self.labels = labels self.colors = colors self.colormap = colormap self.target_words = target_words self.ignore_case = ignore_case self.annotate_docs = annotate_docs def _compute_dispersion(self, text, y): self.boundaries_ = [] offset = 0 if y is None: y = itertools.repeat(None) for doc, target in zip(text, y): for word in doc: if self.ignore_case: word = word.lower() # NOTE: this will find all indices if duplicate words are supplied # In the case that word is not in target words, any empty list is # returned and no data will be yielded offset += 1 for y_coord in (self.indexed_words_ == word).nonzero()[0]: y_coord = int(y_coord) yield (offset, y_coord, target) if self.annotate_docs: self.boundaries_.append(offset) self.boundaries_ = np.array(self.boundaries_, dtype=int) def _check_missing_words(self, points): for index in range(len(self.indexed_words_)): if index in points[:,1]: pass else: raise YellowbrickValueError(( "The indexed word '{}' is not found in " "this corpus" ).format(self.indexed_words_[index])) def fit(self, X, y=None, **kwargs): """ The fit method is the primary drawing input for the dispersion visualization. Parameters ---------- X : list or generator Should be provided as a list of documents or a generator that yields a list of documents that contain a list of words in the order they appear in the document. y : ndarray or Series of length n An optional array or series of target or class values for instances. If this is specified, then the points will be colored according to their class. kwargs : dict Pass generic arguments to the drawing method Returns ------- self : instance Returns the instance of the transformer/visualizer """ if y is not None: self.classes_ = np.unique(y) elif y is None and self.labels is not None: self.classes_ = np.array([self.labels[0]]) else: self.classes_ = np.array([self.NULL_CLASS]) # Create an index (e.g. the y position) for the target words self.indexed_words_ = np.flip(self.target_words, axis=0) if self.ignore_case: self.indexed_words_ = np.array([w.lower() for w in self.indexed_words_]) # Stack is used to create a 2D array from the generator try: points_target = np.stack(self._compute_dispersion(X, y)) except ValueError: raise YellowbrickValueError(( "No indexed words were found in the corpus" )) points = np.stack(zip(points_target[:,0].astype(int), points_target[:,1].astype(int))) self.target = points_target[:,2] self._check_missing_words(points) self.draw(points, self.target) return self def draw(self, points, target=None, **kwargs): """ Called from the fit method, this method creates the canvas and draws the plot on it. Parameters ---------- kwargs: generic keyword arguments. """ # Resolve the labels with the classes labels = self.labels if self.labels is not None else self.classes_ if len(labels) != len(self.classes_): raise YellowbrickValueError(( "number of supplied labels ({}) does not " "match the number of classes ({})" ).format(len(labels), len(self.classes_))) # Create the color mapping for the labels. color_values = resolve_colors( n_colors=len(labels), colormap=self.colormap, colors=self.color) colors = dict(zip(labels, color_values)) # Transform labels into a map of class to label labels = dict(zip(self.classes_, labels)) # Define boundaries with a vertical line if self.annotate_docs: for xcoords in self.boundaries_: self.ax.axvline(x=xcoords, color='lightgray', linestyle='dashed') series = defaultdict(lambda: {'x':[], 'y':[]}) if target is not None: for point, t in zip(points, target): label = labels[t] series[label]['x'].append(point[0]) series[label]['y'].append(point[1]) else: label = self.classes_[0] for x, y in points: series[label]['x'].append(x) series[label]['y'].append(y) for label, points in series.items(): self.ax.scatter(points['x'], points['y'], marker='|', c=colors[label], zorder=100, label=label) self.ax.set_yticks(list(range(len(self.indexed_words_)))) self.ax.set_yticklabels(self.indexed_words_) def finalize(self, **kwargs): """ The finalize method executes any subclass-specific axes finalization steps. The user calls poof & poof calls finalize. Parameters ---------- kwargs: generic keyword arguments. """ self.ax.set_ylim(-1, len(self.indexed_words_)) self.ax.set_title("Lexical Dispersion Plot") self.ax.set_xlabel("Word Offset") self.ax.grid(False) # Add the legend outside of the figure box. if not all(self.classes_ == np.array([self.NULL_CLASS])): box = self.ax.get_position() self.ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) self.ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ########################################################################## ## Quick Method ########################################################################## def dispersion(words, corpus, y=None, ax=None, colors=None, colormap=None, labels=None, annotate_docs=False, ignore_case=False, **kwargs): """ Displays lexical dispersion plot for words in a corpus This helper function is a quick wrapper to utilize the DisperstionPlot Visualizer for one-off analysis Parameters ---------- words : list A list of words whose dispersion will be examined within a corpus y : ndarray or Series of length n An optional array or series of target or class values for instances. If this is specified, then the points will be colored according to their class. corpus : list Should be provided as a list of documents that contain a list of words in the order they appear in the document. ax : matplotlib axes, default: None The axes to plot the figure on. labels : list of strings The names of the classes in the target, used to create a legend. Labels must match names of classes in sorted order. colors : list or tuple of colors Specify the colors for each individual class colormap : string or matplotlib cmap Qualitative colormap for discrete target annotate_docs : boolean, default: False Specify whether document boundaries will be displayed. Vertical lines are positioned at the end of each document. ignore_case : boolean, default: False Specify whether input will be case-sensitive. kwargs : dict Pass any additional keyword arguments to the super class. Returns ------- ax: matplotlib axes Returns the axes that the plot was drawn on """ # Instantiate the visualizer visualizer = DispersionPlot( words, ax=ax, colors=colors, colormap=colormap, ignore_case=ignore_case, labels=labels, annotate_docs=annotate_docs, **kwargs ) # Fit and transform the visualizer (calls draw) visualizer.fit(corpus, y, **kwargs) # Return the axes object on the visualizer return visualizer.ax
apache-2.0
sevenian3/ChromaStarPy
LevelPopsGasServer.py
1
55996
# -*- coding: utf-8 -*- """ Created on Mon Apr 24 14:13:47 2017 @author: ishort """ import math import Useful import ToolBox #import numpy #JB# #from matplotlib.pyplot import plot, title, show, scatter #storage for fits (not all may be used) uw = [] uwa = [] uwb = [] uwStage = [] uwbStage = [] uwu = [] uwl = [] uua=[] uub=[] """ #a function to create a cubic function fit extrapolation def cubicFit(x,y): coeffs = numpy.polyfit(x,y,3) #returns an array of coefficents for the cubic fit of the form #Ax^3 + Bx^2 + Cx + D as [A,B,C,D] return coeffs #this will work for any number of data points! def valueFromFit(fit,x): #return the value y for a given fit, at point x return (fit[0]*(x**3)+fit[1]*(x**2)+fit[2]*x+fit[3]) #holds the five temperature at which we have partition function data """ masterTemp = [130, 500, 3000, 8000, 10000] #JB# #def levelPops(lam0In, logNStage, chiL, log10UwStage, gwL, numDeps, temp): def levelPops(lam0In, logNStage, chiL, logUw, gwL, numDeps, temp): """ Returns depth distribution of occupation numbers in lower level of b-b transition, // Input parameters: // lam0 - line centre wavelength in nm // logNStage - log_e density of absorbers in relevent ion stage (cm^-3) // logFlu - log_10 oscillator strength (unitless) // chiL - energy of lower atomic E-level of b-b transition in eV // Also needs atsmopheric structure information: // numDeps // temp structure """ c = Useful.c() logC = Useful.logC() k = Useful.k() logK = Useful.logK() logH = Useful.logH() logEe = Useful.logEe() logMe = Useful.logMe() ln10 = math.log(10.0) logE = math.log10(math.e); #// for debug output log2pi = math.log(2.0 * math.pi) log2 = math.log(2.0) #//double logNl = logNlIn * ln10; // Convert to base e #// Parition functions passed in are 2-element vectore with remperature-dependent base 10 log Us #// Convert to natural logs: #double thisLogUw, Ttheta; thisLogUw = 0.0 # //default initialization #logUw = [ 0.0 for i in range(5) ] logE10 = math.log(10.0) #print("log10UwStage ", log10UwStage) #for kk in range(len(logUw)): # logUw[kk] = logE10*log10UwStage[kk] #// lburns new loop logGwL = math.log(gwL) #//System.out.println("chiL before: " + chiL); #// If we need to subtract chiI from chiL, do so *before* converting to tiny numbers in ergs! #////For testing with Ca II lines using gS3 internal line list only: #//boolean ionized = true; #//if (ionized) { #// //System.out.println("ionized, doing chiL - chiI: " + ionized); #// // chiL = chiL - chiI; #// chiL = chiL - 6.113; #// } #// // #//Log of line-center wavelength in cm logLam0 = math.log(lam0In) #// * 1.0e-7); #// energy of b-b transition logTransE = logH + logC - logLam0 #//ergs if (chiL <= 0.0): chiL = 1.0e-49 logChiL = math.log(chiL) + Useful.logEv() #// Convert lower E-level from eV to ergs logBoltzFacL = logChiL - Useful.logK() #// Pre-factor for exponent of excitation Boltzmann factor boltzFacL = math.exp(logBoltzFacL) boltzFacGround = 0.0 / k #//I know - its zero, but let's do it this way anyway' #// return a 1D numDeps array of logarithmic number densities #// level population of lower level of bb transition (could be in either stage I or II!) logNums = [ 0.0 for i in range(numDeps)] #double num, logNum, expFac; #JB# #print("thisLogUw:",numpy.shape(logUw)) logUwFit = ToolBox.cubicFit(masterTemp,logUw)#u(T) fit uw.append(logUwFit) #JB# for id in range(numDeps): #//Determine temperature dependenet partition functions Uw: #Ttheta = 5040.0 / temp[0][id] #//NEW Determine temperature dependent partition functions Uw: lburns thisTemp = temp[0][id] """ if (Ttheta >= 1.0): thisLogUw = logUw[0] if (Ttheta <= 0.5): thisLogUw = logUw[1] if (Ttheta > 0.5 and Ttheta < 1.0): thisLogUw = ( logUw[1] * (Ttheta - 0.5)/(1.0 - 0.5) ) \ + ( logUw[0] * (1.0 - Ttheta)/(1.0 - 0.5) ) """ #JB# thisLogUw = ToolBox.valueFromFit(logUwFit,thisTemp)#u(T) value extrapolated #JB# if (thisTemp >= 10000.0): thisLogUw = logUw[4] if (thisTemp <= 130.0): thisLogUw = logUw[0] """ if (thisTemp > 130 and thisTemp <= 500): thisLogUw = logUw[1] * (thisTemp - 130)/(500 - 130) \ + logUw[0] * (500 - thisTemp)/(500 - 130) if (thisTemp > 500 and thisTemp <= 3000): thisLogUw = logUw[2] * (thisTemp - 500)/(3000 - 500) \ + logUw[1] * (3000 - thisTemp)/(3000 - 500) if (thisTemp > 3000 and thisTemp <= 8000): thisLogUw = logUw[3] * (thisTemp - 3000)/(8000 - 3000) \ + logUw[2] * (8000 - thisTemp)/(8000 - 3000) if (thisTemp > 8000 and thisTemp < 10000): thisLogUw = logUw[4] * (thisTemp - 8000)/(10000 - 8000) \ + logUw[3] * (10000 - thisTemp)/(10000 - 8000) """ #print("logUw ", logUw, " thisLogUw ", thisLogUw) #//System.out.println("LevPops: ionized branch taken, ionized = " + ionized); #// Take stat weight of ground state as partition function: logNums[id] = logNStage[id] - boltzFacL / temp[0][id] + logGwL - thisLogUw #// lower level of b-b transition #print("LevelPopsServer.stagePops id ", id, " logNStage[id] ", logNStage[id], " boltzFacL ", boltzFacL, " temp[0][id] ", temp[0][id], " logGwL ", logGwL, " thisLogUw ", thisLogUw, " logNums[id] ", logNums[id]); #// System.out.println("LevelPops: id, logNums[0][id], logNums[1][id], logNums[2][id], logNums[3][id]: " + id + " " #// + Math.exp(logNums[0][id]) + " " #// + Math.exp(logNums[1][id]) + " " #// + Math.exp(logNums[2][id]) + " " #// + Math.exp(logNums[3][id])); #//System.out.println("LevelPops: id, logNums[0][id], logNums[1][id], logNums[2][id], logNums[3][id], logNums[4][id]: " + id + " " #// + logE * (logNums[0][id]) + " " #// + logE * (logNums[1][id]) + " " #// + logE * (logNums[2][id]) + " " # // + logE * (logNums[3][id]) + " " #// + logE * (logNums[4][id]) ); #//System.out.println("LevelPops: id, logIonFracI, logIonFracII: " + id + " " + logE*logIonFracI + " " + logE*logIonFracII #// + "logNum, logNumI, logNums[0][id], logNums[1][id] " #// + logE*logNum + " " + logE*logNumI + " " + logE*logNums[0][id] + " " + logE*logNums[1][id]); #//System.out.println("LevelPops: id, logIonFracI: " + id + " " + logE*logIonFracI #// + "logNums[0][id], boltzFacL/temp[0][id], logNums[2][id]: " #// + logNums[0][id] + " " + boltzFacL/temp[0][id] + " " + logNums[2][id]); #//id loop #stop #print (uw) return logNums #//This version - ionization equilibrium *WITHOUT* molecules - logNum is TOTAL element population #def stagePops2(logNum, Ne, chiIArr, log10UwAArr, \ # numMols, logNumB, dissEArr, log10UwBArr, logQwABArr, logMuABArr, \ # numDeps, temp): def stagePops(logNum, Ne, chiIArr, logUw, \ numDeps, temp): #line 1: //species A data - ionization equilibrium of A #line 2: //data for set of species "B" - molecular equlibrium for set {AB} """Ionization equilibrium routine WITHOUT molecule formation: // Returns depth distribution of ionization stage populations // Input parameters: // logNum - array with depth-dependent total element number densities (cm^-3) // chiI1 - ground state ionization energy of neutral stage // chiI2 - ground state ionization energy of singly ionized stage // Also needs atsmopheric structure information: // numDeps // temp structure // rho structure // Atomic element A is the one whose ionization fractions are being computed // """ ln10 = math.log(10.0) logE = math.log10(math.e) #// for debug output log2pi = math.log(2.0 * math.pi) log2 = math.log(2.0) numStages = len(chiIArr) #// + 1; //need one more stage above the highest stage to be populated #// var numMols = dissEArr.length; #// Parition functions passed in are 2-element vectore with remperature-dependent base 10 log Us #// Convert to natural logs: #double Ttheta, thisTemp; #//Default initializations: #//We need one more stage in size of saha factor than number of stages we're actualy populating thisLogUw = [ 0.0 for i in range(numStages+1) ] for i in range(numStages+1): thisLogUw[i] = 0.0 logE10 = math.log(10.0) #//atomic ionization stage Boltzmann factors: #double logChiI, logBoltzFacI; boltzFacI = [ 0.0 for i in range(numStages) ] #print("numStages ", numStages, " Useful.logEv ", Useful.logEv()) for i in range(numStages): #print("i ", i, " chiIArr ", chiIArr[i]) logChiI = math.log(chiIArr[i]) + Useful.logEv() logBoltzFacI = logChiI - Useful.logK() boltzFacI[i] = math.exp(logBoltzFacI) logSahaFac = log2 + (3.0 / 2.0) * (log2pi + Useful.logMe() + Useful.logK() - 2.0 * Useful.logH()) #// return a 2D 5 x numDeps array of logarithmic number densities #// Row 0: neutral stage ground state population #// Row 1: singly ionized stage ground state population #// Row 2: doubly ionized stage ground state population #// Row 3: triply ionized stage ground state population #// Row 4: quadruply ionized stage ground state population #double[][] logNums = new double[numStages][numDeps]; logNums = [ [ 0.0 for i in range(numDeps)] for j in range(numStages) ] #//We need one more stage in size of saha factor than number of stages we're actualy populating #// for index accounting pirposes #// For atomic ionization stages: logSaha = [ [ 0.0 for i in range(numStages+1)] for j in range(numStages+1) ] saha = [ [ 0.0 for i in range(numStages+1)] for j in range(numStages+1) ] #// logIonFrac = [ 0.0 for i in range(numStages) ] #double expFac, logNe; #// Now - molecular variables: thisLogUwA = 0.0 #// element A #thisLogQwAB = math.log(300.0) #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: logUwA = [ 0.0 for i in range(5) ] #JB# uua=[] #uub=[] #qwab=[] for iStg in range(numStages): currentUwArr=list(logUw[iStg])#u(T) determined values UwFit = ToolBox.cubicFit(masterTemp,currentUwArr)#u(T) fit uua.append(UwFit) #print(logUw[iStg]) for id in range(numDeps): #//// reduce or enhance number density by over-all Rosseland opcity scale parameter #// #//Row 1 of Ne is log_e Ne in cm^-3 logNe = Ne[1][id] #//Determine temperature dependent partition functions Uw: thisTemp = temp[0][id] #Ttheta = 5040.0 / thisTemp #JB# #use temps and partition values to create a function #then use said function to extrapolate values for all points thisLogUw[numStages] = 0.0 for iStg in range(numStages): thisLogUw[iStg] = ToolBox.valueFromFit(uua[iStg],thisTemp)#u(T) value extrapolated #JB# #// NEW Determine temperature dependent partition functions Uw: lburns if (thisTemp <= 130.0): for iStg in range(numStages): thisLogUw[iStg] = logUw[iStg][0] #for iMol in range(numMols): # thisLogUwB[iMol] = logUwB[iMol][0] if (thisTemp >= 10000.0): for iStg in range(numStages): thisLogUw[iStg] = logUw[iStg][4] #for iMol in range(numMols): # thisLogUwB[iMol] = logUwB[iMol][4] #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: thisLogUwA = thisLogUw[0]; #//Ionization stage Saha factors: for iStg in range(numStages): #print("iStg ", iStg) logSaha[iStg+1][iStg] = logSahaFac - logNe - (boltzFacI[iStg] /temp[0][id]) + (3.0 * temp[1][id] / 2.0) + thisLogUw[iStg+1] - thisLogUw[iStg] saha[iStg+1][iStg] = math.exp(logSaha[iStg+1][iStg]) #//Compute log of denominator is ionization fraction, f_stage denominator = 1.0 #//default initialization - leading term is always unity #//ion stage contributions: for jStg in range(1, numStages+1): addend = 1.0 #//default initialization for product series for iStg in range(jStg): #//console.log("jStg " + jStg + " saha[][] indices " + (iStg+1) + " " + iStg); addend = addend * saha[iStg+1][iStg] denominator = denominator + addend #// logDenominator = math.log(denominator) logIonFrac[0] = -1.0 * logDenominator #// log ionization fraction in stage I for jStg in range(1, numStages): addend = 0.0 #//default initialization for product series for iStg in range(jStg): #//console.log("jStg " + jStg + " saha[][] indices " + (iStg+1) + " " + iStg); addend = addend + logSaha[iStg+1][iStg] logIonFrac[jStg] = addend - logDenominator for iStg in range(numStages): logNums[iStg][id] = logNum[id] + logIonFrac[iStg] #//id loop return logNums; #//end method stagePops #end method levelPops #def stagePops2(logNum, Ne, chiIArr, log10UwAArr, \ # numMols, logNumB, dissEArr, log10UwBArr, logQwABArr, logMuABArr, \ # numDeps, temp): def stagePops2(logNum, Ne, chiIArr, logUw, \ numMols, logNumB, dissEArr, logUwB, logQwABArr, logMuABArr, \ numDeps, temp): #line 1: //species A data - ionization equilibrium of A #line 2: //data for set of species "B" - molecular equlibrium for set {AB} """Ionization equilibrium routine that accounts for molecule formation: // Returns depth distribution of ionization stage populations // Input parameters: // logNum - array with depth-dependent total element number densities (cm^-3) // chiI1 - ground state ionization energy of neutral stage // chiI2 - ground state ionization energy of singly ionized stage // Also needs atsmopheric structure information: // numDeps // temp structure // rho structure // Atomic element A is the one whose ionization fractions are being computed // Element B refers to array of other species with which A forms molecules AB """ ln10 = math.log(10.0) logE = math.log10(math.e) #// for debug output log2pi = math.log(2.0 * math.pi) log2 = math.log(2.0) numStages = len(chiIArr) #// + 1; //need one more stage above the highest stage to be populated #// var numMols = dissEArr.length; #// Parition functions passed in are 2-element vectore with remperature-dependent base 10 log Us #// Convert to natural logs: #double Ttheta, thisTemp; #//Default initializations: #//We need one more stage in size of saha factor than number of stages we're actualy populating thisLogUw = [ 0.0 for i in range(numStages+1) ] for i in range(numStages+1): thisLogUw[i] = 0.0 logE10 = math.log(10.0) #//atomic ionization stage Boltzmann factors: #double logChiI, logBoltzFacI; boltzFacI = [ 0.0 for i in range(numStages) ] #print("numStages ", numStages, " Useful.logEv ", Useful.logEv()) for i in range(numStages): #print("i ", i, " chiIArr ", chiIArr[i]) logChiI = math.log(chiIArr[i]) + Useful.logEv() logBoltzFacI = logChiI - Useful.logK() boltzFacI[i] = math.exp(logBoltzFacI) logSahaFac = log2 + (3.0 / 2.0) * (log2pi + Useful.logMe() + Useful.logK() - 2.0 * Useful.logH()) #// return a 2D 5 x numDeps array of logarithmic number densities #// Row 0: neutral stage ground state population #// Row 1: singly ionized stage ground state population #// Row 2: doubly ionized stage ground state population #// Row 3: triply ionized stage ground state population #// Row 4: quadruply ionized stage ground state population #double[][] logNums = new double[numStages][numDeps]; logNums = [ [ 0.0 for i in range(numDeps)] for j in range(numStages) ] #//We need one more stage in size of saha factor than number of stages we're actualy populating #// for index accounting pirposes #// For atomic ionization stages: logSaha = [ [ 0.0 for i in range(numStages+1)] for j in range(numStages+1) ] saha = [ [ 0.0 for i in range(numStages+1)] for j in range(numStages+1) ] #// logIonFrac = [ 0.0 for i in range(numStages) ] #double expFac, logNe; #// Now - molecular variables: #//Treat at least one molecule - if there are really no molecules for an atomic species, #//there will be one phantom molecule in the denominator of the ionization fraction #//with an impossibly high dissociation energy ifMols = True if (numMols == 0): ifMols = False numMols = 1 #//This should be inherited, but let's make sure: dissEArr[0] = 19.0 #//eV #//Molecular partition functions - default initialization: #double[] thisLogUwB = new double[numMols]; thisLogUwB = [ 0.0 for i in range(numMols) ] for iMol in range(numMols): thisLogUwB[iMol] = 0.0 #// variable for temp-dependent computed partn fn of array element B thisLogUwA = 0.0 #// element A thisLogQwAB = math.log(300.0) #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: logUwA = [ 0.0 for i in range(5) ] if (numMols > 0): for kk in range(len(logUwA)): logUwA[kk] = logUw[0][kk] #// lburns #//} #//// Molecular partition functions: #//Molecular dissociation Boltzmann factors: boltzFacIAB = [ 0.0 for i in range(numMols) ] logMolSahaFac = [ 0.0 for i in range(numMols) ] #//if (numMols > 0){ #double logDissE, logBoltzFacIAB; for iMol in range(numMols): logDissE = math.log(dissEArr[iMol]) + Useful.logEv() logBoltzFacIAB = logDissE - Useful.logK() boltzFacIAB[iMol] = math.exp(logBoltzFacIAB) logMolSahaFac[iMol] = (3.0 / 2.0) * (log2pi + logMuABArr[iMol] + Useful.logK() - 2.0 * Useful.logH()) #//console.log("iMol " + iMol + " dissEArr[iMol] " + dissEArr[iMol] + " logDissE " + logE*logDissE + " logBoltzFacIAB " + logE*logBoltzFacIAB + " boltzFacIAB[iMol] " + boltzFacIAB[iMol] + " logMuABArr " + logE*logMuABArr[iMol] + " logMolSahaFac " + logE*logMolSahaFac[iMol]); #//} #// For molecular species: logSahaMol = [ 0.0 for i in range(numMols) ] invSahaMol = [ 0.0 for i in range(numMols) ] #JB# uua=[] uub=[] qwab=[] for iStg in range(numStages): currentUwArr=list(logUw[iStg])#u(T) determined values UwFit = ToolBox.cubicFit(masterTemp,currentUwArr)#u(T) fit uua.append(UwFit) #print(logUw[iStg]) for iMol in range(numMols): currentUwBArr=list(logUwB[iMol])#u(T) determined values UwBFit = ToolBox.cubicFit(masterTemp,currentUwBArr)#u(T) fit uub.append(UwBFit) for id in range(numDeps): #//// reduce or enhance number density by over-all Rosseland opcity scale parameter #// #//Row 1 of Ne is log_e Ne in cm^-3 logNe = Ne[1][id] #//Determine temperature dependent partition functions Uw: thisTemp = temp[0][id] #Ttheta = 5040.0 / thisTemp #JB# #use temps and partition values to create a function #then use said function to extrapolate values for all points thisLogUw[numStages] = 0.0 for iStg in range(numStages): thisLogUw[iStg] = ToolBox.valueFromFit(uua[iStg],thisTemp)#u(T) value extrapolated for iMol in range(numMols): thisLogUwB[iMol] = ToolBox.valueFromFit(uub[iMol],thisTemp)#u(T) value extrapolated #JB# #// NEW Determine temperature dependent partition functions Uw: lburns if (thisTemp <= 130.0): for iStg in range(numStages): thisLogUw[iStg] = logUw[iStg][0] for iMol in range(numMols): thisLogUwB[iMol] = logUwB[iMol][0] if (thisTemp >= 10000.0): for iStg in range(numStages): thisLogUw[iStg] = logUw[iStg][4] for iMol in range(numMols): thisLogUwB[iMol] = logUwB[iMol][4] for iMol in range(numMols): if (thisTemp < 3000.0): thisLogQwAB = ( logQwABArr[iMol][1] * (3000.0 - thisTemp)/(3000.0 - 500.0) ) \ + ( logQwABArr[iMol][2] * (thisTemp - 500.0)/(3000.0 - 500.0) ) if ( (thisTemp >= 3000.0) and (thisTemp <= 8000.0) ): thisLogQwAB = ( logQwABArr[iMol][2] * (8000.0 - thisTemp)/(8000.0 - 3000.0) ) \ + ( logQwABArr[iMol][3] * (thisTemp - 3000.0)/(8000.0 - 3000.0) ) if ( thisTemp > 8000.0 ): thisLogQwAB = ( logQwABArr[iMol][3] * (10000.0 - thisTemp)/(10000.0 - 8000.0) ) \ + ( logQwABArr[iMol][4] * (thisTemp - 8000.0)/(10000.0 - 8000.0) ) #// iMol loop #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: thisLogUwA = thisLogUw[0]; #//Ionization stage Saha factors: for iStg in range(numStages): #print("iStg ", iStg) logSaha[iStg+1][iStg] = logSahaFac - logNe - (boltzFacI[iStg] /temp[0][id]) + (3.0 * temp[1][id] / 2.0) + thisLogUw[iStg+1] - thisLogUw[iStg] saha[iStg+1][iStg] = math.exp(logSaha[iStg+1][iStg]) #//Molecular Saha factors: for iMol in range(numMols): logSahaMol[iMol] = logMolSahaFac[iMol] - logNumB[iMol][id] - (boltzFacIAB[iMol] / temp[0][id]) + (3.0 * temp[1][id] / 2.0) + thisLogUwB[iMol] + thisLogUwA - thisLogQwAB #//For denominator of ionization fraction, we need *inverse* molecular Saha factors (N_AB/NI): logSahaMol[iMol] = -1.0 * logSahaMol[iMol] invSahaMol[iMol] = math.exp(logSahaMol[iMol]) #//Compute log of denominator is ionization fraction, f_stage denominator = 1.0 #//default initialization - leading term is always unity #//ion stage contributions: for jStg in range(1, numStages+1): addend = 1.0 #//default initialization for product series for iStg in range(jStg): #//console.log("jStg " + jStg + " saha[][] indices " + (iStg+1) + " " + iStg); addend = addend * saha[iStg+1][iStg] denominator = denominator + addend #//molecular contribution if (ifMols == True): for iMol in range(numMols): denominator = denominator + invSahaMol[iMol] #// logDenominator = math.log(denominator) logIonFrac[0] = -1.0 * logDenominator #// log ionization fraction in stage I for jStg in range(1, numStages): addend = 0.0 #//default initialization for product series for iStg in range(jStg): #//console.log("jStg " + jStg + " saha[][] indices " + (iStg+1) + " " + iStg); addend = addend + logSaha[iStg+1][iStg] logIonFrac[jStg] = addend - logDenominator for iStg in range(numStages): logNums[iStg][id] = logNum[id] + logIonFrac[iStg] #//id loop return logNums; #//end method stagePops def stagePops3(logNum, Ne, chiIArr, logUw, numDeps, temp): #Version for ChromaStarPyGas: logNum is now *neutral stage* population from Phil # Bennett's GAS package #line 1: //species A data - ionization equilibrium of A #line 2: //data for set of species "B" - molecular equlibrium for set {AB} """Ionization equilibrium routine that accounts for molecule formation: // Returns depth distribution of ionization stage populations // Input parameters: // logNum - array with depth-dependent neutral stage number densities (cm^-3) // chiI1 - ground state ionization energy of neutral stage // chiI2 - ground state ionization energy of singly ionized stage // Also needs atsmopheric structure information: // numDeps // temp structure // rho structure // Atomic element A is the one whose ionization fractions are being computed // Element B refers to array of other species with which A forms molecules AB """ ln10 = math.log(10.0) logE = math.log10(math.e) #// for debug output log2pi = math.log(2.0 * math.pi) log2 = math.log(2.0) numStages = len(chiIArr) #// + 1; //need one more stage above the highest stage to be populated #// var numMols = dissEArr.length; #// Parition functions passed in are 2-element vectore with remperature-dependent base 10 log Us #// Convert to natural logs: #double Ttheta, thisTemp; #//Default initializations: #//We need one more stage in size of saha factor than number of stages we're actualy populating thisLogUw = [ 0.0 for i in range(numStages+1) ] for i in range(numStages+1): thisLogUw[i] = 0.0 logE10 = math.log(10.0) #//atomic ionization stage Boltzmann factors: #double logChiI, logBoltzFacI; boltzFacI = [ 0.0 for i in range(numStages) ] #print("numStages ", numStages, " Useful.logEv ", Useful.logEv()) for i in range(numStages): #print("i ", i, " chiIArr ", chiIArr[i]) logChiI = math.log(chiIArr[i]) + Useful.logEv() logBoltzFacI = logChiI - Useful.logK() boltzFacI[i] = math.exp(logBoltzFacI) logSahaFac = log2 + (3.0 / 2.0) * (log2pi + Useful.logMe() + Useful.logK() - 2.0 * Useful.logH()) #// return a 2D 5 x numDeps array of logarithmic number densities #// Row 0: neutral stage ground state population #// Row 1: singly ionized stage ground state population #// Row 2: doubly ionized stage ground state population #// Row 3: triply ionized stage ground state population #// Row 4: quadruply ionized stage ground state population #double[][] logNums = new double[numStages][numDeps]; logNums = [ [ 0.0 for i in range(numDeps)] for j in range(numStages) ] #//We need one more stage in size of saha factor than number of stages we're actualy populating #// for index accounting pirposes #// For atomic ionization stages: #logSaha = [ [ 0.0 for i in range(numStages+1)] for j in range(numStages+1) ] #saha = [ [ 0.0 for i in range(numStages+1)] for j in range(numStages+1) ] #// #logIonFrac = [ 0.0 for i in range(numStages) ] #double expFac, logNe; #JB# uua=[] uub=[] qwab=[] for iStg in range(numStages): currentUwArr=list(logUw[iStg])#u(T) determined values UwFit = ToolBox.cubicFit(masterTemp,currentUwArr)#u(T) fit uua.append(UwFit) #print(logUw[iStg]) for id in range(numDeps): #//// reduce or enhance number density by over-all Rosseland opcity scale parameter #// #//Row 1 of Ne is log_e Ne in cm^-3 logNe = Ne[1][id] #//Determine temperature dependent partition functions Uw: thisTemp = temp[0][id] #Ttheta = 5040.0 / thisTemp #JB# #use temps and partition values to create a function #then use said function to extrapolate values for all points thisLogUw[numStages] = 0.0 for iStg in range(numStages): thisLogUw[iStg] = ToolBox.valueFromFit(uua[iStg],thisTemp)#u(T) value extrapolated #JB# #// NEW Determine temperature dependent partition functions Uw: lburns if (thisTemp <= 130.0): for iStg in range(numStages): thisLogUw[iStg] = logUw[iStg][0] if (thisTemp >= 10000.0): for iStg in range(numStages): thisLogUw[iStg] = logUw[iStg][4] #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: #thisLogUwA = thisLogUw[0]; #//Ionization stage Saha factors: logNums[0][id] = logNum[id] for iStg in range(1, numStages): #print("iStg ", iStg) thisLogSaha = logSahaFac - logNe - (boltzFacI[iStg-1] /temp[0][id]) + (3.0 * temp[1][id] / 2.0) + thisLogUw[iStg] - thisLogUw[iStg-1] #saha[iStg+1][iStg] = math.exp(logSaha[iStg+1][iStg]) logNums[iStg][id] = logNums[iStg-1][id] + thisLogSaha #//id loop return logNums; #//end method stagePops #def sahaRHS(chiI, log10UwUArr, log10UwLArr, temp): def sahaRHS(chiI, logUwU, logUwL, temp): """RHS of partial pressure formulation of Saha equation in standard form (N_U*P_e/N_L on LHS) // Returns depth distribution of LHS: Phi(T) === N_U*P_e/N_L (David Gray notation) // Input parameters: // chiI - ground state ionization energy of lower stage // log10UwUArr, log10UwLArr - array of temperature-dependent partition function for upper and lower ionization stage // Also needs atsmopheric structure information: // numDeps // temp structure // // Atomic element "A" is the one whose ionization fractions are being computed // Element "B" refers to array of other species with which A forms molecules "AB" """ ln10 = math.log(10.0) logE = math.log10(math.e) #// for debug output log2pi = math.log(2.0 * math.pi) log2 = math.log(2.0) #// var numMols = dissEArr.length; #// Parition functions passed in are 2-element vectore with remperature-dependent base 10 log Us #// Convert to natural logs: #double Ttheta, thisTemp; #//Default initializations: #//We need one more stage in size of saha factor than number of stages we're actualy populating thisLogUwU = 0.0 thisLogUwL = 0.0 logE10 = math.log(10.0) #//We need one more stage in size of saha factor than number of stages we're actualy populating #logUwU = [0.0 for i in range(5)] #logUwL = [0.0 for i in range(5)] for kk in range(len(logUwL)): logUwU[kk] = logUwL[kk] # logUwL[kk] = logE10*log10UwLArr[kk] #//System.out.println("chiL before: " + chiL); #// If we need to subtract chiI from chiL, do so *before* converting to tiny numbers in ergs! #//atomic ionization stage Boltzmann factors: #double logChiI, logBoltzFacI; #double boltzFacI; logChiI = math.log(chiI) + Useful.logEv() logBoltzFacI = logChiI - Useful.logK() boltzFacI = math.exp(logBoltzFacI) #//Extra factor of k to get k^5/2 in the P_e formulation of Saha Eq. logSahaFac = log2 + (3.0 / 2.0) * (log2pi + Useful.logMe() + Useful.logK() - 2.0 * Useful.logH()) + Useful.logK() #//double[] logLHS = new double[numDeps]; #double logLHS; #// For atomic ionization stages: #double logSaha, saha, expFac; #// for (int id = 0; id < numDeps; id++) { #// #//Determine temperature dependent partition functions Uw: thisTemp = temp[0] #Ttheta = 5040.0 / thisTemp """ if (Ttheta >= 1.0): thisLogUwU = logUwU[0] thisLogUwL = logUwL[0] if (Ttheta <= 0.5): thisLogUwU = logUwU[1] thisLogUwL = logUwL[1] if (Ttheta > 0.5 and Ttheta < 1.0): thisLogUwU = ( logUwU[1] * (Ttheta - 0.5)/(1.0 - 0.5) ) + ( logUwU[0] * (1.0 - Ttheta)/(1.0 - 0.5) ) thisLogUwL = ( logUwL[1] * (Ttheta - 0.5)/(1.0 - 0.5) ) + ( logUwL[0] * (1.0 - Ttheta)/(1.0 - 0.5) ) """ #JB# currentUwUArr=list(logUwU)#u(T) determined values UwUFit = ToolBox.cubicFit(masterTemp,currentUwUArr)#u(T) fit thisLogUwU = ToolBox.valueFromFit(UwUFit,thisTemp)#u(T) value extrapolated currentUwLArr=list(logUwL)#u(T) determined values UwLFit = ToolBox.cubicFit(masterTemp,currentUwLArr)#u(T) fit thisLogUwL = ToolBox.valueFromFit(UwLFit,thisTemp)#u(T) value extrapolated #JB# #will need to do this one in Main as it goes through its own loop of temp #if thisTemp == superTemp[0][len(superTemp[0])]: # uwu.append(UwUFit) # uwl.append(UwLFit) # #JB# if (thisTemp <= 130.0): thisLogUwU = logUwU[0] thisLogUwL = logUwL[0] if (thisTemp >= 10000.0): thisLogUwU = logUwU[4] thisLogUwL = logUwL[4] """ if (thisTemp > 130 and thisTemp <= 500): thisLogUwU = logUwU[1] * (thisTemp - 130)/(500 - 130) \ + logUwU[0] * (500 - thisTemp)/(500 - 130) thisLogUwL = logUwL[1] * (thisTemp - 130)/(500 - 130) \ + logUwL[0] * (500 - thisTemp)/(500 - 130) if (thisTemp > 500 and thisTemp <= 3000): thisLogUwU = logUwU[2] * (thisTemp - 500)/(3000 - 500) \ + logUwU[1] * (3000 - thisTemp)/(3000 - 500) thisLogUwL = logUwL[2] * (thisTemp - 500)/(3000 - 500) \ + logUwL[1] * (3000 - thisTemp)/(3000 - 500) if (thisTemp > 3000 and thisTemp <= 8000): thisLogUwU = logUwU[3] * (thisTemp - 3000)/(8000 - 3000) \ + logUwU[2] * (8000 - thisTemp)/(8000 - 3000) thisLogUwL = logUwL[3] * (thisTemp - 3000)/(8000 - 3000) \ + logUwL[2] * (8000 - thisTemp)/(8000 - 3000) if (thisTemp > 8000 and thisTemp < 10000): thisLogUwU = logUwU[4] * (thisTemp - 8000)/(10000 - 8000) \ + logUwU[3] * (10000 - thisTemp)/(10000 - 8000) thisLogUwL = logUwL[4] * (thisTemp - 8000)/(10000 - 8000) \ + logUwL[3] * (10000 - thisTemp)/(10000 - 8000) if (thisTemp >= 10000): thisLogUwU = logUwU[4] thisLogUwL = logUwL[4] """ #//Ionization stage Saha factors: #//Need T_kin^5/2 in the P_e formulation of Saha Eq. logSaha = logSahaFac - (boltzFacI /temp[0]) + (5.0 * temp[1] / 2.0) + thisLogUwU - thisLogUwL #// saha = Math.exp(logSaha); #//logLHS[id] = logSaha; logLHS = logSaha; #// } //id loop return logLHS; #JB #return [logLHS,[[UwUFit,thisLogUwU],[UwLFit,thisLogUwL]]] #// # } //end method sahaRHS #def molPops(nmrtrLogNumB, nmrtrDissE, log10UwA, nmrtrLog10UwB, nmrtrLogQwAB, nmrtrLogMuAB, \ # numMolsB, logNumB, dissEArr, log10UwBArr, logQwABArr, logMuABArr, \ # logGroundRatio, numDeps, temp): def molPops(nmrtrLogNumB, nmrtrDissE, logUwA, nmrtrLogUwB, nmrtrLogQwAB, nmrtrLogMuAB, \ numMolsB, logNumB, dissEArr, logUwB, logQwABArr, logMuABArr, \ logGroundRatio, numDeps, temp): # line 1: //species A data - ionization equilibrium of A # //data for set of species "B" - molecular equlibrium for set {AB} """Diatomic molecular equilibrium routine that accounts for molecule formation: // Returns depth distribution of molecular population // Input parameters: // logNum - array with depth-dependent total element number densities (cm^-3) // chiI1 - ground state ionization energy of neutral stage // chiI2 - ground state ionization energy of singly ionized stage // Also needs atsmopheric structure information: // numDeps // temp structure // rho structure // // Atomic element "A" is the one kept on the LHS of the master fraction, whose ionization fractions are included // in the denominator of the master fraction // Element "B" refers to array of other sintpecies with which A forms molecules "AB" """ logE = math.log10(math.e) #// for debug output #//System.out.println("molPops: nmrtrDissE " + nmrtrDissE + " log10UwA " + log10UwA[0] + " " + log10UwA[1] + " nmrtrLog10UwB " + #// nmrtrLog10UwB[0] + " " + nmrtrLog10UwB[1] + " nmrtrLog10QwAB " + logE*nmrtrLogQwAB[2] + " nmrtrLogMuAB " + logE*nmrtrLogMuAB #// + " numMolsB " + numMolsB + " dissEArr " + dissEArr[0] + " log10UwBArr " + log10UwBArr[0][0] + " " + log10UwBArr[0][1] + " log10QwABArr " + #// logE*logQwABArr[0][2] + " logMuABArr " + logE*logMuABArr[0]); #//System.out.println("Line: nmrtrLog10UwB[0] " + logE*nmrtrLog10UwB[0] + " nmrtrLog10UwB[1] " + logE*nmrtrLog10UwB[1]); ln10 = math.log(10.0) log2pi = math.log(2.0 * math.pi) log2 = math.log(2.0) logE10 = math.log(10.0) #// Convert to natural logs: #double Ttheta, thisTemp; #//Treat at least one molecule - if there are really no molecules for an atomic species, #//there will be one phantom molecule in the denominator of the ionization fraction #//with an impossibly high dissociation energy if (numMolsB == 0): numMolsB = 1 #//This should be inherited, but let's make sure: dissEArr[0] = 29.0 #//eV #//var molPops = function(logNum, numeratorLogNumB, numeratorDissE, numeratorLog10UwA, numeratorLog10QwAB, numeratorLogMuAB, //species A data - ionization equilibrium of A #//Molecular partition functions - default initialization: thisLogUwB = [0.0 for i in range(numMolsB)] for iMol in range(numMolsB): thisLogUwB[iMol] = 0.0 #// variable for temp-dependent computed partn fn of array element B thisLogUwA = 0.0 #// element A nmrtrThisLogUwB = 0.0 #// element A thisLogQwAB = math.log(300.0) nmrtrThisLogQwAB = math.log(300.0) #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: #logUwA = [0.0 for i in range(5)] #nmrtrLogUwB = [0.0 for i in range(5)] #for kk in range(len(logUwA)): #logUwA[kk] = logE10*log10UwA[kk] #nmrtrLogUwB[kk] = logE10*nmrtrLog10UwB[kk] #// lburns #// Array of elements B for all molecular species AB: #double[][] logUwB = new double[numMolsB][2]; #logUwB = [ [ 0.0 for i in range(5) ] for j in range(numMolsB) ] #//if (numMolsB > 0){ #for iMol in range(numMolsB): # for kk in range(5): # logUwB[iMol][kk] = logE10*log10UwBArr[iMol][kk] # // lburns new loop #//} #// Molecular partition functions: #// double nmrtrLogQwAB = logE10*nmrtrLog10QwAB; #// double[] logQwAB = new double[numMolsB]; #// //if (numMolsB > 0){ #// for (int iMol = 0; iMol < numMolsB; iMol++){ #// logQwAB[iMol] = logE10*log10QwABArr[iMol]; #// } # //} #//Molecular dissociation Boltzmann factors: nmrtrBoltzFacIAB = 0.0 nmrtrLogMolSahaFac = 0.0 logDissE = math.log(nmrtrDissE) + Useful.logEv() #//System.out.println("logDissE " + logE*logDissE) logBoltzFacIAB = logDissE - Useful.logK() #//System.out.println("logBoltzFacIAB " + logE*logBoltzFacIAB); nmrtrBoltzFacIAB = math.exp(logBoltzFacIAB) nmrtrLogMolSahaFac = (3.0 / 2.0) * (log2pi + nmrtrLogMuAB + Useful.logK() - 2.0 * Useful.logH()) #//System.out.println("nmrtrLogMolSahaFac " + logE*nmrtrLogMolSahaFac); #//System.out.println("nmrtrDissE " + nmrtrDissE + " logDissE " + logE*logDissE + " logBoltzFacIAB " + logE*logBoltzFacIAB + " nmrtrBoltzFacIAB " + nmrtrBoltzFacIAB + " nmrtrLogMuAB " + logE*nmrtrLogMuAB + " nmrtrLogMolSahaFac " + logE*nmrtrLogMolSahaFac); boltzFacIAB = [0.0 for i in range(numMolsB)] logMolSahaFac = [0.0 for i in range(numMolsB)] #//if (numMolsB > 0){ for iMol in range(numMolsB): logDissE = math.log(dissEArr[iMol]) + Useful.logEv() logBoltzFacIAB = logDissE - Useful.logK() boltzFacIAB[iMol] = math.exp(logBoltzFacIAB) logMolSahaFac[iMol] = (3.0 / 2.0) * (log2pi + logMuABArr[iMol] + Useful.logK() - 2.0 * Useful.logH()) #//System.out.println("logMolSahaFac[iMol] " + logE*logMolSahaFac[iMol]); #//System.out.println("iMol " + iMol + " dissEArr[iMol] " + dissEArr[iMol] + " logDissE " + logE*logDissE + " logBoltzFacIAB " + logE*logBoltzFacIAB + " boltzFacIAB[iMol] " + boltzFacIAB[iMol] + " logMuABArr " + logE*logMuABArr[iMol] + " logMolSahaFac " + logE*logMolSahaFac[iMol]); #//double[] logNums = new double[numDeps] #//} #// For molecular species: #double nmrtrSaha, nmrtrLogSahaMol, nmrtrLogInvSahaMol; //, nmrtrInvSahaMol; logMolFrac = [0.0 for i in range(numDeps)] logSahaMol = [0.0 for i in range(numMolsB)] invSahaMol = [0.0 for i in range(numMolsB)] #JB# currentUwAArr=list(logUwA)#u(T) determined values UwAFit = ToolBox.cubicFit(masterTemp, currentUwAArr)#u(T) fit nmrtrLogUwBArr=list(nmrtrLogUwB)#u(T) determined values nmrtrLogUwBFit = ToolBox.cubicFit(masterTemp, nmrtrLogUwBArr)#u(T) fit #uwa.append(UwAFit) #uwb.append(nmrtrLogUwBFit) uwbFits=[] qwabFit = [] for iMol in range(numMolsB): currentUwBArr=list(logUwB[iMol]) UwBFit = ToolBox.cubicFit(masterTemp, currentUwBArr) uwbFits.append(UwBFit) currentLogQwABArr=list(logQwABArr[iMol])#u(T) determined values QwABFit = ToolBox.cubicFit(masterTemp, currentLogQwABArr)#u(T) fit qwabFit.append(QwABFit) #nmrtrQwABArr=list(nmrtrLogQwAB)#u(T) determined values #nmrtrQwABFit = ToolBox.cubicFit(masterTemp, nmrtrQwABArr)#u(T) fit #for Mols in range(numMolsB): # currentLogUwBArr=list(logUwB[Mols])#u(T) determined values # UwBFit=cubicFit(masterTemp,currentLogUwBArr)#u(T) fit #JB# #// temps=[] #valb=[] #vala=[] #valnb=[] #valqab=[] #valnmrtrqwb=[] #// System.out.println("molPops: id nmrtrLogNumB logNumBArr[0] logGroundRatio"); for id in range(numDeps): #//System.out.format("%03d, %21.15f, %21.15f, %21.15f, %n", id, logE*nmrtrLogNumB[id], logE*logNumB[0][id], logE*logGroundRatio[id]); #//// reduce or enhance number density by over-all Rosseland opcity scale parameter #//Determine temparature dependent partition functions Uw: thisTemp = temp[0][id] temps.append(thisTemp) #Ttheta = 5040.0 / thisTemp """ if (Ttheta >= 1.0): thisLogUwA = logUwA[0] nmrtrThisLogUwB = nmrtrLogUwB[0] for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][0] if (Ttheta <= 0.5): thisLogUwA = logUwA[1] nmrtrThisLogUwB = nmrtrLogUwB[1] for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][1] if (Ttheta > 0.5 and Ttheta < 1.0): thisLogUwA = ( logUwA[1] * ((Ttheta - 0.5)/(1.0 - 0.5)) ) \ + ( logUwA[0] * ((1.0 - Ttheta)/(1.0 - 0.5)) ) nmrtrThisLogUwB = ( nmrtrLogUwB[1] * ((Ttheta - 0.5)/(1.0 - 0.5)) ) \ + ( nmrtrLogUwB[0] * ((1.0 - Ttheta)/(1.0 - 0.5)) ) for iMol in range(numMolsB): thisLogUwB[iMol] = ( logUwB[iMol][1] * ((Ttheta - 0.5)/(1.0 - 0.5)) ) \ + ( logUwB[iMol][0] * ((1.0 - Ttheta)/(1.0 - 0.5)) ) """ #JB# thisLogUwA = float(ToolBox.valueFromFit(UwAFit,thisTemp))#u(T) value extrapolated #vala.append(thisLogUwA) nmrtrThisLogUwB = float(ToolBox.valueFromFit(nmrtrLogUwBFit,thisTemp))#u(T) value extrapolated #valnb.append(nmrtrThisLogUwB) #for iMol in range(numMolsB): # thisLogUwB[iMol]=logUwB[iMol] for iMol in range(numMolsB): thisLogUwB[iMol] = ToolBox.valueFromFit(uwbFits[iMol],thisTemp)#u(T) value extrapolated #valb.append(thisLogUwB[iMol]) #// NEW Determine temperature dependent partition functions Uw: lburns thisTemp = temp[0][id] if (thisTemp <= 130.0): thisLogUwA = logUwA[0] nmrtrThisLogUwB = nmrtrLogUwB[0] for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][0] if (thisTemp >= 10000.0): thisLogUwA = logUwA[4] nmrtrThisLogUwB = nmrtrLogUwB[4] for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][4] """ if (thisTemp > 130 and thisTemp <= 500): thisLogUwA = logUwA[1] * (thisTemp - 130)/(500 - 130) \ + logUwA[0] * (500 - thisTemp)/(500 - 130) nmrtrThisLogUwB = nmrtrLogUwB[1] * (thisTemp - 130)/(500 - 130) \ + nmrtrLogUwB[0] * (500 - thisTemp)/(500 - 130) for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][1] * (thisTemp - 130)/(500 - 130) \ + logUwB[iMol][0] * (500 - thisTemp)/(500 - 130) if (thisTemp > 500 and thisTemp <= 3000): thisLogUwA = logUwA[2] * (thisTemp - 500)/(3000 - 500) \ + logUwA[1] * (3000 - thisTemp)/(3000 - 500) nmrtrThisLogUwB = nmrtrLogUwB[2] * (thisTemp - 500)/(3000 - 500) \ + nmrtrLogUwB[1] * (3000 - thisTemp)/(3000 - 500) for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][2] * (thisTemp - 500)/(3000 - 500) \ + logUwB[iMol][1] * (3000 - thisTemp)/(3000 - 500) if (thisTemp > 3000 and thisTemp <= 8000): thisLogUwA = logUwA[3] * (thisTemp - 3000)/(8000 - 3000) \ + logUwA[2] * (8000 - thisTemp)/(8000 - 3000) nmrtrThisLogUwB = nmrtrLogUwB[3] * (thisTemp - 3000)/(8000 - 3000) \ + nmrtrLogUwB[2] * (8000 - thisTemp)/(8000 - 3000) for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][3] * (thisTemp - 3000)/(8000 - 3000) \ + logUwB[iMol][2] * (8000 - thisTemp)/(8000 - 3000) if (thisTemp > 8000 and thisTemp < 10000): thisLogUwA = logUwA[4] * (thisTemp - 8000)/(10000 - 8000) \ + logUwA[3] * (10000 - thisTemp)/(10000 - 8000) nmrtrThisLogUwB = nmrtrLogUwB[4] * (thisTemp - 8000)/(10000 - 8000) \ + nmrtrLogUwB[3] * (10000 - thisTemp)/(10000 - 8000) for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][4] * (thisTemp - 8000)/(10000 - 8000) \ + logUwB[iMol][3] * (10000 - thisTemp)/(10000 - 8000) if (thisTemp >= 10000): thisLogUwA = logUwA[4] nmrtrThisLogUwB = nmrtrLogUwB[4] for iMol in range(numMolsB): thisLogUwB[iMol] = logUwB[iMol][4] """ #iMol loops for Q's for iMol in range(numMolsB): if (thisTemp < 3000.0): thisLogQwAB = ( logQwABArr[iMol][1] * (3000.0 - thisTemp)/(3000.0 - 500.0) ) \ + ( logQwABArr[iMol][2] * (thisTemp - 500.0)/(3000.0 - 500.0) ) if ( (thisTemp >= 3000.0) and (thisTemp <= 8000.0) ): thisLogQwAB = ( logQwABArr[iMol][2] * (8000.0 - thisTemp)/(8000.0 - 3000.0) ) \ + ( logQwABArr[iMol][3] * (thisTemp - 3000.0)/(8000.0 - 3000.0) ) if ( thisTemp > 8000.0 ): thisLogQwAB = ( logQwABArr[iMol][3] * (10000.0 - thisTemp)/(10000.0 - 8000.0) ) \ + ( logQwABArr[iMol][4] * (thisTemp - 8000.0)/(10000.0 - 8000.0) ) if (thisTemp < 3000.0): nmrtrThisLogQwAB = ( nmrtrLogQwAB[1] * (3000.0 - thisTemp)/(3000.0 - 500.0) ) \ + ( nmrtrLogQwAB[2] * (thisTemp - 500.0)/(3000.0 - 500.0) ) if ( (thisTemp >= 3000.0) and (thisTemp <= 8000.0) ): nmrtrThisLogQwAB = ( nmrtrLogQwAB[2] * (8000.0 - thisTemp)/(8000.0 - 3000.0) ) \ + ( nmrtrLogQwAB[3] * (thisTemp - 3000.0)/(8000.0 - 3000.0) ) if ( thisTemp > 8000.0 ): nmrtrThisLogQwAB = ( nmrtrLogQwAB[3] * (10000.0 - thisTemp)/(10000.0 - 8000.0) ) \ + ( nmrtrLogQwAB[4] * (thisTemp - 8000.0)/(10000.0 - 8000.0) ) #//For clarity: neutral stage of atom whose ionization equilibrium is being computed is element A #// for molecule formation: # //Ionization stage Saha factors: #//System.out.println("id " + id + " nmrtrLogNumB[id] " + logE*nmrtrLogNumB[id]); # // if (id == 16){ # // System.out.println("id " + id + " nmrtrLogNumB[id] " + logE*nmrtrLogNumB[id] + " pp nmrtB " + (logE*(nmrtrLogNumB[id]+temp[1][id]+Useful.logK())) + " nmrtrThisLogUwB " + logE*nmrtrThisLogUwB + " thisLogUwA " + logE*thisLogUwA + " nmrtrLogQwAB " + logE*nmrtrThisLogQwAB); # //System.out.println("nmrtrThisLogUwB " + logE*nmrtrThisLogUwB + " thisLogUwA " + logE*thisLogUwA + " nmrtrThisLogQwAB " + logE*nmrtrThisLogQwAB); # // } nmrtrLogSahaMol = nmrtrLogMolSahaFac - nmrtrLogNumB[id] - (nmrtrBoltzFacIAB / temp[0][id]) + (3.0 * temp[1][id] / 2.0) + nmrtrThisLogUwB + thisLogUwA - nmrtrThisLogQwAB nmrtrLogInvSahaMol = -1.0 * nmrtrLogSahaMol #//System.out.println("nmrtrLogInvSahaMol " + logE*nmrtrLogInvSahaMol); #//nmrtrInvSahaMol = Math.exp(nmrtrLogSahaMol); #// if (id == 16){ #// System.out.println("nmrtrLogInvSahaMol " + logE*nmrtrLogInvSahaMol); #// } #// if (id == 16){ #// System.out.println("nmrtrBoltzFacIAB " + nmrtrBoltzFacIAB + " nmrtrThisLogUwB " + logE*nmrtrThisLogUwB + " thisLogUwA " + logE*thisLogUwA + " nmrtrThisLogQwAB " + nmrtrThisLogQwAB); #// System.out.println("nmrtrLogSahaMol " + logE*nmrtrLogSahaMol); // + " nmrtrInvSahaMol " + nmrtrInvSahaMol); #// } #//Molecular Saha factors: for iMol in range(numMolsB): #//System.out.println("iMol " + iMol + " id " + id + " logNumB[iMol][id] " + logE*nmrtrLogNumB[id]); #//System.out.println("iMol " + iMol + " thisLogUwB[iMol] " + logE*thisLogUwB[iMol] + " thisLogUwA " + logE*thisLogUwA + " thisLogQwAB " + logE*thisLogQwAB); logSahaMol[iMol] = logMolSahaFac[iMol] - logNumB[iMol][id] - (boltzFacIAB[iMol] / temp[0][id]) + (3.0 * temp[1][id] / 2.0) + float(thisLogUwB[iMol]) + thisLogUwA - thisLogQwAB #//For denominator of ionization fraction, we need *inverse* molecular Saha factors (N_AB/NI): logSahaMol[iMol] = -1.0 * logSahaMol[iMol] invSahaMol[iMol] = math.exp(logSahaMol[iMol]) #//TEST invSahaMol[iMol] = 1.0e-99; //test #// if (id == 16){ #// System.out.println("iMol " + iMol + " boltzFacIAB[iMol] " + boltzFacIAB[iMol] + " thisLogUwB[iMol] " + logE*thisLogUwB[iMol] + " logQwAB[iMol] " + logE*thisLogQwAB + " logNumB[iMol][id] " + logE*logNumB[iMol][id] + " logMolSahaFac[iMol] " + logE*logMolSahaFac[iMol]); #// System.out.println("iMol " + iMol + " logSahaMol " + logE*logSahaMol[iMol] + " invSahaMol[iMol] " + invSahaMol[iMol]); #// } #//Compute log of denominator is ionization fraction, f_stage # //default initialization # // - ratio of total atomic particles in all ionization stages to number in ground state: denominator = math.exp(logGroundRatio[id]) #//default initialization - ratio of total atomic particles in all ionization stages to number in ground state #//molecular contribution for iMol in range(numMolsB): #// if (id == 16){ #// System.out.println("invSahaMol[iMol] " + invSahaMol[iMol] + " denominator " + denominator); #// } denominator = denominator + invSahaMol[iMol] #// logDenominator = math.log(denominator) #//System.out.println("logGroundRatio[id] " + logE*logGroundRatio[id] + " logDenominator " + logE*logDenominator); #// if (id == 16){ #// System.out.println("id " + id + " logGroundRatio " + logGroundRatio[id] + " logDenominator " + logDenominator); #// } #//if (id == 36){ #// System.out.println("logDenominator " + logE*logDenominator); #// } #//var logDenominator = Math.log( 1.0 + saha21 + (saha32 * saha21) + (saha43 * saha32 * saha21) + (saha54 * saha43 * saha32 * saha21) ); logMolFrac[id] = nmrtrLogInvSahaMol - logDenominator #// if (id == 16){ #// System.out.println("id " + id + " logMolFrac[id] " + logE*logMolFrac[id]); #// } #//logNums[id] = logNum[id] + logMolFrac; #} //id loop #JB - check (never used)# #print(uwa) #print(uwb) #title("logUwA") """ plot(temps,vala) tempT=[] for t in masterTemp: tempT.append(valueFromFit(UwAFit,t)) scatter(masterTemp,(tempT)) show() #title("nmrtrlogUwB") plot(temps,valnb) tempT=[] for t in masterTemp: tempT.append(valueFromFit(nmrtrLogUwBFit,t)) scatter(masterTemp,(tempT)) show() #title("logUwB") plot(temps,valb) tempT=[] for t in masterTemp: tempT.append(valueFromFit(UwBFit,t)) scatter(masterTemp,(tempT)) show() #title("logQwAB") plot(temps,valqab) tempT=[] for t in masterTemp: tempT.append(valueFromFit(QwABFit,t)) scatter(masterTemp,(tempT)) show() #title("nmrtrlogQwAB") plot(temps,valnmrtrqwb) tempT=[] for t in masterTemp: tempT.append(valueFromFit(nmrtrQwABFit,t)) scatter(masterTemp,(tempT)) show() """ #JB# return logMolFrac #//end method stagePops
mit
JaviMerino/lisa
libs/utils/analysis/frequency_analysis.py
1
24894
# SPDX-License-Identifier: Apache-2.0 # # Copyright (C) 2015, ARM Limited and contributors. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ Frequency Analysis Module """ import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt import pandas as pd import pylab as pl import operator from trappy.utils import listify from devlib.utils.misc import memoized from collections import namedtuple from analysis_module import AnalysisModule # Configure logging import logging NON_IDLE_STATE = 4294967295 ResidencyTime = namedtuple('ResidencyTime', ['total', 'active']) ResidencyData = namedtuple('ResidencyData', ['label', 'residency']) class FrequencyAnalysis(AnalysisModule): """ Support for plotting Frequency Analysis data :param trace: input Trace object :type trace: :mod:`libs.utils.Trace` """ def __init__(self, trace): super(FrequencyAnalysis, self).__init__(trace) ############################################################################### # DataFrame Getter Methods ############################################################################### def _dfg_cpu_frequency_residency(self, cpu, total=True): """ Get per-CPU frequency residency, i.e. amount of time CPU `cpu` spent at each frequency. :param cpu: CPU ID :type cpu: int :param total: if true returns the "total" time, otherwise the "active" time is returned :type total: bool :returns: :mod:`pandas.DataFrame` - "total" or "active" time residency at each frequency. """ residency = self._getCPUFrequencyResidency(cpu) if not residency: return None if total: return residency.total return residency.active def _dfg_cluster_frequency_residency(self, cluster, total=True): """ Get per-Cluster frequency residency, i.e. amount of time CLUSTER `cluster` spent at each frequency. :param cluster: this can be either a single CPU ID or a list of CPU IDs belonging to a cluster or the cluster name as specified in the platform description :type cluster: str or int or list(int) :param total: if true returns the "total" time, otherwise the "active" time is returned :type total: bool :returns: :mod:`pandas.DataFrame` - "total" or "active" time residency at each frequency. """ residency = self._getClusterFrequencyResidency(cluster) if not residency: return None if total: return residency.total return residency.active ############################################################################### # Plotting Methods ############################################################################### def plotClusterFrequencies(self, title='Clusters Frequencies'): """ Plot frequency trend for all clusters. If sched_overutilized events are available, the plots will also show the intervals of time where the cluster was overutilized. :param title: user-defined plot title :type title: str """ if not self._trace.hasEvents('cpu_frequency'): logging.warn('Events [cpu_frequency] not found, plot DISABLED!') return df = self._dfg_trace_event('cpu_frequency') pd.options.mode.chained_assignment = None # Extract LITTLE and big clusters frequencies # and scale them to [MHz] if len(self._platform['clusters']['little']): lfreq = df[df.cpu == self._platform['clusters']['little'][-1]] lfreq['frequency'] = lfreq['frequency']/1e3 else: lfreq = [] if len(self._platform['clusters']['big']): bfreq = df[df.cpu == self._platform['clusters']['big'][-1]] bfreq['frequency'] = bfreq['frequency']/1e3 else: bfreq = [] # Compute AVG frequency for LITTLE cluster avg_lfreq = 0 if len(lfreq) > 0: lfreq['timestamp'] = lfreq.index lfreq['delta'] = (lfreq['timestamp'] -lfreq['timestamp'].shift()).fillna(0).shift(-1) lfreq['cfreq'] = (lfreq['frequency'] * lfreq['delta']).fillna(0) timespan = lfreq.iloc[-1].timestamp - lfreq.iloc[0].timestamp avg_lfreq = lfreq['cfreq'].sum()/timespan # Compute AVG frequency for big cluster avg_bfreq = 0 if len(bfreq) > 0: bfreq['timestamp'] = bfreq.index bfreq['delta'] = (bfreq['timestamp'] - bfreq['timestamp'].shift()).fillna(0).shift(-1) bfreq['cfreq'] = (bfreq['frequency'] * bfreq['delta']).fillna(0) timespan = bfreq.iloc[-1].timestamp - bfreq.iloc[0].timestamp avg_bfreq = bfreq['cfreq'].sum()/timespan pd.options.mode.chained_assignment = 'warn' # Setup a dual cluster plot fig, pltaxes = plt.subplots(2, 1, figsize=(16, 8)) plt.suptitle(title, y=.97, fontsize=16, horizontalalignment='center') # Plot Cluster frequencies axes = pltaxes[0] axes.set_title('big Cluster') if avg_bfreq > 0: axes.axhline(avg_bfreq, color='r', linestyle='--', linewidth=2) axes.set_ylim( (self._platform['freqs']['big'][0] - 100000)/1e3, (self._platform['freqs']['big'][-1] + 100000)/1e3 ) if len(bfreq) > 0: bfreq['frequency'].plot(style=['r-'], ax=axes, drawstyle='steps-post', alpha=0.4) else: logging.warn('NO big CPUs frequency events to plot') axes.set_xlim(self._trace.x_min, self._trace.x_max) axes.set_ylabel('MHz') axes.grid(True) axes.set_xticklabels([]) axes.set_xlabel('') self._trace.analysis.status.plotOverutilized(axes) axes = pltaxes[1] axes.set_title('LITTLE Cluster') if avg_lfreq > 0: axes.axhline(avg_lfreq, color='b', linestyle='--', linewidth=2) axes.set_ylim( (self._platform['freqs']['little'][0] - 100000)/1e3, (self._platform['freqs']['little'][-1] + 100000)/1e3 ) if len(lfreq) > 0: lfreq['frequency'].plot(style=['b-'], ax=axes, drawstyle='steps-post', alpha=0.4) else: logging.warn('NO LITTLE CPUs frequency events to plot') axes.set_xlim(self._trace.x_min, self._trace.x_max) axes.set_ylabel('MHz') axes.grid(True) self._trace.analysis.status.plotOverutilized(axes) # Save generated plots into datadir figname = '{}/{}cluster_freqs.png'\ .format(self._trace.plots_dir, self._trace.plots_prefix) pl.savefig(figname, bbox_inches='tight') logging.info('LITTLE cluster average frequency: %.3f GHz', avg_lfreq/1e3) logging.info('big cluster average frequency: %.3f GHz', avg_bfreq/1e3) return (avg_lfreq/1e3, avg_bfreq/1e3) def plotCPUFrequencyResidency(self, cpus=None, pct=False, active=False): """ Plot per-CPU frequency residency. big CPUs are plotted first and then LITTLEs. Requires the following trace events: - cpu_frequency - cpu_idle :param cpus: List of cpus. By default plot all CPUs :type cpus: list(str) :param pct: plot residencies in percentage :type pct: bool :param active: for percentage plot specify whether to plot active or total time. Default is TOTAL time :type active: bool """ if not self._trace.hasEvents('cpu_frequency'): logging.warn('Events [cpu_frequency] not found, plot DISABLED!') return if not self._trace.hasEvents('cpu_idle'): logging.warn('Events [cpu_idle] not found, plot DISABLED!') return if cpus is None: # Generate plots only for available CPUs cpufreq_data = self._dfg_trace_event('cpu_frequency') _cpus = range(cpufreq_data.cpu.max()+1) else: _cpus = listify(cpus) # Split between big and LITTLE CPUs ordered from higher to lower ID _cpus.reverse() big_cpus = [c for c in _cpus if c in self._platform['clusters']['big']] little_cpus = [c for c in _cpus if c in self._platform['clusters']['little']] _cpus = big_cpus + little_cpus # Precompute active and total time for each CPU residencies = [] xmax = 0.0 for cpu in _cpus: res = self._getCPUFrequencyResidency(cpu) residencies.append(ResidencyData('CPU{}'.format(cpu), res)) max_time = res.total.max().values[0] if xmax < max_time: xmax = max_time self._plotFrequencyResidency(residencies, 'cpu', xmax, pct, active) def plotClusterFrequencyResidency(self, clusters=None, pct=False, active=False): """ Plot the frequency residency in a given cluster, i.e. the amount of time cluster `cluster` spent at frequency `f_i`. By default, both 'big' and 'LITTLE' clusters data are plotted. Requires the following trace events: - cpu_frequency - cpu_idle :param clusters: name of the clusters to be plotted (all of them by default) :type clusters: str ot list(str) :param pct: plot residencies in percentage :type pct: bool :param active: for percentage plot specify whether to plot active or total time. Default is TOTAL time :type active: bool """ if not self._trace.hasEvents('cpu_frequency'): logging.warn('Events [cpu_frequency] not found, plot DISABLED!') return if not self._trace.hasEvents('cpu_idle'): logging.warn('Events [cpu_idle] not found, plot DISABLED!') return # Assumption: all CPUs in a cluster run at the same frequency, i.e. the # frequency is scaled per-cluster not per-CPU. Hence, we can limit the # cluster frequencies data to a single CPU if not self._trace.freq_coherency: logging.warn('Cluster frequency is not coherent, plot DISABLED!') return # Sanitize clusters if clusters is None: _clusters = self._platform['clusters'].keys() else: _clusters = listify(clusters) # Precompute active and total time for each cluster residencies = [] xmax = 0.0 for cluster in _clusters: res = self._getClusterFrequencyResidency( self._platform['clusters'][cluster.lower()]) residencies.append(ResidencyData('{} Cluster'.format(cluster), res)) max_time = res.total.max().values[0] if xmax < max_time: xmax = max_time self._plotFrequencyResidency(residencies, 'cluster', xmax, pct, active) ############################################################################### # Utility Methods ############################################################################### @memoized def _getCPUActiveSignal(self, cpu): """ Build a square wave representing the active (i.e. non-idle) CPU time, i.e.: cpu_active[t] == 1 if at least one CPU is reported to be non-idle by CPUFreq at time t cpu_active[t] == 0 otherwise :param cpu: CPU ID :type cpu: int """ if not self._trace.hasEvents('cpu_idle'): logging.warn('Events [cpu_idle] not found, ' 'cannot compute CPU active signal!') return None idle_df = self._dfg_trace_event('cpu_idle') cpu_df = idle_df[idle_df.cpu_id == cpu] cpu_active = cpu_df.state.apply( lambda s: 1 if s == NON_IDLE_STATE else 0 ) start_time = 0.0 if not self._trace.ftrace.normalized_time: start_time = self._trace.ftrace.basetime if cpu_active.index[0] != start_time: entry_0 = pd.Series(cpu_active.iloc[0] ^ 1, index=[start_time]) cpu_active = pd.concat([entry_0, cpu_active]) return cpu_active @memoized def _getClusterActiveSignal(self, cluster): """ Build a square wave representing the active (i.e. non-idle) cluster time, i.e.: cluster_active[t] == 1 if at least one CPU is reported to be non-idle by CPUFreq at time t cluster_active[t] == 0 otherwise :param cluster: list of CPU IDs belonging to a cluster :type cluster: list(int) """ cpu_active = {} for cpu in cluster: cpu_active[cpu] = self._getCPUActiveSignal(cpu) active = pd.DataFrame(cpu_active) active.fillna(method='ffill', inplace=True) # Cluster active is the OR between the actives on each CPU # belonging to that specific cluster cluster_active = reduce( operator.or_, [cpu_active.astype(int) for _, cpu_active in active.iteritems()] ) return cluster_active @memoized def _getClusterFrequencyResidency(self, cluster): """ Get a DataFrame with per cluster frequency residency, i.e. amount of time spent at a given frequency in each cluster. :param cluster: this can be either a single CPU ID or a list of CPU IDs belonging to a cluster or the cluster name as specified in the platform description :type cluster: str or int or list(int) :returns: namedtuple(ResidencyTime) - tuple of total and active time dataframes :raises: KeyError """ if not self._trace.hasEvents('cpu_frequency'): logging.warn('Events [cpu_frequency] not found, ' 'frequency residency computation not possible!') return None if not self._trace.hasEvents('cpu_idle'): logging.warn('Events [cpu_idle] not found, ' 'frequency residency computation not possible!') return None if isinstance(cluster, str): try: _cluster = self._platform['clusters'][cluster.lower()] except KeyError: logging.warn('%s cluster not found!', cluster) return None else: _cluster = listify(cluster) freq_df = self._dfg_trace_event('cpu_frequency') # Assumption: all CPUs in a cluster run at the same frequency, i.e. the # frequency is scaled per-cluster not per-CPU. Hence, we can limit the # cluster frequencies data to a single CPU. This assumption is verified # by the Trace module when parsing the trace. if len(_cluster) > 1 and not self._trace.freq_coherency: logging.warn('Cluster frequency is NOT coherent,' 'cannot compute residency!') return None cluster_freqs = freq_df[freq_df.cpu == _cluster[0]] # Compute TOTAL Time time_intervals = cluster_freqs.index[1:] - cluster_freqs.index[:-1] total_time = pd.DataFrame({ 'time': time_intervals, 'frequency': [f/1000.0 for f in cluster_freqs.iloc[:-1].frequency] }) total_time = total_time.groupby(['frequency']).sum() # Compute ACTIVE Time cluster_active = self._getClusterActiveSignal(_cluster) # In order to compute the active time spent at each frequency we # multiply 2 square waves: # - cluster_active, a square wave of the form: # cluster_active[t] == 1 if at least one CPU is reported to be # non-idle by CPUFreq at time t # cluster_active[t] == 0 otherwise # - freq_active, square wave of the form: # freq_active[t] == 1 if at time t the frequency is f # freq_active[t] == 0 otherwise available_freqs = sorted(cluster_freqs.frequency.unique()) new_idx = sorted(cluster_freqs.index.tolist() + cluster_active.index.tolist()) cluster_freqs = cluster_freqs.reindex(new_idx, method='ffill') cluster_active = cluster_active.reindex(new_idx, method='ffill') nonidle_time = [] for f in available_freqs: freq_active = cluster_freqs.frequency.apply( lambda x: 1 if x == f else 0 ) active_t = cluster_active * freq_active # Compute total time by integrating the square wave nonidle_time.append(self._trace.integrate_square_wave(active_t)) active_time = pd.DataFrame({'time': nonidle_time}, index=[f/1000.0 for f in available_freqs]) active_time.index.name = 'frequency' return ResidencyTime(total_time, active_time) def _getCPUFrequencyResidency(self, cpu): """ Get a DataFrame with per-CPU frequency residency, i.e. amount of time CPU `cpu` spent at each frequency. Both total and active times will be computed. :param cpu: CPU ID :type cpu: int :returns: namedtuple(ResidencyTime) - tuple of total and active time dataframes """ return self._getClusterFrequencyResidency(cpu) def _plotFrequencyResidencyAbs(self, axes, residency, n_plots, is_first, is_last, xmax, title=''): """ Private method to generate frequency residency plots. :param axes: axes over which to generate the plot :type axes: matplotlib.axes.Axes :param residency: tuple of total and active time dataframes :type residency: namedtuple(ResidencyTime) :param n_plots: total number of plots :type n_plots: int :param is_first: if True this is the first plot :type is_first: bool :param is_last: if True this is the last plot :type is_last: bool :param xmax: x-axes higher bound :param xmax: double :param title: title of this subplot :type title: str """ yrange = 0.4 * max(6, len(residency.total)) * n_plots residency.total.plot.barh(ax=axes, color='g', legend=False, figsize=(16, yrange)) residency.active.plot.barh(ax=axes, color='r', legend=False, figsize=(16, yrange)) axes.set_xlim(0, 1.05*xmax) axes.set_ylabel('Frequency [MHz]') axes.set_title(title) axes.grid(True) if is_last: axes.set_xlabel('Time [s]') else: axes.set_xticklabels([]) if is_first: # Put title on top of the figure. As of now there is no clean way # to make the title appear always in the same position in the # figure because figure heights may vary between different # platforms (different number of OPPs). Hence, we use annotation legend_y = axes.get_ylim()[1] axes.annotate('OPP Residency Time', xy=(0, legend_y), xytext=(-50, 45), textcoords='offset points', fontsize=18) axes.annotate('GREEN: Total', xy=(0, legend_y), xytext=(-50, 25), textcoords='offset points', color='g', fontsize=14) axes.annotate('RED: Active', xy=(0, legend_y), xytext=(50, 25), textcoords='offset points', color='r', fontsize=14) def _plotFrequencyResidencyPct(self, axes, residency_df, label, n_plots, is_first, is_last, res_type): """ Private method to generate PERCENTAGE frequency residency plots. :param axes: axes over which to generate the plot :type axes: matplotlib.axes.Axes :param residency_df: residency time dataframe :type residency_df: :mod:`pandas.DataFrame` :param label: label to be used for percentage residency dataframe :type label: str :param n_plots: total number of plots :type n_plots: int :param is_first: if True this is the first plot :type is_first: bool :param is_first: if True this is the last plot :type is_first: bool :param res_type: type of residency, either TOTAL or ACTIVE :type title: str """ # Compute sum of the time intervals duration = residency_df.time.sum() residency_pct = pd.DataFrame( {label: residency_df.time.apply(lambda x: x*100/duration)}, index=residency_df.index ) yrange = 3 * n_plots residency_pct.T.plot.barh(ax=axes, stacked=True, figsize=(16, yrange)) axes.legend(loc='lower center', ncol=7) axes.set_xlim(0, 100) axes.grid(True) if is_last: axes.set_xlabel('Residency [%]') else: axes.set_xticklabels([]) if is_first: legend_y = axes.get_ylim()[1] axes.annotate('OPP {} Residency Time'.format(res_type), xy=(0, legend_y), xytext=(-50, 35), textcoords='offset points', fontsize=18) def _plotFrequencyResidency(self, residencies, entity_name, xmax, pct, active): """ Generate Frequency residency plots for the given entities. :param residencies: :type residencies: namedtuple(ResidencyData) - tuple containing: 1) as first element, a label to be used as subplot title 2) as second element, a namedtuple(ResidencyTime) :param entity_name: name of the entity ('cpu' or 'cluster') used in the figure name :type entity_name: str :param xmax: upper bound of x-axes :type xmax: double :param pct: plot residencies in percentage :type pct: bool :param active: for percentage plot specify whether to plot active or total time. Default is TOTAL time :type active: bool """ n_plots = len(residencies) gs = gridspec.GridSpec(n_plots, 1) fig = plt.figure() figtype = "" for idx, data in enumerate(residencies): if data.residency is None: plt.close(fig) return axes = fig.add_subplot(gs[idx]) is_first = idx == 0 is_last = idx+1 == n_plots if pct and active: self._plotFrequencyResidencyPct(axes, data.residency.active, data.label, n_plots, is_first, is_last, 'ACTIVE') figtype = "_pct_active" continue if pct: self._plotFrequencyResidencyPct(axes, data.residency.total, data.label, n_plots, is_first, is_last, 'TOTAL') figtype = "_pct_total" continue self._plotFrequencyResidencyAbs(axes, data.residency, n_plots, is_first, is_last, xmax, title=data.label) figname = '{}/{}{}_freq_residency{}.png'\ .format(self._trace.plots_dir, self._trace.plots_prefix, entity_name, figtype) pl.savefig(figname, bbox_inches='tight') # vim :set tabstop=4 shiftwidth=4 expandtab
apache-2.0
MadsJensen/malthe_alpha_project
source_connectivity_permutation.py
1
6505
# -*- coding: utf-8 -*- """ Created on Wed Sep 9 08:41:17 2015. @author: mje """ import numpy as np import numpy.random as npr import os import socket import mne # import pandas as pd from mne.connectivity import spectral_connectivity from mne.minimum_norm import (apply_inverse_epochs, read_inverse_operator) # Permutation test. def permutation_resampling(case, control, num_samples, statistic): """ Permutation test. Return p-value that statistic for case is different from statistc for control. """ observed_diff = abs(statistic(case) - statistic(control)) num_case = len(case) combined = np.concatenate([case, control]) diffs = [] for i in range(num_samples): xs = npr.permutation(combined) diff = np.mean(xs[:num_case]) - np.mean(xs[num_case:]) diffs.append(diff) pval = (np.sum(diffs > observed_diff) + np.sum(diffs < -observed_diff))/float(num_samples) return pval, observed_diff, diffs def permutation_test(a, b, num_samples, statistic): """ Permutation test. Return p-value that statistic for a is different from statistc for b. """ observed_diff = abs(statistic(b) - statistic(a)) num_a = len(a) combined = np.concatenate([a, b]) diffs = [] for i in range(num_samples): xs = npr.permutation(combined) diff = np.mean(xs[:num_a]) - np.mean(xs[num_a:]) diffs.append(diff) pval = np.sum(np.abs(diffs) >= np.abs(observed_diff)) / float(num_samples) return pval, observed_diff, diffs # Setup paths and prepare raw data hostname = socket.gethostname() if hostname == "Wintermute": data_path = "/home/mje/mnt/caa/scratch/" n_jobs = 1 else: data_path = "/projects/MINDLAB2015_MEG-CorticalAlphaAttention/scratch/" n_jobs = 1 subjects_dir = data_path + "fs_subjects_dir/" # change dir to save files the rigth place os.chdir(data_path) fname_inv = data_path + '0001-meg-oct-6-inv.fif' fname_epochs = data_path + '0001_p_03_filter_ds_ica-mc_tsss-epo.fif' fname_evoked = data_path + "0001_p_03_filter_ds_ica-mc_raw_tsss-ave.fif" # Parameters snr = 1.0 # Standard assumption for average data but using it for single trial lambda2 = 1.0 / snr ** 2 method = "dSPM" # use dSPM method (could also be MNE or sLORETA) # Load data inverse_operator = read_inverse_operator(fname_inv) epochs = mne.read_epochs(fname_epochs) # Get labels for FreeSurfer 'aparc' cortical parcellation with 34 labels/hemi #labels = mne.read_labels_from_annot('0001', parc='PALS_B12_Lobes', labels = mne.read_labels_from_annot('0001', parc='PALS_B12_Brodmann', regexp="Brodmann", subjects_dir=subjects_dir) labels_occ = labels[6:12] # labels = mne.read_labels_from_annot('subject_1', parc='aparc.DKTatlas40', # subjects_dir=subjects_dir) for cond in epochs.event_id.keys(): stcs = apply_inverse_epochs(epochs[cond], inverse_operator, lambda2, method, pick_ori="normal") exec("stcs_%s = stcs" % cond) labels_name = [label.name for label in labels_occ] for label in labels_occ: labels_name += [label.name] # Extract time series ts_ctl_left = mne.extract_label_time_course(stcs_ctl_left, labels_occ, src=inverse_operator["src"], mode = "mean_flip") ts_ent_left = mne.extract_label_time_course(stcs_ent_left, labels_occ, src=inverse_operator["src"], mode = "mean_flip") stcs_all_left = stcs_ctl_left + stcs_ent_left ts_all_left = np.asarray(mne.extract_label_time_course(stcs_all_left, labels_occ, src=inverse_operator["src"], mode = "mean_flip")) number_of_permutations = 2000 index = np.arange(0, len(ts_all_left)) permutations_results = np.empty(number_of_permutations) fmin, fmax = 7, 12 tmin, tmax = 0, 1 con_method = "plv" diff_permuatation = np.empty([6, 6, number_of_permutations]) # diff con_ctl, freqs_ctl, times_ctl, n_epochs_ctl, n_tapers_ctl =\ spectral_connectivity( ts_ctl_left, method=con_method, mode='multitaper', sfreq=250, fmin=fmin, fmax=fmax, faverage=True, tmin=tmin, tmax=tmax, mt_adaptive=False, n_jobs=1, verbose=None) con_ent, freqs_ent, times_ent, n_epochs_ent, n_tapers_ent =\ spectral_connectivity( ts_ent_left, method=con_method, mode='multitaper', sfreq=250, fmin=fmin, fmax=fmax, faverage=True, tmin=tmin, tmax=tmax, mt_adaptive=False, n_jobs=1, verbose=None) diff = con_ctl[:, :, 0] - con_ent[:, :, 0] for i in range(number_of_permutations): index = np.random.permutation(index) tmp_ctl = ts_all_left[index[:64], :, :] tmp_case = ts_all_left[index[64:], :, :] con_ctl, freqs_ctl, times_ctl, n_epochs_ctl, n_tapers_ctl =\ spectral_connectivity( tmp_ctl, method=con_method, mode='multitaper', sfreq=250, fmin=fmin, fmax=fmax, faverage=True, tmin=tmin, tmax=tmax, mt_adaptive=False, n_jobs=1) con_case, freqs_case, times_case, n_epochs_case, n_tapers_case =\ spectral_connectivity( tmp_case, method=con_method, mode='multitaper', sfreq=250, fmin=fmin, fmax=fmax, faverage=True, tmin=tmin, tmax=tmax, mt_adaptive=False, n_jobs=1) diff_permuatation[:, :, i] = con_ctl[:, :, 0] - con_case[:, :, 0] pval = np.empty_like(diff) for h in range(diff.shape[0]): for j in range(diff.shape[1]): if diff[h, j] != 0: pval[h, j] = np.sum(np.abs(diff_permuatation[h, h, :] >= np.abs(diff[h, j, :])))/float(number_of_permutations) # np.sum(np.abs(diff[h, j]) >= np.abs( # diff_permuatation[h, j, :]))\ # / float(number_of_permutations)
mit
jblackburne/scikit-learn
doc/tutorial/text_analytics/solutions/exercise_02_sentiment.py
104
3139
"""Build a sentiment analysis / polarity model Sentiment analysis can be casted as a binary text classification problem, that is fitting a linear classifier on features extracted from the text of the user messages so as to guess wether the opinion of the author is positive or negative. In this examples we will use a movie review dataset. """ # Author: Olivier Grisel <olivier.grisel@ensta.org> # License: Simplified BSD import sys from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV from sklearn.datasets import load_files from sklearn.model_selection import train_test_split from sklearn import metrics if __name__ == "__main__": # NOTE: we put the following in a 'if __name__ == "__main__"' protected # block to be able to use a multi-core grid search that also works under # Windows, see: http://docs.python.org/library/multiprocessing.html#windows # The multiprocessing module is used as the backend of joblib.Parallel # that is used when n_jobs != 1 in GridSearchCV # the training data folder must be passed as first argument movie_reviews_data_folder = sys.argv[1] dataset = load_files(movie_reviews_data_folder, shuffle=False) print("n_samples: %d" % len(dataset.data)) # split the dataset in training and test set: docs_train, docs_test, y_train, y_test = train_test_split( dataset.data, dataset.target, test_size=0.25, random_state=None) # TASK: Build a vectorizer / classifier pipeline that filters out tokens # that are too rare or too frequent pipeline = Pipeline([ ('vect', TfidfVectorizer(min_df=3, max_df=0.95)), ('clf', LinearSVC(C=1000)), ]) # TASK: Build a grid search to find out whether unigrams or bigrams are # more useful. # Fit the pipeline on the training set using grid search for the parameters parameters = { 'vect__ngram_range': [(1, 1), (1, 2)], } grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1) grid_search.fit(docs_train, y_train) # TASK: print the mean and std for each candidate along with the parameter # settings for all the candidates explored by grid search. n_candidates = len(grid_search.cv_results_['params']) for i in range(n_candidates): print(i, 'params - %s; mean - %0.2f; std - %0.2f' % (grid_search.cv_results_['params'][i], grid_search.cv_results_['mean_test_score'][i], grid_search.cv_results_['std_test_score'][i])) # TASK: Predict the outcome on the testing set and store it in a variable # named y_predicted y_predicted = grid_search.predict(docs_test) # Print the classification report print(metrics.classification_report(y_test, y_predicted, target_names=dataset.target_names)) # Print and plot the confusion matrix cm = metrics.confusion_matrix(y_test, y_predicted) print(cm) # import matplotlib.pyplot as plt # plt.matshow(cm) # plt.show()
bsd-3-clause
smblance/ggplot
ggplot/tests/test_chart_components.py
12
1664
from __future__ import (absolute_import, division, print_function, unicode_literals) import numpy as np import pandas as pd from nose.tools import assert_raises, assert_equal, assert_is_none from ggplot import * from ggplot.utils.exceptions import GgplotError def test_chart_components(): """ Test invalid arguments to chart components """ df = pd.DataFrame({'x': np.arange(10), 'y': np.arange(10)}) gg = ggplot(df, aes(x='x', y='y')) # test ggtitle assert_raises(GgplotError, ggtitle, None) # test xlim assert_raises(GgplotError, xlim, "foo", 1) assert_raises(GgplotError, xlim, "foo", "bar") # test ylim assert_raises(GgplotError, ylim, "foo", 1) assert_raises(GgplotError, ylim, "foo", "bar") # test xlab assert_raises(GgplotError, ylab, None) # test ylab assert_raises(GgplotError, ylab, None) # test labs test_xlab = 'xlab' gg_xlab = gg + labs(x=test_xlab) assert_equal(gg_xlab.xlab, test_xlab) assert_is_none(gg_xlab.ylab) assert_is_none(gg_xlab.title) test_ylab = 'ylab' gg_ylab = gg + labs(y=test_ylab) assert_is_none(gg_ylab.xlab) assert_equal(gg_ylab.ylab, test_ylab) assert_is_none(gg_ylab.title) test_title = 'title' gg_title = gg + labs(title=test_title) assert_is_none(gg_title.xlab) assert_is_none(gg_title.ylab) assert_equal(gg_title.title, test_title) gg_labs = gg + labs(x=test_xlab, y=test_ylab, title=test_title) assert_equal(gg_labs.xlab, test_xlab) assert_equal(gg_labs.ylab, test_ylab) assert_equal(gg_labs.title, test_title)
bsd-2-clause
jrbourbeau/cr-composition
processing/legacy/anisotropy/random_trials/process_kstest.py
2
7627
#!/usr/bin/env python import os import argparse import numpy as np import pandas as pd import pycondor import comptools as comp if __name__ == "__main__": p = argparse.ArgumentParser( description='Extracts and saves desired information from simulation/data .i3 files') p.add_argument('-c', '--config', dest='config', default='IC86.2012', choices=['IC79', 'IC86.2012', 'IC86.2013', 'IC86.2014', 'IC86.2015'], help='Detector configuration') p.add_argument('--low_energy', dest='low_energy', default=False, action='store_true', help='Only use events with energy < 10**6.75 GeV') p.add_argument('--n_side', dest='n_side', type=int, default=64, help='Number of times to split the DataFrame') p.add_argument('--chunksize', dest='chunksize', type=int, default=1000, help='Number of lines used when reading in DataFrame') p.add_argument('--n_batches', dest='n_batches', type=int, default=50, help='Number batches running in parallel for each ks-test trial') p.add_argument('--ks_trials', dest='ks_trials', type=int, default=100, help='Number of random maps to generate') p.add_argument('--overwrite', dest='overwrite', default=False, action='store_true', help='Option to overwrite reference map file, ' 'if it alreadu exists') p.add_argument('--test', dest='test', default=False, action='store_true', help='Option to run small test version') args = p.parse_args() if args.test: args.ks_trials = 20 args.n_batches = 10000 args.chunksize = 100 # Define output directories error = comp.paths.condor_data_dir + '/ks_test_{}/error'.format(args.config) output = comp.paths.condor_data_dir + '/ks_test_{}/output'.format(args.config) log = comp.paths.condor_scratch_dir + '/ks_test_{}/log'.format(args.config) submit = comp.paths.condor_scratch_dir + '/ks_test_{}/submit'.format(args.config) # Define path to executables make_maps_ex = os.path.join(comp.paths.project_home, 'processing/anisotropy/ks_test_multipart', 'make_maps.py') merge_maps_ex = os.path.join(comp.paths.project_home, 'processing/anisotropy/ks_test_multipart', 'merge_maps.py') save_pvals_ex = os.path.join(comp.paths.project_home, 'processing/anisotropy/ks_test_multipart', 'save_pvals.py') # Create Dagman instance dag_name = 'anisotropy_kstest_{}'.format(args.config) if args.test: dag_name += '_test' dagman = pycondor.Dagman(dag_name, submit=submit, verbose=1) # Create Job for saving ks-test p-values for each trial save_pvals_name = 'save_pvals_{}'.format(args.config) if args.low_energy: save_pvals_name += '_lowenergy' save_pvals_job = pycondor.Job(save_pvals_name, save_pvals_ex, error=error, output=output, log=log, submit=submit, verbose=1) save_pvals_infiles_0 = [] save_pvals_infiles_1 = [] dagman.add_job(save_pvals_job) outdir = os.path.join(comp.paths.comp_data_dir, args.config + '_data', 'anisotropy', 'random_splits') if args.test: outdir = os.path.join(outdir, 'test') for trial_num in range(args.ks_trials): # Create map_maps jobs for this ks_trial make_maps_name = 'make_maps_{}_trial-{}'.format(args.config, trial_num) if args.low_energy: make_maps_name += '_lowenergy' make_maps_job = pycondor.Job(make_maps_name, make_maps_ex, error=error, output=output, log=log, submit=submit, verbose=1) dagman.add_job(make_maps_job) merge_maps_infiles_0 = [] merge_maps_infiles_1 = [] for batch_idx in range(args.n_batches): if args.test and batch_idx > 2: break outfile_sample_1 = os.path.join(outdir, 'random_split_1_trial-{}_batch-{}.fits'.format(trial_num, batch_idx)) outfile_sample_0 = os.path.join(outdir, 'random_split_0_trial-{}_batch-{}.fits'.format(trial_num, batch_idx)) make_maps_arg_list = [] make_maps_arg_list.append('--config {}'.format(args.config)) make_maps_arg_list.append('--n_side {}'.format(args.n_side)) make_maps_arg_list.append('--chunksize {}'.format(args.chunksize)) make_maps_arg_list.append('--n_batches {}'.format(args.n_batches)) make_maps_arg_list.append('--batch_idx {}'.format(batch_idx)) make_maps_arg_list.append('--outfile_sample_0 {}'.format(outfile_sample_0)) make_maps_arg_list.append('--outfile_sample_1 {}'.format(outfile_sample_1)) make_maps_arg = ' '.join(make_maps_arg_list) if args.low_energy: make_maps_arg += ' --low_energy' make_maps_job.add_arg(make_maps_arg) # Add this outfile to the list of infiles for merge_maps_job merge_maps_infiles_0.append(outfile_sample_0) merge_maps_infiles_1.append(outfile_sample_1) for sample_idx, input_file_list in enumerate([merge_maps_infiles_0, merge_maps_infiles_1]): merge_maps_name = 'merge_maps_{}_trial-{}_split-{}'.format(args.config, trial_num, sample_idx) if args.low_energy: merge_maps_name += '_lowenergy' merge_maps_job = pycondor.Job(merge_maps_name, merge_maps_ex, error=error, output=output, log=log, submit=submit, verbose=1) # Ensure that make_maps_job completes before merge_maps_job begins make_maps_job.add_child(merge_maps_job) merge_maps_job.add_child(save_pvals_job) dagman.add_job(merge_maps_job) merge_infiles_str = ' '.join(input_file_list) # Assemble merged output file path merge_outfile = os.path.join(outdir, 'random_split_{}_trial-{}.fits'.format(sample_idx, trial_num)) merge_maps_arg = '--infiles {} --outfile {}'.format(merge_infiles_str, merge_outfile) merge_maps_job.add_arg(merge_maps_arg) if sample_idx == 0: save_pvals_infiles_0.append(merge_outfile) else: save_pvals_infiles_1.append(merge_outfile) save_pvals_infiles_0_str = ' '.join(save_pvals_infiles_0) save_pvals_infiles_1_str = ' '.join(save_pvals_infiles_1) if args.low_energy: outfile_basename = 'ks_test_dataframe_lowenergy.hdf' else: outfile_basename = 'ks_test_dataframe.hdf' outfile = os.path.join(outdir, outfile_basename) save_pvals_arg = '--infiles_sample_0 {} --infiles_sample_1 {} ' \ '--outfile {}'.format(save_pvals_infiles_0_str, save_pvals_infiles_1_str, outfile) save_pvals_job.add_arg(save_pvals_arg) dagman.build_submit(fancyname=True)
mit
Rocamadour7/ml_tutorial
05. Clustering/titanic-data-example.py
1
1721
import numpy as np from sklearn.cluster import KMeans from sklearn import preprocessing import pandas as pd ''' Pclass Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd) survival Survival (0 = No; 1 = Yes) name Name sex Sex age Age sibsp Number of Siblings/Spouses Aboard parch Number of Parents/Children Aboard ticket Ticket Number fare Passenger Fare (British pound) cabin Cabin embarked Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton) boat Lifeboat body Body Identification Number home.dest Home/Destination ''' df = pd.read_excel('titanic.xls') df.drop(['body', 'name'], 1, inplace=True) df.fillna(0, inplace=True) def handle_non_numerical_data(df): columns = df.columns.values for column in columns: text_digit_vals = {} def convert_to_int(val): return text_digit_vals[val] if df[column].dtype != np.int64 and df[column].dtype != np.float64: column_contents = df[column].values.tolist() unique_elements = set(column_contents) x = 0 for unique in unique_elements: if unique not in text_digit_vals: text_digit_vals[unique] = x x += 1 df[column] = list(map(convert_to_int, df[column])) return df df = handle_non_numerical_data(df) X = np.array(df.drop(['survived'], 1).astype(float)) X = preprocessing.scale(X) y = np.array(df['survived']) clf = KMeans(n_clusters=2) clf.fit(X) correct = 0 for i in range(len(X)): predict_me = np.array(X[i].astype(float)) predict_me = predict_me.reshape(-1, len(predict_me)) prediction = clf.predict(predict_me) if prediction[0] == y[i]: correct += 1 print(correct/len(X))
mit
moreati/pandashells
pandashells/lib/arg_lib.py
7
6681
from pandashells.lib import config_lib def _check_for_recognized_args(*args): """ Raise an error if unrecognized argset is specified """ allowed_arg_set = set([ 'io_in', 'io_out', 'example', 'xy_plotting', 'decorating', ]) in_arg_set = set(args) unrecognized_set = in_arg_set - allowed_arg_set if unrecognized_set: msg = '{} not in allowed set {}'.format(unrecognized_set, allowed_arg_set) raise ValueError(msg) def _io_in_adder(parser, config_dict, *args): """ Add input options to the parser """ in_arg_set = set(args) if 'io_in' in in_arg_set: group = parser.add_argument_group('Input Options') # define the valid components io_opt_list = ['csv', 'table', 'header', 'noheader'] # allow the option of supplying input column names msg = 'Overwrite input column names with this list' group.add_argument( '--names', nargs='+', type=str, dest='names', metavar="name", help=msg) default_for_input = [ config_dict['io_input_type'], config_dict['io_input_header'] ] msg = 'Must be one of {}'.format(repr(io_opt_list)) group.add_argument( '-i', '--input_options', nargs='+', type=str, dest='input_options', metavar='option', default=default_for_input, choices=io_opt_list, help=msg) def _io_out_adder(parser, config_dict, *args): """ Add output options to the parser """ in_arg_set = set(args) if 'io_out' in in_arg_set: group = parser.add_argument_group('Output Options') # define the valid components io_opt_list = [ 'csv', 'table', 'html', 'header', 'noheader', 'index', 'noindex', ] # define the current defaults default_for_output = [ config_dict['io_output_type'], config_dict['io_output_header'], config_dict['io_output_index'] ] # show the current defaults in the arg parser msg = 'Must be one of {}'.format(repr(io_opt_list)) group.add_argument( '-o', '--output_options', nargs='+', type=str, dest='output_options', metavar='option', default=default_for_output, help=msg) msg = ( 'Replace NaNs with this string. ' 'A string containing \'nan\' will set na_rep to numpy NaN. ' 'Current default is {}' ).format(repr(str(config_dict['io_output_na_rep']))) group.add_argument( '--output_na_rep', nargs=1, type=str, dest='io_output_na_rep', help=msg) def _decorating_adder(parser, *args): in_arg_set = set(args) if 'decorating' in in_arg_set: # get a list of valid plot styling info context_list = [t for t in config_lib.CONFIG_OPTS if t[0] == 'plot_context'][0][1] theme_list = [t for t in config_lib.CONFIG_OPTS if t[0] == 'plot_theme'][0][1] palette_list = [t for t in config_lib.CONFIG_OPTS if t[0] == 'plot_palette'][0][1] group = parser.add_argument_group('Plot specific Options') msg = "Set the x-limits for the plot" group.add_argument( '--xlim', nargs=2, type=float, dest='xlim', metavar=('XMIN', 'XMAX'), help=msg) msg = "Set the y-limits for the plot" group.add_argument( '--ylim', nargs=2, type=float, dest='ylim', metavar=('YMIN', 'YMAX'), help=msg) msg = "Draw x axis with log scale" group.add_argument( '--xlog', action='store_true', dest='xlog', default=False, help=msg) msg = "Draw y axis with log scale" group.add_argument( '--ylog', action='store_true', dest='ylog', default=False, help=msg) msg = "Set the x-label for the plot" group.add_argument( '--xlabel', nargs=1, type=str, dest='xlabel', help=msg) msg = "Set the y-label for the plot" group.add_argument( '--ylabel', nargs=1, type=str, dest='ylabel', help=msg) msg = "Set the title for the plot" group.add_argument( '--title', nargs=1, type=str, dest='title', help=msg) msg = "Specify legend location" group.add_argument( '--legend', nargs=1, type=str, dest='legend', choices=['1', '2', '3', '4', 'best'], help=msg) msg = "Specify whether hide the grid or not" group.add_argument( '--nogrid', action='store_true', dest='no_grid', default=False, help=msg) msg = "Specify plot context. Default = '{}' ".format(context_list[0]) group.add_argument( '--context', nargs=1, type=str, dest='plot_context', default=[context_list[0]], choices=context_list, help=msg) msg = "Specify plot theme. Default = '{}' ".format(theme_list[0]) group.add_argument( '--theme', nargs=1, type=str, dest='plot_theme', default=[theme_list[0]], choices=theme_list, help=msg) msg = "Specify plot palette. Default = '{}' ".format(palette_list[0]) group.add_argument( '--palette', nargs=1, type=str, dest='plot_palette', default=[palette_list[0]], choices=palette_list, help=msg) msg = "Save the figure to this file" group.add_argument('--savefig', nargs=1, type=str, help=msg) def _xy_adder(parser, *args): in_arg_set = set(args) if 'xy_plotting' in in_arg_set: msg = 'Column to plot on x-axis' parser.add_argument( '-x', nargs=1, type=str, dest='x', metavar='col', help=msg) msg = 'List of columns to plot on y-axis' parser.add_argument( '-y', nargs='+', type=str, dest='y', metavar='col', help=msg) msg = "Plot style(s) defaults to .-" parser.add_argument( '-s', '--style', nargs='+', type=str, dest='style', default=['.-'], help=msg, metavar='style') def add_args(parser, *args): """Adds argument blocks to the arg parser :type parser: argparse instance :param parser: The argarse instance to use in adding arguments Additinional arguments are the names of argument blocks to add """ config_dict = config_lib.get_config() _check_for_recognized_args(*args) _io_in_adder(parser, config_dict, *args) _io_out_adder(parser, config_dict, *args) _decorating_adder(parser, *args) _xy_adder(parser, *args)
bsd-2-clause
cpcloud/ibis
ibis/pandas/execution/tests/test_join.py
1
13150
import pandas as pd import pandas.util.testing as tm import pytest from pytest import param import ibis import ibis.common.exceptions as com pytestmark = pytest.mark.pandas join_type = pytest.mark.parametrize( 'how', [ 'inner', 'left', 'right', 'outer', param( 'semi', marks=pytest.mark.xfail( raises=NotImplementedError, reason='Semi join not implemented' ), ), param( 'anti', marks=pytest.mark.xfail( raises=NotImplementedError, reason='Anti join not implemented' ), ), ], ) @join_type def test_join(how, left, right, df1, df2): expr = left.join(right, left.key == right.key, how=how)[ left, right.other_value, right.key3 ] result = expr.execute() expected = pd.merge(df1, df2, how=how, on='key') tm.assert_frame_equal(result[expected.columns], expected) def test_cross_join(left, right, df1, df2): expr = left.cross_join(right)[left, right.other_value, right.key3] result = expr.execute() expected = pd.merge( df1.assign(dummy=1), df2.assign(dummy=1), how='inner', on='dummy' ).rename(columns=dict(key_x='key')) del expected['dummy'], expected['key_y'] tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_project_left_table(how, left, right, df1, df2): expr = left.join(right, left.key == right.key, how=how)[left, right.key3] result = expr.execute() expected = pd.merge(df1, df2, how=how, on='key')[ list(left.columns) + ['key3'] ] tm.assert_frame_equal(result[expected.columns], expected) def test_cross_join_project_left_table(left, right, df1, df2): expr = left.cross_join(right)[left, right.key3] result = expr.execute() expected = pd.merge( df1.assign(dummy=1), df2.assign(dummy=1), how='inner', on='dummy' ).rename(columns=dict(key_x='key'))[list(left.columns) + ['key3']] tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_multiple_predicates(how, left, right, df1, df2): expr = left.join( right, [left.key == right.key, left.key2 == right.key3], how=how )[left, right.key3, right.other_value] result = expr.execute() expected = pd.merge( df1, df2, how=how, left_on=['key', 'key2'], right_on=['key', 'key3'] ).reset_index(drop=True) tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_multiple_predicates_written_as_one( how, left, right, df1, df2 ): predicate = (left.key == right.key) & (left.key2 == right.key3) expr = left.join(right, predicate, how=how)[ left, right.key3, right.other_value ] result = expr.execute() expected = pd.merge( df1, df2, how=how, left_on=['key', 'key2'], right_on=['key', 'key3'] ).reset_index(drop=True) tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_invalid_predicates(how, left, right): predicate = (left.key == right.key) & (left.key2 <= right.key3) expr = left.join(right, predicate, how=how) with pytest.raises(TypeError): expr.execute() predicate = left.key >= right.key expr = left.join(right, predicate, how=how) with pytest.raises(TypeError): expr.execute() @join_type @pytest.mark.xfail(reason='Hard to detect this case') def test_join_with_duplicate_non_key_columns(how, left, right, df1, df2): left = left.mutate(x=left.value * 2) right = right.mutate(x=right.other_value * 3) expr = left.join(right, left.key == right.key, how=how) # This is undefined behavior because `x` is duplicated. This is difficult # to detect with pytest.raises(ValueError): expr.execute() @join_type def test_join_with_duplicate_non_key_columns_not_selected( how, left, right, df1, df2 ): left = left.mutate(x=left.value * 2) right = right.mutate(x=right.other_value * 3) right = right[['key', 'other_value']] expr = left.join(right, left.key == right.key, how=how)[ left, right.other_value ] result = expr.execute() expected = pd.merge( df1.assign(x=df1.value * 2), df2[['key', 'other_value']], how=how, on='key', ) tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_post_expression_selection(how, left, right, df1, df2): join = left.join(right, left.key == right.key, how=how) expr = join[left.key, left.value, right.other_value] result = expr.execute() expected = pd.merge(df1, df2, on='key', how=how)[ ['key', 'value', 'other_value'] ] tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_post_expression_filter(how, left): lhs = left[['key', 'key2']] rhs = left[['key2', 'value']] joined = lhs.join(rhs, 'key2', how=how) projected = joined[lhs, rhs.value] expr = projected[projected.value == 4] result = expr.execute() df1 = lhs.execute() df2 = rhs.execute() expected = pd.merge(df1, df2, on='key2', how=how) expected = expected.loc[expected.value == 4].reset_index(drop=True) tm.assert_frame_equal(result, expected) @join_type def test_multi_join_with_post_expression_filter(how, left, df1): lhs = left[['key', 'key2']] rhs = left[['key2', 'value']] rhs2 = left[['key2', 'value']].relabel(dict(value='value2')) joined = lhs.join(rhs, 'key2', how=how) projected = joined[lhs, rhs.value] filtered = projected[projected.value == 4] joined2 = filtered.join(rhs2, 'key2') projected2 = joined2[filtered.key, rhs2.value2] expr = projected2[projected2.value2 == 3] result = expr.execute() df1 = lhs.execute() df2 = rhs.execute() df3 = rhs2.execute() expected = pd.merge(df1, df2, on='key2', how=how) expected = expected.loc[expected.value == 4].reset_index(drop=True) expected = pd.merge(expected, df3, on='key2')[['key', 'value2']] expected = expected.loc[expected.value2 == 3].reset_index(drop=True) tm.assert_frame_equal(result, expected) @join_type def test_join_with_non_trivial_key(how, left, right, df1, df2): # also test that the order of operands in the predicate doesn't matter join = left.join(right, right.key.length() == left.key.length(), how=how) expr = join[left.key, left.value, right.other_value] result = expr.execute() expected = ( pd.merge( df1.assign(key_len=df1.key.str.len()), df2.assign(key_len=df2.key.str.len()), on='key_len', how=how, ) .drop(['key_len', 'key_y', 'key2', 'key3'], axis=1) .rename(columns={'key_x': 'key'}) ) tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_non_trivial_key_project_table(how, left, right, df1, df2): # also test that the order of operands in the predicate doesn't matter join = left.join(right, right.key.length() == left.key.length(), how=how) expr = join[left, right.other_value] expr = expr[expr.key.length() == 1] result = expr.execute() expected = ( pd.merge( df1.assign(key_len=df1.key.str.len()), df2.assign(key_len=df2.key.str.len()), on='key_len', how=how, ) .drop(['key_len', 'key_y', 'key2', 'key3'], axis=1) .rename(columns={'key_x': 'key'}) ) expected = expected.loc[expected.key.str.len() == 1] tm.assert_frame_equal(result[expected.columns], expected) @join_type def test_join_with_project_right_duplicate_column(client, how, left, df1, df3): # also test that the order of operands in the predicate doesn't matter right = client.table('df3') join = left.join(right, ['key'], how=how) expr = join[left.key, right.key2, right.other_value] result = expr.execute() expected = ( pd.merge(df1, df3, on='key', how=how) .drop(['key2_x', 'key3', 'value'], axis=1) .rename(columns={'key2_y': 'key2'}) ) tm.assert_frame_equal(result[expected.columns], expected) def test_join_with_window_function( players_base, players_df, batting, batting_df ): players = players_base # this should be semi_join tbl = batting.left_join(players, ['playerID']) t = tbl[batting.G, batting.playerID, batting.teamID] expr = t.groupby(t.teamID).mutate( team_avg=lambda d: d.G.mean(), demeaned_by_player=lambda d: d.G - d.G.mean(), ) result = expr.execute() expected = pd.merge( batting_df, players_df[['playerID']], on='playerID', how='left' )[['G', 'playerID', 'teamID']] team_avg = expected.groupby('teamID').G.transform('mean') expected = expected.assign( team_avg=team_avg, demeaned_by_player=lambda df: df.G - team_avg ) tm.assert_frame_equal(result[expected.columns], expected) merge_asof_minversion = pytest.mark.skipif( pd.__version__ < '0.19.2', reason="at least pandas-0.19.2 required for merge_asof", ) @merge_asof_minversion def test_asof_join(time_left, time_right, time_df1, time_df2): expr = time_left.asof_join(time_right, 'time')[ time_left, time_right.other_value ] result = expr.execute() expected = pd.merge_asof(time_df1, time_df2, on='time') tm.assert_frame_equal(result[expected.columns], expected) @merge_asof_minversion def test_asof_join_predicate(time_left, time_right, time_df1, time_df2): expr = time_left.asof_join(time_right, time_left.time == time_right.time)[ time_left, time_right.other_value ] result = expr.execute() expected = pd.merge_asof(time_df1, time_df2, on='time') tm.assert_frame_equal(result[expected.columns], expected) @merge_asof_minversion def test_keyed_asof_join( time_keyed_left, time_keyed_right, time_keyed_df1, time_keyed_df2 ): expr = time_keyed_left.asof_join(time_keyed_right, 'time', by='key')[ time_keyed_left, time_keyed_right.other_value ] result = expr.execute() expected = pd.merge_asof( time_keyed_df1, time_keyed_df2, on='time', by='key' ) tm.assert_frame_equal(result[expected.columns], expected) @merge_asof_minversion def test_keyed_asof_join_with_tolerance( time_keyed_left, time_keyed_right, time_keyed_df1, time_keyed_df2 ): expr = time_keyed_left.asof_join( time_keyed_right, 'time', by='key', tolerance=2 * ibis.interval(days=1) )[time_keyed_left, time_keyed_right.other_value] result = expr.execute() expected = pd.merge_asof( time_keyed_df1, time_keyed_df2, on='time', by='key', tolerance=pd.Timedelta('2D'), ) tm.assert_frame_equal(result[expected.columns], expected) @pytest.mark.parametrize( "how", [ "left", pytest.param( "right", marks=pytest.mark.xfail( raises=AttributeError, reason="right_join is not an ibis API" ), ), "inner", "outer", ], ) @pytest.mark.parametrize( "func", [ pytest.param(lambda join: join["a0", "a1"], id="tuple"), pytest.param(lambda join: join[["a0", "a1"]], id="list"), pytest.param(lambda join: join.select(["a0", "a1"]), id="select"), ], ) @pytest.mark.xfail( raises=(com.IbisError, AttributeError), reason="Select from unambiguous joins not implemented", ) def test_select_on_unambiguous_join(how, func): df_t = pd.DataFrame(dict(a0=[1, 2, 3], b1=list("aab"))) df_s = pd.DataFrame(dict(a1=[2, 3, 4], b2=list("abc"))) con = ibis.pandas.connect({"t": df_t, "s": df_s}) t = con.table("t") s = con.table("s") method = getattr(t, "{}_join".format(how)) join = method(s, t.b1 == s.b2) expected = pd.merge(df_t, df_s, left_on=["b1"], right_on=["b2"], how=how)[ ["a0", "a1"] ] assert not expected.empty expr = func(join) result = expr.execute() tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "func", [ pytest.param(lambda join: join["a0", "a1"], id="tuple"), pytest.param(lambda join: join[["a0", "a1"]], id="list"), pytest.param(lambda join: join.select(["a0", "a1"]), id="select"), ], ) @pytest.mark.xfail( raises=(com.IbisError, AttributeError), reason="Select from unambiguous joins not implemented", ) @merge_asof_minversion def test_select_on_unambiguous_asof_join(func): df_t = pd.DataFrame( dict(a0=[1, 2, 3], b1=pd.date_range("20180101", periods=3)) ) df_s = pd.DataFrame( dict(a1=[2, 3, 4], b2=pd.date_range("20171230", periods=3)) ) con = ibis.pandas.connect({"t": df_t, "s": df_s}) t = con.table("t") s = con.table("s") join = t.asof_join(s, t.b1 == s.b2) expected = pd.merge_asof(df_t, df_s, left_on=["b1"], right_on=["b2"])[ ["a0", "a1"] ] assert not expected.empty expr = func(join) result = expr.execute() tm.assert_frame_equal(result, expected)
apache-2.0
BiaDarkia/scikit-learn
examples/semi_supervised/plot_label_propagation_digits_active_learning.py
33
4174
""" ======================================== Label Propagation digits active learning ======================================== Demonstrates an active learning technique to learn handwritten digits using label propagation. We start by training a label propagation model with only 10 labeled points, then we select the top five most uncertain points to label. Next, we train with 15 labeled points (original 10 + 5 new ones). We repeat this process four times to have a model trained with 30 labeled examples. Note you can increase this to label more than 30 by changing `max_iterations`. Labeling more than 30 can be useful to get a sense for the speed of convergence of this active learning technique. A plot will appear showing the top 5 most uncertain digits for each iteration of training. These may or may not contain mistakes, but we will train the next model with their true labels. """ print(__doc__) # Authors: Clay Woolam <clay@woolam.org> # License: BSD import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn import datasets from sklearn.semi_supervised import label_propagation from sklearn.metrics import classification_report, confusion_matrix digits = datasets.load_digits() rng = np.random.RandomState(0) indices = np.arange(len(digits.data)) rng.shuffle(indices) X = digits.data[indices[:330]] y = digits.target[indices[:330]] images = digits.images[indices[:330]] n_total_samples = len(y) n_labeled_points = 10 max_iterations = 5 unlabeled_indices = np.arange(n_total_samples)[n_labeled_points:] f = plt.figure() for i in range(max_iterations): if len(unlabeled_indices) == 0: print("No unlabeled items left to label.") break y_train = np.copy(y) y_train[unlabeled_indices] = -1 lp_model = label_propagation.LabelSpreading(gamma=0.25, max_iter=5) lp_model.fit(X, y_train) predicted_labels = lp_model.transduction_[unlabeled_indices] true_labels = y[unlabeled_indices] cm = confusion_matrix(true_labels, predicted_labels, labels=lp_model.classes_) print("Iteration %i %s" % (i, 70 * "_")) print("Label Spreading model: %d labeled & %d unlabeled (%d total)" % (n_labeled_points, n_total_samples - n_labeled_points, n_total_samples)) print(classification_report(true_labels, predicted_labels)) print("Confusion matrix") print(cm) # compute the entropies of transduced label distributions pred_entropies = stats.distributions.entropy( lp_model.label_distributions_.T) # select up to 5 digit examples that the classifier is most uncertain about uncertainty_index = np.argsort(pred_entropies)[::-1] uncertainty_index = uncertainty_index[ np.in1d(uncertainty_index, unlabeled_indices)][:5] # keep track of indices that we get labels for delete_indices = np.array([]) # for more than 5 iterations, visualize the gain only on the first 5 if i < 5: f.text(.05, (1 - (i + 1) * .183), "model %d\n\nfit with\n%d labels" % ((i + 1), i * 5 + 10), size=10) for index, image_index in enumerate(uncertainty_index): image = images[image_index] # for more than 5 iterations, visualize the gain only on the first 5 if i < 5: sub = f.add_subplot(5, 5, index + 1 + (5 * i)) sub.imshow(image, cmap=plt.cm.gray_r, interpolation='none') sub.set_title("predict: %i\ntrue: %i" % ( lp_model.transduction_[image_index], y[image_index]), size=10) sub.axis('off') # labeling 5 points, remote from labeled set delete_index, = np.where(unlabeled_indices == image_index) delete_indices = np.concatenate((delete_indices, delete_index)) unlabeled_indices = np.delete(unlabeled_indices, delete_indices) n_labeled_points += len(uncertainty_index) f.suptitle("Active learning with Label Propagation.\nRows show 5 most " "uncertain labels to learn with the next model.", y=1.15) plt.subplots_adjust(left=0.2, bottom=0.03, right=0.9, top=0.9, wspace=0.2, hspace=0.85) plt.show()
bsd-3-clause
liyi193328/seq2seq
seq2seq/contrib/learn/tests/dataframe/arithmetic_transform_test.py
62
2343
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for arithmetic transforms.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from tensorflow.contrib.learn.python.learn.dataframe import tensorflow_dataframe as df from tensorflow.python.platform import test # pylint: disable=g-import-not-at-top try: import pandas as pd HAS_PANDAS = True except ImportError: HAS_PANDAS = False class SumTestCase(test.TestCase): """Test class for `Sum` transform.""" def testSum(self): if not HAS_PANDAS: return num_rows = 100 pandas_df = pd.DataFrame({ "a": np.arange(num_rows), "b": np.arange(num_rows, 2 * num_rows) }) frame = df.TensorFlowDataFrame.from_pandas( pandas_df, shuffle=False, batch_size=num_rows) frame["a+b"] = frame["a"] + frame["b"] expected_sum = pandas_df["a"] + pandas_df["b"] actual_sum = frame.run_one_batch()["a+b"] np.testing.assert_array_equal(expected_sum, actual_sum) class DifferenceTestCase(test.TestCase): """Test class for `Difference` transform.""" def testDifference(self): if not HAS_PANDAS: return num_rows = 100 pandas_df = pd.DataFrame({ "a": np.arange(num_rows), "b": np.arange(num_rows, 2 * num_rows) }) frame = df.TensorFlowDataFrame.from_pandas( pandas_df, shuffle=False, batch_size=num_rows) frame["a-b"] = frame["a"] - frame["b"] expected_diff = pandas_df["a"] - pandas_df["b"] actual_diff = frame.run_one_batch()["a-b"] np.testing.assert_array_equal(expected_diff, actual_diff) if __name__ == "__main__": test.main()
apache-2.0
Tuyki/TT_RNN
MNISTSeq.py
1
14227
__author__ = "Yinchong Yang" __copyright__ = "Siemens AG, 2018" __licencse__ = "MIT" __version__ = "0.1" """ MIT License Copyright (c) 2018 Siemens AG Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ """ We first sample MNIST digits to form sequences of random lengths. The sequence is labeled as one if it contains a zero, and is labeled zero otherwise. This simulates a high dimensional sequence classification task, such as predicting therapy decision and survival of patients based on their historical clinical event information. We train plain LSTM and Tensor-Train LSTM for this task. After the training, we apply Layer-wise Relevance Propagation to identify the digit(s) that have influenced the classification. Apparently, we would expect the LRP algorithm would assign high relevance value to the zero(s) in the sequence. These experiments turn out to be successful, which demonstrates that i) the LSTM and TT-LSTM can indeed learn the mapping from a zero to the sequence class, and that ii) both LSTMs have no problem in storing the zero pattern over a period of time, because the classifier is deployed only at the last hidden state, and that iii) the implementation of the LRP algorithm, complex as it is, is also correct, in that the zeros are assigned high relevance scores. Especially the experiments with the plain LSTM serve as simulation study supporting our submission of “Yinchong Yang, Volker Tresp, Marius Wunderle, Peter A. Fasching, Explaining Therapy Predictions with Layer-wise Relevance Propagation in Neural Networks, at IEEE ICHI 2018”. The original LRP for LSTM from the repository: https://github.com/ArrasL/LRP_for_LSTM which we modified and adjusted for keras models. Feel free to experiment with the hyper parameters and suggest other sequence classification tasks. Have fun ;) """ import pickle import sys import numpy as np from numpy import newaxis as na import keras from keras.layers.recurrent import Recurrent from keras import backend as K from keras.engine import InputSpec from keras import activations from keras import initializers from keras import regularizers from keras import constraints from keras.engine.topology import Layer from TTLayer import * from TTRNN import TT_LSTM def make_seq(n, x, y, maxlen=32, seed=123): np.random.seed(seed) lens = np.random.choice(range(2, maxlen), n) seqs = np.zeros((n, maxlen, 28**2)) labels = np.zeros(n) digits_label = np.zeros((n, maxlen), dtype='int32')-1 ids = np.zeros((n, maxlen), dtype='int64')-1 for i in range(n): digits_inds = np.random.choice(range(x.shape[0]), lens[i]) ids[i, -lens[i]::] = digits_inds seqs[i, -lens[i]::, :] = x[digits_inds] digits_label[i, -lens[i]::] = y[digits_inds] class_inds = y[digits_inds] if True: # option 1: is there any 0 in the sequence? labels[i] = (0 in class_inds) else: # option 2: even number of 0 -> label=0, odd number of 0 -> label=1 labels[i] = len(np.where(class_inds == 0)[0]) % 2 == 1 return [seqs, labels, digits_label, ids] # From: https://github.com/ArrasL/LRP_for_LSTM def lrp_linear(hin, w, b, hout, Rout, bias_nb_units, eps, bias_factor, debug=False): """ LRP for a linear layer with input dim D and output dim M. Args: - hin: forward pass input, of shape (D,) - w: connection weights, of shape (D, M) - b: biases, of shape (M,) - hout: forward pass output, of shape (M,) (unequal to np.dot(w.T,hin)+b if more than one incoming layer!) - Rout: relevance at layer output, of shape (M,) - bias_nb_units: number of lower-layer units onto which the bias/stabilizer contribution is redistributed - eps: stabilizer (small positive number) - bias_factor: for global relevance conservation set to 1.0, otherwise 0.0 to ignore bias redistribution Returns: - Rin: relevance at layer input, of shape (D,) """ sign_out = np.where(hout[na, :] >= 0, 1., -1.) # shape (1, M) numer = (w * hin[:, na]) + \ ((bias_factor * b[na, :] * 1. + eps * sign_out * 1.) * 1. / bias_nb_units) # shape (D, M) denom = hout[na, :] + (eps * sign_out * 1.) # shape (1, M) message = (numer / denom) * Rout[na, :] # shape (D, M) Rin = message.sum(axis=1) # shape (D,) # Note: local layer relevance conservation if bias_factor==1.0 and bias_nb_units==D # global network relevance conservation if bias_factor==1.0 (can be used for sanity check) if debug: print("local diff: ", Rout.sum() - Rin.sum()) return Rin def sigmoid(x): x = x.astype('float128') return 1. / (1. + np.exp(-x)) # Modified from https://github.com/ArrasL/LRP_for_LSTM def lstm_lrp(l, d, train_data = True): if train_data: x_l = X_tr[l] y_l = Y_tr[l] z_l = Z_tr[l] # d_l = d_tr[l] else: x_l = X_te[l] y_l = Y_te[l] z_l = Z_te[l] # d_l = d_te[l] # calculate the FF pass in LSTM for every time step pre_gates = np.zeros((MAXLEN, d*4)) gates = np.zeros((MAXLEN, d * 4)) h = np.zeros((MAXLEN, d)) c = np.zeros((MAXLEN, d)) for t in range(MAXLEN): z = np.dot(x_l[t], Ws) if t > 0: z += np.dot(h[t-1], Us) z += b pre_gates[t] = z z0 = z[0:d] z1 = z[d:2*d] z2 = z[2*d:3*d] z3 = z[3 * d::] i = sigmoid(z0) f = sigmoid(z1) c[t] = f * c[t-1] + i * np.tanh(z2) o = sigmoid(z3) h[t] = o * np.tanh(c[t]) gates[t] = np.concatenate([i, f, np.tanh(z2), o]) # check: z_l[12] / h[-1][12] Rh = np.zeros((MAXLEN, d)) Rc = np.zeros((MAXLEN, d)) Rg = np.zeros((MAXLEN, d)) Rx = np.zeros((MAXLEN, 28**2)) bias_factor = 0 Rh[MAXLEN-1] = lrp_linear(hin=z_l, w=Dense_w, b=np.array(Dense_b), hout=np.dot(z_l, Dense_w)+Dense_b, Rout=np.array([y_l]), bias_nb_units=len(z_l), eps=eps, bias_factor=bias_factor) for t in reversed(range(MAXLEN)): # t = MAXLEN-1 # print t Rc[t] += Rh[t] # Rc[t] = Rh[t] if t > 0: Rc[t-1] = lrp_linear(gates[t, d: 2 * d] * c[t - 1], # gates[t , 2 *d: 3 *d ] *c[ t -1], np.identity(d), np.zeros((d)), c[t], Rc[t], 2*d, eps, bias_factor, debug=False) Rg[t] = lrp_linear(gates[t, 0:d] * gates[t, 2*d:3*d], # h_input: i + g np.identity(d), # W np.zeros((d)), # b c[t], # h_output Rc[t], # R_output 2 * d, eps, bias_factor, debug=False) # foo = np.dot(x_l[t], Ws[:,2*d:3*d]) + np.dot(h[t-1], Us[:, 2*d:3*d]) + b[2*d:3*d] Rx[t] = lrp_linear(x_l[t], Ws[:,2*d:3*d], b[2*d:3*d], pre_gates[t, 2*d:3*d], Rg[t], d + 28 ** 2, eps, bias_factor, debug=False) if t > 0: Rh[t-1] = lrp_linear(h[t-1], Us[:,2*d:3*d], b[2*d:3*d], pre_gates[t, 2 * d:3 * d], Rg[t], d + 28**2, eps, bias_factor, debug=False) # hin, w, b, hout, Rout, bias_nb_units, eps, bias_factor, debug=False # Rx[np.where(d_l==-1.)[0]] *= 0 return Rx from keras.datasets import mnist from keras.utils import to_categorical from keras.models import Model, Input from keras.layers import Dense, GRU, LSTM, Dropout, Masking from keras.optimizers import * from keras.regularizers import l2 from sklearn.metrics import * # Script configurations ################################################################### seed=111111 use_TT = True # whether use Tensor-Train or plain RNNs # Prepare the data ######################################################################## # Load the MNIST data and build sequences: (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(x_train.shape[0], -1) x_test = x_test.reshape(x_test.shape[0], -1) MAXLEN = 32 # max length of the sequences X_tr, Y_tr, d_tr, idx_tr = make_seq(n=10000, x=x_train, y=y_train, maxlen=MAXLEN, seed=seed) X_te, Y_te, d_te, idx_te = make_seq(n=1000, x=x_test, y=y_test, maxlen=MAXLEN, seed=seed+1) # Define the model ###################################################################### if use_TT: # TT settings tt_input_shape = [7, 7, 16] tt_output_shape = [4, 4, 4] tt_ranks = [1, 4, 4, 1] rnn_size = 64 X = Input(shape=X_tr.shape[1::]) X_mask = Masking(mask_value=0.0, input_shape=X_tr.shape[1::])(X) if use_TT: Z = TT_LSTM(tt_input_shape=tt_input_shape, tt_output_shape=tt_output_shape, tt_ranks=tt_ranks, return_sequences=False, recurrent_dropout=.5)(X_mask) Out = Dense(units=1, activation='sigmoid', kernel_regularizer=l2(1e-2))(Z) else: Z = LSTM(units=rnn_size, return_sequences=False, recurrent_dropout=.5)(X_mask) # dropout=.5, Out = Dense(units=1, activation='sigmoid', kernel_regularizer=l2(1e-2))(Z) rnn_model = Model(X, Out) rnn_model.compile(optimizer=Adam(1e-3), loss='binary_crossentropy', metrics=['accuracy']) # Train the model and save the results ###################################################### rnn_model.fit(X_tr, Y_tr, epochs=50, batch_size=32, validation_split=.2, verbose=2) Y_hat = rnn_model.predict(X_tr, verbose=2).reshape(-1) train_acc = (np.round(Y_hat) == Y_tr).mean() Y_pred = rnn_model.predict(X_te, verbose=2).reshape(-1) (np.round(Y_pred) == Y_te).mean() pred_acc = (np.round(Y_pred) == Y_te).mean() # Collect all hidden layers ################################################################ if use_TT: # Reconstruct the fully connected input-to-hidden weights: from keras.initializers import constant _tt_output_shape = np.copy(tt_output_shape) _tt_output_shape[0] *= 4 fc_w = rnn_model.get_weights()[0] fc_layer = TT_Layer(tt_input_shape=tt_input_shape, tt_output_shape=_tt_output_shape, tt_ranks=tt_ranks, kernel_initializer=constant(value=fc_w), use_bias=False) fc_input = Input(shape=(X_tr.shape[2],)) fc_output = fc_layer(fc_input) fc_model = Model(fc_input, fc_output) fc_model.compile('sgd', 'mse') fc_recon_mat = fc_model.predict(np.identity(X_tr.shape[2])) # Reconstruct the entire LSTM: fc_Z = LSTM(units=np.prod(tt_output_shape), return_sequences=False, dropout=.5, recurrent_dropout=.5, weights=[fc_recon_mat, rnn_model.get_weights()[2], rnn_model.get_weights()[1]])(X_mask) else: fc_Z = LSTM(units=rnn_size, return_sequences=False, dropout=.5, recurrent_dropout=.5, weights=rnn_model.get_weights()[0:3])(X_mask) fc_Out = Dense(units=1, activation='sigmoid', kernel_regularizer=l2(1e-3), weights=rnn_model.get_weights()[3::])(fc_Z) fc_rnn_model = Model(X, fc_Out) fc_rnn_model.compile(optimizer=Adam(1e-3), loss='binary_crossentropy', metrics=['accuracy']) fc_rnn_model.evaluate(X_te, Y_te, verbose=2) # Calculate the LRP: ######################################################################### fc_Z_model = Model(X, fc_Z) fc_Z_model.compile('sgd', 'mse') Y_hat_fc = fc_rnn_model.predict(X_tr) Y_pred_fc = fc_rnn_model.predict(X_te) Ws = fc_rnn_model.get_weights()[0] Us = fc_rnn_model.get_weights()[1] b = fc_rnn_model.get_weights()[2] Dense_w = fc_rnn_model.get_weights()[3] Dense_b = fc_rnn_model.get_weights()[4] Z_tr = fc_Z_model.predict(X_tr) Z_te = fc_Z_model.predict(X_te) eps = 1e-4 is_number_flag = np.where(d_te != -1) # All relevance scores of the test sequences lrp_te = np.vstack([lstm_lrp(i, rnn_size, False).sum(1) for i in range(X_te.shape[0])]) lrp_auroc = roc_auc_score((d_te == 0).astype('int')[is_number_flag].reshape(-1), lrp_te[is_number_flag].reshape(-1)) lrp_auprc = average_precision_score((d_te == 0).astype('int')[is_number_flag].reshape(-1), lrp_te[is_number_flag].reshape(-1)) # The reported results: print pred_acc print lrp_auroc print lrp_auprc
mit
Ziqi-Li/bknqgis
pandas/pandas/core/window.py
3
68731
""" provide a generic structure to support window functions, similar to how we have a Groupby object """ from __future__ import division import warnings import numpy as np from collections import defaultdict from datetime import timedelta from pandas.core.dtypes.generic import ( ABCSeries, ABCDataFrame, ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex, ABCDateOffset) from pandas.core.dtypes.common import ( is_integer, is_bool, is_float_dtype, is_integer_dtype, needs_i8_conversion, is_timedelta64_dtype, is_list_like, _ensure_float64, is_scalar) from pandas.core.base import (PandasObject, SelectionMixin, GroupByMixin) import pandas.core.common as com import pandas._libs.window as _window from pandas import compat from pandas.compat.numpy import function as nv from pandas.util._decorators import (Substitution, Appender, cache_readonly) from pandas.core.generic import _shared_docs from textwrap import dedent _shared_docs = dict(**_shared_docs) _doc_template = """ Returns ------- same type as input See also -------- pandas.Series.%(name)s pandas.DataFrame.%(name)s """ class _Window(PandasObject, SelectionMixin): _attributes = ['window', 'min_periods', 'freq', 'center', 'win_type', 'axis', 'on', 'closed'] exclusions = set() def __init__(self, obj, window=None, min_periods=None, freq=None, center=False, win_type=None, axis=0, on=None, closed=None, **kwargs): if freq is not None: warnings.warn("The freq kw is deprecated and will be removed in a " "future version. You can resample prior to passing " "to a window function", FutureWarning, stacklevel=3) self.__dict__.update(kwargs) self.blocks = [] self.obj = obj self.on = on self.closed = closed self.window = window self.min_periods = min_periods self.freq = freq self.center = center self.win_type = win_type self.win_freq = None self.axis = obj._get_axis_number(axis) if axis is not None else None self.validate() @property def _constructor(self): return Window @property def is_datetimelike(self): return None @property def _on(self): return None @property def is_freq_type(self): return self.win_type == 'freq' def validate(self): if self.center is not None and not is_bool(self.center): raise ValueError("center must be a boolean") if self.min_periods is not None and not \ is_integer(self.min_periods): raise ValueError("min_periods must be an integer") if self.closed is not None and self.closed not in \ ['right', 'both', 'left', 'neither']: raise ValueError("closed must be 'right', 'left', 'both' or " "'neither'") def _convert_freq(self, how=None): """ resample according to the how, return a new object """ obj = self._selected_obj index = None if (self.freq is not None and isinstance(obj, (ABCSeries, ABCDataFrame))): if how is not None: warnings.warn("The how kw argument is deprecated and removed " "in a future version. You can resample prior " "to passing to a window function", FutureWarning, stacklevel=6) obj = obj.resample(self.freq).aggregate(how or 'asfreq') return obj, index def _create_blocks(self, how): """ split data into blocks & return conformed data """ obj, index = self._convert_freq(how) if index is not None: index = self._on # filter out the on from the object if self.on is not None: if obj.ndim == 2: obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False) blocks = obj.as_blocks(copy=False).values() return blocks, obj, index def _gotitem(self, key, ndim, subset=None): """ sub-classes to define return a sliced object Parameters ---------- key : string / list of selections ndim : 1,2 requested ndim of result subset : object, default None subset to act on """ # create a new object to prevent aliasing if subset is None: subset = self.obj self = self._shallow_copy(subset) self._reset_cache() if subset.ndim == 2: if is_scalar(key) and key in subset or is_list_like(key): self._selection = key return self def __getattr__(self, attr): if attr in self._internal_names_set: return object.__getattribute__(self, attr) if attr in self.obj: return self[attr] raise AttributeError("%r object has no attribute %r" % (type(self).__name__, attr)) def _dir_additions(self): return self.obj._dir_additions() def _get_window(self, other=None): return self.window @property def _window_type(self): return self.__class__.__name__ def __unicode__(self): """ provide a nice str repr of our rolling object """ attrs = ["{k}={v}".format(k=k, v=getattr(self, k)) for k in self._attributes if getattr(self, k, None) is not None] return "{klass} [{attrs}]".format(klass=self._window_type, attrs=','.join(attrs)) def _get_index(self, index=None): """ Return index as ndarrays Returns ------- tuple of (index, index_as_ndarray) """ if self.is_freq_type: if index is None: index = self._on return index, index.asi8 return index, index def _prep_values(self, values=None, kill_inf=True, how=None): if values is None: values = getattr(self._selected_obj, 'values', self._selected_obj) # GH #12373 : rolling functions error on float32 data # make sure the data is coerced to float64 if is_float_dtype(values.dtype): values = _ensure_float64(values) elif is_integer_dtype(values.dtype): values = _ensure_float64(values) elif needs_i8_conversion(values.dtype): raise NotImplementedError("ops for {action} for this " "dtype {dtype} are not " "implemented".format( action=self._window_type, dtype=values.dtype)) else: try: values = _ensure_float64(values) except (ValueError, TypeError): raise TypeError("cannot handle this type -> {0}" "".format(values.dtype)) if kill_inf: values = values.copy() values[np.isinf(values)] = np.NaN return values def _wrap_result(self, result, block=None, obj=None): """ wrap a single result """ if obj is None: obj = self._selected_obj index = obj.index if isinstance(result, np.ndarray): # coerce if necessary if block is not None: if is_timedelta64_dtype(block.values.dtype): from pandas import to_timedelta result = to_timedelta( result.ravel(), unit='ns').values.reshape(result.shape) if result.ndim == 1: from pandas import Series return Series(result, index, name=obj.name) return type(obj)(result, index=index, columns=block.columns) return result def _wrap_results(self, results, blocks, obj): """ wrap the results Paramters --------- results : list of ndarrays blocks : list of blocks obj : conformed data (may be resampled) """ from pandas import Series, concat from pandas.core.index import _ensure_index final = [] for result, block in zip(results, blocks): result = self._wrap_result(result, block=block, obj=obj) if result.ndim == 1: return result final.append(result) # if we have an 'on' column # we want to put it back into the results # in the same location columns = self._selected_obj.columns if self.on is not None and not self._on.equals(obj.index): name = self._on.name final.append(Series(self._on, index=obj.index, name=name)) if self._selection is not None: selection = _ensure_index(self._selection) # need to reorder to include original location of # the on column (if its not already there) if name not in selection: columns = self.obj.columns indexer = columns.get_indexer(selection.tolist() + [name]) columns = columns.take(sorted(indexer)) if not len(final): return obj.astype('float64') return concat(final, axis=1).reindex(columns=columns, copy=False) def _center_window(self, result, window): """ center the result in the window """ if self.axis > result.ndim - 1: raise ValueError("Requested axis is larger then no. of argument " "dimensions") offset = _offset(window, True) if offset > 0: if isinstance(result, (ABCSeries, ABCDataFrame)): result = result.slice_shift(-offset, axis=self.axis) else: lead_indexer = [slice(None)] * result.ndim lead_indexer[self.axis] = slice(offset, None) result = np.copy(result[tuple(lead_indexer)]) return result def aggregate(self, arg, *args, **kwargs): result, how = self._aggregate(arg, *args, **kwargs) if result is None: return self.apply(arg, args=args, kwargs=kwargs) return result agg = aggregate _shared_docs['sum'] = dedent(""" %(name)s sum Parameters ---------- how : string, default None .. deprecated:: 0.18.0 Method for down- or re-sampling""") _shared_docs['mean'] = dedent(""" %(name)s mean Parameters ---------- how : string, default None .. deprecated:: 0.18.0 Method for down- or re-sampling""") class Window(_Window): """ Provides rolling window calculations. .. versionadded:: 0.18.0 Parameters ---------- window : int, or offset Size of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size. If its an offset then this will be the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes. This is new in 0.19.0 min_periods : int, default None Minimum number of observations in window required to have a value (otherwise result is NA). For a window that is specified by an offset, this will default to 1. freq : string or DateOffset object, optional (default None) .. deprecated:: 0.18.0 Frequency to conform the data to before computing the statistic. Specified as a frequency string or DateOffset object. center : boolean, default False Set the labels at the center of the window. win_type : string, default None Provide a window type. See the notes below. on : string, optional For a DataFrame, column on which to calculate the rolling window, rather than the index closed : string, default None Make the interval closed on the 'right', 'left', 'both' or 'neither' endpoints. For offset-based windows, it defaults to 'right'. For fixed windows, defaults to 'both'. Remaining cases not implemented for fixed windows. .. versionadded:: 0.20.0 axis : int or string, default 0 Returns ------- a Window or Rolling sub-classed for the particular operation Examples -------- >>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]}) >>> df B 0 0.0 1 1.0 2 2.0 3 NaN 4 4.0 Rolling sum with a window length of 2, using the 'triang' window type. >>> df.rolling(2, win_type='triang').sum() B 0 NaN 1 1.0 2 2.5 3 NaN 4 NaN Rolling sum with a window length of 2, min_periods defaults to the window length. >>> df.rolling(2).sum() B 0 NaN 1 1.0 2 3.0 3 NaN 4 NaN Same as above, but explicity set the min_periods >>> df.rolling(2, min_periods=1).sum() B 0 0.0 1 1.0 2 3.0 3 2.0 4 4.0 A ragged (meaning not-a-regular frequency), time-indexed DataFrame >>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]}, ....: index = [pd.Timestamp('20130101 09:00:00'), ....: pd.Timestamp('20130101 09:00:02'), ....: pd.Timestamp('20130101 09:00:03'), ....: pd.Timestamp('20130101 09:00:05'), ....: pd.Timestamp('20130101 09:00:06')]) >>> df B 2013-01-01 09:00:00 0.0 2013-01-01 09:00:02 1.0 2013-01-01 09:00:03 2.0 2013-01-01 09:00:05 NaN 2013-01-01 09:00:06 4.0 Contrasting to an integer rolling window, this will roll a variable length window corresponding to the time period. The default for min_periods is 1. >>> df.rolling('2s').sum() B 2013-01-01 09:00:00 0.0 2013-01-01 09:00:02 1.0 2013-01-01 09:00:03 3.0 2013-01-01 09:00:05 NaN 2013-01-01 09:00:06 4.0 Notes ----- By default, the result is set to the right edge of the window. This can be changed to the center of the window by setting ``center=True``. The `freq` keyword is used to conform time series data to a specified frequency by resampling the data. This is done with the default parameters of :meth:`~pandas.Series.resample` (i.e. using the `mean`). To learn more about the offsets & frequency strings, please see `this link <http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__. The recognized win_types are: * ``boxcar`` * ``triang`` * ``blackman`` * ``hamming`` * ``bartlett`` * ``parzen`` * ``bohman`` * ``blackmanharris`` * ``nuttall`` * ``barthann`` * ``kaiser`` (needs beta) * ``gaussian`` (needs std) * ``general_gaussian`` (needs power, width) * ``slepian`` (needs width). """ def validate(self): super(Window, self).validate() window = self.window if isinstance(window, (list, tuple, np.ndarray)): pass elif is_integer(window): if window < 0: raise ValueError("window must be non-negative") try: import scipy.signal as sig except ImportError: raise ImportError('Please install scipy to generate window ' 'weight') if not isinstance(self.win_type, compat.string_types): raise ValueError('Invalid win_type {0}'.format(self.win_type)) if getattr(sig, self.win_type, None) is None: raise ValueError('Invalid win_type {0}'.format(self.win_type)) else: raise ValueError('Invalid window {0}'.format(window)) def _prep_window(self, **kwargs): """ provide validation for our window type, return the window we have already been validated """ window = self._get_window() if isinstance(window, (list, tuple, np.ndarray)): return com._asarray_tuplesafe(window).astype(float) elif is_integer(window): import scipy.signal as sig # the below may pop from kwargs def _validate_win_type(win_type, kwargs): arg_map = {'kaiser': ['beta'], 'gaussian': ['std'], 'general_gaussian': ['power', 'width'], 'slepian': ['width']} if win_type in arg_map: return tuple([win_type] + _pop_args(win_type, arg_map[win_type], kwargs)) return win_type def _pop_args(win_type, arg_names, kwargs): msg = '%s window requires %%s' % win_type all_args = [] for n in arg_names: if n not in kwargs: raise ValueError(msg % n) all_args.append(kwargs.pop(n)) return all_args win_type = _validate_win_type(self.win_type, kwargs) # GH #15662. `False` makes symmetric window, rather than periodic. return sig.get_window(win_type, window, False).astype(float) def _apply_window(self, mean=True, how=None, **kwargs): """ Applies a moving window of type ``window_type`` on the data. Parameters ---------- mean : boolean, default True If True computes weighted mean, else weighted sum how : string, default to None .. deprecated:: 0.18.0 how to resample Returns ------- y : type of input argument """ window = self._prep_window(**kwargs) center = self.center blocks, obj, index = self._create_blocks(how=how) results = [] for b in blocks: try: values = self._prep_values(b.values) except TypeError: results.append(b.values.copy()) continue if values.size == 0: results.append(values.copy()) continue offset = _offset(window, center) additional_nans = np.array([np.NaN] * offset) def f(arg, *args, **kwargs): minp = _use_window(self.min_periods, len(window)) return _window.roll_window(np.concatenate((arg, additional_nans)) if center else arg, window, minp, avg=mean) result = np.apply_along_axis(f, self.axis, values) if center: result = self._center_window(result, window) results.append(result) return self._wrap_results(results, blocks, obj) _agg_doc = dedent(""" Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C']) >>> df A B C 0 -2.385977 -0.102758 0.438822 1 -1.004295 0.905829 -0.954544 2 0.735167 -0.165272 -1.619346 3 -0.702657 -1.340923 -0.706334 4 -0.246845 0.211596 -0.901819 5 2.463718 3.157577 -1.380906 6 -1.142255 2.340594 -0.039875 7 1.396598 -1.647453 1.677227 8 -0.543425 1.761277 -0.220481 9 -0.640505 0.289374 -1.550670 >>> df.rolling(3, win_type='boxcar').agg('mean') A B C 0 NaN NaN NaN 1 NaN NaN NaN 2 -0.885035 0.212600 -0.711689 3 -0.323928 -0.200122 -1.093408 4 -0.071445 -0.431533 -1.075833 5 0.504739 0.676083 -0.996353 6 0.358206 1.903256 -0.774200 7 0.906020 1.283573 0.085482 8 -0.096361 0.818139 0.472290 9 0.070889 0.134399 -0.031308 See also -------- pandas.DataFrame.rolling.aggregate pandas.DataFrame.aggregate """) @Appender(_agg_doc) @Appender(_shared_docs['aggregate'] % dict( versionadded='', klass='Series/DataFrame')) def aggregate(self, arg, *args, **kwargs): result, how = self._aggregate(arg, *args, **kwargs) if result is None: # these must apply directly result = arg(self) return result agg = aggregate @Substitution(name='window') @Appender(_doc_template) @Appender(_shared_docs['sum']) def sum(self, *args, **kwargs): nv.validate_window_func('sum', args, kwargs) return self._apply_window(mean=False, **kwargs) @Substitution(name='window') @Appender(_doc_template) @Appender(_shared_docs['mean']) def mean(self, *args, **kwargs): nv.validate_window_func('mean', args, kwargs) return self._apply_window(mean=True, **kwargs) class _GroupByMixin(GroupByMixin): """ provide the groupby facilities """ def __init__(self, obj, *args, **kwargs): parent = kwargs.pop('parent', None) # noqa groupby = kwargs.pop('groupby', None) if groupby is None: groupby, obj = obj, obj.obj self._groupby = groupby self._groupby.mutated = True self._groupby.grouper.mutated = True super(GroupByMixin, self).__init__(obj, *args, **kwargs) count = GroupByMixin._dispatch('count') corr = GroupByMixin._dispatch('corr', other=None, pairwise=None) cov = GroupByMixin._dispatch('cov', other=None, pairwise=None) def _apply(self, func, name, window=None, center=None, check_minp=None, how=None, **kwargs): """ dispatch to apply; we are stripping all of the _apply kwargs and performing the original function call on the grouped object """ def f(x, name=name, *args): x = self._shallow_copy(x) if isinstance(name, compat.string_types): return getattr(x, name)(*args, **kwargs) return x.apply(name, *args, **kwargs) return self._groupby.apply(f) class _Rolling(_Window): @property def _constructor(self): return Rolling def _apply(self, func, name=None, window=None, center=None, check_minp=None, how=None, **kwargs): """ Rolling statistical measure using supplied function. Designed to be used with passed-in Cython array-based functions. Parameters ---------- func : string/callable to apply name : string, optional name of this function window : int/array, default to _get_window() center : boolean, default to self.center check_minp : function, default to _use_window how : string, default to None .. deprecated:: 0.18.0 how to resample Returns ------- y : type of input """ if center is None: center = self.center if window is None: window = self._get_window() if check_minp is None: check_minp = _use_window blocks, obj, index = self._create_blocks(how=how) index, indexi = self._get_index(index=index) results = [] for b in blocks: try: values = self._prep_values(b.values) except TypeError: results.append(b.values.copy()) continue if values.size == 0: results.append(values.copy()) continue # if we have a string function name, wrap it if isinstance(func, compat.string_types): cfunc = getattr(_window, func, None) if cfunc is None: raise ValueError("we do not support this function " "in _window.{0}".format(func)) def func(arg, window, min_periods=None, closed=None): minp = check_minp(min_periods, window) # ensure we are only rolling on floats arg = _ensure_float64(arg) return cfunc(arg, window, minp, indexi, closed, **kwargs) # calculation function if center: offset = _offset(window, center) additional_nans = np.array([np.NaN] * offset) def calc(x): return func(np.concatenate((x, additional_nans)), window, min_periods=self.min_periods, closed=self.closed) else: def calc(x): return func(x, window, min_periods=self.min_periods, closed=self.closed) with np.errstate(all='ignore'): if values.ndim > 1: result = np.apply_along_axis(calc, self.axis, values) else: result = calc(values) if center: result = self._center_window(result, window) results.append(result) return self._wrap_results(results, blocks, obj) class _Rolling_and_Expanding(_Rolling): _shared_docs['count'] = """%(name)s count of number of non-NaN observations inside provided window.""" def count(self): blocks, obj, index = self._create_blocks(how=None) index, indexi = self._get_index(index=index) window = self._get_window() window = min(window, len(obj)) if not self.center else window results = [] for b in blocks: result = b.notna().astype(int) result = self._constructor(result, window=window, min_periods=0, center=self.center, closed=self.closed).sum() results.append(result) return self._wrap_results(results, blocks, obj) _shared_docs['apply'] = dedent(r""" %(name)s function apply Parameters ---------- func : function Must produce a single value from an ndarray input \*args and \*\*kwargs are passed to the function""") def apply(self, func, args=(), kwargs={}): # TODO: _level is unused? _level = kwargs.pop('_level', None) # noqa window = self._get_window() offset = _offset(window, self.center) index, indexi = self._get_index() def f(arg, window, min_periods, closed): minp = _use_window(min_periods, window) return _window.roll_generic(arg, window, minp, indexi, closed, offset, func, args, kwargs) return self._apply(f, func, args=args, kwargs=kwargs, center=False) def sum(self, *args, **kwargs): nv.validate_window_func('sum', args, kwargs) return self._apply('roll_sum', 'sum', **kwargs) _shared_docs['max'] = dedent(""" %(name)s maximum Parameters ---------- how : string, default 'max' .. deprecated:: 0.18.0 Method for down- or re-sampling""") def max(self, how=None, *args, **kwargs): nv.validate_window_func('max', args, kwargs) if self.freq is not None and how is None: how = 'max' return self._apply('roll_max', 'max', how=how, **kwargs) _shared_docs['min'] = dedent(""" %(name)s minimum Parameters ---------- how : string, default 'min' .. deprecated:: 0.18.0 Method for down- or re-sampling""") def min(self, how=None, *args, **kwargs): nv.validate_window_func('min', args, kwargs) if self.freq is not None and how is None: how = 'min' return self._apply('roll_min', 'min', how=how, **kwargs) def mean(self, *args, **kwargs): nv.validate_window_func('mean', args, kwargs) return self._apply('roll_mean', 'mean', **kwargs) _shared_docs['median'] = dedent(""" %(name)s median Parameters ---------- how : string, default 'median' .. deprecated:: 0.18.0 Method for down- or re-sampling""") def median(self, how=None, **kwargs): if self.freq is not None and how is None: how = 'median' return self._apply('roll_median_c', 'median', how=how, **kwargs) _shared_docs['std'] = dedent(""" %(name)s standard deviation Parameters ---------- ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is ``N - ddof``, where ``N`` represents the number of elements.""") def std(self, ddof=1, *args, **kwargs): nv.validate_window_func('std', args, kwargs) window = self._get_window() index, indexi = self._get_index() def f(arg, *args, **kwargs): minp = _require_min_periods(1)(self.min_periods, window) return _zsqrt(_window.roll_var(arg, window, minp, indexi, self.closed, ddof)) return self._apply(f, 'std', check_minp=_require_min_periods(1), ddof=ddof, **kwargs) _shared_docs['var'] = dedent(""" %(name)s variance Parameters ---------- ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is ``N - ddof``, where ``N`` represents the number of elements.""") def var(self, ddof=1, *args, **kwargs): nv.validate_window_func('var', args, kwargs) return self._apply('roll_var', 'var', check_minp=_require_min_periods(1), ddof=ddof, **kwargs) _shared_docs['skew'] = """Unbiased %(name)s skewness""" def skew(self, **kwargs): return self._apply('roll_skew', 'skew', check_minp=_require_min_periods(3), **kwargs) _shared_docs['kurt'] = """Unbiased %(name)s kurtosis""" def kurt(self, **kwargs): return self._apply('roll_kurt', 'kurt', check_minp=_require_min_periods(4), **kwargs) _shared_docs['quantile'] = dedent(""" %(name)s quantile Parameters ---------- quantile : float 0 <= quantile <= 1""") def quantile(self, quantile, **kwargs): window = self._get_window() index, indexi = self._get_index() def f(arg, *args, **kwargs): minp = _use_window(self.min_periods, window) if quantile == 1.0: return _window.roll_max(arg, window, minp, indexi, self.closed) elif quantile == 0.0: return _window.roll_min(arg, window, minp, indexi, self.closed) else: return _window.roll_quantile(arg, window, minp, indexi, self.closed, quantile) return self._apply(f, 'quantile', quantile=quantile, **kwargs) _shared_docs['cov'] = dedent(""" %(name)s sample covariance Parameters ---------- other : Series, DataFrame, or ndarray, optional if not supplied then will default to self and produce pairwise output pairwise : bool, default None If False then only matching columns between self and other will be used and the output will be a DataFrame. If True then all pairwise combinations will be calculated and the output will be a MultiIndexed DataFrame in the case of DataFrame inputs. In the case of missing elements, only complete pairwise observations will be used. ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is ``N - ddof``, where ``N`` represents the number of elements.""") def cov(self, other=None, pairwise=None, ddof=1, **kwargs): if other is None: other = self._selected_obj # only default unset pairwise = True if pairwise is None else pairwise other = self._shallow_copy(other) # GH 16058: offset window if self.is_freq_type: window = self.win_freq else: window = self._get_window(other) def _get_cov(X, Y): # GH #12373 : rolling functions error on float32 data # to avoid potential overflow, cast the data to float64 X = X.astype('float64') Y = Y.astype('float64') mean = lambda x: x.rolling(window, self.min_periods, center=self.center).mean(**kwargs) count = (X + Y).rolling(window=window, center=self.center).count(**kwargs) bias_adj = count / (count - ddof) return (mean(X * Y) - mean(X) * mean(Y)) * bias_adj return _flex_binary_moment(self._selected_obj, other._selected_obj, _get_cov, pairwise=bool(pairwise)) _shared_docs['corr'] = dedent(""" %(name)s sample correlation Parameters ---------- other : Series, DataFrame, or ndarray, optional if not supplied then will default to self and produce pairwise output pairwise : bool, default None If False then only matching columns between self and other will be used and the output will be a DataFrame. If True then all pairwise combinations will be calculated and the output will be a MultiIndex DataFrame in the case of DataFrame inputs. In the case of missing elements, only complete pairwise observations will be used.""") def corr(self, other=None, pairwise=None, **kwargs): if other is None: other = self._selected_obj # only default unset pairwise = True if pairwise is None else pairwise other = self._shallow_copy(other) window = self._get_window(other) def _get_corr(a, b): a = a.rolling(window=window, min_periods=self.min_periods, freq=self.freq, center=self.center) b = b.rolling(window=window, min_periods=self.min_periods, freq=self.freq, center=self.center) return a.cov(b, **kwargs) / (a.std(**kwargs) * b.std(**kwargs)) return _flex_binary_moment(self._selected_obj, other._selected_obj, _get_corr, pairwise=bool(pairwise)) class Rolling(_Rolling_and_Expanding): @cache_readonly def is_datetimelike(self): return isinstance(self._on, (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex)) @cache_readonly def _on(self): if self.on is None: return self.obj.index elif (isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns): from pandas import Index return Index(self.obj[self.on]) else: raise ValueError("invalid on specified as {0}, " "must be a column (if DataFrame) " "or None".format(self.on)) def validate(self): super(Rolling, self).validate() # we allow rolling on a datetimelike index if ((self.obj.empty or self.is_datetimelike) and isinstance(self.window, (compat.string_types, ABCDateOffset, timedelta))): self._validate_monotonic() freq = self._validate_freq() # we don't allow center if self.center: raise NotImplementedError("center is not implemented " "for datetimelike and offset " "based windows") # this will raise ValueError on non-fixed freqs self.win_freq = self.window self.window = freq.nanos self.win_type = 'freq' # min_periods must be an integer if self.min_periods is None: self.min_periods = 1 elif not is_integer(self.window): raise ValueError("window must be an integer") elif self.window < 0: raise ValueError("window must be non-negative") if not self.is_datetimelike and self.closed is not None: raise ValueError("closed only implemented for datetimelike " "and offset based windows") def _validate_monotonic(self): """ validate on is monotonic """ if not self._on.is_monotonic: formatted = self.on or 'index' raise ValueError("{0} must be " "monotonic".format(formatted)) def _validate_freq(self): """ validate & return our freq """ from pandas.tseries.frequencies import to_offset try: return to_offset(self.window) except (TypeError, ValueError): raise ValueError("passed window {0} in not " "compat with a datetimelike " "index".format(self.window)) _agg_doc = dedent(""" Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C']) >>> df A B C 0 -2.385977 -0.102758 0.438822 1 -1.004295 0.905829 -0.954544 2 0.735167 -0.165272 -1.619346 3 -0.702657 -1.340923 -0.706334 4 -0.246845 0.211596 -0.901819 5 2.463718 3.157577 -1.380906 6 -1.142255 2.340594 -0.039875 7 1.396598 -1.647453 1.677227 8 -0.543425 1.761277 -0.220481 9 -0.640505 0.289374 -1.550670 >>> df.rolling(3).sum() A B C 0 NaN NaN NaN 1 NaN NaN NaN 2 -2.655105 0.637799 -2.135068 3 -0.971785 -0.600366 -3.280224 4 -0.214334 -1.294599 -3.227500 5 1.514216 2.028250 -2.989060 6 1.074618 5.709767 -2.322600 7 2.718061 3.850718 0.256446 8 -0.289082 2.454418 1.416871 9 0.212668 0.403198 -0.093924 >>> df.rolling(3).agg({'A':'sum', 'B':'min'}) A B 0 NaN NaN 1 NaN NaN 2 -2.655105 -0.165272 3 -0.971785 -1.340923 4 -0.214334 -1.340923 5 1.514216 -1.340923 6 1.074618 0.211596 7 2.718061 -1.647453 8 -0.289082 -1.647453 9 0.212668 -1.647453 See also -------- pandas.Series.rolling pandas.DataFrame.rolling """) @Appender(_agg_doc) @Appender(_shared_docs['aggregate'] % dict( versionadded='', klass='Series/DataFrame')) def aggregate(self, arg, *args, **kwargs): return super(Rolling, self).aggregate(arg, *args, **kwargs) agg = aggregate @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['count']) def count(self): # different impl for freq counting if self.is_freq_type: return self._apply('roll_count', 'count') return super(Rolling, self).count() @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['apply']) def apply(self, func, args=(), kwargs={}): return super(Rolling, self).apply(func, args=args, kwargs=kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['sum']) def sum(self, *args, **kwargs): nv.validate_rolling_func('sum', args, kwargs) return super(Rolling, self).sum(*args, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['max']) def max(self, *args, **kwargs): nv.validate_rolling_func('max', args, kwargs) return super(Rolling, self).max(*args, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['min']) def min(self, *args, **kwargs): nv.validate_rolling_func('min', args, kwargs) return super(Rolling, self).min(*args, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['mean']) def mean(self, *args, **kwargs): nv.validate_rolling_func('mean', args, kwargs) return super(Rolling, self).mean(*args, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['median']) def median(self, **kwargs): return super(Rolling, self).median(**kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['std']) def std(self, ddof=1, *args, **kwargs): nv.validate_rolling_func('std', args, kwargs) return super(Rolling, self).std(ddof=ddof, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['var']) def var(self, ddof=1, *args, **kwargs): nv.validate_rolling_func('var', args, kwargs) return super(Rolling, self).var(ddof=ddof, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['skew']) def skew(self, **kwargs): return super(Rolling, self).skew(**kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['kurt']) def kurt(self, **kwargs): return super(Rolling, self).kurt(**kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['quantile']) def quantile(self, quantile, **kwargs): return super(Rolling, self).quantile(quantile=quantile, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['cov']) def cov(self, other=None, pairwise=None, ddof=1, **kwargs): return super(Rolling, self).cov(other=other, pairwise=pairwise, ddof=ddof, **kwargs) @Substitution(name='rolling') @Appender(_doc_template) @Appender(_shared_docs['corr']) def corr(self, other=None, pairwise=None, **kwargs): return super(Rolling, self).corr(other=other, pairwise=pairwise, **kwargs) class RollingGroupby(_GroupByMixin, Rolling): """ Provides a rolling groupby implementation .. versionadded:: 0.18.1 """ @property def _constructor(self): return Rolling def _gotitem(self, key, ndim, subset=None): # we are setting the index on the actual object # here so our index is carried thru to the selected obj # when we do the splitting for the groupby if self.on is not None: self._groupby.obj = self._groupby.obj.set_index(self._on) self.on = None return super(RollingGroupby, self)._gotitem(key, ndim, subset=subset) def _validate_monotonic(self): """ validate that on is monotonic; we don't care for groupby.rolling because we have already validated at a higher level """ pass class Expanding(_Rolling_and_Expanding): """ Provides expanding transformations. .. versionadded:: 0.18.0 Parameters ---------- min_periods : int, default None Minimum number of observations in window required to have a value (otherwise result is NA). freq : string or DateOffset object, optional (default None) .. deprecated:: 0.18.0 Frequency to conform the data to before computing the statistic. Specified as a frequency string or DateOffset object. center : boolean, default False Set the labels at the center of the window. axis : int or string, default 0 Returns ------- a Window sub-classed for the particular operation Examples -------- >>> df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) B 0 0.0 1 1.0 2 2.0 3 NaN 4 4.0 >>> df.expanding(2).sum() B 0 NaN 1 1.0 2 3.0 3 3.0 4 7.0 Notes ----- By default, the result is set to the right edge of the window. This can be changed to the center of the window by setting ``center=True``. The `freq` keyword is used to conform time series data to a specified frequency by resampling the data. This is done with the default parameters of :meth:`~pandas.Series.resample` (i.e. using the `mean`). """ _attributes = ['min_periods', 'freq', 'center', 'axis'] def __init__(self, obj, min_periods=1, freq=None, center=False, axis=0, **kwargs): super(Expanding, self).__init__(obj=obj, min_periods=min_periods, freq=freq, center=center, axis=axis) @property def _constructor(self): return Expanding def _get_window(self, other=None): obj = self._selected_obj if other is None: return (max(len(obj), self.min_periods) if self.min_periods else len(obj)) return (max((len(obj) + len(obj)), self.min_periods) if self.min_periods else (len(obj) + len(obj))) _agg_doc = dedent(""" Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C']) >>> df A B C 0 -2.385977 -0.102758 0.438822 1 -1.004295 0.905829 -0.954544 2 0.735167 -0.165272 -1.619346 3 -0.702657 -1.340923 -0.706334 4 -0.246845 0.211596 -0.901819 5 2.463718 3.157577 -1.380906 6 -1.142255 2.340594 -0.039875 7 1.396598 -1.647453 1.677227 8 -0.543425 1.761277 -0.220481 9 -0.640505 0.289374 -1.550670 >>> df.ewm(alpha=0.5).mean() A B C 0 -2.385977 -0.102758 0.438822 1 -1.464856 0.569633 -0.490089 2 -0.207700 0.149687 -1.135379 3 -0.471677 -0.645305 -0.906555 4 -0.355635 -0.203033 -0.904111 5 1.076417 1.503943 -1.146293 6 -0.041654 1.925562 -0.588728 7 0.680292 0.132049 0.548693 8 0.067236 0.948257 0.163353 9 -0.286980 0.618493 -0.694496 See also -------- pandas.DataFrame.expanding.aggregate pandas.DataFrame.rolling.aggregate pandas.DataFrame.aggregate """) @Appender(_agg_doc) @Appender(_shared_docs['aggregate'] % dict( versionadded='', klass='Series/DataFrame')) def aggregate(self, arg, *args, **kwargs): return super(Expanding, self).aggregate(arg, *args, **kwargs) agg = aggregate @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['count']) def count(self, **kwargs): return super(Expanding, self).count(**kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['apply']) def apply(self, func, args=(), kwargs={}): return super(Expanding, self).apply(func, args=args, kwargs=kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['sum']) def sum(self, *args, **kwargs): nv.validate_expanding_func('sum', args, kwargs) return super(Expanding, self).sum(*args, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['max']) def max(self, *args, **kwargs): nv.validate_expanding_func('max', args, kwargs) return super(Expanding, self).max(*args, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['min']) def min(self, *args, **kwargs): nv.validate_expanding_func('min', args, kwargs) return super(Expanding, self).min(*args, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['mean']) def mean(self, *args, **kwargs): nv.validate_expanding_func('mean', args, kwargs) return super(Expanding, self).mean(*args, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['median']) def median(self, **kwargs): return super(Expanding, self).median(**kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['std']) def std(self, ddof=1, *args, **kwargs): nv.validate_expanding_func('std', args, kwargs) return super(Expanding, self).std(ddof=ddof, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['var']) def var(self, ddof=1, *args, **kwargs): nv.validate_expanding_func('var', args, kwargs) return super(Expanding, self).var(ddof=ddof, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['skew']) def skew(self, **kwargs): return super(Expanding, self).skew(**kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['kurt']) def kurt(self, **kwargs): return super(Expanding, self).kurt(**kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['quantile']) def quantile(self, quantile, **kwargs): return super(Expanding, self).quantile(quantile=quantile, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['cov']) def cov(self, other=None, pairwise=None, ddof=1, **kwargs): return super(Expanding, self).cov(other=other, pairwise=pairwise, ddof=ddof, **kwargs) @Substitution(name='expanding') @Appender(_doc_template) @Appender(_shared_docs['corr']) def corr(self, other=None, pairwise=None, **kwargs): return super(Expanding, self).corr(other=other, pairwise=pairwise, **kwargs) class ExpandingGroupby(_GroupByMixin, Expanding): """ Provides a expanding groupby implementation .. versionadded:: 0.18.1 """ @property def _constructor(self): return Expanding _bias_template = """ Parameters ---------- bias : boolean, default False Use a standard estimation bias correction """ _pairwise_template = """ Parameters ---------- other : Series, DataFrame, or ndarray, optional if not supplied then will default to self and produce pairwise output pairwise : bool, default None If False then only matching columns between self and other will be used and the output will be a DataFrame. If True then all pairwise combinations will be calculated and the output will be a MultiIndex DataFrame in the case of DataFrame inputs. In the case of missing elements, only complete pairwise observations will be used. bias : boolean, default False Use a standard estimation bias correction """ class EWM(_Rolling): r""" Provides exponential weighted functions .. versionadded:: 0.18.0 Parameters ---------- com : float, optional Specify decay in terms of center of mass, :math:`\alpha = 1 / (1 + com),\text{ for } com \geq 0` span : float, optional Specify decay in terms of span, :math:`\alpha = 2 / (span + 1),\text{ for } span \geq 1` halflife : float, optional Specify decay in terms of half-life, :math:`\alpha = 1 - exp(log(0.5) / halflife),\text{ for } halflife > 0` alpha : float, optional Specify smoothing factor :math:`\alpha` directly, :math:`0 < \alpha \leq 1` .. versionadded:: 0.18.0 min_periods : int, default 0 Minimum number of observations in window required to have a value (otherwise result is NA). freq : None or string alias / date offset object, default=None .. deprecated:: 0.18.0 Frequency to conform to before computing statistic adjust : boolean, default True Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average) ignore_na : boolean, default False Ignore missing values when calculating weights; specify True to reproduce pre-0.15.0 behavior Returns ------- a Window sub-classed for the particular operation Examples -------- >>> df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) B 0 0.0 1 1.0 2 2.0 3 NaN 4 4.0 >>> df.ewm(com=0.5).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.670213 Notes ----- Exactly one of center of mass, span, half-life, and alpha must be provided. Allowed values and relationship between the parameters are specified in the parameter descriptions above; see the link at the end of this section for a detailed explanation. The `freq` keyword is used to conform time series data to a specified frequency by resampling the data. This is done with the default parameters of :meth:`~pandas.Series.resample` (i.e. using the `mean`). When adjust is True (default), weighted averages are calculated using weights (1-alpha)**(n-1), (1-alpha)**(n-2), ..., 1-alpha, 1. When adjust is False, weighted averages are calculated recursively as: weighted_average[0] = arg[0]; weighted_average[i] = (1-alpha)*weighted_average[i-1] + alpha*arg[i]. When ignore_na is False (default), weights are based on absolute positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are (1-alpha)**2 and 1 (if adjust is True), and (1-alpha)**2 and alpha (if adjust is False). When ignore_na is True (reproducing pre-0.15.0 behavior), weights are based on relative positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are 1-alpha and 1 (if adjust is True), and 1-alpha and alpha (if adjust is False). More details can be found at http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-windows """ _attributes = ['com', 'min_periods', 'freq', 'adjust', 'ignore_na', 'axis'] def __init__(self, obj, com=None, span=None, halflife=None, alpha=None, min_periods=0, freq=None, adjust=True, ignore_na=False, axis=0): self.obj = obj self.com = _get_center_of_mass(com, span, halflife, alpha) self.min_periods = min_periods self.freq = freq self.adjust = adjust self.ignore_na = ignore_na self.axis = axis self.on = None @property def _constructor(self): return EWM _agg_doc = dedent(""" Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C']) >>> df A B C 0 -2.385977 -0.102758 0.438822 1 -1.004295 0.905829 -0.954544 2 0.735167 -0.165272 -1.619346 3 -0.702657 -1.340923 -0.706334 4 -0.246845 0.211596 -0.901819 5 2.463718 3.157577 -1.380906 6 -1.142255 2.340594 -0.039875 7 1.396598 -1.647453 1.677227 8 -0.543425 1.761277 -0.220481 9 -0.640505 0.289374 -1.550670 >>> df.ewm(alpha=0.5).mean() A B C 0 -2.385977 -0.102758 0.438822 1 -1.464856 0.569633 -0.490089 2 -0.207700 0.149687 -1.135379 3 -0.471677 -0.645305 -0.906555 4 -0.355635 -0.203033 -0.904111 5 1.076417 1.503943 -1.146293 6 -0.041654 1.925562 -0.588728 7 0.680292 0.132049 0.548693 8 0.067236 0.948257 0.163353 9 -0.286980 0.618493 -0.694496 See also -------- pandas.DataFrame.rolling.aggregate """) @Appender(_agg_doc) @Appender(_shared_docs['aggregate'] % dict( versionadded='', klass='Series/DataFrame')) def aggregate(self, arg, *args, **kwargs): return super(EWM, self).aggregate(arg, *args, **kwargs) agg = aggregate def _apply(self, func, how=None, **kwargs): """Rolling statistical measure using supplied function. Designed to be used with passed-in Cython array-based functions. Parameters ---------- func : string/callable to apply how : string, default to None .. deprecated:: 0.18.0 how to resample Returns ------- y : type of input argument """ blocks, obj, index = self._create_blocks(how=how) results = [] for b in blocks: try: values = self._prep_values(b.values) except TypeError: results.append(b.values.copy()) continue if values.size == 0: results.append(values.copy()) continue # if we have a string function name, wrap it if isinstance(func, compat.string_types): cfunc = getattr(_window, func, None) if cfunc is None: raise ValueError("we do not support this function " "in _window.{0}".format(func)) def func(arg): return cfunc(arg, self.com, int(self.adjust), int(self.ignore_na), int(self.min_periods)) results.append(np.apply_along_axis(func, self.axis, values)) return self._wrap_results(results, blocks, obj) @Substitution(name='ewm') @Appender(_doc_template) def mean(self, *args, **kwargs): """exponential weighted moving average""" nv.validate_window_func('mean', args, kwargs) return self._apply('ewma', **kwargs) @Substitution(name='ewm') @Appender(_doc_template) @Appender(_bias_template) def std(self, bias=False, *args, **kwargs): """exponential weighted moving stddev""" nv.validate_window_func('std', args, kwargs) return _zsqrt(self.var(bias=bias, **kwargs)) vol = std @Substitution(name='ewm') @Appender(_doc_template) @Appender(_bias_template) def var(self, bias=False, *args, **kwargs): """exponential weighted moving variance""" nv.validate_window_func('var', args, kwargs) def f(arg): return _window.ewmcov(arg, arg, self.com, int(self.adjust), int(self.ignore_na), int(self.min_periods), int(bias)) return self._apply(f, **kwargs) @Substitution(name='ewm') @Appender(_doc_template) @Appender(_pairwise_template) def cov(self, other=None, pairwise=None, bias=False, **kwargs): """exponential weighted sample covariance""" if other is None: other = self._selected_obj # only default unset pairwise = True if pairwise is None else pairwise other = self._shallow_copy(other) def _get_cov(X, Y): X = self._shallow_copy(X) Y = self._shallow_copy(Y) cov = _window.ewmcov(X._prep_values(), Y._prep_values(), self.com, int(self.adjust), int(self.ignore_na), int(self.min_periods), int(bias)) return X._wrap_result(cov) return _flex_binary_moment(self._selected_obj, other._selected_obj, _get_cov, pairwise=bool(pairwise)) @Substitution(name='ewm') @Appender(_doc_template) @Appender(_pairwise_template) def corr(self, other=None, pairwise=None, **kwargs): """exponential weighted sample correlation""" if other is None: other = self._selected_obj # only default unset pairwise = True if pairwise is None else pairwise other = self._shallow_copy(other) def _get_corr(X, Y): X = self._shallow_copy(X) Y = self._shallow_copy(Y) def _cov(x, y): return _window.ewmcov(x, y, self.com, int(self.adjust), int(self.ignore_na), int(self.min_periods), 1) x_values = X._prep_values() y_values = Y._prep_values() with np.errstate(all='ignore'): cov = _cov(x_values, y_values) x_var = _cov(x_values, x_values) y_var = _cov(y_values, y_values) corr = cov / _zsqrt(x_var * y_var) return X._wrap_result(corr) return _flex_binary_moment(self._selected_obj, other._selected_obj, _get_corr, pairwise=bool(pairwise)) # Helper Funcs def _flex_binary_moment(arg1, arg2, f, pairwise=False): if not (isinstance(arg1, (np.ndarray, ABCSeries, ABCDataFrame)) and isinstance(arg2, (np.ndarray, ABCSeries, ABCDataFrame))): raise TypeError("arguments to moment function must be of type " "np.ndarray/Series/DataFrame") if (isinstance(arg1, (np.ndarray, ABCSeries)) and isinstance(arg2, (np.ndarray, ABCSeries))): X, Y = _prep_binary(arg1, arg2) return f(X, Y) elif isinstance(arg1, ABCDataFrame): from pandas import DataFrame def dataframe_from_int_dict(data, frame_template): result = DataFrame(data, index=frame_template.index) if len(result.columns) > 0: result.columns = frame_template.columns[result.columns] return result results = {} if isinstance(arg2, ABCDataFrame): if pairwise is False: if arg1 is arg2: # special case in order to handle duplicate column names for i, col in enumerate(arg1.columns): results[i] = f(arg1.iloc[:, i], arg2.iloc[:, i]) return dataframe_from_int_dict(results, arg1) else: if not arg1.columns.is_unique: raise ValueError("'arg1' columns are not unique") if not arg2.columns.is_unique: raise ValueError("'arg2' columns are not unique") with warnings.catch_warnings(record=True): X, Y = arg1.align(arg2, join='outer') X = X + 0 * Y Y = Y + 0 * X with warnings.catch_warnings(record=True): res_columns = arg1.columns.union(arg2.columns) for col in res_columns: if col in X and col in Y: results[col] = f(X[col], Y[col]) return DataFrame(results, index=X.index, columns=res_columns) elif pairwise is True: results = defaultdict(dict) for i, k1 in enumerate(arg1.columns): for j, k2 in enumerate(arg2.columns): if j < i and arg2 is arg1: # Symmetric case results[i][j] = results[j][i] else: results[i][j] = f(*_prep_binary(arg1.iloc[:, i], arg2.iloc[:, j])) # TODO: not the most efficient (perf-wise) # though not bad code-wise from pandas import Panel, MultiIndex, concat with warnings.catch_warnings(record=True): p = Panel.from_dict(results).swapaxes('items', 'major') if len(p.major_axis) > 0: p.major_axis = arg1.columns[p.major_axis] if len(p.minor_axis) > 0: p.minor_axis = arg2.columns[p.minor_axis] if len(p.items): result = concat( [p.iloc[i].T for i in range(len(p.items))], keys=p.items) else: result = DataFrame( index=MultiIndex(levels=[arg1.index, arg1.columns], labels=[[], []]), columns=arg2.columns, dtype='float64') # reset our index names to arg1 names # reset our column names to arg2 names # careful not to mutate the original names result.columns = result.columns.set_names( arg2.columns.names) result.index = result.index.set_names( arg1.index.names + arg1.columns.names) return result else: raise ValueError("'pairwise' is not True/False") else: results = {} for i, col in enumerate(arg1.columns): results[i] = f(*_prep_binary(arg1.iloc[:, i], arg2)) return dataframe_from_int_dict(results, arg1) else: return _flex_binary_moment(arg2, arg1, f) def _get_center_of_mass(com, span, halflife, alpha): valid_count = len([x for x in [com, span, halflife, alpha] if x is not None]) if valid_count > 1: raise ValueError("com, span, halflife, and alpha " "are mutually exclusive") # Convert to center of mass; domain checks ensure 0 < alpha <= 1 if com is not None: if com < 0: raise ValueError("com must satisfy: com >= 0") elif span is not None: if span < 1: raise ValueError("span must satisfy: span >= 1") com = (span - 1) / 2. elif halflife is not None: if halflife <= 0: raise ValueError("halflife must satisfy: halflife > 0") decay = 1 - np.exp(np.log(0.5) / halflife) com = 1 / decay - 1 elif alpha is not None: if alpha <= 0 or alpha > 1: raise ValueError("alpha must satisfy: 0 < alpha <= 1") com = (1.0 - alpha) / alpha else: raise ValueError("Must pass one of com, span, halflife, or alpha") return float(com) def _offset(window, center): if not is_integer(window): window = len(window) offset = (window - 1) / 2. if center else 0 try: return int(offset) except: return offset.astype(int) def _require_min_periods(p): def _check_func(minp, window): if minp is None: return window else: return max(p, minp) return _check_func def _use_window(minp, window): if minp is None: return window else: return minp def _zsqrt(x): with np.errstate(all='ignore'): result = np.sqrt(x) mask = x < 0 if isinstance(x, ABCDataFrame): if mask.values.any(): result[mask] = 0 else: if mask.any(): result[mask] = 0 return result def _prep_binary(arg1, arg2): if not isinstance(arg2, type(arg1)): raise Exception('Input arrays must be of the same type!') # mask out values, this also makes a common index... X = arg1 + 0 * arg2 Y = arg2 + 0 * arg1 return X, Y # Top-level exports def rolling(obj, win_type=None, **kwds): if not isinstance(obj, (ABCSeries, ABCDataFrame)): raise TypeError('invalid type: %s' % type(obj)) if win_type is not None: return Window(obj, win_type=win_type, **kwds) return Rolling(obj, **kwds) rolling.__doc__ = Window.__doc__ def expanding(obj, **kwds): if not isinstance(obj, (ABCSeries, ABCDataFrame)): raise TypeError('invalid type: %s' % type(obj)) return Expanding(obj, **kwds) expanding.__doc__ = Expanding.__doc__ def ewm(obj, **kwds): if not isinstance(obj, (ABCSeries, ABCDataFrame)): raise TypeError('invalid type: %s' % type(obj)) return EWM(obj, **kwds) ewm.__doc__ = EWM.__doc__
gpl-2.0
paultcochrane/bokeh
examples/charts/file/stocks_timeseries.py
33
1230
from collections import OrderedDict import pandas as pd from bokeh.charts import TimeSeries, show, output_file # read in some stock data from the Yahoo Finance API AAPL = pd.read_csv( "http://ichart.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000&d=0&e=1&f=2010", parse_dates=['Date']) MSFT = pd.read_csv( "http://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=0&e=1&f=2010", parse_dates=['Date']) IBM = pd.read_csv( "http://ichart.yahoo.com/table.csv?s=IBM&a=0&b=1&c=2000&d=0&e=1&f=2010", parse_dates=['Date']) xyvalues = OrderedDict( AAPL=AAPL['Adj Close'], Date=AAPL['Date'], MSFT=MSFT['Adj Close'], IBM=IBM['Adj Close'], ) # any of the following commented are valid Bar inputs #xyvalues = pd.DataFrame(xyvalues) #lindex = xyvalues.pop('Date') #lxyvalues = list(xyvalues.values()) #lxyvalues = np.array(xyvalues.values()) TOOLS="resize,pan,wheel_zoom,box_zoom,reset,previewsave" output_file("stocks_timeseries.html") ts = TimeSeries( xyvalues, index='Date', legend=True, title="Timeseries", tools=TOOLS, ylabel='Stock Prices') # usage with iterable index #ts = TimeSeries( # lxyvalues, index=lindex, # title="timeseries, pd_input", ylabel='Stock Prices') show(ts)
bsd-3-clause
wilsonkichoi/zipline
zipline/data/data_portal.py
1
64491
# # Copyright 2016 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from operator import mul import bcolz from logbook import Logger import numpy as np import pandas as pd from pandas.tslib import normalize_date from six import iteritems from six.moves import reduce from zipline.assets import Asset, Future, Equity from zipline.data.us_equity_pricing import NoDataOnDate from zipline.data.us_equity_loader import ( USEquityDailyHistoryLoader, USEquityMinuteHistoryLoader, ) from zipline.utils import tradingcalendar from zipline.utils.math_utils import ( nansum, nanmean, nanstd ) from zipline.utils.memoize import remember_last, weak_lru_cache from zipline.errors import ( NoTradeDataAvailableTooEarly, NoTradeDataAvailableTooLate, HistoryWindowStartsBeforeData, ) log = Logger('DataPortal') BASE_FIELDS = frozenset([ "open", "high", "low", "close", "volume", "price", "last_traded" ]) OHLCV_FIELDS = frozenset([ "open", "high", "low", "close", "volume" ]) OHLCVP_FIELDS = frozenset([ "open", "high", "low", "close", "volume", "price" ]) HISTORY_FREQUENCIES = set(["1m", "1d"]) class DailyHistoryAggregator(object): """ Converts minute pricing data into a daily summary, to be used for the last slot in a call to history with a frequency of `1d`. This summary is the same as a daily bar rollup of minute data, with the distinction that the summary is truncated to the `dt` requested. i.e. the aggregation slides forward during a the course of simulation day. Provides aggregation for `open`, `high`, `low`, `close`, and `volume`. The aggregation rules for each price type is documented in their respective """ def __init__(self, market_opens, minute_reader): self._market_opens = market_opens self._minute_reader = minute_reader # The caches are structured as (date, market_open, entries), where # entries is a dict of asset -> (last_visited_dt, value) # # Whenever an aggregation method determines the current value, # the entry for the respective asset should be overwritten with a new # entry for the current dt.value (int) and aggregation value. # # When the requested dt's date is different from date the cache is # flushed, so that the cache entries do not grow unbounded. # # Example cache: # cache = (date(2016, 3, 17), # pd.Timestamp('2016-03-17 13:31', tz='UTC'), # { # 1: (1458221460000000000, np.nan), # 2: (1458221460000000000, 42.0), # }) self._caches = { 'open': None, 'high': None, 'low': None, 'close': None, 'volume': None } # The int value is used for deltas to avoid extra computation from # creating new Timestamps. self._one_min = pd.Timedelta('1 min').value def _prelude(self, dt, field): date = dt.date() dt_value = dt.value cache = self._caches[field] if cache is None or cache[0] != date: market_open = self._market_opens.loc[date] cache = self._caches[field] = (dt.date(), market_open, {}) _, market_open, entries = cache if dt != market_open: prev_dt = dt_value - self._one_min else: prev_dt = None return market_open, prev_dt, dt_value, entries def opens(self, assets, dt): """ The open field's aggregation returns the first value that occurs for the day, if there has been no data on or before the `dt` the open is `nan`. Once the first non-nan open is seen, that value remains constant per asset for the remainder of the day. Returns ------- np.array with dtype=float64, in order of assets parameter. """ market_open, prev_dt, dt_value, entries = self._prelude(dt, 'open') opens = [] normalized_date = normalize_date(dt) for asset in assets: if not asset._is_alive(normalized_date, True): opens.append(np.NaN) continue if prev_dt is None: val = self._minute_reader.get_value(asset, dt, 'open') entries[asset] = (dt_value, val) opens.append(val) continue else: try: last_visited_dt, first_open = entries[asset] if last_visited_dt == dt_value: opens.append(first_open) continue elif not pd.isnull(first_open): opens.append(first_open) entries[asset] = (dt_value, first_open) continue else: after_last = pd.Timestamp( last_visited_dt + self._one_min, tz='UTC') window = self._minute_reader.load_raw_arrays( ['open'], after_last, dt, [asset], )[0] nonnan = window[~pd.isnull(window)] if len(nonnan): val = nonnan[0] else: val = np.nan entries[asset] = (dt_value, val) opens.append(val) continue except KeyError: window = self._minute_reader.load_raw_arrays( ['open'], market_open, dt, [asset], )[0] nonnan = window[~pd.isnull(window)] if len(nonnan): val = nonnan[0] else: val = np.nan entries[asset] = (dt_value, val) opens.append(val) continue return np.array(opens) def highs(self, assets, dt): """ The high field's aggregation returns the largest high seen between the market open and the current dt. If there has been no data on or before the `dt` the high is `nan`. Returns ------- np.array with dtype=float64, in order of assets parameter. """ market_open, prev_dt, dt_value, entries = self._prelude(dt, 'high') highs = [] normalized_date = normalize_date(dt) for asset in assets: if not asset._is_alive(normalized_date, True): highs.append(np.NaN) continue if prev_dt is None: val = self._minute_reader.get_value(asset, dt, 'high') entries[asset] = (dt_value, val) highs.append(val) continue else: try: last_visited_dt, last_max = entries[asset] if last_visited_dt == dt_value: highs.append(last_max) continue elif last_visited_dt == prev_dt: curr_val = self._minute_reader.get_value( asset, dt, 'high') if pd.isnull(curr_val): val = last_max elif pd.isnull(last_max): val = curr_val else: val = max(last_max, curr_val) entries[asset] = (dt_value, val) highs.append(val) continue else: after_last = pd.Timestamp( last_visited_dt + self._one_min, tz='UTC') window = self._minute_reader.load_raw_arrays( ['high'], after_last, dt, [asset], )[0].T val = max(last_max, np.nanmax(window)) entries[asset] = (dt_value, val) highs.append(val) continue except KeyError: window = self._minute_reader.load_raw_arrays( ['high'], market_open, dt, [asset], )[0].T val = np.nanmax(window) entries[asset] = (dt_value, val) highs.append(val) continue return np.array(highs) def lows(self, assets, dt): """ The low field's aggregation returns the smallest low seen between the market open and the current dt. If there has been no data on or before the `dt` the low is `nan`. Returns ------- np.array with dtype=float64, in order of assets parameter. """ market_open, prev_dt, dt_value, entries = self._prelude(dt, 'low') lows = [] normalized_date = normalize_date(dt) for asset in assets: if not asset._is_alive(normalized_date, True): lows.append(np.NaN) continue if prev_dt is None: val = self._minute_reader.get_value(asset, dt, 'low') entries[asset] = (dt_value, val) lows.append(val) continue else: try: last_visited_dt, last_min = entries[asset] if last_visited_dt == dt_value: lows.append(last_min) continue elif last_visited_dt == prev_dt: curr_val = self._minute_reader.get_value( asset, dt, 'low') val = np.nanmin([last_min, curr_val]) entries[asset] = (dt_value, val) lows.append(val) continue else: after_last = pd.Timestamp( last_visited_dt + self._one_min, tz='UTC') window = self._minute_reader.load_raw_arrays( ['low'], after_last, dt, [asset], )[0].T window_min = np.nanmin(window) if pd.isnull(window_min): val = last_min else: val = min(last_min, window_min) entries[asset] = (dt_value, val) lows.append(val) continue except KeyError: window = self._minute_reader.load_raw_arrays( ['low'], market_open, dt, [asset], )[0].T val = np.nanmin(window) entries[asset] = (dt_value, val) lows.append(val) continue return np.array(lows) def closes(self, assets, dt): """ The close field's aggregation returns the latest close at the given dt. If the close for the given dt is `nan`, the most recent non-nan `close` is used. If there has been no data on or before the `dt` the close is `nan`. Returns ------- np.array with dtype=float64, in order of assets parameter. """ market_open, prev_dt, dt_value, entries = self._prelude(dt, 'close') closes = [] normalized_dt = normalize_date(dt) for asset in assets: if not asset._is_alive(normalized_dt, True): closes.append(np.NaN) continue if prev_dt is None: val = self._minute_reader.get_value(asset, dt, 'close') entries[asset] = (dt_value, val) closes.append(val) continue else: try: last_visited_dt, last_close = entries[asset] if last_visited_dt == dt_value: closes.append(last_close) continue elif last_visited_dt == prev_dt: val = self._minute_reader.get_value( asset, dt, 'close') if pd.isnull(val): val = last_close entries[asset] = (dt_value, val) closes.append(val) continue else: val = self._minute_reader.get_value( asset, dt, 'close') if pd.isnull(val): val = self.closes( [asset], pd.Timestamp(prev_dt, tz='UTC'))[0] entries[asset] = (dt_value, val) closes.append(val) continue except KeyError: val = self._minute_reader.get_value( asset, dt, 'close') if pd.isnull(val): val = self.closes([asset], pd.Timestamp(prev_dt, tz='UTC'))[0] entries[asset] = (dt_value, val) closes.append(val) continue return np.array(closes) def volumes(self, assets, dt): """ The volume field's aggregation returns the sum of all volumes between the market open and the `dt` If there has been no data on or before the `dt` the volume is 0. Returns ------- np.array with dtype=int64, in order of assets parameter. """ market_open, prev_dt, dt_value, entries = self._prelude(dt, 'volume') volumes = [] normalized_date = normalize_date(dt) for asset in assets: if not asset._is_alive(normalized_date, True): volumes.append(0) continue if prev_dt is None: val = self._minute_reader.get_value(asset, dt, 'volume') entries[asset] = (dt_value, val) volumes.append(val) continue else: try: last_visited_dt, last_total = entries[asset] if last_visited_dt == dt_value: volumes.append(last_total) continue elif last_visited_dt == prev_dt: val = self._minute_reader.get_value( asset, dt, 'volume') val += last_total entries[asset] = (dt_value, val) volumes.append(val) continue else: after_last = pd.Timestamp( last_visited_dt + self._one_min, tz='UTC') window = self._minute_reader.load_raw_arrays( ['volume'], after_last, dt, [asset], )[0] val = np.nansum(window) + last_total entries[asset] = (dt_value, val) volumes.append(val) continue except KeyError: window = self._minute_reader.load_raw_arrays( ['volume'], market_open, dt, [asset], )[0] val = np.nansum(window) entries[asset] = (dt_value, val) volumes.append(val) continue return np.array(volumes) class DataPortal(object): """Interface to all of the data that a zipline simulation needs. This is used by the simulation runner to answer questions about the data, like getting the prices of assets on a given day or to service history calls. Parameters ---------- env : TradingEnvironment The trading environment for the simulation. This includes the trading calendar and benchmark data. first_trading_day : pd.Timestamp The first trading day for the simulation. equity_daily_reader : BcolzDailyBarReader, optional The daily bar reader for equities. This will be used to service daily data backtests or daily history calls in a minute backetest. If a daily bar reader is not provided but a minute bar reader is, the minutes will be rolled up to serve the daily requests. equity_minute_reader : BcolzMinuteBarReader, optional The minute bar reader for equities. This will be used to service minute data backtests or minute history calls. This can be used to serve daily calls if no daily bar reader is provided. future_daily_reader : BcolzDailyBarReader, optional The daily bar ready for futures. This will be used to service daily data backtests or daily history calls in a minute backetest. If a daily bar reader is not provided but a minute bar reader is, the minutes will be rolled up to serve the daily requests. future_minute_reader : BcolzMinuteBarReader, optional The minute bar reader for futures. This will be used to service minute data backtests or minute history calls. This can be used to serve daily calls if no daily bar reader is provided. adjustment_reader : SQLiteAdjustmentWriter, optional The adjustment reader. This is used to apply splits, dividends, and other adjustment data to the raw data from the readers. """ def __init__(self, env, first_trading_day, equity_daily_reader=None, equity_minute_reader=None, future_daily_reader=None, future_minute_reader=None, adjustment_reader=None): self.env = env self.views = {} self._asset_finder = env.asset_finder self._carrays = { 'open': {}, 'high': {}, 'low': {}, 'close': {}, 'volume': {}, 'sid': {}, } self._adjustment_reader = adjustment_reader # caches of sid -> adjustment list self._splits_dict = {} self._mergers_dict = {} self._dividends_dict = {} # Cache of sid -> the first trading day of an asset. self._asset_start_dates = {} self._asset_end_dates = {} # Handle extra sources, like Fetcher. self._augmented_sources_map = {} self._extra_source_df = None self._equity_daily_reader = equity_daily_reader if self._equity_daily_reader is not None: self._equity_history_loader = USEquityDailyHistoryLoader( self.env, self._equity_daily_reader, self._adjustment_reader ) self._equity_minute_reader = equity_minute_reader self._future_daily_reader = future_daily_reader self._future_minute_reader = future_minute_reader self._first_trading_day = first_trading_day if self._equity_minute_reader is not None: self._equity_daily_aggregator = DailyHistoryAggregator( self.env.open_and_closes.market_open, self._equity_minute_reader) self._equity_minute_history_loader = USEquityMinuteHistoryLoader( self.env, self._equity_minute_reader, self._adjustment_reader ) self.MINUTE_PRICE_ADJUSTMENT_FACTOR = \ self._equity_minute_reader._ohlc_inverse def _reindex_extra_source(self, df, source_date_index): return df.reindex(index=source_date_index, method='ffill') def handle_extra_source(self, source_df, sim_params): """ Extra sources always have a sid column. We expand the given data (by forward filling) to the full range of the simulation dates, so that lookup is fast during simulation. """ if source_df is None: return # Normalize all the dates in the df source_df.index = source_df.index.normalize() # source_df's sid column can either consist of assets we know about # (such as sid(24)) or of assets we don't know about (such as # palladium). # # In both cases, we break up the dataframe into individual dfs # that only contain a single asset's information. ie, if source_df # has data for PALLADIUM and GOLD, we split source_df into two # dataframes, one for each. (same applies if source_df has data for # AAPL and IBM). # # We then take each child df and reindex it to the simulation's date # range by forward-filling missing values. this makes reads simpler. # # Finally, we store the data. For each column, we store a mapping in # self.augmented_sources_map from the column to a dictionary of # asset -> df. In other words, # self.augmented_sources_map['days_to_cover']['AAPL'] gives us the df # holding that data. source_date_index = self.env.days_in_range( start=sim_params.period_start, end=sim_params.period_end ) # Break the source_df up into one dataframe per sid. This lets # us (more easily) calculate accurate start/end dates for each sid, # de-dup data, and expand the data to fit the backtest start/end date. grouped_by_sid = source_df.groupby(["sid"]) group_names = grouped_by_sid.groups.keys() group_dict = {} for group_name in group_names: group_dict[group_name] = grouped_by_sid.get_group(group_name) # This will be the dataframe which we query to get fetcher assets at # any given time. Get's overwritten every time there's a new fetcher # call extra_source_df = pd.DataFrame() for identifier, df in iteritems(group_dict): # Before reindexing, save the earliest and latest dates earliest_date = df.index[0] latest_date = df.index[-1] # Since we know this df only contains a single sid, we can safely # de-dupe by the index (dt). If minute granularity, will take the # last data point on any given day df = df.groupby(level=0).last() # Reindex the dataframe based on the backtest start/end date. # This makes reads easier during the backtest. df = self._reindex_extra_source(df, source_date_index) if not isinstance(identifier, Asset): # for fake assets we need to store a start/end date self._asset_start_dates[identifier] = earliest_date self._asset_end_dates[identifier] = latest_date for col_name in df.columns.difference(['sid']): if col_name not in self._augmented_sources_map: self._augmented_sources_map[col_name] = {} self._augmented_sources_map[col_name][identifier] = df # Append to extra_source_df the reindexed dataframe for the single # sid extra_source_df = extra_source_df.append(df) self._extra_source_df = extra_source_df def _open_minute_file(self, field, asset): sid_str = str(int(asset)) try: carray = self._carrays[field][sid_str] except KeyError: carray = self._carrays[field][sid_str] = \ self._get_ctable(asset)[field] return carray def _get_ctable(self, asset): sid = int(asset) if isinstance(asset, Future): if self._future_minute_reader.sid_path_func is not None: path = self._future_minute_reader.sid_path_func( self._future_minute_reader.rootdir, sid ) else: path = "{0}/{1}.bcolz".format( self._future_minute_reader.rootdir, sid) elif isinstance(asset, Equity): if self._equity_minute_reader.sid_path_func is not None: path = self._equity_minute_reader.sid_path_func( self._equity_minute_reader.rootdir, sid ) else: path = "{0}/{1}.bcolz".format( self._equity_minute_reader.rootdir, sid) else: # TODO: Figure out if assets should be allowed if neither, and # why this code path is being hit. if self._equity_minute_reader.sid_path_func is not None: path = self._equity_minute_reader.sid_path_func( self._equity_minute_reader.rootdir, sid ) else: path = "{0}/{1}.bcolz".format( self._equity_minute_reader.rootdir, sid) return bcolz.open(path, mode='r') def get_last_traded_dt(self, asset, dt, data_frequency): """ Given an asset and dt, returns the last traded dt from the viewpoint of the given dt. If there is a trade on the dt, the answer is dt provided. """ if data_frequency == 'minute': return self._equity_minute_reader.get_last_traded_dt(asset, dt) elif data_frequency == 'daily': return self._equity_daily_reader.get_last_traded_dt(asset, dt) @staticmethod def _is_extra_source(asset, field, map): """ Internal method that determines if this asset/field combination represents a fetcher value or a regular OHLCVP lookup. """ # If we have an extra source with a column called "price", only look # at it if it's on something like palladium and not AAPL (since our # own price data always wins when dealing with assets). return not (field in BASE_FIELDS and isinstance(asset, Asset)) def _get_fetcher_value(self, asset, field, dt): day = normalize_date(dt) try: return \ self._augmented_sources_map[field][asset].loc[day, field] except KeyError: return np.NaN def get_spot_value(self, asset, field, dt, data_frequency): """ Public API method that returns a scalar value representing the value of the desired asset's field at either the given dt. Parameters ---------- asset : Asset The asset whose data is desired. field : {'open', 'high', 'low', 'close', 'volume', 'price', 'last_traded'} The desired field of the asset. dt : pd.Timestamp The timestamp for the desired value. data_frequency : str The frequency of the data to query; i.e. whether the data is 'daily' or 'minute' bars Returns ------- value : float, int, or pd.Timestamp The spot value of ``field`` for ``asset`` The return type is based on the ``field`` requested. If the field is one of 'open', 'high', 'low', 'close', or 'price', the value will be a float. If the ``field`` is 'volume' the value will be a int. If the ``field`` is 'last_traded' the value will be a Timestamp. """ if self._is_extra_source(asset, field, self._augmented_sources_map): return self._get_fetcher_value(asset, field, dt) if field not in BASE_FIELDS: raise KeyError("Invalid column: " + str(field)) if dt < asset.start_date or \ (data_frequency == "daily" and dt > asset.end_date) or \ (data_frequency == "minute" and normalize_date(dt) > asset.end_date): if field == "volume": return 0 elif field != "last_traded": return np.NaN if data_frequency == "daily": day_to_use = dt day_to_use = normalize_date(day_to_use) return self._get_daily_data(asset, field, day_to_use) else: if isinstance(asset, Future): return self._get_minute_spot_value_future( asset, field, dt) else: if field == "last_traded": return self._equity_minute_reader.get_last_traded_dt( asset, dt ) elif field == "price": return self._get_minute_spot_value(asset, "close", dt, True) else: return self._get_minute_spot_value(asset, field, dt) def get_adjustments(self, assets, field, dt, perspective_dt): """ Returns a list of adjustments between the dt and perspective_dt for the given field and list of assets Parameters ---------- assets : list of type Asset, or Asset The asset, or assets whose adjustments are desired. field : {'open', 'high', 'low', 'close', 'volume', \ 'price', 'last_traded'} The desired field of the asset. dt : pd.Timestamp The timestamp for the desired value. perspective_dt : pd.Timestamp The timestamp from which the data is being viewed back from. data_frequency : str The frequency of the data to query; i.e. whether the data is 'daily' or 'minute' bars Returns ------- adjustments : list[Adjustment] The adjustments to that field. """ if isinstance(assets, Asset): assets = [assets] adjustment_ratios_per_asset = [] split_adj_factor = lambda x: x if field != 'volume' else 1.0 / x for asset in assets: adjustments_for_asset = [] split_adjustments = self._get_adjustment_list( asset, self._splits_dict, "SPLITS" ) for adj_dt, adj in split_adjustments: if dt <= adj_dt <= perspective_dt: adjustments_for_asset.append(split_adj_factor(adj)) elif adj_dt > perspective_dt: break if field != 'volume': merger_adjustments = self._get_adjustment_list( asset, self._mergers_dict, "MERGERS" ) for adj_dt, adj in merger_adjustments: if dt <= adj_dt <= perspective_dt: adjustments_for_asset.append(adj) elif adj_dt > perspective_dt: break dividend_adjustments = self._get_adjustment_list( asset, self._dividends_dict, "DIVIDENDS", ) for adj_dt, adj in dividend_adjustments: if dt <= adj_dt <= perspective_dt: adjustments_for_asset.append(adj) elif adj_dt > perspective_dt: break ratio = reduce(mul, adjustments_for_asset, 1.0) adjustment_ratios_per_asset.append(ratio) return adjustment_ratios_per_asset def get_adjusted_value(self, asset, field, dt, perspective_dt, data_frequency, spot_value=None): """ Returns a scalar value representing the value of the desired asset's field at the given dt with adjustments applied. Parameters ---------- asset : Asset The asset whose data is desired. field : {'open', 'high', 'low', 'close', 'volume', \ 'price', 'last_traded'} The desired field of the asset. dt : pd.Timestamp The timestamp for the desired value. perspective_dt : pd.Timestamp The timestamp from which the data is being viewed back from. data_frequency : str The frequency of the data to query; i.e. whether the data is 'daily' or 'minute' bars Returns ------- value : float, int, or pd.Timestamp The value of the given ``field`` for ``asset`` at ``dt`` with any adjustments known by ``perspective_dt`` applied. The return type is based on the ``field`` requested. If the field is one of 'open', 'high', 'low', 'close', or 'price', the value will be a float. If the ``field`` is 'volume' the value will be a int. If the ``field`` is 'last_traded' the value will be a Timestamp. """ if spot_value is None: # if this a fetcher field, we want to use perspective_dt (not dt) # because we want the new value as of midnight (fetcher only works # on a daily basis, all timestamps are on midnight) if self._is_extra_source(asset, field, self._augmented_sources_map): spot_value = self.get_spot_value(asset, field, perspective_dt, data_frequency) else: spot_value = self.get_spot_value(asset, field, dt, data_frequency) if isinstance(asset, Equity): ratio = self.get_adjustments(asset, field, dt, perspective_dt)[0] spot_value *= ratio return spot_value def _get_minute_spot_value_future(self, asset, column, dt): # Futures bcolz files have 1440 bars per day (24 hours), 7 days a week. # The file attributes contain the "start_dt" and "last_dt" fields, # which represent the time period for this bcolz file. # The start_dt is midnight of the first day that this future started # trading. # figure out the # of minutes between dt and this asset's start_dt start_date = self._get_asset_start_date(asset) minute_offset = int((dt - start_date).total_seconds() / 60) if minute_offset < 0: # asking for a date that is before the asset's start date, no dice return 0.0 # then just index into the bcolz carray at that offset carray = self._open_minute_file(column, asset) result = carray[minute_offset] # if there's missing data, go backwards until we run out of file while result == 0 and minute_offset > 0: minute_offset -= 1 result = carray[minute_offset] if column != 'volume': # FIXME switch to a futures reader return result * 0.001 else: return result def _get_minute_spot_value(self, asset, column, dt, ffill=False): result = self._equity_minute_reader.get_value( asset.sid, dt, column ) if column == "volume": if result == 0: return 0 elif not ffill or not np.isnan(result): # if we're not forward filling, or we found a result, return it return result # we are looking for price, and didn't find one. have to go hunting. last_traded_dt = \ self._equity_minute_reader.get_last_traded_dt(asset, dt) if last_traded_dt is pd.NaT: # no last traded dt, bail return np.nan # get the value as of the last traded dt result = self._equity_minute_reader.get_value( asset.sid, last_traded_dt, column ) if np.isnan(result): return np.nan if dt == last_traded_dt or dt.date() == last_traded_dt.date(): return result # the value we found came from a different day, so we have to adjust # the data if there are any adjustments on that day barrier return self.get_adjusted_value( asset, column, last_traded_dt, dt, "minute", spot_value=result ) def _get_daily_data(self, asset, column, dt): if column == "last_traded": last_traded_dt = \ self._equity_daily_reader.get_last_traded_dt(asset, dt) if pd.isnull(last_traded_dt): return pd.NaT else: return last_traded_dt elif column in OHLCV_FIELDS: # don't forward fill try: val = self._equity_daily_reader.spot_price(asset, dt, column) if val == -1: if column == "volume": return 0 else: return np.nan else: return val except NoDataOnDate: return np.nan elif column == "price": found_dt = dt while True: try: value = self._equity_daily_reader.spot_price( asset, found_dt, "close" ) if value != -1: if dt == found_dt: return value else: # adjust if needed return self.get_adjusted_value( asset, column, found_dt, dt, "minute", spot_value=value ) else: found_dt -= tradingcalendar.trading_day except NoDataOnDate: return np.nan @remember_last def _get_days_for_window(self, end_date, bar_count): tds = self.env.trading_days end_loc = self.env.trading_days.get_loc(end_date) start_loc = end_loc - bar_count + 1 if start_loc < 0: raise HistoryWindowStartsBeforeData( first_trading_day=self.env.first_trading_day.date(), bar_count=bar_count, suggested_start_day=tds[bar_count].date(), ) return tds[start_loc:end_loc + 1] def _get_history_daily_window(self, assets, end_dt, bar_count, field_to_use): """ Internal method that returns a dataframe containing history bars of daily frequency for the given sids. """ days_for_window = self._get_days_for_window(end_dt.date(), bar_count) if len(assets) == 0: return pd.DataFrame(None, index=days_for_window, columns=None) future_data = [] eq_assets = [] for asset in assets: if isinstance(asset, Future): future_data.append(self._get_history_daily_window_future( asset, days_for_window, end_dt, field_to_use )) else: eq_assets.append(asset) eq_data = self._get_history_daily_window_equities( eq_assets, days_for_window, end_dt, field_to_use ) if future_data: # TODO: This case appears to be uncovered by testing. data = np.concatenate(eq_data, np.array(future_data).T) else: data = eq_data return pd.DataFrame( data, index=days_for_window, columns=assets ) def _get_history_daily_window_future(self, asset, days_for_window, end_dt, column): # Since we don't have daily bcolz files for futures (yet), use minute # bars to calculate the daily values. data = [] data_groups = [] # get all the minutes for the days NOT including today for day in days_for_window[:-1]: minutes = self.env.market_minutes_for_day(day) values_for_day = np.zeros(len(minutes), dtype=np.float64) for idx, minute in enumerate(minutes): minute_val = self._get_minute_spot_value_future( asset, column, minute ) values_for_day[idx] = minute_val data_groups.append(values_for_day) # get the minutes for today last_day_minutes = pd.date_range( start=self.env.get_open_and_close(end_dt)[0], end=end_dt, freq="T" ) values_for_last_day = np.zeros(len(last_day_minutes), dtype=np.float64) for idx, minute in enumerate(last_day_minutes): minute_val = self._get_minute_spot_value_future( asset, column, minute ) values_for_last_day[idx] = minute_val data_groups.append(values_for_last_day) for group in data_groups: if len(group) == 0: continue if column == 'volume': data.append(np.sum(group)) elif column == 'open': data.append(group[0]) elif column == 'close': data.append(group[-1]) elif column == 'high': data.append(np.amax(group)) elif column == 'low': data.append(np.amin(group)) return data def _get_history_daily_window_equities( self, assets, days_for_window, end_dt, field_to_use): ends_at_midnight = end_dt.hour == 0 and end_dt.minute == 0 if ends_at_midnight: # two cases where we use daily data for the whole range: # 1) the history window ends at midnight utc. # 2) the last desired day of the window is after the # last trading day, use daily data for the whole range. return self._get_daily_window_for_sids( assets, field_to_use, days_for_window, extra_slot=False ) else: # minute mode, requesting '1d' daily_data = self._get_daily_window_for_sids( assets, field_to_use, days_for_window[0:-1] ) if field_to_use == 'open': minute_value = self._equity_daily_aggregator.opens( assets, end_dt) elif field_to_use == 'high': minute_value = self._equity_daily_aggregator.highs( assets, end_dt) elif field_to_use == 'low': minute_value = self._equity_daily_aggregator.lows( assets, end_dt) elif field_to_use == 'close': minute_value = self._equity_daily_aggregator.closes( assets, end_dt) elif field_to_use == 'volume': minute_value = self._equity_daily_aggregator.volumes( assets, end_dt) # append the partial day. daily_data[-1] = minute_value return daily_data def _get_history_minute_window(self, assets, end_dt, bar_count, field_to_use): """ Internal method that returns a dataframe containing history bars of minute frequency for the given sids. """ # get all the minutes for this window mm = self.env.market_minutes end_loc = mm.get_loc(end_dt) start_loc = end_loc - bar_count + 1 if start_loc < 0: suggested_start_day = (mm[bar_count] + self.env.trading_day).date() raise HistoryWindowStartsBeforeData( first_trading_day=self.env.first_trading_day.date(), bar_count=bar_count, suggested_start_day=suggested_start_day, ) minutes_for_window = mm[start_loc:end_loc + 1] asset_minute_data = self._get_minute_window_for_assets( assets, field_to_use, minutes_for_window, ) return pd.DataFrame( asset_minute_data, index=minutes_for_window, columns=assets ) def get_history_window(self, assets, end_dt, bar_count, frequency, field, ffill=True): """ Public API method that returns a dataframe containing the requested history window. Data is fully adjusted. Parameters ---------- assets : list of zipline.data.Asset objects The assets whose data is desired. bar_count: int The number of bars desired. frequency: string "1d" or "1m" field: string The desired field of the asset. ffill: boolean Forward-fill missing values. Only has effect if field is 'price'. Returns ------- A dataframe containing the requested data. """ if field not in OHLCVP_FIELDS: raise ValueError("Invalid field: {0}".format(field)) if frequency == "1d": if field == "price": df = self._get_history_daily_window(assets, end_dt, bar_count, "close") else: df = self._get_history_daily_window(assets, end_dt, bar_count, field) elif frequency == "1m": if field == "price": df = self._get_history_minute_window(assets, end_dt, bar_count, "close") else: df = self._get_history_minute_window(assets, end_dt, bar_count, field) else: raise ValueError("Invalid frequency: {0}".format(frequency)) # forward-fill price if field == "price": if frequency == "1m": data_frequency = 'minute' elif frequency == "1d": data_frequency = 'daily' else: raise Exception( "Only 1d and 1m are supported for forward-filling.") dt_to_fill = df.index[0] perspective_dt = df.index[-1] assets_with_leading_nan = np.where(pd.isnull(df.iloc[0]))[0] for missing_loc in assets_with_leading_nan: asset = assets[missing_loc] previous_dt = self.get_last_traded_dt( asset, dt_to_fill, data_frequency) if pd.isnull(previous_dt): continue previous_value = self.get_adjusted_value( asset, field, previous_dt, perspective_dt, data_frequency, ) df.iloc[0, missing_loc] = previous_value df.fillna(method='ffill', inplace=True) for asset in df.columns: if df.index[-1] >= asset.end_date: # if the window extends past the asset's end date, set # all post-end-date values to NaN in that asset's series series = df[asset] series[series.index.normalize() > asset.end_date] = np.NaN return df def _get_minute_window_for_assets(self, assets, field, minutes_for_window): """ Internal method that gets a window of adjusted minute data for an asset and specified date range. Used to support the history API method for minute bars. Missing bars are filled with NaN. Parameters ---------- asset : Asset The asset whose data is desired. field: string The specific field to return. "open", "high", "close_price", etc. minutes_for_window: pd.DateTimeIndex The list of minutes representing the desired window. Each minute is a pd.Timestamp. Returns ------- A numpy array with requested values. """ if isinstance(assets, Future): return self._get_minute_window_for_future([assets], field, minutes_for_window) else: # TODO: Make caller accept assets. window = self._get_minute_window_for_equities(assets, field, minutes_for_window) return window def _get_minute_window_for_future(self, asset, field, minutes_for_window): # THIS IS TEMPORARY. For now, we are only exposing futures within # equity trading hours (9:30 am to 4pm, Eastern). The easiest way to # do this is to simply do a spot lookup for each desired minute. return_data = np.zeros(len(minutes_for_window), dtype=np.float64) for idx, minute in enumerate(minutes_for_window): return_data[idx] = \ self._get_minute_spot_value_future(asset, field, minute) # Note: an improvement could be to find the consecutive runs within # minutes_for_window, and use them to read the underlying ctable # more efficiently. # Once futures are on 24-hour clock, then we can just grab all the # requested minutes in one shot from the ctable. # no adjustments for futures, yay. return return_data def _get_minute_window_for_equities( self, assets, field, minutes_for_window): return self._equity_minute_history_loader.history(assets, minutes_for_window, field) def _apply_all_adjustments(self, data, asset, dts, field, price_adj_factor=1.0): """ Internal method that applies all the necessary adjustments on the given data array. The adjustments are: - splits - if field != "volume": - mergers - dividends - * 0.001 - any zero fields replaced with NaN - all values rounded to 3 digits after the decimal point. Parameters ---------- data : np.array The data to be adjusted. asset: Asset The asset whose data is being adjusted. dts: pd.DateTimeIndex The list of minutes or days representing the desired window. field: string The field whose values are in the data array. price_adj_factor: float Factor with which to adjust OHLC values. Returns ------- None. The data array is modified in place. """ self._apply_adjustments_to_window( self._get_adjustment_list( asset, self._splits_dict, "SPLITS" ), data, dts, field != 'volume' ) if field != 'volume': self._apply_adjustments_to_window( self._get_adjustment_list( asset, self._mergers_dict, "MERGERS" ), data, dts, True ) self._apply_adjustments_to_window( self._get_adjustment_list( asset, self._dividends_dict, "DIVIDENDS" ), data, dts, True ) if price_adj_factor is not None: data *= price_adj_factor np.around(data, 3, out=data) def _get_daily_window_for_sids( self, assets, field, days_in_window, extra_slot=True): """ Internal method that gets a window of adjusted daily data for a sid and specified date range. Used to support the history API method for daily bars. Parameters ---------- asset : Asset The asset whose data is desired. start_dt: pandas.Timestamp The start of the desired window of data. bar_count: int The number of days of data to return. field: string The specific field to return. "open", "high", "close_price", etc. extra_slot: boolean Whether to allocate an extra slot in the returned numpy array. This extra slot will hold the data for the last partial day. It's much better to create it here than to create a copy of the array later just to add a slot. Returns ------- A numpy array with requested values. Any missing slots filled with nan. """ bar_count = len(days_in_window) # create an np.array of size bar_count if extra_slot: return_array = np.zeros((bar_count + 1, len(assets))) else: return_array = np.zeros((bar_count, len(assets))) if field != "volume": # volumes default to 0, so we don't need to put NaNs in the array return_array[:] = np.NAN if bar_count != 0: data = self._equity_history_loader.history(assets, days_in_window, field) if extra_slot: return_array[:len(return_array) - 1, :] = data else: return_array[:len(data)] = data return return_array @staticmethod def _apply_adjustments_to_window(adjustments_list, window_data, dts_in_window, multiply): if len(adjustments_list) == 0: return # advance idx to the correct spot in the adjustments list, based on # when the window starts idx = 0 while idx < len(adjustments_list) and dts_in_window[0] >\ adjustments_list[idx][0]: idx += 1 # if we've advanced through all the adjustments, then there's nothing # to do. if idx == len(adjustments_list): return while idx < len(adjustments_list): adjustment_to_apply = adjustments_list[idx] if adjustment_to_apply[0] > dts_in_window[-1]: break range_end = dts_in_window.searchsorted(adjustment_to_apply[0]) if multiply: window_data[0:range_end] *= adjustment_to_apply[1] else: window_data[0:range_end] /= adjustment_to_apply[1] idx += 1 def _get_adjustment_list(self, asset, adjustments_dict, table_name): """ Internal method that returns a list of adjustments for the given sid. Parameters ---------- asset : Asset The asset for which to return adjustments. adjustments_dict: dict A dictionary of sid -> list that is used as a cache. table_name: string The table that contains this data in the adjustments db. Returns ------- adjustments: list A list of [multiplier, pd.Timestamp], earliest first """ if self._adjustment_reader is None: return [] sid = int(asset) try: adjustments = adjustments_dict[sid] except KeyError: adjustments = adjustments_dict[sid] = self._adjustment_reader.\ get_adjustments_for_sid(table_name, sid) return adjustments def _check_is_currently_alive(self, asset, dt): sid = int(asset) if sid not in self._asset_start_dates: self._get_asset_start_date(asset) start_date = self._asset_start_dates[sid] if self._asset_start_dates[sid] > dt: raise NoTradeDataAvailableTooEarly( sid=sid, dt=normalize_date(dt), start_dt=start_date ) end_date = self._asset_end_dates[sid] if self._asset_end_dates[sid] < dt: raise NoTradeDataAvailableTooLate( sid=sid, dt=normalize_date(dt), end_dt=end_date ) def _get_asset_start_date(self, asset): self._ensure_asset_dates(asset) return self._asset_start_dates[asset] def _get_asset_end_date(self, asset): self._ensure_asset_dates(asset) return self._asset_end_dates[asset] def _ensure_asset_dates(self, asset): sid = int(asset) if sid not in self._asset_start_dates: if self._first_trading_day is not None: self._asset_start_dates[sid] = \ max(asset.start_date, self._first_trading_day) else: self._asset_start_dates[sid] = asset.start_date self._asset_end_dates[sid] = asset.end_date def get_splits(self, sids, dt): """ Returns any splits for the given sids and the given dt. Parameters ---------- sids : container Sids for which we want splits. dt : pd.Timestamp The date for which we are checking for splits. Note: this is expected to be midnight UTC. Returns ------- splits : list[(int, float)] List of splits, where each split is a (sid, ratio) tuple. """ if self._adjustment_reader is None or not sids: return {} # convert dt to # of seconds since epoch, because that's what we use # in the adjustments db seconds = int(dt.value / 1e9) splits = self._adjustment_reader.conn.execute( "SELECT sid, ratio FROM SPLITS WHERE effective_date = ?", (seconds,)).fetchall() splits = [split for split in splits if split[0] in sids] return splits def get_stock_dividends(self, sid, trading_days): """ Returns all the stock dividends for a specific sid that occur in the given trading range. Parameters ---------- sid: int The asset whose stock dividends should be returned. trading_days: pd.DatetimeIndex The trading range. Returns ------- list: A list of objects with all relevant attributes populated. All timestamp fields are converted to pd.Timestamps. """ if self._adjustment_reader is None: return [] if len(trading_days) == 0: return [] start_dt = trading_days[0].value / 1e9 end_dt = trading_days[-1].value / 1e9 dividends = self._adjustment_reader.conn.execute( "SELECT * FROM stock_dividend_payouts WHERE sid = ? AND " "ex_date > ? AND pay_date < ?", (int(sid), start_dt, end_dt,)).\ fetchall() dividend_info = [] for dividend_tuple in dividends: dividend_info.append({ "declared_date": dividend_tuple[1], "ex_date": pd.Timestamp(dividend_tuple[2], unit="s"), "pay_date": pd.Timestamp(dividend_tuple[3], unit="s"), "payment_sid": dividend_tuple[4], "ratio": dividend_tuple[5], "record_date": pd.Timestamp(dividend_tuple[6], unit="s"), "sid": dividend_tuple[7] }) return dividend_info def contains(self, asset, field): return field in BASE_FIELDS or \ (field in self._augmented_sources_map and asset in self._augmented_sources_map[field]) def get_fetcher_assets(self, dt): """ Returns a list of assets for the current date, as defined by the fetcher data. Returns ------- list: a list of Asset objects. """ # return a list of assets for the current date, as defined by the # fetcher source if self._extra_source_df is None: return [] day = normalize_date(dt) if day in self._extra_source_df.index: assets = self._extra_source_df.loc[day]['sid'] else: return [] if isinstance(assets, pd.Series): return [x for x in assets if isinstance(x, Asset)] else: return [assets] if isinstance(assets, Asset) else [] @weak_lru_cache(20) def _get_minute_count_for_transform(self, ending_minute, days_count): # cache size picked somewhat loosely. this code exists purely to # handle deprecated API. # bars is the number of days desired. we have to translate that # into the number of minutes we want. # we get all the minutes for the last (bars - 1) days, then add # all the minutes so far today. the +2 is to account for ignoring # today, and the previous day, in doing the math. previous_day = self.env.previous_trading_day(ending_minute) days = self.env.days_in_range( self.env.add_trading_days(-days_count + 2, previous_day), previous_day, ) minutes_count = \ sum(210 if day in self.env.early_closes else 390 for day in days) # add the minutes for today today_open = self.env.get_open_and_close(ending_minute)[0] minutes_count += \ ((ending_minute - today_open).total_seconds() // 60) + 1 return minutes_count def get_simple_transform(self, asset, transform_name, dt, data_frequency, bars=None): if transform_name == "returns": # returns is always calculated over the last 2 days, regardless # of the simulation's data frequency. hst = self.get_history_window( [asset], dt, 2, "1d", "price", ffill=True )[asset] return (hst.iloc[-1] - hst.iloc[0]) / hst.iloc[0] if bars is None: raise ValueError("bars cannot be None!") if data_frequency == "minute": freq_str = "1m" calculated_bar_count = self._get_minute_count_for_transform( dt, bars ) else: freq_str = "1d" calculated_bar_count = bars price_arr = self.get_history_window( [asset], dt, calculated_bar_count, freq_str, "price", ffill=True )[asset] if transform_name == "mavg": return nanmean(price_arr) elif transform_name == "stddev": return nanstd(price_arr, ddof=1) elif transform_name == "vwap": volume_arr = self.get_history_window( [asset], dt, calculated_bar_count, freq_str, "volume", ffill=True )[asset] vol_sum = nansum(volume_arr) try: ret = nansum(price_arr * volume_arr) / vol_sum except ZeroDivisionError: ret = np.nan return ret
apache-2.0
cactusbin/nyt
matplotlib/lib/matplotlib/tests/test_text.py
2
6893
from __future__ import print_function import numpy as np import matplotlib from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup import matplotlib.pyplot as plt import warnings from nose.tools import with_setup @image_comparison(baseline_images=['font_styles']) def test_font_styles(): from matplotlib import _get_data_path data_path = _get_data_path() def find_matplotlib_font(**kw): prop = FontProperties(**kw) path = findfont(prop, directory=data_path) return FontProperties(fname=path) from matplotlib.font_manager import FontProperties, findfont warnings.filterwarnings('ignore','findfont: Font family \[\'Foo\'\] '+ \ 'not found. Falling back to .', UserWarning, module='matplotlib.font_manager') fig = plt.figure() ax = plt.subplot( 1, 1, 1 ) normalFont = find_matplotlib_font( family = "sans-serif", style = "normal", variant = "normal", size = 14, ) ax.annotate( "Normal Font", (0.1, 0.1), xycoords='axes fraction', fontproperties = normalFont ) boldFont = find_matplotlib_font( family = "Foo", style = "normal", variant = "normal", weight = "bold", stretch = 500, size = 14, ) ax.annotate( "Bold Font", (0.1, 0.2), xycoords='axes fraction', fontproperties = boldFont ) boldItemFont = find_matplotlib_font( family = "sans serif", style = "italic", variant = "normal", weight = 750, stretch = 500, size = 14, ) ax.annotate( "Bold Italic Font", (0.1, 0.3), xycoords='axes fraction', fontproperties = boldItemFont ) lightFont = find_matplotlib_font( family = "sans-serif", style = "normal", variant = "normal", weight = 200, stretch = 500, size = 14, ) ax.annotate( "Light Font", (0.1, 0.4), xycoords='axes fraction', fontproperties = lightFont ) condensedFont = find_matplotlib_font( family = "sans-serif", style = "normal", variant = "normal", weight = 500, stretch = 100, size = 14, ) ax.annotate( "Condensed Font", (0.1, 0.5), xycoords='axes fraction', fontproperties = condensedFont ) ax.set_xticks([]) ax.set_yticks([]) @image_comparison(baseline_images=['multiline']) def test_multiline(): fig = plt.figure() ax = plt.subplot(1, 1, 1) ax.set_title("multiline\ntext alignment") plt.text(0.2, 0.5, "TpTpTp\n$M$\nTpTpTp", size=20, ha="center", va="top") plt.text(0.5, 0.5, "TpTpTp\n$M^{M^{M^{M}}}$\nTpTpTp", size=20, ha="center", va="top") plt.text(0.8, 0.5, "TpTpTp\n$M_{q_{q_{q}}}$\nTpTpTp", size=20, ha="center", va="top") plt.xlim(0, 1) plt.ylim(0, 0.8) ax.set_xticks([]) ax.set_yticks([]) @image_comparison(baseline_images=['antialiased'], extensions=['png']) def test_antialiasing(): matplotlib.rcParams['text.antialiased'] = True fig = plt.figure(figsize=(5.25, 0.75)) fig.text(0.5, 0.75, "antialiased", horizontalalignment='center', verticalalignment='center') fig.text(0.5, 0.25, "$\sqrt{x}$", horizontalalignment='center', verticalalignment='center') # NOTE: We don't need to restore the rcParams here, because the # test cleanup will do it for us. In fact, if we do it here, it # will turn antialiasing back off before the images are actually # rendered. def test_afm_kerning(): from matplotlib.afm import AFM from matplotlib.font_manager import findfont fn = findfont("Helvetica", fontext="afm") with open(fn, 'rb') as fh: afm = AFM(fh) assert afm.string_width_height('VAVAVAVAVAVA') == (7174.0, 718) @image_comparison(baseline_images=['text_contains'], extensions=['png']) def test_contains(): import matplotlib.backend_bases as mbackend fig = plt.figure() ax = plt.axes() mevent = mbackend.MouseEvent('button_press_event', fig.canvas, 0.5, 0.5, 1, None) xs = np.linspace(0.25, 0.75, 30) ys = np.linspace(0.25, 0.75, 30) xs, ys = np.meshgrid(xs, ys) txt = plt.text(0.48, 0.52, 'hello world', ha='center', fontsize=30, rotation=30) # uncomment to draw the text's bounding box # txt.set_bbox(dict(edgecolor='black', facecolor='none')) # draw the text. This is important, as the contains method can only work # when a renderer exists. plt.draw() for x, y in zip(xs.flat, ys.flat): mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y]) contains, _ = txt.contains(mevent) color = 'yellow' if contains else 'red' # capture the viewLim, plot a point, and reset the viewLim vl = ax.viewLim.frozen() ax.plot(x, y, 'o', color=color) ax.viewLim.set(vl) @image_comparison(baseline_images=['titles']) def test_titles(): # left and right side titles fig = plt.figure() ax = plt.subplot(1, 1, 1) ax.set_title("left title", loc="left") ax.set_title("right title", loc="right") ax.set_xticks([]) ax.set_yticks([]) @image_comparison(baseline_images=['text_alignment']) def test_alignment(): fig = plt.figure() ax = plt.subplot(1, 1, 1) x = 0.1 for rotation in (0, 30): for alignment in ('top', 'bottom', 'baseline', 'center'): ax.text(x, 0.5, alignment + " Tj", va=alignment, rotation=rotation, bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5)) ax.text(x, 1.0, r'$\sum_{i=0}^{j}$', va=alignment, rotation=rotation) x += 0.1 ax.plot([0, 1], [0.5, 0.5]) ax.plot([0, 1], [1.0, 1.0]) ax.set_xlim([0, 1]) ax.set_ylim([0, 1.5]) ax.set_xticks([]) ax.set_yticks([])
unlicense
arabenjamin/scikit-learn
sklearn/ensemble/tests/test_base.py
284
1328
""" Testing for the base module (sklearn.ensemble.base). """ # Authors: Gilles Louppe # License: BSD 3 clause from numpy.testing import assert_equal from nose.tools import assert_true from sklearn.utils.testing import assert_raise_message from sklearn.datasets import load_iris from sklearn.ensemble import BaggingClassifier from sklearn.linear_model import Perceptron def test_base(): # Check BaseEnsemble methods. ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=3) iris = load_iris() ensemble.fit(iris.data, iris.target) ensemble.estimators_ = [] # empty the list and create estimators manually ensemble._make_estimator() ensemble._make_estimator() ensemble._make_estimator() ensemble._make_estimator(append=False) assert_equal(3, len(ensemble)) assert_equal(3, len(ensemble.estimators_)) assert_true(isinstance(ensemble[0], Perceptron)) def test_base_zero_n_estimators(): # Check that instantiating a BaseEnsemble with n_estimators<=0 raises # a ValueError. ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0) iris = load_iris() assert_raise_message(ValueError, "n_estimators must be greater than zero, got 0.", ensemble.fit, iris.data, iris.target)
bsd-3-clause
montagnero/political-affiliation-prediction
newsreader.py
2
11936
# -*- coding: utf-8 -*- from sklearn.decomposition import KernelPCA from sklearn.metrics.pairwise import pairwise_distances from scipy.stats.mstats import zscore import glob import json import re import datetime import os import cPickle import codecs import itertools from sklearn.feature_extraction.text import TfidfVectorizer from scipy import double,triu,ones,hstack,arange,reshape,zeros,setdiff1d,array,zeros,eye,argmax,percentile def get_news(sources=['spiegel','faz','welt','zeit'], folder='model'): ''' Collects all news articles from political ressort of major German newspapers Articles are transformed to BoW vectors and assigned to a political party For better visualization, articles' BoW vectors are also clustered into topics INPUT folder the model folder containing classifier and BoW transformer sources a list of strings for each newspaper for which a crawl is implemented default ['zeit','sz'] ''' import classifier from bs4 import BeautifulSoup from api import fetch_url import urllib2 news = dict([(source,[]) for source in sources]) # the classifier for prediction of political affiliation clf = classifier.Classifier(folder=folder) for source in sources: if source is 'spiegel': # fetching articles from sueddeutsche.de/politik url = 'http://www.spiegel.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("div", { "class" : "teaser" }) urls = ['http://www.spiegel.de'+a.findNext('a')['href'] for a in titles] if source is 'faz': # fetching articles from sueddeutsche.de/politik url = 'http://www.faz.net/aktuell/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("a", { "class" : "TeaserHeadLink" }) urls = ['http://www.faz.net'+a['href'] for a in titles] if source is 'welt': # fetching articles from sueddeutsche.de/politik url = 'http://www.welt.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("a", { "class" : "as_teaser-kicker" }) urls = [a['href'] for a in titles] if source is 'sz-without-readability': # fetching articles from sueddeutsche.de/politik url = 'http://www.sueddeutsche.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("div", { "class" : "teaser" }) urls = [a.findNext('a')['href'] for a in titles] if source is 'zeit': # fetching articles from zeit.de/politik url = 'http://www.zeit.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("span", { "class" : "supertitle" }) urls = [a.parent['href'] for a in titles if a.parent['href'].find('/2015-')>0] print "Found %d articles on %s"%(len(urls),url) # predict party from url for this source print "Predicting %s"%source articles = [] for url in urls: try: title,text = fetch_url(url) prediction = clf.predict(text) prediction['url'] = url articles.append((title,prediction)) except: print('Could not get text from %s'%url) pass news[source] = dict(articles) # save results datestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") open(folder+'/news-%s'%(datestr) + '.json', 'wb').write(json.dumps(news,ensure_ascii=False).encode('utf8')) def all_saved_news(folder='model'): import glob from string import digits # get just the most recent news articles file (assuming date label ordering) news = json.load(open(glob.glob(folder+'/news*.json')[-1],"r")) # collect text data from all articles articles, data = [], [] for source in news.keys(): for title, article in news[source].items(): # remove numbers for d in digits: article['text'] = article['text'].replace(d,'') data.append(article['text']) predictions = [prediction['probability'] for prediction in article['prediction']] articles.append({ 'source':source, 'title':title, 'url':article['url'], 'prediction':article['prediction'], 'predictedLabel':article['prediction'][argmax(predictions)]['party'] }) return articles, data def pairwise_dists(data, nneighbors=10, folder='model', dist='l2'): ''' Computes pairwise distances between bag-of-words vectors of articles INPUT folder model folder nneighbors number of closest neighbors to include in distance list ''' stopwords = codecs.open("stopwords.txt", "r", encoding="utf-8", errors='ignore').readlines()[5:] stops = map(lambda x:x.lower().strip(),stopwords) # using now stopwords and filtering out digits bow = TfidfVectorizer(min_df=2,stop_words=stops) X = bow.fit_transform(data) print 'Computing %s pairwise distances'%dist # KPCA transform bow vectors if dist is 'l2_kpca_zscore': K = pairwise_distances(X,metric='l2',n_jobs=1) perc = 50.0 width = percentile(K.flatten(),perc) Xc = zscore(KernelPCA(n_components=50,kernel='rbf',gamma=width).fit_transform(X)) K = pairwise_distances(Xc,metric='l2',n_jobs=1) elif dist is 'l2_kpca': K = pairwise_distances(X,metric='l2',n_jobs=1) perc = 100./len(data) width = percentile(K.flatten(),perc) Xc = KernelPCA(n_components=50,kernel='rbf',gamma=width).fit_transform(X) K = pairwise_distances(Xc,metric='l2',n_jobs=1) elif dist is 'l2': K = pairwise_distances(X,metric='l2',n_jobs=1) elif dist is 'l1': K = pairwise_distances(X,metric='l1',n_jobs=1) # collect closest neighbors distances = [] for urlidx in range(len(data)): idx = (K[urlidx,:]).argsort()[1:nneighbors+1] for sidx in idx: distances.append([urlidx,sidx,(idx==sidx).nonzero()[0][0]]) return distances def load_sentiment(negative='SentiWS_v1.8c/SentiWS_v1.8c_Negative.txt',\ positive='SentiWS_v1.8c/SentiWS_v1.8c_Positive.txt'): words = dict() for line in open(negative).readlines(): parts = line.strip('\n').split('\t') words[parts[0].split('|')[0]] = double(parts[1]) if len(parts)>2: for inflection in parts[2].strip('\n').split(','): words[inflection] = double(parts[1]) for line in open(positive).readlines(): parts = line.strip('\n').split('\t') words[parts[0].split('|')[0]] = double(parts[1]) if len(parts)>2: for inflection in parts[2].strip('\n').split(','): words[inflection] = double(parts[1]) return words def get_sentiments(data): # filtering out some noise words stops = map(lambda x:x.lower().strip(),open('stopwords.txt').readlines()[6:]) # vectorize non-stopwords bow = TfidfVectorizer(min_df=2,stop_words=stops) X = bow.fit_transform(data) # map sentiment vector to bow space words = load_sentiment() sentiment_vec = zeros(X.shape[1]) for key in words.keys(): if bow.vocabulary_.has_key(key): sentiment_vec[bow.vocabulary_[key]] = words[key] # compute sentiments return X.dot(sentiment_vec) def kpca_cluster(data,nclusters=100,ncomponents=40,topwhat=10,zscored=False): ''' Computes clustering of bag-of-words vectors of articles INPUT folder model folder nclusters number of clusters ''' from sklearn.cluster import KMeans # filtering out some noise words stops = map(lambda x:x.lower().strip(),open('stopwords.txt').readlines()[6:]) # vectorize non-stopwords bow = TfidfVectorizer(min_df=2,stop_words=stops) X = bow.fit_transform(data) # creating bow-index-to-word map idx2word = dict(zip(bow.vocabulary_.values(),bow.vocabulary_.keys())) # using now stopwords and filtering out digits print 'Computing pairwise distances' K = pairwise_distances(X,metric='l2',n_jobs=1) perc = 50.0 width = percentile(K.flatten(),perc) # KPCA transform bow vectors Xc = KernelPCA(n_components=ncomponents,kernel='rbf',gamma=width).fit_transform(X) if zscored: Xc = zscore(Xc) # compute clusters km = KMeans(n_clusters=nclusters).fit(Xc) Xc = km.predict(Xc) clusters = [] for icluster in range(nclusters): nmembers = (Xc==icluster).sum() if True:#nmembers < len(data) / 5.0 and nmembers > 1: # only group clusters big enough but not too big members = (Xc==icluster).nonzero()[0] topwordidx = array(X[members,:].sum(axis=0))[0].argsort()[-topwhat:][::-1] topwords = ' '.join([idx2word[wi] for wi in topwordidx]) meanDist = triu(pairwise_distances(X[members,:],metric='l2',n_jobs=1)).sum() meanDist = meanDist / (len(members) + (len(members)**2 - len(members))/2.0) # print u'Cluster %d'%icluster + u' %d members'%nmembers + u' mean Distance %f'%meanDist + u'\n\t'+topwords clusters.append({ 'name':'Cluster-%d'%icluster, 'description': topwords, 'members': list(members), 'meanL2Distances': meanDist }) return clusters def party_cluster(articles): clusters = [] keyf = lambda a: a[1]['predictedLabel'] for k, group in itertools.groupby(sorted(enumerate(articles), key=keyf), keyf): clusters.append({ 'name': k, 'description': k, 'members': [index_article_tuple[0] for index_article_tuple in group] }) return clusters def write_distances_json(folder='model'): articles, data = all_saved_news(folder) dists = ['l2_kpca'] distances_json = { 'articles': articles, 'sentiments': json.dumps(get_sentiments(data).tolist()), 'distances': [ { 'name': dist, 'distances': pairwise_dists(data,dist = dist) } for dist in dists ], 'clusterings': [ { 'name': 'Parteivorhersage', 'clusters': party_cluster(articles) }, { 'name': 'Ähnlichkeit', 'clusters': kpca_cluster(data,nclusters=len(articles)/2,ncomponents=40,zscored=False) }, ] } # save article with party prediction and distances to closest articles datestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") open(folder+'/distances-%s'%(datestr)+'.json', 'wb').write(json.dumps(distances_json)) # also save that latest version for the visualization open(folder+'/distances.json', 'wb').write(json.dumps(distances_json)) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(\ description='Downloads, transforms and clusters news articles') parser.add_argument('-f','--folder',help='Folder to store text files [./model]',\ default='model') parser.add_argument('-d','--download',help='If files should be downloaded',\ action='store_true', default=False) parser.add_argument('-p','--distances',help='If pairwise distances of text should be computed',\ action='store_true', default=False) args = vars(parser.parse_args()) if not os.path.isdir(args['folder']): os.mkdir(args['folder']) if args['download']: get_news(folder=args['folder']) if args['distances']: write_distances_json(folder=args['folder'])
mit
ilo10/scikit-learn
examples/plot_johnson_lindenstrauss_bound.py
134
7452
""" ===================================================================== The Johnson-Lindenstrauss bound for embedding with random projections ===================================================================== The `Johnson-Lindenstrauss lemma`_ states that any high dimensional dataset can be randomly projected into a lower dimensional Euclidean space while controlling the distortion in the pairwise distances. .. _`Johnson-Lindenstrauss lemma`: http://en.wikipedia.org/wiki/Johnson%E2%80%93Lindenstrauss_lemma Theoretical bounds ================== The distortion introduced by a random projection `p` is asserted by the fact that `p` is defining an eps-embedding with good probability as defined by: (1 - eps) ||u - v||^2 < ||p(u) - p(v)||^2 < (1 + eps) ||u - v||^2 Where u and v are any rows taken from a dataset of shape [n_samples, n_features] and p is a projection by a random Gaussian N(0, 1) matrix with shape [n_components, n_features] (or a sparse Achlioptas matrix). The minimum number of components to guarantees the eps-embedding is given by: n_components >= 4 log(n_samples) / (eps^2 / 2 - eps^3 / 3) The first plot shows that with an increasing number of samples ``n_samples``, the minimal number of dimensions ``n_components`` increased logarithmically in order to guarantee an ``eps``-embedding. The second plot shows that an increase of the admissible distortion ``eps`` allows to reduce drastically the minimal number of dimensions ``n_components`` for a given number of samples ``n_samples`` Empirical validation ==================== We validate the above bounds on the the digits dataset or on the 20 newsgroups text document (TF-IDF word frequencies) dataset: - for the digits dataset, some 8x8 gray level pixels data for 500 handwritten digits pictures are randomly projected to spaces for various larger number of dimensions ``n_components``. - for the 20 newsgroups dataset some 500 documents with 100k features in total are projected using a sparse random matrix to smaller euclidean spaces with various values for the target number of dimensions ``n_components``. The default dataset is the digits dataset. To run the example on the twenty newsgroups dataset, pass the --twenty-newsgroups command line argument to this script. For each value of ``n_components``, we plot: - 2D distribution of sample pairs with pairwise distances in original and projected spaces as x and y axis respectively. - 1D histogram of the ratio of those distances (projected / original). We can see that for low values of ``n_components`` the distribution is wide with many distorted pairs and a skewed distribution (due to the hard limit of zero ratio on the left as distances are always positives) while for larger values of n_components the distortion is controlled and the distances are well preserved by the random projection. Remarks ======= According to the JL lemma, projecting 500 samples without too much distortion will require at least several thousands dimensions, irrespective of the number of features of the original dataset. Hence using random projections on the digits dataset which only has 64 features in the input space does not make sense: it does not allow for dimensionality reduction in this case. On the twenty newsgroups on the other hand the dimensionality can be decreased from 56436 down to 10000 while reasonably preserving pairwise distances. """ print(__doc__) import sys from time import time import numpy as np import matplotlib.pyplot as plt from sklearn.random_projection import johnson_lindenstrauss_min_dim from sklearn.random_projection import SparseRandomProjection from sklearn.datasets import fetch_20newsgroups_vectorized from sklearn.datasets import load_digits from sklearn.metrics.pairwise import euclidean_distances # Part 1: plot the theoretical dependency between n_components_min and # n_samples # range of admissible distortions eps_range = np.linspace(0.1, 0.99, 5) colors = plt.cm.Blues(np.linspace(0.3, 1.0, len(eps_range))) # range of number of samples (observation) to embed n_samples_range = np.logspace(1, 9, 9) plt.figure() for eps, color in zip(eps_range, colors): min_n_components = johnson_lindenstrauss_min_dim(n_samples_range, eps=eps) plt.loglog(n_samples_range, min_n_components, color=color) plt.legend(["eps = %0.1f" % eps for eps in eps_range], loc="lower right") plt.xlabel("Number of observations to eps-embed") plt.ylabel("Minimum number of dimensions") plt.title("Johnson-Lindenstrauss bounds:\nn_samples vs n_components") # range of admissible distortions eps_range = np.linspace(0.01, 0.99, 100) # range of number of samples (observation) to embed n_samples_range = np.logspace(2, 6, 5) colors = plt.cm.Blues(np.linspace(0.3, 1.0, len(n_samples_range))) plt.figure() for n_samples, color in zip(n_samples_range, colors): min_n_components = johnson_lindenstrauss_min_dim(n_samples, eps=eps_range) plt.semilogy(eps_range, min_n_components, color=color) plt.legend(["n_samples = %d" % n for n in n_samples_range], loc="upper right") plt.xlabel("Distortion eps") plt.ylabel("Minimum number of dimensions") plt.title("Johnson-Lindenstrauss bounds:\nn_components vs eps") # Part 2: perform sparse random projection of some digits images which are # quite low dimensional and dense or documents of the 20 newsgroups dataset # which is both high dimensional and sparse if '--twenty-newsgroups' in sys.argv: # Need an internet connection hence not enabled by default data = fetch_20newsgroups_vectorized().data[:500] else: data = load_digits().data[:500] n_samples, n_features = data.shape print("Embedding %d samples with dim %d using various random projections" % (n_samples, n_features)) n_components_range = np.array([300, 1000, 10000]) dists = euclidean_distances(data, squared=True).ravel() # select only non-identical samples pairs nonzero = dists != 0 dists = dists[nonzero] for n_components in n_components_range: t0 = time() rp = SparseRandomProjection(n_components=n_components) projected_data = rp.fit_transform(data) print("Projected %d samples from %d to %d in %0.3fs" % (n_samples, n_features, n_components, time() - t0)) if hasattr(rp, 'components_'): n_bytes = rp.components_.data.nbytes n_bytes += rp.components_.indices.nbytes print("Random matrix with size: %0.3fMB" % (n_bytes / 1e6)) projected_dists = euclidean_distances( projected_data, squared=True).ravel()[nonzero] plt.figure() plt.hexbin(dists, projected_dists, gridsize=100, cmap=plt.cm.PuBu) plt.xlabel("Pairwise squared distances in original space") plt.ylabel("Pairwise squared distances in projected space") plt.title("Pairwise distances distribution for n_components=%d" % n_components) cb = plt.colorbar() cb.set_label('Sample pairs counts') rates = projected_dists / dists print("Mean distances rate: %0.2f (%0.2f)" % (np.mean(rates), np.std(rates))) plt.figure() plt.hist(rates, bins=50, normed=True, range=(0., 2.)) plt.xlabel("Squared distances rate: projected / original") plt.ylabel("Distribution of samples pairs") plt.title("Histogram of pairwise distance rates for n_components=%d" % n_components) # TODO: compute the expected value of eps and add them to the previous plot # as vertical lines / region plt.show()
bsd-3-clause
waynenilsen/statsmodels
statsmodels/tsa/base/tests/test_base.py
27
2106
import numpy as np from pandas import Series from pandas import date_range from statsmodels.tsa.base.tsa_model import TimeSeriesModel import numpy.testing as npt from statsmodels.tools.testing import assert_equal def test_pandas_nodates_index(): from statsmodels.datasets import sunspots y = sunspots.load_pandas().data.SUNACTIVITY npt.assert_raises(ValueError, TimeSeriesModel, y) def test_predict_freq(): # test that predicted dates have same frequency x = np.arange(1,36.) # there's a bug in pandas up to 0.10.2 for YearBegin #dates = date_range("1972-4-1", "2007-4-1", freq="AS-APR") dates = date_range("1972-4-30", "2006-4-30", freq="A-APR") series = Series(x, index=dates) model = TimeSeriesModel(series) #npt.assert_(model.data.freq == "AS-APR") npt.assert_(model.data.freq == "A-APR") start = model._get_predict_start("2006-4-30") end = model._get_predict_end("2016-4-30") model._make_predict_dates() predict_dates = model.data.predict_dates #expected_dates = date_range("2006-12-31", "2016-12-31", # freq="AS-APR") expected_dates = date_range("2006-4-30", "2016-4-30", freq="A-APR") assert_equal(predict_dates, expected_dates) #ptesting.assert_series_equal(predict_dates, expected_dates) def test_keyerror_start_date(): x = np.arange(1,36.) from pandas import date_range # there's a bug in pandas up to 0.10.2 for YearBegin #dates = date_range("1972-4-1", "2007-4-1", freq="AS-APR") dates = date_range("1972-4-30", "2006-4-30", freq="A-APR") series = Series(x, index=dates) model = TimeSeriesModel(series) npt.assert_raises(ValueError, model._get_predict_start, "1970-4-30") def test_period_index(): # test 1285 from pandas import PeriodIndex, TimeSeries dates = PeriodIndex(start="1/1/1990", periods=20, freq="M") x = np.arange(1, 21.) model = TimeSeriesModel(Series(x, index=dates)) npt.assert_(model.data.freq == "M") model = TimeSeriesModel(TimeSeries(x, index=dates)) npt.assert_(model.data.freq == "M")
bsd-3-clause
ml-lab/pylearn2
pylearn2/models/tests/test_s3c_inference.py
4
14275
from pylearn2.models.s3c import S3C from pylearn2.models.s3c import E_Step_Scan from pylearn2.models.s3c import Grad_M_Step from pylearn2.models.s3c import E_Step from theano import function import numpy as np import theano.tensor as T from theano import config #from pylearn2.utils import serial import warnings def broadcast(mat, shape_0): rval = mat if mat.shape[0] != shape_0: assert mat.shape[0] == 1 rval = np.zeros((shape_0, mat.shape[1]),dtype=mat.dtype) for i in xrange(shape_0): rval[i,:] = mat[0,:] return rval class Test_S3C_Inference: def setUp(self): # Temporarily change config.floatX to float64, as s3c inference # tests currently fail due to numerical issues for float32. self.prev_floatX = config.floatX config.floatX = 'float64' def tearDown(self): # Restore previous value of floatX config.floatX = self.prev_floatX def __init__(self): """ gets a small batch of data sets up an S3C model """ # We also have to change the value of config.floatX in __init__. self.prev_floatX = config.floatX config.floatX = 'float64' try: self.tol = 1e-5 #dataset = serial.load('${PYLEARN2_DATA_PATH}/stl10/stl10_patches/data.pkl') #X = dataset.get_batch_design(1000) #X = X[:,0:5] X = np.random.RandomState([1,2,3]).randn(1000,5) X -= X.mean() X /= X.std() m, D = X.shape N = 5 #don't give the model an e_step or learning rate so it won't spend years compiling a learn_func self.model = S3C(nvis = D, nhid = N, irange = .1, init_bias_hid = 0., init_B = 3., min_B = 1e-8, max_B = 1000., init_alpha = 1., min_alpha = 1e-8, max_alpha = 1000., init_mu = 1., e_step = None, m_step = Grad_M_Step(), min_bias_hid = -1e30, max_bias_hid = 1e30, ) self.model.make_pseudoparams() self.h_new_coeff_schedule = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1. ] self.e_step = E_Step_Scan(h_new_coeff_schedule = self.h_new_coeff_schedule) self.e_step.register_model(self.model) self.X = X self.N = N self.m = m finally: config.floatX = self.prev_floatX def test_match_unrolled(self): """ tests that inference with scan matches result using unrolled loops """ unrolled_e_step = E_Step(h_new_coeff_schedule = self.h_new_coeff_schedule) unrolled_e_step.register_model(self.model) V = T.matrix() scan_result = self.e_step.infer(V) unrolled_result = unrolled_e_step.infer(V) outputs = [] for key in scan_result: outputs.append(scan_result[key]) outputs.append(unrolled_result[key]) f = function([V], outputs) outputs = f(self.X) assert len(outputs) % 2 == 0 for i in xrange(0,len(outputs),2): assert np.allclose(outputs[i],outputs[i+1]) def test_grad_s(self): "tests that the gradients with respect to s_i are 0 after doing a mean field update of s_i " model = self.model e_step = self.e_step X = self.X assert X.shape[0] == self.m model.test_batch_size = X.shape[0] init_H = e_step.init_H_hat(V = X) init_Mu1 = e_step.init_S_hat(V = X) prev_setting = config.compute_test_value config.compute_test_value= 'off' H, Mu1 = function([], outputs=[init_H, init_Mu1])() config.compute_test_value = prev_setting H = broadcast(H, self.m) Mu1 = broadcast(Mu1, self.m) H = np.cast[config.floatX](self.model.rng.uniform(0.,1.,H.shape)) Mu1 = np.cast[config.floatX](self.model.rng.uniform(-5.,5.,Mu1.shape)) H_var = T.matrix(name='H_var') H_var.tag.test_value = H Mu1_var = T.matrix(name='Mu1_var') Mu1_var.tag.test_value = Mu1 idx = T.iscalar() idx.tag.test_value = 0 S = e_step.infer_S_hat(V = X, H_hat = H_var, S_hat = Mu1_var) s_idx = S[:,idx] s_i_func = function([H_var,Mu1_var,idx],s_idx) sigma0 = 1. / model.alpha Sigma1 = e_step.infer_var_s1_hat() mu0 = T.zeros_like(model.mu) #by truncated KL, I mean that I am dropping terms that don't depend on H and Mu1 # (they don't affect the outcome of this test and some of them are intractable ) trunc_kl = - model.entropy_hs(H_hat = H_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) + \ model.expected_energy_vhs(V = X, H_hat = H_var, S_hat = Mu1_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) grad_Mu1 = T.grad(trunc_kl.sum(), Mu1_var) grad_Mu1_idx = grad_Mu1[:,idx] grad_func = function([H_var, Mu1_var, idx], grad_Mu1_idx) for i in xrange(self.N): Mu1[:,i] = s_i_func(H, Mu1, i) g = grad_func(H,Mu1,i) assert not np.any(np.isnan(g)) g_abs_max = np.abs(g).max() if g_abs_max > self.tol: raise Exception('after mean field step, gradient of kl divergence wrt mean field parameter should be 0, but here the max magnitude of a gradient element is '+str(g_abs_max)+' after updating s_'+str(i)) def test_value_s(self): "tests that the value of the kl divergence decreases with each update to s_i " model = self.model e_step = self.e_step X = self.X assert X.shape[0] == self.m init_H = e_step.init_H_hat(V = X) init_Mu1 = e_step.init_S_hat(V = X) prev_setting = config.compute_test_value config.compute_test_value= 'off' H, Mu1 = function([], outputs=[init_H, init_Mu1])() config.compute_test_value = prev_setting H = broadcast(H, self.m) Mu1 = broadcast(Mu1, self.m) H = np.cast[config.floatX](self.model.rng.uniform(0.,1.,H.shape)) Mu1 = np.cast[config.floatX](self.model.rng.uniform(-5.,5.,Mu1.shape)) H_var = T.matrix(name='H_var') H_var.tag.test_value = H Mu1_var = T.matrix(name='Mu1_var') Mu1_var.tag.test_value = Mu1 idx = T.iscalar() idx.tag.test_value = 0 S = e_step.infer_S_hat( V = X, H_hat = H_var, S_hat = Mu1_var) s_idx = S[:,idx] s_i_func = function([H_var,Mu1_var,idx],s_idx) sigma0 = 1. / model.alpha Sigma1 = e_step.infer_var_s1_hat() mu0 = T.zeros_like(model.mu) #by truncated KL, I mean that I am dropping terms that don't depend on H and Mu1 # (they don't affect the outcome of this test and some of them are intractable ) trunc_kl = - model.entropy_hs(H_hat = H_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) + \ model.expected_energy_vhs(V = X, H_hat = H_var, S_hat = Mu1_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) trunc_kl_func = function([H_var, Mu1_var], trunc_kl) for i in xrange(self.N): prev_kl = trunc_kl_func(H,Mu1) Mu1[:,i] = s_i_func(H, Mu1, i) new_kl = trunc_kl_func(H,Mu1) increase = new_kl - prev_kl mx = increase.max() if mx > 1e-3: raise Exception('after mean field step in s, kl divergence should decrease, but some elements increased by as much as '+str(mx)+' after updating s_'+str(i)) def test_grad_h(self): "tests that the gradients with respect to h_i are 0 after doing a mean field update of h_i " model = self.model e_step = self.e_step X = self.X assert X.shape[0] == self.m init_H = e_step.init_H_hat(V = X) init_Mu1 = e_step.init_S_hat(V = X) prev_setting = config.compute_test_value config.compute_test_value= 'off' H, Mu1 = function([], outputs=[init_H, init_Mu1])() config.compute_test_value = prev_setting H = broadcast(H, self.m) Mu1 = broadcast(Mu1, self.m) H = np.cast[config.floatX](self.model.rng.uniform(0.,1.,H.shape)) Mu1 = np.cast[config.floatX](self.model.rng.uniform(-5.,5.,Mu1.shape)) H_var = T.matrix(name='H_var') H_var.tag.test_value = H Mu1_var = T.matrix(name='Mu1_var') Mu1_var.tag.test_value = Mu1 idx = T.iscalar() idx.tag.test_value = 0 new_H = e_step.infer_H_hat(V = X, H_hat = H_var, S_hat = Mu1_var) h_idx = new_H[:,idx] updates_func = function([H_var,Mu1_var,idx], h_idx) sigma0 = 1. / model.alpha Sigma1 = e_step.infer_var_s1_hat() mu0 = T.zeros_like(model.mu) #by truncated KL, I mean that I am dropping terms that don't depend on H and Mu1 # (they don't affect the outcome of this test and some of them are intractable ) trunc_kl = - model.entropy_hs(H_hat = H_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) + \ model.expected_energy_vhs(V = X, H_hat = H_var, S_hat = Mu1_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) grad_H = T.grad(trunc_kl.sum(), H_var) assert len(grad_H.type.broadcastable) == 2 #from theano.printing import min_informative_str #print min_informative_str(grad_H) #grad_H = Print('grad_H')(grad_H) #grad_H_idx = grad_H[:,idx] grad_func = function([H_var, Mu1_var], grad_H) failed = False for i in xrange(self.N): rval = updates_func(H, Mu1, i) H[:,i] = rval g = grad_func(H,Mu1)[:,i] assert not np.any(np.isnan(g)) g_abs_max = np.abs(g).max() if g_abs_max > self.tol: #print "new values of H" #print H[:,i] #print "gradient on new values of H" #print g failed = True print 'iteration ',i #print 'max value of new H: ',H[:,i].max() #print 'H for failing g: ' failing_h = H[np.abs(g) > self.tol, i] #print failing_h #from matplotlib import pyplot as plt #plt.scatter(H[:,i],g) #plt.show() #ignore failures extremely close to h=1 high_mask = failing_h > .001 low_mask = failing_h < .999 mask = high_mask * low_mask print 'masked failures: ',mask.shape[0],' err ',g_abs_max if mask.sum() > 0: print 'failing h passing the range mask' print failing_h[ mask.astype(bool) ] raise Exception('after mean field step, gradient of kl divergence' ' wrt freshly updated variational parameter should be 0, ' 'but here the max magnitude of a gradient element is ' +str(g_abs_max)+' after updating h_'+str(i)) #assert not failed def test_value_h(self): "tests that the value of the kl divergence decreases with each update to h_i " model = self.model e_step = self.e_step X = self.X assert X.shape[0] == self.m init_H = e_step.init_H_hat(V = X) init_Mu1 = e_step.init_S_hat(V = X) prev_setting = config.compute_test_value config.compute_test_value= 'off' H, Mu1 = function([], outputs=[init_H, init_Mu1])() config.compute_test_value = prev_setting H = broadcast(H, self.m) Mu1 = broadcast(Mu1, self.m) H = np.cast[config.floatX](self.model.rng.uniform(0.,1.,H.shape)) Mu1 = np.cast[config.floatX](self.model.rng.uniform(-5.,5.,Mu1.shape)) H_var = T.matrix(name='H_var') H_var.tag.test_value = H Mu1_var = T.matrix(name='Mu1_var') Mu1_var.tag.test_value = Mu1 idx = T.iscalar() idx.tag.test_value = 0 newH = e_step.infer_H_hat(V = X, H_hat = H_var, S_hat = Mu1_var) h_idx = newH[:,idx] h_i_func = function([H_var,Mu1_var,idx],h_idx) sigma0 = 1. / model.alpha Sigma1 = e_step.infer_var_s1_hat() mu0 = T.zeros_like(model.mu) #by truncated KL, I mean that I am dropping terms that don't depend on H and Mu1 # (they don't affect the outcome of this test and some of them are intractable ) trunc_kl = - model.entropy_hs(H_hat = H_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) + \ model.expected_energy_vhs(V = X, H_hat = H_var, S_hat = Mu1_var, var_s0_hat = sigma0, var_s1_hat = Sigma1) trunc_kl_func = function([H_var, Mu1_var], trunc_kl) for i in xrange(self.N): prev_kl = trunc_kl_func(H,Mu1) H[:,i] = h_i_func(H, Mu1, i) #we don't update mu, the whole point of the split e step is we don't have to new_kl = trunc_kl_func(H,Mu1) increase = new_kl - prev_kl print 'failures after iteration ',i,': ',(increase > self.tol).sum() mx = increase.max() if mx > 1e-4: print 'increase amounts of failing examples:' print increase[increase > self.tol] print 'failing H:' print H[increase > self.tol,:] print 'failing Mu1:' print Mu1[increase > self.tol,:] print 'failing V:' print X[increase > self.tol,:] raise Exception('after mean field step in h, kl divergence should decrease, but some elements increased by as much as '+str(mx)+' after updating h_'+str(i)) if __name__ == '__main__': obj = Test_S3C_Inference() #obj.test_grad_h() #obj.test_grad_s() #obj.test_value_s() obj.test_value_h()
bsd-3-clause
broadinstitute/cms
cms/power/power_func.py
1
8625
## functions for analyzing empirical/simulated CMS output ## last updated 09.14.2017 vitti@broadinstitute.org import matplotlib as mp mp.use('agg') import matplotlib.pyplot as plt import numpy as np import math from scipy.stats import percentileofscore ################### ## DEFINE SCORES ## ################### def write_master_likesfile(writefilename, model, selpop, freq,basedir, miss = "neut",): '''adapted from run_likes_func.py''' writefile = open(writefilename, 'w') for score in ['ihs', 'nsl', 'delihh']: hitlikesfilename = basedir + model + "/" + score + "/likes_sel" + str(selpop) + "_" + str(freq) + "_causal.txt"#_smoothed.txt" misslikesfilename = basedir + model + "/" + score + "/likes_sel" + str(selpop) + "_" + str(freq) + "_" + miss + ".txt"#"_smoothed.txt" #assert(os.path.isfile(hitlikesfilename) and os.path.isfile(misslikesfilename)) writefile.write(hitlikesfilename + "\n" + misslikesfilename + "\n") for score in ['xpehh', 'fst', 'deldaf']: hitlikesfilename = basedir + model + "/" + score + "/likes_sel" + str(selpop) + "_choose_" + str(freq) + "_causal.txt"#_smoothed.txt" misslikesfilename = basedir + model + "/" + score + "/likes_sel" + str(selpop) + "_choose_" + str(freq) + "_" + miss + ".txt"#"_smoothed.txt" #assert(os.path.isfile(hitlikesfilename) and os.path.isfile(misslikesfilename)) writefile.write(hitlikesfilename + "\n" + misslikesfilename + "\n") writefile.close() print("wrote to: " + writefilename) return ############### ## REGION ID ## ############### def get_window(istart, physpos, scores, windowlen = 100000): window_scores = [scores[istart]] startpos = physpos[istart] pos = startpos iscore = istart while pos < (startpos + windowlen): iscore += 1 if iscore >= len(scores): break window_scores.append(scores[iscore]) pos = physpos[iscore] #print(str(pos) + " " + str(startpos)) return window_scores def check_outliers(scorelist, cutoff = 3): numscores = len(scorelist) outliers = [item for item in scorelist if item > cutoff] numoutliers = len(outliers) percentage = (float(numoutliers) / float(numscores)) * 100. return percentage def check_rep_windows(physpos, scores, windowlen = 100000, cutoff = 3, totalchrlen=1000000): ''' previous implementation: !!!! this is going to result in false positives whenever I have a small uptick right near the edge of the replicate ''' #check window defined by each snp as starting point rep_percentages = [] numSnps = len(physpos) numWindows = 0 #get exhaustive windows and stop at chrom edge for isnp in range(numSnps): if physpos[isnp] + windowlen < totalchrlen: numWindows +=1 else: #print(str(physpos[isnp]) + "\t") break for iPos in range(numWindows): window_scores = get_window(iPos, physpos, scores, windowlen) percentage = check_outliers(window_scores, cutoff) rep_percentages.append(percentage) return rep_percentages def merge_windows(chrom_signif, windowlen, maxGap = 100000): print('should implement this using bedtools') starts, ends = [], [] contig = False this_windowlen = 0 starting_pos = 0 if len(chrom_signif) > 0: for i_start in range(len(chrom_signif) - 1): if not contig: starts.append(chrom_signif[i_start]) this_windowlen = windowlen #unmerged, default starting_pos = chrom_signif[i_start] if ((chrom_signif[i_start] + this_windowlen) > chrom_signif[i_start + 1]): #contiguous contig = True this_windowlen = chrom_signif[i_start +1] + windowlen - starting_pos #or, could also be contiguous in the situation where the next snp is not within this window because there doesn't exist such a snp elif chrom_signif[i_start +1] >=(chrom_signif[i_start] + this_windowlen) and chrom_signif[i_start +1] < (chrom_signif[i_start] + maxGap): contig = True this_windowlen = chrom_signif[i_start +1] + windowlen - starting_pos else: contig = False if not contig: windowend = chrom_signif[i_start] + windowlen ends.append(windowend) if contig: #last region is overlapped by its predecssor ends.append(chrom_signif[-1] + windowlen) else: starts.append(chrom_signif[-1]) ends.append(chrom_signif[-1] + windowlen) assert len(starts) == len(ends) return starts, ends ########################## ## POWER & SIGNIFICANCE ## ########################## def calc_pr(all_percentages, threshhold): numNeutReps_exceedThresh = 0 totalnumNeutReps = len(all_percentages) for irep in range(totalnumNeutReps): if len(all_percentages[irep]) != 0: if max(all_percentages[irep]) > threshhold: numNeutReps_exceedThresh +=1 numNeutReps_exceedThresh, totalnumNeutReps = float(numNeutReps_exceedThresh), float(totalnumNeutReps) if totalnumNeutReps != 0: pr = numNeutReps_exceedThresh / totalnumNeutReps else: pr = 0 print('ERROR; empty set') return pr def get_causal_rank(values, causal_val): if np.isnan(causal_val): return(float('nan')) assert(causal_val in values) cleanvals = [] for item in values: if not np.isnan(item) and not np.isinf(item): cleanvals.append(item) values = cleanvals values.sort() values.reverse() causal_rank = values.index(causal_val) return causal_rank def get_cdf_from_causal_ranks(causal_ranks): numbins = max(causal_ranks) #? heuristic counts, bins = np.histogram(causal_ranks, bins=numbins, normed = True) #doublecheck cdf = np.cumsum(counts) return bins, cdf def get_pval(all_simscores, thisScore): r = np.searchsorted(all_simscores,thisScore) n = len(all_simscores) pval = 1. - ((r + 1.) / (n + 1.)) if pval > 0: #pval *= nSnps #Bonferroni return pval else: #print("r: " +str(r) + " , n: " + str(n)) pval = 1. - (r/(n+1)) #pval *= nSnps #Bonferroni return pval ############### ## VISUALIZE ## ############### def quick_plot(ax, pos, val, ylabel,causal_index=-1): ax.scatter(pos, val, s=.8) if causal_index != -1: ax.scatter(pos[causal_index], val[causal_index], color='r', s=4) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize('6') ax.set_ylabel(ylabel, fontsize='6') #ax.set_xlim([0, 1500000]) #make flexible? ax.yaxis.set_label_position('right') #ax.set_ylim([min(val), max(val)]) return ax def plot_dist(allvals, savefilename= "/web/personal/vitti/test.png", numBins=1000): #print(allvals) #get rid of nans and infs #cleanvals = [item for item in allvals if not np.isnan(item)] #allvals = cleanvals allvals = np.array(allvals) allvals = allvals[~np.isnan(allvals)] allvals = allvals[~np.isinf(allvals)] #allvals = list(allvals) #print(allvals) print("percentile for score = 10: " + str(percentileofscore(allvals, 10))) print("percentile for score = 15: " + str(percentileofscore(allvals, 15))) if len(allvals) > 0: f, ax = plt.subplots(1) ax.hist(allvals, bins=numBins) plt.savefig(savefilename) print('plotted to ' + savefilename) return def plotManhattan(ax, neut_rep_scores, emp_scores, chrom_pos, nSnps, maxSkipVal = 0, zscores = True): #neut_rep_scores.sort() #print('sorted neutral scores...') lastpos = 0 for chrom in range(1,23): ichrom = chrom-1 if ichrom%2 == 0: plotcolor = "darkblue" else: plotcolor = "lightblue" if zscores == True: #http://stackoverflow.com/questions/3496656/convert-z-score-z-value-standard-score-to-p-value-for-normal-distribution-in?rq=1 #Z SCORE cf SG email 103116 #pvals = [get_pval(neut_rep_scores, item) for item in emp_scores[ichrom]] pvalues = [] for item in emp_scores[ichrom]: if item < maxSkipVal: #speed up this process by ignoring anything obviously insignificant pval = 1 else: #print('scipy') #sys.exit() pval = scipy.stats.norm.sf(abs(item)) pvalues.append(pval) #else: # pval = get_pval(neut_rep_scores, item) #pvalues.append(pval) print("calculated pvalues for chrom " + str(chrom)) chrom_pos = range(lastpos, lastpos + len(pvalues)) logtenpvals = [(-1. * math.log10(pval)) for pval in pvalues] ax.scatter(chrom_pos, logtenpvals, color =plotcolor, s=.5) lastpos = chrom_pos[-1] else: chrom_pos = range(lastpos, lastpos + len(emp_scores[ichrom])) ax.scatter(chrom_pos, emp_scores[ichrom], color=plotcolor, s=.5) lastpos = chrom_pos[-1] return ax def plotManhattan_extended(ax, emp_scores, chrom_pos, chrom): ''' makes a figure more like in Karlsson 2013 instead of Grossman 2013''' ax.plot(chrom_pos, emp_scores, linestyle='None', marker=".", markersize=.3, color="black") ax.set_ylabel('chr' + str(chrom), fontsize=6, rotation='horizontal') labels = ax.get_yticklabels() ax.set_yticklabels(labels, fontsize=6) ax.set_axis_bgcolor('LightGray') return ax
bsd-2-clause
jstoxrocky/statsmodels
statsmodels/sandbox/tsa/fftarma.py
30
16438
# -*- coding: utf-8 -*- """ Created on Mon Dec 14 19:53:25 2009 Author: josef-pktd generate arma sample using fft with all the lfilter it looks slow to get the ma representation first apply arma filter (in ar representation) to time series to get white noise but seems slow to be useful for fast estimation for nobs=10000 change/check: instead of using marep, use fft-transform of ar and ma separately, use ratio check theory is correct and example works DONE : feels much faster than lfilter -> use for estimation of ARMA -> use pade (scipy.misc) approximation to get starting polynomial from autocorrelation (is autocorrelation of AR(p) related to marep?) check if pade is fast, not for larger arrays ? maybe pade doesn't do the right thing for this, not tried yet scipy.pade([ 1. , 0.6, 0.25, 0.125, 0.0625, 0.1],2) raises LinAlgError: singular matrix also doesn't have roots inside unit circle ?? -> even without initialization, it might be fast for estimation -> how do I enforce stationarity and invertibility, need helper function get function drop imag if close to zero from numpy/scipy source, where? """ from __future__ import print_function import numpy as np import numpy.fft as fft #import scipy.fftpack as fft from scipy import signal #from try_var_convolve import maxabs from statsmodels.sandbox.archive.linalg_decomp_1 import OneTimeProperty from statsmodels.tsa.arima_process import ArmaProcess #trying to convert old experiments to a class class ArmaFft(ArmaProcess): '''fft tools for arma processes This class contains several methods that are providing the same or similar returns to try out and test different implementations. Notes ----- TODO: check whether we don't want to fix maxlags, and create new instance if maxlag changes. usage for different lengths of timeseries ? or fix frequency and length for fft check default frequencies w, terminology norw n_or_w some ffts are currently done without padding with zeros returns for spectral density methods needs checking, is it always the power spectrum hw*hw.conj() normalization of the power spectrum, spectral density: not checked yet, for example no variance of underlying process is used ''' def __init__(self, ar, ma, n): #duplicates now that are subclassing ArmaProcess super(ArmaFft, self).__init__(ar, ma) self.ar = np.asarray(ar) self.ma = np.asarray(ma) self.nobs = n #could make the polynomials into cached attributes self.arpoly = np.polynomial.Polynomial(ar) self.mapoly = np.polynomial.Polynomial(ma) self.nar = len(ar) #1d only currently self.nma = len(ma) def padarr(self, arr, maxlag, atend=True): '''pad 1d array with zeros at end to have length maxlag function that is a method, no self used Parameters ---------- arr : array_like, 1d array that will be padded with zeros maxlag : int length of array after padding atend : boolean If True (default), then the zeros are added to the end, otherwise to the front of the array Returns ------- arrp : ndarray zero-padded array Notes ----- This is mainly written to extend coefficient arrays for the lag-polynomials. It returns a copy. ''' if atend: return np.r_[arr, np.zeros(maxlag-len(arr))] else: return np.r_[np.zeros(maxlag-len(arr)), arr] def pad(self, maxlag): '''construct AR and MA polynomials that are zero-padded to a common length Parameters ---------- maxlag : int new length of lag-polynomials Returns ------- ar : ndarray extended AR polynomial coefficients ma : ndarray extended AR polynomial coefficients ''' arpad = np.r_[self.ar, np.zeros(maxlag-self.nar)] mapad = np.r_[self.ma, np.zeros(maxlag-self.nma)] return arpad, mapad def fftar(self, n=None): '''Fourier transform of AR polynomial, zero-padded at end to n Parameters ---------- n : int length of array after zero-padding Returns ------- fftar : ndarray fft of zero-padded ar polynomial ''' if n is None: n = len(self.ar) return fft.fft(self.padarr(self.ar, n)) def fftma(self, n): '''Fourier transform of MA polynomial, zero-padded at end to n Parameters ---------- n : int length of array after zero-padding Returns ------- fftar : ndarray fft of zero-padded ar polynomial ''' if n is None: n = len(self.ar) return fft.fft(self.padarr(self.ma, n)) #@OneTimeProperty # not while still debugging things def fftarma(self, n=None): '''Fourier transform of ARMA polynomial, zero-padded at end to n The Fourier transform of the ARMA process is calculated as the ratio of the fft of the MA polynomial divided by the fft of the AR polynomial. Parameters ---------- n : int length of array after zero-padding Returns ------- fftarma : ndarray fft of zero-padded arma polynomial ''' if n is None: n = self.nobs return (self.fftma(n) / self.fftar(n)) def spd(self, npos): '''raw spectral density, returns Fourier transform n is number of points in positive spectrum, the actual number of points is twice as large. different from other spd methods with fft ''' n = npos w = fft.fftfreq(2*n) * 2 * np.pi hw = self.fftarma(2*n) #not sure, need to check normalization #return (hw*hw.conj()).real[n//2-1:] * 0.5 / np.pi #doesn't show in plot return (hw*hw.conj()).real * 0.5 / np.pi, w def spdshift(self, n): '''power spectral density using fftshift currently returns two-sided according to fft frequencies, use first half ''' #size = s1+s2-1 mapadded = self.padarr(self.ma, n) arpadded = self.padarr(self.ar, n) hw = fft.fft(fft.fftshift(mapadded)) / fft.fft(fft.fftshift(arpadded)) #return np.abs(spd)[n//2-1:] w = fft.fftfreq(n) * 2 * np.pi wslice = slice(n//2-1, None, None) #return (hw*hw.conj()).real[wslice], w[wslice] return (hw*hw.conj()).real, w def spddirect(self, n): '''power spectral density using padding to length n done by fft currently returns two-sided according to fft frequencies, use first half ''' #size = s1+s2-1 #abs looks wrong hw = fft.fft(self.ma, n) / fft.fft(self.ar, n) w = fft.fftfreq(n) * 2 * np.pi wslice = slice(None, n//2, None) #return (np.abs(hw)**2)[wslice], w[wslice] return (np.abs(hw)**2) * 0.5/np.pi, w def _spddirect2(self, n): '''this looks bad, maybe with an fftshift ''' #size = s1+s2-1 hw = (fft.fft(np.r_[self.ma[::-1],self.ma], n) / fft.fft(np.r_[self.ar[::-1],self.ar], n)) return (hw*hw.conj()) #.real[n//2-1:] def spdroots(self, w): '''spectral density for frequency using polynomial roots builds two arrays (number of roots, number of frequencies) ''' return self.spdroots_(self.arroots, self.maroots, w) def spdroots_(self, arroots, maroots, w): '''spectral density for frequency using polynomial roots builds two arrays (number of roots, number of frequencies) Parameters ---------- arroots : ndarray roots of ar (denominator) lag-polynomial maroots : ndarray roots of ma (numerator) lag-polynomial w : array_like frequencies for which spd is calculated Notes ----- this should go into a function ''' w = np.atleast_2d(w).T cosw = np.cos(w) #Greene 5th edt. p626, section 20.2.7.a. maroots = 1./maroots arroots = 1./arroots num = 1 + maroots**2 - 2* maroots * cosw den = 1 + arroots**2 - 2* arroots * cosw #print 'num.shape, den.shape', num.shape, den.shape hw = 0.5 / np.pi * num.prod(-1) / den.prod(-1) #or use expsumlog return np.squeeze(hw), w.squeeze() def spdpoly(self, w, nma=50): '''spectral density from MA polynomial representation for ARMA process References ---------- Cochrane, section 8.3.3 ''' mpoly = np.polynomial.Polynomial(self.arma2ma(nma)) hw = mpoly(np.exp(1j * w)) spd = np.real_if_close(hw * hw.conj() * 0.5/np.pi) return spd, w def filter(self, x): ''' filter a timeseries with the ARMA filter padding with zero is missing, in example I needed the padding to get initial conditions identical to direct filter Initial filtered observations differ from filter2 and signal.lfilter, but at end they are the same. See Also -------- tsa.filters.fftconvolve ''' n = x.shape[0] if n == self.fftarma: fftarma = self.fftarma else: fftarma = self.fftma(n) / self.fftar(n) tmpfft = fftarma * fft.fft(x) return fft.ifft(tmpfft) def filter2(self, x, pad=0): '''filter a time series using fftconvolve3 with ARMA filter padding of x currently works only if x is 1d in example it produces same observations at beginning as lfilter even without padding. TODO: this returns 1 additional observation at the end ''' from statsmodels.tsa.filters import fftconvolve3 if not pad: pass elif pad == 'auto': #just guessing how much padding x = self.padarr(x, x.shape[0] + 2*(self.nma+self.nar), atend=False) else: x = self.padarr(x, x.shape[0] + int(pad), atend=False) return fftconvolve3(x, self.ma, self.ar) def acf2spdfreq(self, acovf, nfreq=100, w=None): ''' not really a method just for comparison, not efficient for large n or long acf this is also similarly use in tsa.stattools.periodogram with window ''' if w is None: w = np.linspace(0, np.pi, nfreq)[:, None] nac = len(acovf) hw = 0.5 / np.pi * (acovf[0] + 2 * (acovf[1:] * np.cos(w*np.arange(1,nac))).sum(1)) return hw def invpowerspd(self, n): '''autocovariance from spectral density scaling is correct, but n needs to be large for numerical accuracy maybe padding with zero in fft would be faster without slicing it returns 2-sided autocovariance with fftshift >>> ArmaFft([1, -0.5], [1., 0.4], 40).invpowerspd(2**8)[:10] array([ 2.08 , 1.44 , 0.72 , 0.36 , 0.18 , 0.09 , 0.045 , 0.0225 , 0.01125 , 0.005625]) >>> ArmaFft([1, -0.5], [1., 0.4], 40).acovf(10) array([ 2.08 , 1.44 , 0.72 , 0.36 , 0.18 , 0.09 , 0.045 , 0.0225 , 0.01125 , 0.005625]) ''' hw = self.fftarma(n) return np.real_if_close(fft.ifft(hw*hw.conj()), tol=200)[:n] def spdmapoly(self, w, twosided=False): '''ma only, need division for ar, use LagPolynomial ''' if w is None: w = np.linspace(0, np.pi, nfreq) return 0.5 / np.pi * self.mapoly(np.exp(w*1j)) def plot4(self, fig=None, nobs=100, nacf=20, nfreq=100): rvs = self.generate_sample(nsample=100, burnin=500) acf = self.acf(nacf)[:nacf] #TODO: check return length pacf = self.pacf(nacf) w = np.linspace(0, np.pi, nfreq) spdr, wr = self.spdroots(w) if fig is None: import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(2,2,1) ax.plot(rvs) ax.set_title('Random Sample \nar=%s, ma=%s' % (self.ar, self.ma)) ax = fig.add_subplot(2,2,2) ax.plot(acf) ax.set_title('Autocorrelation \nar=%s, ma=%rs' % (self.ar, self.ma)) ax = fig.add_subplot(2,2,3) ax.plot(wr, spdr) ax.set_title('Power Spectrum \nar=%s, ma=%s' % (self.ar, self.ma)) ax = fig.add_subplot(2,2,4) ax.plot(pacf) ax.set_title('Partial Autocorrelation \nar=%s, ma=%s' % (self.ar, self.ma)) return fig def spdar1(ar, w): if np.ndim(ar) == 0: rho = ar else: rho = -ar[1] return 0.5 / np.pi /(1 + rho*rho - 2 * rho * np.cos(w)) if __name__ == '__main__': def maxabs(x,y): return np.max(np.abs(x-y)) nobs = 200 #10000 ar = [1, 0.0] ma = [1, 0.0] ar2 = np.zeros(nobs) ar2[:2] = [1, -0.9] uni = np.zeros(nobs) uni[0]=1. #arrep = signal.lfilter(ma, ar, ar2) #marep = signal.lfilter([1],arrep, uni) # same faster: arcomb = np.convolve(ar, ar2, mode='same') marep = signal.lfilter(ma,arcomb, uni) #[len(ma):] print(marep[:10]) mafr = fft.fft(marep) rvs = np.random.normal(size=nobs) datafr = fft.fft(rvs) y = fft.ifft(mafr*datafr) print(np.corrcoef(np.c_[y[2:], y[1:-1], y[:-2]],rowvar=0)) arrep = signal.lfilter([1],marep, uni) print(arrep[:20]) # roundtrip to ar arfr = fft.fft(arrep) yfr = fft.fft(y) x = fft.ifft(arfr*yfr).real #imag part is e-15 # the next two are equal, roundtrip works print(x[:5]) print(rvs[:5]) print(np.corrcoef(np.c_[x[2:], x[1:-1], x[:-2]],rowvar=0)) # ARMA filter using fft with ratio of fft of ma/ar lag polynomial # seems much faster than using lfilter #padding, note arcomb is already full length arcombp = np.zeros(nobs) arcombp[:len(arcomb)] = arcomb map_ = np.zeros(nobs) #rename: map was shadowing builtin map_[:len(ma)] = ma ar0fr = fft.fft(arcombp) ma0fr = fft.fft(map_) y2 = fft.ifft(ma0fr/ar0fr*datafr) #the next two are (almost) equal in real part, almost zero but different in imag print(y2[:10]) print(y[:10]) print(maxabs(y, y2)) # from chfdiscrete #1.1282071239631782e-014 ar = [1, -0.4] ma = [1, 0.2] arma1 = ArmaFft([1, -0.5,0,0,0,00, -0.7, 0.3], [1, 0.8], nobs) nfreq = nobs w = np.linspace(0, np.pi, nfreq) w2 = np.linspace(0, 2*np.pi, nfreq) import matplotlib.pyplot as plt plt.close('all') plt.figure() spd1, w1 = arma1.spd(2**10) print(spd1.shape) _ = plt.plot(spd1) plt.title('spd fft complex') plt.figure() spd2, w2 = arma1.spdshift(2**10) print(spd2.shape) _ = plt.plot(w2, spd2) plt.title('spd fft shift') plt.figure() spd3, w3 = arma1.spddirect(2**10) print(spd3.shape) _ = plt.plot(w3, spd3) plt.title('spd fft direct') plt.figure() spd3b = arma1._spddirect2(2**10) print(spd3b.shape) _ = plt.plot(spd3b) plt.title('spd fft direct mirrored') plt.figure() spdr, wr = arma1.spdroots(w) print(spdr.shape) plt.plot(w, spdr) plt.title('spd from roots') plt.figure() spdar1_ = spdar1(arma1.ar, w) print(spdar1_.shape) _ = plt.plot(w, spdar1_) plt.title('spd ar1') plt.figure() wper, spdper = arma1.periodogram(nfreq) print(spdper.shape) _ = plt.plot(w, spdper) plt.title('periodogram') startup = 1000 rvs = arma1.generate_sample(startup+10000)[startup:] import matplotlib.mlab as mlb plt.figure() sdm, wm = mlb.psd(x) print('sdm.shape', sdm.shape) sdm = sdm.ravel() plt.plot(wm, sdm) plt.title('matplotlib') from nitime.algorithms import LD_AR_est #yule_AR_est(s, order, Nfreqs) wnt, spdnt = LD_AR_est(rvs, 10, 512) plt.figure() print('spdnt.shape', spdnt.shape) _ = plt.plot(spdnt.ravel()) print(spdnt[:10]) plt.title('nitime') fig = plt.figure() arma1.plot4(fig) #plt.show()
bsd-3-clause
joshzarrabi/e-mission-server
emission/analysis/classification/inference/mode.py
2
17308
# Standard imports from pymongo import MongoClient import logging from datetime import datetime import sys import os import numpy as np import scipy as sp import time from datetime import datetime # Our imports import emission.analysis.section_features as easf import emission.core.get_database as edb # We are not going to use the feature matrix for analysis unless we have at # least 50 points in the training set. 50 is arbitrary. We could also consider # combining the old and new training data, but this is really a bootstrapping # problem, so we don't need to solve it right now. minTrainingSetSize = 1000 class ModeInferencePipeline: def __init__(self): self.featureLabels = ["distance", "duration", "first filter mode", "sectionId", "avg speed", "speed EV", "speed variance", "max speed", "max accel", "isCommute", "heading change rate", "stop rate", "velocity change rate", "start lat", "start lng", "stop lat", "stop lng", "start hour", "end hour", "close to bus stop", "close to train stop", "close to airport"] self.Sections = edb.get_section_db() def runPipeline(self): allConfirmedTripsQuery = ModeInferencePipeline.getSectionQueryWithGroundTruth({'$ne': ''}) (self.modeList, self.confirmedSections) = self.loadTrainingDataStep(allConfirmedTripsQuery) logging.debug("confirmedSections.count() = %s" % (self.confirmedSections.count())) if (self.confirmedSections.count() < minTrainingSetSize): logging.info("initial loadTrainingDataStep DONE") logging.debug("current training set too small, reloading from backup!") backupSections = MongoClient('localhost').Backup_database.Stage_Sections (self.modeList, self.confirmedSections) = self.loadTrainingDataStep(allConfirmedTripsQuery, backupSections) logging.info("loadTrainingDataStep DONE") (self.bus_cluster, self.train_cluster) = self.generateBusAndTrainStopStep() logging.info("generateBusAndTrainStopStep DONE") (self.featureMatrix, self.resultVector) = self.generateFeatureMatrixAndResultVectorStep() logging.info("generateFeatureMatrixAndResultVectorStep DONE") (self.cleanedFeatureMatrix, self.cleanedResultVector) = self.cleanDataStep() logging.info("cleanDataStep DONE") self.selFeatureIndices = self.selectFeatureIndicesStep() logging.info("selectFeatureIndicesStep DONE") self.selFeatureMatrix = self.cleanedFeatureMatrix[:,self.selFeatureIndices] self.model = self.buildModelStep() logging.info("buildModelStep DONE") toPredictTripsQuery = {"$and": [{'type': 'move'}, ModeInferencePipeline.getModeQuery(''), {'predicted_mode': None}]} (self.toPredictFeatureMatrix, self.sectionIds, self.sectionUserIds) = self.generateFeatureMatrixAndIDsStep(toPredictTripsQuery) logging.info("generateFeatureMatrixAndIDsStep DONE") self.predictedProb = self.predictModesStep() logging.info("predictModesStep DONE") self.savePredictionsStep() logging.info("savePredictionsStep DONE") # Most of the time, this will be an int, but it can also be a subquery, like # {'$ne': ''}. This will be used to find the set of entries for the training # set, for example @staticmethod def getModeQuery(groundTruthMode): # We need the existence check because the corrected mode is not guaranteed to exist, # and if it doesn't exist, it will end up match the != '' query (since it # is not '', it is non existent) correctedModeQuery = lambda mode: {'$and': [{'corrected_mode': {'$exists': True}}, {'corrected_mode': groundTruthMode}]} return {'$or': [correctedModeQuery(groundTruthMode), {'confirmed_mode': groundTruthMode}]} @staticmethod def getSectionQueryWithGroundTruth(groundTruthMode): return {"$and": [{'type': 'move'}, ModeInferencePipeline.getModeQuery(groundTruthMode)]} # TODO: Refactor into generic steps and results def loadTrainingDataStep(self, sectionQuery, sectionDb = None): logging.debug("START TRAINING DATA STEP") if (sectionDb == None): sectionDb = self.Sections begin = time.time() logging.debug("Section data set size = %s" % sectionDb.find({'type': 'move'}).count()) duration = time.time() - begin logging.debug("Getting dataset size took %s" % (duration)) logging.debug("Querying confirmedSections %s" % (datetime.now())) begin = time.time() confirmedSections = sectionDb.find(sectionQuery) duration = time.time() - begin logging.debug("Querying confirmedSection took %s" % (duration)) logging.debug("Querying stage modes %s" % (datetime.now())) begin = time.time() modeList = [] for mode in edb.get_mode_db().find(): modeList.append(mode) logging.debug(mode) duration = time.time() - begin logging.debug("Querying stage modes took %s" % (duration)) logging.debug("Section query with ground truth %s" % (datetime.now())) begin = time.time() logging.debug("Training set total size = %s" % sectionDb.find(ModeInferencePipeline.getSectionQueryWithGroundTruth({'$ne': ''})).count()) for mode in modeList: logging.debug("%s: %s" % (mode['mode_name'], sectionDb.find(ModeInferencePipeline.getSectionQueryWithGroundTruth(mode['mode_id'])))) duration = time.time() - begin logging.debug("Getting section query with ground truth took %s" % (duration)) duration = time.time() - begin return (modeList, confirmedSections) # TODO: Should mode_cluster be in featurecalc or here? def generateBusAndTrainStopStep(self): bus_cluster=easf.mode_cluster(5,105,1) train_cluster=easf.mode_cluster(6,600,1) air_cluster=easf.mode_cluster(9,600,1) return (bus_cluster, train_cluster) # Feature matrix construction def generateFeatureMatrixAndResultVectorStep(self): featureMatrix = np.zeros([self.confirmedSections.count(), len(self.featureLabels)]) resultVector = np.zeros(self.confirmedSections.count()) logging.debug("created data structures of size %s" % self.confirmedSections.count()) # There are a couple of additions to the standard confirmedSections cursor here. # First, we read it in batches of 300 in order to avoid the 10 minute timeout # Our logging shows that we can process roughly 500 entries in 10 minutes # Second, it looks like the cursor requeries while iterating. So when we # first check, we get count of x, but if new entries were read (or in # this case, classified) while we are iterating over the cursor, we may # end up processing > x entries. # This will crash the script because we will try to access a record that # doesn't exist. # So we limit the records to the size of the matrix that we have created for (i, section) in enumerate(self.confirmedSections.limit(featureMatrix.shape[0]).batch_size(300)): try: self.updateFeatureMatrixRowWithSection(featureMatrix, i, section) resultVector[i] = self.getGroundTruthMode(section) if i % 100 == 0: logging.debug("Processing record %s " % i) except Exception, e: logging.debug("skipping section %s due to error %s " % (section, e)) return (featureMatrix, resultVector) def getGroundTruthMode(self, section): # logging.debug("getting ground truth for section %s" % section) if 'corrected_mode' in section: # logging.debug("Returning corrected mode %s" % section['corrected_mode']) return section['corrected_mode'] else: # logging.debug("Returning confirmed mode %s" % section['confirmed_mode']) return section['confirmed_mode'] # Features are: # 0. distance # 1. duration # 2. first filter mode # 3. sectionId # 4. avg speed # 5. speed EV # 6. speed variance # 7. max speed # 8. max accel # 9. isCommute # 10. heading change rate (currently unfilled) # 11. stop rate (currently unfilled) # 12. velocity change rate (currently unfilled) # 13. start lat # 14. start lng # 15. stop lat # 16. stop lng # 17. start hour # 18. end hour # 19. both start and end close to bus stop # 20. both start and end close to train station # 21. both start and end close to airport def updateFeatureMatrixRowWithSection(self, featureMatrix, i, section): featureMatrix[i, 0] = section['distance'] featureMatrix[i, 1] = (section['section_end_datetime'] - section['section_start_datetime']).total_seconds() # Deal with unknown modes like "airplane" try: featureMatrix[i, 2] = section['mode'] except ValueError: featureMatrix[i, 2] = 0 featureMatrix[i, 3] = section['section_id'] featureMatrix[i, 4] = easf.calAvgSpeed(section) speeds = easf.calSpeeds(section) if speeds != None and len(speeds) > 0: featureMatrix[i, 5] = np.mean(speeds) featureMatrix[i, 6] = np.std(speeds) featureMatrix[i, 7] = np.max(speeds) else: # They will remain zero pass accels = easf.calAccels(section) if accels != None and len(accels) > 0: featureMatrix[i, 8] = np.max(accels) else: # They will remain zero pass featureMatrix[i, 9] = ('commute' in section) and (section['commute'] == 'to' or section['commute'] == 'from') featureMatrix[i, 10] = easf.calHCR(section) featureMatrix[i, 11] = easf.calSR(section) featureMatrix[i, 12] = easf.calVCR(section) if 'section_start_point' in section and section['section_start_point'] != None: startCoords = section['section_start_point']['coordinates'] featureMatrix[i, 13] = startCoords[0] featureMatrix[i, 14] = startCoords[1] if 'section_end_point' in section and section['section_end_point'] != None: endCoords = section['section_end_point']['coordinates'] featureMatrix[i, 15] = endCoords[0] featureMatrix[i, 16] = endCoords[1] featureMatrix[i, 17] = section['section_start_datetime'].time().hour featureMatrix[i, 18] = section['section_end_datetime'].time().hour if (hasattr(self, "bus_cluster")): featureMatrix[i, 19] = easf.mode_start_end_coverage(section, self.bus_cluster,105) if (hasattr(self, "train_cluster")): featureMatrix[i, 20] = easf.mode_start_end_coverage(section, self.train_cluster,600) if (hasattr(self, "air_cluster")): featureMatrix[i, 21] = easf.mode_start_end_coverage(section, self.air_cluster,600) # Replace NaN and inf by zeros so that it doesn't crash later featureMatrix[i] = np.nan_to_num(featureMatrix[i]) def cleanDataStep(self): runIndices = self.resultVector == 2 transportIndices = self.resultVector == 4 mixedIndices = self.resultVector == 8 airIndices = self.resultVector == 9 unknownIndices = self.resultVector == 0 strippedIndices = np.logical_not(runIndices | transportIndices | mixedIndices | unknownIndices) logging.debug("Stripped trips with mode: run %s, transport %s, mixed %s, unknown %s unstripped %s" % (np.count_nonzero(runIndices), np.count_nonzero(transportIndices), np.count_nonzero(mixedIndices), np.count_nonzero(unknownIndices), np.count_nonzero(strippedIndices))) strippedFeatureMatrix = self.featureMatrix[strippedIndices] strippedResultVector = self.resultVector[strippedIndices] # In spite of stripping out the values, we see that there are clear # outliers. This is almost certainly a mis-classified trip, because the # distance and speed are both really large, but the mode is walking. Let's # manually filter out this outlier. distanceOutliers = strippedFeatureMatrix[:,0] > 500000 speedOutliers = strippedFeatureMatrix[:,4] > 100 speedMeanOutliers = strippedFeatureMatrix[:,5] > 80 speedVarianceOutliers = strippedFeatureMatrix[:,6] > 70 maxSpeedOutliers = strippedFeatureMatrix[:,7] > 160 logging.debug("Stripping out distanceOutliers %s, speedOutliers %s, speedMeanOutliers %s, speedVarianceOutliers %s, maxSpeedOutliers %s" % (np.nonzero(distanceOutliers), np.nonzero(speedOutliers), np.nonzero(speedMeanOutliers), np.nonzero(speedVarianceOutliers), np.nonzero(maxSpeedOutliers))) nonOutlierIndices = np.logical_not(distanceOutliers | speedOutliers | speedMeanOutliers | speedVarianceOutliers | maxSpeedOutliers) logging.debug("nonOutlierIndices.shape = %s" % nonOutlierIndices.shape) return (strippedFeatureMatrix[nonOutlierIndices], strippedResultVector[nonOutlierIndices]) # Feature Indices def selectFeatureIndicesStep(self): genericFeatureIndices = list(xrange(0,10)) AdvancedFeatureIndices = list(xrange(10,13)) LocationFeatureIndices = list(xrange(13,17)) TimeFeatureIndices = list(xrange(17,19)) BusTrainFeatureIndices = list(xrange(19,22)) logging.debug("generic features = %s" % genericFeatureIndices) logging.debug("advanced features = %s" % AdvancedFeatureIndices) logging.debug("location features = %s" % LocationFeatureIndices) logging.debug("time features = %s" % TimeFeatureIndices) logging.debug("bus train features = %s" % BusTrainFeatureIndices) return genericFeatureIndices + BusTrainFeatureIndices def buildModelStep(self): from sklearn import ensemble forestClf = ensemble.RandomForestClassifier() model = forestClf.fit(self.selFeatureMatrix, self.cleanedResultVector) return model def generateFeatureMatrixAndIDsStep(self, sectionQuery): toPredictSections = self.Sections.find(sectionQuery) logging.debug("Predicting values for %d sections" % toPredictSections.count()) featureMatrix = np.zeros([toPredictSections.count(), len(self.featureLabels)]) sectionIds = [] sectionUserIds = [] for (i, section) in enumerate(toPredictSections.limit(featureMatrix.shape[0]).batch_size(300)): if i % 50 == 0: logging.debug("Processing test record %s " % i) self.updateFeatureMatrixRowWithSection(featureMatrix, i, section) sectionIds.append(section['_id']) sectionUserIds.append(section['user_id']) return (featureMatrix[:,self.selFeatureIndices], sectionIds, sectionUserIds) def predictModesStep(self): return self.model.predict_proba(self.toPredictFeatureMatrix) # The current probability will only have results for values from the set of # unique values in the resultVector. This means that the location of the # highest probability is not a 1:1 mapping to the mode, which will probably # have issues down the road. We are going to fix this here by storing the # non-zero probabilities in a map instead of in a list. We used to have an # list here, but we move to a map instead because we plan to support lots of # different modes, and having an giant array consisting primarily of zeros # doesn't sound like a great option. # In other words, uniqueModes = [1, 5] # predictedProb = [[1,0], [0,1]] # allModes has length 8 # returns [{'walking': 1}, {'bus': 1}] def convertPredictedProbToMap(self, allModeList, uniqueModes, predictedProbArr): currProbMap = {} uniqueModesInt = [int(um) for um in uniqueModes] logging.debug("predictedProbArr has %s non-zero elements" % np.count_nonzero(predictedProbArr)) logging.debug("uniqueModes are %s " % uniqueModesInt) for (j, uniqueMode) in enumerate(uniqueModesInt): if predictedProbArr[j] != 0: # Modes start from 1, but allModeList indices start from 0 # so walking (mode id 1) -> modeList[0] modeName = allModeList[uniqueMode-1]['mode_name'] logging.debug("Setting probability of mode %s (%s) to %s" % (uniqueMode, modeName, predictedProbArr[j])) currProbMap[modeName] = predictedProbArr[j] return currProbMap def savePredictionsStep(self): from emission.core.wrapper.user import User from emission.core.wrapper.client import Client uniqueModes = sorted(set(self.cleanedResultVector)) for i in range(self.predictedProb.shape[0]): currSectionId = self.sectionIds[i] currProb = self.convertPredictedProbToMap(self.modeList, uniqueModes, self.predictedProb[i]) logging.debug("Updating probability for section with id = %s" % currSectionId) self.Sections.update({'_id': currSectionId}, {"$set": {"predicted_mode": currProb}}) currUser = User.fromUUID(self.sectionUserIds[i]) clientSpecificUpdate = Client(currUser.getFirstStudy()).clientSpecificSetters(currUser.uuid, currSectionId, currProb) if clientSpecificUpdate != None: self.Sections.update({'_id': currSectionId}, clientSpecificUpdate) if __name__ == "__main__": import json config_data = json.load(open('config.json')) log_base_dir = config_data['paths']['log_base_dir'] logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', filename="%s/pipeline.log" % log_base_dir, level=logging.DEBUG) modeInferPipeline = ModeInferencePipeline() modeInferPipeline.runPipeline()
bsd-3-clause
JosmanPS/scikit-learn
examples/cluster/plot_dict_face_patches.py
337
2747
""" Online learning of a dictionary of parts of faces ================================================== This example uses a large dataset of faces to learn a set of 20 x 20 images patches that constitute faces. From the programming standpoint, it is interesting because it shows how to use the online API of the scikit-learn to process a very large dataset by chunks. The way we proceed is that we load an image at a time and extract randomly 50 patches from this image. Once we have accumulated 500 of these patches (using 10 images), we run the `partial_fit` method of the online KMeans object, MiniBatchKMeans. The verbose setting on the MiniBatchKMeans enables us to see that some clusters are reassigned during the successive calls to partial-fit. This is because the number of patches that they represent has become too low, and it is better to choose a random new cluster. """ print(__doc__) import time import matplotlib.pyplot as plt import numpy as np from sklearn import datasets from sklearn.cluster import MiniBatchKMeans from sklearn.feature_extraction.image import extract_patches_2d faces = datasets.fetch_olivetti_faces() ############################################################################### # Learn the dictionary of images print('Learning the dictionary... ') rng = np.random.RandomState(0) kmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True) patch_size = (20, 20) buffer = [] index = 1 t0 = time.time() # The online learning part: cycle over the whole dataset 6 times index = 0 for _ in range(6): for img in faces.images: data = extract_patches_2d(img, patch_size, max_patches=50, random_state=rng) data = np.reshape(data, (len(data), -1)) buffer.append(data) index += 1 if index % 10 == 0: data = np.concatenate(buffer, axis=0) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) kmeans.partial_fit(data) buffer = [] if index % 100 == 0: print('Partial fit of %4i out of %i' % (index, 6 * len(faces.images))) dt = time.time() - t0 print('done in %.2fs.' % dt) ############################################################################### # Plot the results plt.figure(figsize=(4.2, 4)) for i, patch in enumerate(kmeans.cluster_centers_): plt.subplot(9, 9, i + 1) plt.imshow(patch.reshape(patch_size), cmap=plt.cm.gray, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.suptitle('Patches of faces\nTrain time %.1fs on %d patches' % (dt, 8 * len(faces.images)), fontsize=16) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) plt.show()
bsd-3-clause
micahcochran/geopandas
geopandas/_version.py
3
16750
# This file helps to compute a version number in source trees obtained from # git-archive tarball (such as those provided by githubs download-from-tag # feature). Distribution tarballs (built by setup.py sdist) and build # directories (produced by setup.py build) will contain a much shorter file # that just contains the computed version number. # This file is released into the public domain. Generated by # versioneer-0.16 (https://github.com/warner/python-versioneer) """Git implementation of _version.py.""" import errno import os import re import subprocess import sys def get_keywords(): """Get the keywords needed to look up the version information.""" # these strings will be replaced by git during git-archive. # setup.py/versioneer.py will grep for the variable names, so they must # each be defined on a line of their own. _version.py will just call # get_keywords(). git_refnames = "$Format:%d$" git_full = "$Format:%H$" keywords = {"refnames": git_refnames, "full": git_full} return keywords class VersioneerConfig: """Container for Versioneer configuration parameters.""" def get_config(): """Create, populate and return the VersioneerConfig() object.""" # these strings are filled in when 'setup.py versioneer' creates # _version.py cfg = VersioneerConfig() cfg.VCS = "git" cfg.style = "pep440" cfg.tag_prefix = "v" cfg.parentdir_prefix = "geopandas-" cfg.versionfile_source = "geopandas/_version.py" cfg.verbose = False return cfg class NotThisMethod(Exception): """Exception raised if a method is not valid for the current scenario.""" LONG_VERSION_PY = {} HANDLERS = {} def register_vcs_handler(vcs, method): # decorator """Decorator to mark a method as the handler for a particular VCS.""" def decorate(f): """Store f in HANDLERS[vcs][method].""" if vcs not in HANDLERS: HANDLERS[vcs] = {} HANDLERS[vcs][method] = f return f return decorate def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False): """Call the given command(s).""" assert isinstance(commands, list) p = None for c in commands: try: dispcmd = str([c] + args) # remember shell=False, so use git.cmd on windows, not just git p = subprocess.Popen([c] + args, cwd=cwd, stdout=subprocess.PIPE, stderr=(subprocess.PIPE if hide_stderr else None)) break except EnvironmentError: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue if verbose: print("unable to run %s" % dispcmd) print(e) return None else: if verbose: print("unable to find command, tried %s" % (commands,)) return None stdout = p.communicate()[0].strip() if sys.version_info[0] >= 3: stdout = stdout.decode() if p.returncode != 0: if verbose: print("unable to run %s (error)" % dispcmd) return None return stdout def versions_from_parentdir(parentdir_prefix, root, verbose): """Try to determine the version from the parent directory name. Source tarballs conventionally unpack into a directory that includes both the project name and a version string. """ dirname = os.path.basename(root) if not dirname.startswith(parentdir_prefix): if verbose: print("guessing rootdir is '%s', but '%s' doesn't start with " "prefix '%s'" % (root, dirname, parentdir_prefix)) raise NotThisMethod("rootdir doesn't start with parentdir_prefix") return {"version": dirname[len(parentdir_prefix):], "full-revisionid": None, "dirty": False, "error": None} @register_vcs_handler("git", "get_keywords") def git_get_keywords(versionfile_abs): """Extract version information from the given file.""" # the code embedded in _version.py can just fetch the value of these # keywords. When used from setup.py, we don't want to import _version.py, # so we do it with a regexp instead. This function is not used from # _version.py. keywords = {} try: f = open(versionfile_abs, "r") for line in f.readlines(): if line.strip().startswith("git_refnames ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["refnames"] = mo.group(1) if line.strip().startswith("git_full ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["full"] = mo.group(1) f.close() except EnvironmentError: pass return keywords @register_vcs_handler("git", "keywords") def git_versions_from_keywords(keywords, tag_prefix, verbose): """Get version information from git keywords.""" if not keywords: raise NotThisMethod("no keywords at all, weird") refnames = keywords["refnames"].strip() if refnames.startswith("$Format"): if verbose: print("keywords are unexpanded, not using") raise NotThisMethod("unexpanded keywords, not a git-archive tarball") refs = set([r.strip() for r in refnames.strip("()").split(",")]) # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %d # expansion behaves like git log --decorate=short and strips out the # refs/heads/ and refs/tags/ prefixes that would let us distinguish # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". tags = set([r for r in refs if re.search(r'\d', r)]) if verbose: print("discarding '%s', no digits" % ",".join(refs-tags)) if verbose: print("likely tags: %s" % ",".join(sorted(tags))) for ref in sorted(tags): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): r = ref[len(tag_prefix):] if verbose: print("picking %s" % r) return {"version": r, "full-revisionid": keywords["full"].strip(), "dirty": False, "error": None } # no suitable tags, so version is "0+unknown", but full hex is still there if verbose: print("no suitable tags, using unknown + full revision id") return {"version": "0+unknown", "full-revisionid": keywords["full"].strip(), "dirty": False, "error": "no suitable tags"} @register_vcs_handler("git", "pieces_from_vcs") def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): """Get version from 'git describe' in the root of the source tree. This only gets called if the git-archive 'subst' keywords were *not* expanded, and _version.py hasn't already been rewritten with a short version string, meaning we're inside a checked out source tree. """ if not os.path.exists(os.path.join(root, ".git")): if verbose: print("no .git in %s" % root) raise NotThisMethod("no .git directory") GITS = ["git"] if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) describe_out = run_command(GITS, ["describe", "--tags", "--dirty", "--always", "--long", "--match", "%s*" % tag_prefix], cwd=root) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") describe_out = describe_out.strip() full_out = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) if full_out is None: raise NotThisMethod("'git rev-parse' failed") full_out = full_out.strip() pieces = {} pieces["long"] = full_out pieces["short"] = full_out[:7] # maybe improved later pieces["error"] = None # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] # TAG might have hyphens. git_describe = describe_out # look for -dirty suffix dirty = git_describe.endswith("-dirty") pieces["dirty"] = dirty if dirty: git_describe = git_describe[:git_describe.rindex("-dirty")] # now we have TAG-NUM-gHEX or HEX if "-" in git_describe: # TAG-NUM-gHEX mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) if not mo: # unparseable. Maybe git-describe is misbehaving? pieces["error"] = ("unable to parse git-describe output: '%s'" % describe_out) return pieces # tag full_tag = mo.group(1) if not full_tag.startswith(tag_prefix): if verbose: fmt = "tag '%s' doesn't start with prefix '%s'" print(fmt % (full_tag, tag_prefix)) pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" % (full_tag, tag_prefix)) return pieces pieces["closest-tag"] = full_tag[len(tag_prefix):] # distance: number of commits since tag pieces["distance"] = int(mo.group(2)) # commit: short hex revision ID pieces["short"] = mo.group(3) else: # HEX: no tags pieces["closest-tag"] = None count_out = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root) pieces["distance"] = int(count_out) # total number of commits return pieces def plus_or_dot(pieces): """Return a + if we don't already have one, else return a .""" if "+" in pieces.get("closest-tag", ""): return "." return "+" def render_pep440(pieces): """Build up version string, with post-release "local version identifier". Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty Exceptions: 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += plus_or_dot(pieces) rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" else: # exception #1 rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" return rendered def render_pep440_pre(pieces): """TAG[.post.devDISTANCE] -- No -dirty. Exceptions: 1: no tags. 0.post.devDISTANCE """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"]: rendered += ".post.dev%d" % pieces["distance"] else: # exception #1 rendered = "0.post.dev%d" % pieces["distance"] return rendered def render_pep440_post(pieces): """TAG[.postDISTANCE[.dev0]+gHEX] . The ".dev0" means dirty. Note that .dev0 sorts backwards (a dirty tree will appear "older" than the corresponding clean one), but you shouldn't be releasing software with -dirty anyways. Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += ".post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" rendered += plus_or_dot(pieces) rendered += "g%s" % pieces["short"] else: # exception #1 rendered = "0.post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" rendered += "+g%s" % pieces["short"] return rendered def render_pep440_old(pieces): """TAG[.postDISTANCE[.dev0]] . The ".dev0" means dirty. Eexceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += ".post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" else: # exception #1 rendered = "0.post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" return rendered def render_git_describe(pieces): """TAG[-DISTANCE-gHEX][-dirty]. Like 'git describe --tags --dirty --always'. Exceptions: 1: no tags. HEX[-dirty] (note: no 'g' prefix) """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"]: rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) else: # exception #1 rendered = pieces["short"] if pieces["dirty"]: rendered += "-dirty" return rendered def render_git_describe_long(pieces): """TAG-DISTANCE-gHEX[-dirty]. Like 'git describe --tags --dirty --always -long'. The distance/hash is unconditional. Exceptions: 1: no tags. HEX[-dirty] (note: no 'g' prefix) """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) else: # exception #1 rendered = pieces["short"] if pieces["dirty"]: rendered += "-dirty" return rendered def render(pieces, style): """Render the given version pieces into the requested style.""" if pieces["error"]: return {"version": "unknown", "full-revisionid": pieces.get("long"), "dirty": None, "error": pieces["error"]} if not style or style == "default": style = "pep440" # the default if style == "pep440": rendered = render_pep440(pieces) elif style == "pep440-pre": rendered = render_pep440_pre(pieces) elif style == "pep440-post": rendered = render_pep440_post(pieces) elif style == "pep440-old": rendered = render_pep440_old(pieces) elif style == "git-describe": rendered = render_git_describe(pieces) elif style == "git-describe-long": rendered = render_git_describe_long(pieces) else: raise ValueError("unknown style '%s'" % style) return {"version": rendered, "full-revisionid": pieces["long"], "dirty": pieces["dirty"], "error": None} def get_versions(): """Get version information or return default if unable to do so.""" # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have # __file__, we can work backwards from there to the root. Some # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which # case we can only use expanded keywords. cfg = get_config() verbose = cfg.verbose try: return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose) except NotThisMethod: pass try: root = os.path.realpath(__file__) # versionfile_source is the relative path from the top of the source # tree (where the .git directory might live) to this file. Invert # this to find the root from __file__. for i in cfg.versionfile_source.split('/'): root = os.path.dirname(root) except NameError: return {"version": "0+unknown", "full-revisionid": None, "dirty": None, "error": "unable to find root of source tree"} try: pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) return render(pieces, cfg.style) except NotThisMethod: pass try: if cfg.parentdir_prefix: return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) except NotThisMethod: pass return {"version": "0+unknown", "full-revisionid": None, "dirty": None, "error": "unable to compute version"}
bsd-3-clause
trankmichael/scikit-learn
examples/cluster/plot_agglomerative_clustering_metrics.py
402
4492
""" Agglomerative clustering with different metrics =============================================== Demonstrates the effect of different metrics on the hierarchical clustering. The example is engineered to show the effect of the choice of different metrics. It is applied to waveforms, which can be seen as high-dimensional vector. Indeed, the difference between metrics is usually more pronounced in high dimension (in particular for euclidean and cityblock). We generate data from three groups of waveforms. Two of the waveforms (waveform 1 and waveform 2) are proportional one to the other. The cosine distance is invariant to a scaling of the data, as a result, it cannot distinguish these two waveforms. Thus even with no noise, clustering using this distance will not separate out waveform 1 and 2. We add observation noise to these waveforms. We generate very sparse noise: only 6% of the time points contain noise. As a result, the l1 norm of this noise (ie "cityblock" distance) is much smaller than it's l2 norm ("euclidean" distance). This can be seen on the inter-class distance matrices: the values on the diagonal, that characterize the spread of the class, are much bigger for the Euclidean distance than for the cityblock distance. When we apply clustering to the data, we find that the clustering reflects what was in the distance matrices. Indeed, for the Euclidean distance, the classes are ill-separated because of the noise, and thus the clustering does not separate the waveforms. For the cityblock distance, the separation is good and the waveform classes are recovered. Finally, the cosine distance does not separate at all waveform 1 and 2, thus the clustering puts them in the same cluster. """ # Author: Gael Varoquaux # License: BSD 3-Clause or CC-0 import matplotlib.pyplot as plt import numpy as np from sklearn.cluster import AgglomerativeClustering from sklearn.metrics import pairwise_distances np.random.seed(0) # Generate waveform data n_features = 2000 t = np.pi * np.linspace(0, 1, n_features) def sqr(x): return np.sign(np.cos(x)) X = list() y = list() for i, (phi, a) in enumerate([(.5, .15), (.5, .6), (.3, .2)]): for _ in range(30): phase_noise = .01 * np.random.normal() amplitude_noise = .04 * np.random.normal() additional_noise = 1 - 2 * np.random.rand(n_features) # Make the noise sparse additional_noise[np.abs(additional_noise) < .997] = 0 X.append(12 * ((a + amplitude_noise) * (sqr(6 * (t + phi + phase_noise))) + additional_noise)) y.append(i) X = np.array(X) y = np.array(y) n_clusters = 3 labels = ('Waveform 1', 'Waveform 2', 'Waveform 3') # Plot the ground-truth labelling plt.figure() plt.axes([0, 0, 1, 1]) for l, c, n in zip(range(n_clusters), 'rgb', labels): lines = plt.plot(X[y == l].T, c=c, alpha=.5) lines[0].set_label(n) plt.legend(loc='best') plt.axis('tight') plt.axis('off') plt.suptitle("Ground truth", size=20) # Plot the distances for index, metric in enumerate(["cosine", "euclidean", "cityblock"]): avg_dist = np.zeros((n_clusters, n_clusters)) plt.figure(figsize=(5, 4.5)) for i in range(n_clusters): for j in range(n_clusters): avg_dist[i, j] = pairwise_distances(X[y == i], X[y == j], metric=metric).mean() avg_dist /= avg_dist.max() for i in range(n_clusters): for j in range(n_clusters): plt.text(i, j, '%5.3f' % avg_dist[i, j], verticalalignment='center', horizontalalignment='center') plt.imshow(avg_dist, interpolation='nearest', cmap=plt.cm.gnuplot2, vmin=0) plt.xticks(range(n_clusters), labels, rotation=45) plt.yticks(range(n_clusters), labels) plt.colorbar() plt.suptitle("Interclass %s distances" % metric, size=18) plt.tight_layout() # Plot clustering results for index, metric in enumerate(["cosine", "euclidean", "cityblock"]): model = AgglomerativeClustering(n_clusters=n_clusters, linkage="average", affinity=metric) model.fit(X) plt.figure() plt.axes([0, 0, 1, 1]) for l, c in zip(np.arange(model.n_clusters), 'rgbk'): plt.plot(X[model.labels_ == l].T, c=c, alpha=.5) plt.axis('tight') plt.axis('off') plt.suptitle("AgglomerativeClustering(affinity=%s)" % metric, size=20) plt.show()
bsd-3-clause
reuk/wayverb
scripts/python/dispersion.py
2
6340
from math import e, pi import numpy as np import matplotlib.pyplot as plt from matplotlib import colors, ticker, cm from mpl_toolkits.mplot3d import Axes3D import numpy as np import operator def get_base_vectors(flip): ret = [ np.array([0.0, 2.0 * np.sqrt(2.0) / 3.0, 1.0 / 3.0]), np.array([ np.sqrt(2.0 / 3.0), -np.sqrt(2.0) / 3.0, 1.0 / 3.0]), np.array([0.0, 0.0, -1.0]), np.array([-np.sqrt(2.0 / 3.0), -np.sqrt(2.0) / 3.0, 1.0 / 3.0]), ] if flip: ret = [np.array([1, -1, -1]) * i for i in ret] return ret def get_vectors(): ret = [i + j for i in get_base_vectors(False) for j in get_base_vectors(True)] ret = filter(lambda x: np.any(x != np.array([0, 0, 0])), ret) return ret # DUYNE METHOD def get_speed(arr): """ The diagrams in the paper appear to be continuous outside of the range -1.5, 1.5. However, this function has a strange discontinuity at a radius of 1.4 """ def get_b(arr): summed = sum([pow(e, 1j * np.dot(arr, i)) for i in get_vectors()]) return 1.0 - 0.25 * summed.real def get_ang_g(arr): b = get_b(arr) return 0.5 * np.arctan(np.sqrt(4 - b * b) / abs(b)) c = np.sqrt(1.0 / 3.0) norm = np.linalg.norm(arr) # this analysis is only valid for frequencies below pi / 2 # (spectrum is mirrored above this limit) # simulated frequency is equal to magnitude of wave vector (arr) if norm < pi / 2: return get_ang_g(arr) / (norm * c) else: return None # CAMPOS METHOD def get_speed_campos(arr): def get_b(arr): x, y, z = arr a = np.cos(2.0 * x / np.sqrt(3.0)) * np.cos(2.0 * y / np.sqrt(3.0)) b = np.cos(2.0 * x / np.sqrt(3.0)) * np.cos(2.0 * z / np.sqrt(3.0)) c = np.cos(2.0 * y / np.sqrt(3.0)) * np.cos(2.0 * z / np.sqrt(3.0)) return a + b + c - 1 def get_kd(arr): return np.sqrt(3.0) * np.arccos(get_b(arr) / 2.0) / (2.0 * np.linalg.norm(arr)) return get_kd(arr) # direction error analysis from @hacihabiboglu # p(x) = pressure field in spatial(?) domain # P(w) = pressure field in frequency domain def get_U(): v = get_base_vectors(True) U = np.vstack(v) return U def eq_21(u, w): return pow(e, -1j * np.dot(u, w)) - 1 def eq_22(w): return np.array([eq_21(i, w) for i in get_base_vectors(True)]) def eq_23(w): return np.dot(np.linalg.pinv(get_U()), eq_22(w)) def hermitian_angle(a, b): prod = np.dot(a, np.conj(b)).real mag_a = np.sqrt(np.dot(a, np.conj(a))) mag_b = np.sqrt(np.dot(b, np.conj(b))) return (prod / (mag_a * mag_b)).real def direction_difference(arr): def get_term_1(): return eq_23(arr) def get_term_2(): return 1j * arr return hermitian_angle(get_term_1(), get_term_2()) # monte carlo bandwidth estimation def random_three_vector(): phi = np.random.uniform(0, pi * 2) costheta = np.random.uniform(-1, 1) theta = np.arccos(costheta) x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta) return np.array([x, y, z]) def get_max_valid_frequency(func, accuracy, starting_freq, increments, samples): last = starting_freq + increments ret = starting_freq while True: sample_points = [random_three_vector() * last for i in range(samples)] sampled = [func(i) for i in sample_points] if not all(map(lambda x: x > accuracy, sampled)): return ret else: ret = last last += increments def main(): """ This program duplicates the tetrahedral dispersion diagrams from the paper 'The Tetrahedral Digital Waveguide Mesh' buy Duyne and Smith. I wrote it to try to understand how to do dispersion analysis - the analysis here is of the difference of the actual wavefront speed to the ideal speed. """ w = np.array([0, 1, 0]) w /= np.linalg.norm(w) print "w", w for i in get_base_vectors(True): print "u", i print "21", eq_21(i, w) print "22", eq_22(w) print "23", eq_23(w) print print direction_difference(w) func = direction_difference vfunc = np.vectorize(lambda x, y, z: func(np.array([x, y, z]))) max_val = np.pi / 4 phi, theta = np.mgrid[0:pi:50j, 0:2*pi:50j] XX = max_val * np.sin(phi) * np.cos(theta) YY = max_val * np.sin(phi) * np.sin(theta) ZZ = max_val * np.cos(phi) zz = vfunc(XX, YY, ZZ) zzmin, zzmax = zz.min(), zz.max() print "dispersion error range:", zzmin, "to", zzmax zz = (zz - zzmin) / (zzmax - zzmin) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface( XX, YY, ZZ, rstride=1, cstride=1, facecolors=cm.jet(zz)) plt.show() # func = get_speed_campos # vfunc = np.vectorize(lambda x, y, z: func(np.array([x, y, z]))) # # min_accuracy = 0.99 # max_val = get_max_valid_frequency(func, min_accuracy, 0.1, 0.001, 20) # print "maximum radius (frequency): ", max_val / (pi / 2) # phi, theta = np.mgrid[0:pi:50j, 0:2*pi:50j] # XX = max_val * np.sin(phi) * np.cos(theta) # YY = max_val * np.sin(phi) * np.sin(theta) # ZZ = max_val * np.cos(phi) # zz = vfunc(XX, YY, ZZ) # zzmin, zzmax = zz.min(), zz.max() # print "dispersion error range:", zzmin, "to", zzmax # zz = (zz - zzmin) / (zzmax - zzmin) # # fig = plt.figure() # # bounds = pi / 2 # N = 100 # x = np.linspace(-bounds, bounds, N) # y = np.linspace(-bounds, bounds, N) # X, Y = np.meshgrid(x, y) # Z = np.zeros(X.shape) # depth = np.linspace(0.9, 1, 11) # # ### plot 1 # ax = fig.add_subplot(221 + 0) # z = vfunc(Z, X, Y) # plt.contourf(X, Y, z, depth) # cbar = plt.colorbar() # # ### plot 2 # ax = fig.add_subplot(221 + 1) # z = vfunc(X, Z, Y) # plt.contourf(X, Y, z, depth) # cbar = plt.colorbar() # # ### plot 3 # ax = fig.add_subplot(221 + 2) # z = vfunc(X, Y, Z) # plt.contourf(X, Y, z, depth) # cbar = plt.colorbar() # # ax = fig.add_subplot(224, projection='3d') # ax.plot_surface( # XX, YY, ZZ, rstride=1, cstride=1, facecolors=cm.jet(zz)) # # plt.show() if __name__ == "__main__": main()
gpl-2.0
kaichogami/sympy
sympy/physics/quantum/state.py
58
29186
"""Dirac notation for states.""" from __future__ import print_function, division from sympy import (cacheit, conjugate, Expr, Function, integrate, oo, sqrt, Tuple) from sympy.core.compatibility import u, range from sympy.printing.pretty.stringpict import stringPict from sympy.physics.quantum.qexpr import QExpr, dispatch_method __all__ = [ 'KetBase', 'BraBase', 'StateBase', 'State', 'Ket', 'Bra', 'TimeDepState', 'TimeDepBra', 'TimeDepKet', 'Wavefunction' ] #----------------------------------------------------------------------------- # States, bras and kets. #----------------------------------------------------------------------------- # ASCII brackets _lbracket = "<" _rbracket = ">" _straight_bracket = "|" # Unicode brackets # MATHEMATICAL ANGLE BRACKETS _lbracket_ucode = u("\N{MATHEMATICAL LEFT ANGLE BRACKET}") _rbracket_ucode = u("\N{MATHEMATICAL RIGHT ANGLE BRACKET}") # LIGHT VERTICAL BAR _straight_bracket_ucode = u("\N{LIGHT VERTICAL BAR}") # Other options for unicode printing of <, > and | for Dirac notation. # LEFT-POINTING ANGLE BRACKET # _lbracket = u"\u2329" # _rbracket = u"\u232A" # LEFT ANGLE BRACKET # _lbracket = u"\u3008" # _rbracket = u"\u3009" # VERTICAL LINE # _straight_bracket = u"\u007C" class StateBase(QExpr): """Abstract base class for general abstract states in quantum mechanics. All other state classes defined will need to inherit from this class. It carries the basic structure for all other states such as dual, _eval_adjoint and label. This is an abstract base class and you should not instantiate it directly, instead use State. """ @classmethod def _operators_to_state(self, ops, **options): """ Returns the eigenstate instance for the passed operators. This method should be overridden in subclasses. It will handle being passed either an Operator instance or set of Operator instances. It should return the corresponding state INSTANCE or simply raise a NotImplementedError. See cartesian.py for an example. """ raise NotImplementedError("Cannot map operators to states in this class. Method not implemented!") def _state_to_operators(self, op_classes, **options): """ Returns the operators which this state instance is an eigenstate of. This method should be overridden in subclasses. It will be called on state instances and be passed the operator classes that we wish to make into instances. The state instance will then transform the classes appropriately, or raise a NotImplementedError if it cannot return operator instances. See cartesian.py for examples, """ raise NotImplementedError( "Cannot map this state to operators. Method not implemented!") @property def operators(self): """Return the operator(s) that this state is an eigenstate of""" from .operatorset import state_to_operators # import internally to avoid circular import errors return state_to_operators(self) def _enumerate_state(self, num_states, **options): raise NotImplementedError("Cannot enumerate this state!") def _represent_default_basis(self, **options): return self._represent(basis=self.operators) #------------------------------------------------------------------------- # Dagger/dual #------------------------------------------------------------------------- @property def dual(self): """Return the dual state of this one.""" return self.dual_class()._new_rawargs(self.hilbert_space, *self.args) @classmethod def dual_class(self): """Return the class used to construt the dual.""" raise NotImplementedError( 'dual_class must be implemented in a subclass' ) def _eval_adjoint(self): """Compute the dagger of this state using the dual.""" return self.dual #------------------------------------------------------------------------- # Printing #------------------------------------------------------------------------- def _pretty_brackets(self, height, use_unicode=True): # Return pretty printed brackets for the state # Ideally, this could be done by pform.parens but it does not support the angled < and > # Setup for unicode vs ascii if use_unicode: lbracket, rbracket = self.lbracket_ucode, self.rbracket_ucode slash, bslash, vert = u('\N{BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT}'), \ u('\N{BOX DRAWINGS LIGHT DIAGONAL UPPER LEFT TO LOWER RIGHT}'), \ u('\N{BOX DRAWINGS LIGHT VERTICAL}') else: lbracket, rbracket = self.lbracket, self.rbracket slash, bslash, vert = '/', '\\', '|' # If height is 1, just return brackets if height == 1: return stringPict(lbracket), stringPict(rbracket) # Make height even height += (height % 2) brackets = [] for bracket in lbracket, rbracket: # Create left bracket if bracket in set([_lbracket, _lbracket_ucode]): bracket_args = [ ' ' * (height//2 - i - 1) + slash for i in range(height // 2)] bracket_args.extend( [ ' ' * i + bslash for i in range(height // 2)]) # Create right bracket elif bracket in set([_rbracket, _rbracket_ucode]): bracket_args = [ ' ' * i + bslash for i in range(height // 2)] bracket_args.extend([ ' ' * ( height//2 - i - 1) + slash for i in range(height // 2)]) # Create straight bracket elif bracket in set([_straight_bracket, _straight_bracket_ucode]): bracket_args = [vert for i in range(height)] else: raise ValueError(bracket) brackets.append( stringPict('\n'.join(bracket_args), baseline=height//2)) return brackets def _sympystr(self, printer, *args): contents = self._print_contents(printer, *args) return '%s%s%s' % (self.lbracket, contents, self.rbracket) def _pretty(self, printer, *args): from sympy.printing.pretty.stringpict import prettyForm # Get brackets pform = self._print_contents_pretty(printer, *args) lbracket, rbracket = self._pretty_brackets( pform.height(), printer._use_unicode) # Put together state pform = prettyForm(*pform.left(lbracket)) pform = prettyForm(*pform.right(rbracket)) return pform def _latex(self, printer, *args): contents = self._print_contents_latex(printer, *args) # The extra {} brackets are needed to get matplotlib's latex # rendered to render this properly. return '{%s%s%s}' % (self.lbracket_latex, contents, self.rbracket_latex) class KetBase(StateBase): """Base class for Kets. This class defines the dual property and the brackets for printing. This is an abstract base class and you should not instantiate it directly, instead use Ket. """ lbracket = _straight_bracket rbracket = _rbracket lbracket_ucode = _straight_bracket_ucode rbracket_ucode = _rbracket_ucode lbracket_latex = r'\left|' rbracket_latex = r'\right\rangle ' @classmethod def default_args(self): return ("psi",) @classmethod def dual_class(self): return BraBase def __mul__(self, other): """KetBase*other""" from sympy.physics.quantum.operator import OuterProduct if isinstance(other, BraBase): return OuterProduct(self, other) else: return Expr.__mul__(self, other) def __rmul__(self, other): """other*KetBase""" from sympy.physics.quantum.innerproduct import InnerProduct if isinstance(other, BraBase): return InnerProduct(other, self) else: return Expr.__rmul__(self, other) #------------------------------------------------------------------------- # _eval_* methods #------------------------------------------------------------------------- def _eval_innerproduct(self, bra, **hints): """Evaluate the inner product betweeen this ket and a bra. This is called to compute <bra|ket>, where the ket is ``self``. This method will dispatch to sub-methods having the format:: ``def _eval_innerproduct_BraClass(self, **hints):`` Subclasses should define these methods (one for each BraClass) to teach the ket how to take inner products with bras. """ return dispatch_method(self, '_eval_innerproduct', bra, **hints) def _apply_operator(self, op, **options): """Apply an Operator to this Ket. This method will dispatch to methods having the format:: ``def _apply_operator_OperatorName(op, **options):`` Subclasses should define these methods (one for each OperatorName) to teach the Ket how operators act on it. Parameters ========== op : Operator The Operator that is acting on the Ket. options : dict A dict of key/value pairs that control how the operator is applied to the Ket. """ return dispatch_method(self, '_apply_operator', op, **options) class BraBase(StateBase): """Base class for Bras. This class defines the dual property and the brackets for printing. This is an abstract base class and you should not instantiate it directly, instead use Bra. """ lbracket = _lbracket rbracket = _straight_bracket lbracket_ucode = _lbracket_ucode rbracket_ucode = _straight_bracket_ucode lbracket_latex = r'\left\langle ' rbracket_latex = r'\right|' @classmethod def _operators_to_state(self, ops, **options): state = self.dual_class().operators_to_state(ops, **options) return state.dual def _state_to_operators(self, op_classes, **options): return self.dual._state_to_operators(op_classes, **options) def _enumerate_state(self, num_states, **options): dual_states = self.dual._enumerate_state(num_states, **options) return [x.dual for x in dual_states] @classmethod def default_args(self): return self.dual_class().default_args() @classmethod def dual_class(self): return KetBase def __mul__(self, other): """BraBase*other""" from sympy.physics.quantum.innerproduct import InnerProduct if isinstance(other, KetBase): return InnerProduct(self, other) else: return Expr.__mul__(self, other) def __rmul__(self, other): """other*BraBase""" from sympy.physics.quantum.operator import OuterProduct if isinstance(other, KetBase): return OuterProduct(other, self) else: return Expr.__rmul__(self, other) def _represent(self, **options): """A default represent that uses the Ket's version.""" from sympy.physics.quantum.dagger import Dagger return Dagger(self.dual._represent(**options)) class State(StateBase): """General abstract quantum state used as a base class for Ket and Bra.""" pass class Ket(State, KetBase): """A general time-independent Ket in quantum mechanics. Inherits from State and KetBase. This class should be used as the base class for all physical, time-independent Kets in a system. This class and its subclasses will be the main classes that users will use for expressing Kets in Dirac notation [1]_. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time. Examples ======== Create a simple Ket and looking at its properties:: >>> from sympy.physics.quantum import Ket, Bra >>> from sympy import symbols, I >>> k = Ket('psi') >>> k |psi> >>> k.hilbert_space H >>> k.is_commutative False >>> k.label (psi,) Ket's know about their associated bra:: >>> k.dual <psi| >>> k.dual_class() <class 'sympy.physics.quantum.state.Bra'> Take a linear combination of two kets:: >>> k0 = Ket(0) >>> k1 = Ket(1) >>> 2*I*k0 - 4*k1 2*I*|0> - 4*|1> Compound labels are passed as tuples:: >>> n, m = symbols('n,m') >>> k = Ket(n,m) >>> k |nm> References ========== .. [1] http://en.wikipedia.org/wiki/Bra-ket_notation """ @classmethod def dual_class(self): return Bra class Bra(State, BraBase): """A general time-independent Bra in quantum mechanics. Inherits from State and BraBase. A Bra is the dual of a Ket [1]_. This class and its subclasses will be the main classes that users will use for expressing Bras in Dirac notation. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time. Examples ======== Create a simple Bra and look at its properties:: >>> from sympy.physics.quantum import Ket, Bra >>> from sympy import symbols, I >>> b = Bra('psi') >>> b <psi| >>> b.hilbert_space H >>> b.is_commutative False Bra's know about their dual Ket's:: >>> b.dual |psi> >>> b.dual_class() <class 'sympy.physics.quantum.state.Ket'> Like Kets, Bras can have compound labels and be manipulated in a similar manner:: >>> n, m = symbols('n,m') >>> b = Bra(n,m) - I*Bra(m,n) >>> b -I*<mn| + <nm| Symbols in a Bra can be substituted using ``.subs``:: >>> b.subs(n,m) <mm| - I*<mm| References ========== .. [1] http://en.wikipedia.org/wiki/Bra-ket_notation """ @classmethod def dual_class(self): return Ket #----------------------------------------------------------------------------- # Time dependent states, bras and kets. #----------------------------------------------------------------------------- class TimeDepState(StateBase): """Base class for a general time-dependent quantum state. This class is used as a base class for any time-dependent state. The main difference between this class and the time-independent state is that this class takes a second argument that is the time in addition to the usual label argument. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time as the final argument. """ #------------------------------------------------------------------------- # Initialization #------------------------------------------------------------------------- @classmethod def default_args(self): return ("psi", "t") #------------------------------------------------------------------------- # Properties #------------------------------------------------------------------------- @property def label(self): """The label of the state.""" return self.args[:-1] @property def time(self): """The time of the state.""" return self.args[-1] #------------------------------------------------------------------------- # Printing #------------------------------------------------------------------------- def _print_time(self, printer, *args): return printer._print(self.time, *args) _print_time_repr = _print_time _print_time_latex = _print_time def _print_time_pretty(self, printer, *args): pform = printer._print(self.time, *args) return pform def _print_contents(self, printer, *args): label = self._print_label(printer, *args) time = self._print_time(printer, *args) return '%s;%s' % (label, time) def _print_label_repr(self, printer, *args): label = self._print_sequence(self.label, ',', printer, *args) time = self._print_time_repr(printer, *args) return '%s,%s' % (label, time) def _print_contents_pretty(self, printer, *args): label = self._print_label_pretty(printer, *args) time = self._print_time_pretty(printer, *args) return printer._print_seq((label, time), delimiter=';') def _print_contents_latex(self, printer, *args): label = self._print_sequence( self.label, self._label_separator, printer, *args) time = self._print_time_latex(printer, *args) return '%s;%s' % (label, time) class TimeDepKet(TimeDepState, KetBase): """General time-dependent Ket in quantum mechanics. This inherits from ``TimeDepState`` and ``KetBase`` and is the main class that should be used for Kets that vary with time. Its dual is a ``TimeDepBra``. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time as the final argument. Examples ======== Create a TimeDepKet and look at its attributes:: >>> from sympy.physics.quantum import TimeDepKet >>> k = TimeDepKet('psi', 't') >>> k |psi;t> >>> k.time t >>> k.label (psi,) >>> k.hilbert_space H TimeDepKets know about their dual bra:: >>> k.dual <psi;t| >>> k.dual_class() <class 'sympy.physics.quantum.state.TimeDepBra'> """ @classmethod def dual_class(self): return TimeDepBra class TimeDepBra(TimeDepState, BraBase): """General time-dependent Bra in quantum mechanics. This inherits from TimeDepState and BraBase and is the main class that should be used for Bras that vary with time. Its dual is a TimeDepBra. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time as the final argument. Examples ======== >>> from sympy.physics.quantum import TimeDepBra >>> from sympy import symbols, I >>> b = TimeDepBra('psi', 't') >>> b <psi;t| >>> b.time t >>> b.label (psi,) >>> b.hilbert_space H >>> b.dual |psi;t> """ @classmethod def dual_class(self): return TimeDepKet class Wavefunction(Function): """Class for representations in continuous bases This class takes an expression and coordinates in its constructor. It can be used to easily calculate normalizations and probabilities. Parameters ========== expr : Expr The expression representing the functional form of the w.f. coords : Symbol or tuple The coordinates to be integrated over, and their bounds Examples ======== Particle in a box, specifying bounds in the more primitive way of using Piecewise: >>> from sympy import Symbol, Piecewise, pi, N >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x = Symbol('x', real=True) >>> n = 1 >>> L = 1 >>> g = Piecewise((0, x < 0), (0, x > L), (sqrt(2//L)*sin(n*pi*x/L), True)) >>> f = Wavefunction(g, x) >>> f.norm 1 >>> f.is_normalized True >>> p = f.prob() >>> p(0) 0 >>> p(L) 0 >>> p(0.5) 2 >>> p(0.85*L) 2*sin(0.85*pi)**2 >>> N(p(0.85*L)) 0.412214747707527 Additionally, you can specify the bounds of the function and the indices in a more compact way: >>> from sympy import symbols, pi, diff >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sqrt(2/L)*sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.norm 1 >>> f(L+1) 0 >>> f(L-1) sqrt(2)*sin(pi*n*(L - 1)/L)/sqrt(L) >>> f(-1) 0 >>> f(0.85) sqrt(2)*sin(0.85*pi*n/L)/sqrt(L) >>> f(0.85, n=1, L=1) sqrt(2)*sin(0.85*pi) >>> f.is_commutative False All arguments are automatically sympified, so you can define the variables as strings rather than symbols: >>> expr = x**2 >>> f = Wavefunction(expr, 'x') >>> type(f.variables[0]) <class 'sympy.core.symbol.Symbol'> Derivatives of Wavefunctions will return Wavefunctions: >>> diff(f, x) Wavefunction(2*x, x) """ #Any passed tuples for coordinates and their bounds need to be #converted to Tuples before Function's constructor is called, to #avoid errors from calling is_Float in the constructor def __new__(cls, *args, **options): new_args = [None for i in args] ct = 0 for arg in args: if isinstance(arg, tuple): new_args[ct] = Tuple(*arg) else: new_args[ct] = arg ct += 1 return super(Function, cls).__new__(cls, *new_args, **options) def __call__(self, *args, **options): var = self.variables if len(args) != len(var): raise NotImplementedError( "Incorrect number of arguments to function!") ct = 0 #If the passed value is outside the specified bounds, return 0 for v in var: lower, upper = self.limits[v] #Do the comparison to limits only if the passed symbol is actually #a symbol present in the limits; #Had problems with a comparison of x > L if isinstance(args[ct], Expr) and \ not (lower in args[ct].free_symbols or upper in args[ct].free_symbols): continue if (args[ct] < lower) == True or (args[ct] > upper) == True: return 0 ct += 1 expr = self.expr #Allows user to make a call like f(2, 4, m=1, n=1) for symbol in list(expr.free_symbols): if str(symbol) in options.keys(): val = options[str(symbol)] expr = expr.subs(symbol, val) return expr.subs(zip(var, args)) def _eval_derivative(self, symbol): expr = self.expr deriv = expr._eval_derivative(symbol) return Wavefunction(deriv, *self.args[1:]) def _eval_conjugate(self): return Wavefunction(conjugate(self.expr), *self.args[1:]) def _eval_transpose(self): return self @property def free_symbols(self): return self.expr.free_symbols @property def is_commutative(self): """ Override Function's is_commutative so that order is preserved in represented expressions """ return False @classmethod def eval(self, *args): return None @property def variables(self): """ Return the coordinates which the wavefunction depends on Examples ======== >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy import symbols >>> x,y = symbols('x,y') >>> f = Wavefunction(x*y, x, y) >>> f.variables (x, y) >>> g = Wavefunction(x*y, x) >>> g.variables (x,) """ var = [g[0] if isinstance(g, Tuple) else g for g in self._args[1:]] return tuple(var) @property def limits(self): """ Return the limits of the coordinates which the w.f. depends on If no limits are specified, defaults to ``(-oo, oo)``. Examples ======== >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy import symbols >>> x, y = symbols('x, y') >>> f = Wavefunction(x**2, (x, 0, 1)) >>> f.limits {x: (0, 1)} >>> f = Wavefunction(x**2, x) >>> f.limits {x: (-oo, oo)} >>> f = Wavefunction(x**2 + y**2, x, (y, -1, 2)) >>> f.limits {x: (-oo, oo), y: (-1, 2)} """ limits = [(g[1], g[2]) if isinstance(g, Tuple) else (-oo, oo) for g in self._args[1:]] return dict(zip(self.variables, tuple(limits))) @property def expr(self): """ Return the expression which is the functional form of the Wavefunction Examples ======== >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy import symbols >>> x, y = symbols('x, y') >>> f = Wavefunction(x**2, x) >>> f.expr x**2 """ return self._args[0] @property def is_normalized(self): """ Returns true if the Wavefunction is properly normalized Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sqrt(2/L)*sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.is_normalized True """ return (self.norm == 1.0) @property @cacheit def norm(self): """ Return the normalization of the specified functional form. This function integrates over the coordinates of the Wavefunction, with the bounds specified. Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sqrt(2/L)*sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.norm 1 >>> g = sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.norm sqrt(2)*sqrt(L)/2 """ exp = self.expr*conjugate(self.expr) var = self.variables limits = self.limits for v in var: curr_limits = limits[v] exp = integrate(exp, (v, curr_limits[0], curr_limits[1])) return sqrt(exp) def normalize(self): """ Return a normalized version of the Wavefunction Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x = symbols('x', real=True) >>> L = symbols('L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.normalize() Wavefunction(sqrt(2)*sin(pi*n*x/L)/sqrt(L), (x, 0, L)) """ const = self.norm if const == oo: raise NotImplementedError("The function is not normalizable!") else: return Wavefunction((const)**(-1)*self.expr, *self.args[1:]) def prob(self): """ Return the absolute magnitude of the w.f., `|\psi(x)|^2` Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', real=True) >>> n = symbols('n', integer=True) >>> g = sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.prob() Wavefunction(sin(pi*n*x/L)**2, x) """ return Wavefunction(self.expr*conjugate(self.expr), *self.variables)
bsd-3-clause
lifeinoppo/littlefishlet-scode
RES/REF/python_sourcecode/ipython-master/IPython/sphinxext/ipython_directive.py
12
42845
# -*- coding: utf-8 -*- """ Sphinx directive to support embedded IPython code. This directive allows pasting of entire interactive IPython sessions, prompts and all, and their code will actually get re-executed at doc build time, with all prompts renumbered sequentially. It also allows you to input code as a pure python input by giving the argument python to the directive. The output looks like an interactive ipython section. To enable this directive, simply list it in your Sphinx ``conf.py`` file (making sure the directory where you placed it is visible to sphinx, as is needed for all Sphinx directives). For example, to enable syntax highlighting and the IPython directive:: extensions = ['IPython.sphinxext.ipython_console_highlighting', 'IPython.sphinxext.ipython_directive'] The IPython directive outputs code-blocks with the language 'ipython'. So if you do not have the syntax highlighting extension enabled as well, then all rendered code-blocks will be uncolored. By default this directive assumes that your prompts are unchanged IPython ones, but this can be customized. The configurable options that can be placed in conf.py are: ipython_savefig_dir: The directory in which to save the figures. This is relative to the Sphinx source directory. The default is `html_static_path`. ipython_rgxin: The compiled regular expression to denote the start of IPython input lines. The default is re.compile('In \[(\d+)\]:\s?(.*)\s*'). You shouldn't need to change this. ipython_rgxout: The compiled regular expression to denote the start of IPython output lines. The default is re.compile('Out\[(\d+)\]:\s?(.*)\s*'). You shouldn't need to change this. ipython_promptin: The string to represent the IPython input prompt in the generated ReST. The default is 'In [%d]:'. This expects that the line numbers are used in the prompt. ipython_promptout: The string to represent the IPython prompt in the generated ReST. The default is 'Out [%d]:'. This expects that the line numbers are used in the prompt. ipython_mplbackend: The string which specifies if the embedded Sphinx shell should import Matplotlib and set the backend. The value specifies a backend that is passed to `matplotlib.use()` before any lines in `ipython_execlines` are executed. If not specified in conf.py, then the default value of 'agg' is used. To use the IPython directive without matplotlib as a dependency, set the value to `None`. It may end up that matplotlib is still imported if the user specifies so in `ipython_execlines` or makes use of the @savefig pseudo decorator. ipython_execlines: A list of strings to be exec'd in the embedded Sphinx shell. Typical usage is to make certain packages always available. Set this to an empty list if you wish to have no imports always available. If specified in conf.py as `None`, then it has the effect of making no imports available. If omitted from conf.py altogether, then the default value of ['import numpy as np', 'import matplotlib.pyplot as plt'] is used. ipython_holdcount When the @suppress pseudo-decorator is used, the execution count can be incremented or not. The default behavior is to hold the execution count, corresponding to a value of `True`. Set this to `False` to increment the execution count after each suppressed command. As an example, to use the IPython directive when `matplotlib` is not available, one sets the backend to `None`:: ipython_mplbackend = None An example usage of the directive is: .. code-block:: rst .. ipython:: In [1]: x = 1 In [2]: y = x**2 In [3]: print(y) See http://matplotlib.org/sampledoc/ipython_directive.html for additional documentation. Pseudo-Decorators ================= Note: Only one decorator is supported per input. If more than one decorator is specified, then only the last one is used. In addition to the Pseudo-Decorators/options described at the above link, several enhancements have been made. The directive will emit a message to the console at build-time if code-execution resulted in an exception or warning. You can suppress these on a per-block basis by specifying the :okexcept: or :okwarning: options: .. code-block:: rst .. ipython:: :okexcept: :okwarning: In [1]: 1/0 In [2]: # raise warning. ToDo ---- - Turn the ad-hoc test() function into a real test suite. - Break up ipython-specific functionality from matplotlib stuff into better separated code. Authors ------- - John D Hunter: orignal author. - Fernando Perez: refactoring, documentation, cleanups, port to 0.11. - VáclavŠmilauer <eudoxos-AT-arcig.cz>: Prompt generalizations. - Skipper Seabold, refactoring, cleanups, pure python addition """ from __future__ import print_function #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- # Stdlib import atexit import os import re import sys import tempfile import ast import warnings import shutil # Third-party from docutils.parsers.rst import directives from sphinx.util.compat import Directive # Our own from traitlets.config import Config from IPython import InteractiveShell from IPython.core.profiledir import ProfileDir from IPython.utils import io from IPython.utils.py3compat import PY3 if PY3: from io import StringIO else: from StringIO import StringIO #----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- # for tokenizing blocks COMMENT, INPUT, OUTPUT = range(3) #----------------------------------------------------------------------------- # Functions and class declarations #----------------------------------------------------------------------------- def block_parser(part, rgxin, rgxout, fmtin, fmtout): """ part is a string of ipython text, comprised of at most one input, one output, comments, and blank lines. The block parser parses the text into a list of:: blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and data is, depending on the type of token:: COMMENT : the comment string INPUT: the (DECORATOR, INPUT_LINE, REST) where DECORATOR: the input decorator (or None) INPUT_LINE: the input as string (possibly multi-line) REST : any stdout generated by the input line (not OUTPUT) OUTPUT: the output string, possibly multi-line """ block = [] lines = part.split('\n') N = len(lines) i = 0 decorator = None while 1: if i==N: # nothing left to parse -- the last line break line = lines[i] i += 1 line_stripped = line.strip() if line_stripped.startswith('#'): block.append((COMMENT, line)) continue if line_stripped.startswith('@'): # Here is where we assume there is, at most, one decorator. # Might need to rethink this. decorator = line_stripped continue # does this look like an input line? matchin = rgxin.match(line) if matchin: lineno, inputline = int(matchin.group(1)), matchin.group(2) # the ....: continuation string continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) Nc = len(continuation) # input lines can continue on for more than one line, if # we have a '\' line continuation char or a function call # echo line 'print'. The input line can only be # terminated by the end of the block or an output line, so # we parse out the rest of the input line if it is # multiline as well as any echo text rest = [] while i<N: # look ahead; if the next line is blank, or a comment, or # an output line, we're done nextline = lines[i] matchout = rgxout.match(nextline) #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation)) if matchout or nextline.startswith('#'): break elif nextline.startswith(continuation): # The default ipython_rgx* treat the space following the colon as optional. # However, If the space is there we must consume it or code # employing the cython_magic extension will fail to execute. # # This works with the default ipython_rgx* patterns, # If you modify them, YMMV. nextline = nextline[Nc:] if nextline and nextline[0] == ' ': nextline = nextline[1:] inputline += '\n' + nextline else: rest.append(nextline) i+= 1 block.append((INPUT, (decorator, inputline, '\n'.join(rest)))) continue # if it looks like an output line grab all the text to the end # of the block matchout = rgxout.match(line) if matchout: lineno, output = int(matchout.group(1)), matchout.group(2) if i<N-1: output = '\n'.join([output] + lines[i:]) block.append((OUTPUT, output)) break return block class EmbeddedSphinxShell(object): """An embedded IPython instance to run inside Sphinx""" def __init__(self, exec_lines=None): self.cout = StringIO() if exec_lines is None: exec_lines = [] # Create config object for IPython config = Config() config.HistoryManager.hist_file = ':memory:' config.InteractiveShell.autocall = False config.InteractiveShell.autoindent = False config.InteractiveShell.colors = 'NoColor' # create a profile so instance history isn't saved tmp_profile_dir = tempfile.mkdtemp(prefix='profile_') profname = 'auto_profile_sphinx_build' pdir = os.path.join(tmp_profile_dir,profname) profile = ProfileDir.create_profile_dir(pdir) # Create and initialize global ipython, but don't start its mainloop. # This will persist across different EmbededSphinxShell instances. IP = InteractiveShell.instance(config=config, profile_dir=profile) atexit.register(self.cleanup) # io.stdout redirect must be done after instantiating InteractiveShell io.stdout = self.cout io.stderr = self.cout # For debugging, so we can see normal output, use this: #from IPython.utils.io import Tee #io.stdout = Tee(self.cout, channel='stdout') # dbg #io.stderr = Tee(self.cout, channel='stderr') # dbg # Store a few parts of IPython we'll need. self.IP = IP self.user_ns = self.IP.user_ns self.user_global_ns = self.IP.user_global_ns self.input = '' self.output = '' self.tmp_profile_dir = tmp_profile_dir self.is_verbatim = False self.is_doctest = False self.is_suppress = False # Optionally, provide more detailed information to shell. # this is assigned by the SetUp method of IPythonDirective # to point at itself. # # So, you can access handy things at self.directive.state self.directive = None # on the first call to the savefig decorator, we'll import # pyplot as plt so we can make a call to the plt.gcf().savefig self._pyplot_imported = False # Prepopulate the namespace. for line in exec_lines: self.process_input_line(line, store_history=False) def cleanup(self): shutil.rmtree(self.tmp_profile_dir, ignore_errors=True) def clear_cout(self): self.cout.seek(0) self.cout.truncate(0) def process_input_line(self, line, store_history=True): """process the input, capturing stdout""" stdout = sys.stdout splitter = self.IP.input_splitter try: sys.stdout = self.cout splitter.push(line) more = splitter.push_accepts_more() if not more: source_raw = splitter.raw_reset() self.IP.run_cell(source_raw, store_history=store_history) finally: sys.stdout = stdout def process_image(self, decorator): """ # build out an image directive like # .. image:: somefile.png # :width 4in # # from an input like # savefig somefile.png width=4in """ savefig_dir = self.savefig_dir source_dir = self.source_dir saveargs = decorator.split(' ') filename = saveargs[1] # insert relative path to image file in source outfile = os.path.relpath(os.path.join(savefig_dir,filename), source_dir) imagerows = ['.. image:: %s'%outfile] for kwarg in saveargs[2:]: arg, val = kwarg.split('=') arg = arg.strip() val = val.strip() imagerows.append(' :%s: %s'%(arg, val)) image_file = os.path.basename(outfile) # only return file name image_directive = '\n'.join(imagerows) return image_file, image_directive # Callbacks for each type of token def process_input(self, data, input_prompt, lineno): """ Process data block for INPUT token. """ decorator, input, rest = data image_file = None image_directive = None is_verbatim = decorator=='@verbatim' or self.is_verbatim is_doctest = (decorator is not None and \ decorator.startswith('@doctest')) or self.is_doctest is_suppress = decorator=='@suppress' or self.is_suppress is_okexcept = decorator=='@okexcept' or self.is_okexcept is_okwarning = decorator=='@okwarning' or self.is_okwarning is_savefig = decorator is not None and \ decorator.startswith('@savefig') input_lines = input.split('\n') if len(input_lines) > 1: if input_lines[-1] != "": input_lines.append('') # make sure there's a blank line # so splitter buffer gets reset continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) if is_savefig: image_file, image_directive = self.process_image(decorator) ret = [] is_semicolon = False # Hold the execution count, if requested to do so. if is_suppress and self.hold_count: store_history = False else: store_history = True # Note: catch_warnings is not thread safe with warnings.catch_warnings(record=True) as ws: for i, line in enumerate(input_lines): if line.endswith(';'): is_semicolon = True if i == 0: # process the first input line if is_verbatim: self.process_input_line('') self.IP.execution_count += 1 # increment it anyway else: # only submit the line in non-verbatim mode self.process_input_line(line, store_history=store_history) formatted_line = '%s %s'%(input_prompt, line) else: # process a continuation line if not is_verbatim: self.process_input_line(line, store_history=store_history) formatted_line = '%s %s'%(continuation, line) if not is_suppress: ret.append(formatted_line) if not is_suppress and len(rest.strip()) and is_verbatim: # The "rest" is the standard output of the input. This needs to be # added when in verbatim mode. If there is no "rest", then we don't # add it, as the new line will be added by the processed output. ret.append(rest) # Fetch the processed output. (This is not the submitted output.) self.cout.seek(0) processed_output = self.cout.read() if not is_suppress and not is_semicolon: # # In IPythonDirective.run, the elements of `ret` are eventually # combined such that '' entries correspond to newlines. So if # `processed_output` is equal to '', then the adding it to `ret` # ensures that there is a blank line between consecutive inputs # that have no outputs, as in: # # In [1]: x = 4 # # In [2]: x = 5 # # When there is processed output, it has a '\n' at the tail end. So # adding the output to `ret` will provide the necessary spacing # between consecutive input/output blocks, as in: # # In [1]: x # Out[1]: 5 # # In [2]: x # Out[2]: 5 # # When there is stdout from the input, it also has a '\n' at the # tail end, and so this ensures proper spacing as well. E.g.: # # In [1]: print x # 5 # # In [2]: x = 5 # # When in verbatim mode, `processed_output` is empty (because # nothing was passed to IP. Sometimes the submitted code block has # an Out[] portion and sometimes it does not. When it does not, we # need to ensure proper spacing, so we have to add '' to `ret`. # However, if there is an Out[] in the submitted code, then we do # not want to add a newline as `process_output` has stuff to add. # The difficulty is that `process_input` doesn't know if # `process_output` will be called---so it doesn't know if there is # Out[] in the code block. The requires that we include a hack in # `process_block`. See the comments there. # ret.append(processed_output) elif is_semicolon: # Make sure there is a newline after the semicolon. ret.append('') # context information filename = "Unknown" lineno = 0 if self.directive.state: filename = self.directive.state.document.current_source lineno = self.directive.state.document.current_line # output any exceptions raised during execution to stdout # unless :okexcept: has been specified. if not is_okexcept and "Traceback" in processed_output: s = "\nException in %s at block ending on line %s\n" % (filename, lineno) s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n" sys.stdout.write('\n\n>>>' + ('-' * 73)) sys.stdout.write(s) sys.stdout.write(processed_output) sys.stdout.write('<<<' + ('-' * 73) + '\n\n') # output any warning raised during execution to stdout # unless :okwarning: has been specified. if not is_okwarning: for w in ws: s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno) s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n" sys.stdout.write('\n\n>>>' + ('-' * 73)) sys.stdout.write(s) sys.stdout.write(('-' * 76) + '\n') s=warnings.formatwarning(w.message, w.category, w.filename, w.lineno, w.line) sys.stdout.write(s) sys.stdout.write('<<<' + ('-' * 73) + '\n') self.cout.truncate(0) return (ret, input_lines, processed_output, is_doctest, decorator, image_file, image_directive) def process_output(self, data, output_prompt, input_lines, output, is_doctest, decorator, image_file): """ Process data block for OUTPUT token. """ # Recall: `data` is the submitted output, and `output` is the processed # output from `input_lines`. TAB = ' ' * 4 if is_doctest and output is not None: found = output # This is the processed output found = found.strip() submitted = data.strip() if self.directive is None: source = 'Unavailable' content = 'Unavailable' else: source = self.directive.state.document.current_source content = self.directive.content # Add tabs and join into a single string. content = '\n'.join([TAB + line for line in content]) # Make sure the output contains the output prompt. ind = found.find(output_prompt) if ind < 0: e = ('output does not contain output prompt\n\n' 'Document source: {0}\n\n' 'Raw content: \n{1}\n\n' 'Input line(s):\n{TAB}{2}\n\n' 'Output line(s):\n{TAB}{3}\n\n') e = e.format(source, content, '\n'.join(input_lines), repr(found), TAB=TAB) raise RuntimeError(e) found = found[len(output_prompt):].strip() # Handle the actual doctest comparison. if decorator.strip() == '@doctest': # Standard doctest if found != submitted: e = ('doctest failure\n\n' 'Document source: {0}\n\n' 'Raw content: \n{1}\n\n' 'On input line(s):\n{TAB}{2}\n\n' 'we found output:\n{TAB}{3}\n\n' 'instead of the expected:\n{TAB}{4}\n\n') e = e.format(source, content, '\n'.join(input_lines), repr(found), repr(submitted), TAB=TAB) raise RuntimeError(e) else: self.custom_doctest(decorator, input_lines, found, submitted) # When in verbatim mode, this holds additional submitted output # to be written in the final Sphinx output. # https://github.com/ipython/ipython/issues/5776 out_data = [] is_verbatim = decorator=='@verbatim' or self.is_verbatim if is_verbatim and data.strip(): # Note that `ret` in `process_block` has '' as its last element if # the code block was in verbatim mode. So if there is no submitted # output, then we will have proper spacing only if we do not add # an additional '' to `out_data`. This is why we condition on # `and data.strip()`. # The submitted output has no output prompt. If we want the # prompt and the code to appear, we need to join them now # instead of adding them separately---as this would create an # undesired newline. How we do this ultimately depends on the # format of the output regex. I'll do what works for the default # prompt for now, and we might have to adjust if it doesn't work # in other cases. Finally, the submitted output does not have # a trailing newline, so we must add it manually. out_data.append("{0} {1}\n".format(output_prompt, data)) return out_data def process_comment(self, data): """Process data fPblock for COMMENT token.""" if not self.is_suppress: return [data] def save_image(self, image_file): """ Saves the image file to disk. """ self.ensure_pyplot() command = 'plt.gcf().savefig("%s")'%image_file #print 'SAVEFIG', command # dbg self.process_input_line('bookmark ipy_thisdir', store_history=False) self.process_input_line('cd -b ipy_savedir', store_history=False) self.process_input_line(command, store_history=False) self.process_input_line('cd -b ipy_thisdir', store_history=False) self.process_input_line('bookmark -d ipy_thisdir', store_history=False) self.clear_cout() def process_block(self, block): """ process block from the block_parser and return a list of processed lines """ ret = [] output = None input_lines = None lineno = self.IP.execution_count input_prompt = self.promptin % lineno output_prompt = self.promptout % lineno image_file = None image_directive = None found_input = False for token, data in block: if token == COMMENT: out_data = self.process_comment(data) elif token == INPUT: found_input = True (out_data, input_lines, output, is_doctest, decorator, image_file, image_directive) = \ self.process_input(data, input_prompt, lineno) elif token == OUTPUT: if not found_input: TAB = ' ' * 4 linenumber = 0 source = 'Unavailable' content = 'Unavailable' if self.directive: linenumber = self.directive.state.document.current_line source = self.directive.state.document.current_source content = self.directive.content # Add tabs and join into a single string. content = '\n'.join([TAB + line for line in content]) e = ('\n\nInvalid block: Block contains an output prompt ' 'without an input prompt.\n\n' 'Document source: {0}\n\n' 'Content begins at line {1}: \n\n{2}\n\n' 'Problematic block within content: \n\n{TAB}{3}\n\n') e = e.format(source, linenumber, content, block, TAB=TAB) # Write, rather than include in exception, since Sphinx # will truncate tracebacks. sys.stdout.write(e) raise RuntimeError('An invalid block was detected.') out_data = \ self.process_output(data, output_prompt, input_lines, output, is_doctest, decorator, image_file) if out_data: # Then there was user submitted output in verbatim mode. # We need to remove the last element of `ret` that was # added in `process_input`, as it is '' and would introduce # an undesirable newline. assert(ret[-1] == '') del ret[-1] if out_data: ret.extend(out_data) # save the image files if image_file is not None: self.save_image(image_file) return ret, image_directive def ensure_pyplot(self): """ Ensures that pyplot has been imported into the embedded IPython shell. Also, makes sure to set the backend appropriately if not set already. """ # We are here if the @figure pseudo decorator was used. Thus, it's # possible that we could be here even if python_mplbackend were set to # `None`. That's also strange and perhaps worthy of raising an # exception, but for now, we just set the backend to 'agg'. if not self._pyplot_imported: if 'matplotlib.backends' not in sys.modules: # Then ipython_matplotlib was set to None but there was a # call to the @figure decorator (and ipython_execlines did # not set a backend). #raise Exception("No backend was set, but @figure was used!") import matplotlib matplotlib.use('agg') # Always import pyplot into embedded shell. self.process_input_line('import matplotlib.pyplot as plt', store_history=False) self._pyplot_imported = True def process_pure_python(self, content): """ content is a list of strings. it is unedited directive content This runs it line by line in the InteractiveShell, prepends prompts as needed capturing stderr and stdout, then returns the content as a list as if it were ipython code """ output = [] savefig = False # keep up with this to clear figure multiline = False # to handle line continuation multiline_start = None fmtin = self.promptin ct = 0 for lineno, line in enumerate(content): line_stripped = line.strip() if not len(line): output.append(line) continue # handle decorators if line_stripped.startswith('@'): output.extend([line]) if 'savefig' in line: savefig = True # and need to clear figure continue # handle comments if line_stripped.startswith('#'): output.extend([line]) continue # deal with lines checking for multiline continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2)) if not multiline: modified = u"%s %s" % (fmtin % ct, line_stripped) output.append(modified) ct += 1 try: ast.parse(line_stripped) output.append(u'') except Exception: # on a multiline multiline = True multiline_start = lineno else: # still on a multiline modified = u'%s %s' % (continuation, line) output.append(modified) # if the next line is indented, it should be part of multiline if len(content) > lineno + 1: nextline = content[lineno + 1] if len(nextline) - len(nextline.lstrip()) > 3: continue try: mod = ast.parse( '\n'.join(content[multiline_start:lineno+1])) if isinstance(mod.body[0], ast.FunctionDef): # check to see if we have the whole function for element in mod.body[0].body: if isinstance(element, ast.Return): multiline = False else: output.append(u'') multiline = False except Exception: pass if savefig: # clear figure if plotted self.ensure_pyplot() self.process_input_line('plt.clf()', store_history=False) self.clear_cout() savefig = False return output def custom_doctest(self, decorator, input_lines, found, submitted): """ Perform a specialized doctest. """ from .custom_doctests import doctests args = decorator.split() doctest_type = args[1] if doctest_type in doctests: doctests[doctest_type](self, args, input_lines, found, submitted) else: e = "Invalid option to @doctest: {0}".format(doctest_type) raise Exception(e) class IPythonDirective(Directive): has_content = True required_arguments = 0 optional_arguments = 4 # python, suppress, verbatim, doctest final_argumuent_whitespace = True option_spec = { 'python': directives.unchanged, 'suppress' : directives.flag, 'verbatim' : directives.flag, 'doctest' : directives.flag, 'okexcept': directives.flag, 'okwarning': directives.flag } shell = None seen_docs = set() def get_config_options(self): # contains sphinx configuration variables config = self.state.document.settings.env.config # get config variables to set figure output directory outdir = self.state.document.settings.env.app.outdir savefig_dir = config.ipython_savefig_dir source_dir = os.path.dirname(self.state.document.current_source) if savefig_dir is None: savefig_dir = config.html_static_path or '_static' if isinstance(savefig_dir, list): savefig_dir = os.path.join(*savefig_dir) savefig_dir = os.path.join(outdir, savefig_dir) # get regex and prompt stuff rgxin = config.ipython_rgxin rgxout = config.ipython_rgxout promptin = config.ipython_promptin promptout = config.ipython_promptout mplbackend = config.ipython_mplbackend exec_lines = config.ipython_execlines hold_count = config.ipython_holdcount return (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout, mplbackend, exec_lines, hold_count) def setup(self): # Get configuration values. (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout, mplbackend, exec_lines, hold_count) = self.get_config_options() if self.shell is None: # We will be here many times. However, when the # EmbeddedSphinxShell is created, its interactive shell member # is the same for each instance. if mplbackend: import matplotlib # Repeated calls to use() will not hurt us since `mplbackend` # is the same each time. matplotlib.use(mplbackend) # Must be called after (potentially) importing matplotlib and # setting its backend since exec_lines might import pylab. self.shell = EmbeddedSphinxShell(exec_lines) # Store IPython directive to enable better error messages self.shell.directive = self # reset the execution count if we haven't processed this doc #NOTE: this may be borked if there are multiple seen_doc tmp files #check time stamp? if not self.state.document.current_source in self.seen_docs: self.shell.IP.history_manager.reset() self.shell.IP.execution_count = 1 self.shell.IP.prompt_manager.width = 0 self.seen_docs.add(self.state.document.current_source) # and attach to shell so we don't have to pass them around self.shell.rgxin = rgxin self.shell.rgxout = rgxout self.shell.promptin = promptin self.shell.promptout = promptout self.shell.savefig_dir = savefig_dir self.shell.source_dir = source_dir self.shell.hold_count = hold_count # setup bookmark for saving figures directory self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir, store_history=False) self.shell.clear_cout() return rgxin, rgxout, promptin, promptout def teardown(self): # delete last bookmark self.shell.process_input_line('bookmark -d ipy_savedir', store_history=False) self.shell.clear_cout() def run(self): debug = False #TODO, any reason block_parser can't be a method of embeddable shell # then we wouldn't have to carry these around rgxin, rgxout, promptin, promptout = self.setup() options = self.options self.shell.is_suppress = 'suppress' in options self.shell.is_doctest = 'doctest' in options self.shell.is_verbatim = 'verbatim' in options self.shell.is_okexcept = 'okexcept' in options self.shell.is_okwarning = 'okwarning' in options # handle pure python code if 'python' in self.arguments: content = self.content self.content = self.shell.process_pure_python(content) # parts consists of all text within the ipython-block. # Each part is an input/output block. parts = '\n'.join(self.content).split('\n\n') lines = ['.. code-block:: ipython', ''] figures = [] for part in parts: block = block_parser(part, rgxin, rgxout, promptin, promptout) if len(block): rows, figure = self.shell.process_block(block) for row in rows: lines.extend([' {0}'.format(line) for line in row.split('\n')]) if figure is not None: figures.append(figure) for figure in figures: lines.append('') lines.extend(figure.split('\n')) lines.append('') if len(lines) > 2: if debug: print('\n'.join(lines)) else: # This has to do with input, not output. But if we comment # these lines out, then no IPython code will appear in the # final output. self.state_machine.insert_input( lines, self.state_machine.input_lines.source(0)) # cleanup self.teardown() return [] # Enable as a proper Sphinx directive def setup(app): setup.app = app app.add_directive('ipython', IPythonDirective) app.add_config_value('ipython_savefig_dir', None, 'env') app.add_config_value('ipython_rgxin', re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env') app.add_config_value('ipython_rgxout', re.compile('Out\[(\d+)\]:\s?(.*)\s*'), 'env') app.add_config_value('ipython_promptin', 'In [%d]:', 'env') app.add_config_value('ipython_promptout', 'Out[%d]:', 'env') # We could just let matplotlib pick whatever is specified as the default # backend in the matplotlibrc file, but this would cause issues if the # backend didn't work in headless environments. For this reason, 'agg' # is a good default backend choice. app.add_config_value('ipython_mplbackend', 'agg', 'env') # If the user sets this config value to `None`, then EmbeddedSphinxShell's # __init__ method will treat it as []. execlines = ['import numpy as np', 'import matplotlib.pyplot as plt'] app.add_config_value('ipython_execlines', execlines, 'env') app.add_config_value('ipython_holdcount', True, 'env') metadata = {'parallel_read_safe': True, 'parallel_write_safe': True} return metadata # Simple smoke test, needs to be converted to a proper automatic test. def test(): examples = [ r""" In [9]: pwd Out[9]: '/home/jdhunter/py4science/book' In [10]: cd bookdata/ /home/jdhunter/py4science/book/bookdata In [2]: from pylab import * In [2]: ion() In [3]: im = imread('stinkbug.png') @savefig mystinkbug.png width=4in In [4]: imshow(im) Out[4]: <matplotlib.image.AxesImage object at 0x39ea850> """, r""" In [1]: x = 'hello world' # string methods can be # used to alter the string @doctest In [2]: x.upper() Out[2]: 'HELLO WORLD' @verbatim In [3]: x.st<TAB> x.startswith x.strip """, r""" In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\ .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv' In [131]: print url.split('&') ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv'] In [60]: import urllib """, r"""\ In [133]: import numpy.random @suppress In [134]: numpy.random.seed(2358) @doctest In [135]: numpy.random.rand(10,2) Out[135]: array([[ 0.64524308, 0.59943846], [ 0.47102322, 0.8715456 ], [ 0.29370834, 0.74776844], [ 0.99539577, 0.1313423 ], [ 0.16250302, 0.21103583], [ 0.81626524, 0.1312433 ], [ 0.67338089, 0.72302393], [ 0.7566368 , 0.07033696], [ 0.22591016, 0.77731835], [ 0.0072729 , 0.34273127]]) """, r""" In [106]: print x jdh In [109]: for i in range(10): .....: print i .....: .....: 0 1 2 3 4 5 6 7 8 9 """, r""" In [144]: from pylab import * In [145]: ion() # use a semicolon to suppress the output @savefig test_hist.png width=4in In [151]: hist(np.random.randn(10000), 100); @savefig test_plot.png width=4in In [151]: plot(np.random.randn(10000), 'o'); """, r""" # use a semicolon to suppress the output In [151]: plt.clf() @savefig plot_simple.png width=4in In [151]: plot([1,2,3]) @savefig hist_simple.png width=4in In [151]: hist(np.random.randn(10000), 100); """, r""" # update the current fig In [151]: ylabel('number') In [152]: title('normal distribution') @savefig hist_with_text.png In [153]: grid(True) @doctest float In [154]: 0.1 + 0.2 Out[154]: 0.3 @doctest float In [155]: np.arange(16).reshape(4,4) Out[155]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) In [1]: x = np.arange(16, dtype=float).reshape(4,4) In [2]: x[0,0] = np.inf In [3]: x[0,1] = np.nan @doctest float In [4]: x Out[4]: array([[ inf, nan, 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 12., 13., 14., 15.]]) """, ] # skip local-file depending first example: examples = examples[1:] #ipython_directive.DEBUG = True # dbg #options = dict(suppress=True) # dbg options = dict() for example in examples: content = example.split('\n') IPythonDirective('debug', arguments=None, options=options, content=content, lineno=0, content_offset=None, block_text=None, state=None, state_machine=None, ) # Run test suite as a script if __name__=='__main__': if not os.path.isdir('_static'): os.mkdir('_static') test() print('All OK? Check figures in _static/')
gpl-2.0
jm-begon/scikit-learn
sklearn/__init__.py
154
3014
""" Machine learning module for Python ================================== sklearn is a Python module integrating classical machine learning algorithms in the tightly-knit world of scientific Python packages (numpy, scipy, matplotlib). It aims to provide simple and efficient solutions to learning problems that are accessible to everybody and reusable in various contexts: machine-learning as a versatile tool for science and engineering. See http://scikit-learn.org for complete documentation. """ import sys import re import warnings # Make sure that DeprecationWarning within this package always gets printed warnings.filterwarnings('always', category=DeprecationWarning, module='^{0}\.'.format(re.escape(__name__))) # PEP0440 compatible formatted version, see: # https://www.python.org/dev/peps/pep-0440/ # # Generic release markers: # X.Y # X.Y.Z # For bugfix releases # # Admissible pre-release markers: # X.YaN # Alpha release # X.YbN # Beta release # X.YrcN # Release Candidate # X.Y # Final release # # Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer. # 'X.Y.dev0' is the canonical version of 'X.Y.dev' # __version__ = '0.17.dev0' try: # This variable is injected in the __builtins__ by the build # process. It used to enable importing subpackages of sklearn when # the binaries are not built __SKLEARN_SETUP__ except NameError: __SKLEARN_SETUP__ = False if __SKLEARN_SETUP__: sys.stderr.write('Partial import of sklearn during the build process.\n') # We are not importing the rest of the scikit during the build # process, as it may not be compiled yet else: from . import __check_build from .base import clone __check_build # avoid flakes unused variable error __all__ = ['calibration', 'cluster', 'covariance', 'cross_decomposition', 'cross_validation', 'datasets', 'decomposition', 'dummy', 'ensemble', 'externals', 'feature_extraction', 'feature_selection', 'gaussian_process', 'grid_search', 'isotonic', 'kernel_approximation', 'kernel_ridge', 'lda', 'learning_curve', 'linear_model', 'manifold', 'metrics', 'mixture', 'multiclass', 'naive_bayes', 'neighbors', 'neural_network', 'pipeline', 'preprocessing', 'qda', 'random_projection', 'semi_supervised', 'svm', 'tree', # Non-modules: 'clone'] def setup_module(module): """Fixture for the tests to assure globally controllable seeding of RNGs""" import os import numpy as np import random # It could have been provided in the environment _random_seed = os.environ.get('SKLEARN_SEED', None) if _random_seed is None: _random_seed = np.random.uniform() * (2 ** 31 - 1) _random_seed = int(_random_seed) print("I: Seeding RNGs with %r" % _random_seed) np.random.seed(_random_seed) random.seed(_random_seed)
bsd-3-clause
riddlezyc/geolab
src/structure/Z.py
1
1474
# -*- coding: utf-8 -*- # from framesplit import trajectory # too slow using this module import matplotlib.pyplot as plt dirName = r"F:\simulations\asphaltenes\na-mont\TMBO-oil\water\373-continue/" xyzName = 'all.xyz' hetero = 'O' # 'oh' 'N' 'sp' 'O' 'Np' 'sp' with open(dirName + xyzName, 'r') as foo: coords = foo.readlines() nAtoms = int(coords[0]) nFrames = int(len(coords) / (nAtoms + 2)) pos = [] for i in range(nFrames): istart = i * (nAtoms + 2) iend = (i + 1) * (nAtoms + 2) pos.append(coords[istart:iend]) # for i in range(200): # print coords[i] heteroatom = 0 # all of my molecules have atoms less than 200 for i in range(200): x = pos[0][i].split()[0] if x == hetero: heteroatom = i break heteroZ = [] for p in pos: # print p[heteroatom].split()[0] zx = float(p[heteroatom].split()[3]) if zx < 10: zx = zx + 80 heteroZ.append(zx) with open(dirName + 'heteroZ.dat', 'w') as foo: for i, z in enumerate(heteroZ): print >> foo, "%3d %8.5f" % (i, z) # energy plot plt.figure(0, figsize=(8, 4)) figName = dirName + 'heteroZ.png' plt.title('z of heteroatom', fontsize=20) plt.plot(range(len(heteroZ)-1), heteroZ[1:], linewidth=2) plt.grid(True) plt.xlabel('steps') plt.ylabel('Z') plt.axis([0, len(heteroZ)*1.1, 0, max(heteroZ)*1.1]) plt.savefig(figName, format='png', dpi=300) plt.close()
gpl-3.0
bikong2/scikit-learn
examples/ensemble/plot_ensemble_oob.py
259
3265
""" ============================= OOB Errors for Random Forests ============================= The ``RandomForestClassifier`` is trained using *bootstrap aggregation*, where each new tree is fit from a bootstrap sample of the training observations :math:`z_i = (x_i, y_i)`. The *out-of-bag* (OOB) error is the average error for each :math:`z_i` calculated using predictions from the trees that do not contain :math:`z_i` in their respective bootstrap sample. This allows the ``RandomForestClassifier`` to be fit and validated whilst being trained [1]. The example below demonstrates how the OOB error can be measured at the addition of each new tree during training. The resulting plot allows a practitioner to approximate a suitable value of ``n_estimators`` at which the error stabilizes. .. [1] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical Learning Ed. 2", p592-593, Springer, 2009. """ import matplotlib.pyplot as plt from collections import OrderedDict from sklearn.datasets import make_classification from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier # Author: Kian Ho <hui.kian.ho@gmail.com> # Gilles Louppe <g.louppe@gmail.com> # Andreas Mueller <amueller@ais.uni-bonn.de> # # License: BSD 3 Clause print(__doc__) RANDOM_STATE = 123 # Generate a binary classification dataset. X, y = make_classification(n_samples=500, n_features=25, n_clusters_per_class=1, n_informative=15, random_state=RANDOM_STATE) # NOTE: Setting the `warm_start` construction parameter to `True` disables # support for paralellised ensembles but is necessary for tracking the OOB # error trajectory during training. ensemble_clfs = [ ("RandomForestClassifier, max_features='sqrt'", RandomForestClassifier(warm_start=True, oob_score=True, max_features="sqrt", random_state=RANDOM_STATE)), ("RandomForestClassifier, max_features='log2'", RandomForestClassifier(warm_start=True, max_features='log2', oob_score=True, random_state=RANDOM_STATE)), ("RandomForestClassifier, max_features=None", RandomForestClassifier(warm_start=True, max_features=None, oob_score=True, random_state=RANDOM_STATE)) ] # Map a classifier name to a list of (<n_estimators>, <error rate>) pairs. error_rate = OrderedDict((label, []) for label, _ in ensemble_clfs) # Range of `n_estimators` values to explore. min_estimators = 15 max_estimators = 175 for label, clf in ensemble_clfs: for i in range(min_estimators, max_estimators + 1): clf.set_params(n_estimators=i) clf.fit(X, y) # Record the OOB error for each `n_estimators=i` setting. oob_error = 1 - clf.oob_score_ error_rate[label].append((i, oob_error)) # Generate the "OOB error rate" vs. "n_estimators" plot. for label, clf_err in error_rate.items(): xs, ys = zip(*clf_err) plt.plot(xs, ys, label=label) plt.xlim(min_estimators, max_estimators) plt.xlabel("n_estimators") plt.ylabel("OOB error rate") plt.legend(loc="upper right") plt.show()
bsd-3-clause
kushalbhola/MyStuff
Practice/PythonApplication/env/Lib/site-packages/pandas/tests/extension/test_numpy.py
2
12536
import numpy as np import pytest from pandas.compat.numpy import _np_version_under1p16 import pandas as pd from pandas.core.arrays.numpy_ import PandasArray, PandasDtype import pandas.util.testing as tm from . import base @pytest.fixture(params=["float", "object"]) def dtype(request): return PandasDtype(np.dtype(request.param)) @pytest.fixture def allow_in_pandas(monkeypatch): """ A monkeypatch to tells pandas to let us in. By default, passing a PandasArray to an index / series / frame constructor will unbox that PandasArray to an ndarray, and treat it as a non-EA column. We don't want people using EAs without reason. The mechanism for this is a check against ABCPandasArray in each constructor. But, for testing, we need to allow them in pandas. So we patch the _typ of PandasArray, so that we evade the ABCPandasArray check. """ with monkeypatch.context() as m: m.setattr(PandasArray, "_typ", "extension") yield @pytest.fixture def data(allow_in_pandas, dtype): if dtype.numpy_dtype == "object": return pd.Series([(i,) for i in range(100)]).array return PandasArray(np.arange(1, 101, dtype=dtype._dtype)) @pytest.fixture def data_missing(allow_in_pandas, dtype): # For NumPy <1.16, np.array([np.nan, (1,)]) raises # ValueError: setting an array element with a sequence. if dtype.numpy_dtype == "object": if _np_version_under1p16: raise pytest.skip("Skipping for NumPy <1.16") return PandasArray(np.array([np.nan, (1,)])) return PandasArray(np.array([np.nan, 1.0])) @pytest.fixture def na_value(): return np.nan @pytest.fixture def na_cmp(): def cmp(a, b): return np.isnan(a) and np.isnan(b) return cmp @pytest.fixture def data_for_sorting(allow_in_pandas, dtype): """Length-3 array with a known sort order. This should be three items [B, C, A] with A < B < C """ if dtype.numpy_dtype == "object": # Use an empty tuple for first element, then remove, # to disable np.array's shape inference. return PandasArray(np.array([(), (2,), (3,), (1,)])[1:]) return PandasArray(np.array([1, 2, 0])) @pytest.fixture def data_missing_for_sorting(allow_in_pandas, dtype): """Length-3 array with a known sort order. This should be three items [B, NA, A] with A < B and NA missing. """ if dtype.numpy_dtype == "object": return PandasArray(np.array([(1,), np.nan, (0,)])) return PandasArray(np.array([1, np.nan, 0])) @pytest.fixture def data_for_grouping(allow_in_pandas, dtype): """Data for factorization, grouping, and unique tests. Expected to be like [B, B, NA, NA, A, A, B, C] Where A < B < C and NA is missing """ if dtype.numpy_dtype == "object": a, b, c = (1,), (2,), (3,) else: a, b, c = np.arange(3) return PandasArray(np.array([b, b, np.nan, np.nan, a, a, b, c])) @pytest.fixture def skip_numpy_object(dtype): """ Tests for PandasArray with nested data. Users typically won't create these objects via `pd.array`, but they can show up through `.array` on a Series with nested data. Many of the base tests fail, as they aren't appropriate for nested data. This fixture allows these tests to be skipped when used as a usefixtures marker to either an individual test or a test class. """ if dtype == "object": raise pytest.skip("Skipping for object dtype.") skip_nested = pytest.mark.usefixtures("skip_numpy_object") class BaseNumPyTests: pass class TestCasting(BaseNumPyTests, base.BaseCastingTests): @skip_nested def test_astype_str(self, data): # ValueError: setting an array element with a sequence super().test_astype_str(data) class TestConstructors(BaseNumPyTests, base.BaseConstructorsTests): @pytest.mark.skip(reason="We don't register our dtype") # We don't want to register. This test should probably be split in two. def test_from_dtype(self, data): pass @skip_nested def test_array_from_scalars(self, data): # ValueError: PandasArray must be 1-dimensional. super().test_array_from_scalars(data) class TestDtype(BaseNumPyTests, base.BaseDtypeTests): @pytest.mark.skip(reason="Incorrect expected.") # we unsurprisingly clash with a NumPy name. def test_check_dtype(self, data): pass class TestGetitem(BaseNumPyTests, base.BaseGetitemTests): @skip_nested def test_getitem_scalar(self, data): # AssertionError super().test_getitem_scalar(data) @skip_nested def test_take_series(self, data): # ValueError: PandasArray must be 1-dimensional. super().test_take_series(data) @pytest.mark.xfail(reason="astype doesn't recognize data.dtype") def test_loc_iloc_frame_single_dtype(self, data): super().test_loc_iloc_frame_single_dtype(data) class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests): @skip_nested def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op): # ValueError: Names should be list-like for a MultiIndex super().test_groupby_extension_apply(data_for_grouping, groupby_apply_op) class TestInterface(BaseNumPyTests, base.BaseInterfaceTests): @skip_nested def test_array_interface(self, data): # NumPy array shape inference super().test_array_interface(data) class TestMethods(BaseNumPyTests, base.BaseMethodsTests): @pytest.mark.skip(reason="TODO: remove?") def test_value_counts(self, all_data, dropna): pass @pytest.mark.skip(reason="Incorrect expected") # We have a bool dtype, so the result is an ExtensionArray # but expected is not def test_combine_le(self, data_repeated): super().test_combine_le(data_repeated) @skip_nested def test_combine_add(self, data_repeated): # Not numeric super().test_combine_add(data_repeated) @skip_nested def test_shift_fill_value(self, data): # np.array shape inference. Shift implementation fails. super().test_shift_fill_value(data) @skip_nested @pytest.mark.parametrize("box", [pd.Series, lambda x: x]) @pytest.mark.parametrize("method", [lambda x: x.unique(), pd.unique]) def test_unique(self, data, box, method): # Fails creating expected super().test_unique(data, box, method) @skip_nested def test_fillna_copy_frame(self, data_missing): # The "scalar" for this array isn't a scalar. super().test_fillna_copy_frame(data_missing) @skip_nested def test_fillna_copy_series(self, data_missing): # The "scalar" for this array isn't a scalar. super().test_fillna_copy_series(data_missing) @skip_nested def test_hash_pandas_object_works(self, data, as_frame): # ndarray of tuples not hashable super().test_hash_pandas_object_works(data, as_frame) @skip_nested def test_searchsorted(self, data_for_sorting, as_series): # Test setup fails. super().test_searchsorted(data_for_sorting, as_series) @skip_nested def test_where_series(self, data, na_value, as_frame): # Test setup fails. super().test_where_series(data, na_value, as_frame) @skip_nested @pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]]) def test_repeat(self, data, repeats, as_series, use_numpy): # Fails creating expected super().test_repeat(data, repeats, as_series, use_numpy) @skip_nested class TestArithmetics(BaseNumPyTests, base.BaseArithmeticOpsTests): divmod_exc = None series_scalar_exc = None frame_scalar_exc = None series_array_exc = None def test_divmod_series_array(self, data): s = pd.Series(data) self._check_divmod_op(s, divmod, data, exc=None) @pytest.mark.skip("We implement ops") def test_error(self, data, all_arithmetic_operators): pass def test_arith_series_with_scalar(self, data, all_arithmetic_operators): super().test_arith_series_with_scalar(data, all_arithmetic_operators) def test_arith_series_with_array(self, data, all_arithmetic_operators): super().test_arith_series_with_array(data, all_arithmetic_operators) class TestPrinting(BaseNumPyTests, base.BasePrintingTests): pass @skip_nested class TestNumericReduce(BaseNumPyTests, base.BaseNumericReduceTests): def check_reduce(self, s, op_name, skipna): result = getattr(s, op_name)(skipna=skipna) # avoid coercing int -> float. Just cast to the actual numpy type. expected = getattr(s.astype(s.dtype._dtype), op_name)(skipna=skipna) tm.assert_almost_equal(result, expected) @skip_nested class TestBooleanReduce(BaseNumPyTests, base.BaseBooleanReduceTests): pass class TestMissing(BaseNumPyTests, base.BaseMissingTests): @skip_nested def test_fillna_scalar(self, data_missing): # Non-scalar "scalar" values. super().test_fillna_scalar(data_missing) @skip_nested def test_fillna_series_method(self, data_missing, fillna_method): # Non-scalar "scalar" values. super().test_fillna_series_method(data_missing, fillna_method) @skip_nested def test_fillna_series(self, data_missing): # Non-scalar "scalar" values. super().test_fillna_series(data_missing) @skip_nested def test_fillna_frame(self, data_missing): # Non-scalar "scalar" values. super().test_fillna_frame(data_missing) class TestReshaping(BaseNumPyTests, base.BaseReshapingTests): @pytest.mark.skip("Incorrect parent test") # not actually a mixed concat, since we concat int and int. def test_concat_mixed_dtypes(self, data): super().test_concat_mixed_dtypes(data) @skip_nested def test_merge(self, data, na_value): # Fails creating expected super().test_merge(data, na_value) @skip_nested def test_merge_on_extension_array(self, data): # Fails creating expected super().test_merge_on_extension_array(data) @skip_nested def test_merge_on_extension_array_duplicates(self, data): # Fails creating expected super().test_merge_on_extension_array_duplicates(data) class TestSetitem(BaseNumPyTests, base.BaseSetitemTests): @skip_nested def test_setitem_scalar_series(self, data, box_in_series): # AssertionError super().test_setitem_scalar_series(data, box_in_series) @skip_nested def test_setitem_sequence(self, data, box_in_series): # ValueError: shape mismatch: value array of shape (2,1) could not # be broadcast to indexing result of shape (2,) super().test_setitem_sequence(data, box_in_series) @skip_nested def test_setitem_sequence_mismatched_length_raises(self, data, as_array): # ValueError: PandasArray must be 1-dimensional. super().test_setitem_sequence_mismatched_length_raises(data, as_array) @skip_nested def test_setitem_sequence_broadcasts(self, data, box_in_series): # ValueError: cannot set using a list-like indexer with a different # length than the value super().test_setitem_sequence_broadcasts(data, box_in_series) @skip_nested def test_setitem_loc_scalar_mixed(self, data): # AssertionError super().test_setitem_loc_scalar_mixed(data) @skip_nested def test_setitem_loc_scalar_multiple_homogoneous(self, data): # AssertionError super().test_setitem_loc_scalar_multiple_homogoneous(data) @skip_nested def test_setitem_iloc_scalar_mixed(self, data): # AssertionError super().test_setitem_iloc_scalar_mixed(data) @skip_nested def test_setitem_iloc_scalar_multiple_homogoneous(self, data): # AssertionError super().test_setitem_iloc_scalar_multiple_homogoneous(data) @skip_nested @pytest.mark.parametrize("setter", ["loc", None]) def test_setitem_mask_broadcast(self, data, setter): # ValueError: cannot set using a list-like indexer with a different # length than the value super().test_setitem_mask_broadcast(data, setter) @skip_nested def test_setitem_scalar_key_sequence_raise(self, data): # Failed: DID NOT RAISE <class 'ValueError'> super().test_setitem_scalar_key_sequence_raise(data) @skip_nested class TestParsing(BaseNumPyTests, base.BaseParsingTests): pass
apache-2.0
pvcrossi/OnlineCS
online_CS.py
1
4043
''' Bayesian Online Compressed Sensing (2016) Paulo V. Rossi & Yoshiyuki Kabashima ''' from collections import namedtuple import matplotlib.pyplot as plt import numpy as np from numpy.linalg import norm from numpy.random import normal from utils import DlnH, DDlnH, G, H, moments def simulation(method='standard'): signal_length = 2000 alpha_max = 20 sigma_n_2 = 1e-1 phi = prior() P = posterior(signal_length, phi) x0 = generate_signal(signal_length, phi) print('Simulation parameters:') print('N='+str(signal_length)+', sparsity='+str(phi.rho)+ ', noise='+str(sigma_n_2)+', alpha_max='+str(alpha_max)) print('Measurement model: '+method+'\n') number_of_measurements = alpha_max*signal_length mean_square_error = np.zeros(number_of_measurements) for measurement in range(number_of_measurements): P = update_posterior(P, phi, x0, signal_length, sigma_n_2, method) mean_square_error[measurement] = reconstruction_error(P, x0) plot_results(P, x0, mean_square_error, phi) def prior(): phi = namedtuple('prior_distribution', ['rho', 'sigma_x_2', 'bar_x']) phi.rho = 0.1 phi.sigma_x_2 = 1. phi.bar_x = 0. return phi def posterior(signal_length, phi): P = namedtuple('posterior_distribution', ['m', 'v', 'a', 'h']) P.m = np.zeros(signal_length) P.v = phi.rho * phi.sigma_x_2 * np.ones(signal_length) P.a = np.zeros(signal_length) P.h = np.zeros(signal_length) return P def generate_signal (signal_length, phi): x0 = np.zeros(signal_length) number_of_non_zero_components = int(np.ceil(signal_length*phi.rho)) x0[:number_of_non_zero_components] = normal(loc=phi.bar_x, scale=np.sqrt(phi.sigma_x_2), size=number_of_non_zero_components) return x0 def update_posterior(P, phi, x0, signal_length, sigma_n_2, method): A_t = measurement_vector(signal_length) P.a, P.h = update_and_project(method, A_t, x0, sigma_n_2, P) P.m, P.v = moments(P, phi) return P def measurement_vector(signal_length): A_t = normal(size=signal_length) return A_t/norm(A_t) def update_and_project(method, A_t, x0, sigma_n_2, P): m, v, a, h = P.m, P.v, P.a, P.h u0 = np.dot(A_t, x0) if sigma_n_2 > 0: noise = normal(scale=np.sqrt(sigma_n_2)) else: noise = 0 y = u0 + noise Delta = np.dot(A_t, m) chi = np.dot(A_t**2, v) if method == 'standard': da, dh = update_and_project_std(y, Delta, chi, sigma_n_2, A_t, m) elif method == '1bit': da, dh = update_and_project_1bit(y, Delta, chi, sigma_n_2, A_t, m) else: raise ValueError('Measurement model not recognized. Please use "standard" or "1bit".') return a+da, h+dh def update_and_project_std(y, Delta, chi, sigma_n_2, A_t, m): da = A_t**2 / (sigma_n_2 + chi) dh = (y-Delta)*A_t / (sigma_n_2 + chi) + da*m return da, dh def update_and_project_1bit(y, Delta, chi, sigma_n_2, A_t, m): y = np.sign(y) u = y * np.dot(A_t, m) chi_prime = chi + sigma_n_2 z = -u/np.sqrt(chi_prime) da = -A_t**2/chi_prime * DDlnH(z) dh = -y*A_t/np.sqrt(chi_prime) * DlnH(z) + da*m return da, dh def reconstruction_error(P, x0): return norm(x0 - P.m)**2 / norm(x0)**2 def plot_results(P, x0, mse_t, phi): plt.subplots(figsize=(10,20)) plt.subplot(211) plt.plot(np.arange(len(mse_t))/float(len(P.m)), 10*np.log10(mse_t), color='k') plt.xlabel(r'$\alpha$') plt.ylabel(r'mse (dB)') plt.subplot(212) plt.plot(P.m, color='k', lw = 0.7, label=r'$m$') plt.scatter(range(int(len(x0)*phi.rho)), x0[:int(len(x0)*phi.rho)], \ marker='o', facecolors='none', edgecolors='r', lw=1.5, label=r'$x^0$') plt.xlim([0,len(P.m)]) plt.xlabel(r'Vector Component') plt.legend() plt.show() if __name__ == '__main__': simulation(method='1bit') #simulation(method='standard')
mit
nvoron23/scikit-learn
sklearn/linear_model/tests/test_theil_sen.py
234
9928
""" Testing for Theil-Sen module (sklearn.linear_model.theil_sen) """ # Author: Florian Wilhelm <florian.wilhelm@gmail.com> # License: BSD 3 clause from __future__ import division, print_function, absolute_import import os import sys from contextlib import contextmanager import numpy as np from numpy.testing import assert_array_equal, assert_array_less from numpy.testing import assert_array_almost_equal, assert_warns from scipy.linalg import norm from scipy.optimize import fmin_bfgs from nose.tools import raises, assert_almost_equal from sklearn.utils import ConvergenceWarning from sklearn.linear_model import LinearRegression, TheilSenRegressor from sklearn.linear_model.theil_sen import _spatial_median, _breakdown_point from sklearn.linear_model.theil_sen import _modified_weiszfeld_step from sklearn.utils.testing import assert_greater, assert_less @contextmanager def no_stdout_stderr(): old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = open(os.devnull, 'w') sys.stderr = open(os.devnull, 'w') yield sys.stdout.flush() sys.stderr.flush() sys.stdout = old_stdout sys.stderr = old_stderr def gen_toy_problem_1d(intercept=True): random_state = np.random.RandomState(0) # Linear model y = 3*x + N(2, 0.1**2) w = 3. if intercept: c = 2. n_samples = 50 else: c = 0.1 n_samples = 100 x = random_state.normal(size=n_samples) noise = 0.1 * random_state.normal(size=n_samples) y = w * x + c + noise # Add some outliers if intercept: x[42], y[42] = (-2, 4) x[43], y[43] = (-2.5, 8) x[33], y[33] = (2.5, 1) x[49], y[49] = (2.1, 2) else: x[42], y[42] = (-2, 4) x[43], y[43] = (-2.5, 8) x[53], y[53] = (2.5, 1) x[60], y[60] = (2.1, 2) x[72], y[72] = (1.8, -7) return x[:, np.newaxis], y, w, c def gen_toy_problem_2d(): random_state = np.random.RandomState(0) n_samples = 100 # Linear model y = 5*x_1 + 10*x_2 + N(1, 0.1**2) X = random_state.normal(size=(n_samples, 2)) w = np.array([5., 10.]) c = 1. noise = 0.1 * random_state.normal(size=n_samples) y = np.dot(X, w) + c + noise # Add some outliers n_outliers = n_samples // 10 ix = random_state.randint(0, n_samples, size=n_outliers) y[ix] = 50 * random_state.normal(size=n_outliers) return X, y, w, c def gen_toy_problem_4d(): random_state = np.random.RandomState(0) n_samples = 10000 # Linear model y = 5*x_1 + 10*x_2 + 42*x_3 + 7*x_4 + N(1, 0.1**2) X = random_state.normal(size=(n_samples, 4)) w = np.array([5., 10., 42., 7.]) c = 1. noise = 0.1 * random_state.normal(size=n_samples) y = np.dot(X, w) + c + noise # Add some outliers n_outliers = n_samples // 10 ix = random_state.randint(0, n_samples, size=n_outliers) y[ix] = 50 * random_state.normal(size=n_outliers) return X, y, w, c def test_modweiszfeld_step_1d(): X = np.array([1., 2., 3.]).reshape(3, 1) # Check startvalue is element of X and solution median = 2. new_y = _modified_weiszfeld_step(X, median) assert_array_almost_equal(new_y, median) # Check startvalue is not the solution y = 2.5 new_y = _modified_weiszfeld_step(X, y) assert_array_less(median, new_y) assert_array_less(new_y, y) # Check startvalue is not the solution but element of X y = 3. new_y = _modified_weiszfeld_step(X, y) assert_array_less(median, new_y) assert_array_less(new_y, y) # Check that a single vector is identity X = np.array([1., 2., 3.]).reshape(1, 3) y = X[0, ] new_y = _modified_weiszfeld_step(X, y) assert_array_equal(y, new_y) def test_modweiszfeld_step_2d(): X = np.array([0., 0., 1., 1., 0., 1.]).reshape(3, 2) y = np.array([0.5, 0.5]) # Check first two iterations new_y = _modified_weiszfeld_step(X, y) assert_array_almost_equal(new_y, np.array([1 / 3, 2 / 3])) new_y = _modified_weiszfeld_step(X, new_y) assert_array_almost_equal(new_y, np.array([0.2792408, 0.7207592])) # Check fix point y = np.array([0.21132505, 0.78867497]) new_y = _modified_weiszfeld_step(X, y) assert_array_almost_equal(new_y, y) def test_spatial_median_1d(): X = np.array([1., 2., 3.]).reshape(3, 1) true_median = 2. _, median = _spatial_median(X) assert_array_almost_equal(median, true_median) # Test larger problem and for exact solution in 1d case random_state = np.random.RandomState(0) X = random_state.randint(100, size=(1000, 1)) true_median = np.median(X.ravel()) _, median = _spatial_median(X) assert_array_equal(median, true_median) def test_spatial_median_2d(): X = np.array([0., 0., 1., 1., 0., 1.]).reshape(3, 2) _, median = _spatial_median(X, max_iter=100, tol=1.e-6) def cost_func(y): dists = np.array([norm(x - y) for x in X]) return np.sum(dists) # Check if median is solution of the Fermat-Weber location problem fermat_weber = fmin_bfgs(cost_func, median, disp=False) assert_array_almost_equal(median, fermat_weber) # Check when maximum iteration is exceeded a warning is emitted assert_warns(ConvergenceWarning, _spatial_median, X, max_iter=30, tol=0.) def test_theil_sen_1d(): X, y, w, c = gen_toy_problem_1d() # Check that Least Squares fails lstq = LinearRegression().fit(X, y) assert_greater(np.abs(lstq.coef_ - w), 0.9) # Check that Theil-Sen works theil_sen = TheilSenRegressor(random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_theil_sen_1d_no_intercept(): X, y, w, c = gen_toy_problem_1d(intercept=False) # Check that Least Squares fails lstq = LinearRegression(fit_intercept=False).fit(X, y) assert_greater(np.abs(lstq.coef_ - w - c), 0.5) # Check that Theil-Sen works theil_sen = TheilSenRegressor(fit_intercept=False, random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w + c, 1) assert_almost_equal(theil_sen.intercept_, 0.) def test_theil_sen_2d(): X, y, w, c = gen_toy_problem_2d() # Check that Least Squares fails lstq = LinearRegression().fit(X, y) assert_greater(norm(lstq.coef_ - w), 1.0) # Check that Theil-Sen works theil_sen = TheilSenRegressor(max_subpopulation=1e3, random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_calc_breakdown_point(): bp = _breakdown_point(1e10, 2) assert_less(np.abs(bp - 1 + 1/(np.sqrt(2))), 1.e-6) @raises(ValueError) def test_checksubparams_negative_subpopulation(): X, y, w, c = gen_toy_problem_1d() TheilSenRegressor(max_subpopulation=-1, random_state=0).fit(X, y) @raises(ValueError) def test_checksubparams_too_few_subsamples(): X, y, w, c = gen_toy_problem_1d() TheilSenRegressor(n_subsamples=1, random_state=0).fit(X, y) @raises(ValueError) def test_checksubparams_too_many_subsamples(): X, y, w, c = gen_toy_problem_1d() TheilSenRegressor(n_subsamples=101, random_state=0).fit(X, y) @raises(ValueError) def test_checksubparams_n_subsamples_if_less_samples_than_features(): random_state = np.random.RandomState(0) n_samples, n_features = 10, 20 X = random_state.normal(size=(n_samples, n_features)) y = random_state.normal(size=n_samples) TheilSenRegressor(n_subsamples=9, random_state=0).fit(X, y) def test_subpopulation(): X, y, w, c = gen_toy_problem_4d() theil_sen = TheilSenRegressor(max_subpopulation=250, random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_subsamples(): X, y, w, c = gen_toy_problem_4d() theil_sen = TheilSenRegressor(n_subsamples=X.shape[0], random_state=0).fit(X, y) lstq = LinearRegression().fit(X, y) # Check for exact the same results as Least Squares assert_array_almost_equal(theil_sen.coef_, lstq.coef_, 9) def test_verbosity(): X, y, w, c = gen_toy_problem_1d() # Check that Theil-Sen can be verbose with no_stdout_stderr(): TheilSenRegressor(verbose=True, random_state=0).fit(X, y) TheilSenRegressor(verbose=True, max_subpopulation=10, random_state=0).fit(X, y) def test_theil_sen_parallel(): X, y, w, c = gen_toy_problem_2d() # Check that Least Squares fails lstq = LinearRegression().fit(X, y) assert_greater(norm(lstq.coef_ - w), 1.0) # Check that Theil-Sen works theil_sen = TheilSenRegressor(n_jobs=-1, random_state=0, max_subpopulation=2e3).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_less_samples_than_features(): random_state = np.random.RandomState(0) n_samples, n_features = 10, 20 X = random_state.normal(size=(n_samples, n_features)) y = random_state.normal(size=n_samples) # Check that Theil-Sen falls back to Least Squares if fit_intercept=False theil_sen = TheilSenRegressor(fit_intercept=False, random_state=0).fit(X, y) lstq = LinearRegression(fit_intercept=False).fit(X, y) assert_array_almost_equal(theil_sen.coef_, lstq.coef_, 12) # Check fit_intercept=True case. This will not be equal to the Least # Squares solution since the intercept is calculated differently. theil_sen = TheilSenRegressor(fit_intercept=True, random_state=0).fit(X, y) y_pred = theil_sen.predict(X) assert_array_almost_equal(y_pred, y, 12)
bsd-3-clause
gdementen/PyTables
c-blosc/bench/plot-speeds.py
11
6852
"""Script for plotting the results of the 'suite' benchmark. Invoke without parameters for usage hints. :Author: Francesc Alted :Date: 2010-06-01 """ import matplotlib as mpl from pylab import * KB_ = 1024 MB_ = 1024*KB_ GB_ = 1024*MB_ NCHUNKS = 128 # keep in sync with bench.c linewidth=2 #markers= ['+', ',', 'o', '.', 's', 'v', 'x', '>', '<', '^'] #markers= [ 'x', '+', 'o', 's', 'v', '^', '>', '<', ] markers= [ 's', 'o', 'v', '^', '+', 'x', '>', '<', '.', ',' ] markersize = 8 def get_values(filename): f = open(filename) values = {"memcpyw": [], "memcpyr": []} for line in f: if line.startswith('-->'): tmp = line.split('-->')[1] nthreads, size, elsize, sbits, codec = [i for i in tmp.split(', ')] nthreads, size, elsize, sbits = map(int, (nthreads, size, elsize, sbits)) values["size"] = size * NCHUNKS / MB_; values["elsize"] = elsize; values["sbits"] = sbits; values["codec"] = codec # New run for nthreads (ratios, speedsw, speedsr) = ([], [], []) # Add a new entry for (ratios, speedw, speedr) values[nthreads] = (ratios, speedsw, speedsr) #print "-->", nthreads, size, elsize, sbits elif line.startswith('memcpy(write):'): tmp = line.split(',')[1] memcpyw = float(tmp.split(' ')[1]) values["memcpyw"].append(memcpyw) elif line.startswith('memcpy(read):'): tmp = line.split(',')[1] memcpyr = float(tmp.split(' ')[1]) values["memcpyr"].append(memcpyr) elif line.startswith('comp(write):'): tmp = line.split(',')[1] speedw = float(tmp.split(' ')[1]) ratio = float(line.split(':')[-1]) speedsw.append(speedw) ratios.append(ratio) elif line.startswith('decomp(read):'): tmp = line.split(',')[1] speedr = float(tmp.split(' ')[1]) speedsr.append(speedr) if "OK" not in line: print "WARNING! OK not found in decomp line!" f.close() return nthreads, values def show_plot(plots, yaxis, legends, gtitle, xmax=None): xlabel('Compresssion ratio') ylabel('Speed (MB/s)') title(gtitle) xlim(0, xmax) #ylim(0, 10000) ylim(0, None) grid(True) # legends = [f[f.find('-'):f.index('.out')] for f in filenames] # legends = [l.replace('-', ' ') for l in legends] #legend([p[0] for p in plots], legends, loc = "upper left") legend([p[0] for p in plots if not isinstance(p, mpl.lines.Line2D)], legends, loc = "best") #subplots_adjust(bottom=0.2, top=None, wspace=0.2, hspace=0.2) if outfile: print "Saving plot to:", outfile savefig(outfile, dpi=64) else: show() if __name__ == '__main__': from optparse import OptionParser usage = "usage: %prog [-r] [-o outfile] [-t title ] [-d|-c] filename" compress_title = 'Compression speed' decompress_title = 'Decompression speed' yaxis = 'No axis name' parser = OptionParser(usage=usage) parser.add_option('-o', '--outfile', dest='outfile', help=('filename for output (many extensions ' 'supported, e.g. .png, .jpg, .pdf)')) parser.add_option('-t', '--title', dest='title', help='title of the plot',) parser.add_option('-l', '--limit', dest='limit', help='expression to limit number of threads shown',) parser.add_option('-x', '--xmax', dest='xmax', help='limit the x-axis', default=None) parser.add_option('-r', '--report', action='store_true', dest='report', help='generate file for reporting ', default=False) parser.add_option('-d', '--decompress', action='store_true', dest='dspeed', help='plot decompression data', default=False) parser.add_option('-c', '--compress', action='store_true', dest='cspeed', help='plot compression data', default=False) (options, args) = parser.parse_args() if len(args) == 0: parser.error("No input arguments") elif len(args) > 1: parser.error("Too many input arguments") else: pass if options.report and options.outfile: parser.error("Can only select one of [-r, -o]") if options.dspeed and options.cspeed: parser.error("Can only select one of [-d, -c]") elif options.cspeed: options.dspeed = False plot_title = compress_title else: # either neither or dspeed options.dspeed = True plot_title = decompress_title filename = args[0] cspeed = options.cspeed dspeed = options.dspeed if options.outfile: outfile = options.outfile elif options.report: if cspeed: outfile = filename[:filename.rindex('.')] + '-compr.png' else: outfile = filename[:filename.rindex('.')] + '-decompr.png' else: outfile = None plots = [] legends = [] nthreads, values = get_values(filename) #print "Values:", values if options.limit: thread_range = eval(options.limit) else: thread_range = range(1, nthreads+1) if options.title: plot_title = options.title else: plot_title += " (%(size).1f MB, %(elsize)d bytes, %(sbits)d bits), %(codec)s" % values gtitle = plot_title for nt in thread_range: #print "Values for %s threads --> %s" % (nt, values[nt]) (ratios, speedw, speedr) = values[nt] if cspeed: speed = speedw else: speed = speedr #plot_ = semilogx(ratios, speed, linewidth=2) plot_ = plot(ratios, speed, linewidth=2) plots.append(plot_) nmarker = nt if nt >= len(markers): nmarker = nt%len(markers) setp(plot_, marker=markers[nmarker], markersize=markersize, linewidth=linewidth) legends.append("%d threads" % nt) # Add memcpy lines if cspeed: mean = np.mean(values["memcpyw"]) message = "memcpy (write to memory)" else: mean = np.mean(values["memcpyr"]) message = "memcpy (read from memory)" plot_ = axhline(mean, linewidth=3, linestyle='-.', color='black') text(1.0, mean+50, message) plots.append(plot_) show_plot(plots, yaxis, legends, gtitle, xmax=int(options.xmax) if options.xmax else None)
bsd-3-clause
chrisjdavie/shares
machine_learning/sklearn_dataset_format.py
1
1160
''' Created on 2 Sep 2014 @author: chris ''' '''File format - data, length of data, containing unicode - target, length of data, contains int reference to target - target_names, type names relative to target - filenames, names of files storing data (probably target too) ''' def main(): ''' taken from the tutorials, I'm having a look at how they store datasets''' from sklearn.datasets import fetch_20newsgroups # import numpy as np categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics', 'sci.med'] twenty_train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42) print dir(twenty_train) print twenty_train.keys() # print twenty_train.data[0] print twenty_train.target[0] print len(twenty_train.filenames) print twenty_train.filenames[0] print twenty_train.target_names if __name__ == '__main__': main()
mit
jayflo/scikit-learn
examples/cluster/plot_birch_vs_minibatchkmeans.py
333
3694
""" ================================= Compare BIRCH and MiniBatchKMeans ================================= This example compares the timing of Birch (with and without the global clustering step) and MiniBatchKMeans on a synthetic dataset having 100,000 samples and 2 features generated using make_blobs. If ``n_clusters`` is set to None, the data is reduced from 100,000 samples to a set of 158 clusters. This can be viewed as a preprocessing step before the final (global) clustering step that further reduces these 158 clusters to 100 clusters. """ # Authors: Manoj Kumar <manojkumarsivaraj334@gmail.com # Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr> # License: BSD 3 clause print(__doc__) from itertools import cycle from time import time import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors from sklearn.preprocessing import StandardScaler from sklearn.cluster import Birch, MiniBatchKMeans from sklearn.datasets.samples_generator import make_blobs # Generate centers for the blobs so that it forms a 10 X 10 grid. xx = np.linspace(-22, 22, 10) yy = np.linspace(-22, 22, 10) xx, yy = np.meshgrid(xx, yy) n_centres = np.hstack((np.ravel(xx)[:, np.newaxis], np.ravel(yy)[:, np.newaxis])) # Generate blobs to do a comparison between MiniBatchKMeans and Birch. X, y = make_blobs(n_samples=100000, centers=n_centres, random_state=0) # Use all colors that matplotlib provides by default. colors_ = cycle(colors.cnames.keys()) fig = plt.figure(figsize=(12, 4)) fig.subplots_adjust(left=0.04, right=0.98, bottom=0.1, top=0.9) # Compute clustering with Birch with and without the final clustering step # and plot. birch_models = [Birch(threshold=1.7, n_clusters=None), Birch(threshold=1.7, n_clusters=100)] final_step = ['without global clustering', 'with global clustering'] for ind, (birch_model, info) in enumerate(zip(birch_models, final_step)): t = time() birch_model.fit(X) time_ = time() - t print("Birch %s as the final step took %0.2f seconds" % ( info, (time() - t))) # Plot result labels = birch_model.labels_ centroids = birch_model.subcluster_centers_ n_clusters = np.unique(labels).size print("n_clusters : %d" % n_clusters) ax = fig.add_subplot(1, 3, ind + 1) for this_centroid, k, col in zip(centroids, range(n_clusters), colors_): mask = labels == k ax.plot(X[mask, 0], X[mask, 1], 'w', markerfacecolor=col, marker='.') if birch_model.n_clusters is None: ax.plot(this_centroid[0], this_centroid[1], '+', markerfacecolor=col, markeredgecolor='k', markersize=5) ax.set_ylim([-25, 25]) ax.set_xlim([-25, 25]) ax.set_autoscaley_on(False) ax.set_title('Birch %s' % info) # Compute clustering with MiniBatchKMeans. mbk = MiniBatchKMeans(init='k-means++', n_clusters=100, batch_size=100, n_init=10, max_no_improvement=10, verbose=0, random_state=0) t0 = time() mbk.fit(X) t_mini_batch = time() - t0 print("Time taken to run MiniBatchKMeans %0.2f seconds" % t_mini_batch) mbk_means_labels_unique = np.unique(mbk.labels_) ax = fig.add_subplot(1, 3, 3) for this_centroid, k, col in zip(mbk.cluster_centers_, range(n_clusters), colors_): mask = mbk.labels_ == k ax.plot(X[mask, 0], X[mask, 1], 'w', markerfacecolor=col, marker='.') ax.plot(this_centroid[0], this_centroid[1], '+', markeredgecolor='k', markersize=5) ax.set_xlim([-25, 25]) ax.set_ylim([-25, 25]) ax.set_title("MiniBatchKMeans") ax.set_autoscaley_on(False) plt.show()
bsd-3-clause
jm-begon/scikit-learn
examples/linear_model/plot_bayesian_ridge.py
248
2588
""" ========================= Bayesian Ridge Regression ========================= Computes a Bayesian Ridge Regression on a synthetic dataset. See :ref:`bayesian_ridge_regression` for more information on the regressor. Compared to the OLS (ordinary least squares) estimator, the coefficient weights are slightly shifted toward zeros, which stabilises them. As the prior on the weights is a Gaussian prior, the histogram of the estimated weights is Gaussian. The estimation of the model is done by iteratively maximizing the marginal log-likelihood of the observations. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn.linear_model import BayesianRidge, LinearRegression ############################################################################### # Generating simulated data with Gaussian weigthts np.random.seed(0) n_samples, n_features = 100, 100 X = np.random.randn(n_samples, n_features) # Create Gaussian data # Create weigts with a precision lambda_ of 4. lambda_ = 4. w = np.zeros(n_features) # Only keep 10 weights of interest relevant_features = np.random.randint(0, n_features, 10) for i in relevant_features: w[i] = stats.norm.rvs(loc=0, scale=1. / np.sqrt(lambda_)) # Create noise with a precision alpha of 50. alpha_ = 50. noise = stats.norm.rvs(loc=0, scale=1. / np.sqrt(alpha_), size=n_samples) # Create the target y = np.dot(X, w) + noise ############################################################################### # Fit the Bayesian Ridge Regression and an OLS for comparison clf = BayesianRidge(compute_score=True) clf.fit(X, y) ols = LinearRegression() ols.fit(X, y) ############################################################################### # Plot true weights, estimated weights and histogram of the weights plt.figure(figsize=(6, 5)) plt.title("Weights of the model") plt.plot(clf.coef_, 'b-', label="Bayesian Ridge estimate") plt.plot(w, 'g-', label="Ground truth") plt.plot(ols.coef_, 'r--', label="OLS estimate") plt.xlabel("Features") plt.ylabel("Values of the weights") plt.legend(loc="best", prop=dict(size=12)) plt.figure(figsize=(6, 5)) plt.title("Histogram of the weights") plt.hist(clf.coef_, bins=n_features, log=True) plt.plot(clf.coef_[relevant_features], 5 * np.ones(len(relevant_features)), 'ro', label="Relevant features") plt.ylabel("Features") plt.xlabel("Values of the weights") plt.legend(loc="lower left") plt.figure(figsize=(6, 5)) plt.title("Marginal log-likelihood") plt.plot(clf.scores_) plt.ylabel("Score") plt.xlabel("Iterations") plt.show()
bsd-3-clause
B3AU/waveTree
sklearn/utils/testing.py
4
12125
"""Testing utilities.""" # Copyright (c) 2011, 2012 # Authors: Pietro Berkes, # Andreas Muller # Mathieu Blondel # Olivier Grisel # Arnaud Joly # License: BSD 3 clause import inspect import pkgutil import warnings import scipy as sp from functools import wraps try: # Python 2 from urllib2 import urlopen from urllib2 import HTTPError except ImportError: # Python 3+ from urllib.request import urlopen from urllib.error import HTTPError import sklearn from sklearn.base import BaseEstimator from .fixes import savemat # Conveniently import all assertions in one place. from nose.tools import assert_equal from nose.tools import assert_not_equal from nose.tools import assert_true from nose.tools import assert_false from nose.tools import assert_raises from nose.tools import raises from nose import SkipTest from nose import with_setup from numpy.testing import assert_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_less import numpy as np from sklearn.base import (ClassifierMixin, RegressorMixin, TransformerMixin, ClusterMixin) __all__ = ["assert_equal", "assert_not_equal", "assert_raises", "raises", "with_setup", "assert_true", "assert_false", "assert_almost_equal", "assert_array_equal", "assert_array_almost_equal", "assert_array_less"] try: from nose.tools import assert_in, assert_not_in except ImportError: # Nose < 1.0.0 def assert_in(x, container): assert_true(x in container, msg="%r in %r" % (x, container)) def assert_not_in(x, container): assert_false(x in container, msg="%r in %r" % (x, container)) def _assert_less(a, b, msg=None): message = "%r is not lower than %r" % (a, b) if msg is not None: message += ": " + msg assert a < b, message def _assert_greater(a, b, msg=None): message = "%r is not greater than %r" % (a, b) if msg is not None: message += ": " + msg assert a > b, message # To remove when we support numpy 1.7 def assert_warns(warning_class, func, *args, **kw): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. result = func(*args, **kw) # Verify some things if not len(w) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) if not w[0].category is warning_class: raise AssertionError("First warning for %s is not a " "%s( is %s)" % (func.__name__, warning_class, w[0])) return result # To remove when we support numpy 1.7 def assert_no_warnings(func, *args, **kw): # XXX: once we may depend on python >= 2.6, this can be replaced by the # warnings module context manager. with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') result = func(*args, **kw) if len(w) > 0: raise AssertionError("Got warnings when calling %s: %s" % (func.__name__, w)) return result def ignore_warnings(fn): """Decorator to catch and hide warnings without visual nesting""" @wraps(fn) def wrapper(*args, **kwargs): with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') return fn(*args, **kwargs) w[:] = [] return wrapper try: from nose.tools import assert_less except ImportError: assert_less = _assert_less try: from nose.tools import assert_greater except ImportError: assert_greater = _assert_greater def _assert_allclose(actual, desired, rtol=1e-7, atol=0, err_msg='', verbose=True): actual, desired = np.asanyarray(actual), np.asanyarray(desired) if np.allclose(actual, desired, rtol=rtol, atol=atol): return msg = ('Array not equal to tolerance rtol=%g, atol=%g: ' 'actual %s, desired %s') % (rtol, atol, actual, desired) raise AssertionError(msg) if hasattr(np.testing, 'assert_allclose'): assert_allclose = np.testing.assert_allclose else: assert_allclose = _assert_allclose def assert_raise_message(exception, message, function, *args, **kwargs): """Helper function to test error messages in exceptions""" try: function(*args, **kwargs) raise AssertionError("Should have raised %r" % exception(message)) except exception as e: error_message = str(e) assert_in(message, error_message) def fake_mldata(columns_dict, dataname, matfile, ordering=None): """Create a fake mldata data set. Parameters ---------- columns_dict: contains data as columns_dict[column_name] = array of data dataname: name of data set matfile: file-like object or file name ordering: list of column_names, determines the ordering in the data set Note: this function transposes all arrays, while fetch_mldata only transposes 'data', keep that into account in the tests. """ datasets = dict(columns_dict) # transpose all variables for name in datasets: datasets[name] = datasets[name].T if ordering is None: ordering = sorted(list(datasets.keys())) # NOTE: setting up this array is tricky, because of the way Matlab # re-packages 1D arrays datasets['mldata_descr_ordering'] = sp.empty((1, len(ordering)), dtype='object') for i, name in enumerate(ordering): datasets['mldata_descr_ordering'][0, i] = name savemat(matfile, datasets, oned_as='column') class mock_mldata_urlopen(object): def __init__(self, mock_datasets): """Object that mocks the urlopen function to fake requests to mldata. `mock_datasets` is a dictionary of {dataset_name: data_dict}, or {dataset_name: (data_dict, ordering). `data_dict` itself is a dictionary of {column_name: data_array}, and `ordering` is a list of column_names to determine the ordering in the data set (see `fake_mldata` for details). When requesting a dataset with a name that is in mock_datasets, this object creates a fake dataset in a StringIO object and returns it. Otherwise, it raises an HTTPError. """ self.mock_datasets = mock_datasets def __call__(self, urlname): dataset_name = urlname.split('/')[-1] if dataset_name in self.mock_datasets: resource_name = '_' + dataset_name from io import BytesIO matfile = BytesIO() dataset = self.mock_datasets[dataset_name] ordering = None if isinstance(dataset, tuple): dataset, ordering = dataset fake_mldata(dataset, resource_name, matfile, ordering) matfile.seek(0) return matfile else: raise HTTPError(urlname, 404, dataset_name + " is not available", [], None) def install_mldata_mock(mock_datasets): # Lazy import to avoid mutually recursive imports from sklearn import datasets datasets.mldata.urlopen = mock_mldata_urlopen(mock_datasets) def uninstall_mldata_mock(): # Lazy import to avoid mutually recursive imports from sklearn import datasets datasets.mldata.urlopen = urlopen # Meta estimators need another estimator to be instantiated. meta_estimators = ["OneVsOneClassifier", "OutputCodeClassifier", "OneVsRestClassifier", "RFE", "RFECV", "BaseEnsemble"] # estimators that there is no way to default-construct sensibly other = ["Pipeline", "FeatureUnion", "GridSearchCV", "RandomizedSearchCV"] def all_estimators(include_meta_estimators=False, include_other=False, type_filter=None): """Get a list of all estimators from sklearn. This function crawls the module and gets all classes that inherit from BaseEstimator. Classes that are defined in test-modules are not included. By default meta_estimators such as GridSearchCV are also not included. Parameters ---------- include_meta_estimators : boolean, default=False Whether to include meta-estimators that can be constructed using an estimator as their first argument. These are currently BaseEnsemble, OneVsOneClassifier, OutputCodeClassifier, OneVsRestClassifier, RFE, RFECV. include_others : boolean, default=False Wether to include meta-estimators that are somehow special and can not be default-constructed sensibly. These are currently Pipeline, FeatureUnion and GridSearchCV type_filter : string or None, default=None Which kind of estimators should be returned. If None, no filter is applied and all estimators are returned. Possible values are 'classifier', 'regressor', 'cluster' and 'transformer' to get estimators only of these specific types. Returns ------- estimators : list of tuples List of (name, class), where ``name`` is the class name as string and ``class`` is the actuall type of the class. """ def is_abstract(c): if not(hasattr(c, '__abstractmethods__')): return False if not len(c.__abstractmethods__): return False return True all_classes = [] # get parent folder path = sklearn.__path__ for importer, modname, ispkg in pkgutil.walk_packages( path=path, prefix='sklearn.', onerror=lambda x: None): module = __import__(modname, fromlist="dummy") if ".tests." in modname: continue classes = inspect.getmembers(module, inspect.isclass) all_classes.extend(classes) all_classes = set(all_classes) estimators = [c for c in all_classes if (issubclass(c[1], BaseEstimator) and c[0] != 'BaseEstimator')] # get rid of abstract base classes estimators = [c for c in estimators if not is_abstract(c[1])] if not include_other: estimators = [c for c in estimators if not c[0] in other] # possibly get rid of meta estimators if not include_meta_estimators: estimators = [c for c in estimators if not c[0] in meta_estimators] if type_filter == 'classifier': estimators = [est for est in estimators if issubclass(est[1], ClassifierMixin)] elif type_filter == 'regressor': estimators = [est for est in estimators if issubclass(est[1], RegressorMixin)] elif type_filter == 'transformer': estimators = [est for est in estimators if issubclass(est[1], TransformerMixin)] elif type_filter == 'cluster': estimators = [est for est in estimators if issubclass(est[1], ClusterMixin)] elif type_filter is not None: raise ValueError("Parameter type_filter must be 'classifier', " "'regressor', 'transformer', 'cluster' or None, got" " %s." % repr(type_filter)) # We sort in order to have reproducible test failures return sorted(estimators) def set_random_state(estimator, random_state=0): if "random_state" in estimator.get_params().keys(): estimator.set_params(random_state=random_state) def if_matplotlib(func): """Test decorator that skips test if matplotlib not installed. """ @wraps(func) def run_test(*args, **kwargs): try: import matplotlib matplotlib.use('Agg', warn=False) # this fails if no $DISPLAY specified matplotlib.pylab.figure() except: raise SkipTest('Matplotlib not available.') else: return func(*args, **kwargs) return run_test
bsd-3-clause
boomsbloom/dtm-fmri
DTM/for_gensim/lib/python2.7/site-packages/pandas/computation/ops.py
7
15881
"""Operator classes for eval. """ import operator as op from functools import partial from datetime import datetime import numpy as np from pandas.types.common import is_list_like, is_scalar import pandas as pd from pandas.compat import PY3, string_types, text_type import pandas.core.common as com from pandas.formats.printing import pprint_thing, pprint_thing_encoded from pandas.core.base import StringMixin from pandas.computation.common import _ensure_decoded, _result_type_many from pandas.computation.scope import _DEFAULT_GLOBALS _reductions = 'sum', 'prod' _unary_math_ops = ('sin', 'cos', 'exp', 'log', 'expm1', 'log1p', 'sqrt', 'sinh', 'cosh', 'tanh', 'arcsin', 'arccos', 'arctan', 'arccosh', 'arcsinh', 'arctanh', 'abs') _binary_math_ops = ('arctan2',) _mathops = _unary_math_ops + _binary_math_ops _LOCAL_TAG = '__pd_eval_local_' class UndefinedVariableError(NameError): """NameError subclass for local variables.""" def __init__(self, name, is_local): if is_local: msg = 'local variable {0!r} is not defined' else: msg = 'name {0!r} is not defined' super(UndefinedVariableError, self).__init__(msg.format(name)) class Term(StringMixin): def __new__(cls, name, env, side=None, encoding=None): klass = Constant if not isinstance(name, string_types) else cls supr_new = super(Term, klass).__new__ return supr_new(klass) def __init__(self, name, env, side=None, encoding=None): self._name = name self.env = env self.side = side tname = text_type(name) self.is_local = (tname.startswith(_LOCAL_TAG) or tname in _DEFAULT_GLOBALS) self._value = self._resolve_name() self.encoding = encoding @property def local_name(self): return self.name.replace(_LOCAL_TAG, '') def __unicode__(self): return pprint_thing(self.name) def __call__(self, *args, **kwargs): return self.value def evaluate(self, *args, **kwargs): return self def _resolve_name(self): res = self.env.resolve(self.local_name, is_local=self.is_local) self.update(res) if hasattr(res, 'ndim') and res.ndim > 2: raise NotImplementedError("N-dimensional objects, where N > 2," " are not supported with eval") return res def update(self, value): """ search order for local (i.e., @variable) variables: scope, key_variable [('locals', 'local_name'), ('globals', 'local_name'), ('locals', 'key'), ('globals', 'key')] """ key = self.name # if it's a variable name (otherwise a constant) if isinstance(key, string_types): self.env.swapkey(self.local_name, key, new_value=value) self.value = value @property def isscalar(self): return is_scalar(self._value) @property def type(self): try: # potentially very slow for large, mixed dtype frames return self._value.values.dtype except AttributeError: try: # ndarray return self._value.dtype except AttributeError: # scalar return type(self._value) return_type = type @property def raw(self): return pprint_thing('{0}(name={1!r}, type={2})' ''.format(self.__class__.__name__, self.name, self.type)) @property def is_datetime(self): try: t = self.type.type except AttributeError: t = self.type return issubclass(t, (datetime, np.datetime64)) @property def value(self): return self._value @value.setter def value(self, new_value): self._value = new_value @property def name(self): return self._name @name.setter def name(self, new_name): self._name = new_name @property def ndim(self): return self._value.ndim class Constant(Term): def __init__(self, value, env, side=None, encoding=None): super(Constant, self).__init__(value, env, side=side, encoding=encoding) def _resolve_name(self): return self._name @property def name(self): return self.value def __unicode__(self): # in python 2 str() of float # can truncate shorter than repr() return repr(self.name) _bool_op_map = {'not': '~', 'and': '&', 'or': '|'} class Op(StringMixin): """Hold an operator of arbitrary arity """ def __init__(self, op, operands, *args, **kwargs): self.op = _bool_op_map.get(op, op) self.operands = operands self.encoding = kwargs.get('encoding', None) def __iter__(self): return iter(self.operands) def __unicode__(self): """Print a generic n-ary operator and its operands using infix notation""" # recurse over the operands parened = ('({0})'.format(pprint_thing(opr)) for opr in self.operands) return pprint_thing(' {0} '.format(self.op).join(parened)) @property def return_type(self): # clobber types to bool if the op is a boolean operator if self.op in (_cmp_ops_syms + _bool_ops_syms): return np.bool_ return _result_type_many(*(term.type for term in com.flatten(self))) @property def has_invalid_return_type(self): types = self.operand_types obj_dtype_set = frozenset([np.dtype('object')]) return self.return_type == object and types - obj_dtype_set @property def operand_types(self): return frozenset(term.type for term in com.flatten(self)) @property def isscalar(self): return all(operand.isscalar for operand in self.operands) @property def is_datetime(self): try: t = self.return_type.type except AttributeError: t = self.return_type return issubclass(t, (datetime, np.datetime64)) def _in(x, y): """Compute the vectorized membership of ``x in y`` if possible, otherwise use Python. """ try: return x.isin(y) except AttributeError: if is_list_like(x): try: return y.isin(x) except AttributeError: pass return x in y def _not_in(x, y): """Compute the vectorized membership of ``x not in y`` if possible, otherwise use Python. """ try: return ~x.isin(y) except AttributeError: if is_list_like(x): try: return ~y.isin(x) except AttributeError: pass return x not in y _cmp_ops_syms = '>', '<', '>=', '<=', '==', '!=', 'in', 'not in' _cmp_ops_funcs = op.gt, op.lt, op.ge, op.le, op.eq, op.ne, _in, _not_in _cmp_ops_dict = dict(zip(_cmp_ops_syms, _cmp_ops_funcs)) _bool_ops_syms = '&', '|', 'and', 'or' _bool_ops_funcs = op.and_, op.or_, op.and_, op.or_ _bool_ops_dict = dict(zip(_bool_ops_syms, _bool_ops_funcs)) _arith_ops_syms = '+', '-', '*', '/', '**', '//', '%' _arith_ops_funcs = (op.add, op.sub, op.mul, op.truediv if PY3 else op.div, op.pow, op.floordiv, op.mod) _arith_ops_dict = dict(zip(_arith_ops_syms, _arith_ops_funcs)) _special_case_arith_ops_syms = '**', '//', '%' _special_case_arith_ops_funcs = op.pow, op.floordiv, op.mod _special_case_arith_ops_dict = dict(zip(_special_case_arith_ops_syms, _special_case_arith_ops_funcs)) _binary_ops_dict = {} for d in (_cmp_ops_dict, _bool_ops_dict, _arith_ops_dict): _binary_ops_dict.update(d) def _cast_inplace(terms, acceptable_dtypes, dtype): """Cast an expression inplace. Parameters ---------- terms : Op The expression that should cast. acceptable_dtypes : list of acceptable numpy.dtype Will not cast if term's dtype in this list. .. versionadded:: 0.19.0 dtype : str or numpy.dtype The dtype to cast to. """ dt = np.dtype(dtype) for term in terms: if term.type in acceptable_dtypes: continue try: new_value = term.value.astype(dt) except AttributeError: new_value = dt.type(term.value) term.update(new_value) def is_term(obj): return isinstance(obj, Term) class BinOp(Op): """Hold a binary operator and its operands Parameters ---------- op : str left : Term or Op right : Term or Op """ def __init__(self, op, lhs, rhs, **kwargs): super(BinOp, self).__init__(op, (lhs, rhs)) self.lhs = lhs self.rhs = rhs self._disallow_scalar_only_bool_ops() self.convert_values() try: self.func = _binary_ops_dict[op] except KeyError: # has to be made a list for python3 keys = list(_binary_ops_dict.keys()) raise ValueError('Invalid binary operator {0!r}, valid' ' operators are {1}'.format(op, keys)) def __call__(self, env): """Recursively evaluate an expression in Python space. Parameters ---------- env : Scope Returns ------- object The result of an evaluated expression. """ # handle truediv if self.op == '/' and env.scope['truediv']: self.func = op.truediv # recurse over the left/right nodes left = self.lhs(env) right = self.rhs(env) return self.func(left, right) def evaluate(self, env, engine, parser, term_type, eval_in_python): """Evaluate a binary operation *before* being passed to the engine. Parameters ---------- env : Scope engine : str parser : str term_type : type eval_in_python : list Returns ------- term_type The "pre-evaluated" expression as an instance of ``term_type`` """ if engine == 'python': res = self(env) else: # recurse over the left/right nodes left = self.lhs.evaluate(env, engine=engine, parser=parser, term_type=term_type, eval_in_python=eval_in_python) right = self.rhs.evaluate(env, engine=engine, parser=parser, term_type=term_type, eval_in_python=eval_in_python) # base cases if self.op in eval_in_python: res = self.func(left.value, right.value) else: res = pd.eval(self, local_dict=env, engine=engine, parser=parser) name = env.add_tmp(res) return term_type(name, env=env) def convert_values(self): """Convert datetimes to a comparable value in an expression. """ def stringify(value): if self.encoding is not None: encoder = partial(pprint_thing_encoded, encoding=self.encoding) else: encoder = pprint_thing return encoder(value) lhs, rhs = self.lhs, self.rhs if is_term(lhs) and lhs.is_datetime and is_term(rhs) and rhs.isscalar: v = rhs.value if isinstance(v, (int, float)): v = stringify(v) v = pd.Timestamp(_ensure_decoded(v)) if v.tz is not None: v = v.tz_convert('UTC') self.rhs.update(v) if is_term(rhs) and rhs.is_datetime and is_term(lhs) and lhs.isscalar: v = lhs.value if isinstance(v, (int, float)): v = stringify(v) v = pd.Timestamp(_ensure_decoded(v)) if v.tz is not None: v = v.tz_convert('UTC') self.lhs.update(v) def _disallow_scalar_only_bool_ops(self): if ((self.lhs.isscalar or self.rhs.isscalar) and self.op in _bool_ops_dict and (not (issubclass(self.rhs.return_type, (bool, np.bool_)) and issubclass(self.lhs.return_type, (bool, np.bool_))))): raise NotImplementedError("cannot evaluate scalar only bool ops") def isnumeric(dtype): return issubclass(np.dtype(dtype).type, np.number) class Div(BinOp): """Div operator to special case casting. Parameters ---------- lhs, rhs : Term or Op The Terms or Ops in the ``/`` expression. truediv : bool Whether or not to use true division. With Python 3 this happens regardless of the value of ``truediv``. """ def __init__(self, lhs, rhs, truediv, *args, **kwargs): super(Div, self).__init__('/', lhs, rhs, *args, **kwargs) if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type): raise TypeError("unsupported operand type(s) for {0}:" " '{1}' and '{2}'".format(self.op, lhs.return_type, rhs.return_type)) if truediv or PY3: # do not upcast float32s to float64 un-necessarily acceptable_dtypes = [np.float32, np.float_] _cast_inplace(com.flatten(self), acceptable_dtypes, np.float_) _unary_ops_syms = '+', '-', '~', 'not' _unary_ops_funcs = op.pos, op.neg, op.invert, op.invert _unary_ops_dict = dict(zip(_unary_ops_syms, _unary_ops_funcs)) class UnaryOp(Op): """Hold a unary operator and its operands Parameters ---------- op : str The token used to represent the operator. operand : Term or Op The Term or Op operand to the operator. Raises ------ ValueError * If no function associated with the passed operator token is found. """ def __init__(self, op, operand): super(UnaryOp, self).__init__(op, (operand,)) self.operand = operand try: self.func = _unary_ops_dict[op] except KeyError: raise ValueError('Invalid unary operator {0!r}, valid operators ' 'are {1}'.format(op, _unary_ops_syms)) def __call__(self, env): operand = self.operand(env) return self.func(operand) def __unicode__(self): return pprint_thing('{0}({1})'.format(self.op, self.operand)) @property def return_type(self): operand = self.operand if operand.return_type == np.dtype('bool'): return np.dtype('bool') if (isinstance(operand, Op) and (operand.op in _cmp_ops_dict or operand.op in _bool_ops_dict)): return np.dtype('bool') return np.dtype('int') class MathCall(Op): def __init__(self, func, args): super(MathCall, self).__init__(func.name, args) self.func = func def __call__(self, env): operands = [op(env) for op in self.operands] with np.errstate(all='ignore'): return self.func.func(*operands) def __unicode__(self): operands = map(str, self.operands) return pprint_thing('{0}({1})'.format(self.op, ','.join(operands))) class FuncNode(object): def __init__(self, name): if name not in _mathops: raise ValueError( "\"{0}\" is not a supported function".format(name)) self.name = name self.func = getattr(np, name) def __call__(self, *args): return MathCall(self, args)
mit
vickyting0910/opengeocoding
2reinter.py
1
3991
import pandas as pd import glob import time import numpy as num inter=sorted(glob.glob('*****.csv')) w='*****.xlsx' table1=pd.read_excel(w, '*****', index_col=None, na_values=['NA']).fillna(0) w='*****.csv' tab=pd.read_csv(w).fillna(0) tab.is_copy = False pd.options.mode.chained_assignment = None t1=time.time() for i in range(len(tab)): if tab["IBR"][i]=='9A' or tab["IBR"][i] == '9B' or tab["IBR"][i] == '09A' or tab["IBR"][i] == '09B': tab["IBR"][i]='9' if tab["IBR"][i]=='11A' or tab["IBR"][i] == '11B' or tab["IBR"][i]=='11C' or tab["IBR"][i] == '11D' or tab["IBR"][i]=='36B': tab["IBR"][i]='11' if tab["IBR"][i]=='36A' or tab["IBR"][i] == '36B': tab["IBR"][i]='36' if tab["IBR"][i]=='13A' or tab["IBR"][i] == '13B' or tab["IBR"][i] == '13C': tab["IBR"][i]='13' if tab["IBR"][i]=='23A' or tab["IBR"][i] == '23B' or tab["IBR"][i] == '23E' or tab["IBR"][i] == '23F' or tab["IBR"][i] == '23H': tab["IBR"][i]='23' if tab["IBR"][i]=='26A' or tab["IBR"][i] == '26B' or tab["IBR"][i] == '26C' or tab["IBR"][i] == '26D' or tab["IBR"][i] == '26E': tab["IBR"][i]='26' if tab["IBR"][i]=='35A' or tab["IBR"][i] == '35B': tab["IBR"][i]='35' if tab["IBR"][i]=='36A': tab["IBR"][i]='36' if tab["IBR"][i]=='39A' or tab["IBR"][i] == '39B' or tab["IBR"][i] == '39C' or tab["IBR"][i] == '39D': tab["IBR"][i]='39' if tab["IBR"][i]=='40A' or tab["IBR"][i] == '40B' or tab["IBR"][i] == '40C': tab["IBR"][i]='40' if tab["IBR"][i]=='64A' or tab["IBR"][i] == '64B': tab["IBR"][i]='64' if tab["IBR"][i]=='90A' or tab["IBR"][i] == '90B' or tab["IBR"][i] == '90C' or tab["IBR"][i] == '90H' or tab["IBR"][i] == '90F' or tab["IBR"][i] == '90G' or tab["IBR"][i]=='90J' or tab["IBR"][i]=='90Z': tab["IBR"][i]='90' #convert to string for the join for i in range(len(table1)): table1['IBR_code'][i]=str(table1['IBR_code'][i]) description=table1.set_index([ "IBR_code"]) t2=time.time() print t2-t1 #index crime tab["index"]=num.nan for i in range(len(tab)): #convert to integer tab["index"][i]=tab.index[i]+1 #join tab=tab.join(description, on=["IBR"], sort=True, rsuffix='_1', how='outer').fillna(0) tab=tab[(tab["Reported_address"] != 0)].reset_index(drop=True).fillna(0) tab["IBR_description"]=tab["crime_des12"] t3=time.time() print t3-t2 tab=tab[["Global_ID","Reported_address","Incident_date","Incident_time","Report_date","Report_time","Latitude","Longitude","IBR","IBR_description","Police_Department_Code","PD_description","State_Statute_Literal","State_Statute_Number","flag_geocode",'Fdir_n1','Edir_n1','strname_n1','strtype_n1','Enum_n1','Fdir_n2','Edir_n2','strname_n2','strtype_n2','Enum_n2','comname','mroad1','mratio1','wcorr1','wratio1','mroad2','mratio2','wcorr2','wratio2','match']] tab=tab.replace("",num.nan) tab=tab.replace("0",num.nan) tab=tab.replace("00",num.nan) tab=tab.replace(0,num.nan) tab.to_csv('*****.csv',index=False) for i in range(len(tab)): tab['Global_ID'][i]=str(tab['Global_ID'][i]) description=tab.set_index([ "Global_ID"]) name1=[i[i.find('inter'):i.rfind('C.csv')+1].replace('_matchgeo','') for i in inter] for p, q in zip((inter), (name1)): table1=pd.read_csv(p) for i in range(len(table1)): tab['Global_ID'][i]=str(tab['Global_ID'][i]) table1=table1.join(description, on=["Global_ID"], sort=True, rsuffix='_1', how='outer').fillna(0) table1=table1[(table1["Reported_address"] != 0)].reset_index(drop=True).fillna(0) table1["IBR_description"]=table1["IBR_description_1"] table1["IBR"]=table1["IBR_1"] table1=table1[["Global_ID","Reported_address","Incident_date","Incident_time","Report_date","Report_time","Latitude","Longitude","IBR","IBR_description","Police_Department_Code","PD_description","State_Statute_Literal","State_Statute_Number","flag_geocode",'Fdir_n1','Edir_n1','strname_n1','strtype_n1','Enum_n1','Fdir_n2','Edir_n2','strname_n2','strtype_n2','Enum_n2','comname','mroad1','mratio1','wcorr1','wratio1','mroad2','mratio2','wcorr2','wratio2','match']] table1.to_csv('*****.csv',index=False)
bsd-2-clause
abimannans/scikit-learn
examples/linear_model/plot_logistic_path.py
349
1195
#!/usr/bin/env python """ ================================= Path with L1- Logistic Regression ================================= Computes path on IRIS dataset. """ print(__doc__) # Author: Alexandre Gramfort <alexandre.gramfort@inria.fr> # License: BSD 3 clause from datetime import datetime import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model from sklearn import datasets from sklearn.svm import l1_min_c iris = datasets.load_iris() X = iris.data y = iris.target X = X[y != 2] y = y[y != 2] X -= np.mean(X, 0) ############################################################################### # Demo path functions cs = l1_min_c(X, y, loss='log') * np.logspace(0, 3) print("Computing regularization path ...") start = datetime.now() clf = linear_model.LogisticRegression(C=1.0, penalty='l1', tol=1e-6) coefs_ = [] for c in cs: clf.set_params(C=c) clf.fit(X, y) coefs_.append(clf.coef_.ravel().copy()) print("This took ", datetime.now() - start) coefs_ = np.array(coefs_) plt.plot(np.log10(cs), coefs_) ymin, ymax = plt.ylim() plt.xlabel('log(C)') plt.ylabel('Coefficients') plt.title('Logistic Regression Path') plt.axis('tight') plt.show()
bsd-3-clause
timqian/sms-tools
lectures/8-Sound-transformations/plots-code/sineModelFreqScale-orchestra.py
21
2666
import numpy as np import matplotlib.pyplot as plt from scipy.signal import hamming, hanning, triang, blackmanharris, resample import math import sys, os, functools, time sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/models/')) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/transformations/')) import sineModel as SM import stft as STFT import utilFunctions as UF import sineTransformations as SMT (fs, x) = UF.wavread('../../../sounds/orchestra.wav') w = np.hamming(801) N = 2048 t = -90 minSineDur = .005 maxnSines = 150 freqDevOffset = 20 freqDevSlope = 0.02 Ns = 512 H = Ns/4 mX, pX = STFT.stftAnal(x, fs, w, N, H) tfreq, tmag, tphase = SM.sineModelAnal(x, fs, w, N, H, t, maxnSines, minSineDur, freqDevOffset, freqDevSlope) freqScaling = np.array([0, .8, 1, 1.2]) ytfreq = SMT.sineFreqScaling(tfreq, freqScaling) y = SM.sineModelSynth(ytfreq, tmag, np.array([]), Ns, H, fs) mY, pY = STFT.stftAnal(y, fs, w, N, H) UF.wavwrite(y,fs, 'sineModelFreqScale-orchestra.wav') maxplotfreq = 4000.0 plt.figure(1, figsize=(9.5, 7)) plt.subplot(4,1,1) plt.plot(np.arange(x.size)/float(fs), x, 'b') plt.axis([0,x.size/float(fs),min(x),max(x)]) plt.title('x (orchestra.wav)') plt.subplot(4,1,2) numFrames = int(tfreq[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) tracks = tfreq*np.less(tfreq, maxplotfreq) tracks[tracks<=0] = np.nan plt.plot(frmTime, tracks, color='k', lw=1) plt.autoscale(tight=True) plt.title('sine frequencies') maxplotbin = int(N*maxplotfreq/fs) numFrames = int(mX[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = np.arange(maxplotbin+1)*float(fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(mX[:,:maxplotbin+1])) plt.autoscale(tight=True) plt.subplot(4,1,3) numFrames = int(ytfreq[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) tracks = ytfreq*np.less(ytfreq, maxplotfreq) tracks[tracks<=0] = np.nan plt.plot(frmTime, tracks, color='k', lw=1) plt.autoscale(tight=True) plt.title('freq-scaled sine frequencies') maxplotbin = int(N*maxplotfreq/fs) numFrames = int(mY[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = np.arange(maxplotbin+1)*float(fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(mY[:,:maxplotbin+1])) plt.autoscale(tight=True) plt.subplot(4,1,4) plt.plot(np.arange(y.size)/float(fs), y, 'b') plt.axis([0,y.size/float(fs),min(y),max(y)]) plt.title('y') plt.tight_layout() plt.savefig('sineModelFreqScale-orchestra.png') plt.show()
agpl-3.0
DeepVisionTeam/TensorFlowBook
Titanic/data_processing.py
2
4807
import os import re import pandas as pd import tensorflow as tf pjoin = os.path.join DATA_DIR = pjoin(os.path.dirname(__file__), 'data') train_data = pd.read_csv(pjoin(DATA_DIR, 'train.csv')) test_data = pd.read_csv(pjoin(DATA_DIR, 'test.csv')) # Translation: # Don: an honorific title used in Spain, Portugal, Italy # Dona: Feminine form for don # Mme: Madame, Mrs # Mlle: Mademoiselle, Miss # Jonkheer (female equivalent: Jonkvrouw) is a Dutch honorific of nobility HONORABLE_TITLES = ['sir', 'lady', 'don', 'dona', 'countess', 'jonkheer', 'major', 'col', 'dr', 'master', 'capt'] NORMAL_TITLES = ['mr', 'ms', 'mrs', 'miss', 'mme', 'mlle', 'rev'] TITLES = HONORABLE_TITLES + NORMAL_TITLES def get_title(name): title_search = re.search('([A-Za-z]+)\.', name) return title_search.group(1).lower() def get_family(row): last_name = row['Name'].split(",")[0] if last_name: family_size = 1 + row['Parch'] + row['SibSp'] if family_size > 3: return "{0}_{1}".format(last_name.lower(), family_size) else: return "nofamily" else: return "unknown" def get_deck(cabin): if pd.isnull(cabin): return 'U' return cabin[:1] class TitanicDigest(object): def __init__(self, dataset): self.count_by_sex = dataset.groupby('Sex')['PassengerId'].count() self.mean_age = dataset['Age'].mean() self.mean_age_by_sex = dataset.groupby("Sex")["Age"].mean() self.mean_fare_by_class = dataset.groupby("Pclass")["Fare"].mean() self.titles = TITLES self.families = dataset.apply(get_family, axis=1).unique().tolist() self.decks = dataset["Cabin"].apply(get_deck).unique().tolist() self.embarkments = dataset.Embarked.unique().tolist() self.embark_mode = dataset.Embarked.dropna().mode().values def preprocess(data, digest): # convert ['male', 'female'] values of Sex to [1, 0] data['Sex'] = data['Sex'].apply(lambda s: 1 if s == 'male' else 0) # fill empty age field with mean age data['Age'] = data['Age'].apply( lambda age: digest.mean_age if pd.isnull(age) else age) # is child flag data['Child'] = data['Age'].apply(lambda age: 1 if age <= 15 else 0) # fill fare with mean fare of the class def get_fare_value(row): if pd.isnull(row['Fare']): return digest.mean_fare_by_class[row['Pclass']] else: return row['Fare'] data['Fare'] = data.apply(get_fare_value, axis=1) # fill Embarked with mode data['Embarked'] = data['Embarked'].apply( lambda e: digest.embark_mode if pd.isnull(e) else e) data["EmbarkedF"] = data["Embarked"].apply(digest.embarkments.index) # data['Cabin'] = data['Cabin'].apply(lambda c: 'U0' if pd.isnull(c) else c) # Deck data["Deck"] = data["Cabin"].apply(lambda cabin: cabin[0]) data["DeckF"] = data['Deck'].apply(digest.decks.index) data['Title'] = data['Name'].apply(get_title) data['TitleF'] = data['Title'].apply(digest.titles.index) data['Honor'] = data['Title'].apply( lambda title: int(title in HONORABLE_TITLES)) data['Family'] = data.apply(get_family, axis=1) if 'Survived' in data.keys(): data['Deceased'] = data['Survived'].apply(lambda s: int(not s)) return data digest = TitanicDigest(train_data) def get_train_data(): return preprocess(train_data, digest) def get_test_data(): return preprocess(test_data, digest) def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def transform_to_tfrecord(): data = pd.read_csv(pjoin(DATA_DIR, 'train.csv')) filepath = pjoin(DATA_DIR, 'data.tfrecords') writer = tf.python_io.TFRecordWriter(filepath) for i in range(len(data)): feature = {} for key in data.keys(): value = data[key][i] if isinstance(value, int): value = tf.train.Feature( int64_list=tf.train.Int64List(value=[value])) elif isinstance(value, float): value = tf.train.Feature( float_list=tf.train.FloatList(value=[value]) ) elif isinstance(value, str): value = tf.train.Feature( bytes_list=tf.train.BytesList( value=[value.encode(encoding="utf-8")]) ) feature[key] = value example = tf.train.Example( features=tf.train.Features(feature=feature)) writer.write(example.SerializeToString()) writer.close() if __name__ == '__main__': transform_to_tfrecord()
apache-2.0
linebp/pandas
pandas/tests/series/test_indexing.py
1
88099
# coding=utf-8 # pylint: disable-msg=E1101,W0612 import pytest from datetime import datetime, timedelta from numpy import nan import numpy as np import pandas as pd import pandas._libs.index as _index from pandas.core.dtypes.common import is_integer, is_scalar from pandas import (Index, Series, DataFrame, isnull, date_range, NaT, MultiIndex, Timestamp, DatetimeIndex, Timedelta) from pandas.core.indexing import IndexingError from pandas.tseries.offsets import BDay from pandas._libs import tslib, lib from pandas.compat import lrange, range from pandas import compat from pandas.util.testing import (slow, assert_series_equal, assert_almost_equal, assert_frame_equal) import pandas.util.testing as tm from pandas.tests.series.common import TestData JOIN_TYPES = ['inner', 'outer', 'left', 'right'] class TestSeriesIndexing(TestData): def test_get(self): # GH 6383 s = Series(np.array([43, 48, 60, 48, 50, 51, 50, 45, 57, 48, 56, 45, 51, 39, 55, 43, 54, 52, 51, 54])) result = s.get(25, 0) expected = 0 assert result == expected s = Series(np.array([43, 48, 60, 48, 50, 51, 50, 45, 57, 48, 56, 45, 51, 39, 55, 43, 54, 52, 51, 54]), index=pd.Float64Index( [25.0, 36.0, 49.0, 64.0, 81.0, 100.0, 121.0, 144.0, 169.0, 196.0, 1225.0, 1296.0, 1369.0, 1444.0, 1521.0, 1600.0, 1681.0, 1764.0, 1849.0, 1936.0], dtype='object')) result = s.get(25, 0) expected = 43 assert result == expected # GH 7407 # with a boolean accessor df = pd.DataFrame({'i': [0] * 3, 'b': [False] * 3}) vc = df.i.value_counts() result = vc.get(99, default='Missing') assert result == 'Missing' vc = df.b.value_counts() result = vc.get(False, default='Missing') assert result == 3 result = vc.get(True, default='Missing') assert result == 'Missing' def test_get_nan(self): # GH 8569 s = pd.Float64Index(range(10)).to_series() assert s.get(np.nan) is None assert s.get(np.nan, default='Missing') == 'Missing' # ensure that fixing the above hasn't broken get # with multiple elements idx = [20, 30] assert_series_equal(s.get(idx), Series([np.nan] * 2, index=idx)) idx = [np.nan, np.nan] assert_series_equal(s.get(idx), Series([np.nan] * 2, index=idx)) def test_delitem(self): # GH 5542 # should delete the item inplace s = Series(lrange(5)) del s[0] expected = Series(lrange(1, 5), index=lrange(1, 5)) assert_series_equal(s, expected) del s[1] expected = Series(lrange(2, 5), index=lrange(2, 5)) assert_series_equal(s, expected) # empty s = Series() def f(): del s[0] pytest.raises(KeyError, f) # only 1 left, del, add, del s = Series(1) del s[0] assert_series_equal(s, Series(dtype='int64', index=Index( [], dtype='int64'))) s[0] = 1 assert_series_equal(s, Series(1)) del s[0] assert_series_equal(s, Series(dtype='int64', index=Index( [], dtype='int64'))) # Index(dtype=object) s = Series(1, index=['a']) del s['a'] assert_series_equal(s, Series(dtype='int64', index=Index( [], dtype='object'))) s['a'] = 1 assert_series_equal(s, Series(1, index=['a'])) del s['a'] assert_series_equal(s, Series(dtype='int64', index=Index( [], dtype='object'))) def test_getitem_setitem_ellipsis(self): s = Series(np.random.randn(10)) np.fix(s) result = s[...] assert_series_equal(result, s) s[...] = 5 assert (result == 5).all() def test_getitem_negative_out_of_bounds(self): s = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10)) pytest.raises(IndexError, s.__getitem__, -11) pytest.raises(IndexError, s.__setitem__, -11, 'foo') def test_pop(self): # GH 6600 df = DataFrame({'A': 0, 'B': np.arange(5, dtype='int64'), 'C': 0, }) k = df.iloc[4] result = k.pop('B') assert result == 4 expected = Series([0, 0], index=['A', 'C'], name=4) assert_series_equal(k, expected) def test_getitem_get(self): idx1 = self.series.index[5] idx2 = self.objSeries.index[5] assert self.series[idx1] == self.series.get(idx1) assert self.objSeries[idx2] == self.objSeries.get(idx2) assert self.series[idx1] == self.series[5] assert self.objSeries[idx2] == self.objSeries[5] assert self.series.get(-1) == self.series.get(self.series.index[-1]) assert self.series[5] == self.series.get(self.series.index[5]) # missing d = self.ts.index[0] - BDay() pytest.raises(KeyError, self.ts.__getitem__, d) # None # GH 5652 for s in [Series(), Series(index=list('abc'))]: result = s.get(None) assert result is None def test_iloc(self): s = Series(np.random.randn(10), index=lrange(0, 20, 2)) for i in range(len(s)): result = s.iloc[i] exp = s[s.index[i]] assert_almost_equal(result, exp) # pass a slice result = s.iloc[slice(1, 3)] expected = s.loc[2:4] assert_series_equal(result, expected) # test slice is a view result[:] = 0 assert (s[1:3] == 0).all() # list of integers result = s.iloc[[0, 2, 3, 4, 5]] expected = s.reindex(s.index[[0, 2, 3, 4, 5]]) assert_series_equal(result, expected) def test_iloc_nonunique(self): s = Series([0, 1, 2], index=[0, 1, 0]) assert s.iloc[2] == 2 def test_getitem_regression(self): s = Series(lrange(5), index=lrange(5)) result = s[lrange(5)] assert_series_equal(result, s) def test_getitem_setitem_slice_bug(self): s = Series(lrange(10), lrange(10)) result = s[-12:] assert_series_equal(result, s) result = s[-7:] assert_series_equal(result, s[3:]) result = s[:-12] assert_series_equal(result, s[:0]) s = Series(lrange(10), lrange(10)) s[-12:] = 0 assert (s == 0).all() s[:-12] = 5 assert (s == 0).all() def test_getitem_int64(self): idx = np.int64(5) assert self.ts[idx] == self.ts[5] def test_getitem_fancy(self): slice1 = self.series[[1, 2, 3]] slice2 = self.objSeries[[1, 2, 3]] assert self.series.index[2] == slice1.index[1] assert self.objSeries.index[2] == slice2.index[1] assert self.series[2] == slice1[1] assert self.objSeries[2] == slice2[1] def test_getitem_boolean(self): s = self.series mask = s > s.median() # passing list is OK result = s[list(mask)] expected = s[mask] assert_series_equal(result, expected) tm.assert_index_equal(result.index, s.index[mask]) def test_getitem_boolean_empty(self): s = Series([], dtype=np.int64) s.index.name = 'index_name' s = s[s.isnull()] assert s.index.name == 'index_name' assert s.dtype == np.int64 # GH5877 # indexing with empty series s = Series(['A', 'B']) expected = Series(np.nan, index=['C'], dtype=object) result = s[Series(['C'], dtype=object)] assert_series_equal(result, expected) s = Series(['A', 'B']) expected = Series(dtype=object, index=Index([], dtype='int64')) result = s[Series([], dtype=object)] assert_series_equal(result, expected) # invalid because of the boolean indexer # that's empty or not-aligned def f(): s[Series([], dtype=bool)] pytest.raises(IndexingError, f) def f(): s[Series([True], dtype=bool)] pytest.raises(IndexingError, f) def test_getitem_generator(self): gen = (x > 0 for x in self.series) result = self.series[gen] result2 = self.series[iter(self.series > 0)] expected = self.series[self.series > 0] assert_series_equal(result, expected) assert_series_equal(result2, expected) def test_type_promotion(self): # GH12599 s = pd.Series() s["a"] = pd.Timestamp("2016-01-01") s["b"] = 3.0 s["c"] = "foo" expected = Series([pd.Timestamp("2016-01-01"), 3.0, "foo"], index=["a", "b", "c"]) assert_series_equal(s, expected) def test_getitem_boolean_object(self): # using column from DataFrame s = self.series mask = s > s.median() omask = mask.astype(object) # getitem result = s[omask] expected = s[mask] assert_series_equal(result, expected) # setitem s2 = s.copy() cop = s.copy() cop[omask] = 5 s2[mask] = 5 assert_series_equal(cop, s2) # nans raise exception omask[5:10] = np.nan pytest.raises(Exception, s.__getitem__, omask) pytest.raises(Exception, s.__setitem__, omask, 5) def test_getitem_setitem_boolean_corner(self): ts = self.ts mask_shifted = ts.shift(1, freq=BDay()) > ts.median() # these used to raise...?? pytest.raises(Exception, ts.__getitem__, mask_shifted) pytest.raises(Exception, ts.__setitem__, mask_shifted, 1) # ts[mask_shifted] # ts[mask_shifted] = 1 pytest.raises(Exception, ts.loc.__getitem__, mask_shifted) pytest.raises(Exception, ts.loc.__setitem__, mask_shifted, 1) # ts.loc[mask_shifted] # ts.loc[mask_shifted] = 2 def test_getitem_setitem_slice_integers(self): s = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16]) result = s[:4] expected = s.reindex([2, 4, 6, 8]) assert_series_equal(result, expected) s[:4] = 0 assert (s[:4] == 0).all() assert not (s[4:] == 0).any() def test_getitem_setitem_datetime_tz_pytz(self): from pytz import timezone as tz from pandas import date_range N = 50 # testing with timezone, GH #2785 rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern') ts = Series(np.random.randn(N), index=rng) # also test Timestamp tz handling, GH #2789 result = ts.copy() result["1990-01-01 09:00:00+00:00"] = 0 result["1990-01-01 09:00:00+00:00"] = ts[4] assert_series_equal(result, ts) result = ts.copy() result["1990-01-01 03:00:00-06:00"] = 0 result["1990-01-01 03:00:00-06:00"] = ts[4] assert_series_equal(result, ts) # repeat with datetimes result = ts.copy() result[datetime(1990, 1, 1, 9, tzinfo=tz('UTC'))] = 0 result[datetime(1990, 1, 1, 9, tzinfo=tz('UTC'))] = ts[4] assert_series_equal(result, ts) result = ts.copy() # comparison dates with datetime MUST be localized! date = tz('US/Central').localize(datetime(1990, 1, 1, 3)) result[date] = 0 result[date] = ts[4] assert_series_equal(result, ts) def test_getitem_setitem_datetime_tz_dateutil(self): from dateutil.tz import tzutc from pandas._libs.tslib import _dateutil_gettz as gettz tz = lambda x: tzutc() if x == 'UTC' else gettz( x) # handle special case for utc in dateutil from pandas import date_range N = 50 # testing with timezone, GH #2785 rng = date_range('1/1/1990', periods=N, freq='H', tz='America/New_York') ts = Series(np.random.randn(N), index=rng) # also test Timestamp tz handling, GH #2789 result = ts.copy() result["1990-01-01 09:00:00+00:00"] = 0 result["1990-01-01 09:00:00+00:00"] = ts[4] assert_series_equal(result, ts) result = ts.copy() result["1990-01-01 03:00:00-06:00"] = 0 result["1990-01-01 03:00:00-06:00"] = ts[4] assert_series_equal(result, ts) # repeat with datetimes result = ts.copy() result[datetime(1990, 1, 1, 9, tzinfo=tz('UTC'))] = 0 result[datetime(1990, 1, 1, 9, tzinfo=tz('UTC'))] = ts[4] assert_series_equal(result, ts) result = ts.copy() result[datetime(1990, 1, 1, 3, tzinfo=tz('America/Chicago'))] = 0 result[datetime(1990, 1, 1, 3, tzinfo=tz('America/Chicago'))] = ts[4] assert_series_equal(result, ts) def test_getitem_setitem_datetimeindex(self): N = 50 # testing with timezone, GH #2785 rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern') ts = Series(np.random.randn(N), index=rng) result = ts["1990-01-01 04:00:00"] expected = ts[4] assert result == expected result = ts.copy() result["1990-01-01 04:00:00"] = 0 result["1990-01-01 04:00:00"] = ts[4] assert_series_equal(result, ts) result = ts["1990-01-01 04:00:00":"1990-01-01 07:00:00"] expected = ts[4:8] assert_series_equal(result, expected) result = ts.copy() result["1990-01-01 04:00:00":"1990-01-01 07:00:00"] = 0 result["1990-01-01 04:00:00":"1990-01-01 07:00:00"] = ts[4:8] assert_series_equal(result, ts) lb = "1990-01-01 04:00:00" rb = "1990-01-01 07:00:00" result = ts[(ts.index >= lb) & (ts.index <= rb)] expected = ts[4:8] assert_series_equal(result, expected) # repeat all the above with naive datetimes result = ts[datetime(1990, 1, 1, 4)] expected = ts[4] assert result == expected result = ts.copy() result[datetime(1990, 1, 1, 4)] = 0 result[datetime(1990, 1, 1, 4)] = ts[4] assert_series_equal(result, ts) result = ts[datetime(1990, 1, 1, 4):datetime(1990, 1, 1, 7)] expected = ts[4:8] assert_series_equal(result, expected) result = ts.copy() result[datetime(1990, 1, 1, 4):datetime(1990, 1, 1, 7)] = 0 result[datetime(1990, 1, 1, 4):datetime(1990, 1, 1, 7)] = ts[4:8] assert_series_equal(result, ts) lb = datetime(1990, 1, 1, 4) rb = datetime(1990, 1, 1, 7) result = ts[(ts.index >= lb) & (ts.index <= rb)] expected = ts[4:8] assert_series_equal(result, expected) result = ts[ts.index[4]] expected = ts[4] assert result == expected result = ts[ts.index[4:8]] expected = ts[4:8] assert_series_equal(result, expected) result = ts.copy() result[ts.index[4:8]] = 0 result[4:8] = ts[4:8] assert_series_equal(result, ts) # also test partial date slicing result = ts["1990-01-02"] expected = ts[24:48] assert_series_equal(result, expected) result = ts.copy() result["1990-01-02"] = 0 result["1990-01-02"] = ts[24:48] assert_series_equal(result, ts) def test_getitem_setitem_periodindex(self): from pandas import period_range N = 50 rng = period_range('1/1/1990', periods=N, freq='H') ts = Series(np.random.randn(N), index=rng) result = ts["1990-01-01 04"] expected = ts[4] assert result == expected result = ts.copy() result["1990-01-01 04"] = 0 result["1990-01-01 04"] = ts[4] assert_series_equal(result, ts) result = ts["1990-01-01 04":"1990-01-01 07"] expected = ts[4:8] assert_series_equal(result, expected) result = ts.copy() result["1990-01-01 04":"1990-01-01 07"] = 0 result["1990-01-01 04":"1990-01-01 07"] = ts[4:8] assert_series_equal(result, ts) lb = "1990-01-01 04" rb = "1990-01-01 07" result = ts[(ts.index >= lb) & (ts.index <= rb)] expected = ts[4:8] assert_series_equal(result, expected) # GH 2782 result = ts[ts.index[4]] expected = ts[4] assert result == expected result = ts[ts.index[4:8]] expected = ts[4:8] assert_series_equal(result, expected) result = ts.copy() result[ts.index[4:8]] = 0 result[4:8] = ts[4:8] assert_series_equal(result, ts) def test_getitem_median_slice_bug(self): index = date_range('20090415', '20090519', freq='2B') s = Series(np.random.randn(13), index=index) indexer = [slice(6, 7, None)] result = s[indexer] expected = s[indexer[0]] assert_series_equal(result, expected) def test_getitem_out_of_bounds(self): # don't segfault, GH #495 pytest.raises(IndexError, self.ts.__getitem__, len(self.ts)) # GH #917 s = Series([]) pytest.raises(IndexError, s.__getitem__, -1) def test_getitem_setitem_integers(self): # caused bug without test s = Series([1, 2, 3], ['a', 'b', 'c']) assert s.iloc[0] == s['a'] s.iloc[0] = 5 tm.assert_almost_equal(s['a'], 5) def test_getitem_box_float64(self): value = self.ts[5] assert isinstance(value, np.float64) def test_getitem_ambiguous_keyerror(self): s = Series(lrange(10), index=lrange(0, 20, 2)) pytest.raises(KeyError, s.__getitem__, 1) pytest.raises(KeyError, s.loc.__getitem__, 1) def test_getitem_unordered_dup(self): obj = Series(lrange(5), index=['c', 'a', 'a', 'b', 'b']) assert is_scalar(obj['c']) assert obj['c'] == 0 def test_getitem_dups_with_missing(self): # breaks reindex, so need to use .loc internally # GH 4246 s = Series([1, 2, 3, 4], ['foo', 'bar', 'foo', 'bah']) expected = s.loc[['foo', 'bar', 'bah', 'bam']] result = s[['foo', 'bar', 'bah', 'bam']] assert_series_equal(result, expected) def test_getitem_dups(self): s = Series(range(5), index=['A', 'A', 'B', 'C', 'C'], dtype=np.int64) expected = Series([3, 4], index=['C', 'C'], dtype=np.int64) result = s['C'] assert_series_equal(result, expected) def test_getitem_dataframe(self): rng = list(range(10)) s = pd.Series(10, index=rng) df = pd.DataFrame(rng, index=rng) pytest.raises(TypeError, s.__getitem__, df > 5) def test_getitem_callable(self): # GH 12533 s = pd.Series(4, index=list('ABCD')) result = s[lambda x: 'A'] assert result == s.loc['A'] result = s[lambda x: ['A', 'B']] tm.assert_series_equal(result, s.loc[['A', 'B']]) result = s[lambda x: [True, False, True, True]] tm.assert_series_equal(result, s.iloc[[0, 2, 3]]) def test_setitem_ambiguous_keyerror(self): s = Series(lrange(10), index=lrange(0, 20, 2)) # equivalent of an append s2 = s.copy() s2[1] = 5 expected = s.append(Series([5], index=[1])) assert_series_equal(s2, expected) s2 = s.copy() s2.loc[1] = 5 expected = s.append(Series([5], index=[1])) assert_series_equal(s2, expected) def test_setitem_float_labels(self): # note labels are floats s = Series(['a', 'b', 'c'], index=[0, 0.5, 1]) tmp = s.copy() s.loc[1] = 'zoo' tmp.iloc[2] = 'zoo' assert_series_equal(s, tmp) def test_setitem_callable(self): # GH 12533 s = pd.Series([1, 2, 3, 4], index=list('ABCD')) s[lambda x: 'A'] = -1 tm.assert_series_equal(s, pd.Series([-1, 2, 3, 4], index=list('ABCD'))) def test_setitem_other_callable(self): # GH 13299 inc = lambda x: x + 1 s = pd.Series([1, 2, -1, 4]) s[s < 0] = inc expected = pd.Series([1, 2, inc, 4]) tm.assert_series_equal(s, expected) def test_slice(self): numSlice = self.series[10:20] numSliceEnd = self.series[-10:] objSlice = self.objSeries[10:20] assert self.series.index[9] not in numSlice.index assert self.objSeries.index[9] not in objSlice.index assert len(numSlice) == len(numSlice.index) assert self.series[numSlice.index[0]] == numSlice[numSlice.index[0]] assert numSlice.index[1] == self.series.index[11] assert tm.equalContents(numSliceEnd, np.array(self.series)[-10:]) # Test return view. sl = self.series[10:20] sl[:] = 0 assert (self.series[10:20] == 0).all() def test_slice_can_reorder_not_uniquely_indexed(self): s = Series(1, index=['a', 'a', 'b', 'b', 'c']) s[::-1] # it works! def test_slice_float_get_set(self): pytest.raises(TypeError, lambda: self.ts[4.0:10.0]) def f(): self.ts[4.0:10.0] = 0 pytest.raises(TypeError, f) pytest.raises(TypeError, self.ts.__getitem__, slice(4.5, 10.0)) pytest.raises(TypeError, self.ts.__setitem__, slice(4.5, 10.0), 0) def test_slice_floats2(self): s = Series(np.random.rand(10), index=np.arange(10, 20, dtype=float)) assert len(s.loc[12.0:]) == 8 assert len(s.loc[12.5:]) == 7 i = np.arange(10, 20, dtype=float) i[2] = 12.2 s.index = i assert len(s.loc[12.0:]) == 8 assert len(s.loc[12.5:]) == 7 def test_slice_float64(self): values = np.arange(10., 50., 2) index = Index(values) start, end = values[[5, 15]] s = Series(np.random.randn(20), index=index) result = s[start:end] expected = s.iloc[5:16] assert_series_equal(result, expected) result = s.loc[start:end] assert_series_equal(result, expected) df = DataFrame(np.random.randn(20, 3), index=index) result = df[start:end] expected = df.iloc[5:16] tm.assert_frame_equal(result, expected) result = df.loc[start:end] tm.assert_frame_equal(result, expected) def test_setitem(self): self.ts[self.ts.index[5]] = np.NaN self.ts[[1, 2, 17]] = np.NaN self.ts[6] = np.NaN assert np.isnan(self.ts[6]) assert np.isnan(self.ts[2]) self.ts[np.isnan(self.ts)] = 5 assert not np.isnan(self.ts[2]) # caught this bug when writing tests series = Series(tm.makeIntIndex(20).astype(float), index=tm.makeIntIndex(20)) series[::2] = 0 assert (series[::2] == 0).all() # set item that's not contained s = self.series.copy() s['foobar'] = 1 app = Series([1], index=['foobar'], name='series') expected = self.series.append(app) assert_series_equal(s, expected) # Test for issue #10193 key = pd.Timestamp('2012-01-01') series = pd.Series() series[key] = 47 expected = pd.Series(47, [key]) assert_series_equal(series, expected) series = pd.Series([], pd.DatetimeIndex([], freq='D')) series[key] = 47 expected = pd.Series(47, pd.DatetimeIndex([key], freq='D')) assert_series_equal(series, expected) def test_setitem_dtypes(self): # change dtypes # GH 4463 expected = Series([np.nan, 2, 3]) s = Series([1, 2, 3]) s.iloc[0] = np.nan assert_series_equal(s, expected) s = Series([1, 2, 3]) s.loc[0] = np.nan assert_series_equal(s, expected) s = Series([1, 2, 3]) s[0] = np.nan assert_series_equal(s, expected) s = Series([False]) s.loc[0] = np.nan assert_series_equal(s, Series([np.nan])) s = Series([False, True]) s.loc[0] = np.nan assert_series_equal(s, Series([np.nan, 1.0])) def test_set_value(self): idx = self.ts.index[10] res = self.ts.set_value(idx, 0) assert res is self.ts assert self.ts[idx] == 0 # equiv s = self.series.copy() res = s.set_value('foobar', 0) assert res is s assert res.index[-1] == 'foobar' assert res['foobar'] == 0 s = self.series.copy() s.loc['foobar'] = 0 assert s.index[-1] == 'foobar' assert s['foobar'] == 0 def test_setslice(self): sl = self.ts[5:20] assert len(sl) == len(sl.index) assert sl.index.is_unique def test_basic_getitem_setitem_corner(self): # invalid tuples, e.g. self.ts[:, None] vs. self.ts[:, 2] with tm.assert_raises_regex(ValueError, 'tuple-index'): self.ts[:, 2] with tm.assert_raises_regex(ValueError, 'tuple-index'): self.ts[:, 2] = 2 # weird lists. [slice(0, 5)] will work but not two slices result = self.ts[[slice(None, 5)]] expected = self.ts[:5] assert_series_equal(result, expected) # OK pytest.raises(Exception, self.ts.__getitem__, [5, slice(None, None)]) pytest.raises(Exception, self.ts.__setitem__, [5, slice(None, None)], 2) def test_basic_getitem_with_labels(self): indices = self.ts.index[[5, 10, 15]] result = self.ts[indices] expected = self.ts.reindex(indices) assert_series_equal(result, expected) result = self.ts[indices[0]:indices[2]] expected = self.ts.loc[indices[0]:indices[2]] assert_series_equal(result, expected) # integer indexes, be careful s = Series(np.random.randn(10), index=lrange(0, 20, 2)) inds = [0, 2, 5, 7, 8] arr_inds = np.array([0, 2, 5, 7, 8]) result = s[inds] expected = s.reindex(inds) assert_series_equal(result, expected) result = s[arr_inds] expected = s.reindex(arr_inds) assert_series_equal(result, expected) # GH12089 # with tz for values s = Series(pd.date_range("2011-01-01", periods=3, tz="US/Eastern"), index=['a', 'b', 'c']) expected = Timestamp('2011-01-01', tz='US/Eastern') result = s.loc['a'] assert result == expected result = s.iloc[0] assert result == expected result = s['a'] assert result == expected def test_basic_setitem_with_labels(self): indices = self.ts.index[[5, 10, 15]] cp = self.ts.copy() exp = self.ts.copy() cp[indices] = 0 exp.loc[indices] = 0 assert_series_equal(cp, exp) cp = self.ts.copy() exp = self.ts.copy() cp[indices[0]:indices[2]] = 0 exp.loc[indices[0]:indices[2]] = 0 assert_series_equal(cp, exp) # integer indexes, be careful s = Series(np.random.randn(10), index=lrange(0, 20, 2)) inds = [0, 4, 6] arr_inds = np.array([0, 4, 6]) cp = s.copy() exp = s.copy() s[inds] = 0 s.loc[inds] = 0 assert_series_equal(cp, exp) cp = s.copy() exp = s.copy() s[arr_inds] = 0 s.loc[arr_inds] = 0 assert_series_equal(cp, exp) inds_notfound = [0, 4, 5, 6] arr_inds_notfound = np.array([0, 4, 5, 6]) pytest.raises(Exception, s.__setitem__, inds_notfound, 0) pytest.raises(Exception, s.__setitem__, arr_inds_notfound, 0) # GH12089 # with tz for values s = Series(pd.date_range("2011-01-01", periods=3, tz="US/Eastern"), index=['a', 'b', 'c']) s2 = s.copy() expected = Timestamp('2011-01-03', tz='US/Eastern') s2.loc['a'] = expected result = s2.loc['a'] assert result == expected s2 = s.copy() s2.iloc[0] = expected result = s2.iloc[0] assert result == expected s2 = s.copy() s2['a'] = expected result = s2['a'] assert result == expected def test_loc_getitem(self): inds = self.series.index[[3, 4, 7]] assert_series_equal(self.series.loc[inds], self.series.reindex(inds)) assert_series_equal(self.series.iloc[5::2], self.series[5::2]) # slice with indices d1, d2 = self.ts.index[[5, 15]] result = self.ts.loc[d1:d2] expected = self.ts.truncate(d1, d2) assert_series_equal(result, expected) # boolean mask = self.series > self.series.median() assert_series_equal(self.series.loc[mask], self.series[mask]) # ask for index value assert self.ts.loc[d1] == self.ts[d1] assert self.ts.loc[d2] == self.ts[d2] def test_loc_getitem_not_monotonic(self): d1, d2 = self.ts.index[[5, 15]] ts2 = self.ts[::2][[1, 2, 0]] pytest.raises(KeyError, ts2.loc.__getitem__, slice(d1, d2)) pytest.raises(KeyError, ts2.loc.__setitem__, slice(d1, d2), 0) def test_loc_getitem_setitem_integer_slice_keyerrors(self): s = Series(np.random.randn(10), index=lrange(0, 20, 2)) # this is OK cp = s.copy() cp.iloc[4:10] = 0 assert (cp.iloc[4:10] == 0).all() # so is this cp = s.copy() cp.iloc[3:11] = 0 assert (cp.iloc[3:11] == 0).values.all() result = s.iloc[2:6] result2 = s.loc[3:11] expected = s.reindex([4, 6, 8, 10]) assert_series_equal(result, expected) assert_series_equal(result2, expected) # non-monotonic, raise KeyError s2 = s.iloc[lrange(5) + lrange(5, 10)[::-1]] pytest.raises(KeyError, s2.loc.__getitem__, slice(3, 11)) pytest.raises(KeyError, s2.loc.__setitem__, slice(3, 11), 0) def test_loc_getitem_iterator(self): idx = iter(self.series.index[:10]) result = self.series.loc[idx] assert_series_equal(result, self.series[:10]) def test_setitem_with_tz(self): for tz in ['US/Eastern', 'UTC', 'Asia/Tokyo']: orig = pd.Series(pd.date_range('2016-01-01', freq='H', periods=3, tz=tz)) assert orig.dtype == 'datetime64[ns, {0}]'.format(tz) # scalar s = orig.copy() s[1] = pd.Timestamp('2011-01-01', tz=tz) exp = pd.Series([pd.Timestamp('2016-01-01 00:00', tz=tz), pd.Timestamp('2011-01-01 00:00', tz=tz), pd.Timestamp('2016-01-01 02:00', tz=tz)]) tm.assert_series_equal(s, exp) s = orig.copy() s.loc[1] = pd.Timestamp('2011-01-01', tz=tz) tm.assert_series_equal(s, exp) s = orig.copy() s.iloc[1] = pd.Timestamp('2011-01-01', tz=tz) tm.assert_series_equal(s, exp) # vector vals = pd.Series([pd.Timestamp('2011-01-01', tz=tz), pd.Timestamp('2012-01-01', tz=tz)], index=[1, 2]) assert vals.dtype == 'datetime64[ns, {0}]'.format(tz) s[[1, 2]] = vals exp = pd.Series([pd.Timestamp('2016-01-01 00:00', tz=tz), pd.Timestamp('2011-01-01 00:00', tz=tz), pd.Timestamp('2012-01-01 00:00', tz=tz)]) tm.assert_series_equal(s, exp) s = orig.copy() s.loc[[1, 2]] = vals tm.assert_series_equal(s, exp) s = orig.copy() s.iloc[[1, 2]] = vals tm.assert_series_equal(s, exp) def test_setitem_with_tz_dst(self): # GH XXX tz = 'US/Eastern' orig = pd.Series(pd.date_range('2016-11-06', freq='H', periods=3, tz=tz)) assert orig.dtype == 'datetime64[ns, {0}]'.format(tz) # scalar s = orig.copy() s[1] = pd.Timestamp('2011-01-01', tz=tz) exp = pd.Series([pd.Timestamp('2016-11-06 00:00-04:00', tz=tz), pd.Timestamp('2011-01-01 00:00-05:00', tz=tz), pd.Timestamp('2016-11-06 01:00-05:00', tz=tz)]) tm.assert_series_equal(s, exp) s = orig.copy() s.loc[1] = pd.Timestamp('2011-01-01', tz=tz) tm.assert_series_equal(s, exp) s = orig.copy() s.iloc[1] = pd.Timestamp('2011-01-01', tz=tz) tm.assert_series_equal(s, exp) # vector vals = pd.Series([pd.Timestamp('2011-01-01', tz=tz), pd.Timestamp('2012-01-01', tz=tz)], index=[1, 2]) assert vals.dtype == 'datetime64[ns, {0}]'.format(tz) s[[1, 2]] = vals exp = pd.Series([pd.Timestamp('2016-11-06 00:00', tz=tz), pd.Timestamp('2011-01-01 00:00', tz=tz), pd.Timestamp('2012-01-01 00:00', tz=tz)]) tm.assert_series_equal(s, exp) s = orig.copy() s.loc[[1, 2]] = vals tm.assert_series_equal(s, exp) s = orig.copy() s.iloc[[1, 2]] = vals tm.assert_series_equal(s, exp) def test_where(self): s = Series(np.random.randn(5)) cond = s > 0 rs = s.where(cond).dropna() rs2 = s[cond] assert_series_equal(rs, rs2) rs = s.where(cond, -s) assert_series_equal(rs, s.abs()) rs = s.where(cond) assert (s.shape == rs.shape) assert (rs is not s) # test alignment cond = Series([True, False, False, True, False], index=s.index) s2 = -(s.abs()) expected = s2[cond].reindex(s2.index[:3]).reindex(s2.index) rs = s2.where(cond[:3]) assert_series_equal(rs, expected) expected = s2.abs() expected.iloc[0] = s2[0] rs = s2.where(cond[:3], -s2) assert_series_equal(rs, expected) pytest.raises(ValueError, s.where, 1) pytest.raises(ValueError, s.where, cond[:3].values, -s) # GH 2745 s = Series([1, 2]) s[[True, False]] = [0, 1] expected = Series([0, 2]) assert_series_equal(s, expected) # failures pytest.raises(ValueError, s.__setitem__, tuple([[[True, False]]]), [0, 2, 3]) pytest.raises(ValueError, s.__setitem__, tuple([[[True, False]]]), []) # unsafe dtype changes for dtype in [np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64]: s = Series(np.arange(10), dtype=dtype) mask = s < 5 s[mask] = lrange(2, 7) expected = Series(lrange(2, 7) + lrange(5, 10), dtype=dtype) assert_series_equal(s, expected) assert s.dtype == expected.dtype # these are allowed operations, but are upcasted for dtype in [np.int64, np.float64]: s = Series(np.arange(10), dtype=dtype) mask = s < 5 values = [2.5, 3.5, 4.5, 5.5, 6.5] s[mask] = values expected = Series(values + lrange(5, 10), dtype='float64') assert_series_equal(s, expected) assert s.dtype == expected.dtype # GH 9731 s = Series(np.arange(10), dtype='int64') mask = s > 5 values = [2.5, 3.5, 4.5, 5.5] s[mask] = values expected = Series(lrange(6) + values, dtype='float64') assert_series_equal(s, expected) # can't do these as we are forced to change the itemsize of the input # to something we cannot for dtype in [np.int8, np.int16, np.int32, np.float16, np.float32]: s = Series(np.arange(10), dtype=dtype) mask = s < 5 values = [2.5, 3.5, 4.5, 5.5, 6.5] pytest.raises(Exception, s.__setitem__, tuple(mask), values) # GH3235 s = Series(np.arange(10), dtype='int64') mask = s < 5 s[mask] = lrange(2, 7) expected = Series(lrange(2, 7) + lrange(5, 10), dtype='int64') assert_series_equal(s, expected) assert s.dtype == expected.dtype s = Series(np.arange(10), dtype='int64') mask = s > 5 s[mask] = [0] * 4 expected = Series([0, 1, 2, 3, 4, 5] + [0] * 4, dtype='int64') assert_series_equal(s, expected) s = Series(np.arange(10)) mask = s > 5 def f(): s[mask] = [5, 4, 3, 2, 1] pytest.raises(ValueError, f) def f(): s[mask] = [0] * 5 pytest.raises(ValueError, f) # dtype changes s = Series([1, 2, 3, 4]) result = s.where(s > 2, np.nan) expected = Series([np.nan, np.nan, 3, 4]) assert_series_equal(result, expected) # GH 4667 # setting with None changes dtype s = Series(range(10)).astype(float) s[8] = None result = s[8] assert isnull(result) s = Series(range(10)).astype(float) s[s > 8] = None result = s[isnull(s)] expected = Series(np.nan, index=[9]) assert_series_equal(result, expected) def test_where_array_like(self): # see gh-15414 s = Series([1, 2, 3]) cond = [False, True, True] expected = Series([np.nan, 2, 3]) klasses = [list, tuple, np.array, Series] for klass in klasses: result = s.where(klass(cond)) assert_series_equal(result, expected) def test_where_invalid_input(self): # see gh-15414: only boolean arrays accepted s = Series([1, 2, 3]) msg = "Boolean array expected for the condition" conds = [ [1, 0, 1], Series([2, 5, 7]), ["True", "False", "True"], [Timestamp("2017-01-01"), pd.NaT, Timestamp("2017-01-02")] ] for cond in conds: with tm.assert_raises_regex(ValueError, msg): s.where(cond) msg = "Array conditional must be same shape as self" with tm.assert_raises_regex(ValueError, msg): s.where([True]) def test_where_ndframe_align(self): msg = "Array conditional must be same shape as self" s = Series([1, 2, 3]) cond = [True] with tm.assert_raises_regex(ValueError, msg): s.where(cond) expected = Series([1, np.nan, np.nan]) out = s.where(Series(cond)) tm.assert_series_equal(out, expected) cond = np.array([False, True, False, True]) with tm.assert_raises_regex(ValueError, msg): s.where(cond) expected = Series([np.nan, 2, np.nan]) out = s.where(Series(cond)) tm.assert_series_equal(out, expected) def test_where_setitem_invalid(self): # GH 2702 # make sure correct exceptions are raised on invalid list assignment # slice s = Series(list('abc')) def f(): s[0:3] = list(range(27)) pytest.raises(ValueError, f) s[0:3] = list(range(3)) expected = Series([0, 1, 2]) assert_series_equal(s.astype(np.int64), expected, ) # slice with step s = Series(list('abcdef')) def f(): s[0:4:2] = list(range(27)) pytest.raises(ValueError, f) s = Series(list('abcdef')) s[0:4:2] = list(range(2)) expected = Series([0, 'b', 1, 'd', 'e', 'f']) assert_series_equal(s, expected) # neg slices s = Series(list('abcdef')) def f(): s[:-1] = list(range(27)) pytest.raises(ValueError, f) s[-3:-1] = list(range(2)) expected = Series(['a', 'b', 'c', 0, 1, 'f']) assert_series_equal(s, expected) # list s = Series(list('abc')) def f(): s[[0, 1, 2]] = list(range(27)) pytest.raises(ValueError, f) s = Series(list('abc')) def f(): s[[0, 1, 2]] = list(range(2)) pytest.raises(ValueError, f) # scalar s = Series(list('abc')) s[0] = list(range(10)) expected = Series([list(range(10)), 'b', 'c']) assert_series_equal(s, expected) def test_where_broadcast(self): # Test a variety of differently sized series for size in range(2, 6): # Test a variety of boolean indices for selection in [ # First element should be set np.resize([True, False, False, False, False], size), # Set alternating elements] np.resize([True, False], size), # No element should be set np.resize([False], size)]: # Test a variety of different numbers as content for item in [2.0, np.nan, np.finfo(np.float).max, np.finfo(np.float).min]: # Test numpy arrays, lists and tuples as the input to be # broadcast for arr in [np.array([item]), [item], (item, )]: data = np.arange(size, dtype=float) s = Series(data) s[selection] = arr # Construct the expected series by taking the source # data or item based on the selection expected = Series([item if use_item else data[ i] for i, use_item in enumerate(selection)]) assert_series_equal(s, expected) s = Series(data) result = s.where(~selection, arr) assert_series_equal(result, expected) def test_where_inplace(self): s = Series(np.random.randn(5)) cond = s > 0 rs = s.copy() rs.where(cond, inplace=True) assert_series_equal(rs.dropna(), s[cond]) assert_series_equal(rs, s.where(cond)) rs = s.copy() rs.where(cond, -s, inplace=True) assert_series_equal(rs, s.where(cond, -s)) def test_where_dups(self): # GH 4550 # where crashes with dups in index s1 = Series(list(range(3))) s2 = Series(list(range(3))) comb = pd.concat([s1, s2]) result = comb.where(comb < 2) expected = Series([0, 1, np.nan, 0, 1, np.nan], index=[0, 1, 2, 0, 1, 2]) assert_series_equal(result, expected) # GH 4548 # inplace updating not working with dups comb[comb < 1] = 5 expected = Series([5, 1, 2, 5, 1, 2], index=[0, 1, 2, 0, 1, 2]) assert_series_equal(comb, expected) comb[comb < 2] += 10 expected = Series([5, 11, 2, 5, 11, 2], index=[0, 1, 2, 0, 1, 2]) assert_series_equal(comb, expected) def test_where_datetime(self): s = Series(date_range('20130102', periods=2)) expected = Series([10, 10], dtype='datetime64[ns]') mask = np.array([False, False]) rs = s.where(mask, [10, 10]) assert_series_equal(rs, expected) rs = s.where(mask, 10) assert_series_equal(rs, expected) rs = s.where(mask, 10.0) assert_series_equal(rs, expected) rs = s.where(mask, [10.0, 10.0]) assert_series_equal(rs, expected) rs = s.where(mask, [10.0, np.nan]) expected = Series([10, None], dtype='datetime64[ns]') assert_series_equal(rs, expected) # GH 15701 timestamps = ['2016-12-31 12:00:04+00:00', '2016-12-31 12:00:04.010000+00:00'] s = Series([pd.Timestamp(t) for t in timestamps]) rs = s.where(Series([False, True])) expected = Series([pd.NaT, s[1]]) assert_series_equal(rs, expected) def test_where_timedelta(self): s = Series([1, 2], dtype='timedelta64[ns]') expected = Series([10, 10], dtype='timedelta64[ns]') mask = np.array([False, False]) rs = s.where(mask, [10, 10]) assert_series_equal(rs, expected) rs = s.where(mask, 10) assert_series_equal(rs, expected) rs = s.where(mask, 10.0) assert_series_equal(rs, expected) rs = s.where(mask, [10.0, 10.0]) assert_series_equal(rs, expected) rs = s.where(mask, [10.0, np.nan]) expected = Series([10, None], dtype='timedelta64[ns]') assert_series_equal(rs, expected) def test_mask(self): # compare with tested results in test_where s = Series(np.random.randn(5)) cond = s > 0 rs = s.where(~cond, np.nan) assert_series_equal(rs, s.mask(cond)) rs = s.where(~cond) rs2 = s.mask(cond) assert_series_equal(rs, rs2) rs = s.where(~cond, -s) rs2 = s.mask(cond, -s) assert_series_equal(rs, rs2) cond = Series([True, False, False, True, False], index=s.index) s2 = -(s.abs()) rs = s2.where(~cond[:3]) rs2 = s2.mask(cond[:3]) assert_series_equal(rs, rs2) rs = s2.where(~cond[:3], -s2) rs2 = s2.mask(cond[:3], -s2) assert_series_equal(rs, rs2) pytest.raises(ValueError, s.mask, 1) pytest.raises(ValueError, s.mask, cond[:3].values, -s) # dtype changes s = Series([1, 2, 3, 4]) result = s.mask(s > 2, np.nan) expected = Series([1, 2, np.nan, np.nan]) assert_series_equal(result, expected) def test_mask_broadcast(self): # GH 8801 # copied from test_where_broadcast for size in range(2, 6): for selection in [ # First element should be set np.resize([True, False, False, False, False], size), # Set alternating elements] np.resize([True, False], size), # No element should be set np.resize([False], size)]: for item in [2.0, np.nan, np.finfo(np.float).max, np.finfo(np.float).min]: for arr in [np.array([item]), [item], (item, )]: data = np.arange(size, dtype=float) s = Series(data) result = s.mask(selection, arr) expected = Series([item if use_item else data[ i] for i, use_item in enumerate(selection)]) assert_series_equal(result, expected) def test_mask_inplace(self): s = Series(np.random.randn(5)) cond = s > 0 rs = s.copy() rs.mask(cond, inplace=True) assert_series_equal(rs.dropna(), s[~cond]) assert_series_equal(rs, s.mask(cond)) rs = s.copy() rs.mask(cond, -s, inplace=True) assert_series_equal(rs, s.mask(cond, -s)) def test_ix_setitem(self): inds = self.series.index[[3, 4, 7]] result = self.series.copy() result.loc[inds] = 5 expected = self.series.copy() expected[[3, 4, 7]] = 5 assert_series_equal(result, expected) result.iloc[5:10] = 10 expected[5:10] = 10 assert_series_equal(result, expected) # set slice with indices d1, d2 = self.series.index[[5, 15]] result.loc[d1:d2] = 6 expected[5:16] = 6 # because it's inclusive assert_series_equal(result, expected) # set index value self.series.loc[d1] = 4 self.series.loc[d2] = 6 assert self.series[d1] == 4 assert self.series[d2] == 6 def test_where_numeric_with_string(self): # GH 9280 s = pd.Series([1, 2, 3]) w = s.where(s > 1, 'X') assert not is_integer(w[0]) assert is_integer(w[1]) assert is_integer(w[2]) assert isinstance(w[0], str) assert w.dtype == 'object' w = s.where(s > 1, ['X', 'Y', 'Z']) assert not is_integer(w[0]) assert is_integer(w[1]) assert is_integer(w[2]) assert isinstance(w[0], str) assert w.dtype == 'object' w = s.where(s > 1, np.array(['X', 'Y', 'Z'])) assert not is_integer(w[0]) assert is_integer(w[1]) assert is_integer(w[2]) assert isinstance(w[0], str) assert w.dtype == 'object' def test_setitem_boolean(self): mask = self.series > self.series.median() # similiar indexed series result = self.series.copy() result[mask] = self.series * 2 expected = self.series * 2 assert_series_equal(result[mask], expected[mask]) # needs alignment result = self.series.copy() result[mask] = (self.series * 2)[0:5] expected = (self.series * 2)[0:5].reindex_like(self.series) expected[-mask] = self.series[mask] assert_series_equal(result[mask], expected[mask]) def test_ix_setitem_boolean(self): mask = self.series > self.series.median() result = self.series.copy() result.loc[mask] = 0 expected = self.series expected[mask] = 0 assert_series_equal(result, expected) def test_ix_setitem_corner(self): inds = list(self.series.index[[5, 8, 12]]) self.series.loc[inds] = 5 pytest.raises(Exception, self.series.loc.__setitem__, inds + ['foo'], 5) def test_get_set_boolean_different_order(self): ordered = self.series.sort_values() # setting copy = self.series.copy() copy[ordered > 0] = 0 expected = self.series.copy() expected[expected > 0] = 0 assert_series_equal(copy, expected) # getting sel = self.series[ordered > 0] exp = self.series[self.series > 0] assert_series_equal(sel, exp) def test_setitem_na(self): # these induce dtype changes expected = Series([np.nan, 3, np.nan, 5, np.nan, 7, np.nan, 9, np.nan]) s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10]) s[::2] = np.nan assert_series_equal(s, expected) # get's coerced to float, right? expected = Series([np.nan, 1, np.nan, 0]) s = Series([True, True, False, False]) s[::2] = np.nan assert_series_equal(s, expected) expected = Series([np.nan, np.nan, np.nan, np.nan, np.nan, 5, 6, 7, 8, 9]) s = Series(np.arange(10)) s[:5] = np.nan assert_series_equal(s, expected) def test_basic_indexing(self): s = Series(np.random.randn(5), index=['a', 'b', 'a', 'a', 'b']) pytest.raises(IndexError, s.__getitem__, 5) pytest.raises(IndexError, s.__setitem__, 5, 0) pytest.raises(KeyError, s.__getitem__, 'c') s = s.sort_index() pytest.raises(IndexError, s.__getitem__, 5) pytest.raises(IndexError, s.__setitem__, 5, 0) def test_int_indexing(self): s = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2]) pytest.raises(KeyError, s.__getitem__, 5) pytest.raises(KeyError, s.__getitem__, 'c') # not monotonic s = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1]) pytest.raises(KeyError, s.__getitem__, 5) pytest.raises(KeyError, s.__getitem__, 'c') def test_datetime_indexing(self): from pandas import date_range index = date_range('1/1/2000', '1/7/2000') index = index.repeat(3) s = Series(len(index), index=index) stamp = Timestamp('1/8/2000') pytest.raises(KeyError, s.__getitem__, stamp) s[stamp] = 0 assert s[stamp] == 0 # not monotonic s = Series(len(index), index=index) s = s[::-1] pytest.raises(KeyError, s.__getitem__, stamp) s[stamp] = 0 assert s[stamp] == 0 def test_timedelta_assignment(self): # GH 8209 s = Series([]) s.loc['B'] = timedelta(1) tm.assert_series_equal(s, Series(Timedelta('1 days'), index=['B'])) s = s.reindex(s.index.insert(0, 'A')) tm.assert_series_equal(s, Series( [np.nan, Timedelta('1 days')], index=['A', 'B'])) result = s.fillna(timedelta(1)) expected = Series(Timedelta('1 days'), index=['A', 'B']) tm.assert_series_equal(result, expected) s.loc['A'] = timedelta(1) tm.assert_series_equal(s, expected) # GH 14155 s = Series(10 * [np.timedelta64(10, 'm')]) s.loc[[1, 2, 3]] = np.timedelta64(20, 'm') expected = pd.Series(10 * [np.timedelta64(10, 'm')]) expected.loc[[1, 2, 3]] = pd.Timedelta(np.timedelta64(20, 'm')) tm.assert_series_equal(s, expected) def test_underlying_data_conversion(self): # GH 4080 df = DataFrame(dict((c, [1, 2, 3]) for c in ['a', 'b', 'c'])) df.set_index(['a', 'b', 'c'], inplace=True) s = Series([1], index=[(2, 2, 2)]) df['val'] = 0 df df['val'].update(s) expected = DataFrame( dict(a=[1, 2, 3], b=[1, 2, 3], c=[1, 2, 3], val=[0, 1, 0])) expected.set_index(['a', 'b', 'c'], inplace=True) tm.assert_frame_equal(df, expected) # GH 3970 # these are chained assignments as well pd.set_option('chained_assignment', None) df = DataFrame({"aa": range(5), "bb": [2.2] * 5}) df["cc"] = 0.0 ck = [True] * len(df) df["bb"].iloc[0] = .13 # TODO: unused df_tmp = df.iloc[ck] # noqa df["bb"].iloc[0] = .15 assert df['bb'].iloc[0] == 0.15 pd.set_option('chained_assignment', 'raise') # GH 3217 df = DataFrame(dict(a=[1, 3], b=[np.nan, 2])) df['c'] = np.nan df['c'].update(pd.Series(['foo'], index=[0])) expected = DataFrame(dict(a=[1, 3], b=[np.nan, 2], c=['foo', np.nan])) tm.assert_frame_equal(df, expected) def test_preserveRefs(self): seq = self.ts[[5, 10, 15]] seq[1] = np.NaN assert not np.isnan(self.ts[10]) def test_drop(self): # unique s = Series([1, 2], index=['one', 'two']) expected = Series([1], index=['one']) result = s.drop(['two']) assert_series_equal(result, expected) result = s.drop('two', axis='rows') assert_series_equal(result, expected) # non-unique # GH 5248 s = Series([1, 1, 2], index=['one', 'two', 'one']) expected = Series([1, 2], index=['one', 'one']) result = s.drop(['two'], axis=0) assert_series_equal(result, expected) result = s.drop('two') assert_series_equal(result, expected) expected = Series([1], index=['two']) result = s.drop(['one']) assert_series_equal(result, expected) result = s.drop('one') assert_series_equal(result, expected) # single string/tuple-like s = Series(range(3), index=list('abc')) pytest.raises(ValueError, s.drop, 'bc') pytest.raises(ValueError, s.drop, ('a', )) # errors='ignore' s = Series(range(3), index=list('abc')) result = s.drop('bc', errors='ignore') assert_series_equal(result, s) result = s.drop(['a', 'd'], errors='ignore') expected = s.iloc[1:] assert_series_equal(result, expected) # bad axis pytest.raises(ValueError, s.drop, 'one', axis='columns') # GH 8522 s = Series([2, 3], index=[True, False]) assert s.index.is_object() result = s.drop(True) expected = Series([3], index=[False]) assert_series_equal(result, expected) def test_align(self): def _check_align(a, b, how='left', fill=None): aa, ab = a.align(b, join=how, fill_value=fill) join_index = a.index.join(b.index, how=how) if fill is not None: diff_a = aa.index.difference(join_index) diff_b = ab.index.difference(join_index) if len(diff_a) > 0: assert (aa.reindex(diff_a) == fill).all() if len(diff_b) > 0: assert (ab.reindex(diff_b) == fill).all() ea = a.reindex(join_index) eb = b.reindex(join_index) if fill is not None: ea = ea.fillna(fill) eb = eb.fillna(fill) assert_series_equal(aa, ea) assert_series_equal(ab, eb) assert aa.name == 'ts' assert ea.name == 'ts' assert ab.name == 'ts' assert eb.name == 'ts' for kind in JOIN_TYPES: _check_align(self.ts[2:], self.ts[:-5], how=kind) _check_align(self.ts[2:], self.ts[:-5], how=kind, fill=-1) # empty left _check_align(self.ts[:0], self.ts[:-5], how=kind) _check_align(self.ts[:0], self.ts[:-5], how=kind, fill=-1) # empty right _check_align(self.ts[:-5], self.ts[:0], how=kind) _check_align(self.ts[:-5], self.ts[:0], how=kind, fill=-1) # both empty _check_align(self.ts[:0], self.ts[:0], how=kind) _check_align(self.ts[:0], self.ts[:0], how=kind, fill=-1) def test_align_fill_method(self): def _check_align(a, b, how='left', method='pad', limit=None): aa, ab = a.align(b, join=how, method=method, limit=limit) join_index = a.index.join(b.index, how=how) ea = a.reindex(join_index) eb = b.reindex(join_index) ea = ea.fillna(method=method, limit=limit) eb = eb.fillna(method=method, limit=limit) assert_series_equal(aa, ea) assert_series_equal(ab, eb) for kind in JOIN_TYPES: for meth in ['pad', 'bfill']: _check_align(self.ts[2:], self.ts[:-5], how=kind, method=meth) _check_align(self.ts[2:], self.ts[:-5], how=kind, method=meth, limit=1) # empty left _check_align(self.ts[:0], self.ts[:-5], how=kind, method=meth) _check_align(self.ts[:0], self.ts[:-5], how=kind, method=meth, limit=1) # empty right _check_align(self.ts[:-5], self.ts[:0], how=kind, method=meth) _check_align(self.ts[:-5], self.ts[:0], how=kind, method=meth, limit=1) # both empty _check_align(self.ts[:0], self.ts[:0], how=kind, method=meth) _check_align(self.ts[:0], self.ts[:0], how=kind, method=meth, limit=1) def test_align_nocopy(self): b = self.ts[:5].copy() # do copy a = self.ts.copy() ra, _ = a.align(b, join='left') ra[:5] = 5 assert not (a[:5] == 5).any() # do not copy a = self.ts.copy() ra, _ = a.align(b, join='left', copy=False) ra[:5] = 5 assert (a[:5] == 5).all() # do copy a = self.ts.copy() b = self.ts[:5].copy() _, rb = a.align(b, join='right') rb[:3] = 5 assert not (b[:3] == 5).any() # do not copy a = self.ts.copy() b = self.ts[:5].copy() _, rb = a.align(b, join='right', copy=False) rb[:2] = 5 assert (b[:2] == 5).all() def test_align_same_index(self): a, b = self.ts.align(self.ts, copy=False) assert a.index is self.ts.index assert b.index is self.ts.index a, b = self.ts.align(self.ts, copy=True) assert a.index is not self.ts.index assert b.index is not self.ts.index def test_align_multiindex(self): # GH 10665 midx = pd.MultiIndex.from_product([range(2), range(3), range(2)], names=('a', 'b', 'c')) idx = pd.Index(range(2), name='b') s1 = pd.Series(np.arange(12, dtype='int64'), index=midx) s2 = pd.Series(np.arange(2, dtype='int64'), index=idx) # these must be the same results (but flipped) res1l, res1r = s1.align(s2, join='left') res2l, res2r = s2.align(s1, join='right') expl = s1 tm.assert_series_equal(expl, res1l) tm.assert_series_equal(expl, res2r) expr = pd.Series([0, 0, 1, 1, np.nan, np.nan] * 2, index=midx) tm.assert_series_equal(expr, res1r) tm.assert_series_equal(expr, res2l) res1l, res1r = s1.align(s2, join='right') res2l, res2r = s2.align(s1, join='left') exp_idx = pd.MultiIndex.from_product([range(2), range(2), range(2)], names=('a', 'b', 'c')) expl = pd.Series([0, 1, 2, 3, 6, 7, 8, 9], index=exp_idx) tm.assert_series_equal(expl, res1l) tm.assert_series_equal(expl, res2r) expr = pd.Series([0, 0, 1, 1] * 2, index=exp_idx) tm.assert_series_equal(expr, res1r) tm.assert_series_equal(expr, res2l) def test_reindex(self): identity = self.series.reindex(self.series.index) # __array_interface__ is not defined for older numpies # and on some pythons try: assert np.may_share_memory(self.series.index, identity.index) except AttributeError: pass assert identity.index.is_(self.series.index) assert identity.index.identical(self.series.index) subIndex = self.series.index[10:20] subSeries = self.series.reindex(subIndex) for idx, val in compat.iteritems(subSeries): assert val == self.series[idx] subIndex2 = self.ts.index[10:20] subTS = self.ts.reindex(subIndex2) for idx, val in compat.iteritems(subTS): assert val == self.ts[idx] stuffSeries = self.ts.reindex(subIndex) assert np.isnan(stuffSeries).all() # This is extremely important for the Cython code to not screw up nonContigIndex = self.ts.index[::2] subNonContig = self.ts.reindex(nonContigIndex) for idx, val in compat.iteritems(subNonContig): assert val == self.ts[idx] # return a copy the same index here result = self.ts.reindex() assert not (result is self.ts) def test_reindex_nan(self): ts = Series([2, 3, 5, 7], index=[1, 4, nan, 8]) i, j = [nan, 1, nan, 8, 4, nan], [2, 0, 2, 3, 1, 2] assert_series_equal(ts.reindex(i), ts.iloc[j]) ts.index = ts.index.astype('object') # reindex coerces index.dtype to float, loc/iloc doesn't assert_series_equal(ts.reindex(i), ts.iloc[j], check_index_type=False) def test_reindex_series_add_nat(self): rng = date_range('1/1/2000 00:00:00', periods=10, freq='10s') series = Series(rng) result = series.reindex(lrange(15)) assert np.issubdtype(result.dtype, np.dtype('M8[ns]')) mask = result.isnull() assert mask[-5:].all() assert not mask[:-5].any() def test_reindex_with_datetimes(self): rng = date_range('1/1/2000', periods=20) ts = Series(np.random.randn(20), index=rng) result = ts.reindex(list(ts.index[5:10])) expected = ts[5:10] tm.assert_series_equal(result, expected) result = ts[list(ts.index[5:10])] tm.assert_series_equal(result, expected) def test_reindex_corner(self): # (don't forget to fix this) I think it's fixed self.empty.reindex(self.ts.index, method='pad') # it works # corner case: pad empty series reindexed = self.empty.reindex(self.ts.index, method='pad') # pass non-Index reindexed = self.ts.reindex(list(self.ts.index)) assert_series_equal(self.ts, reindexed) # bad fill method ts = self.ts[::2] pytest.raises(Exception, ts.reindex, self.ts.index, method='foo') def test_reindex_pad(self): s = Series(np.arange(10), dtype='int64') s2 = s[::2] reindexed = s2.reindex(s.index, method='pad') reindexed2 = s2.reindex(s.index, method='ffill') assert_series_equal(reindexed, reindexed2) expected = Series([0, 0, 2, 2, 4, 4, 6, 6, 8, 8], index=np.arange(10)) assert_series_equal(reindexed, expected) # GH4604 s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']) new_index = ['a', 'g', 'c', 'f'] expected = Series([1, 1, 3, 3], index=new_index) # this changes dtype because the ffill happens after result = s.reindex(new_index).ffill() assert_series_equal(result, expected.astype('float64')) result = s.reindex(new_index).ffill(downcast='infer') assert_series_equal(result, expected) expected = Series([1, 5, 3, 5], index=new_index) result = s.reindex(new_index, method='ffill') assert_series_equal(result, expected) # inferrence of new dtype s = Series([True, False, False, True], index=list('abcd')) new_index = 'agc' result = s.reindex(list(new_index)).ffill() expected = Series([True, True, False], index=list(new_index)) assert_series_equal(result, expected) # GH4618 shifted series downcasting s = Series(False, index=lrange(0, 5)) result = s.shift(1).fillna(method='bfill') expected = Series(False, index=lrange(0, 5)) assert_series_equal(result, expected) def test_reindex_nearest(self): s = Series(np.arange(10, dtype='int64')) target = [0.1, 0.9, 1.5, 2.0] actual = s.reindex(target, method='nearest') expected = Series(np.around(target).astype('int64'), target) assert_series_equal(expected, actual) actual = s.reindex_like(actual, method='nearest') assert_series_equal(expected, actual) actual = s.reindex_like(actual, method='nearest', tolerance=1) assert_series_equal(expected, actual) actual = s.reindex(target, method='nearest', tolerance=0.2) expected = Series([0, 1, np.nan, 2], target) assert_series_equal(expected, actual) def test_reindex_backfill(self): pass def test_reindex_int(self): ts = self.ts[::2] int_ts = Series(np.zeros(len(ts), dtype=int), index=ts.index) # this should work fine reindexed_int = int_ts.reindex(self.ts.index) # if NaNs introduced assert reindexed_int.dtype == np.float_ # NO NaNs introduced reindexed_int = int_ts.reindex(int_ts.index[::2]) assert reindexed_int.dtype == np.int_ def test_reindex_bool(self): # A series other than float, int, string, or object ts = self.ts[::2] bool_ts = Series(np.zeros(len(ts), dtype=bool), index=ts.index) # this should work fine reindexed_bool = bool_ts.reindex(self.ts.index) # if NaNs introduced assert reindexed_bool.dtype == np.object_ # NO NaNs introduced reindexed_bool = bool_ts.reindex(bool_ts.index[::2]) assert reindexed_bool.dtype == np.bool_ def test_reindex_bool_pad(self): # fail ts = self.ts[5:] bool_ts = Series(np.zeros(len(ts), dtype=bool), index=ts.index) filled_bool = bool_ts.reindex(self.ts.index, method='pad') assert isnull(filled_bool[:5]).all() def test_reindex_like(self): other = self.ts[::2] assert_series_equal(self.ts.reindex(other.index), self.ts.reindex_like(other)) # GH 7179 day1 = datetime(2013, 3, 5) day2 = datetime(2013, 5, 5) day3 = datetime(2014, 3, 5) series1 = Series([5, None, None], [day1, day2, day3]) series2 = Series([None, None], [day1, day3]) result = series1.reindex_like(series2, method='pad') expected = Series([5, np.nan], index=[day1, day3]) assert_series_equal(result, expected) def test_reindex_fill_value(self): # ----------------------------------------------------------- # floats floats = Series([1., 2., 3.]) result = floats.reindex([1, 2, 3]) expected = Series([2., 3., np.nan], index=[1, 2, 3]) assert_series_equal(result, expected) result = floats.reindex([1, 2, 3], fill_value=0) expected = Series([2., 3., 0], index=[1, 2, 3]) assert_series_equal(result, expected) # ----------------------------------------------------------- # ints ints = Series([1, 2, 3]) result = ints.reindex([1, 2, 3]) expected = Series([2., 3., np.nan], index=[1, 2, 3]) assert_series_equal(result, expected) # don't upcast result = ints.reindex([1, 2, 3], fill_value=0) expected = Series([2, 3, 0], index=[1, 2, 3]) assert issubclass(result.dtype.type, np.integer) assert_series_equal(result, expected) # ----------------------------------------------------------- # objects objects = Series([1, 2, 3], dtype=object) result = objects.reindex([1, 2, 3]) expected = Series([2, 3, np.nan], index=[1, 2, 3], dtype=object) assert_series_equal(result, expected) result = objects.reindex([1, 2, 3], fill_value='foo') expected = Series([2, 3, 'foo'], index=[1, 2, 3], dtype=object) assert_series_equal(result, expected) # ------------------------------------------------------------ # bools bools = Series([True, False, True]) result = bools.reindex([1, 2, 3]) expected = Series([False, True, np.nan], index=[1, 2, 3], dtype=object) assert_series_equal(result, expected) result = bools.reindex([1, 2, 3], fill_value=False) expected = Series([False, True, False], index=[1, 2, 3]) assert_series_equal(result, expected) def test_select(self): n = len(self.ts) result = self.ts.select(lambda x: x >= self.ts.index[n // 2]) expected = self.ts.reindex(self.ts.index[n // 2:]) assert_series_equal(result, expected) result = self.ts.select(lambda x: x.weekday() == 2) expected = self.ts[self.ts.index.weekday == 2] assert_series_equal(result, expected) def test_cast_on_putmask(self): # GH 2746 # need to upcast s = Series([1, 2], index=[1, 2], dtype='int64') s[[True, False]] = Series([0], index=[1], dtype='int64') expected = Series([0, 2], index=[1, 2], dtype='int64') assert_series_equal(s, expected) def test_type_promote_putmask(self): # GH8387: test that changing types does not break alignment ts = Series(np.random.randn(100), index=np.arange(100, 0, -1)).round(5) left, mask = ts.copy(), ts > 0 right = ts[mask].copy().map(str) left[mask] = right assert_series_equal(left, ts.map(lambda t: str(t) if t > 0 else t)) s = Series([0, 1, 2, 0]) mask = s > 0 s2 = s[mask].map(str) s[mask] = s2 assert_series_equal(s, Series([0, '1', '2', 0])) s = Series([0, 'foo', 'bar', 0]) mask = Series([False, True, True, False]) s2 = s[mask] s[mask] = s2 assert_series_equal(s, Series([0, 'foo', 'bar', 0])) def test_head_tail(self): assert_series_equal(self.series.head(), self.series[:5]) assert_series_equal(self.series.head(0), self.series[0:0]) assert_series_equal(self.series.tail(), self.series[-5:]) assert_series_equal(self.series.tail(0), self.series[0:0]) def test_multilevel_preserve_name(self): index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=['first', 'second']) s = Series(np.random.randn(len(index)), index=index, name='sth') result = s['foo'] result2 = s.loc['foo'] assert result.name == s.name assert result2.name == s.name def test_setitem_scalar_into_readonly_backing_data(self): # GH14359: test that you cannot mutate a read only buffer array = np.zeros(5) array.flags.writeable = False # make the array immutable series = Series(array) for n in range(len(series)): with pytest.raises(ValueError): series[n] = 1 assert array[n] == 0 def test_setitem_slice_into_readonly_backing_data(self): # GH14359: test that you cannot mutate a read only buffer array = np.zeros(5) array.flags.writeable = False # make the array immutable series = Series(array) with pytest.raises(ValueError): series[1:3] = 1 assert not array.any() class TestTimeSeriesDuplicates(object): def setup_method(self, method): dates = [datetime(2000, 1, 2), datetime(2000, 1, 2), datetime(2000, 1, 2), datetime(2000, 1, 3), datetime(2000, 1, 3), datetime(2000, 1, 3), datetime(2000, 1, 4), datetime(2000, 1, 4), datetime(2000, 1, 4), datetime(2000, 1, 5)] self.dups = Series(np.random.randn(len(dates)), index=dates) def test_constructor(self): assert isinstance(self.dups, Series) assert isinstance(self.dups.index, DatetimeIndex) def test_is_unique_monotonic(self): assert not self.dups.index.is_unique def test_index_unique(self): uniques = self.dups.index.unique() expected = DatetimeIndex([datetime(2000, 1, 2), datetime(2000, 1, 3), datetime(2000, 1, 4), datetime(2000, 1, 5)]) assert uniques.dtype == 'M8[ns]' # sanity tm.assert_index_equal(uniques, expected) assert self.dups.index.nunique() == 4 # #2563 assert isinstance(uniques, DatetimeIndex) dups_local = self.dups.index.tz_localize('US/Eastern') dups_local.name = 'foo' result = dups_local.unique() expected = DatetimeIndex(expected, name='foo') expected = expected.tz_localize('US/Eastern') assert result.tz is not None assert result.name == 'foo' tm.assert_index_equal(result, expected) # NaT, note this is excluded arr = [1370745748 + t for t in range(20)] + [tslib.iNaT] idx = DatetimeIndex(arr * 3) tm.assert_index_equal(idx.unique(), DatetimeIndex(arr)) assert idx.nunique() == 20 assert idx.nunique(dropna=False) == 21 arr = [Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20)] + [NaT] idx = DatetimeIndex(arr * 3) tm.assert_index_equal(idx.unique(), DatetimeIndex(arr)) assert idx.nunique() == 20 assert idx.nunique(dropna=False) == 21 def test_index_dupes_contains(self): d = datetime(2011, 12, 5, 20, 30) ix = DatetimeIndex([d, d]) assert d in ix def test_duplicate_dates_indexing(self): ts = self.dups uniques = ts.index.unique() for date in uniques: result = ts[date] mask = ts.index == date total = (ts.index == date).sum() expected = ts[mask] if total > 1: assert_series_equal(result, expected) else: assert_almost_equal(result, expected[0]) cp = ts.copy() cp[date] = 0 expected = Series(np.where(mask, 0, ts), index=ts.index) assert_series_equal(cp, expected) pytest.raises(KeyError, ts.__getitem__, datetime(2000, 1, 6)) # new index ts[datetime(2000, 1, 6)] = 0 assert ts[datetime(2000, 1, 6)] == 0 def test_range_slice(self): idx = DatetimeIndex(['1/1/2000', '1/2/2000', '1/2/2000', '1/3/2000', '1/4/2000']) ts = Series(np.random.randn(len(idx)), index=idx) result = ts['1/2/2000':] expected = ts[1:] assert_series_equal(result, expected) result = ts['1/2/2000':'1/3/2000'] expected = ts[1:4] assert_series_equal(result, expected) def test_groupby_average_dup_values(self): result = self.dups.groupby(level=0).mean() expected = self.dups.groupby(self.dups.index).mean() assert_series_equal(result, expected) def test_indexing_over_size_cutoff(self): import datetime # #1821 old_cutoff = _index._SIZE_CUTOFF try: _index._SIZE_CUTOFF = 1000 # create large list of non periodic datetime dates = [] sec = datetime.timedelta(seconds=1) half_sec = datetime.timedelta(microseconds=500000) d = datetime.datetime(2011, 12, 5, 20, 30) n = 1100 for i in range(n): dates.append(d) dates.append(d + sec) dates.append(d + sec + half_sec) dates.append(d + sec + sec + half_sec) d += 3 * sec # duplicate some values in the list duplicate_positions = np.random.randint(0, len(dates) - 1, 20) for p in duplicate_positions: dates[p + 1] = dates[p] df = DataFrame(np.random.randn(len(dates), 4), index=dates, columns=list('ABCD')) pos = n * 3 timestamp = df.index[pos] assert timestamp in df.index # it works! df.loc[timestamp] assert len(df.loc[[timestamp]]) > 0 finally: _index._SIZE_CUTOFF = old_cutoff def test_indexing_unordered(self): # GH 2437 rng = date_range(start='2011-01-01', end='2011-01-15') ts = Series(np.random.rand(len(rng)), index=rng) ts2 = pd.concat([ts[0:4], ts[-4:], ts[4:-4]]) for t in ts.index: # TODO: unused? s = str(t) # noqa expected = ts[t] result = ts2[t] assert expected == result # GH 3448 (ranges) def compare(slobj): result = ts2[slobj].copy() result = result.sort_index() expected = ts[slobj] assert_series_equal(result, expected) compare(slice('2011-01-01', '2011-01-15')) compare(slice('2010-12-30', '2011-01-15')) compare(slice('2011-01-01', '2011-01-16')) # partial ranges compare(slice('2011-01-01', '2011-01-6')) compare(slice('2011-01-06', '2011-01-8')) compare(slice('2011-01-06', '2011-01-12')) # single values result = ts2['2011'].sort_index() expected = ts['2011'] assert_series_equal(result, expected) # diff freq rng = date_range(datetime(2005, 1, 1), periods=20, freq='M') ts = Series(np.arange(len(rng)), index=rng) ts = ts.take(np.random.permutation(20)) result = ts['2005'] for t in result.index: assert t.year == 2005 def test_indexing(self): idx = date_range("2001-1-1", periods=20, freq='M') ts = Series(np.random.rand(len(idx)), index=idx) # getting # GH 3070, make sure semantics work on Series/Frame expected = ts['2001'] expected.name = 'A' df = DataFrame(dict(A=ts)) result = df['2001']['A'] assert_series_equal(expected, result) # setting ts['2001'] = 1 expected = ts['2001'] expected.name = 'A' df.loc['2001', 'A'] = 1 result = df['2001']['A'] assert_series_equal(expected, result) # GH3546 (not including times on the last day) idx = date_range(start='2013-05-31 00:00', end='2013-05-31 23:00', freq='H') ts = Series(lrange(len(idx)), index=idx) expected = ts['2013-05'] assert_series_equal(expected, ts) idx = date_range(start='2013-05-31 00:00', end='2013-05-31 23:59', freq='S') ts = Series(lrange(len(idx)), index=idx) expected = ts['2013-05'] assert_series_equal(expected, ts) idx = [Timestamp('2013-05-31 00:00'), Timestamp(datetime(2013, 5, 31, 23, 59, 59, 999999))] ts = Series(lrange(len(idx)), index=idx) expected = ts['2013'] assert_series_equal(expected, ts) # GH14826, indexing with a seconds resolution string / datetime object df = DataFrame(np.random.rand(5, 5), columns=['open', 'high', 'low', 'close', 'volume'], index=date_range('2012-01-02 18:01:00', periods=5, tz='US/Central', freq='s')) expected = df.loc[[df.index[2]]] # this is a single date, so will raise pytest.raises(KeyError, df.__getitem__, '2012-01-02 18:01:02', ) pytest.raises(KeyError, df.__getitem__, df.index[2], ) class TestDatetimeIndexing(object): """ Also test support for datetime64[ns] in Series / DataFrame """ def setup_method(self, method): dti = DatetimeIndex(start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq='Min') self.series = Series(np.random.rand(len(dti)), dti) def test_fancy_getitem(self): dti = DatetimeIndex(freq='WOM-1FRI', start=datetime(2005, 1, 1), end=datetime(2010, 1, 1)) s = Series(np.arange(len(dti)), index=dti) assert s[48] == 48 assert s['1/2/2009'] == 48 assert s['2009-1-2'] == 48 assert s[datetime(2009, 1, 2)] == 48 assert s[lib.Timestamp(datetime(2009, 1, 2))] == 48 pytest.raises(KeyError, s.__getitem__, '2009-1-3') assert_series_equal(s['3/6/2009':'2009-06-05'], s[datetime(2009, 3, 6):datetime(2009, 6, 5)]) def test_fancy_setitem(self): dti = DatetimeIndex(freq='WOM-1FRI', start=datetime(2005, 1, 1), end=datetime(2010, 1, 1)) s = Series(np.arange(len(dti)), index=dti) s[48] = -1 assert s[48] == -1 s['1/2/2009'] = -2 assert s[48] == -2 s['1/2/2009':'2009-06-05'] = -3 assert (s[48:54] == -3).all() def test_dti_snap(self): dti = DatetimeIndex(['1/1/2002', '1/2/2002', '1/3/2002', '1/4/2002', '1/5/2002', '1/6/2002', '1/7/2002'], freq='D') res = dti.snap(freq='W-MON') exp = date_range('12/31/2001', '1/7/2002', freq='w-mon') exp = exp.repeat([3, 4]) assert (res == exp).all() res = dti.snap(freq='B') exp = date_range('1/1/2002', '1/7/2002', freq='b') exp = exp.repeat([1, 1, 1, 2, 2]) assert (res == exp).all() def test_dti_reset_index_round_trip(self): dti = DatetimeIndex(start='1/1/2001', end='6/1/2001', freq='D') d1 = DataFrame({'v': np.random.rand(len(dti))}, index=dti) d2 = d1.reset_index() assert d2.dtypes[0] == np.dtype('M8[ns]') d3 = d2.set_index('index') assert_frame_equal(d1, d3, check_names=False) # #2329 stamp = datetime(2012, 11, 22) df = DataFrame([[stamp, 12.1]], columns=['Date', 'Value']) df = df.set_index('Date') assert df.index[0] == stamp assert df.reset_index()['Date'][0] == stamp def test_series_set_value(self): # #1561 dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)] index = DatetimeIndex(dates) s = Series().set_value(dates[0], 1.) s2 = s.set_value(dates[1], np.nan) exp = Series([1., np.nan], index=index) assert_series_equal(s2, exp) # s = Series(index[:1], index[:1]) # s2 = s.set_value(dates[1], index[1]) # assert s2.values.dtype == 'M8[ns]' @slow def test_slice_locs_indexerror(self): times = [datetime(2000, 1, 1) + timedelta(minutes=i * 10) for i in range(100000)] s = Series(lrange(100000), times) s.loc[datetime(1900, 1, 1):datetime(2100, 1, 1)] def test_slicing_datetimes(self): # GH 7523 # unique df = DataFrame(np.arange(4., dtype='float64'), index=[datetime(2001, 1, i, 10, 00) for i in [1, 2, 3, 4]]) result = df.loc[datetime(2001, 1, 1, 10):] assert_frame_equal(result, df) result = df.loc[:datetime(2001, 1, 4, 10)] assert_frame_equal(result, df) result = df.loc[datetime(2001, 1, 1, 10):datetime(2001, 1, 4, 10)] assert_frame_equal(result, df) result = df.loc[datetime(2001, 1, 1, 11):] expected = df.iloc[1:] assert_frame_equal(result, expected) result = df.loc['20010101 11':] assert_frame_equal(result, expected) # duplicates df = pd.DataFrame(np.arange(5., dtype='float64'), index=[datetime(2001, 1, i, 10, 00) for i in [1, 2, 2, 3, 4]]) result = df.loc[datetime(2001, 1, 1, 10):] assert_frame_equal(result, df) result = df.loc[:datetime(2001, 1, 4, 10)] assert_frame_equal(result, df) result = df.loc[datetime(2001, 1, 1, 10):datetime(2001, 1, 4, 10)] assert_frame_equal(result, df) result = df.loc[datetime(2001, 1, 1, 11):] expected = df.iloc[1:] assert_frame_equal(result, expected) result = df.loc['20010101 11':] assert_frame_equal(result, expected) def test_frame_datetime64_duplicated(self): dates = date_range('2010-07-01', end='2010-08-05') tst = DataFrame({'symbol': 'AAA', 'date': dates}) result = tst.duplicated(['date', 'symbol']) assert (-result).all() tst = DataFrame({'date': dates}) result = tst.duplicated() assert (-result).all() class TestNatIndexing(object): def setup_method(self, method): self.series = Series(date_range('1/1/2000', periods=10)) # --------------------------------------------------------------------- # NaT support def test_set_none_nan(self): self.series[3] = None assert self.series[3] is NaT self.series[3:5] = None assert self.series[4] is NaT self.series[5] = np.nan assert self.series[5] is NaT self.series[5:7] = np.nan assert self.series[6] is NaT def test_nat_operations(self): # GH 8617 s = Series([0, pd.NaT], dtype='m8[ns]') exp = s[0] assert s.median() == exp assert s.min() == exp assert s.max() == exp def test_round_nat(self): # GH14940 s = Series([pd.NaT]) expected = Series(pd.NaT) for method in ["round", "floor", "ceil"]: round_method = getattr(s.dt, method) for freq in ["s", "5s", "min", "5min", "h", "5h"]: assert_series_equal(round_method(freq), expected)
bsd-3-clause
stylianos-kampakis/scikit-learn
examples/exercises/plot_cv_diabetes.py
231
2527
""" =============================================== Cross-validation on diabetes Dataset Exercise =============================================== A tutorial exercise which uses cross-validation with linear models. This exercise is used in the :ref:`cv_estimators_tut` part of the :ref:`model_selection_tut` section of the :ref:`stat_learn_tut_index`. """ from __future__ import print_function print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import cross_validation, datasets, linear_model diabetes = datasets.load_diabetes() X = diabetes.data[:150] y = diabetes.target[:150] lasso = linear_model.Lasso() alphas = np.logspace(-4, -.5, 30) scores = list() scores_std = list() for alpha in alphas: lasso.alpha = alpha this_scores = cross_validation.cross_val_score(lasso, X, y, n_jobs=1) scores.append(np.mean(this_scores)) scores_std.append(np.std(this_scores)) plt.figure(figsize=(4, 3)) plt.semilogx(alphas, scores) # plot error lines showing +/- std. errors of the scores plt.semilogx(alphas, np.array(scores) + np.array(scores_std) / np.sqrt(len(X)), 'b--') plt.semilogx(alphas, np.array(scores) - np.array(scores_std) / np.sqrt(len(X)), 'b--') plt.ylabel('CV score') plt.xlabel('alpha') plt.axhline(np.max(scores), linestyle='--', color='.5') ############################################################################## # Bonus: how much can you trust the selection of alpha? # To answer this question we use the LassoCV object that sets its alpha # parameter automatically from the data by internal cross-validation (i.e. it # performs cross-validation on the training data it receives). # We use external cross-validation to see how much the automatically obtained # alphas differ across different cross-validation folds. lasso_cv = linear_model.LassoCV(alphas=alphas) k_fold = cross_validation.KFold(len(X), 3) print("Answer to the bonus question:", "how much can you trust the selection of alpha?") print() print("Alpha parameters maximising the generalization score on different") print("subsets of the data:") for k, (train, test) in enumerate(k_fold): lasso_cv.fit(X[train], y[train]) print("[fold {0}] alpha: {1:.5f}, score: {2:.5f}". format(k, lasso_cv.alpha_, lasso_cv.score(X[test], y[test]))) print() print("Answer: Not very much since we obtained different alphas for different") print("subsets of the data and moreover, the scores for these alphas differ") print("quite substantially.") plt.show()
bsd-3-clause
jjhelmus/scipy
scipy/signal/filter_design.py
14
135076
"""Filter design. """ from __future__ import division, print_function, absolute_import import warnings import math import numpy import numpy as np from numpy import (atleast_1d, poly, polyval, roots, real, asarray, resize, pi, absolute, logspace, r_, sqrt, tan, log10, arctan, arcsinh, sin, exp, cosh, arccosh, ceil, conjugate, zeros, sinh, append, concatenate, prod, ones, array, mintypecode) from numpy.polynomial.polynomial import polyval as npp_polyval from scipy import special, optimize from scipy.special import comb, factorial from scipy._lib._numpy_compat import polyvalfromroots __all__ = ['findfreqs', 'freqs', 'freqz', 'tf2zpk', 'zpk2tf', 'normalize', 'lp2lp', 'lp2hp', 'lp2bp', 'lp2bs', 'bilinear', 'iirdesign', 'iirfilter', 'butter', 'cheby1', 'cheby2', 'ellip', 'bessel', 'band_stop_obj', 'buttord', 'cheb1ord', 'cheb2ord', 'ellipord', 'buttap', 'cheb1ap', 'cheb2ap', 'ellipap', 'besselap', 'BadCoefficients', 'freqs_zpk', 'freqz_zpk', 'tf2sos', 'sos2tf', 'zpk2sos', 'sos2zpk', 'group_delay', 'sosfreqz', 'iirnotch', 'iirpeak'] class BadCoefficients(UserWarning): """Warning about badly conditioned filter coefficients""" pass abs = absolute def findfreqs(num, den, N, kind='ba'): """ Find array of frequencies for computing the response of an analog filter. Parameters ---------- num, den : array_like, 1-D The polynomial coefficients of the numerator and denominator of the transfer function of the filter or LTI system, where the coefficients are ordered from highest to lowest degree. Or, the roots of the transfer function numerator and denominator (i.e. zeroes and poles). N : int The length of the array to be computed. kind : str {'ba', 'zp'}, optional Specifies whether the numerator and denominator are specified by their polynomial coefficients ('ba'), or their roots ('zp'). Returns ------- w : (N,) ndarray A 1-D array of frequencies, logarithmically spaced. Examples -------- Find a set of nine frequencies that span the "interesting part" of the frequency response for the filter with the transfer function H(s) = s / (s^2 + 8s + 25) >>> from scipy import signal >>> signal.findfreqs([1, 0], [1, 8, 25], N=9) array([ 1.00000000e-02, 3.16227766e-02, 1.00000000e-01, 3.16227766e-01, 1.00000000e+00, 3.16227766e+00, 1.00000000e+01, 3.16227766e+01, 1.00000000e+02]) """ if kind == 'ba': ep = atleast_1d(roots(den)) + 0j tz = atleast_1d(roots(num)) + 0j elif kind == 'zp': ep = atleast_1d(den) + 0j tz = atleast_1d(num) + 0j else: raise ValueError("input must be one of {'ba', 'zp'}") if len(ep) == 0: ep = atleast_1d(-1000) + 0j ez = r_['-1', numpy.compress(ep.imag >= 0, ep, axis=-1), numpy.compress((abs(tz) < 1e5) & (tz.imag >= 0), tz, axis=-1)] integ = abs(ez) < 1e-10 hfreq = numpy.around(numpy.log10(numpy.max(3 * abs(ez.real + integ) + 1.5 * ez.imag)) + 0.5) lfreq = numpy.around(numpy.log10(0.1 * numpy.min(abs(real(ez + integ)) + 2 * ez.imag)) - 0.5) w = logspace(lfreq, hfreq, N) return w def freqs(b, a, worN=None, plot=None): """ Compute frequency response of analog filter. Given the M-order numerator `b` and N-order denominator `a` of an analog filter, compute its frequency response:: b[0]*(jw)**M + b[1]*(jw)**(M-1) + ... + b[M] H(w) = ---------------------------------------------- a[0]*(jw)**N + a[1]*(jw)**(N-1) + ... + a[N] Parameters ---------- b : array_like Numerator of a linear filter. a : array_like Denominator of a linear filter. worN : {None, int, array_like}, optional If None, then compute at 200 frequencies around the interesting parts of the response curve (determined by pole-zero locations). If a single integer, then compute at that many frequencies. Otherwise, compute the response at the angular frequencies (e.g. rad/s) given in `worN`. plot : callable, optional A callable that takes two arguments. If given, the return parameters `w` and `h` are passed to plot. Useful for plotting the frequency response inside `freqs`. Returns ------- w : ndarray The angular frequencies at which `h` was computed. h : ndarray The frequency response. See Also -------- freqz : Compute the frequency response of a digital filter. Notes ----- Using Matplotlib's "plot" function as the callable for `plot` produces unexpected results, this plots the real part of the complex transfer function, not the magnitude. Try ``lambda w, h: plot(w, abs(h))``. Examples -------- >>> from scipy.signal import freqs, iirfilter >>> b, a = iirfilter(4, [1, 10], 1, 60, analog=True, ftype='cheby1') >>> w, h = freqs(b, a, worN=np.logspace(-1, 2, 1000)) >>> import matplotlib.pyplot as plt >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.xlabel('Frequency') >>> plt.ylabel('Amplitude response [dB]') >>> plt.grid() >>> plt.show() """ if worN is None: w = findfreqs(b, a, 200) elif isinstance(worN, int): N = worN w = findfreqs(b, a, N) else: w = worN w = atleast_1d(w) s = 1j * w h = polyval(b, s) / polyval(a, s) if plot is not None: plot(w, h) return w, h def freqs_zpk(z, p, k, worN=None): """ Compute frequency response of analog filter. Given the zeros `z`, poles `p`, and gain `k` of a filter, compute its frequency response:: (jw-z[0]) * (jw-z[1]) * ... * (jw-z[-1]) H(w) = k * ---------------------------------------- (jw-p[0]) * (jw-p[1]) * ... * (jw-p[-1]) Parameters ---------- z : array_like Zeroes of a linear filter p : array_like Poles of a linear filter k : scalar Gain of a linear filter worN : {None, int, array_like}, optional If None, then compute at 200 frequencies around the interesting parts of the response curve (determined by pole-zero locations). If a single integer, then compute at that many frequencies. Otherwise, compute the response at the angular frequencies (e.g. rad/s) given in `worN`. Returns ------- w : ndarray The angular frequencies at which `h` was computed. h : ndarray The frequency response. See Also -------- freqs : Compute the frequency response of an analog filter in TF form freqz : Compute the frequency response of a digital filter in TF form freqz_zpk : Compute the frequency response of a digital filter in ZPK form Notes ----- .. versionadded: 0.19.0 Examples -------- >>> from scipy.signal import freqs_zpk, iirfilter >>> z, p, k = iirfilter(4, [1, 10], 1, 60, analog=True, ftype='cheby1', ... output='zpk') >>> w, h = freqs_zpk(z, p, k, worN=np.logspace(-1, 2, 1000)) >>> import matplotlib.pyplot as plt >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.xlabel('Frequency') >>> plt.ylabel('Amplitude response [dB]') >>> plt.grid() >>> plt.show() """ k = np.asarray(k) if k.size > 1: raise ValueError('k must be a single scalar gain') if worN is None: w = findfreqs(z, p, 200, kind='zp') elif isinstance(worN, int): N = worN w = findfreqs(z, p, N, kind='zp') else: w = worN w = atleast_1d(w) s = 1j * w num = polyvalfromroots(s, z) den = polyvalfromroots(s, p) h = k * num/den return w, h def freqz(b, a=1, worN=None, whole=False, plot=None): """ Compute the frequency response of a digital filter. Given the M-order numerator `b` and N-order denominator `a` of a digital filter, compute its frequency response:: jw -jw -jwM jw B(e ) b[0] + b[1]e + .... + b[M]e H(e ) = ---- = ----------------------------------- jw -jw -jwN A(e ) a[0] + a[1]e + .... + a[N]e Parameters ---------- b : array_like numerator of a linear filter a : array_like denominator of a linear filter worN : {None, int, array_like}, optional If None (default), then compute at 512 frequencies equally spaced around the unit circle. If a single integer, then compute at that many frequencies. If an array_like, compute the response at the frequencies given (in radians/sample). whole : bool, optional Normally, frequencies are computed from 0 to the Nyquist frequency, pi radians/sample (upper-half of unit-circle). If `whole` is True, compute frequencies from 0 to 2*pi radians/sample. plot : callable A callable that takes two arguments. If given, the return parameters `w` and `h` are passed to plot. Useful for plotting the frequency response inside `freqz`. Returns ------- w : ndarray The normalized frequencies at which `h` was computed, in radians/sample. h : ndarray The frequency response, as complex numbers. See Also -------- sosfreqz Notes ----- Using Matplotlib's "plot" function as the callable for `plot` produces unexpected results, this plots the real part of the complex transfer function, not the magnitude. Try ``lambda w, h: plot(w, abs(h))``. Examples -------- >>> from scipy import signal >>> b = signal.firwin(80, 0.5, window=('kaiser', 8)) >>> w, h = signal.freqz(b) >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.title('Digital filter frequency response') >>> ax1 = fig.add_subplot(111) >>> plt.plot(w, 20 * np.log10(abs(h)), 'b') >>> plt.ylabel('Amplitude [dB]', color='b') >>> plt.xlabel('Frequency [rad/sample]') >>> ax2 = ax1.twinx() >>> angles = np.unwrap(np.angle(h)) >>> plt.plot(w, angles, 'g') >>> plt.ylabel('Angle (radians)', color='g') >>> plt.grid() >>> plt.axis('tight') >>> plt.show() """ b, a = map(atleast_1d, (b, a)) if whole: lastpoint = 2 * pi else: lastpoint = pi if worN is None: N = 512 w = numpy.linspace(0, lastpoint, N, endpoint=False) elif isinstance(worN, int): N = worN w = numpy.linspace(0, lastpoint, N, endpoint=False) else: w = worN w = atleast_1d(w) zm1 = exp(-1j * w) h = polyval(b[::-1], zm1) / polyval(a[::-1], zm1) if plot is not None: plot(w, h) return w, h def freqz_zpk(z, p, k, worN=None, whole=False): """ Compute the frequency response of a digital filter in ZPK form. Given the Zeros, Poles and Gain of a digital filter, compute its frequency response:: :math:`H(z)=k \prod_i (z - Z[i]) / \prod_j (z - P[j])` where :math:`k` is the `gain`, :math:`Z` are the `zeros` and :math:`P` are the `poles`. Parameters ---------- z : array_like Zeroes of a linear filter p : array_like Poles of a linear filter k : scalar Gain of a linear filter worN : {None, int, array_like}, optional If None (default), then compute at 512 frequencies equally spaced around the unit circle. If a single integer, then compute at that many frequencies. If an array_like, compute the response at the frequencies given (in radians/sample). whole : bool, optional Normally, frequencies are computed from 0 to the Nyquist frequency, pi radians/sample (upper-half of unit-circle). If `whole` is True, compute frequencies from 0 to 2*pi radians/sample. Returns ------- w : ndarray The normalized frequencies at which `h` was computed, in radians/sample. h : ndarray The frequency response. See Also -------- freqs : Compute the frequency response of an analog filter in TF form freqs_zpk : Compute the frequency response of an analog filter in ZPK form freqz : Compute the frequency response of a digital filter in TF form Notes ----- .. versionadded: 0.19.0 Examples -------- >>> from scipy import signal >>> z, p, k = signal.butter(4, 0.2, output='zpk') >>> w, h = signal.freqz_zpk(z, p, k) >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.title('Digital filter frequency response') >>> ax1 = fig.add_subplot(111) >>> plt.plot(w, 20 * np.log10(abs(h)), 'b') >>> plt.ylabel('Amplitude [dB]', color='b') >>> plt.xlabel('Frequency [rad/sample]') >>> ax2 = ax1.twinx() >>> angles = np.unwrap(np.angle(h)) >>> plt.plot(w, angles, 'g') >>> plt.ylabel('Angle (radians)', color='g') >>> plt.grid() >>> plt.axis('tight') >>> plt.show() """ z, p = map(atleast_1d, (z, p)) if whole: lastpoint = 2 * pi else: lastpoint = pi if worN is None: N = 512 w = numpy.linspace(0, lastpoint, N, endpoint=False) elif isinstance(worN, int): N = worN w = numpy.linspace(0, lastpoint, N, endpoint=False) else: w = worN w = atleast_1d(w) zm1 = exp(1j * w) h = k * polyvalfromroots(zm1, z) / polyvalfromroots(zm1, p) return w, h def group_delay(system, w=None, whole=False): r"""Compute the group delay of a digital filter. The group delay measures by how many samples amplitude envelopes of various spectral components of a signal are delayed by a filter. It is formally defined as the derivative of continuous (unwrapped) phase:: d jw D(w) = - -- arg H(e) dw Parameters ---------- system : tuple of array_like (b, a) Numerator and denominator coefficients of a filter transfer function. w : {None, int, array-like}, optional If None (default), then compute at 512 frequencies equally spaced around the unit circle. If a single integer, then compute at that many frequencies. If array, compute the delay at the frequencies given (in radians/sample). whole : bool, optional Normally, frequencies are computed from 0 to the Nyquist frequency, pi radians/sample (upper-half of unit-circle). If `whole` is True, compute frequencies from 0 to ``2*pi`` radians/sample. Returns ------- w : ndarray The normalized frequencies at which the group delay was computed, in radians/sample. gd : ndarray The group delay. Notes ----- The similar function in MATLAB is called `grpdelay`. If the transfer function :math:`H(z)` has zeros or poles on the unit circle, the group delay at corresponding frequencies is undefined. When such a case arises the warning is raised and the group delay is set to 0 at those frequencies. For the details of numerical computation of the group delay refer to [1]_. .. versionadded: 0.16.0 See Also -------- freqz : Frequency response of a digital filter References ---------- .. [1] Richard G. Lyons, "Understanding Digital Signal Processing, 3rd edition", p. 830. Examples -------- >>> from scipy import signal >>> b, a = signal.iirdesign(0.1, 0.3, 5, 50, ftype='cheby1') >>> w, gd = signal.group_delay((b, a)) >>> import matplotlib.pyplot as plt >>> plt.title('Digital filter group delay') >>> plt.plot(w, gd) >>> plt.ylabel('Group delay [samples]') >>> plt.xlabel('Frequency [rad/sample]') >>> plt.show() """ if w is None: w = 512 if isinstance(w, int): if whole: w = np.linspace(0, 2 * pi, w, endpoint=False) else: w = np.linspace(0, pi, w, endpoint=False) w = np.atleast_1d(w) b, a = map(np.atleast_1d, system) c = np.convolve(b, a[::-1]) cr = c * np.arange(c.size) z = np.exp(-1j * w) num = np.polyval(cr[::-1], z) den = np.polyval(c[::-1], z) singular = np.absolute(den) < 10 * EPSILON if np.any(singular): warnings.warn( "The group delay is singular at frequencies [{0}], setting to 0". format(", ".join("{0:.3f}".format(ws) for ws in w[singular])) ) gd = np.zeros_like(w) gd[~singular] = np.real(num[~singular] / den[~singular]) - a.size + 1 return w, gd def _validate_sos(sos): """Helper to validate a SOS input""" sos = np.atleast_2d(sos) if sos.ndim != 2: raise ValueError('sos array must be 2D') n_sections, m = sos.shape if m != 6: raise ValueError('sos array must be shape (n_sections, 6)') if not (sos[:, 3] == 1).all(): raise ValueError('sos[:, 3] should be all ones') return sos, n_sections def sosfreqz(sos, worN=None, whole=False): """ Compute the frequency response of a digital filter in SOS format. Given `sos`, an array with shape (n, 6) of second order sections of a digital filter, compute the frequency response of the system function:: B0(z) B1(z) B{n-1}(z) H(z) = ----- * ----- * ... * --------- A0(z) A1(z) A{n-1}(z) for z = exp(omega*1j), where B{k}(z) and A{k}(z) are numerator and denominator of the transfer function of the k-th second order section. Parameters ---------- sos : array_like Array of second-order filter coefficients, must have shape ``(n_sections, 6)``. Each row corresponds to a second-order section, with the first three columns providing the numerator coefficients and the last three providing the denominator coefficients. worN : {None, int, array_like}, optional If None (default), then compute at 512 frequencies equally spaced around the unit circle. If a single integer, then compute at that many frequencies. If an array_like, compute the response at the frequencies given (in radians/sample). whole : bool, optional Normally, frequencies are computed from 0 to the Nyquist frequency, pi radians/sample (upper-half of unit-circle). If `whole` is True, compute frequencies from 0 to 2*pi radians/sample. Returns ------- w : ndarray The normalized frequencies at which `h` was computed, in radians/sample. h : ndarray The frequency response, as complex numbers. See Also -------- freqz, sosfilt Notes ----- .. versionadded:: 0.19.0 Examples -------- Design a 15th-order bandpass filter in SOS format. >>> from scipy import signal >>> sos = signal.ellip(15, 0.5, 60, (0.2, 0.4), btype='bandpass', ... output='sos') Compute the frequency response at 1500 points from DC to Nyquist. >>> w, h = signal.sosfreqz(sos, worN=1500) Plot the response. >>> import matplotlib.pyplot as plt >>> plt.subplot(2, 1, 1) >>> db = 20*np.log10(np.abs(h)) >>> plt.plot(w/np.pi, db) >>> plt.ylim(-75, 5) >>> plt.grid(True) >>> plt.yticks([0, -20, -40, -60]) >>> plt.ylabel('Gain [dB]') >>> plt.title('Frequency Response') >>> plt.subplot(2, 1, 2) >>> plt.plot(w/np.pi, np.angle(h)) >>> plt.grid(True) >>> plt.yticks([-np.pi, -0.5*np.pi, 0, 0.5*np.pi, np.pi], ... [r'$-\\pi$', r'$-\\pi/2$', '0', r'$\\pi/2$', r'$\\pi$']) >>> plt.ylabel('Phase [rad]') >>> plt.xlabel('Normalized frequency (1.0 = Nyquist)') >>> plt.show() If the same filter is implemented as a single transfer function, numerical error corrupts the frequency response: >>> b, a = signal.ellip(15, 0.5, 60, (0.2, 0.4), btype='bandpass', ... output='ba') >>> w, h = signal.freqz(b, a, worN=1500) >>> plt.subplot(2, 1, 1) >>> db = 20*np.log10(np.abs(h)) >>> plt.plot(w/np.pi, db) >>> plt.subplot(2, 1, 2) >>> plt.plot(w/np.pi, np.angle(h)) >>> plt.show() """ sos, n_sections = _validate_sos(sos) if n_sections == 0: raise ValueError('Cannot compute frequencies with no sections') h = 1. for row in sos: w, rowh = freqz(row[:3], row[3:], worN=worN, whole=whole) h *= rowh return w, h def _cplxreal(z, tol=None): """ Split into complex and real parts, combining conjugate pairs. The 1D input vector `z` is split up into its complex (`zc`) and real (`zr`) elements. Every complex element must be part of a complex-conjugate pair, which are combined into a single number (with positive imaginary part) in the output. Two complex numbers are considered a conjugate pair if their real and imaginary parts differ in magnitude by less than ``tol * abs(z)``. Parameters ---------- z : array_like Vector of complex numbers to be sorted and split tol : float, optional Relative tolerance for testing realness and conjugate equality. Default is ``100 * spacing(1)`` of `z`'s data type (i.e. 2e-14 for float64) Returns ------- zc : ndarray Complex elements of `z`, with each pair represented by a single value having positive imaginary part, sorted first by real part, and then by magnitude of imaginary part. The pairs are averaged when combined to reduce error. zr : ndarray Real elements of `z` (those having imaginary part less than `tol` times their magnitude), sorted by value. Raises ------ ValueError If there are any complex numbers in `z` for which a conjugate cannot be found. See Also -------- _cplxpair Examples -------- >>> a = [4, 3, 1, 2-2j, 2+2j, 2-1j, 2+1j, 2-1j, 2+1j, 1+1j, 1-1j] >>> zc, zr = _cplxreal(a) >>> print zc [ 1.+1.j 2.+1.j 2.+1.j 2.+2.j] >>> print zr [ 1. 3. 4.] """ z = atleast_1d(z) if z.size == 0: return z, z elif z.ndim != 1: raise ValueError('_cplxreal only accepts 1D input') if tol is None: # Get tolerance from dtype of input tol = 100 * np.finfo((1.0 * z).dtype).eps # Sort by real part, magnitude of imaginary part (speed up further sorting) z = z[np.lexsort((abs(z.imag), z.real))] # Split reals from conjugate pairs real_indices = abs(z.imag) <= tol * abs(z) zr = z[real_indices].real if len(zr) == len(z): # Input is entirely real return array([]), zr # Split positive and negative halves of conjugates z = z[~real_indices] zp = z[z.imag > 0] zn = z[z.imag < 0] if len(zp) != len(zn): raise ValueError('Array contains complex value with no matching ' 'conjugate.') # Find runs of (approximately) the same real part same_real = np.diff(zp.real) <= tol * abs(zp[:-1]) diffs = numpy.diff(concatenate(([0], same_real, [0]))) run_starts = numpy.where(diffs > 0)[0] run_stops = numpy.where(diffs < 0)[0] # Sort each run by their imaginary parts for i in range(len(run_starts)): start = run_starts[i] stop = run_stops[i] + 1 for chunk in (zp[start:stop], zn[start:stop]): chunk[...] = chunk[np.lexsort([abs(chunk.imag)])] # Check that negatives match positives if any(abs(zp - zn.conj()) > tol * abs(zn)): raise ValueError('Array contains complex value with no matching ' 'conjugate.') # Average out numerical inaccuracy in real vs imag parts of pairs zc = (zp + zn.conj()) / 2 return zc, zr def _cplxpair(z, tol=None): """ Sort into pairs of complex conjugates. Complex conjugates in `z` are sorted by increasing real part. In each pair, the number with negative imaginary part appears first. If pairs have identical real parts, they are sorted by increasing imaginary magnitude. Two complex numbers are considered a conjugate pair if their real and imaginary parts differ in magnitude by less than ``tol * abs(z)``. The pairs are forced to be exact complex conjugates by averaging the positive and negative values. Purely real numbers are also sorted, but placed after the complex conjugate pairs. A number is considered real if its imaginary part is smaller than `tol` times the magnitude of the number. Parameters ---------- z : array_like 1-dimensional input array to be sorted. tol : float, optional Relative tolerance for testing realness and conjugate equality. Default is ``100 * spacing(1)`` of `z`'s data type (i.e. 2e-14 for float64) Returns ------- y : ndarray Complex conjugate pairs followed by real numbers. Raises ------ ValueError If there are any complex numbers in `z` for which a conjugate cannot be found. See Also -------- _cplxreal Examples -------- >>> a = [4, 3, 1, 2-2j, 2+2j, 2-1j, 2+1j, 2-1j, 2+1j, 1+1j, 1-1j] >>> z = _cplxpair(a) >>> print(z) [ 1.-1.j 1.+1.j 2.-1.j 2.+1.j 2.-1.j 2.+1.j 2.-2.j 2.+2.j 1.+0.j 3.+0.j 4.+0.j] """ z = atleast_1d(z) if z.size == 0 or np.isrealobj(z): return np.sort(z) if z.ndim != 1: raise ValueError('z must be 1-dimensional') zc, zr = _cplxreal(z, tol) # Interleave complex values and their conjugates, with negative imaginary # parts first in each pair zc = np.dstack((zc.conj(), zc)).flatten() z = np.append(zc, zr) return z def tf2zpk(b, a): r"""Return zero, pole, gain (z, p, k) representation from a numerator, denominator representation of a linear filter. Parameters ---------- b : array_like Numerator polynomial coefficients. a : array_like Denominator polynomial coefficients. Returns ------- z : ndarray Zeros of the transfer function. p : ndarray Poles of the transfer function. k : float System gain. Notes ----- If some values of `b` are too close to 0, they are removed. In that case, a BadCoefficients warning is emitted. The `b` and `a` arrays are interpreted as coefficients for positive, descending powers of the transfer function variable. So the inputs :math:`b = [b_0, b_1, ..., b_M]` and :math:`a =[a_0, a_1, ..., a_N]` can represent an analog filter of the form: .. math:: H(s) = \frac {b_0 s^M + b_1 s^{(M-1)} + \cdots + b_M} {a_0 s^N + a_1 s^{(N-1)} + \cdots + a_N} or a discrete-time filter of the form: .. math:: H(z) = \frac {b_0 z^M + b_1 z^{(M-1)} + \cdots + b_M} {a_0 z^N + a_1 z^{(N-1)} + \cdots + a_N} This "positive powers" form is found more commonly in controls engineering. If `M` and `N` are equal (which is true for all filters generated by the bilinear transform), then this happens to be equivalent to the "negative powers" discrete-time form preferred in DSP: .. math:: H(z) = \frac {b_0 + b_1 z^{-1} + \cdots + b_M z^{-M}} {a_0 + a_1 z^{-1} + \cdots + a_N z^{-N}} Although this is true for common filters, remember that this is not true in the general case. If `M` and `N` are not equal, the discrete-time transfer function coefficients must first be converted to the "positive powers" form before finding the poles and zeros. """ b, a = normalize(b, a) b = (b + 0.0) / a[0] a = (a + 0.0) / a[0] k = b[0] b /= b[0] z = roots(b) p = roots(a) return z, p, k def zpk2tf(z, p, k): """ Return polynomial transfer function representation from zeros and poles Parameters ---------- z : array_like Zeros of the transfer function. p : array_like Poles of the transfer function. k : float System gain. Returns ------- b : ndarray Numerator polynomial coefficients. a : ndarray Denominator polynomial coefficients. """ z = atleast_1d(z) k = atleast_1d(k) if len(z.shape) > 1: temp = poly(z[0]) b = zeros((z.shape[0], z.shape[1] + 1), temp.dtype.char) if len(k) == 1: k = [k[0]] * z.shape[0] for i in range(z.shape[0]): b[i] = k[i] * poly(z[i]) else: b = k * poly(z) a = atleast_1d(poly(p)) # Use real output if possible. Copied from numpy.poly, since # we can't depend on a specific version of numpy. if issubclass(b.dtype.type, numpy.complexfloating): # if complex roots are all complex conjugates, the roots are real. roots = numpy.asarray(z, complex) pos_roots = numpy.compress(roots.imag > 0, roots) neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots)) if len(pos_roots) == len(neg_roots): if numpy.all(numpy.sort_complex(neg_roots) == numpy.sort_complex(pos_roots)): b = b.real.copy() if issubclass(a.dtype.type, numpy.complexfloating): # if complex roots are all complex conjugates, the roots are real. roots = numpy.asarray(p, complex) pos_roots = numpy.compress(roots.imag > 0, roots) neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots)) if len(pos_roots) == len(neg_roots): if numpy.all(numpy.sort_complex(neg_roots) == numpy.sort_complex(pos_roots)): a = a.real.copy() return b, a def tf2sos(b, a, pairing='nearest'): """ Return second-order sections from transfer function representation Parameters ---------- b : array_like Numerator polynomial coefficients. a : array_like Denominator polynomial coefficients. pairing : {'nearest', 'keep_odd'}, optional The method to use to combine pairs of poles and zeros into sections. See `zpk2sos`. Returns ------- sos : ndarray Array of second-order filter coefficients, with shape ``(n_sections, 6)``. See `sosfilt` for the SOS filter format specification. See Also -------- zpk2sos, sosfilt Notes ----- It is generally discouraged to convert from TF to SOS format, since doing so usually will not improve numerical precision errors. Instead, consider designing filters in ZPK format and converting directly to SOS. TF is converted to SOS by first converting to ZPK format, then converting ZPK to SOS. .. versionadded:: 0.16.0 """ return zpk2sos(*tf2zpk(b, a), pairing=pairing) def sos2tf(sos): """ Return a single transfer function from a series of second-order sections Parameters ---------- sos : array_like Array of second-order filter coefficients, must have shape ``(n_sections, 6)``. See `sosfilt` for the SOS filter format specification. Returns ------- b : ndarray Numerator polynomial coefficients. a : ndarray Denominator polynomial coefficients. Notes ----- .. versionadded:: 0.16.0 """ sos = np.asarray(sos) b = [1.] a = [1.] n_sections = sos.shape[0] for section in range(n_sections): b = np.polymul(b, sos[section, :3]) a = np.polymul(a, sos[section, 3:]) return b, a def sos2zpk(sos): """ Return zeros, poles, and gain of a series of second-order sections Parameters ---------- sos : array_like Array of second-order filter coefficients, must have shape ``(n_sections, 6)``. See `sosfilt` for the SOS filter format specification. Returns ------- z : ndarray Zeros of the transfer function. p : ndarray Poles of the transfer function. k : float System gain. Notes ----- .. versionadded:: 0.16.0 """ sos = np.asarray(sos) n_sections = sos.shape[0] z = np.empty(n_sections*2, np.complex128) p = np.empty(n_sections*2, np.complex128) k = 1. for section in range(n_sections): zpk = tf2zpk(sos[section, :3], sos[section, 3:]) z[2*section:2*(section+1)] = zpk[0] p[2*section:2*(section+1)] = zpk[1] k *= zpk[2] return z, p, k def _nearest_real_complex_idx(fro, to, which): """Get the next closest real or complex element based on distance""" assert which in ('real', 'complex') order = np.argsort(np.abs(fro - to)) mask = np.isreal(fro[order]) if which == 'complex': mask = ~mask return order[np.where(mask)[0][0]] def zpk2sos(z, p, k, pairing='nearest'): """ Return second-order sections from zeros, poles, and gain of a system Parameters ---------- z : array_like Zeros of the transfer function. p : array_like Poles of the transfer function. k : float System gain. pairing : {'nearest', 'keep_odd'}, optional The method to use to combine pairs of poles and zeros into sections. See Notes below. Returns ------- sos : ndarray Array of second-order filter coefficients, with shape ``(n_sections, 6)``. See `sosfilt` for the SOS filter format specification. See Also -------- sosfilt Notes ----- The algorithm used to convert ZPK to SOS format is designed to minimize errors due to numerical precision issues. The pairing algorithm attempts to minimize the peak gain of each biquadratic section. This is done by pairing poles with the nearest zeros, starting with the poles closest to the unit circle. *Algorithms* The current algorithms are designed specifically for use with digital filters. (The output coefficents are not correct for analog filters.) The steps in the ``pairing='nearest'`` and ``pairing='keep_odd'`` algorithms are mostly shared. The ``nearest`` algorithm attempts to minimize the peak gain, while ``'keep_odd'`` minimizes peak gain under the constraint that odd-order systems should retain one section as first order. The algorithm steps and are as follows: As a pre-processing step, add poles or zeros to the origin as necessary to obtain the same number of poles and zeros for pairing. If ``pairing == 'nearest'`` and there are an odd number of poles, add an additional pole and a zero at the origin. The following steps are then iterated over until no more poles or zeros remain: 1. Take the (next remaining) pole (complex or real) closest to the unit circle to begin a new filter section. 2. If the pole is real and there are no other remaining real poles [#]_, add the closest real zero to the section and leave it as a first order section. Note that after this step we are guaranteed to be left with an even number of real poles, complex poles, real zeros, and complex zeros for subsequent pairing iterations. 3. Else: 1. If the pole is complex and the zero is the only remaining real zero*, then pair the pole with the *next* closest zero (guaranteed to be complex). This is necessary to ensure that there will be a real zero remaining to eventually create a first-order section (thus keeping the odd order). 2. Else pair the pole with the closest remaining zero (complex or real). 3. Proceed to complete the second-order section by adding another pole and zero to the current pole and zero in the section: 1. If the current pole and zero are both complex, add their conjugates. 2. Else if the pole is complex and the zero is real, add the conjugate pole and the next closest real zero. 3. Else if the pole is real and the zero is complex, add the conjugate zero and the real pole closest to those zeros. 4. Else (we must have a real pole and real zero) add the next real pole closest to the unit circle, and then add the real zero closest to that pole. .. [#] This conditional can only be met for specific odd-order inputs with the ``pairing == 'keep_odd'`` method. .. versionadded:: 0.16.0 Examples -------- Design a 6th order low-pass elliptic digital filter for a system with a sampling rate of 8000 Hz that has a pass-band corner frequency of 1000 Hz. The ripple in the pass-band should not exceed 0.087 dB, and the attenuation in the stop-band should be at least 90 dB. In the following call to `signal.ellip`, we could use ``output='sos'``, but for this example, we'll use ``output='zpk'``, and then convert to SOS format with `zpk2sos`: >>> from scipy import signal >>> z, p, k = signal.ellip(6, 0.087, 90, 1000/(0.5*8000), output='zpk') Now convert to SOS format. >>> sos = signal.zpk2sos(z, p, k) The coefficients of the numerators of the sections: >>> sos[:, :3] array([[ 0.0014154 , 0.00248707, 0.0014154 ], [ 1. , 0.72965193, 1. ], [ 1. , 0.17594966, 1. ]]) The symmetry in the coefficients occurs because all the zeros are on the unit circle. The coefficients of the denominators of the sections: >>> sos[:, 3:] array([[ 1. , -1.32543251, 0.46989499], [ 1. , -1.26117915, 0.6262586 ], [ 1. , -1.25707217, 0.86199667]]) The next example shows the effect of the `pairing` option. We have a system with three poles and three zeros, so the SOS array will have shape (2, 6). The means there is, in effect, an extra pole and an extra zero at the origin in the SOS representation. >>> z1 = np.array([-1, -0.5-0.5j, -0.5+0.5j]) >>> p1 = np.array([0.75, 0.8+0.1j, 0.8-0.1j]) With ``pairing='nearest'`` (the default), we obtain >>> signal.zpk2sos(z1, p1, 1) array([[ 1. , 1. , 0.5 , 1. , -0.75, 0. ], [ 1. , 1. , 0. , 1. , -1.6 , 0.65]]) The first section has the zeros {-0.5-0.05j, -0.5+0.5j} and the poles {0, 0.75}, and the second section has the zeros {-1, 0} and poles {0.8+0.1j, 0.8-0.1j}. Note that the extra pole and zero at the origin have been assigned to different sections. With ``pairing='keep_odd'``, we obtain: >>> signal.zpk2sos(z1, p1, 1, pairing='keep_odd') array([[ 1. , 1. , 0. , 1. , -0.75, 0. ], [ 1. , 1. , 0.5 , 1. , -1.6 , 0.65]]) The extra pole and zero at the origin are in the same section. The first section is, in effect, a first-order section. """ # TODO in the near future: # 1. Add SOS capability to `filtfilt`, `freqz`, etc. somehow (#3259). # 2. Make `decimate` use `sosfilt` instead of `lfilter`. # 3. Make sosfilt automatically simplify sections to first order # when possible. Note this might make `sosfiltfilt` a bit harder (ICs). # 4. Further optimizations of the section ordering / pole-zero pairing. # See the wiki for other potential issues. valid_pairings = ['nearest', 'keep_odd'] if pairing not in valid_pairings: raise ValueError('pairing must be one of %s, not %s' % (valid_pairings, pairing)) if len(z) == len(p) == 0: return array([[k, 0., 0., 1., 0., 0.]]) # ensure we have the same number of poles and zeros, and make copies p = np.concatenate((p, np.zeros(max(len(z) - len(p), 0)))) z = np.concatenate((z, np.zeros(max(len(p) - len(z), 0)))) n_sections = (max(len(p), len(z)) + 1) // 2 sos = zeros((n_sections, 6)) if len(p) % 2 == 1 and pairing == 'nearest': p = np.concatenate((p, [0.])) z = np.concatenate((z, [0.])) assert len(p) == len(z) # Ensure we have complex conjugate pairs # (note that _cplxreal only gives us one element of each complex pair): z = np.concatenate(_cplxreal(z)) p = np.concatenate(_cplxreal(p)) p_sos = np.zeros((n_sections, 2), np.complex128) z_sos = np.zeros_like(p_sos) for si in range(n_sections): # Select the next "worst" pole p1_idx = np.argmin(np.abs(1 - np.abs(p))) p1 = p[p1_idx] p = np.delete(p, p1_idx) # Pair that pole with a zero if np.isreal(p1) and np.isreal(p).sum() == 0: # Special case to set a first-order section z1_idx = _nearest_real_complex_idx(z, p1, 'real') z1 = z[z1_idx] z = np.delete(z, z1_idx) p2 = z2 = 0 else: if not np.isreal(p1) and np.isreal(z).sum() == 1: # Special case to ensure we choose a complex zero to pair # with so later (setting up a first-order section) z1_idx = _nearest_real_complex_idx(z, p1, 'complex') assert not np.isreal(z[z1_idx]) else: # Pair the pole with the closest zero (real or complex) z1_idx = np.argmin(np.abs(p1 - z)) z1 = z[z1_idx] z = np.delete(z, z1_idx) # Now that we have p1 and z1, figure out what p2 and z2 need to be if not np.isreal(p1): if not np.isreal(z1): # complex pole, complex zero p2 = p1.conj() z2 = z1.conj() else: # complex pole, real zero p2 = p1.conj() z2_idx = _nearest_real_complex_idx(z, p1, 'real') z2 = z[z2_idx] assert np.isreal(z2) z = np.delete(z, z2_idx) else: if not np.isreal(z1): # real pole, complex zero z2 = z1.conj() p2_idx = _nearest_real_complex_idx(p, z1, 'real') p2 = p[p2_idx] assert np.isreal(p2) else: # real pole, real zero # pick the next "worst" pole to use idx = np.where(np.isreal(p))[0] assert len(idx) > 0 p2_idx = idx[np.argmin(np.abs(np.abs(p[idx]) - 1))] p2 = p[p2_idx] # find a real zero to match the added pole assert np.isreal(p2) z2_idx = _nearest_real_complex_idx(z, p2, 'real') z2 = z[z2_idx] assert np.isreal(z2) z = np.delete(z, z2_idx) p = np.delete(p, p2_idx) p_sos[si] = [p1, p2] z_sos[si] = [z1, z2] assert len(p) == len(z) == 0 # we've consumed all poles and zeros del p, z # Construct the system, reversing order so the "worst" are last p_sos = np.reshape(p_sos[::-1], (n_sections, 2)) z_sos = np.reshape(z_sos[::-1], (n_sections, 2)) gains = np.ones(n_sections) gains[0] = k for si in range(n_sections): x = zpk2tf(z_sos[si], p_sos[si], gains[si]) sos[si] = np.concatenate(x) return sos def _align_nums(nums): """Aligns the shapes of multiple numerators. Given an array of numerator coefficient arrays [[a_1, a_2,..., a_n],..., [b_1, b_2,..., b_m]], this function pads shorter numerator arrays with zero's so that all numerators have the same length. Such alignment is necessary for functions like 'tf2ss', which needs the alignment when dealing with SIMO transfer functions. Parameters ---------- nums: array_like Numerator or list of numerators. Not necessarily with same length. Returns ------- nums: array The numerator. If `nums` input was a list of numerators then a 2d array with padded zeros for shorter numerators is returned. Otherwise returns ``np.asarray(nums)``. """ try: # The statement can throw a ValueError if one # of the numerators is a single digit and another # is array-like e.g. if nums = [5, [1, 2, 3]] nums = asarray(nums) if not np.issubdtype(nums.dtype, np.number): raise ValueError("dtype of numerator is non-numeric") return nums except ValueError: nums = [np.atleast_1d(num) for num in nums] max_width = max(num.size for num in nums) # pre-allocate aligned_nums = np.zeros((len(nums), max_width)) # Create numerators with padded zeros for index, num in enumerate(nums): aligned_nums[index, -num.size:] = num return aligned_nums def normalize(b, a): """Normalize numerator/denominator of a continuous-time transfer function. If values of `b` are too close to 0, they are removed. In that case, a BadCoefficients warning is emitted. Parameters ---------- b: array_like Numerator of the transfer function. Can be a 2d array to normalize multiple transfer functions. a: array_like Denominator of the transfer function. At most 1d. Returns ------- num: array The numerator of the normalized transfer function. At least a 1d array. A 2d-array if the input `num` is a 2d array. den: 1d-array The denominator of the normalized transfer function. Notes ----- Coefficients for both the numerator and denominator should be specified in descending exponent order (e.g., ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``). """ num, den = b, a den = np.atleast_1d(den) num = np.atleast_2d(_align_nums(num)) if den.ndim != 1: raise ValueError("Denominator polynomial must be rank-1 array.") if num.ndim > 2: raise ValueError("Numerator polynomial must be rank-1 or" " rank-2 array.") if np.all(den == 0): raise ValueError("Denominator must have at least on nonzero element.") # Trim leading zeros in denominator, leave at least one. den = np.trim_zeros(den, 'f') # Normalize transfer function num, den = num / den[0], den / den[0] # Count numerator columns that are all zero leading_zeros = 0 for col in num.T: if np.allclose(col, 0, atol=1e-14): leading_zeros += 1 else: break # Trim leading zeros of numerator if leading_zeros > 0: warnings.warn("Badly conditioned filter coefficients (numerator): the " "results may be meaningless", BadCoefficients) # Make sure at least one column remains if leading_zeros == num.shape[1]: leading_zeros -= 1 num = num[:, leading_zeros:] # Squeeze first dimension if singular if num.shape[0] == 1: num = num[0, :] return num, den def lp2lp(b, a, wo=1.0): """ Transform a lowpass filter prototype to a different frequency. Return an analog low-pass filter with cutoff frequency `wo` from an analog low-pass filter prototype with unity cutoff frequency, in transfer function ('ba') representation. """ a, b = map(atleast_1d, (a, b)) try: wo = float(wo) except TypeError: wo = float(wo[0]) d = len(a) n = len(b) M = max((d, n)) pwo = pow(wo, numpy.arange(M - 1, -1, -1)) start1 = max((n - d, 0)) start2 = max((d - n, 0)) b = b * pwo[start1] / pwo[start2:] a = a * pwo[start1] / pwo[start1:] return normalize(b, a) def lp2hp(b, a, wo=1.0): """ Transform a lowpass filter prototype to a highpass filter. Return an analog high-pass filter with cutoff frequency `wo` from an analog low-pass filter prototype with unity cutoff frequency, in transfer function ('ba') representation. """ a, b = map(atleast_1d, (a, b)) try: wo = float(wo) except TypeError: wo = float(wo[0]) d = len(a) n = len(b) if wo != 1: pwo = pow(wo, numpy.arange(max((d, n)))) else: pwo = numpy.ones(max((d, n)), b.dtype.char) if d >= n: outa = a[::-1] * pwo outb = resize(b, (d,)) outb[n:] = 0.0 outb[:n] = b[::-1] * pwo[:n] else: outb = b[::-1] * pwo outa = resize(a, (n,)) outa[d:] = 0.0 outa[:d] = a[::-1] * pwo[:d] return normalize(outb, outa) def lp2bp(b, a, wo=1.0, bw=1.0): """ Transform a lowpass filter prototype to a bandpass filter. Return an analog band-pass filter with center frequency `wo` and bandwidth `bw` from an analog low-pass filter prototype with unity cutoff frequency, in transfer function ('ba') representation. """ a, b = map(atleast_1d, (a, b)) D = len(a) - 1 N = len(b) - 1 artype = mintypecode((a, b)) ma = max([N, D]) Np = N + ma Dp = D + ma bprime = numpy.zeros(Np + 1, artype) aprime = numpy.zeros(Dp + 1, artype) wosq = wo * wo for j in range(Np + 1): val = 0.0 for i in range(0, N + 1): for k in range(0, i + 1): if ma - i + 2 * k == j: val += comb(i, k) * b[N - i] * (wosq) ** (i - k) / bw ** i bprime[Np - j] = val for j in range(Dp + 1): val = 0.0 for i in range(0, D + 1): for k in range(0, i + 1): if ma - i + 2 * k == j: val += comb(i, k) * a[D - i] * (wosq) ** (i - k) / bw ** i aprime[Dp - j] = val return normalize(bprime, aprime) def lp2bs(b, a, wo=1.0, bw=1.0): """ Transform a lowpass filter prototype to a bandstop filter. Return an analog band-stop filter with center frequency `wo` and bandwidth `bw` from an analog low-pass filter prototype with unity cutoff frequency, in transfer function ('ba') representation. """ a, b = map(atleast_1d, (a, b)) D = len(a) - 1 N = len(b) - 1 artype = mintypecode((a, b)) M = max([N, D]) Np = M + M Dp = M + M bprime = numpy.zeros(Np + 1, artype) aprime = numpy.zeros(Dp + 1, artype) wosq = wo * wo for j in range(Np + 1): val = 0.0 for i in range(0, N + 1): for k in range(0, M - i + 1): if i + 2 * k == j: val += (comb(M - i, k) * b[N - i] * (wosq) ** (M - i - k) * bw ** i) bprime[Np - j] = val for j in range(Dp + 1): val = 0.0 for i in range(0, D + 1): for k in range(0, M - i + 1): if i + 2 * k == j: val += (comb(M - i, k) * a[D - i] * (wosq) ** (M - i - k) * bw ** i) aprime[Dp - j] = val return normalize(bprime, aprime) def bilinear(b, a, fs=1.0): """Return a digital filter from an analog one using a bilinear transform. The bilinear transform substitutes ``(z-1) / (z+1)`` for ``s``. """ fs = float(fs) a, b = map(atleast_1d, (a, b)) D = len(a) - 1 N = len(b) - 1 artype = float M = max([N, D]) Np = M Dp = M bprime = numpy.zeros(Np + 1, artype) aprime = numpy.zeros(Dp + 1, artype) for j in range(Np + 1): val = 0.0 for i in range(N + 1): for k in range(i + 1): for l in range(M - i + 1): if k + l == j: val += (comb(i, k) * comb(M - i, l) * b[N - i] * pow(2 * fs, i) * (-1) ** k) bprime[j] = real(val) for j in range(Dp + 1): val = 0.0 for i in range(D + 1): for k in range(i + 1): for l in range(M - i + 1): if k + l == j: val += (comb(i, k) * comb(M - i, l) * a[D - i] * pow(2 * fs, i) * (-1) ** k) aprime[j] = real(val) return normalize(bprime, aprime) def iirdesign(wp, ws, gpass, gstop, analog=False, ftype='ellip', output='ba'): """Complete IIR digital and analog filter design. Given passband and stopband frequencies and gains, construct an analog or digital IIR filter of minimum order for a given basic type. Return the output in numerator, denominator ('ba'), pole-zero ('zpk') or second order sections ('sos') form. Parameters ---------- wp, ws : float Passband and stopband edge frequencies. For digital filters, these are normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`wp` and `ws` are thus in half-cycles / sample.) For example: - Lowpass: wp = 0.2, ws = 0.3 - Highpass: wp = 0.3, ws = 0.2 - Bandpass: wp = [0.2, 0.5], ws = [0.1, 0.6] - Bandstop: wp = [0.1, 0.6], ws = [0.2, 0.5] For analog filters, `wp` and `ws` are angular frequencies (e.g. rad/s). gpass : float The maximum loss in the passband (dB). gstop : float The minimum attenuation in the stopband (dB). analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. ftype : str, optional The type of IIR filter to design: - Butterworth : 'butter' - Chebyshev I : 'cheby1' - Chebyshev II : 'cheby2' - Cauer/elliptic: 'ellip' - Bessel/Thomson: 'bessel' output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. See Also -------- butter : Filter design using order and critical points cheby1, cheby2, ellip, bessel buttord : Find order and critical points from passband and stopband spec cheb1ord, cheb2ord, ellipord iirfilter : General filter design using order and critical frequencies Notes ----- The ``'sos'`` output parameter was added in 0.16.0. """ try: ordfunc = filter_dict[ftype][1] except KeyError: raise ValueError("Invalid IIR filter type: %s" % ftype) except IndexError: raise ValueError(("%s does not have order selection. Use " "iirfilter function.") % ftype) wp = atleast_1d(wp) ws = atleast_1d(ws) band_type = 2 * (len(wp) - 1) band_type += 1 if wp[0] >= ws[0]: band_type += 1 btype = {1: 'lowpass', 2: 'highpass', 3: 'bandstop', 4: 'bandpass'}[band_type] N, Wn = ordfunc(wp, ws, gpass, gstop, analog=analog) return iirfilter(N, Wn, rp=gpass, rs=gstop, analog=analog, btype=btype, ftype=ftype, output=output) def iirfilter(N, Wn, rp=None, rs=None, btype='band', analog=False, ftype='butter', output='ba'): """ IIR digital and analog filter design given order and critical points. Design an Nth-order digital or analog filter and return the filter coefficients. Parameters ---------- N : int The order of the filter. Wn : array_like A scalar or length-2 sequence giving the critical frequencies. For digital filters, `Wn` is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`Wn` is thus in half-cycles / sample.) For analog filters, `Wn` is an angular frequency (e.g. rad/s). rp : float, optional For Chebyshev and elliptic filters, provides the maximum ripple in the passband. (dB) rs : float, optional For Chebyshev and elliptic filters, provides the minimum attenuation in the stop band. (dB) btype : {'bandpass', 'lowpass', 'highpass', 'bandstop'}, optional The type of filter. Default is 'bandpass'. analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. ftype : str, optional The type of IIR filter to design: - Butterworth : 'butter' - Chebyshev I : 'cheby1' - Chebyshev II : 'cheby2' - Cauer/elliptic: 'ellip' - Bessel/Thomson: 'bessel' output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. See Also -------- butter : Filter design using order and critical points cheby1, cheby2, ellip, bessel buttord : Find order and critical points from passband and stopband spec cheb1ord, cheb2ord, ellipord iirdesign : General filter design using passband and stopband spec Notes ----- The ``'sos'`` output parameter was added in 0.16.0. Examples -------- Generate a 17th-order Chebyshev II bandpass filter and plot the frequency response: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> b, a = signal.iirfilter(17, [50, 200], rs=60, btype='band', ... analog=True, ftype='cheby2') >>> w, h = signal.freqs(b, a, 1000) >>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> ax.semilogx(w, 20 * np.log10(abs(h))) >>> ax.set_title('Chebyshev Type II bandpass frequency response') >>> ax.set_xlabel('Frequency [radians / second]') >>> ax.set_ylabel('Amplitude [dB]') >>> ax.axis((10, 1000, -100, 10)) >>> ax.grid(which='both', axis='both') >>> plt.show() """ ftype, btype, output = [x.lower() for x in (ftype, btype, output)] Wn = asarray(Wn) try: btype = band_dict[btype] except KeyError: raise ValueError("'%s' is an invalid bandtype for filter." % btype) try: typefunc = filter_dict[ftype][0] except KeyError: raise ValueError("'%s' is not a valid basic IIR filter." % ftype) if output not in ['ba', 'zpk', 'sos']: raise ValueError("'%s' is not a valid output form." % output) if rp is not None and rp < 0: raise ValueError("passband ripple (rp) must be positive") if rs is not None and rs < 0: raise ValueError("stopband attenuation (rs) must be positive") # Get analog lowpass prototype if typefunc == buttap: z, p, k = typefunc(N) elif typefunc == besselap: z, p, k = typefunc(N, norm=bessel_norms[ftype]) elif typefunc == cheb1ap: if rp is None: raise ValueError("passband ripple (rp) must be provided to " "design a Chebyshev I filter.") z, p, k = typefunc(N, rp) elif typefunc == cheb2ap: if rs is None: raise ValueError("stopband attenuation (rs) must be provided to " "design an Chebyshev II filter.") z, p, k = typefunc(N, rs) elif typefunc == ellipap: if rs is None or rp is None: raise ValueError("Both rp and rs must be provided to design an " "elliptic filter.") z, p, k = typefunc(N, rp, rs) else: raise NotImplementedError("'%s' not implemented in iirfilter." % ftype) # Pre-warp frequencies for digital filter design if not analog: if numpy.any(Wn < 0) or numpy.any(Wn > 1): raise ValueError("Digital filter critical frequencies " "must be 0 <= Wn <= 1") fs = 2.0 warped = 2 * fs * tan(pi * Wn / fs) else: warped = Wn # transform to lowpass, bandpass, highpass, or bandstop if btype in ('lowpass', 'highpass'): if numpy.size(Wn) != 1: raise ValueError('Must specify a single critical frequency Wn') if btype == 'lowpass': z, p, k = _zpklp2lp(z, p, k, wo=warped) elif btype == 'highpass': z, p, k = _zpklp2hp(z, p, k, wo=warped) elif btype in ('bandpass', 'bandstop'): try: bw = warped[1] - warped[0] wo = sqrt(warped[0] * warped[1]) except IndexError: raise ValueError('Wn must specify start and stop frequencies') if btype == 'bandpass': z, p, k = _zpklp2bp(z, p, k, wo=wo, bw=bw) elif btype == 'bandstop': z, p, k = _zpklp2bs(z, p, k, wo=wo, bw=bw) else: raise NotImplementedError("'%s' not implemented in iirfilter." % btype) # Find discrete equivalent if necessary if not analog: z, p, k = _zpkbilinear(z, p, k, fs=fs) # Transform to proper out type (pole-zero, state-space, numer-denom) if output == 'zpk': return z, p, k elif output == 'ba': return zpk2tf(z, p, k) elif output == 'sos': return zpk2sos(z, p, k) def _relative_degree(z, p): """ Return relative degree of transfer function from zeros and poles """ degree = len(p) - len(z) if degree < 0: raise ValueError("Improper transfer function. " "Must have at least as many poles as zeros.") else: return degree # TODO: merge these into existing functions or make public versions def _zpkbilinear(z, p, k, fs): """ Return a digital filter from an analog one using a bilinear transform. Transform a set of poles and zeros from the analog s-plane to the digital z-plane using Tustin's method, which substitutes ``(z-1) / (z+1)`` for ``s``, maintaining the shape of the frequency response. Parameters ---------- z : array_like Zeros of the analog IIR filter transfer function. p : array_like Poles of the analog IIR filter transfer function. k : float System gain of the analog IIR filter transfer function. fs : float Sample rate, as ordinary frequency (e.g. hertz). No prewarping is done in this function. Returns ------- z : ndarray Zeros of the transformed digital filter transfer function. p : ndarray Poles of the transformed digital filter transfer function. k : float System gain of the transformed digital filter. """ z = atleast_1d(z) p = atleast_1d(p) degree = _relative_degree(z, p) fs2 = 2*fs # Bilinear transform the poles and zeros z_z = (fs2 + z) / (fs2 - z) p_z = (fs2 + p) / (fs2 - p) # Any zeros that were at infinity get moved to the Nyquist frequency z_z = append(z_z, -ones(degree)) # Compensate for gain change k_z = k * real(prod(fs2 - z) / prod(fs2 - p)) return z_z, p_z, k_z def _zpklp2lp(z, p, k, wo=1.0): r""" Transform a lowpass filter prototype to a different frequency. Return an analog low-pass filter with cutoff frequency `wo` from an analog low-pass filter prototype with unity cutoff frequency, using zeros, poles, and gain ('zpk') representation. Parameters ---------- z : array_like Zeros of the analog IIR filter transfer function. p : array_like Poles of the analog IIR filter transfer function. k : float System gain of the analog IIR filter transfer function. wo : float Desired cutoff, as angular frequency (e.g. rad/s). Defaults to no change. Returns ------- z : ndarray Zeros of the transformed low-pass filter transfer function. p : ndarray Poles of the transformed low-pass filter transfer function. k : float System gain of the transformed low-pass filter. Notes ----- This is derived from the s-plane substitution .. math:: s \rightarrow \frac{s}{\omega_0} """ z = atleast_1d(z) p = atleast_1d(p) wo = float(wo) # Avoid int wraparound degree = _relative_degree(z, p) # Scale all points radially from origin to shift cutoff frequency z_lp = wo * z p_lp = wo * p # Each shifted pole decreases gain by wo, each shifted zero increases it. # Cancel out the net change to keep overall gain the same k_lp = k * wo**degree return z_lp, p_lp, k_lp def _zpklp2hp(z, p, k, wo=1.0): r""" Transform a lowpass filter prototype to a highpass filter. Return an analog high-pass filter with cutoff frequency `wo` from an analog low-pass filter prototype with unity cutoff frequency, using zeros, poles, and gain ('zpk') representation. Parameters ---------- z : array_like Zeros of the analog IIR filter transfer function. p : array_like Poles of the analog IIR filter transfer function. k : float System gain of the analog IIR filter transfer function. wo : float Desired cutoff, as angular frequency (e.g. rad/s). Defaults to no change. Returns ------- z : ndarray Zeros of the transformed high-pass filter transfer function. p : ndarray Poles of the transformed high-pass filter transfer function. k : float System gain of the transformed high-pass filter. Notes ----- This is derived from the s-plane substitution .. math:: s \rightarrow \frac{\omega_0}{s} This maintains symmetry of the lowpass and highpass responses on a logarithmic scale. """ z = atleast_1d(z) p = atleast_1d(p) wo = float(wo) degree = _relative_degree(z, p) # Invert positions radially about unit circle to convert LPF to HPF # Scale all points radially from origin to shift cutoff frequency z_hp = wo / z p_hp = wo / p # If lowpass had zeros at infinity, inverting moves them to origin. z_hp = append(z_hp, zeros(degree)) # Cancel out gain change caused by inversion k_hp = k * real(prod(-z) / prod(-p)) return z_hp, p_hp, k_hp def _zpklp2bp(z, p, k, wo=1.0, bw=1.0): r""" Transform a lowpass filter prototype to a bandpass filter. Return an analog band-pass filter with center frequency `wo` and bandwidth `bw` from an analog low-pass filter prototype with unity cutoff frequency, using zeros, poles, and gain ('zpk') representation. Parameters ---------- z : array_like Zeros of the analog IIR filter transfer function. p : array_like Poles of the analog IIR filter transfer function. k : float System gain of the analog IIR filter transfer function. wo : float Desired passband center, as angular frequency (e.g. rad/s). Defaults to no change. bw : float Desired passband width, as angular frequency (e.g. rad/s). Defaults to 1. Returns ------- z : ndarray Zeros of the transformed band-pass filter transfer function. p : ndarray Poles of the transformed band-pass filter transfer function. k : float System gain of the transformed band-pass filter. Notes ----- This is derived from the s-plane substitution .. math:: s \rightarrow \frac{s^2 + {\omega_0}^2}{s \cdot \mathrm{BW}} This is the "wideband" transformation, producing a passband with geometric (log frequency) symmetry about `wo`. """ z = atleast_1d(z) p = atleast_1d(p) wo = float(wo) bw = float(bw) degree = _relative_degree(z, p) # Scale poles and zeros to desired bandwidth z_lp = z * bw/2 p_lp = p * bw/2 # Square root needs to produce complex result, not NaN z_lp = z_lp.astype(complex) p_lp = p_lp.astype(complex) # Duplicate poles and zeros and shift from baseband to +wo and -wo z_bp = concatenate((z_lp + sqrt(z_lp**2 - wo**2), z_lp - sqrt(z_lp**2 - wo**2))) p_bp = concatenate((p_lp + sqrt(p_lp**2 - wo**2), p_lp - sqrt(p_lp**2 - wo**2))) # Move degree zeros to origin, leaving degree zeros at infinity for BPF z_bp = append(z_bp, zeros(degree)) # Cancel out gain change from frequency scaling k_bp = k * bw**degree return z_bp, p_bp, k_bp def _zpklp2bs(z, p, k, wo=1.0, bw=1.0): r""" Transform a lowpass filter prototype to a bandstop filter. Return an analog band-stop filter with center frequency `wo` and stopband width `bw` from an analog low-pass filter prototype with unity cutoff frequency, using zeros, poles, and gain ('zpk') representation. Parameters ---------- z : array_like Zeros of the analog IIR filter transfer function. p : array_like Poles of the analog IIR filter transfer function. k : float System gain of the analog IIR filter transfer function. wo : float Desired stopband center, as angular frequency (e.g. rad/s). Defaults to no change. bw : float Desired stopband width, as angular frequency (e.g. rad/s). Defaults to 1. Returns ------- z : ndarray Zeros of the transformed band-stop filter transfer function. p : ndarray Poles of the transformed band-stop filter transfer function. k : float System gain of the transformed band-stop filter. Notes ----- This is derived from the s-plane substitution .. math:: s \rightarrow \frac{s \cdot \mathrm{BW}}{s^2 + {\omega_0}^2} This is the "wideband" transformation, producing a stopband with geometric (log frequency) symmetry about `wo`. """ z = atleast_1d(z) p = atleast_1d(p) wo = float(wo) bw = float(bw) degree = _relative_degree(z, p) # Invert to a highpass filter with desired bandwidth z_hp = (bw/2) / z p_hp = (bw/2) / p # Square root needs to produce complex result, not NaN z_hp = z_hp.astype(complex) p_hp = p_hp.astype(complex) # Duplicate poles and zeros and shift from baseband to +wo and -wo z_bs = concatenate((z_hp + sqrt(z_hp**2 - wo**2), z_hp - sqrt(z_hp**2 - wo**2))) p_bs = concatenate((p_hp + sqrt(p_hp**2 - wo**2), p_hp - sqrt(p_hp**2 - wo**2))) # Move any zeros that were at infinity to the center of the stopband z_bs = append(z_bs, +1j*wo * ones(degree)) z_bs = append(z_bs, -1j*wo * ones(degree)) # Cancel out gain change caused by inversion k_bs = k * real(prod(-z) / prod(-p)) return z_bs, p_bs, k_bs def butter(N, Wn, btype='low', analog=False, output='ba'): """ Butterworth digital and analog filter design. Design an Nth-order digital or analog Butterworth filter and return the filter coefficients. Parameters ---------- N : int The order of the filter. Wn : array_like A scalar or length-2 sequence giving the critical frequencies. For a Butterworth filter, this is the point at which the gain drops to 1/sqrt(2) that of the passband (the "-3 dB point"). For digital filters, `Wn` is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`Wn` is thus in half-cycles / sample.) For analog filters, `Wn` is an angular frequency (e.g. rad/s). btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional The type of filter. Default is 'lowpass'. analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. See Also -------- buttord, buttap Notes ----- The Butterworth filter has maximally flat frequency response in the passband. The ``'sos'`` output parameter was added in 0.16.0. Examples -------- Plot the filter's frequency response, showing the critical points: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> b, a = signal.butter(4, 100, 'low', analog=True) >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.title('Butterworth filter frequency response') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.axvline(100, color='green') # cutoff frequency >>> plt.show() """ return iirfilter(N, Wn, btype=btype, analog=analog, output=output, ftype='butter') def cheby1(N, rp, Wn, btype='low', analog=False, output='ba'): """ Chebyshev type I digital and analog filter design. Design an Nth-order digital or analog Chebyshev type I filter and return the filter coefficients. Parameters ---------- N : int The order of the filter. rp : float The maximum ripple allowed below unity gain in the passband. Specified in decibels, as a positive number. Wn : array_like A scalar or length-2 sequence giving the critical frequencies. For Type I filters, this is the point in the transition band at which the gain first drops below -`rp`. For digital filters, `Wn` is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`Wn` is thus in half-cycles / sample.) For analog filters, `Wn` is an angular frequency (e.g. rad/s). btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional The type of filter. Default is 'lowpass'. analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. See Also -------- cheb1ord, cheb1ap Notes ----- The Chebyshev type I filter maximizes the rate of cutoff between the frequency response's passband and stopband, at the expense of ripple in the passband and increased ringing in the step response. Type I filters roll off faster than Type II (`cheby2`), but Type II filters do not have any ripple in the passband. The equiripple passband has N maxima or minima (for example, a 5th-order filter has 3 maxima and 2 minima). Consequently, the DC gain is unity for odd-order filters, or -rp dB for even-order filters. The ``'sos'`` output parameter was added in 0.16.0. Examples -------- Plot the filter's frequency response, showing the critical points: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> b, a = signal.cheby1(4, 5, 100, 'low', analog=True) >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.title('Chebyshev Type I frequency response (rp=5)') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.axvline(100, color='green') # cutoff frequency >>> plt.axhline(-5, color='green') # rp >>> plt.show() """ return iirfilter(N, Wn, rp=rp, btype=btype, analog=analog, output=output, ftype='cheby1') def cheby2(N, rs, Wn, btype='low', analog=False, output='ba'): """ Chebyshev type II digital and analog filter design. Design an Nth-order digital or analog Chebyshev type II filter and return the filter coefficients. Parameters ---------- N : int The order of the filter. rs : float The minimum attenuation required in the stop band. Specified in decibels, as a positive number. Wn : array_like A scalar or length-2 sequence giving the critical frequencies. For Type II filters, this is the point in the transition band at which the gain first reaches -`rs`. For digital filters, `Wn` is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`Wn` is thus in half-cycles / sample.) For analog filters, `Wn` is an angular frequency (e.g. rad/s). btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional The type of filter. Default is 'lowpass'. analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. See Also -------- cheb2ord, cheb2ap Notes ----- The Chebyshev type II filter maximizes the rate of cutoff between the frequency response's passband and stopband, at the expense of ripple in the stopband and increased ringing in the step response. Type II filters do not roll off as fast as Type I (`cheby1`). The ``'sos'`` output parameter was added in 0.16.0. Examples -------- Plot the filter's frequency response, showing the critical points: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> b, a = signal.cheby2(4, 40, 100, 'low', analog=True) >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.title('Chebyshev Type II frequency response (rs=40)') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.axvline(100, color='green') # cutoff frequency >>> plt.axhline(-40, color='green') # rs >>> plt.show() """ return iirfilter(N, Wn, rs=rs, btype=btype, analog=analog, output=output, ftype='cheby2') def ellip(N, rp, rs, Wn, btype='low', analog=False, output='ba'): """ Elliptic (Cauer) digital and analog filter design. Design an Nth-order digital or analog elliptic filter and return the filter coefficients. Parameters ---------- N : int The order of the filter. rp : float The maximum ripple allowed below unity gain in the passband. Specified in decibels, as a positive number. rs : float The minimum attenuation required in the stop band. Specified in decibels, as a positive number. Wn : array_like A scalar or length-2 sequence giving the critical frequencies. For elliptic filters, this is the point in the transition band at which the gain first drops below -`rp`. For digital filters, `Wn` is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`Wn` is thus in half-cycles / sample.) For analog filters, `Wn` is an angular frequency (e.g. rad/s). btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional The type of filter. Default is 'lowpass'. analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. See Also -------- ellipord, ellipap Notes ----- Also known as Cauer or Zolotarev filters, the elliptical filter maximizes the rate of transition between the frequency response's passband and stopband, at the expense of ripple in both, and increased ringing in the step response. As `rp` approaches 0, the elliptical filter becomes a Chebyshev type II filter (`cheby2`). As `rs` approaches 0, it becomes a Chebyshev type I filter (`cheby1`). As both approach 0, it becomes a Butterworth filter (`butter`). The equiripple passband has N maxima or minima (for example, a 5th-order filter has 3 maxima and 2 minima). Consequently, the DC gain is unity for odd-order filters, or -rp dB for even-order filters. The ``'sos'`` output parameter was added in 0.16.0. Examples -------- Plot the filter's frequency response, showing the critical points: >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> b, a = signal.ellip(4, 5, 40, 100, 'low', analog=True) >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.title('Elliptic filter frequency response (rp=5, rs=40)') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.axvline(100, color='green') # cutoff frequency >>> plt.axhline(-40, color='green') # rs >>> plt.axhline(-5, color='green') # rp >>> plt.show() """ return iirfilter(N, Wn, rs=rs, rp=rp, btype=btype, analog=analog, output=output, ftype='elliptic') def bessel(N, Wn, btype='low', analog=False, output='ba', norm='phase'): """ Bessel/Thomson digital and analog filter design. Design an Nth-order digital or analog Bessel filter and return the filter coefficients. Parameters ---------- N : int The order of the filter. Wn : array_like A scalar or length-2 sequence giving the critical frequencies (defined by the `norm` parameter). For analog filters, `Wn` is an angular frequency (e.g. rad/s). For digital filters, `Wn` is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`Wn` is thus in half-cycles / sample.) btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional The type of filter. Default is 'lowpass'. analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. (See Notes.) output : {'ba', 'zpk', 'sos'}, optional Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), or second-order sections ('sos'). Default is 'ba'. norm : {'phase', 'delay', 'mag'}, optional Critical frequency normalization: ``phase`` The filter is normalized such that the phase response reaches its midpoint at angular (e.g. rad/s) frequency `Wn`. This happens for both low-pass and high-pass filters, so this is the "phase-matched" case. The magnitude response asymptotes are the same as a Butterworth filter of the same order with a cutoff of `Wn`. This is the default, and matches MATLAB's implementation. ``delay`` The filter is normalized such that the group delay in the passband is 1/`Wn` (e.g. seconds). This is the "natural" type obtained by solving Bessel polynomials. ``mag`` The filter is normalized such that the gain magnitude is -3 dB at angular frequency `Wn`. .. versionadded:: 0.18.0 Returns ------- b, a : ndarray, ndarray Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. Only returned if ``output='ba'``. z, p, k : ndarray, ndarray, float Zeros, poles, and system gain of the IIR filter transfer function. Only returned if ``output='zpk'``. sos : ndarray Second-order sections representation of the IIR filter. Only returned if ``output=='sos'``. Notes ----- Also known as a Thomson filter, the analog Bessel filter has maximally flat group delay and maximally linear phase response, with very little ringing in the step response. [1]_ The Bessel is inherently an analog filter. This function generates digital Bessel filters using the bilinear transform, which does not preserve the phase response of the analog filter. As such, it is only approximately correct at frequencies below about fs/4. To get maximally-flat group delay at higher frequencies, the analog Bessel filter must be transformed using phase-preserving techniques. See `besselap` for implementation details and references. The ``'sos'`` output parameter was added in 0.16.0. Examples -------- Plot the phase-normalized frequency response, showing the relationship to the Butterworth's cutoff frequency (green): >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> b, a = signal.butter(4, 100, 'low', analog=True) >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(np.abs(h)), color='silver', ls='dashed') >>> b, a = signal.bessel(4, 100, 'low', analog=True, norm='phase') >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(np.abs(h))) >>> plt.title('Bessel filter magnitude response (with Butterworth)') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.axvline(100, color='green') # cutoff frequency >>> plt.show() and the phase midpoint: >>> plt.figure() >>> plt.semilogx(w, np.unwrap(np.angle(h))) >>> plt.axvline(100, color='green') # cutoff frequency >>> plt.axhline(-np.pi, color='red') # phase midpoint >>> plt.title('Bessel filter phase response') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Phase [radians]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.show() Plot the magnitude-normalized frequency response, showing the -3 dB cutoff: >>> b, a = signal.bessel(3, 10, 'low', analog=True, norm='mag') >>> w, h = signal.freqs(b, a) >>> plt.semilogx(w, 20 * np.log10(np.abs(h))) >>> plt.axhline(-3, color='red') # -3 dB magnitude >>> plt.axvline(10, color='green') # cutoff frequency >>> plt.title('Magnitude-normalized Bessel filter frequency response') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.show() Plot the delay-normalized filter, showing the maximally-flat group delay at 0.1 seconds: >>> b, a = signal.bessel(5, 1/0.1, 'low', analog=True, norm='delay') >>> w, h = signal.freqs(b, a) >>> plt.figure() >>> plt.semilogx(w[1:], -np.diff(np.unwrap(np.angle(h)))/np.diff(w)) >>> plt.axhline(0.1, color='red') # 0.1 seconds group delay >>> plt.title('Bessel filter group delay') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Group delay [seconds]') >>> plt.margins(0, 0.1) >>> plt.grid(which='both', axis='both') >>> plt.show() References ---------- .. [1] Thomson, W.E., "Delay Networks having Maximally Flat Frequency Characteristics", Proceedings of the Institution of Electrical Engineers, Part III, November 1949, Vol. 96, No. 44, pp. 487-490. """ return iirfilter(N, Wn, btype=btype, analog=analog, output=output, ftype='bessel_'+norm) def maxflat(): pass def yulewalk(): pass def band_stop_obj(wp, ind, passb, stopb, gpass, gstop, type): """ Band Stop Objective Function for order minimization. Returns the non-integer order for an analog band stop filter. Parameters ---------- wp : scalar Edge of passband `passb`. ind : int, {0, 1} Index specifying which `passb` edge to vary (0 or 1). passb : ndarray Two element sequence of fixed passband edges. stopb : ndarray Two element sequence of fixed stopband edges. gstop : float Amount of attenuation in stopband in dB. gpass : float Amount of ripple in the passband in dB. type : {'butter', 'cheby', 'ellip'} Type of filter. Returns ------- n : scalar Filter order (possibly non-integer). """ passbC = passb.copy() passbC[ind] = wp nat = (stopb * (passbC[0] - passbC[1]) / (stopb ** 2 - passbC[0] * passbC[1])) nat = min(abs(nat)) if type == 'butter': GSTOP = 10 ** (0.1 * abs(gstop)) GPASS = 10 ** (0.1 * abs(gpass)) n = (log10((GSTOP - 1.0) / (GPASS - 1.0)) / (2 * log10(nat))) elif type == 'cheby': GSTOP = 10 ** (0.1 * abs(gstop)) GPASS = 10 ** (0.1 * abs(gpass)) n = arccosh(sqrt((GSTOP - 1.0) / (GPASS - 1.0))) / arccosh(nat) elif type == 'ellip': GSTOP = 10 ** (0.1 * gstop) GPASS = 10 ** (0.1 * gpass) arg1 = sqrt((GPASS - 1.0) / (GSTOP - 1.0)) arg0 = 1.0 / nat d0 = special.ellipk([arg0 ** 2, 1 - arg0 ** 2]) d1 = special.ellipk([arg1 ** 2, 1 - arg1 ** 2]) n = (d0[0] * d1[1] / (d0[1] * d1[0])) else: raise ValueError("Incorrect type: %s" % type) return n def buttord(wp, ws, gpass, gstop, analog=False): """Butterworth filter order selection. Return the order of the lowest order digital or analog Butterworth filter that loses no more than `gpass` dB in the passband and has at least `gstop` dB attenuation in the stopband. Parameters ---------- wp, ws : float Passband and stopband edge frequencies. For digital filters, these are normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`wp` and `ws` are thus in half-cycles / sample.) For example: - Lowpass: wp = 0.2, ws = 0.3 - Highpass: wp = 0.3, ws = 0.2 - Bandpass: wp = [0.2, 0.5], ws = [0.1, 0.6] - Bandstop: wp = [0.1, 0.6], ws = [0.2, 0.5] For analog filters, `wp` and `ws` are angular frequencies (e.g. rad/s). gpass : float The maximum loss in the passband (dB). gstop : float The minimum attenuation in the stopband (dB). analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. Returns ------- ord : int The lowest order for a Butterworth filter which meets specs. wn : ndarray or float The Butterworth natural frequency (i.e. the "3dB frequency"). Should be used with `butter` to give filter results. See Also -------- butter : Filter design using order and critical points cheb1ord : Find order and critical points from passband and stopband spec cheb2ord, ellipord iirfilter : General filter design using order and critical frequencies iirdesign : General filter design using passband and stopband spec Examples -------- Design an analog bandpass filter with passband within 3 dB from 20 to 50 rad/s, while rejecting at least -40 dB below 14 and above 60 rad/s. Plot its frequency response, showing the passband and stopband constraints in gray. >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> N, Wn = signal.buttord([20, 50], [14, 60], 3, 40, True) >>> b, a = signal.butter(N, Wn, 'band', True) >>> w, h = signal.freqs(b, a, np.logspace(1, 2, 500)) >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.title('Butterworth bandpass filter fit to constraints') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.grid(which='both', axis='both') >>> plt.fill([1, 14, 14, 1], [-40, -40, 99, 99], '0.9', lw=0) # stop >>> plt.fill([20, 20, 50, 50], [-99, -3, -3, -99], '0.9', lw=0) # pass >>> plt.fill([60, 60, 1e9, 1e9], [99, -40, -40, 99], '0.9', lw=0) # stop >>> plt.axis([10, 100, -60, 3]) >>> plt.show() """ wp = atleast_1d(wp) ws = atleast_1d(ws) filter_type = 2 * (len(wp) - 1) filter_type += 1 if wp[0] >= ws[0]: filter_type += 1 # Pre-warp frequencies for digital filter design if not analog: passb = tan(pi * wp / 2.0) stopb = tan(pi * ws / 2.0) else: passb = wp * 1.0 stopb = ws * 1.0 if filter_type == 1: # low nat = stopb / passb elif filter_type == 2: # high nat = passb / stopb elif filter_type == 3: # stop wp0 = optimize.fminbound(band_stop_obj, passb[0], stopb[0] - 1e-12, args=(0, passb, stopb, gpass, gstop, 'butter'), disp=0) passb[0] = wp0 wp1 = optimize.fminbound(band_stop_obj, stopb[1] + 1e-12, passb[1], args=(1, passb, stopb, gpass, gstop, 'butter'), disp=0) passb[1] = wp1 nat = ((stopb * (passb[0] - passb[1])) / (stopb ** 2 - passb[0] * passb[1])) elif filter_type == 4: # pass nat = ((stopb ** 2 - passb[0] * passb[1]) / (stopb * (passb[0] - passb[1]))) nat = min(abs(nat)) GSTOP = 10 ** (0.1 * abs(gstop)) GPASS = 10 ** (0.1 * abs(gpass)) ord = int(ceil(log10((GSTOP - 1.0) / (GPASS - 1.0)) / (2 * log10(nat)))) # Find the Butterworth natural frequency WN (or the "3dB" frequency") # to give exactly gpass at passb. try: W0 = (GPASS - 1.0) ** (-1.0 / (2.0 * ord)) except ZeroDivisionError: W0 = 1.0 print("Warning, order is zero...check input parameters.") # now convert this frequency back from lowpass prototype # to the original analog filter if filter_type == 1: # low WN = W0 * passb elif filter_type == 2: # high WN = passb / W0 elif filter_type == 3: # stop WN = numpy.zeros(2, float) discr = sqrt((passb[1] - passb[0]) ** 2 + 4 * W0 ** 2 * passb[0] * passb[1]) WN[0] = ((passb[1] - passb[0]) + discr) / (2 * W0) WN[1] = ((passb[1] - passb[0]) - discr) / (2 * W0) WN = numpy.sort(abs(WN)) elif filter_type == 4: # pass W0 = numpy.array([-W0, W0], float) WN = (-W0 * (passb[1] - passb[0]) / 2.0 + sqrt(W0 ** 2 / 4.0 * (passb[1] - passb[0]) ** 2 + passb[0] * passb[1])) WN = numpy.sort(abs(WN)) else: raise ValueError("Bad type: %s" % filter_type) if not analog: wn = (2.0 / pi) * arctan(WN) else: wn = WN if len(wn) == 1: wn = wn[0] return ord, wn def cheb1ord(wp, ws, gpass, gstop, analog=False): """Chebyshev type I filter order selection. Return the order of the lowest order digital or analog Chebyshev Type I filter that loses no more than `gpass` dB in the passband and has at least `gstop` dB attenuation in the stopband. Parameters ---------- wp, ws : float Passband and stopband edge frequencies. For digital filters, these are normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`wp` and `ws` are thus in half-cycles / sample.) For example: - Lowpass: wp = 0.2, ws = 0.3 - Highpass: wp = 0.3, ws = 0.2 - Bandpass: wp = [0.2, 0.5], ws = [0.1, 0.6] - Bandstop: wp = [0.1, 0.6], ws = [0.2, 0.5] For analog filters, `wp` and `ws` are angular frequencies (e.g. rad/s). gpass : float The maximum loss in the passband (dB). gstop : float The minimum attenuation in the stopband (dB). analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. Returns ------- ord : int The lowest order for a Chebyshev type I filter that meets specs. wn : ndarray or float The Chebyshev natural frequency (the "3dB frequency") for use with `cheby1` to give filter results. See Also -------- cheby1 : Filter design using order and critical points buttord : Find order and critical points from passband and stopband spec cheb2ord, ellipord iirfilter : General filter design using order and critical frequencies iirdesign : General filter design using passband and stopband spec Examples -------- Design a digital lowpass filter such that the passband is within 3 dB up to 0.2*(fs/2), while rejecting at least -40 dB above 0.3*(fs/2). Plot its frequency response, showing the passband and stopband constraints in gray. >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> N, Wn = signal.cheb1ord(0.2, 0.3, 3, 40) >>> b, a = signal.cheby1(N, 3, Wn, 'low') >>> w, h = signal.freqz(b, a) >>> plt.semilogx(w / np.pi, 20 * np.log10(abs(h))) >>> plt.title('Chebyshev I lowpass filter fit to constraints') >>> plt.xlabel('Normalized frequency') >>> plt.ylabel('Amplitude [dB]') >>> plt.grid(which='both', axis='both') >>> plt.fill([.01, 0.2, 0.2, .01], [-3, -3, -99, -99], '0.9', lw=0) # stop >>> plt.fill([0.3, 0.3, 2, 2], [ 9, -40, -40, 9], '0.9', lw=0) # pass >>> plt.axis([0.08, 1, -60, 3]) >>> plt.show() """ wp = atleast_1d(wp) ws = atleast_1d(ws) filter_type = 2 * (len(wp) - 1) if wp[0] < ws[0]: filter_type += 1 else: filter_type += 2 # Pre-warp frequencies for digital filter design if not analog: passb = tan(pi * wp / 2.0) stopb = tan(pi * ws / 2.0) else: passb = wp * 1.0 stopb = ws * 1.0 if filter_type == 1: # low nat = stopb / passb elif filter_type == 2: # high nat = passb / stopb elif filter_type == 3: # stop wp0 = optimize.fminbound(band_stop_obj, passb[0], stopb[0] - 1e-12, args=(0, passb, stopb, gpass, gstop, 'cheby'), disp=0) passb[0] = wp0 wp1 = optimize.fminbound(band_stop_obj, stopb[1] + 1e-12, passb[1], args=(1, passb, stopb, gpass, gstop, 'cheby'), disp=0) passb[1] = wp1 nat = ((stopb * (passb[0] - passb[1])) / (stopb ** 2 - passb[0] * passb[1])) elif filter_type == 4: # pass nat = ((stopb ** 2 - passb[0] * passb[1]) / (stopb * (passb[0] - passb[1]))) nat = min(abs(nat)) GSTOP = 10 ** (0.1 * abs(gstop)) GPASS = 10 ** (0.1 * abs(gpass)) ord = int(ceil(arccosh(sqrt((GSTOP - 1.0) / (GPASS - 1.0))) / arccosh(nat))) # Natural frequencies are just the passband edges if not analog: wn = (2.0 / pi) * arctan(passb) else: wn = passb if len(wn) == 1: wn = wn[0] return ord, wn def cheb2ord(wp, ws, gpass, gstop, analog=False): """Chebyshev type II filter order selection. Return the order of the lowest order digital or analog Chebyshev Type II filter that loses no more than `gpass` dB in the passband and has at least `gstop` dB attenuation in the stopband. Parameters ---------- wp, ws : float Passband and stopband edge frequencies. For digital filters, these are normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`wp` and `ws` are thus in half-cycles / sample.) For example: - Lowpass: wp = 0.2, ws = 0.3 - Highpass: wp = 0.3, ws = 0.2 - Bandpass: wp = [0.2, 0.5], ws = [0.1, 0.6] - Bandstop: wp = [0.1, 0.6], ws = [0.2, 0.5] For analog filters, `wp` and `ws` are angular frequencies (e.g. rad/s). gpass : float The maximum loss in the passband (dB). gstop : float The minimum attenuation in the stopband (dB). analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. Returns ------- ord : int The lowest order for a Chebyshev type II filter that meets specs. wn : ndarray or float The Chebyshev natural frequency (the "3dB frequency") for use with `cheby2` to give filter results. See Also -------- cheby2 : Filter design using order and critical points buttord : Find order and critical points from passband and stopband spec cheb1ord, ellipord iirfilter : General filter design using order and critical frequencies iirdesign : General filter design using passband and stopband spec Examples -------- Design a digital bandstop filter which rejects -60 dB from 0.2*(fs/2) to 0.5*(fs/2), while staying within 3 dB below 0.1*(fs/2) or above 0.6*(fs/2). Plot its frequency response, showing the passband and stopband constraints in gray. >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> N, Wn = signal.cheb2ord([0.1, 0.6], [0.2, 0.5], 3, 60) >>> b, a = signal.cheby2(N, 60, Wn, 'stop') >>> w, h = signal.freqz(b, a) >>> plt.semilogx(w / np.pi, 20 * np.log10(abs(h))) >>> plt.title('Chebyshev II bandstop filter fit to constraints') >>> plt.xlabel('Normalized frequency') >>> plt.ylabel('Amplitude [dB]') >>> plt.grid(which='both', axis='both') >>> plt.fill([.01, .1, .1, .01], [-3, -3, -99, -99], '0.9', lw=0) # stop >>> plt.fill([.2, .2, .5, .5], [ 9, -60, -60, 9], '0.9', lw=0) # pass >>> plt.fill([.6, .6, 2, 2], [-99, -3, -3, -99], '0.9', lw=0) # stop >>> plt.axis([0.06, 1, -80, 3]) >>> plt.show() """ wp = atleast_1d(wp) ws = atleast_1d(ws) filter_type = 2 * (len(wp) - 1) if wp[0] < ws[0]: filter_type += 1 else: filter_type += 2 # Pre-warp frequencies for digital filter design if not analog: passb = tan(pi * wp / 2.0) stopb = tan(pi * ws / 2.0) else: passb = wp * 1.0 stopb = ws * 1.0 if filter_type == 1: # low nat = stopb / passb elif filter_type == 2: # high nat = passb / stopb elif filter_type == 3: # stop wp0 = optimize.fminbound(band_stop_obj, passb[0], stopb[0] - 1e-12, args=(0, passb, stopb, gpass, gstop, 'cheby'), disp=0) passb[0] = wp0 wp1 = optimize.fminbound(band_stop_obj, stopb[1] + 1e-12, passb[1], args=(1, passb, stopb, gpass, gstop, 'cheby'), disp=0) passb[1] = wp1 nat = ((stopb * (passb[0] - passb[1])) / (stopb ** 2 - passb[0] * passb[1])) elif filter_type == 4: # pass nat = ((stopb ** 2 - passb[0] * passb[1]) / (stopb * (passb[0] - passb[1]))) nat = min(abs(nat)) GSTOP = 10 ** (0.1 * abs(gstop)) GPASS = 10 ** (0.1 * abs(gpass)) ord = int(ceil(arccosh(sqrt((GSTOP - 1.0) / (GPASS - 1.0))) / arccosh(nat))) # Find frequency where analog response is -gpass dB. # Then convert back from low-pass prototype to the original filter. new_freq = cosh(1.0 / ord * arccosh(sqrt((GSTOP - 1.0) / (GPASS - 1.0)))) new_freq = 1.0 / new_freq if filter_type == 1: nat = passb / new_freq elif filter_type == 2: nat = passb * new_freq elif filter_type == 3: nat = numpy.zeros(2, float) nat[0] = (new_freq / 2.0 * (passb[0] - passb[1]) + sqrt(new_freq ** 2 * (passb[1] - passb[0]) ** 2 / 4.0 + passb[1] * passb[0])) nat[1] = passb[1] * passb[0] / nat[0] elif filter_type == 4: nat = numpy.zeros(2, float) nat[0] = (1.0 / (2.0 * new_freq) * (passb[0] - passb[1]) + sqrt((passb[1] - passb[0]) ** 2 / (4.0 * new_freq ** 2) + passb[1] * passb[0])) nat[1] = passb[0] * passb[1] / nat[0] if not analog: wn = (2.0 / pi) * arctan(nat) else: wn = nat if len(wn) == 1: wn = wn[0] return ord, wn def ellipord(wp, ws, gpass, gstop, analog=False): """Elliptic (Cauer) filter order selection. Return the order of the lowest order digital or analog elliptic filter that loses no more than `gpass` dB in the passband and has at least `gstop` dB attenuation in the stopband. Parameters ---------- wp, ws : float Passband and stopband edge frequencies. For digital filters, these are normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (`wp` and `ws` are thus in half-cycles / sample.) For example: - Lowpass: wp = 0.2, ws = 0.3 - Highpass: wp = 0.3, ws = 0.2 - Bandpass: wp = [0.2, 0.5], ws = [0.1, 0.6] - Bandstop: wp = [0.1, 0.6], ws = [0.2, 0.5] For analog filters, `wp` and `ws` are angular frequencies (e.g. rad/s). gpass : float The maximum loss in the passband (dB). gstop : float The minimum attenuation in the stopband (dB). analog : bool, optional When True, return an analog filter, otherwise a digital filter is returned. Returns ------- ord : int The lowest order for an Elliptic (Cauer) filter that meets specs. wn : ndarray or float The Chebyshev natural frequency (the "3dB frequency") for use with `ellip` to give filter results. See Also -------- ellip : Filter design using order and critical points buttord : Find order and critical points from passband and stopband spec cheb1ord, cheb2ord iirfilter : General filter design using order and critical frequencies iirdesign : General filter design using passband and stopband spec Examples -------- Design an analog highpass filter such that the passband is within 3 dB above 30 rad/s, while rejecting -60 dB at 10 rad/s. Plot its frequency response, showing the passband and stopband constraints in gray. >>> from scipy import signal >>> import matplotlib.pyplot as plt >>> N, Wn = signal.ellipord(30, 10, 3, 60, True) >>> b, a = signal.ellip(N, 3, 60, Wn, 'high', True) >>> w, h = signal.freqs(b, a, np.logspace(0, 3, 500)) >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.title('Elliptical highpass filter fit to constraints') >>> plt.xlabel('Frequency [radians / second]') >>> plt.ylabel('Amplitude [dB]') >>> plt.grid(which='both', axis='both') >>> plt.fill([.1, 10, 10, .1], [1e4, 1e4, -60, -60], '0.9', lw=0) # stop >>> plt.fill([30, 30, 1e9, 1e9], [-99, -3, -3, -99], '0.9', lw=0) # pass >>> plt.axis([1, 300, -80, 3]) >>> plt.show() """ wp = atleast_1d(wp) ws = atleast_1d(ws) filter_type = 2 * (len(wp) - 1) filter_type += 1 if wp[0] >= ws[0]: filter_type += 1 # Pre-warp frequencies for digital filter design if not analog: passb = tan(pi * wp / 2.0) stopb = tan(pi * ws / 2.0) else: passb = wp * 1.0 stopb = ws * 1.0 if filter_type == 1: # low nat = stopb / passb elif filter_type == 2: # high nat = passb / stopb elif filter_type == 3: # stop wp0 = optimize.fminbound(band_stop_obj, passb[0], stopb[0] - 1e-12, args=(0, passb, stopb, gpass, gstop, 'ellip'), disp=0) passb[0] = wp0 wp1 = optimize.fminbound(band_stop_obj, stopb[1] + 1e-12, passb[1], args=(1, passb, stopb, gpass, gstop, 'ellip'), disp=0) passb[1] = wp1 nat = ((stopb * (passb[0] - passb[1])) / (stopb ** 2 - passb[0] * passb[1])) elif filter_type == 4: # pass nat = ((stopb ** 2 - passb[0] * passb[1]) / (stopb * (passb[0] - passb[1]))) nat = min(abs(nat)) GSTOP = 10 ** (0.1 * gstop) GPASS = 10 ** (0.1 * gpass) arg1 = sqrt((GPASS - 1.0) / (GSTOP - 1.0)) arg0 = 1.0 / nat d0 = special.ellipk([arg0 ** 2, 1 - arg0 ** 2]) d1 = special.ellipk([arg1 ** 2, 1 - arg1 ** 2]) ord = int(ceil(d0[0] * d1[1] / (d0[1] * d1[0]))) if not analog: wn = arctan(passb) * 2.0 / pi else: wn = passb if len(wn) == 1: wn = wn[0] return ord, wn def buttap(N): """Return (z,p,k) for analog prototype of Nth-order Butterworth filter. The filter will have an angular (e.g. rad/s) cutoff frequency of 1. See Also -------- butter : Filter design function using this prototype """ if abs(int(N)) != N: raise ValueError("Filter order must be a nonnegative integer") z = numpy.array([]) m = numpy.arange(-N+1, N, 2) # Middle value is 0 to ensure an exactly real pole p = -numpy.exp(1j * pi * m / (2 * N)) k = 1 return z, p, k def cheb1ap(N, rp): """ Return (z,p,k) for Nth-order Chebyshev type I analog lowpass filter. The returned filter prototype has `rp` decibels of ripple in the passband. The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1, defined as the point at which the gain first drops below ``-rp``. See Also -------- cheby1 : Filter design function using this prototype """ if abs(int(N)) != N: raise ValueError("Filter order must be a nonnegative integer") elif N == 0: # Avoid divide-by-zero error # Even order filters have DC gain of -rp dB return numpy.array([]), numpy.array([]), 10**(-rp/20) z = numpy.array([]) # Ripple factor (epsilon) eps = numpy.sqrt(10 ** (0.1 * rp) - 1.0) mu = 1.0 / N * arcsinh(1 / eps) # Arrange poles in an ellipse on the left half of the S-plane m = numpy.arange(-N+1, N, 2) theta = pi * m / (2*N) p = -sinh(mu + 1j*theta) k = numpy.prod(-p, axis=0).real if N % 2 == 0: k = k / sqrt((1 + eps * eps)) return z, p, k def cheb2ap(N, rs): """ Return (z,p,k) for Nth-order Chebyshev type I analog lowpass filter. The returned filter prototype has `rs` decibels of ripple in the stopband. The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1, defined as the point at which the gain first reaches ``-rs``. See Also -------- cheby2 : Filter design function using this prototype """ if abs(int(N)) != N: raise ValueError("Filter order must be a nonnegative integer") elif N == 0: # Avoid divide-by-zero warning return numpy.array([]), numpy.array([]), 1 # Ripple factor (epsilon) de = 1.0 / sqrt(10 ** (0.1 * rs) - 1) mu = arcsinh(1.0 / de) / N if N % 2: m = numpy.concatenate((numpy.arange(-N+1, 0, 2), numpy.arange(2, N, 2))) else: m = numpy.arange(-N+1, N, 2) z = -conjugate(1j / sin(m * pi / (2.0 * N))) # Poles around the unit circle like Butterworth p = -exp(1j * pi * numpy.arange(-N+1, N, 2) / (2 * N)) # Warp into Chebyshev II p = sinh(mu) * p.real + 1j * cosh(mu) * p.imag p = 1.0 / p k = (numpy.prod(-p, axis=0) / numpy.prod(-z, axis=0)).real return z, p, k EPSILON = 2e-16 def _vratio(u, ineps, mp): [s, c, d, phi] = special.ellipj(u, mp) ret = abs(ineps - s / c) return ret def _kratio(m, k_ratio): m = float(m) if m < 0: m = 0.0 if m > 1: m = 1.0 if abs(m) > EPSILON and (abs(m) + EPSILON) < 1: k = special.ellipk([m, 1 - m]) r = k[0] / k[1] - k_ratio elif abs(m) > EPSILON: r = -k_ratio else: r = 1e20 return abs(r) def ellipap(N, rp, rs): """Return (z,p,k) of Nth-order elliptic analog lowpass filter. The filter is a normalized prototype that has `rp` decibels of ripple in the passband and a stopband `rs` decibels down. The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1, defined as the point at which the gain first drops below ``-rp``. See Also -------- ellip : Filter design function using this prototype References ---------- .. [1] Lutova, Tosic, and Evans, "Filter Design for Signal Processing", Chapters 5 and 12. """ if abs(int(N)) != N: raise ValueError("Filter order must be a nonnegative integer") elif N == 0: # Avoid divide-by-zero warning # Even order filters have DC gain of -rp dB return numpy.array([]), numpy.array([]), 10**(-rp/20) elif N == 1: p = -sqrt(1.0 / (10 ** (0.1 * rp) - 1.0)) k = -p z = [] return asarray(z), asarray(p), k eps = numpy.sqrt(10 ** (0.1 * rp) - 1) ck1 = eps / numpy.sqrt(10 ** (0.1 * rs) - 1) ck1p = numpy.sqrt(1 - ck1 * ck1) if ck1p == 1: raise ValueError("Cannot design a filter with given rp and rs" " specifications.") val = special.ellipk([ck1 * ck1, ck1p * ck1p]) if abs(1 - ck1p * ck1p) < EPSILON: krat = 0 else: krat = N * val[0] / val[1] m = optimize.fmin(_kratio, [0.5], args=(krat,), maxfun=250, maxiter=250, disp=0) if m < 0 or m > 1: m = optimize.fminbound(_kratio, 0, 1, args=(krat,), maxfun=250, maxiter=250, disp=0) capk = special.ellipk(m) j = numpy.arange(1 - N % 2, N, 2) jj = len(j) [s, c, d, phi] = special.ellipj(j * capk / N, m * numpy.ones(jj)) snew = numpy.compress(abs(s) > EPSILON, s, axis=-1) z = 1.0 / (sqrt(m) * snew) z = 1j * z z = numpy.concatenate((z, conjugate(z))) r = optimize.fmin(_vratio, special.ellipk(m), args=(1. / eps, ck1p * ck1p), maxfun=250, maxiter=250, disp=0) v0 = capk * r / (N * val[0]) [sv, cv, dv, phi] = special.ellipj(v0, 1 - m) p = -(c * d * sv * cv + 1j * s * dv) / (1 - (d * sv) ** 2.0) if N % 2: newp = numpy.compress(abs(p.imag) > EPSILON * numpy.sqrt(numpy.sum(p * numpy.conjugate(p), axis=0).real), p, axis=-1) p = numpy.concatenate((p, conjugate(newp))) else: p = numpy.concatenate((p, conjugate(p))) k = (numpy.prod(-p, axis=0) / numpy.prod(-z, axis=0)).real if N % 2 == 0: k = k / numpy.sqrt((1 + eps * eps)) return z, p, k # TODO: Make this a real public function scipy.misc.ff def _falling_factorial(x, n): r""" Return the factorial of `x` to the `n` falling. This is defined as: .. math:: x^\underline n = (x)_n = x (x-1) \cdots (x-n+1) This can more efficiently calculate ratios of factorials, since: n!/m! == falling_factorial(n, n-m) where n >= m skipping the factors that cancel out the usual factorial n! == ff(n, n) """ val = 1 for k in range(x - n + 1, x + 1): val *= k return val def _bessel_poly(n, reverse=False): """ Return the coefficients of Bessel polynomial of degree `n` If `reverse` is true, a reverse Bessel polynomial is output. Output is a list of coefficients: [1] = 1 [1, 1] = 1*s + 1 [1, 3, 3] = 1*s^2 + 3*s + 3 [1, 6, 15, 15] = 1*s^3 + 6*s^2 + 15*s + 15 [1, 10, 45, 105, 105] = 1*s^4 + 10*s^3 + 45*s^2 + 105*s + 105 etc. Output is a Python list of arbitrary precision long ints, so n is only limited by your hardware's memory. Sequence is http://oeis.org/A001498 , and output can be confirmed to match http://oeis.org/A001498/b001498.txt : >>> i = 0 >>> for n in range(51): ... for x in _bessel_poly(n, reverse=True): ... print(i, x) ... i += 1 """ if abs(int(n)) != n: raise ValueError("Polynomial order must be a nonnegative integer") else: n = int(n) # np.int32 doesn't work, for instance out = [] for k in range(n + 1): num = _falling_factorial(2*n - k, n) den = 2**(n - k) * factorial(k, exact=True) out.append(num // den) if reverse: return out[::-1] else: return out def _campos_zeros(n): """ Return approximate zero locations of Bessel polynomials y_n(x) for order `n` using polynomial fit (Campos-Calderon 2011) """ if n == 1: return asarray([-1+0j]) s = npp_polyval(n, [0, 0, 2, 0, -3, 1]) b3 = npp_polyval(n, [16, -8]) / s b2 = npp_polyval(n, [-24, -12, 12]) / s b1 = npp_polyval(n, [8, 24, -12, -2]) / s b0 = npp_polyval(n, [0, -6, 0, 5, -1]) / s r = npp_polyval(n, [0, 0, 2, 1]) a1 = npp_polyval(n, [-6, -6]) / r a2 = 6 / r k = np.arange(1, n+1) x = npp_polyval(k, [0, a1, a2]) y = npp_polyval(k, [b0, b1, b2, b3]) return x + 1j*y def _aberth(f, fp, x0, tol=1e-15, maxiter=50): """ Given a function `f`, its first derivative `fp`, and a set of initial guesses `x0`, simultaneously find the roots of the polynomial using the Aberth-Ehrlich method. ``len(x0)`` should equal the number of roots of `f`. (This is not a complete implementation of Bini's algorithm.) """ N = len(x0) x = array(x0, complex) beta = np.empty_like(x0) for iteration in range(maxiter): alpha = -f(x) / fp(x) # Newton's method # Model "repulsion" between zeros for k in range(N): beta[k] = np.sum(1/(x[k] - x[k+1:])) beta[k] += np.sum(1/(x[k] - x[:k])) x += alpha / (1 + alpha * beta) if not all(np.isfinite(x)): raise RuntimeError('Root-finding calculation failed') # Mekwi: The iterative process can be stopped when |hn| has become # less than the largest error one is willing to permit in the root. if all(abs(alpha) <= tol): break else: raise Exception('Zeros failed to converge') return x def _bessel_zeros(N): """ Find zeros of ordinary Bessel polynomial of order `N`, by root-finding of modified Bessel function of the second kind """ if N == 0: return asarray([]) # Generate starting points x0 = _campos_zeros(N) # Zeros are the same for exp(1/x)*K_{N+0.5}(1/x) and Nth-order ordinary # Bessel polynomial y_N(x) def f(x): return special.kve(N+0.5, 1/x) # First derivative of above def fp(x): return (special.kve(N-0.5, 1/x)/(2*x**2) - special.kve(N+0.5, 1/x)/(x**2) + special.kve(N+1.5, 1/x)/(2*x**2)) # Starting points converge to true zeros x = _aberth(f, fp, x0) # Improve precision using Newton's method on each for i in range(len(x)): x[i] = optimize.newton(f, x[i], fp, tol=1e-15) # Average complex conjugates to make them exactly symmetrical x = np.mean((x, x[::-1].conj()), 0) # Zeros should sum to -1 if abs(np.sum(x) + 1) > 1e-15: raise RuntimeError('Generated zeros are inaccurate') return x def _norm_factor(p, k): """ Numerically find frequency shift to apply to delay-normalized filter such that -3 dB point is at 1 rad/sec. `p` is an array_like of polynomial poles `k` is a float gain First 10 values are listed in "Bessel Scale Factors" table, "Bessel Filters Polynomials, Poles and Circuit Elements 2003, C. Bond." """ p = asarray(p, dtype=complex) def G(w): """ Gain of filter """ return abs(k / prod(1j*w - p)) def cutoff(w): """ When gain = -3 dB, return 0 """ return G(w) - 1/np.sqrt(2) return optimize.newton(cutoff, 1.5) def besselap(N, norm='phase'): """ Return (z,p,k) for analog prototype of an Nth-order Bessel filter. Parameters ---------- N : int The order of the filter. norm : {'phase', 'delay', 'mag'}, optional Frequency normalization: ``phase`` The filter is normalized such that the phase response reaches its midpoint at an angular (e.g. rad/s) cutoff frequency of 1. This happens for both low-pass and high-pass filters, so this is the "phase-matched" case. [6]_ The magnitude response asymptotes are the same as a Butterworth filter of the same order with a cutoff of `Wn`. This is the default, and matches MATLAB's implementation. ``delay`` The filter is normalized such that the group delay in the passband is 1 (e.g. 1 second). This is the "natural" type obtained by solving Bessel polynomials ``mag`` The filter is normalized such that the gain magnitude is -3 dB at angular frequency 1. This is called "frequency normalization" by Bond. [1]_ .. versionadded:: 0.18.0 Returns ------- z : ndarray Zeros of the transfer function. Is always an empty array. p : ndarray Poles of the transfer function. k : scalar Gain of the transfer function. For phase-normalized, this is always 1. See Also -------- bessel : Filter design function using this prototype Notes ----- To find the pole locations, approximate starting points are generated [2]_ for the zeros of the ordinary Bessel polynomial [3]_, then the Aberth-Ehrlich method [4]_ [5]_ is used on the Kv(x) Bessel function to calculate more accurate zeros, and these locations are then inverted about the unit circle. References ---------- .. [1] C.R. Bond, "Bessel Filter Constants", http://www.crbond.com/papers/bsf.pdf .. [2] Campos and Calderon, "Approximate closed-form formulas for the zeros of the Bessel Polynomials", :arXiv:`1105.0957`. .. [3] Thomson, W.E., "Delay Networks having Maximally Flat Frequency Characteristics", Proceedings of the Institution of Electrical Engineers, Part III, November 1949, Vol. 96, No. 44, pp. 487-490. .. [4] Aberth, "Iteration Methods for Finding all Zeros of a Polynomial Simultaneously", Mathematics of Computation, Vol. 27, No. 122, April 1973 .. [5] Ehrlich, "A modified Newton method for polynomials", Communications of the ACM, Vol. 10, Issue 2, pp. 107-108, Feb. 1967, :DOI:`10.1145/363067.363115` .. [6] Miller and Bohn, "A Bessel Filter Crossover, and Its Relation to Others", RaneNote 147, 1998, http://www.rane.com/note147.html """ if abs(int(N)) != N: raise ValueError("Filter order must be a nonnegative integer") if N == 0: p = [] k = 1 else: # Find roots of reverse Bessel polynomial p = 1/_bessel_zeros(N) a_last = _falling_factorial(2*N, N) // 2**N # Shift them to a different normalization if required if norm in ('delay', 'mag'): # Normalized for group delay of 1 k = a_last if norm == 'mag': # -3 dB magnitude point is at 1 rad/sec norm_factor = _norm_factor(p, k) p /= norm_factor k = norm_factor**-N * a_last elif norm == 'phase': # Phase-matched (1/2 max phase shift at 1 rad/sec) # Asymptotes are same as Butterworth filter p *= 10**(-math.log10(a_last)/N) k = 1 else: raise ValueError('normalization not understood') return asarray([]), asarray(p, dtype=complex), float(k) def iirnotch(w0, Q): """ Design second-order IIR notch digital filter. A notch filter is a band-stop filter with a narrow bandwidth (high quality factor). It rejects a narrow frequency band and leaves the rest of the spectrum little changed. Parameters ---------- w0 : float Normalized frequency to remove from a signal. It is a scalar that must satisfy ``0 < w0 < 1``, with ``w0 = 1`` corresponding to half of the sampling frequency. Q : float Quality factor. Dimensionless parameter that characterizes notch filter -3 dB bandwidth ``bw`` relative to its center frequency, ``Q = w0/bw``. Returns ------- b, a : ndarray, ndarray Numerator (``b``) and denominator (``a``) polynomials of the IIR filter. See Also -------- iirpeak Notes ----- .. versionadded: 0.19.0 References ---------- .. [1] Sophocles J. Orfanidis, "Introduction To Signal Processing", Prentice-Hall, 1996 Examples -------- Design and plot filter to remove the 60Hz component from a signal sampled at 200Hz, using a quality factor Q = 30 >>> from scipy import signal >>> import numpy as np >>> import matplotlib.pyplot as plt >>> fs = 200.0 # Sample frequency (Hz) >>> f0 = 60.0 # Frequency to be removed from signal (Hz) >>> Q = 30.0 # Quality factor >>> w0 = f0/(fs/2) # Normalized Frequency >>> # Design notch filter >>> b, a = signal.iirnotch(w0, Q) >>> # Frequency response >>> w, h = signal.freqz(b, a) >>> # Generate frequency axis >>> freq = w*fs/(2*np.pi) >>> # Plot >>> fig, ax = plt.subplots(2, 1, figsize=(8, 6)) >>> ax[0].plot(freq, 20*np.log10(abs(h)), color='blue') >>> ax[0].set_title("Frequency Response") >>> ax[0].set_ylabel("Amplitude (dB)", color='blue') >>> ax[0].set_xlim([0, 100]) >>> ax[0].set_ylim([-25, 10]) >>> ax[0].grid() >>> ax[1].plot(freq, np.unwrap(np.angle(h))*180/np.pi, color='green') >>> ax[1].set_ylabel("Angle (degrees)", color='green') >>> ax[1].set_xlabel("Frequency (Hz)") >>> ax[1].set_xlim([0, 100]) >>> ax[1].set_yticks([-90, -60, -30, 0, 30, 60, 90]) >>> ax[1].set_ylim([-90, 90]) >>> ax[1].grid() >>> plt.show() """ return _design_notch_peak_filter(w0, Q, "notch") def iirpeak(w0, Q): """ Design second-order IIR peak (resonant) digital filter. A peak filter is a band-pass filter with a narrow bandwidth (high quality factor). It rejects components outside a narrow frequency band. Parameters ---------- w0 : float Normalized frequency to be retained in a signal. It is a scalar that must satisfy ``0 < w0 < 1``, with ``w0 = 1`` corresponding to half of the sampling frequency. Q : float Quality factor. Dimensionless parameter that characterizes peak filter -3 dB bandwidth ``bw`` relative to its center frequency, ``Q = w0/bw``. Returns ------- b, a : ndarray, ndarray Numerator (``b``) and denominator (``a``) polynomials of the IIR filter. See Also -------- iirnotch Notes ----- .. versionadded: 0.19.0 References ---------- .. [1] Sophocles J. Orfanidis, "Introduction To Signal Processing", Prentice-Hall, 1996 Examples -------- Design and plot filter to remove the frequencies other than the 300Hz component from a signal sampled at 1000Hz, using a quality factor Q = 30 >>> from scipy import signal >>> import numpy as np >>> import matplotlib.pyplot as plt >>> fs = 1000.0 # Sample frequency (Hz) >>> f0 = 300.0 # Frequency to be retained (Hz) >>> Q = 30.0 # Quality factor >>> w0 = f0/(fs/2) # Normalized Frequency >>> # Design peak filter >>> b, a = signal.iirpeak(w0, Q) >>> # Frequency response >>> w, h = signal.freqz(b, a) >>> # Generate frequency axis >>> freq = w*fs/(2*np.pi) >>> # Plot >>> fig, ax = plt.subplots(2, 1, figsize=(8, 6)) >>> ax[0].plot(freq, 20*np.log10(abs(h)), color='blue') >>> ax[0].set_title("Frequency Response") >>> ax[0].set_ylabel("Amplitude (dB)", color='blue') >>> ax[0].set_xlim([0, 500]) >>> ax[0].set_ylim([-50, 10]) >>> ax[0].grid() >>> ax[1].plot(freq, np.unwrap(np.angle(h))*180/np.pi, color='green') >>> ax[1].set_ylabel("Angle (degrees)", color='green') >>> ax[1].set_xlabel("Frequency (Hz)") >>> ax[1].set_xlim([0, 500]) >>> ax[1].set_yticks([-90, -60, -30, 0, 30, 60, 90]) >>> ax[1].set_ylim([-90, 90]) >>> ax[1].grid() >>> plt.show() """ return _design_notch_peak_filter(w0, Q, "peak") def _design_notch_peak_filter(w0, Q, ftype): """ Design notch or peak digital filter. Parameters ---------- w0 : float Normalized frequency to remove from a signal. It is a scalar that must satisfy ``0 < w0 < 1``, with ``w0 = 1`` corresponding to half of the sampling frequency. Q : float Quality factor. Dimensionless parameter that characterizes notch filter -3 dB bandwidth ``bw`` relative to its center frequency, ``Q = w0/bw``. ftype : str The type of IIR filter to design: - notch filter : ``notch`` - peak filter : ``peak`` Returns ------- b, a : ndarray, ndarray Numerator (``b``) and denominator (``a``) polynomials of the IIR filter. """ # Guarantee that the inputs are floats w0 = float(w0) Q = float(Q) # Checks if w0 is within the range if w0 > 1.0 or w0 < 0.0: raise ValueError("w0 should be such that 0 < w0 < 1") # Get bandwidth bw = w0/Q # Normalize inputs bw = bw*np.pi w0 = w0*np.pi # Compute -3dB atenuation gb = 1/np.sqrt(2) if ftype == "notch": # Compute beta: formula 11.3.4 (p.575) from reference [1] beta = (np.sqrt(1.0-gb**2.0)/gb)*np.tan(bw/2.0) elif ftype == "peak": # Compute beta: formula 11.3.19 (p.579) from reference [1] beta = (gb/np.sqrt(1.0-gb**2.0))*np.tan(bw/2.0) else: raise ValueError("Unknown ftype.") # Compute gain: formula 11.3.6 (p.575) from reference [1] gain = 1.0/(1.0+beta) # Compute numerator b and denominator a # formulas 11.3.7 (p.575) and 11.3.21 (p.579) # from reference [1] if ftype == "notch": b = gain*np.array([1.0, -2.0*np.cos(w0), 1.0]) else: b = (1.0-gain)*np.array([1.0, 0.0, -1.0]) a = np.array([1.0, -2.0*gain*np.cos(w0), (2.0*gain-1.0)]) return b, a filter_dict = {'butter': [buttap, buttord], 'butterworth': [buttap, buttord], 'cauer': [ellipap, ellipord], 'elliptic': [ellipap, ellipord], 'ellip': [ellipap, ellipord], 'bessel': [besselap], 'bessel_phase': [besselap], 'bessel_delay': [besselap], 'bessel_mag': [besselap], 'cheby1': [cheb1ap, cheb1ord], 'chebyshev1': [cheb1ap, cheb1ord], 'chebyshevi': [cheb1ap, cheb1ord], 'cheby2': [cheb2ap, cheb2ord], 'chebyshev2': [cheb2ap, cheb2ord], 'chebyshevii': [cheb2ap, cheb2ord], } band_dict = {'band': 'bandpass', 'bandpass': 'bandpass', 'pass': 'bandpass', 'bp': 'bandpass', 'bs': 'bandstop', 'bandstop': 'bandstop', 'bands': 'bandstop', 'stop': 'bandstop', 'l': 'lowpass', 'low': 'lowpass', 'lowpass': 'lowpass', 'lp': 'lowpass', 'high': 'highpass', 'highpass': 'highpass', 'h': 'highpass', 'hp': 'highpass', } bessel_norms = {'bessel': 'phase', 'bessel_phase': 'phase', 'bessel_delay': 'delay', 'bessel_mag': 'mag'}
bsd-3-clause
aje/POT
examples/plot_optim_OTreg.py
2
2940
# -*- coding: utf-8 -*- """ ================================== Regularized OT with generic solver ================================== Illustrates the use of the generic solver for regularized OT with user-designed regularization term. It uses Conditional gradient as in [6] and generalized Conditional Gradient as proposed in [5][7]. [5] N. Courty; R. Flamary; D. Tuia; A. Rakotomamonjy, Optimal Transport for Domain Adaptation, in IEEE Transactions on Pattern Analysis and Machine Intelligence , vol.PP, no.99, pp.1-1. [6] Ferradans, S., Papadakis, N., Peyré, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. [7] Rakotomamonjy, A., Flamary, R., & Courty, N. (2015). Generalized conditional gradient: analysis of convergence and applications. arXiv preprint arXiv:1510.06567. """ import numpy as np import matplotlib.pylab as pl import ot import ot.plot ############################################################################## # Generate data # ------------- #%% parameters n = 100 # nb bins # bin positions x = np.arange(n, dtype=np.float64) # Gaussian distributions a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std b = ot.datasets.get_1D_gauss(n, m=60, s=10) # loss matrix M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) M /= M.max() ############################################################################## # Solve EMD # --------- #%% EMD G0 = ot.emd(a, b, M) pl.figure(3, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') ############################################################################## # Solve EMD with Frobenius norm regularization # -------------------------------------------- #%% Example with Frobenius norm regularization def f(G): return 0.5 * np.sum(G**2) def df(G): return G reg = 1e-1 Gl2 = ot.optim.cg(a, b, M, reg, f, df, verbose=True) pl.figure(3) ot.plot.plot1D_mat(a, b, Gl2, 'OT matrix Frob. reg') ############################################################################## # Solve EMD with entropic regularization # -------------------------------------- #%% Example with entropic regularization def f(G): return np.sum(G * np.log(G)) def df(G): return np.log(G) + 1. reg = 1e-3 Ge = ot.optim.cg(a, b, M, reg, f, df, verbose=True) pl.figure(4, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, Ge, 'OT matrix Entrop. reg') ############################################################################## # Solve EMD with Frobenius norm + entropic regularization # ------------------------------------------------------- #%% Example with Frobenius norm + entropic regularization with gcg def f(G): return 0.5 * np.sum(G**2) def df(G): return G reg1 = 1e-3 reg2 = 1e-1 Gel2 = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True) pl.figure(5, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, Gel2, 'OT entropic + matrix Frob. reg') pl.show()
mit
andnovar/ggplot
ggplot/scales/scale_colour_gradient.py
12
2017
from __future__ import (absolute_import, division, print_function, unicode_literals) from .scale import scale from copy import deepcopy import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap, rgb2hex, ColorConverter def colors_at_breaks(cmap, breaks=[0, 0.25, 0.5, 0.75, 1.]): return [rgb2hex(cmap(bb)[:3]) for bb in breaks] class scale_colour_gradient(scale): """ Specify a two- or three-point gradient. Parameters ---------- name : Name of an existing gradient scheme limits : list of the upper and lower bounds of the gradient low : colour at the lower bound of the gradient mid : colour at the middle of the gradient high : Colour at the upper bound of the gradient Examples -------- >>> from ggplot import * >>> diamons_premium = diamonds[diamonds.cut=='Premium'] >>> gg = ggplot(diamons_premium, aes(x='depth', y='carat', colour='price')) + \\ ... geom_point() >>> print(gg + scale_colour_gradient(low='red', mid='white', high='blue', limits=[4000,6000]) + \\ ... ggtitle('With red-blue gradient')) >>> print(gg + ggtitle('With standard gradient')) """ VALID_SCALES = ['name', 'limits', 'low', 'mid', 'high'] def __radd__(self, gg): gg = deepcopy(gg) if self.name: gg.color_label = self.name if not (self.limits is None): gg.color_limits = self.limits color_spectrum = [] if self.low: color_spectrum.append(self.low) if self.mid: color_spectrum.append(self.mid) if self.high: color_spectrum.append(self.high) if self.low and self.high: gradient2n = LinearSegmentedColormap.from_list('gradient2n', color_spectrum) plt.cm.register_cmap(cmap=gradient2n) # add them back to ggplot gg.color_scale = colors_at_breaks(gradient2n) gg.colormap = gradient2n return gg
bsd-2-clause
PyQuake/earthquakemodels
code/runExperiments/histogramMagnitude.py
1
1982
import matplotlib.pyplot as plt import models.model as model import earthquake.catalog as catalog from collections import OrderedDict def histogramMagnitude(catalog_, region): """ Creates the histogram of magnitudes by a given region. Saves the histogram to the follwing path ./code/Zona2/histograms/'+region+'/Magnitude Histogram of ' + str(year) + " " + region + '.png' Where region, year are given by the application From 2000 to 2011 """ definition = model.loadModelDefinition('../params/' + region + '.txt') catalogFiltred = catalog.filter(catalog_, definition) year = 2000 while(year < 2012): data = dict() for i in range(len(catalogFiltred)): if catalogFiltred[i]['year'] == year and catalogFiltred[i]['lat'] > 34.8 and catalogFiltred[i][ 'lat'] < 37.05 and catalogFiltred[i]['lon'] > 138.8 and catalogFiltred[i]['lon'] < 141.05: data[catalogFiltred[i]['mag']] = data.get(catalogFiltred[i]['mag'], 0) + 1 b = OrderedDict(sorted(data.items())) plt.title('Histogram of ' + str(year) + " " + region) plt.bar(range(len(data)), b.values(), align='center') plt.xticks(range(len(data)), b.keys(), rotation=25) # print(b) axes = plt.gca() plt.savefig( '../Zona2/histograms/'+region+'/Magnitude Histogram of ' + str(year) + " " + region + '.png') del data year += 1 def main(): """ Calls function to plot a hitogram of magnitudes by region, based on JMA catalog """ catalog_ = catalog.readFromFile('../data/jmacat_2000_2013.dat') region = "Kanto" histogramMagnitude(catalog_, region) region = "Kansai" histogramMagnitude(catalog_, region) region = "Tohoku" histogramMagnitude(catalog_, region) region = "EastJapan" histogramMagnitude(catalog_, region) if __name__ == "__main__": main()
bsd-3-clause
YoungKwonJo/mlxtend
tests/tests_evaluate/test_learning_curves.py
1
2212
from mlxtend.evaluate import plot_learning_curves from sklearn import datasets from sklearn.cross_validation import train_test_split from sklearn.tree import DecisionTreeClassifier import numpy as np def test_training_size(): iris = datasets.load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.6, random_state=2) clf = DecisionTreeClassifier(max_depth=1, random_state=1) training_errors, test_errors = plot_learning_curves(X_train, y_train, X_test, y_test, clf, kind='training_size', suppress_plot=True) desired1 = [0.32, 0.33, 0.32, 0.33, 0.30, 0.31, 0.31, 0.22, 0.22, 0.22] desired2 = [0.35, 0.35, 0.35, 0.35, 0.43, 0.45, 0.35, 0.35, 0.45, 0.45] np.testing.assert_almost_equal(training_errors, desired1, decimal=2) np.testing.assert_almost_equal(test_errors, desired2, decimal=2) def test_scikit_metrics(): iris = datasets.load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.6, random_state=2) clf = DecisionTreeClassifier(max_depth=1, random_state=1) training_errors, test_errors = plot_learning_curves(X_train, y_train, X_test, y_test, clf, kind='training_size', suppress_plot=True, scoring='accuracy') desired1 = [0.68, 0.67, 0.68, 0.67, 0.7, 0.69, 0.69, 0.78, 0.78, 0.78] desired2 = [0.65, 0.65, 0.65, 0.65, 0.57, 0.55, 0.65, 0.65, 0.55, 0.55] np.testing.assert_almost_equal(training_errors, desired1, decimal=2) np.testing.assert_almost_equal(test_errors, desired2, decimal=2) def test_n_features(): iris = datasets.load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.6, random_state=2) clf = DecisionTreeClassifier(max_depth=1, random_state=1) training_errors, test_errors = plot_learning_curves(X_train, y_train, X_test, y_test, clf, kind='n_features', suppress_plot=True) desired1 = [0.40, 0.40, 0.32, 0.32] desired2 = [0.42, 0.42, 0.35, 0.35] np.testing.assert_almost_equal(training_errors, desired1, decimal=2) np.testing.assert_almost_equal(test_errors, desired2, decimal=2)
bsd-3-clause
rigetticomputing/grove
grove/tomography/state_tomography.py
1
11664
############################################################################## # Copyright 2017-2018 Rigetti Computing # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ############################################################################## import logging import numpy as np import matplotlib.pyplot as plt from pyquil.quilbase import Pragma from scipy.sparse import csr_matrix, coo_matrix from pyquil.quil import Program import grove.tomography.operator_utils from grove.tomography.tomography import TomographyBase, TomographySettings, DEFAULT_SOLVER_KWARGS from grove.tomography import tomography import grove.tomography.utils as ut import grove.tomography.operator_utils as o_ut _log = logging.getLogger(__name__) qt = ut.import_qutip() cvxpy = ut.import_cvxpy() UNIT_TRACE = 'unit_trace' POSITIVE = 'positive' DEFAULT_STATE_TOMO_SETTINGS = TomographySettings( constraints={UNIT_TRACE}, solver_kwargs=DEFAULT_SOLVER_KWARGS ) def _prepare_c_jk_m(readout_povm, pauli_basis, channel_ops): """ Prepare the coefficient matrix for state tomography. This function uses sparse matrices for much greater efficiency. The coefficient matrix is defined as: .. math:: C_{(jk)m} = \tr{\Pi_{s_j} \Lambda_k(P_m)} = \sum_{r}\pi_{jr}(\mathcal{R}_{k})_{rm} where :math:`\Lambda_k(\cdot)` is the quantum map corresponding to the k-th pre-measurement channel, i.e., :math:`\Lambda_k(\rho) = E_k \rho E_k^\dagger` where :math:`E_k` is the k-th channel operator. This map can also be represented via its transfer matrix :math:`\mathcal{R}_{k}`. In that case one also requires the overlap between the (generalized) Pauli basis ops and the projection operators :math:`\pi_{jl}:=\sbraket{\Pi_j}{P_l} = \tr{\Pi_j P_l}`. See the grove documentation on tomography for detailed information. :param DiagonalPOVM readout_povm: The POVM corresponding to the readout plus classifier. :param OperatorBasis pauli_basis: The (generalized) Pauli basis employed in the estimation. :param list channel_ops: The pre-measurement channel operators as `qutip.Qobj` :return: The coefficient matrix necessary to set up the binomial state tomography problem. :rtype: scipy.sparse.csr_matrix """ channel_transfer_matrices = [pauli_basis.transfer_matrix(qt.to_super(ek)) for ek in channel_ops] # This bit could be more efficient but does not run super long and is thus preserved for # readability. pi_jr = csr_matrix( [pauli_basis.project_op(n_j).toarray().ravel() for n_j in readout_povm.ops]) # Dict used for constructing our sparse matrix, keys are tuples (row_index, col_index), values # are the non-zero elements of the final matrix. c_jk_m_elms = {} # This explicitly exploits the sparsity of all operators involved for k in range(len(channel_ops)): pi_jr__rk_rm = (pi_jr * channel_transfer_matrices[k]).tocoo() for (j, m, val) in ut.izip(pi_jr__rk_rm.row, pi_jr__rk_rm.col, pi_jr__rk_rm.data): # The multi-index (j,k) is enumerated in column-major ordering (like Fortran arrays) c_jk_m_elms[(j + k * readout_povm.pi_basis.dim, m)] = val.real # create sparse matrix from COO-format (see scipy.sparse docs) _keys, _values = ut.izip(*c_jk_m_elms.items()) _rows, _cols = ut.izip(*_keys) c_jk_m = coo_matrix((list(_values), (list(_rows), list(_cols))), shape=(readout_povm.pi_basis.dim * len(channel_ops), pauli_basis.dim)).tocsr() return c_jk_m class StateTomography(TomographyBase): """ A StateTomography object encapsulates the result of quantum state estimation from tomographic data. It provides convenience functions for visualization and computing state fidelities. """ __tomography_type__ = "STATE" @staticmethod def estimate_from_ssr(histograms, readout_povm, channel_ops, settings): """ Estimate a density matrix from single shot histograms obtained by measuring bitstrings in the Z-eigenbasis after application of given channel operators. :param numpy.ndarray histograms: The single shot histograms, `shape=(n_channels, dim)`. :param DiagognalPOVM readout_povm: The POVM corresponding to the readout plus classifier. :param list channel_ops: The tomography measurement channels as `qutip.Qobj`'s. :param TomographySettings settings: The solver and estimation settings. :return: The generated StateTomography object. :rtype: StateTomography """ nqc = len(channel_ops[0].dims[0]) pauli_basis = grove.tomography.operator_utils.PAULI_BASIS ** nqc pi_basis = readout_povm.pi_basis if not histograms.shape[1] == pi_basis.dim: # pragma no coverage raise ValueError("Currently tomography is only implemented for two-level systems.") # prepare the log-likelihood function parameters, see documentation n_kj = np.asarray(histograms) c_jk_m = _prepare_c_jk_m(readout_povm, pauli_basis, channel_ops) rho_m = cvxpy.Variable(pauli_basis.dim) p_jk = c_jk_m * rho_m obj = -n_kj.ravel() * cvxpy.log(p_jk) p_jk_mat = cvxpy.reshape(p_jk, pi_basis.dim, len(channel_ops)) # cvxpy has col-major order # Default constraints: # MLE must describe valid probability distribution # i.e., for each k, p_jk must sum to one and be element-wise non-negative: # 1. \sum_j p_jk == 1 for all k # 2. p_jk >= 0 for all j, k # where p_jk = \sum_m c_jk_m rho_m constraints = [ p_jk >= 0, np.matrix(np.ones((1, pi_basis.dim))) * p_jk_mat == 1, ] rho_m_real_imag = sum((rm * o_ut.to_realimag(Pm) for (rm, Pm) in ut.izip(rho_m, pauli_basis.ops)), 0) if POSITIVE in settings.constraints: if tomography._SDP_SOLVER.is_functional(): constraints.append(rho_m_real_imag >> 0) else: # pragma no coverage _log.warning("No convex solver capable of semi-definite problems installed.\n" "Dropping the positivity constraint on the density matrix.") if UNIT_TRACE in settings.constraints: # this assumes that the first element of the Pauli basis is always proportional to # the identity constraints.append(rho_m[0, 0] == 1. / pauli_basis.ops[0].tr().real) prob = cvxpy.Problem(cvxpy.Minimize(obj), constraints) _log.info("Starting convex solver") prob.solve(solver=tomography.SOLVER, **settings.solver_kwargs) if prob.status != cvxpy.OPTIMAL: # pragma no coverage _log.warning("Problem did not converge to optimal solution. " "Solver settings: {}".format(settings.solver_kwargs)) return StateTomography(np.array(rho_m.value).ravel(), pauli_basis, settings) def __init__(self, rho_coeffs, pauli_basis, settings): """ Construct a StateTomography to encapsulate the result of estimating the quantum state from a quantum tomography measurement. :param numpy.ndarray r_est: The estimated quantum state represented in a given (generalized) Pauli basis. :param OperatorBasis pauli_basis: The employed (generalized) Pauli basis. :param TomographySettings settings: The settings used to estimate the state. """ self.rho_coeffs = rho_coeffs self.pauli_basis = pauli_basis self.rho_est = sum((r_m * p_m for r_m, p_m in ut.izip(rho_coeffs, pauli_basis.ops))) self.settings = settings def fidelity(self, other): """ Compute the quantum state fidelity of the estimated state with another state. :param qutip.Qobj other: The other quantum state. :return: The fidelity, a real number between 0 and 1. :rtype: float """ return qt.fidelity(self.rho_est, other) def plot_state_histogram(self, ax): """ Visualize the complex matrix elements of the estimated state. :param matplotlib.Axes ax: A matplotlib Axes object to plot into. """ title = "Estimated state" nqc = int(round(np.log2(self.rho_est.data.shape[0]))) labels = ut.basis_labels(nqc) return ut.state_histogram(self.rho_est, ax, title) def plot(self): """ Visualize the state. :return: The generated figure. :rtype: matplotlib.Figure """ width = 10 # The pleasing golden ratio. height = width / 1.618 f = plt.figure(figsize=(width, height)) ax = f.add_subplot(111, projection="3d") self.plot_state_histogram(ax) return f def state_tomography_programs(state_prep, qubits=None, rotation_generator=tomography.default_rotations): """ Yield tomographic sequences that prepare a state with Quil program `state_prep` and then append tomographic rotations on the specified `qubits`. If `qubits is None`, it assumes all qubits in the program should be tomographically rotated. :param Program state_prep: The program to prepare the state to be tomographed. :param list|NoneType qubits: A list of Qubits or Numbers, to perform the tomography on. If `None`, performs it on all in state_prep. :param generator rotation_generator: A generator that yields tomography rotations to perform. :return: Program for state tomography. :rtype: Program """ if qubits is None: qubits = state_prep.get_qubits() for tomography_program in rotation_generator(*qubits): state_tomography_program = Program(Pragma("PRESERVE_BLOCK")) state_tomography_program.inst(state_prep) state_tomography_program.inst(tomography_program) state_tomography_program.inst(Pragma("END_PRESERVE_BLOCK")) yield state_tomography_program def do_state_tomography(preparation_program, nsamples, cxn, qubits=None, use_run=False): """ Method to perform both a QPU and QVM state tomography, and use the latter as as reference to calculate the fidelity of the former. :param Program preparation_program: Program to execute. :param int nsamples: Number of samples to take for the program. :param QVMConnection|QPUConnection cxn: Connection on which to run the program. :param list qubits: List of qubits for the program. to use in the tomography analysis. :param bool use_run: If ``True``, use append measurements on all qubits and use ``cxn.run`` instead of ``cxn.run_and_measure``. :return: The state tomogram. :rtype: StateTomography """ return tomography._do_tomography(preparation_program, nsamples, cxn, qubits, tomography.MAX_QUBITS_STATE_TOMO, StateTomography, state_tomography_programs, DEFAULT_STATE_TOMO_SETTINGS, use_run=use_run)
apache-2.0
natj/bender
paper/figs/fig9.py
1
4141
import numpy as np import math from pylab import * from palettable.wesanderson import Zissou_5 as wsZ import matplotlib.ticker as mtick from scipy.interpolate import interp1d from scipy.interpolate import griddata from scipy.signal import savgol_filter def smooth(xx, yy): yy = savgol_filter(yy, 7, 2) np.clip(yy, 0.0, 1000.0, out=yy) yy[0] = 0.0 yy[-1] = 0.0 return xx, yy #Read JN files def read_lineprof(fname): da = np.genfromtxt(fname, delimiter=",") des = np.diff(da[:,0])[2] norm = np.sum(des*da[:,1]) return da[:,0],da[:,1]/norm #Read JN files def read_csv(fname): da = np.genfromtxt(fname, delimiter=",") des = np.diff(da[:,0])[2] norm = np.sum(des*da[:,1]) return da[:,0],da[:,1] #/norm ## Plot fig = figure(figsize=(5,3), dpi=80) rc('font', family='serif') rc('xtick', labelsize='xx-small') rc('ytick', labelsize='xx-small') gs = GridSpec(1, 1) #gs.update(wspace = 0.34) #gs.update(hspace = 0.4) lsize = 10.0 xmin = 0.69 xmax = 0.82 #error window limits eymin = -0.5 eymax = 0.5 #path to files #path_JN = "../../out3/lines/" path_JN = "../../out/lines2/" #labels size tsize = 10.0 nu = '700' #fig.text(0.5, 0.92, '$\\theta_s = 18^{\\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.72, '$\\theta_s = 45^{\\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.52, '$\\theta_s = 90^{\\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.32, 'Hopf $\\theta_s = 45^{\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.12, 'Phase',ha='center', va='center', size=lsize) ax1 = subplot(gs[0,0]) ax1.minorticks_on() ax1.set_xlim(xmin, xmax) ax1.set_ylim(0.0, 30) ax1.set_ylabel('Normalized flux',size=lsize) ax1.set_xlabel('Energy $E/E\'$',size=lsize) #xx1, yy1 = read_lineprof(path_JN+'lineprof_f700pbbr10m1.4i20.csv') #ax1.plot(xx1, yy1, "k--") #xx2, yy2 = read_lineprof(path_JN+'lineprof_obl_HTq0_f700pbbr10m1.4i20.csv') #ax1.plot(xx2, yy2, "k-") #lineprof_obl_HTq3_f700pbbr10m1.4i20.csv #lineprof_obl_HTq5_f700pbbr10m1.4i20.csv #lineprof_obl_HTq2_f700pbbr10m1.4i20.csv files_JN = [ "lineprof_f700pbbr10m1.4i20.csv", "lineprof_obl_f700pbbr10m1.4i20.csv", #"lineprof_sph2_HTqfix_f700pbbr10m1.4i20.csv"] #"lineprof_obl_HTq0_f700pbbr10m1.4i20.csv", "lineprof_obl_HTq1_f700pbbr10m1.4i20.csv"] #"lineprof_obl_HTq4_f700pbbr10m1.4i20.csv"] files_JN = ['sch/lineprofile_f700_bb_r10_m1.4_i20.csv', 'obl/lineprofile_f700_bb_r10_m1.4_i20.csv', 'q/lineprofile_f700_bb_r10_m1.4_i20.csv'] cols = ["black", "blue", "red", "magenta"] i = 0 for file_name in files_JN: xx, yy = read_lineprof(path_JN+file_name) xx, yy = smooth(xx, yy) ax1.plot(xx, yy, color=cols[i], linestyle="solid") i += 1 #path_JN = "../../out3/lines/" xx, yy = read_lineprof("../../out3/lines/lineprof_obl_HTq4_f700pbbr10m1.4i20.csv") ax1.plot(xx, yy, color="red", linestyle="dashed") #files_Bau = [ #"sch+dopp.csv", #"sch+dopp+obl.csv", #"HT.csv", #"HT_obl.csv"] files_Bau = ['sch.csv', 'obl.csv', 'ht.csv'] i = 0 for file_name in files_Bau: xx, yy = read_csv(path_JN+file_name) #rescale xx for correct scaling #xx = (xx-0.72)/(0.89-0.72)*(0.8-0.72) + 0.72 #ax1.plot(xx, yy, color=cols[i], linestyle="dashed") i += 1 ############ q's #xx3, yy3 = read_lineprof(path_JN+'lineprof_obl_HTq1_f700pbbr10m1.4i20.csv') #ax1.plot(xx3, yy3, "k-", label="$q = -0.268$") # #xx4, yy4 = read_lineprof(path_JN+'lineprof_obl_HTq2_f700pbbr10m1.4i20.csv') #ax1.plot(xx4, yy4, "r-", label="$q \\times 2$") # #xx5, yy5 = read_lineprof(path_JN+'lineprof_obl_HTq3_f700pbbr10m1.4i20.csv') #ax1.plot(xx5, yy5, "g-", label="$q \\times 3$") # #xx6, yy6 = read_lineprof(path_JN+'lineprof_obl_HTq4_f700pbbr10m1.4i20.csv') #ax1.plot(xx6, yy6, "b-", label="$q \\times 4$") # #xx7, yy7 = read_lineprof(path_JN+'lineprof_obl_HTq5_f700pbbr10m1.4i20.csv') #ax1.plot(xx7, yy7, "m-", label="$q \\times 5$") # #legend = ax1.legend(loc='upper left', shadow=False, labelspacing=0.1) #for label in legend.get_texts(): # label.set_fontsize('x-small') savefig('fig9_testi.pdf', bbox_inches='tight')
mit
nicholaschris/landsatpy
utils.py
1
2693
import operator import pandas as pd import numpy as np from numpy import ma from scipy.misc import imresize import scipy.ndimage as ndimage from skimage.morphology import disk, dilation def get_truth(input_one, input_two, comparison): # too much abstraction ops = {'>': operator.gt, '<': operator.lt, '>=': operator.ge, '<=': operator.le, '=': operator.eq} return ops[comparison](input_one, input_two) def convert_to_celsius(brightness_temp_input): return brightness_temp_input - 272.15 def calculate_percentile(input_masked_array, percentile): flat_fill_input = input_masked_array.filled(np.nan).flatten() df = pd.DataFrame(flat_fill_input) percentile = df.quantile(percentile/100.0) return percentile[0] def save_object(obj, filename): import pickle with open(filename, 'wb') as output: pickle.dump(obj, output) def downsample(input_array, factor=4): output_array = input_array[::2, ::2] / 4 + input_array[1::2, ::2] / 4 + input_array[::2, 1::2] / 4 + input_array[1::2, 1::2] / 4 return output_array def dilate_boolean_array(input_array, disk_size=3): selem = disk(disk_size) dilated = dilation(input_array, selem) return dilated def get_resized_array(img, size): lena = imresize(img, (size, size)) return lena def interp_and_resize(array, new_length): orig_y_length, orig_x_length = array.shape interp_factor_y = new_length / orig_y_length interp_factor_x = new_length / orig_x_length y = round(interp_factor_y * orig_y_length) x = round(interp_factor_x * orig_x_length) # http://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html new_indicies = np.mgrid[0:orig_y_length:y * 1j, 0:orig_x_length:x * 1j] # order=1 indicates bilinear interpolation. interp_array = ndimage.map_coordinates(array, new_indicies, order=1, output=array.dtype) interp_array = interp_array.reshape((y, x)) return interp_array def parse_mtl(in_file): awesome = True f = open(in_file, 'r') print(in_file) mtl_dict = {} with open(in_file, 'r') as f: while awesome: line = f.readline() if line.strip() == '' or line.strip() == 'END': return mtl_dict elif 'END_GROUP' in line: pass elif 'GROUP' in line: curr_group = line.split('=')[1].strip() mtl_dict[curr_group] = {} else: attr, value = line.split('=')[0].strip(), line.split('=')[1].strip() mtl_dict[curr_group][attr] = value
mit