Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiable cosmo: Cosmology now a full module with optional Phytorch #72

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
4071206
Cosmology now a full module with optional Phytorch
mjyb16 Aug 22, 2023
b1a6ba5
Fixed tests for updated version of cosmology, cosmology is now a full…
mjyb16 Aug 22, 2023
9b8791c
Fixed tests to exclude phytorch for now due to compilation complexity
mjyb16 Aug 22, 2023
2c592b3
Last fix to remove phytorch contamination in tests
mjyb16 Aug 22, 2023
38aa017
adding simplification of imports to remove unecessary base
mjyb16 Aug 23, 2023
31129b9
Changed FLatLambdaCDM to use big H0 instead of little h
mjyb16 Aug 23, 2023
8a89435
Updated cosmology test for big H0
mjyb16 Aug 23, 2023
87bb593
Chased down all the little h
mjyb16 Aug 23, 2023
99f56d8
Trying to add phytorch build to github workflow
mjyb16 Aug 25, 2023
d5f91b9
Trying to add phytorch build to github workflow...changes
mjyb16 Aug 25, 2023
f5e53c6
Trying to add phytorch build to github workflow...added boost preproc…
mjyb16 Aug 25, 2023
685b56a
Trying to add phytorch build to github workflow...more edits
mjyb16 Aug 25, 2023
14bb43d
Trying to add phytorch build to github workflow...still
mjyb16 Aug 25, 2023
8d889dd
Trying to add phytorch build to github workflow...changing boost dir
mjyb16 Aug 25, 2023
e5fd77a
Commenting out the github workflow for now....boost install is failing
mjyb16 Aug 25, 2023
57b199c
Create phytorch_instructions.md
mjyb16 Aug 25, 2023
ea1bfe8
Update README.md
mjyb16 Aug 25, 2023
e14c69a
Update phytorch_instructions.md
mjyb16 Aug 25, 2023
2d13b10
Trying to add phytorch build to github workflow...modifying phytorch
mjyb16 Aug 26, 2023
af60cc3
Trying to add phytorch build to github workflow...modifying boost loc…
mjyb16 Aug 26, 2023
36dfb9f
Update python-app.yml
mjyb16 Aug 26, 2023
33a5676
Update python-app.yml
mjyb16 Aug 26, 2023
f89a51c
Update python-app.yml
mjyb16 Aug 26, 2023
56e0729
Update python-app.yml
mjyb16 Aug 26, 2023
b1d4d5f
Update python-app.yml
mjyb16 Aug 26, 2023
954f44a
Update python-app.yml
mjyb16 Aug 26, 2023
384d030
Update python-app.yml
mjyb16 Aug 26, 2023
291b3da
Update python-app.yml
mjyb16 Aug 26, 2023
3df003a
Debugging python-app.yml
mjyb16 Aug 26, 2023
764949d
Debugging python-app.yml
mjyb16 Aug 26, 2023
bbf3e75
Debugging python-app.yml
mjyb16 Aug 27, 2023
43ab7f0
Debug python-app.yml
mjyb16 Aug 27, 2023
518ec44
Debug python-app.yml
mjyb16 Aug 27, 2023
a957f38
Debug python-app.yml
mjyb16 Aug 27, 2023
15f48a1
Debugging python-app.yml
mjyb16 Aug 27, 2023
b0b1182
Update python-app.yml
mjyb16 Aug 27, 2023
892d3a5
Who uses windows anyway?
mjyb16 Aug 27, 2023
d52d9a9
Add phytorch to test_cosmology.py
mjyb16 Aug 27, 2023
a403118
Fixed import in test_cosmology.py
mjyb16 Aug 27, 2023
7fce541
Update coverage.yaml
mjyb16 Aug 27, 2023
ace4b80
Fixed test_cosmology.py
mjyb16 Aug 27, 2023
cbcad83
Fix test_cosmology.py
mjyb16 Aug 27, 2023
f9a5b20
Fix test_cosmology.py
mjyb16 Aug 27, 2023
2bd59a0
Add crit density test to test_cosmology.py
mjyb16 Aug 27, 2023
9503ea1
Fix critical density units in LambdaCDM.py
mjyb16 Aug 27, 2023
4974374
Reduce test threshold for accuracy of critical density test_cosmology.py
mjyb16 Aug 27, 2023
51cf340
Update README.md
mjyb16 Aug 27, 2023
30cb5f0
Update phytorch_instructions.md
mjyb16 Aug 27, 2023
db82596
Update python-app.yml
mjyb16 Aug 31, 2023
32d9b13
Update python-app.yml
mjyb16 Aug 31, 2023
d739e0c
Trying to make windows test ignore cosmology
mjyb16 Aug 31, 2023
d802592
Update docstring in FlatLambdaCDM.py
mjyb16 Aug 31, 2023
4da6311
Still working on windows tests
mjyb16 Aug 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ jobs:
echo github.event_name is: ${{ github.event_name }}
echo github workspace: ${{ github.workspace }}
pip --version
- name: Build Boost
id: boost
uses: egor-tensin/build-boost@v1
with:
version: 1.83.0
libraries: filesystem program_options system python
platform: x64
configuration: Release
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -44,6 +52,37 @@ jobs:
pip install -e .
pip show caustic
shell: bash
- name: Install phytorch dependencies
run: |
cd $GITHUB_WORKSPACE
pip install ninja
pip install git+https://github.com/rtqichen/torchdiffeq.git
pip show ninja
pip show torchdiffeq
shell: bash

