Exemplo n.º 1
0
 def testSlicing(self):
     """Test slicing."""
     # Test get error if try to slice before setup.
     self.assertRaises(NotImplementedError, self.testslicer._sliceSimData, 0)
     nbins = 10
     binsize = (self.dvmax - self.dvmin) / (float(nbins))
     self.testslicer = NDSlicer(self.dvlist, binsList=nbins)
     for nvalues in (1000, 10000):
         dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
         self.testslicer.setupSlicer(dv)
         sum = 0
         for i, s in enumerate(self.testslicer):
             idxs = s['idxs']
             dataslice = dv[idxs]
             sum += len(idxs)
             if len(dataslice) > 0:
                 for i, dvname, b in zip(list(range(self.nd)), self.dvlist, s['slicePoint']['binLeft']):
                     self.assertGreaterEqual((dataslice[dvname].min() - b), 0)
                 if i < self.testslicer.nslice-1:
                     self.assertLessEqual((dataslice[dvname].max() - b), binsize)
                 else:
                     self.assertAlmostEqual((dataslice[dvname].max() - b), binsize)
                 self.assertTrue(len(dataslice), nvalues/float(nbins))
         # and check that every data value was assigned somewhere.
         self.assertEqual(sum, nvalues)
Exemplo n.º 2
0
 def setUp(self):
     self.dvmin = 0
     self.dvmax = 1
     nvalues = 1000
     self.nd = 3
     self.dv = makeDataValues(nvalues,
                              self.dvmin,
                              self.dvmax,
                              self.nd,
                              random=True)
     self.dvlist = self.dv.dtype.names
     nvalues = 1000
     bins = np.arange(self.dvmin, self.dvmax, 0.1)
     binsList = []
     self.iterlist = []
     for i in range(self.nd):
         binsList.append(bins)
         # (remember iteration doesn't use the very last bin in 'bins')
         self.iterlist.append(bins[:-1])
     dv = makeDataValues(nvalues,
                         self.dvmin,
                         self.dvmax,
                         self.nd,
                         random=True)
     self.testslicer = NDSlicer(self.dvlist, binsList=binsList)
     self.testslicer.setupSlicer(dv)
Exemplo n.º 3
0
class TestNDSlicerIteration(unittest.TestCase):
    def setUp(self):
        self.dvmin = 0
        self.dvmax = 1
        nvalues = 1000
        self.nd = 3
        self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
        self.dvlist = self.dv.dtype.names
        nvalues = 1000
        bins = np.arange(self.dvmin, self.dvmax, 0.1)
        binsList = []
        self.iterlist = []
        for i in range(self.nd):
            binsList.append(bins)
            # (remember iteration doesn't use the very last bin in 'bins')
            self.iterlist.append(bins[:-1])
        dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
        self.testslicer = NDSlicer(self.dvlist, binsList=binsList)
        self.testslicer.setupSlicer(dv)

    def tearDown(self):
        del self.testslicer
        self.testslicer = None

    def testIteration(self):
        """Test iteration."""
        for s, ib in zip(self.testslicer, itertools.product(*self.iterlist)):
            self.assertEqual(s['slicePoint']['binLeft'], ib)

    def testGetItem(self):
        """Test getting indexed binpoint."""
        for i, s in enumerate(self.testslicer):
            self.assertEqual(self.testslicer[i]['slicePoint']['binLeft'], s['slicePoint']['binLeft'])
        self.assertEqual(self.testslicer[0]['slicePoint']['binLeft'], (0.0, 0.0, 0.0))
Exemplo n.º 4
0
class TestNDSlicerIteration(unittest.TestCase):

    def setUp(self):
        self.dvmin = 0
        self.dvmax = 1
        nvalues = 1000
        self.nd = 3
        self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
        self.dvlist = self.dv.dtype.names
        nvalues = 1000
        bins = np.arange(self.dvmin, self.dvmax, 0.1)
        binsList = []
        self.iterlist = []
        for i in range(self.nd):
            binsList.append(bins)
            # (remember iteration doesn't use the very last bin in 'bins')
            self.iterlist.append(bins[:-1])
        dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
        self.testslicer = NDSlicer(self.dvlist, binsList=binsList)
        self.testslicer.setupSlicer(dv)

    def tearDown(self):
        del self.testslicer
        self.testslicer = None

    def testIteration(self):
        """Test iteration."""
        for s, ib in zip(self.testslicer, itertools.product(*self.iterlist)):
            self.assertEqual(s['slicePoint']['binLeft'], ib)

    def testGetItem(self):
        """Test getting indexed binpoint."""
        for i, s in enumerate(self.testslicer):
            self.assertEqual(self.testslicer[i]['slicePoint']['binLeft'], s['slicePoint']['binLeft'])
        self.assertEqual(self.testslicer[0]['slicePoint']['binLeft'], (0.0, 0.0, 0.0))
