o
     i                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZ d dlZd dl Z d d
l!m"Z" d dl#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 e9d\Z:Z;e9ddd\Z<Z;e9d\Z=Z;g dZ>deiZ?dZ@	ddddZA			 ddd)d*ZB	 ddd.d/ZC			0ddd3d4ZDd ddd e-jEfdd=d>ZFdd@dAZGdddHdCZHddIdJdKZIdLdM ZJddPdQZKddVdWZLdddXdYZMe,jNe-jOfdd[d\ZPd]d^ ZQddadbZRddddeZSdfeTd fddkdlZUdmdn ZVdodp ZWdddudvZX	0ddd}d~ZYejZfdddZ[dddZ\		 		 ddddZ]e+jOdde j^fdddZ_dddZ`			0	 	0	0ddddZa			0	 	0	0ddddZbddddZcdddZddddZeejffdddZgdddZh				dd dd̄Ziddd΄Zjddd҄ZkdddՄZldddׄZmdddلZndS (      )annotationsN)abcdefaultdict)	GeneratorIterableMappingSequenceSizeddeepcopy)reduce)productstarmapzip_longest)PurePath)Any)default_collate)NdarrayOrTensorNdarrayTensorPathLike)MetaObj)MAX_SEED	BlendModeMethodNumpyPadMode	TraceKeysconvert_data_typeconvert_to_dst_typeensure_tupleensure_tuple_repensure_tuple_sizefall_back_tuplefirstget_equivalent_dtypeissequenceiterablelook_up_optionoptional_importpandas	DataFrame)namenibabel)%
AFFINE_TOLSUPPORTED_PICKLE_MODaffine_to_spacingcompute_importance_mapcompute_shape_offsetconvert_tables_to_dicts!correct_nifti_header_if_necessarycreate_file_basenamedecollate_batchdense_patch_slicesget_random_patchget_valid_patch_sizeis_supported_format
iter_patchiter_patch_positioniter_patch_slicesjson_hashinglist_data_collateno_collationorientation_ras_lpspad_list_data_collatepartition_datasetpartition_dataset_classespickle_hashingrectify_header_sform_qformreorient_spatial_axesresample_datalistselect_cross_validation_foldsset_rndsorted_dictto_affine_ndworker_init_fnzoom_affineremove_keysremove_extra_metadataget_extra_metadata_keysis_no_channelpickleMbP?dimsSequence[int]
patch_size
rand_statenp.random.RandomState | Nonereturntuple[slice, ...]c                   sJ   |du rt jjn|j t fddt| |D }tdd t||D S )a,  
    Returns a tuple of slices to define a random patch in an array of shape `dims` with size `patch_size` or the as
    close to it as possible within the given dimension. It is expected that `patch_size` is a valid patch for a source
    of shape `dims` as returned by `get_valid_patch_size`.

    Args:
        dims: shape of source array
        patch_size: shape of patch size to generate
        rand_state: a random state object to generate random numbers from

    Returns:
        (tuple of slice): a tuple of slice objects defining the patch
    Nc                 3  s2    | ]\}}||kr d || d nd V  qdS )r      N .0mspsZrand_intrZ   R/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/data/utils.py	<genexpr>|   s   0 z#get_random_patch.<locals>.<genexpr>c                 s  "    | ]\}}t ||| V  qd S Nslice)r\   mcr^   rZ   rZ   r`   ra           )nprandomrandinttuplezip)rR   rT   rU   
min_cornerrZ   r_   r`   r5   i   s   r5   rZ           T
image_sizeSequence[int] | int	start_posoverlapSequence[float] | floatpaddedbool(Generator[tuple[slice, ...], None, None]c                 c  sB    t | |}t| ||||dD ]}tdd t||D V  qdS )a  
    Yield successive tuples of slices defining patches of size `patch_size` from an array of dimensions `image_size`.
    The iteration starts from position `start_pos` in the array, or starting at the origin if this isn't provided. Each
    patch is chosen in a contiguous grid using a rwo-major ordering.

    Args:
        image_size: dimensions of array to iterate over
        patch_size: size of patches to generate slices for, 0 or None selects whole dimension
        start_pos: starting position in the array, default is 0 for each dimension
        overlap: the amount of overlap of neighboring patches in each dimension (a value between 0.0 and 1.0).
            If only one float number is given, it will be applied to all dimensions. Defaults to 0.0.
        padded: if the image is padded so the patches can go beyond the borders. Defaults to False.

    Yields:
        Tuples of slice objects defining each patch
    )ro   rT   rq   rr   rt   c                 s  rb   rc   rd   r\   sprZ   rZ   r`   ra      rg   z$iter_patch_slices.<locals>.<genexpr>N)r6   r9   rk   rl   )ro   rT   rq   rr   rt   patch_size_positionrZ   rZ   r`   r:      s   


r:   scan_intervalreturn_slicelist[tuple[slice, ...]]c                   sN  t }tt|g }t|D ]:   dkr"|d qttt     }t	 fddt|D }||durK|d nd qg }t|D ],}	g }
t||	 D ]}||	  }|t
||	  |	  d8 }|
| q_||
 qUtdd tj|dd	iD j}|rfd
d|D S fdd|D S )a  
    Enumerate all slices defining ND patches of size `patch_size` from an `image_size` input image.

    Args:
        image_size: dimensions of image to iterate over
        patch_size: size of patches to generate slices
        scan_interval: dense patch sampling interval
        return_slice: whether to return a list of slices (or tuples of indices), defaults to True

    Returns:
        a list of slice objects defining each patch

    r   rY   c                 3  s0    | ]}|        kr|V  qd S rc   rZ   r\   diro   rT   r|   rZ   r`   ra      s   . z%dense_patch_slices.<locals>.<genexpr>Nc                 S     g | ]}|  qS rZ   )flattenr\   xrZ   rZ   r`   
<listcomp>       z&dense_patch_slices.<locals>.<listcomp>indexingijc                   &   g | ]}t  fd dt|D qS )c                 3  s&    | ]\}}t || |  V  qd S rc   rd   r\   r   rx   rT   rZ   r`   ra      s   $ 0dense_patch_slices.<locals>.<listcomp>.<genexpr>rk   	enumerater   r   rZ   r`   r         & c                   r   )c                 3  s$    | ]\}}|| |  fV  qd S rc   rZ   r   r   rZ   r`   ra         " r   r   r   r   rZ   r`   r      r   )lenr6   r    rangeappendintmathceilfloatr"   maxrh   asarraymeshgridT)ro   rT   r|   r}   num_spatial_dimsZscan_numnumZscan_dimstartsdimZ
dim_startsidx	start_idxoutrZ   r   r`   r4      s,   

 "r4   F Sequence[int] | int | np.ndarray-Sequence[float] | float | Sequence[int] | intc           
      C  s   t | }t| |}t||}t||}t|d tr'tdd t||D }ntdd t||D }|r7| ntdd t| |D }tt	t|||}	t
|	 S )a_  
    Yield successive tuples of upper left corner of patches of size `patch_size` from an array of dimensions `image_size`.
    The iteration starts from position `start_pos` in the array, or starting at the origin if this isn't provided. Each
    patch is chosen in a contiguous grid using a rwo-major ordering.

    Args:
        image_size: dimensions of array to iterate over
        patch_size: size of patches to generate slices for, 0 or None selects whole dimension
        start_pos: starting position in the array, default is 0 for each dimension
        overlap: the amount of overlap of neighboring patches in each dimension.
            Either a float or list of floats between 0.0 and 1.0 to define relative overlap to patch size, or
            an int or list of ints to define number of pixels for overlap.
            If only one float/int number is given, it will be applied to all dimensions. Defaults to 0.0.
        padded: if the image is padded so the patches can go beyond the borders. Defaults to False.

    Yields:
        Tuples of positions defining the upper left corner of each patch
    r   c                 s  s$    | ]\}}t |d |  V  qdS )      ?Nroundr\   ry   orZ   rZ   r`   ra      r   z&iter_patch_position.<locals>.<genexpr>c                 s  s    | ]	\}}|| V  qd S rc   rZ   r   rZ   rZ   r`   ra          c                 s  s$    | ]\}}|t | d  V  qdS )rY   Nr   rw   rZ   rZ   r`   ra      r   )r   r6   r    r   
