
    [hG!                     f   S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	J
r
JrJr  SSKrSSKJr   " S S\5      r\ " S	 S
5      5       rS\S\4S jrS\S\4S jrS\S\\   4S jrS\S\	\\4   4S jrSS.S\	\\4   S\R0                  \R2                     S\S\	\\R0                  \   4   4S jjrg)z
Classes and functions for reshaping Stan output.

Especially with the addition of tuples, Stan writes
flat arrays of data with a rich internal structure.
    )	dataclass)Enum)prod)AnyDictIterableListTupleNc                        \ rS rSrSrSrSrSrg)VariableType             N)__name__
__module____qualname____firstlineno__SCALARCOMPLEXTUPLE__static_attributes__r       aC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\stanio/reshape.pyr   r      s    FGEr   r   c                   J   \ rS rSr% Sr\\S'   \\S'   \\S'   \\S4   \S'   \	\S'   \
S    \S	'   SS
\S\R                  4S jjrS\\   4S jrS\4S jrS\4S jrSS\R$                  S\S\R$                  4S jjrSS\R$                  S\S\R*                  \   4S jjrSrg)Variable   aq  
This class represents a single output variable of a Stan model.

It contains information about the name, dimensions, and type of the
variable, as well as the indices of where that variable is located in
the flattened output array Stan models write.

Generally, this class should not be instantiated directly, but rather
created by the :func:`parse_header()` function.
name	start_idxend_idx.
dimensionstypecontentstopreturnc           	         U R                   [        R                  :X  a[  [        U R                  5       VVs/ s H"  u  p#[        US-   5      UR                  SS94PM$     nnn[        R                  " U5      nO]U R                   [        R                  :X  a  [        R                  nO.U R                   [        R                  :X  a  [        R                  nU(       a  W$ [        R                  " WU R                  45      $ s  snnf )Nr   F)r%   )r#   r   r   	enumerater$   strdtypenpr   float64r   
complex128r"   )selfr%   iparameltsr*   s         r   r*   Variable.dtype2   s    99*** !*$-- 8 8HA QUU[[U[34 8   HHTNEYY,---JJEYY,...MMEL88UDOO455s   )Dc                 B    [        U R                  U R                  5      $ N)ranger    r!   r.   s    r   columnsVariable.columnsC   s    T^^T\\22r   c                 ,    [        U R                  5      $ r4   )r   r"   r6   s    r   num_eltsVariable.num_eltsF   s    DOO$$r   c                 4    U R                   U R                  -
  $ r4   )r!   r    r6   s    r   elt_sizeVariable.elt_sizeI   s    ||dnn,,r   srcoffsetc           	      N  ^ U R                   U-   nU R                  U-   nU R                  [        R                  :X  a&  USX424   R
                  " S/U R                  Q7SS06$ U R                  [        R                  :X  aq  USX424   R
                  " SS/U R                  Q7SS06nUS S 2S S S24   SUS S 2SS S24   -  -   nUR                  5       R
                  " S/U R                  Q7SS06$ U R                  [        R                  :X  Ga  [        R                  " [        UR                  S S 5      [        U R                  5      4[        S9n[        U R!                  5       5       H  nXpR#                  5       -  U R!                  5       -  nU R$                   V	s/ s H  n	U	R'                  XU-   S	9PM     n
n	[        U
S
   R                  S
   5       H  m[)        U4S jU
 5       5      UTU4'   M     M     UR
                  " S/U R                  Q7SS06$ g s  sn	f )N.orderFr   y              ?r   )r*   )r@   r   c              3   ,   >#    U  H	  oT   v   M     g 7fr4   r   ).0eltr/   s     r   	<genexpr>+Variable._extract_helper.<locals>.<genexpr>b   s     '?$3A$s   )r    r!   r#   r   r   reshaper"   r   squeezer   r+   emptyr   shapeobjectr5   r:   r=   r$   _extract_helpertuple)r.   r?   r@   startendretoutidxoffr0   r1   r/   s              @r   rO   Variable._extract_helperN   s   'llV#99+++sEI~&..rODOOO3OOYY,...c59n%--b!QdooQSQCa1f+SADqD\ 11C;;=((IdooISIIYY,,,, hhciin%tDOO'<=VC T]]_-MMO+t}}> "&!. ))#ck)B!.   tAw}}Q/0A"''?$'?"?C3K 1 . ;;r?DOO?3?? -s   #H"rN   c                    U R                  U5      nU(       d  UR                  U R                  5       5      nUR                  S:  a0  UR                  " / UR
                  SS QU R                  Q7SS06nU$ UR                  SS9nU$ )a   
Given an array where the final dimension is the flattened output of a
Stan model, (e.g. one row of a Stan CSV file), extract the variable
and reshape it to the correct type and dimensions.

This will most likely result in copies of the data being made if
the variable is not a scalar.

Parameters
----------
src : np.ndarray
    The array to extract from.

    Indicies besides the final dimension are preserved
    in the output.

object : bool
    If True, the output of tuple types will be an object array,
    otherwise it will use custom dtypes to represent tuples.

Returns
-------
npt.NDArray[Any]
    The extracted variable, reshaped to the correct dimensions.
    If the variable is a tuple, this will be an object array,
    otherwise it will have a dtype of either float64 or complex128.
r   NrB   rC   rD   r   )axis)rO   astyper*   ndimrJ   rM   r"   rK   )r.   r?   rN   rT   s       r   extract_reshapeVariable.extract_reshapee   s}    8 ""3'**TZZ\*C88a<++Ksyy"~KKsKC 
 ++1+%C
r   r   N)T)r   )r   r   r   r   __doc__r)   __annotations__intr
   r   r	   boolr+   r*   r   r7   r:   r=   ndarrayrO   nptNDArrayr   r\   r   r   r   r   r   r      s    	 I NL c3h
:6 6 6"3# 3%# %-# -
@2:: @s @2:: @.$2:: $t $s{{SVGW $ $r   r   tupr&   c                 2    SU R                  SS5      S   -   $ )Ndummy_:r   split)re   s    r   _munge_first_tuplerk      s    ciiQ'***r   r0   c                 N    U R                  S5      S   R                  S5      S   $ )N.r   rh   ri   )r0   s    r   _get_base_namern      s&    ;;sA$$S)!,,r   headerc                 V   U R                  5       S-   n U R                  S5      n/ nSn[        US   5      n[        S[	        U5      S-
  5       GHU  nX   n[        XS-      5      nXt:w  d  M  SU;  a~  UR                  S5      SS  nSU;   d  SU;   a  [
        R                  n	US S	 nO[
        R                  n	UR                  [        UUUS-   [        [        [        U5      5      U	/ S
95        OUR                  S5      S   R                  S5      SS  nSR                  [        R                  [        [         XUS-    5      5      5      n
UR                  [        UUUS-   [        [        [        U5      5      [
        R"                  [%        U
5      S
95        US-   nUnGMX     U$ )Nz,__dummy,r   r   rh   rm   z.realz.imagrB   )r   r    r!   r"   r#   r$   )striprj   rn   r5   lenr   r   r   appendr   rP   mapr`   joindictfromkeysrk   r   _from_header)ro   entriesparamsr    r   r/   entry	next_namedimsr#   munged_headers              r   ry   ry      s   \\^j(Fll3GFI'!*%D1c'lQ&'
"7q5>2	%{{3'+e#w%'7'//D9D'..D!"+ !A#(S$#8!!#	 {{3'*005ab9 #MM#&8'aRSe:T"UV! !"+ !A#(S$#8)//!-m!<	 AIDQ (T Mr   c                 X    [        U 5       Vs0 s H  oR                  U_M     sn$ s  snf )a  
Given a comma-separated list of names of Stan outputs, like
that from the header row of a CSV file, parse it into a dictionary of
:class:`Variable` objects.

Parameters
----------
header : str
    Comma separated list of Stan variables, including index information.
    For example, an ``array[2] real foo` would be represented as
    ``foo.1,foo.2``.

Returns
-------
Dict[str, Variable]
    A dictionary mapping the base name of each variable to a :class:`Variable`.
)ry   r   )ro   r0   s     r   parse_headerr      s*    $ ,8+?@+?%JJ+?@@@s   'TrN   
parameterssourcerN   c          	      ~    U R                  5        Vs0 s H  nUR                  UR                  XS9_M     sn$ s  snf )a  
Given a dictionary of :class:`Variable` objects and a source array,
extract the variables from the source array and reshape them to the
correct dimensions.

Parameters
----------
parameters : Dict[str, Variable]
    A dictionary of :class:`Variable` objects,
    like that returned by :func:`parse_header()`.
source : npt.NDArray[np.float64]
    The array to extract from.
object : bool
    If True, the output of tuple types will be an object array,
    otherwise it will use custom dtypes to represent tuples.

Returns
-------
Dict[str, npt.NDArray[Any]]
    A dictionary mapping the base name of each variable to the extracted
    and reshaped data.
r   )valuesr   r\   )r   r   rN   r0   s       r   stan_variablesr      sI    <  &&((E 	

E))&)@@(  s   $:)r^   dataclassesr   enumr   mathr   typingr   r   r   r	   r
   numpyr+   numpy.typingrc   r   r   r)   rk   rn   ry   r   rd   r,   ra   r   r   r   r   <module>r      s    "   3 3  4  r r rj+C +C +-# -# -1 1h 1hA Ac8m!4 A2 	S(]#KK

# 	
 
#s{{3
 r   