Exemplo n.º 5
0
 def setUp(self):
     self.dvmin = 0
     self.dvmax = 1
     nvalues = 1000
     self.nd = 3
     self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
     self.dvlist = self.dv.dtype.names
     self.testslicer = NDSlicer(self.dvlist)
Exemplo n.º 6
0
class TestNDSlicerEqual(unittest.TestCase):
    def setUp(self):
        self.dvmin = 0
        self.dvmax = 1
        nvalues = 1000
        self.nd = 3
        self.dv = makeDataValues(nvalues,
                                 self.dvmin,
                                 self.dvmax,
                                 self.nd,
                                 random=20367)
        self.dvlist = self.dv.dtype.names
        self.testslicer = NDSlicer(self.dvlist, binsList=100)
        self.testslicer.setupSlicer(self.dv)

    def tearDown(self):
        del self.testslicer
        self.testslicer = None

    def testEquivalence(self):
        """Test equals method."""
        # Note that two ND slicers will be considered equal if they are both the same kind of
        # slicer AND have the same bins in all dimensions.
        # Set up another slicer to match (same bins, although not the same data).
        dv2 = makeDataValues(100,
                             self.dvmin,
                             self.dvmax,
                             self.nd,
                             random=10029)
        dvlist = dv2.dtype.names
        testslicer2 = NDSlicer(sliceColList=dvlist,
                               binsList=self.testslicer.bins)
        testslicer2.setupSlicer(dv2)
        self.assertEqual(self.testslicer, testslicer2)
        # Set up another slicer that should not match (different bins)
        dv2 = makeDataValues(1000,
                             self.dvmin + 1,
                             self.dvmax + 1,
                             self.nd,
                             random=209837)
        testslicer2 = NDSlicer(sliceColList=dvlist, binsList=100)
        testslicer2.setupSlicer(dv2)
        self.assertNotEqual(self.testslicer, testslicer2)
        # Set up another slicer that should not match (different dimensions)
        dv2 = makeDataValues(1000,
                             self.dvmin,
                             self.dvmax,
                             self.nd - 1,
                             random=50623)
        testslicer2 = NDSlicer(dv2.dtype.names, binsList=100)
        testslicer2.setupSlicer(dv2)
        self.assertNotEqual(self.testslicer, testslicer2)
        # Set up a different kind of slicer that should not match.
        testslicer2 = UniSlicer()
        dv2 = makeDataValues(100, 0, 1, random=22310098)
        testslicer2.setupSlicer(dv2)
        self.assertNotEqual(self.testslicer, testslicer2)
Exemplo n.º 7
0
 def testSetupSlicerNbinsZeros(self):
     """Test handling case of data being single values."""
     dv = makeDataValues(100, 0, 0, self.nd, random=-1)
     nbins = 10
     testslicer = NDSlicer(self.dvlist, binsList=nbins)
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         testslicer.setupSlicer(dv)
         self.assertIn('creasing binMax', str(w[-1].message))
     expectednbins = nbins**self.nd
     self.assertEqual(testslicer.nslice, expectednbins)
Exemplo n.º 8
0
 def testSetupSlicerNbinsZeros(self):
     """Test handling case of data being single values."""
     dv = makeDataValues(100, 0, 0, self.nd, random=-1)
     nbins = 10
     testslicer = NDSlicer(self.dvlist, binsList=nbins)
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         testslicer.setupSlicer(dv)
         self.assertIn('creasing binMax', str(w[-1].message))
     expectednbins = nbins ** self.nd
     self.assertEqual(testslicer.nslice, expectednbins)
Exemplo n.º 9
0
 def testSetupSlicerBins(self):
     """Test setting up slicer using defined bins."""
     # Used right bins?
     bins = np.arange(self.dvmin, self.dvmax, 0.1)
     binlist = []
     for d in range(self.nd):
         binlist.append(bins)
     testslicer = NDSlicer(self.dvlist, binsList=binlist)
     testslicer.setupSlicer(self.dv)
     for d in range(self.nd):
         np.testing.assert_equal(testslicer.bins[d], bins)
     self.assertEqual(testslicer.nslice, (len(bins)-1)**self.nd)
