<commit_before>"""
	Copyright (C) 2009 Sergey Koposov

    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 mpfit
import re
import numpy
import scipy

def mpfitexpr(func, x, y, err , start_params, check=True, full_output=False, **kw):
	"""Fit the used defined expression to the data
	Input:
	- func: string with the function definition
	- x: x vector
	- y: y vector
	- err: vector with the errors of y
	- start_params: the starting parameters for the fit
	Output:
	- The tuple (params, yfit) with best-fit params and the values of func evaluated at x
	Keywords:
	- check: boolean parameter. If true(default) the function will be checked for sanity
	- full_output: boolean parameter. If True(default is False) then instead of best-fit parameters the mpfit object is returned
	Example:
	params,yfit=mpfitexpr('p[0]+p[2]*(x-p[1])',x,y,err,[0,10,1])

	If you need to use numpy and scipy functions in your function, then
		you must to use the full names of these functions, e.g.:
		numpy.sin, numpy.cos etc.

	This function is motivated by mpfitexpr() from wonderful MPFIT IDL package
		written by Craig Markwardt

	"""

	def myfunc(p,fjac=None,x=None, y=None, err=None):
		return [0, eval('(y-(%s))/err'%func)]

	myre = "[^a-zA-Z]p\[(\d+)\]"
	r = re.compile(myre)
	maxp = -1
	for m in re.finditer(r,func):
		curp = int(m.group(1))
		maxp = curp if curp > maxp else maxp
	if check:
		if maxp == -1:
			raise Exception("wrong function format")
		if maxp + 1 != len(start_params):
			raise Exception("the length of the start_params != the length of the parameter verctor of the function")
	fa={'x' : x, 'y' : y,'err' : err}
	res = mpfit.mpfit(myfunc,start_params,functkw=fa,**kw)
	yfit = eval(func, globals(), {'x':x, 'p': res.params})
	if full_output:
		return (res, yfit)
	else:
		return (res.params, yfit)
<commit_msg>same to mpfitexpr<commit_after>"""
	Copyright (C) 2009 Sergey Koposov

    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 mpfit
import re
import numpy

def mpfitexpr(func, x, y, err , start_params, check=True, full_output=False, **kw):
	"""Fit the used defined expression to the data
	Input:
	- func: string with the function definition
	- x: x vector
	- y: y vector
	- err: vector with the errors of y
	- start_params: the starting parameters for the fit
	Output:
	- The tuple (params, yfit) with best-fit params and the values of func evaluated at x
	Keywords:
	- check: boolean parameter. If true(default) the function will be checked for sanity
	- full_output: boolean parameter. If True(default is False) then instead of best-fit parameters the mpfit object is returned
	Example:
	params,yfit=mpfitexpr('p[0]+p[2]*(x-p[1])',x,y,err,[0,10,1])

	If you need to use numpy functions in your function, then
		you must to use the full names of these functions, e.g.:
		numpy.sin, numpy.cos etc.

	This function is motivated by mpfitexpr() from wonderful MPFIT IDL package
		written by Craig Markwardt

	"""

	def myfunc(p,fjac=None,x=None, y=None, err=None):
		return [0, eval('(y-(%s))/err'%func)]

	myre = "[^a-zA-Z]p\[(\d+)\]"
	r = re.compile(myre)
	maxp = -1
	for m in re.finditer(r,func):
		curp = int(m.group(1))
		maxp = curp if curp > maxp else maxp
	if check:
		if maxp == -1:
			raise Exception("wrong function format")
		if maxp + 1 != len(start_params):
			raise Exception("the length of the start_params != the length of the parameter verctor of the function")
	fa={'x' : x, 'y' : y,'err' : err}
	res = mpfit.mpfit(myfunc,start_params,functkw=fa,**kw)
	yfit = eval(func, globals(), {'x':x, 'p': res.params})
	if full_output:
		return (res, yfit)
	else:
		return (res.params, yfit)
<|endoftext|>


<commit_before>from jsonifyDF import *
from flask import Flask, jsonify, request

app = Flask(__name__)
mdf = getMatchDF()

@app.route("/", methods=["GET"])
def test():
	return jsonify({"message":"It works!"})

@app.route("/overallstandings", methods=["GET"])
def returnOverallStandings():
	args = request.args
	season = args['season']
	return jsonify({"Overall_Standings_"+season: overall_rank_jsonify(mdf, season)})

@app.route("/consistency", methods=["GET"])
def returnPerformanceConsistencies():
	args = request.args
	lbound = int(args['lbound'])
	ubound = int(args['ubound'])
	print lbound
	print ubound
	return jsonify({"Performance_Consistency_"+str(lbound)+"_to_"+str(ubound): consistency_jsonify(mdf, lbound, ubound)})

if __name__ == "__main__":
	app.run(port=5000)
	<commit_msg>added welcome & help pages for RESTapi usage instructions<commit_after>from jsonifyDF import *
from flask import Flask, jsonify, request

app = Flask(__name__)
mdf = getMatchDF()

@app.route("/", methods=["GET"])
def test():
	return '''
Welcome!

	Modules directory:

	Team Overall Standings

	Team Performance Consistency
'''

@app.route("/overallstandings/help", methods=["GET"])
def returnOverallStandingsHelp():
	return '''
**** Team Overall Standings ****

	about: returns json data of overall standings for each team on the basis of total wins

	usage: /overallstandings?season=2013

	range: 2008 - 2016
'''

@app.route("/consistency/help", methods=["GET"])
def returnPerformanceConsistenciesHelp():
	return '''
**** Team Performance Consistency ****

	about: returns json data of performance consistencies for each team

	usage: /consistency?lbound=2010&ubound=2013

	range: 2008-2016
'''

@app.route("/overallstandings", methods=["GET"])
def returnOverallStandings():
	args = request.args
	season = args['season']
	return jsonify({"Overall_Standings_"+season: overall_rank_jsonify(mdf, season)})

@app.route("/consistency", methods=["GET"])
def returnPerformanceConsistencies():
	args = request.args
	lbound = int(args['lbound'])
	ubound = int(args['ubound'])
	return jsonify({"Performance_Consistency_"+str(lbound)+"_to_"+str(ubound): consistency_jsonify(mdf, lbound, ubound)})

if __name__ == "__main__":
	app.run(port=5000)
	<|endoftext|>


<commit_before>""" Contains the Mode parent class for the Media Controller"""

import logging
from collections import namedtuple

RemoteMethod = namedtuple('RemoteMethod',
                          'method config_section kwargs priority',
                          )
"""RemotedMethod is used by other modules that want to register a method to
be called on mode_start or mode_stop.
"""

# pylint: disable-msg=too-many-instance-attributes
class Mode:
    """Parent class for in-game mode code."""

    __slots__ = ["mc", "config", "name", "path", "asset_paths", "log", "priority", "_active", "stop_methods",
                 "start_callback", "stop_callback", "event_handlers", "target"]

    def __init__(self, mc, config, name, path, asset_paths):
        self.mc = mc
        self.config = config
        self.name = name.lower()
        self.path = path
        self.asset_paths = asset_paths

        self.log = logging.getLogger('Mode.' + name)

        self.priority = 0
        self._active = False
        self.stop_methods = list()
        self.start_callback = None
        self.stop_callback = None
        self.event_handlers = set()
        self.target = None

        if 'mode' in self.config:
            self.configure_mode_settings(config['mode'])

        # Call registered remote loader methods
        for item in self.mc.mode_controller.loader_methods:
            if ((item.config_section in self.config and
                    self.config[item.config_section]) or not
                    item.config_section):
                item.method(config=self.config.get(item.config_section),
                            mode=self,
                            mode_path=self.path,
                            root_config_dict=self.config,
                            **item.kwargs)

    def __repr__(self):
        return '<Mode.{}>'.format(self.name)

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, active):
        if self._active != active:
            self._active = active
            self.mc.mode_controller.active_change(self, self._active)

    def configure_mode_settings(self, config):
        """Processes this mode's configuration settings from a config
        dictionary.
        """

        if not ('priority' in config and isinstance(config['priority'], int)):
            config['priority'] = 0

        try:
            self.target = self.mc.targets[config['target']]
        except KeyError:
            self.target = self.mc.targets['default']

        self.config['mode'] = config

    def start(self, mode_priority=None, callback=None, **kwargs):
        """Starts this mode.

        Args:
            mode_ priority: Integer value of what you want this mode to run at.
                If you don't specify one, it will use the "Mode: priority"
                setting from this mode's configuration file.
            **kwargs: Catch-all since this mode might start from events with
                who-knows-what keyword arguments.

        Warning: You can safely call this method, but do not override it in
        your
        mode code. If you want to write your own mode code by subclassing Mode,
        put whatever code you want to run when this mode starts in the
        mode_start method which will be called automatically.

        """

        # todo might want to use this
        del callback
        del kwargs

        if isinstance(mode_priority, int):
            self.priority = mode_priority
        else:
            self.priority = self.config['mode']['priority']

        self.log.debug('Mode Start. Priority: %s', self.priority)

        self.active = True

        for item in self.mc.mode_controller.start_methods:
            if item.config_section in self.config or not item.config_section:
                self.stop_methods.append(item.method(
                    config=self.config.get(item.config_section),
                    priority=self.priority,
                    mode=self,
                    **item.kwargs))

    @staticmethod
    def is_game_mode() -> bool:
        """Return false since mc does not have game modes."""
        return False

    def stop(self, callback=None, **kwargs):
        """Stops this mode.

        Args:
            **kwargs: Catch-all since this mode might start from events with
                who-knows-what keyword arguments.

        Warning: You can safely call this method, but do not override it in
        your
        mode code. If you want to write your own mode code by subclassing Mode,
        put whatever code you want to run when this mode stops in the
        mode_stop method which will be called automatically.

        """

        # todo might want to use this?
        del callback

        del kwargs

        self.log.debug('Mode Stop.')

        self.priority = 0
        self.active = False

        for callback_func in self.mc.mode_controller.stop_methods:
            callback_func[0](self)

        for item in self.stop_methods:
            item[0](item[1])

        self.stop_methods = list()
<commit_msg>CS fix<commit_after>""" Contains the Mode parent class for the Media Controller"""

import logging
from collections import namedtuple

RemoteMethod = namedtuple('RemoteMethod',
                          'method config_section kwargs priority',
                          )
"""RemotedMethod is used by other modules that want to register a method to
be called on mode_start or mode_stop.
"""

