LSSTApplications  18.1.0
LSSTDataManagementBasePackage
Classes | Functions
lsst.daf.persistence.posixStorage Namespace Reference

Classes

class  PosixStorage
 

Functions

def readConfigStorage (butlerLocation)
 
def writeConfigStorage (butlerLocation, obj)
 
def readFitsStorage (butlerLocation)
 
def writeFitsStorage (butlerLocation, obj)
 
def readParquetStorage (butlerLocation)
 
def writeParquetStorage (butlerLocation, obj)
 
def writeYamlStorage (butlerLocation, obj)
 
def readPickleStorage (butlerLocation)
 
def writePickleStorage (butlerLocation, obj)
 
def readFitsCatalogStorage (butlerLocation)
 
def writeFitsCatalogStorage (butlerLocation, obj)
 
def readMatplotlibStorage (butlerLocation)
 
def writeMatplotlibStorage (butlerLocation, obj)
 
def readPafStorage (butlerLocation)
 
def readYamlStorage (butlerLocation)
 

Function Documentation

◆ readConfigStorage()

def lsst.daf.persistence.posixStorage.readConfigStorage (   butlerLocation)
Read an lsst.pex.config.Config from a butlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 510 of file posixStorage.py.

510 def readConfigStorage(butlerLocation):
511  """Read an lsst.pex.config.Config from a butlerLocation.
512 
513  Parameters
514  ----------
515  butlerLocation : ButlerLocation
516  The location for the object(s) to be read.
517 
518  Returns
519  -------
520  A list of objects as described by the butler location. One item for
521  each location in butlerLocation.getLocations()
522  """
523  results = []
524  for locationString in butlerLocation.getLocations():
525  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
526  logLoc = LogicalLocation(locStringWithRoot, butlerLocation.getAdditionalData())
527  if not os.path.exists(logLoc.locString()):
528  raise RuntimeError("No such config file: " + logLoc.locString())
529  pythonType = butlerLocation.getPythonType()
530  if pythonType is not None:
531  if isinstance(pythonType, basestring):
532  pythonType = doImport(pythonType)
533  finalItem = pythonType()
534  finalItem.load(logLoc.locString())
535  results.append(finalItem)
536  return results
537 
538 
def readConfigStorage(butlerLocation)
def doImport(pythonType)
Definition: utils.py:106

◆ readFitsCatalogStorage()

def lsst.daf.persistence.posixStorage.readFitsCatalogStorage (   butlerLocation)
Read a catalog from a FITS table specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 767 of file posixStorage.py.

767 def readFitsCatalogStorage(butlerLocation):
768  """Read a catalog from a FITS table specified by ButlerLocation.
769 
770  Parameters
771  ----------
772  butlerLocation : ButlerLocation
773  The location for the object(s) to be read.
774 
775  Returns
776  -------
777  A list of objects as described by the butler location. One item for
778  each location in butlerLocation.getLocations()
779  """
780  pythonType = butlerLocation.getPythonType()
781  if pythonType is not None:
782  if isinstance(pythonType, basestring):
783  pythonType = doImport(pythonType)
784  results = []
785  additionalData = butlerLocation.getAdditionalData()
786  for locationString in butlerLocation.getLocations():
787  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
788  logLoc = LogicalLocation(locStringWithRoot, additionalData)
789  if not os.path.exists(logLoc.locString()):
790  raise RuntimeError("No such FITS catalog file: " + logLoc.locString())
791  kwds = {}
792  if additionalData.exists("hdu"):
793  kwds["hdu"] = additionalData.getInt("hdu")
794  if additionalData.exists("flags"):
795  kwds["flags"] = additionalData.getInt("flags")
796  finalItem = pythonType.readFits(logLoc.locString(), **kwds)
797  results.append(finalItem)
798  return results
799 
800 
def readFitsCatalogStorage(butlerLocation)
def doImport(pythonType)
Definition: utils.py:106

◆ readFitsStorage()

def lsst.daf.persistence.posixStorage.readFitsStorage (   butlerLocation)
Read objects from a FITS file specified by ButlerLocation.

The object is read using class or static method
``readFitsWithOptions(path, options)``, if it exists, else
``readFits(path)``. The ``options`` argument is the data returned by
``butlerLocation.getAdditionalData()``.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 556 of file posixStorage.py.

