def testMatplotlibFormatter(self): butler = Butler(self.root, run="testrun") datasetType = DatasetType("test_plot", [], "Plot", universe=butler.registry.dimensions) butler.registry.registerDatasetType(datasetType) # Does not have to be a random image pyplot.imshow([self.rng.sample(range(50), 10), self.rng.sample(range(50), 10), self.rng.sample(range(50), 10), ]) ref = butler.put(pyplot.gcf(), datasetType) uri = butler.getURI(ref) # Following test needs a local file with uri.as_local() as local: with tempfile.NamedTemporaryFile(suffix=".png") as file: pyplot.gcf().savefig(file.name) self.assertTrue( filecmp.cmp( local.ospath, file.name, shallow=True ) ) self.assertTrue(butler.datasetExists(ref)) with self.assertRaises(ValueError): butler.get(ref) butler.pruneDatasets([ref], unstore=True, purge=True) with self.assertRaises(LookupError): butler.datasetExists(ref)
def testMatplotlibFormatter(self): butler = Butler(self.root, run="testrun") datasetType = DatasetType("test_plot", [], "Plot", universe=butler.registry.dimensions) butler.registry.registerDatasetType(datasetType) # Does not have to be a random image pyplot.imshow([ self.rng.sample(range(50), 10), self.rng.sample(range(50), 10), self.rng.sample(range(50), 10), ]) ref = butler.put(pyplot.gcf(), datasetType) uri = butler.getURI(ref) # The test after this will not work if we don't have local file self.assertEqual(uri.scheme, "file", "Testing returned URI: {uri}") with tempfile.NamedTemporaryFile(suffix=".png") as file: pyplot.gcf().savefig(file.name) self.assertTrue(filecmp.cmp(uri.path, file.name, shallow=True)) self.assertTrue(butler.datasetExists(ref)) with self.assertRaises(ValueError): butler.get(ref) butler.pruneDatasets([ref], unstore=True, purge=True) with self.assertRaises(LookupError): butler.datasetExists(ref)
def makeWriteButler(cls, args: argparse.Namespace) -> Butler: """Return a read-write butler initialized to write to and read from the collections specified by the given command-line arguments. Parameters ---------- args : `argparse.Namespace` Parsed command-line arguments. See class documentation for the construction parameter of the same name. Returns ------- butler : `lsst.daf.butler.Butler` A read-write butler initialized according to the given arguments. """ butler = Butler(args.butler_config, writeable=True) self = cls(butler.registry, args, writeable=True) self.check(args) if self.output is not None: chainDefinition = list(self.output.chain if self.output.exists else self.inputs) if args.replace_run: replaced = chainDefinition.pop(0) if args.prune_replaced == "unstore": # Remove datasets from datastore with butler.transaction(): refs = butler.registry.queryDatasets(..., collections=replaced) butler.pruneDatasets(refs, unstore=True, run=replaced, disassociate=False) elif args.prune_replaced == "purge": # Erase entire collection and all datasets, need to remove # collection from its chain collection first. with butler.transaction(): butler.registry.setCollectionChain(self.output.name, chainDefinition) butler.pruneCollection(replaced, purge=True, unstore=True) elif args.prune_replaced is not None: raise NotImplementedError( f"Unsupported --prune-replaced option '{args.prune_replaced}'." ) if not self.output.exists: butler.registry.registerCollection(self.output.name, CollectionType.CHAINED) if not args.extend_run: butler.registry.registerCollection(self.outputRun.name, CollectionType.RUN) chainDefinition.insert(0, self.outputRun.name) butler.registry.setCollectionChain(self.output.name, chainDefinition) _LOG.debug("Preparing butler to write to '%s' and read from '%s'=%s", self.outputRun.name, self.output.name, chainDefinition) butler.registry.defaults = RegistryDefaults(run=self.outputRun.name, collections=self.output.name) else: inputs = CollectionSearch.fromExpression((self.outputRun.name,) + self.inputs) _LOG.debug("Preparing butler to write to '%s' and read from %s.", self.outputRun.name, inputs) butler.registry.defaults = RegistryDefaults(run=self.outputRun.name, collections=inputs) return butler