Mercurial > repos > goeckslab > tabular_learner
comparison utils.py @ 0:209b663a4f62 draft
planemo upload for repository https://github.com/goeckslab/gleam commit 5dd048419fcbd285a327f88267e93996cd279ee6
| author | goeckslab |
|---|---|
| date | Wed, 18 Jun 2025 15:38:19 +0000 |
| parents | |
| children | 77c88226bfde |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:209b663a4f62 |
|---|---|
| 1 import base64 | |
| 2 import logging | |
| 3 | |
| 4 import numpy as np | |
| 5 | |
| 6 logging.basicConfig(level=logging.DEBUG) | |
| 7 LOG = logging.getLogger(__name__) | |
| 8 | |
| 9 | |
| 10 def get_html_template(): | |
| 11 return """ | |
| 12 <html> | |
| 13 <head> | |
| 14 <title>Model Training Report</title> | |
| 15 <style> | |
| 16 body { | |
| 17 font-family: Arial, sans-serif; | |
| 18 margin: 0; | |
| 19 padding: 20px; | |
| 20 background-color: #f4f4f4; | |
| 21 } | |
| 22 .container { | |
| 23 max-width: 800px; | |
| 24 margin: auto; | |
| 25 background: white; | |
| 26 padding: 20px; | |
| 27 box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | |
| 28 } | |
| 29 h1 { | |
| 30 text-align: center; | |
| 31 color: #333; | |
| 32 } | |
| 33 h2 { | |
| 34 border-bottom: 2px solid #4CAF50; | |
| 35 color: #4CAF50; | |
| 36 padding-bottom: 5px; | |
| 37 } | |
| 38 table { | |
| 39 width: 100%; | |
| 40 border-collapse: collapse; | |
| 41 margin: 20px 0; | |
| 42 } | |
| 43 table, th, td { | |
| 44 border: 1px solid #ddd; | |
| 45 } | |
| 46 th, td { | |
| 47 padding: 8px; | |
| 48 text-align: left; | |
| 49 } | |
| 50 th { | |
| 51 background-color: #4CAF50; | |
| 52 color: white; | |
| 53 } | |
| 54 .plot { | |
| 55 text-align: center; | |
| 56 margin: 20px 0; | |
| 57 } | |
| 58 .plot img { | |
| 59 max-width: 100%; | |
| 60 height: auto; | |
| 61 } | |
| 62 .tabs { | |
| 63 display: flex; | |
| 64 margin-bottom: 20px; | |
| 65 cursor: pointer; | |
| 66 justify-content: space-around; | |
| 67 } | |
| 68 .tab { | |
| 69 padding: 10px; | |
| 70 background-color: #4CAF50; | |
| 71 color: white; | |
| 72 border-radius: 5px 5px 0 0; | |
| 73 flex-grow: 1; | |
| 74 text-align: center; | |
| 75 margin: 0 5px; | |
| 76 } | |
| 77 .tab.active-tab { | |
| 78 background-color: #333; | |
| 79 } | |
| 80 .tab-content { | |
| 81 display: none; | |
| 82 padding: 20px; | |
| 83 border: 1px solid #ddd; | |
| 84 border-top: none; | |
| 85 background-color: white; | |
| 86 } | |
| 87 .tab-content.active-content { | |
| 88 display: block; | |
| 89 } | |
| 90 </style> | |
| 91 </head> | |
| 92 <body> | |
| 93 <div class="container"> | |
| 94 """ | |
| 95 | |
| 96 | |
| 97 def get_html_closing(): | |
| 98 return """ | |
| 99 </div> | |
| 100 <script> | |
| 101 function openTab(evt, tabName) {{ | |
| 102 var i, tabcontent, tablinks; | |
| 103 tabcontent = document.getElementsByClassName("tab-content"); | |
| 104 for (i = 0; i < tabcontent.length; i++) {{ | |
| 105 tabcontent[i].style.display = "none"; | |
| 106 }} | |
| 107 tablinks = document.getElementsByClassName("tab"); | |
| 108 for (i = 0; i < tablinks.length; i++) {{ | |
| 109 tablinks[i].className = | |
| 110 tablinks[i].className.replace(" active-tab", ""); | |
| 111 }} | |
| 112 document.getElementById(tabName).style.display = "block"; | |
| 113 evt.currentTarget.className += " active-tab"; | |
| 114 }} | |
| 115 document.addEventListener("DOMContentLoaded", function() {{ | |
| 116 document.querySelector(".tab").click(); | |
| 117 }}); | |
| 118 </script> | |
| 119 </body> | |
| 120 </html> | |
| 121 """ | |
| 122 | |
| 123 | |
| 124 def customize_figure_layout(fig, margin_dict=None): | |
| 125 """ | |
| 126 Update the layout of a Plotly figure to reduce margins. | |
| 127 | |
| 128 Parameters: | |
| 129 fig (plotly.graph_objects.Figure): The Plotly figure to customize. | |
| 130 margin_dict (dict, optional): A dictionary specifying margin sizes. | |
| 131 Example: {'l': 10, 'r': 10, 't': 10, 'b': 10} | |
| 132 | |
| 133 Returns: | |
| 134 plotly.graph_objects.Figure: The updated Plotly figure. | |
| 135 """ | |
| 136 if margin_dict is None: | |
| 137 # Set default smaller margins | |
| 138 margin_dict = {'l': 40, 'r': 40, 't': 40, 'b': 40} | |
| 139 | |
| 140 fig.update_layout(margin=margin_dict) | |
| 141 return fig | |
| 142 | |
| 143 | |
| 144 def add_plot_to_html(fig, include_plotlyjs=True): | |
| 145 custom_margin = {'l': 40, 'r': 40, 't': 60, 'b': 60} | |
| 146 fig = customize_figure_layout(fig, margin_dict=custom_margin) | |
| 147 return fig.to_html(full_html=False, | |
| 148 default_height=350, | |
| 149 include_plotlyjs="cdn" if include_plotlyjs else False) | |
| 150 | |
| 151 | |
| 152 def add_hr_to_html(): | |
| 153 return "<hr>" | |
| 154 | |
| 155 | |
| 156 def encode_image_to_base64(image_path): | |
| 157 """Convert an image file to a base64 encoded string.""" | |
| 158 with open(image_path, "rb") as img_file: | |
| 159 return base64.b64encode(img_file.read()).decode("utf-8") | |
| 160 | |
| 161 | |
| 162 def predict_proba(self, X): | |
| 163 pred = self.predict(X) | |
| 164 return np.array([1 - pred, pred]).T |
