jeonghin commited on
Commit
24bcceb
1 Parent(s): 403c866

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -102
app.py CHANGED
@@ -1,110 +1,66 @@
1
- import dash
2
- from dash import dcc, html
3
- from dash.dependencies import Input, Output
4
- import plotly.express as px
5
  import pandas as pd
6
-
7
-
8
- layers = ["conv2d5", "conv2d6", "conv2d7", "conv2d8", "conv2d9", "linear1", "linear2"]
9
- color_dict = {
10
- "ClassifierA_Cr203_C6": "#B3262A",
11
- "ClassifierA_test_stim": "#2f559a",
12
- "ClassifierA_test_unstim": "#5AADC5",
13
- }
14
-
15
- # Initialize Dash app
16
- app = dash.Dash(__name__)
17
-
18
- server = app.server
19
-
20
- # App layout
21
- app.layout = html.Div(
22
- [
23
- html.Div(
24
- id="plot-container",
25
- children=[
26
- html.P(
27
- children=[
28
- "The original report can be accessed through this ",
29
- html.A(
30
- "BioRxiv Link",
31
- href="https://www.biorxiv.org/content/10.1101/2023.06.01.542416v1.full.pdf",
32
- target="_blank",
33
- ),
34
- " and the data through this ",
35
- html.A(
36
- "GitHub repository",
37
- href="https://github.com/MannLabs/SPARCS_pub_figures",
38
- target="_blank",
39
- ),
40
- ],
41
- id="initial-announcement",
42
- ),
43
- html.P(
44
- "Please select a layer to view from the dropdown.",
45
- id="initial-message",
46
- ),
47
- ],
48
- ),
49
- dcc.Dropdown(
50
- id="layer-dropdown",
51
- options=[{"label": layer, "value": layer} for layer in layers],
52
- value=layers[0],
53
- ),
54
- dcc.Graph(id="umap-plot"),
55
- ]
56
- )
57
-
58
-
59
- # Callback to update the plot based on dropdown selection
60
- @app.callback(Output("umap-plot", "figure"), [Input("layer-dropdown", "value")])
61
- def update_plot(selected_layer):
62
-
63
- input_path = (
64
- f"data/classifier_1_Test_Data/UMAP_data/Raw_data_UMAP_{selected_layer}.csv"
65
  )
66
- df_train_umap = pd.read_csv(input_path)
67
 
68
- fig = px.scatter(
69
- df_train_umap.sample(frac=1, random_state=19),
70
- x="UMAP_1",
71
- y="UMAP_2",
72
- color="class_label",
73
- color_discrete_map=color_dict,
74
- title=f"UMAP Test Data Labels - {selected_layer}",
75
- )
76
- fig.update_traces(
77
- marker=dict(size=4, opacity=1, line=dict(width=0)),
78
- selector=dict(mode="markers"),
79
- )
80
 
81
- # Update layout to have equal aspect ratio
82
- fig.update_layout(
83
- xaxis=dict(
84
- scaleanchor="y",
85
- scaleratio=1,
86
- dtick=2,
87
- ),
88
- yaxis=dict(
89
- scaleanchor="x",
90
- scaleratio=1,
91
- dtick=2,
92
- ),
93
- legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
94
- )
95
 
96
- # Set a constant range for both axes
97
- umap_min = min(df_train_umap["UMAP_1"].min(), df_train_umap["UMAP_2"].min())
98
- umap_max = max(df_train_umap["UMAP_1"].max(), df_train_umap["UMAP_2"].max())
99
 
100
- x_range = [umap_min - 2, umap_max + 2]
101
- # y_range = [umap_min - 2, umap_max + 2]
102
-
103
- fig.update_xaxes(range=x_range, constrain="domain")
104
- # fig.update_yaxes(range=y_range, scaleanchor="x", scaleratio=1)
105
-
106
- return fig
107
 
 
 
 
108
 
109
- if __name__ == "__main__":
110
- app.run_server(debug=True)
 
1
+ import altair as alt
 
 
 
2
  import pandas as pd
3
+ import panel as pn
4
+
5
+ pn.extension('vega')
6
+
7
+ # Selection widgets
8
+ subgroup_select = pn.widgets.Select(name='Layers', options=["conv2d5", "conv2d6", "conv2d7", "conv2d8", "conv2d9", "linear1", "linear2"])
9
+
10
+ # Bind the widgets to the create_plot function
11
+ @pn.depends(subgroup_select.param.value)
12
+ def create_plot(layers):
13
+ df_train_umap = pd.read_csv(f"https://raw.githubusercontent.com/jeonghin/si649_project2_sparcs/main/data/classifier_1_Test_Data/UMAP_data/Raw_data_UMAP_{layers}.csv").sample(n=5000, random_state=19)
14
+ color_dict = {
15
+ "ClassifierA_Cr203_C6": "#B3262A",
16
+ "ClassifierA_test_stim": "#2f559a",
17
+ "ClassifierA_test_unstim": "#5AADC5",
18
+ }
19
+
20
+ # Define a selection for zooming and panning (interval)
21
+ zoom = alt.selection_interval(bind='scales')
22
+
23
+ # Define a selection for the legend
24
+ legend_selection = alt.selection_multi(fields=['class_label'], bind='legend')
25
+
26
+
27
+ # Create the Altair chart object
28
+ chart = alt.Chart(df_train_umap).mark_circle(size=4, opacity=1).encode(
29
+ x=alt.X("UMAP_1:Q", scale=alt.Scale(zero=False)),
30
+ y=alt.Y("UMAP_2:Q", scale=alt.Scale(zero=False)),
31
+ color=alt.Color("class_label:N", scale=alt.Scale(domain=list(color_dict.keys()), range=list(color_dict.values()))),
32
+ tooltip=['UMAP_1', 'UMAP_2', 'class_label'],
33
+ opacity=alt.condition(legend_selection, alt.value(1), alt.value(0)) # Use the selection here to control opacity
34
+ ).properties(
35
+ title=f"UMAP Test Data Labels - {layers}",
36
+ ).add_selection(
37
+ legend_selection, # Add the selection to the chart
38
+
39
+ zoom
40
+ ).configure_axis(
41
+ grid=False
42
+ ).configure_view(
43
+ strokeWidth=0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  )
 
45
 
46
+ return chart
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ # Combine everything in a Panel Column to create an app
49
+ app = pn.Column("# UMAP Data Interactive Visualization", subgroup_select, pn.panel(create_plot, reactive=True))
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # Set the app to be servable
52
+ app.servable()
 
53
 
54
+ # Main layout
55
+ main_layout = pn.Column(
56
+ "# Data Interactive Visualization (SPARCS)",
57
+ pn.panel(create_plot, reactive=True),
58
+ subgroup_select,
59
+ )
 
60
 
61
+ # Create a basic template
62
+ template = pn.template.BootstrapTemplate(title='SI649 Project 2')
63
+ template.main.append(main_layout)
64
 
65
+ # Serve the application
66
+ template.servable()