isinstancer   rk   rl   r   r   r   )
ro   rT   rq   rr   rt   ndimrz   stepsend_posrangesrZ   rZ   r`   r9      s   


 r9   arrr   	copy_backmode
str | Nonepad_optsdict9Generator[tuple[NdarrayOrTensor, np.ndarray], None, None]c                 +  sp   ddl m} t| j|}t|| j}t| dd t|| jD }	t fddt||	D }
dd tt	|| j|	D } rj|| fdd |
D |d	|}td
d t||
D }tdd t| j|
D }n| }|}| j}t
|||| dD ]$} rtdd t||
D }n	tdd |D }|| t|fV  qz|rtdd t|
| jD }|| | d< dS dS )a  
    Yield successive patches from `arr` of size `patch_size`. The iteration can start from position `start_pos` in `arr`
    but drawing from a padded array extended by the `patch_size` in each dimension (so these coordinates can be negative
    to start in the padded region). If `copy_back` is True the values from each patch are written back to `arr`.

    Args:
        arr: array to iterate over
        patch_size: size of patches to generate slices for, 0 or None selects whole dimension.
            For 0 or None, padding and overlap ratio of the corresponding dimension will be 0.
        start_pos: starting position in the array, default is 0 for each dimension
        overlap: the amount of overlap of neighboring patches in each dimension (a value between 0.0 and 1.0).
            If only one float number is given, it will be applied to all dimensions. Defaults to 0.0.
        copy_back: if True data from the yielded patches is copied back to `arr` once the generator completes
        mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
            ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
            (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
            One of the listed string values or a user supplied function.
            If None, no wrapping is performed. Defaults to ``"wrap"``.
            See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            requires pytorch >= 1.10 for best compatibility.
        pad_opts: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    Yields:
        Patches of array data from `arr` which are views into a padded array which can be modified, if `copy_back` is
        True these changes will be reflected in `arr` once the iteration completes.

    Note:
        coordinate format is:

            [1st_dim_start, 1st_dim_end,
             2nd_dim_start, 2nd_dim_end,
             ...,
             Nth_dim_start, Nth_dim_end]]

    r   )pad_ndc                 S     g | ]}t |qS rZ   )ru   r\   ry   rZ   rZ   r`   r   6  r   ziter_patch.<locals>.<listcomp>c                 3  s$    | ]\}}|r r|nd V  qdS r   NrZ   )r\   ry   vrt   rZ   r`   ra   7  r   ziter_patch.<locals>.<genexpr>c                 S  s   g | ]
\}}|r
|nd qS )rn   rZ   )r\   opr   rZ   rZ   r`   r   8      c                 S  s   g | ]}||fqS rZ   rZ   r   rZ   rZ   r`   r   ;  r   )Zto_padr   c                 s      | ]	\}}|| V  qd S rc   rZ   rw   rZ   rZ   r`   ra   =  r   c                 s  r   rc   rZ   rw   rZ   rZ   r`   ra   A  r   r   c                 s  s(    | ]\}}|j | |j| fV  qd S rc   startstop)r\   coordry   rZ   rZ   r`   ra   J  s   & c                 s  s    | ]	}|j |jfV  qd S rc   r   )r\   r   rZ   rZ   r`   ra   L  r   c                 s  rb   rc   rd   )r\   ry   rx   rZ   rZ   r`   ra   Q  rg   .N)Z#monai.transforms.croppad.functionalr   r6   shaper    r   ru   rk   rl   r   r:   rh   r   )r   rT   rq   rr   r   r   r   r   rz   Zis_vZ	_pad_sizeZ_overlapZarrpadZstart_pos_paddedZ	iter_sizeslicesZcoords_no_padrZ   r   r`   r8      s0   / r8   tuple[int, ...]c                 C  s*   t | }t||}tdd t| |D S )a  
    Given an image of dimensions `image_size`, return a patch size tuple taking the dimension from `patch_size` if this is
    not 0/None. Otherwise, or if `patch_size` is shorter than `image_size`, the dimension from `image_size` is taken. This ensures
    the returned patch size is within the bounds of `image_size`. If `patch_size` is a single number this is interpreted as a
    patch of the same dimensionality of `image_size` with that size in each dimension.
    c                 s  s"    | ]\}}t ||p|V  qd S rc   minr[   rZ   rZ   r`   ra   `  rg   z'get_valid_patch_size.<locals>.<genexpr>)r   r    rk   rl   )ro   rT   r   rz   rZ   rZ   r`   r6   U  s   
r6   rY   dev_collatelevelr   logger_namestrc                   s  | d }t |}d }| dd  t| dkrdnd }t|tjrzt| d t| dW S  t	yb } z t| d| d	d
d | D  d| d W Y d}~dS d}~w t
y } z t| d| ddd | D  d| d W Y d}~dS d}~ww |jdkr|jdkr|jdkr|jdv rt| d tdd | D dS |jdkr| S nt|ttttfr| S t|tjri }|D ]' t| d  dt| d t fdd| D d d| < q|S t|tjrvt| }	t|	}
z	dd |
D W n# t	y<   dd |
D }t| d| d| d Y dS w t| d  d! tfd"d#D rht| d$ d%| d t|  }fd&d|D S t| d'| d! dS )(a  
    Recursively run collate logic and provide detailed loggings for debugging purposes.
    It reports results at the 'critical' level, is therefore suitable in the context of exception handling.

    Args:
        batch: batch input to collate
        level: current level of recursion for logging purposes
        logger_name: name of logger to use for logging

    See also: https://pytorch.org/docs/stable/data.html#working-with-collate-fn
    r   >N
   z ...  z  collate/stack a list of tensorsz E: z, type c                 S     g | ]}t |jqS rZ   type__name__r\   elemrZ   rZ   r`   r   y      zdev_collate.<locals>.<listcomp>z in collate()z, shape c                 S  s   g | ]}|j qS rZ   )r   r   rZ   rZ   r`   r   ~  s    numpystr_string_)ndarraymemmapz% collate/stack a list of numpy arraysc                 S  s   g | ]}t |qS rZ   )torch	as_tensorr\   brZ   rZ   r`   r     r   r   r   rZ   z collate dict key "z	" out of z keysc                      g | ]}|  qS rZ   rZ   r   keyrZ   r`   r     r   rY   c                 S  r   rZ   )r   r   rZ   rZ   r`   r     r   c                 S  r   rZ   r   r   rZ   rZ   r`   r     r   z	 E: type z collate list of sizes: .c                 3  s    | ]	}| d  kV  qdS r   rZ   r\   rx   )sizesrZ   r`   ra     r   zdev_collate.<locals>.<genexpr>z, collate list inconsistent sizes, got size: z, in collate(c                   s   g | ]}t | d  dqS )rY   r   )r   )r\   samplesr   rZ   r`   r         z  E: unsupported type in collate )r   r   r   r   Tensorlogging	getLoggercriticalstack	TypeErrorRuntimeError
