
    h5                       % S r SSKJr  SSKrSSKrSSKrSSKJrJ	r	J
r
  SSKJrJr  SSKJr  SSKJr  SSKJrJrJr  SS	KJr   SSKrS
rSSKJr  / SQr0 rS\S'   0 rS\S'   S S jr  " S S\	5      r! " S S\!S9r"S!S"S jjr# S#     S$S jjr$ S#     S%S jjr%S&S jr&S'S jr'  S(         S)S jjr(  S*       S+S jjr)S,S jr*S-S jr+g! \ a    Sr Nf = f).a  Module for serialization and deserialization of Albumentations transforms.

This module provides functionality to serialize transforms to JSON or YAML format and
deserialize them back. It implements the Serializable interface that allows transforms
to be converted to and from dictionaries, which can then be saved to disk or transmitted
over a network. This is particularly useful for saving augmentation pipelines and
restoring them later with the exact same configuration.
    )annotationsN)ABCABCMetaabstractmethod)MappingSequence)Enum)Path)AnyLiteralTextIO)warnTF)__version__)	from_dictloadsaveto_dictzdict[str, SerializableMeta]SERIALIZABLE_REGISTRYNON_SERIALIZABLE_REGISTRYc                n    U R                  S5      nUS:X  d  U R                  S5      (       d  U $ XS-   S  $ )N.zalbumentations.   )rfind
startswith)class_fullnamesplit_indexs     [/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/core/serialization.pyshorten_class_namer   '   sE     &&s+K b 9 9:K L L /+,,    c                  l   ^  \ rS rSrSrSU 4S jjr\S	S j5       r\S
S j5       r\SS j5       r	Sr
U =r$ )SerializableMeta3   zA metaclass that is used to register classes in `SERIALIZABLE_REGISTRY` or `NON_SERIALIZABLE_REGISTRY`
so they can be found later while deserializing transformation pipeline using classes full names.
c                   > [         TU ]  " XU/UQ70 UD6nUS:w  aO  [        U;  aE  UR                  5       (       a  U[        UR                  5       '   U$ U[        UR                  5       '   U$ )NSerializable)super__new__r   is_serializabler   get_class_fullnamer   )clsnamebasesargskwargscls_obj	__class__s         r   r'   SerializableMeta.__new__8   ss    '/#UDTDVD>!c&6&&((FM%g&@&@&BC  KR)'*D*D*FGr    c                    g)NF r*   s    r   r(    SerializableMeta.is_serializableA   s    r    c                    [        U 5      $ N)get_shortest_class_fullnamer4   s    r   r)   #SerializableMeta.get_class_fullnameE   s    *3//r    c                    0 $ r7   r3   r4   s    r   _to_dictSerializableMeta._to_dictI   s    	r    r3   )
r+   strr,   ztuple[type, ...]r-   r   r.   r   returnr"   r>   boolr>   r=   r>   dict[str, Any])__name__
__module____qualname____firstlineno____doc__r'   classmethodr(   r)   r;   __static_attributes____classcell__)r0   s   @r   r"   r"   3   sI       0 0  r    r"   c                  r    \ rS rSr\\SS j5       5       r\\S	S j5       5       r\S
S j5       rSSS jjr	Sr
g)r%   N   c                    [         er7   NotImplementedErrorr4   s    r   r(   Serializable.is_serializableO   
     "!r    c                    [         er7   rO   r4   s    r   r)   Serializable.get_class_fullnameT   rR   r    c                    [         er7   rO   )selfs    r   to_dict_privateSerializable.to_dict_privateY   s    !!r    c                    US;  a  SU S3n [        U5      e U R                  5       n[        US
.$ ! [         a<    US:X  a  e 0 n[        R                  " SU  SU R
                  R                   S3SS	9   NNf = f)aY  Take a transform pipeline and convert it to a serializable representation that uses only standard
python data types: dictionaries, lists, strings, integers, and floats.

Args:
    self (Serializable): A transform that should be serialized. If the transform doesn't implement the `to_dict`
        method and `on_not_implemented_error` equals to 'raise' then `NotImplementedError` is raised.
        If `on_not_implemented_error` equals to 'warn' then `NotImplementedError` will be ignored
        but no transform parameters will be serialized.
    on_not_implemented_error (str): `raise` or `warn`.

>   r   raisez(Unknown on_not_implemented_error value: z . Supported values are: 'raise' rZ   z2Got NotImplementedError while trying to serialize z;. Object arguments are not preserved. The transform class 'z`' needs to implement 'to_dict_private' or inherit from BasicTransform to be properly serialized.   
stacklevel)r   	transform)
ValueErrorrW   rP   warningsr   r0   rD   r   )rV   on_not_implemented_errormsgtransform_dicts       r   r   Serializable.to_dict]   s     $+<<<=U<VVvwCS/!	!113N  +HH # 
	'72NMMDTF K((,(?(?'@ A<= 	
	s   3 AA98A9r3   Nr?   rA   rB   rZ   )ra   r=   r>   rC   )rD   rE   rF   rG   rI   r   r(   r)   rW   r   rJ   r3   r    r   r%   r%   N   sX    "  " "  " " "I Ir    r%   )	metaclassc                $    U R                  U5      $ )a^  Take a transform pipeline and convert it to a serializable representation that uses only standard
