-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractSymptoms.py
152 lines (84 loc) · 3.37 KB
/
extractSymptoms.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
144
145
146
147
###############################
### Extract Symptoms
### VERSION 2
### PyTorch GPU
###############################
import pandas as pd
import numpy as np
import os
import nltk
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords
from transformers import BertForSequenceClassification, BertTokenizer, BertForMaskedLM
from simpletransformers.language_modeling import LanguageModelingModel
from sklearn.metrics.pairwise import cosine_similarity, paired_euclidean_distances
from sklearn.metrics.pairwise import euclidean_distances
from tqdm import tqdm
import torch
import time
import pickle
from dask.distributed import Client
import torch
from torch.nn import CosineSimilarity
from functools import partial
from itertools import *
import itertools
device='cuda:2'
def getSimilarWords(tokenizer, combinedOutputFolder, symptom, meanEmb, similarityThreshold = 0.3, numThreshold = 150000, numComp = 10000):
output = []
symptomToken = tokenizer.encode(symptom)[1]
fileList = os.listdir(combinedOutputFolder)
cos = CosineSimilarity(dim=1, eps=1e-6)
examineCount = 0
for i in tqdm(range(len(fileList))):
if examineCount >= numThreshold:
break
filename = os.path.join(combinedOutputFolder, f"{i}.pkl")
subDict = pickle.load(open(filename,'rb'))
IDList = subDict['id']
tokenList = subDict['token']
embList = subDict['emb']
arrA = torch.from_numpy(meanEmb.reshape(1,-1)).to(device).type(torch.cuda.FloatTensor)
arrB = torch.from_numpy(embList).to(device).type(torch.cuda.FloatTensor)
# arrA = torch.from_numpy(meanEmb.reshape(1,-1)).to(device)
# arrB = torch.from_numpy(embList).to(device)
sim = cos(arrA,arrB).cpu().numpy().reshape(-1)
del arrA
del arrB
sim = np.round(sim,4)
index= np.where([sim> similarityThreshold])[1]
tokenList_ = tokenList[index]
IDList_ = IDList[index]
simList = sim[index]
out = [(x,y,z) for x,y,z in zip(tokenList_, simList, IDList_)]
print(len(out))
output += out
examineCount += numComp
return output
########################
# 'fever_9016_Emb.npy',
# 'fatigue_16342_Emb.npy',
# 'cough_19340_Emb.npy'
combinedOutputFolder = '/data2/roshansk/ADRModel_DataStore_10000/'
numComp = 10000
numThreshold = 1000000
file = 'cough_19340_Emb.npy'
symptom = ''
dumpFile = '/data1/roshansk/SymptomAnalysis/combined_1000k_thresh0.4_v2.p'
########################
dataFolder = '/data1/roshansk/covid_data/'
fileList = os.listdir(dataFolder)
df = pd.read_csv(os.path.join(dataFolder, fileList[0]), nrows = numThreshold)
tokenizer = BertTokenizer.from_pretrained('/data1/roshansk/Exp1/checkpoint-141753-epoch-1')
fileList = [ 'fever_9016_Emb.npy', 'fatigue_16342_Emb.npy', 'cough_19340_Emb.npy']
embList = []
for file in fileList:
embList += list(np.load(os.path.join('EmbFolder/',file)))
meanEmb = np.mean(embList,0)
startTime = time.time()
result = getSimilarWords(tokenizer, combinedOutputFolder, symptom,
meanEmb, similarityThreshold = 0.4, numThreshold = numThreshold, numComp = numComp)
print(len(result))
print(f"Time taken : {time.time() - startTime}")
pickle.dump( result, open( dumpFile, "wb" ) )
print(f"Saved data for {file}")