Skip to content

happy.io

Jan Kukačka edited this page Mar 11, 2022 · 2 revisions

happy.io

Module for reading and writing files.

Functions

  • load: load a file
  • save: save data to a file
  • ensure_folder: check if a folder exists and create it if not

load(filename, **kwargs):

Loads file. Handles multiple formats (.mat, .nii, .nii.gz, .hdr, .mha, .npy, .npz, .pkl, .json, .csv) and selects the appropriate method to open them.

Arguments

  • filename: string with the file path or file_info dictionary containing the "filename" record.
  • kwargs:
    • key: string. Key in .mat or .npz file. If no key is given, this method tries to infer it automatically and if multiple keys are available, returns the whole dictionary.

save(filename, data, overwrite=False, parents=False, **kwargs):

Saves data to a file. Supports various file formats (Nifti: .nii.gz, .gif, Pickle: .pkl)

Arguments

  • filename: string with the file path or file_info dictionary containing the "filename" record.
  • data: array or dict or whatever suitable data to save.
  • overwrite: bool. If the file exists, is it okay to overwrite it? Default False.
  • parents: bool. Should the parent directories be created if they don't already exist? Default False.
  • kwargs:
    • is_vector: for nifti images. See save_nifti for details.
    • spacing: for nifti images. See save_nifti for details.
    • directoon: for nifti images. See save_nifti for details.
    • origin: for nifti images. See save_nifti for details.
    • key: string. Key in .mat or .npz file.

ensure_folder(path):

Make sure a folder exists.

Arguments

  • path: path to the folder whose existence should be ensured

Returns

  • True if it exists or was created, False if this function failed to ensure its existence.