Rev. | 784702bf1412c01dddf813f6609d2b2e32668b76 |
---|---|
Größe | 11,038 Bytes |
Zeit | 2009-09-10 02:30:12 |
Autor | lorenzo |
Log Message | A variation of the code addendum_infectitious_power2.py. I need this just to print out some extra files I need when analyzing the data from Dublin
|
#!/usr/bin/env python
import scipy as s
import numpy as n
import pylab as p
# def visit_duration_many_tags_new(data_filename):
# data_arr=p.load(data_filename)
# print "I finished reading the file"
# #convert to integer
# data_arr=data_arr.astype("int")
# #keep only the relevant data (time and tag id)
# data_arr=data_arr[:,0:2] #Now I am left with timestamps and
# #tag_ids
# tag_id_list=s.unique1d(data_arr[:,1])
# print "the number of detected tags is, ", len(tag_id_list)
# duration_arr=s.zeros(0).astype("int")
# for i in xrange(len(tag_id_list)):
# tag_id=tag_id_list[i]
# time_sel=s.where(data_arr[:,1]==tag_id)[0]
# tag_reboot_times=data_arr[time_sel,0]
# duration_single_tag=s.diff(tag_reboot_times)
# if (tag_id==1189):
# p.save("1189_new_visit_durations.dat", duration_single_tag,fmt='%d')
# duration_arr=s.hstack((duration_arr,duration_single_tag))
# return (duration_arr)
def select_entrance_single_tag(entrance_data):
len_arr=len(entrance_data)
entrance_list=s.zeros(len_arr).astype("int")-1 #array initialization
ip_set=set((168624164,3232235522))
for i in xrange(len_arr):
my_set=set(entrance_data[i])
if len(ip_set & my_set)>0:
entrance_list[i]=1
return entrance_list
def visit_duration_single_tag(data_arr,tag_id):
find_id_by_tag=s.where(data_arr[:,1]==tag_id)
#the following may look odd but it retrieves the 1st and
#7th column (counted from 1, or the 0th and 6nd counted from zero)
#of the matrix, that is to day the timestamp and bootcount of the
#sighting reports of a given tag
retrive_time_bootcount=data_arr[find_id_by_tag[0],:][:,0:7:6]
#now calculate an array giving the gaps in the bootcounts and the
#corresponding gaps in the timestamps
#Now I fetch the last 7 columns in my array (and the rows corresponding
#to station IPs)
single_tag_entrance_data=data_arr[find_id_by_tag[0],:][:,-8:-1]
entrance_check_single_tag=select_entrance_single_tag(single_tag_entrance_data)
bootcount_list=retrive_time_bootcount[:,1]
bootcount_unique=s.unique1d(bootcount_list)
time_list=retrive_time_bootcount[:,0]
duration_arr=s.zeros(len(bootcount_unique)).astype("int") -1 #it
#will be overwritten
flag_duration_arr=s.zeros(len(bootcount_unique)).astype("int") -1
interval_arr=s.zeros(len(bootcount_unique)-1).astype("int") -1 #it
#will be overwritten
single_tag_jumps=s.zeros(len(bootcount_unique)-1).astype("int")
#will be partially overwritten
single_tag_visit_begin=s.zeros(len(bootcount_unique)).astype("int") -1
single_tag_visit_end=s.zeros(len(bootcount_unique)).astype("int") -1
#i=0
#for my_bootcount in bootcount_unique:
#sel=s.where(bootcount_list==my_bootcount)
#duration_arr[i]=max(time_list[sel])-min(time_list[sel])
#i+=1
for i in xrange(len(bootcount_unique)):
my_bootcount=bootcount_unique[i]
sel=s.where(bootcount_list==my_bootcount)
single_tag_visit_begin[i]=min(time_list[sel])
single_tag_visit_end[i]=max(time_list[sel])
# duration_arr[i]=max(time_list[sel])-min(time_list[sel])
duration_arr[i]=single_tag_visit_end[i]-single_tag_visit_begin[i]
flag_duration_arr[i]=entrance_check_single_tag[sel][0]
#i.e. flag duration will amount to 1 if the visit
#begins at the entrance and to zero otherwise
#if (duration_arr[i]==0):
#print "my_bootcount is, ", my_bootcount
#print "time_list[sel] is, ", time_list[sel]
#print "tag_id is, ", tag_id
if (i>0):
lower=bootcount_unique[i-1]
upper=bootcount_unique[i]
sel_lower=s.where(bootcount_list==lower)[0][-1]
sel_upper=s.where(bootcount_list==upper)[0][0]
interval_arr[i-1]=time_list[sel_upper]-time_list[sel_lower]
single_tag_jumps[i-1]=upper-lower #this tells me how much
#the bootcount is incremented when it gets updated (should
#be incremented by 1 only)
if (tag_id==1601):
p.save("1601_duration_arr.dat",duration_arr,fmt='%d' )
p.save("1601_bootcount_list.dat",bootcount_list,fmt='%d' )
p.save("1601_bootcount_unique.dat",bootcount_unique,fmt='%d' )
p.save("1601_flag_visit.dat",flag_duration_arr,fmt='%d' )
p.save("1601_flag_all.dat",entrance_check_single_tag,fmt='%d')
duration_flagged=duration_arr[s.where(flag_duration_arr<0)]
duration_flagged=duration_flagged/60. #convert into minutes
p.save("1601_duration_min_flagged_visits.dat",duration_flagged,fmt='%d')
tag_jumps=s.where(s.diff(bootcount_list)>1)[0]+1 #I added 1 to
#start counting the rows from 1 instead of zero.
p.save("1601_bootcount_jumps.dat",tag_jumps,fmt='%d')
p.save("1601_bootcount_jumps_at_visit_intervals.dat",single_tag_jumps,fmt='%d')
if (tag_id==1595):
p.save("1595_duration_arr.dat",duration_arr,fmt='%d' )
p.save("1595_bootcount_list.dat",bootcount_list,fmt='%d' )
p.save("1595_bootcount_unique.dat",bootcount_unique,fmt='%d' )
p.save("1595_flag_visit.dat",flag_duration_arr,fmt='%d' )
p.save("1595_flag_all.dat",entrance_check_single_tag,fmt='%d')
duration_flagged=duration_arr[s.where(flag_duration_arr<0)]
duration_flagged=duration_flagged/60. #convert into minutes
p.save("1595_duration_min_flagged_visits.dat",duration_flagged,fmt='%d')
tag_jumps=s.where(s.diff(bootcount_list)>1)[0]+1 #I added 1 to
#start counting the rows from 1 instead of zero.
p.save("1595_bootcount_jumps.dat",tag_jumps,fmt='%d')
p.save("1595_bootcount_jumps_at_visit_intervals.dat",single_tag_jumps,fmt='%d')
if (tag_id==1567):
p.save("1567_duration_arr.dat",duration_arr,fmt='%d' )
p.save("1567_bootcount_list.dat",bootcount_list,fmt='%d' )
p.save("1567_bootcount_unique.dat",bootcount_unique,fmt='%d' )
p.save("1567_flag_visit.dat",flag_duration_arr,fmt='%d' )
p.save("1567_flag_all.dat",entrance_check_single_tag,fmt='%d')
duration_flagged=duration_arr[s.where(flag_duration_arr<0)]
duration_flagged=duration_flagged/60. #convert into minutes
p.save("1567_duration_min_flagged_visits.dat",duration_flagged,fmt='%d')
tag_jumps=s.where(s.diff(bootcount_list)>1)[0]+1 #I added 1 to
#start counting the rows from 1 instead of zero.
p.save("1567_bootcount_jumps.dat",tag_jumps,fmt='%d')
p.save("1567_bootcount_jumps_at_visit_intervals.dat",single_tag_jumps,fmt='%d')
#NB: the number of reboots for a tag is given by the number of unique
#bootcounts minus 1 !!!
return [duration_arr, len(bootcount_unique)-1, interval_arr \
,entrance_check_single_tag, flag_duration_arr, single_tag_jumps,\
single_tag_visit_begin,single_tag_visit_end]
def visit_duration_many_tags(data_arr):
tag_id_list=s.unique1d(data_arr[:,1])
p.save("all_visit_tag_ids.dat", tag_id_list,fmt='%d' )
res=s.zeros(0).astype("int")
count_boot=s.zeros(0).astype("int")
interval_between_boots=s.zeros(0).astype("int")
all_tags_jumps=s.zeros(0).astype("int")
find_entrance=s.zeros(0).astype("int")
flag_duration_total=s.zeros(0).astype("int")
all_visit_begin=s.zeros(0).astype("int")
all_visit_end=s.zeros(0).astype("int")
all_tag_id_list_long=s.zeros(0).astype("int")
for track_tag in tag_id_list:
duration_and_count=visit_duration_single_tag(data_arr,track_tag)
single_tag_duration=duration_and_count[0]
single_tag_count_boot=duration_and_count[1]
single_tag_boot_interval=duration_and_count[2]
single_tag_entrance=duration_and_count[3]
single_tag_flag_duration=duration_and_count[4]
single_tag_jump=duration_and_count[5]
single_visit_begin=duration_and_count[6]
single_visit_end=duration_and_count[7]
single_tag_id_long=s.ones(len(single_visit_end)).astype("int")*track_tag
res=s.hstack((res,single_tag_duration))
count_boot=s.hstack((count_boot,single_tag_count_boot))
interval_between_boots=s.hstack((interval_between_boots,\
single_tag_boot_interval))
find_entrance=s.hstack((find_entrance,single_tag_entrance))
all_tags_jumps=s.hstack((all_tags_jumps,single_tag_jump))
all_visit_begin=s.hstack((all_visit_begin,single_visit_begin))
all_visit_end=s.hstack((all_visit_end,single_visit_end))
flag_duration_total=s.hstack((flag_duration_total,single_tag_flag_duration))
all_tag_id_list_long=s.hstack((all_tag_id_list_long,single_tag_id_long))
sel_duration_flagged=s.where(flag_duration_total<0)
extract_duration_flagged_visits=res[sel_duration_flagged]/60. #to convert into minutes
p.save("all_flagged_visits_duration.dat",extract_duration_flagged_visits,fmt='%d')
return [res, count_boot,interval_between_boots, \
find_entrance,flag_duration_total,all_tags_jumps,all_visit_begin,\
all_visit_end, all_tag_id_list_long]
# data_arr=p.load("infectious.log")
#filename="output_long_boot_count_number.dat"
filename="tag_and_boot_every_20_sec.dat"
# visit_durations_from_code_by_ciro=visit_duration_many_tags(filename)
# p.save("duration_new.dat",visit_durations_from_code_by_ciro, fmt='%d')
data_arr=p.load(filename)
print "I finished reading the file"
all_duration_and_all_counts=visit_duration_many_tags(data_arr)
all_durations=all_duration_and_all_counts[0]
p.save("all_visit_duration.dat",all_durations , fmt='%d')
all_counts=all_duration_and_all_counts[1]
p.save("all_counts.dat",all_counts , fmt='%d')
all_boot_intervals=all_duration_and_all_counts[2]
p.save("all_boot_intervals.dat",all_boot_intervals , fmt='%d')
all_tag_entries=all_duration_and_all_counts[3]
p.save("all_tag_entries.dat",all_tag_entries , fmt='%d')
all_tag_flag_visits=all_duration_and_all_counts[4]
p.save("all_visit_duration_flag.dat",all_tag_flag_visits , fmt='%d')
all_tag_jumps=all_duration_and_all_counts[5]
p.save("all_tag_jumps_in_bootcount.dat",all_tag_jumps , fmt='%d')
all_visit_start_time=all_duration_and_all_counts[6]
p.save("all_visit_start_time.dat",all_visit_start_time , fmt='%d')
all_visit_end_time=all_duration_and_all_counts[7]
p.save("all_visit_end_time.dat",all_visit_end_time , fmt='%d')
all_visit_tag_id_long=all_duration_and_all_counts[8]
p.save("all_visit_tag_id_long.dat",all_visit_tag_id_long , fmt='%d')
print "len(s.where(all_tag_flag_visits<0)[0]) is, ", len(s.where(all_tag_flag_visits<0)[0])
print " len(s.where(all_visit_start_time<0)[0]) is, ", len(s.where(all_visit_start_time<0)[0])
print "So far so good"