python data types: dictionaries, lists, strings, integers, and floats.

Args:
    transform (Serializable): A transform that should be serialized. If the transform doesn't implement
        the `to_dict` method and `on_not_implemented_error` equals to 'raise' then `NotImplementedError` is raised.
        If `on_not_implemented_error` equals to 'warn' then `NotImplementedError` will be ignored
        but no transform parameters will be serialized.
    on_not_implemented_error (str): `raise` or `warn`.

)r   )r^   ra   s     r   r   r   }   s     566r    c                    U R                  S5      [        ;   a?  U S   nUc  SU S3n [        U5      eUR                  U5      nU c  [        SU S35      eU$ g )N__class_fullname__rD   z6To deserialize a non-serializable transform with name z you need to pass a dict withz Non-serializable transform with z# was not found in `nonserializable`)getr   r_   )r^   nonserializabler+   rb   result_transforms        r   instantiate_nonserializablerm      sy     }})*.GG$"J4&PmnC@S/!*..t4?vEhijjr    c                   [        5         U S   n[        X!5      nU(       a  U$ US   nUR                  5        VVs0 s H  u  pVUS:w  d  M  XV_M     nnnSU;  a  US;  a  [        SU S3SS9  S	US'   [        [        U5         nS
U;   a#  US
    V	s/ s H  n	[        SU	0US9PM     sn	US
'   U" S0 UD6$ s  snnf s  sn	f )a  Args:
transform_dict: A dictionary with serialized transform pipeline.
nonserializable (dict): A dictionary that contains non-serializable transforms.
    This dictionary is required when you are restoring a pipeline that contains non-serializable transforms.
    Keys in that dictionary should be named same as `name` arguments in respective transforms from
    a serialized pipeline.

r^   ri   p)Compose
Sequentialz
Transform z; has no 'p' parameter in serialized data, defaulting to 0.5r[   r\   g      ?
transformsrk   r3   )register_additional_transformsrm   itemsr   r   r   r   )
rc   rk   r^   lmbdr+   kvr-   r*   ts
             r   r   r      s     #${+I&yBD)*D&__.L.TQ!7K2KDAD.DL $4'@@z$Z[hijS	
 24 8
9Ctdhiudvwdv_`ia(8/Zdvw\;; M xs   B7B7B=c                .    U S;  a  [        SU  S35      eg )N>   jsonyamlzUnknown data_format z*. Supported formats are: 'json' and 'yaml')r_   )data_formats    r   check_data_formatr~      s&    **/}<fghh +r    c                r   [        U [        5      (       a/  U R                  5        VVs0 s H  u  pU[        U5      _M     snn$ [        U [        5      (       a1  [        U [
        5      (       d  U  Vs/ s H  n[        U5      PM     sn$ [        U [        5      (       a  U R                  $ U $ s  snnf s  snf )zpRecursively search for Enum objects and convert them to their value.
Also handle any Mapping or Sequence types.
)
isinstancer   ru   serialize_enumr   r=   r	   value)objrw   rx   s      r   r   r      s     #w14=>!$$==#x  C)=)=+./3aq!3//"3--399636 >/s   B.3B4c                .   [        U5        U R                  US9n[        U5      n[        U[        [
        45      (       at  [        U5      R                  S5       nUS:X  a.  [        (       d  Sn[        U5      e[        R                  " XESS9  OUS:X  a  [        R                  " XE5        SSS5        gUS:X  a.  [        (       d  Sn[        U5      e[        R                  " XASS9  gUS:X  a  [        R                  " XAS	S
9  gg! , (       d  f       g= f)a  Serialize a transform pipeline and save it to either a file specified by a path or a file-like object
in either JSON or YAML format.

Args:
    transform (Serializable): The transform pipeline to serialize.
    filepath_or_buffer (Union[str, Path, TextIO]): The file path or file-like object to write the serialized
        data to.
        If a string is provided, it is interpreted as a path to a file. If a file-like object is provided,
        the serialized data will be written to it directly.
    data_format (str): The format to serialize the data in. Valid options are 'json' and 'yaml'.
        Defaults to 'json'.
    on_not_implemented_error (str): Determines the behavior if a transform does not implement the `to_dict` method.
        If set to 'raise', a `NotImplementedError` is raised. If set to 'warn', the exception is ignored, and
        no transform arguments are saved. Defaults to 'raise'.

Raises:
    ValueError: If `data_format` is 'yaml' but PyYAML is not installed.

)ra   wr|   z<You need to install PyYAML to save a pipeline in YAML formatF)default_flow_styler{   Nr[   )indent)r~   r   r   r   r=   r
   openyaml_availabler_   r|   	safe_dumpr{   dump)r^   filepath_or_bufferr}   ra   rc   frb   s          r   r   r      s    2 k"&&@X&YN#N3N $sDk22$%**3/1f$%~XC$S/)~UK&		., 0/ 
	~PCS/!~eT				.Q? 
 0/s   AD
Dc                   [        U5        [        U [        [        45      (       an  [        U 5      R	                  5        nUS:X  a  [
        R                  " U5      nO.[        (       d  Sn[        U5      e[        R                  " U5      nSSS5        OKUS:X  a  [
        R                  " U 5      nO.[        (       d  Sn[        U5      e[        R                  " U 5      n[        WUS9$ ! , (       d  f       N= f)a  Load a serialized pipeline from a file or file-like object and construct a transform pipeline.

Args:
    filepath_or_buffer (Union[str, Path, TextIO]): The file path or file-like object to read the serialized
        data from.
        If a string is provided, it is interpreted as a path to a file. If a file-like object is provided,
        the serialized data will be read from it directly.
    data_format (Literal["json", "yaml"]): The format of the serialized data.
        Defaults to 'json'.
    nonserializable (Optional[dict[str, Any]]): A dictionary that contains non-serializable transforms.
        This dictionary is required when restoring a pipeline that contains non-serializable transforms.
        Keys in the dictionary should be named the same as the `name` arguments in respective transforms
        from the serialized pipeline. Defaults to None.

Returns:
    object: The deserialized transform pipeline.

Raises:
    ValueError: If `data_format` is 'yaml' but PyYAML is not installed.

r{   z<You need to install PyYAML to load a pipeline in yaml formatNrs   )r~   r   r=   r
   r   r{   r   r   r_   r|   	safe_loadr   )r   r}   rk   r   rc   rb   s         r   r   r      s    4 k"$sDk22$%**,f$!%1%~XC$S/)!%!2 -, 
	#56~PCS/!(:;^_EE! -,s    AC**
C8c                     [         R                  R                  S5      b   SSKn U R                  R
                  ngg! [         a     gf = f)zRegister transforms that are not imported directly into the `albumentations` module by checking
the availability of optional dependencies.
torchNr   )	importlibutil	find_specalbumentations.pytorchpytorch
ToTensorV2ImportError)albumentations_s     r   rt   rt   ,  sM     ~~(4	) &&11A 5  		s   > 
A
Ac                N    U R                    SU R                   3n[        U5      $ )a\  The function `get_shortest_class_fullname` takes a class object as input and returns its shortened
full name.

:param cls: The parameter `cls` is of type `Type[BasicCompose]`, which means it expects a class that
is a subclass of `BasicCompose`
:type cls: Type[BasicCompose]
:return: a string, which is the shortened version of the full class name.
r   )rE   rD   r   )r*   r   s     r   r8   r8   ;  s(     'q7Nn--r    )r   r=   r>   r=   re   )r^   r%   ra   r=   r>   rC   r7   )r^   rC   rk   dict[str, Any] | Noner>   Serializable | None)rc   rC   rk   r   r>   r   )r}   Literal['json', 'yaml']r>   None)r   r   r>   r   )r{   rZ   )
r^   r%   r   str | Path | TextIOr}   r   ra   zLiteral['raise', 'warn']r>   r   )r{   N)r   r   r}   r   rk   r   r>   object)r>   r   )r*   z	type[Any]r>   r=   ),rH   
__future__r   importlib.utilr   r{   r`   abcr   r   r   collections.abcr   r   enumr	   pathlibr
   typingr   r   r   r   r|   r   r   r   r   __all__r   __annotations__r   r   r"   r%   r   rm   r   r~   r   r   r   rt   r8   r3   r    r   <module>r      st   #    , , -   ' ' N
 '
2 68 2 79; 6 ;	-w 6,I- ,I^7" .2* & .2"* >i
7 ,29@	-@-@+-@ )-@ 7	-@
 
-@d ,2-1-F+-F(-F +-F 	-F`
.C	  Ns   C CC