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

Numpy Arrays Being Converted to Strings in Testbook Tests #159

Open
AnsonSavage opened this issue Sep 30, 2024 · 0 comments
Open

Numpy Arrays Being Converted to Strings in Testbook Tests #159

AnsonSavage opened this issue Sep 30, 2024 · 0 comments

Comments

@AnsonSavage
Copy link

Problem Description:

When passing numpy arrays as arguments to a function in the notebook, they seem to be converted to strings, leading to TypeError exceptions.

The function I’m testing is supposed to adjust the brightness of a grayscale image (represented as a 2D numpy array).

However, during testing, it seems like the numpy array is converted to a string before being passed to the function, which results in the following error:

TypeError: unsupported operand type(s) for -: 'str' and 'int'

Minimum Breaking Example:

1. Jupyter Notebook Code (brightAdjust function):
import numpy as np

def brightAdjust(image, c):
    return np.clip(image + c, 0, 255)
2. Test Code Using Testbook:

I’m using the testbook library to test the function from the notebook. Here’s the test script I’m running:

# test_lab_2.py

from testbook import testbook
import numpy as np
import pytest

NOTEBOOK_PATH = '/path_to_notebook/notebook.ipynb'

@testbook(NOTEBOOK_PATH, execute=True)
def test_bright_adjust(tb):
    # Get reference to the brightAdjust function in the notebook
    bright_adjust_func = tb.ref('brightAdjust')

    # Create a very small grayscale image (1x1 numpy array)
    image_small = np.array([[100]], dtype=np.uint8)

    # Apply brightness adjustment with c = 50 on the small array
    adjusted_image_small = bright_adjust_func(image_small, 50)

    # Expected output for the small image: brightness increased by 50
    expected_image_small = np.array([[150]], dtype=np.uint8)

    # Assert that the adjusted small image matches the expected output
    np.testing.assert_array_equal(adjusted_image_small, expected_image_small)
3. Error Output:

Here’s the error traceback I’m getting when I run the test:

TypeError: unsupported operand type(s) for -: 'str' and 'int'

During handling of the above exception, another exception occurred:

testbook.exceptions.TestbookRuntimeError: An error occurred while executing the following cell:
------------------
brightAdjust(*("[[100]]", 50, ), **{})
------------------

It seems like the numpy array image_small is being passed as the string "[[100]]", rather than as a numpy array, causing the function to throw an error. The 50 seems to be properly being passed in.

What I’ve Tried:

  • I’ve confirmed that the notebook runs as expected when I manually execute the cell with the brightAdjust function in Jupyter, so the issue seems to be specifically related to testbook.
  • I’ve checked that the data I’m passing from the test script is indeed a numpy array, but somehow it’s converted to a string when sent to the notebook function.

Expected Behavior:

The numpy array should be passed as-is to the function in the notebook, not as a string.

Request:

Could anyone provide guidance on how to prevent numpy arrays from being converted to strings when passing them from the test code to the notebook function? If this is a limitation of testbook, is there a recommended workaround?

Thank you in advance for any help!

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