oasislmf.pytools.common.data¶
Attributes¶
Functions¶
|
Generates *_header, *_dtype and *_fmt items given a list of tuples describing some output description |
|
|
|
Load a file as a numpy ndarray |
|
Load file as a single numpy array, |
|
Writes a custom dtype array with headers to csv with the provided row_fmt str |
|
|
|
Resolve file path to open file or use sys.stdin |
Module Contents¶
- oasislmf.pytools.common.data.generate_output_metadata(output)[source]¶
Generates *_header, *_dtype and *_fmt items given a list of tuples describing some output description output description has type List(Tuple({name: str}, {type: Any}, {format: str}))
- oasislmf.pytools.common.data.aggregate_vulnerability_id = ('aggregate_vulnerability_id', 'i4', '%d')[source]¶
- oasislmf.pytools.common.data.peril_correlation_group = ('peril_correlation_group', 'i4', '%d')[source]¶
- oasislmf.pytools.common.data.load_as_ndarray(dir_path, name, _dtype, must_exist=True, col_map=None)[source]¶
Load a file as a numpy ndarray useful for multi-columns files
- Parameters:
dir_path – path to the directory where the binary or csv file is stored
name – name of the file
_dtype – np.dtype
must_exist – raise FileNotFoundError if no file is present
col_map – name re-mapping to change name of csv columns
- Returns:
numpy ndarray
- oasislmf.pytools.common.data.load_as_array(dir_path, name, _dtype, must_exist=True)[source]¶
Load file as a single numpy array, useful for files with a binary version with only one type of value where their index correspond to an id. For example coverage.bin only contains tiv value for each coverage id coverage_id n correspond to index n-1
- Parameters:
dir_path – path to the directory where the binary or csv file is stored
name – name of the file
_dtype – numpy dtype of the required array
must_exist – raise FileNotFoundError if no file is present
- Returns:
numpy array of dtype type
- oasislmf.pytools.common.data.write_ndarray_to_fmt_csv(output_file, data, headers, row_fmt, use_cython=True)[source]¶
Writes a custom dtype array with headers to csv with the provided row_fmt str
This function is a faster replacement for np.savetxt as it formats each row one at a time before writing to csv. We create one large string, and formats all the data at once, and writes all the data at once.
WARNING: untested with string types in custom data.
- Parameters:
output_file (io.TextIOWrapper) – CSV file
data (ndarray[<custom dtype>]) – Custom dtype ndarray with column names
row_fmt (str) – Format for each row in csv
use_cython (bool) – Use the Cython implementation. Default: True.
- oasislmf.pytools.common.data.resolve_file(path, mode, stack)[source]¶
Resolve file path to open file or use sys.stdin
- Parameters:
path (str | os.PathLike) – File path or “-” indicationg standard input/output.
mode (str) – Mode to open file (“r”, “rb, “w”, “wb”).
stack (ExitStack) – Context manager stack used to manage file lifecycle.
- Returns:
A file-like object opened in the specified mode.
- Return type:
file (IO)