Changeset 10806:eb51260debca in orange for Orange/OrangeWidgets/Prototypes/OWPCA.py
 Timestamp:
 04/18/12 12:42:10 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/OrangeWidgets/Prototypes/OWPCA.py
r10805 r10806 113 113 ##### 114 114 grid = QGridLayout() 115 box = OWGUI.widgetBox(self.controlArea, " Settings",115 box = OWGUI.widgetBox(self.controlArea, "Components Selection", 116 116 orientation=grid) 117 117 … … 186 186 self.variances_sum = None 187 187 self.projector_full = None 188 self.currently_selected = 0 188 189 189 190 self.resize(800, 400) … … 205 206 self.variances_cumsum = None 206 207 self.projector_full = None 208 self.currently_selected = 0 207 209 208 210 def set_data(self, data=None): … … 213 215 self.data = data 214 216 self.on_change() 217 else: 218 self.send("Transformed Data", None) 219 self.send("Eigen Vectors", None) 215 220 216 221 def on_change(self): 222 """Data has changed and we need to recompute the projection. 223 """ 217 224 if self.data is None: 218 225 return … … 221 228 222 229 def on_update(self): 230 """Component selection was changed by the user. 231 """ 223 232 if self.data is None: 224 233 return 225 234 self.update_cutoff_curve() 226 self.update_components() 235 if self.currently_selected != self.number_of_selected_components(): 236 self.update_components() 227 237 228 238 def construct_pca_all_comp(self): … … 245 255 246 256 def apply(self): 247 """Apply PCA in input data, caching the full projection,257 """Apply PCA on input data, caching the full projection, 248 258 then updating the selected components. 249 259 … … 258 268 self.max_components_spin.setRange(1, len(self.variances)) 259 269 self.update_scree_plot() 270 self.update_cutoff_curve() 260 271 self.update_components() 261 272 262 273 def update_components(self): 274 """Update the output components. 275 """ 263 276 scale = self.projector_full.scale 264 277 center = self.projector_full.center … … 283 296 eigenvectors = self.eigenvectors_as_table(components) 284 297 298 self.currently_selected = self.number_of_selected_components() 299 285 300 self.send("Transformed Data", projected_data) 286 301 self.send("Eigen Vectors", eigenvectors) … … 293 308 294 309 def update_scree_plot(self): 295 variances = self.projector_full.variances 296 s = np.sum(variances) 297 cv = variances / s 298 cs = np.cumsum(cv) 299 x_space = np.arange(0, len(variances)) 310 x_space = np.arange(0, len(self.variances)) 300 311 self.scree_plot.set_axis_enabled(owaxis.xBottom, True) 301 312 self.scree_plot.set_axis_enabled(owaxis.yLeft, True) … … 303 314 ["PC" + str(i + 1) for i in x_space]) 304 315 305 self.variance_curve.set_data(x_space, cv)306 self.cumulative_variance_curve.set_data(x_space, cs)316 self.variance_curve.set_data(x_space, self.variances) 317 self.cumulative_variance_curve.set_data(x_space, self.variances_cumsum) 307 318 self.variance_curve.setVisible(True) 308 319 self.cumulative_variance_curve.setVisible(True) … … 311 322 312 323 def on_cutoff_moved(self, value): 324 """Cutoff curve was moved by the user. 325 """ 313 326 components = int(np.floor(value)) + 1 314 if components != self.max_components: 315 self.max_components = int(np.floor(value)) + 1 316 self.variance_covered = self.variances_cumsum[self.max_components  1] * 100 327 # Did the number of components actually change 328 self.max_components = components 329 self.variance_covered = self.variances_cumsum[components  1] * 100 330 if self.currently_selected != self.number_of_selected_components(): 331 # self.max_components = int(np.floor(value)) + 1 332 # self.variance_covered = self.variances_cumsum[self.max_components  1] * 100 317 333 self.update_components() 318 334 319 335 def update_cutoff_curve(self): 320 """Update cutoff line from gui control elements.336 """Update cutoff curve from 'Components Selection' control box. 321 337 """ 322 338 variance = self.variances_cumsum[self.max_components  1] * 100.0 … … 328 344 self.scree_plot.set_cutoff_value(cutoff + 0.5) 329 345 346 def number_of_selected_components(self): 347 """How many components are selected. 348 """ 349 if self.data is None: 350 return 0 351 352 variance_components = np.searchsorted(self.variances_cumsum, 353 self.variance_covered / 100.0) 354 return min(variance_components + 1, self.max_components) 330 355 331 356 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.