Skip to content

Commit

Permalink
Adding old versions of data processing code. (python)
Browse files Browse the repository at this point in the history
  • Loading branch information
liliakai committed Oct 5, 2011
0 parents commit 123a3d9
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 0 deletions.
95 changes: 95 additions & 0 deletions analyzeBins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import sys
import math
import graph
import Image,ImageDraw

def run(filename):

f = open(filename)

bins = {}
binCount = 0
maxBinValue = 0
mbvIndex= 0
maxBinNum = 0
lastBinOver10 = 0
print f.readline().strip()
print f.readline().strip()
print f.readline().strip()
for line in f:

binCount = binCount+1
data = line.split()
bin = int(data[0])
value = int(data[1])
bins[bin] = value
if (value > maxBinValue):
maxBinValue = value
mbvIndex = bin
if (value > 10):
lastBinOver10 = bin

keys = bins.keys()
keys.sort()
if len(keys) > 0:
maxBinNum = keys[len(keys)-1]
else:
maxBinNum = -1
#print keys
#print maxBinValue
print filename
print "Non-empty bins:\t%d" % (binCount)
print "Highest bin:\t%d" % (maxBinNum)
print "Fullest bin:\t%d (%d counts)" % (mbvIndex,maxBinValue)

g = graph.Graph(4196,1000,0,4096,0,maxBinValue)
for key in keys:
#g.graphBar(key,bins[key])
g.graphColorBar(key,bins[key],[float(key)/4096])
g.drawLabels("adc reading [0,4096]", "total counts [1,%d]" % maxBinValue)
g.save(filename.split("_")[0]+"_linearspectrum.bmp")


if maxBinValue > 0:
g = graph.Graph(4196,1000,0,4096,0,math.log(maxBinValue))
else:
g = graph.Graph(4196,1000,0,4096,0,1)

gridline = 1
while( gridline <= 10 and gridline < maxBinValue):
g.gridline(math.log(gridline),str(gridline))
tmp = gridline
while( gridline < maxBinValue ):
g.gridline(math.log(gridline),str(gridline))
gridline *= 10

gridline = tmp+1

for key in keys:
value = bins[key]
#g.graphBar(key,math.log(value))
if key > 2044:
tmp = math.log(max(1,0.25*value))
for i in range(4):
#g.graphBar(key+i,tmp)
g.graphColorBar(key+i,tmp,[float(key+i)/4096])
elif key > 1022:
tmp = math.log(max(1,0.5*value))
for i in range(2):
#g.graphBar(key+i,tmp)
g.graphColorBar(key+i,tmp,[float(key+i)/4096])
else:
#g.graphBar(key,math.log(value))
g.graphColorBar(key,math.log(value),[float(key)/4096])

g.drawLabels("adc reading [0,4096]", "total counts [1,%d]" % maxBinValue)
g.save(filename.split("_")[0]+"_logspectrum.bmp")




if __name__ == "__main__":
filename = "./bindata.txt"
if len(sys.argv) > 1:
filename = sys.argv[1]
run(filename)
88 changes: 88 additions & 0 deletions graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import colorsys
import Image,ImageDraw

class Graph():

def __init__(self,width,height,xmin=0,xmax=4096,ymin=0,ymax=500):
self.im = Image.new('RGB',(width,height))
self.draw = ImageDraw.Draw(self.im)
self.width = width
self.height = height
self.xmax = xmax
self.ymax = ymax
self.xmin = xmin
self.ymin = ymin

self.yborder = height / 10
self.xborder = height / 20

self.gheight = height-2*self.yborder
self.gwidth = width-2*self.xborder
self.gxmax = width-self.xborder
self.gymax = height-self.yborder
if self.xmax > 0:
self.xunit = self.gwidth / self.xmax;
else:
self.xunit = self.gwidth


def drawLabels(self, xlabel, ylabel):
lines = [ ylabel, "VS", xlabel ]

for i in range(3):
x = (self.width-self.draw.textsize(lines[i])[0])/2
y = self.gymax+(i+1)*self.yborder/5
self.draw.text((x,y),lines[i])

self.draw.line((self.xborder,self.yborder,self.gxmax,self.yborder),fill=(255,0,0))
self.draw.line((self.xborder,self.gymax+1,self.gxmax,self.gymax+1),fill=(255,0,0))
self.draw.line((self.xborder,self.yborder,self.xborder,self.gymax+1),fill=(255,0,0))
self.draw.line((self.gxmax,self.yborder,self.gxmax,self.gymax+1),fill=(255,0,0))

def save(self,filename):
self.im.save(filename,"BMP")

def gridline(self,y,s=""):
if (s==""):
s = str(y)
sl = self.draw.textsize(s)
imy = self.gymax - (self.gheight * float(y) / self.ymax)
self.draw.line((self.xborder-5, imy, self.gxmax, imy), fill=(200,200,200))
self.draw.text((self.xborder-10 - sl[0], imy-sl[1]/2), s, fill=(255,255,255))


