Create test_dataset.py
Browse files- test_dataset.py +159 -0
test_dataset.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def _info(self):
|
2 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
3 |
+
if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
|
4 |
+
features = datasets.Features(
|
5 |
+
{
|
6 |
+
"year": datasets.Value("int32"),
|
7 |
+
"locationabbr": datasets.Value("string"),
|
8 |
+
"locationdesc": datasets.Value("string"),
|
9 |
+
"geolocation": datasets.Features({"latitude": datasets.Value("float32"), "longitude": datasets.Value("float32")}),
|
10 |
+
"disease_type": datasets.Value("int32"),
|
11 |
+
"data_value_type": datasets.Value("int32"),
|
12 |
+
"data_value": datasets.Value("float32"),
|
13 |
+
"break_out_category": datasets.Value("string"),
|
14 |
+
"break_out_details": datasets.Value("string"),
|
15 |
+
"break_out_type": datasets.Value("int32"),
|
16 |
+
"life_expectancy": datasets.Value("float32")
|
17 |
+
# These are the features of your dataset like images, labels ...
|
18 |
+
}
|
19 |
+
)
|
20 |
+
return datasets.DatasetInfo(
|
21 |
+
# This is the description that will appear on the datasets page.
|
22 |
+
description=_DESCRIPTION,
|
23 |
+
# This defines the different columns of the dataset and their types
|
24 |
+
features=features, # Here we define them above because they are different between the two configurations
|
25 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
26 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
27 |
+
# supervised_keys=("sentence", "label"),
|
28 |
+
# Homepage of the dataset for documentation
|
29 |
+
homepage=_HOMEPAGE,
|
30 |
+
# License for the dataset if available
|
31 |
+
license=_LICENSE,
|
32 |
+
# Citation for the dataset
|
33 |
+
citation=_CITATION,
|
34 |
+
)
|
35 |
+
|
36 |
+
def _split_generators(self, dl_manager):
|
37 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
38 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
39 |
+
|
40 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
41 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
42 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
43 |
+
dl_paths = dl_manager.download_and_extract({
|
44 |
+
'train_csv': 'https://drive.google.com/file/d/1eChYmZ3RMq1v-ek1u6DD2m_dGIrz3sbi/view?usp=sharing'
|
45 |
+
})
|
46 |
+
return [
|
47 |
+
datasets.SplitGenerator(
|
48 |
+
name=datasets.Split.TRAIN,
|
49 |
+
gen_kwargs={
|
50 |
+
"csvpath": dl_paths['train_csv'],
|
51 |
+
},
|
52 |
+
),
|
53 |
+
]
|
54 |
+
|
55 |
+
def _generate_examples(self, csvpath):
|
56 |
+
with open(csvpath, encoding="utf-8") as f:
|
57 |
+
reader = csv.DictReader(f)
|
58 |
+
|
59 |
+
for key, row in enumerate(reader):
|
60 |
+
yield key, {
|
61 |
+
"year": int(row["Year"]),
|
62 |
+
"location_abbr": row["LocationAbbr"],
|
63 |
+
"location_desc": row["LocationDesc"],
|
64 |
+
"geolocation": {
|
65 |
+
"latitude": float(row["latitude"]),
|
66 |
+
"longitude": float(row["longitude"])
|
67 |
+
},
|
68 |
+
"disease_type": int(row["Disease_Type"]),
|
69 |
+
"data_value_type": int(row["Data_Value_Type"]),
|
70 |
+
"data_value": float(row["Data_Value"]),
|
71 |
+
"break_out_category": row["Break_Out_Category"],
|
72 |
+
"break_out_details": row["Break_Out_Details"],
|
73 |
+
"break_out_type": int(row["Break_Out_Type"]),
|
74 |
+
"life_expectancy": float(row["Life_Expectancy"]) if row["Life_Expectancy"] else None
|
75 |
+
}
|
76 |
+
|
77 |
+
@staticmethod
|
78 |
+
def preprocess_data(filepath):
|
79 |
+
data = pd.read_csv("/content/drive/MyDrive/National_Vital_Statistics_System__NVSS__-_National_Cardiovascular_Disease_Surveillance_Data_20240129.csv")
|
80 |
+
data = data[['YearStart', 'LocationAbbr', 'LocationDesc', 'Geolocation', 'Topic', 'Question', 'Data_Value_Type', 'Data_Value', 'Data_Value_Alt',
|
81 |
+
'Low_Confidence_Limit', 'High_Confidence_Limit', 'Break_Out_Category', 'Break_Out']]
|
82 |
+
def convert_to_tuple(geo_str):
|
83 |
+
if isinstance(geo_str, str):
|
84 |
+
geo_str = geo_str.replace('POINT (', '').replace(')', '')
|
85 |
+
lon, lat = map(float, geo_str.split())
|
86 |
+
return (lon, lat)
|
87 |
+
else:
|
88 |
+
return geo_str
|
89 |
+
|
90 |
+
data['Geolocation'] = data['Geolocation'].apply(convert_to_tuple)
|
91 |
+
disease_columns = [
|
92 |
+
'Major cardiovascular disease mortality rate among US adults (18+); NVSS',
|
93 |
+
'Diseases of the heart (heart disease) mortality rate among US adults (18+); NVSS',
|
94 |
+
'Acute myocardial infarction (heart attack) mortality rate among US adults (18+); NVSS',
|
95 |
+
'Coronary heart disease mortality rate among US adults (18+); NVSS',
|
96 |
+
'Heart failure mortality rate among US adults (18+); NVSS',
|
97 |
+
'Cerebrovascular disease (stroke) mortality rate among US adults (18+); NVSS',
|
98 |
+
'Ischemic stroke mortality rate among US adults (18+); NVSS',
|
99 |
+
'Hemorrhagic stroke mortality rate among US adults (18+); NVSS'
|
100 |
+
]
|
101 |
+
|
102 |
+
disease_column_mapping = {column_name: index for index, column_name in enumerate(disease_columns)}
|
103 |
+
data['Question'] = data['Question'].apply(lambda x: disease_column_mapping.get(x, -1))
|
104 |
+
|
105 |
+
sex_columns = ['Male', 'Female']
|
106 |
+
sex_column_mapping = {column_name: index + 1 for index, column_name in enumerate(sex_columns)}
|
107 |
+
|
108 |
+
age_columns = ['18-24', '25-44', '45-64', '65+']
|
109 |
+
age_column_mapping = {column_name: index + 1 for index, column_name in enumerate(age_columns)}
|
110 |
+
|
111 |
+
race_columns = ['Non-Hispanic White', 'Non-Hispanic Black', 'Hispanic', 'Other']
|
112 |
+
race_column_mapping = {column_name: index + 1 for index, column_name in enumerate(race_columns)}
|
113 |
+
|
114 |
+
def map_break_out_category(value):
|
115 |
+
if value in sex_column_mapping:
|
116 |
+
return sex_column_mapping[value]
|
117 |
+
elif value in age_column_mapping:
|
118 |
+
return age_column_mapping[value]
|
119 |
+
elif value in race_column_mapping:
|
120 |
+
return race_column_mapping[value]
|
121 |
+
else:
|
122 |
+
return value
|
123 |
+
|
124 |
+
data['Break_Out_Type'] = data['Break_Out'].apply(map_break_out_category)
|
125 |
+
data.drop(columns=['Topic', 'Low_Confidence_Limit', 'High_Confidence_Limit', 'Data_Value_Alt'], axis=1, inplace=True)
|
126 |
+
data['Data_Value_Type'] = data['Data_Value_Type'].apply(lambda x: 1 if x == 'Age-Standardized' else 0)
|
127 |
+
data.rename(columns={'Question':'Disease_Type', 'YearStart':'Year', 'Break_Out':'Break_Out_Details'}, inplace=True)
|
128 |
+
data['Break_Out_Type'] = data['Break_Out_Type'].replace('Overall', 0)
|
129 |
+
|
130 |
+
lt2000 = pd.read_csv("https://drive.google.com/file/d/1ktRNl7jg0Z83rkymD9gcsGLdVqVaFtd-/view?usp=drive_link")
|
131 |
+
lt2000 = lt2000[(lt2000['race_name'] == 'Total') & (lt2000['age_name'] == '<1 year')]
|
132 |
+
lt2000 = lt2000[['location_name', 'val']]
|
133 |
+
lt2000.rename(columns={'val':'Life_Expectancy'}, inplace=True)
|
134 |
+
|
135 |
+
lt2005 = pd.read_csv("https://drive.google.com/file/d/1xZqeOgj32-BkOhDTZVc4k_tp1ddnOEh7/view?usp=drive_link")
|
136 |
+
lt2005 = lt2005[(lt2005['race_name'] == 'Total') & (lt2005['age_name'] == '<1 year')]
|
137 |
+
lt2005 = lt2005[['location_name', 'val']]
|
138 |
+
lt2005.rename(columns={'val':'Life_Expectancy'}, inplace=True)
|
139 |
+
|
140 |
+
lt2010 = pd.read_csv("https://drive.google.com/file/d/1ItqHBuuUa38PVytfahaAV8NWwbhHMMg8/view?usp=drive_link")
|
141 |
+
lt2010 = lt2010[(lt2010['race_name'] == 'Total') & (lt2010['age_name'] == '<1 year')]
|
142 |
+
lt2010 = lt2010[['location_name', 'val']]
|
143 |
+
lt2010.rename(columns={'val':'Life_Expectancy'}, inplace=True)
|
144 |
+
|
145 |
+
lt2015 = pd.read_csv("https://drive.google.com/file/d/1rOgQY1RQiry2ionTKM_UWgT8cYD2E0vX/view?usp=drive_link")
|
146 |
+
lt2015 = lt2015[(lt2015['race_name'] == 'Total') & (lt2015['age_name'] == '<1 year')]
|
147 |
+
lt2015 = lt2015[['location_name', 'val']]
|
148 |
+
lt2015.rename(columns={'val':'Life_Expectancy'}, inplace=True)
|
149 |
+
|
150 |
+
lt_data = pd.concat([lt2000, lt2005, lt2010, lt2015])
|
151 |
+
lt_data.drop_duplicates(subset=['location_name'], inplace=True)
|
152 |
+
|
153 |
+
data2 = pd.merge(data, lt_data, how='inner', left_on='LocationDesc', right_on='location_name')
|
154 |
+
data2.drop(columns=['location_name'], axis=1, inplace=True)
|
155 |
+
data2 = data2[(data2['Break_Out_Details'] != '75+') & (data2['Break_Out_Details'] != '35+')]
|
156 |
+
data2.rename(columns={'Question':'Disease_Type'}, inplace=True)
|
157 |
+
data2['Life_Expectancy'] = np.where(data2['Break_Out_Type'] == 0, data2['Life_Expectancy'], np.nan)
|
158 |
+
processed_filepath = '/content/drive/MyDrive/my_processed_data.csv'
|
159 |
+
return processed_filepath
|