Skip to content

Commit fd0c619

Browse files
authored
✨Factory function for user-facing plot functions (#33)
* ✨ Feat(vuecore/plots/plot_factory.py): Create factory function to create plots, avoiding writing redundant code in specific plots * ✨ Feat(vuecore/plots/__init__.py): register create_plot function in the __init__.py script to limit what is exported with * * ✅ Update bar, box, line, and scatter user-facing functions with the factory function * ✅ Update box plot file with the changes that wer not saved
1 parent 4029150 commit fd0c619

File tree

7 files changed

+120
-72
lines changed

7 files changed

+120
-72
lines changed

src/vuecore/plots/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# vuecore/plots/__init__.py
2+
from .plot_factory import create_plot
3+
4+
__all__ = ["create_plot"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# vuecore/plots/basic/__init__.py
2+
from .bar import create_bar_plot
3+
from .box import create_box_plot
4+
from .line import create_line_plot
5+
from .scatter import create_scatter_plot
6+
7+
__all__ = [
8+
"create_bar_plot",
9+
"create_box_plot",
10+
"create_line_plot",
11+
"create_scatter_plot",
12+
]

src/vuecore/plots/basic/bar.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import pandas as pd
44

5-
from vuecore import EngineType
6-
from vuecore.engines import get_builder, get_saver
5+
from vuecore import EngineType, PlotType
76
from vuecore.schemas.basic.bar import BarConfig
7+
from vuecore.plots.plot_factory import create_plot
88
from vuecore.utils.docs_utils import document_pydant_params
99

1010

@@ -19,7 +19,7 @@ def create_bar_plot(
1919
Creates, styles, and optionally saves a bar plot using the specified engine.
2020
2121
This function serves as the main entry point for users to generate bar plots.
22-
It validates the provided configuration against the BarConfig schema,
22+
It validates the provided configuration against the `BarConfig` schema,
2323
retrieves the appropriate plotting builder and saver functions based on the
2424
selected engine, builds the plot, and optionally saves it to a file.
2525
@@ -63,18 +63,11 @@ def create_bar_plot(
6363
* **Python Script:** `docs/api_examples/bar_plot.py` -
6464
https://github.com/Multiomics-Analytics-Group/vuecore/blob/main/docs/api_examples/bar_plot.py
6565
"""
66-
# 1. Validate configuration using Pydantic.
67-
config = BarConfig(**kwargs)
68-
69-
# 2. Get the correct builder function from the registry.
70-
builder_func = get_builder(plot_type="bar", engine=engine)
71-
72-
# 3. Build the figure object.
73-
figure = builder_func(data, config)
74-
75-
# 4. Save the plot using the correct saver function, if a file_path is provided.
76-
if file_path:
77-
saver_func = get_saver(engine=engine)
78-
saver_func(figure, file_path)
79-
80-
return figure
66+
return create_plot(
67+
data=data,
68+
config=BarConfig,
69+
plot_type=PlotType.BAR,
70+
engine=engine,
71+
file_path=file_path,
72+
**kwargs,
73+
)

src/vuecore/plots/basic/box.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import pandas as pd
44

5-
from vuecore import EngineType
6-
from vuecore.engines import get_builder, get_saver
5+
from vuecore import EngineType, PlotType
76
from vuecore.schemas.basic.box import BoxConfig
7+
from vuecore.plots.plot_factory import create_plot
88
from vuecore.utils.docs_utils import document_pydant_params
99

1010

@@ -19,7 +19,7 @@ def create_box_plot(
1919
Creates, styles, and optionally saves a box plot using the specified engine.
2020
2121
This function serves as the main entry point for users to generate box plots.
22-
It validates the provided configuration against the BoxConfig schema,
22+
It validates the provided configuration against the `BoxConfig` schema,
2323
retrieves the appropriate plotting builder and saver functions based on the
2424
selected engine, builds the plot, and optionally saves it to a file.
2525
@@ -63,18 +63,11 @@ def create_box_plot(
6363
* **Python Script:** `docs/api_examples/box_plot.py` -
6464
https://github.com/Multiomics-Analytics-Group/vuecore/blob/main/docs/api_examples/box_plot.py
6565
"""
66-
# 1. Validate configuration using Pydantic.
67-
config = BoxConfig(**kwargs)
68-
69-
# 2. Get the correct builder function from the registry.
70-
builder_func = get_builder(plot_type="box", engine=engine)
71-
72-
# 3. Build the figure object.
73-
figure = builder_func(data, config)
74-
75-
# 4. Save the plot using the correct saver function, if a file_path is provided.
76-
if file_path:
77-
saver_func = get_saver(engine=engine)
78-
saver_func(figure, file_path)
79-
80-
return figure
66+
return create_plot(
67+
data=data,
68+
config=BoxConfig,
69+
plot_type=PlotType.BOX,
70+
engine=engine,
71+
file_path=file_path,
72+
**kwargs,
73+
)

src/vuecore/plots/basic/line.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import pandas as pd
44

5-
from vuecore import EngineType
6-
from vuecore.engines import get_builder, get_saver
5+
from vuecore import EngineType, PlotType
76
from vuecore.schemas.basic.line import LineConfig
7+
from vuecore.plots.plot_factory import create_plot
88
from vuecore.utils.docs_utils import document_pydant_params
99

1010

@@ -19,7 +19,7 @@ def create_line_plot(
1919
Creates, styles, and optionally saves a line plot using the specified engine.
2020
2121
This function serves as the main entry point for users to generate line plots.
22-
It validates the provided configuration against the LineConfig schema,
22+
It validates the provided configuration against the `LineConfig` schema,
2323
retrieves the appropriate plotting builder and saver functions based on the
2424
selected engine, builds the plot, and optionally saves it to a file.
2525
@@ -62,18 +62,11 @@ def create_line_plot(
6262
https://vuecore.readthedocs.io/en/latest/api_examples/scatter_plot.html
6363
* **Python Script:** `docs/api_examples/line_plot.py`
6464
"""
65-
# 1. Validate configuration using Pydantic
66-
config = LineConfig(**kwargs)
67-
68-
# 2. Get the correct builder function from the registry
69-
builder_func = get_builder(plot_type="line", engine=engine)
70-
71-
# 3. Build the figure object (the API doesn't know or care what type it is)
72-
figure = builder_func(data, config)
73-
74-
# 4. Save the plot using the correct saver
75-
if file_path:
76-
saver_func = get_saver(engine=engine)
77-
saver_func(figure, file_path)
78-
79-
return figure
65+
return create_plot(
66+
data=data,
67+
config=LineConfig,
68+
plot_type=PlotType.LINE,
69+
engine=engine,
70+
file_path=file_path,
71+
**kwargs,
72+
)

src/vuecore/plots/basic/scatter.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import pandas as pd
44

5-
from vuecore import EngineType
6-
from vuecore.engines import get_builder, get_saver
5+
from vuecore import EngineType, PlotType
76
from vuecore.schemas.basic.scatter import ScatterConfig
7+
from vuecore.plots.plot_factory import create_plot
88
from vuecore.utils.docs_utils import document_pydant_params
99

1010

@@ -19,7 +19,7 @@ def create_scatter_plot(
1919
Creates, styles, and optionally saves a scatter plot using the specified engine.
2020
2121
This function serves as the main entry point for users to generate scatter plots.
22-
It validates the provided configuration against the ScatterConfig schema,
22+
It validates the provided configuration against the `ScatterConfig` schema,
2323
retrieves the appropriate plotting builder and saver functions based on the
2424
selected engine, builds the plot, and optionally saves it to a file.
2525
@@ -63,18 +63,11 @@ def create_scatter_plot(
6363
* **Python Script:** `docs/api_examples/scatter_plot.py` -
6464
https://github.com/Multiomics-Analytics-Group/vuecore/blob/main/docs/api_examples/scatter_plot.py
6565
"""
66-
# 1. Validate configuration using Pydantic
67-
config = ScatterConfig(**kwargs)
68-
69-
# 2. Get the correct builder function from the registry
70-
builder_func = get_builder(plot_type="scatter", engine=engine)
71-
72-
# 3. Build the figure object (the API doesn't know or care what type it is)
73-
figure = builder_func(data, config)
74-
75-
# 4. Save the plot using the correct saver
76-
if file_path:
77-
saver_func = get_saver(engine=engine)
78-
saver_func(figure, file_path)
79-
80-
return figure
66+
return create_plot(
67+
data=data,
68+
config=ScatterConfig,
69+
plot_type=PlotType.SCATTER,
70+
engine=engine,
71+
file_path=file_path,
72+
**kwargs,
73+
)

src/vuecore/plots/plot_factory.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from typing import Any, Type
2+
import pandas as pd
3+
from vuecore import EngineType, PlotType
4+
from vuecore.engines import get_builder, get_saver
5+
from pydantic import BaseModel
6+
7+
8+
def create_plot(
9+
data: pd.DataFrame,
10+
config: Type[BaseModel],
11+
plot_type: PlotType,
12+
engine: EngineType = EngineType.PLOTLY,
13+
file_path: str = None,
14+
**kwargs,
15+
) -> Any:
16+
"""
17+
Factory function to create, style, and optionally save plots.
18+
19+
This function handles the common workflow for creating plots:
20+
1. Validate configuration using the provided Pydantic model
21+
2. Get the appropriate builder function from the engine registry
22+
3. Build the figure using the builder
23+
4. Optionally save the plot if a file path is provided
24+
25+
Parameters
26+
----------
27+
data : pd.DataFrame
28+
The DataFrame containing the data to be plotted.
29+
config : Type[BaseModel]
30+
The Pydantic config class for validation.
31+
plot_type : PlotType
32+
The plot type from the `PlotType` enum (e.g., PlotType.BAR, PlotType.BOX, etc).
33+
engine : EngineType, optional
34+
The plotting engine to use for rendering the plot.
35+
Defaults to `EngineType.PLOTLY`.
36+
file_path : str, optional
37+
If provided, the path where the final plot will be saved.
38+
**kwargs
39+
Keyword arguments for plot configuration.
40+
41+
Returns
42+
-------
43+
Any
44+
The final plot object returned by the selected engine.
45+
"""
46+
# 1. Validate configuration using Pydantic
47+
config = config(**kwargs)
48+
49+
# 2. Get the correct builder function from the registry
50+
builder_func = get_builder(plot_type=plot_type, engine=engine)
51+
52+
# 3. Build the figure object
53+
figure = builder_func(data, config)
54+
55+
# 4. Save the plot using the correct saver function, if a file_path is provided
56+
if file_path:
57+
saver_func = get_saver(engine=engine)
58+
saver_func(figure, file_path)
59+
60+
return figure

0 commit comments

Comments
 (0)