dfm / george

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

Metric attributes not set correctly when passed a Metric instance #127

Open StijnDebackere opened 4 years ago

StijnDebackere commented 4 years ago

The following example fails in both the python2 and python3 versions of george

from george import kernels
from george import metrics

matern =  kernels.Matern32Kernel(4, ndim=2)
m = metrics.Metric(matern.metric)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-e15f1390792f> in <module>
      3 
      4 matern =  kernels.Matern32Kernel(4, ndim=2)
----> 5 m = metrics.Metric(matern.metric)

/usr/local/lib/python3.7/site-packages/george/metrics.py in __init__(self, metric, bounds, ndim, axes, lower)
     37             self.set_parameter_vector(
     38                 metric.get_parameter_vector(include_frozen=True),
---> 39                 include_frozen=True)
     40             self.parameter_bounds = metric.parameter_bounds
     41             self.ndim = metric.ndim

/usr/local/lib/python3.7/site-packages/george/modeling.py in set_parameter_vector(self, vector, include_frozen)
    244 
    245         """
--> 246         v = self.parameter_vector
    247         if include_frozen:
    248             v[:] = vector

/usr/local/lib/python3.7/site-packages/george/modeling.py in parameter_vector(self)
    164     def parameter_vector(self):
    165         """An array of all parameters (including frozen parameters)"""
--> 166         return np.array([getattr(self, k) for k in self.parameter_names])
    167 
    168     @parameter_vector.setter

/usr/local/lib/python3.7/site-packages/george/modeling.py in <listcomp>(.0)
    164     def parameter_vector(self):
    165         """An array of all parameters (including frozen parameters)"""
--> 166         return np.array([getattr(self, k) for k in self.parameter_names])
    167 
    168     @parameter_vector.setter

AttributeError: 'Metric' object has no attribute 'log_M_0_0'

The Model.parameter_vector, which is called in Model.set_parameter_vector(), needs all parameter names to be loaded as attributes, which does not happen when calling Model.set_parameter_vector(). Changing

self.set_parameter_vector(
    metric.get_parameter_vector(include_frozen=True),
    include_frozen=True)

to

self.parameter_vector = metric.get_parameter_vector(include_frozen=True)

in metrics.Metric when metric is a metrics.Metric instance fixes this problem.