File size: 4,022 Bytes
73025cd
 
 
 
 
 
 
 
 
 
12f6038
 
 
 
 
 
 
 
73025cd
 
 
 
12f6038
 
 
 
 
 
 
 
73025cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfc8a2f
73025cd
 
 
12f6038
 
 
 
 
 
 
 
73025cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d75003
73025cd
 
 
e28185c
73025cd
 
 
 
 
 
 
 
 
 
 
 
 
e28185c
 
 
73025cd
e28185c
73025cd
e28185c
 
 
 
 
 
73025cd
 
 
12f6038
3a9740d
8d75003
73025cd
 
f3ba2de
73025cd
8d75003
 
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
from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_ORIGINAL_FEATURE_NAMES = [
    "id",
    "clump_thickness",
    "uniformity_of_cell_size",
    "uniformity_of_cell_shape",
    "marginal_adhesion",
    "single_epithelial_cell_size",
    "bare_nuclei",
    "bland_chromatin",
    "normal_nucleoli",
    "mitoses",
    "is_cancer"
]
_BASE_FEATURE_NAMES = [
    "clump_thickness",
    "uniformity_of_cell_size",
    "uniformity_of_cell_shape",
    "marginal_adhesion",
    "single_epithelial_cell_size",
    "bare_nuclei",
    "bland_chromatin",
    "normal_nucleoli",
    "mitoses",
    "is_cancer"
]

DESCRIPTION = "Breast dataset for cancer prediction."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Original%29"
_URLS = ("https://huggingface.co/datasets/mstz/breast/raw/main/breast-cancer-wisconsin.data")
_CITATION = """
@article{wolberg1990multisurface,
  title={Multisurface method of pattern separation for medical diagnosis applied to breast cytology.},
  author={Wolberg, William H and Mangasarian, Olvi L},
  journal={Proceedings of the national academy of sciences},
  volume={87},
  number={23},
  pages={9193--9196},
  year={1990},
  publisher={National Acad Sciences}
}
"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/breast/raw/main/breast-cancer-wisconsin.data",
}
features_types_per_config = {
    "cancer": {
        "clump_thickness": datasets.Value("int8"),
        "uniformity_of_cell_size": datasets.Value("int8"),
        "uniformity_of_cell_shape": datasets.Value("int8"),
        "marginal_adhesion": datasets.Value("int8"),
        "single_epithelial_cell_size": datasets.Value("int8"),
        "bare_nuclei": datasets.Value("int8"),
        "bland_chromatin": datasets.Value("int8"),
        "normal_nucleoli": datasets.Value("int8"),
        "mitoses": datasets.Value("int8"),
        "is_cancer": 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 BreastConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(BreastConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Breast(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "cancer"
    BUILDER_CONFIGS = [
        BreastConfig(name="cancer",
                   description="Breast cancer 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):
        if self.config.name == "cancer":
            data = pandas.read_csv(filepath, header=None)
            data.columns=_ORIGINAL_FEATURE_NAMES

            data = self.preprocess(data, config=self.config.name)

            for row_id, row in data.iterrows():
                data_row = dict(row)

                yield row_id, data_row
        else:
            raise ValueError(f"Unknown config: {self.config.name}")

    def preprocess(self, data: pandas.DataFrame, config: str = "cancer") -> pandas.DataFrame:
        data.drop("id", axis="columns", inplace=True)

        data = data[data.bare_nuclei != "?"]
        data = data.astype({f: int for f in data.columns})
        
        data.columns = _BASE_FEATURE_NAMES
        data.loc[:, "is_cancer"] = data.is_cancer.apply(lambda x: 0 if x == 2 else 1)
        

        return data