Skip to content

Commit

Permalink
docs: Adding basic lensing example to docs (#134)
Browse files Browse the repository at this point in the history
* Add minimal lensing example, update basic intro tutorial

* Add minimal example to home page of website

* Update minimal example to include vmap and jacobian

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ConnorStoneAstro and pre-commit-ci[bot] authored Jan 4, 2024
1 parent 791c40a commit 3a9fa09
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 77 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,65 @@ Simply install caustics from PyPI:
pip install caustics
```

## Minimal Example

```python
import matplotlib.pyplot as plt
import caustics
import torch

cosmology = caustics.cosmology.FlatLambdaCDM()
sie = caustics.lenses.SIE(cosmology=cosmology, name="lens")
src = caustics.light.Sersic(name="source")
lnslt = caustics.light.Sersic(name="lenslight")

x = torch.tensor([
# z_s z_l x0 y0 q phi b x0 y0 q phi n Re
1.5, 0.5, -0.2, 0.0, 0.4, 1.5708, 1.7, 0.0, 0.0, 0.5, -0.985, 1.3, 1.0,
# Ie x0 y0 q phi n Re Ie
5.0, -0.2, 0.0, 0.8, 0.0, 1., 1.0, 10.0
]) # fmt: skip

minisim = caustics.sims.Lens_Source(
lens=sie, source=src, lens_light=lnslt, pixelscale=0.05, pixels_x=100
)
plt.imshow(minisim(x, quad_level=3), origin="lower")
plt.axis("off")
plt.show()
```

![Caustics lensed image](./media/minimal_example.png)

### Batched simulator

```python
newx = x.repeat(20, 1)
newx += torch.normal(mean=0, std=0.1 * torch.ones_like(newx))

images = torch.vmap(minisim)(newx)

fig, axarr = plt.subplots(4, 5, figsize=(20, 16))
for ax, im in zip(axarr.flatten(), images):
ax.imshow(im, origin="lower")
plt.show()
```

![Batched Caustics lensed images](../../media/minisim_vmap.png)

### Automatic Differentiation

```python
J = torch.func.jacfwd(minisim)(x)

# Plot the new images
fig, axarr = plt.subplots(3, 7, figsize=(20, 9))
for i, ax in enumerate(axarr.flatten()):
ax.imshow(J[..., i], origin="lower")
plt.show()
```

![Jacobian Caustics lensed image](../../media/minisim_jacobian.png)

## Documentation

Please see our [documentation page](https://caustics.readthedocs.io/en/latest/)
Expand Down
60 changes: 59 additions & 1 deletion docs/source/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,65 @@ are ready to go! You can check out the tutorials afterwards to see some of
caustics' capabilities. If you want to help out with building the caustics code
base check out the developer installation instructions instead.

### Contents
## Minimal Example

```python
import matplotlib.pyplot as plt
import caustics
import torch

cosmology = caustics.cosmology.FlatLambdaCDM()
sie = caustics.lenses.SIE(cosmology=cosmology, name="lens")
src = caustics.light.Sersic(name="source")
lnslt = caustics.light.Sersic(name="lenslight")

x = torch.tensor([
# z_s z_l x0 y0 q phi b x0 y0 q phi n Re
1.5, 0.5, -0.2, 0.0, 0.4, 1.5708, 1.7, 0.0, 0.0, 0.5, -0.985, 1.3, 1.0,
# Ie x0 y0 q phi n Re Ie
5.0, -0.2, 0.0, 0.8, 0.0, 1., 1.0, 10.0
]) # fmt: skip

minisim = caustics.sims.Lens_Source(
lens=sie, source=src, lens_light=lnslt, pixelscale=0.05, pixels_x=100
)
plt.imshow(minisim(x, quad_level=3), origin="lower")
plt.show()
```

![Caustics lensed image](../../media/minimal_example.png)

### Batched simulator

```python
newx = x.repeat(20, 1)
newx += torch.normal(mean=0, std=0.1 * torch.ones_like(newx))

images = torch.vmap(minisim)(newx)

fig, axarr = plt.subplots(4, 5, figsize=(20, 16))
for ax, im in zip(axarr.flatten(), images):
ax.imshow(im, origin="lower")
plt.show()
```

![Batched Caustics lensed images](../../media/minisim_vmap.png)

### Automatic Differentiation

```python
J = torch.func.jacfwd(minisim)(x)

# Plot the new images
fig, axarr = plt.subplots(3, 7, figsize=(20, 9))
for i, ax in enumerate(axarr.flatten()):
ax.imshow(J[..., i], origin="lower")
plt.show()
```

![Jacobian Caustics lensed image](../../media/minisim_jacobian.png)

## Contents

```{tableofcontents}
Expand Down
Loading

0 comments on commit 3a9fa09

Please sign in to comment.