def setUp(self): super().setUp() simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig() simpleMapConfig.raList = [45] simpleMapConfig.decList = [45] simpleMapConfig.radiusList = [0.1] self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig) self.tractId = 0 bCircle = self.simpleMap.generateTract( self.tractId).getInnerSkyPolygon().getBoundingCircle() self.targetSources = 1000 self.sourceDensity = (self.targetSources / (bCircle.getArea() * (180 / np.pi)**2)) fakesConfig = CreateRandomApFakesConfig() fakesConfig.fraction = 0.0 fakesConfig.fakeDensity = self.sourceDensity fakesTask = CreateRandomApFakesTask(config=fakesConfig) fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat self.band = 'g' self.magCut = 25 magMask = (fakeCat[fakesConfig.magVar % self.band] < self.magCut) self.expectedAllMatched = magMask.sum() ids = np.where(magMask, np.arange(1, len(fakeCat) + 1, dtype=int), 0) # Add columns to mimic the matched fakes result without running the # full pipeline. self.fakeCat = fakeCat.assign(diaObjectId=ids, filterName=["g"] * len(fakeCat), diaSourceId=ids)
def testRun(self): """Test the run method. """ fakesConfig = CreateRandomApFakesConfig() fakesConfig.fraction = 0.5 fakesConfig.fakeDensity = self.sourceDensity fakesTask = CreateRandomApFakesTask(config=fakesConfig) bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle() result = fakesTask.run(self.tractId, self.simpleMap) fakeCat = result.fakeCat self.assertEqual(len(fakeCat), self.nSources) for idx, row in fakeCat.iterrows(): self.assertTrue( bCircle.contains( geom.SpherePoint(row[fakesTask.config.raColName], row[fakesTask.config.decColName], geom.radians).getVector())) self.assertEqual(fakeCat[fakesConfig.visitSourceFlagCol].sum(), self.nInVisit) self.assertEqual(fakeCat[fakesConfig.templateSourceFlagCol].sum(), self.nInTemplate) for f in fakesConfig.filterSet: filterMags = fakeCat[fakesConfig.magVar % f] self.assertEqual(self.nSources, len(filterMags)) self.assertTrue( np.all(fakesConfig.magMin <= filterMags)) self.assertTrue( np.all(fakesConfig.magMax > filterMags))
def setUp(self): """Create fake data to use in the tests. """ self.bbox = geom.Box2I(geom.Point2I(0, 0), geom.Extent2I(1024, 1153)) dataset = measTests.TestDataset(self.bbox) self.exposure = dataset.exposure simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig() simpleMapConfig.raList = [ dataset.exposure.getWcs().getSkyOrigin().getRa().asDegrees() ] simpleMapConfig.decList = [ dataset.exposure.getWcs().getSkyOrigin().getDec().asDegrees() ] simpleMapConfig.radiusList = [0.1] self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig) self.tractId = 0 bCircle = self.simpleMap.generateTract( self.tractId).getInnerSkyPolygon().getBoundingCircle() targetSources = 10000 self.sourceDensity = (targetSources / (bCircle.getArea() * (180 / np.pi)**2)) self.rng = np.random.default_rng(1234) fakesConfig = CreateRandomApFakesConfig() fakesConfig.fraction = 0.0 fakesConfig.fakeDensity = self.sourceDensity fakesTask = CreateRandomApFakesTask(config=fakesConfig) self.fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat self.inExp = np.zeros(len(self.fakeCat), dtype=bool) bbox = geom.Box2D(self.exposure.getBBox()) for idx, row in self.fakeCat.iterrows(): coord = geom.SpherePoint(row[fakesConfig.raColName], row[fakesConfig.decColName], geom.radians) cent = self.exposure.getWcs().skyToPixel(coord) self.inExp[idx] = bbox.contains(cent) tmpCat = self.fakeCat[self.inExp].iloc[:int(self.inExp.sum() / 2)] extraColumnData = self.rng.integers(0, 100, size=len(tmpCat)) self.sourceCat = pd.DataFrame( data={ "ra": np.degrees(tmpCat[fakesTask.config.raColName]), "decl": np.degrees(tmpCat[fakesTask.config.decColName]), "diaObjectId": np.arange(1, len(tmpCat) + 1, dtype=int), "filterName": "g", "diaSourceId": np.arange(1, len(tmpCat) + 1, dtype=int), "extraColumn": extraColumnData }) self.sourceCat.set_index(["diaObjectId", "filterName", "extraColumn"], drop=False, inplace=True)
def testCreateRotMatrix(self): """Test that the rotation matrix is computed correctly and rotates a test vector to the expected location. """ createFakes = CreateRandomApFakesTask() bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle() rotMatrix = createFakes._createRotMatrix(bCircle) rotatedVector = np.dot(rotMatrix, np.array([0, 0, 1])) expectedVect = bCircle.getCenter() self.assertAlmostEqual(expectedVect.x(), rotatedVector[0]) self.assertAlmostEqual(expectedVect.y(), rotatedVector[1]) self.assertAlmostEqual(expectedVect.z(), rotatedVector[2])
def testVisitCoaddSubdivision(self): """Test that the number of assigned visit to template objects is correct. """ fakesConfig = CreateRandomApFakesConfig() fakesConfig.fraction = 0.5 fakesTask = CreateRandomApFakesTask(config=fakesConfig) subdivision = fakesTask.createVisitCoaddSubdivision(self.nSources) self.assertEqual( subdivision[fakesConfig.visitSourceFlagCol].sum(), self.nInVisit) self.assertEqual( subdivision[fakesConfig.templateSourceFlagCol].sum(), self.nInTemplate)
def testRunQuantum(self): """Test the run quantum method with a gen3 butler. """ root = tempfile.mkdtemp() dimensions = {"instrument": ["notACam"], "skymap": ["skyMap"], "tract": [0, 42], } testRepo = butlerTests.makeTestRepo(root, dimensions) fakesTask = CreateRandomApFakesTask() connections = fakesTask.config.ConnectionsClass( config=fakesTask.config) butlerTests.addDatasetType( testRepo, connections.skyMap.name, connections.skyMap.dimensions, connections.skyMap.storageClass) butlerTests.addDatasetType( testRepo, connections.fakeCat.name, connections.fakeCat.dimensions, connections.fakeCat.storageClass) dataId = {"skymap": "skyMap", "tract": 0} butler = butlerTests.makeTestCollection(testRepo) butler.put(self.simpleMap, "skyMap", {"skymap": "skyMap"}) quantum = testUtils.makeQuantum( fakesTask, butler, dataId, {key: dataId for key in {"skyMap", "fakeCat"}}) run = testUtils.runTestQuantum(fakesTask, butler, quantum, True) # Actual input dataset omitted for simplicity run.assert_called_once_with(tractId=dataId["tract"], skyMap=self.simpleMap) shutil.rmtree(root, ignore_errors=True)
def testRandomMagnitudes(self): """Test that the correct number of filters and magnitudes have been produced. """ fakesConfig = CreateRandomApFakesConfig() fakesConfig.filterSet = ["u", "g"] fakesConfig.magVar = "%smag" fakesConfig.magMin = 20 fakesConfig.magMax = 21 fakesTask = CreateRandomApFakesTask(config=fakesConfig) mags = fakesTask.createRandomMagnitudes(self.nSources, self.rng) self.assertEqual(len(fakesConfig.filterSet), len(mags)) for f in fakesConfig.filterSet: filterMags = mags[fakesConfig.magVar % f] self.assertEqual(self.nSources, len(filterMags)) self.assertTrue( np.all(fakesConfig.magMin <= filterMags)) self.assertTrue( np.all(fakesConfig.magMax > filterMags))
def testCreateRandomPositions(self): """Test that the correct number of sources are produced and are contained in the cap bound. """ fakesTask = CreateRandomApFakesTask() bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle() randData = fakesTask.createRandomPositions( nFakes=self.nSources, boundingCircle=bCircle, rng=self.rng) self.assertEqual(self.nSources, len(randData[fakesTask.config.raColName])) self.assertEqual(self.nSources, len(randData[fakesTask.config.decColName])) for idx in range(self.nSources): self.assertTrue( bCircle.contains( geom.SpherePoint(randData[fakesTask.config.raColName][idx], randData[fakesTask.config.decColName][idx], geom.radians).getVector()))