556 def readFitsStorage(butlerLocation):
557  """Read objects from a FITS file specified by ButlerLocation.
558 
559  The object is read using class or static method
560  ``readFitsWithOptions(path, options)``, if it exists, else
561  ``readFits(path)``. The ``options`` argument is the data returned by
562  ``butlerLocation.getAdditionalData()``.
563 
564  Parameters
565  ----------
566  butlerLocation : ButlerLocation
567  The location for the object(s) to be read.
568 
569  Returns
570  -------
571  A list of objects as described by the butler location. One item for
572  each location in butlerLocation.getLocations()
573  """
574  pythonType = butlerLocation.getPythonType()
575  if pythonType is not None:
576  if isinstance(pythonType, basestring):
577  pythonType = doImport(pythonType)
578  supportsOptions = hasattr(pythonType, "readFitsWithOptions")
579  if not supportsOptions:
580  from lsst.daf.base import PropertySet, PropertyList
581  if issubclass(pythonType, (PropertySet, PropertyList)):
582  from lsst.afw.image import readMetadata
583  reader = readMetadata
584  else:
585  reader = pythonType.readFits
586  results = []
587  additionalData = butlerLocation.getAdditionalData()
588  for locationString in butlerLocation.getLocations():
589  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
590  logLoc = LogicalLocation(locStringWithRoot, additionalData)
591  # test for existence of file, ignoring trailing [...]
592  # because that can specify the HDU or other information
593  filePath = re.sub(r"(\.fits(.[a-zA-Z0-9]+)?)(\[.+\])$", r"\1", logLoc.locString())
594  if not os.path.exists(filePath):
595  raise RuntimeError("No such FITS file: " + logLoc.locString())
596  if supportsOptions:
597  finalItem = pythonType.readFitsWithOptions(logLoc.locString(), options=additionalData)
598  else:
599  fileName = logLoc.locString()
600  mat = re.search(r"^(.*)\[(\d+)\]$", fileName)
601 
602  if mat and reader == readMetadata: # readMetadata() only understands the hdu argument, not [hdu]
603  fileName = mat.group(1)
604  hdu = int(mat.group(2))
605 
606  finalItem = reader(fileName, hdu=hdu)
607  else:
608  finalItem = reader(fileName)
609  results.append(finalItem)
610  return results
611 
612 
def readFitsStorage(butlerLocation)
def doImport(pythonType)
Definition: utils.py:106
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...

◆ readMatplotlibStorage()

def lsst.daf.persistence.posixStorage.readMatplotlibStorage (   butlerLocation)
Read from a butlerLocation (always fails for this storage type).

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 822 of file posixStorage.py.

822 def readMatplotlibStorage(butlerLocation):
823  """Read from a butlerLocation (always fails for this storage type).
824 
825  Parameters
826  ----------
827  butlerLocation : ButlerLocation
828  The location for the object(s) to be read.
829 
830  Returns
831  -------
832  A list of objects as described by the butler location. One item for
833  each location in butlerLocation.getLocations()
834  """
835  raise NotImplementedError("Figures saved with MatplotlibStorage cannot be retreived using the Butler.")
836 
837 
def readMatplotlibStorage(butlerLocation)

◆ readPafStorage()

def lsst.daf.persistence.posixStorage.readPafStorage (   butlerLocation)
Read a policy from a PAF file specified by a ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 867 of file posixStorage.py.

867 def readPafStorage(butlerLocation):
868  """Read a policy from a PAF file specified by a ButlerLocation.
869 
870  Parameters
871  ----------
872  butlerLocation : ButlerLocation
873  The location for the object(s) to be read.
874 
875  Returns
876  -------
877  A list of objects as described by the butler location. One item for
878  each location in butlerLocation.getLocations()
879  """
880  results = []
881  for locationString in butlerLocation.getLocations():
882  logLoc = LogicalLocation(butlerLocation.getStorage().locationWithRoot(locationString),
883  butlerLocation.getAdditionalData())
884  finalItem = pexPolicy.Policy.createPolicy(logLoc.locString())
885  results.append(finalItem)
886  return results
887 
888 
def readPafStorage(butlerLocation)

◆ readParquetStorage()

def lsst.daf.persistence.posixStorage.readParquetStorage (   butlerLocation)
Read a catalog from a Parquet file specified by ButlerLocation.

