
    +dI                     N    d dl mZmZ  G d de          Z G d de          ZdS )   )
DataSourceSchemac                   X     e Zd ZdZdZdZdZdZd fd	Zd Z	d	 Z
d
 Zd Zd Zd Z xZS )	NPySourcezRead numpy binary files into an array

    Prototype source showing example of working with arrays

    Each file becomes one or more partitions, but partitioning within a file
    is only along the largest dimension, to ensure contiguous data.
    ndarraynumpyz0.0.1TNc                     || _         || _        || _        |pi | _        d| _        || _        d| _        d| _        t          t          |           
                    |           dS )a  
        The parameters dtype and shape will be determined from the first
        file, if not given.

        Parameters
        ----------
        path: str of list of str
            Location of data file(s), possibly including glob and protocol
            information
        dtype: str dtype spec
            In known, the dtype (e.g., "int64" or "f4").
        shape: tuple of int
            If known, the length of each axis
        chunks: int
            Size of chunks within a file along biggest dimension - must exactly divide
            each file, or None for one partition per file.
        storage_options: dict
            Passed to file-system backend.
        N)metadata)pathshapedtypestoragefiles_chunks_arrs_arrsuperr   __init__)selfr   r   r   chunksstorage_optionsr
   	__class__s          1lib/python3.11/site-packages/intake/source/npy.pyr   zNPySource.__init__   si    ( 	

&,"

	i''':::::    c           	         ddl m} | j        |                     | j                  d         } ||dfdd i| j        | _        | j        | j        2t          | j        d                   }|j        | _        |j        | _        | j	        p| j        d         | _
        t          | j                  }|d         | j
        z  |d         | j
        z  dk    z   }t          | j                  dk    ret          | j                  g|z   }dt          | j                  z  f| j
        f|z  fz   t          d | j        dd          D                       z   }n5|}| j
        f|z  ft          d | j        dd          D                       z   }t                       t          t!          | j                  t          |          | j        |t          | j                  z  |	          S )
N    )
open_filesrbcompressionr   )r   c              3      K   | ]}|fV  d S N .0ss     r   	<genexpr>z(NPySource._get_schema.<locals>.<genexpr>G   s'      [y[yef]^\`[y[y[y[y[y[yr   c              3      K   | ]}|fV  d S r!   r"   r#   s     r   r&   z(NPySource._get_schema.<locals>.<genexpr>J   s$      ?]?]?]?]?]?]?]?]r   )r   r   extra_metadatanpartitionsr   )fsspecr   r   
_get_cacher   r   r   r   NumpyAccessr   r   listlentupleprintr   strr
   )r   r   r   arrr   parts_per_filer   s          r   _get_schemazNPySource._get_schema7   s   %%%%%%: 	8??49--a0D#D$QQDQDLQQDJz 'TZ '!$*Q-00 Y
 Y
,7$*Q-DKTZ  (dk1eAh6Lq6PQtz??Q 	^__%-ES__,.4;.>2Q1SSV[[y[yjnjtuvuwuwjx[y[y[yVyVyyFFE{n~57%?]?]djYZY[Y[n?]?]?]:]:]]FC
OO5<<PTP]ky|  AE  AK  }L  }L  lL  U[  \  \  \  	\r   c                 *   t          |t          t          f          r|d         }| j        d         | j        z  | j        d         | j        z  dk    z   }||z  }t          | j        |                   }||z  }|| j        |z  | j        |dz   z           S )Nr   r   )
isinstancer-   r/   r   r   r,   r   )r   ir3   nfiler2   infiles         r   _get_partitionzNPySource._get_partitionO   s    a$'' 	!A*Q-4;64:a=4;;VYZ;Z[^#$*U+,,^#4;'$+!*DDEEr   c                 ,    |                      |          S r!   )r:   )r   r7   s     r   read_partitionzNPySource.read_partitionX   s    ""1%%%r   c                     dd l m                                    fd j        D             }t	          |          dk    r& j        pdfdt	           j                  dz
  z  z   n% j        pdfdt	           j                  dz
  z  z   fd|D              _                             j                   _	         j	        S )Nr   c                 F    g | ]}t          |j        j                  S r"   )r,   r   r   )r$   fr   s     r   
<listcomp>z%NPySource.to_dask.<locals>.<listcomp>_   s)    KKK1Atz4:66KKKr   r   rA      c                 <    g | ]}                     |          S r"   )
from_array)r$   r2   r   das     r   r@   z%NPySource.to_dask.<locals>.<listcomp>e   s'    AAASbmmC00AAAr   )

dask.arrayarrayr4   r   r.   r   r   r   stackr   )r   arrsr   rF   s   ` @@r   to_daskzNPySource.to_dask[   s    KKKK
KKKt99q= 	Kl(b*Uc$*oo6I-JJFFl(b*Uc$*oo6I-JJFAAAAADAAA
HHTZ((	yr   c                     dd l |                                  | j        5 }fd|D             }d d d            n# 1 swxY w Y                       |          S )Nr   c                 :    g | ]}                     |          S r"   )load)r$   r?   nps     r   r@   z"NPySource.read.<locals>.<listcomp>o   s#    +++1BGGAJJ+++r   )r   r4   r   rI   )r   ffrJ   rO   s      @r   readzNPySource.readj   s    Z 	,2+++++++D	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,xx~~s   <A A c                 "    d | _         d | _        d S r!   )r   r   )r   s    r   _closezNPySource._closer   s    
			r   )NNNNN)__name__
__module____qualname____doc__	containernameversionpartition_accessr   r4   r:   r<   rK   rQ   rS   __classcell__)r   s   @r   r   r      s          IDG; ; ; ; ; ;<\ \ \0F F F& & &          r   r   c                   "    e Zd ZddZd Zd ZdS )r,   NCc                     || _         || _        || _        || _        || _        | j        |||                                  t          | j                  | _        d S r!   )r?   r   r   orderoffset	_get_infor.   ndim)r   r?   r   r   r`   ra   s         r   r   zNumpyAccess.__init__x   s`    


: 	 	& 	NN
OO			r   c                    dd l }dd l}t          |t                    r|d         }|j        p| j        d         |j        pdz
  }|j        pd}|}| j        dd          D ]}||z  }||z  }|dk    r4 |                    g | j                  j	        d| j        dd          z    S | j
        || j        j        z  z   }|f| j        dd          z   }	|                     | j                  }
|
5 }|                    |           |                    || j        j        z            }|                    || j                  	                    |	          cd d d            S # 1 swxY w Y   d S )Nr   r   )r   rB   )copyr   r6   r/   stopr   startrH   r   reshapera   itemsizer?   seekrQ   
frombuffer)r   itemre   rO   firstblockcountr7   rg   r   fnr?   datas                r   __getitem__zNumpyAccess.__getitem__   s   dE"" 	7D+djm
a@
aABB 	 	AQJEQJEEA: 	S9288Bdj811954:abb>;QRRedj&9994:abb>)YYtv 	H1FF5MMM66%$*"5566D==TZ=88@@GG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hs   +A&EE"%E"c                 >   ddl m} | j        5 }|                    |          }|                    |           |                    ||          \  }}}|| _        || _        |rdnd| _        |	                                | _
        d d d            d S # 1 swxY w Y   d S )Nr   )formatFr^   )	numpy.librt   r?   
read_magic_check_version_read_array_headerr   r   r`   tellra   )r   rt   fprZ   r   fortran_orderr   s          r   rb   zNumpyAccess._get_info   s    $$$$$$V 	$r''++G!!'****0*C*CB*P*P'E=%DJDJ -63DJ''))DK	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   A7BBB)NNr^   N)rT   rU   rV   r   rr   rb   r"   r   r   r,   r,   w   sI        $ $ $ $H H H0$ $ $ $ $r   r,   N)baser   r   r   objectr,   r"   r   r   <module>r      s    % $ $ $ $ $ $ $i i i i i
 i i iX.$ .$ .$ .$ .$& .$ .$ .$ .$ .$r   