# pylint: disable-msg=too-many-instance-attributes
class Mode:
    """Parent class for in-game mode code."""

    __slots__ = ["mc", "config", "name", "path", "asset_paths", "log", "priority", "_active", "stop_methods",
                 "start_callback", "stop_callback", "event_handlers", "target"]

    # pylint: disable-msg=too-many-arguments
    def __init__(self, mc, config, name, path, asset_paths):
        self.mc = mc
        self.config = config
        self.name = name.lower()
        self.path = path
        self.asset_paths = asset_paths

        self.log = logging.getLogger('Mode.' + name)

        self.priority = 0
        self._active = False
        self.stop_methods = list()
        self.start_callback = None
        self.stop_callback = None
        self.event_handlers = set()
        self.target = None

        if 'mode' in self.config:
            self.configure_mode_settings(config['mode'])

        # Call registered remote loader methods
        for item in self.mc.mode_controller.loader_methods:
            if ((item.config_section in self.config and
                    self.config[item.config_section]) or not
                    item.config_section):
                item.method(config=self.config.get(item.config_section),
                            mode=self,
                            mode_path=self.path,
                            root_config_dict=self.config,
                            **item.kwargs)

    def __repr__(self):
        return '<Mode.{}>'.format(self.name)

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, active):
        if self._active != active:
            self._active = active
            self.mc.mode_controller.active_change(self, self._active)

    def configure_mode_settings(self, config):
        """Processes this mode's configuration settings from a config
        dictionary.
        """

        if not ('priority' in config and isinstance(config['priority'], int)):
            config['priority'] = 0

        try:
            self.target = self.mc.targets[config['target']]
        except KeyError:
            self.target = self.mc.targets['default']

        self.config['mode'] = config

    def start(self, mode_priority=None, callback=None, **kwargs):
        """Starts this mode.

        Args:
            mode_ priority: Integer value of what you want this mode to run at.
                If you don't specify one, it will use the "Mode: priority"
                setting from this mode's configuration file.
            **kwargs: Catch-all since this mode might start from events with
                who-knows-what keyword arguments.

        Warning: You can safely call this method, but do not override it in
        your
        mode code. If you want to write your own mode code by subclassing Mode,
        put whatever code you want to run when this mode starts in the
        mode_start method which will be called automatically.

        """

        # todo might want to use this
        del callback
        del kwargs

        if isinstance(mode_priority, int):
            self.priority = mode_priority
        else:
            self.priority = self.config['mode']['priority']

        self.log.debug('Mode Start. Priority: %s', self.priority)

        self.active = True

        for item in self.mc.mode_controller.start_methods:
            if item.config_section in self.config or not item.config_section:
                self.stop_methods.append(item.method(
                    config=self.config.get(item.config_section),
                    priority=self.priority,
                    mode=self,
                    **item.kwargs))

    @staticmethod
    def is_game_mode() -> bool:
        """Return false since mc does not have game modes."""
        return False

    def stop(self, callback=None, **kwargs):
        """Stops this mode.

        Args:
            **kwargs: Catch-all since this mode might start from events with
                who-knows-what keyword arguments.

        Warning: You can safely call this method, but do not override it in
        your
        mode code. If you want to write your own mode code by subclassing Mode,
        put whatever code you want to run when this mode stops in the
        mode_stop method which will be called automatically.

        """

        # todo might want to use this?
        del callback

        del kwargs

        self.log.debug('Mode Stop.')

        self.priority = 0
        self.active = False

        for callback_func in self.mc.mode_controller.stop_methods:
            callback_func[0](self)

        for item in self.stop_methods:
            item[0](item[1])

        self.stop_methods = list()
<|endoftext|>


<commit_before>__version__ = '0.18-svn'

from .usertools import monitor, timing

from .ctx_fp import FPContext
from .ctx_mp import MPContext
from .ctx_iv import MPIntervalContext

fp = FPContext()
mp = MPContext()
iv = MPIntervalContext()

fp._mp = mp
mp._mp = mp
iv._mp = mp
mp._fp = fp
fp._fp = fp
mp._iv = iv
fp._iv = iv
iv._iv = iv

# XXX: extremely bad pickle hack
from . import ctx_mp as _ctx_mp
_ctx_mp._mpf_module.mpf = mp.mpf
_ctx_mp._mpf_module.mpc = mp.mpc

make_mpf = mp.make_mpf
make_mpc = mp.make_mpc

extraprec = mp.extraprec
extradps = mp.extradps
workprec = mp.workprec
workdps = mp.workdps
autoprec = mp.autoprec
maxcalls = mp.maxcalls
memoize = mp.memoize

mag = mp.mag

bernfrac = mp.bernfrac

qfrom = mp.qfrom
mfrom = mp.mfrom
kfrom = mp.kfrom
taufrom = mp.taufrom
qbarfrom = mp.qbarfrom
ellipfun = mp.ellipfun
jtheta = mp.jtheta
kleinj = mp.kleinj

qp = mp.qp
qhyper = mp.qhyper
qgamma = mp.qgamma
qfac = mp.qfac

nint_distance = mp.nint_distance

plot = mp.plot
cplot = mp.cplot
splot = mp.splot

odefun = mp.odefun

jacobian = mp.jacobian
findroot = mp.findroot
multiplicity = mp.multiplicity

isinf = mp.isinf
isnan = mp.isnan
isnormal = mp.isnormal
isint = mp.isint
almosteq = mp.almosteq
nan = mp.nan
rand = mp.rand

absmin = mp.absmin
absmax = mp.absmax

fraction = mp.fraction

linspace = mp.linspace
arange = mp.arange

mpmathify = convert = mp.convert
mpc = mp.mpc

mpi = iv._mpi

nstr = mp.nstr
nprint = mp.nprint
chop = mp.chop

fneg = mp.fneg
fadd = mp.fadd
fsub = mp.fsub
fmul = mp.fmul
fdiv = mp.fdiv
fprod = mp.fprod

quad = mp.quad
quadgl = mp.quadgl
quadts = mp.quadts
quadosc = mp.quadosc

pslq = mp.pslq
identify = mp.identify
findpoly = mp.findpoly

richardson = mp.richardson
shanks = mp.shanks
nsum = mp.nsum
nprod = mp.nprod
difference = mp.difference
diff = mp.diff
diffs = mp.diffs
diffs_prod = mp.diffs_prod
diffs_exp = mp.diffs_exp
diffun = mp.diffun
differint = mp.differint
taylor = mp.taylor
pade = mp.pade
polyval = mp.polyval
polyroots = mp.polyroots
fourier = mp.fourier
fourierval = mp.fourierval
sumem = mp.sumem
sumap = mp.sumap
chebyfit = mp.chebyfit
limit = mp.limit

matrix = mp.matrix
eye = mp.eye
diag = mp.diag
zeros = mp.zeros
ones = mp.ones
hilbert = mp.hilbert
randmatrix = mp.randmatrix
swap_row = mp.swap_row
extend = mp.extend
norm = mp.norm
mnorm = mp.mnorm

lu_solve = mp.lu_solve
lu = mp.lu
qr = mp.qr
unitvector = mp.unitvector
inverse = mp.inverse
residual = mp.residual
qr_solve = mp.qr_solve
cholesky = mp.cholesky
cholesky_solve = mp.cholesky_solve
det = mp.det
cond = mp.cond

expm = mp.expm
sqrtm = mp.sqrtm
powm = mp.powm
logm = mp.logm
sinm = mp.sinm
cosm = mp.cosm

mpf = mp.mpf
j = mp.j
exp = mp.exp
expj = mp.expj
expjpi = mp.expjpi
ln = mp.ln
im = mp.im
re = mp.re
inf = mp.inf
ninf = mp.ninf
sign = mp.sign

eps = mp.eps
pi = mp.pi
ln2 = mp.ln2
ln10 = mp.ln10
phi = mp.phi
e = mp.e
euler = mp.euler
catalan = mp.catalan
khinchin = mp.khinchin
glaisher = mp.glaisher
apery = mp.apery
degree = mp.degree
twinprime = mp.twinprime
mertens = mp.mertens

ldexp = mp.ldexp
frexp = mp.frexp

fsum = mp.fsum
fdot = mp.fdot

