Beispiel #1
0
    def testSerializationLinearBoudaryWithLeaf(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid

        srcLeaf = []
        factory = Grid.createLinearBoundaryGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        for i in xrange(factory.getStorage().size()):
            srcLeaf.append(factory.getStorage().get(i).isLeaf())

        str = factory.serialize()
        self.assert_(len(str) > 0)

        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)

        self.assertEqual(factory.getStorage().size(),
                         newfac.getStorage().size())

        for i in xrange(factory.getStorage().size()):
            self.failUnlessEqual(newfac.getStorage().get(i).isLeaf(),
                                 srcLeaf[i])
    def testSerializationLinearBoudaryBoundingBox(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid
        
        factory = Grid.createLinearBoundaryGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        boundingBox = factory.getBoundingBox()
        tempBound = boundingBox.getBoundary(0)
        tempBound.leftBoundary = 0.0
        tempBound.rightBoundary = 100.0
        tempBound.bDirichletLeft = False;
        tempBound.bDirichletRight = False;
        boundingBox.setBoundary(0, tempBound)

        str = factory.serialize()
        self.assert_(len(str) > 0)
        
        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)
        
        self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
            
        boundingBox = newfac.getBoundingBox()
        tempBound = boundingBox.getBoundary(0)
        self.assertEqual(0.0, tempBound.leftBoundary)
        self.assertEqual(100.0, tempBound.rightBoundary)
        self.assertEqual(False, tempBound.bDirichletLeft)
        self.assertEqual(False, tempBound.bDirichletRight)
Beispiel #3
0
    def testSerializationLinearBoudaryBoundingBox(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid

        factory = Grid.createLinearBoundaryGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        boundingBox = factory.getBoundingBox()
        tempBound = boundingBox.getBoundary(0)
        tempBound.leftBoundary = 0.0
        tempBound.rightBoundary = 100.0
        tempBound.bDirichletLeft = False
        tempBound.bDirichletRight = False
        boundingBox.setBoundary(0, tempBound)

        str = factory.serialize()
        self.assert_(len(str) > 0)

        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)

        self.assertEqual(factory.getStorage().size(),
                         newfac.getStorage().size())

        boundingBox = newfac.getBoundingBox()
        tempBound = boundingBox.getBoundary(0)
        self.assertEqual(0.0, tempBound.leftBoundary)
        self.assertEqual(100.0, tempBound.rightBoundary)
        self.assertEqual(False, tempBound.bDirichletLeft)
        self.assertEqual(False, tempBound.bDirichletRight)
Beispiel #4
0
    def fromJson(cls, jsonObject):
        """
        Restores the Beta object from the json object with its
        attributes.

        Arguments:
        jsonObject -- json object

        Return the restored UQSetting object
        """
        # restore surplusses
        key = '_SGDEdist__grid'
        if key in jsonObject:
            # undo the hack that made it json compatible
            gridString = jsonObject[key].replace('__', '\n').encode('utf8')
            # deserialize ...
            grid = Grid.unserialize(gridString)
        else:
            raise AttributeError("SGDEDist: fromJson - grid is missing")

        key = '_SGDEdist__alpha'
        if key in jsonObject:
            alpha = np.array(jsonObject[key])
        else:
            raise AttributeError(
                "SGDEDist: fromJson - coefficients are missing")

        key = '_SGDEdist__trainData'
        trainData = None
        if key in jsonObject:
            trainData = np.array(jsonObject[key])

        key = '_SGDEdist__bounds'
        bounds = None
        if key in jsonObject:
            bounds = np.array(jsonObject[key])

        key = '_SGDEdist__config'
        config = None
        if key in jsonObject:
            config = jsonObject[key]

        key = '_SGDEdist__unitIntegrand'
        unitIntegrand = True
        if key in jsonObject:
            unitIntegrand = bool(jsonObject[key])

        return SGDEdist(grid,
                        alpha,
                        trainData=trainData,
                        bounds=bounds,
                        config=config,
                        learner=None,
                        unitIntegrand=unitIntegrand)
    def fromJson(cls, jsonObject):
        """
        Restores the ASGC object from the json object with its
        attributes.
        @param jsonObject: json object
        @return: the restored ASGC object
        """
        knowledge = ASGCKnowledge()

        # restore iteration
        key = '_ASGCKnowledge__iteration'
        if key in jsonObject:
            knowledge.setIteration(int(jsonObject[key]))

        # restore surpluses: {iteration: {qoi: {dtype: {t: <Grid>}}}}
        key = '_ASGCKnowledge__grids'
        if key in jsonObject:
            grids = {}
            for iteration, v1 in jsonObject[key].items():
                d1 = {}
                for qoi, gridString in v1.items():
                    # undo the hack that made it json compatible
                    gridString = gridString.replace('__', '\n')\
                                           .encode('utf8')
                    # compatibility with older poly basis type
                    gridString = gridString.replace('myPoly', 'poly')
                    # deserialize ...
                    grid = Grid.unserialize(gridString)
                    # ... and store it
                    d1[qoi] = grid
                grids[int(iteration)] = d1
            knowledge.setGrids(grids)

        # restore surpluses: {iteration: {qoi: {dtype: {t: <list float>}}}}
        key = '_ASGCKnowledge__alphas'
        if key in jsonObject:
            alphas = {}
            for iteration, v1 in jsonObject[key].items():
                d1 = {}
                for qoi, v2 in v1.items():
                    d2 = {}
                    for dtype, v3 in v2.items():
                        d3 = {}
                        for t, alpha in v3.items():
                            d3[float(t)] = DataVector(alpha)
                        d2[int(dtype)] = d3
                    d1[qoi] = d2
                alphas[int(iteration)] = d1
            knowledge.setAlphas(alphas)

        return knowledge
