| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
from collections import OrderedDict |
| 4 |
|
| 5 |
from petl.test.failonerror import assert_failonerror |
| 6 |
from petl.test.helpers import ieq |
| 7 |
from petl.transform.maps import fieldmap, rowmap, rowmapmany |
| 8 |
|
| 9 |
from functools import partial |
| 10 |
|
| 11 |
|
| 12 |
def test_fieldmap(): |
| 13 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 14 |
(1, 'male', 16, 1.45, 62.0), |
| 15 |
(2, 'female', 19, 1.34, 55.4), |
| 16 |
(3, 'female', 17, 1.78, 74.4), |
| 17 |
(4, 'male', 21, 1.33, 45.2), |
| 18 |
(5, '-', 25, 1.65, 51.9)) |
| 19 |
|
| 20 |
mappings = OrderedDict() |
| 21 |
mappings['subject_id'] = 'id' |
| 22 |
mappings['gender'] = 'sex', {'male': 'M', 'female': 'F'} |
| 23 |
mappings['age_months'] = 'age', lambda v: v * 12 |
| 24 |
mappings['bmi'] = lambda rec: rec['weight'] / rec['height'] ** 2 |
| 25 |
actual = fieldmap(table, mappings) |
| 26 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 27 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 28 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 29 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 30 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2), |
| 31 |
(5, '-', 25 * 12, 51.9 / 1.65 ** 2)) |
| 32 |
ieq(expect, actual) |
| 33 |
ieq(expect, actual) # can iteratate twice? |
| 34 |
|
| 35 |
# do it with suffix |
| 36 |
actual = fieldmap(table) |
| 37 |
actual['subject_id'] = 'id' |
| 38 |
actual['gender'] = 'sex', {'male': 'M', 'female': 'F'} |
| 39 |
actual['age_months'] = 'age', lambda v: v * 12 |
| 40 |
actual['bmi'] = '{weight} / {height}**2' |
| 41 |
ieq(expect, actual) |
| 42 |
|
| 43 |
# test short rows |
| 44 |
table2 = (('id', 'sex', 'age', 'height', 'weight'), |
| 45 |
(1, 'male', 16, 1.45, 62.0), |
| 46 |
(2, 'female', 19, 1.34, 55.4), |
| 47 |
(3, 'female', 17, 1.78, 74.4), |
| 48 |
(4, 'male', 21, 1.33, 45.2), |
| 49 |
(5, '-', 25, 1.65)) |
| 50 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 51 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 52 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 53 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 54 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2), |
| 55 |
(5, '-', 25 * 12, None)) |
| 56 |
actual = fieldmap(table2, mappings) |
| 57 |
ieq(expect, actual) |
| 58 |
|
| 59 |
|
| 60 |
def test_fieldmap_record_access(): |
| 61 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 62 |
(1, 'male', 16, 1.45, 62.0), |
| 63 |
(2, 'female', 19, 1.34, 55.4), |
| 64 |
(3, 'female', 17, 1.78, 74.4), |
| 65 |
(4, 'male', 21, 1.33, 45.2), |
| 66 |
(5, '-', 25, 1.65, 51.9)) |
| 67 |
|
| 68 |
mappings = OrderedDict() |
| 69 |
mappings['subject_id'] = 'id' |
| 70 |
mappings['gender'] = 'sex', {'male': 'M', 'female': 'F'} |
| 71 |
mappings['age_months'] = 'age', lambda v: v * 12 |
| 72 |
mappings['bmi'] = lambda rec: rec.weight / rec.height ** 2 |
| 73 |
actual = fieldmap(table, mappings) |
| 74 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 75 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 76 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 77 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 78 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2), |
| 79 |
(5, '-', 25 * 12, 51.9 / 1.65 ** 2)) |
| 80 |
ieq(expect, actual) |
| 81 |
ieq(expect, actual) # can iteratate twice? |
| 82 |
|
| 83 |
|
| 84 |
def test_fieldmap_empty(): |
| 85 |
table = (('foo', 'bar'),) |
| 86 |
expect = (('foo', 'baz'),) |
| 87 |
mappings = OrderedDict() |
| 88 |
mappings['foo'] = 'foo' |
| 89 |
mappings['baz'] = 'bar', lambda v: v * 2 |
| 90 |
actual = fieldmap(table, mappings) |
| 91 |
ieq(expect, actual) |
| 92 |
|
| 93 |
|
| 94 |
def test_fieldmap_headerless(): |
| 95 |
table = [] |
| 96 |
expect = [] |
| 97 |
mappings = OrderedDict() |
| 98 |
mappings['foo'] = 'foo' |
| 99 |
mappings['baz'] = 'bar', lambda v: v * 2 |
| 100 |
actual = fieldmap(table, mappings) |
| 101 |
ieq(expect, actual) |
| 102 |
|
| 103 |
|
| 104 |
def test_fieldmap_failonerror(): |
| 105 |
input_ = (('foo',), ('A',), (1,)) |
| 106 |
mapper_ = {'bar': ('foo', lambda v: v.lower())} |
| 107 |
expect_ = (('bar',), ('a',), (None,)) |
| 108 |
|
| 109 |
assert_failonerror( |
| 110 |
input_fn=partial(fieldmap, input_, mapper_), |
| 111 |
expected_output=expect_) |
| 112 |
|
| 113 |
|
| 114 |
def test_rowmap(): |
| 115 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 116 |
(1, 'male', 16, 1.45, 62.0), |
| 117 |
(2, 'female', 19, 1.34, 55.4), |
| 118 |
(3, 'female', 17, 1.78, 74.4), |
| 119 |
(4, 'male', 21, 1.33, 45.2), |
| 120 |
(5, '-', 25, 1.65, 51.9)) |
| 121 |
|
| 122 |
def rowmapper(row): |
| 123 |
transmf = {'male': 'M', 'female': 'F'} |
| 124 |
return [row[0], |
| 125 |
transmf[row[1]] if row[1] in transmf else row[1], |
| 126 |
row[2] * 12, |
| 127 |
row[4] / row[3] ** 2] |
| 128 |
|
| 129 |
actual = rowmap(table, rowmapper, header=['subject_id', 'gender', |
| 130 |
'age_months', 'bmi']) |
| 131 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 132 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 133 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 134 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 135 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2), |
| 136 |
(5, '-', 25 * 12, 51.9 / 1.65 ** 2)) |
| 137 |
ieq(expect, actual) |
| 138 |
ieq(expect, actual) # can iteratate twice? |
| 139 |
|
| 140 |
# test short rows |
| 141 |
table2 = (('id', 'sex', 'age', 'height', 'weight'), |
| 142 |
(1, 'male', 16, 1.45, 62.0), |
| 143 |
(2, 'female', 19, 1.34, 55.4), |
| 144 |
(3, 'female', 17, 1.78, 74.4), |
| 145 |
(4, 'male', 21, 1.33, 45.2), |
| 146 |
(5, '-', 25, 1.65)) |
| 147 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 148 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 149 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 150 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 151 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2)) |
| 152 |
actual = rowmap(table2, rowmapper, header=['subject_id', 'gender', |
| 153 |
'age_months', 'bmi']) |
| 154 |
ieq(expect, actual) |
| 155 |
|
| 156 |
|
| 157 |
def test_rowmap_empty(): |
| 158 |
table = (('id', 'sex', 'age', 'height', 'weight'),) |
| 159 |
|
| 160 |
def rowmapper(row): |
| 161 |
transmf = {'male': 'M', 'female': 'F'} |
| 162 |
return [row[0], |
| 163 |
transmf[row[1]] if row[1] in transmf else row[1], |
| 164 |
row[2] * 12, |
| 165 |
row[4] / row[3] ** 2] |
| 166 |
|
| 167 |
actual = rowmap(table, rowmapper, header=['subject_id', 'gender', |
| 168 |
'age_months', 'bmi']) |
| 169 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'),) |
| 170 |
ieq(expect, actual) |
| 171 |
|
| 172 |
|
| 173 |
def test_rowmap_headerless(): |
| 174 |
table = [] |
| 175 |
|
| 176 |
def rowmapper(row): |
| 177 |
return row |
| 178 |
|
| 179 |
actual = rowmap(table, rowmapper, header=['subject_id', 'gender']) |
| 180 |
expect = [] |
| 181 |
ieq(expect, actual) |
| 182 |
|
| 183 |
|
| 184 |
def test_rowmap_failonerror(): |
| 185 |
input_ = (('foo',), ('A',), (1,), ('B',)) |
| 186 |
mapper = lambda r: [r[0].lower()] |
| 187 |
# exceptions in rowmappers do not generate an output row |
| 188 |
expect_ = (('foo',), ('a',), ('b',)) |
| 189 |
|
| 190 |
assert_failonerror( |
| 191 |
input_fn=partial(rowmap, input_, mapper, header=('foo',)), |
| 192 |
expected_output=expect_) |
| 193 |
|
| 194 |
|
| 195 |
def test_recordmap(): |
| 196 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 197 |
(1, 'male', 16, 1.45, 62.0), |
| 198 |
(2, 'female', 19, 1.34, 55.4), |
| 199 |
(3, 'female', 17, 1.78, 74.4), |
| 200 |
(4, 'male', 21, 1.33, 45.2), |
| 201 |
(5, '-', 25, 1.65, 51.9)) |
| 202 |
|
| 203 |
def recmapper(rec): |
| 204 |
transmf = {'male': 'M', 'female': 'F'} |
| 205 |
return [rec['id'], |
| 206 |
transmf[rec['sex']] if rec['sex'] in transmf else rec['sex'], |
| 207 |
rec['age'] * 12, |
| 208 |
rec['weight'] / rec['height'] ** 2] |
| 209 |
|
| 210 |
actual = rowmap(table, recmapper, header=['subject_id', 'gender', |
| 211 |
'age_months', 'bmi']) |
| 212 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 213 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 214 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 215 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 216 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2), |
| 217 |
(5, '-', 25 * 12, 51.9 / 1.65 ** 2)) |
| 218 |
ieq(expect, actual) |
| 219 |
ieq(expect, actual) # can iteratate twice? |
| 220 |
|
| 221 |
# test short rows |
| 222 |
table2 = (('id', 'sex', 'age', 'height', 'weight'), |
| 223 |
(1, 'male', 16, 1.45, 62.0), |
| 224 |
(2, 'female', 19, 1.34, 55.4), |
| 225 |
(3, 'female', 17, 1.78, 74.4), |
| 226 |
(4, 'male', 21, 1.33, 45.2), |
| 227 |
(5, '-', 25, 1.65)) |
| 228 |
expect = (('subject_id', 'gender', 'age_months', 'bmi'), |
| 229 |
(1, 'M', 16 * 12, 62.0 / 1.45 ** 2), |
| 230 |
(2, 'F', 19 * 12, 55.4 / 1.34 ** 2), |
| 231 |
(3, 'F', 17 * 12, 74.4 / 1.78 ** 2), |
| 232 |
(4, 'M', 21 * 12, 45.2 / 1.33 ** 2)) |
| 233 |
actual = rowmap(table2, recmapper, header=['subject_id', 'gender', |
| 234 |
'age_months', 'bmi']) |
| 235 |
ieq(expect, actual) |
| 236 |
|
| 237 |
|
| 238 |
def test_rowmapmany(): |
| 239 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 240 |
(1, 'male', 16, 1.45, 62.0), |
| 241 |
(2, 'female', 19, 1.34, 55.4), |
| 242 |
(3, '-', 17, 1.78, 74.4), |
| 243 |
(4, 'male', 21, 1.33)) |
| 244 |
|
| 245 |
def rowgenerator(row): |
| 246 |
transmf = {'male': 'M', 'female': 'F'} |
| 247 |
yield [row[0], 'gender', |
| 248 |
transmf[row[1]] if row[1] in transmf else row[1]] |
| 249 |
yield [row[0], 'age_months', row[2] * 12] |
| 250 |
yield [row[0], 'bmi', row[4] / row[3] ** 2] |
| 251 |
|
| 252 |
actual = rowmapmany(table, rowgenerator, header=['subject_id', 'variable', |
| 253 |
'value']) |
| 254 |
expect = (('subject_id', 'variable', 'value'), |
| 255 |
(1, 'gender', 'M'), |
| 256 |
(1, 'age_months', 16 * 12), |
| 257 |
(1, 'bmi', 62.0 / 1.45 ** 2), |
| 258 |
(2, 'gender', 'F'), |
| 259 |
(2, 'age_months', 19 * 12), |
| 260 |
(2, 'bmi', 55.4 / 1.34 ** 2), |
| 261 |
(3, 'gender', '-'), |
| 262 |
(3, 'age_months', 17 * 12), |
| 263 |
(3, 'bmi', 74.4 / 1.78 ** 2), |
| 264 |
(4, 'gender', 'M'), |
| 265 |
(4, 'age_months', 21 * 12)) |
| 266 |
ieq(expect, actual) |
| 267 |
ieq(expect, actual) # can iteratate twice? |
| 268 |
|
| 269 |
|
| 270 |
def test_rowmapmany_failonerror(): |
| 271 |
input_ = (('foo',), ('A',), (1,), ('B',)) |
| 272 |
mapper = lambda r: [r[0].lower()] |
| 273 |
expect_ = (('foo',), ('a',), ('b',),) |
| 274 |
|
| 275 |
assert_failonerror( |
| 276 |
input_fn=partial(rowmapmany, input_, mapper, header=('foo',)), |
| 277 |
expected_output=expect_) |
| 278 |
|
| 279 |
|
| 280 |
def test_recordmapmany(): |
| 281 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 282 |
(1, 'male', 16, 1.45, 62.0), |
| 283 |
(2, 'female', 19, 1.34, 55.4), |
| 284 |
(3, '-', 17, 1.78, 74.4), |
| 285 |
(4, 'male', 21, 1.33)) |
| 286 |
|
| 287 |
def rowgenerator(rec): |
| 288 |
transmf = {'male': 'M', 'female': 'F'} |
| 289 |
yield [rec['id'], 'gender', |
| 290 |
transmf[rec['sex']] if rec['sex'] in transmf else rec['sex']] |
| 291 |
yield [rec['id'], 'age_months', rec['age'] * 12] |
| 292 |
yield [rec['id'], 'bmi', rec['weight'] / rec['height'] ** 2] |
| 293 |
|
| 294 |
actual = rowmapmany(table, rowgenerator, header=['subject_id', 'variable', |
| 295 |
'value']) |
| 296 |
expect = (('subject_id', 'variable', 'value'), |
| 297 |
(1, 'gender', 'M'), |
| 298 |
(1, 'age_months', 16 * 12), |
| 299 |
(1, 'bmi', 62.0 / 1.45 ** 2), |
| 300 |
(2, 'gender', 'F'), |
| 301 |
(2, 'age_months', 19 * 12), |
| 302 |
(2, 'bmi', 55.4 / 1.34 ** 2), |
| 303 |
(3, 'gender', '-'), |
| 304 |
(3, 'age_months', 17 * 12), |
| 305 |
(3, 'bmi', 74.4 / 1.78 ** 2), |
| 306 |
(4, 'gender', 'M'), |
| 307 |
(4, 'age_months', 21 * 12)) |
| 308 |
ieq(expect, actual) |
| 309 |
ieq(expect, actual) # can iteratate twice? |
| 310 |
|
| 311 |
|
| 312 |
def test_recordmapmany_headerless(): |
| 313 |
table = [] |
| 314 |
|
| 315 |
def duplicate(rec): |
| 316 |
yield rec |
| 317 |
yield rec |
| 318 |
|
| 319 |
actual = rowmapmany(table, duplicate, header=['subject_id', 'variable']) |
| 320 |
expect = [] |
| 321 |
ieq(expect, actual) |
| 322 |
ieq(expect, actual) # can iteratate twice? |
| 323 |
|
| 324 |
|
| 325 |
def test_fieldmap_untrusted(): |
| 326 |
table = (('id', 'sex', 'age', 'height', 'weight'), |
| 327 |
(1, 'male', 16, 1.45, 62.0), |
| 328 |
(2, 'female', 19, 1.34, 55.4), |
| 329 |
(3, 'female', 17, 1.78, 74.4), |
| 330 |
(4, 'male', 21, 1.33, 45.2), |
| 331 |
(5, '-', 25, 1.65, 51.9)) |
| 332 |
|
| 333 |
expect = (('subject_id', 'bmi'), |
| 334 |
(1, 62.0 / 1.45 ** 2), |
| 335 |
(2, 55.4 / 1.34 ** 2), |
| 336 |
(3, 74.4 / 1.78 ** 2), |
| 337 |
(4, 45.2 / 1.33 ** 2), |
| 338 |
(5, 51.9 / 1.65 ** 2)) |
| 339 |
|
| 340 |
# do it with suffix |
| 341 |
actual = fieldmap(table, trusted=True) |
| 342 |
actual['subject_id'] = 'id' |
| 343 |
actual['bmi'] = '{weight} / {height}**2' |
| 344 |
ieq(expect, actual) |