Initial code commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
def common_input_types():
|
||||
"""
|
||||
Defines the input types for a plot function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"dataframe_json": ("STRING", {"multiscatter": True}),
|
||||
"x_column_name": ("STRING", {"default": ""}),
|
||||
"y_column_name": ("STRING", {"default": ""}),
|
||||
"title": ("STRING", {"default": ""}),
|
||||
"x_axis_label": ("STRING", {"default": ""}),
|
||||
"y_axis_label": ("STRING", {"default": ""}),
|
||||
"x_tick_as_int": ("BOOLEAN", {"default": False}) # To show 2002 instead of 2002.5 on x-axis
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
from typing import Any, Dict
|
||||
from matplotlib import pyplot as plt
|
||||
import pandas as pd
|
||||
from .common import common_input_types
|
||||
from .util import plot_post_steps
|
||||
|
||||
class MPLBar:
|
||||
"""
|
||||
MPLBar:
|
||||
A class for generating bar charts from a pandas DataFrame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `bar_chart` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return common_input_types()
|
||||
|
||||
RETURN_TYPES: tuple = ("IMAGE",)
|
||||
FUNCTION: str = "bar_chart"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def bar_chart(self,
|
||||
dataframe_json: str,
|
||||
x_column_name: str,
|
||||
y_column_name: str,
|
||||
title: str,
|
||||
x_axis_label: str,
|
||||
y_axis_label: str,
|
||||
x_tick_as_int: bool) -> tuple:
|
||||
"""
|
||||
Generates a bar chart from a pandas DataFrame.
|
||||
|
||||
Args:
|
||||
dataframe_json (str): A JSON string representation of the DataFrame.
|
||||
x_column_name (str): The column name for the x-axis.
|
||||
y_column_name (str): The column name for the y-axis.
|
||||
title (str): The title of the plot.
|
||||
x_axis_label (str): The label for the x-axis data.
|
||||
y_axis_label (str): The label for the y-axis data.
|
||||
x_tick_as_int (bool): If set, use integers for x ticks. For example,
|
||||
if ticks show floating point numbers for years, e.g. 2002.5,
|
||||
setting this flag will show integer values instead.
|
||||
Returns:
|
||||
tuple: A tuple containing the image tensor representation of the plot.
|
||||
"""
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(dataframe_json)
|
||||
|
||||
# Create the plot
|
||||
fig, ax = plt.subplots()
|
||||
ax.bar(df[x_column_name], df[y_column_name])
|
||||
|
||||
return plot_post_steps(
|
||||
fig,
|
||||
ax,
|
||||
plt,
|
||||
title,
|
||||
x_axis_label,
|
||||
y_axis_label,
|
||||
x_tick_as_int)
|
||||
@@ -0,0 +1,65 @@
|
||||
from typing import Any, Dict
|
||||
from matplotlib import pyplot as plt
|
||||
import pandas as pd
|
||||
from .common import common_input_types
|
||||
from .util import plot_post_steps
|
||||
|
||||
class MPLLine:
|
||||
"""
|
||||
MPLPlotLine:
|
||||
A class for generating line plots from a pandas DataFrame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `line_plot` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return common_input_types()
|
||||
|
||||
RETURN_TYPES: tuple = ("IMAGE",)
|
||||
FUNCTION: str = "line_plot"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def line_plot(self,
|
||||
dataframe_json: str,
|
||||
x_column_name: str,
|
||||
y_column_name: str,
|
||||
title: str,
|
||||
x_axis_label: str,
|
||||
y_axis_label: str,
|
||||
x_tick_as_int: bool) -> tuple:
|
||||
"""
|
||||
Generates a line plot from a pandas DataFrame.
|
||||
|
||||
Args:
|
||||
dataframe_json (str): A JSON string representation of the DataFrame.
|
||||
x_column_name (str): The column name for the x-axis.
|
||||
y_column_name (str): The column name for the y-axis.
|
||||
title (str): The title of the plot.
|
||||
x_axis_label (str): The label for the x-axis data.
|
||||
y_axis_label (str): The label for the y-axis data.
|
||||
x_tick_as_int (bool): If set, use integers for x ticks. For example,
|
||||
if ticks show floating point numbers for years, e.g. 2002.5,
|
||||
setting this flag will show integer values instead.
|
||||
Returns:
|
||||
tuple: A tuple containing the image tensor representation of the plot.
|
||||
"""
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(dataframe_json)
|
||||
|
||||
# Create the plot
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot(df[x_column_name], df[y_column_name])
|
||||
|
||||
return plot_post_steps(
|
||||
fig,
|
||||
ax,
|
||||
plt,
|
||||
title,
|
||||
x_axis_label,
|
||||
y_axis_label,
|
||||
x_tick_as_int)
|
||||
@@ -0,0 +1,65 @@
|
||||
from typing import Any, Dict
|
||||
from matplotlib import pyplot as plt
|
||||
import pandas as pd
|
||||
from .common import common_input_types
|
||||
from .util import plot_post_steps
|
||||
|
||||
class MPLScatter:
|
||||
"""
|
||||
MPLPlotScatter:
|
||||
A class for generating scatter plots from a pandas DataFrame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `scatter_plot` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return common_input_types()
|
||||
|
||||
RETURN_TYPES: tuple = ("IMAGE",)
|
||||
FUNCTION: str = "scatter_plot"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def scatter_plot(self,
|
||||
dataframe_json: str,
|
||||
x_column_name: str,
|
||||
y_column_name: str,
|
||||
title: str,
|
||||
x_axis_label: str,
|
||||
y_axis_label: str,
|
||||
x_tick_as_int: bool) -> tuple:
|
||||
"""
|
||||
Generates a scatter plot from a pandas DataFrame.
|
||||
|
||||
Args:
|
||||
dataframe_json (str): A JSON string representation of the DataFrame.
|
||||
x_column_name (str): The column name for the x-axis.
|
||||
y_column_name (str): The column name for the y-axis.
|
||||
title (str): The title of the plot.
|
||||
x_axis_label (str): The label for the x-axis data.
|
||||
y_axis_label (str): The label for the y-axis data.
|
||||
x_tick_as_int (bool): If set, use integers for x ticks. For example,
|
||||
if ticks show floating point numbers for years, e.g. 2002.5,
|
||||
setting this flag will show integer values instead.
|
||||
Returns:
|
||||
tuple: A tuple containing the image tensor representation of the plot.
|
||||
"""
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(dataframe_json)
|
||||
|
||||
# Create the plot
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot(df[x_column_name], df[y_column_name])
|
||||
|
||||
return plot_post_steps(
|
||||
fig,
|
||||
ax,
|
||||
plt,
|
||||
title,
|
||||
x_axis_label,
|
||||
y_axis_label,
|
||||
x_tick_as_int)
|
||||
@@ -0,0 +1,56 @@
|
||||
import io
|
||||
from typing import Any, Dict
|
||||
import numpy as np
|
||||
import matplotlib.figure
|
||||
import matplotlib.axes
|
||||
from matplotlib import pyplot as plt
|
||||
import matplotlib.ticker as mticker
|
||||
import pandas as pd
|
||||
from PIL import Image
|
||||
import torch
|
||||
|
||||
|
||||
def plot_post_steps(
|
||||
fig: matplotlib.figure.Figure,
|
||||
ax: matplotlib.axes.Axes,
|
||||
plt: plt,
|
||||
title: str,
|
||||
x_axis_label: str,
|
||||
y_axis_label: str,
|
||||
x_tick_as_int: bool
|
||||
) -> tuple:
|
||||
r"""Plots a post-processed step plot using Matplotlib.
|
||||
|
||||
Args:
|
||||
fig (matplotlib.figure.Figure): The figure object for the plot.
|
||||
ax (matplotlib.axes.Axes): The axes object for the plot.
|
||||
plt (module): The Matplotlib module.
|
||||
title (str): The title of the plot.
|
||||
x_axis_label (str): The label for the x-axis.
|
||||
y_axis_label (str): The label for the y-axis.
|
||||
x_tick_as_int (bool): If ``True``, use integers for x-axis ticks.
|
||||
For example, if ticks display floating point numbers (e.g., 2002.5),
|
||||
setting this flag forces integer values instead.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the image tensor representation of the plot.
|
||||
"""
|
||||
if x_tick_as_int:
|
||||
ax.xaxis.set_major_locator(mticker.MaxNLocator(integer=True))
|
||||
|
||||
# Set title and labels
|
||||
ax.set_title(title)
|
||||
ax.set_xlabel(x_axis_label)
|
||||
ax.set_ylabel(y_axis_label)
|
||||
|
||||
# Save the plot to a buffer
|
||||
buf = io.BytesIO()
|
||||
fig.savefig(buf, format='PNG')
|
||||
buf.seek(0)
|
||||
plt.close(fig) # Close the figure to free memory
|
||||
|
||||
# Load image and convert to tensor
|
||||
image = Image.open(buf).convert("RGB")
|
||||
image_tensor = torch.from_numpy(np.array(image)).float() / 255.0 # Normalize to [0,1]
|
||||
image_tensor = image_tensor.unsqueeze(0)
|
||||
return (image_tensor,)
|
||||
Reference in New Issue
Block a user