__module__r   r   r   r   r   r   bytesr   r   r   iterlistanyrl   )batchr   r   r   	elem_typeZl_strZ	batch_strer   itZelstypes
transposedrZ   )r   r   r   r   r`   r   c  sn   $
$
$

&&"
)collate_fn_mapc                  sp   ddl m} || }dd | D }tjdd |D    r& fdd|D }t||_dd | D |_d|_|S )	z
    Collate a sequence of meta tensor into a single batched metatensor. This is called by `collage_meta_tensor`
    and so should not be used as a collate function directly in dataloaders.
    r   )collate_tensor_fnc                 S     g | ]}|j p	tjqS rZ   )metar   NONEr\   r   rZ   rZ   r`   r         z*collate_meta_tensor_fn.<locals>.<listcomp>c                 S  s"   g | ]}t |trt| qS rZ   )r   r   setkeysr   rZ   rZ   r`   r        " c                   s.   g | ] t  tr fd dD ntjqS )c                   s   i | ]}| | qS rZ   rZ   )r\   kr   rZ   r`   
<dictcomp>  r   z5collate_meta_tensor_fn.<locals>.<listcomp>.<dictcomp>)r   r   r   r  r\   Zcommon_r	  r`   r     s   . c                 S  r   rZ   )applied_operationsr   r  r  rZ   rZ   r`   r     r  T)torch.utils.data._utils.collater   r  intersectionr   r  r  is_batch)r   r   r   ZcollatedZ
meta_dictsrZ   r  r`   collate_meta_tensor_fn  s   
r  c                   sv   t  tst t }t |trt S t |tr# fdd|D S t |ttfr7 fddt	t
|D S t S )zcollate a sequence of meta tensor sequences/dictionaries into
    a single batched metatensor or a dictionary of batched metatensorc                   s$   i | ]  t  fd dD qS )c                   r   rZ   rZ   r   r  rZ   r`   r     r   z2collate_meta_tensor.<locals>.<dictcomp>.<listcomp>collate_meta_tensorr  r   r  r`   r
    s   $ z'collate_meta_tensor.<locals>.<dictcomp>c                   s"   g | ] t  fd dD qS )c                   r   rZ   rZ   r   r   rZ   r`   r     r   z2collate_meta_tensor.<locals>.<listcomp>.<listcomp>r  r  r  r  r`   r     r  z'collate_meta_tensor.<locals>.<listcomp>)r   r   NotImplementedErrorr"   r   r  r   rk   r   r   r   r   )r   Zelem_0rZ   r  r`   r    s   


r  r   r   c              
     s\  ddl m} ddlm} ||ti | d }t|tr#dd | D n| }d t}z't|t	rJi }|D ]}|  fdd|D }||| < q3|W S ||}|W S  t
y} }	 z!t|	}
d|
v rp durl|
d	  d
7 }
|
d7 }
t|}t
|
|	d}	~	w ty }	 z%t|	}
d|
v rd|
v r dur|
d	  d
7 }
|
d7 }
t|}t|
|	d}	~	ww )aI  
    Enhancement for PyTorch DataLoader default collate.
    If dataset already returns a list of batch data that generated in transforms, need to merge all data to 1 list.
    Then it's same as the default collate behavior.

    Note:
        Need to use this collate if apply some transforms that can generate batch data.

    r   )default_collate_fn_map)
MetaTensorc                 S  s   g | ]	}|D ]}|qqS rZ   rZ   )r\   r  r   rZ   rZ   r`   r         z%list_data_collate.<locals>.<listcomp>Nc                   r   rZ   rZ   r   r   rZ   r`   r     r   z
equal sizez
Collate error on the key 'z' of dictionary data.z

MONAI hint: if your transforms intentionally create images of different shapes, creating your `DataLoader` with `collate_fn=pad_list_data_collate` might solve this problem (check its documentation).r   r   z

MONAI hint: if your transforms intentionally create mixtures of torch Tensor and numpy ndarray, creating your `DataLoader` with `collate_fn=pad_list_data_collate` might solve this problem (check its documentation).)r  r  Zmonai.data.meta_tensorr  updater  r   r   r   r   r   r   r   r   )r   r  r  r   data
collate_fnretr  Zdata_for_batchreZre_str_rZ   r   r`   r<     sP   



r<   
batch_dataMapping | Iterabledetachpadc           	        s   t  tr fdd D }nt  tr#fdd D }ntd  dt  ddg }}t |tr>| nt|D ]-\}}t |tr]t |ttfs]t |t	j
rc|jdkrc|| qBt |trot|t|}qB|||fS )	a  
    Utility function based on `decollate_batch`, to identify the largest batch size from the collated data.
    returns batch_size, the list of non-iterable items, and the dictionary or list with their items decollated.

    See `decollate_batch` for more details.
    c              	     s"   i | ]}|t  | d qS )r$  
fill_valuer3   )r\   r   r!  r#  r&  r$  rZ   r`   r
  	  r  z&_non_zipping_check.<locals>.<dictcomp>c                   s   g | ]
}t | d qS r%  r'  r   )r#  r&  r$  rZ   r`   r     r   z&_non_zipping_check.<locals>.<listcomp>Unable to de-collate: , type: r   r   )r   r   r   r  r   itemsr   r   r   r   r   r   r   r	   r   r   )	r!  r#  r$  r&  Z_deco
batch_sizenon_iterabler  r   rZ   r(  r`   _non_zipping_check   s   


".

r.  c                   s  | du r| S t | ttttfst| jdkrt | ts| S t | tj	r|r)| 
 } | jdkr6|r4|  S | S tj| dd}t | trpt|t| jD ]\}}t |trY||_d|_qJt|| jD ]\}}t |tro||_d|_q`|d jdkr|rdd |D S t|S t| |||\}} |dkr S |r|D ] fddt|D  < qt  tr|rt  d	|int   }	 fd
