Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

CosmoSim Demo II Annotation

CosmoSim can add several annotations to generated images. This tutorial will show the ones available as of v3.0.0.

Preparation

We use the same basic setup and lens configuration as we used in CosmoSim Demo I. The following code is copied therefrom and trimmed.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import tomllib as tl

from CosmoSim.datagen import SimImage
import CosmoSim.Image as csimg
from CosmoSim import Parameters


with open( "Demo01.toml", 'rb') as f:
            toml = tl.load(f)

param = Parameters( toml )
imsim = SimImage( param, verbose=0 )
im = imsim.getImage()
csimg.imshow( im,  "Initial configuration" )
<Figure size 640x480 with 1 Axes>

Annotation

The cosmological annotations is retrieved with the getAnnotated() method.

im = imsim.getAnnotated()
csimg.imshow( im,  "Annotated image" )
Annotation at (276, 250) colour (64, 255, 64)
Annotation at (291, 298) colour (64, 64, 255)
Annotation at (291, 298) colour (64, 64, 255)
<Figure size 640x480 with 1 Axes>

The critical curve is shown in white and the convergence ring in blue. The green point is the centre of light. In machine learning we would translate the image to centre it on the centre of image. We can also get the exact co-ordinates of this centre.

print( "Centre point", imsim.centrepoint )
Centre point (np.float64(20.218618591066843), np.float64(5.911662614493025))

We can add an axis cross, which is useful to see the location of the origin, and hence also of the lens.

csimg.drawAxes( im )
plt.imshow( im, cmap='gray')
plt.title( "With axis cross" )
plt.axis("off")
(np.float64(-0.5), np.float64(255.5), np.float64(255.5), np.float64(-0.5))
<Figure size 640x480 with 1 Axes>

Note that the convergence ring passes through the centre of the lens, as it always should.

The source

The unlensed source can be displayed as follows.

im = imsim.getActualImage()
csimg.drawAxes( im )
plt.imshow( im, cmap='gray')
plt.title( "The source w/o lensing" )
plt.axis("off")
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
<Figure size 640x480 with 1 Axes>

We barely see the tiny source which sits on the xx-axis. We could remove the axis cross to see it better.

Cropping

Generally, we use an oversized image for calculation, and then crop it down to a manageable size.

print( "Prior shape", im.shape )
im = csimg.crop( im )
print( "Posterior shape", im.shape )
csimg.imshow( im, "Cropped image" )
Prior shape (512, 512)
[crop] cropsize=256
Posterior shape (256, 256)
<Figure size 640x480 with 1 Axes>

The default cropsize is 256, but in the batch process, the sizes are read from simulator.imagesize and simulator.cropsize. We could give it as an argument to crop() too.

im = csimg.crop( im, 128 )
plt.imshow( im, cmap='gray')
plt.title( "Much cropped image" )
plt.axis("off")
[crop] cropsize=128
(np.float64(-0.5), np.float64(127.5), np.float64(127.5), np.float64(-0.5))
<Figure size 640x480 with 1 Axes>

Closure

Annotation is work in process, as we mentioned. Stay tuned.