- name: Install phytorch
run: |
cd $GITHUB_WORKSPACE
git clone https://github.com/mjyb16/phytorch_forked.git
cd phytorch_forked
pip install -e .
pip show phytorch
shell: bash

- name: Modify pytorch
run: |
complex_utils_path=$(LOCAL_VARIABLE="complex_utils.h" && echo $(cd $(dirname "$LOCAL_VARIABLE") && pwd -P)/$(basename "$LOCAL_VARIABLE"))
complex_utils_folder=$(LOCAL_VARIABLE="complex_utils.h" && echo $(cd $(dirname "$LOCAL_VARIABLE") && pwd -P))
rm -rf complex_utils_path
mv phytorch_forked/complex_utils.h complex_utils_folder
shell: bash

- name: Build phytorch extensions
run: |
cd $GITHUB_WORKSPACE/phytorch_forked/phytorch-extensions
python setup.py build_ext -b ../phytorch/extensions
shell: bash
- name: Test with pytest
run: |
cd $GITHUB_WORKSPACE
Expand Down
106 changes: 105 additions & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
matrix:
python-version: ["3.9", "3.10"]
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest, macOS-latest]
mjyb16 marked this conversation as resolved.
Show resolved Hide resolved

steps:
- name: Checkout caustic
Expand All @@ -31,6 +31,22 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Build Boost
id: boost
uses: egor-tensin/build-boost@v1
with:
version: 1.83.0
libraries: filesystem program_options system python
platform: x64
configuration: Release

- name: Show paths
run: |
printf 'Boost has been unpacked to: %s\n' '${{ steps.boost.outputs.root }}'
printf 'Libraries can be found here: %s\n' '${{ steps.boost.outputs.librarydir }}'

shell: bash

- name: Record State
run: |
pwd
Expand All @@ -54,11 +70,99 @@ jobs:
cd $GITHUB_WORKSPACE
pip install -e .
pip show caustic
cd $GITHUB_WORKSPACE
pwd
shell: bash

- name: Install phytorch dependencies
run: |
cd $GITHUB_WORKSPACE
pip install ninja
pip install git+https://github.com/rtqichen/torchdiffeq.git
pip show ninja
pip show torchdiffeq
shell: bash

- name: Install phytorch
run: |
cd $GITHUB_WORKSPACE
git clone https://github.com/mjyb16/phytorch_forked.git
cd phytorch_forked
pip install -e .
pip show phytorch
shell: bash

- name: Modify pytorch
run: |
complex_utils_path=$(LOCAL_VARIABLE="complex_utils.h" && echo $(cd $(dirname "$LOCAL_VARIABLE") && pwd -P)/$(basename "$LOCAL_VARIABLE"))
complex_utils_folder=$(LOCAL_VARIABLE="complex_utils.h" && echo $(cd $(dirname "$LOCAL_VARIABLE") && pwd -P))
rm -rf complex_utils_path
mv phytorch_forked/complex_utils.h complex_utils_folder
shell: bash

- name: Build phytorch extensions
run: |
cd $GITHUB_WORKSPACE/phytorch_forked/phytorch-extensions
python setup.py build_ext -b ../phytorch/extensions
shell: bash


- name: Test with pytest
run: |
cd $GITHUB_WORKSPACE
pytest test
shell: bash

build_windows:

runs-on: ${{matrix.os}}
strategy:
matrix:
python-version: ["3.9", "3.10"]
os: [windows-latest]

steps:
- name: Checkout caustic
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Record State
run: |
pwd
echo github.ref is: ${{ github.ref }}
echo GITHUB_SHA is: $GITHUB_SHA
echo github.event_name is: ${{ github.event_name }}
echo github workspace: ${{ github.workspace }}
pip --version

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pytest-cov torch
# Install deps
cd $GITHUB_WORKSPACE
pip install -r requirements.txt
shell: bash

- name: Install Caustic
run: |
cd $GITHUB_WORKSPACE
pip install -e .
pip show caustic
cd $GITHUB_WORKSPACE
pwd
shell: bash



- name: Test with pytest
run: |
cd $GITHUB_WORKSPACE
pytest test --ignore-glob='*cosmology.py'
shell: bash


