| Conditions | 12 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 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 papers.paper1.plot_test_scalar_metrics() 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 | """ |
||
| 17 | def plot_test_scalar_metrics(metric_function, filenames, metric_label=None, |
||
| 18 | debug=True, **kwargs): |
||
| 19 | |||
| 20 | Lambdas = [] |
||
| 21 | scale_factors = [] |
||
| 22 | metrics = [] |
||
| 23 | |||
| 24 | for filename in filenames: |
||
| 25 | |||
| 26 | try: |
||
| 27 | _ = filename.split("-") |
||
| 28 | scale_factor = float(_[1]) |
||
| 29 | log10_Lambda = float(_[2].split(".model")[0]) |
||
| 30 | |||
| 31 | except: |
||
| 32 | print("Skipping filename {}".format(filename)) |
||
| 33 | continue |
||
| 34 | |||
| 35 | with open(filename, "rb") as fp: |
||
| 36 | contents = pickle.load(fp, encoding="latin-1") |
||
| 37 | |||
| 38 | snrs, high_snr_expected, high_snr_inferred, \ |
||
| 39 | differences_expected, differences_inferred, \ |
||
| 40 | single_visit_inferred = contents |
||
| 41 | |||
| 42 | # Calculate the metric. |
||
| 43 | try: |
||
| 44 | metric = metric_function(*contents) |
||
| 45 | metric = float(metric) # Must be a float |
||
| 46 | |||
| 47 | except: |
||
| 48 | logging.exception("Failed to calculate metric for {}".format(filename)) |
||
| 49 | if debug: raise |
||
| 50 | |||
| 51 | else: |
||
| 52 | |||
| 53 | metrics.append(metric) |
||
| 54 | Lambdas.append(10**log10_Lambda) |
||
| 55 | scale_factors.append(scale_factor) |
||
| 56 | |||
| 57 | metrics = np.array(metrics) |
||
| 58 | Lambdas = np.array(Lambdas) |
||
| 59 | scale_factors = np.array(scale_factors) |
||
| 60 | |||
| 61 | # Make the figure |
||
| 62 | fig, ax = plt.subplots() |
||
| 63 | |||
| 64 | # scale factors are non-linear, so lets show them as indices then we will |
||
| 65 | # adjust the y-ticks and labels as necessary |
||
| 66 | unique_scale_factors = list(np.sort(np.unique(scale_factors))) |
||
| 67 | scale_factor_indices \ |
||
| 68 | = np.array([unique_scale_factors.index(_) for _ in scale_factors]) |
||
| 69 | |||
| 70 | # Scale the points so that the best metric has s=250. |
||
| 71 | unity = 250 * min(metrics) |
||
| 72 | scat = ax.scatter(Lambdas, scale_factor_indices, c=metrics, s=unity/metrics, |
||
| 73 | cmap=plt.cm.plasma, vmin=0.04, vmax=0.11, **kwargs) |
||
| 74 | ax.set_yticks(np.arange(len(unique_scale_factors))) |
||
| 75 | ax.set_yticklabels([r"${0:.1f}$".format(_) for _ in unique_scale_factors]) |
||
| 76 | ax.set_ylim(-1, len(unique_scale_factors)) |
||
| 77 | |||
| 78 | # Draw a circle around the best three. |
||
| 79 | #for index, color in zip(np.argsort(metrics), ("k", )):#"#AAAAAA", "#BBBBBB", "#CCCCCC", "#DDDDDD")): |
||
| 80 | # ax.scatter([Lambdas[index]], [scale_factor_indices[index]], |
||
| 81 | # s=450, edgecolor=color, facecolor="w", zorder=-1, linewidths=2) |
||
| 82 | |||
| 83 | |||
| 84 | ax.semilogx() |
||
| 85 | |||
| 86 | for _ in np.arange(len(unique_scale_factors) - 1): |
||
| 87 | ax.axhline(_ + 0.5, c="#EEEEEE", zorder=-1) |
||
| 88 | |||
| 89 | cbar = plt.colorbar(scat) |
||
| 90 | cbar.set_label(metric_label or r"Metric") |
||
| 91 | cbar.set_ticks([0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10, 0.11]) |
||
| 92 | |||
| 93 | ax.set_xlabel(r"$\rm{Regularization},$ $\Lambda$") |
||
| 94 | ax.set_ylabel(r"$\rm{Scale}$ $\rm{factor},$ $f$") |
||
| 95 | ax.yaxis.set_tick_params(width=0) |
||
| 96 | |||
| 97 | fig.tight_layout() |
||
| 98 | return fig |
||
| 99 | |||
| 163 | raise a |