From 0184921322270d13259be5505fa6d9b6c7231aa3 Mon Sep 17 00:00:00 2001 From: joey-kilgore Date: Sat, 1 Feb 2025 02:11:04 -0500 Subject: [PATCH] feat: add skip cloud save cloud saves were previously required, which meant you needed to setup the kachery-cloud connection (setting up account and registering your machine) and so this allows for skipping this step for anyone who wants to pickle their plots to have for reference, but doesn't want to have to setup kachery-cloud. resolves #11 --- src/SimLogger/FigLogger.py | 32 ++++++++++++++++++++++++++------ tests/test_FigLogger.py | 12 ++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 tests/test_FigLogger.py diff --git a/src/SimLogger/FigLogger.py b/src/SimLogger/FigLogger.py index 93af59b..ee1e1b1 100644 --- a/src/SimLogger/FigLogger.py +++ b/src/SimLogger/FigLogger.py @@ -16,6 +16,7 @@ def createPlot( title="Plot", labels={"x": "x", "y": "y", "z": "z"}, makeNote=True, + cloudSave=True, **kwargs, ): """Generate a plot that will automatically be saved (using SimLogger) @@ -35,10 +36,12 @@ def createPlot( labels ({str:str}): Key value pairs for the keys "x", "y", ("z") that go to the values corresponding to the axis labels makeNote (bool): Whether the log should be noted with the saved files + cloudSave (bool): optionally save to the cloud (default True) **kwargs: Additional args for the plotly express graph Returns: - url (str): The figurl link to access the graph + url (str): The figurl link to access the graph. + Returns None if cloud save not chosen """ data = {"x": x, "y": y} if z is not None: @@ -59,13 +62,25 @@ def createPlot( raise ValueError("Unsupported plotType. Supported types: 'scatter', 'line'.") url = savePlotly( - simTag, objTag, fig, label=title, objFolder=objFolder, makeNote=makeNote + simTag, + objTag, + fig, + label=title, + objFolder=objFolder, + makeNote=makeNote, + cloudSave=cloudSave, ) return url def savePlotly( - simTag, objTag, ff, label="", objFolder=os.path.join("data", "obj"), makeNote=True + simTag, + objTag, + ff, + label="", + objFolder=os.path.join("data", "obj"), + makeNote=True, + cloudSave=True, ): """Save a previously created plotly graph to the cloud for remote access and pickle the graph for access later @@ -77,14 +92,19 @@ def savePlotly( label (str): Optional label for the figurl cloud saved graph objFolder (str): location where the pickled objects should go makeNote (str): Note in the log that the pickled file was saved + cloudSave (bool): save the figure to cloud Returns: - url (str): The figurl link to access the graph + url (str): The figurl link to access the graph. + Returns None if not saving to the cloud """ if label == "": label = objTag - url = fig.Plotly(ff).url(label=label) - SimLogger.logNotes(f"{label} GRAPH AVAILABLE AT: {url}") + + url = None + if cloudSave: + url = fig.Plotly(ff).url(label=label) + SimLogger.logNotes(f"{label} GRAPH AVAILABLE AT: {url}") SimLogger.saveObj(simTag, objTag, ff, objFolder=objFolder, makeNote=makeNote) return url diff --git a/tests/test_FigLogger.py b/tests/test_FigLogger.py new file mode 100644 index 0000000..fb7dce9 --- /dev/null +++ b/tests/test_FigLogger.py @@ -0,0 +1,12 @@ +from SimLogger import SimLogger, FigLogger + + +def test_graph_save(): + x = [0, 1, 2, 3] + y = [1, 2, 3, 4] + simTag = "testing-simTag" + objTag = "testing-graph" + FigLogger.createPlot(simTag, objTag, x, y, cloudSave=False) + loadedFig = SimLogger.getObj(simTag, objTag) + assert list(loadedFig["data"][0]["x"]) == x + assert list(loadedFig["data"][0]["y"]) == y