File size: 3,013 Bytes
3427d84
e706125
 
 
 
c16288b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e706125
c16288b
e706125
c16288b
 
e706125
c16288b
 
e706125
c16288b
 
e706125
c16288b
 
 
 
 
e706125
c16288b
e706125
c16288b
 
 
 
e706125
c16288b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e706125
c16288b
 
 
 
 
 
 
 
 
 
 
 
 
 
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

"""The dataset contains 13,955 imgaes of pills inside medication bottles, 
which are from a top down view. They are labeled with 20 distinct National 
Drug Code (NDC) and each image is associated with an image id. The dataset
 is split into train, test, and validation sets. """


import os
from typing import List

import datasets


_CITATION = """\
@InProceedings{University of Michigan - Deep Blue Data,
title = {Images of pills inside medication bottles dataset},
author={Lester, C. A., Al Kontar, R., Chen, Q.},
year={2022}
}
"""


_DESCRIPTION = """\
This dataset contains pills images inside medication bottles from a top down view, with National Drug Code (NDC) and image id.
"""


_HOMEPAGE = "https://deepblue.lib.umich.edu/data/concern/data_sets/6d56zw997?locale=en#items_display"


_LICENSE = "CC BY 4.0"


_URLS = {
    "dataset": "https://deepblue.lib.umich.edu/data/downloads/rr171x63c",
}



class NewDataset(datasets.GeneratorBasedBuilder):
    """The dataset contains train, test, and validation data for pills images inside medication bottles"""
    _URLS = _URLS
    VERSION = datasets.Version("1.1.0")

    def _info(self):

        features = datasets.Features(
            {
                "image": datasets.Image(),
                "id": datasets.Value("string"),
                "ndc": datasets.Value("string")
            }
        )
        
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:

        data_dir = dl_manager.download_and_extract(self._URLS["dataset"])
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": os.path.join(data_dir, "NLM20/train")},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": os.path.join(data_dir, "NLM20/test")},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": os.path.join(data_dir, "NLM20/valid")},
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath):

        for ndc in os.listdir(filepath):
            ndc_path = os.path.join(filepath, ndc)
            if os.path.isdir(ndc_path):
                for image_file in os.listdir(ndc_path):
                    image_path = os.path.join(ndc_path, image_file)
                    image_id = os.path.splitext(image_file)[0]
                    yield image_id, {
                        "image": image_path,
                        "id": image_id,
                        "ndc": ndc,
                        }