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

opencdms-process: create pytest tests that verify calls to R functions generate the expected R warnings #31

Open
lloyddewit opened this issue Jul 8, 2022 · 0 comments

Comments

@lloyddewit
Copy link
Contributor

lloyddewit commented Jul 8, 2022

  • Python version: 3.8.10
  • R version: 4.1.2
  • Operating System: Ubuntu 20.04

I want to write a 'PyTest' function that checks if a specific call to an R function raises the expected warning(s). For example, the call below should generate warning 'facet_by set to none since no stations are given in data':

    cdms_products.histogram_plot(
        path=output_path_actual,
        file_name=file_name_actual,
        data=niger50,
        date_time="date",
        elements=["tmax"],
        facet_by="stations",
    )

I am using the 'rpy2' library to call the R function. The 'rpy2' library does not yet seem to allow warnings/errors generated in the R environment to be read in the Python environment. I created 'rpy2' issue 894. This describes the problem in detail.

I see two possible ways ahead:

  1. Wait to see if the rpy2 issue is resolved.
  2. Use a workaround like writing the R output to file in the R environment, (example here).

I prefer option 1.

Personal notes on possible approaches if/when 'rpy2' issue above is resolved

The Python wrapper would write any R warnings/errors to a stream that is returned to the calling function. Or it could write errors/warnings to a log file. The log file name would be the same as the output file name, except that the file name extension would be ".log".
Example code (Python wrapper function):

    from rpy2.rinterface_lib.callbacks import logger as rpy2_logger
    file_name_log = os.path.join(path, os.path.splitext(file_name)[0] + ".log")
    log_file_handler = logging.FileHandler(file_name_log)
    rpy2_logger.addHandler(log_file_handler)

Example code ('pytest' function):

    cdms_products.histogram_plot(
        path=output_path_actual,
        file_name=file_name_actual,
        data=niger50,
        date_time="date",
        elements=["tmax"],
        facet_by="stations",
    )
    assert __is_expected_file(file_name_actual)
    assert __is_expected_file(os.path.splitext(file_name_actual)[0]+".log")

Alternatively, the Python wrapper function could ignore the R warnings, and the 'pytest' function could analyse the warnings directly (see pytest warning documentation here and here).

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

No branches or pull requests

1 participant