<s> from __future__ import division , print_function <EOL> from math import radians , sin , cos <EOL> import numpy as np <EOL> from bumps . parameter import Parameter , varying <EOL> def plot ( X , Y , theory , data , err ) : <EOL> import pylab <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> pylab . pcolormesh ( X , Y , data ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> pylab . pcolormesh ( X , Y , theory ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> pylab . pcolormesh ( X , Y , ( data - theory ) / ( err + <NUM_LIT:1> ) ) <EOL> class Gaussian ( object ) : <EOL> def __init__ ( self , A = <NUM_LIT:1> , xc = <NUM_LIT:0> , yc = <NUM_LIT:0> , s1 = <NUM_LIT:1> , s2 = <NUM_LIT:1> , theta = <NUM_LIT:0> , name = "<STR_LIT>" ) : <EOL> self . A = Parameter ( A , name = name + "<STR_LIT:A>" ) <EOL> self . xc = Parameter ( xc , name = name + "<STR_LIT>" ) <EOL> self . yc = Parameter ( yc , name = name + "<STR_LIT>" ) <EOL> self . s1 = Parameter ( s1 , name = name + "<STR_LIT>" ) <EOL> self . s2 = Parameter ( s2 , name = name + "<STR_LIT>" ) <EOL> self . theta = Parameter ( theta , name = name + "<STR_LIT>" ) <EOL> def parameters ( self ) : <EOL> return dict ( A = self . A , <EOL> xc = self . xc , yc = self . yc , <EOL> s1 = self . s1 , s2 = self . s2 , <EOL> theta = self . theta ) <EOL> def __call__ ( self , x , y ) : <EOL> height = self . A . value <EOL> s1 = self . s1 . value <EOL> s2 = self . s2 . value <EOL> t = - radians ( self . theta . value ) <EOL> xc = self . xc . value <EOL> yc = self . yc . value <EOL> if s1 == <NUM_LIT:0> or s2 == <NUM_LIT:0> : return np . zeros_like ( x ) <EOL> a = cos ( t ) ** <NUM_LIT:2> / s1 ** <NUM_LIT:2> + sin ( t ) ** <NUM_LIT:2> / s2 ** <NUM_LIT:2> <EOL> b = sin ( <NUM_LIT:2> * t ) * ( - <NUM_LIT:1> / s1 ** <NUM_LIT:2> + <NUM_LIT:1> / s2 ** <NUM_LIT:2> ) <EOL> c = sin ( t ) ** <NUM_LIT:2> / s1 ** <NUM_LIT:2> + cos ( t ) ** <NUM_LIT:2> / s2 ** <NUM_LIT:2> <EOL> xbar , ybar = x - xc , y - yc <EOL> Zf = np . exp ( - <NUM_LIT:0.5> * ( a * xbar ** <NUM_LIT:2> + b * xbar * ybar + c * ybar ** <NUM_LIT:2> ) ) <EOL> total = np . sum ( Zf ) <EOL> if False and ( np . isnan ( total ) or total == <NUM_LIT:0> ) : <EOL> print ( "<STR_LIT>" , total , ( height , s1 , s2 , t , xc , yc ) ) <EOL> print ( "<STR_LIT>" , a , b , c ) <EOL> return Zf / total * abs ( height ) if total > <NUM_LIT:0> else np . zeros_like ( x ) <EOL> class Background ( object ) : <EOL> def __init__ ( self , C = <NUM_LIT:0> , name = "<STR_LIT>" ) : <EOL> self . C = Parameter ( C , name = name + "<STR_LIT>" ) <EOL> def parameters ( self ) : <EOL> return dict ( C = self . C ) <EOL> def __call__ ( self , x , y ) : <EOL> return self . C . value <EOL> class Peaks ( object ) : <EOL> def __init__ ( self , parts , X , Y , data , err ) : <EOL> self . X , self . Y , self . data , self . err = X , Y , data , err <EOL> self . parts = parts <EOL> def numpoints ( self ) : <EOL> return np . prod ( self . data . shape ) <EOL> def parameters ( self ) : <EOL> return [ p . parameters ( ) for p in self . parts ] <EOL> def theory ( self ) : <EOL> return sum ( M ( self . X , self . Y ) for M in self . parts ) <EOL> def residuals ( self ) : <EOL> return ( self . theory ( ) - self . data ) / ( self . err + <NUM_LIT:1> ) <EOL> def nllf ( self ) : <EOL> R = self . residuals ( ) <EOL> return <NUM_LIT:0.5> * np . sum ( R ** <NUM_LIT:2> ) <EOL> def __call__ ( self ) : <EOL> return <NUM_LIT:2> * self . nllf ( ) / self . dof <EOL> def plot ( self , view = '<STR_LIT>' ) : <EOL> plot ( self . X , self . Y , self . theory ( ) , self . data , self . err ) <EOL> def save ( self , basename ) : <EOL> import json <EOL> pars = [ ( p . name , p . value ) for p in varying ( self . parameters ( ) ) ] <EOL> out = json . dumps ( dict ( theory = self . theory ( ) . tolist ( ) , <EOL> data = self . data . tolist ( ) , <EOL> err = self . err . tolist ( ) , <EOL> X = self . X . tolist ( ) , <EOL> Y = self . Y . tolist ( ) , <EOL> pars = pars ) ) <EOL> open ( basename + "<STR_LIT>" , "<STR_LIT:w>" ) . write ( out ) <EOL> def update ( self ) : <EOL> pass </s>
