-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_compute_wh.py
More file actions
61 lines (51 loc) · 1.87 KB
/
Copy pathtest_compute_wh.py
File metadata and controls
61 lines (51 loc) · 1.87 KB
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
from typing import Any
import pandas as pd
import pytest
from ml.compute_wh import emission_factor, prepare_frames
@pytest.mark.parametrize(
("frames", "arch", "expected"),
[
(49, "hybrid", 1),
(50, "hybrid", 2),
(100, "hybrid", 3),
(100, "unet", 100),
(100, "dit", 100),
(0, "hybrid", 0),
],
)
def test_prepare_frames(frames: int, arch: str, expected: int) -> None:
assert prepare_frames(frames, arch) == expected
def test_emission_factor_uses_patched_table(monkeypatch: pytest.MonkeyPatch) -> None:
def fake_read_csv(_path: Any, **_: Any) -> pd.DataFrame:
return pd.DataFrame(
{
"country": ["Testland"],
"Emission factor": [100.0],
}
)
monkeypatch.setattr("ml.compute_wh.pd.read_csv", fake_read_csv)
wh, run_t = 1000.0, 100.0
carbon_embodied, carbon_electricity, water_used = emission_factor(
"Testland", wh, run_t
)
assert carbon_embodied == pytest.approx(0.20139540255138402, rel=1e-9, abs=1e-12)
assert carbon_electricity == pytest.approx(156.0)
assert water_used == pytest.approx(0.546, rel=1e-9)
def test_emission_factor_unknown_country_fallback_factor(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def fake_read_csv(_path: Any, **_: Any) -> pd.DataFrame:
return pd.DataFrame(
{
"country": ["Somewhere"],
"Emission factor": [50.0],
}
)
monkeypatch.setattr("ml.compute_wh.pd.read_csv", fake_read_csv)
wh, run_t = 1000.0, 100.0
carbon_embodied, carbon_electricity, water_used = emission_factor(
"NotInTable", wh, run_t
)
assert carbon_embodied == pytest.approx(0.20139540255138402, rel=1e-9, abs=1e-12)
assert carbon_electricity == pytest.approx(343.2)
assert water_used == pytest.approx(0.546, rel=1e-9)