mirror of
				https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
				synced 2025-10-31 18:58:01 +00:00 
			
		
		
		
	lf_graph.py lf_report_test.py : pep8 complience
Signed-off-by: Chuck SmileyRekiere <chuck.smileyrekiere@candelatech.com>
This commit is contained in:
		| @@ -25,15 +25,18 @@ import numpy as np | ||||
| import pdfkit | ||||
| from matplotlib.colors import ListedColormap | ||||
| import argparse | ||||
|   | ||||
|  | ||||
| sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../"))) | ||||
|  | ||||
| lf_csv = importlib.import_module("py-scripts.lf_csv") | ||||
| lf_csv = lf_csv.lf_csv | ||||
|  | ||||
| # internal candela references included during intial phases, to be deleted at future date | ||||
| # internal candela references included during intial phases, to be deleted | ||||
| # at future date | ||||
|  | ||||
| # graph reporting classes | ||||
|  | ||||
|  | ||||
| class lf_bar_graph(): | ||||
|     def __init__(self, _data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]], | ||||
|                  _xaxis_name="x-axis", | ||||
| @@ -52,11 +55,11 @@ class lf_bar_graph(): | ||||
|                  _figsize=(10, 5), | ||||
|                  _show_bar_value=False, | ||||
|                  _xaxis_step=1, | ||||
|                  _xticks_font = None, | ||||
|                  _xaxis_value_location = 0, | ||||
|                  _xticks_font=None, | ||||
|                  _xaxis_value_location=0, | ||||
|                  _text_font=None, | ||||
|                  _text_rotation=None, | ||||
|                  _grp_title = "", | ||||
|                  _grp_title="", | ||||
|                  _legend_handles=None, | ||||
|                  _legend_loc="best", | ||||
|                  _legend_box=None, | ||||
| @@ -132,11 +135,19 @@ class lf_bar_graph(): | ||||
|         plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) | ||||
|         plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) | ||||
|         if self.xaxis_categories[0] == 0: | ||||
|             plt.xticks(np.arange(0, len(self.xaxis_categories), step=self.xaxis_step),fontsize = self.xticks_font) | ||||
|             plt.xticks(np.arange(0, | ||||
|                                  len(self.xaxis_categories), | ||||
|                                  step=self.xaxis_step), | ||||
|                        fontsize=self.xticks_font) | ||||
|         else: | ||||
|             plt.xticks([i + self._xaxis_value_location for i in np.arange(0, len(self.data_set[0]), step=self.xaxis_step)], | ||||
|                 self.xaxis_categories, fontsize=self.xticks_font) | ||||
|         plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize) | ||||
|                        self.xaxis_categories, fontsize=self.xticks_font) | ||||
|         plt.legend( | ||||
|             handles=self.legend_handles, | ||||
|             loc=self.legend_loc, | ||||
|             bbox_to_anchor=self.legend_box, | ||||
|             ncol=self.legend_ncol, | ||||
|             fontsize=self.legend_fontsize) | ||||
|         plt.suptitle(self.title, fontsize=self.title_size) | ||||
|         plt.title(self.grp_title) | ||||
|         fig = plt.gcf() | ||||
| @@ -155,7 +166,8 @@ class lf_bar_graph(): | ||||
|                     self.lf_csv.filename = f"{self.graph_image_name}.csv" | ||||
|                     self.lf_csv.generate_csv() | ||||
|                 else: | ||||
|                     raise ValueError("Length and x-axis values and y-axis values should be same.") | ||||
|                     raise ValueError( | ||||
|                         "Length and x-axis values and y-axis values should be same.") | ||||
|             else: | ||||
|                 print("No Dataset Found") | ||||
|         print("{}.csv".format(self.graph_image_name)) | ||||
| @@ -188,20 +200,38 @@ class lf_scatter_graph(): | ||||
|  | ||||
|     def build_scatter_graph(self): | ||||
|         if self.color is None: | ||||
|             self.color = ["orchid", "lime", "aquamarine", "royalblue", "darkgray", "maroon"] | ||||
|             self.color = [ | ||||
|                 "orchid", | ||||
|                 "lime", | ||||
|                 "aquamarine", | ||||
|                 "royalblue", | ||||
|                 "darkgray", | ||||
|                 "maroon"] | ||||
|         fig = plt.subplots(figsize=self.figsize) | ||||
|         if self.values is None: | ||||
|             plt.scatter(self.x_data_set, self.y_data_set[0], color=self.color[0], label=self.label[0]) | ||||
|             plt.scatter( | ||||
|                 self.x_data_set, | ||||
|                 self.y_data_set[0], | ||||
|                 color=self.color[0], | ||||
|                 label=self.label[0]) | ||||
|             if len(self.y_data_set) > 1: | ||||
|                 for i in range(1, len(self.y_data_set)): | ||||
|                     plt.scatter(self.x_data_set, self.y_data_set[i], color=self.color[i], label=self.label[i]) | ||||
|                     plt.scatter( | ||||
|                         self.x_data_set, | ||||
|                         self.y_data_set[i], | ||||
|                         color=self.color[i], | ||||
|                         label=self.label[i]) | ||||
|             plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) | ||||
|             plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) | ||||
|             plt.gcf().autofmt_xdate() | ||||
|             plt.legend() | ||||
|         else: | ||||
|             colours = ListedColormap(self.color) | ||||
|             scatter = plt.scatter(self.x_data_set, self.y_data_set, c=self.values, cmap=colours) | ||||
|             scatter = plt.scatter( | ||||
|                 self.x_data_set, | ||||
|                 self.y_data_set, | ||||
|                 c=self.values, | ||||
|                 cmap=colours) | ||||
|             plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) | ||||
|             plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) | ||||
|             plt.gcf().autofmt_xdate() | ||||
| @@ -241,9 +271,19 @@ class lf_stacked_graph(): | ||||
|     def build_stacked_graph(self): | ||||
|         fig = plt.subplots(figsize=self.figsize) | ||||
|         if self.color is None: | ||||
|             self.color = ["darkred", "tomato", "springgreen", "skyblue", "indigo", "plum"] | ||||
|             self.color = [ | ||||
|                 "darkred", | ||||
|                 "tomato", | ||||
|                 "springgreen", | ||||
|                 "skyblue", | ||||
|                 "indigo", | ||||
|                 "plum"] | ||||
|         plt.bar(self.data_set[0], self.data_set[1], color=self.color[0]) | ||||
|         plt.bar(self.data_set[0], self.data_set[2], bottom=self.data_set[1], color=self.color[1]) | ||||
|         plt.bar( | ||||
|             self.data_set[0], | ||||
|             self.data_set[2], | ||||
|             bottom=self.data_set[1], | ||||
|             color=self.color[1]) | ||||
|         if len(self.data_set) > 3: | ||||
|             for i in range(3, len(self.data_set)): | ||||
|                 plt.bar(self.data_set[0], self.data_set[i], | ||||
| @@ -303,8 +343,19 @@ class lf_horizontal_stacked_graph(): | ||||
|         ind = np.arange(n) + .15 | ||||
|         width = 0.3 | ||||
|  | ||||
|         rects1 = plt.barh(ind, values1, width, color=self.color[0], label=self.label[0]) | ||||
|         rects2 = plt.barh(ind, values2, width, left=sumzip(values1), color=self.color[1], label=self.label[1]) | ||||
|         rects1 = plt.barh( | ||||
|             ind, | ||||
|             values1, | ||||
|             width, | ||||
|             color=self.color[0], | ||||
|             label=self.label[0]) | ||||
|         rects2 = plt.barh( | ||||
|             ind, | ||||
|             values2, | ||||
|             width, | ||||
|             left=sumzip(values1), | ||||
|             color=self.color[1], | ||||
|             label=self.label[1]) | ||||
|  | ||||
|         extra_space = 0.15 | ||||
|         ax.set_yticks(ind + width - extra_space) | ||||
| @@ -326,7 +377,12 @@ class lf_horizontal_stacked_graph(): | ||||
|         ax.spines['top'].set_visible(False) | ||||
|         ax.legend(loc='upper right') | ||||
|         if self.disable_xaxis: | ||||
|             plt.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)  # disable x-axis | ||||
|             plt.tick_params( | ||||
|                 axis='x', | ||||
|                 which='both', | ||||
|                 bottom=False, | ||||
|                 top=False, | ||||
|                 labelbottom=False)  # disable x-axis | ||||
|         plt.savefig("%s.png" % self.graph_image_name, dpi=96) | ||||
|         plt.close() | ||||
|         print("{}.png".format(self.graph_image_name)) | ||||
| @@ -339,7 +395,7 @@ class lf_horizontal_stacked_graph(): | ||||
|  | ||||
|  | ||||
| class lf_line_graph(): | ||||
|     def __init__(self,_data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]], | ||||
|     def __init__(self, _data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]], | ||||
|                  _xaxis_name="x-axis", | ||||
|                  _yaxis_name="y-axis", | ||||
|                  _xaxis_categories=[1, 2, 3, 4, 5], | ||||
| @@ -351,8 +407,8 @@ class lf_line_graph(): | ||||
|                  _font_weight='bold', | ||||
|                  _color=['forestgreen', 'c', 'r', 'g', 'b', 'p'], | ||||
|                  _figsize=(10, 5), | ||||
|                  _xaxis_step = 5, | ||||
|                  _xticks_font = None, | ||||
|                  _xaxis_step=5, | ||||
|                  _xticks_font=None, | ||||
|                  _text_font=None, | ||||
|                  _legend_handles=None, | ||||
|                  _legend_loc="best", | ||||
| @@ -390,12 +446,22 @@ class lf_line_graph(): | ||||
|         fig = plt.subplots(figsize=self.figsize) | ||||
|         i = 0 | ||||
|         for data in self.data_set: | ||||
|             plt.plot(self.xaxis_categories, data, color=self.color[i], label=self.label[i], marker = self.marker) | ||||
|             plt.plot( | ||||
|                 self.xaxis_categories, | ||||
|                 data, | ||||
|                 color=self.color[i], | ||||
|                 label=self.label[i], | ||||
|                 marker=self.marker) | ||||
|             i += 1 | ||||
|  | ||||
|         plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) | ||||
|         plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) | ||||
|         plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize) | ||||
|         plt.legend( | ||||
|             handles=self.legend_handles, | ||||
|             loc=self.legend_loc, | ||||
|             bbox_to_anchor=self.legend_box, | ||||
|             ncol=self.legend_ncol, | ||||
|             fontsize=self.legend_fontsize) | ||||
|         plt.suptitle(self.grp_title, fontsize=self.title_size) | ||||
|         fig = plt.gcf() | ||||
|         plt.savefig("%s.png" % self.graph_image_name, dpi=96) | ||||
| @@ -412,6 +478,7 @@ class lf_line_graph(): | ||||
|         print("{}.csv".format(self.graph_image_name)) | ||||
|         return "%s.png" % self.graph_image_name | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     # arguments | ||||
|     parser = argparse.ArgumentParser( | ||||
| @@ -421,7 +488,7 @@ def main(): | ||||
|             lf_graph.py : unit test in lf_graph.py for exersizing the lf_graph.py library | ||||
|             ''', | ||||
|         description='''\ | ||||
| -----------------             | ||||
| ----------------- | ||||
| NAME: lf_graph.py | ||||
|  | ||||
| PURPOSE: | ||||
| @@ -440,8 +507,13 @@ COPYWRITE | ||||
| INCLUDE_IN_README | ||||
| --------------------- | ||||
|             ''') | ||||
|     parser.add_argument('--mgr','--lfmgr', dest='lfmgr', help='sample argument: where LANforge GUI is running', default='localhost')     | ||||
|     # the args parser is not really used , this is so the report is not generated when testing  | ||||
|     parser.add_argument( | ||||
|         '--mgr', | ||||
|         '--lfmgr', | ||||
|         dest='lfmgr', | ||||
|         help='sample argument: where LANforge GUI is running', | ||||
|         default='localhost') | ||||
|     # the args parser is not really used , this is so the report is not generated when testing | ||||
|     # the imports with --help | ||||
|     args = parser.parse_args() | ||||
|     print("LANforge manager {lfmgr}".format(lfmgr=args.lfmgr)) | ||||
| @@ -464,7 +536,8 @@ INCLUDE_IN_README | ||||
|     # write logic to generate pdf here | ||||
|     # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb | ||||
|     # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb | ||||
|     options = {"enable-local-file-access": None}  # prevent eerror Blocked access to file | ||||
|     # prevent eerror Blocked access to file | ||||
|     options = {"enable-local-file-access": None} | ||||
|     pdfkit.from_file(output_html_1, output_pdf_1, options=options) | ||||
|  | ||||
|     # test build_bar_graph setting values | ||||
| @@ -497,10 +570,11 @@ INCLUDE_IN_README | ||||
|     # write logic to generate pdf here | ||||
|     # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb | ||||
|     # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb | ||||
|     options = {"enable-local-file-access": None}  # prevent eerror Blocked access to file | ||||
|     # prevent eerror Blocked access to file | ||||
|     options = {"enable-local-file-access": None} | ||||
|     pdfkit.from_file(output_html_2, output_pdf_2, options=options) | ||||
|  | ||||
|  | ||||
| # Unit Test | ||||
| if __name__ == "__main__": | ||||
|     main()     | ||||
|  | ||||
|     main() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Chuck SmileyRekiere
					Chuck SmileyRekiere