Changeset 10803:921898cb37bd in orange


Ignore:
Timestamp:
04/18/12 12:21:57 (2 years ago)
Author:
anze <anze.staric@…>
Branch:
default
Message:

BUG Fixed pickling of pca projector.

Location:
Orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Orange/projection/linear.py

    r10715 r10803  
    12291229        for i in range(len(self.projection)): 
    12301230            f = feature.Continuous("Comp.%d" % (i + 1)) 
    1231             f.get_value_from = lambda ex, w: self._project_single(ex, w, f, i) 
     1231            f.get_value_from = _ProjectSingleComponent(self, f, i) 
    12321232            features.append(f) 
    12331233 
     
    12351235                                                self.input_domain.class_var, 
    12361236                                                class_vars=self.input_domain.class_vars) 
    1237  
    1238     def _project_single(self, example, return_what, new_feature, feature_idx): 
    1239         ex = Orange.data.Table([example]).to_numpy("a")[0] 
    1240         ex -= self.center 
    1241         if self.standardize: 
    1242             ex /= self.scale 
    1243         return new_feature(numpy.dot(self.projection[feature_idx, :], ex.T)[0]) 
    12441237 
    12451238    def __call__(self, dataset): 
     
    12731266        class_, classes = dataset.to_numpy("c")[0], dataset.to_numpy("m")[0] 
    12741267        return data.Table(self.output_domain, numpy.hstack((self.A, class_, classes))) 
     1268 
     1269class _ProjectSingleComponent(): 
     1270    def __init__(self, projector, feature, idx): 
     1271        self.projector = projector 
     1272        self.feature = feature 
     1273        self.idx = idx 
     1274 
     1275    def __call__(self, example, return_what): 
     1276        ex = Orange.data.Table([example]).to_numpy("a")[0] 
     1277        ex -= self.projector.center 
     1278        if self.projector.standardize: 
     1279            ex /= self.projector.scale 
     1280        return self.feature(numpy.dot(self.projector.projection[self.idx, :], ex.T)[0]) 
    12751281 
    12761282 
  • Orange/testing/unit/tests/test_projection_linear.py

    r10710 r10803  
    55 
    66import numpy as np 
    7 import random 
     7import pickle, random 
    88 
    99from Orange import data, feature 
    1010from Orange.projection import linear 
    1111 
     12np.random.seed(0) 
    1213random.seed(0) 
    1314 
     
    211212        projector(new_examples) 
    212213 
     214    def test_can_pickle_and_unpickle(self): 
     215        self.create_normal_dataset() 
     216        projector = linear.PCA(variance_covered=.99)(self.dataset) 
     217 
     218        pickled = pickle.dumps(projector) 
     219        restored = pickle.loads(pickled) 
     220 
     221        self.assertFalse((projector.projection - restored.projection).any()) 
     222        self.assertFalse((projector.center - restored.center).any()) 
     223        self.assertFalse((projector.scale - restored.scale).any()) 
     224 
     225        transformed, new_transformed = projector(self.dataset), restored(self.dataset) 
     226        print transformed[0][0] 
     227        for ex1, ex2 in zip(transformed, new_transformed): 
     228            for v1, v2 in zip(ex1, ex2): 
     229                self.assertEqual(v1, v2) 
     230 
    213231 
    214232class TestFda(unittest.TestCase): 
Note: See TracChangeset for help on using the changeset viewer.