def test_compute_jaccard(): sh = (20, 25, 30) S = np.array(range(sh[0]*sh[1]*sh[2]), dtype=np.int32).reshape(sh) A = S % 2 B = (S + 1) % 2 expected = np.array([0.0, 0.0]) actual = np.array(compute_jaccard(A, B)) assert_array_almost_equal(actual, expected) expected = np.array([1.0, 1.0]) actual = np.array(compute_jaccard(A, A)) assert_array_almost_equal(actual, expected) B = S % 3 expected = np.array([0.25, 0.25, 0.0]) actual = np.array(compute_jaccard(A, B))
def compute_jaccard(aname, bname, keep_existing = True): baseA=getBaseFileName(aname) baseB=getBaseFileName(bname) oname="jaccard_"+baseA+"_"+baseB+".txt" if keep_existing and os.path.exists(oname): print('Jaccard overlap found. Skipped computation.') jaccard=np.loadtxt(oname) return jaccard nib_A=nib.load(aname) affineA=nib_A.get_affine() A=nib_A.get_data().squeeze().astype(np.int32) A=np.copy(A, order='C') print("A range:",A.min(), A.max()) nib_B=nib.load(bname) #newB=nib.Nifti1Image(nib_B.get_data(),affineA) #newB.to_filename(bname) B=nib_B.get_data().squeeze().astype(np.int32) B=np.copy(B, order='C') print("B range:",B.min(), B.max()) jaccard=np.array(evaluation.compute_jaccard(A,B)) print("Jaccard range:",jaccard.min(), jaccard.max()) np.savetxt(oname,jaccard) return jaccard