Coverage for python/lsst/daf/butler/registry/tests/_registry.py: 4%

1298 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-02 18:18 -0700

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21from __future__ import annotations 

22 

23__all__ = ["RegistryTests"] 

24 

25import itertools 

26import logging 

27import os 

28import re 

29import unittest 

30import uuid 

31from abc import ABC, abstractmethod 

32from collections import defaultdict, namedtuple 

33from datetime import datetime, timedelta 

34from typing import TYPE_CHECKING, Iterator, Optional, Type, Union 

35 

36import astropy.time 

37import sqlalchemy 

38 

39try: 

40 import numpy as np 

41except ImportError: 

42 np = None 

43 

44import lsst.sphgeom 

45 

46from ...core import ( 

47 DataCoordinate, 

48 DataCoordinateSet, 

49 DatasetAssociation, 

50 DatasetRef, 

51 DatasetType, 

52 DimensionGraph, 

53 NamedValueSet, 

54 StorageClass, 

55 Timespan, 

56 ddl, 

57) 

58from .._collectionType import CollectionType 

59from .._config import RegistryConfig 

60from .._exceptions import ( 

61 ArgumentError, 

62 CollectionError, 

63 CollectionTypeError, 

64 ConflictingDefinitionError, 

65 DataIdValueError, 

66 InconsistentDataIdError, 

67 MissingCollectionError, 

68 OrphanedRecordError, 

69) 

70from ..interfaces import ButlerAttributeExistsError, DatasetIdGenEnum 

71from ..summaries import CollectionSummary 

72 

73if TYPE_CHECKING: 73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true

74 from .._registry import Registry 

75 

76 

77class RegistryTests(ABC): 

78 """Generic tests for the `Registry` class that can be subclassed to 

79 generate tests for different configurations. 

80 """ 

81 

82 collectionsManager: Optional[str] = None 

83 """Name of the collections manager class, if subclass provides value for 

84 this member then it overrides name specified in default configuration 

85 (`str`). 

86 """ 

87 

88 datasetsManager: Optional[str] = None 

89 """Name of the datasets manager class, if subclass provides value for 

90 this member then it overrides name specified in default configuration 

91 (`str`). 

92 """ 

93 

94 @classmethod 

95 @abstractmethod 

96 def getDataDir(cls) -> str: 

97 """Return the root directory containing test data YAML files.""" 

98 raise NotImplementedError() 

99 

100 def makeRegistryConfig(self) -> RegistryConfig: 

101 """Create RegistryConfig used to create a registry. 

102 

103 This method should be called by a subclass from `makeRegistry`. 

104 Returned instance will be pre-configured based on the values of class 

105 members, and default-configured for all other parameters. Subclasses 

106 that need default configuration should just instantiate 

107 `RegistryConfig` directly. 

108 """ 

109 config = RegistryConfig() 

110 if self.collectionsManager: 

111 config["managers", "collections"] = self.collectionsManager 

112 if self.datasetsManager: 

113 config["managers", "datasets"] = self.datasetsManager 

114 return config 

115 

116 @abstractmethod 

117 def makeRegistry(self, share_repo_with: Optional[Registry] = None) -> Optional[Registry]: 

118 """Return the Registry instance to be tested. 

119 

120 Parameters 

121 ---------- 

122 share_repo_with : `Registry`, optional 

123 If provided, the new registry should point to the same data 

124 repository as this existing registry. 

125 

126 Returns 

127 ------- 

128 registry : `Registry` 

129 New `Registry` instance, or `None` *only* if `share_repo_with` is 

130 not `None` and this test case does not support that argument 

131 (e.g. it is impossible with in-memory SQLite DBs). 

132 """ 

133 raise NotImplementedError() 

134 

135 def loadData(self, registry: Registry, filename: str): 

136 """Load registry test data from ``getDataDir/<filename>``, 

137 which should be a YAML import/export file. 

138 """ 

139 from ...transfers import YamlRepoImportBackend 

140 

141 with open(os.path.join(self.getDataDir(), filename), "r") as stream: 

142 backend = YamlRepoImportBackend(stream, registry) 

143 backend.register() 

144 backend.load(datastore=None) 

145 

146 def checkQueryResults(self, results, expected): 

147 """Check that a query results object contains expected values. 

148 

149 Parameters 

150 ---------- 

151 results : `DataCoordinateQueryResults` or `DatasetQueryResults` 

152 A lazy-evaluation query results object. 

153 expected : `list` 

154 A list of `DataCoordinate` o `DatasetRef` objects that should be 

155 equal to results of the query, aside from ordering. 

156 """ 

157 self.assertCountEqual(list(results), expected) 

158 self.assertEqual(results.count(), len(expected)) 

159 if expected: 

160 self.assertTrue(results.any()) 

161 else: 

162 self.assertFalse(results.any()) 

163 

164 def testOpaque(self): 

165 """Tests for `Registry.registerOpaqueTable`, 

166 `Registry.insertOpaqueData`, `Registry.fetchOpaqueData`, and 

167 `Registry.deleteOpaqueData`. 

168 """ 

169 registry = self.makeRegistry() 

170 table = "opaque_table_for_testing" 

171 registry.registerOpaqueTable( 

172 table, 

173 spec=ddl.TableSpec( 

174 fields=[ 

175 ddl.FieldSpec("id", dtype=sqlalchemy.BigInteger, primaryKey=True), 

176 ddl.FieldSpec("name", dtype=sqlalchemy.String, length=16, nullable=False), 

177 ddl.FieldSpec("count", dtype=sqlalchemy.SmallInteger, nullable=True), 

178 ], 

179 ), 

180 ) 

181 rows = [ 

182 {"id": 1, "name": "one", "count": None}, 

183 {"id": 2, "name": "two", "count": 5}, 

184 {"id": 3, "name": "three", "count": 6}, 

185 ] 

186 registry.insertOpaqueData(table, *rows) 

187 self.assertCountEqual(rows, list(registry.fetchOpaqueData(table))) 

188 self.assertEqual(rows[0:1], list(registry.fetchOpaqueData(table, id=1))) 

189 self.assertEqual(rows[1:2], list(registry.fetchOpaqueData(table, name="two"))) 

190 self.assertEqual(rows[0:1], list(registry.fetchOpaqueData(table, id=(1, 3), name=("one", "two")))) 

191 self.assertEqual(rows, list(registry.fetchOpaqueData(table, id=(1, 2, 3)))) 

192 # Test very long IN clause which exceeds sqlite limit on number of 

193 # parameters. SQLite says the limit is 32k but it looks like it is 

194 # much higher. 

195 self.assertEqual(rows, list(registry.fetchOpaqueData(table, id=list(range(300_000))))) 

196 # Two IN clauses, each longer than 1k batch size, first with 

197 # duplicates, second has matching elements in different batches (after 

198 # sorting). 

199 self.assertEqual( 

200 rows[0:2], 

201 list( 

202 registry.fetchOpaqueData( 

203 table, 

204 id=list(range(1000)) + list(range(100, 0, -1)), 

205 name=["one"] + [f"q{i}" for i in range(2200)] + ["two"], 

206 ) 

207 ), 

208 ) 

209 self.assertEqual([], list(registry.fetchOpaqueData(table, id=1, name="two"))) 

210 registry.deleteOpaqueData(table, id=3) 

211 self.assertCountEqual(rows[:2], list(registry.fetchOpaqueData(table))) 

212 registry.deleteOpaqueData(table) 

213 self.assertEqual([], list(registry.fetchOpaqueData(table))) 

214 

215 def testDatasetType(self): 

216 """Tests for `Registry.registerDatasetType` and 

217 `Registry.getDatasetType`. 

218 """ 

219 registry = self.makeRegistry() 

220 # Check valid insert 

221 datasetTypeName = "test" 

222 storageClass = StorageClass("testDatasetType") 

223 registry.storageClasses.registerStorageClass(storageClass) 

224 dimensions = registry.dimensions.extract(("instrument", "visit")) 

225 differentDimensions = registry.dimensions.extract(("instrument", "patch")) 

226 inDatasetType = DatasetType(datasetTypeName, dimensions, storageClass) 

227 # Inserting for the first time should return True 

228 self.assertTrue(registry.registerDatasetType(inDatasetType)) 

229 outDatasetType1 = registry.getDatasetType(datasetTypeName) 

230 self.assertEqual(outDatasetType1, inDatasetType) 

231 

232 # Re-inserting should work 

233 self.assertFalse(registry.registerDatasetType(inDatasetType)) 

234 # Except when they are not identical 

235 with self.assertRaises(ConflictingDefinitionError): 

236 nonIdenticalDatasetType = DatasetType(datasetTypeName, differentDimensions, storageClass) 

237 registry.registerDatasetType(nonIdenticalDatasetType) 

238 

239 # Template can be None 

240 datasetTypeName = "testNoneTemplate" 

241 storageClass = StorageClass("testDatasetType2") 

242 registry.storageClasses.registerStorageClass(storageClass) 

243 dimensions = registry.dimensions.extract(("instrument", "visit")) 

244 inDatasetType = DatasetType(datasetTypeName, dimensions, storageClass) 

245 registry.registerDatasetType(inDatasetType) 

246 outDatasetType2 = registry.getDatasetType(datasetTypeName) 

247 self.assertEqual(outDatasetType2, inDatasetType) 

248 

249 allTypes = set(registry.queryDatasetTypes()) 

250 self.assertEqual(allTypes, {outDatasetType1, outDatasetType2}) 

251 

252 def testDimensions(self): 

253 """Tests for `Registry.insertDimensionData`, 

254 `Registry.syncDimensionData`, and `Registry.expandDataId`. 

255 """ 

256 registry = self.makeRegistry() 

257 dimensionName = "instrument" 

258 dimension = registry.dimensions[dimensionName] 

259 dimensionValue = { 

260 "name": "DummyCam", 

261 "visit_max": 10, 

262 "visit_system": 0, 

263 "exposure_max": 10, 

264 "detector_max": 2, 

265 "class_name": "lsst.pipe.base.Instrument", 

266 } 

267 registry.insertDimensionData(dimensionName, dimensionValue) 

268 # Inserting the same value twice should fail 

269 with self.assertRaises(sqlalchemy.exc.IntegrityError): 

270 registry.insertDimensionData(dimensionName, dimensionValue) 

271 # expandDataId should retrieve the record we just inserted 

272 self.assertEqual( 

273 registry.expandDataId(instrument="DummyCam", graph=dimension.graph) 

274 .records[dimensionName] 

275 .toDict(), 

276 dimensionValue, 

277 ) 

278 # expandDataId should raise if there is no record with the given ID. 

279 with self.assertRaises(DataIdValueError): 

280 registry.expandDataId({"instrument": "Unknown"}, graph=dimension.graph) 

281 # band doesn't have a table; insert should fail. 

282 with self.assertRaises(TypeError): 

283 registry.insertDimensionData("band", {"band": "i"}) 

284 dimensionName2 = "physical_filter" 

285 dimension2 = registry.dimensions[dimensionName2] 

286 dimensionValue2 = {"name": "DummyCam_i", "band": "i"} 

287 # Missing required dependency ("instrument") should fail 

288 with self.assertRaises(KeyError): 

289 registry.insertDimensionData(dimensionName2, dimensionValue2) 

290 # Adding required dependency should fix the failure 

291 dimensionValue2["instrument"] = "DummyCam" 

292 registry.insertDimensionData(dimensionName2, dimensionValue2) 

293 # expandDataId should retrieve the record we just inserted. 

294 self.assertEqual( 

295 registry.expandDataId(instrument="DummyCam", physical_filter="DummyCam_i", graph=dimension2.graph) 

296 .records[dimensionName2] 

297 .toDict(), 

298 dimensionValue2, 

299 ) 

300 # Use syncDimensionData to insert a new record successfully. 

301 dimensionName3 = "detector" 

302 dimensionValue3 = { 

303 "instrument": "DummyCam", 

304 "id": 1, 

305 "full_name": "one", 

306 "name_in_raft": "zero", 

307 "purpose": "SCIENCE", 

308 } 

309 self.assertTrue(registry.syncDimensionData(dimensionName3, dimensionValue3)) 

310 # Sync that again. Note that one field ("raft") is NULL, and that 

311 # should be okay. 

312 self.assertFalse(registry.syncDimensionData(dimensionName3, dimensionValue3)) 

313 # Now try that sync with the same primary key but a different value. 

314 # This should fail. 

315 with self.assertRaises(ConflictingDefinitionError): 

316 registry.syncDimensionData( 

317 dimensionName3, 

318 { 

319 "instrument": "DummyCam", 

320 "id": 1, 

321 "full_name": "one", 

322 "name_in_raft": "four", 

323 "purpose": "SCIENCE", 

324 }, 

325 ) 

326 

327 @unittest.skipIf(np is None, "numpy not available.") 

328 def testNumpyDataId(self): 

329 """Test that we can use a numpy int in a dataId.""" 

330 registry = self.makeRegistry() 

331 dimensionEntries = [ 

332 ("instrument", {"instrument": "DummyCam"}), 

333 ("physical_filter", {"instrument": "DummyCam", "name": "d-r", "band": "R"}), 

334 # Using an np.int64 here fails unless Records.fromDict is also 

335 # patched to look for numbers.Integral 

336 ("visit", {"instrument": "DummyCam", "id": 42, "name": "fortytwo", "physical_filter": "d-r"}), 

337 ] 

338 for args in dimensionEntries: 

339 registry.insertDimensionData(*args) 

340 

341 # Try a normal integer and something that looks like an int but 

342 # is not. 

343 for visit_id in (42, np.int64(42)): 

344 with self.subTest(visit_id=visit_id, id_type=type(visit_id).__name__): 

345 expanded = registry.expandDataId({"instrument": "DummyCam", "visit": visit_id}) 

346 self.assertEqual(expanded["visit"], int(visit_id)) 

347 self.assertIsInstance(expanded["visit"], int) 

348 

349 def testDataIdRelationships(self): 

350 """Test that `Registry.expandDataId` raises an exception when the given 

351 keys are inconsistent. 

352 """ 

353 registry = self.makeRegistry() 

354 self.loadData(registry, "base.yaml") 

355 # Insert a few more dimension records for the next test. 

356 registry.insertDimensionData( 

357 "exposure", 

358 {"instrument": "Cam1", "id": 1, "obs_id": "one", "physical_filter": "Cam1-G"}, 

359 ) 

360 registry.insertDimensionData( 

361 "exposure", 

362 {"instrument": "Cam1", "id": 2, "obs_id": "two", "physical_filter": "Cam1-G"}, 

363 ) 

364 registry.insertDimensionData( 

365 "visit_system", 

366 {"instrument": "Cam1", "id": 0, "name": "one-to-one"}, 

367 ) 

368 registry.insertDimensionData( 

369 "visit", 

370 {"instrument": "Cam1", "id": 1, "name": "one", "physical_filter": "Cam1-G", "visit_system": 0}, 

371 ) 

372 registry.insertDimensionData( 

373 "visit_definition", 

374 {"instrument": "Cam1", "visit": 1, "exposure": 1, "visit_system": 0}, 

375 ) 

376 with self.assertRaises(InconsistentDataIdError): 

377 registry.expandDataId( 

378 {"instrument": "Cam1", "visit": 1, "exposure": 2}, 

379 ) 

380 

381 def testDataset(self): 

382 """Basic tests for `Registry.insertDatasets`, `Registry.getDataset`, 

383 and `Registry.removeDatasets`. 

384 """ 

385 registry = self.makeRegistry() 

386 self.loadData(registry, "base.yaml") 

387 run = "tésτ" 

388 registry.registerRun(run) 

389 datasetType = registry.getDatasetType("bias") 

390 dataId = {"instrument": "Cam1", "detector": 2} 

391 (ref,) = registry.insertDatasets(datasetType, dataIds=[dataId], run=run) 

392 outRef = registry.getDataset(ref.id) 

393 self.assertIsNotNone(ref.id) 

394 self.assertEqual(ref, outRef) 

395 with self.assertRaises(ConflictingDefinitionError): 

396 registry.insertDatasets(datasetType, dataIds=[dataId], run=run) 

397 registry.removeDatasets([ref]) 

398 self.assertIsNone(registry.findDataset(datasetType, dataId, collections=[run])) 

399 

400 def testFindDataset(self): 

401 """Tests for `Registry.findDataset`.""" 

402 registry = self.makeRegistry() 

403 self.loadData(registry, "base.yaml") 

404 run = "tésτ" 

405 datasetType = registry.getDatasetType("bias") 

406 dataId = {"instrument": "Cam1", "detector": 4} 

407 registry.registerRun(run) 

408 (inputRef,) = registry.insertDatasets(datasetType, dataIds=[dataId], run=run) 

409 outputRef = registry.findDataset(datasetType, dataId, collections=[run]) 

410 self.assertEqual(outputRef, inputRef) 

411 # Check that retrieval with invalid dataId raises 

412 with self.assertRaises(LookupError): 

413 dataId = {"instrument": "Cam1"} # no detector 

414 registry.findDataset(datasetType, dataId, collections=run) 

415 # Check that different dataIds match to different datasets 

416 dataId1 = {"instrument": "Cam1", "detector": 1} 

417 (inputRef1,) = registry.insertDatasets(datasetType, dataIds=[dataId1], run=run) 

418 dataId2 = {"instrument": "Cam1", "detector": 2} 

419 (inputRef2,) = registry.insertDatasets(datasetType, dataIds=[dataId2], run=run) 

420 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=run), inputRef1) 

421 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=run), inputRef2) 

422 self.assertNotEqual(registry.findDataset(datasetType, dataId1, collections=run), inputRef2) 

423 self.assertNotEqual(registry.findDataset(datasetType, dataId2, collections=run), inputRef1) 

424 # Check that requesting a non-existing dataId returns None 

425 nonExistingDataId = {"instrument": "Cam1", "detector": 3} 

426 self.assertIsNone(registry.findDataset(datasetType, nonExistingDataId, collections=run)) 

427 

428 def testRemoveDatasetTypeSuccess(self): 

429 """Test that Registry.removeDatasetType works when there are no 

430 datasets of that type present. 

431 """ 

432 registry = self.makeRegistry() 

433 self.loadData(registry, "base.yaml") 

434 registry.removeDatasetType("flat") 

435 with self.assertRaises(KeyError): 

436 registry.getDatasetType("flat") 

437 

438 def testRemoveDatasetTypeFailure(self): 

439 """Test that Registry.removeDatasetType raises when there are datasets 

440 of that type present or if the dataset type is for a component. 

441 """ 

442 registry = self.makeRegistry() 

443 self.loadData(registry, "base.yaml") 

444 self.loadData(registry, "datasets.yaml") 

445 with self.assertRaises(OrphanedRecordError): 

446 registry.removeDatasetType("flat") 

