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

775 implement knit print for subclasses of cohortsize including tests #784

Conversation

Puzzled-Face
Copy link
Collaborator

Implements #775, part 1 of #774.

Copy link
Contributor

github-actions bot commented Feb 1, 2024

Unit Tests Summary

    1 files     40 suites   3m 55s ⏱️
1 278 tests 1 123 ✅ 155 💤 0 ❌
6 894 runs  6 701 ✅ 193 💤 0 ❌

Results for commit b629f79.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 1, 2024

Unit Test Performance Difference

Test Suite $Status$ Time on main $±Time$ $±Tests$ $±Skipped$ $±Failures$ $±Errors$
CrmPackClass-class 💚 $32.14$ $-10.43$ $0$ $0$ $0$ $0$
helpers_knitr 👶 $+26.46$ $+37$ $+1$ $0$ $0$
Additional test case details
Test Suite $Status$ Time on main $±Time$ Test Case
CrmPackClass-class 💚 $31.99$ $-10.44$ CrmPackClass_correctly_identifies_crmPack_classes
CrmPackClass-methods 💔 $21.52$ $+1.62$ tidy_methods_exist_for_all_relevant_classes
CrmPackClass-methods 💚 $24.66$ $-2.06$ tidy_methods_return_non_empty_value_for_all_classes
Simulations-class 👶 $+0.01$ .DefaultSimulationsSummary_cannot_be_instantiated_directly
Simulations-class 👶 $+0.01$ GeneralSimulationsSummary_cannot_be_instantiated_directly
Simulations-class 👶 $+0.02$ GeneralSimulationsSummary_generates_object_correctly
Simulations-class 👶 $+0.02$ SimulationsSummary_generates_object_correctly
helpers_knitr 👶 $+0.02$ Global_environment_is_clean_after_testing_h_custom_method_exists
helpers_knitr 👶 $+25.90$ asis_parameter_works_correctly_for_all_implemented_methods
helpers_knitr 👶 $+0.03$ h_custom_method_exists_works_correctly
helpers_knitr 👶 $+0.07$ knit_print.CohortSizeConst_works_correctly
helpers_knitr 👶 $+0.02$ knit_print.CohortSizeParts_works_correctly
helpers_knitr 👶 $+0.41$ knit_print_methods_exist_for_all_relevant_classes_and_produce_consistent_output

Results for commit 01941b3

♻️ This comment has been updated with latest results.

Puzzled-Face and others added 16 commits February 1, 2024 08:44
…ncluding-tests' of github.com:Roche/crmPack into 775-implement-knit_print-for-subclasses-of-cohortsize-including-tests

# Conflicts:
#	tests/testthat/test-helpers_knitr.R
…ncluding-tests' of github.com:Roche/crmPack into 775-implement-knit_print-for-subclasses-of-cohortsize-including-tests
…ncluding-tests' of github.com:Roche/crmPack into 775-implement-knit_print-for-subclasses-of-cohortsize-including-tests
Copy link
Contributor

github-actions bot commented Feb 2, 2024

badge

Code Coverage Summary

