duongthienz commited on
Commit
8a9879c
Β·
verified Β·
1 Parent(s): 421d25d

map color to speaker to maintain color consistency across all plotly

Browse files
Files changed (1) hide show
  1. utils.py +23 -26
utils.py CHANGED
@@ -338,51 +338,48 @@ def build_fig_pie1(df3, catTypeColors):
338
  return fig
339
 
340
 
341
- def build_fig_pie2(df4, speakerNames, speakerColors, catColors, get_display_name_fn, currFile):
342
  """Speaker / category pie chart."""
343
  df4 = df4.copy()
344
- figColors = [
345
- speakerColors[list(speakerNames).index(n)]
346
- for n in df4["names"] if n in speakerNames
347
- ]
348
  df4["names"] = df4["names"].apply(lambda s: get_display_name_fn(s, currFile))
 
 
349
  fig = go.Figure()
350
- fig.update_layout(
351
- title_text="Percentage of Speakers per Role",
352
- colorway=_SPEAKER_PALETTE,
353
- **TRANSPARENT_BG,
354
- )
355
- fig.add_trace(go.Pie(values=df4["values"], labels=df4["names"], sort=False))
356
  return fig
357
 
358
 
359
- def _voice_color_map(df5_labels, speakerColors):
360
  """Build the label->color map for sunburst/treemap charts.
361
 
362
  Single Voice β†’ _PALETTE[0] (red shade 0, reserved)
363
- Multi Voice β†’ _PALETTE[3] (green shade 0, reserved)
364
  No Voice β†’ _PALETTE[-1] (grey, reserved)
365
- Speakers β†’ _SPEAKER_PALETTE in order (never collides with reserved)
366
  """
367
  top_labels = ["No Voice", "Single Voice", "Multi Voice"]
368
  color_map = {
369
- "Single Voice": _PALETTE[0], # red shade 0
370
- "Multi Voice": _PALETTE[9], # green shade 0
371
- "No Voice": _PALETTE[-1], # grey
372
  }
373
  speaker_labels = [l for l in df5_labels if l not in top_labels]
374
  for i, lbl in enumerate(speaker_labels):
375
- color_map[lbl] = _SPEAKER_PALETTE[i % len(_SPEAKER_PALETTE)]
 
 
376
  return color_map
377
 
378
 
379
- def build_fig_sunburst(df5, catTypeColors, speakerColors, get_display_name_fn, currFile):
380
  """Sunburst voice-category chart."""
381
  df5 = df5.copy()
382
  df5["labels"] = df5["labels"].apply(lambda s: get_display_name_fn(s, currFile))
383
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
384
 
385
- color_map = _voice_color_map(df5["labels"], speakerColors)
386
 
