Arts-of-coding commited on
Commit
0fbe7d1
1 Parent(s): ce09969

Update dash_plotly_QC_scRNA.py

Browse files
Files changed (1) hide show
  1. dash_plotly_QC_scRNA.py +19 -16
dash_plotly_QC_scRNA.py CHANGED
@@ -361,28 +361,31 @@ def update_slider_values(min_1, max_1, min_2, max_2, min_3, max_3):
361
  )
362
 
363
  def update_graph_and_pie_chart(batch_chosen, s_chosen, g2m_chosen, condition1_chosen, condition2_chosen, condition3_chosen, range_value_1, range_value_2, range_value_3):
364
- dff = df.filter(
365
- (pl.col('batch').cast(str).is_in(batch_chosen)) &
366
- (pl.col(col_features) >= range_value_1[0]) &
367
- (pl.col(col_features) <= range_value_1[1]) &
368
- (pl.col(col_counts) >= range_value_2[0]) &
369
- (pl.col(col_counts) <= range_value_2[1]) &
370
- (pl.col(col_mt) >= range_value_3[0]) &
371
- (pl.col(col_mt) <= range_value_3[1])
372
- )
 
 
 
 
 
373
 
374
- #Drop categories that are not in the filtered data
375
- dff = dff.with_columns(dff['batch'].cast(str))
376
- dff = dff.with_columns(dff['batch'].cast(pl.Categorical))
377
 
378
  # Plot figures
379
  fig_violin = px.violin(data_frame=dff, x='batch', y=col_features, box=True, points="all",
380
  color='batch', hover_name='batch',template="seaborn")
381
 
382
- # Calculate the percentage of each category (normalized_count) for pie chart
383
- category_counts = dff.group_by("batch").agg(pl.col("batch").count().alias("count"))
384
- total_count = len(dff)
385
- category_counts = category_counts.with_columns((pl.col("count") / total_count * 100).alias("normalized_count"))
386
 
387
  # Display the result
388
  labels = category_counts["batch"].to_list()
 
361
  )
362
 
363
  def update_graph_and_pie_chart(batch_chosen, s_chosen, g2m_chosen, condition1_chosen, condition2_chosen, condition3_chosen, range_value_1, range_value_2, range_value_3):
364
+ batch_col = dff['batch']
365
+ batch_cat = batch_col.cast(pl.Categorical)
366
+ feature_cols = pl.col(cols)
367
+ count_cols = pl.col(col_counts)
368
+ mt_cols = pl.col(col_mt)
369
+
370
+ # Perform filtering on all features simultaneously
371
+ filter_expr = (batch_cat.is_in(batch_chosen)) & \
372
+ (feature_cols >= range_value_1[0]) & \
373
+ (feature_cols <= range_value_1[1]) & \
374
+ (count_cols >= range_value_2[0]) & \
375
+ (count_cols <= range_value_2[1]) & \
376
+ (mt_cols >= range_value_3[0]) & \
377
+ (mt_cols <= range_value_3[1])
378
 
379
+ dff = dff.filter(filter_expr)
 
 
380
 
381
  # Plot figures
382
  fig_violin = px.violin(data_frame=dff, x='batch', y=col_features, box=True, points="all",
383
  color='batch', hover_name='batch',template="seaborn")
384
 
385
+ # Cache commonly used subexpressions
386
+ total_count = pl.lit(len(dff))
387
+ category_counts = dff.groupby("batch").agg(pl.col("batch").count().alias("count"))
388
+ category_counts = category_counts.with_columns(((pl.col("count") / total_count * 100).round(decimals=2)).alias("normalized_count"))
389
 
390
  # Display the result
391
  labels = category_counts["batch"].to_list()