-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_ddi.py
402 lines (330 loc) · 13 KB
/
test_ddi.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from pathlib import Path
import pandas as pd
import numpy as np
import pytest
from ipumspy import ddi, readers
@pytest.fixture(scope="module")
def cps_ddi(fixtures_path: Path) -> ddi.Codebook:
return readers.read_ipums_ddi(fixtures_path / "cps_00006.xml")
@pytest.fixture(scope="module")
def cps_df(fixtures_path: Path, cps_ddi: ddi.Codebook) -> pd.DataFrame:
return readers.read_microdata(cps_ddi, fixtures_path / "cps_00006.csv.gz")
@pytest.fixture(scope="function")
def cps_ddi2(fixtures_path: Path) -> ddi.Codebook:
return readers.read_ipums_ddi(fixtures_path / "cps_00361.xml")
@pytest.fixture(scope="function")
def cps_df2(fixtures_path: Path, cps_ddi2: ddi.Codebook) -> pd.DataFrame:
return readers.read_microdata(cps_ddi2, fixtures_path / "cps_00361.dat.gz")
@pytest.fixture(scope="function")
def cps_ddi_hierarchical(fixtures_path: Path) -> ddi.Codebook:
return readers.read_ipums_ddi(fixtures_path / "cps_00421.xml")
# not implemented yet
# @pytest.fixture(scope="function")
# def cps_df_hierarchical(fixtures_path: Path, cps_ddi_hierarchical: ddi.Codebook) -> pd.DataFrame:
# return readers.read_microdata(cps_ddi_hierarchical, fixtures_path / "cps_00421.dat.gz")
def test_get_variable_info_rectangular(cps_ddi: ddi.Codebook):
# Does it retrieve the appropriate variable?
assert cps_ddi.get_variable_info("YEAR").id == "YEAR"
# Even if the name is not UPPERCASE?
assert cps_ddi.get_variable_info("year").id == "YEAR"
# does it give the right description
assert (
cps_ddi.get_variable_info("year").description
== "YEAR reports the year in which the survey was conducted. YEARP is repeated on person records."
)
# does it return the name
assert cps_ddi.get_variable_info("year").name == "YEAR"
# codes
assert cps_ddi.get_variable_info("month").codes == {
"January": 1,
"February": 2,
"March": 3,
"April": 4,
"May": 5,
"June": 6,
"July": 7,
"August": 8,
"September": 9,
"October": 10,
"November": 11,
"December": 12,
}
# does it have the correct rectype
assert cps_ddi.get_variable_info("year").rectype == ""
# And does it raise a ValueError if the variable does not exist?
with pytest.raises(ValueError):
cps_ddi.get_variable_info("foo")
def test_get_variable_info_hierarchical(cps_ddi_hierarchical: ddi.Codebook):
# Does it retrieve the appropriate variable?
assert cps_ddi_hierarchical.get_variable_info("YEAR").id == "YEAR"
# Even if the name is not UPPERCASE?
assert cps_ddi_hierarchical.get_variable_info("year").id == "YEAR"
# does it give the right description
assert (
cps_ddi_hierarchical.get_variable_info("year").description
== "YEAR reports the year in which the survey was conducted. YEARP is repeated on person records."
)
# does it return the name
assert cps_ddi_hierarchical.get_variable_info("year").name == "YEAR"
# codes
assert cps_ddi_hierarchical.get_variable_info("month").codes == {
"January": 1,
"February": 2,
"March": 3,
"April": 4,
"May": 5,
"June": 6,
"July": 7,
"August": 8,
"September": 9,
"October": 10,
"November": 11,
"December": 12,
}
# does it have the correct rectype
assert cps_ddi_hierarchical.get_variable_info("year").rectype == "H P"
# And does it raise a ValueError if the variable does not exist?
with pytest.raises(ValueError):
cps_ddi_hierarchical.get_variable_info("foo")
def test_ddi_codebook_rectangular(cps_ddi: ddi.Codebook):
# sample descriptions/names
# looks like the test ddi was generated several versions ago
# and sample <notes> are now formatted differently.
# assert cps_ddi.samples_description == ["IPUMS-CPS, ASEC 1962",
# "IPUMS-CPS, ASEC 1963"]
# doi
assert cps_ddi.ipums_doi == "DOI:10.18128/D030.V5.0"
# data format
assert cps_ddi.file_description.format == "fixed length fields"
# data structure
assert cps_ddi.file_description.structure == "rectangular"
# rectypes
assert cps_ddi.file_description.rectypes == []
# rectype idvar
assert cps_ddi.file_description.rectype_idvar == ""
# rectype keyvar
assert cps_ddi.file_description.rectype_keyvar == ""
# data collection
assert cps_ddi.ipums_collection == "cps"
# citation
assert cps_ddi.ipums_citation == (
"Publications and research reports based on the "
"IPUMS-CPS database must cite it appropriately. "
"The citation should include the following:\n"
"\n"
"Sarah Flood, Miriam King, Steven Ruggles, and "
"J. Robert Warren. Integrated Public Use "
"Microdata Series, Current Population Survey: "
"Version 5.0 [dataset]. Minneapolis, MN: "
"University of Minnesota, 2017. "
"https://doi.org/10.18128/D030.V5.0\n"
"\n"
"The licensing agreement for use of IPUMS-CPS "
"data requires that users supply us with the "
"title and full citation for any publications, "
"research reports, or educational materials "
"making use of the data or documentation. Please "
"add your citation to the IPUMS bibliography: "
"http://bibliography.ipums.org/"
)
# terms of use
assert cps_ddi.ipums_conditions == (
"Users of IPUMS-CPS data must agree to abide by "
"the conditions of use. A user's license is "
"valid for one year and may be renewed. Users "
"must agree to the following conditions:\n"
"\n"
"(1) No fees may be charged for use or "
"distribution of the data. All persons are "
"granted a limited license to use these data, "
"but you may not charge a fee for the data if "
"you distribute it to others.\n"
"\n"
"(2) Cite IPUMS appropriately. For information "
"on proper citation, refer to the citation "
"requirement section of this DDI document.\n"
"\n"
"(3) Tell us about any work you do using the "
"IPUMS. Publications, research reports, or "
"presentations making use of IPUMS-CPS should "
"be added to our Bibliography. Continued "
"funding for the IPUMS depends on our ability "
"to show our sponsor agencies that researchers "
"are using the data for productive purposes.\n"
"\n"
"(4) Use it for GOOD -- never for EVIL."
)
def test_ddi_codebook_hierarchical(cps_ddi_hierarchical: ddi.Codebook):
# sample descriptions/names
assert cps_ddi_hierarchical.samples_description == [
"IPUMS-CPS, January 2022",
"IPUMS-CPS, January 2023",
]
# doi
assert cps_ddi_hierarchical.ipums_doi == "DOI:10.18128/D030.V10.0"
# data format
assert cps_ddi_hierarchical.file_description.format == "fixed length fields"
# data structure
assert cps_ddi_hierarchical.file_description.structure == "hierarchical"
# rectypes
assert cps_ddi_hierarchical.file_description.rectypes == ["P", "H"]
# rectype idvar
assert cps_ddi_hierarchical.file_description.rectype_idvar == "RECTYPE"
# rectype keyvar
assert cps_ddi_hierarchical.file_description.rectype_keyvar == "SERIAL"
# data collection
assert cps_ddi_hierarchical.ipums_collection == "cps"
# citation
assert cps_ddi_hierarchical.ipums_citation == (
"Publications and research reports based on the "
"IPUMS-CPS database must cite it appropriately. "
"The citation should include the following:\n"
"\n"
"Sarah Flood, Miriam King, Renae Rodgers, Steven Ruggles, "
"J. Robert Warren and Michael Westberry. Integrated Public Use "
"Microdata Series, Current Population Survey: "
"Version 10.0 [dataset]. Minneapolis, MN: "
"IPUMS, 2022. "
"https://doi.org/10.18128/D030.V10.0\n"
"\n"
"The licensing agreement for use of IPUMS-CPS "
"data requires that users supply us with the "
"title and full citation for any publications, "
"research reports, or educational materials "
"making use of the data or documentation. Please "
"add your citation to the IPUMS bibliography: "
"http://bibliography.ipums.org/"
)
# terms of use
assert cps_ddi_hierarchical.ipums_conditions == (
"Users of IPUMS-CPS data must agree to abide by "
"the conditions of use. A user's license is "
"valid for one year and may be renewed. Users "
"must agree to the following conditions:\n"
"\n"
"(1) No fees may be charged for use or "
"distribution of the data. All persons are "
"granted a limited license to use these data, "
"but you may not charge a fee for the data if "
"you distribute it to others.\n"
"\n"
"(2) Cite IPUMS appropriately. For information "
"on proper citation, refer to the citation "
"requirement section of this DDI document.\n"
"\n"
"(3) Tell us about any work you do using the "
"IPUMS. Publications, research reports, or "
"presentations making use of IPUMS-CPS should "
"be added to our Bibliography. Continued "
"funding for the IPUMS depends on our ability "
"to show our sponsor agencies that researchers "
"are using the data for productive purposes.\n"
"\n"
"(4) Use it for GOOD -- never for EVIL."
)
def test_get_all_types(cps_ddi: ddi.Codebook, cps_df: pd.DataFrame):
var_types = {
"YEAR": "integer",
"SERIAL": "integer",
"HWTSUPP": "integer",
"STATEFIP": "integer",
"MONTH": "integer",
"PERNUM": "integer",
"WTSUPP": "integer",
"INCTOT": "integer",
}
assert cps_ddi.get_all_types(type_format="vartype") == var_types
python_types = {
"YEAR": int,
"SERIAL": int,
"HWTSUPP": float,
"STATEFIP": int,
"MONTH": int,
"PERNUM": int,
"WTSUPP": float,
"INCTOT": int,
}
assert cps_ddi.get_all_types(type_format="python_type") == python_types
# always np.float64 due to eventual NaNs.
numpy_types = {
"YEAR": np.float64,
"SERIAL": np.float64,
"HWTSUPP": np.float64,
"STATEFIP": np.float64,
"MONTH": np.float64,
"PERNUM": np.float64,
"WTSUPP": np.float64,
"INCTOT": np.float64,
}
assert cps_ddi.get_all_types(type_format="numpy_type") == numpy_types
pandas_types = {
"YEAR": pd.Int64Dtype(),
"SERIAL": pd.Int64Dtype(),
"HWTSUPP": np.float64,
"STATEFIP": pd.Int64Dtype(),
"MONTH": pd.Int64Dtype(),
"PERNUM": pd.Int64Dtype(),
"WTSUPP": np.float64,
"INCTOT": pd.Int64Dtype(),
}
assert cps_ddi.get_all_types(type_format="pandas_type") == pandas_types
pandas_types_efficient = {
"YEAR": np.float64,
"SERIAL": np.float64,
"HWTSUPP": np.float64,
"STATEFIP": np.float64,
"MONTH": np.float64,
"PERNUM": np.float64,
"WTSUPP": np.float64,
"INCTOT": np.float64,
}
assert (
cps_ddi.get_all_types(type_format="pandas_type_efficient")
== pandas_types_efficient
)
# Does it raise a ValueError if the specified type of format, doesn't match existing attribute?
with pytest.raises(ValueError):
cps_ddi.get_all_types(type_format="foo")
def test_get_all_types_with_pyarrow(cps_ddi2: ddi.Codebook, cps_df2: pd.DataFrame):
pandas_types = {
"YEAR": pd.Int64Dtype(),
"SERIAL": pd.Int64Dtype(),
"MONTH": pd.Int64Dtype(),
"HWTFINL": np.float64,
"CPSID": pd.Int64Dtype(),
"ASECFLAG": pd.Int64Dtype(),
"STATEFIP": pd.Int64Dtype(),
"HRSERSUF": pd.StringDtype(storage="pyarrow"),
"PERNUM": pd.Int64Dtype(),
"WTFINL": np.float64,
"CPSIDP": pd.Int64Dtype(),
"AGE": pd.Int64Dtype(),
}
assert (
cps_ddi2.get_all_types(type_format="pandas_type", string_pyarrow=True)
== pandas_types
)
pandas_types_efficient = {
"YEAR": np.float64,
"SERIAL": np.float64,
"MONTH": np.float64,
"HWTFINL": np.float64,
"CPSID": np.float64,
"ASECFLAG": np.float64,
"STATEFIP": np.float64,
"HRSERSUF": pd.StringDtype(storage="pyarrow"),
"PERNUM": np.float64,
"WTFINL": np.float64,
"CPSIDP": np.float64,
"AGE": np.float64,
}
assert (
cps_ddi2.get_all_types(type_format="pandas_type_efficient", string_pyarrow=True)
== pandas_types_efficient
)
with pytest.raises(ValueError):
cps_ddi2.get_all_types(type_format="numpy_type", string_pyarrow=True)
with pytest.raises(ValueError):
cps_ddi2.get_all_types(type_format="vartype", string_pyarrow=True)
with pytest.raises(ValueError):
cps_ddi2.get_all_types(type_format="python_type", string_pyarrow=True)