Example #1
0
    def test_parameterized(self):
        par = Parameterized('parameterized')
        p2 = Parameterized('rbf')
        p2.p1 = Param('lengthscale', np.random.uniform(0.1,.5,3), Exponent())
        p2.link_parameter(p2.p1)
        par.p1 = p2
        par.p2 = Param('linear', np.random.uniform(0.1, .5, 2), Logexp())
        par.link_parameters(par.p1, par.p2)

        par.gradient = 10
        par.randomize()
        pcopy = par.copy()
        self.assertIsInstance(pcopy.constraints, ParameterIndexOperations)
        self.assertIsInstance(pcopy.rbf.constraints, ParameterIndexOperationsView)
        self.assertIs(pcopy.constraints, pcopy.rbf.constraints._param_index_ops)
        self.assertIs(pcopy.constraints, pcopy.rbf.lengthscale.constraints._param_index_ops)
        self.assertListEqual(par.param_array.tolist(), pcopy.param_array.tolist())
        pcopy.gradient = 10 # gradient does not get copied anymore
        self.assertListEqual(par.gradient_full.tolist(), pcopy.gradient_full.tolist())
        self.assertSequenceEqual(str(par), str(pcopy))
        self.assertIsNot(par.param_array, pcopy.param_array)
        self.assertIsNot(par.gradient_full, pcopy.gradient_full)
        with tempfile.TemporaryFile('w+b') as f:
            par.pickle(f)
            f.seek(0)
            pcopy = paramz.load(f)
        self.assertListEqual(par.param_array.tolist(), pcopy.param_array.tolist())
        pcopy.gradient = 10
        np.testing.assert_allclose(par.linear.gradient_full, pcopy.linear.gradient_full)
        np.testing.assert_allclose(pcopy.linear.gradient_full, 10)
        self.assertSequenceEqual(str(par), str(pcopy))
Example #2
0
    def test_parameter_index_operations(self):
        pio = ParameterIndexOperations(dict(test1=np.array([4,3,1,6,4]), test2=np.r_[2:130]))
        piov = ParameterIndexOperationsView(pio, 20, 250)
        #py3 fix
        #self.assertListDictEquals(dict(piov.items()), dict(piov.copy().iteritems()))
        self.assertListDictEquals(dict(piov.items()), dict(piov.copy().items()))

        #py3 fix
        #self.assertListDictEquals(dict(pio.iteritems()), dict(pio.copy().items()))
        self.assertListDictEquals(dict(pio.items()), dict(pio.copy().items()))

        self.assertArrayListEquals(pio.copy().indices(), pio.indices())
        self.assertArrayListEquals(piov.copy().indices(), piov.indices())

        with tempfile.TemporaryFile('w+b') as f:
            pickle.dump(pio, f)
            f.seek(0)
            pio2 = pickle.load(f)
            self.assertListDictEquals(pio._properties, pio2._properties)

        with tempfile.TemporaryFile('w+b') as f:
            pickle.dump(piov, f)
            f.seek(0)
            pio2 = paramz.load(f)
            #py3 fix
            #self.assertListDictEquals(dict(piov.items()), dict(pio2.iteritems()))
            self.assertListDictEquals(dict(piov.items()), dict(pio2.items()))
Example #3
0
    def test_parameter_index_operations(self):
        pio = ParameterIndexOperations(
            dict(test1=np.array([4, 3, 1, 6, 4]), test2=np.r_[2:130]))
        piov = ParameterIndexOperationsView(pio, 20, 250)
        #py3 fix
        #self.assertListDictEquals(dict(piov.items()), dict(piov.copy().iteritems()))
        self.assertListDictEquals(dict(piov.items()),
                                  dict(piov.copy().items()))

        #py3 fix
        #self.assertListDictEquals(dict(pio.iteritems()), dict(pio.copy().items()))
        self.assertListDictEquals(dict(pio.items()), dict(pio.copy().items()))

        self.assertArrayListEquals(pio.copy().indices(), pio.indices())
        self.assertArrayListEquals(piov.copy().indices(), piov.indices())

        with tempfile.TemporaryFile('w+b') as f:
            pickle.dump(pio, f)
            f.seek(0)
            pio2 = pickle.load(f)
            self.assertListDictEquals(pio._properties, pio2._properties)

        with tempfile.TemporaryFile('w+b') as f:
            pickle.dump(piov, f)
            f.seek(0)
            pio2 = paramz.load(f)
            #py3 fix
            #self.assertListDictEquals(dict(piov.items()), dict(pio2.iteritems()))
            self.assertListDictEquals(dict(piov.items()), dict(pio2.items()))
Example #4
0
 def test_observable_array(self):
     obs = ObsAr(np.arange(4*2).reshape(4,2))
     pcopy = obs.copy()
     self.assertListEqual(obs.tolist(), pcopy.tolist())
     tmpfile = ''.join(map(str, np.random.randint(10, size=20)))
     try:
         obs.pickle(tmpfile)
         pcopy = paramz.load(tmpfile)
     except:
         raise
     finally:
         os.remove(tmpfile)
     self.assertListEqual(obs.tolist(), pcopy.tolist())
     self.assertSequenceEqual(str(obs), str(pcopy))
Example #5
0
 def test_param(self):
     param = Param('test', np.arange(4*2).reshape(4,2))
     param[0].constrain_positive()
     param[1].fix()
     pcopy = param.copy()
     self.assertListEqual(param.tolist(), pcopy.tolist())
     self.assertListEqual(str(param).split('\n'), str(pcopy).split('\n'))
     self.assertIsNot(param, pcopy)
     with tempfile.TemporaryFile('w+b') as f:
         pickle.dump(param, f)
         f.seek(0)
         pcopy = paramz.load(f)
     self.assertListEqual(param.tolist(), pcopy.tolist())
     self.assertSequenceEqual(str(param), str(pcopy))
Example #6
0
 def test_observable_array(self):
     obs = ObsAr(np.arange(4 * 2).reshape(4, 2))
     pcopy = obs.copy()
     self.assertListEqual(obs.tolist(), pcopy.tolist())
     tmpfile = ''.join(map(str, np.random.randint(10, size=20)))
     try:
         obs.pickle(tmpfile)
         pcopy = paramz.load(tmpfile)
     except:
         raise
     finally:
         os.remove(tmpfile)
     self.assertListEqual(obs.tolist(), pcopy.tolist())
     self.assertSequenceEqual(str(obs), str(pcopy))
Example #7
0
 def test_param(self):
     param = Param('test', np.arange(4 * 2).reshape(4, 2))
     param[0].constrain_positive()
     param[1].fix()
     pcopy = param.copy()
     self.assertListEqual(param.tolist(), pcopy.tolist())
     self.assertListEqual(str(param).split('\n'), str(pcopy).split('\n'))
     self.assertIsNot(param, pcopy)
     with tempfile.TemporaryFile('w+b') as f:
         pickle.dump(param, f)
         f.seek(0)
         pcopy = paramz.load(f)
     self.assertListEqual(param.tolist(), pcopy.tolist())
     self.assertSequenceEqual(str(param), str(pcopy))
def load(file_or_path):
    """
    Load a previously pickled model, using `m.pickle('path/to/file.pickle)'

    :param file_name: path/to/file.pickle
    """
    # This is the pickling pain when changing _src -> src
    import sys
    import inspect
    sys.modules['GPy.kern._src'] = kern.src
    for name, module in inspect.getmembers(kern.src):
        if not name.startswith('_'):
            sys.modules['GPy.kern._src.{}'.format(name)] = module
    sys.modules['GPy.inference.optimization'] = inference.optimization
    import paramz
    return paramz.load(file_or_path)
Example #9
0
    def test_parameterized(self):
        par = Parameterized('parameterized')
        p2 = Parameterized('rbf')
        p2.p1 = Param('lengthscale', np.random.uniform(0.1, .5, 3), Exponent())
        p2.link_parameter(p2.p1)
        par.p1 = p2
        par.p2 = Param('linear', np.random.uniform(0.1, .5, 2), Logexp())
        par.link_parameters(par.p1, par.p2)

        par.gradient = 10
        par.randomize()
        pcopy = par.copy()
        self.assertIsInstance(pcopy.constraints, ParameterIndexOperations)
        self.assertIsInstance(pcopy.rbf.constraints,
                              ParameterIndexOperationsView)
        self.assertIs(pcopy.constraints,
                      pcopy.rbf.constraints._param_index_ops)
        self.assertIs(pcopy.constraints,
                      pcopy.rbf.lengthscale.constraints._param_index_ops)
        self.assertListEqual(par.param_array.tolist(),
                             pcopy.param_array.tolist())
        pcopy.gradient = 10  # gradient does not get copied anymore
        self.assertListEqual(par.gradient_full.tolist(),
                             pcopy.gradient_full.tolist())
        self.assertSequenceEqual(str(par), str(pcopy))
        self.assertIsNot(par.param_array, pcopy.param_array)
        self.assertIsNot(par.gradient_full, pcopy.gradient_full)
        with tempfile.TemporaryFile('w+b') as f:
            par.pickle(f)
            f.seek(0)
            pcopy = paramz.load(f)
        self.assertListEqual(par.param_array.tolist(),
                             pcopy.param_array.tolist())
        pcopy.gradient = 10
        np.testing.assert_allclose(par.linear.gradient_full,
                                   pcopy.linear.gradient_full)
        np.testing.assert_allclose(pcopy.linear.gradient_full, 10)
        self.assertSequenceEqual(str(par), str(pcopy))