1313import numpy as np
1414import pandas as pd
1515from numpy .typing import ArrayLike
16+ import argparse
1617
1718
1819def min_max_normalized_mae (y_true : ArrayLike , y_pred : ArrayLike ) -> float :
@@ -124,8 +125,8 @@ def get_env_var(var_name: str, default: Any = None) -> Any:
124125 if value == "" and default is None :
125126 msg = f"The environment variable '{ var_name } ' is not set."
126127 raise OSError (msg )
127- if value .lower () in ["true" , "false" ]:
128- return value .lower () == "true"
128+ if str ( value ) .lower () in ["true" , "false" ]:
129+ return str ( value ) .lower () == "true"
129130 return value
130131
131132
@@ -147,8 +148,8 @@ class CommentData:
147148 get_env_var ("DIR_ARTIFACTS" , Path (get_env_var ("HOME" )) / "artifacts" )
148149 )
149150 # For RunSuccessfull body
150- plots_hash : str = get_env_var ("PLOTS_HASH" )
151- plots_string : str = get_env_var ("PLOTS" )
151+ plots_hash : str = get_env_var ("PLOTS_HASH" , "" )
152+ plots_string : str = get_env_var ("PLOTS" , "" )
152153
153154 _sucessfull_run = None
154155
@@ -216,13 +217,15 @@ def __init__(self):
216217 self .dir_feature = self .dir_feature [0 ]
217218
218219 self .plots_list = [
219- plot . split ( "/" )[ - 1 ]
220+ plot
220221 for plot in self .plots_string .split (" " )
221222 if plot .split ("." )[- 1 ] in ["png" , "jpg" , "jpeg" , "svg" ]
222223 ]
223224
224225 self ._variables_deviation_ds = None
225226
227+ self .plots_base_url = f"https://raw.githubusercontent.com/lkstrp/pypsa-validator/{ self .plots_hash } /_validation-images/"
228+
226229 # Status strings for file comparison table
227230 STATUS_FILE_MISSING = " :warning: Missing"
228231 STATUS_EQUAL = ":white_check_mark: Equal"
@@ -235,6 +238,7 @@ def __init__(self):
235238 STATUS_NEW = ":warning: New"
236239
237240 VARIABLES_FILE = "KN2045_Bal_v4/ariadne/exported_variables_full.xlsx"
241+ VARIABLES_THRESHOLD = 5
238242
239243 @property
240244 def variables_deviation_ds (self ):
@@ -258,10 +262,22 @@ def variables_deviation_ds(self):
258262 np .round (deviation , 2 ).mean (axis = 1 ), index = vars1 .index
259263 ).sort_values (ascending = False )
260264
261- self ._variables_deviation_ds = deviation
265+ # Filter for threshold
266+ deviation = deviation .loc [deviation > self .VARIABLES_THRESHOLD ]
262267
268+ self ._variables_deviation_ds = deviation
263269 return self ._variables_deviation_ds
264270
271+ @property
272+ def variables_plot_strings (self ):
273+ plots = (
274+ self .variables_deviation_ds .index .to_series ()
275+ .apply (lambda x : re .sub (r"[ |/]" , "-" , x ))
276+ .apply (lambda x : "ariadne_comparison/" + x + ".png" )
277+ .to_list ()
278+ )
279+ return plots
280+
265281 @property
266282 def variables_comparison (self ) -> str :
267283 if (
@@ -270,11 +286,9 @@ def variables_comparison(self) -> str:
270286 ):
271287 return ""
272288
273- df = self .variables_deviation_ds .loc [self .variables_deviation_ds > 5 ].apply (
274- lambda x : f"{ x :.2f} %"
275- )
289+ df = self .variables_deviation_ds .apply (lambda x : f"{ x :.2f} %" )
276290 df = pd .DataFrame (df , columns = ["MAPE" ])
277- df .index .name = ""
291+ df .index .name = None
278292
279293 return (
280294 f"{ df .to_html (escape = False )} \n "
@@ -292,18 +306,32 @@ def changed_variables_plots(self) -> str:
292306 or not (self .dir_feature / self .VARIABLES_FILE ).exists ()
293307 ):
294308 return ""
295- # Not implemented yet
296- return ""
309+
310+ rows : list = []
311+ for plot in self .variables_plot_strings :
312+ url_a = self .plots_base_url + "main/" + plot
313+ url_b = self .plots_base_url + "feature/" + plot
314+ rows .append (
315+ [
316+ f'<img src="{ url_a } " alt="Error in loading image.">' ,
317+ f'<img src="{ url_b } " alt="Error in loading image.">' ,
318+ ]
319+ )
320+
321+ df = pd .DataFrame (
322+ rows ,
323+ columns = pd .Index (["Main branch" , "Feature branch" ]),
324+ )
325+ return df .to_html (escape = False , index = False ) + "\n "
297326
298327 @property
299328 def plots_table (self ) -> str :
300329 """Plots comparison table."""
301- base_url = f"https://raw.githubusercontent.com/lkstrp/pypsa-validator/{ self .plots_hash } /_validation-images/"
302330
303331 rows : list = []
304332 for plot in self .plots_list :
305- url_a = base_url + "main/" + plot
306- url_b = base_url + "feature/" + plot
333+ url_a = self . plots_base_url + "main/" + plot
334+ url_b = self . plots_base_url + "feature/" + plot
307335 rows .append (
308336 [
309337 f'<img src="{ url_a } " alt="Image not found in results">' ,
@@ -575,6 +603,14 @@ def subtext(self) -> str:
575603 f"Last updated on `{ time } `."
576604 )
577605
606+ def needed_plots (self ):
607+ if self .sucessfull_run :
608+ body_sucessfull = RunSuccessfull ()
609+ plots_string = " " .join (body_sucessfull .variables_plot_strings )
610+ return plots_string
611+ else :
612+ ""
613+
578614 def __repr__ (self ) -> str :
579615 """Return full formatted comment."""
580616 if self .sucessfull_run :
@@ -598,7 +634,21 @@ def __repr__(self) -> str:
598634 )
599635
600636
601- if __name__ == "__main__" :
637+ def main ():
638+ parser = argparse .ArgumentParser (description = "Process some comments." )
639+ parser .add_argument (
640+ "command" , nargs = "?" , default = "" , help = 'Command to run, e.g., "plots".'
641+ )
642+ args = parser .parse_args ()
643+
602644 comment = Comment ()
603645
604- print (comment ) # noqa T201
646+ if args .command == "plots" :
647+ print (comment .needed_plots ())
648+
649+ else :
650+ print (comment ) # noqa T201
651+
652+
653+ if __name__ == "__main__" :
654+ main ()
0 commit comments