pattern_weights.py 3.96 KB
Newer Older
Jiri Borovec committed
1 2 3 4 5
"""


Copyright (C) 2015-2016 Jiri Borovec <jiri.borovec@fel.cvut.cz>
"""
Jiri Borovec committed
6
import logging
Jiri Borovec committed
7 8 9

import numpy as np

Jiri Borovec committed
10

11
def initialise_weights_random(nb_imgs, nb_labels, ratio_select=0.2):
Jiri Borovec committed
12 13
    """
    :param nb_imgs: int, numer of all images
14 15
    :param nb_labels: int, numer of all avalaible labels
    :param ratio_select: float<0, 1> defining how many should be set on,
Jiri Borovec committed
16
        1 means all and 0 means none
17
    :return: np.array<nb_imgs, nb_labels>
Jiri Borovec committed
18
    """
Jiri Borovec committed
19
    logging.debug('initialise weights for %i images and %i labels '
20 21
                 'as random selection', nb_imgs, nb_labels)
    prob = np.random.random((nb_imgs, nb_labels))
Jiri Borovec committed
22
    weights = np.zeros_like(prob)
23
    weights[prob <= ratio_select] = 1
Jiri Borovec committed
24 25 26 27 28 29 30 31 32
    return weights


def convert_weights_binary2indexes(weights):
    """ convert binary matrix oof weights to list of indexes o activated ptns

    :param weights: np.array<nb_imgs, nb_lbs>
    :return: [[int, ...]] * nb_imgs
    """
Jiri Borovec committed
33
    logging.debug('convert binary weights %s to list of indexes with True',
Jiri Borovec committed
34
                 repr(weights.shape))
Jiri Borovec committed
35
    # if type(weights) is np.ndarray:  weights = weights.tolist()
Jiri Borovec committed
36
    w_index = [None] * weights.shape[0]
Jiri Borovec committed
37 38 39 40
    for i in range(weights.shape[0]):
        # find postions equal 1
        # vec = [j for j in range(weights.shape[1]) if weights[i,j]==1]
        vec = np.where(weights[i, :] == 1)[0]
Jiri Borovec committed
41
        w_index[i] = vec + 1
Jiri Borovec committed
42 43 44
    # idxs =  np.where(weights == 1)
    # for i in range(weights.shape[0]):
    #     w_idx[i] = idxs[1][idxs[0]==i] +1
Jiri Borovec committed
45
    return w_index
Jiri Borovec committed
46 47 48 49 50 51 52 53


def weights_image_atlas_overlap_major(img, atlas):
    """
    :param img: np.array<w, h>
    :param atlas: np.array<w, h>
    :return: [int] * nb_lbs of values {0, 1}
    """
Jiri Borovec committed
54
    # logging.debug('weights input image according given atlas')
55
    weights = weights_image_atlas_overlap_threshold(img, atlas, 1.)
Jiri Borovec committed
56 57 58 59 60 61 62 63 64
    return weights


def weights_image_atlas_overlap_partial(img, atlas):
    """
    :param img: np.array<w, h>
    :param atlas: np.array<w, h>
    :return: [int] * nb_lbs of values {0, 1}
    """
Jiri Borovec committed
65
    # logging.debug('weights input image according given atlas')
Jiri Borovec committed
66
    labels = np.unique(atlas).tolist()
67 68
    weights = weights_image_atlas_overlap_threshold(img, atlas,
                                                    (1. / np.max(labels)))
Jiri Borovec committed
69 70 71
    return weights


72
def weights_image_atlas_overlap_threshold(img, atlas, threshold=1.):
Jiri Borovec committed
73 74 75 76 77
    """ estimate what patterns are activated  with given atlas and input image
    compute overlap matrix and eval nr of overlapping and non pixels and threshold

    :param img: np.array<w, h>
    :param atlas: np.array<w, h>
78
    :param threshold: float, represent the ration between overlapping and non pixels
Jiri Borovec committed
79 80
    :return: [int] * nb_lbs of values {0, 1}
    """
Jiri Borovec committed
81
    # logging.debug('weights input image according given atlas')
Jiri Borovec committed
82
    # simple weight
Jiri Borovec committed
83
    labels = np.unique(atlas).tolist()
Jiri Borovec committed
84
    # logging.debug('weights image by atlas with labels: {}'.format(lbs))
Jiri Borovec committed
85 86 87
    if 0 in labels:
        labels.remove(0)
    weight = [0] * np.max(atlas)
Jiri Borovec committed
88 89
    for lb in labels:
        total = np.sum(atlas == lb)
90 91 92 93
        nequal = np.sum(abs(1 - img[atlas == lb]))
        score = total / float(nequal) - 1.
        # equal = np.sum(img[atlas == lb])
        # score = equal / float(total)
94
        if score >= threshold:
Jiri Borovec committed
95
            weight[lb - 1] = 1
Jiri Borovec committed
96 97 98
    return weight


Jiri Borovec committed
99
def weights_label_atlas_overlap_threshold(imgs, atlas, label, threshold=1e-3):
Jiri Borovec committed
100 101 102 103 104
    """ estimate what patterns are activated  with given atlas and input image
    compute overlap matrix and eval nr of overlapping and non pixels and threshold

    :param imgs: [np.array<w, h>]
    :param atlas: np.array<w, h>
Jiri Borovec committed
105 106
    :param label: int
    :param threshold: float, represent the ration between overlapping and non pixels
Jiri Borovec committed
107 108 109 110
    :return: np.array<nb_imgs> of values {0, 1}
    """
    weight = [0] * len(imgs)
    for i, img in enumerate(imgs):
Jiri Borovec committed
111 112
        equal = np.sum(img[atlas == label])
        total = np.sum(atlas == label)
Jiri Borovec committed
113
        score = equal / float(total)
Jiri Borovec committed
114
        if score >= threshold:
Jiri Borovec committed
115 116 117
            weight[i] = 1
    return np.array(weight)