d|	D }
|
S t  tr|rt d	|int  }	dd |	D }|S td|  dt|  d)aS	  De-collate a batch of data (for example, as produced by a `DataLoader`).

    Returns a list of structures with the original tensor's 0-th dimension sliced into elements using `torch.unbind`.

    Images originally stored as (B,C,H,W,[D]) will be returned as (C,H,W,[D]). Other information,
    such as metadata, may have been stored in a list (or a list inside nested dictionaries). In
    this case we return the element of the list corresponding to the batch idx.

    Return types aren't guaranteed to be the same as the original, since numpy arrays will have been
    converted to torch.Tensor, sequences may be converted to lists of tensors,
    mappings may be converted into dictionaries.

    For example:

    .. code-block:: python

        batch_data = {
            "image": torch.rand((2,1,10,10)),
            DictPostFix.meta("image"): {"scl_slope": torch.Tensor([0.0, 0.0])}
        }
        out = decollate_batch(batch_data)
        print(len(out))
        >>> 2

        print(out[0])
        >>> {'image': tensor([[[4.3549e-01...43e-01]]]), DictPostFix.meta("image"): {'scl_slope': 0.0}}

        batch_data = [torch.rand((2,1,10,10)), torch.rand((2,3,5,5))]
        out = decollate_batch(batch_data)
        print(out[0])
        >>> [tensor([[[4.3549e-01...43e-01]]], tensor([[[5.3435e-01...45e-01]]])]

        batch_data = torch.rand((2,1,10,10))
        out = decollate_batch(batch_data)
        print(out[0])
        >>> tensor([[[4.3549e-01...43e-01]]])

        batch_data = {
            "image": [1, 2, 3], "meta": [4, 5],  # undetermined batch size
        }
        out = decollate_batch(batch_data, pad=True, fill_value=0)
        print(out)
        >>> [{'image': 1, 'meta': 4}, {'image': 2, 'meta': 5}, {'image': 3, 'meta': 0}]
        out = decollate_batch(batch_data, pad=False)
        print(out)
        >>> [{'image': 1, 'meta': 4}, {'image': 2, 'meta': 5}]

    Args:
        batch: data to be de-collated.
        detach: whether to detach the tensors. Scalars tensors will be detached into number types
            instead of torch tensors.
        pad: when the items in a batch indicate different batch size, whether to pad all the sequences to the longest.
            If False, the batch size will be the length of the shortest sequence.
        fill_value: when `pad` is True, the `fillvalue` to use when padding, defaults to `None`.
    Nr   r   r   Fc                 S  r   rZ   )item)r\   trZ   rZ   r`   r   i  r   z#decollate_batch.<locals>.<listcomp>c                   s   g | ]}t   qS rZ   r
   )r\   r   decor  rZ   r`   r   q  r  	fillvaluec                   s   g | ]	}t t |qS rZ   )r   rl   r\   r0  )r3  rZ   r`   r   t  r  c                 S  r   rZ   )r   r5  rZ   rZ   r`   r   {  r   r)  r*  r   )r   r   r   r   r   r   r   r   r   r   r#  r   r0  unbindr   rl   r3   r  r  r  r   r.  r   r   r   valuesr  )r   r#  r$  r&  Zout_listr1  mr   r-  Z_genr  ret_listrZ   r2  r`   r3     sP   8




"
r3   methodc                 K  s$   ddl m} |d||d|| S )aS  
    Function version of :py:class:`monai.transforms.croppad.batch.PadListDataCollate`.

    Same as MONAI's ``list_data_collate``, except any tensors are centrally padded to match the shape of the biggest
    tensor in each dimension. This transform is useful if some of the applied transforms generate batch data of
    different sizes.

    This can be used on both list and dictionary data.
    Note that in the case of the dictionary data, this decollate function may add the transform information of
    `PadListDataCollate` to the list of invertible transforms if input batch have different spatial shape, so need to
    call static method: `monai.transforms.croppad.batch.PadListDataCollate.inverse` before inverting other transforms.

    Args:
        batch: batch of data to pad-collate
        method: padding method (see :py:class:`monai.transforms.SpatialPad`)
        mode: padding mode (see :py:class:`monai.transforms.SpatialPad`)
        kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    r   )PadListDataCollate)r:  r   NrZ   )Zmonai.transforms.croppad.batchr;  )r   r:  r   kwargsr;  rZ   rZ   r`   r?     s   r?   c                 C  s   | S )z%
    No any collation operation.
    rZ   )r   rZ   rZ   r`   r=     s   r=   	worker_idNonec                 C  s    t jj }t|j|jd dS )z
    Callback function for PyTorch DataLoader `worker_init_fn`.
    It can set different random seed for the transforms in different workers.

    seedN)r   utilsr  get_worker_inforG   datasetr@  )r=  worker_inforZ   rZ   r`   rJ     s   rJ   r@  c                 C  s   t | ttfr|}| D ]}t||d}q||kr|S |d S t| ds%|S t| dr6| j|t d |d S | jD ]}|drAq9t| j| |d}q9|S )z
    Set seed or random state for all randomizable properties of obj.

    Args:
        obj: object to set seed or random state for.
        seed: set the random state with an integer seed.
    r?  rY   __dict__set_random_state__)	r   rk   r   rG   hasattrrF  r   rE  
startswith)objr@  _seedr0  r   rZ   rZ   r`   rG     s   



rG      affiner   rsuppress_zerosc                 C  s   t | jdks| jd | jd krtd| j dt| d|d|f | |d^}}t|tjr>ttj|| dd}nt	t	j|| dd	}|rRd
||dk< t|| |d^}}|S )ap  
    Computing the current spacing from the affine matrix.

    Args:
        affine: a d x d affine matrix.
        r: indexing based on the spatial rank, spacing is computed from `affine[:r, :r]`.
        dtype: data type of the output.
        suppress_zeros: whether to suppress the zeros with ones.

    Returns:
        an `r` dimensional vector of spacing.
       r   rY   z$affine must be a square matrix, got r   N)dstdtyper/  axisr   )
r   r   
ValueErrorr   r   r   r   sqrtsumrh   )rM  rN  rR  rO  _affiner   spacingZspacing_rZ   rZ   r`   r-     s   ""r-   c                 C  sz   | j ddu r
| S | j d d }|dkr| S t| j  d| }t| j|d}t||r2| S t| dr;t	| S | S )z
    Check nifti object header's format, update the header if needed.
    In the updated image pixdim matches the affine.

    Args:
        img_nii: nifti image object
    r   Nr      rN  	get_sform)
headergetrh   r   	get_zoomsr-   rM  allcloserH  rC   )img_niir   pixdimZnorm_affinerZ   rZ   r`   r1     s   
r1   c           
      C  s   | j d d }t| j  d| }|  |  }}t||d}t||d}t|| }t|| }| j d dkrL|sA| S |sL| |   | S | j d dkrb|sW| S |sb| 	|   | S t| j
|d}	| j |	 | S )a  
    Look at the sform and qform of the nifti object and correct it if any
    incompatibilities with pixel dimensions

    Adapted from https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/io/misc_io.py

    Args:
        img_nii: nifti image object
    r   r   Nr[  Z
sform_codeZ
qform_code)r]  rh   r   r_  r\  Z	get_qformr-   r`  Z	set_sformZ	set_qformrM  Z	set_zooms)
ra  r   rb  ZsformqformZ
norm_sformZ
norm_qformZsform_mismatchZqform_mismatchnormrZ   rZ   r`   rC     s,   
rC   
np.ndarrayscalenp.ndarray | Sequence[float]diagonalc                 C  sT  t j| tdd} t| t| d kr#tdt|  dt| d  dt j|tdd}t| d }t| |d}t||k rIt ||t|d	 }|d	| }t t||}d
||dk< |rit 	t |d
gS | d	dd	df }t j
|j| j}|t j
| }t t 	|t | }	t t| }
|t 	|	 |
d	dd	df< |
S )a#  
    To make column norm of `affine` the same as `scale`.  If diagonal is False,
    returns an affine that combines orthogonal rotation and the new scale.
    This is done by first decomposing `affine`, then setting the zoom factors to
    `scale`, and composing a new affine; the shearing factors are removed.  If
    diagonal is True, returns a diagonal matrix, the scaling factors are set
    to the diagonal elements.  This function always return an affine with zero
    translations.

    Args:
        affine (nxn matrix): a square matrix.
        scale: new scaling factor along each dimension. if the components of the `scale` are non-positive values,
            will use the corresponding components of the original pixdim, which is computed from the `affine`.
        diagonal: whether to return a diagonal scaling matrix.
            Defaults to True.

    Raises:
        ValueError: When ``affine`` is not a square matrix.
        ValueError: When ``scale`` contains a nonpositive scalar.

    Returns:
        the updated `n x n` affine.

    TrR  copyr   zaffine must be n x n, got z x r   rY   r[  Nr   )rh   arrayr   r   rU  r-   r   r   r!   diaglinalgcholeskyr   invsignabseye)rM  rf  rh  Zscale_npr   rd  Zrzszsrotationrx   
new_affinerZ   rZ   r`   rK     s(   "rK   spatial_shapenp.ndarray | Sequence[int]	in_affine
out_affinescale_extenttuple[np.ndarray, np.ndarray]c              
     s@  t j| dtd}t|}tt||t jd }tt||t jd } fdd|D }t t j|ddi	t|df}	t 
|	t |	d	d
 f}	zt j|||	 }
W n t jjym } z	td| d|d	}~ww ||	 }	|
d	d  }|
d	d |
d  }
 rt t j|
d
dnt t j|
d
dd }d	}t|	jd
 D ]&}t ||d	d	||d
 f  d
}t j|dtdr|	d	d|f } nq|d	u r|d	dd	df |d  |d	ddf  |d	dd	df |d   } rt d|| d  d}t || |d  d	d t | }|jtdd|fS )a  
    Given input and output affine, compute appropriate shapes
    in the output space based on the input array's shape.
    This function also returns the offset to put the shape
    in a good position with respect to the world coordinate system.

    Args:
        spatial_shape: input array's shape
        in_affine (matrix): 2D affine matrix
        out_affine (matrix): 2D affine matrix
        scale_extent: whether the scale is computed based on the spacing or the full extent of voxels, for example, for
            a factor of 0.5 scaling:

            option 1, "o" represents a voxel, scaling the distance between voxels::

                o--o--o
                o-----o

            option 2, each voxel has a physical extent, scaling the full voxel extent::

                | voxel 1 | voxel 2 | voxel 3 | voxel 4 |
                |      voxel 1      |      voxel 2      |

            Option 1 may reduce the number of locations that requiring interpolation. Option 2 is more resolution
            agnostic, that is, resampling coordinates depend on the scaling factor, not on the number of voxels.
            Default is False, using option 1 to compute the shape and offset.

    T)rj  rR  r   c                   s(   g | ]} rd |d fnd|d fqS )g            ?rn   r   rZ   )r\   r   r{  rZ   r`   r   w     ( z(compute_shape_offset.<locals>.<listcomp>r   r   rk  NrY   zAffine z is not invertiblerS  r   rn   )rtol       @r}  F)rj  )rh   rl  r   r   r   rI   r   r   r   reshapeconcatenate	ones_likern  solveLinAlgErrorrU  rj  r   ptpr   r   r   r`  r+   r   rr  rq  astyper   )rw  ry  rz  r{  r   srZ
in_affine_Zout_affine_Z	in_coordscornersZcorners_outr   Zall_dist	out_shapeoffsetr   rm   Z	in_offsetrZ   r~  r`   r/   Q  s<   "$0$H(r/   np.ndarray | intc           	      C  s"  t |tj}t|tj|ddd }| }|jdkr#td|j dtj| |dd}|jdkrRt|	tj
}t|rA|dk rItd| dtj|d	 |d
}ttt|d	 t|d	 d	}|d|d|f |d|d|f< |d	kr|d|df |d|df< t|||d
^}}|S )aP  
    Using elements from affine, to create a new affine matrix by
    assigning the rotation/zoom/scaling matrix and the translation vector.

    When ``r`` is an integer, output is an (r+1)x(r+1) matrix,
    where the top left kxk elements are copied from ``affine``,
    the last column of the output affine is copied from ``affine``'s last column.
    `k` is determined by `min(r, len(affine) - 1)`.

    When ``r`` is an affine matrix, the output has the same shape as ``r``,
    and the top left kxk elements are copied from ``affine``,
    the last column of the output affine is copied from ``affine``'s last column.
    `k` is determined by `min(len(r) - 1, len(affine) - 1)`.

    Args:
        r (int or matrix): number of spatial dimensions or an output affine to be filled.
        affine (matrix): 2D affine matrix
        dtype: data type of the output array.

    Raises:
        ValueError: When ``affine`` dimensions is not 2.
        ValueError: When ``r`` is nonpositive.

    Returns:
        an (r+1) x (r+1) matrix (tensor or ndarray depends on the input ``affine`` data type)

    T)output_typerR  wrap_sequencer   rP  z#affine must have 2 dimensions, got r   ri  zr must be positive, got rY   )rR  Nrk  )r#   rh   r   r   rj  r   rU  rl  r   r  uintisfiniters  r   r   r   r   )	rN  rM  rR  Z	affine_nprv  r  r   outputr   rZ   rZ   r`   rI     s"   

 $rI   
data_shapeinit_affinetarget_affine"tuple[np.ndarray, NdarrayOrTensor]c              
   C  s   t |tj^}}t |tj^}}tj|}tj|}z	tj||}W n ty= }	 ztd| d| d|	d}	~	ww |tj||  }
t	|
|^}
}||
fS )ac  
    Given the input ``init_affine``, compute the orientation transform between
    it and ``target_affine`` by rearranging/flipping the axes.

    Returns the orientation transform and the updated affine (tensor or ndarray
    depends on the input ``affine`` data type).
    Note that this function requires external module ``nibabel.orientations``.
    zThe input affine z and target affine z are not compatible.N)
