Skip to content

Commit

Permalink
Updated NumCosmo usage due to deprecation of old constructors. (#615)
Browse files Browse the repository at this point in the history
* Updated NumCosmo usage due to deprecation.

* Updated examples and backend to remove the use of deprecated interfaces.
Fixed bug in model update.

* Update comparison notebook

* Update version to 1.11.0

---------

Co-authored-by: Hsin Fan <[email protected]>
Co-authored-by: m-aguena <[email protected]>
  • Loading branch information
3 people authored Feb 20, 2024
1 parent d9de5a3 commit 13b7c8a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 88 deletions.
10 changes: 5 additions & 5 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
## Basic procedure <a name="basic_install"></a>

Here we provide a quick guide for a basic instalation, this will install all the packages in your current environment.
To create a specific conda environment for CLMM, we recommend you to check the begining of section
To create a specific conda environment for CLMM, we recommend you to check the begining of section
[Access to the proper environment on cori.nersc.gov](#access_to_the_proper_environment_on_cori).

### Theory backend installation
First, choose and install a theory backend for CLMM.
This can be CCL (versions between 2.7.1.dev10+gf81b59a4 and 3),
NumCosmo (v0.15 or later),
NumCosmo (v0.19 or later),
or cluster_toolkit and they are installable as follows.

To install CCL as the theory/cosmology backend, run
Expand Down Expand Up @@ -60,7 +60,7 @@ Now, you can install CLMM and its dependencies as
pip install pytest sphinx sphinx_rtd_theme
pip install jupyter # need to have jupyter notebook tied to this environment, you can then see the environment in jupyter.nersc.gov
git clone https://github.com/LSSTDESC/CLMM.git # If you'd like to contribute but don't have edit permissions to the CLMM repo, see below how to fork the repo instead.
cd CLMM
cd CLMM
python setup.py install # build from source
```

Expand All @@ -72,9 +72,9 @@ If you have access to NERSC, this will likely be the easiest to make sure you ha
module load python # Also loads anaconda
conda create --name clmmenv # Create an anaconda environment for clmm
source activate clmmenv # switch to your newly created environment
conda install pip # need pip to install everything else necessary for clmm
conda install pip # need pip to install everything else necessary for clmm
conda install ipython # need to have the ipython tied to this environment
conda install -c conda-forge firefox # Need a browser to view jupyter notebooks
conda install -c conda-forge firefox # Need a browser to view jupyter notebooks
```

Note, for regular contributions and use, we recommend adding `module load python` to your `~/.bashrc` so you have anaconda installed every time you log in. You will subseqeuntly also want to be in the correct environment whenever working with `clmm`, which means running `source activate clmmenv` at the start of each session.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CLMM requires Python version 3.8 or later. CLMM has the following dependencies:

- [NumPy](https://www.numpy.org/) (v1.17 or later)
- [SciPy](https://scipy.org/) (v1.3 or later)
- [Astropy](https://www.astropy.org/) (v4.0 or later for units and cosmology dependence)
- [Astropy](https://www.astropy.org/) (v4.0 or later for units and cosmology dependence)
(Please avoid Astropy v5.0 since there is bug breaking CCL backend. It has been fixed in Astropy v5.0.1.)
- [Matplotlib](https://matplotlib.org/) (for plotting and going through tutorials)

Expand All @@ -36,7 +36,7 @@ For the theoretical predictions of the signal, CLMM relies on existing libraries

- [cluster-toolkit](https://cluster-toolkit.readthedocs.io/en/latest/)
- [CCL](https://ccl.readthedocs.io/en/latest/) (versions between 2.7.1.dev10+gf81b59a4 and 3)
- [NumCosmo](https://numcosmo.github.io/) (v0.15 or later)
- [NumCosmo](https://numcosmo.github.io/) (v0.19 or later)


(See the [INSTALL documentation](INSTALL.md) for more detailed installation instructions.)
Expand Down Expand Up @@ -91,8 +91,8 @@ non-DESC project concept and progress to be presented to the working group,
so working group members can help co-identify tools and/or ongoing development
that might mutually benefit your non-DESC project and ongoing DESC projects.

**External Projects by Non-DESC members**: If you are not from the DESC
community, you are also welcome to contact CLMM Topical Team leads to introduce
**External Projects by Non-DESC members**: If you are not from the DESC
community, you are also welcome to contact CLMM Topical Team leads to introduce
your project and share feedback.


Expand Down
2 changes: 1 addition & 1 deletion clmm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
)
from . import support

__version__ = "1.10.0"
__version__ = "1.11.0"
8 changes: 7 additions & 1 deletion clmm/cosmology/numcosmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class NumCosmoCosmology(CLMMCosmology):

def __init__(self, dist=None, dist_zmax=15.0, **kwargs):
self.dist = None
self.smd = None

super().__init__(**kwargs)

Expand All @@ -54,6 +55,7 @@ def __init__(self, dist=None, dist_zmax=15.0, **kwargs):
self.smd = Nc.WLSurfaceMassDensity.new(self.dist)
self.smd.prepare_if_needed(self.be_cosmo)


def _init_from_cosmo(self, be_cosmo):
assert isinstance(be_cosmo, Nc.HICosmo)
assert isinstance(be_cosmo, Nc.HICosmoDECpl)
Expand All @@ -63,7 +65,7 @@ def _init_from_cosmo(self, be_cosmo):

def _init_from_params(self, H0, Omega_b0, Omega_dm0, Omega_k0):
# pylint: disable=arguments-differ
self.be_cosmo = Nc.HICosmo.new_from_name(Nc.HICosmo, "NcHICosmoDECpl{'massnu-length':<1>}")
self.be_cosmo = Nc.HICosmoDECpl(massnu_length=1)
self.be_cosmo.omega_x2omega_k()
self.be_cosmo.param_set_by_name("w0", -1.0)
self.be_cosmo.param_set_by_name("w1", 0.0)
Expand Down Expand Up @@ -136,6 +138,10 @@ def _update_vec_funcs(self):
self._eval_sigma_crit_core = np.vectorize(
lambda z_len, z_src: (self.smd.sigma_critical(self.be_cosmo, z_src, z_len, z_len))
)
if self.dist is not None:
self.dist.prepare_if_needed(self.be_cosmo)
if self.smd is not None:
self.smd.prepare_if_needed(self.be_cosmo)

def _get_rho_c(self, z):
return (
Expand Down
1 change: 1 addition & 0 deletions clmm/theory/numcosmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def set_mset(self, mset):

self.hdpm = mset.get(Nc.HaloDensityProfile.id())
self.cosmo.smd = mset.get(Nc.WLSurfaceMassDensity.id())
self.cosmo.smd.prepare_if_needed(self.cosmo.be_cosmo)


Cosmology = NumCosmoCosmology
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,20 +585,20 @@
" self.props.z_src = Ncm.Vector.new_array(z_src)\n",
" self.props.r_source = Ncm.Vector.new_array(r_source)\n",
"\n",
" self.y.set_array(gt_profile)\n",
" self.peek_mean().set_array(gt_profile)\n",
"\n",
" self.sigma.set_all(\n",
" self.peek_std().set_all(\n",
" 1.0e-2\n",
" ) # Diagonal covariance matrix: all points have the same standard deviation value\n",
"\n",
" self.set_init(True)\n",
"\n",
" # Once the NcmDataGaussDiag is initialized, its parent class variable np is set with the n_points value.\n",
" def do_get_length(self):\n",
" return self.np\n",
" return self.get_size()\n",
"\n",
" def do_get_dof(self):\n",
" return self.np\n",
" return self.get_size()\n",
"\n",
" def do_begin(self):\n",
" pass\n",
Expand Down Expand Up @@ -686,8 +686,8 @@
"metadata": {},
"outputs": [],
"source": [
"fit1 = Ncm.Fit.new(Ncm.FitType.NLOPT, \"ln-neldermead\", lh1, mset1, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit2 = Ncm.Fit.new(Ncm.FitType.NLOPT, \"ln-neldermead\", lh2, mset2, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit1 = Ncm.Fit.factory(Ncm.FitType.NLOPT, \"ln-neldermead\", lh1, mset1, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit2 = Ncm.Fit.factory(Ncm.FitType.NLOPT, \"ln-neldermead\", lh2, mset2, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"\n",
"fit1.run(Ncm.FitRunMsgs.SIMPLE)\n",
"fit1.fisher()\n",
Expand Down
26 changes: 13 additions & 13 deletions examples/NumCosmo/Example2_Fit_Halo_Mass_to_Shear_Catalog_NC.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -468,20 +468,20 @@
" if z_err:\n",
" self.props.r_source = Ncm.Vector.new_array(z_err)\n",
"\n",
" self.y.set_array(gt_profile)\n",
" self.peek_mean().set_array(gt_profile)\n",
"\n",
" self.sigma.set_array(\n",
" self.peek_std().set_array(\n",
" gt_err\n",
" ) # Diagonal covariance matrix: standard deviation values in gt_err.\n",
"\n",
" self.set_init(True)\n",
"\n",
" # Once the NcmDataGaussDiag is initialized, its parent class variable np is set with the n_points value.\n",
" def do_get_length(self):\n",
" return self.np\n",
" return self.get_size()\n",
"\n",
" def do_get_dof(self):\n",
" return self.np\n",
" return self.get_size()\n",
"\n",
" def do_begin(self):\n",
" pass\n",
Expand Down Expand Up @@ -605,9 +605,9 @@
"metadata": {},
"outputs": [],
"source": [
"fit1 = Ncm.Fit.new(Ncm.FitType.NLOPT, \"ln-neldermead\", lh1, mset1, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit2 = Ncm.Fit.new(Ncm.FitType.NLOPT, \"ln-neldermead\", lh2, mset2, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit3 = Ncm.Fit.new(Ncm.FitType.NLOPT, \"ln-neldermead\", lh3, mset3, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit1 = Ncm.Fit.factory(Ncm.FitType.NLOPT, \"ln-neldermead\", lh1, mset1, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit2 = Ncm.Fit.factory(Ncm.FitType.NLOPT, \"ln-neldermead\", lh2, mset2, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"fit3 = Ncm.Fit.factory(Ncm.FitType.NLOPT, \"ln-neldermead\", lh3, mset3, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
"\n",
"fit1.run(Ncm.FitRunMsgs.SIMPLE)\n",
"fit1.fisher()\n",
Expand Down Expand Up @@ -768,7 +768,7 @@
"init_sampler.set_cov_from_rescale(1.0e-1)\n",
"\n",
"nwalkers = 100 # Number of walkers\n",
"walker = Ncm.FitESMCMCWalkerAPS.new(nwalkers, mset3.fparams_len())\n",
"walker = Ncm.FitESMCMCWalkerAPES.new(nwalkers, mset3.fparams_len())\n",
"\n",
"# Ensemble Sampler MCMC\n",
"esmcmc = Ncm.FitESMCMC.new(fit3, nwalkers, init_sampler, walker, Ncm.FitRunMsgs.SIMPLE)\n",
Expand All @@ -792,7 +792,7 @@
"\n",
"Here, instead of building an object directly on top of NcmDataGauss*, we use NumCosmo's framework to build non-binned likelihood for weak-lensing cluster analysis.\n",
"\n",
"For that we need two objects: a NcGalaxyWLReducedShearGauss that model a Gaussian distributed reduced shear likelihood, here the observables matrix is simply $(r, \\gamma_t, \\sigma_{\\gamma_t})$ for each galaxy. If the data has spectroscopic redshifts then we use NcGalaxyRedshiftSpec with an array of real redshifts. When photometric errors are included we use the NcGalaxyRedshiftGauss object that receives $(z, \\sigma_z)$ for each galaxy. \n",
"For that we need two objects: a NcGalaxyWLEllipticityGauss that model a Gaussian distributed reduced shear likelihood, here the observables matrix is simply $(r, \\gamma_t, \\sigma_{\\gamma_t})$ for each galaxy. If the data has spectroscopic redshifts then we use NcGalaxyRedshiftSpec with an array of real redshifts. When photometric errors are included we use the NcGalaxyRedshiftGauss object that receives $(z, \\sigma_z)$ for each galaxy. \n",
"\n",
"Once we have the data objects ready we can proceed as in the previous examples.\n"
]
Expand All @@ -812,7 +812,7 @@
" sigma_g = 1.0e-4 if not sigma_g else sigma_g\n",
" m_obs = np.column_stack((r, g_t, np.repeat(sigma_g, len(r))))\n",
"\n",
" grsg = Nc.GalaxyWLReducedShearGauss(pos=Nc.GalaxyWLReducedShearGaussPos.R)\n",
" grsg = Nc.GalaxyWLEllipticityGauss(pos=Nc.GalaxyWLEllipticityGaussPos.R)\n",
" grsg.set_obs(Ncm.Matrix.new_array(m_obs.flatten(), 3))\n",
"\n",
" if sigma_z:\n",
Expand All @@ -837,7 +837,7 @@
" for data in data_array:\n",
" dset.append_data(data)\n",
" lh = Ncm.Likelihood.new(dset)\n",
" fit = Ncm.Fit.new(Ncm.FitType.NLOPT, \"ln-neldermead\", lh, mset, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
" fit = Ncm.Fit.factory(Ncm.FitType.NLOPT, \"ln-neldermead\", lh, mset, Ncm.FitGradType.NUMDIFF_FORWARD)\n",
" # fit.set_params_reltol (1.0e-8)\n",
" # fit.set_m2lnL_reltol (1.0e-11)\n",
"\n",
Expand Down Expand Up @@ -948,7 +948,7 @@
"init_sampler.set_cov_from_rescale(1.0e-1)\n",
"\n",
"nwalkers = 100\n",
"stretch = Ncm.FitESMCMCWalkerAPS.new(nwalkers, mset3.fparams_len())\n",
"stretch = Ncm.FitESMCMCWalkerAPES.new(nwalkers, mset3.fparams_len())\n",
"\n",
"esmcmc = Ncm.FitESMCMC.new(fit3, nwalkers, init_sampler, stretch, Ncm.FitRunMsgs.SIMPLE)\n",
"esmcmc.set_data_file(\"example2_fit3_esmcmc_out_aps.fits\")\n",
Expand Down Expand Up @@ -1026,7 +1026,7 @@
"outputs": [],
"source": [
"ser = Ncm.Serialize.new(0)\n",
"data = fit3.lh.dset.get_data(0)\n",
"data = fit3.peek_likelihood().peek_dataset().get_data(0)\n",
"ser.to_file(data, \"example2_fit3_data.obj\")"
]
}
Expand Down
94 changes: 36 additions & 58 deletions examples/NumCosmo/modeling_cmp_numcosmo.ipynb

Large diffs are not rendered by default.

0 comments on commit 13b7c8a

Please sign in to comment.