Hack90 commited on
Commit
6a306c0
·
verified ·
1 Parent(s): c15e5d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -149
app.py CHANGED
@@ -1,151 +1,110 @@
1
- from pathlib import Path
2
- from typing import List, Dict, Tuple
3
- import matplotlib.colors as mpl_colors
4
-
5
  import pandas as pd
6
- import seaborn as sns
7
- import shinyswatch
8
-
9
- from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui
10
-
11
- sns.set_theme()
12
-
13
- www_dir = Path(__file__).parent.resolve() / "www"
14
-
15
- df = pd.read_csv(Path(__file__).parent / "penguins.csv", na_values="NA")
16
- numeric_cols: List[str] = df.select_dtypes(include=["float64"]).columns.tolist()
17
- species: List[str] = df["Species"].unique().tolist()
18
- species.sort()
19
-
20
- app_ui = ui.page_fillable(
21
- shinyswatch.theme.minty(),
22
- ui.layout_sidebar(
23
- ui.sidebar(
24
- # Artwork by @allison_horst
25
- ui.input_selectize(
26
- "xvar",
27
- "X variable",
28
- numeric_cols,
29
- selected="Bill Length (mm)",
30
- ),
31
- ui.input_selectize(
32
- "yvar",
33
- "Y variable",
34
- numeric_cols,
35
- selected="Bill Depth (mm)",
36
- ),
37
- ui.input_checkbox_group(
38
- "species", "Filter by species", species, selected=species
39
- ),
40
- ui.hr(),
41
- ui.input_switch("by_species", "Show species", value=True),
42
- ui.input_switch("show_margins", "Show marginal plots", value=True),
43
- ),
44
- ui.output_ui("value_boxes"),
45
- ui.output_plot("scatter", fill=True),
46
- ui.help_text(
47
- "Artwork by ",
48
- ui.a("@allison_horst", href="https://twitter.com/allison_horst"),
49
- class_="text-end",
50
- ),
51
- ),
52
- )
53
-
54
-
55
- def server(input: Inputs, output: Outputs, session: Session):
56
- @reactive.Calc
57
- def filtered_df() -> pd.DataFrame:
58
- """Returns a Pandas data frame that includes only the desired rows"""
59
-
60
- # This calculation "req"uires that at least one species is selected
61
- req(len(input.species()) > 0)
62
-
63
- # Filter the rows so we only include the desired species
64
- return df[df["Species"].isin(input.species())]
65
-
66
- @output
67
- @render.plot
68
- def scatter():
69
- """Generates a plot for Shiny to display to the user"""
70
-
71
- # The plotting function to use depends on whether margins are desired
72
- plotfunc = sns.jointplot if input.show_margins() else sns.scatterplot
73
-
74
- plotfunc(
75
- data=filtered_df(),
76
- x=input.xvar(),
77
- y=input.yvar(),
78
- palette=palette,
79
- hue="Species" if input.by_species() else None,
80
- hue_order=species,
81
- legend=False,
82
- )
83
-
84
- @output
85
- @render.ui
86
- def value_boxes():
87
- df = filtered_df()
88
-
89
- def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
90
- return ui.value_box(
91
- title,
92
- count,
93
- {"class_": "pt-1 pb-0"},
94
- showcase=ui.fill.as_fill_item(
95
- ui.tags.img(
96
- {"style": "object-fit:contain;"},
97
- src=showcase_img,
98
- )
99
- ),
100
- theme_color=None,
101
- style=f"background-color: {bgcol};",
102
- )
103
-
104
- if not input.by_species():
105
- return penguin_value_box(
106
- "Penguins",
107
- len(df.index),
108
- bg_palette["default"],
109
- # Artwork by @allison_horst
110
- showcase_img="penguins.png",
111
- )
112
-
113
- value_boxes = [
114
- penguin_value_box(
115
- name,
116
- len(df[df["Species"] == name]),
117
- bg_palette[name],
118
- # Artwork by @allison_horst
119
- showcase_img=f"{name}.png",
120
- )
121
- for name in species
122
- # Only include boxes for _selected_ species
123
- if name in input.species()
124
- ]
125
-
126
- return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
127
-
128
-
129
- # "darkorange", "purple", "cyan4"
130
- colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
131
- colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
132
-
133
- palette: Dict[str, Tuple[float, float, float]] = {
134
- "Adelie": colors[0],
135
- "Chinstrap": colors[1],
136
- "Gentoo": colors[2],
137
- "default": sns.color_palette()[0], # type: ignore
138
- }
139
-
140
- bg_palette = {}
141
- # Use `sns.set_style("whitegrid")` to help find approx alpha value
142
- for name, col in palette.items():
143
- # Adjusted n_colors until `axe` accessibility did not complain about color contrast
144
- bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
145
-
146
-
147
- app = App(
148
- app_ui,
149
- server,
150
- static_assets=str(www_dir),
151
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from scipy.interpolate import interp1d
5
+ from shiny import render
6
+ from shiny.express import input, output, ui
7
+ from utils import (
8
+ generate_2d_sequence,
9
+ plot_seq_full_label
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  )
11
+ import os
12
+ import matplotlib as mpl
13
+ import seaborn as sns
14
+ mpl.rcParams.update(mpl.rcParamsDefault)
15
+
16
+ df_gene_varient = pd.read_parquet("gene_varient.parquet")
17
+ df_histone = pd.read_parquet("histone.parquet")
18
+ df_gene_len = len(df_gene_varient)
19
+ df_histone_len = len(df_histone)
20
+ df_enhancer_annotation = pd.read_parquet('enhancer_annotation.parquet')
21
+ df_enhancer_annotation_len = len(df_enhancer_annotation)
22
+ ui.page_opts(fillable=True)
23
+
24
+ with ui.navset_card_tab(id="tab"):
25
+ with ui.nav_panel("Gene Varient"):
26
+ ui.panel_title("Is there a pattern to gene varient location?")
27
+ with ui.layout_columns():
28
+ with ui.card():
29
+ ui.input_slider("sample", "sample", 0, df_gene_len, 40)
30
+
31
+ def plot_loss_rates(df, sample, enhancer=False):
32
+ y_values = generate_2d_sequence(df['seq'].iloc[sample])[0]
33
+ x_values = generate_2d_sequence(df['seq'].iloc[sample])[1]
34
+
35
+ integers = df['labels'].iloc[sample]
36
+ if enhancer:
37
+ K= 128
38
+ res = []
39
+ for i in integers:
40
+ res.extend([i]*K)
41
+ integers = res
42
+ # Create a DataFrame with the x values, y values, and integers
43
+ data = {'x': x_values, 'y': y_values, 'color': integers}
44
+
45
+ # fig, ax = plt.subplots()
46
+
47
+
48
+ # Create a figure and axis
49
+ fig, ax = plt.subplots()
50
+
51
+ # Create the scatter plot
52
+ scatter = ax.scatter(data['x'], data['y'], c=data['color'], cmap='tab20', s=0.5)
53
+
54
+ # Add a colorbar
55
+ cbar = fig.colorbar(scatter, ax=ax)
56
+ cbar.set_label('Label')
57
+
58
+ # Set labels and title
59
+ # ax.set_xlabel('X')
60
+ # ax.set_ylabel('Y')
61
+ # ax.set_title(f"Loss ra")
62
+ # ax.set_xlabel("Training steps")
63
+ # ax.set_ylabel("Loss rate")
64
+ return fig
65
+
66
+ @render.plot()
67
+ def plot_context_size_scaling():
68
+ fig = plot_loss_rates(df_gene_varient,input.sample() )
69
+ if fig:
70
+ return fig
71
+ with ui.nav_panel("Histone Modification"):
72
+ ui.panel_title("Is there a pattern to histone modification?")
73
+ with ui.layout_columns():
74
+ with ui.card():
75
+ ui.input_slider("sample_histone", "sample", 0, df_histone_len, 40)
76
+
77
+
78
+ def plot_histone(df,sample):
79
+ y_values = generate_2d_sequence(df['seq'].iloc[sample])[0]
80
+ x_values = generate_2d_sequence(df['seq'].iloc[sample])[1]
81
+
82
+ integers = str((np.argwhere(df['labels'][sample] == np.amax(df['labels'][sample]))).flatten().tolist())
83
+ # Create a DataFrame with the x values, y values, and integers
84
+ data = {'x': x_values, 'y': y_values, 'color': integers}
85
+
86
+ fig, ax = plt.subplots()
87
+
88
+ sns.scatterplot(x='x', y='y', hue='color', data=data, palette='viridis', ax=ax)
89
+ ax.legend()
90
+ # ax.set_title(f"Loss ra")
91
+ # ax.set_xlabel("Training steps")
92
+ # ax.set_ylabel("Loss rate")
93
+ return fig
94
+ @render.plot()
95
+ def plot_histones_two():
96
+ fig = plot_histone(df_histone,input.sample_histone() )
97
+ if fig:
98
+ return fig
99
+ with ui.nav_panel("Enhancer Annontations"):
100
+ ui.panel_title("Is there a pattern to enhancer annotations?")
101
+ with ui.layout_columns():
102
+ with ui.card():
103
+ ui.input_slider("sample_enhancer", "sample", 0, df_enhancer_annotation_len, 40)
104
+ @render.plot()
105
+ def plot_enhancer():
106
+ fig = plot_loss_rates(df_enhancer_annotation,input.sample_enhancer() , True)
107
+ if fig:
108
+ return fig
109
+
110
+