Exemplo n.º 10
0
 def testSetupSlicerBins(self):
     """Test setting up slicer using defined bins."""
     # Used right bins?
     bins = np.arange(self.dvmin, self.dvmax, 0.1)
     binlist = []
     for d in range(self.nd):
         binlist.append(bins)
     testslicer = NDSlicer(self.dvlist, binsList=binlist)
     testslicer.setupSlicer(self.dv)
     for d in range(self.nd):
         np.testing.assert_equal(testslicer.bins[d], bins)
     self.assertEqual(testslicer.nslice, (len(bins)-1)**self.nd)
Exemplo n.º 11
0
 def testSetupSlicerEquivalent(self):
     """Test setting up slicer using defined bins and nbins is equal where expected."""
     for nbins in (20, 105):
         testslicer = NDSlicer(self.dvlist, binsList=nbins)
         bins = makeDataValues(nbins+1, self.dvmin, self.dvmax, self.nd, random=-1)
         binsList = []
         for i in bins.dtype.names:
             binsList.append(bins[i])
         for nvalues in (100, 10000):
             dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=64432)
             testslicer.setupSlicer(dv)
             for i in range(self.nd):
                 np.testing.assert_allclose(testslicer.bins[i], binsList[i])
Exemplo n.º 12
0
 def setUp(self):
     self.dvmin = 0
     self.dvmax = 1
     nvalues = 1000
     self.nd = 3
     self.dv = makeDataValues(nvalues,
                              self.dvmin,
                              self.dvmax,
                              self.nd,
                              random=20367)
     self.dvlist = self.dv.dtype.names
     self.testslicer = NDSlicer(self.dvlist, binsList=100)
     self.testslicer.setupSlicer(self.dv)
Exemplo n.º 13
0
 def testSetupSlicerEquivalent(self):
     """Test setting up slicer using defined bins and nbins is equal where expected."""
     for nbins in (20, 105):
         testslicer = NDSlicer(self.dvlist, binsList=nbins)
         bins = makeDataValues(nbins+1, self.dvmin, self.dvmax, self.nd, random=False)
         binsList = []
         for i in bins.dtype.names:
             binsList.append(bins[i])
         for nvalues in (100, 10000):
             dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
             testslicer.setupSlicer(dv)
             for i in range(self.nd):
                 np.testing.assert_allclose(testslicer.bins[i], binsList[i])
Exemplo n.º 14
0
 def testSetupSlicerNbins(self):
     """Test setting up slicer using nbins."""
     for nvalues in (100, 1000):
         for nbins in (5, 25, 74):
             dv = makeDataValues(nvalues,
                                 self.dvmin,
                                 self.dvmax,
                                 self.nd,
                                 random=False)
             # Right number of bins?
             # expect one more 'bin' to accomodate last right edge, but nbins accounts for this
             testslicer = NDSlicer(self.dvlist, binsList=nbins)
             testslicer.setupSlicer(dv)
             self.assertEqual(testslicer.nslice, nbins**self.nd)
             # Bins of the right size?
             for i in range(self.nd):
                 bindiff = np.diff(testslicer.bins[i])
                 expectedbindiff = (self.dvmax - self.dvmin) / float(nbins)
                 np.testing.assert_allclose(bindiff, expectedbindiff)
             # Can we use a list of nbins too and get the right number of bins?
             nbinsList = []
             expectednbins = 1
             for d in range(self.nd):
                 nbinsList.append(nbins + d)
                 expectednbins *= (nbins + d)
             testslicer = NDSlicer(self.dvlist, binsList=nbinsList)
             testslicer.setupSlicer(dv)
             self.assertEqual(testslicer.nslice, expectednbins)
