Skip to content

Commit

Permalink
Fixed issue with indentation in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstolker committed Dec 7, 2023
1 parent 65bb4d3 commit f05dce9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 59 deletions.
38 changes: 20 additions & 18 deletions pycrires/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def __init__(self, path: Optional[str] = None, wavel_setting: Optional[str] = No

# manually set spectral setting

self.setting = wavel_setting
self.wavel_setting = wavel_setting

if self.setting:
print(f"Manually set spectral setting: {self.setting}")
if self.wavel_setting:
print(f"Manually set spectral setting: {self.wavel_setting}")

# Create attributes with the file paths

Expand Down Expand Up @@ -736,12 +736,12 @@ def _download_archive(self, dpr_type: str, det_dit: Optional[float]) -> None:
break

if download in ["y", "Y"]:
if self.setting:
if self.wavel_setting:
# No SCIENCE frames: we use the spectral setting provided by the user
indices = np.where(self.header_data["DPR.CATG"] == "CALIB")[0]

# Wavelength setting
wlen_id = self.setting
wlen_id = self.wavel_setting
else:
indices = np.where(self.header_data["DPR.CATG"] == "SCIENCE")[0]

Expand Down Expand Up @@ -921,8 +921,10 @@ def _plot_trace(self, dpr_catg: str) -> None:

if "UTIL_SLIT_CURV_TW" in self.file_dict:
trace_file = list(self.file_dict["UTIL_SLIT_CURV_TW"].keys())[0]
else:
elif "UTIL_TRACE_TW" in self.file_dict:
trace_file = list(self.file_dict["UTIL_TRACE_TW"].keys())[0]
else:
raise ValueError("Could not find a file with the TW table.")

with fits.open(trace_file) as hdu_list:
trace_data = [hdu_list[1].data, hdu_list[2].data, hdu_list[3].data]
Expand Down Expand Up @@ -1547,8 +1549,8 @@ def cal_flat(self, verbose: bool = True) -> None:

# Wavelength setting

if not self.setting:
wlen_id = self.setting
if not self.wavel_setting:
wlen_id = self.wavel_setting
else:
science_idx = np.where(self.header_data["DPR.CATG"] == "SCIENCE")[0]
wlen_id = self.header_data["INS.WLEN.ID"][science_idx[0]]
Expand Down Expand Up @@ -2074,8 +2076,8 @@ def util_calib(self, calib_type: str, verbose: bool = True) -> None:

dit_item = float(dit_item)

if self.setting:
wlen_id = self.setting
if self.wavel_setting:
wlen_id = self.wavel_setting
else:
science_idx = np.where(self.header_data["DPR.CATG"] == "SCIENCE")[0]
wlen_id = self.header_data["INS.WLEN.ID"][science_idx[0]]
Expand Down Expand Up @@ -2613,7 +2615,7 @@ def util_extract(self, calib_type: str, verbose: bool = True) -> None:
# "the util_trace method."
# )

# Find UTIL_SLIT_CURV_TW file
# Find UTIL_WAVE_TW or UTIL_SLIT_CURV_TW file

file_found = False

Expand All @@ -2623,9 +2625,9 @@ def util_extract(self, calib_type: str, verbose: bool = True) -> None:
with open(sof_file, "a", encoding="utf-8") as sof_open:
file_name = key.split("/")[-2:]
print(
f" - calib/{file_name[-2]}/{file_name[-1]} UTIL_SLIT_CURV_TW"
f" - calib/{file_name[-2]}/{file_name[-1]} UTIL_WAVE_TW"
)
sof_open.write(f"{key} UTIL_SLIT_CURV_TW\n")
sof_open.write(f"{key} UTIL_WAVE_TW\n")
file_found = True

if "UTIL_SLIT_CURV_TW" in self.file_dict:
Expand Down Expand Up @@ -3046,8 +3048,8 @@ def util_genlines(self, verbose: bool = True) -> None:

code_dir = Path(__file__).parent

if self.setting:
wavel_set = self.setting
if self.wavel_setting:
wavel_set = self.wavel_setting
else:
indices = np.where(self.header_data["DPR.CATG"] == "SCIENCE")[0]
wavel_set = self.header_data["INS.WLEN.ID"][indices[0]]
Expand Down Expand Up @@ -3162,8 +3164,8 @@ def util_genlines(self, verbose: bool = True) -> None:
fits_file = output_dir / line_file.with_suffix(".fits").name
self._update_files("EMISSION_LINES", str(fits_file))

if self.setting:
wlen_id = self.setting
if self.wavel_setting:
wlen_id = self.wavel_setting
else:
indices = np.where(self.header_data["DPR.CATG"] == "SCIENCE")[0]
wlen_id = self.header_data["INS.WLEN.ID"][indices[0]]
Expand Down Expand Up @@ -3590,7 +3592,7 @@ def obs_staring(self, verbose: bool = True, check_existing: bool = True) -> None

science_idx = np.where(self.header_data["DPR.CATG"] == "SCIENCE")[0]

if len(science_idx) > 0:
if len(science_idx) == 0:
raise RuntimeError("Cannot run obs_staring: there are no SCIENCE frames")

# Wavelength setting and DIT
Expand Down
86 changes: 45 additions & 41 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def setup_class(self) -> None:
if not os.path.exists(data_folder):
os.makedirs(data_folder)

url = "https://home.strw.leidenuniv.nl/~stolker/pycrires/test_data.tgz"
data_file = self.test_dir + "raw/test_data.tgz"
url = "https://home.strw.leidenuniv.nl/~stolker/pycrires/betapic_crires.tgz"
data_file = self.test_dir + "raw/betapic_crires.tgz"

pooch.retrieve(
url=url,
known_hash="7392f4be894470a08cbf565fe35d97f71606b07d3a3a692f3f9b38cddc5eb695",
fname="test_data.tgz",
known_hash="a3dd312f1d8115b1ce0eee5929f1287ef5764f82d649731aa859a11b1a545fa6",
fname="betapic_crires.tgz",
path=os.path.join(self.test_dir, "raw"),
progressbar=True,
)
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_cal_dark(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.cal_dark(verbose=False)

assert str(error.value) == self.esorex_error
assert str(error.value) == self.esorex_error

else:
self.pipeline.cal_dark(verbose=False)
Expand All @@ -104,7 +104,7 @@ def test_util_calib_flat(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_calib(calib_type="flat", verbose=False)

assert str(error.value) == self.esorex_error
assert str(error.value) == self.esorex_error

else:
self.pipeline.util_calib(calib_type="flat", verbose=False)
Expand All @@ -115,7 +115,7 @@ def test_util_trace(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_trace(plot_trace=False, verbose=False)

assert str(error.value) == self.esorex_error
assert str(error.value) == self.esorex_error

else:
self.pipeline.util_trace(plot_trace=False, verbose=False)
Expand All @@ -126,9 +126,9 @@ def test_util_slit_curv(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_slit_curv(plot_trace=True, verbose=False)

assert str(error.value) == "The UTIL_TRACE_TW file is not found " \
"in the 'calib' folder. Please first " \
"run the util_trace method."
assert str(error.value) == "The UTIL_TRACE_TW file is not found " \
"in the 'calib' folder. Please first " \
"run the util_trace method."

else:
self.pipeline.util_slit_curv(plot_trace=True, verbose=False)
Expand All @@ -139,9 +139,9 @@ def test_util_extract_flat(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_extract(calib_type="flat", verbose=False)

assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."
assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."

else:
self.pipeline.util_extract(calib_type="flat", verbose=False)
Expand All @@ -152,9 +152,9 @@ def test_util_normflat(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_normflat(verbose=False)

assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."
assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."

else:
self.pipeline.util_normflat(verbose=False)
Expand All @@ -165,7 +165,7 @@ def test_util_calib_une(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_calib(calib_type="une", verbose=False)

assert str(error.value) == self.esorex_error
assert str(error.value) == self.esorex_error

else:
self.pipeline.util_calib(calib_type="une", verbose=False)
Expand All @@ -176,9 +176,9 @@ def test_util_extract_une(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_extract(calib_type="une", verbose=False)

assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."
assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."

else:
self.pipeline.util_extract(calib_type="une", verbose=False)
Expand All @@ -189,7 +189,7 @@ def test_util_genlines(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_genlines(verbose=False)

assert str(error.value) == self.esorex_error
assert str(error.value) == self.esorex_error

else:
self.pipeline.util_genlines(verbose=False)
Expand All @@ -200,10 +200,10 @@ def test_util_wave_une(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_wave(calib_type="une", verbose=False)

assert str(error.value) == "The EMISSION_LINES file is not " \
"found in the 'calib/genlines' " \
"folder. Please first run the " \
"util_genlines method."
assert str(error.value) == "The EMISSION_LINES file is not " \
"found in the 'calib/genlines' " \
"folder. Please first run the " \
"util_genlines method."

else:
self.pipeline.util_wave(calib_type="une", poly_deg=0, wl_err=0.1, verbose=False)
Expand All @@ -215,7 +215,7 @@ def test_util_calib_fpet(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_calib(calib_type="fpet", verbose=False)

assert str(error.value) == self.esorex_error
assert str(error.value) == self.esorex_error

else:
self.pipeline.util_calib(calib_type="fpet", verbose=False)
Expand All @@ -226,9 +226,9 @@ def test_util_extract_fpet(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_extract(calib_type="fpet", verbose=False)

assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."
assert str(error.value) == "The UTIL_CALIB file is not found in " \
"the 'calib' folder. Please first " \
"run the util_calib method."

else:
self.pipeline.util_extract(calib_type="fpet", verbose=False)
Expand All @@ -239,11 +239,11 @@ def test_util_wave_fpet(self) -> None:
with pytest.raises(RuntimeError) as error:
self.pipeline.util_wave(calib_type="fpet", verbose=False)

assert str(error.value) == "The UTIL_EXTRACT_1D file is not " \
"found in the 'calib/" \
"util_extract_fpet' folder. Please " \
"first run the util_extract method " \
"with calib_type='une'."
assert str(error.value) == "The UTIL_EXTRACT_1D file is not " \
"found in the 'calib/" \
"util_extract_fpet' folder. Please " \
"first run the util_extract method " \
"with calib_type='une'."

else:
self.pipeline.util_wave(calib_type="fpet", poly_deg=4, wl_err=0.01, verbose=False)
Expand All @@ -252,25 +252,29 @@ def test_obs_nodding(self) -> None:

if shutil.which("esorex") is None:
with pytest.raises(RuntimeError) as error:
self.pipeline.obs_nodding(verbose=False)
self.pipeline.obs_nodding(verbose=False, correct_bad_pixels=True, extraction_required=True)

assert str(error.value) == "Cannot run obs_nodding: there are no SCIENCE frames"

else:
self.pipeline.obs_nodding(verbose=False)
self.pipeline.obs_nodding(verbose=False, correct_bad_pixels=True, extraction_required=True)

def test_run_skycalc(self) -> None:

with pytest.raises(RuntimeError) as error:
self.pipeline.run_skycalc(pwv=1.0)
if shutil.which("esorex") is None:
with pytest.raises(RuntimeError) as error:
self.pipeline.run_skycalc(pwv=1.0)

assert str(error.value) == "Cannot run skycalc: there are no SCIENCE frames"
assert str(error.value) == "Cannot run skycalc: there are no SCIENCE frames"

else:
self.pipeline.run_skycalc(pwv=1.0)

def test_plot_spectra(self) -> None:

if shutil.which("esorex") is None:
with pytest.raises(FileNotFoundError):
self.pipeline.plot_spectra(nod_ab="A", telluric=True)
self.pipeline.plot_spectra(nod_ab="A", telluric=True, corrected=False, file_id=0)

else:
self.pipeline.plot_spectra(nod_ab="A", telluric=True)
self.pipeline.plot_spectra(nod_ab="A", telluric=True, corrected=False, file_id=0)

0 comments on commit f05dce9

Please sign in to comment.