Пример #1
0
 def testParseValuesWithIndexAssigment1_IgnoreUnknown(self):
     """Assignment to an index position."""
     parse_dict = hparam.parse_values('arr[1]=10,b=5', {'arr': int},
                                      ignore_unknown=True)
     self.assertEqual(len(parse_dict), 1)
     self.assertTrue(isinstance(parse_dict['arr'], dict))
     self.assertDictEqual(parse_dict['arr'], {1: 10})
Пример #2
0
 def testParseValuesWithIndexAssigment2_IgnoreUnknown(self):
     """Assignment to multiple index positions."""
     parse_dict = hparam.parse_values('arr[0]=10,arr[5]=20,foo=bar',
                                      {'arr': int},
                                      ignore_unknown=True)
     self.assertEqual(len(parse_dict), 1)
     self.assertTrue(isinstance(parse_dict['arr'], dict))
     self.assertDictEqual(parse_dict['arr'], {0: 10, 5: 20})
Пример #3
0
 def testParseValuesWithIndexAssigment4(self):
     """Assignment of index positions and scalars."""
     parse_dict = hparam.parse_values('x=10,arr[1]=20,y=30', {
         'x': int,
         'y': int,
         'arr': int
     })
     self.assertEqual(len(parse_dict), 3)
     self.assertTrue(isinstance(parse_dict['arr'], dict))
     self.assertDictEqual(parse_dict['arr'], {1: 20})
     self.assertEqual(parse_dict['x'], 10)
     self.assertEqual(parse_dict['y'], 30)
Пример #4
0
 def testParseValuesWithIndexAssigment3(self):
     """Assignment to index positions in multiple names."""
     parse_dict = hparam.parse_values(
         'arr[0]=10,arr[1]=20,L[5]=100,L[10]=200', {
             'arr': int,
             'L': int
         })
     self.assertEqual(len(parse_dict), 2)
     self.assertTrue(isinstance(parse_dict['arr'], dict))
     self.assertDictEqual(parse_dict['arr'], {0: 10, 1: 20})
     self.assertTrue(isinstance(parse_dict['L'], dict))
     self.assertDictEqual(parse_dict['L'], {5: 100, 10: 200})
Пример #5
0
    def testWithReusedVariables(self):
        with self.assertRaisesRegexp(ValueError,
                                     'Multiple assignments to variable \'x\''):
            hparam.parse_values('x=1,x=1', {'x': int})

        with self.assertRaisesRegexp(
                ValueError, 'Multiple assignments to variable \'arr\''):
            hparam.parse_values('arr=[100,200],arr[0]=10', {'arr': int})

        with self.assertRaisesRegexp(
                ValueError, r'Multiple assignments to variable \'arr\[0\]\''):
            hparam.parse_values('arr[0]=10,arr[0]=20', {'arr': int})

        with self.assertRaisesRegexp(
                ValueError, 'Multiple assignments to variable \'arr\''):
            hparam.parse_values('arr[0]=10,arr=[100]', {'arr': int})
Пример #6
0
 def testParseValuesWithIndexAssigment4_IgnoreUnknown(self):
     """Assignment of index positions and scalars."""
     parse_dict = hparam.parse_values(
         'x=10,foo[0]=bar,arr[1]=20,zzz=78,y=30', {
             'x': int,
             'y': int,
             'arr': int
         },
         ignore_unknown=True)
     self.assertEqual(len(parse_dict), 3)
     self.assertTrue(isinstance(parse_dict['arr'], dict))
     self.assertDictEqual(parse_dict['arr'], {1: 20})
     self.assertEqual(parse_dict['x'], 10)
     self.assertEqual(parse_dict['y'], 30)
Пример #7
0
 def testParseValuesWithIndexAssigment5(self):
     """Different variable types."""
     parse_dict = hparam.parse_values('a[0]=5,b[1]=true,c[2]=abc,d[3]=3.14',
                                      {
                                          'a': int,
                                          'b': bool,
                                          'c': str,
                                          'd': float
                                      })
     self.assertEqual(set(parse_dict.keys()), {'a', 'b', 'c', 'd'})
     self.assertTrue(isinstance(parse_dict['a'], dict))
     self.assertDictEqual(parse_dict['a'], {0: 5})
     self.assertTrue(isinstance(parse_dict['b'], dict))
     self.assertDictEqual(parse_dict['b'], {1: True})
     self.assertTrue(isinstance(parse_dict['c'], dict))
     self.assertDictEqual(parse_dict['c'], {2: 'abc'})
     self.assertTrue(isinstance(parse_dict['d'], dict))
     self.assertDictEqual(parse_dict['d'], {3: 3.14})
Пример #8
0
 def testParseValuesWithBadIndexAssigment3_IgnoreUnknown(self):
     """Ignore type of the form name[index]."""
     hparam.parse_values('arr[1]=1', {'arr[1]': int}, ignore_unknown=True)
Пример #9
0
 def testParseValuesWithBadIndexAssigment3(self):
     """Reject type of the form name[index]."""
     with self.assertRaisesRegexp(ValueError,
                                  'Unknown hyperparameter type for arr'):
         hparam.parse_values('arr[1]=1', {'arr[1]': int})
Пример #10
0
 def testParseValuesWithBadIndexAssigment2_IgnoreUnknown(self):
     """Ignore missing type."""
     hparam.parse_values('arr[1]=5', {}, ignore_unknown=True)
Пример #11
0
 def testParseValuesWithBadIndexAssigment2(self):
     """Reject if type missing."""
     with self.assertRaisesRegexp(ValueError,
                                  r'Unknown hyperparameter type for arr'):
         hparam.parse_values('arr[1]=5', {})
Пример #12
0
 def testParseValuesWithBadIndexAssigment1_IgnoreUnknown(self):
     """Reject assignment of list to variable type."""
     with self.assertRaisesRegexp(ValueError,
                                  r'Assignment of a list to a list index.'):
         hparam.parse_values('arr[1]=[1,2,3],c=8', {'arr': int},
                             ignore_unknown=True)