Adds distribution saving and cl args

This commit is contained in:
LSaldyt
2017-11-12 14:52:46 -07:00
parent 97c9b2eb57
commit 45ca7ff912
2 changed files with 94 additions and 26 deletions

BIN
.distributions Normal file

Binary file not shown.

View File

@ -1,6 +1,10 @@
import unittest import unittest
from pprint import pprint import os.path
import pickle
import argparse
import sys
from pprint import pprint
from copycat import Copycat from copycat import Copycat
# TODO: update test cases to use entropy # TODO: update test cases to use entropy
@ -19,12 +23,10 @@ _chiSquared_table = {
10:18.307 10:18.307
} }
class TestCopycat(unittest.TestCase): class ChiSquaredException(Exception):
pass
def setUp(self): def chi_squared(actual, expected):
self.longMessage = True # new in Python 2.7
def assertProbabilitiesLookRoughlyLike(self, actual, expected, iterations):
answerKeys = set(list(actual.keys()) + list(expected.keys())) answerKeys = set(list(actual.keys()) + list(expected.keys()))
degreesFreedom = len(answerKeys) degreesFreedom = len(answerKeys)
chiSquared = 0 chiSquared = 0
@ -40,17 +42,75 @@ class TestCopycat(unittest.TestCase):
chiSquared += (O - E) ** 2 / E chiSquared += (O - E) ** 2 / E
if chiSquared >= _chiSquared_table[degreesFreedom]: if chiSquared >= _chiSquared_table[degreesFreedom]:
self.fail('Significant difference between expected and actual answer distributions: \n' + raise ChiSquaredException('Significant difference between expected and actual answer distributions: \n' +
'Chi2 value: {} with {} degrees of freedom'.format(chiSquared, degreesFreedom)) 'Chi2 value: {} with {} degrees of freedom'.format(chiSquared, degreesFreedom))
def run_testcase(self, initial, modified, target, iterations, expected): class AnswerDistribution:
def __init__(self, initial, modified, target, iterations, distribution):
self.initial = initial
self.modified = modified
self.target = target
self.iterations = iterations
self.distribution = distribution
def test(self):
print('expected:') print('expected:')
pprint(expected) pprint(self.distribution)
actual = Copycat().run(initial, modified, target, iterations) actual = Copycat().run(self.initial,
self.modified,
self.target,
self.iterations)
print('actual:') print('actual:')
pprint(actual) pprint(actual)
self.assertEqual(sum(a['count'] for a in list(actual.values())), iterations) chi_squared(actual, self.distribution)
self.assertProbabilitiesLookRoughlyLike(actual, expected, iterations)
def generate(self):
self.distribution = Copycat().run(self.initial,
self.modified,
self.target,
self.iterations)
class TestCopycat(unittest.TestCase):
Filename = '.distributions'
def setUp(self):
self.longMessage = True # new in Python 2.7
def generate(self):
print('Generating distributions for new file')
iterations = 30
distributions = [
AnswerDistribution('abc', 'abd', 'efg', iterations, None),
AnswerDistribution('abc', 'abd', 'ijk', iterations, None),
AnswerDistribution('abc', 'abd', 'xyz', iterations, None),
AnswerDistribution('abc', 'abd', 'ijkk', iterations, None),
AnswerDistribution('abc', 'abd', 'mrrjjj', iterations, None)]
for distribution in distributions:
distribution.generate()
with open(TestCopycat.Filename, 'wb') as outfile:
pickle.dump(distributions, outfile)
return distributions
def test(self):
try:
with open(TestCopycat.Filename, 'rb') as infile:
distributions = pickle.load(infile)
except Exception as e:
print(e)
print('Generating due to error..')
distributions = self.generate()
for distribution in distributions:
distribution.test()
'''
def run_testcase(self, initial, modified, target, iterations, expected):
adist = AnswerDistribution(initial, modified, target, iterations, expected)
adist.test()
def test_simple_cases(self): def test_simple_cases(self):
self.run_testcase('abc', 'abd', 'efg', 30, self.run_testcase('abc', 'abd', 'efg', 30,
@ -102,8 +162,7 @@ class TestCopycat(unittest.TestCase):
'count': 11}, 'count': 11},
'mrrkkk': {'avgtemp': 43.709349775080746, 'avgtime': 1376.2, 'count': 10}}) 'mrrkkk': {'avgtemp': 43.709349775080746, 'avgtime': 1376.2, 'count': 10}})
''' # Below are examples of improvements that could be made to copycat.
Below are examples of improvements that could be made to copycat.
def test_elongation(self): def test_elongation(self):
# This isn't remotely what a human would say. # This isn't remotely what a human would say.
@ -137,6 +196,15 @@ class TestCopycat(unittest.TestCase):
}) })
''' '''
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--generate', action='store_true')
parser.add_argument('filename', default='.distributions', nargs='?')
parser.add_argument('unittest_args', default=[], nargs='?')
args = parser.parse_args()
# TODO: Go do something with args.input and args.filename
# Now set the sys.argv to the unittest_args (leaving sys.argv[0] alone)
sys.argv[1:] = args.unittest_args
unittest.main() unittest.main()