File size: 3,701 Bytes
5543393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Dataset loading script.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1ceCOKzOpSOMsgkw__rxtBVv7R3QrSkbg
"""

import csv
import os

import datasets

_DESCRIPTION = """
The datasets were collected and published to present the educational level of NC population in different areas. The educational attainment for the black population data can raise concern for the educational equity issue in North Carolina. The combined dataset aims to offer a holistic perspective on educational levels and equity, with a specific focus on the educational attainment of the Black population aged 25 and over.
"""

_HOMEPAGE = "https://huggingface.co/datasets/YXu120/NC_Education"

_LICENSE = "cc-by-sa-4.0"

_URL = "https://drive.google.com/uc?id=1Au9xwsnDkRx4TMWwndWKbzLR5u9WXUQZ"

class NCEducationDataset(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.1")

    def _info(self):
        features = datasets.Features(
            {
                "area_name": datasets.Value("string"),
                "area_type": datasets.Value("string"),
                "years": datasets.Sequence(
                    {
                        "year": datasets.Value("int64"),
                        "variables": datasets.Sequence(
                            {
                                "variable": datasets.Value("string"),
                                "value": datasets.Value("int64"),
                                "value_black": datasets.Value("int64")
                            }
                        )
                    }
                )
            }
        )

        return datasets.DatasetInfo(
            description = _DESCRIPTION,
            features = features,
            homepage = _HOMEPAGE,
            license = _LICENSE,
        )

    def _split_generators(self, dl_manager):
        data_file = dl_manager.download(_URL)
        return [
            datasets.SplitGenerator(
                name = "train",
                gen_kwargs = {
                    "filepath": data_file,
                },
            )
        ]

    def _generate_examples(self, filepath):
        data = {}
        with open(filepath, "r", encoding="utf-8") as file:
            csv_reader = csv.DictReader(file)
            for row in csv_reader:
                area_name = row["area_name"]
                area_type = row["area_type"]
                year = int(row["year"])
                variable = row["variable"]
                value = int(row["value"]) if row["value"] else None
                value_black = int(row["value_black"]) if row["value_black"] else None

                if area_name not in data:
                    data[area_name] = {
                        "area_type": area_type,
                        "years": {}
                    }

                if year not in data[area_name]["years"]:
                    data[area_name]["years"][year] = []

                data[area_name]["years"][year].append({
                    "variable": variable,
                    "value": value,
                    "value_black": value_black
                })

        for idx, (area_name, area_data) in enumerate(data.items()):
            years_data = []
            for year, variables in area_data["years"].items():
                year_data = {
                    "year": year,
                    "variables": variables
                }
                years_data.append(year_data)

            yield idx, {
                "area_name": area_name,
                "area_type": area_data["area_type"],
                "years": years_data
            }