Conditions | 28 |
Total Lines | 71 |
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 Orange.Model.__call__() 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 | import inspect |
||
108 | def __call__(self, data, ret=Value): |
||
109 | if not 0 <= ret <= 2: |
||
110 | raise ValueError("invalid value of argument 'ret'") |
||
111 | if (ret > 0 |
||
112 | and any(v.is_continuous for v in self.domain.class_vars)): |
||
113 | raise ValueError("cannot predict continuous distributions") |
||
114 | |||
115 | # Call the predictor |
||
116 | if isinstance(data, np.ndarray): |
||
117 | prediction = self.predict(np.atleast_2d(data)) |
||
118 | elif isinstance(data, scipy.sparse.csr.csr_matrix): |
||
119 | prediction = self.predict(data) |
||
120 | elif isinstance(data, Instance): |
||
121 | if data.domain != self.domain: |
||
122 | data = Instance(self.domain, data) |
||
123 | data = Table(data.domain, [data]) |
||
124 | prediction = self.predict_storage(data) |
||
125 | elif isinstance(data, Table): |
||
126 | if data.domain != self.domain: |
||
127 | data = data.from_table(self.domain, data) |
||
128 | prediction = self.predict_storage(data) |
||
129 | elif isinstance(data, (list, tuple)): |
||
130 | if not isinstance(data[0], (list, tuple)): |
||
131 | data = [ data ] |
||
132 | data = Table(self.original_domain, data) |
||
133 | data = Table(self.domain, data) |
||
134 | prediction = self.predict_storage(data) |
||
135 | else: |
||
136 | raise TypeError("Unrecognized argument (instance of '{}')".format( |
||
137 | type(data).__name__)) |
||
138 | |||
139 | # Parse the result into value and probs |
||
140 | multitarget = len(self.domain.class_vars) > 1 |
||
141 | if isinstance(prediction, tuple): |
||
142 | value, probs = prediction |
||
143 | elif prediction.ndim == 1 + multitarget: |
||
144 | value, probs = prediction, None |
||
145 | elif prediction.ndim == 2 + multitarget: |
||
146 | value, probs = None, prediction |
||
147 | else: |
||
148 | raise TypeError("model returned a %i-dimensional array", |
||
149 | prediction.ndim) |
||
150 | |||
151 | # Ensure that we have what we need to return |
||
152 | if ret != Model.Probs and value is None: |
||
153 | value = np.argmax(probs, axis=-1) |
||
154 | if ret != Model.Value and probs is None: |
||
155 | if multitarget: |
||
156 | max_card = max(len(c.values) |
||
157 | for c in self.domain.class_vars) |
||
158 | probs = np.zeros(value.shape + (max_card,), float) |
||
159 | for i, cvar in enumerate(self.domain.class_vars): |
||
160 | probs[:, i, :], _ = bn.bincount(np.atleast_2d(value[:, i]), |
||
161 | max_card - 1) |
||
162 | else: |
||
163 | probs, _ = bn.bincount(np.atleast_2d(value), |
||
164 | len(self.domain.class_var.values) - 1) |
||
165 | if ret == Model.ValueProbs: |
||
166 | return value, probs |
||
167 | else: |
||
168 | return probs |
||
169 | |||
170 | # Return what we need to |
||
171 | if ret == Model.Probs: |
||
172 | return probs |
||
173 | if isinstance(data, Instance) and not multitarget: |
||
174 | value = Value(self.domain.class_var, value[0]) |
||
175 | if ret == Model.Value: |
||
176 | return value |
||
177 | else: # ret == Model.ValueProbs |
||
178 | return value, probs |
||
179 | |||
264 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.