This repository was archived by the owner on Feb 12, 2025. It is now read-only.
forked from ShrRa/InterPython_Workshop_Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_models.py
156 lines (143 loc) · 5.14 KB
/
test_models.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
"""Tests for statistics functions within the Model layer."""
import pandas as pd
import pandas.testing as pdt
import pytest
@pytest.mark.parametrize(
"test_df, test_colname, expected",
[
(pd.DataFrame(data=[[1, 5, 3],
[7, 8, 9],
[3, 4, 1]],
columns=list("abc")),
"a",
7),
(pd.DataFrame(data=[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
columns=list("abc")),
"b",
0),
])
def test_max_mag(test_df, test_colname, expected):
"""Test max function works for array of zeroes and positive integers."""
from lcanalyzer.models import max_mag
assert max_mag(test_df,test_colname) == expected
# Parametrization for mean_mag function testing
@pytest.mark.parametrize(
"test_df, test_colname, expected",
[
(pd.DataFrame(data=[[1, 5, 3],
[7, 8, 9],
[3, 4, 1]],
columns=list("abc")),
"a",
pytest.approx(3.66,0.01)),
(pd.DataFrame(data=[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
columns=list("abc")),
"b",
0),
(pd.DataFrame(data=[[-7, -7, -3],
[-4, -3, -1],
[-1, -5, -3]],
columns=list("abc")),
"a",
-4),
])
def test_mean_mag(test_df, test_colname, expected):
"""Test mean function works for array of zeroes and positive integers."""
from lcanalyzer.models import mean_mag
assert mean_mag(test_df, test_colname) == expected
# Parametrization for min_mag function testing
@pytest.mark.parametrize(
"test_df, test_colname, expected",
[
(pd.DataFrame(data=[[1, 5, 3],
[7, 8, 9],
[3, 4, 1]],
columns=list("abc")),
"a",
1),
(pd.DataFrame(data=[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
columns=list("abc")),
"b",
0),
(pd.DataFrame(data=[[-7, -7, -3],
[-4, -3, -1],
[-1, -5, -3]],
columns=list("abc")),
"b",
-7),
])
def test_min_mag(test_df, test_colname, expected):
"""Test mean function works for array of zeroes and positive integers."""
from lcanalyzer.models import min_mag
assert min_mag(test_df, test_colname) == expected
def test_max_mag_strings():
""" Test for TypeError when passing a string. """
from lcanalyzer.models import max_mag
test_input_colname = "b"
with pytest.raises(TypeError):
error_expected = max_mag('string', test_input_colname)
def test_calc_stats():
""" Test calc_stats function works for multiple bands. """
from lcanalyzer.models import calc_stats
test_cols = list("abc")
test_dict = {}
test_dict["df0"] = pd.DataFrame(
data=[[8, 8, 0],
[0, 1, 1],
[2, 3, 1],
[7, 9, 7]], columns=test_cols
)
test_dict["df1"] = pd.DataFrame(
data=[[3, 8, 2],
[3, 8, 0],
[3, 9, 8],
[8, 2, 5]], columns=test_cols
)
test_dict["df2"] = pd.DataFrame(
data=[[8, 4, 3],
[7, 6, 3],
[4, 2, 9],
[6, 4, 0]], columns=test_cols
)
test_output = pd.DataFrame(data=[[9,9,6],[5.25,6.75,4.],[1,2,2]],columns=['df0','df1','df2'],index=['max','mean','min'])
pdt.assert_frame_equal(calc_stats(test_dict, test_dict.keys(), 'b'),
test_output,
check_exact=False,
atol=0.01)
# Parametrization for normalize_lc function testing
@pytest.mark.parametrize(
"test_input_df, test_input_colname, expected",
[
(pd.DataFrame(data=[[8, 9, 1],
[1, 4, 1],
[1, 2, 4],
[1, 4, 1]],
columns=list("abc")),
"b",
pd.Series(data=[1,0.285,0,0.285])),
(pd.DataFrame(data=[[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]],
columns=list("abc")),
"b",
pd.Series(data=[0.,0.,0.,0.])),
(pd.DataFrame(data=[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
columns=list("abc")),
"b",
pd.Series(data=[0.,0.,0.,0.])),
])
def test_normalize_lc(test_input_df, test_input_colname, expected):
"""Test how normalize_lc function works for arrays of positive integers."""
from lcanalyzer.models import normalize_lc
import pandas.testing as pdt
pdt.assert_series_equal(normalize_lc(test_input_df,test_input_colname),expected,check_exact=False,atol=0.01,check_names=False)