def graphBar(self,x,y):
if (self.ymax == self.ymin):
return
if (x > self.xmax or y > self.ymax or x < self.xmin or y < self.ymin):
print "Bad graph point:"
print x,y
#return
imx = self.xborder + self.gwidth * float(x) / self.xmax
imy = self.gymax - max(1,self.gheight * y / self.ymax)
#print x, imx, imy
self.draw.rectangle((imx,imy,imx+self.xunit-0.5,self.gymax),fill=(255,255,255))

def graphColorBar(self,x,y,colors):
if (self.ymax == self.ymin):
return
if (x > self.xmax or y > self.ymax or x < self.xmin or y < self.ymin):
print "Bad graph point:"
print x,y
#return
n = len(colors) # the bar will be divided into n blocks of color.

if (n==0):
return
colors.sort()
imx = self.xborder + self.gwidth * float(x) / self.xmax
imx2= self.xborder + self.gwidth * float(x+1) / self.xmax - 1
imy = self.gymax - (self.gheight * float(y) / self.ymax)
k = (self.gymax-imy)/n
for i in range(n):
#print x, imx, imy
(r,g,b) = colorsys.hsv_to_rgb(colors[i],1,1)
self.draw.rectangle((imx,imy+i*k,imx2,imy+(i+1)*k),fill=(int(255*r),int(255*g),int(255*b)))



96 changes: 96 additions & 0 deletions processData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import sys
import time
import graph
import Image,ImageDraw

def run(filename):

f = open(filename)
counts = 0;
minutes = 0;
now = 0;
bins = {}
start = 0;
cpm = []
maxcpm = 0;
lastcount = 0
minuteData = []
while True:
bytes = f.read(2)
if (len(bytes) < 2):
break

data = (ord(bytes[0]) << 8) + ord(bytes[1])
if (data == 0xffff):
diff = counts - lastcount
cpm.append(minuteData)
if len(minuteData) > maxcpm:
maxcpm = len(minuteData)
minuteData = []
lastcount = counts
minutes = minutes + 1
#print "min %d, %d counts of %d" % (minutes, diff, counts)
bytes = f.read(4)
if len(bytes) == 4:
data = (ord(bytes[0])<< 24) + (ord(bytes[1])<<16) + (ord(bytes[2]) << 8) + ord(bytes[3])
now = data
#print time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(now))
if start == 0:
start = now
elif data > 0x0fff:
print "ERR! ignoring, pulse height too big: %d" % data
else:
minuteData.append(data)
binNum = data #4096 * data / 0x0fff
if bins.has_key(binNum):
bins[binNum] = bins[binNum] + 1
else:
bins[binNum] = 1

counts = counts+1
#print str(data)

outputFilename = filename.split(".")[0] + "_bins.txt";
fbin = open(outputFilename,'w')
if (now != 0):
fbin.write(time.strftime("Start Time: %a, %d %b %Y %H:%M:%S +0000\n", time.gmtime(start)));
fbin.write("Duration: %d minutes\n" % minutes);
if minutes > 0:
fbin.write("Counts: %d (%f CPM)\n" % (counts, counts/minutes));
else:
fbin.write("Counts: %d (%f CPM)\n" % (counts, counts));

keys = bins.keys()
keys.sort()
for key in keys:
fbin.write(str(key) + " " + str(bins[key]) + "\n")
fbin.close()

#ymax = max(cpm)
#im = Image.new('RGB',(minutes,ymax))
#draw = ImageDraw.Draw(im)
#for t in range(minutes):
# draw.line((t,ymax - cpm[t],t,ymax),fill=(255,255,255))
#im.save(filename.split(".")[0]+".bmp","BMP")

g = graph.Graph(1000,1000,0,minutes,0,maxcpm)
for t in range(minutes):
#g.graphBar(t,cpm[t])
#print len(cpm[t])
g.graphColorBar(t,len(cpm[t]),[float(f)/4096.0 for f in cpm[t]])
g.drawLabels("time (min) since %s [%d,%d]" % (time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(start)),0,minutes), "pulses recorded [%d,%d]" % (0,maxcpm))
g.save(filename.split(".")[0]+"_timeline.bmp")

if (minutes > 0):
print "avg cpm: %f" % (counts/minutes)
print "max cpm: %d" % (maxcpm)

return outputFilename


if __name__ == "__main__":
if ( len(sys.argv) > 1):
filename = sys.argv[1]
run(filename)
else:
exit()
17 changes: 17 additions & 0 deletions radcount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/local/bin/python
# radcount.py
import sys

sys.path.append("/home/lilia/Imaging-1.1.7/build/lib.freebsd-8.2-RELEASE-i386-2.6")
sys.path.append("/home/lilia/rad")

import processData
import analyzeBins

if __name__ == "__main__":
if ( len(sys.argv) > 1):
filename = sys.argv[1]
binFileName = processData.run(filename)
analyzeBins.run(binFileName)
else:
exit()

0 comments on commit 123a3d9

Please sign in to comment.