sqrt = mp.sqrt
cbrt = mp.cbrt
exp = mp.exp
ln = mp.ln
log = mp.log
log10 = mp.log10
power = mp.power
cos = mp.cos
sin = mp.sin
tan = mp.tan
cosh = mp.cosh
sinh = mp.sinh
tanh = mp.tanh
acos = mp.acos
asin = mp.asin
atan = mp.atan
asinh = mp.asinh
acosh = mp.acosh
atanh = mp.atanh
sec = mp.sec
csc = mp.csc
cot = mp.cot
sech = mp.sech
csch = mp.csch
coth = mp.coth
asec = mp.asec
acsc = mp.acsc
acot = mp.acot
asech = mp.asech
acsch = mp.acsch
acoth = mp.acoth
cospi = mp.cospi
sinpi = mp.sinpi
sinc = mp.sinc
sincpi = mp.sincpi
cos_sin = mp.cos_sin
cospi_sinpi = mp.cospi_sinpi
fabs = mp.fabs
re = mp.re
im = mp.im
conj = mp.conj
floor = mp.floor
ceil = mp.ceil
nint = mp.nint
frac = mp.frac
root = mp.root
nthroot = mp.nthroot
hypot = mp.hypot
fmod = mp.fmod
ldexp = mp.ldexp
frexp = mp.frexp
sign = mp.sign
arg = mp.arg
phase = mp.phase
polar = mp.polar
rect = mp.rect
degrees = mp.degrees
radians = mp.radians
atan2 = mp.atan2
fib = mp.fib
fibonacci = mp.fibonacci
lambertw = mp.lambertw
zeta = mp.zeta
altzeta = mp.altzeta
gamma = mp.gamma
rgamma = mp.rgamma
factorial = mp.factorial
fac = mp.fac
fac2 = mp.fac2
beta = mp.beta
betainc = mp.betainc
psi = mp.psi
#psi0 = mp.psi0
#psi1 = mp.psi1
#psi2 = mp.psi2
#psi3 = mp.psi3
polygamma = mp.polygamma
digamma = mp.digamma
#trigamma = mp.trigamma
#tetragamma = mp.tetragamma
#pentagamma = mp.pentagamma
harmonic = mp.harmonic
bernoulli = mp.bernoulli
bernfrac = mp.bernfrac
stieltjes = mp.stieltjes
hurwitz = mp.hurwitz
dirichlet = mp.dirichlet
bernpoly = mp.bernpoly
eulerpoly = mp.eulerpoly
eulernum = mp.eulernum
polylog = mp.polylog
clsin = mp.clsin
clcos = mp.clcos
gammainc = mp.gammainc
gammaprod = mp.gammaprod
binomial = mp.binomial
rf = mp.rf
ff = mp.ff
hyper = mp.hyper
hyp0f1 = mp.hyp0f1
hyp1f1 = mp.hyp1f1
hyp1f2 = mp.hyp1f2
hyp2f1 = mp.hyp2f1
hyp2f2 = mp.hyp2f2
hyp2f0 = mp.hyp2f0
hyp2f3 = mp.hyp2f3
hyp3f2 = mp.hyp3f2
hyperu = mp.hyperu
hypercomb = mp.hypercomb
meijerg = mp.meijerg
appellf1 = mp.appellf1
appellf2 = mp.appellf2
appellf3 = mp.appellf3
appellf4 = mp.appellf4
hyper2d = mp.hyper2d
bihyper = mp.bihyper
erf = mp.erf
erfc = mp.erfc
erfi = mp.erfi
erfinv = mp.erfinv
npdf = mp.npdf
ncdf = mp.ncdf
expint = mp.expint
e1 = mp.e1
ei = mp.ei
li = mp.li
ci = mp.ci
si = mp.si
chi = mp.chi
shi = mp.shi
fresnels = mp.fresnels
fresnelc = mp.fresnelc
airyai = mp.airyai
airybi = mp.airybi
airyaizero = mp.airyaizero
airybizero = mp.airybizero
scorergi = mp.scorergi
scorerhi = mp.scorerhi
ellipk = mp.ellipk
ellipe = mp.ellipe
ellipf = mp.ellipf
ellippi = mp.ellippi
elliprc = mp.elliprc
elliprj = mp.elliprj
elliprf = mp.elliprf
elliprd = mp.elliprd
elliprg = mp.elliprg
agm = mp.agm
jacobi = mp.jacobi
chebyt = mp.chebyt
chebyu = mp.chebyu
legendre = mp.legendre
legenp = mp.legenp
legenq = mp.legenq
hermite = mp.hermite
pcfd = mp.pcfd
pcfu = mp.pcfu
pcfv = mp.pcfv
pcfw = mp.pcfw
gegenbauer = mp.gegenbauer
laguerre = mp.laguerre
spherharm = mp.spherharm
besselj = mp.besselj
j0 = mp.j0
j1 = mp.j1
besseli = mp.besseli
bessely = mp.bessely
besselk = mp.besselk
besseljzero = mp.besseljzero
besselyzero = mp.besselyzero
hankel1 = mp.hankel1
hankel2 = mp.hankel2
struveh = mp.struveh
struvel = mp.struvel
angerj = mp.angerj
webere = mp.webere
lommels1 = mp.lommels1
lommels2 = mp.lommels2
whitm = mp.whitm
whitw = mp.whitw
ber = mp.ber
bei = mp.bei
ker = mp.ker
kei = mp.kei
coulombc = mp.coulombc
coulombf = mp.coulombf
coulombg = mp.coulombg
lambertw = mp.lambertw
barnesg = mp.barnesg
superfac = mp.superfac
hyperfac = mp.hyperfac
loggamma = mp.loggamma
siegeltheta = mp.siegeltheta
siegelz = mp.siegelz
grampoint = mp.grampoint
zetazero = mp.zetazero
riemannr = mp.riemannr
primepi = mp.primepi
primepi2 = mp.primepi2
primezeta = mp.primezeta
bell = mp.bell
polyexp = mp.polyexp
expm1 = mp.expm1
powm1 = mp.powm1
unitroots = mp.unitroots
cyclotomic = mp.cyclotomic
mangoldt = mp.mangoldt
secondzeta = mp.secondzeta
nzeros = mp.nzeros
backlunds = mp.backlunds
lerchphi = mp.lerchphi
stirling1 = mp.stirling1
stirling2 = mp.stirling2

# be careful when changing this name, don't use test*!
def runtests():
    """
    Run all mpmath tests and print output.
    """
    import os.path
    from inspect import getsourcefile
    from .tests import runtests as tests
    testdir = os.path.dirname(os.path.abspath(getsourcefile(tests)))
    importdir = os.path.abspath(testdir + '/../..')
    tests.testit(importdir, testdir)

def doctests(filter=[]):
    try:
        import psyco; psyco.full()
    except ImportError:
        pass
    import sys
    from timeit import default_timer as clock
    for i, arg in enumerate(sys.argv):
        if '__init__.py' in arg:
            filter = [sn for sn in sys.argv[i+1:] if not sn.startswith("-")]
            break
    import doctest
    globs = globals().copy()
    for obj in globs: #sorted(globs.keys()):
        if filter:
            if not sum([pat in obj for pat in filter]):
                continue
        sys.stdout.write(str(obj) + " ")
        sys.stdout.flush()
        t1 = clock()
        doctest.run_docstring_examples(globs[obj], {}, verbose=("-v" in sys.argv))
        t2 = clock()
        print(round(t2-t1, 3))

if __name__ == '__main__':
    doctests()

<commit_msg>make eigsy and eighe global functions<commit_after>__version__ = '0.18-svn'

from .usertools import monitor, timing

from .ctx_fp import FPContext
from .ctx_mp import MPContext
from .ctx_iv import MPIntervalContext

fp = FPContext()
mp = MPContext()
iv = MPIntervalContext()

fp._mp = mp
mp._mp = mp
iv._mp = mp
mp._fp = fp
fp._fp = fp
mp._iv = iv
fp._iv = iv
iv._iv = iv

# XXX: extremely bad pickle hack
from . import ctx_mp as _ctx_mp
_ctx_mp._mpf_module.mpf = mp.mpf
_ctx_mp._mpf_module.mpc = mp.mpc

make_mpf = mp.make_mpf
make_mpc = mp.make_mpc

extraprec = mp.extraprec
extradps = mp.extradps
workprec = mp.workprec
workdps = mp.workdps
autoprec = mp.autoprec
maxcalls = mp.maxcalls
memoize = mp.memoize

mag = mp.mag

bernfrac = mp.bernfrac

qfrom = mp.qfrom
mfrom = mp.mfrom
kfrom = mp.kfrom
taufrom = mp.taufrom
qbarfrom = mp.qbarfrom
ellipfun = mp.ellipfun
jtheta = mp.jtheta
kleinj = mp.kleinj

qp = mp.qp
qhyper = mp.qhyper
qgamma = mp.qgamma
qfac = mp.qfac

nint_distance = mp.nint_distance

plot = mp.plot
cplot = mp.cplot
splot = mp.splot

odefun = mp.odefun

jacobian = mp.jacobian
findroot = mp.findroot
multiplicity = mp.multiplicity

isinf = mp.isinf
isnan = mp.isnan
isnormal = mp.isnormal
isint = mp.isint
almosteq = mp.almosteq
nan = mp.nan
rand = mp.rand

absmin = mp.absmin
absmax = mp.absmax

fraction = mp.fraction

linspace = mp.linspace
arange = mp.arange

mpmathify = convert = mp.convert
mpc = mp.mpc

mpi = iv._mpi

nstr = mp.nstr
nprint = mp.nprint
chop = mp.chop

fneg = mp.fneg
fadd = mp.fadd
fsub = mp.fsub
fmul = mp.fmul
fdiv = mp.fdiv
fprod = mp.fprod

quad = mp.quad
quadgl = mp.quadgl
quadts = mp.quadts
quadosc = mp.quadosc

pslq = mp.pslq
identify = mp.identify
findpoly = mp.findpoly

richardson = mp.richardson
shanks = mp.shanks
nsum = mp.nsum
nprod = mp.nprod
difference = mp.difference
diff = mp.diff
diffs = mp.diffs
diffs_prod = mp.diffs_prod
diffs_exp = mp.diffs_exp
diffun = mp.diffun
differint = mp.differint
taylor = mp.taylor
pade = mp.pade
polyval = mp.polyval
polyroots = mp.polyroots
fourier = mp.fourier
fourierval = mp.fourierval
sumem = mp.sumem
sumap = mp.sumap
chebyfit = mp.chebyfit
limit = mp.limit

matrix = mp.matrix
eye = mp.eye
diag = mp.diag
zeros = mp.zeros
ones = mp.ones
hilbert = mp.hilbert
randmatrix = mp.randmatrix
swap_row = mp.swap_row
extend = mp.extend
norm = mp.norm
mnorm = mp.mnorm

lu_solve = mp.lu_solve
lu = mp.lu
qr = mp.qr
unitvector = mp.unitvector
inverse = mp.inverse
residual = mp.residual
qr_solve = mp.qr_solve
cholesky = mp.cholesky
cholesky_solve = mp.cholesky_solve
det = mp.det
cond = mp.cond
eigsy = mp.eigsy
eighe = mp.eighe

expm = mp.expm
sqrtm = mp.sqrtm
powm = mp.powm
logm = mp.logm
sinm = mp.sinm
cosm = mp.cosm

mpf = mp.mpf
j = mp.j
exp = mp.exp
expj = mp.expj
expjpi = mp.expjpi
ln = mp.ln
im = mp.im
re = mp.re
inf = mp.inf
ninf = mp.ninf
sign = mp.sign

eps = mp.eps
pi = mp.pi
ln2 = mp.ln2
ln10 = mp.ln10
phi = mp.phi
e = mp.e
euler = mp.euler
catalan = mp.catalan
khinchin = mp.khinchin
glaisher = mp.glaisher
apery = mp.apery
degree = mp.degree
twinprime = mp.twinprime
mertens = mp.mertens

ldexp = mp.ldexp
frexp = mp.frexp

fsum = mp.fsum
fdot = mp.fdot

