Mercurial > ~darius > hgwebdir.cgi > beermon.old
annotate logplot.py @ 16:f1832dec26e3 default tip
Move OWReadError to MonitorDev since that's where it's raised.
author | darius |
---|---|
date | Tue, 23 Oct 2007 01:05:21 +0000 |
parents | de698afbe6fc |
children |
rev | line source |
---|---|
10 | 1 #!/usr/bin/env python |
2 | |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
3 import re, datetime, time, sys, cPickle, numpy |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
4 import matplotlib |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
5 import matplotlib.figure |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
6 import matplotlib.backends.backend_agg |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
7 import matplotlib.dates |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
8 |
10 | 9 daterestr = '([0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}): ' |
10 targtre = re.compile(daterestr + 'target temperature - (-?[0-9]+\.[0-9]+)') | |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
11 templre = re.compile(daterestr + '(-?[0-9]+\.[0-9]+)\s+(-?[0-9]+\.[0-9]+)\s+(-?[0-9]+\.[0-9]+)\s+([a-z-]+)\s+([a-z-]+)') |
10 | 12 datefmt = '%Y/%m/%d %H:%M:%S' |
13 tz = matplotlib.pytz.timezone('Australia/Adelaide') | |
14 | |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
15 #start = time.mktime(time.strptime("2007/09/22 14:00:00", datefmt)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
16 start = None |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
17 #end = time.mktime(time.strptime("2007/09/30 14:00:00", datefmt)) |
10 | 18 end = None |
19 | |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
20 def parselog(f, start, end, times, fermTemps, fridgeTemps, ambTemps, targetTemps, states): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
21 targetTemp = 18.0 # Have to guess this.. |
10 | 22 |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
23 for line in f: |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
24 # Log line? |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
25 m = templre.match(line) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
26 if (m == None): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
27 # No, might be a target temp line |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
28 m = targtre.match(line) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
29 if (m != None): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
30 # Parse date portion |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
31 t = datetime.datetime(*time.strptime(m.group(1), datefmt)[0:6]) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
32 if ((start != None and t < start) or (end != None and t > end)): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
33 continue |
10 | 34 |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
35 targetTemp = float(m.group(2)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
36 continue |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
37 else: |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
38 # Isn't anything, next! |
10 | 39 continue |
40 | |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
41 # Parse date portion |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
42 t = datetime.datetime(*time.strptime(m.group(1), datefmt)[0:6]) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
43 if ((start != None and t < start) or (end != None and t > end)): |
10 | 44 continue |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
45 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
46 fermTemp = float(m.group(2)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
47 fridgeTemp = float(m.group(3)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
48 ambTemp = float(m.group(4)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
49 state = m.group(5) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
50 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
51 # Convert to Gregorian days (float) because that is what matplotlib uses |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
52 times.append(matplotlib.dates._to_ordinalf(t)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
53 fermTemps.append(fermTemp) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
54 fridgeTemps.append(fridgeTemp) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
55 ambTemps.append(ambTemp) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
56 targetTemps.append(targetTemp) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
57 if (state == 'heat'): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
58 states.append(10) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
59 elif (state == 'idle'): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
60 states.append(8) |
10 | 61 else: |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
62 states.append(6) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
63 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
64 return(times, fermTemps, fridgeTemps, ambTemps, targetTemps, states) |
10 | 65 |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
66 def doplot(times, fermTemps, fridgeTemps, ambTemps, targetTemps, states, outfile): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
67 assert(len(times) == len(fermTemps)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
68 assert(len(times) == len(fridgeTemps)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
69 assert(len(times) == len(ambTemps)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
70 assert(len(times) == len(targetTemps)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
71 assert(len(times) == len(states)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
72 nrec = len(times) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
73 print "nrec = %d" % (nrec) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
74 newsz = [1000] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
75 then = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
76 times = rebin(numpy.array(times), newsz) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
77 fermTemps = rebin(numpy.array(fermTemps), newsz) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
78 fridgeTemps = rebin(numpy.array(fridgeTemps), newsz) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
79 ambTemps = rebin(numpy.array(ambTemps), newsz) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
80 targetTemps = rebin(numpy.array(targetTemps), newsz) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
81 states = rebin(numpy.array(states, 'f'), newsz) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
82 now = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
83 print "Rebinning took %.2f msec" % ((now - then) * 1000) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
84 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
85 title = "%s to %s (%d -> %d pts)" % (matplotlib.dates._from_ordinalf(times[0]).strftime(datefmt), |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
86 matplotlib.dates._from_ordinalf(times[-1]).strftime(datefmt), |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
87 nrec, len(times)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
88 print title |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
89 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
90 then = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
91 # A figure |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
92 fig = matplotlib.figure.Figure(figsize = (9.0, 5.0)) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
93 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
94 # Create a plot, get some axes |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
95 ax = fig.add_subplot(111) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
96 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
97 # Plot the data onto the axes |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
98 p = ax.plot(times, fermTemps, times, fridgeTemps, times, ambTemps, times, targetTemps, times, states, '.') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
99 ax.set_title(title, weight = 'bold') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
100 ax.set_autoscale_on(True) |
10 | 101 |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
102 # Add a formatter |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
103 hoursFmt = matplotlib.dates.DateFormatter('%d %b\n%H:%M') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
104 ax.xaxis.set_major_formatter(hoursFmt) |
10 | 105 |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
106 # Prettify it |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
107 ax.grid(True) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
108 ax.set_xlabel('Date') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
109 ax.set_ylabel('Temperature(C)') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
110 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
111 # Set the legend |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
112 legend = ax.legend((p[0], p[1], p[2], p[3], p[4]), ('Fermenter', 'Fridge', 'Ambient', 'Target', 'State'), 'best') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
113 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
114 # Render the figure |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
115 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
116 canvas.print_figure(outfile, dpi = 96) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
117 now = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
118 print "Plotting took %.2f msec" % ((now - then) * 1000) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
119 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
120 def main(): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
121 logf = open(sys.argv[1], 'r') |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
122 outfile = sys.argv[1][:-3] + 'png' |
10 | 123 |
14
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
124 try: |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
125 # Try reading the pickled data |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
126 p = cPickle.Unpickler(open("data.pck", "r")) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
127 then = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
128 times, fermTemps, fridgeTemps, ambTemps, targetTemps, states, offs = p.load() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
129 now = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
130 print "Unpickle took %.2f msec" % ((now - then) * 1000) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
131 # Seek up to the last place we parsed |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
132 logf.seek(offs) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
133 del p |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
134 print "Parsed pickle OK" |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
135 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
136 except (IOError, cPickle.UnpicklingError, ValueError): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
137 # Either the pickle doesn't exist, or it's invalid, start anew |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
138 print "Can't read pickle, starting from scratch" |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
139 times = [] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
140 fermTemps = [] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
141 fridgeTemps = [] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
142 ambTemps = [] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
143 states = [] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
144 targetTemps = [] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
145 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
146 then = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
147 parselog(logf, start, end, times, fermTemps, fridgeTemps, ambTemps, targetTemps, states) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
148 now = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
149 print "Parselog took %.2f msec" % ((now - then) * 1000) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
150 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
151 # Write the pickle back out |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
152 p = cPickle.Pickler(open("data.pck", "w"), protocol = -1) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
153 then = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
154 p.dump((times, fermTemps, fridgeTemps, ambTemps, targetTemps, states, logf.tell())) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
155 now = time.time() |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
156 print "Pickle took %.2f msec" % ((now - then) * 1000) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
157 del p |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
158 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
159 # Do the plot |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
160 doplot(times, fermTemps, fridgeTemps, ambTemps, targetTemps, states, outfile) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
161 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
162 def rebin(a, newshape): |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
163 '''Rebin an array to a new shape. |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
164 ''' |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
165 assert len(a.shape) == len(newshape) |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
166 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
167 slices = [slice(0, old, float(old)/new) for old,new in zip(a.shape,newshape) ] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
168 coordinates = numpy.mgrid[slices] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
169 indices = coordinates.astype('i') #choose the biggest smaller integer index |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
170 return a[tuple(indices)] |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
171 |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
172 if __name__ == "__main__": |
de698afbe6fc
Avoid using pylab helper functions to reduce the magic level.
darius
parents:
10
diff
changeset
|
173 main() |