
     i<V                    &   d Z ddlZd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
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 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 d	d
lmZmZmZ 	 d	dl m!Z! n# e"$ r  e"d          w xY wd Z#d Z$ddZ%edd            Z&ddZ'e(dfdZ)d Z*d Z+d Z,d Z-ej.         G d de	j/        ej0                              Z1d Z2d Z3d Z4d  Z5d! Z6d" Z7d# Z8d$Z9 ej:        e9          Z;d% Z< G d& d'e=          Z> G d( d)e=          Z?d* Z@d+ ZAi d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQZBdR eBC                                D             ZDi dSd9dTd9dUd9dVd9dWd9dXd9dYd9dZd9d[d1d\d1d]d3d^d3d_d=d`d=dadIdbd/dcd/ddd/iZEi ZFeFG                    eB           eFG                    eE           de ZH ej:        dfejI        ejJ        z            ZKdg ZLdh ZM G di dj          ZNddkZOddlZPdm ZQdn ZR G do dpeS          ZTdq ZU eV            fdrZWds ZX eXdt          du             ZYdv ZZdw Z[dx Z\ G dy dze=          Z]d{ Z^d| Z_d} Z`d~ Zad ZbdecdedfdZedecfdZfdS )a~  
Helper functions --- :mod:`MDAnalysis.lib.util`
====================================================

Small helper functions that don't fit anywhere else.

.. versionchanged:: 0.11.0
   Moved mathematical functions into lib.mdamath

.. versionchanged::2.0.0
   The following aliases, that existed for compatibility with python versions
   older than 3.6, were removed: `callable` for the built-in of the same name,
   `PathLike` for :class:`os.PathLike`, and `bz_open` for :func:`bz2.open`.


Files and directories
---------------------

.. autofunction:: filename
.. autofunction:: openany
.. autofunction:: anyopen
.. autofunction:: greedy_splitext
.. autofunction:: which
.. autofunction:: realpath
.. autofunction:: get_ext
.. autofunction:: check_compressed_format
.. autofunction:: format_from_filename_extension
.. autofunction:: guess_format

Modules and packages
--------------------

.. autofunction:: is_installed

Streams
-------

Many of the readers are not restricted to just reading files. They can
also use gzip-compressed or bzip2-compressed files (through the
internal use of :func:`openany`). It is also possible to provide more
general streams as inputs, such as a :class:`io.StringIO`
instances (essentially, a memory buffer) by wrapping these instances
into a :class:`NamedStream`. This :class:`NamedStream` can then be
used in place of an ordinary file name (typically, with a
class:`~MDAnalysis.core.universe.Universe` but it is also possible to
*write* to such a stream using :func:`MDAnalysis.Writer`).

.. rubric: Examples

In the following example, we use a PDB stored as a string ``pdb_s``::

   import MDAnalysis
   from MDAnalysis.lib.util import NamedStream
   from io import StringIO

   pdb_s = "TITLE     Lonely Ion\\nATOM      1  NA  NA+     1      81.260  64.982  10.926  1.00  0.00\\n"
   u = MDAnalysis.Universe(NamedStream(StringIO(pdb_s), "ion.pdb"))
   print(u)
   #  <Universe with 1 atoms>
   print(u.atoms.positions)
   # [[ 81.26000214  64.98200226  10.92599964]]

It is important to provide a proper pseudo file name with the correct extension
(".pdb") to :class:`NamedStream` because the file type recognition uses the
extension of the file name to determine the file format or alternatively
provide the ``format="pdb"`` keyword argument to the
:class:`~MDAnalysis.core.universe.Universe`.

The use of streams becomes more interesting when MDAnalysis is used as glue
between different analysis packages and when one can arrange things so that
intermediate frames (typically in the PDB format) are not written to disk but
remain in memory via e.g. :class:`io.StringIO` buffers.


.. The following does *not* work because most readers need to
.. reopen files, which is not possible with http streams. Might
.. need to implement a buffer.
..
.. Read a test LAMMPS data file from the MDAnalysis repository::
..
..   import MDAnalysis
..   from MDAnalysis.lib.util import NamedStream
..   import urllib2
..   URI = "https://mdanalysis.googlecode.com/git-history/develop/testsuite/MDAnalysisTests/data/mini.data"
..   urldata = NamedStream(urllib2.urlopen(URI), "mini.data")
..   u = MDAnalysis.Universe(urldata)

.. Note::  A remote connection created by :func:`urllib2.urlopen` is not seekable
           and therefore will often not work as an input. But try it...

.. autoclass:: NamedStream
   :members:

.. autofunction:: isstream

Containers and lists
--------------------

.. autofunction:: iterable
.. autofunction:: asiterable
.. autofunction:: hasmethod
.. autoclass:: Namespace

Arrays
------

.. autofunction:: unique_int_1d(values)
.. autofunction:: unique_rows
.. autofunction:: blocks_of
.. autofunction:: group_same_or_consecutive_integers

File parsing
------------

.. autoclass:: FORTRANReader
   :members:
.. autodata:: FORTRAN_format_regex

Data manipulation and handling
------------------------------

.. autofunction:: fixedwidth_bins
.. autofunction:: get_weights
.. autofunction:: ltruncate_int
.. autofunction:: flatten_dict

Strings
-------

.. autofunction:: convert_aa_code
.. autofunction:: parse_residue
.. autofunction:: conv_float
.. autofunction:: atoi

Class decorators
----------------

.. autofunction:: cached
.. autofunction:: store_init_arguments

Function decorators
-------------------

.. autofunction:: static_variables
.. autofunction:: warn_if_not_unique
.. autofunction:: check_coords
.. autofunction:: check_atomgroup_not_empty

Code management
---------------

.. autofunction:: deprecate
.. autoclass:: _Deprecate
.. autofunction:: dedent_docstring

Data format checks
------------------

.. autofunction:: check_box

.. Rubric:: Footnotes

.. [#NamedStreamClose] The reason why :meth:`NamedStream.close` does
   not close a stream by default (but just rewinds it to the
   beginning) is so that one can use the class :class:`NamedStream` as
   a drop-in replacement for file names, which are often re-opened
   (e.g. when the same file is used as a topology and coordinate file
   or when repeatedly iterating through a trajectory in some
   implementations). The ``close=True`` keyword can be supplied in
   order to make :meth:`NamedStream.close` actually close the
   underlying stream and ``NamedStream.close(force=True)`` will also
   close it.
    Nzrestructuredtext en)contextmanagerwraps)assert_equal   )DuplicateWarningStreamWarning   )bz2_pickle_opengzip_pickle_openpickle_open)unique_int_1dz\MDAnalysis not installed properly. This can happen if your C extensions have not been built.c                     | d d         | dd          k    }	 |d         o|                                 dk    S # t          $ r Y dS w xY w)Nr
   r   T)argmin
IndexError)arraymasks     ]/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/lib/util.pyint_array_is_sortedr      s_    ":qrr"DAw-4;;==A--   tts   8 
AAc                 h    t          j        | d          \  }}| t          j        |                   S )NTreturn_index)npuniquesort)r   valuesindicess      r   unique_int_1d_unsortedr      s/    iD999OFG!!""    Fc                    ||                                 }|                    t          j        j                  st          j        j        |z   }t          j                            |           \  }}|rt          |          dk    r||z   }t          |           r|| _        n|} t          |           r| nt          |           S )a^  Return a new name that has suffix attached; replaces other extensions.

    Parameters
    ----------
    name : str or NamedStream
        filename; extension is replaced unless ``keep=True``;
        `name` can also be a :class:`NamedStream` (and its
        :attr:`NamedStream.name` will be changed accordingly)
    ext : None or str
        extension to use in the new filename
    keep : bool
        - ``False``: replace existing extension with `ext`;
        - ``True``: keep old extension if one existed


    .. versionchanged:: 0.9.0
       Also permits :class:`NamedStream` to pass through.
    Nr   )
lower
startswithospathextsepsplitextlenisstreamnamestr)r*   extkeeprootorigextnewnames         r   filenamer1      s    & iikk~~bgn-- 	''.3&C((..g 	s7||q((SjG~~ #		D>>044s4yy0r    rtTc              #      K   t          | ||          }	 |V  |                                 dS # |                                 w xY w)a  Context manager for :func:`anyopen`.

    Open the `datasource` and close it when the context of the :keyword:`with`
    statement exits.

    `datasource` can be a filename or a stream (see :func:`isstream`). A stream
    is reset to its start if possible (via :meth:`~io.IOBase.seek` or
    :meth:`~cString.StringIO.reset`).

    The advantage of this function is that very different input sources
    ("streams") can be used for a "file", ranging from files on disk (including
    compressed files) to open file objects to sockets and strings---as long as
    they have a file-like interface.

    Parameters
    ----------
    datasource : a file or a stream
    mode : {'r', 'w'} (optional)
        open in r(ead) or w(rite) mode
    reset : bool (optional)
        try to read (`mode` 'r') the stream from the start [``True``]

    Examples
    --------
    Open a gzipped file and process it line by line::

        with openany("input.pdb.gz") as pdb:
            for line in pdb:
                if line.startswith('ATOM'):
                    print(line)

    Open a URL and read it::

       import urllib2
       with openany(urllib2.urlopen("https://www.mdanalysis.org/")) as html:
           print(html.read())


    See Also
    --------
    :func:`anyopen`
    )moderesetN)anyopenclose)
datasourcer4   r5   streams       r   openanyr:     sN      X Zd%888Fs	   0 Ac           	         t           t          t          d}t          j        t
          j        t          d}|                    d          r2t          |           r| }	 t          |j	                  }n# t          $ r d}Y nw xY w|r	 |                                 n# t          t          f$ r` 	 |                    d           nE# t          t          f$ r1 t          j        d                    |          t"                     Y nw xY wY nw xY wnd}| }dD ] }||         }t%          | ||          }| n!|?t          t&          j         d	j        di t+                      t-          |                    n+|                    d
          s|                    d          rt          |           r*| }	 t          |j	                  }n# t          $ r d}Y nw xY wd}| }t.          j                            |          \  }	}|                    d          r
