Added Pandas Feature Split To Pt and Pandas Replace Advanced nodes.

This commit is contained in:
HowToSD
2025-03-14 02:53:10 -07:00
parent 4d05b00f45
commit a8aaa21ce0
9 changed files with 303 additions and 1 deletions
@@ -0,0 +1,50 @@
import os
import sys
import unittest
import numpy as np
import pandas as pd
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 pandas_wrapper.pandas_feature_split_to_pt import PandasFeatureSplitToPt
class TestPandasFeatureSplitToPt(unittest.TestCase):
def setUp(self):
"""Set up test data."""
self.node = PandasFeatureSplitToPt()
# Sample DataFrame
self.df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
})
def test_feature_split(self):
"""Test splitting with different label positions."""
test_cases = [
(0, True, [[4, 7], [5, 8], [6, 9]], [1, 2, 3]),
(1, True, [[1, 7], [2, 8], [3, 9]], [4, 5, 6]),
(2, True, [[1, 4], [2, 5], [3, 6]], [7, 8, 9]),
(-1, True, [[1, 4], [2, 5], [3, 6]], [7, 8, 9]),
]
data_types = ["float32", "float16", "bfloat16", "float64", "int8", "uint8", "int16", "int32", "int64"]
for feature_dtype in data_types:
for label_dtype in data_types:
for label_pos, output_1d, expected_features, expected_labels in test_cases:
with self.subTest(label_pos=label_pos, output_1d=output_1d, feature_dtype=feature_dtype, label_dtype=label_dtype):
features, labels = self.node.f(self.df, label_pos, output_1d, feature_dtype, label_dtype)
torch.testing.assert_close(features, torch.tensor(expected_features, dtype=getattr(torch, feature_dtype)))
torch.testing.assert_close(labels, torch.tensor(expected_labels, dtype=getattr(torch, label_dtype)))
def test_invalid_label_position(self):
"""Test invalid label positions."""
with self.assertRaises(ValueError):
self.node.f(self.df, 3, True, "float32", "int64")
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,60 @@
import os
import sys
import unittest
import pandas as pd
import ast
from typing import Any, Dict
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_replace_advanced import PandasReplaceAdvanced
class TestPandasReplaceAdvanced(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.node = PandasReplaceAdvanced()
cls.df = pd.DataFrame(
{
"col1": ["cat", "dog", "fish"],
"col2": ["mountain", "ocean", "forest"],
}
)
def test_valid_replacement(self):
test_cases = [
('{"cat": 1, "dog": 2.34}', {"col1": [1, 2.34, "fish"], "col2": ["mountain", "ocean", "forest"]}),
('{"mountain": "hill", "forest": "jungle"}', {"col1": ["cat", "dog", "fish"], "col2": ["hill", "ocean", "jungle"]}),
]
for replacements, expected_data in test_cases:
with self.subTest(replacements=replacements):
result_df = self.node.f(self.df, replacements)[0]
expected_df = pd.DataFrame(expected_data)
pd.testing.assert_frame_equal(result_df, expected_df)
def test_invalid_replacement_value_error(self):
invalid_cases = [
'["cat", "lion"]', # List is not allowed
'{"cat": ["lion", "tiger"]}', # List is not allowed
'{"dog": {"wolf": "coyote"}}' # Nested dict is not allowed
]
for invalid_pairs in invalid_cases:
with self.subTest(invalid_pairs=invalid_pairs):
with self.assertRaises(ValueError):
self.node.f(self.df, invalid_pairs)
def test_invalid_replacement_syntax_error(self):
invalid_cases = [
'{"mountain": ()"hill", "ocean")}', # Syntax error
]
for invalid_pairs in invalid_cases:
with self.subTest(invalid_pairs=invalid_pairs):
with self.assertRaises(SyntaxError):
self.node.f(self.df, invalid_pairs)
if __name__ == "__main__":
unittest.main()