Exemplo n.º 15
0
 def testSlicing(self):
     """Test slicing."""
     # Test get error if try to slice before setup.
     self.assertRaises(NotImplementedError, self.testslicer._sliceSimData, 0)
     nbins = 10
     binsize = (self.dvmax - self.dvmin) / (float(nbins))
     self.testslicer = NDSlicer(self.dvlist, binsList=nbins)
     for nvalues in (1000, 10000):
         dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
         self.testslicer.setupSlicer(dv)
         sum = 0
         for i, s in enumerate(self.testslicer):
             idxs = s['idxs']
             dataslice = dv[idxs]
             sum += len(idxs)
             if len(dataslice)>0:
                 for i, dvname, b in zip(range(self.nd), self.dvlist, s['slicePoint']['binLeft']):
                     self.assertGreaterEqual((dataslice[dvname].min() - b), 0)
                 if i < self.testslicer.nslice-1:
                     self.assertLessEqual((dataslice[dvname].max() - b), binsize)
                 else:
                     self.assertAlmostEqual((dataslice[dvname].max() - b), binsize)
                 self.assertTrue(len(dataslice), nvalues/float(nbins))
         # and check that every data value was assigned somewhere.
         self.assertEqual(sum, nvalues)
Exemplo n.º 16
0
 def setUp(self):
     self.dvmin = 0
     self.dvmax = 1
     nvalues = 1000
     self.nd = 3
     self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
     self.dvlist = self.dv.dtype.names
     self.testslicer = NDSlicer(self.dvlist)
Exemplo n.º 17
0
 def setUp(self):
     self.dvmin = 0
     self.dvmax = 1
     nvalues = 1000
     self.nd = 3
     self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=20367)
     self.dvlist = self.dv.dtype.names
     self.testslicer = NDSlicer(self.dvlist, binsList=100)
     self.testslicer.setupSlicer(self.dv)
Exemplo n.º 18
0
class TestNDSlicerEqual(unittest.TestCase):

    def setUp(self):
        self.dvmin = 0
        self.dvmax = 1
        nvalues = 1000
        self.nd = 3
        self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=20367)
        self.dvlist = self.dv.dtype.names
        self.testslicer = NDSlicer(self.dvlist, binsList=100)
        self.testslicer.setupSlicer(self.dv)

    def tearDown(self):
        del self.testslicer
        self.testslicer = None

    def testEquivalence(self):
        """Test equals method."""
        # Note that two ND slicers will be considered equal if they are both the same kind of
        # slicer AND have the same bins in all dimensions.
        # Set up another slicer to match (same bins, although not the same data).
        dv2 = makeDataValues(100, self.dvmin, self.dvmax, self.nd, random=10029)
        dvlist = dv2.dtype.names
        testslicer2 = NDSlicer(sliceColList=dvlist, binsList=self.testslicer.bins)
        testslicer2.setupSlicer(dv2)
        self.assertEqual(self.testslicer, testslicer2)
        # Set up another slicer that should not match (different bins)
        dv2 = makeDataValues(1000, self.dvmin+1, self.dvmax+1, self.nd, random=209837)
        testslicer2 = NDSlicer(sliceColList=dvlist, binsList=100)
        testslicer2.setupSlicer(dv2)
        self.assertNotEqual(self.testslicer, testslicer2)
        # Set up another slicer that should not match (different dimensions)
        dv2 = makeDataValues(1000, self.dvmin, self.dvmax, self.nd-1, random=50623)
        testslicer2 = NDSlicer(dv2.dtype.names, binsList=100)
        testslicer2.setupSlicer(dv2)
        self.assertNotEqual(self.testslicer, testslicer2)
        # Set up a different kind of slicer that should not match.
        testslicer2 = UniSlicer()
        dv2 = makeDataValues(100, 0, 1, random=22310098)
        testslicer2.setupSlicer(dv2)
        self.assertNotEqual(self.testslicer, testslicer2)
Exemplo n.º 19
0
 def setUp(self):
     self.dvmin = 0
     self.dvmax = 1
     nvalues = 1000
     self.nd = 3
     self.dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
     self.dvlist = self.dv.dtype.names
     nvalues = 1000
     bins = np.arange(self.dvmin, self.dvmax, 0.1)
     binsList = []
     self.iterlist = []
     for i in range(self.nd):
         binsList.append(bins)
         # (remember iteration doesn't use the very last bin in 'bins')
         self.iterlist.append(bins[:-1])
     dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=True)
     self.testslicer = NDSlicer(self.dvlist, binsList=binsList)
     self.testslicer.setupSlicer(dv)