|dd         }|dvrd}||         } || |          }|?t          t&          j         d	j        di t+                      t-          |                    n&t5           dj        di t+                                	 ||_	        n# t          t6          f$ r Y nw xY w|S )a   Open datasource (gzipped, bzipped, uncompressed) and return a stream.

    `datasource` can be a filename or a stream (see :func:`isstream`). By
    default, a stream is reset to its start if possible (via
    :meth:`~io.IOBase.seek` or :meth:`~cString.StringIO.reset`).

    If possible, the attribute ``stream.name`` is set to the filename or
    "<stream>" if no filename could be associated with the *datasource*.

    Parameters
    ----------
    datasource
        a file (from :class:`file` or :func:`open`) or a stream (e.g. from
        :func:`urllib2.urlopen` or :class:`io.StringIO`)
    mode: {'r', 'w', 'a'} (optional)
        Open in r(ead), w(rite) or a(ppend) mode. This string is directly
        passed to the file opening handler (either Python's openfe, bz2, or
        gzip). More complex modes are supported if the file opening handler
        supports it.
    reset: bool (optional)
        try to read (`mode` 'r') the stream from the start

    Returns
    -------
    file-like object

    See Also
    --------
    :func:`openany`
      to be used with the :keyword:`with` statement.


    .. versionchanged:: 0.9.0
       Only returns the ``stream`` and tries to set ``stream.name = filename`` instead of the previous
       behavior to return a tuple ``(stream, filename)``.

    .. versionchanged:: 2.0.0
       New read handlers support pickle functionality
       if `datasource` is a filename.
       They return a custom picklable file stream in
       :class:`MDAnalysis.lib.picklable_file_io`.

    )bz2gz rz<stream>r   z2Stream {0}: not guaranteed to be at the beginning.categoryNr4   z,Cannot open file or stream in mode={mode!r}.wa.r
   r<   r=   r>   z:Sorry, mode={mode!r} is not implemented for {datasource!r} )r   r   r   r<   opengzipr#   r)   r+   r*   AttributeErrorr5   IOErrorseekwarningswarnformatr	   _get_streamerrnoEIOvarsreprr$   r%   r'   NotImplementedError	TypeError)
r8   r4   r5   read_handlerswrite_handlersr9   r1   r,   openfuncr*   s
             r   r6   r6   M  s   Z  M
 !XTYDAANs C
J !	F&v{++! & & &%& 
LLNNNN&0   A*G4    %vh//%2      F!H(  (-$ZEEE%E &~IIBI  &&  NN    
		  
" "  
 J 	F&v{++! & & &%& F!H((22ID#~~c"" !""g-''%c*HXjt444F~IIBI  &&  NN    "OHO  && 
 
 	

I&   Msl   A2 2B BB D/CD?DDDDD9G GG)J1 1KKr?   c                    	  || |          }nR# t           t          f$ r>}t          j        |j                 dv rt	          j                    d         |Y d}~dS d}~ww xY w|                    d          rr	 |                                 |                                  || |          }n;# t           $ r |                                 d}Y n |                                  xY w|S )zTReturn open stream if *filename* can be opened with *openfunction* or else ``None``.rB   )ENOENTEACCESr
   Nr?   )	rK   OSErrorrQ   	errorcodesysexc_infor#   readliner7   )r1   openfunctionr4   r9   errs        r   rP   rP     s   hT222W    ?39%)===,..#,ttttt s 7
	7OO LLNNN!\(666FF  	 	 	LLNNNFFF	LLNNN Ms&    A3AA8B. . C&C&c                     t           j                            |           \  }}d}	 t           j                            |          \  }}||z   }|sn+t           j                            ||          |fS )a  Split extension in path *p* at the left-most separator.

    Extensions are taken to be separated from the filename with the
    separator :data:`os.extsep` (as used by :func:`os.path.splitext`).

    Arguments
    ---------
    p : str
       path

    Returns
    -------
    (root, extension) : tuple
          where ``root`` is the full path and filename with all
          extensions removed whereas ``extension`` is the string of
          all extensions.

    Example
    -------

    >>> from MDAnalysis.lib.util import greedy_splitext
    >>> greedy_splitext("/home/joe/protein.pdb.bz2")
    ('/home/joe/protein', '.pdb.bz2')

    r>   )r$   r%   splitr'   join)pr%   r.   	extensionr,   s        r   greedy_splitextri     st    4 q!!JD$IG$$T**	c)O	 		
 7<<d##Y..r    c                 \    t          | |          ot          t          | |                    S )z8Return ``True`` if object *obj* contains the method *m*.)hasattrcallablegetattr)objms     r   	hasmethodrp   
  s%    3??8xQ888r    c                      d}d}|D ]}t           |          s dS  fd|D             }t          j        |          S )a  Detect if `obj` is a stream.

    We consider anything a stream that has the methods

    - ``close()``

    and either set of the following

    - ``read()``, ``readline()``, ``readlines()``
    - ``write()``, ``writeline()``, ``writelines()``

    Parameters
    ----------
    obj : stream or str

    Returns
    -------
    bool
        ``True`` if `obj` is a stream, ``False`` otherwise

    See Also
    --------
    :mod:`io`


    .. versionadded:: 0.9.0
    r7   ))readra   	readlines)write	writeline
writelinesFc                 P    g | ]"}t          j        fd |D                       #S )c                 0    g | ]}t          |          S rG   )rp   ).0ro   rn   s     r   
<listcomp>z'isstream.<locals>.<listcomp>.<listcomp>7  s#    888a	#q!!888r    )r   all)rz   alternativesrn   s     r   r{   zisstream.<locals>.<listcomp>6  sH        	8888<88899  r    )rp   r   any)rn   signature_methodsalternative_methodsro   alternative_resultss   `    r   r)   r)     s~    8 #   a   	55	   /   6%&&&r    c                    d}t          j        |t                     d }t          j                            |           \  }}|rt          |           } ||          r|S nat          j        d                             t          j                  D ]1}t          j        	                    ||           } ||          r|c S 2dS )a  Determine full path of executable `program` on :envvar:`PATH`.

    (Jay at http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python)

    Parameters
    ----------
    programe : str
       name of the executable

    Returns
    -------
    path : str or None
       absolute path to the executable if it can be found, else ``None``


    .. deprecated:: 2.7.0
       This method is deprecated and will be removed in version 3.0.0.
       Please use shutil.which instead.
    zThis method is deprecated as of MDAnalysis version 2.7.0 and will be removed in version 3.0.0. Please use shutil.which instead.c                 ~    t           j                            |           ot          j        | t           j                  S N)r$   r%   isfileaccessX_OK)fpaths    r   is_exezwhich.<locals>.is_exeY  s)    w~~e$$B5"')B)BBr    PATHN)
rM   rN   DeprecationWarningr$   r%   re   realpathenvironpathseprf   )programwmsgr   r   fnamereal_programr%   exe_files           r   whichr   =  s    ,	 	
 M$*+++C C C 7==))LE5  ((6, 	 	  Jv&,,RZ88 	  	 Dw||D'22Hvh   4r    c                        e Zd ZdZd#dZd Zd Zd Zd Zd	 Z	d
 Z
d Zd$ fd	Zd Ze fd            Zej        f fd	Z fdZ fdZ fdZ fdZ fdZ fdZd Z fdZd Zd Zd Zd Zd Zd Z d Z!d Z"e"Z#d  Z$d! Z%d" Z& xZ'S )%NamedStreama\  Stream that also provides a (fake) name.

    By wrapping a stream `stream` in this class, it can be passed to
    code that uses inspection of the filename to make decisions. For
    instance. :func:`os.path.split` will work correctly on a
    :class:`NamedStream`.

    The class can be used as a context manager.

    :class:`NamedStream` is derived from :class:`io.IOBase` (to indicate that
    it is a stream). Many operations that normally expect a string will also
    work with a :class:`NamedStream`; for instance, most of the functions in
    :mod:`os.path` will work with the exception of :func:`os.path.expandvars`
    and :func:`os.path.expanduser`, which will return the :class:`NamedStream`
    itself instead of a string if no substitutions were made.

    Example
    -------
    Wrap a :class:`io.StringIO` instance to write to::

      from io import StringIO
      import os.path
      stream = StringIO()
      f = NamedStream(stream, "output.pdb")
      print(os.path.splitext(f))

    Wrap a :class:`file` instance to read from::

      stream = open("input.pdb")
      f = NamedStream(stream, stream.name)

    Use as a context manager (closes stream automatically when the
    :keyword:`with` block is left)::

      with NamedStream(open("input.pdb"), "input.pdb") as f:
         # use f
         print f.closed  # --> False
         # ...
      print f.closed     # --> True

    Note
    ----
    This class uses its own :meth:`__getitem__` method so if `stream`
    implements :meth:`stream.__getitem__` then that will be masked and
    this class should not be used.

    Warning
    -------
    By default, :meth:`NamedStream.close` will **not close the
    stream** but instead :meth:`~NamedStream.reset` it to the
    beginning. [#NamedStreamClose]_ Provide the ``force=True`` keyword
    to :meth:`NamedStream.close` to always close the stream.

    TFc                     t          |t                    r!t          j        dt                     |j        }|| _        || _        || _        |r|                                  dS dS )aZ  Initialize the :class:`NamedStream` from a `stream` and give it a `name`.

        The constructor attempts to rewind the stream to the beginning unless
        the keyword `reset` is set to ``False``. If rewinding fails, a
        :class:`MDAnalysis.StreamWarning` is issued.

        Parameters
        ----------
        stream : stream
            an open stream (e.g. :class:`file` or :class:`io.StringIO`)
        filename : str
            the filename that should be associated with the stream
        reset : bool (optional)
            start the stream from the beginning (either :meth:`reset` or :meth:`seek`)
            when the class instance is constructed
        close : bool (optional)
            close the stream when a :keyword:`with` block exits or when
            :meth:`close` is called; note that the default is **not to close
            the stream**

        Notes
        -----
        By default, this stream will *not* be closed by :keyword:`with` and
        :meth:`close` (see there) unless the `close` keyword is set to
        ``True``.


        .. versionadded:: 0.9.0
        z*Constructed NamedStream from a NamedStreamN)	
isinstancer   rM   rN   RuntimeWarningr9   r*   close_streamr5   )selfr9   r1   r5   r7   s        r   __init__zNamedStream.__init__  sr    B fk** 	#M<n   ]F	! 	JJLLLLL	 	r    c                 8   	 | j                                          dS # t          t          f$ rk 	 | j                             d           Y dS # t          t          f$ r8 t          j        d                    | j                  t                     Y Y dS w xY ww xY w)z#Move to the beginning of the streamr   z7NamedStream {0}: not guaranteed to be at the beginning.r@   N)
r9   r5   rJ   rK   rL   rM   rN   rO   r*   r	   r   s    r   r5   zNamedStream.reset  s    
	K( 	 	 	  ######"G,   vdi((*       	s'    BAABBBBc                 x    	 t          | j        |          S # t          $ r t          | j        |          cY S w xY wr   )rm   r9   rJ   r*   r   xs     r   __getattr__zNamedStream.__getattr__  sL    	)4;*** 	) 	) 	)49a(((((	)s    99c                 *    t          | j                  S r   )iterr9   r   s    r   __iter__zNamedStream.__iter__  s    DK   r    c                 4    | j                                         S r   )r9   __next__r   s    r   r   zNamedStream.__next__  s    {##%%%r    c                     | S r   rG   r   s    r   	__enter__zNamedStream.__enter__  s    r    c                 .    |                                   d S r   rr   )r   argss     r   __exit__zNamedStream.__exit__  s     	

r    c                     | j         S r   r*   r   s    r   
__fspath__zNamedStream.__fspath__  
    yr    c                    | j         rdS | j        s|rQ	 | j                                        S # t          $ r* t          t          |                                           cY S w xY w|                                  |                                  dS )a  Reset or close the stream.

        If :attr:`NamedStream.close_stream` is set to ``False`` (the default)
        then this method will *not close the stream* and only :meth:`reset` it.

        If the *force* = ``True`` keyword is provided, the stream will be
        closed.

        .. Note:: This ``close()`` method is non-standard. ``del NamedStream``
                  always closes the underlying stream.

        N)	closedr   r9   r7   rJ   superr   flushr5   )r   force	__class__s     r   r7   zNamedStream.close  s     ; 	F 	 	8{((***! 8 8 8[$//55777778 JJLLLJJLLLLLs   . 1A"!A"c                 2    |                      d           dS )zAlways closes the stream.T)r   Nrr   r   s    r   __del__zNamedStream.__del__  s    


r    c                 r    	 | j         j        S # t          $ r t          t          |           j        cY S w xY w)z``True`` if stream is closed.)r9   r   rJ   r   r   r   r   s    r   r   zNamedStream.closed  sG    	3;%% 	3 	3 	3d++2222	3s    $66c                     	 | j                             ||          S # t          $ r, t          t          |                               ||          cY S w xY w)u  Change the stream position to the given byte `offset` .

        Parameters
        ----------
        offset : int
             `offset` is interpreted relative to the position
             indicated by `whence`.
        whence : {0, 1, 2} (optional)
             Values for `whence` are:

               - :data:`io.SEEK_SET` or 0 – start of the stream (the default); `offset`
                 should be zero or positive
               - :data:`io.SEEK_CUR` or 1 – current stream position; `offset` may be
                 negative
               - :data:`io.SEEK_END` or 2 – end of the stream; `offset` is usually
                 negative

        Returns
        -------
        int
            the new absolute position in bytes.

        )r9   rL   rJ   r   r   )r   offsetwhencer   s      r   rL   zNamedStream.seek"  sf    0	A;##FF333 	A 	A 	Ad++00@@@@@	As    3AAc                     	 | j                                         S # t          $ r* t          t          |                                           cY S w xY w)z#Return the current stream position.)r9   tellrJ   r   r   r   s    r   r   zNamedStream.tell?  sY    	3;##%%% 	3 	3 	3d++0022222	3    1AAc                 ~    	  | j         j        | S # t          $ r   t          t          |           j        | cY S w xY w)a	  Truncate the stream's size to `size`.

        Parameters
        ----------
        size : int (optional)
             The `size` defaults to the current position (if no `size` argument
             is supplied). The current file position is not changed.

        )r9   truncaterJ   r   r   )r   sizer   s     r   r   zNamedStream.truncateF  sU    	<'4;'.. 	< 	< 	<45d++4d;;;;	<s    '<<c                     	 | j                                         S # t          $ r* t          t          |                                           cY S w xY w)zReturn ``True`` if the stream supports random access.

        If ``False``, :meth:`seek`, :meth:`tell` and :meth:`truncate` will
        raise :exc:`IOError`.

        )r9   seekablerJ   r   r   r   s    r   r   zNamedStream.seekableU  sY    	7;''))) 	7 	7 	7d++4466666	7r   c                     	 | j                                         S # t          $ r* t          t          |                                           cY S w xY w)zwReturn ``True`` if the stream can be read from.

        If ``False``, :meth:`read` will raise :exc:`IOError`.
        )r9   readablerJ   r   r   r   s    r   r   zNamedStream.readablea  Y    
	7;''))) 	7 	7 	7d++4466666	7r   c                     	 | j                                         S # t          $ r* t          t          |                                           cY S w xY w)zyReturn ``True`` if the stream can be written to.

        If ``False``, :meth:`write` will raise :exc:`IOError`.
        )r9   writablerJ   r   r   r   s    r   r   zNamedStream.writablek  r   r   c                     	 | j                                         S # t          $ r* t          t          |                                           cY S w xY w)zFlush the write buffers of the stream if applicable.

        This does nothing for read-only and non-blocking streams. For file
        objects one also needs to call :func:`os.fsync` to write contents to
        disk.
        )r9   r   rJ   r   r   r   s    r   r   zNamedStream.flushu  sY    	4;$$&&& 	4 	4 	4d++1133333	4r   c                 v    	 | j                                         S # t          $ r d}t          |          dw xY w)zReturn the underlying file descriptor (an integer) of the stream if it exists.

        An :exc:`IOError` is raised if the IO object does not use a file descriptor.
        z0This NamedStream does not use a file descriptor.N)r9   filenorJ   rK   )r   errmsgs     r   r   zNamedStream.fileno  sI    
	,;%%''' 	, 	, 	,GF&//t+	,s    8c                     	 | j                                         S # t          $ r* t          t          |                                           cY S w xY wr   )r9   ra   rJ   r   r   r   s    r   ra   zNamedStream.readline  sY    	7;''))) 	7 	7 	7d++4466666	7r   c                     | j         |         S r   r   r   s     r   __getitem__zNamedStream.__getitem__  s    y|r    c                     | j         |k    S r   r   r   s     r   __eq__zNamedStream.__eq__  s    yA~r    c                     | |k     S r   rG   r   s     r   __ne__zNamedStream.__ne__  s    19}r    c                     | j         |k     S r   r   r   s     r   __lt__zNamedStream.__lt__  s    y1}r    c                 *    t          | j                  S r   )r(   r*   r   s    r   __len__zNamedStream.__len__  s    49~~r    c                     | j         |z   S r   r   r   s     r   __add__zNamedStream.__add__      y1}r    c                     || j         z   S r   r   r   s     r   __radd__zNamedStream.__radd__  s    49}r    c                     | j         |z  S r   r   r   s     r   __mul__zNamedStream.__mul__  r   r    c                 6    | j                             |          S r   )r*   rO   )r   format_specs     r   
__format__zNamedStream.__format__  s    y,,,r    c                     | j         S r   r   r   s    r   __str__zNamedStream.__str__  r   r    c                 B    d                     | j        | j                  S )Nz<NamedStream({0}, {1})>)rO   r9   r*   r   s    r   __repr__zNamedStream.__repr__  s    (//TYGGGr    )TFF)(__name__
__module____qualname____doc__r   r5   r   r   r   r   r   r   r7   r   propertyr   r$   SEEK_SETrL   r   r   r   r   r   r   r   ra   r   r   r   r   r   r   r   r   __rmul__r   r   r   __classcell__)r   s   @r   r   r   i  s       5 5n* * * *X   ) ) )! ! !& & &      
     2   3 3 3 3 X3 #%+ A A A A A A:3 3 3 3 3< < < < <
7 
7 
7 
7 
77 7 7 7 77 7 7 7 7
4 
4 
4 
4 
4
, 
, 
,7 7 7 7 7                 H- - -  H H H H H H Hr    r   c            	          d| v rdS t           j                            t           j                            t           j                            t          j        j        |                                S )zJoin all args and return the real path, rooted at ``/``.

    Expands '~', '~user', and environment variables such as :envvar:`$HOME`.

    Returns ``None`` if any of the args is ``None``.
    N)r$   r%   r   
expanduser
expandvarsrf   )r   s    r   r   r     sX     t||t7
27--bglD.ABBCC  r    c                     t           j                            |           \  }}|                    t           j                  r
|dd         }||                                fS )zReturn the lower-cased extension of `filename` without a leading dot.

    Parameters
    ----------
    filename : str

    Returns
    -------
    root : str
    ext : str
    r
   N)r$   r%   r'   r#   r&   r"   )r1   r.   r,   s      r   get_extr     sS       **ID#
~~bi   !""gr    c                     |                                 dv r;	 t          |           \  } }n'# t          $ r d|  d| d}t          |          dw xY w|                                S )af  Check if this is a supported gzipped/bzip2ed file format and return UPPERCASE format.

    Parameters
    ----------
    root : str
       path of a file, without extension `ext`
    ext : str
       extension (currently only "bz2" and "gz" are recognized as compressed formats)

    Returns
    -------
    format : str
       upper case format extension *if* the compression can be handled by
       :func:`openany`

    See Also
    --------
    openany : function that is used to open and decompress formats on the fly; only
              compression formats implemented in :func:`openany` are recognized

    rF   z(Cannot determine coordinate format for 'rE   'N)r"   r   	ExceptionrV   upper)r.   r,   r   s      r   check_compressed_formatr     s    0 yy{{m##	.ID## 	. 	. 	.MMMsMMMFF##-	. 99;;s	   + $Ac                     	 t          |           \  }}n$# t          $ r d|  d}t          |          dw xY wt          ||          }|S )zGuess file format from the file extension.

    Parameters
    ----------
    filename : str

    Returns
    -------
    format : str

    Raises
    ------
    TypeError
        if the file format cannot be determined
    z'Cannot determine file format for file 'zT'.
           You can set the format explicitly with 'Universe(..., format=FORMAT)'.N)r   r   rV   r   )r1   r.   r,   r   rO   s        r   format_from_filename_extensionr     sx     *H%%	cc * * */h / / / 	
 T)* %T3//FMs    !6c                     t          |           r9	 t          | j                  }nC# t          $ r d|  }t	          |          dw xY wt          |           st          |           nd}|                                S )aD  Return the format of `filename`

    The current heuristic simply looks at the filename extension and can work
    around compressed format extensions.

    Parameters
    ----------
    filename : str or stream
        path to the file or a stream, in which case ``filename.name`` is looked
        at for a hint to the format

    Returns
    -------
    format : str
        format specifier (upper case)

    Raises
    ------
    ValueError
        if the heuristics are insufficient to guess a supported format


    .. versionadded:: 0.11.0
       Moved into lib.util

    z>guess_format requires an explicit format specifier for stream NCHAIN)r)   r   r*   rJ   
ValueErroriterabler   )r1   rO   r   s      r   guess_formatr    s    6  
	/3HMBBFF 	/ 	/ 	/)&) )  V$$$.	/ H%%*8444 	 <<>>s	   &  Ac                     t          | t          t          f          rdS t          | d          rdS 	 t	          |            n# t
          t          f$ r Y dS w xY wdS )zdReturns ``True`` if `obj` can be iterated over and is *not* a  string
    nor a :class:`NamedStream`FnextT)r   r+   r   rk   r(   rV   rJ   rn   s    r   r   r   N  su     #[)** usF tC~&   uu4s   A AAc                 *    t          |           s| g} | S )zReturns `obj` so that it can be iterated over.

    A string is *not* detected as and iterable and is wrapped into a :class:`list`
    with a single element.

    See Also
    --------
    iterable

    )r   r  s    r   
asiterabler  ]  s     C== eJr    zU(?P<repeat>\d+?)(?P<format>[IFEAX])(?P<numfmt>(?P<length>\d+)(\.(?P<decimals>\d+))?)?c                 D    t          |                                           S )z;Convert `s` to a string and return it white-space stripped.)r+   stripss    r   r  r  x  s    q66<<>>r    c                   8    e Zd ZdZeeeedZd Zd Z	d Z
d ZdS )FixedcolumnEntryzRepresent an entry at specific fixed columns.

    Reads from line[start:stop] and converts according to
    typespecifier.
    )IFEAc                 T    || _         || _        || _        | j        |         | _        dS )a}  
        Parameters
        ----------
        start : int
            first column
        stop : int
            last column + 1
        typespecifier : str
            'I': int, 'F': float, 'E': float, 'A': stripped string

        The start/stop arguments follow standard Python convention in that
        they are 0-based and that the *stop* argument is not included.
        N)startstoptypespecifier
convertors	convertor)r   r  r  r  s       r   r   zFixedcolumnEntry.__init__  s,     
	*7r    c                     	 |                      || j        | j                           S # t          $ r* |  d|| j        | j                  }t          |          dw xY w)z;Read the entry from `line` and convert to appropriate type.z: Failed to read&convert N)r  r  r  r   )r   liner   s      r   rs   zFixedcolumnEntry.read  s    	/>>$tzDI'=">??? 	/ 	/ 	/ 0 0
49,-0 0  V$$$.	/s	   &) 4Ac                      | j         | j        z
  S )z-Length of the field in columns (stop - start))r  r  r   s    r   r   zFixedcolumnEntry.__len__  s    y4:%%r    c                 N    d                     | j        | j        | j                  S )Nz#FixedcolumnEntry({0:d},{1:d},{2!r}))rO   r  r  r  r   s    r   r   zFixedcolumnEntry.__repr__  s(    4;;J	4#5
 
 	
r    N)r   r   r   r   intfloatr  r  r   rs   r   r   rG   r    r   r  r  }  sm          U??J8 8 8&	/ 	/ 	/& & &
 
 
 
 
r    r  c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	FORTRANReadera8  FORTRANReader provides a method to parse FORTRAN formatted lines in a file.

    The contents of lines in a file can be parsed according to FORTRAN format
    edit descriptors (see `Fortran Formats`_ for the syntax).

    Only simple one-character specifiers supported here: *I F E A X* (see
    :data:`FORTRAN_format_regex`).

    Strings are stripped of leading and trailing white space.

    .. _`Fortran Formats`: http://www.webcitation.org/5xbaWMV2x
    .. _`Fortran Formats (URL)`:
       http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

    c           
      V    |                     d           _         fd j        D             }d}g  _        |D ]n}|d         dk    rUt          |d                   D ]>}||d         z   } j                            t          |||d                              |}?c||d         z  }od	S )
ac  Set up the reader with the FORTRAN format string.

        The string `fmt` should look like '2I10,2X,A8,2X,A8,3F20.10,2X,A8,2X,A8,F20.10'.

        Parameters
        ----------
        fmt : str
           FORTRAN format edit descriptor for a line as described in `Fortran
           Formats`_

        Example
        -------
        Parsing of a standard CRD file::

           atomformat = FORTRANReader('2I10,2X,A8,2X,A8,3F20.10,2X,A8,2X,A8,F20.10')
           for line in open('coordinates.crd'):
               serial,TotRes,resName,name,x,y,z,chainID,resSeq,tempFactor = atomformat.read(line)

        ,c                 :    g | ]}                     |          S rG   )parse_FORTRAN_format)rz   
descriptorr   s     r   r{   z*FORTRANReader.__init__.<locals>.<listcomp>  s4     
 
 
6@D%%j11
 
 
r    r   rO   XrepeatlengthtotallengthN)re   fmtentriesrangeappendr  )r   r(  descriptorsr  dr   r  s   `      r   r   zFORTRANReader.__init__  s    ( 99S>>
 
 
 
DHH
 
 
  		* 		*A{c!!q{++ ! !A 1X;.DL''(akBB   !EE! =))		* 		*r    c                 *    fd| j         D             S )a
  Parse `line` according to the format string and return list of values.

        Values are converted to Python types according to the format specifier.

        Parameters
        ----------
        line : str

        Returns
        -------
        list
            list of entries with appropriate types

        Raises
        ------
        ValueError
            Any of the conversions cannot be made (e.g. space for an int)

        See Also
        --------
        :meth:`FORTRANReader.number_of_matches`
        c                 :    g | ]}|                               S rG   )rs   )rz   er  s     r   r{   z&FORTRANReader.read.<locals>.<listcomp>  s#    333t333r    )r)  )r   r  s    `r   rs   zFORTRANReader.read  s!    . 4333dl3333r    c                 t    d}| j         D ]-}	 |                    |           |dz  }# t          $ r Y *w xY w|S )zDReturn how many format entries could be populated with legal values.r   r
   )r)  rs   r   )r   r  matchesr0  s       r   number_of_matcheszFORTRANReader.number_of_matches  s^      	 	At1   s   (
55c                 4   t                               |                                          }|a	 t                               d|                                z             }|t          n'#  t          d                    |                    xY w|                                }|d         dk    rd|d<   |d         dk    rd|d	<   d
D ];}	 t          ||                   ||<   # t          $ r d||<   Y -t          $ r Y 8w xY w|d         |d	         z  |d<   |S )a0  Parse the descriptor.


        Parameters
        ----------
        edit_descriptor : str
            FORTRAN format edit descriptor

        Returns
        -------
        dict
            dict with totallength (in chars), repeat, length, format, decimals

        Raises
        ------
        ValueError
            The `edit_descriptor` is not recognized and cannot be parsed

        Note
        ----
        Specifiers: *L ES EN T TL TR / r S SP SS BN BZ* are *not* supported,
        and neither are the scientific notation *Ew.dEe* forms.

        N1z!unrecognized FORTRAN format {0!r}r%  r>   r
   rO   r$  r&  )r%  r&  decimalsr   r'  )_FORTRAN_format_patternmatchr   r   rO   	groupdictr  rV   )r   edit_descriptorro   r-  ks        r   r"  z"FORTRANReader.parse_FORTRAN_format	  sW   4 $))/*?*?*A*ABB9	+11///111  9$$  7>>OO   KKMMX;"AhKX;#AhK1 	 	A1Q4yy!   !   X;84-s#   8A) )$BC$$D5	D Dc                 *    t          | j                  S )zReturns number of entries.)r(   r)  r   s    r   r   zFORTRANReader.__len__>  s    4<   r    c                 \    | j         j        dz   d                    | j                  z   dz   S )N(r   ))r   r   rf   r(  r   s    r   r   zFORTRANReader.__repr__B  s*    ~&,sxx/A/AACGGr    N)
r   r   r   r   r   rs   r3  r"  r   r   rG   r    r   r  r    s          #* #* #*J4 4 42
 
 
3 3 3j! ! !H H H H Hr    r  c                    t          j        ||k               st          d          t          j        | t           j                  }t          j        |t           j                  }t          j        |t           j                  }||z
  }t          j        ||z                                t           j                  }d||z  |z
  z  }||||z
  ||z   dS )a  Return bins of width `delta` that cover `xmin`, `xmax` (or a larger range).

    The bin parameters are computed such that the bin size `delta` is
    guaranteed. In order to achieve this, the range `[xmin, xmax]` can be
    increased.

    Bins can be calculated for 1D data (then all parameters are simple floats)
    or nD data (then parameters are supplied as arrays, with each entry
    correpsonding to one dimension).

    Parameters
    ----------
    delta : float or array_like
        desired spacing of the bins
    xmin : float or array_like
        lower bound (left boundary of first bin)
    xmax : float or array_like
        upper bound (right boundary of last bin)

    Returns
    -------
    dict
        The dict contains 'Nbins', 'delta', 'min', and 'max'; these are either
        floats or arrays, depending on the input.

    Example
    -------
    Use with :func:`numpy.histogram`::

       B = fixedwidth_bins(delta, xmin, xmax)
       h, e = np.histogram(data, bins=B['Nbins'], range=(B['min'], B['max']))

    z/Boundaries are not sane: should be xmin < xmax.dtypeg      ?)Nbinsdeltaminmax)r   r|   r   asarrayfloat64ceilastypeint_)	rD  xminxmax_delta_xmin_xmax_lengthNdxs	            r   fixedwidth_binsrT  F  s    D 6$+ LJKKKZRZ000FJt2:...EJt2:...EemG
& !!((11A	F
W$	%B
52:NNNr    c                 `   t          |          s/|dk    r)	 | j        }n # t          $ r d}t          |          dw xY wt          |          rt	          t          j        |t                    j                  dk    r@t          d
                    t          j        |t                    j                            t	          |          t	          |           k    r=t          d
                    t	          |          t	          |                               n|t          d          |S )	a  Check that a `weights` argument is compatible with `atoms`.

    Parameters
    ----------
    atoms : AtomGroup or array_like
        The atoms that the `weights` should be applied to. Typically this
        is a :class:`AtomGroup` but because only the length is compared,
        any sequence for which ``len(atoms)`` is defined is acceptable.
    weights : {"mass", None} or array_like
        All MDAnalysis functions or classes understand "mass" and will then
        use ``atoms.masses``. ``None`` indicates equal weights for all atoms.
        Using an ``array_like`` assigns a custom weight to each element of
        `atoms`.

    Returns
    -------
    weights : array_like or None
         If "mass" was selected, ``atoms.masses`` is returned, otherwise the
         value of `weights` (which can be ``None``).

    Raises
    ------
    TypeError
        If `weights` is not one of the allowed values or if "mass" is
        selected but ``atoms.masses`` is not available.
    ValueError
        If `weights` is not a 1D array with the same length as
        `atoms`, then the exception is raised.  :exc:`TypeError` is
        also raised if ``atoms.masses`` is not defined.

    massz3weights='mass' selected but atoms.masses is missingNrA  r
   z.weights must be a 1D array, not with shape {0}z>weights (length {0}) must be of same length as the atoms ({1})zPweights must be {'mass', None} or an iterable of the same size as the atomgroup.)r   massesrJ   rV   r(   r   rG  objectshaper   rO   )atomsweightsr   s      r   get_weightsr\  s  s@   @ G .F!2!2	.lGG 	. 	. 	.JFF##-	.  
rz'0006771<<fRZv>>>DEE   \\SZZ''""(&Ws5zz"B"B   (
 
	*
 
 	

 Ns    <ALAr  CYSCASPDGLUr  PHEr  GLYGHISHILEr  LYSKLEULMETMASNrR  PROPGLNQARGRSERSTHRTVWY)VALTRPTYRc                     i | ]\  }}||	S rG   rG   )rz   threeones      r   
<dictcomp>r    s+       5#C  r    HISAHISBHSEHSDHIDHIEHIS1HIS2ASPHASHGLUHGLHLYSHLYNARGNCYSHCYS1CYS2c                     t          |           dk    rt          }nt          }	 ||                                          S # t          $ r d|  d}t          |          dw xY w)a  Converts between 3-letter and 1-letter amino acid codes.

    Parameters
    ----------
    x : str
        1-letter or 3-letter amino acid code

    Returns
    -------
    str
        3-letter or 1-letter amino acid code

    Raises
    ------
    ValueError
        No conversion can be made; the amino acid code is not defined.

    Note
    ----
    Data are defined in :data:`amino_acid_codes` and :data:`inverse_aa_codes`.
    r
   zNo conversion for z7 found (1 letter -> 3 letter or 3/4 letter -> 1 letter)N)r(   amino_acid_codesinverse_aa_codesr   KeyErrorr   )r   r-  r   s      r   convert_aa_coder    s~    , 1vv{{+| + + +# # # # 	   d*+s	   > !Aat  
                 (?P<aa>([ACDEFGHIKLMNPQRSTVWY])   # 1-letter amino acid
                        |                          #   or
                        ([0-9A-Z][a-zA-Z][A-Z][A-Z]?)    # 3-letter or 4-letter residue name
                 )
                 \s*                               # white space allowed
                 (?P<resid>\d+)                    # resid
                 \s*
                 (:                                # separator ':'
                   \s*
                   (?P<atom>\w+)                   # atom name
                 )?                                # possibly one
            c                 r   t                               |           }|s&t           dj        di t	                                t          |                    d                    }|                    d          } t          |           dk    rt          |           }n| }|                    d          }|||fS )a  Process residue string.

    Parameters
    ----------
    residue: str
        The *residue* must contain a 1-letter or 3-letter or
        4-letter residue string, a number (the resid) and
        optionally an atom identifier, which must be separate
        from the residue with a colon (":"). White space is
        allowed in between.

    Returns
    -------
    tuple
        `(3-letter aa string, resid, atomname)`; known 1-letter
        aa codes are converted to 3-letter codes

    Examples
    --------
     - "LYS300:HZ1" --> ("LYS", 300, "HZ1")
     - "K300:HZ1" --> ("LYS", 300, "HZ1")
     - "K300" --> ("LYS", 300, None)
     - "4GB300:H6O" --> ("4GB", 300, "H6O")
     - "4GB300" --> ("4GB", 300, None)

    zPSelection {residue!r} is not valid (only 1/3/4 letter resnames, resid required).residaar
   atomrG   )	RESIDUEr8  r   rO   rS   r  groupr(   r  )residuero   r  resnameatomnames        r   parse_residuer    s    : 	gA 
e^e  && 
 
 	

   !!EggdmmG
7||q!'**wwvHUH%%r    c                 F    	 t          |           S # t          $ r | cY S w xY w)zConvert an object `s` to float if possible.

    Function to be passed into :func:`map` or a list comprehension. If
    the argument can be interpreted as a float it is converted,
    otherwise the original object is passed back.
    )r  r   r	  s    r   
conv_floatr  L  s6    Qxx   s      c                       e Zd ZdS )	_CacheKeyN)r   r   r   rG   r    r   r  r  \  s        Dr    r  c                       fd}|S )a_  Cache a property within a class.

    Requires the Class to have a cache dict :attr:`_cache` and, with
    `universe_validation`, a :attr:`universe` with a cache dict :attr:`_cache`.

    Example
    -------
    How to add a cache for a variable to a class by using the `@cached`
    decorator::

       class A(object):
           def__init__(self):
               self._cache = dict()

           @property
           @cached('keyname')
           def size(self):
               # This code gets run only if the lookup of keyname fails
               # After this code has been run once, the result is stored in
               # _cache with the key: 'keyname'
               return 10.0

           @property
           @cached('keyname', universe_validation=True)
           def othersize(self):
               # This code gets run only if the lookup
               # id(self) is not in the validation set under
               # self.universe._cache['_valid']['keyname']
               # After this code has been run once, id(self) is added to that
               # set. The validation set can be centrally invalidated at the
               # universe level (say, if a topology change invalidates specific
               # caches).
               return 20.0


    .. versionadded:: 0.9.0

    .. versionchanged::2.0.0
        Added the `universe_validation` keyword.
    c                 B     t                      fd            }|S )Nc                    	 r| j         j                            dt                                }|                    t	          j                              }	 | j        |vrt          n(# t          $ r t                      | _        t          w xY w| j                 S # t          $ r7  | g|R i |x| j        <   }r|
                    | j                   |cY S w xY w)N_valid)universe_cache
setdefaultdictweakrefWeakSet
_cache_keyr  rJ   r  add)	r   r   kwargsu_cachevalid_cachesretfunckeyuniverse_validations	         r   wrapperz.cached.<locals>.cached_lookup.<locals>.wrapper  s   & '"m2==hOOG $+#5#5c7?;L;L#M#ML	'?,>>"*N ?) ' ' '
 +4++&' {3''   )-d)DT)D)D)DV)D)DDC 3& 6 $$T_555


	s*   AB A* )B *%BB >C C r   )r  r  r  r  s   ` r   cached_lookupzcached.<locals>.cached_lookup  s>    	t	 	 	 	 	 	 
	0 r    rG   )r  r  r  s   `` r   cachedr  `  s+    T     8 r    c           
      v     j         d         s                                   j        d         }|rt          j                             t          j         fdt          |          D                                 d          \  }}|                     j                                      d|          |fS t          j                             t          j         fdt          |          D                                           }|                     j                                      d|          S )	aq  Return the unique rows of an array.

    Arguments
    ---------
    arr : numpy.ndarray
        Array of shape ``(n1, m)``.
    return_index : bool, optional
      If ``True``, returns indices of array that formed answer (see
      :func:`numpy.unique`)

    Returns
    -------
    unique_rows : numpy.ndarray
         Array of shape ``(n2, m)`` containing only the unique rows of `arr`.
    r_idx : numpy.ndarray (optional)
          Array containing the corresponding row indices (if `return_index`
          is ``True``).

    Examples
    --------
    Remove dupicate rows from an array:

    >>> import numpy as np
    >>> from MDAnalysis.lib.util import unique_rows
    >>> a = np.array([[0, 1], [1, 2], [1, 2], [0, 1], [2, 3]])
    >>> b = unique_rows(a)
    >>> b
    array([[0, 1],
           [1, 2],
           [2, 3]])

    See Also
    --------
    numpy.unique

    OWNDATAr
   c                 <    g | ]}t          |          j        fS rG   r+   rB  rz   iarrs     r   r{   zunique_rows.<locals>.<listcomp>  &    $K$K$KQc!ffci%8$K$K$Kr    rA  Tr   r   c                 <    g | ]}t          |          j        fS rG   r  r  s     r   r{   zunique_rows.<locals>.<listcomp>  r  r    )	flagscopyrY  r   r   viewrB  r*  reshape)r  r   ro   ur_idxs   `    r   unique_rowsr    s    V 9Y hhjj	!A 
09HH28$K$K$K$K%(($K$K$KLLHMM
 
 
5 vvci  ((Q//66IHH28$K$K$K$K%(($K$K$KLLHMM
 
 vvci  ((Q///r    c                    | j         d         st          d          | j        d         |z  }| j        d         |z  }||k    s#t          d                    ||                    |||f}|| j        d         z  || j        d         z  z   | j        d         | j        d         f}t
          j        j                            | ||          S )a  Extract a view of ``(n, m)`` blocks along the diagonal of the array `a`.

    Parameters
    ----------
    a : numpy.ndarray
        Input array, must be C contiguous and at least 2D.
    n : int
        Size of block in first dimension.
    m : int
        Size of block in second dimension.

    Returns
    -------
    view : numpy.ndarray
        A view of the original array with shape ``(nblocks, n, m)``, where
        ``nblocks`` is the number of times the miniblocks of shape ``(n, m)``
        fit in the original.

    Raises
    ------
    ValueError
        If the supplied `n` and `m` don't divide `a` into an integer number
        of blocks or if `a` is not C contiguous.

    Examples
    --------
    >>> import numpy as np
    >>> from MDAnalysis.lib.util import blocks_of
    >>> arr = np.arange(16).reshape(4, 4)
    >>> view = blocks_of(arr, 2, 2)
    >>> view[:] = 100
    >>> arr
    array([[100, 100,   2,   3],
           [100, 100,   6,   7],
           [  8,   9, 100, 100],
           [ 12,  13, 100, 100]])

    Notes
    -----
    `n`, `m` must divide `a` into an identical integer number of blocks. Please
    note that if the block size is larger than the input array, this number will
    be zero, resulting in an empty view!

    Uses strides and therefore requires that the array is C contiguous.

    Returns a view, so editing this modifies the original array.


    .. versionadded:: 0.12.0

    C_CONTIGUOUSz Input array is not C contiguous.r   r
   zHMust divide into same number of blocks in both directions.  Got {} by {})	r  r   rY  rO   stridesr   libstride_tricks
as_strided)rD   nro   nblocksnblocks2	new_shapenew_stridess          r   	blocks_ofr    s    n 7>" =;<<<gajAoGwqzQHhvgx((
 
 	
 !QI	AIaL1qy|++		!		!K 6**1iEEEr    c                     t          j        | t          j        t          j        |           dz
  dk              d         dz             S )a  Split an array of integers into a list of same or consecutive
    sequences.

    Parameters
    ----------
    arr: :class:`numpy.ndarray`

    Returns
    -------
    list of :class:`numpy.ndarray`

    Examples
    >>> import numpy as np
    >>> arr = np.array([ 2,  3,  4,  7,  8,  9, 10, 11, 15, 16])
    >>> group_same_or_consecutive_integers(arr)
    [array([2, 3, 4]), array([ 7,  8,  9, 10, 11]), array([15, 16])]
    r
   r   )r   re   whereediff1d)r  s    r   "group_same_or_consecutive_integersr  4  s<    $ 8C"*S//A"5"9::1=ABBBr    c                   *    e Zd ZdZd Zd Zd Zd ZdS )	Namespacez3Class to allow storing attributes in new namespace.c                     	 t                               | |          S # t          $ r d| d}t          |          d w xY wN"z " is not known in the namespace.)r  r   r  rJ   r   r  r   s      r   r   zNamespace.__getattr__L  sX    	3##D#... 	3 	3 	3>>>>F ((d2	3s    !>c                 >    t                               | ||           d S r   )r  __setitem__)r   r  values      r   __setattr__zNamespace.__setattr__T  s     sE*****r    c                     	 t                               | |           d S # t          $ r d| d}t          |          d w xY wr  )r  __delitem__r  rJ   r  s      r   __delattr__zNamespace.__delattr__W  s^    	3T3''''' 	3 	3 	3>>>>F ((d2	3s	    !A c                 L    	 t          | |           n# t          $ r Y dS w xY wdS )NFT)r   AssertionError)r   others     r   r   zNamespace.__eq__^  sA    	u%%%% 	 	 	55	ts    
!!N)r   r   r   r   r   r  r  r   rG   r    r   r  r  I  sV        ==3 3 3+ + +3 3 3    r    r  c                 L    t          t          |           | d                   S )a  Truncate an integer, retaining least significant digits

    Parameters
    ----------
    value : int
      value to truncate
    ndigits : int
      number of digits to keep

    Returns
    -------
    truncated : int
      only the `ndigits` least significant digits from `value`

    Examples
    --------
    >>> from MDAnalysis.lib.util import ltruncate_int
    >>> ltruncate_int(123, 2)
    23
    >>> ltruncate_int(1234, 5)
    1234
    N)r  r+   )r  ndigitss     r   ltruncate_intr  g  s#    . s5zz7())$%%%r    c                 d   g }|                                  D ]\  }}t          |          t          k    r||fz   }n||z   }t          |t                    r6|                    t          ||                                                      t|                    ||f           t	          |          S )a0  Flatten a nested dict `d` into a shallow dict with tuples as keys.

    Parameters
    ----------
    d : dict

    Returns
    -------
    dict

    Note
    -----
    Based on https://stackoverflow.com/a/6027615/
    by user https://stackoverflow.com/users/1897/imran

    .. versionadded:: 0.18.0
    )itemstypetupler   r  extendflatten_dictr+  )r-  
parent_keyr  r;  vnew_keys         r   r  r    s    & E		 ' '177e A4'GG 1nGa 	'LLa117799::::LL'1&&&&;;r    c                        fd}|S )a  Decorator equipping functions or methods with static variables.

    Static variables are declared and initialized by supplying keyword arguments
    and initial values to the decorator.

    Example
    -------

    >>> from MDAnalysis.lib.util import static_variables
    >>> @static_variables(msg='foo calls', calls=0)
    ... def foo():
    ...     foo.calls += 1
    ...     print("{}: {}".format(foo.msg, foo.calls))
    ...
    >>> foo()
    foo calls: 1
    >>> foo()
    foo calls: 2


    .. note:: Based on https://stackoverflow.com/a/279586
        by `Claudiu <https://stackoverflow.com/users/15055/claudiu>`_

    .. versionadded:: 0.19.0
    c                 @    D ]}t          | ||                    | S r   )setattr)r  kwargr  s     r   static_decoratorz*static_variables.<locals>.static_decorator  s0     	0 	0ED%////r    rG   )r  r  s   ` r   static_variablesr     s$    6    
 r    )warnedc                 <     t                      fd            }|S )aq  Decorator triggering a :class:`~MDAnalysis.exceptions.DuplicateWarning`
    if the underlying group is not unique.

    Assures that during execution of the decorated method only the first of
    potentially multiple warnings concerning the uniqueness of groups is shown.

    Raises
    ------
    :class:`~MDAnalysis.exceptions.DuplicateWarning`
        If the :class:`~MDAnalysis.core.groups.AtomGroup`,
        :class:`~MDAnalysis.core.groups.ResidueGroup`, or
        :class:`~MDAnalysis.core.groups.SegmentGroup` of which the decorated
        method is a member contains duplicates.


    .. versionadded:: 0.19.0
    c                    | j         st          j        r | g|R i |S d                    | j        j        j        f          }t          j                    j        j	        
                                }g }|D ]9\  }}	 || u r(|                    d                    |                     3#  Y 7xY w|s d                    | j        j                  }n>t          |          dk    r	|d         }n"d                    t          |                    }t          |           }	d                    |||	          }
t!          j        |
t$          d	           d
t          _        	  | g|R i |}dt          _        n# dt          _        w xY w|S )NrE   z'{}'z'unnamed {}'r
   r   z a.k.a. z9{}(): {} {} contains duplicates. Results might be biased!r   )messagerA   
stacklevelTF)isuniquewarn_if_not_uniquer  rf   r   r   inspectcurrentframef_backf_localsr  r+  rO   r(   sortedrT   rM   rN   r   )r  r   r  method_namecaller_localsgroup_namesr*   rn   
group_name
group_reprmsgresultgroupmethods               r   r  z#warn_if_not_unique.<locals>.wrapper  s    > 	7/6 	7;u6t666v666hh_%{';<
 
  ,..5>DDFF& 	 	ID#%<<&&v}}T':':;;; 	>'..u/GHHJJ""$QJJ#)<)<==J%[[
vk:z:: 	 	c,<KKKK$(!	. [888888F(-%%%----s    ,B--B1#E< <F
r   r  r  s   ` r   r  r    s9    ( ;# # # # #J Nr    c                     	 |                     dd          |                     dd          |                     dd          |                     dd          |                     dd          	|                     dt                     dk              |                     d	d
           st          d           	fd}|S )a  Decorator for automated coordinate array checking.

    This decorator is intended for use especially in
    :mod:`MDAnalysis.lib.distances`.
    It takes an arbitrary number of positional arguments which must correspond
    to names of positional arguments of the decorated function.
    It then checks if the corresponding values are valid coordinate arrays or
    an :class:`~MDAnalysis.core.groups.AtomGroup`.
    If the input is an array and all these arrays are single coordinates
    (i.e., their shape is ``(3,)``), the decorated function can optionally
    return a single coordinate (or angle) instead of an array of coordinates
    (or angles). This can be used to enable computations of single observables
    using functions originally designed to accept only 2-d coordinate arrays.

    If the input is an :class:`~MDAnalysis.core.groups.AtomGroup` it is
    converted into its corresponding position array via a call to
    `AtomGroup.positions`.

    The checks performed on each individual coordinate array are:

    * Check that coordinate arrays are of type :class:`numpy.ndarray`.
    * Check that coordinate arrays have a shape of ``(n, 3)`` (or ``(3,)`` if
      single coordinates are allowed; see keyword argument `allow_single`).
    * Automatic dtype conversion to ``numpy.float32``.
    * Optional replacement by a copy; see keyword argument `enforce_copy` .
    * If coordinate arrays aren't C-contiguous, they will be automatically
      replaced by a C-contiguous copy.
    * Optional check for equal length of all coordinate arrays; see optional
      keyword argument `check_lengths_match`.

    Parameters
    ----------
    *coord_names : tuple
        Arbitrary number of strings corresponding to names of positional
        arguments of the decorated function.
    **options : dict, optional
        * **enforce_copy** (:class:`bool`, optional) -- Enforce working on a
          copy of the coordinate arrays. This is useful to ensure that the input
          arrays are left unchanged. Default: ``True``
        * **enforce_dtype** (:class:`bool`, optional) -- Enforce a conversion
          to float32.  Default: ``True``
        * **allow_single** (:class:`bool`, optional) -- Allow the input
          coordinate array to be a single coordinate with shape ``(3,)``.
        * **convert_single** (:class:`bool`, optional) -- If ``True``, single
          coordinate arrays will be converted to have a shape of ``(1, 3)``.
          Only has an effect if `allow_single` is ``True``. Default: ``True``
        * **reduce_result_if_single** (:class:`bool`, optional) -- If ``True``
          and *all* input coordinates are single, a decorated function ``func``
          will return ``func()[0]`` instead of ``func()``. Only has an effect if
          `allow_single` is ``True``. Default: ``True``
        * **check_lengths_match** (:class:`bool`, optional) -- If ``True``, a
          :class:`ValueError` is raised if not all coordinate arrays contain the
          same number of coordinates. Default: ``True``
        * **allow_atomgroup** (:class:`bool`, optional) -- If ``False``, a
          :class:`TypeError` is raised if an :class:`AtomGroup` is supplied
          Default: ``False``

    Raises
    ------
    ValueError
        If the decorator is used without positional arguments (for development
        purposes only).

        If any of the positional arguments supplied to the decorator doesn't
        correspond to a name of any of the decorated function's positional
        arguments.

        If any of the coordinate arrays has a wrong shape.
    TypeError
        If any of the coordinate arrays is not a :class:`numpy.ndarray` or an
        :class:`~MDAnalysis.core.groups.AtomGroup`.

        If the dtype of any of the coordinate arrays is not convertible to
          ``numpy.float32``.

    Example
    -------

    >>> import numpy as np
    >>> import MDAnalysis as mda
    >>> from MDAnalysis.tests.datafiles import PSF, DCD
    >>> from MDAnalysis.lib.util import check_coords
    >>> @check_coords('coords1', 'coords2', allow_atomgroup=True)
    ... def coordsum(coords1, coords2):
    ...     assert coords1.dtype == np.float32
    ...     assert coords2.flags['C_CONTIGUOUS']
    ...     return coords1 + coords2
    ...
    >>> # automatic dtype conversion:
    >>> coordsum(np.zeros(3, dtype=np.int64), np.ones(3))
    array([1., 1., 1.], dtype=float32)
    >>>
    >>> # automatic handling of non-contiguous arrays:
    >>> coordsum(np.zeros(3), np.ones(6)[::2])
    array([1., 1., 1.], dtype=float32)
    >>>
    >>> # automatic handling of AtomGroups
    >>> u = mda.Universe(PSF, DCD)
    >>> try:
    ...     coordsum(u.atoms, u.select_atoms("index 1 to 10"))
    ... except ValueError as err:
    ...     err
    ValueError('coordsum(): coords1, coords2 must contain the same number of coordinates, got [3341, 10].')
    >>>
    >>> # automatic shape checking:
    >>> try:
    ...     coordsum(np.zeros(3), np.ones(6))
    ... except ValueError as err:
    ...     err
    ValueError('coordsum(): coords2.shape must be (3,) or (n, 3), got (6,)')


    .. versionadded:: 0.19.0
    .. versionchanged:: 2.3.0
       Can now accept an :class:`AtomGroup` as input, and added option
       allow_atomgroup with default False to retain old behaviour
    enforce_copyTenforce_dtypeallow_singleconvert_singlereduce_result_if_singlecheck_lengths_matchr
   allow_atomgroupFzEDecorator check_coords() cannot be used without positional arguments.c                    	
  j          j        }|j        t          |j                   j        rt           j                  nd}|j        |z
  d          	D ].}|	vr(t          d                    | j                             /fdt                      	
fd            

S )Nr   ztIn decorator check_coords(): Name '{}' doesn't correspond to any positional argument of the decorated function {}().c           
         d}t          | t          j                  rrT| j        dvs| j        d         dk    r d| d| j         }t          |          | j        dk    rd}r| d d d f         } n:| j        d	k    s| j        d         dk    r d| d
| j         }t          |          
rR	 |                     t          j        d	          } n.# t
          $ r!  d| d| j         d}t          |          d w xY w| j        d         }n\	 | j
        } | j        d         }st          d          }|n3# t          $ r& t           d| dt          |            d          w xY w| ||fS )NF)r
   r   r      z(): z#.shape must be (3,) or (n, 3), got r
   Tr   z.shape must be (n, 3) got r_  )orderr  z*.dtype must beconvertible to float32, got rE   r   zgAtomGroup or other class with a`.positions` method supplied as anargument, but allow_atomgroup is Falsez(): Parameter 'z/' must be a numpy.ndarray or an AtomGroup, got )r   r   ndarrayndimrY  r   rJ  float32rB  rV   	positionsrJ   r  )coordsargname	is_singler   ncoordrc   r  r  r  r  r  r   s         r   _check_coordszCcheck_coords.<locals>.check_coords_decorator.<locals>._check_coords  sT   I&"*-- 2 1611v|B7G17L7L$ : :' : :+1<: :  )000{a''$(	) 5%+D!!!G_Fq((fl1o.B.B$ 2 2' 2 2#)<2 2  )000  :
:!'Jc "/ " " & : : :$ 0 0' 0 0 &0 0 0 
 (//T9:  a#-F#\!_F* "'%  "	" &   #  0 0 0 0 $V0 0 0   9f,,s   1"C +C?'D9 90E)c                     t          |           k    rd_        t          |           k    r | i |S d t          |                    D ]}||v r
 | i |c S t          |           d          D ]}||vr
 | i |c S |D ]}|
vr
 | i |c S d_        t          |           } g }	}D ]}                    |          }|t          |           k     r4 | |         |          \  | |<   }}||z  }|                    |           ^ ||         |          \  ||<   }}||z  }|                    |           re|rc|                    |d                   t          |          k    r7t          d                    d                              |                    |rr | i |d         S  | i |S )NTFr   z={}(): {} must contain the same number of coordinates, got {}.z, )	r(   _invalid_calllistindexr+  countr   rO   rf   )r   r  r*   ncoords
all_singleidxr(  r)  r*  r  argnamesr  coord_namesr   r  nargsnposargsposargnamesr  r  s           r   r  z=check_coords.<locals>.check_coords_decorator.<locals>.wrapper  s    4yyH$$(,%t99u$$40000'#d))4 5 5Dv~~#tT4V44444 & (D		4 5 5D6))#tT4V44444 * # 5 5D8++#tT4V44444 , ).%::DG%J# + +!''--T??3@=S	44 40DIy& )+JNN6****6Cmtd7 73F4L)V )+JNN6****" w ==,,G<<$!6%;)?)?II    05 0tT,V,,Q//4((((r    )	r   __code__co_varnamesr(   __defaults__co_argcountr   rO   r   )r  code	ndefaultsr*   r*  r3  r   r5  r6  r7  r  r  r  r  r  r4  r  r  r  s   `   @@@@@@@r   check_coords_decoratorz,check_coords.<locals>.check_coords_decorator  sQ   }#D$%%.2.?FC)***Q	#i/yy)   	 	D;&&  vdDM22	   '6	- 6	- 6	- 6	- 6	- 6	- 6	- 6	- 6	- 6	-p 
t1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 1	) 
1	)f r    )getr(   r   )
r4  optionsr>  r  r  r  r  r  r  r  s
   `  @@@@@@@r   check_coordsrA    s   l ;;~t44LKK66M;;~t44L[[!1488N%kk*CTJJ!++s;//!3  kk"3U;;O 
$
 
 	

@ @ @ @ @ @ @ @ @ @ @ @D "!r    c                 <     t                      fd            }|S )aD  Decorator triggering a ``ValueError`` if the underlying group is empty.

    Avoids downstream errors in computing properties of empty atomgroups.

    Raises
    ------
    ValueError
        If the input :class:`~MDAnalysis.core.groups.AtomGroup`,
        of a decorated method is empty.


    .. versionadded:: 2.4.0
    c                 L    | j         st          d           | g|R i |}|S )NzAtomGroup is empty.)rZ  r   )r  r   r  r  r  s       r   r  z*check_atomgroup_not_empty.<locals>.wrapper(	  sD     { 	92333 ![888888Fr    r   r  s   ` r   check_atomgroup_not_emptyrD  	  s8     ;     Nr    c                     || _         | S r   )r   )r  r*   s     r   _set_function_namerF  >	  s    DMKr    c                   *    e Zd ZdZ	 	 	 	 	 ddZd ZdS )
_Deprecatez
    Decorator class to deprecate old functions.

    Refer to `deprecate` for details.

    See Also
    --------
    deprecate


    .. versionadded:: 0.19.0
    Nc                     || _         || _        |t          d          t          |          | _        |t          |          n|| _        || _        d S )Nz;deprecate: provide release in which feature was deprecated.)old_namenew_namer   r+   releaseremover  )r   rJ  rK  rL  rM  r  s         r   r   z_Deprecate.__init__Q	  s^     ! ?*   7||%+%7c&kkkVr    c                    | j         }| j        }| j        }| j        }| j        }| 	 j        }n# t          $ r
 j        }Y nw xY w|d                    |          }	nd                    ||          }	|	d}
|d                    ||          }
d|
z   z  |d|z   z  fd}t          ||          }	 t          j
                  }n# t          $ r d}Y nw xY wt          d                    ||r|n|	|
                    }d	                    |	||          }||_
        	 j        }|j                            |           n# t          $ r Y nw xY w|S )
z:
        Decorator call.  Refer to ``decorate``.

        Nz`{0}` is deprecated!z'`{0}` is deprecated, use `{1}` instead!r>   z%`{0}` will be removed in release {1}.
c                  L    t          j        t          d            | i |S )zThis function is deprecated.r   )r  )rM   rN   r   )r   kwdsr  warn_messages     r   newfuncz$_Deprecate.__call__.<locals>.newfunc	  s0    M,(:qIIII4&&&&r    zE


        .. deprecated:: {0}
           {1}
           {2}
        z{0}

{1}
{2}
)rJ  rK  r  rL  rM  r   rJ   rO   rF  dedent_docstringr   rV   __dict__update)r   r  r   r  rJ  rK  r  rL  rM  depdocremove_textrS  docdeprecation_textr-  rR  s    `             @r   __call__z_Deprecate.__call__d	  s   
 ==,,)=! ) ) )=)+228<<FF>EE( F AHH& K D;..LD7N*L	' 	' 	' 	' 	' 	'
 %Wh77	"4<00CC 	 	 	CCC	 , FG7 
 
 "((6FGG	'A ##A&&&&  	 	 	D	 s3   1 AA;C CC)E 
EE)NNNNN)r   r   r   r   r   r[  rG   r    r   rH  rH  C	  sW             &H H H H Hr    rH  c                  l    | r'| d         }| dd         }  t          | i ||          S t          | i |S )aw
  Issues a DeprecationWarning, adds warning to `old_name`'s
    docstring, rebinds ``old_name.__name__`` and returns the new
    function object.

    This function may also be used as a decorator.

    It adds a restructured text ``.. deprecated:: release`` block with
    the sphinx deprecated role to the end of the docs. The `message`
    is added under the deprecation block and contains the `release` in
    which the function was deprecated.

    Parameters
    ----------
    func : function
        The function to be deprecated.
    old_name : str, optional
        The name of the function to be deprecated. Default is None, in
        which case the name of `func` is used.
    new_name : str, optional
        The new name for the function. Default is None, in which case the
        deprecation message is that `old_name` is deprecated. If given, the
        deprecation message is that `old_name` is deprecated and `new_name`
        should be used instead.
    release : str
        Release in which the function was deprecated. This is given as
        a keyword argument for technical reasons but is required; a
        :exc:`ValueError` is raised if it is missing.
    remove : str, optional
        Release for which removal of the feature is planned.
    message : str, optional
        Additional explanation of the deprecation.  Displayed in the
        docstring after the warning.

    Returns
    -------
    old_func : function
        The deprecated function.

    Examples
    --------
    When :func:`deprecate` is used as a function as in the following
    example,

    .. code-block:: python

       oldfunc = deprecate(func, release="0.19.0", remove="1.0",
                           message="Do it yourself instead.")

    then ``oldfunc`` will return a value after printing
    :exc:`DeprecationWarning`; ``func`` is still available as it was
    before.

    When used as a decorator, ``func`` will be changed and issue the
    warning and contain the deprecation note in the do string.

    .. code-block:: python

       @deprecate(release="0.19.0", remove="1.0",
                  message="Do it yourself instead.")
       def func():
           \"\"\"Just pass\"\"\"
           pass

    The resulting doc string (``help(func)``) will look like:

    .. code-block:: reST

       `func` is deprecated!

       Just pass.

       .. deprecated:: 0.19.0
          Do it yourself instead.
          `func` will be removed in 1.0.

    (It is possible but confusing to change the name of ``func`` with
    the decorator so it is not recommended to use the `new_func`
    keyword argument with the decorator.)

    .. versionadded:: 0.19.0

    r   r
   N)rH  )r   r  fns      r   	deprecater^  	  sS    n  +!WABBx*z4*6**2...4*6***r    c                    |                                  }t          |          dk     r|                                 S |d                                         dz   t          j        d                    |dd                             z   S )aO  Dedent typical python doc string.

    Parameters
    ----------
    text : str
        string, typically something like ``func.__doc__``.

    Returns
    -------
    str
        string with the leading common whitespace removed from each
        line

    See Also
    --------
    textwrap.dedent


    .. versionadded:: 0.19.0
    r   r   rO  r
   N)
splitlinesr(   lstriptextwrapdedentrf   )textliness     r   rT  rT  
  sn    * OOE
5zzA~~{{}} 8??t#hodiiabb	6J6J&K&KKKr    c                    | t          d          ddlm} t          j        | t          j        d          } | j        dk    rt          d          t          j        | d	d         d
k              rd| dd	         fS d ||           fS )a  Take a box input and deduce what type of system it represents based on
    the shape of the array and whether all angles are 90 degrees.

    Parameters
    ----------
    box : array_like
        The unitcell dimensions of the system, which can be orthogonal or
        triclinic and must be provided in the same format as returned by
        :attr:`MDAnalysis.coordinates.timestep.Timestep.dimensions`:
        ``[lx, ly, lz, alpha, beta, gamma]``.

    Returns
    -------
    boxtype : {``'ortho'``, ``'tri_vecs'``}
        String indicating the box type (orthogonal or triclinic).
    checked_box : numpy.ndarray
        Array of dtype ``numpy.float32`` containing box information:
          * If `boxtype` is ``'ortho'``, `cecked_box` will have the shape ``(3,)``
            containing the x-, y-, and z-dimensions of the orthogonal box.
          * If  `boxtype` is ``'tri_vecs'``, `cecked_box` will have the shape
            ``(3, 3)`` containing the triclinic box vectors in a lower triangular
            matrix as returned by
            :meth:`~MDAnalysis.lib.mdamath.triclinic_vectors`.

    Raises
    ------
    ValueError
        If `box` is not of the form ``[lx, ly, lz, alpha, beta, gamma]``
        or contains data that is not convertible to ``numpy.float32``.

    See Also
    --------
    MDAnalysis.lib.mdamath.triclinic_vectors


    .. versionchanged: 0.19.0
       * Enforced correspondence of `box` with specified format.
       * Added automatic conversion of input to :class:`numpy.ndarray` with
         dtype ``numpy.float32``.
       * Now also returns the box in the format expected by low-level functions
         in :mod:`~MDAnalysis.lib.c_distances`.
       * Removed obsolete box types ``tri_box`` and ``tri_vecs_bad``.
    NzBox is Noner
   )triclinic_vectorsr_  )rB  r!  )   zNInvalid box information. Must be of the form [lx, ly, lz, alpha, beta, gamma].r   g     V@orthotri_vecs)r   mdamathrg  r   rG  r$  rY  r|   )boxrg  s     r   	check_boxrm  /
  s    X {'''******
*S
#
6
6
6C
yD0
 
 	
 
vc!""go  BQB((----r    c                 r     t          j                   t          j                    fd            }|S )a  Decorator to store arguments passed to the init method of a class.

    Arguments are stored as a dictionary in ``cls._kwargs``.

    Notes
    -----
    * Only does a shallow copy, if the arguments are changed
      by the class after passing through the decorator this will be
      reflected in the stored arguments.
    * If not empty, ``args`` is not unpacked and stored as-is in the
      dictionary. If no ``args`` are passed, then no ``arg`` entry will be
      stored in the dictionary.


    .. versionadded:: 2.2.0
    c                    t          | d          s 	j        | g|R i |}|                                 i | _        |j                                        D ]d\  }}|dk    rY|dk    r%|                                D ]\  }}|| j        |<   6|dk    rt          |          dk    r
|| j        |<   Z|| j        |<   e | g|R i |S )N_kwargsr   r  r   r   )rk   bindapply_defaultsrp  	argumentsr  r(   )
r   r   r  
arg_valuesr  argr;  r  r  sigs
           r   r  z%store_init_arguments.<locals>.wrapper}
  s   tY'' 	0!$888888J%%'''DL&06688 	0 	0S&==h$'IIKK 0 0DAq./DLOO0s88a<<03DL-,/S)tD*4***6***r    )r  	signature	functoolsr   )r  r  rv  s   ` @r   store_init_argumentsry  j
  sP    " 
D
!
!C_T+ + + + + +" Nr    c                  :    t           j        j        dk    rd } nd} | S )Nz2.0.0rc1F)r   r  NumpyVersion)r  s    r   no_copy_shimr|  
  s#    	vj((Kr    r
  returnc           	          	 t          d                    t          j        t          j        |                                                               S # t          $ r Y dS w xY w)a  Convert the leading number part of a string to an integer.

    Parameters
    ----------
    s : str
        The string to convert to an integer.

    Returns
    -------
    number : int
        The first numeric part of the string converted to an integer.
        If the string does not start with a number, 0 is returned.

    Examples
    --------
    >>> from MDAnalysis.lib.util import atoi
    >>> atoi('34f4')
    34
    >>> atoi('foo')
    0


    .. versionadded:: 2.8.0
    r>   r   )r  rf   	itertools	takewhiler+   isdigitr  r   r	  s    r   atoir  
  sZ    22779.s{AGGIIFFGGHHH   qqs   AA 
A! A!
modulenamec                 D    t           j                            |           duS )zChecks if module is installed

    Parameters
    ----------
    modulename : str
        name of the module to be tested


    .. versionadded:: 2.8.0
    N)	importlibutil	find_spec)r  s    r   is_installedr  
  s     >##J//t;;r    )NF)r2   Tr   )gr   r_   __docformat__r<   rQ   rx  rI   r  r  ior  r$   os.pathrerb  rM   r  
