| Conditions | 17 |
| Total Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 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 Runner.__exportToLatex() 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 | from __future__ import print_function # for backward compatibility purpose |
||
| 121 | def __exportToLatex(self): |
||
| 122 | metrics = ['Best', 'Median', 'Worst', 'Mean', 'Std.'] |
||
| 123 | |||
| 124 | def only_upper(s): |
||
| 125 | return "".join(c for c in s if c.isupper()) |
||
| 126 | |||
| 127 | with open(self.__generateExportName('tex'), 'a') as outFile: |
||
| 128 | outFile.write('\\documentclass{article}\n') |
||
| 129 | outFile.write('\\usepackage[utf8]{inputenc}\n') |
||
| 130 | outFile.write('\\usepackage{siunitx}\n') |
||
| 131 | outFile.write('\\sisetup{\n') |
||
| 132 | outFile.write('round-mode=places,round-precision=3}\n') |
||
| 133 | outFile.write('\\begin{document}\n') |
||
| 134 | outFile.write('\\begin{table}[h]\n') |
||
| 135 | outFile.write('\\centering\n') |
||
| 136 | |||
| 137 | begin_tabular = '\\begin{tabular}{cc' |
||
| 138 | |||
| 139 | for alg in self.results: |
||
| 140 | for _i in range(len(self.results[alg])): |
||
| 141 | begin_tabular += 'S' |
||
| 142 | |||
| 143 | firstLine = ' &' |
||
| 144 | |||
| 145 | for benchmark in self.results[alg].keys(): |
||
| 146 | firstLine += ' & \\multicolumn{1}{c}{\\textbf{' + \ |
||
| 147 | benchmark + '}}' |
||
| 148 | |||
| 149 | firstLine += ' \\\\' |
||
| 150 | |||
| 151 | break |
||
| 152 | |||
| 153 | begin_tabular += '}\n' |
||
| 154 | outFile.write(begin_tabular) |
||
| 155 | outFile.write('\\hline\n') |
||
| 156 | outFile.write(firstLine + '\n') |
||
| 157 | outFile.write('\\hline\n') |
||
| 158 | |||
| 159 | for alg in self.results: |
||
| 160 | for metric in metrics: |
||
| 161 | line = '' |
||
| 162 | |||
| 163 | if metric != 'Worst': |
||
| 164 | line += ' & ' + metric |
||
| 165 | else: |
||
| 166 | shortAlg = '' |
||
| 167 | if alg.endswith('Algorithm'): |
||
| 168 | shortAlg = only_upper(alg[:-9]) |
||
| 169 | else: |
||
| 170 | shortAlg = only_upper(alg) |
||
| 171 | line += '\\textbf{' + shortAlg + '} & ' + metric |
||
| 172 | |||
| 173 | for benchmark in self.results[alg]: |
||
| 174 | if metric == 'Best': |
||
| 175 | line += ' & ' + str(np.amin(self.results[alg][benchmark])) |
||
| 176 | elif metric == 'Median': |
||
| 177 | line += ' & ' + str(np.median(self.results[alg][benchmark])) |
||
| 178 | elif metric == 'Worst': |
||
| 179 | line += ' & ' + str(np.amax(self.results[alg][benchmark])) |
||
| 180 | elif metric == 'Mean': |
||
| 181 | line += ' & ' + str(np.mean(self.results[alg][benchmark])) |
||
| 182 | else: |
||
| 183 | line += ' & ' + str(np.std(self.results[alg][benchmark])) |
||
| 184 | |||
| 185 | line += ' \\\\' |
||
| 186 | outFile.write(line + '\n') |
||
| 187 | |||
| 188 | outFile.write('\\hline\n') |
||
| 189 | outFile.write('\\end{tabular}\n') |
||
| 190 | outFile.write('\\end{table}\n') |
||
| 191 | outFile.write('\\end{document}') |
||
| 192 | |||
| 193 | logger.info('Export to Latex completed!') |
||
| 194 | |||
| 233 |