447 with self.assertRaises(ValueError): 

448 registry.removeDatasetType(DatasetType.nameWithComponent("flat", "image")) 

449 

450 def testImportDatasetsUUID(self): 

451 """Test for `Registry._importDatasets` with UUID dataset ID.""" 

452 if not self.datasetsManager.endswith(".ByDimensionsDatasetRecordStorageManagerUUID"): 

453 self.skipTest(f"Unexpected dataset manager {self.datasetsManager}") 

454 

455 registry = self.makeRegistry() 

456 self.loadData(registry, "base.yaml") 

457 for run in range(6): 

458 registry.registerRun(f"run{run}") 

459 datasetTypeBias = registry.getDatasetType("bias") 

460 datasetTypeFlat = registry.getDatasetType("flat") 

461 dataIdBias1 = {"instrument": "Cam1", "detector": 1} 

462 dataIdBias2 = {"instrument": "Cam1", "detector": 2} 

463 dataIdFlat1 = {"instrument": "Cam1", "detector": 1, "physical_filter": "Cam1-G", "band": "g"} 

464 

465 dataset_id = uuid.uuid4() 

466 ref = DatasetRef(datasetTypeBias, dataIdBias1, id=dataset_id, run="run0") 

467 (ref1,) = registry._importDatasets([ref]) 

468 # UUID is used without change 

469 self.assertEqual(ref.id, ref1.id) 

470 

471 # All different failure modes 

472 refs = ( 

473 # Importing same DatasetRef with different dataset ID is an error 

474 DatasetRef(datasetTypeBias, dataIdBias1, id=uuid.uuid4(), run="run0"), 

475 # Same DatasetId but different DataId 

476 DatasetRef(datasetTypeBias, dataIdBias2, id=ref1.id, run="run0"), 

477 DatasetRef(datasetTypeFlat, dataIdFlat1, id=ref1.id, run="run0"), 

478 # Same DatasetRef and DatasetId but different run 

479 DatasetRef(datasetTypeBias, dataIdBias1, id=ref1.id, run="run1"), 

480 ) 

481 for ref in refs: 

482 with self.assertRaises(ConflictingDefinitionError): 

483 registry._importDatasets([ref]) 

484 

485 # Test for non-unique IDs, they can be re-imported multiple times. 

486 for run, idGenMode in ((2, DatasetIdGenEnum.DATAID_TYPE), (4, DatasetIdGenEnum.DATAID_TYPE_RUN)): 

487 with self.subTest(idGenMode=idGenMode): 

488 # Use integer dataset ID to force UUID calculation in _import 

489 ref = DatasetRef(datasetTypeBias, dataIdBias1, id=0, run=f"run{run}") 

490 (ref1,) = registry._importDatasets([ref], idGenerationMode=idGenMode) 

491 self.assertIsInstance(ref1.id, uuid.UUID) 

492 self.assertEqual(ref1.id.version, 5) 

493 

494 # Importing it again is OK 

495 (ref2,) = registry._importDatasets([ref1]) 

496 self.assertEqual(ref2.id, ref1.id) 

497 

498 # Cannot import to different run with the same ID 

499 ref = DatasetRef(datasetTypeBias, dataIdBias1, id=ref1.id, run=f"run{run+1}") 

500 with self.assertRaises(ConflictingDefinitionError): 

501 registry._importDatasets([ref]) 

502 

503 ref = DatasetRef(datasetTypeBias, dataIdBias1, id=0, run=f"run{run+1}") 

504 if idGenMode is DatasetIdGenEnum.DATAID_TYPE: 

505 # Cannot import same DATAID_TYPE ref into a new run 

506 with self.assertRaises(ConflictingDefinitionError): 

507 (ref2,) = registry._importDatasets([ref], idGenerationMode=idGenMode) 

508 else: 

509 # DATAID_TYPE_RUN ref can be imported into a new run 

510 (ref2,) = registry._importDatasets([ref], idGenerationMode=idGenMode) 

511 

512 def testImportDatasetsInt(self): 

513 """Test for `Registry._importDatasets` with integer dataset ID.""" 

514 if not self.datasetsManager.endswith(".ByDimensionsDatasetRecordStorageManager"): 

515 self.skipTest(f"Unexpected dataset manager {self.datasetsManager}") 

516 

517 registry = self.makeRegistry() 

518 self.loadData(registry, "base.yaml") 

519 run = "tésτ" 

520 registry.registerRun(run) 

521 datasetTypeBias = registry.getDatasetType("bias") 

522 datasetTypeFlat = registry.getDatasetType("flat") 

523 dataIdBias1 = {"instrument": "Cam1", "detector": 1} 

524 dataIdBias2 = {"instrument": "Cam1", "detector": 2} 

525 dataIdFlat1 = {"instrument": "Cam1", "detector": 1, "physical_filter": "Cam1-G", "band": "g"} 

526 dataset_id = 999999999 

527 

528 ref = DatasetRef(datasetTypeBias, dataIdBias1, id=dataset_id, run=run) 

529 (ref1,) = registry._importDatasets([ref]) 

530 # Should make new integer ID. 

531 self.assertNotEqual(ref1.id, ref.id) 

532 

533 # Ingesting same dataId with different dataset ID is an error 

534 ref2 = ref1.unresolved().resolved(dataset_id, run=run) 

535 with self.assertRaises(ConflictingDefinitionError): 

536 registry._importDatasets([ref2]) 

537 

538 # Ingesting different dataId with the same dataset ID should work 

539 ref3 = DatasetRef(datasetTypeBias, dataIdBias2, id=ref1.id, run=run) 

540 (ref4,) = registry._importDatasets([ref3]) 

541 self.assertNotEqual(ref4.id, ref1.id) 

542 

543 ref3 = DatasetRef(datasetTypeFlat, dataIdFlat1, id=ref1.id, run=run) 

544 (ref4,) = registry._importDatasets([ref3]) 

545 self.assertNotEqual(ref4.id, ref1.id) 

546 

547 def testDatasetTypeComponentQueries(self): 

548 """Test component options when querying for dataset types.""" 

549 registry = self.makeRegistry() 

550 self.loadData(registry, "base.yaml") 

551 self.loadData(registry, "datasets.yaml") 

552 # Test querying for dataset types with different inputs. 

553 # First query for all dataset types; components should only be included 

554 # when components=True. 

555 self.assertEqual({"bias", "flat"}, NamedValueSet(registry.queryDatasetTypes()).names) 

556 self.assertEqual({"bias", "flat"}, NamedValueSet(registry.queryDatasetTypes(components=False)).names) 

557 self.assertLess( 

558 {"bias", "flat", "bias.wcs", "flat.photoCalib"}, 

559 NamedValueSet(registry.queryDatasetTypes(components=True)).names, 

560 ) 

561 # Use a pattern that can match either parent or components. Again, 

562 # components are only returned if components=True. 

563 self.assertEqual({"bias"}, NamedValueSet(registry.queryDatasetTypes(re.compile("^bias.*"))).names) 

564 self.assertEqual( 

565 {"bias"}, NamedValueSet(registry.queryDatasetTypes(re.compile("^bias.*"), components=False)).names 

566 ) 

567 self.assertLess( 

568 {"bias", "bias.wcs"}, 

569 NamedValueSet(registry.queryDatasetTypes(re.compile("^bias.*"), components=True)).names, 

570 ) 

571 # This pattern matches only a component. In this case we also return 

572 # that component dataset type if components=None. 

573 self.assertEqual( 

574 {"bias.wcs"}, NamedValueSet(registry.queryDatasetTypes(re.compile(r"^bias\.wcs"))).names 

575 ) 

576 self.assertEqual( 

577 set(), 

578 NamedValueSet(registry.queryDatasetTypes(re.compile(r"^bias\.wcs"), components=False)).names, 

579 ) 

580 self.assertEqual( 

581 {"bias.wcs"}, 

582 NamedValueSet(registry.queryDatasetTypes(re.compile(r"^bias\.wcs"), components=True)).names, 

583 ) 

584 # Add a dataset type using a StorageClass that we'll then remove; check 

585 # that this does not affect our ability to query for dataset types 

586 # (though it will warn). 

587 tempStorageClass = StorageClass( 

588 name="TempStorageClass", 

589 components={"data", registry.storageClasses.getStorageClass("StructuredDataDict")}, 

590 ) 

591 registry.storageClasses.registerStorageClass(tempStorageClass) 

592 datasetType = DatasetType( 

593 "temporary", 

594 dimensions=["instrument"], 

595 storageClass=tempStorageClass, 

596 universe=registry.dimensions, 

597 ) 

598 registry.registerDatasetType(datasetType) 

599 registry.storageClasses._unregisterStorageClass(tempStorageClass.name) 

600 datasetType._storageClass = None 

601 del tempStorageClass 

602 # Querying for all dataset types, including components, should include 

603 # at least all non-component dataset types (and I don't want to 

604 # enumerate all of the Exposure components for bias and flat here). 

605 with self.assertLogs("lsst.daf.butler.registries", logging.WARN) as cm: 

606 everything = NamedValueSet(registry.queryDatasetTypes(components=True)) 

607 self.assertIn("TempStorageClass", cm.output[0]) 

608 self.assertLess({"bias", "flat", "temporary"}, everything.names) 

609 # It should not include "temporary.columns", because we tried to remove 

610 # the storage class that would tell it about that. So if the next line 

611 # fails (i.e. "temporary.columns" _is_ in everything.names), it means 

612 # this part of the test isn't doing anything, because the _unregister 

613 # call about isn't simulating the real-life case we want it to 

614 # simulate, in which different versions of daf_butler in entirely 

615 # different Python processes interact with the same repo. 

616 self.assertNotIn("temporary.data", everything.names) 

617 # Query for dataset types that start with "temp". This should again 

618 # not include the component, and also not fail. 

619 with self.assertLogs("lsst.daf.butler.registries", logging.WARN) as cm: 

620 startsWithTemp = NamedValueSet(registry.queryDatasetTypes(re.compile("temp.*"))) 

621 self.assertIn("TempStorageClass", cm.output[0]) 

622 self.assertEqual({"temporary"}, startsWithTemp.names) 

623 # Querying with no components should not warn at all. 

624 with self.assertLogs("lsst.daf.butler.registries", logging.WARN) as cm: 

625 startsWithTemp = NamedValueSet(registry.queryDatasetTypes(re.compile("temp.*"), components=False)) 

626 # Must issue a warning of our own to be captured. 

627 logging.getLogger("lsst.daf.butler.registries").warning("test message") 

628 self.assertEqual(len(cm.output), 1) 

629 self.assertIn("test message", cm.output[0]) 

630 

631 def testComponentLookups(self): 

632 """Test searching for component datasets via their parents.""" 

633 registry = self.makeRegistry() 

634 self.loadData(registry, "base.yaml") 

635 self.loadData(registry, "datasets.yaml") 

636 # Test getting the child dataset type (which does still exist in the 

637 # Registry), and check for consistency with 

638 # DatasetRef.makeComponentRef. 

639 collection = "imported_g" 

640 parentType = registry.getDatasetType("bias") 

641 childType = registry.getDatasetType("bias.wcs") 

642 parentRefResolved = registry.findDataset( 

643 parentType, collections=collection, instrument="Cam1", detector=1 

644 ) 

645 self.assertIsInstance(parentRefResolved, DatasetRef) 

646 self.assertEqual(childType, parentRefResolved.makeComponentRef("wcs").datasetType) 

647 # Search for a single dataset with findDataset. 

648 childRef1 = registry.findDataset("bias.wcs", collections=collection, dataId=parentRefResolved.dataId) 

649 self.assertEqual(childRef1, parentRefResolved.makeComponentRef("wcs")) 

650 # Search for detector data IDs constrained by component dataset 

651 # existence with queryDataIds. 

652 dataIds = registry.queryDataIds( 

653 ["detector"], 

654 datasets=["bias.wcs"], 

655 collections=collection, 

656 ).toSet() 

657 self.assertEqual( 

658 dataIds, 

659 DataCoordinateSet( 

660 { 

661 DataCoordinate.standardize(instrument="Cam1", detector=d, graph=parentType.dimensions) 

662 for d in (1, 2, 3) 

663 }, 

664 parentType.dimensions, 

665 ), 

666 ) 

667 # Search for multiple datasets of a single type with queryDatasets. 

668 childRefs2 = set( 

669 registry.queryDatasets( 

670 "bias.wcs", 

671 collections=collection, 

672 ) 

673 ) 

674 self.assertEqual( 

675 {ref.unresolved() for ref in childRefs2}, {DatasetRef(childType, dataId) for dataId in dataIds} 

676 ) 

677 

678 def testCollections(self): 

679 """Tests for registry methods that manage collections.""" 

680 registry = self.makeRegistry() 

681 other_registry = self.makeRegistry(share_repo_with=registry) 

682 self.loadData(registry, "base.yaml") 

683 self.loadData(registry, "datasets.yaml") 

684 run1 = "imported_g" 

685 run2 = "imported_r" 

686 # Test setting a collection docstring after it has been created. 

687 registry.setCollectionDocumentation(run1, "doc for run1") 

688 self.assertEqual(registry.getCollectionDocumentation(run1), "doc for run1") 

689 registry.setCollectionDocumentation(run1, None) 

690 self.assertIsNone(registry.getCollectionDocumentation(run1)) 

691 datasetType = "bias" 

692 # Find some datasets via their run's collection. 

693 dataId1 = {"instrument": "Cam1", "detector": 1} 

694 ref1 = registry.findDataset(datasetType, dataId1, collections=run1) 

695 self.assertIsNotNone(ref1) 

696 dataId2 = {"instrument": "Cam1", "detector": 2} 

697 ref2 = registry.findDataset(datasetType, dataId2, collections=run1) 

698 self.assertIsNotNone(ref2) 

699 # Associate those into a new collection, then look for them there. 

700 tag1 = "tag1" 

701 registry.registerCollection(tag1, type=CollectionType.TAGGED, doc="doc for tag1") 

702 # Check that we can query for old and new collections by type. 

703 self.assertEqual(set(registry.queryCollections(collectionTypes=CollectionType.RUN)), {run1, run2}) 

704 self.assertEqual( 

705 set(registry.queryCollections(collectionTypes={CollectionType.TAGGED, CollectionType.RUN})), 

706 {tag1, run1, run2}, 

707 ) 

708 self.assertEqual(registry.getCollectionDocumentation(tag1), "doc for tag1") 

709 registry.associate(tag1, [ref1, ref2]) 

710 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=tag1), ref1) 

711 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=tag1), ref2) 

712 # Disassociate one and verify that we can't it there anymore... 

713 registry.disassociate(tag1, [ref1]) 

714 self.assertIsNone(registry.findDataset(datasetType, dataId1, collections=tag1)) 

715 # ...but we can still find ref2 in tag1, and ref1 in the run. 

716 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=run1), ref1) 

717 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=tag1), ref2) 

718 collections = set(registry.queryCollections()) 

719 self.assertEqual(collections, {run1, run2, tag1}) 

720 # Associate both refs into tag1 again; ref2 is already there, but that 

721 # should be a harmless no-op. 

722 registry.associate(tag1, [ref1, ref2]) 

723 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=tag1), ref1) 

724 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=tag1), ref2) 

725 # Get a different dataset (from a different run) that has the same 

726 # dataset type and data ID as ref2. 

727 ref2b = registry.findDataset(datasetType, dataId2, collections=run2) 

728 self.assertNotEqual(ref2, ref2b) 

729 # Attempting to associate that into tag1 should be an error. 

730 with self.assertRaises(ConflictingDefinitionError): 

731 registry.associate(tag1, [ref2b]) 

732 # That error shouldn't have messed up what we had before. 

733 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=tag1), ref1) 

734 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=tag1), ref2) 

735 # Attempt to associate the conflicting dataset again, this time with 

736 # a dataset that isn't in the collection and won't cause a conflict. 

737 # Should also fail without modifying anything. 

738 dataId3 = {"instrument": "Cam1", "detector": 3} 

739 ref3 = registry.findDataset(datasetType, dataId3, collections=run1) 

740 with self.assertRaises(ConflictingDefinitionError): 

741 registry.associate(tag1, [ref3, ref2b]) 

742 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=tag1), ref1) 

743 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=tag1), ref2) 

744 self.assertIsNone(registry.findDataset(datasetType, dataId3, collections=tag1)) 

745 # Register a chained collection that searches [tag1, run2] 

746 chain1 = "chain1" 

747 registry.registerCollection(chain1, type=CollectionType.CHAINED) 

748 self.assertIs(registry.getCollectionType(chain1), CollectionType.CHAINED) 

749 # Chained collection exists, but has no collections in it. 

750 self.assertFalse(registry.getCollectionChain(chain1)) 

751 # If we query for all collections, we should get the chained collection 

752 # only if we don't ask to flatten it (i.e. yield only its children). 

753 self.assertEqual(set(registry.queryCollections(flattenChains=False)), {tag1, run1, run2, chain1}) 

754 self.assertEqual(set(registry.queryCollections(flattenChains=True)), {tag1, run1, run2}) 

755 # Attempt to set its child collections to something circular; that 

756 # should fail. 

757 with self.assertRaises(ValueError): 

758 registry.setCollectionChain(chain1, [tag1, chain1]) 

759 # Add the child collections. 

760 registry.setCollectionChain(chain1, [tag1, run2]) 

761 self.assertEqual(list(registry.getCollectionChain(chain1)), [tag1, run2]) 

762 self.assertEqual(registry.getCollectionParentChains(tag1), {chain1}) 

763 self.assertEqual(registry.getCollectionParentChains(run2), {chain1}) 

764 # Refresh the other registry that points to the same repo, and make 

765 # sure it can see the things we've done (note that this does require 

766 # an explicit refresh(); that's the documented behavior, because 

767 # caching is ~impossible otherwise). 

768 if other_registry is not None: 

769 other_registry.refresh() 

770 self.assertEqual(list(other_registry.getCollectionChain(chain1)), [tag1, run2]) 

771 self.assertEqual(other_registry.getCollectionParentChains(tag1), {chain1}) 

772 self.assertEqual(other_registry.getCollectionParentChains(run2), {chain1}) 

773 # Searching for dataId1 or dataId2 in the chain should return ref1 and 

774 # ref2, because both are in tag1. 

775 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=chain1), ref1) 

776 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=chain1), ref2) 

777 # Now disassociate ref2 from tag1. The search (for bias) with 

778 # dataId2 in chain1 should then: 

779 # 1. not find it in tag1 

780 # 2. find a different dataset in run2 

781 registry.disassociate(tag1, [ref2]) 

782 ref2b = registry.findDataset(datasetType, dataId2, collections=chain1) 

783 self.assertNotEqual(ref2b, ref2) 

784 self.assertEqual(ref2b, registry.findDataset(datasetType, dataId2, collections=run2)) 

785 # Define a new chain so we can test recursive chains. 

786 chain2 = "chain2" 

787 registry.registerCollection(chain2, type=CollectionType.CHAINED) 

788 registry.setCollectionChain(chain2, [run2, chain1]) 

789 self.assertEqual(registry.getCollectionParentChains(chain1), {chain2}) 

790 self.assertEqual(registry.getCollectionParentChains(run2), {chain1, chain2}) 

791 # Query for collections matching a regex. 

792 self.assertCountEqual( 

793 list(registry.queryCollections(re.compile("imported_."), flattenChains=False)), 

794 ["imported_r", "imported_g"], 

795 ) 

796 # Query for collections matching a regex or an explicit str. 

797 self.assertCountEqual( 

798 list(registry.queryCollections([re.compile("imported_."), "chain1"], flattenChains=False)), 

799 ["imported_r", "imported_g", "chain1"], 

800 ) 

801 # Search for bias with dataId1 should find it via tag1 in chain2, 

802 # recursing, because is not in run1. 

803 self.assertIsNone(registry.findDataset(datasetType, dataId1, collections=run2)) 

804 self.assertEqual(registry.findDataset(datasetType, dataId1, collections=chain2), ref1) 

805 # Search for bias with dataId2 should find it in run2 (ref2b). 

806 self.assertEqual(registry.findDataset(datasetType, dataId2, collections=chain2), ref2b) 

807 # Search for a flat that is in run2. That should not be found 

808 # at the front of chain2, because of the restriction to bias 

809 # on run2 there, but it should be found in at the end of chain1. 

810 dataId4 = {"instrument": "Cam1", "detector": 3, "physical_filter": "Cam1-R2"} 

811 ref4 = registry.findDataset("flat", dataId4, collections=run2) 

812 self.assertIsNotNone(ref4) 

813 self.assertEqual(ref4, registry.findDataset("flat", dataId4, collections=chain2)) 

814 # Deleting a collection that's part of a CHAINED collection is not 

815 # allowed, and is exception-safe. 

816 with self.assertRaises(Exception): 

817 registry.removeCollection(run2) 

818 self.assertEqual(registry.getCollectionType(run2), CollectionType.RUN) 

819 with self.assertRaises(Exception): 

820 registry.removeCollection(chain1) 

821 self.assertEqual(registry.getCollectionType(chain1), CollectionType.CHAINED) 

822 # Actually remove chain2, test that it's gone by asking for its type. 

823 registry.removeCollection(chain2) 

824 with self.assertRaises(MissingCollectionError): 

825 registry.getCollectionType(chain2) 

826 # Actually remove run2 and chain1, which should work now. 

827 registry.removeCollection(chain1) 

828 registry.removeCollection(run2) 

829 with self.assertRaises(MissingCollectionError): 

830 registry.getCollectionType(run2) 

831 with self.assertRaises(MissingCollectionError): 

832 registry.getCollectionType(chain1) 

833 # Remove tag1 as well, just to test that we can remove TAGGED 

834 # collections. 

835 registry.removeCollection(tag1) 

836 with self.assertRaises(MissingCollectionError): 

837 registry.getCollectionType(tag1) 

838 

839 def testCollectionChainFlatten(self): 

840 """Test that Registry.setCollectionChain obeys its 'flatten' option.""" 

841 registry = self.makeRegistry() 

842 registry.registerCollection("inner", CollectionType.CHAINED) 

843 registry.registerCollection("innermost", CollectionType.RUN) 

844 registry.setCollectionChain("inner", ["innermost"]) 

845 registry.registerCollection("outer", CollectionType.CHAINED) 

846 registry.setCollectionChain("outer", ["inner"], flatten=False) 

847 self.assertEqual(list(registry.getCollectionChain("outer")), ["inner"]) 

848 registry.setCollectionChain("outer", ["inner"], flatten=True) 

849 self.assertEqual(list(registry.getCollectionChain("outer")), ["innermost"]) 

850 

851 def testBasicTransaction(self): 

852 """Test that all operations within a single transaction block are 

853 rolled back if an exception propagates out of the block. 

854 """ 

855 registry = self.makeRegistry() 

856 storageClass = StorageClass("testDatasetType") 

857 registry.storageClasses.registerStorageClass(storageClass) 

858 with registry.transaction(): 

859 registry.insertDimensionData("instrument", {"name": "Cam1", "class_name": "A"}) 

860 with self.assertRaises(ValueError): 

861 with registry.transaction(): 

862 registry.insertDimensionData("instrument", {"name": "Cam2"}) 

863 raise ValueError("Oops, something went wrong") 

864 # Cam1 should exist 

865 self.assertEqual(registry.expandDataId(instrument="Cam1").records["instrument"].class_name, "A") 

866 # But Cam2 and Cam3 should both not exist 

867 with self.assertRaises(DataIdValueError): 

868 registry.expandDataId(instrument="Cam2") 

869 with self.assertRaises(DataIdValueError): 

870 registry.expandDataId(instrument="Cam3") 

871 

872 def testNestedTransaction(self): 

873 """Test that operations within a transaction block are not rolled back 

874 if an exception propagates out of an inner transaction block and is 

875 then caught. 

876 """ 

877 registry = self.makeRegistry() 

878 dimension = registry.dimensions["instrument"] 

879 dataId1 = {"instrument": "DummyCam"} 

880 dataId2 = {"instrument": "DummyCam2"} 

881 checkpointReached = False 

882 with registry.transaction(): 

883 # This should be added and (ultimately) committed. 

884 registry.insertDimensionData(dimension, dataId1) 

885 with self.assertRaises(sqlalchemy.exc.IntegrityError): 

886 with registry.transaction(savepoint=True): 

887 # This does not conflict, and should succeed (but not 

888 # be committed). 

889 registry.insertDimensionData(dimension, dataId2) 

890 checkpointReached = True 

891 # This should conflict and raise, triggerring a rollback 

892 # of the previous insertion within the same transaction 

893 # context, but not the original insertion in the outer 

894 # block. 

895 registry.insertDimensionData(dimension, dataId1) 

896 self.assertTrue(checkpointReached) 

897 self.assertIsNotNone(registry.expandDataId(dataId1, graph=dimension.graph)) 

898 with self.assertRaises(DataIdValueError): 

899 registry.expandDataId(dataId2, graph=dimension.graph) 

900 

901 def testInstrumentDimensions(self): 

902 """Test queries involving only instrument dimensions, with no joins to 

903 skymap.""" 

904 registry = self.makeRegistry() 

905 

906 # need a bunch of dimensions and datasets for test 

907 registry.insertDimensionData( 

908 "instrument", dict(name="DummyCam", visit_max=25, exposure_max=300, detector_max=6) 

909 ) 

910 registry.insertDimensionData( 

911 "physical_filter", 

912 dict(instrument="DummyCam", name="dummy_r", band="r"), 

913 dict(instrument="DummyCam", name="dummy_i", band="i"), 

914 ) 

915 registry.insertDimensionData( 

916 "detector", *[dict(instrument="DummyCam", id=i, full_name=str(i)) for i in range(1, 6)] 

917 ) 

918 registry.insertDimensionData( 

919 "visit_system", 

920 dict(instrument="DummyCam", id=1, name="default"), 

921 ) 

922 registry.insertDimensionData( 

923 "visit", 

924 dict(instrument="DummyCam", id=10, name="ten", physical_filter="dummy_i", visit_system=1), 

925 dict(instrument="DummyCam", id=11, name="eleven", physical_filter="dummy_r", visit_system=1), 

926 dict(instrument="DummyCam", id=20, name="twelve", physical_filter="dummy_r", visit_system=1), 

927 ) 

928 registry.insertDimensionData( 

929 "exposure", 

930 dict(instrument="DummyCam", id=100, obs_id="100", physical_filter="dummy_i"), 

931 dict(instrument="DummyCam", id=101, obs_id="101", physical_filter="dummy_i"), 

932 dict(instrument="DummyCam", id=110, obs_id="110", physical_filter="dummy_r"), 

933 dict(instrument="DummyCam", id=111, obs_id="111", physical_filter="dummy_r"), 

934 dict(instrument="DummyCam", id=200, obs_id="200", physical_filter="dummy_r"), 

935 dict(instrument="DummyCam", id=201, obs_id="201", physical_filter="dummy_r"), 

936 ) 

937 registry.insertDimensionData( 

938 "visit_definition", 

939 dict(instrument="DummyCam", exposure=100, visit_system=1, visit=10), 

940 dict(instrument="DummyCam", exposure=101, visit_system=1, visit=10), 

941 dict(instrument="DummyCam", exposure=110, visit_system=1, visit=11), 

942 dict(instrument="DummyCam", exposure=111, visit_system=1, visit=11), 

943 dict(instrument="DummyCam", exposure=200, visit_system=1, visit=20), 

944 dict(instrument="DummyCam", exposure=201, visit_system=1, visit=20), 

945 ) 

946 # dataset types 

947 run1 = "test1_r" 

948 run2 = "test2_r" 

949 tagged2 = "test2_t" 

950 registry.registerRun(run1) 

951 registry.registerRun(run2) 

952 registry.registerCollection(tagged2) 

953 storageClass = StorageClass("testDataset") 

954 registry.storageClasses.registerStorageClass(storageClass) 

955 rawType = DatasetType( 

956 name="RAW", 

957 dimensions=registry.dimensions.extract(("instrument", "exposure", "detector")), 

958 storageClass=storageClass, 

959 ) 

960 registry.registerDatasetType(rawType) 

961 calexpType = DatasetType( 

962 name="CALEXP", 

963 dimensions=registry.dimensions.extract(("instrument", "visit", "detector")), 

964 storageClass=storageClass, 

965 ) 

966 registry.registerDatasetType(calexpType) 

967 

968 # add pre-existing datasets 

969 for exposure in (100, 101, 110, 111): 

970 for detector in (1, 2, 3): 

971 # note that only 3 of 5 detectors have datasets 

972 dataId = dict(instrument="DummyCam", exposure=exposure, detector=detector) 

973 (ref,) = registry.insertDatasets(rawType, dataIds=[dataId], run=run1) 

974 # exposures 100 and 101 appear in both run1 and tagged2. 

975 # 100 has different datasets in the different collections 

976 # 101 has the same dataset in both collections. 

977 if exposure == 100: 

978 (ref,) = registry.insertDatasets(rawType, dataIds=[dataId], run=run2) 

979 if exposure in (100, 101): 

980 registry.associate(tagged2, [ref]) 

981 # Add pre-existing datasets to tagged2. 

982 for exposure in (200, 201): 

983 for detector in (3, 4, 5): 

984 # note that only 3 of 5 detectors have datasets 

985 dataId = dict(instrument="DummyCam", exposure=exposure, detector=detector) 

986 (ref,) = registry.insertDatasets(rawType, dataIds=[dataId], run=run2) 

987 registry.associate(tagged2, [ref]) 

988 

989 dimensions = DimensionGraph( 

990 registry.dimensions, dimensions=(rawType.dimensions.required | calexpType.dimensions.required) 

991 ) 

992 # Test that single dim string works as well as list of str 

993 rows = registry.queryDataIds("visit", datasets=rawType, collections=run1).expanded().toSet() 

994 rowsI = registry.queryDataIds(["visit"], datasets=rawType, collections=run1).expanded().toSet() 

995 self.assertEqual(rows, rowsI) 

996 # with empty expression 

997 rows = registry.queryDataIds(dimensions, datasets=rawType, collections=run1).expanded().toSet() 

998 self.assertEqual(len(rows), 4 * 3) # 4 exposures times 3 detectors 

999 for dataId in rows: 

1000 self.assertCountEqual(dataId.keys(), ("instrument", "detector", "exposure", "visit")) 

1001 packer1 = registry.dimensions.makePacker("visit_detector", dataId) 

1002 packer2 = registry.dimensions.makePacker("exposure_detector", dataId) 

1003 self.assertEqual( 

1004 packer1.unpack(packer1.pack(dataId)), 

1005 DataCoordinate.standardize(dataId, graph=packer1.dimensions), 

1006 ) 

1007 self.assertEqual( 

1008 packer2.unpack(packer2.pack(dataId)), 

1009 DataCoordinate.standardize(dataId, graph=packer2.dimensions), 

1010 ) 

1011 self.assertNotEqual(packer1.pack(dataId), packer2.pack(dataId)) 

1012 self.assertCountEqual(set(dataId["exposure"] for dataId in rows), (100, 101, 110, 111)) 

1013 self.assertCountEqual(set(dataId["visit"] for dataId in rows), (10, 11)) 

1014 self.assertCountEqual(set(dataId["detector"] for dataId in rows), (1, 2, 3)) 

1015 

1016 # second collection 

1017 rows = registry.queryDataIds(dimensions, datasets=rawType, collections=tagged2).toSet() 

1018 self.assertEqual(len(rows), 4 * 3) # 4 exposures times 3 detectors 

1019 for dataId in rows: 

1020 self.assertCountEqual(dataId.keys(), ("instrument", "detector", "exposure", "visit")) 

1021 self.assertCountEqual(set(dataId["exposure"] for dataId in rows), (100, 101, 200, 201)) 

1022 self.assertCountEqual(set(dataId["visit"] for dataId in rows), (10, 20)) 

1023 self.assertCountEqual(set(dataId["detector"] for dataId in rows), (1, 2, 3, 4, 5)) 

1024 

1025 # with two input datasets 

1026 rows = registry.queryDataIds(dimensions, datasets=rawType, collections=[run1, tagged2]).toSet() 

1027 self.assertEqual(len(set(rows)), 6 * 3) # 6 exposures times 3 detectors; set needed to de-dupe 

1028 for dataId in rows: 

1029 self.assertCountEqual(dataId.keys(), ("instrument", "detector", "exposure", "visit")) 

1030 self.assertCountEqual(set(dataId["exposure"] for dataId in rows), (100, 101, 110, 111, 200, 201)) 

1031 self.assertCountEqual(set(dataId["visit"] for dataId in rows), (10, 11, 20)) 

1032 self.assertCountEqual(set(dataId["detector"] for dataId in rows), (1, 2, 3, 4, 5)) 

1033 

1034 # limit to single visit 

1035 rows = registry.queryDataIds( 

1036 dimensions, datasets=rawType, collections=run1, where="visit = 10", instrument="DummyCam" 

1037 ).toSet() 

1038 self.assertEqual(len(rows), 2 * 3) # 2 exposures times 3 detectors 

1039 self.assertCountEqual(set(dataId["exposure"] for dataId in rows), (100, 101)) 

1040 self.assertCountEqual(set(dataId["visit"] for dataId in rows), (10,)) 

1041 self.assertCountEqual(set(dataId["detector"] for dataId in rows), (1, 2, 3)) 

1042 

1043 # more limiting expression, using link names instead of Table.column 

1044 rows = registry.queryDataIds( 

1045 dimensions, 

1046 datasets=rawType, 

1047 collections=run1, 

1048 where="visit = 10 and detector > 1 and 'DummyCam'=instrument", 

1049 ).toSet() 

1050 self.assertEqual(len(rows), 2 * 2) # 2 exposures times 2 detectors 

1051 self.assertCountEqual(set(dataId["exposure"] for dataId in rows), (100, 101)) 

1052 self.assertCountEqual(set(dataId["visit"] for dataId in rows), (10,)) 

1053 self.assertCountEqual(set(dataId["detector"] for dataId in rows), (2, 3)) 

1054 

1055 # queryDataIds with only one of `datasets` and `collections` is an 

1056 # error. 

1057 with self.assertRaises(CollectionError): 

1058 registry.queryDataIds(dimensions, datasets=rawType) 

1059 with self.assertRaises(ArgumentError): 

1060 registry.queryDataIds(dimensions, collections=run1) 

1061 

1062 # expression excludes everything 

1063 rows = registry.queryDataIds( 

1064 dimensions, datasets=rawType, collections=run1, where="visit > 1000", instrument="DummyCam" 

1065 ).toSet() 

1066 self.assertEqual(len(rows), 0) 

1067 

1068 # Selecting by physical_filter, this is not in the dimensions, but it 

1069 # is a part of the full expression so it should work too. 

1070 rows = registry.queryDataIds( 

1071 dimensions, 

1072 datasets=rawType, 

1073 collections=run1, 

1074 where="physical_filter = 'dummy_r'", 

1075 instrument="DummyCam", 

1076 ).toSet() 

1077 self.assertEqual(len(rows), 2 * 3) # 2 exposures times 3 detectors 

1078 self.assertCountEqual(set(dataId["exposure"] for dataId in rows), (110, 111)) 

1079 self.assertCountEqual(set(dataId["visit"] for dataId in rows), (11,)) 

1080 self.assertCountEqual(set(dataId["detector"] for dataId in rows), (1, 2, 3)) 

1081 

1082 def testSkyMapDimensions(self): 

1083 """Tests involving only skymap dimensions, no joins to instrument.""" 

1084 registry = self.makeRegistry() 

1085 

1086 # need a bunch of dimensions and datasets for test, we want 

1087 # "band" in the test so also have to add physical_filter 

1088 # dimensions 

1089 registry.insertDimensionData("instrument", dict(instrument="DummyCam")) 

1090 registry.insertDimensionData( 

1091 "physical_filter", 

1092 dict(instrument="DummyCam", name="dummy_r", band="r"), 

1093 dict(instrument="DummyCam", name="dummy_i", band="i"), 

1094 ) 

1095 registry.insertDimensionData("skymap", dict(name="DummyMap", hash="sha!".encode("utf8"))) 

1096 for tract in range(10): 

1097 registry.insertDimensionData("tract", dict(skymap="DummyMap", id=tract)) 

1098 registry.insertDimensionData( 

1099 "patch", 

1100 *[dict(skymap="DummyMap", tract=tract, id=patch, cell_x=0, cell_y=0) for patch in range(10)], 

1101 ) 

1102 

1103 # dataset types 

1104 run = "tésτ" 

1105 registry.registerRun(run) 

1106 storageClass = StorageClass("testDataset") 

1107 registry.storageClasses.registerStorageClass(storageClass) 

1108 calexpType = DatasetType( 

1109 name="deepCoadd_calexp", 

1110 dimensions=registry.dimensions.extract(("skymap", "tract", "patch", "band")), 

1111 storageClass=storageClass, 

1112 ) 

1113 registry.registerDatasetType(calexpType) 

1114 mergeType = DatasetType( 

1115 name="deepCoadd_mergeDet", 

1116 dimensions=registry.dimensions.extract(("skymap", "tract", "patch")), 

1117 storageClass=storageClass, 

1118 ) 

1119 registry.registerDatasetType(mergeType) 

