forked from AlanMorningLight/baidu-remote-sensing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisit2array_adapt.py
88 lines (73 loc) · 3.37 KB
/
visit2array_adapt.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
import time
import numpy as np
import sys
import datetime
import pandas as pd
import os
date2position = {}
datestr2dateint = {}
str2int = {}
for i in range(24):
str2int[str(i).zfill(2)] = i
for i in range(182):
date = datetime.date(day=1, month=10, year=2018)+datetime.timedelta(days=i)
date_int = int(date.__str__().replace("-", ""))
date2position[date_int] = [i%7, i//7]
datestr2dateint[str(date_int)] = date_int
def visit2array(table):
strings = table[1]
init = np.zeros((7, 26, 24))
for string in strings:
temp = []
for item in string.split(','):
temp.append([item[0:8], item[9:].split("|")])
for date, visit_lst in temp:
x, y = date2position[datestr2dateint[date]]
for visit in visit_lst:
init[x][y][str2int[visit]] += 1
return init
def visit2array_test():
start_time = time.time()
for i in range(0, 10000):
filename = str(i).zfill(6)
table = pd.read_table("/media/dell/dell/data/remote_sensing/Preliminary/test_visit/test/"+filename+".txt", header=None)
array = visit2array(table)
np.save("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/test_visit/"+filename+".npy", array)
sys.stdout.write('\r>> Processing visit data %d/%d'%(i+1, 10000))
sys.stdout.flush()
sys.stdout.write('\n')
print("using time:%.2fs"%(time.time()-start_time))
def visit2array_train():
table = pd.read_csv("/home/dell/Desktop/2019BaiduXJTU/data/train/train.txt", header=None)
filenames = [a[0].split("/")[-1].split('.')[0] for a in table.values]
length = len(filenames)
start_time = time.time()
for index, filename in enumerate(filenames):
table = pd.read_table("/media/dell/dell/data/remote_sensing/Preliminary/train_visit/"+filename+".txt", header=None)
array = visit2array(table)
np.save("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/train_visit/"+filename+".npy", array)
sys.stdout.write('\r>> Processing visit data %d/%d'%(index+1, length))
sys.stdout.flush()
sys.stdout.write('\n')
print("using time:%.2fs"%(time.time()-start_time))
def visit2array_valid():
table = pd.read_csv("/home/dell/Desktop/2019BaiduXJTU/data/train/val.txt", header=None)
filenames = [a[0].split("/")[-1].split('.')[0] for a in table.values]
length = len(filenames)
start_time = time.time()
for index, filename in enumerate(filenames):
table = pd.read_table("/media/dell/dell/data/remote_sensing/Preliminary/train_visit/"+filename+".txt", header=None)
array = visit2array(table)
np.save("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/train_visit/"+filename+".npy", array)
sys.stdout.write('\r>> Processing visit data %d/%d'%(index+1, length))
sys.stdout.flush()
sys.stdout.write('\n')
print("using time:%.2fs"%(time.time()-start_time))
if __name__ == '__main__':
if not os.path.exists("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/test_visit/"):
os.makedirs("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/test_visit/")
if not os.path.exists("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/train_visit/"):
os.makedirs("/media/dell/dell/data/remote_sensing/Preliminary/data/npy/train_visit/")
# visit2array_train()
# visit2array_valid()
visit2array_test()