Skip to content

Commit 6ff5242

Browse files
malcolm-dsiderbpulluta
authored andcommitted
added rich HTML output for All of GEOPHIRES.
1 parent b9d0741 commit 6ff5242

File tree

6 files changed

+1866
-412
lines changed

6 files changed

+1866
-412
lines changed

src/geophires_monte_carlo/MC_GeoPHIRES3.py

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from rich.table import Table
2727

2828
from geophires_monte_carlo.common import _get_logger
29-
from geophires_x.Parameter import Parameter
29+
from geophires_x.GeoPHIRESUtils import InsertImagesIntoHTML
30+
from geophires_x.GeoPHIRESUtils import render_default
3031
from geophires_x_client import GeophiresInputParameters
3132
from geophires_x_client import GeophiresXClient
3233
from geophires_x_client import GeophiresXResult
@@ -125,95 +126,7 @@ def Write_HTML_Output(
125126
console.print(statistics_table)
126127
console.save_html(html_path)
127128

128-
# Write a reference to the image(s) into the HTML file by inserting before the "</body>" tag
129-
# build the string to be inserted first
130-
insert_string = ''
131-
for _ in range(len(full_names)):
132-
name_to_use = short_names.pop()
133-
insert_string = insert_string + f'<img src="{name_to_use}.png" alt="{name_to_use}">\n'
134-
135-
match_string = '</body>'
136-
with open(html_path, 'r+', encoding='UTF-8') as html_file:
137-
contents = html_file.readlines()
138-
if match_string in contents[-1]: # Handle last line to prevent IndexError
139-
pass
140-
else:
141-
for index, line in enumerate(contents):
142-
if match_string in line and insert_string not in contents[index + 1]:
143-
contents.insert(index, insert_string)
144-
break
145-
html_file.seek(0)
146-
html_file.writelines(contents)
147-
148-
149-
def UpgradeSymbologyOfUnits(unit: str) -> str:
150-
"""
151-
UpgradeSymbologyOfUnits is a function that takes a string that represents a unit and replaces the **2 and **3
152-
with the appropriate unicode characters for superscript 2 and 3, and replaces "deg" with the unicode character
153-
for degrees.
154-
:param unit: a string that represents a unit
155-
:return: a string that represents a unit with the appropriate unicode characters for superscript 2 and 3, and
156-
replaces "deg" with the unicode character for degrees.
157-
"""
158-
return unit.replace('**2', '\u00b2').replace('**3', '\u00b3').replace('deg', '\u00b0')
159-
160-
161-
def render_default(p: float, unit: str = '') -> str:
162-
"""
163-
RenderDefault - render a float as a string with 2 decimal places, or in scientific notation if it is greater than
164-
10,000 with the unit appended to it if it is not an empty string (the default)
165-
:param p: the float to render
166-
:type p: float
167-
:param unit: the unit to append to the string
168-
:type unit: str
169-
:return: the string representation of the float
170-
:rtype: str
171-
"""
172-
unit = UpgradeSymbologyOfUnits(unit)
173-
# if the number is greater than 10,000, render it in scientific notation
174-
if p > 10_000:
175-
return f'{p:10.2e} {unit}'.strip()
176-
# otherwise, render it with 2 decimal places
177-
else:
178-
return f'{p:10.2f} {unit}'.strip()
179-
180-
181-
def render_scientific(p: float, unit: str = '') -> str:
182-
"""
183-
RenderScientific - render a float as a string in scientific notation with 2 decimal places
184-
and the unit appended to it if it is not an empty string (the default)
185-
:param p: the float to render
186-
:type p: float
187-
:param unit: the unit to append to the string
188-
:type unit: str
189-
:return: the string representation of the float
190-
:rtype: str
191-
"""
192-
unit = UpgradeSymbologyOfUnits(unit)
193-
return f'{p:10.2e} {unit}'.strip()
194-
195-
196-
def render_Parameter_default(p: Parameter) -> str:
197-
"""
198-
RenderDefault - render a float as a string with 2 decimal places, or in scientific notation if it is greater than
199-
10,000 with the unit appended to it if it is not an empty string (the default) by calling the render_default base
200-
function
201-
:param p: the parameter to render
202-
:type p: float
203-
:return: the string representation of the float
204-
"""
205-
return render_default(p.value, p.CurrentUnits.value)
206-
207-
208-
def render_parameter_scientific(p: Parameter) -> str:
209-
"""
210-
RenderScientific - render a float as a string in scientific notation with 2 decimal places
211-
and the unit appended to it if it is not an empty string (the default) by calling the render_scientific base function
212-
:param p: the parameter to render
213-
:type p: float
214-
:return: the string representation of the float
215-
"""
216-
return render_scientific(p.value, p.CurrentUnits.value)
129+
InsertImagesIntoHTML(html_path, full_names, short_names)
217130

218131

219132
def check_and_replace_mean(input_value, args) -> list:

src/geophires_x/GEOPHIRESv3.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ def main(enable_geophires_logging_config=True):
8080
for line in content:
8181
sys.stdout.write(line)
8282

83-
# make district heating plot
84-
if model.surfaceplant.plant_type.value == OptionList.PlantType.DISTRICT_HEATING:
85-
model.outputs.MakeDistrictHeatingPlot(model)
86-
8783
logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
8884

8985

src/geophires_x/GeoPHIRESUtils.py

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import CoolProp.CoolProp as CP
1919

20-
from geophires_x.Parameter import ParameterEntry
20+
from geophires_x.Parameter import ParameterEntry, Parameter
2121
from geophires_x.Units import get_unit_registry
2222

2323
_logger = logging.getLogger('root') # TODO use __name__ instead of root
@@ -94,6 +94,131 @@
9494
_ureg = get_unit_registry()
9595

9696

97+
def InsertImagesIntoHTML(html_path: str, short_names: set, full_names: set) -> None:
98+
99+
# Write a reference to the image(s) into the HTML file by inserting before the "</body>" tag
100+
# build the string to be inserted first
101+
insert_string = ''
102+
for _ in range(len(full_names)):
103+
name_to_use = short_names.pop()
104+
insert_string = insert_string + f'<img src="{name_to_use}.png" alt="{name_to_use}">\n<br>'
105+
106+
match_string = '</body>'
107+
with open(html_path, 'r+', encoding='UTF-8') as html_file:
108+
contents = html_file.readlines()
109+
if match_string in contents[-1]: # Handle last line to prevent IndexError
110+
pass
111+
else:
112+
for index, line in enumerate(contents):
113+
if match_string in line and insert_string not in contents[index + 1]:
114+
contents.insert(index, insert_string)
115+
break
116+
html_file.seek(0)
117+
html_file.writelines(contents)
118+
119+
120+
def UpgradeSymbologyOfUnits(unit: str) -> str:
121+
"""
122+
UpgradeSymbologyOfUnits is a function that takes a string that represents a unit and replaces the **2 and **3
123+
with the appropriate unicode characters for superscript 2 and 3, and replaces "deg" with the unicode character
124+
for degrees.
125+
:param unit: a string that represents a unit
126+
:return: a string that represents a unit with the appropriate unicode characters for superscript 2 and 3, and
127+
replaces "deg" with the unicode character for degrees.
128+
"""
129+
130+
return unit.replace('**2', '\u00b2').replace('**3', '\u00b3').replace('deg', '\u00b0')
131+
132+
133+
def render_default(p: float, unit: str = '', fmt: str = '') -> str:
134+
"""
135+
RenderDefault - render a float as a string with 2 decimal place by default, or whatever format the user specifies,
136+
or in scientific notation if it is greater than 10,000
137+
with the unit appended to it if it is not an empty string (the default)
138+
:param p: the float to render
139+
:type p: float
140+
:param unit: the unit to append to the string
141+
:type unit: str
142+
:param fmt: the format to use for the string representation of the float
143+
:type fmt: str
144+
:return: the string representation of the float
145+
"""
146+
if not np.can_cast(p, float):
147+
raise ValueError(f'Parameter ({p}) must be a float or convertible to float.')
148+
149+
unit = UpgradeSymbologyOfUnits(unit)
150+
# if the number is greater than 10,000, render it in scientific notation
151+
if p > 10_000:
152+
return render_scientific(p, unit)
153+
# otherwise, render it with 2 decimal places
154+
else:
155+
if not fmt:
156+
return f'{p:10.2f} {unit}'.strip()
157+
else:
158+
if ':' in fmt:
159+
fmt = fmt.split(':')[1]
160+
fmt = '{0:' + fmt + '}{1:s}'
161+
return fmt.format(p, unit.strip())
162+
163+
164+
def render_scientific(p: float, unit: str = '', fmt: str = '') -> str:
165+
"""
166+
RenderScientific - render a float as a string in scientific notation with 2 decimal places by default, or whatever
167+
format the user specifies, and the unit appended to it if it is not an empty string (the default)
168+
:param p: the float to render
169+
:type p: float
170+
:param unit: the unit to append to the string
171+
:type unit: str
172+
:param fmt: the format to use for the string representation of the float
173+
:type fmt: str
174+
:return: the string representation of the float
175+
:rtype: str
176+
"""
177+
178+
if not np.can_cast(p, float):
179+
raise ValueError(f'Parameter ({p}) must be a float or convertible to float.')
180+
181+
unit = UpgradeSymbologyOfUnits(unit)
182+
if not fmt:
183+
return f'{p:10.2e} {unit}'.strip()
184+
else:
185+
pass
186+
187+
188+
def render_Parameter_default(p: Parameter, fmt: str = '') -> str:
189+
"""
190+
RenderDefault - render a float parameter in scientific notation as a string with 2 decimal places,
191+
or whatever format the user specifies with the unit appended to it if it is not an empty string (the default)
192+
function
193+
:param p: the parameter to render
194+
:type p: Parameter
195+
:param fmt: the format to use for the string representation of the float
196+
:type fmt: str
197+
:return: the string representation of the float
198+
"""
199+
if not np.can_cast(p.value, float):
200+
raise ValueError(f'Parameter ({p.value}) must be a float or convertible to float.')
201+
202+
return render_default(p.value, p.CurrentUnits.value)
203+
204+
205+
def render_parameter_scientific(p: Parameter, fmt: str = '') -> str:
206+
"""
207+
RenderScientific - render a float as a string in scientific notation with 2 decimal places
208+
and the unit appended to it if it is not an empty string (the default) by calling the render_scientific base function
209+
:param p: the parameter to render
210+
:type p: float
211+
:param fmt: the format to use for the string representation of the float
212+
:type fmt: str
213+
:return: the string representation of the float
214+
"""
215+
216+
if not np.can_cast(p.value, float):
217+
raise ValueError(f'Parameter ({p.value}) must be a float or convertible to float.')
218+
219+
return render_scientific(p.value, p.CurrentUnits.value)
220+
221+
97222
def quantity(value: float, unit: str) -> PlainQuantity:
98223
"""
99224
:rtype: pint.registry.Quantity - note type annotation uses PlainQuantity due to issues with python 3.8 failing

0 commit comments

Comments
 (0)