sqrt = mp.sqrt
cbrt = mp.cbrt
exp = mp.exp
ln = mp.ln
log = mp.log
log10 = mp.log10
power = mp.power
cos = mp.cos
sin = mp.sin
tan = mp.tan
cosh = mp.cosh
sinh = mp.sinh
tanh = mp.tanh
acos = mp.acos
asin = mp.asin
atan = mp.atan
asinh = mp.asinh
acosh = mp.acosh
atanh = mp.atanh
sec = mp.sec
csc = mp.csc
cot = mp.cot
sech = mp.sech
csch = mp.csch
coth = mp.coth
asec = mp.asec
acsc = mp.acsc
acot = mp.acot
asech = mp.asech
acsch = mp.acsch
acoth = mp.acoth
cospi = mp.cospi
sinpi = mp.sinpi
sinc = mp.sinc
sincpi = mp.sincpi
cos_sin = mp.cos_sin
cospi_sinpi = mp.cospi_sinpi
fabs = mp.fabs
re = mp.re
im = mp.im
conj = mp.conj
floor = mp.floor
ceil = mp.ceil
nint = mp.nint
frac = mp.frac
root = mp.root
nthroot = mp.nthroot
hypot = mp.hypot
fmod = mp.fmod
ldexp = mp.ldexp
frexp = mp.frexp
sign = mp.sign
arg = mp.arg
phase = mp.phase
polar = mp.polar
rect = mp.rect
degrees = mp.degrees
radians = mp.radians
atan2 = mp.atan2
fib = mp.fib
fibonacci = mp.fibonacci
lambertw = mp.lambertw
zeta = mp.zeta
altzeta = mp.altzeta
gamma = mp.gamma
rgamma = mp.rgamma
factorial = mp.factorial
fac = mp.fac
fac2 = mp.fac2
beta = mp.beta
betainc = mp.betainc
psi = mp.psi
#psi0 = mp.psi0
#psi1 = mp.psi1
#psi2 = mp.psi2
#psi3 = mp.psi3
polygamma = mp.polygamma
digamma = mp.digamma
#trigamma = mp.trigamma
#tetragamma = mp.tetragamma
#pentagamma = mp.pentagamma
harmonic = mp.harmonic
bernoulli = mp.bernoulli
bernfrac = mp.bernfrac
stieltjes = mp.stieltjes
hurwitz = mp.hurwitz
dirichlet = mp.dirichlet
bernpoly = mp.bernpoly
eulerpoly = mp.eulerpoly
eulernum = mp.eulernum
polylog = mp.polylog
clsin = mp.clsin
clcos = mp.clcos
gammainc = mp.gammainc
gammaprod = mp.gammaprod
binomial = mp.binomial
rf = mp.rf
ff = mp.ff
hyper = mp.hyper
hyp0f1 = mp.hyp0f1
hyp1f1 = mp.hyp1f1
hyp1f2 = mp.hyp1f2
hyp2f1 = mp.hyp2f1
hyp2f2 = mp.hyp2f2
hyp2f0 = mp.hyp2f0
hyp2f3 = mp.hyp2f3
hyp3f2 = mp.hyp3f2
hyperu = mp.hyperu
hypercomb = mp.hypercomb
meijerg = mp.meijerg
appellf1 = mp.appellf1
appellf2 = mp.appellf2
appellf3 = mp.appellf3
appellf4 = mp.appellf4
hyper2d = mp.hyper2d
bihyper = mp.bihyper
erf = mp.erf
erfc = mp.erfc
erfi = mp.erfi
erfinv = mp.erfinv
npdf = mp.npdf
ncdf = mp.ncdf
expint = mp.expint
e1 = mp.e1
ei = mp.ei
li = mp.li
ci = mp.ci
si = mp.si
chi = mp.chi
shi = mp.shi
fresnels = mp.fresnels
fresnelc = mp.fresnelc
airyai = mp.airyai
airybi = mp.airybi
airyaizero = mp.airyaizero
airybizero = mp.airybizero
scorergi = mp.scorergi
scorerhi = mp.scorerhi
ellipk = mp.ellipk
ellipe = mp.ellipe
ellipf = mp.ellipf
ellippi = mp.ellippi
elliprc = mp.elliprc
elliprj = mp.elliprj
elliprf = mp.elliprf
elliprd = mp.elliprd
elliprg = mp.elliprg
agm = mp.agm
jacobi = mp.jacobi
chebyt = mp.chebyt
chebyu = mp.chebyu
legendre = mp.legendre
legenp = mp.legenp
legenq = mp.legenq
hermite = mp.hermite
pcfd = mp.pcfd
pcfu = mp.pcfu
pcfv = mp.pcfv
pcfw = mp.pcfw
gegenbauer = mp.gegenbauer
laguerre = mp.laguerre
spherharm = mp.spherharm
besselj = mp.besselj
j0 = mp.j0
j1 = mp.j1
besseli = mp.besseli
bessely = mp.bessely
besselk = mp.besselk
besseljzero = mp.besseljzero
besselyzero = mp.besselyzero
hankel1 = mp.hankel1
hankel2 = mp.hankel2
struveh = mp.struveh
struvel = mp.struvel
angerj = mp.angerj
webere = mp.webere
lommels1 = mp.lommels1
lommels2 = mp.lommels2
whitm = mp.whitm
whitw = mp.whitw
ber = mp.ber
bei = mp.bei
ker = mp.ker
kei = mp.kei
coulombc = mp.coulombc
coulombf = mp.coulombf
coulombg = mp.coulombg
lambertw = mp.lambertw
barnesg = mp.barnesg
superfac = mp.superfac
hyperfac = mp.hyperfac
loggamma = mp.loggamma
siegeltheta = mp.siegeltheta
siegelz = mp.siegelz
grampoint = mp.grampoint
zetazero = mp.zetazero
riemannr = mp.riemannr
primepi = mp.primepi
primepi2 = mp.primepi2
primezeta = mp.primezeta
bell = mp.bell
polyexp = mp.polyexp
expm1 = mp.expm1
powm1 = mp.powm1
unitroots = mp.unitroots
cyclotomic = mp.cyclotomic
mangoldt = mp.mangoldt
secondzeta = mp.secondzeta
nzeros = mp.nzeros
backlunds = mp.backlunds
lerchphi = mp.lerchphi
stirling1 = mp.stirling1
stirling2 = mp.stirling2

# be careful when changing this name, don't use test*!
def runtests():
    """
    Run all mpmath tests and print output.
    """
    import os.path
    from inspect import getsourcefile
    from .tests import runtests as tests
    testdir = os.path.dirname(os.path.abspath(getsourcefile(tests)))
    importdir = os.path.abspath(testdir + '/../..')
    tests.testit(importdir, testdir)

def doctests(filter=[]):
    try:
        import psyco; psyco.full()
    except ImportError:
        pass
    import sys
    from timeit import default_timer as clock
    for i, arg in enumerate(sys.argv):
        if '__init__.py' in arg:
            filter = [sn for sn in sys.argv[i+1:] if not sn.startswith("-")]
            break
    import doctest
    globs = globals().copy()
    for obj in globs: #sorted(globs.keys()):
        if filter:
            if not sum([pat in obj for pat in filter]):
                continue
        sys.stdout.write(str(obj) + " ")
        sys.stdout.flush()
        t1 = clock()
        doctest.run_docstring_examples(globs[obj], {}, verbose=("-v" in sys.argv))
        t2 = clock()
        print(round(t2-t1, 3))

if __name__ == '__main__':
    doctests()

<|endoftext|>


<commit_before>import numbers
import numpy as np

class MultiplicativeFunctional(object):
    """
    Class representing a multiplicative functional.

    Parameters
    ----------
    mc : MarkovChain
        MarkovChain instance with n states representing the `X` process.

    G : array_like(float, ndim=2)
        Growth rate matrix. Must be of shape n x n.

    M_inits : array_like(float, ndim=1) or scalar(float),
              optional(default=None)
        Array containing the initial values of the `M` process, one for
        each state. Must be of length n. If not specified, default to
        the vector of all ones. If it is a scalar, then it is converted
        to the constant vector of that scalar.

    Attributes
    ----------
    mc : MarkovChain
        See Parameters.

    G : ndarray(float, ndim=2)
        See Parameters.

    M_inits: ndarray(float, ndim=1)
        See Parameters.

    n : scalar(int)
        Number of the state.

    P : ndarray(float, ndim=2)
        Transition probability matrix of `mc`.

    M_matrix : ndarray(float, ndim=2)
        Generating matrix for the `M` process.

    P_tilde : ndarray(float, ndim=2)
        Matrix representation of the M-operator, given by
        `P * M_matrix`.

    exp_eta : scalar(float)
        Dominant eigenvalue of `P_tilde`.

    eta : scalar(float)
        Log of `exp_eta`.

    e : ndarray(float, ndim=1)
        Dominant eigenvector of `P_tilde`.

    M_tilde_matrix : ndarray(float, ndim=2)
        Generating matrix for the `M_tilde` process.

    """
    def __init__(self, mc, G, M_inits=None):
        self.mc = mc
        self.n = mc.n
        self.P = mc.P

        self.G = np.asarray(G)

        if self.G.ndim != 2 or self.G.shape[0] != self.G.shape[1]:
            raise ValueError('G must be a square matrix')

        if self.G.shape[0] != self.n:
            raise ValueError(
                'order of G must be equal to the number of states'
            )

        if M_inits is None:
            self.M_inits = np.ones(self.n)
        elif isinstance(M_inits, numbers.Number):
            self.M_inits = np.empty(self.n)
            self.M_inits.fill(M_inits)
        else:
            if len(M_inits) != self.n:
                raise ValueError(
                    'length of M_inits must be equal to the number of states'
                )
            self.M_inits = np.asarray(M_inits)

        self.M_matrix = np.exp(self.G)
        self.P_tilde = self.P * self.M_matrix

        self.exp_eta, self.e = _solve_principal_eig(self.P_tilde)
        self.eta = np.log(self.exp_eta)

        self.M_tilde_matrix = \
            self.M_matrix * self.e / self.e.reshape((self.n, 1)) / self.exp_eta

    def simulate(self, ts_length, X_init=None,
                 num_reps=None, random_state=None):
        """
        Simulate the `M` and `M_tilde` processes.

        Parameters
        ----------
        ts_length : scalar(int)
            Length of each simulation.

        X_init : scalar(int), optional(default=None)
            Initial state of the `X` process. If None, the initial state
            is randomly drawn.

        num_reps : scalar(int), optional(default=None)
            Number of repetitions of simulation.

        random_state : scalar(int) or np.random.RandomState,
                       optional(default=None)
            Random seed (integer) or np.random.RandomState instance to
            set the initial state of the random number generator for
            reproducibility. If None, a randomly initialized RandomState
            is used.

        Returns
        -------
        res: MFSimulateResult
            Simulation result represetned as a `MFSimulateResult`. See
            `MFSimulateResult` for details. The array for each attribute
            is of shape `(ts_length,)` if `num_reps=None`, or of shape
            `(num_reps, ts_length)` otherwise.

        """
        X = self.mc.simulate(ts_length, init=X_init,
                             num_reps=num_reps, random_state=random_state)
        return self.generate_paths(X)

    def generate_paths(self, X):
        """
        Given a simulation of the `X` process, generate sample paths of
        the `M` and `M_tilde` processes.

        Parameters
        ----------
        X : array_like(int)
            Array containing the sample path(s) of the `X` process.

        Returns
        -------
        res: MFSimulateResult
            Simulation result represetned as a `MFSimulateResult`. See
            `MFSimulateResult` for details. The array for each attribute
            is of the same shape as `X`.

        """
        X = np.asarray(X)
        M = _generate_mult_process(X, self.M_matrix, self.M_inits)
        M_tilde_inits = np.ones(self.n)
        M_tilde = _generate_mult_process(X, self.M_tilde_matrix, M_tilde_inits)
        res = MFSimulateResult(X=X,
                               M=M,
                               M_tilde=M_tilde)
        return res

