File size: 3,090 Bytes
965f33e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f571139
 
 
 
965f33e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567e325
965f33e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce74f9c
dd32fea
567e325
 
 
 
 
 
 
afc0e36
965f33e
 
 
778b279
 
 
65a666d
 
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
"""Blood"""

from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
    "months_since_last_donation",
    "total_donation",
    "total_blood_donated_in_cc",
    "months_since_last_donation",
    "has_donated_last_month"
]

DESCRIPTION = "Blood dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Blood"
_URLS = ("https://huggingface.co/datasets/mstz/blood/raw/blood.csv")
_CITATION = """
@misc{misc_blood_transfusion_service_center_176,
  author       = {Yeh,I-Cheng},
  title        = {{Blood Transfusion Service Center}},
  year         = {2008},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C5GS39}}
}"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/blood/raw/main/transfusion.data"
}
features_types_per_config = {
    "blood": {
        "months_since_last_donation": datasets.Value("int64"),
        "total_donation": datasets.Value("int64"),
        "total_blood_donated_in_cc": datasets.Value("int64"),
        "months_since_last_donation": datasets.Value("int64"),
        "has_donated_last_month": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
    }
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


class BloodConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(BloodConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Blood(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "blood"
    BUILDER_CONFIGS = [
        BloodConfig(name="blood", description="Blood 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, header=None)
        data.columns = _BASE_FEATURE_NAMES

        data = data.astype({
            "months_since_last_donation": "int64",
            "total_donation": "int64",
            "total_blood_donated_in_cc": "int64",
            "months_since_last_donation": "int64",
            "has_donated_last_month": "int64"
        })        

        for row_id, row in data.iterrows():
            data_row = dict(row)
            
            if isinstance(data_row["months_since_last_donation"], pandas.Series):
                data_row["months_since_last_donation"] = data_row["months_since_last_donation"].values[0]

            yield row_id, data_row