"""File input/output for localization data in ASDF files"""from__future__importannotationsimportloggingimportosfromtypingimportTYPE_CHECKING,AnyifTYPE_CHECKING:from_typeshedimportSupportsRead,SupportsWriteimportpandasaspdfromasdfimportAsdfFilefromasdfimportopenasasdf_openfromgoogle.protobufimportjson_formatfromlocan.constantsimportPROPERTY_KEYSfromlocan.dataimportmetadata_pb2fromlocan.data.locdataimportLocDatafromlocan.locan_io.locdata.utilitiesimport(convert_property_names,convert_property_types,)__all__:list[str]=["save_asdf","load_asdf_file"]logger=logging.getLogger(__name__)
[docs]defsave_asdf(locdata:LocData,path:str|os.PathLike[Any]|SupportsWrite[Any])->None:""" Save LocData attributes in an asdf file. In the Advanced Scientific Data Format (ASDF) file format we store metadata, properties and column names as human-readable yaml header. The data is stored as binary numpy.ndarray. Note ---- Only selected LocData attributes are saved. Currently these are: 'data', 'columns', 'properties', 'meta'. Parameters ---------- locdata The LocData object to be saved. path File path including file name to save to. """# Prepare treemeta_json=json_format.MessageToJson(locdata.meta,always_print_fields_with_no_presence=False)tree={"data":locdata.data.values,"columns":list(locdata.data),"properties":locdata.properties,"meta":meta_json,}# Create the ASDF file object from treeaf=AsdfFile(tree)# Write the data to a new fileaf.write_to(path)
[docs]defload_asdf_file(path:str|os.PathLike[Any]|SupportsRead[Any],nrows:int|None=None,convert:bool=True,)->LocData:""" Load data from ASDF localization file. Parameters ---------- path File path for a file to load. nrows The number of localizations to load from file. None means that all available rows are loaded. convert If True convert types by applying type specifications in locan.constants.PROPERTY_KEYS. Returns ------- LocData A new instance of LocData with all localizations. """withasdf_open(path)asaf:new_df=pd.DataFrame({k:af.tree["data"][slice(nrows),n]forn,kinenumerate(af.tree["columns"])})column_keys=convert_property_names(properties=new_df.columns.tolist(),property_mapping=None)mapper={key:valueforkey,valueinzip(new_df.columns,column_keys)}new_df=new_df.rename(columns=mapper)ifconvert:new_df=convert_property_types(new_df,types=PROPERTY_KEYS)locdata=LocData(dataframe=new_df)locdata.meta=json_format.Parse(af.tree["meta"],locdata.meta,ignore_unknown_fields=True)locdata.meta.file.type=metadata_pb2.ASDFlocdata.meta.file.path=str(path)returnlocdata