repo_name
stringlengths
7
92
path
stringlengths
5
129
copies
stringclasses
201 values
size
stringlengths
4
6
content
stringlengths
1.03k
375k
license
stringclasses
15 values
rafafigueroa/compass-gait
hasimpy.py
1
9216
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: Rafael Figueroa """ dp = True import numpy as np DEBUG = False class H: """Hybrid Automata Model""" def __init__(self, Q, Init_X, Init_qID, state_names = None): self.q = Q #list of q self.Init_X = Init_X self.Init_qID = Init_qID self.states = state_names self.Ts = None def mode_tracker_guard_check(self, qID, X): # Called by mode_tracker to set the mode q = self.q[qID] g=q.E.G #guard list oe=q.E.OE #out edges list [g_activated, oID_activated_g] = guard_check(g, X) # return new qID when a guard is activated if g_activated: qID_activated_g = oe[oID_activated_g] else: qID_activated_g = qID return qID_activated_g def sim(self, qID, X, u, t0, tlim, haws_flag=False, debug_flag=False, Ts=1e-4): self.Ts = Ts #t0 refers to the initial time of #each continuous dynamic time interval sr = SimResult(self.states) #Initialize class q = self.q[qID] #get a ref to current mode global DEBUG DEBUG = debug_flag #change global DEBUG variable while t0<tlim: #get values from current q object f=q.f #continuous dynamics func # when simulating is requested by haws # with a forced input if not haws_flag: u=q.u g=q.E.G #guard list r=q.E.R #reset map list oe=q.E.OE #out edges list dom=q.Dom #discrete mode domain avoid=q.Avoid #discrete mode avoid if DEBUG: print '\n*** New Discrete State *** \n' print 'f=',f,'\ng=',g,'\nr=',r,'\noe=',oe,'\ndom=',dom print 'Avoid=',avoid print 'qID=',q.qID,'\nX=',X,'\nu=',u print '\n*** domain check *** \n' if not dom(X): errorString = 'Outside domain!' print errorString #raise NameError(errorString) if DEBUG: print '\n*** continuous dynamics *** \n' #simulate continuous dynamics T, Y, oID_activated_g, \ avoid_activated, tlim_activated = \ odeeul(f, u, g, avoid, X, t0, tlim, Ts) # store this time interval # in the simulation results sr.newTimeInterval(T, Y, q) # when inside the avoid set, simulation stops # and the information is stored in the simulation results if avoid_activated: sr.avoid_activated = True sr.timeToAvoid = T[-1] break #while loop if tlim_activated: break #while loop # *** after guard is activated *** # prepare data for the next loop t0=T[-1] #reset initial time to the end of #last time interval last_state = np.array(Y[-1]) if DEBUG: print '\n *** reset map *** \n' print 'last state =',last_state X=r[oID_activated_g](last_state) #reset map qID_activated_g = oe[oID_activated_g] #guard activated print out if DEBUG: print 'sim -- guard activated' print 'sim -- from q =', q.qID, 'to q =', qID_activated_g print 'sim -- State =', X #get new q q = self.q[qID_activated_g] return sr class Q: def __init__(self,qID,f,u,E, Dom = lambda X:True, Avoid = lambda X:False , TC=True): self.qID = qID self.f = f self.u = u self.E = E self.Dom = Dom self.Avoid = Avoid self.TC = TC class E: def __init__(self,OE,G,R): self.OE = OE self.G = G self.R = R def guard_check(g,X): guard_list = [] #evaluate every guard in g #g is the list of guards for this q #store the results in guard_list for guard in g: guard_list.append(guard(X)) oID_activated_g = None g_activated = False #check if any result in guard_list is True #if it is, store the index for oID,guard in enumerate(guard_list): if guard: oID_activated_g = oID #outside q which tripped the guard g_activated = True break return [g_activated, oID_activated_g] def avoid_check(avoid,X): 'avoid returns True when inside the avoid set' return avoid(X) def odeeul(f, u, g, avoid, X0, t0, tlim, Ts): X=np.array(X0) Y=np.array(X0) T=np.array([t0]) if DEBUG: print 'State=',X g_activated, oID_activated_g = guard_check(g,X) avoid_activated = avoid_check(avoid,X) tlim_activated = (t0>=tlim) if g_activated: print 'instant jump' if DEBUG: print 'First checks:' print '\tg_activated:', g_activated print '\tavoid_activated', avoid_activated print '\ttlim_activated', tlim_activated while not (g_activated or avoid_activated or tlim_activated): #Evolve continuously until a #termination condition is activated X=Ts*f(X,u)+X Y=np.vstack((Y,X)) tnew = np.array([T[-1]+Ts]) T=np.concatenate([T,tnew]) #termination checks g_activated, oID_activated_g = guard_check(g,X) avoid_activated = avoid_check(avoid,X) tlim_activated = (tnew>=tlim) if DEBUG: print 'Running checks:' print '\tg_activated:',g_activated print '\tavoid_activated',avoid_activated print '\ttlim_activated',tlim_activated return [T, Y, oID_activated_g, avoid_activated, tlim_activated] class SimResult: """Output from one simulation run""" def __init__(self, states = None): self.I = [] self.j = 0 self.timesteps = 0 self.timeToAvoid = None self.avoid_activated = False self.path = None self.time = None self.mode = None self.states = states for yi in range(0, len(states)): self.states[yi] = "$" + self.states[yi] + "$" self.states[yi] = self.states[yi].encode('string-escape') self.states[yi] = self.states[yi].replace("\\\\", "\\") def newTimeInterval(self, T, Y, qID): """Simulation is broken into continuous chunks Here the chunks are put together""" if self.j == 0: # First interval self.path = Y self.time = T self.mode = np.array([qID]) else: self.path = np.vstack((self.path, Y)) self.time = np.concatenate((self.time, T)) self.mode = np.concatenate((self.mode, np.array([qID]))) self.j = self.j + 1 self.timesteps = self.timesteps + np.size(T) self.I.append(TimeInterval(T, Y, self.j)) def simPlot(self): Y_plot = self.path T_plot = self.time import matplotlib.pyplot as plt # TODO: Configurate at install? # user might not want latex from matplotlib import rc rc('text', usetex=True) nstates = np.size(Y_plot,1) f, axarr = plt.subplots(nstates, sharex=True) if nstates>1: for yi in range(nstates): axarr[yi].plot(T_plot, Y_plot[:,yi]) if self.states is not None: axarr[nstates-1].set_xlabel(r'time(s)') axarr[yi].set_ylabel(self.states[yi], fontsize = 20) axarr[yi].yaxis.set_label_coords(-0.08, 0.5) else: axarr.plot(T_plot,Y_plot) if self.states is not None: axarr.set_xlabel('time(s)') axarr.set_ylabel(self.states[0]) plt.ion() plt.show() def phasePlot(self, plotStates): #TODO:check size of Y,plotStates X1_plot = self.path[:,plotStates[0]] X2_plot = self.path[:,plotStates[1]] import matplotlib.pyplot as plt # figx = plt.figure() f, axarr = plt.subplots(1, sharex=True) axarr.plot(X1_plot,X2_plot) if self.states is not None: axarr.set_xlabel(self.states[plotStates[0]], fontsize = 20) axarr.set_ylabel(self.states[plotStates[1]], fontsize = 20) axarr.yaxis.set_label_coords(-0.08, 0.5) plt.ion() plt.show() class TimeInterval: def __init__(self,T,Y,j): self.T=T self.Y=Y self.j=j def idem(X): return X def tolEqual(a,b,tol=1e-2): return abs(a-b)<tol def last_row(Y): print 'shape', np.shape(Y) rows = np.shape(Y)[0] print 'rows',rows if rows>1: return Y[-1] else: return Y
gpl-2.0
tequa/ammisoft
ammimain/WinPython-64bit-2.7.13.1Zero/python-2.7.13.amd64/Lib/site-packages/matplotlib/offsetbox.py
10
54984
""" The OffsetBox is a simple container artist. The child artist are meant to be drawn at a relative position to its parent. The [VH]Packer, DrawingArea and TextArea are derived from the OffsetBox. The [VH]Packer automatically adjust the relative postisions of their children, which should be instances of the OffsetBox. This is used to align similar artists together, e.g., in legend. The DrawingArea can contain any Artist as a child. The DrawingArea has a fixed width and height. The position of children relative to the parent is fixed. The TextArea is contains a single Text instance. The width and height of the TextArea instance is the width and height of the its child text. """ from __future__ import (absolute_import, division, print_function, unicode_literals) import six from six.moves import xrange, zip import warnings import matplotlib.transforms as mtransforms import matplotlib.artist as martist import matplotlib.text as mtext import matplotlib.path as mpath import numpy as np from matplotlib.transforms import Bbox, BboxBase, TransformedBbox from matplotlib.font_manager import FontProperties from matplotlib.patches import FancyBboxPatch, FancyArrowPatch from matplotlib import rcParams from matplotlib import docstring #from bboximage import BboxImage from matplotlib.image import BboxImage from matplotlib.patches import bbox_artist as mbbox_artist from matplotlib.text import _AnnotationBase DEBUG = False # for debuging use def bbox_artist(*args, **kwargs): if DEBUG: mbbox_artist(*args, **kwargs) # _get_packed_offsets() and _get_aligned_offsets() are coded assuming # that we are packing boxes horizontally. But same function will be # used with vertical packing. def _get_packed_offsets(wd_list, total, sep, mode="fixed"): """ Geiven a list of (width, xdescent) of each boxes, calculate the total width and the x-offset positions of each items according to *mode*. xdescent is analagous to the usual descent, but along the x-direction. xdescent values are currently ignored. *wd_list* : list of (width, xdescent) of boxes to be packed. *sep* : spacing between boxes *total* : Intended total length. None if not used. *mode* : packing mode. 'fixed', 'expand', or 'equal'. """ w_list, d_list = list(zip(*wd_list)) # d_list is currently not used. if mode == "fixed": offsets_ = np.add.accumulate([0] + [w + sep for w in w_list]) offsets = offsets_[:-1] if total is None: total = offsets_[-1] - sep return total, offsets elif mode == "expand": if len(w_list) > 1: sep = (total - sum(w_list)) / (len(w_list) - 1.) else: sep = 0. offsets_ = np.add.accumulate([0] + [w + sep for w in w_list]) offsets = offsets_[:-1] return total, offsets elif mode == "equal": maxh = max(w_list) if total is None: total = (maxh + sep) * len(w_list) else: sep = float(total) / (len(w_list)) - maxh offsets = np.array([(maxh + sep) * i for i in range(len(w_list))]) return total, offsets else: raise ValueError("Unknown mode : %s" % (mode,)) def _get_aligned_offsets(hd_list, height, align="baseline"): """ Given a list of (height, descent) of each boxes, align the boxes with *align* and calculate the y-offsets of each boxes. total width and the offset positions of each items according to *mode*. xdescent is analogous to the usual descent, but along the x-direction. xdescent values are currently ignored. *hd_list* : list of (width, xdescent) of boxes to be aligned. *sep* : spacing between boxes *height* : Intended total length. None if not used. *align* : align mode. 'baseline', 'top', 'bottom', or 'center'. """ if height is None: height = max([h for h, d in hd_list]) if align == "baseline": height_descent = max([h - d for h, d in hd_list]) descent = max([d for h, d in hd_list]) height = height_descent + descent offsets = [0. for h, d in hd_list] elif align in ["left", "top"]: descent = 0. offsets = [d for h, d in hd_list] elif align in ["right", "bottom"]: descent = 0. offsets = [height - h + d for h, d in hd_list] elif align == "center": descent = 0. offsets = [(height - h) * .5 + d for h, d in hd_list] else: raise ValueError("Unknown Align mode : %s" % (align,)) return height, descent, offsets class OffsetBox(martist.Artist): """ The OffsetBox is a simple container artist. The child artist are meant to be drawn at a relative position to its parent. """ def __init__(self, *args, **kwargs): super(OffsetBox, self).__init__(*args, **kwargs) # Clipping has not been implemented in the OffesetBox family, so # disable the clip flag for consistency. It can always be turned back # on to zero effect. self.set_clip_on(False) self._children = [] self._offset = (0, 0) def __getstate__(self): state = martist.Artist.__getstate__(self) # pickle cannot save instancemethods, so handle them here from .cbook import _InstanceMethodPickler import inspect offset = state['_offset'] if inspect.ismethod(offset): state['_offset'] = _InstanceMethodPickler(offset) return state def __setstate__(self, state): self.__dict__ = state from .cbook import _InstanceMethodPickler if isinstance(self._offset, _InstanceMethodPickler): self._offset = self._offset.get_instancemethod() self.stale = True def set_figure(self, fig): """ Set the figure accepts a class:`~matplotlib.figure.Figure` instance """ martist.Artist.set_figure(self, fig) for c in self.get_children(): c.set_figure(fig) @martist.Artist.axes.setter def axes(self, ax): # TODO deal with this better martist.Artist.axes.fset(self, ax) for c in self.get_children(): if c is not None: c.axes = ax def contains(self, mouseevent): for c in self.get_children(): a, b = c.contains(mouseevent) if a: return a, b return False, {} def set_offset(self, xy): """ Set the offset accepts x, y, tuple, or a callable object. """ self._offset = xy self.stale = True def get_offset(self, width, height, xdescent, ydescent, renderer): """ Get the offset accepts extent of the box """ if six.callable(self._offset): return self._offset(width, height, xdescent, ydescent, renderer) else: return self._offset def set_width(self, width): """ Set the width accepts float """ self.width = width self.stale = True def set_height(self, height): """ Set the height accepts float """ self.height = height self.stale = True def get_visible_children(self): """ Return a list of visible artists it contains. """ return [c for c in self._children if c.get_visible()] def get_children(self): """ Return a list of artists it contains. """ return self._children def get_extent_offsets(self, renderer): raise Exception("") def get_extent(self, renderer): """ Return with, height, xdescent, ydescent of box """ w, h, xd, yd, offsets = self.get_extent_offsets(renderer) return w, h, xd, yd def get_window_extent(self, renderer): ''' get the bounding box in display space. ''' w, h, xd, yd, offsets = self.get_extent_offsets(renderer) px, py = self.get_offset(w, h, xd, yd, renderer) return mtransforms.Bbox.from_bounds(px - xd, py - yd, w, h) def draw(self, renderer): """ Update the location of children if necessary and draw them to the given *renderer*. """ width, height, xdescent, ydescent, offsets = self.get_extent_offsets( renderer) px, py = self.get_offset(width, height, xdescent, ydescent, renderer) for c, (ox, oy) in zip(self.get_visible_children(), offsets): c.set_offset((px + ox, py + oy)) c.draw(renderer) bbox_artist(self, renderer, fill=False, props=dict(pad=0.)) self.stale = False class PackerBase(OffsetBox): def __init__(self, pad=None, sep=None, width=None, height=None, align=None, mode=None, children=None): """ Parameters ---------- pad : float, optional Boundary pad. sep : float, optional Spacing between items. width : float, optional height : float, optional Width and height of the container box, calculated if `None`. align : str, optional Alignment of boxes. Can be one of ``top``, ``bottom``, ``left``, ``right``, ``center`` and ``baseline`` mode : str, optional Packing mode. Notes ----- *pad* and *sep* need to given in points and will be scale with the renderer dpi, while *width* and *height* need to be in pixels. """ super(PackerBase, self).__init__() self.height = height self.width = width self.sep = sep self.pad = pad self.mode = mode self.align = align self._children = children class VPacker(PackerBase): """ The VPacker has its children packed vertically. It automatically adjust the relative positions of children in the drawing time. """ def __init__(self, pad=None, sep=None, width=None, height=None, align="baseline", mode="fixed", children=None): """ Parameters ---------- pad : float, optional Boundary pad. sep : float, optional Spacing between items. width : float, optional height : float, optional width and height of the container box, calculated if `None`. align : str, optional Alignment of boxes. mode : str, optional Packing mode. Notes ----- *pad* and *sep* need to given in points and will be scale with the renderer dpi, while *width* and *height* need to be in pixels. """ super(VPacker, self).__init__(pad, sep, width, height, align, mode, children) def get_extent_offsets(self, renderer): """ update offset of childrens and return the extents of the box """ dpicor = renderer.points_to_pixels(1.) pad = self.pad * dpicor sep = self.sep * dpicor if self.width is not None: for c in self.get_visible_children(): if isinstance(c, PackerBase) and c.mode == "expand": c.set_width(self.width) whd_list = [c.get_extent(renderer) for c in self.get_visible_children()] whd_list = [(w, h, xd, (h - yd)) for w, h, xd, yd in whd_list] wd_list = [(w, xd) for w, h, xd, yd in whd_list] width, xdescent, xoffsets = _get_aligned_offsets(wd_list, self.width, self.align) pack_list = [(h, yd) for w, h, xd, yd in whd_list] height, yoffsets_ = _get_packed_offsets(pack_list, self.height, sep, self.mode) yoffsets = yoffsets_ + [yd for w, h, xd, yd in whd_list] ydescent = height - yoffsets[0] yoffsets = height - yoffsets #w, h, xd, h_yd = whd_list[-1] yoffsets = yoffsets - ydescent return width + 2 * pad, height + 2 * pad, \ xdescent + pad, ydescent + pad, \ list(zip(xoffsets, yoffsets)) class HPacker(PackerBase): """ The HPacker has its children packed horizontally. It automatically adjusts the relative positions of children at draw time. """ def __init__(self, pad=None, sep=None, width=None, height=None, align="baseline", mode="fixed", children=None): """ Parameters ---------- pad : float, optional Boundary pad. sep : float, optional Spacing between items. width : float, optional height : float, optional Width and height of the container box, calculated if `None`. align : str Alignment of boxes. mode : str Packing mode. Notes ----- *pad* and *sep* need to given in points and will be scale with the renderer dpi, while *width* and *height* need to be in pixels. """ super(HPacker, self).__init__(pad, sep, width, height, align, mode, children) def get_extent_offsets(self, renderer): """ update offset of children and return the extents of the box """ dpicor = renderer.points_to_pixels(1.) pad = self.pad * dpicor sep = self.sep * dpicor whd_list = [c.get_extent(renderer) for c in self.get_visible_children()] if not whd_list: return 2 * pad, 2 * pad, pad, pad, [] if self.height is None: height_descent = max([h - yd for w, h, xd, yd in whd_list]) ydescent = max([yd for w, h, xd, yd in whd_list]) height = height_descent + ydescent else: height = self.height - 2 * pad # width w/o pad hd_list = [(h, yd) for w, h, xd, yd in whd_list] height, ydescent, yoffsets = _get_aligned_offsets(hd_list, self.height, self.align) pack_list = [(w, xd) for w, h, xd, yd in whd_list] width, xoffsets_ = _get_packed_offsets(pack_list, self.width, sep, self.mode) xoffsets = xoffsets_ + [xd for w, h, xd, yd in whd_list] xdescent = whd_list[0][2] xoffsets = xoffsets - xdescent return width + 2 * pad, height + 2 * pad, \ xdescent + pad, ydescent + pad, \ list(zip(xoffsets, yoffsets)) class PaddedBox(OffsetBox): def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None): """ *pad* : boundary pad .. note:: *pad* need to given in points and will be scale with the renderer dpi, while *width* and *height* need to be in pixels. """ super(PaddedBox, self).__init__() self.pad = pad self._children = [child] self.patch = FancyBboxPatch( xy=(0.0, 0.0), width=1., height=1., facecolor='w', edgecolor='k', mutation_scale=1, # self.prop.get_size_in_points(), snap=True ) self.patch.set_boxstyle("square", pad=0) if patch_attrs is not None: self.patch.update(patch_attrs) self._drawFrame = draw_frame def get_extent_offsets(self, renderer): """ update offset of childrens and return the extents of the box """ dpicor = renderer.points_to_pixels(1.) pad = self.pad * dpicor w, h, xd, yd = self._children[0].get_extent(renderer) return w + 2 * pad, h + 2 * pad, \ xd + pad, yd + pad, \ [(0, 0)] def draw(self, renderer): """ Update the location of children if necessary and draw them to the given *renderer*. """ width, height, xdescent, ydescent, offsets = self.get_extent_offsets( renderer) px, py = self.get_offset(width, height, xdescent, ydescent, renderer) for c, (ox, oy) in zip(self.get_visible_children(), offsets): c.set_offset((px + ox, py + oy)) self.draw_frame(renderer) for c in self.get_visible_children(): c.draw(renderer) #bbox_artist(self, renderer, fill=False, props=dict(pad=0.)) self.stale = False def update_frame(self, bbox, fontsize=None): self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height) if fontsize: self.patch.set_mutation_scale(fontsize) self.stale = True def draw_frame(self, renderer): # update the location and size of the legend bbox = self.get_window_extent(renderer) self.update_frame(bbox) if self._drawFrame: self.patch.draw(renderer) class DrawingArea(OffsetBox): """ The DrawingArea can contain any Artist as a child. The DrawingArea has a fixed width and height. The position of children relative to the parent is fixed. The children can be clipped at the boundaries of the parent. """ def __init__(self, width, height, xdescent=0., ydescent=0., clip=False): """ *width*, *height* : width and height of the container box. *xdescent*, *ydescent* : descent of the box in x- and y-direction. *clip* : Whether to clip the children """ super(DrawingArea, self).__init__() self.width = width self.height = height self.xdescent = xdescent self.ydescent = ydescent self._clip_children = clip self.offset_transform = mtransforms.Affine2D() self.offset_transform.clear() self.offset_transform.translate(0, 0) self.dpi_transform = mtransforms.Affine2D() @property def clip_children(self): """ If the children of this DrawingArea should be clipped by DrawingArea bounding box. """ return self._clip_children @clip_children.setter def clip_children(self, val): self._clip_children = bool(val) self.stale = True def get_transform(self): """ Return the :class:`~matplotlib.transforms.Transform` applied to the children """ return self.dpi_transform + self.offset_transform def set_transform(self, t): """ set_transform is ignored. """ pass def set_offset(self, xy): """ set offset of the container. Accept : tuple of x,y cooridnate in disokay units. """ self._offset = xy self.offset_transform.clear() self.offset_transform.translate(xy[0], xy[1]) self.stale = True def get_offset(self): """ return offset of the container. """ return self._offset def get_window_extent(self, renderer): ''' get the bounding box in display space. ''' w, h, xd, yd = self.get_extent(renderer) ox, oy = self.get_offset() # w, h, xd, yd) return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h) def get_extent(self, renderer): """ Return with, height, xdescent, ydescent of box """ dpi_cor = renderer.points_to_pixels(1.) return self.width * dpi_cor, self.height * dpi_cor, \ self.xdescent * dpi_cor, self.ydescent * dpi_cor def add_artist(self, a): 'Add any :class:`~matplotlib.artist.Artist` to the container box' self._children.append(a) if not a.is_transform_set(): a.set_transform(self.get_transform()) if self.axes is not None: a.axes = self.axes fig = self.figure if fig is not None: a.set_figure(fig) def draw(self, renderer): """ Draw the children """ dpi_cor = renderer.points_to_pixels(1.) self.dpi_transform.clear() self.dpi_transform.scale(dpi_cor, dpi_cor) # At this point the DrawingArea has a transform # to the display space so the path created is # good for clipping children tpath = mtransforms.TransformedPath( mpath.Path([[0, 0], [0, self.height], [self.width, self.height], [self.width, 0]]), self.get_transform()) for c in self._children: if self._clip_children and not (c.clipbox or c._clippath): c.set_clip_path(tpath) c.draw(renderer) bbox_artist(self, renderer, fill=False, props=dict(pad=0.)) self.stale = False class TextArea(OffsetBox): """ The TextArea is contains a single Text instance. The text is placed at (0,0) with baseline+left alignment. The width and height of the TextArea instance is the width and height of the its child text. """ def __init__(self, s, textprops=None, multilinebaseline=None, minimumdescent=True, ): """ Parameters ---------- s : str a string to be displayed. textprops : `~matplotlib.font_manager.FontProperties`, optional multilinebaseline : bool, optional If `True`, baseline for multiline text is adjusted so that it is (approximatedly) center-aligned with singleline text. minimumdescent : bool, optional If `True`, the box has a minimum descent of "p". """ if textprops is None: textprops = {} if "va" not in textprops: textprops["va"] = "baseline" self._text = mtext.Text(0, 0, s, **textprops) OffsetBox.__init__(self) self._children = [self._text] self.offset_transform = mtransforms.Affine2D() self.offset_transform.clear() self.offset_transform.translate(0, 0) self._baseline_transform = mtransforms.Affine2D() self._text.set_transform(self.offset_transform + self._baseline_transform) self._multilinebaseline = multilinebaseline self._minimumdescent = minimumdescent def set_text(self, s): "Set the text of this area as a string." self._text.set_text(s) self.stale = True def get_text(self): "Returns the string representation of this area's text" return self._text.get_text() def set_multilinebaseline(self, t): """ Set multilinebaseline . If True, baseline for multiline text is adjusted so that it is (approximatedly) center-aligned with singleline text. """ self._multilinebaseline = t self.stale = True def get_multilinebaseline(self): """ get multilinebaseline . """ return self._multilinebaseline def set_minimumdescent(self, t): """ Set minimumdescent . If True, extent of the single line text is adjusted so that it has minimum descent of "p" """ self._minimumdescent = t self.stale = True def get_minimumdescent(self): """ get minimumdescent. """ return self._minimumdescent def set_transform(self, t): """ set_transform is ignored. """ pass def set_offset(self, xy): """ set offset of the container. Accept : tuple of x,y coordinates in display units. """ self._offset = xy self.offset_transform.clear() self.offset_transform.translate(xy[0], xy[1]) self.stale = True def get_offset(self): """ return offset of the container. """ return self._offset def get_window_extent(self, renderer): ''' get the bounding box in display space. ''' w, h, xd, yd = self.get_extent(renderer) ox, oy = self.get_offset() # w, h, xd, yd) return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h) def get_extent(self, renderer): clean_line, ismath = self._text.is_math_text(self._text._text) _, h_, d_ = renderer.get_text_width_height_descent( "lp", self._text._fontproperties, ismath=False) bbox, info, d = self._text._get_layout(renderer) w, h = bbox.width, bbox.height line = info[-1][0] # last line self._baseline_transform.clear() if len(info) > 1 and self._multilinebaseline: d_new = 0.5 * h - 0.5 * (h_ - d_) self._baseline_transform.translate(0, d - d_new) d = d_new else: # single line h_d = max(h_ - d_, h - d) if self.get_minimumdescent(): ## to have a minimum descent, #i.e., "l" and "p" have same ## descents. d = max(d, d_) #else: # d = d h = h_d + d return w, h, 0., d def draw(self, renderer): """ Draw the children """ self._text.draw(renderer) bbox_artist(self, renderer, fill=False, props=dict(pad=0.)) self.stale = False class AuxTransformBox(OffsetBox): """ Offset Box with the aux_transform . Its children will be transformed with the aux_transform first then will be offseted. The absolute coordinate of the aux_transform is meaning as it will be automatically adjust so that the left-lower corner of the bounding box of children will be set to (0,0) before the offset transform. It is similar to drawing area, except that the extent of the box is not predetermined but calculated from the window extent of its children. Furthermore, the extent of the children will be calculated in the transformed coordinate. """ def __init__(self, aux_transform): self.aux_transform = aux_transform OffsetBox.__init__(self) self.offset_transform = mtransforms.Affine2D() self.offset_transform.clear() self.offset_transform.translate(0, 0) # ref_offset_transform is used to make the offset_transform is # always reference to the lower-left corner of the bbox of its # children. self.ref_offset_transform = mtransforms.Affine2D() self.ref_offset_transform.clear() def add_artist(self, a): 'Add any :class:`~matplotlib.artist.Artist` to the container box' self._children.append(a) a.set_transform(self.get_transform()) self.stale = True def get_transform(self): """ Return the :class:`~matplotlib.transforms.Transform` applied to the children """ return self.aux_transform + \ self.ref_offset_transform + \ self.offset_transform def set_transform(self, t): """ set_transform is ignored. """ pass def set_offset(self, xy): """ set offset of the container. Accept : tuple of x,y coordinate in disokay units. """ self._offset = xy self.offset_transform.clear() self.offset_transform.translate(xy[0], xy[1]) self.stale = True def get_offset(self): """ return offset of the container. """ return self._offset def get_window_extent(self, renderer): ''' get the bounding box in display space. ''' w, h, xd, yd = self.get_extent(renderer) ox, oy = self.get_offset() # w, h, xd, yd) return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h) def get_extent(self, renderer): # clear the offset transforms _off = self.offset_transform.to_values() # to be restored later self.ref_offset_transform.clear() self.offset_transform.clear() # calculate the extent bboxes = [c.get_window_extent(renderer) for c in self._children] ub = mtransforms.Bbox.union(bboxes) # adjust ref_offset_tansform self.ref_offset_transform.translate(-ub.x0, -ub.y0) # restor offset transform mtx = self.offset_transform.matrix_from_values(*_off) self.offset_transform.set_matrix(mtx) return ub.width, ub.height, 0., 0. def draw(self, renderer): """ Draw the children """ for c in self._children: c.draw(renderer) bbox_artist(self, renderer, fill=False, props=dict(pad=0.)) self.stale = False class AnchoredOffsetbox(OffsetBox): """ An offset box placed according to the legend location loc. AnchoredOffsetbox has a single child. When multiple children is needed, use other OffsetBox class to enclose them. By default, the offset box is anchored against its parent axes. You may explicitly specify the bbox_to_anchor. """ zorder = 5 # zorder of the legend def __init__(self, loc, pad=0.4, borderpad=0.5, child=None, prop=None, frameon=True, bbox_to_anchor=None, bbox_transform=None, **kwargs): """ loc is a string or an integer specifying the legend location. The valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10, pad : pad around the child for drawing a frame. given in fraction of fontsize. borderpad : pad between offsetbox frame and the bbox_to_anchor, child : OffsetBox instance that will be anchored. prop : font property. This is only used as a reference for paddings. frameon : draw a frame box if True. bbox_to_anchor : bbox to anchor. Use self.axes.bbox if None. bbox_transform : with which the bbox_to_anchor will be transformed. """ super(AnchoredOffsetbox, self).__init__(**kwargs) self.set_bbox_to_anchor(bbox_to_anchor, bbox_transform) self.set_child(child) self.loc = loc self.borderpad = borderpad self.pad = pad if prop is None: self.prop = FontProperties(size=rcParams["legend.fontsize"]) elif isinstance(prop, dict): self.prop = FontProperties(**prop) if "size" not in prop: self.prop.set_size(rcParams["legend.fontsize"]) else: self.prop = prop self.patch = FancyBboxPatch( xy=(0.0, 0.0), width=1., height=1., facecolor='w', edgecolor='k', mutation_scale=self.prop.get_size_in_points(), snap=True ) self.patch.set_boxstyle("square", pad=0) self._drawFrame = frameon def set_child(self, child): "set the child to be anchored" self._child = child if child is not None: child.axes = self.axes self.stale = True def get_child(self): "return the child" return self._child def get_children(self): "return the list of children" return [self._child] def get_extent(self, renderer): """ return the extent of the artist. The extent of the child added with the pad is returned """ w, h, xd, yd = self.get_child().get_extent(renderer) fontsize = renderer.points_to_pixels(self.prop.get_size_in_points()) pad = self.pad * fontsize return w + 2 * pad, h + 2 * pad, xd + pad, yd + pad def get_bbox_to_anchor(self): """ return the bbox that the legend will be anchored """ if self._bbox_to_anchor is None: return self.axes.bbox else: transform = self._bbox_to_anchor_transform if transform is None: return self._bbox_to_anchor else: return TransformedBbox(self._bbox_to_anchor, transform) def set_bbox_to_anchor(self, bbox, transform=None): """ set the bbox that the child will be anchored. *bbox* can be a Bbox instance, a list of [left, bottom, width, height], or a list of [left, bottom] where the width and height will be assumed to be zero. The bbox will be transformed to display coordinate by the given transform. """ if bbox is None or isinstance(bbox, BboxBase): self._bbox_to_anchor = bbox else: try: l = len(bbox) except TypeError: raise ValueError("Invalid argument for bbox : %s" % str(bbox)) if l == 2: bbox = [bbox[0], bbox[1], 0, 0] self._bbox_to_anchor = Bbox.from_bounds(*bbox) self._bbox_to_anchor_transform = transform self.stale = True def get_window_extent(self, renderer): ''' get the bounding box in display space. ''' self._update_offset_func(renderer) w, h, xd, yd = self.get_extent(renderer) ox, oy = self.get_offset(w, h, xd, yd, renderer) return Bbox.from_bounds(ox - xd, oy - yd, w, h) def _update_offset_func(self, renderer, fontsize=None): """ Update the offset func which depends on the dpi of the renderer (because of the padding). """ if fontsize is None: fontsize = renderer.points_to_pixels( self.prop.get_size_in_points()) def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self): bbox = Bbox.from_bounds(0, 0, w, h) borderpad = self.borderpad * fontsize bbox_to_anchor = self.get_bbox_to_anchor() x0, y0 = self._get_anchored_bbox(self.loc, bbox, bbox_to_anchor, borderpad) return x0 + xd, y0 + yd self.set_offset(_offset) def update_frame(self, bbox, fontsize=None): self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height) if fontsize: self.patch.set_mutation_scale(fontsize) def draw(self, renderer): "draw the artist" if not self.get_visible(): return fontsize = renderer.points_to_pixels(self.prop.get_size_in_points()) self._update_offset_func(renderer, fontsize) if self._drawFrame: # update the location and size of the legend bbox = self.get_window_extent(renderer) self.update_frame(bbox, fontsize) self.patch.draw(renderer) width, height, xdescent, ydescent = self.get_extent(renderer) px, py = self.get_offset(width, height, xdescent, ydescent, renderer) self.get_child().set_offset((px, py)) self.get_child().draw(renderer) self.stale = False def _get_anchored_bbox(self, loc, bbox, parentbbox, borderpad): """ return the position of the bbox anchored at the parentbbox with the loc code, with the borderpad. """ assert loc in range(1, 11) # called only internally BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = list(xrange(11)) anchor_coefs = {UR: "NE", UL: "NW", LL: "SW", LR: "SE", R: "E", CL: "W", CR: "E", LC: "S", UC: "N", C: "C"} c = anchor_coefs[loc] container = parentbbox.padded(-borderpad) anchored_box = bbox.anchored(c, container=container) return anchored_box.x0, anchored_box.y0 class AnchoredText(AnchoredOffsetbox): """ AnchoredOffsetbox with Text. """ def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, **kwargs): """ Parameters ---------- s : string Text. loc : str Location code. pad : float, optional Pad between the text and the frame as fraction of the font size. borderpad : float, optional Pad between the frame and the axes (or *bbox_to_anchor*). prop : `matplotlib.font_manager.FontProperties` Font properties. Notes ----- Other keyword parameters of `AnchoredOffsetbox` are also allowed. """ if prop is None: prop = {} propkeys = list(six.iterkeys(prop)) badkwargs = ('ha', 'horizontalalignment', 'va', 'verticalalignment') if set(badkwargs) & set(propkeys): warnings.warn("Mixing horizontalalignment or verticalalignment " "with AnchoredText is not supported.") self.txt = TextArea(s, textprops=prop, minimumdescent=False) fp = self.txt._text.get_fontproperties() super(AnchoredText, self).__init__(loc, pad=pad, borderpad=borderpad, child=self.txt, prop=fp, **kwargs) class OffsetImage(OffsetBox): def __init__(self, arr, zoom=1, cmap=None, norm=None, interpolation=None, origin=None, filternorm=1, filterrad=4.0, resample=False, dpi_cor=True, **kwargs ): OffsetBox.__init__(self) self._dpi_cor = dpi_cor self.image = BboxImage(bbox=self.get_window_extent, cmap=cmap, norm=norm, interpolation=interpolation, origin=origin, filternorm=filternorm, filterrad=filterrad, resample=resample, **kwargs ) self._children = [self.image] self.set_zoom(zoom) self.set_data(arr) def set_data(self, arr): self._data = np.asarray(arr) self.image.set_data(self._data) self.stale = True def get_data(self): return self._data def set_zoom(self, zoom): self._zoom = zoom self.stale = True def get_zoom(self): return self._zoom # def set_axes(self, axes): # self.image.set_axes(axes) # martist.Artist.set_axes(self, axes) # def set_offset(self, xy): # """ # set offset of the container. # Accept : tuple of x,y coordinate in disokay units. # """ # self._offset = xy # self.offset_transform.clear() # self.offset_transform.translate(xy[0], xy[1]) def get_offset(self): """ return offset of the container. """ return self._offset def get_children(self): return [self.image] def get_window_extent(self, renderer): ''' get the bounding box in display space. ''' w, h, xd, yd = self.get_extent(renderer) ox, oy = self.get_offset() return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h) def get_extent(self, renderer): if self._dpi_cor: # True, do correction dpi_cor = renderer.points_to_pixels(1.) else: dpi_cor = 1. zoom = self.get_zoom() data = self.get_data() ny, nx = data.shape[:2] w, h = dpi_cor * nx * zoom, dpi_cor * ny * zoom return w, h, 0, 0 def draw(self, renderer): """ Draw the children """ self.image.draw(renderer) # bbox_artist(self, renderer, fill=False, props=dict(pad=0.)) self.stale = False class AnnotationBbox(martist.Artist, _AnnotationBase): """ Annotation-like class, but with offsetbox instead of Text. """ zorder = 3 def __str__(self): return "AnnotationBbox(%g,%g)" % (self.xy[0], self.xy[1]) @docstring.dedent_interpd def __init__(self, offsetbox, xy, xybox=None, xycoords='data', boxcoords=None, frameon=True, pad=0.4, # BboxPatch annotation_clip=None, box_alignment=(0.5, 0.5), bboxprops=None, arrowprops=None, fontsize=None, **kwargs): """ *offsetbox* : OffsetBox instance *xycoords* : same as Annotation but can be a tuple of two strings which are interpreted as x and y coordinates. *boxcoords* : similar to textcoords as Annotation but can be a tuple of two strings which are interpreted as x and y coordinates. *box_alignment* : a tuple of two floats for a vertical and horizontal alignment of the offset box w.r.t. the *boxcoords*. The lower-left corner is (0.0) and upper-right corner is (1.1). other parameters are identical to that of Annotation. """ martist.Artist.__init__(self, **kwargs) _AnnotationBase.__init__(self, xy, xycoords=xycoords, annotation_clip=annotation_clip) self.offsetbox = offsetbox self.arrowprops = arrowprops self.set_fontsize(fontsize) if xybox is None: self.xybox = xy else: self.xybox = xybox if boxcoords is None: self.boxcoords = xycoords else: self.boxcoords = boxcoords if arrowprops is not None: self._arrow_relpos = self.arrowprops.pop("relpos", (0.5, 0.5)) self.arrow_patch = FancyArrowPatch((0, 0), (1, 1), **self.arrowprops) else: self._arrow_relpos = None self.arrow_patch = None #self._fw, self._fh = 0., 0. # for alignment self._box_alignment = box_alignment # frame self.patch = FancyBboxPatch( xy=(0.0, 0.0), width=1., height=1., facecolor='w', edgecolor='k', mutation_scale=self.prop.get_size_in_points(), snap=True ) self.patch.set_boxstyle("square", pad=pad) if bboxprops: self.patch.set(**bboxprops) self._drawFrame = frameon @property def xyann(self): return self.xybox @xyann.setter def xyann(self, xyann): self.xybox = xyann self.stale = True @property def anncoords(self): return self.boxcoords @anncoords.setter def anncoords(self, coords): self.boxcoords = coords self.stale = True def contains(self, event): t, tinfo = self.offsetbox.contains(event) #if self.arrow_patch is not None: # a,ainfo=self.arrow_patch.contains(event) # t = t or a # self.arrow_patch is currently not checked as this can be a line - JJ return t, tinfo def get_children(self): children = [self.offsetbox, self.patch] if self.arrow_patch: children.append(self.arrow_patch) return children def set_figure(self, fig): if self.arrow_patch is not None: self.arrow_patch.set_figure(fig) self.offsetbox.set_figure(fig) martist.Artist.set_figure(self, fig) def set_fontsize(self, s=None): """ set fontsize in points """ if s is None: s = rcParams["legend.fontsize"] self.prop = FontProperties(size=s) self.stale = True def get_fontsize(self, s=None): """ return fontsize in points """ return self.prop.get_size_in_points() def update_positions(self, renderer): """ Update the pixel positions of the annotated point and the text. """ xy_pixel = self._get_position_xy(renderer) self._update_position_xybox(renderer, xy_pixel) mutation_scale = renderer.points_to_pixels(self.get_fontsize()) self.patch.set_mutation_scale(mutation_scale) if self.arrow_patch: self.arrow_patch.set_mutation_scale(mutation_scale) def _update_position_xybox(self, renderer, xy_pixel): """ Update the pixel positions of the annotation text and the arrow patch. """ x, y = self.xybox if isinstance(self.boxcoords, tuple): xcoord, ycoord = self.boxcoords x1, y1 = self._get_xy(renderer, x, y, xcoord) x2, y2 = self._get_xy(renderer, x, y, ycoord) ox0, oy0 = x1, y2 else: ox0, oy0 = self._get_xy(renderer, x, y, self.boxcoords) w, h, xd, yd = self.offsetbox.get_extent(renderer) _fw, _fh = self._box_alignment self.offsetbox.set_offset((ox0 - _fw * w + xd, oy0 - _fh * h + yd)) # update patch position bbox = self.offsetbox.get_window_extent(renderer) #self.offsetbox.set_offset((ox0-_fw*w, oy0-_fh*h)) self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height) x, y = xy_pixel ox1, oy1 = x, y if self.arrowprops: x0, y0 = x, y d = self.arrowprops.copy() # Use FancyArrowPatch if self.arrowprops has "arrowstyle" key. # adjust the starting point of the arrow relative to # the textbox. # TODO : Rotation needs to be accounted. relpos = self._arrow_relpos ox0 = bbox.x0 + bbox.width * relpos[0] oy0 = bbox.y0 + bbox.height * relpos[1] # The arrow will be drawn from (ox0, oy0) to (ox1, # oy1). It will be first clipped by patchA and patchB. # Then it will be shrinked by shirnkA and shrinkB # (in points). If patch A is not set, self.bbox_patch # is used. self.arrow_patch.set_positions((ox0, oy0), (ox1, oy1)) fs = self.prop.get_size_in_points() mutation_scale = d.pop("mutation_scale", fs) mutation_scale = renderer.points_to_pixels(mutation_scale) self.arrow_patch.set_mutation_scale(mutation_scale) patchA = d.pop("patchA", self.patch) self.arrow_patch.set_patchA(patchA) def draw(self, renderer): """ Draw the :class:`Annotation` object to the given *renderer*. """ if renderer is not None: self._renderer = renderer if not self.get_visible(): return xy_pixel = self._get_position_xy(renderer) if not self._check_xy(renderer, xy_pixel): return self.update_positions(renderer) if self.arrow_patch is not None: if self.arrow_patch.figure is None and self.figure is not None: self.arrow_patch.figure = self.figure self.arrow_patch.draw(renderer) if self._drawFrame: self.patch.draw(renderer) self.offsetbox.draw(renderer) self.stale = False class DraggableBase(object): """ helper code for a draggable artist (legend, offsetbox) The derived class must override following two method. def saveoffset(self): pass def update_offset(self, dx, dy): pass *saveoffset* is called when the object is picked for dragging and it is meant to save reference position of the artist. *update_offset* is called during the dragging. dx and dy is the pixel offset from the point where the mouse drag started. Optionally you may override following two methods. def artist_picker(self, artist, evt): return self.ref_artist.contains(evt) def finalize_offset(self): pass *artist_picker* is a picker method that will be used. *finalize_offset* is called when the mouse is released. In current implementaion of DraggableLegend and DraggableAnnotation, *update_offset* places the artists simply in display coordinates. And *finalize_offset* recalculate their position in the normalized axes coordinate and set a relavant attribute. """ def __init__(self, ref_artist, use_blit=False): self.ref_artist = ref_artist self.got_artist = False self.canvas = self.ref_artist.figure.canvas self._use_blit = use_blit and self.canvas.supports_blit c2 = self.canvas.mpl_connect('pick_event', self.on_pick) c3 = self.canvas.mpl_connect('button_release_event', self.on_release) ref_artist.set_picker(self.artist_picker) self.cids = [c2, c3] def on_motion(self, evt): if self.got_artist: dx = evt.x - self.mouse_x dy = evt.y - self.mouse_y self.update_offset(dx, dy) self.canvas.draw() def on_motion_blit(self, evt): if self.got_artist: dx = evt.x - self.mouse_x dy = evt.y - self.mouse_y self.update_offset(dx, dy) self.canvas.restore_region(self.background) self.ref_artist.draw(self.ref_artist.figure._cachedRenderer) self.canvas.blit(self.ref_artist.figure.bbox) def on_pick(self, evt): if evt.artist == self.ref_artist: self.mouse_x = evt.mouseevent.x self.mouse_y = evt.mouseevent.y self.got_artist = True if self._use_blit: self.ref_artist.set_animated(True) self.canvas.draw() self.background = self.canvas.copy_from_bbox( self.ref_artist.figure.bbox) self.ref_artist.draw(self.ref_artist.figure._cachedRenderer) self.canvas.blit(self.ref_artist.figure.bbox) self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion_blit) else: self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion) self.save_offset() def on_release(self, event): if self.got_artist: self.finalize_offset() self.got_artist = False self.canvas.mpl_disconnect(self._c1) if self._use_blit: self.ref_artist.set_animated(False) def disconnect(self): """disconnect the callbacks""" for cid in self.cids: self.canvas.mpl_disconnect(cid) try: c1 = self._c1 except AttributeError: pass else: self.canvas.mpl_disconnect(c1) def artist_picker(self, artist, evt): return self.ref_artist.contains(evt) def save_offset(self): pass def update_offset(self, dx, dy): pass def finalize_offset(self): pass class DraggableOffsetBox(DraggableBase): def __init__(self, ref_artist, offsetbox, use_blit=False): DraggableBase.__init__(self, ref_artist, use_blit=use_blit) self.offsetbox = offsetbox def save_offset(self): offsetbox = self.offsetbox renderer = offsetbox.figure._cachedRenderer w, h, xd, yd = offsetbox.get_extent(renderer) offset = offsetbox.get_offset(w, h, xd, yd, renderer) self.offsetbox_x, self.offsetbox_y = offset self.offsetbox.set_offset(offset) def update_offset(self, dx, dy): loc_in_canvas = self.offsetbox_x + dx, self.offsetbox_y + dy self.offsetbox.set_offset(loc_in_canvas) def get_loc_in_canvas(self): offsetbox = self.offsetbox renderer = offsetbox.figure._cachedRenderer w, h, xd, yd = offsetbox.get_extent(renderer) ox, oy = offsetbox._offset loc_in_canvas = (ox - xd, oy - yd) return loc_in_canvas class DraggableAnnotation(DraggableBase): def __init__(self, annotation, use_blit=False): DraggableBase.__init__(self, annotation, use_blit=use_blit) self.annotation = annotation def save_offset(self): ann = self.annotation self.ox, self.oy = ann.get_transform().transform(ann.xyann) def update_offset(self, dx, dy): ann = self.annotation ann.xyann = ann.get_transform().inverted().transform( (self.ox + dx, self.oy + dy)) if __name__ == "__main__": import matplotlib.pyplot as plt fig = plt.figure(1) fig.clf() ax = plt.subplot(121) #txt = ax.text(0.5, 0.5, "Test", size=30, ha="center", color="w") kwargs = dict() a = np.arange(256).reshape(16, 16) / 256. myimage = OffsetImage(a, zoom=2, norm=None, origin=None, **kwargs ) ax.add_artist(myimage) myimage.set_offset((100, 100)) myimage2 = OffsetImage(a, zoom=2, norm=None, origin=None, **kwargs ) ann = AnnotationBbox(myimage2, (0.5, 0.5), xybox=(30, 30), xycoords='data', boxcoords="offset points", frameon=True, pad=0.4, # BboxPatch bboxprops=dict(boxstyle="round", fc="y"), fontsize=None, arrowprops=dict(arrowstyle="->"), ) ax.add_artist(ann) plt.draw() plt.show()
bsd-3-clause
lucidfrontier45/scikit-learn
examples/manifold/plot_manifold_sphere.py
1
4572
#!/usr/bin/python # -*- coding: utf-8 -*- """ ============================================= Manifold Learning methods on a severed sphere ============================================= An application of the different :ref:`manifold` techniques on a spherical data-set. Here one can see the use of dimensionality reduction in order to gain some intuition regarding the Manifold learning methods. Regarding the dataset, the poles are cut from the sphere, as well as a thin slice down its side. This enables the manifold learning techniques to 'spread it open' whilst projecting it onto two dimensions. For a similiar example, where the methods are applied to the S-curve dataset, see :ref:`example_manifold_plot_compare_methods.py` Note that the purpose of the :ref:`MDS <multidimensional_scaling>` is to find a low-dimensional representation of the data (here 2D) in which the distances respect well the distances in the original high-dimensional space, unlike other manifold-learning algorithms, it does not seeks an isotropic representation of the data in the low-dimensional space. Here the manifold problem matches fairly that of representing a flat map of the Earth, as with `map projection <http://en.wikipedia.org/wiki/Map_projection>`_ """ # Author: Jaques Grobler <jaques.grobler@inria.fr> # License: BSD print __doc__ from time import time import numpy as np import pylab as pl from mpl_toolkits.mplot3d import Axes3D from matplotlib.ticker import NullFormatter from sklearn import manifold from sklearn.utils import check_random_state # Next line to silence pyflakes. Axes3D # Variables for manifold learning. n_neighbors = 10 n_samples = 1000 # Create our sphere. random_state = check_random_state(0) p = random_state.rand(n_samples) * (2 * np.pi - 0.55) t = random_state.rand(n_samples) * np.pi # Sever the poles from the sphere. indices = ((t < (np.pi - (np.pi / 8))) & (t > ((np.pi / 8)))) colors = p[indices] x, y, z = np.sin(t[indices]) * np.cos(p[indices]), \ np.sin(t[indices]) * np.sin(p[indices]), \ np.cos(t[indices]) # Plot our dataset. fig = pl.figure(figsize=(15, 8)) pl.suptitle("Manifold Learning with %i points, %i neighbors" % (1000, n_neighbors), fontsize=14) ax = fig.add_subplot(241, projection='3d') ax.scatter(x, y, z, c=p[indices], cmap=pl.cm.rainbow) try: # compatibility matplotlib < 1.0 ax.view_init(40, -10) except: pass sphere_data = np.array([x, y, z]).T # Perform Locally Linear Embedding Manifold learning methods = ['standard', 'ltsa', 'hessian', 'modified'] labels = ['LLE', 'LTSA', 'Hessian LLE', 'Modified LLE'] for i, method in enumerate(methods): t0 = time() trans_data = manifold\ .LocallyLinearEmbedding(n_neighbors, 2, method=method).fit_transform(sphere_data).T t1 = time() print "%s: %.2g sec" % (methods[i], t1 - t0) ax = fig.add_subplot(242 + i) pl.scatter(trans_data[0], trans_data[1], c=colors, cmap=pl.cm.rainbow) pl.title("%s (%.2g sec)" % (labels[i], t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) pl.axis('tight') # Perform Isomap Manifold learning. t0 = time() trans_data = manifold.Isomap(n_neighbors, n_components=2)\ .fit_transform(sphere_data).T t1 = time() print "%s: %.2g sec" % ('ISO', t1 - t0) ax = fig.add_subplot(246) pl.scatter(trans_data[0], trans_data[1], c=colors, cmap=pl.cm.rainbow) pl.title("%s (%.2g sec)" % ('Isomap', t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) pl.axis('tight') # Perform Multi-dimensional scaling. t0 = time() mds = manifold.MDS(2, max_iter=100, n_init=1) trans_data = mds.fit_transform(sphere_data).T t1 = time() print "MDS: %.2g sec" % (t1 - t0) ax = fig.add_subplot(247) pl.scatter(trans_data[0], trans_data[1], c=colors, cmap=pl.cm.rainbow) pl.title("MDS (%.2g sec)" % (t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) pl.axis('tight') # Perform Spectral Embedding. t0 = time() se = manifold.SpectralEmbedding(n_components=2, n_neighbors=n_neighbors) trans_data = se.fit_transform(sphere_data).T t1 = time() print "Spectral Embedding: %.2g sec" % (t1 - t0) ax = fig.add_subplot(248) pl.scatter(trans_data[0], trans_data[1], c=colors, cmap=pl.cm.rainbow) pl.title("Spectral Embedding (%.2g sec)" % (t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) pl.axis('tight') pl.show()
bsd-3-clause
chugunovyar/factoryForBuild
env/lib/python2.7/site-packages/matplotlib/tests/test_mlab.py
5
122196
from __future__ import (absolute_import, division, print_function, unicode_literals) import six import tempfile from numpy.testing import assert_allclose, assert_array_equal import numpy.ma.testutils as matest import numpy as np import datetime as datetime from nose.tools import (assert_equal, assert_almost_equal, assert_not_equal, assert_true, assert_raises) import matplotlib.mlab as mlab import matplotlib.cbook as cbook from matplotlib.testing.decorators import knownfailureif, CleanupTestCase try: from mpl_toolkits.natgrid import _natgrid HAS_NATGRID = True except ImportError: HAS_NATGRID = False class general_testcase(CleanupTestCase): def test_colinear_pca(self): a = mlab.PCA._get_colinear() pca = mlab.PCA(a) assert_allclose(pca.fracs[2:], 0., atol=1e-8) assert_allclose(pca.Y[:, 2:], 0., atol=1e-8) def test_prctile(self): # test odd lengths x = [1, 2, 3] assert_equal(mlab.prctile(x, 50), np.median(x)) # test even lengths x = [1, 2, 3, 4] assert_equal(mlab.prctile(x, 50), np.median(x)) # derived from email sent by jason-sage to MPL-user on 20090914 ob1 = [1, 1, 2, 2, 1, 2, 4, 3, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 7, 6, 4, 5, 5] p = [0, 75, 100] expected = [1, 5.5, 9] # test vectorized actual = mlab.prctile(ob1, p) assert_allclose(expected, actual) # test scalar for pi, expectedi in zip(p, expected): actuali = mlab.prctile(ob1, pi) assert_allclose(expectedi, actuali) def test_norm(self): np.random.seed(0) N = 1000 x = np.random.standard_normal(N) targ = np.linalg.norm(x) res = mlab._norm(x) assert_almost_equal(targ, res) class spacing_testcase(CleanupTestCase): def test_logspace_tens(self): xmin = .01 xmax = 1000. N = 6 res = mlab.logspace(xmin, xmax, N) targ = np.logspace(np.log10(xmin), np.log10(xmax), N) assert_allclose(targ, res) def test_logspace_primes(self): xmin = .03 xmax = 1313. N = 7 res = mlab.logspace(xmin, xmax, N) targ = np.logspace(np.log10(xmin), np.log10(xmax), N) assert_allclose(targ, res) def test_logspace_none(self): xmin = .03 xmax = 1313. N = 0 res = mlab.logspace(xmin, xmax, N) targ = np.logspace(np.log10(xmin), np.log10(xmax), N) assert_array_equal(targ, res) assert_equal(res.size, 0) def test_logspace_single(self): xmin = .03 xmax = 1313. N = 1 res = mlab.logspace(xmin, xmax, N) targ = np.logspace(np.log10(xmin), np.log10(xmax), N) assert_array_equal(targ, res) assert_equal(res.size, 1) class stride_testcase(CleanupTestCase): def get_base(self, x): y = x while y.base is not None: y = y.base return y def calc_window_target(self, x, NFFT, noverlap=0): '''This is an adaptation of the original window extraction algorithm. This is here to test to make sure the new implementation has the same result''' step = NFFT - noverlap ind = np.arange(0, len(x) - NFFT + 1, step) n = len(ind) result = np.zeros((NFFT, n)) # do the ffts of the slices for i in range(n): result[:, i] = x[ind[i]:ind[i]+NFFT] return result def test_stride_windows_2D_ValueError(self): x = np.arange(10)[np.newaxis] assert_raises(ValueError, mlab.stride_windows, x, 5) def test_stride_windows_0D_ValueError(self): x = np.array(0) assert_raises(ValueError, mlab.stride_windows, x, 5) def test_stride_windows_noverlap_gt_n_ValueError(self): x = np.arange(10) assert_raises(ValueError, mlab.stride_windows, x, 2, 3) def test_stride_windows_noverlap_eq_n_ValueError(self): x = np.arange(10) assert_raises(ValueError, mlab.stride_windows, x, 2, 2) def test_stride_windows_n_gt_lenx_ValueError(self): x = np.arange(10) assert_raises(ValueError, mlab.stride_windows, x, 11) def test_stride_windows_n_lt_1_ValueError(self): x = np.arange(10) assert_raises(ValueError, mlab.stride_windows, x, 0) def test_stride_repeat_2D_ValueError(self): x = np.arange(10)[np.newaxis] assert_raises(ValueError, mlab.stride_repeat, x, 5) def test_stride_repeat_axis_lt_0_ValueError(self): x = np.array(0) assert_raises(ValueError, mlab.stride_repeat, x, 5, axis=-1) def test_stride_repeat_axis_gt_1_ValueError(self): x = np.array(0) assert_raises(ValueError, mlab.stride_repeat, x, 5, axis=2) def test_stride_repeat_n_lt_1_ValueError(self): x = np.arange(10) assert_raises(ValueError, mlab.stride_repeat, x, 0) def test_stride_repeat_n1_axis0(self): x = np.arange(10) y = mlab.stride_repeat(x, 1) assert_equal((1, ) + x.shape, y.shape) assert_array_equal(x, y.flat) assert_true(self.get_base(y) is x) def test_stride_repeat_n1_axis1(self): x = np.arange(10) y = mlab.stride_repeat(x, 1, axis=1) assert_equal(x.shape + (1, ), y.shape) assert_array_equal(x, y.flat) assert_true(self.get_base(y) is x) def test_stride_repeat_n5_axis0(self): x = np.arange(10) y = mlab.stride_repeat(x, 5) yr = np.repeat(x[np.newaxis], 5, axis=0) assert_equal(yr.shape, y.shape) assert_array_equal(yr, y) assert_equal((5, ) + x.shape, y.shape) assert_true(self.get_base(y) is x) def test_stride_repeat_n5_axis1(self): x = np.arange(10) y = mlab.stride_repeat(x, 5, axis=1) yr = np.repeat(x[np.newaxis], 5, axis=0).T assert_equal(yr.shape, y.shape) assert_array_equal(yr, y) assert_equal(x.shape + (5, ), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n1_noverlap0_axis0(self): x = np.arange(10) y = mlab.stride_windows(x, 1) yt = self.calc_window_target(x, 1) assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((1, ) + x.shape, y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n1_noverlap0_axis1(self): x = np.arange(10) y = mlab.stride_windows(x, 1, axis=1) yt = self.calc_window_target(x, 1).T assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal(x.shape + (1, ), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n5_noverlap0_axis0(self): x = np.arange(100) y = mlab.stride_windows(x, 5) yt = self.calc_window_target(x, 5) assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((5, 20), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n5_noverlap0_axis1(self): x = np.arange(100) y = mlab.stride_windows(x, 5, axis=1) yt = self.calc_window_target(x, 5).T assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((20, 5), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n15_noverlap2_axis0(self): x = np.arange(100) y = mlab.stride_windows(x, 15, 2) yt = self.calc_window_target(x, 15, 2) assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((15, 7), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n15_noverlap2_axis1(self): x = np.arange(100) y = mlab.stride_windows(x, 15, 2, axis=1) yt = self.calc_window_target(x, 15, 2).T assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((7, 15), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n13_noverlapn3_axis0(self): x = np.arange(100) y = mlab.stride_windows(x, 13, -3) yt = self.calc_window_target(x, 13, -3) assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((13, 6), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n13_noverlapn3_axis1(self): x = np.arange(100) y = mlab.stride_windows(x, 13, -3, axis=1) yt = self.calc_window_target(x, 13, -3).T assert_equal(yt.shape, y.shape) assert_array_equal(yt, y) assert_equal((6, 13), y.shape) assert_true(self.get_base(y) is x) def test_stride_windows_n32_noverlap0_axis0_unflatten(self): n = 32 x = np.arange(n)[np.newaxis] x1 = np.tile(x, (21, 1)) x2 = x1.flatten() y = mlab.stride_windows(x2, n) assert_equal(y.shape, x1.T.shape) assert_array_equal(y, x1.T) def test_stride_windows_n32_noverlap0_axis1_unflatten(self): n = 32 x = np.arange(n)[np.newaxis] x1 = np.tile(x, (21, 1)) x2 = x1.flatten() y = mlab.stride_windows(x2, n, axis=1) assert_equal(y.shape, x1.shape) assert_array_equal(y, x1) def test_stride_ensure_integer_type(self): N = 100 x = np.empty(N + 20, dtype='>f4') x.fill(np.NaN) y = x[10:-10] y.fill(0.3) # previous to #3845 lead to corrupt access y_strided = mlab.stride_windows(y, n=33, noverlap=0.6) assert_array_equal(y_strided, 0.3) # previous to #3845 lead to corrupt access y_strided = mlab.stride_windows(y, n=33.3, noverlap=0) assert_array_equal(y_strided, 0.3) # even previous to #3845 could not find any problematic # configuration however, let's be sure it's not accidentally # introduced y_strided = mlab.stride_repeat(y, n=33.815) assert_array_equal(y_strided, 0.3) class csv_testcase(CleanupTestCase): def setUp(self): if six.PY3: self.fd = tempfile.TemporaryFile(suffix='csv', mode="w+", newline='') else: self.fd = tempfile.TemporaryFile(suffix='csv', mode="wb+") def tearDown(self): self.fd.close() def test_recarray_csv_roundtrip(self): expected = np.recarray((99,), [(str('x'), np.float), (str('y'), np.float), (str('t'), np.float)]) # initialising all values: uninitialised memory sometimes produces # floats that do not round-trip to string and back. expected['x'][:] = np.linspace(-1e9, -1, 99) expected['y'][:] = np.linspace(1, 1e9, 99) expected['t'][:] = np.linspace(0, 0.01, 99) mlab.rec2csv(expected, self.fd) self.fd.seek(0) actual = mlab.csv2rec(self.fd) assert_allclose(expected['x'], actual['x']) assert_allclose(expected['y'], actual['y']) assert_allclose(expected['t'], actual['t']) def test_rec2csv_bad_shape_ValueError(self): bad = np.recarray((99, 4), [(str('x'), np.float), (str('y'), np.float)]) # the bad recarray should trigger a ValueError for having ndim > 1. assert_raises(ValueError, mlab.rec2csv, bad, self.fd) def test_csv2rec_names_with_comments(self): self.fd.write('# comment\n1,2,3\n4,5,6\n') self.fd.seek(0) array = mlab.csv2rec(self.fd, names='a,b,c') assert len(array) == 2 assert len(array.dtype) == 3 def test_csv2rec_usdate(self): self.fd.write('01/11/14\n' + '03/05/76 12:00:01 AM\n' + '07/09/83 5:17:34 PM\n' + '06/20/2054 2:31:45 PM\n' + '10/31/00 11:50:23 AM\n') expected = [datetime.datetime(2014, 1, 11, 0, 0), datetime.datetime(1976, 3, 5, 0, 0, 1), datetime.datetime(1983, 7, 9, 17, 17, 34), datetime.datetime(2054, 6, 20, 14, 31, 45), datetime.datetime(2000, 10, 31, 11, 50, 23)] self.fd.seek(0) array = mlab.csv2rec(self.fd, names='a') assert_array_equal(array['a'].tolist(), expected) def test_csv2rec_dayfirst(self): self.fd.write('11/01/14\n' + '05/03/76 12:00:01 AM\n' + '09/07/83 5:17:34 PM\n' + '20/06/2054 2:31:45 PM\n' + '31/10/00 11:50:23 AM\n') expected = [datetime.datetime(2014, 1, 11, 0, 0), datetime.datetime(1976, 3, 5, 0, 0, 1), datetime.datetime(1983, 7, 9, 17, 17, 34), datetime.datetime(2054, 6, 20, 14, 31, 45), datetime.datetime(2000, 10, 31, 11, 50, 23)] self.fd.seek(0) array = mlab.csv2rec(self.fd, names='a', dayfirst=True) assert_array_equal(array['a'].tolist(), expected) def test_csv2rec_yearfirst(self): self.fd.write('14/01/11\n' + '76/03/05 12:00:01 AM\n' + '83/07/09 5:17:34 PM\n' + '2054/06/20 2:31:45 PM\n' + '00/10/31 11:50:23 AM\n') expected = [datetime.datetime(2014, 1, 11, 0, 0), datetime.datetime(1976, 3, 5, 0, 0, 1), datetime.datetime(1983, 7, 9, 17, 17, 34), datetime.datetime(2054, 6, 20, 14, 31, 45), datetime.datetime(2000, 10, 31, 11, 50, 23)] self.fd.seek(0) array = mlab.csv2rec(self.fd, names='a', yearfirst=True) assert_array_equal(array['a'].tolist(), expected) class rec2txt_testcase(CleanupTestCase): def test_csv2txt_basic(self): # str() calls around field names necessary b/c as of numpy 1.11 # dtype doesn't like unicode names (caused by unicode_literals import) a = np.array([(1.0, 2, 'foo', 'bing'), (2.0, 3, 'bar', 'blah')], dtype=np.dtype([(str('x'), np.float32), (str('y'), np.int8), (str('s'), str, 3), (str('s2'), str, 4)])) truth = (' x y s s2\n' ' 1.000 2 foo bing \n' ' 2.000 3 bar blah ').splitlines() assert_equal(mlab.rec2txt(a).splitlines(), truth) class window_testcase(CleanupTestCase): def setUp(self): np.random.seed(0) self.n = 1000 self.x = np.arange(0., self.n) self.sig_rand = np.random.standard_normal(self.n) + 100. self.sig_ones = np.ones_like(self.x) self.sig_slope = np.linspace(-10., 90., self.n) def check_window_apply_repeat(self, x, window, NFFT, noverlap): '''This is an adaptation of the original window application algorithm. This is here to test to make sure the new implementation has the same result''' step = NFFT - noverlap ind = np.arange(0, len(x) - NFFT + 1, step) n = len(ind) result = np.zeros((NFFT, n)) if cbook.iterable(window): windowVals = window else: windowVals = window(np.ones((NFFT,), x.dtype)) # do the ffts of the slices for i in range(n): result[:, i] = windowVals * x[ind[i]:ind[i]+NFFT] return result def test_window_none_rand(self): res = mlab.window_none(self.sig_ones) assert_array_equal(res, self.sig_ones) def test_window_none_ones(self): res = mlab.window_none(self.sig_rand) assert_array_equal(res, self.sig_rand) def test_window_hanning_rand(self): targ = np.hanning(len(self.sig_rand)) * self.sig_rand res = mlab.window_hanning(self.sig_rand) assert_allclose(targ, res, atol=1e-06) def test_window_hanning_ones(self): targ = np.hanning(len(self.sig_ones)) res = mlab.window_hanning(self.sig_ones) assert_allclose(targ, res, atol=1e-06) def test_apply_window_1D_axis1_ValueError(self): x = self.sig_rand window = mlab.window_hanning assert_raises(ValueError, mlab.apply_window, x, window, axis=1, return_window=False) def test_apply_window_1D_els_wrongsize_ValueError(self): x = self.sig_rand window = mlab.window_hanning(np.ones(x.shape[0]-1)) assert_raises(ValueError, mlab.apply_window, x, window) def test_apply_window_0D_ValueError(self): x = np.array(0) window = mlab.window_hanning assert_raises(ValueError, mlab.apply_window, x, window, axis=1, return_window=False) def test_apply_window_3D_ValueError(self): x = self.sig_rand[np.newaxis][np.newaxis] window = mlab.window_hanning assert_raises(ValueError, mlab.apply_window, x, window, axis=1, return_window=False) def test_apply_window_hanning_1D(self): x = self.sig_rand window = mlab.window_hanning window1 = mlab.window_hanning(np.ones(x.shape[0])) y, window2 = mlab.apply_window(x, window, return_window=True) yt = window(x) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) def test_apply_window_hanning_1D_axis0(self): x = self.sig_rand window = mlab.window_hanning y = mlab.apply_window(x, window, axis=0, return_window=False) yt = window(x) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_els_1D_axis0(self): x = self.sig_rand window = mlab.window_hanning(np.ones(x.shape[0])) window1 = mlab.window_hanning y = mlab.apply_window(x, window, axis=0, return_window=False) yt = window1(x) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D_axis0(self): x = np.random.standard_normal([1000, 10]) + 100. window = mlab.window_hanning y = mlab.apply_window(x, window, axis=0, return_window=False) yt = np.zeros_like(x) for i in range(x.shape[1]): yt[:, i] = window(x[:, i]) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_els1_2D_axis0(self): x = np.random.standard_normal([1000, 10]) + 100. window = mlab.window_hanning(np.ones(x.shape[0])) window1 = mlab.window_hanning y = mlab.apply_window(x, window, axis=0, return_window=False) yt = np.zeros_like(x) for i in range(x.shape[1]): yt[:, i] = window1(x[:, i]) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_els2_2D_axis0(self): x = np.random.standard_normal([1000, 10]) + 100. window = mlab.window_hanning window1 = mlab.window_hanning(np.ones(x.shape[0])) y, window2 = mlab.apply_window(x, window, axis=0, return_window=True) yt = np.zeros_like(x) for i in range(x.shape[1]): yt[:, i] = window1*x[:, i] assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) def test_apply_window_hanning_els3_2D_axis0(self): x = np.random.standard_normal([1000, 10]) + 100. window = mlab.window_hanning window1 = mlab.window_hanning(np.ones(x.shape[0])) y, window2 = mlab.apply_window(x, window, axis=0, return_window=True) yt = mlab.apply_window(x, window1, axis=0, return_window=False) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) def test_apply_window_hanning_2D_axis1(self): x = np.random.standard_normal([10, 1000]) + 100. window = mlab.window_hanning y = mlab.apply_window(x, window, axis=1, return_window=False) yt = np.zeros_like(x) for i in range(x.shape[0]): yt[i, :] = window(x[i, :]) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D__els1_axis1(self): x = np.random.standard_normal([10, 1000]) + 100. window = mlab.window_hanning(np.ones(x.shape[1])) window1 = mlab.window_hanning y = mlab.apply_window(x, window, axis=1, return_window=False) yt = np.zeros_like(x) for i in range(x.shape[0]): yt[i, :] = window1(x[i, :]) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D_els2_axis1(self): x = np.random.standard_normal([10, 1000]) + 100. window = mlab.window_hanning window1 = mlab.window_hanning(np.ones(x.shape[1])) y, window2 = mlab.apply_window(x, window, axis=1, return_window=True) yt = np.zeros_like(x) for i in range(x.shape[0]): yt[i, :] = window1 * x[i, :] assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) def test_apply_window_hanning_2D_els3_axis1(self): x = np.random.standard_normal([10, 1000]) + 100. window = mlab.window_hanning window1 = mlab.window_hanning(np.ones(x.shape[1])) y = mlab.apply_window(x, window, axis=1, return_window=False) yt = mlab.apply_window(x, window1, axis=1, return_window=False) assert_equal(yt.shape, y.shape) assert_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_stride_windows_hanning_2D_n13_noverlapn3_axis0(self): x = self.sig_rand window = mlab.window_hanning yi = mlab.stride_windows(x, n=13, noverlap=2, axis=0) y = mlab.apply_window(yi, window, axis=0, return_window=False) yt = self.check_window_apply_repeat(x, window, 13, 2) assert_equal(yt.shape, y.shape) assert_not_equal(x.shape, y.shape) assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D_stack_axis1(self): ydata = np.arange(32) ydata1 = ydata+5 ydata2 = ydata+3.3 ycontrol1 = mlab.apply_window(ydata1, mlab.window_hanning) ycontrol2 = mlab.window_hanning(ydata2) ydata = np.vstack([ydata1, ydata2]) ycontrol = np.vstack([ycontrol1, ycontrol2]) ydata = np.tile(ydata, (20, 1)) ycontrol = np.tile(ycontrol, (20, 1)) result = mlab.apply_window(ydata, mlab.window_hanning, axis=1, return_window=False) assert_allclose(ycontrol, result, atol=1e-08) def test_apply_window_hanning_2D_stack_windows_axis1(self): ydata = np.arange(32) ydata1 = ydata+5 ydata2 = ydata+3.3 ycontrol1 = mlab.apply_window(ydata1, mlab.window_hanning) ycontrol2 = mlab.window_hanning(ydata2) ydata = np.vstack([ydata1, ydata2]) ycontrol = np.vstack([ycontrol1, ycontrol2]) ydata = np.tile(ydata, (20, 1)) ycontrol = np.tile(ycontrol, (20, 1)) result = mlab.apply_window(ydata, mlab.window_hanning, axis=1, return_window=False) assert_allclose(ycontrol, result, atol=1e-08) def test_apply_window_hanning_2D_stack_windows_axis1_unflatten(self): n = 32 ydata = np.arange(n) ydata1 = ydata+5 ydata2 = ydata+3.3 ycontrol1 = mlab.apply_window(ydata1, mlab.window_hanning) ycontrol2 = mlab.window_hanning(ydata2) ydata = np.vstack([ydata1, ydata2]) ycontrol = np.vstack([ycontrol1, ycontrol2]) ydata = np.tile(ydata, (20, 1)) ycontrol = np.tile(ycontrol, (20, 1)) ydata = ydata.flatten() ydata1 = mlab.stride_windows(ydata, 32, noverlap=0, axis=0) result = mlab.apply_window(ydata1, mlab.window_hanning, axis=0, return_window=False) assert_allclose(ycontrol.T, result, atol=1e-08) class detrend_testcase(CleanupTestCase): def setUp(self): np.random.seed(0) n = 1000 x = np.linspace(0., 100, n) self.sig_zeros = np.zeros(n) self.sig_off = self.sig_zeros + 100. self.sig_slope = np.linspace(-10., 90., n) self.sig_slope_mean = x - x.mean() sig_rand = np.random.standard_normal(n) sig_sin = np.sin(x*2*np.pi/(n/100)) sig_rand -= sig_rand.mean() sig_sin -= sig_sin.mean() self.sig_base = sig_rand + sig_sin self.atol = 1e-08 def test_detrend_none_0D_zeros(self): input = 0. targ = input res = mlab.detrend_none(input) assert_equal(input, targ) def test_detrend_none_0D_zeros_axis1(self): input = 0. targ = input res = mlab.detrend_none(input, axis=1) assert_equal(input, targ) def test_detrend_str_none_0D_zeros(self): input = 0. targ = input res = mlab.detrend(input, key='none') assert_equal(input, targ) def test_detrend_detrend_none_0D_zeros(self): input = 0. targ = input res = mlab.detrend(input, key=mlab.detrend_none) assert_equal(input, targ) def test_detrend_none_0D_off(self): input = 5.5 targ = input res = mlab.detrend_none(input) assert_equal(input, targ) def test_detrend_none_1D_off(self): input = self.sig_off targ = input res = mlab.detrend_none(input) assert_array_equal(res, targ) def test_detrend_none_1D_slope(self): input = self.sig_slope targ = input res = mlab.detrend_none(input) assert_array_equal(res, targ) def test_detrend_none_1D_base(self): input = self.sig_base targ = input res = mlab.detrend_none(input) assert_array_equal(res, targ) def test_detrend_none_1D_base_slope_off_list(self): input = self.sig_base + self.sig_slope + self.sig_off targ = input.tolist() res = mlab.detrend_none(input.tolist()) assert_equal(res, targ) def test_detrend_none_2D(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] input = np.vstack(arri) targ = input res = mlab.detrend_none(input) assert_array_equal(res, targ) def test_detrend_none_2D_T(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] input = np.vstack(arri) targ = input res = mlab.detrend_none(input.T) assert_array_equal(res.T, targ) def test_detrend_mean_0D_zeros(self): input = 0. targ = 0. res = mlab.detrend_mean(input) assert_almost_equal(res, targ) def test_detrend_str_mean_0D_zeros(self): input = 0. targ = 0. res = mlab.detrend(input, key='mean') assert_almost_equal(res, targ) def test_detrend_detrend_mean_0D_zeros(self): input = 0. targ = 0. res = mlab.detrend(input, key=mlab.detrend_mean) assert_almost_equal(res, targ) def test_detrend_mean_0D_off(self): input = 5.5 targ = 0. res = mlab.detrend_mean(input) assert_almost_equal(res, targ) def test_detrend_str_mean_0D_off(self): input = 5.5 targ = 0. res = mlab.detrend(input, key='mean') assert_almost_equal(res, targ) def test_detrend_detrend_mean_0D_off(self): input = 5.5 targ = 0. res = mlab.detrend(input, key=mlab.detrend_mean) assert_almost_equal(res, targ) def test_detrend_mean_1D_zeros(self): input = self.sig_zeros targ = self.sig_zeros res = mlab.detrend_mean(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_mean_1D_base(self): input = self.sig_base targ = self.sig_base res = mlab.detrend_mean(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_mean_1D_base_off(self): input = self.sig_base + self.sig_off targ = self.sig_base res = mlab.detrend_mean(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_mean_1D_base_slope(self): input = self.sig_base + self.sig_slope targ = self.sig_base + self.sig_slope_mean res = mlab.detrend_mean(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_mean_1D_base_slope_off(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.detrend_mean(input) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_1D_base_slope_off_axis0(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.detrend_mean(input, axis=0) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_1D_base_slope_off_list(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.detrend_mean(input.tolist()) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_1D_base_slope_off_list_axis0(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.detrend_mean(input.tolist(), axis=0) assert_allclose(res, targ, atol=1e-08) def test_demean_0D_off(self): input = 5.5 targ = 0. res = mlab.demean(input, axis=None) assert_almost_equal(res, targ) def test_demean_1D_base_slope_off(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.demean(input) assert_allclose(res, targ, atol=1e-08) def test_demean_1D_base_slope_off_axis0(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.demean(input, axis=0) assert_allclose(res, targ, atol=1e-08) def test_demean_1D_base_slope_off_list(self): input = self.sig_base + self.sig_slope + self.sig_off targ = self.sig_base + self.sig_slope_mean res = mlab.demean(input.tolist()) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_default(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_none(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=None) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_none_T(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri).T targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=None) assert_allclose(res.T, targ, atol=1e-08) def test_detrend_mean_2D_axis0(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.detrend_mean(input, axis=0) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_axis1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=1) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_axism1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=-1) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_none(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=None) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_none_T(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri).T targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=None) assert_allclose(res.T, targ, atol=1e-08) def test_detrend_mean_2D_axis0(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.detrend_mean(input, axis=0) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_axis1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=1) assert_allclose(res, targ, atol=1e-08) def test_detrend_mean_2D_axism1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend_mean(input, axis=-1) assert_allclose(res, targ, atol=1e-08) def test_detrend_2D_default(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend(input) assert_allclose(res, targ, atol=1e-08) def test_detrend_2D_none(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend(input, axis=None) assert_allclose(res, targ, atol=1e-08) def test_detrend_str_mean_2D_axis0(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.detrend(input, key='mean', axis=0) assert_allclose(res, targ, atol=1e-08) def test_detrend_str_constant_2D_none_T(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri).T targ = np.vstack(arrt) res = mlab.detrend(input, key='constant', axis=None) assert_allclose(res.T, targ, atol=1e-08) def test_detrend_str_default_2D_axis1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend(input, key='default', axis=1) assert_allclose(res, targ, atol=1e-08) def test_detrend_detrend_mean_2D_axis0(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.detrend(input, key=mlab.detrend_mean, axis=0) assert_allclose(res, targ, atol=1e-08) def test_demean_2D_default(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.demean(input) assert_allclose(res, targ, atol=1e-08) def test_demean_2D_none(self): arri = [self.sig_off, self.sig_base + self.sig_off] arrt = [self.sig_zeros, self.sig_base] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.demean(input, axis=None) assert_allclose(res, targ, atol=1e-08) def test_demean_2D_axis0(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.demean(input, axis=0) assert_allclose(res, targ, atol=1e-08) def test_demean_2D_axis1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.demean(input, axis=1) assert_allclose(res, targ, atol=1e-08) def test_demean_2D_axism1(self): arri = [self.sig_base, self.sig_base + self.sig_off, self.sig_base + self.sig_slope, self.sig_base + self.sig_off + self.sig_slope] arrt = [self.sig_base, self.sig_base, self.sig_base + self.sig_slope_mean, self.sig_base + self.sig_slope_mean] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.demean(input, axis=-1) assert_allclose(res, targ, atol=1e-08) def test_detrend_bad_key_str_ValueError(self): input = self.sig_slope[np.newaxis] assert_raises(ValueError, mlab.detrend, input, key='spam') def test_detrend_bad_key_var_ValueError(self): input = self.sig_slope[np.newaxis] assert_raises(ValueError, mlab.detrend, input, key=5) def test_detrend_mean_0D_d0_ValueError(self): input = 5.5 assert_raises(ValueError, mlab.detrend_mean, input, axis=0) def test_detrend_0D_d0_ValueError(self): input = 5.5 assert_raises(ValueError, mlab.detrend, input, axis=0) def test_detrend_mean_1D_d1_ValueError(self): input = self.sig_slope assert_raises(ValueError, mlab.detrend_mean, input, axis=1) def test_detrend_1D_d1_ValueError(self): input = self.sig_slope assert_raises(ValueError, mlab.detrend, input, axis=1) def test_demean_1D_d1_ValueError(self): input = self.sig_slope assert_raises(ValueError, mlab.demean, input, axis=1) def test_detrend_mean_2D_d2_ValueError(self): input = self.sig_slope[np.newaxis] assert_raises(ValueError, mlab.detrend_mean, input, axis=2) def test_detrend_2D_d2_ValueError(self): input = self.sig_slope[np.newaxis] assert_raises(ValueError, mlab.detrend, input, axis=2) def test_demean_2D_d2_ValueError(self): input = self.sig_slope[np.newaxis] assert_raises(ValueError, mlab.demean, input, axis=2) def test_detrend_linear_0D_zeros(self): input = 0. targ = 0. res = mlab.detrend_linear(input) assert_almost_equal(res, targ) def test_detrend_linear_0D_off(self): input = 5.5 targ = 0. res = mlab.detrend_linear(input) assert_almost_equal(res, targ) def test_detrend_str_linear_0D_off(self): input = 5.5 targ = 0. res = mlab.detrend(input, key='linear') assert_almost_equal(res, targ) def test_detrend_detrend_linear_0D_off(self): input = 5.5 targ = 0. res = mlab.detrend(input, key=mlab.detrend_linear) assert_almost_equal(res, targ) def test_detrend_linear_1d_off(self): input = self.sig_off targ = self.sig_zeros res = mlab.detrend_linear(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_linear_1d_slope(self): input = self.sig_slope targ = self.sig_zeros res = mlab.detrend_linear(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_linear_1d_slope_off(self): input = self.sig_slope + self.sig_off targ = self.sig_zeros res = mlab.detrend_linear(input) assert_allclose(res, targ, atol=self.atol) def test_detrend_str_linear_1d_slope_off(self): input = self.sig_slope + self.sig_off targ = self.sig_zeros res = mlab.detrend(input, key='linear') assert_allclose(res, targ, atol=self.atol) def test_detrend_detrend_linear_1d_slope_off(self): input = self.sig_slope + self.sig_off targ = self.sig_zeros res = mlab.detrend(input, key=mlab.detrend_linear) assert_allclose(res, targ, atol=self.atol) def test_detrend_linear_1d_slope_off_list(self): input = self.sig_slope + self.sig_off targ = self.sig_zeros res = mlab.detrend_linear(input.tolist()) assert_allclose(res, targ, atol=self.atol) def test_detrend_linear_2D_ValueError(self): input = self.sig_slope[np.newaxis] assert_raises(ValueError, mlab.detrend_linear, input) def test_detrend_str_linear_2d_slope_off_axis0(self): arri = [self.sig_off, self.sig_slope, self.sig_slope + self.sig_off] arrt = [self.sig_zeros, self.sig_zeros, self.sig_zeros] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.detrend(input, key='linear', axis=0) assert_allclose(res, targ, atol=self.atol) def test_detrend_detrend_linear_1d_slope_off_axis1(self): arri = [self.sig_off, self.sig_slope, self.sig_slope + self.sig_off] arrt = [self.sig_zeros, self.sig_zeros, self.sig_zeros] input = np.vstack(arri).T targ = np.vstack(arrt).T res = mlab.detrend(input, key=mlab.detrend_linear, axis=0) assert_allclose(res, targ, atol=self.atol) def test_detrend_str_linear_2d_slope_off_axis0(self): arri = [self.sig_off, self.sig_slope, self.sig_slope + self.sig_off] arrt = [self.sig_zeros, self.sig_zeros, self.sig_zeros] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend(input, key='linear', axis=1) assert_allclose(res, targ, atol=self.atol) def test_detrend_detrend_linear_1d_slope_off_axis1(self): arri = [self.sig_off, self.sig_slope, self.sig_slope + self.sig_off] arrt = [self.sig_zeros, self.sig_zeros, self.sig_zeros] input = np.vstack(arri) targ = np.vstack(arrt) res = mlab.detrend(input, key=mlab.detrend_linear, axis=1) assert_allclose(res, targ, atol=self.atol) class spectral_testcase_nosig_real_onesided(CleanupTestCase): def setUp(self): self.createStim(fstims=[], iscomplex=False, sides='onesided', nsides=1) def createStim(self, fstims, iscomplex, sides, nsides, len_x=None, NFFT_density=-1, nover_density=-1, pad_to_density=-1, pad_to_spectrum=-1): Fs = 100. x = np.arange(0, 10, 1/Fs) if len_x is not None: x = x[:len_x] # get the stimulus frequencies, defaulting to None fstims = [Fs/fstim for fstim in fstims] # get the constants, default to calculated values if NFFT_density is None: NFFT_density_real = 256 elif NFFT_density < 0: NFFT_density_real = NFFT_density = 100 else: NFFT_density_real = NFFT_density if nover_density is None: nover_density_real = 0 elif nover_density < 0: nover_density_real = nover_density = NFFT_density_real//2 else: nover_density_real = nover_density if pad_to_density is None: pad_to_density_real = NFFT_density_real elif pad_to_density < 0: pad_to_density = int(2**np.ceil(np.log2(NFFT_density_real))) pad_to_density_real = pad_to_density else: pad_to_density_real = pad_to_density if pad_to_spectrum is None: pad_to_spectrum_real = len(x) elif pad_to_spectrum < 0: pad_to_spectrum_real = pad_to_spectrum = len(x) else: pad_to_spectrum_real = pad_to_spectrum if pad_to_spectrum is None: NFFT_spectrum_real = NFFT_spectrum = pad_to_spectrum_real else: NFFT_spectrum_real = NFFT_spectrum = len(x) nover_spectrum_real = nover_spectrum = 0 NFFT_specgram = NFFT_density nover_specgram = nover_density pad_to_specgram = pad_to_density NFFT_specgram_real = NFFT_density_real nover_specgram_real = nover_density_real if nsides == 1: # frequencies for specgram, psd, and csd # need to handle even and odd differently if pad_to_density_real % 2: freqs_density = np.linspace(0, Fs/2, num=pad_to_density_real, endpoint=False)[::2] else: freqs_density = np.linspace(0, Fs/2, num=pad_to_density_real//2+1) # frequencies for complex, magnitude, angle, and phase spectrums # need to handle even and odd differently if pad_to_spectrum_real % 2: freqs_spectrum = np.linspace(0, Fs/2, num=pad_to_spectrum_real, endpoint=False)[::2] else: freqs_spectrum = np.linspace(0, Fs/2, num=pad_to_spectrum_real//2+1) else: # frequencies for specgram, psd, and csd # need to handle even and odd differentl if pad_to_density_real % 2: freqs_density = np.linspace(-Fs/2, Fs/2, num=2*pad_to_density_real, endpoint=False)[1::2] else: freqs_density = np.linspace(-Fs/2, Fs/2, num=pad_to_density_real, endpoint=False) # frequencies for complex, magnitude, angle, and phase spectrums # need to handle even and odd differently if pad_to_spectrum_real % 2: freqs_spectrum = np.linspace(-Fs/2, Fs/2, num=2*pad_to_spectrum_real, endpoint=False)[1::2] else: freqs_spectrum = np.linspace(-Fs/2, Fs/2, num=pad_to_spectrum_real, endpoint=False) freqs_specgram = freqs_density # time points for specgram t_start = NFFT_specgram_real//2 t_stop = len(x) - NFFT_specgram_real//2+1 t_step = NFFT_specgram_real - nover_specgram_real t_specgram = x[t_start:t_stop:t_step] if NFFT_specgram_real % 2: t_specgram += 1/Fs/2 if len(t_specgram) == 0: t_specgram = np.array([NFFT_specgram_real/(2*Fs)]) t_spectrum = np.array([NFFT_spectrum_real/(2*Fs)]) t_density = t_specgram y = np.zeros_like(x) for i, fstim in enumerate(fstims): y += np.sin(fstim * x * np.pi * 2) * 10**i if iscomplex: y = y.astype('complex') self.Fs = Fs self.sides = sides self.fstims = fstims self.NFFT_density = NFFT_density self.nover_density = nover_density self.pad_to_density = pad_to_density self.NFFT_spectrum = NFFT_spectrum self.nover_spectrum = nover_spectrum self.pad_to_spectrum = pad_to_spectrum self.NFFT_specgram = NFFT_specgram self.nover_specgram = nover_specgram self.pad_to_specgram = pad_to_specgram self.t_specgram = t_specgram self.t_density = t_density self.t_spectrum = t_spectrum self.y = y self.freqs_density = freqs_density self.freqs_spectrum = freqs_spectrum self.freqs_specgram = freqs_specgram self.NFFT_density_real = NFFT_density_real def check_freqs(self, vals, targfreqs, resfreqs, fstims): assert_true(resfreqs.argmin() == 0) assert_true(resfreqs.argmax() == len(resfreqs)-1) assert_allclose(resfreqs, targfreqs, atol=1e-06) for fstim in fstims: i = np.abs(resfreqs - fstim).argmin() assert_true(vals[i] > vals[i+2]) assert_true(vals[i] > vals[i-2]) def check_maxfreq(self, spec, fsp, fstims): # skip the test if there are no frequencies if len(fstims) == 0: return # if twosided, do the test for each side if fsp.min() < 0: fspa = np.abs(fsp) zeroind = fspa.argmin() self.check_maxfreq(spec[:zeroind], fspa[:zeroind], fstims) self.check_maxfreq(spec[zeroind:], fspa[zeroind:], fstims) return fstimst = fstims[:] spect = spec.copy() # go through each peak and make sure it is correctly the maximum peak while fstimst: maxind = spect.argmax() maxfreq = fsp[maxind] assert_almost_equal(maxfreq, fstimst[-1]) del fstimst[-1] spect[maxind-5:maxind+5] = 0 def test_spectral_helper_raises_complex_same_data(self): # test that mode 'complex' cannot be used if x is not y assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y+1, mode='complex') def test_spectral_helper_raises_magnitude_same_data(self): # test that mode 'magnitude' cannot be used if x is not y assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y+1, mode='magnitude') def test_spectral_helper_raises_angle_same_data(self): # test that mode 'angle' cannot be used if x is not y assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y+1, mode='angle') def test_spectral_helper_raises_phase_same_data(self): # test that mode 'phase' cannot be used if x is not y assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y+1, mode='phase') def test_spectral_helper_raises_unknown_mode(self): # test that unknown value for mode cannot be used assert_raises(ValueError, mlab._spectral_helper, x=self.y, mode='spam') def test_spectral_helper_raises_unknown_sides(self): # test that unknown value for sides cannot be used assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y, sides='eggs') def test_spectral_helper_raises_noverlap_gt_NFFT(self): # test that noverlap cannot be larger than NFFT assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y, NFFT=10, noverlap=20) def test_spectral_helper_raises_noverlap_eq_NFFT(self): # test that noverlap cannot be equal to NFFT assert_raises(ValueError, mlab._spectral_helper, x=self.y, NFFT=10, noverlap=10) def test_spectral_helper_raises_winlen_ne_NFFT(self): # test that the window length cannot be different from NFFT assert_raises(ValueError, mlab._spectral_helper, x=self.y, y=self.y, NFFT=10, window=np.ones(9)) def test_single_spectrum_helper_raises_mode_default(self): # test that mode 'default' cannot be used with _single_spectrum_helper assert_raises(ValueError, mlab._single_spectrum_helper, x=self.y, mode='default') def test_single_spectrum_helper_raises_mode_psd(self): # test that mode 'psd' cannot be used with _single_spectrum_helper assert_raises(ValueError, mlab._single_spectrum_helper, x=self.y, mode='psd') def test_spectral_helper_psd(self): freqs = self.freqs_density spec, fsp, t = mlab._spectral_helper(x=self.y, y=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides, mode='psd') assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_density, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) def test_spectral_helper_magnitude_specgram(self): freqs = self.freqs_specgram spec, fsp, t = mlab._spectral_helper(x=self.y, y=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='magnitude') assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) def test_spectral_helper_magnitude_magnitude_spectrum(self): freqs = self.freqs_spectrum spec, fsp, t = mlab._spectral_helper(x=self.y, y=self.y, NFFT=self.NFFT_spectrum, Fs=self.Fs, noverlap=self.nover_spectrum, pad_to=self.pad_to_spectrum, sides=self.sides, mode='magnitude') assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_spectrum, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], 1) def test_csd(self): freqs = self.freqs_density spec, fsp = mlab.csd(x=self.y, y=self.y+1, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides) assert_allclose(fsp, freqs, atol=1e-06) assert_equal(spec.shape, freqs.shape) def test_psd(self): freqs = self.freqs_density spec, fsp = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides) assert_equal(spec.shape, freqs.shape) self.check_freqs(spec, freqs, fsp, self.fstims) def test_psd_detrend_mean_func_offset(self): if self.NFFT_density is None: return freqs = self.freqs_density ydata = np.zeros(self.NFFT_density) ydata1 = ydata+5 ydata2 = ydata+3.3 ydata = np.vstack([ydata1, ydata2]) ydata = np.tile(ydata, (20, 1)) ydatab = ydata.T.flatten() ydata = ydata.flatten() ycontrol = np.zeros_like(ydata) spec_g, fsp_g = mlab.psd(x=ydata, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend=mlab.detrend_mean) spec_b, fsp_b = mlab.psd(x=ydatab, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend=mlab.detrend_mean) spec_c, fsp_c = mlab.psd(x=ycontrol, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides) assert_array_equal(fsp_g, fsp_c) assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal assert_raises(AssertionError, assert_allclose, spec_b, spec_c, atol=1e-08) def test_psd_detrend_mean_str_offset(self): if self.NFFT_density is None: return freqs = self.freqs_density ydata = np.zeros(self.NFFT_density) ydata1 = ydata+5 ydata2 = ydata+3.3 ydata = np.vstack([ydata1, ydata2]) ydata = np.tile(ydata, (20, 1)) ydatab = ydata.T.flatten() ydata = ydata.flatten() ycontrol = np.zeros_like(ydata) spec_g, fsp_g = mlab.psd(x=ydata, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend='mean') spec_b, fsp_b = mlab.psd(x=ydatab, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend='mean') spec_c, fsp_c = mlab.psd(x=ycontrol, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides) assert_array_equal(fsp_g, fsp_c) assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal assert_raises(AssertionError, assert_allclose, spec_b, spec_c, atol=1e-08) def test_psd_detrend_linear_func_trend(self): if self.NFFT_density is None: return freqs = self.freqs_density ydata = np.arange(self.NFFT_density) ydata1 = ydata+5 ydata2 = ydata+3.3 ydata = np.vstack([ydata1, ydata2]) ydata = np.tile(ydata, (20, 1)) ydatab = ydata.T.flatten() ydata = ydata.flatten() ycontrol = np.zeros_like(ydata) spec_g, fsp_g = mlab.psd(x=ydata, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend=mlab.detrend_linear) spec_b, fsp_b = mlab.psd(x=ydatab, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend=mlab.detrend_linear) spec_c, fsp_c = mlab.psd(x=ycontrol, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides) assert_array_equal(fsp_g, fsp_c) assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal assert_raises(AssertionError, assert_allclose, spec_b, spec_c, atol=1e-08) def test_psd_detrend_linear_str_trend(self): if self.NFFT_density is None: return freqs = self.freqs_density ydata = np.arange(self.NFFT_density) ydata1 = ydata+5 ydata2 = ydata+3.3 ydata = np.vstack([ydata1, ydata2]) ydata = np.tile(ydata, (20, 1)) ydatab = ydata.T.flatten() ydata = ydata.flatten() ycontrol = np.zeros_like(ydata) spec_g, fsp_g = mlab.psd(x=ydata, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend='linear') spec_b, fsp_b = mlab.psd(x=ydatab, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend='linear') spec_c, fsp_c = mlab.psd(x=ycontrol, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides) assert_array_equal(fsp_g, fsp_c) assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal assert_raises(AssertionError, assert_allclose, spec_b, spec_c, atol=1e-08) def test_psd_window_hanning(self): if self.NFFT_density is None: return freqs = self.freqs_density ydata = np.arange(self.NFFT_density) ydata1 = ydata+5 ydata2 = ydata+3.3 ycontrol1, windowVals = mlab.apply_window(ydata1, mlab.window_hanning, return_window=True) ycontrol2 = mlab.window_hanning(ydata2) ydata = np.vstack([ydata1, ydata2]) ycontrol = np.vstack([ycontrol1, ycontrol2]) ydata = np.tile(ydata, (20, 1)) ycontrol = np.tile(ycontrol, (20, 1)) ydatab = ydata.T.flatten() ydataf = ydata.flatten() ycontrol = ycontrol.flatten() spec_g, fsp_g = mlab.psd(x=ydataf, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, window=mlab.window_hanning) spec_b, fsp_b = mlab.psd(x=ydatab, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, window=mlab.window_hanning) spec_c, fsp_c = mlab.psd(x=ycontrol, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, window=mlab.window_none) spec_c *= len(ycontrol1)/(np.abs(windowVals)**2).sum() assert_array_equal(fsp_g, fsp_c) assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal assert_raises(AssertionError, assert_allclose, spec_b, spec_c, atol=1e-08) def test_psd_window_hanning_detrend_linear(self): if self.NFFT_density is None: return freqs = self.freqs_density ydata = np.arange(self.NFFT_density) ycontrol = np.zeros(self.NFFT_density) ydata1 = ydata+5 ydata2 = ydata+3.3 ycontrol1 = ycontrol ycontrol2 = ycontrol ycontrol1, windowVals = mlab.apply_window(ycontrol1, mlab.window_hanning, return_window=True) ycontrol2 = mlab.window_hanning(ycontrol2) ydata = np.vstack([ydata1, ydata2]) ycontrol = np.vstack([ycontrol1, ycontrol2]) ydata = np.tile(ydata, (20, 1)) ycontrol = np.tile(ycontrol, (20, 1)) ydatab = ydata.T.flatten() ydataf = ydata.flatten() ycontrol = ycontrol.flatten() spec_g, fsp_g = mlab.psd(x=ydataf, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend=mlab.detrend_linear, window=mlab.window_hanning) spec_b, fsp_b = mlab.psd(x=ydatab, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, detrend=mlab.detrend_linear, window=mlab.window_hanning) spec_c, fsp_c = mlab.psd(x=ycontrol, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=0, sides=self.sides, window=mlab.window_none) spec_c *= len(ycontrol1)/(np.abs(windowVals)**2).sum() assert_array_equal(fsp_g, fsp_c) assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal assert_raises(AssertionError, assert_allclose, spec_b, spec_c, atol=1e-08) def test_psd_windowarray(self): freqs = self.freqs_density spec, fsp = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides, window=np.ones(self.NFFT_density_real)) assert_allclose(fsp, freqs, atol=1e-06) assert_equal(spec.shape, freqs.shape) def test_psd_windowarray_scale_by_freq(self): freqs = self.freqs_density win = mlab.window_hanning(np.ones(self.NFFT_density_real)) spec, fsp = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides, window=mlab.window_hanning) spec_s, fsp_s = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides, window=mlab.window_hanning, scale_by_freq=True) spec_n, fsp_n = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides, window=mlab.window_hanning, scale_by_freq=False) assert_array_equal(fsp, fsp_s) assert_array_equal(fsp, fsp_n) assert_array_equal(spec, spec_s) assert_allclose(spec_s*(win**2).sum(), spec_n/self.Fs*win.sum()**2, atol=1e-08) def test_complex_spectrum(self): freqs = self.freqs_spectrum spec, fsp = mlab.complex_spectrum(x=self.y, Fs=self.Fs, sides=self.sides, pad_to=self.pad_to_spectrum) assert_allclose(fsp, freqs, atol=1e-06) assert_equal(spec.shape, freqs.shape) def test_magnitude_spectrum(self): freqs = self.freqs_spectrum spec, fsp = mlab.magnitude_spectrum(x=self.y, Fs=self.Fs, sides=self.sides, pad_to=self.pad_to_spectrum) assert_equal(spec.shape, freqs.shape) self.check_maxfreq(spec, fsp, self.fstims) self.check_freqs(spec, freqs, fsp, self.fstims) def test_angle_spectrum(self): freqs = self.freqs_spectrum spec, fsp = mlab.angle_spectrum(x=self.y, Fs=self.Fs, sides=self.sides, pad_to=self.pad_to_spectrum) assert_allclose(fsp, freqs, atol=1e-06) assert_equal(spec.shape, freqs.shape) def test_phase_spectrum(self): freqs = self.freqs_spectrum spec, fsp = mlab.phase_spectrum(x=self.y, Fs=self.Fs, sides=self.sides, pad_to=self.pad_to_spectrum) assert_allclose(fsp, freqs, atol=1e-06) assert_equal(spec.shape, freqs.shape) def test_specgram_auto(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides) specm = np.mean(spec, axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) # since we are using a single freq, all time slices # should be about the same if np.abs(spec.max()) != 0: assert_allclose(np.diff(spec, axis=1).max()/np.abs(spec.max()), 0, atol=1e-02) self.check_freqs(specm, freqs, fsp, self.fstims) def test_specgram_default(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='default') specm = np.mean(spec, axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) # since we are using a single freq, all time slices # should be about the same if np.abs(spec.max()) != 0: assert_allclose(np.diff(spec, axis=1).max()/np.abs(spec.max()), 0, atol=1e-02) self.check_freqs(specm, freqs, fsp, self.fstims) def test_specgram_psd(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='psd') specm = np.mean(spec, axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) # since we are using a single freq, all time slices # should be about the same if np.abs(spec.max()) != 0: assert_allclose(np.diff(spec, axis=1).max()/np.abs(spec.max()), 0, atol=1e-02) self.check_freqs(specm, freqs, fsp, self.fstims) def test_specgram_complex(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='complex') specm = np.mean(np.abs(spec), axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) self.check_freqs(specm, freqs, fsp, self.fstims) def test_specgram_magnitude(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='magnitude') specm = np.mean(spec, axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) # since we are using a single freq, all time slices # should be about the same if np.abs(spec.max()) != 0: assert_allclose(np.diff(spec, axis=1).max()/np.abs(spec.max()), 0, atol=1e-02) self.check_freqs(specm, freqs, fsp, self.fstims) def test_specgram_angle(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='angle') specm = np.mean(spec, axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) def test_specgram_phase(self): freqs = self.freqs_specgram spec, fsp, t = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='phase') specm = np.mean(spec, axis=1) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) assert_equal(spec.shape[0], freqs.shape[0]) assert_equal(spec.shape[1], self.t_specgram.shape[0]) def test_psd_csd_equal(self): freqs = self.freqs_density Pxx, freqsxx = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides) Pxy, freqsxy = mlab.csd(x=self.y, y=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides) assert_array_equal(Pxx, Pxy) assert_array_equal(freqsxx, freqsxy) def test_specgram_auto_default_equal(self): '''test that mlab.specgram without mode and with mode 'default' and 'psd' are all the same''' freqs = self.freqs_specgram speca, freqspeca, ta = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides) specb, freqspecb, tb = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='default') assert_array_equal(speca, specb) assert_array_equal(freqspeca, freqspecb) assert_array_equal(ta, tb) def test_specgram_auto_psd_equal(self): '''test that mlab.specgram without mode and with mode 'default' and 'psd' are all the same''' freqs = self.freqs_specgram speca, freqspeca, ta = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides) specc, freqspecc, tc = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='psd') assert_array_equal(speca, specc) assert_array_equal(freqspeca, freqspecc) assert_array_equal(ta, tc) def test_specgram_complex_mag_equivalent(self): freqs = self.freqs_specgram specc, freqspecc, tc = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='complex') specm, freqspecm, tm = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='magnitude') assert_array_equal(freqspecc, freqspecm) assert_array_equal(tc, tm) assert_allclose(np.abs(specc), specm, atol=1e-06) def test_specgram_complex_angle_equivalent(self): freqs = self.freqs_specgram specc, freqspecc, tc = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='complex') speca, freqspeca, ta = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='angle') assert_array_equal(freqspecc, freqspeca) assert_array_equal(tc, ta) assert_allclose(np.angle(specc), speca, atol=1e-06) def test_specgram_complex_phase_equivalent(self): freqs = self.freqs_specgram specc, freqspecc, tc = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='complex') specp, freqspecp, tp = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='phase') assert_array_equal(freqspecc, freqspecp) assert_array_equal(tc, tp) assert_allclose(np.unwrap(np.angle(specc), axis=0), specp, atol=1e-06) def test_specgram_angle_phase_equivalent(self): freqs = self.freqs_specgram speca, freqspeca, ta = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='angle') specp, freqspecp, tp = mlab.specgram(x=self.y, NFFT=self.NFFT_specgram, Fs=self.Fs, noverlap=self.nover_specgram, pad_to=self.pad_to_specgram, sides=self.sides, mode='phase') assert_array_equal(freqspeca, freqspecp) assert_array_equal(ta, tp) assert_allclose(np.unwrap(speca, axis=0), specp, atol=1e-06) def test_psd_windowarray_equal(self): freqs = self.freqs_density win = mlab.window_hanning(np.ones(self.NFFT_density_real)) speca, fspa = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides, window=win) specb, fspb = mlab.psd(x=self.y, NFFT=self.NFFT_density, Fs=self.Fs, noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides) assert_array_equal(fspa, fspb) assert_allclose(speca, specb, atol=1e-08) class spectral_testcase_nosig_real_twosided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], iscomplex=True, sides='default', nsides=2) class spectral_testcase_Fs4_real_onesided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4], iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_Fs4_real_twosided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4], iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_Fs4_real_defaultsided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4], iscomplex=False, sides='default', nsides=1) class spectral_testcase_Fs4_complex_onesided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4], iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_Fs4_complex_twosided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4], iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_Fs4_complex_defaultsided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4], iscomplex=True, sides='default', nsides=2) class spectral_testcase_FsAll_real_onesided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4, 5, 10], iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_FsAll_real_twosided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4, 5, 10], iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_FsAll_real_defaultsided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4, 5, 10], iscomplex=False, sides='default', nsides=1) class spectral_testcase_FsAll_complex_onesided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4, 5, 10], iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_FsAll_complex_twosided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4, 5, 10], iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_FsAll_complex_defaultsided( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[4, 5, 10], iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_noNFFT( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_spectrum=None, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_noNFFT( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_spectrum=None, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_noNFFT( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_spectrum=None, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_noNFFT( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_spectrum=None, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_noNFFT( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_spectrum=None, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_noNFFT( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_spectrum=None, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_nopad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], pad_to_density=None, pad_to_spectrum=None, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_nopad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], pad_to_density=None, pad_to_spectrum=None, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_nopad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], pad_to_density=None, pad_to_spectrum=None, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_nopad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], pad_to_density=None, pad_to_spectrum=None, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_nopad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_nopad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_noNFFT_no_pad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_noNFFT_no_pad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_noNFFT_no_pad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_noNFFT_no_pad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_noNFFT_no_pad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_noNFFT_no_pad_to( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], NFFT_density=None, pad_to_density=None, pad_to_spectrum=None, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_trim( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, NFFT_density=512, pad_to_spectrum=128, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_trim( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, NFFT_density=512, pad_to_spectrum=128, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_trim( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, NFFT_density=512, pad_to_spectrum=128, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_trim( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, NFFT_density=512, pad_to_spectrum=128, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_trim( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, NFFT_density=512, pad_to_spectrum=128, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_trim( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, NFFT_density=128, pad_to_spectrum=128, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_odd( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, pad_to_density=33, pad_to_spectrum=257, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_odd( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, pad_to_density=33, pad_to_spectrum=257, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_odd( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, pad_to_density=33, pad_to_spectrum=257, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_odd( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, pad_to_density=33, pad_to_spectrum=257, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_odd( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, pad_to_density=33, pad_to_spectrum=257, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_odd( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=256, pad_to_density=33, pad_to_spectrum=257, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_oddlen( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=255, NFFT_density=33, pad_to_spectrum=None, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_oddlen( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=255, NFFT_density=33, pad_to_spectrum=None, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_oddlen( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=255, NFFT_density=33, pad_to_spectrum=None, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_oddlen( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=255, NFFT_density=33, pad_to_spectrum=None, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_oddlen( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=255, NFFT_density=33, pad_to_spectrum=None, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_oddlen( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=255, NFFT_density=128, pad_to_spectrum=None, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_stretch( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=128, NFFT_density=128, pad_to_density=256, pad_to_spectrum=256, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_stretch( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=128, NFFT_density=128, pad_to_density=256, pad_to_spectrum=256, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_stretch( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=128, NFFT_density=128, pad_to_density=256, pad_to_spectrum=256, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_stretch( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=128, NFFT_density=128, pad_to_density=256, pad_to_spectrum=256, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_stretch( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=128, NFFT_density=128, pad_to_density=256, pad_to_spectrum=256, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_stretch( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], len_x=128, NFFT_density=128, pad_to_density=256, pad_to_spectrum=256, iscomplex=True, sides='default', nsides=2) class spectral_testcase_nosig_real_onesided_overlap( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], nover_density=32, iscomplex=False, sides='onesided', nsides=1) class spectral_testcase_nosig_real_twosided_overlap( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], nover_density=32, iscomplex=False, sides='twosided', nsides=2) class spectral_testcase_nosig_real_defaultsided_overlap( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], nover_density=32, iscomplex=False, sides='default', nsides=1) class spectral_testcase_nosig_complex_onesided_overlap( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], nover_density=32, iscomplex=True, sides='onesided', nsides=1) class spectral_testcase_nosig_complex_twosided_overlap( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], nover_density=32, iscomplex=True, sides='twosided', nsides=2) class spectral_testcase_nosig_complex_defaultsided_overlap( spectral_testcase_nosig_real_onesided): def setUp(self): self.createStim(fstims=[], nover_density=32, iscomplex=True, sides='default', nsides=2) def test_griddata_linear(): # z is a linear function of x and y. def get_z(x, y): return 3.0*x - y # Passing 1D xi and yi arrays to griddata. x = np.asarray([0.0, 1.0, 0.0, 1.0, 0.5]) y = np.asarray([0.0, 0.0, 1.0, 1.0, 0.5]) z = get_z(x, y) xi = [0.2, 0.4, 0.6, 0.8] yi = [0.1, 0.3, 0.7, 0.9] zi = mlab.griddata(x, y, z, xi, yi, interp='linear') xi, yi = np.meshgrid(xi, yi) np.testing.assert_array_almost_equal(zi, get_z(xi, yi)) # Passing 2D xi and yi arrays to griddata. zi = mlab.griddata(x, y, z, xi, yi, interp='linear') np.testing.assert_array_almost_equal(zi, get_z(xi, yi)) # Masking z array. z_masked = np.ma.array(z, mask=[False, False, False, True, False]) correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, get_z(xi, yi)) zi = mlab.griddata(x, y, z_masked, xi, yi, interp='linear') matest.assert_array_almost_equal(zi, correct_zi_masked) np.testing.assert_array_equal(np.ma.getmask(zi), np.ma.getmask(correct_zi_masked)) @knownfailureif(not HAS_NATGRID) def test_griddata_nn(): # z is a linear function of x and y. def get_z(x, y): return 3.0*x - y # Passing 1D xi and yi arrays to griddata. x = np.asarray([0.0, 1.0, 0.0, 1.0, 0.5]) y = np.asarray([0.0, 0.0, 1.0, 1.0, 0.5]) z = get_z(x, y) xi = [0.2, 0.4, 0.6, 0.8] yi = [0.1, 0.3, 0.7, 0.9] correct_zi = [[0.49999252, 1.0999978, 1.7000030, 2.3000080], [0.29999208, 0.8999978, 1.5000029, 2.1000059], [-0.1000099, 0.4999943, 1.0999964, 1.6999979], [-0.3000128, 0.2999894, 0.8999913, 1.4999933]] zi = mlab.griddata(x, y, z, xi, yi, interp='nn') np.testing.assert_array_almost_equal(zi, correct_zi, 5) # Decreasing xi or yi should raise ValueError. assert_raises(ValueError, mlab.griddata, x, y, z, xi[::-1], yi, interp='nn') assert_raises(ValueError, mlab.griddata, x, y, z, xi, yi[::-1], interp='nn') # Passing 2D xi and yi arrays to griddata. xi, yi = np.meshgrid(xi, yi) zi = mlab.griddata(x, y, z, xi, yi, interp='nn') np.testing.assert_array_almost_equal(zi, correct_zi, 5) # Masking z array. z_masked = np.ma.array(z, mask=[False, False, False, True, False]) correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, correct_zi) zi = mlab.griddata(x, y, z_masked, xi, yi, interp='nn') np.testing.assert_array_almost_equal(zi, correct_zi_masked, 5) np.testing.assert_array_equal(np.ma.getmask(zi), np.ma.getmask(correct_zi_masked)) #***************************************************************** # These Tests where taken from SCIPY with some minor modifications # this can be retreived from: # https://github.com/scipy/scipy/blob/master/scipy/stats/tests/test_kdeoth.py #***************************************************************** class gaussian_kde_tests(): def test_kde_integer_input(self): """Regression test for #1181.""" x1 = np.arange(5) kde = mlab.GaussianKDE(x1) y_expected = [0.13480721, 0.18222869, 0.19514935, 0.18222869, 0.13480721] np.testing.assert_array_almost_equal(kde(x1), y_expected, decimal=6) def test_gaussian_kde_covariance_caching(self): x1 = np.array([-7, -5, 1, 4, 5], dtype=np.float) xs = np.linspace(-10, 10, num=5) # These expected values are from scipy 0.10, before some changes to # gaussian_kde. They were not compared with any external reference. y_expected = [0.02463386, 0.04689208, 0.05395444, 0.05337754, 0.01664475] # set it to the default bandwidth. kde2 = mlab.GaussianKDE(x1, 'scott') y2 = kde2(xs) np.testing.assert_array_almost_equal(y_expected, y2, decimal=7) def test_kde_bandwidth_method(self): np.random.seed(8765678) n_basesample = 50 xn = np.random.randn(n_basesample) # Default gkde = mlab.GaussianKDE(xn) # Supply a callable gkde2 = mlab.GaussianKDE(xn, 'scott') # Supply a scalar gkde3 = mlab.GaussianKDE(xn, bw_method=gkde.factor) xs = np.linspace(-7, 7, 51) kdepdf = gkde.evaluate(xs) kdepdf2 = gkde2.evaluate(xs) assert_almost_equal(kdepdf.all(), kdepdf2.all()) kdepdf3 = gkde3.evaluate(xs) assert_almost_equal(kdepdf.all(), kdepdf3.all()) class gaussian_kde_custom_tests(object): def test_no_data(self): """Pass no data into the GaussianKDE class.""" assert_raises(ValueError, mlab.GaussianKDE, []) def test_single_dataset_element(self): """Pass a single dataset element into the GaussianKDE class.""" assert_raises(ValueError, mlab.GaussianKDE, [42]) def test_silverman_multidim_dataset(self): """Use a multi-dimensional array as the dataset and test silverman's output""" x1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert_raises(np.linalg.LinAlgError, mlab.GaussianKDE, x1, "silverman") def test_silverman_singledim_dataset(self): """Use a single dimension list as the dataset and test silverman's output.""" x1 = np.array([-7, -5, 1, 4, 5]) mygauss = mlab.GaussianKDE(x1, "silverman") y_expected = 0.76770389927475502 assert_almost_equal(mygauss.covariance_factor(), y_expected, 7) def test_scott_multidim_dataset(self): """Use a multi-dimensional array as the dataset and test scott's output """ x1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert_raises(np.linalg.LinAlgError, mlab.GaussianKDE, x1, "scott") def test_scott_singledim_dataset(self): """Use a single-dimensional array as the dataset and test scott's output""" x1 = np.array([-7, -5, 1, 4, 5]) mygauss = mlab.GaussianKDE(x1, "scott") y_expected = 0.72477966367769553 assert_almost_equal(mygauss.covariance_factor(), y_expected, 7) def test_scalar_empty_dataset(self): """Use an empty array as the dataset and test the scalar's cov factor """ assert_raises(ValueError, mlab.GaussianKDE, [], bw_method=5) def test_scalar_covariance_dataset(self): """Use a dataset and test a scalar's cov factor """ np.random.seed(8765678) n_basesample = 50 multidim_data = [np.random.randn(n_basesample) for i in range(5)] kde = mlab.GaussianKDE(multidim_data, bw_method=0.5) assert_equal(kde.covariance_factor(), 0.5) def test_callable_covariance_dataset(self): """Use a multi-dimensional array as the dataset and test the callable's cov factor""" np.random.seed(8765678) n_basesample = 50 multidim_data = [np.random.randn(n_basesample) for i in range(5)] def callable_fun(x): return 0.55 kde = mlab.GaussianKDE(multidim_data, bw_method=callable_fun) assert_equal(kde.covariance_factor(), 0.55) def test_callable_singledim_dataset(self): """Use a single-dimensional array as the dataset and test the callable's cov factor""" np.random.seed(8765678) n_basesample = 50 multidim_data = np.random.randn(n_basesample) kde = mlab.GaussianKDE(multidim_data, bw_method='silverman') y_expected = 0.48438841363348911 assert_almost_equal(kde.covariance_factor(), y_expected, 7) def test_wrong_bw_method(self): """Test the error message that should be called when bw is invalid.""" np.random.seed(8765678) n_basesample = 50 data = np.random.randn(n_basesample) assert_raises(ValueError, mlab.GaussianKDE, data, bw_method="invalid") class gaussian_kde_evaluate_tests(object): def test_evaluate_diff_dim(self): """Test the evaluate method when the dim's of dataset and points are different dimensions""" x1 = np.arange(3, 10, 2) kde = mlab.GaussianKDE(x1) x2 = np.arange(3, 12, 2) y_expected = [ 0.08797252, 0.11774109, 0.11774109, 0.08797252, 0.0370153 ] y = kde.evaluate(x2) np.testing.assert_array_almost_equal(y, y_expected, 7) def test_evaluate_inv_dim(self): """ Invert the dimensions. i.e., Give the dataset a dimension of 1 [3,2,4], and the points will have a dimension of 3 [[3],[2],[4]]. ValueError should be raised""" np.random.seed(8765678) n_basesample = 50 multidim_data = np.random.randn(n_basesample) kde = mlab.GaussianKDE(multidim_data) x2 = [[1], [2], [3]] assert_raises(ValueError, kde.evaluate, x2) def test_evaluate_dim_and_num(self): """ Tests if evaluated against a one by one array""" x1 = np.arange(3, 10, 2) x2 = np.array([3]) kde = mlab.GaussianKDE(x1) y_expected = [0.08797252] y = kde.evaluate(x2) np.testing.assert_array_almost_equal(y, y_expected, 7) def test_evaluate_point_dim_not_one(self): """Test""" x1 = np.arange(3, 10, 2) x2 = [np.arange(3, 10, 2), np.arange(3, 10, 2)] kde = mlab.GaussianKDE(x1) assert_raises(ValueError, kde.evaluate, x2) def test_evaluate_equal_dim_and_num_lt(self): """Test when line 3810 fails""" x1 = np.arange(3, 10, 2) x2 = np.arange(3, 8, 2) kde = mlab.GaussianKDE(x1) y_expected = [0.08797252, 0.11774109, 0.11774109] y = kde.evaluate(x2) np.testing.assert_array_almost_equal(y, y_expected, 7) def test_contiguous_regions(): a, b, c = 3, 4, 5 # Starts and ends with True mask = [True]*a + [False]*b + [True]*c expected = [(0, a), (a+b, a+b+c)] assert_equal(mlab.contiguous_regions(mask), expected) d, e = 6, 7 # Starts with True ends with False mask = mask + [False]*e assert_equal(mlab.contiguous_regions(mask), expected) # Starts with False ends with True mask = [False]*d + mask[:-e] expected = [(d, d+a), (d+a+b, d+a+b+c)] assert_equal(mlab.contiguous_regions(mask), expected) # Starts and ends with False mask = mask + [False]*e assert_equal(mlab.contiguous_regions(mask), expected) # No True in mask assert_equal(mlab.contiguous_regions([False]*5), []) # Empty mask assert_equal(mlab.contiguous_regions([]), []) def test_psd_onesided_norm(): u = np.array([0, 1, 2, 3, 1, 2, 1]) dt = 1.0 Su = np.abs(np.fft.fft(u) * dt)**2 / float(dt * u.size) P, f = mlab.psd(u, NFFT=u.size, Fs=1/dt, window=mlab.window_none, detrend=mlab.detrend_none, noverlap=0, pad_to=None, scale_by_freq=None, sides='onesided') Su_1side = np.append([Su[0]], Su[1:4] + Su[4:][::-1]) assert_allclose(P, Su_1side, atol=1e-06) if __name__ == '__main__': import nose import sys args = ['-s', '--with-doctest'] argv = sys.argv argv = argv[:1] + args + argv[1:] nose.runmodule(argv=argv, exit=False)
gpl-3.0
dimarkov/seaborn
seaborn/tests/test_axisgrid.py
11
42805
import warnings import numpy as np import pandas as pd from scipy import stats import matplotlib as mpl import matplotlib.pyplot as plt from distutils.version import LooseVersion import nose.tools as nt import numpy.testing as npt from numpy.testing.decorators import skipif import pandas.util.testing as tm from .. import axisgrid as ag from .. import rcmod from ..palettes import color_palette from ..distributions import kdeplot from ..categorical import pointplot from ..linearmodels import pairplot from ..utils import categorical_order rs = np.random.RandomState(0) old_matplotlib = LooseVersion(mpl.__version__) < "1.4" class TestFacetGrid(object): df = pd.DataFrame(dict(x=rs.normal(size=60), y=rs.gamma(4, size=60), a=np.repeat(list("abc"), 20), b=np.tile(list("mn"), 30), c=np.tile(list("tuv"), 20), d=np.tile(list("abcdefghij"), 6))) def test_self_data(self): g = ag.FacetGrid(self.df) nt.assert_is(g.data, self.df) plt.close("all") def test_self_fig(self): g = ag.FacetGrid(self.df) nt.assert_is_instance(g.fig, plt.Figure) plt.close("all") def test_self_axes(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") for ax in g.axes.flat: nt.assert_is_instance(ax, plt.Axes) plt.close("all") def test_axes_array_size(self): g1 = ag.FacetGrid(self.df) nt.assert_equal(g1.axes.shape, (1, 1)) g2 = ag.FacetGrid(self.df, row="a") nt.assert_equal(g2.axes.shape, (3, 1)) g3 = ag.FacetGrid(self.df, col="b") nt.assert_equal(g3.axes.shape, (1, 2)) g4 = ag.FacetGrid(self.df, hue="c") nt.assert_equal(g4.axes.shape, (1, 1)) g5 = ag.FacetGrid(self.df, row="a", col="b", hue="c") nt.assert_equal(g5.axes.shape, (3, 2)) for ax in g5.axes.flat: nt.assert_is_instance(ax, plt.Axes) plt.close("all") def test_single_axes(self): g1 = ag.FacetGrid(self.df) nt.assert_is_instance(g1.ax, plt.Axes) g2 = ag.FacetGrid(self.df, row="a") with nt.assert_raises(AttributeError): g2.ax g3 = ag.FacetGrid(self.df, col="a") with nt.assert_raises(AttributeError): g3.ax g4 = ag.FacetGrid(self.df, col="a", row="b") with nt.assert_raises(AttributeError): g4.ax def test_col_wrap(self): g = ag.FacetGrid(self.df, col="d") nt.assert_equal(g.axes.shape, (1, 10)) nt.assert_is(g.facet_axis(0, 8), g.axes[0, 8]) g_wrap = ag.FacetGrid(self.df, col="d", col_wrap=4) nt.assert_equal(g_wrap.axes.shape, (10,)) nt.assert_is(g_wrap.facet_axis(0, 8), g_wrap.axes[8]) nt.assert_equal(g_wrap._ncol, 4) nt.assert_equal(g_wrap._nrow, 3) with nt.assert_raises(ValueError): g = ag.FacetGrid(self.df, row="b", col="d", col_wrap=4) df = self.df.copy() df.loc[df.d == "j"] = np.nan g_missing = ag.FacetGrid(df, col="d") nt.assert_equal(g_missing.axes.shape, (1, 9)) g_missing_wrap = ag.FacetGrid(df, col="d", col_wrap=4) nt.assert_equal(g_missing_wrap.axes.shape, (9,)) plt.close("all") def test_normal_axes(self): null = np.empty(0, object).flat g = ag.FacetGrid(self.df) npt.assert_array_equal(g._bottom_axes, g.axes.flat) npt.assert_array_equal(g._not_bottom_axes, null) npt.assert_array_equal(g._left_axes, g.axes.flat) npt.assert_array_equal(g._not_left_axes, null) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, col="c") npt.assert_array_equal(g._bottom_axes, g.axes.flat) npt.assert_array_equal(g._not_bottom_axes, null) npt.assert_array_equal(g._left_axes, g.axes[:, 0].flat) npt.assert_array_equal(g._not_left_axes, g.axes[:, 1:].flat) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, row="c") npt.assert_array_equal(g._bottom_axes, g.axes[-1, :].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:-1, :].flat) npt.assert_array_equal(g._left_axes, g.axes.flat) npt.assert_array_equal(g._not_left_axes, null) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, col="a", row="c") npt.assert_array_equal(g._bottom_axes, g.axes[-1, :].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:-1, :].flat) npt.assert_array_equal(g._left_axes, g.axes[:, 0].flat) npt.assert_array_equal(g._not_left_axes, g.axes[:, 1:].flat) npt.assert_array_equal(g._inner_axes, g.axes[:-1, 1:].flat) plt.close("all") def test_wrapped_axes(self): null = np.empty(0, object).flat g = ag.FacetGrid(self.df, col="a", col_wrap=2) npt.assert_array_equal(g._bottom_axes, g.axes[np.array([1, 2])].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:1].flat) npt.assert_array_equal(g._left_axes, g.axes[np.array([0, 2])].flat) npt.assert_array_equal(g._not_left_axes, g.axes[np.array([1])].flat) npt.assert_array_equal(g._inner_axes, null) plt.close("all") def test_figure_size(self): g = ag.FacetGrid(self.df, row="a", col="b") npt.assert_array_equal(g.fig.get_size_inches(), (6, 9)) g = ag.FacetGrid(self.df, row="a", col="b", size=6) npt.assert_array_equal(g.fig.get_size_inches(), (12, 18)) g = ag.FacetGrid(self.df, col="c", size=4, aspect=.5) npt.assert_array_equal(g.fig.get_size_inches(), (6, 4)) plt.close("all") def test_figure_size_with_legend(self): g1 = ag.FacetGrid(self.df, col="a", hue="c", size=4, aspect=.5) npt.assert_array_equal(g1.fig.get_size_inches(), (6, 4)) g1.add_legend() nt.assert_greater(g1.fig.get_size_inches()[0], 6) g2 = ag.FacetGrid(self.df, col="a", hue="c", size=4, aspect=.5, legend_out=False) npt.assert_array_equal(g2.fig.get_size_inches(), (6, 4)) g2.add_legend() npt.assert_array_equal(g2.fig.get_size_inches(), (6, 4)) plt.close("all") def test_legend_data(self): g1 = ag.FacetGrid(self.df, hue="a") g1.map(plt.plot, "x", "y") g1.add_legend() palette = color_palette(n_colors=3) nt.assert_equal(g1._legend.get_title().get_text(), "a") a_levels = sorted(self.df.a.unique()) lines = g1._legend.get_lines() nt.assert_equal(len(lines), len(a_levels)) for line, hue in zip(lines, palette): nt.assert_equal(line.get_color(), hue) labels = g1._legend.get_texts() nt.assert_equal(len(labels), len(a_levels)) for label, level in zip(labels, a_levels): nt.assert_equal(label.get_text(), level) plt.close("all") def test_legend_data_missing_level(self): g1 = ag.FacetGrid(self.df, hue="a", hue_order=list("azbc")) g1.map(plt.plot, "x", "y") g1.add_legend() b, g, r, p = color_palette(n_colors=4) palette = [b, r, p] nt.assert_equal(g1._legend.get_title().get_text(), "a") a_levels = sorted(self.df.a.unique()) lines = g1._legend.get_lines() nt.assert_equal(len(lines), len(a_levels)) for line, hue in zip(lines, palette): nt.assert_equal(line.get_color(), hue) labels = g1._legend.get_texts() nt.assert_equal(len(labels), 4) for label, level in zip(labels, list("azbc")): nt.assert_equal(label.get_text(), level) plt.close("all") def test_get_boolean_legend_data(self): self.df["b_bool"] = self.df.b == "m" g1 = ag.FacetGrid(self.df, hue="b_bool") g1.map(plt.plot, "x", "y") g1.add_legend() palette = color_palette(n_colors=2) nt.assert_equal(g1._legend.get_title().get_text(), "b_bool") b_levels = list(map(str, categorical_order(self.df.b_bool))) lines = g1._legend.get_lines() nt.assert_equal(len(lines), len(b_levels)) for line, hue in zip(lines, palette): nt.assert_equal(line.get_color(), hue) labels = g1._legend.get_texts() nt.assert_equal(len(labels), len(b_levels)) for label, level in zip(labels, b_levels): nt.assert_equal(label.get_text(), level) plt.close("all") def test_legend_options(self): g1 = ag.FacetGrid(self.df, hue="b") g1.map(plt.plot, "x", "y") g1.add_legend() def test_legendout_with_colwrap(self): g = ag.FacetGrid(self.df, col="d", hue='b', col_wrap=4, legend_out=False) g.map(plt.plot, "x", "y", linewidth=3) g.add_legend() def test_subplot_kws(self): g = ag.FacetGrid(self.df, subplot_kws=dict(axisbg="blue")) for ax in g.axes.flat: nt.assert_equal(ax.get_axis_bgcolor(), "blue") @skipif(old_matplotlib) def test_gridspec_kws(self): ratios = [3, 1, 2] sizes = [0.46, 0.15, 0.31] gskws = dict(width_ratios=ratios, height_ratios=ratios) g = ag.FacetGrid(self.df, col='c', row='a', gridspec_kws=gskws) # clear out all ticks for ax in g.axes.flat: ax.set_xticks([]) ax.set_yticks([]) g.fig.tight_layout() widths, heights = np.meshgrid(sizes, sizes) for n, ax in enumerate(g.axes.flat): npt.assert_almost_equal( ax.get_position().width, widths.flatten()[n], decimal=2 ) npt.assert_almost_equal( ax.get_position().height, heights.flatten()[n], decimal=2 ) @skipif(old_matplotlib) def test_gridspec_kws_col_wrap(self): ratios = [3, 1, 2, 1, 1] sizes = [0.46, 0.15, 0.31] gskws = dict(width_ratios=ratios) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("always") npt.assert_warns(UserWarning, ag.FacetGrid, self.df, col='d', col_wrap=5, gridspec_kws=gskws) @skipif(not old_matplotlib) def test_gridsic_kws_old_mpl(self): ratios = [3, 1, 2] sizes = [0.46, 0.15, 0.31] gskws = dict(width_ratios=ratios, height_ratios=ratios) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("always") npt.assert_warns(UserWarning, ag.FacetGrid, self.df, col='c', row='a', gridspec_kws=gskws) def test_data_generator(self): g = ag.FacetGrid(self.df, row="a") d = list(g.facet_data()) nt.assert_equal(len(d), 3) tup, data = d[0] nt.assert_equal(tup, (0, 0, 0)) nt.assert_true((data["a"] == "a").all()) tup, data = d[1] nt.assert_equal(tup, (1, 0, 0)) nt.assert_true((data["a"] == "b").all()) g = ag.FacetGrid(self.df, row="a", col="b") d = list(g.facet_data()) nt.assert_equal(len(d), 6) tup, data = d[0] nt.assert_equal(tup, (0, 0, 0)) nt.assert_true((data["a"] == "a").all()) nt.assert_true((data["b"] == "m").all()) tup, data = d[1] nt.assert_equal(tup, (0, 1, 0)) nt.assert_true((data["a"] == "a").all()) nt.assert_true((data["b"] == "n").all()) tup, data = d[2] nt.assert_equal(tup, (1, 0, 0)) nt.assert_true((data["a"] == "b").all()) nt.assert_true((data["b"] == "m").all()) g = ag.FacetGrid(self.df, hue="c") d = list(g.facet_data()) nt.assert_equal(len(d), 3) tup, data = d[1] nt.assert_equal(tup, (0, 0, 1)) nt.assert_true((data["c"] == "u").all()) plt.close("all") def test_map(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") g.map(plt.plot, "x", "y", linewidth=3) lines = g.axes[0, 0].lines nt.assert_equal(len(lines), 3) line1, _, _ = lines nt.assert_equal(line1.get_linewidth(), 3) x, y = line1.get_data() mask = (self.df.a == "a") & (self.df.b == "m") & (self.df.c == "t") npt.assert_array_equal(x, self.df.x[mask]) npt.assert_array_equal(y, self.df.y[mask]) def test_map_dataframe(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") plot = lambda x, y, data=None, **kws: plt.plot(data[x], data[y], **kws) g.map_dataframe(plot, "x", "y", linestyle="--") lines = g.axes[0, 0].lines nt.assert_equal(len(lines), 3) line1, _, _ = lines nt.assert_equal(line1.get_linestyle(), "--") x, y = line1.get_data() mask = (self.df.a == "a") & (self.df.b == "m") & (self.df.c == "t") npt.assert_array_equal(x, self.df.x[mask]) npt.assert_array_equal(y, self.df.y[mask]) def test_set(self): g = ag.FacetGrid(self.df, row="a", col="b") xlim = (-2, 5) ylim = (3, 6) xticks = [-2, 0, 3, 5] yticks = [3, 4.5, 6] g.set(xlim=xlim, ylim=ylim, xticks=xticks, yticks=yticks) for ax in g.axes.flat: npt.assert_array_equal(ax.get_xlim(), xlim) npt.assert_array_equal(ax.get_ylim(), ylim) npt.assert_array_equal(ax.get_xticks(), xticks) npt.assert_array_equal(ax.get_yticks(), yticks) plt.close("all") def test_set_titles(self): g = ag.FacetGrid(self.df, row="a", col="b") g.map(plt.plot, "x", "y") # Test the default titles nt.assert_equal(g.axes[0, 0].get_title(), "a = a | b = m") nt.assert_equal(g.axes[0, 1].get_title(), "a = a | b = n") nt.assert_equal(g.axes[1, 0].get_title(), "a = b | b = m") # Test a provided title g.set_titles("{row_var} == {row_name} \/ {col_var} == {col_name}") nt.assert_equal(g.axes[0, 0].get_title(), "a == a \/ b == m") nt.assert_equal(g.axes[0, 1].get_title(), "a == a \/ b == n") nt.assert_equal(g.axes[1, 0].get_title(), "a == b \/ b == m") # Test a single row g = ag.FacetGrid(self.df, col="b") g.map(plt.plot, "x", "y") # Test the default titles nt.assert_equal(g.axes[0, 0].get_title(), "b = m") nt.assert_equal(g.axes[0, 1].get_title(), "b = n") # test with dropna=False g = ag.FacetGrid(self.df, col="b", hue="b", dropna=False) g.map(plt.plot, 'x', 'y') plt.close("all") def test_set_titles_margin_titles(self): g = ag.FacetGrid(self.df, row="a", col="b", margin_titles=True) g.map(plt.plot, "x", "y") # Test the default titles nt.assert_equal(g.axes[0, 0].get_title(), "b = m") nt.assert_equal(g.axes[0, 1].get_title(), "b = n") nt.assert_equal(g.axes[1, 0].get_title(), "") # Test the row "titles" nt.assert_equal(g.axes[0, 1].texts[0].get_text(), "a = a") nt.assert_equal(g.axes[1, 1].texts[0].get_text(), "a = b") # Test a provided title g.set_titles(col_template="{col_var} == {col_name}") nt.assert_equal(g.axes[0, 0].get_title(), "b == m") nt.assert_equal(g.axes[0, 1].get_title(), "b == n") nt.assert_equal(g.axes[1, 0].get_title(), "") plt.close("all") def test_set_ticklabels(self): g = ag.FacetGrid(self.df, row="a", col="b") g.map(plt.plot, "x", "y") xlab = [l.get_text() + "h" for l in g.axes[1, 0].get_xticklabels()] ylab = [l.get_text() for l in g.axes[1, 0].get_yticklabels()] g.set_xticklabels(xlab) g.set_yticklabels(rotation=90) got_x = [l.get_text() + "h" for l in g.axes[1, 1].get_xticklabels()] got_y = [l.get_text() for l in g.axes[0, 0].get_yticklabels()] npt.assert_array_equal(got_x, xlab) npt.assert_array_equal(got_y, ylab) x, y = np.arange(10), np.arange(10) df = pd.DataFrame(np.c_[x, y], columns=["x", "y"]) g = ag.FacetGrid(df).map(pointplot, "x", "y") g.set_xticklabels(step=2) got_x = [int(l.get_text()) for l in g.axes[0, 0].get_xticklabels()] npt.assert_array_equal(x[::2], got_x) g = ag.FacetGrid(self.df, col="d", col_wrap=5) g.map(plt.plot, "x", "y") g.set_xticklabels(rotation=45) g.set_yticklabels(rotation=75) for ax in g._bottom_axes: for l in ax.get_xticklabels(): nt.assert_equal(l.get_rotation(), 45) for ax in g._left_axes: for l in ax.get_yticklabels(): nt.assert_equal(l.get_rotation(), 75) plt.close("all") def test_set_axis_labels(self): g = ag.FacetGrid(self.df, row="a", col="b") g.map(plt.plot, "x", "y") xlab = 'xx' ylab = 'yy' g.set_axis_labels(xlab, ylab) got_x = [ax.get_xlabel() for ax in g.axes[-1, :]] got_y = [ax.get_ylabel() for ax in g.axes[:, 0]] npt.assert_array_equal(got_x, xlab) npt.assert_array_equal(got_y, ylab) plt.close("all") def test_axis_lims(self): g = ag.FacetGrid(self.df, row="a", col="b", xlim=(0, 4), ylim=(-2, 3)) nt.assert_equal(g.axes[0, 0].get_xlim(), (0, 4)) nt.assert_equal(g.axes[0, 0].get_ylim(), (-2, 3)) plt.close("all") def test_data_orders(self): g = ag.FacetGrid(self.df, row="a", col="b", hue="c") nt.assert_equal(g.row_names, list("abc")) nt.assert_equal(g.col_names, list("mn")) nt.assert_equal(g.hue_names, list("tuv")) nt.assert_equal(g.axes.shape, (3, 2)) g = ag.FacetGrid(self.df, row="a", col="b", hue="c", row_order=list("bca"), col_order=list("nm"), hue_order=list("vtu")) nt.assert_equal(g.row_names, list("bca")) nt.assert_equal(g.col_names, list("nm")) nt.assert_equal(g.hue_names, list("vtu")) nt.assert_equal(g.axes.shape, (3, 2)) g = ag.FacetGrid(self.df, row="a", col="b", hue="c", row_order=list("bcda"), col_order=list("nom"), hue_order=list("qvtu")) nt.assert_equal(g.row_names, list("bcda")) nt.assert_equal(g.col_names, list("nom")) nt.assert_equal(g.hue_names, list("qvtu")) nt.assert_equal(g.axes.shape, (4, 3)) plt.close("all") def test_palette(self): rcmod.set() g = ag.FacetGrid(self.df, hue="c") nt.assert_equal(g._colors, color_palette(n_colors=3)) g = ag.FacetGrid(self.df, hue="d") nt.assert_equal(g._colors, color_palette("husl", 10)) g = ag.FacetGrid(self.df, hue="c", palette="Set2") nt.assert_equal(g._colors, color_palette("Set2", 3)) dict_pal = dict(t="red", u="green", v="blue") list_pal = color_palette(["red", "green", "blue"], 3) g = ag.FacetGrid(self.df, hue="c", palette=dict_pal) nt.assert_equal(g._colors, list_pal) list_pal = color_palette(["green", "blue", "red"], 3) g = ag.FacetGrid(self.df, hue="c", hue_order=list("uvt"), palette=dict_pal) nt.assert_equal(g._colors, list_pal) plt.close("all") def test_hue_kws(self): kws = dict(marker=["o", "s", "D"]) g = ag.FacetGrid(self.df, hue="c", hue_kws=kws) g.map(plt.plot, "x", "y") for line, marker in zip(g.axes[0, 0].lines, kws["marker"]): nt.assert_equal(line.get_marker(), marker) def test_dropna(self): df = self.df.copy() hasna = pd.Series(np.tile(np.arange(6), 10), dtype=np.float) hasna[hasna == 5] = np.nan df["hasna"] = hasna g = ag.FacetGrid(df, dropna=False, row="hasna") nt.assert_equal(g._not_na.sum(), 60) g = ag.FacetGrid(df, dropna=True, row="hasna") nt.assert_equal(g._not_na.sum(), 50) plt.close("all") @classmethod def teardown_class(cls): """Ensure that all figures are closed on exit.""" plt.close("all") class TestPairGrid(object): rs = np.random.RandomState(sum(map(ord, "PairGrid"))) df = pd.DataFrame(dict(x=rs.normal(size=80), y=rs.randint(0, 4, size=(80)), z=rs.gamma(3, size=80), a=np.repeat(list("abcd"), 20), b=np.repeat(list("abcdefgh"), 10))) def test_self_data(self): g = ag.PairGrid(self.df) nt.assert_is(g.data, self.df) plt.close("all") def test_ignore_datelike_data(self): df = self.df.copy() df['date'] = pd.date_range('2010-01-01', periods=len(df), freq='d') result = ag.PairGrid(self.df).data expected = df.drop('date', axis=1) tm.assert_frame_equal(result, expected) plt.close("all") def test_self_fig(self): g = ag.PairGrid(self.df) nt.assert_is_instance(g.fig, plt.Figure) plt.close("all") def test_self_axes(self): g = ag.PairGrid(self.df) for ax in g.axes.flat: nt.assert_is_instance(ax, plt.Axes) plt.close("all") def test_default_axes(self): g = ag.PairGrid(self.df) nt.assert_equal(g.axes.shape, (3, 3)) nt.assert_equal(g.x_vars, ["x", "y", "z"]) nt.assert_equal(g.y_vars, ["x", "y", "z"]) nt.assert_true(g.square_grid) plt.close("all") def test_specific_square_axes(self): vars = ["z", "x"] g = ag.PairGrid(self.df, vars=vars) nt.assert_equal(g.axes.shape, (len(vars), len(vars))) nt.assert_equal(g.x_vars, vars) nt.assert_equal(g.y_vars, vars) nt.assert_true(g.square_grid) plt.close("all") def test_specific_nonsquare_axes(self): x_vars = ["x", "y"] y_vars = ["z", "y", "x"] g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) nt.assert_equal(g.axes.shape, (len(y_vars), len(x_vars))) nt.assert_equal(g.x_vars, x_vars) nt.assert_equal(g.y_vars, y_vars) nt.assert_true(not g.square_grid) x_vars = ["x", "y"] y_vars = "z" g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) nt.assert_equal(g.axes.shape, (len(y_vars), len(x_vars))) nt.assert_equal(g.x_vars, list(x_vars)) nt.assert_equal(g.y_vars, list(y_vars)) nt.assert_true(not g.square_grid) plt.close("all") def test_specific_square_axes_with_array(self): vars = np.array(["z", "x"]) g = ag.PairGrid(self.df, vars=vars) nt.assert_equal(g.axes.shape, (len(vars), len(vars))) nt.assert_equal(g.x_vars, list(vars)) nt.assert_equal(g.y_vars, list(vars)) nt.assert_true(g.square_grid) plt.close("all") def test_specific_nonsquare_axes_with_array(self): x_vars = np.array(["x", "y"]) y_vars = np.array(["z", "y", "x"]) g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) nt.assert_equal(g.axes.shape, (len(y_vars), len(x_vars))) nt.assert_equal(g.x_vars, list(x_vars)) nt.assert_equal(g.y_vars, list(y_vars)) nt.assert_true(not g.square_grid) plt.close("all") def test_size(self): g1 = ag.PairGrid(self.df, size=3) npt.assert_array_equal(g1.fig.get_size_inches(), (9, 9)) g2 = ag.PairGrid(self.df, size=4, aspect=.5) npt.assert_array_equal(g2.fig.get_size_inches(), (6, 12)) g3 = ag.PairGrid(self.df, y_vars=["z"], x_vars=["x", "y"], size=2, aspect=2) npt.assert_array_equal(g3.fig.get_size_inches(), (8, 2)) plt.close("all") def test_map(self): vars = ["x", "y", "z"] g1 = ag.PairGrid(self.df) g1.map(plt.scatter) for i, axes_i in enumerate(g1.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) g2 = ag.PairGrid(self.df, "a") g2.map(plt.scatter) for i, axes_i in enumerate(g2.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] for k, k_level in enumerate("abcd"): x_in_k = x_in[self.df.a == k_level] y_in_k = y_in[self.df.a == k_level] x_out, y_out = ax.collections[k].get_offsets().T npt.assert_array_equal(x_in_k, x_out) npt.assert_array_equal(y_in_k, y_out) plt.close("all") def test_map_nonsquare(self): x_vars = ["x"] y_vars = ["y", "z"] g = ag.PairGrid(self.df, x_vars=x_vars, y_vars=y_vars) g.map(plt.scatter) x_in = self.df.x for i, i_var in enumerate(y_vars): ax = g.axes[i, 0] y_in = self.df[i_var] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) plt.close("all") def test_map_lower(self): vars = ["x", "y", "z"] g = ag.PairGrid(self.df) g.map_lower(plt.scatter) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.triu_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) plt.close("all") def test_map_upper(self): vars = ["x", "y", "z"] g = ag.PairGrid(self.df) g.map_upper(plt.scatter) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) plt.close("all") @skipif(old_matplotlib) def test_map_diag(self): g1 = ag.PairGrid(self.df) g1.map_diag(plt.hist) for ax in g1.diag_axes: nt.assert_equal(len(ax.patches), 10) g2 = ag.PairGrid(self.df) g2.map_diag(plt.hist, bins=15) for ax in g2.diag_axes: nt.assert_equal(len(ax.patches), 15) g3 = ag.PairGrid(self.df, hue="a") g3.map_diag(plt.hist) for ax in g3.diag_axes: nt.assert_equal(len(ax.patches), 40) plt.close("all") @skipif(old_matplotlib) def test_map_diag_and_offdiag(self): vars = ["x", "y", "z"] g = ag.PairGrid(self.df) g.map_offdiag(plt.scatter) g.map_diag(plt.hist) for ax in g.diag_axes: nt.assert_equal(len(ax.patches), 10) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) plt.close("all") def test_palette(self): rcmod.set() g = ag.PairGrid(self.df, hue="a") nt.assert_equal(g.palette, color_palette(n_colors=4)) g = ag.PairGrid(self.df, hue="b") nt.assert_equal(g.palette, color_palette("husl", 8)) g = ag.PairGrid(self.df, hue="a", palette="Set2") nt.assert_equal(g.palette, color_palette("Set2", 4)) dict_pal = dict(a="red", b="green", c="blue", d="purple") list_pal = color_palette(["red", "green", "blue", "purple"], 4) g = ag.PairGrid(self.df, hue="a", palette=dict_pal) nt.assert_equal(g.palette, list_pal) list_pal = color_palette(["purple", "blue", "red", "green"], 4) g = ag.PairGrid(self.df, hue="a", hue_order=list("dcab"), palette=dict_pal) nt.assert_equal(g.palette, list_pal) plt.close("all") def test_hue_kws(self): kws = dict(marker=["o", "s", "d", "+"]) g = ag.PairGrid(self.df, hue="a", hue_kws=kws) g.map(plt.plot) for line, marker in zip(g.axes[0, 0].lines, kws["marker"]): nt.assert_equal(line.get_marker(), marker) g = ag.PairGrid(self.df, hue="a", hue_kws=kws, hue_order=list("dcab")) g.map(plt.plot) for line, marker in zip(g.axes[0, 0].lines, kws["marker"]): nt.assert_equal(line.get_marker(), marker) plt.close("all") @skipif(old_matplotlib) def test_hue_order(self): order = list("dcab") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_diag(plt.plot) for line, level in zip(g.axes[0, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_lower(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_upper(plt.plot) for line, level in zip(g.axes[0, 1].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "y"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") @skipif(old_matplotlib) def test_hue_order_missing_level(self): order = list("dcaeb") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_diag(plt.plot) for line, level in zip(g.axes[0, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_lower(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_upper(plt.plot) for line, level in zip(g.axes[0, 1].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "y"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") def test_nondefault_index(self): df = self.df.copy().set_index("b") vars = ["x", "y", "z"] g1 = ag.PairGrid(df) g1.map(plt.scatter) for i, axes_i in enumerate(g1.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) g2 = ag.PairGrid(df, "a") g2.map(plt.scatter) for i, axes_i in enumerate(g2.axes): for j, ax in enumerate(axes_i): x_in = self.df[vars[j]] y_in = self.df[vars[i]] for k, k_level in enumerate("abcd"): x_in_k = x_in[self.df.a == k_level] y_in_k = y_in[self.df.a == k_level] x_out, y_out = ax.collections[k].get_offsets().T npt.assert_array_equal(x_in_k, x_out) npt.assert_array_equal(y_in_k, y_out) plt.close("all") @skipif(old_matplotlib) def test_pairplot(self): vars = ["x", "y", "z"] g = pairplot(self.df) for ax in g.diag_axes: nt.assert_equal(len(ax.patches), 10) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) plt.close("all") @skipif(old_matplotlib) def test_pairplot_reg(self): vars = ["x", "y", "z"] g = pairplot(self.df, kind="reg") for ax in g.diag_axes: nt.assert_equal(len(ax.patches), 10) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) nt.assert_equal(len(ax.lines), 1) nt.assert_equal(len(ax.collections), 2) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) nt.assert_equal(len(ax.lines), 1) nt.assert_equal(len(ax.collections), 2) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) plt.close("all") @skipif(old_matplotlib) def test_pairplot_kde(self): vars = ["x", "y", "z"] g = pairplot(self.df, diag_kind="kde") for ax in g.diag_axes: nt.assert_equal(len(ax.lines), 1) for i, j in zip(*np.triu_indices_from(g.axes, 1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.tril_indices_from(g.axes, -1)): ax = g.axes[i, j] x_in = self.df[vars[j]] y_in = self.df[vars[i]] x_out, y_out = ax.collections[0].get_offsets().T npt.assert_array_equal(x_in, x_out) npt.assert_array_equal(y_in, y_out) for i, j in zip(*np.diag_indices_from(g.axes)): ax = g.axes[i, j] nt.assert_equal(len(ax.collections), 0) plt.close("all") @skipif(old_matplotlib) def test_pairplot_markers(self): vars = ["x", "y", "z"] markers = ["o", "x", "s", "d"] g = pairplot(self.df, hue="a", vars=vars, markers=markers) nt.assert_equal(g.hue_kws["marker"], markers) plt.close("all") with nt.assert_raises(ValueError): g = pairplot(self.df, hue="a", vars=vars, markers=markers[:-2]) @classmethod def teardown_class(cls): """Ensure that all figures are closed on exit.""" plt.close("all") class TestJointGrid(object): rs = np.random.RandomState(sum(map(ord, "JointGrid"))) x = rs.randn(100) y = rs.randn(100) x_na = x.copy() x_na[10] = np.nan x_na[20] = np.nan data = pd.DataFrame(dict(x=x, y=y, x_na=x_na)) def test_margin_grid_from_arrays(self): g = ag.JointGrid(self.x, self.y) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y) plt.close("all") def test_margin_grid_from_series(self): g = ag.JointGrid(self.data.x, self.data.y) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y) plt.close("all") def test_margin_grid_from_dataframe(self): g = ag.JointGrid("x", "y", self.data) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y) plt.close("all") def test_margin_grid_axis_labels(self): g = ag.JointGrid("x", "y", self.data) xlabel, ylabel = g.ax_joint.get_xlabel(), g.ax_joint.get_ylabel() nt.assert_equal(xlabel, "x") nt.assert_equal(ylabel, "y") g.set_axis_labels("x variable", "y variable") xlabel, ylabel = g.ax_joint.get_xlabel(), g.ax_joint.get_ylabel() nt.assert_equal(xlabel, "x variable") nt.assert_equal(ylabel, "y variable") plt.close("all") def test_dropna(self): g = ag.JointGrid("x_na", "y", self.data, dropna=False) nt.assert_equal(len(g.x), len(self.x_na)) g = ag.JointGrid("x_na", "y", self.data, dropna=True) nt.assert_equal(len(g.x), pd.notnull(self.x_na).sum()) plt.close("all") def test_axlims(self): lim = (-3, 3) g = ag.JointGrid("x", "y", self.data, xlim=lim, ylim=lim) nt.assert_equal(g.ax_joint.get_xlim(), lim) nt.assert_equal(g.ax_joint.get_ylim(), lim) nt.assert_equal(g.ax_marg_x.get_xlim(), lim) nt.assert_equal(g.ax_marg_y.get_ylim(), lim) def test_marginal_ticks(self): g = ag.JointGrid("x", "y", self.data) nt.assert_true(~len(g.ax_marg_x.get_xticks())) nt.assert_true(~len(g.ax_marg_y.get_yticks())) plt.close("all") def test_bivariate_plot(self): g = ag.JointGrid("x", "y", self.data) g.plot_joint(plt.plot) x, y = g.ax_joint.lines[0].get_xydata().T npt.assert_array_equal(x, self.x) npt.assert_array_equal(y, self.y) plt.close("all") def test_univariate_plot(self): g = ag.JointGrid("x", "x", self.data) g.plot_marginals(kdeplot) _, y1 = g.ax_marg_x.lines[0].get_xydata().T y2, _ = g.ax_marg_y.lines[0].get_xydata().T npt.assert_array_equal(y1, y2) plt.close("all") def test_plot(self): g = ag.JointGrid("x", "x", self.data) g.plot(plt.plot, kdeplot) x, y = g.ax_joint.lines[0].get_xydata().T npt.assert_array_equal(x, self.x) npt.assert_array_equal(y, self.x) _, y1 = g.ax_marg_x.lines[0].get_xydata().T y2, _ = g.ax_marg_y.lines[0].get_xydata().T npt.assert_array_equal(y1, y2) plt.close("all") def test_annotate(self): g = ag.JointGrid("x", "y", self.data) rp = stats.pearsonr(self.x, self.y) g.annotate(stats.pearsonr) annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, "pearsonr = %.2g; p = %.2g" % rp) g.annotate(stats.pearsonr, stat="correlation") annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, "correlation = %.2g; p = %.2g" % rp) def rsquared(x, y): return stats.pearsonr(x, y)[0] ** 2 r2 = rsquared(self.x, self.y) g.annotate(rsquared) annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, "rsquared = %.2g" % r2) template = "{stat} = {val:.3g} (p = {p:.3g})" g.annotate(stats.pearsonr, template=template) annotation = g.ax_joint.legend_.texts[0].get_text() nt.assert_equal(annotation, template.format(stat="pearsonr", val=rp[0], p=rp[1])) plt.close("all") def test_space(self): g = ag.JointGrid("x", "y", self.data, space=0) joint_bounds = g.ax_joint.bbox.bounds marg_x_bounds = g.ax_marg_x.bbox.bounds marg_y_bounds = g.ax_marg_y.bbox.bounds nt.assert_equal(joint_bounds[2], marg_x_bounds[2]) nt.assert_equal(joint_bounds[3], marg_y_bounds[3]) @classmethod def teardown_class(cls): """Ensure that all figures are closed on exit.""" plt.close("all")
bsd-3-clause
BadrYoubiIdrissi/TIPE-Algorithme-Genetique
Source/NEAT/test.py
1
2640
# -*- coding: utf-8 -*- """ Created on Wed Oct 12 11:36:14 2016 @author: Badr Youbi Idrissi """ import pygame import pygame.gfxdraw import numpy as np from pygame.locals import * from individu import Individu from phenotype import Phenotype from population import Population from datadisplay import DataDisplay import utilitaires as ut import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D pygame.init() screen = pygame.display.set_mode((860, 600), DOUBLEBUF and RESIZABLE) pygame.display.set_caption("Test") f = pygame.font.SysFont(pygame.font.get_default_font(), 20) clock = pygame.time.Clock() nb_e = 3 nb_s = 1 pop = Population(10, nb_e, nb_s) pop.generer() status = DataDisplay((0,0), padding = 20) status.add("FPS", lambda : clock.get_fps()) status.add("Current generation", lambda : pop.generationCount) status.add("Number of species", lambda : len(pop.especes)) status.add("Best fitness", pop.getBestFitness) status.add("Best shared fitness", pop.getBestSharedFitness) status.add("Average fitness", lambda : pop.averageFitness) evol = False while True: clock.tick() screen.fill((255,255,255)) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit() elif event.type == KEYDOWN and event.key == K_UP: nbPoints = 100 X,Y = np.meshgrid(np.linspace(0,1,nbPoints),np.linspace(0,1,nbPoints)) Z = np.zeros((nbPoints,nbPoints)) for i in range(nbPoints): for j in range(nbPoints): pop.best[-1].phenotype.evaluate(ut.entree('1;'+str(X[i,j])+';'+str(Y[i,j]))) Z[i,j] = pop.best[-1].output() fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z) plt.show() elif event.type == KEYDOWN and event.key == K_DOWN: l = [pop.contenu[i].fitness for i in range(pop.length)] l2 = [pop.contenu[i].sharedFitness for i in range(pop.length)] plt.plot(range(pop.length), l) plt.plot(range(pop.length), l2) plt.show() elif event.type == KEYDOWN and event.key == K_e: evol = not(evol) elif event.type == VIDEORESIZE: pygame.display.set_mode((event.w, event.h), DOUBLEBUF and RESIZABLE) if evol: pop.evoluer() if (pop.generationCount % 10 == 0): pop.updateBest() pop.draw(status.police) status.draw() pygame.display.flip()
gpl-3.0
gef756/scipy
scipy/interpolate/interpolate.py
25
80287
""" Classes for interpolating values. """ from __future__ import division, print_function, absolute_import __all__ = ['interp1d', 'interp2d', 'spline', 'spleval', 'splmake', 'spltopp', 'ppform', 'lagrange', 'PPoly', 'BPoly', 'RegularGridInterpolator', 'interpn'] import itertools from numpy import (shape, sometrue, array, transpose, searchsorted, ones, logical_or, atleast_1d, atleast_2d, ravel, dot, poly1d, asarray, intp) import numpy as np import scipy.linalg import scipy.special as spec from scipy.special import comb import math import warnings import functools import operator from scipy._lib.six import xrange, integer_types from . import fitpack from . import dfitpack from . import _fitpack from .polyint import _Interpolator1D from . import _ppoly from .fitpack2 import RectBivariateSpline from .interpnd import _ndim_coords_from_arrays def reduce_sometrue(a): all = a while len(shape(all)) > 1: all = sometrue(all, axis=0) return all def prod(x): """Product of a list of numbers; ~40x faster vs np.prod for Python tuples""" if len(x) == 0: return 1 return functools.reduce(operator.mul, x) def lagrange(x, w): """ Return a Lagrange interpolating polynomial. Given two 1-D arrays `x` and `w,` returns the Lagrange interpolating polynomial through the points ``(x, w)``. Warning: This implementation is numerically unstable. Do not expect to be able to use more than about 20 points even if they are chosen optimally. Parameters ---------- x : array_like `x` represents the x-coordinates of a set of datapoints. w : array_like `w` represents the y-coordinates of a set of datapoints, i.e. f(`x`). Returns ------- lagrange : numpy.poly1d instance The Lagrange interpolating polynomial. """ M = len(x) p = poly1d(0.0) for j in xrange(M): pt = poly1d(w[j]) for k in xrange(M): if k == j: continue fac = x[j]-x[k] pt *= poly1d([1.0, -x[k]])/fac p += pt return p # !! Need to find argument for keeping initialize. If it isn't # !! found, get rid of it! class interp2d(object): """ interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan) Interpolate over a 2-D grid. `x`, `y` and `z` are arrays of values used to approximate some function f: ``z = f(x, y)``. This class returns a function whose call method uses spline interpolation to find the value of new points. If `x` and `y` represent a regular grid, consider using RectBivariateSpline. Methods ------- __call__ Parameters ---------- x, y : array_like Arrays defining the data point coordinates. If the points lie on a regular grid, `x` can specify the column coordinates and `y` the row coordinates, for example:: >>> x = [0,1,2]; y = [0,3]; z = [[1,2,3], [4,5,6]] Otherwise, `x` and `y` must specify the full coordinates for each point, for example:: >>> x = [0,1,2,0,1,2]; y = [0,0,0,3,3,3]; z = [1,2,3,4,5,6] If `x` and `y` are multi-dimensional, they are flattened before use. z : array_like The values of the function to interpolate at the data points. If `z` is a multi-dimensional array, it is flattened before use. The length of a flattened `z` array is either len(`x`)*len(`y`) if `x` and `y` specify the column and row coordinates or ``len(z) == len(x) == len(y)`` if `x` and `y` specify coordinates for each point. kind : {'linear', 'cubic', 'quintic'}, optional The kind of spline interpolation to use. Default is 'linear'. copy : bool, optional If True, the class makes internal copies of x, y and z. If False, references may be used. The default is to copy. bounds_error : bool, optional If True, when interpolated values are requested outside of the domain of the input data (x,y), a ValueError is raised. If False, then `fill_value` is used. fill_value : number, optional If provided, the value to use for points outside of the interpolation domain. If omitted (None), values outside the domain are extrapolated. Returns ------- values_x : ndarray, shape xi.shape[:-1] + values.shape[ndim:] Interpolated values at input coordinates. See Also -------- RectBivariateSpline : Much faster 2D interpolation if your input data is on a grid bisplrep, bisplev : Spline interpolation based on FITPACK BivariateSpline : a more recent wrapper of the FITPACK routines interp1d : one dimension version of this function Notes ----- The minimum number of data points required along the interpolation axis is ``(k+1)**2``, with k=1 for linear, k=3 for cubic and k=5 for quintic interpolation. The interpolator is constructed by `bisplrep`, with a smoothing factor of 0. If more control over smoothing is needed, `bisplrep` should be used directly. Examples -------- Construct a 2-D grid and interpolate on it: >>> from scipy import interpolate >>> x = np.arange(-5.01, 5.01, 0.25) >>> y = np.arange(-5.01, 5.01, 0.25) >>> xx, yy = np.meshgrid(x, y) >>> z = np.sin(xx**2+yy**2) >>> f = interpolate.interp2d(x, y, z, kind='cubic') Now use the obtained interpolation function and plot the result: >>> import matplotlib.pyplot as plt >>> xnew = np.arange(-5.01, 5.01, 1e-2) >>> ynew = np.arange(-5.01, 5.01, 1e-2) >>> znew = f(xnew, ynew) >>> plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-') >>> plt.show() """ def __init__(self, x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=None): x = ravel(x) y = ravel(y) z = asarray(z) rectangular_grid = (z.size == len(x) * len(y)) if rectangular_grid: if z.ndim == 2: if z.shape != (len(y), len(x)): raise ValueError("When on a regular grid with x.size = m " "and y.size = n, if z.ndim == 2, then z " "must have shape (n, m)") if not np.all(x[1:] >= x[:-1]): j = np.argsort(x) x = x[j] z = z[:, j] if not np.all(y[1:] >= y[:-1]): j = np.argsort(y) y = y[j] z = z[j, :] z = ravel(z.T) else: z = ravel(z) if len(x) != len(y): raise ValueError( "x and y must have equal lengths for non rectangular grid") if len(z) != len(x): raise ValueError( "Invalid length for input z for non rectangular grid") try: kx = ky = {'linear': 1, 'cubic': 3, 'quintic': 5}[kind] except KeyError: raise ValueError("Unsupported interpolation type.") if not rectangular_grid: # TODO: surfit is really not meant for interpolation! self.tck = fitpack.bisplrep(x, y, z, kx=kx, ky=ky, s=0.0) else: nx, tx, ny, ty, c, fp, ier = dfitpack.regrid_smth( x, y, z, None, None, None, None, kx=kx, ky=ky, s=0.0) self.tck = (tx[:nx], ty[:ny], c[:(nx - kx - 1) * (ny - ky - 1)], kx, ky) self.bounds_error = bounds_error self.fill_value = fill_value self.x, self.y, self.z = [array(a, copy=copy) for a in (x, y, z)] self.x_min, self.x_max = np.amin(x), np.amax(x) self.y_min, self.y_max = np.amin(y), np.amax(y) def __call__(self, x, y, dx=0, dy=0, assume_sorted=False): """Interpolate the function. Parameters ---------- x : 1D array x-coordinates of the mesh on which to interpolate. y : 1D array y-coordinates of the mesh on which to interpolate. dx : int >= 0, < kx Order of partial derivatives in x. dy : int >= 0, < ky Order of partial derivatives in y. assume_sorted : bool, optional If False, values of `x` and `y` can be in any order and they are sorted first. If True, `x` and `y` have to be arrays of monotonically increasing values. Returns ------- z : 2D array with shape (len(y), len(x)) The interpolated values. """ x = atleast_1d(x) y = atleast_1d(y) if x.ndim != 1 or y.ndim != 1: raise ValueError("x and y should both be 1-D arrays") if not assume_sorted: x = np.sort(x) y = np.sort(y) if self.bounds_error or self.fill_value is not None: out_of_bounds_x = (x < self.x_min) | (x > self.x_max) out_of_bounds_y = (y < self.y_min) | (y > self.y_max) any_out_of_bounds_x = np.any(out_of_bounds_x) any_out_of_bounds_y = np.any(out_of_bounds_y) if self.bounds_error and (any_out_of_bounds_x or any_out_of_bounds_y): raise ValueError("Values out of range; x must be in %r, y in %r" % ((self.x_min, self.x_max), (self.y_min, self.y_max))) z = fitpack.bisplev(x, y, self.tck, dx, dy) z = atleast_2d(z) z = transpose(z) if self.fill_value is not None: if any_out_of_bounds_x: z[:, out_of_bounds_x] = self.fill_value if any_out_of_bounds_y: z[out_of_bounds_y, :] = self.fill_value if len(z) == 1: z = z[0] return array(z) class interp1d(_Interpolator1D): """ Interpolate a 1-D function. `x` and `y` are arrays of values used to approximate some function f: ``y = f(x)``. This class returns a function whose call method uses interpolation to find the value of new points. Parameters ---------- x : (N,) array_like A 1-D array of real values. y : (...,N,...) array_like A N-D array of real values. The length of `y` along the interpolation axis must be equal to the length of `x`. kind : str or int, optional Specifies the kind of interpolation as a string ('linear', 'nearest', 'zero', 'slinear', 'quadratic, 'cubic' where 'slinear', 'quadratic' and 'cubic' refer to a spline interpolation of first, second or third order) or as an integer specifying the order of the spline interpolator to use. Default is 'linear'. axis : int, optional Specifies the axis of `y` along which to interpolate. Interpolation defaults to the last axis of `y`. copy : bool, optional If True, the class makes internal copies of x and y. If False, references to `x` and `y` are used. The default is to copy. bounds_error : bool, optional If True, a ValueError is raised any time interpolation is attempted on a value outside of the range of x (where extrapolation is necessary). If False, out of bounds values are assigned `fill_value`. By default, an error is raised. fill_value : float, optional If provided, then this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. assume_sorted : bool, optional If False, values of `x` can be in any order and they are sorted first. If True, `x` has to be an array of monotonically increasing values. Methods ------- __call__ See Also -------- splrep, splev Spline interpolation/smoothing based on FITPACK. UnivariateSpline : An object-oriented wrapper of the FITPACK routines. interp2d : 2-D interpolation Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy import interpolate >>> x = np.arange(0, 10) >>> y = np.exp(-x/3.0) >>> f = interpolate.interp1d(x, y) >>> xnew = np.arange(0, 9, 0.1) >>> ynew = f(xnew) # use interpolation function returned by `interp1d` >>> plt.plot(x, y, 'o', xnew, ynew, '-') >>> plt.show() """ def __init__(self, x, y, kind='linear', axis=-1, copy=True, bounds_error=True, fill_value=np.nan, assume_sorted=False): """ Initialize a 1D linear interpolation class.""" _Interpolator1D.__init__(self, x, y, axis=axis) self.copy = copy self.bounds_error = bounds_error self.fill_value = fill_value if kind in ['zero', 'slinear', 'quadratic', 'cubic']: order = {'nearest': 0, 'zero': 0,'slinear': 1, 'quadratic': 2, 'cubic': 3}[kind] kind = 'spline' elif isinstance(kind, int): order = kind kind = 'spline' elif kind not in ('linear', 'nearest'): raise NotImplementedError("%s is unsupported: Use fitpack " "routines for other types." % kind) x = array(x, copy=self.copy) y = array(y, copy=self.copy) if not assume_sorted: ind = np.argsort(x) x = x[ind] y = np.take(y, ind, axis=axis) if x.ndim != 1: raise ValueError("the x array must have exactly one dimension.") if y.ndim == 0: raise ValueError("the y array must have at least one dimension.") # Force-cast y to a floating-point type, if it's not yet one if not issubclass(y.dtype.type, np.inexact): y = y.astype(np.float_) # Backward compatibility self.axis = axis % y.ndim # Interpolation goes internally along the first axis self.y = y y = self._reshape_yi(y) # Adjust to interpolation kind; store reference to *unbound* # interpolation methods, in order to avoid circular references to self # stored in the bound instance methods, and therefore delayed garbage # collection. See: http://docs.python.org/2/reference/datamodel.html if kind in ('linear', 'nearest'): # Make a "view" of the y array that is rotated to the interpolation # axis. minval = 2 if kind == 'nearest': self.x_bds = (x[1:] + x[:-1]) / 2.0 self._call = self.__class__._call_nearest else: self._call = self.__class__._call_linear else: minval = order + 1 self._spline = splmake(x, y, order=order) self._call = self.__class__._call_spline if len(x) < minval: raise ValueError("x and y arrays must have at " "least %d entries" % minval) self._kind = kind self.x = x self._y = y def _call_linear(self, x_new): # 2. Find where in the orignal data, the values to interpolate # would be inserted. # Note: If x_new[n] == x[m], then m is returned by searchsorted. x_new_indices = searchsorted(self.x, x_new) # 3. Clip x_new_indices so that they are within the range of # self.x indices and at least 1. Removes mis-interpolation # of x_new[n] = x[0] x_new_indices = x_new_indices.clip(1, len(self.x)-1).astype(int) # 4. Calculate the slope of regions that each x_new value falls in. lo = x_new_indices - 1 hi = x_new_indices x_lo = self.x[lo] x_hi = self.x[hi] y_lo = self._y[lo] y_hi = self._y[hi] # Note that the following two expressions rely on the specifics of the # broadcasting semantics. slope = (y_hi - y_lo) / (x_hi - x_lo)[:, None] # 5. Calculate the actual value for each entry in x_new. y_new = slope*(x_new - x_lo)[:, None] + y_lo return y_new def _call_nearest(self, x_new): """ Find nearest neighbour interpolated y_new = f(x_new).""" # 2. Find where in the averaged data the values to interpolate # would be inserted. # Note: use side='left' (right) to searchsorted() to define the # halfway point to be nearest to the left (right) neighbour x_new_indices = searchsorted(self.x_bds, x_new, side='left') # 3. Clip x_new_indices so that they are within the range of x indices. x_new_indices = x_new_indices.clip(0, len(self.x)-1).astype(intp) # 4. Calculate the actual value for each entry in x_new. y_new = self._y[x_new_indices] return y_new def _call_spline(self, x_new): return spleval(self._spline, x_new) def _evaluate(self, x_new): # 1. Handle values in x_new that are outside of x. Throw error, # or return a list of mask array indicating the outofbounds values. # The behavior is set by the bounds_error variable. x_new = asarray(x_new) out_of_bounds = self._check_bounds(x_new) y_new = self._call(self, x_new) if len(y_new) > 0: y_new[out_of_bounds] = self.fill_value return y_new def _check_bounds(self, x_new): """Check the inputs for being in the bounds of the interpolated data. Parameters ---------- x_new : array Returns ------- out_of_bounds : bool array The mask on x_new of values that are out of the bounds. """ # If self.bounds_error is True, we raise an error if any x_new values # fall outside the range of x. Otherwise, we return an array indicating # which values are outside the boundary region. below_bounds = x_new < self.x[0] above_bounds = x_new > self.x[-1] # !! Could provide more information about which values are out of bounds if self.bounds_error and below_bounds.any(): raise ValueError("A value in x_new is below the interpolation " "range.") if self.bounds_error and above_bounds.any(): raise ValueError("A value in x_new is above the interpolation " "range.") # !! Should we emit a warning if some values are out of bounds? # !! matlab does not. out_of_bounds = logical_or(below_bounds, above_bounds) return out_of_bounds class _PPolyBase(object): """ Base class for piecewise polynomials. """ __slots__ = ('c', 'x', 'extrapolate', 'axis') def __init__(self, c, x, extrapolate=None, axis=0): self.c = np.asarray(c) self.x = np.ascontiguousarray(x, dtype=np.float64) if extrapolate is None: extrapolate = True self.extrapolate = bool(extrapolate) if not (0 <= axis < self.c.ndim - 1): raise ValueError("%s must be between 0 and %s" % (axis, c.ndim-1)) self.axis = axis if axis != 0: # roll the interpolation axis to be the first one in self.c # More specifically, the target shape for self.c is (k, m, ...), # and axis !=0 means that we have c.shape (..., k, m, ...) # ^ # axis # So we roll two of them. self.c = np.rollaxis(self.c, axis+1) self.c = np.rollaxis(self.c, axis+1) if self.x.ndim != 1: raise ValueError("x must be 1-dimensional") if self.x.size < 2: raise ValueError("at least 2 breakpoints are needed") if self.c.ndim < 2: raise ValueError("c must have at least 2 dimensions") if self.c.shape[0] == 0: raise ValueError("polynomial must be at least of order 0") if self.c.shape[1] != self.x.size-1: raise ValueError("number of coefficients != len(x)-1") if np.any(self.x[1:] - self.x[:-1] < 0): raise ValueError("x-coordinates are not in increasing order") dtype = self._get_dtype(self.c.dtype) self.c = np.ascontiguousarray(self.c, dtype=dtype) def _get_dtype(self, dtype): if np.issubdtype(dtype, np.complexfloating) \ or np.issubdtype(self.c.dtype, np.complexfloating): return np.complex_ else: return np.float_ @classmethod def construct_fast(cls, c, x, extrapolate=None, axis=0): """ Construct the piecewise polynomial without making checks. Takes the same parameters as the constructor. Input arguments `c` and `x` must be arrays of the correct shape and type. The `c` array can only be of dtypes float and complex, and `x` array must have dtype float. """ self = object.__new__(cls) self.c = c self.x = x self.axis = axis if extrapolate is None: extrapolate = True self.extrapolate = extrapolate return self def _ensure_c_contiguous(self): """ c and x may be modified by the user. The Cython code expects that they are C contiguous. """ if not self.x.flags.c_contiguous: self.x = self.x.copy() if not self.c.flags.c_contiguous: self.c = self.c.copy() def extend(self, c, x, right=True): """ Add additional breakpoints and coefficients to the polynomial. Parameters ---------- c : ndarray, size (k, m, ...) Additional coefficients for polynomials in intervals ``self.x[-1] <= x < x_right[0]``, ``x_right[0] <= x < x_right[1]``, ..., ``x_right[m-2] <= x < x_right[m-1]`` x : ndarray, size (m,) Additional breakpoints. Must be sorted and either to the right or to the left of the current breakpoints. right : bool, optional Whether the new intervals are to the right or to the left of the current intervals. """ c = np.asarray(c) x = np.asarray(x) if c.ndim < 2: raise ValueError("invalid dimensions for c") if x.ndim != 1: raise ValueError("invalid dimensions for x") if x.shape[0] != c.shape[1]: raise ValueError("x and c have incompatible sizes") if c.shape[2:] != self.c.shape[2:] or c.ndim != self.c.ndim: raise ValueError("c and self.c have incompatible shapes") if right: if x[0] < self.x[-1]: raise ValueError("new x are not to the right of current ones") else: if x[-1] > self.x[0]: raise ValueError("new x are not to the left of current ones") if c.size == 0: return dtype = self._get_dtype(c.dtype) k2 = max(c.shape[0], self.c.shape[0]) c2 = np.zeros((k2, self.c.shape[1] + c.shape[1]) + self.c.shape[2:], dtype=dtype) if right: c2[k2-self.c.shape[0]:, :self.c.shape[1]] = self.c c2[k2-c.shape[0]:, self.c.shape[1]:] = c self.x = np.r_[self.x, x] else: c2[k2-self.c.shape[0]:, :c.shape[1]] = c c2[k2-c.shape[0]:, c.shape[1]:] = self.c self.x = np.r_[x, self.x] self.c = c2 def __call__(self, x, nu=0, extrapolate=None): """ Evaluate the piecewise polynomial or its derivative Parameters ---------- x : array_like Points to evaluate the interpolant at. nu : int, optional Order of derivative to evaluate. Must be non-negative. extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Returns ------- y : array_like Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x. Notes ----- Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open, ``[a, b)``, except for the last interval which is closed ``[a, b]``. """ if extrapolate is None: extrapolate = self.extrapolate x = np.asarray(x) x_shape, x_ndim = x.shape, x.ndim x = np.ascontiguousarray(x.ravel(), dtype=np.float_) out = np.empty((len(x), prod(self.c.shape[2:])), dtype=self.c.dtype) self._ensure_c_contiguous() self._evaluate(x, nu, extrapolate, out) out = out.reshape(x_shape + self.c.shape[2:]) if self.axis != 0: # transpose to move the calculated values to the interpolation axis l = list(range(out.ndim)) l = l[x_ndim:x_ndim+self.axis] + l[:x_ndim] + l[x_ndim+self.axis:] out = out.transpose(l) return out class PPoly(_PPolyBase): """ Piecewise polynomial in terms of coefficients and breakpoints The polynomial in the ith interval is ``x[i] <= xp < x[i+1]``:: S = sum(c[m, i] * (xp - x[i])**(k-m) for m in range(k+1)) where ``k`` is the degree of the polynomial. This representation is the local power basis. Parameters ---------- c : ndarray, shape (k, m, ...) Polynomial coefficients, order `k` and `m` intervals x : ndarray, shape (m+1,) Polynomial breakpoints. These must be sorted in increasing order. extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Default: True. axis : int, optional Interpolation axis. Default is zero. Attributes ---------- x : ndarray Breakpoints. c : ndarray Coefficients of the polynomials. They are reshaped to a 3-dimensional array with the last dimension representing the trailing dimensions of the original coefficient array. axis : int Interpolation axis. Methods ------- __call__ derivative antiderivative integrate roots extend from_spline from_bernstein_basis construct_fast See also -------- BPoly : piecewise polynomials in the Bernstein basis Notes ----- High-order polynomials in the power basis can be numerically unstable. Precision problems can start to appear for orders larger than 20-30. """ def _evaluate(self, x, nu, extrapolate, out): _ppoly.evaluate(self.c.reshape(self.c.shape[0], self.c.shape[1], -1), self.x, x, nu, bool(extrapolate), out) def derivative(self, nu=1): """ Construct a new piecewise polynomial representing the derivative. Parameters ---------- nu : int, optional Order of derivative to evaluate. (Default: 1) If negative, the antiderivative is returned. Returns ------- pp : PPoly Piecewise polynomial of order k2 = k - n representing the derivative of this polynomial. Notes ----- Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open, ``[a, b)``, except for the last interval which is closed ``[a, b]``. """ if nu < 0: return self.antiderivative(-nu) # reduce order if nu == 0: c2 = self.c.copy() else: c2 = self.c[:-nu,:].copy() if c2.shape[0] == 0: # derivative of order 0 is zero c2 = np.zeros((1,) + c2.shape[1:], dtype=c2.dtype) # multiply by the correct rising factorials factor = spec.poch(np.arange(c2.shape[0], 0, -1), nu) c2 *= factor[(slice(None),) + (None,)*(c2.ndim-1)] # construct a compatible polynomial return self.construct_fast(c2, self.x, self.extrapolate, self.axis) def antiderivative(self, nu=1): """ Construct a new piecewise polynomial representing the antiderivative. Antiderivativative is also the indefinite integral of the function, and derivative is its inverse operation. Parameters ---------- nu : int, optional Order of antiderivative to evaluate. (Default: 1) If negative, the derivative is returned. Returns ------- pp : PPoly Piecewise polynomial of order k2 = k + n representing the antiderivative of this polynomial. Notes ----- The antiderivative returned by this function is continuous and continuously differentiable to order n-1, up to floating point rounding error. """ if nu <= 0: return self.derivative(-nu) c = np.zeros((self.c.shape[0] + nu, self.c.shape[1]) + self.c.shape[2:], dtype=self.c.dtype) c[:-nu] = self.c # divide by the correct rising factorials factor = spec.poch(np.arange(self.c.shape[0], 0, -1), nu) c[:-nu] /= factor[(slice(None),) + (None,)*(c.ndim-1)] # fix continuity of added degrees of freedom self._ensure_c_contiguous() _ppoly.fix_continuity(c.reshape(c.shape[0], c.shape[1], -1), self.x, nu - 1) # construct a compatible polynomial return self.construct_fast(c, self.x, self.extrapolate, self.axis) def integrate(self, a, b, extrapolate=None): """ Compute a definite integral over a piecewise polynomial. Parameters ---------- a : float Lower integration bound b : float Upper integration bound extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Returns ------- ig : array_like Definite integral of the piecewise polynomial over [a, b] """ if extrapolate is None: extrapolate = self.extrapolate # Swap integration bounds if needed sign = 1 if b < a: a, b = b, a sign = -1 # Compute the integral range_int = np.empty((prod(self.c.shape[2:]),), dtype=self.c.dtype) self._ensure_c_contiguous() _ppoly.integrate(self.c.reshape(self.c.shape[0], self.c.shape[1], -1), self.x, a, b, bool(extrapolate), out=range_int) # Return range_int *= sign return range_int.reshape(self.c.shape[2:]) def roots(self, discontinuity=True, extrapolate=None): """ Find real roots of the piecewise polynomial. Parameters ---------- discontinuity : bool, optional Whether to report sign changes across discontinuities at breakpoints as roots. extrapolate : bool, optional Whether to return roots from the polynomial extrapolated based on first and last intervals. Returns ------- roots : ndarray Roots of the polynomial(s). If the PPoly object describes multiple polynomials, the return value is an object array whose each element is an ndarray containing the roots. Notes ----- This routine works only on real-valued polynomials. If the piecewise polynomial contains sections that are identically zero, the root list will contain the start point of the corresponding interval, followed by a ``nan`` value. If the polynomial is discontinuous across a breakpoint, and there is a sign change across the breakpoint, this is reported if the `discont` parameter is True. Examples -------- Finding roots of ``[x**2 - 1, (x - 1)**2]`` defined on intervals ``[-2, 1], [1, 2]``: >>> from scipy.interpolate import PPoly >>> pp = PPoly(np.array([[1, -4, 3], [1, 0, 0]]).T, [-2, 1, 2]) >>> pp.roots() array([-1., 1.]) """ if extrapolate is None: extrapolate = self.extrapolate self._ensure_c_contiguous() if np.issubdtype(self.c.dtype, np.complexfloating): raise ValueError("Root finding is only for " "real-valued polynomials") r = _ppoly.real_roots(self.c.reshape(self.c.shape[0], self.c.shape[1], -1), self.x, bool(discontinuity), bool(extrapolate)) if self.c.ndim == 2: return r[0] else: r2 = np.empty(prod(self.c.shape[2:]), dtype=object) # this for-loop is equivalent to ``r2[...] = r``, but that's broken # in numpy 1.6.0 for ii, root in enumerate(r): r2[ii] = root return r2.reshape(self.c.shape[2:]) @classmethod def from_spline(cls, tck, extrapolate=None): """ Construct a piecewise polynomial from a spline Parameters ---------- tck A spline, as returned by `splrep` extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Default: True. """ t, c, k = tck cvals = np.empty((k + 1, len(t)-1), dtype=c.dtype) for m in xrange(k, -1, -1): y = fitpack.splev(t[:-1], tck, der=m) cvals[k - m, :] = y/spec.gamma(m+1) return cls.construct_fast(cvals, t, extrapolate) @classmethod def from_bernstein_basis(cls, bp, extrapolate=None): """ Construct a piecewise polynomial in the power basis from a polynomial in Bernstein basis. Parameters ---------- bp : BPoly A Bernstein basis polynomial, as created by BPoly extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Default: True. """ dx = np.diff(bp.x) k = bp.c.shape[0] - 1 # polynomial order rest = (None,)*(bp.c.ndim-2) c = np.zeros_like(bp.c) for a in range(k+1): factor = (-1)**(a) * comb(k, a) * bp.c[a] for s in range(a, k+1): val = comb(k-a, s-a) * (-1)**s c[k-s] += factor * val / dx[(slice(None),)+rest]**s if extrapolate is None: extrapolate = bp.extrapolate return cls.construct_fast(c, bp.x, extrapolate, bp.axis) class BPoly(_PPolyBase): """ Piecewise polynomial in terms of coefficients and breakpoints The polynomial in the ``i``-th interval ``x[i] <= xp < x[i+1]`` is written in the Bernstein polynomial basis:: S = sum(c[a, i] * b(a, k; x) for a in range(k+1)) where ``k`` is the degree of the polynomial, and:: b(a, k; x) = comb(k, a) * t**k * (1 - t)**(k - a) with ``t = (x - x[i]) / (x[i+1] - x[i])``. Parameters ---------- c : ndarray, shape (k, m, ...) Polynomial coefficients, order `k` and `m` intervals x : ndarray, shape (m+1,) Polynomial breakpoints. These must be sorted in increasing order. extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Default: True. axis : int, optional Interpolation axis. Default is zero. Attributes ---------- x : ndarray Breakpoints. c : ndarray Coefficients of the polynomials. They are reshaped to a 3-dimensional array with the last dimension representing the trailing dimensions of the original coefficient array. axis : int Interpolation axis. Methods ------- __call__ extend derivative antiderivative integrate construct_fast from_power_basis from_derivatives See also -------- PPoly : piecewise polynomials in the power basis Notes ----- Properties of Bernstein polynomials are well documented in the literature. Here's a non-exhaustive list: .. [1] http://en.wikipedia.org/wiki/Bernstein_polynomial .. [2] Kenneth I. Joy, Bernstein polynomials, http://www.idav.ucdavis.edu/education/CAGDNotes/Bernstein-Polynomials.pdf .. [3] E. H. Doha, A. H. Bhrawy, and M. A. Saker, Boundary Value Problems, vol 2011, article ID 829546, doi:10.1155/2011/829543 Examples -------- >>> from scipy.interpolate import BPoly >>> x = [0, 1] >>> c = [[1], [2], [3]] >>> bp = BPoly(c, x) This creates a 2nd order polynomial .. math:: B(x) = 1 \\times b_{0, 2}(x) + 2 \\times b_{1, 2}(x) + 3 \\times b_{2, 2}(x) \\\\ = 1 \\times (1-x)^2 + 2 \\times 2 x (1 - x) + 3 \\times x^2 """ def _evaluate(self, x, nu, extrapolate, out): _ppoly.evaluate_bernstein( self.c.reshape(self.c.shape[0], self.c.shape[1], -1), self.x, x, nu, bool(extrapolate), out) def derivative(self, nu=1): """ Construct a new piecewise polynomial representing the derivative. Parameters ---------- nu : int, optional Order of derivative to evaluate. (Default: 1) If negative, the antiderivative is returned. Returns ------- bp : BPoly Piecewise polynomial of order k2 = k - nu representing the derivative of this polynomial. """ if nu < 0: return self.antiderivative(-nu) if nu > 1: bp = self for k in range(nu): bp = bp.derivative() return bp # reduce order if nu == 0: c2 = self.c.copy() else: # For a polynomial # B(x) = \sum_{a=0}^{k} c_a b_{a, k}(x), # we use the fact that # b'_{a, k} = k ( b_{a-1, k-1} - b_{a, k-1} ), # which leads to # B'(x) = \sum_{a=0}^{k-1} (c_{a+1} - c_a) b_{a, k-1} # # finally, for an interval [y, y + dy] with dy != 1, # we need to correct for an extra power of dy rest = (None,)*(self.c.ndim-2) k = self.c.shape[0] - 1 dx = np.diff(self.x)[(None, slice(None))+rest] c2 = k * np.diff(self.c, axis=0) / dx if c2.shape[0] == 0: # derivative of order 0 is zero c2 = np.zeros((1,) + c2.shape[1:], dtype=c2.dtype) # construct a compatible polynomial return self.construct_fast(c2, self.x, self.extrapolate, self.axis) def antiderivative(self, nu=1): """ Construct a new piecewise polynomial representing the antiderivative. Parameters ---------- nu : int, optional Order of derivative to evaluate. (Default: 1) If negative, the derivative is returned. Returns ------- bp : BPoly Piecewise polynomial of order k2 = k + nu representing the antiderivative of this polynomial. """ if nu <= 0: return self.derivative(-nu) if nu > 1: bp = self for k in range(nu): bp = bp.antiderivative() return bp # Construct the indefinite integrals on individual intervals c, x = self.c, self.x k = c.shape[0] c2 = np.zeros((k+1,) + c.shape[1:], dtype=c.dtype) c2[1:, ...] = np.cumsum(c, axis=0) / k delta = x[1:] - x[:-1] c2 *= delta[(None, slice(None)) + (None,)*(c.ndim-2)] # Now fix continuity: on the very first interval, take the integration # constant to be zero; on an interval [x_j, x_{j+1}) with j>0, # the integration constant is then equal to the jump of the `bp` at x_j. # The latter is given by the coefficient of B_{n+1, n+1} # *on the previous interval* (other B. polynomials are zero at the breakpoint) # Finally, use the fact that BPs form a partition of unity. c2[:,1:] += np.cumsum(c2[k,:], axis=0)[:-1] return self.construct_fast(c2, x, self.extrapolate, axis=self.axis) def integrate(self, a, b, extrapolate=None): """ Compute a definite integral over a piecewise polynomial. Parameters ---------- a : float Lower integration bound b : float Upper integration bound extrapolate : bool, optional Whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. Defaults to ``self.extrapolate``. Returns ------- array_like Definite integral of the piecewise polynomial over [a, b] """ # XXX: can probably use instead the fact that # \int_0^{1} B_{j, n}(x) \dx = 1/(n+1) ib = self.antiderivative() if extrapolate is not None: ib.extrapolate = extrapolate return ib(b) - ib(a) def extend(self, c, x, right=True): k = max(self.c.shape[0], c.shape[0]) self.c = self._raise_degree(self.c, k - self.c.shape[0]) c = self._raise_degree(c, k - c.shape[0]) return _PPolyBase.extend(self, c, x, right) extend.__doc__ = _PPolyBase.extend.__doc__ @classmethod def from_power_basis(cls, pp, extrapolate=None): """ Construct a piecewise polynomial in Bernstein basis from a power basis polynomial. Parameters ---------- pp : PPoly A piecewise polynomial in the power basis extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Default: True. """ dx = np.diff(pp.x) k = pp.c.shape[0] - 1 # polynomial order rest = (None,)*(pp.c.ndim-2) c = np.zeros_like(pp.c) for a in range(k+1): factor = pp.c[a] / comb(k, k-a) * dx[(slice(None),)+rest]**(k-a) for j in range(k-a, k+1): c[j] += factor * comb(j, k-a) if extrapolate is None: extrapolate = pp.extrapolate return cls.construct_fast(c, pp.x, extrapolate, pp.axis) @classmethod def from_derivatives(cls, xi, yi, orders=None, extrapolate=None): """Construct a piecewise polynomial in the Bernstein basis, compatible with the specified values and derivatives at breakpoints. Parameters ---------- xi : array_like sorted 1D array of x-coordinates yi : array_like or list of array_likes ``yi[i][j]`` is the ``j``-th derivative known at ``xi[i]`` orders : None or int or array_like of ints. Default: None. Specifies the degree of local polynomials. If not None, some derivatives are ignored. extrapolate : bool, optional Whether to extrapolate to ouf-of-bounds points based on first and last intervals, or to return NaNs. Default: True. Notes ----- If ``k`` derivatives are specified at a breakpoint ``x``, the constructed polynomial is exactly ``k`` times continuously differentiable at ``x``, unless the ``order`` is provided explicitly. In the latter case, the smoothness of the polynomial at the breakpoint is controlled by the ``order``. Deduces the number of derivatives to match at each end from ``order`` and the number of derivatives available. If possible it uses the same number of derivatives from each end; if the number is odd it tries to take the extra one from y2. In any case if not enough derivatives are available at one end or another it draws enough to make up the total from the other end. If the order is too high and not enough derivatives are available, an exception is raised. Examples -------- >>> from scipy.interpolate import BPoly >>> BPoly.from_derivatives([0, 1], [[1, 2], [3, 4]]) Creates a polynomial `f(x)` of degree 3, defined on `[0, 1]` such that `f(0) = 1, df/dx(0) = 2, f(1) = 3, df/dx(1) = 4` >>> BPoly.from_derivatives([0, 1, 2], [[0, 1], [0], [2]]) Creates a piecewise polynomial `f(x)`, such that `f(0) = f(1) = 0`, `f(2) = 2`, and `df/dx(0) = 1`. Based on the number of derivatives provided, the order of the local polynomials is 2 on `[0, 1]` and 1 on `[1, 2]`. Notice that no restriction is imposed on the derivatives at `x = 1` and `x = 2`. Indeed, the explicit form of the polynomial is:: f(x) = | x * (1 - x), 0 <= x < 1 | 2 * (x - 1), 1 <= x <= 2 So that f'(1-0) = -1 and f'(1+0) = 2 """ xi = np.asarray(xi) if len(xi) != len(yi): raise ValueError("xi and yi need to have the same length") if np.any(xi[1:] - xi[:1] <= 0): raise ValueError("x coordinates are not in increasing order") # number of intervals m = len(xi) - 1 # global poly order is k-1, local orders are <=k and can vary try: k = max(len(yi[i]) + len(yi[i+1]) for i in range(m)) except TypeError: raise ValueError("Using a 1D array for y? Please .reshape(-1, 1).") if orders is None: orders = [None] * m else: if isinstance(orders, integer_types): orders = [orders] * m k = max(k, max(orders)) if any(o <= 0 for o in orders): raise ValueError("Orders must be positive.") c = [] for i in range(m): y1, y2 = yi[i], yi[i+1] if orders[i] is None: n1, n2 = len(y1), len(y2) else: n = orders[i]+1 n1 = min(n//2, len(y1)) n2 = min(n - n1, len(y2)) n1 = min(n - n2, len(y2)) if n1+n2 != n: raise ValueError("Point %g has %d derivatives, point %g" " has %d derivatives, but order %d requested" % (xi[i], len(y1), xi[i+1], len(y2), orders[i])) if not (n1 <= len(y1) and n2 <= len(y2)): raise ValueError("`order` input incompatible with" " length y1 or y2.") b = BPoly._construct_from_derivatives(xi[i], xi[i+1], y1[:n1], y2[:n2]) if len(b) < k: b = BPoly._raise_degree(b, k - len(b)) c.append(b) c = np.asarray(c) return cls(c.swapaxes(0, 1), xi, extrapolate) @staticmethod def _construct_from_derivatives(xa, xb, ya, yb): """Compute the coefficients of a polynomial in the Bernstein basis given the values and derivatives at the edges. Return the coefficients of a polynomial in the Bernstein basis defined on `[xa, xb]` and having the values and derivatives at the endpoints ``xa`` and ``xb`` as specified by ``ya`` and ``yb``. The polynomial constructed is of the minimal possible degree, i.e., if the lengths of ``ya`` and ``yb`` are ``na`` and ``nb``, the degree of the polynomial is ``na + nb - 1``. Parameters ---------- xa : float Left-hand end point of the interval xb : float Right-hand end point of the interval ya : array_like Derivatives at ``xa``. ``ya[0]`` is the value of the function, and ``ya[i]`` for ``i > 0`` is the value of the ``i``-th derivative. yb : array_like Derivatives at ``xb``. Returns ------- array coefficient array of a polynomial having specified derivatives Notes ----- This uses several facts from life of Bernstein basis functions. First of all, .. math:: b'_{a, n} = n (b_{a-1, n-1} - b_{a, n-1}) If B(x) is a linear combination of the form .. math:: B(x) = \sum_{a=0}^{n} c_a b_{a, n}, then :math: B'(x) = n \sum_{a=0}^{n-1} (c_{a+1} - c_{a}) b_{a, n-1}. Iterating the latter one, one finds for the q-th derivative .. math:: B^{q}(x) = n!/(n-q)! \sum_{a=0}^{n-q} Q_a b_{a, n-q}, with .. math:: Q_a = \sum_{j=0}^{q} (-)^{j+q} comb(q, j) c_{j+a} This way, only `a=0` contributes to :math: `B^{q}(x = xa)`, and `c_q` are found one by one by iterating `q = 0, ..., na`. At `x = xb` it's the same with `a = n - q`. """ ya, yb = np.asarray(ya), np.asarray(yb) if ya.shape[1:] != yb.shape[1:]: raise ValueError('ya and yb have incompatible dimensions.') dta, dtb = ya.dtype, yb.dtype if (np.issubdtype(dta, np.complexfloating) or np.issubdtype(dtb, np.complexfloating)): dt = np.complex_ else: dt = np.float_ na, nb = len(ya), len(yb) n = na + nb c = np.empty((na+nb,) + ya.shape[1:], dtype=dt) # compute coefficients of a polynomial degree na+nb-1 # walk left-to-right for q in range(0, na): c[q] = ya[q] / spec.poch(n - q, q) * (xb - xa)**q for j in range(0, q): c[q] -= (-1)**(j+q) * comb(q, j) * c[j] # now walk right-to-left for q in range(0, nb): c[-q-1] = yb[q] / spec.poch(n - q, q) * (-1)**q * (xb - xa)**q for j in range(0, q): c[-q-1] -= (-1)**(j+1) * comb(q, j+1) * c[-q+j] return c @staticmethod def _raise_degree(c, d): """Raise a degree of a polynomial in the Bernstein basis. Given the coefficients of a polynomial degree `k`, return (the coefficients of) the equivalent polynomial of degree `k+d`. Parameters ---------- c : array_like coefficient array, 1D d : integer Returns ------- array coefficient array, 1D array of length `c.shape[0] + d` Notes ----- This uses the fact that a Bernstein polynomial `b_{a, k}` can be identically represented as a linear combination of polynomials of a higher degree `k+d`: .. math:: b_{a, k} = comb(k, a) \sum_{j=0}^{d} b_{a+j, k+d} \ comb(d, j) / comb(k+d, a+j) """ if d == 0: return c k = c.shape[0] - 1 out = np.zeros((c.shape[0] + d,) + c.shape[1:], dtype=c.dtype) for a in range(c.shape[0]): f = c[a] * comb(k, a) for j in range(d+1): out[a+j] += f * comb(d, j) / comb(k+d, a+j) return out class RegularGridInterpolator(object): """ Interpolation on a regular grid in arbitrary dimensions The data must be defined on a regular grid; the grid spacing however may be uneven. Linear and nearest-neighbour interpolation are supported. After setting up the interpolator object, the interpolation method (*linear* or *nearest*) may be chosen at each evaluation. Parameters ---------- points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, ) The points defining the regular grid in n dimensions. values : array_like, shape (m1, ..., mn, ...) The data on the regular grid in n dimensions. method : str, optional The method of interpolation to perform. Supported are "linear" and "nearest". This parameter will become the default for the object's ``__call__`` method. Default is "linear". bounds_error : bool, optional If True, when interpolated values are requested outside of the domain of the input data, a ValueError is raised. If False, then `fill_value` is used. fill_value : number, optional If provided, the value to use for points outside of the interpolation domain. If None, values outside the domain are extrapolated. Methods ------- __call__ Notes ----- Contrary to LinearNDInterpolator and NearestNDInterpolator, this class avoids expensive triangulation of the input data by taking advantage of the regular grid structure. .. versionadded:: 0.14 Examples -------- Evaluate a simple example function on the points of a 3D grid: >>> from scipy.interpolate import RegularGridInterpolator >>> def f(x,y,z): ... return 2 * x**3 + 3 * y**2 - z >>> x = np.linspace(1, 4, 11) >>> y = np.linspace(4, 7, 22) >>> z = np.linspace(7, 9, 33) >>> data = f(*np.meshgrid(x, y, z, indexing='ij', sparse=True)) ``data`` is now a 3D array with ``data[i,j,k] = f(x[i], y[j], z[k])``. Next, define an interpolating function from this data: >>> my_interpolating_function = RegularGridInterpolator((x, y, z), data) Evaluate the interpolating function at the two points ``(x,y,z) = (2.1, 6.2, 8.3)`` and ``(3.3, 5.2, 7.1)``: >>> pts = np.array([[2.1, 6.2, 8.3], [3.3, 5.2, 7.1]]) >>> my_interpolating_function(pts) array([ 125.80469388, 146.30069388]) which is indeed a close approximation to ``[f(2.1, 6.2, 8.3), f(3.3, 5.2, 7.1)]``. See also -------- NearestNDInterpolator : Nearest neighbour interpolation on unstructured data in N dimensions LinearNDInterpolator : Piecewise linear interpolant on unstructured data in N dimensions References ---------- .. [1] Python package *regulargrid* by Johannes Buchner, see https://pypi.python.org/pypi/regulargrid/ .. [2] Trilinear interpolation. (2013, January 17). In Wikipedia, The Free Encyclopedia. Retrieved 27 Feb 2013 01:28. http://en.wikipedia.org/w/index.php?title=Trilinear_interpolation&oldid=533448871 .. [3] Weiser, Alan, and Sergio E. Zarantonello. "A note on piecewise linear and multilinear table interpolation in many dimensions." MATH. COMPUT. 50.181 (1988): 189-196. http://www.ams.org/journals/mcom/1988-50-181/S0025-5718-1988-0917826-0/S0025-5718-1988-0917826-0.pdf """ # this class is based on code originally programmed by Johannes Buchner, # see https://github.com/JohannesBuchner/regulargrid def __init__(self, points, values, method="linear", bounds_error=True, fill_value=np.nan): if method not in ["linear", "nearest"]: raise ValueError("Method '%s' is not defined" % method) self.method = method self.bounds_error = bounds_error if not hasattr(values, 'ndim'): # allow reasonable duck-typed values values = np.asarray(values) if len(points) > values.ndim: raise ValueError("There are %d point arrays, but values has %d " "dimensions" % (len(points), values.ndim)) if hasattr(values, 'dtype') and hasattr(values, 'astype'): if not np.issubdtype(values.dtype, np.inexact): values = values.astype(float) self.fill_value = fill_value if fill_value is not None: fill_value_dtype = np.asarray(fill_value).dtype if (hasattr(values, 'dtype') and not np.can_cast(fill_value_dtype, values.dtype, casting='same_kind')): raise ValueError("fill_value must be either 'None' or " "of a type compatible with values") for i, p in enumerate(points): if not np.all(np.diff(p) > 0.): raise ValueError("The points in dimension %d must be strictly " "ascending" % i) if not np.asarray(p).ndim == 1: raise ValueError("The points in dimension %d must be " "1-dimensional" % i) if not values.shape[i] == len(p): raise ValueError("There are %d points and %d values in " "dimension %d" % (len(p), values.shape[i], i)) self.grid = tuple([np.asarray(p) for p in points]) self.values = values def __call__(self, xi, method=None): """ Interpolation at coordinates Parameters ---------- xi : ndarray of shape (..., ndim) The coordinates to sample the gridded data at method : str The method of interpolation to perform. Supported are "linear" and "nearest". """ method = self.method if method is None else method if method not in ["linear", "nearest"]: raise ValueError("Method '%s' is not defined" % method) ndim = len(self.grid) xi = _ndim_coords_from_arrays(xi, ndim=ndim) if xi.shape[-1] != len(self.grid): raise ValueError("The requested sample points xi have dimension " "%d, but this RegularGridInterpolator has " "dimension %d" % (xi.shape[1], ndim)) xi_shape = xi.shape xi = xi.reshape(-1, xi_shape[-1]) if self.bounds_error: for i, p in enumerate(xi.T): if not np.logical_and(np.all(self.grid[i][0] <= p), np.all(p <= self.grid[i][-1])): raise ValueError("One of the requested xi is out of bounds " "in dimension %d" % i) indices, norm_distances, out_of_bounds = self._find_indices(xi.T) if method == "linear": result = self._evaluate_linear(indices, norm_distances, out_of_bounds) elif method == "nearest": result = self._evaluate_nearest(indices, norm_distances, out_of_bounds) if not self.bounds_error and self.fill_value is not None: result[out_of_bounds] = self.fill_value return result.reshape(xi_shape[:-1] + self.values.shape[ndim:]) def _evaluate_linear(self, indices, norm_distances, out_of_bounds): # slice for broadcasting over trailing dimensions in self.values vslice = (slice(None),) + (None,)*(self.values.ndim - len(indices)) # find relevant values # each i and i+1 represents a edge edges = itertools.product(*[[i, i + 1] for i in indices]) values = 0. for edge_indices in edges: weight = 1. for ei, i, yi in zip(edge_indices, indices, norm_distances): weight *= np.where(ei == i, 1 - yi, yi) values += np.asarray(self.values[edge_indices]) * weight[vslice] return values def _evaluate_nearest(self, indices, norm_distances, out_of_bounds): idx_res = [] for i, yi in zip(indices, norm_distances): idx_res.append(np.where(yi <= .5, i, i + 1)) return self.values[idx_res] def _find_indices(self, xi): # find relevant edges between which xi are situated indices = [] # compute distance to lower edge in unity units norm_distances = [] # check for out of bounds xi out_of_bounds = np.zeros((xi.shape[1]), dtype=bool) # iterate through dimensions for x, grid in zip(xi, self.grid): i = np.searchsorted(grid, x) - 1 i[i < 0] = 0 i[i > grid.size - 2] = grid.size - 2 indices.append(i) norm_distances.append((x - grid[i]) / (grid[i + 1] - grid[i])) if not self.bounds_error: out_of_bounds += x < grid[0] out_of_bounds += x > grid[-1] return indices, norm_distances, out_of_bounds def interpn(points, values, xi, method="linear", bounds_error=True, fill_value=np.nan): """ Multidimensional interpolation on regular grids. Parameters ---------- points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, ) The points defining the regular grid in n dimensions. values : array_like, shape (m1, ..., mn, ...) The data on the regular grid in n dimensions. xi : ndarray of shape (..., ndim) The coordinates to sample the gridded data at method : str, optional The method of interpolation to perform. Supported are "linear" and "nearest", and "splinef2d". "splinef2d" is only supported for 2-dimensional data. bounds_error : bool, optional If True, when interpolated values are requested outside of the domain of the input data, a ValueError is raised. If False, then `fill_value` is used. fill_value : number, optional If provided, the value to use for points outside of the interpolation domain. If None, values outside the domain are extrapolated. Extrapolation is not supported by method "splinef2d". Returns ------- values_x : ndarray, shape xi.shape[:-1] + values.shape[ndim:] Interpolated values at input coordinates. Notes ----- .. versionadded:: 0.14 See also -------- NearestNDInterpolator : Nearest neighbour interpolation on unstructured data in N dimensions LinearNDInterpolator : Piecewise linear interpolant on unstructured data in N dimensions RegularGridInterpolator : Linear and nearest-neighbor Interpolation on a regular grid in arbitrary dimensions RectBivariateSpline : Bivariate spline approximation over a rectangular mesh """ # sanity check 'method' kwarg if method not in ["linear", "nearest", "splinef2d"]: raise ValueError("interpn only understands the methods 'linear', " "'nearest', and 'splinef2d'. You provided %s." % method) if not hasattr(values, 'ndim'): values = np.asarray(values) ndim = values.ndim if ndim > 2 and method == "splinef2d": raise ValueError("The method spline2fd can only be used for " "2-dimensional input data") if not bounds_error and fill_value is None and method == "splinef2d": raise ValueError("The method spline2fd does not support extrapolation.") # sanity check consistency of input dimensions if len(points) > ndim: raise ValueError("There are %d point arrays, but values has %d " "dimensions" % (len(points), ndim)) if len(points) != ndim and method == 'splinef2d': raise ValueError("The method spline2fd can only be used for " "scalar data with one point per coordinate") # sanity check input grid for i, p in enumerate(points): if not np.all(np.diff(p) > 0.): raise ValueError("The points in dimension %d must be strictly " "ascending" % i) if not np.asarray(p).ndim == 1: raise ValueError("The points in dimension %d must be " "1-dimensional" % i) if not values.shape[i] == len(p): raise ValueError("There are %d points and %d values in " "dimension %d" % (len(p), values.shape[i], i)) grid = tuple([np.asarray(p) for p in points]) # sanity check requested xi xi = _ndim_coords_from_arrays(xi, ndim=len(grid)) if xi.shape[-1] != len(grid): raise ValueError("The requested sample points xi have dimension " "%d, but this RegularGridInterpolator has " "dimension %d" % (xi.shape[1], len(grid))) for i, p in enumerate(xi.T): if bounds_error and not np.logical_and(np.all(grid[i][0] <= p), np.all(p <= grid[i][-1])): raise ValueError("One of the requested xi is out of bounds " "in dimension %d" % i) # perform interpolation if method == "linear": interp = RegularGridInterpolator(points, values, method="linear", bounds_error=bounds_error, fill_value=fill_value) return interp(xi) elif method == "nearest": interp = RegularGridInterpolator(points, values, method="nearest", bounds_error=bounds_error, fill_value=fill_value) return interp(xi) elif method == "splinef2d": xi_shape = xi.shape xi = xi.reshape(-1, xi.shape[-1]) # RectBivariateSpline doesn't support fill_value; we need to wrap here idx_valid = np.all((grid[0][0] <= xi[:, 0], xi[:, 0] <= grid[0][-1], grid[1][0] <= xi[:, 1], xi[:, 1] <= grid[1][-1]), axis=0) result = np.empty_like(xi[:, 0]) # make a copy of values for RectBivariateSpline interp = RectBivariateSpline(points[0], points[1], values[:]) result[idx_valid] = interp.ev(xi[idx_valid, 0], xi[idx_valid, 1]) result[np.logical_not(idx_valid)] = fill_value return result.reshape(xi_shape[:-1]) # backward compatibility wrapper class ppform(PPoly): """ Deprecated piecewise polynomial class. New code should use the `PPoly` class instead. """ def __init__(self, coeffs, breaks, fill=0.0, sort=False): warnings.warn("ppform is deprecated -- use PPoly instead", category=DeprecationWarning) if sort: breaks = np.sort(breaks) else: breaks = np.asarray(breaks) PPoly.__init__(self, coeffs, breaks) self.coeffs = self.c self.breaks = self.x self.K = self.coeffs.shape[0] self.fill = fill self.a = self.breaks[0] self.b = self.breaks[-1] def __call__(self, x): return PPoly.__call__(self, x, 0, False) def _evaluate(self, x, nu, extrapolate, out): PPoly._evaluate(self, x, nu, extrapolate, out) out[~((x >= self.a) & (x <= self.b))] = self.fill return out @classmethod def fromspline(cls, xk, cvals, order, fill=0.0): # Note: this spline representation is incompatible with FITPACK N = len(xk)-1 sivals = np.empty((order+1, N), dtype=float) for m in xrange(order, -1, -1): fact = spec.gamma(m+1) res = _fitpack._bspleval(xk[:-1], xk, cvals, order, m) res /= fact sivals[order-m, :] = res return cls(sivals, xk, fill=fill) def _dot0(a, b): """Similar to numpy.dot, but sum over last axis of a and 1st axis of b""" if b.ndim <= 2: return dot(a, b) else: axes = list(range(b.ndim)) axes.insert(-1, 0) axes.pop(0) return dot(a, b.transpose(axes)) def _find_smoothest(xk, yk, order, conds=None, B=None): # construct Bmatrix, and Jmatrix # e = J*c # minimize norm(e,2) given B*c=yk # if desired B can be given # conds is ignored N = len(xk)-1 K = order if B is None: B = _fitpack._bsplmat(order, xk) J = _fitpack._bspldismat(order, xk) u, s, vh = scipy.linalg.svd(B) ind = K-1 V2 = vh[-ind:,:].T V1 = vh[:-ind,:].T A = dot(J.T,J) tmp = dot(V2.T,A) Q = dot(tmp,V2) p = scipy.linalg.solve(Q, tmp) tmp = dot(V2,p) tmp = np.eye(N+K) - tmp tmp = dot(tmp,V1) tmp = dot(tmp,np.diag(1.0/s)) tmp = dot(tmp,u.T) return _dot0(tmp, yk) def _setdiag(a, k, v): if not a.ndim == 2: raise ValueError("Input array should be 2-D.") M,N = a.shape if k > 0: start = k num = N - k else: num = M + k start = abs(k)*N end = start + num*(N+1)-1 a.flat[start:end:(N+1)] = v # Return the spline that minimizes the dis-continuity of the # "order-th" derivative; for order >= 2. def _find_smoothest2(xk, yk): N = len(xk) - 1 Np1 = N + 1 # find pseudo-inverse of B directly. Bd = np.empty((Np1, N)) for k in range(-N,N): if (k < 0): l = np.arange(-k, Np1) v = (l+k+1) if ((k+1) % 2): v = -v else: l = np.arange(k,N) v = N - l if ((k % 2)): v = -v _setdiag(Bd, k, v) Bd /= (Np1) V2 = np.ones((Np1,)) V2[1::2] = -1 V2 /= math.sqrt(Np1) dk = np.diff(xk) b = 2*np.diff(yk, axis=0)/dk J = np.zeros((N-1,N+1)) idk = 1.0/dk _setdiag(J,0,idk[:-1]) _setdiag(J,1,-idk[1:]-idk[:-1]) _setdiag(J,2,idk[1:]) A = dot(J.T,J) val = dot(V2,dot(A,V2)) res1 = dot(np.outer(V2,V2)/val,A) mk = dot(np.eye(Np1)-res1, _dot0(Bd,b)) return mk def _get_spline2_Bb(xk, yk, kind, conds): Np1 = len(xk) dk = xk[1:]-xk[:-1] if kind == 'not-a-knot': # use banded-solver nlu = (1,1) B = ones((3,Np1)) alpha = 2*(yk[1:]-yk[:-1])/dk zrs = np.zeros((1,)+yk.shape[1:]) row = (Np1-1)//2 b = np.concatenate((alpha[:row],zrs,alpha[row:]),axis=0) B[0,row+2:] = 0 B[2,:(row-1)] = 0 B[0,row+1] = dk[row-1] B[1,row] = -dk[row]-dk[row-1] B[2,row-1] = dk[row] return B, b, None, nlu else: raise NotImplementedError("quadratic %s is not available" % kind) def _get_spline3_Bb(xk, yk, kind, conds): # internal function to compute different tri-diagonal system # depending on the kind of spline requested. # conds is only used for 'second' and 'first' Np1 = len(xk) if kind in ['natural', 'second']: if kind == 'natural': m0, mN = 0.0, 0.0 else: m0, mN = conds # the matrix to invert is (N-1,N-1) # use banded solver beta = 2*(xk[2:]-xk[:-2]) alpha = xk[1:]-xk[:-1] nlu = (1,1) B = np.empty((3,Np1-2)) B[0,1:] = alpha[2:] B[1,:] = beta B[2,:-1] = alpha[1:-1] dyk = yk[1:]-yk[:-1] b = (dyk[1:]/alpha[1:] - dyk[:-1]/alpha[:-1]) b *= 6 b[0] -= m0 b[-1] -= mN def append_func(mk): # put m0 and mN into the correct shape for # concatenation ma = array(m0,copy=0,ndmin=yk.ndim) mb = array(mN,copy=0,ndmin=yk.ndim) if ma.shape[1:] != yk.shape[1:]: ma = ma*(ones(yk.shape[1:])[np.newaxis,...]) if mb.shape[1:] != yk.shape[1:]: mb = mb*(ones(yk.shape[1:])[np.newaxis,...]) mk = np.concatenate((ma,mk),axis=0) mk = np.concatenate((mk,mb),axis=0) return mk return B, b, append_func, nlu elif kind in ['clamped', 'endslope', 'first', 'not-a-knot', 'runout', 'parabolic']: if kind == 'endslope': # match slope of lagrange interpolating polynomial of # order 3 at end-points. x0,x1,x2,x3 = xk[:4] sl_0 = (1./(x0-x1)+1./(x0-x2)+1./(x0-x3))*yk[0] sl_0 += (x0-x2)*(x0-x3)/((x1-x0)*(x1-x2)*(x1-x3))*yk[1] sl_0 += (x0-x1)*(x0-x3)/((x2-x0)*(x2-x1)*(x3-x2))*yk[2] sl_0 += (x0-x1)*(x0-x2)/((x3-x0)*(x3-x1)*(x3-x2))*yk[3] xN3,xN2,xN1,xN0 = xk[-4:] sl_N = (1./(xN0-xN1)+1./(xN0-xN2)+1./(xN0-xN3))*yk[-1] sl_N += (xN0-xN2)*(xN0-xN3)/((xN1-xN0)*(xN1-xN2)*(xN1-xN3))*yk[-2] sl_N += (xN0-xN1)*(xN0-xN3)/((xN2-xN0)*(xN2-xN1)*(xN3-xN2))*yk[-3] sl_N += (xN0-xN1)*(xN0-xN2)/((xN3-xN0)*(xN3-xN1)*(xN3-xN2))*yk[-4] elif kind == 'clamped': sl_0, sl_N = 0.0, 0.0 elif kind == 'first': sl_0, sl_N = conds # Now set up the (N+1)x(N+1) system of equations beta = np.r_[0,2*(xk[2:]-xk[:-2]),0] alpha = xk[1:]-xk[:-1] gamma = np.r_[0,alpha[1:]] B = np.diag(alpha,k=-1) + np.diag(beta) + np.diag(gamma,k=1) d1 = alpha[0] dN = alpha[-1] if kind == 'not-a-knot': d2 = alpha[1] dN1 = alpha[-2] B[0,:3] = [d2,-d1-d2,d1] B[-1,-3:] = [dN,-dN1-dN,dN1] elif kind == 'runout': B[0,:3] = [1,-2,1] B[-1,-3:] = [1,-2,1] elif kind == 'parabolic': B[0,:2] = [1,-1] B[-1,-2:] = [-1,1] elif kind == 'periodic': raise NotImplementedError elif kind == 'symmetric': raise NotImplementedError else: B[0,:2] = [2*d1,d1] B[-1,-2:] = [dN,2*dN] # Set up RHS (b) b = np.empty((Np1,)+yk.shape[1:]) dyk = (yk[1:]-yk[:-1])*1.0 if kind in ['not-a-knot', 'runout', 'parabolic']: b[0] = b[-1] = 0.0 elif kind == 'periodic': raise NotImplementedError elif kind == 'symmetric': raise NotImplementedError else: b[0] = (dyk[0]/d1 - sl_0) b[-1] = -(dyk[-1]/dN - sl_N) b[1:-1,...] = (dyk[1:]/alpha[1:]-dyk[:-1]/alpha[:-1]) b *= 6.0 return B, b, None, None else: raise ValueError("%s not supported" % kind) # conds is a tuple of an array and a vector # giving the left-hand and the right-hand side # of the additional equations to add to B def _find_user(xk, yk, order, conds, B): lh = conds[0] rh = conds[1] B = np.concatenate((B, lh), axis=0) w = np.concatenate((yk, rh), axis=0) M, N = B.shape if (M > N): raise ValueError("over-specification of conditions") elif (M < N): return _find_smoothest(xk, yk, order, None, B) else: return scipy.linalg.solve(B, w) # If conds is None, then use the not_a_knot condition # at K-1 farthest separated points in the interval def _find_not_a_knot(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) # If conds is None, then ensure zero-valued second # derivative at K-1 farthest separated points def _find_natural(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) # If conds is None, then ensure zero-valued first # derivative at K-1 farthest separated points def _find_clamped(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) def _find_fixed(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) # If conds is None, then use coefficient periodicity # If conds is 'function' then use function periodicity def _find_periodic(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) # Doesn't use conds def _find_symmetric(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) # conds is a dictionary with multiple values def _find_mixed(xk, yk, order, conds, B): raise NotImplementedError return _find_user(xk, yk, order, conds, B) def splmake(xk, yk, order=3, kind='smoothest', conds=None): """ Return a representation of a spline given data-points at internal knots Parameters ---------- xk : array_like The input array of x values of rank 1 yk : array_like The input array of y values of rank N. `yk` can be an N-d array to represent more than one curve, through the same `xk` points. The first dimension is assumed to be the interpolating dimension and is the same length of `xk`. order : int, optional Order of the spline kind : str, optional Can be 'smoothest', 'not_a_knot', 'fixed', 'clamped', 'natural', 'periodic', 'symmetric', 'user', 'mixed' and it is ignored if order < 2 conds : optional Conds Returns ------- splmake : tuple Return a (`xk`, `cvals`, `k`) representation of a spline given data-points where the (internal) knots are at the data-points. """ yk = np.asanyarray(yk) order = int(order) if order < 0: raise ValueError("order must not be negative") if order == 0: return xk, yk[:-1], order elif order == 1: return xk, yk, order try: func = eval('_find_%s' % kind) except: raise NotImplementedError # the constraint matrix B = _fitpack._bsplmat(order, xk) coefs = func(xk, yk, order, conds, B) return xk, coefs, order def spleval(xck, xnew, deriv=0): """ Evaluate a fixed spline represented by the given tuple at the new x-values The `xj` values are the interior knot points. The approximation region is `xj[0]` to `xj[-1]`. If N+1 is the length of `xj`, then `cvals` should have length N+k where `k` is the order of the spline. Parameters ---------- (xj, cvals, k) : tuple Parameters that define the fixed spline xj : array_like Interior knot points cvals : array_like Curvature k : int Order of the spline xnew : array_like Locations to calculate spline deriv : int Deriv Returns ------- spleval : ndarray If `cvals` represents more than one curve (`cvals.ndim` > 1) and/or `xnew` is N-d, then the result is `xnew.shape` + `cvals.shape[1:]` providing the interpolation of multiple curves. Notes ----- Internally, an additional `k`-1 knot points are added on either side of the spline. """ (xj,cvals,k) = xck oldshape = np.shape(xnew) xx = np.ravel(xnew) sh = cvals.shape[1:] res = np.empty(xx.shape + sh, dtype=cvals.dtype) for index in np.ndindex(*sh): sl = (slice(None),)+index if issubclass(cvals.dtype.type, np.complexfloating): res[sl].real = _fitpack._bspleval(xx,xj,cvals.real[sl],k,deriv) res[sl].imag = _fitpack._bspleval(xx,xj,cvals.imag[sl],k,deriv) else: res[sl] = _fitpack._bspleval(xx,xj,cvals[sl],k,deriv) res.shape = oldshape + sh return res def spltopp(xk, cvals, k): """Return a piece-wise polynomial object from a fixed-spline tuple. """ return ppform.fromspline(xk, cvals, k) def spline(xk, yk, xnew, order=3, kind='smoothest', conds=None): """ Interpolate a curve at new points using a spline fit Parameters ---------- xk, yk : array_like The x and y values that define the curve. xnew : array_like The x values where spline should estimate the y values. order : int Default is 3. kind : string One of {'smoothest'} conds : Don't know Don't know Returns ------- spline : ndarray An array of y values; the spline evaluated at the positions `xnew`. """ return spleval(splmake(xk,yk,order=order,kind=kind,conds=conds),xnew)
bsd-3-clause
pnedunuri/scikit-learn
examples/applications/plot_model_complexity_influence.py
323
6372
""" ========================== Model Complexity Influence ========================== Demonstrate how model complexity influences both prediction accuracy and computational performance. The dataset is the Boston Housing dataset (resp. 20 Newsgroups) for regression (resp. classification). For each class of models we make the model complexity vary through the choice of relevant model parameters and measure the influence on both computational performance (latency) and predictive power (MSE or Hamming Loss). """ print(__doc__) # Author: Eustache Diemert <eustache@diemert.fr> # License: BSD 3 clause import time import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.parasite_axes import host_subplot from mpl_toolkits.axisartist.axislines import Axes from scipy.sparse.csr import csr_matrix from sklearn import datasets from sklearn.utils import shuffle from sklearn.metrics import mean_squared_error from sklearn.svm.classes import NuSVR from sklearn.ensemble.gradient_boosting import GradientBoostingRegressor from sklearn.linear_model.stochastic_gradient import SGDClassifier from sklearn.metrics import hamming_loss ############################################################################### # Routines # initialize random generator np.random.seed(0) def generate_data(case, sparse=False): """Generate regression/classification data.""" bunch = None if case == 'regression': bunch = datasets.load_boston() elif case == 'classification': bunch = datasets.fetch_20newsgroups_vectorized(subset='all') X, y = shuffle(bunch.data, bunch.target) offset = int(X.shape[0] * 0.8) X_train, y_train = X[:offset], y[:offset] X_test, y_test = X[offset:], y[offset:] if sparse: X_train = csr_matrix(X_train) X_test = csr_matrix(X_test) else: X_train = np.array(X_train) X_test = np.array(X_test) y_test = np.array(y_test) y_train = np.array(y_train) data = {'X_train': X_train, 'X_test': X_test, 'y_train': y_train, 'y_test': y_test} return data def benchmark_influence(conf): """ Benchmark influence of :changing_param: on both MSE and latency. """ prediction_times = [] prediction_powers = [] complexities = [] for param_value in conf['changing_param_values']: conf['tuned_params'][conf['changing_param']] = param_value estimator = conf['estimator'](**conf['tuned_params']) print("Benchmarking %s" % estimator) estimator.fit(conf['data']['X_train'], conf['data']['y_train']) conf['postfit_hook'](estimator) complexity = conf['complexity_computer'](estimator) complexities.append(complexity) start_time = time.time() for _ in range(conf['n_samples']): y_pred = estimator.predict(conf['data']['X_test']) elapsed_time = (time.time() - start_time) / float(conf['n_samples']) prediction_times.append(elapsed_time) pred_score = conf['prediction_performance_computer']( conf['data']['y_test'], y_pred) prediction_powers.append(pred_score) print("Complexity: %d | %s: %.4f | Pred. Time: %fs\n" % ( complexity, conf['prediction_performance_label'], pred_score, elapsed_time)) return prediction_powers, prediction_times, complexities def plot_influence(conf, mse_values, prediction_times, complexities): """ Plot influence of model complexity on both accuracy and latency. """ plt.figure(figsize=(12, 6)) host = host_subplot(111, axes_class=Axes) plt.subplots_adjust(right=0.75) par1 = host.twinx() host.set_xlabel('Model Complexity (%s)' % conf['complexity_label']) y1_label = conf['prediction_performance_label'] y2_label = "Time (s)" host.set_ylabel(y1_label) par1.set_ylabel(y2_label) p1, = host.plot(complexities, mse_values, 'b-', label="prediction error") p2, = par1.plot(complexities, prediction_times, 'r-', label="latency") host.legend(loc='upper right') host.axis["left"].label.set_color(p1.get_color()) par1.axis["right"].label.set_color(p2.get_color()) plt.title('Influence of Model Complexity - %s' % conf['estimator'].__name__) plt.show() def _count_nonzero_coefficients(estimator): a = estimator.coef_.toarray() return np.count_nonzero(a) ############################################################################### # main code regression_data = generate_data('regression') classification_data = generate_data('classification', sparse=True) configurations = [ {'estimator': SGDClassifier, 'tuned_params': {'penalty': 'elasticnet', 'alpha': 0.001, 'loss': 'modified_huber', 'fit_intercept': True}, 'changing_param': 'l1_ratio', 'changing_param_values': [0.25, 0.5, 0.75, 0.9], 'complexity_label': 'non_zero coefficients', 'complexity_computer': _count_nonzero_coefficients, 'prediction_performance_computer': hamming_loss, 'prediction_performance_label': 'Hamming Loss (Misclassification Ratio)', 'postfit_hook': lambda x: x.sparsify(), 'data': classification_data, 'n_samples': 30}, {'estimator': NuSVR, 'tuned_params': {'C': 1e3, 'gamma': 2 ** -15}, 'changing_param': 'nu', 'changing_param_values': [0.1, 0.25, 0.5, 0.75, 0.9], 'complexity_label': 'n_support_vectors', 'complexity_computer': lambda x: len(x.support_vectors_), 'data': regression_data, 'postfit_hook': lambda x: x, 'prediction_performance_computer': mean_squared_error, 'prediction_performance_label': 'MSE', 'n_samples': 30}, {'estimator': GradientBoostingRegressor, 'tuned_params': {'loss': 'ls'}, 'changing_param': 'n_estimators', 'changing_param_values': [10, 50, 100, 200, 500], 'complexity_label': 'n_trees', 'complexity_computer': lambda x: x.n_estimators, 'data': regression_data, 'postfit_hook': lambda x: x, 'prediction_performance_computer': mean_squared_error, 'prediction_performance_label': 'MSE', 'n_samples': 30}, ] for conf in configurations: prediction_performances, prediction_times, complexities = \ benchmark_influence(conf) plot_influence(conf, prediction_performances, prediction_times, complexities)
bsd-3-clause
liu-jc/reinforcement-learning
lib/plotting.py
4
3457
import matplotlib import numpy as np import pandas as pd from collections import namedtuple from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D EpisodeStats = namedtuple("Stats",["episode_lengths", "episode_rewards"]) def plot_cost_to_go_mountain_car(env, estimator, num_tiles=20): x = np.linspace(env.observation_space.low[0], env.observation_space.high[0], num=num_tiles) y = np.linspace(env.observation_space.low[1], env.observation_space.high[1], num=num_tiles) X, Y = np.meshgrid(x, y) Z = np.apply_along_axis(lambda _: -np.max(estimator.predict(_)), 2, np.dstack([X, Y])) fig = plt.figure(figsize=(10, 5)) ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0) ax.set_xlabel('Position') ax.set_ylabel('Velocity') ax.set_zlabel('Value') ax.set_title("Mountain \"Cost To Go\" Function") fig.colorbar(surf) plt.show() def plot_value_function(V, title="Value Function"): """ Plots the value function as a surface plot. """ min_x = min(k[0] for k in V.keys()) max_x = max(k[0] for k in V.keys()) min_y = min(k[1] for k in V.keys()) max_y = max(k[1] for k in V.keys()) x_range = np.arange(min_x, max_x + 1) y_range = np.arange(min_y, max_y + 1) X, Y = np.meshgrid(x_range, y_range) # Find value for all (x, y) coordinates Z_noace = np.apply_along_axis(lambda _: V[(_[0], _[1], False)], 2, np.dstack([X, Y])) Z_ace = np.apply_along_axis(lambda _: V[(_[0], _[1], True)], 2, np.dstack([X, Y])) def plot_surface(X, Y, Z, title): fig = plt.figure(figsize=(20, 10)) ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0) ax.set_xlabel('Player Sum') ax.set_ylabel('Dealer Showing') ax.set_zlabel('Value') ax.set_title(title) ax.view_init(ax.elev, -120) fig.colorbar(surf) plt.show() plot_surface(X, Y, Z_noace, "{} (No Usable Ace)".format(title)) plot_surface(X, Y, Z_ace, "{} (Usable Ace)".format(title)) def plot_episode_stats(stats, smoothing_window=10, noshow=False): # Plot the episode length over time fig1 = plt.figure(figsize=(10,5)) plt.plot(stats.episode_lengths) plt.xlabel("Episode") plt.ylabel("Episode Length") plt.title("Episode Length over Time") if noshow: plt.close(fig1) else: plt.show(fig1) # Plot the episode reward over time fig2 = plt.figure(figsize=(10,5)) rewards_smoothed = pd.Series(stats.episode_rewards).rolling(smoothing_window, min_periods=smoothing_window).mean() plt.plot(rewards_smoothed) plt.xlabel("Episode") plt.ylabel("Episode Reward (Smoothed)") plt.title("Episode Reward over Time (Smoothed over window size {})".format(smoothing_window)) if noshow: plt.close(fig2) else: plt.show(fig2) # Plot time steps and episode number fig3 = plt.figure(figsize=(10,5)) plt.plot(np.cumsum(stats.episode_lengths), np.arange(len(stats.episode_lengths))) plt.xlabel("Time Steps") plt.ylabel("Episode") plt.title("Episode per time step") if noshow: plt.close(fig3) else: plt.show(fig3) return fig1, fig2, fig3
mit
ifuding/Kaggle
ADDC/Code/BarisKanber.py
3
14070
""" A non-blending lightGBM model that incorporates portions and ideas from various public kernels This kernel gives LB: 0.977 when the parameter 'debug' below is set to 0 but this implementation requires a machine with ~32 GB of memory """ import pandas as pd import time import numpy as np from sklearn.cross_validation import train_test_split import lightgbm as lgb import gc import matplotlib.pyplot as plt import os debug=1 if debug: print('*** debug parameter set: this is a test run for debugging purposes ***') def lgb_modelfit_nocv(params, dtrain, dvalid, predictors, target='target', objective='binary', metrics='auc', feval=None, early_stopping_rounds=20, num_boost_round=3000, verbose_eval=10, categorical_features=None): lgb_params = { 'boosting_type': 'gbdt', 'objective': objective, 'metric':metrics, 'learning_rate': 0.2, #'is_unbalance': 'true', #because training data is unbalance (replaced with scale_pos_weight) 'num_leaves': 31, # we should let it be smaller than 2^(max_depth) 'max_depth': -1, # -1 means no limit 'min_child_samples': 20, # Minimum number of data need in a child(min_data_in_leaf) 'max_bin': 255, # Number of bucketed bin for feature values 'subsample': 0.6, # Subsample ratio of the training instance. 'subsample_freq': 0, # frequence of subsample, <=0 means no enable 'colsample_bytree': 0.3, # Subsample ratio of columns when constructing each tree. 'min_child_weight': 5, # Minimum sum of instance weight(hessian) needed in a child(leaf) 'subsample_for_bin': 200000, # Number of samples for constructing bin 'min_split_gain': 0, # lambda_l1, lambda_l2 and min_gain_to_split to regularization 'reg_alpha': 0, # L1 regularization term on weights 'reg_lambda': 0, # L2 regularization term on weights 'nthread': 4, 'verbose': 0, 'metric':metrics } lgb_params.update(params) print("preparing validation datasets") xgtrain = lgb.Dataset(dtrain[predictors].values, label=dtrain[target].values, feature_name=predictors, categorical_feature=categorical_features ) xgvalid = lgb.Dataset(dvalid[predictors].values, label=dvalid[target].values, feature_name=predictors, categorical_feature=categorical_features ) evals_results = {} bst1 = lgb.train(lgb_params, xgtrain, valid_sets=[xgtrain, xgvalid], valid_names=['train','valid'], evals_result=evals_results, num_boost_round=num_boost_round, early_stopping_rounds=early_stopping_rounds, verbose_eval=10, feval=feval) print("\nModel Report") print("bst1.best_iteration: ", bst1.best_iteration) print(metrics+":", evals_results['valid'][metrics][bst1.best_iteration-1]) return (bst1,bst1.best_iteration) def DO(frm,to,fileno): dtypes = { 'ip' : 'uint32', 'app' : 'uint16', 'device' : 'uint16', 'os' : 'uint16', 'channel' : 'uint16', 'is_attributed' : 'uint8', 'click_id' : 'uint32', } print('loading train data...',frm,to) train_df = pd.read_csv("../input/train.csv", parse_dates=['click_time'], skiprows=range(1,frm), nrows=to-frm, dtype=dtypes, usecols=['ip','app','device','os', 'channel', 'click_time', 'is_attributed']) print('loading test data...') if debug: test_df = pd.read_csv("../input/test.csv", nrows=100000, parse_dates=['click_time'], dtype=dtypes, usecols=['ip','app','device','os', 'channel', 'click_time', 'click_id']) else: test_df = pd.read_csv("../input/test.csv", parse_dates=['click_time'], dtype=dtypes, usecols=['ip','app','device','os', 'channel', 'click_time', 'click_id']) len_train = len(train_df) train_df=train_df.append(test_df) del test_df gc.collect() print('Extracting new features...') train_df['hour'] = pd.to_datetime(train_df.click_time).dt.hour.astype('uint8') train_df['day'] = pd.to_datetime(train_df.click_time).dt.day.astype('uint8') gc.collect() naddfeat=9 for i in range(0,naddfeat): if i==0: selcols=['ip', 'channel']; QQ=4; if i==1: selcols=['ip', 'device', 'os', 'app']; QQ=5; if i==2: selcols=['ip', 'day', 'hour']; QQ=4; if i==3: selcols=['ip', 'app']; QQ=4; if i==4: selcols=['ip', 'app', 'os']; QQ=4; if i==5: selcols=['ip', 'device']; QQ=4; if i==6: selcols=['app', 'channel']; QQ=4; if i==7: selcols=['ip', 'os']; QQ=5; if i==8: selcols=['ip', 'device', 'os', 'app']; QQ=4; print('selcols',selcols,'QQ',QQ) filename='X%d_%d_%d.csv'%(i,frm,to) if os.path.exists(filename): if QQ==5: gp=pd.read_csv(filename,header=None) train_df['X'+str(i)]=gp else: gp=pd.read_csv(filename) train_df = train_df.merge(gp, on=selcols[0:len(selcols)-1], how='left') else: if QQ==0: gp = train_df[selcols].groupby(by=selcols[0:len(selcols)-1])[selcols[len(selcols)-1]].count().reset_index().\ rename(index=str, columns={selcols[len(selcols)-1]: 'X'+str(i)}) train_df = train_df.merge(gp, on=selcols[0:len(selcols)-1], how='left') if QQ==1: gp = train_df[selcols].groupby(by=selcols[0:len(selcols)-1])[selcols[len(selcols)-1]].mean().reset_index().\ rename(index=str, columns={selcols[len(selcols)-1]: 'X'+str(i)}) train_df = train_df.merge(gp, on=selcols[0:len(selcols)-1], how='left') if QQ==2: gp = train_df[selcols].groupby(by=selcols[0:len(selcols)-1])[selcols[len(selcols)-1]].var().reset_index().\ rename(index=str, columns={selcols[len(selcols)-1]: 'X'+str(i)}) train_df = train_df.merge(gp, on=selcols[0:len(selcols)-1], how='left') if QQ==3: gp = train_df[selcols].groupby(by=selcols[0:len(selcols)-1])[selcols[len(selcols)-1]].skew().reset_index().\ rename(index=str, columns={selcols[len(selcols)-1]: 'X'+str(i)}) train_df = train_df.merge(gp, on=selcols[0:len(selcols)-1], how='left') if QQ==4: gp = train_df[selcols].groupby(by=selcols[0:len(selcols)-1])[selcols[len(selcols)-1]].nunique().reset_index().\ rename(index=str, columns={selcols[len(selcols)-1]: 'X'+str(i)}) train_df = train_df.merge(gp, on=selcols[0:len(selcols)-1], how='left') if QQ==5: gp = train_df[selcols].groupby(by=selcols[0:len(selcols)-1])[selcols[len(selcols)-1]].cumcount() train_df['X'+str(i)]=gp.values if not debug: gp.to_csv(filename,index=False) del gp gc.collect() print('doing nextClick') predictors=[] new_feature = 'nextClick' filename='nextClick_%d_%d.csv'%(frm,to) if os.path.exists(filename): print('loading from save file') QQ=pd.read_csv(filename).values else: D=2**26 train_df['category'] = (train_df['ip'].astype(str) + "_" + train_df['app'].astype(str) + "_" + train_df['device'].astype(str) \ + "_" + train_df['os'].astype(str)).apply(hash) % D click_buffer= np.full(D, 3000000000, dtype=np.uint32) train_df['epochtime']= train_df['click_time'].astype(np.int64) // 10 ** 9 next_clicks= [] for category, t in zip(reversed(train_df['category'].values), reversed(train_df['epochtime'].values)): next_clicks.append(click_buffer[category]-t) click_buffer[category]= t del(click_buffer) QQ= list(reversed(next_clicks)) if not debug: print('saving') pd.DataFrame(QQ).to_csv(filename,index=False) train_df[new_feature] = QQ predictors.append(new_feature) train_df[new_feature+'_shift'] = pd.DataFrame(QQ).shift(+1).values predictors.append(new_feature+'_shift') del QQ gc.collect() print('grouping by ip-day-hour combination...') gp = train_df[['ip','day','hour','channel']].groupby(by=['ip','day','hour'])[['channel']].count().reset_index().rename(index=str, columns={'channel': 'ip_tcount'}) train_df = train_df.merge(gp, on=['ip','day','hour'], how='left') del gp gc.collect() print('grouping by ip-app combination...') gp = train_df[['ip', 'app', 'channel']].groupby(by=['ip', 'app'])[['channel']].count().reset_index().rename(index=str, columns={'channel': 'ip_app_count'}) train_df = train_df.merge(gp, on=['ip','app'], how='left') del gp gc.collect() print('grouping by ip-app-os combination...') gp = train_df[['ip','app', 'os', 'channel']].groupby(by=['ip', 'app', 'os'])[['channel']].count().reset_index().rename(index=str, columns={'channel': 'ip_app_os_count'}) train_df = train_df.merge(gp, on=['ip','app', 'os'], how='left') del gp gc.collect() # Adding features with var and mean hour (inspired from nuhsikander's script) print('grouping by : ip_day_chl_var_hour') gp = train_df[['ip','day','hour','channel']].groupby(by=['ip','day','channel'])[['hour']].var().reset_index().rename(index=str, columns={'hour': 'ip_tchan_count'}) train_df = train_df.merge(gp, on=['ip','day','channel'], how='left') del gp gc.collect() print('grouping by : ip_app_os_var_hour') gp = train_df[['ip','app', 'os', 'hour']].groupby(by=['ip', 'app', 'os'])[['hour']].var().reset_index().rename(index=str, columns={'hour': 'ip_app_os_var'}) train_df = train_df.merge(gp, on=['ip','app', 'os'], how='left') del gp gc.collect() print('grouping by : ip_app_channel_var_day') gp = train_df[['ip','app', 'channel', 'day']].groupby(by=['ip', 'app', 'channel'])[['day']].var().reset_index().rename(index=str, columns={'day': 'ip_app_channel_var_day'}) train_df = train_df.merge(gp, on=['ip','app', 'channel'], how='left') del gp gc.collect() print('grouping by : ip_app_chl_mean_hour') gp = train_df[['ip','app', 'channel','hour']].groupby(by=['ip', 'app', 'channel'])[['hour']].mean().reset_index().rename(index=str, columns={'hour': 'ip_app_channel_mean_hour'}) print("merging...") train_df = train_df.merge(gp, on=['ip','app', 'channel'], how='left') del gp gc.collect() print("vars and data type: ") train_df.info() train_df['ip_tcount'] = train_df['ip_tcount'].astype('uint16') train_df['ip_app_count'] = train_df['ip_app_count'].astype('uint16') train_df['ip_app_os_count'] = train_df['ip_app_os_count'].astype('uint16') target = 'is_attributed' predictors.extend(['app','device','os', 'channel', 'hour', 'day', 'ip_tcount', 'ip_tchan_count', 'ip_app_count', 'ip_app_os_count', 'ip_app_os_var', 'ip_app_channel_var_day','ip_app_channel_mean_hour']) categorical = ['app', 'device', 'os', 'channel', 'hour', 'day'] for i in range(0,naddfeat): predictors.append('X'+str(i)) print('predictors',predictors) test_df = train_df[len_train:] val_df = train_df[(len_train-val_size):len_train] train_df = train_df[:(len_train-val_size)] print("train size: ", len(train_df)) print("valid size: ", len(val_df)) print("test size : ", len(test_df)) sub = pd.DataFrame() sub['click_id'] = test_df['click_id'].astype('int') gc.collect() print("Training...") start_time = time.time() params = { 'learning_rate': 0.20, #'is_unbalance': 'true', # replaced with scale_pos_weight argument 'num_leaves': 7, # 2^max_depth - 1 'max_depth': 3, # -1 means no limit 'min_child_samples': 100, # Minimum number of data need in a child(min_data_in_leaf) 'max_bin': 100, # Number of bucketed bin for feature values 'subsample': 0.7, # Subsample ratio of the training instance. 'subsample_freq': 1, # frequence of subsample, <=0 means no enable 'colsample_bytree': 0.9, # Subsample ratio of columns when constructing each tree. 'min_child_weight': 0, # Minimum sum of instance weight(hessian) needed in a child(leaf) 'scale_pos_weight':200 # because training data is extremely unbalanced } (bst,best_iteration) = lgb_modelfit_nocv(params, train_df, val_df, predictors, target, objective='binary', metrics='auc', early_stopping_rounds=30, verbose_eval=True, num_boost_round=1000, categorical_features=categorical) print('[{}]: model training time'.format(time.time() - start_time)) del train_df del val_df gc.collect() print('Plot feature importances...') ax = lgb.plot_importance(bst, max_num_features=100) plt.show() print("Predicting...") sub['is_attributed'] = bst.predict(test_df[predictors],num_iteration=best_iteration) if not debug: print("writing...") sub.to_csv('sub_it%d.csv.gz'%(fileno),index=False,compression='gzip') print("done...") return sub nrows=184903891-1 nchunk=40000000 val_size=2500000 frm=nrows-75000000 if debug: frm=0 nchunk=100000 val_size=10000 to=frm+nchunk sub=DO(frm,to,0)
apache-2.0
henriquegemignani/randovania
randovania/gui/tracker_window.py
1
32424
import collections import functools import json import typing from pathlib import Path from random import Random from typing import Optional, Dict, Set, List, Tuple, Iterator, Union import matplotlib.pyplot as plt import networkx from PySide2 import QtWidgets from PySide2.QtCore import Qt from PySide2.QtWidgets import QMainWindow, QTreeWidgetItem, QCheckBox, QLabel, QGridLayout, QWidget, QMessageBox from matplotlib.axes import Axes from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure from randovania.game_description.area_location import AreaLocation from randovania.game_description.game_description import GameDescription from randovania.game_description.item.item_category import ItemCategory from randovania.game_description.node import Node, ResourceNode, TranslatorGateNode, TeleporterNode, DockNode from randovania.game_description.resources.item_resource_info import ItemResourceInfo from randovania.game_description.resources.pickup_entry import PickupEntry from randovania.game_description.resources.resource_info import add_resource_gain_to_current_resources from randovania.game_description.resources.translator_gate import TranslatorGate from randovania.game_description.world import World from randovania.games.game import RandovaniaGame from randovania.games.prime import patcher_file from randovania.generator import generator from randovania.gui.generated.tracker_window_ui import Ui_TrackerWindow from randovania.gui.lib.common_qt_lib import set_default_window_icon from randovania.gui.lib.custom_spin_box import CustomSpinBox from randovania.layout import translator_configuration from randovania.layout.echoes_configuration import EchoesConfiguration from randovania.layout.teleporters import TeleporterShuffleMode from randovania.layout.translator_configuration import LayoutTranslatorRequirement from randovania.resolver.bootstrap import logic_bootstrap from randovania.resolver.logic import Logic from randovania.resolver.resolver_reach import ResolverReach from randovania.resolver.state import State, add_pickup_to_state class InvalidLayoutForTracker(Exception): pass def _load_previous_state(persistence_path: Path, layout_configuration: EchoesConfiguration, ) -> Optional[dict]: previous_layout_path = persistence_path.joinpath("layout_configuration.json") try: with previous_layout_path.open() as previous_layout_file: previous_layout = EchoesConfiguration.from_json(json.load(previous_layout_file)) except (FileNotFoundError, TypeError, KeyError, ValueError, json.JSONDecodeError): return None if previous_layout != layout_configuration: return None previous_state_path = persistence_path.joinpath("state.json") try: with previous_state_path.open() as previous_state_file: return json.load(previous_state_file) except (FileNotFoundError, json.JSONDecodeError): return None class MatplotlibWidget(QtWidgets.QWidget): ax: Axes def __init__(self, parent=None): super().__init__(parent) fig = Figure(figsize=(7, 5), dpi=65, facecolor=(1, 1, 1), edgecolor=(0, 0, 0)) self.canvas = FigureCanvas(fig) self.toolbar = NavigationToolbar(self.canvas, self) lay = QtWidgets.QVBoxLayout(self) lay.addWidget(self.toolbar) lay.addWidget(self.canvas) self.ax = fig.add_subplot(111) self.line, *_ = self.ax.plot([]) class TrackerWindow(QMainWindow, Ui_TrackerWindow): # Tracker state _collected_pickups: Dict[PickupEntry, int] _actions: List[Node] # Tracker configuration logic: Logic game_description: GameDescription layout_configuration: EchoesConfiguration persistence_path: Path _initial_state: State _elevator_id_to_combo: Dict[int, QtWidgets.QComboBox] _translator_gate_to_combo: Dict[TranslatorGate, QtWidgets.QComboBox] _starting_nodes: Set[ResourceNode] _undefined_item = ItemResourceInfo(-1, "Undefined", "Undefined", 0, None) # UI tools _asset_id_to_item: Dict[int, QTreeWidgetItem] _node_to_item: Dict[Node, QTreeWidgetItem] _widget_for_pickup: Dict[PickupEntry, Union[QCheckBox, CustomSpinBox]] _during_setup = False def __init__(self, persistence_path: Path, layout_configuration: EchoesConfiguration): super().__init__() self.setupUi(self) set_default_window_icon(self) self._collected_pickups = {} self._widget_for_pickup = {} self._actions = [] self._asset_id_to_item = {} self._node_to_item = {} self.layout_configuration = layout_configuration self.persistence_path = persistence_path player_pool = generator.create_player_pool(Random(0), self.layout_configuration, 0, 1) pool_patches = player_pool.patches self.game_description, self._initial_state = logic_bootstrap(layout_configuration, player_pool.game, pool_patches) self.logic = Logic(self.game_description, layout_configuration) self._initial_state.resources["add_self_as_requirement_to_resources"] = 1 self.menu_reset_action.triggered.connect(self._confirm_reset) self.resource_filter_check.stateChanged.connect(self.update_locations_tree_for_reachable_nodes) self.hide_collected_resources_check.stateChanged.connect(self.update_locations_tree_for_reachable_nodes) self.undo_last_action_button.clicked.connect(self._undo_last_action) self.configuration_label.setText("Trick Level: {}; Starts with:\n{}".format( layout_configuration.trick_level.pretty_description, ", ".join( resource.short_name for resource in pool_patches.starting_items.keys() ) )) self.setup_pickups_box(player_pool.pickups) self.setup_possible_locations_tree() self.setup_elevators() self.setup_translator_gates() self.matplot_widget = MatplotlibWidget(self.tab_graph_map) self.tab_graph_map_layout.addWidget(self.matplot_widget) self._world_to_node_positions = {} self.map_tab_widget.currentChanged.connect(self._on_tab_changed) for world in self.game_description.world_list.worlds: self.graph_map_world_combo.addItem(world.name, world) self.graph_map_world_combo.currentIndexChanged.connect(self.on_graph_map_world_combo) persistence_path.mkdir(parents=True, exist_ok=True) previous_state = _load_previous_state(persistence_path, layout_configuration) if not self.apply_previous_state(previous_state): self.setup_starting_location(None) with persistence_path.joinpath("layout_configuration.json").open("w") as layout_file: json.dump(layout_configuration.as_json, layout_file) self._add_new_action(self._initial_state.node) def apply_previous_state(self, previous_state: Optional[dict]) -> bool: if previous_state is None: return False starting_location = None needs_starting_location = len(self.layout_configuration.starting_location.locations) > 1 resource_db = self.game_description.resource_database translator_gates = {} try: pickup_name_to_pickup = {pickup.name: pickup for pickup in self._collected_pickups.keys()} quantity_to_change = { pickup_name_to_pickup[pickup_name]: quantity for pickup_name, quantity in previous_state["collected_pickups"].items() } previous_actions = [ self.game_description.world_list.all_nodes[index] for index in previous_state["actions"] ] if needs_starting_location: starting_location = AreaLocation.from_json(previous_state["starting_location"]) elevators = { int(elevator_id): AreaLocation.from_json(location) if location is not None else None for elevator_id, location in previous_state["elevators"].items() } if self.layout_configuration.game == RandovaniaGame.PRIME2: translator_gates = { TranslatorGate(int(gate)): (resource_db.get_item(item) if item is not None else self._undefined_item) for gate, item in previous_state["translator_gates"].items() } except KeyError: return False self.setup_starting_location(starting_location) for elevator_id, area_location in elevators.items(): combo = self._elevator_id_to_combo[elevator_id] if area_location is None: combo.setCurrentIndex(0) continue for i in range(combo.count()): if area_location == combo.itemData(i): combo.setCurrentIndex(i) break for gate, item in translator_gates.items(): combo = self._translator_gate_to_combo[gate] for i in range(combo.count()): if item == combo.itemData(i): combo.setCurrentIndex(i) break self.bulk_change_quantity(quantity_to_change) self._add_new_actions(previous_actions) return True def reset(self): self.bulk_change_quantity({ pickup: 0 for pickup in self._collected_pickups.keys() }) while len(self._actions) > 1: self._actions.pop() self.actions_list.takeItem(len(self._actions)) for elevator in self._elevator_id_to_combo.values(): elevator.setCurrentIndex(0) for elevator in self._translator_gate_to_combo.values(): elevator.setCurrentIndex(0) self._refresh_for_new_action() def _confirm_reset(self): reply = QMessageBox.question(self, "Reset Tracker?", "Do you want to reset the tracker progression?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: self.reset() @property def _show_only_resource_nodes(self) -> bool: return self.resource_filter_check.isChecked() @property def _hide_collected_resources(self) -> bool: return self.hide_collected_resources_check.isChecked() @property def _collected_nodes(self) -> Set[ResourceNode]: return self._starting_nodes | set(action for action in self._actions if action.is_resource_node) def _pretty_node_name(self, node: Node) -> str: world_list = self.game_description.world_list return "{} / {}".format(world_list.area_name(world_list.nodes_to_area(node)), node.name) def _refresh_for_new_action(self): self.undo_last_action_button.setEnabled(len(self._actions) > 1) self.current_location_label.setText("Current location: {}".format(self._pretty_node_name(self._actions[-1]))) self.update_locations_tree_for_reachable_nodes() def _add_new_action(self, node: Node): self._add_new_actions([node]) def _add_new_actions(self, nodes: Iterator[Node]): for node in nodes: self.actions_list.addItem(self._pretty_node_name(node)) self._actions.append(node) self._refresh_for_new_action() def _undo_last_action(self): self._actions.pop() self.actions_list.takeItem(len(self._actions)) self._refresh_for_new_action() def _on_tree_node_double_clicked(self, item: QTreeWidgetItem, _): node: Optional[Node] = getattr(item, "node", None) if not item.isDisabled() and node is not None and node != self._actions[-1]: self._add_new_action(node) def _positions_for_world(self, world: World): g = networkx.DiGraph() world_list = self.game_description.world_list state = self.state_for_current_configuration() for area in world.areas: g.add_node(area) for area in world.areas: nearby_areas = set() for node in area.nodes: if isinstance(node, DockNode): try: target_node = world_list.resolve_dock_node(node, state.patches) nearby_areas.add(world_list.nodes_to_area(target_node)) except IndexError as e: print(f"For {node.name} in {area.name}, received {e}") continue for other_area in nearby_areas: g.add_edge(area, other_area) return networkx.drawing.spring_layout(g) def update_matplot_widget(self, nodes_in_reach: Set[Node]): g = networkx.DiGraph() world_list = self.game_description.world_list state = self.state_for_current_configuration() world = self.graph_map_world_combo.currentData() for area in world.areas: g.add_node(area) for area in world.areas: nearby_areas = set() for node in area.nodes: if node not in nodes_in_reach: continue if isinstance(node, DockNode): # TODO: respect is_blast_shield: if already opened once, no requirement needed. # Includes opening form behind with different criteria try: target_node = world_list.resolve_dock_node(node, state.patches) dock_weakness = state.patches.dock_weakness.get((area.area_asset_id, node.dock_index), node.default_dock_weakness) if dock_weakness.requirement.satisfied(state.resources, state.energy): nearby_areas.add(world_list.nodes_to_area(target_node)) except IndexError as e: print(f"For {node.name} in {area.name}, received {e}") continue for other_area in nearby_areas: g.add_edge(area, other_area) self.matplot_widget.ax.clear() cf = self.matplot_widget.ax.get_figure() cf.set_facecolor("w") if world.world_asset_id not in self._world_to_node_positions: self._world_to_node_positions[world.world_asset_id] = self._positions_for_world(world) pos = self._world_to_node_positions[world.world_asset_id] networkx.draw_networkx_nodes(g, pos, ax=self.matplot_widget.ax) networkx.draw_networkx_edges(g, pos, arrows=True, ax=self.matplot_widget.ax) networkx.draw_networkx_labels(g, pos, ax=self.matplot_widget.ax, labels={area: area.name for area in world.areas}, verticalalignment='top') self.matplot_widget.ax.set_axis_off() plt.draw_if_interactive() self.matplot_widget.canvas.draw() def on_graph_map_world_combo(self): nodes_in_reach = self.current_nodes_in_reach(self.state_for_current_configuration()) self.update_matplot_widget(nodes_in_reach) def current_nodes_in_reach(self, state): if state is None: nodes_in_reach = set() else: reach = ResolverReach.calculate_reach(self.logic, state) nodes_in_reach = set(reach.nodes) nodes_in_reach.add(state.node) return nodes_in_reach def _on_tab_changed(self): if self.map_tab_widget.currentWidget() == self.tab_graph_map: self.on_graph_map_world_combo() def update_locations_tree_for_reachable_nodes(self): state = self.state_for_current_configuration() nodes_in_reach = self.current_nodes_in_reach(state) if self.map_tab_widget.currentWidget() == self.tab_graph_map: self.update_matplot_widget(nodes_in_reach) all_nodes = self.game_description.world_list.all_nodes for world in self.game_description.world_list.worlds: for area in world.areas: area_is_visible = False for node in area.nodes: is_collected = node in self._collected_nodes is_visible = node in nodes_in_reach and not (self._hide_collected_resources and is_collected) if self._show_only_resource_nodes: is_visible = is_visible and node.is_resource_node node_item = self._node_to_item[node] node_item.setHidden(not is_visible) if node.is_resource_node: resource_node = typing.cast(ResourceNode, node) node_item.setDisabled(not resource_node.can_collect(state.patches, state.resources, all_nodes)) node_item.setCheckState(0, Qt.Checked if is_collected else Qt.Unchecked) area_is_visible = area_is_visible or is_visible self._asset_id_to_item[area.area_asset_id].setHidden(not area_is_visible) # Persist the current state self.persist_current_state() def persist_current_state(self): world_list = self.game_description.world_list with self.persistence_path.joinpath("state.json").open("w") as state_file: json.dump( { "actions": [ node.index for node in self._actions ], "collected_pickups": { pickup.name: quantity for pickup, quantity in self._collected_pickups.items() }, "elevators": { str(elevator_id): combo.currentData().as_json if combo.currentIndex() > 0 else None for elevator_id, combo in self._elevator_id_to_combo.items() }, "translator_gates": { str(gate.index): combo.currentData().index if combo.currentIndex() > 0 else None for gate, combo in self._translator_gate_to_combo.items() }, "starting_location": world_list.node_to_area_location(self._initial_state.node).as_json, }, state_file ) def setup_possible_locations_tree(self): """ Creates the possible_locations_tree with all worlds, areas and nodes. """ self.possible_locations_tree.itemDoubleClicked.connect(self._on_tree_node_double_clicked) # TODO: Dark World names for world in self.game_description.world_list.worlds: world_item = QTreeWidgetItem(self.possible_locations_tree) world_item.setText(0, world.name) world_item.setExpanded(True) self._asset_id_to_item[world.world_asset_id] = world_item for area in world.areas: area_item = QTreeWidgetItem(world_item) area_item.area = area area_item.setText(0, area.name) area_item.setHidden(True) self._asset_id_to_item[area.area_asset_id] = area_item for node in area.nodes: node_item = QTreeWidgetItem(area_item) if isinstance(node, TranslatorGateNode): node_item.setText(0, "{} ({})".format(node.name, node.gate)) else: node_item.setText(0, node.name) node_item.node = node if node.is_resource_node: node_item.setFlags(node_item.flags() & ~Qt.ItemIsUserCheckable) self._node_to_item[node] = node_item def setup_elevators(self): world_list = self.game_description.world_list nodes_by_world: Dict[str, List[TeleporterNode]] = collections.defaultdict(list) self._elevator_id_to_combo = {} areas_to_not_change = { 2278776548, # Sky Temple Gateway 2068511343, # Sky Temple Energy Controller 3136899603, # Aerie Transport Station 1564082177, # Aerie } targets = {} for world, area, node in world_list.all_worlds_areas_nodes: if isinstance(node, TeleporterNode) and node.editable and area.area_asset_id not in areas_to_not_change: name = world.correct_name(area.in_dark_aether) nodes_by_world[name].append(node) location = AreaLocation(world.world_asset_id, area.area_asset_id) targets[patcher_file.elevator_area_name(world_list, location, True)] = location if self.layout_configuration.elevators.mode == TeleporterShuffleMode.ONE_WAY_ANYTHING: targets = {} for world in world_list.worlds: for area in world.areas: name = world.correct_name(area.in_dark_aether) targets[f"{name} - {area.name}"] = AreaLocation(world.world_asset_id, area.area_asset_id) combo_targets = sorted(targets.items(), key=lambda it: it[0]) for world_name in sorted(nodes_by_world.keys()): nodes = nodes_by_world[world_name] nodes_locations = [AreaLocation(world_list.nodes_to_world(node).world_asset_id, world_list.nodes_to_area(node).area_asset_id) for node in nodes] nodes_names = [patcher_file.elevator_area_name(world_list, location, True) for location in nodes_locations] nodes = sorted(nodes_by_world[world_name], key=lambda it: world_list.nodes_to_area(it).name) group = QtWidgets.QGroupBox(self.elevators_scroll_contents) group.setTitle(world_name) self.elevators_scroll_layout.addWidget(group) layout = QtWidgets.QGridLayout(group) for i, (node, location, name) in enumerate(sorted(zip(nodes, nodes_locations, nodes_names), key=lambda it: it[2])): node_name = QtWidgets.QLabel(group) node_name.setText(name) layout.addWidget(node_name, i, 0) combo = QtWidgets.QComboBox(group) if self.layout_configuration.elevators.is_vanilla: combo.addItem("Vanilla", node.default_connection) combo.setEnabled(False) else: combo.addItem("Undefined", location) for target_name, connection in combo_targets: combo.addItem(target_name, connection) combo.currentIndexChanged.connect(self.update_locations_tree_for_reachable_nodes) self._elevator_id_to_combo[node.teleporter_instance_id] = combo layout.addWidget(combo, i, 1) def setup_translator_gates(self): world_list = self.game_description.world_list resource_db = self.game_description.resource_database self._translator_gate_to_combo = {} if self.layout_configuration.game != RandovaniaGame.PRIME2: return gates = { f"{area.name} ({node.gate.index})": node.gate for world, area, node in world_list.all_worlds_areas_nodes if isinstance(node, TranslatorGateNode) } translator_requirement = self.layout_configuration.translator_configuration.translator_requirement for i, (gate_name, gate) in enumerate(sorted(gates.items(), key=lambda it: it[0])): node_name = QtWidgets.QLabel(self.translator_gate_scroll_contents) node_name.setText(gate_name) self.translator_gate_scroll_layout.addWidget(node_name, i, 0) combo = QtWidgets.QComboBox(self.translator_gate_scroll_contents) gate_requirement = translator_requirement[gate] if gate_requirement in (LayoutTranslatorRequirement.RANDOM, LayoutTranslatorRequirement.RANDOM_WITH_REMOVED): combo.addItem("Undefined", self._undefined_item) for translator, index in translator_configuration.ITEM_INDICES.items(): combo.addItem(translator.long_name, resource_db.get_item(index)) else: combo.addItem(gate_requirement.long_name, resource_db.get_item(gate_requirement.item_index)) combo.setEnabled(False) combo.currentIndexChanged.connect(self.update_locations_tree_for_reachable_nodes) self._translator_gate_to_combo[gate] = combo self.translator_gate_scroll_layout.addWidget(combo, i, 1) def setup_starting_location(self, area_location: Optional[AreaLocation]): world_list = self.game_description.world_list if len(self.layout_configuration.starting_location.locations) > 1: if area_location is None: area_locations = sorted(self.layout_configuration.starting_location.locations, key=lambda it: world_list.area_name(world_list.area_by_area_location(it))) location_names = [world_list.area_name(world_list.area_by_area_location(it)) for it in area_locations] selected_name = QtWidgets.QInputDialog.getItem(self, "Starting Location", "Select starting location", location_names, 0, False) area_location = area_locations[location_names.index(selected_name[0])] self._initial_state.node = world_list.resolve_teleporter_connection(area_location) self._starting_nodes = { node for node in world_list.all_nodes if node.is_resource_node and node.resource() in self._initial_state.resources } def _change_item_quantity(self, pickup: PickupEntry, use_quantity_as_bool: bool, quantity: int): if use_quantity_as_bool: if bool(quantity): quantity = 1 else: quantity = 0 self._collected_pickups[pickup] = quantity if not self._during_setup: self.update_locations_tree_for_reachable_nodes() def bulk_change_quantity(self, new_quantity: Dict[PickupEntry, int]): self._during_setup = True for pickup, quantity in new_quantity.items(): widget = self._widget_for_pickup[pickup] if isinstance(widget, QCheckBox): widget.setChecked(quantity > 0) else: widget.setValue(quantity) self._during_setup = False def _create_widgets_with_quantity(self, pickup: PickupEntry, parent_widget: QWidget, parent_layout: QGridLayout, row: int, quantity: int, ): label = QLabel(parent_widget) label.setText(pickup.name) parent_layout.addWidget(label, row, 0) spin_bix = CustomSpinBox(parent_widget) spin_bix.setMaximumWidth(50) spin_bix.setMaximum(quantity) spin_bix.valueChanged.connect(functools.partial(self._change_item_quantity, pickup, False)) self._widget_for_pickup[pickup] = spin_bix parent_layout.addWidget(spin_bix, row, 1) def setup_pickups_box(self, item_pool: List[PickupEntry]): parent_widgets: Dict[ItemCategory, Tuple[QWidget, QGridLayout]] = { ItemCategory.EXPANSION: (self.expansions_box, self.expansions_layout), ItemCategory.ENERGY_TANK: (self.expansions_box, self.expansions_layout), ItemCategory.TRANSLATOR: (self.translators_box, self.translators_layout), ItemCategory.TEMPLE_KEY: (self.keys_box, self.keys_layout), ItemCategory.SKY_TEMPLE_KEY: (self.keys_box, self.keys_layout), } major_pickup_parent_widgets = (self.upgrades_box, self.upgrades_layout) row_for_parent = { self.expansions_box: 0, self.translators_box: 0, self.upgrades_box: 0, self.keys_box: 0, } column_for_parent = { self.translators_box: 0, self.upgrades_box: 0, self.keys_box: 0, } k_column_count = 2 pickup_by_name = {} pickup_with_quantity = {} for pickup in item_pool: if pickup.name in pickup_by_name: pickup_with_quantity[pickup_by_name[pickup.name]] += 1 else: pickup_by_name[pickup.name] = pickup pickup_with_quantity[pickup] = 1 non_expansions_with_quantity = [] for pickup, quantity in pickup_with_quantity.items(): self._collected_pickups[pickup] = 0 parent_widget, parent_layout = parent_widgets.get(pickup.item_category, major_pickup_parent_widgets) row = row_for_parent[parent_widget] if parent_widget is self.expansions_box: self._create_widgets_with_quantity(pickup, parent_widget, parent_layout, row, quantity) row_for_parent[parent_widget] += 1 else: if quantity > 1: non_expansions_with_quantity.append((parent_widget, parent_layout, pickup, quantity)) else: check_box = QCheckBox(parent_widget) check_box.setText(pickup.name) check_box.stateChanged.connect(functools.partial(self._change_item_quantity, pickup, True)) self._widget_for_pickup[pickup] = check_box column = column_for_parent[parent_widget] parent_layout.addWidget(check_box, row, column) column += 1 if column >= k_column_count: column = 0 row += 1 row_for_parent[parent_widget] = row column_for_parent[parent_widget] = column for parent_widget, parent_layout, pickup, quantity in non_expansions_with_quantity: if column_for_parent[parent_widget] != 0: column_for_parent[parent_widget] = 0 row_for_parent[parent_widget] += 1 self._create_widgets_with_quantity(pickup, parent_widget, parent_layout, row_for_parent[parent_widget], quantity) row_for_parent[parent_widget] += 1 def state_for_current_configuration(self) -> Optional[State]: all_nodes = self.game_description.world_list.all_nodes state = self._initial_state.copy() if self._actions: state.node = self._actions[-1] for teleporter, combo in self._elevator_id_to_combo.items(): assert combo.currentData() is not None state.patches.elevator_connection[teleporter] = combo.currentData() for gate, item in self._translator_gate_to_combo.items(): state.patches.translator_gates[gate] = item.currentData() for pickup, quantity in self._collected_pickups.items(): for _ in range(quantity): add_pickup_to_state(state, pickup) for node in self._collected_nodes: add_resource_gain_to_current_resources(node.resource_gain_on_collect(state.patches, state.resources, all_nodes), state.resources) return state
gpl-3.0
gplssm/europepstrans
europepstrans/results/__init__.py
1
13654
""" TimeFrameResults steals methods from oemof.outputlib adapted to the structure applied here. Most relevant difference is results data stored in self.data """ from oemof.outputlib import DataFramePlot, ResultsDataFrame import pickle from matplotlib import pyplot as plt import logging import pandas as pd class TimeFrameResults: """ Container for results of one time frame (i.e. one year) Attributes ---------- data : DataFrame Structure multi-indexed result data """ def __init__(self, **kwargs): """ Initializes data object based on oemof results class """ results_file = kwargs.get('results_file', None) self.subset = kwargs.get('subset', None) self.ax = kwargs.get('ax') if results_file is None: # self.data = DataFramePlot(energy_system=kwargs.get('energy_system')) self.data = ResultsDataFrame(energy_system=kwargs.get('energy_system')) else: self.data = pickle.load(open(results_file, 'rb')) self.reformat_data() def preview(self): """ Print short preview of data """ return self.data.head() def reformat_data(self): """ Extract region information from bus label put into separate index label """ # TODO: get regions list from elsewhere regions = ['deu', 'xfra', 'xbnl'] regions_leading_underscore = ['_' + x for x in regions] # put bus_label to column (required to work on) self.data.reset_index(level='bus_label', inplace=True) self.data.reset_index(level='obj_label', inplace=True) # extra region from bus label and write to new column self.data['region'] = self.data['bus_label'].str.extract( r"(?=(" + '|'.join(regions) + r"))", expand=True) self.data['region'].fillna('global', inplace=True) # remove region from bus_label and obj_label self.data['bus_label'] = self.data['bus_label'].str.replace( r"(" + '|'.join(regions_leading_underscore) + r")", '') self.data['obj_label'] = self.data['obj_label'].str.replace( r"(" + '|'.join(regions_leading_underscore) + r")", '') # put bus_label back to index self.data = self.data.set_index(['bus_label', 'region', 'obj_label'], append=True) # reorder and resort levels level_order = ['bus_label', 'type', 'obj_label', 'region', 'datetime'] self.data = self.data.reorder_levels(level_order) def slice_by(self, **kwargs): r""" Method for slicing the ResultsDataFrame. A subset is returned. Parameters ---------- bus_label : string type : string (to_bus/from_bus/other) obj_label: string date_from : string Start date selection e.g. "2016-01-01 00:00:00". If not set, the whole time range will be plotted. date_to : string End date selection e.g. "2016-03-01 00:00:00". If not set, the whole time range will be plotted. """ kwargs.setdefault('bus_label', slice(None)) kwargs.setdefault('type', slice(None)) kwargs.setdefault('obj_label', slice(None)) kwargs.setdefault( 'date_from', self.data.index.get_level_values('datetime')[0]) kwargs.setdefault( 'date_to', self.data.index.get_level_values('datetime')[-1]) # slicing idx = pd.IndexSlice subset = self.data.loc[idx[ kwargs['bus_label'], kwargs['type'], kwargs['obj_label'], slice(pd.Timestamp(kwargs['date_from']), pd.Timestamp(kwargs['date_to']))], :] return subset def slice_unstacked(self, unstacklevel='obj_label', formatted=False, **kwargs): r"""Method for slicing the ResultsDataFrame. An unstacked subset is returned. Parameters ---------- unstacklevel : string (default: 'obj_label') Level to unstack the subset of the DataFrame. formatted : boolean missing... """ subset = self.slice_by(**kwargs) subset = subset.unstack(level=unstacklevel) if formatted is True: subset.reset_index(level=['bus_label', 'type'], drop=True, inplace=True) # user standard insteadt of multi-indexed columns subset.columns = subset.columns.get_level_values(1).unique() # return subset self.subset = subset def plot(self, **kwargs): r""" Passing the data attribute to the pandas plotting method. All parameters will be directly passed to pandas.DataFrame.plot(). See http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html for more information. Returns ------- self """ self.ax = self.subset.plot(**kwargs) return self def io_plot(self, bus_label, cdict, line_kwa=None, lineorder=None, bar_kwa=None, barorder=None, **kwargs): r""" Plotting a combined bar and line plot to see the fitting of in- and outcomming flows of a bus balance. Parameters ---------- bus_label : string Uid of the bus to plot the balance. cdict : dictionary A dictionary that has all possible components as keys and its colors as items. line_kwa : dictionary Keyword arguments to be passed to the pandas line plot. bar_kwa : dictionary Keyword arguments to be passed to the pandas bar plot. lineorder : list Order of columns to plot the line plot barorder : list Order of columns to plot the bar plot Note ---- Further keyword arguments will be passed to the :class:`slice_unstacked method <DataFramePlot.slice_unstacked>`. Returns ------- handles, labels Manipulated labels to correct the unsual construction of the stack line plot. You can use them for further maipulations. """ self.ax = kwargs.get('ax', self.ax) if bar_kwa is None: bar_kwa = dict() if line_kwa is None: line_kwa = dict() if self.ax is None: fig = plt.figure() self.ax = fig.add_subplot(1, 1, 1) # Create a bar plot for all input flows self.slice_unstacked(bus_label=bus_label, type='to_bus', **kwargs) if barorder is not None: self.rearrange_subset(barorder) self.subset.plot(kind='bar', linewidth=0, stacked=True, width=1, ax=self.ax, color=self.color_from_dict(cdict), **bar_kwa) # Create a line plot for all output flows self.slice_unstacked(bus_label=bus_label, type='from_bus', **kwargs) if lineorder is not None: self.rearrange_subset(lineorder) # The following changes are made to have the bottom line on top layer # of all lines. Normally the bottom line is the first line that is # plotted and will be on the lowest layer. This is difficult to read. new_df = pd.DataFrame(index=self.subset.index) n = 0 tmp = 0 for col in self.subset.columns: if n < 1: new_df[col] = self.subset[col] else: new_df[col] = self.subset[col] + tmp tmp = new_df[col] n += 1 if lineorder is None: new_df.sort_index(axis=1, ascending=False, inplace=True) else: lineorder.reverse() new_df = new_df[lineorder] colorlist = self.color_from_dict(cdict) if isinstance(colorlist, list): colorlist.reverse() separator = len(colorlist) new_df.plot(kind='line', ax=self.ax, color=colorlist, drawstyle='steps-mid', **line_kwa) # Adapt the legend to the new oder handles, labels = self.ax.get_legend_handles_labels() tmp_lab = [x for x in reversed(labels[0:separator])] tmp_hand = [x for x in reversed(handles[0:separator])] handles = tmp_hand + handles[separator:] labels = tmp_lab + labels[separator:] labels.reverse() handles.reverse() self.ax.legend(handles, labels) return handles, labels def rearrange_subset(self, order): r""" Change the order of the subset DataFrame Parameters ---------- order : list New order of columns Returns ------- self """ cols = list(self.subset.columns.values) neworder = [x for x in list(order) if x in set(cols)] missing = [x for x in list(cols) if x not in set(order)] if len(missing) > 0: logging.warning( "Columns that are not part of the order list are removed: " + str(missing)) self.subset = self.subset[neworder] def color_from_dict(self, colordict): r""" Method to convert a dictionary containing the components and its colors to a color list that can be directly useed with the color parameter of the pandas plotting method. Parameters ---------- colordict : dictionary A dictionary that has all possible components as keys and its colors as items. Returns ------- list Containing the colors of all components of the subset attribute """ tmplist = list( map(colordict.get, list(self.subset.columns))) tmplist = ['#00FFFF' if v is None else v for v in tmplist] if len(tmplist) == 1: colorlist = tmplist[0] else: colorlist = tmplist return colorlist def set_datetime_ticks(self, tick_distance=None, number_autoticks=3, date_format='%d-%m-%Y %H:%M'): r""" Set configurable ticks for the time axis. One can choose the number of ticks or the distance between ticks and the format. Parameters ---------- tick_distance : real The disctance between to ticks in hours. If not set autoticks are set (see number_autoticks). number_autoticks : int (default: 3) The number of ticks on the time axis, independent of the time range. The higher the number of ticks is, the shorter should be the date_format string. date_format : string (default: '%d-%m-%Y %H:%M') The string to define the format of the date and time. See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior for more information. """ dates = self.subset.index.get_level_values('datetime').unique() if tick_distance is None: tick_distance = int(len(dates) / number_autoticks) - 1 self.ax.set_xticks(range(0, len(dates), tick_distance), minor=False) self.ax.set_xticklabels( [item.strftime(date_format) for item in dates.tolist()[0::tick_distance]], rotation=0, minor=False) def outside_legend(self, reverse=False, plotshare=0.9, **kwargs): r""" Move the legend outside the plot. Bases on the ideas of Joe Kington. See http://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot for more information. Parameters ---------- reverse : boolean (default: False) Print out the legend in reverse order. This is interesting for stack-plots to have the legend in the same order as the stacks. plotshare : real (default: 0.9) Share of the plot area to create space for the legend (0 to 1). loc : string (default: 'center left') Location of the plot. bbox_to_anchor : tuple (default: (1, 0.5)) Set the anchor for the legend. ncol : integer (default: 1) Number of columns of the legend. handles : list of handles A list of handels if they are already modified by another function or method. Normally these handles will be automatically taken from the artis object. lables : list of labels A list of labels if they are already modified by another function or method. Normally these handles will be automatically taken from the artis object. Note ---- All keyword arguments (kwargs) will be directly passed to the matplotlib legend class. See http://matplotlib.org/api/legend_api.html#matplotlib.legend.Legend for more parameters. """ kwargs.setdefault('loc', 'center left') kwargs.setdefault('bbox_to_anchor', (1, 0.5)) kwargs.setdefault('ncol', 1) handles = kwargs.pop('handles', self.ax.get_legend_handles_labels()[0]) labels = kwargs.pop('labels', self.ax.get_legend_handles_labels()[1]) if reverse: handles.reverse() labels.reverse() box = self.ax.get_position() self.ax.set_position([box.x0, box.y0, box.width * plotshare, box.height]) self.ax.legend(handles, labels, **kwargs) if __name__ == '__main__': pass
gpl-3.0
samzhang111/scikit-learn
examples/calibration/plot_calibration_curve.py
225
5903
""" ============================== Probability Calibration curves ============================== When performing classification one often wants to predict not only the class label, but also the associated probability. This probability gives some kind of confidence on the prediction. This example demonstrates how to display how well calibrated the predicted probabilities are and how to calibrate an uncalibrated classifier. The experiment is performed on an artificial dataset for binary classification with 100.000 samples (1.000 of them are used for model fitting) with 20 features. Of the 20 features, only 2 are informative and 10 are redundant. The first figure shows the estimated probabilities obtained with logistic regression, Gaussian naive Bayes, and Gaussian naive Bayes with both isotonic calibration and sigmoid calibration. The calibration performance is evaluated with Brier score, reported in the legend (the smaller the better). One can observe here that logistic regression is well calibrated while raw Gaussian naive Bayes performs very badly. This is because of the redundant features which violate the assumption of feature-independence and result in an overly confident classifier, which is indicated by the typical transposed-sigmoid curve. Calibration of the probabilities of Gaussian naive Bayes with isotonic regression can fix this issue as can be seen from the nearly diagonal calibration curve. Sigmoid calibration also improves the brier score slightly, albeit not as strongly as the non-parametric isotonic regression. This can be attributed to the fact that we have plenty of calibration data such that the greater flexibility of the non-parametric model can be exploited. The second figure shows the calibration curve of a linear support-vector classifier (LinearSVC). LinearSVC shows the opposite behavior as Gaussian naive Bayes: the calibration curve has a sigmoid curve, which is typical for an under-confident classifier. In the case of LinearSVC, this is caused by the margin property of the hinge loss, which lets the model focus on hard samples that are close to the decision boundary (the support vectors). Both kinds of calibration can fix this issue and yield nearly identical results. This shows that sigmoid calibration can deal with situations where the calibration curve of the base classifier is sigmoid (e.g., for LinearSVC) but not where it is transposed-sigmoid (e.g., Gaussian naive Bayes). """ print(__doc__) # Author: Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr> # Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # License: BSD Style. import matplotlib.pyplot as plt from sklearn import datasets from sklearn.naive_bayes import GaussianNB from sklearn.svm import LinearSVC from sklearn.linear_model import LogisticRegression from sklearn.metrics import (brier_score_loss, precision_score, recall_score, f1_score) from sklearn.calibration import CalibratedClassifierCV, calibration_curve from sklearn.cross_validation import train_test_split # Create dataset of classification task with many redundant and few # informative features X, y = datasets.make_classification(n_samples=100000, n_features=20, n_informative=2, n_redundant=10, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.99, random_state=42) def plot_calibration_curve(est, name, fig_index): """Plot calibration curve for est w/o and with calibration. """ # Calibrated with isotonic calibration isotonic = CalibratedClassifierCV(est, cv=2, method='isotonic') # Calibrated with sigmoid calibration sigmoid = CalibratedClassifierCV(est, cv=2, method='sigmoid') # Logistic regression with no calibration as baseline lr = LogisticRegression(C=1., solver='lbfgs') fig = plt.figure(fig_index, figsize=(10, 10)) ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2) ax2 = plt.subplot2grid((3, 1), (2, 0)) ax1.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated") for clf, name in [(lr, 'Logistic'), (est, name), (isotonic, name + ' + Isotonic'), (sigmoid, name + ' + Sigmoid')]: clf.fit(X_train, y_train) y_pred = clf.predict(X_test) if hasattr(clf, "predict_proba"): prob_pos = clf.predict_proba(X_test)[:, 1] else: # use decision function prob_pos = clf.decision_function(X_test) prob_pos = \ (prob_pos - prob_pos.min()) / (prob_pos.max() - prob_pos.min()) clf_score = brier_score_loss(y_test, prob_pos, pos_label=y.max()) print("%s:" % name) print("\tBrier: %1.3f" % (clf_score)) print("\tPrecision: %1.3f" % precision_score(y_test, y_pred)) print("\tRecall: %1.3f" % recall_score(y_test, y_pred)) print("\tF1: %1.3f\n" % f1_score(y_test, y_pred)) fraction_of_positives, mean_predicted_value = \ calibration_curve(y_test, prob_pos, n_bins=10) ax1.plot(mean_predicted_value, fraction_of_positives, "s-", label="%s (%1.3f)" % (name, clf_score)) ax2.hist(prob_pos, range=(0, 1), bins=10, label=name, histtype="step", lw=2) ax1.set_ylabel("Fraction of positives") ax1.set_ylim([-0.05, 1.05]) ax1.legend(loc="lower right") ax1.set_title('Calibration plots (reliability curve)') ax2.set_xlabel("Mean predicted value") ax2.set_ylabel("Count") ax2.legend(loc="upper center", ncol=2) plt.tight_layout() # Plot calibration cuve for Gaussian Naive Bayes plot_calibration_curve(GaussianNB(), "Naive Bayes", 1) # Plot calibration cuve for Linear SVC plot_calibration_curve(LinearSVC(), "SVC", 2) plt.show()
bsd-3-clause
allenxwang/kafka
system_test/utils/metrics.py
28
13903
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you 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. #!/usr/bin/env python # =================================== # file: metrics.py # =================================== import inspect import json import logging import os import signal import subprocess import sys import traceback import csv import time import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt from collections import namedtuple import numpy from pyh import * import kafka_system_test_utils import system_test_utils logger = logging.getLogger("namedLogger") thisClassName = '(metrics)' d = {'name_of_class': thisClassName} attributeNameToNameInReportedFileMap = { 'Min': 'min', 'Max': 'max', 'Mean': 'mean', '50thPercentile': 'median', 'StdDev': 'stddev', '95thPercentile': '95%', '99thPercentile': '99%', '999thPercentile': '99.9%', 'Count': 'count', 'OneMinuteRate': '1 min rate', 'MeanRate': 'mean rate', 'FiveMinuteRate': '5 min rate', 'FifteenMinuteRate': '15 min rate', 'Value': 'value' } def getCSVFileNameFromMetricsMbeanName(mbeanName): return mbeanName.replace(":type=", ".").replace(",name=", ".") + ".csv" def read_metrics_definition(metricsFile): metricsFileData = open(metricsFile, "r").read() metricsJsonData = json.loads(metricsFileData) allDashboards = metricsJsonData['dashboards'] allGraphs = [] for dashboard in allDashboards: dashboardName = dashboard['name'] graphs = dashboard['graphs'] for graph in graphs: bean = graph['bean_name'] allGraphs.append(graph) attributes = graph['attributes'] #print "Filtering on attributes " + attributes return allGraphs def get_dashboard_definition(metricsFile, role): metricsFileData = open(metricsFile, "r").read() metricsJsonData = json.loads(metricsFileData) allDashboards = metricsJsonData['dashboards'] dashboardsForRole = [] for dashboard in allDashboards: if dashboard['role'] == role: dashboardsForRole.append(dashboard) return dashboardsForRole def ensure_valid_headers(headers, attributes): if headers[0] != "# time": raise Exception("First column should be time") for header in headers: logger.debug(header, extra=d) # there should be exactly one column with a name that matches attributes try: attributeColumnIndex = headers.index(attributes) return attributeColumnIndex except ValueError as ve: #print "#### attributes : ", attributes #print "#### headers : ", headers raise Exception("There should be exactly one column that matches attribute: {0} in".format(attributes) + " headers: {0}".format(",".join(headers))) def plot_graphs(inputCsvFiles, labels, title, xLabel, yLabel, attribute, outputGraphFile): # create empty plot fig=plt.figure() fig.subplots_adjust(bottom=0.2) ax=fig.add_subplot(111) labelx = -0.3 # axes coords ax.set_xlabel(xLabel) ax.set_ylabel(yLabel) ax.grid() #ax.yaxis.set_label_coords(labelx, 0.5) Coordinates = namedtuple("Coordinates", 'x y') plots = [] coordinates = [] # read data for all files, organize by label in a dict for fileAndLabel in zip(inputCsvFiles, labels): inputCsvFile = fileAndLabel[0] label = fileAndLabel[1] csv_reader = list(csv.reader(open(inputCsvFile, "rb"))) x,y = [],[] xticks_labels = [] try: # read first line as the headers headers = csv_reader.pop(0) attributeColumnIndex = ensure_valid_headers(headers, attributeNameToNameInReportedFileMap[attribute]) logger.debug("Column index for attribute {0} is {1}".format(attribute, attributeColumnIndex), extra=d) start_time = (int)(os.path.getctime(inputCsvFile) * 1000) int(csv_reader[0][0]) for line in csv_reader: if(len(line) == 0): continue yVal = float(line[attributeColumnIndex]) xVal = int(line[0]) y.append(yVal) epoch= start_time + int(line[0]) x.append(xVal) xticks_labels.append(time.strftime("%H:%M:%S", time.localtime(epoch))) coordinates.append(Coordinates(xVal, yVal)) p1 = ax.plot(x,y) plots.append(p1) except Exception as e: logger.error("ERROR while plotting data for {0}: {1}".format(inputCsvFile, e), extra=d) traceback.print_exc() # find xmin, xmax, ymin, ymax from all csv files xmin = min(map(lambda coord: coord.x, coordinates)) xmax = max(map(lambda coord: coord.x, coordinates)) ymin = min(map(lambda coord: coord.y, coordinates)) ymax = max(map(lambda coord: coord.y, coordinates)) # set x and y axes limits plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) # set ticks accordingly xticks = numpy.arange(xmin, xmax, 0.2*xmax) # yticks = numpy.arange(ymin, ymax) plt.xticks(xticks,xticks_labels,rotation=17) # plt.yticks(yticks) plt.legend(plots,labels, loc=2) plt.title(title) plt.savefig(outputGraphFile) def draw_all_graphs(metricsDescriptionFile, testcaseEnv, clusterConfig): # go through each role and plot graphs for the role's metrics roles = set(map(lambda config: config['role'], clusterConfig)) for role in roles: dashboards = get_dashboard_definition(metricsDescriptionFile, role) entities = kafka_system_test_utils.get_entities_for_role(clusterConfig, role) for dashboard in dashboards: graphs = dashboard['graphs'] # draw each graph for all entities draw_graph_for_role(graphs, entities, role, testcaseEnv) def draw_graph_for_role(graphs, entities, role, testcaseEnv): for graph in graphs: graphName = graph['graph_name'] yLabel = graph['y_label'] inputCsvFiles = [] graphLegendLabels = [] for entity in entities: entityMetricsDir = kafka_system_test_utils.get_testcase_config_log_dir_pathname(testcaseEnv, role, entity['entity_id'], "metrics") entityMetricCsvFile = entityMetricsDir + "/" + getCSVFileNameFromMetricsMbeanName(graph['bean_name']) if(not os.path.exists(entityMetricCsvFile)): logger.warn("The file {0} does not exist for plotting".format(entityMetricCsvFile), extra=d) else: inputCsvFiles.append(entityMetricCsvFile) graphLegendLabels.append(role + "-" + entity['entity_id']) # print "Plotting graph for metric {0} on entity {1}".format(graph['graph_name'], entity['entity_id']) try: # plot one graph per mbean attribute labels = graph['y_label'].split(',') fullyQualifiedAttributeNames = map(lambda attribute: graph['bean_name'] + ':' + attribute, graph['attributes'].split(',')) attributes = graph['attributes'].split(',') for labelAndAttribute in zip(labels, fullyQualifiedAttributeNames, attributes): outputGraphFile = testcaseEnv.testCaseDashboardsDir + "/" + role + "/" + labelAndAttribute[1] + ".svg" plot_graphs(inputCsvFiles, graphLegendLabels, graph['graph_name'] + '-' + labelAndAttribute[2], "time", labelAndAttribute[0], labelAndAttribute[2], outputGraphFile) # print "Finished plotting graph for metric {0} on entity {1}".format(graph['graph_name'], entity['entity_id']) except Exception as e: logger.error("ERROR while plotting graph {0}: {1}".format(outputGraphFile, e), extra=d) traceback.print_exc() def build_all_dashboards(metricsDefinitionFile, testcaseDashboardsDir, clusterConfig): metricsHtmlFile = testcaseDashboardsDir + "/metrics.html" centralDashboard = PyH('Kafka Metrics Dashboard') centralDashboard << h1('Kafka Metrics Dashboard', cl='center') roles = set(map(lambda config: config['role'], clusterConfig)) for role in roles: entities = kafka_system_test_utils.get_entities_for_role(clusterConfig, role) dashboardPagePath = build_dashboard_for_role(metricsDefinitionFile, role, entities, testcaseDashboardsDir) centralDashboard << a(role, href = dashboardPagePath) centralDashboard << br() centralDashboard.printOut(metricsHtmlFile) def build_dashboard_for_role(metricsDefinitionFile, role, entities, testcaseDashboardsDir): # build all dashboards for the input entity's based on its role. It can be one of kafka, zookeeper, producer # consumer dashboards = get_dashboard_definition(metricsDefinitionFile, role) entityDashboard = PyH('Kafka Metrics Dashboard for ' + role) entityDashboard << h1('Kafka Metrics Dashboard for ' + role, cl='center') entityDashboardHtml = testcaseDashboardsDir + "/" + role + "-dashboards.html" for dashboard in dashboards: # place the graph svg files in this dashboard allGraphs = dashboard['graphs'] for graph in allGraphs: attributes = map(lambda attribute: graph['bean_name'] + ':' + attribute, graph['attributes'].split(',')) for attribute in attributes: graphFileLocation = testcaseDashboardsDir + "/" + role + "/" + attribute + ".svg" entityDashboard << embed(src = graphFileLocation, type = "image/svg+xml") entityDashboard.printOut(entityDashboardHtml) return entityDashboardHtml def start_metrics_collection(jmxHost, jmxPort, role, entityId, systemTestEnv, testcaseEnv): logger.info("starting metrics collection on jmx port : " + jmxPort, extra=d) jmxUrl = "service:jmx:rmi:///jndi/rmi://" + jmxHost + ":" + jmxPort + "/jmxrmi" clusterConfig = systemTestEnv.clusterEntityConfigDictList metricsDefinitionFile = systemTestEnv.METRICS_PATHNAME entityMetricsDir = kafka_system_test_utils.get_testcase_config_log_dir_pathname(testcaseEnv, role, entityId, "metrics") dashboardsForRole = get_dashboard_definition(metricsDefinitionFile, role) mbeansForRole = get_mbeans_for_role(dashboardsForRole) kafkaHome = system_test_utils.get_data_by_lookup_keyval(clusterConfig, "entity_id", entityId, "kafka_home") javaHome = system_test_utils.get_data_by_lookup_keyval(clusterConfig, "entity_id", entityId, "java_home") for mbean in mbeansForRole: outputCsvFile = entityMetricsDir + "/" + mbean + ".csv" startMetricsCmdList = ["ssh " + jmxHost, "'JAVA_HOME=" + javaHome, "JMX_PORT= " + kafkaHome + "/bin/kafka-run-class.sh kafka.tools.JmxTool", "--jmx-url " + jmxUrl, "--object-name " + mbean + " 1> ", outputCsvFile + " & echo pid:$! > ", entityMetricsDir + "/entity_pid'"] startMetricsCommand = " ".join(startMetricsCmdList) logger.debug("executing command: [" + startMetricsCommand + "]", extra=d) system_test_utils.async_sys_call(startMetricsCommand) time.sleep(1) pidCmdStr = "ssh " + jmxHost + " 'cat " + entityMetricsDir + "/entity_pid' 2> /dev/null" logger.debug("executing command: [" + pidCmdStr + "]", extra=d) subproc = system_test_utils.sys_call_return_subproc(pidCmdStr) # keep track of JMX ppid in a dictionary of entity_id to list of JMX ppid # testcaseEnv.entityJmxParentPidDict: # key: entity_id # val: list of JMX ppid associated to that entity_id # { 1: [1234, 1235, 1236], 2: [2234, 2235, 2236], ... } for line in subproc.stdout.readlines(): line = line.rstrip('\n') logger.debug("line: [" + line + "]", extra=d) if line.startswith("pid"): logger.debug("found pid line: [" + line + "]", extra=d) tokens = line.split(':') thisPid = tokens[1] if entityId not in testcaseEnv.entityJmxParentPidDict: testcaseEnv.entityJmxParentPidDict[entityId] = [] testcaseEnv.entityJmxParentPidDict[entityId].append(thisPid) #print "\n#### testcaseEnv.entityJmxParentPidDict ", testcaseEnv.entityJmxParentPidDict, "\n" def stop_metrics_collection(jmxHost, jmxPort): logger.info("stopping metrics collection on " + jmxHost + ":" + jmxPort, extra=d) system_test_utils.sys_call("ps -ef | grep JmxTool | grep -v grep | grep " + jmxPort + " | awk '{print $2}' | xargs kill -9") def get_mbeans_for_role(dashboardsForRole): graphs = reduce(lambda x,y: x+y, map(lambda dashboard: dashboard['graphs'], dashboardsForRole)) return set(map(lambda metric: metric['bean_name'], graphs))
apache-2.0
pyspeckit/pyspeckit
pyspeckit/cubes/mapplot.py
4
16759
""" MapPlot ------- Make plots of the cube and interactively connect them to spectrum plotting. This is really an interactive component of the package; nothing in here is meant for publication-quality plots, but more for user interactive analysis. That said, the plotter makes use of `APLpy <https://github.com/aplpy/aplpy>`_, so it is possible to make publication-quality plots. :author: Adam Ginsburg :date: 03/17/2011 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ from __future__ import print_function import matplotlib import matplotlib.figure import numpy as np import copy import itertools import six try: import astropy.wcs as pywcs import astropy.io.fits as pyfits pywcsOK = True except ImportError: try: import pyfits import pywcs pywcsOK = True except ImportError: pywcsOK = False try: import aplpy icanhasaplpy = True except: # aplpy fails with generic exceptions instead of ImportError icanhasaplpy = False from . import cubes class MapPlotter(object): """ Class to plot a spectrum See `mapplot` for use documentation; this docstring is only for initialization. """ def __init__(self, Cube=None, figure=None, doplot=False, **kwargs): """ Create a map figure for future plotting """ import matplotlib.pyplot self._pyplot = matplotlib.pyplot # figure out where to put the plot if isinstance(figure,matplotlib.figure.Figure): self.figure = figure elif type(figure) is int: self.figure = self._pyplot.figure(figure) else: self.figure = None self.axis = None self.FITSFigure = None self._click_marks = [] self._circles = [] self._clickX = None self._clickY = None self.overplot_colorcycle = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y']) self.overplot_linestyle = '-' self.Cube = Cube if self.Cube is not None: self.header = cubes.flatten_header(self.Cube.header, delete=True) if pywcsOK: self.wcs = pywcs.WCS(self.header) if doplot: self.mapplot(**kwargs) def __call__(self, **kwargs): """ see mapplot """ return self.mapplot(**kwargs) def mapplot(self, convention='calabretta', colorbar=True, useaplpy=True, vmin=None, vmax=None, cmap=None, plotkwargs={}, **kwargs): """ Plot up a map based on an input data cube. The map to be plotted is selected using `makeplane`. The `estimator` keyword argument is passed to that function. The plotted map, once shown, is interactive. You can click on it with any of the three mouse buttons. Button 1 or keyboard '1': Plot the selected pixel's spectrum in another window. Mark the clicked pixel with an 'x' Button 2 or keyboard 'o': Overplot a second (or third, fourth, fifth...) spectrum in the external plot window Button 3: Disconnect the interactive viewer You can also click-and-drag with button 1 to average over a circular region. This same effect can be achieved by using the 'c' key to set the /c/enter of a circle and the 'r' key to set its /r/adius (i.e., hover over the center and press 'c', then hover some distance away and press 'r'). Parameters ---------- convention : 'calabretta' or 'griesen' The default projection to assume for Galactic data when plotting with aplpy. colorbar : bool Whether to show a colorbar plotkwargs : dict, optional A dictionary of keyword arguments to pass to aplpy.show_colorscale or matplotlib.pyplot.imshow useaplpy : bool Use aplpy if a FITS header is available vmin, vmax: float or None Override values for the vmin/vmax values. Will be automatically determined if left as None .. todo: Allow mapplot in subfigure """ if (self.figure is None): self.figure = self._pyplot.figure() elif (not self._pyplot.fignum_exists(self.figure.number)): self.figure = self._pyplot.figure() else: self._disconnect() self.figure.clf() # this is where the map is created; everything below this is just plotting self.makeplane(**kwargs) # have tot pop out estimator so that kwargs can be passed to imshow if 'estimator' in kwargs: kwargs.pop('estimator') # Below here is all plotting stuff if vmin is None: vmin = self.plane[self.plane==self.plane].min() if vmax is None: vmax = self.plane[self.plane==self.plane].max() if icanhasaplpy and useaplpy: self.fitsfile = pyfits.PrimaryHDU(data=self.plane,header=self.header) self.FITSFigure = aplpy.FITSFigure(self.fitsfile,figure=self.figure,convention=convention) self.FITSFigure.show_colorscale(vmin=vmin, vmax=vmax, cmap=cmap, **plotkwargs) if hasattr(self.FITSFigure, '_ax1'): self.axis = self.FITSFigure._ax1 else: self.axis = self.FITSFigure.ax if colorbar: try: self.FITSFigure.add_colorbar() except Exception as ex: print("ERROR: Could not create colorbar! Error was %s" % str(ex)) self._origin = 0 # FITS convention # TODO: set _origin to 1 if using PIXEL units, not real wcs else: self.axis = self.figure.add_subplot(111) if hasattr(self,'colorbar') and self.colorbar is not None: if self.colorbar.ax in self.axis.figure.axes: self.axis.figure.delaxes(self.colorbar.ax) self.axis.imshow(self.plane, vmin=vmin, vmax=vmax, cmap=cmap, **plotkwargs) if colorbar: try: self.colorbar = self._pyplot.colorbar(self.axis.images[0]) except Exception as ex: print("ERROR: Could not create colorbar! Error was %s" % str(ex)) self._origin = 0 # normal convention self.canvas = self.axis.figure.canvas self._connect() def _connect(self): """ Connect click, click up (release click), and key press to events """ self.clickid = self.canvas.callbacks.connect('button_press_event',self.click) self.clickupid = self.canvas.callbacks.connect('button_release_event',self.plot_spectrum) self.keyid = self.canvas.callbacks.connect('key_press_event',self.plot_spectrum) def _disconnect(self): """ Disconnect click, click up (release click), and key press from events """ if hasattr(self,'canvas'): self.canvas.mpl_disconnect(self.clickid) self.canvas.mpl_disconnect(self.clickupid) self.canvas.mpl_disconnect(self.keyid) def makeplane(self, estimator=np.nanmean): """ Create a "plane" view of the cube, either by slicing or projecting it or by showing a slice from the best-fit model parameter cube. Parameters ---------- estimator : [ function | 'max' | 'int' | FITS filename | integer | slice ] A non-pythonic, non-duck-typed variable. If it's a function, apply that function along the cube's spectral axis to obtain an estimate (e.g., mean, min, max, etc.). 'max' will do the same thing as passing np.max 'int' will attempt to integrate the image (which is why I didn't duck-type) (integrate means sum and multiply by dx) a .fits filename will be read using pyfits (so you can make your own cover figure) an integer will get the n'th slice in the parcube if it exists If it's a slice, slice the input data cube along the Z-axis with this slice """ # THIS IS A HACK!!! isinstance(a function, function) must be a thing... FUNCTION = type(np.max) # estimator is NOT duck-typed if type(estimator) is FUNCTION: self.plane = estimator(self.Cube.cube,axis=0) elif isinstance(estimator, six.string_types): if estimator == 'max': self.plane = self.Cube.cube.max(axis=0) elif estimator == 'int': dx = np.abs(self.Cube.xarr[1:] - self.Cube.xarr[:-1]) dx = np.concatenate([dx,[dx[-1]]]) self.plane = (self.Cube.cube * dx[:,np.newaxis,np.newaxis]).sum(axis=0) elif estimator[-5:] == ".fits": self.plane = pyfits.getdata(estimator) elif type(estimator) is slice: self.plane = self.Cube.cube[estimator,:,:] elif type(estimator) is int: if hasattr(self.Cube,'parcube'): self.plane = self.Cube.parcube[estimator,:,:] if self.plane is None: raise ValueError("Invalid estimator %s" % (str(estimator))) if np.sum(np.isfinite(self.plane)) == 0: raise ValueError("Map is all NaNs or infs. Check your estimator or your input cube.") def click(self,event): """ Record location of downclick """ if event.inaxes: self._clickX = np.round(event.xdata) - self._origin self._clickY = np.round(event.ydata) - self._origin def plot_spectrum(self, event, plot_fit=True): """ Connects map cube to Spectrum... """ self.event = event if event.inaxes: clickX = np.round(event.xdata) - self._origin clickY = np.round(event.ydata) - self._origin # grab toolbar info so that we don't do anything if a tool is selected tb = self.canvas.toolbar if tb.mode != '': return elif event.key is not None: if event.key == 'c': self._center = (clickX-1,clickY-1) self._remove_circle() self._add_click_mark(clickX,clickY,clear=True) elif event.key == 'r': x,y = self._center self._add_circle(x,y,clickX,clickY) self.circle(x,y,clickX-1,clickY-1) elif event.key == 'o': clickX,clickY = round(clickX),round(clickY) print("OverPlotting spectrum from point %i,%i" % (clickX-1,clickY-1)) color = next(self.overplot_colorcycle) self._add_click_mark(clickX,clickY,clear=False, color=color) self.Cube.plot_spectrum(clickX-1,clickY-1,clear=False, color=color, linestyle=self.overplot_linestyle) elif event.key in ('1','2'): event.button = int(event.key) event.key = None self.plot_spectrum(event) elif (hasattr(event,'button') and event.button in (1,2) and not (self._clickX == clickX and self._clickY == clickY)): if event.button == 1: self._remove_circle() clear=True color = 'k' linestyle = 'steps-mid' else: color = next(self.overplot_colorcycle) linestyle = self.overplot_linestyle clear=False rad = ( (self._clickX-clickX)**2 + (self._clickY-clickY)**2 )**0.5 print("Plotting circle from point %i,%i to %i,%i (r=%f)" % (self._clickX,self._clickY,clickX,clickY,rad)) self._add_circle(self._clickX,self._clickY,clickX,clickY) self.circle(self._clickX,self._clickY,clickX,clickY,clear=clear,linestyle=linestyle,color=color) elif hasattr(event,'button') and event.button is not None: if event.button==1: clickX,clickY = round(clickX),round(clickY) print("Plotting spectrum from point %i,%i" % (clickX,clickY)) self._remove_circle() self._add_click_mark(clickX,clickY,clear=True) self.Cube.plot_spectrum(clickX,clickY,clear=True) if plot_fit: self.Cube.plot_fit(clickX, clickY, silent=True) elif event.button==2: clickX,clickY = round(clickX),round(clickY) print("OverPlotting spectrum from point %i,%i" % (clickX,clickY)) color = next(self.overplot_colorcycle) self._add_click_mark(clickX,clickY,clear=False, color=color) self.Cube.plot_spectrum(clickX,clickY,clear=False, color=color, linestyle=self.overplot_linestyle) elif event.button==3: print("Disconnecting GAIA-like tool") self._disconnect() else: print("Call failed for some reason: ") print("event: ",event) else: pass # never really needed... warn("Click outside of axes") def _add_click_mark(self,x,y,clear=False,color='k'): """ Add an X at some position """ if clear: self._clear_click_marks() if self.FITSFigure is not None: label = 'xmark%i' % (len(self._click_marks)+1) x,y = self.FITSFigure.pixel2world(x,y) self.FITSFigure.show_markers(x,y,marker='x',c=color,layer=label) self._click_marks.append( label ) else: self._click_marks.append( self.axis.plot(x,y,'kx') ) self.refresh() def _clear_click_marks(self): """ Remove all marks added by previous clicks """ if self.FITSFigure is not None: for mark in self._click_marks: if mark in self.FITSFigure._layers: self.FITSFigure.remove_layer(mark) else: for mark in self._click_marks: self._click_marks.remove(mark) if mark in self.axis.lines: self.axis.lines.remove(mark) self.refresh() def _add_circle(self,x,y,x2,y2,**kwargs): """ """ if self.FITSFigure is not None: x,y = self.FITSFigure.pixel2world(x,y) x2,y2 = self.FITSFigure.pixel2world(x2,y2) r = (np.linalg.norm(np.array([x,y])-np.array([x2,y2]))) #self.FITSFigure.show_markers(x,y,s=r,marker='o',facecolor='none',edgecolor='black',layer='circle') layername = "circle%02i" % len(self._circles) self.FITSFigure.show_circles(x,y,r,edgecolor='black',facecolor='none',layer=layername,**kwargs) self._circles.append(layername) else: r = np.linalg.norm(np.array([x,y])-np.array([x2,y2])) circle = matplotlib.patches.Circle([x,y],radius=r,**kwargs) self._circles.append( circle ) self.axis.patches.append(circle) self.refresh() def _remove_circle(self): """ """ if self.FITSFigure is not None: for layername in self._circles: if layername in self.FITSFigure._layers: self.FITSFigure.remove_layer(layername) else: for circle in self._circles: if circle in self.axis.patches: self.axis.patches.remove(circle) self._circles.remove(circle) self.refresh() def refresh(self): if self.axis is not None: self.axis.figure.canvas.draw() def circle(self,x1,y1,x2,y2,**kwargs): """ Plot the spectrum of a circular aperture """ r = (np.linalg.norm(np.array([x1,y1])-np.array([x2,y2]))) self.Cube.plot_apspec([x1,y1,r],**kwargs) #self.Cube.data = cubes.extract_aperture( self.Cube.cube, [x1,y1,r] , coordsys=None ) #self.Cube.plotter() def copy(self, parent=None): """ Create a copy of the map plotter with blank (uninitialized) axis & figure [ parent ] A spectroscopic axis instance that is the parent of the specfit instance. This needs to be specified at some point, but defaults to None to prevent overwriting a previous plot. """ newmapplot = copy.copy(self) newmapplot.Cube = parent newmapplot.axis = None newmapplot.figure = None return newmapplot
mit
AlfredNeverKog/BrainCarya
src/my/kadenze/lesson3/mnist_autoencoder.py
1
2610
from mnist import MNIST import numpy as np import tensorflow as tf from src.my.lib.utils import montage import matplotlib.pyplot as plt from PIL import Image src = '../../../../data/mnist/' output='./content/1/%s.jpg' mndata = MNIST(src) data = np.array(mndata.load_testing()) X = data[0] Y = data[1] items = 100 imgs = np.array([i for i in np.array(X[:items])]).reshape(items,28,28) n_features = 784 n_input = n_features Y = imgs.reshape(items,n_features).astype(float) current_input = imgs.reshape(items,n_features).astype(float) Ws = [] Bs = [] dimensions = [512,256,128,64] for layer_i,n_ouputs in enumerate(dimensions): with tf.variable_scope("encoder/variable/%s" % layer_i): W = tf.get_variable(name="weight%s" % layer_i, dtype=tf.float64, initializer=tf.contrib.layers.xavier_initializer(), shape=[n_input, n_ouputs]) #B = tf.get_variable(name='bias%s' % layer_i, dtype=tf.float64, # initializer=tf.random_normal_initializer(mean=0.0, stddev=1.1), # shape=[n_ouputs]) #h = tf.nn.bias_add(value=tf.matmul(current_input, W), # bias=B) h = tf.matmul(current_input, W) current_input = h current_input = tf.nn.relu(current_input) n_input = n_ouputs Ws.append(W) #Bs.append() Ws = Ws[::-1]#reverse Bs = Bs[::-1]#reverse #dimensions = dimensions[::1][1:].append(n_features) dimensions = dimensions[::-1][1:] +[n_features] #Build DECODER for layer_i,n_ouputs in enumerate(dimensions): with tf.variable_scope("encoder/variable/%s" % layer_i): ##128x64 -> 64x128 h = value=tf.matmul(current_input,tf.transpose(Ws[layer_i])) if layer_i + 1 < len(Bs): h = tf.nn.bias_add(h,bias=Bs[layer_i + 1]) current_input = h current_input = tf.nn.relu(current_input) n_input = n_ouputs loss_func = tf.reduce_mean(tf.squared_difference(current_input, Y), 1) optimizer = tf.train.AdamOptimizer(learning_rate=0.00001) train = optimizer.minimize(loss_func) counter = 0 with tf.Session() as sess: sess.run(tf.initialize_all_variables()) for i in range(50000): sess.run(train) if i % 15 == 0: Image.fromarray(montage(sess.run(current_input).reshape(items,28,28)).astype(np.uint8)) \ .save(output % ("0"*(5 - len(str(counter))) + str(counter))) print(sess.run(tf.reduce_mean(loss_func))) counter += 1
mit
f3r/scikit-learn
examples/covariance/plot_lw_vs_oas.py
159
2951
""" ============================= Ledoit-Wolf vs OAS estimation ============================= The usual covariance maximum likelihood estimate can be regularized using shrinkage. Ledoit and Wolf proposed a close formula to compute the asymptotically optimal shrinkage parameter (minimizing a MSE criterion), yielding the Ledoit-Wolf covariance estimate. Chen et al. proposed an improvement of the Ledoit-Wolf shrinkage parameter, the OAS coefficient, whose convergence is significantly better under the assumption that the data are Gaussian. This example, inspired from Chen's publication [1], shows a comparison of the estimated MSE of the LW and OAS methods, using Gaussian distributed data. [1] "Shrinkage Algorithms for MMSE Covariance Estimation" Chen et al., IEEE Trans. on Sign. Proc., Volume 58, Issue 10, October 2010. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from scipy.linalg import toeplitz, cholesky from sklearn.covariance import LedoitWolf, OAS np.random.seed(0) ############################################################################### n_features = 100 # simulation covariance matrix (AR(1) process) r = 0.1 real_cov = toeplitz(r ** np.arange(n_features)) coloring_matrix = cholesky(real_cov) n_samples_range = np.arange(6, 31, 1) repeat = 100 lw_mse = np.zeros((n_samples_range.size, repeat)) oa_mse = np.zeros((n_samples_range.size, repeat)) lw_shrinkage = np.zeros((n_samples_range.size, repeat)) oa_shrinkage = np.zeros((n_samples_range.size, repeat)) for i, n_samples in enumerate(n_samples_range): for j in range(repeat): X = np.dot( np.random.normal(size=(n_samples, n_features)), coloring_matrix.T) lw = LedoitWolf(store_precision=False, assume_centered=True) lw.fit(X) lw_mse[i, j] = lw.error_norm(real_cov, scaling=False) lw_shrinkage[i, j] = lw.shrinkage_ oa = OAS(store_precision=False, assume_centered=True) oa.fit(X) oa_mse[i, j] = oa.error_norm(real_cov, scaling=False) oa_shrinkage[i, j] = oa.shrinkage_ # plot MSE plt.subplot(2, 1, 1) plt.errorbar(n_samples_range, lw_mse.mean(1), yerr=lw_mse.std(1), label='Ledoit-Wolf', color='navy', lw=2) plt.errorbar(n_samples_range, oa_mse.mean(1), yerr=oa_mse.std(1), label='OAS', color='darkorange', lw=2) plt.ylabel("Squared error") plt.legend(loc="upper right") plt.title("Comparison of covariance estimators") plt.xlim(5, 31) # plot shrinkage coefficient plt.subplot(2, 1, 2) plt.errorbar(n_samples_range, lw_shrinkage.mean(1), yerr=lw_shrinkage.std(1), label='Ledoit-Wolf', color='navy', lw=2) plt.errorbar(n_samples_range, oa_shrinkage.mean(1), yerr=oa_shrinkage.std(1), label='OAS', color='darkorange', lw=2) plt.xlabel("n_samples") plt.ylabel("Shrinkage") plt.legend(loc="lower right") plt.ylim(plt.ylim()[0], 1. + (plt.ylim()[1] - plt.ylim()[0]) / 10.) plt.xlim(5, 31) plt.show()
bsd-3-clause
phobson/statsmodels
statsmodels/sandbox/examples/example_crossval.py
33
2232
import numpy as np from statsmodels.sandbox.tools import cross_val if __name__ == '__main__': #A: josef-pktd import statsmodels.api as sm from statsmodels.api import OLS #from statsmodels.datasets.longley import load from statsmodels.datasets.stackloss import load from statsmodels.iolib.table import (SimpleTable, default_txt_fmt, default_latex_fmt, default_html_fmt) import numpy as np data = load() data.exog = sm.tools.add_constant(data.exog, prepend=False) resols = sm.OLS(data.endog, data.exog).fit() print('\n OLS leave 1 out') for inidx, outidx in cross_val.LeaveOneOut(len(data.endog)): res = sm.OLS(data.endog[inidx], data.exog[inidx,:]).fit() print(data.endog[outidx], res.model.predict(res.params, data.exog[outidx,:], end=' ')) print(data.endog[outidx] - res.model.predict(res.params, data.exog[outidx,:])) print('\n OLS leave 2 out') resparams = [] for inidx, outidx in cross_val.LeavePOut(len(data.endog), 2): res = sm.OLS(data.endog[inidx], data.exog[inidx,:]).fit() #print data.endog[outidx], res.model.predict(data.exog[outidx,:]), #print ((data.endog[outidx] - res.model.predict(data.exog[outidx,:]))**2).sum() resparams.append(res.params) resparams = np.array(resparams) print(resparams) doplots = 1 if doplots: import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties plt.figure() figtitle = 'Leave2out parameter estimates' t = plt.gcf().text(0.5, 0.95, figtitle, horizontalalignment='center', fontproperties=FontProperties(size=16)) for i in range(resparams.shape[1]): plt.subplot(4, 2, i+1) plt.hist(resparams[:,i], bins = 10) #plt.title("Leave2out parameter estimates") plt.show() for inidx, outidx in cross_val.KStepAhead(20,2): #note the following were broken because KStepAhead returns now a slice by default print(inidx) print(np.ones(20)[inidx].sum(), np.arange(20)[inidx][-4:]) print(outidx) print(np.nonzero(np.ones(20)[outidx])[0][()])
bsd-3-clause
raincoatrun/basemap
examples/testgdal.py
4
2655
""" example showing how to plot data from a DEM file and an ESRI shape file using gdal (http://pypi.python.org/pypi/GDAL). """ from osgeo import gdal, ogr from mpl_toolkits.basemap import Basemap, cm import numpy as np import matplotlib.pyplot as plt from numpy import ma # read 2.5 minute U.S. DEM file using gdal. # (http://www.prism.oregonstate.edu/docs/meta/dem_25m.htm) gd = gdal.Open('us_25m.dem') array = gd.ReadAsArray() # get lat/lon coordinates from DEM file. coords = gd.GetGeoTransform() nlons = array.shape[1]; nlats = array.shape[0] delon = coords[1] delat = coords[5] lons = coords[0] + delon*np.arange(nlons) lats = coords[3] + delat*np.arange(nlats)[::-1] # reverse lats # setup figure. fig = plt.figure(figsize=(11,6)) # setup basemap instance. m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, projection='lcc',lat_1=33,lat_2=45,lon_0=-95) # create masked array, reversing data in latitude direction # (so that data is oriented in increasing latitude, as transform_scalar requires). topoin = ma.masked_values(array[::-1,:],-999.) # transform DEM data to a 4 km native projection grid nx = int((m.xmax-m.xmin)/4000.)+1; ny = int((m.ymax-m.ymin)/4000.)+1 topodat = m.transform_scalar(topoin,lons,lats,nx,ny,masked=True) # plot DEM image on map. im = m.imshow(topodat,cmap=cm.GMT_haxby_r) # draw meridians and parallels. m.drawparallels(np.arange(20,71,10),labels=[1,0,0,0]) m.drawmeridians(np.arange(-120,-40,10),labels=[0,0,0,1]) # plot state boundaries from shapefile using ogr. g = ogr.Open ("st99_d00.shp") L = g.GetLayer(0) # data is in 1st layer. for feat in L: # iterate over features in layer geo = feat.GetGeometryRef() # iterate over geometries. for count in range(geo.GetGeometryCount()): geom = geo.GetGeometryRef(count) if not geom.GetGeometryCount(): # just one geometry. # get lon,lat points lons = [geom.GetX(i) for i in range(geom.GetPointCount())] lats = [geom.GetY(i) for i in range(geom.GetPointCount())] # convert to map projection coords. x, y = m(lons,lats) # plot on map. m.plot(x,y,'k') else: # iterate over nested geometries. for cnt in range( geom.GetGeometryCount()): g = geom.GetGeometryRef( cnt ) lons = [g.GetX(i) for i in range(g.GetPointCount())] lats = [g.GetY(i) for i in range(g.GetPointCount())] x, y = m(lons,lats) m.plot(x,y,'k') # draw colorbar. m.colorbar(im) plt.title(gd.GetDescription()+' with state boundaries from '+g.GetName(),y=1.05) plt.show()
gpl-2.0
andeplane/lammps
python/examples/matplotlib_plot.py
22
2270
#!/usr/bin/env python -i # preceding line should have path for Python on your machine # matplotlib_plot.py # Purpose: plot Temp of running LAMMPS simulation via matplotlib # Syntax: plot.py in.lammps Nfreq Nsteps compute-ID # in.lammps = LAMMPS input script # Nfreq = plot data point every this many steps # Nsteps = run for this many steps # compute-ID = ID of compute that calculates temperature # (or any other scalar quantity) from __future__ import print_function import sys sys.path.append("./pizza") import matplotlib matplotlib.use('tkagg') import matplotlib.pyplot as plt # parse command line argv = sys.argv if len(argv) != 5: print("Syntax: plot.py in.lammps Nfreq Nsteps compute-ID") sys.exit() infile = sys.argv[1] nfreq = int(sys.argv[2]) nsteps = int(sys.argv[3]) compute = sys.argv[4] me = 0 # uncomment if running in parallel via Pypar #import pypar #me = pypar.rank() #nprocs = pypar.size() from lammps import lammps lmp = lammps() # run infile all at once # assumed to have no run command in it lmp.file(infile) lmp.command("thermo %d" % nfreq) # initial 0-step run to generate initial 1-point plot lmp.command("run 0 pre yes post no") value = lmp.extract_compute(compute,0,0) ntimestep = 0 xaxis = [ntimestep] yaxis = [value] # create matplotlib plot # just proc 0 handles plotting if me == 0: fig = plt.figure() line, = plt.plot(xaxis, yaxis) plt.xlim([0, nsteps]) plt.title(compute) plt.xlabel("Timestep") plt.ylabel("Temperature") plt.show(block=False) # run nfreq steps at a time w/out pre/post, query compute, refresh plot import time while ntimestep < nsteps: lmp.command("run %d pre no post no" % nfreq) ntimestep += nfreq value = lmp.extract_compute(compute,0,0) xaxis.append(ntimestep) yaxis.append(value) if me == 0: line.set_xdata(xaxis) line.set_ydata(yaxis) ax = plt.gca() ax.relim() ax.autoscale_view(True, True, True) fig.canvas.draw() lmp.command("run 0 pre no post yes") # uncomment if running in parallel via Pypar #print("Proc %d out of %d procs has" % (me,nprocs), lmp) #pypar.finalize() if sys.version_info[0] == 3: input("Press Enter to exit...") else: raw_input("Press Enter to exit...")
gpl-2.0
evanbiederstedt/CMBintheLikeHoodz
source_code/CAMB_vary_OmegaB_lmax1100_Feb2016.py
1
137613
# coding: utf-8 # In[1]: # # # hundred_samples = np.linspace(0.05, 0.5, num=100) # # Planck found \Omega_CDM # GAVO simulated map set at \Omega_CDM = 0.122 # CAMB default below at omch2=0.122 # # In[2]: # # First output 200 CAMB scalar outputs # # 0.005 to 0.05 # # In[3]: from matplotlib import pyplot as plt import numpy as np import camb from camb import model, initialpower # In[4]: """ #Set up a new set of parameters for CAMB pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(2000, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) for name in powers: print(name) # In[5]: # plot the total lensed CMB power spectra versus unlensed, and fractional difference totCL=powers['total'] unlensedCL=powers['unlensed_scalar'] print(totCL.shape) # Python CL arrays are all zero based (starting at L=0), Note L=0,1 entries will be zero by default. # The differenent CL are always in the order TT, EE, BB, TE (with BB=0 for unlensed scalar results). ls = np.arange(totCL.shape[0]) print(ls) #print(totCL[:30]) # print first 30 totCL fig, ax = plt.subplots(2,2, figsize = (12,12)) ax[0,0].plot(ls,totCL[:,0], color='k') ax[0,0].plot(ls,unlensedCL[:,0], color='r') ax[0,0].set_title('TT') ax[0,1].plot(ls[2:], 1-unlensedCL[2:,0]/totCL[2:,0]); ax[0,1].set_title(r'$\Delta TT$') ax[1,0].plot(ls,totCL[:,1], color='k') ax[1,0].plot(ls,unlensedCL[:,1], color='r') ax[1,0].set_title(r'$EE$') ax[1,1].plot(ls,totCL[:,3], color='k') ax[1,1].plot(ls,unlensedCL[:,3], color='r') ax[1,1].set_title(r'$TE$'); for ax in ax.reshape(-1): ax.set_xlim([2,2500]) """ # In[6]: twohundred_samples = np.linspace(0.005, 0.05, num=200) #print(twohundred_samples) #Set up a new set of parameters for CAMB pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) pars.set_for_lmax(2500, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers =results.get_cmb_power_spectra(pars) for name in powers: print(name) """ array([ 0.005 , 0.00522613, 0.00545226, 0.00567839, 0.00590452, 0.00613065, 0.00635678, 0.00658291, 0.00680905, 0.00703518, 0.00726131, 0.00748744, 0.00771357, 0.0079397 , 0.00816583, 0.00839196, 0.00861809, 0.00884422, 0.00907035, 0.00929648, 0.00952261, 0.00974874, 0.00997487, 0.01020101, 0.01042714, 0.01065327, 0.0108794 , 0.01110553, 0.01133166, 0.01155779, 0.01178392, 0.01201005, 0.01223618, 0.01246231, 0.01268844, 0.01291457, 0.0131407 , 0.01336683, 0.01359296, 0.0138191 , 0.01404523, 0.01427136, 0.01449749, 0.01472362, 0.01494975, 0.01517588, 0.01540201, 0.01562814, 0.01585427, 0.0160804 , 0.01630653, 0.01653266, 0.01675879, 0.01698492, 0.01721106, 0.01743719, 0.01766332, 0.01788945, 0.01811558, 0.01834171, 0.01856784, 0.01879397, 0.0190201 , 0.01924623, 0.01947236, 0.01969849, 0.01992462, 0.02015075, 0.02037688, 0.02060302, 0.02082915, 0.02105528, 0.02128141, 0.02150754, 0.02173367, 0.0219598 , 0.02218593, 0.02241206, 0.02263819, 0.02286432, 0.02309045, 0.02331658, 0.02354271, 0.02376884, 0.02399497, 0.02422111, 0.02444724, 0.02467337, 0.0248995 , 0.02512563, 0.02535176, 0.02557789, 0.02580402, 0.02603015, 0.02625628, 0.02648241, 0.02670854, 0.02693467, 0.0271608 , 0.02738693, 0.02761307, 0.0278392 , 0.02806533, 0.02829146, 0.02851759, 0.02874372, 0.02896985, 0.02919598, 0.02942211, 0.02964824, 0.02987437, 0.0301005 , 0.03032663, 0.03055276, 0.03077889, 0.03100503, 0.03123116, 0.03145729, 0.03168342, 0.03190955, 0.03213568, 0.03236181, 0.03258794, 0.03281407, 0.0330402 , 0.03326633, 0.03349246, 0.03371859, 0.03394472, 0.03417085, 0.03439698, 0.03462312, 0.03484925, 0.03507538, 0.03530151, 0.03552764, 0.03575377, 0.0359799 , 0.03620603, 0.03643216, 0.03665829, 0.03688442, 0.03711055, 0.03733668, 0.03756281, 0.03778894, 0.03801508, 0.03824121, 0.03846734, 0.03869347, 0.0389196 , 0.03914573, 0.03937186, 0.03959799, 0.03982412, 0.04005025, 0.04027638, 0.04050251, 0.04072864, 0.04095477, 0.0411809 , 0.04140704, 0.04163317, 0.0418593 , 0.04208543, 0.04231156, 0.04253769, 0.04276382, 0.04298995, 0.04321608, 0.04344221, 0.04366834, 0.04389447, 0.0441206 , 0.04434673, 0.04457286, 0.04479899, 0.04502513, 0.04525126, 0.04547739, 0.04570352, 0.04592965, 0.04615578, 0.04638191, 0.04660804, 0.04683417, 0.0470603 , 0.04728643, 0.04751256, 0.04773869, 0.04796482, 0.04819095, 0.04841709, 0.04864322, 0.04886935, 0.04909548, 0.04932161, 0.04954774, 0.04977387, 0.05 ]) """ pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls0 = unlencl[:,0][2:1101] print(len(cls0)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls1 = unlencl[:,0][2:1101] print(len(cls1)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls2 = unlencl[:,0][2:1101] print(len(cls2)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls3 = unlencl[:,0][2:1101] print(len(cls3)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls4 = unlencl[:,0][2:1101] print(len(cls4)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls5 = unlencl[:,0][2:1101] print(len(cls5)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls6 = unlencl[:,0][2:1101] print(len(cls6)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls7 = unlencl[:,0][2:1101] print(len(cls7)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls8 = unlencl[:,0][2:1101] print(len(cls8)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls9 = unlencl[:,0][2:1101] print(len(cls9)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls10 = unlencl[:,0][2:1101] print(len(cls10)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls11 = unlencl[:,0][2:1101] print(len(cls11)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls12 = unlencl[:,0][2:1101] print(len(cls12)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls13 = unlencl[:,0][2:1101] print(len(cls13)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls14 = unlencl[:,0][2:1101] print(len(cls14)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls15 = unlencl[:,0][2:1101] print(len(cls15)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls16 = unlencl[:,0][2:1101] print(len(cls16)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls17 = unlencl[:,0][2:1101] print(len(cls17)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls18 = unlencl[:,0][2:1101] print(len(cls18)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls19 = unlencl[:,0][2:1101] print(len(cls19)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls20 = unlencl[:,0][2:1101] print(len(cls20)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls21 = unlencl[:,0][2:1101] print(len(cls21)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls22 = unlencl[:,0][2:1101] print(len(cls22)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls23 = unlencl[:,0][2:1101] print(len(cls23)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls24 = unlencl[:,0][2:1101] print(len(cls24)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls25 = unlencl[:,0][2:1101] print(len(cls25)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls26 = unlencl[:,0][2:1101] print(len(cls26)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls27 = unlencl[:,0][2:1101] print(len(cls27)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls28 = unlencl[:,0][2:1101] print(len(cls28)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls29 = unlencl[:,0][2:1101] print(len(cls29)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls30 = unlencl[:,0][2:1101] print(len(cls30)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls31 = unlencl[:,0][2:1101] print(len(cls31)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls32 = unlencl[:,0][2:1101] print(len(cls32)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls33 = unlencl[:,0][2:1101] print(len(cls33)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls34 = unlencl[:,0][2:1101] print(len(cls34)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls35 = unlencl[:,0][2:1101] print(len(cls35)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls36 = unlencl[:,0][2:1101] print(len(cls36)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls37 = unlencl[:,0][2:1101] print(len(cls37)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls38 = unlencl[:,0][2:1101] print(len(cls38)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls39 = unlencl[:,0][2:1101] print(len(cls39)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls40 = unlencl[:,0][2:1101] print(len(cls40)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls41 = unlencl[:,0][2:1101] print(len(cls41)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls42 = unlencl[:,0][2:1101] print(len(cls42)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls43 = unlencl[:,0][2:1101] print(len(cls43)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls44 = unlencl[:,0][2:1101] print(len(cls44)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls45 = unlencl[:,0][2:1101] print(len(cls45)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls46 = unlencl[:,0][2:1101] print(len(cls46)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls47 = unlencl[:,0][2:1101] print(len(cls47)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls48 = unlencl[:,0][2:1101] print(len(cls48)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls49 = unlencl[:,0][2:1101] print(len(cls49)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls50 = unlencl[:,0][2:1101] print(len(cls50)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls51 = unlencl[:,0][2:1101] print(len(cls51)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls52 = unlencl[:,0][2:1101] print(len(cls52)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls53 = unlencl[:,0][2:1101] print(len(cls53)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls54 = unlencl[:,0][2:1101] print(len(cls54)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls55 = unlencl[:,0][2:1101] print(len(cls55)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls56 = unlencl[:,0][2:1101] print(len(cls56)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls57 = unlencl[:,0][2:1101] print(len(cls57)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls58 = unlencl[:,0][2:1101] print(len(cls58)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls59 = unlencl[:,0][2:1101] print(len(cls59)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls60 = unlencl[:,0][2:1101] print(len(cls60)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls61 = unlencl[:,0][2:1101] print(len(cls61)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls62 = unlencl[:,0][2:1101] print(len(cls62)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls63 = unlencl[:,0][2:1101] print(len(cls63)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls64 = unlencl[:,0][2:1101] print(len(cls64)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls65 = unlencl[:,0][2:1101] print(len(cls65)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls66 = unlencl[:,0][2:1101] print(len(cls66)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls67 = unlencl[:,0][2:1101] print(len(cls67)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls68 = unlencl[:,0][2:1101] print(len(cls68)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls69 = unlencl[:,0][2:1101] print(len(cls69)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls70 = unlencl[:,0][2:1101] print(len(cls70)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls71 = unlencl[:,0][2:1101] print(len(cls71)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls72 = unlencl[:,0][2:1101] print(len(cls72)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls73 = unlencl[:,0][2:1101] print(len(cls73)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls74 = unlencl[:,0][2:1101] print(len(cls74)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls75 = unlencl[:,0][2:1101] print(len(cls75)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls76 = unlencl[:,0][2:1101] print(len(cls76)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls77 = unlencl[:,0][2:1101] print(len(cls77)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls78 = unlencl[:,0][2:1101] print(len(cls78)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls79 = unlencl[:,0][2:1101] print(len(cls79)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls80 = unlencl[:,0][2:1101] print(len(cls80)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls81 = unlencl[:,0][2:1101] print(len(cls81)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls82 = unlencl[:,0][2:1101] print(len(cls82)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls83 = unlencl[:,0][2:1101] print(len(cls83)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls84 = unlencl[:,0][2:1101] print(len(cls84)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls85 = unlencl[:,0][2:1101] print(len(cls85)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls86 = unlencl[:,0][2:1101] print(len(cls86)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls87 = unlencl[:,0][2:1101] print(len(cls87)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls88 = unlencl[:,0][2:1101] print(len(cls88)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls89 = unlencl[:,0][2:1101] print(len(cls89)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls90 = unlencl[:,0][2:1101] print(len(cls90)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls91 = unlencl[:,0][2:1101] print(len(cls91)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls92 = unlencl[:,0][2:1101] print(len(cls92)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls93 = unlencl[:,0][2:1101] print(len(cls93)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls94 = unlencl[:,0][2:1101] print(len(cls94)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls95 = unlencl[:,0][2:1101] print(len(cls95)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls96 = unlencl[:,0][2:1101] print(len(cls96)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls97 = unlencl[:,0][2:1101] print(len(cls97)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls98 = unlencl[:,0][2:1101] print(len(cls98)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls99 = unlencl[:,0][2:1101] print(len(cls99)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls100 = unlencl[:,0][2:1101] print(len(cls100)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls101 = unlencl[:,0][2:1101] print(len(cls101)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls102 = unlencl[:,0][2:1101] print(len(cls102)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls103 = unlencl[:,0][2:1101] print(len(cls103)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls104 = unlencl[:,0][2:1101] print(len(cls104)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls105 = unlencl[:,0][2:1101] print(len(cls105)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls106 = unlencl[:,0][2:1101] print(len(cls106)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls107 = unlencl[:,0][2:1101] print(len(cls107)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls108 = unlencl[:,0][2:1101] print(len(cls108)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls109 = unlencl[:,0][2:1101] print(len(cls109)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls110 = unlencl[:,0][2:1101] print(len(cls110)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls111 = unlencl[:,0][2:1101] print(len(cls111)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls112 = unlencl[:,0][2:1101] print(len(cls112)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls113 = unlencl[:,0][2:1101] print(len(cls113)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls114 = unlencl[:,0][2:1101] print(len(cls114)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls115 = unlencl[:,0][2:1101] print(len(cls115)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls116 = unlencl[:,0][2:1101] print(len(cls116)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls117 = unlencl[:,0][2:1101] print(len(cls117)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls118 = unlencl[:,0][2:1101] print(len(cls118)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls119 = unlencl[:,0][2:1101] print(len(cls119)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls120 = unlencl[:,0][2:1101] print(len(cls120)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls121 = unlencl[:,0][2:1101] print(len(cls121)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls122 = unlencl[:,0][2:1101] print(len(cls122)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls123 = unlencl[:,0][2:1101] print(len(cls123)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls124 = unlencl[:,0][2:1101] print(len(cls124)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls125 = unlencl[:,0][2:1101] print(len(cls125)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls126 = unlencl[:,0][2:1101] print(len(cls126)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls127 = unlencl[:,0][2:1101] print(len(cls127)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls128 = unlencl[:,0][2:1101] print(len(cls128)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls129 = unlencl[:,0][2:1101] print(len(cls129)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls130 = unlencl[:,0][2:1101] print(len(cls130)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls131 = unlencl[:,0][2:1101] print(len(cls131)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls132 = unlencl[:,0][2:1101] print(len(cls132)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls133 = unlencl[:,0][2:1101] print(len(cls133)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls134 = unlencl[:,0][2:1101] print(len(cls134)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls135 = unlencl[:,0][2:1101] print(len(cls135)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls136 = unlencl[:,0][2:1101] print(len(cls136)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls137 = unlencl[:,0][2:1101] print(len(cls137)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls138 = unlencl[:,0][2:1101] print(len(cls138)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls139 = unlencl[:,0][2:1101] print(len(cls139)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls140 = unlencl[:,0][2:1101] print(len(cls140)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls141 = unlencl[:,0][2:1101] print(len(cls141)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls142 = unlencl[:,0][2:1101] print(len(cls142)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls143 = unlencl[:,0][2:1101] print(len(cls143)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls144 = unlencl[:,0][2:1101] print(len(cls144)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls145 = unlencl[:,0][2:1101] print(len(cls145)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls146 = unlencl[:,0][2:1101] print(len(cls146)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls147 = unlencl[:,0][2:1101] print(len(cls147)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls148 = unlencl[:,0][2:1101] print(len(cls148)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls149 = unlencl[:,0][2:1101] print(len(cls149)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls150 = unlencl[:,0][2:1101] print(len(cls150)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls151 = unlencl[:,0][2:1101] print(len(cls151)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls152 = unlencl[:,0][2:1101] print(len(cls152)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls153 = unlencl[:,0][2:1101] print(len(cls153)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls154 = unlencl[:,0][2:1101] print(len(cls154)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls155 = unlencl[:,0][2:1101] print(len(cls155)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls156 = unlencl[:,0][2:1101] print(len(cls156)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls157 = unlencl[:,0][2:1101] print(len(cls157)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls158 = unlencl[:,0][2:1101] print(len(cls158)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls159 = unlencl[:,0][2:1101] print(len(cls159)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls160 = unlencl[:,0][2:1101] print(len(cls160)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls161 = unlencl[:,0][2:1101] print(len(cls161)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls162 = unlencl[:,0][2:1101] print(len(cls162)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls163 = unlencl[:,0][2:1101] print(len(cls163)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls164 = unlencl[:,0][2:1101] print(len(cls164)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls165 = unlencl[:,0][2:1101] print(len(cls165)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls166 = unlencl[:,0][2:1101] print(len(cls166)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls167 = unlencl[:,0][2:1101] print(len(cls167)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls168 = unlencl[:,0][2:1101] print(len(cls168)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls169 = unlencl[:,0][2:1101] print(len(cls169)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls170 = unlencl[:,0][2:1101] print(len(cls170)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls171 = unlencl[:,0][2:1101] print(len(cls171)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls172 = unlencl[:,0][2:1101] print(len(cls172)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls173 = unlencl[:,0][2:1101] print(len(cls173)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls174 = unlencl[:,0][2:1101] print(len(cls174)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls175 = unlencl[:,0][2:1101] print(len(cls175)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls176 = unlencl[:,0][2:1101] print(len(cls176)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls177 = unlencl[:,0][2:1101] print(len(cls177)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls178 = unlencl[:,0][2:1101] print(len(cls178)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls179 = unlencl[:,0][2:1101] print(len(cls179)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls180 = unlencl[:,0][2:1101] print(len(cls180)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls181 = unlencl[:,0][2:1101] print(len(cls181)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls182 = unlencl[:,0][2:1101] print(len(cls182)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls183 = unlencl[:,0][2:1101] print(len(cls183)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls184 = unlencl[:,0][2:1101] print(len(cls184)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls185 = unlencl[:,0][2:1101] print(len(cls185)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls186 = unlencl[:,0][2:1101] print(len(cls186)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls187 = unlencl[:,0][2:1101] print(len(cls187)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls188 = unlencl[:,0][2:1101] print(len(cls188)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls189 = unlencl[:,0][2:1101] print(len(cls189)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls190 = unlencl[:,0][2:1101] print(len(cls190)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls191 = unlencl[:,0][2:1101] print(len(cls191)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls192 = unlencl[:,0][2:1101] print(len(cls192)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls193 = unlencl[:,0][2:1101] print(len(cls193)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls194 = unlencl[:,0][2:1101] print(len(cls194)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls195 = unlencl[:,0][2:1101] print(len(cls195)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls196 = unlencl[:,0][2:1101] print(len(cls196)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls197 = unlencl[:,0][2:1101] print(len(cls197)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls198 = unlencl[:,0][2:1101] print(len(cls198)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls199 = unlencl[:,0][2:1101] print(len(cls199)) pars = camb.CAMBparams() #This function sets up CosmoMC-like settings, with one massive neutrino and helium set using BBN consistency pars.set_cosmology(H0=67.5, ombh2=0.005, omch2=0.122, mnu=0.06, omk=0, tau=0.06) pars.InitPower.set_params(ns=0.965, r=0) #pars.set_for_lmax(514, lens_potential_accuracy=0) #calculate results for these parameters results = camb.get_results(pars) #get dictionary of CAMB power spectra powers = results.get_cmb_power_spectra(pars) unlencl = powers['unlensed_scalar'] ls = np.arange(unlencl.shape[0]) print(ls) print(len(ls)) # # plot of spectrum cls200 = unlencl[:,0][2:1101] print(len(cls200)) """ 0.005 0.00522613065327 0.00545226130653 0.0056783919598 0.00590452261307 0.00613065326633 0.0063567839196 0.00658291457286 0.00680904522613 0.0070351758794 0.00726130653266 0.00748743718593 0.0077135678392 0.00793969849246 0.00816582914573 0.00839195979899 0.00861809045226 0.00884422110553 0.00907035175879 0.00929648241206 0.00952261306533 0.00974874371859 0.00997487437186 0.0102010050251 0.0104271356784 0.0106532663317 0.0108793969849 0.0111055276382 0.0113316582915 0.0115577889447 0.011783919598 0.0120100502513 0.0122361809045 0.0124623115578 0.0126884422111 0.0129145728643 0.0131407035176 0.0133668341709 0.0135929648241 0.0138190954774 0.0140452261307 0.0142713567839 0.0144974874372 0.0147236180905 0.0149497487437 0.015175879397 0.0154020100503 0.0156281407035 0.0158542713568 0.0160804020101 0.0163065326633 0.0165326633166 0.0167587939698 0.0169849246231 0.0172110552764 0.0174371859296 0.0176633165829 0.0178894472362 0.0181155778894 0.0183417085427 0.018567839196 0.0187939698492 0.0190201005025 0.0192462311558 0.019472361809 0.0196984924623 0.0199246231156 0.0201507537688 0.0203768844221 0.0206030150754 0.0208291457286 0.0210552763819 0.0212814070352 0.0215075376884 0.0217336683417 0.021959798995 0.0221859296482 0.0224120603015 0.0226381909548 0.022864321608 0.0230904522613 0.0233165829146 0.0235427135678 0.0237688442211 0.0239949748744 0.0242211055276 0.0244472361809 0.0246733668342 0.0248994974874 0.0251256281407 0.025351758794 0.0255778894472 0.0258040201005 0.0260301507538 0.026256281407 0.0264824120603 0.0267085427136 0.0269346733668 0.0271608040201 0.0273869346734 0.0276130653266 0.0278391959799 0.0280653266332 0.0282914572864 0.0285175879397 0.028743718593 0.0289698492462 0.0291959798995 0.0294221105528 0.029648241206 0.0298743718593 0.0301005025126 0.0303266331658 0.0305527638191 0.0307788944724 0.0310050251256 0.0312311557789 0.0314572864322 0.0316834170854 0.0319095477387 0.032135678392 0.0323618090452 0.0325879396985 0.0328140703518 0.033040201005 0.0332663316583 0.0334924623116 0.0337185929648 0.0339447236181 0.0341708542714 0.0343969849246 0.0346231155779 0.0348492462312 0.0350753768844 0.0353015075377 0.035527638191 0.0357537688442 0.0359798994975 0.0362060301508 0.036432160804 0.0366582914573 0.0368844221106 0.0371105527638 0.0373366834171 0.0375628140704 0.0377889447236 0.0380150753769 0.0382412060302 0.0384673366834 0.0386934673367 0.0389195979899 0.0391457286432 0.0393718592965 0.0395979899497 0.039824120603 0.0400502512563 0.0402763819095 0.0405025125628 0.0407286432161 0.0409547738693 0.0411809045226 0.0414070351759 0.0416331658291 0.0418592964824 0.0420854271357 0.0423115577889 0.0425376884422 0.0427638190955 0.0429899497487 0.043216080402 0.0434422110553 0.0436683417085 0.0438944723618 0.0441206030151 0.0443467336683 0.0445728643216 0.0447989949749 0.0450251256281 0.0452512562814 0.0454773869347 0.0457035175879 0.0459296482412 0.0461557788945 0.0463819095477 0.046608040201 0.0468341708543 0.0470603015075 0.0472864321608 0.0475125628141 0.0477386934673 0.0479648241206 0.0481909547739 0.0484170854271 0.0486432160804 0.0488693467337 0.0490954773869 0.0493216080402 0.0495477386935 0.0497738693467 0.05 """ # In[50]: cl_array = np.array([cls0, cls1, cls2, cls3, cls4, cls5, cls6, cls7, cls8, cls9, cls10, cls11, cls12, cls13, cls14, cls15, cls16, cls17, cls18, cls19, cls20, cls21, cls22, cls23, cls24, cls25, cls26, cls27, cls28, cls29, cls30, cls31, cls32, cls33, cls34, cls35, cls36, cls37, cls38, cls39, cls40, cls41, cls42, cls43, cls44, cls45, cls46, cls47, cls48, cls49, cls50, cls51, cls52, cls53, cls54, cls55, cls56, cls57, cls58, cls59, cls60, cls61, cls62, cls63, cls64, cls65, cls66, cls67, cls68, cls69, cls70, cls71, cls72, cls73, cls74, cls75, cls76, cls77, cls78, cls79, cls80, cls81, cls82, cls83, cls84, cls85, cls86, cls87, cls88, cls89, cls90, cls91, cls92, cls93, cls94, cls95, cls96, cls97, cls98, cls99, cls100, cls101, cls102, cls103, cls104, cls105, cls106, cls107, cls108, cls109, cls110, cls111, cls112, cls113, cls114, cls115, cls116, cls117, cls118, cls119, cls120, cls121, cls122, cls123, cls124, cls125, cls126, cls127, cls128, cls129, cls130, cls131, cls132, cls133, cls134, cls135, cls136, cls137, cls138, cls139, cls140, cls141, cls142, cls143, cls144, cls145, cls146, cls147, cls148, cls149, cls150, cls151, cls152, cls153, cls154, cls155, cls156, cls157, cls158, cls159, cls160, cls161, cls162, cls163, cls164, cls165, cls166, cls167, cls168, cls169, cls170, cls171, cls172, cls173, cls174, cls175, cls176, cls177, cls178, cls179, cls180, cls181, cls182, cls183, cls184, cls185, cls186, cls187, cls188, cls189, cls190, cls191, cls192, cls193, cls194, cls195, cls196, cls197, cls198, cls199, cls200]) # In[51]: print(cl_array.shape) # In[52]: f = "CAMB_cl_varyBaryon_lmax1100varyFeb2016.npy" np.save(f, cl_array)
mit
Upward-Spiral-Science/team1
code/test_assumptions.py
1
1525
import numpy as np import matplotlib.pyplot as plt import urllib2 #%matplotlib inline sample_size = 1000 np.random.seed(1) url = ('https://raw.githubusercontent.com/Upward-Spiral-Science' '/data/master/syn-density/output.csv') data = urllib2.urlopen(url) csv = np.genfromtxt(data, delimiter=",")[1:] csv_rand = None for i in range (1, sample_size): #Randomly sample from dataset a = np.random.permutation(np.arange(csv.shape[0]))[:100] csv_rand_sample = csv[a] # Normalize mean_unmask = np.mean(csv_rand_sample[:,3]) std_unmask = np.std(csv_rand_sample[:,3]) csv_rand_sample[:,3] = (csv_rand_sample[:,3]-mean_unmask)/std_unmask #Stack matrix if i == 1: csv_rand = csv_rand_sample else: csv_rand = np.dstack((csv_rand,csv_rand_sample)) #Average across random samples csv_rand = np.mean(csv_rand,axis=2) #Independence Assumption covar = np.cov(csv_rand_sample) plt.figure(figsize=(7,7)) plt.imshow(covar) plt.title('Covariance of Synapse Density dataset') plt.colorbar() plt.show() diag = covar.diagonal()*np.eye(covar.shape[0]) hollow = covar-diag d_det = np.linalg.slogdet(diag)[1] h_det = np.linalg.slogdet(hollow)[1] print d_det print h_det plt.figure(figsize=(11,8)) plt.subplot(121) plt.imshow(diag) plt.clim([0, np.max(covar)]) plt.title('Determinant of on-diagonal: ' + str(d_det)) plt.subplot(122) plt.imshow(hollow) plt.clim([0, np.max(covar)]) plt.title('Determinant of off-diagonal: ' + str(h_det)) plt.show() print "Ratio of on and off-diagonal determinants: " + str(d_det/h_det)
apache-2.0
bottydim/detect-credit-card-fraud
ccfd_dnn/model_weight.py
1
20628
import os os.environ['CUDA_LAUNCH_BLOCKING'] = '1' import pandas as pd import matplotlib import numpy as np import math import matplotlib.pyplot as plt from sklearn.preprocessing import Imputer from sklearn.cross_validation import train_test_split from sklearn import preprocessing import plotly.tools as tls import pandas as pd from sqlalchemy import create_engine # database connection import datetime as dt import io import logging import plotly.plotly as py # interactive graphing from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot from plotly.graph_objs import Bar, Scatter, Marker, Layout from heraspy.model import HeraModel np.random.seed(1337) import theano import keras from keras.preprocessing.sequence import pad_sequences from keras.models import Model,model_from_yaml from keras.layers import Input, Dense, GRU, LSTM, TimeDistributed, Masking,merge from model import * import argparse import sys if __name__ == "__main__": t_start = dt.datetime.now() parser = argparse.ArgumentParser(prog='Weighted Model') parser.add_argument('-t','--table',required=True) args = parser.parse_args() ####################################DATA SOURCE################################ table = vars(args)['table'] # table = 'data_trim' # rsl_file = './data/gs_results_trim.csv' # rsl_file = './data/psql_data_trim.csv' # table = 'data_little_enc' # rsl_file = './data/gs_results_little.csv' # table = 'data_more' # rsl_file = './data/gs_results_more.csv' # table = 'auth' # rsl_file = './data/auth.csv' events_tbl = 'event' events_tbl = None rsl_file = './data/psql_{table}.csv'.format(table=table) ################################################################################ print "Commencing..." data_dir = './data/' evt_name = 'Featurespace_events_output.csv' auth_name = 'Featurespace_auths_output.csv' db_name = 'c1_agg.db' address = "postgresql+pg8000://script@localhost:5432/ccfd" # disk_engine = create_engine('sqlite:///'+data_dir+db_name,convert_unicode=True) # disk_engine.raw_connection().connection.text_factory = str disk_engine = create_engine(address) #######################Settings############################################# samples_per_epoch = trans_num_table(table,disk_engine,mode='train',trans_mode='train') # epoch_limit = 10000 # samples_per_epoch = epoch_limit # user_sample_size = 8000 epoch_limit = samples_per_epoch user_sample_size = None nb_epoch = 300 fraud_w_list = [1000.] ##########ENCODERS CONF tbl_src = 'auth' # tbl_src = table tbl_evnt = 'event' ################################## batch_size = 300 batch_size_val = 1000 print "SAMPLES per epoch:",samples_per_epoch print "User sample size:",user_sample_size print 'sequence length size',batch_size # samples_per_epoch = 1959 # table = 'data_trim' # samples_per_epoch = 485 lbl_pad_val = 2 pad_val = 0 # dropout_W_list = [0.3] dropout_W_list = [0.4,0.5,0.6,0.7] # dropout_W_list = [0.15,0.3,0.4,0.8] input_dim = 44 hid_dims = [320] num_l = [7] lr_s = [2.5e-4] # lr_s = [1.25e-4,6e-5] # lr_s = [1e-2,1e-3,1e-4] # lr_s = [1e-1,1e-2,1e-3] num_opt = 1 opts = lambda x,lr:[keras.optimizers.RMSprop(lr=lr, rho=0.9, epsilon=1e-08), # keras.optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08), # keras.optimizers.Nadam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) ][x] # add_info = str(int(seq_len_param))+'_class_w_'+str(fraud_w) print 'Populating encoders' path_encoders ='./data/encoders/{tbl_src}+{tbl_evnt}'.format(tbl_src=tbl_src,tbl_evnt=tbl_evnt) if os.path.exists(path_encoders): encoders = load_encoders(path_encoders) else: encoders = populate_encoders_scale(tbl_src,disk_engine,tbl_evnt) with open(path_encoders, 'wb') as output: pickle.dump(encoders, output, pickle.HIGHEST_PROTOCOL) print 'ENCODERS SAVED to {path}!'.format(path=path_encoders) # sys.exit() gru_dict = {} lstm_dict = {} for fraud_w in fraud_w_list: add_info = 'Mask=pad_class_w_'+str(fraud_w)+'ES-OFF' class_weight = {0 : 1., 1: fraud_w, 2: 0.} for dropout_W in dropout_W_list: for hidden_dim in hid_dims: # gru for opt_id in range(num_opt): for lr in lr_s: optimizer = opts(opt_id,lr) for num_layers in num_l: for rnn in ['gru']: short_title = 'bi_'+rnn.upper()+'_'+str(hidden_dim)+'_'+str(num_layers)+'_DO-'+str(dropout_W)+'_w'+str(class_weight[1]) title = 'Bidirectional_Class'+str(class_weight[1])+'_'+rnn.upper()+'_'+str(hidden_dim)+'_'+str(num_layers)+'_'+str(type(optimizer).__name__)+'_'+str(lr)+'_epochs_'+str(nb_epoch)+'_DO-'+str(dropout_W) print title input_layer = Input(shape=(int(seq_len_param), input_dim),name='main_input') mask = Masking(mask_value=pad_val)(input_layer) x = mask for i in range(num_layers): if rnn == 'gru': prev_frw = GRU(hidden_dim,#input_length=50, return_sequences=True,go_backwards=False,stateful=False, unroll=False,consume_less='gpu', init='glorot_uniform', inner_init='orthogonal', activation='tanh', inner_activation='hard_sigmoid', W_regularizer=None, U_regularizer=None, b_regularizer=None, dropout_W=dropout_W, dropout_U=0.0)(x) prev_bck = GRU(hidden_dim,#input_length=50, return_sequences=True,go_backwards=True,stateful=False, unroll=False,consume_less='gpu', init='glorot_uniform', inner_init='orthogonal', activation='tanh', inner_activation='hard_sigmoid', W_regularizer=None, U_regularizer=None, b_regularizer=None, dropout_W=dropout_W, dropout_U=0.0)(x) else: prev_frw = LSTM(hidden_dim, return_sequences=True,go_backwards=False,stateful=False, init='glorot_uniform', inner_init='orthogonal', forget_bias_init='one', activation='tanh', inner_activation='hard_sigmoid', W_regularizer=None, U_regularizer=None, b_regularizer=None, dropout_W=dropout_W, dropout_U=0.0)(x) prev_bck = LSTM(hidden_dim, return_sequences=True,go_backwards=True,stateful=False, init='glorot_uniform', inner_init='orthogonal', forget_bias_init='one', activation='tanh', inner_activation='hard_sigmoid', W_regularizer=None, U_regularizer=None, b_regularizer=None, dropout_W=dropout_W, dropout_U=0.0)(x) x = merge([prev_frw, prev_bck], mode='concat') output_layer = TimeDistributed(Dense(3,activation='softmax'))(x) model = Model(input=[input_layer],output=[output_layer]) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'], sample_weight_mode="temporal") ########save architecture ###### arch_dir = './data/models/archs/'+short_title+'.yml' yaml_string = model.to_yaml() with open(arch_dir, 'wb') as output: pickle.dump(yaml_string, output, pickle.HIGHEST_PROTOCOL) print 'model saved!' ############## user_mode = 'train' trans_mode = 'train' data_gen = data_generator(user_mode,trans_mode,disk_engine,encoders,table=table, batch_size=batch_size,usr_ratio=80,class_weight=class_weight,lbl_pad_val = lbl_pad_val, pad_val = pad_val, sub_sample=user_sample_size,epoch_size=epoch_limit,events_tbl=events_tbl) # sub_sample=user_sample_size,epoch_size=samples_per_epoch) ########validation data print 'Generating Validation set!' user_mode = 'test' trans_mode = 'test' val_gen = data_generator(user_mode,trans_mode,disk_engine,encoders,table=table, batch_size=batch_size_val,usr_ratio=80,class_weight=class_weight,lbl_pad_val = lbl_pad_val, pad_val = pad_val, sub_sample=None,epoch_size=None,events_tbl=events_tbl) validation_data = next(val_gen) print '################GENERATED#######################' ###############CALLBACKS patience = 30 early_Stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=patience, verbose=0, mode='auto') save_path = './data/models/'+table+'/' var_name = '.{epoch:02d}-{val_loss:.5f}.hdf5' checkpoint = keras.callbacks.ModelCheckpoint(save_path+short_title+var_name, monitor='val_loss', verbose=1, save_best_only=True, mode='auto') root_url = 'http://localhost:9000' remote_log = keras.callbacks.RemoteMonitor(root=root_url) # callbacks = [early_Stop,checkpoint] callbacks = [early_Stop,checkpoint,remote_log] callbacks = [] history = model.fit_generator(data_gen, samples_per_epoch, nb_epoch, verbose=1, callbacks=callbacks,validation_data=validation_data, nb_val_samples=None, class_weight=None, max_q_size=10000) py.sign_in('bottydim', 'o1kuyms9zv') auc_list = [] print '#########################TRAIN STATS################' user_mode = 'train' trans_mode = 'train' val_samples = trans_num_table(table,disk_engine,mode=user_mode,trans_mode=trans_mode) print '# samples',val_samples plt_filename = './figures/GS/'+table+'/'+'ROC_'+user_mode+'_'+trans_mode+'_'+title+'_'+add_info+".png" data_gen = data_generator(user_mode,trans_mode,disk_engine,encoders,table=table, batch_size=batch_size,usr_ratio=80,class_weight=None,lbl_pad_val = lbl_pad_val, pad_val = pad_val,events_tbl=events_tbl) eval_list = eval_auc_generator(model, data_gen, val_samples, max_q_size=10000,plt_filename=plt_filename) auc_val = eval_list[0] clc_report = eval_list[1] acc = eval_list[2] print "AUC:",auc_val print 'CLassification report' print clc_report print 'Accuracy' print acc auc_list.append(str(auc_val)) print '##################EVALUATION USERS#########################' user_mode = 'test' trans_mode = 'train' val_samples = trans_num_table(table,disk_engine,mode=user_mode,trans_mode=trans_mode) print '# samples',val_samples plt_filename = './figures/GS/'+table+'/'+'ROC_'+user_mode+'_'+trans_mode+'_'+title+'_'+add_info+".png" eval_gen = data_generator(user_mode,trans_mode,disk_engine,encoders,table=table, batch_size=batch_size,usr_ratio=80,class_weight=None,lbl_pad_val = lbl_pad_val, pad_val = pad_val,events_tbl=events_tbl) eval_list = eval_auc_generator(model, eval_gen, val_samples, max_q_size=10000,plt_filename=plt_filename) auc_val = eval_list[0] clc_report = eval_list[1] acc = eval_list[2] print "AUC:",auc_val print 'CLassification report' print clc_report print 'Accuracy' print acc auc_list.append(str(auc_val)) print '#####################################################' print '##################EVALUATION Transactions#########################' user_mode = 'train' trans_mode = 'test' val_samples = trans_num_table(table,disk_engine,mode=user_mode,trans_mode=trans_mode) print '# samples',val_samples plt_filename = './figures/GS/'+table+'/'+'ROC_'+user_mode+'_'+trans_mode+'_'+title+'_'+add_info+".png" eval_gen = data_generator(user_mode,trans_mode,disk_engine,encoders,table=table, batch_size=batch_size,usr_ratio=80,class_weight=None,lbl_pad_val = lbl_pad_val, pad_val = pad_val,events_tbl=events_tbl) eval_list = eval_auc_generator(model, eval_gen, val_samples, max_q_size=10000,plt_filename=plt_filename) auc_val = eval_list[0] clc_report = eval_list[1] acc = eval_list[2] print "AUC:",auc_val print 'CLassification report' print clc_report print 'Accuracy' print acc auc_list.append(str(auc_val)) print '#####################################################' print '##################EVALUATION Pure#########################' user_mode = 'test' trans_mode = 'test' val_samples = trans_num_table(table,disk_engine,mode=user_mode,trans_mode=trans_mode) print '# samples',val_samples plt_filename = './figures/GS/'+table+'/'+'ROC_'+user_mode+'_'+trans_mode+'_'+title+'_'+add_info+".png" eval_gen = data_generator(user_mode,trans_mode,disk_engine,encoders,table=table, batch_size=batch_size,usr_ratio=80,class_weight=None,lbl_pad_val = lbl_pad_val, pad_val = pad_val,events_tbl=events_tbl) eval_list = eval_auc_generator(model, eval_gen, val_samples, max_q_size=10000,plt_filename=plt_filename) auc_val = eval_list[0] clc_report = eval_list[1] acc = eval_list[2] print "AUC:",auc_val print 'CLassification report' print clc_report print 'Accuracy' print acc auc_list.append(str(auc_val)) print '#####################################################' with io.open(rsl_file, 'a', encoding='utf-8') as file: auc_string = ','.join(auc_list) title_csv = title.replace('_',',')+','+str(history.history['acc'][-1])+','+str(history.history['loss'][-1])+','+str(auc_val)+','+str(acc)+','+auc_string+'\n' file.write(unicode(title_csv)) print 'logged @ {file}'.format(file=rsl_file) trim_point = -15 fig = { 'data': [Scatter( x=history.epoch[trim_point:], y=history.history['loss'][trim_point:])], 'layout': {'title': title} } py.image.save_as(fig,filename='./results/figures/'+table+'/'+short_title+'_'+'LOSS'+'_'+add_info+".png") trim_point = 0 fig = { 'data': [Scatter( x=history.epoch[trim_point:], y=history.history['loss'][trim_point:])], 'layout': {'title': title} } py.image.save_as(fig,filename='./results/figures/'+table+'/'+short_title+'_'+'LOSS'+'_'+'FULL'+".png") # iplot(fig,filename='figures/'+title,image='png') # title = title.replace('Loss','Acc') fig = { 'data': [Scatter( x=history.epoch[trim_point:], y=history.history['acc'][trim_point:])], 'layout': {'title': title} } filename_val='./results/figures/'+table+'/'+short_title+'_'+'ACC'+'_'+add_info+".png" py.image.save_as(fig,filename=filename_val) print 'exported @',filename_val fig = { 'data': [Scatter( x=history.epoch[trim_point:], y=history.history['val_loss'][trim_point:])], 'layout': {'title': title} } py.image.save_as(fig,filename='./results/figures/'+table+'/'+short_title+'_'+'VAL LOSS'+'_'+add_info+".png") print 'time taken: {time}'.format(time=days_hours_minutes_seconds(dt.datetime.now()-t_start))
mit
zhenv5/scikit-learn
examples/classification/plot_lda.py
70
2413
""" ==================================================================== Normal and Shrinkage Linear Discriminant Analysis for classification ==================================================================== Shows how shrinkage improves classification. """ from __future__ import division import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs from sklearn.discriminant_analysis import LinearDiscriminantAnalysis n_train = 20 # samples for training n_test = 200 # samples for testing n_averages = 50 # how often to repeat classification n_features_max = 75 # maximum number of features step = 4 # step size for the calculation def generate_data(n_samples, n_features): """Generate random blob-ish data with noisy features. This returns an array of input data with shape `(n_samples, n_features)` and an array of `n_samples` target labels. Only one feature contains discriminative information, the other features contain only noise. """ X, y = make_blobs(n_samples=n_samples, n_features=1, centers=[[-2], [2]]) # add non-discriminative features if n_features > 1: X = np.hstack([X, np.random.randn(n_samples, n_features - 1)]) return X, y acc_clf1, acc_clf2 = [], [] n_features_range = range(1, n_features_max + 1, step) for n_features in n_features_range: score_clf1, score_clf2 = 0, 0 for _ in range(n_averages): X, y = generate_data(n_train, n_features) clf1 = LinearDiscriminantAnalysis(solver='lsqr', shrinkage='auto').fit(X, y) clf2 = LinearDiscriminantAnalysis(solver='lsqr', shrinkage=None).fit(X, y) X, y = generate_data(n_test, n_features) score_clf1 += clf1.score(X, y) score_clf2 += clf2.score(X, y) acc_clf1.append(score_clf1 / n_averages) acc_clf2.append(score_clf2 / n_averages) features_samples_ratio = np.array(n_features_range) / n_train plt.plot(features_samples_ratio, acc_clf1, linewidth=2, label="Linear Discriminant Analysis with shrinkage", color='r') plt.plot(features_samples_ratio, acc_clf2, linewidth=2, label="Linear Discriminant Analysis", color='g') plt.xlabel('n_features / n_samples') plt.ylabel('Classification accuracy') plt.legend(loc=1, prop={'size': 12}) plt.suptitle('Linear Discriminant Analysis vs. \ shrinkage Linear Discriminant Analysis (1 discriminative feature)') plt.show()
bsd-3-clause
pinkavaj/gnuradio
gr-fec/python/fec/polar/channel_construction_bec.py
22
8068
#!/usr/bin/env python # # Copyright 2015 Free Software Foundation, Inc. # # GNU Radio 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, or (at your option) # any later version. # # GNU Radio 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 GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # import numpy as np import helper_functions as hf def bec_channel(eta): ''' binary erasure channel (BEC) for each y e Y W(y|0) * W(y|1) = 0 or W(y|0) = W(y|1) transistions are 1 -> 1 or 0 -> 0 or {0, 1} -> ? (erased symbol) ''' # looks like BSC but should be interpreted differently. w = np.array((1 - eta, eta, 1 - eta), dtype=float) return w def odd_rec(iwn): return iwn ** 2 def even_rec(iwn): return 2 * iwn - iwn ** 2 def calc_one_recursion(iw0): iw1 = np.zeros(2 * len(iw0)) # double values for i in range(len(iw0)): # careful indices screw you because paper is '1' based :( iw1[2 * i] = odd_rec(iw0[i]) iw1[2 * i + 1] = even_rec(iw0[i]) return iw1 def calculate_bec_channel_capacities_loop(initial_channel, block_power): # compare [0, Arikan] eq. 6 iw = np.array([initial_channel, ], dtype=float) for i in range(block_power): iw = calc_one_recursion(iw) return iw def calc_vector_capacities_one_recursion(iw0): degraded = odd_rec(iw0) upgraded = even_rec(iw0) iw1 = np.empty(2 * len(iw0), dtype=degraded.dtype) iw1[0::2] = degraded iw1[1::2] = upgraded return iw1 def calculate_bec_channel_capacities_vector(initial_channel, block_power): # compare [0, Arikan] eq. 6 # this version is ~ 180 times faster than the loop version with 2**22 synthetic channels iw = np.array([initial_channel, ], dtype=float) for i in range(block_power): iw = calc_vector_capacities_one_recursion(iw) return iw def calculate_bec_channel_capacities(eta, block_size): # compare [0, Arikan] eq. 6 iw = 1 - eta # holds for BEC as stated in paper lw = hf.power_of_2_int(block_size) return calculate_bec_channel_capacities_vector(iw, lw) def calculate_z_parameters_one_recursion(z_params): z_next = np.empty(2 * z_params.size, dtype=z_params.dtype) z_sq = z_params ** 2 z_low = 2 * z_params - z_sq z_next[0::2] = z_low z_next[1::2] = z_sq return z_next def calculate_bec_channel_z_parameters(eta, block_size): # compare [0, Arikan] eq. 38 block_power = hf.power_of_2_int(block_size) z_params = np.array([eta, ], dtype=float) for block_size in range(block_power): z_params = calculate_z_parameters_one_recursion(z_params) return z_params def design_snr_to_bec_eta(design_snr): # minimum design snr = -1.5917 corresponds to BER = 0.5 s = 10. ** (design_snr / 10.) return np.exp(-s) def bhattacharyya_bounds(design_snr, block_size): ''' Harish Vangala, Emanuele Viterbo, Yi Hong: 'A Comparative Study of Polar Code Constructions for the AWGN Channel', 2015 In this paper it is called Bhattacharyya bounds channel construction and is abbreviated PCC-0 Best design SNR for block_size = 2048, R = 0.5, is 0dB. Compare with Arikan: 'Channel Polarization: A Method for Constructing Capacity-Achieving Codes for Symmetric Binary-Input Memoryless Channels. Proposition 5. inequalities turn into equalities for BEC channel. Otherwise they represent an upper bound. Also compare [0, Arikan] eq. 6 and 38 For BEC that translates to capacity(i) = 1 - bhattacharyya(i) :return Z-parameters in natural bit-order. Choose according to desired rate. ''' eta = design_snr_to_bec_eta(design_snr) return calculate_bec_channel_z_parameters(eta, block_size) def plot_channel_capacities(capacity, save_file=None): block_size = len(capacity) try: import matplotlib.pyplot as plt # FUN with matplotlib LaTeX fonts! http://matplotlib.org/users/usetex.html plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.rc('figure', autolayout=True) plt.plot(capacity) plt.xlim([0, block_size]) plt.ylim([-0.01, 1.01]) plt.xlabel('synthetic channel number') plt.ylabel('channel capacity') # plt.title('BEC channel construction') plt.grid() plt.gcf().set_size_inches(plt.gcf().get_size_inches() * .5) if save_file: plt.savefig(save_file) plt.show() except ImportError: pass # only plot in case matplotlib is installed def plot_average_channel_distance(save_file=None): eta = 0.5 # design_snr_to_bec_eta(-1.5917) powers = np.arange(4, 26) try: import matplotlib.pyplot as plt import matplotlib # FUN with matplotlib LaTeX fonts! http://matplotlib.org/users/usetex.html plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.rc('figure', autolayout=True) dist = [] medians = [] initial_channel = 1 - eta for p in powers: bs = int(2 ** p) capacities = calculate_bec_channel_capacities(eta, bs) avg_capacity = np.repeat(initial_channel, len(capacities)) averages = np.abs(capacities - avg_capacity) avg_distance = np.sum(averages) / float(len(capacities)) dist.append(avg_distance) variance = np.std(averages) medians.append(variance) plt.errorbar(powers, dist, yerr=medians) plt.grid() plt.xlabel(r'block size $N$') plt.ylabel(r'$\frac{1}{N} \sum_i |I(W_N^{(i)}) - 0.5|$') axes = plt.axes() tick_values = np.array(axes.get_xticks().tolist()) tick_labels = np.array(tick_values, dtype=int) tick_labels = ['$2^{' + str(i) + '}$' for i in tick_labels] plt.xticks(tick_values, tick_labels) plt.xlim((powers[0], powers[-1])) plt.ylim((0.2, 0.5001)) plt.gcf().set_size_inches(plt.gcf().get_size_inches() * .5) if save_file: plt.savefig(save_file) plt.show() except ImportError: pass def plot_capacity_histogram(design_snr, save_file=None): eta = design_snr_to_bec_eta(design_snr) # capacities = calculate_bec_channel_capacities(eta, block_size) try: import matplotlib.pyplot as plt # FUN with matplotlib LaTeX fonts! http://matplotlib.org/users/usetex.html plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.rc('figure', autolayout=True) block_sizes = [32, 128, 512] for b in block_sizes: capacities = calculate_bec_channel_capacities(eta, b) w = 1. / float(len(capacities)) weights = [w, ] * b plt.hist(capacities, bins=b, weights=weights, range=(0.95, 1.0)) plt.grid() plt.xlabel('synthetic channel capacity') plt.ylabel('normalized item count') print(plt.gcf().get_size_inches()) plt.gcf().set_size_inches(plt.gcf().get_size_inches() * .5) if save_file: plt.savefig(save_file) plt.show() except ImportError: pass def main(): print 'channel construction main' n = 11 block_size = int(2 ** n) design_snr = -1.59 eta = design_snr_to_bec_eta(design_snr) # print(calculate_bec_channel_z_parameters(eta, block_size)) # capacity = calculate_bec_channel_capacities(eta, block_size) # plot_average_channel_distance() calculate_bec_channel_z_parameters(eta, block_size) if __name__ == '__main__': main()
gpl-3.0
marshallmcdonnell/interactive_plotting
matplotlib/draggable_legend_code.py
1
3140
#!/usr/bin/env python import numpy as np import matplotlib.pyplot as _plt class DraggableLegend: def __init__(self, legend): self.legend = legend self.gotLegend = False legend.figure.canvas.mpl_connect('motion_notify_event', self.on_motion) legend.figure.canvas.mpl_connect('pick_event', self.on_picker) legend.figure.canvas.mpl_connect('button_release_event', self.on_release) legend.set_picker(self.my_legend_picker) #----------------------------------------------------# # Connected event handlers def on_motion(self, event): if self.gotLegend: dx = event.x - self.mouse_x dy = event.y - self.mouse_y loc_in_canvas = self.legend_x + dx, self.legend_y + dy loc_in_norm_axes = self.legend.parent.transAxes.inverted().transform_point(loc_in_canvas) self.legend._loc = tuple(loc_in_norm_axes) self.legend.figure.canvas.draw() def my_legend_picker(self, legend, event): return self.legend.legendPatch.contains(event) def on_picker(self, event): if event.artist == self.legend: # left-click if event.mouseevent.button == 1: self._move_legend(event) # mouse button pressed if event.mouseevent.button == 2: pass # right-click if event.mouseevent.button == 3: self._hideLegend() # mouse up if event.mouseevent.button == 'up': self._scaleUpLegendFont() # mouse down if event.mouseevent.button == 'down': self._scaleDownLegendFont() def on_release(self, event): if self.gotLegend: self.gotLegend = False #----------------------------------------------------# # Utility functions def _move_legend(self,event): bbox = self.legend.get_window_extent() self.mouse_x = event.mouseevent.x self.mouse_y = event.mouseevent.y self.legend_x = bbox.xmin self.legend_y = bbox.ymin self.gotLegend = 1 def _scaleUpLegendFont(self,size_step=4): size = self.legend.get_texts()[0].get_fontsize() size += size_step _plt.setp(self.legend.get_texts(), fontsize=size) #legend 'list' fontsize self.legend.figure.canvas.draw() def _scaleDownLegendFont(self,size_step=4): size = self.legend.get_texts()[0].get_fontsize() size -= size_step _plt.setp(self.legend.get_texts(), fontsize=size) #legend 'list' fontsize self.legend.figure.canvas.draw() def _hideLegend(self): if self.legend.get_visible(): self.legend.set_visible(False) else: self.legend.set_visible(True) self.legend.figure.canvas.draw() figure = _plt.figure() ax = figure.add_subplot(111) scatter = ax.scatter(np.random.randn(100), np.random.randn(100), label='hi') legend = ax.legend() legend = DraggableLegend(legend) _plt.show()
mit
wesm/statsmodels
scikits/statsmodels/sandbox/tsa/examples/ex_mle_garch.py
1
10649
# -*- coding: utf-8 -*- """ Created on Fri Feb 12 01:01:50 2010 Author: josef-pktd latest result ------------- all are very close garch0 has different parameterization of constant ordering of parameters is different seed 2780185 h.shape (2000,) Optimization terminated successfully. Current function value: 2093.813397 Iterations: 387 Function evaluations: 676 ggres.params [-0.6146253 0.1914537 0.01039355 0.78802188] Optimization terminated successfully. Current function value: 2093.972953 Iterations: 201 Function evaluations: 372 ggres0.params [-0.61537527 0.19635128 4.00706058] Warning: Desired error not necessarily achieveddue to precision loss Current function value: 2093.972953 Iterations: 51 Function evaluations: 551 Gradient evaluations: 110 ggres0.params [-0.61537855 0.19635265 4.00694669] Optimization terminated successfully. Current function value: 2093.751420 Iterations: 103 Function evaluations: 187 [ 0.78671519 0.19692222 0.61457171] -2093.75141963 Final Estimate: LLH: 2093.750 norm LLH: 2.093750 omega alpha1 beta1 0.7867438 0.1970437 0.6145467 long run variance comparison ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ R >>> 0.7867438/(1- 0.1970437- 0.6145467) 4.1757097302897526 Garch (gjr) asymetric, longrun var ? >>> 1/(1-0.6146253 - 0.1914537 - 0.01039355) * 0.78802188 4.2937548579245242 >>> 1/(1-0.6146253 - 0.1914537 + 0.01039355) * 0.78802188 3.8569053452140345 Garch0 >>> (1-0.61537855 - 0.19635265) * 4.00694669 0.7543830449902722 >>> errgjr4.var() #for different random seed 4.0924199964716106 todo: add code and verify, check for longer lagpolys """ import numpy as np from numpy.testing import assert_almost_equal import matplotlib.pyplot as plt import numdifftools as ndt import scikits.statsmodels.api as sm from scikits.statsmodels.sandbox import tsa from scikits.statsmodels.sandbox.tsa.garch import * # local import nobs = 1000 examples = ['garch', 'rpyfit'] if 'garch' in examples: err,h = generate_kindofgarch(nobs, [1.0, -0.95], [1.0, 0.1], mu=0.5) plt.figure() plt.subplot(211) plt.plot(err) plt.subplot(212) plt.plot(h) #plt.show() seed = 3842774 #91234 #8837708 seed = np.random.randint(9999999) print 'seed', seed np.random.seed(seed) ar1 = -0.9 err,h = generate_garch(nobs, [1.0, ar1], [1.0, 0.50], mu=0.0,scale=0.1) # plt.figure() # plt.subplot(211) # plt.plot(err) # plt.subplot(212) # plt.plot(h) # plt.figure() # plt.subplot(211) # plt.plot(err[-400:]) # plt.subplot(212) # plt.plot(h[-400:]) #plt.show() garchplot(err, h) garchplot(err[-400:], h[-400:]) np.random.seed(seed) errgjr,hgjr, etax = generate_gjrgarch(nobs, [1.0, ar1], [[1,0],[0.5,0]], mu=0.0,scale=0.1) garchplot(errgjr[:nobs], hgjr[:nobs], 'GJR-GARCH(1,1) Simulation - symmetric') garchplot(errgjr[-400:nobs], hgjr[-400:nobs], 'GJR-GARCH(1,1) Simulation - symmetric') np.random.seed(seed) errgjr2,hgjr2, etax = generate_gjrgarch(nobs, [1.0, ar1], [[1,0],[0.1,0.9]], mu=0.0,scale=0.1) garchplot(errgjr2[:nobs], hgjr2[:nobs], 'GJR-GARCH(1,1) Simulation') garchplot(errgjr2[-400:nobs], hgjr2[-400:nobs], 'GJR-GARCH(1,1) Simulation') np.random.seed(seed) errgjr3,hgjr3, etax3 = generate_gjrgarch(nobs, [1.0, ar1], [[1,0],[0.1,0.9],[0.1,0.9],[0.1,0.9]], mu=0.0,scale=0.1) garchplot(errgjr3[:nobs], hgjr3[:nobs], 'GJR-GARCH(1,3) Simulation') garchplot(errgjr3[-400:nobs], hgjr3[-400:nobs], 'GJR-GARCH(1,3) Simulation') np.random.seed(seed) errgjr4,hgjr4, etax4 = generate_gjrgarch(nobs, [1.0, ar1], [[1., 1,0],[0, 0.1,0.9],[0, 0.1,0.9],[0, 0.1,0.9]], mu=0.0,scale=0.1) garchplot(errgjr4[:nobs], hgjr4[:nobs], 'GJR-GARCH(1,3) Simulation') garchplot(errgjr4[-400:nobs], hgjr4[-400:nobs], 'GJR-GARCH(1,3) Simulation') varinno = np.zeros(100) varinno[0] = 1. errgjr5,hgjr5, etax5 = generate_gjrgarch(100, [1.0, -0.], [[1., 1,0],[0, 0.1,0.8],[0, 0.05,0.7],[0, 0.01,0.6]], mu=0.0,scale=0.1, varinnovation=varinno) garchplot(errgjr5[:20], hgjr5[:20], 'GJR-GARCH(1,3) Simulation') #garchplot(errgjr4[-400:nobs], hgjr4[-400:nobs], 'GJR-GARCH(1,3) Simulation') #plt.show() seed = np.random.randint(9999999) # 9188410 print 'seed', seed x = np.arange(20).reshape(10,2) x3 = np.column_stack((np.ones((x.shape[0],1)),x)) y, inp = miso_lfilter([1., 0],np.array([[-2.0,3,1],[0.0,0.0,0]]),x3) nobs = 1000 warmup = 1000 np.random.seed(seed) ar = [1.0, -0.7]#7, -0.16, -0.1] #ma = [[1., 1, 0],[0, 0.6,0.1],[0, 0.1,0.1],[0, 0.1,0.1]] ma = [[1., 0, 0],[0, 0.8,0.0]] #,[0, 0.9,0.0]] # errgjr4,hgjr4, etax4 = generate_gjrgarch(warmup+nobs, [1.0, -0.99], # [[1., 1, 0],[0, 0.6,0.1],[0, 0.1,0.1],[0, 0.1,0.1]], # mu=0.2, scale=0.25) errgjr4,hgjr4, etax4 = generate_gjrgarch(warmup+nobs, ar, ma, mu=0.4, scale=1.01) errgjr4,hgjr4, etax4 = errgjr4[warmup:], hgjr4[warmup:], etax4[warmup:] garchplot(errgjr4[:nobs], hgjr4[:nobs], 'GJR-GARCH(1,3) Simulation - DGP') ggmod = Garch(errgjr4-errgjr4.mean())#hgjr4[:nobs])#-hgjr4.mean()) #errgjr4) ggmod.nar = 1 ggmod.nma = 1 ggmod._start_params = np.array([-0.6, 0.1, 0.2, 0.0]) ggres = ggmod.fit(start_params=np.array([-0.6, 0.1, 0.2, 0.0]), maxiter=1000) print 'ggres.params', ggres.params garchplot(ggmod.errorsest, ggmod.h, title='Garch estimated') ggmod0 = Garch0(errgjr4-errgjr4.mean())#hgjr4[:nobs])#-hgjr4.mean()) #errgjr4) ggmod0.nar = 1 ggmod.nma = 1 start_params = np.array([-0.6, 0.2, 0.1]) ggmod0._start_params = start_params #np.array([-0.6, 0.1, 0.2, 0.0]) ggres0 = ggmod0.fit(start_params=start_params, maxiter=2000) print 'ggres0.params', ggres0.params ggmod0 = Garch0(errgjr4-errgjr4.mean())#hgjr4[:nobs])#-hgjr4.mean()) #errgjr4) ggmod0.nar = 1 ggmod.nma = 1 start_params = np.array([-0.6, 0.2, 0.1]) ggmod0._start_params = start_params #np.array([-0.6, 0.1, 0.2, 0.0]) ggres0 = ggmod0.fit(start_params=start_params, method='bfgs', maxiter=2000) print 'ggres0.params', ggres0.params g11res = optimize.fmin(lambda params: -loglike_GARCH11(params, errgjr4-errgjr4.mean())[0], [0.93, 0.9, 0.2]) print g11res llf = loglike_GARCH11(g11res, errgjr4-errgjr4.mean()) print llf[0] if 'rpyfit' in examples: from rpy import r r.library('fGarch') f = r.formula('~garch(1, 1)') fit = r.garchFit(f, data = errgjr4-errgjr4.mean(), include_mean=False) if 'rpysim' in examples: from rpy import r f = r.formula('~garch(1, 1)') #fit = r.garchFit(f, data = errgjr4) x = r.garchSim( n = 500) print 'R acf', tsa.acf(np.power(x,2))[:15] arma3 = Arma(np.power(x,2)) arma3res = arma3.fit(start_params=[-0.2,0.1,0.5],maxiter=5000) print arma3res.params arma3b = Arma(np.power(x,2)) arma3bres = arma3b.fit(start_params=[-0.2,0.1,0.5],maxiter=5000, method='bfgs') print arma3bres.params xr = r.garchSim( n = 100) x = np.asarray(xr) ggmod = Garch(x-x.mean()) ggmod.nar = 1 ggmod.nma = 1 ggmod._start_params = np.array([-0.6, 0.1, 0.2, 0.0]) ggres = ggmod.fit(start_params=np.array([-0.6, 0.1, 0.2, 0.0]), maxiter=1000) print 'ggres.params', ggres.params g11res = optimize.fmin(lambda params: -loglike_GARCH11(params, x-x.mean())[0], [0.6, 0.6, 0.2]) print g11res llf = loglike_GARCH11(g11res, x-x.mean()) print llf[0] garchplot(ggmod.errorsest, ggmod.h, title='Garch estimated') fit = r.garchFit(f, data = x-x.mean(), include_mean=False, trace=False) print r.summary(fit) '''based on R default simulation model = list(omega = 1e-06, alpha = 0.1, beta = 0.8) nobs = 1000 (with nobs=500, gjrgarch doesn't do well >>> ggres = ggmod.fit(start_params=np.array([-0.6, 0.1, 0.2, 0.0]), maxiter=1000) Optimization terminated successfully. Current function value: -448.861335 Iterations: 385 Function evaluations: 690 >>> print 'ggres.params', ggres.params ggres.params [ -7.75090330e-01 1.57714749e-01 -9.60223930e-02 8.76021411e-07] rearranged 8.76021411e-07 1.57714749e-01(-9.60223930e-02) 7.75090330e-01 >>> print g11res [ 2.97459808e-06 7.83128600e-01 2.41110860e-01] >>> llf = loglike_GARCH11(g11res, x-x.mean()) >>> print llf[0] 442.603541936 Log Likelihood: -448.9376 normalized: -4.489376 omega alpha1 beta1 1.01632e-06 1.02802e-01 7.57537e-01 ''' ''' the following is for errgjr4-errgjr4.mean() ggres.params [-0.54510407 0.22723132 0.06482633 0.82325803] Final Estimate: LLH: 2065.56 norm LLH: 2.06556 mu omega alpha1 beta1 0.07229732 0.83069480 0.26313883 0.53986167 ggres.params [-0.50779163 0.2236606 0.00700036 1.154832 Final Estimate: LLH: 2116.084 norm LLH: 2.116084 mu omega alpha1 beta1 -4.759227e-17 1.145404e+00 2.288348e-01 5.085949e-01 run3 DGP 0.4/?? 0.8 0.7 gjrgarch: ggres.params [-0.45196579 0.2569641 0.02201904 1.11942636] rearranged const/omega ma1/alpha1 ar1/beta1 1.11942636 0.2569641(+0.02201904) 0.45196579 g11: [ 1.10262688 0.26680468 0.45724957] -2055.73912687 R: Final Estimate: LLH: 2055.738 norm LLH: 2.055738 mu omega alpha1 beta1 -1.665226e-17 1.102396e+00 2.668712e-01 4.573224e-01 fit = r.garchFit(f, data = errgjr4-errgjr4.mean()) rpy.RPy_RException: Error in solve.default(fit$hessian) : Lapack routine dgesv: system is exactly singular run4 DGP: mu=0.4, scale=1.01 ma = [[1., 0, 0],[0, 0.8,0.0]], ar = [1.0, -0.7] maybe something wrong with simulation gjrgarch ggres.params [-0.50554663 0.24449867 -0.00521004 1.00796791] rearranged 1.00796791 0.24449867(-0.00521004) 0.50554663 garch11: [ 1.01258264 0.24149155 0.50479994] -2056.3877404 R include_constant=False Final Estimate: LLH: 2056.397 norm LLH: 2.056397 omega alpha1 beta1 1.0123560 0.2409589 0.5049154 ''' erro,ho, etaxo = generate_gjrgarch(20, ar, ma, mu=0.04, scale=0.01, varinnovation = np.ones(20)) if 'sp500' in examples: import tabular as tb import scikits.timeseries as ts a = tb.loadSV(r'C:\Josef\work-oth\gspc_table.csv') s = ts.time_series(a[0]['Close'][::-1], dates=ts.date_array(a[0]['Date'][::-1],freq="D")) sp500 = a[0]['Close'][::-1] sp500r = np.diff(np.log(sp500)) #plt.show()
bsd-3-clause
zymsys/sms-tools
lectures/07-Sinusoidal-plus-residual-model/plots-code/hprModelAnal-flute.py
21
2771
import numpy as np import matplotlib.pyplot as plt from scipy.signal import hamming, hanning, triang, blackmanharris, resample import math import sys, os, time sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/models/')) import stft as STFT import utilFunctions as UF import harmonicModel as HM (fs, x) = UF.wavread(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../sounds/flute-A4.wav')) w = np.blackman(551) N = 1024 t = -100 nH = 40 minf0 = 420 maxf0 = 460 f0et = 5 maxnpeaksTwm = 5 minSineDur = .1 harmDevSlope = 0.01 Ns = 512 H = Ns/4 mX, pX = STFT.stftAnal(x, fs, w, N, H) hfreq, hmag, hphase = HM.harmonicModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope, minSineDur) xr = UF.sineSubtraction(x, Ns, H, hfreq, hmag, hphase, fs) mXr, pXr = STFT.stftAnal(xr, fs, hamming(Ns), Ns, H) maxplotfreq = 5000.0 plt.figure(1, figsize=(9, 7)) plt.subplot(221) numFrames = int(mX[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = fs*np.arange(N*maxplotfreq/fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(mX[:,:N*maxplotfreq/fs+1])) plt.autoscale(tight=True) harms = hfreq*np.less(hfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.autoscale(tight=True) plt.title('mX + harmonics (flute-A4.wav)') plt.subplot(222) numFrames = int(mX[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = fs*np.arange(N*maxplotfreq/fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(np.diff(pX[:,:N*maxplotfreq/fs+1],axis=1))) plt.autoscale(tight=True) harms = hfreq*np.less(hfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.autoscale(tight=True) plt.title('pX + harmonics') plt.subplot(223) numFrames = int(mXr[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = fs*np.arange(Ns*maxplotfreq/fs)/Ns plt.pcolormesh(frmTime, binFreq, np.transpose(mXr[:,:Ns*maxplotfreq/fs+1])) plt.autoscale(tight=True) plt.title('mXr') plt.subplot(224) numFrames = int(pXr[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = fs*np.arange(Ns*maxplotfreq/fs)/Ns plt.pcolormesh(frmTime, binFreq, np.transpose(np.diff(pXr[:,:Ns*maxplotfreq/fs+1],axis=1))) plt.autoscale(tight=True) plt.title('pXr') plt.tight_layout() plt.savefig('hprModelAnal-flute.png') UF.wavwrite(5*xr, fs, 'flute-residual.wav') plt.show()
agpl-3.0
joernhees/scikit-learn
examples/datasets/plot_random_multilabel_dataset.py
278
3402
""" ============================================== Plot randomly generated multilabel dataset ============================================== This illustrates the `datasets.make_multilabel_classification` dataset generator. Each sample consists of counts of two features (up to 50 in total), which are differently distributed in each of two classes. Points are labeled as follows, where Y means the class is present: ===== ===== ===== ====== 1 2 3 Color ===== ===== ===== ====== Y N N Red N Y N Blue N N Y Yellow Y Y N Purple Y N Y Orange Y Y N Green Y Y Y Brown ===== ===== ===== ====== A star marks the expected sample for each class; its size reflects the probability of selecting that class label. The left and right examples highlight the ``n_labels`` parameter: more of the samples in the right plot have 2 or 3 labels. Note that this two-dimensional example is very degenerate: generally the number of features would be much greater than the "document length", while here we have much larger documents than vocabulary. Similarly, with ``n_classes > n_features``, it is much less likely that a feature distinguishes a particular class. """ from __future__ import print_function import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_multilabel_classification as make_ml_clf print(__doc__) COLORS = np.array(['!', '#FF3333', # red '#0198E1', # blue '#BF5FFF', # purple '#FCD116', # yellow '#FF7216', # orange '#4DBD33', # green '#87421F' # brown ]) # Use same random seed for multiple calls to make_multilabel_classification to # ensure same distributions RANDOM_SEED = np.random.randint(2 ** 10) def plot_2d(ax, n_labels=1, n_classes=3, length=50): X, Y, p_c, p_w_c = make_ml_clf(n_samples=150, n_features=2, n_classes=n_classes, n_labels=n_labels, length=length, allow_unlabeled=False, return_distributions=True, random_state=RANDOM_SEED) ax.scatter(X[:, 0], X[:, 1], color=COLORS.take((Y * [1, 2, 4] ).sum(axis=1)), marker='.') ax.scatter(p_w_c[0] * length, p_w_c[1] * length, marker='*', linewidth=.5, edgecolor='black', s=20 + 1500 * p_c ** 2, color=COLORS.take([1, 2, 4])) ax.set_xlabel('Feature 0 count') return p_c, p_w_c _, (ax1, ax2) = plt.subplots(1, 2, sharex='row', sharey='row', figsize=(8, 4)) plt.subplots_adjust(bottom=.15) p_c, p_w_c = plot_2d(ax1, n_labels=1) ax1.set_title('n_labels=1, length=50') ax1.set_ylabel('Feature 1 count') plot_2d(ax2, n_labels=3) ax2.set_title('n_labels=3, length=50') ax2.set_xlim(left=0, auto=True) ax2.set_ylim(bottom=0, auto=True) plt.show() print('The data was generated from (random_state=%d):' % RANDOM_SEED) print('Class', 'P(C)', 'P(w0|C)', 'P(w1|C)', sep='\t') for k, p, p_w in zip(['red', 'blue', 'yellow'], p_c, p_w_c.T): print('%s\t%0.2f\t%0.2f\t%0.2f' % (k, p, p_w[0], p_w[1]))
bsd-3-clause
aleju/self-driving-truck
lib/plotting.py
1
13772
"""Classes to handle plotting during the training.""" from __future__ import print_function, division import math import cPickle as pickle from collections import OrderedDict import numpy as np import matplotlib.pyplot as plt import time GROWTH_BY = 500 class History(object): def __init__(self): self.line_groups = OrderedDict() @staticmethod def from_string(s): return pickle.loads(s) def to_string(self): return pickle.dumps(self, protocol=-1) @staticmethod def load_from_filepath(fp): #return json.loads(open(, "r").read()) with open(fp, "r") as f: history = pickle.load(f) return history def save_to_filepath(self, fp): with open(fp, "w") as f: pickle.dump(self, f, protocol=-1) def add_group(self, group_name, line_names, increasing=True): self.line_groups[group_name] = LineGroup(group_name, line_names, increasing=increasing) def add_value(self, group_name, line_name, x, y, average=False): self.line_groups[group_name].lines[line_name].append(x, y, average=average) def get_group_names(self): return list(self.line_groups.iterkeys()) def get_groups_increasing(self): return [group.increasing for group in self.line_groups.itervalues()] def get_max_x(self): return max([group.get_max_x() for group in self.line_groups.itervalues()]) def get_recent_average(self, group_name, line_name, nb_points): ys = self.line_groups[group_name].lines[line_name].ys[-nb_points:] return np.average(ys) class LineGroup(object): def __init__(self, group_name, line_names, increasing=True): self.group_name = group_name self.lines = OrderedDict([(name, Line()) for name in line_names]) self.increasing = increasing self.xlim = (None, None) def get_line_names(self): return list(self.lines.iterkeys()) def get_line_xs(self): #return [line.xs for line in self.lines.itervalues()] """ for key, line in self.lines.items(): if not hasattr(line, "last_index"): print(self.group_name, key, "no last index") else: print(self.group_name, key, "OK") print(type(line.xs), type(line.ys), type(line.counts), type(line.datetimes)) """ return [line.get_xs() for line in self.lines.itervalues()] def get_line_ys(self): #return [line.ys for line in self.lines.itervalues()] return [line.get_ys() for line in self.lines.itervalues()] def get_max_x(self): #return max([max(line.xs) if len(line.xs) > 0 else 0 for line in self.lines.itervalues()]) return max([np.maximum(line.get_xs()) if line.last_index > -1 else 0 for line in self.lines.itervalues()]) """ class Line(object): def __init__(self, xs=None, ys=None, counts=None, datetimes=None): self.xs = xs if xs is not None else [] self.ys = ys if ys is not None else [] self.counts = counts if counts is not None else [] self.datetimes = datetimes if datetimes is not None else [] self.last_index = -1 def append(self, x, y, average=False): # legacy (for loading from pickle) #if not hasattr(self, "counts"): # self.counts = [1] * len(self.xs) # --- if not average or len(self.xs) == 0 or self.xs[-1] != x: self.xs.append(x) self.ys.append(float(y)) # float to get rid of numpy self.counts.append(1) self.datetimes.append(time.time()) else: count = self.counts[-1] self.ys[-1] = ((self.ys[-1] * count) + y) / (count+1) self.counts[-1] += 1 self.datetimes[-1] = time.time() """ class Line(object): def __init__(self, xs=None, ys=None, counts=None, datetimes=None): zeros = np.tile(np.array([0], dtype=np.int32), GROWTH_BY) self.xs = xs if xs is not None else np.copy(zeros) self.ys = ys if ys is not None else zeros.astype(np.float32) self.counts = counts if counts is not None else zeros.astype(np.uint16) self.datetimes = datetimes if datetimes is not None else zeros.astype(np.uint64) self.last_index = -1 # for legacy as functions, replace with properties def get_xs(self): # legacy if isinstance(self.xs, list): self._legacy_convert_from_list_to_np() return self.xs[0:self.last_index+1] def get_ys(self): return self.ys[0:self.last_index+1] def get_counts(self): return self.counts[0:self.last_index+1] def get_datetimes(self): return self.datetimes[0:self.last_index+1] def _legacy_convert_from_list_to_np(self): #print("is list!") print("[plotting] Converting from list to numpy...") self.last_index = len(self.xs) - 1 self.xs = np.array(self.xs, dtype=np.int32) self.ys = np.array(self.ys, dtype=np.float32) self.counts = np.array(self.counts, dtype=np.uint16) self.datetimes = np.array([int(dt*1000) for dt in self.datetimes], dtype=np.uint64) def append(self, x, y, average=False): # legacy (for loading from pickle) #if not hasattr(self, "counts"): # self.counts = [1] * len(self.xs) # --- #legacy if isinstance(self.xs, list): self._legacy_convert_from_list_to_np() if (self.last_index+1) == self.xs.shape[0]: #print("growing from %d by %d..." % (self.xs.shape[0], GROWTH_BY), self.xs.shape, self.ys.shape, self.counts.shape, self.datetimes.shape) zeros = np.tile(np.array([0], dtype=np.int32), GROWTH_BY) self.xs = np.append(self.xs, np.copy(zeros)) self.ys = np.append(self.ys, zeros.astype(np.float32)) self.counts = np.append(self.counts, zeros.astype(np.uint16)) self.datetimes = np.append(self.datetimes, zeros.astype(np.uint64)) #print("growing done", self.xs.shape, self.ys.shape, self.counts.shape, self.datetimes.shape) first_entry = (self.last_index == -1) if not average or first_entry or self.xs[self.last_index] != x: idx = self.last_index + 1 self.xs[idx] = x self.ys[idx] = y self.counts[idx] = 1 self.datetimes[idx] = int(time.time()*1000) self.last_index = idx else: idx = self.last_index count = self.counts[idx] self.ys[idx] = ((self.ys[idx] * count) + y) / (count+1) self.counts[idx] = count + 1 self.datetimes[idx] = int(time.time()*1000) #print("added", x, y, average) #print(self.xs[self.last_index-10:self.last_index+10+1]) #print(self.ys[self.last_index-10:self.last_index+10+1]) #print(self.counts[self.last_index-10:self.last_index+10+1]) #print(self.datetimes[self.last_index-10:self.last_index+10+1]) class LossPlotter(object): def __init__(self, titles, increasing, save_to_fp): assert len(titles) == len(increasing) n_plots = len(titles) self.titles = titles self.increasing = dict([(title, incr) for title, incr in zip(titles, increasing)]) self.xlim = dict([(title, (None, None)) for title in titles]) self.colors = ["red", "blue", "cyan", "magenta", "orange", "black"] self.nb_points_max = 500 self.save_to_fp = save_to_fp self.start_batch_idx = 0 self.autolimit_y = False self.autolimit_y_multiplier = 5 #self.fig, self.axes = plt.subplots(nrows=2, ncols=2, figsize=(20, 20)) nrows = max(1, int(math.sqrt(n_plots))) ncols = int(math.ceil(n_plots / nrows)) width = ncols * 10 height = nrows * 10 self.fig, self.axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(width, height)) if nrows == 1 and ncols == 1: self.axes = [self.axes] else: self.axes = self.axes.flat title_to_ax = dict() for idx, (title, ax) in enumerate(zip(self.titles, self.axes)): title_to_ax[title] = ax self.title_to_ax = title_to_ax self.fig.tight_layout() self.fig.subplots_adjust(left=0.05) def plot(self, history): for plot_idx, title in enumerate(self.titles): ax = self.title_to_ax[title] group_name = title group_increasing = self.increasing[title] group = history.line_groups[title] line_names = group.get_line_names() #print("getting line x/y...", time.time()) line_xs = group.get_line_xs() line_ys = group.get_line_ys() #print("getting line x/y FIN", time.time()) """ print("title", title) print("line_names", line_names) for i, xx in enumerate(line_xs): print("line_xs i: ", xx) for i, yy in enumerate(line_ys): print("line_ys i: ", yy) """ if any([len(xx) > 0 for xx in line_xs]): xs_min = min([min(xx) for xx in line_xs if len(xx) > 0]) xs_max = max([max(xx) for xx in line_xs if len(xx) > 0]) xlim = self.xlim[title] xlim = [ max(xs_min, self.start_batch_idx) if xlim[0] is None else min(xlim[0], xs_max-1), xs_max+1 if xlim[1] is None else xlim[1] ] if xlim[0] < 0: xlim[0] = max(xs_max - abs(xlim[0]), 0) if xlim[1] < 0: xlim[1] = max(xs_max - abs(xlim[1]), 1) else: # none of the lines has any value, so just use dummy values # to avoid min/max of empty sequence errors xlim = [ 0 if self.xlim[title][0] is None else self.xlim[title][0], 1 if self.xlim[title][1] is None else self.xlim[title][1] ] self._plot_group(ax, group_name, group_increasing, line_names, line_xs, line_ys, xlim) self.fig.savefig(self.save_to_fp) # this seems to be slow sometimes def _line_to_xy(self, line_x, line_y, xlim, limit_y_min=None, limit_y_max=None): def _add_point(points_x, points_y, curr_sum, counter): points_x.append(batch_idx) y = curr_sum / counter if limit_y_min is not None and limit_y_max is not None: y = np.clip(y, limit_y_min, limit_y_max) elif limit_y_min is not None: y = max(y, limit_y_min) elif limit_y_max is not None: y = min(y, limit_y_max) points_y.append(y) nb_points = 0 for i in range(len(line_x)): batch_idx = line_x[i] if xlim[0] <= batch_idx < xlim[1]: nb_points += 1 point_every = max(1, int(nb_points / self.nb_points_max)) points_x = [] points_y = [] curr_sum = 0 counter = 0 for i in range(len(line_x)): batch_idx = line_x[i] if xlim[0] <= batch_idx < xlim[1]: curr_sum += line_y[i] counter += 1 if counter >= point_every: _add_point(points_x, points_y, curr_sum, counter) counter = 0 curr_sum = 0 if counter > 0: _add_point(points_x, points_y, curr_sum, counter) return points_x, points_y def _plot_group(self, ax, group_name, group_increasing, line_names, line_xs, line_ys, xlim): ax.cla() ax.grid() if self.autolimit_y and any([len(line_xs) > 0 for line_xs in line_xs]): min_x = min([np.min(line_x) for line_x in line_xs]) max_x = max([np.max(line_x) for line_x in line_xs]) min_y = min([np.min(line_y) for line_y in line_ys]) max_y = max([np.max(line_y) for line_y in line_ys]) if group_increasing: if max_y > 0: limit_y_max = None limit_y_min = max_y / self.autolimit_y_multiplier if min_y > limit_y_min: limit_y_min = None else: if min_y > 0: limit_y_max = min_y * self.autolimit_y_multiplier limit_y_min = None if max_y < limit_y_max: limit_y_max = None if limit_y_min is not None: ax.plot((min_x, max_x), (limit_y_min, limit_y_min), c="purple") if limit_y_max is not None: ax.plot((min_x, max_x), (limit_y_max, limit_y_max), c="purple") # y achse range begrenzen yaxmin = min_y if limit_y_min is None else limit_y_min yaxmax = max_y if limit_y_max is None else limit_y_max yrange = yaxmax - yaxmin yaxmin = yaxmin - (0.05 * yrange) yaxmax = yaxmax + (0.05 * yrange) ax.set_ylim([yaxmin, yaxmax]) else: limit_y_min = None limit_y_max = None for line_name, line_x, line_y, line_col in zip(line_names, line_xs, line_ys, self.colors): #print("line to xy...", time.time()) x, y = self._line_to_xy(line_x, line_y, xlim, limit_y_min=limit_y_min, limit_y_max=limit_y_max) #print("line to xy FIN", time.time()) #print("plotting ax...", time.time()) ax.plot(x, y, color=line_col, linewidth=1.0) #print("plotting ax FIN", time.time()) ax.set_title(group_name)
mit
valsson/MD-MC-Codes-2016
HarmonicOscillator-MD/HarmonicOscillator-MD-Verlet.py
1
4262
#! /usr/bin/env python import numpy as np import matplotlib.pyplot as plt from DataTools import writeDataToFile import argparse parser = argparse.ArgumentParser() parser.add_argument('--time-step',dest='time_step',required=False) parser.add_argument('--output-file',dest='fn_out',required=False) args = parser.parse_args() # Parameters of potential m = 1.0 k = (2.0*np.pi)**2 angular_freq = np.sqrt(k/m) freq = angular_freq/(2.0*np.pi) period = 1.0/freq # MD Parameters if(args.time_step): time_step = np.float64(args.time_step) else: time_step = 0.01*period if(args.fn_out): fn_out = args.fn_out else: fn_out = 'results.data' showPlots = False #num_periods = 20 #num_steps = np.int(np.rint( (num_periods*period)/time_step )) num_steps = 10000 # initial postion and velocity at t=0 initial_position = 2.0 initial_velocity = 0.0 def getPotentialEnergy(x): potential_ener = 0.5*k*x**2 return potential_ener #------------------------------- def getForce(x): force = -k*x return force #------------------------------- def getAccleration(x): return getForce(x)/m #------------------------------- def getPotentialAndForce(x): return ( getPotentialEnergy(x), getForce(x) ) #------------------------------- def getKineticEnergy(v): kinetic_ener = 0.5*m*v**2 return kinetic_ener #------------------------------- def getTotalEnergy(x,v): return getPotentialEnergy(x)+getKineticEnergy(v) #------------------------------- # analytical solution: phi = np.arctan(-initial_velocity/(initial_position*angular_freq)) amplitude = initial_position/np.cos(phi) conserved_energy = getPotentialEnergy(amplitude) # ---------------------- times = [] positions = [] velocites = [] pot_energies = [] kin_energies = [] tot_energies = [] time = 0.0 curr_position = initial_position prev_position = curr_position-initial_velocity*time_step + 0.5*getAccleration(curr_position)*time_step**2 curr_velocity = initial_velocity for i in range(num_steps): if (i+1) % (num_steps/10) == 0: print 'MD step {0:6d} of {1:6d}'.format(i+1,num_steps) # get force at t accleration = getAccleration(curr_position) # get new position at t+dt new_position = 2.0*curr_position - prev_position + accleration*time_step**2 # get velocity at t curr_velocity = (new_position - prev_position) / (2.0*time_step) # get energies at t curr_pot_ener = getPotentialEnergy(curr_position) curr_kin_ener = getKineticEnergy(curr_velocity) curr_tot_ener = curr_pot_ener + curr_kin_ener # times.append( time ) positions.append( curr_position ) velocites.append( curr_velocity ) pot_energies.append( curr_pot_ener ) kin_energies.append( curr_kin_ener ) tot_energies.append( curr_tot_ener ) # prev_position = curr_position curr_position = new_position time += time_step # #---------------------------------------- times = np.array(times) positions = np.array(positions) velocites = np.array(velocites) pot_energies = np.array(pot_energies) kin_energies = np.array(kin_energies) tot_energies = np.array(tot_energies) positions_analytical = amplitude*np.cos(angular_freq*times+phi) velocites_analytical = -angular_freq*amplitude*np.sin(angular_freq*times+phi) writeDataToFile(fn_out, [times,positions,velocites,pot_energies,kin_energies,tot_energies,positions_analytical,velocites_analytical], ['time','pos','vel','pot_ene','kin_ene','tot_ene','pos_an','vel_an'], constantsNames=['time_step','period','amplitude','k','m','phi','conserved_energy'], constantsValues=[time_step,period,amplitude,k,m,phi,conserved_energy], dataFormat='%15.8f') if showPlots: plt.figure(1) plt.plot(times,tot_energies) plt.plot(times,pot_energies) plt.plot(times,kin_energies) plt.show() plt.figure(2) plt.plot(times,pot_energies) plt.show() plt.figure(3) plt.plot(times,kin_energies) plt.show() plt.figure(4) plt.plot(times,velocites) plt.show() plt.figure(5) plt.plot(times,positions) plt.plot(times,positions_analytical) plt.show() plt.figure(6) plt.plot(times,positions-positions_analytical) plt.show() #
mit
QuLogic/specfem1d
Python_version/grid.py
2
2988
# -*- coding: utf-8 -*- ''' Definitions of the grid. ''' from __future__ import (absolute_import, division, print_function) import numpy as np import functions import gll class OneDimensionalGrid(object): """Contains the grid properties""" def __init__(self, param): """Init""" self.param = param self.z = np.zeros(param.nGlob) self.rho = np.zeros((param.nSpec, param.nGLL)) self.mu = np.zeros((param.nSpec, param.nGLL)) self.ticks = np.zeros(param.nSpec + 1) if param.gridType == 'homogeneous': self.ticks = np.linspace(0, param.length, param.nSpec + 1) self.rho.fill(param.meanRho) self.mu.fill(param.meanMu) self.z[1:param.nGLJ] = functions.project_inverse( param.ksiGLJ[1:param.nGLJ], 0, self.ticks) ksiGLL = param.ksiGLL[1:] for i in range(param.nGLL, param.nGlob, param.N): self.z[i:i + param.N] = functions.project_inverse(ksiGLL, i // param.N, self.ticks) self.z[-1] = self.ticks[-1] elif param.gridType == 'gradient': msg = "typeOfGrid == 'gradient' has not been implemented yet" raise NotImplementedError(msg) elif param.gridType == 'miscellaneous': msg = "typeOfGrid == 'miscellaneous' has not been implemented yet" raise NotImplementedError(msg) elif param.gridType == 'file': self.z, self.rho, self.mu = np.loadtxt(param.gridFile, unpack=True) self.ticks = np.loadtxt(param.ticksFile) else: raise ValueError('Unknown grid type: %s' % (param.gridType, )) # Jacobians at the GLL (and GLJ for the first element in axisym) # points (arrays nSpec*(N+1) elements) self.dXdKsi = gll.jacobian(self.ticks, param) self.dKsiDx = gll.jacobian_inverse(self.ticks, param) def plot(self): """Plot the grid my_ticks gives the abscissa of the borders TODO I should test : _the types of the parameters _their sizes""" import matplotlib.pyplot as plt from matplotlib.ticker import FixedLocator fig, ax = plt.subplots(2, 1, sharex=True) ax[0].plot(self.z[self.param.ibool].flat, self.rho.flat, 'b+') ax[0].set_title(r'$\rho(z)$') ax[0].xaxis.set_minor_locator(FixedLocator(self.ticks)) ax[0].xaxis.grid(True, which='minor', alpha=0.5) ax[0].yaxis.grid(True) ax[1].plot(self.z[self.param.ibool].flat, self.mu.flat, 'r+') ax[1].set_title(r'$\mu(z)$') ax[1].xaxis.set_minor_locator(FixedLocator(self.ticks)) ax[1].xaxis.grid(True, which='minor', alpha=0.5) ax[1].yaxis.grid(True) plt.suptitle('Grid') plt.show()
gpl-2.0
anntzer/scikit-learn
examples/model_selection/plot_confusion_matrix.py
8
2074
""" ================ Confusion matrix ================ Example of confusion matrix usage to evaluate the quality of the output of a classifier on the iris data set. The diagonal elements represent the number of points for which the predicted label is equal to the true label, while off-diagonal elements are those that are mislabeled by the classifier. The higher the diagonal values of the confusion matrix the better, indicating many correct predictions. The figures show the confusion matrix with and without normalization by class support size (number of elements in each class). This kind of normalization can be interesting in case of class imbalance to have a more visual interpretation of which class is being misclassified. Here the results are not as good as they could be as our choice for the regularization parameter C was not the best. In real life applications this parameter is usually chosen using :ref:`grid_search`. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets from sklearn.model_selection import train_test_split from sklearn.metrics import ConfusionMatrixDisplay # import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target class_names = iris.target_names # Split the data into a training set and a test set X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) # Run classifier, using a model that is too regularized (C too low) to see # the impact on the results classifier = svm.SVC(kernel='linear', C=0.01).fit(X_train, y_train) np.set_printoptions(precision=2) # Plot non-normalized confusion matrix titles_options = [("Confusion matrix, without normalization", None), ("Normalized confusion matrix", 'true')] for title, normalize in titles_options: disp = ConfusionMatrixDisplay.from_estimator( classifier, X_test, y_test, display_labels=class_names, cmap=plt.cm.Blues, normalize=normalize ) disp.ax_.set_title(title) print(title) print(disp.confusion_matrix) plt.show()
bsd-3-clause
CroatianMeteorNetwork/RMS
RMS/Astrometry/CheckFit.py
1
25717
""" Automatic refining of astrometry calibration. The initial astrometric calibration is needed, which will be refined by using all stars from a given night. """ from __future__ import print_function, division, absolute_import import os import sys import copy import shutil import random import argparse import numpy as np import scipy.optimize import matplotlib.pyplot as plt import RMS.ConfigReader as cr from RMS.Formats import Platepar from RMS.Formats import CALSTARS from RMS.Formats import StarCatalog from RMS.Formats import FFfile from RMS.Astrometry.ApplyAstrometry import raDecToXYPP, xyToRaDecPP, rotationWrtHorizon, getFOVSelectionRadius from RMS.Astrometry.Conversions import date2JD, jd2Date, raDec2AltAz from RMS.Astrometry.FFTalign import alignPlatepar from RMS.Math import angularSeparation # Import Cython functions import pyximport pyximport.install(setup_args={'include_dirs':[np.get_include()]}) from RMS.Astrometry.CyFunctions import matchStars, subsetCatalog def computeMinimizationTolerances(config, platepar, star_dict_len): """ Compute tolerances for minimization. """ # Calculate the function tolerance, so the desired precision can be reached (the number is calculated # in the same regard as the cost function) fatol = (config.dist_check_threshold**2)/np.sqrt(star_dict_len*config.min_matched_stars + 1) # Parameter estimation tolerance for angular values fov_w = platepar.X_res/platepar.F_scale xatol_ang = config.dist_check_threshold*fov_w/platepar.X_res return fatol, xatol_ang def matchStarsResiduals(config, platepar, catalog_stars, star_dict, match_radius, ret_nmatch=False, \ sky_coords=False, lim_mag=None, verbose=False): """ Match the image and catalog stars with the given astrometry solution and estimate the residuals between them. Arguments: config: [Config structure] platepar: [Platepar structure] Astrometry parameters. catalog_stars: [ndarray] An array of catalog stars (ra, dec, mag). star_dict: [ndarray] A dictionary where the keys are JDs when the stars were recorded and values are 2D list of stars, each entry is (X, Y, bg_level, level, fwhm). match_radius: [float] Maximum radius for star matching (pixels). min_matched_stars: [int] Minimum number of matched stars on the image for the image to be accepted. Keyword arguments: ret_nmatch: [bool] If True, the function returns the number of matched stars and the average deviation. False by default. sky_coords: [bool] If True, sky coordinate residuals in RA, dec will be used to compute the cost, function, not image coordinates. lim_mag: [float] Override the limiting magnitude from config. None by default. verbose: [bool] Print results. True by default. Return: cost: [float] The cost function which weights the number of matched stars and the average deviation. """ if lim_mag is None: lim_mag = config.catalog_mag_limit # Estimate the FOV radius fov_radius = getFOVSelectionRadius(platepar) # Dictionary containing the matched stars, the keys are JDs of every image matched_stars = {} # Go through every FF image and its stars for jd in star_dict: # Estimate RA,dec of the centre of the FOV _, RA_c, dec_c, _ = xyToRaDecPP([jd2Date(jd)], [platepar.X_res/2], [platepar.Y_res/2], [1], \ platepar, extinction_correction=False) RA_c = RA_c[0] dec_c = dec_c[0] # Get stars from the catalog around the defined center in a given radius _, extracted_catalog = subsetCatalog(catalog_stars, RA_c, dec_c, jd, platepar.lat, platepar.lon, \ fov_radius, lim_mag) ra_catalog, dec_catalog, mag_catalog = extracted_catalog.T # Extract stars for the given Julian date stars_list = star_dict[jd] stars_list = np.array(stars_list) # Convert all catalog stars to image coordinates cat_x_array, cat_y_array = raDecToXYPP(ra_catalog, dec_catalog, jd, platepar) # Take only those stars which are within the FOV x_indices = np.argwhere((cat_x_array >= 0) & (cat_x_array < platepar.X_res)) y_indices = np.argwhere((cat_y_array >= 0) & (cat_y_array < platepar.Y_res)) cat_good_indices = np.intersect1d(x_indices, y_indices).astype(np.uint32) # cat_x_array = cat_x_array[good_indices] # cat_y_array = cat_y_array[good_indices] # # Plot image stars # im_y, im_x, _, _ = stars_list.T # plt.scatter(im_y, im_x, facecolors='none', edgecolor='g') # # Plot catalog stars # plt.scatter(cat_y_array[cat_good_indices], cat_x_array[cat_good_indices], c='r', s=20, marker='+') # plt.show() # Match image and catalog stars matched_indices = matchStars(stars_list, cat_x_array, cat_y_array, cat_good_indices, match_radius) # Skip this image is no stars were matched if len(matched_indices) < config.min_matched_stars: continue matched_indices = np.array(matched_indices) matched_img_inds, matched_cat_inds, dist_list = matched_indices.T # Extract data from matched stars matched_img_stars = stars_list[matched_img_inds.astype(np.int)] matched_cat_stars = extracted_catalog[matched_cat_inds.astype(np.int)] # Put the matched stars to a dictionary matched_stars[jd] = [matched_img_stars, matched_cat_stars, dist_list] # # Plot matched stars # im_y, im_x, _, _ = matched_img_stars.T # cat_y = cat_y_array[matched_cat_inds.astype(np.int)] # cat_x = cat_x_array[matched_cat_inds.astype(np.int)] # plt.scatter(im_x, im_y, c='r', s=5) # plt.scatter(cat_x, cat_y, facecolors='none', edgecolor='g') # plt.xlim([0, platepar.X_res]) # plt.ylim([platepar.Y_res, 0]) # plt.show() # If residuals on the image should be computed if not sky_coords: unit_label = 'px' # Extract all distances global_dist_list = [] # level_list = [] # mag_list = [] for jd in matched_stars: # matched_img_stars, matched_cat_stars, dist_list = matched_stars[jd] _, _, dist_list = matched_stars[jd] global_dist_list += dist_list.tolist() # # TEST # level_list += matched_img_stars[:, 3].tolist() # mag_list += matched_cat_stars[:, 2].tolist() # # Plot levels vs. magnitudes # plt.scatter(mag_list, np.log10(level_list)) # plt.xlabel('Magnitude') # plt.ylabel('Log10 level') # plt.show() # Compute the residuals on the sky else: unit_label = 'arcmin' global_dist_list = [] # Go through all matched stars for jd in matched_stars: matched_img_stars, matched_cat_stars, dist_list = matched_stars[jd] # Go through all stars on the image for img_star_entry, cat_star_entry in zip(matched_img_stars, matched_cat_stars): # Extract star coords star_y = img_star_entry[0] star_x = img_star_entry[1] cat_ra = cat_star_entry[0] cat_dec = cat_star_entry[1] # Convert image coordinates to RA/Dec _, star_ra, star_dec, _ = xyToRaDecPP([jd2Date(jd)], [star_x], [star_y], [1], \ platepar, extinction_correction=False) # Compute angular distance between the predicted and the catalog position ang_dist = np.degrees(angularSeparation(np.radians(cat_ra), np.radians(cat_dec), \ np.radians(star_ra[0]), np.radians(star_dec[0]))) # Store the angular separation in arc minutes global_dist_list.append(ang_dist*60) # Number of matched stars n_matched = len(global_dist_list) if n_matched == 0: if verbose: print('No matched stars with radius {:.1f} px!'.format(match_radius)) if ret_nmatch: return 0, 9999.0, 9999.0, {} else: return 9999.0 # Calculate the average distance avg_dist = np.median(global_dist_list) cost = (avg_dist**2)*(1.0/np.sqrt(n_matched + 1)) if verbose: print() print("Matched {:d} stars with radius of {:.1f} px".format(n_matched, match_radius)) print(" Average distance = {:.3f} {:s}".format(avg_dist, unit_label)) print(" Cost function = {:.5f}".format(cost)) if ret_nmatch: return n_matched, avg_dist, cost, matched_stars else: return cost def checkFitGoodness(config, platepar, catalog_stars, star_dict, match_radius, verbose=False): """ Checks if the platepar is 'good enough', given the extracted star positions. Returns True if the fit is deemed good, False otherwise. The goodness of fit is determined by 2 criteria: the average star residual (in pixels) has to be below a certain threshold, and an average number of matched stars per image has to be above a predefined threshold as well. Arguments: config: [Config structure] platepar: [Platepar structure] Initial astrometry parameters. catalog_stars: [ndarray] An array of catalog stars (ra, dec, mag). star_dict: [ndarray] A dictionary where the keys are JDs when the stars were recorded and values are 2D list of stars, each entry is (X, Y, bg_level, level). match_radius: [float] Maximum radius for star matching (pixels). Keyword arguments: verbose: [bool] If True, fit status will be printed on the screen. False by default. Return: [bool] True if the platepar is good, False otherwise. """ if verbose: print() print("CHECK FIT GOODNESS:") # Match the stars and calculate the residuals n_matched, avg_dist, cost, matched_stars = matchStarsResiduals(config, platepar, catalog_stars, \ star_dict, match_radius, ret_nmatch=True, verbose=verbose) # ### Plot zenith distance vs. residual # # Go through all images # for jd in matched_stars: # _, cat_stars, dists = matched_stars[jd] # # Extract RA/Dec # ra, dec, _ = cat_stars.T # zangle_list = [] # for ra_t, dec_t in zip(ra, dec): # # Compute zenith distance # azim, elev = raDec2AltAz(ra_t, dec_t, jd, platepar.lat, platepar.lon) # zangle = 90 - elev # zangle_list.append(zangle) # # Plot zangle vs. distance # plt.scatter(zangle_list, dists, c='k', s=0.1) # plt.xlabel('Zenith angle') # plt.ylabel('Residual (px)') # plt.show() # ### # Check that the average distance is within the threshold if avg_dist <= config.dist_check_threshold: if verbose: print() print('The minimum residual is satisfied!') # Check that the minimum number of stars is matched per every image if n_matched >= len(star_dict)*1: return True else: if verbose: print('But there are not enough stars on every image, recalibrating...') return False def _calcImageResidualsAstro(params, config, platepar, catalog_stars, star_dict, match_radius): """ Calculates the differences between the stars on the image and catalog stars in image coordinates with the given astrometrical solution. Arguments: params: [list] Fit parameters - reference RA, Dec, position angle, and scale. config: [Config] platepar: [Platepar] catalog_stars: [list] List of (ra, dec, mag) entries (angles in degrees). star_dict: [dict] Dictionary which contains the JD, and a list of (X, Y, bg_intens, intens) of the stars on the image. match_radius: [float] Star match radius (px). Return: [float] The average pixel residual (difference between image and catalog positions) normalized by the square root of the total number of matched stars. """ # Make a copy of the platepar pp = copy.deepcopy(platepar) # Extract fitting parameters ra_ref, dec_ref, pos_angle_ref, F_scale = params # Set the fitting parameters to the platepar clone pp.RA_d = ra_ref pp.dec_d = dec_ref pp.pos_angle_ref = pos_angle_ref pp.F_scale = F_scale # Match stars and calculate image residuals return matchStarsResiduals(config, pp, catalog_stars, star_dict, match_radius, verbose=False) def starListToDict(config, calstars_list, max_ffs=None): """ Converts the list of calstars into dictionary where the keys are FF file JD and the values is a list of (X, Y, bg_intens, intens) of stars. """ # Convert the list to a dictionary calstars = {ff_file: star_data for ff_file, star_data in calstars_list} # Dictionary which will contain the JD, and a list of (X, Y, bg_intens, intens) of the stars star_dict = {} # Take only those files with enough stars on them for ff_name in calstars: stars_list = calstars[ff_name] # Check if there are enough stars on the image if len(stars_list) >= config.ff_min_stars: # Calculate the JD time of the FF file dt = FFfile.getMiddleTimeFF(ff_name, config.fps, ret_milliseconds=True) jd = date2JD(*dt) # Add the time and the stars to the dict star_dict[jd] = stars_list if max_ffs is not None: # Limit the number of FF files used if len(star_dict) > max_ffs: # Randomly choose calstars_files_N image files from the whole list rand_keys = random.sample(list(star_dict), max_ffs) star_dict = {key: star_dict[key] for key in rand_keys} return star_dict def autoCheckFit(config, platepar, calstars_list, _fft_refinement=False): """ Attempts to refine the astrometry fit with the given stars and and initial astrometry parameters. Arguments: config: [Config structure] platepar: [Platepar structure] Initial astrometry parameters. calstars_list: [list] A list containing stars extracted from FF files. See RMS.Formats.CALSTARS for more details. Keyword arguments: _fft_refinement: [bool] Internal flag indicating that autoCF is running the second time recursively after FFT platepar adjustment. Return: (platepar, fit_status): platepar: [Platepar structure] Estimated/refined platepar. fit_status: [bool] True if fit was successfuly, False if not. """ def _handleFailure(config, platepar, calstars_list, catalog_stars, _fft_refinement): """ Run FFT alignment before giving up on ACF. """ if not _fft_refinement: print() print("-------------------------------------------------------------------------------") print('The initial platepar is bad, trying to refine it using FFT phase correlation...') print() # Prepare data for FFT image registration calstars_dict = {ff_file: star_data for ff_file, star_data in calstars_list} # Extract star list from CALSTARS file from FF file with most stars max_len_ff = max(calstars_dict, key=lambda k: len(calstars_dict[k])) # Take only X, Y (change order so X is first) calstars_coords = np.array(calstars_dict[max_len_ff])[:, :2] calstars_coords[:, [0, 1]] = calstars_coords[:, [1, 0]] # Get the time of the FF file calstars_time = FFfile.getMiddleTimeFF(max_len_ff, config.fps, ret_milliseconds=True) # Try aligning the platepar using FFT image registration platepar_refined = alignPlatepar(config, platepar, calstars_time, calstars_coords) print() ### If there are still not enough stars matched, try FFT again ### min_radius = 10 # Prepare star dictionary to check the match dt = FFfile.getMiddleTimeFF(max_len_ff, config.fps, ret_milliseconds=True) jd = date2JD(*dt) star_dict_temp = {} star_dict_temp[jd] = calstars_dict[max_len_ff] # Check the number of matched stars n_matched, _, _, _ = matchStarsResiduals(config, platepar_refined, catalog_stars, \ star_dict_temp, min_radius, ret_nmatch=True, verbose=True) # Realign again if necessary if n_matched < config.min_matched_stars: print() print("-------------------------------------------------------------------------------") print('Doing a second FFT pass as the number of matched stars was too small...') print() platepar_refined = alignPlatepar(config, platepar_refined, calstars_time, calstars_coords) print() ### ### # Redo autoCF return autoCheckFit(config, platepar_refined, calstars_list, _fft_refinement=True) else: print('Auto Check Fit failed completely, please redo the plate manually!') return platepar, False if _fft_refinement: print('Second ACF run with an updated platepar via FFT phase correlation...') # Load catalog stars (overwrite the mag band ratios if specific catalog is used) catalog_stars, _, config.star_catalog_band_ratios = StarCatalog.readStarCatalog(config.star_catalog_path, \ config.star_catalog_file, lim_mag=config.catalog_mag_limit, \ mag_band_ratios=config.star_catalog_band_ratios) # Dictionary which will contain the JD, and a list of (X, Y, bg_intens, intens) of the stars star_dict = starListToDict(config, calstars_list, max_ffs=config.calstars_files_N) # There has to be a minimum of 200 FF files for star fitting if len(star_dict) < config.calstars_files_N: print('Not enough FF files in CALSTARS for ACF!') return platepar, False # Calculate the total number of calibration stars used total_calstars = sum([len(star_dict[key]) for key in star_dict]) print('Total calstars:', total_calstars) if total_calstars < config.calstars_min_stars: print('Not enough calibration stars, need at least', config.calstars_min_stars) return platepar, False print() # A list of matching radiuses to try min_radius = 0.5 radius_list = [10, 5, 3, 1.5, min_radius] # Calculate the function tolerance, so the desired precision can be reached (the number is calculated # in the same regard as the cost function) fatol, xatol_ang = computeMinimizationTolerances(config, platepar, len(star_dict)) ### If the initial match is good enough, do only quick recalibratoin ### # Match the stars and calculate the residuals n_matched, avg_dist, cost, _ = matchStarsResiduals(config, platepar, catalog_stars, star_dict, \ min_radius, ret_nmatch=True) if n_matched >= config.calstars_files_N: # Check if the average distance with the tightest radius is close if avg_dist < config.dist_check_quick_threshold: print("Using quick fit with smaller radiia...") # Use a reduced set of initial radius values radius_list = [1.5, min_radius] ########## # Match increasingly smaller search radiia around image stars for i, match_radius in enumerate(radius_list): # Match the stars and calculate the residuals n_matched, avg_dist, cost, _ = matchStarsResiduals(config, platepar, catalog_stars, star_dict, \ match_radius, ret_nmatch=True) print() print("-------------------------------------------------------------") print("Refining camera pointing with max pixel deviation = {:.1f} px".format(match_radius)) print("Initial values:") print(" Matched stars = {:>6d}".format(n_matched)) print(" Average deviation = {:>6.2f} px".format(avg_dist)) # The initial number of matched stars has to be at least the number of FF imaages, otherwise it means # that the initial platepar is no good if n_matched < config.calstars_files_N: print("The total number of initially matched stars is too small! Please manually redo the plate or make sure there are enough calibration stars.") # Try to refine the platepar with FFT phase correlation and redo the ACF return _handleFailure(config, platepar, calstars_list, catalog_stars, _fft_refinement) # Check if the platepar is good enough and do not estimate further parameters if checkFitGoodness(config, platepar, catalog_stars, star_dict, min_radius, verbose=True): # Print out notice only if the platepar is good right away if i == 0: print("Initial platepar is good enough!") return platepar, True # Initial parameters for the astrometric fit p0 = [platepar.RA_d, platepar.dec_d, platepar.pos_angle_ref, platepar.F_scale] # Fit the astrometric parameters res = scipy.optimize.minimize(_calcImageResidualsAstro, p0, args=(config, platepar, catalog_stars, \ star_dict, match_radius), method='Nelder-Mead', \ options={'fatol': fatol, 'xatol': xatol_ang}) print(res) # If the fit was not successful, stop further fitting if not res.success: # Try to refine the platepar with FFT phase correlation and redo the ACF return _handleFailure(config, platepar, calstars_list, catalog_stars, _fft_refinement) else: # If the fit was successful, use the new parameters from now on ra_ref, dec_ref, pos_angle_ref, F_scale = res.x platepar.RA_d = ra_ref platepar.dec_d = dec_ref platepar.pos_angle_ref = pos_angle_ref platepar.F_scale = F_scale # Check if the platepar is good enough and do not estimate further parameters if checkFitGoodness(config, platepar, catalog_stars, star_dict, min_radius, verbose=True): return platepar, True # Match the stars and calculate the residuals n_matched, avg_dist, cost, matched_stars = matchStarsResiduals(config, platepar, catalog_stars, \ star_dict, min_radius, ret_nmatch=True) print("FINAL SOLUTION with radius {:.1} px:".format(min_radius)) print(" Matched stars = {:>6d}".format(n_matched)) print(" Average deviation = {:>6.2f} px".format(avg_dist)) # Mark the platepar to indicate that it was automatically refined with CheckFit platepar.auto_check_fit_refined = True # Recompute alt/az of the FOV centre platepar.az_centre, platepar.alt_centre = raDec2AltAz(platepar.RA_d, platepar.dec_d, platepar.JD, \ platepar.lat, platepar.lon) # Recompute the rotation wrt horizon platepar.rotation_from_horiz = rotationWrtHorizon(platepar) return platepar, True if __name__ == "__main__": ### COMMAND LINE ARGUMENTS # Init the command line arguments parser arg_parser = argparse.ArgumentParser(description="Check if the calibration file matches the stars, and improve it.") arg_parser.add_argument('dir_path', nargs=1, metavar='DIR_PATH', type=str, \ help='Path to the folder with FF or image files. This folder also has to contain the platepar file.') arg_parser.add_argument('-c', '--config', nargs=1, metavar='CONFIG_PATH', type=str, \ help="Path to a config file which will be used instead of the default one.") # Parse the command line arguments cml_args = arg_parser.parse_args() ######################### dir_path = cml_args.dir_path[0] # Check if the given directory is OK if not os.path.exists(dir_path): print('No such directory:', dir_path) sys.exit() # Load the config file config = cr.loadConfigFromDirectory(cml_args.config, dir_path) # Get a list of files in the night folder file_list = os.listdir(dir_path) # Find and load the platepar file if config.platepar_name in file_list: # Load the platepar platepar = Platepar.Platepar() platepar.read(os.path.join(dir_path, config.platepar_name), use_flat=config.use_flat) else: print('Cannot find the platepar file in the night directory: ', config.platepar_name) sys.exit() # Find the CALSTARS file in the given folder calstars_file = None for calstars_file in file_list: if ('CALSTARS' in calstars_file) and ('.txt' in calstars_file): break if calstars_file is None: print('CALSTARS file could not be found in the given directory!') sys.exit() # Load the calstars file calstars_list = CALSTARS.readCALSTARS(dir_path, calstars_file) print('CALSTARS file: ' + calstars_file + ' loaded!') # Run the automatic astrometry fit pp, fit_status = autoCheckFit(config, platepar, calstars_list) # If the fit suceeded, save the platepar if fit_status: print('ACF sucessful!') # Save the old platepar shutil.move(os.path.join(dir_path, config.platepar_name), os.path.join(dir_path, config.platepar_name + '.old')) # Save the new platepar pp.write(os.path.join(dir_path, config.platepar_name))
gpl-3.0
mehdidc/scikit-learn
examples/gaussian_process/plot_gp_regression.py
253
4054
#!/usr/bin/python # -*- coding: utf-8 -*- r""" ========================================================= Gaussian Processes regression: basic introductory example ========================================================= A simple one-dimensional regression exercise computed in two different ways: 1. A noise-free case with a cubic correlation model 2. A noisy case with a squared Euclidean correlation model In both cases, the model parameters are estimated using the maximum likelihood principle. The figures illustrate the interpolating property of the Gaussian Process model as well as its probabilistic nature in the form of a pointwise 95% confidence interval. Note that the parameter ``nugget`` is applied as a Tikhonov regularization of the assumed covariance between the training points. In the special case of the squared euclidean correlation model, nugget is mathematically equivalent to a normalized variance: That is .. math:: \mathrm{nugget}_i = \left[\frac{\sigma_i}{y_i}\right]^2 """ print(__doc__) # Author: Vincent Dubourg <vincent.dubourg@gmail.com> # Jake Vanderplas <vanderplas@astro.washington.edu> # Licence: BSD 3 clause import numpy as np from sklearn.gaussian_process import GaussianProcess from matplotlib import pyplot as pl np.random.seed(1) def f(x): """The function to predict.""" return x * np.sin(x) #---------------------------------------------------------------------- # First the noiseless case X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T # Observations y = f(X).ravel() # Mesh the input space for evaluations of the real function, the prediction and # its MSE x = np.atleast_2d(np.linspace(0, 10, 1000)).T # Instanciate a Gaussian Process model gp = GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1e-1, random_start=100) # Fit to data using Maximum Likelihood Estimation of the parameters gp.fit(X, y) # Make the prediction on the meshed x-axis (ask for MSE as well) y_pred, MSE = gp.predict(x, eval_MSE=True) sigma = np.sqrt(MSE) # Plot the function, the prediction and the 95% confidence interval based on # the MSE fig = pl.figure() pl.plot(x, f(x), 'r:', label=u'$f(x) = x\,\sin(x)$') pl.plot(X, y, 'r.', markersize=10, label=u'Observations') pl.plot(x, y_pred, 'b-', label=u'Prediction') pl.fill(np.concatenate([x, x[::-1]]), np.concatenate([y_pred - 1.9600 * sigma, (y_pred + 1.9600 * sigma)[::-1]]), alpha=.5, fc='b', ec='None', label='95% confidence interval') pl.xlabel('$x$') pl.ylabel('$f(x)$') pl.ylim(-10, 20) pl.legend(loc='upper left') #---------------------------------------------------------------------- # now the noisy case X = np.linspace(0.1, 9.9, 20) X = np.atleast_2d(X).T # Observations and noise y = f(X).ravel() dy = 0.5 + 1.0 * np.random.random(y.shape) noise = np.random.normal(0, dy) y += noise # Mesh the input space for evaluations of the real function, the prediction and # its MSE x = np.atleast_2d(np.linspace(0, 10, 1000)).T # Instanciate a Gaussian Process model gp = GaussianProcess(corr='squared_exponential', theta0=1e-1, thetaL=1e-3, thetaU=1, nugget=(dy / y) ** 2, random_start=100) # Fit to data using Maximum Likelihood Estimation of the parameters gp.fit(X, y) # Make the prediction on the meshed x-axis (ask for MSE as well) y_pred, MSE = gp.predict(x, eval_MSE=True) sigma = np.sqrt(MSE) # Plot the function, the prediction and the 95% confidence interval based on # the MSE fig = pl.figure() pl.plot(x, f(x), 'r:', label=u'$f(x) = x\,\sin(x)$') pl.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label=u'Observations') pl.plot(x, y_pred, 'b-', label=u'Prediction') pl.fill(np.concatenate([x, x[::-1]]), np.concatenate([y_pred - 1.9600 * sigma, (y_pred + 1.9600 * sigma)[::-1]]), alpha=.5, fc='b', ec='None', label='95% confidence interval') pl.xlabel('$x$') pl.ylabel('$f(x)$') pl.ylim(-10, 20) pl.legend(loc='upper left') pl.show()
bsd-3-clause
cscorley/doc2vec-feature-location
scripts/boxplots.py
2
1180
# coding: utf-8 # In[1]: import numpy as np import matplotlib.pyplot as plt import gensim import src.main # In[2]: def get_all_ranks(project): r_lda = [x for x,y,z in src.main.read_ranks(project, 'release_lda')] r_lsi = [x for x,y,z in src.main.read_ranks(project, 'release_lsi')] c_lda = [x for x,y,z in src.main.read_ranks(project, 'changeset_lda')] c_lsi = [x for x,y,z in src.main.read_ranks(project, 'changeset_lsi')] try: t_lda = [x for x,y,z in src.main.read_ranks(project, 'temporal_lda')] t_lsi = [x for x,y,z in src.main.read_ranks(project, 'temporal_lsi')] except: t_lda = [] t_lsi = [] return r_lda, c_lda, t_lda, r_lsi, c_lsi, t_lsi # In[3]: projects = src.main.load_projects() # In[8]: for project in projects: ranks = get_all_ranks(project) fig = plt.figure(dpi=300) fig.gca().boxplot(ranks, labels=['S-LDA', 'C-LDA', 'T-LDA', 'S-LSI', 'C-LSI', 'T-LSI']) fig.gca().set_title(' '.join([project.name, project.version, project.level])) plt.savefig('paper/figures/' + project.name + project.version + project.level + '.png') plt.close() # In[ ]:
bsd-3-clause
jmetzen/scikit-learn
examples/svm/plot_iris.py
225
3252
""" ================================================== Plot different SVM classifiers in the iris dataset ================================================== Comparison of different linear SVM classifiers on a 2D projection of the iris dataset. We only consider the first 2 features of this dataset: - Sepal length - Sepal width This example shows how to plot the decision surface for four SVM classifiers with different kernels. The linear models ``LinearSVC()`` and ``SVC(kernel='linear')`` yield slightly different decision boundaries. This can be a consequence of the following differences: - ``LinearSVC`` minimizes the squared hinge loss while ``SVC`` minimizes the regular hinge loss. - ``LinearSVC`` uses the One-vs-All (also known as One-vs-Rest) multiclass reduction while ``SVC`` uses the One-vs-One multiclass reduction. Both linear models have linear decision boundaries (intersecting hyperplanes) while the non-linear kernel models (polynomial or Gaussian RBF) have more flexible non-linear decision boundaries with shapes that depend on the kind of kernel and its parameters. .. NOTE:: while plotting the decision function of classifiers for toy 2D datasets can help get an intuitive understanding of their respective expressive power, be aware that those intuitions don't always generalize to more realistic high-dimensional problems. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target h = .02 # step size in the mesh # we create an instance of SVM and fit out data. We do not scale our # data since we want to plot the support vectors C = 1.0 # SVM regularization parameter svc = svm.SVC(kernel='linear', C=C).fit(X, y) rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y) poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y) lin_svc = svm.LinearSVC(C=C).fit(X, y) # create a mesh to plot in x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # title for the plots titles = ['SVC with linear kernel', 'LinearSVC (linear kernel)', 'SVC with RBF kernel', 'SVC with polynomial (degree 3) kernel'] for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)): # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. plt.subplot(2, 2, i + 1) plt.subplots_adjust(wspace=0.4, hspace=0.4) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8) # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired) plt.xlabel('Sepal length') plt.ylabel('Sepal width') plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.xticks(()) plt.yticks(()) plt.title(titles[i]) plt.show()
bsd-3-clause
Ziqi-Li/bknqgis
pandas/pandas/tests/plotting/test_series.py
2
32812
# coding: utf-8 """ Test cases for Series.plot """ import itertools import pytest from datetime import datetime import pandas as pd from pandas import Series, DataFrame, date_range from pandas.compat import range, lrange import pandas.util.testing as tm import numpy as np from numpy.random import randn import pandas.plotting as plotting from pandas.tests.plotting.common import (TestPlotBase, _check_plot_works, _skip_if_no_scipy_gaussian_kde, _ok_for_gaussian_kde) tm._skip_if_no_mpl() class TestSeriesPlots(TestPlotBase): def setup_method(self, method): TestPlotBase.setup_method(self, method) import matplotlib as mpl mpl.rcdefaults() self.ts = tm.makeTimeSeries() self.ts.name = 'ts' self.series = tm.makeStringSeries() self.series.name = 'series' self.iseries = tm.makePeriodSeries() self.iseries.name = 'iseries' @pytest.mark.slow def test_plot(self): _check_plot_works(self.ts.plot, label='foo') _check_plot_works(self.ts.plot, use_index=False) axes = _check_plot_works(self.ts.plot, rot=0) self._check_ticks_props(axes, xrot=0) ax = _check_plot_works(self.ts.plot, style='.', logy=True) self._check_ax_scales(ax, yaxis='log') ax = _check_plot_works(self.ts.plot, style='.', logx=True) self._check_ax_scales(ax, xaxis='log') ax = _check_plot_works(self.ts.plot, style='.', loglog=True) self._check_ax_scales(ax, xaxis='log', yaxis='log') _check_plot_works(self.ts[:10].plot.bar) _check_plot_works(self.ts.plot.area, stacked=False) _check_plot_works(self.iseries.plot) for kind in ['line', 'bar', 'barh', 'kde', 'hist', 'box']: if not _ok_for_gaussian_kde(kind): continue _check_plot_works(self.series[:5].plot, kind=kind) _check_plot_works(self.series[:10].plot.barh) ax = _check_plot_works(Series(randn(10)).plot.bar, color='black') self._check_colors([ax.patches[0]], facecolors=['black']) # GH 6951 ax = _check_plot_works(self.ts.plot, subplots=True) self._check_axes_shape(ax, axes_num=1, layout=(1, 1)) ax = _check_plot_works(self.ts.plot, subplots=True, layout=(-1, 1)) self._check_axes_shape(ax, axes_num=1, layout=(1, 1)) ax = _check_plot_works(self.ts.plot, subplots=True, layout=(1, -1)) self._check_axes_shape(ax, axes_num=1, layout=(1, 1)) @pytest.mark.slow def test_plot_figsize_and_title(self): # figsize and title _, ax = self.plt.subplots() ax = self.series.plot(title='Test', figsize=(16, 8), ax=ax) self._check_text_labels(ax.title, 'Test') self._check_axes_shape(ax, axes_num=1, layout=(1, 1), figsize=(16, 8)) def test_dont_modify_rcParams(self): # GH 8242 if self.mpl_ge_1_5_0: key = 'axes.prop_cycle' else: key = 'axes.color_cycle' colors = self.plt.rcParams[key] _, ax = self.plt.subplots() Series([1, 2, 3]).plot(ax=ax) assert colors == self.plt.rcParams[key] def test_ts_line_lim(self): fig, ax = self.plt.subplots() ax = self.ts.plot(ax=ax) xmin, xmax = ax.get_xlim() lines = ax.get_lines() assert xmin == lines[0].get_data(orig=False)[0][0] assert xmax == lines[0].get_data(orig=False)[0][-1] tm.close() ax = self.ts.plot(secondary_y=True, ax=ax) xmin, xmax = ax.get_xlim() lines = ax.get_lines() assert xmin == lines[0].get_data(orig=False)[0][0] assert xmax == lines[0].get_data(orig=False)[0][-1] def test_ts_area_lim(self): _, ax = self.plt.subplots() ax = self.ts.plot.area(stacked=False, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] assert xmin == line[0] assert xmax == line[-1] tm.close() # GH 7471 _, ax = self.plt.subplots() ax = self.ts.plot.area(stacked=False, x_compat=True, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] assert xmin == line[0] assert xmax == line[-1] tm.close() tz_ts = self.ts.copy() tz_ts.index = tz_ts.tz_localize('GMT').tz_convert('CET') _, ax = self.plt.subplots() ax = tz_ts.plot.area(stacked=False, x_compat=True, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] assert xmin == line[0] assert xmax == line[-1] tm.close() _, ax = self.plt.subplots() ax = tz_ts.plot.area(stacked=False, secondary_y=True, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] assert xmin == line[0] assert xmax == line[-1] def test_label(self): s = Series([1, 2]) _, ax = self.plt.subplots() ax = s.plot(label='LABEL', legend=True, ax=ax) self._check_legend_labels(ax, labels=['LABEL']) self.plt.close() _, ax = self.plt.subplots() ax = s.plot(legend=True, ax=ax) self._check_legend_labels(ax, labels=['None']) self.plt.close() # get name from index s.name = 'NAME' _, ax = self.plt.subplots() ax = s.plot(legend=True, ax=ax) self._check_legend_labels(ax, labels=['NAME']) self.plt.close() # override the default _, ax = self.plt.subplots() ax = s.plot(legend=True, label='LABEL', ax=ax) self._check_legend_labels(ax, labels=['LABEL']) self.plt.close() # Add lebel info, but don't draw _, ax = self.plt.subplots() ax = s.plot(legend=False, label='LABEL', ax=ax) assert ax.get_legend() is None # Hasn't been drawn ax.legend() # draw it self._check_legend_labels(ax, labels=['LABEL']) def test_line_area_nan_series(self): values = [1, 2, np.nan, 3] s = Series(values) ts = Series(values, index=tm.makeDateIndex(k=4)) for d in [s, ts]: ax = _check_plot_works(d.plot) masked = ax.lines[0].get_ydata() # remove nan for comparison purpose exp = np.array([1, 2, 3], dtype=np.float64) tm.assert_numpy_array_equal(np.delete(masked.data, 2), exp) tm.assert_numpy_array_equal( masked.mask, np.array([False, False, True, False])) expected = np.array([1, 2, 0, 3], dtype=np.float64) ax = _check_plot_works(d.plot, stacked=True) tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected) ax = _check_plot_works(d.plot.area) tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected) ax = _check_plot_works(d.plot.area, stacked=False) tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected) def test_line_use_index_false(self): s = Series([1, 2, 3], index=['a', 'b', 'c']) s.index.name = 'The Index' _, ax = self.plt.subplots() ax = s.plot(use_index=False, ax=ax) label = ax.get_xlabel() assert label == '' _, ax = self.plt.subplots() ax2 = s.plot.bar(use_index=False, ax=ax) label2 = ax2.get_xlabel() assert label2 == '' @pytest.mark.slow def test_bar_log(self): expected = np.array([1., 10., 100., 1000.]) if not self.mpl_le_1_2_1: expected = np.hstack((.1, expected, 1e4)) _, ax = self.plt.subplots() ax = Series([200, 500]).plot.bar(log=True, ax=ax) tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), expected) tm.close() _, ax = self.plt.subplots() ax = Series([200, 500]).plot.barh(log=True, ax=ax) tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), expected) tm.close() # GH 9905 expected = np.array([1.0e-03, 1.0e-02, 1.0e-01, 1.0e+00]) if not self.mpl_le_1_2_1: expected = np.hstack((1.0e-04, expected, 1.0e+01)) if self.mpl_ge_2_0_0: expected = np.hstack((1.0e-05, expected)) _, ax = self.plt.subplots() ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='bar', ax=ax) ymin = 0.0007943282347242822 if self.mpl_ge_2_0_0 else 0.001 ymax = 0.12589254117941673 if self.mpl_ge_2_0_0 else .10000000000000001 res = ax.get_ylim() tm.assert_almost_equal(res[0], ymin) tm.assert_almost_equal(res[1], ymax) tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), expected) tm.close() _, ax = self.plt.subplots() ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='barh', ax=ax) res = ax.get_xlim() tm.assert_almost_equal(res[0], ymin) tm.assert_almost_equal(res[1], ymax) tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), expected) @pytest.mark.slow def test_bar_ignore_index(self): df = Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) _, ax = self.plt.subplots() ax = df.plot.bar(use_index=False, ax=ax) self._check_text_labels(ax.get_xticklabels(), ['0', '1', '2', '3']) def test_rotation(self): df = DataFrame(randn(5, 5)) # Default rot 0 _, ax = self.plt.subplots() axes = df.plot(ax=ax) self._check_ticks_props(axes, xrot=0) _, ax = self.plt.subplots() axes = df.plot(rot=30, ax=ax) self._check_ticks_props(axes, xrot=30) def test_irregular_datetime(self): rng = date_range('1/1/2000', '3/1/2000') rng = rng[[0, 1, 2, 3, 5, 9, 10, 11, 12]] ser = Series(randn(len(rng)), rng) _, ax = self.plt.subplots() ax = ser.plot(ax=ax) xp = datetime(1999, 1, 1).toordinal() ax.set_xlim('1/1/1999', '1/1/2001') assert xp == ax.get_xlim()[0] @pytest.mark.slow def test_pie_series(self): # if sum of values is less than 1.0, pie handle them as rate and draw # semicircle. series = Series(np.random.randint(1, 5), index=['a', 'b', 'c', 'd', 'e'], name='YLABEL') ax = _check_plot_works(series.plot.pie) self._check_text_labels(ax.texts, series.index) assert ax.get_ylabel() == 'YLABEL' # without wedge labels ax = _check_plot_works(series.plot.pie, labels=None) self._check_text_labels(ax.texts, [''] * 5) # with less colors than elements color_args = ['r', 'g', 'b'] ax = _check_plot_works(series.plot.pie, colors=color_args) color_expected = ['r', 'g', 'b', 'r', 'g'] self._check_colors(ax.patches, facecolors=color_expected) # with labels and colors labels = ['A', 'B', 'C', 'D', 'E'] color_args = ['r', 'g', 'b', 'c', 'm'] ax = _check_plot_works(series.plot.pie, labels=labels, colors=color_args) self._check_text_labels(ax.texts, labels) self._check_colors(ax.patches, facecolors=color_args) # with autopct and fontsize ax = _check_plot_works(series.plot.pie, colors=color_args, autopct='%.2f', fontsize=7) pcts = ['{0:.2f}'.format(s * 100) for s in series.values / float(series.sum())] iters = [iter(series.index), iter(pcts)] expected_texts = list(next(it) for it in itertools.cycle(iters)) self._check_text_labels(ax.texts, expected_texts) for t in ax.texts: assert t.get_fontsize() == 7 # includes negative value with pytest.raises(ValueError): series = Series([1, 2, 0, 4, -1], index=['a', 'b', 'c', 'd', 'e']) series.plot.pie() # includes nan series = Series([1, 2, np.nan, 4], index=['a', 'b', 'c', 'd'], name='YLABEL') ax = _check_plot_works(series.plot.pie) self._check_text_labels(ax.texts, ['a', 'b', '', 'd']) def test_pie_nan(self): s = Series([1, np.nan, 1, 1]) _, ax = self.plt.subplots() ax = s.plot.pie(legend=True, ax=ax) expected = ['0', '', '2', '3'] result = [x.get_text() for x in ax.texts] assert result == expected @pytest.mark.slow def test_hist_df_kwargs(self): df = DataFrame(np.random.randn(10, 2)) _, ax = self.plt.subplots() ax = df.plot.hist(bins=5, ax=ax) assert len(ax.patches) == 10 @pytest.mark.slow def test_hist_df_with_nonnumerics(self): # GH 9853 with tm.RNGContext(1): df = DataFrame( np.random.randn(10, 4), columns=['A', 'B', 'C', 'D']) df['E'] = ['x', 'y'] * 5 _, ax = self.plt.subplots() ax = df.plot.hist(bins=5, ax=ax) assert len(ax.patches) == 20 _, ax = self.plt.subplots() ax = df.plot.hist(ax=ax) # bins=10 assert len(ax.patches) == 40 @pytest.mark.slow def test_hist_legacy(self): _check_plot_works(self.ts.hist) _check_plot_works(self.ts.hist, grid=False) _check_plot_works(self.ts.hist, figsize=(8, 10)) # _check_plot_works adds an ax so catch warning. see GH #13188 with tm.assert_produces_warning(UserWarning): _check_plot_works(self.ts.hist, by=self.ts.index.month) with tm.assert_produces_warning(UserWarning): _check_plot_works(self.ts.hist, by=self.ts.index.month, bins=5) fig, ax = self.plt.subplots(1, 1) _check_plot_works(self.ts.hist, ax=ax) _check_plot_works(self.ts.hist, ax=ax, figure=fig) _check_plot_works(self.ts.hist, figure=fig) tm.close() fig, (ax1, ax2) = self.plt.subplots(1, 2) _check_plot_works(self.ts.hist, figure=fig, ax=ax1) _check_plot_works(self.ts.hist, figure=fig, ax=ax2) with pytest.raises(ValueError): self.ts.hist(by=self.ts.index, figure=fig) @pytest.mark.slow def test_hist_bins_legacy(self): df = DataFrame(np.random.randn(10, 2)) ax = df.hist(bins=2)[0][0] assert len(ax.patches) == 2 @pytest.mark.slow def test_hist_layout(self): df = self.hist_df with pytest.raises(ValueError): df.height.hist(layout=(1, 1)) with pytest.raises(ValueError): df.height.hist(layout=[1, 1]) @pytest.mark.slow def test_hist_layout_with_by(self): df = self.hist_df # _check_plot_works adds an ax so catch warning. see GH #13188 with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.gender, layout=(2, 1)) self._check_axes_shape(axes, axes_num=2, layout=(2, 1)) with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.gender, layout=(3, -1)) self._check_axes_shape(axes, axes_num=2, layout=(3, 1)) with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.category, layout=(4, 1)) self._check_axes_shape(axes, axes_num=4, layout=(4, 1)) with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.category, layout=(2, -1)) self._check_axes_shape(axes, axes_num=4, layout=(2, 2)) with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.category, layout=(3, -1)) self._check_axes_shape(axes, axes_num=4, layout=(3, 2)) with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.category, layout=(-1, 4)) self._check_axes_shape(axes, axes_num=4, layout=(1, 4)) with tm.assert_produces_warning(UserWarning): axes = _check_plot_works(df.height.hist, by=df.classroom, layout=(2, 2)) self._check_axes_shape(axes, axes_num=3, layout=(2, 2)) axes = df.height.hist(by=df.category, layout=(4, 2), figsize=(12, 7)) self._check_axes_shape(axes, axes_num=4, layout=(4, 2), figsize=(12, 7)) @pytest.mark.slow def test_hist_no_overlap(self): from matplotlib.pyplot import subplot, gcf x = Series(randn(2)) y = Series(randn(2)) subplot(121) x.hist() subplot(122) y.hist() fig = gcf() axes = fig.axes if self.mpl_ge_1_5_0 else fig.get_axes() assert len(axes) == 2 @pytest.mark.slow def test_hist_secondary_legend(self): # GH 9610 df = DataFrame(np.random.randn(30, 4), columns=list('abcd')) # primary -> secondary _, ax = self.plt.subplots() ax = df['a'].plot.hist(legend=True, ax=ax) df['b'].plot.hist(ax=ax, legend=True, secondary_y=True) # both legends are dran on left ax # left and right axis must be visible self._check_legend_labels(ax, labels=['a', 'b (right)']) assert ax.get_yaxis().get_visible() assert ax.right_ax.get_yaxis().get_visible() tm.close() # secondary -> secondary _, ax = self.plt.subplots() ax = df['a'].plot.hist(legend=True, secondary_y=True, ax=ax) df['b'].plot.hist(ax=ax, legend=True, secondary_y=True) # both legends are draw on left ax # left axis must be invisible, right axis must be visible self._check_legend_labels(ax.left_ax, labels=['a (right)', 'b (right)']) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() tm.close() # secondary -> primary _, ax = self.plt.subplots() ax = df['a'].plot.hist(legend=True, secondary_y=True, ax=ax) # right axes is returned df['b'].plot.hist(ax=ax, legend=True) # both legends are draw on left ax # left and right axis must be visible self._check_legend_labels(ax.left_ax, labels=['a (right)', 'b']) assert ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() tm.close() @pytest.mark.slow def test_df_series_secondary_legend(self): # GH 9779 df = DataFrame(np.random.randn(30, 3), columns=list('abc')) s = Series(np.random.randn(30), name='x') # primary -> secondary (without passing ax) _, ax = self.plt.subplots() ax = df.plot(ax=ax) s.plot(legend=True, secondary_y=True, ax=ax) # both legends are dran on left ax # left and right axis must be visible self._check_legend_labels(ax, labels=['a', 'b', 'c', 'x (right)']) assert ax.get_yaxis().get_visible() assert ax.right_ax.get_yaxis().get_visible() tm.close() # primary -> secondary (with passing ax) _, ax = self.plt.subplots() ax = df.plot(ax=ax) s.plot(ax=ax, legend=True, secondary_y=True) # both legends are dran on left ax # left and right axis must be visible self._check_legend_labels(ax, labels=['a', 'b', 'c', 'x (right)']) assert ax.get_yaxis().get_visible() assert ax.right_ax.get_yaxis().get_visible() tm.close() # seconcary -> secondary (without passing ax) _, ax = self.plt.subplots() ax = df.plot(secondary_y=True, ax=ax) s.plot(legend=True, secondary_y=True, ax=ax) # both legends are dran on left ax # left axis must be invisible and right axis must be visible expected = ['a (right)', 'b (right)', 'c (right)', 'x (right)'] self._check_legend_labels(ax.left_ax, labels=expected) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() tm.close() # secondary -> secondary (with passing ax) _, ax = self.plt.subplots() ax = df.plot(secondary_y=True, ax=ax) s.plot(ax=ax, legend=True, secondary_y=True) # both legends are dran on left ax # left axis must be invisible and right axis must be visible expected = ['a (right)', 'b (right)', 'c (right)', 'x (right)'] self._check_legend_labels(ax.left_ax, expected) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() tm.close() # secondary -> secondary (with passing ax) _, ax = self.plt.subplots() ax = df.plot(secondary_y=True, mark_right=False, ax=ax) s.plot(ax=ax, legend=True, secondary_y=True) # both legends are dran on left ax # left axis must be invisible and right axis must be visible expected = ['a', 'b', 'c', 'x (right)'] self._check_legend_labels(ax.left_ax, expected) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() tm.close() @pytest.mark.slow def test_plot_fails_with_dupe_color_and_style(self): x = Series(randn(2)) with pytest.raises(ValueError): _, ax = self.plt.subplots() x.plot(style='k--', color='k', ax=ax) @pytest.mark.slow def test_hist_kde(self): _, ax = self.plt.subplots() ax = self.ts.plot.hist(logy=True, ax=ax) self._check_ax_scales(ax, yaxis='log') xlabels = ax.get_xticklabels() # ticks are values, thus ticklabels are blank self._check_text_labels(xlabels, [''] * len(xlabels)) ylabels = ax.get_yticklabels() self._check_text_labels(ylabels, [''] * len(ylabels)) tm._skip_if_no_scipy() _skip_if_no_scipy_gaussian_kde() _check_plot_works(self.ts.plot.kde) _check_plot_works(self.ts.plot.density) _, ax = self.plt.subplots() ax = self.ts.plot.kde(logy=True, ax=ax) self._check_ax_scales(ax, yaxis='log') xlabels = ax.get_xticklabels() self._check_text_labels(xlabels, [''] * len(xlabels)) ylabels = ax.get_yticklabels() self._check_text_labels(ylabels, [''] * len(ylabels)) @pytest.mark.slow def test_kde_kwargs(self): tm._skip_if_no_scipy() _skip_if_no_scipy_gaussian_kde() from numpy import linspace _check_plot_works(self.ts.plot.kde, bw_method=.5, ind=linspace(-100, 100, 20)) _check_plot_works(self.ts.plot.density, bw_method=.5, ind=linspace(-100, 100, 20)) _, ax = self.plt.subplots() ax = self.ts.plot.kde(logy=True, bw_method=.5, ind=linspace(-100, 100, 20), ax=ax) self._check_ax_scales(ax, yaxis='log') self._check_text_labels(ax.yaxis.get_label(), 'Density') @pytest.mark.slow def test_kde_missing_vals(self): tm._skip_if_no_scipy() _skip_if_no_scipy_gaussian_kde() s = Series(np.random.uniform(size=50)) s[0] = np.nan axes = _check_plot_works(s.plot.kde) # gh-14821: check if the values have any missing values assert any(~np.isnan(axes.lines[0].get_xdata())) @pytest.mark.slow def test_hist_kwargs(self): _, ax = self.plt.subplots() ax = self.ts.plot.hist(bins=5, ax=ax) assert len(ax.patches) == 5 self._check_text_labels(ax.yaxis.get_label(), 'Frequency') tm.close() if self.mpl_ge_1_3_1: _, ax = self.plt.subplots() ax = self.ts.plot.hist(orientation='horizontal', ax=ax) self._check_text_labels(ax.xaxis.get_label(), 'Frequency') tm.close() _, ax = self.plt.subplots() ax = self.ts.plot.hist(align='left', stacked=True, ax=ax) tm.close() @pytest.mark.slow def test_hist_kde_color(self): _, ax = self.plt.subplots() ax = self.ts.plot.hist(logy=True, bins=10, color='b', ax=ax) self._check_ax_scales(ax, yaxis='log') assert len(ax.patches) == 10 self._check_colors(ax.patches, facecolors=['b'] * 10) tm._skip_if_no_scipy() _skip_if_no_scipy_gaussian_kde() _, ax = self.plt.subplots() ax = self.ts.plot.kde(logy=True, color='r', ax=ax) self._check_ax_scales(ax, yaxis='log') lines = ax.get_lines() assert len(lines) == 1 self._check_colors(lines, ['r']) @pytest.mark.slow def test_boxplot_series(self): _, ax = self.plt.subplots() ax = self.ts.plot.box(logy=True, ax=ax) self._check_ax_scales(ax, yaxis='log') xlabels = ax.get_xticklabels() self._check_text_labels(xlabels, [self.ts.name]) ylabels = ax.get_yticklabels() self._check_text_labels(ylabels, [''] * len(ylabels)) @pytest.mark.slow def test_kind_both_ways(self): s = Series(range(3)) kinds = (plotting._core._common_kinds + plotting._core._series_kinds) _, ax = self.plt.subplots() for kind in kinds: if not _ok_for_gaussian_kde(kind): continue s.plot(kind=kind, ax=ax) getattr(s.plot, kind)() @pytest.mark.slow def test_invalid_plot_data(self): s = Series(list('abcd')) _, ax = self.plt.subplots() for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue with pytest.raises(TypeError): s.plot(kind=kind, ax=ax) @pytest.mark.slow def test_valid_object_plot(self): s = Series(lrange(10), dtype=object) for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue _check_plot_works(s.plot, kind=kind) def test_partially_invalid_plot_data(self): s = Series(['a', 'b', 1.0, 2]) _, ax = self.plt.subplots() for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue with pytest.raises(TypeError): s.plot(kind=kind, ax=ax) def test_invalid_kind(self): s = Series([1, 2]) with pytest.raises(ValueError): s.plot(kind='aasdf') @pytest.mark.slow def test_dup_datetime_index_plot(self): dr1 = date_range('1/1/2009', periods=4) dr2 = date_range('1/2/2009', periods=4) index = dr1.append(dr2) values = randn(index.size) s = Series(values, index=index) _check_plot_works(s.plot) @pytest.mark.slow def test_errorbar_plot(self): s = Series(np.arange(10), name='x') s_err = np.random.randn(10) d_err = DataFrame(randn(10, 2), index=s.index, columns=['x', 'y']) # test line and bar plots kinds = ['line', 'bar'] for kind in kinds: ax = _check_plot_works(s.plot, yerr=Series(s_err), kind=kind) self._check_has_errorbars(ax, xerr=0, yerr=1) ax = _check_plot_works(s.plot, yerr=s_err, kind=kind) self._check_has_errorbars(ax, xerr=0, yerr=1) ax = _check_plot_works(s.plot, yerr=s_err.tolist(), kind=kind) self._check_has_errorbars(ax, xerr=0, yerr=1) ax = _check_plot_works(s.plot, yerr=d_err, kind=kind) self._check_has_errorbars(ax, xerr=0, yerr=1) ax = _check_plot_works(s.plot, xerr=0.2, yerr=0.2, kind=kind) self._check_has_errorbars(ax, xerr=1, yerr=1) ax = _check_plot_works(s.plot, xerr=s_err) self._check_has_errorbars(ax, xerr=1, yerr=0) # test time series plotting ix = date_range('1/1/2000', '1/1/2001', freq='M') ts = Series(np.arange(12), index=ix, name='x') ts_err = Series(np.random.randn(12), index=ix) td_err = DataFrame(randn(12, 2), index=ix, columns=['x', 'y']) ax = _check_plot_works(ts.plot, yerr=ts_err) self._check_has_errorbars(ax, xerr=0, yerr=1) ax = _check_plot_works(ts.plot, yerr=td_err) self._check_has_errorbars(ax, xerr=0, yerr=1) # check incorrect lengths and types with pytest.raises(ValueError): s.plot(yerr=np.arange(11)) s_err = ['zzz'] * 10 # in mpl 1.5+ this is a TypeError with pytest.raises((ValueError, TypeError)): s.plot(yerr=s_err) def test_table(self): _check_plot_works(self.series.plot, table=True) _check_plot_works(self.series.plot, table=self.series) @pytest.mark.slow def test_series_grid_settings(self): # Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792 self._check_grid_settings(Series([1, 2, 3]), plotting._core._series_kinds + plotting._core._common_kinds) @pytest.mark.slow def test_standard_colors(self): from pandas.plotting._style import _get_standard_colors for c in ['r', 'red', 'green', '#FF0000']: result = _get_standard_colors(1, color=c) assert result == [c] result = _get_standard_colors(1, color=[c]) assert result == [c] result = _get_standard_colors(3, color=c) assert result == [c] * 3 result = _get_standard_colors(3, color=[c]) assert result == [c] * 3 @pytest.mark.slow def test_standard_colors_all(self): import matplotlib.colors as colors from pandas.plotting._style import _get_standard_colors # multiple colors like mediumaquamarine for c in colors.cnames: result = _get_standard_colors(num_colors=1, color=c) assert result == [c] result = _get_standard_colors(num_colors=1, color=[c]) assert result == [c] result = _get_standard_colors(num_colors=3, color=c) assert result == [c] * 3 result = _get_standard_colors(num_colors=3, color=[c]) assert result == [c] * 3 # single letter colors like k for c in colors.ColorConverter.colors: result = _get_standard_colors(num_colors=1, color=c) assert result == [c] result = _get_standard_colors(num_colors=1, color=[c]) assert result == [c] result = _get_standard_colors(num_colors=3, color=c) assert result == [c] * 3 result = _get_standard_colors(num_colors=3, color=[c]) assert result == [c] * 3 def test_series_plot_color_kwargs(self): # GH1890 _, ax = self.plt.subplots() ax = Series(np.arange(12) + 1).plot(color='green', ax=ax) self._check_colors(ax.get_lines(), linecolors=['green']) def test_time_series_plot_color_kwargs(self): # #1890 _, ax = self.plt.subplots() ax = Series(np.arange(12) + 1, index=date_range( '1/1/2000', periods=12)).plot(color='green', ax=ax) self._check_colors(ax.get_lines(), linecolors=['green']) def test_time_series_plot_color_with_empty_kwargs(self): import matplotlib as mpl if self.mpl_ge_1_5_0: def_colors = self._maybe_unpack_cycler(mpl.rcParams) else: def_colors = mpl.rcParams['axes.color_cycle'] index = date_range('1/1/2000', periods=12) s = Series(np.arange(1, 13), index=index) ncolors = 3 _, ax = self.plt.subplots() for i in range(ncolors): ax = s.plot(ax=ax) self._check_colors(ax.get_lines(), linecolors=def_colors[:ncolors]) def test_xticklabels(self): # GH11529 s = Series(np.arange(10), index=['P%02d' % i for i in range(10)]) _, ax = self.plt.subplots() ax = s.plot(xticks=[0, 3, 5, 9], ax=ax) exp = ['P%02d' % i for i in [0, 3, 5, 9]] self._check_text_labels(ax.get_xticklabels(), exp) def test_custom_business_day_freq(self): # GH7222 from pandas.tseries.offsets import CustomBusinessDay s = Series(range(100, 121), index=pd.bdate_range( start='2014-05-01', end='2014-06-01', freq=CustomBusinessDay(holidays=['2014-05-26']))) _check_plot_works(s.plot)
gpl-2.0
terrycojones/dark-matter
dark/mutations.py
1
16454
import os from collections import defaultdict import numpy as np try: import matplotlib if not os.environ.get('DISPLAY'): # Use non-interactive Agg backend matplotlib.use('Agg') import matplotlib.pyplot as plt except ImportError: import platform if platform.python_implementation() == 'PyPy': # PyPy doesn't have a version of matplotlib. Make a fake # class that raises if it is used. This allows us to use other # 'dark' code that happens to import dark.mutations but not use the # functions that rely on matplotlib. class plt(object): def __getattr__(self, _): raise NotImplementedError( 'matplotlib is not supported under pypy') else: raise from random import choice, uniform from dark import ncbidb def basePlotter(blastHits, title): """ Plot the reads and the subject, so that bases in the reads which are different from the subject are shown. Else a '.' is shown. like so: subject_gi ATGCGTACGTACGACACC read_1 A......TTC..T @param blastHits: A L{dark.blast.BlastHits} instance. @param title: A C{str} sequence title that was matched by BLAST. We plot the reads that matched this title. """ result = [] params = blastHits.plotParams assert params is not None, ('Oops, it looks like you forgot to run ' 'computePlotInfo.') sequence = ncbidb.getSequence(title, blastHits.records.blastDb) subject = sequence.seq gi = title.split('|')[1] sub = '%s\t \t \t%s' % (gi, subject) result.append(sub) plotInfo = blastHits.titles[title]['plotInfo'] assert plotInfo is not None, ('Oops, it looks like you forgot to run ' 'computePlotInfo.') items = plotInfo['items'] count = 0 for item in items: count += 1 hsp = item['hsp'] queryTitle = blastHits.fasta[item['readNum']].id # If the product of the subject and query frame values is +ve, # then they're either both +ve or both -ve, so we just use the # query as is. Otherwise, we need to reverse complement it. if item['frame']['subject'] * item['frame']['query'] > 0: query = blastHits.fasta[item['readNum']].seq reverse = False else: # One of the subject or query has negative sense. query = blastHits.fasta[ item['readNum']].reverse_complement().seq reverse = True query = query.upper() queryStart = hsp['queryStart'] subjectStart = hsp['subjectStart'] queryEnd = hsp['queryEnd'] subjectEnd = hsp['subjectEnd'] # Before comparing the read to the subject, make a string of the # same length as the subject, which contains the read and # has ' ' where the read does not match. # 3 parts need to be taken into account: # 1) the left offset (if the query doesn't stick out to the left) # 2) the query. if the frame is -1, it has to be reversed. # The query consists of 3 parts: left, middle (control for gaps) # 3) the right offset # Do part 1) and 2). if queryStart < 0: # The query is sticking out to the left. leftQuery = '' if subjectStart == 0: # The match starts at the first base of the subject. middleLeftQuery = '' else: # The match starts into the subject. # Determine the length of the not matching query # part to the left. leftOffset = -1 * queryStart rightOffset = subjectStart + leftOffset middleLeftQuery = query[leftOffset:rightOffset] else: # The query is not sticking out to the left # make the left offset. leftQuery = queryStart * ' ' leftQueryOffset = subjectStart - queryStart middleLeftQuery = query[:leftQueryOffset] # Do part 3). # Disregard gaps in subject while adding. matchQuery = item['origHsp'].query matchSubject = item['origHsp'].sbjct index = 0 mid = '' for item in range(len(matchQuery)): if matchSubject[index] != ' ': mid += matchQuery[index] index += 1 # if the query has been reversed, turn the matched part around if reverse: rev = '' toReverse = mid reverseDict = {' ': ' ', '-': '-', 'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C', '.': '.', 'N': 'N'} for item in toReverse: newItem = reverseDict[item] rev += newItem mid = rev[::-1] middleQuery = middleLeftQuery + mid # add right not-matching part of the query rightQueryOffset = queryEnd - subjectEnd rightQuery = query[-rightQueryOffset:] middleQuery += rightQuery read = leftQuery + middleQuery # do part 3) offset = len(subject) - len(read) # if the read is sticking out to the right # chop it off if offset < 0: read = read[:offset] # if it's not sticking out, fill the space with ' ' elif offset > 0: read += offset * ' ' # compare the subject and the read, make a string # called 'comparison', which contains a '.' if the bases # are equal and the letter of the read if they are not. comparison = '' for readBase, subjectBase in zip(read, subject): if readBase == ' ': comparison += ' ' elif readBase == subjectBase: comparison += '.' elif readBase != subjectBase: comparison += readBase index += 1 que = '%s \t %s' % (queryTitle, comparison) result.append(que) # sanity checks assert (len(comparison) == len(subject)), ( '%d != %d' % (len(comparison), len(subject))) index = 0 if comparison[index] == ' ': index += 1 else: start = index - 1 assert (start == queryStart or start == -1), ( '%s != %s or %s != -1' % (start, queryStart, start)) return result def getAPOBECFrequencies(dotAlignment, orig, new, pattern): """ Gets mutation frequencies if they are in a certain pattern. @param dotAlignment: result from calling basePlotter @param orig: A C{str}, naming the original base @param new: A C{str}, what orig was mutated to @param pattern: A C{str}m which pattern we're looking for (must be one of 'cPattern', 'tPattern') """ cPattern = ['ACA', 'ACC', 'ACG', 'ACT', 'CCA', 'CCC', 'CCG', 'CCT', 'GCA', 'GCC', 'GCG', 'GCT', 'TCA', 'TCC', 'TCG', 'TCT'] tPattern = ['ATA', 'ATC', 'ATG', 'ATT', 'CTA', 'CTC', 'CTG', 'CTT', 'GTA', 'GTC', 'GTG', 'GTT', 'TTA', 'TTC', 'TTG', 'TTT'] # choose the right pattern if pattern == 'cPattern': patterns = cPattern middleBase = 'C' else: patterns = tPattern middleBase = 'T' # generate the freqs dict with the right pattern freqs = defaultdict(int) for pattern in patterns: freqs[pattern] = 0 # get the subject sequence from dotAlignment subject = dotAlignment[0].split('\t')[3] # exclude the subject from the dotAlignment, so just the queries # are left over queries = dotAlignment[1:] for item in queries: query = item.split('\t')[1] index = 0 for queryBase in query: qBase = query[index] sBase = subject[index] if qBase == new and sBase == orig: try: plusSb = subject[index + 1] minusSb = subject[index - 1] except IndexError: plusSb = 'end' motif = '%s%s%s' % (minusSb, middleBase, plusSb) if motif in freqs: freqs[motif] += 1 index += 1 return freqs def getCompleteFreqs(blastHits): """ Make a dictionary which collects all mutation frequencies from all reads. Calls basePlotter to get dotAlignment, which is passed to getAPOBECFrequencies with the respective parameter, to collect the frequencies. @param blastHits: A L{dark.blast.BlastHits} instance. """ allFreqs = {} for title in blastHits.titles: allFreqs[title] = { 'C>A': {}, 'C>G': {}, 'C>T': {}, 'T>A': {}, 'T>C': {}, 'T>G': {}, } basesPlotted = basePlotter(blastHits, title) for mutation in allFreqs[title]: orig = mutation[0] new = mutation[2] if orig == 'C': pattern = 'cPattern' else: pattern = 'tPattern' freqs = getAPOBECFrequencies(basesPlotted, orig, new, pattern) allFreqs[title][mutation] = freqs numberOfReads = len(blastHits.titles[title]['plotInfo']['items']) allFreqs[title]['numberOfReads'] = numberOfReads allFreqs[title]['bitScoreMax'] = blastHits.titles[ title]['plotInfo']['bitScoreMax'] return allFreqs def makeFrequencyGraph(allFreqs, title, substitution, pattern, color='blue', createFigure=True, showFigure=True, readsAx=False): """ For a title, make a graph showing the frequencies. @param allFreqs: result from getCompleteFreqs @param title: A C{str}, title of virus of which frequencies should be plotted. @param substitution: A C{str}, which substitution should be plotted; must be one of 'C>A', 'C>G', 'C>T', 'T>A', 'T>C', 'T>G'. @param pattern: A C{str}, which pattern we're looking for ( must be one of 'cPattern', 'tPattern') @param color: A C{str}, color of bars. @param createFigure: If C{True}, create a figure. @param showFigure: If C{True}, show the created figure. @param readsAx: If not None, use this as the subplot for displaying reads. """ cPattern = ['ACA', 'ACC', 'ACG', 'ACT', 'CCA', 'CCC', 'CCG', 'CCT', 'GCA', 'GCC', 'GCG', 'GCT', 'TCA', 'TCC', 'TCG', 'TCT'] tPattern = ['ATA', 'ATC', 'ATG', 'ATT', 'CTA', 'CTC', 'CTG', 'CTT', 'GTA', 'GTC', 'GTG', 'GTT', 'TTA', 'TTC', 'TTG', 'TTT'] # choose the right pattern if pattern == 'cPattern': patterns = cPattern else: patterns = tPattern fig = plt.figure(figsize=(10, 10)) ax = readsAx or fig.add_subplot(111) # how many bars N = 16 ind = np.arange(N) width = 0.4 # make a list in the right order, so that it can be plotted easily divisor = allFreqs[title]['numberOfReads'] toPlot = allFreqs[title][substitution] index = 0 data = [] for item in patterns: newData = toPlot[patterns[index]] / divisor data.append(newData) index += 1 # create the bars ax.bar(ind, data, width, color=color) maxY = np.max(data) + 5 # axes and labels if createFigure: title = title.split('|')[4][:50] ax.set_title('%s \n %s' % (title, substitution), fontsize=20) ax.set_ylim(0, maxY) ax.set_ylabel('Absolute Number of Mutations', fontsize=16) ax.set_xticks(ind + width) ax.set_xticklabels(patterns, rotation=45, fontsize=8) if createFigure is False: ax.set_xticks(ind + width) ax.set_xticklabels(patterns, rotation=45, fontsize=0) else: if showFigure: plt.show() return maxY def makeFrequencyPanel(allFreqs, patientName): """ For a title, make a graph showing the frequencies. @param allFreqs: result from getCompleteFreqs @param patientName: A C{str}, title for the panel """ titles = sorted( iter(allFreqs.keys()), key=lambda title: (allFreqs[title]['bitScoreMax'], title)) origMaxY = 0 cols = 6 rows = len(allFreqs) figure, ax = plt.subplots(rows, cols, squeeze=False) substitutions = ['C>A', 'C>G', 'C>T', 'T>A', 'T>C', 'T>G'] colors = ['blue', 'black', 'red', 'yellow', 'green', 'orange'] for i, title in enumerate(titles): for index in range(6): for subst in allFreqs[str(title)]: substitution = substitutions[index] print(i, index, title, 'substitution', substitutions[index]) if substitution[0] == 'C': pattern = 'cPattern' else: pattern = 'tPattern' maxY = makeFrequencyGraph(allFreqs, title, substitution, pattern, color=colors[index], createFigure=False, showFigure=False, readsAx=ax[i][index]) if maxY > origMaxY: origMaxY = maxY # add title for individual plot. # if used for other viruses, this will have to be adapted. if index == 0: gi = title.split('|')[1] titles = title.split(' ') try: typeIndex = titles.index('type') except ValueError: typeNumber = 'gi: %s' % gi else: typeNumber = titles[typeIndex + 1] ax[i][index].set_ylabel(('Type %s \n maxBitScore: %s' % ( typeNumber, allFreqs[title]['bitScoreMax'])), fontsize=10) # add xAxis tick labels if i == 0: ax[i][index].set_title(substitution, fontsize=13) if i == len(allFreqs) - 1 or i == (len(allFreqs) - 1) / 2: if index < 3: pat = ['ACA', 'ACC', 'ACG', 'ACT', 'CCA', 'CCC', 'CCG', 'CCT', 'GCA', 'GCC', 'GCG', 'GCT', 'TCA', 'TCC', 'TCG', 'TCT'] else: pat = ['ATA', 'ATC', 'ATG', 'ATT', 'CTA', 'CTC', 'CTG', 'CTT', 'GTA', 'GTC', 'GTG', 'GTT', 'TTA', 'TTC', 'TTG', 'TTT'] ax[i][index].set_xticklabels(pat, rotation=45, fontsize=8) # make Y-axis equal for i, title in enumerate(allFreqs): for index in range(6): a = ax[i][index] a.set_ylim([0, origMaxY]) # add title of whole panel figure.suptitle('Mutation Signatures in %s' % patientName, fontsize=20) figure.set_size_inches(5 * cols, 3 * rows, forward=True) figure.show() return allFreqs def mutateString(original, n, replacements='acgt'): """ Mutate C{original} in C{n} places with chars chosen from C{replacements}. @param original: The original C{str} to mutate. @param n: The C{int} number of locations to mutate. @param replacements: The C{str} of replacement letters. @return: A new C{str} with C{n} places of C{original} mutated. @raises ValueError: if C{n} is too high, or C{replacement} contains duplicates, or if no replacement can be made at a certain locus because C{replacements} is of length one, or if C{original} is of zero length. """ if not original: raise ValueError('Empty original string passed.') if n > len(original): raise ValueError('Cannot make %d mutations in a string of length %d' % (n, len(original))) if len(replacements) != len(set(replacements)): raise ValueError('Replacement string contains duplicates') if len(replacements) == 1 and original.find(replacements) != -1: raise ValueError('Impossible replacement') result = list(original) length = len(original) for offset in range(length): if uniform(0.0, 1.0) < float(n) / (length - offset): # Mutate. while True: new = choice(replacements) if new != result[offset]: result[offset] = new break n -= 1 if n == 0: break return ''.join(result)
mit
nuclear-wizard/moose
test/tests/time_integrators/scalar/run.py
12
4487
#!/usr/bin/env python3 #* This file is part of the MOOSE framework #* https://www.mooseframework.org #* #* All rights reserved, see COPYRIGHT for full restrictions #* https://github.com/idaholab/moose/blob/master/COPYRIGHT #* #* Licensed under LGPL 2.1, please see LICENSE for details #* https://www.gnu.org/licenses/lgpl-2.1.html import subprocess import sys import csv import matplotlib.pyplot as plt import numpy as np # Use fonts that match LaTeX from matplotlib import rcParams rcParams['font.family'] = 'serif' rcParams['font.size'] = 17 rcParams['font.serif'] = ['Computer Modern Roman'] rcParams['text.usetex'] = True # Small font size for the legend from matplotlib.font_manager import FontProperties fontP = FontProperties() fontP.set_size('x-small') def get_last_row(csv_filename): ''' Function which returns just the last row of a CSV file. We have to read every line of the file, there was no stackoverflow example of reading just the last line. http://stackoverflow.com/questions/20296955/reading-last-row-from-csv-file-python-error ''' with open(csv_filename, 'r') as f: lastrow = None for row in csv.reader(f): if (row != []): # skip blank lines at end of file. lastrow = row return lastrow def run_moose(dt, time_integrator): ''' Function which actually runs MOOSE. ''' implicit_flag = 'true' explicit_methods = ['ExplicitEuler', 'ExplicitMidpoint', 'Heun', 'Ralston'] # Set implicit_flag based on TimeIntegrator name if (time_integrator in explicit_methods): implicit_flag = 'false' command_line_args = ['../../../moose_test-opt', '-i', 'scalar.i', 'Executioner/dt={}'.format(dt), 'Executioner/TimeIntegrator/type={}'.format(time_integrator), 'GlobalParams/implicit={}'.format(implicit_flag)] try: child = subprocess.Popen(command_line_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # communicate() waits for the process to terminate, so there's no # need to wait() for it. It also sets the returncode attribute on # child. (stdoutdata, stderrdata) = child.communicate() if (child.returncode != 0): print('Running MOOSE failed: program output is below:') print(stdoutdata) raise except: print('Error executing moose_test') sys.exit(1) # Parse the last line of the output file to get the error at the final time. last_row = get_last_row('scalar_out.csv') return float(last_row[1]) # # Main program # fig = plt.figure() ax1 = fig.add_subplot(111) # Lists of timesteps and TimeIntegrators to plot. time_integrators = ['ImplicitEuler', 'ImplicitMidpoint', 'LStableDirk2', 'BDF2', 'CrankNicolson', 'LStableDirk3', 'LStableDirk4', 'AStableDirk4', 'ExplicitEuler', 'ExplicitMidpoint', 'Heun', 'Ralston'] dts = [.125, .0625, .03125, .015625] # Plot colors colors = ['maroon', 'blue', 'green', 'black', 'burlywood', 'olivedrab', 'midnightblue', 'tomato', 'darkmagenta', 'chocolate', 'lightslategray', 'skyblue'] # Plot line markers markers = ['v', 'o', 'x', '^', 'H', 'h', '+', 'D', '*', '4', 'd', '8'] # Plot line styles linestyles = [':', '-', '-.', '--', ':', '-.', '--', ':', '--', '-', '-.', '-'] for i in xrange(len(time_integrators)): time_integrator = time_integrators[i] # Place to store the results for this TimeIntegrator results = [] # Call MOOSE to compute the results for dt in dts: results.append(run_moose(dt, time_integrator)) # Make plot xdata = np.log10(np.reciprocal(dts)) ydata = np.log10(results) # Compute linear fit of last three points. start_fit = len(xdata) - 3 end_fit = len(xdata) fit = np.polyfit(xdata[start_fit:end_fit], ydata[start_fit:end_fit], 1) # Make the plot -- unpack the user's additional plotting arguments # from kwargs by prepending with **. ax1.plot(xdata, ydata, label=time_integrator + ", $" + "{:.2f}".format(fit[0]) + "$", color=colors[i], marker=markers[i], linestyle=linestyles[i]) # Set up the axis labels. ax1.set_xlabel('$\log (\Delta t^{-1})$') ax1.set_ylabel('$\log \|e(T)\|_{L^2}$') # Add a legend plt.legend(loc='lower left', prop=fontP) # Save a PDF plt.savefig('plot.pdf', format='pdf') # Local Variables: # python-indent: 2 # End:
lgpl-2.1
OSU-CS-325/Project_Two_Coin_Change
run-files/analysisQ7.py
1
2957
import sys import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import random import datetime # Import the three change making algorithms sys.path.insert(0, "../divide-conquer/") sys.path.insert(0, "../dynamic-programming") sys.path.insert(0, "../greedy") from changeslow import changeslow from changegreedy import changegreedy from changedp import changedp ### QUESTION 7 ### def Q7(slow, minChange, maxChange): lenV = [] runtimeGreedy = [] runtimeDP = [] runtimeSlow = [] numExp = 10 maxRange = 1000 if (slow): maxRange = 10 # some much smaller number for i in range(1, maxRange): # V can be of length 1 to (maxRange - 1) print "\n------ running V length=" + str(i) + "------" lenV.append(i) #print "lenV:", lenV runtimeGreedy.append(0) runtimeDP.append(0) runtimeSlow.append(0) for j in range(numExp): # run numExp experiments for this length of V print "\n ---- running experiment=" + str(j + 1) + " ----" coinArray = [] for k in range(i): # generate V of size i [1, rand, ..., rand, max=1 + 5*(maxRange-2)] if (k == 0): coinArray.append(1) else: randFrom = coinArray[len(coinArray) - 1] + 1 randTo = coinArray[len(coinArray) - 1] + 5 coinArray.append(random.randint(randFrom, randTo)) change = random.randint(minChange, maxChange) #print " coinArray:", coinArray #print " change:", change print " running greedy..." start = datetime.datetime.now() _, _ = changegreedy(coinArray, change) end = datetime.datetime.now() delta = end - start delta = int(delta.total_seconds() * 1000000) print " " + str(delta) runtimeGreedy[i - 1] += delta print " running DP..." start = datetime.datetime.now() _, _ = changedp(coinArray, change) end = datetime.datetime.now() delta = end - start delta = int(delta.total_seconds() * 1000000) print " " + str(delta) runtimeDP[i - 1] += delta if (slow): print " running slow..." start = datetime.datetime.now() _, _ = changeslow(coinArray, change) end = datetime.datetime.now() delta = end - start delta = int(delta.total_seconds() * 1000000) print " " + str(delta) runtimeSlow[i - 1] += delta runtimeGreedy[i - 1] /= numExp runtimeDP[i - 1] /= numExp if (slow): runtimeSlow[i - 1] /= numExp plt.figure(21) plt.plot(lenV, runtimeGreedy, 'b-', linewidth=2.0, label='Greedy') plt.plot(lenV, runtimeDP, 'r--', linewidth=2.0, label='DP') if (slow): plt.plot(lenV, runtimeSlow, 'g-.', linewidth=2.0, label='Slow') plt.legend(loc='upper left') plt.title('Runtime vs len(V[]) for randomized V[] and A') plt.ylabel('Avg. Runtime (10^-6 sec)') plt.xlabel('len(V[])') plt.grid(True) if (slow): plt.savefig('img/Q7slow_runtime.png', bbox_inches='tight') else: plt.savefig('img/Q7_runtime.png', bbox_inches='tight') def main(): Q7(False, 100, 100) #Q7(True) if __name__ == "__main__": main()
mit
stormvirux/vturra-cli
vturra/asys.py
1
1936
#!/usr/bin/env python # -*- coding: utf-8 -*- import pandas as pd import numpy as np import matplotlib.pyplot as plt # from scipy import stats # import statsmodels.api as sm # from numpy.random import randn import matplotlib as mpl # import seaborn as sns # sns.set_color_palette("deep", desat=.6) mpl.rc("figure", figsize=(8, 4)) def Compavg(): data=Total() markMax=[] markAvg=[] N = 5 ind = np.arange(N) width = 0.35 fig = plt.figure() ax = fig.add_subplot(111) markMax.extend((data["Total"].max(),data["Total.1"].max(),data["Total.2"].max(),data["Total.3"].max(),data["Total.4"].max())) markAvg.extend((data["Total"].mean(),data["Total.1"].mean(),data["Total.2"].mean(),data["Total.3"].mean(),data["Total.4"].mean())) rects1 = ax.bar(ind, markMax, width, color='black') rects2 = ax.bar(ind+width, markAvg, width, color='green') ax.set_xlim(-width,len(ind)+width) ax.set_ylim(0,120) ax.set_ylabel('Marks') ax.set_title('Max, Mean and Your Marks') xTickMarks = ['Subject'+str(i) for i in range(1,6)] ax.set_xticks(ind+width) xtickNames = ax.set_xticklabels(xTickMarks) plt.setp(xtickNames, rotation=10, fontsize=10) ax.legend( (rects1[0], rects2[0]), ('Max', 'Mean') ) plt.show() def compSub(): # max_data = np.r_[data["Total"]].max() # bins = np.linspace(0, max_data, max_data + 1) data=Total() plt.hist(data['Total'],linewidth=0, alpha=.7) plt.hist(data['Total.1'],linewidth=0,alpha=.7) plt.hist(data['Total.2'],linewidth=0,alpha=.7) plt.hist(data['Total.3'],linewidth=0,alpha=.7) plt.hist(data['Total.4'],linewidth=0,alpha=.7) plt.title("Total marks Histogram") plt.xlabel("Value") plt.ylabel("Frequency") plt.show() def Total(): data=pd.read_csv("output10cs.csv") df3=data[['Total','Total.1','Total.2','Total.3','Total.4','Total.5','Total.6','Total.7']] data["Main Total"]=df3.sum(axis=1) data = data.dropna() data.reset_index(drop=True) return data #compSub() # Compavg()
mit
abhishekkrthakur/scikit-learn
examples/svm/plot_oneclass.py
249
2302
""" ========================================== One-class SVM with non-linear kernel (RBF) ========================================== An example using a one-class SVM for novelty detection. :ref:`One-class SVM <svm_outlier_detection>` is an unsupervised algorithm that learns a decision function for novelty detection: classifying new data as similar or different to the training set. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) # Generate train data X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations X = 0.3 * np.random.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate some abnormal novel observations X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) # fit the model clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) n_error_train = y_pred_train[y_pred_train == -1].size n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size # plot the line, the points, and the nearest vectors to the plane Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.title("Novelty Detection") plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.Blues_r) a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='red') plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors='orange') b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white') b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='green') c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='red') plt.axis('tight') plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.legend([a.collections[0], b1, b2, c], ["learned frontier", "training observations", "new regular observations", "new abnormal observations"], loc="upper left", prop=matplotlib.font_manager.FontProperties(size=11)) plt.xlabel( "error train: %d/200 ; errors novel regular: %d/40 ; " "errors novel abnormal: %d/40" % (n_error_train, n_error_test, n_error_outliers)) plt.show()
bsd-3-clause
sssundar/Drone
rotation/viz.py
1
5332
# Python script to visualize rotation about a non-body axis. # Let the lab frame be the inertial frame S. # Let the origin of the rigid body be O, in the inertial frame S'. # Let r_ss' be the vector from S to S'. # Let the body frame relative to O be S''. # Consider a fixed point on the body, r_s' in S', and r_s'' in S''. # Assume the body is subject to zero external torques. # It must be rotating about a fixed axis, n, by Euler's rotation theorem. # It must have a constant angular velocity about that axis by d/dt L = sum(T_external) = 0 and L = Jw about the rotation axis. # Let R be the rotation matrix mapping a vector in S'' to S', with inverse R^T # We know r_s' = R r_s'' # We know d/dt r_s' = (dR/dt R^T) * (R r_s'') = (dR/dt R^T) r_s' # Therefore we expect (dR/dt R^T) to be the operator (w x) in the S' frame. # The goal of this script is to visualize this. import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import sys import numpy as np from numpy import pi as pi from numpy import cos as c from numpy import sin as s from numpy import dot as dot from numpy import transpose as transpose # The axis phi is a rotation about the z axis in the body frame (yaw) # The axis theta is a rotation about the y axis in the phi-rotated body frame (pitch) # The axis psi is a rotation about the x axis in the phi, theta-rotated body frame (roll) def R(phi, theta, psi): R = np.zeros((3,3)) R[0,0] = c(phi)*c(theta) R[1,0] = s(phi)*c(theta) R[2,0] = -s(theta) R[0,1] = -s(phi)*c(psi) + c(phi)*s(theta)*s(psi) R[1,1] = c(phi)*c(psi) + s(phi)*s(theta)*s(psi) R[2,1] = c(theta)*s(psi) R[0,2] = s(phi)*s(psi) + c(phi)*s(theta)*c(psi) R[1,2] = -c(phi)*s(psi) + s(phi)*s(theta)*c(psi) R[2,2] = c(theta)*c(psi) return R # Rotate z-axis (0,0,1) by pi radians about x-axis. Should end up at (0,0,-1) cutting across y. # Rotate (0,0,-1) by pi radians about y-axis. Should end up at (0,0,1) again, cutting across x. # Try both at the same time. Should still end up at (0,0,1). def test_R(): e3_spp = np.array((0,0,1)) vectors = [] for k in np.linspace(0,pi,100): vectors.append(dot(R(0,0,k), e3_spp)) e3_spp = vectors[-1] for k in np.linspace(0,pi,100): vectors.append(dot(R(0,k,0), e3_spp)) e3_spp = vectors[-1] for k in np.linspace(0,pi,100): vectors.append(dot(R(0,k,k), e3_spp)) xs = [k[0] for k in vectors] ys = [k[1] for k in vectors] zs = [k[2] for k in vectors] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(xs=xs,ys=ys,zs=zs) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") plt.show() # Sets values lower than epsilon to zero. # Prints the result with precision 0.3f. def sanitize_matrix(A): print "" epsilon = 0.001 for r in xrange(3): text = "" for c in xrange(3): if abs(A[r, c]) < epsilon: A[r,c] = 0 text += "%6.2f,\t" % A[r,c] print text[:-2] print "" def sanitize_vector(a): print "" epsilon = 0.001 text = "" for r in xrange(3): if abs(a[r]) < epsilon: a[r] = 0 text += "%6.2f,\t" % a[r] print text[:-2] print "" def vectorize(W): v = np.zeros(3) v[0] = W[1,0] v[1] = W[0,2] v[2] = W[2,1] return v # This is the (w x) operator, W, with respect to changing body yaw, pitch, and roll. # It is dR/dt R^T. The arguments are the current Euler angles and their time derivatives. def W(phi, theta, psi, dphi, dtheta, dpsi): Rp = np.zeros((3,3)) Rp[0,0] = (-s(phi)*dphi)*c(theta) Rp[0,0] += c(phi)*(-s(theta)*dtheta) Rp[1,0] = (c(phi)*dphi)*c(theta) Rp[1,0] += s(phi)*(-s(theta)*dtheta) Rp[2,0] = -c(theta)*dtheta Rp[0,1] = (-c(phi)*dphi)*c(psi) Rp[0,1] += -s(phi)*(-s(psi)*dpsi) Rp[0,1] += (-s(phi)*dphi)*s(theta)*s(psi) Rp[0,1] += c(phi)*(c(theta)*dtheta)*s(psi) Rp[0,1] += c(phi)*s(theta)*(c(psi)*dpsi) Rp[1,1] = (-s(phi)*dphi)*c(psi) Rp[1,1] += c(phi)*(-s(psi)*dpsi) Rp[1,1] += (c(phi)*dphi)*s(theta)*s(psi) Rp[1,1] += s(phi)*(c(theta)*dtheta)*s(psi) Rp[1,1] += s(phi)*s(theta)*(c(psi)*dpsi) Rp[2,1] = (-s(theta)*dtheta)*s(psi) Rp[2,1] += c(theta)*(c(psi)*dpsi) Rp[0,2] = (c(phi)*dphi)*s(psi) Rp[0,2] += s(phi)*(c(psi)*dpsi) Rp[0,2] += (-s(phi)*dphi)*s(theta)*c(psi) Rp[0,2] += c(phi)*(c(theta)*dtheta)*c(psi) Rp[0,2] += c(phi)*s(theta)*(-s(psi)*dpsi) Rp[1,2] = (s(phi)*dphi)*s(psi) Rp[1,2] += -c(phi)*(c(psi)*dpsi) Rp[1,2] += (c(phi)*dphi)*s(theta)*c(psi) Rp[1,2] += s(phi)*(c(theta)*dtheta)*c(psi) Rp[1,2] += s(phi)*s(theta)*(-s(psi)*dpsi) Rp[2,2] = (-s(theta)*dtheta)*c(psi) Rp[2,2] += c(theta)*(-s(psi)*dpsi) w_i = vectorize(dot(Rp, transpose(R(phi,theta,psi)))) w_b = dot(transpose(R(phi,theta,psi)), w_i) return (w_i, w_b) def test_W(): # Is the effective w for a rotation of x rad/s about ek just.. ek*x, # regardless of the angle about axis ek? We expect W = -W^T as well. # sanitize_matrix(W(3*pi/12,0,0,2*pi,0,0)[0]) # sanitize_matrix(W(0,3*pi/12,0,0,2*pi,0)[0]) # sanitize_matrix(W(0,0,3*pi/12,0,0,2*pi)[0]) # Let's see what it looks like once we've rotated a bit. # It's still skew antisymmetric with zero trace! This looks like the operation (w x)!!!! phi, theta, psi = (pi/4, 3*pi/12, -pi) w_i, w_b = W(phi, theta, psi, pi, 2*pi, 3*pi) def Main(): test_W() if __name__ == "__main__": Main()
gpl-3.0
Dwii/Master-Thesis
implementation/Palabos/cavity_benchmark/plot_benchmark.py
1
1854
# Display a list of *.dat files in a bar chart. # Based on an example from https://chrisalbon.com/python/matplotlib_grouped_bar_plot.html import sys import os import matplotlib.pyplot as plt import numpy as np if len(sys.argv) > 3 and (len(sys.argv)-3) % 2 : print("usage: python3 {0} <benchmark> <image path> (<dat1> <legend1> [<dat2> <legend2>] .. [<datN> <legendN>] ) ".format(os.path.basename(sys.argv[0]))) exit(1) benchmark = sys.argv[1] image_path = sys.argv[2] groups = (len(sys.argv)-3)/2 # Load benchark domains = () nb_setups = 0 for line in open(benchmark,'r'): n, snx, sny, snz = line.split() domains += ( r"{0}$^3$".format(snx), ) #+= ( "{0}x{1}x{2}".format(snx, sny, snz), ) nb_setups += 1 # Setting the positions and width for the bars pos = list(range(nb_setups)) width = 1 / (groups+2) # Plotting the bars fig, ax = plt.subplots(figsize=(10,5)) prop_iter = iter(plt.rcParams['axes.prop_cycle']) legends = () maxLups = 0 for i, argi in enumerate(range(3, len(sys.argv), 2)): mlups = np.array(list(map(float, open(sys.argv[argi])))) / 1E6 legends += ( sys.argv[argi+1], ) maxLups = max(maxLups, max(mlups)) plt.bar([p + width*i for p in pos], mlups, width, alpha=0.5, color=next(prop_iter)['color']) # Set the y axis label ax.set_ylabel('MLUPS') ax.set_xlabel('Taille du sous-domaine') # Set the chart's title #ax.set_title(title) # Set the position of the x ticks ax.set_xticks([p + 1.5 * width for p in pos]) # Set the labels for the x ticks ax.set_xticklabels(domains) # Setting the x-axis and y-axis limits plt.xlim(min(pos)-width, max(pos)+width*4) #plt.ylim([0, maxLups] ) # Adding the legend and showing the plot plt.legend(legends, loc='upper center') ax.yaxis.grid() plt.savefig(image_path) plt.tight_layout() plt.show()
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
yukisakurai/hhana
mva/plotting/utils.py
1
4190
import ROOT from itertools import izip from matplotlib import cm from rootpy.plotting.style.atlas.labels import ATLAS_label from rootpy.memory.keepalive import keepalive from .. import ATLAS_LABEL def set_colors(hists, colors='jet'): if isinstance(colors, basestring): colors = cm.get_cmap(colors, len(hists)) if hasattr(colors, '__call__'): for i, h in enumerate(hists): color = colors((i + 1) / float(len(hists) + 1)) h.SetColor(color) else: for h, color in izip(hists, colors): h.SetColor(color) def category_lumi_atlas(pad, category_label=None, data_info=None, atlas_label=None, textsize=20): left, right, bottom, top = pad.margin_pixels height = float(pad.height_pixels) # draw the category label if category_label: label = ROOT.TLatex( 1. - pad.GetRightMargin(), 1. - (textsize - 2) / height, category_label) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) label.SetTextAlign(31) with pad: label.Draw() keepalive(pad, label) # draw the luminosity label if data_info is not None: plabel = ROOT.TLatex( 1. - pad.GetLeftMargin() - 0.25, 1. - (top + textsize + 60) / height, str(data_info)) plabel.SetNDC() plabel.SetTextFont(43) plabel.SetTextSize(textsize) plabel.SetTextAlign(31) with pad: plabel.Draw() keepalive(pad, plabel) # draw the ATLAS label if atlas_label is not False: label = atlas_label or ATLAS_LABEL ATLAS_label(pad.GetLeftMargin() + 0.03, 1. - (top + textsize + 15) / height, sep=0.132, pad=pad, sqrts=None, text=label, textsize=textsize) pad.Update() pad.Modified() def label_plot(pad, template, xaxis, yaxis, ylabel='Events', xlabel=None, units=None, data_info=None, category_label=None, atlas_label=None, extra_label=None, extra_label_position='left', textsize=22): # set the axis labels binw = list(template.xwidth()) binwidths = list(set(['%.2g' % w for w in binw])) if units is not None: if xlabel is not None: xlabel = '%s [%s]' % (xlabel, units) if ylabel and len(binwidths) == 1 and binwidths[0] != '1': # constant width bins ylabel = '%s / %s %s' % (ylabel, binwidths[0], units) elif ylabel and len(binwidths) == 1 and binwidths[0] != '1': ylabel = '%s / %s' % (ylabel, binwidths[0]) if ylabel: yaxis.SetTitle(ylabel) if xlabel: xaxis.SetTitle(xlabel) left, right, bottom, top = pad.margin_pixels height = float(pad.height_pixels) category_lumi_atlas(pad, category_label, data_info, atlas_label) # draw the extra label if extra_label is not None: if extra_label_position == 'left': label = ROOT.TLatex(pad.GetLeftMargin() + 0.03, 1. - (top + 2 * (textsize + 40)) / height, extra_label) else: # right label = ROOT.TLatex(1. - pad.GetRightMargin() - 0.03, 1. - (top + 2 * (textsize + 40)) / height, extra_label) label.SetTextAlign(31) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) with pad: label.Draw() keepalive(pad, label) pad.Update() pad.Modified() # class rootpy.plotting.Legend( # entries, pad=None, # leftmargin=0.5, topmargin=0.05, rightmargin=0.05, # entryheight=0.06, entrysep=0.02, margin=0.3, # textfont=None, textsize=None, header=None) def legend_params(position, textsize): return dict( leftmargin=0.48, topmargin=0.03, rightmargin=0.05, entryheight=0.05, entrysep=0.01, margin=0.25, textsize=textsize)
gpl-3.0
Loisel/tmr3
tmr.py
1
15096
#!/usr/bin/python """ A module to calculate the current, the conductance and the TMR from a set of rate arrays. The rate arrays are supposed to be stored in a h5 file in the job directory. The result is stored in a h5 file. The name of the dataset contains all parameters. They are also stored as attributes in the dataset. The conductance in the two lead configurations (parallel/anti-parallel) are stored in arrays in the dataset. Usage: ./tmr.py <jobname> """ import numpy as np from numpy import linalg import time import sys import getopt import h5py import os # We are picky about possible floating point overflows # to avoid calculating NaNs np.seterr(divide="raise") np.seterr(invalid="raise") # A helper module to calculate the populations. import pop # The configuration module import cfg # path to the dat directory datpath = "dat/" # name of the temporary file where the rates are stored ratefile = "running_calc.h5" # name of the h5 file to store the conductance for the two configuration # and the configuraion parameters. hdffile = "simdata_new.h5" def save_hdf5(fname,G_P,G_AP): """ Store the conductance and the configuration to the h5 file. Args: fname: filename of the h5 file G_P: the conductance for leads with parallel magnetization G_AP: the conductance for leads with anti-parallel magnetization """ print "Shape of GP {}".format(G_P.shape) fileh = h5py.File(fname,"a") # Note that the selection of parameters to construct the name of the # dataset should be chosen such that this string is unique! # That is, it should contain all running parameters. dset_name = "G={}_kbT={}_Ec={}_E0={}_Pol={}_PolOrb={}_SO={}_tau={}_DS={}_B_P={}_B_AP={}_B_ORB_P={}_B_ORB_AP={}_W_e={}_W_0={}".format(cfg.conf['G_scale'],cfg.conf['kBT'],cfg.conf['E_C'],cfg.conf['E_0'],cfg.conf['Pol'],cfg.conf['OrbPol'],cfg.conf['SO'],cfg.conf['tau_r'],cfg.conf['D_S_factor'],cfg.conf['B_P'],cfg.conf['B_AP'],cfg.conf['B_ORB_P'],cfg.conf['B_ORB_AP'],cfg.conf['W_E'],cfg.conf['W_0']) try: # we create the dataset dset = fileh.create_dataset(dset_name,data=np.vstack((G_P,G_AP))) # and store the config attributes dset.attrs['alpha'] = cfg.conf['ALPHA'] dset.attrs['temperature'] = cfg.conf['kBT'] dset.attrs['coupling'] = cfg.conf['G_scale'] dset.attrs['electron_number'] = cfg.conf['N_0'] dset.attrs['charging_energy'] = cfg.conf['E_C'] dset.attrs['level_spacing'] = cfg.conf['E_0'] dset.attrs['polarization_spin'] = cfg.conf['Pol'] dset.attrs['polarization_orbit'] = cfg.conf['OrbPol'] dset.attrs['spinorbit'] = cfg.conf['SO'] dset.attrs['stonershift'] = cfg.conf['D_S_factor'] dset.attrs['tau_r'] = cfg.conf['tau_r'] dset.attrs['vg_min'] = cfg.conf['V_g_min'] dset.attrs['vg_max'] = cfg.conf['V_g_max'] dset.attrs['b_p'] = cfg.conf['B_P'] dset.attrs['b_ap'] = cfg.conf['B_AP'] dset.attrs['b_orb_p'] = cfg.conf['B_ORB_P'] dset.attrs['b_orb_ap'] = cfg.conf['B_ORB_AP'] dset.attrs['w_0'] = cfg.conf['W_0'] dset.attrs['w_e'] = cfg.conf['W_E'] dset.attrs['timestamp'] = time.time() except KeyError: # If the choice was not unique we complain but continue. print "Dataset exists." fileh.close() def eval_DENKER(GM,GP,configuration): """ Evaluate the density matrix kernel using the in- and out-tunneling rates. Args: GM,GP: numpy arrays containing in- and out-tunneling rates in the order of cfg.TLIST. configuration: integer determining parallel (0) or anti-parallel(1) configuration Returns: the density matrix as a square 2-d numpy array that is NP**2 in size, where NP is the number of states in the groundstatespace. """ # we get a view on the transition list and, for simplicity, its transpose TLIST = cfg.TLIST[configuration] TLIST_T = np.transpose(TLIST) # from all transitions we extract all groundstates in the statespace # this is probably a complicated way to do it PLIST = list(set(TLIST_T[0]).union(TLIST_T[1])) # ... and sort it by index PLIST.sort() # the number of groundstates NP = len(PLIST) # let's create an empty density matrix ME = np.zeros((NP,NP)) # we create a version of the transition list that does not contain # the indices in terms of the energy array (see cfg.py), but # in terms of the number in the state list (plist) # (the transition list can then be used to denote non-zero matrix elements) TMP = np.copy(TLIST) for idx,val in enumerate(PLIST): TMP[TLIST == val] = idx # We calculate diagonal elements of the density matrix: # TLIST_T[1] == num selects the correct in-tunneling rates for the # state with label num # have a look at numpy.where to understand this line for idx,num in enumerate(PLIST): ME[idx,idx] = -np.sum(np.where(TLIST_T[1] == num,GP,0.)) - np.sum(np.where(TLIST_T[0] == num,GM,0.)) # for the off diagonal elements we can directly use the generated TMP # transition list for k,tup in enumerate(TMP): ME[tup[0],tup[1]] = GP[k] ME[tup[1],tup[0]] = GM[k] # print "tup: {} and matrix element {}".format(tup,ME[tuple(tup)]) return ME def eval_CURKER(GM,GP,configuration): """ Evaluate the current kernel using the in- and out-tunneling rates. Args: GM,GP: numpy arrays containing in- and out-tunneling rates in the order of cfg.TLIST. configuration: integer determining parallel (0) or anti-parallel(1) configuration Returns: the current kernel as a 1-d numpy array. """ # We get a view on the transition list and its transpose TLIST = cfg.TLIST[configuration] TLIST_T = np.transpose(TLIST) # ... and extract the list of groundstates (see also eval_DENKER) PLIST = list(set(TLIST_T[0]).union(TLIST_T[1])) PLIST.sort() # this determines the size of the statespace NP = len(PLIST) CUR = np.zeros(NP) # Note that the current kernel can be calculated by summing the diagonal elements # of the density matrix with opposite sign # compare eval_DENKER for idx,num in enumerate(PLIST): CUR[idx] = np.sum(np.where(TLIST_T[1] == num,GP,0.)) - np.sum(np.where(TLIST_T[0] == num,GM,0.)) return CUR def current(GP,GM,POP,configuration): """ Calculate the current using the rates and populations. Args: GP, GM: np-arrays containing in- and out-tunneling rates. POP: np-array for the populations configuration: integer determining parallel (0) or anti-parallel(1) configuration Returns: current as a float. """ # We calculate the current kernel CURKER = eval_CURKER(GM,GP,configuration) # and vector-multiply it with the population vector I = -np.sum(cfg.conf["ELE"]*np.dot( CURKER, POP)) return I def eval_tmr(fname,plotname,pop): """ Calculates the TMR by evaluating conductance through parallel and anti-parallel polarized contacts. Args: fname: the h5 file to load the rates from. plotname: A name for the pdf output to produce. pop: If True, we plot the populations, too. """ # We prepare the current and conductance vectors for different # values of gate and bias voltage C_p = np.zeros((cfg.conf['NV'],cfg.conf['NVb'])) C_ap = np.zeros((cfg.conf['NV'],cfg.conf['NVb'])) G_p = np.zeros((cfg.conf['NV'],cfg.conf['NVb']-1)) G_ap = np.zeros((cfg.conf['NV'],cfg.conf['NVb']-1)) dVb = cfg.conf['Vb_range'][1]- cfg.conf['Vb_range'][0] # the population vectors, for all values of gate and bias POP_p = np.zeros((cfg.conf['NVb'],cfg.conf['NV'],cfg.N_GS[0])) POP_ap = np.zeros((cfg.conf['NVb'],cfg.conf['NV'],cfg.N_GS[1])) # We iterate over two bias values first for nV,Vb in enumerate(cfg.conf["Vb_range"]): # now the rates are loaded from the h5 file # note that the label of the specific rate arrays are fixed with h5py.File(fname) as file: GP0_p = np.array(file['par_P0_V{}'.format(Vb)]) GP0_ap = np.array(file['apa_P0_V{}'.format(Vb)]) GP1_p = np.array(file['par_P1_V{}'.format(Vb)]) GP1_ap = np.array(file['apa_P1_V{}'.format(Vb)]) GM0_p = np.array(file['par_M0_V{}'.format(Vb)]) GM0_ap = np.array(file['apa_M0_V{}'.format(Vb)]) GM1_p = np.array(file['par_M1_V{}'.format(Vb)]) GM1_ap = np.array(file['apa_M1_V{}'.format(Vb)]) # for the density kernel, we sum all rates over both leads DENKER_p = np.array([eval_DENKER(GM0_p[n]+GM1_p[n],GP0_p[n]+GP1_p[n],0)for n in range(cfg.conf["NV"])]) DENKER_ap = np.array([eval_DENKER(GM0_ap[n]+GM1_ap[n],GP0_ap[n]+GP1_ap[n],1)for n in range(cfg.conf["NV"])]) # the populations are calculated from the density kernel by an asymptotic # approximation scheme POP_ap[nV] = np.array([pop.asymptotic_ssp(DENKER_ap[n]) for n in range(cfg.conf["NV"])]) POP_p[nV] = np.array([pop.asymptotic_ssp(DENKER_p[n]) for n in range(cfg.conf["NV"])]) # note that the current is calculated from the rates in one of the leads only C_p[:,nV] = np.array([ current(GP0_p[n],GM0_p[n],POP_p[nV,n],0) for n in np.arange(cfg.conf["NV"]) ]) C_ap[:,nV] = np.array([ current(GP0_ap[n],GM0_ap[n],POP_ap[nV,n],1) for n in np.arange(cfg.conf["NV"]) ]) # the numerical derivative gives the conductance G_p = np.diff(C_p).flatten()/dVb G_ap = np.diff(C_ap).flatten()/dVb # we save the conductance traces to a h5 file specified as a global variable # hdffile in the path datpath # It is possible that the dataset already exists. In this case, we issue a warning. try: save_hdf5("{}{}".format(datpath,hdffile),G_p,G_ap) except RuntimeError: print "Unable to save to {}, maybe there is already a dataset with similar parameters...".format(hdffile) # the tmr and conductance graphs are plotted to a pdf file for review. plot_tmr_pdf(G_p,G_ap,plotname) # if the pop flag is set, we also plot the population for one bias value if pop: plot_population([POP_p[0],POP_ap[0]],os.path.splitext(plotname)[0]+"_POP.pdf") def plot_tmr_pdf(C_p,C_ap,fname): """ A helper routine to plot the conductance and TMR to a pdf file in the datpath. Args: C_p, C_ap: the parallel and anti-parallel conductance. fname: the filename to plot to """ import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt # we plot the conductance graph on top, p and ap with different colors Axes1 = plt.subplot(2,1,1) Axes1.set_xticklabels([]) plt.ylabel("Conductance (e^2/h)") plt.title("Conductance at zero bias") # parallel is plotted in red, and anti-parallel as blue dashed line plt.plot( cfg.conf["V_g"],C_p,'r',cfg.conf["V_g"], C_ap, 'b--') # on the second panel, the TMR is plotted Axes2 = plt.subplot(2,1,2) plt.xlabel("gate voltage (V)") plt.ylabel("TMR") plt.title("TMR") plt.ylim((-0.3,1.5)) TMR = np.zeros(cfg.conf["NV"]) for i in range(cfg.conf["NV"]): try: TMR[i] = C_p[i]/C_ap[i]-1. except ZeroDivisionError: print "Zero Division, returning null." TMR[i] = 0. plt.plot( cfg.conf["V_g"], TMR) plt.savefig(fname, bbox_inches='tight') def plot_population(POP, fname): """ Calculates and plots selected populations of the quantum dot with gate voltage. The edge states N=-1 and 5 are neglected. Args: POP: a list with the two population vectors for parallel and anti-parallel configurations fname: the filename to plot to """ import matplotlib.pyplot as plt NV = cfg.conf["NV"] print "Calculating populations..." # We plot the populations for both configurations # the parallel populations on top # the anti-parallel on bottom Ax = [plt.subplot(2,1,1),plt.subplot(2,1,2)] cm = plt.get_cmap('gist_rainbow') PopPlots = [1,4,8,12,17,18] NP = len(PopPlots) for gamidx in range(2): TLIST = cfg.TLIST[gamidx] TLIST_T = np.transpose(TLIST) PLIST = list(set(TLIST_T[0]).union(TLIST_T[1])) PLIST.sort() # we cycle through the linecolors to distinguish the different # groundstates Ax[gamidx].set_color_cycle([cm(1.*k/NP) for k in range(NP)]) for i in PopPlots: color = cm(1.*i/NP) LABEL = "P_{}".format(cfg.int_to_state(PLIST[i])) Ax[gamidx].plot( cfg.conf["V_g"], POP[gamidx][:,i],label=LABEL) lines =Ax[gamidx].get_lines() labels = [l.get_label() for l in lines] leg = plt.figlegend(lines,labels,loc='upper right') plt.savefig(fname) plt.show() class Usage(Exception): def __init__(self, msg): self.msg = msg def main(argv=None): """ Interface routine to call the tmr module. Example: ./tmr.py <jobname> In principle, there were routines to plot rates, populations, conductances etc. but apart from the population plotting, none of the use cases was needed anymore. """ POP = False # The default config file is called cnt.conf cfile = "cnt.conf" rlist = [0.,] if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "hc:P", ["help","config=","pop"]) except getopt.error, msg: raise Usage(msg) for o,a in opts: if o in ('-h','--help'): usage() exit() elif o in ('-c','--config'): cfile = a elif o in ('-P','--pop'): POP = True else: raise Usage('Invalid argument.') # we parse the config and initialize it cfg.parse_conf("dat/{0}/{1}".format(args[0],cfile)) cfg.init() h5file = "{}{}/{}".format(datpath,args[0],ratefile) pdffile = "{}{}.pdf".format(datpath,args[0]) print "Try to open {}".format(h5file) eval_tmr(h5file,pdffile,POP) except Usage, err: print >>sys.stderr, err.msg print >>sys.stderr, "for help use --help" return 2 def usage(): print "This is a tool to process rate files.\n\ \n\ usage: tmr.py [-hP] [--pop] jobname\n\ \n\ --pop or -P: Plot the populations.\n\ \n\ jobname: The name of the directory for the rate files.\n\ \n\ The script searches for files dat/jobname/running_calc.h5\n\ and dat/jobname/cnt.conf" if __name__ == "__main__": sys.exit(main())
gpl-3.0
dariox2/CADL
session-5/libs/stylenet.py
4
11350
"""Style Net w/ tests for Video Style Net. Video Style Net requires OpenCV 3.0.0+ w/ Contrib for Python to be installed. Creative Applications of Deep Learning w/ Tensorflow. Kadenze, Inc. Copyright Parag K. Mital, June 2016. """ import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import os from . import vgg16 from . import gif def make_4d(img): """Create a 4-dimensional N x H x W x C image. Parameters ---------- img : np.ndarray Given image as H x W x C or H x W. Returns ------- img : np.ndarray N x H x W x C image. Raises ------ ValueError Unexpected number of dimensions. """ if img.ndim == 2: img = np.expand_dims(img[np.newaxis], 3) elif img.ndim == 3: img = img[np.newaxis] elif img.ndim == 4: return img else: raise ValueError('Incorrect dimensions for image!') return img def stylize(content_img, style_img, base_img=None, saveto=None, gif_step=5, n_iterations=100, style_weight=1.0, content_weight=1.0): """Stylization w/ the given content and style images. Follows the approach in Leon Gatys et al. Parameters ---------- content_img : np.ndarray Image to use for finding the content features. style_img : TYPE Image to use for finding the style features. base_img : None, optional Image to use for the base content. Can be noise or an existing image. If None, the content image will be used. saveto : str, optional Name of GIF image to write to, e.g. "stylization.gif" gif_step : int, optional Modulo of iterations to save the current stylization. n_iterations : int, optional Number of iterations to run for. style_weight : float, optional Weighting on the style features. content_weight : float, optional Weighting on the content features. Returns ------- stylization : np.ndarray Final iteration of the stylization. """ # Preprocess both content and style images content_img = make_4d(content_img) style_img = make_4d(style_img) if base_img is None: base_img = content_img else: base_img = make_4d(base_img) # Get Content and Style features net = vgg16.get_vgg_model() g = tf.Graph() with tf.Session(graph=g) as sess: tf.import_graph_def(net['graph_def'], name='vgg') names = [op.name for op in g.get_operations()] x = g.get_tensor_by_name(names[0] + ':0') content_layer = 'vgg/conv3_2/conv3_2:0' content_features = g.get_tensor_by_name( content_layer).eval(feed_dict={ x: content_img, 'vgg/dropout_1/random_uniform:0': [[1.0]], 'vgg/dropout/random_uniform:0': [[1.0]]}) style_layers = ['vgg/conv1_1/conv1_1:0', 'vgg/conv2_1/conv2_1:0', 'vgg/conv3_1/conv3_1:0', 'vgg/conv4_1/conv4_1:0', 'vgg/conv5_1/conv5_1:0'] style_activations = [] for style_i in style_layers: style_activation_i = g.get_tensor_by_name(style_i).eval( feed_dict={ x: style_img, 'vgg/dropout_1/random_uniform:0': [[1.0]], 'vgg/dropout/random_uniform:0': [[1.0]]}) style_activations.append(style_activation_i) style_features = [] for style_activation_i in style_activations: s_i = np.reshape(style_activation_i, [-1, style_activation_i.shape[-1]]) gram_matrix = np.matmul(s_i.T, s_i) / s_i.size style_features.append(gram_matrix.astype(np.float32)) # Optimize both g = tf.Graph() with tf.Session(graph=g) as sess: net_input = tf.Variable(base_img) tf.import_graph_def( net['graph_def'], name='vgg', input_map={'images:0': net_input}) content_loss = tf.nn.l2_loss((g.get_tensor_by_name(content_layer) - content_features) / content_features.size) style_loss = np.float32(0.0) for style_layer_i, style_gram_i in zip(style_layers, style_features): layer_i = g.get_tensor_by_name(style_layer_i) layer_shape = layer_i.get_shape().as_list() layer_size = layer_shape[1] * layer_shape[2] * layer_shape[3] layer_flat = tf.reshape(layer_i, [-1, layer_shape[3]]) gram_matrix = tf.matmul( tf.transpose(layer_flat), layer_flat) / layer_size style_loss = tf.add( style_loss, tf.nn.l2_loss( (gram_matrix - style_gram_i) / np.float32(style_gram_i.size))) loss = content_weight * content_loss + style_weight * style_loss optimizer = tf.train.AdamOptimizer(0.01).minimize(loss) sess.run(tf.initialize_all_variables()) imgs = [] for it_i in range(n_iterations): _, this_loss, synth = sess.run( [optimizer, loss, net_input], feed_dict={ 'vgg/dropout_1/random_uniform:0': np.ones( g.get_tensor_by_name( 'vgg/dropout_1/random_uniform:0' ).get_shape().as_list()), 'vgg/dropout/random_uniform:0': np.ones( g.get_tensor_by_name( 'vgg/dropout/random_uniform:0' ).get_shape().as_list()) }) print("iteration %d, loss: %f, range: (%f - %f)" % (it_i, this_loss, np.min(synth), np.max(synth)), end='\r') if it_i % gif_step == 0: imgs.append(np.clip(synth[0], 0, 1)) if saveto is not None: gif.build_gif(imgs, saveto=saveto) return np.clip(synth[0], 0, 1) def warp_img(img, dx, dy): """Apply the motion vectors to the given image. Parameters ---------- img : np.ndarray Input image to apply motion to. dx : np.ndarray H x W matrix defining the magnitude of the X vector dy : np.ndarray H x W matrix defining the magnitude of the Y vector Returns ------- img : np.ndarray Image with pixels warped according to dx, dy. """ warped = img.copy() for row_i in range(img.shape[0]): for col_i in range(img.shape[1]): dx_i = int(np.round(dx[row_i, col_i])) dy_i = int(np.round(dy[row_i, col_i])) sample_dx = np.clip(dx_i + col_i, 0, img.shape[0] - 1) sample_dy = np.clip(dy_i + row_i, 0, img.shape[1] - 1) warped[sample_dy, sample_dx, :] = img[row_i, col_i, :] return warped def test_video(style_img='arles.jpg', videodir='kurosawa'): r"""Test for artistic stylization using video. This requires the python installation of OpenCV for the Deep Flow algorithm. If cv2 is not found, then there will be reduced "temporal coherence". Unfortunately, installing opencv for python3 is not the easiest thing to do. OSX users can install this using: $ brew install opencv --with-python3 --with-contrib then will have to symlink the libraries. I think you can do this w/: $ brew link --force opencv3 But the problems start to arise depending on which python you have installed, and it is always a mess w/ homebrew. Sorry! Your best bet is installing from source. Something along these lines should get you there: $ cd ~ $ git clone https://github.com/Itseez/opencv.git $ cd opencv $ git checkout 3.1.0 $ cd ~ $ git clone https://github.com/Itseez/opencv_contrib.git $ cd opencv_contrib $ git checkout 3.1.0 $ cd ~/opencv $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D BUILD_EXAMPLES=OFF .. Parameters ---------- style_img : str, optional Location to style image videodir : str, optional Location to directory containing images of each frame to stylize. Returns ------- imgs : list of np.ndarray Stylized images for each frame. """ has_cv2 = True try: import cv2 has_cv2 = True optflow = cv2.optflow.createOptFlow_DeepFlow() except ImportError: has_cv2 = False style_img = plt.imread(style_img) content_files = [os.path.join(videodir, f) for f in os.listdir(videodir) if f.endswith('.png')] content_img = plt.imread(content_files[0]) from scipy.misc import imresize style_img = imresize(style_img, (448, 448)).astype(np.float32) / 255.0 content_img = imresize(content_img, (448, 448)).astype(np.float32) / 255.0 if has_cv2: prev_lum = cv2.cvtColor(content_img, cv2.COLOR_RGB2HSV)[:, :, 2] else: prev_lum = (content_img[..., 0] * 0.3 + content_img[..., 1] * 0.59 + content_img[..., 2] * 0.11) imgs = [] stylized = stylize(content_img, style_img, content_weight=5.0, style_weight=0.5, n_iterations=50) plt.imsave(fname=content_files[0] + 'stylized.png', arr=stylized) imgs.append(stylized) for f in content_files[1:]: content_img = plt.imread(f) content_img = imresize(content_img, (448, 448)).astype(np.float32) / 255.0 if has_cv2: lum = cv2.cvtColor(content_img, cv2.COLOR_RGB2HSV)[:, :, 2] flow = optflow.calc(prev_lum, lum, None) warped = warp_img(stylized, flow[..., 0], flow[..., 1]) stylized = stylize(content_img, style_img, content_weight=5.0, style_weight=0.5, base_img=warped, n_iterations=50) else: lum = (content_img[..., 0] * 0.3 + content_img[..., 1] * 0.59 + content_img[..., 2] * 0.11) stylized = stylize(content_img, style_img, content_weight=5.0, style_weight=0.5, base_img=None, n_iterations=50) imgs.append(stylized) plt.imsave(fname=f + 'stylized.png', arr=stylized) prev_lum = lum return imgs def test(): """Test for artistic stylization.""" from six.moves import urllib f = ('https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/' + 'Claude_Monet%2C_Impression%2C_soleil_levant.jpg/617px-Claude_Monet' + '%2C_Impression%2C_soleil_levant.jpg?download') filepath, _ = urllib.request.urlretrieve(f, f.split('/')[-1], None) style = plt.imread(filepath) f = ('https://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/' + 'El_jard%C3%ADn_de_las_Delicias%2C_de_El_Bosco.jpg/640px-El_jard' + '%C3%ADn_de_las_Delicias%2C_de_El_Bosco.jpg') filepath, _ = urllib.request.urlretrieve(f, f.split('/')[-1], None) content = plt.imread(filepath) stylize(content, style) if __name__ == '__main__': test_video()
apache-2.0
google/brain-tokyo-workshop
WANNRelease/prettyNEAT/vis/lplot.py
2
2027
""" Laconic plot functions to replace some of the matplotlibs verbosity """ from matplotlib import pyplot as plt import numpy as np import seaborn as sns # -- File I/O ------------------------------------------------------------ -- # def lsave(data,fileName): np.savetxt(fileName, data, delimiter=',',fmt='%1.2e') def lload(fileName): return np.loadtxt(fileName, delimiter=',') # -- Basic Plotting ------------------------------------------------------ -- # def lplot(*args,label=[],axis=False): """Plots an vector, a set of vectors, with or without an x scale """ fig, ax = getAxis(axis) if len(args) == 1: # No xscale x = np.arange(np.shape(args)[1]) y = args[0] if len(args) == 2: # xscale given x = args[0] y = args[1] if np.ndim(y) == 2: for i in range(np.shape(y)[1]): ax.plot(x,y[:,i],'-') if len(label) > 0: ax.legend((label)) else: ax.plot(x,y,'o-') if axis is False: return fig, ax else: return ax def ldist(x, axis=False): """Plots histogram with estimated distribution """ fig, ax = getAxis(axis) if isinstance(x, str): vals = lload(x) else: vals = x sns.distplot(vals.flatten(),ax=ax,bins=10) #sns.distplot(vals.flatten(),ax=ax,hist_kws={"histtype": "step", "linewidth": 3, "alpha": 1, "color": "g"}) return ax def lquart(x,y,label=[],axis=False): """Plots quartiles, x is a vector, y is a matrix with same length as x """ if axis is not False: ax = axis fig = ax.figure.canvas else: fig, ax = plt.subplots() q = np.percentile(y,[25,50,75],axis=1) plt.plot(x,q[1,:],label=label) # median plt.plot(x,q[0,:],'k:',alpha=0.5) plt.plot(x,q[2,:],'k:',alpha=0.5) plt.fill_between(x,q[0,:],q[2,:],alpha=0.25) return ax def getAxis(axis): if axis is not False: ax = axis fig = ax.figure.canvas else: fig, ax = plt.subplots() return fig,ax # -- --------------- -- --------------------------------------------#
apache-2.0
samstern/Greengraph
Greengraph/tests/test_maps.py
1
2937
from ..greengraph import Greengraph from ..map import Map import geopy from nose.tools import assert_equal, assert_almost_equal import numpy.testing as np_test from mock import Mock, patch import requests from matplotlib import image import yaml import os import numpy as np #@patch.object(Greengraph, 'location_sequence') #@patch.object(Map, 'count_green') def test_map_constructor(): mock_image= open(os.path.join(os.path.dirname(__file__),'fixtures','NY_2.png'),'rb') #as mock_imgfile: with patch.object(requests,'get',return_value=Mock(content=mock_image.read())) as mock_get: with patch.object(image,'imread') as mock_img: #default Map(40.7127837, -74.0059413) # New York #Longon Map(51.5073509,-0.1277583) mock_get.assert_called_with( "http://maps.googleapis.com/maps/api/staticmap?", params={ 'sensor':'false', 'zoom':10, 'size':'400x400', 'center':'40.7127837,-74.0059413', 'style':'feature:all|element:labels|visibility:off', 'maptype': 'satellite' } ) #changing parameters Map(41.8781136, -87.6297982,satellite=False,zoom=15,size=(500,350),sensor=True) # New York mock_get.assert_called_with( "http://maps.googleapis.com/maps/api/staticmap?", params={ 'sensor':'true', 'zoom':15, 'size':'500x350', 'center':'41.8781136,-87.6297982', 'style':'feature:all|element:labels|visibility:off', #'maptype': 'satellite' } ) def test_green(): mock_image= open(os.path.join(os.path.dirname(__file__),'fixtures','NY_2.png'),'rb') fixture_green = np.load(os.path.join(os.path.dirname(__file__),'fixtures','ny_green.npy')) threshold = 1.1 with patch('requests.get', return_value=Mock(content=mock_image.read())) as mock_get: testMap = Map(41.8781136, -87.6297982) # New York assert_equal(fixture_green.shape,testMap.green(threshold).shape) assert (testMap.green(threshold) == fixture_green).all() == True assert (testMap.green(1.5) == fixture_green).all() == False def test_count_green(): mock_image= open(os.path.join(os.path.dirname(__file__),'fixtures','NY_2.png'),'rb') fixture_green = np.load(os.path.join(os.path.dirname(__file__),'fixtures','ny_green.npy')) threshold = 1.1 with patch('requests.get', return_value=Mock(content=mock_image.read())) as mock_get: testMap = Map(41.8781136, -87.6297982) # New York count_from_fixture=np.sum(fixture_green) assert (testMap.count_green() == count_from_fixture) assert (testMap.count_green(1.5) != count_from_fixture)
mit
andyh616/mne-python
mne/tests/test_epochs.py
1
71695
# Author: Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr> # Denis Engemann <denis.engemann@gmail.com> # # License: BSD (3-clause) import os.path as op from copy import deepcopy from nose.tools import (assert_true, assert_equal, assert_raises, assert_not_equal) from numpy.testing import (assert_array_equal, assert_array_almost_equal, assert_allclose) import numpy as np import copy as cp import warnings from scipy import fftpack import matplotlib from mne import (io, Epochs, read_events, pick_events, read_epochs, equalize_channels, pick_types, pick_channels, read_evokeds, write_evokeds) from mne.epochs import ( bootstrap, equalize_epoch_counts, combine_event_ids, add_channels_epochs, EpochsArray, concatenate_epochs, _BaseEpochs) from mne.utils import (_TempDir, requires_pandas, slow_test, clean_warning_registry, run_tests_if_main, requires_scipy_version) from mne.io.meas_info import create_info from mne.io.proj import _has_eeg_average_ref_proj from mne.event import merge_events from mne.io.constants import FIFF from mne.externals.six import text_type from mne.externals.six.moves import zip, cPickle as pickle matplotlib.use('Agg') # for testing don't use X server warnings.simplefilter('always') # enable b/c these tests throw warnings base_dir = op.join(op.dirname(__file__), '..', 'io', 'tests', 'data') raw_fname = op.join(base_dir, 'test_raw.fif') event_name = op.join(base_dir, 'test-eve.fif') evoked_nf_name = op.join(base_dir, 'test-nf-ave.fif') event_id, tmin, tmax = 1, -0.2, 0.5 event_id_2 = 2 def _get_data(): raw = io.Raw(raw_fname, add_eeg_ref=False) events = read_events(event_name) picks = pick_types(raw.info, meg=True, eeg=True, stim=True, ecg=True, eog=True, include=['STI 014'], exclude='bads') return raw, events, picks reject = dict(grad=1000e-12, mag=4e-12, eeg=80e-6, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) clean_warning_registry() # really clean warning stack def test_reject(): """Test epochs rejection """ raw, events, picks = _get_data() # cull the list just to contain the relevant event events = events[events[:, 2] == event_id, :] selection = np.arange(3) drop_log = [[]] * 3 + [['MEG 2443']] * 4 assert_raises(TypeError, pick_types, raw) picks_meg = pick_types(raw.info, meg=True, eeg=False) assert_raises(TypeError, Epochs, raw, events, event_id, tmin, tmax, picks=picks, preload=False, reject='foo') assert_raises(ValueError, Epochs, raw, events, event_id, tmin, tmax, picks=picks_meg, preload=False, reject=dict(eeg=1.)) assert_raises(KeyError, Epochs, raw, events, event_id, tmin, tmax, picks=picks, preload=False, reject=dict(foo=1.)) data_7 = dict() keep_idx = [0, 1, 2] for preload in (True, False): for proj in (True, False, 'delayed'): # no rejection epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=preload) assert_raises(ValueError, epochs.drop_bad_epochs, reject='foo') epochs.drop_bad_epochs() assert_equal(len(epochs), len(events)) assert_array_equal(epochs.selection, np.arange(len(events))) assert_array_equal(epochs.drop_log, [[]] * 7) if proj not in data_7: data_7[proj] = epochs.get_data() assert_array_equal(epochs.get_data(), data_7[proj]) # with rejection epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, reject=reject, preload=preload) epochs.drop_bad_epochs() assert_equal(len(epochs), len(events) - 4) assert_array_equal(epochs.selection, selection) assert_array_equal(epochs.drop_log, drop_log) assert_array_equal(epochs.get_data(), data_7[proj][keep_idx]) # rejection post-hoc epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=preload) epochs.drop_bad_epochs() assert_equal(len(epochs), len(events)) assert_array_equal(epochs.get_data(), data_7[proj]) epochs.drop_bad_epochs(reject) assert_equal(len(epochs), len(events) - 4) assert_equal(len(epochs), len(epochs.get_data())) assert_array_equal(epochs.selection, selection) assert_array_equal(epochs.drop_log, drop_log) assert_array_equal(epochs.get_data(), data_7[proj][keep_idx]) # rejection twice reject_part = dict(grad=1100e-12, mag=4e-12, eeg=80e-6, eog=150e-6) epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, reject=reject_part, preload=preload) epochs.drop_bad_epochs() assert_equal(len(epochs), len(events) - 1) epochs.drop_bad_epochs(reject) assert_equal(len(epochs), len(events) - 4) assert_array_equal(epochs.selection, selection) assert_array_equal(epochs.drop_log, drop_log) assert_array_equal(epochs.get_data(), data_7[proj][keep_idx]) # ensure that thresholds must become more stringent, not less assert_raises(ValueError, epochs.drop_bad_epochs, reject_part) assert_equal(len(epochs), len(events) - 4) assert_array_equal(epochs.get_data(), data_7[proj][keep_idx]) epochs.drop_bad_epochs(flat=dict(mag=1.)) assert_equal(len(epochs), 0) assert_raises(ValueError, epochs.drop_bad_epochs, flat=dict(mag=0.)) # rejection of subset of trials (ensure array ownership) reject_part = dict(grad=1100e-12, mag=4e-12, eeg=80e-6, eog=150e-6) epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, reject=None, preload=preload) epochs = epochs[:-1] epochs.drop_bad_epochs(reject=reject) assert_equal(len(epochs), len(events) - 4) assert_array_equal(epochs.get_data(), data_7[proj][keep_idx]) def test_decim(): """Test epochs decimation """ # First with EpochsArray n_epochs, n_channels, n_times = 5, 10, 20 dec_1, dec_2 = 2, 3 decim = dec_1 * dec_2 sfreq = 1000. sfreq_new = sfreq / decim data = np.random.randn(n_epochs, n_channels, n_times) events = np.array([np.arange(n_epochs), [0] * n_epochs, [1] * n_epochs]).T info = create_info(n_channels, sfreq, 'eeg') info['lowpass'] = sfreq_new / float(decim) epochs = EpochsArray(data, info, events) data_epochs = epochs.decimate(decim, copy=True).get_data() data_epochs_2 = epochs.decimate(dec_1).decimate(dec_2).get_data() assert_array_equal(data_epochs, data[:, :, ::decim]) assert_array_equal(data_epochs, data_epochs_2) # Now let's do it with some real data raw, events, picks = _get_data() sfreq_new = raw.info['sfreq'] / decim raw.info['lowpass'] = sfreq_new / 4. # suppress aliasing warnings epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=False) assert_raises(ValueError, epochs.decimate, -1) expected_data = epochs.get_data()[:, :, ::decim] expected_times = epochs.times[::decim] for preload in (True, False): # at init epochs = Epochs(raw, events, event_id, tmin, tmax, decim=decim, preload=preload) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) # split between init and afterward epochs = Epochs(raw, events, event_id, tmin, tmax, decim=dec_1, preload=preload).decimate(dec_2) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) epochs = Epochs(raw, events, event_id, tmin, tmax, decim=dec_2, preload=preload).decimate(dec_1) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) # split between init and afterward, with preload in between epochs = Epochs(raw, events, event_id, tmin, tmax, decim=dec_1, preload=preload) epochs.preload_data() epochs = epochs.decimate(dec_2) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) epochs = Epochs(raw, events, event_id, tmin, tmax, decim=dec_2, preload=preload) epochs.preload_data() epochs = epochs.decimate(dec_1) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) # decimate afterward epochs = Epochs(raw, events, event_id, tmin, tmax, preload=preload).decimate(decim) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) # decimate afterward, with preload in between epochs = Epochs(raw, events, event_id, tmin, tmax, preload=preload) epochs.preload_data() epochs.decimate(decim) assert_allclose(epochs.get_data(), expected_data) assert_allclose(epochs.get_data(), expected_data) assert_equal(epochs.info['sfreq'], sfreq_new) assert_array_equal(epochs.times, expected_times) def test_base_epochs(): """Test base epochs class """ raw = _get_data()[0] epochs = _BaseEpochs(raw.info, None, np.ones((1, 3), int), event_id, tmin, tmax) assert_raises(NotImplementedError, epochs.get_data) # events with non integers assert_raises(ValueError, _BaseEpochs, raw.info, None, np.ones((1, 3), float), event_id, tmin, tmax) assert_raises(ValueError, _BaseEpochs, raw.info, None, np.ones((1, 3, 2), int), event_id, tmin, tmax) @requires_scipy_version('0.14') def test_savgol_filter(): """Test savgol filtering """ h_freq = 10. raw, events = _get_data()[:2] epochs = Epochs(raw, events, event_id, tmin, tmax) assert_raises(RuntimeError, epochs.savgol_filter, 10.) epochs = Epochs(raw, events, event_id, tmin, tmax, preload=True) freqs = fftpack.fftfreq(len(epochs.times), 1. / epochs.info['sfreq']) data = np.abs(fftpack.fft(epochs.get_data())) match_mask = np.logical_and(freqs >= 0, freqs <= h_freq / 2.) mismatch_mask = np.logical_and(freqs >= h_freq * 2, freqs < 50.) epochs.savgol_filter(h_freq) data_filt = np.abs(fftpack.fft(epochs.get_data())) # decent in pass-band assert_allclose(np.mean(data[:, :, match_mask], 0), np.mean(data_filt[:, :, match_mask], 0), rtol=1e-4, atol=1e-2) # suppression in stop-band assert_true(np.mean(data[:, :, mismatch_mask]) > np.mean(data_filt[:, :, mismatch_mask]) * 5) def test_epochs_hash(): """Test epoch hashing """ raw, events = _get_data()[:2] epochs = Epochs(raw, events, event_id, tmin, tmax) assert_raises(RuntimeError, epochs.__hash__) epochs = Epochs(raw, events, event_id, tmin, tmax, preload=True) assert_equal(hash(epochs), hash(epochs)) epochs_2 = Epochs(raw, events, event_id, tmin, tmax, preload=True) assert_equal(hash(epochs), hash(epochs_2)) # do NOT use assert_equal here, failing output is terrible assert_true(pickle.dumps(epochs) == pickle.dumps(epochs_2)) epochs_2._data[0, 0, 0] -= 1 assert_not_equal(hash(epochs), hash(epochs_2)) def test_event_ordering(): """Test event order""" raw, events = _get_data()[:2] events2 = events.copy() np.random.shuffle(events2) for ii, eve in enumerate([events, events2]): with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') Epochs(raw, eve, event_id, tmin, tmax, baseline=(None, 0), reject=reject, flat=flat) assert_equal(len(w), ii) if ii > 0: assert_true('chronologically' in '%s' % w[-1].message) def test_epochs_bad_baseline(): """Test Epochs initialization with bad baseline parameters """ raw, events = _get_data()[:2] assert_raises(ValueError, Epochs, raw, events, None, -0.1, 0.3, (-0.2, 0)) assert_raises(ValueError, Epochs, raw, events, None, -0.1, 0.3, (0, 0.4)) def test_epoch_combine_ids(): """Test combining event ids in epochs compared to events """ raw, events, picks = _get_data() epochs = Epochs(raw, events, {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 32}, tmin, tmax, picks=picks, preload=False) events_new = merge_events(events, [1, 2], 12) epochs_new = combine_event_ids(epochs, ['a', 'b'], {'ab': 12}) assert_equal(epochs_new['ab'].name, 'ab') assert_array_equal(events_new, epochs_new.events) # should probably add test + functionality for non-replacement XXX def test_epoch_multi_ids(): """Test epoch selection via multiple/partial keys """ raw, events, picks = _get_data() epochs = Epochs(raw, events, {'a/b/a': 1, 'a/b/b': 2, 'a/c': 3, 'b/d': 4, 'a_b': 5}, tmin, tmax, picks=picks, preload=False) epochs_regular = epochs[['a', 'b']] epochs_multi = epochs[['a/b/a', 'a/b/b']] assert_array_equal(epochs_regular.events, epochs_multi.events) def test_read_epochs_bad_events(): """Test epochs when events are at the beginning or the end of the file """ raw, events, picks = _get_data() # Event at the beginning epochs = Epochs(raw, np.array([[raw.first_samp, 0, event_id]]), event_id, tmin, tmax, picks=picks, baseline=(None, 0)) with warnings.catch_warnings(record=True): evoked = epochs.average() epochs = Epochs(raw, np.array([[raw.first_samp, 0, event_id]]), event_id, tmin, tmax, picks=picks, baseline=(None, 0)) assert_true(repr(epochs)) # test repr epochs.drop_bad_epochs() assert_true(repr(epochs)) with warnings.catch_warnings(record=True): evoked = epochs.average() # Event at the end epochs = Epochs(raw, np.array([[raw.last_samp, 0, event_id]]), event_id, tmin, tmax, picks=picks, baseline=(None, 0)) with warnings.catch_warnings(record=True): evoked = epochs.average() assert evoked warnings.resetwarnings() @slow_test def test_read_write_epochs(): """Test epochs from raw files with IO as fif file """ raw, events, picks = _get_data() tempdir = _TempDir() temp_fname = op.join(tempdir, 'test-epo.fif') temp_fname_no_bl = op.join(tempdir, 'test_no_bl-epo.fif') baseline = (None, 0) epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=baseline, preload=True) epochs_no_bl = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=None, preload=True) assert_true(epochs_no_bl.baseline is None) evoked = epochs.average() data = epochs.get_data() # Bad tmin/tmax parameters assert_raises(ValueError, Epochs, raw, events, event_id, tmax, tmin, baseline=None) epochs_no_id = Epochs(raw, pick_events(events, include=event_id), None, tmin, tmax, picks=picks, baseline=(None, 0)) assert_array_equal(data, epochs_no_id.get_data()) eog_picks = pick_types(raw.info, meg=False, eeg=False, stim=False, eog=True, exclude='bads') eog_ch_names = [raw.ch_names[k] for k in eog_picks] epochs.drop_channels(eog_ch_names) epochs_no_bl.drop_channels(eog_ch_names) assert_true(len(epochs.info['chs']) == len(epochs.ch_names) == epochs.get_data().shape[1]) data_no_eog = epochs.get_data() assert_true(data.shape[1] == (data_no_eog.shape[1] + len(eog_picks))) # test decim kwarg with warnings.catch_warnings(record=True) as w: # decim with lowpass warnings.simplefilter('always') epochs_dec = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), decim=4) assert_equal(len(w), 1) # decim without lowpass lowpass = raw.info['lowpass'] raw.info['lowpass'] = None epochs_dec = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), decim=4) assert_equal(len(w), 2) raw.info['lowpass'] = lowpass data_dec = epochs_dec.get_data() assert_allclose(data[:, :, epochs_dec._decim_slice], data_dec, rtol=1e-7, atol=1e-12) evoked_dec = epochs_dec.average() assert_allclose(evoked.data[:, epochs_dec._decim_slice], evoked_dec.data, rtol=1e-12) n = evoked.data.shape[1] n_dec = evoked_dec.data.shape[1] n_dec_min = n // 4 assert_true(n_dec_min <= n_dec <= n_dec_min + 1) assert_true(evoked_dec.info['sfreq'] == evoked.info['sfreq'] / 4) # test IO epochs.save(temp_fname) epochs_no_bl.save(temp_fname_no_bl) epochs_read = read_epochs(temp_fname) epochs_no_bl_read = read_epochs(temp_fname_no_bl) assert_raises(ValueError, epochs.apply_baseline, baseline=[1, 2, 3]) epochs_no_bl_read.apply_baseline(baseline) assert_true(epochs_no_bl_read.baseline == baseline) assert_true(str(epochs_read).startswith('<Epochs')) assert_array_equal(epochs_no_bl_read.times, epochs.times) assert_array_almost_equal(epochs_read.get_data(), epochs.get_data()) assert_array_almost_equal(epochs.get_data(), epochs_no_bl_read.get_data()) assert_array_equal(epochs_read.times, epochs.times) assert_array_almost_equal(epochs_read.average().data, evoked.data) assert_equal(epochs_read.proj, epochs.proj) bmin, bmax = epochs.baseline if bmin is None: bmin = epochs.times[0] if bmax is None: bmax = epochs.times[-1] baseline = (bmin, bmax) assert_array_almost_equal(epochs_read.baseline, baseline) assert_array_almost_equal(epochs_read.tmin, epochs.tmin, 2) assert_array_almost_equal(epochs_read.tmax, epochs.tmax, 2) assert_equal(epochs_read.event_id, epochs.event_id) epochs.event_id.pop('1') epochs.event_id.update({'a:a': 1}) # test allow for ':' in key epochs.save(op.join(tempdir, 'foo-epo.fif')) epochs_read2 = read_epochs(op.join(tempdir, 'foo-epo.fif')) assert_equal(epochs_read2.event_id, epochs.event_id) # add reject here so some of the epochs get dropped epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject) epochs.save(temp_fname) # ensure bad events are not saved epochs_read3 = read_epochs(temp_fname) assert_array_equal(epochs_read3.events, epochs.events) data = epochs.get_data() assert_true(epochs_read3.events.shape[0] == data.shape[0]) # test copying loaded one (raw property) epochs_read4 = epochs_read3.copy() assert_array_almost_equal(epochs_read4.get_data(), data) # test equalizing loaded one (drop_log property) epochs_read4.equalize_event_counts(epochs.event_id) epochs.drop_epochs([1, 2], reason='can we recover orig ID?') epochs.save(temp_fname) epochs_read5 = read_epochs(temp_fname) assert_array_equal(epochs_read5.selection, epochs.selection) assert_equal(len(epochs_read5.selection), len(epochs_read5.events)) assert_array_equal(epochs_read5.drop_log, epochs.drop_log) # Test that one can drop channels on read file epochs_read5.drop_channels(epochs_read5.ch_names[:1]) # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') epochs_badname = op.join(tempdir, 'test-bad-name.fif.gz') epochs.save(epochs_badname) read_epochs(epochs_badname) assert_true(len(w) == 2) # test loading epochs with missing events epochs = Epochs(raw, events, dict(foo=1, bar=999), tmin, tmax, picks=picks, on_missing='ignore') epochs.save(temp_fname) epochs_read = read_epochs(temp_fname) assert_allclose(epochs.get_data(), epochs_read.get_data()) assert_array_equal(epochs.events, epochs_read.events) assert_equal(set(epochs.event_id.keys()), set(text_type(x) for x in epochs_read.event_id.keys())) # test saving split epoch files epochs.save(temp_fname, split_size='7MB') epochs_read = read_epochs(temp_fname) assert_allclose(epochs.get_data(), epochs_read.get_data()) assert_array_equal(epochs.events, epochs_read.events) assert_array_equal(epochs.selection, epochs_read.selection) assert_equal(epochs.drop_log, epochs_read.drop_log) # Test that having a single time point works epochs.preload_data() epochs.crop(0, 0, copy=False) assert_equal(len(epochs.times), 1) assert_equal(epochs.get_data().shape[-1], 1) epochs.save(temp_fname) epochs_read = read_epochs(temp_fname) assert_equal(len(epochs_read.times), 1) assert_equal(epochs.get_data().shape[-1], 1) def test_epochs_proj(): """Test handling projection (apply proj in Raw or in Epochs) """ tempdir = _TempDir() raw, events, picks = _get_data() exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more this_picks = pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True, exclude=exclude) epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True) assert_true(all(p['active'] is True for p in epochs.info['projs'])) evoked = epochs.average() assert_true(all(p['active'] is True for p in evoked.info['projs'])) data = epochs.get_data() raw_proj = io.Raw(raw_fname, proj=True) epochs_no_proj = Epochs(raw_proj, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=False) data_no_proj = epochs_no_proj.get_data() assert_true(all(p['active'] is True for p in epochs_no_proj.info['projs'])) evoked_no_proj = epochs_no_proj.average() assert_true(all(p['active'] is True for p in evoked_no_proj.info['projs'])) assert_true(epochs_no_proj.proj is True) # as projs are active from Raw assert_array_almost_equal(data, data_no_proj, decimal=8) # make sure we can exclude avg ref this_picks = pick_types(raw.info, meg=True, eeg=True, stim=True, eog=True, exclude=exclude) epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True, add_eeg_ref=True) assert_true(_has_eeg_average_ref_proj(epochs.info['projs'])) epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True, add_eeg_ref=False) assert_true(not _has_eeg_average_ref_proj(epochs.info['projs'])) # make sure we don't add avg ref when a custom ref has been applied raw.info['custom_ref_applied'] = True epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True) assert_true(not _has_eeg_average_ref_proj(epochs.info['projs'])) # From GH#2200: # This has no problem proj = raw.info['projs'] epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=False) epochs.info['projs'] = [] data = epochs.copy().add_proj(proj).apply_proj().get_data() # save and reload data fname_epo = op.join(tempdir, 'temp-epo.fif') epochs.save(fname_epo) # Save without proj added epochs_read = read_epochs(fname_epo) epochs_read.add_proj(proj) epochs_read.apply_proj() # This used to bomb data_2 = epochs_read.get_data() # Let's check the result assert_allclose(data, data_2, atol=1e-15, rtol=1e-3) def test_evoked_arithmetic(): """Test arithmetic of evoked data """ raw, events, picks = _get_data() epochs1 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) evoked1 = epochs1.average() epochs2 = Epochs(raw, events[4:8], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) evoked2 = epochs2.average() epochs = Epochs(raw, events[:8], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) evoked = epochs.average() evoked_sum = evoked1 + evoked2 assert_array_equal(evoked.data, evoked_sum.data) assert_array_equal(evoked.times, evoked_sum.times) assert_true(evoked_sum.nave == (evoked1.nave + evoked2.nave)) evoked_diff = evoked1 - evoked1 assert_array_equal(np.zeros_like(evoked.data), evoked_diff.data) def test_evoked_io_from_epochs(): """Test IO of evoked data made from epochs """ tempdir = _TempDir() raw, events, picks = _get_data() # offset our tmin so we don't get exactly a zero value when decimating with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') epochs = Epochs(raw, events[:4], event_id, tmin + 0.011, tmax, picks=picks, baseline=(None, 0), decim=5) assert_true(len(w) == 1) evoked = epochs.average() evoked.save(op.join(tempdir, 'evoked-ave.fif')) evoked2 = read_evokeds(op.join(tempdir, 'evoked-ave.fif'))[0] assert_allclose(evoked.data, evoked2.data, rtol=1e-4, atol=1e-20) assert_allclose(evoked.times, evoked2.times, rtol=1e-4, atol=1 / evoked.info['sfreq']) # now let's do one with negative time with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') epochs = Epochs(raw, events[:4], event_id, 0.1, tmax, picks=picks, baseline=(0.1, 0.2), decim=5) evoked = epochs.average() evoked.save(op.join(tempdir, 'evoked-ave.fif')) evoked2 = read_evokeds(op.join(tempdir, 'evoked-ave.fif'))[0] assert_allclose(evoked.data, evoked2.data, rtol=1e-4, atol=1e-20) assert_allclose(evoked.times, evoked2.times, rtol=1e-4, atol=1e-20) # should be equivalent to a cropped original with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') epochs = Epochs(raw, events[:4], event_id, -0.2, tmax, picks=picks, baseline=(0.1, 0.2), decim=5) evoked = epochs.average() evoked.crop(0.099, None) assert_allclose(evoked.data, evoked2.data, rtol=1e-4, atol=1e-20) assert_allclose(evoked.times, evoked2.times, rtol=1e-4, atol=1e-20) def test_evoked_standard_error(): """Test calculation and read/write of standard error """ raw, events, picks = _get_data() tempdir = _TempDir() epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) evoked = [epochs.average(), epochs.standard_error()] write_evokeds(op.join(tempdir, 'evoked-ave.fif'), evoked) evoked2 = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), [0, 1]) evoked3 = [read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 'Unknown'), read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 'Unknown', kind='standard_error')] for evoked_new in [evoked2, evoked3]: assert_true(evoked_new[0]._aspect_kind == FIFF.FIFFV_ASPECT_AVERAGE) assert_true(evoked_new[0].kind == 'average') assert_true(evoked_new[1]._aspect_kind == FIFF.FIFFV_ASPECT_STD_ERR) assert_true(evoked_new[1].kind == 'standard_error') for ave, ave2 in zip(evoked, evoked_new): assert_array_almost_equal(ave.data, ave2.data) assert_array_almost_equal(ave.times, ave2.times) assert_equal(ave.nave, ave2.nave) assert_equal(ave._aspect_kind, ave2._aspect_kind) assert_equal(ave.kind, ave2.kind) assert_equal(ave.last, ave2.last) assert_equal(ave.first, ave2.first) def test_reject_epochs(): """Test of epochs rejection """ raw, events, picks = _get_data() events1 = events[events[:, 2] == event_id] epochs = Epochs(raw, events1, event_id, tmin, tmax, baseline=(None, 0), reject=reject, flat=flat) assert_raises(RuntimeError, len, epochs) n_events = len(epochs.events) data = epochs.get_data() n_clean_epochs = len(data) # Should match # mne_process_raw --raw test_raw.fif --projoff \ # --saveavetag -ave --ave test.ave --filteroff assert_true(n_events > n_clean_epochs) assert_true(n_clean_epochs == 3) assert_true(epochs.drop_log == [[], [], [], ['MEG 2443'], ['MEG 2443'], ['MEG 2443'], ['MEG 2443']]) # Ensure epochs are not dropped based on a bad channel raw_2 = raw.copy() raw_2.info['bads'] = ['MEG 2443'] reject_crazy = dict(grad=1000e-15, mag=4e-15, eeg=80e-9, eog=150e-9) epochs = Epochs(raw_2, events1, event_id, tmin, tmax, baseline=(None, 0), reject=reject_crazy, flat=flat) epochs.drop_bad_epochs() assert_true(all('MEG 2442' in e for e in epochs.drop_log)) assert_true(all('MEG 2443' not in e for e in epochs.drop_log)) # Invalid reject_tmin/reject_tmax/detrend assert_raises(ValueError, Epochs, raw, events1, event_id, tmin, tmax, reject_tmin=1., reject_tmax=0) assert_raises(ValueError, Epochs, raw, events1, event_id, tmin, tmax, reject_tmin=tmin - 1, reject_tmax=1.) assert_raises(ValueError, Epochs, raw, events1, event_id, tmin, tmax, reject_tmin=0., reject_tmax=tmax + 1) epochs = Epochs(raw, events1, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat, reject_tmin=0., reject_tmax=.1) data = epochs.get_data() n_clean_epochs = len(data) assert_true(n_clean_epochs == 7) assert_true(len(epochs) == 7) assert_true(epochs.times[epochs._reject_time][0] >= 0.) assert_true(epochs.times[epochs._reject_time][-1] <= 0.1) # Invalid data for _is_good_epoch function epochs = Epochs(raw, events1, event_id, tmin, tmax, reject=None, flat=None) assert_equal(epochs._is_good_epoch(None), (False, ['NO_DATA'])) assert_equal(epochs._is_good_epoch(np.zeros((1, 1))), (False, ['TOO_SHORT'])) data = epochs[0].get_data()[0] assert_equal(epochs._is_good_epoch(data), (True, None)) def test_preload_epochs(): """Test preload of epochs """ raw, events, picks = _get_data() epochs_preload = Epochs(raw, events[:16], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat) data_preload = epochs_preload.get_data() epochs = Epochs(raw, events[:16], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat) data = epochs.get_data() assert_array_equal(data_preload, data) assert_array_almost_equal(epochs_preload.average().data, epochs.average().data, 18) def test_indexing_slicing(): """Test of indexing and slicing operations """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:20], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat) data_normal = epochs.get_data() n_good_events = data_normal.shape[0] # indices for slicing start_index = 1 end_index = n_good_events - 1 assert((end_index - start_index) > 0) for preload in [True, False]: epochs2 = Epochs(raw, events[:20], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=preload, reject=reject, flat=flat) if not preload: epochs2.drop_bad_epochs() # using slicing epochs2_sliced = epochs2[start_index:end_index] data_epochs2_sliced = epochs2_sliced.get_data() assert_array_equal(data_epochs2_sliced, data_normal[start_index:end_index]) # using indexing pos = 0 for idx in range(start_index, end_index): data = epochs2_sliced[pos].get_data() assert_array_equal(data[0], data_normal[idx]) pos += 1 # using indexing with an int data = epochs2[data_epochs2_sliced.shape[0]].get_data() assert_array_equal(data, data_normal[[idx]]) # using indexing with an array idx = np.random.randint(0, data_epochs2_sliced.shape[0], 10) data = epochs2[idx].get_data() assert_array_equal(data, data_normal[idx]) # using indexing with a list of indices idx = [0] data = epochs2[idx].get_data() assert_array_equal(data, data_normal[idx]) idx = [0, 1] data = epochs2[idx].get_data() assert_array_equal(data, data_normal[idx]) def test_comparision_with_c(): """Test of average obtained vs C code """ raw, events = _get_data()[:2] c_evoked = read_evokeds(evoked_nf_name, condition=0) epochs = Epochs(raw, events, event_id, tmin, tmax, baseline=None, preload=True, reject=None, flat=None) evoked = epochs.average() sel = pick_channels(c_evoked.ch_names, evoked.ch_names) evoked_data = evoked.data c_evoked_data = c_evoked.data[sel] assert_true(evoked.nave == c_evoked.nave) assert_array_almost_equal(evoked_data, c_evoked_data, 10) assert_array_almost_equal(evoked.times, c_evoked.times, 12) def test_crop(): """Test of crop of epochs """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat) assert_raises(RuntimeError, epochs.crop, None, 0.2) # not preloaded data_normal = epochs.get_data() epochs2 = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat) with warnings.catch_warnings(record=True) as w: epochs2.crop(-20, 200) assert_true(len(w) == 2) # indices for slicing tmin_window = tmin + 0.1 tmax_window = tmax - 0.1 tmask = (epochs.times >= tmin_window) & (epochs.times <= tmax_window) assert_true(tmin_window > tmin) assert_true(tmax_window < tmax) epochs3 = epochs2.crop(tmin_window, tmax_window, copy=True) data3 = epochs3.get_data() epochs2.crop(tmin_window, tmax_window) data2 = epochs2.get_data() assert_array_equal(data2, data_normal[:, :, tmask]) assert_array_equal(data3, data_normal[:, :, tmask]) # test time info is correct epochs = EpochsArray(np.zeros((1, 1, 1000)), create_info(1, 1000., 'eeg'), np.ones((1, 3), int), tmin=-0.2) epochs.crop(-.200, .700) last_time = epochs.times[-1] with warnings.catch_warnings(record=True): # not LP filtered epochs.decimate(10) assert_allclose(last_time, epochs.times[-1]) def test_resample(): """Test of resample of epochs """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:10], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat) assert_raises(RuntimeError, epochs.resample, 100) epochs_o = Epochs(raw, events[:10], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat) epochs = epochs_o.copy() data_normal = cp.deepcopy(epochs.get_data()) times_normal = cp.deepcopy(epochs.times) sfreq_normal = epochs.info['sfreq'] # upsample by 2 epochs = epochs_o.copy() epochs.resample(sfreq_normal * 2, npad=0) data_up = cp.deepcopy(epochs.get_data()) times_up = cp.deepcopy(epochs.times) sfreq_up = epochs.info['sfreq'] # downsamply by 2, which should match epochs.resample(sfreq_normal, npad=0) data_new = cp.deepcopy(epochs.get_data()) times_new = cp.deepcopy(epochs.times) sfreq_new = epochs.info['sfreq'] assert_true(data_up.shape[2] == 2 * data_normal.shape[2]) assert_true(sfreq_up == 2 * sfreq_normal) assert_true(sfreq_new == sfreq_normal) assert_true(len(times_up) == 2 * len(times_normal)) assert_array_almost_equal(times_new, times_normal, 10) assert_true(data_up.shape[2] == 2 * data_normal.shape[2]) assert_array_almost_equal(data_new, data_normal, 5) # use parallel epochs = epochs_o.copy() epochs.resample(sfreq_normal * 2, n_jobs=2, npad=0) assert_true(np.allclose(data_up, epochs._data, rtol=1e-8, atol=1e-16)) # test copy flag epochs = epochs_o.copy() epochs_resampled = epochs.resample(sfreq_normal * 2, npad=0, copy=True) assert_true(epochs_resampled is not epochs) epochs_resampled = epochs.resample(sfreq_normal * 2, npad=0, copy=False) assert_true(epochs_resampled is epochs) def test_detrend(): """Test detrending of epochs """ raw, events, picks = _get_data() # test first-order epochs_1 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, detrend=1) epochs_2 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, detrend=None) data_picks = pick_types(epochs_1.info, meg=True, eeg=True, exclude='bads') evoked_1 = epochs_1.average() evoked_2 = epochs_2.average() evoked_2.detrend(1) # Due to roundoff these won't be exactly equal, but they should be close assert_true(np.allclose(evoked_1.data, evoked_2.data, rtol=1e-8, atol=1e-20)) # test zeroth-order case for preload in [True, False]: epochs_1 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, None), preload=preload) epochs_2 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, preload=preload, detrend=0) a = epochs_1.get_data() b = epochs_2.get_data() # All data channels should be almost equal assert_true(np.allclose(a[:, data_picks, :], b[:, data_picks, :], rtol=1e-16, atol=1e-20)) # There are non-M/EEG channels that should not be equal: assert_true(not np.allclose(a, b)) assert_raises(ValueError, Epochs, raw, events[:4], event_id, tmin, tmax, detrend=2) def test_bootstrap(): """Test of bootstrapping of epochs """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat) epochs2 = bootstrap(epochs, random_state=0) assert_true(len(epochs2.events) == len(epochs.events)) assert_true(epochs._data.shape == epochs2._data.shape) def test_epochs_copy(): """Test copy epochs """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat) copied = epochs.copy() assert_array_equal(epochs._data, copied._data) epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat) copied = epochs.copy() data = epochs.get_data() copied_data = copied.get_data() assert_array_equal(data, copied_data) def test_iter_evoked(): """Test the iterator for epochs -> evoked """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) for ii, ev in enumerate(epochs.iter_evoked()): x = ev.data y = epochs.get_data()[ii, :, :] assert_array_equal(x, y) def test_subtract_evoked(): """Test subtraction of Evoked from Epochs """ raw, events, picks = _get_data() epochs = Epochs(raw, events[:10], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) # make sure subraction fails if data channels are missing assert_raises(ValueError, epochs.subtract_evoked, epochs.average(picks[:5])) # do the subraction using the default argument epochs.subtract_evoked() # apply SSP now epochs.apply_proj() # use preloading and SSP from the start epochs2 = Epochs(raw, events[:10], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, proj=True) evoked = epochs2.average() epochs2.subtract_evoked(evoked) # this gives the same result assert_allclose(epochs.get_data(), epochs2.get_data()) # if we compute the evoked response after subtracting it we get zero zero_evoked = epochs.average() data = zero_evoked.data assert_allclose(data, np.zeros_like(data), atol=1e-15) def test_epoch_eq(): """Test epoch count equalization and condition combining """ raw, events, picks = _get_data() # equalizing epochs objects epochs_1 = Epochs(raw, events, event_id, tmin, tmax, picks=picks) epochs_2 = Epochs(raw, events, event_id_2, tmin, tmax, picks=picks) epochs_1.drop_bad_epochs() # make sure drops are logged assert_true(len([l for l in epochs_1.drop_log if not l]) == len(epochs_1.events)) drop_log1 = epochs_1.drop_log = [[] for _ in range(len(epochs_1.events))] drop_log2 = [[] if l == ['EQUALIZED_COUNT'] else l for l in epochs_1.drop_log] assert_true(drop_log1 == drop_log2) assert_true(len([l for l in epochs_1.drop_log if not l]) == len(epochs_1.events)) assert_true(epochs_1.events.shape[0] != epochs_2.events.shape[0]) equalize_epoch_counts([epochs_1, epochs_2], method='mintime') assert_true(epochs_1.events.shape[0] == epochs_2.events.shape[0]) epochs_3 = Epochs(raw, events, event_id, tmin, tmax, picks=picks) epochs_4 = Epochs(raw, events, event_id_2, tmin, tmax, picks=picks) equalize_epoch_counts([epochs_3, epochs_4], method='truncate') assert_true(epochs_1.events.shape[0] == epochs_3.events.shape[0]) assert_true(epochs_3.events.shape[0] == epochs_4.events.shape[0]) # equalizing conditions epochs = Epochs(raw, events, {'a': 1, 'b': 2, 'c': 3, 'd': 4}, tmin, tmax, picks=picks, reject=reject) epochs.drop_bad_epochs() # make sure drops are logged assert_true(len([l for l in epochs.drop_log if not l]) == len(epochs.events)) drop_log1 = deepcopy(epochs.drop_log) old_shapes = [epochs[key].events.shape[0] for key in ['a', 'b', 'c', 'd']] epochs.equalize_event_counts(['a', 'b'], copy=False) # undo the eq logging drop_log2 = [[] if l == ['EQUALIZED_COUNT'] else l for l in epochs.drop_log] assert_true(drop_log1 == drop_log2) assert_true(len([l for l in epochs.drop_log if not l]) == len(epochs.events)) new_shapes = [epochs[key].events.shape[0] for key in ['a', 'b', 'c', 'd']] assert_true(new_shapes[0] == new_shapes[1]) assert_true(new_shapes[2] == new_shapes[2]) assert_true(new_shapes[3] == new_shapes[3]) # now with two conditions collapsed old_shapes = new_shapes epochs.equalize_event_counts([['a', 'b'], 'c'], copy=False) new_shapes = [epochs[key].events.shape[0] for key in ['a', 'b', 'c', 'd']] assert_true(new_shapes[0] + new_shapes[1] == new_shapes[2]) assert_true(new_shapes[3] == old_shapes[3]) assert_raises(KeyError, epochs.equalize_event_counts, [1, 'a']) # now let's combine conditions old_shapes = new_shapes epochs = epochs.equalize_event_counts([['a', 'b'], ['c', 'd']])[0] new_shapes = [epochs[key].events.shape[0] for key in ['a', 'b', 'c', 'd']] assert_true(old_shapes[0] + old_shapes[1] == new_shapes[0] + new_shapes[1]) assert_true(new_shapes[0] + new_shapes[1] == new_shapes[2] + new_shapes[3]) assert_raises(ValueError, combine_event_ids, epochs, ['a', 'b'], {'ab': 1}) combine_event_ids(epochs, ['a', 'b'], {'ab': 12}, copy=False) caught = 0 for key in ['a', 'b']: try: epochs[key] except KeyError: caught += 1 assert_raises(Exception, caught == 2) assert_true(not np.any(epochs.events[:, 2] == 1)) assert_true(not np.any(epochs.events[:, 2] == 2)) epochs = combine_event_ids(epochs, ['c', 'd'], {'cd': 34}) assert_true(np.all(np.logical_or(epochs.events[:, 2] == 12, epochs.events[:, 2] == 34))) assert_true(epochs['ab'].events.shape[0] == old_shapes[0] + old_shapes[1]) assert_true(epochs['ab'].events.shape[0] == epochs['cd'].events.shape[0]) def test_access_by_name(): """Test accessing epochs by event name and on_missing for rare events """ tempdir = _TempDir() raw, events, picks = _get_data() # Test various invalid inputs assert_raises(ValueError, Epochs, raw, events, {1: 42, 2: 42}, tmin, tmax, picks=picks) assert_raises(ValueError, Epochs, raw, events, {'a': 'spam', 2: 'eggs'}, tmin, tmax, picks=picks) assert_raises(ValueError, Epochs, raw, events, {'a': 'spam', 2: 'eggs'}, tmin, tmax, picks=picks) assert_raises(ValueError, Epochs, raw, events, 'foo', tmin, tmax, picks=picks) assert_raises(ValueError, Epochs, raw, events, ['foo'], tmin, tmax, picks=picks) # Test accessing non-existent events (assumes 12345678 does not exist) event_id_illegal = dict(aud_l=1, does_not_exist=12345678) assert_raises(ValueError, Epochs, raw, events, event_id_illegal, tmin, tmax) # Test on_missing assert_raises(ValueError, Epochs, raw, events, 1, tmin, tmax, on_missing='foo') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') Epochs(raw, events, event_id_illegal, tmin, tmax, on_missing='warning') nw = len(w) assert_true(1 <= nw <= 2) Epochs(raw, events, event_id_illegal, tmin, tmax, on_missing='ignore') assert_equal(len(w), nw) # Test constructing epochs with a list of ints as events epochs = Epochs(raw, events, [1, 2], tmin, tmax, picks=picks) for k, v in epochs.event_id.items(): assert_equal(int(k), v) epochs = Epochs(raw, events, {'a': 1, 'b': 2}, tmin, tmax, picks=picks) assert_raises(KeyError, epochs.__getitem__, 'bar') data = epochs['a'].get_data() event_a = events[events[:, 2] == 1] assert_true(len(data) == len(event_a)) epochs = Epochs(raw, events, {'a': 1, 'b': 2}, tmin, tmax, picks=picks, preload=True) assert_raises(KeyError, epochs.__getitem__, 'bar') temp_fname = op.join(tempdir, 'test-epo.fif') epochs.save(temp_fname) epochs2 = read_epochs(temp_fname) for ep in [epochs, epochs2]: data = ep['a'].get_data() event_a = events[events[:, 2] == 1] assert_true(len(data) == len(event_a)) assert_array_equal(epochs2['a'].events, epochs['a'].events) epochs3 = Epochs(raw, events, {'a': 1, 'b': 2, 'c': 3, 'd': 4}, tmin, tmax, picks=picks, preload=True) assert_equal(list(sorted(epochs3[('a', 'b')].event_id.values())), [1, 2]) epochs4 = epochs['a'] epochs5 = epochs3['a'] assert_array_equal(epochs4.events, epochs5.events) # 20 is our tolerance because epochs are written out as floats assert_array_almost_equal(epochs4.get_data(), epochs5.get_data(), 20) epochs6 = epochs3[['a', 'b']] assert_true(all(np.logical_or(epochs6.events[:, 2] == 1, epochs6.events[:, 2] == 2))) assert_array_equal(epochs.events, epochs6.events) assert_array_almost_equal(epochs.get_data(), epochs6.get_data(), 20) # Make sure we preserve names assert_equal(epochs['a'].name, 'a') assert_equal(epochs[['a', 'b']]['a'].name, 'a') @requires_pandas def test_to_data_frame(): """Test epochs Pandas exporter""" raw, events, picks = _get_data() epochs = Epochs(raw, events, {'a': 1, 'b': 2}, tmin, tmax, picks=picks) assert_raises(ValueError, epochs.to_data_frame, index=['foo', 'bar']) assert_raises(ValueError, epochs.to_data_frame, index='qux') assert_raises(ValueError, epochs.to_data_frame, np.arange(400)) df = epochs.to_data_frame(index=['condition', 'epoch', 'time'], picks=list(range(epochs.info['nchan']))) # Default index and picks df2 = epochs.to_data_frame() assert_equal(df.index.names, df2.index.names) assert_array_equal(df.columns.values, epochs.ch_names) data = np.hstack(epochs.get_data()) assert_true((df.columns == epochs.ch_names).all()) assert_array_equal(df.values[:, 0], data[0] * 1e13) assert_array_equal(df.values[:, 2], data[2] * 1e15) for ind in ['time', ['condition', 'time'], ['condition', 'time', 'epoch']]: df = epochs.to_data_frame(index=ind) assert_true(df.index.names == ind if isinstance(ind, list) else [ind]) # test that non-indexed data were present as categorial variables assert_array_equal(sorted(df.reset_index().columns[:3]), sorted(['time', 'condition', 'epoch'])) def test_epochs_proj_mixin(): """Test SSP proj methods from ProjMixin class """ raw, events, picks = _get_data() for proj in [True, False]: epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=proj) assert_true(all(p['active'] == proj for p in epochs.info['projs'])) # test adding / deleting proj if proj: epochs.get_data() assert_true(all(p['active'] == proj for p in epochs.info['projs'])) assert_raises(ValueError, epochs.add_proj, epochs.info['projs'][0], {'remove_existing': True}) assert_raises(ValueError, epochs.add_proj, 'spam') assert_raises(ValueError, epochs.del_proj, 0) else: projs = deepcopy(epochs.info['projs']) n_proj = len(epochs.info['projs']) epochs.del_proj(0) assert_true(len(epochs.info['projs']) == n_proj - 1) epochs.add_proj(projs, remove_existing=False) assert_true(len(epochs.info['projs']) == 2 * n_proj - 1) epochs.add_proj(projs, remove_existing=True) assert_true(len(epochs.info['projs']) == n_proj) # catch no-gos. # wrong proj argument assert_raises(ValueError, Epochs, raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj='crazy') # delayed without reject params assert_raises(RuntimeError, Epochs, raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj='delayed', reject=None) for preload in [True, False]: epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj='delayed', preload=preload, add_eeg_ref=True, reject=reject) epochs2 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=True, preload=preload, add_eeg_ref=True, reject=reject) assert_allclose(epochs.copy().apply_proj().get_data()[0], epochs2.get_data()[0], rtol=1e-10, atol=1e-25) # make sure data output is constant across repeated calls # e.g. drop bads assert_array_equal(epochs.get_data(), epochs.get_data()) assert_array_equal(epochs2.get_data(), epochs2.get_data()) # test epochs.next calls data = epochs.get_data().copy() data2 = np.array([e for e in epochs]) assert_array_equal(data, data2) # cross application from processing stream 1 to 2 epochs.apply_proj() assert_array_equal(epochs._projector, epochs2._projector) assert_allclose(epochs._data, epochs2.get_data()) # test mixin against manual application epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, proj=False, add_eeg_ref=True) data = epochs.get_data().copy() epochs.apply_proj() assert_allclose(np.dot(epochs._projector, data[0]), epochs._data[0]) def test_delayed_epochs(): """Test delayed projection """ raw, events, picks = _get_data() events = events[:10] picks = np.concatenate([pick_types(raw.info, meg=True, eeg=True)[::22], pick_types(raw.info, meg=False, eeg=False, ecg=True, eog=True)]) picks = np.sort(picks) raw.info['lowpass'] = 40. # fake the LP info so no warnings for preload in (True, False): for proj in (True, False, 'delayed'): for decim in (1, 3): for ii in range(2): epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, proj=proj, reject=reject, preload=preload, decim=decim) if ii == 1: epochs.preload_data() picks_data = pick_types(epochs.info, meg=True, eeg=True) evoked = epochs.average(picks=picks_data) if proj is True: evoked.apply_proj() epochs_data = epochs.get_data().mean(axis=0)[picks_data] assert_array_equal(evoked.ch_names, np.array(epochs.ch_names)[picks_data]) assert_allclose(evoked.times, epochs.times) assert_allclose(evoked.data, epochs_data, rtol=1e-5, atol=1e-15) def test_drop_epochs(): """Test dropping of epochs. """ raw, events, picks = _get_data() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0)) events1 = events[events[:, 2] == event_id] # Bound checks assert_raises(IndexError, epochs.drop_epochs, [len(epochs.events)]) assert_raises(IndexError, epochs.drop_epochs, [-1]) assert_raises(ValueError, epochs.drop_epochs, [[1, 2], [3, 4]]) # Test selection attribute assert_array_equal(epochs.selection, np.where(events[:, 2] == event_id)[0]) assert_equal(len(epochs.drop_log), len(events)) assert_true(all(epochs.drop_log[k] == ['IGNORED'] for k in set(range(len(events))) - set(epochs.selection))) selection = epochs.selection.copy() n_events = len(epochs.events) epochs.drop_epochs([2, 4], reason='d') assert_equal(epochs.drop_log_stats(), 2. / n_events * 100) assert_equal(len(epochs.drop_log), len(events)) assert_equal([epochs.drop_log[k] for k in selection[[2, 4]]], [['d'], ['d']]) assert_array_equal(events[epochs.selection], events1[[0, 1, 3, 5, 6]]) assert_array_equal(events[epochs[3:].selection], events1[[5, 6]]) assert_array_equal(events[epochs['1'].selection], events1[[0, 1, 3, 5, 6]]) def test_drop_epochs_mult(): """Test that subselecting epochs or making less epochs is equivalent""" raw, events, picks = _get_data() for preload in [True, False]: epochs1 = Epochs(raw, events, {'a': 1, 'b': 2}, tmin, tmax, picks=picks, reject=reject, preload=preload)['a'] epochs2 = Epochs(raw, events, {'a': 1}, tmin, tmax, picks=picks, reject=reject, preload=preload) if preload: # In the preload case you cannot know the bads if already ignored assert_equal(len(epochs1.drop_log), len(epochs2.drop_log)) for d1, d2 in zip(epochs1.drop_log, epochs2.drop_log): if d1 == ['IGNORED']: assert_true(d2 == ['IGNORED']) if d1 != ['IGNORED'] and d1 != []: assert_true((d2 == d1) or (d2 == ['IGNORED'])) if d1 == []: assert_true(d2 == []) assert_array_equal(epochs1.events, epochs2.events) assert_array_equal(epochs1.selection, epochs2.selection) else: # In the non preload is should be exactly the same assert_equal(epochs1.drop_log, epochs2.drop_log) assert_array_equal(epochs1.events, epochs2.events) assert_array_equal(epochs1.selection, epochs2.selection) def test_contains(): """Test membership API""" raw, events = _get_data()[:2] tests = [(('mag', False), ('grad', 'eeg')), (('grad', False), ('mag', 'eeg')), ((False, True), ('grad', 'mag'))] for (meg, eeg), others in tests: picks_contains = pick_types(raw.info, meg=meg, eeg=eeg) epochs = Epochs(raw, events, {'a': 1, 'b': 2}, tmin, tmax, picks=picks_contains, reject=None, preload=False) test = 'eeg' if eeg is True else meg assert_true(test in epochs) assert_true(not any(o in epochs for o in others)) assert_raises(ValueError, epochs.__contains__, 'foo') assert_raises(ValueError, epochs.__contains__, 1) def test_drop_channels_mixin(): """Test channels-dropping functionality """ raw, events = _get_data()[:2] # here without picks to get additional coverage epochs = Epochs(raw, events, event_id, tmin, tmax, picks=None, baseline=(None, 0), preload=True) drop_ch = epochs.ch_names[:3] ch_names = epochs.ch_names[3:] ch_names_orig = epochs.ch_names dummy = epochs.drop_channels(drop_ch, copy=True) assert_equal(ch_names, dummy.ch_names) assert_equal(ch_names_orig, epochs.ch_names) assert_equal(len(ch_names_orig), epochs.get_data().shape[1]) epochs.drop_channels(drop_ch) assert_equal(ch_names, epochs.ch_names) assert_equal(len(ch_names), epochs.get_data().shape[1]) def test_pick_channels_mixin(): """Test channel-picking functionality """ raw, events, picks = _get_data() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) ch_names = epochs.ch_names[:3] epochs.preload = False assert_raises(RuntimeError, epochs.drop_channels, ['foo']) epochs.preload = True ch_names_orig = epochs.ch_names dummy = epochs.pick_channels(ch_names, copy=True) assert_equal(ch_names, dummy.ch_names) assert_equal(ch_names_orig, epochs.ch_names) assert_equal(len(ch_names_orig), epochs.get_data().shape[1]) epochs.pick_channels(ch_names) assert_equal(ch_names, epochs.ch_names) assert_equal(len(ch_names), epochs.get_data().shape[1]) # Invalid picks assert_raises(ValueError, Epochs, raw, events, event_id, tmin, tmax, picks=[]) def test_equalize_channels(): """Test equalization of channels """ raw, events, picks = _get_data() epochs1 = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=False, preload=True) epochs2 = epochs1.copy() ch_names = epochs1.ch_names[2:] epochs1.drop_channels(epochs1.ch_names[:1]) epochs2.drop_channels(epochs2.ch_names[1:2]) my_comparison = [epochs1, epochs2] equalize_channels(my_comparison) for e in my_comparison: assert_equal(ch_names, e.ch_names) def test_illegal_event_id(): """Test handling of invalid events ids""" raw, events, picks = _get_data() event_id_illegal = dict(aud_l=1, does_not_exist=12345678) assert_raises(ValueError, Epochs, raw, events, event_id_illegal, tmin, tmax, picks=picks, baseline=(None, 0), proj=False) def test_add_channels_epochs(): """Test adding channels""" raw, events, picks = _get_data() def make_epochs(picks, proj): return Epochs(raw, events, event_id, tmin, tmax, baseline=(None, 0), reject=None, preload=True, proj=proj, picks=picks) picks = pick_types(raw.info, meg=True, eeg=True, exclude='bads') picks_meg = pick_types(raw.info, meg=True, eeg=False, exclude='bads') picks_eeg = pick_types(raw.info, meg=False, eeg=True, exclude='bads') for proj in (False, True): epochs = make_epochs(picks=picks, proj=proj) epochs_meg = make_epochs(picks=picks_meg, proj=proj) epochs_eeg = make_epochs(picks=picks_eeg, proj=proj) epochs.info._check_consistency() epochs_meg.info._check_consistency() epochs_eeg.info._check_consistency() epochs2 = add_channels_epochs([epochs_meg, epochs_eeg]) assert_equal(len(epochs.info['projs']), len(epochs2.info['projs'])) assert_equal(len(epochs.info.keys()), len(epochs2.info.keys())) data1 = epochs.get_data() data2 = epochs2.get_data() data3 = np.concatenate([e.get_data() for e in [epochs_meg, epochs_eeg]], axis=1) assert_array_equal(data1.shape, data2.shape) assert_allclose(data1, data3, atol=1e-25) assert_allclose(data1, data2, atol=1e-25) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['meas_date'] += 10 add_channels_epochs([epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs2.info['filename'] = epochs2.info['filename'].upper() epochs2 = add_channels_epochs([epochs_meg, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.events[3, 2] -= 1 assert_raises(ValueError, add_channels_epochs, [epochs_meg2, epochs_eeg]) assert_raises(ValueError, add_channels_epochs, [epochs_meg, epochs_eeg[:2]]) epochs_meg.info['chs'].pop(0) epochs_meg.info['ch_names'].pop(0) epochs_meg.info['nchan'] -= 1 assert_raises(RuntimeError, add_channels_epochs, [epochs_meg, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['sfreq'] = None assert_raises(RuntimeError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['sfreq'] += 10 assert_raises(RuntimeError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['ch_names'][1] = epochs_meg2.info['ch_names'][0] epochs_meg2.info['chs'][1]['ch_name'] = epochs_meg2.info['ch_names'][1] assert_raises(ValueError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['dev_head_t']['to'] += 1 assert_raises(ValueError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['dev_head_t']['to'] += 1 assert_raises(ValueError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.info['expimenter'] = 'foo' assert_raises(RuntimeError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.preload = False assert_raises(ValueError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.tmin += 0.4 assert_raises(NotImplementedError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.tmin += 0.5 assert_raises(NotImplementedError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.baseline = None assert_raises(NotImplementedError, add_channels_epochs, [epochs_meg2, epochs_eeg]) epochs_meg2 = epochs_meg.copy() epochs_meg2.event_id['b'] = 2 assert_raises(NotImplementedError, add_channels_epochs, [epochs_meg2, epochs_eeg]) def test_array_epochs(): """Test creating epochs from array """ import matplotlib.pyplot as plt tempdir = _TempDir() # creating rng = np.random.RandomState(42) data = rng.random_sample((10, 20, 300)) sfreq = 1e3 ch_names = ['EEG %03d' % (i + 1) for i in range(20)] types = ['eeg'] * 20 info = create_info(ch_names, sfreq, types) events = np.c_[np.arange(1, 600, 60), np.zeros(10, int), [1, 2] * 5] event_id = {'a': 1, 'b': 2} epochs = EpochsArray(data, info, events, tmin, event_id) assert_true(str(epochs).startswith('<EpochsArray')) # From GH#1963 assert_raises(ValueError, EpochsArray, data[:-1], info, events, tmin, event_id) assert_raises(ValueError, EpochsArray, data, info, events, tmin, dict(a=1)) # saving temp_fname = op.join(tempdir, 'test-epo.fif') epochs.save(temp_fname) epochs2 = read_epochs(temp_fname) data2 = epochs2.get_data() assert_allclose(data, data2) assert_allclose(epochs.times, epochs2.times) assert_equal(epochs.event_id, epochs2.event_id) assert_array_equal(epochs.events, epochs2.events) # plotting epochs[0].plot() plt.close('all') # indexing assert_array_equal(np.unique(epochs['a'].events[:, 2]), np.array([1])) assert_equal(len(epochs[:2]), 2) data[0, 5, 150] = 3000 data[1, :, :] = 0 data[2, 5, 210] = 3000 data[3, 5, 260] = 0 epochs = EpochsArray(data, info, events=events, event_id=event_id, tmin=0, reject=dict(eeg=1000), flat=dict(eeg=1e-1), reject_tmin=0.1, reject_tmax=0.2) assert_equal(len(epochs), len(events) - 2) assert_equal(epochs.drop_log[0], ['EEG 006']) assert_equal(len(epochs.drop_log), 10) assert_equal(len(epochs.events), len(epochs.selection)) # baseline data = np.ones((10, 20, 300)) epochs = EpochsArray(data, info, events=events, event_id=event_id, tmin=-.2, baseline=(None, 0)) ep_data = epochs.get_data() assert_array_equal(np.zeros_like(ep_data), ep_data) # one time point epochs = EpochsArray(data[:, :, :1], info, events=events, event_id=event_id, tmin=0., baseline=None) assert_allclose(epochs.times, [0.]) assert_allclose(epochs.get_data(), data[:, :, :1]) epochs.save(temp_fname) epochs_read = read_epochs(temp_fname) assert_allclose(epochs_read.times, [0.]) assert_allclose(epochs_read.get_data(), data[:, :, :1]) # event as integer (#2435) mask = (events[:, 2] == 1) data_1 = data[mask] events_1 = events[mask] epochs = EpochsArray(data_1, info, events=events_1, event_id=1, tmin=-0.2, baseline=(None, 0)) def test_concatenate_epochs(): """Test concatenate epochs""" raw, events, picks = _get_data() epochs = Epochs( raw=raw, events=events, event_id=event_id, tmin=tmin, tmax=tmax, picks=picks) epochs2 = epochs.copy() epochs_list = [epochs, epochs2] epochs_conc = concatenate_epochs(epochs_list) assert_array_equal( epochs_conc.events[:, 0], np.unique(epochs_conc.events[:, 0])) expected_shape = list(epochs.get_data().shape) expected_shape[0] *= 2 expected_shape = tuple(expected_shape) assert_equal(epochs_conc.get_data().shape, expected_shape) assert_equal(epochs_conc.drop_log, epochs.drop_log * 2) epochs2 = epochs.copy() epochs2._data = epochs2.get_data() epochs2.preload = True assert_raises( ValueError, concatenate_epochs, [epochs, epochs2.drop_channels(epochs2.ch_names[:1], copy=True)]) epochs2.times = np.delete(epochs2.times, 1) assert_raises( ValueError, concatenate_epochs, [epochs, epochs2]) assert_equal(epochs_conc._raw, None) # check if baseline is same for all epochs epochs2.baseline = (-0.1, None) assert_raises(ValueError, concatenate_epochs, [epochs, epochs2]) def test_add_channels(): """Test epoch splitting / re-appending channel types """ raw, events, picks = _get_data() epoch_nopre = Epochs( raw=raw, events=events, event_id=event_id, tmin=tmin, tmax=tmax, picks=picks) epoch = Epochs( raw=raw, events=events, event_id=event_id, tmin=tmin, tmax=tmax, picks=picks, preload=True) epoch_eeg = epoch.pick_types(meg=False, eeg=True, copy=True) epoch_meg = epoch.pick_types(meg=True, copy=True) epoch_stim = epoch.pick_types(meg=False, stim=True, copy=True) epoch_eeg_meg = epoch.pick_types(meg=True, eeg=True, copy=True) epoch_new = epoch_meg.add_channels([epoch_eeg, epoch_stim], copy=True) assert_true(all(ch in epoch_new.ch_names for ch in epoch_stim.ch_names + epoch_meg.ch_names)) epoch_new = epoch_meg.add_channels([epoch_eeg], copy=True) assert_true(ch in epoch_new.ch_names for ch in epoch.ch_names) assert_array_equal(epoch_new._data, epoch_eeg_meg._data) assert_true(all(ch not in epoch_new.ch_names for ch in epoch_stim.ch_names)) # Now test errors epoch_badsf = epoch_eeg.copy() epoch_badsf.info['sfreq'] = 3.1415927 epoch_eeg = epoch_eeg.crop(-.1, .1) assert_raises(AssertionError, epoch_meg.add_channels, [epoch_nopre]) assert_raises(RuntimeError, epoch_meg.add_channels, [epoch_badsf]) assert_raises(AssertionError, epoch_meg.add_channels, [epoch_eeg]) assert_raises(ValueError, epoch_meg.add_channels, [epoch_meg]) assert_raises(AssertionError, epoch_meg.add_channels, epoch_badsf) run_tests_if_main()
bsd-3-clause
yl565/statsmodels
statsmodels/examples/ex_scatter_ellipse.py
39
1367
'''example for grid of scatter plots with probability ellipses Author: Josef Perktold License: BSD-3 ''' from statsmodels.compat.python import lrange import numpy as np import matplotlib.pyplot as plt from statsmodels.graphics.plot_grids import scatter_ellipse nvars = 6 mmean = np.arange(1.,nvars+1)/nvars * 1.5 rho = 0.5 #dcorr = rho*np.ones((nvars, nvars)) + (1-rho)*np.eye(nvars) r = np.random.uniform(-0.99, 0.99, size=(nvars, nvars)) ##from scipy import stats ##r = stats.rdist.rvs(1, size=(nvars, nvars)) r = (r + r.T) / 2. assert np.allclose(r, r.T) mcorr = r mcorr[lrange(nvars), lrange(nvars)] = 1 #dcorr = np.array([[1, 0.5, 0.1],[0.5, 1, -0.2], [0.1, -0.2, 1]]) mstd = np.arange(1.,nvars+1)/nvars mcov = mcorr * np.outer(mstd, mstd) evals = np.linalg.eigvalsh(mcov) assert evals.min > 0 #assert positive definite nobs = 100 data = np.random.multivariate_normal(mmean, mcov, size=nobs) dmean = data.mean(0) dcov = np.cov(data, rowvar=0) print(dmean) print(dcov) dcorr = np.corrcoef(data, rowvar=0) dcorr[np.triu_indices(nvars)] = 0 print(dcorr) #default #fig = scatter_ellipse(data, level=[0.5, 0.75, 0.95]) #used for checking #fig = scatter_ellipse(data, level=[0.5, 0.75, 0.95], add_titles=True, keep_ticks=True) #check varnames varnames = ['var%d' % i for i in range(nvars)] fig = scatter_ellipse(data, level=0.9, varnames=varnames) plt.show()
bsd-3-clause
harshaneelhg/scikit-learn
examples/linear_model/plot_lasso_lars.py
363
1080
#!/usr/bin/env python """ ===================== Lasso path using LARS ===================== Computes Lasso Path along the regularization parameter using the LARS algorithm on the diabetes dataset. Each color represents a different feature of the coefficient vector, and this is displayed as a function of the regularization parameter. """ print(__doc__) # Author: Fabian Pedregosa <fabian.pedregosa@inria.fr> # Alexandre Gramfort <alexandre.gramfort@inria.fr> # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model from sklearn import datasets diabetes = datasets.load_diabetes() X = diabetes.data y = diabetes.target print("Computing regularization path using the LARS ...") alphas, _, coefs = linear_model.lars_path(X, y, method='lasso', verbose=True) xx = np.sum(np.abs(coefs.T), axis=1) xx /= xx[-1] plt.plot(xx, coefs.T) ymin, ymax = plt.ylim() plt.vlines(xx, ymin, ymax, linestyle='dashed') plt.xlabel('|coef| / max|coef|') plt.ylabel('Coefficients') plt.title('LASSO Path') plt.axis('tight') plt.show()
bsd-3-clause
bestwpw/BDA_py_demos
demos_ch5/demo5_2.py
19
3326
"""Bayesian Data Analysis, 3rd ed Chapter 5, demo 2 Hierarchical model for SAT-example data (BDA3, p. 102) """ from __future__ import division import numpy as np from scipy.stats import norm import scipy.io # For importing a matlab file import matplotlib.pyplot as plt # Edit default plot settings (colours from colorbrewer2.org) plt.rc('font', size=14) plt.rc('lines', color='#377eb8', linewidth=2) plt.rc('axes', color_cycle=(plt.rcParams['lines.color'],)) # Disable color cycle # SAT-example data (BDA3 p. 120) # y is the estimated treatment effect # s is the standard error of effect estimate y = np.array([28, 8, -3, 7, -1, 1, 18, 12]) s = np.array([15, 10, 16, 11, 9, 11, 10, 18]) M = len(y) # load the pre-computed results for the hierarchical model # replace this with your own code in Ex 5.1* hres_path = '../utilities_and_data/demo5_2.mat' hres = scipy.io.loadmat(hres_path) ''' Content information of the precalculated results: >>> scipy.io.whosmat('demo5_2.mat') [('pxm', (8, 500), 'double'), ('t', (1, 1000), 'double'), ('tp', (1, 1000), 'double'), ('tsd', (8, 1000), 'double'), ('tm', (8, 1000), 'double')] ''' pxm = hres['pxm'] t = hres['t'][0] tp = hres['tp'][0] tsd = hres['tsd'] tm = hres['tm'] # plot the separate, pooled and hierarchical models fig, axes = plt.subplots(3, 1, sharex=True, figsize=(8,10)) x = np.linspace(-40, 60, 500) # separate lines = axes[0].plot(x, norm.pdf(x[:,None], y[1:], s[1:]), linewidth=1) line, = axes[0].plot(x, norm.pdf(x, y[0], s[0]), 'r') axes[0].legend((line, lines[1]), ('school A', 'other schools'), loc='upper left') axes[0].set_yticks(()) axes[0].set_title('separate model') # pooled axes[1].plot( x, norm.pdf( x, np.sum(y/s**2)/np.sum(1/s**2), np.sqrt(1/np.sum(1/s**2)) ), label='All schools' ) axes[1].legend(loc='upper left') axes[1].set_yticks(()) axes[1].set_title('pooled model') # hierarchical lines = axes[2].plot(x, pxm[1:].T, linewidth=1) line, = axes[2].plot(x, pxm[0], 'r') axes[2].legend((line, lines[1]), ('school A', 'other schools'), loc='upper left') axes[2].set_yticks(()) axes[2].set_title('hierarchical model') axes[2].set_xlabel('Treatment effect') # plot various marginal and conditional posterior summaries fig, axes = plt.subplots(3, 1, sharex=True, figsize=(8,10)) axes[0].plot(t, tp) axes[0].set_yticks(()) axes[0].set_title(r'marginal posterior density $p(\tau|y)$') axes[0].set_ylabel(r'$p(\tau|y)$', fontsize=20) axes[0].set_xlim([0,35]) lines = axes[1].plot(t, tm[1:].T, linewidth=1) line, = axes[1].plot(t, tm[0].T, 'r') axes[1].legend((line, lines[1]), ('school A', 'other schools'), loc='upper left') axes[1].set_title(r'conditional posterior means of effects ' r'$\operatorname{E}(\theta_j|\tau,y)$') axes[1].set_ylabel(r'$\operatorname{E}(\theta_j|\tau,y)$', fontsize=20) lines = axes[2].plot(t, tsd[1:].T, linewidth=1) line, = axes[2].plot(t, tsd[0].T, 'r') axes[2].legend((line, lines[1]), ('school A', 'other schools'), loc='upper left') axes[2].set_title(r'standard deviations of effects ' r'$\operatorname{sd}(\theta_j|\tau,y)$') axes[2].set_ylabel(r'$\operatorname{sd}(\theta_j|\tau,y)$', fontsize=20) axes[2].set_xlabel(r'$\tau$', fontsize=20) plt.show()
gpl-3.0
CoolProp/CoolProp
dev/scripts/viscosity_builder.py
2
3895
from math import sqrt, exp from CoolProp.CoolProp import Props import numpy as np import matplotlib.pyplot as plt from scipy.odr import * from math import log E_K = {'REFPROP-Ammonia': 386, 'REFPROP-Argon': 143.2 } SIGMA = {'REFPROP-Ammonia': 0.2957, 'REFPROP-Argon': 0.335 } E_K['REFPROP-Propane'] = 263.88 SIGMA['REFPROP-Propane'] = 0.49748 E_K['REFPROP-R32'] = 289.65 SIGMA['REFPROP-R32'] = 0.4098 E_K['REFPROP-R245fa'] = 329.72 SIGMA['REFPROP-R245fa'] = 0.5529 def viscosity_dilute(fluid, T, e_k, sigma): """ T in [K], e_k in [K], sigma in [nm] viscosity returned is in [Pa-s] """ Tstar = T / e_k molemass = Props(fluid, 'molemass') if fluid == 'Propane' or fluid == 'REFPROP-Propane': a = [0.25104574, -0.47271238, 0, 0.060836515, 0] theta_star = exp(a[0] * pow(log(Tstar), 0) + a[1] * pow(log(Tstar), 1) + a[3] * pow(log(Tstar), 3)); eta_star = 0.021357 * sqrt(molemass * T) / (pow(sigma, 2) * theta_star) / 1e6; return eta_star # From Neufeld, 1972, Journal of Chemical Physics - checked coefficients OMEGA_2_2 = 1.16145 * pow(Tstar, -0.14874) + 0.52487 * exp(-0.77320 * Tstar) + 2.16178 * exp(-2.43787 * Tstar) # Using the leading constant from McLinden, 2000 since the leading term from Huber 2003 gives crazy values eta_star = 26.692e-3 * sqrt(molemass * T) / (pow(sigma, 2) * OMEGA_2_2) / 1e6 return eta_star def viscosity_linear(fluid, T, rho, e_k, sigma): """ Implements the method of Vogel 1998 (Propane) for the linear part """ N_A = 6.02214129e23 molemass = Props(fluid, 'molemass') Tstar = T / e_k b = [-19.572881, 219.73999, -1015.3226, 2471.01251, -3375.1717, 2491.6597, -787.26086, 14.085455, -0.34664158] s = sum([b[i] * pow(Tstar, -0.25 * i) for i in range(7)]) B_eta_star = s + b[7] * pow(Tstar, -2.5) + b[8] * pow(Tstar, -5.5) # //[no units] B_eta = N_A * pow(sigma / 1e9, 3) * B_eta_star # [m3/mol] return viscosity_dilute(fluid, T, e_k, sigma) * B_eta * rho / molemass * 1000 from PDSim.misc.datatypes import Collector RHO = Collector() TT = Collector() DELTA = Collector() TAU = Collector() VV = Collector() VV0 = Collector() VV1 = Collector() VVH = Collector() fluid = 'REFPROP-R32' Tc = Props(fluid, 'Tcrit') rhoc = Props(fluid, 'rhocrit') for T in np.linspace(290, Props(fluid, 'Tcrit') - 0.1, 100): rhoV = Props('D', 'T', T, 'Q', 1, fluid) rhoL = Props('D', 'T', T, 'Q', 0, fluid) rhomax = Props('D', 'T', Props(fluid, 'Tmin'), 'Q', 0, fluid) for rho in list(np.linspace(rhoL, rhomax, 100)): # +list(np.linspace(rhoV,0.0001,100)): # for rho in list(np.linspace(rhoV,0.0001,100)): mu_0 = viscosity_dilute(fluid, T, E_K[fluid], SIGMA[fluid]) mu_1 = viscosity_linear(fluid, T, rho, E_K[fluid], SIGMA[fluid]) mu = Props('V', 'T', T, 'D', rho, fluid) VV << mu VV0 << mu_0 VV1 << mu_1 VVH << mu - mu_0 - mu_1 TT << T RHO << rho DELTA << rho / rhoc TAU << Tc / T def f_RHS(E, DELTA_TAU, VV): k = 0 sum = 0 DELTA = DELTA_TAU[0, :] TAU = DELTA_TAU[1, :] for i in range(2, 5): for j in range(3): sum += E[k] * DELTA**i / TAU**j k += 1 # f1,f2,f3,g1,g2 = E[k],E[k+1],E[k+2],E[k+3],E[k+4] # DELTA0 = g1*(1+g2*np.sqrt(TAU)) # sum += (f1+f2/TAU+f3/TAU/TAU)*(DELTA/(DELTA0-DELTA)-DELTA/DELTA0) print('%s %%' % np.mean(np.abs(((sum / VV - 1) * 100)))) return sum log_muH = np.log(VVH.v().T) x = np.c_[DELTA.v().T, TAU.v().T].T y = VVH.v() linear = Model(f_RHS, extra_args=(y,)) mydata = Data(x, y) myodr = ODR(mydata, linear, beta0=np.array([0.1] * 17),) myoutput = myodr.run() E = myoutput.beta print(E) #plt.plot(TT.vec, y,'b.',TT.vec, f_RHS(E, x, y),'r.') # plt.show() # plt.plot() plt.plot(y.T, f_RHS(E, x, y)) plt.show()
mit
sniemi/SamPy
sandbox/src1/examples/font_indexing.py
4
1299
""" A little example that shows how the various indexing into the font tables relate to one another. Mainly for mpl developers.... """ import matplotlib from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, KERNING_UNFITTED, KERNING_UNSCALED #fname = '/usr/share/fonts/sfd/FreeSans.ttf' fname = matplotlib.get_data_path() + '/fonts/ttf/Vera.ttf' font = FT2Font(fname) font.set_charmap(0) codes = font.get_charmap().items() #dsu = [(ccode, glyphind) for ccode, glyphind in codes] #dsu.sort() #for ccode, glyphind in dsu: # try: name = font.get_glyph_name(glyphind) # except RuntimeError: pass # else: print '% 4d % 4d %s %s'%(glyphind, ccode, hex(int(ccode)), name) # make a charname to charcode and glyphind dictionary coded = {} glyphd = {} for ccode, glyphind in codes: name = font.get_glyph_name(glyphind) coded[name] = ccode glyphd[name] = glyphind code = coded['A'] glyph = font.load_char(code) #print glyph.bbox print glyphd['A'], glyphd['V'], coded['A'], coded['V'] print 'AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_DEFAULT) print 'AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNFITTED) print 'AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNSCALED) print 'AV', font.get_kerning(glyphd['A'], glyphd['T'], KERNING_UNSCALED)
bsd-2-clause
edhuckle/statsmodels
statsmodels/examples/example_enhanced_boxplots.py
33
3179
from __future__ import print_function import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm # Necessary to make horizontal axis labels fit plt.rcParams['figure.subplot.bottom'] = 0.23 data = sm.datasets.anes96.load_pandas() party_ID = np.arange(7) labels = ["Strong Democrat", "Weak Democrat", "Independent-Democrat", "Independent-Independent", "Independent-Republican", "Weak Republican", "Strong Republican"] # Group age by party ID. age = [data.exog['age'][data.endog == id] for id in party_ID] # Create a violin plot. fig = plt.figure() ax = fig.add_subplot(111) sm.graphics.violinplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30}) ax.set_xlabel("Party identification of respondent.") ax.set_ylabel("Age") ax.set_title("US national election '96 - Age & Party Identification") # Create a bean plot. fig2 = plt.figure() ax = fig2.add_subplot(111) sm.graphics.beanplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30}) ax.set_xlabel("Party identification of respondent.") ax.set_ylabel("Age") ax.set_title("US national election '96 - Age & Party Identification") # Create a jitter plot. fig3 = plt.figure() ax = fig3.add_subplot(111) plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30, 'violin_fc':(0.8, 0.8, 0.8), 'jitter_marker':'.', 'jitter_marker_size':3, 'bean_color':'#FF6F00', 'bean_mean_color':'#009D91'} sm.graphics.beanplot(age, ax=ax, labels=labels, jitter=True, plot_opts=plot_opts) ax.set_xlabel("Party identification of respondent.") ax.set_ylabel("Age") ax.set_title("US national election '96 - Age & Party Identification") # Create an asymmetrical jitter plot. ix = data.exog['income'] < 16 # incomes < $30k age = data.exog['age'][ix] endog = data.endog[ix] age_lower_income = [age[endog == id] for id in party_ID] ix = data.exog['income'] >= 20 # incomes > $50k age = data.exog['age'][ix] endog = data.endog[ix] age_higher_income = [age[endog == id] for id in party_ID] fig = plt.figure() ax = fig.add_subplot(111) plot_opts['violin_fc'] = (0.5, 0.5, 0.5) plot_opts['bean_show_mean'] = False plot_opts['bean_show_median'] = False plot_opts['bean_legend_text'] = 'Income < \$30k' plot_opts['cutoff_val'] = 10 sm.graphics.beanplot(age_lower_income, ax=ax, labels=labels, side='left', jitter=True, plot_opts=plot_opts) plot_opts['violin_fc'] = (0.7, 0.7, 0.7) plot_opts['bean_color'] = '#009D91' plot_opts['bean_legend_text'] = 'Income > \$50k' sm.graphics.beanplot(age_higher_income, ax=ax, labels=labels, side='right', jitter=True, plot_opts=plot_opts) ax.set_xlabel("Party identification of respondent.") ax.set_ylabel("Age") ax.set_title("US national election '96 - Age & Party Identification") # Show all plots. plt.show()
bsd-3-clause
Tahsin-Mayeesha/Udacity-Machine-Learning-Nanodegree
projects/titanic_survival_exploration/titanic_visualizations.py
24
5425
import numpy as np import pandas as pd import matplotlib.pyplot as plt def filter_data(data, condition): """ Remove elements that do not match the condition provided. Takes a data list as input and returns a filtered list. Conditions should be a list of strings of the following format: '<field> <op> <value>' where the following operations are valid: >, <, >=, <=, ==, != Example: ["Sex == 'male'", 'Age < 18'] """ field, op, value = condition.split(" ") # convert value into number or strip excess quotes if string try: value = float(value) except: value = value.strip("\'\"") # get booleans for filtering if op == ">": matches = data[field] > value elif op == "<": matches = data[field] < value elif op == ">=": matches = data[field] >= value elif op == "<=": matches = data[field] <= value elif op == "==": matches = data[field] == value elif op == "!=": matches = data[field] != value else: # catch invalid operation codes raise Exception("Invalid comparison operator. Only >, <, >=, <=, ==, != allowed.") # filter data and outcomes data = data[matches].reset_index(drop = True) return data def survival_stats(data, outcomes, key, filters = []): """ Print out selected statistics regarding survival, given a feature of interest and any number of filters (including no filters) """ # Check that the key exists if key not in data.columns.values : print "'{}' is not a feature of the Titanic data. Did you spell something wrong?".format(key) return False # Return the function before visualizing if 'Cabin' or 'Ticket' # is selected: too many unique categories to display if(key == 'Cabin' or key == 'PassengerId' or key == 'Ticket'): print "'{}' has too many unique categories to display! Try a different feature.".format(key) return False # Merge data and outcomes into single dataframe all_data = pd.concat([data, outcomes], axis = 1) # Apply filters to data for condition in filters: all_data = filter_data(all_data, condition) # Create outcomes DataFrame all_data = all_data[[key, 'Survived']] # Create plotting figure plt.figure(figsize=(8,6)) # 'Numerical' features if(key == 'Age' or key == 'Fare'): # Remove NaN values from Age data all_data = all_data[~np.isnan(all_data[key])] # Divide the range of data into bins and count survival rates min_value = all_data[key].min() max_value = all_data[key].max() value_range = max_value - min_value # 'Fares' has larger range of values than 'Age' so create more bins if(key == 'Fare'): bins = np.arange(0, all_data['Fare'].max() + 20, 20) if(key == 'Age'): bins = np.arange(0, all_data['Age'].max() + 10, 10) # Overlay each bin's survival rates nonsurv_vals = all_data[all_data['Survived'] == 0][key].reset_index(drop = True) surv_vals = all_data[all_data['Survived'] == 1][key].reset_index(drop = True) plt.hist(nonsurv_vals, bins = bins, alpha = 0.6, color = 'red', label = 'Did not survive') plt.hist(surv_vals, bins = bins, alpha = 0.6, color = 'green', label = 'Survived') # Add legend to plot plt.xlim(0, bins.max()) plt.legend(framealpha = 0.8) # 'Categorical' features else: # Set the various categories if(key == 'Pclass'): values = np.arange(1,4) if(key == 'Parch' or key == 'SibSp'): values = np.arange(0,np.max(data[key]) + 1) if(key == 'Embarked'): values = ['C', 'Q', 'S'] if(key == 'Sex'): values = ['male', 'female'] # Create DataFrame containing categories and count of each frame = pd.DataFrame(index = np.arange(len(values)), columns=(key,'Survived','NSurvived')) for i, value in enumerate(values): frame.loc[i] = [value, \ len(all_data[(all_data['Survived'] == 1) & (all_data[key] == value)]), \ len(all_data[(all_data['Survived'] == 0) & (all_data[key] == value)])] # Set the width of each bar bar_width = 0.4 # Display each category's survival rates for i in np.arange(len(frame)): nonsurv_bar = plt.bar(i-bar_width, frame.loc[i]['NSurvived'], width = bar_width, color = 'r') surv_bar = plt.bar(i, frame.loc[i]['Survived'], width = bar_width, color = 'g') plt.xticks(np.arange(len(frame)), values) plt.legend((nonsurv_bar[0], surv_bar[0]),('Did not survive', 'Survived'), framealpha = 0.8) # Common attributes for plot formatting plt.xlabel(key) plt.ylabel('Number of Passengers') plt.title('Passenger Survival Statistics With \'%s\' Feature'%(key)) plt.show() # Report number of passengers with missing values if sum(pd.isnull(all_data[key])): nan_outcomes = all_data[pd.isnull(all_data[key])]['Survived'] print "Passengers with missing '{}' values: {} ({} survived, {} did not survive)".format( \ key, len(nan_outcomes), sum(nan_outcomes == 1), sum(nan_outcomes == 0))
mit
google/material-design-icons
update/venv/lib/python3.9/site-packages/fontTools/varLib/plot.py
5
4153
"""Visualize DesignSpaceDocument and resulting VariationModel.""" from fontTools.varLib.models import VariationModel, supportScalar from fontTools.designspaceLib import DesignSpaceDocument from matplotlib import pyplot from mpl_toolkits.mplot3d import axes3d from itertools import cycle import math import logging import sys log = logging.getLogger(__name__) def stops(support, count=10): a,b,c = support return [a + (b - a) * i / count for i in range(count)] + \ [b + (c - b) * i / count for i in range(count)] + \ [c] def _plotLocationsDots(locations, axes, subplot, **kwargs): for loc, color in zip(locations, cycle(pyplot.cm.Set1.colors)): if len(axes) == 1: subplot.plot( [loc.get(axes[0], 0)], [1.], 'o', color=color, **kwargs ) elif len(axes) == 2: subplot.plot( [loc.get(axes[0], 0)], [loc.get(axes[1], 0)], [1.], 'o', color=color, **kwargs ) else: raise AssertionError(len(axes)) def plotLocations(locations, fig, names=None, **kwargs): n = len(locations) cols = math.ceil(n**.5) rows = math.ceil(n / cols) if names is None: names = [None] * len(locations) model = VariationModel(locations) names = [names[model.reverseMapping[i]] for i in range(len(names))] axes = sorted(locations[0].keys()) if len(axes) == 1: _plotLocations2D( model, axes[0], fig, cols, rows, names=names, **kwargs ) elif len(axes) == 2: _plotLocations3D( model, axes, fig, cols, rows, names=names, **kwargs ) else: raise ValueError("Only 1 or 2 axes are supported") def _plotLocations2D(model, axis, fig, cols, rows, names, **kwargs): subplot = fig.add_subplot(111) for i, (support, color, name) in enumerate( zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names)) ): if name is not None: subplot.set_title(name) subplot.set_xlabel(axis) pyplot.xlim(-1.,+1.) Xs = support.get(axis, (-1.,0.,+1.)) X, Y = [], [] for x in stops(Xs): y = supportScalar({axis:x}, support) X.append(x) Y.append(y) subplot.plot(X, Y, color=color, **kwargs) _plotLocationsDots(model.locations, [axis], subplot) def _plotLocations3D(model, axes, fig, rows, cols, names, **kwargs): ax1, ax2 = axes axis3D = fig.add_subplot(111, projection='3d') for i, (support, color, name) in enumerate( zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names)) ): if name is not None: axis3D.set_title(name) axis3D.set_xlabel(ax1) axis3D.set_ylabel(ax2) pyplot.xlim(-1.,+1.) pyplot.ylim(-1.,+1.) Xs = support.get(ax1, (-1.,0.,+1.)) Ys = support.get(ax2, (-1.,0.,+1.)) for x in stops(Xs): X, Y, Z = [], [], [] for y in Ys: z = supportScalar({ax1:x, ax2:y}, support) X.append(x) Y.append(y) Z.append(z) axis3D.plot(X, Y, Z, color=color, **kwargs) for y in stops(Ys): X, Y, Z = [], [], [] for x in Xs: z = supportScalar({ax1:x, ax2:y}, support) X.append(x) Y.append(y) Z.append(z) axis3D.plot(X, Y, Z, color=color, **kwargs) _plotLocationsDots(model.locations, [ax1, ax2], axis3D) def plotDocument(doc, fig, **kwargs): doc.normalize() locations = [s.location for s in doc.sources] names = [s.name for s in doc.sources] plotLocations(locations, fig, names, **kwargs) def main(args=None): from fontTools import configLogger if args is None: args = sys.argv[1:] # configure the library logger (for >= WARNING) configLogger() # comment this out to enable debug messages from logger # log.setLevel(logging.DEBUG) if len(args) < 1: print("usage: fonttools varLib.plot source.designspace", file=sys.stderr) print(" or") print("usage: fonttools varLib.plot location1 location2 ...", file=sys.stderr) sys.exit(1) fig = pyplot.figure() fig.set_tight_layout(True) if len(args) == 1 and args[0].endswith('.designspace'): doc = DesignSpaceDocument() doc.read(args[0]) plotDocument(doc, fig) else: axes = [chr(c) for c in range(ord('A'), ord('Z')+1)] locs = [dict(zip(axes, (float(v) for v in s.split(',')))) for s in args] plotLocations(locs, fig) pyplot.show() if __name__ == '__main__': import sys sys.exit(main())
apache-2.0
cauchycui/scikit-learn
examples/mixture/plot_gmm_pdf.py
284
1528
""" ============================================= Density Estimation for a mixture of Gaussians ============================================= Plot the density estimation of a mixture of two Gaussians. Data is generated from two Gaussians with different centers and covariance matrices. """ import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LogNorm from sklearn import mixture n_samples = 300 # generate random sample, two components np.random.seed(0) # generate spherical data centered on (20, 20) shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 20]) # generate zero centered stretched Gaussian data C = np.array([[0., -0.7], [3.5, .7]]) stretched_gaussian = np.dot(np.random.randn(n_samples, 2), C) # concatenate the two datasets into the final training set X_train = np.vstack([shifted_gaussian, stretched_gaussian]) # fit a Gaussian Mixture Model with two components clf = mixture.GMM(n_components=2, covariance_type='full') clf.fit(X_train) # display predicted scores by the model as a contour plot x = np.linspace(-20.0, 30.0) y = np.linspace(-20.0, 40.0) X, Y = np.meshgrid(x, y) XX = np.array([X.ravel(), Y.ravel()]).T Z = -clf.score_samples(XX)[0] Z = Z.reshape(X.shape) CS = plt.contour(X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0), levels=np.logspace(0, 3, 10)) CB = plt.colorbar(CS, shrink=0.8, extend='both') plt.scatter(X_train[:, 0], X_train[:, 1], .8) plt.title('Negative log-likelihood predicted by a GMM') plt.axis('tight') plt.show()
bsd-3-clause
xubenben/scikit-learn
examples/manifold/plot_lle_digits.py
181
8510
""" ============================================================================= Manifold learning on handwritten digits: Locally Linear Embedding, Isomap... ============================================================================= An illustration of various embeddings on the digits dataset. The RandomTreesEmbedding, from the :mod:`sklearn.ensemble` module, is not technically a manifold embedding method, as it learn a high-dimensional representation on which we apply a dimensionality reduction method. However, it is often useful to cast a dataset into a representation in which the classes are linearly-separable. t-SNE will be initialized with the embedding that is generated by PCA in this example, which is not the default setting. It ensures global stability of the embedding, i.e., the embedding does not depend on random initialization. """ # Authors: Fabian Pedregosa <fabian.pedregosa@inria.fr> # Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Gael Varoquaux # License: BSD 3 clause (C) INRIA 2011 print(__doc__) from time import time import numpy as np import matplotlib.pyplot as plt from matplotlib import offsetbox from sklearn import (manifold, datasets, decomposition, ensemble, lda, random_projection) digits = datasets.load_digits(n_class=6) X = digits.data y = digits.target n_samples, n_features = X.shape n_neighbors = 30 #---------------------------------------------------------------------- # Scale and visualize the embedding vectors def plot_embedding(X, title=None): x_min, x_max = np.min(X, 0), np.max(X, 0) X = (X - x_min) / (x_max - x_min) plt.figure() ax = plt.subplot(111) for i in range(X.shape[0]): plt.text(X[i, 0], X[i, 1], str(digits.target[i]), color=plt.cm.Set1(y[i] / 10.), fontdict={'weight': 'bold', 'size': 9}) if hasattr(offsetbox, 'AnnotationBbox'): # only print thumbnails with matplotlib > 1.0 shown_images = np.array([[1., 1.]]) # just something big for i in range(digits.data.shape[0]): dist = np.sum((X[i] - shown_images) ** 2, 1) if np.min(dist) < 4e-3: # don't show points that are too close continue shown_images = np.r_[shown_images, [X[i]]] imagebox = offsetbox.AnnotationBbox( offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r), X[i]) ax.add_artist(imagebox) plt.xticks([]), plt.yticks([]) if title is not None: plt.title(title) #---------------------------------------------------------------------- # Plot images of the digits n_img_per_row = 20 img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row)) for i in range(n_img_per_row): ix = 10 * i + 1 for j in range(n_img_per_row): iy = 10 * j + 1 img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8)) plt.imshow(img, cmap=plt.cm.binary) plt.xticks([]) plt.yticks([]) plt.title('A selection from the 64-dimensional digits dataset') #---------------------------------------------------------------------- # Random 2D projection using a random unitary matrix print("Computing random projection") rp = random_projection.SparseRandomProjection(n_components=2, random_state=42) X_projected = rp.fit_transform(X) plot_embedding(X_projected, "Random Projection of the digits") #---------------------------------------------------------------------- # Projection on to the first 2 principal components print("Computing PCA projection") t0 = time() X_pca = decomposition.TruncatedSVD(n_components=2).fit_transform(X) plot_embedding(X_pca, "Principal Components projection of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Projection on to the first 2 linear discriminant components print("Computing LDA projection") X2 = X.copy() X2.flat[::X.shape[1] + 1] += 0.01 # Make X invertible t0 = time() X_lda = lda.LDA(n_components=2).fit_transform(X2, y) plot_embedding(X_lda, "Linear Discriminant projection of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Isomap projection of the digits dataset print("Computing Isomap embedding") t0 = time() X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X) print("Done.") plot_embedding(X_iso, "Isomap projection of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Locally linear embedding of the digits dataset print("Computing LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='standard') t0 = time() X_lle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_lle, "Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Modified Locally linear embedding of the digits dataset print("Computing modified LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='modified') t0 = time() X_mlle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_mlle, "Modified Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # HLLE embedding of the digits dataset print("Computing Hessian LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='hessian') t0 = time() X_hlle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_hlle, "Hessian Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # LTSA embedding of the digits dataset print("Computing LTSA embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='ltsa') t0 = time() X_ltsa = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_ltsa, "Local Tangent Space Alignment of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # MDS embedding of the digits dataset print("Computing MDS embedding") clf = manifold.MDS(n_components=2, n_init=1, max_iter=100) t0 = time() X_mds = clf.fit_transform(X) print("Done. Stress: %f" % clf.stress_) plot_embedding(X_mds, "MDS embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Random Trees embedding of the digits dataset print("Computing Totally Random Trees embedding") hasher = ensemble.RandomTreesEmbedding(n_estimators=200, random_state=0, max_depth=5) t0 = time() X_transformed = hasher.fit_transform(X) pca = decomposition.TruncatedSVD(n_components=2) X_reduced = pca.fit_transform(X_transformed) plot_embedding(X_reduced, "Random forest embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # Spectral embedding of the digits dataset print("Computing Spectral embedding") embedder = manifold.SpectralEmbedding(n_components=2, random_state=0, eigen_solver="arpack") t0 = time() X_se = embedder.fit_transform(X) plot_embedding(X_se, "Spectral embedding of the digits (time %.2fs)" % (time() - t0)) #---------------------------------------------------------------------- # t-SNE embedding of the digits dataset print("Computing t-SNE embedding") tsne = manifold.TSNE(n_components=2, init='pca', random_state=0) t0 = time() X_tsne = tsne.fit_transform(X) plot_embedding(X_tsne, "t-SNE embedding of the digits (time %.2fs)" % (time() - t0)) plt.show()
bsd-3-clause
tequa/ammisoft
ammimain/WinPython-64bit-2.7.13.1Zero/python-2.7.13.amd64/Lib/site-packages/mpl_toolkits/axes_grid1/anchored_artists.py
10
12803
from __future__ import (absolute_import, division, print_function, unicode_literals) import six from matplotlib import docstring from matplotlib.offsetbox import (AnchoredOffsetbox, AnchoredText, AnnotationBbox, AuxTransformBox, DrawingArea, TextArea, VPacker) from matplotlib.patches import Rectangle, Ellipse class AnchoredDrawingArea(AnchoredOffsetbox): @docstring.dedent def __init__(self, width, height, xdescent, ydescent, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): """ An anchored container with a fixed size and fillable DrawingArea. Artists added to the *drawing_area* will have their coordinates interpreted as pixels. Any transformations set on the artists will be overridden. Parameters ---------- width, height : int or float width and height of the container, in pixels. xdescent, ydescent : int or float descent of the container in the x- and y- direction, in pixels. loc : int Location of this artist. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the child objects, in fraction of the font size. Defaults to 0.4. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.5. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. frameon : bool, optional If True, draw a box around this artists. Defaults to True. **kwargs : Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- drawing_area : `matplotlib.offsetbox.DrawingArea` A container for artists to display. Examples -------- To display blue and red circles of different sizes in the upper right of an axes *ax*: >>> ada = AnchoredDrawingArea(20, 20, 0, 0, loc=1, frameon=False) >>> ada.drawing_area.add_artist(Circle((10, 10), 10, fc="b")) >>> ada.drawing_area.add_artist(Circle((30, 10), 5, fc="r")) >>> ax.add_artist(ada) """ self.da = DrawingArea(width, height, xdescent, ydescent) self.drawing_area = self.da super(AnchoredDrawingArea, self).__init__( loc, pad=pad, borderpad=borderpad, child=self.da, prop=None, frameon=frameon, **kwargs ) class AnchoredAuxTransformBox(AnchoredOffsetbox): @docstring.dedent def __init__(self, transform, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): """ An anchored container with transformed coordinates. Artists added to the *drawing_area* are scaled according to the coordinates of the transformation used. The dimensions of this artist will scale to contain the artists added. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. loc : int Location of this artist. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the child objects, in fraction of the font size. Defaults to 0.4. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.5. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. frameon : bool, optional If True, draw a box around this artists. Defaults to True. **kwargs : Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- drawing_area : `matplotlib.offsetbox.AuxTransformBox` A container for artists to display. Examples -------- To display an ellipse in the upper left, with a width of 0.1 and height of 0.4 in data coordinates: >>> box = AnchoredAuxTransformBox(ax.transData, loc=2) >>> el = Ellipse((0,0), width=0.1, height=0.4, angle=30) >>> box.drawing_area.add_artist(el) >>> ax.add_artist(box) """ self.drawing_area = AuxTransformBox(transform) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self.drawing_area, prop=prop, frameon=frameon, **kwargs) class AnchoredEllipse(AnchoredOffsetbox): @docstring.dedent def __init__(self, transform, width, height, angle, loc, pad=0.1, borderpad=0.1, prop=None, frameon=True, **kwargs): """ Draw an anchored ellipse of a given size. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. width, height : int or float Width and height of the ellipse, given in coordinates of *transform*. angle : int or float Rotation of the ellipse, in degrees, anti-clockwise. loc : int Location of this size bar. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the ellipse, in fraction of the font size. Defaults to 0.1. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.1. frameon : bool, optional If True, draw a box around the ellipse. Defaults to True. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. **kwargs : Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- ellipse : `matplotlib.patches.Ellipse` Ellipse patch drawn. """ self._box = AuxTransformBox(transform) self.ellipse = Ellipse((0, 0), width, height, angle) self._box.add_artist(self.ellipse) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon, **kwargs) class AnchoredSizeBar(AnchoredOffsetbox): @docstring.dedent def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, frameon=True, size_vertical=0, color='black', label_top=False, fontproperties=None, **kwargs): """ Draw a horizontal scale bar with a center-aligned label underneath. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. size : int or float Horizontal length of the size bar, given in coordinates of *transform*. label : str Label to display. loc : int Location of this size bar. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the label and size bar, in fraction of the font size. Defaults to 0.1. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.1. sep : int or float, optional Seperation between the label and the size bar, in points. Defaults to 2. frameon : bool, optional If True, draw a box around the horizontal bar and label. Defaults to True. size_vertical : int or float, optional Vertical length of the size bar, given in coordinates of *transform*. Defaults to 0. color : str, optional Color for the size bar and label. Defaults to black. label_top : bool, optional If True, the label will be over the size bar. Defaults to False. fontproperties : `matplotlib.font_manager.FontProperties`, optional Font properties for the label text. **kwargs : Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- size_bar : `matplotlib.offsetbox.AuxTransformBox` Container for the size bar. txt_label : `matplotlib.offsetbox.TextArea` Container for the label of the size bar. Notes ----- If *prop* is passed as a keyworded argument, but *fontproperties* is not, then *prop* is be assumed to be the intended *fontproperties*. Using both *prop* and *fontproperties* is not supported. Examples -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import \ AnchoredSizeBar >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10,10))) >>> bar = AnchoredSizeBar(ax.transData, 3, '3 data units', 4) >>> ax.add_artist(bar) >>> fig.show() Using all the optional parameters >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(size=14, family='monospace') >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, \ sep=5, borderpad=0.5, frameon=False, \ size_vertical=0.5, color='white', \ fontproperties=fontprops) """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0, 0), size, size_vertical, fill=False, facecolor=color, edgecolor=color)) if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if fontproperties is None: textprops = {'color': color} else: textprops = {'color': color, 'fontproperties': fontproperties} self.txt_label = TextArea( label, minimumdescent=False, textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=fontproperties, frameon=frameon, **kwargs)
bsd-3-clause
mganeva/mantid
qt/applications/workbench/workbench/widgets/plotselector/presenter.py
1
15293
# Mantid Repository : https://github.com/mantidproject/mantid # # Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source # & Institut Laue - Langevin # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantid workbench. # # from __future__ import absolute_import, print_function import os import re from .model import PlotSelectorModel from .view import PlotSelectorView, Column class PlotSelectorPresenter(object): """ Presenter for the plot selector widget. This class can be responsible for the creation of the model and view, passing in the GlobalFigureManager as an argument, or the presenter and view can be passed as arguments (only intended for testing). """ def __init__(self, global_figure_manager, view=None, model=None): """ Initialise the presenter, creating the view and model, and setting the initial plot list :param global_figure_manager: The GlobalFigureManager class :param view: Optional - a view to use instead of letting the class create one (intended for testing) :param model: Optional - a model to use instead of letting the class create one (intended for testing) """ # Create model and view, or accept mocked versions if view is None: self.view = PlotSelectorView(self) else: self.view = view if model is None: self.model = PlotSelectorModel(self, global_figure_manager) else: self.model = model # Make sure the plot list is up to date self.update_plot_list() def get_plot_name_from_number(self, plot_number): return self.model.get_plot_name_from_number(plot_number) # ------------------------ Plot Updates ------------------------ def update_plot_list(self): """ Updates the plot list in the model and the view. Filter text is applied to the updated selection if required. """ plot_list = self.model.get_plot_list() self.view.set_plot_list(plot_list) def append_to_plot_list(self, plot_number): """ Appends the plot name to the end of the plot list :param plot_number: The unique number in GlobalFigureManager """ self.view.append_to_plot_list(plot_number) self.view.set_visibility_icon(plot_number, self.model.is_visible(plot_number)) def remove_from_plot_list(self, plot_number): """ Removes the plot name from the plot list :param plot_number: The unique number in GlobalFigureManager """ self.view.remove_from_plot_list(plot_number) def rename_in_plot_list(self, plot_number, new_name): """ Replaces a name in the plot list :param plot_number: The unique number in GlobalFigureManager :param new_name: The new name for the plot """ self.view.rename_in_plot_list(plot_number, new_name) # ----------------------- Plot Filtering ------------------------ def filter_text_changed(self): """ Called by the view when the filter text is changed (e.g. by typing or clearing the text) """ if self.view.get_filter_text(): self.view.filter_plot_list() else: self.view.unhide_all_plots() def is_shown_by_filter(self, plot_number): """ :param plot_number: The unique number in GlobalFigureManager :return: True if shown, or False if filtered out """ filter_text = self.view.get_filter_text() plot_name = self.get_plot_name_from_number(plot_number) return filter_text.lower() in plot_name.lower() # ------------------------ Plot Showing ------------------------ def show_single_selected(self): """ When a list item is double clicked the view calls this method to bring the selected plot to the front """ plot_number = self.view.get_currently_selected_plot_number() self._make_plot_active(plot_number) def show_multiple_selected(self): """ Shows multiple selected plots, e.g. from pressing the 'Show' button with multiple selected plots """ selected_plots = self.view.get_all_selected_plot_numbers() for plot_number in selected_plots: self._make_plot_active(plot_number) def _make_plot_active(self, plot_number): """ Make the plot with the given name active - bring it to the front and make it the choice for overplotting :param plot_number: The unique number in GlobalFigureManager """ try: self.model.show_plot(plot_number) except ValueError as e: print(e) def set_active_font(self, plot_number): """ Set the icon for the active plot to be colored :param plot_number: The unique number in GlobalFigureManager """ active_plot_number = self.view.active_plot_number if active_plot_number > 0: try: self.view.set_active_font(active_plot_number, False) except TypeError: pass # The last active plot could have been closed # already, so there is nothing to do self.view.set_active_font(plot_number, True) self.view.active_plot_number = plot_number # ------------------------ Plot Hiding ------------------------- def hide_selected_plots(self): """ Hide all plots that are selected in the view """ selected_plots = self.view.get_all_selected_plot_numbers() for plot_number in selected_plots: self._hide_plot(plot_number) def _hide_plot(self, plot_number): """ Hides a single plot """ try: self.model.hide_plot(plot_number) except ValueError as e: print(e) def toggle_plot_visibility(self, plot_number): """ Toggles a plot between hidden and shown :param plot_number: The unique number in GlobalFigureManager """ if self.model.is_visible(plot_number): self._hide_plot(plot_number) else: self._make_plot_active(plot_number) self.update_visibility_icon(plot_number) def update_visibility_icon(self, plot_number): """ Updates the icon to indicate a plot as hidden or visible :param plot_number: The unique number in GlobalFigureManager """ try: is_visible = self.model.is_visible(plot_number) self.view.set_visibility_icon(plot_number, is_visible) except ValueError: # There is a chance the plot was closed, which calls an # update to this method. If we can not get the visibility # status it is safe to assume the plot has been closed. pass # ------------------------ Plot Renaming ------------------------ def rename_figure(self, plot_number, new_name): """ Replaces a name in the plot list :param plot_number: The unique number in GlobalFigureManager :param new_name: The new plot name """ try: self.model.rename_figure(plot_number, new_name) except ValueError as e: # We need to undo the rename in the view self.view.rename_in_plot_list(plot_number, new_name) print(e) # ------------------------ Plot Closing ------------------------- def close_action_called(self): """ This is called by the view when closing plots is requested (e.g. pressing close or delete). """ selected_plots = self.view.get_all_selected_plot_numbers() self._close_plots(selected_plots) def close_single_plot(self, plot_number): """ This is used to close plots when a close action is called that does not refer to the selected plot(s) :param plot_number: The unique number in GlobalFigureManager """ self._close_plots([plot_number]) def _close_plots(self, list_of_plot_numbers): """ Accepts a list of plot names to close :param list_of_plots: A list of strings containing plot names """ for plot_number in list_of_plot_numbers: try: self.model.close_plot(plot_number) except ValueError as e: print(e) # ----------------------- Plot Sorting -------------------------- def set_sort_order(self, is_ascending): """ Sets the sort order in the view :param is_ascending: If true ascending order, else descending """ self.view.set_sort_order(is_ascending) def set_sort_type(self, sort_type): """ Sets the sort order in the view :param sort_type: A Column enum with the column to sort on """ self.view.set_sort_type(sort_type) self.update_last_active_order() def update_last_active_order(self): """ Update the sort keys in the view. This is only required when changes to the last shown order occur in the model, when renaming the key is set already """ if self.view.sort_type() == Column.LastActive: self._set_last_active_order() def _set_last_active_order(self): """ Set the last shown order in the view. This checks the sorting currently set and then sets the sort keys to the appropriate values """ last_active_values = self.model.last_active_values() self.view.set_last_active_values(last_active_values) def get_initial_last_active_value(self, plot_number): """ Gets the initial last active value for a plot just added, in this case it is assumed to not have been shown :param plot_number: The unique number in GlobalFigureManager :return: A string with the last active value """ return '_' + self.model.get_plot_name_from_number(plot_number) def get_renamed_last_active_value(self, plot_number, old_last_active_value): """ Gets the initial last active value for a plot that was renamed. If the plot had a numeric value, i.e. has been shown this is retained, else it is set :param plot_number: The unique number in GlobalFigureManager :param old_last_active_value: The previous last active value """ if old_last_active_value.isdigit(): return old_last_active_value else: return self.get_initial_last_active_value(plot_number) # ---------------------- Plot Exporting ------------------------- def export_plots_called(self, extension): """ Export plots called from the view, then a single or multiple plots exported depending on the number currently selected :param extension: The file extension as a string including a '.', for example '.png' (must be a type supported by matplotlib) """ plot_numbers = self.view.get_all_selected_plot_numbers() if len(plot_numbers) == 1: self._export_single_plot(plot_numbers[0], extension) elif len(plot_numbers) > 1: self._export_multiple_plots(plot_numbers, extension) def _export_single_plot(self, plot_number, extension): """ Called when a single plot is selected to export - prompts for a filename then tries to save the plot :param plot_number: The unique number in GlobalFigureManager :param extension: The file extension as a string including a '.', for example '.png' (must be a type supported by matplotlib) """ absolute_path = self.view.get_file_name_for_saving(extension) if not absolute_path[-4:] == extension: absolute_path += extension try: self.model.export_plot(plot_number, absolute_path) except ValueError as e: print(e) def _export_multiple_plots(self, plot_numbers, extension): """ Export all selected plots in the plot_numbers list, first prompting for a save directory then sanitising plot names to unique, usable file names :param plot_numbers: A list of plot numbers to export :param extension: The file extension as a string including a '.', for example '.png' (must be a type supported by matplotlib) """ dir_name = self.view.get_directory_name_for_saving() # A temporary dictionary holding plot numbers as keys, plot # names as values plots = {} for plot_number in plot_numbers: plot_name = self.model.get_plot_name_from_number(plot_number) plot_name = self._replace_special_characters(plot_name) if plot_name in plots.values(): plot_name = self._make_unique_name(plot_name, plots) plots[plot_number] = plot_name self._export_plot(plot_number, plot_name, dir_name, extension) def _replace_special_characters(self, string): """ Removes any characters that are not valid in file names across all operating systems ('/' for Linux/Mac), more for Windows :param string: The string to replace characters in :return: The string with special characters replace by '-' """ return re.sub(r'[<>:"/|\\?*]', r'-', string) def _make_unique_name(self, name, dictionary): """ Given a name and a dictionary, make a unique name that does not already exist in the dictionary values by appending ' (1)', ' (2)', ' (3)' etc. to the end of the name :param name: A string with the non-unique name :param dictionary: A dictionary with string values :return : The unique plot name """ i = 1 while True: plot_name_attempt = name + ' ({})'.format(str(i)) if plot_name_attempt not in dictionary.values(): break i += 1 return plot_name_attempt def _export_plot(self, plot_number, plot_name, dir_name, extension): """ Given a plot number, plot name, directory and extension construct the absolute path name and call the model to save the figure :param plot_number: The unique number in GlobalFigureManager :param plot_name: The name to use for saving :param dir_name: The directory to save to :param extension: The file extension as a string including a '.', for example '.png' (must be a type supported by matplotlib) """ if dir_name: filename = os.path.join(dir_name, plot_name + extension) try: self.model.export_plot(plot_number, filename) except ValueError as e: print(e)
gpl-3.0
wangkua1/sportvu
sportvu/detection_from_raw_pred.py
1
3391
"""detection_from_raw_pred.py * not super useful, a simple script that plots a) raw pred, b) gt pnr, c) detector output at 1 single setting Usage: detection_from_raw_pred.py <fold_index> <f_data_config> <f_model_config> <f_detect_config> --train Arguments: Example: """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import sys import os from tqdm import tqdm from docopt import docopt import yaml import gc import matplotlib.pylab as plt import cPickle as pkl ## from sportvu.data.dataset import BaseDataset from sportvu.detect.running_window_p import RunWindowP from sportvu.detect.nms import NMS from sportvu.detect.utils import smooth_1D_array arguments = docopt(__doc__) print ("...Docopt... ") print(arguments) print ("............\n") f_data_config = arguments['<f_data_config>'] f_model_config = arguments['<f_model_config>'] f_detect_config = arguments['<f_detect_config>'] if arguments['--train']: dataset = BaseDataset(f_data_config, fold_index=int(arguments['<fold_index>']), load_raw=True) # pre_trained = arguments['<pre_trained>'] data_config = yaml.load(open(f_data_config, 'rb')) model_config = yaml.load(open(f_model_config, 'rb')) model_name = os.path.basename(f_model_config).split('.')[0] data_name = os.path.basename(f_data_config).split('.')[0] exp_name = '%s-X-%s' % (model_name, data_name) detect_config = yaml.load(open(f_detect_config, 'rb')) detector = eval(detect_config['class'])(detect_config) plot_folder = os.path.join('./plots', exp_name) if not os.path.exists(plot_folder): raise Exception('Run test.py first to get raw predictions') def label_in_cand(cand, labels): for l in labels: if l > cand[1] and l < cand[0]: return True return False plt.figure() if arguments['--train']: split = 'train' else: split = 'val' all_pred_f = filter(lambda s:'.pkl' in s and split in s and 'meta' not in s,os.listdir(os.path.join(plot_folder,'pkl'))) if arguments['--train']: annotations = [] for _, f in tqdm(enumerate(all_pred_f)): ind = int(f.split('.')[0].split('-')[1]) gameclocks, pnr_probs, labels = pkl.load(open(os.path.join(plot_folder,'pkl/%s-%i.pkl'%(split,ind)), 'rb')) meta = pkl.load( open( os.path.join(plot_folder, 'pkl/%s-meta-%i.pkl' %(split, ind)), 'rb')) cands, mp, frame_indices = detector.detect(pnr_probs, gameclocks, True) print (cands) plt.plot(gameclocks, pnr_probs, '-') if mp is not None: plt.plot(gameclocks, mp, '-') plt.plot(np.array(labels), np.ones((len(labels))), '.') for ind, cand in enumerate(cands): cand_x = np.arange(cand[1], cand[0], .1) plt.plot(cand_x, np.ones((len(cand_x))) * .95, '-' ) ## if FP, record annotations if arguments['--train'] and not label_in_cand(cand, labels): anno = {'gameid':meta[1], 'gameclock':gameclocks[frame_indices[ind]], 'eid':meta[0], 'quarter':dataset.games[meta[1]]['events'][meta[0]]['quarter']} annotations.append(anno) plt.ylim([0,1]) plt.title('Game: %s, Event: %i'%(meta[1], meta[0])) plt.savefig(os.path.join(plot_folder, '%s-%s-%i.png' %(detect_config['class'], split, ind))) plt.clf() pkl.dump(annotations, open(os.path.join(plot_folder,'pkl/hard-negative-examples.pkl'), 'wb'))
mit
Edu-Glez/Bank_sentiment_analysis
env/lib/python3.6/site-packages/ipykernel/kernelapp.py
5
19344
"""An Application for launching a kernel""" # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. from __future__ import print_function import atexit import os import sys import signal import traceback import logging from tornado import ioloop import zmq from zmq.eventloop import ioloop as zmq_ioloop from zmq.eventloop.zmqstream import ZMQStream from IPython.core.application import ( BaseIPythonApplication, base_flags, base_aliases, catch_config_error ) from IPython.core.profiledir import ProfileDir from IPython.core.shellapp import ( InteractiveShellApp, shell_flags, shell_aliases ) from IPython.utils import io from ipython_genutils.path import filefind, ensure_dir_exists from traitlets import ( Any, Instance, Dict, Unicode, Integer, Bool, DottedObjectName, Type, default ) from ipython_genutils.importstring import import_item from jupyter_core.paths import jupyter_runtime_dir from jupyter_client import write_connection_file from jupyter_client.connect import ConnectionFileMixin # local imports from .iostream import IOPubThread from .heartbeat import Heartbeat from .ipkernel import IPythonKernel from .parentpoller import ParentPollerUnix, ParentPollerWindows from jupyter_client.session import ( Session, session_flags, session_aliases, ) from .zmqshell import ZMQInteractiveShell #----------------------------------------------------------------------------- # Flags and Aliases #----------------------------------------------------------------------------- kernel_aliases = dict(base_aliases) kernel_aliases.update({ 'ip' : 'IPKernelApp.ip', 'hb' : 'IPKernelApp.hb_port', 'shell' : 'IPKernelApp.shell_port', 'iopub' : 'IPKernelApp.iopub_port', 'stdin' : 'IPKernelApp.stdin_port', 'control' : 'IPKernelApp.control_port', 'f' : 'IPKernelApp.connection_file', 'transport': 'IPKernelApp.transport', }) kernel_flags = dict(base_flags) kernel_flags.update({ 'no-stdout' : ( {'IPKernelApp' : {'no_stdout' : True}}, "redirect stdout to the null device"), 'no-stderr' : ( {'IPKernelApp' : {'no_stderr' : True}}, "redirect stderr to the null device"), 'pylab' : ( {'IPKernelApp' : {'pylab' : 'auto'}}, """Pre-load matplotlib and numpy for interactive use with the default matplotlib backend."""), }) # inherit flags&aliases for any IPython shell apps kernel_aliases.update(shell_aliases) kernel_flags.update(shell_flags) # inherit flags&aliases for Sessions kernel_aliases.update(session_aliases) kernel_flags.update(session_flags) _ctrl_c_message = """\ NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work. To exit, you will have to explicitly quit this process, by either sending "quit" from a client, or using Ctrl-\\ in UNIX-like environments. To read more about this, see https://github.com/ipython/ipython/issues/2049 """ #----------------------------------------------------------------------------- # Application class for starting an IPython Kernel #----------------------------------------------------------------------------- class IPKernelApp(BaseIPythonApplication, InteractiveShellApp, ConnectionFileMixin): name='ipython-kernel' aliases = Dict(kernel_aliases) flags = Dict(kernel_flags) classes = [IPythonKernel, ZMQInteractiveShell, ProfileDir, Session] # the kernel class, as an importstring kernel_class = Type('ipykernel.ipkernel.IPythonKernel', klass='ipykernel.kernelbase.Kernel', help="""The Kernel subclass to be used. This should allow easy re-use of the IPKernelApp entry point to configure and launch kernels other than IPython's own. """).tag(config=True) kernel = Any() poller = Any() # don't restrict this even though current pollers are all Threads heartbeat = Instance(Heartbeat, allow_none=True) ports = Dict() subcommands = { 'install': ( 'ipykernel.kernelspec.InstallIPythonKernelSpecApp', 'Install the IPython kernel' ), } # connection info: connection_dir = Unicode() @default('connection_dir') def _default_connection_dir(self): return jupyter_runtime_dir() @property def abs_connection_file(self): if os.path.basename(self.connection_file) == self.connection_file: return os.path.join(self.connection_dir, self.connection_file) else: return self.connection_file # streams, etc. no_stdout = Bool(False, help="redirect stdout to the null device").tag(config=True) no_stderr = Bool(False, help="redirect stderr to the null device").tag(config=True) outstream_class = DottedObjectName('ipykernel.iostream.OutStream', help="The importstring for the OutStream factory").tag(config=True) displayhook_class = DottedObjectName('ipykernel.displayhook.ZMQDisplayHook', help="The importstring for the DisplayHook factory").tag(config=True) # polling parent_handle = Integer(int(os.environ.get('JPY_PARENT_PID') or 0), help="""kill this process if its parent dies. On Windows, the argument specifies the HANDLE of the parent process, otherwise it is simply boolean. """).tag(config=True) interrupt = Integer(int(os.environ.get('JPY_INTERRUPT_EVENT') or 0), help="""ONLY USED ON WINDOWS Interrupt this process when the parent is signaled. """).tag(config=True) def init_crash_handler(self): sys.excepthook = self.excepthook def excepthook(self, etype, evalue, tb): # write uncaught traceback to 'real' stderr, not zmq-forwarder traceback.print_exception(etype, evalue, tb, file=sys.__stderr__) def init_poller(self): if sys.platform == 'win32': if self.interrupt or self.parent_handle: self.poller = ParentPollerWindows(self.interrupt, self.parent_handle) elif self.parent_handle: self.poller = ParentPollerUnix() def _bind_socket(self, s, port): iface = '%s://%s' % (self.transport, self.ip) if self.transport == 'tcp': if port <= 0: port = s.bind_to_random_port(iface) else: s.bind("tcp://%s:%i" % (self.ip, port)) elif self.transport == 'ipc': if port <= 0: port = 1 path = "%s-%i" % (self.ip, port) while os.path.exists(path): port = port + 1 path = "%s-%i" % (self.ip, port) else: path = "%s-%i" % (self.ip, port) s.bind("ipc://%s" % path) return port def write_connection_file(self): """write connection info to JSON file""" cf = self.abs_connection_file self.log.debug("Writing connection file: %s", cf) write_connection_file(cf, ip=self.ip, key=self.session.key, transport=self.transport, shell_port=self.shell_port, stdin_port=self.stdin_port, hb_port=self.hb_port, iopub_port=self.iopub_port, control_port=self.control_port) def cleanup_connection_file(self): cf = self.abs_connection_file self.log.debug("Cleaning up connection file: %s", cf) try: os.remove(cf) except (IOError, OSError): pass self.cleanup_ipc_files() def init_connection_file(self): if not self.connection_file: self.connection_file = "kernel-%s.json"%os.getpid() try: self.connection_file = filefind(self.connection_file, ['.', self.connection_dir]) except IOError: self.log.debug("Connection file not found: %s", self.connection_file) # This means I own it, and I'll create it in this directory: ensure_dir_exists(os.path.dirname(self.abs_connection_file), 0o700) # Also, I will clean it up: atexit.register(self.cleanup_connection_file) return try: self.load_connection_file() except Exception: self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True) self.exit(1) def init_sockets(self): # Create a context, a session, and the kernel sockets. self.log.info("Starting the kernel at pid: %i", os.getpid()) context = zmq.Context.instance() # Uncomment this to try closing the context. # atexit.register(context.term) self.shell_socket = context.socket(zmq.ROUTER) self.shell_socket.linger = 1000 self.shell_port = self._bind_socket(self.shell_socket, self.shell_port) self.log.debug("shell ROUTER Channel on port: %i" % self.shell_port) self.stdin_socket = context.socket(zmq.ROUTER) self.stdin_socket.linger = 1000 self.stdin_port = self._bind_socket(self.stdin_socket, self.stdin_port) self.log.debug("stdin ROUTER Channel on port: %i" % self.stdin_port) self.control_socket = context.socket(zmq.ROUTER) self.control_socket.linger = 1000 self.control_port = self._bind_socket(self.control_socket, self.control_port) self.log.debug("control ROUTER Channel on port: %i" % self.control_port) self.init_iopub(context) def init_iopub(self, context): self.iopub_socket = context.socket(zmq.PUB) self.iopub_socket.linger = 1000 self.iopub_port = self._bind_socket(self.iopub_socket, self.iopub_port) self.log.debug("iopub PUB Channel on port: %i" % self.iopub_port) self.configure_tornado_logger() self.iopub_thread = IOPubThread(self.iopub_socket, pipe=True) self.iopub_thread.start() # backward-compat: wrap iopub socket API in background thread self.iopub_socket = self.iopub_thread.background_socket def init_heartbeat(self): """start the heart beating""" # heartbeat doesn't share context, because it mustn't be blocked # by the GIL, which is accessed by libzmq when freeing zero-copy messages hb_ctx = zmq.Context() self.heartbeat = Heartbeat(hb_ctx, (self.transport, self.ip, self.hb_port)) self.hb_port = self.heartbeat.port self.log.debug("Heartbeat REP Channel on port: %i" % self.hb_port) self.heartbeat.start() def log_connection_info(self): """display connection info, and store ports""" basename = os.path.basename(self.connection_file) if basename == self.connection_file or \ os.path.dirname(self.connection_file) == self.connection_dir: # use shortname tail = basename else: tail = self.connection_file lines = [ "To connect another client to this kernel, use:", " --existing %s" % tail, ] # log connection info # info-level, so often not shown. # frontends should use the %connect_info magic # to see the connection info for line in lines: self.log.info(line) # also raw print to the terminal if no parent_handle (`ipython kernel`) # unless log-level is CRITICAL (--quiet) if not self.parent_handle and self.log_level < logging.CRITICAL: io.rprint(_ctrl_c_message) for line in lines: io.rprint(line) self.ports = dict(shell=self.shell_port, iopub=self.iopub_port, stdin=self.stdin_port, hb=self.hb_port, control=self.control_port) def init_blackhole(self): """redirects stdout/stderr to devnull if necessary""" if self.no_stdout or self.no_stderr: blackhole = open(os.devnull, 'w') if self.no_stdout: sys.stdout = sys.__stdout__ = blackhole if self.no_stderr: sys.stderr = sys.__stderr__ = blackhole def init_io(self): """Redirect input streams and set a display hook.""" if self.outstream_class: outstream_factory = import_item(str(self.outstream_class)) sys.stdout = outstream_factory(self.session, self.iopub_thread, u'stdout') sys.stderr = outstream_factory(self.session, self.iopub_thread, u'stderr') if self.displayhook_class: displayhook_factory = import_item(str(self.displayhook_class)) self.displayhook = displayhook_factory(self.session, self.iopub_socket) sys.displayhook = self.displayhook self.patch_io() def patch_io(self): """Patch important libraries that can't handle sys.stdout forwarding""" try: import faulthandler except ImportError: pass else: # Warning: this is a monkeypatch of `faulthandler.enable`, watch for possible # updates to the upstream API and update accordingly (up-to-date as of Python 3.5): # https://docs.python.org/3/library/faulthandler.html#faulthandler.enable # change default file to __stderr__ from forwarded stderr faulthandler_enable = faulthandler.enable def enable(file=sys.__stderr__, all_threads=True, **kwargs): return faulthandler_enable(file=file, all_threads=all_threads, **kwargs) faulthandler.enable = enable if hasattr(faulthandler, 'register'): faulthandler_register = faulthandler.register def register(signum, file=sys.__stderr__, all_threads=True, chain=False, **kwargs): return faulthandler_register(signum, file=file, all_threads=all_threads, chain=chain, **kwargs) faulthandler.register = register def init_signal(self): signal.signal(signal.SIGINT, signal.SIG_IGN) def init_kernel(self): """Create the Kernel object itself""" shell_stream = ZMQStream(self.shell_socket) control_stream = ZMQStream(self.control_socket) kernel_factory = self.kernel_class.instance kernel = kernel_factory(parent=self, session=self.session, shell_streams=[shell_stream, control_stream], iopub_thread=self.iopub_thread, iopub_socket=self.iopub_socket, stdin_socket=self.stdin_socket, log=self.log, profile_dir=self.profile_dir, user_ns=self.user_ns, ) kernel.record_ports({ name + '_port': port for name, port in self.ports.items() }) self.kernel = kernel # Allow the displayhook to get the execution count self.displayhook.get_execution_count = lambda: kernel.execution_count def init_gui_pylab(self): """Enable GUI event loop integration, taking pylab into account.""" # Register inline backend as default # this is higher priority than matplotlibrc, # but lower priority than anything else (mpl.use() for instance). # This only affects matplotlib >= 1.5 if not os.environ.get('MPLBACKEND'): os.environ['MPLBACKEND'] = 'module://ipykernel.pylab.backend_inline' # Provide a wrapper for :meth:`InteractiveShellApp.init_gui_pylab` # to ensure that any exception is printed straight to stderr. # Normally _showtraceback associates the reply with an execution, # which means frontends will never draw it, as this exception # is not associated with any execute request. shell = self.shell _showtraceback = shell._showtraceback try: # replace error-sending traceback with stderr def print_tb(etype, evalue, stb): print ("GUI event loop or pylab initialization failed", file=sys.stderr) print (shell.InteractiveTB.stb2text(stb), file=sys.stderr) shell._showtraceback = print_tb InteractiveShellApp.init_gui_pylab(self) finally: shell._showtraceback = _showtraceback def init_shell(self): self.shell = getattr(self.kernel, 'shell', None) if self.shell: self.shell.configurables.append(self) def init_extensions(self): super(IPKernelApp, self).init_extensions() # BEGIN HARDCODED WIDGETS HACK # Ensure ipywidgets extension is loaded if available extension_man = self.shell.extension_manager if 'ipywidgets' not in extension_man.loaded: try: extension_man.load_extension('ipywidgets') except ImportError as e: self.log.debug('ipywidgets package not installed. Widgets will not be available.') # END HARDCODED WIDGETS HACK def configure_tornado_logger(self): """ Configure the tornado logging.Logger. Must set up the tornado logger or else tornado will call basicConfig for the root logger which makes the root logger go to the real sys.stderr instead of the capture streams. This function mimics the setup of logging.basicConfig. """ logger = logging.getLogger('tornado') handler = logging.StreamHandler() formatter = logging.Formatter(logging.BASIC_FORMAT) handler.setFormatter(formatter) logger.addHandler(handler) @catch_config_error def initialize(self, argv=None): super(IPKernelApp, self).initialize(argv) if self.subapp is not None: return # register zmq IOLoop with tornado zmq_ioloop.install() self.init_blackhole() self.init_connection_file() self.init_poller() self.init_sockets() self.init_heartbeat() # writing/displaying connection info must be *after* init_sockets/heartbeat self.write_connection_file() # Log connection info after writing connection file, so that the connection # file is definitely available at the time someone reads the log. self.log_connection_info() self.init_io() self.init_signal() self.init_kernel() # shell init steps self.init_path() self.init_shell() if self.shell: self.init_gui_pylab() self.init_extensions() self.init_code() # flush stdout/stderr, so that anything written to these streams during # initialization do not get associated with the first execution request sys.stdout.flush() sys.stderr.flush() def start(self): if self.subapp is not None: return self.subapp.start() if self.poller is not None: self.poller.start() self.kernel.start() try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass launch_new_instance = IPKernelApp.launch_instance def main(): """Run an IPKernel as an application""" app = IPKernelApp.instance() app.initialize() app.start() if __name__ == '__main__': main()
apache-2.0
tortugueta/multilayers
examples/radcenter_distribution.py
1
8087
# -*- coding: utf-8 -*- """ Name : radcenter_distribution Author : Joan Juvert <trust.no.one.51@gmail.com> Version : 1.0 Description : This script calculates the influence of the distribution of : radiative centers in the active layer on the observed : spectrum. Copyright 2012 Joan Juvert 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/>. """ import multilayers as ml import numpy as np import bphysics as bp import scipy.integrate as integ import argparse as ap import sys import pdb # Argument parsing parser = ap.ArgumentParser( description = "This script calculates the effect of the " + \ "distribution of radiative centers in the active layer on " + \ "the modificator to the spectrum. The observation angle is " + \ "a fixed parameter. Optionally, the output can be plotted " + \ "and output to the standard output or to a file. The matrix " + \ "containing the values of F(z, lambda) can be saved to a file " + \ "and recovered in a following run of the program to avoid " + \ "recalculating it in case we want to calculate the effect of " + \ "different distributions on the same system.") parser.add_argument( "--graph", help = "Plot the results", action = "store_true") parser.add_argument( "-o", "--output", help = "Dump the results to a file") parser.add_argument( "-s", "--savematrix", help = "Save the matrix with the F(z, lambda) values to a file") parser.add_argument( "-l", "--loadmatrix", help = "Load the matrix with the F(z, lambda) values from a file") args = parser.parse_args() # Load the depth distribution of radiative centers. Note that the origin # and units of z must be the same as in the multilayer.The distribution # should be normalized to 1. print("Loading the distribution...") path = "/home/joan/Dropbox/CNM/projectes/simulations_report/figures/" + \ "rcdistributions/" distribution = bp.rdfile(path + "gaussian_m25_s07.dat", usecols = [0, 1])[1] print("Done") print("Checking the distribution...") integral = integ.simps(distribution[:, 1], distribution[:, 0], 0) np.testing.assert_almost_equal(integral, 1, 2) print("Done") # If we load the values of F(z, lambda) calculated in a previous # execution we do not need to build the multilayer and repeat the # calculation of the F function. Notice that the values of z at which # the new distribution is sampled should be the same as the previous # one. if args.loadmatrix: print("Loading matrix...") fmatrix = np.load(args.loadmatrix) zlist = fmatrix['zlist'] np.testing.assert_array_equal(zlist, distribution[:, 0]) wlist = fmatrix['wlist'] angle = fmatrix['angle'] fte = fmatrix['fte'] ftm = fmatrix['ftm'] print("Done") else: # Create the materials print("Loading materials... ") silicon = ml.Medium("silicon.dat") air = ml.Medium("air.dat") sio2 = ml.Medium("sio2.dat") poly = ml.Medium("polysilicon.dat") print("Done") # Set the fixed parameters. angle = np.deg2rad(0) # Create the multilayer print("Building multilayer and allocating memory... ") thicknesses = [300, 50] multilayer = ml.Multilayer([ air, [poly, thicknesses[0]], [sio2, thicknesses[1]], silicon]) # Define the wavelengths and z coordinates at which F will be calculated # and allocate memory for the results. We will use a structured array to # store the values of F(z, lambda). wstep = 1 wmin = multilayer.getMinMaxWlength()[0] wmax = multilayer.getMinMaxWlength()[1] wlist = np.arange(wmin, wmax, wstep) zlist = distribution[:, 0] ftype = np.dtype([ ('fx', np.complex128), ('fy', np.complex128), ('fz', np.complex128)]) resmatrix = np.empty((zlist.size, wlist.size), dtype = ftype) print("Done") # I(wavelength, theta) = s(wavelength) * F'(wavelength, theta), where # F'(wav, theta) = integral[z](|F|^2 * rcdist(z). Therefore, we # calculate the new spectrum as a modification to the original spectrum. # The modification factor F'(wav, theta) is an integral over z. # First calculate |Fy|^2 for te and |Fx*cos^2 + Fz*sin^2|^2 for tm. We # do fx and fz in one loop and fy in another independent loop to avoid # recalculating the characteristic matrix at every iteration due to the # change of polarization. print("Calculating F...") for (widx, wlength) in enumerate(wlist): percent = (float(widx) / wlist.size) * 100 print("%.2f%%" % percent) for (zidx, z) in enumerate(zlist): resmatrix[zidx][widx]['fx'] = multilayer.calculateFx(z, wlength, angle) resmatrix[zidx][widx]['fz'] = multilayer.calculateFz(z, wlength, angle) for (zidx, z) in enumerate(zlist): resmatrix[zidx][widx]['fy'] = multilayer.calculateFy(z, wlength, angle) # We are probably more interesed on the effect of the multilayer on the # energy rather than the electric field. What we want is |Fy(z)|^2 for # TE waves and |Fx(z) cosA^2 + Fz(z) sinA^2|^2 for TM waves. ftm = np.absolute( resmatrix['fx'] * np.cos(angle) ** 2 + \ resmatrix['fz'] * np.sin(angle) ** 2) ** 2 fte = np.absolute(resmatrix['fy']) ** 2 print("Done") # Notice that until now we have not used the distribution of the # radiative ceneters, but the calculation of ftm and fte is costly. # If requested, we can save fte and ftm to a file. In a following # execution of the script, the matrix can be loaded from the file # instead of recalculated. if args.savematrix: print("Saving matrix...") np.savez(args.savematrix, fte = fte, ftm = ftm, zlist = zlist, wlist = wlist, angle = angle) print("Done") # Build or load the original spectrum. It should be sampled at the same # wavelengths defined in wlist. If we are interested only in the # modificator to the spectrum, not in the modified spectrum, we can # leave it at 1. original_spec = 1 # Multiply each F(z, lambda) by the distribution. print("Integrating...") distval = distribution[:, 1].reshape(distribution[:, 1].size, 1) fte_mplied = fte * distval ftm_mplied = ftm * distval fte_int = integ.simps(fte_mplied, zlist, axis = 0) ftm_int = integ.simps(ftm_mplied, zlist, axis = 0) spectrum_modte = original_spec * fte_int spectrum_modtm = original_spec * ftm_int print("Done") # Dump data to file or stdout comments = "# F_TE = |Fy^2|^2\n" + \ "# F_TM = |Fx * cosA^2 + Fz * sinA^2|^2\n" + \ "# Modified spectrum for TE and TM waves for a\n" + \ "# distributions of the radiative centers.\n" + \ "# wlength\tF_TE\tF_TM" if args.output: bp.wdfile(args.output, comments, np.array([wlist, spectrum_modte, spectrum_modtm]).T, '%.6e') else: print(comments) for i in xrange(wlist.size): print("%.6e\t%.6e\t%.6e" % (wlist[i], spectrum_modte[i], spectrum_modtm[i])) # Plot data if requested if args.graph: import matplotlib.pyplot as plt plt.plot(wlist, spectrum_modte, label='TE', color = 'r') plt.plot(wlist, spectrum_modtm, label='TM', color = 'b') plt.xlabel('Wavelength (nm)') plt.ylabel('Energy ratio') plt.grid() plt.legend(loc=2) plt.title('%.1f rad' % angle) plt.show() plt.close()
gpl-3.0
dpshelio/sunpy
examples/units_and_coordinates/planet_locations.py
1
1252
""" =================================== Getting the location of the planets =================================== How to get the position of planetary bodies im the solar system using `astropy's solar system ephemeris <http://docs.astropy.org/en/stable/coordinates/solarsystem.html#solar-system-ephemerides>`__ information and SunPy. """ import matplotlib.pyplot as plt from astropy.time import Time from sunpy.coordinates import get_body_heliographic_stonyhurst ############################################################################## # Lets grab the positions of each of the planets in Heliographic Stonyhurst # coordinates. obstime = Time('2014-05-15T07:54:00.005') planet_list = ['earth', 'venus', 'mars', 'mercury', 'jupiter', 'neptune', 'uranus', 'sun'] planet_coord = [get_body_heliographic_stonyhurst(this_planet, time=obstime) for this_planet in planet_list] ############################################################################## # Let's plot the results. Remember the Sun is at the center of this coordinate # system. ax = plt.subplot(projection='polar') for this_planet, this_coord in zip(planet_list, planet_coord): plt.polar(this_coord.lon.to('rad'), this_coord.radius, 'o', label=this_planet) plt.legend() plt.show()
bsd-2-clause
kgullikson88/TS23-Scripts
CheckSyntheticTemperature.py
1
14868
import os import re from collections import defaultdict from operator import itemgetter import logging import pandas from scipy.interpolate import InterpolatedUnivariateSpline as spline from george import kernels import matplotlib.pyplot as plt import numpy as np import george import emcee import StarData import SpectralTypeRelations def classify_filename(fname, type='bright'): """ Given a CCF filename, it classifies the star combination, temperature, metallicity, and vsini :param fname: :return: """ # First, remove any leading directories fname = fname.split('/')[-1] # Star combination m1 = re.search('\.[0-9]+kps', fname) stars = fname[:m1.start()] star1 = stars.split('+')[0].replace('_', ' ') star2 = stars.split('+')[1].split('_{}'.format(type))[0].replace('_', ' ') # secondary star vsini vsini = float(fname[m1.start() + 1:].split('kps')[0]) # Temperature m2 = re.search('[0-9]+\.0K', fname) temp = float(m2.group()[:-1]) # logg m3 = re.search('K\+[0-9]\.[0-9]', fname) logg = float(m3.group()[1:]) # metallicity metal = float(fname.split(str(logg))[-1]) return star1, star2, vsini, temp, logg, metal def get_ccf_data(basedir, primary_name=None, secondary_name=None, vel_arr=np.arange(-900.0, 900.0, 0.1), type='bright'): """ Searches the given directory for CCF files, and classifies by star, temperature, metallicity, and vsini :param basedir: The directory to search for CCF files :keyword primary_name: Optional keyword. If given, it will only get the requested primary star data :keyword secondary_name: Same as primary_name, but only reads ccfs for the given secondary :keyword vel_arr: The velocities to interpolate each ccf at :return: pandas DataFrame """ if not basedir.endswith('/'): basedir += '/' all_files = ['{}{}'.format(basedir, f) for f in os.listdir(basedir) if type in f.lower()] primary = [] secondary = [] vsini_values = [] temperature = [] gravity = [] metallicity = [] ccf = [] for fname in all_files: star1, star2, vsini, temp, logg, metal = classify_filename(fname, type=type) if primary_name is not None and star1.lower() != primary_name.lower(): continue if secondary_name is not None and star2.lower() != secondary_name.lower(): continue vel, corr = np.loadtxt(fname, unpack=True) fcn = spline(vel, corr) ccf.append(fcn(vel_arr)) primary.append(star1) secondary.append(star2) vsini_values.append(vsini) temperature.append(temp) gravity.append(logg) metallicity.append(metal) # Make a pandas dataframe with all this data df = pandas.DataFrame(data={'Primary': primary, 'Secondary': secondary, 'Temperature': temperature, 'vsini': vsini_values, 'logg': gravity, '[Fe/H]': metallicity, 'CCF': ccf}) return df def get_ccf_summary(basedir, vel_arr=np.arange(-900.0, 900.0, 0.1), velocity='highest', type='bright'): """ Very similar to get_ccf_data, but does it in a way that is more memory efficient :param basedir: The directory to search for CCF files :keyword velocity: The velocity to measure the CCF at. The default is 'highest', and uses the maximum of the ccf :keyword vel_arr: The velocities to interpolate each ccf at :return: pandas DataFrame """ if not basedir.endswith('/'): basedir += '/' all_files = ['{}{}'.format(basedir, f) for f in os.listdir(basedir) if type in f.lower()] file_dict = defaultdict(lambda: defaultdict(list)) for fname in all_files: star1, star2, vsini, temp, logg, metal = classify_filename(fname, type=type) file_dict[star1][star2].append(fname) # Now, read the ccfs for each primary/secondary combo, and find the best combination summary_dfs = [] for primary in file_dict.keys(): for secondary in file_dict[primary].keys(): data = get_ccf_data(basedir, primary_name=primary, secondary_name=secondary, vel_arr=vel_arr, type=type) summary_dfs.append(find_best_pars(data, velocity=velocity, vel_arr=vel_arr)) return pandas.concat(summary_dfs, ignore_index=True) def find_best_pars(df, velocity='highest', vel_arr=np.arange(-900.0, 900.0, 0.1)): """ Find the 'best-fit' parameters for each combination of primary and secondary star :param df: the dataframe to search in :keyword velocity: The velocity to measure the CCF at. The default is 'highest', and uses the maximum of the ccf :keyword vel_arr: The velocities to interpolate each ccf at :return: a dataframe with keys of primary, secondary, and the parameters """ # Get the names of the primary and secondary stars primary_names = pandas.unique(df.Primary) secondary_names = pandas.unique(df.Secondary) # Find the ccf value at the given velocity if velocity == 'highest': fcn = lambda row: (np.max(row), vel_arr[np.argmax(row)]) vals = df['CCF'].map(fcn) df['ccf_max'] = vals.map(lambda l: l[0]) df['rv'] = vals.map(lambda l: l[1]) # df['ccf_max'] = df['CCF'].map(np.max) else: df['ccf_max'] = df['CCF'].map(lambda arr: arr[np.argmin(np.abs(vel_arr - velocity))]) # Find the best parameter for each combination d = defaultdict(list) for primary in primary_names: for secondary in secondary_names: good = df.loc[(df.Primary == primary) & (df.Secondary == secondary)] best = good.loc[good.ccf_max == good.ccf_max.max()] d['Primary'].append(primary) d['Secondary'].append(secondary) d['Temperature'].append(best['Temperature'].item()) d['vsini'].append(best['vsini'].item()) d['logg'].append(best['logg'].item()) d['[Fe/H]'].append(best['[Fe/H]'].item()) d['rv'].append(best['rv'].item()) return pandas.DataFrame(data=d) def get_detected_objects(df, tol=1.0): """ Takes a summary dataframe with RV information. Finds the median rv for each star, and removes objects that are 'tol' km/s from the median value :param df: A summary dataframe, such as created by find_best_pars :param tol: The tolerance, in km/s, to accept an observation as detected :return: a dataframe containing only detected companions """ secondary_names = pandas.unique(df.Secondary) secondary_to_rv = defaultdict(float) for secondary in secondary_names: rv = df.loc[df.Secondary == secondary]['rv'].median() secondary_to_rv[secondary] = rv print secondary, rv keys = df.Secondary.values good = df.loc[abs(df.rv.values - np.array(itemgetter(*keys)(secondary_to_rv))) < tol] return good def add_actual_temperature(df, method='spt'): """ Add the actual temperature to a given summary dataframe :param df: The dataframe to which we will add the actual secondary star temperature :param method: How to get the actual temperature. Options are: - 'spt': Use main-sequence relationships to go from spectral type --> temperature - 'excel': Use tabulated data, available in the file 'SecondaryStar_Temperatures.xls' :return: copy of the original dataframe, with an extra column for the secondary star temperature """ # First, get a list of the secondary stars in the data secondary_names = pandas.unique(df.Secondary) secondary_to_temperature = defaultdict(float) secondary_to_error = defaultdict(float) if method.lower() == 'spt': MS = SpectralTypeRelations.MainSequence() for secondary in secondary_names: star_data = StarData.GetData(secondary) spt = star_data.spectype[0] + re.search('[0-9]\.*[0-9]*', star_data.spectype).group() T_sec = MS.Interpolate(MS.Temperature, spt) secondary_to_temperature[secondary] = T_sec elif method.lower() == 'excel': table = pandas.read_excel('SecondaryStar_Temperatures.xls', 0) for secondary in secondary_names: T_sec = table.loc[table.Star.str.lower().str.contains(secondary.strip().lower())]['Literature_Temp'].item() T_error = table.loc[table.Star.str.lower().str.contains(secondary.strip().lower())][ 'Literature_error'].item() secondary_to_temperature[secondary] = T_sec secondary_to_error[secondary] = T_error df['Tactual'] = df['Secondary'].map(lambda s: secondary_to_temperature[s]) df['Tact_err'] = df['Secondary'].map(lambda s: secondary_to_error[s]) return def make_gaussian_process_samples(df): """ Make a gaussian process fitting the Tactual-Tmeasured relationship :param df: pandas DataFrame with columns 'Temperature' (with the measured temperature) and 'Tactual' (for the actual temperature) :return: emcee sampler instance """ # First, find the uncertainties at each actual temperature # Tactual = df['Tactual'].values #Tmeasured = df['Temperature'].values #error = df['Tact_err'].values temp = df.groupby('Temperature').mean()['Tactual'] Tmeasured = temp.keys().values Tactual = temp.values error = np.nan_to_num(df.groupby('Temperature').std(ddof=1)['Tactual'].values) default = np.median(error[error > 1]) error = np.maximum(error, np.ones(error.size) * default) for Tm, Ta, e in zip(Tmeasured, Tactual, error): print Tm, Ta, e plt.figure(1) plt.errorbar(Tmeasured, Tactual, yerr=error, fmt='.k', capsize=0) plt.plot(Tmeasured, Tmeasured, 'r--') plt.xlim((min(Tmeasured) - 100, max(Tmeasured) + 100)) plt.xlabel('Measured Temperature') plt.ylabel('Actual Temperature') plt.show(block=False) # Define some functions to use in the GP fit def model(pars, T): #polypars = pars[2:] #return np.poly1d(polypars)(T) return T def lnlike(pars, Tact, Tmeas, Terr): a, tau = np.exp(pars[:2]) gp = george.GP(a * kernels.ExpSquaredKernel(tau)) gp.compute(Tmeas, Terr) return gp.lnlikelihood(Tact - model(pars, Tmeas)) def lnprior(pars): lna, lntau = pars[:2] polypars = pars[2:] if -20 < lna < 20 and 4 < lntau < 20: return 0.0 return -np.inf def lnprob(pars, x, y, yerr): lp = lnprior(pars) return lp + lnlike(pars, x, y, yerr) if np.isfinite(lp) else -np.inf # Set up the emcee fitter initial = np.array([0, 6])#, 1.0, 0.0]) ndim = len(initial) nwalkers = 100 p0 = [np.array(initial) + 1e-8 * np.random.randn(ndim) for i in xrange(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(Tactual, Tmeasured, error)) print 'Running first burn-in' p1, lnp, _ = sampler.run_mcmc(p0, 500) sampler.reset() print "Running second burn-in..." p_best = p1[np.argmax(lnp)] p2 = [p_best + 1e-8 * np.random.randn(ndim) for i in xrange(nwalkers)] p3, _, _ = sampler.run_mcmc(p2, 250) sampler.reset() print "Running production..." sampler.run_mcmc(p3, 1000) # Plot a bunch of the fits print "Plotting..." N = 100 Tvalues = np.arange(3300, 7000, 20) idx = np.argsort(-sampler.lnprobability.flatten())[:N] # Get N 'best' curves par_vals = sampler.flatchain[idx] for i, pars in enumerate(par_vals): a, tau = np.exp(pars[:2]) gp = george.GP(a * kernels.ExpSquaredKernel(tau)) gp.compute(Tmeasured, error) s = gp.sample_conditional(Tactual - model(pars, Tmeasured), Tvalues) + model(pars, Tvalues) plt.plot(Tvalues, s, 'b-', alpha=0.1) plt.draw() # Finally, get posterior samples at all the possibly measured temperatures print 'Generating posterior samples at all temperatures...' N = 10000 # This is 1/10th of the total number of samples! idx = np.argsort(-sampler.lnprobability.flatten())[:N] # Get N 'best' curves par_vals = sampler.flatchain[idx] Tvalues = np.arange(3000, 6900, 100) gp_posterior = [] for pars in par_vals: a, tau = np.exp(pars[:2]) gp = george.GP(a * kernels.ExpSquaredKernel(tau)) gp.compute(Tmeasured, error) s = gp.sample_conditional(Tactual - model(pars, Tmeasured), Tvalues) + model(pars, Tvalues) gp_posterior.append(s) # Finally, make confidence intervals for the actual temperatures gp_posterior = np.array(gp_posterior) l, m, h = np.percentile(gp_posterior, [16.0, 50.0, 84.0], axis=0) conf = pandas.DataFrame(data={'Measured Temperature': Tvalues, 'Actual Temperature': m, 'Lower Bound': l, 'Upper bound': h}) conf.to_csv('Confidence_Intervals.csv', index=False) return sampler, np.array(gp_posterior) def check_posterior(df, posterior, Tvalues): """ Checks the posterior samples: Are 95% of the measurements within 2-sigma of the prediction? :param df: The summary dataframe :param posterior: The MCMC predicted values :param Tvalues: The measured temperatures the posterior was made with :return: boolean, as well as some warning messages if applicable """ # First, make 2-sigma confidence intervals l, m, h = np.percentile(posterior, [5.0, 50.0, 95.0], axis=0) # Save the confidence intervals # conf = pandas.DataFrame(data={'Measured Temperature': Tvalues, 'Actual Temperature': m, # 'Lower Bound': l, 'Upper bound': h}) #conf.to_csv('Confidence_Intervals.csv', index=False) Ntot = [] # The total number of observations with the given measured temperature Nacc = [] # The number that have actual temperatures within the confidence interval g = df.groupby('Temperature') for i, T in enumerate(Tvalues): if T in g.groups.keys(): Ta = g.get_group(T)['Tactual'] low, high = l[i], h[i] Ntot.append(len(Ta)) Nacc.append(len(Ta.loc[(Ta >= low) & (Ta <= high)])) p = float(Nacc[-1]) / float(Ntot[-1]) if p < 0.95: logging.warn( 'Only {}/{} of the samples ({:.2f}%) were accepted for T = {} K'.format(Nacc[-1], Ntot[-1], p * 100, T)) print low, high print sorted(Ta) else: Ntot.append(0) Nacc.append(0) p = float(sum(Nacc)) / float(sum(Ntot)) if p < 0.95: logging.warn('Only {:.2f}% of the total samples were accepted!'.format(p * 100)) return False return True if __name__ == '__main__': pass
gpl-3.0
codester2/devide.johannes
install_packages/ip_matplotlib.py
5
5932
# Copyright (c) Charl P. Botha, TU Delft. # All rights reserved. # See COPYRIGHT for details. import config from install_package import InstallPackage import os import shutil import sys import utils from distutils import sysconfig MPL_VER = "1.1.0" if os.name == 'posix': MPL_ARCHIVE = "matplotlib-%s.tar.gz" % (MPL_VER,) MPL_URL = "http://surfnet.dl.sourceforge.net/sourceforge/matplotlib/%s" % \ (MPL_ARCHIVE,) elif os.name == 'nt': if config.WINARCH_STR == 'x64': WINTHINGY = 'win-amd64' else: WINTHINGY = 'win32' MPL_ARCHIVE = "matplotlib-%s.%s-py2.7.exe" % (MPL_VER, WINTHINGY) MPL_URL = "http://graphics.tudelft.nl/~cpbotha/files/devide/johannes_support/gohlke/%s" % (MPL_ARCHIVE,) MPL_DIRBASE = "matplotlib-%s" % (MPL_VER,) # I prefer that this be built with numpy, but it is not a dependency # per se dependencies = [] class matplotlib(InstallPackage): def __init__(self): self.tbfilename = os.path.join(config.archive_dir, MPL_ARCHIVE) self.build_dir = os.path.join(config.build_dir, MPL_DIRBASE) self.inst_dir = os.path.join(config.inst_dir, 'matplotlib') def get(self): if os.path.exists(self.tbfilename): utils.output("%s already present, not downloading." % (MPL_ARCHIVE,)) else: utils.goto_archive() utils.urlget(MPL_URL) def unpack(self): if os.path.isdir(self.build_dir): utils.output("MATPLOTLIB source already unpacked, not redoing.") else: if os.name == 'posix': utils.output("Unpacking MATPLOTLIB source.") utils.unpack_build(self.tbfilename) else: utils.output("Unpacking MATPLOTLIB binaries.") os.mkdir(self.build_dir) os.chdir(self.build_dir) utils.unpack(self.tbfilename) def configure(self): if os.name == 'nt': utils.output("Skipping configure (WINDOWS).") return # pre-configure setup.py and setupext.py so that everything is # found and configured as we want it. os.chdir(self.build_dir) if os.path.exists('setup.py.new'): utils.output('matplotlib already configured. Skipping step.') else: # pre-filter setup.py repls = [("(BUILD_GTKAGG\s*=\s*).*", "\\1 0"), ("(BUILD_GTK\s*=\s*).*", "\\1 0"), ("(BUILD_TKAGG\s*=\s*).*", "\\1 0"), ("(BUILD_WXAGG\s*=\s*).*", "\\1 1"), ("(rc\s*=\s*dict\().*", "\\1 {'backend':'PS', 'numerix':'numpy'} )")] utils.re_sub_filter_file(repls, 'setup.py') def build(self): if os.name == 'nt': utils.output("Skipping build (WINDOWS).") return os.chdir(self.build_dir) # weak test... there are .so files deeper, but they're in platform # specific directories if os.path.exists('build'): utils.output('matplotlib already built. Skipping step.') else: # add wx bin to path so that wx-config can be found os.environ['PATH'] = "%s%s%s" % (config.WX_BIN_PATH, os.pathsep, os.environ['PATH']) ret = os.system('%s setup.py build' % (sys.executable,)) if ret != 0: utils.error('matplotlib build failed. Please fix and try again.') def install(self): # to test for install, just do python -c "import matplotlib" # and test the result (we could just import directly, but that would # only work once our invoking python has been stopped and started # again) os.chdir(config.archive_dir) # we need to be elsewhere! ret = os.system('%s -c "import matplotlib"' % (sys.executable,)) if ret == 0: utils.output('matplotlib already installed. Skipping step.') else: utils.output('ImportError test shows that matplotlib is not ' 'installed. Installing...') if os.name == 'nt': self.install_nt() else: self.install_posix() # make sure the backend is set to WXAgg # and that interactive is set to True rcfn = os.path.join( config.PYTHON_SITE_PACKAGES, 'matplotlib', 'mpl-data', 'matplotlibrc') utils.re_sub_filter_file( [("(\s*backend\s*\:).*", "\\1 WXAgg"), ("#*(\s*interactive\s:).*","\\1 True")], rcfn) def install_nt(self): sp_dir = sysconfig.get_python_lib() utils.copy_glob(os.path.join(self.build_dir, 'PLATLIB', '*'), sp_dir) def install_posix(self): os.chdir(self.build_dir) # add wx bin to path so that wx-config can be found os.environ['PATH'] = "%s%s%s" % (config.WX_BIN_PATH, os.pathsep, os.environ['PATH']) ret = os.system('%s setup.py install' % (sys.executable,)) if ret != 0: utils.error( 'matplotlib install failed. Please fix and try again.') def clean_build(self): utils.output("Removing build and install directories.") if os.path.exists(self.build_dir): shutil.rmtree(self.build_dir) from distutils import sysconfig matplotlib_instdir = os.path.join(sysconfig.get_python_lib(), 'matplotlib') if os.path.exists(matplotlib_instdir): shutil.rmtree(matplotlib_instdir) def get_installed_version(self): import matplotlib return matplotlib.__version__
bsd-3-clause
vshtanko/scikit-learn
examples/neural_networks/plot_rbm_logistic_classification.py
258
4609
""" ============================================================== Restricted Boltzmann Machine features for digit classification ============================================================== For greyscale image data where pixel values can be interpreted as degrees of blackness on a white background, like handwritten digit recognition, the Bernoulli Restricted Boltzmann machine model (:class:`BernoulliRBM <sklearn.neural_network.BernoulliRBM>`) can perform effective non-linear feature extraction. In order to learn good latent representations from a small dataset, we artificially generate more labeled data by perturbing the training data with linear shifts of 1 pixel in each direction. This example shows how to build a classification pipeline with a BernoulliRBM feature extractor and a :class:`LogisticRegression <sklearn.linear_model.LogisticRegression>` classifier. The hyperparameters of the entire model (learning rate, hidden layer size, regularization) were optimized by grid search, but the search is not reproduced here because of runtime constraints. Logistic regression on raw pixel values is presented for comparison. The example shows that the features extracted by the BernoulliRBM help improve the classification accuracy. """ from __future__ import print_function print(__doc__) # Authors: Yann N. Dauphin, Vlad Niculae, Gabriel Synnaeve # License: BSD import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import convolve from sklearn import linear_model, datasets, metrics from sklearn.cross_validation import train_test_split from sklearn.neural_network import BernoulliRBM from sklearn.pipeline import Pipeline ############################################################################### # Setting up def nudge_dataset(X, Y): """ This produces a dataset 5 times bigger than the original one, by moving the 8x8 images in X around by 1px to left, right, down, up """ direction_vectors = [ [[0, 1, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 1, 0]]] shift = lambda x, w: convolve(x.reshape((8, 8)), mode='constant', weights=w).ravel() X = np.concatenate([X] + [np.apply_along_axis(shift, 1, X, vector) for vector in direction_vectors]) Y = np.concatenate([Y for _ in range(5)], axis=0) return X, Y # Load Data digits = datasets.load_digits() X = np.asarray(digits.data, 'float32') X, Y = nudge_dataset(X, digits.target) X = (X - np.min(X, 0)) / (np.max(X, 0) + 0.0001) # 0-1 scaling X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0) # Models we will use logistic = linear_model.LogisticRegression() rbm = BernoulliRBM(random_state=0, verbose=True) classifier = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)]) ############################################################################### # Training # Hyper-parameters. These were set by cross-validation, # using a GridSearchCV. Here we are not performing cross-validation to # save time. rbm.learning_rate = 0.06 rbm.n_iter = 20 # More components tend to give better prediction performance, but larger # fitting time rbm.n_components = 100 logistic.C = 6000.0 # Training RBM-Logistic Pipeline classifier.fit(X_train, Y_train) # Training Logistic regression logistic_classifier = linear_model.LogisticRegression(C=100.0) logistic_classifier.fit(X_train, Y_train) ############################################################################### # Evaluation print() print("Logistic regression using RBM features:\n%s\n" % ( metrics.classification_report( Y_test, classifier.predict(X_test)))) print("Logistic regression using raw pixel features:\n%s\n" % ( metrics.classification_report( Y_test, logistic_classifier.predict(X_test)))) ############################################################################### # Plotting plt.figure(figsize=(4.2, 4)) for i, comp in enumerate(rbm.components_): plt.subplot(10, 10, i + 1) plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.suptitle('100 components extracted by RBM', fontsize=16) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) plt.show()
bsd-3-clause
matousc89/padasip
padasip/filters/nlmf.py
1
5444
""" .. versionadded:: 1.1.0 The least-mean-fourth (LMF) adaptive filter implemented according to the paper :cite:`zerguine2000convergence`. The NLMF is an extension of the LMF adaptive filter (:ref:`filter-lmf`). The NLMF filter can be created as follows >>> import padasip as pa >>> pa.filters.FilterNLMF(n) where `n` is the size (number of taps) of the filter. Content of this page: .. contents:: :local: :depth: 1 .. seealso:: :ref:`filters` Algorithm Explanation ====================================== The NLMF is extension of LMF filter. See :ref:`filter-lmf` for explanation of the algorithm behind. The extension is based on normalization of learning rate. The learning rage :math:`\mu` is replaced by learning rate :math:`\eta(k)` normalized with every new sample according to input power as follows :math:`\eta (k) = \\frac{\mu}{\epsilon + || \\textbf{x}(k) ||^2}`, where :math:`|| \\textbf{x}(k) ||^2` is norm of input vector and :math:`\epsilon` is a small positive constant (regularization term). This constant is introduced to preserve the stability in cases where the input is close to zero. Minimal Working Examples ====================================== If you have measured data you may filter it as follows .. code-block:: python import numpy as np import matplotlib.pylab as plt import padasip as pa # creation of data N = 500 x = np.random.normal(0, 1, (N, 4)) # input matrix v = np.random.normal(0, 0.1, N) # noise d = 2*x[:,0] + 0.1*x[:,1] - 0.3*x[:,2] + 0.5*x[:,3] + v # target # identification f = pa.filters.FilterNLMF(n=4, mu=0.005, w="random") y, e, w = f.run(d, x) # show results plt.figure(figsize=(15,9)) plt.subplot(211);plt.title("Adaptation");plt.xlabel("samples - k") plt.plot(d,"b", label="d - target") plt.plot(y,"g", label="y - output");plt.legend() plt.subplot(212);plt.title("Filter error");plt.xlabel("samples - k") plt.plot(10*np.log10(e**2),"r", label="e - error [dB]");plt.legend() plt.tight_layout() plt.show() References ====================================== .. bibliography:: lmf.bib :style: plain Code Explanation ====================================== """ import numpy as np from padasip.filters.base_filter import AdaptiveFilter class FilterNLMF(AdaptiveFilter): """ Adaptive NLMF filter. **Args:** * `n` : length of filter (integer) - how many input is input array (row of input matrix) **Kwargs:** * `mu` : learning rate (float). Also known as step size. If it is too slow, the filter may have bad performance. If it is too high, the filter will be unstable. The default value can be unstable for ill-conditioned input data. * `eps` : regularization term (float). It is introduced to preserve stability for close-to-zero input vectors * `w` : initial weights of filter. Possible values are: * array with initial weights (1 dimensional array) of filter size * "random" : create random weights * "zeros" : create zero value weights """ def __init__(self, n, mu=0.1, eps=1., w="random"): self.kind = "NLMF filter" if type(n) == int: self.n = n else: raise ValueError('The size of filter must be an integer') self.mu = self.check_float_param(mu, 0, 1000, "mu") self.eps = self.check_float_param(eps, 0, 1000, "eps") self.init_weights(w, self.n) self.w_history = False def adapt(self, d, x): """ Adapt weights according one desired value and its input. **Args:** * `d` : desired value (float) * `x` : input array (1-dimensional array) """ y = np.dot(self.w, x) e = d - y nu = self.mu / (self.eps + np.dot(x, x)) self.w += nu * x * e**3 def run(self, d, x): """ This function filters multiple samples in a row. **Args:** * `d` : desired value (1 dimensional array) * `x` : input matrix (2-dimensional array). Rows are samples, columns are input arrays. **Returns:** * `y` : output value (1 dimensional array). The size corresponds with the desired value. * `e` : filter error for every sample (1 dimensional array). The size corresponds with the desired value. * `w` : history of all weights (2 dimensional array). Every row is set of the weights for given sample. """ # measure the data and check if the dimmension agree N = len(x) if not len(d) == N: raise ValueError('The length of vector d and matrix x must agree.') self.n = len(x[0]) # prepare data try: x = np.array(x) d = np.array(d) except: raise ValueError('Impossible to convert x or d to a numpy array') # create empty arrays y = np.zeros(N) e = np.zeros(N) self.w_history = np.zeros((N,self.n)) # adaptation loop for k in range(N): self.w_history[k,:] = self.w y[k] = np.dot(self.w, x[k]) e[k] = d[k] - y[k] nu = self.mu / (self.eps + np.dot(x[k], x[k])) dw = nu * x[k] * e[k]**3 self.w += dw return y, e, self.w_history
mit
jcchin/MagnePlane
src/hyperloop/Python/ticket_cost.py
4
8796
from __future__ import print_function import numpy as np from openmdao.api import IndepVarComp, Component, Group, Problem, ExecComp import matplotlib.pylab as plt class TicketCost(Component): ''' Notes ------- This Component takes into account various cost figures from the system model and combines them to estimate tickt cost per passenger. Params ------- length_cost : float Cost of materials per unit length. Default value is 2.437e6 USD/km pod_cost : float Cost per individual pod. Default value is 1.0e6 USD. capital_cost : float Estimate of overhead capital cost. Default value is 1.0e10 USD. energy_cost : float Cost of electricity. Default value is .13 USD/kWh ib : float Bond interest rate. Default value is .04 bm : float Bond maturity. Default value is 20.0 years. operating_time : float operating time per day. Default value is 16.0*3600 s JtokWh : float Convert J to kWh. Default value is J/kWh m_pod : float Pod mass. Default value is 3100 kg n_passengers : float Number of passengers. Default value is 28.0 pod_period : float Time in between pod departures. Default value is 120.0 s avg_speed : float average pod speed. Default value is 286.86 m/s track_length : float length of the track. Default value is 600e3 m pod_power : float Power consumption of the pod. Default value is 1.5e6 W prop_power : float power of an individual propulsion section. Default value is 350e3 W vac_power : float Power of the vacuum pumps. Default value is 71.049e6 W alpha : float percent of vacuum power used in steady state. Default value is .0001 vf : float Pod top speed. Default value is 286.86 m/s g : float Gravity. Default value is 9.81 m/s/s Cd : float Pod drag coefficient. Default value is .2 S : float Pod planform area. Default value is 40.42 m**2 p_tunnel : float Tunnel pressure. Default value is 850.0 Pa T_tunnel : float Tunnel temperature. Default value is 320 K R : float Ideal gas constant. Default value is 287 J/kg/K eta : float Efficiency of propulsion system D_mag : float Magnetic drag. Default value is (9.81*3100.0)/200.0 N thrust_time : float Time spent during a propulsive section. Default value is 1.5 s prop_period : float distance between pripulsion sections. Defualt value is 25.0e3 km Returns ------- ticket_cost : float cost of individual ticket. Default value is 0.0 USD prop_energy_cost : float cost of energy used by propulsion section per year. Default value is 0.0 USD ''' def __init__(self): super(TicketCost, self).__init__() self.add_param('land_cost', val = 2.437e6, desc = 'Cost of materials over land per unit length', units = 'USD/km') self.add_param('water_cost', val = 389.346941e3, desc = 'Cost of materials underwater per unit length', units = 'USD/km') self.add_param('pod_cost', val = 1.0e6, desc = 'Cost of individual pod', units = 'USD') self.add_param('capital_cost', val = 1.0e10, desc = 'Estimate of overhead capital cost', units = 'USD') self.add_param('energy_cost', val = .13, desc = 'Cost of electricity', units = 'USD/kW/h') self.add_param('ib', val = .04, desc = 'Bond interest rate', units = 'unitless') self.add_param('bm', val = 20.0, desc = 'Bond maturity', units = 'yr') self.add_param('operating_time', val = 16.0*3600, desc = 'Operating time per day', units = 's') self.add_param('JtokWh', val = 2.7778e-7, desc = 'Convert Joules to kWh', units = '(kw*h)/J') self.add_param('m_pod', val = 3100.0, desc = 'Pod Mass', units = 'kg') self.add_param('n_passengers', val = 28.0, desc = 'number of passengers', units = 'unitless') self.add_param('pod_period', val = 120.0, desc = 'Time in between departures', units = 's') self.add_param('avg_speed', val = 286.86, desc = 'Average Pod Speed', units = 'm/s') self.add_param('track_length', val = 600.0e3, desc = 'Track Length', units = 'm') self.add_param('land_length', val = 600e3, desc = 'Length traveled over land', units = 'm') self.add_param('water_length', val = 0.0e3, desc = 'Length traveled underwater', units = 'm') self.add_param('pod_power', val = 1.5e6, desc = 'Power required by pod motor', units = 'W') self.add_param('prop_power', val = 350.0e3, desc = 'Power of single propulsive section', units = 'W') self.add_param('vac_power', val = 71.049e6, desc = 'Power of vacuums', units = 'W') self.add_param('steady_vac_power', val = 950.0e3, desc = 'Steady State run power of vacuum pumps', units = 'W') self.add_param('vf', val = 286.86, desc = 'Pod top speed', units = 'm/s') self.add_param('g', val = 9.81, desc = 'Gravity', units = 'm/s/s') self.add_param('Cd', val = .2, desc = 'Pod drag coefficient', units = 'unitless') self.add_param('S', val = 40.42, desc = 'Pod planform area', units = 'm**2') self.add_param('p_tunnel', val = 850.0, desc = 'Tunnel Pressure', units = 'Pa') self.add_param('T_tunnel', val = 320.0, desc = 'Tunnel Temperature', units = 'K') self.add_param('R', val = 287.0, desc = 'Ideal gas constant', units = 'J/kg/K') self.add_param('eta', val = .8, desc = 'Propulsive efficiency', units = 'unitless') self.add_param('D_mag', val = (9.81*3100.0)/200.0, desc = 'Magnetic Drag', units = 'N') self.add_param('thrust_time', val = 1.5, desc = 'Time that pod is over propulsive section', units = 's') self.add_param('prop_period', val = 25.0e3, desc = 'distance between propulsive sections', units = 'm') self.add_param('num_thrust', val = 10.0, desc = 'Number of booster sections along track', units = 'unitless') self.add_output('num_pods', val = 0.0, desc = 'Number of Pods', units = 'unitless') self.add_output('ticket_cost', val = 0.0, desc = 'Ticket cost', units = 'USD') self.add_output('prop_energy_cost', val = 0.0, desc = 'Cost of propulsion energy', units = 'USD') self.add_output('tube_energy_cost', val = 0.0, desc = 'Cost of tube energy', units = 'USD') self.add_output('total_energy_cost', val = 0.0, desc = 'Cost of energy consumpition per year', units = 'USD') def solve_nonlinear(self, p, u,r): land_cost = p['land_cost'] water_cost = p['water_cost'] pod_cost= p['pod_cost'] capital_cost = p['capital_cost'] energy_cost = p['energy_cost'] ib = p['ib'] bm = p['bm'] operating_time = p['operating_time'] JtokWh = p['JtokWh'] m_pod = p['m_pod'] n_passengers = p['n_passengers'] pod_period = p['pod_period'] avg_speed = p['avg_speed'] track_length = p['track_length'] land_length = p['land_length'] water_length = p['water_length'] pod_power = -1.0*p['pod_power'] prop_power = p['prop_power'] vac_power = p['vac_power'] steady_vac_power = -1.0*p['steady_vac_power'] vf = p['vf'] g = p['g'] Cd = p['Cd'] S = p['S'] p_tunnel = p['p_tunnel'] T_tunnel = p['T_tunnel'] R = p['R'] eta = p['eta'] D_mag = p['D_mag'] thrust_time = p['thrust_time'] prop_period = p['prop_period'] num_thrust = p['num_thrust'] length_cost = ((water_length/track_length)*water_cost) + ((land_length/track_length)*land_cost) pod_frequency = 1.0/pod_period num_pods = np.ceil((track_length/avg_speed)*pod_frequency) flights_per_pod = (operating_time*pod_frequency)/num_pods energy_per_flight = pod_power*(track_length/avg_speed)*.9 pod_energy = energy_per_flight*flights_per_pod*num_pods*JtokWh vac_energy = steady_vac_power*operating_time*JtokWh rho = p_tunnel/(R*T_tunnel) start_distance = (vf**2)/(2*g) start_energy = ((m_pod*g+D_mag)*start_distance + (.5*Cd*rho*g*S*(start_distance**2)))/eta prop_energy = (num_thrust*thrust_time*prop_power + start_energy)*flights_per_pod*num_pods*JtokWh tube_energy = prop_energy + vac_energy u['num_pods'] = num_pods u['prop_energy_cost'] = prop_energy*energy_cost*365 u['tube_energy_cost'] = tube_energy*energy_cost*365 u['total_energy_cost'] = (pod_energy+tube_energy)*energy_cost*365 u['ticket_cost'] = cost_ticket = (length_cost*(track_length/1000.0) + pod_cost*num_pods + capital_cost*(1.0+ib) + \ energy_cost*(tube_energy + pod_energy)*365.0)/(n_passengers*pod_frequency*bm*365.0*24.0*3600.0) if __name__ == '__main__': top = Problem() root = top.root = Group() params = (('n_passengers', 28.0), ('track_length', 600.0e3, {'units' : 'm'})) root.add('p', TicketCost()) root.add('des_vars', IndepVarComp(params), promotes = ['n_passengers']) root.connect('n_passengers', 'p.n_passengers') root.connect('des_vars.track_length', 'p.track_length') top.setup() top.run() print(top['p.ticket_cost']) # n_passengers = np.linspace(10,100,num = 90) # ticket_cost = np.zeros((1, len(n_passengers))) # for i in range(len(n_passengers)): # top['n_passengers'] = n_passengers[i] # top.run() # ticket_cost[0, i] = top['p.ticket_cost'] # plt.plot(n_passengers*(175200.0/(1.0e6)), ticket_cost[0,:]) # plt.show()
apache-2.0
shangwuhencc/scikit-learn
examples/svm/plot_svm_anova.py
250
2000
""" ================================================= SVM-Anova: SVM with univariate feature selection ================================================= This example shows how to perform univariate feature before running a SVC (support vector classifier) to improve the classification scores. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets, feature_selection, cross_validation from sklearn.pipeline import Pipeline ############################################################################### # Import some data to play with digits = datasets.load_digits() y = digits.target # Throw away data, to be in the curse of dimension settings y = y[:200] X = digits.data[:200] n_samples = len(y) X = X.reshape((n_samples, -1)) # add 200 non-informative features X = np.hstack((X, 2 * np.random.random((n_samples, 200)))) ############################################################################### # Create a feature-selection transform and an instance of SVM that we # combine together to have an full-blown estimator transform = feature_selection.SelectPercentile(feature_selection.f_classif) clf = Pipeline([('anova', transform), ('svc', svm.SVC(C=1.0))]) ############################################################################### # Plot the cross-validation score as a function of percentile of features score_means = list() score_stds = list() percentiles = (1, 3, 6, 10, 15, 20, 30, 40, 60, 80, 100) for percentile in percentiles: clf.set_params(anova__percentile=percentile) # Compute cross-validation score using all CPUs this_scores = cross_validation.cross_val_score(clf, X, y, n_jobs=1) score_means.append(this_scores.mean()) score_stds.append(this_scores.std()) plt.errorbar(percentiles, score_means, np.array(score_stds)) plt.title( 'Performance of the SVM-Anova varying the percentile of features selected') plt.xlabel('Percentile') plt.ylabel('Prediction rate') plt.axis('tight') plt.show()
bsd-3-clause
rbalda/neural_ocr
env/lib/python2.7/site-packages/matplotlib/compat/subprocess.py
19
2827
""" A replacement wrapper around the subprocess module, with a number of work-arounds: - Provides the check_output function (which subprocess only provides from Python 2.7 onwards). - Provides a stub implementation of subprocess members on Google App Engine (which are missing in subprocess). Instead of importing subprocess, other modules should use this as follows: from matplotlib.compat import subprocess This module is safe to import from anywhere within matplotlib. """ from __future__ import absolute_import # Required to import subprocess from __future__ import print_function import subprocess __all__ = ['Popen', 'PIPE', 'STDOUT', 'check_output', 'CalledProcessError'] if hasattr(subprocess, 'Popen'): Popen = subprocess.Popen # Assume that it also has the other constants. PIPE = subprocess.PIPE STDOUT = subprocess.STDOUT CalledProcessError = subprocess.CalledProcessError else: # In restricted environments (such as Google App Engine), these are # non-existent. Replace them with dummy versions that always raise OSError. def Popen(*args, **kwargs): raise OSError("subprocess.Popen is not supported") PIPE = -1 STDOUT = -2 # There is no need to catch CalledProcessError. These stubs cannot raise # it. None in an except clause will simply not match any exceptions. CalledProcessError = None def _check_output(*popenargs, **kwargs): r"""Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute. The arguments are the same as for the Popen constructor. Example:: >>> check_output(["ls", "-l", "/dev/null"]) 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally. To capture standard error in the result, use stderr=STDOUT.:: >>> check_output(["/bin/sh", "-c", ... "ls -l non_existent_file ; exit 0"], ... stderr=STDOUT) 'ls: non_existent_file: No such file or directory\n' """ if 'stdout' in kwargs: raise ValueError('stdout argument not allowed, it will be overridden.') process = Popen(stdout=PIPE, *popenargs, **kwargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kwargs.get("args") if cmd is None: cmd = popenargs[0] raise subprocess.CalledProcessError(retcode, cmd, output=output) return output # python2.7's subprocess provides a check_output method if hasattr(subprocess, 'check_output'): check_output = subprocess.check_output else: check_output = _check_output
mit
kagayakidan/scikit-learn
examples/linear_model/plot_lasso_and_elasticnet.py
249
1982
""" ======================================== Lasso and Elastic Net for Sparse Signals ======================================== Estimates Lasso and Elastic-Net regression models on a manually generated sparse signal corrupted with an additive noise. Estimated coefficients are compared with the ground-truth. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import r2_score ############################################################################### # generate some sparse data to play with np.random.seed(42) n_samples, n_features = 50, 200 X = np.random.randn(n_samples, n_features) coef = 3 * np.random.randn(n_features) inds = np.arange(n_features) np.random.shuffle(inds) coef[inds[10:]] = 0 # sparsify coef y = np.dot(X, coef) # add noise y += 0.01 * np.random.normal((n_samples,)) # Split data in train set and test set n_samples = X.shape[0] X_train, y_train = X[:n_samples / 2], y[:n_samples / 2] X_test, y_test = X[n_samples / 2:], y[n_samples / 2:] ############################################################################### # Lasso from sklearn.linear_model import Lasso alpha = 0.1 lasso = Lasso(alpha=alpha) y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test) r2_score_lasso = r2_score(y_test, y_pred_lasso) print(lasso) print("r^2 on test data : %f" % r2_score_lasso) ############################################################################### # ElasticNet from sklearn.linear_model import ElasticNet enet = ElasticNet(alpha=alpha, l1_ratio=0.7) y_pred_enet = enet.fit(X_train, y_train).predict(X_test) r2_score_enet = r2_score(y_test, y_pred_enet) print(enet) print("r^2 on test data : %f" % r2_score_enet) plt.plot(enet.coef_, label='Elastic net coefficients') plt.plot(lasso.coef_, label='Lasso coefficients') plt.plot(coef, '--', label='original coefficients') plt.legend(loc='best') plt.title("Lasso R^2: %f, Elastic Net R^2: %f" % (r2_score_lasso, r2_score_enet)) plt.show()
bsd-3-clause
yavalvas/yav_com
build/matplotlib/lib/mpl_examples/pylab_examples/centered_ticklabels.py
6
1355
# sometimes it is nice to have ticklabels centered. mpl currently # associates a label with a tick, and the label can be aligned # 'center', 'left', or 'right' using the horizontal alignment property: # # # for label in ax.xaxis.get_xticklabels(): # label.set_horizontalalignment('right') # # # but this doesn't help center the label between ticks. One solution # is to "face it". Use the minor ticks to place a tick centered # between the major ticks. Here is an example that labels the months, # centered between the ticks import numpy as np import matplotlib.cbook as cbook import matplotlib.dates as dates import matplotlib.ticker as ticker import matplotlib.pyplot as plt # load some financial data; apple's stock price fh = cbook.get_sample_data('aapl.npy.gz') r = np.load(fh); fh.close() r = r[-250:] # get the last 250 days fig, ax = plt.subplots() ax.plot(r.date, r.adj_close) ax.xaxis.set_major_locator(dates.MonthLocator()) ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) for tick in ax.xaxis.get_minor_ticks(): tick.tick1line.set_markersize(0) tick.tick2line.set_markersize(0) tick.label1.set_horizontalalignment('center') imid = len(r)/2 ax.set_xlabel(str(r.date[imid].year)) plt.show()
mit
mne-tools/mne-tools.github.io
0.21/_downloads/ae7d4d6bcae82f99a78c3f8a0c94f7b0/plot_mne_inverse_envelope_correlation.py
3
4522
""" .. _ex-envelope-correlation: ============================================= Compute envelope correlations in source space ============================================= Compute envelope correlations of orthogonalized activity [1]_ [2]_ in source space using resting state CTF data. """ # Authors: Eric Larson <larson.eric.d@gmail.com> # Sheraz Khan <sheraz@khansheraz.com> # Denis Engemann <denis.engemann@gmail.com> # # License: BSD (3-clause) import os.path as op import numpy as np import matplotlib.pyplot as plt import mne from mne.connectivity import envelope_correlation from mne.minimum_norm import make_inverse_operator, apply_inverse_epochs from mne.preprocessing import compute_proj_ecg, compute_proj_eog data_path = mne.datasets.brainstorm.bst_resting.data_path() subjects_dir = op.join(data_path, 'subjects') subject = 'bst_resting' trans = op.join(data_path, 'MEG', 'bst_resting', 'bst_resting-trans.fif') src = op.join(subjects_dir, subject, 'bem', subject + '-oct-6-src.fif') bem = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem-sol.fif') raw_fname = op.join(data_path, 'MEG', 'bst_resting', 'subj002_spontaneous_20111102_01_AUX.ds') ############################################################################## # Here we do some things in the name of speed, such as crop (which will # hurt SNR) and downsample. Then we compute SSP projectors and apply them. raw = mne.io.read_raw_ctf(raw_fname, verbose='error') raw.crop(0, 60).pick_types(meg=True, eeg=False).load_data().resample(80) raw.apply_gradient_compensation(3) projs_ecg, _ = compute_proj_ecg(raw, n_grad=1, n_mag=2) projs_eog, _ = compute_proj_eog(raw, n_grad=1, n_mag=2, ch_name='MLT31-4407') raw.info['projs'] += projs_ecg raw.info['projs'] += projs_eog raw.apply_proj() cov = mne.compute_raw_covariance(raw) # compute before band-pass of interest ############################################################################## # Now we band-pass filter our data and create epochs. raw.filter(14, 30) events = mne.make_fixed_length_events(raw, duration=5.) epochs = mne.Epochs(raw, events=events, tmin=0, tmax=5., baseline=None, reject=dict(mag=8e-13), preload=True) del raw ############################################################################## # Compute the forward and inverse # ------------------------------- src = mne.read_source_spaces(src) fwd = mne.make_forward_solution(epochs.info, trans, src, bem) inv = make_inverse_operator(epochs.info, fwd, cov) del fwd, src ############################################################################## # Compute label time series and do envelope correlation # ----------------------------------------------------- labels = mne.read_labels_from_annot(subject, 'aparc_sub', subjects_dir=subjects_dir) epochs.apply_hilbert() # faster to apply in sensor space stcs = apply_inverse_epochs(epochs, inv, lambda2=1. / 9., pick_ori='normal', return_generator=True) label_ts = mne.extract_label_time_course( stcs, labels, inv['src'], return_generator=True) corr = envelope_correlation(label_ts, verbose=True) # let's plot this matrix fig, ax = plt.subplots(figsize=(4, 4)) ax.imshow(corr, cmap='viridis', clim=np.percentile(corr, [5, 95])) fig.tight_layout() ############################################################################## # Compute the degree and plot it # ------------------------------ # sphinx_gallery_thumbnail_number = 2 threshold_prop = 0.15 # percentage of strongest edges to keep in the graph degree = mne.connectivity.degree(corr, threshold_prop=threshold_prop) stc = mne.labels_to_stc(labels, degree) stc = stc.in_label(mne.Label(inv['src'][0]['vertno'], hemi='lh') + mne.Label(inv['src'][1]['vertno'], hemi='rh')) brain = stc.plot( clim=dict(kind='percent', lims=[75, 85, 95]), colormap='gnuplot', subjects_dir=subjects_dir, views='dorsal', hemi='both', smoothing_steps=25, time_label='Beta band') ############################################################################## # References # ---------- # .. [1] Hipp JF, Hawellek DJ, Corbetta M, Siegel M, Engel AK (2012) # Large-scale cortical correlation structure of spontaneous # oscillatory activity. Nature Neuroscience 15:884–890 # .. [2] Khan S et al. (2018). Maturation trajectories of cortical # resting-state networks depend on the mediating frequency band. # Neuroimage 174:57–68
bsd-3-clause
dsquareindia/scikit-learn
examples/model_selection/plot_confusion_matrix.py
63
3231
""" ================ Confusion matrix ================ Example of confusion matrix usage to evaluate the quality of the output of a classifier on the iris data set. The diagonal elements represent the number of points for which the predicted label is equal to the true label, while off-diagonal elements are those that are mislabeled by the classifier. The higher the diagonal values of the confusion matrix the better, indicating many correct predictions. The figures show the confusion matrix with and without normalization by class support size (number of elements in each class). This kind of normalization can be interesting in case of class imbalance to have a more visual interpretation of which class is being misclassified. Here the results are not as good as they could be as our choice for the regularization parameter C was not the best. In real life applications this parameter is usually chosen using :ref:`grid_search`. """ print(__doc__) import itertools import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix # import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target class_names = iris.target_names # Split the data into a training set and a test set X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) # Run classifier, using a model that is too regularized (C too low) to see # the impact on the results classifier = svm.SVC(kernel='linear', C=0.01) y_pred = classifier.fit(X_train, y_train).predict(X_test) def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') # Compute confusion matrix cnf_matrix = confusion_matrix(y_test, y_pred) np.set_printoptions(precision=2) # Plot non-normalized confusion matrix plt.figure() plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion matrix, without normalization') # Plot normalized confusion matrix plt.figure() plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True, title='Normalized confusion matrix') plt.show()
bsd-3-clause
tomlof/scikit-learn
examples/plot_digits_pipe.py
65
1652
#!/usr/bin/python # -*- coding: utf-8 -*- """ ========================================================= Pipelining: chaining a PCA and a logistic regression ========================================================= The PCA does an unsupervised dimensionality reduction, while the logistic regression does the prediction. We use a GridSearchCV to set the dimensionality of the PCA """ print(__doc__) # Code source: Gaël Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model, decomposition, datasets from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV logistic = linear_model.LogisticRegression() pca = decomposition.PCA() pipe = Pipeline(steps=[('pca', pca), ('logistic', logistic)]) digits = datasets.load_digits() X_digits = digits.data y_digits = digits.target # Plot the PCA spectrum pca.fit(X_digits) plt.figure(1, figsize=(4, 3)) plt.clf() plt.axes([.2, .2, .7, .7]) plt.plot(pca.explained_variance_, linewidth=2) plt.axis('tight') plt.xlabel('n_components') plt.ylabel('explained_variance_') # Prediction n_components = [20, 40, 64] Cs = np.logspace(-4, 4, 3) # Parameters of pipelines can be set using ‘__’ separated parameter names: estimator = GridSearchCV(pipe, dict(pca__n_components=n_components, logistic__C=Cs)) estimator.fit(X_digits, y_digits) plt.axvline(estimator.best_estimator_.named_steps['pca'].n_components, linestyle=':', label='n_components chosen') plt.legend(prop=dict(size=12)) plt.show()
bsd-3-clause
mdhaber/scipy
scipy/optimize/_lsq/least_squares.py
12
39190
"""Generic interface for least-squares minimization.""" from warnings import warn import numpy as np from numpy.linalg import norm from scipy.sparse import issparse, csr_matrix from scipy.sparse.linalg import LinearOperator from scipy.optimize import _minpack, OptimizeResult from scipy.optimize._numdiff import approx_derivative, group_columns from .trf import trf from .dogbox import dogbox from .common import EPS, in_bounds, make_strictly_feasible TERMINATION_MESSAGES = { -1: "Improper input parameters status returned from `leastsq`", 0: "The maximum number of function evaluations is exceeded.", 1: "`gtol` termination condition is satisfied.", 2: "`ftol` termination condition is satisfied.", 3: "`xtol` termination condition is satisfied.", 4: "Both `ftol` and `xtol` termination conditions are satisfied." } FROM_MINPACK_TO_COMMON = { 0: -1, # Improper input parameters from MINPACK. 1: 2, 2: 3, 3: 4, 4: 1, 5: 0 # There are 6, 7, 8 for too small tolerance parameters, # but we guard against it by checking ftol, xtol, gtol beforehand. } def call_minpack(fun, x0, jac, ftol, xtol, gtol, max_nfev, x_scale, diff_step): n = x0.size if diff_step is None: epsfcn = EPS else: epsfcn = diff_step**2 # Compute MINPACK's `diag`, which is inverse of our `x_scale` and # ``x_scale='jac'`` corresponds to ``diag=None``. if isinstance(x_scale, str) and x_scale == 'jac': diag = None else: diag = 1 / x_scale full_output = True col_deriv = False factor = 100.0 if jac is None: if max_nfev is None: # n squared to account for Jacobian evaluations. max_nfev = 100 * n * (n + 1) x, info, status = _minpack._lmdif( fun, x0, (), full_output, ftol, xtol, gtol, max_nfev, epsfcn, factor, diag) else: if max_nfev is None: max_nfev = 100 * n x, info, status = _minpack._lmder( fun, jac, x0, (), full_output, col_deriv, ftol, xtol, gtol, max_nfev, factor, diag) f = info['fvec'] if callable(jac): J = jac(x) else: J = np.atleast_2d(approx_derivative(fun, x)) cost = 0.5 * np.dot(f, f) g = J.T.dot(f) g_norm = norm(g, ord=np.inf) nfev = info['nfev'] njev = info.get('njev', None) status = FROM_MINPACK_TO_COMMON[status] active_mask = np.zeros_like(x0, dtype=int) return OptimizeResult( x=x, cost=cost, fun=f, jac=J, grad=g, optimality=g_norm, active_mask=active_mask, nfev=nfev, njev=njev, status=status) def prepare_bounds(bounds, n): lb, ub = [np.asarray(b, dtype=float) for b in bounds] if lb.ndim == 0: lb = np.resize(lb, n) if ub.ndim == 0: ub = np.resize(ub, n) return lb, ub def check_tolerance(ftol, xtol, gtol, method): def check(tol, name): if tol is None: tol = 0 elif tol < EPS: warn("Setting `{}` below the machine epsilon ({:.2e}) effectively " "disables the corresponding termination condition." .format(name, EPS)) return tol ftol = check(ftol, "ftol") xtol = check(xtol, "xtol") gtol = check(gtol, "gtol") if method == "lm" and (ftol < EPS or xtol < EPS or gtol < EPS): raise ValueError("All tolerances must be higher than machine epsilon " "({:.2e}) for method 'lm'.".format(EPS)) elif ftol < EPS and xtol < EPS and gtol < EPS: raise ValueError("At least one of the tolerances must be higher than " "machine epsilon ({:.2e}).".format(EPS)) return ftol, xtol, gtol def check_x_scale(x_scale, x0): if isinstance(x_scale, str) and x_scale == 'jac': return x_scale try: x_scale = np.asarray(x_scale, dtype=float) valid = np.all(np.isfinite(x_scale)) and np.all(x_scale > 0) except (ValueError, TypeError): valid = False if not valid: raise ValueError("`x_scale` must be 'jac' or array_like with " "positive numbers.") if x_scale.ndim == 0: x_scale = np.resize(x_scale, x0.shape) if x_scale.shape != x0.shape: raise ValueError("Inconsistent shapes between `x_scale` and `x0`.") return x_scale def check_jac_sparsity(jac_sparsity, m, n): if jac_sparsity is None: return None if not issparse(jac_sparsity): jac_sparsity = np.atleast_2d(jac_sparsity) if jac_sparsity.shape != (m, n): raise ValueError("`jac_sparsity` has wrong shape.") return jac_sparsity, group_columns(jac_sparsity) # Loss functions. def huber(z, rho, cost_only): mask = z <= 1 rho[0, mask] = z[mask] rho[0, ~mask] = 2 * z[~mask]**0.5 - 1 if cost_only: return rho[1, mask] = 1 rho[1, ~mask] = z[~mask]**-0.5 rho[2, mask] = 0 rho[2, ~mask] = -0.5 * z[~mask]**-1.5 def soft_l1(z, rho, cost_only): t = 1 + z rho[0] = 2 * (t**0.5 - 1) if cost_only: return rho[1] = t**-0.5 rho[2] = -0.5 * t**-1.5 def cauchy(z, rho, cost_only): rho[0] = np.log1p(z) if cost_only: return t = 1 + z rho[1] = 1 / t rho[2] = -1 / t**2 def arctan(z, rho, cost_only): rho[0] = np.arctan(z) if cost_only: return t = 1 + z**2 rho[1] = 1 / t rho[2] = -2 * z / t**2 IMPLEMENTED_LOSSES = dict(linear=None, huber=huber, soft_l1=soft_l1, cauchy=cauchy, arctan=arctan) def construct_loss_function(m, loss, f_scale): if loss == 'linear': return None if not callable(loss): loss = IMPLEMENTED_LOSSES[loss] rho = np.empty((3, m)) def loss_function(f, cost_only=False): z = (f / f_scale) ** 2 loss(z, rho, cost_only=cost_only) if cost_only: return 0.5 * f_scale ** 2 * np.sum(rho[0]) rho[0] *= f_scale ** 2 rho[2] /= f_scale ** 2 return rho else: def loss_function(f, cost_only=False): z = (f / f_scale) ** 2 rho = loss(z) if cost_only: return 0.5 * f_scale ** 2 * np.sum(rho[0]) rho[0] *= f_scale ** 2 rho[2] /= f_scale ** 2 return rho return loss_function def least_squares( fun, x0, jac='2-point', bounds=(-np.inf, np.inf), method='trf', ftol=1e-8, xtol=1e-8, gtol=1e-8, x_scale=1.0, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options={}, jac_sparsity=None, max_nfev=None, verbose=0, args=(), kwargs={}): """Solve a nonlinear least-squares problem with bounds on the variables. Given the residuals f(x) (an m-D real function of n real variables) and the loss function rho(s) (a scalar function), `least_squares` finds a local minimum of the cost function F(x):: minimize F(x) = 0.5 * sum(rho(f_i(x)**2), i = 0, ..., m - 1) subject to lb <= x <= ub The purpose of the loss function rho(s) is to reduce the influence of outliers on the solution. Parameters ---------- fun : callable Function which computes the vector of residuals, with the signature ``fun(x, *args, **kwargs)``, i.e., the minimization proceeds with respect to its first argument. The argument ``x`` passed to this function is an ndarray of shape (n,) (never a scalar, even for n=1). It must allocate and return a 1-D array_like of shape (m,) or a scalar. If the argument ``x`` is complex or the function ``fun`` returns complex residuals, it must be wrapped in a real function of real arguments, as shown at the end of the Examples section. x0 : array_like with shape (n,) or float Initial guess on independent variables. If float, it will be treated as a 1-D array with one element. jac : {'2-point', '3-point', 'cs', callable}, optional Method of computing the Jacobian matrix (an m-by-n matrix, where element (i, j) is the partial derivative of f[i] with respect to x[j]). The keywords select a finite difference scheme for numerical estimation. The scheme '3-point' is more accurate, but requires twice as many operations as '2-point' (default). The scheme 'cs' uses complex steps, and while potentially the most accurate, it is applicable only when `fun` correctly handles complex inputs and can be analytically continued to the complex plane. Method 'lm' always uses the '2-point' scheme. If callable, it is used as ``jac(x, *args, **kwargs)`` and should return a good approximation (or the exact value) for the Jacobian as an array_like (np.atleast_2d is applied), a sparse matrix (csr_matrix preferred for performance) or a `scipy.sparse.linalg.LinearOperator`. bounds : 2-tuple of array_like, optional Lower and upper bounds on independent variables. Defaults to no bounds. Each array must match the size of `x0` or be a scalar, in the latter case a bound will be the same for all variables. Use ``np.inf`` with an appropriate sign to disable bounds on all or some variables. method : {'trf', 'dogbox', 'lm'}, optional Algorithm to perform minimization. * 'trf' : Trust Region Reflective algorithm, particularly suitable for large sparse problems with bounds. Generally robust method. * 'dogbox' : dogleg algorithm with rectangular trust regions, typical use case is small problems with bounds. Not recommended for problems with rank-deficient Jacobian. * 'lm' : Levenberg-Marquardt algorithm as implemented in MINPACK. Doesn't handle bounds and sparse Jacobians. Usually the most efficient method for small unconstrained problems. Default is 'trf'. See Notes for more information. ftol : float or None, optional Tolerance for termination by the change of the cost function. Default is 1e-8. The optimization process is stopped when ``dF < ftol * F``, and there was an adequate agreement between a local quadratic model and the true model in the last step. If None and 'method' is not 'lm', the termination by this condition is disabled. If 'method' is 'lm', this tolerance must be higher than machine epsilon. xtol : float or None, optional Tolerance for termination by the change of the independent variables. Default is 1e-8. The exact condition depends on the `method` used: * For 'trf' and 'dogbox' : ``norm(dx) < xtol * (xtol + norm(x))``. * For 'lm' : ``Delta < xtol * norm(xs)``, where ``Delta`` is a trust-region radius and ``xs`` is the value of ``x`` scaled according to `x_scale` parameter (see below). If None and 'method' is not 'lm', the termination by this condition is disabled. If 'method' is 'lm', this tolerance must be higher than machine epsilon. gtol : float or None, optional Tolerance for termination by the norm of the gradient. Default is 1e-8. The exact condition depends on a `method` used: * For 'trf' : ``norm(g_scaled, ord=np.inf) < gtol``, where ``g_scaled`` is the value of the gradient scaled to account for the presence of the bounds [STIR]_. * For 'dogbox' : ``norm(g_free, ord=np.inf) < gtol``, where ``g_free`` is the gradient with respect to the variables which are not in the optimal state on the boundary. * For 'lm' : the maximum absolute value of the cosine of angles between columns of the Jacobian and the residual vector is less than `gtol`, or the residual vector is zero. If None and 'method' is not 'lm', the termination by this condition is disabled. If 'method' is 'lm', this tolerance must be higher than machine epsilon. x_scale : array_like or 'jac', optional Characteristic scale of each variable. Setting `x_scale` is equivalent to reformulating the problem in scaled variables ``xs = x / x_scale``. An alternative view is that the size of a trust region along jth dimension is proportional to ``x_scale[j]``. Improved convergence may be achieved by setting `x_scale` such that a step of a given size along any of the scaled variables has a similar effect on the cost function. If set to 'jac', the scale is iteratively updated using the inverse norms of the columns of the Jacobian matrix (as described in [JJMore]_). loss : str or callable, optional Determines the loss function. The following keyword values are allowed: * 'linear' (default) : ``rho(z) = z``. Gives a standard least-squares problem. * 'soft_l1' : ``rho(z) = 2 * ((1 + z)**0.5 - 1)``. The smooth approximation of l1 (absolute value) loss. Usually a good choice for robust least squares. * 'huber' : ``rho(z) = z if z <= 1 else 2*z**0.5 - 1``. Works similarly to 'soft_l1'. * 'cauchy' : ``rho(z) = ln(1 + z)``. Severely weakens outliers influence, but may cause difficulties in optimization process. * 'arctan' : ``rho(z) = arctan(z)``. Limits a maximum loss on a single residual, has properties similar to 'cauchy'. If callable, it must take a 1-D ndarray ``z=f**2`` and return an array_like with shape (3, m) where row 0 contains function values, row 1 contains first derivatives and row 2 contains second derivatives. Method 'lm' supports only 'linear' loss. f_scale : float, optional Value of soft margin between inlier and outlier residuals, default is 1.0. The loss function is evaluated as follows ``rho_(f**2) = C**2 * rho(f**2 / C**2)``, where ``C`` is `f_scale`, and ``rho`` is determined by `loss` parameter. This parameter has no effect with ``loss='linear'``, but for other `loss` values it is of crucial importance. max_nfev : None or int, optional Maximum number of function evaluations before the termination. If None (default), the value is chosen automatically: * For 'trf' and 'dogbox' : 100 * n. * For 'lm' : 100 * n if `jac` is callable and 100 * n * (n + 1) otherwise (because 'lm' counts function calls in Jacobian estimation). diff_step : None or array_like, optional Determines the relative step size for the finite difference approximation of the Jacobian. The actual step is computed as ``x * diff_step``. If None (default), then `diff_step` is taken to be a conventional "optimal" power of machine epsilon for the finite difference scheme used [NR]_. tr_solver : {None, 'exact', 'lsmr'}, optional Method for solving trust-region subproblems, relevant only for 'trf' and 'dogbox' methods. * 'exact' is suitable for not very large problems with dense Jacobian matrices. The computational complexity per iteration is comparable to a singular value decomposition of the Jacobian matrix. * 'lsmr' is suitable for problems with sparse and large Jacobian matrices. It uses the iterative procedure `scipy.sparse.linalg.lsmr` for finding a solution of a linear least-squares problem and only requires matrix-vector product evaluations. If None (default), the solver is chosen based on the type of Jacobian returned on the first iteration. tr_options : dict, optional Keyword options passed to trust-region solver. * ``tr_solver='exact'``: `tr_options` are ignored. * ``tr_solver='lsmr'``: options for `scipy.sparse.linalg.lsmr`. Additionally, ``method='trf'`` supports 'regularize' option (bool, default is True), which adds a regularization term to the normal equation, which improves convergence if the Jacobian is rank-deficient [Byrd]_ (eq. 3.4). jac_sparsity : {None, array_like, sparse matrix}, optional Defines the sparsity structure of the Jacobian matrix for finite difference estimation, its shape must be (m, n). If the Jacobian has only few non-zero elements in *each* row, providing the sparsity structure will greatly speed up the computations [Curtis]_. A zero entry means that a corresponding element in the Jacobian is identically zero. If provided, forces the use of 'lsmr' trust-region solver. If None (default), then dense differencing will be used. Has no effect for 'lm' method. verbose : {0, 1, 2}, optional Level of algorithm's verbosity: * 0 (default) : work silently. * 1 : display a termination report. * 2 : display progress during iterations (not supported by 'lm' method). args, kwargs : tuple and dict, optional Additional arguments passed to `fun` and `jac`. Both empty by default. The calling signature is ``fun(x, *args, **kwargs)`` and the same for `jac`. Returns ------- result : OptimizeResult `OptimizeResult` with the following fields defined: x : ndarray, shape (n,) Solution found. cost : float Value of the cost function at the solution. fun : ndarray, shape (m,) Vector of residuals at the solution. jac : ndarray, sparse matrix or LinearOperator, shape (m, n) Modified Jacobian matrix at the solution, in the sense that J^T J is a Gauss-Newton approximation of the Hessian of the cost function. The type is the same as the one used by the algorithm. grad : ndarray, shape (m,) Gradient of the cost function at the solution. optimality : float First-order optimality measure. In unconstrained problems, it is always the uniform norm of the gradient. In constrained problems, it is the quantity which was compared with `gtol` during iterations. active_mask : ndarray of int, shape (n,) Each component shows whether a corresponding constraint is active (that is, whether a variable is at the bound): * 0 : a constraint is not active. * -1 : a lower bound is active. * 1 : an upper bound is active. Might be somewhat arbitrary for 'trf' method as it generates a sequence of strictly feasible iterates and `active_mask` is determined within a tolerance threshold. nfev : int Number of function evaluations done. Methods 'trf' and 'dogbox' do not count function calls for numerical Jacobian approximation, as opposed to 'lm' method. njev : int or None Number of Jacobian evaluations done. If numerical Jacobian approximation is used in 'lm' method, it is set to None. status : int The reason for algorithm termination: * -1 : improper input parameters status returned from MINPACK. * 0 : the maximum number of function evaluations is exceeded. * 1 : `gtol` termination condition is satisfied. * 2 : `ftol` termination condition is satisfied. * 3 : `xtol` termination condition is satisfied. * 4 : Both `ftol` and `xtol` termination conditions are satisfied. message : str Verbal description of the termination reason. success : bool True if one of the convergence criteria is satisfied (`status` > 0). See Also -------- leastsq : A legacy wrapper for the MINPACK implementation of the Levenberg-Marquadt algorithm. curve_fit : Least-squares minimization applied to a curve-fitting problem. Notes ----- Method 'lm' (Levenberg-Marquardt) calls a wrapper over least-squares algorithms implemented in MINPACK (lmder, lmdif). It runs the Levenberg-Marquardt algorithm formulated as a trust-region type algorithm. The implementation is based on paper [JJMore]_, it is very robust and efficient with a lot of smart tricks. It should be your first choice for unconstrained problems. Note that it doesn't support bounds. Also, it doesn't work when m < n. Method 'trf' (Trust Region Reflective) is motivated by the process of solving a system of equations, which constitute the first-order optimality condition for a bound-constrained minimization problem as formulated in [STIR]_. The algorithm iteratively solves trust-region subproblems augmented by a special diagonal quadratic term and with trust-region shape determined by the distance from the bounds and the direction of the gradient. This enhancements help to avoid making steps directly into bounds and efficiently explore the whole space of variables. To further improve convergence, the algorithm considers search directions reflected from the bounds. To obey theoretical requirements, the algorithm keeps iterates strictly feasible. With dense Jacobians trust-region subproblems are solved by an exact method very similar to the one described in [JJMore]_ (and implemented in MINPACK). The difference from the MINPACK implementation is that a singular value decomposition of a Jacobian matrix is done once per iteration, instead of a QR decomposition and series of Givens rotation eliminations. For large sparse Jacobians a 2-D subspace approach of solving trust-region subproblems is used [STIR]_, [Byrd]_. The subspace is spanned by a scaled gradient and an approximate Gauss-Newton solution delivered by `scipy.sparse.linalg.lsmr`. When no constraints are imposed the algorithm is very similar to MINPACK and has generally comparable performance. The algorithm works quite robust in unbounded and bounded problems, thus it is chosen as a default algorithm. Method 'dogbox' operates in a trust-region framework, but considers rectangular trust regions as opposed to conventional ellipsoids [Voglis]_. The intersection of a current trust region and initial bounds is again rectangular, so on each iteration a quadratic minimization problem subject to bound constraints is solved approximately by Powell's dogleg method [NumOpt]_. The required Gauss-Newton step can be computed exactly for dense Jacobians or approximately by `scipy.sparse.linalg.lsmr` for large sparse Jacobians. The algorithm is likely to exhibit slow convergence when the rank of Jacobian is less than the number of variables. The algorithm often outperforms 'trf' in bounded problems with a small number of variables. Robust loss functions are implemented as described in [BA]_. The idea is to modify a residual vector and a Jacobian matrix on each iteration such that computed gradient and Gauss-Newton Hessian approximation match the true gradient and Hessian approximation of the cost function. Then the algorithm proceeds in a normal way, i.e., robust loss functions are implemented as a simple wrapper over standard least-squares algorithms. .. versionadded:: 0.17.0 References ---------- .. [STIR] M. A. Branch, T. F. Coleman, and Y. Li, "A Subspace, Interior, and Conjugate Gradient Method for Large-Scale Bound-Constrained Minimization Problems," SIAM Journal on Scientific Computing, Vol. 21, Number 1, pp 1-23, 1999. .. [NR] William H. Press et. al., "Numerical Recipes. The Art of Scientific Computing. 3rd edition", Sec. 5.7. .. [Byrd] R. H. Byrd, R. B. Schnabel and G. A. Shultz, "Approximate solution of the trust region problem by minimization over two-dimensional subspaces", Math. Programming, 40, pp. 247-263, 1988. .. [Curtis] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of sparse Jacobian matrices", Journal of the Institute of Mathematics and its Applications, 13, pp. 117-120, 1974. .. [JJMore] J. J. More, "The Levenberg-Marquardt Algorithm: Implementation and Theory," Numerical Analysis, ed. G. A. Watson, Lecture Notes in Mathematics 630, Springer Verlag, pp. 105-116, 1977. .. [Voglis] C. Voglis and I. E. Lagaris, "A Rectangular Trust Region Dogleg Approach for Unconstrained and Bound Constrained Nonlinear Optimization", WSEAS International Conference on Applied Mathematics, Corfu, Greece, 2004. .. [NumOpt] J. Nocedal and S. J. Wright, "Numerical optimization, 2nd edition", Chapter 4. .. [BA] B. Triggs et. al., "Bundle Adjustment - A Modern Synthesis", Proceedings of the International Workshop on Vision Algorithms: Theory and Practice, pp. 298-372, 1999. Examples -------- In this example we find a minimum of the Rosenbrock function without bounds on independent variables. >>> def fun_rosenbrock(x): ... return np.array([10 * (x[1] - x[0]**2), (1 - x[0])]) Notice that we only provide the vector of the residuals. The algorithm constructs the cost function as a sum of squares of the residuals, which gives the Rosenbrock function. The exact minimum is at ``x = [1.0, 1.0]``. >>> from scipy.optimize import least_squares >>> x0_rosenbrock = np.array([2, 2]) >>> res_1 = least_squares(fun_rosenbrock, x0_rosenbrock) >>> res_1.x array([ 1., 1.]) >>> res_1.cost 9.8669242910846867e-30 >>> res_1.optimality 8.8928864934219529e-14 We now constrain the variables, in such a way that the previous solution becomes infeasible. Specifically, we require that ``x[1] >= 1.5``, and ``x[0]`` left unconstrained. To this end, we specify the `bounds` parameter to `least_squares` in the form ``bounds=([-np.inf, 1.5], np.inf)``. We also provide the analytic Jacobian: >>> def jac_rosenbrock(x): ... return np.array([ ... [-20 * x[0], 10], ... [-1, 0]]) Putting this all together, we see that the new solution lies on the bound: >>> res_2 = least_squares(fun_rosenbrock, x0_rosenbrock, jac_rosenbrock, ... bounds=([-np.inf, 1.5], np.inf)) >>> res_2.x array([ 1.22437075, 1.5 ]) >>> res_2.cost 0.025213093946805685 >>> res_2.optimality 1.5885401433157753e-07 Now we solve a system of equations (i.e., the cost function should be zero at a minimum) for a Broyden tridiagonal vector-valued function of 100000 variables: >>> def fun_broyden(x): ... f = (3 - x) * x + 1 ... f[1:] -= x[:-1] ... f[:-1] -= 2 * x[1:] ... return f The corresponding Jacobian matrix is sparse. We tell the algorithm to estimate it by finite differences and provide the sparsity structure of Jacobian to significantly speed up this process. >>> from scipy.sparse import lil_matrix >>> def sparsity_broyden(n): ... sparsity = lil_matrix((n, n), dtype=int) ... i = np.arange(n) ... sparsity[i, i] = 1 ... i = np.arange(1, n) ... sparsity[i, i - 1] = 1 ... i = np.arange(n - 1) ... sparsity[i, i + 1] = 1 ... return sparsity ... >>> n = 100000 >>> x0_broyden = -np.ones(n) ... >>> res_3 = least_squares(fun_broyden, x0_broyden, ... jac_sparsity=sparsity_broyden(n)) >>> res_3.cost 4.5687069299604613e-23 >>> res_3.optimality 1.1650454296851518e-11 Let's also solve a curve fitting problem using robust loss function to take care of outliers in the data. Define the model function as ``y = a + b * exp(c * t)``, where t is a predictor variable, y is an observation and a, b, c are parameters to estimate. First, define the function which generates the data with noise and outliers, define the model parameters, and generate data: >>> from numpy.random import default_rng >>> rng = default_rng() >>> def gen_data(t, a, b, c, noise=0., n_outliers=0, seed=None): ... rng = default_rng(seed) ... ... y = a + b * np.exp(t * c) ... ... error = noise * rng.standard_normal(t.size) ... outliers = rng.integers(0, t.size, n_outliers) ... error[outliers] *= 10 ... ... return y + error ... >>> a = 0.5 >>> b = 2.0 >>> c = -1 >>> t_min = 0 >>> t_max = 10 >>> n_points = 15 ... >>> t_train = np.linspace(t_min, t_max, n_points) >>> y_train = gen_data(t_train, a, b, c, noise=0.1, n_outliers=3) Define function for computing residuals and initial estimate of parameters. >>> def fun(x, t, y): ... return x[0] + x[1] * np.exp(x[2] * t) - y ... >>> x0 = np.array([1.0, 1.0, 0.0]) Compute a standard least-squares solution: >>> res_lsq = least_squares(fun, x0, args=(t_train, y_train)) Now compute two solutions with two different robust loss functions. The parameter `f_scale` is set to 0.1, meaning that inlier residuals should not significantly exceed 0.1 (the noise level used). >>> res_soft_l1 = least_squares(fun, x0, loss='soft_l1', f_scale=0.1, ... args=(t_train, y_train)) >>> res_log = least_squares(fun, x0, loss='cauchy', f_scale=0.1, ... args=(t_train, y_train)) And, finally, plot all the curves. We see that by selecting an appropriate `loss` we can get estimates close to optimal even in the presence of strong outliers. But keep in mind that generally it is recommended to try 'soft_l1' or 'huber' losses first (if at all necessary) as the other two options may cause difficulties in optimization process. >>> t_test = np.linspace(t_min, t_max, n_points * 10) >>> y_true = gen_data(t_test, a, b, c) >>> y_lsq = gen_data(t_test, *res_lsq.x) >>> y_soft_l1 = gen_data(t_test, *res_soft_l1.x) >>> y_log = gen_data(t_test, *res_log.x) ... >>> import matplotlib.pyplot as plt >>> plt.plot(t_train, y_train, 'o') >>> plt.plot(t_test, y_true, 'k', linewidth=2, label='true') >>> plt.plot(t_test, y_lsq, label='linear loss') >>> plt.plot(t_test, y_soft_l1, label='soft_l1 loss') >>> plt.plot(t_test, y_log, label='cauchy loss') >>> plt.xlabel("t") >>> plt.ylabel("y") >>> plt.legend() >>> plt.show() In the next example, we show how complex-valued residual functions of complex variables can be optimized with ``least_squares()``. Consider the following function: >>> def f(z): ... return z - (0.5 + 0.5j) We wrap it into a function of real variables that returns real residuals by simply handling the real and imaginary parts as independent variables: >>> def f_wrap(x): ... fx = f(x[0] + 1j*x[1]) ... return np.array([fx.real, fx.imag]) Thus, instead of the original m-D complex function of n complex variables we optimize a 2m-D real function of 2n real variables: >>> from scipy.optimize import least_squares >>> res_wrapped = least_squares(f_wrap, (0.1, 0.1), bounds=([0, 0], [1, 1])) >>> z = res_wrapped.x[0] + res_wrapped.x[1]*1j >>> z (0.49999999999925893+0.49999999999925893j) """ if method not in ['trf', 'dogbox', 'lm']: raise ValueError("`method` must be 'trf', 'dogbox' or 'lm'.") if jac not in ['2-point', '3-point', 'cs'] and not callable(jac): raise ValueError("`jac` must be '2-point', '3-point', 'cs' or " "callable.") if tr_solver not in [None, 'exact', 'lsmr']: raise ValueError("`tr_solver` must be None, 'exact' or 'lsmr'.") if loss not in IMPLEMENTED_LOSSES and not callable(loss): raise ValueError("`loss` must be one of {0} or a callable." .format(IMPLEMENTED_LOSSES.keys())) if method == 'lm' and loss != 'linear': raise ValueError("method='lm' supports only 'linear' loss function.") if verbose not in [0, 1, 2]: raise ValueError("`verbose` must be in [0, 1, 2].") if len(bounds) != 2: raise ValueError("`bounds` must contain 2 elements.") if max_nfev is not None and max_nfev <= 0: raise ValueError("`max_nfev` must be None or positive integer.") if np.iscomplexobj(x0): raise ValueError("`x0` must be real.") x0 = np.atleast_1d(x0).astype(float) if x0.ndim > 1: raise ValueError("`x0` must have at most 1 dimension.") lb, ub = prepare_bounds(bounds, x0.shape[0]) if method == 'lm' and not np.all((lb == -np.inf) & (ub == np.inf)): raise ValueError("Method 'lm' doesn't support bounds.") if lb.shape != x0.shape or ub.shape != x0.shape: raise ValueError("Inconsistent shapes between bounds and `x0`.") if np.any(lb >= ub): raise ValueError("Each lower bound must be strictly less than each " "upper bound.") if not in_bounds(x0, lb, ub): raise ValueError("`x0` is infeasible.") x_scale = check_x_scale(x_scale, x0) ftol, xtol, gtol = check_tolerance(ftol, xtol, gtol, method) def fun_wrapped(x): return np.atleast_1d(fun(x, *args, **kwargs)) if method == 'trf': x0 = make_strictly_feasible(x0, lb, ub) f0 = fun_wrapped(x0) if f0.ndim != 1: raise ValueError("`fun` must return at most 1-d array_like. " "f0.shape: {0}".format(f0.shape)) if not np.all(np.isfinite(f0)): raise ValueError("Residuals are not finite in the initial point.") n = x0.size m = f0.size if method == 'lm' and m < n: raise ValueError("Method 'lm' doesn't work when the number of " "residuals is less than the number of variables.") loss_function = construct_loss_function(m, loss, f_scale) if callable(loss): rho = loss_function(f0) if rho.shape != (3, m): raise ValueError("The return value of `loss` callable has wrong " "shape.") initial_cost = 0.5 * np.sum(rho[0]) elif loss_function is not None: initial_cost = loss_function(f0, cost_only=True) else: initial_cost = 0.5 * np.dot(f0, f0) if callable(jac): J0 = jac(x0, *args, **kwargs) if issparse(J0): J0 = J0.tocsr() def jac_wrapped(x, _=None): return jac(x, *args, **kwargs).tocsr() elif isinstance(J0, LinearOperator): def jac_wrapped(x, _=None): return jac(x, *args, **kwargs) else: J0 = np.atleast_2d(J0) def jac_wrapped(x, _=None): return np.atleast_2d(jac(x, *args, **kwargs)) else: # Estimate Jacobian by finite differences. if method == 'lm': if jac_sparsity is not None: raise ValueError("method='lm' does not support " "`jac_sparsity`.") if jac != '2-point': warn("jac='{0}' works equivalently to '2-point' " "for method='lm'.".format(jac)) J0 = jac_wrapped = None else: if jac_sparsity is not None and tr_solver == 'exact': raise ValueError("tr_solver='exact' is incompatible " "with `jac_sparsity`.") jac_sparsity = check_jac_sparsity(jac_sparsity, m, n) def jac_wrapped(x, f): J = approx_derivative(fun, x, rel_step=diff_step, method=jac, f0=f, bounds=bounds, args=args, kwargs=kwargs, sparsity=jac_sparsity) if J.ndim != 2: # J is guaranteed not sparse. J = np.atleast_2d(J) return J J0 = jac_wrapped(x0, f0) if J0 is not None: if J0.shape != (m, n): raise ValueError( "The return value of `jac` has wrong shape: expected {0}, " "actual {1}.".format((m, n), J0.shape)) if not isinstance(J0, np.ndarray): if method == 'lm': raise ValueError("method='lm' works only with dense " "Jacobian matrices.") if tr_solver == 'exact': raise ValueError( "tr_solver='exact' works only with dense " "Jacobian matrices.") jac_scale = isinstance(x_scale, str) and x_scale == 'jac' if isinstance(J0, LinearOperator) and jac_scale: raise ValueError("x_scale='jac' can't be used when `jac` " "returns LinearOperator.") if tr_solver is None: if isinstance(J0, np.ndarray): tr_solver = 'exact' else: tr_solver = 'lsmr' if method == 'lm': result = call_minpack(fun_wrapped, x0, jac_wrapped, ftol, xtol, gtol, max_nfev, x_scale, diff_step) elif method == 'trf': result = trf(fun_wrapped, jac_wrapped, x0, f0, J0, lb, ub, ftol, xtol, gtol, max_nfev, x_scale, loss_function, tr_solver, tr_options.copy(), verbose) elif method == 'dogbox': if tr_solver == 'lsmr' and 'regularize' in tr_options: warn("The keyword 'regularize' in `tr_options` is not relevant " "for 'dogbox' method.") tr_options = tr_options.copy() del tr_options['regularize'] result = dogbox(fun_wrapped, jac_wrapped, x0, f0, J0, lb, ub, ftol, xtol, gtol, max_nfev, x_scale, loss_function, tr_solver, tr_options, verbose) result.message = TERMINATION_MESSAGES[result.status] result.success = result.status > 0 if verbose >= 1: print(result.message) print("Function evaluations {0}, initial cost {1:.4e}, final cost " "{2:.4e}, first-order optimality {3:.2e}." .format(result.nfev, initial_cost, result.cost, result.optimality)) return result
bsd-3-clause
chrisburr/scikit-learn
examples/linear_model/plot_sgd_loss_functions.py
73
1232
""" ========================== SGD: convex loss functions ========================== A plot that compares the various convex loss functions supported by :class:`sklearn.linear_model.SGDClassifier` . """ print(__doc__) import numpy as np import matplotlib.pyplot as plt def modified_huber_loss(y_true, y_pred): z = y_pred * y_true loss = -4 * z loss[z >= -1] = (1 - z[z >= -1]) ** 2 loss[z >= 1.] = 0 return loss xmin, xmax = -4, 4 xx = np.linspace(xmin, xmax, 100) lw = 2 plt.plot([xmin, 0, 0, xmax], [1, 1, 0, 0], color='gold', lw=lw, label="Zero-one loss") plt.plot(xx, np.where(xx < 1, 1 - xx, 0), color='teal', lw=lw, label="Hinge loss") plt.plot(xx, -np.minimum(xx, 0), color='yellowgreen', lw=lw, label="Perceptron loss") plt.plot(xx, np.log2(1 + np.exp(-xx)), color='cornflowerblue', lw=lw, label="Log loss") plt.plot(xx, np.where(xx < 1, 1 - xx, 0) ** 2, color='orange', lw=lw, label="Squared hinge loss") plt.plot(xx, modified_huber_loss(xx, 1), color='darkorchid', lw=lw, linestyle='--', label="Modified Huber loss") plt.ylim((0, 8)) plt.legend(loc="upper right") plt.xlabel(r"Decision function $f(x)$") plt.ylabel("$L(y, f(x))$") plt.show()
bsd-3-clause
WilliamDiakite/ExperimentationsACA
processing/lsa.py
1
3364
import os import sys import itertools import operator import nltk import numpy as np import matplotlib.pyplot as plt from nltk.util import ngrams from collections import Counter from spell_checker import SpellChecker from sklearn.decomposition import TruncatedSVD from sklearn.pipeline import make_pipeline from sklearn.preprocessing import Normalizer sys.path.insert(0, '/Users/diakite_w/Documents/Dev/ExperimentationsACA/FrenchLefffLemmatizer') from FrenchLefffLemmatizer import FrenchLefffLemmatizer def extract_ngrams(documents, n): ''' Return list of n-grams ''' chained_documents = list(itertools.chain.from_iterable(documents)) return Counter(ngrams(chained_documents, n)) def tokenize(text): fll = FrenchLefffLemmatizer() splck = SpellChecker() contracted_pronouns = ["l'", "m'", "n'", "d'", "c'", "j'", "qu'", "s'"] dictionnary = [] stopwords = [w.rstrip() for w in open('stopwords-fr.txt')] # Put everything to lower case text = text.lower() # Tokenize text tokens = nltk.tokenize.word_tokenize(text) print('Nombre de tokens dans le texte :', len(tokens)) #tokens = [splck.correct(t) if t not in dictionnary else t for t in tokens] # Remove contacted pronous from tokens tokens = [t[2:] if t[:2] in contracted_pronouns else t for t in tokens] tokens = [t for t in tokens if len(t) > 2] tokens = [t for t in tokens if t not in stopwords] tokens = [fll.lemmatize(t) for t in tokens] print('Nombre de tokens apres traitement :', len(tokens), '\n') return tokens def tokens_to_vec(tokens): vec = np.zeros(len(word_index_map)) for token in tokens: idx = word_index_map[token] vec[idx] = 1 return vec def read_txt(textfile): with open(textfile, 'r') as f: text = f.read() text = text.replace('\n', ' ') text = text.replace('- ', '') text = text.replace('.', '') text = text.replace('-', '') text = text.replace("‘l'", 'ï') return text def get_all_doc(directory): ''' Read all txt documents and append them in string ''' documents = [] counter = 1 for filename in os.listdir(directory): if filename.endswith('.txt'): print('\n[...] Reading document', counter) filename = 'data/' + filename documents.append(read_txt(filename)) counter += 1 return documents documents = get_all_doc('data/') all_tokens = [tokenize(doc) for doc in documents] vocabulary = list(set(itertools.chain.from_iterable(all_tokens))) print ('\nVocab size:', len(vocabulary)) # Computing n-grams bigrams = extract_ngrams(all_tokens, 2) trigrams = extract_ngrams(all_tokens, 3) [print(t) for t in trigrams.most_common(5)] print('\n') [print(t) for t in bigrams.most_common(10)] ''' # Key: word - value: index word_index_map = {j: i for i, j in enumerate(vocabulary)} # Key: index - value: word index_word_map = sorted(word_index_map.items(), key=operator.itemgetter(1)) index_word_map = [t[0] for t in index_word_map] N = len(documents) D = len(word_index_map) X = np.zeros((D,N)) i = 0 for tokens in all_tokens: X[:,i] = tokens_to_vec(tokens) i += 1 print(X.shape) svd = TruncatedSVD() Z = svd.fit_transform(X) print('Z shape', Z.shape) plt.scatter(Z[:,0], Z[:,1]) print('D:', D) for i in range(D): plt.annotate(s=index_word_map[i], xy=(Z[i,0], Z[i,1])) plt.show() '''
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
pglomski/shopnotes
drill_speed_chart.py
1
2778
#!/usr/bin/env python # -*- coding: UTF-8 -*- '''Produce a custom twist drill plot''' import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt plt.rc('text', usetex=True) # set some rcParams mpl.rcParams['font.weight'] = 'bold' mpl.rcParams['xtick.major.pad'] = 10 mpl.rcParams['xtick.direction'] = 'inout' mpl.rcParams['xtick.labelsize'] = 26 mpl.rcParams['ytick.direction'] = 'inout' mpl.rcParams['ytick.labelsize'] = 20 # define the constants for our chart materials = [ ('Acrylic' , 650 , 'c' , '-' ) , ('Aluminum' , 300 , 'b' , '-' ) , ('Brass' , 200 , 'g' , '-' ) , ('LC Steel' , 110 , 'k' , '-' ) , ('Wood' , 100 , 'brown' , '-' ) , ('MC Steel' , 80 , 'darkgray' , '-' ) , ('HC Steel' , 60 , 'lightgray' , '-' ) , ('Stainless' , 50 , 'purple' , '-' ) , ] drill_speeds = [250, 340, 390, 510, 600, 650, 990, 1550, 1620, 1900, 2620, 3100] #rpm speed_lims = (200., 4000.) # rpm max_in = 1. # in. incr = 1./16. # in. im_sz = 25. # in. ratio = 8.5/11. fig = plt.figure(figsize=(im_sz,ratio * im_sz), dpi=600) fig.patch.set_alpha(0) # generate a vector of drill bit diameter x = np.array([float(i) * incr for i in range(1,int(max_in/incr) + 1)]) # in. # calculate the drill speed curve for each material type and plot the curve for name, speed, color, linestyle in materials: plt.loglog(x, 12/np.pi/x*speed, label=name, linewidth=5, color=color, linestyle=linestyle) ax = plt.gca() # adjust the axis tick locators to match drill press speeds ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(drill_speeds)) ax.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%4d')) ax.yaxis.set_minor_locator(mpl.ticker.NullLocator()) ax.set_ylim(speed_lims) # set the drill diameter locators and format the ticks with LaTeX ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(base=incr)) ax.xaxis.set_minor_locator(mpl.ticker.NullLocator()) ax.set_xlim((incr, max_in)) ticks = ['0', r'$$\frac{1}{16}$$' , r'$$\frac{1}{8}$$' , r'$$\frac{3}{16}$$' , r'$$\frac{1}{4}$$' , r'$$\frac{5}{16}$$' , r'$$\frac{3}{8}$$' , r'$$\frac{7}{16}$$' , r'$$\frac{1}{2}$$' , r'$$\frac{9}{16}$$' , r'$$\frac{5}{8}$$' , r'$$\frac{11}{16}$$' , r'$$\frac{3}{4}$$' , r'$$\frac{13}{16}$$' , r'$$\frac{7}{8}$$' , r'$$\frac{15}{16}$$' , r'$$1$$' ] ax.xaxis.set_ticklabels(ticks) # Add the Texts plt.xlabel('Bit Diameter (in.)', fontsize=26) plt.ylabel('Drill Speed (rpm)' , fontsize=26) plt.title('Twist Drill Speeds' , fontsize=50) plt.legend(ncol=2, loc=3, fontsize=40) plt.grid('on') plt.savefig('drill_speed_chart.png')
agpl-3.0
raghavrv/scikit-learn
examples/decomposition/plot_pca_iris.py
49
1511
#!/usr/bin/python # -*- coding: utf-8 -*- """ ========================================================= PCA example with Iris Data-set ========================================================= Principal Component Analysis applied to the Iris dataset. See `here <https://en.wikipedia.org/wiki/Iris_flower_data_set>`_ for more information on this dataset. """ print(__doc__) # Code source: Gaël Varoquaux # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn import decomposition from sklearn import datasets np.random.seed(5) centers = [[1, 1], [-1, -1], [1, -1]] iris = datasets.load_iris() X = iris.data y = iris.target fig = plt.figure(1, figsize=(4, 3)) plt.clf() ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134) plt.cla() pca = decomposition.PCA(n_components=3) pca.fit(X) X = pca.transform(X) for name, label in [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]: ax.text3D(X[y == label, 0].mean(), X[y == label, 1].mean() + 1.5, X[y == label, 2].mean(), name, horizontalalignment='center', bbox=dict(alpha=.5, edgecolor='w', facecolor='w')) # Reorder the labels to have colors matching the cluster results y = np.choose(y, [1, 2, 0]).astype(np.float) ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.spectral, edgecolor='k') ax.w_xaxis.set_ticklabels([]) ax.w_yaxis.set_ticklabels([]) ax.w_zaxis.set_ticklabels([]) plt.show()
bsd-3-clause
LiaoPan/scikit-learn
examples/applications/plot_outlier_detection_housing.py
243
5577
""" ==================================== Outlier detection on a real data set ==================================== This example illustrates the need for robust covariance estimation on a real data set. It is useful both for outlier detection and for a better understanding of the data structure. We selected two sets of two variables from the Boston housing data set as an illustration of what kind of analysis can be done with several outlier detection tools. For the purpose of visualization, we are working with two-dimensional examples, but one should be aware that things are not so trivial in high-dimension, as it will be pointed out. In both examples below, the main result is that the empirical covariance estimate, as a non-robust one, is highly influenced by the heterogeneous structure of the observations. Although the robust covariance estimate is able to focus on the main mode of the data distribution, it sticks to the assumption that the data should be Gaussian distributed, yielding some biased estimation of the data structure, but yet accurate to some extent. The One-Class SVM algorithm First example ------------- The first example illustrates how robust covariance estimation can help concentrating on a relevant cluster when another one exists. Here, many observations are confounded into one and break down the empirical covariance estimation. Of course, some screening tools would have pointed out the presence of two clusters (Support Vector Machines, Gaussian Mixture Models, univariate outlier detection, ...). But had it been a high-dimensional example, none of these could be applied that easily. Second example -------------- The second example shows the ability of the Minimum Covariance Determinant robust estimator of covariance to concentrate on the main mode of the data distribution: the location seems to be well estimated, although the covariance is hard to estimate due to the banana-shaped distribution. Anyway, we can get rid of some outlying observations. The One-Class SVM is able to capture the real data structure, but the difficulty is to adjust its kernel bandwidth parameter so as to obtain a good compromise between the shape of the data scatter matrix and the risk of over-fitting the data. """ print(__doc__) # Author: Virgile Fritsch <virgile.fritsch@inria.fr> # License: BSD 3 clause import numpy as np from sklearn.covariance import EllipticEnvelope from sklearn.svm import OneClassSVM import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn.datasets import load_boston # Get data X1 = load_boston()['data'][:, [8, 10]] # two clusters X2 = load_boston()['data'][:, [5, 12]] # "banana"-shaped # Define "classifiers" to be used classifiers = { "Empirical Covariance": EllipticEnvelope(support_fraction=1., contamination=0.261), "Robust Covariance (Minimum Covariance Determinant)": EllipticEnvelope(contamination=0.261), "OCSVM": OneClassSVM(nu=0.261, gamma=0.05)} colors = ['m', 'g', 'b'] legend1 = {} legend2 = {} # Learn a frontier for outlier detection with several classifiers xx1, yy1 = np.meshgrid(np.linspace(-8, 28, 500), np.linspace(3, 40, 500)) xx2, yy2 = np.meshgrid(np.linspace(3, 10, 500), np.linspace(-5, 45, 500)) for i, (clf_name, clf) in enumerate(classifiers.items()): plt.figure(1) clf.fit(X1) Z1 = clf.decision_function(np.c_[xx1.ravel(), yy1.ravel()]) Z1 = Z1.reshape(xx1.shape) legend1[clf_name] = plt.contour( xx1, yy1, Z1, levels=[0], linewidths=2, colors=colors[i]) plt.figure(2) clf.fit(X2) Z2 = clf.decision_function(np.c_[xx2.ravel(), yy2.ravel()]) Z2 = Z2.reshape(xx2.shape) legend2[clf_name] = plt.contour( xx2, yy2, Z2, levels=[0], linewidths=2, colors=colors[i]) legend1_values_list = list( legend1.values() ) legend1_keys_list = list( legend1.keys() ) # Plot the results (= shape of the data points cloud) plt.figure(1) # two clusters plt.title("Outlier detection on a real data set (boston housing)") plt.scatter(X1[:, 0], X1[:, 1], color='black') bbox_args = dict(boxstyle="round", fc="0.8") arrow_args = dict(arrowstyle="->") plt.annotate("several confounded points", xy=(24, 19), xycoords="data", textcoords="data", xytext=(13, 10), bbox=bbox_args, arrowprops=arrow_args) plt.xlim((xx1.min(), xx1.max())) plt.ylim((yy1.min(), yy1.max())) plt.legend((legend1_values_list[0].collections[0], legend1_values_list[1].collections[0], legend1_values_list[2].collections[0]), (legend1_keys_list[0], legend1_keys_list[1], legend1_keys_list[2]), loc="upper center", prop=matplotlib.font_manager.FontProperties(size=12)) plt.ylabel("accessibility to radial highways") plt.xlabel("pupil-teacher ratio by town") legend2_values_list = list( legend2.values() ) legend2_keys_list = list( legend2.keys() ) plt.figure(2) # "banana" shape plt.title("Outlier detection on a real data set (boston housing)") plt.scatter(X2[:, 0], X2[:, 1], color='black') plt.xlim((xx2.min(), xx2.max())) plt.ylim((yy2.min(), yy2.max())) plt.legend((legend2_values_list[0].collections[0], legend2_values_list[1].collections[0], legend2_values_list[2].collections[0]), (legend2_values_list[0], legend2_values_list[1], legend2_values_list[2]), loc="upper center", prop=matplotlib.font_manager.FontProperties(size=12)) plt.ylabel("% lower status of the population") plt.xlabel("average number of rooms per dwelling") plt.show()
bsd-3-clause
LarsDu/DeepNuc
deepnuc/nucbinaryclassifier.py
2
15464
import tensorflow as tf import numpy as np import sklearn.metrics as metrics #from databatcher import DataBatcher import nucconvmodel #import dubiotools as dbt import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import pprint from itertools import cycle import os import sys #Logging imports from logger import Logger from nucinference import NucInference from collections import OrderedDict class NucBinaryClassifier(NucInference): use_onehot_labels = True def __init__(self, sess, train_batcher, test_batcher, num_epochs, learning_rate, batch_size, seq_len, save_dir, keep_prob=0.5, beta1=0.9, concat_revcom_input=False, nn_method_key="inferenceA", pos_index=1): """NucBinaryClassifier encapsulates training and data evaluation for :param sess: tf.Session() object :param train_batcher: DataBatcher object for training set :param test_batcher: DataBatcher object for test set :param num_epochs: Number of epoch cycles to perform training :param learning_rate: Learning rate :param batch_size: Mini-batch pull size :param seq_len: Sequence length :param save_dir: Root save directory for binary classification model :param keep_prob: Probability of keeping weight for dropout regularization :param beta1: Beta1 parameter for AdamOptimizer :param concat_revcom_input: If true, concatenate reverse complement of nucleotide sequence to input vector :param nn_method_key: Dictionary key for inference method found in nucconvmodels.py file. Determines which model to use. Example: "inferenceA" will run nucconvmodels.inferenceA :param pos_index: The index to use for the positive class (defaults to 1) :returns: a NucBinaryClassifier object :rtype: NucBinaryClassifier """ super(NucBinaryClassifier, self).__init__(sess, train_batcher, test_batcher, num_epochs, learning_rate, batch_size, seq_len, save_dir, keep_prob, beta1, concat_revcom_input, nn_method_key="inferenceA") if self.train_batcher.num_classes != 2: print "Error, more than two classes detected in train batcher" else: self.num_classes = 2 #The index for the label that should be considered the positive class self.pos_index=pos_index self.save_on_epoch = 5 def build_model(self): self.dna_seq_placeholder = tf.placeholder(tf.float32, shape=[None,self.seq_len,4], name="dna_seq") self.labels_placeholder = tf.placeholder(tf.float32, shape=[None, self.num_classes], name="labels") self.keep_prob_placeholder = tf.placeholder(tf.float32,name="keep_prob") self.logits, self.network = self.nn_method(self.dna_seq_placeholder, self.keep_prob_placeholder, self.num_classes) self.probs = tf.nn.softmax(self.logits) self.loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.labels_placeholder, logits=self.logits)) ''' Calculate metrics. num_true positives is the number of true positives for the current batch Table below shows index if tf.argmax is applied +-----+-----------+---------+ | | Classifier| Label | +-----+-----------+---------+ | TP | 1 | 1 | +-----+-----------+---------+ | FP | 1 | 0 | +-----+-----------+---------+ | TN | 0 | 0 | +-----+-----------+---------+ | FN | 0 | 1 | +-----+-----------+---------+ Precision = TP/(TP+FP) Recall = TP/(TP+FN) F1-score = 2*(Prec*Rec)/(Prec+Rec) # Note: I ended up not using the tp,fp,tn,fn ops because I ended up calculating # these metrics using sklearn. ''' #correct = TN+TP #Used for calculating accuracy self.logits_ind = tf.argmax(self.logits,1) self.labels_ind = tf.argmax(self.labels_placeholder,1) #Create max_mask of logits (ie: [-.5,.5] --> [0 1]. Note logits have # shape [batch_size * num_classes= 2] #self.inverse_logits_col = tf.ones_like(self.logits_ind) - self.logits_ind #self.max_mask_logits = tf.concat([self.inverse_logits_col,self.logits_ind],1) #True positives where logits_ind+labels_ind == 2 #True negatives where logits_ind+labels_ind == 0 self.sum_ind = tf.add(self.logits_ind,self.labels_ind) self.true_positives = tf.equal(self.sum_ind,2*tf.ones_like(self.sum_ind)) #bool self.num_true_positives =tf.reduce_sum(tf.cast(self.true_positives, tf.int32)) #For FP classifier index > label index self.false_positives=tf.greater(self.logits_ind,self.labels_ind) self.num_false_positives = tf.reduce_sum(tf.cast(self.false_positives, tf.int32)) self.true_negatives = tf.equal(self.sum_ind,tf.zeros_like(self.sum_ind)) #bool self.num_true_negatives= tf.reduce_sum(tf.cast(self.true_negatives,tf.int32)) #For FN classifier index < label index self.false_negatives=tf.less(self.logits_ind,self.labels_ind) self.num_false_negatives = tf.reduce_sum(tf.cast(self.false_negatives,tf.int32)) #num correct can be used to calculate accuracy self.correct = tf.equal(self.logits_ind,self.labels_ind) self.num_correct= tf.reduce_sum(tf.cast(self.correct, tf.int32)) self.relevance =self.network.relevance_backprop(tf.multiply(self.logits, self.labels_placeholder)) '''Write and consolidate summaries''' self.loss_summary = tf.summary.scalar('loss',self.loss) self.summary_writer = tf.summary.FileWriter(self.summary_dir,self.sess.graph) self.summary_op = tf.summary.merge([self.loss_summary]) #Note: Do not use tf.summary.merge_all() here. This will break encapsulation for # cross validation and lead to crashes when training multiple models # Add gradient ops to graph with learning rate self.train_op = tf.train.AdamOptimizer(self.learning_rate, beta1=self.beta1).minimize(self.loss) self.vars = tf.trainable_variables() self.var_names = [var.name for var in self.vars] #print "Trainable variables:\n" #for vname in self.var_names: # print vname self.saver = tf.train.Saver() self.init_op = tf.global_variables_initializer() #Important note: Restoring model does not require init_op. #In fact calling tf.global_variables_initializer() after loading a model #will overwrite loaded weights self.sess.run(self.init_op) self.load(self.checkpoint_dir) def eval_model_metrics(self, batcher, save_plots=False, image_name ='metrics.png', eval_batch_size=50): """ Note: This method only works for binary classification as auPRC and auROC graphs only apply to binary classificaton problems. TODO: Modify this code to perform auROC generation for one-vs-all in the case of multiclass classification. """ #Ref: http://scikit-learn.org/stable/modules/model_evaluation.html#roc-metrics ##auROC calculations #Keep batch size at 1 for now to ensure 1 full epoch is evaluated all_labels = np.zeros((batcher.num_records,self.num_classes), dtype = np.float32) all_probs = np.zeros((batcher.num_records,self.num_classes), dtype = np.float32) #num_correct = 0 #counts number of correct predictions num_whole_pulls = batcher.num_records//eval_batch_size num_single_pulls = batcher.num_records%eval_batch_size num_steps = num_whole_pulls+num_single_pulls for i in range(num_steps): if i<num_whole_pulls: batch_size=eval_batch_size else: batch_size=1 labels_batch, dna_seq_batch = batcher.pull_batch(batch_size) feed_dict = { self.dna_seq_placeholder:dna_seq_batch, self.labels_placeholder:labels_batch, self.keep_prob_placeholder:1.0 } cur_prob= self.sess.run(self.probs,feed_dict=feed_dict) #Fill labels array if batch_size > 1: start_ind = batch_size*i elif batch_size == 1: start_ind = num_whole_pulls*eval_batch_size+(i-num_whole_pulls) else: print "Never reach this condition" all_labels[start_ind:start_ind+batch_size,:] = labels_batch all_probs[start_ind:start_ind+batch_size,:] = cur_prob #Calculate metrics and save results in a dict md = self.calc_classifier_metrics(all_labels,all_probs) md["epoch"]=self.epoch md["step"]=self.step #print "Testing accuracy",float(num_correct)/float(batcher.num_records) print 'Num examples: %d Num correct: %d Accuracy: %0.04f' % \ (batcher.num_records, md["num_correct"], md["accuracy"])+'\n' if save_plots: ###Plot some metrics plot_colors = cycle(['cyan','blue','orange','teal']) #print "Labels shape",all_labels.shape #print "Probs shape",all_probs.shape #print "Preds shape",all_preds.shape #Generate auROC plot axes fig1,ax1 = plt.subplots(2) fig1.subplots_adjust(bottom=0.2) ax1[0].plot([0,1],[0,1],color='navy',lw=2,linestyle='--') ax1[0].set_xbound(0.0,1.0) ax1[0].set_ybound(0.0,1.05) ax1[0].set_xlabel('False Positive Rate') ax1[0].set_ylabel('True Positive Rate') ax1[0].set_title('auROC') #plt.legend(loc='lower right') ax1[0].plot(md["fpr"],md["tpr"],color=plot_colors.next(), lw=2,linestyle='-',label='auROC curve (area=%0.2f)' % md["auroc"] ) #Generate auPRC plot axes #ax1[1].plot([0,1],[1,1],color='royalblue',lw=2,linestyle='--') ax1[1].set_xlabel('Precision') ax1[1].set_ylabel('Recall') ax1[1].set_title('auPRC') ax1[1].plot(md["thresh_precision"],md["thresh_recall"],color=plot_colors.next(), lw=2,linestyle='-',label='auPRC curve (area=%0.2f)' % md["auprc"] ) ax1[1].set_xbound(0.0,1.0) ax1[1].set_ybound(0.0,1.05) #Note: avg prec score is the area under the prec recall curve #Note: Presumably class 1 (pos examples) should be the only f1 score we focus on #print "F1 score for class",i,"is",f1_score plt.tight_layout() plt_fname = self.save_dir+os.sep+image_name print "Saving auROC image to",plt_fname fig1.savefig(plt_fname) #Return metrics dictionary return md def calc_classifier_metrics(self,all_labels,all_probs): """Calculate some metrics for the dataset return dictionary with metrics :param all_probs: nx2 prob values :param all_labels: nx2 labels :returns: dictionary of metrics :rtype: dict() """ num_records = all_probs.shape[0] all_preds = np.zeros((num_records, self.num_classes),dtype = np.float32) all_preds[np.arange(num_records),all_probs.argmax(1)] = 1 #Calculate accuracy num_correct = metrics.accuracy_score(all_labels[:,self.pos_index],all_preds[:,self.pos_index],normalize=False) accuracy = num_correct/float(all_preds.shape[0]) ###Calculate auROC #http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html #metrics.roc_curve(y_true, y_score[, ...]) #y_score is probs fpr,tpr,_ = metrics.roc_curve(all_labels[:,self.pos_index], all_probs[:,self.pos_index], pos_label=self.pos_index) auroc = metrics.auc(fpr,tpr) thresh_precision,thresh_recall,prc_thresholds = metrics.precision_recall_curve( all_labels[:,self.pos_index], all_probs[:,self.pos_index]) #Calculate precision, recall, and f1-score for threshold = 0.5 #confusion_matrix = metrics.confusion_matrix(all_labels[:,self.pos_index],all_probs[:,self.pos_index]) precision, recall, f1_score, support = metrics.precision_recall_fscore_support( all_labels[:,self.pos_index], all_preds[:,self.pos_index], pos_label=self.pos_index) precision = precision[self.pos_index] recall = recall[self.pos_index] f1_score = f1_score[self.pos_index] support = support[self.pos_index] auprc = metrics.average_precision_score(all_labels[:,self.pos_index], all_probs[:,self.pos_index]) return OrderedDict([ ("num_correct",num_correct), ("accuracy",accuracy), ("auroc",auroc), ("auprc",auprc), ("fpr",fpr), ("tpr",tpr), ("precision",precision), ("recall",recall), ("f1_score",f1_score), ("support",support), ("thresh_precision",thresh_precision), ("thresh_recall",thresh_recall), ("prc_thresholds",prc_thresholds) ])
gpl-3.0
Pragmatismo/TimelapsePi-EasyControl
webcamcap_show_numpy.py
1
8684
#!/usr/bin/python import time import os import sys import pygame import numpy from PIL import Image, ImageDraw, ImageChops print("") print("") print(" USE l=3 to take a photo every 3 somethings, try a 1000 or 2") print(" t to take triggered photos ") print(" cap=/home/pi/folder/ to set caps path other than current dir") print(" ") pi_paper = False #updates pi wall paper, use -nopaper to turn it off. s_val = "10" c_val = "2" g_val = "10" b_val = "15" x_dim = 1600 y_dim = 896 additonal_commands = "-d/dev/video1 -w" try: cappath = os.getcwd() cappath += "/" except: print(" COULD NOT GET CURRENT DIR SET WITH A FLAG ") cappath = "./" print(" COULD NOT GET CURRENT DIR SET WITH A FLAG ") loc_settings = "./camera_settings.txt" try: with open(loc_settings, "r") as f: for line in f: s_item = line.split("=") if s_item[0] == "s_val": s_val = s_item[1].split("\n")[0] elif s_item[0] == "c_val": c_val = s_item[1].split("\n")[0] elif s_item[0] == "g_val": g_val = s_item[1].split("\n")[0] elif s_item[0] == "b_val": b_val = s_item[1].split("\n")[0] elif s_item[0] == "x_dim": x_dim = s_item[1].split("\n")[0] elif s_item[0] == "y_dim": y_dim = s_item[1].split("\n")[0] elif s_item[0] == "additonal_commands": additonal_commands = s_item[1].split("\n")[0] except: print("No config file for camera, using default") print("Run cam_config.py to create one") def photo(): # take and save photo timenow = time.time() timenow = str(timenow)[0:10] filename= "cap_"+str(timenow)+".jpg" #os.system("uvccapture "+additonal_commands+" -S"+s_val+" -C" + c_val + " -G"+ g_val +" -B"+ b_val +" -x"+str(x_dim)+" -y"+str(y_dim)+" -v -t0 -o"+cappath+filename) cmd = str("uvccapture "+additonal_commands+" -x"+str(x_dim)+" -y"+str(y_dim)+" -v -t0 -o"+cappath+filename) print("####") print("####") print cmd print("####") print("####") os.system(cmd) print("Image taken and saved to "+cappath+filename) if pi_paper == True: os.system("export DISPLAY=:0 && pcmanfm --set-wallpaper "+cappath+filename) return filename if 'wp' in sys.argv or 'wallpaper' in sys.argv: pi_paper = True print(" Going to try changing wall paper") loop = False trig = False for argu in sys.argv[1:]: try: thearg = str(argu).split('=')[0] except: thearg = str(argu) if thearg == 'cap' or thearg =='cappath': cappath = str(argu).split('=')[1] elif thearg == 'l' or thearg == 'looped': try: num = int(str(argu).split('=')[1]) except: print("No speed supplied, taking every 10") num = 10 loop = True elif thearg == 't' or thearg == 'TRIGGERED': trig = True print(" Saving files to, " + str(cappath)) pygame.init() display_width = x_dim display_height = y_dim gameDisplay = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('Most recent image') black = (0,0,0) white = (255,255,255) clock = pygame.time.Clock() crashed = False import matplotlib.pyplot as plt def show_pic(imgtaken, x=0,y=0): gameDisplay.blit(imgtaken, (x,y)) gameDisplay.fill(white) c_photo = photo() pil_c_photo = Image.open(c_photo) numpy_pic = numpy.array(pil_c_photo) b_photo = photo() pil_b_photo = Image.open(b_photo) numpy_pic_b = numpy.array(pil_b_photo) mask = numpy_pic_b > numpy_pic + 30 #the +30 gets rid of noise mask2 = numpy_pic_b < numpy_pic - 30 lol = mask + mask2 e_pic = numpy_pic.copy() num = 0 while not crashed: for event in pygame.event.get(): if event.type == pygame.QUIT: crashed = True timenow = time.time() e_photo = str(timenow).split(".")[0] e_photo= "numpy_"+str(timenow)+".jpg" num = num + 1 b_photo = c_photo c_photo = photo() numpy_pic_b = numpy_pic.copy() pil_c_photo = Image.open(c_photo) numpy_pic = numpy.array(pil_c_photo) print numpy_pic.size #print len(numpy_pic[3]) print "###" #print numpy_pic[1:,1,1] #a = np.arange(100) print "##########" #numpy_pic[1:500, range(0, len(numpy_pic[2]), 10), 1] = 0 #for x in numpy_pic[1:500, range(0, len(numpy_pic[2])), 1]: # if x >= 100: # x = 255 #for x in range(10,170,10): # mask = numpy_pic < x # numpy_pic[mask] = 255-x #numpy_pic[mask] + numpy_pic[mask] #for x in range(200,255,5): # mask = numpy_pic > x # numpy_pic[mask] = 0+(x/10) # numpy_pic[mask] / numpy_pic[mask]+(numpy_pic[mask]/numpy_pic[mask]) #print numpy_pic[1:,1,1] #print numpy_pic.min() print "###" #print numpy_pic.shape #Array dimensions #print numpy_pic.ndim #Number of array dimensions #print numpy_pic.dtype #Data type of array elements #print numpy_pic.dtype.name #Name of data type #print numpy_pic.mean() #print numpy_pic.max() #print numpy_pic.min() #print numpy.info(numpy.ndarray.dtype) #print numpy_pic.astype(int) #mask = numpy_pic > numpy_pic_b #mask = numpy_pic[:, :, 2] > 150 #numpy_pic[mask] = [0, 0, 255] #lol = numpy_pic + #mask = numpy_pic_b > numpy_pic + 30 #the +30 gets rid of noise #mask2 = numpy_pic_b < numpy_pic - 30 margin = 20 maskr = numpy_pic[:, :, 0] < numpy_pic_b[:, :, 0] - margin maskg = numpy_pic[:, :, 1] < numpy_pic_b[:, :, 1] - margin maskb = numpy_pic[:, :, 2] < numpy_pic_b[:, :, 2] - margin maskr2 = numpy_pic[:, :, 0] > numpy_pic_b[:, :, 0] + margin maskg2 = numpy_pic[:, :, 1] > numpy_pic_b[:, :, 1] + margin maskb2 = numpy_pic[:, :, 2] > numpy_pic_b[:, :, 2] + margin #numpy_pic[mask] = [0, 0, 255] #lol_old = lol #lol = mask + mask2 #lol = lol + lol_old persist = 'ohhh' if persist == 'True': numpy_pic[maskr] = [255, 0, 0] numpy_pic[maskg] = [0, 255, 0] numpy_pic[maskb] = [0, 0, 255] numpy_pic[maskb2] = [0, 0, 100] numpy_pic[maskr2] = [100, 0, 0] numpy_pic[maskg2] = [0, 100, 0] Image.fromarray(numpy_pic).save(e_photo) elif persist == 'False': old_e = e_pic e_pic = numpy_pic.copy() e_pic[maskr] = [255, 0, 0] e_pic[maskg] = [0, 255, 0] e_pic[maskb] = [0, 0, 255] e_pic[maskr2] = [100, 0, 0] e_pic[maskg2] = [0, 100, 0] e_pic[maskb2] = [0, 0, 100] show1 = 'waa' if show1 == '1': e_pic = ((e_pic/4) - (numpy_pic))*3 e_pic = e_pic / 3 + old_e / 2 elif show1 == 'tripsy': e_pic = ((e_pic/4) - (numpy_pic))*3 e_pic = e_pic - old_e / 2 elif show1 == 'waa': e_pic = ((e_pic/4) - (numpy_pic))*3 #e_pic = old_e * 0.8 + e_pic * 0.2 Image.fromarray(e_pic).save(e_photo) elif persist == 'ohhh': old_e = e_pic.copy() mask_b_pic = numpy_pic.copy() mask_d_pic = numpy_pic.copy() mask_b_pic[maskr] = [255, 255, 255] mask_b_pic[maskg] = [255, 255, 255] mask_b_pic[maskb] = [255, 255, 255] mask_d_pic[maskr2] = [0, 0, 0] mask_d_pic[maskg2] = [0, 0, 0] mask_d_pic[maskb2] = [0, 0, 0] #e_pic = e_pic/6 + old_e e_pic = [200, 200, 0] #e_pic = e_pic/2 - ((mask_d_pic) + (mask_b_pic)) #e_pic = e_pic/2 + ((mask_d_pic) + (mask_b_pic)) #choose one of the following #e_pic = mask_d_pic #shows when pixel is darker than it was #e_pic = mask_b_pic #shows when pixel is lighter than prior e_pic = mask_d_pic - mask_b_pic #black execpt for movement e_pic = mask_b_pic / (mask_d_pic / 100) #black execpt for movement #e_pic = mask_d_pic + mask_b_pic #looks odd Image.fromarray(e_pic).save(e_photo) #plt.imshow(lol) #plt.show() #Image.fromarray(numpy_pic).save(e_photo) onscreen = pygame.image.load(e_photo) gameDisplay.blit(onscreen, (0,0)) pygame.display.update() if trig == True: print("Waiting for input before taking next image...") tp = raw_input("press return to take picture; ") if tp == "q": print("---bye!") exit() clock.tick(20) if loop == True: pygame.time.wait(num) clock.tick(20) elif trig == False and loop == False: crashed = True #while True: #pygame.time.wait(1000) #clock.tick(20) pygame.quit() quit()
gpl-2.0
SMTorg/smt
smt/surrogate_models/tests/test_surrogate_model_examples.py
2
17391
""" Author: John Hwang <<hwangjt@umich.edu>> This package is distributed under New BSD license. """ import unittest import matplotlib matplotlib.use("Agg") try: from smt.surrogate_models import IDW, RBF, RMTB, RMTC compiled_available = True except: compiled_available = False class Test(unittest.TestCase): @unittest.skipIf(not compiled_available, "C compilation failed") def test_idw(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import IDW xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = IDW(p=2) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() @unittest.skipIf(not compiled_available, "C compilation failed") def test_rbf(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import RBF xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = RBF(d0=5) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() @unittest.skipIf(not compiled_available, "C compilation failed") def test_rmtb(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import RMTB xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) xlimits = np.array([[0.0, 4.0]]) sm = RMTB( xlimits=xlimits, order=4, num_ctrl_pts=20, energy_weight=1e-15, regularization_weight=0.0, ) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() @unittest.skipIf(not compiled_available, "C compilation failed") def test_rmtc(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import RMTC xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) xlimits = np.array([[0.0, 4.0]]) sm = RMTC( xlimits=xlimits, num_elements=20, energy_weight=1e-15, regularization_weight=0.0, ) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() def test_ls(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import LS xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = LS() sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() def test_qp(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import QP xt = np.array([[0.0, 1.0, 2.0, 3.0, 4.0]]).T yt = np.array([[0.2, 1.4, 1.5, 0.9, 1.0], [0.0, 1.0, 2.0, 4, 3]]).T sm = QP() sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) t1, _ = plt.plot(xt, yt[:, 0], "o", "C0") p1 = plt.plot(x, y[:, 0], "C0", label="Prediction 1") t2, _ = plt.plot(xt, yt[:, 1], "o", "C1") p2 = plt.plot(x, y[:, 1], "C1", label="Prediction 2") plt.xlabel("x") plt.ylabel("y") plt.legend() plt.show() def test_krg(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KRG xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = KRG(theta0=[1e-2]) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) # estimated variance s2 = sm.predict_variances(x) # derivative according to the first variable dydx = sm.predict_derivatives(xt, 0) fig, axs = plt.subplots(1) # add a plot with variance axs.plot(xt, yt, "o") axs.plot(x, y) axs.fill_between( np.ravel(x), np.ravel(y - 3 * np.sqrt(s2)), np.ravel(y + 3 * np.sqrt(s2)), color="lightgrey", ) axs.set_xlabel("x") axs.set_ylabel("y") axs.legend( ["Training data", "Prediction", "Confidence Interval 99%"], loc="lower right", ) plt.show() def test_mixed_int_krg(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KRG from smt.applications.mixed_integer import MixedIntegerSurrogateModel, INT xt = np.array([0.0, 2.0, 3.0]) yt = np.array([0.0, 1.5, 0.9]) # xtypes = [FLOAT, INT, (ENUM, 3), (ENUM, 2)] # FLOAT means x1 continuous # INT means x2 integer # (ENUM, 3) means x3, x4 & x5 are 3 levels of the same categorical variable # (ENUM, 2) means x6 & x7 are 2 levels of the same categorical variable sm = MixedIntegerSurrogateModel( xtypes=[INT], xlimits=[[0, 4]], surrogate=KRG(theta0=[1e-2]) ) sm.set_training_values(xt, yt) sm.train() num = 500 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) # estimated variance s2 = sm.predict_variances(x) fig, axs = plt.subplots(1) axs.plot(xt, yt, "o") axs.plot(x, y) axs.fill_between( np.ravel(x), np.ravel(y - 3 * np.sqrt(s2)), np.ravel(y + 3 * np.sqrt(s2)), color="lightgrey", ) axs.set_xlabel("x") axs.set_ylabel("y") axs.legend( ["Training data", "Prediction", "Confidence Interval 99%"], loc="lower right", ) plt.show() def test_mixed_gower_krg(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KRG from smt.applications.mixed_integer import MixedIntegerSurrogateModel from smt.applications.mixed_integer import ENUM # xtypes = [FLOAT, INT, (ENUM, 3), (ENUM, 2)] # FLOAT means x1 continuous # INT means x2 integer # (ENUM, 3) means x3, x4 & x5 are 3 levels of the same categorical variable # (ENUM, 2) means x6 & x7 are 2 levels of the same categorical variable xt = np.linspace(1.0, 5.0, 5) x_train = np.array(["%.2f" % i for i in xt], dtype=object) yt = np.array([0.0, 1.0, 1.5, 0.5, 1.0]) xlimits = [["0.0", "1.0", " 2.0", "3.0", "4.0"]] sm = MixedIntegerSurrogateModel( use_gower_distance=True, xtypes=[(ENUM, 5)], xlimits=xlimits, surrogate=KRG(theta0=[1e-2]), ) sm.set_training_values(x_train, yt) sm.train() num = 101 x = np.linspace(0, 5, num) x_pred = np.array(["%.2f" % i for i in x], dtype=object) y = sm.predict_values(x_pred) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() def test_kpls(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KPLS xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = KPLS(theta0=[1e-2]) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) # estimated variance s2 = sm.predict_variances(x) # to compute the derivative according to the first variable dydx = sm.predict_derivatives(xt, 0) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() # add a plot with variance plt.plot(xt, yt, "o") plt.plot(x, y) plt.fill_between( np.ravel(x), np.ravel(y - 3 * np.sqrt(s2)), np.ravel(y + 3 * np.sqrt(s2)), color="lightgrey", ) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction", "Confidence Interval 99%"]) plt.show() def test_kplsk(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KPLSK xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = KPLSK(theta0=[1e-2]) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) # estimated variance s2 = sm.predict_variances(x) # derivative according to the first variable dydx = sm.predict_derivatives(xt, 0) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() # add a plot with variance plt.plot(xt, yt, "o") plt.plot(x, y) plt.fill_between( np.ravel(x), np.ravel(y - 3 * np.sqrt(s2)), np.ravel(y + 3 * np.sqrt(s2)), color="lightgrey", ) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction", "Confidence Interval 99%"]) plt.show() def test_gekpls(self): import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from smt.surrogate_models import GEKPLS from smt.problems import Sphere from smt.sampling_methods import LHS # Construction of the DOE fun = Sphere(ndim=2) sampling = LHS(xlimits=fun.xlimits, criterion="m") xt = sampling(20) yt = fun(xt) # Compute the gradient for i in range(2): yd = fun(xt, kx=i) yt = np.concatenate((yt, yd), axis=1) # Build the GEKPLS model sm = GEKPLS( theta0=[1e-2], xlimits=fun.xlimits, extra_points=1, print_prediction=False ) sm.set_training_values(xt, yt[:, 0]) for i in range(2): sm.set_training_derivatives(xt, yt[:, 1 + i].reshape((yt.shape[0], 1)), i) sm.train() # Test the model X = np.arange(fun.xlimits[0, 0], fun.xlimits[0, 1], 0.25) Y = np.arange(fun.xlimits[1, 0], fun.xlimits[1, 1], 0.25) X, Y = np.meshgrid(X, Y) Z = np.zeros((X.shape[0], X.shape[1])) for i in range(X.shape[0]): for j in range(X.shape[1]): Z[i, j] = sm.predict_values( np.hstack((X[i, j], Y[i, j])).reshape((1, 2)) ) fig = plt.figure() ax = fig.gca(projection="3d") surf = ax.plot_surface(X, Y, Z) plt.show() def test_genn(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models.genn import GENN, load_smt_data # Training data lower_bound = -np.pi upper_bound = np.pi number_of_training_points = 4 xt = np.linspace(lower_bound, upper_bound, number_of_training_points) yt = xt * np.sin(xt) dyt_dxt = np.sin(xt) + xt * np.cos(xt) # Validation data number_of_validation_points = 30 xv = np.linspace(lower_bound, upper_bound, number_of_validation_points) yv = xv * np.sin(xv) dyv_dxv = np.sin(xv) + xv * np.cos(xv) # Truth model x = np.arange(lower_bound, upper_bound, 0.01) y = x * np.sin(x) # GENN genn = GENN() genn.options["alpha"] = 0.1 # learning rate that controls optimizer step size genn.options["beta1"] = 0.9 # tuning parameter to control ADAM optimization genn.options["beta2"] = 0.99 # tuning parameter to control ADAM optimization genn.options[ "lambd" ] = 0.1 # lambd = 0. = no regularization, lambd > 0 = regularization genn.options[ "gamma" ] = 1.0 # gamma = 0. = no grad-enhancement, gamma > 0 = grad-enhancement genn.options["deep"] = 2 # number of hidden layers genn.options["wide"] = 6 # number of nodes per hidden layer genn.options[ "mini_batch_size" ] = 64 # used to divide data into training batches (use for large data sets) genn.options["num_epochs"] = 20 # number of passes through data genn.options[ "num_iterations" ] = 100 # number of optimizer iterations per mini-batch genn.options["is_print"] = True # print output (or not) load_smt_data( genn, xt, yt, dyt_dxt ) # convenience function to read in data that is in SMT format genn.train() # API function to train model genn.plot_training_history() # non-API function to plot training history (to check convergence) genn.goodness_of_fit( xv, yv, dyv_dxv ) # non-API function to check accuracy of regression y_pred = genn.predict_values( x ) # API function to predict values at new (unseen) points # Plot fig, ax = plt.subplots() ax.plot(x, y_pred) ax.plot(x, y, "k--") ax.plot(xv, yv, "ro") ax.plot(xt, yt, "k+", mew=3, ms=10) ax.set(xlabel="x", ylabel="y", title="GENN") ax.legend(["Predicted", "True", "Test", "Train"]) plt.show() def test_mgp(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import MGP from smt.sampling_methods import LHS # Construction of the DOE dim = 3 def fun(x): import numpy as np res = ( np.sum(x, axis=1) ** 2 - np.sum(x, axis=1) + 0.2 * (np.sum(x, axis=1) * 1.2) ** 3 ) return res sampling = LHS(xlimits=np.asarray([(-1, 1)] * dim), criterion="m") xt = sampling(8) yt = np.atleast_2d(fun(xt)).T # Build the MGP model sm = MGP( theta0=[1e-2], print_prediction=False, n_comp=1, ) sm.set_training_values(xt, yt[:, 0]) sm.train() # Get the transfert matrix A emb = sm.embedding["C"] # Compute the smallest box containing all points of A upper = np.sum(np.abs(emb), axis=0) lower = -upper # Test the model u_plot = np.atleast_2d(np.arange(lower, upper, 0.01)).T x_plot = sm.get_x_from_u(u_plot) # Get corresponding points in Omega y_plot_true = fun(x_plot) y_plot_pred = sm.predict_values(u_plot) sigma_MGP, sigma_KRG = sm.predict_variances(u_plot, True) u_train = sm.get_u_from_x(xt) # Get corresponding points in A # Plots fig, ax = plt.subplots() ax.plot(u_plot, y_plot_pred, label="Predicted") ax.plot(u_plot, y_plot_true, "k--", label="True") ax.plot(u_train, yt, "k+", mew=3, ms=10, label="Train") ax.fill_between( u_plot[:, 0], y_plot_pred - 3 * sigma_MGP, y_plot_pred + 3 * sigma_MGP, color="r", alpha=0.5, label="Variance with hyperparameters uncertainty", ) ax.fill_between( u_plot[:, 0], y_plot_pred - 3 * sigma_KRG, y_plot_pred + 3 * sigma_KRG, color="b", alpha=0.5, label="Variance without hyperparameters uncertainty", ) ax.set(xlabel="x", ylabel="y", title="MGP") fig.legend(loc="upper center", ncol=2) fig.tight_layout() fig.subplots_adjust(top=0.74) plt.show() if __name__ == "__main__": unittest.main()
bsd-3-clause
hainm/scipy
scipy/stats/_discrete_distns.py
34
21220
# # Author: Travis Oliphant 2002-2011 with contributions from # SciPy Developers 2004-2011 # from __future__ import division, print_function, absolute_import from scipy import special from scipy.special import entr, gammaln as gamln from scipy.misc import logsumexp from numpy import floor, ceil, log, exp, sqrt, log1p, expm1, tanh, cosh, sinh import numpy as np from ._distn_infrastructure import ( rv_discrete, _lazywhere, _ncx2_pdf, _ncx2_cdf, get_distribution_names) class binom_gen(rv_discrete): """A binomial discrete random variable. %(before_notes)s Notes ----- The probability mass function for `binom` is:: binom.pmf(k) = choose(n, k) * p**k * (1-p)**(n-k) for ``k`` in ``{0, 1,..., n}``. `binom` takes ``n`` and ``p`` as shape parameters. %(after_notes)s %(example)s """ def _rvs(self, n, p): return self._random_state.binomial(n, p, self._size) def _argcheck(self, n, p): self.b = n return (n >= 0) & (p >= 0) & (p <= 1) def _logpmf(self, x, n, p): k = floor(x) combiln = (gamln(n+1) - (gamln(k+1) + gamln(n-k+1))) return combiln + special.xlogy(k, p) + special.xlog1py(n-k, -p) def _pmf(self, x, n, p): return exp(self._logpmf(x, n, p)) def _cdf(self, x, n, p): k = floor(x) vals = special.bdtr(k, n, p) return vals def _sf(self, x, n, p): k = floor(x) return special.bdtrc(k, n, p) def _ppf(self, q, n, p): vals = ceil(special.bdtrik(q, n, p)) vals1 = np.maximum(vals - 1, 0) temp = special.bdtr(vals1, n, p) return np.where(temp >= q, vals1, vals) def _stats(self, n, p, moments='mv'): q = 1.0 - p mu = n * p var = n * p * q g1, g2 = None, None if 's' in moments: g1 = (q - p) / sqrt(var) if 'k' in moments: g2 = (1.0 - 6*p*q) / var return mu, var, g1, g2 def _entropy(self, n, p): k = np.r_[0:n + 1] vals = self._pmf(k, n, p) return np.sum(entr(vals), axis=0) binom = binom_gen(name='binom') class bernoulli_gen(binom_gen): """A Bernoulli discrete random variable. %(before_notes)s Notes ----- The probability mass function for `bernoulli` is:: bernoulli.pmf(k) = 1-p if k = 0 = p if k = 1 for ``k`` in ``{0, 1}``. `bernoulli` takes ``p`` as shape parameter. %(after_notes)s %(example)s """ def _rvs(self, p): return binom_gen._rvs(self, 1, p) def _argcheck(self, p): return (p >= 0) & (p <= 1) def _logpmf(self, x, p): return binom._logpmf(x, 1, p) def _pmf(self, x, p): return binom._pmf(x, 1, p) def _cdf(self, x, p): return binom._cdf(x, 1, p) def _sf(self, x, p): return binom._sf(x, 1, p) def _ppf(self, q, p): return binom._ppf(q, 1, p) def _stats(self, p): return binom._stats(1, p) def _entropy(self, p): return entr(p) + entr(1-p) bernoulli = bernoulli_gen(b=1, name='bernoulli') class nbinom_gen(rv_discrete): """A negative binomial discrete random variable. %(before_notes)s Notes ----- The probability mass function for `nbinom` is:: nbinom.pmf(k) = choose(k+n-1, n-1) * p**n * (1-p)**k for ``k >= 0``. `nbinom` takes ``n`` and ``p`` as shape parameters. %(after_notes)s %(example)s """ def _rvs(self, n, p): return self._random_state.negative_binomial(n, p, self._size) def _argcheck(self, n, p): return (n > 0) & (p >= 0) & (p <= 1) def _pmf(self, x, n, p): return exp(self._logpmf(x, n, p)) def _logpmf(self, x, n, p): coeff = gamln(n+x) - gamln(x+1) - gamln(n) return coeff + n*log(p) + special.xlog1py(x, -p) def _cdf(self, x, n, p): k = floor(x) return special.betainc(n, k+1, p) def _sf_skip(self, x, n, p): # skip because special.nbdtrc doesn't work for 0<n<1 k = floor(x) return special.nbdtrc(k, n, p) def _ppf(self, q, n, p): vals = ceil(special.nbdtrik(q, n, p)) vals1 = (vals-1).clip(0.0, np.inf) temp = self._cdf(vals1, n, p) return np.where(temp >= q, vals1, vals) def _stats(self, n, p): Q = 1.0 / p P = Q - 1.0 mu = n*P var = n*P*Q g1 = (Q+P)/sqrt(n*P*Q) g2 = (1.0 + 6*P*Q) / (n*P*Q) return mu, var, g1, g2 nbinom = nbinom_gen(name='nbinom') class geom_gen(rv_discrete): """A geometric discrete random variable. %(before_notes)s Notes ----- The probability mass function for `geom` is:: geom.pmf(k) = (1-p)**(k-1)*p for ``k >= 1``. `geom` takes ``p`` as shape parameter. %(after_notes)s %(example)s """ def _rvs(self, p): return self._random_state.geometric(p, size=self._size) def _argcheck(self, p): return (p <= 1) & (p >= 0) def _pmf(self, k, p): return np.power(1-p, k-1) * p def _logpmf(self, k, p): return special.xlog1py(k - 1, -p) + log(p) def _cdf(self, x, p): k = floor(x) return -expm1(log1p(-p)*k) def _sf(self, x, p): return np.exp(self._logsf(x, p)) def _logsf(self, x, p): k = floor(x) return k*log1p(-p) def _ppf(self, q, p): vals = ceil(log(1.0-q)/log(1-p)) temp = self._cdf(vals-1, p) return np.where((temp >= q) & (vals > 0), vals-1, vals) def _stats(self, p): mu = 1.0/p qr = 1.0-p var = qr / p / p g1 = (2.0-p) / sqrt(qr) g2 = np.polyval([1, -6, 6], p)/(1.0-p) return mu, var, g1, g2 geom = geom_gen(a=1, name='geom', longname="A geometric") class hypergeom_gen(rv_discrete): """A hypergeometric discrete random variable. The hypergeometric distribution models drawing objects from a bin. M is the total number of objects, n is total number of Type I objects. The random variate represents the number of Type I objects in N drawn without replacement from the total population. %(before_notes)s Notes ----- The probability mass function is defined as:: pmf(k, M, n, N) = choose(n, k) * choose(M - n, N - k) / choose(M, N), for max(0, N - (M-n)) <= k <= min(n, N) %(after_notes)s Examples -------- >>> from scipy.stats import hypergeom >>> import matplotlib.pyplot as plt Suppose we have a collection of 20 animals, of which 7 are dogs. Then if we want to know the probability of finding a given number of dogs if we choose at random 12 of the 20 animals, we can initialize a frozen distribution and plot the probability mass function: >>> [M, n, N] = [20, 7, 12] >>> rv = hypergeom(M, n, N) >>> x = np.arange(0, n+1) >>> pmf_dogs = rv.pmf(x) >>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> ax.plot(x, pmf_dogs, 'bo') >>> ax.vlines(x, 0, pmf_dogs, lw=2) >>> ax.set_xlabel('# of dogs in our group of chosen animals') >>> ax.set_ylabel('hypergeom PMF') >>> plt.show() Instead of using a frozen distribution we can also use `hypergeom` methods directly. To for example obtain the cumulative distribution function, use: >>> prb = hypergeom.cdf(x, M, n, N) And to generate random numbers: >>> R = hypergeom.rvs(M, n, N, size=10) """ def _rvs(self, M, n, N): return self._random_state.hypergeometric(n, M-n, N, size=self._size) def _argcheck(self, M, n, N): cond = (M > 0) & (n >= 0) & (N >= 0) cond &= (n <= M) & (N <= M) self.a = max(N-(M-n), 0) self.b = min(n, N) return cond def _logpmf(self, k, M, n, N): tot, good = M, n bad = tot - good return gamln(good+1) - gamln(good-k+1) - gamln(k+1) + gamln(bad+1) \ - gamln(bad-N+k+1) - gamln(N-k+1) - gamln(tot+1) + gamln(tot-N+1) \ + gamln(N+1) def _pmf(self, k, M, n, N): # same as the following but numerically more precise # return comb(good, k) * comb(bad, N-k) / comb(tot, N) return exp(self._logpmf(k, M, n, N)) def _stats(self, M, n, N): # tot, good, sample_size = M, n, N # "wikipedia".replace('N', 'M').replace('n', 'N').replace('K', 'n') M, n, N = 1.*M, 1.*n, 1.*N m = M - n p = n/M mu = N*p var = m*n*N*(M - N)*1.0/(M*M*(M-1)) g1 = (m - n)*(M-2*N) / (M-2.0) * sqrt((M-1.0) / (m*n*N*(M-N))) g2 = M*(M+1) - 6.*N*(M-N) - 6.*n*m g2 *= (M-1)*M*M g2 += 6.*n*N*(M-N)*m*(5.*M-6) g2 /= n * N * (M-N) * m * (M-2.) * (M-3.) return mu, var, g1, g2 def _entropy(self, M, n, N): k = np.r_[N - (M - n):min(n, N) + 1] vals = self.pmf(k, M, n, N) return np.sum(entr(vals), axis=0) def _sf(self, k, M, n, N): """More precise calculation, 1 - cdf doesn't cut it.""" # This for loop is needed because `k` can be an array. If that's the # case, the sf() method makes M, n and N arrays of the same shape. We # therefore unpack all inputs args, so we can do the manual # integration. res = [] for quant, tot, good, draw in zip(k, M, n, N): # Manual integration over probability mass function. More accurate # than integrate.quad. k2 = np.arange(quant + 1, draw + 1) res.append(np.sum(self._pmf(k2, tot, good, draw))) return np.asarray(res) def _logsf(self, k, M, n, N): """ More precise calculation than log(sf) """ res = [] for quant, tot, good, draw in zip(k, M, n, N): # Integration over probability mass function using logsumexp k2 = np.arange(quant + 1, draw + 1) res.append(logsumexp(self._logpmf(k2, tot, good, draw))) return np.asarray(res) hypergeom = hypergeom_gen(name='hypergeom') # FIXME: Fails _cdfvec class logser_gen(rv_discrete): """A Logarithmic (Log-Series, Series) discrete random variable. %(before_notes)s Notes ----- The probability mass function for `logser` is:: logser.pmf(k) = - p**k / (k*log(1-p)) for ``k >= 1``. `logser` takes ``p`` as shape parameter. %(after_notes)s %(example)s """ def _rvs(self, p): # looks wrong for p>0.5, too few k=1 # trying to use generic is worse, no k=1 at all return self._random_state.logseries(p, size=self._size) def _argcheck(self, p): return (p > 0) & (p < 1) def _pmf(self, k, p): return -np.power(p, k) * 1.0 / k / log(1 - p) def _stats(self, p): r = log(1 - p) mu = p / (p - 1.0) / r mu2p = -p / r / (p - 1.0)**2 var = mu2p - mu*mu mu3p = -p / r * (1.0+p) / (1.0 - p)**3 mu3 = mu3p - 3*mu*mu2p + 2*mu**3 g1 = mu3 / np.power(var, 1.5) mu4p = -p / r * ( 1.0 / (p-1)**2 - 6*p / (p - 1)**3 + 6*p*p / (p-1)**4) mu4 = mu4p - 4*mu3p*mu + 6*mu2p*mu*mu - 3*mu**4 g2 = mu4 / var**2 - 3.0 return mu, var, g1, g2 logser = logser_gen(a=1, name='logser', longname='A logarithmic') class poisson_gen(rv_discrete): """A Poisson discrete random variable. %(before_notes)s Notes ----- The probability mass function for `poisson` is:: poisson.pmf(k) = exp(-mu) * mu**k / k! for ``k >= 0``. `poisson` takes ``mu`` as shape parameter. %(after_notes)s %(example)s """ def _rvs(self, mu): return self._random_state.poisson(mu, self._size) def _logpmf(self, k, mu): Pk = k*log(mu)-gamln(k+1) - mu return Pk def _pmf(self, k, mu): return exp(self._logpmf(k, mu)) def _cdf(self, x, mu): k = floor(x) return special.pdtr(k, mu) def _sf(self, x, mu): k = floor(x) return special.pdtrc(k, mu) def _ppf(self, q, mu): vals = ceil(special.pdtrik(q, mu)) vals1 = np.maximum(vals - 1, 0) temp = special.pdtr(vals1, mu) return np.where(temp >= q, vals1, vals) def _stats(self, mu): var = mu tmp = np.asarray(mu) g1 = sqrt(1.0 / tmp) g2 = 1.0 / tmp return mu, var, g1, g2 poisson = poisson_gen(name="poisson", longname='A Poisson') class planck_gen(rv_discrete): """A Planck discrete exponential random variable. %(before_notes)s Notes ----- The probability mass function for `planck` is:: planck.pmf(k) = (1-exp(-lambda_))*exp(-lambda_*k) for ``k*lambda_ >= 0``. `planck` takes ``lambda_`` as shape parameter. %(after_notes)s %(example)s """ def _argcheck(self, lambda_): if (lambda_ > 0): self.a = 0 self.b = np.inf return 1 elif (lambda_ < 0): self.a = -np.inf self.b = 0 return 1 else: return 0 def _pmf(self, k, lambda_): fact = (1-exp(-lambda_)) return fact*exp(-lambda_*k) def _cdf(self, x, lambda_): k = floor(x) return 1-exp(-lambda_*(k+1)) def _ppf(self, q, lambda_): vals = ceil(-1.0/lambda_ * log1p(-q)-1) vals1 = (vals-1).clip(self.a, np.inf) temp = self._cdf(vals1, lambda_) return np.where(temp >= q, vals1, vals) def _stats(self, lambda_): mu = 1/(exp(lambda_)-1) var = exp(-lambda_)/(expm1(-lambda_))**2 g1 = 2*cosh(lambda_/2.0) g2 = 4+2*cosh(lambda_) return mu, var, g1, g2 def _entropy(self, lambda_): l = lambda_ C = (1-exp(-l)) return l*exp(-l)/C - log(C) planck = planck_gen(name='planck', longname='A discrete exponential ') class boltzmann_gen(rv_discrete): """A Boltzmann (Truncated Discrete Exponential) random variable. %(before_notes)s Notes ----- The probability mass function for `boltzmann` is:: boltzmann.pmf(k) = (1-exp(-lambda_)*exp(-lambda_*k)/(1-exp(-lambda_*N)) for ``k = 0,..., N-1``. `boltzmann` takes ``lambda_`` and ``N`` as shape parameters. %(after_notes)s %(example)s """ def _pmf(self, k, lambda_, N): fact = (1-exp(-lambda_))/(1-exp(-lambda_*N)) return fact*exp(-lambda_*k) def _cdf(self, x, lambda_, N): k = floor(x) return (1-exp(-lambda_*(k+1)))/(1-exp(-lambda_*N)) def _ppf(self, q, lambda_, N): qnew = q*(1-exp(-lambda_*N)) vals = ceil(-1.0/lambda_ * log(1-qnew)-1) vals1 = (vals-1).clip(0.0, np.inf) temp = self._cdf(vals1, lambda_, N) return np.where(temp >= q, vals1, vals) def _stats(self, lambda_, N): z = exp(-lambda_) zN = exp(-lambda_*N) mu = z/(1.0-z)-N*zN/(1-zN) var = z/(1.0-z)**2 - N*N*zN/(1-zN)**2 trm = (1-zN)/(1-z) trm2 = (z*trm**2 - N*N*zN) g1 = z*(1+z)*trm**3 - N**3*zN*(1+zN) g1 = g1 / trm2**(1.5) g2 = z*(1+4*z+z*z)*trm**4 - N**4 * zN*(1+4*zN+zN*zN) g2 = g2 / trm2 / trm2 return mu, var, g1, g2 boltzmann = boltzmann_gen(name='boltzmann', longname='A truncated discrete exponential ') class randint_gen(rv_discrete): """A uniform discrete random variable. %(before_notes)s Notes ----- The probability mass function for `randint` is:: randint.pmf(k) = 1./(high - low) for ``k = low, ..., high - 1``. `randint` takes ``low`` and ``high`` as shape parameters. Note the difference to the numpy ``random_integers`` which returns integers on a *closed* interval ``[low, high]``. %(after_notes)s %(example)s """ def _argcheck(self, low, high): self.a = low self.b = high - 1 return (high > low) def _pmf(self, k, low, high): p = np.ones_like(k) / (high - low) return np.where((k >= low) & (k < high), p, 0.) def _cdf(self, x, low, high): k = floor(x) return (k - low + 1.) / (high - low) def _ppf(self, q, low, high): vals = ceil(q * (high - low) + low) - 1 vals1 = (vals - 1).clip(low, high) temp = self._cdf(vals1, low, high) return np.where(temp >= q, vals1, vals) def _stats(self, low, high): m2, m1 = np.asarray(high), np.asarray(low) mu = (m2 + m1 - 1.0) / 2 d = m2 - m1 var = (d*d - 1) / 12.0 g1 = 0.0 g2 = -6.0/5.0 * (d*d + 1.0) / (d*d - 1.0) return mu, var, g1, g2 def _rvs(self, low, high=None): """An array of *size* random integers >= ``low`` and < ``high``. If ``high`` is ``None``, then range is >=0 and < low """ return self._random_state.randint(low, high, self._size) def _entropy(self, low, high): return log(high - low) randint = randint_gen(name='randint', longname='A discrete uniform ' '(random integer)') # FIXME: problems sampling. class zipf_gen(rv_discrete): """A Zipf discrete random variable. %(before_notes)s Notes ----- The probability mass function for `zipf` is:: zipf.pmf(k, a) = 1/(zeta(a) * k**a) for ``k >= 1``. `zipf` takes ``a`` as shape parameter. %(after_notes)s %(example)s """ def _rvs(self, a): return self._random_state.zipf(a, size=self._size) def _argcheck(self, a): return a > 1 def _pmf(self, k, a): Pk = 1.0 / special.zeta(a, 1) / k**a return Pk def _munp(self, n, a): return _lazywhere( a > n + 1, (a, n), lambda a, n: special.zeta(a - n, 1) / special.zeta(a, 1), np.inf) zipf = zipf_gen(a=1, name='zipf', longname='A Zipf') class dlaplace_gen(rv_discrete): """A Laplacian discrete random variable. %(before_notes)s Notes ----- The probability mass function for `dlaplace` is:: dlaplace.pmf(k) = tanh(a/2) * exp(-a*abs(k)) for ``a > 0``. `dlaplace` takes ``a`` as shape parameter. %(after_notes)s %(example)s """ def _pmf(self, k, a): return tanh(a/2.0) * exp(-a * abs(k)) def _cdf(self, x, a): k = floor(x) f = lambda k, a: 1.0 - exp(-a * k) / (exp(a) + 1) f2 = lambda k, a: exp(a * (k+1)) / (exp(a) + 1) return _lazywhere(k >= 0, (k, a), f=f, f2=f2) def _ppf(self, q, a): const = 1 + exp(a) vals = ceil(np.where(q < 1.0 / (1 + exp(-a)), log(q*const) / a - 1, -log((1-q) * const) / a)) vals1 = vals - 1 return np.where(self._cdf(vals1, a) >= q, vals1, vals) def _stats(self, a): ea = exp(a) mu2 = 2.*ea/(ea-1.)**2 mu4 = 2.*ea*(ea**2+10.*ea+1.) / (ea-1.)**4 return 0., mu2, 0., mu4/mu2**2 - 3. def _entropy(self, a): return a / sinh(a) - log(tanh(a/2.0)) dlaplace = dlaplace_gen(a=-np.inf, name='dlaplace', longname='A discrete Laplacian') class skellam_gen(rv_discrete): """A Skellam discrete random variable. %(before_notes)s Notes ----- Probability distribution of the difference of two correlated or uncorrelated Poisson random variables. Let k1 and k2 be two Poisson-distributed r.v. with expected values lam1 and lam2. Then, ``k1 - k2`` follows a Skellam distribution with parameters ``mu1 = lam1 - rho*sqrt(lam1*lam2)`` and ``mu2 = lam2 - rho*sqrt(lam1*lam2)``, where rho is the correlation coefficient between k1 and k2. If the two Poisson-distributed r.v. are independent then ``rho = 0``. Parameters mu1 and mu2 must be strictly positive. For details see: http://en.wikipedia.org/wiki/Skellam_distribution `skellam` takes ``mu1`` and ``mu2`` as shape parameters. %(after_notes)s %(example)s """ def _rvs(self, mu1, mu2): n = self._size return (self._random_state.poisson(mu1, n) - self._random_state.poisson(mu2, n)) def _pmf(self, x, mu1, mu2): px = np.where(x < 0, _ncx2_pdf(2*mu2, 2*(1-x), 2*mu1)*2, _ncx2_pdf(2*mu1, 2*(1+x), 2*mu2)*2) # ncx2.pdf() returns nan's for extremely low probabilities return px def _cdf(self, x, mu1, mu2): x = floor(x) px = np.where(x < 0, _ncx2_cdf(2*mu2, -2*x, 2*mu1), 1-_ncx2_cdf(2*mu1, 2*(x+1), 2*mu2)) return px def _stats(self, mu1, mu2): mean = mu1 - mu2 var = mu1 + mu2 g1 = mean / sqrt((var)**3) g2 = 1 / var return mean, var, g1, g2 skellam = skellam_gen(a=-np.inf, name="skellam", longname='A Skellam') # Collect names of classes and objects in this module. pairs = list(globals().items()) _distn_names, _distn_gen_names = get_distribution_names(pairs, rv_discrete) __all__ = _distn_names + _distn_gen_names
bsd-3-clause
chinageology/GeoPython
Experimental/PreTreat.py
2
6940
# coding:utf-8 import math import sys import os import csv import random from bs4 import BeautifulSoup import pandas as pd import numpy as np from mpl_toolkits.mplot3d import Axes3D from sklearn import datasets from sklearn.decomposition import PCA from sklearn.neighbors import NearestNeighbors import matplotlib import scipy.stats as st matplotlib.use('Qt5Agg') import matplotlib.pyplot as plt import matplotlib.font_manager as fm from matplotlib import ft2font from matplotlib.font_manager import ttfFontProperty import matplotlib.font_manager as font_manager import matplotlib.image as mpimg from pandas.plotting import radviz from sklearn.neighbors import KNeighborsRegressor def Del(prepath='/Volumes/Virtual/FastTmp/', path='raw', name='test.txt', head=3, end=-2): lines = open(path + '/' + name, 'r', encoding='windows-1252').readlines() open(prepath + 'new' + path + '/' + 'new' + name, 'w', encoding='utf-8').writelines(lines[head:end]) def OldJoin(prepath='/Volumes/Virtual/FastTmp/', path='Raw'): SourceList = os.listdir(path) for i in SourceList: if 'csv' in i and 'new' not in i and i[0] != '.': Del(prepath=prepath, path=path, name=i, head=3, end=-2) TargetList = [] for i in SourceList: if 'csv' in i: df = pd.read_csv(prepath + 'new' + path + '/' + 'new' + i) TargetList.append(df) result = pd.concat(TargetList) result.reindex() result.to_csv(prepath + 'result.csv', sep=',', encoding='utf-8') return (result) def Join(prepath='/Volumes/Virtual/FastTmp/', path='Excel', name='result'): SourceList = os.listdir(prepath + path) TargetList = [] for i in SourceList: if 'csv' in i and '~' not in i and i[0] != '.': print(prepath + path + '/' + i) try: df = pd.read_csv(prepath + path + '/' + i) except(): pass elif 'xls' in i and '~' not in i and i[0] != '.': try: df = pd.read_excel(prepath + path + '/' + i) except(): pass TargetList.append(df) result = pd.concat(TargetList) result.reindex() result.to_excel(prepath + name + '.xlsx', encoding='utf-8') return (result) def CsvToExcel(name='result'): if 'csv' in name and '~' not in name and name[0] != '.': df = pd.read_csv(name) df.to_excel('new' + name[0:-4] + '.xlsx', encoding='utf-8') pass def ExcelToCsv(name='result'): if 'xls' in name and '~' not in name and name[0] != '.': df = pd.read_excel(name) df.to_csv('new' + name[0:-5] + '.csv', sep=',', encoding='utf-8') pass prepath = '/Volumes/Virtual/FastTmp/' path = 'Target' df = pd.read_excel(prepath + 'XiaYing-SiO2-FeMg.xlsx') # m = ['Width', 'Style', 'Alpha', 'Size', 'Color', 'Marker', 'Author'] # for i in m: # df = df.drop(i, 1) df.set_index('Label', inplace=True) newdf = pd.concat([df.SiO2, df.Ratio], axis=1) numpyMatrix = df.as_matrix() # X = numpyMatrix[:, :3] # we only take the first two features. # y = df.index X = numpyMatrix y = df.index color = [] for i in range(len(y)): if i == 0: color.append(1) else: if y[i] == y[0]: color.append(1) else: color.append(2) # x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 # y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 X_reduced = PCA(n_components=5).fit_transform(numpyMatrix) df = pd.read_excel(prepath + 'XiaYing-SiO2-FeMg.xlsx') df.set_index('Label', inplace=True) x = df.SiO2 y = df.Ratio xtouse = x.values ytouse = y.values XtoFit=[] YtoFit=[] for i in range(len(x.values)): if x.values[i] < 60: XtoFit.append(x.values[i]) YtoFit.append(y.values[i]) z = np.polyfit(YtoFit, XtoFit, 3) Yline = np.linspace(min(YtoFit), max(YtoFit), 30) p = np.poly1d(z) Xline = p(Yline) newXline = [] ##################################### xmin,xmax = min(x),max(x) ymin,ymax = min(y),max(y) # Peform the kernel density estimate xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j] positions = np.vstack([xx.ravel(), yy.ravel()]) values = np.vstack([x, y]) kernel = st.gaussian_kde(values) f = np.reshape(kernel(positions).T, xx.shape) fig = plt.figure() ax = fig.gca() # Contourf plot cfset = ax.contourf(xx, yy, f, cmap='Blues',alpha=0.3) ## Or kernel density estimate plot instead of the contourf plot #ax.imshow(np.rot90(f), cmap='Blues', extent=[xmin, xmax, ymin, ymax]) # Contour plot cset = ax.contour(xx, yy, f, colors='k',alpha=0.3) # Label plot ax.clabel(cset, inline=1, fontsize=10) ##################################### plt.plot(Xline, Yline, 'b-') alphatouse = [] leveldistance=[] for i in range(len(xtouse)): tmp = abs(p(ytouse[i]) - xtouse[i]) leveldistance.append(tmp) alphatouse.append(tmp) a = [] group= 100 step= abs(min(alphatouse)-max(alphatouse))/group for i in alphatouse: if min(alphatouse)<=i<min(alphatouse)+step: a.append(0.8) elif min(alphatouse)+step<=i<min(alphatouse)+2*step: a.append(0.6) elif min(alphatouse)+2*step<=i<min(alphatouse)+3*step: a.append(0.4) else: a.append(0.2) #plt.scatter(x, y, label='', s=3, color='red', alpha=a) for i in range(len(xtouse)): plt.scatter(xtouse[i], ytouse[i], label='', s=3, color='red', alpha=a[i]) pass #fig = plt.figure(1, figsize=(8, 6)) # ax = Axes3D(fig, elev=-150, azim=110) # plt.scatter(X_reduced[:, 1], X_reduced[:, 2], c=color, cmap=plt.cm.Set1, edgecolor='k', s=40) # plt.scatter(x, y, label='', s=3, color='blue', alpha=0.3) # z= np.polyfit(x, y, 2) # ax.set_title("First three PCA directions") # ax.set_xlabel("SiO2") # ax.w_xaxis.set_ticklabels([]) # ax.set_ylabel("TFeO") # ax.w_yaxis.set_ticklabels([]) # ax.set_zlabel("MgO") # ax.w_zaxis.set_ticklabels([]) plt.show() tm=Join(path='塔木兰沟组',name='新塔木兰沟') ''' SourceList = os.listdir(prepath + path) TargetList = [] for i in SourceList: ExcelToCsv(path='塔木兰沟组',name=i) #df = pd.read_excel(prepath+"塔木兰沟组数据交集.xlsx",keep_default_na=False, na_values=[""]) #tm=Join(path='Target',name='满克头鄂博组数据交集') #DataToPlot = pd.read_excel(prepath+'result.xlsx') #DataToPlot.plot() #DataToPlot.plot.area() #plt.figure() #radviz(DataToPlot , 'Ag109') #plt.show() # created by Huang Lu # 27/08/2016 17:05:45 # Department of EE, Tsinghua Univ. import cv2 import numpy as np cap = cv2.VideoCapture(1) while(1): # get a frame ret, frame = cap.read() # show a frame cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() for i in alphatouse: #tmp = - np.power(np.e,i / max(alphatouse)) #tmp = 1- np.power(i / max(alphatouse),2) tmp = np.power(np.e,i) a.append(tmp) '''
gpl-3.0
tmhm/scikit-learn
examples/model_selection/plot_roc_crossval.py
247
3253
""" ============================================================= Receiver Operating Characteristic (ROC) with cross validation ============================================================= Example of Receiver Operating Characteristic (ROC) metric to evaluate classifier output quality using cross-validation. ROC curves typically feature true positive rate on the Y axis, and false positive rate on the X axis. This means that the top left corner of the plot is the "ideal" point - a false positive rate of zero, and a true positive rate of one. This is not very realistic, but it does mean that a larger area under the curve (AUC) is usually better. The "steepness" of ROC curves is also important, since it is ideal to maximize the true positive rate while minimizing the false positive rate. This example shows the ROC response of different datasets, created from K-fold cross-validation. Taking all of these curves, it is possible to calculate the mean area under curve, and see the variance of the curve when the training set is split into different subsets. This roughly shows how the classifier output is affected by changes in the training data, and how different the splits generated by K-fold cross-validation are from one another. .. note:: See also :func:`sklearn.metrics.auc_score`, :func:`sklearn.cross_validation.cross_val_score`, :ref:`example_model_selection_plot_roc.py`, """ print(__doc__) import numpy as np from scipy import interp import matplotlib.pyplot as plt from sklearn import svm, datasets from sklearn.metrics import roc_curve, auc from sklearn.cross_validation import StratifiedKFold ############################################################################### # Data IO and generation # import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target X, y = X[y != 2], y[y != 2] n_samples, n_features = X.shape # Add noisy features random_state = np.random.RandomState(0) X = np.c_[X, random_state.randn(n_samples, 200 * n_features)] ############################################################################### # Classification and ROC analysis # Run classifier with cross-validation and plot ROC curves cv = StratifiedKFold(y, n_folds=6) classifier = svm.SVC(kernel='linear', probability=True, random_state=random_state) mean_tpr = 0.0 mean_fpr = np.linspace(0, 1, 100) all_tpr = [] for i, (train, test) in enumerate(cv): probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test]) # Compute ROC curve and area the curve fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1]) mean_tpr += interp(mean_fpr, fpr, tpr) mean_tpr[0] = 0.0 roc_auc = auc(fpr, tpr) plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.2f)' % (i, roc_auc)) plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck') mean_tpr /= len(cv) mean_tpr[-1] = 1.0 mean_auc = auc(mean_fpr, mean_tpr) plt.plot(mean_fpr, mean_tpr, 'k--', label='Mean ROC (area = %0.2f)' % mean_auc, lw=2) plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic example') plt.legend(loc="lower right") plt.show()
bsd-3-clause
rlpy/rlpy
rlpy/Representations/LocalBases.py
1
7491
""" Representations which use local bases function (e.g. kernels) distributed in the statespace according to some scheme (e.g. grid, random, on previous samples) """ from __future__ import division from __future__ import print_function from __future__ import absolute_import from __future__ import unicode_literals from builtins import super from future import standard_library standard_library.install_aliases() from builtins import range from past.utils import old_div from .Representation import Representation import numpy as np from rlpy.Tools.GeneralTools import addNewElementForAllActions import matplotlib.pyplot as plt try: from .kernels import batch except ImportError: from .slow_kernels import batch print("C-Extensions for kernels not available, expect slow runtime") __copyright__ = "Copyright 2013, RLPy http://acl.mit.edu/RLPy" __credits__ = ["Alborz Geramifard", "Robert H. Klein", "Christoph Dann", "William Dabney", "Jonathan P. How"] __license__ = "BSD 3-Clause" class LocalBases(Representation): """ abstract base class for representations that use local basis functions """ #: centers of bases centers = None #: widths of bases widths = None def __init__(self, domain, kernel, normalization=False, seed=1, **kwargs): """ :param domain: domain to learn on. :param kernel: function handle to use for kernel function evaluations. :param normalization: (Boolean) If true, normalize feature vector so that sum( phi(s) ) = 1. Associates a kernel function with each """ self.kernel = batch[kernel.__name__] self.normalization = normalization self.centers = np.zeros((0, domain.statespace_limits.shape[0])) self.widths = np.zeros((0, domain.statespace_limits.shape[0])) super(LocalBases, self).__init__(domain, seed=seed) def phi_nonTerminal(self, s): v = self.kernel(s, self.centers, self.widths) if self.normalization and not v.sum() == 0.: # normalize such that each vector has a l1 norm of 1 v /= v.sum() return v def plot_2d_feature_centers(self, d1=None, d2=None): """ :param d1: 1 (of 2 possible) indices of dimensions to plot; ignore all others, purely visual. :param d2: 1 (of 2 possible) indices of dimensions to plot; ignore all others, purely visual. Phe centers of all features in dimension d1 and d2. If no dimensions are specified, the first two continuous dimensions are shown. """ if d1 is None and d2 is None: # just take the first two dimensions d1, d2 = self.domain.continuous_dims[:2] plt.figure("Feature Dimensions {} and {}".format(d1, d2)) for i in range(self.centers.shape[0]): plt.plot([self.centers[i, d1]], [self.centers[i, d2]], "r", marker="x") plt.draw() class NonparametricLocalBases(LocalBases): def __init__(self, domain, kernel, max_similarity=0.9, resolution=5, **kwargs): """ :param domain: domain to learn on. :param kernel: function handle to use for kernel function evaluations. :param max_similarity: threshold to allow feature to be added to representation. Larger max_similarity makes it \"easier\" to add more features by permitting larger values of phi(s) before discarding. (An existing feature function in phi() with large value at phi(s) implies that it is very representative of the true function at *s*. i.e., the value of a feature in phi(s) is inversely related to the \"similarity\" of a potential new feature. :param resolution: to be used by the ``kernel()`` function, see parent. Determines *width* of basis functions, eg sigma in Gaussian basis. """ self.max_similarity = max_similarity self.common_width = old_div((domain.statespace_limits[:, 1] - domain.statespace_limits[:, 0]), resolution) self.features_num = 0 super( NonparametricLocalBases, self).__init__( domain, kernel, **kwargs) def pre_discover(self, s, terminal, a, sn, terminaln): norm = self.normalization expanded = 0 self.normalization = False if not terminal: phi_s = self.phi_nonTerminal(s) if np.all(phi_s < self.max_similarity): self._add_feature(s) expanded += 1 if not terminaln: phi_s = self.phi_nonTerminal(sn) if np.all(phi_s < self.max_similarity): self._add_feature(sn) expanded += 1 self.normalization = norm return expanded def _add_feature(self, center): self.features_num += 1 self.centers = np.vstack((self.centers, center)) self.widths = np.vstack((self.widths, self.common_width)) # TODO if normalized, use Q estimate for center to fill weight_vec new = np.zeros((self.domain.actions_num, 1)) self.weight_vec = addNewElementForAllActions( self.weight_vec, self.domain.actions_num, new) class RandomLocalBases(LocalBases): def __init__(self, domain, kernel, num=100, resolution_min=5, resolution_max=None, seed=1, **kwargs): """ :param domain: domain to learn on. :param kernel: function handle to use for kernel function evaluations. :param num: Fixed number of feature (kernel) functions to use in EACH dimension. (for a total of features_num=numDims * num) :param resolution_min: resolution selected uniform random, lower bound. :param resolution_max: resolution selected uniform random, upper bound. :param seed: the random seed to use when scattering basis functions. Randomly scatter ``num`` feature functions throughout the domain, with sigma / noise parameter selected uniform random between ``resolution_min`` and ``resolution_max``. NOTE these are sensitive to the choice of coordinate (scale with coordinate units). """ self.features_num = num self.dim_widths = (domain.statespace_limits[:, 1] - domain.statespace_limits[:, 0]) self.resolution_max = resolution_max self.resolution_min = resolution_min super( RandomLocalBases, self).__init__( domain, kernel, seed=seed, **kwargs) self.centers = np.zeros((num, len(self.dim_widths))) self.widths = np.zeros((num, len(self.dim_widths))) self.init_randomization() def init_randomization(self): for i in range(self.features_num): for d in range(len(self.dim_widths)): self.centers[i, d] = self.random_state.uniform( self.domain.statespace_limits[d, 0], self.domain.statespace_limits[d, 1]) self.widths[i, d] = self.random_state.uniform( old_div(self.dim_widths[d], self.resolution_max), old_div(self.dim_widths[d], self.resolution_min))
bsd-3-clause
valexandersaulys/airbnb_kaggle_contest
venv/lib/python3.4/site-packages/numpy/lib/function_base.py
7
134697
from __future__ import division, absolute_import, print_function import warnings import sys import collections import operator import numpy as np import numpy.core.numeric as _nx from numpy.core import linspace, atleast_1d, atleast_2d from numpy.core.numeric import ( ones, zeros, arange, concatenate, array, asarray, asanyarray, empty, empty_like, ndarray, around, floor, ceil, take, dot, where, intp, integer, isscalar ) from numpy.core.umath import ( pi, multiply, add, arctan2, frompyfunc, cos, less_equal, sqrt, sin, mod, exp, log10 ) from numpy.core.fromnumeric import ( ravel, nonzero, sort, partition, mean, any, sum ) from numpy.core.numerictypes import typecodes, number from numpy.lib.twodim_base import diag from .utils import deprecate from numpy.core.multiarray import _insert, add_docstring from numpy.core.multiarray import digitize, bincount, interp as compiled_interp from numpy.core.umath import _add_newdoc_ufunc as add_newdoc_ufunc from numpy.compat import long # Force range to be a generator, for np.delete's usage. if sys.version_info[0] < 3: range = xrange __all__ = [ 'select', 'piecewise', 'trim_zeros', 'copy', 'iterable', 'percentile', 'diff', 'gradient', 'angle', 'unwrap', 'sort_complex', 'disp', 'extract', 'place', 'vectorize', 'asarray_chkfinite', 'average', 'histogram', 'histogramdd', 'bincount', 'digitize', 'cov', 'corrcoef', 'msort', 'median', 'sinc', 'hamming', 'hanning', 'bartlett', 'blackman', 'kaiser', 'trapz', 'i0', 'add_newdoc', 'add_docstring', 'meshgrid', 'delete', 'insert', 'append', 'interp', 'add_newdoc_ufunc' ] def iterable(y): """ Check whether or not an object can be iterated over. Parameters ---------- y : object Input object. Returns ------- b : {0, 1} Return 1 if the object has an iterator method or is a sequence, and 0 otherwise. Examples -------- >>> np.iterable([1, 2, 3]) 1 >>> np.iterable(2) 0 """ try: iter(y) except: return 0 return 1 def histogram(a, bins=10, range=None, normed=False, weights=None, density=None): """ Compute the histogram of a set of data. Parameters ---------- a : array_like Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars, optional If `bins` is an int, it defines the number of equal-width bins in the given range (10, by default). If `bins` is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), optional The lower and upper range of the bins. If not provided, range is simply ``(a.min(), a.max())``. Values outside the range are ignored. normed : bool, optional This keyword is deprecated in Numpy 1.6 due to confusing/buggy behavior. It will be removed in Numpy 2.0. Use the density keyword instead. If False, the result will contain the number of samples in each bin. If True, the result is the value of the probability *density* function at the bin, normalized such that the *integral* over the range is 1. Note that this latter behavior is known to be buggy with unequal bin widths; use `density` instead. weights : array_like, optional An array of weights, of the same shape as `a`. Each value in `a` only contributes its associated weight towards the bin count (instead of 1). If `normed` is True, the weights are normalized, so that the integral of the density over the range remains 1 density : bool, optional If False, the result will contain the number of samples in each bin. If True, the result is the value of the probability *density* function at the bin, normalized such that the *integral* over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability *mass* function. Overrides the `normed` keyword if given. Returns ------- hist : array The values of the histogram. See `normed` and `weights` for a description of the possible semantics. bin_edges : array of dtype float Return the bin edges ``(length(hist)+1)``. See Also -------- histogramdd, bincount, searchsorted, digitize Notes ----- All but the last (righthand-most) bin is half-open. In other words, if `bins` is:: [1, 2, 3, 4] then the first bin is ``[1, 2)`` (including 1, but excluding 2) and the second ``[2, 3)``. The last bin, however, is ``[3, 4]``, which *includes* 4. Examples -------- >>> np.histogram([1, 2, 1], bins=[0, 1, 2, 3]) (array([0, 2, 1]), array([0, 1, 2, 3])) >>> np.histogram(np.arange(4), bins=np.arange(5), density=True) (array([ 0.25, 0.25, 0.25, 0.25]), array([0, 1, 2, 3, 4])) >>> np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3]) (array([1, 4, 1]), array([0, 1, 2, 3])) >>> a = np.arange(5) >>> hist, bin_edges = np.histogram(a, density=True) >>> hist array([ 0.5, 0. , 0.5, 0. , 0. , 0.5, 0. , 0.5, 0. , 0.5]) >>> hist.sum() 2.4999999999999996 >>> np.sum(hist*np.diff(bin_edges)) 1.0 """ a = asarray(a) if weights is not None: weights = asarray(weights) if np.any(weights.shape != a.shape): raise ValueError( 'weights should have the same shape as a.') weights = weights.ravel() a = a.ravel() if (range is not None): mn, mx = range if (mn > mx): raise AttributeError( 'max must be larger than min in range parameter.') # Histogram is an integer or a float array depending on the weights. if weights is None: ntype = np.dtype(np.intp) else: ntype = weights.dtype # We set a block size, as this allows us to iterate over chunks when # computing histograms, to minimize memory usage. BLOCK = 65536 if not iterable(bins): if np.isscalar(bins) and bins < 1: raise ValueError( '`bins` should be a positive integer.') if range is None: if a.size == 0: # handle empty arrays. Can't determine range, so use 0-1. range = (0, 1) else: range = (a.min(), a.max()) mn, mx = [mi + 0.0 for mi in range] if mn == mx: mn -= 0.5 mx += 0.5 # At this point, if the weights are not integer, floating point, or # complex, we have to use the slow algorithm. if weights is not None and not (np.can_cast(weights.dtype, np.double) or np.can_cast(weights.dtype, np.complex)): bins = linspace(mn, mx, bins + 1, endpoint=True) if not iterable(bins): # We now convert values of a to bin indices, under the assumption of # equal bin widths (which is valid here). # Initialize empty histogram n = np.zeros(bins, ntype) # Pre-compute histogram scaling factor norm = bins / (mx - mn) # We iterate over blocks here for two reasons: the first is that for # large arrays, it is actually faster (for example for a 10^8 array it # is 2x as fast) and it results in a memory footprint 3x lower in the # limit of large arrays. for i in arange(0, len(a), BLOCK): tmp_a = a[i:i+BLOCK] if weights is None: tmp_w = None else: tmp_w = weights[i:i + BLOCK] # Only include values in the right range keep = (tmp_a >= mn) keep &= (tmp_a <= mx) if not np.logical_and.reduce(keep): tmp_a = tmp_a[keep] if tmp_w is not None: tmp_w = tmp_w[keep] tmp_a = tmp_a.astype(float) tmp_a -= mn tmp_a *= norm # Compute the bin indices, and for values that lie exactly on mx we # need to subtract one indices = tmp_a.astype(np.intp) indices[indices == bins] -= 1 # We now compute the histogram using bincount if ntype.kind == 'c': n.real += np.bincount(indices, weights=tmp_w.real, minlength=bins) n.imag += np.bincount(indices, weights=tmp_w.imag, minlength=bins) else: n += np.bincount(indices, weights=tmp_w, minlength=bins).astype(ntype) # We now compute the bin edges since these are returned bins = linspace(mn, mx, bins + 1, endpoint=True) else: bins = asarray(bins) if (np.diff(bins) < 0).any(): raise AttributeError( 'bins must increase monotonically.') # Initialize empty histogram n = np.zeros(bins.shape, ntype) if weights is None: for i in arange(0, len(a), BLOCK): sa = sort(a[i:i+BLOCK]) n += np.r_[sa.searchsorted(bins[:-1], 'left'), sa.searchsorted(bins[-1], 'right')] else: zero = array(0, dtype=ntype) for i in arange(0, len(a), BLOCK): tmp_a = a[i:i+BLOCK] tmp_w = weights[i:i+BLOCK] sorting_index = np.argsort(tmp_a) sa = tmp_a[sorting_index] sw = tmp_w[sorting_index] cw = np.concatenate(([zero, ], sw.cumsum())) bin_index = np.r_[sa.searchsorted(bins[:-1], 'left'), sa.searchsorted(bins[-1], 'right')] n += cw[bin_index] n = np.diff(n) if density is not None: if density: db = array(np.diff(bins), float) return n/db/n.sum(), bins else: return n, bins else: # deprecated, buggy behavior. Remove for Numpy 2.0 if normed: db = array(np.diff(bins), float) return n/(n*db).sum(), bins else: return n, bins def histogramdd(sample, bins=10, range=None, normed=False, weights=None): """ Compute the multidimensional histogram of some data. Parameters ---------- sample : array_like The data to be histogrammed. It must be an (N,D) array or data that can be converted to such. The rows of the resulting array are the coordinates of points in a D dimensional polytope. bins : sequence or int, optional The bin specification: * A sequence of arrays describing the bin edges along each dimension. * The number of bins for each dimension (nx, ny, ... =bins) * The number of bins for all dimensions (nx=ny=...=bins). range : sequence, optional A sequence of lower and upper bin edges to be used if the edges are not given explicitly in `bins`. Defaults to the minimum and maximum values along each dimension. normed : bool, optional If False, returns the number of samples in each bin. If True, returns the bin density ``bin_count / sample_count / bin_volume``. weights : (N,) array_like, optional An array of values `w_i` weighing each sample `(x_i, y_i, z_i, ...)`. Weights are normalized to 1 if normed is True. If normed is False, the values of the returned histogram are equal to the sum of the weights belonging to the samples falling into each bin. Returns ------- H : ndarray The multidimensional histogram of sample x. See normed and weights for the different possible semantics. edges : list A list of D arrays describing the bin edges for each dimension. See Also -------- histogram: 1-D histogram histogram2d: 2-D histogram Examples -------- >>> r = np.random.randn(100,3) >>> H, edges = np.histogramdd(r, bins = (5, 8, 4)) >>> H.shape, edges[0].size, edges[1].size, edges[2].size ((5, 8, 4), 6, 9, 5) """ try: # Sample is an ND-array. N, D = sample.shape except (AttributeError, ValueError): # Sample is a sequence of 1D arrays. sample = atleast_2d(sample).T N, D = sample.shape nbin = empty(D, int) edges = D*[None] dedges = D*[None] if weights is not None: weights = asarray(weights) try: M = len(bins) if M != D: raise AttributeError( 'The dimension of bins must be equal to the dimension of the ' ' sample x.') except TypeError: # bins is an integer bins = D*[bins] # Select range for each dimension # Used only if number of bins is given. if range is None: # Handle empty input. Range can't be determined in that case, use 0-1. if N == 0: smin = zeros(D) smax = ones(D) else: smin = atleast_1d(array(sample.min(0), float)) smax = atleast_1d(array(sample.max(0), float)) else: smin = zeros(D) smax = zeros(D) for i in arange(D): smin[i], smax[i] = range[i] # Make sure the bins have a finite width. for i in arange(len(smin)): if smin[i] == smax[i]: smin[i] = smin[i] - .5 smax[i] = smax[i] + .5 # avoid rounding issues for comparisons when dealing with inexact types if np.issubdtype(sample.dtype, np.inexact): edge_dt = sample.dtype else: edge_dt = float # Create edge arrays for i in arange(D): if isscalar(bins[i]): if bins[i] < 1: raise ValueError( "Element at index %s in `bins` should be a positive " "integer." % i) nbin[i] = bins[i] + 2 # +2 for outlier bins edges[i] = linspace(smin[i], smax[i], nbin[i]-1, dtype=edge_dt) else: edges[i] = asarray(bins[i], edge_dt) nbin[i] = len(edges[i]) + 1 # +1 for outlier bins dedges[i] = diff(edges[i]) if np.any(np.asarray(dedges[i]) <= 0): raise ValueError( "Found bin edge of size <= 0. Did you specify `bins` with" "non-monotonic sequence?") nbin = asarray(nbin) # Handle empty input. if N == 0: return np.zeros(nbin-2), edges # Compute the bin number each sample falls into. Ncount = {} for i in arange(D): Ncount[i] = digitize(sample[:, i], edges[i]) # Using digitize, values that fall on an edge are put in the right bin. # For the rightmost bin, we want values equal to the right edge to be # counted in the last bin, and not as an outlier. for i in arange(D): # Rounding precision mindiff = dedges[i].min() if not np.isinf(mindiff): decimal = int(-log10(mindiff)) + 6 # Find which points are on the rightmost edge. not_smaller_than_edge = (sample[:, i] >= edges[i][-1]) on_edge = (around(sample[:, i], decimal) == around(edges[i][-1], decimal)) # Shift these points one bin to the left. Ncount[i][where(on_edge & not_smaller_than_edge)[0]] -= 1 # Flattened histogram matrix (1D) # Reshape is used so that overlarge arrays # will raise an error. hist = zeros(nbin, float).reshape(-1) # Compute the sample indices in the flattened histogram matrix. ni = nbin.argsort() xy = zeros(N, int) for i in arange(0, D-1): xy += Ncount[ni[i]] * nbin[ni[i+1:]].prod() xy += Ncount[ni[-1]] # Compute the number of repetitions in xy and assign it to the # flattened histmat. if len(xy) == 0: return zeros(nbin-2, int), edges flatcount = bincount(xy, weights) a = arange(len(flatcount)) hist[a] = flatcount # Shape into a proper matrix hist = hist.reshape(sort(nbin)) for i in arange(nbin.size): j = ni.argsort()[i] hist = hist.swapaxes(i, j) ni[i], ni[j] = ni[j], ni[i] # Remove outliers (indices 0 and -1 for each dimension). core = D*[slice(1, -1)] hist = hist[core] # Normalize if normed is True if normed: s = hist.sum() for i in arange(D): shape = ones(D, int) shape[i] = nbin[i] - 2 hist = hist / dedges[i].reshape(shape) hist /= s if (hist.shape != nbin - 2).any(): raise RuntimeError( "Internal Shape Error") return hist, edges def average(a, axis=None, weights=None, returned=False): """ Compute the weighted average along the specified axis. Parameters ---------- a : array_like Array containing data to be averaged. If `a` is not an array, a conversion is attempted. axis : int, optional Axis along which to average `a`. If `None`, averaging is done over the flattened array. weights : array_like, optional An array of weights associated with the values in `a`. Each value in `a` contributes to the average according to its associated weight. The weights array can either be 1-D (in which case its length must be the size of `a` along the given axis) or of the same shape as `a`. If `weights=None`, then all data in `a` are assumed to have a weight equal to one. returned : bool, optional Default is `False`. If `True`, the tuple (`average`, `sum_of_weights`) is returned, otherwise only the average is returned. If `weights=None`, `sum_of_weights` is equivalent to the number of elements over which the average is taken. Returns ------- average, [sum_of_weights] : array_type or double Return the average along the specified axis. When returned is `True`, return a tuple with the average as the first element and the sum of the weights as the second element. The return type is `Float` if `a` is of integer type, otherwise it is of the same type as `a`. `sum_of_weights` is of the same type as `average`. Raises ------ ZeroDivisionError When all weights along axis are zero. See `numpy.ma.average` for a version robust to this type of error. TypeError When the length of 1D `weights` is not the same as the shape of `a` along axis. See Also -------- mean ma.average : average for masked arrays -- useful if your data contains "missing" values Examples -------- >>> data = range(1,5) >>> data [1, 2, 3, 4] >>> np.average(data) 2.5 >>> np.average(range(1,11), weights=range(10,0,-1)) 4.0 >>> data = np.arange(6).reshape((3,2)) >>> data array([[0, 1], [2, 3], [4, 5]]) >>> np.average(data, axis=1, weights=[1./4, 3./4]) array([ 0.75, 2.75, 4.75]) >>> np.average(data, weights=[1./4, 3./4]) Traceback (most recent call last): ... TypeError: Axis must be specified when shapes of a and weights differ. """ if not isinstance(a, np.matrix): a = np.asarray(a) if weights is None: avg = a.mean(axis) scl = avg.dtype.type(a.size/avg.size) else: a = a + 0.0 wgt = np.asarray(weights) # Sanity checks if a.shape != wgt.shape: if axis is None: raise TypeError( "Axis must be specified when shapes of a and weights " "differ.") if wgt.ndim != 1: raise TypeError( "1D weights expected when shapes of a and weights differ.") if wgt.shape[0] != a.shape[axis]: raise ValueError( "Length of weights not compatible with specified axis.") # setup wgt to broadcast along axis wgt = np.array(wgt, copy=0, ndmin=a.ndim).swapaxes(-1, axis) scl = wgt.sum(axis=axis, dtype=np.result_type(a.dtype, wgt.dtype)) if (scl == 0.0).any(): raise ZeroDivisionError( "Weights sum to zero, can't be normalized") avg = np.multiply(a, wgt).sum(axis)/scl if returned: scl = np.multiply(avg, 0) + scl return avg, scl else: return avg def asarray_chkfinite(a, dtype=None, order=None): """Convert the input to an array, checking for NaNs or Infs. Parameters ---------- a : array_like Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Success requires no NaNs or Infs. dtype : data-type, optional By default, the data-type is inferred from the input data. order : {'C', 'F'}, optional Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to 'C'. Returns ------- out : ndarray Array interpretation of `a`. No copy is performed if the input is already an ndarray. If `a` is a subclass of ndarray, a base class ndarray is returned. Raises ------ ValueError Raises ValueError if `a` contains NaN (Not a Number) or Inf (Infinity). See Also -------- asarray : Create and array. asanyarray : Similar function which passes through subclasses. ascontiguousarray : Convert input to a contiguous array. asfarray : Convert input to a floating point ndarray. asfortranarray : Convert input to an ndarray with column-major memory order. fromiter : Create an array from an iterator. fromfunction : Construct an array by executing a function on grid positions. Examples -------- Convert a list into an array. If all elements are finite ``asarray_chkfinite`` is identical to ``asarray``. >>> a = [1, 2] >>> np.asarray_chkfinite(a, dtype=float) array([1., 2.]) Raises ValueError if array_like contains Nans or Infs. >>> a = [1, 2, np.inf] >>> try: ... np.asarray_chkfinite(a) ... except ValueError: ... print 'ValueError' ... ValueError """ a = asarray(a, dtype=dtype, order=order) if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all(): raise ValueError( "array must not contain infs or NaNs") return a def piecewise(x, condlist, funclist, *args, **kw): """ Evaluate a piecewise-defined function. Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true. Parameters ---------- x : ndarray The input domain. condlist : list of bool arrays Each boolean array corresponds to a function in `funclist`. Wherever `condlist[i]` is True, `funclist[i](x)` is used as the output value. Each boolean array in `condlist` selects a piece of `x`, and should therefore be of the same shape as `x`. The length of `condlist` must correspond to that of `funclist`. If one extra function is given, i.e. if ``len(funclist) - len(condlist) == 1``, then that extra function is the default value, used wherever all conditions are false. funclist : list of callables, f(x,*args,**kw), or scalars Each function is evaluated over `x` wherever its corresponding condition is True. It should take an array as input and give an array or a scalar value as output. If, instead of a callable, a scalar is provided then a constant function (``lambda x: scalar``) is assumed. args : tuple, optional Any further arguments given to `piecewise` are passed to the functions upon execution, i.e., if called ``piecewise(..., ..., 1, 'a')``, then each function is called as ``f(x, 1, 'a')``. kw : dict, optional Keyword arguments used in calling `piecewise` are passed to the functions upon execution, i.e., if called ``piecewise(..., ..., lambda=1)``, then each function is called as ``f(x, lambda=1)``. Returns ------- out : ndarray The output is the same shape and type as x and is found by calling the functions in `funclist` on the appropriate portions of `x`, as defined by the boolean arrays in `condlist`. Portions not covered by any condition have a default value of 0. See Also -------- choose, select, where Notes ----- This is similar to choose or select, except that functions are evaluated on elements of `x` that satisfy the corresponding condition from `condlist`. The result is:: |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- Examples -------- Define the sigma function, which is -1 for ``x < 0`` and +1 for ``x >= 0``. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.]) Define the absolute value, which is ``-x`` for ``x <0`` and ``x`` for ``x >= 0``. >>> np.piecewise(x, [x < 0, x >= 0], [lambda x: -x, lambda x: x]) array([ 2.5, 1.5, 0.5, 0.5, 1.5, 2.5]) """ x = asanyarray(x) n2 = len(funclist) if (isscalar(condlist) or not (isinstance(condlist[0], list) or isinstance(condlist[0], ndarray))): condlist = [condlist] condlist = array(condlist, dtype=bool) n = len(condlist) # This is a hack to work around problems with NumPy's # handling of 0-d arrays and boolean indexing with # numpy.bool_ scalars zerod = False if x.ndim == 0: x = x[None] zerod = True if condlist.shape[-1] != 1: condlist = condlist.T if n == n2 - 1: # compute the "otherwise" condition. totlist = np.logical_or.reduce(condlist, axis=0) condlist = np.vstack([condlist, ~totlist]) n += 1 if (n != n2): raise ValueError( "function list and condition list must be the same") y = zeros(x.shape, x.dtype) for k in range(n): item = funclist[k] if not isinstance(item, collections.Callable): y[condlist[k]] = item else: vals = x[condlist[k]] if vals.size > 0: y[condlist[k]] = item(vals, *args, **kw) if zerod: y = y.squeeze() return y def select(condlist, choicelist, default=0): """ Return an array drawn from elements in choicelist, depending on conditions. Parameters ---------- condlist : list of bool ndarrays The list of conditions which determine from which array in `choicelist` the output elements are taken. When multiple conditions are satisfied, the first one encountered in `condlist` is used. choicelist : list of ndarrays The list of arrays from which the output elements are taken. It has to be of the same length as `condlist`. default : scalar, optional The element inserted in `output` when all conditions evaluate to False. Returns ------- output : ndarray The output at position m is the m-th element of the array in `choicelist` where the m-th element of the corresponding array in `condlist` is True. See Also -------- where : Return elements from one of two arrays depending on condition. take, choose, compress, diag, diagonal Examples -------- >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81]) """ # Check the size of condlist and choicelist are the same, or abort. if len(condlist) != len(choicelist): raise ValueError( 'list of cases must be same length as list of conditions') # Now that the dtype is known, handle the deprecated select([], []) case if len(condlist) == 0: # 2014-02-24, 1.9 warnings.warn("select with an empty condition list is not possible" "and will be deprecated", DeprecationWarning) return np.asarray(default)[()] choicelist = [np.asarray(choice) for choice in choicelist] choicelist.append(np.asarray(default)) # need to get the result type before broadcasting for correct scalar # behaviour dtype = np.result_type(*choicelist) # Convert conditions to arrays and broadcast conditions and choices # as the shape is needed for the result. Doing it seperatly optimizes # for example when all choices are scalars. condlist = np.broadcast_arrays(*condlist) choicelist = np.broadcast_arrays(*choicelist) # If cond array is not an ndarray in boolean format or scalar bool, abort. deprecated_ints = False for i in range(len(condlist)): cond = condlist[i] if cond.dtype.type is not np.bool_: if np.issubdtype(cond.dtype, np.integer): # A previous implementation accepted int ndarrays accidentally. # Supported here deliberately, but deprecated. condlist[i] = condlist[i].astype(bool) deprecated_ints = True else: raise ValueError( 'invalid entry in choicelist: should be boolean ndarray') if deprecated_ints: # 2014-02-24, 1.9 msg = "select condlists containing integer ndarrays is deprecated " \ "and will be removed in the future. Use `.astype(bool)` to " \ "convert to bools." warnings.warn(msg, DeprecationWarning) if choicelist[0].ndim == 0: # This may be common, so avoid the call. result_shape = condlist[0].shape else: result_shape = np.broadcast_arrays(condlist[0], choicelist[0])[0].shape result = np.full(result_shape, choicelist[-1], dtype) # Use np.copyto to burn each choicelist array onto result, using the # corresponding condlist as a boolean mask. This is done in reverse # order since the first choice should take precedence. choicelist = choicelist[-2::-1] condlist = condlist[::-1] for choice, cond in zip(choicelist, condlist): np.copyto(result, choice, where=cond) return result def copy(a, order='K'): """ Return an array copy of the given object. Parameters ---------- a : array_like Input data. order : {'C', 'F', 'A', 'K'}, optional Controls the memory layout of the copy. 'C' means C-order, 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of `a` as closely as possible. (Note that this function and :meth:ndarray.copy are very similar, but have different default values for their order= arguments.) Returns ------- arr : ndarray Array interpretation of `a`. Notes ----- This is equivalent to >>> np.array(a, copy=True) #doctest: +SKIP Examples -------- Create an array x, with a reference y and a copy z: >>> x = np.array([1, 2, 3]) >>> y = x >>> z = np.copy(x) Note that, when we modify x, y changes, but not z: >>> x[0] = 10 >>> x[0] == y[0] True >>> x[0] == z[0] False """ return array(a, order=order, copy=True) # Basic operations def gradient(f, *varargs, **kwargs): """ Return the gradient of an N-dimensional array. The gradient is computed using second order accurate central differences in the interior and either first differences or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array. Parameters ---------- f : array_like An N-dimensional array containing samples of a scalar function. varargs : list of scalar, optional N scalars specifying the sample distances for each dimension, i.e. `dx`, `dy`, `dz`, ... Default distance: 1. edge_order : {1, 2}, optional Gradient is calculated using N\ :sup:`th` order accurate differences at the boundaries. Default: 1. .. versionadded:: 1.9.1 Returns ------- gradient : list of ndarray Each element of `list` has the same shape as `f` giving the derivative of `f` with respect to each dimension. Examples -------- >>> x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float) >>> np.gradient(x) array([ 1. , 1.5, 2.5, 3.5, 4.5, 5. ]) >>> np.gradient(x, 2) array([ 0.5 , 0.75, 1.25, 1.75, 2.25, 2.5 ]) For two dimensional arrays, the return will be two arrays ordered by axis. In this example the first array stands for the gradient in rows and the second one in columns direction: >>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float)) [array([[ 2., 2., -1.], [ 2., 2., -1.]]), array([[ 1. , 2.5, 4. ], [ 1. , 1. , 1. ]])] >>> x = np.array([0, 1, 2, 3, 4]) >>> dx = np.gradient(x) >>> y = x**2 >>> np.gradient(y, dx, edge_order=2) array([-0., 2., 4., 6., 8.]) """ f = np.asanyarray(f) N = len(f.shape) # number of dimensions n = len(varargs) if n == 0: dx = [1.0]*N elif n == 1: dx = [varargs[0]]*N elif n == N: dx = list(varargs) else: raise SyntaxError( "invalid number of arguments") edge_order = kwargs.pop('edge_order', 1) if kwargs: raise TypeError('"{}" are not valid keyword arguments.'.format( '", "'.join(kwargs.keys()))) if edge_order > 2: raise ValueError("'edge_order' greater than 2 not supported") # use central differences on interior and one-sided differences on the # endpoints. This preserves second order-accuracy over the full domain. outvals = [] # create slice objects --- initially all are [:, :, ..., :] slice1 = [slice(None)]*N slice2 = [slice(None)]*N slice3 = [slice(None)]*N slice4 = [slice(None)]*N otype = f.dtype.char if otype not in ['f', 'd', 'F', 'D', 'm', 'M']: otype = 'd' # Difference of datetime64 elements results in timedelta64 if otype == 'M': # Need to use the full dtype name because it contains unit information otype = f.dtype.name.replace('datetime', 'timedelta') elif otype == 'm': # Needs to keep the specific units, can't be a general unit otype = f.dtype # Convert datetime64 data into ints. Make dummy variable `y` # that is a view of ints if the data is datetime64, otherwise # just set y equal to the the array `f`. if f.dtype.char in ["M", "m"]: y = f.view('int64') else: y = f for axis in range(N): if y.shape[axis] < 2: raise ValueError( "Shape of array too small to calculate a numerical gradient, " "at least two elements are required.") # Numerical differentiation: 1st order edges, 2nd order interior if y.shape[axis] == 2 or edge_order == 1: # Use first order differences for time data out = np.empty_like(y, dtype=otype) slice1[axis] = slice(1, -1) slice2[axis] = slice(2, None) slice3[axis] = slice(None, -2) # 1D equivalent -- out[1:-1] = (y[2:] - y[:-2])/2.0 out[slice1] = (y[slice2] - y[slice3])/2.0 slice1[axis] = 0 slice2[axis] = 1 slice3[axis] = 0 # 1D equivalent -- out[0] = (y[1] - y[0]) out[slice1] = (y[slice2] - y[slice3]) slice1[axis] = -1 slice2[axis] = -1 slice3[axis] = -2 # 1D equivalent -- out[-1] = (y[-1] - y[-2]) out[slice1] = (y[slice2] - y[slice3]) # Numerical differentiation: 2st order edges, 2nd order interior else: # Use second order differences where possible out = np.empty_like(y, dtype=otype) slice1[axis] = slice(1, -1) slice2[axis] = slice(2, None) slice3[axis] = slice(None, -2) # 1D equivalent -- out[1:-1] = (y[2:] - y[:-2])/2.0 out[slice1] = (y[slice2] - y[slice3])/2.0 slice1[axis] = 0 slice2[axis] = 0 slice3[axis] = 1 slice4[axis] = 2 # 1D equivalent -- out[0] = -(3*y[0] - 4*y[1] + y[2]) / 2.0 out[slice1] = -(3.0*y[slice2] - 4.0*y[slice3] + y[slice4])/2.0 slice1[axis] = -1 slice2[axis] = -1 slice3[axis] = -2 slice4[axis] = -3 # 1D equivalent -- out[-1] = (3*y[-1] - 4*y[-2] + y[-3]) out[slice1] = (3.0*y[slice2] - 4.0*y[slice3] + y[slice4])/2.0 # divide by step size out /= dx[axis] outvals.append(out) # reset the slice object in this dimension to ":" slice1[axis] = slice(None) slice2[axis] = slice(None) slice3[axis] = slice(None) slice4[axis] = slice(None) if N == 1: return outvals[0] else: return outvals def diff(a, n=1, axis=-1): """ Calculate the n-th order discrete difference along given axis. The first order difference is given by ``out[n] = a[n+1] - a[n]`` along the given axis, higher order differences are calculated by using `diff` recursively. Parameters ---------- a : array_like Input array n : int, optional The number of times values are differenced. axis : int, optional The axis along which the difference is taken, default is the last axis. Returns ------- diff : ndarray The `n` order differences. The shape of the output is the same as `a` except along `axis` where the dimension is smaller by `n`. See Also -------- gradient, ediff1d, cumsum Examples -------- >>> x = np.array([1, 2, 4, 7, 0]) >>> np.diff(x) array([ 1, 2, 3, -7]) >>> np.diff(x, n=2) array([ 1, 1, -10]) >>> x = np.array([[1, 3, 6, 10], [0, 5, 6, 8]]) >>> np.diff(x) array([[2, 3, 4], [5, 1, 2]]) >>> np.diff(x, axis=0) array([[-1, 2, 0, -2]]) """ if n == 0: return a if n < 0: raise ValueError( "order must be non-negative but got " + repr(n)) a = asanyarray(a) nd = len(a.shape) slice1 = [slice(None)]*nd slice2 = [slice(None)]*nd slice1[axis] = slice(1, None) slice2[axis] = slice(None, -1) slice1 = tuple(slice1) slice2 = tuple(slice2) if n > 1: return diff(a[slice1]-a[slice2], n-1, axis=axis) else: return a[slice1]-a[slice2] def interp(x, xp, fp, left=None, right=None, period=None): """ One-dimensional linear interpolation. Returns the one-dimensional piecewise linear interpolant to a function with given values at discrete data-points. Parameters ---------- x : array_like The x-coordinates of the interpolated values. xp : 1-D sequence of floats The x-coordinates of the data points, must be increasing if argument `period` is not specified. Otherwise, `xp` is internally sorted after normalizing the periodic boundaries with ``xp = xp % period``. fp : 1-D sequence of floats The y-coordinates of the data points, same length as `xp`. left : float, optional Value to return for `x < xp[0]`, default is `fp[0]`. right : float, optional Value to return for `x > xp[-1]`, default is `fp[-1]`. period : None or float, optional A period for the x-coordinates. This parameter allows the proper interpolation of angular x-coordinates. Parameters `left` and `right` are ignored if `period` is specified. .. versionadded:: 1.10.0 Returns ------- y : float or ndarray The interpolated values, same shape as `x`. Raises ------ ValueError If `xp` and `fp` have different length If `xp` or `fp` are not 1-D sequences If `period == 0` Notes ----- Does not check that the x-coordinate sequence `xp` is increasing. If `xp` is not increasing, the results are nonsense. A simple check for increasing is:: np.all(np.diff(xp) > 0) Examples -------- >>> xp = [1, 2, 3] >>> fp = [3, 2, 0] >>> np.interp(2.5, xp, fp) 1.0 >>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp) array([ 3. , 3. , 2.5 , 0.56, 0. ]) >>> UNDEF = -99.0 >>> np.interp(3.14, xp, fp, right=UNDEF) -99.0 Plot an interpolant to the sine function: >>> x = np.linspace(0, 2*np.pi, 10) >>> y = np.sin(x) >>> xvals = np.linspace(0, 2*np.pi, 50) >>> yinterp = np.interp(xvals, x, y) >>> import matplotlib.pyplot as plt >>> plt.plot(x, y, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.plot(xvals, yinterp, '-x') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.show() Interpolation with periodic x-coordinates: >>> x = [-180, -170, -185, 185, -10, -5, 0, 365] >>> xp = [190, -190, 350, -350] >>> fp = [5, 10, 3, 4] >>> np.interp(x, xp, fp, period=360) array([7.5, 5., 8.75, 6.25, 3., 3.25, 3.5, 3.75]) """ if period is None: if isinstance(x, (float, int, number)): return compiled_interp([x], xp, fp, left, right).item() elif isinstance(x, np.ndarray) and x.ndim == 0: return compiled_interp([x], xp, fp, left, right).item() else: return compiled_interp(x, xp, fp, left, right) else: if period == 0: raise ValueError("period must be a non-zero value") period = abs(period) left = None right = None return_array = True if isinstance(x, (float, int, number)): return_array = False x = [x] x = np.asarray(x, dtype=np.float64) xp = np.asarray(xp, dtype=np.float64) fp = np.asarray(fp, dtype=np.float64) if xp.ndim != 1 or fp.ndim != 1: raise ValueError("Data points must be 1-D sequences") if xp.shape[0] != fp.shape[0]: raise ValueError("fp and xp are not of the same length") # normalizing periodic boundaries x = x % period xp = xp % period asort_xp = np.argsort(xp) xp = xp[asort_xp] fp = fp[asort_xp] xp = np.concatenate((xp[-1:]-period, xp, xp[0:1]+period)) fp = np.concatenate((fp[-1:], fp, fp[0:1])) if return_array: return compiled_interp(x, xp, fp, left, right) else: return compiled_interp(x, xp, fp, left, right).item() def angle(z, deg=0): """ Return the angle of the complex argument. Parameters ---------- z : array_like A complex number or sequence of complex numbers. deg : bool, optional Return angle in degrees if True, radians if False (default). Returns ------- angle : ndarray or scalar The counterclockwise angle from the positive real axis on the complex plane, with dtype as numpy.float64. See Also -------- arctan2 absolute Examples -------- >>> np.angle([1.0, 1.0j, 1+1j]) # in radians array([ 0. , 1.57079633, 0.78539816]) >>> np.angle(1+1j, deg=True) # in degrees 45.0 """ if deg: fact = 180/pi else: fact = 1.0 z = asarray(z) if (issubclass(z.dtype.type, _nx.complexfloating)): zimag = z.imag zreal = z.real else: zimag = 0 zreal = z return arctan2(zimag, zreal) * fact def unwrap(p, discont=pi, axis=-1): """ Unwrap by changing deltas between values to 2*pi complement. Unwrap radian phase `p` by changing absolute jumps greater than `discont` to their 2*pi complement along the given axis. Parameters ---------- p : array_like Input array. discont : float, optional Maximum discontinuity between values, default is ``pi``. axis : int, optional Axis along which unwrap will operate, default is the last axis. Returns ------- out : ndarray Output array. See Also -------- rad2deg, deg2rad Notes ----- If the discontinuity in `p` is smaller than ``pi``, but larger than `discont`, no unwrapping is done because taking the 2*pi complement would only make the discontinuity larger. Examples -------- >>> phase = np.linspace(0, np.pi, num=5) >>> phase[3:] += np.pi >>> phase array([ 0. , 0.78539816, 1.57079633, 5.49778714, 6.28318531]) >>> np.unwrap(phase) array([ 0. , 0.78539816, 1.57079633, -0.78539816, 0. ]) """ p = asarray(p) nd = len(p.shape) dd = diff(p, axis=axis) slice1 = [slice(None, None)]*nd # full slices slice1[axis] = slice(1, None) ddmod = mod(dd + pi, 2*pi) - pi _nx.copyto(ddmod, pi, where=(ddmod == -pi) & (dd > 0)) ph_correct = ddmod - dd _nx.copyto(ph_correct, 0, where=abs(dd) < discont) up = array(p, copy=True, dtype='d') up[slice1] = p[slice1] + ph_correct.cumsum(axis) return up def sort_complex(a): """ Sort a complex array using the real part first, then the imaginary part. Parameters ---------- a : array_like Input array Returns ------- out : complex ndarray Always returns a sorted complex array. Examples -------- >>> np.sort_complex([5, 3, 6, 2, 1]) array([ 1.+0.j, 2.+0.j, 3.+0.j, 5.+0.j, 6.+0.j]) >>> np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j]) array([ 1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j]) """ b = array(a, copy=True) b.sort() if not issubclass(b.dtype.type, _nx.complexfloating): if b.dtype.char in 'bhBH': return b.astype('F') elif b.dtype.char == 'g': return b.astype('G') else: return b.astype('D') else: return b def trim_zeros(filt, trim='fb'): """ Trim the leading and/or trailing zeros from a 1-D array or sequence. Parameters ---------- filt : 1-D array or sequence Input array. trim : str, optional A string with 'f' representing trim from front and 'b' to trim from back. Default is 'fb', trim zeros from both front and back of the array. Returns ------- trimmed : 1-D array or sequence The result of trimming the input. The input data type is preserved. Examples -------- >>> a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0)) >>> np.trim_zeros(a) array([1, 2, 3, 0, 2, 1]) >>> np.trim_zeros(a, 'b') array([0, 0, 0, 1, 2, 3, 0, 2, 1]) The input data type is preserved, list/tuple in means list/tuple out. >>> np.trim_zeros([0, 1, 2, 0]) [1, 2] """ first = 0 trim = trim.upper() if 'F' in trim: for i in filt: if i != 0.: break else: first = first + 1 last = len(filt) if 'B' in trim: for i in filt[::-1]: if i != 0.: break else: last = last - 1 return filt[first:last] @deprecate def unique(x): """ This function is deprecated. Use numpy.lib.arraysetops.unique() instead. """ try: tmp = x.flatten() if tmp.size == 0: return tmp tmp.sort() idx = concatenate(([True], tmp[1:] != tmp[:-1])) return tmp[idx] except AttributeError: items = sorted(set(x)) return asarray(items) def extract(condition, arr): """ Return the elements of an array that satisfy some condition. This is equivalent to ``np.compress(ravel(condition), ravel(arr))``. If `condition` is boolean ``np.extract`` is equivalent to ``arr[condition]``. Note that `place` does the exact opposite of `extract`. Parameters ---------- condition : array_like An array whose nonzero or True entries indicate the elements of `arr` to extract. arr : array_like Input array of the same size as `condition`. Returns ------- extract : ndarray Rank 1 array of values from `arr` where `condition` is True. See Also -------- take, put, copyto, compress, place Examples -------- >>> arr = np.arange(12).reshape((3, 4)) >>> arr array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> condition = np.mod(arr, 3)==0 >>> condition array([[ True, False, False, True], [False, False, True, False], [False, True, False, False]], dtype=bool) >>> np.extract(condition, arr) array([0, 3, 6, 9]) If `condition` is boolean: >>> arr[condition] array([0, 3, 6, 9]) """ return _nx.take(ravel(arr), nonzero(ravel(condition))[0]) def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : array_like Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N it will be repeated. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ return _insert(arr, mask, vals) def disp(mesg, device=None, linefeed=True): """ Display a message on a device. Parameters ---------- mesg : str Message to display. device : object Device to write message. If None, defaults to ``sys.stdout`` which is very similar to ``print``. `device` needs to have ``write()`` and ``flush()`` methods. linefeed : bool, optional Option whether to print a line feed or not. Defaults to True. Raises ------ AttributeError If `device` does not have a ``write()`` or ``flush()`` method. Examples -------- Besides ``sys.stdout``, a file-like object can also be used as it has both required methods: >>> from StringIO import StringIO >>> buf = StringIO() >>> np.disp('"Display" in a file', device=buf) >>> buf.getvalue() '"Display" in a file\\n' """ if device is None: device = sys.stdout if linefeed: device.write('%s\n' % mesg) else: device.write('%s' % mesg) device.flush() return class vectorize(object): """ vectorize(pyfunc, otypes='', doc=None, excluded=None, cache=False) Generalized function class. Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a numpy array as output. The vectorized function evaluates `pyfunc` over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy. The data type of the output of `vectorized` is determined by calling the function with the first element of the input. This can be avoided by specifying the `otypes` argument. Parameters ---------- pyfunc : callable A python function or method. otypes : str or list of dtypes, optional The output data type. It must be specified as either a string of typecode characters or a list of data type specifiers. There should be one data type specifier for each output. doc : str, optional The docstring for the function. If `None`, the docstring will be the ``pyfunc.__doc__``. excluded : set, optional Set of strings or integers representing the positional or keyword arguments for which the function will not be vectorized. These will be passed directly to `pyfunc` unmodified. .. versionadded:: 1.7.0 cache : bool, optional If `True`, then cache the first function call that determines the number of outputs if `otypes` is not provided. .. versionadded:: 1.7.0 Returns ------- vectorized : callable Vectorized function. Examples -------- >>> def myfunc(a, b): ... "Return a-b if a>b, otherwise return a+b" ... if a > b: ... return a - b ... else: ... return a + b >>> vfunc = np.vectorize(myfunc) >>> vfunc([1, 2, 3, 4], 2) array([3, 4, 1, 2]) The docstring is taken from the input function to `vectorize` unless it is specified >>> vfunc.__doc__ 'Return a-b if a>b, otherwise return a+b' >>> vfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`') >>> vfunc.__doc__ 'Vectorized `myfunc`' The output type is determined by evaluating the first element of the input, unless it is specified >>> out = vfunc([1, 2, 3, 4], 2) >>> type(out[0]) <type 'numpy.int32'> >>> vfunc = np.vectorize(myfunc, otypes=[np.float]) >>> out = vfunc([1, 2, 3, 4], 2) >>> type(out[0]) <type 'numpy.float64'> The `excluded` argument can be used to prevent vectorizing over certain arguments. This can be useful for array-like arguments of a fixed length such as the coefficients for a polynomial as in `polyval`: >>> def mypolyval(p, x): ... _p = list(p) ... res = _p.pop(0) ... while _p: ... res = res*x + _p.pop(0) ... return res >>> vpolyval = np.vectorize(mypolyval, excluded=['p']) >>> vpolyval(p=[1, 2, 3], x=[0, 1]) array([3, 6]) Positional arguments may also be excluded by specifying their position: >>> vpolyval.excluded.add(0) >>> vpolyval([1, 2, 3], x=[0, 1]) array([3, 6]) Notes ----- The `vectorize` function is provided primarily for convenience, not for performance. The implementation is essentially a for loop. If `otypes` is not specified, then a call to the function with the first argument will be used to determine the number of outputs. The results of this call will be cached if `cache` is `True` to prevent calling the function twice. However, to implement the cache, the original function must be wrapped which will slow down subsequent calls, so only do this if your function is expensive. The new keyword argument interface and `excluded` argument support further degrades performance. """ def __init__(self, pyfunc, otypes='', doc=None, excluded=None, cache=False): self.pyfunc = pyfunc self.cache = cache self._ufunc = None # Caching to improve default performance if doc is None: self.__doc__ = pyfunc.__doc__ else: self.__doc__ = doc if isinstance(otypes, str): self.otypes = otypes for char in self.otypes: if char not in typecodes['All']: raise ValueError( "Invalid otype specified: %s" % (char,)) elif iterable(otypes): self.otypes = ''.join([_nx.dtype(x).char for x in otypes]) else: raise ValueError( "Invalid otype specification") # Excluded variable support if excluded is None: excluded = set() self.excluded = set(excluded) def __call__(self, *args, **kwargs): """ Return arrays with the results of `pyfunc` broadcast (vectorized) over `args` and `kwargs` not in `excluded`. """ excluded = self.excluded if not kwargs and not excluded: func = self.pyfunc vargs = args else: # The wrapper accepts only positional arguments: we use `names` and # `inds` to mutate `the_args` and `kwargs` to pass to the original # function. nargs = len(args) names = [_n for _n in kwargs if _n not in excluded] inds = [_i for _i in range(nargs) if _i not in excluded] the_args = list(args) def func(*vargs): for _n, _i in enumerate(inds): the_args[_i] = vargs[_n] kwargs.update(zip(names, vargs[len(inds):])) return self.pyfunc(*the_args, **kwargs) vargs = [args[_i] for _i in inds] vargs.extend([kwargs[_n] for _n in names]) return self._vectorize_call(func=func, args=vargs) def _get_ufunc_and_otypes(self, func, args): """Return (ufunc, otypes).""" # frompyfunc will fail if args is empty if not args: raise ValueError('args can not be empty') if self.otypes: otypes = self.otypes nout = len(otypes) # Note logic here: We only *use* self._ufunc if func is self.pyfunc # even though we set self._ufunc regardless. if func is self.pyfunc and self._ufunc is not None: ufunc = self._ufunc else: ufunc = self._ufunc = frompyfunc(func, len(args), nout) else: # Get number of outputs and output types by calling the function on # the first entries of args. We also cache the result to prevent # the subsequent call when the ufunc is evaluated. # Assumes that ufunc first evaluates the 0th elements in the input # arrays (the input values are not checked to ensure this) inputs = [asarray(_a).flat[0] for _a in args] outputs = func(*inputs) # Performance note: profiling indicates that -- for simple # functions at least -- this wrapping can almost double the # execution time. # Hence we make it optional. if self.cache: _cache = [outputs] def _func(*vargs): if _cache: return _cache.pop() else: return func(*vargs) else: _func = func if isinstance(outputs, tuple): nout = len(outputs) else: nout = 1 outputs = (outputs,) otypes = ''.join([asarray(outputs[_k]).dtype.char for _k in range(nout)]) # Performance note: profiling indicates that creating the ufunc is # not a significant cost compared with wrapping so it seems not # worth trying to cache this. ufunc = frompyfunc(_func, len(args), nout) return ufunc, otypes def _vectorize_call(self, func, args): """Vectorized call to `func` over positional `args`.""" if not args: _res = func() else: ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args) # Convert args to object arrays first inputs = [array(_a, copy=False, subok=True, dtype=object) for _a in args] outputs = ufunc(*inputs) if ufunc.nout == 1: _res = array(outputs, copy=False, subok=True, dtype=otypes[0]) else: _res = tuple([array(_x, copy=False, subok=True, dtype=_t) for _x, _t in zip(outputs, otypes)]) return _res def cov(m, y=None, rowvar=1, bias=0, ddof=None, fweights=None, aweights=None): """ Estimate a covariance matrix, given data and weights. Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, :math:`X = [x_1, x_2, ... x_N]^T`, then the covariance matrix element :math:`C_{ij}` is the covariance of :math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance of :math:`x_i`. See the notes for an outline of the algorithm. Parameters ---------- m : array_like A 1-D or 2-D array containing multiple variables and observations. Each row of `m` represents a variable, and each column a single observation of all those variables. Also see `rowvar` below. y : array_like, optional An additional set of variables and observations. `y` has the same form as that of `m`. rowvar : int, optional If `rowvar` is non-zero (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. bias : int, optional Default normalization is by ``(N - 1)``, where ``N`` corresponds to the number of observations given (unbiased estimate). If `bias` is 1, then normalization is by ``N``. These values can be overridden by using the keyword ``ddof`` in numpy versions >= 1.5. ddof : int, optional If not ``None`` the default value implied by `bias` is overridden. Note that ``ddof=1`` will return the unbiased estimate, even if both `fweights` and `aweights` are specified, and ``ddof=0`` will return the simple average. See the notes for the details. The default value is ``None``. .. versionadded:: 1.5 fweights : array_like, int, optional 1-D array of integer freguency weights; the number of times each observation vector should be repeated. .. versionadded:: 1.10 aweights : array_like, optional 1-D array of observation vector weights. These relative weights are typically large for observations considered "important" and smaller for observations considered less "important". If ``ddof=0`` the array of weights can be used to assign probabilities to observation vectors. .. versionadded:: 1.10 Returns ------- out : ndarray The covariance matrix of the variables. See Also -------- corrcoef : Normalized covariance matrix Notes ----- Assume that the observations are in the columns of the observation array `m` and let ``f = fweights`` and ``a = aweights`` for brevity. The steps to compute the weighted covariance are as follows:: >>> w = f * a >>> v1 = np.sum(w) >>> v2 = np.sum(w * a) >>> m -= np.sum(m * w, axis=1, keepdims=True) / v1 >>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2) Note that when ``a == 1``, the normalization factor ``v1 / (v1**2 - ddof * v2)`` goes over to ``1 / (np.sum(f) - ddof)`` as it should. Examples -------- Consider two variables, :math:`x_0` and :math:`x_1`, which correlate perfectly, but in opposite directions: >>> x = np.array([[0, 2], [1, 1], [2, 0]]).T >>> x array([[0, 1, 2], [2, 1, 0]]) Note how :math:`x_0` increases while :math:`x_1` decreases. The covariance matrix shows this clearly: >>> np.cov(x) array([[ 1., -1.], [-1., 1.]]) Note that element :math:`C_{0,1}`, which shows the correlation between :math:`x_0` and :math:`x_1`, is negative. Further, note how `x` and `y` are combined: >>> x = [-2.1, -1, 4.3] >>> y = [3, 1.1, 0.12] >>> X = np.vstack((x,y)) >>> print np.cov(X) [[ 11.71 -4.286 ] [ -4.286 2.14413333]] >>> print np.cov(x, y) [[ 11.71 -4.286 ] [ -4.286 2.14413333]] >>> print np.cov(x) 11.71 """ # Check inputs if ddof is not None and ddof != int(ddof): raise ValueError( "ddof must be integer") # Handles complex arrays too m = np.asarray(m) if y is None: dtype = np.result_type(m, np.float64) else: y = np.asarray(y) dtype = np.result_type(m, y, np.float64) X = array(m, ndmin=2, dtype=dtype) if rowvar == 0 and X.shape[0] != 1: X = X.T if X.shape[0] == 0: return np.array([]).reshape(0, 0) if y is not None: y = array(y, copy=False, ndmin=2, dtype=dtype) if rowvar == 0 and y.shape[0] != 1: y = y.T X = np.vstack((X, y)) if ddof is None: if bias == 0: ddof = 1 else: ddof = 0 # Get the product of frequencies and weights w = None if fweights is not None: fweights = np.asarray(fweights, dtype=np.float) if not np.all(fweights == np.around(fweights)): raise TypeError( "fweights must be integer") if fweights.ndim > 1: raise RuntimeError( "cannot handle multidimensional fweights") if fweights.shape[0] != X.shape[1]: raise RuntimeError( "incompatible numbers of samples and fweights") if any(fweights < 0): raise ValueError( "fweights cannot be negative") w = fweights if aweights is not None: aweights = np.asarray(aweights, dtype=np.float) if aweights.ndim > 1: raise RuntimeError( "cannot handle multidimensional aweights") if aweights.shape[0] != X.shape[1]: raise RuntimeError( "incompatible numbers of samples and aweights") if any(aweights < 0): raise ValueError( "aweights cannot be negative") if w is None: w = aweights else: w *= aweights avg, w_sum = average(X, axis=1, weights=w, returned=True) w_sum = w_sum[0] # Determine the normalization if w is None: fact = float(X.shape[1] - ddof) elif ddof == 0: fact = w_sum elif aweights is None: fact = w_sum - ddof else: fact = w_sum - ddof*sum(w*aweights)/w_sum if fact <= 0: warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning) fact = 0.0 X -= avg[:, None] if w is None: X_T = X.T else: X_T = (X*w).T return (dot(X, X_T.conj())/fact).squeeze() def corrcoef(x, y=None, rowvar=1, bias=np._NoValue, ddof=np._NoValue): """ Return Pearson product-moment correlation coefficients. Please refer to the documentation for `cov` for more detail. The relationship between the correlation coefficient matrix, `R`, and the covariance matrix, `C`, is .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } The values of `R` are between -1 and 1, inclusive. Parameters ---------- x : array_like A 1-D or 2-D array containing multiple variables and observations. Each row of `x` represents a variable, and each column a single observation of all those variables. Also see `rowvar` below. y : array_like, optional An additional set of variables and observations. `y` has the same shape as `x`. rowvar : int, optional If `rowvar` is non-zero (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. bias : _NoValue, optional Has no affect, do not use. .. deprecated:: 1.10.0 ddof : _NoValue, optional Has no affect, do not use. .. deprecated:: 1.10.0 Returns ------- R : ndarray The correlation coefficient matrix of the variables. See Also -------- cov : Covariance matrix Notes ----- This function accepts but discards arguments `bias` and `ddof`. This is for backwards compatibility with previous versions of this function. These arguments had no effect on the return values of the function and can be safely ignored in this and previous versions of numpy. """ if bias is not np._NoValue or ddof is not np._NoValue: # 2015-03-15, 1.10 warnings.warn('bias and ddof have no affect and are deprecated', DeprecationWarning) c = cov(x, y, rowvar) try: d = diag(c) except ValueError: # scalar covariance # nan if incorrect value (nan, inf, 0), 1 otherwise return c / c return c / sqrt(multiply.outer(d, d)) def blackman(M): """ Return the Blackman window. The Blackman window is a taper formed by using the first three terms of a summation of cosines. It was designed to have close to the minimal leakage possible. It is close to optimal, only slightly worse than a Kaiser window. Parameters ---------- M : int Number of points in the output window. If zero or less, an empty array is returned. Returns ------- out : ndarray The window, with the maximum value normalized to one (the value one appears only if the number of samples is odd). See Also -------- bartlett, hamming, hanning, kaiser Notes ----- The Blackman window is defined as .. math:: w(n) = 0.42 - 0.5 \\cos(2\\pi n/M) + 0.08 \\cos(4\\pi n/M) Most references to the Blackman window come from the signal processing literature, where it is used as one of many windowing functions for smoothing values. It is also known as an apodization (which means "removing the foot", i.e. smoothing discontinuities at the beginning and end of the sampled signal) or tapering function. It is known as a "near optimal" tapering function, almost as good (by some measures) as the kaiser window. References ---------- Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra, Dover Publications, New York. Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing. Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471. Examples -------- >>> np.blackman(12) array([ -1.38777878e-17, 3.26064346e-02, 1.59903635e-01, 4.14397981e-01, 7.36045180e-01, 9.67046769e-01, 9.67046769e-01, 7.36045180e-01, 4.14397981e-01, 1.59903635e-01, 3.26064346e-02, -1.38777878e-17]) Plot the window and the frequency response: >>> from numpy.fft import fft, fftshift >>> window = np.blackman(51) >>> plt.plot(window) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Blackman window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Amplitude") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 >>> mag = np.abs(fftshift(A)) >>> freq = np.linspace(-0.5, 0.5, len(A)) >>> response = 20 * np.log10(mag) >>> response = np.clip(response, -100, 100) >>> plt.plot(freq, response) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Frequency response of Blackman window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Magnitude [dB]") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Normalized frequency [cycles per sample]") <matplotlib.text.Text object at 0x...> >>> plt.axis('tight') (-0.5, 0.5, -100.0, ...) >>> plt.show() """ if M < 1: return array([]) if M == 1: return ones(1, float) n = arange(0, M) return 0.42 - 0.5*cos(2.0*pi*n/(M-1)) + 0.08*cos(4.0*pi*n/(M-1)) def bartlett(M): """ Return the Bartlett window. The Bartlett window is very similar to a triangular window, except that the end points are at zero. It is often used in signal processing for tapering a signal, without generating too much ripple in the frequency domain. Parameters ---------- M : int Number of points in the output window. If zero or less, an empty array is returned. Returns ------- out : array The triangular window, with the maximum value normalized to one (the value one appears only if the number of samples is odd), with the first and last samples equal to zero. See Also -------- blackman, hamming, hanning, kaiser Notes ----- The Bartlett window is defined as .. math:: w(n) = \\frac{2}{M-1} \\left( \\frac{M-1}{2} - \\left|n - \\frac{M-1}{2}\\right| \\right) Most references to the Bartlett window come from the signal processing literature, where it is used as one of many windowing functions for smoothing values. Note that convolution with this window produces linear interpolation. It is also known as an apodization (which means"removing the foot", i.e. smoothing discontinuities at the beginning and end of the sampled signal) or tapering function. The fourier transform of the Bartlett is the product of two sinc functions. Note the excellent discussion in Kanasewich. References ---------- .. [1] M.S. Bartlett, "Periodogram Analysis and Continuous Spectra", Biometrika 37, 1-16, 1950. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The University of Alberta Press, 1975, pp. 109-110. .. [3] A.V. Oppenheim and R.W. Schafer, "Discrete-Time Signal Processing", Prentice-Hall, 1999, pp. 468-471. .. [4] Wikipedia, "Window function", http://en.wikipedia.org/wiki/Window_function .. [5] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, "Numerical Recipes", Cambridge University Press, 1986, page 429. Examples -------- >>> np.bartlett(12) array([ 0. , 0.18181818, 0.36363636, 0.54545455, 0.72727273, 0.90909091, 0.90909091, 0.72727273, 0.54545455, 0.36363636, 0.18181818, 0. ]) Plot the window and its frequency response (requires SciPy and matplotlib): >>> from numpy.fft import fft, fftshift >>> window = np.bartlett(51) >>> plt.plot(window) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Bartlett window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Amplitude") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 >>> mag = np.abs(fftshift(A)) >>> freq = np.linspace(-0.5, 0.5, len(A)) >>> response = 20 * np.log10(mag) >>> response = np.clip(response, -100, 100) >>> plt.plot(freq, response) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Frequency response of Bartlett window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Magnitude [dB]") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Normalized frequency [cycles per sample]") <matplotlib.text.Text object at 0x...> >>> plt.axis('tight') (-0.5, 0.5, -100.0, ...) >>> plt.show() """ if M < 1: return array([]) if M == 1: return ones(1, float) n = arange(0, M) return where(less_equal(n, (M-1)/2.0), 2.0*n/(M-1), 2.0 - 2.0*n/(M-1)) def hanning(M): """ Return the Hanning window. The Hanning window is a taper formed by using a weighted cosine. Parameters ---------- M : int Number of points in the output window. If zero or less, an empty array is returned. Returns ------- out : ndarray, shape(M,) The window, with the maximum value normalized to one (the value one appears only if `M` is odd). See Also -------- bartlett, blackman, hamming, kaiser Notes ----- The Hanning window is defined as .. math:: w(n) = 0.5 - 0.5cos\\left(\\frac{2\\pi{n}}{M-1}\\right) \\qquad 0 \\leq n \\leq M-1 The Hanning was named for Julius von Hann, an Austrian meteorologist. It is also known as the Cosine Bell. Some authors prefer that it be called a Hann window, to help avoid confusion with the very similar Hamming window. Most references to the Hanning window come from the signal processing literature, where it is used as one of many windowing functions for smoothing values. It is also known as an apodization (which means "removing the foot", i.e. smoothing discontinuities at the beginning and end of the sampled signal) or tapering function. References ---------- .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra, Dover Publications, New York. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The University of Alberta Press, 1975, pp. 106-108. .. [3] Wikipedia, "Window function", http://en.wikipedia.org/wiki/Window_function .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, "Numerical Recipes", Cambridge University Press, 1986, page 425. Examples -------- >>> np.hanning(12) array([ 0. , 0.07937323, 0.29229249, 0.57115742, 0.82743037, 0.97974649, 0.97974649, 0.82743037, 0.57115742, 0.29229249, 0.07937323, 0. ]) Plot the window and its frequency response: >>> from numpy.fft import fft, fftshift >>> window = np.hanning(51) >>> plt.plot(window) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Hann window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Amplitude") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 >>> mag = np.abs(fftshift(A)) >>> freq = np.linspace(-0.5, 0.5, len(A)) >>> response = 20 * np.log10(mag) >>> response = np.clip(response, -100, 100) >>> plt.plot(freq, response) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Frequency response of the Hann window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Magnitude [dB]") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Normalized frequency [cycles per sample]") <matplotlib.text.Text object at 0x...> >>> plt.axis('tight') (-0.5, 0.5, -100.0, ...) >>> plt.show() """ if M < 1: return array([]) if M == 1: return ones(1, float) n = arange(0, M) return 0.5 - 0.5*cos(2.0*pi*n/(M-1)) def hamming(M): """ Return the Hamming window. The Hamming window is a taper formed by using a weighted cosine. Parameters ---------- M : int Number of points in the output window. If zero or less, an empty array is returned. Returns ------- out : ndarray The window, with the maximum value normalized to one (the value one appears only if the number of samples is odd). See Also -------- bartlett, blackman, hanning, kaiser Notes ----- The Hamming window is defined as .. math:: w(n) = 0.54 - 0.46cos\\left(\\frac{2\\pi{n}}{M-1}\\right) \\qquad 0 \\leq n \\leq M-1 The Hamming was named for R. W. Hamming, an associate of J. W. Tukey and is described in Blackman and Tukey. It was recommended for smoothing the truncated autocovariance function in the time domain. Most references to the Hamming window come from the signal processing literature, where it is used as one of many windowing functions for smoothing values. It is also known as an apodization (which means "removing the foot", i.e. smoothing discontinuities at the beginning and end of the sampled signal) or tapering function. References ---------- .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra, Dover Publications, New York. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The University of Alberta Press, 1975, pp. 109-110. .. [3] Wikipedia, "Window function", http://en.wikipedia.org/wiki/Window_function .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, "Numerical Recipes", Cambridge University Press, 1986, page 425. Examples -------- >>> np.hamming(12) array([ 0.08 , 0.15302337, 0.34890909, 0.60546483, 0.84123594, 0.98136677, 0.98136677, 0.84123594, 0.60546483, 0.34890909, 0.15302337, 0.08 ]) Plot the window and the frequency response: >>> from numpy.fft import fft, fftshift >>> window = np.hamming(51) >>> plt.plot(window) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Hamming window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Amplitude") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 >>> mag = np.abs(fftshift(A)) >>> freq = np.linspace(-0.5, 0.5, len(A)) >>> response = 20 * np.log10(mag) >>> response = np.clip(response, -100, 100) >>> plt.plot(freq, response) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Frequency response of Hamming window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Magnitude [dB]") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Normalized frequency [cycles per sample]") <matplotlib.text.Text object at 0x...> >>> plt.axis('tight') (-0.5, 0.5, -100.0, ...) >>> plt.show() """ if M < 1: return array([]) if M == 1: return ones(1, float) n = arange(0, M) return 0.54 - 0.46*cos(2.0*pi*n/(M-1)) ## Code from cephes for i0 _i0A = [ -4.41534164647933937950E-18, 3.33079451882223809783E-17, -2.43127984654795469359E-16, 1.71539128555513303061E-15, -1.16853328779934516808E-14, 7.67618549860493561688E-14, -4.85644678311192946090E-13, 2.95505266312963983461E-12, -1.72682629144155570723E-11, 9.67580903537323691224E-11, -5.18979560163526290666E-10, 2.65982372468238665035E-9, -1.30002500998624804212E-8, 6.04699502254191894932E-8, -2.67079385394061173391E-7, 1.11738753912010371815E-6, -4.41673835845875056359E-6, 1.64484480707288970893E-5, -5.75419501008210370398E-5, 1.88502885095841655729E-4, -5.76375574538582365885E-4, 1.63947561694133579842E-3, -4.32430999505057594430E-3, 1.05464603945949983183E-2, -2.37374148058994688156E-2, 4.93052842396707084878E-2, -9.49010970480476444210E-2, 1.71620901522208775349E-1, -3.04682672343198398683E-1, 6.76795274409476084995E-1 ] _i0B = [ -7.23318048787475395456E-18, -4.83050448594418207126E-18, 4.46562142029675999901E-17, 3.46122286769746109310E-17, -2.82762398051658348494E-16, -3.42548561967721913462E-16, 1.77256013305652638360E-15, 3.81168066935262242075E-15, -9.55484669882830764870E-15, -4.15056934728722208663E-14, 1.54008621752140982691E-14, 3.85277838274214270114E-13, 7.18012445138366623367E-13, -1.79417853150680611778E-12, -1.32158118404477131188E-11, -3.14991652796324136454E-11, 1.18891471078464383424E-11, 4.94060238822496958910E-10, 3.39623202570838634515E-9, 2.26666899049817806459E-8, 2.04891858946906374183E-7, 2.89137052083475648297E-6, 6.88975834691682398426E-5, 3.36911647825569408990E-3, 8.04490411014108831608E-1 ] def _chbevl(x, vals): b0 = vals[0] b1 = 0.0 for i in range(1, len(vals)): b2 = b1 b1 = b0 b0 = x*b1 - b2 + vals[i] return 0.5*(b0 - b2) def _i0_1(x): return exp(x) * _chbevl(x/2.0-2, _i0A) def _i0_2(x): return exp(x) * _chbevl(32.0/x - 2.0, _i0B) / sqrt(x) def i0(x): """ Modified Bessel function of the first kind, order 0. Usually denoted :math:`I_0`. This function does broadcast, but will *not* "up-cast" int dtype arguments unless accompanied by at least one float or complex dtype argument (see Raises below). Parameters ---------- x : array_like, dtype float or complex Argument of the Bessel function. Returns ------- out : ndarray, shape = x.shape, dtype = x.dtype The modified Bessel function evaluated at each of the elements of `x`. Raises ------ TypeError: array cannot be safely cast to required type If argument consists exclusively of int dtypes. See Also -------- scipy.special.iv, scipy.special.ive Notes ----- We use the algorithm published by Clenshaw [1]_ and referenced by Abramowitz and Stegun [2]_, for which the function domain is partitioned into the two intervals [0,8] and (8,inf), and Chebyshev polynomial expansions are employed in each interval. Relative error on the domain [0,30] using IEEE arithmetic is documented [3]_ as having a peak of 5.8e-16 with an rms of 1.4e-16 (n = 30000). References ---------- .. [1] C. W. Clenshaw, "Chebyshev series for mathematical functions", in *National Physical Laboratory Mathematical Tables*, vol. 5, London: Her Majesty's Stationery Office, 1962. .. [2] M. Abramowitz and I. A. Stegun, *Handbook of Mathematical Functions*, 10th printing, New York: Dover, 1964, pp. 379. http://www.math.sfu.ca/~cbm/aands/page_379.htm .. [3] http://kobesearch.cpan.org/htdocs/Math-Cephes/Math/Cephes.html Examples -------- >>> np.i0([0.]) array(1.0) >>> np.i0([0., 1. + 2j]) array([ 1.00000000+0.j , 0.18785373+0.64616944j]) """ x = atleast_1d(x).copy() y = empty_like(x) ind = (x < 0) x[ind] = -x[ind] ind = (x <= 8.0) y[ind] = _i0_1(x[ind]) ind2 = ~ind y[ind2] = _i0_2(x[ind2]) return y.squeeze() ## End of cephes code for i0 def kaiser(M, beta): """ Return the Kaiser window. The Kaiser window is a taper formed by using a Bessel function. Parameters ---------- M : int Number of points in the output window. If zero or less, an empty array is returned. beta : float Shape parameter for window. Returns ------- out : array The window, with the maximum value normalized to one (the value one appears only if the number of samples is odd). See Also -------- bartlett, blackman, hamming, hanning Notes ----- The Kaiser window is defined as .. math:: w(n) = I_0\\left( \\beta \\sqrt{1-\\frac{4n^2}{(M-1)^2}} \\right)/I_0(\\beta) with .. math:: \\quad -\\frac{M-1}{2} \\leq n \\leq \\frac{M-1}{2}, where :math:`I_0` is the modified zeroth-order Bessel function. The Kaiser was named for Jim Kaiser, who discovered a simple approximation to the DPSS window based on Bessel functions. The Kaiser window is a very good approximation to the Digital Prolate Spheroidal Sequence, or Slepian window, which is the transform which maximizes the energy in the main lobe of the window relative to total energy. The Kaiser can approximate many other windows by varying the beta parameter. ==== ======================= beta Window shape ==== ======================= 0 Rectangular 5 Similar to a Hamming 6 Similar to a Hanning 8.6 Similar to a Blackman ==== ======================= A beta value of 14 is probably a good starting point. Note that as beta gets large, the window narrows, and so the number of samples needs to be large enough to sample the increasingly narrow spike, otherwise NaNs will get returned. Most references to the Kaiser window come from the signal processing literature, where it is used as one of many windowing functions for smoothing values. It is also known as an apodization (which means "removing the foot", i.e. smoothing discontinuities at the beginning and end of the sampled signal) or tapering function. References ---------- .. [1] J. F. Kaiser, "Digital Filters" - Ch 7 in "Systems analysis by digital computer", Editors: F.F. Kuo and J.F. Kaiser, p 218-285. John Wiley and Sons, New York, (1966). .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The University of Alberta Press, 1975, pp. 177-178. .. [3] Wikipedia, "Window function", http://en.wikipedia.org/wiki/Window_function Examples -------- >>> np.kaiser(12, 14) array([ 7.72686684e-06, 3.46009194e-03, 4.65200189e-02, 2.29737120e-01, 5.99885316e-01, 9.45674898e-01, 9.45674898e-01, 5.99885316e-01, 2.29737120e-01, 4.65200189e-02, 3.46009194e-03, 7.72686684e-06]) Plot the window and the frequency response: >>> from numpy.fft import fft, fftshift >>> window = np.kaiser(51, 14) >>> plt.plot(window) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Kaiser window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Amplitude") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 >>> mag = np.abs(fftshift(A)) >>> freq = np.linspace(-0.5, 0.5, len(A)) >>> response = 20 * np.log10(mag) >>> response = np.clip(response, -100, 100) >>> plt.plot(freq, response) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Frequency response of Kaiser window") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Magnitude [dB]") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("Normalized frequency [cycles per sample]") <matplotlib.text.Text object at 0x...> >>> plt.axis('tight') (-0.5, 0.5, -100.0, ...) >>> plt.show() """ from numpy.dual import i0 if M == 1: return np.array([1.]) n = arange(0, M) alpha = (M-1)/2.0 return i0(beta * sqrt(1-((n-alpha)/alpha)**2.0))/i0(float(beta)) def sinc(x): """ Return the sinc function. The sinc function is :math:`\\sin(\\pi x)/(\\pi x)`. Parameters ---------- x : ndarray Array (possibly multi-dimensional) of values for which to to calculate ``sinc(x)``. Returns ------- out : ndarray ``sinc(x)``, which has the same shape as the input. Notes ----- ``sinc(0)`` is the limit value 1. The name sinc is short for "sine cardinal" or "sinus cardinalis". The sinc function is used in various signal processing applications, including in anti-aliasing, in the construction of a Lanczos resampling filter, and in interpolation. For bandlimited interpolation of discrete-time signals, the ideal interpolation kernel is proportional to the sinc function. References ---------- .. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/SincFunction.html .. [2] Wikipedia, "Sinc function", http://en.wikipedia.org/wiki/Sinc_function Examples -------- >>> x = np.linspace(-4, 4, 41) >>> np.sinc(x) array([ -3.89804309e-17, -4.92362781e-02, -8.40918587e-02, -8.90384387e-02, -5.84680802e-02, 3.89804309e-17, 6.68206631e-02, 1.16434881e-01, 1.26137788e-01, 8.50444803e-02, -3.89804309e-17, -1.03943254e-01, -1.89206682e-01, -2.16236208e-01, -1.55914881e-01, 3.89804309e-17, 2.33872321e-01, 5.04551152e-01, 7.56826729e-01, 9.35489284e-01, 1.00000000e+00, 9.35489284e-01, 7.56826729e-01, 5.04551152e-01, 2.33872321e-01, 3.89804309e-17, -1.55914881e-01, -2.16236208e-01, -1.89206682e-01, -1.03943254e-01, -3.89804309e-17, 8.50444803e-02, 1.26137788e-01, 1.16434881e-01, 6.68206631e-02, 3.89804309e-17, -5.84680802e-02, -8.90384387e-02, -8.40918587e-02, -4.92362781e-02, -3.89804309e-17]) >>> plt.plot(x, np.sinc(x)) [<matplotlib.lines.Line2D object at 0x...>] >>> plt.title("Sinc Function") <matplotlib.text.Text object at 0x...> >>> plt.ylabel("Amplitude") <matplotlib.text.Text object at 0x...> >>> plt.xlabel("X") <matplotlib.text.Text object at 0x...> >>> plt.show() It works in 2-D as well: >>> x = np.linspace(-4, 4, 401) >>> xx = np.outer(x, x) >>> plt.imshow(np.sinc(xx)) <matplotlib.image.AxesImage object at 0x...> """ x = np.asanyarray(x) y = pi * where(x == 0, 1.0e-20, x) return sin(y)/y def msort(a): """ Return a copy of an array sorted along the first axis. Parameters ---------- a : array_like Array to be sorted. Returns ------- sorted_array : ndarray Array of the same type and shape as `a`. See Also -------- sort Notes ----- ``np.msort(a)`` is equivalent to ``np.sort(a, axis=0)``. """ b = array(a, subok=True, copy=True) b.sort(0) return b def _ureduce(a, func, **kwargs): """ Internal Function. Call `func` with `a` as first argument swapping the axes to use extended axis on functions that don't support it natively. Returns result and a.shape with axis dims set to 1. Parameters ---------- a : array_like Input array or object that can be converted to an array. func : callable Reduction function Kapable of receiving an axis argument. It is is called with `a` as first argument followed by `kwargs`. kwargs : keyword arguments additional keyword arguments to pass to `func`. Returns ------- result : tuple Result of func(a, **kwargs) and a.shape with axis dims set to 1 which can be used to reshape the result to the same shape a ufunc with keepdims=True would produce. """ a = np.asanyarray(a) axis = kwargs.get('axis', None) if axis is not None: keepdim = list(a.shape) nd = a.ndim try: axis = operator.index(axis) if axis >= nd or axis < -nd: raise IndexError("axis %d out of bounds (%d)" % (axis, a.ndim)) keepdim[axis] = 1 except TypeError: sax = set() for x in axis: if x >= nd or x < -nd: raise IndexError("axis %d out of bounds (%d)" % (x, nd)) if x in sax: raise ValueError("duplicate value in axis") sax.add(x % nd) keepdim[x] = 1 keep = sax.symmetric_difference(frozenset(range(nd))) nkeep = len(keep) # swap axis that should not be reduced to front for i, s in enumerate(sorted(keep)): a = a.swapaxes(i, s) # merge reduced axis a = a.reshape(a.shape[:nkeep] + (-1,)) kwargs['axis'] = -1 else: keepdim = [1] * a.ndim r = func(a, **kwargs) return r, keepdim def median(a, axis=None, out=None, overwrite_input=False, keepdims=False): """ Compute the median along the specified axis. Returns the median of the array elements. Parameters ---------- a : array_like Input array or object that can be converted to an array. axis : int or sequence of int, optional Axis along which the medians are computed. The default (axis=None) is to compute the median along a flattened version of the array. A sequence of axes is supported since version 1.9.0. out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary. overwrite_input : bool, optional If True, then allow use of memory of input array (a) for calculations. The input array will be modified by the call to median. This will save memory when you do not need to preserve the contents of the input array. Treat the input as undefined, but it will probably be fully or partially sorted. Default is False. Note that, if `overwrite_input` is True and the input is not already an ndarray, an error will be raised. keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original `arr`. .. versionadded:: 1.9.0 Returns ------- median : ndarray A new array holding the result (unless `out` is specified, in which case that array is returned instead). If the input contains integers, or floats of smaller precision than 64, then the output data-type is float64. Otherwise, the output data-type is the same as that of the input. See Also -------- mean, percentile Notes ----- Given a vector V of length N, the median of V is the middle value of a sorted copy of V, ``V_sorted`` - i.e., ``V_sorted[(N-1)/2]``, when N is odd. When N is even, it is the average of the two middle values of ``V_sorted``. Examples -------- >>> a = np.array([[10, 7, 4], [3, 2, 1]]) >>> a array([[10, 7, 4], [ 3, 2, 1]]) >>> np.median(a) 3.5 >>> np.median(a, axis=0) array([ 6.5, 4.5, 2.5]) >>> np.median(a, axis=1) array([ 7., 2.]) >>> m = np.median(a, axis=0) >>> out = np.zeros_like(m) >>> np.median(a, axis=0, out=m) array([ 6.5, 4.5, 2.5]) >>> m array([ 6.5, 4.5, 2.5]) >>> b = a.copy() >>> np.median(b, axis=1, overwrite_input=True) array([ 7., 2.]) >>> assert not np.all(a==b) >>> b = a.copy() >>> np.median(b, axis=None, overwrite_input=True) 3.5 >>> assert not np.all(a==b) """ r, k = _ureduce(a, func=_median, axis=axis, out=out, overwrite_input=overwrite_input) if keepdims: return r.reshape(k) else: return r def _median(a, axis=None, out=None, overwrite_input=False): # can't be reasonably be implemented in terms of percentile as we have to # call mean to not break astropy a = np.asanyarray(a) # Set the partition indexes if axis is None: sz = a.size else: sz = a.shape[axis] if sz % 2 == 0: szh = sz // 2 kth = [szh - 1, szh] else: kth = [(sz - 1) // 2] # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): kth.append(-1) if overwrite_input: if axis is None: part = a.ravel() part.partition(kth) else: a.partition(kth, axis=axis) part = a else: part = partition(a, kth, axis=axis) if part.shape == (): # make 0-D arrays work return part.item() if axis is None: axis = 0 indexer = [slice(None)] * part.ndim index = part.shape[axis] // 2 if part.shape[axis] % 2 == 1: # index with slice to allow mean (below) to work indexer[axis] = slice(index, index+1) else: indexer[axis] = slice(index-1, index+1) # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): # warn and return nans like mean would rout = mean(part[indexer], axis=axis, out=out) part = np.rollaxis(part, axis, part.ndim) n = np.isnan(part[..., -1]) if rout.ndim == 0: if n == True: warnings.warn("Invalid value encountered in median", RuntimeWarning) if out is not None: out[...] = a.dtype.type(np.nan) rout = out else: rout = a.dtype.type(np.nan) elif np.count_nonzero(n.ravel()) > 0: warnings.warn("Invalid value encountered in median for" + " %d results" % np.count_nonzero(n.ravel()), RuntimeWarning) rout[n] = np.nan return rout else: # if there are no nans # Use mean in odd and even case to coerce data type # and check, use out array. return mean(part[indexer], axis=axis, out=out) def percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False): """ Compute the qth percentile of the data along the specified axis. Returns the qth percentile of the array elements. Parameters ---------- a : array_like Input array or object that can be converted to an array. q : float in range of [0,100] (or sequence of floats) Percentile to compute which must be between 0 and 100 inclusive. axis : int or sequence of int, optional Axis along which the percentiles are computed. The default (None) is to compute the percentiles along a flattened version of the array. A sequence of axes is supported since version 1.9.0. out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary. overwrite_input : bool, optional If True, then allow use of memory of input array `a` for calculations. The input array will be modified by the call to percentile. This will save memory when you do not need to preserve the contents of the input array. In this case you should not make any assumptions about the content of the passed in array `a` after this function completes -- treat it as undefined. Default is False. Note that, if the `a` input is not already an array this parameter will have no effect, `a` will be converted to an array internally regardless of the value of this parameter. interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points `i` and `j`: * linear: `i + (j - i) * fraction`, where `fraction` is the fractional part of the index surrounded by `i` and `j`. * lower: `i`. * higher: `j`. * nearest: `i` or `j` whichever is nearest. * midpoint: (`i` + `j`) / 2. .. versionadded:: 1.9.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array `a`. .. versionadded:: 1.9.0 Returns ------- percentile : scalar or ndarray If a single percentile `q` is given and axis=None a scalar is returned. If multiple percentiles `q` are given an array holding the result is returned. The results are listed in the first axis. (If `out` is specified, in which case that array is returned instead). If the input contains integers, or floats of smaller precision than 64, then the output data-type is float64. Otherwise, the output data-type is the same as that of the input. See Also -------- mean, median Notes ----- Given a vector V of length N, the q-th percentile of V is the q-th ranked value in a sorted copy of V. The values and distances of the two nearest neighbors as well as the `interpolation` parameter will determine the percentile if the normalized ranking does not match q exactly. This function is the same as the median if ``q=50``, the same as the minimum if ``q=0`` and the same as the maximum if ``q=100``. Examples -------- >>> a = np.array([[10, 7, 4], [3, 2, 1]]) >>> a array([[10, 7, 4], [ 3, 2, 1]]) >>> np.percentile(a, 50) array([ 3.5]) >>> np.percentile(a, 50, axis=0) array([[ 6.5, 4.5, 2.5]]) >>> np.percentile(a, 50, axis=1) array([[ 7.], [ 2.]]) >>> m = np.percentile(a, 50, axis=0) >>> out = np.zeros_like(m) >>> np.percentile(a, 50, axis=0, out=m) array([[ 6.5, 4.5, 2.5]]) >>> m array([[ 6.5, 4.5, 2.5]]) >>> b = a.copy() >>> np.percentile(b, 50, axis=1, overwrite_input=True) array([[ 7.], [ 2.]]) >>> assert not np.all(a==b) >>> b = a.copy() >>> np.percentile(b, 50, axis=None, overwrite_input=True) array([ 3.5]) """ q = array(q, dtype=np.float64, copy=True) r, k = _ureduce(a, func=_percentile, q=q, axis=axis, out=out, overwrite_input=overwrite_input, interpolation=interpolation) if keepdims: if q.ndim == 0: return r.reshape(k) else: return r.reshape([len(q)] + k) else: return r def _percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False): a = asarray(a) if q.ndim == 0: # Do not allow 0-d arrays because following code fails for scalar zerod = True q = q[None] else: zerod = False # avoid expensive reductions, relevant for arrays with < O(1000) elements if q.size < 10: for i in range(q.size): if q[i] < 0. or q[i] > 100.: raise ValueError("Percentiles must be in the range [0,100]") q[i] /= 100. else: # faster than any() if np.count_nonzero(q < 0.) or np.count_nonzero(q > 100.): raise ValueError("Percentiles must be in the range [0,100]") q /= 100. # prepare a for partioning if overwrite_input: if axis is None: ap = a.ravel() else: ap = a else: if axis is None: ap = a.flatten() else: ap = a.copy() if axis is None: axis = 0 Nx = ap.shape[axis] indices = q * (Nx - 1) # round fractional indices according to interpolation method if interpolation == 'lower': indices = floor(indices).astype(intp) elif interpolation == 'higher': indices = ceil(indices).astype(intp) elif interpolation == 'midpoint': indices = floor(indices) + 0.5 elif interpolation == 'nearest': indices = around(indices).astype(intp) elif interpolation == 'linear': pass # keep index as fraction and interpolate else: raise ValueError( "interpolation can only be 'linear', 'lower' 'higher', " "'midpoint', or 'nearest'") n = np.array(False, dtype=bool) # check for nan's flag if indices.dtype == intp: # take the points along axis # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): indices = concatenate((indices, [-1])) ap.partition(indices, axis=axis) # ensure axis with qth is first ap = np.rollaxis(ap, axis, 0) axis = 0 # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): indices = indices[:-1] n = np.isnan(ap[-1:, ...]) if zerod: indices = indices[0] r = take(ap, indices, axis=axis, out=out) else: # weight the points above and below the indices indices_below = floor(indices).astype(intp) indices_above = indices_below + 1 indices_above[indices_above > Nx - 1] = Nx - 1 # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): indices_above = concatenate((indices_above, [-1])) weights_above = indices - indices_below weights_below = 1.0 - weights_above weights_shape = [1, ] * ap.ndim weights_shape[axis] = len(indices) weights_below.shape = weights_shape weights_above.shape = weights_shape ap.partition(concatenate((indices_below, indices_above)), axis=axis) # ensure axis with qth is first ap = np.rollaxis(ap, axis, 0) weights_below = np.rollaxis(weights_below, axis, 0) weights_above = np.rollaxis(weights_above, axis, 0) axis = 0 # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): indices_above = indices_above[:-1] n = np.isnan(ap[-1:, ...]) x1 = take(ap, indices_below, axis=axis) * weights_below x2 = take(ap, indices_above, axis=axis) * weights_above # ensure axis with qth is first x1 = np.rollaxis(x1, axis, 0) x2 = np.rollaxis(x2, axis, 0) if zerod: x1 = x1.squeeze(0) x2 = x2.squeeze(0) if out is not None: r = add(x1, x2, out=out) else: r = add(x1, x2) if np.any(n): warnings.warn("Invalid value encountered in median", RuntimeWarning) if zerod: if ap.ndim == 1: if out is not None: out[...] = a.dtype.type(np.nan) r = out else: r = a.dtype.type(np.nan) else: r[..., n.squeeze(0)] = a.dtype.type(np.nan) else: if r.ndim == 1: r[:] = a.dtype.type(np.nan) else: r[..., n.repeat(q.size, 0)] = a.dtype.type(np.nan) return r def trapz(y, x=None, dx=1.0, axis=-1): """ Integrate along the given axis using the composite trapezoidal rule. Integrate `y` (`x`) along given axis. Parameters ---------- y : array_like Input array to integrate. x : array_like, optional If `x` is None, then spacing between all `y` elements is `dx`. dx : scalar, optional If `x` is None, spacing given by `dx` is assumed. Default is 1. axis : int, optional Specify the axis. Returns ------- trapz : float Definite integral as approximated by trapezoidal rule. See Also -------- sum, cumsum Notes ----- Image [2]_ illustrates trapezoidal rule -- y-axis locations of points will be taken from `y` array, by default x-axis distances between points will be 1.0, alternatively they can be provided with `x` array or with `dx` scalar. Return value will be equal to combined area under the red lines. References ---------- .. [1] Wikipedia page: http://en.wikipedia.org/wiki/Trapezoidal_rule .. [2] Illustration image: http://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png Examples -------- >>> np.trapz([1,2,3]) 4.0 >>> np.trapz([1,2,3], x=[4,6,8]) 8.0 >>> np.trapz([1,2,3], dx=2) 8.0 >>> a = np.arange(6).reshape(2, 3) >>> a array([[0, 1, 2], [3, 4, 5]]) >>> np.trapz(a, axis=0) array([ 1.5, 2.5, 3.5]) >>> np.trapz(a, axis=1) array([ 2., 8.]) """ y = asanyarray(y) if x is None: d = dx else: x = asanyarray(x) if x.ndim == 1: d = diff(x) # reshape to correct shape shape = [1]*y.ndim shape[axis] = d.shape[0] d = d.reshape(shape) else: d = diff(x, axis=axis) nd = len(y.shape) slice1 = [slice(None)]*nd slice2 = [slice(None)]*nd slice1[axis] = slice(1, None) slice2[axis] = slice(None, -1) try: ret = (d * (y[slice1] + y[slice2]) / 2.0).sum(axis) except ValueError: # Operations didn't work, cast to ndarray d = np.asarray(d) y = np.asarray(y) ret = add.reduce(d * (y[slice1]+y[slice2])/2.0, axis) return ret #always succeed def add_newdoc(place, obj, doc): """Adds documentation to obj which is in module place. If doc is a string add it to obj as a docstring If doc is a tuple, then the first element is interpreted as an attribute of obj and the second as the docstring (method, docstring) If doc is a list, then each element of the list should be a sequence of length two --> [(method1, docstring1), (method2, docstring2), ...] This routine never raises an error. This routine cannot modify read-only docstrings, as appear in new-style classes or built-in functions. Because this routine never raises an error the caller must check manually that the docstrings were changed. """ try: new = getattr(__import__(place, globals(), {}, [obj]), obj) if isinstance(doc, str): add_docstring(new, doc.strip()) elif isinstance(doc, tuple): add_docstring(getattr(new, doc[0]), doc[1].strip()) elif isinstance(doc, list): for val in doc: add_docstring(getattr(new, val[0]), val[1].strip()) except: pass # Based on scitools meshgrid def meshgrid(*xi, **kwargs): """ Return coordinate matrices from coordinate vectors. Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,..., xn. .. versionchanged:: 1.9 1-D and 0-D cases are allowed. Parameters ---------- x1, x2,..., xn : array_like 1-D arrays representing the coordinates of a grid. indexing : {'xy', 'ij'}, optional Cartesian ('xy', default) or matrix ('ij') indexing of output. See Notes for more details. .. versionadded:: 1.7.0 sparse : bool, optional If True a sparse grid is returned in order to conserve memory. Default is False. .. versionadded:: 1.7.0 copy : bool, optional If False, a view into the original arrays are returned in order to conserve memory. Default is True. Please note that ``sparse=False, copy=False`` will likely return non-contiguous arrays. Furthermore, more than one element of a broadcast array may refer to a single memory location. If you need to write to the arrays, make copies first. .. versionadded:: 1.7.0 Returns ------- X1, X2,..., XN : ndarray For vectors `x1`, `x2`,..., 'xn' with lengths ``Ni=len(xi)`` , return ``(N1, N2, N3,...Nn)`` shaped arrays if indexing='ij' or ``(N2, N1, N3,...Nn)`` shaped arrays if indexing='xy' with the elements of `xi` repeated to fill the matrix along the first dimension for `x1`, the second for `x2` and so on. Notes ----- This function supports both indexing conventions through the indexing keyword argument. Giving the string 'ij' returns a meshgrid with matrix indexing, while 'xy' returns a meshgrid with Cartesian indexing. In the 2-D case with inputs of length M and N, the outputs are of shape (N, M) for 'xy' indexing and (M, N) for 'ij' indexing. In the 3-D case with inputs of length M, N and P, outputs are of shape (N, M, P) for 'xy' indexing and (M, N, P) for 'ij' indexing. The difference is illustrated by the following code snippet:: xv, yv = meshgrid(x, y, sparse=False, indexing='ij') for i in range(nx): for j in range(ny): # treat xv[i,j], yv[i,j] xv, yv = meshgrid(x, y, sparse=False, indexing='xy') for i in range(nx): for j in range(ny): # treat xv[j,i], yv[j,i] In the 1-D and 0-D case, the indexing and sparse keywords have no effect. See Also -------- index_tricks.mgrid : Construct a multi-dimensional "meshgrid" using indexing notation. index_tricks.ogrid : Construct an open multi-dimensional "meshgrid" using indexing notation. Examples -------- >>> nx, ny = (3, 2) >>> x = np.linspace(0, 1, nx) >>> y = np.linspace(0, 1, ny) >>> xv, yv = meshgrid(x, y) >>> xv array([[ 0. , 0.5, 1. ], [ 0. , 0.5, 1. ]]) >>> yv array([[ 0., 0., 0.], [ 1., 1., 1.]]) >>> xv, yv = meshgrid(x, y, sparse=True) # make sparse output arrays >>> xv array([[ 0. , 0.5, 1. ]]) >>> yv array([[ 0.], [ 1.]]) `meshgrid` is very useful to evaluate functions on a grid. >>> x = np.arange(-5, 5, 0.1) >>> y = np.arange(-5, 5, 0.1) >>> xx, yy = meshgrid(x, y, sparse=True) >>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) >>> h = plt.contourf(x,y,z) """ ndim = len(xi) copy_ = kwargs.pop('copy', True) sparse = kwargs.pop('sparse', False) indexing = kwargs.pop('indexing', 'xy') if kwargs: raise TypeError("meshgrid() got an unexpected keyword argument '%s'" % (list(kwargs)[0],)) if indexing not in ['xy', 'ij']: raise ValueError( "Valid values for `indexing` are 'xy' and 'ij'.") s0 = (1,) * ndim output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1::]) for i, x in enumerate(xi)] shape = [x.size for x in output] if indexing == 'xy' and ndim > 1: # switch first and second axis output[0].shape = (1, -1) + (1,)*(ndim - 2) output[1].shape = (-1, 1) + (1,)*(ndim - 2) shape[0], shape[1] = shape[1], shape[0] if sparse: if copy_: return [x.copy() for x in output] else: return output else: # Return the full N-D matrix (not only the 1-D vector) if copy_: mult_fact = np.ones(shape, dtype=int) return [x * mult_fact for x in output] else: return np.broadcast_arrays(*output) def delete(arr, obj, axis=None): """ Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by `arr[obj]`. Parameters ---------- arr : array_like Input array. obj : slice, int or array of ints Indicate which sub-arrays to remove. axis : int, optional The axis along which to delete the subarray defined by `obj`. If `axis` is None, `obj` is applied to the flattened array. Returns ------- out : ndarray A copy of `arr` with the elements specified by `obj` removed. Note that `delete` does not occur in-place. If `axis` is None, `out` is a flattened array. See Also -------- insert : Insert elements into an array. append : Append elements at the end of an array. Notes ----- Often it is preferable to use a boolean mask. For example: >>> mask = np.ones(len(arr), dtype=bool) >>> mask[[0,2,4]] = False >>> result = arr[mask,...] Is equivalent to `np.delete(arr, [0,2,4], axis=0)`, but allows further use of `mask`. Examples -------- >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> arr array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) >>> np.delete(arr, 1, 0) array([[ 1, 2, 3, 4], [ 9, 10, 11, 12]]) >>> np.delete(arr, np.s_[::2], 1) array([[ 2, 4], [ 6, 8], [10, 12]]) >>> np.delete(arr, [1,3,5], None) array([ 1, 3, 5, 7, 8, 9, 10, 11, 12]) """ wrap = None if type(arr) is not ndarray: try: wrap = arr.__array_wrap__ except AttributeError: pass arr = asarray(arr) ndim = arr.ndim if axis is None: if ndim != 1: arr = arr.ravel() ndim = arr.ndim axis = ndim - 1 if ndim == 0: # 2013-09-24, 1.9 warnings.warn( "in the future the special handling of scalars will be removed " "from delete and raise an error", DeprecationWarning) if wrap: return wrap(arr) else: return arr.copy() slobj = [slice(None)]*ndim N = arr.shape[axis] newshape = list(arr.shape) if isinstance(obj, slice): start, stop, step = obj.indices(N) xr = range(start, stop, step) numtodel = len(xr) if numtodel <= 0: if wrap: return wrap(arr.copy()) else: return arr.copy() # Invert if step is negative: if step < 0: step = -step start = xr[-1] stop = xr[0] + 1 newshape[axis] -= numtodel new = empty(newshape, arr.dtype, arr.flags.fnc) # copy initial chunk if start == 0: pass else: slobj[axis] = slice(None, start) new[slobj] = arr[slobj] # copy end chunck if stop == N: pass else: slobj[axis] = slice(stop-numtodel, None) slobj2 = [slice(None)]*ndim slobj2[axis] = slice(stop, None) new[slobj] = arr[slobj2] # copy middle pieces if step == 1: pass else: # use array indexing. keep = ones(stop-start, dtype=bool) keep[:stop-start:step] = False slobj[axis] = slice(start, stop-numtodel) slobj2 = [slice(None)]*ndim slobj2[axis] = slice(start, stop) arr = arr[slobj2] slobj2[axis] = keep new[slobj] = arr[slobj2] if wrap: return wrap(new) else: return new _obj = obj obj = np.asarray(obj) # After removing the special handling of booleans and out of # bounds values, the conversion to the array can be removed. if obj.dtype == bool: warnings.warn( "in the future insert will treat boolean arrays and array-likes " "as boolean index instead of casting it to integer", FutureWarning) obj = obj.astype(intp) if isinstance(_obj, (int, long, integer)): # optimization for a single value obj = obj.item() if (obj < -N or obj >= N): raise IndexError( "index %i is out of bounds for axis %i with " "size %i" % (obj, axis, N)) if (obj < 0): obj += N newshape[axis] -= 1 new = empty(newshape, arr.dtype, arr.flags.fnc) slobj[axis] = slice(None, obj) new[slobj] = arr[slobj] slobj[axis] = slice(obj, None) slobj2 = [slice(None)]*ndim slobj2[axis] = slice(obj+1, None) new[slobj] = arr[slobj2] else: if obj.size == 0 and not isinstance(_obj, np.ndarray): obj = obj.astype(intp) if not np.can_cast(obj, intp, 'same_kind'): # obj.size = 1 special case always failed and would just # give superfluous warnings. # 2013-09-24, 1.9 warnings.warn( "using a non-integer array as obj in delete will result in an " "error in the future", DeprecationWarning) obj = obj.astype(intp) keep = ones(N, dtype=bool) # Test if there are out of bound indices, this is deprecated inside_bounds = (obj < N) & (obj >= -N) if not inside_bounds.all(): # 2013-09-24, 1.9 warnings.warn( "in the future out of bounds indices will raise an error " "instead of being ignored by `numpy.delete`.", DeprecationWarning) obj = obj[inside_bounds] positive_indices = obj >= 0 if not positive_indices.all(): warnings.warn( "in the future negative indices will not be ignored by " "`numpy.delete`.", FutureWarning) obj = obj[positive_indices] keep[obj, ] = False slobj[axis] = keep new = arr[slobj] if wrap: return wrap(new) else: return new def insert(arr, obj, values, axis=None): """ Insert values along the given axis before the given indices. Parameters ---------- arr : array_like Input array. obj : int, slice or sequence of ints Object that defines the index or indices before which `values` is inserted. .. versionadded:: 1.8.0 Support for multiple insertions when `obj` is a single scalar or a sequence with one element (similar to calling insert multiple times). values : array_like Values to insert into `arr`. If the type of `values` is different from that of `arr`, `values` is converted to the type of `arr`. `values` should be shaped so that ``arr[...,obj,...] = values`` is legal. axis : int, optional Axis along which to insert `values`. If `axis` is None then `arr` is flattened first. Returns ------- out : ndarray A copy of `arr` with `values` inserted. Note that `insert` does not occur in-place: a new array is returned. If `axis` is None, `out` is a flattened array. See Also -------- append : Append elements at the end of an array. concatenate : Join a sequence of arrays along an existing axis. delete : Delete elements from an array. Notes ----- Note that for higher dimensional inserts `obj=0` behaves very different from `obj=[0]` just like `arr[:,0,:] = values` is different from `arr[:,[0],:] = values`. Examples -------- >>> a = np.array([[1, 1], [2, 2], [3, 3]]) >>> a array([[1, 1], [2, 2], [3, 3]]) >>> np.insert(a, 1, 5) array([1, 5, 1, 2, 2, 3, 3]) >>> np.insert(a, 1, 5, axis=1) array([[1, 5, 1], [2, 5, 2], [3, 5, 3]]) Difference between sequence and scalars: >>> np.insert(a, [1], [[1],[2],[3]], axis=1) array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) >>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1), ... np.insert(a, [1], [[1],[2],[3]], axis=1)) True >>> b = a.flatten() >>> b array([1, 1, 2, 2, 3, 3]) >>> np.insert(b, [2, 2], [5, 6]) array([1, 1, 5, 6, 2, 2, 3, 3]) >>> np.insert(b, slice(2, 4), [5, 6]) array([1, 1, 5, 2, 6, 2, 3, 3]) >>> np.insert(b, [2, 2], [7.13, False]) # type casting array([1, 1, 7, 0, 2, 2, 3, 3]) >>> x = np.arange(8).reshape(2, 4) >>> idx = (1, 3) >>> np.insert(x, idx, 999, axis=1) array([[ 0, 999, 1, 2, 999, 3], [ 4, 999, 5, 6, 999, 7]]) """ wrap = None if type(arr) is not ndarray: try: wrap = arr.__array_wrap__ except AttributeError: pass arr = asarray(arr) ndim = arr.ndim if axis is None: if ndim != 1: arr = arr.ravel() ndim = arr.ndim axis = ndim - 1 else: if ndim > 0 and (axis < -ndim or axis >= ndim): raise IndexError( "axis %i is out of bounds for an array of " "dimension %i" % (axis, ndim)) if (axis < 0): axis += ndim if (ndim == 0): # 2013-09-24, 1.9 warnings.warn( "in the future the special handling of scalars will be removed " "from insert and raise an error", DeprecationWarning) arr = arr.copy() arr[...] = values if wrap: return wrap(arr) else: return arr slobj = [slice(None)]*ndim N = arr.shape[axis] newshape = list(arr.shape) if isinstance(obj, slice): # turn it into a range object indices = arange(*obj.indices(N), **{'dtype': intp}) else: # need to copy obj, because indices will be changed in-place indices = np.array(obj) if indices.dtype == bool: # See also delete warnings.warn( "in the future insert will treat boolean arrays and " "array-likes as a boolean index instead of casting it to " "integer", FutureWarning) indices = indices.astype(intp) # Code after warning period: #if obj.ndim != 1: # raise ValueError('boolean array argument obj to insert ' # 'must be one dimensional') #indices = np.flatnonzero(obj) elif indices.ndim > 1: raise ValueError( "index array argument obj to insert must be one dimensional " "or scalar") if indices.size == 1: index = indices.item() if index < -N or index > N: raise IndexError( "index %i is out of bounds for axis %i with " "size %i" % (obj, axis, N)) if (index < 0): index += N # There are some object array corner cases here, but we cannot avoid # that: values = array(values, copy=False, ndmin=arr.ndim, dtype=arr.dtype) if indices.ndim == 0: # broadcasting is very different here, since a[:,0,:] = ... behaves # very different from a[:,[0],:] = ...! This changes values so that # it works likes the second case. (here a[:,0:1,:]) values = np.rollaxis(values, 0, (axis % values.ndim) + 1) numnew = values.shape[axis] newshape[axis] += numnew new = empty(newshape, arr.dtype, arr.flags.fnc) slobj[axis] = slice(None, index) new[slobj] = arr[slobj] slobj[axis] = slice(index, index+numnew) new[slobj] = values slobj[axis] = slice(index+numnew, None) slobj2 = [slice(None)] * ndim slobj2[axis] = slice(index, None) new[slobj] = arr[slobj2] if wrap: return wrap(new) return new elif indices.size == 0 and not isinstance(obj, np.ndarray): # Can safely cast the empty list to intp indices = indices.astype(intp) if not np.can_cast(indices, intp, 'same_kind'): # 2013-09-24, 1.9 warnings.warn( "using a non-integer array as obj in insert will result in an " "error in the future", DeprecationWarning) indices = indices.astype(intp) indices[indices < 0] += N numnew = len(indices) order = indices.argsort(kind='mergesort') # stable sort indices[order] += np.arange(numnew) newshape[axis] += numnew old_mask = ones(newshape[axis], dtype=bool) old_mask[indices] = False new = empty(newshape, arr.dtype, arr.flags.fnc) slobj2 = [slice(None)]*ndim slobj[axis] = indices slobj2[axis] = old_mask new[slobj] = values new[slobj2] = arr if wrap: return wrap(new) return new def append(arr, values, axis=None): """ Append values to the end of an array. Parameters ---------- arr : array_like Values are appended to a copy of this array. values : array_like These values are appended to a copy of `arr`. It must be of the correct shape (the same shape as `arr`, excluding `axis`). If `axis` is not specified, `values` can be any shape and will be flattened before use. axis : int, optional The axis along which `values` are appended. If `axis` is not given, both `arr` and `values` are flattened before use. Returns ------- append : ndarray A copy of `arr` with `values` appended to `axis`. Note that `append` does not occur in-place: a new array is allocated and filled. If `axis` is None, `out` is a flattened array. See Also -------- insert : Insert elements into an array. delete : Delete elements from an array. Examples -------- >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) array([1, 2, 3, 4, 5, 6, 7, 8, 9]) When `axis` is specified, `values` must have the correct shape. >>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0) Traceback (most recent call last): ... ValueError: arrays must have same number of dimensions """ arr = asanyarray(arr) if axis is None: if arr.ndim != 1: arr = arr.ravel() values = ravel(values) axis = arr.ndim-1 return concatenate((arr, values), axis=axis)
gpl-2.0
Agent007/deep-learning
image-classification/helper.py
155
5631
import pickle import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import LabelBinarizer def _load_label_names(): """ Load the label names from file """ return ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] def load_cfar10_batch(cifar10_dataset_folder_path, batch_id): """ Load a batch of the dataset """ with open(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id), mode='rb') as file: batch = pickle.load(file, encoding='latin1') features = batch['data'].reshape((len(batch['data']), 3, 32, 32)).transpose(0, 2, 3, 1) labels = batch['labels'] return features, labels def display_stats(cifar10_dataset_folder_path, batch_id, sample_id): """ Display Stats of the the dataset """ batch_ids = list(range(1, 6)) if batch_id not in batch_ids: print('Batch Id out of Range. Possible Batch Ids: {}'.format(batch_ids)) return None features, labels = load_cfar10_batch(cifar10_dataset_folder_path, batch_id) if not (0 <= sample_id < len(features)): print('{} samples in batch {}. {} is out of range.'.format(len(features), batch_id, sample_id)) return None print('\nStats of batch {}:'.format(batch_id)) print('Samples: {}'.format(len(features))) print('Label Counts: {}'.format(dict(zip(*np.unique(labels, return_counts=True))))) print('First 20 Labels: {}'.format(labels[:20])) sample_image = features[sample_id] sample_label = labels[sample_id] label_names = _load_label_names() print('\nExample of Image {}:'.format(sample_id)) print('Image - Min Value: {} Max Value: {}'.format(sample_image.min(), sample_image.max())) print('Image - Shape: {}'.format(sample_image.shape)) print('Label - Label Id: {} Name: {}'.format(sample_label, label_names[sample_label])) plt.axis('off') plt.imshow(sample_image) def _preprocess_and_save(normalize, one_hot_encode, features, labels, filename): """ Preprocess data and save it to file """ features = normalize(features) labels = one_hot_encode(labels) pickle.dump((features, labels), open(filename, 'wb')) def preprocess_and_save_data(cifar10_dataset_folder_path, normalize, one_hot_encode): """ Preprocess Training and Validation Data """ n_batches = 5 valid_features = [] valid_labels = [] for batch_i in range(1, n_batches + 1): features, labels = load_cfar10_batch(cifar10_dataset_folder_path, batch_i) validation_count = int(len(features) * 0.1) # Prprocess and save a batch of training data _preprocess_and_save( normalize, one_hot_encode, features[:-validation_count], labels[:-validation_count], 'preprocess_batch_' + str(batch_i) + '.p') # Use a portion of training batch for validation valid_features.extend(features[-validation_count:]) valid_labels.extend(labels[-validation_count:]) # Preprocess and Save all validation data _preprocess_and_save( normalize, one_hot_encode, np.array(valid_features), np.array(valid_labels), 'preprocess_validation.p') with open(cifar10_dataset_folder_path + '/test_batch', mode='rb') as file: batch = pickle.load(file, encoding='latin1') # load the test data test_features = batch['data'].reshape((len(batch['data']), 3, 32, 32)).transpose(0, 2, 3, 1) test_labels = batch['labels'] # Preprocess and Save all test data _preprocess_and_save( normalize, one_hot_encode, np.array(test_features), np.array(test_labels), 'preprocess_test.p') def batch_features_labels(features, labels, batch_size): """ Split features and labels into batches """ for start in range(0, len(features), batch_size): end = min(start + batch_size, len(features)) yield features[start:end], labels[start:end] def load_preprocess_training_batch(batch_id, batch_size): """ Load the Preprocessed Training data and return them in batches of <batch_size> or less """ filename = 'preprocess_batch_' + str(batch_id) + '.p' features, labels = pickle.load(open(filename, mode='rb')) # Return the training data in batches of size <batch_size> or less return batch_features_labels(features, labels, batch_size) def display_image_predictions(features, labels, predictions): n_classes = 10 label_names = _load_label_names() label_binarizer = LabelBinarizer() label_binarizer.fit(range(n_classes)) label_ids = label_binarizer.inverse_transform(np.array(labels)) fig, axies = plt.subplots(nrows=4, ncols=2) fig.tight_layout() fig.suptitle('Softmax Predictions', fontsize=20, y=1.1) n_predictions = 3 margin = 0.05 ind = np.arange(n_predictions) width = (1. - 2. * margin) / n_predictions for image_i, (feature, label_id, pred_indicies, pred_values) in enumerate(zip(features, label_ids, predictions.indices, predictions.values)): pred_names = [label_names[pred_i] for pred_i in pred_indicies] correct_name = label_names[label_id] axies[image_i][0].imshow(feature) axies[image_i][0].set_title(correct_name) axies[image_i][0].set_axis_off() axies[image_i][1].barh(ind + margin, pred_values[::-1], width) axies[image_i][1].set_yticks(ind + margin) axies[image_i][1].set_yticklabels(pred_names[::-1]) axies[image_i][1].set_xticks([0, 0.5, 1.0])
mit
JanNash/sms-tools
lectures/06-Harmonic-model/plots-code/spectral-peaks.py
22
1161
import numpy as np import matplotlib.pyplot as plt from scipy.signal import hamming, triang, blackmanharris import math import sys, os, functools, time sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/models/')) import dftModel as DFT import utilFunctions as UF (fs, x) = UF.wavread('../../../sounds/oboe-A4.wav') N = 512*2 M = 511 t = -60 w = np.hamming(M) start = .8*fs hN = N/2 hM = (M+1)/2 x1 = x[start:start+M] mX, pX = DFT.dftAnal(x1, w, N) ploc = UF.peakDetection(mX, t) iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) pmag = mX[ploc] freqaxis = fs*np.arange(mX.size)/float(N) plt.figure(1, figsize=(9, 6)) plt.subplot (2,1,1) plt.plot(freqaxis, mX,'r', lw=1.5) plt.axis([0,7000,-80,max(mX)+1]) plt.plot(fs * iploc / N, ipmag, marker='x', color='b', linestyle='', markeredgewidth=1.5) plt.title('mX + peaks (oboe-A4.wav)') plt.subplot (2,1,2) plt.plot(freqaxis, pX,'c', lw=1.5) plt.axis([0,7000, min(pX),10]) plt.plot(fs * iploc/N, ipphase, marker='x', color='b', linestyle='', markeredgewidth=1.5) plt.title('pX + peaks') plt.tight_layout() plt.savefig('spectral-peaks.png') plt.show()
agpl-3.0
rosswhitfield/mantid
qt/python/mantidqt/widgets/sliceviewer/test/test_sliceviewer_presenter.py
3
24997
# Mantid Repository : https://github.com/mantidproject/mantid # # Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source, # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantid workbench. # # import sys import unittest from unittest import mock from unittest.mock import patch from mantid.api import MultipleExperimentInfos import matplotlib matplotlib.use('Agg') # Mock out simpleapi to import expensive import of something we don't use anyway sys.modules['mantid.simpleapi'] = mock.MagicMock() from mantidqt.widgets.sliceviewer.model import SliceViewerModel, WS_TYPE # noqa: E402 from mantidqt.widgets.sliceviewer.presenter import ( # noqa: E402 PeaksViewerCollectionPresenter, SliceViewer) from mantidqt.widgets.sliceviewer.transform import NonOrthogonalTransform # noqa: E402 from mantidqt.widgets.sliceviewer.toolbar import ToolItemText # noqa: E402 from mantidqt.widgets.sliceviewer.view import SliceViewerView, SliceViewerDataView # noqa: E402 def _create_presenter(model, view, mock_sliceinfo_cls, enable_nonortho_axes, supports_nonortho): model.get_ws_type = mock.Mock(return_value=WS_TYPE.MDH) model.is_ragged_matrix_plotted.return_value = False model.get_dim_limits.return_value = ((-1, 1), (-2, 2)) data_view_mock = view.data_view data_view_mock.plot_MDH = mock.Mock() presenter = SliceViewer(None, model=model, view=view) if enable_nonortho_axes: data_view_mock.nonorthogonal_mode = True data_view_mock.nonortho_transform = mock.MagicMock(NonOrthogonalTransform) data_view_mock.nonortho_transform.tr.return_value = (0, 1) presenter.nonorthogonal_axes(True) else: data_view_mock.nonorthogonal_mode = False data_view_mock.nonortho_transform = None data_view_mock.disable_tool_button.reset_mock() data_view_mock.create_axes_orthogonal.reset_mock() data_view_mock.create_axes_nonorthogonal.reset_mock() mock_sliceinfo_instance = mock_sliceinfo_cls.return_value mock_sliceinfo_instance.can_support_nonorthogonal_axes.return_value = supports_nonortho return presenter, data_view_mock def create_workspace_mock(): # Mock out workspace methods needed for SliceViewerModel.__init__ workspace = mock.Mock(spec=MultipleExperimentInfos) workspace.isMDHistoWorkspace = lambda: False workspace.getNumDims = lambda: 2 workspace.name = lambda: "workspace" return workspace class SliceViewerTest(unittest.TestCase): def setUp(self): self.view = mock.Mock(spec=SliceViewerView) data_view = mock.Mock(spec=SliceViewerDataView) data_view.plot_MDH = mock.Mock() data_view.dimensions = mock.Mock() data_view.norm_opts = mock.Mock() data_view.image_info_widget = mock.Mock() data_view.canvas = mock.Mock() data_view.nonorthogonal_mode = False data_view.nonortho_transform = None data_view.get_axes_limits.return_value = None dimensions = mock.Mock() dimensions.get_slicepoint.return_value = [None, None, 0.5] dimensions.transpose = False dimensions.get_slicerange.return_value = [None, None, (-15, 15)] dimensions.qflags = [True, True, True] data_view.dimensions = dimensions self.view.data_view = data_view self.model = mock.Mock(spec=SliceViewerModel) self.model.get_ws = mock.Mock() self.model.get_data = mock.Mock() self.model.rebin = mock.Mock() self.model.workspace_equals = mock.Mock() self.model.get_properties.return_value = { "workspace_type": "WS_TYPE.MATRIX", "supports_normalise": True, "supports_nonorthogonal_axes": False, "supports_dynamic_rebinning": False, "supports_peaks_overlays": True } @patch("sip.isdeleted", return_value=False) def test_sliceviewer_MDH(self, _): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MDH) presenter = SliceViewer(None, model=self.model, view=self.view) # setup calls self.assertEqual(self.model.get_dimensions_info.call_count, 0) self.assertEqual(self.model.get_ws.call_count, 1) self.assertEqual(self.model.get_properties.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 1) self.assertEqual(self.view.data_view.plot_MDH.call_count, 1) # new_plot self.model.reset_mock() self.view.reset_mock() presenter.new_plot() self.assertEqual(self.model.get_ws.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 1) self.assertEqual(self.view.data_view.plot_MDH.call_count, 1) # update_plot_data self.model.reset_mock() self.view.reset_mock() presenter.update_plot_data() self.assertEqual(self.model.get_data.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 1) self.assertEqual(self.view.data_view.update_plot_data.call_count, 1) @patch("sip.isdeleted", return_value=False) def test_sliceviewer_MDE(self, _): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MDE) presenter = SliceViewer(None, model=self.model, view=self.view) # setup calls self.assertEqual(self.model.get_dimensions_info.call_count, 0) self.assertEqual(self.model.get_ws_MDE.call_count, 1) self.assertEqual(self.model.get_properties.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_bin_params.call_count, 1) self.assertEqual(self.view.data_view.plot_MDH.call_count, 1) # new_plot self.model.reset_mock() self.view.reset_mock() presenter.new_plot() self.assertEqual(self.model.get_ws_MDE.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_bin_params.call_count, 1) self.assertEqual(self.view.data_view.plot_MDH.call_count, 1) # update_plot_data self.model.reset_mock() self.view.reset_mock() presenter.update_plot_data() self.assertEqual(self.model.get_data.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_bin_params.call_count, 1) self.assertEqual(self.view.data_view.update_plot_data.call_count, 1) @patch("sip.isdeleted", return_value=False) def test_sliceviewer_matrix(self, _): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MATRIX) presenter = SliceViewer(None, model=self.model, view=self.view) # setup calls self.assertEqual(self.model.get_dimensions_info.call_count, 0) self.assertEqual(self.model.get_ws.call_count, 1) self.assertEqual(self.model.get_properties.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 0) self.assertEqual(self.view.data_view.plot_matrix.call_count, 1) # new_plot self.model.reset_mock() self.view.reset_mock() presenter.new_plot() self.assertEqual(self.model.get_ws.call_count, 1) self.assertEqual(self.view.data_view.dimensions.get_slicepoint.call_count, 0) self.assertEqual(self.view.data_view.plot_matrix.call_count, 1) @patch("sip.isdeleted", return_value=False) def test_normalization_change_set_correct_normalization(self, _): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MATRIX) self.view.data_view.plot_matrix = mock.Mock() presenter = SliceViewer(None, model=self.model, view=self.view) presenter.normalization_changed("By bin width") self.view.data_view.plot_matrix.assert_called_with(self.model.get_ws(), distribution=False) def peaks_button_disabled_if_model_cannot_support_it(self): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MATRIX) self.model.can_support_peaks_overlay.return_value = False SliceViewer(None, model=self.model, view=self.view) self.view.data_view.disable_tool_button.assert_called_once_with(ToolItemText.OVERLAY_PEAKS) def peaks_button_not_disabled_if_model_can_support_it(self): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MATRIX) self.model.can_support_peaks_overlay.return_value = True SliceViewer(None, model=self.model, view=self.view) self.view.data_view.disable_tool_button.assert_not_called() @patch("sip.isdeleted", return_value=False) def test_non_orthogonal_axes_toggled_on(self, _): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MDE) self.model.get_dim_limits.return_value = ((-1, 1), (-2, 2)) self.model.is_ragged_matrix_plotted.return_value = False data_view_mock = self.view.data_view data_view_mock.plot_MDH = mock.Mock() presenter = SliceViewer(None, model=self.model, view=self.view) data_view_mock.plot_MDH.reset_mock() # clear initial plot call data_view_mock.create_axes_orthogonal.reset_mock() presenter.nonorthogonal_axes(True) data_view_mock.deactivate_and_disable_tool.assert_called_once_with( ToolItemText.REGIONSELECTION) data_view_mock.create_axes_nonorthogonal.assert_called_once() data_view_mock.create_axes_orthogonal.assert_not_called() self.assertEqual(data_view_mock.plot_MDH.call_count, 2) data_view_mock.disable_tool_button.assert_has_calls([mock.call(ToolItemText.LINEPLOTS)]) @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.presenter.SliceInfo") def test_non_orthogonal_axes_toggled_off(self, mock_sliceinfo_cls, _): self.model.get_ws_type = mock.Mock(return_value=WS_TYPE.MDE) presenter, data_view_mock = _create_presenter(self.model, self.view, mock_sliceinfo_cls, enable_nonortho_axes=True, supports_nonortho=True) data_view_mock.plot_MDH.reset_mock() # clear initial plot call data_view_mock.create_axes_orthogonal.reset_mock() data_view_mock.create_axes_nonorthogonal.reset_mock() data_view_mock.enable_tool_button.reset_mock() data_view_mock.disable_tool_button.reset_mock() data_view_mock.remove_line_plots.reset_mock() presenter.nonorthogonal_axes(False) data_view_mock.create_axes_orthogonal.assert_called_once() data_view_mock.create_axes_nonorthogonal.assert_not_called() data_view_mock.plot_MDH.assert_called_once() data_view_mock.enable_tool_button.assert_has_calls( (mock.call(ToolItemText.LINEPLOTS), mock.call(ToolItemText.REGIONSELECTION))) @patch("sip.isdeleted", return_value=False) def test_request_to_show_all_data_sets_correct_limits_on_view_MD(self, _): presenter = SliceViewer(None, model=self.model, view=self.view) self.model.is_ragged_matrix_plotted.return_value = False self.model.get_dim_limits.return_value = ((-1, 1), (-2, 2)) presenter.show_all_data_requested() data_view = self.view.data_view self.model.get_dim_limits.assert_called_once_with([None, None, 0.5], data_view.dimensions.transpose) data_view.get_full_extent.assert_not_called() data_view.set_axes_limits.assert_called_once_with((-1, 1), (-2, 2)) @patch("sip.isdeleted", return_value=False) def test_request_to_show_all_data_sets_correct_limits_on_view_ragged_matrix(self, _): presenter = SliceViewer(None, model=self.model, view=self.view) self.model.is_ragged_matrix_plotted.return_value = True self.view.data_view.get_full_extent.return_value = [-1, 1, -2, 2] presenter.show_all_data_requested() data_view = self.view.data_view self.model.get_dim_limits.assert_not_called() data_view.set_axes_limits.assert_called_once_with((-1, 1), (-2, 2)) @patch("sip.isdeleted", return_value=False) def test_data_limits_changed_creates_new_plot_if_dynamic_rebinning_supported(self, _): presenter = SliceViewer(None, model=self.model, view=self.view) self.model.can_support_dynamic_rebinning.return_value = True new_plot_mock = mock.MagicMock() presenter.new_plot = new_plot_mock presenter.data_limits_changed() new_plot_mock.assert_called_once() @patch("sip.isdeleted", return_value=False) def test_data_limits_changed_does_not_create_new_plot_if_dynamic_rebinning_not_supported(self, _): presenter = SliceViewer(None, model=self.model, view=self.view) self.model.can_support_dynamic_rebinning.return_value = False new_plot_mock = mock.MagicMock() presenter.new_plot = new_plot_mock presenter.data_limits_changed() new_plot_mock.assert_not_called() @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.presenter.SliceInfo") def test_changing_dimensions_in_nonortho_mode_switches_to_ortho_when_dim_not_Q( self, mock_sliceinfo_cls, is_view_delete): presenter, data_view_mock = _create_presenter(self.model, self.view, mock_sliceinfo_cls, enable_nonortho_axes=True, supports_nonortho=False) presenter.dimensions_changed() data_view_mock.disable_tool_button.assert_called_once_with(ToolItemText.NONORTHOGONAL_AXES) data_view_mock.create_axes_orthogonal.assert_called_once() data_view_mock.create_axes_nonorthogonal.assert_not_called() @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.presenter.SliceInfo") def test_changing_dimensions_in_nonortho_mode_keeps_nonortho_when_dim_is_Q( self, mock_sliceinfo_cls, _): presenter, data_view_mock = _create_presenter(self.model, self.view, mock_sliceinfo_cls, enable_nonortho_axes=True, supports_nonortho=True) presenter.dimensions_changed() data_view_mock.create_axes_nonorthogonal.assert_called_once() data_view_mock.disable_tool_button.assert_not_called() data_view_mock.create_axes_orthogonal.assert_not_called() @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.presenter.SliceInfo") def test_changing_dimensions_in_ortho_mode_disables_nonortho_btn_if_not_supported( self, mock_sliceinfo_cls, _): presenter, data_view_mock = _create_presenter(self.model, self.view, mock_sliceinfo_cls, enable_nonortho_axes=False, supports_nonortho=False) presenter.dimensions_changed() data_view_mock.disable_tool_button.assert_called_once_with(ToolItemText.NONORTHOGONAL_AXES) @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.presenter.SliceInfo") def test_changing_dimensions_in_ortho_mode_enables_nonortho_btn_if_supported( self, mock_sliceinfo_cls, _): presenter, data_view_mock = _create_presenter(self.model, self.view, mock_sliceinfo_cls, enable_nonortho_axes=False, supports_nonortho=True) presenter.dimensions_changed() data_view_mock.enable_tool_button.assert_called_once_with(ToolItemText.NONORTHOGONAL_AXES) @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.peaksviewer.presenter.TableWorkspaceDataPresenterStandard") @mock.patch("mantidqt.widgets.sliceviewer.presenter.PeaksViewerCollectionPresenter", spec=PeaksViewerCollectionPresenter) def test_overlay_peaks_workspaces_attaches_view_and_draws_peaks(self, mock_peaks_presenter, *_): for nonortho_axes in (False, True): presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), nonortho_axes, nonortho_axes) presenter.view.query_peaks_to_overlay.side_effect = ["peaks_workspace"] presenter.overlay_peaks_workspaces() presenter.view.query_peaks_to_overlay.assert_called_once() mock_peaks_presenter.assert_called_once() mock_peaks_presenter.overlay_peaksworkspaces.asssert_called_once() mock_peaks_presenter.reset_mock() presenter.view.query_peaks_to_overlay.reset_mock() @patch("sip.isdeleted", return_value=False) def test_gui_starts_with_zoom_selected(self, _): SliceViewer(None, model=self.model, view=self.view) self.view.data_view.activate_tool.assert_called_once_with(ToolItemText.ZOOM) @patch("sip.isdeleted", return_value=False) def test_replace_workspace_returns_when_the_workspace_is_not_the_model_workspace(self, _): self.model.workspace_equals.return_value = False presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) presenter.update_view = mock.Mock() presenter._decide_plot_update_methods = mock.Mock() other_workspace = mock.Mock() presenter.replace_workspace('other_workspace', other_workspace) presenter._decide_plot_update_methods.assert_not_called() presenter.update_view.assert_not_called() @patch("sip.isdeleted", return_value=False) def test_replace_workspace_closes_view_when_model_properties_change(self, _): self.model.workspace_equals.return_value = True presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) presenter.refresh_view = mock.Mock() presenter._decide_plot_update_methods = mock.Mock() workspace = create_workspace_mock() # Not equivalent to self.model.get_properties() new_model_properties = { "workspace_type": "WS_TYPE.MDE", "supports_normalise": False, "supports_nonorthogonal_axes": False, "supports_dynamic_rebinning": False, "supports_peaks_overlays": True } with patch.object(SliceViewerModel, "get_properties", return_value=new_model_properties): presenter.replace_workspace('workspace', workspace) self.view.emit_close.assert_called_once() presenter._decide_plot_update_methods.assert_not_called() presenter.refresh_view.assert_not_called() @patch("sip.isdeleted", return_value=False) def test_replace_workspace_updates_view(self, _): presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) self.view.delayed_refresh = mock.Mock() presenter._decide_plot_update_methods = mock.Mock( return_value=(presenter.new_plot_matrix(), presenter.update_plot_data_matrix())) workspace = create_workspace_mock() new_model_properties = self.model.get_properties() # Patch get_properties so that the properties of the new model match those of self.model with patch.object(SliceViewerModel, "get_properties", return_value=new_model_properties): presenter.replace_workspace('workspace', workspace) self.view.emit_close.assert_not_called() presenter._decide_plot_update_methods.assert_called_once() self.view.delayed_refresh.assert_called_once() @patch("sip.isdeleted", return_value=False) def test_refresh_view(self, _): presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) presenter.new_plot = mock.Mock() presenter.refresh_view() self.view.data_view.image_info_widget.setWorkspace.assert_called() self.view.setWindowTitle.assert_called_with(self.model.get_title()) presenter.new_plot.assert_called_once() @patch("sip.isdeleted", return_value=True) def test_refresh_view_does_nothing_when_view_deleted(self, _): presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) presenter.new_plot = mock.Mock() presenter.refresh_view() self.view.data_view.image_info_widget.setWorkspace.assert_not_called() presenter.new_plot.assert_not_called() @patch("sip.isdeleted", return_value=False) def test_clear_observer_peaks_presenter_not_none(self, _): presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) presenter._peaks_presenter = mock.MagicMock() presenter.clear_observer() presenter._peaks_presenter.clear_observer.assert_called_once() @patch("sip.isdeleted", return_value=False) def test_clear_observer_peaks_presenter_is_none(self, _): presenter, _ = _create_presenter(self.model, self.view, mock.MagicMock(), enable_nonortho_axes=False, supports_nonortho=False) presenter._peaks_presenter = None # Will raise exception if misbehaving. presenter.clear_observer() @patch("sip.isdeleted", return_value=False) @mock.patch("mantidqt.widgets.sliceviewer.presenter.SliceInfo") @mock.patch("mantidqt.widgets.sliceviewer.presenter.PeaksViewerCollectionPresenter", spec=PeaksViewerCollectionPresenter) def test_peak_add_delete_event(self, mock_peaks_presenter, mock_sliceinfo_cls, _): mock_sliceinfo_cls().inverse_transform = mock.Mock(side_effect=lambda pos: pos[::-1]) mock_sliceinfo_cls().z_value = 3 presenter, _ = _create_presenter(self.model, self.view, mock_sliceinfo_cls, enable_nonortho_axes=False, supports_nonortho=True) presenter._peaks_presenter = mock_peaks_presenter event = mock.Mock() event.inaxes = True event.xdata = 1.0 event.ydata = 2.0 presenter.add_delete_peak(event) mock_sliceinfo_cls.get_sliceinfo.assert_not_called() mock_peaks_presenter.add_delete_peak.assert_called_once_with([3, 2, 1]) self.view.data_view.canvas.draw_idle.assert_called_once() if __name__ == '__main__': unittest.main()
gpl-3.0
Phil9l/cosmos
code/artificial_intelligence/src/naive_bayes/gaussian_naive_bayes.py
3
1370
# example using iris dataset # Part of Cosmos by OpenGenus import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn.metrics import classification_report, confusion_matrix dataset = pd.read_csv("iris1.csv", header=0) X = dataset.iloc[:, :-1].values y = dataset.iloc[:, 4].values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, stratify=y) classifier = GaussianNB() classifier.fit(X_train, y_train) y_pred = classifier.predict(X_test) # labeled confusion matrix print( pd.crosstab(y_test, y_pred, rownames=["True"], colnames=["Predicted"], margins=True) ) from sklearn.model_selection import StratifiedKFold from sklearn.metrics import accuracy_score skf = StratifiedKFold(n_splits=10) skf.get_n_splits(X, y) StratifiedKFold(n_splits=10, random_state=None, shuffle=False) a = 0 for train_index, test_index in skf.split(X, y): # print("TRAIN:", train_index, "TEST:", test_index) #These are the mutually exclusive sets from the 10 folds X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] y_pred = classifier.predict(X_test) accuracy = accuracy_score(y_test, y_pred) a += accuracy print("\nK-fold cross validation (10 folds): " + str(a / 10))
gpl-3.0
embray/numpy
numpy/lib/npyio.py
1
66490
from __future__ import division, absolute_import, print_function import sys import os import re import itertools import warnings import weakref from operator import itemgetter import numpy as np from . import format from ._datasource import DataSource from ._compiled_base import packbits, unpackbits from ._iotools import ( LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, has_nested_fields, flatten_dtype, easy_dtype, _bytes_to_name ) from numpy.compat import ( asbytes, asstr, asbytes_nested, bytes, basestring, unicode ) if sys.version_info[0] >= 3: import pickle else: import cPickle as pickle from future_builtins import map loads = pickle.loads __all__ = [ 'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource'] def seek_gzip_factory(f): """Use this factory to produce the class so that we can do a lazy import on gzip. """ import gzip class GzipFile(gzip.GzipFile): def seek(self, offset, whence=0): # figure out new position (we can only seek forwards) if whence == 1: offset = self.offset + offset if whence not in [0, 1]: raise IOError("Illegal argument") if offset < self.offset: # for negative seek, rewind and do positive seek self.rewind() count = offset - self.offset for i in range(count // 1024): self.read(1024) self.read(count % 1024) def tell(self): return self.offset if isinstance(f, str): f = GzipFile(f) elif isinstance(f, gzip.GzipFile): # cast to our GzipFile if its already a gzip.GzipFile try: name = f.name except AttributeError: # Backward compatibility for <= 2.5 name = f.filename mode = f.mode f = GzipFile(fileobj=f.fileobj, filename=name) f.mode = mode return f class BagObj(object): """ BagObj(obj) Convert attribute look-ups to getitems on the object passed in. Parameters ---------- obj : class instance Object on which attribute look-up is performed. Examples -------- >>> from numpy.lib.npyio import BagObj as BO >>> class BagDemo(object): ... def __getitem__(self, key): # An instance of BagObj(BagDemo) ... # will call this method when any ... # attribute look-up is required ... result = "Doesn't matter what you want, " ... return result + "you're gonna get this" ... >>> demo_obj = BagDemo() >>> bagobj = BO(demo_obj) >>> bagobj.hello_there "Doesn't matter what you want, you're gonna get this" >>> bagobj.I_can_be_anything "Doesn't matter what you want, you're gonna get this" """ def __init__(self, obj): # Use weakref to make NpzFile objects collectable by refcount self._obj = weakref.proxy(obj) def __getattribute__(self, key): try: return object.__getattribute__(self, '_obj')[key] except KeyError: raise AttributeError(key) def zipfile_factory(*args, **kwargs): import zipfile kwargs['allowZip64'] = True return zipfile.ZipFile(*args, **kwargs) class NpzFile(object): """ NpzFile(fid) A dictionary-like object with lazy-loading of files in the zipped archive provided on construction. `NpzFile` is used to load files in the NumPy ``.npz`` data archive format. It assumes that files in the archive have a ``.npy`` extension, other files are ignored. The arrays and file strings are lazily loaded on either getitem access using ``obj['key']`` or attribute lookup using ``obj.f.key``. A list of all files (without ``.npy`` extensions) can be obtained with ``obj.files`` and the ZipFile object itself using ``obj.zip``. Attributes ---------- files : list of str List of all files in the archive with a ``.npy`` extension. zip : ZipFile instance The ZipFile object initialized with the zipped archive. f : BagObj instance An object on which attribute can be performed as an alternative to getitem access on the `NpzFile` instance itself. Parameters ---------- fid : file or str The zipped archive to open. This is either a file-like object or a string containing the path to the archive. own_fid : bool, optional Whether NpzFile should close the file handle. Requires that `fid` is a file-like object. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npz = np.load(outfile) >>> isinstance(npz, np.lib.io.NpzFile) True >>> npz.files ['y', 'x'] >>> npz['x'] # getitem access array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> npz.f.x # attribute lookup array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ def __init__(self, fid, own_fid=False): # Import is postponed to here since zipfile depends on gzip, an # optional component of the so-called standard library. _zip = zipfile_factory(fid) self._files = _zip.namelist() self.files = [] for x in self._files: if x.endswith('.npy'): self.files.append(x[:-4]) else: self.files.append(x) self.zip = _zip self.f = BagObj(self) if own_fid: self.fid = fid else: self.fid = None def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): """ Close the file. """ if self.zip is not None: self.zip.close() self.zip = None if self.fid is not None: self.fid.close() self.fid = None self.f = None # break reference cycle def __del__(self): self.close() def __getitem__(self, key): # FIXME: This seems like it will copy strings around # more than is strictly necessary. The zipfile # will read the string and then # the format.read_array will copy the string # to another place in memory. # It would be better if the zipfile could read # (or at least uncompress) the data # directly into the array memory. member = 0 if key in self._files: member = 1 elif key in self.files: member = 1 key += '.npy' if member: bytes = self.zip.open(key) magic = bytes.read(len(format.MAGIC_PREFIX)) bytes.close() if magic == format.MAGIC_PREFIX: bytes = self.zip.open(key) return format.read_array(bytes) else: return self.zip.read(key) else: raise KeyError("%s is not a file in the archive" % key) def __iter__(self): return iter(self.files) def items(self): """ Return a list of tuples, with each tuple (filename, array in file). """ return [(f, self[f]) for f in self.files] def iteritems(self): """Generator that returns tuples (filename, array in file).""" for f in self.files: yield (f, self[f]) def keys(self): """Return files in the archive with a ``.npy`` extension.""" return self.files def iterkeys(self): """Return an iterator over the files in the archive.""" return self.__iter__() def __contains__(self, key): return self.files.__contains__(key) def load(file, mmap_mode=None): """ Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. Parameters ---------- file : file-like object or string The file to read. Compressed files with the filename extension ``.gz`` are acceptable. File-like objects must support the ``seek()`` and ``read()`` methods. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see `numpy.memmap` for a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. Returns ------- result : array, tuple, dict, etc. Data stored in the file. For ``.npz`` files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors. Raises ------ IOError If the input file does not exist or cannot be read. See Also -------- save, savez, savez_compressed, loadtxt memmap : Create a memory-map to an array stored in a file on disk. Notes ----- - If the file contains pickle data, then whatever object is stored in the pickle is returned. - If the file is a ``.npy`` file, then a single array is returned. - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. - If the file is a ``.npz`` file, the returned value supports the context manager protocol in a similar fashion to the open function:: with load('foo.npz') as data: a = data['a'] The underlying file descriptor is closed when exiting the 'with' block. Examples -------- Store data to disk, and load it again: >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) >>> np.load('/tmp/123.npy') array([[1, 2, 3], [4, 5, 6]]) Store compressed data to disk, and load it again: >>> a=np.array([[1, 2, 3], [4, 5, 6]]) >>> b=np.array([1, 2]) >>> np.savez('/tmp/123.npz', a=a, b=b) >>> data = np.load('/tmp/123.npz') >>> data['a'] array([[1, 2, 3], [4, 5, 6]]) >>> data['b'] array([1, 2]) >>> data.close() Mem-map the stored array, and then access the second row directly from disk: >>> X = np.load('/tmp/123.npy', mmap_mode='r') >>> X[1, :] memmap([4, 5, 6]) """ import gzip own_fid = False if isinstance(file, basestring): fid = open(file, "rb") own_fid = True elif isinstance(file, gzip.GzipFile): fid = seek_gzip_factory(file) else: fid = file try: # Code to distinguish from NumPy binary files and pickles. _ZIP_PREFIX = asbytes('PK\x03\x04') N = len(format.MAGIC_PREFIX) magic = fid.read(N) fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) # Transfer file ownership to NpzFile tmp = own_fid own_fid = False return NpzFile(fid, own_fid=tmp) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) else: return format.read_array(fid) else: # Try a pickle try: return pickle.load(fid) except: raise IOError( "Failed to interpret file %s as a pickle" % repr(file)) finally: if own_fid: fid.close() def save(file, arr): """ Save an array to a binary file in NumPy ``.npy`` format. Parameters ---------- file : file or str File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string, a ``.npy`` extension will be appended to the file name if it does not already have one. arr : array_like Array data to be saved. See Also -------- savez : Save several arrays into a ``.npz`` archive savetxt, load Notes ----- For a description of the ``.npy`` format, see `format`. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ own_fid = False if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") own_fid = True else: fid = file try: arr = np.asanyarray(arr) format.write_array(fid, arr) finally: if own_fid: fid.close() def savez(file, *args, **kwds): """ Save several arrays into a single file in uncompressed ``.npz`` format. If arguments are passed in with no keywords, the corresponding variable names, in the ``.npz`` file, are 'arr_0', 'arr_1', etc. If keyword arguments are given, the corresponding variable names, in the ``.npz`` file will match the keyword names. Parameters ---------- file : str or file Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the ``.npz`` extension will be appended to the file name if it is not already there. args : Arguments, optional Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside `savez`, the arrays will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments, optional Arrays to save to the file. Arrays will be saved in the file with the keyword names. Returns ------- None See Also -------- save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in ``.npy`` format. For a description of the ``.npy`` format, see `format`. When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the ``.files`` attribute), and for the arrays themselves. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) Using `savez` with \\*args, the arrays are saved with default names. >>> np.savez(outfile, x, y) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_1', 'arr_0'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Using `savez` with \\**kwds, the arrays are saved with the keyword names. >>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npzfile = np.load(outfile) >>> npzfile.files ['y', 'x'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ _savez(file, args, kwds, False) def savez_compressed(file, *args, **kwds): """ Save several arrays into a single file in compressed ``.npz`` format. If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : str File name of ``.npz`` file. args : Arguments Function arguments. kwds : Keyword arguments Keywords. See Also -------- numpy.savez : Save several arrays into an uncompressed ``.npz`` file format numpy.load : Load the files created by savez_compressed. """ _savez(file, args, kwds, True) def _savez(file, args, kwds, compress): # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile # Import deferred for startup time improvement import tempfile if isinstance(file, basestring): if not file.endswith('.npz'): file = file + '.npz' namedict = kwds for i, val in enumerate(args): key = 'arr_%d' % i if key in namedict.keys(): raise ValueError( "Cannot use un-named variables and keyword %s" % key) namedict[key] = val if compress: compression = zipfile.ZIP_DEFLATED else: compression = zipfile.ZIP_STORED zipf = zipfile_factory(file, mode="w", compression=compression) # Stage arrays in a temporary file on disk, before writing to zip. fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy') os.close(fd) try: for key, val in namedict.items(): fname = key + '.npy' fid = open(tmpfile, 'wb') try: format.write_array(fid, np.asanyarray(val)) fid.close() fid = None zipf.write(tmpfile, arcname=fname) finally: if fid: fid.close() finally: os.remove(tmpfile) zipf.close() def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) if issubclass(typ, np.uint64): return np.uint64 if issubclass(typ, np.int64): return np.int64 if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.floating): return float elif issubclass(typ, np.complex): return complex elif issubclass(typ, np.bytes_): return bytes else: return str def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0): """ Load data from a text file. Each row in the text file must have the same number of values. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings for Python 3k. dtype : data-type, optional Data-type of the resulting array; default: float. If this is a record data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type. comments : str, optional The character used to indicate the start of a comment; default: '#'. delimiter : str, optional The string used to separate values. By default, this is any whitespace. converters : dict, optional A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None. skiprows : int, optional Skip the first `skiprows` lines; default: 0. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)``. When used with a record data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 Returns ------- out : ndarray Data read from the text file. See Also -------- load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files Notes ----- This function aims to be a fast reader for simply formatted files. The `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. Examples -------- >>> from StringIO import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\\n2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72\\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([('M', 21, 72.0), ('F', 35, 58.0)], dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')]) >>> c = StringIO("1,0,2\\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([ 1., 3.]) >>> y array([ 2., 4.]) """ # Type conversions for Py3 convenience comments = asbytes(comments) user_converters = converters if delimiter is not None: delimiter = asbytes(delimiter) if usecols is not None: usecols = list(usecols) fown = False try: if _is_string_like(fname): fown = True if fname.endswith('.gz'): fh = iter(seek_gzip_factory(fname)) elif fname.endswith('.bz2'): import bz2 fh = iter(bz2.BZ2File(fname)) elif sys.version_info[0] == 2: fh = iter(open(fname, 'U')) else: fh = iter(open(fname)) else: fh = iter(fname) except TypeError: raise ValueError('fname must be a string, file handle, or generator') X = [] def flatten_dtype(dt): """Unpack a structured data-type, and produce re-packing info.""" if dt.names is None: # If the dtype is flattened, return. # If the dtype has a shape, the dtype occurs # in the list more than once. shape = dt.shape if len(shape) == 0: return ([dt.base], None) else: packing = [(shape[-1], list)] if len(shape) > 1: for dim in dt.shape[-2::-1]: packing = [(dim*packing[0][0], packing*dim)] return ([dt.base] * int(np.prod(dt.shape)), packing) else: types = [] packing = [] for field in dt.names: tp, bytes = dt.fields[field] flat_dt, flat_packing = flatten_dtype(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays if len(tp.shape) > 0: packing.extend(flat_packing) else: packing.append((len(flat_dt), flat_packing)) return (types, packing) def pack_items(items, packing): """Pack items into nested lists based on re-packing info.""" if packing is None: return items[0] elif packing is tuple: return tuple(items) elif packing is list: return list(items) else: start = 0 ret = [] for length, subpacking in packing: ret.append(pack_items(items[start:start+length], subpacking)) start += length return tuple(ret) def split_line(line): """Chop off comments, strip, and split at delimiter.""" line = asbytes(line).split(comments)[0].strip(asbytes('\r\n')) if line: return line.split(delimiter) else: return [] try: # Make sure we're dealing with a proper dtype dtype = np.dtype(dtype) defconv = _getconv(dtype) # Skip the first `skiprows` lines for i in range(skiprows): next(fh) # Read until we find a line with some values, and use # it to estimate the number of columns, N. first_vals = None try: while not first_vals: first_line = next(fh) first_vals = split_line(first_line) except StopIteration: # End of lines reached first_line = '' first_vals = [] warnings.warn('loadtxt: Empty input file: "%s"' % fname) N = len(usecols or first_vals) dtype_types, packing = flatten_dtype(dtype) if len(dtype_types) > 1: # We're dealing with a structured array, each field of # the dtype matches a column converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype converters = [defconv for i in range(N)] if N > 1: packing = [(N, tuple)] # By preference, use the converters specified by the user for i, conv in (user_converters or {}).items(): if usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue converters[i] = conv # Parse each line, including the first for i, line in enumerate(itertools.chain([first_line], fh)): vals = split_line(line) if len(vals) == 0: continue if usecols: vals = [vals[i] for i in usecols] # Convert each value according to its column and store items = [conv(val) for (conv, val) in zip(converters, vals)] # Then pack it according to the dtype's nesting items = pack_items(items, packing) X.append(items) finally: if fown: fh.close() X = np.array(X, dtype) # Multicolumn data are returned with shape (1, N, M), i.e. # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) # Verify that the array has at least dimensions `ndmin`. # Check correctness of the values of `ndmin` if not ndmin in [0, 1, 2]: raise ValueError('Illegal value of ndmin keyword: %s' % ndmin) # Tweak the size and shape of the arrays - remove extraneous dimensions if X.ndim > ndmin: X = np.squeeze(X) # and ensure we have the minimum number of dimensions asked for # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 if X.ndim < ndmin: if ndmin == 1: X = np.atleast_1d(X) elif ndmin == 2: X = np.atleast_2d(X).T if unpack: if len(dtype_types) > 1: # For structured arrays, return an array for each field. return [X[field] for field in dtype.names] else: return X.T else: return X def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '): """ Save an array to a text file. Parameters ---------- fname : filename or file handle If the filename ends in ``.gz``, the file is automatically saved in compressed gzip format. `loadtxt` understands gzipped files transparently. X : array_like Data to be saved to a text file. fmt : str or sequence of strs, optional A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. For complex `X`, the legal options for `fmt` are: a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted like `' (%s+%sj)' % (fmt, fmt)` b) a full string specifying every real and imaginary part, e.g. `' %.4e %+.4j %.4e %+.4j %.4e %+.4j'` for 3 columns c) a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns delimiter : str, optional Character separating columns. newline : str, optional .. versionadded:: 1.5.0 header : str, optional String that will be written at the beginning of the file. .. versionadded:: 1.7.0 footer : str, optional String that will be written at the end of the file. .. versionadded:: 1.7.0 comments : str, optional String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# ', as expected by e.g. ``numpy.loadtxt``. .. versionadded:: 1.7.0 Character separating lines. See Also -------- save : Save an array to a binary file in NumPy ``.npy`` format savez : Save several arrays into an uncompressed ``.npz`` archive savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- Further explanation of the `fmt` parameter (``%[flag]width[.precision]specifier``): flags: ``-`` : left justify ``+`` : Forces to precede result with + or -. ``0`` : Left pad the number with zeros instead of space (see width). width: Minimum number of characters to be printed. The value is not truncated if it has more characters. precision: - For integer specifiers (eg. ``d,i,o,x``), the minimum number of digits. - For ``e, E`` and ``f`` specifiers, the number of digits to print after the decimal point. - For ``g`` and ``G``, the maximum number of significant digits. - For ``s``, the maximum number of characters. specifiers: ``c`` : character ``d`` or ``i`` : signed decimal integer ``e`` or ``E`` : scientific notation with ``e`` or ``E``. ``f`` : decimal floating point ``g,G`` : use the shorter of ``e,E`` or ``f`` ``o`` : signed octal ``s`` : string of characters ``u`` : unsigned decimal integer ``x,X`` : unsigned hexadecimal integer This explanation of ``fmt`` is not complete, for an exhaustive specification see [1]_. References ---------- .. [1] `Format Specification Mini-Language <http://docs.python.org/library/string.html# format-specification-mini-language>`_, Python Documentation. Examples -------- >>> x = y = z = np.arange(0.0,5.0,1.0) >>> np.savetxt('test.out', x, delimiter=',') # X is an array >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation """ # Py3 conversions first if isinstance(fmt, bytes): fmt = asstr(fmt) delimiter = asstr(delimiter) own_fh = False if _is_string_like(fname): own_fh = True if fname.endswith('.gz'): import gzip fh = gzip.open(fname, 'wb') else: if sys.version_info[0] >= 3: fh = open(fname, 'wb') else: fh = open(fname, 'w') elif hasattr(fname, 'write'): fh = fname else: raise ValueError('fname must be a string or file handle') try: X = np.asarray(X) # Handle 1-dimensional arrays if X.ndim == 1: # Common case -- 1d array of numbers if X.dtype.names is None: X = np.atleast_2d(X).T ncol = 1 # Complex dtype -- each field indicates a separate column else: ncol = len(X.dtype.descr) else: ncol = X.shape[1] iscomplex_X = np.iscomplexobj(X) # `fmt` can be a string with multiple insertion points or a # list of formats. E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') if type(fmt) in (list, tuple): if len(fmt) != ncol: raise AttributeError('fmt has wrong shape. %s' % str(fmt)) format = asstr(delimiter).join(map(asstr, fmt)) elif isinstance(fmt, str): n_fmt_chars = fmt.count('%') error = ValueError('fmt has wrong number of %% formats: %s' % fmt) if n_fmt_chars == 1: if iscomplex_X: fmt = [' (%s+%sj)' % (fmt, fmt), ] * ncol else: fmt = [fmt, ] * ncol format = delimiter.join(fmt) elif iscomplex_X and n_fmt_chars != (2 * ncol): raise error elif ((not iscomplex_X) and n_fmt_chars != ncol): raise error else: format = fmt else: raise ValueError('invalid fmt: %r' % (fmt,)) if len(header) > 0: header = header.replace('\n', '\n' + comments) fh.write(asbytes(comments + header + newline)) if iscomplex_X: for row in X: row2 = [] for number in row: row2.append(number.real) row2.append(number.imag) fh.write(asbytes(format % tuple(row2) + newline)) else: for row in X: fh.write(asbytes(format % tuple(row) + newline)) if len(footer) > 0: footer = footer.replace('\n', '\n' + comments) fh.write(asbytes(comments + footer + newline)) finally: if own_fh: fh.close() def fromregex(file, regexp, dtype): """ Construct an array from a text file, using regular expression parsing. The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array. Parameters ---------- file : str or file File name or file object to read. regexp : str or regexp Regular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype. dtype : dtype or list of dtypes Dtype for the structured array. Returns ------- output : ndarray The output array, containing the part of the content of `file` that was matched by `regexp`. `output` is always a structured array. Raises ------ TypeError When `dtype` is not a valid dtype for a structured array. See Also -------- fromstring, loadtxt Notes ----- Dtypes for structured arrays can be specified in several forms, but all forms specify at least the data type and field name. For details see `doc.structured_arrays`. Examples -------- >>> f = open('test.dat', 'w') >>> f.write("1312 foo\\n1534 bar\\n444 qux") >>> f.close() >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex('test.dat', regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], dtype=[('num', '<i8'), ('key', '|S3')]) >>> output['num'] array([1312, 1534, 444], dtype=int64) """ own_fh = False if not hasattr(file, "read"): file = open(file, 'rb') own_fh = True try: if not hasattr(regexp, 'match'): regexp = re.compile(asbytes(regexp)) if not isinstance(dtype, np.dtype): dtype = np.dtype(dtype) seq = regexp.findall(file.read()) if seq and not isinstance(seq[0], tuple): # Only one group is in the regexp. # Create the new array as a single data-type and then # re-interpret as a single-field structured array. newdtype = np.dtype(dtype[dtype.names[0]]) output = np.array(seq, dtype=newdtype) output.dtype = dtype else: output = np.array(seq, dtype=dtype) return output finally: if own_fh: file.close() #####-------------------------------------------------------------------------- #---- --- ASCII functions --- #####-------------------------------------------------------------------------- def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skiprows=0, skip_header=0, skip_footer=0, converters=None, missing='', missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True): """ Load data from a text file, with missing values handled as specified. Each line past the first `skip_header` lines is split at the `delimiter` character, and characters following the `comments` character are discarded. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is `.gz` or `.bz2`, the file is first decompressed. Note that generators must return byte strings in Python 3k. dtype : dtype, optional Data type of the resulting array. If None, the dtypes will be determined by the contents of each column, individually. comments : str, optional The character used to indicate the start of a comment. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive whitespaces act as delimiter. An integer or sequence of integers can also be provided as width(s) of each field. skip_rows : int, optional `skip_rows` was deprecated in numpy 1.5, and will be removed in numpy 2.0. Please use `skip_header` instead. skip_header : int, optional The number of lines to skip at the beginning of the file. skip_footer : int, optional The number of lines to skip at the end of the file. converters : variable, optional The set of functions that convert the data of a column to a value. The converters can also be used to provide a default value for missing data: ``converters = {3: lambda s: float(s or 0)}``. missing : variable, optional `missing` was deprecated in numpy 1.5, and will be removed in numpy 2.0. Please use `missing_values` instead. missing_values : variable, optional The set of strings corresponding to missing data. filling_values : variable, optional The set of values to be used as default when the data are missing. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. names : {None, True, str, sequence}, optional If `names` is True, the field names are read from the first valid line after the first `skip_header` lines. If `names` is a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. If `names` is None, the names of the dtype fields will be used, if any. excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended an underscore: for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional Whether to automatically strip white spaces from the variables. replace_space : char, optional Character(s) used in replacement of white spaces in the variables names. By default, use a '_'. case_sensitive : {True, False, 'upper', 'lower'}, optional If True, field names are case sensitive. If False or 'upper', field names are converted to upper case. If 'lower', field names are converted to lower case. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)`` usemask : bool, optional If True, return a masked array. If False, return a regular array. loose : bool, optional If True, do not raise errors for invalid values. invalid_raise : bool, optional If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped. Returns ------- out : ndarray Data read from the text file. If `usemask` is True, this is a masked array. See Also -------- numpy.loadtxt : equivalent function when no data is missing. Notes ----- * When spaces are used as delimiters, or when no delimiter has been given as input, there should not be any missing data between two fields. * When the variables are named (either by a flexible dtype or with `names`, there must not be any header in the file (else a ValueError exception is raised). * Individual values are not stripped of spaces by default. When using a custom converter, make sure the function does remove spaces. References ---------- .. [1] Numpy User Guide, section `I/O with Numpy <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_. Examples --------- >>> from StringIO import StringIO >>> import numpy as np Comma delimited file with mixed dtype >>> s = StringIO("1,1.3,abcde") >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ... ('mystring','S5')], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Using dtype = None >>> s.seek(0) # needed for StringIO example only >>> data = np.genfromtxt(s, dtype=None, ... names = ['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Specifying dtype and names >>> s.seek(0) >>> data = np.genfromtxt(s, dtype="i8,f8,S5", ... names=['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) An example with fixed-width columns >>> s = StringIO("11.3abcde") >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], ... delimiter=[1,3,5]) >>> data array((1, 1.3, 'abcde'), dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')]) """ # Py3 data conversions to bytes, for convenience if comments is not None: comments = asbytes(comments) if isinstance(delimiter, unicode): delimiter = asbytes(delimiter) if isinstance(missing, unicode): missing = asbytes(missing) if isinstance(missing_values, (unicode, list, tuple)): missing_values = asbytes_nested(missing_values) # if usemask: from numpy.ma import MaskedArray, make_mask_descr # Check the input dictionary of converters user_converters = converters or {} if not isinstance(user_converters, dict): raise TypeError( "The input argument 'converter' should be a valid dictionary " "(got '%s' instead)" % type(user_converters)) # Initialize the filehandle, the LineSplitter and the NameValidator own_fhd = False try: if isinstance(fname, basestring): if sys.version_info[0] == 2: fhd = iter(np.lib._datasource.open(fname, 'rbU')) else: fhd = iter(np.lib._datasource.open(fname, 'rb')) own_fhd = True else: fhd = iter(fname) except TypeError: raise TypeError( "fname must be a string, filehandle, or generator. " "(got %s instead)" % type(fname)) split_line = LineSplitter(delimiter=delimiter, comments=comments, autostrip=autostrip)._handyman validate_names = NameValidator(excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Get the first valid lines after the first skiprows ones .. if skiprows: warnings.warn( "The use of `skiprows` is deprecated, it will be removed in " "numpy 2.0.\nPlease use `skip_header` instead.", DeprecationWarning) skip_header = skiprows # Skip the first `skip_header` rows for i in range(skip_header): next(fhd) # Keep on until we find the first valid values first_values = None try: while not first_values: first_line = next(fhd) if names is True: if comments in first_line: first_line = asbytes('').join(first_line.split(comments)[1:]) first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty first_line = asbytes('') first_values = [] warnings.warn('genfromtxt: Empty input file: "%s"' % fname) # Should we take the first values as names ? if names is True: fval = first_values[0].strip() if fval in comments: del first_values[0] # Check the columns to use: make sure `usecols` is a list if usecols is not None: try: usecols = [_.strip() for _ in usecols.split(",")] except AttributeError: try: usecols = list(usecols) except TypeError: usecols = [usecols, ] nbcols = len(usecols or first_values) # Check the names and overwrite the dtype.names if needed if names is True: names = validate_names([_bytes_to_name(_.strip()) for _ in first_values]) first_line = asbytes('') elif _is_string_like(names): names = validate_names([_.strip() for _ in names.split(',')]) elif names: names = validate_names(names) # Get the dtype if dtype is not None: dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names) # Make sure the names is a list (for 2.5) if names is not None: names = list(names) if usecols: for (i, current) in enumerate(usecols): # if usecols is a list of names, convert to a list of indices if _is_string_like(current): usecols[i] = names.index(current) elif current < 0: usecols[i] = current + len(first_values) # If the dtype is not None, make sure we update it if (dtype is not None) and (len(dtype) > nbcols): descr = dtype.descr dtype = np.dtype([descr[_] for _ in usecols]) names = list(dtype.names) # If `names` is not None, update the names elif (names is not None) and (len(names) > nbcols): names = [names[_] for _ in usecols] elif (names is not None) and (dtype is not None): names = list(dtype.names) # Process the missing values ............................... # Rename missing_values for convenience user_missing_values = missing_values or () # Define the list of missing_values (one column: one list) missing_values = [list([asbytes('')]) for _ in range(nbcols)] # We have a dictionary: process it field by field if isinstance(user_missing_values, dict): # Loop on the items for (key, val) in user_missing_values.items(): # Is the key a string ? if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped continue # Redefine the key as needed if it's a column number if usecols: try: key = usecols.index(key) except ValueError: pass # Transform the value as a list of string if isinstance(val, (list, tuple)): val = [str(_) for _ in val] else: val = [str(val), ] # Add the value(s) to the current list of missing if key is None: # None acts as default for miss in missing_values: miss.extend(val) else: missing_values[key].extend(val) # We have a sequence : each item matches a column elif isinstance(user_missing_values, (list, tuple)): for (value, entry) in zip(user_missing_values, missing_values): value = str(value) if value not in entry: entry.append(value) # We have a string : apply it to all entries elif isinstance(user_missing_values, bytes): user_value = user_missing_values.split(asbytes(",")) for entry in missing_values: entry.extend(user_value) # We have something else: apply it to all entries else: for entry in missing_values: entry.extend([str(user_missing_values)]) # Process the deprecated `missing` if missing != asbytes(''): warnings.warn( "The use of `missing` is deprecated, it will be removed in " "Numpy 2.0.\nPlease use `missing_values` instead.", DeprecationWarning) values = [str(_) for _ in missing.split(asbytes(","))] for entry in missing_values: entry.extend(values) # Process the filling_values ............................... # Rename the input for convenience user_filling_values = filling_values or [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually if isinstance(user_filling_values, dict): for (key, val) in user_filling_values.items(): if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped, continue # Redefine the key if it's a column number and usecols is defined if usecols: try: key = usecols.index(key) except ValueError: pass # Add the value to the list filling_values[key] = val # We have a sequence : update on a one-to-one basis elif isinstance(user_filling_values, (list, tuple)): n = len(user_filling_values) if (n <= nbcols): filling_values[:n] = user_filling_values else: filling_values = user_filling_values[:nbcols] # We have something else : use it for all entries else: filling_values = [user_filling_values] * nbcols # Initialize the converters ................................ if dtype is None: # Note: we can't use a [...]*nbcols, as we would have 3 times the same # ... converter, instead of 3 different converters. converters = [StringConverter(None, missing_values=miss, default=fill) for (miss, fill) in zip(missing_values, filling_values)] else: dtype_flat = flatten_dtype(dtype, flatten_base=True) # Initialize the converters if len(dtype_flat) > 1: # Flexible type : get a converter from each dtype zipit = zip(dtype_flat, missing_values, filling_values) converters = [StringConverter(dt, locked=True, missing_values=miss, default=fill) for (dt, miss, fill) in zipit] else: # Set to a default converter (but w/ different missing values) zipit = zip(missing_values, filling_values) converters = [StringConverter(dtype, locked=True, missing_values=miss, default=fill) for (miss, fill) in zipit] # Update the converters to use the user-defined ones uc_update = [] for (i, conv) in user_converters.items(): # If the converter is specified by column names, use the index instead if _is_string_like(i): try: i = names.index(i) except ValueError: continue elif usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue # Find the value to test: if len(first_line): testing_value = first_values[i] else: testing_value = None converters[i].update(conv, locked=True, testing_value=testing_value, default=filling_values[i], missing_values=missing_values[i],) uc_update.append((i, conv)) # Make sure we have the corrected keys in user_converters... user_converters.update(uc_update) miss_chars = [_.missing_values for _ in converters] # Initialize the output lists ... # ... rows rows = [] append_to_rows = rows.append # ... masks if usemask: masks = [] append_to_masks = masks.append # ... invalid invalid = [] append_to_invalid = invalid.append # Parse each line for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): values = split_line(line) nbvalues = len(values) # Skip an empty line if nbvalues == 0: continue # Select only the columns we need if usecols: try: values = [values[_] for _ in usecols] except IndexError: append_to_invalid((i + skip_header + 1, nbvalues)) continue elif nbvalues != nbcols: append_to_invalid((i + skip_header + 1, nbvalues)) continue # Store the values append_to_rows(tuple(values)) if usemask: append_to_masks(tuple([v.strip() in m for (v, m) in zip(values, missing_values)])) if own_fhd: fhd.close() # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): current_column = [itemgetter(i)(_m) for _m in rows] try: converter.iterupgrade(current_column) except ConverterLockError: errmsg = "Converter #%i is locked and cannot be upgraded: " % i current_column = map(itemgetter(i), rows) for (j, value) in enumerate(current_column): try: converter.upgrade(value) except (ConverterError, ValueError): errmsg += "(occurred line #%i for value '%s')" errmsg %= (j + 1 + skip_header, value) raise ConverterError(errmsg) # Check that we don't have invalid values nbinvalid = len(invalid) if nbinvalid > 0: nbrows = len(rows) + nbinvalid - skip_footer # Construct the error message template = " Line #%%i (got %%i columns instead of %i)" % nbcols if skip_footer > 0: nbinvalid_skipped = len([_ for _ in invalid if _[0] > nbrows + skip_header]) invalid = invalid[:nbinvalid - nbinvalid_skipped] skip_footer -= nbinvalid_skipped # # nbrows -= skip_footer # errmsg = [template % (i, nb) # for (i, nb) in invalid if i < nbrows] # else: errmsg = [template % (i, nb) for (i, nb) in invalid] if len(errmsg): errmsg.insert(0, "Some errors were detected !") errmsg = "\n".join(errmsg) # Raise an exception ? if invalid_raise: raise ValueError(errmsg) # Issue a warning ? else: warnings.warn(errmsg, ConversionWarning) # Strip the last skip_footer data if skip_footer > 0: rows = rows[:-skip_footer] if usemask: masks = masks[:-skip_footer] # Convert each value according to the converter: # We want to modify the list in place to avoid creating a new one... # # if loose: # conversionfuncs = [conv._loose_call for conv in converters] # else: # conversionfuncs = [conv._strict_call for conv in converters] # for (i, vals) in enumerate(rows): # rows[i] = tuple([convert(val) # for (convert, val) in zip(conversionfuncs, vals)]) if loose: rows = list(zip(*[[converter._loose_call(_r) for _r in map(itemgetter(i), rows)] for (i, converter) in enumerate(converters)])) else: rows = list(zip(*[[converter._strict_call(_r) for _r in map(itemgetter(i), rows)] for (i, converter) in enumerate(converters)])) # Reset the dtype data = rows if dtype is None: # Get the dtypes from the types of the converters column_types = [conv.type for conv in converters] # Find the columns with strings... strcolidx = [i for (i, v) in enumerate(column_types) if v in (type('S'), np.string_)] # ... and take the largest number of chars. for i in strcolidx: column_types[i] = "|S%i" % max(len(row[i]) for row in data) # if names is None: # If the dtype is uniform, don't define names, else use '' base = set([c.type for c in converters if c._checked]) if len(base) == 1: (ddtype, mdtype) = (list(base)[0], np.bool) else: ddtype = [(defaultfmt % i, dt) for (i, dt) in enumerate(column_types)] if usemask: mdtype = [(defaultfmt % i, np.bool) for (i, dt) in enumerate(column_types)] else: ddtype = list(zip(names, column_types)) mdtype = list(zip(names, [np.bool] * len(column_types))) output = np.array(data, dtype=ddtype) if usemask: outputmask = np.array(masks, dtype=mdtype) else: # Overwrite the initial dtype names if needed if names and dtype.names: dtype.names = names # Case 1. We have a structured type if len(dtype_flat) > 1: # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] # First, create the array using a flattened dtype: # [('a', int), ('b1', int), ('b2', float)] # Then, view the array using the specified dtype. if 'O' in (_.char for _ in dtype_flat): if has_nested_fields(dtype): raise NotImplementedError( "Nested fields involving objects are not supported...") else: output = np.array(data, dtype=dtype) else: rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) output = rows.view(dtype) # Now, process the rowmasks the same way if usemask: rowmasks = np.array( masks, dtype=np.dtype([('', np.bool) for t in dtype_flat])) # Construct the new dtype mdtype = make_mask_descr(dtype) outputmask = rowmasks.view(mdtype) # Case #2. We have a basic dtype else: # We used some user-defined converters if user_converters: ishomogeneous = True descr = [] for (i, ttype) in enumerate([conv.type for conv in converters]): # Keep the dtype of the current converter if i in user_converters: ishomogeneous &= (ttype == dtype.type) if ttype == np.string_: ttype = "|S%i" % max(len(row[i]) for row in data) descr.append(('', ttype)) else: descr.append(('', dtype)) # So we changed the dtype ? if not ishomogeneous: # We have more than one field if len(descr) > 1: dtype = np.dtype(descr) # We have only one field: drop the name if not needed. else: dtype = np.dtype(ttype) # output = np.array(data, dtype) if usemask: if dtype.names: mdtype = [(_, np.bool) for _ in dtype.names] else: mdtype = np.bool outputmask = np.array(masks, dtype=mdtype) # Try to take care of the missing data we missed names = output.dtype.names if usemask and names: for (name, conv) in zip(names or (), converters): missing_values = [conv(_) for _ in conv.missing_values if _ != asbytes('')] for mval in missing_values: outputmask[name] |= (output[name] == mval) # Construct the final array if usemask: output = output.view(MaskedArray) output._mask = outputmask if unpack: return output.squeeze().T return output.squeeze() def ndfromtxt(fname, **kwargs): """ Load ASCII data stored in a file and return it as a single array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function. """ kwargs['usemask'] = False return genfromtxt(fname, **kwargs) def mafromtxt(fname, **kwargs): """ Load ASCII data stored in a text file and return a masked array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ kwargs['usemask'] = True return genfromtxt(fname, **kwargs) def recfromtxt(fname, **kwargs): """ Load ASCII data from a file and return it in a record array. If ``usemask=False`` a standard `recarray` is returned, if ``usemask=True`` a MaskedRecords array is returned. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ kwargs.update(dtype=kwargs.get('dtype', None)) usemask = kwargs.get('usemask', False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output def recfromcsv(fname, **kwargs): """ Load ASCII data stored in a comma-separated file. The returned array is a record array (if ``usemask=False``, see `recarray`) or a masked record array (if ``usemask=True``, see `ma.mrecords.MaskedRecords`). Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ case_sensitive = kwargs.get('case_sensitive', "lower") or "lower" names = kwargs.get('names', True) if names is None: names = True kwargs.update(dtype=kwargs.get('update', None), delimiter=kwargs.get('delimiter', ",") or ",", names=names, case_sensitive=case_sensitive) usemask = kwargs.get("usemask", False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output
bsd-3-clause