forked from BCHSI/philter-ucsf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinate_map.py
More file actions
258 lines (209 loc) · 7.98 KB
/
Copy pathcoordinate_map.py
File metadata and controls
258 lines (209 loc) · 7.98 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import numpy
import itertools
import re
class CoordinateMap:
"""
Hits are stored in a coordinate map data structure
This class stores start coordinates for any matches found for this pattern
"""
def __init__(self, pattern={"title":"untitled"}, debug=False):
""" internal data structure maps filepaths to a map of int:string (coordinate start --> stop)
map is the internal structure of
{ filename : { startcoordinate : stop_coordinate}}
eg: { "data/foo.txt": {123:126, 19:25} }
coord2pattern keeps reference of the patterns
that matched this coorinate (can be multiple patterns)
all_coords keeps a reference of all coordinates mapped by filename,
allowing us to easily check if these coordinates have been matched yet
"""
self.map = {}
self.coord2pattern = {}
self.pattern = pattern
self.debug = debug
self.all_coords = {}
def add(self, filename, start, stop, overlap=False, pattern=""):
""" adds a new coordinate to the coordinate map
if overlap is false, this will reject any overlapping hits (usually from multiple regex scan runs)
"""
if filename not in self.map:
self.map[filename] = {}
if filename not in self.all_coords:
self.all_coords[filename] = {}
if overlap == False:
if self.does_overlap(filename, start, stop):
return False, "Error, overlaps were found: {} {} {}".format(filename, start, stop)
#add our start / stop coordinates
self.map[filename][start] = stop
#add these coordinates to our all_coords map
for i in range(start,stop):
self.all_coords[filename][i] = 1
if pattern != "":
self.add_pattern(filename, start, stop, pattern)
return True, None
def add_pattern(self, filename, start, stop, pattern):
""" adds this pattern to this start coord """
if filename not in self.coord2pattern:
self.coord2pattern[filename] = {}
if start not in self.coord2pattern[filename]:
self.coord2pattern[filename][start] = []
self.coord2pattern[filename][start].append(pattern)
def add_extend(self, filename, start, stop, pattern=""):
""" adds a new coordinate to the coordinate map
if overlaps with another, will extend to the larger size
"""
# if filename == "./data/i2b2_notes/167-02.txt":
if self.debug:
print("add_extend", start, stop)
if filename not in self.map:
self.map[filename] = {}
overlaps = self.max_overlap(filename, start, stop)
# if filename == "./data/i2b2_notes/167-02.txt":
# print(self.map)
def clear_overlaps(filename, lst):
for o in lst:
self.remove(filename, o["orig_start"], o["orig_end"])
if len(overlaps) == 0:
#no overlap, just save these coordinates
self.add(filename,start,stop,pattern=pattern, overlap=True)
# if filename == "./data/i2b2_notes/167-02.txt":
# print("No overlaps:")
# print(filename,start,stop,pattern)
elif len(overlaps) == 1:
clear_overlaps(filename, overlaps)
#1 overlap, save this value
o = overlaps[0]
self.add(filename,o["new_start"],o["new_stop"],pattern=pattern, overlap=True)
# if filename == "./data/i2b2_notes/167-02.txt":
# print("One overlap:")
# print(filename,start,stop,pattern)
else:
clear_overlaps(filename, overlaps)
#greater than 1 overlap, by default this is sorted because of scan order
o1 = overlaps[0]
o2 = overlaps[-1]
self.add(filename,o2["new_start"], o1["new_stop"],pattern=pattern, overlap=True)
# if filename == "./data/i2b2_notes/167-02.txt":
# print("Multiple overlaps:")
# print(filename,start,stop,pattern)
return True, None
def remove(self, filename, start, stop):
""" Removes this coordinate pairing from the map, all_coords, and coord2pattern"""
if filename not in self.map:
raise Exception('Filename does not exist', filename)
#delete from our map structure
if start in self.map[filename]:
del self.map[filename][start]
#delete any of these coordinates in our all_coords data structure
for i in range(start, stop+1):
if i in self.all_coords:
del self.all_coords[i]
return True, None
def scan(self):
""" does an inorder scan of the coordinates and their values"""
for fn in self.map:
coords = list(self.map[fn].keys())
coords.sort()
for coord in coords:
yield fn,coord,self.map[fn][coord]
def keys(self):
for fn in self.map:
yield fn
def get_coords(self, filename, start):
stop = self.map[filename][start]
return start,stop
def filecoords(self, filename):
"""
generator does an inorder scan of the coordinates for this file
"""
if filename not in self.map:
return
#raise Exception('Filename not found', filename)
coords = sorted(self.map[filename].keys())
for coord in coords:
yield coord,self.map[filename][coord]
def does_exist(self, filename, index):
""" Simple check to see if this index is a hit (start of coordinates)"""
if index in self.map[filename]:
return True
return False
def does_overlap(self, filename, start, stop):
""" Check if this coordinate overlaps with any existing range"""
ranges = [list(range(key,self.map[filename][key]+1)) for key in self.map[filename]]
all_coords = [item for sublist in ranges for item in sublist]
#removing all_coords implementation until we write some tests
for i in range(start, stop+1):
if i in all_coords:
return True
return False
def calc_overlap(self, filename, start, stop):
""" given a set of coordinates, will calculate all overlaps
perf: stop after we know we won't hit any more
perf: use binary search approach
"""
overlaps = []
for s in self.map[filename]:
e = self.map[filename][s]
if s >= start or s <= stop:
#We found an overlap
if e <= stop:
overlaps.append({"start":s, "stop":e})
else:
overlaps.append({"start":s, "stop":stop})
elif e >= start or e <= stop:
if s >= start:
overlaps.append({"start":s, "stop":e})
else:
overlaps.append({"start":start, "stop":e})
return overlaps
def max_overlap(self, filename, start, stop):
""" given a set of coordinates, will calculate max of all overlaps
perf: stop after we know we won't hit any more
perf: use binary search approach
"""
overlaps = []
for s in self.map[filename]:
e = self.map[filename][s]
if start >= s and start <= e:
#We found an overlap
if stop >= e:
overlaps.append({"orig_start":s, "orig_end":e, "new_start":s, "new_stop":stop})
else:
overlaps.append({"orig_start":s, "orig_end":e, "new_start":s, "new_stop":e})
elif stop >= s and stop <= e:
if start <= s:
overlaps.append({"orig_start":s, "orig_end":e, "new_start":start, "new_stop":e})
else:
overlaps.append({"orig_start":s, "orig_end":e, "new_start":s, "new_stop":e})
return overlaps
def add_file(self, filename):
""" add our fileto map, may not have any coordinates"""
self.map[filename] = {}
def get_complement(self, filename, text):
""" get the complementary coordinates of the input coordinate map (excludes punctuation)"""
complement_coordinate_map = {}
current_map_coordinates = []
for start_key in self.map[filename]:
start = start_key
stop = self.map[filename][start_key]
current_map_coordinates += range(start,stop)
text_coordinates = list(range(0,len(text)))
complement_coordinates = list(set(text_coordinates) - set(current_map_coordinates))
# Remove punctuation from complement coordinates
punctuation_matcher = re.compile(r"[^a-zA-Z0-9*]")
for i in range(0, len(text)):
if punctuation_matcher.match(text[i]):
if i in complement_coordinates:
complement_coordinates.remove(i)
# Group complement coordinates into ranges
def to_ranges(iterable):
iterable = sorted(set(iterable))
for key, group in itertools.groupby(enumerate(iterable), lambda t: t[1] - t[0]):
group = list(group)
yield group[0][1], group[-1][1]+1
complement_coordinate_ranges = list(to_ranges(complement_coordinates))
# Create complement dictionary
for tup in complement_coordinate_ranges:
start = tup[0]
stop = tup[1]
complement_coordinate_map[start] = stop
return complement_coordinate_map