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.""" |
||
145 | def __export_to_latex(self): |
||
146 | r"""Export the results in the form of latex table. |
||
147 | |||
148 | See Also: |
||
149 | :func:`NiaPy.Runner.__createExportDir` |
||
150 | :func:`NiaPy.Runner.__generateExportName` |
||
151 | |||
152 | """ |
||
153 | |||
154 | self.__create_export_dir() |
||
155 | |||
156 | metrics = ["Best", "Median", "Worst", "Mean", "Std."] |
||
157 | |||
158 | def only_upper(s): |
||
159 | return "".join(c for c in s if c.isupper()) |
||
160 | |||
161 | with open(self.__generate_export_name("tex"), "a") as outFile: |
||
162 | outFile.write("\\documentclass{article}\n") |
||
163 | outFile.write("\\usepackage[utf8]{inputenc}\n") |
||
164 | outFile.write("\\usepackage{siunitx}\n") |
||
165 | outFile.write("\\sisetup{\n") |
||
166 | outFile.write("round-mode=places,round-precision=3}\n") |
||
167 | outFile.write("\\begin{document}\n") |
||
168 | outFile.write("\\begin{table}[h]\n") |
||
169 | outFile.write("\\centering\n") |
||
170 | begin_tabular = "\\begin{tabular}{cc" |
||
171 | for alg in self.results: |
||
172 | for _i in range(len(self.results[alg])): |
||
173 | begin_tabular += "S" |
||
174 | firstLine = " &" |
||
175 | for benchmark in self.results[alg].keys(): |
||
176 | firstLine += " & \\multicolumn{1}{c}{\\textbf{" + benchmark + "}}" |
||
177 | firstLine += " \\\\" |
||
178 | break |
||
179 | begin_tabular += "}\n" |
||
180 | outFile.write(begin_tabular) |
||
181 | outFile.write("\\hline\n") |
||
182 | outFile.write(firstLine + "\n") |
||
|
|||
183 | outFile.write("\\hline\n") |
||
184 | for alg in self.results: |
||
185 | for metric in metrics: |
||
186 | line = "" |
||
187 | if metric != "Worst": |
||
188 | line += " & " + metric |
||
189 | else: |
||
190 | shortAlg = "" |
||
191 | if alg.endswith("Algorithm"): |
||
192 | shortAlg = only_upper(alg[:-9]) |
||
193 | else: |
||
194 | shortAlg = only_upper(alg) |
||
195 | line += "\\textbf{" + shortAlg + "} & " + metric |
||
196 | for benchmark in self.results[alg]: |
||
197 | if metric == "Best": |
||
198 | line += " & " + str(amin(self.results[alg][benchmark])) |
||
199 | elif metric == "Median": |
||
200 | line += " & " + str(median(self.results[alg][benchmark])) |
||
201 | elif metric == "Worst": |
||
202 | line += " & " + str(amax(self.results[alg][benchmark])) |
||
203 | elif metric == "Mean": |
||
204 | line += " & " + str(mean(self.results[alg][benchmark])) |
||
205 | else: |
||
206 | line += " & " + str(std(self.results[alg][benchmark])) |
||
207 | line += " \\\\" |
||
208 | outFile.write(line + "\n") |
||
209 | outFile.write("\\hline\n") |
||
210 | outFile.write("\\end{tabular}\n") |
||
211 | outFile.write("\\end{table}\n") |
||
212 | outFile.write("\\end{document}") |
||
213 | logger.info("Export to Latex completed!") |
||
214 | |||
272 |