Skip to content

Commit bbd8982

Browse files
utili-fy additional calls relevant to NREL#365
1 parent c7104ab commit bbd8982

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

src/geophires_monte_carlo/MC_GeoPHIRES3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from geophires_monte_carlo.common import _get_logger
3030
from geophires_x.GeoPHIRESUtils import InsertImagesIntoHTML
3131
from geophires_x.GeoPHIRESUtils import render_default
32+
from geophires_x.MatplotlibUtils import plt_subplot
3233
from geophires_x_client import GeophiresInputParameters
3334
from geophires_x_client import GeophiresXClient
3435
from geophires_x_client import GeophiresXResult
@@ -519,7 +520,7 @@ def main(command_line_args=None):
519520
for i in range(len(inputs)):
520521
input = inputs[i][0]
521522
plt.figure(figsize=(8, 6))
522-
ax = plt.subplot()
523+
ax = plt_subplot()
523524
ax.set_title(input)
524525
ax.set_xlabel('Random Values')
525526
ax.set_ylabel('Probability')
@@ -553,7 +554,7 @@ def main(command_line_args=None):
553554
annotations += display
554555

555556
plt.figure(figsize=(8, 6))
556-
ax = plt.subplot()
557+
ax = plt_subplot()
557558
ax.set_title(output)
558559
ax.set_xlabel('Output units')
559560
ax.set_ylabel('Probability')

src/geophires_x/GeoPHIRESUtils.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import logging
4-
import os
54
import sys
65
from enum import Enum
76
from os.path import exists
@@ -15,7 +14,6 @@
1514
from pint.facets.plain import PlainQuantity
1615
from scipy.interpolate import interp1d
1716
import numpy as np
18-
import matplotlib.pyplot as plt
1917

2018
import CoolProp.CoolProp as CP
2119

@@ -622,21 +620,3 @@ def static_pressure_MPa(rho_kg_per_m3: float, depth_m: float) -> float:
622620
pressure_mpa = quantity(pressure_Pa, 'Pa').to('MPa').magnitude
623621

624622
return pressure_mpa
625-
626-
627-
def show_plot(**kw_args):
628-
"""
629-
Workaround for intermittent Windows GitHub Actions failures - see https://github.com/NREL/GEOPHIRES-X/issues/365
630-
"""
631-
try:
632-
plt.show(**kw_args)
633-
except Exception as e:
634-
# Can't import TclError directly since Python is not configured for Tk on all systems
635-
is_tcl_error = e.__class__.__name__ == 'TclError'
636-
637-
is_windows_on_github_actions = os.name == 'nt' and 'TOXPYTHON' in os.environ
638-
if is_tcl_error and is_windows_on_github_actions:
639-
_logger.warning(f'Ignoring TclError when attempting to show plot '
640-
f'since we appear to be running on Windows in GitHub Actions ({str(e)})')
641-
else:
642-
raise e

