Managing IN/OUT data
Utilities for simple file I/O used in examples and scripts: saving/loading dictionaries, appending columns to plain-text tables, reading columnar data, and exporting sparse matrices.
Utilities for saving and loading dictionaries, tabular text data, and sparse matrices.
This module provides lightweight I/O helpers used by examples and scripts:
store Python dictionaries with
pickleor NumPy.npzarchives,append data series to a simple comma-separated text file format,
read tabular text files back into NumPy arrays,
export sparse matrices to a human-readable
.datfile.
- edlgt.tools.manage_data.save_dictionary(dictionary, filename, file_format=None, compressed=True)[source]
Serialize a dictionary to a pickle file or NumPy
.npzarchive.- Parameters:
dictionary (
dict) – Dictionary to save.filename (
str) – Output file path. Iffile_formatis omitted, the archive format is inferred from the extension:.npzselects the NumPy archive path, while every other extension defaults to pickle.file_format (
strorNone, optional) – Explicit archive format. Supported values are"pickle"/"pkl"and"npz". When omitted, the format is inferred fromfilename.compressed (
bool, optional) – Compression flag used only for.npzarchives. It is ignored for pickle output.
- Returns:
Final archive filename written to disk. For
.npzarchives the suffix is appended automatically when missing.- Return type:
- Raises:
TypeError – If
dictionary,filename,file_format, orcompressedhas an invalid type.ValueError – If
file_formatis unsupported or if a value cannot be represented safely in.npzformat.
Notes
The
.npzpath is intended for flat dictionaries of array-like values. Nested Python objects should be stored with pickle instead.
- edlgt.tools.manage_data.load_dictionary(filename, file_format=None)[source]
Load a dictionary stored with pickle or as a NumPy
.npzarchive.- Parameters:
filename (
str) – Path to the stored dictionary. Iffile_formatis omitted,.npzfiles are detected from the extension. Whenfile_format="npz", the suffix is added automatically if missing. For convenience, a bare file name also resolves tofilename + ".npz"when that archive exists.file_format (
strorNone, optional) – Explicit archive format. Supported values are"pickle"/"pkl"and"npz". When omitted, the format is inferred fromfilename.
- Returns:
Deserialized dictionary. For
.npzarchives, 0-dimensional arrays are converted back to Python scalars while higher-dimensional entries are returned as NumPy arrays.- Return type:
- Raises:
TypeError – If
filenameorfile_formathas an invalid type.ValueError – If
file_formatis unsupported.
- edlgt.tools.manage_data.save_data_in_textfile(data_file, x_data, new_data)[source]
Append a data series as a new column in a simple text table.
The file format is line-based and comma-separated. On first write, the file is created and initialized with the values from
x_data(usually an x-axis label followed by x values). On subsequent writes, each line gets one extra comma-separated entry fromnew_data.- Parameters:
data_file (
str) – Path to the text file to create/update.x_data (
list) – Reference column written when the file does not yet exist. Conventionally the first item is a label and the remaining items are x values.new_data (
list) – Column to append to the file. It must contain the same number of entries as the current file line count. Conventionally the first item is a label.
- Return type:
- Raises:
TypeError – If the input arguments have invalid types.
Notes
This helper assumes
x_dataandnew_dataare already aligned line by line. It does not validate lengths or parse values.
- edlgt.tools.manage_data.load_data_from_textfile(data_file_name, row_for_labels=False)[source]
Load columnar numeric data from a comma-separated text file.
- Parameters:
- Returns:
Dictionary containing one NumPy array per column under string keys
"0","1", … . Ifrow_for_labelsisTrue, label entries are also included.- Return type:
- Raises:
TypeError – If
data_file_nameorrow_for_labelshas an invalid type.
Notes
Data values are converted to
float. This helper expects a regular comma-separated table with the same number of columns on each line.
- edlgt.tools.manage_data.save_sparse_matrix_to_dat(sparse_matrix, filename)[source]
Export a sparse matrix to a human-readable
.dattext file.The output contains:
a header line with the matrix dimension,
one line per non-zero entry with row/column indices and complex value.
- Parameters:
sparse_matrix (
scipy.sparse.spmatrix) – Sparse matrix to export.filename (
str) – Output file path.
- Return type:
- Raises:
TypeError – If
sparse_matrixorfilenamehas an invalid type.