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.

Sample Datasets

The purpose of this demonstration is to explore reasonable parameters for dataset generation. We aim to follow established custom in the literature, as far as possible.

Because we centre the images on the visible light, there will usually be a lot of empty background to crop. Making the calculations on
larger images will prevent many cropping artifacts. We also note that the image size is limited by computer memory, when they are used with machine learning. We were unable to use 512×512512\times512 images on a GPU with 50Gb memory, but 256×256256\times256 work.

Parameter ranges

To build training sets we need to generate random sets of plausible images. The ranges and probability distribution may vary from study to study. The parameters we establish here are designed to be realistic examples of strong lensing, erring on the side of wider ranges.

ParameterSymnolIdentifierDistributionRange
Einstein radiusθE\theta_EeinsteinradiusUniform0.1"θE3.0"0.1"\le\theta_E\le3.0"
Source positionRRposition.rUniformR1.2θER\le1.2\cdot\theta_E
Source locationϕ\phiposition.phiUniform0ϕ1800\le\phi\le180
Lens orientationorientationUniform01800\ldots180
Source orientationUniform01800\ldots180
Lens ellipticityffellipseratioUniform0.1f0.90.1\le f\le 0.9
Source sizeσ\sigmasigmaUniform0.04"f0.2"0.04"\le f\le 0.2"
Sersic indexnsn_sn_sersicUniform1ns51\le n_s\le 5
LuminosityllluminosityExponential10l2010\le l\le 20, λ=2.0\lambda=2.0
  1. The source position is given in polar co-ordinates (R,ϕ)(R,\phi).

    • The distance RR is chosen to be inside or around the critical curve, hence the limit RcθER\le c\theta_E for some constant cc.

  2. Angles are given in degrees in the source code and chosen uniformly from a half circle, because of symmetry.

  3. The source is spherical with a sersic profile.

Dataset generation

Before we start, lets check the version.

import CosmoSim
print( CosmoSim.__version__ )
3.2.2b2

The CosmoSim.dataset module provides the functions to generate random datasets. We will also use the Imge module as before.

import CosmoSim.dataset as csd
import CosmoSim.Image as csimg

The probability distribution is specified by a nested dict, typically defined in a TOML file, similar to those used in other CosmoSim modules. However, the dataset submodule does not use the Parameters class for its parameters at present.

cfg = csd.readtoml( "dataset.toml" )
display( cfg )
{'simulator': {'config': ['raysie'], 'size': 20000, 'nterms': 4, 'imagesize': 512, 'cropsize': 256, 'centred': True}, 'dataset': {'directory': 'images'}, 'lens': {'einstein-min': 2.5, 'einstein-max': 75, 'orientation-min': 0, 'orientation-max': 180, 'ellipseratio-min': 0.1, 'ellipseratio-max': 0.9}, 'source': {'mode': 'SersicSphere', 'n_sersic-min': 1, 'n_sersic-max': 5, 'luminosity-min': 10, 'luminosity-max': 20, 'luminosity-lambda': 2.0, 'sigma-min': 1, 'sigma-max': 5, 'position': 'relative'}, 'position': {'phi-min': 0, 'phi-max': 360, 'r-relativemax': 1.2}}

We see that this configuration is set up to generate 24000 images in 256×\times256 format. We can see the paremeter ranges for the lens and for the source, which is a sphere with sersic profile. As of CosmoSim v3.1 we can still use the config parameter for both the simulation model and the lens; “raysie” is raytrace simualation of a SIE lens. This will change in v3.2, to require separate settings for simulator.model and lens.mode.

To get a single random object, we use the getline() function.

ob = csd.getline( cfg, fn="test.png" )
display( ob )
index 0 filename test.png config raysie einsteinradius 40.460766 ellipseratio 0.840452 orientation 170.832878 source SersicSphere R 44.317944 phi 198.820884 sigma 2.901464 sigma2 37.42415 theta 164.068265 n_sersic 3.644871 luminosity 15.651086 x -41.94834 y -14.297444 dtype: object

The filename fn is used for indexing as well as saving images in batch processing.
It can otherwise be omitted.

Generating the image

Observe that the row generated by getline() is a flat structure, and not the hierarchical structure used in TOML files. However, we can add the parameters from ob to the parameters object as follows.

import CosmoSim as cs
import CosmoSim.datagen as csg
import matplotlib.pyplot as plt

param = cs.Parameters( )
param.setRow( ob )

Now we can run the simulator as we have done in previous demos (e.g. CosmoSim Demo I).

imsim = csg.SimImage( param, verbose=0 )
im = imsim.getImage()
csimg.imshow( im, "Random image test" )
<Figure size 640x480 with 1 Axes>

The image is very small, but that may well be correct. We can inspect the undistorted source image as well.

actual = imsim.getActualImage()
csimg.imshow( actual, "Undistorted source" )
<Figure size 640x480 with 1 Axes>

Sampling

To get an impression of distribution, we can bulk generate configurations. Let’s first look at the dataset, as follows.

import pandas as pd
obs = [ csd.getline( cfg ) for ob in range(8) ]
df = pd.DataFrame( obs )
display(df)
Loading...

To create the images we make a quick function to generate a each one.

def getImage(ob):
    param = cs.Parameters(cliconfig=cfg)
    param.setRow( ob )
    sim = csg.SimImage( param, verbose=0 )
    im = sim.getImage() 
    csimg.crop( im, param.get( "cropsize" ), verbose=0 )
    return im

Each line of the function is as used above in the document, except that we have added cropping. Now we can quickly generate a list of images and display them.

import CosmoSim.Image as csimg

ims = [ getImage(ob) for ob in obs ]

fig = plt.figure(figsize=(20, 10))
fig.tight_layout(pad=0.0)
plt.subplots_adjust(hspace=0.1, wspace=0.1) 

for idx,im in enumerate(ims):
    fig.add_subplot(2, 4, idx+1)
    csimg.imshow( im )
<Figure size 2000x1000 with 8 Axes>

Bulk generation

Managing thousands of images in a Jupyter Notebook is not very practical. In most cases, one will want to generate the dataset from the command line

python -m CosmoSim.dataset config.toml dataset.csv

This generates a CSV file which can be used for image generation.

It is also possible to generate the dataset and the images in one go, using

python -m CosmoSim --rnd --toml config.toml --csvfile dataset.csv \
      --outfile roulette.csv --directory images

This generates the random dataset (dataset.csv) and the images in the images directory, as well as a dataset of corresponding roulettes amplitudes (roulette.csv).