1120 measType = DatasetType( 

1121 name="deepCoadd_meas", 

1122 dimensions=registry.dimensions.extract(("skymap", "tract", "patch", "band")), 

1123 storageClass=storageClass, 

1124 ) 

1125 registry.registerDatasetType(measType) 

1126 

1127 dimensions = DimensionGraph( 

1128 registry.dimensions, 

1129 dimensions=( 

1130 calexpType.dimensions.required | mergeType.dimensions.required | measType.dimensions.required 

1131 ), 

1132 ) 

1133 

1134 # add pre-existing datasets 

1135 for tract in (1, 3, 5): 

1136 for patch in (2, 4, 6, 7): 

1137 dataId = dict(skymap="DummyMap", tract=tract, patch=patch) 

1138 registry.insertDatasets(mergeType, dataIds=[dataId], run=run) 

1139 for aFilter in ("i", "r"): 

1140 dataId = dict(skymap="DummyMap", tract=tract, patch=patch, band=aFilter) 

1141 registry.insertDatasets(calexpType, dataIds=[dataId], run=run) 

1142 

1143 # with empty expression 

1144 rows = registry.queryDataIds(dimensions, datasets=[calexpType, mergeType], collections=run).toSet() 

1145 self.assertEqual(len(rows), 3 * 4 * 2) # 4 tracts x 4 patches x 2 filters 

1146 for dataId in rows: 

1147 self.assertCountEqual(dataId.keys(), ("skymap", "tract", "patch", "band")) 

1148 self.assertCountEqual(set(dataId["tract"] for dataId in rows), (1, 3, 5)) 

1149 self.assertCountEqual(set(dataId["patch"] for dataId in rows), (2, 4, 6, 7)) 

1150 self.assertCountEqual(set(dataId["band"] for dataId in rows), ("i", "r")) 

1151 

1152 # limit to 2 tracts and 2 patches 

1153 rows = registry.queryDataIds( 

1154 dimensions, 

1155 datasets=[calexpType, mergeType], 

1156 collections=run, 

1157 where="tract IN (1, 5) AND patch IN (2, 7)", 

1158 skymap="DummyMap", 

1159 ).toSet() 

1160 self.assertEqual(len(rows), 2 * 2 * 2) # 2 tracts x 2 patches x 2 filters 

1161 self.assertCountEqual(set(dataId["tract"] for dataId in rows), (1, 5)) 

1162 self.assertCountEqual(set(dataId["patch"] for dataId in rows), (2, 7)) 

1163 self.assertCountEqual(set(dataId["band"] for dataId in rows), ("i", "r")) 

1164 

1165 # limit to single filter 

1166 rows = registry.queryDataIds( 

1167 dimensions, datasets=[calexpType, mergeType], collections=run, where="band = 'i'" 

1168 ).toSet() 

1169 self.assertEqual(len(rows), 3 * 4 * 1) # 4 tracts x 4 patches x 2 filters 

1170 self.assertCountEqual(set(dataId["tract"] for dataId in rows), (1, 3, 5)) 

1171 self.assertCountEqual(set(dataId["patch"] for dataId in rows), (2, 4, 6, 7)) 

1172 self.assertCountEqual(set(dataId["band"] for dataId in rows), ("i",)) 

1173 

1174 # Specifying non-existing skymap is an exception 

1175 with self.assertRaisesRegex(DataIdValueError, "Unknown values specified for governor dimension"): 

1176 rows = registry.queryDataIds( 

1177 dimensions, datasets=[calexpType, mergeType], collections=run, where="skymap = 'Mars'" 

1178 ).toSet() 

1179 

1180 def testSpatialJoin(self): 

1181 """Test queries that involve spatial overlap joins.""" 

1182 registry = self.makeRegistry() 

1183 self.loadData(registry, "hsc-rc2-subset.yaml") 

1184 

1185 # Dictionary of spatial DatabaseDimensionElements, keyed by the name of 

1186 # the TopologicalFamily they belong to. We'll relate all elements in 

1187 # each family to all of the elements in each other family. 

1188 families = defaultdict(set) 

1189 # Dictionary of {element.name: {dataId: region}}. 

1190 regions = {} 

1191 for element in registry.dimensions.getDatabaseElements(): 

1192 if element.spatial is not None: 

1193 families[element.spatial.name].add(element) 

1194 regions[element.name] = { 

1195 record.dataId: record.region for record in registry.queryDimensionRecords(element) 

1196 } 

1197 

1198 # If this check fails, it's not necessarily a problem - it may just be 

1199 # a reasonable change to the default dimension definitions - but the 

1200 # test below depends on there being more than one family to do anything 

1201 # useful. 

1202 self.assertEqual(len(families), 2) 

1203 

1204 # Overlap DatabaseDimensionElements with each other. 

1205 for family1, family2 in itertools.combinations(families, 2): 

1206 for element1, element2 in itertools.product(families[family1], families[family2]): 

1207 graph = DimensionGraph.union(element1.graph, element2.graph) 

1208 # Construct expected set of overlapping data IDs via a 

1209 # brute-force comparison of the regions we've already fetched. 

1210 expected = { 

1211 DataCoordinate.standardize({**dataId1.byName(), **dataId2.byName()}, graph=graph) 

1212 for (dataId1, region1), (dataId2, region2) in itertools.product( 

1213 regions[element1.name].items(), regions[element2.name].items() 

1214 ) 

1215 if not region1.isDisjointFrom(region2) 

1216 } 

1217 self.assertGreater(len(expected), 2, msg="Test that we aren't just comparing empty sets.") 

1218 queried = set(registry.queryDataIds(graph)) 

1219 self.assertEqual(expected, queried) 

1220 

1221 # Overlap each DatabaseDimensionElement with the commonSkyPix system. 

1222 commonSkyPix = registry.dimensions.commonSkyPix 

1223 for elementName, regions in regions.items(): 

1224 graph = DimensionGraph.union(registry.dimensions[elementName].graph, commonSkyPix.graph) 

1225 expected = set() 

1226 for dataId, region in regions.items(): 

1227 for begin, end in commonSkyPix.pixelization.envelope(region): 

1228 expected.update( 

1229 DataCoordinate.standardize({commonSkyPix.name: index, **dataId.byName()}, graph=graph) 

1230 for index in range(begin, end) 

1231 ) 

1232 self.assertGreater(len(expected), 2, msg="Test that we aren't just comparing empty sets.") 

1233 queried = set(registry.queryDataIds(graph)) 

1234 self.assertEqual(expected, queried) 

1235 

1236 def testAbstractQuery(self): 

1237 """Test that we can run a query that just lists the known 

1238 bands. This is tricky because band is 

1239 backed by a query against physical_filter. 

1240 """ 

1241 registry = self.makeRegistry() 

1242 registry.insertDimensionData("instrument", dict(name="DummyCam")) 

1243 registry.insertDimensionData( 

1244 "physical_filter", 

1245 dict(instrument="DummyCam", name="dummy_i", band="i"), 

1246 dict(instrument="DummyCam", name="dummy_i2", band="i"), 

1247 dict(instrument="DummyCam", name="dummy_r", band="r"), 

1248 ) 

1249 rows = registry.queryDataIds(["band"]).toSet() 

1250 self.assertCountEqual( 

1251 rows, 

1252 [ 

1253 DataCoordinate.standardize(band="i", universe=registry.dimensions), 

1254 DataCoordinate.standardize(band="r", universe=registry.dimensions), 

1255 ], 

1256 ) 

1257 

1258 def testAttributeManager(self): 

1259 """Test basic functionality of attribute manager.""" 

1260 # number of attributes with schema versions in a fresh database, 

1261 # 6 managers with 3 records per manager, plus config for dimensions 

1262 VERSION_COUNT = 6 * 3 + 1 

1263 

1264 registry = self.makeRegistry() 

1265 attributes = registry._managers.attributes 

1266 

1267 # check what get() returns for non-existing key 

1268 self.assertIsNone(attributes.get("attr")) 

1269 self.assertEqual(attributes.get("attr", ""), "") 

1270 self.assertEqual(attributes.get("attr", "Value"), "Value") 

1271 self.assertEqual(len(list(attributes.items())), VERSION_COUNT) 

1272 

1273 # cannot store empty key or value 

1274 with self.assertRaises(ValueError): 

1275 attributes.set("", "value") 

1276 with self.assertRaises(ValueError): 

1277 attributes.set("attr", "") 

1278 

1279 # set value of non-existing key 

1280 attributes.set("attr", "value") 

1281 self.assertEqual(len(list(attributes.items())), VERSION_COUNT + 1) 

1282 self.assertEqual(attributes.get("attr"), "value") 

1283 

1284 # update value of existing key 

1285 with self.assertRaises(ButlerAttributeExistsError): 

1286 attributes.set("attr", "value2") 

1287 

1288 attributes.set("attr", "value2", force=True) 

1289 self.assertEqual(len(list(attributes.items())), VERSION_COUNT + 1) 

1290 self.assertEqual(attributes.get("attr"), "value2") 

1291 

1292 # delete existing key 

1293 self.assertTrue(attributes.delete("attr")) 

1294 self.assertEqual(len(list(attributes.items())), VERSION_COUNT) 

1295 

1296 # delete non-existing key 

1297 self.assertFalse(attributes.delete("non-attr")) 

1298 

1299 # store bunch of keys and get the list back 

1300 data = [ 

1301 ("version.core", "1.2.3"), 

1302 ("version.dimensions", "3.2.1"), 

1303 ("config.managers.opaque", "ByNameOpaqueTableStorageManager"), 

1304 ] 

1305 for key, value in data: 

1306 attributes.set(key, value) 

1307 items = dict(attributes.items()) 

1308 for key, value in data: 

1309 self.assertEqual(items[key], value) 

1310 

1311 def testQueryDatasetsDeduplication(self): 

1312 """Test that the findFirst option to queryDatasets selects datasets 

1313 from collections in the order given". 

1314 """ 

1315 registry = self.makeRegistry() 

1316 self.loadData(registry, "base.yaml") 

1317 self.loadData(registry, "datasets.yaml") 

1318 self.assertCountEqual( 

1319 list(registry.queryDatasets("bias", collections=["imported_g", "imported_r"])), 

1320 [ 

1321 registry.findDataset("bias", instrument="Cam1", detector=1, collections="imported_g"), 

1322 registry.findDataset("bias", instrument="Cam1", detector=2, collections="imported_g"), 

1323 registry.findDataset("bias", instrument="Cam1", detector=3, collections="imported_g"), 

1324 registry.findDataset("bias", instrument="Cam1", detector=2, collections="imported_r"), 

1325 registry.findDataset("bias", instrument="Cam1", detector=3, collections="imported_r"), 

1326 registry.findDataset("bias", instrument="Cam1", detector=4, collections="imported_r"), 

1327 ], 

1328 ) 

1329 self.assertCountEqual( 

1330 list(registry.queryDatasets("bias", collections=["imported_g", "imported_r"], findFirst=True)), 

1331 [ 

1332 registry.findDataset("bias", instrument="Cam1", detector=1, collections="imported_g"), 

1333 registry.findDataset("bias", instrument="Cam1", detector=2, collections="imported_g"), 

1334 registry.findDataset("bias", instrument="Cam1", detector=3, collections="imported_g"), 

1335 registry.findDataset("bias", instrument="Cam1", detector=4, collections="imported_r"), 

1336 ], 

1337 ) 

1338 self.assertCountEqual( 

1339 list(registry.queryDatasets("bias", collections=["imported_r", "imported_g"], findFirst=True)), 

1340 [ 

1341 registry.findDataset("bias", instrument="Cam1", detector=1, collections="imported_g"), 

1342 registry.findDataset("bias", instrument="Cam1", detector=2, collections="imported_r"), 

1343 registry.findDataset("bias", instrument="Cam1", detector=3, collections="imported_r"), 

1344 registry.findDataset("bias", instrument="Cam1", detector=4, collections="imported_r"), 

1345 ], 

1346 ) 

1347 

1348 def testQueryResults(self): 

1349 """Test querying for data IDs and then manipulating the QueryResults 

1350 object returned to perform other queries. 

1351 """ 

1352 registry = self.makeRegistry() 

1353 self.loadData(registry, "base.yaml") 

1354 self.loadData(registry, "datasets.yaml") 

1355 bias = registry.getDatasetType("bias") 

1356 flat = registry.getDatasetType("flat") 

1357 # Obtain expected results from methods other than those we're testing 

1358 # here. That includes: 

1359 # - the dimensions of the data IDs we want to query: 

1360 expectedGraph = DimensionGraph(registry.dimensions, names=["detector", "physical_filter"]) 

1361 # - the dimensions of some other data IDs we'll extract from that: 

1362 expectedSubsetGraph = DimensionGraph(registry.dimensions, names=["detector"]) 

1363 # - the data IDs we expect to obtain from the first queries: 

1364 expectedDataIds = DataCoordinateSet( 

1365 { 

1366 DataCoordinate.standardize( 

1367 instrument="Cam1", detector=d, physical_filter=p, universe=registry.dimensions 

1368 ) 

1369 for d, p in itertools.product({1, 2, 3}, {"Cam1-G", "Cam1-R1", "Cam1-R2"}) 

1370 }, 

1371 graph=expectedGraph, 

1372 hasFull=False, 

1373 hasRecords=False, 

1374 ) 

1375 # - the flat datasets we expect to find from those data IDs, in just 

1376 # one collection (so deduplication is irrelevant): 

1377 expectedFlats = [ 

1378 registry.findDataset( 

1379 flat, instrument="Cam1", detector=1, physical_filter="Cam1-R1", collections="imported_r" 

1380 ), 

1381 registry.findDataset( 

1382 flat, instrument="Cam1", detector=2, physical_filter="Cam1-R1", collections="imported_r" 

1383 ), 

1384 registry.findDataset( 

1385 flat, instrument="Cam1", detector=3, physical_filter="Cam1-R2", collections="imported_r" 

1386 ), 

1387 ] 

1388 # - the data IDs we expect to extract from that: 

1389 expectedSubsetDataIds = expectedDataIds.subset(expectedSubsetGraph) 

1390 # - the bias datasets we expect to find from those data IDs, after we 

1391 # subset-out the physical_filter dimension, both with duplicates: 

1392 expectedAllBiases = [ 

1393 registry.findDataset(bias, instrument="Cam1", detector=1, collections="imported_g"), 

1394 registry.findDataset(bias, instrument="Cam1", detector=2, collections="imported_g"), 

1395 registry.findDataset(bias, instrument="Cam1", detector=3, collections="imported_g"), 

1396 registry.findDataset(bias, instrument="Cam1", detector=2, collections="imported_r"), 

1397 registry.findDataset(bias, instrument="Cam1", detector=3, collections="imported_r"), 

1398 ] 

1399 # - ...and without duplicates: 

1400 expectedDeduplicatedBiases = [ 

1401 registry.findDataset(bias, instrument="Cam1", detector=1, collections="imported_g"), 

1402 registry.findDataset(bias, instrument="Cam1", detector=2, collections="imported_r"), 

1403 registry.findDataset(bias, instrument="Cam1", detector=3, collections="imported_r"), 

1404 ] 

1405 # Test against those expected results, using a "lazy" query for the 

1406 # data IDs (which re-executes that query each time we use it to do 

1407 # something new). 

1408 dataIds = registry.queryDataIds( 

1409 ["detector", "physical_filter"], 

1410 where="detector.purpose = 'SCIENCE'", # this rejects detector=4 

1411 instrument="Cam1", 

1412 ) 

1413 self.assertEqual(dataIds.graph, expectedGraph) 

1414 self.assertEqual(dataIds.toSet(), expectedDataIds) 

1415 self.assertCountEqual( 

1416 list( 

1417 dataIds.findDatasets( 

1418 flat, 

1419 collections=["imported_r"], 

1420 ) 

1421 ), 

1422 expectedFlats, 

1423 ) 

1424 subsetDataIds = dataIds.subset(expectedSubsetGraph, unique=True) 

1425 self.assertEqual(subsetDataIds.graph, expectedSubsetGraph) 

1426 self.assertEqual(subsetDataIds.toSet(), expectedSubsetDataIds) 

1427 self.assertCountEqual( 

1428 list(subsetDataIds.findDatasets(bias, collections=["imported_r", "imported_g"], findFirst=False)), 

1429 expectedAllBiases, 

1430 ) 

1431 self.assertCountEqual( 

1432 list(subsetDataIds.findDatasets(bias, collections=["imported_r", "imported_g"], findFirst=True)), 

1433 expectedDeduplicatedBiases, 

1434 ) 

1435 # Materialize the bias dataset queries (only) by putting the results 

1436 # into temporary tables, then repeat those tests. 

1437 with subsetDataIds.findDatasets( 

1438 bias, collections=["imported_r", "imported_g"], findFirst=False 

1439 ).materialize() as biases: 

1440 self.assertCountEqual(list(biases), expectedAllBiases) 

1441 with subsetDataIds.findDatasets( 

1442 bias, collections=["imported_r", "imported_g"], findFirst=True 

1443 ).materialize() as biases: 

1444 self.assertCountEqual(list(biases), expectedDeduplicatedBiases) 

1445 # Materialize the data ID subset query, but not the dataset queries. 

1446 with subsetDataIds.materialize() as subsetDataIds: 

1447 self.assertEqual(subsetDataIds.graph, expectedSubsetGraph) 

1448 self.assertEqual(subsetDataIds.toSet(), expectedSubsetDataIds) 

1449 self.assertCountEqual( 

1450 list( 

1451 subsetDataIds.findDatasets( 

1452 bias, collections=["imported_r", "imported_g"], findFirst=False 

1453 ) 

1454 ), 

1455 expectedAllBiases, 

1456 ) 

1457 self.assertCountEqual( 

1458 list( 

1459 subsetDataIds.findDatasets(bias, collections=["imported_r", "imported_g"], findFirst=True) 

1460 ), 

1461 expectedDeduplicatedBiases, 

1462 ) 

1463 # Materialize the dataset queries, too. 

1464 with subsetDataIds.findDatasets( 

1465 bias, collections=["imported_r", "imported_g"], findFirst=False 

1466 ).materialize() as biases: 

1467 self.assertCountEqual(list(biases), expectedAllBiases) 

1468 with subsetDataIds.findDatasets( 

1469 bias, collections=["imported_r", "imported_g"], findFirst=True 

1470 ).materialize() as biases: 

1471 self.assertCountEqual(list(biases), expectedDeduplicatedBiases) 

1472 # Materialize the original query, but none of the follow-up queries. 

1473 with dataIds.materialize() as dataIds: 

1474 self.assertEqual(dataIds.graph, expectedGraph) 

1475 self.assertEqual(dataIds.toSet(), expectedDataIds) 

1476 self.assertCountEqual( 

1477 list( 

1478 dataIds.findDatasets( 

1479 flat, 

1480 collections=["imported_r"], 

1481 ) 

1482 ), 

1483 expectedFlats, 

1484 ) 

1485 subsetDataIds = dataIds.subset(expectedSubsetGraph, unique=True) 

1486 self.assertEqual(subsetDataIds.graph, expectedSubsetGraph) 

1487 self.assertEqual(subsetDataIds.toSet(), expectedSubsetDataIds) 

1488 self.assertCountEqual( 

1489 list( 

1490 subsetDataIds.findDatasets( 

1491 bias, collections=["imported_r", "imported_g"], findFirst=False 

1492 ) 

1493 ), 

1494 expectedAllBiases, 

1495 ) 

1496 self.assertCountEqual( 

1497 list( 

1498 subsetDataIds.findDatasets(bias, collections=["imported_r", "imported_g"], findFirst=True) 

1499 ), 

1500 expectedDeduplicatedBiases, 

1501 ) 

1502 # Materialize just the bias dataset queries. 

1503 with subsetDataIds.findDatasets( 

1504 bias, collections=["imported_r", "imported_g"], findFirst=False 

1505 ).materialize() as biases: 

1506 self.assertCountEqual(list(biases), expectedAllBiases) 

1507 with subsetDataIds.findDatasets( 

1508 bias, collections=["imported_r", "imported_g"], findFirst=True 

1509 ).materialize() as biases: 

1510 self.assertCountEqual(list(biases), expectedDeduplicatedBiases) 

1511 # Materialize the subset data ID query, but not the dataset 

1512 # queries. 

1513 with subsetDataIds.materialize() as subsetDataIds: 

1514 self.assertEqual(subsetDataIds.graph, expectedSubsetGraph) 

1515 self.assertEqual(subsetDataIds.toSet(), expectedSubsetDataIds) 

1516 self.assertCountEqual( 

1517 list( 

1518 subsetDataIds.findDatasets( 

1519 bias, collections=["imported_r", "imported_g"], findFirst=False 

1520 ) 

1521 ), 

1522 expectedAllBiases, 

1523 ) 

1524 self.assertCountEqual( 

1525 list( 

1526 subsetDataIds.findDatasets( 

1527 bias, collections=["imported_r", "imported_g"], findFirst=True 

1528 ) 

1529 ), 

1530 expectedDeduplicatedBiases, 

1531 ) 

1532 # Materialize the bias dataset queries, too, so now we're 

1533 # materializing every single step. 

1534 with subsetDataIds.findDatasets( 

1535 bias, collections=["imported_r", "imported_g"], findFirst=False 

1536 ).materialize() as biases: 

1537 self.assertCountEqual(list(biases), expectedAllBiases) 

1538 with subsetDataIds.findDatasets( 

1539 bias, collections=["imported_r", "imported_g"], findFirst=True 

1540 ).materialize() as biases: 

1541 self.assertCountEqual(list(biases), expectedDeduplicatedBiases) 

1542 

1543 def testEmptyDimensionsQueries(self): 

1544 """Test Query and QueryResults objects in the case where there are no 

1545 dimensions. 

1546 """ 

1547 # Set up test data: one dataset type, two runs, one dataset in each. 

1548 registry = self.makeRegistry() 

1549 self.loadData(registry, "base.yaml") 

1550 schema = DatasetType("schema", dimensions=registry.dimensions.empty, storageClass="Catalog") 

1551 registry.registerDatasetType(schema) 

1552 dataId = DataCoordinate.makeEmpty(registry.dimensions) 

1553 run1 = "run1" 

1554 run2 = "run2" 

1555 registry.registerRun(run1) 

1556 registry.registerRun(run2) 

1557 (dataset1,) = registry.insertDatasets(schema, dataIds=[dataId], run=run1) 

1558 (dataset2,) = registry.insertDatasets(schema, dataIds=[dataId], run=run2) 

1559 # Query directly for both of the datasets, and each one, one at a time. 

1560 self.checkQueryResults( 

1561 registry.queryDatasets(schema, collections=[run1, run2], findFirst=False), [dataset1, dataset2] 

1562 ) 

1563 self.checkQueryResults( 

1564 registry.queryDatasets(schema, collections=[run1, run2], findFirst=True), 

1565 [dataset1], 

1566 ) 

1567 self.checkQueryResults( 

1568 registry.queryDatasets(schema, collections=[run2, run1], findFirst=True), 

1569 [dataset2], 

1570 ) 

1571 # Query for data IDs with no dimensions. 

1572 dataIds = registry.queryDataIds([]) 

1573 self.checkQueryResults(dataIds, [dataId]) 

1574 # Use queried data IDs to find the datasets. 

1575 self.checkQueryResults( 

1576 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=False), 

1577 [dataset1, dataset2], 

1578 ) 

1579 self.checkQueryResults( 

1580 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=True), 

1581 [dataset1], 

1582 ) 

1583 self.checkQueryResults( 

1584 dataIds.findDatasets(schema, collections=[run2, run1], findFirst=True), 

1585 [dataset2], 

1586 ) 

1587 # Now materialize the data ID query results and repeat those tests. 

1588 with dataIds.materialize() as dataIds: 

1589 self.checkQueryResults(dataIds, [dataId]) 

1590 self.checkQueryResults( 

1591 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=True), 

1592 [dataset1], 

1593 ) 

1594 self.checkQueryResults( 

1595 dataIds.findDatasets(schema, collections=[run2, run1], findFirst=True), 

1596 [dataset2], 

1597 ) 

1598 # Query for non-empty data IDs, then subset that to get the empty one. 

1599 # Repeat the above tests starting from that. 

1600 dataIds = registry.queryDataIds(["instrument"]).subset(registry.dimensions.empty, unique=True) 

1601 self.checkQueryResults(dataIds, [dataId]) 

1602 self.checkQueryResults( 

1603 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=False), 

1604 [dataset1, dataset2], 

1605 ) 

1606 self.checkQueryResults( 

1607 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=True), 

1608 [dataset1], 

1609 ) 

1610 self.checkQueryResults( 

1611 dataIds.findDatasets(schema, collections=[run2, run1], findFirst=True), 

1612 [dataset2], 

1613 ) 

1614 with dataIds.materialize() as dataIds: 

1615 self.checkQueryResults(dataIds, [dataId]) 

1616 self.checkQueryResults( 

1617 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=False), 

1618 [dataset1, dataset2], 

1619 ) 

1620 self.checkQueryResults( 

1621 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=True), 

1622 [dataset1], 

1623 ) 

1624 self.checkQueryResults( 

1625 dataIds.findDatasets(schema, collections=[run2, run1], findFirst=True), 

1626 [dataset2], 

1627 ) 

1628 # Query for non-empty data IDs, then materialize, then subset to get 

1629 # the empty one. Repeat again. 

1630 with registry.queryDataIds(["instrument"]).materialize() as nonEmptyDataIds: 

1631 dataIds = nonEmptyDataIds.subset(registry.dimensions.empty, unique=True) 

1632 self.checkQueryResults(dataIds, [dataId]) 

1633 self.checkQueryResults( 

1634 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=False), 

1635 [dataset1, dataset2], 

1636 ) 

1637 self.checkQueryResults( 

1638 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=True), 

1639 [dataset1], 

1640 ) 

1641 self.checkQueryResults( 

1642 dataIds.findDatasets(schema, collections=[run2, run1], findFirst=True), 

1643 [dataset2], 

1644 ) 

1645 with dataIds.materialize() as dataIds: 

1646 self.checkQueryResults(dataIds, [dataId]) 

1647 self.checkQueryResults( 

1648 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=False), 

1649 [dataset1, dataset2], 

1650 ) 

1651 self.checkQueryResults( 

1652 dataIds.findDatasets(schema, collections=[run1, run2], findFirst=True), 

1653 [dataset1], 

1654 ) 

1655 self.checkQueryResults( 

1656 dataIds.findDatasets(schema, collections=[run2, run1], findFirst=True), 

1657 [dataset2], 

1658 ) 

1659 # Query for non-empty data IDs with a constraint on an empty-data-ID 

1660 # dataset that exists. 

1661 dataIds = registry.queryDataIds(["instrument"], datasets="schema", collections=...) 

1662 self.checkQueryResults( 

1663 dataIds.subset(unique=True), 

1664 [DataCoordinate.standardize(instrument="Cam1", universe=registry.dimensions)], 

1665 ) 

1666 # Again query for non-empty data IDs with a constraint on empty-data-ID 

1667 # datasets, but when the datasets don't exist. We delete the existing 

1668 # dataset and query just that collection rather than creating a new 

1669 # empty collection because this is a bit less likely for our build-time 

1670 # logic to shortcut-out (via the collection summaries), and such a 

1671 # shortcut would make this test a bit more trivial than we'd like. 

1672 registry.removeDatasets([dataset2]) 

1673 dataIds = registry.queryDataIds(["instrument"], datasets="schema", collections=run2) 

1674 self.checkQueryResults(dataIds, []) 

1675 

1676 def testDimensionDataModifications(self): 

1677 """Test that modifying dimension records via: 

1678 syncDimensionData(..., update=True) and 

1679 insertDimensionData(..., replace=True) works as expected, even in the 

1680 presence of datasets using those dimensions and spatial overlap 

1681 relationships. 

1682 """ 

1683 

1684 def unpack_range_set(ranges: lsst.sphgeom.RangeSet) -> Iterator[int]: 

1685 """Unpack a sphgeom.RangeSet into the integers it contains.""" 

1686 for begin, end in ranges: 

1687 yield from range(begin, end) 

1688 

1689 def range_set_hull( 

1690 ranges: lsst.sphgeom.RangeSet, 

1691 pixelization: lsst.sphgeom.HtmPixelization, 

1692 ) -> lsst.sphgeom.ConvexPolygon: 

1693 """Create a ConvexPolygon hull of the region defined by a set of 

1694 HTM pixelization index ranges. 

1695 """ 

1696 points = [] 

1697 for index in unpack_range_set(ranges): 

1698 points.extend(pixelization.triangle(index).getVertices()) 

1699 return lsst.sphgeom.ConvexPolygon(points) 

1700 

1701 # Use HTM to set up an initial parent region (one arbitrary trixel) 

1702 # and four child regions (the trixels within the parent at the next 

1703 # level. We'll use the parent as a tract/visit region and the children 

1704 # as its patch/visit_detector regions. 

1705 registry = self.makeRegistry() 

1706 htm6 = registry.dimensions.skypix["htm"][6].pixelization 

1707 commonSkyPix = registry.dimensions.commonSkyPix.pixelization 

1708 index = 12288 

1709 child_ranges_small = lsst.sphgeom.RangeSet(index).scaled(4) 

1710 assert htm6.universe().contains(child_ranges_small) 

1711 child_regions_small = [htm6.triangle(i) for i in unpack_range_set(child_ranges_small)] 

1712 parent_region_small = lsst.sphgeom.ConvexPolygon( 

1713 list(itertools.chain.from_iterable(c.getVertices() for c in child_regions_small)) 

1714 ) 

1715 assert all(parent_region_small.contains(c) for c in child_regions_small) 

1716 # Make a larger version of each child region, defined to be the set of 

1717 # htm6 trixels that overlap the original's bounding circle. Make a new 

1718 # parent that's the convex hull of the new children. 

1719 child_regions_large = [ 

1720 range_set_hull(htm6.envelope(c.getBoundingCircle()), htm6) for c in child_regions_small 

1721 ] 

1722 assert all(large.contains(small) for large, small in zip(child_regions_large, child_regions_small)) 

1723 parent_region_large = lsst.sphgeom.ConvexPolygon( 

1724 list(itertools.chain.from_iterable(c.getVertices() for c in child_regions_large)) 

1725 ) 

1726 assert all(parent_region_large.contains(c) for c in child_regions_large) 

1727 assert parent_region_large.contains(parent_region_small) 

1728 assert not parent_region_small.contains(parent_region_large) 

1729 assert not all(parent_region_small.contains(c) for c in child_regions_large) 

1730 # Find some commonSkyPix indices that overlap the large regions but not 

1731 # overlap the small regions. We use commonSkyPix here to make sure the 

1732 # real tests later involve what's in the database, not just post-query 

1733 # filtering of regions. 

1734 child_difference_indices = [] 

1735 for large, small in zip(child_regions_large, child_regions_small): 

1736 difference = list(unpack_range_set(commonSkyPix.envelope(large) - commonSkyPix.envelope(small))) 

1737 assert difference, "if this is empty, we can't test anything useful with these regions" 

1738 assert all( 

1739 not commonSkyPix.triangle(d).isDisjointFrom(large) 

1740 and commonSkyPix.triangle(d).isDisjointFrom(small) 

1741 for d in difference 

1742 ) 

1743 child_difference_indices.append(difference) 

1744 parent_difference_indices = list( 

1745 unpack_range_set( 

1746 commonSkyPix.envelope(parent_region_large) - commonSkyPix.envelope(parent_region_small) 

1747 ) 

1748 ) 

1749 assert parent_difference_indices, "if this is empty, we can't test anything useful with these regions" 

1750 assert all( 

1751 ( 

1752 not commonSkyPix.triangle(d).isDisjointFrom(parent_region_large) 

1753 and commonSkyPix.triangle(d).isDisjointFrom(parent_region_small) 

1754 ) 

1755 for d in parent_difference_indices 

1756 ) 

1757 # Now that we've finally got those regions, we'll insert the large ones 

1758 # as tract/patch dimension records. 

1759 skymap_name = "testing_v1" 

1760 registry.insertDimensionData( 

1761 "skymap", 

1762 { 

1763 "name": skymap_name, 

1764 "hash": bytes([42]), 

1765 "tract_max": 1, 

1766 "patch_nx_max": 2, 

1767 "patch_ny_max": 2, 

1768 }, 

1769 ) 

1770 registry.insertDimensionData("tract", {"skymap": skymap_name, "id": 0, "region": parent_region_large}) 

