"""`io` module contains useful functions for loading and saving data."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/P4_HPD_cg/93_io.ipynb.

# %% auto 0
__all__ = ['epsilon', 'read_data']

# %% ../nbs/P4_HPD_cg/93_io.ipynb 2
from nbdev.showdoc import *
from fastcore.test import *
from fastcore.utils import *

# %% ../nbs/P4_HPD_cg/93_io.ipynb 4
import numpy as np
from prettytable import PrettyTable

# %% ../nbs/P4_HPD_cg/93_io.ipynb 5
from .plot import *
from .metrics import *
from .math import *

# %% ../nbs/P4_HPD_cg/93_io.ipynb 6
import h5py

# %% ../nbs/P4_HPD_cg/93_io.ipynb 7
epsilon = 1e-10

# %% ../nbs/P4_HPD_cg/93_io.ipynb 27
def read_data(
    fpath:str,  # path to the rawdata file
    R: int=1,   # acceleration factor
) -> tuple:     # output a tuple of ndarrays
    """
    Read the rawdata file and output tupple of np.ndarray of individual data
    Have a look at all the keys to see which individual data is available
    For the test brain data the individual data are rawdata, trajectory, InScale, and Coils    
    """
    data_dict = {} 
    
    with h5py.File(fpath, 'r') as f:
        keys = list(f.keys())
        print(keys)
        for key in keys:
            data_dict[key] = np.squeeze(f[key][()])
    
    rawdata = np.transpose(data_dict['rawdata'])
    trajectory = np.transpose(data_dict['trajectory'])
    
    # retrospectively undersampled
    rawdata = rawdata[:,::R,:]
    trajectory = trajectory[::R]
    
    return rawdata, trajectory, data_dict['InScale'], data_dict['Coils']
