From 96d24c37305b339afc2906690cd4319d038314ed Mon Sep 17 00:00:00 2001 From: "Sebastian Zieba (astronodes)" Date: Wed, 21 Feb 2024 23:55:29 +0100 Subject: [PATCH] fixed coverage --- .github/workflows/ci.yml | 22 +-- .github/workflows/ci_windows.yml | 4 +- src/pacman/s10_direct_images.py | 2 +- tests/test_all.py | 228 +++++++++++++++---------------- 4 files changed, 128 insertions(+), 128 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f91fd87..3dbbe9e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: Python package +name: Pytest (Linux) on: [push, pull_request] jobs: @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - name: Checkout @@ -24,17 +24,9 @@ jobs: python -m pip install --upgrade pip pip install -e ".[test]" -# - name: Lint with flake8 -# run: | -# # stop the build if there are Python syntax errors or undefined names -# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics -# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide -# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - - - name: Test with pytest run: | - pytest tests --cov=./src/pacman --cov-report=xml + pytest tests --cov=./src/pacman --cov-report=xml -s - name: Upload coverage.xml as artifact uses: actions/upload-artifact@v4 @@ -52,3 +44,11 @@ jobs: #name: codecov-umbrella # optional fail_ci_if_error: false # optional (default = false) verbose: true # optional (default = false) + +# - name: Lint with flake8 +# run: | +# # stop the build if there are Python syntax errors or undefined names +# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics +# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide +# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + diff --git a/.github/workflows/ci_windows.yml b/.github/workflows/ci_windows.yml index c67b3be..9742d87 100644 --- a/.github/workflows/ci_windows.yml +++ b/.github/workflows/ci_windows.yml @@ -1,4 +1,4 @@ -name: Python package +name: Pytest (Windows) on: [push, pull_request] jobs: @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install ".[test]" + pip install -e ".[test]" - name: Test with pytest run: | diff --git a/src/pacman/s10_direct_images.py b/src/pacman/s10_direct_images.py index ed62242..a699bad 100644 --- a/src/pacman/s10_direct_images.py +++ b/src/pacman/s10_direct_images.py @@ -95,6 +95,6 @@ def run10(eventlabel, workdir: Path, meta=None): # Save results print('Saving Metadata') me.saveevent(meta, meta.workdir / f'WFC3_{meta.eventlabel}_Meta_Save', save=[]) - print('tmp: ', 2+1) + print('Finished s10 \n') return meta diff --git a/tests/test_all.py b/tests/test_all.py index 3b7a40e..01a2c39 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -321,120 +321,120 @@ def test_sim_source(capsys): assert source_ypos-1 <= results[3] <= source_ypos+1 -# @pytest.mark.run(order=27) -# def test_s20(capsys): -# """The extraction step. Extracts flux as a function -# of wavelength and time. -# """ -# reload(s20) -# time.sleep(1) -# -# workdir, eventlabel = workdir_finder() -# -# # Run s20 -# meta = s20.run20(eventlabel, workdir) -# -# extracted_lc_dir_path = workdir / 'extracted_lc' -# -# s20_dir = np.array([path for path in extracted_lc_dir_path.iterdir() if path.is_dir()])[0] -# s20_lc_spec_file = s20_dir / 'lc_spec.txt' -# s20_lc_white_file = s20_dir / 'lc_white.txt' -# -# # Check if the files were created -# assert s20_lc_spec_file.exists() -# assert s20_lc_white_file.exists() -# -# s20_lc_spec = ascii.read(s20_lc_spec_file) -# s20_lc_white = ascii.read(s20_lc_white_file) -# -# # Check the amount of columns -# assert len(s20_lc_spec.colnames) == 10 -# assert len(s20_lc_white.colnames) == 11 -# -# # test_optextr -# spectrum = np.ones((20, 9)) -# -# for i in range(len(spectrum)): -# for j in range(len(spectrum[0])): -# if 4 < i < 8: -# if 1 < j < 7: -# spectrum[i, j] = 10 -# -# err = np.ones((20, 9))*0.01 -# Mnew = np.ones((20, 9)) -# spec_box_0 = 15 * 10 -# var_box_0 = 1 -# -# [f_opt_0, _, numoutliers] = optextr.optextr( -# spectrum, err, spec_box_0, var_box_0, Mnew, -# meta.nsmooth, meta.sig_cut, meta.save_optextr_plot, 0, 0, meta) -# -# assert np.round(np.sum(f_opt_0), 0) == np.round(np.sum(spectrum), 0) #optimal extraction flux should be the same as the total flux in the array -# assert numoutliers == 0 # We didnt introduce any outliers -# -# -# @pytest.mark.run(order=29) -# def test_s21(capsys): -# """Creates spectroscopic light curves.""" -# reload(s21) -# time.sleep(1) -# -# workdir, eventlabel = workdir_finder() -# -# # Run s21 -# meta = s21.run21(eventlabel, workdir) -# -# extracted_sp_dir_path = workdir / 'extracted_sp' -# -# s21_dir = np.array([path for path in extracted_sp_dir_path.iterdir() -# if path.is_dir()])[0] -# s21_wvl_table_file = s21_dir / 'wvl_table.dat' -# assert s21_wvl_table_file.exists() -# s21_wvl_table = ascii.read(s21_wvl_table_file) -# -# wvl_s21 = s21_wvl_table['wavelengths'] -# -# # Check if the number of bins defined in the pcf is the same as -# # the number of wavelength bins saved into the wvl_table.dat file. -# assert meta.wvl_bins == len(wvl_s21) -# -# # Number of light curves should be the same as meta.wvl_bins -# extracted_sp_lcs_files = list(s21_dir.glob("*.txt")) -# assert meta.wvl_bins == len(extracted_sp_lcs_files) -# -# # There should be 10 columns as for the /lc_spec.txt file which was generated after running s20. -# extracted_sp_lc_file_0 = sn.sort_nicely(extracted_sp_lcs_files)[0] -# extracted_sp_lc_0 = ascii.read(extracted_sp_lc_file_0) -# assert len(extracted_sp_lc_0.colnames) == 10 -# -# -# @pytest.mark.run(order=30) -# def test_s30(capsys): -# """Fits spectroscopic light curves.""" -# reload(s30) -# time.sleep(1) -# -# workdir, eventlabel = workdir_finder() -# -# # Run s30 -# meta = s30.run30(eventlabel, workdir) -# -# dirs = np.array([path for path in workdir.iterdir() if path.is_dir()]) -# dirs_bool = np.array(['fit_' in dir.name for dir in dirs]) -# fit_dirs = dirs[dirs_bool] -# fit_dir = fit_dirs[0] -# assert fit_dir.exists() -# -# meta.s30_fit_white = True -# meta.s30_most_recent_s20 = True -# -# s30.run30(eventlabel, workdir, meta=meta) -# -# dirs = np.array([path for path in workdir.iterdir() if path.is_dir()]) -# dirs_bool = np.array(['fit_' in dir.name for dir in dirs]) -# -# print('dirs_bool: ', dirs_bool) -# assert True +@pytest.mark.run(order=27) +def test_s20(capsys): + """The extraction step. Extracts flux as a function + of wavelength and time. + """ + reload(s20) + time.sleep(1) + + workdir, eventlabel = workdir_finder() + + # Run s20 + meta = s20.run20(eventlabel, workdir) + + extracted_lc_dir_path = workdir / 'extracted_lc' + + s20_dir = np.array([path for path in extracted_lc_dir_path.iterdir() if path.is_dir()])[0] + s20_lc_spec_file = s20_dir / 'lc_spec.txt' + s20_lc_white_file = s20_dir / 'lc_white.txt' + + # Check if the files were created + assert s20_lc_spec_file.exists() + assert s20_lc_white_file.exists() + + s20_lc_spec = ascii.read(s20_lc_spec_file) + s20_lc_white = ascii.read(s20_lc_white_file) + + # Check the amount of columns + assert len(s20_lc_spec.colnames) == 10 + assert len(s20_lc_white.colnames) == 11 + + # test_optextr + spectrum = np.ones((20, 9)) + + for i in range(len(spectrum)): + for j in range(len(spectrum[0])): + if 4 < i < 8: + if 1 < j < 7: + spectrum[i, j] = 10 + + err = np.ones((20, 9))*0.01 + Mnew = np.ones((20, 9)) + spec_box_0 = 15 * 10 + var_box_0 = 1 + + [f_opt_0, _, numoutliers] = optextr.optextr( + spectrum, err, spec_box_0, var_box_0, Mnew, + meta.nsmooth, meta.sig_cut, meta.save_optextr_plot, 0, 0, meta) + + assert np.round(np.sum(f_opt_0), 0) == np.round(np.sum(spectrum), 0) #optimal extraction flux should be the same as the total flux in the array + assert numoutliers == 0 # We didnt introduce any outliers + + +@pytest.mark.run(order=29) +def test_s21(capsys): + """Creates spectroscopic light curves.""" + reload(s21) + time.sleep(1) + + workdir, eventlabel = workdir_finder() + + # Run s21 + meta = s21.run21(eventlabel, workdir) + + extracted_sp_dir_path = workdir / 'extracted_sp' + + s21_dir = np.array([path for path in extracted_sp_dir_path.iterdir() + if path.is_dir()])[0] + s21_wvl_table_file = s21_dir / 'wvl_table.dat' + assert s21_wvl_table_file.exists() + s21_wvl_table = ascii.read(s21_wvl_table_file) + + wvl_s21 = s21_wvl_table['wavelengths'] + + # Check if the number of bins defined in the pcf is the same as + # the number of wavelength bins saved into the wvl_table.dat file. + assert meta.wvl_bins == len(wvl_s21) + + # Number of light curves should be the same as meta.wvl_bins + extracted_sp_lcs_files = list(s21_dir.glob("*.txt")) + assert meta.wvl_bins == len(extracted_sp_lcs_files) + + # There should be 10 columns as for the /lc_spec.txt file which was generated after running s20. + extracted_sp_lc_file_0 = sn.sort_nicely(extracted_sp_lcs_files)[0] + extracted_sp_lc_0 = ascii.read(extracted_sp_lc_file_0) + assert len(extracted_sp_lc_0.colnames) == 10 + + +@pytest.mark.run(order=30) +def test_s30(capsys): + """Fits spectroscopic light curves.""" + reload(s30) + time.sleep(1) + + workdir, eventlabel = workdir_finder() + + # Run s30 + meta = s30.run30(eventlabel, workdir) + + dirs = np.array([path for path in workdir.iterdir() if path.is_dir()]) + dirs_bool = np.array(['fit_' in dir.name for dir in dirs]) + fit_dirs = dirs[dirs_bool] + fit_dir = fit_dirs[0] + assert fit_dir.exists() + + meta.s30_fit_white = True + meta.s30_most_recent_s20 = True + + s30.run30(eventlabel, workdir, meta=meta) + + dirs = np.array([path for path in workdir.iterdir() if path.is_dir()]) + dirs_bool = np.array(['fit_' in dir.name for dir in dirs]) + + print('dirs_bool: ', dirs_bool) + assert True @pytest.mark.run(order=40)