修訂. | fd2a10f8a811605534377b659656b1720f554f4e |
---|---|
大小 | 5,319 bytes |
時間 | 2009-12-03 23:42:27 |
作者 | lorenzo |
Log Message | I updated the way in which I save files (p.save gets replaced by n.savetxt). |
#!/usr/bin/env python
import scipy as s
import pylab as p
import numpy as n
import sys
import string
def cut_raw_data(bootcount_data):
boot_out=bootcount_data[:,0:3]
boot_out[:,2]=bootcount_data[:,6]
return (boot_out)
def create_new_bootcounts_for_all(boot_count_cut,time_gap,tag_id_list):
tag_list_unique=s.unique1d(tag_id_list)
boot_new_out=s.copy(boot_count_cut[:,2])
print "len(tag_list_unique) is, ",len(tag_list_unique)
for i in xrange(len(tag_list_unique)):
print "i is, ", i
tag_id_current=tag_list_unique[i]
print "tag_id_current is, ", tag_id_current
select_boot=s.where(tag_id_list==tag_id_current)[0]
if (len(s.unique1d(boot_count_cut[select_boot,2]))>1): # it all makes sense
#if I have at least two distinct bootcounts (otherwise I cannot
#define a visit)
tag_boot_new= \
new_bootcount_single_tag(tag_id_current,boot_count_cut,time_gap,tag_id_list)
boot_new_out[select_boot]=tag_boot_new
#These other two bits need to be outside the loop!
boot_count_cut[:,2]=boot_new_out
n.savetxt("boot_new_out.dat", boot_count_cut, fmt="%d")
return
def new_bootcount_single_tag(tag_id,boot_count_cut,time_gap, tag_id_list):
# print "len(boot_count_cut) is, ", len(boot_count_cut)
# tag_id_unique=s.unique1d(boot_count_cut[:,1])
tag_sel=s.where(tag_id_list==tag_id)[0] #then select a tag with a given
#tag_id by selecting the positions where its tag_id appears
# print "tag_sel is, ", tag_sel
# print "len(tag_sel) is, ", len(tag_sel)
tag_times=boot_count_cut[:,0][tag_sel] #all times for tag with tag_ids
tag_boot=boot_count_cut[:,2][tag_sel] #all bootcounts for tag with tag_ids
# if (tag_id==1624):
# p.save("tag_times.dat", tag_times, fmt="%d")
# p.save("tag_boot.dat", tag_boot, fmt="%d")
time_jump_arr=s.diff(tag_times) #all time intervals between two sightings
#of the tag we are following
boot_jump_arr=s.diff(tag_boot) #all bootcount intervals for the tag we are
#following
#NB: the arrays with the discontinuities in the bootcount/time are one entry
#shorter than the arrays I initially started from.
# print "time_jump_arr is, ", time_jump_arr
# print "boot_jump_arr is, ", boot_jump_arr
# if (tag_id==1624):
# p.save("time_jump_arr.dat", time_jump_arr, fmt="%d")
# p.save("boot_jump_arr.dat", boot_jump_arr, fmt="%d")
tag_sel_restricted=s.where((time_jump_arr>=time_gap) & \
(boot_jump_arr!=0))[0]
#tell me when the bootcount
#increases and I also see a time gap of at least a couple of minutes in
#between (otherwise it is a spurious reboot and it should be considered as
#the same visit).
tag_sel_new=tag_sel[tag_sel_restricted] #array to select
#times and bootcounts for our
#tag when the two conditions above are met
# if (tag_id==1624):
# p.save("tag_sel_new.dat", tag_sel_new, fmt="%d")
#The following two lines are just a test
tag_new=boot_count_cut[tag_sel_new,:]
# if (tag_id==1624):
# p.save("tag_new.dat",tag_new,fmt="%d")
#Now assign new "conventional" bootcounts to the tag.
#The bootcount will be incremented only when the two conditions above are
#met
tag_boot_new=s.arange(len(tag_boot)) #the number of distinct bootcounts
#must be equal to the number of valid changes in the bootcount +1
# count_i=0
# for i in xrange(len(tag_new)):
# my_sel=s.where(tag_times<=tag_new[i,0])[0]
# my_sel_new=max(my_sel)
# tag_boot_new[my_sel]=i
# count_i+=1
if (len(tag_new)>0): #this all makes sense only if the tag actually
#sent out sighting packets with different bootcount and separated in
#time by the chosen time_gap
for i in xrange(len(tag_new)):
my_sel=s.where(tag_times>tag_new[i,0])[0]
# my_sel_new=max(my_sel)
tag_boot_new[my_sel]=i+1
# print "ok end loop"
# my_sel=s.where(tag_times>tag_new[-1,0])[0]
# tag_boot_new[my_sel]=count_i
my_sel=s.where(tag_times<=tag_new[0,0])[0]
tag_boot_new[my_sel]=0
# p.save("tag_boot_new.dat", tag_boot_new, fmt="%d")
elif(len(tag_new)==0):
tag_boot_new[:]=0
tag_boot_new=tag_boot_new.astype("int64")
return (tag_boot_new)
#Maybe not necessary, but this is a safer way of reading integer numbers
f = open(sys.argv[1])
raw_data = [map(int, string.split(line)) for line in f.readlines()]
f.close()
raw_data = s.array(raw_data, dtype="int64")
#raw_data=raw_data.astype("uint64")
# p.save("test_rewrite.dat", raw_data, fmt="%d")
boot_count_cut=cut_raw_data(raw_data)
n.savetxt("new_boot_count_cut.dat", boot_count_cut, fmt="%d")
print "done reading"
# my_tag_id=1374
# test=new_bootcount_single_tag(my_tag_id,boot_count_cut,time_gap,tag_id_list)
time_gap=120
tag_id_list=boot_count_cut[:,1] #first get the id of all
#the tags which broadcast a sighting protocol
test_all=create_new_bootcounts_for_all(boot_count_cut,time_gap,tag_id_list)
print "So far so good"