nessai.utils.io
Utilities related to loading files, saving files etc.
Module Contents
Classes
Class to encode numpy arrays and other non-serialisable objects. |
Functions
|
Check if an object is JSON serialisable. |
|
Save a dictionary to a JSON file. |
|
Safely dump data to a .pickle file. |
|
Save live points to a file using JSON. |
|
Encode a value for HDF5 file format. |
|
Save a dictionary to a HDF5 file. |
|
Save a dictionary to a HDF5 file. |
- nessai.utils.io.is_jsonable(x)
Check if an object is JSON serialisable.
Based on: https://stackoverflow.com/a/53112659
- Parameters:
- xobj
Object to check
- Returns:
- bool
Boolean that indicates if the object is JSON serialisable.
- class nessai.utils.io.NessaiJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Bases:
json.JSONEncoder
Class to encode numpy arrays and other non-serialisable objects.
Based on: https://stackoverflow.com/a/57915246.
Examples
This class should be used in the
cls
argument:with open(filename, 'w') as wf: json.dump(d, wf, indent=4, cls=NessaiJSONEncoder)
- default(obj)
Method that returns a serialisable object
- nessai.utils.io.save_to_json(d, filename, **kwargs)
Save a dictionary to a JSON file.
Kwargs are passed to
json.dump
. UsesNessaiJSONEncoder
by default.- Parameters:
- ddict
Dictionary to save.
- filenamestr
Filename (with the extension) to save the dictionary to. Should include the complete path.
- kwargsAny
Keyword arguments passed to
json.dump
.
- nessai.utils.io.safe_file_dump(data, filename, module, save_existing=False)
Safely dump data to a .pickle file.
See Bilby for the original implementation.
- Parameters:
- data
Data to dump.
- filenamestr
The file to dump to.
- module{pickle, dill}
The python module to use.
- save_existingbool, optional
If true move the existing file to <file>.old.
- nessai.utils.io.save_live_points(live_points, filename)
Save live points to a file using JSON.
Live points are converted to a dictionary and then saved.
- Parameters:
- live_pointsndarray
Live points to save.
- filenamestr
File to save to.
- nessai.utils.io.encode_for_hdf5(value)
Encode a value for HDF5 file format.
- Parameters:
- valueAny
Value to encode.
- Returns:
- Any
Encoded value.
- nessai.utils.io.add_dict_to_hdf5_file(hdf5_file, path, d)
Save a dictionary to a HDF5 file.
Based on
recursively_save_dict_contents_to_group
in bilby.- Parameters:
- hdf5_fileh5py.File
HDF5 file.
- pathstr
Path added to the keys of the dictionary.
- ddict
The dictionary to save.
- nessai.utils.io.save_dict_to_hdf5(d, filename)
Save a dictionary to a HDF5 file.
- Parameters:
- ddict
Dictionary to save.
- filenamestr
Filename (with the extension) to save the dictionary to. Should include the complete path.