1313from ..__info__ import __version__
1414
1515
16+ """
17+ Curve fitting:
18+
19+ >>> import matplotlib.pyplot as plt
20+ >>> import pandas as pd
21+ >>> import scipy.optimize
22+ >>> from statistics import mean
23+ >>> from tinyscript import random
24+ >>> x, y = [], []
25+ >>> for i in range(2, 256):
26+ v = []
27+ for j in range(16, 2048, 16):
28+ s = random.randstr(j)
29+ v.append(float(len(codext.encode(s, "base%d-generic" % i))) / len(s))
30+ x.append(i)
31+ y.append(mean(v))
32+ >>> data = pd.DataFrame({'base': x, 'expf': y})
33+ >>> def fit(x, y, func, params):
34+ params, cv = scipy.optimize.curve_fit(func, x, y, params)
35+ print(params)
36+ y2 = func(x, *params)
37+ plt.clf()
38+ plt.plot(x, y, ".", color="blue", alpha=.3)
39+ plt.plot(x, y2, color="red", linewidth=3.0)
40+ plt.show()
41+ >>> fit(data['base'], data['expf'], lambda x, a, b, c, d: a / (x**b + c) + d, (1, 1, 1, 1))
42+ [ 0.02841434 0.00512664 -0.99999984 0.01543879]
43+ >>> fit(data['base'], data['expf'], lambda x, a, b, c, d: a / (x**b + c) + d, (.028, .005, -1, .015))
44+ [ 0.02827357 0.00510124 -0.99999984 0.01536941]
45+ """
46+ EXPANSION_FACTOR = lambda base : 0.02827357 / (base ** 0.00510124 - 0.99999984 ) + 0.01536941
47+
48+
1649class BaseError (ValueError ):
1750 pass
1851
@@ -144,6 +177,7 @@ def _decode(input, errors="strict"):
144177
145178 kwargs ['len_charset' ] = n
146179 kwargs ['printables_rate' ] = float (len ([c for c in cs if c in printable ])) / len (cs )
180+ kwargs ['expansion_factor' ] = kwargs .pop ('expansion_factor' , (EXPANSION_FACTOR (n ), .05 ))
147181 n = "base{}" .format (n ) if name is None else name
148182 kwargs ['guess' ] = kwargs .get ('guess' , [n ])
149183 add (n , encode , decode , pattern , entropy = nb , ** kwargs )
@@ -167,7 +201,8 @@ def _decode(input, errors="strict"):
167201
168202 add ("base" , encode , decode , r"^base[-_]?([2-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?:[-_]generic)?$" ,
169203 guess = ["base%d-generic" % i for i in range (2 , 255 )], entropy = lambda e , n : log (int (n .split ("-" )[0 ][4 :]), 2 ),
170- len_charset = lambda n : int (n .split ("-" )[0 ][4 :]), printables_rate = 1. , category = "base-generic" , penalty = .4 )
204+ len_charset = lambda n : int (n .split ("-" )[0 ][4 :]), printables_rate = 1. , category = "base-generic" , penalty = .4 ,
205+ expansion_factor = lambda f , n : (EXPANSION_FACTOR (int (n .split ("-" )[0 ][4 :])), .05 ))
171206
172207
173208def main (n , ref = None , alt = None , inv = True ):
0 commit comments