r   rh   r   nibZorientationsZio_orientationornt_transformrU  Zinv_ornt_affr   )r  r  r  Zinit_affine_r   Ztarget_affine_Z
start_orntZtarget_orntr  r   rv  rZ   rZ   r`   rD     s   rD   r   postfixinput_file_namer   folder_pathdata_root_dirseparate_foldermakedirsc                 C  s   t j|\}}t j|\}}	|	dkrt j|\}}	d}
|r)|r)t j||}
t j||
}|r9t j||}|rBt j|dd t j|| dkrP|d |  n|}|dur^|d| 7 }t j|S )a  
    Utility function to create the path to the output file based on the input
    filename (file name extension is not added by this function).
    When ``data_root_dir`` is not specified, the output file name is:

        `folder_path/input_file_name (no ext.) /input_file_name (no ext.)[_postfix][_patch_index]`

    otherwise the relative path with respect to ``data_root_dir`` will be inserted, for example:

    .. code-block:: python

        from monai.data import create_file_basename
        create_file_basename(
            postfix="seg",
            input_file_name="/foo/bar/test1/image.png",
            folder_path="/output",
            data_root_dir="/foo/bar",
            separate_folder=True,
            makedirs=False)
        # output: /output/test1/image/image_seg

    Args:
        postfix: output name's postfix
        input_file_name: path to the input image file.
        folder_path: path for the output file
        data_root_dir: if not empty, it specifies the beginning parts of the input file's
            absolute path. This is used to compute `input_file_rel_path`, the relative path to the file from
            `data_root_dir` to preserve folder structure when saving in case there are files in different
            folders with the same file names.
        separate_folder: whether to save every file in a separate folder, for example: if input filename is
            `image.nii`, postfix is `seg` and folder_path is `output`, if `True`, save as:
            `output/image/image_seg.nii`, if `False`, save as `output/image_seg.nii`. default to `True`.
        patch_index: if not None, append the patch index to filename.
        makedirs: whether to create the folder if it does not exist.
    z.gzr   T)exist_okr   N)ospathsplitsplitextrelpathjoinr  normpath)r  r  r  r  r  patch_indexr  ZfiledirfilenameextZfiledir_rel_pathr  rZ   rZ   r`   r2     s    ."r2   g      ?cpuBlendMode | strsigma_scaledevicetorch.device | int | strrR  torch.dtype | str | Nonetorch.Tensorc           
      C  s8  t |t}t|}|tjkrtj| |tjd}ni|tjkrrt|t	| }dd t
| |D }tt	| D ];}tj| | d  d | | d d d tj|d}t|d d|| d   }|d	krn|d
|d|   n|}q5ntd| dtj dtj dtt| d}	tj|tj|	d|}|S )a9  Get importance map for different weight modes.

    Args:
        patch_size: Size of the required importance map. This should be either H, W [,D].
        mode: {``"constant"``, ``"gaussian"``}
            How to blend output of overlapping windows. Defaults to ``"constant"``.

            - ``"constant``": gives equal weight to all predictions.
            - ``"gaussian``": gives less weight to predictions on edges of windows.

        sigma_scale: Sigma_scale to calculate sigma for each dimension
            (sigma = sigma_scale * dim_size). Used for gaussian mode only.
        device: Device to put importance map on.
        dtype: Data type of the output importance map.

    Raises:
        ValueError: When ``mode`` is not one of ["constant", "gaussian"].

    Returns:
        Tensor of size patch_size.

    )r  rR  c                 S  s   g | ]\}}|| qS rZ   rZ   )r\   r   Zsigma_srZ   rZ   r`   r   E  r  z*compute_importance_map.<locals>.<listcomp>rY   r  )r   endrR  r  rP  r   rk  rc   zUnsupported mode: z, available options are [z, z].rQ   r   )r%   r   r   r  CONSTANTonesr   GAUSSIANr   r   rl   r   arangeexp	unsqueezerU  r   r   r0  clamp_to)
rT   r   r  r  rR  Zimportance_mapsigmasr   r   Zmin_non_zerorZ   rZ   r`   r.   "  s(   



($r.   r  Sequence[PathLike] | PathLikesuffixesSequence[str]c                   sJ   t | }|D ]}dttjt|j t fdd|D r" dS qdS )a  
    Verify whether the specified file or files format match supported suffixes.
    If supported suffixes is None, skip the verification and return True.

    Args:
        filename: file name or a list of file names to read.
            if a list of files, verify all the suffixes.
        suffixes: all the supported image suffixes of current reader, must be a list of lower case suffixes.

    r   c                 3  s"    | ]}d |    vV  qdS )r   N)lowerr   Zfull_suffixrZ   r`   ra   e  rg   z&is_supported_format.<locals>.<genexpr>FT)r   r  mapr   r  r   r  all)r  r  	filenamesr)   rZ   r  r`   r7   W  s   r7   r  ratiosSequence[float] | Nonenum_partitions
int | Noneshuffle	drop_lasteven_divisiblec                   sr  t  }g }tt|}	|rtj|}
|
|	 |rId}t|}|D ]#}|}t|t	|| | d  |}|
 fdd|	|| D  q#|S |sOtd|sW|rWtd||k rctd| d|ru|| dkrut|| | }nt|| }|r|| n|}|s|| dkr|	|	d	||  7 }	n|	d	| }	t|D ]}|	||| }|
 fd
d|D  q|S )a>
  
    Split the dataset into N partitions. It can support shuffle based on specified random seed.
    Will return a set of datasets, every dataset contains 1 partition of original dataset.
    And it can split the dataset based on specified ratios or evenly split into `num_partitions`.
    Refer to: https://pytorch.org/docs/stable/distributed.html#module-torch.distributed.launch.

    Note:
        It also can be used to partition dataset for ranks in distributed training.
        For example, partition dataset before training and use `CacheDataset`, every rank trains with its own data.
        It can avoid duplicated caching content in each rank, but will not do global shuffle before every epoch:

        .. code-block:: python

            data_partition = partition_dataset(
                data=train_files,
                num_partitions=dist.get_world_size(),
                shuffle=True,
                even_divisible=True,
            )[dist.get_rank()]

            train_ds = SmartCacheDataset(
                data=data_partition,
                transform=train_transforms,
                replace_rate=0.2,
                cache_num=15,
            )

    Args:
        data: input dataset to split, expect a list of data.
        ratios: a list of ratio number to split the dataset, like [8, 1, 1].
        num_partitions: expected number of the partitions to evenly split, only works when `ratios` not specified.
        shuffle: whether to shuffle the original dataset before splitting.
        seed: random seed to shuffle the dataset, only works when `shuffle` is True.
        drop_last: only works when `even_divisible` is False and no ratios specified.
            if True, will drop the tail of the data to make it evenly divisible across partitions.
            if False, will add extra indices to make the data evenly divisible across partitions.
        even_divisible: if True, guarantee every partition has same length.

    Examples::

        >>> data = [1, 2, 3, 4, 5]
        >>> partition_dataset(data, ratios=[0.6, 0.2, 0.2], shuffle=False)
        [[1, 2, 3], [4], [5]]
        >>> partition_dataset(data, num_partitions=2, shuffle=False)
        [[1, 3, 5], [2, 4]]
        >>> partition_dataset(data, num_partitions=2, shuffle=False, even_divisible=True, drop_last=True)
        [[1, 3], [2, 4]]
        >>> partition_dataset(data, num_partitions=2, shuffle=False, even_divisible=True, drop_last=False)
        [[1, 3, 5], [2, 4, 1]]
        >>> partition_dataset(data, num_partitions=2, shuffle=False, even_divisible=False, drop_last=False)
        [[1, 3, 5], [2, 4]]

    r   r}  c                      g | ]} | qS rZ   rZ   r  r  rZ   r`   r     r   z%partition_dataset.<locals>.<listcomp>z,must specify number of partitions or ratios.z1drop_last only works when even_divisible is True.z)there is no enough data to be split into z partitions.Nc                   r  rZ   rZ   r\   jr  rZ   r`   r     r   )r   r   r   rh   ri   RandomStater  rW  r   r   r   rU  r   r   r   )r  r  r  r  r@  r  r  data_lendatasetsindicesrsnext_idxZrsumrN  r   num_samples