The object returned by this is expected to be a subtype
of `ParquetTable`, which is a thin wrapper to `pyarrow.ParquetFile`
that allows for lazy loading of the data.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 639 of file posixStorage.py.

639 def readParquetStorage(butlerLocation):
640  """Read a catalog from a Parquet file specified by ButlerLocation.
641 
642  The object returned by this is expected to be a subtype
643  of `ParquetTable`, which is a thin wrapper to `pyarrow.ParquetFile`
644  that allows for lazy loading of the data.
645 
646  Parameters
647  ----------
648  butlerLocation : ButlerLocation
649  The location for the object(s) to be read.
650 
651  Returns
652  -------
653  A list of objects as described by the butler location. One item for
654  each location in butlerLocation.getLocations()
655  """
656  results = []
657  additionalData = butlerLocation.getAdditionalData()
658 
659  for locationString in butlerLocation.getLocations():
660  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
661  logLoc = LogicalLocation(locStringWithRoot, additionalData)
662  if not os.path.exists(logLoc.locString()):
663  raise RuntimeError("No such parquet file: " + logLoc.locString())
664 
665  pythonType = butlerLocation.getPythonType()
666  if pythonType is not None:
667  if isinstance(pythonType, basestring):
668  pythonType = doImport(pythonType)
669 
670  filename = logLoc.locString()
671 
672  # pythonType will be ParquetTable (or perhaps MultilevelParquetTable)
673  # filename should be the first kwarg, but being explicit here.
674  results.append(pythonType(filename=filename))
675 
676  return results
677 
678 
def readParquetStorage(butlerLocation)
def doImport(pythonType)
Definition: utils.py:106

◆ readPickleStorage()

def lsst.daf.persistence.posixStorage.readPickleStorage (   butlerLocation)
Read an object from a pickle file specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 716 of file posixStorage.py.

716 def readPickleStorage(butlerLocation):
717  """Read an object from a pickle file specified by ButlerLocation.
718 
719  Parameters
720  ----------
721  butlerLocation : ButlerLocation
722  The location for the object(s) to be read.
723 
724  Returns
725  -------
726  A list of objects as described by the butler location. One item for
727  each location in butlerLocation.getLocations()
728  """
729  # Create a list of Storages for the item.
730  results = []
731  additionalData = butlerLocation.getAdditionalData()
732  for locationString in butlerLocation.getLocations():
733  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
734  logLoc = LogicalLocation(locStringWithRoot, additionalData)
735  if not os.path.exists(logLoc.locString()):
736  raise RuntimeError("No such pickle file: " + logLoc.locString())
737  with open(logLoc.locString(), "rb") as infile:
738  # py3: We have to specify encoding since some files were written
739  # by python2, and 'latin1' manages that conversion safely. See:
740  # http://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3/28218598#28218598
741  if sys.version_info.major >= 3:
742  finalItem = pickle.load(infile, encoding="latin1")
743  else:
744  finalItem = pickle.load(infile)
745  results.append(finalItem)
746  return results
747 
748 
def readPickleStorage(butlerLocation)

◆ readYamlStorage()

def lsst.daf.persistence.posixStorage.readYamlStorage (   butlerLocation)
Read an object from a YAML file specified by a butlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 889 of file posixStorage.py.

889 def readYamlStorage(butlerLocation):
890  """Read an object from a YAML file specified by a butlerLocation.
891 
892  Parameters
893  ----------
894  butlerLocation : ButlerLocation
895  The location for the object(s) to be read.
896 
897  Returns
898  -------
899  A list of objects as described by the butler location. One item for
900  each location in butlerLocation.getLocations()
901  """
902  results = []
903  for locationString in butlerLocation.getLocations():
904  logLoc = LogicalLocation(butlerLocation.getStorage().locationWithRoot(locationString),
905  butlerLocation.getAdditionalData())
906  if not os.path.exists(logLoc.locString()):
907  raise RuntimeError("No such YAML file: " + logLoc.locString())
908  # Butler Gen2 repository configurations are handled specially
909  if butlerLocation.pythonType == 'lsst.daf.persistence.RepositoryCfg':
910  finalItem = Policy(filePath=logLoc.locString())
911  else:
912  try:
913  # PyYAML >=5.1 prefers a different loader
914  loader = yaml.FullLoader
915  except AttributeError:
916  loader = yaml.Loader
917  with open(logLoc.locString(), "rb") as infile:
918  finalItem = yaml.load(infile, Loader=loader)
919  results.append(finalItem)
920  return results
921 
922 
923 PosixStorage.registerFormatters("FitsStorage", readFitsStorage, writeFitsStorage)
924 PosixStorage.registerFormatters("ParquetStorage", readParquetStorage, writeParquetStorage)
925 PosixStorage.registerFormatters("ConfigStorage", readConfigStorage, writeConfigStorage)
926 PosixStorage.registerFormatters("PickleStorage", readPickleStorage, writePickleStorage)
927 PosixStorage.registerFormatters("FitsCatalogStorage", readFitsCatalogStorage, writeFitsCatalogStorage)
928 PosixStorage.registerFormatters("MatplotlibStorage", readMatplotlibStorage, writeMatplotlibStorage)
929 PosixStorage.registerFormatters("PafStorage", readFormatter=readPafStorage)
930 PosixStorage.registerFormatters("YamlStorage", readYamlStorage, writeYamlStorage)
931 
932 Storage.registerStorageClass(scheme='', cls=PosixStorage)
933 Storage.registerStorageClass(scheme='file', cls=PosixStorage)
934 
def readYamlStorage(butlerLocation)

