Source code for oasislmf.pytools.data_layer.oasis_files.correlations
"""This file defines the loading and saving for correlations data."""importloggingimportosfromtypingimportOptionalimportnumbaasnbimportnumpyasnpimportpandasaspdfromoasislmf.pytools.common.dataimportoasis_float
[docs]classCorrelationsData:""" This class is responsible for managing the loading and saving of correlation data from binary and CSV files. Attributes: data (Optional[pd.DataFrame): correlation data that is either loaded or saved """
def__init__(self,data:Optional[pd.DataFrame]=None)->None:""" The constructor for the CorrelationsData class. Args: data: (Optional[pd.DataFrame] default is None but if supplied must have the columns listed in CorrelationsData.COLUMNS. """
[docs]deffrom_csv(file_path:str)->"CorrelationsData":""" Loads correlations data from a CSV file. Args: file_path: (str) the path to the CSV file housing the data Returns: (CorrelationsData) the loaded data from the CSV file """returnCorrelationsData(data=pd.read_csv(file_path))
@staticmethod
[docs]deffrom_bin(file_path:str)->"CorrelationsData":""" Loads correlations data from a binary file. Args: file_path: (str) the path to the binary file housing the data Returns: (CorrelationsData) the loaded data from the binary file """returnCorrelationsData(data=pd.DataFrame(np.fromfile(file_path,dtype=Correlation)))
[docs]defto_csv(self,file_path:str)->None:""" Writes self.data to a CSV file. Args: file_path: (str) the file path for the CSV file to be written to Returns: None """self.data.to_csv(file_path,index=False)
[docs]defto_bin(self,file_path:str)->None:""" Writes self.data to a binary file. Args: file_path: (str) the file path for the binary file to be written to Returns: None """data=np.array([rforrinself.data.itertuples(index=False)],dtype=Correlation)data.tofile(file_path)
[docs]defread_correlations(input_path,ignore_file_type=set()):"""Load the correlations from the correlations file. Args: input_path (str): the path pointing to the file ignore_file_type (Set[str]): file extension to ignore when loading. Returns: Tuple[Dict[int, int], List[int], Dict[int, int], List[Tuple[int, int]], List[int]] vulnerability dictionary, vulnerability IDs, areaperil to vulnerability index dictionary, areaperil ID to vulnerability index array, areaperil ID to vulnerability array """input_files=set(os.listdir(input_path))if"correlations.bin"ininput_filesand"bin"notinignore_file_type:correlations_fname=os.path.join(input_path,'correlations.bin')logger.debug(f"loading {correlations_fname}")try:correlations=np.memmap(correlations_fname,dtype=Correlation,mode='r')exceptValueError:logger.debug("binary file is empty, numpy.memmap failed. trying to read correlations.csv.")correlations=read_correlations(input_path,ignore_file_type={'bin'})elif"correlations.csv"ininput_filesand"csv"notinignore_file_type:correlations_fname=os.path.join(input_path,'correlations.csv')logger.debug(f"loading {correlations_fname}")correlations=np.loadtxt(correlations_fname,dtype=Correlation,delimiter=",",skiprows=1,ndmin=1)else:raiseFileNotFoundError(f'correlations file not found at {input_path}')returncorrelations