|
| 1 | +import pandas as pd |
| 2 | +from pandas.io import gbq |
| 3 | + |
| 4 | +def test_gcs_first_day_calculated_correctly(dataset, project_id): |
| 5 | + """Verifies GCS first day values are calculated correctly.""" |
| 6 | + # almost every individual should have a GCS first day |
| 7 | + query = f""" |
| 8 | + SELECT COUNT(*) AS n, COUNT(g.gcs) AS n_gcs |
| 9 | + FROM {dataset}.first_day_gcs g |
| 10 | + """ |
| 11 | + df = gbq.read_gbq(query, project_id=project_id, dialect="standard") |
| 12 | + n, n_gcs = df.iloc[0, 0], df.iloc[0, 1] |
| 13 | + frac = float(n_gcs) / n * 100.0 |
| 14 | + assert frac > 98, 'less than 98%% of stays have a first day GCS' |
| 15 | + |
| 16 | + |
| 17 | + # verify a subset of values |
| 18 | + known_values = { |
| 19 | + 37535507: {'gcs': 13, 'gcs_motor': 4, 'gcs_verbal': None, 'gcs_eyes': None}, |
| 20 | + 38852627: {'gcs': None, 'gcs_motor': None, 'gcs_verbal': None, 'gcs_eyes': None}, |
| 21 | + 32435143: {'gcs': 8, 'gcs_motor': 5, 'gcs_verbal': 1, 'gcs_eyes': 2}, |
| 22 | + } |
| 23 | + query = f""" |
| 24 | + SELECT g.stay_id |
| 25 | + , g.gcs |
| 26 | + , g.gcs_motor |
| 27 | + , g.gcs_verbal |
| 28 | + , g.gcs_eyes |
| 29 | + , g.gcs_unable |
| 30 | + FROM {dataset}.first_day_gcs g |
| 31 | + WHERE g.stay_id IN |
| 32 | + ( |
| 33 | + {','.join([str(x) for x in known_values.keys()])} |
| 34 | + ) |
| 35 | + """ |
| 36 | + df = gbq.read_gbq(query, project_id=project_id, dialect="standard") |
| 37 | + df = df.sort_values(['stay_id']).set_index('stay_id') |
| 38 | + for stay_id, row in df.iterrows(): |
| 39 | + for col, expected_val in known_values[stay_id].items(): |
| 40 | + assert row[col] == expected_val, f'first_day_gcs {col} value incorrect for stay_id={stay_id}' |
0 commit comments