import statistics


def find_heavy_genes(weight_matrix):
    """
    Return the heavy genes based on input weight_matrix. The return value
    is a dict, in which each key is a positive or nagative sigature name,
    and each value is a nested dict, whose keys are heavy genes' systematic
    names, and values are corresponding weights.
    """

    heavy_genes = dict()
    for node_name, genes_weights in weight_matrix.items():
        weights = genes_weights.values()

        mean = statistics.mean(weights)
        std_dev = statistics.stdev(weights)

        pos_sig_name = node_name + "pos"
        neg_sig_name = node_name + "neg"

        heavy_genes[pos_sig_name] = dict()
        heavy_genes[neg_sig_name] = dict()

        for g, w in genes_weights.items():
            if w > mean + 2.5 * std_dev:
                heavy_genes[pos_sig_name][g] = w
            elif w < mean - 2.5 * std_dev:
                heavy_genes[neg_sig_name][g] = w

    return heavy_genes