def from_mlab_linkage(Z):
 Z = np.asarray(Z, dtype=np.double, order='c')
 Zs = Z.shape
 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 (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)])

def from_mlab_linkage ( Z ) : Z = np . asarray ( Z , dtype = np . double , order = 'c' ) Zs = Z . shape 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 ( 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 ) ] )
converts a linkage matrix generated by matlab to a new linkage matrix compatible with this module .