def _generate_mult_process(X, mat, inits):
    """
    Return the array `M` given by `M[t+1]/M[t] = mat[X[t], X[t+1]]`
    with `M[0] = inits[X[0]]`.

    """
    M = np.empty_like(X, dtype=float)
    M[..., 0] = inits[X[..., 0]]
    M[..., 1:] = mat[X[..., :-1], X[..., 1:]]
    np.cumprod(M, axis=-1, out=M)
    return M

def _solve_principal_eig(a):
    """
    Solve the principal eigenvalue problem for a non-negative matrix
    `a`.

    """
    w, v = np.linalg.eig(a)
    idx = np.argmax(w)
    eig_val = w[idx]
    eig_vec = v[:, idx]

    # Let eig_vec non-negative
    sign = 0
    i = 0
    while sign == 0 and i < len(eig_vec):
        sign = np.sign(eig_vec[i])
        i += 1
    if sign < 0:
        eig_vec *= -1

    return eig_val, eig_vec

class _Result(dict):
    # This is sourced from sicpy.optimize.OptimizeResult.
    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            raise AttributeError(name)

    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

    def __repr__(self):
        if self.keys():
            m = max(map(len, list(self.keys()))) + 1
            return '\n'.join([k.rjust(m) + ': ' + repr(v)
                              for k, v in sorted(self.items())])
        else:
            return self.__class__.__name__ + "()"

    def __dir__(self):
        return self.keys()

class MFSimulateResult(_Result):
    """
    Contain the information about the simulation result for
    `MultiplicativeFunctional`.

    Attributes
    ----------
    M : ndarray(float, ndim=1 or 2)
        Array containing the sample path(s) of the `M` process.

    M_tilde : ndarray(float, ndim=1 or 2)
        Array containing the sample path(s) of the `M_tilde` process.

    X : ndarray(int, ndim=1 or 2)
        Array containing the sample path(s) of the `X` process.

    """
    pass
<commit_msg>Change numbers.Number to numbers.Real<commit_after>import numbers
import numpy as np

class MultiplicativeFunctional(object):
    """
    Class representing a multiplicative functional.

    Parameters
    ----------
    mc : MarkovChain
        MarkovChain instance with n states representing the `X` process.

    G : array_like(float, ndim=2)
        Growth rate matrix. Must be of shape n x n.

    M_inits : array_like(float, ndim=1) or scalar(float),
              optional(default=None)
        Array containing the initial values of the `M` process, one for
        each state. Must be of length n. If not specified, default to
        the vector of all ones. If it is a scalar, then it is converted
        to the constant vector of that scalar.

    Attributes
    ----------
    mc : MarkovChain
        See Parameters.

    G : ndarray(float, ndim=2)
        See Parameters.

    M_inits: ndarray(float, ndim=1)
        See Parameters.

    n : scalar(int)
        Number of the state.

    P : ndarray(float, ndim=2)
        Transition probability matrix of `mc`.

    M_matrix : ndarray(float, ndim=2)
        Generating matrix for the `M` process.

    P_tilde : ndarray(float, ndim=2)
        Matrix representation of the M-operator, given by
        `P * M_matrix`.

    exp_eta : scalar(float)
        Dominant eigenvalue of `P_tilde`.

    eta : scalar(float)
        Log of `exp_eta`.

    e : ndarray(float, ndim=1)
        Dominant eigenvector of `P_tilde`.

    M_tilde_matrix : ndarray(float, ndim=2)
        Generating matrix for the `M_tilde` process.

    """
    def __init__(self, mc, G, M_inits=None):
        self.mc = mc
        self.n = mc.n
        self.P = mc.P

        self.G = np.asarray(G)

        if self.G.ndim != 2 or self.G.shape[0] != self.G.shape[1]:
            raise ValueError('G must be a square matrix')

        if self.G.shape[0] != self.n:
            raise ValueError(
                'order of G must be equal to the number of states'
            )

        if M_inits is None:
            self.M_inits = np.ones(self.n)
        elif isinstance(M_inits, numbers.Real):
            self.M_inits = np.empty(self.n)
            self.M_inits.fill(M_inits)
        else:
            if len(M_inits) != self.n:
                raise ValueError(
                    'length of M_inits must be equal to the number of states'
                )
            self.M_inits = np.asarray(M_inits)

        self.M_matrix = np.exp(self.G)
        self.P_tilde = self.P * self.M_matrix

        self.exp_eta, self.e = _solve_principal_eig(self.P_tilde)
        self.eta = np.log(self.exp_eta)

        self.M_tilde_matrix = \
            self.M_matrix * self.e / self.e.reshape((self.n, 1)) / self.exp_eta

    def simulate(self, ts_length, X_init=None,
                 num_reps=None, random_state=None):
        """
        Simulate the `M` and `M_tilde` processes.

        Parameters
        ----------
        ts_length : scalar(int)
            Length of each simulation.

        X_init : scalar(int), optional(default=None)
            Initial state of the `X` process. If None, the initial state
            is randomly drawn.

        num_reps : scalar(int), optional(default=None)
            Number of repetitions of simulation.

        random_state : scalar(int) or np.random.RandomState,
                       optional(default=None)
            Random seed (integer) or np.random.RandomState instance to
            set the initial state of the random number generator for
            reproducibility. If None, a randomly initialized RandomState
            is used.

        Returns
        -------
        res: MFSimulateResult
            Simulation result represetned as a `MFSimulateResult`. See
            `MFSimulateResult` for details. The array for each attribute
            is of shape `(ts_length,)` if `num_reps=None`, or of shape
            `(num_reps, ts_length)` otherwise.

        """
        X = self.mc.simulate(ts_length, init=X_init,
                             num_reps=num_reps, random_state=random_state)
        return self.generate_paths(X)

    def generate_paths(self, X):
        """
        Given a simulation of the `X` process, generate sample paths of
        the `M` and `M_tilde` processes.

        Parameters
        ----------
        X : array_like(int)
            Array containing the sample path(s) of the `X` process.

        Returns
        -------
        res: MFSimulateResult
            Simulation result represetned as a `MFSimulateResult`. See
            `MFSimulateResult` for details. The array for each attribute
            is of the same shape as `X`.

        """
        X = np.asarray(X)
        M = _generate_mult_process(X, self.M_matrix, self.M_inits)
        M_tilde_inits = np.ones(self.n)
        M_tilde = _generate_mult_process(X, self.M_tilde_matrix, M_tilde_inits)
        res = MFSimulateResult(X=X,
                               M=M,
                               M_tilde=M_tilde)
        return res

def _generate_mult_process(X, mat, inits):
    """
    Return the array `M` given by `M[t+1]/M[t] = mat[X[t], X[t+1]]`
    with `M[0] = inits[X[0]]`.

    """
    M = np.empty_like(X, dtype=float)
    M[..., 0] = inits[X[..., 0]]
    M[..., 1:] = mat[X[..., :-1], X[..., 1:]]
    np.cumprod(M, axis=-1, out=M)
    return M

def _solve_principal_eig(a):
    """
    Solve the principal eigenvalue problem for a non-negative matrix
    `a`.

    """
    w, v = np.linalg.eig(a)
    idx = np.argmax(w)
    eig_val = w[idx]
    eig_vec = v[:, idx]

    # Let eig_vec non-negative
    sign = 0
    i = 0
    while sign == 0 and i < len(eig_vec):
        sign = np.sign(eig_vec[i])
        i += 1
    if sign < 0:
        eig_vec *= -1

    return eig_val, eig_vec

class _Result(dict):
    # This is sourced from sicpy.optimize.OptimizeResult.
    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            raise AttributeError(name)

    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

    def __repr__(self):
        if self.keys():
            m = max(map(len, list(self.keys()))) + 1
            return '\n'.join([k.rjust(m) + ': ' + repr(v)
                              for k, v in sorted(self.items())])
        else:
            return self.__class__.__name__ + "()"

    def __dir__(self):
        return self.keys()

class MFSimulateResult(_Result):
    """
    Contain the information about the simulation result for
    `MultiplicativeFunctional`.

    Attributes
    ----------
    M : ndarray(float, ndim=1 or 2)
        Array containing the sample path(s) of the `M` process.

    M_tilde : ndarray(float, ndim=1 or 2)
        Array containing the sample path(s) of the `M_tilde` process.

    X : ndarray(int, ndim=1 or 2)
        Array containing the sample path(s) of the `X` process.

    """
    pass
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import atexit
import os
import sys
from datetime import datetime

from yalix.exceptions import YalixError
from yalix.completer import Completer
from yalix.interpreter.primitives import *
from yalix.interpreter.builtins import *
from yalix.parser import parse
from yalix.utils import log_progress, log
from yalix.utils.color import red, green, blue, bold
from yalix.globals import create_initial_env

def version():
    return '0.0.1'

def copyright():
    return Atom("""
Copyright (c) {0} Richard Hull.
All Rights Reserved.""".format(datetime.now().year))

def license():
    return Atom("""
The MIT License (MIT)

Copyright (c) {0} Richard Hull

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.""".format(datetime.now().year))

def help():
    return Atom("""
Yalix is a LISP interpreter: it's dialect most closely resembles that of Racket
or PLT-Scheme. This particular implementation is written in Python, and serves
as a reference implementation only. It is subject to on-going development, and
as such will suffer breaking changes, as well as being buggy and not having
been optimised. It is therefore not recommended for production use.

The main purpose of this software is to provide a pedagogical experience in
writing a LISP interpreter and a core library of functions, in a number of
different computer languages. The intention is that there will eventually be
"Seven LISPs in seven languages", where each implementation is idiomatic in
the target language. Although not fully decided, the languages will most
likely be: Python, Lua, Julia, Java/Scala, Haskell, a.n.other, and course
there really needs to be a Yalix implementation too!

The REPL that you are now connected to has readline capabilities, and stores
a history in ~/.yalix-history. Pressing <TAB> on a partially typed word will
interrogate the environment for matching definitions and will present a word
completion facility.

Documentation for a particular function (e.g. first) can be found by evaluating
the following S-Exp at the prompt:

    (doc first)

See https://github.com/rm-hull/yalex/ for further information.""")

def credits():
    return Atom("""
TBD""")

def init_readline(env):
    try:
        import readline
    except ImportError:
        print "Module readline not available"
    else:
        histfile = os.path.join(os.path.expanduser("~"), ".yalix_history")
        readline.set_completer(Completer(env).complete)
        readline.parse_and_bind("tab: complete")
        if hasattr(readline, "read_history_file"):
            try:
                with log_progress("Reading history"):
                    readline.read_history_file(histfile)

            except IOError:
                pass
            atexit.register(readline.write_history_file, histfile)

