diff --git a/rework_summaries/tb-05-integrate-spectral-bins_changes.md b/rework_summaries/tb-05-integrate-spectral-bins_changes.md new file mode 100644 index 00000000..0baaad9a --- /dev/null +++ b/rework_summaries/tb-05-integrate-spectral-bins_changes.md @@ -0,0 +1,165 @@ +# tb-05-integrate-spectral-bins + +Puts the fork's spectral-sampling rework behind +`!SIM.spectral.integrate_spectral_bins`, defaulting to upstream behaviour. + +This is the largest and riskiest of the Tier B branches, because the fork +**deleted** the upstream code paths rather than branching around them. +Restoring them is most of the diff. + +## Why + +The fork replaced upstream's point sampling of spectra (`spec(fov_waveset)`, +`interp1d` onto the FOV grid) with flux-conserving bin integration. The physics +is better — a line narrower than the grid keeps its photons instead of being +sampled at whatever height it happens to have at a bin centre — but it moves +the flux of every instrument in the IRDB. Upstream cannot take that as a +default. `trace_flux_jacobian : False` in `defaults.yaml` is this fork's own +precedent for how such a change should ship. + +## One flag, not several + +The parts are not independently selectable. Every mixed setting is physically +wrong rather than merely different: + +- **`prepare_source` resampling x cube rebinning.** With upstream resampling on + *and* rebinning on, the cube is first point-sampled onto the global grid, + destroying narrow lines, and only then integrated — strictly worse than + either pure mode. +- **Edge-plane `f0`/`f1` scaling x rebinning.** Pre-scaling the first and last + planes turns them into partial-bin *counts*, but `rebin_spectral_density` + consumes a *density* and integrates each plane over its own width, so the + partial bin is counted twice. The opposite mix leaves the edge planes at full + height and over-counts band-edge flux. +- **`cube_waveset` padding x `extract_from` padding.** A padded output grid over + an unpadded extraction fills the pad with zeros — a fake flux cliff at both + band edges. Both are driven by the same `spectral_padding` value. +- **`average_spectrum` x cube rebinning** are mechanically separable, but + `test_photon_integrals_agree_between_representations` asserts that a table, an + image and a cube deliver the same photon rate. Splitting the flag would make + that invariant configuration-dependent, which is exactly what a config switch + should not be able to express. + +## Sites + +Declared once in `defaults.yaml`, read via a lazy property on the **base** +`FieldOfView` (not `FieldOfView3D` — `_extract_volume_from_cube` lives on the +base), following the `spline_order` idiom: bang-string read directly, no entry +in the `__init__` meta dict and none in `FOVManager.meta`. `sub_pixel`'s meta +entry exists because it is overridable per FOV; this is a global physics +switch. + +**`optical_train.py:608` first — it removes work elsewhere.** `spectral_padding` +is written in exactly one place, and `slit_spectral_width` only as a side +effect of the same call. Gating that single assignment leaves +`fov.meta['spectral_padding']` at `0*u.um`, which reduces five further sites to +upstream *by construction* with no edit at all, since `x + (-0.0) == x` +exactly. + +Then: + +- **`cube_waveset`** — early return giving upstream's grid. +- **`_extract_volume_from_cube`** — one named `density_input` intermediate, + read twice (mask margin, and the `f0`/`f1` edge scaling). +- **`_make_cubefields`** — `if/else` on the one expression that differs, + restoring `interp1d`. +- **The four `average_spectrum` call sites** — via a small + `_sampled_spectrum` helper. Note there are **four**, not three: the fourth is + in `make_hdu`'s deferred-PSF table branch, not in a `_make_*` method, and is + the one that gets missed. +- **`prepare_source`** — restores the per-spectrum resampling loop (deleted + outright by the fork, no counterpart) and the layer-by-layer cube + interpolation with its `CTYPE3`/`CRPIX3`/`CRVAL3`/`CDELT3`/`CUNIT3` rewrite + and the "wavesets disjoint" warning. Re-adds three imports: `interp1d`, + `SourceSpectrum, Empirical1D`, `SpectrumSourceField`. A shared + `_global_fov_waveset` helper builds the grid both places need. + +## The off path must be upstream's literal `arange` + +`start + np.arange(n)*step` is **not** `np.arange(start, stop, step)[:n]`. +Measured: + +| range | step | len(arange) | n_wave | max difference | +|---|---:|---:|---:|---:| +| 1.9 to 2.1 | 0.01 | 21 | 20 | 4.4e-16 um | +| 0.3 to 2.5 | 1e-5 | 220000 | 220000 | 2.2e-12 um | + +The first case shows the off-by-one guard dropping a whole bin, so getting this +wrong changes `NAXIS3`, not just the last ulp. Upstream's comment about "some +floating point madness" is preserved verbatim, and +`test_cube_waveset_matches_upstream_arange_grid` asserts with +`assert_array_equal`, not `allclose`, specifically to catch a future +"simplification". + +The early return also happens *before* the padding and step-refinement lines, +so stale meta cannot widen the grid even if something sets it. + +## Left unconditional on purpose + +These are correct on both paths and should not be gated: + +- The `.copy()` in `_extract_volume_from_cube`. Upstream mutated the caller's + source array in place via `data[0] *= f0`. That is a bug on any path; + `test_make_spectrum_from_cube` already asserts the source is unchanged. This + is the one place where "restore upstream" would mean "restore a bug". +- `abs(wdel)` in the mask. Bit-identical for an ascending cube; upstream's raw + `0.5 * wdel` is simply broken for a descending one. +- float64 for cube data. A 1e-8 contrast cannot survive float32, which + `test_prepare_cube_retains_faint_contrast_in_double_precision` asserts. This + does change METIS/MICADO cube results at roughly the 1e-7 relative level and + should be called out as a deliberate exception in any upstream PR. +- The `CDELT3`/`CRVAL3` header written by `_make_cubefields`. In both branches + the data lies on `fov_waveset`, so the WCS is correct either way; upstream + simply left a stale third axis on the yielded HDU. + +## Tests + +Opt-ins, three edits covering everything: + +- `physical_fov()` in `test_slit_spatial_integration.py` gains the key. Because + `test_adc_slit_illumination.py` imports that helper, one line covers both + files. +- `test_FieldOfView.py`'s cross-representation test gains the key in its + `UserCommands`, plus the new `integrate_spectral_bins` fixture — it calls + `OpticalTrain().prepare_source(...)` *before* building those commands, so it + straddles both modes without the fixture. +- A function-scoped fixture in `conftest.py` sets the key in + `rc.__currsys__` and restores it. Needed because `prepare_source` takes no + `cmds` and `OpticalTrain(cmds)` would require a real instrument package. + +New tests: the bit-exact grid above; that padding is ignored when the flag is +off (guards the early-return ordering) and applied when on; that a narrow line +*is lost* under point sampling, so a regression in the restored path is caught +rather than silently "improved"; and that `prepare_source` resamples onto the +global grid by default. + +**Deleted:** `test_reuses_cached_spectrum_waveset`, which referenced +`fov_mod._SPECTRUM_WAVESET_AA_CACHE` — a module attribute that exists nowhere +in the tree, so the test *errored* rather than failed. The cache is +deliberately gone: a `SourceSpectrum` can re-sample itself in place through +`z`, which `test_mutating_spectrum_redshift_updates_native_sampling` asserts. + +## What this does not fix + +The two `test_basic_instrument` flux regressions are **unchanged** by this flag +(still 11 vs 7 and 78 vs 75). They come from the unflagged *spatial* projection +rework — `project_spatial_density` replacing `add_imagehdu_to_imagehdu` in +`_make_imagefields`, and the PSF-deferred rendering path. Full parity with +upstream needs that gated too, which is separate work. + +Three further unflagged divergences sit inside functions this branch touches +and will be visible to any reviewer diffing against upstream: +`int(np.floor(xpix + .5))` vs `int(xpix)`, the `project_spatial_density` +rewiring, and `FieldOfView1D._make_cubefields`'s surface-brightness unit fix. +Each changes default results and each needs its own story before this goes +upstream. + +## Verification + +`scopesim/tests`: 3 failed, 1382 passed — one *below* the `main` baseline of 4, +because the stale cache test is gone. + +End to end, with the companion irdb branch applied, a ZShooter SPEC observation +is **bit-identical** to `main`: all six image planes match on SHA-256. Without +the irdb opt-in the same run shifts +3.1% in total flux and +24% in peak, so +the two must merge together. diff --git a/scopesim/defaults.yaml b/scopesim/defaults.yaml index 29efb05a..cfb0013b 100644 --- a/scopesim/defaults.yaml +++ b/scopesim/defaults.yaml @@ -14,6 +14,7 @@ properties : spectral_bin_width : !!float 1E-4 spectral_resolution: 5000 trace_flux_jacobian : False + integrate_spectral_bins : False minimum_throughput : !!float 1E-6 minimum_pixel_flux : 1 diff --git a/scopesim/optics/fov.py b/scopesim/optics/fov.py index 3f56c4e2..d7d8d336 100644 --- a/scopesim/optics/fov.py +++ b/scopesim/optics/fov.py @@ -8,6 +8,7 @@ import numpy as np from scipy.integrate import cumulative_trapezoid +from scipy.interpolate import interp1d from scipy.signal import fftconvolve from astropy import units as u @@ -409,11 +410,14 @@ def _extract_volume_from_cube(self, cubehdu, new_hdr, xy0p, xy1p): wdel = hdr["CDELT3"] * u.Unit(hdr.get("CUNIT3", "AA")) fov_wmin, fov_wmax = self.waverange + np.array([-1, 1])*self.meta['spectral_padding'] + # Spectroscopic inputs stay densities until bin integration, so they + # need neighbours on both sides of the output bin edges and must not + # be pre-scaled for partial coverage. Everything else keeps the + # upstream half-bin margin and edge-slice scaling. + density_input = isinstance(self, FieldOfView3D) and self.integrate_spectral_bins # need to go [+/-] half a bin margin = abs(wdel)/2 - if isinstance(self, FieldOfView3D): - # Spectroscopic inputs remain densities until bin integration; - # retain neighbours on both sides of the output bin edges. + if density_input: margin = abs(wdel) + np.diff(self.cube_waveset[:2])[0]/2 mask = (hdu_waves > fov_wmin - margin) & (hdu_waves <= fov_wmax + margin) @@ -431,7 +435,7 @@ def _extract_volume_from_cube(self, cubehdu, new_hdr, xy0p, xy1p): f1 = (abs(hdu_waves[i1p] - fov_wmax - 0.5 * wdel) % wdel) / wdel # red edge data = cubehdu.data[i0p:i1p+1, xy0p[1]:xy1p[1], xy0p[0]:xy1p[0]].copy() - if not isinstance(self, FieldOfView3D): + if not density_input: data[0] *= f0.to_value(1).round(11) if i1p > i0p: data[-1] *= f1.to_value(1).round(11) @@ -530,6 +534,12 @@ def sub_pixel(self) -> bool: # TODO: Make this an attribute once meta resolving is implemented. return from_currsys(self.meta["sub_pixel"], self.cmds) + @property + def integrate_spectral_bins(self) -> bool: + """Return "!SIM.spectral.integrate_spectral_bins" from cmds.""" + # TODO: Make this an attribute once meta resolving is implemented. + return from_currsys("!SIM.spectral.integrate_spectral_bins", self.cmds) + @property def spline_order(self) -> int: """Return "!SIM.computing.spline_order" from cmds.""" @@ -930,12 +940,27 @@ class FieldOfView3D(FieldOfView): def cube_waveset(self): wave_unit = u.Unit(from_currsys('!SIM.spectral.wave_unit', self.cmds)) step = from_currsys('!SIM.spectral.spectral_bin_width', self.cmds) + if not self.integrate_spectral_bins: + start, stop = self.meta['wave_min'].value, self.meta['wave_max'].value + # Note: There is an edge case where some floating point madness + # results in the arange ticking over by another bin by just + # .000000000005 (yeah...), which creates an off-by-one error + # further down the line. + n_wave = int((stop - start) / step) + return np.arange(start, stop, step)[:n_wave] * wave_unit + if 'slit_spectral_width' in self.meta: step = min(step, self.meta['slit_spectral_width'].to_value(wave_unit)/20) start, stop = self.meta['wave_min'].value, self.meta['wave_max'].value padding = int(np.ceil(self.meta['spectral_padding'].to_value(wave_unit)/step)) return (start + np.arange(-padding, int((stop - start)/step) + padding)*step)*wave_unit + def _sampled_spectrum(self, spectrum, fov_waveset): + """PHOTLAM at the cube cells: bin integrals, or upstream bin-centre samples.""" + if self.integrate_spectral_bins: + return average_spectrum(spectrum, fov_waveset) + return spectrum(fov_waveset) + def _make_cubefields(self, fov_waveset): """ Find Cube fields. @@ -949,7 +974,11 @@ def _make_cubefields(self, fov_waveset): # Assumption is that ImageHDUs have units of PHOTLAM arcsec-2 # TODO: Deal with this bounds_error in a more elegant way - field_data = rebin_spectral_density(field.waveset.to_value(u.um), field.data, spectral_bin_edges(fov_waveset.to_value(u.um))) + if self.integrate_spectral_bins: + field_data = rebin_spectral_density(field.waveset.to_value(u.um), field.data, spectral_bin_edges(fov_waveset.to_value(u.um))) + else: + # Upstream point-samples the native cube onto the FOV grid. + field_data = interp1d(field.waveset.to_value(u.um), field.data, axis=0, kind="linear", bounds_error=False, fill_value=0)(fov_waveset.to_value(u.um)) field_hdu = fits.ImageHDU(data=field_data, header=field.header) field_hdu.header.update(CRPIX3=1., CRVAL3=fov_waveset[0].to_value(u.um), CDELT3=np.diff(fov_waveset[:2])[0].to_value(u.um), CUNIT3='um', CTYPE3='WAVE') yield field_hdu @@ -979,7 +1008,7 @@ def _make_imagefields(self, fov_waveset, spline_order, spatial_psf=None): py = (padded['NAXIS2'] - self.header['NAXIS2'])//2 image = image[py:py + self.header['NAXIS2'], px:px + self.header['NAXIS1']] - spec = average_spectrum(field.spectrum, fov_waveset) + spec = self._sampled_spectrum(field.spectrum, fov_waveset) # 2D * 1D -> 3D field_cube = image[None, :, :] * spec[:, None, None] yield field_cube.value @@ -1002,7 +1031,7 @@ def _make_tablefields(self, fov_waveset): # if this is the case and if yes, remove the condition here!! # Fluxes (yes that's the correct plural) in PHOTLAM fluxes = { - ref: average_spectrum(spec, fov_waveset) + ref: self._sampled_spectrum(spec, fov_waveset) for ref, spec in field.spectra.items() if ref in field.field["ref"] } @@ -1029,7 +1058,7 @@ def _make_backfields(self, fov_waveset): # Remember, cube output needs PHOTLAM arcsec-2 ! # So if SOLIDANG or BUNIT or whatever is not in arcsec-2, # the spectrum shoule be scaled accordingly! - spec = average_spectrum(field.spectrum, fov_waveset) + spec = self._sampled_spectrum(field.spectrum, fov_waveset) yield spec[:, None, None].value def make_hdu(self, spatial_psf=None, spatial_shifts=()): @@ -1118,7 +1147,7 @@ def make_hdu(self, spatial_psf=None, spatial_shifts=()): canvas_cube_hdu.data[:, y, x] += flux else: for field in self._get_table_fields(): - spectra = {ref: average_spectrum(spec, fov_waveset).value for ref, spec in field.spectra.items()} + spectra = {ref: self._sampled_spectrum(spec, fov_waveset).value for ref, spec in field.spectra.items()} for row in field.field: image = spatial_psf.point_source_image(self, row['x'], row['y'])/self.pixel_area.value for start in range(0, len(fov_waveset), 64): diff --git a/scopesim/optics/optical_train.py b/scopesim/optics/optical_train.py index ebad53f7..09ea1e07 100644 --- a/scopesim/optics/optical_train.py +++ b/scopesim/optics/optical_train.py @@ -13,6 +13,9 @@ from tqdm.auto import tqdm +from scipy.interpolate import interp1d + +from synphot import SourceSpectrum, Empirical1D from synphot.units import PHOTLAM from astar_utils.nested_mapping import RecursiveNestedMapping, is_bangkey, recursive_update @@ -28,6 +31,7 @@ from ..effects.atmo_dispersion import ShiftFoV3D from ..utils import from_currsys, top_level_catch, get_logger from ..source.source_templates import empty_sky +from ..source.source_fields import SpectrumSourceField from .. import __version__ @@ -539,6 +543,7 @@ def observe(self, orig_source=None, update=True, **kwargs): else: source = orig_source.make_copy() source = self.prepare_source(source) + integrate_bins = from_currsys("!SIM.spectral.integrate_spectral_bins", self.cmds) # [1D - transmission curves] source_effects = self.optics_manager.source_effects @@ -604,7 +609,7 @@ def observe(self, orig_source=None, update=True, **kwargs): for effect in foveffs: # Slit shear needs spectral support before extraction. # Fibre bundles have no single FOV trace and map separately. - if isinstance(effect, SpectralTraceList) and isinstance(fov, FieldOfView3D) and fov.trace_id is not None: + if integrate_bins and isinstance(effect, SpectralTraceList) and isinstance(fov, FieldOfView3D) and fov.trace_id is not None: fov.meta['spectral_padding'] = effect.spectral_traces[fov.trace_id].spectral_padding(fov) # Render at the PSF stage when preceding effects only multiply @@ -674,6 +679,14 @@ def observe(self, orig_source=None, update=True, **kwargs): self._last_fovs = fovs self._last_source = source + def _global_fov_waveset(self): + """The single wavelength grid upstream resamples every source onto.""" + wave_min = min(fov.meta["wave_min"] for fov in self.fov_manager.fovs) + wave_max = max(fov.meta["wave_max"] for fov in self.fov_manager.fovs) + wave_unit = u.Unit(from_currsys("!SIM.spectral.wave_unit", self.cmds)) + dwave = from_currsys("!SIM.spectral.spectral_bin_width", self.cmds) + return (np.arange(wave_min.value, wave_max.value, dwave) * wave_unit).to(u.um) + def prepare_source(self, source): """ Prepare source for observation. @@ -691,6 +704,18 @@ def prepare_source(self, source): # Convert to PHOTLAM per arcsec2 # TODO: this is not sufficiently general # TODO: Maybe move this to source_fields?? + integrate_bins = from_currsys("!SIM.spectral.integrate_spectral_bins", self.cmds) + + if not integrate_bins: + # Upstream puts every spectrum on the global FOV grid up front. + for field in source.fields: + if not isinstance(field, SpectrumSourceField): + continue + for ispec, spec in field.spectra.items(): + fov_waveset = self._global_fov_waveset() + field.spectra[ispec] = SourceSpectrum(Empirical1D, + points=fov_waveset, + lookup_table=spec(fov_waveset)) for cube in source.cube_fields: header, data, wave = cube.header, cube.data, cube.waveset @@ -726,10 +751,33 @@ def prepare_source(self, source): cube.header["CUNIT1"] = "deg" cube.header["CUNIT2"] = "deg" - # Keep the native wavelength samples until the spatial FOV is cut. - # Rebinning the entire source here loses narrow lines and expands - # resolved cubes over the full instrument band unnecessarily. - cube.data = data + if integrate_bins: + # Keep the native wavelength samples until the spatial FOV is + # cut. Rebinning the entire source here loses narrow lines and + # expands resolved cubes over the full instrument band + # unnecessarily. + cube.data = data + else: + fov_waveset = self._global_fov_waveset() + if (wave.to(u.um).min() > fov_waveset.max() + or wave.to(u.um).max() < fov_waveset.min()): + logger.warning("Source and FOV wavesets disjoint.") + + # Interpolate into new data cube. + # This is done layer by layer for memory reasons. + new_data = np.zeros((fov_waveset.shape[0], data.shape[1], data.shape[2]), dtype=np.float64) + for j in range(data.shape[1]): + cube_interp = interp1d(wave.to_value(u.um), data[:, j, :], + axis=0, kind="linear", bounds_error=False, fill_value=0) + new_data[:, j, :] = cube_interp(fov_waveset.value) + + cube.data = new_data + cube.header["CTYPE3"] = "WAVE" + cube.header["CRPIX3"] = 1 + cube.header["CRVAL3"] = fov_waveset[0].value + cube.header["CDELT3"] = from_currsys("!SIM.spectral.spectral_bin_width", self.cmds) + cube.header["CUNIT3"] = "um" + cube.wcs = WCS(cube.field) return source diff --git a/scopesim/tests/conftest.py b/scopesim/tests/conftest.py index 705fd5aa..f5956c6f 100644 --- a/scopesim/tests/conftest.py +++ b/scopesim/tests/conftest.py @@ -121,3 +121,13 @@ def protect_search_path(): """Prevent modification of global search_path.""" with patch("scopesim.rc.__search_path__"): yield + + +@pytest.fixture(scope="function") +def integrate_spectral_bins(): + """Opt in to bin-integrated spectral sampling for a single test.""" + key = "!SIM.spectral.integrate_spectral_bins" + previous = sim.rc.__currsys__[key] + sim.rc.__currsys__[key] = True + yield + sim.rc.__currsys__[key] = previous diff --git a/scopesim/tests/tests_optics/test_FieldOfView.py b/scopesim/tests/tests_optics/test_FieldOfView.py index bab799ad..ffe15737 100644 --- a/scopesim/tests/tests_optics/test_FieldOfView.py +++ b/scopesim/tests/tests_optics/test_FieldOfView.py @@ -384,7 +384,10 @@ def test_makes_spectrum_from_all_types_of_source_object(self): class TestMakeSpectrumImageCubeAllPlayNicely: @pytest.mark.parametrize("kind", ["table", "image", "cube", "two_cubes", "mixed"]) @pytest.mark.parametrize("spectral_step", [.01, .005]) - def test_photon_integrals_agree_between_representations(self, kind, spectral_step): + def test_photon_integrals_agree_between_representations(self, kind, spectral_step, + integrate_spectral_bins): + # The invariant that a table, an image and a cube deliver the same + # photon rate only holds on the bin-integrated path. from synphot import SourceSpectrum, Empirical1D from scopesim.commands import UserCommands from scopesim.optics.optical_train import OpticalTrain @@ -411,7 +414,8 @@ def test_photon_integrals_agree_between_representations(self, kind, spectral_ste expected = spatial_weight[kind]*np.trapezoid(profile, wave.to_value(u.um)) source = OpticalTrain().prepare_source(source) - commands = UserCommands(properties={'!SIM.spectral.spectral_bin_width': spectral_step}) + commands = UserCommands(properties={'!SIM.spectral.spectral_bin_width': spectral_step, + '!SIM.spectral.integrate_spectral_bins': True}) rates = [] for cls in (FieldOfView1D, FieldOfView2D, FieldOfView3D): fov = cls(ho._fov_header(), [1.9, 2.1]*u.um, area=1*u.m**2, cmds=commands) diff --git a/scopesim/tests/tests_optics/test_fov_utls.py b/scopesim/tests/tests_optics/test_fov_utls.py index dd0821dd..5a782a9b 100644 --- a/scopesim/tests/tests_optics/test_fov_utls.py +++ b/scopesim/tests/tests_optics/test_fov_utls.py @@ -117,29 +117,6 @@ def test_extracts_the_wave_range_needed(self): assert new_spec.waverange[0] == 1.98 * u.um assert new_spec(1.98 * u.um).value == approx(12.8) - def test_reuses_cached_spectrum_waveset(self, monkeypatch): - from scopesim.optics import fov as fov_mod - - fov_mod._SPECTRUM_WAVESET_AA_CACHE.clear() - wave = np.arange(0.7, 2.5, 0.1) * u.um - flux = np.arange(len(wave)) * PHOTLAM - spec = SourceSpectrum(Empirical1D, points=wave, lookup_table=flux) - original_waveset = SourceSpectrum.waveset.fget - calls = 0 - - def counting_waveset(self): - nonlocal calls - if self is spec: - calls += 1 - return original_waveset(self) - - monkeypatch.setattr(SourceSpectrum, "waveset", property(counting_waveset)) - - extract_range_from_spectrum(spec, [1.98, 2.12] * u.um) - extract_range_from_spectrum(spec, [1.90, 2.00] * u.um) - - assert calls == 1 - @pytest.mark.parametrize(("endpoint", "msg"), [pytest.param(1.5, "Waverange does not overlap", marks=pytest.mark.xfail(reason="Check was disabled in function, dunno why.")), (2.05, "Waverange only partially overlaps")]) diff --git a/scopesim/tests/tests_optics/test_slit_spatial_integration.py b/scopesim/tests/tests_optics/test_slit_spatial_integration.py index 2a12e4ba..0d2a5f41 100644 --- a/scopesim/tests/tests_optics/test_slit_spatial_integration.py +++ b/scopesim/tests/tests_optics/test_slit_spatial_integration.py @@ -63,7 +63,8 @@ def physical_fov(width=.33): header = slit_fov(width).header detector_wcs, _ = det_wcs_from_sky_wcs(WCS(header), .1570545, 10.) header.update(detector_wcs.to_header()) - cmds = UserCommands(properties={'!SIM.spectral.spectral_bin_width': .01}) + cmds = UserCommands(properties={'!SIM.spectral.spectral_bin_width': .01, + '!SIM.spectral.integrate_spectral_bins': True}) return FieldOfView3D(header, [.5, .6], cmds=cmds, area=1*u.cm**2) diff --git a/scopesim/tests/tests_optics/test_spectral_bin_integration.py b/scopesim/tests/tests_optics/test_spectral_bin_integration.py index a037bb5b..df21e9c2 100644 --- a/scopesim/tests/tests_optics/test_spectral_bin_integration.py +++ b/scopesim/tests/tests_optics/test_spectral_bin_integration.py @@ -57,7 +57,7 @@ def test_cube_rebinning_preserves_flux_spatial_weights_and_support(descending): np.testing.assert_allclose(rebinned.sum(axis=0), 4*weights) -def test_prepare_source_keeps_native_line_sampling(): +def test_prepare_source_keeps_native_line_sampling(integrate_spectral_bins): wave = np.linspace(4999.9, 5000.1, 101)*u.AA spectrum = SourceSpectrum(Empirical1D, points=wave, lookup_table=np.exp(-((wave.value - 5000)/.01)**2)*PHOTLAM) source = Source(x=[0.], y=[0.], ref=[0], weight=[1.], spectra=[spectrum]) @@ -66,7 +66,7 @@ def test_prepare_source_keeps_native_line_sampling(): np.testing.assert_array_equal(prepared.spectra[0].waveset, spectrum.waveset) -def test_prepare_cube_retains_faint_contrast_in_double_precision(): +def test_prepare_cube_retains_faint_contrast_in_double_precision(integrate_spectral_bins): from scopesim.tests.mocks.py_objects.source_objects import _cube_source source = _cube_source() field = source.cube_fields[0] @@ -76,3 +76,84 @@ def test_prepare_cube_retains_faint_contrast_in_double_precision(): data = prepared.cube_fields[0].data contrast = (data[:, 0, 0] - data[:, 0, 1])/data[:, 0, 1] np.testing.assert_allclose(contrast, 1e-8, rtol=1e-6) + + +def _cube_fov(wave_min, wave_max, step, integrate, **meta): + from astropy.wcs import WCS + from scopesim.commands import UserCommands + from scopesim.optics.fov import FieldOfView3D + from scopesim.optics.image_plane_utils import det_wcs_from_sky_wcs + from scopesim.tests.tests_optics.test_slit_spatial_integration import slit_fov + header = slit_fov(.33).header + detector_wcs, _ = det_wcs_from_sky_wcs(WCS(header), .1570545, 10.) + header.update(detector_wcs.to_header()) + cmds = UserCommands(properties={'!SIM.spectral.spectral_bin_width': step, + '!SIM.spectral.integrate_spectral_bins': integrate}) + fov = FieldOfView3D(header, [wave_min, wave_max], cmds=cmds, area=1*u.cm**2) + fov.meta.update(meta) + return fov + + +@pytest.mark.parametrize('wave_min, wave_max, step', [(1.9, 2.1, .01), (.3, 2.5, 1e-5)]) +def test_cube_waveset_matches_upstream_arange_grid(wave_min, wave_max, step): + # Must be the literal arange, not start + arange(n)*step: the two differ + # at 4e-16 for the first case and 2e-12 for the second, and the truncation + # drops a whole bin (21 samples from arange, n_wave of 20). + fov = _cube_fov(wave_min, wave_max, step, integrate=False) + expected = np.arange(wave_min, wave_max, step)[:int((wave_max - wave_min)/step)] + np.testing.assert_array_equal(fov.cube_waveset.to_value(u.um), expected) + + +def test_cube_waveset_ignores_padding_when_not_integrating(): + # The upstream grid is returned before padding or step refinement are + # consulted, so stale meta cannot widen it. + plain = _cube_fov(1.9, 2.1, .01, integrate=False) + padded = _cube_fov(1.9, 2.1, .01, integrate=False, + spectral_padding=.05*u.um, slit_spectral_width=.002*u.um) + np.testing.assert_array_equal(padded.cube_waveset, plain.cube_waveset) + + +def test_cube_waveset_pads_and_refines_when_integrating(): + plain = _cube_fov(1.9, 2.1, .01, integrate=True) + padded = _cube_fov(1.9, 2.1, .01, integrate=True, + spectral_padding=.05*u.um, slit_spectral_width=.002*u.um) + assert padded.cube_waveset.min() < plain.cube_waveset.min() + assert padded.cube_waveset.max() > plain.cube_waveset.max() + # slit_spectral_width/20 is finer than the configured bin width + assert np.diff(padded.cube_waveset[:2])[0] < np.diff(plain.cube_waveset[:2])[0] + + +def test_narrow_line_is_point_sampled_when_not_integrating(): + # Documents the upstream behaviour the flag restores: a line narrower than + # the grid keeps only the photons that land on a sample point, so its + # recovered flux depends on where the line sits between grid points. + wave = np.linspace(1.999, 2.001, 20001)*u.um + line = np.exp(-((wave.value - 2.00045)/2e-5)**2)*PHOTLAM + spectrum = SourceSpectrum(Empirical1D, points=wave, lookup_table=line) + grid = np.arange(1.999, 2.001, 1e-4)*u.um + step = 1e-4 + + truth = np.trapezoid(line.value, wave.value) + sampled = spectrum(grid).value.sum()*step + integrated = average_spectrum(spectrum, grid).value.sum()*step + + assert integrated == pytest.approx(truth, rel=.01) + assert sampled < truth/2 + + +def test_prepare_source_resamples_spectra_onto_fov_grid_by_default(): + from types import SimpleNamespace + from scopesim.commands import UserCommands + wave = np.linspace(4999.9, 5000.1, 101)*u.AA + spectrum = SourceSpectrum(Empirical1D, points=wave, lookup_table=np.exp(-((wave.value - 5000)/.01)**2)*PHOTLAM) + source = Source(x=[0.], y=[0.], ref=[0], weight=[1.], spectra=[spectrum]) + + train = OpticalTrain() + train.cmds = UserCommands(properties={'!SIM.spectral.spectral_bin_width': 1e-4, + '!SIM.spectral.integrate_spectral_bins': False}) + train.fov_manager = SimpleNamespace(fovs=[SimpleNamespace( + meta={"wave_min": .4999*u.um, "wave_max": .5001*u.um})]) + prepared = train.prepare_source(source) + + expected = np.arange(.4999, .5001, 1e-4) + np.testing.assert_allclose(prepared.spectra[0].waveset.to_value(u.um), expected, rtol=1e-12)