-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAddressOscillation.py
101 lines (79 loc) · 3.33 KB
/
AddressOscillation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sys, json,os, psutil, csv, time
import numpy as np
from distance import distance
from class_cluster import cluster
from collections import defaultdict
from multiprocessing import Pool
from multiprocessing import current_process, Lock, cpu_count
from oscillation_type1 import oscillation_h1_oscill
def init(l):
global lock
lock = l
def func(args):
name, user, dur_constraint, outputFile = args
try:
user = oscillation_h1_oscill(user,dur_constraint)
with lock:
f = open(outputFile, 'a')
writeCSV = csv.writer(f, delimiter=',')
for day in sorted(user.keys()):
for trace in user[day]:
trace[1] = name
writeCSV.writerow(trace)
f.close()
except:
return
if __name__ == '__main__':
inputFile = sys.argv[1]
outputFile = sys.argv[2]
duration_constraint = int(sys.argv[3])
outputFile = outputFile.replace('.csv','_tmp.csv')
f = open(outputFile, 'w')
f.write('unix_start_t,user_ID,mark_1,orig_lat,orig_long,orig_unc,stay_lat,stay_long,stay_unc,stay_dur,stay_ind,human_start_t\n')
f.close()
l = Lock() # thread locker
pool = Pool(cpu_count(), initializer=init, initargs=(l,))
# fixed param
user_num_in_mem = 1000
usernamelist = set() # user names
with open(inputFile,'rU') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
readCSV.next()
for row in readCSV:
#if not len(row) ==12 : continue
usernamelist.add(row[1]) # get ID list; the second colume is userID
usernamelist = list(usernamelist)
print('total number of users to be processed: ', len(usernamelist))
def divide_chunks(usernamelist, n):
for i in range(0, len(usernamelist), n): # looping till length usernamelist
yield usernamelist[i:i + n]
usernamechunks = list(divide_chunks(usernamelist, user_num_in_mem))
print('number of chunks to be processed', len(usernamechunks))
## read and process traces for one bulk
while (len(usernamechunks)):
namechunk = usernamechunks.pop()
print("Start processing bulk: ", len(usernamechunks) + 1, ' at time: ', time.strftime("%m%d-%H:%M"), ' memory: ', psutil.virtual_memory().percent)
UserList = {name: defaultdict(list) for name in namechunk}
with open(inputFile,'rU') as readfile:
readCSV = csv.reader(readfile, delimiter=',')
readCSV.next()
for row in readCSV:
#if not len(row) ==12 : continue
#if '.' not in row[3] or '.' not in row[4]: continue # debug a data issue: not '.' in lat or long
name = row[1]
if name in UserList:
UserList[name][row[-1][:6]].append(row)
print("End reading")
# pool
tasks = [pool.apply_async(func, (task,)) for task in [(name, UserList[name], duration_constraint, outputFile) for name in UserList]]
finishit = [t.get() for t in tasks]
'''
for name in UserList:
func((name, UserList[name], duration_constraint, outputFile))
'''
pool.close()
pool.join()
outputFile_real = outputFile.replace('_tmp.csv','.csv')
if os.path.isfile(outputFile_real):
os.remove(outputFile_real)
os.rename(outputFile,outputFile_real)