-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a baseline CFIS dataset #40
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
""" TensorFlow Dataset of simulated CFIS images.""" | ||
import tensorflow_datasets as tfds | ||
import tensorflow as tf | ||
import numpy as np | ||
import galsim as gs | ||
|
||
_CITATION = """ | ||
""" | ||
|
||
_DESCRIPTION = """ | ||
""" | ||
|
||
class CFISConfig(tfds.core.BuilderConfig): | ||
"""BuilderConfig for CFIS Galaxies.""" | ||
|
||
def __init__(self, *, galaxy_type="parametric", | ||
data_set_size=1000, stamp_size=51, | ||
shear_g1=0.0, shear_g2=0.0, **kwargs): | ||
"""BuilderConfig for CFIS. | ||
Args: | ||
size: size of sample. | ||
stamp_size: image stamp size in pixels. | ||
pixel_scale: pixel scale of stamps in arcsec. | ||
**kwargs: keyword arguments forwarded to super. | ||
""" | ||
v1 = tfds.core.Version("0.0.1") | ||
super(CFISConfig, self).__init__( | ||
description=("Galaxy stamps"), | ||
version=v1, | ||
**kwargs) | ||
|
||
# Adjustable parameters | ||
self.galaxy_type = galaxy_type | ||
self.data_set_size = data_set_size | ||
self.stamp_size = stamp_size | ||
self.kstamp_size = stamp_size | ||
self.shear_g1 = shear_g1 | ||
self.shear_g2 = shear_g2 | ||
|
||
# Fixed parameters | ||
self.pixel_scale = 0.187 | ||
self.psf_fwhm = 0.65 # TODO: add varying PSF | ||
self.psf_e1 = 0.0 | ||
self.psf_e2 = 0.025 | ||
|
||
|
||
class CFIS(tfds.core.GeneratorBasedBuilder): | ||
"""DatasetBuilder for simulated CFIS dataset.""" | ||
|
||
VERSION = tfds.core.Version('0.0.1') | ||
RELEASE_NOTES = { | ||
'0.0.1': 'Initial release.', | ||
} | ||
|
||
BUILDER_CONFIGS = [CFISConfig(name="parametric_1k", galaxy_type="parametric", data_set_size=1000), | ||
CFISConfig(name="parametric_shear_1k", galaxy_type="parametric", data_set_size=1000, shear_g1=0.02)] | ||
|
||
def _info(self) -> tfds.core.DatasetInfo: | ||
"""Returns the dataset metadata.""" | ||
# TODO: Specifies the tfds.core.DatasetInfo object | ||
return tfds.core.DatasetInfo( | ||
builder=self, | ||
description=_DESCRIPTION, | ||
features=tfds.features.FeaturesDict({ | ||
'obs': tfds.features.Tensor(shape=[self.builder_config.stamp_size, | ||
self.builder_config.stamp_size], | ||
dtype=tf.float32), | ||
'psf': tfds.features.Tensor(shape=[self.builder_config.stamp_size, | ||
self.builder_config.stamp_size], | ||
dtype=tf.float32), | ||
# 'gal_kimage': tfds.features.Tensor(shape=[2, self.builder_config.kstamp_size, | ||
# self.builder_config.kstamp_size], | ||
# dtype=tf.float32), | ||
# 'psf_kimage': tfds.features.Tensor(shape=[2, self.builder_config.kstamp_size, | ||
# self.builder_config.kstamp_size], | ||
# dtype=tf.float32), | ||
"noise_std": tfds.features.Tensor(shape=[1], dtype=tf.float32), | ||
"mag": tfds.features.Tensor(shape=[1], dtype=tf.float32), | ||
}), | ||
# If there's a common (input, target) tuple from the | ||
# features, specify them here. They'll be used if | ||
# `as_supervised=True` in `builder.as_dataset`. | ||
supervised_keys=("obs", "obs"), | ||
homepage='https://dataset-homepage/', | ||
citation=_CITATION, | ||
) | ||
|
||
def _split_generators(self, dl_manager: tfds.download.DownloadManager): | ||
"""Returns SplitGenerators.""" | ||
if self.builder_config.data_set_size: | ||
size = self.builder_config.data_set_size | ||
else: | ||
size = 81499 | ||
|
||
return [ | ||
tfds.core.SplitGenerator( | ||
name=tfds.Split.TRAIN, | ||
gen_kwargs={ | ||
"size": size, | ||
},), | ||
] | ||
|
||
def _generate_examples(self, size): | ||
"""Yields examples.""" | ||
# Loads the galsim COSMOS catalog | ||
cat = gs.COSMOSCatalog(sample="25.2") | ||
mag_zp = 32 | ||
sky_level = 400 # ADU (~variance) | ||
psf = gs.Kolmogorov(fwhm=self.builder_config.psf_fwhm, flux=1.0) | ||
psf = psf.shear(g1=self.builder_config.psf_e1, g2=self.builder_config.psf_e2) | ||
|
||
# Prepare borders for kimage | ||
Nk = self.builder_config.kstamp_size | ||
bounds = gs._BoundsI(-Nk//2, Nk//2-1, -Nk//2, Nk//2-1) | ||
|
||
for i in range(size): | ||
# retrieving galaxy and magnitude | ||
gal = cat.makeGalaxy(i, gal_type='parametric') | ||
gal_mag = cat.param_cat['mag_auto'][cat.orig_index[i]] | ||
gal_flux = 10**(-(gal_mag-mag_zp)/2.5) | ||
|
||
gal = gal.withFlux(gal_flux) | ||
gal = gal.shear(g1=self.builder_config.shear_g1, g2=self.builder_config.shear_g2) | ||
|
||
gal_conv = gs.Convolve(gal, psf) | ||
|
||
gal_stamp = gal_conv.drawImage(nx=self.builder_config.stamp_size, | ||
ny=self.builder_config.stamp_size, | ||
scale=self.builder_config.pixel_scale | ||
).array.astype('float32') | ||
|
||
psf_stamp = psf.drawImage(nx=self.builder_config.stamp_size, | ||
ny=self.builder_config.stamp_size, | ||
scale=self.builder_config.pixel_scale | ||
).array.astype('float32') | ||
|
||
# gal_kimage = gal.drawKImage(bounds=bounds, | ||
# scale=2.*np.pi/(self.builder_config.stamp_size*self.builder_config.pixel_scale), | ||
# recenter=False).array.astype('complex64') | ||
|
||
# psf_kimage = psf.drawKImage(bounds=bounds, | ||
# scale=2.*np.pi/(self.builder_config.stamp_size*self.builder_config.pixel_scale), | ||
# recenter=False).array.astype('complex64') | ||
|
||
yield '%d'%i, {"obs": gal_stamp, | ||
"psf": psf_stamp, | ||
# "gal_kimage": np.stack([gal_kimage.real, gal_kimage.imag]), | ||
# "psf_kimage": np.stack([psf_kimage.real, psf_kimage.imag]), | ||
"noise_std": np.array([np.sqrt(sky_level)]).astype('float32'), | ||
"mag": np.array([gal_mag]).astype('float32')} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"""gal_gen dataset.""" | ||
|
||
from .gal_gen import GalGen | ||
from .CFIS import CFIS |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you have these commented lines? If they are not needed we should remove to avoid cluttering the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that;s a good point, I'll rmove this stuff for now