1771 registry.insertDimensionData( 

1772 "patch", 

1773 *[ 

1774 {"skymap": skymap_name, "tract": 0, "id": n, "cell_x": n % 2, "cell_y": n // 2, "region": c} 

1775 for n, c in enumerate(child_regions_large) 

1776 ], 

1777 ) 

1778 # Add at dataset that uses these dimensions to make sure that modifying 

1779 # them doesn't disrupt foreign keys (need to make sure DB doesn't 

1780 # implement insert with replace=True as delete-then-insert). 

1781 dataset_type = DatasetType( 

1782 "coadd", 

1783 dimensions=["tract", "patch"], 

1784 universe=registry.dimensions, 

1785 storageClass="Exposure", 

1786 ) 

1787 registry.registerDatasetType(dataset_type) 

1788 registry.registerCollection("the_run", CollectionType.RUN) 

1789 registry.insertDatasets( 

1790 dataset_type, 

1791 [{"skymap": skymap_name, "tract": 0, "patch": 2}], 

1792 run="the_run", 

1793 ) 

1794 # Query for tracts and patches that overlap some "difference" htm9 

1795 # pixels; there should be overlaps, because the database has 

1796 # the "large" suite of regions. 

1797 self.assertEqual( 

1798 {0}, 

1799 { 

1800 data_id["tract"] 

1801 for data_id in registry.queryDataIds( 

1802 ["tract"], 

1803 skymap=skymap_name, 

1804 dataId={registry.dimensions.commonSkyPix.name: parent_difference_indices[0]}, 

1805 ) 

1806 }, 

1807 ) 

1808 for patch_id, patch_difference_indices in enumerate(child_difference_indices): 

1809 self.assertIn( 

1810 patch_id, 

1811 { 

1812 data_id["patch"] 

1813 for data_id in registry.queryDataIds( 

1814 ["patch"], 

1815 skymap=skymap_name, 

1816 dataId={registry.dimensions.commonSkyPix.name: patch_difference_indices[0]}, 

1817 ) 

1818 }, 

1819 ) 

1820 # Use sync to update the tract region and insert to update the regions 

1821 # of the patches, to the "small" suite. 

1822 updated = registry.syncDimensionData( 

1823 "tract", 

1824 {"skymap": skymap_name, "id": 0, "region": parent_region_small}, 

1825 update=True, 

1826 ) 

1827 self.assertEqual(updated, {"region": parent_region_large}) 

1828 registry.insertDimensionData( 

1829 "patch", 

1830 *[ 

1831 {"skymap": skymap_name, "tract": 0, "id": n, "cell_x": n % 2, "cell_y": n // 2, "region": c} 

1832 for n, c in enumerate(child_regions_small) 

1833 ], 

1834 replace=True, 

1835 ) 

1836 # Query again; there now should be no such overlaps, because the 

1837 # database has the "small" suite of regions. 

1838 self.assertFalse( 

1839 set( 

1840 registry.queryDataIds( 

1841 ["tract"], 

1842 skymap=skymap_name, 

1843 dataId={registry.dimensions.commonSkyPix.name: parent_difference_indices[0]}, 

1844 ) 

1845 ) 

1846 ) 

1847 for patch_id, patch_difference_indices in enumerate(child_difference_indices): 

1848 self.assertNotIn( 

1849 patch_id, 

1850 { 

1851 data_id["patch"] 

1852 for data_id in registry.queryDataIds( 

1853 ["patch"], 

1854 skymap=skymap_name, 

1855 dataId={registry.dimensions.commonSkyPix.name: patch_difference_indices[0]}, 

1856 ) 

1857 }, 

1858 ) 

1859 # Update back to the large regions and query one more time. 

1860 updated = registry.syncDimensionData( 

1861 "tract", 

1862 {"skymap": skymap_name, "id": 0, "region": parent_region_large}, 

1863 update=True, 

1864 ) 

1865 self.assertEqual(updated, {"region": parent_region_small}) 

1866 registry.insertDimensionData( 

1867 "patch", 

1868 *[ 

1869 {"skymap": skymap_name, "tract": 0, "id": n, "cell_x": n % 2, "cell_y": n // 2, "region": c} 

1870 for n, c in enumerate(child_regions_large) 

1871 ], 

1872 replace=True, 

1873 ) 

1874 self.assertEqual( 

1875 {0}, 

1876 { 

1877 data_id["tract"] 

1878 for data_id in registry.queryDataIds( 

1879 ["tract"], 

1880 skymap=skymap_name, 

1881 dataId={registry.dimensions.commonSkyPix.name: parent_difference_indices[0]}, 

1882 ) 

1883 }, 

1884 ) 

1885 for patch_id, patch_difference_indices in enumerate(child_difference_indices): 

1886 self.assertIn( 

1887 patch_id, 

1888 { 

1889 data_id["patch"] 

1890 for data_id in registry.queryDataIds( 

1891 ["patch"], 

1892 skymap=skymap_name, 

1893 dataId={registry.dimensions.commonSkyPix.name: patch_difference_indices[0]}, 

1894 ) 

1895 }, 

1896 ) 

1897 

1898 def testCalibrationCollections(self): 

1899 """Test operations on `~CollectionType.CALIBRATION` collections, 

1900 including `Registry.certify`, `Registry.decertify`, and 

1901 `Registry.findDataset`. 

1902 """ 

1903 # Setup - make a Registry, fill it with some datasets in 

1904 # non-calibration collections. 

1905 registry = self.makeRegistry() 

1906 self.loadData(registry, "base.yaml") 

1907 self.loadData(registry, "datasets.yaml") 

1908 # Set up some timestamps. 

1909 t1 = astropy.time.Time("2020-01-01T01:00:00", format="isot", scale="tai") 

1910 t2 = astropy.time.Time("2020-01-01T02:00:00", format="isot", scale="tai") 

1911 t3 = astropy.time.Time("2020-01-01T03:00:00", format="isot", scale="tai") 

1912 t4 = astropy.time.Time("2020-01-01T04:00:00", format="isot", scale="tai") 

1913 t5 = astropy.time.Time("2020-01-01T05:00:00", format="isot", scale="tai") 

1914 allTimespans = [ 

1915 Timespan(a, b) for a, b in itertools.combinations([None, t1, t2, t3, t4, t5, None], r=2) 

1916 ] 

1917 # Get references to some datasets. 

1918 bias2a = registry.findDataset("bias", instrument="Cam1", detector=2, collections="imported_g") 

1919 bias3a = registry.findDataset("bias", instrument="Cam1", detector=3, collections="imported_g") 

1920 bias2b = registry.findDataset("bias", instrument="Cam1", detector=2, collections="imported_r") 

1921 bias3b = registry.findDataset("bias", instrument="Cam1", detector=3, collections="imported_r") 

1922 # Register the main calibration collection we'll be working with. 

1923 collection = "Cam1/calibs/default" 

1924 registry.registerCollection(collection, type=CollectionType.CALIBRATION) 

1925 # Cannot associate into a calibration collection (no timespan). 

1926 with self.assertRaises(CollectionTypeError): 

1927 registry.associate(collection, [bias2a]) 

1928 # Certify 2a dataset with [t2, t4) validity. 

1929 registry.certify(collection, [bias2a], Timespan(begin=t2, end=t4)) 

1930 # Test that we can query for this dataset via the new collection, both 

1931 # on its own and with a RUN collection, as long as we don't try to join 

1932 # in temporal dimensions or use findFirst=True. 

1933 self.assertEqual( 

1934 set(registry.queryDatasets("bias", findFirst=False, collections=collection)), 

1935 {bias2a}, 

1936 ) 

1937 self.assertEqual( 

1938 set(registry.queryDatasets("bias", findFirst=False, collections=[collection, "imported_r"])), 

1939 { 

1940 bias2a, 

1941 bias2b, 

1942 bias3b, 

1943 registry.findDataset("bias", instrument="Cam1", detector=4, collections="imported_r"), 

1944 }, 

1945 ) 

1946 self.assertEqual( 

1947 set(registry.queryDataIds("detector", datasets="bias", collections=collection)), 

1948 {registry.expandDataId(instrument="Cam1", detector=2)}, 

1949 ) 

1950 self.assertEqual( 

1951 set(registry.queryDataIds("detector", datasets="bias", collections=[collection, "imported_r"])), 

1952 { 

1953 registry.expandDataId(instrument="Cam1", detector=2), 

1954 registry.expandDataId(instrument="Cam1", detector=3), 

1955 registry.expandDataId(instrument="Cam1", detector=4), 

1956 }, 

1957 ) 

1958 

1959 # We should not be able to certify 2b with anything overlapping that 

1960 # window. 

1961 with self.assertRaises(ConflictingDefinitionError): 

1962 registry.certify(collection, [bias2b], Timespan(begin=None, end=t3)) 

1963 with self.assertRaises(ConflictingDefinitionError): 

1964 registry.certify(collection, [bias2b], Timespan(begin=None, end=t5)) 

1965 with self.assertRaises(ConflictingDefinitionError): 

1966 registry.certify(collection, [bias2b], Timespan(begin=t1, end=t3)) 

1967 with self.assertRaises(ConflictingDefinitionError): 

1968 registry.certify(collection, [bias2b], Timespan(begin=t1, end=t5)) 

1969 with self.assertRaises(ConflictingDefinitionError): 

1970 registry.certify(collection, [bias2b], Timespan(begin=t1, end=None)) 

1971 with self.assertRaises(ConflictingDefinitionError): 

1972 registry.certify(collection, [bias2b], Timespan(begin=t2, end=t3)) 

1973 with self.assertRaises(ConflictingDefinitionError): 

1974 registry.certify(collection, [bias2b], Timespan(begin=t2, end=t5)) 

1975 with self.assertRaises(ConflictingDefinitionError): 

1976 registry.certify(collection, [bias2b], Timespan(begin=t2, end=None)) 

1977 # We should be able to certify 3a with a range overlapping that window, 

1978 # because it's for a different detector. 

1979 # We'll certify 3a over [t1, t3). 

1980 registry.certify(collection, [bias3a], Timespan(begin=t1, end=t3)) 

1981 # Now we'll certify 2b and 3b together over [t4, ∞). 

1982 registry.certify(collection, [bias2b, bias3b], Timespan(begin=t4, end=None)) 

1983 

1984 # Fetch all associations and check that they are what we expect. 

1985 self.assertCountEqual( 

1986 list( 

1987 registry.queryDatasetAssociations( 

1988 "bias", 

1989 collections=[collection, "imported_g", "imported_r"], 

1990 ) 

1991 ), 

1992 [ 

1993 DatasetAssociation( 

1994 ref=registry.findDataset("bias", instrument="Cam1", detector=1, collections="imported_g"), 

1995 collection="imported_g", 

1996 timespan=None, 

1997 ), 

1998 DatasetAssociation( 

1999 ref=registry.findDataset("bias", instrument="Cam1", detector=4, collections="imported_r"), 

2000 collection="imported_r", 

2001 timespan=None, 

2002 ), 

2003 DatasetAssociation(ref=bias2a, collection="imported_g", timespan=None), 

2004 DatasetAssociation(ref=bias3a, collection="imported_g", timespan=None), 

2005 DatasetAssociation(ref=bias2b, collection="imported_r", timespan=None), 

2006 DatasetAssociation(ref=bias3b, collection="imported_r", timespan=None), 

2007 DatasetAssociation(ref=bias2a, collection=collection, timespan=Timespan(begin=t2, end=t4)), 

2008 DatasetAssociation(ref=bias3a, collection=collection, timespan=Timespan(begin=t1, end=t3)), 

2009 DatasetAssociation(ref=bias2b, collection=collection, timespan=Timespan(begin=t4, end=None)), 

2010 DatasetAssociation(ref=bias3b, collection=collection, timespan=Timespan(begin=t4, end=None)), 

2011 ], 

2012 ) 

2013 

2014 class Ambiguous: 

2015 """Tag class to denote lookups that should be ambiguous.""" 

2016 

2017 pass 

2018 

2019 def assertLookup( 

2020 detector: int, timespan: Timespan, expected: Optional[Union[DatasetRef, Type[Ambiguous]]] 

2021 ) -> None: 

2022 """Local function that asserts that a bias lookup returns the given 

2023 expected result. 

2024 """ 

2025 if expected is Ambiguous: 

2026 with self.assertRaises(RuntimeError): 

2027 registry.findDataset( 

2028 "bias", 

2029 collections=collection, 

2030 instrument="Cam1", 

2031 detector=detector, 

2032 timespan=timespan, 

2033 ) 

2034 else: 

2035 self.assertEqual( 

2036 expected, 

2037 registry.findDataset( 

2038 "bias", 

2039 collections=collection, 

2040 instrument="Cam1", 

2041 detector=detector, 

2042 timespan=timespan, 

2043 ), 

2044 ) 

2045 

2046 # Systematically test lookups against expected results. 

2047 assertLookup(detector=2, timespan=Timespan(None, t1), expected=None) 

2048 assertLookup(detector=2, timespan=Timespan(None, t2), expected=None) 

2049 assertLookup(detector=2, timespan=Timespan(None, t3), expected=bias2a) 

2050 assertLookup(detector=2, timespan=Timespan(None, t4), expected=bias2a) 

2051 assertLookup(detector=2, timespan=Timespan(None, t5), expected=Ambiguous) 

2052 assertLookup(detector=2, timespan=Timespan(None, None), expected=Ambiguous) 

2053 assertLookup(detector=2, timespan=Timespan(t1, t2), expected=None) 

2054 assertLookup(detector=2, timespan=Timespan(t1, t3), expected=bias2a) 

2055 assertLookup(detector=2, timespan=Timespan(t1, t4), expected=bias2a) 

2056 assertLookup(detector=2, timespan=Timespan(t1, t5), expected=Ambiguous) 

2057 assertLookup(detector=2, timespan=Timespan(t1, None), expected=Ambiguous) 

2058 assertLookup(detector=2, timespan=Timespan(t2, t3), expected=bias2a) 

2059 assertLookup(detector=2, timespan=Timespan(t2, t4), expected=bias2a) 

2060 assertLookup(detector=2, timespan=Timespan(t2, t5), expected=Ambiguous) 

2061 assertLookup(detector=2, timespan=Timespan(t2, None), expected=Ambiguous) 

2062 assertLookup(detector=2, timespan=Timespan(t3, t4), expected=bias2a) 

2063 assertLookup(detector=2, timespan=Timespan(t3, t5), expected=Ambiguous) 

2064 assertLookup(detector=2, timespan=Timespan(t3, None), expected=Ambiguous) 

2065 assertLookup(detector=2, timespan=Timespan(t4, t5), expected=bias2b) 

2066 assertLookup(detector=2, timespan=Timespan(t4, None), expected=bias2b) 

2067 assertLookup(detector=2, timespan=Timespan(t5, None), expected=bias2b) 

2068 assertLookup(detector=3, timespan=Timespan(None, t1), expected=None) 

2069 assertLookup(detector=3, timespan=Timespan(None, t2), expected=bias3a) 

2070 assertLookup(detector=3, timespan=Timespan(None, t3), expected=bias3a) 

2071 assertLookup(detector=3, timespan=Timespan(None, t4), expected=bias3a) 

2072 assertLookup(detector=3, timespan=Timespan(None, t5), expected=Ambiguous) 

2073 assertLookup(detector=3, timespan=Timespan(None, None), expected=Ambiguous) 

2074 assertLookup(detector=3, timespan=Timespan(t1, t2), expected=bias3a) 

2075 assertLookup(detector=3, timespan=Timespan(t1, t3), expected=bias3a) 

2076 assertLookup(detector=3, timespan=Timespan(t1, t4), expected=bias3a) 

2077 assertLookup(detector=3, timespan=Timespan(t1, t5), expected=Ambiguous) 

2078 assertLookup(detector=3, timespan=Timespan(t1, None), expected=Ambiguous) 

2079 assertLookup(detector=3, timespan=Timespan(t2, t3), expected=bias3a) 

2080 assertLookup(detector=3, timespan=Timespan(t2, t4), expected=bias3a) 

2081 assertLookup(detector=3, timespan=Timespan(t2, t5), expected=Ambiguous) 

2082 assertLookup(detector=3, timespan=Timespan(t2, None), expected=Ambiguous) 

2083 assertLookup(detector=3, timespan=Timespan(t3, t4), expected=None) 

2084 assertLookup(detector=3, timespan=Timespan(t3, t5), expected=bias3b) 

2085 assertLookup(detector=3, timespan=Timespan(t3, None), expected=bias3b) 

2086 assertLookup(detector=3, timespan=Timespan(t4, t5), expected=bias3b) 

2087 assertLookup(detector=3, timespan=Timespan(t4, None), expected=bias3b) 

2088 assertLookup(detector=3, timespan=Timespan(t5, None), expected=bias3b) 

2089 

2090 # Decertify [t3, t5) for all data IDs, and do test lookups again. 

2091 # This should truncate bias2a to [t2, t3), leave bias3a unchanged at 

2092 # [t1, t3), and truncate bias2b and bias3b to [t5, ∞). 

2093 registry.decertify(collection=collection, datasetType="bias", timespan=Timespan(t3, t5)) 

2094 assertLookup(detector=2, timespan=Timespan(None, t1), expected=None) 

2095 assertLookup(detector=2, timespan=Timespan(None, t2), expected=None) 

2096 assertLookup(detector=2, timespan=Timespan(None, t3), expected=bias2a) 

2097 assertLookup(detector=2, timespan=Timespan(None, t4), expected=bias2a) 

2098 assertLookup(detector=2, timespan=Timespan(None, t5), expected=bias2a) 

2099 assertLookup(detector=2, timespan=Timespan(None, None), expected=Ambiguous) 

2100 assertLookup(detector=2, timespan=Timespan(t1, t2), expected=None) 

2101 assertLookup(detector=2, timespan=Timespan(t1, t3), expected=bias2a) 

2102 assertLookup(detector=2, timespan=Timespan(t1, t4), expected=bias2a) 

2103 assertLookup(detector=2, timespan=Timespan(t1, t5), expected=bias2a) 

2104 assertLookup(detector=2, timespan=Timespan(t1, None), expected=Ambiguous) 

2105 assertLookup(detector=2, timespan=Timespan(t2, t3), expected=bias2a) 

2106 assertLookup(detector=2, timespan=Timespan(t2, t4), expected=bias2a) 

2107 assertLookup(detector=2, timespan=Timespan(t2, t5), expected=bias2a) 

2108 assertLookup(detector=2, timespan=Timespan(t2, None), expected=Ambiguous) 

2109 assertLookup(detector=2, timespan=Timespan(t3, t4), expected=None) 

2110 assertLookup(detector=2, timespan=Timespan(t3, t5), expected=None) 

2111 assertLookup(detector=2, timespan=Timespan(t3, None), expected=bias2b) 

2112 assertLookup(detector=2, timespan=Timespan(t4, t5), expected=None) 

2113 assertLookup(detector=2, timespan=Timespan(t4, None), expected=bias2b) 

2114 assertLookup(detector=2, timespan=Timespan(t5, None), expected=bias2b) 

2115 assertLookup(detector=3, timespan=Timespan(None, t1), expected=None) 

2116 assertLookup(detector=3, timespan=Timespan(None, t2), expected=bias3a) 

2117 assertLookup(detector=3, timespan=Timespan(None, t3), expected=bias3a) 

2118 assertLookup(detector=3, timespan=Timespan(None, t4), expected=bias3a) 

2119 assertLookup(detector=3, timespan=Timespan(None, t5), expected=bias3a) 

2120 assertLookup(detector=3, timespan=Timespan(None, None), expected=Ambiguous) 

2121 assertLookup(detector=3, timespan=Timespan(t1, t2), expected=bias3a) 

2122 assertLookup(detector=3, timespan=Timespan(t1, t3), expected=bias3a) 

2123 assertLookup(detector=3, timespan=Timespan(t1, t4), expected=bias3a) 

2124 assertLookup(detector=3, timespan=Timespan(t1, t5), expected=bias3a) 

2125 assertLookup(detector=3, timespan=Timespan(t1, None), expected=Ambiguous) 

2126 assertLookup(detector=3, timespan=Timespan(t2, t3), expected=bias3a) 

2127 assertLookup(detector=3, timespan=Timespan(t2, t4), expected=bias3a) 

2128 assertLookup(detector=3, timespan=Timespan(t2, t5), expected=bias3a) 

2129 assertLookup(detector=3, timespan=Timespan(t2, None), expected=Ambiguous) 

2130 assertLookup(detector=3, timespan=Timespan(t3, t4), expected=None) 

2131 assertLookup(detector=3, timespan=Timespan(t3, t5), expected=None) 

2132 assertLookup(detector=3, timespan=Timespan(t3, None), expected=bias3b) 

2133 assertLookup(detector=3, timespan=Timespan(t4, t5), expected=None) 

2134 assertLookup(detector=3, timespan=Timespan(t4, None), expected=bias3b) 

2135 assertLookup(detector=3, timespan=Timespan(t5, None), expected=bias3b) 

2136 

2137 # Decertify everything, this time with explicit data IDs, then check 

2138 # that no lookups succeed. 

2139 registry.decertify( 

2140 collection, 

2141 "bias", 

2142 Timespan(None, None), 

2143 dataIds=[ 

2144 dict(instrument="Cam1", detector=2), 

2145 dict(instrument="Cam1", detector=3), 

2146 ], 

2147 ) 

2148 for detector in (2, 3): 

2149 for timespan in allTimespans: 

2150 assertLookup(detector=detector, timespan=timespan, expected=None) 

2151 # Certify bias2a and bias3a over (-∞, ∞), check that all lookups return 

2152 # those. 

2153 registry.certify( 

2154 collection, 

2155 [bias2a, bias3a], 

2156 Timespan(None, None), 

2157 ) 

2158 for timespan in allTimespans: 

2159 assertLookup(detector=2, timespan=timespan, expected=bias2a) 

2160 assertLookup(detector=3, timespan=timespan, expected=bias3a) 

2161 # Decertify just bias2 over [t2, t4). 

2162 # This should split a single certification row into two (and leave the 

2163 # other existing row, for bias3a, alone). 

2164 registry.decertify( 

2165 collection, "bias", Timespan(t2, t4), dataIds=[dict(instrument="Cam1", detector=2)] 

2166 ) 

2167 for timespan in allTimespans: 

2168 assertLookup(detector=3, timespan=timespan, expected=bias3a) 

2169 overlapsBefore = timespan.overlaps(Timespan(None, t2)) 

2170 overlapsAfter = timespan.overlaps(Timespan(t4, None)) 

2171 if overlapsBefore and overlapsAfter: 

2172 expected = Ambiguous 

2173 elif overlapsBefore or overlapsAfter: 

2174 expected = bias2a 

2175 else: 

2176 expected = None 

2177 assertLookup(detector=2, timespan=timespan, expected=expected) 

2178 

2179 def testSkipCalibs(self): 

2180 """Test how queries handle skipping of calibration collections.""" 

2181 registry = self.makeRegistry() 

2182 self.loadData(registry, "base.yaml") 

2183 self.loadData(registry, "datasets.yaml") 

2184 

2185 coll_calib = "Cam1/calibs/default" 

2186 registry.registerCollection(coll_calib, type=CollectionType.CALIBRATION) 

2187 

2188 # Add all biases to the calibration collection. 

2189 # Without this, the logic that prunes dataset subqueries based on 

2190 # datasetType-collection summary information will fire before the logic 

2191 # we want to test below. This is a good thing (it avoids the dreaded 

2192 # NotImplementedError a bit more often) everywhere but here. 

2193 registry.certify(coll_calib, registry.queryDatasets("bias", collections=...), Timespan(None, None)) 

2194 

2195 coll_list = [coll_calib, "imported_g", "imported_r"] 

2196 chain = "Cam1/chain" 

2197 registry.registerCollection(chain, type=CollectionType.CHAINED) 

2198 registry.setCollectionChain(chain, coll_list) 

2199 

2200 # explicit list will raise if findFirst=True or there are temporal 

2201 # dimensions 

2202 with self.assertRaises(NotImplementedError): 

2203 registry.queryDatasets("bias", collections=coll_list, findFirst=True) 

2204 with self.assertRaises(NotImplementedError): 

2205 registry.queryDataIds( 

2206 ["instrument", "detector", "exposure"], datasets="bias", collections=coll_list 

2207 ).count() 

2208 

2209 # chain will skip 

2210 datasets = list(registry.queryDatasets("bias", collections=chain)) 

2211 self.assertGreater(len(datasets), 0) 

2212 

2213 dataIds = list(registry.queryDataIds(["instrument", "detector"], datasets="bias", collections=chain)) 

2214 self.assertGreater(len(dataIds), 0) 

2215 

2216 # glob will skip too 

2217 datasets = list(registry.queryDatasets("bias", collections="*d*")) 

2218 self.assertGreater(len(datasets), 0) 

2219 

2220 # regular expression will skip too 

2221 pattern = re.compile(".*") 

2222 datasets = list(registry.queryDatasets("bias", collections=pattern)) 

2223 self.assertGreater(len(datasets), 0) 

2224 

2225 # ellipsis should work as usual 

2226 datasets = list(registry.queryDatasets("bias", collections=...)) 

2227 self.assertGreater(len(datasets), 0) 

2228 

2229 # few tests with findFirst 

2230 datasets = list(registry.queryDatasets("bias", collections=chain, findFirst=True)) 

2231 self.assertGreater(len(datasets), 0) 

2232 

2233 def testIngestTimeQuery(self): 

2234 registry = self.makeRegistry() 

2235 self.loadData(registry, "base.yaml") 

2236 dt0 = datetime.utcnow() 

2237 self.loadData(registry, "datasets.yaml") 

2238 dt1 = datetime.utcnow() 

2239 

2240 datasets = list(registry.queryDatasets(..., collections=...)) 

2241 len0 = len(datasets) 

2242 self.assertGreater(len0, 0) 

2243 

2244 where = "ingest_date > T'2000-01-01'" 

2245 datasets = list(registry.queryDatasets(..., collections=..., where=where)) 

2246 len1 = len(datasets) 

2247 self.assertEqual(len0, len1) 

2248 

2249 # no one will ever use this piece of software in 30 years 

2250 where = "ingest_date > T'2050-01-01'" 

2251 datasets = list(registry.queryDatasets(..., collections=..., where=where)) 

2252 len2 = len(datasets) 

2253 self.assertEqual(len2, 0) 

2254 

2255 # Check more exact timing to make sure there is no 37 seconds offset 

2256 # (after fixing DM-30124). SQLite time precision is 1 second, make 

2257 # sure that we don't test with higher precision. 

2258 tests = [ 

2259 # format: (timestamp, operator, expected_len) 

2260 (dt0 - timedelta(seconds=1), ">", len0), 

2261 (dt0 - timedelta(seconds=1), "<", 0), 

2262 (dt1 + timedelta(seconds=1), "<", len0), 

2263 (dt1 + timedelta(seconds=1), ">", 0), 

2264 ] 

2265 for dt, op, expect_len in tests: 

2266 dt_str = dt.isoformat(sep=" ") 

2267 

2268 where = f"ingest_date {op} T'{dt_str}'" 

2269 datasets = list(registry.queryDatasets(..., collections=..., where=where)) 

2270 self.assertEqual(len(datasets), expect_len) 

2271 

2272 # same with bind using datetime or astropy Time 

2273 where = f"ingest_date {op} ingest_time" 

2274 datasets = list( 

2275 registry.queryDatasets(..., collections=..., where=where, bind={"ingest_time": dt}) 

2276 ) 

2277 self.assertEqual(len(datasets), expect_len) 

2278 

2279 dt_astropy = astropy.time.Time(dt, format="datetime") 

2280 datasets = list( 

2281 registry.queryDatasets(..., collections=..., where=where, bind={"ingest_time": dt_astropy}) 

2282 ) 

2283 self.assertEqual(len(datasets), expect_len) 

2284 

2285 def testTimespanQueries(self): 

2286 """Test query expressions involving timespans.""" 

2287 registry = self.makeRegistry() 

2288 self.loadData(registry, "hsc-rc2-subset.yaml") 

2289 # All exposures in the database; mapping from ID to timespan. 

2290 visits = {record.id: record.timespan for record in registry.queryDimensionRecords("visit")} 

2291 # Just those IDs, sorted (which is also temporal sorting, because HSC 

2292 # exposure IDs are monotonically increasing). 

2293 ids = sorted(visits.keys()) 

2294 self.assertGreater(len(ids), 20) 

2295 # Pick some quasi-random indexes into `ids` to play with. 

2296 i1 = int(len(ids) * 0.1) 

2297 i2 = int(len(ids) * 0.3) 

2298 i3 = int(len(ids) * 0.6) 

2299 i4 = int(len(ids) * 0.8) 

2300 # Extract some times from those: just before the beginning of i1 (which 

2301 # should be after the end of the exposure before), exactly the 

2302 # beginning of i2, just after the beginning of i3 (and before its end), 

2303 # and the exact end of i4. 

2304 t1 = visits[ids[i1]].begin - astropy.time.TimeDelta(1.0, format="sec") 

2305 self.assertGreater(t1, visits[ids[i1 - 1]].end) 

2306 t2 = visits[ids[i2]].begin 

2307 t3 = visits[ids[i3]].begin + astropy.time.TimeDelta(1.0, format="sec") 

2308 self.assertLess(t3, visits[ids[i3]].end) 

2309 t4 = visits[ids[i4]].end 

2310 # Make sure those are actually in order. 

2311 self.assertEqual([t1, t2, t3, t4], sorted([t4, t3, t2, t1])) 

2312 

2313 bind = { 

2314 "t1": t1, 

2315 "t2": t2, 

2316 "t3": t3, 

2317 "t4": t4, 

2318 "ts23": Timespan(t2, t3), 

2319 } 

2320 

2321 def query(where): 

2322 """Helper function that queries for visit data IDs and returns 

2323 results as a sorted, deduplicated list of visit IDs. 

2324 """ 

2325 return sorted( 

2326 { 

2327 dataId["visit"] 

2328 for dataId in registry.queryDataIds("visit", instrument="HSC", bind=bind, where=where) 

2329 } 

2330 ) 

2331 

2332 # Try a bunch of timespan queries, mixing up the bounds themselves, 

2333 # where they appear in the expression, and how we get the timespan into 

2334 # the expression. 

2335 

2336 # t1 is before the start of i1, so this should not include i1. 

2337 self.assertEqual(ids[:i1], query("visit.timespan OVERLAPS (null, t1)")) 

2338 # t2 is exactly at the start of i2, but ends are exclusive, so these 

2339 # should not include i2. 

2340 self.assertEqual(ids[i1:i2], query("(t1, t2) OVERLAPS visit.timespan")) 

2341 self.assertEqual(ids[:i2], query("visit.timespan < (t2, t4)")) 

2342 # t3 is in the middle of i3, so this should include i3. 

2343 self.assertEqual(ids[i2 : i3 + 1], query("visit.timespan OVERLAPS ts23")) 

2344 # This one should not include t3 by the same reasoning. 

2345 self.assertEqual(ids[i3 + 1 :], query("visit.timespan > (t1, t3)")) 

2346 # t4 is exactly at the end of i4, so this should include i4. 

2347 self.assertEqual(ids[i3 : i4 + 1], query(f"visit.timespan OVERLAPS (T'{t3.tai.isot}', t4)")) 

2348 # i4's upper bound of t4 is exclusive so this should not include t4. 

2349 self.assertEqual(ids[i4 + 1 :], query("visit.timespan OVERLAPS (t4, NULL)")) 

2350 

2351 # Now some timespan vs. time scalar queries. 

2352 self.assertEqual(ids[:i2], query("visit.timespan < t2")) 

2353 self.assertEqual(ids[:i2], query("t2 > visit.timespan")) 

2354 self.assertEqual(ids[i3 + 1 :], query("visit.timespan > t3")) 

2355 self.assertEqual(ids[i3 + 1 :], query("t3 < visit.timespan")) 

2356 self.assertEqual(ids[i3 : i3 + 1], query("visit.timespan OVERLAPS t3")) 

2357 self.assertEqual(ids[i3 : i3 + 1], query(f"T'{t3.tai.isot}' OVERLAPS visit.timespan")) 

2358 

2359 # Empty timespans should not overlap anything. 

2360 self.assertEqual([], query("visit.timespan OVERLAPS (t3, t2)")) 

2361 

2362 def testCollectionSummaries(self): 

2363 """Test recording and retrieval of collection summaries.""" 

2364 self.maxDiff = None 

2365 registry = self.makeRegistry() 

2366 # Importing datasets from yaml should go through the code path where 

2367 # we update collection summaries as we insert datasets. 

2368 self.loadData(registry, "base.yaml") 

2369 self.loadData(registry, "datasets.yaml") 

2370 flat = registry.getDatasetType("flat") 

2371 expected1 = CollectionSummary.makeEmpty(registry.dimensions) 

2372 expected1.datasetTypes.add(registry.getDatasetType("bias")) 

2373 expected1.datasetTypes.add(flat) 

2374 expected1.dimensions.update_extract( 

2375 DataCoordinate.standardize(instrument="Cam1", universe=registry.dimensions) 

2376 ) 

2377 self.assertEqual(registry.getCollectionSummary("imported_g"), expected1) 

2378 self.assertEqual(registry.getCollectionSummary("imported_r"), expected1) 

2379 # Create a chained collection with both of the imported runs; the 

2380 # summary should be the same, because it's a union with itself. 

2381 chain = "chain" 

2382 registry.registerCollection(chain, CollectionType.CHAINED) 

2383 registry.setCollectionChain(chain, ["imported_r", "imported_g"]) 

2384 self.assertEqual(registry.getCollectionSummary(chain), expected1) 

2385 # Associate flats only into a tagged collection and a calibration 

2386 # collection to check summaries of those. 

2387 tag = "tag" 

2388 registry.registerCollection(tag, CollectionType.TAGGED) 

2389 registry.associate(tag, registry.queryDatasets(flat, collections="imported_g")) 

2390 calibs = "calibs" 

2391 registry.registerCollection(calibs, CollectionType.CALIBRATION) 

2392 registry.certify( 

2393 calibs, registry.queryDatasets(flat, collections="imported_g"), timespan=Timespan(None, None) 

2394 ) 

2395 expected2 = expected1.copy() 

2396 expected2.datasetTypes.discard("bias") 

2397 self.assertEqual(registry.getCollectionSummary(tag), expected2) 

2398 self.assertEqual(registry.getCollectionSummary(calibs), expected2) 

2399 # Explicitly calling Registry.refresh() should load those same 

2400 # summaries, via a totally different code path. 

2401 registry.refresh() 

2402 self.assertEqual(registry.getCollectionSummary("imported_g"), expected1) 

2403 self.assertEqual(registry.getCollectionSummary("imported_r"), expected1) 

2404 self.assertEqual(registry.getCollectionSummary(tag), expected2) 

2405 self.assertEqual(registry.getCollectionSummary(calibs), expected2) 

2406 

2407 def testBindInQueryDatasets(self): 

2408 """Test that the bind parameter is correctly forwarded in 

2409 queryDatasets recursion. 

2410 """ 

2411 registry = self.makeRegistry() 

2412 # Importing datasets from yaml should go through the code path where 

2413 # we update collection summaries as we insert datasets. 

2414 self.loadData(registry, "base.yaml") 

2415 self.loadData(registry, "datasets.yaml") 

2416 self.assertEqual( 

2417 set(registry.queryDatasets("flat", band="r", collections=...)), 

2418 set(registry.queryDatasets("flat", where="band=my_band", bind={"my_band": "r"}, collections=...)), 

2419 ) 

2420 

2421 def testQueryResultSummaries(self): 

2422 """Test summary methods like `count`, `any`, and `explain_no_results` 

2423 on `DataCoordinateQueryResults` and `DatasetQueryResults` 

2424 """ 

2425 registry = self.makeRegistry() 

2426 self.loadData(registry, "base.yaml") 

2427 self.loadData(registry, "datasets.yaml") 

2428 self.loadData(registry, "spatial.yaml") 

2429 # Default test dataset has two collections, each with both flats and 

2430 # biases. Add a new collection with only biases. 

2431 registry.registerCollection("biases", CollectionType.TAGGED) 

2432 registry.associate("biases", registry.queryDatasets("bias", collections=["imported_g"])) 

2433 # First query yields two results, and involves no postprocessing. 

2434 query1 = registry.queryDataIds(["physical_filter"], band="r") 

2435 self.assertTrue(query1.any(execute=False, exact=False)) 

2436 self.assertTrue(query1.any(execute=True, exact=False)) 

2437 self.assertTrue(query1.any(execute=True, exact=True)) 

2438 self.assertEqual(query1.count(exact=False), 2) 

2439 self.assertEqual(query1.count(exact=True), 2) 

2440 self.assertFalse(list(query1.explain_no_results())) 

2441 # Second query should yield no results, but this isn't detectable 

2442 # unless we actually run a query. 

2443 query2 = registry.queryDataIds(["physical_filter"], band="h") 

2444 self.assertTrue(query2.any(execute=False, exact=False)) 

2445 self.assertFalse(query2.any(execute=True, exact=False)) 

2446 self.assertFalse(query2.any(execute=True, exact=True)) 

2447 self.assertEqual(query2.count(exact=False), 0) 

2448 self.assertEqual(query2.count(exact=True), 0) 

2449 self.assertFalse(list(query2.explain_no_results())) 

2450 # These queries yield no results due to various problems that can be 

2451 # spotted prior to execution, yielding helpful diagnostics. 

2452 base_query = registry.queryDataIds(["detector", "physical_filter"]) 

2453 for query, snippets in [ 

2454 ( 

2455 # Dataset type name doesn't match any existing dataset types. 

2456 registry.queryDatasets("nonexistent", collections=...), 

2457 ["nonexistent"], 

2458 ), 

2459 ( 

2460 # Dataset type name doesn't match any existing dataset types. 

2461 base_query.findDatasets("nonexistent", collections=["biases"]), 

2462 ["nonexistent"], 

2463 ), 

2464 ( 

2465 # Dataset type name doesn't match any existing dataset types. 

2466 registry.queryDataIds(["detector"], datasets=["nonexistent"], collections=...), 

2467 ["nonexistent"], 

2468 ), 

2469 ( 

2470 # Dataset type object isn't registered. 

2471 registry.queryDatasets( 

2472 DatasetType( 

2473 "nonexistent", 

2474 dimensions=["instrument"], 

2475 universe=registry.dimensions, 

2476 storageClass="Image", 

2477 ), 

2478 collections=..., 

2479 ), 

2480 ["nonexistent"], 

2481 ), 

2482 ( 

2483 # Dataset type object isn't registered. 

2484 base_query.findDatasets( 

2485 DatasetType( 

2486 "nonexistent", 

2487 dimensions=["instrument"], 

2488 universe=registry.dimensions, 

2489 storageClass="Image", 

2490 ), 

2491 collections=["biases"], 

2492 ), 

2493 ["nonexistent"], 

2494 ), 

2495 ( 

2496 # No datasets of this type in this collection. 

2497 registry.queryDatasets("flat", collections=["biases"]), 

2498 ["flat", "biases"], 

2499 ), 

2500 ( 

2501 # No datasets of this type in this collection. 

2502 base_query.findDatasets("flat", collections=["biases"]), 

2503 ["flat", "biases"], 

2504 ), 

2505 ( 

2506 # No collections matching at all. 

2507 registry.queryDatasets("flat", collections=re.compile("potato.+")), 

2508 ["potato"], 

2509 ), 

2510 ( 

2511 # Dataset type name doesn't match any existing dataset types. 

2512 registry.queryDimensionRecords("detector", datasets=["nonexistent"], collections=...), 

2513 ["nonexistent"], 

2514 ), 

2515 ]: 

2516 self.assertFalse(query.any(execute=False, exact=False)) 

2517 self.assertFalse(query.any(execute=True, exact=False)) 

2518 self.assertFalse(query.any(execute=True, exact=True)) 

2519 self.assertEqual(query.count(exact=False), 0) 

2520 self.assertEqual(query.count(exact=True), 0) 

2521 messages = list(query.explain_no_results()) 

2522 self.assertTrue(messages) 

2523 # Want all expected snippets to appear in at least one message. 

2524 self.assertTrue( 

2525 any( 

2526 all(snippet in message for snippet in snippets) for message in query.explain_no_results() 

2527 ), 

2528 messages, 

2529 ) 

2530 

2531 # These queries yield no results due to problems that can be identified 

2532 # by cheap follow-up queries, yielding helpful diagnostics. 

2533 for query, snippets in [ 

2534 ( 

2535 # No records for one of the involved dimensions. 

2536 registry.queryDataIds(["subfilter"]), 

2537 ["dimension records", "subfilter"], 

2538 ), 

2539 ( 

2540 # No records for one of the involved dimensions. 

2541 registry.queryDimensionRecords("subfilter"), 

2542 ["dimension records", "subfilter"], 

2543 ), 

2544 ]: 

2545 self.assertFalse(query.any(execute=True, exact=False)) 

2546 self.assertFalse(query.any(execute=True, exact=True)) 

2547 self.assertEqual(query.count(exact=True), 0) 

2548 messages = list(query.explain_no_results()) 

2549 self.assertTrue(messages) 

2550 # Want all expected snippets to appear in at least one message. 

2551 self.assertTrue( 

2552 any( 

2553 all(snippet in message for snippet in snippets) for message in query.explain_no_results() 

2554 ), 

2555 messages, 

2556 ) 

2557 

2558 # This query yields four overlaps in the database, but one is filtered 

2559 # out in postprocessing. The count queries aren't accurate because 

2560 # they don't account for duplication that happens due to an internal 

2561 # join against commonSkyPix. 

2562 query3 = registry.queryDataIds(["visit", "tract"], instrument="Cam1", skymap="SkyMap1") 

2563 self.assertEqual( 

2564 { 

2565 DataCoordinate.standardize( 

2566 instrument="Cam1", 

2567 skymap="SkyMap1", 

2568 visit=v, 

2569 tract=t, 

2570 universe=registry.dimensions, 

2571 ) 

2572 for v, t in [(1, 0), (2, 0), (2, 1)] 

2573 }, 

2574 set(query3), 

2575 ) 

2576 self.assertTrue(query3.any(execute=False, exact=False)) 

2577 self.assertTrue(query3.any(execute=True, exact=False)) 

2578 self.assertTrue(query3.any(execute=True, exact=True)) 

2579 self.assertGreaterEqual(query3.count(exact=False), 4) 

2580 self.assertGreaterEqual(query3.count(exact=True), 3) 

2581 self.assertFalse(list(query3.explain_no_results())) 

2582 # This query yields overlaps in the database, but all are filtered 

2583 # out in postprocessing. The count queries again aren't very useful. 

2584 # We have to use `where=` here to avoid an optimization that 

2585 # (currently) skips the spatial postprocess-filtering because it 

2586 # recognizes that no spatial join is necessary. That's not ideal, but 

2587 # fixing it is out of scope for this ticket. 

2588 query4 = registry.queryDataIds( 

2589 ["visit", "tract"], 

2590 instrument="Cam1", 

2591 skymap="SkyMap1", 

2592 where="visit=1 AND detector=1 AND tract=0 AND patch=4", 

2593 ) 

2594 self.assertFalse(set(query4)) 

2595 self.assertTrue(query4.any(execute=False, exact=False)) 

2596 self.assertTrue(query4.any(execute=True, exact=False)) 

2597 self.assertFalse(query4.any(execute=True, exact=True)) 

2598 self.assertGreaterEqual(query4.count(exact=False), 1) 

2599 self.assertEqual(query4.count(exact=True), 0) 

2600 messages = list(query4.explain_no_results()) 

2601 self.assertTrue(messages) 

2602 self.assertTrue(any("regions did not overlap" in message for message in messages)) 

2603 

2604 # And there are cases when queries make empty results but we do not 

2605 # know how to explain that yet (could we just say miracles happen?) 

2606 query5 = registry.queryDimensionRecords( 

2607 "detector", where="detector.purpose = 'no-purpose'", instrument="Cam1" 

2608 ) 

2609 self.assertEqual(query5.count(exact=True), 0) 

2610 messages = list(query5.explain_no_results()) 

2611 self.assertFalse(messages) 

2612 

2613 def testQueryDataIdsOrderBy(self): 

2614 """Test order_by and limit on result returned by queryDataIds().""" 

2615 registry = self.makeRegistry() 

2616 self.loadData(registry, "base.yaml") 

2617 self.loadData(registry, "datasets.yaml") 

2618 self.loadData(registry, "spatial.yaml") 

2619 

2620 def do_query(dimensions=("visit", "tract"), datasets=None, collections=None): 

2621 return registry.queryDataIds( 

2622 dimensions, datasets=datasets, collections=collections, instrument="Cam1", skymap="SkyMap1" 

2623 ) 

2624 

2625 Test = namedtuple( 

2626 "testQueryDataIdsOrderByTest", 

2627 ("order_by", "keys", "result", "limit", "datasets", "collections"), 

2628 defaults=(None, None, None), 

2629 ) 

2630 

2631 test_data = ( 

2632 Test("tract,visit", "tract,visit", ((0, 1), (0, 1), (0, 2), (0, 2), (1, 2), (1, 2))), 

2633 Test("-tract,visit", "tract,visit", ((1, 2), (1, 2), (0, 1), (0, 1), (0, 2), (0, 2))), 

2634 Test("tract,-visit", "tract,visit", ((0, 2), (0, 2), (0, 1), (0, 1), (1, 2), (1, 2))), 

2635 Test("-tract,-visit", "tract,visit", ((1, 2), (1, 2), (0, 2), (0, 2), (0, 1), (0, 1))), 

2636 Test( 

2637 "tract.id,visit.id", 

2638 "tract,visit", 

2639 ((0, 1), (0, 1), (0, 2)), 

2640 limit=(3,), 

2641 ), 

2642 Test("-tract,-visit", "tract,visit", ((1, 2), (1, 2), (0, 2)), limit=(3,)), 

2643 Test("tract,visit", "tract,visit", ((0, 2), (1, 2), (1, 2)), limit=(3, 3)), 

2644 Test("-tract,-visit", "tract,visit", ((0, 1),), limit=(3, 5)), 

2645 Test( 

2646 "tract,visit.exposure_time", "tract,visit", ((0, 2), (0, 2), (0, 1), (0, 1), (1, 2), (1, 2)) 

2647 ), 

2648 Test( 

2649 "-tract,-visit.exposure_time", "tract,visit", ((1, 2), (1, 2), (0, 1), (0, 1), (0, 2), (0, 2)) 

2650 ), 

2651 Test("tract,-exposure_time", "tract,visit", ((0, 1), (0, 1), (0, 2), (0, 2), (1, 2), (1, 2))), 

2652 Test("tract,visit.name", "tract,visit", ((0, 1), (0, 1), (0, 2), (0, 2), (1, 2), (1, 2))), 

2653 Test( 

2654 "tract,-timespan.begin,timespan.end", 

2655 "tract,visit", 

2656 ((0, 2), (0, 2), (0, 1), (0, 1), (1, 2), (1, 2)), 

2657 ), 

2658 Test("visit.day_obs,exposure.day_obs", "visit,exposure", ()), 

2659 Test("visit.timespan.begin,-exposure.timespan.begin", "visit,exposure", ()), 

2660 Test( 

2661 "tract,detector", 

2662 "tract,detector", 

2663 ((0, 1), (0, 2), (0, 3), (0, 4), (1, 1), (1, 2), (1, 3), (1, 4)), 

2664 datasets="flat", 

2665 collections="imported_r", 

2666 ), 

2667 Test( 

2668 "tract,detector.full_name", 

2669 "tract,detector", 

2670 ((0, 1), (0, 2), (0, 3), (0, 4), (1, 1), (1, 2), (1, 3), (1, 4)), 

2671 datasets="flat", 

2672 collections="imported_r", 

2673 ), 

2674 Test( 

2675 "tract,detector.raft,detector.name_in_raft", 

2676 "tract,detector", 

2677 ((0, 1), (0, 2), (0, 3), (0, 4), (1, 1), (1, 2), (1, 3), (1, 4)), 

2678 datasets="flat", 

2679 collections="imported_r", 

2680 ), 

2681 ) 

2682 

2683 for test in test_data: 

2684 order_by = test.order_by.split(",") 

2685 keys = test.keys.split(",") 

2686 query = do_query(keys, test.datasets, test.collections).order_by(*order_by) 

2687 if test.limit is not None: 

2688 query = query.limit(*test.limit) 

2689 dataIds = tuple(tuple(dataId[k] for k in keys) for dataId in query) 

2690 self.assertEqual(dataIds, test.result) 

2691 

2692 # and materialize 

2693 query = do_query(keys).order_by(*order_by) 

2694 if test.limit is not None: 

2695 query = query.limit(*test.limit) 

2696 with query.materialize() as materialized: 

2697 dataIds = tuple(tuple(dataId[k] for k in keys) for dataId in materialized) 

2698 self.assertEqual(dataIds, test.result) 

2699 

2700 # errors in a name 

2701 for order_by in ("", "-"): 

2702 with self.assertRaisesRegex(ValueError, "Empty dimension name in ORDER BY"): 

2703 list(do_query().order_by(order_by)) 

2704 

2705 for order_by in ("undimension.name", "-undimension.name"): 

2706 with self.assertRaisesRegex(ValueError, "Unknown dimension element name 'undimension'"): 

2707 list(do_query().order_by(order_by)) 

2708 

2709 for order_by in ("attract", "-attract"): 

2710 with self.assertRaisesRegex(ValueError, "Metadata 'attract' cannot be found in any dimension"): 

2711 list(do_query().order_by(order_by)) 

2712 

2713 with self.assertRaisesRegex(ValueError, "Metadata 'exposure_time' exists in more than one dimension"): 

2714 list(do_query(("exposure", "visit")).order_by("exposure_time")) 

2715 

2716 with self.assertRaisesRegex(ValueError, "Timespan exists in more than one dimesion"): 

2717 list(do_query(("exposure", "visit")).order_by("timespan.begin")) 

2718 

2719 with self.assertRaisesRegex( 

2720 ValueError, "Cannot find any temporal dimension element for 'timespan.begin'" 

2721 ): 

2722 list(do_query(("tract")).order_by("timespan.begin")) 

2723 

2724 with self.assertRaisesRegex(ValueError, "Cannot use 'timespan.begin' with non-temporal element"): 

2725 list(do_query(("tract")).order_by("tract.timespan.begin")) 

2726 

2727 with self.assertRaisesRegex(ValueError, "Field 'name' does not exist in 'tract'."): 

2728 list(do_query(("tract")).order_by("tract.name")) 

2729 

2730 def testQueryDataIdsGovernorExceptions(self): 

2731 """Test exceptions raised by queryDataIds() for incorrect governors.""" 

2732 registry = self.makeRegistry() 

2733 self.loadData(registry, "base.yaml") 

2734 self.loadData(registry, "datasets.yaml") 

2735 self.loadData(registry, "spatial.yaml") 

2736 

2737 def do_query(dimensions, dataId=None, where=None, bind=None, **kwargs): 

2738 return registry.queryDataIds(dimensions, dataId=dataId, where=where, bind=bind, **kwargs) 

2739 

2740 Test = namedtuple( 

2741 "testQueryDataIdExceptionsTest", 

2742 ("dimensions", "dataId", "where", "bind", "kwargs", "exception", "count"), 

2743 defaults=(None, None, None, {}, None, 0), 

2744 ) 

2745 

2746 test_data = ( 

2747 Test("tract,visit", count=6), 

2748 Test("tract,visit", kwargs={"instrument": "Cam1", "skymap": "SkyMap1"}, count=6), 

2749 Test( 

2750 "tract,visit", kwargs={"instrument": "Cam2", "skymap": "SkyMap1"}, exception=DataIdValueError 

2751 ), 

2752 Test("tract,visit", dataId={"instrument": "Cam1", "skymap": "SkyMap1"}, count=6), 

2753 Test( 

2754 "tract,visit", dataId={"instrument": "Cam1", "skymap": "SkyMap2"}, exception=DataIdValueError 

2755 ), 

2756 Test("tract,visit", where="instrument='Cam1' AND skymap='SkyMap1'", count=6), 

2757 Test("tract,visit", where="instrument='Cam1' AND skymap='SkyMap5'", exception=DataIdValueError), 

2758 Test( 

2759 "tract,visit", 

2760 where="instrument=cam AND skymap=map", 

2761 bind={"cam": "Cam1", "map": "SkyMap1"}, 

2762 count=6, 

2763 ), 

2764 Test( 

2765 "tract,visit", 

2766 where="instrument=cam AND skymap=map", 

2767 bind={"cam": "Cam", "map": "SkyMap"}, 

2768 exception=DataIdValueError, 

2769 ), 

2770 ) 

2771 

2772 for test in test_data: 

2773 dimensions = test.dimensions.split(",") 

2774 if test.exception: 

2775 with self.assertRaises(test.exception): 

2776 do_query(dimensions, test.dataId, test.where, bind=test.bind, **test.kwargs).count() 

2777 else: 

2778 query = do_query(dimensions, test.dataId, test.where, bind=test.bind, **test.kwargs) 

2779 self.assertEqual(query.count(), test.count) 

2780 

2781 # and materialize 

2782 if test.exception: 

2783 with self.assertRaises(test.exception): 

2784 query = do_query(dimensions, test.dataId, test.where, bind=test.bind, **test.kwargs) 

2785 with query.materialize() as materialized: 

2786 materialized.count() 

2787 else: 

2788 query = do_query(dimensions, test.dataId, test.where, bind=test.bind, **test.kwargs) 

2789 with query.materialize() as materialized: 

2790 self.assertEqual(materialized.count(), test.count) 

2791 

2792 def testQueryDimensionRecordsOrderBy(self): 

2793 """Test order_by and limit on result returned by 

2794 queryDimensionRecords(). 

2795 """ 

2796 registry = self.makeRegistry() 

2797 self.loadData(registry, "base.yaml") 

2798 self.loadData(registry, "datasets.yaml") 

2799 self.loadData(registry, "spatial.yaml") 

2800 

2801 def do_query(element, datasets=None, collections=None): 

2802 return registry.queryDimensionRecords( 

2803 element, instrument="Cam1", datasets=datasets, collections=collections 

2804 ) 

2805 

2806 query = do_query("detector") 

2807 self.assertEqual(len(list(query)), 4) 

2808 

2809 Test = namedtuple( 

2810 "testQueryDataIdsOrderByTest", 

2811 ("element", "order_by", "result", "limit", "datasets", "collections"), 

2812 defaults=(None, None, None), 

2813 ) 

2814 

2815 test_data = ( 

2816 Test("detector", "detector", (1, 2, 3, 4)), 

2817 Test("detector", "-detector", (4, 3, 2, 1)), 

2818 Test("detector", "raft,-name_in_raft", (2, 1, 4, 3)), 

2819 Test("detector", "-detector.purpose", (4,), limit=(1,)), 

2820 Test("detector", "-purpose,detector.raft,name_in_raft", (2, 3), limit=(2, 2)), 

2821 Test("visit", "visit", (1, 2)), 

2822 Test("visit", "-visit.id", (2, 1)), 

2823 Test("visit", "zenith_angle", (1, 2)), 

2824 Test("visit", "-visit.name", (2, 1)), 

2825 Test("visit", "day_obs,-timespan.begin", (2, 1)), 

2826 ) 

2827 

2828 for test in test_data: 

2829 order_by = test.order_by.split(",") 

2830 query = do_query(test.element).order_by(*order_by) 

2831 if test.limit is not None: 

2832 query = query.limit(*test.limit) 

2833 dataIds = tuple(rec.id for rec in query) 

2834 self.assertEqual(dataIds, test.result) 

2835 

2836 # errors in a name 

2837 for order_by in ("", "-"): 

2838 with self.assertRaisesRegex(ValueError, "Empty dimension name in ORDER BY"): 

2839 list(do_query("detector").order_by(order_by)) 

2840 

2841 for order_by in ("undimension.name", "-undimension.name"): 

2842 with self.assertRaisesRegex(ValueError, "Element name mismatch: 'undimension'"): 

2843 list(do_query("detector").order_by(order_by)) 

2844 

2845 for order_by in ("attract", "-attract"): 

2846 with self.assertRaisesRegex(ValueError, "Field 'attract' does not exist in 'detector'."): 

2847 list(do_query("detector").order_by(order_by)) 

2848 

2849 def testQueryDimensionRecordsExceptions(self): 

2850 """Test exceptions raised by queryDimensionRecords().""" 

2851 registry = self.makeRegistry() 

2852 self.loadData(registry, "base.yaml") 

2853 self.loadData(registry, "datasets.yaml") 

2854 self.loadData(registry, "spatial.yaml") 

2855 

2856 result = registry.queryDimensionRecords("detector") 

2857 self.assertEqual(result.count(), 4) 

2858 result = registry.queryDimensionRecords("detector", instrument="Cam1") 

2859 self.assertEqual(result.count(), 4) 

2860 result = registry.queryDimensionRecords("detector", dataId={"instrument": "Cam1"}) 

2861 self.assertEqual(result.count(), 4) 

2862 result = registry.queryDimensionRecords("detector", where="instrument='Cam1'") 

2863 self.assertEqual(result.count(), 4) 

2864 result = registry.queryDimensionRecords("detector", where="instrument=instr", bind={"instr": "Cam1"}) 

2865 self.assertEqual(result.count(), 4) 

2866 

2867 with self.assertRaisesRegex( 

2868 DataIdValueError, "Could not fetch record for required dimension instrument" 

2869 ): 

2870 registry.queryDimensionRecords("detector", instrument="NotCam1") 

2871 

2872 with self.assertRaisesRegex( 

2873 DataIdValueError, "Could not fetch record for required dimension instrument" 

2874 ): 

2875 result = registry.queryDimensionRecords("detector", dataId={"instrument": "NotCam1"}) 

2876 

2877 with self.assertRaisesRegex(DataIdValueError, "Unknown values specified for governor dimension"): 

2878 result = registry.queryDimensionRecords("detector", where="instrument='NotCam1'") 

2879 result.count() 

2880 

2881 with self.assertRaisesRegex(DataIdValueError, "Unknown values specified for governor dimension"): 

2882 result = registry.queryDimensionRecords( 

2883 "detector", where="instrument=instr", bind={"instr": "NotCam1"} 

2884 ) 

2885 result.count() 

2886 

2887 def testDatasetConstrainedDimensionRecordQueries(self): 

2888 """Test that queryDimensionRecords works even when given a dataset 

2889 constraint whose dimensions extend beyond the requested dimension 

2890 element's. 

2891 """ 

2892 registry = self.makeRegistry() 

2893 self.loadData(registry, "base.yaml") 

2894 self.loadData(registry, "datasets.yaml") 

2895 # Query for physical_filter dimension records, using a dataset that 

2896 # has both physical_filter and dataset dimensions. 

2897 records = registry.queryDimensionRecords( 

2898 "physical_filter", 

2899 datasets=["flat"], 

2900 collections="imported_r", 

2901 ) 

2902 self.assertEqual({record.name for record in records}, {"Cam1-R1", "Cam1-R2"}) 

2903 

2904 def testSkyPixDatasetQueries(self): 

2905 """Test that we can build queries involving skypix dimensions as long 

2906 as a dataset type that uses those dimensions is included. 

2907 """ 

2908 registry = self.makeRegistry() 

2909 self.loadData(registry, "base.yaml") 

2910 dataset_type = DatasetType( 

2911 "a", dimensions=["htm7", "instrument"], universe=registry.dimensions, storageClass="int" 

2912 ) 

2913 registry.registerDatasetType(dataset_type) 

2914 run = "r" 

2915 registry.registerRun(run) 

2916 # First try queries where there are no datasets; the concern is whether 

2917 # we can even build and execute these queries without raising, even 

2918 # when "doomed" query shortcuts are in play. 

2919 self.assertFalse( 

2920 list(registry.queryDataIds(["htm7", "instrument"], datasets=dataset_type, collections=run)) 

2921 ) 

2922 self.assertFalse(list(registry.queryDatasets(dataset_type, collections=run))) 

2923 # Now add a dataset and see that we can get it back. 

2924 htm7 = registry.dimensions.skypix["htm"][7].pixelization 

2925 data_id = registry.expandDataId(instrument="Cam1", htm7=htm7.universe()[0][0]) 

2926 (ref,) = registry.insertDatasets(dataset_type, [data_id], run=run) 

2927 self.assertEqual( 

2928 set(registry.queryDataIds(["htm7", "instrument"], datasets=dataset_type, collections=run)), 

2929 {data_id}, 

2930 ) 

2931 self.assertEqual(set(registry.queryDatasets(dataset_type, collections=run)), {ref})