2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Simply install caustic from PyPI:
pip install caustic
```

- If you would like to take gradients through cosmological parameters, see [here](https://github.com/Ciela-Institute/caustic/blob/differentiable_cosmo/phytorch_instructions.md) for additional installation instructions.

## Contributing

Please reach out to one of us if you're interested in contributing!
Expand Down
132 changes: 132 additions & 0 deletions caustic/cosmology/FlatLambdaCDM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from .base import Cosmology
from typing import Any, Optional

import torch
from astropy.cosmology import default_cosmology
from scipy.special import hyp2f1
from torch import Tensor

from ..utils import interp1d
from ..constants import G_over_c2, c_Mpc_s, km_to_Mpc
from ..parametrized import Parametrized, unpack

__all__ = (
"FlatLambdaCDM",
)

H0_default = float(default_cosmology.get().h)*100
critical_density_0_default = float(
default_cosmology.get().critical_density(0).to("solMass/Mpc^3").value
)
Om0_default = float(default_cosmology.get().Om0)

# Set up interpolator to speed up comoving distance calculations in Lambda-CDM
# cosmologies. Construct with float64 precision.
_comoving_distance_helper_x_grid = 10 ** torch.linspace(-3, 1, 500, dtype=torch.float64)
_comoving_distance_helper_y_grid = torch.as_tensor(
_comoving_distance_helper_x_grid
* hyp2f1(1 / 3, 1 / 2, 4 / 3, -(_comoving_distance_helper_x_grid ** 3)),
dtype=torch.float64,
)

class FlatLambdaCDM(Cosmology):
"""
Subclass of Cosmology representing a Flat Lambda Cold Dark Matter (LCDM) cosmology with no radiation.
NOTE: THIS IS THE VERSION WITHOUT PRECISE COSMOLOGICAL GRADIENTS AND WHICH USES AN APPROXIMATION SCHEME. It is kept for backwards compatibility. To use autograd through cosmological calculations, follow the installation instructions for phytorch.
"""

def __init__(
self,
H0: Optional[Tensor] = torch.tensor(H0_default),
critical_density_0: Optional[Tensor] = torch.tensor(critical_density_0_default),
Om0: Optional[Tensor] = torch.tensor(Om0_default),
name: str = None,
):
"""
Initialize a new instance of the FlatLambdaCDM class.

Args:
name (str): Name of the cosmology.
h0 (Optional[Tensor]): Hubble constant over 100. Default is h0_default.
critical_density_0 (Optional[Tensor]): Critical density at z=0. Default is critical_density_0_default.
Om0 (Optional[Tensor]): Matter density parameter at z=0. Default is Om0_default.
"""
super().__init__(name)

self.add_param("H0", H0)
self.add_param("critical_density_0", critical_density_0)
self.add_param("Om0", Om0)

self._comoving_distance_helper_x_grid = _comoving_distance_helper_x_grid.to(
dtype=torch.float32
)
self._comoving_distance_helper_y_grid = _comoving_distance_helper_y_grid.to(
dtype=torch.float32
)

def hubble_distance(self, H0):
"""
Calculate the Hubble distance.

Args:
h0 (Tensor): Hubble constant.

Returns:
Tensor: Hubble distance.
"""
return c_Mpc_s / (km_to_Mpc) / H0

@unpack(1)
def critical_density(self, z: Tensor, H0, central_critical_density, Om0, *args, params: Optional["Packed"] = None) -> torch.Tensor:
"""
Calculate the critical density at redshift z.

Args:
z (Tensor): Redshift.
params (Packed, optional): Dynamic parameter container for the computation.

Returns:
torch.Tensor: Critical density at redshift z.
"""
Ode0 = 1 - Om0
return central_critical_density * (Om0 * (1 + z) ** 3 + Ode0)

@unpack(1)
def _comoving_distance_helper(self, x: Tensor, *args, params: Optional["Packed"] = None) -> Tensor:
"""
Helper method for computing comoving distances.

Args:
x (Tensor): Input tensor.

Returns:
Tensor: Computed comoving distances.
"""
return interp1d(
self._comoving_distance_helper_x_grid,
self._comoving_distance_helper_y_grid,
torch.atleast_1d(x),
).reshape(x.shape)

@unpack(1)
def comoving_distance(self, z: Tensor, H0, central_critical_density, Om0, *args, params: Optional["Packed"] = None) -> Tensor:
"""
Calculate the comoving distance to redshift z.

Args:
z (Tensor): Redshift.
params (Packed, optional): Dynamic parameter container for the computation.

Returns:
Tensor: Comoving distance to redshift z.
"""
Ode0 = 1 - Om0
ratio = (Om0 / Ode0) ** (1 / 3)
return (
self.hubble_distance(H0)
* (
self._comoving_distance_helper((1 + z) * ratio, params)
- self._comoving_distance_helper(ratio, params)
)
/ (Om0 ** (1 / 3) * Ode0 ** (1 / 6))
)
Loading