total_sizer   _indicesrZ   r  r`   r@   k  s>   >
"r@   classesc              
     s   t |rt|t krtd| dt  dg }tt}	t|D ]\}
}|	| |
 q#g }t|	 D ]"\}}t	|||||||d}|sK|}q7t
||D ]\}}||7 }qPq7tj|}|D ]}|rk|| | fdd|D  qb|S )a_  
    Split the dataset into N partitions based on the given class labels.
    It can make sure the same ratio of classes in every partition.
    Others are same as :py:class:`monai.data.partition_dataset`.

    Args:
        data: input dataset to split, expect a list of data.
        classes: a list of labels to help split the data, the length must match the length of data.
        ratios: a list of ratio number to split the dataset, like [8, 1, 1].
        num_partitions: expected number of the partitions to evenly split, only works when no `ratios`.
        shuffle: whether to shuffle the original dataset before splitting.
        seed: random seed to shuffle the dataset, only works when `shuffle` is True.
        drop_last: only works when `even_divisible` is False and no ratios specified.
            if True, will drop the tail of the data to make it evenly divisible across partitions.
            if False, will add extra indices to make the data evenly divisible across partitions.
        even_divisible: if True, guarantee every partition has same length.

    Examples::

        >>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
        >>> classes = [2, 0, 2, 1, 3, 2, 2, 0, 2, 0, 3, 3, 1, 3]
        >>> partition_dataset_classes(data, classes, shuffle=False, ratios=[2, 1])
        [[2, 8, 4, 1, 3, 6, 5, 11, 12], [10, 13, 7, 9, 14]]

    zlength of classes z must match the dataset length r   )r  r  r  r  r@  r  r  c                   r  rZ   rZ   r  r  rZ   r`   r     r   z-partition_dataset_classes.<locals>.<listcomp>)r$   r   rU  r   r   r   r   sortedr+  r@   rl   rh   ri   r  r  )r  r  r  r  r  r@  r  r  r  Zclass_indicesr   cZclass_partition_indicesr   Zper_class_indicesZper_class_partition_indicespartZdata_indicesr  r  rZ   r  r`   rA     s8   #	

rA   factorr   random_pickc                 C  sf   t |\}}t }tt|D ]}|tt|  q|dkr1|t| |d| g||dd  |S )aY  
    Utility function to resample the loaded datalist for training, for example:
    If factor < 1.0, randomly pick part of the datalist and set to Dataset, useful to quickly test the program.
    If factor > 1.0, repeat the datalist to enhance the Dataset.

    Args:
        data: original datalist to scale.
        factor: scale factor for the datalist, for example, factor=4.5, repeat the datalist 4 times and plus
            50% of the original datalist.
        random_pick: whether to randomly pick data if scale factor has decimal part.
        seed: random seed to randomly pick data.

    gư>rY   )r  r  r  r@  r   )r   modfr   r   r   extendr   r@   )r  r  r  r@  rf  repeatsr  r   rZ   rZ   r`   rE     s   "rE   
partitionsSequence[Iterable]foldsr   c                   s    fddt |D S )a  
    Select cross validation data based on data partitions and specified fold index.
    if a list of fold indices is provided, concatenate the partitions of these folds.

    Args:
        partitions: a sequence of datasets, each item is a iterable
        folds: the indices of the partitions to be combined.

    Returns:
        A list of combined datasets.

    Example::

        >>> partitions = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
        >>> select_cross_validation_folds(partitions, 2)
        [5, 6]
        >>> select_cross_validation_folds(partitions, [1, 2])
        [3, 4, 5, 6]
        >>> select_cross_validation_folds(partitions, [-1, 2])
        [9, 10, 5, 6]
    c                   s   g | ]} | D ]}|qqS rZ   rZ   )r\   Zfold_idZ	data_itemr  rZ   r`   r   L  r   z1select_cross_validation_folds.<locals>.<listcomp>)r   )r  r  rZ   r  r`   rF   6  s   rF   r   c                 C  s\   d}t jjdk rttj| ddd }ntjtj| ddddd }|  S )z_

    Args:
        item: data item to be hashed

    Returns: the corresponding hash key

    r   	   T)	sort_keyszutf-8Fusedforsecurity)	sysversion_infominorhashlibmd5jsondumpsencode	hexdigest)r0  	cache_keyrZ   rZ   r`   r;   O  s   
 
r;   c                 C  sX   d}t jjdk rttjt| |d }ntjtjt| |ddd }| 	 S )z

    Args:
        item: data item to be hashed
        protocol: protocol version used for pickling,
            defaults to `pickle.HIGHEST_PROTOCOL`.

    Returns: the corresponding hash key

    r   r  )protocolFr  )
r  r  r  r  r  rP   r  rH   r  r  )r0  r  r  rZ   rZ   r`   rB   c  s   
rB   c                 C  s*   t | ts| S dd t|  ||dD S )z/Return a new sorted dictionary from the `item`.c                 S  s(   i | ]\}}|t |trt|n|qS rZ   )r   r   rH   r\   r  r   rZ   rZ   r`   r
  |  r  zsorted_dict.<locals>.<dictcomp>)r   reverse)r   r   r  r+  )r0  r   r  rZ   rZ   r`   rH   x  s   