src/geophires_x/MatplotlibUtils.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Workarounds for intermittent Windows GitHub Actions failures - see https://github.com/NREL/GEOPHIRES-X/issues/365
3+
"""
4+
5+
from __future__ import annotations
6+
7+
import logging
8+
import os
9+
from typing import Any
10+
11+
from matplotlib import pyplot as plt
12+
13+
_logger = logging.getLogger(__name__)
14+
15+
16+
def plt_show(**kw_args):
17+
try:
18+
plt.show(**kw_args)
19+
except Exception as e:
20+
_handle_tcl_error_on_windows_github_actions(e)
21+
22+
23+
def plt_subplot() -> Any:
24+
try:
25+
return plt.subplot()
26+
except Exception as e:
27+
_handle_tcl_error_on_windows_github_actions(e)
28+
29+
30+
def plt_subplots(**kw_args) -> Any:
31+
try:
32+
return plt.subplots(**kw_args)
33+
except Exception as e:
34+
_handle_tcl_error_on_windows_github_actions(e)
35+
36+
37+
def _handle_tcl_error_on_windows_github_actions(e) -> None:
38+
# Can't import TclError directly since Python is not configured for Tk on all systems
39+
is_tcl_error = e.__class__.__name__ == 'TclError'
40+
41+
if os.name == 'nt' and 'TOXPYTHON' in os.environ:
42+
_logger.warning(f'Ignoring TclError when attempting to show plot '
43+
f'since we appear to be running on Windows in GitHub Actions ({str(e)})')
44+
else:
45+
raise e

src/geophires_x/OutputsRich.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from geophires_x.Economics import Economics
1818

1919
from geophires_x.GeoPHIRESUtils import UpgradeSymbologyOfUnits, render_default, InsertImagesIntoHTML
20+
from geophires_x.MatplotlibUtils import plt_subplots
2021
from geophires_x.OptionList import EndUseOptions, PlantType, EconomicModel, ReservoirModel, FractureShape, \
2122
ReservoirVolume
2223

@@ -1258,7 +1259,7 @@ def Plot_Twin_Graph(title: str, html_path: str, x: pd.array, y1: pd.array, y2: p
12581259
COLOR_TEMPERATURE = "#69b3a2"
12591260
COLOR_PRICE = "#3399e6"
12601261

1261-
fig, ax1 = plt.subplots(figsize=(40, 4))
1262+
fig, ax1 = plt_subplots(figsize=(40, 4))
12621263

12631264
ax1.plot(x, y1, label=UpgradeSymbologyOfUnits(y1_label), color=COLOR_PRICE, lw=3)
12641265
ax1.set_xlabel(UpgradeSymbologyOfUnits(x_label), color = COLOR_PRICE, fontsize=14)
@@ -1305,7 +1306,7 @@ def Plot_Single_Graph(title: str, html_path: str, x: pd.array, y: pd.array, x_la
13051306
COLOR_PRICE = "#3399e6"
13061307

13071308
# plt.plot(x, y, color=COLOR_PRICE)
1308-
fig, ax = plt.subplots(figsize=(40, 4))
1309+
fig, ax = plt_subplots(figsize=(40, 4))
13091310
ax.plot(x, y, label=UpgradeSymbologyOfUnits(y_label), color=COLOR_PRICE)
13101311
ax.set_xlabel(UpgradeSymbologyOfUnits(x_label), color = COLOR_PRICE, fontsize=14)
13111312
ax.set_ylabel(UpgradeSymbologyOfUnits(y_label), color=COLOR_PRICE, fontsize=14)

src/geophires_x/SUTRAReservoir.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from .GeoPHIRESUtils import show_plot
3+
from .MatplotlibUtils import plt_show
44
from .Parameter import strParameter, OutputParameter
55
from .Units import *
66
import geophires_x.Model as Model
@@ -220,7 +220,7 @@ def Calculate(self, model: Model):
220220
#plt.ylim([0, max(model.surfaceplant.daily_heating_demand.value) * 1.05])
221221
plt.legend()
222222
plt.title('SUTRA Heat Balance')
223-
show_plot(block=False)
223+
plt_show(block=False)
224224

225225
plt.figure(2)
226226
plt.plot(self.TimeProfile.value[0:-1:2], self.TargetHeat.value[0:-1:2], label='Target Heat')
@@ -230,7 +230,7 @@ def Calculate(self, model: Model):
230230
#plt.ylim([0, max(model.surfaceplant.daily_heating_demand.value) * 1.05])
231231
plt.legend()
232232
plt.title('SUTRA Target and Simulated Heat')
233-
show_plot(block=False)
233+
plt_show(block=False)
234234

235235
plt.figure(3)
236236
plt.plot(self.TimeProfile.value[0:-1:2], self.StorageWellFlowRate.value[0:-1:2], label='Storage Well Flow Rate')
@@ -240,7 +240,7 @@ def Calculate(self, model: Model):
240240
#plt.ylim([0, max(model.surfaceplant.daily_heating_demand.value) * 1.05])
241241
plt.legend()
242242
plt.title('SUTRA Well Flow Rates')
243-
show_plot(block=False)
243+
plt_show(block=False)
244244

245245
plt.figure(4)
246246
plt.plot(self.TimeProfile.value[0:-1:2], self.StorageWellTemperature.value[0:-1:2], label='Storage Well Temperature')
@@ -250,6 +250,6 @@ def Calculate(self, model: Model):
250250
# plt.ylim([0, max(model.surfaceplant.daily_heating_demand.value) * 1.05])
251251
plt.legend()
252252
plt.title('SUTRA Well Temperatures')
253-
show_plot(block=False)
253+
plt_show(block=False)
254254

255255
model.logger.info("Complete " + str(__class__) + ": " + sys._getframe().f_code.co_name)

0 commit comments

Comments
 (0)