Skip to content

Commit 430928b

Browse files
mpolson64facebook-github-bot
authored andcommitted
Change blob type to str (facebook#2624)
Summary: Pull Request resolved: facebook#2624 Change the blob on AnalysisCard to always be a string rather than Any. This does not change anything for the base Anaysis or MarkdownAnalysis, but changes the dataflow on PlotlyAnalysis to use pio.to_json and pio.from_json during compute (both are very cheap). Doing things this way allows us to decode any AnaysisCard is the same regardless of its subclass and the `card == decoded(encoded))` relatioship holds. Reviewed By: Cesar-Cardoso Differential Revision: D60538382 fbshipit-source-id: 08d4bdf7cb5e4a6c5784fc4d5ea61d70488a8bd5
1 parent 62350f1 commit 430928b

File tree

4 files changed

+13
-31
lines changed

4 files changed

+13
-31
lines changed

ax/analysis/analysis.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# pyre-strict
77

88
from enum import Enum
9-
from typing import Any, Optional, Protocol
9+
from typing import Optional, Protocol
1010

1111
import pandas as pd
1212
from ax.core.experiment import Experiment
@@ -33,9 +33,11 @@ class AnalysisCard:
3333

3434
df: pd.DataFrame # Raw data produced by the Analysis
3535

36-
# pyre-ignore[4] We explicitly want to allow any type here, blob is narrowed in
37-
# AnalysisCard's subclasses
38-
blob: Any # Data processed and ready for end-user consumption
36+
# Blob is the data processed for end-user consumption, encoded as a string,
37+
# typically JSON. Subclasses of Analysis can define their own methods for consuming
38+
# the blob and presenting it to the user (ex. PlotlyAnalysisCard.get_figure()
39+
# decodes the blob into a go.Figure object).
40+
blob: str
3941

4042
# How to interpret the blob (ex. "dataframe", "plotly", "markdown")
4143
blob_annotation = "dataframe"
@@ -47,9 +49,7 @@ def __init__(
4749
subtitle: str,
4850
level: AnalysisCardLevel,
4951
df: pd.DataFrame,
50-
# pyre-ignore[2] We explicitly want to allow any type here, blob is narrowed in
51-
# AnalysisCard's subclasses
52-
blob: Any,
52+
blob: str,
5353
) -> None:
5454
self.name = name
5555
self.title = title

ax/analysis/markdown/markdown_analysis.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,12 @@
77

88
from typing import Optional
99

10-
import pandas as pd
11-
from ax.analysis.analysis import Analysis, AnalysisCard, AnalysisCardLevel
10+
from ax.analysis.analysis import Analysis, AnalysisCard
1211
from ax.core.experiment import Experiment
1312
from ax.modelbridge.generation_strategy import GenerationStrategy
1413

1514

1615
class MarkdownAnalysisCard(AnalysisCard):
17-
name: str
18-
19-
title: str
20-
subtitle: str
21-
level: AnalysisCardLevel
22-
23-
df: pd.DataFrame
24-
blob: str
2516
blob_annotation = "markdown"
2617

2718
def get_markdown(self) -> str:

ax/analysis/plotly/parallel_coordinates/parallel_coordinates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ax.core.objective import MultiObjective, ScalarizedObjective
1717
from ax.exceptions.core import UnsupportedError, UserInputError
1818
from ax.modelbridge.generation_strategy import GenerationStrategy
19-
from plotly import graph_objects as go
19+
from plotly import graph_objects as go, io as pio
2020

2121

2222
class ParallelCoordinatesPlot(PlotlyAnalysis):
@@ -61,7 +61,7 @@ def compute(
6161
subtitle="View arm parameterizations with their respective metric values",
6262
level=AnalysisCardLevel.HIGH,
6363
df=df,
64-
blob=fig,
64+
blob=pio.to_json(fig),
6565
)
6666

6767

ax/analysis/plotly/plotly_analysis.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,17 @@
77

88
from typing import Optional
99

10-
import pandas as pd
11-
from ax.analysis.analysis import Analysis, AnalysisCard, AnalysisCardLevel
10+
from ax.analysis.analysis import Analysis, AnalysisCard
1211
from ax.core.experiment import Experiment
1312
from ax.modelbridge.generation_strategy import GenerationStrategy
14-
from plotly import graph_objects as go
13+
from plotly import graph_objects as go, io as pio
1514

1615

1716
class PlotlyAnalysisCard(AnalysisCard):
18-
name: str
19-
20-
title: str
21-
subtitle: str
22-
level: AnalysisCardLevel
23-
24-
df: pd.DataFrame
25-
blob: go.Figure
2617
blob_annotation = "plotly"
2718

2819
def get_figure(self) -> go.Figure:
29-
return self.blob
20+
return pio.from_json(self.blob)
3021

3122

3223
class PlotlyAnalysis(Analysis):

0 commit comments

Comments
 (0)