示例#1
0
def get_colors(n: int, labels: Union[dict, np.ndarray, None], scores: Union[dict, np.ndarray, None], color: str) \
                -> np.ndarray:
    """Return the colors using either labels or scores or default color."""
    colors = np.array(n * [color]).astype('U64')
    if labels is not None:
        colors_label = STANDARD_COLORS.copy()
        if isinstance(labels, dict):
            index = np.array(list(labels.keys()))
            values = np.array(list(labels.values())).astype(int)
            labels = -np.ones(n, dtype=int)
            labels[index] = values
        index = labels >= 0
        colors[index] = colors_label[labels[index] % len(colors_label)]
    elif scores is not None:
        colors_score = COOLWARM_RGB.copy()
        n_colors = colors_score.shape[0]
        colors_score_svg = np.array(['rgb' + str(tuple(colors_score[i])) for i in range(n_colors)])
        if isinstance(scores, dict):
            index = np.array(list(scores.keys()))
            values = np.array(list(scores.values()))
            scores = (min_max_scaling(values) * (n_colors - 1)).astype(int)
            colors[index] = colors_score_svg[scores]
        else:
            scores = (min_max_scaling(scores) * (n_colors - 1)).astype(int)
            colors = colors_score_svg[scores]
    return colors
示例#2
0
def get_node_colors(n: int,
                    labels: Optional[Iterable],
                    scores: Optional[Iterable],
                    membership: Optional[sparse.csr_matrix],
                    node_color: str,
                    label_colors: Optional[Iterable],
                    score_min: Optional[float] = None,
                    score_max: Optional[float] = None) -> np.ndarray:
    """Return the colors of the nodes using either labels or scores or default color."""
    node_colors = np.array(n * [node_color]).astype('U64')
    if labels is not None:
        if isinstance(labels, dict):
            keys = np.array(list(labels.keys()))
            values = np.array(list(labels.values())).astype(int)
            labels = -np.ones(n, dtype=int)
            labels[keys] = values
        elif isinstance(labels, list):
            if len(labels) != n:
                raise ValueError(
                    "The number of labels must be equal to the corresponding number of nodes."
                )
            else:
                labels = np.array(labels)
        index = labels >= 0
        label_colors = get_label_colors(label_colors)
        node_colors[index] = label_colors[labels[index] % len(label_colors)]
    elif scores is not None:
        colors_score = COOLWARM_RGB.copy()
        n_colors = colors_score.shape[0]
        colors_score_svg = np.array(
            ['rgb' + str(tuple(colors_score[i])) for i in range(n_colors)])
        if isinstance(scores, dict):
            keys = np.array(list(scores.keys()))
            values = np.array(list(scores.values()))
            scores = (min_max_scaling(values, score_min, score_max) *
                      (n_colors - 1)).astype(int)
            node_colors[keys] = colors_score_svg[scores]
        else:
            if isinstance(scores, list):
                if len(scores) != n:
                    raise ValueError(
                        "The number of scores must be equal to the corresponding number of nodes."
                    )
                else:
                    scores = np.array(scores)
            scores = (min_max_scaling(scores, score_min, score_max) *
                      (n_colors - 1)).astype(int)
            node_colors = colors_score_svg[scores]
    elif membership is not None:
        if isinstance(label_colors, dict):
            raise TypeError(
                "Label colors must be a list or an array when using a membership."
            )
        label_colors = get_label_colors(label_colors)
        node_colors = label_colors
    return node_colors