Filename                        Stmts    Miss  Cover    Missing
----------------------------  -------  ------  -------  ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
R/checkmate.R                      73       1  98.63%   72
R/crmPack-package.R                 4       0  100.00%
R/CrmPackClass-methods.R            5       0  100.00%
R/Data-class.R                    147       5  96.60%   43, 564-565, 571-576
R/Data-methods.R                  203       0  100.00%
R/Data-validity.R                 144       1  99.31%   21
R/Design-class.R                  391       0  100.00%
R/Design-methods.R               2775     771  72.22%   563-567, 591-594, 602-621, 626-655, 659-660, 662, 677-685, 689, 701-720, 1122-1126, 1253, 1267-1271, 1333, 1523, 1740, 1767-1770, 1779-1790, 1794-1813, 1824-1828, 1834-1846, 2109, 2134-2137, 2144-2155, 2159-2178, 2190-2194, 2200-2212, 2496-2499, 2527-2530, 2538-2550, 2553-2564, 2568-2604, 2621-2630, 2636-2651, 2676, 2717-2718, 2981-3451, 3546-3557, 3560-3571, 3575-3611, 3628-3637, 3645-3660, 3698, 3740-3741, 4020, 4022-4023, 4082, 4118, 4155-4158, 4177-4181, 4242-4249, 4277-4281, 4289-4307, 4333-4352, 4355, 4388, 4410-4428, 4697, 4792
R/Design-validity.R                38      10  73.68%   47-56
R/fromQuantiles.R                 176      67  61.93%   238-378
R/helpers_broom.R                  74      10  86.49%   30, 34-35, 37-38, 40, 81, 102-104
R/helpers_covr.R                   23       0  100.00%
R/helpers_data.R                   96       1  98.96%   139
R/helpers_design.R                126      41  67.46%   77-129, 250, 255-259
R/helpers_jags.R                   77       0  100.00%
R/helpers_knitr_CohortSize.R       99       0  100.00%
R/helpers_model.R                  85       4  95.29%   38, 89-90, 139
R/helpers_rules.R                 428       0  100.00%
R/helpers_samples.R                 5       0  100.00%
R/helpers_simulations.R            27       0  100.00%
R/helpers.R                       214      61  71.50%   107-127, 162-178, 200-304, 339-353
R/logger.R                         11       0  100.00%
R/mcmc.R                          290      18  93.79%   92-97, 376-377, 387, 389-390, 393-396, 579-580, 669, 675, 733
R/McmcOptions-class.R              22       0  100.00%
R/McmcOptions-methods.R             8       1  87.50%   43
R/McmcOptions-validity.R           42       0  100.00%
R/Model-class.R                  1062     166  84.37%   145-147, 216-218, 222-224, 283-285, 357-359, 363-365, 444-446, 513-515, 577-581, 584-587, 690-693, 697-698, 813-817, 937-939, 943-951, 1096-1098, 1103-1106, 1110-1113, 1229-1233, 1235-1238, 1242-1245, 1248, 1409-1419, 1424-1430, 1585-1588, 1594-1601, 1758, 1767, 1776, 1785, 1794-1799, 1935, 1944, 1953, 1961-1963, 2807-2836, 2840-2846, 2853-2857, 2862, 2969-2982, 3008, 3104-3106, 3110, 3203-3205, 3209, 3278-3290, 3308, 3368-3370, 3372-3373, 3376-3381
R/Model-methods.R                 472      38  91.95%   78, 233-238, 809-854, 1175-1184
R/Model-validity.R                443      16  96.39%   430-433, 442-445, 596-604
R/ModelParams-class.R              17       0  100.00%
R/ModelParams-validity.R           21       0  100.00%
R/Rules-class.R                   458       0  100.00%
R/Rules-methods.R                1536     184  88.02%   889, 892, 895, 1010, 1013, 1016, 1136-1139, 1173, 1276-1279, 1314, 2582-2590, 2614-2621, 2784-2793, 3073-3082, 3215-3458, 3727, 3731, 3776, 3780
R/Rules-validity.R                448      30  93.30%   684-723
R/Samples-class.R                   6       0  100.00%
R/Samples-methods.R              1188      21  98.23%   410-420, 648, 1665-1666, 1698, 1711, 1893, 2223-2228
R/Samples-validity.R               10       0  100.00%
R/Simulations-class.R             208       5  97.60%   784-787, 1043
R/Simulations-methods.R          1617    1473  8.91%    65-350, 406, 416-435, 448-453, 500-509, 674-2969
R/Simulations-validity.R           75      75  0.00%    20-168
R/utils.R                           6       0  100.00%
TOTAL                           13150    2999  77.19%

Diff against main

Filename                        Stmts    Miss  Cover
----------------------------  -------  ------  --------
R/helpers_knitr_CohortSize.R      +99       0  +100.00%
R/Simulations-class.R              +8       0  +0.10%
TOTAL                            +107       0  +0.19%

Results for commit: b629f79

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

Puzzled-Face and others added 5 commits February 2, 2024 11:15
…ncluding-tests' of github.com:Roche/crmPack into 775-implement-knit_print-for-subclasses-of-cohortsize-including-tests
@Puzzled-Face
Copy link
Collaborator Author

Puzzled-Face commented Feb 2, 2024

Test coverage report suggests that knit_print methods for CohortSizeMax, CohortSizeMin and CohortSizeOrdinal are not covered. But the contents of the /_snaps/helpers_knitr clearly shows that they are.

@danielinteractive danielinteractive self-assigned this Feb 2, 2024
Copy link
Collaborator

@danielinteractive danielinteractive left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Puzzled-Face , nice work!
I have some questions here about the tests... can we maybe stick to a simpler, even though more lines of code involved, way of handling this?

R/helpers_knitr_CohortSize.R Outdated Show resolved Hide resolved
R/helpers_knitr_CohortSize.R Outdated Show resolved Hide resolved
R/helpers_knitr_CohortSize.R Outdated Show resolved Hide resolved
crmpack_class_list <- setdiff(crmpack_class_list, exclusions)

