code: def from_mlab_linkage(Z):
      """
      Convert a linkage matrix generated by MATLAB(TM) to a new
      linkage matrix compatible with this module.
  
      The conversion does two things:
  
       * the indices are converted from ``1..N`` to ``0..(N-1)`` form,
         and
  
       * a fourth column ``Z[:,3]`` is added where ``Z[i,3]`` represents the
         number of original observations (leaves) in the non-singleton
         cluster ``i``.
  
      This function is useful when loading in linkages from legacy data
      files generated by MATLAB.
  
      Parameters
      ----------
      Z : ndarray
          A linkage matrix generated by MATLAB(TM).
  
      Returns
      -------
      ZS : ndarray
          A linkage matrix compatible with ``scipy.cluster.hierarchy``.
  
      See Also
      --------
      linkage : for a description of what a linkage matrix is.
      to_mlab_linkage : transform from SciPy to MATLAB format.
  
      Examples
      --------
      >>> import numpy as np
      >>> from scipy.cluster.hierarchy import ward, from_mlab_linkage
  
      Given a linkage matrix in MATLAB format ``mZ``, we can use
      `scipy.cluster.hierarchy.from_mlab_linkage` to import
      it into SciPy format:
  
      >>> mZ = np.array([[1, 2, 1], [4, 5, 1], [7, 8, 1],
      ...                [10, 11, 1], [3, 13, 1.29099445],
      ...                [6, 14, 1.29099445],
      ...                [9, 15, 1.29099445],
      ...                [12, 16, 1.29099445],
      ...                [17, 18, 5.77350269],
      ...                [19, 20, 5.77350269],
      ...                [21, 22,  8.16496581]])
  
      >>> Z = from_mlab_linkage(mZ)
      >>> Z
      array([[  0.        ,   1.        ,   1.        ,   2.        ],
             [  3.        ,   4.        ,   1.        ,   2.        ],
             [  6.        ,   7.        ,   1.        ,   2.        ],
             [  9.        ,  10.        ,   1.        ,   2.        ],
             [  2.        ,  12.        ,   1.29099445,   3.        ],
             [  5.        ,  13.        ,   1.29099445,   3.        ],
             [  8.        ,  14.        ,   1.29099445,   3.        ],
             [ 11.        ,  15.        ,   1.29099445,   3.        ],
             [ 16.        ,  17.        ,   5.77350269,   6.        ],
             [ 18.        ,  19.        ,   5.77350269,   6.        ],
             [ 20.        ,  21.        ,   8.16496581,  12.        ]])
  
      As expected, the linkage matrix ``Z`` returned includes an
      additional column counting the number of original samples in
      each cluster. Also, all cluster indices are reduced by 1
      (MATLAB format uses 1-indexing, whereas SciPy uses 0-indexing).
  
      """
      Z = np.asarray(Z, dtype=np.double, order="c")
      Zs = Z.shape
  
      # If it's empty, return it.
      if len(Zs) == 0 or (len(Zs) == 1 and Zs[0] == 0):
          return Z.copy()
  
      if len(Zs) != 2:
          raise ValueError("The linkage array must be rectangular.")
  
      # If it contains no rows, return it.
      if Zs[0] == 0:
          return Z.copy()
  
      Zpart = Z.copy()
      if (
          Zpart[:, 0:2].min() != 1.0
          and Zpart[:, 0:2].max() != 2 * Zs[0]
      ):
          raise ValueError("The format of the indices is not 1..N")
  
      Zpart[:, 0:2] -= 1.0
      CS = np.zeros((Zs[0],), dtype=np.double)
      _hierarchy.calculate_cluster_sizes(Zpart, CS, int(Zs[0]) + 1)
      return np.hstack([Zpart, CS.reshape(Zs[0], 1)])
  
imports: import warnings
  import bisect
  from collections import deque
  import numpy
  from .None import _hierarchy, _optimal_leaf_ordering
  import scipy.spatial.distance
  from scipy._lib._disjoint_set import DisjointSet
label_desc: Use image node_id 21 for calling a global function with example usage: from_mlab_linkage(Z) and returns: np, Z, Z