"""File input/output for localization data in Elyra files."""from__future__importannotationsimportioimportloggingimportosfromtypingimportTYPE_CHECKING,AnyifTYPE_CHECKING:from_typeshedimportSupportsRead,SupportsReadlineimportpandasaspdimportlocan.constantsfromlocan.dataimportmetadata_pb2fromlocan.data.locdataimportLocDatafromlocan.locan_io.locdata.utilitiesimport(convert_property_names,convert_property_types,open_path_or_file_like,)__all__:list[str]=["load_Elyra_header","load_Elyra_file"]logger=logging.getLogger(__name__)def_read_Elyra_header(file:SupportsReadline[Any])->list[str]:""" Read xml header from a Zeiss Elyra single-molecule localization file and identify column names. Parameters ---------- file A file to load. Returns ------- list[str] A list of valid dataset property keys as derived from the identifiers. """header=file.readline().split("\n")[0]# list identifiersidentifiers=header.split("\t")column_keys=convert_property_names(properties=identifiers,property_mapping=locan.constants.ELYRA_KEYS)returncolumn_keys
[docs]defload_Elyra_header(path:str|os.PathLike[Any]|SupportsRead[Any],)->list[str]:""" Load xml header from a Zeiss Elyra single-molecule localization file and identify column names. Parameters ---------- path File path for a file to load. Returns ------- list[str] A list of valid dataset property keys as derived from the identifiers. """withopen_path_or_file_like(path,encoding="latin-1")asfile:return_read_Elyra_header(file)
[docs]defload_Elyra_file(path:str|os.PathLike[Any]|SupportsRead[Any],nrows:int|None=None,convert:bool=True,**kwargs:Any,)->LocData:""" Load data from a rapidSTORM single-molecule 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. kwargs Other parameters passed to `pandas.read_csv()`. Returns ------- LocData A new instance of LocData with all localizations. Note ---- Data is loaded with encoding = 'latin-1' and only data before the first NUL character is returned. Additional information appended at the end of the file is thus ignored. """withopen_path_or_file_like(path,encoding="latin-1")asfile:columns=_read_Elyra_header(file)string=file.read()# remove metadata following nul bytestring=string.split("\x00")[0]stream=io.StringIO(string)dataframe=pd.read_csv(stream,sep="\t",skiprows=0,nrows=nrows,names=columns,**kwargs)ifconvert:dataframe=convert_property_types(dataframe,types=locan.constants.PROPERTY_KEYS)dat=LocData.from_dataframe(dataframe=dataframe)dat.meta.source=metadata_pb2.EXPERIMENTdat.meta.state=metadata_pb2.RAWdat.meta.file.type=metadata_pb2.ELYRAdat.meta.file.path=str(path)deldat.meta.history[:]dat.meta.history.add(name="load_Elyra_file",parameter=f"path={str(path)}, nrows={nrows}",)returndat