From cdf8100970272edcc56d5483bae1ef4a19c29812 Mon Sep 17 00:00:00 2001 From: teutoburg Date: Mon, 11 Sep 2023 15:52:49 +0200 Subject: [PATCH 1/4] Use dict.values() and enumerate for iterating --- scopesim/effects/metis_lms_trace_list.py | 4 ++-- scopesim/effects/psfs.py | 8 ++++---- scopesim/effects/spectral_trace_list.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/scopesim/effects/metis_lms_trace_list.py b/scopesim/effects/metis_lms_trace_list.py index 935eba48..7a4422b9 100644 --- a/scopesim/effects/metis_lms_trace_list.py +++ b/scopesim/effects/metis_lms_trace_list.py @@ -65,8 +65,8 @@ def apply_to(self, obj, **kwargs): if isinstance(obj, FOVSetupBase): # Create a single volume that covers the aperture and # the maximum wavelength range of LMS - volumes = [self.spectral_traces[key].fov_grid() - for key in self.spectral_traces] + volumes = [spectral_trace.fov_grid() + for spectral_trace in self.spectral_traces.values()] wave_min = min(vol["wave_min"] for vol in volumes) wave_max = max(vol["wave_max"] for vol in volumes) extracted_vols = obj.extract(axes=["wave"], diff --git a/scopesim/effects/psfs.py b/scopesim/effects/psfs.py index 10c74728..003de243 100644 --- a/scopesim/effects/psfs.py +++ b/scopesim/effects/psfs.py @@ -821,11 +821,11 @@ def get_kernel(self, fov): # rescale the pixel scale of the kernel to match the fov images pix_ratio = fov_pixel_scale / kernel_pixel_scale if abs(pix_ratio - 1) > self.meta["flux_accuracy"]: - for ii in range(len(self.kernel)): - self.kernel[ii][0] = pu.rescale_kernel(self.kernel[ii][0], pix_ratio) + for ii, kern in enumerate(self.kernel): + self.kernel[ii][0] = pu.rescale_kernel(kern[0], pix_ratio) - for i in range(len(self.kernel)): - self.kernel[i][0] /= np.sum(self.kernel[i][0]) + for i, kern in enumerate(self.kernel): + self.kernel[i][0] /= np.sum(kern[0]) return self.kernel diff --git a/scopesim/effects/spectral_trace_list.py b/scopesim/effects/spectral_trace_list.py index 829b33f3..2a9f92a3 100644 --- a/scopesim/effects/spectral_trace_list.py +++ b/scopesim/effects/spectral_trace_list.py @@ -156,8 +156,8 @@ def apply_to(self, obj, **kwargs): """ if isinstance(obj, FOVSetupBase): # Setup of FieldOfView object - volumes = [self.spectral_traces[key].fov_grid() - for key in self.spectral_traces] + volumes = [spectral_trace.fov_grid() + for spectral_trace in self.spectral_traces.values()] new_vols_list = [] for vol in volumes: wave_edges = [vol["wave_min"], vol["wave_max"]] From 8f508011cbcad483a29567b24d6a486eaa10f034 Mon Sep 17 00:00:00 2001 From: teutoburg Date: Mon, 11 Sep 2023 15:53:33 +0200 Subject: [PATCH 2/4] Use dict.get() instead of if ... in ... --- scopesim/effects/psfs.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scopesim/effects/psfs.py b/scopesim/effects/psfs.py index 003de243..94bd1764 100644 --- a/scopesim/effects/psfs.py +++ b/scopesim/effects/psfs.py @@ -115,7 +115,6 @@ def apply_to(self, obj, **kwargs): return obj - def fov_grid(self, which="waveset", **kwargs): """See parent docstring.""" waveset = [] @@ -124,10 +123,11 @@ def fov_grid(self, which="waveset", **kwargs): _waveset = self._waveset waves = 0.5 * (np.array(_waveset)[1:] + np.array(_waveset)[:-1]) - wave_min = kwargs["wave_min"] if "wave_min" in kwargs else np.min(_waveset) - wave_max = kwargs["wave_max"] if "wave_max" in kwargs else np.max(_waveset) + wave_min = kwargs.get("wave_min", np.min(_waveset)) + wave_max = kwargs.get("wave_max", np.max(_waveset)) mask = (wave_min < waves) * (waves < wave_max) - waveset = np.unique([wave_min] + list(waves[mask]) + [wave_max]) + waveset = np.unique([wave_min] + list(waves[mask]) + + [wave_max]) return waveset From 4d1d616d1bb024786826a00085069af5e7135bc0 Mon Sep 17 00:00:00 2001 From: teutoburg Date: Mon, 11 Sep 2023 15:53:49 +0200 Subject: [PATCH 3/4] better bools --- scopesim/effects/detector_list.py | 4 ++-- scopesim/optics/image_plane_utils.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scopesim/effects/detector_list.py b/scopesim/effects/detector_list.py index 0820e3c1..812c8081 100644 --- a/scopesim/effects/detector_list.py +++ b/scopesim/effects/detector_list.py @@ -129,9 +129,9 @@ def __init__(self, **kwargs): if col in self.table.colnames: self.table[col] = self.table[col] * mult_cols[col] self.table.rename_column(col, new_name) - if not "x_size_unit" in self.meta and "xhw_unit" in self.meta: + if "x_size_unit" not in self.meta and "xhw_unit" in self.meta: self.meta["x_size_unit"] = self.meta["xhw_unit"] - if not "y_size_unit" in self.meta and "yhw_unit" in self.meta: + if "y_size_unit" not in self.meta and "yhw_unit" in self.meta: self.meta["y_size_unit"] = self.meta["yhw_unit"] def apply_to(self, obj, **kwargs): diff --git a/scopesim/optics/image_plane_utils.py b/scopesim/optics/image_plane_utils.py index e1051179..3bcf9394 100644 --- a/scopesim/optics/image_plane_utils.py +++ b/scopesim/optics/image_plane_utils.py @@ -264,7 +264,7 @@ def add_table_to_imagehdu(table: Table, canvas_hdu: fits.ImageHDU, eps = -1e-7 mask = (xpix >= eps) * (xpix < naxis1) * (ypix >= eps) * (ypix < naxis2) - if sub_pixel is True: + if sub_pixel: canvas_hdu = _add_subpixel_sources_to_canvas(canvas_hdu, xpix, ypix, f, mask) else: From 57f84f07297b0f2da56e74e4ecfe2949255b25df Mon Sep 17 00:00:00 2001 From: teutoburg Date: Mon, 11 Sep 2023 15:56:53 +0200 Subject: [PATCH 4/4] Use f-strings and empty iterable is False --- scopesim/optics/image_plane_utils.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/scopesim/optics/image_plane_utils.py b/scopesim/optics/image_plane_utils.py index 3bcf9394..a6f006a1 100644 --- a/scopesim/optics/image_plane_utils.py +++ b/scopesim/optics/image_plane_utils.py @@ -468,10 +468,9 @@ def rescale_imagehdu(imagehdu: fits.ImageHDU, pixel_scale: float, imagehdu : fits.ImageHDU """ - wcs_suffix = wcs_suffix - s0 = wcs_suffix[0] if len(wcs_suffix) > 0 else "" - cdelt1 = imagehdu.header["CDELT1"+s0] - cdelt2 = imagehdu.header["CDELT2"+s0] + s0 = wcs_suffix[0] if wcs_suffix else "" + cdelt1 = imagehdu.header[f"CDELT1{s0}"] + cdelt2 = imagehdu.header[f"CDELT2{s0}"] # making sure that zoom1,zoom2 are positive zoom1 = np.abs(cdelt1 / pixel_scale) @@ -494,13 +493,13 @@ def rescale_imagehdu(imagehdu: fits.ImageHDU, pixel_scale: float, imagehdu.data = new_im for ii in range(max(1, len(wcs_suffix))): - si = wcs_suffix[ii] if len(wcs_suffix) > 0 else "" - imagehdu.header["CRPIX1"+si] *= zoom1 - imagehdu.header["CRPIX2"+si] *= zoom2 - imagehdu.header["CDELT1"+si] = pixel_scale - imagehdu.header["CDELT2"+si] = pixel_scale - imagehdu.header["CUNIT1"+si] = "mm" if si == "D" else "deg" - imagehdu.header["CUNIT2"+si] = "mm" if si == "D" else "deg" + si = wcs_suffix[ii] if wcs_suffix else "" + imagehdu.header[f"CRPIX1{si}"] *= zoom1 + imagehdu.header[f"CRPIX2{si}"] *= zoom2 + imagehdu.header[f"CDELT1{si}"] = pixel_scale + imagehdu.header[f"CDELT2{si}"] = pixel_scale + imagehdu.header[f"CUNIT1{si}"] = "mm" if si == "D" else "deg" + imagehdu.header[f"CUNIT2{si}"] = "mm" if si == "D" else "deg" return imagehdu @@ -552,9 +551,9 @@ def reorient_imagehdu(imagehdu: fits.ImageHDU, wcs_suffix: str = "", new_im *= np.sum(imagehdu.data) / np.sum(new_im) imagehdu.data = new_im - hdr["CRPIX1"+s] = hdr["NAXIS1"] / 2. - hdr["CRPIX2"+s] = hdr["NAXIS2"] / 2. - for card in ["PC1_1"+s, "PC1_2"+s, "PC2_1"+s, "PC2_2"+s]: + hdr[f"CRPIX1{s}"] = hdr["NAXIS1"] / 2. + hdr[f"CRPIX2{s}"] = hdr["NAXIS2"] / 2. + for card in [f"PC1_1{s}", f"PC1_2{s}", f"PC2_1{s}", f"PC2_2{s}"]: hdr.remove(card) imagehdu.header = hdr