File: cirs_test.py

package info (click to toggle)
python-ulmo 0.8.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,056 kB
  • sloc: python: 6,550; makefile: 144
file content (208 lines) | stat: -rw-r--r-- 5,691 bytes parent folder | download | duplicates (4)
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
from past.builtins import basestring
import copy

import numpy as np
import pandas

import ulmo
import test_util


test_sets = [
    {
        'element': 'pdsi',
        'by_state': False,
        'values': [
            {
                'state': 'WY',
                'state_code': 48,
                'division': 8,
                'year': 1998,
                'month': 1,
                'pdsi': 1.84,
            }, {
                'state': 'WY',
                'state_code': 48,
                'division': 8,
                'year': 1998,
                'month': 2,
                'pdsi': 1.88,
            }, {
                'state': 'WY',
                'state_code': 48,
                'division': 8,
                'year': 1998,
                'month': 3,
                'pdsi': 1.99,
            }, {
                'state': 'WY',
                'state_code': 48,
                'division': 8,
                'year': 1998,
                'month': 11,
                'pdsi': 2.71,
            }, {
                'state': 'WY',
                'state_code': 48,
                'division': 8,
                'year': 1998,
                'month': 12,
                'pdsi': 2.53,
            }, {
                'state': 'TX',
                'state_code': 41,
                'division': 10,
                'year': 1895,
                'month': 1,
                'pdsi': -0.05,
            }, {
                'state': 'TX',
                'state_code': 41,
                'division': 10,
                'year': 1895,
                'month': 12,
                'pdsi': 1.72,
            },
        ]
    }, {
        'element': 'tmpc',
        'by_state': True,
        'values': [
            {
                'location': 'ME',
                'location_code': 17,
                'year': 1895,
                'month': 1,
                'tmpc': 12.20,
            }, {
                'location': 'ME',
                'location_code': 17,
                'year': 1895,
                'month': 12,
                'tmpc': 20.7,
            }, {
                'location': 'national',
                'location_code': 110,
                'year': 2013,
                'month': 1,
                'tmpc': 32.25,
            }, {
                'location': 'national',
                'location_code': 110,
                'year': 2013,
                'month': 2,
                'tmpc': 34.79,
            },
        ]
    }, {
        'element': 'tmpc',
        'by_state': True,
        'location_names': 'full',
        'values': [
            {
                'location': 'Maine',
                'location_code': 17,
                'year': 1895,
                'month': 1,
                'tmpc': 12.20,
            },
        ]
    }, {
        'element': 'sp01',
        'by_state': True,
        'values': [
            {
                'location': 'AL',
                'location_code': 1,
                'year': 1895,
                'month': 1,
                'sp01': 1.23,
            },
        ]
    }, {
        'element': ['sp01', 'tmpc', 'pdsi'],
        'by_state': True,
        'values': [
            {
                'location': 'AL',
                'location_code': 1,
                'year': 1895,
                'month': 1,
                'sp01': 1.23,
                'tmpc': 43.1,
                'pdsi': 0.78,
            },
            {
                'location': None,
                'location_code': 113,
                'year': 1895,
                'month': 1,
                'sp01': None,
                'tmpc': 15.5,
                'pdsi': None,
            },
        ]
    },
]


def test_get_data_by_state():
    state_tests = [s for s in test_sets if s['by_state']]
    _run_test_sets(state_tests)


def test_get_data_by_climate_division():
    division_tests = [s for s in test_sets if not s['by_state']]
    _run_test_sets(division_tests)


def test_doesnt_have_locations_if_location_names_is_none():
    element = 'pdsi'
    use_file = _test_use_file(element, False)
    data = ulmo.ncdc.cirs.get_data(
        element, location_names=None, use_file=use_file, as_dataframe=True)
    assert 'location' not in data.columns


def _run_test_sets(test_sets):
    for test_set in test_sets:
        test_args = copy.copy(test_set)
        element = test_args.pop('element')
        test_values = test_args.pop('values')
        by_state = test_args['by_state']

        use_file = _test_use_file(element, by_state)
        data = ulmo.ncdc.cirs.get_data(
            element, use_file=use_file, as_dataframe=True, **test_args)
        if isinstance(element, basestring) or len(element) == 1:
            not_nulls = data[element].notnull()
        else:
            not_nulls = pandas.notnull(data[element]).any(axis=1)
        assert np.all(not_nulls)
        for test_value in test_values:
            _assert_inclusion(test_value, data)


def _assert_inclusion(value_dict, dataframe):
    "tests that a value_dict is in a dataframe"
    sub_df = dataframe.copy()
    for k, v in value_dict.items():
        if pandas.isnull(v):
            sub_df = sub_df[pandas.isnull(sub_df[k])]
        else:
            sub_df = sub_df[sub_df[k] == v]

    assert len(sub_df) == 1


def _test_use_file(elements, by_state):
    if test_util.use_test_files():
        if isinstance(elements, basestring):
            div = "st" if by_state else "dv"
            path = 'ncdc/cirs/climdiv-{elements}{div}-v1.0.0-20140304'.format(
                elements=elements, div=div)
        else:
            path = 'ncdc/cirs/'
        return test_util.get_test_file_path(path)
    else:
        return None