Conditions | 18 |
Total Lines | 83 |
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 AnniesLasso.parse_label_vector() 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 | #!/usr/bin/env python |
||
62 | def parse_label_vector(label_vector_description, columns=None, **kwargs): |
||
63 | """ |
||
64 | Return a structured form of a label vector from unstructured, |
||
65 | human-readable input. |
||
66 | |||
67 | :param label_vector_description: |
||
68 | A human-readable or structured form of a label vector. |
||
69 | |||
70 | :type label_vector_description: |
||
71 | str or list |
||
72 | |||
73 | :param columns: [optional] |
||
74 | If `columns` are provided, instead of text columns being provided as the |
||
75 | output parameter, the corresponding index location in `column` will be |
||
76 | given. |
||
77 | |||
78 | :returns: |
||
79 | A structured form of the label vector as a multi-level list. |
||
80 | |||
81 | |||
82 | :Example: |
||
83 | |||
84 | >>> parse_label_vector("Teff^4 + logg*Teff^3 + feh + feh^0*Teff") |
||
85 | [ |
||
86 | [ |
||
87 | ("Teff", 4), |
||
88 | ], |
||
89 | [ |
||
90 | ("logg", 1), |
||
91 | ("Teff", 3) |
||
92 | ], |
||
93 | [ |
||
94 | ("feh", 1), |
||
95 | ], |
||
96 | [ |
||
97 | ("feh", 0), |
||
98 | ("Teff", 1) |
||
99 | ] |
||
100 | ] |
||
101 | """ |
||
102 | |||
103 | if is_structured_label_vector(label_vector_description): |
||
104 | return label_vector_description |
||
105 | |||
106 | # Allow for custom characters, but don't advertise it. |
||
107 | # (Astronomers have bad enough habits already.) |
||
108 | kwds = dict(zip(("sep", "mul", "pow"), "+*^")) |
||
109 | kwds.update(kwargs) |
||
110 | sep, mul, pow = (kwds[k] for k in ("sep", "mul", "pow")) |
||
111 | |||
112 | if isinstance(label_vector_description, string_types): |
||
113 | label_vector_description = label_vector_description.split(sep) |
||
114 | label_vector_description = map(str.strip, label_vector_description) |
||
115 | |||
116 | # Functions to parse the parameter (or index) and order for each term. |
||
117 | get_power = lambda t: float(t.split(pow)[1].strip()) if pow in t else 1 |
||
118 | if columns is None: |
||
119 | get_label = lambda d: d.split(pow)[0].strip() |
||
120 | else: |
||
121 | get_label = lambda d: list(columns).index(d.split(pow)[0].strip()) |
||
122 | |||
123 | label_vector = [] |
||
124 | for descriptor in (item.split(mul) for item in label_vector_description): |
||
125 | |||
126 | labels = map(get_label, descriptor) |
||
127 | orders = map(get_power, descriptor) |
||
128 | |||
129 | term = OrderedDict() |
||
130 | for label, order in zip(labels, orders): |
||
131 | term[label] = term.get(label, 0) + order # Sum repeat term powers. |
||
132 | |||
133 | # Prevent uses of x^0 etc clogging up the label vector. |
||
134 | valid_terms = [(l, o) for l, o in term.items() if o != 0] |
||
135 | if not np.all(np.isfinite([o for l, o in valid_terms])): |
||
136 | raise ValueError("non-finite power provided") |
||
137 | |||
138 | if len(valid_terms) > 0: |
||
139 | label_vector.append(valid_terms) |
||
140 | |||
141 | if sum(map(len, label_vector)) == 0: |
||
142 | raise ValueError("no valid terms provided") |
||
143 | |||
144 | return label_vector |
||
145 | |||
258 |