pantdipendra commited on
Commit
f92effe
·
verified ·
1 Parent(s): 726e8be
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -43,7 +43,7 @@ class ModelPredictor:
43
  "YODPPROB": ["No other problems for 2+ weeks", "Had other problems for 2+ weeks"],
44
  "YOWRPROB": ["No 'worst time ever' feeling", "Had 'worst time ever' feeling"],
45
  "YODPR2WK": ["No depressed feelings for 2+ wks", "Had depressed feelings for 2+ wks"],
46
- "YOWRDEPR": ["Did NOT feel sad/depressed daily", "Felt sad/depressed mostly everyday"],
47
  "YODPDISC": ["Overall mood not sad/depressed", "Overall mood was sad/depressed"],
48
  "YOLOSEV": ["Did NOT lose interest in things", "Lost interest in enjoyable things"],
49
  "YOWRDCSN": ["Was able to make decisions", "Was unable to make decisions"],
@@ -425,37 +425,57 @@ def combined_plot(feature_list, label_col):
425
  If user picks 1 feature => distribution plot.
426
  If user picks 2 features => co-occurrence plot.
427
  Otherwise => show error or empty plot.
 
 
 
428
  """
429
  if not label_col:
430
  return px.bar(title="Please select a label column.")
431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  if len(feature_list) == 1:
433
  f_ = feature_list[0]
434
- if f_ not in df.columns or label_col not in df.columns:
435
  return px.bar(title="Selected columns not found in the dataset.")
436
- grouped = df.groupby([f_, label_col]).size().reset_index(name="count")
437
  fig = px.bar(
438
- grouped,
439
- x=f_,
440
- y="count",
441
  color=label_col,
442
- title=f"Distribution of {f_} vs {label_col}"
443
  )
444
  fig.update_layout(width=1200, height=600)
445
  return fig
446
 
447
  elif len(feature_list) == 2:
448
  f1, f2 = feature_list
449
- if (f1 not in df.columns) or (f2 not in df.columns) or (label_col not in df.columns):
450
  return px.bar(title="Selected columns not found in the dataset.")
451
- grouped = df.groupby([f1, f2, label_col]).size().reset_index(name="count")
452
  fig = px.bar(
453
- grouped,
454
- x=f1,
455
- y="count",
456
  color=label_col,
457
- facet_col=f2,
458
- title=f"Co-occurrence: {f1}, {f2} vs {label_col}"
459
  )
460
  fig.update_layout(width=1200, height=600)
461
  return fig
@@ -579,11 +599,12 @@ with gr.Blocks(css=".gradio-container {max-width: 1200px;}") as demo:
579
  # ======== TAB 2: Unified Distribution/Co-occurrence ========
580
  with gr.Tab("Distribution/Co-occurrence"):
581
  gr.Markdown("### Select 1 or 2 features + 1 label to see a bar chart.")
582
-
583
- # Instead of using df.columns, we now only use the keys from input_mapping
584
  list_of_features = sorted(input_mapping.keys())
 
585
  list_of_labels = sorted(predictor.prediction_map.keys())
586
-
587
  selected_features = gr.CheckboxGroup(
588
  choices=list_of_features,
589
  label="Select 1 or 2 features"
@@ -592,10 +613,10 @@ with gr.Blocks(css=".gradio-container {max-width: 1200px;}") as demo:
592
  choices=list_of_labels,
593
  label="Label Column (e.g., YOWRCONC, YOSEEDOC, etc.)"
594
  )
595
-
596
  generate_combined_btn = gr.Button("Generate Plot")
597
  combined_output = gr.Plot()
598
-
599
  generate_combined_btn.click(
600
  fn=combined_plot,
601
  inputs=[selected_features, label_dd],
 
43
  "YODPPROB": ["No other problems for 2+ weeks", "Had other problems for 2+ weeks"],
44
  "YOWRPROB": ["No 'worst time ever' feeling", "Had 'worst time ever' feeling"],
45
  "YODPR2WK": ["No depressed feelings for 2+ wks", "Had depressed feelings for 2+ wks"],
46
+ "YOWRDEPR": ["Did NOT feel sad/depressed daily", "Felt sad/depressed mostly everyday"],
47
  "YODPDISC": ["Overall mood not sad/depressed", "Overall mood was sad/depressed"],
48
  "YOLOSEV": ["Did NOT lose interest in things", "Lost interest in enjoyable things"],
49
  "YOWRDCSN": ["Was able to make decisions", "Was unable to make decisions"],
 
425
  If user picks 1 feature => distribution plot.
426
  If user picks 2 features => co-occurrence plot.
427
  Otherwise => show error or empty plot.
428
+
429
+ This function also maps numeric codes to text using 'input_mapping'
430
+ and 'predictor.prediction_map' so that the plots display more readable labels.
431
  """
