Skip to content

Commit

Permalink
Fix rounding test (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: vfisikop <vissarion.fisikopoulos@gmail.com>
  • Loading branch information
vissarion and vfisikop authored Mar 28, 2024
1 parent 1d60180 commit c51f0a5
Showing 1 changed file with 13 additions and 47 deletions.
60 changes: 13 additions & 47 deletions tests/rounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# dingo is part of GeomScale project

# Copyright (c) 2022 Apostolos Chalkis
# Copyright (c) 2022 Vissarion Fisikopoulos
# Copyright (c) 2022-2024 Vissarion Fisikopoulos
# Copyright (c) 2022 Haris Zafeiropoulos

# Licensed under GNU LGPL.3, see LICENCE file
Expand All @@ -13,17 +13,15 @@
from dingo import MetabolicNetwork, PolytopeSampler
from dingo.gurobi_based_implementations import fast_inner_ball

class TestSampling(unittest.TestCase):

def test_rounding_min_ellipsoid(self):
def test_rounding(self, method_str):

input_file_json = os.getcwd() + "/ext_data/e_coli_core.json"
model = MetabolicNetwork.from_json( input_file_json )
sampler = PolytopeSampler(model)

A, b, N, N_shift = sampler.get_polytope()

A_rounded, b_rounded, Tr, Tr_shift = sampler.round_polytope(A, b, method="min_ellipsoid")
A_rounded, b_rounded, Tr, Tr_shift = sampler.round_polytope(A, b, method = method_str)

self.assertTrue( A_rounded.shape[0] == 26 )
self.assertTrue( A_rounded.shape[1] == 24 )
Expand All @@ -32,46 +30,7 @@ def test_rounding_min_ellipsoid(self):
self.assertTrue( N_shift.size == 95 )
self.assertTrue( b_rounded.size == 26 )
self.assertTrue( Tr_shift.size == 24 )


self.assertTrue( N.shape[0] == 95 )
self.assertTrue( N.shape[1] == 24 )

self.assertTrue( Tr.shape[0] == 24 )
self.assertTrue( Tr.shape[1] == 24 )

samples = sampler.sample_from_polytope_no_multiphase(
A_rounded, b_rounded, method = 'billiard_walk', n=1000, burn_in=10, thinning=1
)

temp = np.full((samples.shape[0], samples.shape[1]), Tr_shift)
Tr_samples = Tr.dot(samples.T) + temp.T
all_points_in = True
for i in range(Tr_samples.shape[1]):
if np.any(A.dot(Tr_samples[:,i]) - b > 1e-05):
all_points_in = False
break

self.assertTrue( all_points_in )

def test_rounding_john_position(self):

input_file_json = os.getcwd() + "/ext_data/e_coli_core.json"
model = MetabolicNetwork.from_json( input_file_json )
sampler = PolytopeSampler(model)

A, b, N, N_shift = sampler.get_polytope()

A_rounded, b_rounded, Tr, Tr_shift = sampler.round_polytope(A, b, method="john_position")

self.assertTrue( A_rounded.shape[0] == 26 )
self.assertTrue( A_rounded.shape[1] == 24 )

self.assertTrue( b.size == 26 )
self.assertTrue( N_shift.size == 95 )
self.assertTrue( b_rounded.size == 26 )
self.assertTrue( Tr_shift.size == 24 )


self.assertTrue( N.shape[0] == 95 )
self.assertTrue( N.shape[1] == 24 )
Expand All @@ -83,18 +42,25 @@ def test_rounding_john_position(self):
A_rounded, b_rounded, method = 'billiard_walk', n=1000, burn_in=10, thinning=1
)

temp = np.full((samples.shape[0], samples.shape[1]), Tr_shift)
Tr_samples = Tr.dot(samples.T) + temp.T
Tr_shift = Tr_shift.reshape(Tr_shift.shape[0], 1)
Tr_shift_mat = np.full((samples.shape[0], samples.shape[1]), Tr_shift)
Tr_samples = Tr.dot(samples) + Tr_shift_mat

all_points_in = True
for i in range(Tr_samples.shape[1]):
if np.any(A.dot(Tr_samples[:,i]) - b > 1e-05):
all_points_in = False
break

self.assertTrue( all_points_in )

class TestSampling(unittest.TestCase):

def test_rounding_min_ellipsoid(self):
test_rounding(self, "min_ellipsoid")

def test_rounding_john_position(self):
test_rounding(self, "john_position")

if __name__ == "__main__":
unittest.main()

0 comments on commit c51f0a5

Please sign in to comment.