nessai.utils.io
===============

.. py:module:: nessai.utils.io

.. autoapi-nested-parse::

   Utilities related to loading files, saving files etc.

   ..
       !! processed by numpydoc !!


Classes
-------

.. autoapisummary::

   nessai.utils.io.NessaiJSONEncoder


Functions
---------

.. autoapisummary::

   nessai.utils.io.is_jsonable
   nessai.utils.io.save_to_json
   nessai.utils.io.safe_file_dump
   nessai.utils.io.save_live_points
   nessai.utils.io.encode_for_hdf5
   nessai.utils.io.add_dict_to_hdf5_file
   nessai.utils.io.save_dict_to_hdf5


Module Contents
---------------

.. py:function:: is_jsonable(x)

   
   Check if an object is JSON serialisable.

   Based on: https://stackoverflow.com/a/53112659

   :Parameters:

       **x** : obj
           Object to check



   :Returns:

       bool
           Boolean that indicates if the object is JSON serialisable.











   ..
       !! processed by numpydoc !!

.. py:class:: NessaiJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

   Bases: :py:obj:`json.JSONEncoder`


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

   Based on: https://stackoverflow.com/a/57915246.














   .. rubric:: Examples

   This class should be used in the ``cls`` argument::

       with open(filename, 'w') as wf:
            json.dump(d, wf, indent=4, cls=NessaiJSONEncoder)

   ..
       !! processed by numpydoc !!

   .. py:method:: default(obj)

      
      Method that returns a serialisable object
















      ..
          !! processed by numpydoc !!


.. py:function:: save_to_json(d, filename, **kwargs)

   
   Save a dictionary to a JSON file.

   Kwargs are passed to :code:`json.dump`. Uses :code:`NessaiJSONEncoder` by
   default.

   :Parameters:

       **d** : dict
           Dictionary to save.

       **filename** : str
           Filename (with the extension) to save the dictionary to. Should include
           the complete path.

       **kwargs** : Any
           Keyword arguments passed to :code:`json.dump`.














   ..
       !! processed by numpydoc !!

.. py:function:: 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.

       **filename** : str
           The file to dump to.

       **module** : {pickle, dill}
           The python module to use.

       **save_existing** : bool, optional
           If true move the existing file to <file>.old.














   ..
       !! processed by numpydoc !!

.. py:function:: 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_points** : ndarray
           Live points to save.

       **filename** : str
           File to save to.














   ..
       !! processed by numpydoc !!

.. py:function:: encode_for_hdf5(value)

   
   Encode a value for HDF5 file format.


   :Parameters:

       **value** : Any
           Value to encode.



   :Returns:

       Any
           Encoded value.











   ..
       !! processed by numpydoc !!

.. py:function:: add_dict_to_hdf5_file(hdf5_file, path, d)

   
   Save a dictionary to a HDF5 file.

   Based on :code:`recursively_save_dict_contents_to_group` in bilby.

   :Parameters:

       **hdf5_file** : h5py.File
           HDF5 file.

       **path** : str
           Path added to the keys of the dictionary.

       **d** : dict
           The dictionary to save.














   ..
       !! processed by numpydoc !!

.. py:function:: save_dict_to_hdf5(d, filename)

   
   Save a dictionary to a HDF5 file.


   :Parameters:

       **d** : dict
           Dictionary to save.

       **filename** : str
           Filename (with the extension) to save the dictionary to. Should include
           the complete path.














   ..
       !! processed by numpydoc !!