Beispiel #6
0
    def fromJson(cls, jsonObject):
        """
        Restores the ASGC object from the json object with its
        attributes.
        @param jsonObject: json object
        @return: the restored ASGC object
        """
        knowledge = ASGCKnowledge()

        # restore iteration
        key = '_ASGCKnowledge__iteration'
        if key in jsonObject:
            knowledge.setIteration(int(jsonObject[key]))

        # restore surpluses: {iteration: {qoi: {dtype: {t: <Grid>}}}}
        key = '_ASGCKnowledge__grids'
        if key in jsonObject:
            grids = {}
            for iteration, v1 in jsonObject[key].items():
                d1 = {}
                for qoi, gridString in v1.items():
                    # undo the hack that made it json compatible
                    gridString = gridString.replace('__', '\n')\
                                           .encode('utf8')
                    # compatibility with older poly basis type
                    gridString = gridString.replace('myPoly', 'poly')
                    # deserialize ...
                    grid = Grid.unserialize(gridString)
                    # ... and store it
                    d1[qoi] = grid
                grids[int(iteration)] = d1
            knowledge.setGrids(grids)

        # restore surpluses: {iteration: {qoi: {dtype: {t: <list float>}}}}
        key = '_ASGCKnowledge__alphas'
        if key in jsonObject:
            alphas = {}
            for iteration, v1 in jsonObject[key].items():
                d1 = {}
                for qoi, v2 in v1.items():
                    d2 = {}
                    for dtype, v3 in v2.items():
                        d3 = {}
                        for t, alpha in v3.items():
                            d3[float(t)] = DataVector(alpha)
                        d2[int(dtype)] = d3
                    d1[qoi] = d2
                alphas[int(iteration)] = d1
            knowledge.setAlphas(alphas)

        return knowledge
    def testSerializationLinearBoudary(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid
        
        factory = Grid.createLinearBoundaryGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        str = factory.serialize()
        self.assert_(len(str) > 0)
        
        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)
        
        self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())        
Beispiel #8
0
    def testSerializationLinearBoudary(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid

        factory = Grid.createLinearBoundaryGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        str = factory.serialize()
        self.assert_(len(str) > 0)

        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)

        self.assertEqual(factory.getStorage().size(),
                         newfac.getStorage().size())
    def testSerializationLinearBoudaryWithLeaf(self):
        """Uses Linear grid for tests"""
        from pysgpp import Grid
        
        srcLeaf = []
        factory = Grid.createLinearBoundaryGrid(2)
        self.failIfEqual(factory, None)

        gen = factory.createGridGenerator()
        gen.regular(3)

        for i in xrange(factory.getStorage().size()):
            srcLeaf.append(factory.getStorage().get(i).isLeaf())

        str = factory.serialize()
        self.assert_(len(str) > 0)
        
        newfac = Grid.unserialize(str)
        self.failIfEqual(newfac, None)
        
        self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
        
        for i in xrange(factory.getStorage().size()):
            self.failUnlessEqual(newfac.getStorage().get(i).isLeaf(), srcLeaf[i])