charts.html


001| ####################################################################################################
002| from sys import argv, stdout
003| from os import path
004| from math import exp, log
005| from matplotlib import pyplot as plt
006| from matplotlib.ticker import NullLocator, FixedLocator, MultipleLocator
007| from matplotlib.ticker import NullFormatter, FixedFormatter, FormatStrFormatter
008| from goodies import typename, KWARG, divisions, intervals
009| ####################################################################################################
010| BasePath = r'C:\CJS\prj\Python\blog\substack\ESC'
011| DataPath = path.join(BasePath, 'dat')
012| TextPath = path.join(BasePath, 'txt')
013| ImgsPath = path.join(BasePath, 'img')
014| ZipsPath = path.join(BasePath, 'zip')
015| FileName = 'command.txt'
016| 
017| 
018| ##================================================================================================##
019| def chart_meaningful_complexity (fname='meaningful-complexity.png', fpath=BasePath):
020|     f = lambda x: exp(-pow(x,2)/12)
021|     #
022|     fig, ax = plt.subplots()
023|     fig.set_figwidth(6.0)
024|     fig.set_figheight(3.0)
025|     fig.subplots_adjust(left=0.060, right=0.98, bottom=0.12, top=0.90)
026|     fig.suptitle('Meaningful Complexity', color='#7f007f', fontsize=15, fontweight='bold')
027| 
028|     ax.set_xlabel('Complexity', color='#0000cc', fontsize=11, fontweight='bold')
029|     ax.set_xlim(-10.0, +10.0)
030|     ax.xaxis.set_major_locator(MultipleLocator(5))
031|     ax.xaxis.set_minor_locator(MultipleLocator(1.0))
032|     ax.xaxis.set_major_formatter(NullFormatter())
033|     ax.xaxis.set_minor_formatter(NullFormatter())
034| 
035|     ax.set_ylabel('"Meaning"', color='#cc0000', fontsize=11, fontweight='bold', fontstyle='italic')
036|     ax.set_ylim(0, +1.2)
037|     ax.yaxis.set_major_locator(MultipleLocator(1.0))
038|     ax.yaxis.set_minor_locator(MultipleLocator(0.1))
039|     ax.yaxis.set_major_formatter(NullFormatter())
040|     ax.yaxis.set_minor_formatter(NullFormatter())
041| 
042|     ax.grid(which='major', axis='both', color='#7f7f9f', lw=1.4,ls='-',zorder=1, alpha=1.0)
043|     ax.grid(which='minor', axis='both', color='#7f7f9f', lw=0.5,ls='-',zorder=0, alpha=1.0)
044|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
045|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
046| 
047|     xs = list(intervals(-10, +10, 0.1))
048|     ys = [f(x) for x in xs]
049|     ax.plot(xs,ys, c='#990099', lw=5.0, alpha=1.0, zorder=11)
050|     ax.fill_between(xs,0,ys, color='#df99df', alpha=0.50, zorder=10)
051| 
052|     fn = path.join(fpath,fname) if fpath else fname
053|     plt.savefig(fn)
054|     plt.close(fig)
055|     print(f'wrote: {fn}')
056|     return (xs, ys)
057| 
058| ##================================================================================================##
059| def chart_complexity_over_time (fname='complexity-time.png', fpath=BasePath):
060|     f = lambda x: exp(-pow(x,2)/12)
061|     g = lambda x: (x+10.0)/20.0
062|     #
063|     fig, ax = plt.subplots()
064|     fig.set_figwidth(6.0)
065|     fig.set_figheight(3.0)
066|     fig.subplots_adjust(left=0.060, right=0.945, bottom=0.12, top=0.90)
067|     fig.suptitle('Complexity Over Time', color='#000066', fontsize=15, fontweight='bold')
068| 
069|     ax.set_xlabel('Time', color='#00661f', fontsize=11, fontweight='bold')
070|     ax.set_xlim(-10.0, +10.0)
071|     ax.xaxis.set_major_locator(MultipleLocator(5))
072|     ax.xaxis.set_major_formatter(NullFormatter())
073|     ax.xaxis.set_minor_locator(MultipleLocator(1.0))
074|     ax.xaxis.set_minor_formatter(NullFormatter())
075| 
076|     ax.set_ylabel('Complexity', color='#cc0000', fontsize=11, fontweight='bold')
077|     ax.set_ylim(0, +1.1)
078|     ax.yaxis.set_major_locator(MultipleLocator(1.0))
079|     ax.yaxis.set_minor_locator(MultipleLocator(0.1))
080|     ax.yaxis.set_major_formatter(NullFormatter())
081|     ax.yaxis.set_minor_formatter(NullFormatter())
082|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
083|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
084| 
085|     ax.grid(which='major', axis='both', color='#7f7f9f', lw=1.4,ls='-',zorder=1, alpha=1.0)
086|     ax.grid(which='minor', axis='both', color='#7f7f9f', lw=0.5,ls='-',zorder=0, alpha=1.0)
087| 
088|     ax2 = ax.twinx()
089|     ax2.set_ylabel('Entropy / Randomness', color='#007f00', fontsize=11, fontweight='bold')
090|     ax2.set_ylim(0.0, +1.1)
091|     ax2.yaxis.set_major_locator(MultipleLocator(1.0))
092|     ax2.yaxis.set_minor_locator(MultipleLocator(0.1))
093|     ax2.yaxis.set_major_formatter(NullFormatter())
094|     ax2.yaxis.set_minor_formatter(NullFormatter())
095|     ax2.tick_params(which='major', axis='both', direction='out', color='#000000')
096|     ax2.tick_params(which='minor', axis='both', direction='out', color='#000000')
097| 
098|     xs  = list(intervals(-10, +10, 0.1))
099|     ys1 = [f(x) for x in xs]
100|     ax.plot(xs,ys1, c='#cc0000', lw=5.0, alpha=1.0, zorder=12)
101|     ax.fill_between(xs,0,ys1, color='#df7f7f', alpha=0.50, zorder=11)
102| 
103|     ys2 = [g(x) for x in xs]
104|     ax.plot(xs,ys2, c='#009933', lw=5.0, alpha=1.0, zorder=21)
105| 
106|     fn = path.join(fpath,fname) if fpath else fname
107|     plt.savefig(fn)
108|     plt.close(fig)
109|     print(f'wrote: {fn}')
110|     return (xs, ys1, ys2)
111| 
112| ##================================================================================================##
113| def chart_determining_ef (fname='determing-ef.png', fpath=BasePath):
114|     xs = list(range(1,11))
115|     ys = [8+(8*x) for x in xs]
116| 
117|     fig, ax = plt.subplots()
118|     fig.set_figwidth(6.0)
119|     fig.set_figheight(3.0)
120|     fig.subplots_adjust(left=0.125, right=0.985, bottom=0.110, top=0.91)
121|     fig.suptitle('Determining Emergence Factor', fontsize='12', fontweight='bold')
122| 
123|     ax.set_xlim(0.5, 10.5)
124|     ax.set_ylim(0, 100)
125|     ax.xaxis.set_major_locator(MultipleLocator(1))
126|     ax.xaxis.set_minor_locator(MultipleLocator(0.5))
127|     ax.yaxis.set_major_locator(MultipleLocator(10))
128|     ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
129|     ax.grid(which='minor', axis='x', color='#7f7fbf', lw=0.8,zorder=1, alpha=1.0)
130| 
131|     hs = [ys[-1]-y for y in ys]
132|     bs = [y-ys[0] for y in ys]
133|     ax.bar(xs,bottom=ys,height=hs   , color='#00bf00', width=1.0, edgecolor='#000000', linewidth=1.0, alpha=0.50, zorder=11)
134|     ax.bar(xs,height=bs,bottom=ys[0], color='#bf0000', width=1.0, edgecolor='#000000', linewidth=1.0, alpha=0.50, zorder=11)
135|     ax.bar(xs,height=ys[0],bottom=0 , color='#3f7fbf', width=1.0, edgecolor='#000000', linewidth=1.0, alpha=0.50, zorder=11)
136| 
137|     ax.axhline(ys[0] , color='#000000', lw=2.0, alpha=0.75, zorder=21)
138|     ax.axhline(ys[-1], color='#000000', lw=2.0, alpha=0.75, zorder=22)
139| 
140|     fn = path.join(fpath,fname) if fpath else fname
141|     plt.savefig(fn)
142|     plt.close(fig)
143|     print(f'wrote: {fn}')
144| 
145|     return (xs, ys)
146| 
147| ##================================================================================================##
148| def chart_log_lin_exp (fname='lin-function.png', fpath=BasePath):
149|     from emerg_struc_compl import ESC
150| 
151|     log_curve = lambda x: (log(1+pow(x,0.60)) * 3.3219281)
152|     lin_curve = lambda x: x
153|     exp_curve = lambda x: pow(x,3)
154| 
155|     def do_lin_curve (name, start, step, incr, length=11):
156|         buf = []
157|         for base in range(1,25):
158|             nmin = start + (base * step)
159|             nums = list([nmin+(base*incr*lin_curve(ix)) for ix in range(length)])
160|             obj = ESC(name, nums)
161|             print(obj)
162|             buf.append(obj)
163|         print()
164|         return buf
165| 
166|     def do_log_curve (name, start, step, incr, length=11):
167|         buf = []
168|         for base in range(1,25):
169|             nmin = int(start + (base * step))
170|             nums = list([nmin+(base*incr*log_curve(ix/length)) for ix in range(length)])
171|             obj = ESC(name, nums)
172|             print(obj)
173|             buf.append(obj)
174|         print()
175|         return buf
176| 
177|     def do_exp_curve (name, start, step, incr, length=11):
178|         buf = []
179|         for base in range(1,25):
180|             nmin = int(start + (base * step))
181|             nums = list([nmin+(base*incr*exp_curve(ix/length)) for ix in range(length)])
182|             obj = ESC(name, nums)
183|             print(obj)
184|             buf.append(obj)
185|         print()
186|         return buf
187| 
188|     # -------------------------------------------
189|     # Tests for the curve generation functions...
190|     # -------------------------------------------
191|     #rv = do_lin_curve('lin_005k_025k_010k', 5_000, 25_000, 10_000)
192|     #rv = do_log_curve('log090_005k_025k_020k', 5_000, 25_000, 20_000)
193|     #rv = do_exp_curve('exp4_005k_025k_010k', 5_000, 25_000, 10_000)
194| 
195|     def do_plot (function, name, start, step, incr, length=11, fname=fname):
196|         yss = function(name, start, step, incr, length=length)
197|         ys = yss[-1].V
198|         xs = list(range(len(ys)))
199|         for x,y in zip(xs,ys):
200|             print(f'{x}: {int(y)}')
201|         print()
202| 
203|         fig, ax = plt.subplots()
204|         fig.set_figwidth(6.0)
205|         fig.set_figheight(3.0)
206|         fig.subplots_adjust(left=0.125, right=0.985, bottom=0.110, top=0.91)
207|         fig.suptitle(f'{name.title()} Function', fontsize='12', fontweight='bold')
208| 
209|         ax.set_xlim(0, len(xs)-1)
210|         ax.xaxis.set_major_locator(MultipleLocator(1))
211|         ax.yaxis.set_major_formatter(FormatStrFormatter('%.1e'))
212|         ax.grid(which='major', axis='both', color='#7f7fbf', lw=0.8,zorder=1, alpha=1.0)
213| 
214|         ax.fill_between(xs, ys[0] ,ys, color='#bf7f3f', alpha=0.50, zorder=11)
215|         ax.fill_between(xs, ys[-1],ys, color='#7fbf3f', alpha=0.50, zorder=12)
216|         ax.axhline(ys[0] , color='#7f3f1f', lw=1.0, alpha=0.75, zorder=21)
217|         ax.axhline(ys[-1], color='#3f7f1f', lw=1.0, alpha=0.75, zorder=22)
218|         ax.plot(xs,ys, c='#bf0000', lw=2.5, alpha=1.00, zorder=51)
219| 
220|         fn = path.join(fpath,fname) if fpath else fname
221|         plt.savefig(fn)
222|         plt.close(fig)
223|         print(f'wrote: {fn}')
224|         print()
225|         return (xs, ys, yss)
226| 
227|     # ----------------------------------------
228|     # Create a chart from a generated curve...
229|     # ----------------------------------------
230|     rv = do_plot(do_lin_curve, 'lin', 5_000, 5_000, 50_000, fname='lin-function.png', length=21)
231|     rv = do_plot(do_log_curve, 'log', 5_000, 5_000, 50_000, fname='log-function.png', length=21)
232|     rv = do_plot(do_exp_curve, 'exp', 5_000, 5_000, 50_000, fname='exp-function.png', length=21)
233|     return rv
234| 
235| ##================================================================================================##
236| def do_test ():
237|     return ()
238| 
239| ##================================================================================================##
240| def main (*args):
241|     rv = ()
242|     #rv = chart_meaningful_complexity()
243|     #rv = chart_complexity_over_time()
244|     #rv = chart_determining_ef()
245|     #rv = chart_log_lin_exp()
246|     rv = do_test()
247|     return rv
248| ####################################################################################################
249| if __name__ == '__main__':
250|     print(argv[0])
251|     print()
252|     rv = main(*argv[1:])
253|     print()
254| ####################################################################################################
255| '''eof'''
256|