def balance(text, bal=0):
    """
    Checks whether the parens in the text are balanced:
        - zero: balanced
        - negative: too many right parens
        - positive: too many left parens
    """
    if text == '':
        return bal
    elif text[0] == '(' and bal >= 0:
        return balance(text[1:], bal + 1)
    elif text[0] == ')':
        return balance(text[1:], bal - 1)
    else:
        return balance(text[1:], bal)

def read(count, primary_prompt):
    prompt = primary_prompt.format(count)
    secondary_prompt = ' ' * len(str(count)) + green('  ...: ')

    entry = ''
    while True:
        entry += raw_input(prompt) + '\n'
        if balance(entry) == 0:
            return entry
        prompt = secondary_prompt

def prn(input, result, count, prompt):
    print prompt.format(count, result)

def ready():
    log()
    log(bold('Yalix [{0}]') + ' on Python {1} {2}', version(), sys.version, sys.platform)
    log('Type "help", "copyright", "credits" or "license" for more information.')

def repl(print_callback=prn):
    env = create_initial_env()
    env['copyright'] = copyright()
    env['license'] = license()
    env['help'] = help()
    env['credits'] = credits()

    init_readline(env)
    ready()

    in_prompt = green('In [') + green(bold('{0}')) + green(']: ')
    out_prompt = red('Out[') + red(bold('{0}')) + red(']: ') + '{1}\n'

    count = 1
    while True:
        try:
            text = read(count, in_prompt)
            for ast in parse(text):
                result = ast.eval(env)
                print_callback(text, result, count, out_prompt)

        except EOFError:
            log(bold(blue('\nBye!')))
            break

        except KeyboardInterrupt:
            log(bold(red('\nKeyboardInterrupt')))

        except YalixError as ex:
            log("{0}: {1}", bold(red(repr(type(ex)))), ex)
        count += 1

env = create_initial_env()

lst1 = Cons(Atom(4), Cons(Atom(2), Cons(Atom(3), Nil())))
lst2 = List(Atom(4), Atom(2), Atom(3))

# Eq(Cons(Atom(4), Cons(Atom(2), Cons(Atom(3), Nil()))), lst1).eval(env)
# Eq(lst2, lst1).eval(env)
#
# Eq(Atom(True),
#   Not(Atom(False))).eval(env)

Nil_QUESTION(Nil()).eval(env)
Nil_QUESTION(Atom('Freddy')).eval(env)

Atom_QUESTION(Atom('Freddy')).eval(env)
Atom_QUESTION(Nil()).eval(env)

# Eq(Nil(), Atom(None)).eval(env)

# Car(Cdr(lst1)).eval(env)

(Cdr(Nil())).eval(env)

Let("f",
    Atom("Hello"),
    Cons(Symbol("f"),
         Symbol("f"))).eval(env)

Let_STAR([('a', Atom('Hello')),
          ('b', List(Atom(1), Atom(2), Atom(3))),
          ('c', Atom('World')),
          ('c', List(Atom('Big'), Symbol('c')))],  # <-- re-def shadowing
         List(Symbol('a'), Symbol('c'), Symbol('b'))).eval(env)

Let('identity',
    Lambda(['x'], Symbol('x')),  # <-- anonymous fn
    Call(Symbol('identity'), Atom(99))).eval(env)

# InterOp, i.e. using Python functions
import operator
InterOp(operator.add, Atom(41), Atom(23)).eval(env)

# check unicode, pi & golden ratio
Define('π', Atom(3.14159265358979323846264338327950288419716939937510)).eval(env)
Define('ϕ', Atom(1.618033988749894848204586834)).eval(env)
env['π']
env['ϕ']

Symbol('π').eval(env)
Symbol('ϕ').eval(env)

# from __future__ import print_function
# Define('print', Lambda(['text'], InterOp(print_function, Symbol('text')))).eval(env)

Call(Symbol('+'), Atom(99), Atom(55)).eval(env)
Call([Symbol('+'), Atom(99), Atom(55)]).eval(env)
Call(array_to_linked_list([Symbol('+'), Atom(99), Atom(55)])).eval(env)
array_to_linked_list([Symbol('+'), Atom(99), Atom(55)])

env['+']

Call(Symbol('random')).eval(env)

q = Quote([Symbol('+'), Atom(2), Atom(3)]).eval(env)
q
Call(q).eval(env)

# Call(Quote([Symbol('+'), Atom(2), Atom(3)])).eval(env)

# (let (rnd (random))
#   (if (< rnd 0.5)
#     "Unlucky"
#     (if (< rnd 0.75)
#       "Close, but no cigar"
#       "Lucky")))
#
Let('rnd',
    Call(Symbol('random')),
    If(Call(Symbol('<'), Symbol('rnd'), Atom(0.5)),
       Atom("Unlucky"),
       If(Call(Symbol('<'), Symbol('rnd'), Atom(0.75)),
          Atom("Close, but no cigar"),
          Atom("Lucky")))).eval(env)

# (define factorial
#   (lambda (x)
#     (if (zero? x)
#       1
#       (* x (factorial (- x 1))))))
#
Define('zero?', Lambda(['n'], Call(Symbol('=='), Symbol('n'), Atom(0)))).eval(env)
Define('factorial', Lambda(['x'],
                           If(Call(Symbol('zero?'), Symbol('x')),
                              Atom(1),
                              Call(Symbol('*'),
                                   Symbol('x'),
                                   Call(Symbol('factorial'),
                                        Call(Symbol('-'),
                                             Symbol('x'),
                                             Atom(1))))))).eval(env)

# (factorial 10)
Call(Symbol('factorial'), Atom(10)).eval(env)

# (format "{0} PI = {1}" (list 2 (* 2 math/pi)))
Call(Symbol('format'),
     Atom('{0} PI = {1}'),
     List(Atom(2),
          Call(Symbol('*'),
               Atom(2),
               Symbol('math/pi')))).eval(env)

# (gensym)
g = Call(Symbol('gensym')).eval(env)
g
g.name

# =============================================================================================

test2 = """
(define *hello* ["world" -1 #t 3.14159 [1 2 3]])

(let (rnd (random))
  (if (< rnd 0.5)
    "Unlucky"
    (if (< rnd 0.75)
      "Close, but no cigar"
      "Lucky")))

(define factorial
  (lambda (x)
    (if (zero? x)
      1
      (* x (factorial (- x 1))))))

(factorial 10)

*hello*

"""

# No yet fully supported
# (let* ((x 5)
#        (y (+ x 7))
#        (z (+ y x 2)))
#    (* x y z))

for ptree in parse(test2):
    print ptree.eval(env)
<commit_msg>Auto-indent feature<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import atexit
import os
import sys
from datetime import datetime

from yalix.exceptions import YalixError
from yalix.completer import Completer
from yalix.interpreter.primitives import *
from yalix.interpreter.builtins import *
from yalix.parser import parse
from yalix.utils import log_progress, log
from yalix.utils.color import red, green, blue, bold
from yalix.globals import create_initial_env

def version():
    return '0.0.1'

def copyright():
    return Atom("""
Copyright (c) {0} Richard Hull.
All Rights Reserved.""".format(datetime.now().year))

def license():
    return Atom("""
The MIT License (MIT)

Copyright (c) {0} Richard Hull

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.""".format(datetime.now().year))

def help():
    return Atom("""
Yalix is a LISP interpreter: it's dialect most closely resembles that of Racket
or PLT-Scheme. This particular implementation is written in Python, and serves
as a reference implementation only. It is subject to on-going development, and
as such will suffer breaking changes, as well as being buggy and not having
been optimised. It is therefore not recommended for production use.

The main purpose of this software is to provide a pedagogical experience in
writing a LISP interpreter and a core library of functions, in a number of
different computer languages. The intention is that there will eventually be
"Seven LISPs in seven languages", where each implementation is idiomatic in
the target language. Although not fully decided, the languages will most
likely be: Python, Lua, Julia, Java/Scala, Haskell, a.n.other, and course
there really needs to be a Yalix implementation too!

The REPL that you are now connected to has readline capabilities, and stores
a history in ~/.yalix-history. Pressing <TAB> on a partially typed word will
interrogate the environment for matching definitions and will present a word
completion facility.

Documentation for a particular function (e.g. first) can be found by evaluating
the following S-Exp at the prompt:

    (doc first)

See https://github.com/rm-hull/yalex/ for further information.""")

def credits():
    return Atom("""
TBD""")

def init_readline(env):
    try:
        import readline
    except ImportError:
        print "Module readline not available"
    else:
        histfile = os.path.join(os.path.expanduser("~"), ".yalix_history")
        readline.set_completer(Completer(env).complete)
        readline.parse_and_bind("tab: complete")
        if hasattr(readline, "read_history_file"):
            try:
                with log_progress("Reading history"):
                    readline.read_history_file(histfile)

            except IOError:
                pass
            atexit.register(readline.write_history_file, histfile)

def balance(text, bal=0):
    """
    Checks whether the parens in the text are balanced:
        - zero: balanced
        - negative: too many right parens
        - positive: too many left parens
    """
    if text == '':
        return bal
    elif text[0] == '(' and bal >= 0:
        return balance(text[1:], bal + 1)
    elif text[0] == ')':
        return balance(text[1:], bal - 1)
    else:
        return balance(text[1:], bal)

def read(count, primary_prompt):
    prompt = primary_prompt.format(count)
    secondary_prompt = ' ' * len(str(count)) + green('  ...: ')

    entry = ''
    while True:
        entry += raw_input(prompt) + '\n'
        if balance(entry) == 0:
            return entry
        prompt = secondary_prompt + '  ' * balance(entry)

def prn(input, result, count, prompt):
    print prompt.format(count, result)

def ready():
    log()
    log(bold('Yalix [{0}]') + ' on Python {1} {2}', version(), sys.version, sys.platform)
    log('Type "help", "copyright", "credits" or "license" for more information.')

def repl(print_callback=prn):
    env = create_initial_env()
    env['copyright'] = copyright()
    env['license'] = license()
    env['help'] = help()
    env['credits'] = credits()

    init_readline(env)
    ready()

    in_prompt = green('In [') + green(bold('{0}')) + green(']: ')
    out_prompt = red('Out[') + red(bold('{0}')) + red(']: ') + '{1}\n'

    count = 1
    while True:
        try:
            text = read(count, in_prompt)
            for ast in parse(text):
                result = ast.eval(env)
                print_callback(text, result, count, out_prompt)

        except EOFError:
            log(bold(blue('\nBye!')))
            break

        except KeyboardInterrupt:
            log(bold(red('\nKeyboardInterrupt')))

        except YalixError as ex:
            log("{0}: {1}", bold(red(repr(type(ex)))), ex)
        count += 1

env = create_initial_env()

