Skip to content

Commit

Permalink
generalized unit_test.py to systems_test.py as per shirtsgroup#103
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen Zhong committed Jun 22, 2014
1 parent 63e5ef8 commit 546149a
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 75 deletions.
46 changes: 28 additions & 18 deletions testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,51 @@ Other optional arguments:
-v, --verbose high verbosity, includes DEBUG level output
````

UnitTest.py
systems_test.py
===========

````
$ python UnitTest.py -h
usage: PROG [-h] [--desmond] [--gromacs] [--lammps] [-e] [-d path] [-g path]
[-l path] [-v]
$ python systems_test.py -h
usage: PROG [-h] [--unit] [--stress] [-d] [-g] [-l] [-e] [-dp path] [-gp path]
[-lp path] [-v]
InterMol Unit Testing Script
InterMol Systems Testing Script
--------------------------------
After specifying input type X, this script will convert files
found in ./Inputs/X/UnitTest/ to all file formats. All output
files will be found in ./UnitTestOutput.
After specifying --unit and input type X, this script will
convert files found in ./inputs/X/UnitTest/ to all file formats.
All output files will be found in ./unit_test_output.
After specifying --stress and input type X, this script will
convert files found in ./inputs/X/StressTest/ to all file formats.
All output files will be found in ./stress_test_output.
Additional systems can be tested simply by adding files to the
appropriate path specified above.
optional arguments:
-h, --help show this help message and exit
Run unit test on the following input format(s):
--desmond test conversion of DESMOND files found in
Inputs/Desmond/UnitTest/
--gromacs test conversion of GROMACS files found in
Inputs/Gromacs/UnitTest/
--lammps test conversion of LAMMPS files found in
Inputs/Lammps/UnitTest/
Choose unit tests and/or stress tests:
--unit run unit tests found in inputs/****/UnitTest/
--stress run stress tests found in inputs/****/StressTest/
Choose one or more of the following input format(s):
-d, --desmond test conversion of DESMOND files found in
inputs/Desmond/****Test/
-g, --gromacs test conversion of GROMACS files found in
inputs/Gromacs/****Test/
-l, --lammps test conversion of LAMMPS files found in
inputs/Lammps/****Test/
Other optional arguments:
-e, --energy evaluate energy of input and output files for
comparison
-d path, --despath path
-dp path, --despath path
path for DESMOND binary, needed for energy evaluation
-g path, --gropath path
-gp path, --gropath path
path for GROMACS binary, needed for energy evaluation
-l path, --lmppath path
-lp path, --lmppath path
path for LAMMPS binary, needed for energy evaluation
-v, --verbose print conversion output to console, -v for INFO level,
-vv for DEBUG level
Expand Down
153 changes: 96 additions & 57 deletions testing/unit_test.py → testing/systems_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import pdb
import logging
import convert
# This script runs a unit test on subdirectories found within inputs/*/UnitTest/

DES_IN = './inputs/Desmond/UnitTest'
GRO_IN = './inputs/Gromacs/UnitTest'
LMP_IN = './inputs/Lammps/UnitTest'
OUTPUT_DIR = 'unit_test_output'
UNIT_DES_IN = './inputs/Desmond/UnitTest'
UNIT_GRO_IN = './inputs/Gromacs/UnitTest'
UNIT_LMP_IN = './inputs/Lammps/UnitTest'
UNIT_OUTPUT_DIR = 'unit_test_output'
STRESS_DES_IN = './inputs/Desmond/StressTest'
STRESS_GRO_IN = './inputs/Gromacs/StressTest'
STRESS_LMP_IN = './inputs/Lammps/StressTest'
STRESS_OUTPUT_DIR = 'stress_test_output'
N_FORMATS = 3

