@bell-curves.html
001| """Bell Curves.
001|
001| Commands:
001| c1 do_basic_bell_curve
001| c2 do_two_camps_curve
001| c3 do_many_camps_curve
001| c4 do_outliers_curve
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, exp
007| from random import random
008| import matplotlib.pyplot as plt
009| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
010| from logger import logger, info, debug, trace
011| from goodies import divisions
012|
013| BasePath = r'C:\CJS\prj\Python\blog\proj'
014| ImgsPath = path.join(BasePath, 'img')
015| Log = logger('Gaussian')
016|
017| CMND = ''
018| ARGS = {
019|
020|
021|
022| 'fpath': BasePath,
023| }
024|
025| I2PI = complex(0,1) * complex(2.0*pi,0)
026|
027|
028| def generate_chart (plotter, **kwargs):
029| Log.info('generate-chart:: %s' % str(list(kwargs.keys())))
030| fpath = kwargs['fpath']
031| fname = kwargs['fname']
032| title = kwargs['title'] if 'title' in kwargs else 'Gaussian Distribution #1'
033| xname = kwargs['xname'] if 'xname' in kwargs else 'Subject Space'
034| yname = kwargs['yname'] if 'yname' in kwargs else 'Concordance'
035| xmin = float(kwargs['xmin']) if 'xmin' in kwargs else -10.0
036| xmax = float(kwargs['xmax']) if 'xmax' in kwargs else +10.0
037| ymin = float(kwargs['ymin']) if 'ymin' in kwargs else 0.0
038| ymax = float(kwargs['ymax']) if 'ymax' in kwargs else +1.1
039| Log.info('fpath="%s"' % fpath)
040| Log.info('fname="%s"' % fname)
041| Log.info('title="%s"' % title)
042| Log.info('xname="%s"' % xname)
043| Log.info('yname="%s"' % yname)
044| Log.info('xlimit=[%.2f, %.2f]' % (xmin,xmax))
045| Log.info('ylimit=[%.2f, %.2f]' % (ymin,ymax))
046| Log.info()
047| filename = path.join(fpath, fname)
048|
049| fig, ax = plt.subplots()
050| fig.suptitle(title, fontsize='xx-large', fontweight='bold')
051|
052| ax.set_xlabel(xname, fontsize='x-large', fontweight='bold')
053| ax.set_xlim(xmin, xmax)
054| ax.xaxis.set_major_locator(MultipleLocator(10.0))
055| ax.xaxis.set_minor_locator(MultipleLocator(1.0))
056| ax.xaxis.set_major_formatter(NullFormatter())
057|
058| ax.set_ylabel(yname, fontsize='large', fontweight='bold')
059| ax.set_ylim(ymin, ymax)
060| ax.yaxis.set_major_locator(MultipleLocator(1.0))
061| ax.yaxis.set_minor_locator(MultipleLocator(0.1))
062| ax.yaxis.set_major_formatter(NullFormatter())
063|
064| ax.grid(which='major', axis='both', color='#9f9f9f', lw=2.0,ls='-',zorder=1, alpha=1.0)
065| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=1.0,ls='-',zorder=0, alpha=1.0)
066| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
067| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
068|
069| plotter(ax)
070|
071| fig.set_figwidth(8.0)
072| fig.set_figheight(4.0)
073| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.11, top=0.91)
074| fig.savefig(filename)
075|
076|
077| def do_outliers_curve (**kwargs):
078| Log.info('outliers-curve:: %s' % str(list(kwargs.keys())))
079| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
080| fname = kwargs['fname'] if 'fname' in kwargs else 'bell-curve-4.png'
081| title = kwargs['title'] if 'title' in kwargs else 'Mainstream & Outliers'
082| width = float(kwargs['width']) if 'width' in kwargs else 2.5
083| Log.info('fpath="%s"' % fpath)
084| Log.info('fname="%s"' % fname)
085| Log.info('title="%s"' % title)
086| Log.info('width=%f' % width)
087| Log.info()
088| F0 = lambda x: exp(-pow(x-0.0,2)/pow(width,2))
089| F1 = lambda x: 0.420*exp(-pow(x-3.5,2)/pow(1.00,2))
090| F2 = lambda x: 0.250*exp(-pow(x-10.0,2)/pow(0.66,2))
091|
092| def plotter (ax):
093| xs = divisions(-5.0, +15.0, 2001)
094| ys0 = [F0(x) for x in xs]
095| ys1 = [F1(x) for x in xs]
096| ys2 = [F2(x) for x in xs]
097| ax.fill_between(xs,ys0,0, color='#0000ff', zorder=11, alpha=1.0, lw=1.0)
098| ax.fill_between(xs,ys1,0, color='#ff0000', zorder=13, alpha=1.0, lw=1.0)
099| ax.fill_between(xs,ys2,0, color='#00ff00', zorder=15, alpha=1.0, lw=1.0)
100| ax.plot(xs,ys0, c='#000000', zorder=12, alpha=1.0, lw=2.0)
101| ax.plot(xs,ys1, c='#000000', zorder=14, alpha=1.0, lw=1.0)
102| ax.plot(xs,ys2, c='#000000', zorder=16, alpha=1.0, lw=1.0)
103|
104| kwargs['fpath'] = fpath
105| kwargs['fname'] = fname
106| kwargs['title'] = title
107| kwargs['xmin'] = -5.0
108| kwargs['xmax'] = +15.0
109| generate_chart(plotter, **kwargs)
110|
111| Log.info()
112| return 'Done'
113|
114| def do_many_camps_curve (**kwargs):
115| Log.info('many-camps-curve:: %s' % str(list(kwargs.keys())))
116| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
117| fname = kwargs['fname'] if 'fname' in kwargs else 'bell-curve-3.png'
118| title = kwargs['title'] if 'title' in kwargs else 'Many Camps of Thought'
119| width = float(kwargs['width']) if 'width' in kwargs else 1.50
120| Log.info('fpath="%s"' % fpath)
121| Log.info('fname="%s"' % fname)
122| Log.info('title="%s"' % title)
123| Log.info('width=%f' % width)
124| Log.info()
125| factor = lambda: (0.75+(-0.10+(random()*0.20)))
126| FF = lambda cp,height: lambda x:height*exp(-pow(x-cp,2)/pow(width,2))
127| F1 = FF(-8.0,factor())
128| F2 = FF(-4.0,factor())
129| F3 = FF( 0.0,factor())
130| F4 = FF(+4.0,factor())
131| F5 = FF(+8.0,factor())
132|
133| def plotter (ax):
134| xs = divisions(-10.0, +10.0, 2001)
135| ys1 = [F1(x) for x in xs]
136| ys2 = [F2(x) for x in xs]
137| ys3 = [F3(x) for x in xs]
138| ys4 = [F4(x) for x in xs]
139| ys5 = [F5(x) for x in xs]
140| ax.fill_between(xs,ys1,0, color='#ff0000', zorder=11, alpha=0.80, lw=2.0)
141| ax.fill_between(xs,ys2,0, color='#ff7f00', zorder=12, alpha=0.80, lw=2.0)
142| ax.fill_between(xs,ys3,0, color='#007f00', zorder=13, alpha=0.80, lw=2.0)
143| ax.fill_between(xs,ys4,0, color='#007fff', zorder=14, alpha=0.80, lw=2.0)
144| ax.fill_between(xs,ys5,0, color='#0000ff', zorder=15, alpha=0.80, lw=2.0)
145| ax.plot(xs,ys1, c='#00007f', zorder=21, alpha=1.00, lw=2.0)
146| ax.plot(xs,ys2, c='#00007f', zorder=22, alpha=1.00, lw=2.0)
147| ax.plot(xs,ys3, c='#00007f', zorder=23, alpha=1.00, lw=2.0)
148| ax.plot(xs,ys4, c='#00007f', zorder=24, alpha=1.00, lw=2.0)
149| ax.plot(xs,ys5, c='#00007f', zorder=25, alpha=1.00, lw=2.0)
150|
151| kwargs['fpath'] = fpath
152| kwargs['fname'] = fname
153| kwargs['title'] = title
154| generate_chart(plotter, **kwargs)
155|
156| Log.info()
157| return 'Done'
158|
159| def do_two_camps_curve (**kwargs):
160| Log.info('two-camps-curve:: %s' % str(list(kwargs.keys())))
161| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
162| fname = kwargs['fname'] if 'fname' in kwargs else 'bell-curve-2.png'
163| title = kwargs['title'] if 'title' in kwargs else 'Two Camps of Thought'
164| width = float(kwargs['width']) if 'width' in kwargs else 2.00
165| Log.info('fpath="%s"' % fpath)
166| Log.info('fname="%s"' % fname)
167| Log.info('title="%s"' % title)
168| Log.info('width=%f' % width)
169| Log.info()
170| F1 = lambda x: 0.95*exp(-pow(x+5.0,2)/pow(width,2))
171| F2 = lambda x: 0.85*exp(-pow(x-5.0,2)/pow(width,2))
172|
173| def plotter (ax):
174| xs = divisions(-10.0, +10.0, 2001)
175| ys1 = [F1(x) for x in xs]
176| ys2 = [F2(x) for x in xs]
177| ax.fill_between(xs,ys1,0, color='#0000ff', zorder=11, alpha=0.80, lw=3.0)
178| ax.fill_between(xs,ys2,0, color='#ff0000', zorder=12, alpha=0.80, lw=3.0)
179| ax.plot(xs,ys1, c='#00007f', zorder=11, alpha=1.00, lw=3.0)
180| ax.plot(xs,ys2, c='#7f0000', zorder=12, alpha=1.00, lw=3.0)
181|
182| kwargs['fpath'] = fpath
183| kwargs['fname'] = fname
184| kwargs['title'] = title
185| generate_chart(plotter, **kwargs)
186|
187| Log.info()
188| return 'Done'
189|
190| def do_basic_bell_curve (**kwargs):
191| Log.info('basic-bell-curve:: %s' % str(list(kwargs.keys())))
192| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
193| fname = kwargs['fname'] if 'fname' in kwargs else 'bell-curve-1.png'
194| title = kwargs['title'] if 'title' in kwargs else 'Single Mainstream Thought'
195| width = float(kwargs['width']) if 'width' in kwargs else 3.0
196| Log.info('fpath="%s"' % fpath)
197| Log.info('fname="%s"' % fname)
198| Log.info('title="%s"' % title)
199| Log.info('width=%f' % width)
200| Log.info()
201| F0 = lambda x: exp(-pow(x-0.0,2)/pow(width,2))
202| F1 = lambda x: exp(-pow(x-0.0,2)/pow(0.50,2))
203| F2 = lambda x: exp(-pow(x-0.0,2)/pow(8.0,2))
204|
205| def plotter (ax):
206| xs = divisions(-10.0, +10.0, 2001)
207| ys0 = [F0(x) for x in xs]
208| ys1 = [F1(x) for x in xs]
209| ys2 = [F2(x) for x in xs]
210| ax.fill_between(xs,ys2,0, color='#00ff00', zorder=11, alpha=1.0, lw=1.0)
211| ax.fill_between(xs,ys0,0, color='#0000ff', zorder=12, alpha=1.0, lw=1.0)
212| ax.fill_between(xs,ys1,0, color='#ff0000', zorder=13, alpha=1.0, lw=1.0)
213| ax.plot(xs,ys2, c='#000000', zorder=14, alpha=1.0, lw=1.0)
214| ax.plot(xs,ys0, c='#000000', zorder=15, alpha=1.0, lw=2.0)
215| ax.plot(xs,ys1, c='#000000', zorder=16, alpha=1.0, lw=1.0)
216|
217| kwargs['fpath'] = fpath
218| kwargs['fname'] = fname
219| kwargs['title'] = title
220| generate_chart(plotter, **kwargs)
221|
222| Log.info()
223| return 'Done'
224|
225| def do_test (**kwargs):
226| Log.info('test:: %s' % str(list(kwargs.keys())))
227| fpath = kwargs['fpath'] if 'fpath' in kwargs else BasePath
228| fname = kwargs['fname'] if 'fname' in kwargs else 'bell-curve.png'
229| title = kwargs['title'] if 'title' in kwargs else 'Bell Curve Test'
230| width = float(kwargs['width']) if 'width' in kwargs else 3.0
231| center = float(kwargs['center']) if 'center' in kwargs else 0.0
232| Log.info('fpath="%s"' % fpath)
233| Log.info('fname="%s"' % fname)
234| Log.info('title="%s"' % title)
235| Log.info('width=%f' % width)
236| Log.info('center=%f' % center)
237| Log.info()
238| F = lambda x: exp(-pow(x-center,2)/pow(width,2))
239|
240| def plotter (ax):
241| xs = divisions(-10.0, +10.0, 2001)
242| ys = [F(x) for x in xs]
243| ax.fill_between(xs,ys,0, color='#0000ff', zorder=11, alpha=0.75, lw=1.0)
244| ax.plot(xs,ys, c='#000000', zorder=12, alpha=1.0, lw=1.0)
245|
246| kwargs['fpath'] = fpath
247| kwargs['fname'] = fname
248| kwargs['title'] = title
249| generate_chart(plotter, **kwargs)
250|
251| Log.info()
252| return 'Done'
253|
254| def dispatch (cmd, **kwargs):
255| Log.info('command: %s' % cmd)
256| for kw in kwargs:
257| Log.info('argument[%s]: %s' % (kw,kwargs[kw]))
258| Log.info()
259| if cmd == 'c1': return do_basic_bell_curve(**kwargs)
260| if cmd == 'c2': return do_two_camps_curve(**kwargs)
261| if cmd == 'c3': return do_many_camps_curve(**kwargs)
262| if cmd == 'c4': return do_outliers_curve(**kwargs)
263| if cmd == 'test': return do_test(**kwargs)
264| return 'Nothing to do!'
265|
266| if __name__ == '__main__':
267| print('autorun: %s' % argv[0])
268| Log.start(path.join(BasePath,'bell-curves.log'))
269| Log.level(debug())
270| cmd = argv[1] if 1 < len(argv) else CMND
271| kwargs = ARGS
272| for a in argv[2:]:
273| nv = a.split('=', 2)
274| kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
275| Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
276| try:
277| print(cmd)
278| print()
279| obj = dispatch(cmd, **kwargs)
280| print()
281| print(obj)
282| Log.info()
283| Log.info(obj)
284| except:
285| etype, evalue, tb = exc_info()
286| ts = extract_tb(tb)
287| Log.error()
288| Log.error('%s: %s' % (etype.__name__,str(evalue)))
289| for t in ts[-5:]:
290| Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
291| Log.error(' %s' % t[3])
292| raise
293| finally:
294| Log.end()
295|
296| '''eof'''
297|