-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Config plot libraries #705
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3c79ac6
In this commit, I introduced a new configuration parameter in our app…
241bd33
This commit adds a configuration parameter for users to set their pre…
b9ddba2
viz_library_type' in test_generate_python_code_prompt.py, resolved fa…
645a7fb
Merge branch 'main' into config_plot_libraries
gventuri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import logging | ||
from typing import Union, Optional | ||
from .base import VisualizationLibrary | ||
|
||
from ._viz_library_types import ( | ||
MatplotlibVizLibraryType, | ||
PlotlyVizLibraryType, | ||
SeabornVizLibraryType, | ||
) | ||
from .. import Logger | ||
|
||
|
||
viz_lib_map = { | ||
VisualizationLibrary.MATPLOTLIB.value: MatplotlibVizLibraryType, | ||
VisualizationLibrary.PLOTLY.value: PlotlyVizLibraryType, | ||
VisualizationLibrary.SEABORN.value: SeabornVizLibraryType, | ||
} | ||
|
||
|
||
def viz_lib_type_factory( | ||
viz_lib_type: str = None, logger: Optional[Logger] = None | ||
) -> Union[MatplotlibVizLibraryType, PlotlyVizLibraryType, SeabornVizLibraryType,]: | ||
""" | ||
Factory function to get appropriate instance for viz library type. | ||
|
||
Uses `viz_library_types_map` to determine the viz library type class. | ||
|
||
Args: | ||
viz_lib_type (Optional[str]): A name of the viz library type. | ||
Defaults to None, an instance of `DefaultVizLibraryType` will be | ||
returned. | ||
logger (Optional[str]): If passed, collects logs about correctness | ||
of the `viz_library_type` argument and what kind of VizLibraryType | ||
is created. | ||
|
||
Returns: | ||
(Union[ | ||
MatplotlibVizLibraryType, | ||
PlotlyVizLibraryType, | ||
SeabornVizLibraryType, | ||
DefaultVizLibraryType | ||
]): An instance of the output type. | ||
""" | ||
|
||
if viz_lib_type is not None and viz_lib_type not in viz_lib_map and logger: | ||
possible_types_msg = ", ".join(f"'{type_}'" for type_ in viz_lib_map) | ||
logger.log( | ||
f"Unknown value for the parameter `viz_library_type`: '{viz_lib_type}'." | ||
f"Possible values are: {possible_types_msg} and None for default " | ||
f"viz library type (miscellaneous).", | ||
level=logging.WARNING, | ||
) | ||
|
||
viz_lib_default = MatplotlibVizLibraryType | ||
viz_lib_type_helper = viz_lib_map.get(viz_lib_type, viz_lib_default)() | ||
|
||
if logger: | ||
logger.log( | ||
f"{viz_lib_type_helper.__class__} is going to be used.", level=logging.DEBUG | ||
) | ||
|
||
return viz_lib_type_helper | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from abc import abstractmethod, ABC | ||
from typing import Any, Iterable | ||
|
||
|
||
class BaseVizLibraryType(ABC): | ||
@property | ||
@abstractmethod | ||
def template_hint(self) -> str: | ||
... | ||
|
||
@property | ||
@abstractmethod | ||
def name(self) -> str: | ||
... | ||
|
||
def _validate_type(self, actual_type: str) -> bool: | ||
if actual_type != self.name: | ||
return False | ||
return True | ||
|
||
def validate(self, result: dict[str, Any]) -> tuple[bool, Iterable[str]]: | ||
""" | ||
Validate 'type' and 'constraint' from the result dict. | ||
|
||
Args: | ||
result (dict[str, Any]): The result of code execution in | ||
dict representation. Should have the following schema: | ||
{ | ||
"viz_library_type": <viz_library_name> | ||
} | ||
|
||
Returns: | ||
(tuple(bool, Iterable(str)): | ||
Boolean value whether the result matches output type | ||
and collection of logs containing messages about | ||
'type' mismatches. | ||
""" | ||
validation_logs = [] | ||
actual_type = result.get("type") | ||
|
||
type_ok = self._validate_type(actual_type) | ||
if not type_ok: | ||
validation_logs.append( | ||
f"The result dict contains inappropriate 'type'. " | ||
f"Expected '{self.name}', actual '{actual_type}'." | ||
) | ||
|
||
return type_ok, validation_logs | ||
|
||
|
||
class MatplotlibVizLibraryType(BaseVizLibraryType): | ||
@property | ||
def template_hint(self): | ||
return """When a user requests to create a chart, utilize the Python matplotlib | ||
library to generate high-quality graphics that will be saved | ||
directly to a file. | ||
If you import matplotlib use the 'agg' backend for rendering plots.""" | ||
|
||
@property | ||
def name(self): | ||
return "matplotlib" | ||
|
||
|
||
class PlotlyVizLibraryType(BaseVizLibraryType): | ||
@property | ||
def template_hint(self): | ||
return """When a user requests to create a chart, utilize the Python plotly | ||
library to generate high-quality graphics that will be saved | ||
directly to a file. | ||
If you import matplotlib use the 'agg' backend for rendering plots.""" | ||
|
||
@property | ||
def name(self): | ||
return "plotly" | ||
|
||
|
||
class SeabornVizLibraryType(BaseVizLibraryType): | ||
@property | ||
def template_hint(self): | ||
return """When a user requests to create a chart, utilize the Python Seaborn | ||
library to generate high-quality graphics that will be saved | ||
directly to a file. | ||
If you import matplotlib use the 'agg' backend for rendering plots.""" | ||
|
||
@property | ||
def name(self): | ||
return "seaborn" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from enum import Enum | ||
|
||
|
||
class VisualizationLibrary(str, Enum): | ||
""" | ||
VisualizationLibrary is an enumeration that represents the available | ||
data visualization libraries. | ||
|
||
Attributes: | ||
MATPLOTLIB (str): Represents the Matplotlib library. | ||
SEABORN (str): Represents the Seaborn library. | ||
PLOTLY (str): Represents the Plotly library. | ||
""" | ||
|
||
MATPLOTLIB = "matplotlib" | ||
SEABORN = "seaborn" | ||
PLOTLY = "plotly" | ||
|
||
DEFAULT = "default" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_load_data_viz_library
method correctly sets the_data_viz_library
attribute based on thedata_viz_library
argument. However, the method's docstring needs to be updated to reflect its functionality and the possible exceptions it might raise.