Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding test #92

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix, the rest are refactoring

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()
Loading