/timm's /charming /python /tricks
Download
log.py.
Read more on How to be Charming (in Python).
001: import sys
002: sys.dont_write_bytecode=True # no .pyc files
003: from the import *
004: from about import *
005: from lib import *
006:
007: class Nums(AutoDict):
008: def __init__(i):
009: AutoDict.__init__(i,Num)
010:
011: stop=StopIteration
012:
013: class Control:
014: def __init__(i,
015: iter=range(0,100),
016: epsilon= 0.005,
017: also=None,
018: era= 100,
019: cohen=The.math.cohen,
020: haltOn=None,
021: cmp=lambda old, new: new - old):
022: i.iter=iter
023: i.era = era
024: i.epsilon = epsilon
025: i.all = Nums()
026: i.now = Nums()
027: i.also = also
028: i.cohen = cohen
029: i.haltOn = haltOn
030: i.cmp = cmp
031: def thisEra(i,tick):
032: return int(tick/i.era)*i.era
033: def small(i,where) :
034: if not where in i.all: 1/0
035: return i.all[where].s*i.cohen
036: def enough(i,where,when) :
037: if (where,when) in i.now:
038: val = i.now[(where,when)].mu
039: return val > (1 - i.epsilon)
040: else:
041: return False
042: def done(i,when):
043: where = i.haltOn
044: if where:
045: if i.enough(where,when) or \
046: not i.improving(where,when):
047: return True
048: return False
049: def improving(i,where,when):
050: before = when - i.era
051: if (where,before) in i.now and \
052: (where,when) in i.now:
053: new = i.now[(where,when)]
054: old = i.now[(where,before)]
055: return i.cmp(old.mu,new.mu) > i.small(where)
056: return False
057: def seen(i,tick,**d):
058: when = i.thisEra(tick)
059: for where,what in d.items():
060: i.seen1(when,what,where)
061: def seen1(i,when,what,where):
062: if i.also:
063: i.also.seen1(when,what,where)
064: i.all[where].seen(what)
065: i.now[(where, when)].seen(what)
066: def loop(i):
067: before = 0
068: for tick in i.iter:
069: now = i.thisEra(tick)
070: if before and now != before:
071: if i.done(before):
072: break
073: before = now
074: yield tick
075:
076:
077: def controlled(control,lo,hi,
078: key =lambda z:'%10s' % z,
079: value=lambda z: '%2d' % z
080: ):
081: d = control.now
082: wheres= {}
083: whens = {}
084: for where,when in d.keys():
085: wheres[where] = 1
086: whens[ when ] = 1
087: wheres = sorted(wheres.keys())
088: whens = sorted(whens.keys())
089: for where in wheres:
090: print '\n------|',str(where),\
091: '|----------------------------------\n'
092: for when in whens:
093: if (where,when) in d:
094: all = d[(where,when)].cached()
095: s = "?"
096: if len(all) > 5:
097: s= xtile(all,show = value,
098: lo=lo,hi=hi)
099: print '%10s' % key(when),\
100: '[%5s]' % len(all), s
101:
102: """
103: ### xtile: Pretty Print Distributions
104:
105: The function _xtile_ takes a list of (possibly) unsorted numbers and
106: presents them as a horizontal xtile chart (in ascii format). The default
107: is a contracted _quintile_ that shows the 10,30,50,70,90 breaks in the
108: data (but this can be changed- see the optional flags of the function).
109:
110: """
111: def xtile(lst,lo=0,hi=100,width=50,
112: chops=[0.1 ,0.3,0.5,0.7,0.9],
113: marks=["-" ," "," ","-"," "],
114: bar="|",star="*",show= lambda s:" %3.0f" % s):
115: def pos(p) : return ordered[int(len(lst)*p)]
116: def place(x) :
117: tmp= int(width*float((x - lo))/(hi - lo))
118: if tmp == width: tmp += -1
119: return tmp
120: def pretty(lst) :
121: return ', '.join([show(x) for x in lst])
122: ordered = sorted(lst)
123: lo = min(lo,ordered[0])
124: hi = max(hi,ordered[-1])
125: what = [pos(p) for p in chops]
126: where = [place(n) for n in what]
127: out = [" "] * width
128: for one,two in pairs(where):
129: for i in range(one,two):
130: out[i] = marks[0]
131: marks = marks[1:]
132: out[int(width/2)] = bar
133: loc = place(pos(0.5))
134: #print loc, len(out)
135: out[loc] = star
136: return ''.join(out) + "," + pretty(what)
137:
138: def _tileX() :
139: import random
140: random.seed(1)
141: nums = [random.random()**2 for _ in range(100)]
142: print xtile(nums,lo=0,hi=1.0,width=25,show= lambda s:" %3.2f" % s)
143:
144:
145: #c = Control(haltOn='a',era=200,hi=1000,val='%5.2f')
146: #for x in c:
147: # c.seen(x,f(x**0.25),'a')
148: def _control():
149: def f(t): return 1.0/(1 + e**(-1*t))
150: c = Control(range(0,100))
151: for x in c.loop():
152: c.seens(x/20,seen=f(x**0.1))
153: print x
154: controlled(c,0,1,
155: value=lambda s: '%4.2f' % s)
156:
157:
158:
This file is part of Timm's charming Python tricks.
© 2014, Tim Menzies:
tim.menzies@gmail.com,
http://menzies.us.
Timm's charming Python tricks are free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Timm's charming Python tricks are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser Public License along with Foobar. If not, see http://www.gnu.org/licenses.