annotate logplot.py @ 18:ff12367786a7

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