Logging Plots as PNGs#
rubicon_ml makes it easy to log
Plotly plots and images with
artifacts. In this example we’ll walk through creating a plot using
plotly.express and saving it as an
Before getting started, we’ll have to install some dependencies for this example.
! pip install kaleido Pillow
Requirement already satisfied: Pillow in /Users/nvd215/mambaforge/envs/rubicon-ml-dev/lib/python3.10/site-packages (9.2.0)
First lets set up our
import os from rubicon_ml import Rubicon rubicon = Rubicon(persistence="memory") project = rubicon.get_or_create_project("Artifact Plots")
Next, create plotting data. This example leverages
plotly.express for data and plot generation.
import plotly.express as px from plotly import data df = data.wind() df.head()
Using the plotting data generated, create a plot and save it to a file. Utilizing
write_image we can save our plot to a file. We can use this file to later log with
scatter_plot = px.scatter(df, x="direction", y="frequency", color="strength") scatter_plot.write_image("saved-scatter-plot-for-logging.png")
We can also create a plot and covert it to bytes to save it in memory. Using
to_image we can convert our plot to bytes.
bar_plot = px.bar(df, x="direction", y="frequency", color="strength") bar_plot_bytes = bar_plot.to_image(format="png")
Logging your plot#
artifacts are a catch-all for any other type of data that can be logged to a file. We can use artifacts to capture information that doesn’t fall into the
artifacts, plots can be logged just by passing its file location in the
data_path argument to
project.log_artifact( name="scatter plot", data_path="saved-scatter-plot-for-logging.png", description="saved scatter plot with path", ) artifact_plot_from_file = project.artifact(name="scatter plot")
We can also log plots by passing its byte representation to the
data_bytes argument to
project.log_artifact() as well.
project.log_artifact(name="bar plot", data_bytes=bar_plot_bytes) artifact_plot_bytes = project.artifact(name="bar plot")
Retrieving your logged plot programmatically#
PNG representations of both plots were logged, we can retrieve our artifact plots programmatically. Using
PIL we can visualize our plots as
import io from PIL import Image imageScatterPlotStream = io.BytesIO(artifact_plot_from_file.data) scatter_plot_image = Image.open(imageScatterPlotStream) imageBarPlotStream = io.BytesIO(artifact_plot_bytes.data) bar_plot_image = Image.open(imageBarPlotStream)
Finally, view the two images using
We have logged our scatter_plot with Rubicon, so we can remove the file created earlier.