duongthienz commited on
Commit
1f18cf5
·
verified ·
1 Parent(s): 909f662

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +26 -11
utils.py CHANGED
@@ -181,14 +181,29 @@ def build_df5(oneVoice, multiVoice, sumNoVoice, sumOneVoice, sumMultiVoice, curr
181
  multiSpeakerList = [p[0] for p in mv_pairs]
182
  multiTimeList = [p[1] for p in mv_pairs]
183
 
 
 
184
  summativeMulti = sum(multiTimeList) if multiTimeList else 1
185
- safeOneVoice = sumOneVoice if sumOneVoice > 0 else 1
186
 
187
- safeTotalTime = currTotalTime if currTotalTime > 0 else 1
188
  base = [sumNoVoice / safeTotalTime, sumOneVoice / safeTotalTime, sumMultiVoice / safeTotalTime]
189
 
190
- timeStrings = su.timeToString(timeList) if timeList else []
191
- multiTimeStrings = su.timeToString(multiTimeList) if multiTimeList else []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  if isinstance(timeStrings, str):
193
  timeStrings = [timeStrings]
194
  if isinstance(multiTimeStrings, str):
@@ -202,18 +217,18 @@ def build_df5(oneVoice, multiVoice, sumNoVoice, sumOneVoice, sumMultiVoice, curr
202
  "labels": ["No Voice", "Single Voice", "Multi Voice"] + speakerList + multiSpeakerList,
203
  "parents": ["", "", ""] + ["OV"] * n_ov + ["MV"] * n_mv,
204
  "parentNames": ["Total", "Total", "Total"] + ["Single Voice"] * n_ov + ["Multi Voice"] * n_mv,
205
- "values": [sumNoVoice, sumOneVoice, sumMultiVoice] + timeList + multiTimeList,
206
  "valueStrings": [
207
  su.timeToString(sumNoVoice),
208
  su.timeToString(sumOneVoice),
209
  su.timeToString(sumMultiVoice),
210
  ] + timeStrings + multiTimeStrings,
211
  "percentiles": [b * 100 for b in base]
212
- + [(t * 100) / safeOneVoice * base[1] for t in timeList]
213
- + [(t * 100) / summativeMulti * base[2] for t in multiTimeList],
214
  "parentPercentiles": [b * 100 for b in base]
215
- + [(t * 100) / safeOneVoice for t in timeList]
216
- + [(t * 100) / summativeMulti for t in multiTimeList],
217
  })
218
 
219
 
@@ -275,7 +290,7 @@ def build_fig_sunburst(df5, catTypeColors, speakerColors, get_display_name_fn, c
275
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
276
  fig = px.sunburst(
277
  df5,
278
- branchvalues="remainder",
279
  names="labels", ids="ids", parents="parents",
280
  values="percentiles",
281
  custom_data=["labels", "valueStrings", "percentiles", "parentNames", "parentPercentiles"],
@@ -301,7 +316,7 @@ def build_fig_treemap(df5, catTypeColors, speakerColors, get_display_name_fn, cu
301
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
302
  fig = px.treemap(
303
  df5,
304
- branchvalues="remainder",
305
  names="labels", parents="parents", ids="ids",
306
  values="percentiles",
307
  custom_data=["labels", "valueStrings", "percentiles", "parentNames", "parentPercentiles"],
 
181
  multiSpeakerList = [p[0] for p in mv_pairs]
182
  multiTimeList = [p[1] for p in mv_pairs]
183
 
184
+ safeTotalTime = currTotalTime if currTotalTime > 0 else 1
185
+ safeOneVoice = sumOneVoice if sumOneVoice > 0 else 1
186
  summativeMulti = sum(multiTimeList) if multiTimeList else 1
 
187
 
 
188
  base = [sumNoVoice / safeTotalTime, sumOneVoice / safeTotalTime, sumMultiVoice / safeTotalTime]
189
 
190
+ # Normalize child times so they sum exactly to their parent's total.
191
+ # This guarantees branchvalues="total" is satisfied regardless of whether
192
+ # the classifier's segment durations perfectly match sumOneVoice/sumMultiVoice.
193
+ sumTimeList = sum(timeList) if timeList else 0
194
+ if sumTimeList > 0:
195
+ normTimeList = [t / sumTimeList * sumOneVoice for t in timeList]
196
+ else:
197
+ normTimeList = timeList
198
+
199
+ sumMultiTimeList = sum(multiTimeList) if multiTimeList else 0
200
+ if sumMultiTimeList > 0:
201
+ normMultiTimeList = [t / sumMultiTimeList * sumMultiVoice for t in multiTimeList]
202
+ else:
203
+ normMultiTimeList = multiTimeList
204
+
205
+ timeStrings = su.timeToString(normTimeList) if normTimeList else []
206
+ multiTimeStrings = su.timeToString(normMultiTimeList) if normMultiTimeList else []
207
  if isinstance(timeStrings, str):
208
  timeStrings = [timeStrings]
209
  if isinstance(multiTimeStrings, str):
 
217
  "labels": ["No Voice", "Single Voice", "Multi Voice"] + speakerList + multiSpeakerList,
218
  "parents": ["", "", ""] + ["OV"] * n_ov + ["MV"] * n_mv,
219
  "parentNames": ["Total", "Total", "Total"] + ["Single Voice"] * n_ov + ["Multi Voice"] * n_mv,
220
+ "values": [sumNoVoice, sumOneVoice, sumMultiVoice] + normTimeList + normMultiTimeList,
221
  "valueStrings": [
222
  su.timeToString(sumNoVoice),
223
  su.timeToString(sumOneVoice),
224
  su.timeToString(sumMultiVoice),
225
  ] + timeStrings + multiTimeStrings,
226
  "percentiles": [b * 100 for b in base]
227
+ + [t / safeTotalTime * 100 for t in normTimeList]
228
+ + [t / safeTotalTime * 100 for t in normMultiTimeList],
229
  "parentPercentiles": [b * 100 for b in base]
230
+ + [t / safeOneVoice * 100 for t in normTimeList]
231
+ + [t / summativeMulti * 100 for t in normMultiTimeList],
232
  })
233
 
234
 
 
290
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
291
  fig = px.sunburst(
292
  df5,
293
+ branchvalues="total",
294
  names="labels", ids="ids", parents="parents",
295
  values="percentiles",
296
  custom_data=["labels", "valueStrings", "percentiles", "parentNames", "parentPercentiles"],
 
316
  df5["parentNames"] = df5["parentNames"].apply(lambda s: get_display_name_fn(s, currFile))
317
  fig = px.treemap(
318
  df5,
319
+ branchvalues="total",
320
  names="labels", parents="parents", ids="ids",
321
  values="percentiles",
322
  custom_data=["labels", "valueStrings", "percentiles", "parentNames", "parentPercentiles"],