Conditions | 25 |
Total Lines | 106 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 plot_first_order_derivatives() 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 | |||
63 | def plot_first_order_derivatives(model, label_names=None, scaled=True, |
||
64 | show_clipped_region=False, colors=None, zorders=None, |
||
65 | clip_less_than=None, label_wavelengths=None, latex_label_names=None, |
||
66 | wavelength_regions=None, show_legend=True, **kwargs): |
||
67 | |||
68 | if wavelength_regions is None: |
||
69 | wavelength_regions = [(model.dispersion[0], model.dispersion[-1])] |
||
70 | |||
71 | if label_names is None: |
||
72 | label_names = model.vectorizer.label_names |
||
73 | |||
74 | if latex_label_names is None: |
||
75 | latex_label_names = {} |
||
76 | |||
77 | fig, axes = plt.subplots(1, len(wavelength_regions), figsize=(15, 3.5)) |
||
78 | |||
79 | if len(label_names) > 1: |
||
80 | #cmap = cm.LinearSegmentedColormap.from_list( |
||
81 | # "inferno", cmaps._inferno_data, len(label_names)) |
||
82 | cmap = plt.cm.get_cmap("Set1", len(label_names)) |
||
83 | else: |
||
84 | cmap = lambda x: "k" |
||
85 | |||
86 | if colors is not None: |
||
87 | cmap = lambda x: colors[x % len(colors)] |
||
88 | |||
89 | axes = np.array(axes).flatten() |
||
90 | |||
91 | scales = [] |
||
92 | for i, label_name in enumerate(label_names): |
||
93 | |||
94 | # First order derivatives are always indexed first. |
||
95 | index = 1 + model.vectorizer.label_names.index(label_name) |
||
96 | y = model.theta[:, index] |
||
97 | #y = y |
||
98 | if clip_less_than is not None: |
||
99 | y[np.abs(y) < clip_less_than] = 0 |
||
100 | |||
101 | scale = np.nanmax(np.abs(y)) if scaled else 1. |
||
102 | y = y / scale |
||
103 | |||
104 | c = cmap(i) |
||
105 | zorder = 1 |
||
106 | if zorders is not None: zorder = zorders[i] |
||
107 | for ax in axes: |
||
108 | ax.plot(model.dispersion, y, c=c, zorder=zorder, |
||
109 | label=latex_label_names.get(label_name, label_name)) |
||
110 | |||
111 | if clip_less_than is not None and show_clipped_region: |
||
112 | ax.axhspan(-clip_less_than/scale, +clip_less_than/scale, |
||
113 | xmin=-1, xmax=+2, |
||
114 | facecolor=c, edgecolor=c, zorder=-100, alpha=0.1) |
||
115 | |||
116 | |||
117 | # Plot any wavelengths. |
||
118 | if label_wavelengths is not None: |
||
119 | label_yvalue = 1.0 |
||
120 | for label_name, wavelengths in label_wavelengths.items(): |
||
121 | try: |
||
122 | color = cmap(label_names.index(label_name)) |
||
123 | label = None |
||
124 | except (IndexError, ValueError): |
||
125 | color = 'k' |
||
126 | #for ax in axes: |
||
127 | # ax.plot([model.dispersion[0] - 1], [0], c=color, label=latex_label_names.get(label_name, label_name)) |
||
128 | |||
129 | for ax in axes: |
||
130 | ax.plot(wavelengths, label_yvalue * np.ones_like(wavelengths), |
||
131 | "|", markersize=20, markeredgewidth=2, c=color) |
||
132 | |||
133 | for ax, wavelength_region in zip(axes, wavelength_regions): |
||
134 | ax.set_xlim(wavelength_region) |
||
135 | if scaled: |
||
136 | ax.set_ylim(-1.2, 1.2) |
||
137 | |||
138 | if ax.is_first_col(): |
||
139 | if scaled: |
||
140 | ax.set_ylabel(r"$\theta/{\max|\theta|}$") |
||
141 | else: |
||
142 | ax.set_ylabel(r"$\theta$") |
||
143 | else: |
||
144 | ax.set_yticklabels([]) |
||
145 | |||
146 | ax.xaxis.set_major_locator(MaxNLocator(4)) |
||
147 | |||
148 | xlabel = r"$\lambda$ $({\rm\AA})$" |
||
149 | if len(wavelength_regions) == 1: |
||
150 | ax.set_xlabel(xlabel) |
||
151 | |||
152 | else: |
||
153 | _show_xlim_changes(fig) |
||
154 | |||
155 | ax = fig.add_axes([0, 0, 1, 1]) |
||
156 | ax.set_axis_off() |
||
157 | ax.set_xlim(0, 1) |
||
158 | ax.set_ylim(0, 1) |
||
159 | ax.text(0.5, 0.05, xlabel, rotation='horizontal', |
||
160 | horizontalalignment='center', verticalalignment='center') |
||
161 | |||
162 | fig.tight_layout() |
||
163 | |||
164 | if show_legend: |
||
165 | axes[0].legend(loc="upper right", ncol=kwargs.get("legend_ncol", len(label_names) % 7), frameon=False) |
||
166 | fig.subplots_adjust(wspace=0.01, bottom=0.20) |
||
167 | |||
168 | return fig |
||
169 | |||
261 |