U
    Ph<                     @  s   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZ ed	\ZZed	d
G dd deZdS )    )annotationsN)Sequence)ndarray)PathLike)ImageReader)is_supported_format)FastMRIKeysoptional_importrequire_pkgh5py)pkg_namec                   @  sP   e Zd ZdZdddddZdddd	d
ZdddddZdddddZdS )FastMRIReaderam  
    Load fastMRI files with '.h5' suffix. fastMRI files, when loaded with "h5py",
    are HDF5 dictionary-like datasets. The keys are:

    - kspace: contains the fully-sampled kspace
    - reconstruction_rss: contains the root sum of squares of ifft of kspace. This
        is the ground-truth image.

    It also has several attributes with the following keys:

    - acquisition (str): acquisition mode of the data (e.g., AXT2 denotes T2 brain MRI scans)
    - max (float): dynamic range of the data
    - norm (float): norm of the kspace
    - patient_id (str): the patient's id whose measurements were recorded
    zSequence[PathLike] | PathLikebool)filenamereturnc                 C  s   dg}t ot||S )z
         Verify whether the specified file format is supported by h5py reader.

        Args:
             filename: file name
        z.h5)has_h5pyr   )selfr   suffixes r   ]/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/reconstruction/fastmri_reader.pyverify_suffix.   s    zFastMRIReader.verify_suffixdict)datar   c              	     sx   t |ttfr|d }t|dD t fdd D  fdd jD  tjt	j
|fg }W 5 Q R X    |S )z
        Read data from specified h5 file.
        Note that the returned object is a dictionary.

        Args:
            data: file name to read.
        r   rc                   s   g | ]}| | d  fqS )r   r   .0keyfr   r   
<listcomp>F   s     z&FastMRIReader.read.<locals>.<listcomp>c                   s   g | ]}| j | fqS r   )attrsr   r   r   r   r   G   s     )
isinstancetuplelistr   Filer   r    r   FILENAMEospathbasenameclose)r   r   datr   r   r   read8   s    zFastMRIReader.readztuple[ndarray, dict])r*   r   c                 C  s^   |  |}t|tj }tj| krDtt|tj dd n
t|j	|tj< ||fS )a  
        Extract data array and metadata from the loaded data and return them.
        This function returns two objects, first is numpy array of image data, second is dict of metadata.

        Args:
            dat: a dictionary loaded from an h5 file
        r   )N.N)
_get_meta_dictnparrayr   KSPACEMASKkeysexpand_dimszerosshape)r   r*   headerr   r   r   r   get_dataN   s    

zFastMRIReader.get_datac                   s    fddt D S )z
        Get all the metadata of the loaded dict and return the meta dict.

        Args:
            dat: a dictionary object loaded from an h5 file.
        c                   s$   i | ]}|j  kr|j  |j  qS r   )value)r   kr*   r   r   
<dictcomp>f   s     
  z0FastMRIReader._get_meta_dict.<locals>.<dictcomp>)r   )r   r*   r   r9   r   r,   _   s    zFastMRIReader._get_meta_dictN)__name__
__module____qualname____doc__r   r+   r6   r,   r   r   r   r   r      s
   
r   )
__future__r   r&   collections.abcr   numpyr-   r   monai.configr   monai.data.image_readerr   monai.data.utilsr   monai.utilsr   r	   r
   r   r   r   r   r   r   r   <module>   s   