Repository: ColmTalbot/gwpopulation (language: python)

Issue:
MAINT: use Bilby variable_names
As of Bilby==1.2.0 the `bilby.hyper.model.Model` can use a `variable_names` attribute/property rather than introspection to get the parameters for a specific model.

This allows some of the boilerplate code in the smoothed mass distributions to be removed.


Discussion / hints:

Fix patch:
diff --git a/gwpopulation/models/mass.py b/gwpopulation/models/mass.py
index 59103726..2988f609 100644
--- a/gwpopulation/models/mass.py
+++ b/gwpopulation/models/mass.py
@@ -500,9 +500,10 @@ def variable_names(self):
         vars = getattr(
             self.primary_model,
             "variable_names",
-            inspect.getfullargspec(self.primary_model).args,
+            inspect.getfullargspec(self.primary_model).args[1:],
         )
         vars += ["beta", "delta_m"]
+        vars = set(vars).difference(self.kwargs.keys())
         return vars
 
     @property
diff --git a/priors/bbh_population.prior b/priors/bbh_population.prior
index 59054984..48816ba9 100644
--- a/priors/bbh_population.prior
+++ b/priors/bbh_population.prior
@@ -3,10 +3,10 @@ amax_1 = Uniform(minimum=0, maximum=1, name='amax_1', latex_label='$a_{\max, 1}$
 amax_2 = Uniform(minimum=0, maximum=1, name='amax_2', latex_label='$a_{\max, 2}$')
 sigma_1 = Uniform(minimum=1e-2, maximum=4e0, name='sigma_1', latex_label='$\\sigma_1$')
 sigma_2 = Uniform(minimum=1e-2, maximum=4e0, name='sigma_2', latex_label='$\\sigma_2$')
-alpha_1 = Uniform(minimum=1, maximum=5, name='alpha_1', latex_label='$\\alpha_1$')
-alpha_2 = Uniform(minimum=1, maximum=5, name='alpha_2', latex_label='$\\alpha_2$')
-beta_1 = Uniform(minimum=1, maximum=5, name='beta_1', latex_label='$\\beta_1$')
-beta_2 = Uniform(minimum=1, maximum=5, name='beta_2', latex_label='$\\beta_2$')
+alpha_chi_1 = Uniform(minimum=1, maximum=5, name='alpha_1', latex_label='$\\alpha_1$')
+alpha_chi_2 = Uniform(minimum=1, maximum=5, name='alpha_2', latex_label='$\\alpha_2$')
+beta_chi_1 = Uniform(minimum=1, maximum=5, name='beta_1', latex_label='$\\beta_1$')
+beta_chi_2 = Uniform(minimum=1, maximum=5, name='beta_2', latex_label='$\\beta_2$')
 alpha = Uniform(minimum=-4, maximum=12, name='alpha', latex_label='$\\alpha$')
 mmin = Uniform(minimum=3, maximum=10, name='mmin', latex_label='$m_{\min}$')
 mmax = Uniform(minimum=30, maximum=65, name='mmax', latex_label='$m_{\max}$')
@@ -15,4 +15,5 @@ mpp = Uniform(minimum=20, maximum=60, name='mpp', latex_label='$\mu_{pp}$')
 sigpp = Uniform(minimum=5e-1, maximum=1e1, name='sigpp', latex_label='$\\sigma_{pp}$')
 beta = Uniform(minimum=-4, maximum=12, name='beta', latex_label='$\\beta$')
 delta_m = Uniform(minimum=0, maximum=10, name='delta_m', latex_label='$\delta m$')
+lamb = Uniform(minimum=-5, maximum=10, name="lamb", latex_label="$\\lambda_{z}$")
 rate = LogUniform(minimum=1e-1, maximum=1e3, name='rate', latex_label='$R$')


Test patch:
diff --git a/test/example_test.py b/test/example_test.py
new file mode 100644
index 00000000..7cd81e45
--- /dev/null
+++ b/test/example_test.py
@@ -0,0 +1,55 @@
+import bilby
+import numpy as np
+import pandas as pd
+
+import gwpopulation
+
+
+def test_likelihood_evaluation():
+    np.random.seed(10)
+
+    model = bilby.hyper.model.Model(
+        [
+            gwpopulation.models.mass.SinglePeakSmoothedMassDistribution(),
+            gwpopulation.models.spin.independent_spin_magnitude_beta,
+            gwpopulation.models.spin.independent_spin_orientation_gaussian_isotropic,
+            gwpopulation.models.redshift.PowerLawRedshift(),
+        ]
+    )
+    vt_model = bilby.hyper.model.Model(
+        [
+            gwpopulation.models.mass.SinglePeakSmoothedMassDistribution(),
+            gwpopulation.models.spin.independent_spin_magnitude_beta,
+            gwpopulation.models.spin.independent_spin_orientation_gaussian_isotropic,
+            gwpopulation.models.redshift.PowerLawRedshift(),
+        ]
+    )
+
+    bounds = dict(
+        mass_1=(20, 25),
+        mass_ratio=(0.9, 1),
+        a_1=(0, 1),
+        a_2=(0, 1),
+        cos_tilt_1=(-1, -1),
+        cos_tilt_2=(-1, -1),
+        redshift=(0, 2),
+        prior=(1, 1),
+    )
+    posteriors = [
+        pd.DataFrame(
+            {key: np.random.uniform(*bound, 100) for key, bound in bounds.items()}
+        )
+        for _ in range(10)
+    ]
+    vt_data = {key: np.random.uniform(*bound, 10000) for key, bound in bounds.items()}
+
+    selection = gwpopulation.vt.ResamplingVT(vt_model, vt_data, len(posteriors))
+
+    likelihood = gwpopulation.hyperpe.HyperparameterLikelihood(
+        hyper_prior=model, posteriors=posteriors, selection_function=selection
+    )
+
+    priors = bilby.core.prior.PriorDict("priors/bbh_population.prior")
+
+    likelihood.parameters.update(priors.sample())
+    assert abs(likelihood.log_likelihood_ratio() - 0.1319280773148961) < 0.01