Skip to content

Commit

Permalink
Updated validate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfaeder committed May 23, 2024
1 parent 37c069f commit 50d6c96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ validate/basicModels/*.*dat
validate/basicModels/*.net
validate/basicModels/*.xml
validate/basicModels/*.tsv
validate/basicModels/*.json
validate/basicModels/*.json
test/Issue37/test2.nfevent.json
test/Issue37/test.nfevent.json
test/Issue37/out.gdat
test/Issue37/issue37_test2.species
test/Issue37/issue37_test2.gdat
53 changes: 26 additions & 27 deletions validate/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import sys
import bionetgen

nIterations=15
nfsimPrePath='..'
mfolder='./basicModels'
bngPath = os.path.join(bionetgen.defaults.bng_path, "BNG2.pl")
if os.name == "nt":
nfsimPath = os.path.join('..', 'build', 'NFsim.exe')
nfsimPath = os.path.join(nfsimPrePath, 'build', 'NFsim.exe')
else:
nfsimPath = os.path.join('..', 'build', 'NFsim')
nfsimPath = os.path.join(nfsimPrePath, 'build', 'NFsim')



class ParametrizedTestCase(unittest.TestCase):
Expand Down Expand Up @@ -58,18 +62,15 @@ def loadResults(fileName, split):


class TestNFSimFile(ParametrizedTestCase):


# XXX:ideally this should be done through the console but I'm doing the quick and dirty version right now
def BNGtrajectoryGeneration(self, outputDirectory, fileNumber):
bngFileName = os.path.join(outputDirectory, 'v{0}.bngl'.format(fileNumber))
with open(os.devnull, "w") as fnull:
subprocess.check_call(['perl', bngPath, '-outdir', outputDirectory, bngFileName], stdout=fnull)
subprocess.check_call(['perl', bngPath, '-outdir', outputDirectory, '-log', bngFileName], stdout=fnull)

def NFsimtrajectoryGeneration(self, outputDirectory, fileNumber, runOptions):
runOptions = [x.strip() for x in runOptions.split(' ')]
with open(os.devnull, "w") as fnull:

subprocess.check_call([nfsimPath, '-xml', os.path.join(outputDirectory, 'v{0}.xml'.format(fileNumber)),
'-o', os.path.join(outputDirectory, 'v{0}_nf.gdat'.format(fileNumber))] + runOptions,
stdout=fnull)
Expand All @@ -81,19 +82,19 @@ def loadConfigurationFile(self, outputDirectory, fileNumber):
def test_nfsim(self):
tol = 0.35 # this is the error tolerance when comparing nfsim's run to the ssa where 0.35 = 35%
(modelName, runOptions) = self.loadConfigurationFile(self.param['odir'], self.param['num'])
print('Processing model "{0}"'.format(modelName.strip()))
print(f"Processing model r{self.param['num']}.txt: {modelName.strip()}")
# here we decide if this is a NFsim only run or not
if modelName.startswith("NFSIM ONLY"):
nfDiff = 0
for index in range(self.param['iterations']):
self.BNGtrajectoryGeneration(self.param['odir'], self.param['num'])
self.NFsimtrajectoryGeneration(self.param['odir'], self.param['num'], runOptions)
nfh, nf = loadResults(os.path.join(self.param['odir'], 'v{0}_nf.gdat'.format(self.param['num'])), ' ')
self.BNGtrajectoryGeneration(self.param['odir'], self.param['num'])
self.NFsimtrajectoryGeneration(self.param['odir'], self.param['num'], runOptions)
nfh, nf = loadResults(os.path.join(self.param['odir'], 'v{0}_nf.gdat'.format(self.param['num'])), ' ')
# here we just need to make sure we managed to get here without errors
assert len(nf) > 0
#assert len(nf) > 0
self.assertTrue(len(nf) > 0)
else:
ssaDiff = nfDiff = 0
for index in range(self.param['iterations']):
print(f'Iteration {index+1}')
self.BNGtrajectoryGeneration(self.param['odir'], self.param['num'])
self.NFsimtrajectoryGeneration(self.param['odir'], self.param['num'], runOptions)
odeh, ode = loadResults(os.path.join(self.param['odir'], 'v{0}_ode.gdat'.format(self.param['num'])), ' ')
Expand All @@ -103,17 +104,15 @@ def test_nfsim(self):
#square root difference
ssaDiff += pow(sum(pow(ode[:, 1:] - ssa[:, 1:], 2)), 0.5)
nfDiff += pow(sum(pow(ode[:, 1:] - nf[:, 1:], 2)), 0.5)
#nfDiff += sum(abs(ode[:, 1:] - nf[:, 1:]))

ssaDiff = ssaDiff / self.param['iterations']
nfDiff = nfDiff / self.param['iterations']

#rdiff = (nfDiff - ssaDiff) / nfDiff
rdiff = nfDiff - ssaDiff - (tol * ssaDiff)
# relative difference should be less than 'tol'
for element in rdiff:
# self.assertTrue(element < tol)
self.assertTrue(element <= 0)
rdiff = nfDiff - ssaDiff - (tol * ssaDiff)
# relative difference should be less than 'tol'
bad=np.where(rdiff>0)[0]
if (bad.size>0):
print(f"Sir, the observables {bad+1} did not pass. Trying again")
else:
print("Check passed.")
break
self.assertTrue(bad.size==0)


def getTests(directory):
Expand All @@ -130,12 +129,12 @@ def getTests(directory):
suite = unittest.TestSuite()
if len(sys.argv) > 1:
os.chdir(sys.argv[1])
testFolder = './basicModels'
testFolder = mfolder
tests = getTests(testFolder)
for index in tests:
suite.addTest(ParametrizedTestCase.parametrize(TestNFSimFile, param={'num': index,
'odir': 'basicModels', 'iterations': 15}))
result = unittest.TextTestRunner(verbosity=2).run(suite)
'odir': mfolder, 'iterations': nIterations}))
result = unittest.TextTestRunner(verbosity=1).run(suite)

ret = (list(result.failures) == [] and list(result.errors) == [])
ret = 0 if ret else 1
Expand Down

0 comments on commit 50d6c96

Please sign in to comment.