◆ writeConfigStorage()

def lsst.daf.persistence.posixStorage.writeConfigStorage (   butlerLocation,
  obj 
)
Writes an lsst.pex.config.Config  object to a location specified by
ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 539 of file posixStorage.py.

539 def writeConfigStorage(butlerLocation, obj):
540  """Writes an lsst.pex.config.Config object to a location specified by
541  ButlerLocation.
542 
543  Parameters
544  ----------
545  butlerLocation : ButlerLocation
546  The location for the object to be written.
547  obj : object instance
548  The object to be written.
549  """
550  filename = os.path.join(butlerLocation.getStorage().root, butlerLocation.getLocations()[0])
551  with SafeFilename(filename) as locationString:
552  logLoc = LogicalLocation(locationString, butlerLocation.getAdditionalData())
553  obj.save(logLoc.locString())
554 
555 
def writeConfigStorage(butlerLocation, obj)

◆ writeFitsCatalogStorage()

def lsst.daf.persistence.posixStorage.writeFitsCatalogStorage (   butlerLocation,
  obj 
)
Writes a catalog to a FITS table specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 801 of file posixStorage.py.

801 def writeFitsCatalogStorage(butlerLocation, obj):
802  """Writes a catalog to a FITS table specified by ButlerLocation.
803 
804  Parameters
805  ----------
806  butlerLocation : ButlerLocation
807  The location for the object to be written.
808  obj : object instance
809  The object to be written.
810  """
811  additionalData = butlerLocation.getAdditionalData()
812  locations = butlerLocation.getLocations()
813  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
814  logLoc = LogicalLocation(locationString, additionalData)
815  if additionalData.exists("flags"):
816  kwds = dict(flags=additionalData.getInt("flags"))
817  else:
818  kwds = {}
819  obj.writeFits(logLoc.locString(), **kwds)
820 
821 
def writeFitsCatalogStorage(butlerLocation, obj)

◆ writeFitsStorage()

def lsst.daf.persistence.posixStorage.writeFitsStorage (   butlerLocation,
  obj 
)
Writes an object to a FITS file specified by ButlerLocation.

The object is written using method
``writeFitsWithOptions(path, options)``, if it exists, else
``writeFits(path)``. The ``options`` argument is the data returned by
``butlerLocation.getAdditionalData()``.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 613 of file posixStorage.py.

613 def writeFitsStorage(butlerLocation, obj):
614  """Writes an object to a FITS file specified by ButlerLocation.
615 
616  The object is written using method
617  ``writeFitsWithOptions(path, options)``, if it exists, else
618  ``writeFits(path)``. The ``options`` argument is the data returned by
619  ``butlerLocation.getAdditionalData()``.
620 
621  Parameters
622  ----------
623  butlerLocation : ButlerLocation
624  The location for the object to be written.
625  obj : object instance
626  The object to be written.
627  """
628  supportsOptions = hasattr(obj, "writeFitsWithOptions")
629  additionalData = butlerLocation.getAdditionalData()
630  locations = butlerLocation.getLocations()
631  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
632  logLoc = LogicalLocation(locationString, additionalData)
633  if supportsOptions:
634  obj.writeFitsWithOptions(logLoc.locString(), options=additionalData)
635  else:
636  obj.writeFits(logLoc.locString())
637 
638 
def writeFitsStorage(butlerLocation, obj)

◆ writeMatplotlibStorage()

def lsst.daf.persistence.posixStorage.writeMatplotlibStorage (   butlerLocation,
  obj 
)
Writes a matplotlib.figure.Figure to a location, using the template's
filename suffix to infer the file format.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : matplotlib.figure.Figure
    The object to be written.

Definition at line 838 of file posixStorage.py.

838 def writeMatplotlibStorage(butlerLocation, obj):
839  """Writes a matplotlib.figure.Figure to a location, using the template's
840  filename suffix to infer the file format.
841 
842  Parameters
843  ----------
844  butlerLocation : ButlerLocation
845  The location for the object to be written.
846  obj : matplotlib.figure.Figure
847  The object to be written.
848  """
849  additionalData = butlerLocation.getAdditionalData()
850  locations = butlerLocation.getLocations()
851  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
852  logLoc = LogicalLocation(locationString, additionalData)
853  # SafeFilename appends a random suffix, which corrupts the extension
854  # matplotlib uses to guess the file format.
855  # Instead, we extract the extension from the original location
856  # and pass that as the format directly.
857  _, ext = os.path.splitext(locations[0])
858  if ext:
859  ext = ext[1:] # strip off leading '.'
860  else:
861  # If there is no extension, we let matplotlib fall back to its
862  # default.
863  ext = None
864  obj.savefig(logLoc.locString(), format=ext)
865 
866 
def writeMatplotlibStorage(butlerLocation, obj)

◆ writeParquetStorage()

def lsst.daf.persistence.posixStorage.writeParquetStorage (   butlerLocation,
  obj 
)
Writes pandas dataframe to parquet file.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.
obj : `lsst.qa.explorer.parquetTable.ParquetTable`
    Wrapped DataFrame to write.

Definition at line 679 of file posixStorage.py.

679 def writeParquetStorage(butlerLocation, obj):
680  """Writes pandas dataframe to parquet file.
681 
682  Parameters
683  ----------
684  butlerLocation : ButlerLocation
685  The location for the object(s) to be read.
686  obj : `lsst.qa.explorer.parquetTable.ParquetTable`
687  Wrapped DataFrame to write.
688 
689  """
690  additionalData = butlerLocation.getAdditionalData()
691  locations = butlerLocation.getLocations()
692  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
693  logLoc = LogicalLocation(locationString, additionalData)
694  filename = logLoc.locString()
695  obj.write(filename)
696 
697 
def writeParquetStorage(butlerLocation, obj)

◆ writePickleStorage()

def lsst.daf.persistence.posixStorage.writePickleStorage (   butlerLocation,
  obj 
)
Writes an object to a pickle file specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 749 of file posixStorage.py.

749 def writePickleStorage(butlerLocation, obj):
750  """Writes an object to a pickle file specified by ButlerLocation.
751 
752  Parameters
753  ----------
754  butlerLocation : ButlerLocation
755  The location for the object to be written.
756  obj : object instance
757  The object to be written.
758  """
759  additionalData = butlerLocation.getAdditionalData()
760  locations = butlerLocation.getLocations()
761  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
762  logLoc = LogicalLocation(locationString, additionalData)
763  with open(logLoc.locString(), "wb") as outfile:
764  pickle.dump(obj, outfile, pickle.HIGHEST_PROTOCOL)
765 
766 
def writePickleStorage(butlerLocation, obj)

◆ writeYamlStorage()

def lsst.daf.persistence.posixStorage.writeYamlStorage (   butlerLocation,
  obj 
)
Writes an object to a YAML file specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 698 of file posixStorage.py.

698 def writeYamlStorage(butlerLocation, obj):
699  """Writes an object to a YAML file specified by ButlerLocation.
700 
701  Parameters
702  ----------
703  butlerLocation : ButlerLocation
704  The location for the object to be written.
705  obj : object instance
706  The object to be written.
707  """
708  additionalData = butlerLocation.getAdditionalData()
709  locations = butlerLocation.getLocations()
710  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
711  logLoc = LogicalLocation(locationString, additionalData)
712  with open(logLoc.locString(), "w") as outfile:
713  yaml.dump(obj, outfile)
714 
715 
def writeYamlStorage(butlerLocation, obj)