def test_patch_all(self):
        """
		Test complete window patching of a directory
		"""

        fail = False
        try:
            seq = wP.get_patch_sequence(self.source)
            max_err_name, max_err_val = wP.patch_all_windows(
                seq, 'composite.nc', 'patch.log', 1, False)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)

        fail = False
        if ('composite.nc' in os.listdir('./')):
            os.remove('composite.nc')
        else:
            fail = True
        self.assertTrue(not fail)

        fail = False
        if ('patch.log' in os.listdir('./')):
            os.remove('patch.log')
        else:
            fail = True
        self.assertTrue(not fail)
    def test_merge_with_smooth_mom(self):
        """
		Check nn moments merging properly with smoothing
		"""

        seq = wP.get_patch_sequence(self.source)
        wh1 = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 1, True)
        wh2 = wP.window(seq[1][0], seq[1][1], seq[1][2], seq[1][3], 1, True)
        ref_mom = copy.deepcopy(wh2.mom)

        fail = False
        try:
            shift, e2 = wh2.merge(wh1)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)

        # use lower nn_mom outside overlap
        self.assertTrue(
            np.all(np.abs(wh2.mom[:, :17] - wh1.mom[:, :17]) < 1.0e-6))
        # in overlapping region (less offset = 1), average the nn_mom, check that average deviation from the average is "small" (depends on data quality)
        self.assertTrue(
            np.average(
                np.abs(wh2.mom[:, 16 + 1:21 - 1] - wh1.mom[:, 16 + 1:21 - 1]) /
                (0.5 *
                 (wh2.mom[:, 16 + 1:21 - 1] + wh1.mom[:, 16 + 1:21 - 1]))) <
            0.02)
        # above the overlap, just use larger nn_mom
        self.assertTrue(
            np.all(np.abs(wh2.mom[:, 20:] - ref_mom[:, 4:]) < 1.0e-6))
    def test_to_nc(self):
        """
		Check print to netCDF4 function
		"""

        seq = wP.get_patch_sequence(self.source)
        wh1 = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 1, True)
        wh2 = wP.window(seq[1][0], seq[1][1], seq[1][2], seq[1][3], 1, True)

        fail = False
        try:
            shift, e2 = wh2.merge(wh1)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)

        try:
            wh2.to_nc("test.nc")
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)

        fail = False
        if ('test.nc' in os.listdir('./')):
            os.remove('test.nc')
        else:
            fail = True
        self.assertTrue(not fail)
    def test_merge_no_smooth_mom(self):
        """
		Check moments merging properly without smoothing
		"""

        seq = wP.get_patch_sequence(self.source)
        wh1 = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 1, False)
        wh2 = wP.window(seq[1][0], seq[1][1], seq[1][2], seq[1][3], 1, False)
        ref_mom = copy.deepcopy(wh2.mom)

        fail = False
        try:
            shift, e2 = wh2.merge(wh1)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)

        self.assertTrue(wh2.mom.shape == (36 * 3, 31))

        # use lower nn_mom outside overlap
        self.assertTrue(
            np.all(np.abs(wh2.mom[:, :17] - wh1.mom[:, :17]) < 1.0e-6))
        # in overlapping region (less offset = 1), just use lower nn_mom
        self.assertTrue(
            np.all(
                np.abs(wh2.mom[:, 16 + 1:21 - 1] -
                       wh1.mom[:, 16 + 1:21 - 1]) < 1.0e-6))
        # above the overlap, just use larger nn_mom
        self.assertTrue(
            np.all(np.abs(wh2.mom[:, 20:] - ref_mom[:, 4:]) < 1.0e-6))
    def test_merge_with_smooth_lnpi(self):
        """
		Check lnPI merging properly with smoothing
		"""

        seq = wP.get_patch_sequence(self.source)
        wh1 = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 1, True)
        wh2 = wP.window(seq[1][0], seq[1][1], seq[1][2], seq[1][3], 1, True)
        ref_lnpi = copy.deepcopy(wh2.lnPI)

        fail = False
        try:
            shift, e2 = wh2.merge(wh1)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)

        # use lower lnPI outside overlap
        self.assertTrue(np.all(np.abs(wh2.lnPI[:17] - wh1.lnPI[:17]) < 1.0e-6))
        # in overlapping region (less offset = 1), average the lnPI, check that answer is close to average
        self.assertTrue(
            np.all(
                np.abs((wh2.lnPI[16 + 1:21 - 1] - wh1.lnPI[16 + 1:21 - 1]) /
                       (0.5 *
                        (wh2.lnPI[16 + 1:21 - 1] + wh1.lnPI[16 + 1:21 - 1]))) <
                1.0e-3))
        # above the overlap, just use larger lnPI
        self.assertTrue(
            np.all(np.abs(wh2.lnPI[20:] - (ref_lnpi[4:] + shift)) < 1.0e-6))
    def test_repr(self):
        """
		Check self-representation
		"""

        seq = wP.get_patch_sequence(self.source)
        wh = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 2, False)
        a = seq[0][0] + "::" + seq[0][1] + "::" + seq[0][2] + "::" + seq[0][
            3] + "-[0,20]"
        self.assertTrue(a == str(wh))
