-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word2VecBiomarker.py
141 lines (115 loc) · 4 KB
/
Word2VecBiomarker.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
from __future__ import unicode_literals
import pandas as pd
import numpy as np
import spacy
from numpy import dot
from numpy.linalg import norm
import math
import json
nlp = spacy.load('en')
def vec(s):
return nlp(s).vector
def addv(coord1, coord2):
return [c1 + c2 for c1, c2 in zip(coord1, coord2)]
addv([10, 1], [5, 2])
def subtractv(coord1, coord2):
return [c1 - c2 for c1, c2 in zip(coord1, coord2)]
subtractv([10, 1], [5, 2])
def distance(coord1, coord2):
# note, this is VERY SLOW, don't use for actual code
return math.sqrt(sum([(i - j)**2 for i, j in zip(coord1, coord2)]))
distance([10, 1], [5, 2])
def meanv(coords):
# assumes every item in coords has same length as item 0
sumv = [0] * len(coords[0])
for item in coords:
for i in range(len(item)):
sumv[i] += item[i]
mean = [0] * len(sumv)
for i in range(len(sumv)):
mean[i] = float(sumv[i]) / len(coords)
return mean
def closest(space, coord, n=10):
closest = []
for key in sorted(space.keys(),
key=lambda x: distance(coord, space[x]))[:n]:
closest.append(key)
return closest
# cosine similarity
def cosine(v1, v2):
if norm(v1) > 0 and norm(v2) > 0:
return dot(v1, v2) / (norm(v1) * norm(v2))
else:
return 0.0
def spacy_closest(token_list, vec_to_check, n=10):
return sorted(token_list,
key=lambda x: cosine(vec_to_check, vec(x)),
reverse=True)[:n]
color_data = json.loads(open("/Users/bholmes/Desktop/DeleteMeSoon/word2vec/colors.json").read())
def hex_to_int(s):
s = s.lstrip("#")
return int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16)
colors = dict()
for item in color_data['colors']:
colors[item["color"]] = hex_to_int(item["hex"])
# The path reports are located here
reports = pd.read_csv("/Users/bholmes/Desktop/DeleteMeSoon/MSMDR Narratives/PathReports.csv", low_memory=False)
# 'description' is the text of the reports
reports = reports['description']
brcaReports = []
# Looking at colors
# We'll collect the breast-containing reports
#reportsOrig = reports
for report in reports:
if 'autopsy' in report:
#startSection = report[report.index("MICROSCOPIC:"):]
#section = startSection[:startSection.index('\n\n')]
#brcaReports.append(section)
brcaReports.append(report)
# print(len(brcaReports))
reports = ' '.join(brcaReports)
reports = reports.replace('\n', ' ')
reports = reports.split(' ')
microscopic_colors = [colors[word.lower()] for word in reports if word.lower() in colors]
avg_color = meanv(microscopic_colors)
print(avg_color)
print(closest(colors, avg_color))
input()
# Looking for similarity words!
#reports = reportsOrig
# We'll collect the breast-containing reports
#for report in reports:
# brcaReports.append(report)
#reports = ' '.join(brcaReports)
#reports = reports.replace('\n', ' ')
#reports = reports.lower()
#reports = reports.split(' ')
#tokens = list(set([w for w in reports if w]))
#print(spacy_closest(tokens, vec("carcinoma")))
def sentvec(s):
sent = nlp(s)
return meanv([w.vector for w in sent])
reports = reportsOrig
for report in reports:
if 'PATHOLOGICAL DIAGNOSIS:' in report:
report = report[report.index('PATHOLOGICAL DIAGNOSIS:') + len('PATHOLOGICAL DIAGNOSIS: '):]
if '\n\n' in report:
report = report[:report.index('\n\n')]
else:
print("HERE!")
print(report)
input()
brcaReports.append(report)
brcaReports = brcaReports[0:1000]
print(len(brcaReports))
reports = ' '.join(brcaReports)
reports = reports.replace('\n', ' ')
reports = reports.lower()
sentences = nlp(reports)
sentences = list(sentences.sents)
def spacy_closest_sent(space, input_str, n=10):
input_vec = sentvec(input_str)
return sorted(space, key=lambda x: cosine(np.mean([w.vector for w in x], axis=0), input_vec), reverse=True)[:n]
for sent in spacy_closest_sent(sentences, "We were able to find a large lesion"):
print(sent)
print("---")