AstarVienna / ScopeSim

A telescope observation simulator for Python.
GNU General Public License v3.0
16 stars 10 forks source link

Replace NaNs in images, log warning #466

Closed teutoburg closed 2 months ago

teutoburg commented 2 months ago

Closes #419

Simple example to illustrate:

import numpy as np
from matplotlib import pyplot as plt
import scopesim as sim
import scopesim_templates as sim_tp

src=sim_tp.galaxy("brown/NGC5866")
src.fields[0].data[70:80, 70:80] = np.nan
np.isnan(src.fields[0].data).sum()

sim.download_packages(["ELT", "Armazones", "MICADO"])
cmds=sim.UserCommands(use_instrument="MICADO")
opt=sim.OpticalTrain(cmds)

opt.observe(src)
plt.imshow(opt.readout()[0][1].data)

shows:

hole

and prints:

astar.scopesim.optics.fov - WARNING: Source contained NaN values, which were replaced by 0.000000.
astar.scopesim.optics.fov - The replacement value for NaNs in sources can be set in '!SIM.computing.nan_fill_value'.

The resulting image has a clear hole in the middle where the NaNs were, but still has some noise in there, as would be expected when setting the pixel values in the source to zero.

Setting the fill value to something intentionally ridiculously high: cmds["!SIM.computing.nan_fill_value"] = 1e20 results in:

brighthole

Also added a test with a similar setup to at least run that new code and make sure no NaNs remain.

I picked this particular place ScopeSim to put this because that's where the actually used part of the image is created. Any NaNs outside the FOV can be ignored anyway as they don't affect the final result. Note that this now applies only to ImageHDU-type source fields, so Tables are not checked. However, one would have to be particularly dense to create a source table with NaNs, I guess. Most of the NaN cases will come from users loading mythical FITS files from God-knows-where...

codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 75.99%. Comparing base (1f8c746) to head (eb7ed1e). Report is 4 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #466 +/- ## ========================================== + Coverage 75.97% 75.99% +0.02% ========================================== Files 64 64 Lines 7820 7828 +8 ========================================== + Hits 5941 5949 +8 Misses 1879 1879 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.