lst1 = Cons(Atom(4), Cons(Atom(2), Cons(Atom(3), Nil())))
lst2 = List(Atom(4), Atom(2), Atom(3))

# Eq(Cons(Atom(4), Cons(Atom(2), Cons(Atom(3), Nil()))), lst1).eval(env)
# Eq(lst2, lst1).eval(env)
#
# Eq(Atom(True),
#   Not(Atom(False))).eval(env)

Nil_QUESTION(Nil()).eval(env)
Nil_QUESTION(Atom('Freddy')).eval(env)

Atom_QUESTION(Atom('Freddy')).eval(env)
Atom_QUESTION(Nil()).eval(env)

# Eq(Nil(), Atom(None)).eval(env)

# Car(Cdr(lst1)).eval(env)

(Cdr(Nil())).eval(env)

Let("f",
    Atom("Hello"),
    Cons(Symbol("f"),
         Symbol("f"))).eval(env)

Let_STAR([('a', Atom('Hello')),
          ('b', List(Atom(1), Atom(2), Atom(3))),
          ('c', Atom('World')),
          ('c', List(Atom('Big'), Symbol('c')))],  # <-- re-def shadowing
         List(Symbol('a'), Symbol('c'), Symbol('b'))).eval(env)

Let('identity',
    Lambda(['x'], Symbol('x')),  # <-- anonymous fn
    Call(Symbol('identity'), Atom(99))).eval(env)

# InterOp, i.e. using Python functions
import operator
InterOp(operator.add, Atom(41), Atom(23)).eval(env)

# check unicode, pi & golden ratio
Define('π', Atom(3.14159265358979323846264338327950288419716939937510)).eval(env)
Define('ϕ', Atom(1.618033988749894848204586834)).eval(env)
env['π']
env['ϕ']

Symbol('π').eval(env)
Symbol('ϕ').eval(env)

# from __future__ import print_function
# Define('print', Lambda(['text'], InterOp(print_function, Symbol('text')))).eval(env)

Call(Symbol('+'), Atom(99), Atom(55)).eval(env)
Call([Symbol('+'), Atom(99), Atom(55)]).eval(env)
Call(array_to_linked_list([Symbol('+'), Atom(99), Atom(55)])).eval(env)
array_to_linked_list([Symbol('+'), Atom(99), Atom(55)])

env['+']

Call(Symbol('random')).eval(env)

q = Quote([Symbol('+'), Atom(2), Atom(3)]).eval(env)
q
Call(q).eval(env)

# Call(Quote([Symbol('+'), Atom(2), Atom(3)])).eval(env)

# (let (rnd (random))
#   (if (< rnd 0.5)
#     "Unlucky"
#     (if (< rnd 0.75)
#       "Close, but no cigar"
#       "Lucky")))
#
Let('rnd',
    Call(Symbol('random')),
    If(Call(Symbol('<'), Symbol('rnd'), Atom(0.5)),
       Atom("Unlucky"),
       If(Call(Symbol('<'), Symbol('rnd'), Atom(0.75)),
          Atom("Close, but no cigar"),
          Atom("Lucky")))).eval(env)

# (define factorial
#   (lambda (x)
#     (if (zero? x)
#       1
#       (* x (factorial (- x 1))))))
#
Define('zero?', Lambda(['n'], Call(Symbol('='), Symbol('n'), Atom(0)))).eval(env)
Define('factorial', Lambda(['x'],
                           If(Call(Symbol('zero?'), Symbol('x')),
                              Atom(1),
                              Call(Symbol('*'),
                                   Symbol('x'),
                                   Call(Symbol('factorial'),
                                        Call(Symbol('-'),
                                             Symbol('x'),
                                             Atom(1))))))).eval(env)

# (factorial 10)
Call(Symbol('factorial'), Atom(10)).eval(env)

# (format "{0} PI = {1}" (list 2 (* 2 math/pi)))
Call(Symbol('format'),
     Atom('{0} PI = {1}'),
     List(Atom(2),
          Call(Symbol('*'),
               Atom(2),
               Symbol('math/pi')))).eval(env)

# (gensym)
g = Call(Symbol('gensym')).eval(env)
g
g.name

# =============================================================================================

test2 = """
(define *hello* ["world" -1 #t 3.14159 [1 2 3]])

(let (rnd (random))
  (if (< rnd 0.5)
    "Unlucky"
    (if (< rnd 0.75)
      "Close, but no cigar"
      "Lucky")))

(define factorial
  (lambda (x)
    (if (zero? x)
      1
      (* x (factorial (- x 1))))))

(factorial 10)

*hello*

"""

# No yet fully supported
# (let* ((x 5)
#        (y (+ x 7))
#        (z (+ y x 2)))
#    (* x y z))

for ptree in parse(test2):
    print ptree.eval(env)
<|endoftext|>


<commit_before># Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import csv
import logging
import operator
import os
import platform
import re
import shutil
import tempfile
import urllib2
import zipfile

from telemetry import decorators
from telemetry.core import util
from telemetry.core.platform import platform_backend
from telemetry.core.platform import power_monitor
from telemetry.util import cloud_storage
from telemetry.util import path
from telemetry.util import statistics

try:
  import win32con  # pylint: disable=F0401
  import win32event  # pylint: disable=F0401
  import win32process  # pylint: disable=F0401
except ImportError:
  win32con = None
  win32event = None
  win32process = None

class IppetError(Exception):
  pass

@decorators.Cache
def IppetPath():
  # Look for pre-installed IPPET.
  ippet_path = path.FindInstalledWindowsApplication(os.path.join(
    'Intel', 'Intel(R) Platform Power Estimation Tool', 'ippet.exe'))
  if ippet_path:
    return ippet_path

  # Look for IPPET installed previously by this script.
  ippet_path = os.path.join(
      path.GetTelemetryDir(), 'bin', 'win', 'ippet', 'ippet.exe')
  if path.IsExecutable(ippet_path):
    return ippet_path

  # Install IPPET.
  zip_path = os.path.join(path.GetTelemetryDir(), 'bin', 'win', 'ippet.zip')
  cloud_storage.GetIfChanged(zip_path, bucket=cloud_storage.PUBLIC_BUCKET)
  with zipfile.ZipFile(zip_path, 'r') as zip_file:
    zip_file.extractall(os.path.dirname(zip_path))
  os.remove(zip_path)

  if path.IsExecutable(ippet_path):
    return ippet_path

  return None

class IppetPowerMonitor(power_monitor.PowerMonitor):
  def __init__(self, backend):
    super(IppetPowerMonitor, self).__init__()
    self._backend = backend
    self._ippet_handle = None
    self._ippet_port = None
    self._output_dir = None

  def CanMonitorPower(self):
    if not win32event:
      return False

    windows_7_or_later = (
        self._backend.GetOSName() == 'win' and
        self._backend.GetOSVersionName() >= platform_backend.WIN7)
    if not windows_7_or_later:
      return False

    # This check works on Windows only.
    family, model = map(int, re.match('.+ Family ([0-9]+) Model ([0-9]+)',
                        platform.processor()).groups())
    # Model numbers from:
    # https://software.intel.com/en-us/articles/intel-architecture-and- \
    # processor-identification-with-cpuid-model-and-family-numbers
    # http://www.speedtraq.com
    sandy_bridge_or_later = ('Intel' in platform.processor() and family == 6 and
                             (model in (0x2A, 0x2D) or model >= 0x30))
    if not sandy_bridge_or_later:
      return False

    if not IppetPath():
      return False

    return True

  def CanMeasurePerApplicationPower(self):
    return self.CanMonitorPower()

  def StartMonitoringPower(self, browser):
    assert not self._ippet_handle, 'Called StartMonitoringPower() twice.'
    self._output_dir = tempfile.mkdtemp()
    self._ippet_port = util.GetUnreservedAvailableLocalPort()
    parameters = ['-log_dir', self._output_dir,
                  '-web_port', str(self._ippet_port),
                  '-zip', 'n']
    self._ippet_handle = self._backend.LaunchApplication(
        IppetPath(), parameters, elevate_privilege=True)

    def IppetServerIsUp():
      try:
        urllib2.urlopen('http://127.0.0.1:%d/ippet' % self._ippet_port)
      except urllib2.URLError:
        return False
      return True
    util.WaitFor(IppetServerIsUp, timeout=5)

  def StopMonitoringPower(self):
    assert self._ippet_handle, (
        'Called StopMonitoringPower() before StartMonitoringPower().')
    # Stop IPPET.
    ippet_quit_url = 'http://127.0.0.1:%d/ippet?cmd=quit' % self._ippet_port
    quit_output = urllib2.urlopen(ippet_quit_url).read()
    if quit_output != 'quiting\r\n':
      raise IppetError('Failed to quit IPPET: %s' % quit_output.strip())
    wait_return_code = win32event.WaitForSingleObject(self._ippet_handle, 20000)
    if wait_return_code != win32event.WAIT_OBJECT_0:
      if wait_return_code == win32event.WAIT_TIMEOUT:
        raise IppetError('Timed out waiting for IPPET to close.')
      else:
        raise IppetError('Error code %d while waiting for IPPET to close.' %
                        wait_return_code)
    ippet_exit_code = win32process.GetExitCodeProcess(self._ippet_handle)
    if ippet_exit_code == win32con.STILL_ACTIVE:
      raise IppetError('IPPET is still running but should have stopped.')
    elif ippet_exit_code != 0:
      raise IppetError('IPPET closed with exit code %d.' % ippet_exit_code)
    self._ippet_handle.Close()
    self._ippet_handle = None
    self._ippet_port = None

    # Read IPPET's log file.
    log_file = os.path.join(self._output_dir, 'ippet_log_processes.xls')
    try:
      with open(log_file, 'r') as f:
        reader = csv.DictReader(f, dialect='excel-tab')
        data = list(reader)[1:]  # The first iteration only reports temperature.
    except IOError:
      logging.error('Output directory %s contains: %s',
                    self._output_dir, os.listdir(self._output_dir))
      raise
    shutil.rmtree(self._output_dir)
    self._output_dir = None

    def get(*args, **kwargs):
      """Pull all iterations of a field from the IPPET data as a list.

      Args:
        args: A list representing the field name.
        mult: A cosntant to multiply the field's value by, for unit conversions.
        default: The default value if the field is not found in the iteration.

      Returns:
        A list containing the field's value across all iterations.
      """
      key = '\\\\.\\' + '\\'.join(args)
      def value(line):
        if key in line:
          return line[key]
        elif 'default' in kwargs:
          return kwargs['default']
        else:
          raise KeyError('Key "%s" not found in data and '
                         'no default was given.' % key)
      return [float(value(line)) * kwargs.get('mult', 1) for line in data]

    result = {
        'identifier': 'ippet',
        'power_samples_mw': get('Power(_Total)', 'Package W', mult=1000),
        'energy_consumption_mwh':
            sum(map(operator.mul,
                    get('Power(_Total)', 'Package W', mult=1000),
                    get('sys', 'Interval(secs)', mult=1./3600.))),
        'component_utilization': {
            'whole_package': {
                'average_temperature_c':
                    statistics.ArithmeticMean(get(
                        'Temperature(Package)', 'Current C')),
            },
            'cpu': {
                'power_samples_mw': get('Power(_Total)', 'CPU W', mult=1000),
                'energy_consumption_mwh':
                    sum(map(operator.mul,
                            get('Power(_Total)', 'CPU W', mult=1000),
                            get('sys', 'Interval(secs)', mult=1./3600.))),
            },
            'disk': {
                'power_samples_mw': get('Power(_Total)', 'Disk W', mult=1000),
                'energy_consumption_mwh':
                    sum(map(operator.mul,
                            get('Power(_Total)', 'Disk W', mult=1000),
                            get('sys', 'Interval(secs)', mult=1./3600.))),
            },
            'gpu': {
                'power_samples_mw': get('Power(_Total)', 'GPU W', mult=1000),
                'energy_consumption_mwh':
                    sum(map(operator.mul,
                            get('Power(_Total)', 'GPU W', mult=1000),
                            get('sys', 'Interval(secs)', mult=1./3600.))),
            },
        },
    }

    # Find Chrome processes in data. Note that this won't work if there are
    # extra Chrome processes lying around.
    chrome_keys = set()
    for iteration in data:
      for key in iteration.iterkeys():
        parts = key.split('\\')
        if (len(parts) >= 4 and
            re.match(r'Process\(Google Chrome [0-9]+\)', parts[3])):
          chrome_keys.add(parts[3])
    # Add Chrome process power usage to result.
    # Note that this is only an estimate of Chrome's CPU power usage.
    if chrome_keys:
      per_process_power_usage = [
          get(key, 'CPU Power W', default=0, mult=1000) for key in chrome_keys]
      result['application_energy_consumption_mwh'] = (
          sum(map(operator.mul,
                  map(sum, zip(*per_process_power_usage)),
                  get('sys', 'Interval(secs)', mult=1./3600.))))

    return result
