def build_data_dict(stats_arr, prefix):
    """
    Build a data dictionary with columns named according to (k,z) bins, a
    threshold value, and some prefix.

    Assumes that stats_arr has shape: (N_samp, N_thres, N_z, N_kbins)
    """
    # Get no. of points in each dimension.
    N_sam, N_thres, N_z, N_kbins = stats_arr.shape

    # Create dictionary with column names that can be used by ColumnDataSource
    data_dict = {}
    for n in range(N_thres):
        for j in range(N_z):
            for m in range(N_kbins):
                key = "tot_%s_h%d_k%d_z%d" % (prefix, n+1, m+1, j+1)
                data_dict[key] = stats_arr[:,n, j, m]
    return data_dict