def build_single_loudness_chart()

in python/ts-to-word.py [0:0]


def build_single_loudness_chart(axes, loudness, interrupts, quiet_time, speech_segments, xaxis_max, yaxis_max, caller,
                                show_x_legend, show_chart_legend):
    """
    Builds a single loundness/sentiment chart using the given data

    :param axes: Axis to use for the chart in our larger table
    :param loudness: Data series for the speakers loudness levels
    :param interrupts: Data series for marking interrupts on the chart
    :param quiet_time: Data series for marking non-talk time on the chart
    :param speech_segments: Call transcript structures
    :param xaxis_max: Second for the last speech entry in the call, which may not have been this speaker
    :param yaxis_max: Max decibel level in the call, which may not have been this speaker
    :param caller: Name of the caller to check for in the transcript
    :param show_x_legend: Flag to show/hide the x-axis legend
    :param show_chart_legend: Flag to show/hide the top-right graph legend
    """

    # Draw the main loudness data bar-chart
    seconds = loudness["Seconds"]
    decibels = loudness["dB"]
    axes.bar(seconds, decibels, label="Speaker volume", width=BAR_CHART_WIDTH)
    axes.set_xlim(xmin=0, xmax=xaxis_max)
    axes.set_ylim(ymax=yaxis_max)
    if show_x_legend:
        axes.set_xlabel("Time (in seconds)")
    axes.set_ylabel("decibels")

    # Build up sentiment data series for positive and negative, plotting it at the bottom
    x = np.linspace(0, max(seconds), endpoint=True, num=(max(seconds) + 1))
    ypos = np.linspace(0, 0, endpoint=True, num=(max(seconds) + 1))
    yneg = np.linspace(0, 0, endpoint=True, num=(max(seconds) + 1))
    yneut = np.linspace(0, 0, endpoint=True, num=(max(seconds) + 1))
    for segment in speech_segments:
        this_second = int(segment.segmentStartTime)
        if segment.segmentSpeaker == caller:
            if segment.segmentIsPositive:
                for score in segment.segmentLoudnessScores:
                    ypos[this_second] = 10
                    this_second += 1
            elif segment.segmentNegative:
                for score in segment.segmentLoudnessScores:
                    yneg[this_second] = 10
                    this_second += 1
            else:
                for score in segment.segmentLoudnessScores:
                    yneut[this_second] = 10
                    this_second += 1
    axes.bar(x, ypos, label="Positive sentiment", color="limegreen", width=BAR_CHART_WIDTH)
    axes.bar(x, yneg, label="Negative sentiment", color="orangered", width=BAR_CHART_WIDTH)
    axes.bar(x, yneut, label="Neutral sentiment", color="cadetblue", width=BAR_CHART_WIDTH)

    # Finish with the non-talk and interrupt overlays (if there are any)
    if len(quiet_time["Seconds"]) > 0:
        axes.bar(quiet_time["Seconds"], quiet_time["dB"], label="Non-talk time", color="lightcyan", width=BAR_CHART_WIDTH)
    if len(interrupts["Seconds"]) > 0:
        axes.bar(interrupts["Seconds"], interrupts["dB"], label="Interruptions", color="goldenrod", width=BAR_CHART_WIDTH, alpha=0.5, bottom=10)

    # Only show the legend for the top graph if requested
    box = axes.get_position()
    axes.set_position([0.055, box.y0, box.width, box.height])
    axes.text(5, yaxis_max-5, caller, style='normal', color='black', bbox={'facecolor': 'white', 'pad': 5})
    if show_chart_legend:
        axes.legend(loc="upper right", bbox_to_anchor=(1.21, 1.0), ncol=1, borderaxespad=0)