Mentions légales du service

Skip to content
Snippets Groups Projects
Commit e3e8ac6d authored by hhakim's avatar hhakim
Browse files

Add unit tests for pyfaust.__getitem__()/fancy indexing and fix a bug...

Add unit tests for pyfaust.__getitem__()/fancy indexing and fix a bug occurring for a 1-factor Faust fancy indexing.

The unit tests come to validate what's implemented by 8b3ad335.
This commit is related to issue #32.

Secondarly:
- Remove debug outputs,
- Fix the norm-2 unit test for pyfaust.
parent a127751d
Branches
Tags
No related merge requests found
......@@ -122,13 +122,15 @@ class TestFaustPy(unittest.TestCase):
def testNorm2(self):
print("testNorm2()")
ref_norm = norm(self.mulFactors())
ref_norm = norm(self.mulFactors(),2)
test_norm = self.F.norm(2)
print("ref_norm=", ref_norm, "test_norm=", test_norm)
# TODO: remove this workaround when the supposed bug will be corrected in core lib
if(math.isnan(test_norm) and not math.isnan(ref_norm)):
return
self.assertLessEqual(abs(ref_norm-test_norm)/abs(ref_norm), 0.05)
if(ref_norm == 0.0):
self.assertLessEqual(test_norm, 0.0005)
self.assertLessEqual(abs(ref_norm-test_norm)/ref_norm, 0.05)
def testNorm1(self):
print('test Faust.norm(1)')
......@@ -224,23 +226,57 @@ class TestFaustPy(unittest.TestCase):
self.assertLessEqual(abs(col[i,0]-(prod[i,rand_j]))/prod[i,rand_j],10**-6)
# test that the sliced Faust is consistent with the sliced full matrix
# of the Faust
rand_ii, rand_jj = self.r.randint(0,self.F.shape[0]),self.r.randint(0,self.F.shape[1])
si1 = min(rand_i,rand_ii)
si2 = max(rand_i,rand_ii)
sj1 = min(rand_j,rand_jj)
sj2 = max(rand_j,rand_jj)
sF = self.F[si1:si2,sj1:sj2]
self.assertTrue((sF.todense() == self.F.todense()[si1:si2,sj1:sj2]).all())
rand_ii, rand_jj = \
self.r.randint(rand_i+1,self.F.shape[0]),self.r.randint(rand_j+1,self.F.shape[1])
sF = self.F[rand_i:rand_ii,rand_j:rand_jj]
self.assertTrue((sF.todense() ==
self.F.todense()[rand_i:rand_ii,rand_j:rand_jj]).all())
# test a second slice on sF
rand_i, rand_j = self.r.randint(0,sF.shape[0]-1),self.r.randint(0,sF.shape[1]-1)
rand_ii, rand_jj = \
self.r.randint(rand_i+1,sF.shape[0]),self.r.randint(rand_j+1,sF.shape[1])
si1 = min(rand_i,rand_ii)
si2 = max(rand_i,rand_ii)
sj1 = min(rand_j,rand_jj)
sj2 = max(rand_j,rand_jj)
sF2 = sF[si1:si2,sj1:sj2]
self.assertTrue((sF2.todense() == sF.todense()[si1:si2,sj1:sj2]).all())
sF2 = sF[rand_i:rand_ii,rand_j:rand_jj]
n1 = norm(sF2.todense()-sF.todense()[rand_i:rand_ii,rand_j:rand_jj])
n2 = norm(sF.todense()[rand_i:rand_ii,rand_j:rand_jj])
if(n2 == 0): # avoid nan error
self.assertLessEqual(n1,0.0005)
else:
self.assertLessEqual(n1/n2,0.005)
print("test fancy indexing on rows")
F = self.F
num_inds = self.r.randint(1,F.shape[0])
row_ids = [ self.r.randint(0,F.shape[0]-1) for i in
range(0,num_inds)]
F_rows = F[row_ids,:]
self.assertTrue((F_rows.toarray() == F.toarray()[row_ids,:]).all())
print("test fancy indexing on cols")
F = self.F
num_inds = self.r.randint(1,F.shape[1])
col_ids = [ self.r.randint(0,F.shape[1]-1) for i in
range(0,num_inds)]
F_cols = F[:,col_ids]
self.assertTrue((F_cols.toarray() == F.toarray()[:,col_ids]).all())
print("test fancy indexing on rows with slice on cols")
F = self.F
num_inds = self.r.randint(1,F.shape[0]-1)
col_slice_start = self.r.randint(0,F.shape[1]-1)
col_slice_stop = self.r.randint(col_slice_start+1,F.shape[1])
row_ids = [ self.r.randint(0,F.shape[0]-1) for i in
range(0,num_inds)]
F_rows = F[row_ids,col_slice_start:col_slice_stop]
self.assertLessEqual(norm(F_rows.toarray()-F.toarray()[row_ids,col_slice_start:col_slice_stop])/norm(F.toarray()[row_ids,col_slice_start:col_slice_stop]),
0.005)
print("test fancy indexing on cols with slice on rows")
F = self.F
num_inds = self.r.randint(1,F.shape[1])
col_ids = [ self.r.randint(0,F.shape[1]-1) for i in
range(0,num_inds)]
row_slice_start = self.r.randint(0,F.shape[0]-1)
row_slice_stop = self.r.randint(row_slice_start+1, F.shape[0])
F_cols = F[row_slice_start:row_slice_stop,col_ids]
self.assertTrue((F_cols.toarray() ==
F.toarray()[row_slice_start:row_slice_stop,col_ids]).all())
def testToDense(self):
......
......@@ -223,8 +223,6 @@ namespace Faust {
this->fancy_num_cols = num_cols;
this->fancy_num_rows = num_rows;
this->is_fancy_indexed= true;
cout << "TransformHelper num_rows=" << num_rows << endl;
cout << "TransformHelper num_cols=" << num_cols << endl;
memcpy(this->fancy_indices[0], row_ids, num_rows*sizeof(faust_unsigned_int));
memcpy(this->fancy_indices[1], col_ids, num_cols*sizeof(faust_unsigned_int));
this->transform = make_shared<Transform<FPP,Cpu>>(*eval_fancy_idx_Transform());
......@@ -568,7 +566,7 @@ namespace Faust {
factors.resize(size);
}
else { //only one factor
last_sub_fac = gen_fac->get_cols(this->fancy_indices[1], this->fancy_num_cols);
last_sub_fac = first_sub_fac->get_cols(this->fancy_indices[1], this->fancy_num_cols);
delete first_sub_fac;
factors[0] = last_sub_fac;
factors.resize(1);
......
......@@ -754,7 +754,6 @@ class Faust:
# out_indices checking on the end)
#TODO: check that step == 1 on slices and stop on failure if it is or
# implement negative steps
print("__getitem__(), indices=",indices)
if(indices == Ellipsis): # F[...]
out_indices = [slice(0,F.shape[0]), slice(0, F.shape[1])]
elif(isinstance(indices,int)): # F[i] # a line
......@@ -764,7 +763,7 @@ class Faust:
out_indices = [indices, slice(0, F.shape[1])]
elif(isinstance(indices, list)):
out_indices = [indices, list(range(0,F.shape[1]))]
#TODO: check indices are all integers
#TODO: check indices are all integers lying into F shape
elif(isinstance(indices, tuple)):
if(len(indices) == 2):
out_indices = [0,0]
......@@ -783,7 +782,6 @@ class Faust:
elif(isinstance(indices[0], slice)):
out_indices[0] = indices[0]
elif(isinstance(indices[0], list)):
print("indices[0] is list")
out_indices[0] = indices[0]
else:
raise IndexError("only integers, slices (`:`), ellipsis"
......@@ -797,7 +795,6 @@ class Faust:
elif(isinstance(indices[1], slice)):
out_indices[1] = indices[1]
elif(isinstance(indices[1], list)):
print("indices[1] is list")
out_indices[1] = indices[1]
else:
raise IndexError("only integers, slices (`:`), ellipsis"
......
......@@ -577,10 +577,10 @@ cdef class FaustCore:
col_indices = np.array(indices[1], dtype=np.uint64)
row_indices_view = row_indices
col_indices_view = col_indices
print("FaustCorePy.pyx row_indices=", row_indices, " size=",
row_indices.size)
print("FaustCorePy.pyx row_indices=", col_indices," size=",
col_indices.size)
# print("FaustCorePy.fancy_idx(), row_indices=", row_indices, " size=",
# row_indices.size)
# print("FaustCorePy.fancy_idx(), col_indices=", col_indices," size=",
# col_indices.size)
if(self._isReal):
core.core_faust_dbl = \
self.core_faust_dbl.fancy_idx(&row_indices_view[0], row_indices.size,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment