Initial phase of adding PyTorch support for data analysis. Added conversion nodes.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
|
||||
from numpy_wrapper.numpy_float_create import NumpyFloatCreate
|
||||
|
||||
class TestNumpyFloatCreate(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = NumpyFloatCreate()
|
||||
|
||||
# Test cases covering rank 0 (scalar) to rank 5 tensors
|
||||
self.test_cases = [
|
||||
# Rank 0 (scalar)
|
||||
("12345", np.array(12345, dtype=np.float32)),
|
||||
("12345.6", np.array(12345.6, dtype=np.float32)),
|
||||
|
||||
# Rank 1 (1D array)
|
||||
("[1, 2, 3]", np.array([1, 2, 3], dtype=np.float32)),
|
||||
("[1.1, 2.2, 3.3]", np.array([1.1, 2.2, 3.3], dtype=np.float32)),
|
||||
|
||||
# Rank 2 (2D matrix)
|
||||
("[[1, 2, 3], [4, 5, 6]]", np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)),
|
||||
("[[1.1, 2.2], [3.3, 4.4]]", np.array([[1.1, 2.2], [3.3, 4.4]], dtype=np.float32)),
|
||||
|
||||
# Rank 3 (3D tensor)
|
||||
("[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]",
|
||||
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.float32)),
|
||||
|
||||
# Rank 4 (4D tensor)
|
||||
("[[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]]",
|
||||
np.array([[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]], dtype=np.float32)),
|
||||
|
||||
# Rank 5 (5D tensor)
|
||||
("[[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]]",
|
||||
np.array([[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]], dtype=np.float32)),
|
||||
]
|
||||
|
||||
def test_create_ndarray(self):
|
||||
"""Test NumPy ndarray creation from various structured inputs."""
|
||||
for data_str, expected_array in self.test_cases:
|
||||
with self.subTest(data=data_str):
|
||||
retval = self.node.f(data_str)[0]
|
||||
|
||||
# Ensure the returned value is a NumPy array
|
||||
self.assertTrue(isinstance(retval, np.ndarray), f"Expected ndarray, got {type(retval)}")
|
||||
|
||||
# Ensure the dtype is float32
|
||||
self.assertEqual(retval.dtype, np.float32, f"Dtype mismatch for input: {data_str}")
|
||||
|
||||
# Ensure the shape matches
|
||||
self.assertEqual(retval.shape, expected_array.shape,
|
||||
f"Shape mismatch for input: {data_str} (Expected {expected_array.shape}, got {retval.shape})")
|
||||
|
||||
# Ensure values match
|
||||
np.testing.assert_array_equal(retval, expected_array,
|
||||
f"Value mismatch for input: {data_str}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,63 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
|
||||
from numpy_wrapper.numpy_int_create import NumpyIntCreate
|
||||
|
||||
class TestNumpyIntCreate(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = NumpyIntCreate()
|
||||
|
||||
# Test cases covering rank 0 (scalar) to rank 5 tensors
|
||||
self.test_cases = [
|
||||
# Rank 0 (scalar)
|
||||
("12345", np.array(12345, dtype=np.int32)),
|
||||
|
||||
# Rank 1 (1D array)
|
||||
("[1, 2, 3]", np.array([1, 2, 3], dtype=np.int32)),
|
||||
|
||||
# Rank 2 (2D matrix)
|
||||
("[[1, 2, 3], [4, 5, 6]]", np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)),
|
||||
|
||||
# Rank 3 (3D tensor)
|
||||
("[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]",
|
||||
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.int32)),
|
||||
|
||||
# Rank 4 (4D tensor)
|
||||
("[[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]]",
|
||||
np.array([[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]], dtype=np.int32)),
|
||||
|
||||
# Rank 5 (5D tensor)
|
||||
("[[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]]",
|
||||
np.array([[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]], dtype=np.int32)),
|
||||
]
|
||||
|
||||
def test_create_ndarray(self):
|
||||
"""Test NumPy int32 ndarray creation from various structured inputs."""
|
||||
for data_str, expected_array in self.test_cases:
|
||||
with self.subTest(data=data_str):
|
||||
retval = self.node.f(data_str)[0]
|
||||
|
||||
# Ensure the returned value is a NumPy array
|
||||
self.assertTrue(isinstance(retval, np.ndarray), f"Expected ndarray, got {type(retval)}")
|
||||
|
||||
# Ensure the dtype is int32
|
||||
self.assertEqual(retval.dtype, np.int32, f"Dtype mismatch for input: {data_str}")
|
||||
|
||||
# Ensure the shape matches
|
||||
self.assertEqual(retval.shape, expected_array.shape,
|
||||
f"Shape mismatch for input: {data_str} (Expected {expected_array.shape}, got {retval.shape})")
|
||||
|
||||
# Ensure values match
|
||||
np.testing.assert_array_equal(retval, expected_array,
|
||||
f"Value mismatch for input: {data_str}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,61 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_create_from_numpy import PandasCreateFromNumpy
|
||||
|
||||
class TestPandasCreateFromNumpy(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasCreateFromNumpy()
|
||||
|
||||
self.test_cases = [
|
||||
# Rank 0
|
||||
(np.array(42), ValueError),
|
||||
(np.array(3.14), ValueError),
|
||||
(np.array("hello"), ValueError),
|
||||
(np.array(True), ValueError),
|
||||
|
||||
# Rank 1
|
||||
(np.array([1, 2, 3]), pd.DataFrame([1, 2, 3])),
|
||||
(np.array([1.1, 2.2, 3.3]), pd.DataFrame([1.1, 2.2, 3.3])),
|
||||
(np.array(["a", "b", "c"]), pd.DataFrame(["a", "b", "c"])),
|
||||
(np.array([True, False, True]), pd.DataFrame([True, False, True])),
|
||||
|
||||
# Rank 2
|
||||
(np.array([[1, 2], [3, 4]]), pd.DataFrame([[1, 2], [3, 4]])),
|
||||
(np.array([[1.1, 2.2], [3.3, 4.4]]), pd.DataFrame([[1.1, 2.2], [3.3, 4.4]])),
|
||||
(np.array([["a", "b"], ["c", "d"]]), pd.DataFrame([["a", "b"], ["c", "d"]])),
|
||||
(np.array([[True, False], [False, True]]), pd.DataFrame([[True, False], [False, True]])),
|
||||
|
||||
# Rank 3 (Invalid)
|
||||
(np.array([[[1, 2], [3, 4]]]), ValueError),
|
||||
(np.array([[["x", "y"], ["z", "w"]]]), ValueError),
|
||||
|
||||
# Rank 4 (Invalid)
|
||||
(np.random.rand(2, 2, 2, 2), ValueError)
|
||||
]
|
||||
|
||||
def test_create_dataframe(self):
|
||||
"""Test DataFrame creation from various NumPy arrays."""
|
||||
for data, expected in self.test_cases:
|
||||
with self.subTest(data=data):
|
||||
if isinstance(expected, type) and issubclass(expected, Exception):
|
||||
with self.assertRaises(expected):
|
||||
self.node.f(data)
|
||||
else:
|
||||
retval = self.node.f(data)[0]
|
||||
|
||||
# Test if DataFrame structure matches
|
||||
self.assertTrue(isinstance(retval, pd.DataFrame))
|
||||
|
||||
# Test if data matches
|
||||
pd.testing.assert_frame_equal(retval, expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_to_numpy import PandasToNumpy
|
||||
|
||||
class TestPandasToNumpy(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasToNumpy()
|
||||
|
||||
self.test_cases = [
|
||||
# Rank 1
|
||||
(pd.DataFrame([1, 2, 3]), np.array([[1], [2], [3]])),
|
||||
(pd.DataFrame([1.1, 2.2, 3.3]), np.array([[1.1], [2.2], [3.3]])),
|
||||
(pd.DataFrame(["a", "b", "c"]), np.array([["a"], ["b"], ["c"]], dtype=object)),
|
||||
(pd.DataFrame([True, False, True]), np.array([[True], [False], [True]])),
|
||||
|
||||
# Rank 2
|
||||
(pd.DataFrame([[1, 2], [3, 4]]), np.array([[1, 2], [3, 4]])),
|
||||
(pd.DataFrame([[1.1, 2.2], [3.3, 4.4]]), np.array([[1.1, 2.2], [3.3, 4.4]])),
|
||||
(pd.DataFrame([["a", "b"], ["c", "d"]]), np.array([["a", "b"], ["c", "d"]], dtype=object)),
|
||||
(pd.DataFrame([[True, False], [False, True]]), np.array([[True, False], [False, True]])),
|
||||
|
||||
# Invalid cases
|
||||
(None, TypeError),
|
||||
("not a DataFrame", TypeError),
|
||||
(np.array([[1, 2], [3, 4]]), TypeError)
|
||||
]
|
||||
|
||||
def test_dataframe_to_numpy(self):
|
||||
"""Test NumPy array conversion from various Pandas DataFrames."""
|
||||
for dataframe, expected in self.test_cases:
|
||||
with self.subTest(dataframe=dataframe):
|
||||
if isinstance(expected, type) and issubclass(expected, Exception):
|
||||
with self.assertRaises(expected):
|
||||
self.node.f(dataframe)
|
||||
else:
|
||||
retval = self.node.f(dataframe)[0]
|
||||
|
||||
# Ensure the return type is a NumPy array
|
||||
self.assertTrue(isinstance(retval, np.ndarray))
|
||||
|
||||
# Check array equality
|
||||
np.testing.assert_array_equal(retval, expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,65 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
|
||||
from pytorch_wrapper.pt_float_create import PtFloatCreate
|
||||
|
||||
class TestPtFloatCreate(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PtFloatCreate()
|
||||
|
||||
# Test cases covering rank 0 (scalar) to rank 5 tensors
|
||||
self.test_cases = [
|
||||
# Rank 0 (scalar)
|
||||
("12345", torch.tensor(12345, dtype=torch.float32)),
|
||||
("12345.6", torch.tensor(12345.6, dtype=torch.float32)),
|
||||
|
||||
# Rank 1 (1D array)
|
||||
("[1, 2, 3]", torch.tensor([1, 2, 3], dtype=torch.float32)),
|
||||
("[1.1, 2.2, 3.3]", torch.tensor([1.1, 2.2, 3.3], dtype=torch.float32)),
|
||||
|
||||
# Rank 2 (2D matrix)
|
||||
("[[1, 2, 3], [4, 5, 6]]", torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32)),
|
||||
("[[1.1, 2.2], [3.3, 4.4]]", torch.tensor([[1.1, 2.2], [3.3, 4.4]], dtype=torch.float32)),
|
||||
|
||||
# Rank 3 (3D tensor)
|
||||
("[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]",
|
||||
torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.float32)),
|
||||
|
||||
# Rank 4 (4D tensor)
|
||||
("[[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]]",
|
||||
torch.tensor([[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]], dtype=torch.float32)),
|
||||
|
||||
# Rank 5 (5D tensor)
|
||||
("[[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]]",
|
||||
torch.tensor([[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]], dtype=torch.float32)),
|
||||
]
|
||||
|
||||
def test_create_tensor(self):
|
||||
"""Test PyTorch tensor creation from various structured inputs."""
|
||||
for data_str, expected_tensor in self.test_cases:
|
||||
with self.subTest(data=data_str):
|
||||
retval = self.node.f(data_str)[0]
|
||||
|
||||
# Ensure the returned value is a PyTorch tensor
|
||||
self.assertTrue(isinstance(retval, torch.Tensor), f"Expected tensor, got {type(retval)}")
|
||||
|
||||
# Ensure the dtype is float32
|
||||
self.assertEqual(retval.dtype, torch.float32, f"Dtype mismatch for input: {data_str}")
|
||||
|
||||
# Ensure the shape matches
|
||||
self.assertEqual(retval.shape, expected_tensor.shape,
|
||||
f"Shape mismatch for input: {data_str} (Expected {expected_tensor.shape}, got {retval.shape})")
|
||||
|
||||
# Ensure values match
|
||||
torch.testing.assert_close(retval, expected_tensor,
|
||||
msg=f"Value mismatch for input: {data_str}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,65 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
import numpy as np
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
|
||||
from pytorch_wrapper.pt_from_numpy import PtFromNumpy
|
||||
|
||||
|
||||
class TestPtFromNumpy(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PtFromNumpy()
|
||||
|
||||
# Test cases covering rank 0 (scalar) to rank 3 tensors
|
||||
self.test_cases = [
|
||||
# Rank 0 (scalar) int32
|
||||
(np.array(12345, dtype=np.int32), torch.tensor(12345, dtype=torch.int32)),
|
||||
# Rank 0 (scalar) float32
|
||||
(np.array(12345.6, dtype=np.float32), torch.tensor(12345.6, dtype=torch.float32)),
|
||||
|
||||
# Rank 1 (1D array) int32
|
||||
(np.array([1, 2, 3], dtype=np.int32), torch.tensor([1, 2, 3], dtype=torch.int32)),
|
||||
# Rank 1 (1D array) float32
|
||||
(np.array([1.1, 2.2, 3.3], dtype=np.float32), torch.tensor([1.1, 2.2, 3.3], dtype=torch.float32)),
|
||||
|
||||
# Rank 2 (2D matrix) int32
|
||||
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int32)),
|
||||
# Rank 2 (2D matrix) float32
|
||||
(np.array([[1.1, 2.2], [3.3, 4.4]], dtype=np.float32), torch.tensor([[1.1, 2.2], [3.3, 4.4]], dtype=torch.float32)),
|
||||
|
||||
# Rank 3 (3D tensor) int32
|
||||
(np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.int32),
|
||||
torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.int32)),
|
||||
# Rank 3 (3D tensor) float32
|
||||
(np.array([[[1.1, 2.2], [3.3, 4.4]], [[5.5, 6.6], [7.7, 8.8]]], dtype=np.float32),
|
||||
torch.tensor([[[1.1, 2.2], [3.3, 4.4]], [[5.5, 6.6], [7.7, 8.8]]], dtype=torch.float32)),
|
||||
]
|
||||
|
||||
def test_convert_numpy_to_tensor(self):
|
||||
"""Test conversion of NumPy ndarray to PyTorch tensor."""
|
||||
for np_array, expected_tensor in self.test_cases:
|
||||
with self.subTest(data=np_array):
|
||||
retval = self.node.f(np_array)[0]
|
||||
|
||||
# Ensure the returned value is a PyTorch tensor
|
||||
self.assertTrue(isinstance(retval, torch.Tensor), f"Expected tensor, got {type(retval)}")
|
||||
|
||||
# Ensure the dtype matches
|
||||
self.assertEqual(retval.dtype, expected_tensor.dtype, f"Dtype mismatch for input: {np_array}")
|
||||
|
||||
# Ensure the shape matches
|
||||
self.assertEqual(retval.shape, expected_tensor.shape,
|
||||
f"Shape mismatch for input: {np_array} (Expected {expected_tensor.shape}, got {retval.shape})")
|
||||
|
||||
# Ensure values match
|
||||
torch.testing.assert_close(retval, expected_tensor,
|
||||
msg=f"Value mismatch for input: {np_array}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
|
||||
from pytorch_wrapper.pt_int_create import PtIntCreate
|
||||
|
||||
class TestPtIntCreate(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PtIntCreate()
|
||||
|
||||
# Test cases covering rank 0 (scalar) to rank 5 tensors
|
||||
self.test_cases = [
|
||||
# Rank 0 (scalar)
|
||||
("12345", torch.tensor(12345, dtype=torch.int32)),
|
||||
|
||||
# Rank 1 (1D array)
|
||||
("[1, 2, 3]", torch.tensor([1, 2, 3], dtype=torch.int32)),
|
||||
|
||||
# Rank 2 (2D matrix)
|
||||
("[[1, 2, 3], [4, 5, 6]]", torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int32)),
|
||||
|
||||
# Rank 3 (3D tensor)
|
||||
("[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]",
|
||||
torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.int32)),
|
||||
|
||||
# Rank 4 (4D tensor)
|
||||
("[[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]]",
|
||||
torch.tensor([[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]], dtype=torch.int32)),
|
||||
|
||||
# Rank 5 (5D tensor)
|
||||
("[[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]]",
|
||||
torch.tensor([[[[[1], [2]], [[3], [4]]]], [[[[5], [6]], [[7], [8]]]]], dtype=torch.int32)),
|
||||
]
|
||||
|
||||
def test_create_tensor(self):
|
||||
"""Test PyTorch tensor creation from various structured inputs."""
|
||||
for data_str, expected_tensor in self.test_cases:
|
||||
with self.subTest(data=data_str):
|
||||
retval = self.node.f(data_str)[0]
|
||||
|
||||
# Ensure the returned value is a PyTorch tensor
|
||||
self.assertTrue(isinstance(retval, torch.Tensor), f"Expected tensor, got {type(retval)}")
|
||||
|
||||
# Ensure the dtype is int32
|
||||
self.assertEqual(retval.dtype, torch.int32, f"Dtype mismatch for input: {data_str}")
|
||||
|
||||
# Ensure the shape matches
|
||||
self.assertEqual(retval.shape, expected_tensor.shape,
|
||||
f"Shape mismatch for input: {data_str} (Expected {expected_tensor.shape}, got {retval.shape})")
|
||||
|
||||
# Ensure values match
|
||||
torch.testing.assert_close(retval, expected_tensor,
|
||||
msg=f"Value mismatch for input: {data_str}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
import numpy as np
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
|
||||
from pytorch_wrapper.pt_to_numpy import PtToNumpy
|
||||
|
||||
class TestPtToNumpy(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PtToNumpy()
|
||||
|
||||
# Test cases covering rank 0 (scalar) to rank 5 tensors
|
||||
self.test_cases = [
|
||||
# Rank 0 (scalar) int32
|
||||
(torch.tensor(12345, dtype=torch.int32), np.array(12345, dtype=np.int32)),
|
||||
# Rank 0 (scalar) float32
|
||||
(torch.tensor(12345.6, dtype=torch.float32), np.array(12345.6, dtype=np.float32)),
|
||||
|
||||
# Rank 1 (1D array) int32
|
||||
(torch.tensor([1, 2, 3], dtype=torch.int32), np.array([1, 2, 3], dtype=np.int32)),
|
||||
# Rank 1 (1D array) float32
|
||||
(torch.tensor([1.1, 2.2, 3.3], dtype=torch.float32), np.array([1.1, 2.2, 3.3], dtype=np.float32)),
|
||||
|
||||
# Rank 2 (2D matrix) int32
|
||||
(torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int32), np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)),
|
||||
# Rank 2 (2D matrix) float32
|
||||
(torch.tensor([[1.1, 2.2], [3.3, 4.4]], dtype=torch.float32), np.array([[1.1, 2.2], [3.3, 4.4]], dtype=np.float32)),
|
||||
|
||||
# Rank 3 (3D tensor) int32
|
||||
(torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.int32),
|
||||
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.int32)),
|
||||
# Rank 3 (3D tensor) float32
|
||||
(torch.tensor([[[1.1, 2.2], [3.3, 4.4]], [[5.5, 6.6], [7.7, 8.8]]], dtype=torch.float32),
|
||||
np.array([[[1.1, 2.2], [3.3, 4.4]], [[5.5, 6.6], [7.7, 8.8]]], dtype=np.float32)),
|
||||
]
|
||||
|
||||
def test_convert_tensor_to_numpy(self):
|
||||
"""Test conversion of PyTorch tensor to NumPy ndarray."""
|
||||
for torch_tensor, expected_array in self.test_cases:
|
||||
with self.subTest(data=torch_tensor):
|
||||
retval = self.node.f(torch_tensor)[0]
|
||||
|
||||
# Ensure the returned value is a NumPy array
|
||||
self.assertTrue(isinstance(retval, np.ndarray), f"Expected ndarray, got {type(retval)}")
|
||||
|
||||
# Ensure the dtype matches
|
||||
self.assertEqual(retval.dtype, expected_array.dtype, f"Dtype mismatch for input: {torch_tensor}")
|
||||
|
||||
# Ensure the shape matches
|
||||
self.assertEqual(retval.shape, expected_array.shape,
|
||||
f"Shape mismatch for input: {torch_tensor} (Expected {expected_array.shape}, got {retval.shape})")
|
||||
|
||||
# Ensure values match
|
||||
np.testing.assert_array_equal(retval, expected_array,
|
||||
f"Value mismatch for input: {torch_tensor}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user