/
test_df_metrics.py
40 lines (31 loc) · 1.36 KB
/
test_df_metrics.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
# -*- coding: utf-8 -*-
"""
Test functions from the df_metrics module that applies metrics to combinations
of columns from a data frame.
"""
import pytesmo.df_metrics as df_metrics
from pytesmo.metrics import bias
import pandas as pd
import numpy as np
def test_n_combinations():
coll = [1,2,3,4]
combs = df_metrics.n_combinations(coll, n=2, must_include=[1], permutations=False)
assert combs == [(1,2), (1,3), (1,4)]
coll = [1, 2, 3, 4]
combs = df_metrics.n_combinations(coll, n=3, permutations=False)
assert combs == [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
def test_apply():
df = pd.DataFrame(index=pd.date_range(start='2000-01-01', end='2000-12-31', freq='D'),
data={'ds0': np.repeat(0, 366), 'ds1': np.repeat(1, 366)})
bias_matrix_old = df_metrics.pairwise_apply(df, bias)
bias_matrix_new = df_metrics.nwise_apply(df, bias, n=2, as_df=True)
assert bias_matrix_old.equals(bias_matrix_new)
# check if dict implementation and matrix implementation have same result
bias_new = df_metrics.nwise_apply(df, bias, n=2, as_df=False)
for i, v in bias_new.items():
assert bias_matrix_new.loc[i] == v
def test_dict_to_namedtuple():
d = {'a': 1, 'b': 2}
d_named = df_metrics._dict_to_namedtuple(d, 'name')
assert d_named._fields == ('a', 'b')
assert type(d_named).__name__ == 'name'