| Conditions | 14 |
| Total Lines | 69 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like NiaPy.runner.Runner.__export_to_latex() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Implementation of Runner utility class.""" |
||
| 142 | def __export_to_latex(self): |
||
| 143 | r"""Export the results in the form of latex table. |
||
| 144 | |||
| 145 | See Also: |
||
| 146 | :func:`NiaPy.Runner.__createExportDir` |
||
| 147 | :func:`NiaPy.Runner.__generateExportName` |
||
| 148 | |||
| 149 | """ |
||
| 150 | |||
| 151 | self.__create_export_dir() |
||
| 152 | |||
| 153 | metrics = ["Best", "Median", "Worst", "Mean", "Std."] |
||
| 154 | |||
| 155 | def only_upper(s): |
||
| 156 | return "".join(c for c in s if c.isupper()) |
||
| 157 | |||
| 158 | with open(self.__generate_export_name("tex"), "a") as outFile: |
||
| 159 | outFile.write("\\documentclass{article}\n") |
||
| 160 | outFile.write("\\usepackage[utf8]{inputenc}\n") |
||
| 161 | outFile.write("\\usepackage{siunitx}\n") |
||
| 162 | outFile.write("\\sisetup{\n") |
||
| 163 | outFile.write("round-mode=places,round-precision=3}\n") |
||
| 164 | outFile.write("\\begin{document}\n") |
||
| 165 | outFile.write("\\begin{table}[h]\n") |
||
| 166 | outFile.write("\\centering\n") |
||
| 167 | begin_tabular = "\\begin{tabular}{cc" |
||
| 168 | for alg in self.results: |
||
| 169 | for _i in range(len(self.results[alg])): |
||
| 170 | begin_tabular += "S" |
||
| 171 | firstLine = " &" |
||
| 172 | for benchmark in self.results[alg].keys(): |
||
| 173 | firstLine += " & \\multicolumn{1}{c}{\\textbf{" + benchmark + "}}" |
||
| 174 | firstLine += " \\\\" |
||
| 175 | break |
||
| 176 | begin_tabular += "}\n" |
||
| 177 | outFile.write(begin_tabular) |
||
| 178 | outFile.write("\\hline\n") |
||
| 179 | outFile.write(firstLine + "\n") |
||
|
|
|||
| 180 | outFile.write("\\hline\n") |
||
| 181 | for alg in self.results: |
||
| 182 | for metric in metrics: |
||
| 183 | line = "" |
||
| 184 | if metric != "Worst": |
||
| 185 | line += " & " + metric |
||
| 186 | else: |
||
| 187 | shortAlg = "" |
||
| 188 | if alg.endswith("Algorithm"): |
||
| 189 | shortAlg = only_upper(alg[:-9]) |
||
| 190 | else: |
||
| 191 | shortAlg = only_upper(alg) |
||
| 192 | line += "\\textbf{" + shortAlg + "} & " + metric |
||
| 193 | for benchmark in self.results[alg]: |
||
| 194 | if metric == "Best": |
||
| 195 | line += " & " + str(amin(self.results[alg][benchmark])) |
||
| 196 | elif metric == "Median": |
||
| 197 | line += " & " + str(median(self.results[alg][benchmark])) |
||
| 198 | elif metric == "Worst": |
||
| 199 | line += " & " + str(amax(self.results[alg][benchmark])) |
||
| 200 | elif metric == "Mean": |
||
| 201 | line += " & " + str(mean(self.results[alg][benchmark])) |
||
| 202 | else: |
||
| 203 | line += " & " + str(std(self.results[alg][benchmark])) |
||
| 204 | line += " \\\\" |
||
| 205 | outFile.write(line + "\n") |
||
| 206 | outFile.write("\\hline\n") |
||
| 207 | outFile.write("\\end{tabular}\n") |
||
| 208 | outFile.write("\\end{table}\n") |
||
| 209 | outFile.write("\\end{document}") |
||
| 210 | logger.info("Export to Latex completed!") |
||
| 211 | |||
| 269 |