# See https://stackoverflow.com/questions/42738851/r-how-to-find-what-s3-method-will-be-called-on-an-object
identifyMethod <- function(generic, ...) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm........ this will be difficult to maintain. any way we can keep this much simpler for the tests?...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure which part you think will be difficult to maintain: the exclusion list, the code to "find" the S3 method for each class, or something else?

The exclusion list is awkward, I agree. But I think it's reasonably stable - it's essentially a list of the virtual classes that crmPack defines. I don't see that changing very frequently. I propose two ways of handling this in #734. [Note the paragraph that reads

An alternative approach would be to require all methods to exist even for virtual superclasses. The actual method implementation for these virtual superclasses would then throw an exception (as is done by the default constructors for these super classes. This would simplify the unit testing, but at the cost of additional code that is never expected to be used.

I used the same approach to test that all the necessary tidy methods had been defined.

[As an aside, I think one weakness of our current tests is that we only test that what we have done works correctly. We don't test that we have done everything we should have done. The approach I've taken here is, I think, the only way to address that weakness.]

If you mean that the code to find the appropriate S3 method is flaky, I disagree. The fundamental process should hold true regardless of what happens in the future. If not, it's likely that S3 is broken and we will have bigger issues to deal with!

If you mean the use of is_checking(), I think that can probably be removed now. It was one of the things I tried to solve the problems I asked about in chat. I think Craig spotted the root cause, so this is probably unnecessary now.

The use of if (methodName != "knit_print.default") is effectively temporary. Once we have all the necessary knit_print methods implemented, we can either remove it entirely, or (my preference) replace it with fail(paste0("Required knit_print method has not been defined for ", cls)) or similar. There's a step to do this listed in #774. Again, I took the same approach when testing the tidy methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add more comments if you think that would be helpful.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the whole lines 22-63 are difficult to maintain. I don't understand what the code is doing to be honest. Plus there is a lot of typically dangerous things that work on the language, e.g. deparse , substitute, assign.

My question is would it be possible to have much more vanilla kind of testing here, which is avoiding those dangerous things, avoiding loops where possible, and keep with a maybe tedious but explicit list of tests?

Example: https://github.com/Roche/crmPack/blob/main/tests/testthat/test-Model-class.R I guess we could
have tried to use loops to avoid manual writing of tests here too, but we kept with the much more simple structure.

If it is possible - let's go with the more simple approach. If not - then we need to derisk this code, by testing it explicitly (yes, testing the code that is used for tests here), documenting, commenting, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I hope I can reassure you. The code that makes you uncomfortable is temporary. Put simply, it says "do we have a custom knit_print method for the current class? If so, test it. If not, do nothing.". So, once we (think we) have knit_print methods for every crmPack class, we can delete the conditional testing and just test everything.

In this particular case, we could delete the conditional testing immediately. That will mean that tests will fail when we introduce new knit_print methods, but I don't see that as a major problem: we'd have to inspect new snapshot files in that case anyway.

The more important reason for keeping the current structure is that I can re-use it in other situations. For example, to test the behaviour of the asis parameter to knit_print for crmPack classes. The generic knir_print doesn't have this, so we would need either to use a class-specific test (which I think is less reliable and definitely less efficient) or to use a similar approach.

I'll refactor to make the code more self-explanatory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok and let's try to also have tests for the test code, e.g. make a function for pieces of it, add documentation and tests. Otherwise somebody else cannot maintain this in the future

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. See revisions in the latest push

Copy link
Collaborator

@danielinteractive danielinteractive left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work, thanks @Puzzled-Face !

Puzzled-Face and others added 4 commits February 5, 2024 13:05
Merge branch '775-implement-knit_print-for-subclasses-of-cohortsize-including-tests' of github.com:Roche/crmPack into 775-implement-knit_print-for-subclasses-of-cohortsize-including-tests

# Conflicts:
#	R/helpers_knitr_CohortSize.R
@Puzzled-Face Puzzled-Face enabled auto-merge (squash) February 5, 2024 13:21
@Puzzled-Face Puzzled-Face merged commit 6fbd508 into main Feb 5, 2024
21 checks passed
@Puzzled-Face Puzzled-Face deleted the 775-implement-knit_print-for-subclasses-of-cohortsize-including-tests branch February 5, 2024 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement knit_print for subclasses of CohortSize (including tests)
2 participants