Skip to content

Commit

Permalink
Merge pull request #99 from Ciela-Institute/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ConnorStoneAstro authored Nov 3, 2023
2 parents 233c195 + 22ad60e commit 305ddda
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ jobs:
uses: codecov/codecov-action@v3
with:
files: ${{ github.workspace }}/coverage.xml
fail_ci_if_error: true
fail_ci_if_error: false
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![tests](https://github.com/Ciela-Institute/caustic/actions/workflows/python-app.yml/badge.svg?branch=main)](https://github.com/Ciela-Institute/caustic/actions)
[![Docs](https://github.com/Ciela-Institute/caustic/actions/workflows/documentation.yaml/badge.svg)](https://github.com/Ciela-Institute/caustic/actions/workflows/documentation.yaml)
[![PyPI version](https://badge.fury.io/py/caustic.svg)](https://pypi.org/project/caustic/)
[![coverage](https://img.shields.io/codecov/c/github/Ciela-Institute/caustic)](https://app.codecov.io/gh/Ciela-Institute/caustic)
# caustic
Expand Down Expand Up @@ -28,6 +29,9 @@ pip install -e ".[dev]"
```
This creates an editable install and installs the dev dependencies.

Please use `isort` and `black` to format your code. Open up issues for bugs/missing
features. Use pull requests for additions to the code. Write tests that can be run
by [`pytest`](https://docs.pytest.org/).
Some guidelines:
- Please use `isort` and `black` to format your code.
- Use `CamelCase` for class names and `snake_case` for variable and method names.
- Open up issues for bugs/missing features.
- Use pull requests for additions to the code.
- Write tests that can be run by [`pytest`](https://docs.pytest.org/).
10 changes: 10 additions & 0 deletions caustic/parametrized.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import torch
import re
import keyword
from torch import Tensor

from .packed import Packed
Expand All @@ -14,6 +15,13 @@

__all__ = ("Parametrized","unpack")


def check_valid_name(name):
if keyword.iskeyword(name) or not bool(re.match("^[a-zA-Z_][a-zA-Z0-9_]*$", name)):
raise NameError(f"The string {name} contains illegal characters (like space or '-'). "\
"Please use snake case or another valid python variable naming style.")


class Parametrized:
"""
Represents a class with Param and Parametrized attributes, typically used to construct parts of a simulator
Expand All @@ -40,6 +48,7 @@ class Parametrized:
def __init__(self, name: str = None):
if name is None:
name = self._default_name()
check_valid_name(name)
if not isinstance(name, str):
raise ValueError(f"name must be a string (received {name})")
self._name = name
Expand Down Expand Up @@ -87,6 +96,7 @@ def name(self) -> str:

@name.setter
def name(self, new_name: str):
check_valid_name(new_name)
old_name = self.name
for parent in self._parents.values():
del parent._childs[old_name]
Expand Down
4 changes: 3 additions & 1 deletion test/test_cosmology.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from astropy.cosmology import Cosmology as Cosmology_AP
from astropy.cosmology import FlatLambdaCDM as AstropyFlatLambdaCDM

from caustic.cosmology import Cosmology, FlatLambdaCDM as CausticFlatLambdaCDM, Om0_default, h0_default
from caustic.cosmology import Cosmology
from caustic.cosmology import FlatLambdaCDM as CausticFlatLambdaCDM
from caustic.cosmology import Om0_default, h0_default


def get_cosmologies() -> List[Tuple[Cosmology, Cosmology_AP]]:
Expand Down
6 changes: 3 additions & 3 deletions test/test_jacobian_lens_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_multiplane_jacobian():
x = torch.tensor([p for _xs in xs for p in _xs], dtype=torch.float32)

lens = Multiplane(
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie-{i}", cosmology=cosmology) for i in range(len(xs))]
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie_{i}", cosmology=cosmology) for i in range(len(xs))]
)
thx, thy = get_meshgrid(0.1, 10, 10)

Expand All @@ -66,7 +66,7 @@ def test_multiplane_jacobian_autograd_vs_finitediff():
x = torch.tensor([p for _xs in xs for p in _xs], dtype=torch.float32)

lens = Multiplane(
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie-{i}", cosmology=cosmology) for i in range(len(xs))]
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie_{i}", cosmology=cosmology) for i in range(len(xs))]
)
thx, thy = get_meshgrid(0.01, 10, 10)

Expand Down Expand Up @@ -96,7 +96,7 @@ def test_multiplane_effective_convergence():
x = torch.tensor([p for _xs in xs for p in _xs], dtype=torch.float32)

lens = Multiplane(
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie-{i}", cosmology=cosmology) for i in range(len(xs))]
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie_{i}", cosmology=cosmology) for i in range(len(xs))]
)
thx, thy = get_meshgrid(0.1, 10, 10)

Expand Down
Empty file removed test/test_lenses.py
Empty file.
2 changes: 1 addition & 1 deletion test/test_multiplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test():
x = torch.tensor([p for _xs in xs for p in _xs], dtype=torch.float32)

lens = Multiplane(
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie-{i}", cosmology=cosmology) for i in range(len(xs))]
name="multiplane", cosmology=cosmology, lenses=[SIE(name=f"sie_{i}", cosmology=cosmology) for i in range(len(xs))]
)
#lens.effective_reduced_deflection_angle = lens.raytrace

Expand Down
22 changes: 17 additions & 5 deletions test/test_parametrized.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
from torch import vmap
import pytest
import numpy as np
from caustic.sims import Simulator
from caustic.parameter import Parameter
Expand Down Expand Up @@ -103,11 +104,22 @@ def __init__(self):
assert sim.name == "Test"

# Check that DAG in SIM is being update updated
sim.lens.name = "Test Lens"
assert sim.lens.name == "Test Lens"
assert "Test Lens" in sim.params.dynamic.keys()
assert "Test Lens" in sim.cosmo._parents.keys()

sim.lens.name = "test_lens"
assert sim.lens.name == "test_lens"
assert "test_lens" in sim.params.dynamic.keys()
assert "test_lens" in sim.cosmo._parents.keys()


def test_parametrized_name_setter_bad_names():
# Make sure bad names are catched by our added method. Bad names are name which cannot be used as class attributes.
good_names = ["variable", "_variable", "var_iable2"]
for name in good_names:
module = Sersic(name=name)
bad_names = ["for", "2variable", "variable!", "var-iable", "var iable", "def"]
for name in bad_names:
print(name)
with pytest.raises(NameError):
module = Sersic(name=name)

def test_parametrized_name_collision():
# Case 1: Name collision in children of simulator
Expand Down
9 changes: 9 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import torch
import numpy as np
from astropy.cosmology import FlatLambdaCDM as FlatLambdaCDM_AP
from lenstronomy.Data.pixel_grid import PixelGrid
from lenstronomy.LensModel.lens_model import LensModel

Expand Down Expand Up @@ -121,6 +122,14 @@ def forward(self, params):
return Sim(), (cosmo_params, lens_params, [kappa], [source])


def get_default_cosmologies():
cosmology = FlatLambdaCDM("cosmo")
cosmology_ap = FlatLambdaCDM_AP(
100 * cosmology.h0.value, cosmology.Om0.value, Tcmb0=0
)
return cosmology, cosmology_ap


def setup_grids(res=0.05, n_pix=100):
# Caustic setup
thx, thy = get_meshgrid(res, n_pix, n_pix)
Expand Down

0 comments on commit 305ddda

Please sign in to comment.