387
  fig = px.sunburst(
388
  df5,
@@ -405,13 +402,13 @@ def build_fig_sunburst(df5, catTypeColors, speakerColors, get_display_name_fn, c
405
  return fig
406
 
407
 
408
- def build_fig_treemap(df5, catTypeColors, speakerColors, get_display_name_fn, currFile):
409
  """Treemap voice-category chart."""
410
  df5 = df5.copy()
411
  df5["labels"] = df5["labels"].apply(lambda s: get_display_name_fn(s, currFile))
412
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
413
 
414
- color_map = _voice_color_map(df5["labels"], speakerColors)
415
 
416
  fig = px.treemap(
417
  df5,
@@ -434,7 +431,7 @@ def build_fig_treemap(df5, catTypeColors, speakerColors, get_display_name_fn, cu
434
  return fig
435
 
436
 
437
- def build_fig_timeline(speakers_dataFrame, currTotalTime, speakerColors, get_display_name_fn, currFile):
438
  """Gantt-style speaker timeline."""
439
  df = speakers_dataFrame.copy()
440
  df["Resource"] = df["Resource"].apply(lambda s: get_display_name_fn(s, currFile))
@@ -455,7 +452,7 @@ def build_fig_timeline(speakers_dataFrame, currTotalTime, speakerColors, get_dis
455
  fig = px.timeline(
456
  df, x_start="Start", x_end="Finish", y="Resource", color="Resource",
457
  title="Timeline of Audio with Speakers",
458
- color_discrete_sequence=_SPEAKER_PALETTE,
459
  )
460
  fig.update_yaxes(autorange="reversed")
461
 
@@ -492,7 +489,7 @@ def _seconds_to_hhmmss(seconds):
492
  return f"{h:02d}:{m:02d}:{s:05.2f}"
493
 
494
 
495
- def build_fig_bar(df2, speakerNames, catColors, speakerColors, get_display_name_fn, currFile):
496
  """Horizontal bar chart β€” time spoken per speaker (hh:mm:ss.ss).
497
  Only individual speakers are shown; role/category rows are excluded.
498
  """
@@ -505,7 +502,7 @@ def build_fig_bar(df2, speakerNames, catColors, speakerColors, get_display_name_
505
  df2, x="values", y="names", color="names", orientation="h",
506
  custom_data=["names", "time_label"],
507
  title="Time Spoken by each Speaker",
508
- color_discrete_sequence=_SPEAKER_PALETTE,
509
  )
510
  # Hide x-axis tick labels β€” values are crowded with many speakers.
511
  # The exact time is still visible on hover via the hovertemplate.
 
338
  return fig
339
 
340
 
341
+ def build_fig_pie2(df4, speakerNames, speaker_color_map, catColors, get_display_name_fn, currFile):
342
  """Speaker / category pie chart."""
343
  df4 = df4.copy()
 
 
 
 
344
  df4["names"] = df4["names"].apply(lambda s: get_display_name_fn(s, currFile))
345
+ colors = [speaker_color_map.get(n, _SPEAKER_PALETTE[i % len(_SPEAKER_PALETTE)])
346
+ for i, n in enumerate(df4["names"])]
347
  fig = go.Figure()
348
+ fig.update_layout(title_text="Percentage of Speakers per Role", **TRANSPARENT_BG)
349
+ fig.add_trace(go.Pie(values=df4["values"], labels=df4["names"],
350
+ marker_colors=colors, sort=False))
 
 
 
351
  return fig
352
 
353
 
354
+ def _voice_color_map(df5_labels, speaker_color_map):
355
  """Build the label->color map for sunburst/treemap charts.
356
 
357
  Single Voice β†’ _PALETTE[0] (red shade 0, reserved)
358
+ Multi Voice β†’ _PALETTE[9] (green shade 0, reserved)
359
  No Voice β†’ _PALETTE[-1] (grey, reserved)
360
+ Speakers β†’ looked up from speaker_color_map for cross-chart consistency
361
  """
362
  top_labels = ["No Voice", "Single Voice", "Multi Voice"]
363
  color_map = {
364
+ "Single Voice": _PALETTE[0],
365
+ "Multi Voice": _PALETTE[9],
366
+ "No Voice": _PALETTE[-1],
367
  }
368
  speaker_labels = [l for l in df5_labels if l not in top_labels]
369
  for i, lbl in enumerate(speaker_labels):
370
+ color_map[lbl] = speaker_color_map.get(
371
+ lbl, _SPEAKER_PALETTE[i % len(_SPEAKER_PALETTE)]
372
+ )
373
  return color_map
374
 
375
 
376
+ def build_fig_sunburst(df5, catTypeColors, speaker_color_map, get_display_name_fn, currFile):
377
  """Sunburst voice-category chart."""
378
  df5 = df5.copy()
379
  df5["labels"] = df5["labels"].apply(lambda s: get_display_name_fn(s, currFile))
380
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
381
 
382
+ color_map = _voice_color_map(df5["labels"], speaker_color_map)
383
 
384
  fig = px.sunburst(
385
  df5,
 
402
  return fig
403
 
404
 
405
+ def build_fig_treemap(df5, catTypeColors, speaker_color_map, get_display_name_fn, currFile):
406
  """Treemap voice-category chart."""
407
  df5 = df5.copy()
408
  df5["labels"] = df5["labels"].apply(lambda s: get_display_name_fn(s, currFile))
409
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
410
 
411
+ color_map = _voice_color_map(df5["labels"], speaker_color_map)
412
 
413
  fig = px.treemap(
414
  df5,
 
431
  return fig
432
 
433
 
434
+ def build_fig_timeline(speakers_dataFrame, currTotalTime, speaker_color_map, get_display_name_fn, currFile):
435
  """Gantt-style speaker timeline."""
436
  df = speakers_dataFrame.copy()
437
  df["Resource"] = df["Resource"].apply(lambda s: get_display_name_fn(s, currFile))
 
452
  fig = px.timeline(
453
  df, x_start="Start", x_end="Finish", y="Resource", color="Resource",
454
  title="Timeline of Audio with Speakers",
455
+ color_discrete_map=speaker_color_map,
456
  )
457
  fig.update_yaxes(autorange="reversed")
458
 
 
489
  return f"{h:02d}:{m:02d}:{s:05.2f}"
490
 
491
 
492
+ def build_fig_bar(df2, speakerNames, catColors, speaker_color_map, get_display_name_fn, currFile):
493
  """Horizontal bar chart β€” time spoken per speaker (hh:mm:ss.ss).
494
  Only individual speakers are shown; role/category rows are excluded.
495
  """
 
502
  df2, x="values", y="names", color="names", orientation="h",
503
  custom_data=["names", "time_label"],
504
  title="Time Spoken by each Speaker",
505
+ color_discrete_map=speaker_color_map,
506
  )
507
  # Hide x-axis tick labels β€” values are crowded with many speakers.
508
  # The exact time is still visible on hover via the hovertemplate.