<commit_msg>[telemetry] Disable IPPET power monitor<commit_after># Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import csv
import logging
import operator
import os
import platform
import re
import shutil
import tempfile
import urllib2
import zipfile

from telemetry import decorators
from telemetry.core import util
from telemetry.core.platform import platform_backend
from telemetry.core.platform import power_monitor
from telemetry.util import cloud_storage
from telemetry.util import path
from telemetry.util import statistics

try:
  import win32con  # pylint: disable=F0401
  import win32event  # pylint: disable=F0401
  import win32process  # pylint: disable=F0401
except ImportError:
  win32con = None
  win32event = None
  win32process = None

class IppetError(Exception):
  pass

@decorators.Cache
def IppetPath():
  # Look for pre-installed IPPET.
  ippet_path = path.FindInstalledWindowsApplication(os.path.join(
    'Intel', 'Intel(R) Platform Power Estimation Tool', 'ippet.exe'))
  if ippet_path:
    return ippet_path

  # Look for IPPET installed previously by this script.
  ippet_path = os.path.join(
      path.GetTelemetryDir(), 'bin', 'win', 'ippet', 'ippet.exe')
  if path.IsExecutable(ippet_path):
    return ippet_path

  # Install IPPET.
  zip_path = os.path.join(path.GetTelemetryDir(), 'bin', 'win', 'ippet.zip')
  cloud_storage.GetIfChanged(zip_path, bucket=cloud_storage.PUBLIC_BUCKET)
  with zipfile.ZipFile(zip_path, 'r') as zip_file:
    zip_file.extractall(os.path.dirname(zip_path))
  os.remove(zip_path)

  if path.IsExecutable(ippet_path):
    return ippet_path

  return None

class IppetPowerMonitor(power_monitor.PowerMonitor):
  def __init__(self, backend):
    super(IppetPowerMonitor, self).__init__()
    self._backend = backend
    self._ippet_handle = None
    self._ippet_port = None
    self._output_dir = None

  def CanMonitorPower(self):
    # IPPET disabled because of flakiness (crbug.com/336558).
    if True:
      return False

    if not win32event:
      return False

    windows_7_or_later = (
        self._backend.GetOSName() == 'win' and
        self._backend.GetOSVersionName() >= platform_backend.WIN7)
    if not windows_7_or_later:
      return False

    # This check works on Windows only.
    family, model = map(int, re.match('.+ Family ([0-9]+) Model ([0-9]+)',
                        platform.processor()).groups())
    # Model numbers from:
    # https://software.intel.com/en-us/articles/intel-architecture-and- \
    # processor-identification-with-cpuid-model-and-family-numbers
    # http://www.speedtraq.com
    sandy_bridge_or_later = ('Intel' in platform.processor() and family == 6 and
                             (model in (0x2A, 0x2D) or model >= 0x30))
    if not sandy_bridge_or_later:
      return False

    if not IppetPath():
      return False

    return True

  def CanMeasurePerApplicationPower(self):
    return self.CanMonitorPower()

  def StartMonitoringPower(self, browser):
    assert not self._ippet_handle, 'Called StartMonitoringPower() twice.'
    self._output_dir = tempfile.mkdtemp()
    self._ippet_port = util.GetUnreservedAvailableLocalPort()
    parameters = ['-log_dir', self._output_dir,
                  '-web_port', str(self._ippet_port),
                  '-zip', 'n']
    self._ippet_handle = self._backend.LaunchApplication(
        IppetPath(), parameters, elevate_privilege=True)

    def IppetServerIsUp():
      try:
        urllib2.urlopen('http://127.0.0.1:%d/ippet' % self._ippet_port)
      except urllib2.URLError:
        return False
      return True
    util.WaitFor(IppetServerIsUp, timeout=5)

  def StopMonitoringPower(self):
    assert self._ippet_handle, (
        'Called StopMonitoringPower() before StartMonitoringPower().')
    # Stop IPPET.
    ippet_quit_url = 'http://127.0.0.1:%d/ippet?cmd=quit' % self._ippet_port
    quit_output = urllib2.urlopen(ippet_quit_url).read()
    if quit_output != 'quiting\r\n':
      raise IppetError('Failed to quit IPPET: %s' % quit_output.strip())
    wait_return_code = win32event.WaitForSingleObject(self._ippet_handle, 20000)
    if wait_return_code != win32event.WAIT_OBJECT_0:
      if wait_return_code == win32event.WAIT_TIMEOUT:
        raise IppetError('Timed out waiting for IPPET to close.')
      else:
        raise IppetError('Error code %d while waiting for IPPET to close.' %
                        wait_return_code)
    ippet_exit_code = win32process.GetExitCodeProcess(self._ippet_handle)
    if ippet_exit_code == win32con.STILL_ACTIVE:
      raise IppetError('IPPET is still running but should have stopped.')
    elif ippet_exit_code != 0:
      raise IppetError('IPPET closed with exit code %d.' % ippet_exit_code)
    self._ippet_handle.Close()
    self._ippet_handle = None
    self._ippet_port = None

    # Read IPPET's log file.
    log_file = os.path.join(self._output_dir, 'ippet_log_processes.xls')
    try:
      with open(log_file, 'r') as f:
        reader = csv.DictReader(f, dialect='excel-tab')
        data = list(reader)[1:]  # The first iteration only reports temperature.
    except IOError:
      logging.error('Output directory %s contains: %s',
                    self._output_dir, os.listdir(self._output_dir))
      raise
    shutil.rmtree(self._output_dir)
    self._output_dir = None

    def get(*args, **kwargs):
      """Pull all iterations of a field from the IPPET data as a list.

      Args:
        args: A list representing the field name.
        mult: A cosntant to multiply the field's value by, for unit conversions.
        default: The default value if the field is not found in the iteration.

      Returns:
        A list containing the field's value across all iterations.
      """
      key = '\\\\.\\' + '\\'.join(args)
      def value(line):
        if key in line:
          return line[key]
        elif 'default' in kwargs:
          return kwargs['default']
        else:
          raise KeyError('Key "%s" not found in data and '
                         'no default was given.' % key)
      return [float(value(line)) * kwargs.get('mult', 1) for line in data]

    result = {
        'identifier': 'ippet',
        'power_samples_mw': get('Power(_Total)', 'Package W', mult=1000),
        'energy_consumption_mwh':
            sum(map(operator.mul,
                    get('Power(_Total)', 'Package W', mult=1000),
                    get('sys', 'Interval(secs)', mult=1./3600.))),
        'component_utilization': {
            'whole_package': {
                'average_temperature_c':
                    statistics.ArithmeticMean(get(
                        'Temperature(Package)', 'Current C')),
            },
            'cpu': {
                'power_samples_mw': get('Power(_Total)', 'CPU W', mult=1000),
                'energy_consumption_mwh':
                    sum(map(operator.mul,
                            get('Power(_Total)', 'CPU W', mult=1000),
                            get('sys', 'Interval(secs)', mult=1./3600.))),
            },
            'disk': {
                'power_samples_mw': get('Power(_Total)', 'Disk W', mult=1000),
                'energy_consumption_mwh':
                    sum(map(operator.mul,
                            get('Power(_Total)', 'Disk W', mult=1000),
                            get('sys', 'Interval(secs)', mult=1./3600.))),
            },
            'gpu': {
                'power_samples_mw': get('Power(_Total)', 'GPU W', mult=1000),
                'energy_consumption_mwh':
                    sum(map(operator.mul,
                            get('Power(_Total)', 'GPU W', mult=1000),
                            get('sys', 'Interval(secs)', mult=1./3600.))),
            },
        },
    }

    # Find Chrome processes in data. Note that this won't work if there are
    # extra Chrome processes lying around.
    chrome_keys = set()
    for iteration in data:
      for key in iteration.iterkeys():
        parts = key.split('\\')
        if (len(parts) >= 4 and
            re.match(r'Process\(Google Chrome [0-9]+\)', parts[3])):
          chrome_keys.add(parts[3])
    # Add Chrome process power usage to result.
    # Note that this is only an estimate of Chrome's CPU power usage.
    if chrome_keys:
      per_process_power_usage = [
          get(key, 'CPU Power W', default=0, mult=1000) for key in chrome_keys]
      result['application_energy_consumption_mwh'] = (
          sum(map(operator.mul,
                  map(sum, zip(*per_process_power_usage)),
                  get('sys', 'Interval(secs)', mult=1./3600.))))

    return result
<|endoftext|>