Exemple #1
0
    def test_UnitNormMapSimplify(self):
        """Test advanced simplification of UnitNormMap

        Basic simplification is tested elsewhere.

        ShiftMap              + UnitNormMap(forward) = UnitNormMap(forward)
        UnitNormMap(inverted) + ShiftMap             = UnitNormMap(inverted)
        UnitNormMap(forward)  + non-equal UnitNormMap(inverted) = ShiftMap
        """
        center1 = [2, -1, 0]
        center2 = [-1, 6, 4]
        shift = [3, 7, -9]
        # an array of points, each of 4 axes, the max we'll need
        testpoints = np.array([
            [1.0, 2.0, -6.0, 30.0, 1.0],
            [3.0, 99.0, -5.0, 21.0, 0.0],
            [-5.0, 3.0, -7.0, 37.0, 0.0],
            [7.0, -23.0, -3.0, 45.0, 0.0],
        ],
                              dtype=float)
        unm1 = ast.UnitNormMap(center1)
        unm1inv = unm1.inverted()
        unm2 = ast.UnitNormMap(center2)
        unm2inv = unm2.inverted()
        shiftmap = ast.ShiftMap(shift)
        winmap_unitscale = ast.WinMap(np.zeros(3), shift, np.ones(3),
                                      np.ones(3) + shift)
        winmap_notunitscale = ast.WinMap(np.zeros(3), shift, np.ones(3),
                                         np.ones(3) * 2 + shift)

        if ast.astVersion() >= 9001003:
            expected_map = "ShiftMap"  # ShiftMap is ShiftMap in 9.1.3
        else:
            expected_map = "WinMap"  # ShiftMap gets simplified to WinMap

        for map1, map2, pred_simplified_class_name in (
            (unm1, unm2inv, expected_map),
            (shiftmap, unm1, "UnitNormMap"),
            (winmap_unitscale, unm1, "UnitNormMap"),
            (winmap_notunitscale, unm1, "SeriesMap"),
            (unm1inv, shiftmap, "UnitNormMap"),
            (unm1inv, winmap_unitscale, "UnitNormMap"),
            (unm1inv, winmap_notunitscale, "SeriesMap"),
        ):
            cmpmap = map1.then(map2)
            self.assertEqual(map1.nIn, cmpmap.nIn)
            self.assertEqual(map2.nOut, cmpmap.nOut)
            cmpmap_simp = cmpmap.simplified()
            self.assertEqual(cmpmap_simp.className, pred_simplified_class_name)
            self.assertEqual(cmpmap.nIn, cmpmap_simp.nIn)
            self.assertEqual(cmpmap.nOut, cmpmap_simp.nOut)
            testptview = np.array(testpoints[0:cmpmap.nIn])
            assert_allclose(cmpmap.applyForward(testptview),
                            cmpmap_simp.applyForward(testptview))
    def test_MapBox(self):
        """Test MapBox for the simple case of a shift and zoom"""
        shift = np.array([1.5, 0.5])
        zoom = np.array([2.0, 3.0])
        winmap = ast.WinMap([0, 0], [1, 1], zoom * [0, 0] + shift,
                            zoom * [1, 1] + shift)
        # arbitrary values chosen so that inbnd_a is NOT < inbnd_b for both axes because
        # MapBox uses the minimum of inbnd_b, inbnd_a for each axis for the lower bound,
        # and the maximum for the upper bound
        inbnd_a = np.array([-1.2, 3.3])
        inbnd_b = np.array([2.7, 2.2])
        mapbox = ast.MapBox(winmap, inbnd_a, inbnd_b)

        lbndin = np.minimum(inbnd_a, inbnd_b)
        ubndin = np.maximum(inbnd_a, inbnd_b)
        predlbndOut = lbndin * zoom + shift
        predubndOut = ubndin * zoom + shift
        assert_allclose(mapbox.lbndOut, predlbndOut)
        assert_allclose(mapbox.ubndOut, predubndOut)

        # note that mapbox.xl and xu is only partially predictable
        # because any X from the input gives the same Y
        for i in range(2):
            self.assertAlmostEqual(mapbox.xl[i, i], lbndin[i])
            self.assertAlmostEqual(mapbox.xu[i, i], ubndin[i])

        # confirm that order of inbnd_a, inbnd_b doesn't matter
        mapbox2 = ast.MapBox(winmap, inbnd_b, inbnd_a)
        assert_allclose(mapbox2.lbndOut, mapbox.lbndOut)
        assert_allclose(mapbox2.ubndOut, mapbox.ubndOut)

        # the xl and xu need only agree on the diagonal, as above
        for i in range(2):
            self.assertAlmostEqual(mapbox.xl[i, i], mapbox2.xl[i, i])
            self.assertAlmostEqual(mapbox.xu[i, i], mapbox2.xu[i, i])
    def test_WinMap(self):
        # a map describing a shift of [1.0, -0.5] followed by a zoom of 2
        winmap = ast.WinMap([0, 0], [1, 1], [1, -0.5], [3, 1.5])
        pred_shift = [1.0, -0.5]
        pred_zoom = 2.0
        self.assertIsInstance(winmap, ast.WinMap)
        self.assertEqual(winmap.nIn, 2)
        self.assertEqual(winmap.nOut, 2)

        self.checkBasicSimplify(winmap)
        self.checkCopy(winmap)

        indata = np.array([
            [0.0, 0.5, 1.0],
            [-3.0, 1.5, 0.13],
        ], dtype=float)
        pred_outdata = (indata.T * pred_zoom + pred_shift).T
        outdata = winmap.applyForward(indata)
        assert_allclose(outdata, pred_outdata)

        self.checkRoundTrip(winmap, indata)
        self.checkMappingPersistence(winmap, indata)