This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIMICDataset.py
65 lines (50 loc) · 1.67 KB
/
MIMICDataset.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
import torch
import pandas as pd
from torch.utils.data import Dataset
import ast
class MimicDataset(Dataset):
"""Mimic Dataset - a dataset of diagnostic reports and their corresponding icd9 labels"""
def __init__(
self,
text,
labels,
classes: list,
class2id: dict | None = None,
id2class: dict | None = None,
):
"""
Arguments:
csv_file (string): Path to the csv file with data and annotations
"""
self.text = text
# create a dictionary of icd codes and their corresponding index in the list
self.labels = labels
self.icd_labels = classes
self.class2id = (
{class_: id for id, class_ in enumerate(classes)}
if class2id is None
else class2id
)
self.id2class = (
{id: class_ for class_, id in self.class2id.items()}
if id2class is None
else id2class
)
self.icd_size = len(self.icd_labels)
def __len__(self):
return len(self.text)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
# get the tokenized data
item = self.text[idx]
icd_codes = ast.literal_eval(self.labels[idx])
# create the label tensor
label_tensor = torch.zeros(self.icd_size)
for code in icd_codes:
label_tensor[self.class2id[code]] = 1.0
item["labels"] = label_tensor
# squeeze item to remove the extra dimension
item["input_ids"] = item["input_ids"].squeeze()
item["attention_mask"] = item["attention_mask"].squeeze()
return item