@exp.html


001| """Exponential.
001| 
001| Command:
001|     exp0        do_exponential_0
001|     exp1        do_exponential_1
001|     exp2        do_exponential_2
001|     exp3        do_exponential_3
001| 
001|     gauss0      do_gaussian_0
001|     gauss1      do_gaussian_1
001|     gauss2      do_gaussian_2
001|     gauss3      do_gaussian_3
001|     gauss4      do_gaussian_4
001| 
001|     sig1        do_sigmoid_1
001|     sig2        do_sigmoid_2
001| 
001|     ex          do_e_to_the_x_power
001| 
001|     test        do_test
001| 
001| """

002| ####################################################################################################
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from math import pi,sin,cos, sqrt, exp as exp_r
007| from cmath import exp as exp_c
008| from logger import logger, info, debug, trace
009| from goodies import divisions
010| from mlchart import mlchart
011| from numpy import linspace
012| from goodies import KWARG, divisions
013| ####################################################################################################
014| BasePath = r'C:\CJS\prj\Python\blog\proj'
015| ImgsPath = path.join(BasePath,'img')
016| Log = logger('expf')
017| 
018| CMND = ''
019| ARGS = {
020|     #'fname': 'chart.png',
021|     'fpath': BasePath,
022| }
023| 
024| I2PI = complex(0,1)*complex(2.0*pi,0)
025| 
026| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
027| class exp_function_chart (mlchart):
028| 
029|     def __init__ (self, dims=(8.0, 3.0), **kwargs):
030|         super().__init__(dims, **kwargs)
031|         self.title = 'Exponential Function'
032|         self.x_title = ''
033|         self.y_title = ''
034|         self.x_tickf = '%.1f'
035|         self.y_tickf = '%.2f'
036|         self.x_grids = (pi, pi/6.0)
037|         self.y_grids = (1.0, 0.10)
038|         self.margins = dict(left=0.08, right=0.97, bottom=0.10, top=0.90)
039|         self.x0 = -2.0
040|         self.x1 = +2.0
041|         self.y0 = -2.00
042|         self.y1 = +2.00
043|         self.calculate_limits()
044| 
045|     def calculate_limits (self):
046|         self.x_limit = (self.x0 * pi, self.x1 * pi)
047|         self.y_limit = (self.y0, self.y1)
048|         self.xt = linspace(self.x_limit[0], self.x_limit[1], 501)
049|         self.xticks = linspace(self.x0, self.x1, int(self.x1-self.x0)+1)
050| 
051|     def make_x_axis_spcl (self):
052|         self.ax.set_xticks([x*pi for x in self.xticks])
053|         self.ax.set_xticklabels(['%+d $pi$' % int(x) for x in self.xticks])
054| 
055| 
056| ##================================================================================================##
057| def do_exponential_0 (**kwargs):
058|     """CMND: Exponential 0."""
059|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
060|     fname = KWARG('fname', 'exponential-0.png', str,  kwargs)
061|     Log.info('exponential-0:: %s' % str(list(kwargs.keys())))
062|     Log.info('fpath="%s"' % fpath)
063|     Log.info('fname="%s"' % fname)
064|     Log.info()
065|     #
066|     class exponential_chart (mlchart):
067|         def __init__ (self, **kwargs):
068|             super().__init__(**kwargs)
069|             self.dims = (6.4, 4.8)
070|             self.margins = dict(left=0.08, right=0.97, bottom=0.10, top=0.90)
071|             self.title = r'Exp Function - $e^x$'
072|             self.x_title = ''
073|             self.y_title = ''
074|             self.x_limit = (-5.0, +5.0)
075|             self.y_limit = (0.0, 20.0)
076|             self.x_grids = (5.0, 1.0)
077|             self.y_grids = (10.0, 1.0)
078|             self.x_tickf = '%.0f'
079|             self.y_tickf = '%.0f'
080|             self.xt = list(divisions(self.x_limit[0], self.x_limit[1], 501))
081|         def plotter (self):
082|             # Generate data...
083|             ys0 = [exp_r(x) for x in self.xt]
084|             ys1 = [pow(0.125,x) for x in self.xt]
085|             ys2 = [pow(0.25,x) for x in self.xt]
086|             ys3 = [pow(0.50,x) for x in self.xt]
087|             ys4 = [pow(1,x) for x in self.xt]
088|             ys5 = [pow(1.5,x) for x in self.xt]
089|             ys6 = [pow(2,x) for x in self.xt]
090|             ys7 = [pow(4,x) for x in self.xt]
091|             ys8 = [pow(8,x) for x in self.xt]
092|             # Plot data...
093|             self.ax.plot(self.xt,ys0, label='a=e'    , c='#007f00', zorder=10, alpha=0.80, lw=3.0)
094|             self.ax.plot(self.xt,ys1, label='a=0.125', c='#bf00bf', zorder=11, alpha=0.80)
095|             self.ax.plot(self.xt,ys2, label='a=0.25' , c='#bf00bf', zorder=12, alpha=0.80)
096|             self.ax.plot(self.xt,ys3, label='a=0.5'  , c='#bf00bf', zorder=13, alpha=0.80)
097|             self.ax.plot(self.xt,ys4, label='a=1.0'  , c='#ff0000', zorder=14, alpha=0.80, lw=3.0)
098|             self.ax.plot(self.xt,ys5, label='a=1.5'  , c='#00007f', zorder=15, alpha=0.80)
099|             self.ax.plot(self.xt,ys6, label='a=2.0'  , c='#0000ff', zorder=16, alpha=0.80)
100|             self.ax.plot(self.xt,ys7, label='a=4.0'  , c='#0000ff', zorder=17, alpha=0.80)
101|             self.ax.plot(self.xt,ys8, label='a=8.0'  , c='#0000ff', zorder=18, alpha=0.80)
102|     #
103|     cht = exponential_chart(fname=fname, fpath=fpath)
104|     cht.make()
105|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
106|     Log.info()
107|     Log.info()
108|     return 'Done'
109| 
110| ##================================================================================================##
111| def do_exponential_1 (**kwargs):
112|     """CMND: Exponential 1."""
113|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
114|     fname = KWARG('fname', 'exponential-1.png', str,  kwargs)
115|     freq  = float(kwargs['f']) if 'f' in kwargs else 5.0
116|     phaz  = float(kwargs['w']) if 'w' in kwargs else 0.2
117|     Log.info('exponential-1:: %s' % str(list(kwargs.keys())))
118|     Log.info('fpath="%s"' % fpath)
119|     Log.info('fname="%s"' % fname)
120|     Log.info('freq="%.3f"' % freq)
121|     Log.info('phaz="%.3f"' % phaz)
122|     Log.info()
123|     #
124|     class exponential_chart (exp_function_chart):
125|         def __init__ (self, **kwargs):
126|             super().__init__(**kwargs)
127|             self.title = r'Exp Function - $e^{{i}2\pi{x}{k}-2\pi{w}}$'
128|             self.x0 =  0.0
129|             self.x1 = +2.0
130|             self.calculate_limits()
131|         def plotter (self):
132|             E = lambda k,w: lambda x: exp_c((complex(0,1)*complex(x*k,0))-complex(0,w*2*pi))
133|             F = E(freq, phaz)
134|             # Generate data...
135|             cs = [F(x) for x in self.xt]
136|             ys1 = [c.real for c in cs]
137|             ys2 = [c.imag for c in cs]
138|             # Plot data...
139|             self.ax.plot(self.xt,ys1, label='real', c='#0000ff', zorder=10, alpha=0.80, lw=1.0)
140|             self.ax.plot(self.xt,ys2, label='imag', c='#ff0000', zorder=11, alpha=0.80, lw=1.0)
141|             self.ax.legend(loc='lower left')
142|             self.ax.text(0.05*pi, 1.5, '[k=%.2f, w=%.2f]' % (freq,phaz), fontsize=10, backgroundcolor='#ffffbf')
143|     #
144|     cht = exponential_chart(fname=fname, fpath=fpath)
145|     cht.make()
146|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
147|     Log.info()
148|     Log.info()
149|     return 'Done'
150| 
151| ##================================================================================================##
152| def do_exponential_2 (**kwargs):
153|     """CMND: Exponential 2."""
154|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
155|     fname = KWARG('fname', 'exponential-2.png', str,  kwargs)
156|     f1  = float(kwargs['f1']) if 'f1' in kwargs else 7.0
157|     f2  = float(kwargs['f2']) if 'f2' in kwargs else 13.0
158|     f3  = float(kwargs['f3']) if 'f3' in kwargs else 31.0
159|     Log.info('exponential-2:: %s' % str(list(kwargs.keys())))
160|     Log.info('fpath="%s"' % fpath)
161|     Log.info('fname="%s"' % fname)
162|     Log.info('freq1="%.3f"' % f1)
163|     Log.info('freq2="%.3f"' % f2)
164|     Log.info('freq3="%.3f"' % f3)
165|     Log.info()
166|     #
167|     class exponential_chart (exp_function_chart):
168|         def __init__ (self, **kwargs):
169|             super().__init__(**kwargs)
170|             self.title = r'Exp Function - $e^{{i}2\pi{x}{f_1}}+e^{{i}2\pi{x}{f_2}}+e^{{i}2\pi{x}{f_3}}$'
171|             self.x0 =  0.0
172|             self.x1 = +2.0
173|             self.calculate_limits()
174|         def plotter (self):
175|             K = lambda k: lambda x:exp_c(complex(0,1)*complex(x*k,0))
176|             N = 3.0
177|             F1 = K(f1)
178|             F2 = K(f2)
179|             F3 = K(f3)
180| 
181|             cs = [(F1(x)+F2(x)+F3(x))/N for x in self.xt]
182|             ys1 = [c.real for c in cs]
183|             ys2 = [c.imag for c in cs]
184|             # Plot data...
185|             self.ax.plot(self.xt,ys1, label='real', c='#0000ff', zorder=10, alpha=0.80, lw=1.0)
186|             self.ax.plot(self.xt,ys2, label='imag', c='#ff0000', zorder=11, alpha=0.80, lw=1.0)
187|             self.ax.legend(loc='lower left')
188|             self.ax.text(0.05*pi, 1.5, '[f1=%.2f, f2=%.2f, f3=%.2f]' % (f1,f2,f3), fontsize=10, backgroundcolor='#ffffbf')
189|     #
190|     cht = exponential_chart(fname=fname, fpath=fpath)
191|     cht.make()
192|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
193|     Log.info()
194|     Log.info()
195|     return 'Done'
196| 
197| ##================================================================================================##
198| def do_exponential_3 (**kwargs):
199|     """CMND: Exponential 3."""
200|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
201|     fname = KWARG('fname', 'exponential-3.png', str,  kwargs)
202|     freq  = float(kwargs['f']) if 'f' in kwargs else 21.0
203|     wide  = float(kwargs['w']) if 'w' in kwargs else 0.75
204|     Log.info('exponential-3:: %s' % str(list(kwargs.keys())))
205|     Log.info('fpath="%s"' % fpath)
206|     Log.info('fname="%s"' % fname)
207|     Log.info('freq="%.3f"' % freq)
208|     Log.info('wide="%.3f"' % wide)
209|     Log.info()
210|     #
211|     class exponential_chart (exp_function_chart):
212|         def __init__ (self, **kwargs):
213|             super().__init__(**kwargs)
214|             self.title = r'Exp Function - $e^{{i}2\pi{x}{k}}\times{e}^{\frac{-x^2}{W}}$'
215|             #self.x0 = 0.0
216|             #self.x1 = +2.0
217|             #self.calculate_limits()
218|         def plotter (self):
219|             f1 = lambda x:exp_c(complex(0,1)*complex(x*freq,0))
220|             f2 = lambda x:exp_c(-(x*x)/wide)
221| 
222|             cs = [f1(x)*f2(x) for x in self.xt]
223|             ys1 = [c.real for c in cs]
224|             ys2 = [c.imag for c in cs]
225|             # Plot data...
226|             self.ax.plot(self.xt,ys1, label='real', c='#0000ff', zorder=10, alpha=0.80, lw=1.0)
227|             self.ax.plot(self.xt,ys2, label='imag', c='#ff0000', zorder=11, alpha=0.80, lw=1.0)
228|             self.ax.legend(loc='lower left')
229|             self.ax.text(0.05*pi, 1.5, '[f=%.2f, w=%.2f]' % (freq,wide), fontsize=10, backgroundcolor='#ffffbf')
230|     #
231|     cht = exponential_chart(fname=fname, fpath=fpath)
232|     cht.make()
233|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
234|     Log.info()
235|     #
236|     Log.info()
237|     return 'Done'
238| 
239| ##================================================================================================##
240| def do_sigmoid_1 (**kwargs):
241|     """CMND: Sigmoid 1."""
242|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
243|     fname = KWARG('fname', 'sigmoid-1.png', str,  kwargs)
244|     Log.info('sigmoid-1:: %s' % str(list(kwargs.keys())))
245|     Log.info('fpath="%s"' % fpath)
246|     Log.info('fname="%s"' % fname)
247|     Log.info()
248|     #
249|     #TODO: These shouldn't use the x-pi scale...
250|     #
251|     class sigmoid_chart (exp_function_chart):
252|         def __init__ (self, **kwargs):
253|             super().__init__((8.0, 4.5), **kwargs)
254|             self.title = r'Exp Function - Sigmoid'
255|         def plotter (self):
256|             M = +2.0
257|             F = lambda x: -(M/2)+((M*exp_r(x))/(1.0+exp_r(x)))
258|             # Generate data...
259|             dx1 = [F(x) for x in self.xt]
260|             # Plot data...
261|             self.ax.plot(self.xt,dx1, color='#0000ff', lw=1.60, zorder=20, alpha=0.85)
262|             self.ax.text(-1.75*pi, 1.5, r'$ f(x)=\frac{{M}\times e^x}{1+e^x} $', fontsize=18, backgroundcolor='#ffffbf')
263|     #
264|     cht = sigmoid_chart(fname=fname, fpath=fpath)
265|     cht.make()
266|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
267|     Log.info()
268|     Log.info()
269|     return 'Done'
270| 
271| ##================================================================================================##
272| def do_sigmoid_2 (**kwargs):
273|     """CMND: Sigmoid 1."""
274|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
275|     fname = KWARG('fname', 'sigmoid-2.png', str,  kwargs)
276|     Log.info('sigmoid-2:: %s' % str(list(kwargs.keys())))
277|     Log.info('fpath="%s"' % fpath)
278|     Log.info('fname="%s"' % fname)
279|     Log.info()
280|     #
281|     #TODO: These shouldn't use the x-pi scale...
282|     #
283|     class sigmoid_chart (exp_function_chart):
284|         def __init__ (self, **kwargs):
285|             super().__init__((8.0, 4.5), **kwargs)
286|             self.title = r'Exp Function - Logistic'
287|         def plotter (self):
288|             X0 = 1.0 * pi       # X-axis crossing
289|             K = 3.0             # steepness
290|             M = 2.4             # max height
291|             F = lambda x: -(M/2)+(M/(1.0+exp_r(-K*(x-X0))))
292|             # Generate data...
293|             dx1 = [F(x) for x in self.xt]
294|             # Plot data...
295|             self.ax.plot(self.xt,dx1, color='#0000ff', lw=1.60, zorder=20, alpha=0.85)
296|             self.ax.text(-1.75*pi, 1.5, r'$ f(x)=\frac{M}{1+e^{-K(x-x_0)}} $', fontsize=18, backgroundcolor='#ffffbf')
297|     #
298|     cht = sigmoid_chart(fname=fname, fpath=fpath)
299|     cht.make()
300|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
301|     Log.info()
302|     Log.info()
303|     return 'Done'
304| 
305| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
306| class gaussian_chart (mlchart):
307|     def __init__ (self, **kwargs):
308|         super().__init__(**kwargs)
309|         self.dims = (8.0, 4.8)
310|         self.margins = dict(left=0.06, right=0.97, bottom=0.07, top=0.93)
311|         self.title = r'Exp Function - Gaussian'
312|         self.x_title = 'X Axis (position along rod)'
313|         self.y_title = 'Y Axis (temperature)'
314|         self.title_kw['fontsize'] = 16
315|         self.x_limit = (-5.0, +5.0)
316|         self.y_limit = (0.0, 5.0)
317|         self.x_grids = (1.0, 0.1)
318|         self.y_grids = (0.1, 0.025)
319|         self.x_tickf = '%+.1f'
320|         self.y_tickf = '%+.1f'
321| 
322|     def make_x_axis_spcl (self):
323|         """Rotate X-axis labels 90 degrees."""
324|         for tick in self.ax.xaxis.get_major_ticks():
325|             tick.label1.set_rotation('vertical')
326|             tick.label1.set_size(9)
327| 
328| ##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##
329| GaussExp = lambda b,c,x:exp_r(-pow((x-b),2)/pow(c,2))
330| 
331| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
332| class Gaussian (object):
333|     """Gaussian curve and its derivatives."""
334| 
335|     def __init__ (self, a=1.0, b=0.0, c=1.0):
336|         self.a = a
337|         self.b = b
338|         self.c = c
339| 
340|     def ftn (self, x):
341|         """Return function(x)."""
342|         g = GaussExp(self.b, self.c, x)
343|         return (self.a * g)
344| 
345|     def dx (self, x):
346|         """Return d/dx function(x)."""
347|         t = -2 * self.a * (x-self.b)
348|         h = t / pow(self.c,2)
349|         g = GaussExp(self.b, self.c, x)
350|         return (h * g)
351| 
352|     def dx2 (self, x):
353|         """Return d2/dx2 function(x)."""
354|         t = -2 * self.a * pow(self.c,2)
355|         u = +4 * self.a * pow(x-self.b,2)
356|         h = (t + u) / pow(self.c,4)
357|         g = GaussExp(self.b, self.c, x)
358|         return (h * g)
359| 
360|     def dx3 (self, x):
361|         """Return d3/dx3 function(x)."""
362|         t = -8 * self.a * pow((x-self.b),3)
363|         u = 12 * self.a * pow(self.c,2) * (x-self.b)
364|         h = (t + u) / pow(self.c,6)
365|         g = GaussExp(self.b, self.c, x)
366|         return (h * g)
367| 
368|     def root2 (self): return (self.c*sqrt(1/2))
369|     def root3 (self): return (self.c*sqrt(3/2))
370| 
371| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
372| class GaussianCurves (Gaussian):
373|     """Set of Gaussian curves and derivatives."""
374|     lbls = [
375|         r"Basic Gaussian $\left[f(x)=ae^{-\left(\frac{x}{c}\right)}\right]$ and its Derivatives",
376|         r'$f(x)=ae^{-\left(\frac{x}{c}\right)}$',
377|         r'$\frac{d}{dx}\;f(x)$',
378|         r'$\frac{d^2}{dx^2}\;f(x)$',
379|         r'$\frac{d^3}{dx^3}\;f(x)$',
380|     ]
381|     def __init__ (self, a=1.0, b=0.0, c=1.0):
382|         super().__init__(a=a, b=b, c=c)
383|         self.xs = []
384|         self.ys0 = []
385|         self.ys1 = []
386|         self.ys2 = []
387|         self.ys3 = []
388|         self.norm1 = 1.0
389|         self.norm2 = 1.0
390|         self.norm3 = 1.0
391| 
392|     def calculate (self, xmin, xmax, steps=1000):
393|         self.xs = list(divisions(xmin, xmax, steps))
394|         # Generate curves...
395|         self.ys0 = [self.ftn(x) for x in self.xs]
396|         self.ys1 = [self.dx(x) for x in self.xs]
397|         self.ys2 = [self.dx2(x) for x in self.xs]
398|         self.ys3 = [self.dx3(x) for x in self.xs]
399|         # Normalize dx, dx2, and dx3 curves...
400|         m1 = self.norm1 * max([abs(y) for y in self.ys1])
401|         m2 = self.norm2 * max([abs(y) for y in self.ys2])
402|         m3 = self.norm3 * max([abs(y) for y in self.ys3])
403|         self.ys1 = [y/m1 for y in self.ys1]
404|         self.ys2 = [y/m2 for y in self.ys2]
405|         self.ys3 = [y/m3 for y in self.ys3]
406|         self.data = (self.xs, self.ys0, self.ys1, self.ys2, self.ys3)
407| 
408| ##================================================================================================##
409| def do_gaussian_0 (**kwargs):
410|     """CMND: Gaussian 0."""
411|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
412|     fname = KWARG('fname', 'gaussian-0.png', str,  kwargs)
413|     xmin = KWARG('xmin', -4.0, float, kwargs)
414|     xmax = KWARG('xmax', +4.0, float, kwargs)
415|     ymin = KWARG('ymin',  0.0, float, kwargs)
416|     ymax = KWARG('ymax', +1.1, float, kwargs)
417|     xtick0 = KWARG('xtk0', 0.50, float, kwargs)
418|     xtick1 = KWARG('xtk1', 0.10, float, kwargs)
419|     ytick0 = KWARG('ytk0', 1.00, float, kwargs)
420|     ytick1 = KWARG('ytk1', 0.25, float, kwargs)
421|     Log.info('gaussian-0:: %s' % str(list(kwargs.keys())))
422|     Log.info('fpath="%s"' % fpath)
423|     Log.info('fname="%s"' % fname)
424|     Log.info('xmin,xmax=[%.3f, %.3f]' % (xmin,xmax))
425|     Log.info('ymin,ymax=[%.3f, %.3f]' % (ymin,ymax))
426|     Log.info('xticks=[%.3f, %.3f]' % (xtick0,xtick1))
427|     Log.info('yticks=[%.3f, %.3f]' % (ytick0,ytick1))
428|     Log.info()
429| 
430|     class gaussian_0 (gaussian_chart):
431|         """Gaussian Chart."""
432|         def __init__ (self, **kwargs):
433|             super().__init__(**kwargs)
434|             self.title = r'Gaussian Distribution (Bell Curve)'
435|             self.x_title = ''
436|             self.y_title = ''
437|             self.x_limit = (xmin,xmax)
438|             self.y_limit = (ymin, ymax)
439|             self.fname = fname
440|             self.fpath = fpath
441| 
442|         def plotter (self):
443|             """Plot something!"""
444|             xs = list(divisions(xmin, xmax, 501))
445|             ys = [exp_r(-pow(x,2)) for x in xs]
446|             self.ax.axhline(c='#000000', lw=2.0, alpha=1.00, zorder=12)
447|             self.ax.fill_between(xs,ys,0, color='#7fbfff', alpha=0.50, zorder=10)
448|             self.ax.plot(xs,ys, c='#0000ff', lw=2.0, alpha=0.80, zorder=11)
449|             self.ax.text(xmin+0.25, ymax-0.15, r'$f(x)=e^{-(x^2)}$', fontsize=24, bbox=dict(color='#ffffdf', ec='#000000'))
450| 
451|         def make_x_axis_spcl (self):
452|             """Rotate X-axis labels 90 degrees."""
453|             pass
454| 
455|     # Create graph...
456|     cht = gaussian_0()
457|     cht.make()
458|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
459|     Log.info()
460|     Log.info()
461|     return 'Done'
462| 
463| ##================================================================================================##
464| def do_gaussian_1 (**kwargs):
465|     """CMND: Gaussian 1."""
466|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
467|     fname = KWARG('fname', 'gaussian-1.png', str,  kwargs)
468|     xmin = KWARG('xmin', -3.0, float, kwargs)
469|     xmax = KWARG('xmax', +3.0, float, kwargs)
470|     ymin = KWARG('ymin',  0.0, float, kwargs)
471|     ymax = KWARG('ymax', +6.0, float, kwargs)
472|     xtick0 = KWARG('xtk0', 0.50, float, kwargs)
473|     xtick1 = KWARG('xtk1', 0.10, float, kwargs)
474|     ytick0 = KWARG('ytk0', 1.00, float, kwargs)
475|     ytick1 = KWARG('ytk1', 0.25, float, kwargs)
476|     Log.info('gaussian-1:: %s' % str(list(kwargs.keys())))
477|     Log.info('fpath="%s"' % fpath)
478|     Log.info('fname="%s"' % fname)
479|     Log.info('xmin,xmax=[%.3f, %.3f]' % (xmin,xmax))
480|     Log.info('xmin,xmax=[%.3f, %.3f]' % (ymin,ymax))
481|     Log.info('xticks=[%.3f, %.3f]' % (xtick0,xtick1))
482|     Log.info('yticks=[%.3f, %.3f]' % (ytick0,ytick1))
483|     Log.info()
484|     Gfactory = lambda A,Z,W: lambda x: A*exp_r(-pow(x-Z,2)/(2*pow(W,2)))
485| 
486|     class gaussian_1 (gaussian_chart):
487|         """Gaussian Chart."""
488|         def __init__ (self, data, lbls, **kwargs):
489|             super().__init__(**kwargs)
490|             self.data = data
491|             self.lbls = lbls
492|             self.x_title = ''
493|             self.y_title = ''
494|             self.x_limit = (xmin, xmax)
495|             self.y_limit = (ymin, ymax)
496|             self.x_grids = (xtick0, xtick1)
497|             self.y_grids = (ytick0, ytick1)
498|             self.fname = fname
499|             self.fpath = fpath
500| 
501|         def plotter (self):
502|             """Plot something!"""
503|             # Plot data...
504|             self.ax.axvline(c='#000000', lw=2.0, alpha=1.00, zorder=11)
505|             self.ax.axhline(c='#000000', lw=2.0, alpha=1.00, zorder=12)
506|             self.ax.text(-2.75, +4.95, r'$f(x)={A}e^{\frac{-(x-B)^2}{2C^2}}$', fontsize=18, bbox=dict(color='#ffffdf', ec='#000000'))
507|             #
508|             self.ax.fill_between(self.data[0],self.data[1],0, color='#ff0000', alpha=0.10, zorder=31)
509|             self.ax.fill_between(self.data[0],self.data[2],0, color='#007f00', alpha=0.10, zorder=32)
510|             self.ax.fill_between(self.data[0],self.data[3],0, color='#0000bf', alpha=0.10, zorder=33)
511|             self.ax.fill_between(self.data[0],self.data[4],0, color='#7f007f', alpha=0.10, zorder=34)
512|             self.ax.fill_between(self.data[0],self.data[5],0, color='#000000', alpha=0.10, zorder=35)
513|             #
514|             self.ax.plot(self.data[0],self.data[1], label=self.lbls[1], color='#ff0000', lw=2.0, alpha=0.80, zorder=31)
515|             self.ax.plot(self.data[0],self.data[2], label=self.lbls[2], color='#007f00', lw=2.0, alpha=0.80, zorder=32)
516|             self.ax.plot(self.data[0],self.data[3], label=self.lbls[3], color='#0000bf', lw=2.0, alpha=0.80, zorder=33)
517|             self.ax.plot(self.data[0],self.data[4], label=self.lbls[4], color='#7f007f', lw=2.0, alpha=0.80, zorder=34)
518|             self.ax.plot(self.data[0],self.data[5], label=self.lbls[5], color='#000000', lw=2.0, alpha=0.80, zorder=35)
519|             #
520|             self.ax.legend(loc='upper right', shadow=True)
521| 
522|         def make_x_axis_spcl (self):
523|             """Rotate X-axis labels 90 degrees."""
524|             pass
525| 
526|     labels = ['',
527|         'A=5.0, B=0.0, C=0.10',
528|         'A=4.0, B=0.0, C=0.25',
529|         'A=3.0, B=0.0, C=0.50',
530|         'A=2.0, B=0.0, C=1.00',
531|         'A=1.0, B=0.0, C=1.70'
532|     ]
533| 
534|     G1 = Gfactory(5.00, 0.00, 0.10)
535|     G2 = Gfactory(4.00, 0.00, 0.25)
536|     G3 = Gfactory(3.00, 0.00, 0.50)
537|     G4 = Gfactory(2.00, 0.00, 1.00)
538|     G5 = Gfactory(1.00, 0.00, 1.70)
539| 
540|     # Generate plot data...
541|     xs = list(divisions(xmin, xmax, 501))
542|     ys1 = [G1(x) for x in xs]
543|     ys2 = [G2(x) for x in xs]
544|     ys3 = [G3(x) for x in xs]
545|     ys4 = [G4(x) for x in xs]
546|     ys5 = [G5(x) for x in xs]
547|     data = (xs, ys1, ys2, ys3, ys4, ys5)
548| 
549|     # Create graph...
550|     cht = gaussian_1(data, labels)
551|     cht.make()
552|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
553|     Log.info()
554|     Log.info()
555|     return 'Done'
556| 
557| ##================================================================================================##
558| def do_gaussian_2 (**kwargs):
559|     """CMND: Gaussian 2."""
560|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
561|     fname = KWARG('fname', 'gaussian-2.png', str,  kwargs)
562|     w = KWARG('w', 8.0, float, kwargs)
563|     h = KWARG('h', 5.0, float, kwargs)
564|     xmin = KWARG('xmin', -2.5, float, kwargs)
565|     xmax = KWARG('xmax', +2.5, float, kwargs)
566|     ymin = KWARG('ymin', -3.0, float, kwargs)
567|     ymax = KWARG('ymax', +3.2, float, kwargs)
568|     xtick0 = KWARG('xtk0', 0.50, float, kwargs)
569|     xtick1 = KWARG('xtk1', 0.10, float, kwargs)
570|     ytick0 = KWARG('ytk0', 1.00, float, kwargs)
571|     ytick1 = KWARG('ytk1', 0.25, float, kwargs)
572|     ml = KWARG('ml', 0.09, float, kwargs)
573|     mr = KWARG('mr', 0.99, float, kwargs)
574|     mt = KWARG('mt', 0.91, float, kwargs)
575|     mb = KWARG('mb', 0.14, float, kwargs)
576|     Log.info('gaussian-2:: %s' % str(list(kwargs.keys())))
577|     Log.info('fpath="%s"' % fpath)
578|     Log.info('fname="%s"' % fname)
579|     Log.info('dims=(%.2f, %.2f)' % (w,h))
580|     Log.info('xmin,xmax=[%.3f, %.3f]' % (xmin,xmax))
581|     Log.info('xmin,xmax=[%.3f, %.3f]' % (ymin,ymax))
582|     Log.info('xticks=[%.3f, %.3f]' % (xtick0,xtick1))
583|     Log.info('yticks=[%.3f, %.3f]' % (ytick0,ytick1))
584|     Log.info('margin=[(%.2f, %.2f), (%.2f, %.2f)]' % (ml,mr,mt,mb))
585|     Log.info()
586|     #
587|     class gaussian_2 (gaussian_chart):
588|         """Gaussian Chart."""
589|         def __init__ (self, curves, **kwargs):
590|             super().__init__(**kwargs)
591|             self.curves = curves
592|             self.title = curves.lbls[0]
593|             self.x_limit = (xmin, xmax)
594|             self.y_limit = (ymin, ymax)
595|             self.x_grids = (xtick0, xtick1)
596|             self.y_grids = (ytick0, ytick1)
597|             self.margins = dict(left=ml, right=mr, bottom=mb, top=mt)
598|             self.fname = fname
599|             self.fpath = fpath
600| 
601|         def plotter (self):
602|             """Plot something!"""
603|             data = self.curves.data
604|             self.ax.axvline(c='#000000', lw=2.0, alpha=1.00, zorder=11)
605|             self.ax.axhline(c='#000000', lw=2.0, alpha=1.00, zorder=12)
606|             #
607|             self.ax.fill_between(data[0], data[3], 0, color='#7f007f', alpha=0.10, zorder=20)
608|             #
609|             self.ax.axvline(+self.curves.root2(), c='#7f007f', lw=2.0, ls='--', alpha=0.70, zorder=21)
610|             self.ax.axvline(-self.curves.root2(), c='#7f007f', lw=2.0, ls='--', alpha=0.70, zorder=21)
611|             self.ax.axvline(+self.curves.root3(), c='#1f6600', lw=2.0, ls='--', alpha=0.30, zorder=22)
612|             self.ax.axvline(-self.curves.root3(), c='#1f6600', lw=2.0, ls='--', alpha=0.30, zorder=22)
613|             #
614|             self.ax.plot(data[0], data[1], label=self.curves.lbls[1], lw=2.5, color='#ff0000', alpha=0.75, zorder=39)
615|             self.ax.plot(data[0], data[2], label=self.curves.lbls[2], lw=1.5, color='#0000dd', alpha=0.75, zorder=38)
616|             self.ax.plot(data[0], data[3], label=self.curves.lbls[3], lw=2.0, color='#7f007f', alpha=0.75, zorder=37)
617|             self.ax.plot(data[0], data[4], label=self.curves.lbls[4], lw=1.5, color='#1f6600', alpha=0.75, zorder=36)
618|             self.ax.legend(loc='lower left', fontsize=11, shadow=True)
619| 
620|     gc = GaussianCurves(a=3.0, c=1.00)
621|     gc.calculate(xmin, xmax)
622|     # Make the Chart...
623|     cht = gaussian_2(gc, **kwargs)
624|     cht.make()
625|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
626|     Log.info()
627|     Log.info()
628|     return 'Done'
629| 
630| ##================================================================================================##
631| def do_gaussian_3 (**kwargs):
632|     """CMND: Gaussian 3."""
633|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
634|     fname = KWARG('fname', 'gaussian-3.png', str,  kwargs)
635|     w = KWARG('w', 8.0, float, kwargs)
636|     h = KWARG('h', 5.0, float, kwargs)
637|     xmin = KWARG('xmin', -4.0, float, kwargs)
638|     xmax = KWARG('xmax', +4.0, float, kwargs)
639|     ymin = KWARG('ymin', -2.0, float, kwargs)
640|     ymax = KWARG('ymax', +5.0, float, kwargs)
641|     xtick0 = KWARG('xtk0', 0.50, float, kwargs)
642|     xtick1 = KWARG('xtk1', 0.10, float, kwargs)
643|     ytick0 = KWARG('ytk0', 1.00, float, kwargs)
644|     ytick1 = KWARG('ytk1', 0.25, float, kwargs)
645|     ml = KWARG('ml', 0.09, float, kwargs)
646|     mr = KWARG('mr', 0.99, float, kwargs)
647|     mt = KWARG('mt', 0.91, float, kwargs)
648|     mb = KWARG('mb', 0.14, float, kwargs)
649|     Log.info('gaussian-3:: %s' % str(list(kwargs.keys())))
650|     Log.info('fpath="%s"' % fpath)
651|     Log.info('fname="%s"' % fname)
652|     Log.info('dims=(%.2f, %.2f)' % (w,h))
653|     Log.info('xmin,xmax=[%.3f, %.3f]' % (xmin,xmax))
654|     Log.info('xmin,xmax=[%.3f, %.3f]' % (ymin,ymax))
655|     Log.info('xticks=[%.3f, %.3f]' % (xtick0,xtick1))
656|     Log.info('yticks=[%.3f, %.3f]' % (ytick0,ytick1))
657|     Log.info('margin=[(%.2f, %.2f), (%.2f, %.2f)]' % (ml,mr,mt,mb))
658|     Log.info()
659|     #
660|     class gaussian_3 (gaussian_chart):
661|         """Gaussian Chart."""
662|         def __init__ (self, data, **kwargs):
663|             super().__init__(**kwargs)
664|             self.xs  = data[0]
665|             self.ys1 = data[1]
666|             self.ys2 = data[2]
667|             self.xmark = data[3]
668|             self.title = 'Effect of second spatial derivative on Gaussian function'
669|             self.x_limit = (xmin, xmax)
670|             self.y_limit = (ymin, ymax)
671|             self.x_grids = (xtick0, xtick1)
672|             self.y_grids = (ytick0, ytick1)
673|             self.margins = dict(left=ml, right=mr, bottom=mb, top=mt)
674|             self.fname = fname
675|             self.fpath = fpath
676| 
677|         def plotter (self):
678|             """Plot something!"""
679|             self.ax.axvline(c='#000000', lw=2.0, alpha=1.00, zorder=11)
680|             self.ax.axhline(c='#000000', lw=2.0, alpha=1.00, zorder=12)
681|             self.ax.axvline(+self.xmark, c='#5f005f', lw=1.5, ls='--', alpha=0.90, zorder=13)
682|             self.ax.axvline(-self.xmark, c='#5f005f', lw=1.5, ls='--', alpha=0.90, zorder=13)
683|             #
684|             difs = [y1+y2 for y1,y2 in zip(self.ys1,self.ys2)]
685|             self.ax.fill_between(self.xs, self.ys1, difs, color='#7f007f', alpha=0.20, zorder=29)
686|             self.ax.fill_between(self.xs, self.ys2, 0, color='#0000dd', alpha=0.10, zorder=28)
687|             #
688|             self.ax.plot(self.xs, self.ys1, lw=2.5, color='#ff0000', alpha=0.75, zorder=39)
689|             self.ax.plot(self.xs, self.ys2, lw=2.5, color='#0000dd', alpha=0.75, zorder=38)
690| 
691|     gc = GaussianCurves(a=4.0, c=0.50)
692|     gc.norm2 = 0.8
693|     gc.calculate(xmin, xmax)
694|     data = (gc.data[0], gc.data[1], gc.data[3], gc.root2())
695|     # Make the Chart...
696|     cht = gaussian_3(data, **kwargs)
697|     cht.make()
698|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
699|     Log.info()
700|     Log.info()
701|     return 'Done'
702| 
703| ##================================================================================================##
704| def do_gaussian_4 (**kwargs):
705|     """CMND: Gaussian 4."""
706|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
707|     fname = KWARG('fname', 'gaussian-4.png', str,  kwargs)
708|     w = KWARG('w', 8.0, float, kwargs)
709|     h = KWARG('h', 5.0, float, kwargs)
710|     dx = KWARG('dx', 1000, int, kwargs)
711|     xmin = KWARG('xmin', -5.0, float, kwargs)
712|     xmax = KWARG('xmax', +5.0, float, kwargs)
713|     ymin = KWARG('ymin',  0.0, float, kwargs)
714|     ymax = KWARG('ymax', +8.0, float, kwargs)
715|     xtick0 = KWARG('xtk0', 0.50, float, kwargs)
716|     xtick1 = KWARG('xtk1', 0.10, float, kwargs)
717|     ytick0 = KWARG('ytk0', 1.00, float, kwargs)
718|     ytick1 = KWARG('ytk1', 0.25, float, kwargs)
719|     ml = KWARG('ml', 0.09, float, kwargs)
720|     mr = KWARG('mr', 0.99, float, kwargs)
721|     mt = KWARG('mt', 0.91, float, kwargs)
722|     mb = KWARG('mb', 0.14, float, kwargs)
723|     Log.info('gaussian-4:: %s' % str(list(kwargs.keys())))
724|     Log.info('fpath="%s"' % fpath)
725|     Log.info('fname="%s"' % fname)
726|     Log.info('dims=(%.2f, %.2f)' % (w,h))
727|     Log.info('xmin,xmax=[%.3f, %.3f]' % (xmin,xmax))
728|     Log.info('xmin,xmax=[%.3f, %.3f]' % (ymin,ymax))
729|     Log.info('xticks=[%.3f, %.3f]' % (xtick0,xtick1))
730|     Log.info('yticks=[%.3f, %.3f]' % (ytick0,ytick1))
731|     Log.info('margin=[(%.2f, %.2f), (%.2f, %.2f)]' % (ml,mr,mt,mb))
732|     Log.info('dx=%d' % dx)
733|     Log.info()
734|     #
735|     class gaussian_4 (gaussian_chart):
736|         """Gaussian Chart."""
737|         def __init__ (self, xs,ys, **kwargs):
738|             super().__init__(**kwargs)
739|             self.xs = xs
740|             self.ys = ys
741|             self.title = 'Gaussian Diffusion'
742|             self.x_limit = (xmin, xmax)
743|             self.y_limit = (ymin, ymax)
744|             self.x_grids = (xtick0, xtick1)
745|             self.y_grids = (ytick0, ytick1)
746|             self.margins = dict(left=ml, right=mr, bottom=mb, top=mt)
747|             self.fname = fname
748|             self.fpath = fpath
749|         def plotter (self):
750|             """Plot something!"""
751|             self.ax.axvline(c='#000000', lw=2.0, alpha=1.00, zorder=11)
752|             self.ax.axhline(c='#000000', lw=2.0, alpha=1.00, zorder=12)
753|             for ix in range(len(self.ys)):
754|                 self.ax.fill_between(self.xs, self.ys[ix], 0, color='#5f4f3f', alpha=0.10, zorder=21+ix)
755|                 self.ax.plot(self.xs, self.ys[ix], lw=1.0, color='#000000', alpha=0.75, zorder=31+ix)
756| 
757|     N = 15
758|     height,width = 8.0, 0.1
759|     hstep, wstep = -0.5, +0.75
760|     xs = list(divisions(xmin, xmax, dx))
761|     ys = []
762|     for ix in range(N):
763|         g = Gaussian(a=height, c=width)
764|         ys.append([g.ftn(x) for x in xs])
765|         height += hstep
766|         width += wstep
767| 
768|     cht = gaussian_4(xs,ys, **kwargs)
769|     cht.make()
770|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
771|     Log.info()
772|     Log.info()
773|     return 'Done'
774| 
775| ##================================================================================================##
776| def do_e_to_the_x_power (**kwargs):
777|     """CMND: Simple graph of e^x."""
778|     fpath = KWARG('fpath', BasePath, str, kwargs)
779|     fname = KWARG('fname', 'e-to-the-x.png', str,  kwargs)
780|     Log.info('e-to-the-x-power:: %s' % str(list(kwargs.keys())))
781|     Log.info('fpath="%s"' % fpath)
782|     Log.info('fname="%s"' % fname)
783|     Log.info()
784|     #
785|     class e_to_the_x_chart (mlchart):
786|         def __init__ (self, **kwargs):
787|             super().__init__((4.0,5.0), **kwargs)
788|             self.title = r'Exp Function - ${e}^{x}$'
789|             self.x_title = ''
790|             self.y_title = ''
791|             self.x_limit = (-5.0, +5.0)
792|             self.y_limit = (-1.0, 20.0)
793|             self.x_grids = (1.0, 0.2)
794|             self.y_grids = (5.0, 1.0)
795|             self.x_tickf = '%d'
796|             self.y_tickf = '%+d'
797|             self.title_kw['fontsize'] = 24
798|             self.margins = dict(left=0.12, right=0.97, bottom=0.08, top=0.90)
799|         def plotter (self):
800|             xs = list(divisions(self.x_limit[0], self.x_limit[1], 200))
801|             ys = [exp_r(x) for x in xs]
802|             self.ax.axvline(0, self.y_limit[0], self.y_limit[1], color='#3f3f3f', lw=3.0, zorder=9)
803|             self.ax.axhline(0, self.x_limit[0], self.x_limit[1], color='#3f3f3f', lw=3.0, zorder=9)
804|             self.ax.plot(xs,ys, c='#ff0000', lw=5.0, zorder=11, alpha=0.80)
805| 
806|     cht = e_to_the_x_chart(fname=fname, fpath=fpath)
807|     cht.make()
808|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
809|     Log.info()
810|     #
811|     Log.info()
812|     return 'Done'
813| ##================================================================================================##
814| def do_test (**kwargs):
815|     """CMND: Test."""
816|     fpath = KWARG('fpath', BasePath, str, kwargs)
817|     fname = KWARG('fname', 'exp-test.png', str,  kwargs)
818|     Log.info('test:: %s' % str(list(kwargs.keys())))
819|     Log.info('fpath="%s"' % fpath)
820|     Log.info('fname="%s"' % fname)
821|     Log.info()
822|     #
823|     class test_chart (mlchart):
824|         def __init__ (self, **kwargs):
825|             super().__init__((4.0,5.0), **kwargs)
826|             self.title = r'Exp Function - ${e}^{x}$'
827|         def plotter (self):
828|             pass
829|     #
830|     cht = test_chart(fname=fname, fpath=fpath)
831|     cht.make()
832|     Log.info('wrote: %s' % path.join(cht.fpath, cht.fname))
833|     Log.info()
834|     #
835|     Log.info()
836|     return 'Done'
837| ####################################################################################################
838| def dispatch (cmd, **kwargs):
839|     Log.info('command: %s' % cmd)
840|     for kw in kwargs:
841|         Log.info('argument[%s]: %s' % (kw,kwargs[kw]))
842|     Log.info()
843|     if cmd == 'exp0': return do_exponential_0(**kwargs)
844|     if cmd == 'exp1': return do_exponential_1(**kwargs)
845|     if cmd == 'exp2': return do_exponential_2(**kwargs)
846|     if cmd == 'exp3': return do_exponential_3(**kwargs)
847|     if cmd == 'gauss0': return do_gaussian_0(**kwargs)
848|     if cmd == 'gauss1': return do_gaussian_1(**kwargs)
849|     if cmd == 'gauss2': return do_gaussian_2(**kwargs)
850|     if cmd == 'gauss3': return do_gaussian_3(**kwargs)
851|     if cmd == 'gauss4': return do_gaussian_4(**kwargs)
852|     if cmd == 'sig1': return do_sigmoid_1(**kwargs)
853|     if cmd == 'sig2': return do_sigmoid_2(**kwargs)
854|     if cmd == 'ex': return do_e_to_the_x_power(**kwargs)
855|     if cmd == 'test': return do_test(**kwargs)
856|     return 'Nothing to do!'
857| ####################################################################################################
858| if __name__ == '__main__':
859|     print('autorun: %s' % argv[0])
860|     Log.start(path.join(BasePath,'@exp.log'))
861|     Log.level(debug())
862|     cmd = argv[1] if 1 < len(argv) else CMND
863|     kwargs = ARGS
864|     for a in argv[2:]:
865|         nv = a.split('=', 2)
866|         kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
867|         Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
868|     try:
869|         obj = dispatch(cmd, **kwargs)
870|         print()
871|         print(obj)
872|         Log.info()
873|         Log.info(obj)
874|     except:
875|         etype, evalue, tb = exc_info()
876|         ts = extract_tb(tb)
877|         Log.error()
878|         Log.error('%s: %s' % (etype.__name__,str(evalue)))
879|         for t in ts[-5:]:
880|             Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
881|             Log.error(' %s' % t[3])
882|         raise
883|     finally:
884|         Log.end()
885| ####################################################################################################
886| '''eof'''
887|