rH   row_indicesSequence[int | str] | None	col_namesSequence[str] | None	col_types'dict[str, dict[str, Any] | None] | None
col_groupsdict[str, Sequence[str]] | Nonelist[dict[str, Any]]c                   sJ  t fddt| }g }|du r|j }n*|D ]'}t|ttfr;t|dkr,td|	tt
|d |d  q|| q|du rJ|j| n|j||f }	t|trydd	 | D }
|
rg|	j|
d
}	dd	 | D }|ry|	j|dd}	|	jdd}|duri  | D ]\}}|j||f j |< q fddt|D }|S )a-  
    Utility to join pandas tables, select rows, columns and generate groups.
    Will return a list of dictionaries, every dictionary maps to a row of data in tables.

    Args:
        dfs: data table in pandas Dataframe format. if providing a list of tables, will join them.
        row_indices: indices of the expected rows to load. it should be a list,
            every item can be a int number or a range `[start, end)` for the indices.
            for example: `row_indices=[[0, 100], 200, 201, 202, 300]`. if None,
            load all the rows in the file.
        col_names: names of the expected columns to load. if None, load all the columns.
        col_types: `type` and `default value` to convert the loaded columns, if None, use original data.
            it should be a dictionary, every item maps to an expected column, the `key` is the column
            name and the `value` is None or a dictionary to define the default value and data type.
            the supported keys in dictionary are: ["type", "default"], and note that the value of `default`
            should not be `None`. for example::

                col_types = {
                    "subject_id": {"type": str},
                    "label": {"type": int, "default": 0},
                    "ehr_0": {"type": float, "default": 0.0},
                    "ehr_1": {"type": float, "default": 0.0},
                }

        col_groups: args to group the loaded columns to generate a new column,
            it should be a dictionary, every item maps to a group, the `key` will
            be the new column name, the `value` is the names of columns to combine. for example:
            `col_groups={"ehr": [f"ehr_{i}" for i in range(10)], "meta": ["meta_1", "meta_2"]}`
        kwargs: additional arguments for `pandas.merge()` API to join tables.

    c                   s   t j| |fi  S rc   )pdmerge)lrN  )r<  rZ   r`   <lambda>  r   z)convert_tables_to_dicts.<locals>.<lambda>NrP  z:range of row indices must contain 2 values: start and end.r   rY   c                 S  s0   i | ]\}}|d ur| dd ur||d qS )Ndefault)r^  r  rZ   rZ   r`   r
    s   0 z+convert_tables_to_dicts.<locals>.<dictcomp>)valuec                 S  s*   i | ]\}}|d urd|v r||d qS )Nr   rZ   r  rZ   rZ   r`   r
    s   * Fri  records)orientc                   s2   g | ]\ }t |fi  fd d D qS )c                   s   i | ]	\}}||  qS rZ   rZ   r  r  rZ   r`   r
    r  z6convert_tables_to_dicts.<locals>.<listcomp>.<dictcomp>)r   r+  r   )groupsr  r`   r     s   2 z+convert_tables_to_dicts.<locals>.<listcomp>)r   r   indextolistr   rk   r   r   rU  r  r   r   locr   r+  fillnar  to_dictr7  r   )dfsr  r  r  r  r<  dfrowsr   data_defaultsr   r  r)   colsrZ   )r  r<  r`   r0     s2   ' 
r0   c                 C  s   t | jd d d}ddgg dg dg}|t|d d dg|d   }t| tjr8tt|| |  S t	|
| j|  S )z
    Convert the ``affine`` between the `RAS` and `LPS` orientation
    by flipping the first two spatial dimensions.

    Args:
        affine: a 2D affine matrix.
    r   rY   rk  )rk  rk  rY   )rk  rk  rY   rY   rP  rL  )r   r   r   r   r   r   rm  r   r  rh   r  rR  )rM  r  Zflip_dZ	flip_diagrZ   rZ   r`   r>     s    r>   r  	list[str]c                 C  s   |D ]}|  |d}qdS )z
    Remove keys from a dictionary. Operates in-place so nothing is returned.

    Args:
        data: dictionary to be modified.
        keys: keys to be deleted from dictionary.

    Returns:
        `None`
    N)pop)r  r  r  r   rZ   rZ   r`   rL     s   rL   r  c                 C  s   t  }t| |d dS )z
    Remove extra metadata from the dictionary. Operates in-place so nothing is returned.

    Args:
        meta: dictionary containing metadata to be modified.

    Returns:
        `None`
    )r  r  N)rN   rL   )r  r  rZ   rZ   r`   rM     s   
rM   c                  C  sB   ddddddddd	d
dgdd t dD dd t dD } | S )z|
    Get a list of unnecessary keys for metadata that can be removed.

    Returns:
        List of keys to be removed.
    Zsrow_xZsrow_yZsrow_zZ	quatern_bZ	quatern_cZ	quatern_dZ	qoffset_xZ	qoffset_yZ	qoffset_zr   rb  c                 S     g | ]}d | dqS )zdim[]rZ   r  rZ   rZ   r`   r     r  z+get_extra_metadata_keys.<locals>.<listcomp>   c                 S  r  )zpixdim[r  rZ   r  rZ   rZ   r`   r     r  )r   )r  rZ   rZ   r`   rN     s"   rN   c                 C  sL   t | tjrtt| S t | tr| dkS t| r"tt| S | du S )zPReturns whether `val` indicates "no_channel", for MetaKeys.ORIGINAL_CHANNEL_DIM.Z
no_channelN)r   r   r   ru   isnanr   rh   isscalar)valrZ   rZ   r`   rO     s   

rO   rc   )rR   rS   rT   rS   rU   rV   rW   rX   )rZ   rn   T)ro   rS   rT   rp   rq   rS   rr   rs   rt   ru   rW   rv   )T)
ro   rS   rT   rS   r|   rS   r}   ru   rW   r~   )rZ   rn   F)
ro   rS   rT   r   rq   rS   rr   r   rt   ru   )r   r   rT   rp   rq   rS   rr   rs   r   ru   r   r   r   r   rW   r   )ro   rS   rT   r   rW   r   )rY   r   )r   r   r   r   )r   r   )r!  r"  r#  ru   r$  ru   )TTN)r#  ru   )r   r   r:  r   r   r   )r=  r   rW   r>  )r@  r   rW   r   )rM  r   rN  r   rO  ru   rW   r   )rM  re  rf  rg  rh  ru   )F)
rw  rx  ry  r   rz  r   r{  ru   rW   r|  )rN  r  rM  r   rW   r   )r  rS   r  r   r  r   rW   r  )r   TNT)r  r   r  r   r  r   r  r   r  ru   r  ru   rW   r   )rT   r   r   r  r  rs   r  r  rR  r  rW   r  )r  r  r  r  rW   ru   )NNFr   FF)r  r   r  r  r  r  r  ru   r@  r   r  ru   r  ru   )r  r   r  rS   r  r  r  r  r  ru   r@  r   r  ru   r  ru   )Fr   )r  r   r  r   r  ru   r@  r   )r  r  r  rp   rW   r   )rW   r   )NF)NNNN)
r  r  r  r  r  r  r  r  rW   r  )rM  r   rW   r   )r  r   r  r  rW   r>  )r  r   rW   r>  )rW   r  )rW   ru   )o
__future__r   r  r  r   r   r  rP   r  collectionsr   r   collections.abcr   r   r   r   r	   rj  r   	functoolsr   	itertoolsr   r   r   pathlibr   typingr   r   rh   r   r  r   monai.config.type_definitionsr   r   r   monai.data.meta_objr   monai.utilsr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r  r   r(   r  __all__r,   r+   r5   r:   r4   r9   WRAPr8   r6   r   r  r  r<   r.  r3   	SYMMETRICr  r?   r=   rJ   rG   r   r-   r1   rC   rK   r/   float64rI   rD   r2   float32r.   r7   r@   rA   rE   rF   r;   HIGHEST_PROTOCOLrB   rH   r0   r>   rL   rM   rN   rO   rZ   rZ   rZ   r`   <module>   s   H)#.2
V@

9i


%8?
/M
5qD


	M"