def uniform_weighting(self, n_regions=5, perc=95): from numpy import union1d as union from numpy import intersect1d as intersect u, s = self.u, self.s u_b = np.linspace(0, np.percentile(u, perc), n_regions) s_b = np.linspace(0, np.percentile(s, perc), n_regions) regions, weights = {}, np.ones(len(u)) for i in range(n_regions): if i == 0: region = intersect(np.where(u < u_b[i + 1]), np.where(s < s_b[i + 1])) elif i < n_regions - 1: lower_cut = union(np.where(u > u_b[i]), np.where(s > s_b[i])) upper_cut = intersect(np.where(u < u_b[i + 1]), np.where(s < s_b[i + 1])) region = intersect(lower_cut, upper_cut) else: region = union( np.where(u > u_b[i]), np.where(s > s_b[i])) # lower_cut for last region regions[i] = region if len(region) > 0: weights[region] = n_regions / len(region) # set weights accordingly such that each region has an equal overall contribution. self.weights = weights * len(u) / np.sum(weights) self.u_b, self.s_b = u_b, s_b
def compute_dice_coef(seg1, seg2, labelset=None): if labelset is None: lbset = np.union(np.unique(seg1), np.unique(seg2)) else: lbset = np.asarray(labelset, dtype=int) seg1.flat[~np.in1d(seg1, lbset)] = -1 seg2.flat[~np.in1d(seg2, lbset)] = -1 dicecoef = {} for label in lbset: l1 = (seg1==label) l2 = (seg2==label) d = 2*np.sum(l1&l2)/(1e-9 + np.sum(l1) + np.sum(l2)) dicecoef[label] = d return dicecoef
def compute_dice_coef(seg1, seg2, labelset=None): if labelset is None: lbset = np.union(np.unique(seg1), np.unique(seg2)) else: lbset = np.asarray(labelset, dtype=int) seg1.flat[~np.in1d(seg1, lbset)] = -1 seg2.flat[~np.in1d(seg2, lbset)] = -1 dicecoef = {} for label in lbset: l1 = (seg1 == label) l2 = (seg2 == label) d = 2 * np.sum(l1 & l2) / (1e-9 + np.sum(l1) + np.sum(l2)) dicecoef[label] = d return dicecoef
def aprioriGen(freqSets, k): # generate candidate 2-itemsets if k == 2: Ck = np.array( [list(comb) for comb in itertools.combinations(freqSets, 2)]) else: # generate candidate k-itemsets (k > 2) Ck = [] for i in range(0, np.shape(freqSets)[1]): for j in range(i + 1, np.shape(freqSets)[1]): # Merge a pair of (k-1) frequent itemsets if k-2 items are identical using Fk-1 x Fk-1 method L1 = np.sort(freqSets[i, 0:k - 2]) L2 = np.sort(freqSets[j, 0:k - 2]) if np.isequal(L1, L2): Ck = np.concatenate( (Ck, np.union(freqSets[i, :], freqSets[j, :])), axis=0) return (Ck)
def compute_dice_per_slice(seg1, seg2, labelset=None, axis=0): if labelset is None: lbset = np.union(np.unique(seg1), np.unique(seg2)) else: lbset = np.asarray(labelset, dtype=int) seg1.flat[~np.in1d(seg1, lbset)] = -1 seg2.flat[~np.in1d(seg2, lbset)] = -2 nslice = seg1.shape[axis] s = [slice(None) for d in range(seg1.ndim)] dcoefs = {} for i in range(nslice): s[axis] = i s1 = seg1[s] s2 = seg2[s] n1 = np.sum(s1 >= 0) n2 = np.sum(s2 >= 0) if n1 == 0 and n2 == 0: continue d = 2. * np.sum(s1 == s2) / float(n1 + n2) dcoefs[i] = d return dcoefs
def compute_dice_per_slice(seg1, seg2, labelset=None, axis=0): if labelset is None: lbset = np.union(np.unique(seg1), np.unique(seg2)) else: lbset = np.asarray(labelset, dtype=int) seg1.flat[~np.in1d(seg1, lbset)] = -1 seg2.flat[~np.in1d(seg2, lbset)] = -2 nslice = seg1.shape[axis] s = [slice(None) for d in range(seg1.ndim)] dcoefs = {} for i in range(nslice): s[axis] = i s1 = seg1[s] s2 = seg2[s] n1 = np.sum(s1>=0) n2 = np.sum(s2>=0) if n1==0 and n2==0: continue d = 2.*np.sum(s1==s2)/float(n1 + n2) dcoefs[i] = d return dcoefs