hypo / hypo.py
mstz's picture
Upload hypo.py
b4b6905
raw
history blame contribute delete
No virus
5.27 kB
"""Hypo Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {
"class": {
"negative": 0,
"compensatedhypothyroid": 1,
"secondaryhypothyroid": 2,
"primaryhypothyroid": 3
}
}
DESCRIPTION = "Hypo dataset."
_HOMEPAGE = ""
_URLS = ("")
_CITATION = """"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/hypo/resolve/main/hypo.data"
}
features_types_per_config = {
"hypo": {
"age": datasets.Value("int64"),
"sex": datasets.Value("string"),
"on_thyroxine": datasets.Value("bool"),
"query_on_thyroxine": datasets.Value("bool"),
"on_antithyroid_medication": datasets.Value("bool"),
"sick": datasets.Value("bool"),
"pregnant": datasets.Value("bool"),
"thyroid_surgery": datasets.Value("bool"),
"I131_treatment": datasets.Value("bool"),
"query_hypothyroid": datasets.Value("bool"),
"query_hyperthyroid": datasets.Value("bool"),
"lithium": datasets.Value("bool"),
"goitre": datasets.Value("bool"),
"tumor": datasets.Value("bool"),
"hypopituitary": datasets.Value("bool"),
"psych": datasets.Value("bool"),
"TSH_measured": datasets.Value("bool"),
"TSH": datasets.Value("string"),
"T3_measured": datasets.Value("bool"),
"T3": datasets.Value("float64"),
"TT4_measured": datasets.Value("bool"),
"TT4": datasets.Value("float64"),
"T4U_measured": datasets.Value("bool"),
"T4U": datasets.Value("float64"),
"FTI_measured": datasets.Value("bool"),
"FTI": datasets.Value("float64"),
"TBG_measured": datasets.Value("string"),
"referral_source": datasets.Value("string"),
"class": datasets.ClassLabel(num_classes=4,
names=("negative", "compensated hypothyroid", "secondary hypothyroid", "primary hypothyroid"))
},
"has_hypo": {
"age": datasets.Value("int64"),
"sex": datasets.Value("string"),
"on_thyroxine": datasets.Value("bool"),
"query_on_thyroxine": datasets.Value("bool"),
"on_antithyroid_medication": datasets.Value("bool"),
"sick": datasets.Value("bool"),
"pregnant": datasets.Value("bool"),
"thyroid_surgery": datasets.Value("bool"),
"I131_treatment": datasets.Value("bool"),
"query_hypothyroid": datasets.Value("bool"),
"query_hyperthyroid": datasets.Value("bool"),
"lithium": datasets.Value("bool"),
"goitre": datasets.Value("bool"),
"tumor": datasets.Value("bool"),
"hypopituitary": datasets.Value("bool"),
"psych": datasets.Value("bool"),
"TSH_measured": datasets.Value("bool"),
"TSH": datasets.Value("string"),
"T3_measured": datasets.Value("bool"),
"T3": datasets.Value("string"),
"TT4_measured": datasets.Value("bool"),
"TT4": datasets.Value("float64"),
"T4U_measured": datasets.Value("bool"),
"T4U": datasets.Value("float64"),
"FTI_measured": datasets.Value("bool"),
"FTI": datasets.Value("float64"),
"TBG_measured": datasets.Value("string"),
"referral_source": datasets.Value("string"),
"class": datasets.ClassLabel(num_classes=2)
},
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
class HypoConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(HypoConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Hypo(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "hypo"
BUILDER_CONFIGS = [
HypoConfig(name="hypo", description="Hypo for multiclass classification."),
HypoConfig(name="has_hypo", description="Hypo for binary classification."),
]
def _info(self):
info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
features=features_per_config[self.config.name])
return info
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
downloads = dl_manager.download_and_extract(urls_per_split)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
]
def _generate_examples(self, filepath: str):
data = pandas.read_csv(filepath)
data = self.preprocess(data)
for row_id, row in data.iterrows():
data_row = dict(row)
yield row_id, data_row
def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame:
data.drop("id", axis="columns", inplace=True)
data.drop("TBG", axis="columns", inplace=True)
data = data[data.age != "?"]
data = data[data.sex != "?"]
data = data[data.TSH != "?"]
data.loc[data.T3 == "?", "T3"] = -1
data.loc[data.TT4 == "?", "TT4"] = -1
data.loc[data.T4U == "?", "T4U"] = -1
data.loc[data.FTI == "?", "FTI"] = -1
data = data.infer_objects()
for feature in _ENCODING_DICS:
encoding_function = partial(self.encode, feature)
data[feature] = data[feature].apply(encoding_function)
if self.config.name == "has_hypo":
data["class"] = data["class"].apply(lambda x: 0 if x == 0 else 1)
print("has hypo\n\n\n")
print("classes")
print(data["class"].unique())
return data[list(features_types_per_config[self.config.name].keys())]
def encode(self, feature, value):
if feature in _ENCODING_DICS:
return _ENCODING_DICS[feature][value]
raise ValueError(f"Unknown feature: {feature}")