示例#1
0
    def testId(self):
        """Test Dataset.idhash() if it gets changed if any of the
        labels/chunks changes
        """

        dataset = Dataset(samples=N.arange(12).reshape((4, 3)), labels=1, chunks=1)
        origid = dataset.idhash
        dataset.labels = [3, 1, 2, 3]  # change all labels
        self.failUnless(origid != dataset.idhash, msg="Changing all labels should alter dataset's idhash")

        origid = dataset.idhash

        z = dataset.labels[1]
        self.failUnlessEqual(origid, dataset.idhash, msg="Accessing shouldn't change idhash")
        z = dataset.chunks
        self.failUnlessEqual(origid, dataset.idhash, msg="Accessing shouldn't change idhash")
        z[2] = 333
        self.failUnless(origid != dataset.idhash, msg="Changing value in attribute should change idhash")

        origid = dataset.idhash
        dataset.samples[1, 1] = 1000
        self.failUnless(origid != dataset.idhash, msg="Changing value in data should change idhash")

        origid = dataset.idhash
        dataset.permuteLabels(True)
        self.failUnless(origid != dataset.idhash, msg="Permutation also changes idhash")

        dataset.permuteLabels(False)
        self.failUnless(origid == dataset.idhash, msg="idhash should be restored after " "permuteLabels(False)")
示例#2
0
    def testLabelRandomizationAndSampling(self):
        """
        """
        data = Dataset(samples=N.ones((5, 1)), labels=range(5), chunks=1)
        data += Dataset(samples=N.ones((5, 1)) + 1, labels=range(5), chunks=2)
        data += Dataset(samples=N.ones((5, 1)) + 2, labels=range(5), chunks=3)
        data += Dataset(samples=N.ones((5, 1)) + 3, labels=range(5), chunks=4)
        data += Dataset(samples=N.ones((5, 1)) + 4, labels=range(5), chunks=5)
        self.failUnless(data.samplesperlabel == {0: 5, 1: 5, 2: 5, 3: 5, 4: 5})

        sample = data.getRandomSamples(2)
        self.failUnless(sample.samplesperlabel.values() == [2, 2, 2, 2, 2])

        self.failUnless((data.uniquechunks == range(1, 6)).all())

        # store the old labels
        origlabels = data.labels.copy()

        data.permuteLabels(True)

        self.failIf((data.labels == origlabels).all())

        data.permuteLabels(False)

        self.failUnless((data.labels == origlabels).all())

        # now try another object with the same data
        data2 = Dataset(samples=data.samples, labels=data.labels, chunks=data.chunks)

        # labels are the same as the originals
        self.failUnless((data2.labels == origlabels).all())

        # now permute in the new object
        data2.permuteLabels(True)

        # must not affect the old one
        self.failUnless((data.labels == origlabels).all())
        # but only the new one
        self.failIf((data2.labels == origlabels).all())