Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
Removed deprecated plots API
Browse files Browse the repository at this point in the history
  • Loading branch information
urig committed Jul 26, 2022
1 parent 5564f9c commit 67a7385
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 194 deletions.
5 changes: 2 additions & 3 deletions entropylab/dashboard/pages/results/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def render_figure_tabs_from_selected_experiments_table_rows(
alert_on_fail = row_num == added_row
exp_id = data[row_num]["id"]
try:
figure_records = dashboard_data_reader.get_plot_and_figure_data(
exp_id
)
figure_records = dashboard_data_reader.get_figure_records(exp_id)
except EntropyError:
logger.exception(
f"Exception when getting figure data for exp_id={exp_id}"
Expand Down Expand Up @@ -196,6 +194,7 @@ def build_figure_tab(

def build_img_tab(img_src: str, figure_name: str, figure_key: str) -> dbc.Tab:
return dbc.Tab(
# TODO: Fit img into tab dimensions
html.Img(src=img_src),
label=figure_name,
id=f"figure-tab-{figure_key}",
Expand Down
20 changes: 8 additions & 12 deletions entropylab/dashboard/pages/results/dashboard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from entropylab.dashboard.pages.results.auto_plot import auto_plot
from entropylab.logger import logger
from entropylab.pipeline.api.data_reader import (
PlotRecord,
FigureRecord,
MatplotlibFigureRecord,
)
Expand All @@ -29,9 +28,9 @@ def get_last_experiments(
pass

@abc.abstractmethod
def get_plot_and_figure_data(
def get_figure_records(
self, exp_id: int
) -> List[PlotRecord | FigureRecord | MatplotlibFigureRecord]:
) -> List[FigureRecord | MatplotlibFigureRecord]:
pass


Expand Down Expand Up @@ -71,11 +70,9 @@ def get_last_result_of_experiment(
):
return self._db.get_last_result_of_experiment(experiment_id)

def get_plot_and_figure_data(
def get_figure_records(
self, exp_id: int
) -> List[PlotRecord | FigureRecord | MatplotlibFigureRecord]:
# Old plots
plots = self._db.get_plots(exp_id)
) -> List[FigureRecord | MatplotlibFigureRecord]:
# Plotly figures
if exp_id not in self._figures_cache:
logger.debug(f"Figures cache miss. exp_id=[{exp_id}]")
Expand All @@ -86,14 +83,13 @@ def get_plot_and_figure_data(
# Matplotlib figures
# TODO: Cache matplotlib figures?
matplotlib_figures = self._db.get_matplotlib_figures(exp_id)
if len(plots) > 0 or len(figures) > 0 or len(matplotlib_figures) > 0:
return [*plots, *figures, *matplotlib_figures]
if len(figures) > 0 or len(matplotlib_figures) > 0:
return [*figures, *matplotlib_figures]
else:
# TODO: auto_plot to produce figures, not plots
last_result = self._db.get_last_result_of_experiment(exp_id)
if last_result is not None and last_result.data is not None:
plot = auto_plot(exp_id, last_result.data)
return [plot]
figure = auto_plot(exp_id, last_result.data)
return [figure]
else:
return []

Expand Down
48 changes: 6 additions & 42 deletions entropylab/pipeline/api/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
from dataclasses import dataclass
from datetime import datetime
from typing import List, Any, Optional, Iterable
from warnings import warn

from pandas import DataFrame
from plotly import graph_objects as go

from entropylab.pipeline.api.data_writer import PlotGenerator


class ScriptViewer:
def __init__(self, stages: List[str]) -> None:
Expand Down Expand Up @@ -83,21 +80,6 @@ class ResultRecord:
time: datetime


@dataclass
class PlotRecord:
"""
A single plot information and plotting instructions that was saved during the
experiment
"""

experiment_id: int
id: int
plot_data: Any = None
generator: Optional[PlotGenerator] = None
label: Optional[str] = None
story: Optional[str] = None


@dataclass
class FigureRecord:
"""
Expand Down Expand Up @@ -239,19 +221,6 @@ def get_debug_record(self, experiment_id: int) -> Optional[DebugRecord]:
"""
pass

# noinspection PyTypeChecker
@abstractmethod
def get_plots(self, experiment_id: int) -> List[PlotRecord]:
"""
returns a list of all plots saved with the requested experiment
"""
warn(
"This method will soon be deprecated. Please use get_figures() instead",
PendingDeprecationWarning,
stacklevel=2,
)
pass

@abstractmethod
def get_figures(self, experiment_id: int) -> List[FigureRecord]:
"""
Expand Down Expand Up @@ -357,19 +326,14 @@ def get_results(self, label: Optional[str] = None) -> Iterable[ResultRecord]:
"""
return self._data_reader.get_results(self._experiment_id, label)

def get_plots(self) -> List[PlotRecord]:
"""
returns a list of plot records that were saved for current experiment
"""
warn(
"This method will soon be deprecated. Please use get_plots() instead",
PendingDeprecationWarning,
stacklevel=2,
)
return self._data_reader.get_plots(self._experiment_id)

def get_figures(self) -> List[FigureRecord]:
"""
returns a list of plotly figures that were saved for current experiment
"""
return self._data_reader.get_figures(self._experiment_id)

def get_matplotlib_figures(self) -> List[MatplotlibFigureRecord]:
"""
returns a list of matplotlib figures that were saved for current experiment
"""
return self._data_reader.get_matplotlib_figures(self._experiment_id)
29 changes: 1 addition & 28 deletions entropylab/pipeline/api/data_writer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Optional, Type
from warnings import warn
from typing import Any

import matplotlib
from bokeh.models import Renderer
Expand Down Expand Up @@ -118,17 +117,6 @@ def plot_plotly(self, figure: go.Figure, data, **kwargs) -> None:
pass


@dataclass(frozen=True, eq=True)
class PlotSpec:
"""
Description and plotting instructions for a plot that will be saved
"""

generator: Optional[Type[PlotGenerator]] = None
label: Optional[str] = None
story: Optional[str] = ""


@dataclass
class NodeData:
"""
Expand Down Expand Up @@ -185,21 +173,6 @@ def save_debug(self, experiment_id: int, debug: Debug):
"""
pass

@abstractmethod
def save_plot(self, experiment_id: int, plot: PlotSpec, data: Any):
"""
save a new plot to the db according to the PlotSpec class
:param experiment_id: the experiment id
:param plot: plotting instructions
:param data: the data of the plot
"""
warn(
"This method will soon be deprecated. Please use save_figure() instead",
PendingDeprecationWarning,
stacklevel=2,
)
pass

def save_figure(self, experiment_id: int, figure: go.Figure) -> None:
"""
saves a new plotly figure to the db and associates it with an experiment
Expand Down
12 changes: 1 addition & 11 deletions entropylab/pipeline/api/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

from plotly import graph_objects as go

from entropylab.components.lab_topology import ExperimentResources
from entropylab.pipeline.api.data_writer import (
DataWriter,
RawResultData,
Metadata,
PlotSpec,
)
from entropylab.components.lab_topology import ExperimentResources


class EntropyContext:
Expand Down Expand Up @@ -60,15 +59,6 @@ def add_metadata(self, label: str, metadata: Any):
self._exp_id, Metadata(label, self._stage_id, metadata)
)

def add_plot(self, plot: PlotSpec, data: Any):
"""
saves a new plot from this experiment in the database
:param plot: description and plotting instructions
:param data: the data for plotting
"""
self._data_writer.save_plot(self._exp_id, plot, data)

def add_figure(self, figure: go.Figure) -> None:
"""
saves a new figure from this experiment in the database
Expand Down
27 changes: 6 additions & 21 deletions entropylab/pipeline/api/memory_reader_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
from datetime import datetime
from time import time_ns
from typing import List, Optional, Iterable, Any, Dict, Tuple
from typing import List, Optional, Iterable, Dict, Tuple

import matplotlib.figure
from pandas import DataFrame
Expand All @@ -14,11 +14,10 @@
MetadataRecord,
ExperimentRecord,
ScriptViewer,
PlotRecord,
FigureRecord,
MatplotlibFigureRecord,
)
from entropylab.pipeline.api.data_writer import DataWriter, PlotSpec, NodeData
from entropylab.pipeline.api.data_writer import DataWriter, NodeData
from entropylab.pipeline.api.data_writer import (
ExperimentInitialData,
ExperimentEndData,
Expand Down Expand Up @@ -46,8 +45,8 @@ def __init__(self):
self._results: List[Tuple[RawResultData, datetime]] = []
self._metadata: List[Tuple[Metadata, datetime]] = []
self._debug: Optional[Debug] = None
self._plot: Dict[PlotSpec, Any] = {}
self._figure: Dict[int, List[FigureRecord]] = {}
self._matplotlib_figure: Dict[int, List[MatplotlibFigureRecord]] = {}
self._nodes: List[NodeData] = []

def save_experiment_initial_data(self, initial_data: ExperimentInitialData) -> int:
Expand All @@ -66,9 +65,6 @@ def save_metadata(self, experiment_id: int, metadata: Metadata):
def save_debug(self, experiment_id: int, debug: Debug):
self._debug = debug

def save_plot(self, experiment_id: int, plot: PlotSpec, data: Any):
self._plot[plot] = data

def save_figure(self, experiment_id: int, figure: go.Figure) -> None:
figure_record = FigureRecord(
experiment_id=experiment_id,
Expand Down Expand Up @@ -98,7 +94,9 @@ def save_matplotlib_figure(
def save_node(self, experiment_id: int, node_data: NodeData):
self._nodes.append(node_data)

def get_experiments_range(self, starting_from_index: int, count: int) -> DataFrame:
def get_experiments_range(
self, starting_from_index: int, count: int, success: bool = None
) -> DataFrame:
raise NotImplementedError()

def get_experiments(
Expand Down Expand Up @@ -182,19 +180,6 @@ def get_debug_record(self, experiment_id: int) -> Optional[DebugRecord]:
else:
return None

def get_plots(self, experiment_id: int) -> List[PlotRecord]:
return [
PlotRecord(
experiment_id,
id(plot),
self._plot[plot],
plot.generator(),
plot.label,
plot.story,
)
for plot in self._plot
]

def get_figures(self, experiment_id: int) -> List[FigureRecord]:
return self._figure[experiment_id]

Expand Down
31 changes: 1 addition & 30 deletions entropylab/pipeline/results_backend/sqlalchemy/db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import base64
import io
from datetime import datetime
from typing import List, TypeVar, Optional, ContextManager, Iterable, Union, Any
from typing import List, TypeVar, Optional, ContextManager, Iterable, Union
from typing import Set
from warnings import warn

import jsonpickle
import matplotlib
Expand Down Expand Up @@ -33,7 +32,6 @@
ResultRecord,
MetadataRecord,
DebugRecord,
PlotRecord,
FigureRecord,
MatplotlibFigureRecord,
)
Expand All @@ -44,14 +42,12 @@
RawResultData,
Metadata,
Debug,
PlotSpec,
NodeData,
)
from entropylab.pipeline.api.errors import EntropyError
from entropylab.pipeline.results_backend.sqlalchemy.db_initializer import _DbInitializer
from entropylab.pipeline.results_backend.sqlalchemy.model import (
ExperimentTable,
PlotTable,
ResultTable,
DebugTable,
MetadataTable,
Expand Down Expand Up @@ -146,15 +142,6 @@ def save_debug(self, experiment_id: int, debug: Debug):
transaction = DebugTable.from_model(experiment_id, debug)
return self._execute_transaction(transaction)

def save_plot(self, experiment_id: int, plot: PlotSpec, data: Any):
warn(
"This method will soon be deprecated. Please use save_figure() instead",
PendingDeprecationWarning,
stacklevel=2,
)
transaction = PlotTable.from_model(experiment_id, plot, data)
return self._execute_transaction(transaction)

def save_figure(self, experiment_id: int, figure: go.Figure) -> None:
transaction = FigureTable.from_model(experiment_id, figure)
return self._execute_transaction(transaction)
Expand Down Expand Up @@ -284,22 +271,6 @@ def get_all_results_with_label(self, exp_id, name) -> DataFrame:
)
return self._query_pandas(query)

def get_plots(self, experiment_id: int) -> List[PlotRecord]:
warn(
"This method will soon be deprecated. Please use get_figures() instead",
PendingDeprecationWarning,
stacklevel=2,
)
with self._session_maker() as sess:
query = (
sess.query(PlotTable)
.filter(PlotTable.experiment_id == int(experiment_id))
.all()
)
if query:
return [plot.to_record() for plot in query]
return []

def get_figures(self, experiment_id: int) -> List[FigureRecord]:
with self._session_maker() as sess:
query = (
Expand Down
Loading

0 comments on commit 67a7385

Please sign in to comment.