Ignore:
Timestamp:
Jul 30, 2019, 6:27:35 PM (5 years ago)
Author:
Murray Heymann <heymann.murray@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
50e8722ce7219d67ee649c39300d30d2e9e90edd
Parents:
f598830d8683f1182349ba48230ab28a08198004
git-author:
Murray Heymann <heymann.murray@gmail.com>2019-07-30 18:27:35+02:00
git-committer:
Murray Heymann <heymann.murray@gmail.com>2019-07-30 18:28:16+02:00
Message:
start migrating to unittest framework
File:
1 edited

Legend:

Unmodified
Added
Removed
  • machine_learning/common/keyword_vector.py

    rf59883 rda8925  
    123123        vec = vec / norm
    124124    return vec
    125 
    126 def test_read_dictionary():
    127     """
    128     Create test for read_dictionary function
    129     """
    130     print("\033[1;32;40mTesting read_dictionary function:\033[1;37;40m")
    131 
    132     print("Non-existant file")
    133     correct = False
    134     try:
    135         read_dictionary("asdfasdf")
    136     except FileNotFoundError:
    137         print("correctly caught non-existant file")
    138         print("\033[1;32;40mpass\033[1;37;40m")
    139         correct = True
    140     if not correct:
    141         print("\033[1;31;40mfail\033[1;37;40m")
    142 
    143     print("Reading default file")
    144     correct = True
    145     try:
    146         read_dictionary()
    147     except FileNotFoundError:
    148         print("Default file for dictionary missing")
    149         print("\033[1;31;40mfail\033[1;37;40m")
    150         correct = False
    151     if correct:
    152         print("\033[1;32;40mpass\033[1;37;40m")
    153     print()
    154     print()
    155 
    156 def test_count_occurances():
    157     """
    158     Create test for count_occurances function
    159     """
    160     print("\033[1;32;40mTesting count_occurances function:\033[1;37;40m")
    161     dic = read_dictionary()
    162     correct = False
    163     try:
    164         vec = count_occurances("asdfasdf", dic)
    165     except FileNotFoundError:
    166         correct = True
    167         print("Correctly raised FileNotFoundError")
    168         print("\033[1;32;40mpass\033[1;37;40m")
    169     if not correct:
    170         print("\033[1;31;40mfail\033[1;37;40m")
    171 
    172     print("Count occurances with None dictionary:")
    173     correct = False
    174     try:
    175         count_occurances("../Singular/table.h", None)
    176     except AssertionError:
    177         print("Correctly caught AssertionError")
    178         print("\033[1;32;40mpass\033[1;37;40m")
    179         correct = True
    180     if not correct:
    181         print("\033[1;31;40mfail\033[1;37;40m")
    182 
    183 
    184     print("Count occurances with empty dictionary:")
    185     correct = False
    186     try:
    187         count_occurances("../Singular/table.h", np.array([]))
    188     except AssertionError:
    189         print("Correctly caught AssertionError")
    190         print("\033[1;32;40mpass\033[1;37;40m")
    191         correct = True
    192     if not correct:
    193         print("\033[1;31;40mfail\033[1;37;40m")
    194 
    195 
    196     print("vector of ../Singular/table.h")
    197     vec = count_occurances("../Singular/table.h", dic)
    198     print(vec)
    199     print()
    200     print()
    201 
    202 def test_create_vector_dictionary():
    203     """
    204     Create test for create_vector_dictionary function
    205     """
    206     print("\033[1;32;40mTesting create_vector_dictionary " \
    207             "function:\033[1;37;40m")
    208     read_dictionary()
    209 
    210     print("Create Vector Dictionary with None as dictionary:")
    211     correct = False
    212     try:
    213         create_vector_dictionary(None)
    214     except AssertionError:
    215         correct = True
    216         print("\033[1;32;40mpass\033[1;37;40m")
    217     if not correct:
    218         print("\033[1;31;40mfail\033[1;37;40m")
    219 
    220     print("Create Vector Dictionary with empty dictionary:")
    221     correct = False
    222     try:
    223         create_vector_dictionary(np.array([]))
    224     except AssertionError:
    225         correct = True
    226         print("\033[1;32;40mpass\033[1;37;40m")
    227     if not correct:
    228         print("\033[1;31;40mfail\033[1;37;40m")
    229 
    230     print()
    231     print()
    232 
    233 def test_vector_distance():
    234     """
    235     Create test for vector_distance function
    236     """
    237     print("\033[1;32;40mTesting vector_distance function:\033[1;37;40m")
    238 
    239     vector1 = np.array([3, 4])
    240     vector1 = normalise_vector(vector1)
    241     vector2 = np.array([4, 3])
    242     vector2 = normalise_vector(vector2)
    243 
    244     print("Distance of vectors of different dimensions:")
    245     correct = False
    246     try:
    247         vector_distance(np.array([1, 2, 3]), vector1)
    248     except AssertionError:
    249         correct = True
    250         print("\033[1;32;40mpass\033[1;37;40m")
    251     if not correct:
    252         print("\033[1;31;40mfail\033[1;37;40m")
    253 
    254 
    255     print("Distance same vector: " + str(vector_distance(vector1, vector1)))
    256     assert vector_distance(vector1, vector1) == 0, \
    257             "distance to same vectorshould be 0"
    258     print("\033[1;32;40mpass\033[1;37;40m")
    259 
    260     print("Distance different vector: " + str(vector_distance(vector1, vector2)))
    261     assert vector_distance(vector1, vector2) > 0, \
    262             "Distance between nonequal vectors should be strictly positive"
    263     print("\033[1;32;40mpass\033[1;37;40m")
    264     print()
    265     print()
    266 
    267 def test_normalise_vector():
    268     """
    269     Create test for normalise_vector function
    270     """
    271     print("\033[1;32;40mTesting normalise_vector function:\033[1;37;40m")
    272     testvector = np.array([3, 4])
    273     testvector = normalise_vector(testvector)
    274     assert np.linalg.norm(testvector) == 1, \
    275             "Normalised vector should have norm of 1"
    276     print("\033[1;32;40mpass\033[1;37;40m")
    277     print("normalised vector: " + str(testvector))
    278     print("\033[1;32;40mpass\033[1;37;40m")
    279 
    280     print("Attempt to normalise the zero vector")
    281     print(normalise_vector(np.array([0, 0, 0, 0, 0])))
    282     print("\033[1;32;40mpass\033[1;37;40m")
    283 
    284     print("Attempt to normalise list")
    285     print(normalise_vector([3, 4, 0, 0, 0]))
    286     print("\033[1;32;40mpass\033[1;37;40m")
    287 
    288     print("Attempt to normalise empty vector")
    289     print(normalise_vector(np.array([])))
    290     print("\033[1;32;40mpass\033[1;37;40m")
    291 
    292     print("Attempt to normalise None")
    293     print(normalise_vector(None))
    294     print("\033[1;32;40mpass\033[1;37;40m")
    295     print()
    296     print()
    297 
    298 
    299 def main():
    300     """
    301     Run some basic tests
    302     """
    303     test_normalise_vector()
    304 
    305     test_vector_distance()
    306 
    307     test_read_dictionary()
    308 
    309     test_count_occurances()
    310 
    311     test_create_vector_dictionary()
    312 
    313 if __name__ == '__main__':
    314     main()
Note: See TracChangeset for help on using the changeset viewer.