You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
I’m using the testbook library to test the function from the notebook. Here’s the test script I’m running:
# test_lab_2.pyfromtestbookimporttestbookimportnumpyasnpimportpytestNOTEBOOK_PATH='/path_to_notebook/notebook.ipynb'@testbook(NOTEBOOK_PATH, execute=True)deftest_bright_adjust(tb):
# Get reference to the brightAdjust function in the notebookbright_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 arrayadjusted_image_small=bright_adjust_func(image_small, 50)
# Expected output for the small image: brightness increased by 50expected_image_small=np.array([[150]], dtype=np.uint8)
# Assert that the adjusted small image matches the expected outputnp.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!
The text was updated successfully, but these errors were encountered:
Problem Description:
When passing
numpy
arrays as arguments to a function in the notebook, they seem to be converted to strings, leading toTypeError
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:Minimum Breaking Example:
1. Jupyter Notebook Code (
brightAdjust
function):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:3. Error Output:
Here’s the error traceback I’m getting when I run the test:
It seems like the
numpy
arrayimage_small
is being passed as the string"[[100]]"
, rather than as anumpy
array, causing the function to throw an error. The 50 seems to be properly being passed in.What I’ve Tried:
brightAdjust
function in Jupyter, so the issue seems to be specifically related totestbook
.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 oftestbook
, is there a recommended workaround?Thank you in advance for any help!
The text was updated successfully, but these errors were encountered: