def _dendrogram_calculate_info(Z, p, truncate_mode,<EOL>color_threshold=np.inf, get_leaves=True,<EOL>orientation='<STR_LIT>', labels=None,<EOL>count_sort=False, distance_sort=False,<EOL>show_leaf_counts=False, i=-<NUM_LIT:1>, iv=<NUM_LIT:0.0>,<EOL>ivl=[], n=<NUM_LIT:0>, icoord_list=[], dcoord_list=[],<EOL>lvs=None, mhr=False,<EOL>current_color=[], color_list=[],<EOL>currently_below_threshold=[],<EOL>leaf_label_func=None, level=<NUM_LIT:0>,<EOL>contraction_marks=None,<EOL>link_color_func=None):
if n == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if i == -<NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if truncate_mode == '<STR_LIT>':<EOL><INDENT>if i < <NUM_LIT:2> * n - p and i >= n:<EOL><INDENT>d = Z[i - n, <NUM_LIT:2>]<EOL>_append_nonsingleton_leaf_node(Z, p, n, level, lvs, ivl,<EOL>leaf_label_func, i, labels,<EOL>show_leaf_counts)<EOL>if contraction_marks is not None:<EOL><INDENT>_append_contraction_marks(Z, iv + <NUM_LIT>, i, n, contraction_marks)<EOL><DEDENT>return (iv + <NUM_LIT>, <NUM_LIT>, <NUM_LIT:0.0>, d)<EOL><DEDENT>elif i < n:<EOL><INDENT>_append_singleton_leaf_node(Z, p, n, level, lvs, ivl,<EOL>leaf_label_func, i, labels)<EOL>return (iv + <NUM_LIT>, <NUM_LIT>, <NUM_LIT:0.0>, <NUM_LIT:0.0>)<EOL><DEDENT><DEDENT>elif truncate_mode in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if i > n and level > p:<EOL><INDENT>d = Z[i - n, <NUM_LIT:2>]<EOL>_append_nonsingleton_leaf_node(Z, p, n, level, lvs, ivl,<EOL>leaf_label_func, i, labels,<EOL>show_leaf_counts)<EOL>if contraction_marks is not None:<EOL><INDENT>_append_contraction_marks(Z, iv + <NUM_LIT>, i, n, contraction_marks)<EOL><DEDENT>return (iv + <NUM_LIT>, <NUM_LIT>, <NUM_LIT:0.0>, d)<EOL><DEDENT>elif i < n:<EOL><INDENT>_append_singleton_leaf_node(Z, p, n, level, lvs, ivl,<EOL>leaf_label_func, i, labels)<EOL>return (iv + <NUM_LIT>, <NUM_LIT>, <NUM_LIT:0.0>, <NUM_LIT:0.0>)<EOL><DEDENT><DEDENT>elif truncate_mode in ('<STR_LIT>',):<EOL><INDENT>pass<EOL><DEDENT>if i < n:<EOL><INDENT>_append_singleton_leaf_node(Z, p, n, level, lvs, ivl,<EOL>leaf_label_func, i, labels)<EOL>return (iv + <NUM_LIT>, <NUM_LIT>, <NUM_LIT:0.0>, <NUM_LIT:0.0>)<EOL><DEDENT>aa = int(Z[i - n, <NUM_LIT:0>])<EOL>ab = int(Z[i - n, <NUM_LIT:1>])<EOL>if aa > n:<EOL><INDENT>na = Z[aa - n, <NUM_LIT:3>]<EOL>da = Z[aa - n, <NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>na = <NUM_LIT:1><EOL>da = <NUM_LIT:0.0><EOL><DEDENT>if ab > n:<EOL><INDENT>nb = Z[ab - n, <NUM_LIT:3>]<EOL>db = Z[ab - n, <NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>nb = <NUM_LIT:1><EOL>db = <NUM_LIT:0.0><EOL><DEDENT>if count_sort == '<STR_LIT>' or count_sort == True:<EOL><INDENT>if na > nb:<EOL><INDENT>ua = ab<EOL>ub = aa<EOL><DEDENT>else:<EOL><INDENT>ua = aa<EOL>ub = ab<EOL><DEDENT><DEDENT>elif count_sort == '<STR_LIT>':<EOL><INDENT>if na > nb:<EOL><INDENT>ua = aa<EOL>ub = ab<EOL><DEDENT>else:<EOL><INDENT>ua = ab<EOL>ub = aa<EOL><DEDENT><DEDENT>elif distance_sort == '<STR_LIT>' or distance_sort == True:<EOL><INDENT>if da > db:<EOL><INDENT>ua = ab<EOL>ub = aa<EOL><DEDENT>else:<EOL><INDENT>ua = aa<EOL>ub = ab<EOL><DEDENT><DEDENT>elif distance_sort == '<STR_LIT>':<EOL><INDENT>if da > db:<EOL><INDENT>ua = aa<EOL>ub = ab<EOL><DEDENT>else:<EOL><INDENT>ua = ab<EOL>ub = aa<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ua = aa<EOL>ub = ab<EOL><DEDENT>(uiva, uwa, uah, uamd) =_dendrogram_calculate_info(<EOL>Z=Z, p=p,<EOL>truncate_mode=truncate_mode,<EOL>color_threshold=color_threshold,<EOL>get_leaves=get_leaves,<EOL>orientation=orientation,<EOL>labels=labels,<EOL>count_sort=count_sort,<EOL>distance_sort=distance_sort,<EOL>show_leaf_counts=show_leaf_counts,<EOL>i=ua, iv=iv, ivl=ivl, n=n,<EOL>icoord_list=icoord_list,<EOL>dcoord_list=dcoord_list, lvs=lvs,<EOL>current_color=current_color,<EOL>color_list=color_list,<EOL>currently_below_threshold=currently_below_threshold,<EOL>leaf_label_func=leaf_label_func,<EOL>level=level + <NUM_LIT:1>, contraction_marks=contraction_marks,<EOL>link_color_func=link_color_func)<EOL>h = Z[i - n, <NUM_LIT:2>]<EOL>if h >= color_threshold or color_threshold <= <NUM_LIT:0>:<EOL><INDENT>c = '<STR_LIT:b>'<EOL>if currently_below_threshold[<NUM_LIT:0>]:<EOL><INDENT>current_color[<NUM_LIT:0>] = (current_color[<NUM_LIT:0>] + <NUM_LIT:1>) % len(_link_line_colors)<EOL><DEDENT>currently_below_threshold[<NUM_LIT:0>] = False<EOL><DEDENT>else:<EOL><INDENT>currently_below_threshold[<NUM_LIT:0>] = True<EOL>c = _link_line_colors[current_color[<NUM_LIT:0>]]<EOL><DEDENT>(uivb, uwb, ubh, ubmd) =_dendrogram_calculate_info(<EOL>Z=Z, p=p,<EOL>truncate_mode=truncate_mode,<EOL>color_threshold=color_threshold,<EOL>get_leaves=get_leaves,<EOL>orientation=orientation,<EOL>labels=labels,<EOL>count_sort=count_sort,<EOL>distance_sort=distance_sort,<EOL>show_leaf_counts=show_leaf_counts,<EOL>i=ub, iv=iv + uwa, ivl=ivl, n=n,<EOL>icoord_list=icoord_list,<EOL>dcoord_list=dcoord_list, lvs=lvs,<EOL>current_color=current_color,<EOL>color_list=color_list,<EOL>currently_below_threshold=currently_below_threshold,<EOL>leaf_label_func=leaf_label_func,<EOL>level=level + <NUM_LIT:1>, contraction_marks=contraction_marks,<EOL>link_color_func=link_color_func)<EOL>max_dist = max(uamd, ubmd, h)<EOL>icoord_list.append([uiva, uiva, uivb, uivb])<EOL>dcoord_list.append([uah, h, h, ubh])<EOL>if link_color_func is not None:<EOL><INDENT>v = link_color_func(int(i))<EOL>if not isinstance(v, string_types):<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>color_list.append(v)<EOL><DEDENT>else:<EOL><INDENT>color_list.append(c)<EOL><DEDENT>return (((uiva + uivb) / <NUM_LIT:2>), uwa + uwb, h, max_dist)<EOL>
Calculates the endpoints of the links as well as the labels for the
the dendrogram rooted at the node with index i. iv is the independent
variable value to plot the left-most leaf node below the root node i
(if orientation='top', this would be the left-most x value where the
plotting of this root node i and its descendents should begin).

ivl is a list to store the labels of the leaf nodes. The leaf_label_func
is called whenever ivl != None, labels == None, and
leaf_label_func != None. When ivl != None and labels != None, the
labels list is used only for labeling the leaf nodes. When
ivl == None, no labels are generated for leaf nodes.

When get_leaves==True, a list of leaves is built as they are visited
in the dendrogram.

Returns a tuple with l being the independent variable coordinate that
corresponds to the midpoint of cluster to the left of cluster i if
i is non-singleton, otherwise the independent coordinate of the leaf
node if i is a leaf node.

Returns
-------
A tuple (left, w, h, md), where:

  * left is the independent variable coordinate of the center of the
    the U of the subtree

  * w is the amount of space used for the subtree (in independent
    variable units)

  * h is the height of the subtree in dependent variable units

  * md is the max(Z[*,2]) for all nodes * below and including
    the target node.