Commit d09ea5ae authored by Jirka's avatar Jirka

pep8

parent 2dac32d9
This diff is collapsed.
import numpy as np
import logging
logger = logging.getLogger(__name__)
def convert_weights_binary2indexes(weights):
logger.debug('convert binary weights {} '
'to list of indexes with True'.format(weights.shape))
# if type(weights)==np.ndarray: weights = weights.tolist()
w_idx = [None] * weights.shape[0]
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]
w_idx[i] = vec +1
# idxs = np.where(weights == 1)
# for i in range(weights.shape[0]):
# w_idx[i] = idxs[1][idxs[0]==i] +1
return w_idx
def weighs_image_atlas_overlap_major(img, atlas):
# logger.debug('weights input image according given atlas')
weights = weighs_image_atlas_overlap_threshold(img, atlas, 0.5)
return weights
def weighs_image_atlas_overlap_partial(img, atlas):
# logger.debug('weights input image according given atlas')
lbs = np.unique(atlas).tolist()
weights = weighs_image_atlas_overlap_threshold(img, atlas, (1. / len(lbs)))
return weights
def weighs_image_atlas_overlap_threshold(img, atlas, thr=0.5):
# logger.debug('weights input image according given atlas')
# simple weight
lbs = np.unique(atlas).tolist()
# logger.debug('weights image by atlas with labels: {}'.format(lbs))
if 0 in lbs: lbs.remove(0)
weight = [0] * np.max(lbs)
for l in lbs:
equal = np.sum(img[atlas==l])
total = np.sum(atlas==l)
score = equal / float(total)
if score >= thr:
weight[l-1] = 1
return weight
def fill_empty_patterns(w_bin):
sums = np.sum(w_bin, axis=0)
logger.debug('IN > sum over weights: {}'.format(sums))
for i, v in enumerate(sums):
if v == 0:
w_bin[:, i] = 1
logger.debug('OUT < sum over weights: {}'.format(np.sum(w_bin, axis=0)))
return w_bin
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
import numpy as np
import logging
logger = logging.getLogger(__name__)
def convert_weights_binary2indexes(weights):
logger.debug('convert binary weights {} '
'to list of indexes with True'.format(weights.shape))
# if type(weights)==np.ndarray: weights = weights.tolist()
w_idx = [None] * weights.shape[0]
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]
w_idx[i] = vec +1
# idxs = np.where(weights == 1)
# for i in range(weights.shape[0]):
# w_idx[i] = idxs[1][idxs[0]==i] +1
return w_idx
def weighs_image_atlas_overlap_major(img, atlas):
# logger.debug('weights input image according given atlas')
weights = weighs_image_atlas_overlap_threshold(img, atlas, 0.5)
return weights
def weighs_image_atlas_overlap_partial(img, atlas):
# logger.debug('weights input image according given atlas')
lbs = np.unique(atlas).tolist()
weights = weighs_image_atlas_overlap_threshold(img, atlas, (1. / len(lbs)))
return weights
def weighs_image_atlas_overlap_threshold(img, atlas, thr=0.5):
# logger.debug('weights input image according given atlas')
# simple weight
lbs = np.unique(atlas).tolist()
# logger.debug('weights image by atlas with labels: {}'.format(lbs))
if 0 in lbs: lbs.remove(0)
weight = [0] * np.max(lbs)
for l in lbs:
equal = np.sum(img[atlas==l])
total = np.sum(atlas==l)
score = equal / float(total)
if score >= thr:
weight[l-1] = 1
return weight
def fill_empty_patterns(w_bin):
sums = np.sum(w_bin, axis=0)
logger.debug('IN > sum over weights: {}'.format(sums))
for i, v in enumerate(sums):
if v == 0:
w_bin[:, i] = 1
logger.debug('OUT < sum over weights: {}'.format(np.sum(w_bin, axis=0)))
return w_bin
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger.info('DONE')
\ No newline at end of file
import numpy as np
from sklearn import metrics
import logging
logger = logging.getLogger(__name__)
def compare_atlas_rnd_pairs(a1, a2):
logger.debug('comparing two atlases '
'of shapes {} <-> {}'.format(a1.shape, a2.shape))
assert np.array_equal(a1.shape, a2.shape)
# assert A1.shape[0]==A2.shape[0] and A1.shape[1]==A2.shape[1]
logger.debug('unique labels are {} and {}'.format(np.unique(a1).tolist(), np.unique(a2).tolist()))
X, Y = np.meshgrid(range(a1.shape[0]), range(a1.shape[1]))
vec_x, vec_y = X.flatten(), Y.flatten()
vec_x_perm = np.random.permutation(vec_x)
vec_y_perm = np.random.permutation(vec_y)
diffs = 0
for x1, y1, x2, y2 in zip(vec_x, vec_y, vec_x_perm, vec_y_perm):
b1 = a1[x1, y1] == a1[x2, y2]
b2 = a2[x1, y1] == a2[x2, y2]
if not b1 == b2: # T&F or F&T
# logger.debug('coords [{},{}], [{},{}] gives vals: {}, {}'.format(x1, y1, x2, y2, b1, b2))
# logger.debug('diff: {}?={} and {}?={}'.format(A1[x1,y1], A1[x2,y2], A2[x1,y1], A2[x2,y2]))
diffs += 1
res = diffs / float(len(vec_x))
return res
# Rand index, o tom uz jsme mluvili.
# Ted jde jen o to, jak tento index vypocitat efektivne. Coz, kdyz si
# rozmyslite, neni prilis tezke: udelate kontingencni tabulku=histogram, tedy oznacime n_ij kolik pixelu na stejnych
# souradnicich patri do tridy 'i'v prvnim obrazku a do tridy 'j' v druhem. Pak pocet shod(dva prvky jsou ve strejne
# tride v prvnim obrazku a ve stejne tride v druhem, nebo jsou v ruznych tridach v prvnim i v druhem) lze vyjadrit
# z histogramu - viz vzorec(1), clanek http://link.springer.com/article/10.1007%2FBF01908075
#
# Existuje i Adjusted Random Index, viz stejny clanek, nebo taky https://en.wikipedia.org/wiki/Rand_index
# Pro nase ucely je to asi jedno, proto budeme chtit jen porovnavat metody mezi sebou.
def compare_atlas_adjusted_rand(a1, a2):
assert np.array_equal(a1.shape, a2.shape)
ars = metrics.adjusted_rand_score(a1.ravel(), a2.ravel())
res = 0.5 - (ars / 2.)
return res
def overlap_matrix_mlabel_segm(seg1, seg2):
logger.debug('computing overlap of two segm '
'of shapes {} <-> {}'.format(seg1.shape, seg2.shape))
assert np.array_equal(seg1.shape, seg2.shape)
u_lb1 = np.unique(seg1)
u_lb2 = np.unique(seg2)
u_lb1 = dict(zip(u_lb1,range(len(u_lb1))))
u_lb2 = dict(zip(u_lb2,range(len(u_lb2))))
logger.debug('unique labels:\n {}\n {}'.format(u_lb1, u_lb2))
res = np.zeros([len(u_lb1), len(u_lb2)])
for i in range(seg1.shape[0]):
for j in range(seg1.shape[1]):
u1, u2 = u_lb1[seg1[i, j]], u_lb2[seg2[i, j]]
res[u1, u2] += 1
res[u2, u1] += 1
# logger.debug(res)
return res
def compare_matrices(m1, m2):
assert np.array_equal(m1.shape, m2.shape)
diff = np.sum(abs(m1 - m2))
return diff / float(np.product(m1.shape))
def compare_weights(c1, c2):
return compare_matrices(c1, c2)
def test_atlases():
logger.info('testing METRIC')
a = np.random.randint(0,4,(5,5))
a2 = a.copy()
a2[a2==0] = -1
b = np.random.randint(0,4,(5,5))
logger.debug('compare_atlas_rnd_pairs, a <-> a: {}'.format(compare_atlas_rnd_pairs(a, a2)))
logger.debug('compare_atlas_rnd_pairs, a <-> b: {}'.format(compare_atlas_rnd_pairs(a, b)))
logger.debug('compare_atlas_adjusted_rand, a <-> a: {}'.format(compare_atlas_adjusted_rand(a, a2)))
logger.debug('compare_atlas_adjusted_rand, a <-> b: {}'.format(compare_atlas_adjusted_rand(a, b)))
return None
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test_atlases()
import numpy as np
from sklearn import metrics
import logging
logger = logging.getLogger(__name__)
def compare_atlas_rnd_pairs(a1, a2):
logger.debug('comparing two atlases '
'of shapes {} <-> {}'.format(a1.shape, a2.shape))
assert np.array_equal(a1.shape, a2.shape)
# assert A1.shape[0]==A2.shape[0] and A1.shape[1]==A2.shape[1]
logger.debug('unique labels are {} and {}'.format(np.unique(a1).tolist(), np.unique(a2).tolist()))
X, Y = np.meshgrid(range(a1.shape[0]), range(a1.shape[1]))
vec_x, vec_y = X.flatten(), Y.flatten()
vec_x_perm = np.random.permutation(vec_x)
vec_y_perm = np.random.permutation(vec_y)
diffs = 0
for x1, y1, x2, y2 in zip(vec_x, vec_y, vec_x_perm, vec_y_perm):
b1 = a1[x1, y1] == a1[x2, y2]
b2 = a2[x1, y1] == a2[x2, y2]
if not b1 == b2: # T&F or F&T
# logger.debug('coords [{},{}], [{},{}] gives vals: {}, {}'.format(x1, y1, x2, y2, b1, b2))
# logger.debug('diff: {}?={} and {}?={}'.format(A1[x1,y1], A1[x2,y2], A2[x1,y1], A2[x2,y2]))
diffs += 1
res = diffs / float(len(vec_x))
return res
# Rand index, o tom uz jsme mluvili.
# Ted jde jen o to, jak tento index vypocitat efektivne. Coz, kdyz si
# rozmyslite, neni prilis tezke: udelate kontingencni tabulku=histogram, tedy oznacime n_ij kolik pixelu na stejnych
# souradnicich patri do tridy 'i'v prvnim obrazku a do tridy 'j' v druhem. Pak pocet shod(dva prvky jsou ve strejne
# tride v prvnim obrazku a ve stejne tride v druhem, nebo jsou v ruznych tridach v prvnim i v druhem) lze vyjadrit
# z histogramu - viz vzorec(1), clanek http://link.springer.com/article/10.1007%2FBF01908075
#
# Existuje i Adjusted Random Index, viz stejny clanek, nebo taky https://en.wikipedia.org/wiki/Rand_index
# Pro nase ucely je to asi jedno, proto budeme chtit jen porovnavat metody mezi sebou.
def compare_atlas_adjusted_rand(a1, a2):
assert np.array_equal(a1.shape, a2.shape)
ars = metrics.adjusted_rand_score(a1.ravel(), a2.ravel())
res = 0.5 - (ars / 2.)
return res
def overlap_matrix_mlabel_segm(seg1, seg2):
logger.debug('computing overlap of two segm '
'of shapes {} <-> {}'.format(seg1.shape, seg2.shape))
assert np.array_equal(seg1.shape, seg2.shape)
u_lb1 = np.unique(seg1)
u_lb2 = np.unique(seg2)
u_lb1 = dict(zip(u_lb1,range(len(u_lb1))))
u_lb2 = dict(zip(u_lb2,range(len(u_lb2))))
logger.debug('unique labels:\n {}\n {}'.format(u_lb1, u_lb2))
res = np.zeros([len(u_lb1), len(u_lb2)])
for i in range(seg1.shape[0]):
for j in range(seg1.shape[1]):
u1, u2 = u_lb1[seg1[i, j]], u_lb2[seg2[i, j]]
res[u1, u2] += 1
res[u2, u1] += 1
# logger.debug(res)
return res
def compare_matrices(m1, m2):
assert np.array_equal(m1.shape, m2.shape)
diff = np.sum(abs(m1 - m2))
return diff / float(np.product(m1.shape))
def compare_weights(c1, c2):
return compare_matrices(c1, c2)
def test_atlases():
logger.info('testing METRIC')
a = np.random.randint(0,4,(5,5))
a2 = a.copy()
a2[a2==0] = -1
b = np.random.randint(0,4,(5,5))
logger.debug('compare_atlas_rnd_pairs, a <-> a: {}'.format(compare_atlas_rnd_pairs(a, a2)))
logger.debug('compare_atlas_rnd_pairs, a <-> b: {}'.format(compare_atlas_rnd_pairs(a, b)))
logger.debug('compare_atlas_adjusted_rand, a <-> a: {}'.format(compare_atlas_adjusted_rand(a, a2)))
logger.debug('compare_atlas_adjusted_rand, a <-> b: {}'.format(compare_atlas_adjusted_rand(a, b)))
return None
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test_atlases()
logger.info('DONE')
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment