Skip to content

Commit

Permalink
Invisible shifts and indices (#244)
Browse files Browse the repository at this point in the history
* add notes to documentation for users about shifts/indexes

* remove shifts/ indices from notebooks to avoid confusing users
  • Loading branch information
ismael-mendoza authored Nov 14, 2021
1 parent ee60c2b commit 2aea6b6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 55 deletions.
3 changes: 2 additions & 1 deletion btk/create_blend_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def __init__(
batch_size (int): Size of batches returned.
shifts (list): Contains arbitrary shifts to be applied instead of
random shifts. Must be of length batch_size. Must be used
with indexes.
with indexes. Used mostly for internal testing purposes.
indexes (list): Contains the ids of the galaxies to use in the stamp.
Must be of length batch_size. Must be used with shifts.
Used mostly for internal testing purposes.
verbose (bool): Whether to print additional information.
"""
self.catalog = catalog
Expand Down
3 changes: 2 additions & 1 deletion btk/draw_blends.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ def __init__(
add_noise (bool): Indicates if the blends should be generated with noise
shifts (list): Contains arbitrary shifts to be applied instead of
random shifts. Must be of length batch_size. Must be used
with indexes.
with indexes. Used mostly for internal testing purposes.
indexes (list): Contains the ids of the galaxies to use in the stamp.
Must be of length batch_size. Must be used with shifts.
Used mostly for internal testing purposes.
channels_last (bool): Whether to return images as numpy arrays with the channel
(band) dimension as the last dimension or before the pixels
dimensions (default).
Expand Down
21 changes: 3 additions & 18 deletions docs/source/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ which galaxies are drawn, with what shifts, etc. This is achieved using the ``Sa
max_shift = 3.0 # Maximum shift of the galaxies, in arcseconds
sampling_function = btk.sampling_functions.DefaultSampling(max_number=max_number, stamp_size=stamp_size, maxshift=max_shift, seed=seed)

As a reference, here is the code for this sampling function:
As a reference, here is a (slightly) simplified version of the code for this sampling function:

.. jupyter-execute::

Expand All @@ -106,7 +106,7 @@ As a reference, here is the code for this sampling function:
def compatible_catalogs(self):
return "CatsimCatalog", "CosmosCatalog"

def __call__(self, table, shifts=None, indexes=None):
def __call__(self, table):
"""Applies default sampling to the input CatSim-like catalog and returns an
astropy table with entries corresponding to a blend centered close to postage
stamp center.
Expand All @@ -122,27 +122,16 @@ As a reference, here is the code for this sampling function:
Args:
table (astropy.table): Table containing entries corresponding to galaxies
from which to sample.
shifts (list): Contains arbitrary shifts to be applied instead of random ones.
Should of the form [x_peak,y_peak] where x_peak and y_peak are the lists
containing the x and y shifts.
indexes (list): Contains the indexes of the galaxies to use.

Returns:
Astropy.table with entries corresponding to one blend.
"""
number_of_objects = np.random.randint(1, self.max_number + 1)
(q,) = np.where(table["ref_mag"] <= 25.3)

if indexes is None:
blend_table = table[np.random.choice(q, size=number_of_objects)]
else:
blend_table = table[indexes]
blend_table = table[np.random.choice(q, size=number_of_objects)]
blend_table["ra"] = 0.0
blend_table["dec"] = 0.0
if shifts is None:
x_peak, y_peak = _get_random_center_shift(number_of_objects, self.maxshift)
else:
x_peak, y_peak = shifts
blend_table["ra"] += x_peak
blend_table["dec"] += y_peak

Expand Down Expand Up @@ -296,8 +285,6 @@ Now that we have all the objects at our disposal, we can create the DrawBlendsGe
[Rubin],
batch_size=8,
stamp_size=stamp_size,
shifts=None,
indexes=None,
cpus=1,
add_noise=True,
seed=seed
Expand Down Expand Up @@ -464,8 +451,6 @@ Saving the results can be automatically achieved by providing the save_path argu
Rubin,
batch_size=100,
stamp_size=stamp_size,
shifts=None,
indexes=None,
cpus=1,
add_noise=True,
save_path=save_path
Expand Down
22 changes: 4 additions & 18 deletions notebooks/00-intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As a reference, here is the code for this sampling function."
"As a reference, here is a (slightly) simplified version of the code for this sampling function."
]
},
{
Expand Down Expand Up @@ -234,7 +234,7 @@
" def compatible_catalogs(self):\n",
" return \"CatsimCatalog\", \"CosmosCatalog\"\n",
"\n",
" def __call__(self, table, shifts=None, indexes=None):\n",
" def __call__(self, table):\n",
" \"\"\"Applies default sampling to the input CatSim-like catalog and returns an\n",
" astropy table with entries corresponding to a blend centered close to postage\n",
" stamp center.\n",
Expand All @@ -250,27 +250,17 @@
" Args:\n",
" table (astropy.table): Table containing entries corresponding to galaxies\n",
" from which to sample.\n",
" shifts (list): Contains arbitrary shifts to be applied instead of random ones.\n",
" Should of the form [x_peak,y_peak] where x_peak and y_peak are the lists\n",
" containing the x and y shifts.\n",
" indexes (list): Contains the indexes of the galaxies to use.\n",
"\n",
" Returns:\n",
" Astropy.table with entries corresponding to one blend.\n",
" \"\"\"\n",
" number_of_objects = np.random.randint(1, self.max_number + 1)\n",
" (q,) = np.where(table[\"ref_mag\"] <= 25.3)\n",
"\n",
" if indexes is None:\n",
" blend_table = table[np.random.choice(q, size=number_of_objects)]\n",
" else:\n",
" blend_table = table[indexes]\n",
" blend_table = table[np.random.choice(q, size=number_of_objects)]\n",
" blend_table[\"ra\"] = 0.0\n",
" blend_table[\"dec\"] = 0.0\n",
" if shifts is None:\n",
" x_peak, y_peak = _get_random_center_shift(number_of_objects, self.maxshift)\n",
" else:\n",
" x_peak, y_peak = shifts\n",
" x_peak, y_peak = _get_random_center_shift(number_of_objects, self.maxshift)\n",
" blend_table[\"ra\"] += x_peak\n",
" blend_table[\"dec\"] += y_peak\n",
"\n",
Expand Down Expand Up @@ -500,8 +490,6 @@
" Rubin,\n",
" batch_size=100,\n",
" stamp_size=stamp_size,\n",
" shifts=None,\n",
" indexes=None,\n",
" cpus=1,\n",
" add_noise=True,\n",
" seed=seed, # same random seed is used here too!\n",
Expand Down Expand Up @@ -970,8 +958,6 @@
" Rubin,\n",
" batch_size=100,\n",
" stamp_size=stamp_size,\n",
" shifts=None,\n",
" indexes=None,\n",
" cpus=1,\n",
" add_noise=True,\n",
" save_path=save_path\n",
Expand Down
2 changes: 0 additions & 2 deletions notebooks/01c-galsim_hub_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@
" survey,\n",
" batch_size=8,\n",
" stamp_size=stamp_size,\n",
" shifts=None,\n",
" indexes=None,\n",
" cpus=1,\n",
" add_noise=True,\n",
" galsim_hub_model=\"hub:Lanusse2020\", #May be replaced by any model compatible with galsim_hub\n",
Expand Down
20 changes: 5 additions & 15 deletions notebooks/02b-custom-tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"import os\n",
"import sys\n",
"import btk\n",
"import galsim"
"import galsim\n",
"import warnings"
]
},
{
Expand Down Expand Up @@ -79,7 +80,7 @@
" def compatible_catalogs(self):\n",
" return \"CatsimCatalog\", \"CosmosCatalog\"\n",
"\n",
" def __call__(self, table, shifts=None, indexes=None):\n",
" def __call__(self, table):\n",
" \"\"\"Applies default sampling to the input CatSim-like catalog and returns an\n",
" astropy table with entries corresponding to a blend centered close to postage\n",
" stamp center.\n",
Expand All @@ -95,27 +96,17 @@
" Args:\n",
" table (astropy.table): Table containing entries corresponding to galaxies\n",
" from which to sample.\n",
" shifts (list): Contains arbitrary shifts to be applied instead of random ones.\n",
" Should of the form [x_peak,y_peak] where x_peak and y_peak are the lists\n",
" containing the x and y shifts.\n",
" indexes (list): Contains the indexes of the galaxies to use.\n",
"\n",
" Returns:\n",
" Astropy.table with entries corresponding to one blend.\n",
" \"\"\"\n",
" number_of_objects = np.random.randint(1, self.max_number + 1)\n",
" (q,) = np.where(table[\"ref_mag\"] <= 25.3)\n",
"\n",
" if indexes is None:\n",
" blend_table = table[np.random.choice(q, size=number_of_objects)]\n",
" else:\n",
" blend_table = table[indexes]\n",
" blend_table = table[np.random.choice(q, size=number_of_objects)]\n",
" blend_table[\"ra\"] = 0.0\n",
" blend_table[\"dec\"] = 0.0\n",
" if shifts is None:\n",
" x_peak, y_peak = _get_random_center_shift(number_of_objects, self.maxshift)\n",
" else:\n",
" x_peak, y_peak = shifts\n",
" x_peak, y_peak = _get_random_center_shift(number_of_objects, self.maxshift)\n",
" blend_table[\"ra\"] += x_peak\n",
" blend_table[\"dec\"] += y_peak\n",
"\n",
Expand Down Expand Up @@ -170,7 +161,6 @@
" return \"CatsimCatalog\", \"CosmosCatalog\"\n",
"\n",
" def __call__(self,table):\n",
" number_of_objects = np.random.randint(1, self.max_number + 1)\n",
" (q_bright,) = np.where(table[\"ref_mag\"] <= 25.3)\n",
" (q_dim,) = np.where((table[\"ref_mag\"] > 25.3) & (table[\"ref_mag\"] <= 28))\n",
" \n",
Expand Down

0 comments on commit 2aea6b6

Please sign in to comment.