nessai.utils.io

Utilities related to loading files, saving files etc.

Module Contents

Classes

NessaiJSONEncoder

Class to encode numpy arrays and other non-serialisable objects.

Functions

is_jsonable(x)

Check if an object is JSON serialisable.

safe_file_dump(data, filename, module, save_existing=False)

Safely dump data to a .pickle file.

save_live_points(live_points, filename)

Save live points to a file using JSON.

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(self, obj)

Method that returns a serialisable object

nessai.utils.io.safe_file_dump(data, filename, module, save_existing=False)

Safely dump data to a .pickle file.

See Bilby for the original impletmentation: https://git.ligo.org/michael.williams/bilby/-/blob/master/bilby/core/utils.py

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.