Initial phase of adding PyTorch support for data analysis. Added conversion nodes.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from typing import Any, Dict
|
||||
import numpy as np
|
||||
import ast
|
||||
|
||||
|
||||
class NumpyFloatCreate:
|
||||
"""
|
||||
Creates a NumPy ndarray with 32-bit floating point precision
|
||||
using values entered in the text field.
|
||||
|
||||
category: Numpy
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"data": ("STRING", {"multiline": True}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("NDARRAY",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Numpy wrapper"
|
||||
|
||||
def f(self, data: str) -> tuple:
|
||||
"""
|
||||
Creates a Numpy ndarray using values entered in the text field.
|
||||
|
||||
Args:
|
||||
data (str): String value in the text field.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a the DataFrame.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_data = ast.literal_eval(data)
|
||||
|
||||
# Convert list to NumPy array
|
||||
array = np.array(list_data, dtype=np.float32)
|
||||
return (array,)
|
||||
@@ -0,0 +1,48 @@
|
||||
from typing import Any, Dict
|
||||
import numpy as np
|
||||
import ast
|
||||
|
||||
|
||||
class NumpyIntCreate:
|
||||
"""
|
||||
Creates a NumPy ndarray with 32-bit integer
|
||||
using values entered in the text field.
|
||||
|
||||
category: Numpy
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"data": ("STRING", {"multiline": True}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("NDARRAY",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Numpy wrapper"
|
||||
|
||||
def f(self, data: str) -> tuple:
|
||||
"""
|
||||
Creates a NumPy ndarray with 32-bit integer using values
|
||||
entered in the text field.
|
||||
|
||||
Args:
|
||||
data (str): String value in the text field.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the NumPy ndarray.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_data = ast.literal_eval(data)
|
||||
|
||||
# Convert list to NumPy int32 array
|
||||
array = np.array(list_data, dtype=np.int32)
|
||||
return (array,)
|
||||
@@ -0,0 +1,48 @@
|
||||
from typing import Any, Dict
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
|
||||
class PandasCreateFromNumpy:
|
||||
"""
|
||||
Creates a pandas DataFrame from a NumPy ndarray.
|
||||
|
||||
category: IO
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `create` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"data": ("NDARRAY", {}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("DATAFRAME",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, data: np.ndarray) -> tuple:
|
||||
"""
|
||||
Creates a pandas DataFrame from a NumPy ndarray.
|
||||
|
||||
Args:
|
||||
data (np.ndarray): A NumPy array.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the DataFrame.
|
||||
|
||||
Raises:
|
||||
ValueError: If the array is not rank 1 or rank 2.
|
||||
"""
|
||||
if len(data.shape) not in [1, 2]:
|
||||
raise ValueError("Only rank 1 and rank 2 arrays are supported.")
|
||||
|
||||
return (pd.DataFrame(data),)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
from typing import Any, Dict
|
||||
import pandas as pd
|
||||
|
||||
class PandasToNumpy:
|
||||
"""
|
||||
Converts a pandas DataFrame to Numpy ndarray.
|
||||
|
||||
category: Data type conversion
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"dataframe": ("DATAFRAME", {}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("NDARRAY",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, dataframe: pd.DataFrame) -> tuple:
|
||||
"""
|
||||
Converts a pandas DataFrame to a NumPy ndarray.
|
||||
|
||||
Args:
|
||||
dataframe (DataFrame): The DataFrame.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the NumPy representation of the DataFrame.
|
||||
"""
|
||||
if not isinstance(dataframe, pd.DataFrame):
|
||||
raise TypeError("PandasToNumpy expects pd.DataFrame.")
|
||||
return (dataframe.to_numpy(),)
|
||||
@@ -0,0 +1,47 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
|
||||
|
||||
class PtFloatCreate:
|
||||
"""
|
||||
Creates a PyTorch tensor with 32-bit floating point precision
|
||||
using values entered in the text field.
|
||||
|
||||
category: PyTorch wrapper
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"data": ("STRING", {"multiline": True}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "PyTorch wrapper"
|
||||
|
||||
def f(self, data: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor using values entered in the text field.
|
||||
|
||||
Args:
|
||||
data (str): String value in the text field.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_data = ast.literal_eval(data)
|
||||
|
||||
# Convert list to PyTorch tensor
|
||||
tensor = torch.tensor(list_data, dtype=torch.float32)
|
||||
return (tensor,)
|
||||
@@ -0,0 +1,42 @@
|
||||
from typing import Any, Dict, Tuple
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
|
||||
class PtFromNumpy:
|
||||
"""
|
||||
Converts a NumPy ndarray to a PyTorch tensor while preserving its data type.
|
||||
|
||||
category: PyTorch wrapper
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"array": ("NDARRAY", {}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "PyTorch wrapper"
|
||||
|
||||
def f(self, array: np.ndarray) -> Tuple[torch.Tensor]:
|
||||
"""
|
||||
Converts a NumPy ndarray to a PyTorch tensor while preserving its data type.
|
||||
|
||||
Args:
|
||||
array (np.ndarray): NumPy ndarray.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
tens = torch.tensor(array, dtype=torch.from_numpy(array).dtype)
|
||||
return (tens,)
|
||||
@@ -0,0 +1,47 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
|
||||
|
||||
class PtIntCreate:
|
||||
"""
|
||||
Creates a PyTorch tensor with 32-bit integer
|
||||
using values entered in the text field.
|
||||
|
||||
category: PyTorch wrapper
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"data": ("STRING", {"multiline": True}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "PyTorch wrapper"
|
||||
|
||||
def f(self, data: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor using values entered in the text field.
|
||||
|
||||
Args:
|
||||
data (str): String value in the text field.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_data = ast.literal_eval(data)
|
||||
|
||||
# Convert list to PyTorch tensor
|
||||
tensor = torch.tensor(list_data, dtype=torch.int32)
|
||||
return (tensor,)
|
||||
@@ -0,0 +1,41 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
|
||||
|
||||
class PtToNumpy:
|
||||
"""
|
||||
Converts PyTorch tensor to NumPy ndarray.
|
||||
|
||||
category: PyTorch wrapper
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"tens": ("TENSOR", {}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("NDARRAY",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "PyTorch wrapper"
|
||||
|
||||
def f(self, tens: torch.Tensor) -> tuple:
|
||||
"""
|
||||
Converts PyTorch tensor to NumPy ndarray.
|
||||
|
||||
Args:
|
||||
tens (torch.Tensor): PyTorch Tensor
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the ndarray.
|
||||
"""
|
||||
array = tens.cpu().detach().numpy()
|
||||
return (array,)
|
||||
Reference in New Issue
Block a user