Skip to content

Commit 93e8825

Browse files
committed
Add test for LCOEPDFPlot
1 parent 9b789bd commit 93e8825

5 files changed

Lines changed: 223 additions & 5 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*.xcf filter=lfs diff=lfs merge=lfs -text
55
*.svg filter=lfs diff=lfs merge=lfs -text
66
*.xlsx filter=lfs diff=lfs merge=lfs -text
7+
*.nc filter=lfs diff=lfs merge=lfs -text

packages/dtocean-economics/src/dtocean_plugins/plots/plots_lcoe.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_name(cls):
2929
str: A unique string
3030
"""
3131

32-
return "LCOE PDF Plot"
32+
return "LCOE PDF Analysis"
3333

3434
@classmethod
3535
def declare_inputs(cls):
@@ -100,8 +100,10 @@ def connect(self):
100100
)
101101

102102
xx = self.data.lcoe_pdf.coords["Discounted OPEX"].values
103-
yy = self.data.lcoe_pdf.coords["Discounted Energy"].values
104-
zz = self.data.lcoe_pdf.data.values
103+
yy = (
104+
self.data.lcoe_pdf.coords["Discounted Energy"].values / 1e6
105+
) # Wh to MWh
106+
zz = self.data.lcoe_pdf.data
105107

106108
plt.figure()
107109
cf = plt.contourf(
@@ -119,7 +121,7 @@ def connect(self):
119121
plt.contour(xx, yy, zz.T, clevels, colors="k")
120122

121123
opex = self.data.economics_metrics["Discounted OPEX"]
122-
energy = self.data.economics_metrics["Discounted Energy"] / 1000
124+
energy = self.data.economics_metrics["Discounted Energy"]
123125

124126
sp = plt.scatter(
125127
opex,
@@ -137,4 +139,9 @@ def connect(self):
137139
)
138140

139141
plt.xlabel("Discounted OPEX [Euro]")
140-
plt.ylabel("Discounted Energy [kWh]")
142+
plt.ylabel("Discounted Energy [MWh]")
143+
144+
plt.title("LCOE PDF Analysis")
145+
plt.tight_layout()
146+
147+
self.fig_handle = plt.gcf()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:d3f801ea608b2d492169878fb79d35fc935e7976c5e1f6d02a9083a8bca2e32f
3+
size 8203
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:b3a3ff7defddd95f58c79a19f41ab7ba23953eb6559e1ce535ea946dbd2546df
3+
size 8023199
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from dtocean_core.core import Core
5+
from dtocean_core.menu import ModuleMenu, ProjectMenu
6+
from dtocean_core.pipeline import Tree
7+
from matplotlib import pyplot as plt
8+
9+
from dtocean_plugins.modules.base import ModuleInterface
10+
11+
DIR_PATH = Path(__file__).parent
12+
ROOT_DIR_PATH = DIR_PATH.parents[2]
13+
TEST_DATA_DIR_PATH = ROOT_DIR_PATH / "test_data" / "lcoe_pdf_plot"
14+
15+
16+
class MockModule(ModuleInterface):
17+
@classmethod
18+
def get_name(cls):
19+
return "Mock Module"
20+
21+
@classmethod
22+
def declare_weight(cls):
23+
return 999
24+
25+
@classmethod
26+
def declare_inputs(cls):
27+
input_list = [
28+
"project.economics_metrics",
29+
"project.lcoe_pdf",
30+
"project.confidence_density",
31+
]
32+
33+
return input_list
34+
35+
@classmethod
36+
def declare_outputs(cls):
37+
return None
38+
39+
@classmethod
40+
def declare_optional(cls):
41+
return None
42+
43+
@classmethod
44+
def declare_id_map(cls):
45+
id_map = {
46+
"economics_metrics": "project.economics_metrics",
47+
"confidence_density": "project.confidence_density",
48+
"lcoe_pdf": "project.lcoe_pdf",
49+
}
50+
51+
return id_map
52+
53+
def connect(self, debug_entry=False, export_data=True):
54+
pass
55+
56+
57+
@pytest.fixture()
58+
def tree():
59+
"""Share a Tree object"""
60+
61+
new_tree = Tree()
62+
63+
return new_tree
64+
65+
66+
# Using a py.test fixture to reduce boilerplate and test times.
67+
@pytest.fixture()
68+
def core():
69+
"""Share a Core object"""
70+
71+
new_core = Core()
72+
socket = new_core.control._sequencer.get_socket("ModuleInterface")
73+
socket.add_interface(MockModule)
74+
75+
return new_core
76+
77+
78+
@pytest.fixture()
79+
def project(core, tree):
80+
"""Share a Project object"""
81+
project_title = "Test"
82+
project_menu = ProjectMenu()
83+
84+
new_project = project_menu.new_project(core, project_title)
85+
86+
options_branch = tree.get_branch(core, new_project, "System Type Selection")
87+
device_type = options_branch.get_input_variable(
88+
core, new_project, "device.system_type"
89+
)
90+
device_type.set_raw_interface(core, "Tidal Fixed")
91+
device_type.read(core, new_project)
92+
93+
project_menu.initiate_pipeline(core, new_project)
94+
95+
return new_project
96+
97+
98+
def test_LCOEPDFPlot_available(
99+
core,
100+
project,
101+
tree,
102+
):
103+
module_menu = ModuleMenu()
104+
project_menu = ProjectMenu()
105+
106+
mod_name = "Mock Module"
107+
module_menu.activate(core, project, mod_name)
108+
project_menu.initiate_dataflow(core, project)
109+
110+
mod_branch = tree.get_branch(core, project, mod_name)
111+
eco_metrics = mod_branch.get_input_variable(
112+
core,
113+
project,
114+
"project.economics_metrics",
115+
)
116+
assert eco_metrics is not None
117+
118+
eco_metrics.set_file_interface(
119+
core,
120+
TEST_DATA_DIR_PATH / "eco_metrics.xlsx",
121+
)
122+
eco_metrics.read(core, project)
123+
124+
lcoe_pdf = mod_branch.get_input_variable(
125+
core,
126+
project,
127+
"project.lcoe_pdf",
128+
)
129+
assert lcoe_pdf is not None
130+
131+
lcoe_pdf.set_file_interface(
132+
core,
133+
TEST_DATA_DIR_PATH / "lcoe_pdf.nc",
134+
)
135+
lcoe_pdf.read(core, project)
136+
137+
confidence_density = mod_branch.get_input_variable(
138+
core,
139+
project,
140+
"project.confidence_density",
141+
)
142+
assert confidence_density is not None
143+
144+
confidence_density.set_raw_interface(core, 1.40122390504e-07)
145+
confidence_density.read(core, project)
146+
147+
result = lcoe_pdf.get_available_plots(core, project)
148+
149+
assert "LCOE PDF Analysis" in result
150+
151+
152+
def test_LCOEPDFPlot(
153+
core,
154+
project,
155+
tree,
156+
):
157+
module_menu = ModuleMenu()
158+
project_menu = ProjectMenu()
159+
160+
mod_name = "Mock Module"
161+
module_menu.activate(core, project, mod_name)
162+
project_menu.initiate_dataflow(core, project)
163+
164+
mod_branch = tree.get_branch(core, project, mod_name)
165+
eco_metrics = mod_branch.get_input_variable(
166+
core,
167+
project,
168+
"project.economics_metrics",
169+
)
170+
assert eco_metrics is not None
171+
172+
eco_metrics.set_file_interface(
173+
core,
174+
TEST_DATA_DIR_PATH / "eco_metrics.xlsx",
175+
)
176+
eco_metrics.read(core, project)
177+
178+
lcoe_pdf = mod_branch.get_input_variable(
179+
core,
180+
project,
181+
"project.lcoe_pdf",
182+
)
183+
assert lcoe_pdf is not None
184+
185+
lcoe_pdf.set_file_interface(
186+
core,
187+
TEST_DATA_DIR_PATH / "lcoe_pdf.nc",
188+
)
189+
lcoe_pdf.read(core, project)
190+
191+
confidence_density = mod_branch.get_input_variable(
192+
core,
193+
project,
194+
"project.confidence_density",
195+
)
196+
assert confidence_density is not None
197+
198+
confidence_density.set_raw_interface(core, 1.40122390504e-07)
199+
confidence_density.read(core, project)
200+
201+
lcoe_pdf.plot(core, project, "LCOE PDF Analysis")
202+
203+
assert len(plt.get_fignums()) == 1
204+
plt.close("all")

0 commit comments

Comments
 (0)