432
  if not label_col:
433
  return px.bar(title="Please select a label column.")
434
 
435
+ # Make a copy of your dataset
436
+ df_copy = df.copy()
437
+
438
+ # A) Convert numeric codes -> text for each feature in `input_mapping`
439
+ for col, text_to_num_dict in input_mapping.items():
440
+ if col in df_copy.columns:
441
+ # Reverse mapping: "Yes"->1 becomes 1->"Yes"
442
+ num_to_text = {v: k for k, v in text_to_num_dict.items()}
443
+ df_copy[col] = df_copy[col].map(num_to_text).fillna(df_copy[col])
444
+
445
+ # B) Convert label 0/1 to text in df_copy if label_col is in predictor.prediction_map
446
+ if label_col in predictor.prediction_map and label_col in df_copy.columns:
447
+ zero_text, one_text = predictor.prediction_map[label_col]
448
+ label_map = {0: zero_text, 1: one_text}
449
+ df_copy[label_col] = df_copy[label_col].map(label_map).fillna(df_copy[label_col])
450
+
451
+ # Now proceed with the plotting
452
  if len(feature_list) == 1:
453
  f_ = feature_list[0]
454
+ if f_ not in df_copy.columns or label_col not in df_copy.columns:
455
  return px.bar(title="Selected columns not found in the dataset.")
456
+ grouped = df_copy.groupby([f_, label_col]).size().reset_index(name="count")
457
  fig = px.bar(
458
+ grouped,
459
+ x=f_,
460
+ y="count",
461
  color=label_col,
462
+ title=f"Distribution of {f_} vs {label_col} (Text Mapped)"
463
  )
464
  fig.update_layout(width=1200, height=600)
465
  return fig
466
 
467
  elif len(feature_list) == 2:
468
  f1, f2 = feature_list
469
+ if (f1 not in df_copy.columns) or (f2 not in df_copy.columns) or (label_col not in df_copy.columns):
470
  return px.bar(title="Selected columns not found in the dataset.")
471
+ grouped = df_copy.groupby([f1, f2, label_col]).size().reset_index(name="count")
472
  fig = px.bar(
473
+ grouped,
474
+ x=f1,
475
+ y="count",
476
  color=label_col,
477
+ facet_col=f2,
478
+ title=f"Co-occurrence: {f1}, {f2} vs {label_col} (Text Mapped)"
479
  )
480
  fig.update_layout(width=1200, height=600)
481
  return fig
 
599
  # ======== TAB 2: Unified Distribution/Co-occurrence ========
600
  with gr.Tab("Distribution/Co-occurrence"):
601
  gr.Markdown("### Select 1 or 2 features + 1 label to see a bar chart.")
602
+
603
+ # Show only your 25 input features
604
  list_of_features = sorted(input_mapping.keys())
605
+ # Show all label columns from the predictor map
606
  list_of_labels = sorted(predictor.prediction_map.keys())
607
+
608
  selected_features = gr.CheckboxGroup(
609
  choices=list_of_features,
610
  label="Select 1 or 2 features"
 
613
  choices=list_of_labels,
614
  label="Label Column (e.g., YOWRCONC, YOSEEDOC, etc.)"
615
  )
616
+
617
  generate_combined_btn = gr.Button("Generate Plot")
618
  combined_output = gr.Plot()
619
+
620
  generate_combined_btn.click(
621
  fn=combined_plot,
622
  inputs=[selected_features, label_dd],