deeplenstronomy
is designed for the purpose of facilitating machine and deep-learning studies of strong gravitational lensing. Supervised problems in these fields are typically framed as either classification problems or regression problems. This notebook will illustrate how you might utilize the features of deeplenstronomy
to aid the development of your classification and regression algorithms.
Let's start by simulating a dataset with the demo_distributions.yaml
file, and we'll also utilize the solve_lens_equation
and return_planes
arguments of deeplenstronomy.makedataset()
:
import deeplenstronomy.deeplenstronomy as dl
dataset = dl.make_dataset("data/demo_distributions.yaml", solve_lens_equation=True, return_planes=True, verbose=True)
Entering main organization loop Organizing CONFIGURATION_1 Generating images for CONFIGURATION_1 Progress: 100.0 % --- Elapsed Time: 0 H 3 M 30 S
For classification tasks, we are trying to predict a discretely-valued variable. The simplest approach to do this in deeplenstronomy
is to classify the images from one configuration against the images in another configuration, with the configuration label serving as your discrete variable. If you take that simple approach, structure each CONFIGURATION
section in the GEOMETRY
section to be one of the classes in your classification problem.
In strong lensing searches, the terminology of "quads" and "doubles" are often used to describe the type of lensing going on (refering to the number of images of the source galaxy produced by the lensing). If you would like to classify based on the number of images of the source object, you can use the solve_lens_equation
argument of deeplenstronomy.makedataset()
to your advantage.
Setting this argument to True
will add several columns to your dataset's metadata:
metric_cols = ['num_source_images-g',
'num_source_images-r',
'num_source_images-i',
'num_source_images-z',
'num_source_images-Y']
dataset.CONFIGURATION_1_metadata[metric_cols].head()
num_source_images-g | num_source_images-r | num_source_images-i | num_source_images-z | num_source_images-Y | |
---|---|---|---|---|---|
0 | 4 | 4 | 4 | 4 | 4 |
1 | 4 | 4 | 4 | 4 | 4 |
2 | 4 | 4 | 4 | 4 | 4 |
3 | 4 | 4 | 4 | 4 | 4 |
4 | 4 | 4 | 4 | 4 | 4 |
There will be a column of num_source_images
for each band you choose to simulate. Using these columns you can determine which images contain quads, doubles, or no lensing. There are also columns such as x_mins_g
, y_mins_g
, x_mins_r
, etc. for each band. These columns contain the information of the locations of each of the found positions of the lensed source object in your images.
Traditionally, strong lensing has been treated as a classification problem. However, there exist corner cases of slightly aligned galaxies that may produce small amounts of lensing as opposed to magnificent arcs and multiple images. If these corner cases exist in your training dataset, your classification algorithm may struggle with them. Framing lens detection as a regression problem is an interesting solution where instead of asking "Is this a lens?" or "Is this a quad?" you ask "How lensy is this image?"
To make this type of approach possible, you need to define a continuous variable that reflects the amount of lensing in the system. A mathematical approach to this is by calculating a quantity called the "Strong Lensing Cross Section," and as an open source framework, you are welcome to submit a pull-request to add this calculation to deeplenstronomy
.
Another option that is possible with the current implementation of deeplenstronomy
is to utilize the light from the source plane separate from all other light. Naively, if there is a lot of lensing going on, there will be a lot of light in the source plane, and if there is only a small amount of lensing then one would also expect a small amount of light in the source plane. This argument has to be normalized by the un-lensed brightness of the objects in the source plane, but is still a useable metric.
You can access this information by setting the return_planes
argument to True
. When you use this setting, your dataset will gain a new attribute for each configuration:
[x for x in dir(dataset) if x.endswith('planes')]
['CONFIGURATION_1_planes']
type(dataset.CONFIGURATION_1_planes)
numpy.ndarray
dataset.CONFIGURATION_1_planes.shape
(250, 4, 5, 100, 100)
The planes
for each configuration will be a numpy array with the following dimension structure:
Specifically, the plane
axis is ordered as ("Lens Plane", "Source Plane", "Point Sources", "Noise"). Thus you can access all the source planes like this:
source_planes = dataset.CONFIGURATION_1_planes[:,1,:,:,:]
From this point, you can define a metric of your choice to characterize the amount of lensing going on.
A simple example could be the number of pixels with brightness above a certain threshold in a single band (the $g$-band in this case):
import numpy as np
threshold = 5.0
metric = np.sum(np.where(source_planes[:,0,:,:] > threshold, 1, 0), axis=(-1,-2))
print(metric)
[2592 1562 750 1864 570 556 2010 2284 594 1192 624 718 1612 1306 1374 1122 1012 1008 1292 200 2124 946 872 408 622 1374 856 458 1040 2224 880 2002 1158 1044 1096 1074 740 594 592 678 600 520 704 562 740 1018 1018 322 474 648 1084 1034 532 542 1522 2698 1474 630 2082 374 644 492 862 1046 2476 924 996 976 600 1446 796 818 1224 2040 588 472 1282 1444 1156 1026 578 786 1404 816 752 310 514 2116 774 768 1216 1228 322 448 1818 442 696 814 906 1062 378 546 552 1092 922 566 870 692 1008 324 2158 774 932 2836 732 676 586 508 1438 1614 758 832 2740 1040 1094 606 462 614 328 1086 1162 596 710 456 2816 556 968 548 986 1086 1008 266 1756 440 372 864 1106 1690 570 512 460 804 734 1408 712 1150 2168 446 3884 662 840 1544 1012 1042 392 1336 1028 254 1054 1126 1598 886 1048 678 512 430 664 600 1148 704 608 932 434 388 406 1754 516 900 824 580 1712 1132 596 822 1422 338 2970 1014 1508 604 1168 1592 870 902 992 500 1064 762 1520 500 1782 490 752 1736 1466 846 960 328 2220 1266 1196 1422 440 504 600 2922 1376 430 178 652 596 714 3118 884 462 1606 1912 1966 1134 426 486 1122 926 354 866 634 1152 1090 562 930]
Note that I did not perform any normalization of the pixel values by the brightness of the objects simulated, nor did I normalize the number of pixels above the threshold by the angular sizes of the objects simulated, but these quantities are present in the dataset.CONFIGURATION_1.metadata
. If these other two properties of the simulated dat were accounted for, then this metric could potentially frame the problem of lens detection as a regression problem. The purpose of this specific example, though, was to display how to pull information out of the individual planes and present the general concept of approaching strong lens searches with regression.