-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmimic_old_query_5.py
143 lines (105 loc) · 3.8 KB
/
mimic_old_query_5.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import numpy as np
import pandas as pd
data_file = 'ADMISSIONS'
# data_path = 'mimic_data/' + data_file + '.csv'
data_file2 = 'PRESCRIPTIONS'
# data_path1 = '/content/drive/MyDrive/Research/PIR/' + data_file + '.csv'
# data_path2 = '/content/drive/MyDrive/Research/PIR/' + data_file2 + '.csv'
# df = pd.read_csv(data_path1)
# df2 = pd.read_csv(data_path2)
from scipy.sparse import csr_matrix
import datetime
data_file = 'ADMISSIONS'
data_path = 'mimic_data/'+ data_file + '.csv'
df = pd.read_csv(data_path)
data_file = 'PRESCRIPTIONS'
data_path = 'mimic_data/' + data_file + '.csv'
df2 = pd.read_csv(data_path)
c = df.columns.tolist()+['DOSE_VAL_RX']
new_df = pd.merge(df2, df, how='left', on=['SUBJECT_ID','HADM_ID'])[[i for i in c if i != 'ROW_ID']]
import timeit
start = timeit.default_timer()
filename = 'mimic_data/mimic_old_query_5'
filter_field = 'SUBJECT_ID'
aggregation_field = 'DOSE_VAL_RX'
database = new_df
filter_field_list = database[filter_field].tolist()
times= []
for i in range(100):
users_map = {}
index = 0
start = timeit.default_timer()
for i in filter_field_list:
if i not in users_map:
users_map[i] = index
index+=1
# print(users_map)
hashmap_unique_users_tweet= {}
hashmap_col = {}
index = 0
for i in range(len(filter_field_list)):
# if (users_map[filter_field_list[i]] not in hashmap_unique_users_tweet):
# hashmap_unique_users_tweet[users_map[filter_field_list[i]]] = [index]
# elif((users_map[filter_field_list[i]] in hashmap_unique_users_tweet)):
# hashmap_unique_users_tweet[users_map[filter_field_list[i]]].append(index)
#### Not used for generating matrix
if index not in hashmap_col:
hashmap_col[index] = [users_map[filter_field_list[i]]]
else:
hashmap_col[index].append(users_map[filter_field_list[i]])
index+=1
unique_users = set(database[filter_field])
### calculate matrix
# M = np.zeros((len(unique_users), len(database[filter_field])))
# index = 0
# for key, val in hashmap_unique_users_tweet.items():
# for i in val:
# M[key][i] = 1
# index+=1
# stop = timeit.default_timer()
# diff1 = (stop - start)
# start = timeit.default_timer()
### Calculate col and row from hashmap directly
# value_pairs = []
index = 0
my_row = []
my_col = []
for key, val in hashmap_col.items():
for i in val:
# value_pairs.append((i, key))
my_row.append(i)
my_col.append(index)
index += len(val)
my_col.append(index)
# import numpy as np
# from scipy.sparse import csr_matrix
# m = index_of_query_array
# m = M.transpose()
#print(sum([sum(i) for i in index_of_query]))
#row = get_non_zero_indices(m)
# row = csr_matrix(m).indices.tolist()
# col = csr_matrix(m).indptr.tolist()
# vals = csr_matrix(m).data.tolist()
# print(row == my_row)
# print(col == my_col)
#print(vals)
a_row_file = [len(unique_users)] + my_row
a_col_file = [len(database[filter_field])] + my_col
a_row_file = [str(i) for i in a_row_file]
a_col_file = [str(i) for i in a_col_file]
stop = timeit.default_timer()
diff = (stop - start)
# print("times 1:", diff1)
# print("times 2:", diff2)
times.append(diff)
print(sum(times)/len(times))
print(1.96*np.std(times)/np.sqrt(len(times)))
print("P: " , len(unique_users))
print("R: ", len(database[filter_field]))
print("Row: ", len(my_row))
print(len(my_row)/(len(unique_users)* len(database[filter_field])))
# with open(filename+".row", 'w') as totxt_file:
# totxt_file.write(" ".join(a_row_file))
# with open(filename+".col", 'w') as totxt_file:
# totxt_file.write(" ".join(a_col_file))
# stop = timeit.default_timer()