Exemple #7
0
    def test_nebr_seq_conv(self):
        """
		Check neighbor sequence convergence test
		"""

        seq = wP.get_patch_sequence(self.source)
        match, uerr, nerr = eQ.test_nebr_match(seq[0], seq[1], 1.0)
        self.assertTrue(not match)  # too stringent of convergence criterion
        match, uerr, nerr = eQ.test_nebr_match(seq[0], seq[1], 10.0)
        self.assertTrue(match)  # less stringent of convergence criterion
        self.assertTrue(np.abs(uerr - 4.31410893236) < 1.0e-8)
        self.assertTrue(np.abs(nerr - 8.04638999443) < 1.0e-8)
    def test_clear(self):
        """
		Check ability to clear all data from window
		"""

        seq = wP.get_patch_sequence(self.source)
        wh = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 2, False)
        self.assertTrue(len(wh.lnPI) != 0)
        self.assertTrue(wh.nspec != 0)
        wh.clear()
        self.assertTrue(len(wh.lnPI) == 0)
        self.assertTrue(wh.nspec == 0)
    def test_get_seq(self):
        """
		Check that we can get a sequence from a source tree of varying complexity
		"""

        fail = False
        try:
            seq = wP.get_patch_sequence(self.source)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)
    def test_init(self):
        """
		Initialize a window for patching
		"""

        seq = wP.get_patch_sequence(self.source)
        fail = False
        try:
            wh = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 2,
                           False)
        except Exception as e:
            fail = True
            print e
        self.assertTrue(not fail)
Exemple #11
0
    def test_nebr_equil(self):
        """
		Check neighbor convergence workflow
		"""

        seq = wP.get_patch_sequence(self.source)

        # too conservative, none converged
        fail = False
        try:
            refined_seq = eQ.test_nebr_equil(seq, 1.0, 'maxEq', False)
        except Exception as e:
            fail = True
        self.assertTrue(fail)

        # found counverged, but don't trust last one
        fail = False
        try:
            refined_seq = eQ.test_nebr_equil(seq, 10.0, 'maxEq', False)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)
        self.assertTrue(len(refined_seq) == 1)

        # found converged, and trust last one
        fail = False
        try:
            refined_seq = eQ.test_nebr_equil(seq, 10.0, 'maxEq', True)
        except Exception as e:
            print e
            fail = True
        self.assertTrue(not fail)
        self.assertTrue(len(refined_seq) == 2)

        if ('maxEq' in os.listdir('./')):
            os.remove('maxEq')
    def test_load_info(self):
        """
		Check information loaded correctly at init
		"""

        seq = wP.get_patch_sequence(self.source)
        wh = wP.window(seq[0][0], seq[0][1], seq[0][2], seq[0][3], 2, False)
        a = np.array([
            0.0, 11.5792872, 22.55514816, 33.16632265, 43.53878289,
            53.80927566, 63.94826804, 73.97895064, 83.96576198, 93.94840544,
            103.8773032, 113.77306514, 123.71227577, 133.68404802,
            143.69837309, 153.86625598, 164.18813354, 174.70527468,
            185.3787788, 196.24217909, 207.27150728
        ])
        self.assertTrue(len(wh.lnPI) == 21)
        self.assertTrue(np.all(np.abs(a - wh.lnPI) < 1.0e-6))

        self.assertTrue(wh.mom.shape == (36 * 3, 21))

        # check symmetry of moments
        for i in xrange(0, 2):
            for j in xrange(0, 3):
                for k in xrange(0, 2):
                    for m in xrange(0, 3):
                        for p in xrange(0, 3):
                            # N_i^j*N_k^m*U^0 == N_k^m*N_i^j*U^0
                            address1 = p + 3 * m + 3 * 3 * k + 3 * 3 * 2 * j + 3 * 3 * 2 * 3 * i
                            address2 = p + 3 * j + 3 * 3 * i + 3 * 3 * 2 * m + 3 * 3 * 2 * 3 * k
                            self.assertTrue(
                                np.all(wh.mom[address1] == wh.mom[address2]))

                            if (j == m and m == 0 and p == 0):
                                # all raised to 0 power so should be 1
                                self.assertTrue(
                                    np.all(
                                        np.abs(wh.mom[address1] -
                                               np.ones(len(wh.mom[address1])))
                                        < 1.0e-8))

                            if (i == k and m + j < 3):
                                # e.g., N_1*N_1*U^p = N_1^2*N_1^0*U^p, also true that N_1*N_1*U^p = N_1^2*N_x^0*U^p
                                for kk in xrange(0, 2):
                                    address2 = p + 3 * 0 + 3 * 3 * kk + 3 * 3 * 2 * (
                                        j + m) + 3 * 3 * 2 * 3 * i
                                    self.assertTrue(
                                        np.all(wh.mom[address1] ==
                                               wh.mom[address2]))

        # check some histogram information from pk and energy
        x = [
            0, 0, -1, -3, -6, -10, -11, -15, -20, -26, -32, -41, -43, -49, -55,
            -67, -73, -82, -88, -94, -102
        ]
        self.assertTrue(np.all(wh.e_hist.lb == x))
        x = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -3, -4, -7,
            -11
        ]
        self.assertTrue(np.all(wh.e_hist.ub == x))
        x = np.ones(21)
        self.assertTrue(np.all(wh.e_hist.bw == x))
        for i in xrange(len(wh.e_hist.h)):
            self.assertTrue(
                len(wh.e_hist.h[i]) == wh.e_hist.ub[i] - wh.e_hist.lb[i] + 1)
        self.assertTrue(
            np.all(
                np.abs(wh.e_hist.h[3] - np.array([
                    0.00907625393757033, 0.0185828627062264, 0.248847389827399,
                    0.723493493528804
                ])) < 1.0e-8))
        self.assertTrue(len(wh.pk_hist) == 2)