contextlibr   r   mmtfnumpyr   numpy.testingr   
exceptionsr   r	   picklable_file_ior   r   r   _cutilr   ImportErrorr   r   r1   r:   r6   rH   rP   ri   rp   r)   r   total_orderingIOBasePathLiker   r   r   r   r   r  r   r  FORTRAN_format_regexcompiler7  r  rX  r  r  rT  r\  canonical_inverse_aa_codesr  r  alternative_inverse_aa_codesr  rV  r  VERBOSE
IGNORECASEr  r  r  r  r  r  r  r  r  r  r  r  r  r   r  rA  rD  rF  rH  r^  rT  rm  ry  r|  r+   r  r  r  rG   r    r   <module>r     s9  .l lZ 


% 


            				     				  				    % % % % % %            & & & & & & 8 8 8 8 8 8 8 8 M M M M M M M M M M%%%%%%%   
+	    # # #
1 1 1 1B / / / /d{ { { {| (,#    6!/ !/ !/H9 9 9
+' +' +'\) ) )X LH LH LH LH LH")R[ LH LH LH^
    (  D  :1 1 1h    *:  %"*%9::   
.
 .
 .
 .
 .
v .
 .
 .
bUH UH UH UH UHF UH UH UHp*O *O *OZ8 8 8@	3	3 
3 
3	
 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3  
3!" 
3#$ )   0 !;!A!A!C!C    
C  #S */ 6;S BG 
C   C    C	  	 
 C 
   C  C    %c         2 3 3 3   4 5 5 5"+ "+ "+N "* J &+& +& +&\
 
 
 	 	 	 	 	 	 	 	F F F FR:0 :0 :0 :0zKF KF KF\C C C*       <& & &4  %uww    @     b 9 9  9xG" G" G"T  J  
i i i i i i i iX\+ \+ \+FL L L:8. 8. 8.v% % %P  C C    ><S < < < < < <s   .A5 5B