-
Notifications
You must be signed in to change notification settings - Fork 28
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
Hackday mteodoro sky variance readnoise #1556
base: main
Are you sure you want to change the base?
Changes from all commits
55fc57a
1159562
6d9f437
d7df709
a3e8634
25e4a16
9f6df6f
d277a66
3ba72ff
5900cf0
bc62c2b
5717c3b
e8925c6
7173bf3
f850013
fadc18d
047350b
c448778
7c3a31c
fcb2c17
6507bfd
332943c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow resample to use inverse sky variance when building the drizzle weight map. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ | |||||
from stcal.alignment.util import wcs_from_footprints | ||||||
|
||||||
from romancal.assign_wcs.utils import wcs_bbox_from_shape | ||||||
from romancal.datamodels.library import ModelLibrary | ||||||
|
||||||
log = logging.getLogger(__name__) | ||||||
log.setLevel(logging.DEBUG) | ||||||
|
@@ -111,7 +112,7 @@ | |||||
model : object | ||||||
The input model. | ||||||
weight_type : str, optional | ||||||
The type of weight to use. Allowed values are 'ivm' or 'exptime'. | ||||||
The type of weight to use. Allowed values are 'ivm', 'exptime', or 'ivsky'. | ||||||
mairanteodoro marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Defaults to None. | ||||||
good_bits : str, optional | ||||||
The good bits to use for building the mask. Defaults to None. | ||||||
|
@@ -163,6 +164,22 @@ | |||||
elif weight_type == "exptime": | ||||||
exptime = model.meta.exposure.exposure_time | ||||||
result = exptime * dqmask | ||||||
elif weight_type == "ivsky": | ||||||
if ( | ||||||
hasattr(model, "var_sky") | ||||||
and model.var_sky is not None | ||||||
and model.var_sky.shape == model.data.shape | ||||||
): | ||||||
with np.errstate(divide="ignore", invalid="ignore"): | ||||||
inv_sky_variance = model.var_sky**-1 | ||||||
inv_sky_variance[~np.isfinite(inv_sky_variance)] = 0 | ||||||
else: | ||||||
warnings.warn( | ||||||
"var_sky array is not available. Setting drizzle weight map to 1", | ||||||
stacklevel=2, | ||||||
) | ||||||
inv_sky_variance = 1.0 | ||||||
result = inv_sky_variance * dqmask | ||||||
mairanteodoro marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
elif weight_type is None: | ||||||
result = np.ones(model.data.shape, dtype=model.data.dtype) * dqmask | ||||||
else: | ||||||
|
@@ -402,3 +419,32 @@ | |||||
ymax = min(data_shape[0] - 1, int(y2 + 0.5)) | ||||||
|
||||||
return xmin, xmax, ymin, ymax | ||||||
|
||||||
|
||||||
def add_var_sky_array(input_models: ModelLibrary): | ||||||
""" | ||||||
Add sky variance array to each model of a ModelLibrary. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
input_models : ModelLibrary | ||||||
A library of models to which the sky variance array will be added. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
None | ||||||
""" | ||||||
with input_models: | ||||||
ref_img = input_models.borrow(index=0) | ||||||
input_models.shelve(model=ref_img, index=0) | ||||||
Comment on lines
+438
to
+439
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is ref_img used? |
||||||
for i, img in enumerate(input_models): | ||||||
try: | ||||||
ok_data = img.data != 0 | ||||||
img["var_sky"] = np.empty_like(img.data) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be simplified by starting with something like: img["var_sky"] = img.var_rnoise.copy() since all pixels are either:
|
||||||
img["var_sky"][ok_data] = img.var_rnoise[ok_data] + img.var_poisson[ | ||||||
ok_data | ||||||
] / img.data[ok_data] * np.median(img.data) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||||||
img["var_sky"][~ok_data] = img.var_rnoise[~ok_data] | ||||||
except (AttributeError, KeyError, TypeError, ValueError) as e: | ||||||
raise ValueError("Input model contains invalid data array.") from e | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this raises an exception (if for instance Also I suggest checking for |
||||||
input_models.shelve(img, i, modify=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ def create_image(self): | |
}, | ||
) | ||
# data from WFISim simulation of SCA #01 | ||
l2.data[:] = 0.01 | ||
l2.meta.filename = self.filename | ||
l2.meta["wcs"] = create_wcs_object_without_distortion( | ||
fiducial_world=self.fiducial_world, | ||
|
@@ -305,7 +306,7 @@ def test_resampledata_init(exposure_1): | |
pixfrac=pixfrac, | ||
kernel=kernel, | ||
fillval=fillval, | ||
wht_type=wht_type, | ||
weight_type=wht_type, | ||
good_bits=good_bits, | ||
pscale_ratio=pscale_ratio, | ||
pscale=pscale, | ||
|
@@ -694,15 +695,19 @@ def get_footprint(model, index): | |
) | ||
|
||
|
||
@pytest.mark.parametrize("weight_type", ["ivm", "exptime"]) | ||
@pytest.mark.parametrize("weight_type", ["ivm", "exptime", "ivsky"]) | ||
def test_resampledata_do_drizzle_default_single_exposure_weight_array( | ||
exposure_1, | ||
weight_type, | ||
): | ||
"""Test that resample methods return non-empty weight arrays.""" | ||
|
||
# adding a few zero flux pixels | ||
for i, model in enumerate(exposure_1): | ||
model.data[10 + i, 40 - i] = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you help me understand how setting pixels offset by the index of the exposure in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added a random pixel with zero flux. I was not interested in the position of the pixel, just that we did have a pixel with zero flux. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still confused by this. What's the need for a random zero flux pixel per image? If I comment out this code the test doesn't fail. |
||
|
||
input_models = ModelLibrary(exposure_1) | ||
resample_data = ResampleData(input_models, wht_type=weight_type) | ||
resample_data = ResampleData(input_models, weight_type=weight_type) | ||
|
||
output_models_many_to_one = resample_data.resample_many_to_one() | ||
output_models_many_to_many = resample_data.resample_many_to_many() | ||
|
@@ -712,6 +717,8 @@ def test_resampledata_do_drizzle_default_single_exposure_weight_array( | |
many_to_one_model = output_models_many_to_one.borrow(0) | ||
assert np.any(many_to_one_model.weight > 0) | ||
assert np.any(many_to_many_model.weight > 0) | ||
assert many_to_one_model.data[10, 40] == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
assert many_to_many_model.data[10, 40] == 0 | ||
output_models_many_to_many.shelve(many_to_many_model, 0, modify=False) | ||
output_models_many_to_one.shelve(many_to_one_model, 0, modify=False) | ||
|
||
|
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.
This will always re-compute
var_sky
(even if the input contains avar_sky
). If that's the goal (always have resample computevar_sky
) what about something like:weight
isivsky
computevar_sky
for each modelvar_sky
compute it (if it wasn't computed before)That way for non
ivsky
weighting resample won't need to make a separate pass through all the models here (inadd_var_sky_array
).