Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest fixtures in unit tests instead of from_name methods #1985

Merged
merged 1 commit into from
Jul 19, 2022

Conversation

maxnoe
Copy link
Member

@maxnoe maxnoe commented Jul 1, 2022

We are preparing a number of changes to the data model, including also the instrument model and dropping support for older versions.

This is very hard to do when the tests are relying on very old files on the data server accessed via from_name.

Here I replace all use in our unit tests of from_name with pytest fixtures, which are defined mostly in ctapipe/conftest so we can change them in a single location.

Also, most fixtures are updated to use prod5 data.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@maxnoe maxnoe changed the title Remove from name Remove from_name methods of CameraDescription, CameraGeometry, CameraReadout, Optics and TelescopeDescription Jul 1, 2022
@kosack
Copy link
Contributor

kosack commented Jul 1, 2022

what's the reasoning for this?

@maxnoe
Copy link
Member Author

maxnoe commented Jul 1, 2022

@kosack As explained in #1978, I think these methods do more harm than good.

They make it very easy to make mistakes like loading the wrong instrument compared to the data being analyzed. I stopped counting the occurences I had to change code from doing from_name to reading the subarray from the input file / just using the one provided by the event source.

@maxnoe
Copy link
Member Author

maxnoe commented Jul 1, 2022

I think the use case for our unit tests is much better solved by defining common fixtures.

It reduced the code already by quite a lot. I could also imagine just doing the changes in our tests and keeping from_name for now, but the main thing is that it was abused far too often resulting in many errors.

@maxnoe maxnoe force-pushed the remove_from_name branch 5 times, most recently from 2dd70a6 to bbb2d38 Compare July 1, 2022 19:18
@codecov
Copy link

codecov bot commented Jul 1, 2022

Codecov Report

Merging #1985 (0580385) into master (93527bf) will decrease coverage by 0.01%.
The diff coverage is 99.33%.

@@            Coverage Diff             @@
##           master    #1985      +/-   ##
==========================================
- Coverage   92.33%   92.32%   -0.02%     
==========================================
  Files         193      193              
  Lines       15708    15725      +17     
==========================================
+ Hits        14504    14518      +14     
- Misses       1204     1207       +3     
Impacted Files Coverage Δ
ctapipe/image/tests/test_hillas.py 98.90% <95.12%> (-1.10%) ⬇️
ctapipe/calib/camera/tests/test_flatfield.py 92.85% <100.00%> (+0.17%) ⬆️
ctapipe/calib/camera/tests/test_pedestals.py 100.00% <100.00%> (ø)
ctapipe/conftest.py 94.89% <100.00%> (+0.64%) ⬆️
ctapipe/core/tests/test_component.py 97.81% <100.00%> (-0.03%) ⬇️
ctapipe/image/hillas.py 97.01% <100.00%> (-1.50%) ⬇️
ctapipe/image/muon/tests/test_intensity_fit.py 98.00% <100.00%> (ø)
ctapipe/image/muon/tests/test_ring_fitter.py 100.00% <100.00%> (ø)
ctapipe/image/tests/test_cleaning.py 100.00% <100.00%> (ø)
ctapipe/image/tests/test_concentration.py 100.00% <100.00%> (+3.70%) ⬆️
... and 23 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 93527bf...0580385. Read the comment docs.

@maxnoe maxnoe force-pushed the remove_from_name branch 2 times, most recently from 5014775 to 25c5fee Compare July 15, 2022 11:58
@maxnoe maxnoe changed the title Remove from_name methods of CameraDescription, CameraGeometry, CameraReadout, Optics and TelescopeDescription Use pytest fixtures in unit tests instead of from_name methods Jul 15, 2022
@maxnoe
Copy link
Member Author

maxnoe commented Jul 15, 2022

@kosack I removed the "remove from_name" part for now and only included the changes to the fixtures.

It would make it much easier to test the data model changes without constantly having to update files on the data server to include this.

kosack
kosack previously approved these changes Jul 18, 2022
@kosack
Copy link
Contributor

kosack commented Jul 18, 2022

This is nice in that we don't rely on the data format. However, what about tests to read old data formats? We should still have those (perhaps not now but after the 1.0 release, to ensure backward compatibility)

@maxnoe
Copy link
Member Author

maxnoe commented Jul 18, 2022

The point is, that we now do want do break backwards compatibilty.

For me this means that tests checking compatibility to old data should be added after we merge the current PRs changing the data model, make a release and first thing we add tests testing files pepduced with that release?

@maxnoe
Copy link
Member Author

maxnoe commented Jul 18, 2022

I would propose the following:

  • Merge the remaining, data model changing PRs
  • Make the 0.16.0 release
  • Process test data files using the released version and add them to the data server in some meaningful structure
  • Add (parametrized) tests checking we are still compatible with all data model versions we want to support (i.e. for now the current one for 0.16.0 and the currently implemented on, e.g.:

@pytest.fixture("session")
def prod5_dl1_file(tmp_dir_factory):
    # run ctapipe-process tool
    return output_path
    

@pytest.fixture("session", params=["v0.16.0", "current"])
def prod5_dl1_files(request, prod5_dl1_file):
    if request.param == "current":
        return prod5_dl1_file
    return get_dataset_path(f"prod5/{request.param}/test.dl1.h5")

@maxnoe
Copy link
Member Author

maxnoe commented Jul 19, 2022

@kosack @nbiederbeck @LukasNickel Please review again

@maxnoe maxnoe requested a review from nbiederbeck July 19, 2022 07:01
@maxnoe maxnoe requested a review from kosack July 19, 2022 08:03
@maxnoe maxnoe merged commit df492e7 into master Jul 19, 2022
@maxnoe maxnoe deleted the remove_from_name branch July 19, 2022 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants