dfm / george

Fast and flexible Gaussian Process regression in Python
http://george.readthedocs.io
MIT License
445 stars 128 forks source link

kerne has no attribute kernel #153

Closed allisony closed 2 years ago

allisony commented 2 years ago

With george installed from source, I can no longer get basic examples to run because of an attribute error. There are a lot of related errors when I run python -m pytest -v tests (one for every available kernel basically). The below code is essentially just copied from one of the tutorials, and worked great with george installed via pip. But now the variable kernel doesn't have a property called kernel. Any ideas?

In [12]: np.random.seed(1234)
    ...: x = 10 * np.sort(np.random.rand(15))
    ...: yerr = 0.2 * np.ones_like(x)
    ...: y = np.sin(x) + yerr * np.random.randn(len(x))
    ...: kernel = np.var(y) * george.kernels.ExpSquaredKernel(0.5)
    ...: gp = george.GP(kernel)
    ...: gp.compute(x, yerr)
    ...: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-7677c3a76d4d> in <module>
      5 kernel = np.var(y) * george.kernels.ExpSquaredKernel(0.5)
      6 gp = george.GP(kernel)
----> 7 gp.compute(x, yerr)

~/Documents/GitHub/george/src/george/gp.py in compute(self, x, yerr, **kwargs)
    329         # Include the white noise term.
    330         yerr = np.sqrt(self._yerr2 + np.exp(self._call_white_noise(self._x)))
--> 331         self.solver.compute(self._x, yerr, **kwargs)
    332 
    333         self._const = -0.5 * (

~/Documents/GitHub/george/src/george/solvers/basic.py in compute(self, x, yerr)
     62         """
     63         # Compute the kernel matrix.
---> 64         K = self.kernel.get_value(x)
     65         K[np.diag_indices_from(K)] += yerr ** 2
     66 

~/Documents/GitHub/george/src/george/kernels.py in get_value(self, x1, x2, diag)
    107                 return self.kernel.value_diagonal(x1, x1)
    108             else:
--> 109                 return self.kernel.value_symmetric(x1)
    110         x2 = np.ascontiguousarray(x2, dtype=np.float64)
    111         if diag:

~/Documents/GitHub/george/src/george/kernels.py in __getattr__(self, name)
     64             if None in self.models:
     65                 return getattr(self.models[None], name)
---> 66         raise AttributeError(name)
     67 
     68     @property

AttributeError: kernel
dfm commented 2 years ago

I can't reproduce this - everything runs fine locally when I install from source even if I add a new kernel definition. I'd recommend trying a fresh environment or at least making sure that you uninstall all previous installations of george first. If that doesn't work, please post exactly the steps you're running to get from a fresh environment to this error and I'll see what I can do.

allisony commented 2 years ago

Ok, uninstalling and reinstalling george from source (0.4.1.dev7+g15205b9), the example code from above works fine. I copied RationalQuadratic.yml into a new file in /george/kernels/ called test.yml and renamed the kernel's name to 'RationalQuadraticKernel2', then ran generate_kernels.py in ipython. (I wanted to make sure the problem wasn't coming from any errors made writing a new kernel) Restarting ipython, george.kernels.RationalQuadraticKernel2 exists, but running the above code (which doesn't use RationalQuadraticKernel2) fails with the exact error as above. It seems running generate_kernels.py with a new .yml file in /george/kernels/ breaks it.

I tested this further by starting a fresh environment (follow along here george_test_AY.pdf or here george_test_AY.txt), not adding a new .yml file or running generate_kernels.py, and confirmed the example works. If I run generate_kernels.py without adding a new .yml file to /george/kernels, restart iPython, try the example again, it works fine with no erros. However after I add test.yml to /george/kernels, run generate_kernels.py, and restart ipython, the error returns. I'm attaching my environment file and test.yml file, both with .txt extensions so I can upload them here, and pasting the exact error message from the fresh environment. george_AY.yml.txt test.yml.txt

In [1]: import numpy as np
   ...: import george
   ...: 
   ...: np.random.seed(1234)
   ...: x = 10 * np.sort(np.random.rand(15))
   ...: yerr = 0.2 * np.ones_like(x)
   ...: y = np.sin(x) + yerr * np.random.randn(len(x))
   ...: kernel = np.var(y) * george.kernels.ExpSquaredKernel(0.5)
   ...: gp = george.GP(kernel)
   ...: gp.compute(x, yerr)
   ...: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [1], in <module>
      8 kernel = np.var(y) * george.kernels.ExpSquaredKernel(0.5)
      9 gp = george.GP(kernel)
---> 10 gp.compute(x, yerr)

File ~/george/src/george/gp.py:331, in GP.compute(self, x, yerr, **kwargs)
    329 # Include the white noise term.
    330 yerr = np.sqrt(self._yerr2 + np.exp(self._call_white_noise(self._x)))
--> 331 self.solver.compute(self._x, yerr, **kwargs)
    333 self._const = -0.5 * (
    334     len(self._x) * np.log(2 * np.pi) + self.solver.log_determinant
    335 )
    336 self.computed = True

File ~/george/src/george/solvers/basic.py:64, in BasicSolver.compute(self, x, yerr)
     52 """
     53 Compute and factorize the covariance matrix.
     54 
   (...)
     61 
     62 """
     63 # Compute the kernel matrix.
---> 64 K = self.kernel.get_value(x)
     65 K[np.diag_indices_from(K)] += yerr ** 2
     67 # Factor the matrix and compute the log-determinant.

File ~/george/src/george/kernels.py:109, in Kernel.get_value(self, x1, x2, diag)
    107         return self.kernel.value_diagonal(x1, x1)
    108     else:
--> 109         return self.kernel.value_symmetric(x1)
    110 x2 = np.ascontiguousarray(x2, dtype=np.float64)
    111 if diag:

File ~/george/src/george/kernels.py:66, in Kernel.__getattr__(self, name)
     64     if None in self.models:
     65         return getattr(self.models[None], name)
---> 66 raise AttributeError(name)

AttributeError: kernel
dfm commented 2 years ago

I can reproduce this given these notes and here's how to fix it. After adding your new kernel file, you'll need to run

python generate_kernels.py  # or from inside ipython is ok too
rm -rf build  # in the root george repo directory
python -m pip install -e .

Your experiment was missing the install step, which is needed to actually compile your new kernel, but I found that the rm step is also necessary if you've previously pip installed from source since it won't re-compile the backend otherwise.

allisony commented 2 years ago

Oh great, thank you so much! It works now!

Majoburo commented 1 year ago

I just experienced the same error and resolution while adding a new kernel. Maybe these steps should be in the new kernels tutorial? Or maybe in some kind of Makefile.