예제 #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 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})
예제 #4
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})
예제 #5
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})
예제 #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 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)
예제 #8
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)
예제 #9
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})
예제 #10
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})
예제 #11
0
 def testParseValuesWithIndexAssigment5_IgnoreUnknown(self):
   """Different variable types."""
   parse_dict = hparam.parse_values(
       'a[0]=5,cc=4,b[1]=true,c[2]=abc,mm=2,d[3]=3.14',
       {'a': int, 'b': bool, 'c': str, 'd': float},
       ignore_unknown=True)
   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})
예제 #12
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})
예제 #13
0
 def testParseValuesWithBadIndexAssigment2(self):
   """Reject if type missing."""
   with self.assertRaisesRegexp(ValueError,
                                r'Unknown hyperparameter type for arr'):
     hparam.parse_values('arr[1]=5', {})
예제 #14
0
 def testParseValuesWithBadIndexAssigment3_IgnoreUnknown(self):
   """Ignore type of the form name[index]."""
   hparam.parse_values('arr[1]=1', {'arr[1]': int}, ignore_unknown=True)
예제 #15
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})
예제 #16
0
 def testParseValuesWithBadIndexAssigment2_IgnoreUnknown(self):
   """Ignore missing type."""
   hparam.parse_values('arr[1]=5', {}, ignore_unknown=True)
예제 #17
0
 def testParseValuesWithBadIndexAssigment2(self):
   """Reject if type missing."""
   with self.assertRaisesRegexp(ValueError,
                                r'Unknown hyperparameter type for arr'):
     hparam.parse_values('arr[1]=5', {})
예제 #18
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)
예제 #19
0
 def testParseValuesWithBadIndexAssigment2_IgnoreUnknown(self):
     """Ignore missing type."""
     hparam.parse_values('arr[1]=5', {}, ignore_unknown=True)
예제 #20
0
 def testParseValuesWithBadIndexAssigment3_IgnoreUnknown(self):
     """Ignore type of the form name[index]."""
     hparam.parse_values('arr[1]=1', {'arr[1]': int}, ignore_unknown=True)
예제 #21
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})
예제 #22
0
 def testParseValuesWithBadIndexAssigment1(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]', {'arr': int})