Ronak Ramachandran commited on
Commit
8a7b5b1
1 Parent(s): 0691d7e
Files changed (1) hide show
  1. app.py +38 -32
app.py CHANGED
@@ -280,45 +280,51 @@ def plot_scatter_hist(x_gene, y_gene, mode='raw'):
280
  # ax.fill_betweenx([plotted_data[y_gene].min(), plotted_data[y_gene].max()],
281
  # *ax.get_xlim(), color='C0', alpha=0.01, lw=0)
282
 
283
-
284
- def create_correct_gene_plot(genes, mode):
285
- if len(genes) == 0:
286
- raise gr.Error("Please select at least one gene to plot.")
287
- elif len(genes) == 1:
288
- plot_gene(*genes)
289
- elif len(genes) == 2:
290
- mode = 'norm' if mode else None
291
- plot_scatter_hist(*genes, mode)
292
- else:
293
- raise gr.Error("Cannot plot more than two genes at a time.")
294
-
295
  fig = plt.gcf()
296
  fig.canvas.draw()
297
-
298
  image = PIL.Image.frombytes(
299
  'RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb())
300
-
301
  plt.close('all')
302
-
303
  return image
304
 
305
- demo = gr.Interface(
306
- create_correct_gene_plot,
307
- [
308
- gr.Dropdown(
309
- gene_table.columns.to_list(), value=["APP", "PSENEN"], multiselect=True, label="Genes", info="Select one or two genes to plot."
310
- ),
311
- gr.Checkbox(label="Normalize",
312
- info="Recenter and normalize the Gaussian for two genes."),
313
- ],
314
- "image",
315
- # examples=[
316
- # [2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
317
- # [4, "dog", ["Japan"], "zoo", ["ate", "swam"], False],
318
- # [10, "bird", ["USA", "Pakistan"], "road", ["ran"], False],
319
- # [8, "cat", ["Pakistan"], "zoo", ["ate"], True],
320
- # ]
321
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
  if __name__ == "__main__":
324
  demo.launch()
 
280
  # ax.fill_betweenx([plotted_data[y_gene].min(), plotted_data[y_gene].max()],
281
  # *ax.get_xlim(), color='C0', alpha=0.01, lw=0)
282
 
283
+ def plt_to_img():
 
 
 
 
 
 
 
 
 
 
 
284
  fig = plt.gcf()
285
  fig.canvas.draw()
 
286
  image = PIL.Image.frombytes(
287
  'RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb())
 
288
  plt.close('all')
 
289
  return image
290
 
291
+ def random_gene():
292
+ return np.random.default_rng().choice(gene_table.columns.values)
293
+
294
+ def plot_one_gene(gene):
295
+ if gene is None or gene == '':
296
+ gene = random_gene()
297
+ plot_gene(gene.upper())
298
+ return plt_to_img(), f"{gene}"
299
+
300
+ def plot_two_genes(x_gene, y_gene, is_normed):
301
+ if x_gene is None or x_gene == '':
302
+ x_gene = random_gene()
303
+ if y_gene is None or y_gene == '':
304
+ y_gene = random_gene()
305
+ mode = 'norm' if is_normed else 'raw'
306
+ plot_scatter_hist(x_gene.upper(), y_gene.upper(), mode)
307
+ return plt_to_img(), f"{x_gene} vs. {y_gene}"
308
+
309
+ def picker(only_one_gene, x_gene, y_gene, is_normed):
310
+ if only_one_gene:
311
+ return plot_one_gene(x_gene)
312
+ return plot_two_genes(x_gene, y_gene, is_normed)
313
+
314
+ with gr.Blocks() as demo:
315
+ with gr.Row():
316
+ with gr.Column():
317
+ only_one_gene = gr.Checkbox(label="Only plot Gene 1", info="By default, two genes are plotted.")
318
+ x_gene = gr.Textbox(label='Gene 1', value='APP')
319
+ y_gene = gr.Textbox(label='Gene 2', value='PSENEN')
320
+ is_normed = gr.Checkbox(label="Normalize", info="Recenter and normalize the Gaussian for two genes.")
321
+ plot_button = gr.Button("Plot")
322
+ with gr.Column():
323
+ image_output = gr.Image()
324
+ text_output = gr.Textbox()
325
+
326
+ plot_button.click(picker, inputs=[only_one_gene, x_gene, y_gene, is_normed], outputs=[image_output,text_output])
327
+
328
 
329
  if __name__ == "__main__":
330
  demo.launch()