# Make a global logging object.
Expand All @@ -23,32 +26,44 @@ def parse_args():
parser = argparse.ArgumentParser(prog='PROG',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''
InterMol Unit Testing Script
InterMol Systems Testing Script
--------------------------------
After specifying input type X, this script will convert files
found in ./inputs/X/UnitTest/ to all file formats. All output files
will be found in ./unit_test_output.
After specifying --unit and input type X, this script will
convert files found in ./inputs/X/UnitTest/ to all file formats.
All output files will be found in ./unit_test_output.
After specifying --stress and input type X, this script will
convert files found in ./inputs/X/StressTest/ to all file formats.
All output files will be found in ./stress_test_output.
Additional systems can be tested simply by adding files to the
appropriate path specified above.
''')

group_out = parser.add_argument_group('Run unit test on the following input format(s)')
group_out.add_argument('--desmond', action='store_true',
help='test conversion of DESMOND files found in inputs/Desmond/UnitTest/')
group_out.add_argument('--gromacs', action='store_true',
help='test conversion of GROMACS files found in inputs/Gromacs/UnitTest/')
group_out.add_argument('--lammps', action='store_true',
help='test conversion of LAMMPS files found in inputs/Lammps/UnitTest/')
group_type = parser.add_argument_group('Choose unit tests and/or stress tests')
group_type.add_argument('--unit', action='store_true',
help='run unit tests found in inputs/****/UnitTest/')
group_type.add_argument('--stress', action='store_true',
help='run stress tests found in inputs/****/StressTest/')

group_in = parser.add_argument_group('Choose one or more of the following input format(s)')
group_in.add_argument('-d', '--desmond', action='store_true',
help='test conversion of DESMOND files found in inputs/Desmond/****Test/')
group_in.add_argument('-g', '--gromacs', action='store_true',
help='test conversion of GROMACS files found in inputs/Gromacs/****Test/')
group_in.add_argument('-l', '--lammps', action='store_true',
help='test conversion of LAMMPS files found in inputs/Lammps/****Test/')

group_misc = parser.add_argument_group('Other optional arguments')
group_misc.add_argument('-e', '--energy', dest='energy', action='store_true',
help='evaluate energy of input and output files for comparison')
group_misc.add_argument('-d', '--despath', dest='despath',
group_misc.add_argument('-dp', '--despath', dest='despath',
metavar='path', default='',
help='path for DESMOND binary, needed for energy evaluation')
group_misc.add_argument('-g', '--gropath', dest='gropath',
group_misc.add_argument('-gp', '--gropath', dest='gropath',
metavar='path', default='',
help='path for GROMACS binary, needed for energy evaluation')
group_misc.add_argument('-l', '--lmppath', dest='lmppath',
group_misc.add_argument('-lp', '--lmppath', dest='lmppath',
metavar='path', default='lmp_openmpi',
help='path for LAMMPS binary, needed for energy evaluation')
group_misc.add_argument('-v', '--verbose', dest='verbose', action='count',
Expand Down Expand Up @@ -80,17 +95,17 @@ def add_handler(dir):
return:
h1, h2 -- FileHandlers so that they can be removed later
h1: logs all >=INFO-level messages to dir/UnitTestInfo.log
h2: logs all >=DEBUG-level messages to dir/UnitTestDebug.log
h1: logs all >=INFO-level messages to dir/testing_info.log
h2: logs all >=DEBUG-level messages to dir/testing_debug.log
also includes function and line number for each message
'''
h1 = logging.FileHandler('%s/UnitTestInfo.log' % dir, mode='w') # don't append to existing file
h1 = logging.FileHandler('%s/testing_info.log' % dir, mode='w') # don't append to existing file
f1 = logging.Formatter("%(levelname)s %(asctime)s %(message)s", "%Y-%m-%d %H:%M:%S")
h1.setFormatter(f1)
h1.setLevel(logging.INFO)
logger.addHandler(h1)

h2 = logging.FileHandler('%s/UnitTestDebug.log' % dir, mode='w')
h2 = logging.FileHandler('%s/testing_debug.log' % dir, mode='w')
f2 = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s", "%Y-%m-%d %H:%M:%S")
h2.setFormatter(f2)
h2.setLevel(logging.DEBUG)
Expand All @@ -102,12 +117,12 @@ def remove_handler(h1, h2):
logger.removeHandler(h1)
logger.removeHandler(h2)

def test_desmond(args):
files = sorted(glob.glob('%s/*/*.cms' % DES_IN)) # return list of files that match the string
def test_desmond(args, indir, outdir):
files = sorted(glob.glob('%s/*/*.cms' % indir)) # return list of files that match the string
files = [x for x in files if not x.endswith('-out.cms')]
results = []

basedir = '%s/FromDesmond' % OUTPUT_DIR
basedir = '%s/FromDesmond' % outdir
if not os.path.isdir(basedir):
os.mkdir(basedir)

Expand All @@ -130,13 +145,13 @@ def test_desmond(args):
remove_handler(h1, h2)
return files, results

def test_gromacs(args):
gro_files = sorted(glob.glob('%s/*/*.gro' % GRO_IN)) # return list of files that match the string
def test_gromacs(args, indir, outdir):
gro_files = sorted(glob.glob('%s/*/*.gro' % indir)) # return list of files that match the string
gro_files = [x for x in gro_files if not x.endswith('out.gro')]
top_files = sorted(glob.glob('%s/*/*.top' % GRO_IN)) # return list of files that match the string
top_files = sorted(glob.glob('%s/*/*.top' % indir)) # return list of files that match the string
results = []

basedir = '%s/FromGromacs' % OUTPUT_DIR
basedir = '%s/FromGromacs' % outdir
if not os.path.isdir(basedir):
os.mkdir(basedir)

Expand All @@ -160,11 +175,11 @@ def test_gromacs(args):
remove_handler(h1, h2)
return gro_files, results

def test_lammps(args):
files = sorted(glob.glob('%s/*/*.lmp' % LMP_IN)) # return list of files that match the string
def test_lammps(args, indir, outdir):
files = sorted(glob.glob('%s/*/*.lmp' % indir)) # return list of files that match the string
results = []

basedir = '%s/FromLammps' % OUTPUT_DIR
basedir = '%s/FromLammps' % outdir
if not os.path.isdir(basedir):
os.mkdir(basedir)

Expand All @@ -187,40 +202,44 @@ def test_lammps(args):
remove_handler(h1, h2)
return files, results

def summarize_results(input_type, files, results):
def summarize_results(input_type, files, results, outdir):
col1_width = max(len(x) for x in files)
col1_width = max(col1_width, 10) # 10 is length of Input File
col2_width = max(len(str(x)) for x in results)
col2_width = max(col2_width,28) # 28 is length of Status/Potential Energy Diff
col2_width = max(col2_width, 28) # 28 is length of Status/Potential Energy Diff
n = len(files)

des_res = results[::N_FORMATS]
print ''
print '*'*15, 'Results for {0} to Desmond Conversion'.format(input_type), '*'*15
print '{:{}} {:{}}'.format('File', col1_width, 'Status/Potential Energy Diff', col2_width)
print ' '*((col1_width+3+col2_width)/2-20), 'Results for {0} to DESMOND Conversion'.format(input_type.upper())
print '='*(col1_width+3+col2_width)
print '{:{}} {:{}}'.format('Input File', col1_width, 'Status/Potential Energy Diff', col2_width)
print '-'*(col1_width+3+col2_width)
for f,r in zip(files, des_res):
print '{:{}} {:>{}}'.format(f, col1_width, r, col2_width)
print ''

gro_res = results[1:][::N_FORMATS]
print ''
print '*'*15, 'Results for {0} to Gromacs Conversion'.format(input_type), '*'*15
print '{:{}} {:{}}'.format('File', col1_width, 'Status/Potential Energy Diff', col2_width)
print ' '*((col1_width+3+col2_width)/2-20), 'Results for {0} to GROMACS Conversion'.format(input_type.upper())
print '='*(col1_width+3+col2_width)
print '{:{}} {:{}}'.format('Input File', col1_width, 'Status/Potential Energy Diff', col2_width)
print '-'*(col1_width+3+col2_width)
for f,r in zip(files, gro_res):
print '{:{}} {:>{}}'.format(f, col1_width, r, col2_width)
print ''

lmp_res = results[2:][::N_FORMATS]
print ''
print '*'*15, 'Results for {0} to Lammps Conversion'.format(input_type), '*'*15
print '{:{}} {:{}}'.format('File', col1_width, 'Status/Potential Energy Diff', col2_width)
print ' '*((col1_width+3+col2_width)/2-20), 'Results for {0} to LAMMPS Conversion'.format(input_type.upper())
print '='*(col1_width+3+col2_width)
print '{:{}} {:{}}'.format('Input File', col1_width, 'Status/Potential Energy Diff', col2_width)
print '-'*(col1_width+3+col2_width)
for f,r in zip(files, lmp_res):
print '{:{}} {:>{}}'.format(f, col1_width, r, col2_width)
print ''
print 'See %s/From%s/*/UnitTestInfo.log for the standard output of each conversion' % (OUTPUT_DIR, input_type)
print 'See %s/From%s/*/UnitTestDebug.log for a detailed DEBUG-level log of each conversion' % (OUTPUT_DIR, input_type)
print 'See %s/From%s/[system name]/testing_info.log for the standard output of each conversion' % (outdir, input_type)
print 'See %s/From%s/[system name]/testing_debug.log for a detailed DEBUG-level log of each conversion' % (outdir, input_type)
print ''

def main():
Expand All @@ -236,22 +255,42 @@ def main():
h.setFormatter(f)
logger.addHandler(h)

if not os.path.isdir(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
if args.unit:
if not os.path.isdir(UNIT_OUTPUT_DIR):
os.mkdir(UNIT_OUTPUT_DIR)

if args.desmond:
des_input_files, results_des = test_desmond(args, UNIT_DES_IN, UNIT_OUTPUT_DIR)
if args.gromacs:
gro_input_files, results_gro = test_gromacs(args, UNIT_GRO_IN, UNIT_OUTPUT_DIR)
if args.lammps:
lmp_input_files, results_lmp = test_lammps(args, UNIT_LMP_IN, UNIT_OUTPUT_DIR)

if args.desmond:
summarize_results('Desmond', des_input_files, results_des, UNIT_OUTPUT_DIR)
if args.gromacs:
summarize_results('Gromacs', gro_input_files, results_gro, UNIT_OUTPUT_DIR)
if args.lammps:
summarize_results('Lammps', lmp_input_files, results_lmp, UNIT_OUTPUT_DIR)

if args.desmond:
des_input_files, results_des = test_desmond(args)
if args.gromacs:
gro_input_files, results_gro = test_gromacs(args)
if args.lammps:
lmp_input_files, results_lmp = test_lammps(args)
if args.stress:
if not os.path.isdir(STRESS_OUTPUT_DIR):
os.mkdir(STRESS_OUTPUT_DIR)

if args.desmond:
summarize_results('Desmond', des_input_files, results_des)
if args.gromacs:
summarize_results('Gromacs', gro_input_files, results_gro)
if args.lammps:
summarize_results('Lammps', lmp_input_files, results_lmp)
if args.desmond:
des_input_files, results_des = test_desmond(args, STRESS_DES_IN, STRESS_OUTPUT_DIR)
if args.gromacs:
gro_input_files, results_gro = test_gromacs(args, STRESS_GRO_IN, STRESS_OUTPUT_DIR)
if args.lammps:
lmp_input_files, results_lmp = test_lammps(args, STRESS_LMP_IN, STRESS_OUTPUT_DIR)

if args.desmond:
summarize_results('Desmond', des_input_files, results_des, STRESS_OUTPUT_DIR)
if args.gromacs:
summarize_results('Gromacs', gro_input_files, results_gro, STRESS_OUTPUT_DIR)
if args.lammps:
summarize_results('Lammps', lmp_input_files, results_lmp, STRESS_OUTPUT_DIR)


if __name__ == '__main__':
main()

0 comments on commit 546149a

Please sign in to comment.