File size: 9,690 Bytes
e3ef0b9 |
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from typing import List, Dict
import os
import time
import logging
import json
import gzip
from dataclasses import dataclass, field
import torch
from torch import Tensor as T
from transformers import PreTrainedTokenizer
logger = logging.getLogger()
@dataclass
class Mention:
cui: str
start: int
end: int
text: str
types: str
@dataclass
class ContextualMention:
mention: str
cuis: List[str]
ctx_l: str
ctx_r: str
def to_tensor(self, tokenizer: PreTrainedTokenizer, max_length: int) -> T:
ctx_l_ids = tokenizer.encode(
text=self.ctx_l,
add_special_tokens=False,
max_length=max_length,
truncation=True,
)
ctx_r_ids = tokenizer.encode(
text=self.ctx_r,
add_special_tokens=False,
max_length=max_length,
truncation=True,
)
mention_ids = tokenizer.encode(
text=self.mention,
add_special_tokens=False,
max_length=max_length,
truncation=True,
)
# Concatenate context and mention to the max length.
token_ids = tokenizer.convert_tokens_to_ids(['<ENT>']) + mention_ids \
+ tokenizer.convert_tokens_to_ids(['</ENT>'])
max_ctx_len = max_length - len(token_ids) - 2 # Exclude [CLS] and [SEP]
max_ctx_l_len = max_ctx_len // 2
max_ctx_r_len = max_ctx_len - max_ctx_l_len
if len(ctx_l_ids) < max_ctx_l_len and len(ctx_r_ids) < max_ctx_r_len:
token_ids = ctx_l_ids + token_ids + ctx_r_ids
elif len(ctx_l_ids) >= max_ctx_l_len and len(ctx_r_ids) >= max_ctx_r_len:
token_ids = ctx_l_ids[-max_ctx_l_len:] + token_ids \
+ ctx_r_ids[:max_ctx_r_len]
elif len(ctx_l_ids) >= max_ctx_l_len:
ctx_l_len = max_ctx_len - len(ctx_r_ids)
token_ids = ctx_l_ids[-ctx_l_len:] + token_ids + ctx_r_ids
else:
ctx_r_len = max_ctx_len - len(ctx_l_ids)
token_ids = ctx_l_ids + token_ids + ctx_r_ids[:ctx_r_len]
token_ids = [tokenizer.cls_token_id] + token_ids
# The above snippet doesn't guarantee the max length limit.
token_ids = token_ids[:max_length - 1] + [tokenizer.sep_token_id]
if len(token_ids) < max_length:
token_ids = token_ids + [tokenizer.pad_token_id] * (max_length - len(token_ids))
return torch.tensor(token_ids)
@dataclass
class Document:
id: str = None
title: str = None
abstract: str = None
mentions: List[Mention] = field(default_factory=list)
def concatenate_text(self) -> str:
return ' '.join([self.title, self.abstract])
@classmethod
def from_PubTator(cls, path: str, split_path_prefix: str) -> Dict[str, List]:
docs = []
with gzip.open(path, 'rb') as f:
for b in f.read().decode().strip().split('\n\n'):
d = cls()
s = ''
for i, ln in enumerate(b.split('\n')):
if i == 0:
id, type, text = ln.strip().split('|', 2)
assert type == 't'
d.id, d.title = id, text
elif i == 1:
id, type, text = ln.strip().split('|', 2)
assert type == 'a'
assert d.id == id
d.abstract = text
s = d.concatenate_text()
else:
items = ln.strip().split('\t')
assert d.id == items[0]
cui = items[5].split('UMLS:')[-1]
assert len(cui) == 8, breakpoint()
m = Mention(
cui=cui,
start=int(items[1]),
end=int(items[2]),
text=items[3],
types=items[4].split(',')
)
assert m.text == s[m.start: m.end]
d.mentions.append(m)
docs.append(d)
dataset = split_dataset(docs, split_path_prefix)
print_dataset_stats(dataset)
return dataset
def to_contextual_mentions(self, max_length: int = 64) -> List[ContextualMention]:
text = self.concatenate_text()
mentions = []
for m in self.mentions:
assert m.text == text[m.start:m.end]
# Context
ctx_l, ctx_r = text[:m.start].strip().split(), text[m.end:].strip().split()
ctx_l, ctx_r = ' '.join(ctx_l[-max_length:]), ' '.join(ctx_r[:max_length])
cm = ContextualMention(
mention=m.text,
cuis=[m.cui],
ctx_l=ctx_l,
ctx_r=ctx_r,
)
mentions.append(cm)
return mentions
def split_dataset(docs: List, split_path_prefix: str) -> Dict[str, List]:
split_kv = {'train': 'trng', 'dev': 'dev', 'test': 'test'}
id_to_split = {}
dataset = {}
for k, v in split_kv.items():
dataset[k] = []
path = split_path_prefix + v + '.txt'
for i in open(path, encoding='utf-8').read().strip().split('\n'):
assert i not in id_to_split, breakpoint()
id_to_split[i] = k
for doc in docs:
split = id_to_split[doc.id]
dataset[split].append(doc)
return dataset
def print_dataset_stats(dataset: Dict[str, List[Document]]) -> None:
all_docs = []
for v in dataset.values():
all_docs.extend(v)
for split, docs in {'all': all_docs, **dataset}.items():
logger.info(f"***** {split} *****")
logger.info(f"Documents: {len(docs)}")
logger.info(f"Mentions: {sum(len(d.mentions) for d in docs)}")
cuis = set()
for d in docs:
for m in d.mentions:
cuis.add(m.cui)
logger.info(f"Mentioned concepts: {len(cuis)}")
class MedMentionsDataset(torch.utils.data.Dataset):
def __init__(self, dataset_path: str, split: str) -> None:
super().__init__()
self.dataset_path = dataset_path
self.docs = Document.from_PubTator(
path=os.path.join(self.dataset_path, 'corpus_pubtator.txt.gz'),
split_path_prefix=os.path.join(self.dataset_path, 'corpus_pubtator_pmids_')
)[split]
self.mentions = []
self.name_to_cuis = {}
self._post_init()
def _post_init(self):
for d in self.docs:
self.mentions.extend(d.to_contextual_mentions())
for m in self.mentions:
if m.mention not in self.name_to_cuis:
self.name_to_cuis[m.mention] = set()
self.name_to_cuis[m.mention].update(m.cuis)
def __getitem__(self, index: int) -> ContextualMention:
return self.mentions[index]
def __len__(self) -> int:
return len(self.mentions)
class PreprocessedDataset(torch.utils.data.Dataset):
def __init__(self, dataset_path: str) -> None:
super().__init__()
self.file = dataset_path
self.data = []
self.load_data()
def load_data(self) -> None:
with open(self.file, encoding='utf-8') as f:
logger.info("Reading file %s" % self.file)
for ln in f:
if ln.strip():
self.data.append(json.loads(ln))
logger.info("Loaded data size: {}".format(len(self.data)))
def __getitem__(self, index: int) -> ContextualMention:
d = self.data[index]
return ContextualMention(
ctx_l=d['context_left'],
ctx_r=d['context_right'],
mention=d['mention'],
cuis=d['cuis'],
)
def __len__(self) -> int:
return len(self.data)
def generate_vectors(
encoder: torch.nn.Module,
tokenizer: PreTrainedTokenizer,
dataset: torch.utils.data.Dataset,
batch_size: int,
max_length: int,
is_prototype: bool = False,
):
n = len(dataset)
total = 0
results = []
start_time = time.time()
logger.info("Start encoding...")
for i, batch_start in enumerate(range(0, n, batch_size)):
batch = [dataset[i] for i in range(batch_start, min(n, batch_start + batch_size))]
batch_token_tensors = [m.to_tensor(tokenizer, max_length) for m in batch]
ids_batch = torch.stack(batch_token_tensors, dim=0).cuda()
seg_batch = torch.zeros_like(ids_batch)
attn_mask = (ids_batch != tokenizer.pad_token_id)
with torch.inference_mode():
out = encoder(
input_ids=ids_batch,
token_type_ids=seg_batch,
attention_mask=attn_mask
)
out = out[0][:, 0, :]
out = out.cpu()
num_mentions = out.size(0)
total += num_mentions
if is_prototype:
meta_batch = [{'cuis': m.cuis} for m in batch]
assert len(meta_batch) == num_mentions
results.extend([(meta_batch[i], out[i].view(-1).numpy()) for i in range(num_mentions)])
else:
results.extend(out.cpu().split(1, dim=0))
if (i + 1) % 10 == 0:
eta = (n - total) * (time.time() - start_time) / 60 / total
logger.info(f"Batch={i + 1}, Encoded mentions={total}, ETA={eta:.1f}m")
assert len(results) == n
logger.info(f"Total encoded mentions={n}")
if not is_prototype:
results = torch.cat(results, dim=0)
return results
|