# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <span style="font-family: Century Gothic; font-size:2.0em;color:navy;"> **fierClass: Visualization API** </p> 

# +
#   Authors: <NAME> (<EMAIL>)
#            <NAME> (<EMAIL>)
#            <NAME> (<EMAIL>, <EMAIL>)
#   Date: 2019/05/03.
#
#   If you are going to use fierClass in your research project, please cite its reference article
#   <NAME>, <NAME>, et al. "fierClass: A multi-signal, cepstrum-based, time series classifier,"
#   Engineering Applications of Artificial Intelligence, Volume 87, 2020, https://doi.org/10.1016/j.engappai.2019.103262.
#
#   Copyright and license: © <NAME>, <NAME>, <NAME>, Politecnico di Milano
#   Licensed under the [MIT License](LICENSE).
#
#  In case of need, feel free to ask the author.
# -

# <span style="font-family: Century Gothic; font-size:1.8em;color:SteelBlue;"> **Libraries** </p> 

# <span style="font-family: Century Gothic; font-size:1.2em;color:dimGray;"> *This code block imports all the required libraries and packages.* </p> 

# Visualization
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, Grid, LinearAxis
from bokeh.models import Plot, Step, Legend, LegendItem
from bokeh.models import Title
from bokeh.plotting import figure
from bokeh.palettes import Accent
from bokeh.io import output_notebook, curdoc, show
output_notebook()


# <span style="font-family: Century Gothic; font-size:1.8em;color:SteelBlue;"> **Cepstra Plot** </p> 

# <span style="font-family: Century Gothic; font-size:1.2em;color:dimGray;"> *This function plots the cepstra for all the instances contained in the input matrix.* </p> 

def train_cepstra_plot(order, train_cepstra, label):
    # Color palette definition (one for each classes)
    colors = Accent.get(train_cepstra.shape[0])

    # x-axis definition
    x = list(range(order))

    # Cepstra plots. Each plot contains the cepstrum of
    # the considered signal for each available class.
    plots = [figure() for i in range(train_cepstra.shape[1])]
    glyphs = [plot.line(x, train_cepstra[class_idx,0,:order],
                color=colors[class_idx], line_width=2,
                legend_label=label[class_idx]) 
              for plot in plots 
              for i in range(2)
              for class_idx in range(train_cepstra.shape[0])]

    # Legend settings
    for plot in plots:
        plot.legend.location = "top_right"
        plot.legend.click_policy= "hide"

    show(gridplot(children = plots, ncols = 3, merge_tools = False, 
                 plot_width=300, plot_height=300))


# <span style="font-family: Century Gothic; font-size:1.8em;color:SteelBlue;"> **Preditcions vs Actual Labels** </p> 

# <span style="font-family: Century Gothic; font-size:1.2em;color:dimGray;"> *This function plots the predicted and the actual labels, to allow the user have a visual evaluation of the trained classifier performance.* </p> 

def results_plot(y_test,y_pred,title,featurefusion):
    # x-axis definition
    x = list(range(len(y_test)))
    
    # Classes vs subclasses evaluation plot
    if(featurefusion!='conv'):
        source = ColumnDataSource(dict(x=x, y1=y_test, y2=y_pred[0]))
    else:
        source = ColumnDataSource(dict(x=x, y1=y_test, y2=y_pred))

    plot = Plot(plot_width=900, plot_height=700,
                title=Title(text=title, align="center"))

    # Actual labels
    glyph1 = Step(x="x", y="y1", line_width=2, line_color="#f46d43")
    plot.add_glyph(source, glyph1)

    # Predictedd labels
    glyph2 = Step(x="x", y="y2", line_width=2, line_dash="dashed", 
                  line_color="#1d91d0")
    plot.add_glyph(source, glyph2)

    xaxis = LinearAxis()
    plot.add_layout(xaxis, 'below')

    yaxis = LinearAxis()
    plot.add_layout(yaxis, 'left')

    # Legend
    li1 = LegendItem(label='True', renderers=[plot.renderers[0]])
    li2 = LegendItem(label='Predicted', renderers=[plot.renderers[1]])
    legend1 = Legend(items=[li1, li2], location='top_right')

    plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
    plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))
    plot.add_layout(legend1)
    plot.legend.click_policy= "hide"

    curdoc().add_root(plot)

    show(plot)

StarcoderdataJupyterScriptsDedupFiltered