def relabel_sequential(label_field, offset=1):
 m = label_field.max()
 if (not np.issubdtype(label_field.dtype, np.int)):
  new_type = np.min_scalar_type(int(m))
  label_field = label_field.astype(new_type)
  m = m.astype(new_type)
 labels = np.unique(label_field)
 labels0 = labels[(labels != 0)]
 if (m == len(labels0)):
  return (label_field, labels, labels)
 forward_map = np.zeros((m + 1), int)
 forward_map[labels0] = np.arange(offset, (offset + len(labels0)))
 if (not (labels == 0).any()):
  labels = np.concatenate(([0], labels))
 inverse_map = np.zeros(((offset - 1) + len(labels)), dtype=np.intp)
 inverse_map[(offset - 1):] = labels
 relabeled = forward_map[label_field]
 return (relabeled, forward_map, inverse_map)

def relabel_sequential ( label_field , offset = 1 ) : m = label_field . max ( ) if ( not np . issubdtype ( label_field . dtype , np . int ) ) : new_type = np . min_scalar_type ( int ( m ) ) label_field = label_field . astype ( new_type ) m = m . astype ( new_type ) labels = np . unique ( label_field ) labels0 = labels [ ( labels != 0 ) ] if ( m == len ( labels0 ) ) : return ( label_field , labels , labels ) forward_map = np . zeros ( ( m + 1 ) , int ) forward_map [ labels0 ] = np . arange ( offset , ( offset + len ( labels0 ) ) ) if ( not ( labels == 0 ) . any ( ) ) : labels = np . concatenate ( ( [ 0 ] , labels ) ) inverse_map = np . zeros ( ( ( offset - 1 ) + len ( labels ) ) , dtype = np . intp ) inverse_map [ ( offset - 1 ) : ] = labels relabeled = forward_map [ label_field ] return ( relabeled , forward_map , inverse_map )
relabel arbitrary labels to {offset .