Exemplo n.º 20
0
 def testSetupSlicerNbins(self):
     """Test setting up slicer using nbins."""
     for nvalues in (100, 1000):
         for nbins in (5, 25, 74):
             dv = makeDataValues(nvalues, self.dvmin, self.dvmax, self.nd, random=False)
             # Right number of bins?
             # expect one more 'bin' to accomodate last right edge, but nbins accounts for this
             testslicer = NDSlicer(self.dvlist, binsList=nbins)
             testslicer.setupSlicer(dv)
             self.assertEqual(testslicer.nslice, nbins**self.nd)
             # Bins of the right size?
             for i in range(self.nd):
                 bindiff = np.diff(testslicer.bins[i])
                 expectedbindiff = (self.dvmax - self.dvmin) / float(nbins)
                 np.testing.assert_allclose(bindiff, expectedbindiff)
             # Can we use a list of nbins too and get the right number of bins?
             nbinsList = []
             expectednbins = 1
             for d in range(self.nd):
                 nbinsList.append(nbins + d)
                 expectednbins *= (nbins + d)
             testslicer = NDSlicer(self.dvlist, binsList=nbinsList)
             testslicer.setupSlicer(dv)
             self.assertEqual(testslicer.nslice, expectednbins)
Exemplo n.º 21
0
 def testEquivalence(self):
     """Test equals method."""
     # Note that two ND slicers will be considered equal if they are both the same kind of
     # slicer AND have the same bins in all dimensions.
     # Set up another slicer to match (same bins, although not the same data).
     dv2 = makeDataValues(100, self.dvmin, self.dvmax, self.nd, random=True)
     dvlist = dv2.dtype.names
     testslicer2 = NDSlicer(sliceColList=dvlist, binsList=self.testslicer.bins)
     testslicer2.setupSlicer(dv2)
     self.assertEqual(self.testslicer, testslicer2)
     # Set up another slicer that should not match (different bins)
     dv2 = makeDataValues(1000, self.dvmin+1, self.dvmax+1, self.nd, random=True)
     testslicer2 = NDSlicer(sliceColList=dvlist, binsList=100)
     testslicer2.setupSlicer(dv2)
     self.assertNotEqual(self.testslicer, testslicer2)
     # Set up another slicer that should not match (different dimensions)
     dv2 = makeDataValues(1000, self.dvmin, self.dvmax, self.nd-1, random=True)
     testslicer2 = NDSlicer(dv2.dtype.names, binsList=100)
     testslicer2.setupSlicer(dv2)
     self.assertNotEqual(self.testslicer, testslicer2)
     # Set up a different kind of slicer that should not match.
     testslicer2 = UniSlicer()
     dv2 = makeDataValues(100, 0, 1, random=True)
     testslicer2.setupSlicer(dv2)
     self.assertNotEqual(self.testslicer, testslicer2)
Exemplo n.º 22
0
 def testSlicertype(self):
     """Test instantiation of slicer sets slicer type as expected."""
     testslicer = NDSlicer(self.dvlist)
     self.assertEqual(testslicer.slicerName, testslicer.__class__.__name__)
     self.assertEqual(testslicer.slicerName, 'NDSlicer')
Exemplo n.º 23
0
 def testEquivalence(self):
     """Test equals method."""
     # Note that two ND slicers will be considered equal if they are both the same kind of
     # slicer AND have the same bins in all dimensions.
     # Set up another slicer to match (same bins, although not the same data).
     dv2 = makeDataValues(100, self.dvmin, self.dvmax, self.nd, random=True)
     dvlist = dv2.dtype.names
     testslicer2 = NDSlicer(sliceColList=dvlist, binsList=self.testslicer.bins)
     testslicer2.setupSlicer(dv2)
     self.assertEqual(self.testslicer, testslicer2)
     # Set up another slicer that should not match (different bins)
     dv2 = makeDataValues(1000, self.dvmin+1, self.dvmax+1, self.nd, random=True)
     testslicer2 = NDSlicer(sliceColList=dvlist, binsList=100)
     testslicer2.setupSlicer(dv2)
     self.assertNotEqual(self.testslicer, testslicer2)
     # Set up another slicer that should not match (different dimensions)
     dv2 = makeDataValues(1000, self.dvmin, self.dvmax, self.nd-1, random=True)
     testslicer2 = NDSlicer(dv2.dtype.names, binsList=100)
     testslicer2.setupSlicer(dv2)
     self.assertNotEqual(self.testslicer, testslicer2)
     # Set up a different kind of slicer that should not match.
     testslicer2 = UniSlicer()
     dv2 = makeDataValues(100, 0, 1, random=True)
     testslicer2.setupSlicer(dv2)
     self.assertNotEqual(self.testslicer, testslicer2)