예제 #1
0
def test_Image_kwargs() -> None:
    glyph = Image(x=0, y=0, dw=10, dh=10)
    assert glyph.image == field("image")
    assert glyph.x == 0
    assert glyph.y == 0
    assert glyph.dw == 10
    assert glyph.dh == 10
    assert glyph.dilate is False
예제 #2
0
def test_Image():
    glyph = Image()
    assert glyph.image is None
    assert glyph.x is None
    assert glyph.y is None
    assert glyph.dw is None
    assert glyph.dh is None
    assert glyph.dilate == False
    yield (check_properties_existence, glyph, [
        "image",
        "x",
        "y",
        "dw",
        "dw_units",
        "dh",
        "dh_units",
        "dilate",
        "color_mapper",
    ], GLYPH)
예제 #3
0
def test_Image() -> None:
    glyph = Image()
    assert glyph.image is None
    assert glyph.x == field("x")
    assert glyph.y == field("y")
    assert glyph.dw is None
    assert glyph.dh is None
    assert glyph.dilate is False
    check_properties_existence(glyph, [
        "image",
        "x",
        "y",
        "dw",
        "dw_units",
        "dh",
        "dh_units",
        "global_alpha",
        "dilate",
        "color_mapper",
    ], GLYPH)
예제 #4
0
    def create_plot(self):

        # define list of metric names (needed for tooltip)
        tmp = []
        metric_names = []
        yaxis = {}

        # number of datapoints
        max_width = 0
        for key in self.system_metrics.keys():
            if key.startswith("CPUUtilization"):
                width = self.system_metrics[key]["total"].shape[0]
                if width >= max_width:
                    max_width = width

        self.width = max_width

        for dimension in self.filtered_dimensions:
            for event in self.filtered_events:
                if event in self.system_metrics[dimension]:
                    values = self.system_metrics[dimension][event][:self.width,
                                                                   1]
                    tmp.append(values)
                    metric_names.append(dimension + "_" + event),
                    timestamps = self.system_metrics[dimension][event][:self.
                                                                       width,
                                                                       0]
                    yaxis[len(tmp)] = dimension + "_" + event

        ymax = len(tmp) + 1
        yaxis[ymax] = ""

        # define figure
        start = 0
        if self.width > 1000:
            start = self.width - 1000
        self.plot = figure(
            plot_height=self.plot_height,
            x_range=(start, self.width),
            y_range=(0, ymax),
            plot_width=1000,
            tools="crosshair,reset,xwheel_zoom, box_edit",
        )
        self.plot.xaxis.axis_label = "Indices"
        # tooltip
        hover = HoverTool(tooltips=[("usage",
                                     "@image"), ("metric",
                                                 "@metric"), ("index",
                                                              "$x{10}")])

        color_mapper = bokeh.models.LinearColorMapper(
            bokeh.palettes.viridis(100))
        color_mapper.high = 100
        color_mapper.low = 0

        tmp = np.array(tmp)
        self.source = ColumnDataSource(data=dict(
            image=[np.array(tmp[i]).reshape(1, -1) for i in range(len(tmp))],
            x=[0] * ymax,
            y=[i for i in range(ymax)],
            dw=[self.width] * (ymax),
            dh=[1.3] * (ymax),
            metric=[i for i in metric_names],
        ))

        images = Image(image="image",
                       x="x",
                       y="y",
                       dw="dw",
                       dh="dh",
                       color_mapper=color_mapper)

        # plot
        self.plot.add_glyph(self.source, images)
        self.plot.add_tools(hover)
        self.plot.xgrid.visible = False
        self.plot.ygrid.visible = False
        self.plot.yaxis.ticker = FixedTicker(ticks=np.arange(0, ymax).tolist())
        self.plot.yaxis.major_label_text_font_size = "5pt"
        self.plot.yaxis.major_label_overrides = yaxis

        self.target = show(self.plot, notebook_handle=True)
예제 #5
0
    def create_plot(self):

        # define list of metric names (needed for tooltip)
        tmp = []
        metric_names = []
        yaxis = {}

        for node in self.system_metrics:
            for dimension in self.system_metrics[node]:
                if dimension in self.filtered_dimensions:
                    for event in self.system_metrics[node][dimension]:
                        if event in self.filtered_events:
                            values = self.system_metrics[node][dimension][
                                event][:self.width]
                            tmp.append(values)
                            metric_names.append(dimension + "_" + event + "_" +
                                                node)
                            yaxis[len(
                                tmp)] = dimension + "_" + event + "_" + node
        ymax = len(tmp)
        yaxis[ymax] = ""

        # define figure
        start = 0
        if self.width > 1000:
            start = self.width - 1000
        self.plot = figure(
            plot_height=self.plot_height,
            x_range=(start, self.width),
            y_range=(0, ymax),
            plot_width=1000,
            tools="crosshair,reset,xwheel_zoom, box_edit",
        )
        self.plot.xaxis.axis_label = "Indices"

        # tooltip
        hover = HoverTool(tooltips=[("usage",
                                     "@image"), ("metric",
                                                 "@metric"), ("index",
                                                              "$x{10}")])

        # map colors to values between 0 and 100
        color_mapper = bokeh.models.LinearColorMapper(
            bokeh.palettes.viridis(100))
        color_mapper.high = 100
        color_mapper.low = 0

        tmp = np.array(tmp)

        # create column data source
        self.source = ColumnDataSource(data=dict(
            image=[np.array(tmp[i]).reshape(1, -1) for i in range(len(tmp))],
            x=[0] * ymax,
            y=[i for i in range(ymax)],
            dw=[self.width] * (ymax),
            dh=[1.3] * (ymax),
            metric=[i for i in metric_names],
        ))

        # heatmap placeholder
        images = Image(image="image",
                       x="x",
                       y="y",
                       dw="dw",
                       dh="dh",
                       color_mapper=color_mapper)

        # plot
        self.plot.add_glyph(self.source, images)
        self.plot.add_tools(hover)
        self.plot.xgrid.visible = False
        self.plot.ygrid.visible = False
        self.plot.yaxis.ticker = FixedTicker(ticks=np.arange(0, ymax).tolist())
        self.plot.yaxis.major_label_text_font_size = "7pt"
        self.plot.yaxis.major_label_overrides = yaxis
        self.plot.xaxis.major_label_text_font_size = "0pt"
        self.target = show(self.plot, notebook_handle=True)
    def create(self):

        # set size of figure
        self.p = figure(width=900,
                        plot_height=35 * self.n_tokens,
                        x_range=Range1d(0, self.n_tokens + 100),
                        y_range=Range1d(-1, self.n_tokens))

        self.p.xgrid.visible = False
        self.p.ygrid.visible = False
        self.p.axis.visible = False

        x = np.zeros(self.n_tokens) + 2
        y = np.flip(np.arange(0, self.n_tokens), axis=0) + 0.25

        # set input tokens in plot
        for token, x_i, y_i in zip(self.input_tokens, x, y):
            text1 = Label(x=x_i - 1, y=y_i, text=token, text_font_size='10pt')
            text2 = Label(x=x_i + 105,
                          y=y_i,
                          text=token,
                          text_font_size='10pt')
            self.p.add_layout(text2)
            self.p.add_layout(text1)

        # set plot labels
        text = Label(x=17, y=-1, text="query", text_font_size='15pt')
        self.p.add_layout(text)
        text = Label(x=50, y=-1, text="key", text_font_size='15pt')
        self.p.add_layout(text)
        text = Label(x=80, y=-1, text="q x k", text_font_size='15pt')
        self.p.add_layout(text)
        text = Label(x=98, y=-1, text="q * k", text_font_size='15pt')
        self.p.add_layout(text)

        color_mapper = LinearColorMapper(palette="Blues8", nan_color='white')

        #get key matrix and query vector
        key = self.keys[self.key_name][self.step][0, self.head, :, :]
        query = self.queries[self.query_name][self.step][
            0, self.head, :self.n_tokens, :]

        #plot key matrix
        self.source_key = ColumnDataSource(
            data=dict(image=[key[:self.n_tokens, :]],
                      x=[40],
                      y=[0],
                      dw=[25],
                      dh=[self.n_tokens]))
        img = Image(image="image",
                    x="x",
                    y="y",
                    dw="dw",
                    dh="dh",
                    color_mapper=color_mapper)
        self.p.add_glyph(self.source_key, img)

        #create an empty query matrix where only one vector is set
        query_input = np.zeros((self.n_tokens, query.shape[-1]))
        query_input[:, :] = np.nan
        query_input[self.query, :] = query[self.n_tokens - self.query - 1, :]
        self.source_query = ColumnDataSource(data=dict(
            image=[query_input], x=[10], y=[0], dw=[25], dh=[self.n_tokens]))
        img = Image(image="image",
                    x="x",
                    y="y",
                    dw="dw",
                    dh="dh",
                    color_mapper=color_mapper)
        self.p.add_glyph(self.source_query, img)

        #compute elementwise product between a query vector and key matrix
        product = np.multiply(query[self.n_tokens - self.query - 1, :], key)
        self.product = ColumnDataSource(
            data=dict(image=[product[:self.n_tokens, :]],
                      x=[70],
                      y=[0],
                      dw=[25],
                      dh=[self.n_tokens]))
        img = Image(image="image",
                    x="x",
                    y="y",
                    dw="dw",
                    dh="dh",
                    color_mapper=color_mapper)
        self.p.add_glyph(self.product, img)

        #compute dot product between query vector and key matrix
        dot_product = np.dot(key, query[self.n_tokens - self.query - 1, :])
        dot_product = dot_product.reshape((dot_product.shape[0], 1))
        self.dot_product = ColumnDataSource(
            data=dict(image=[dot_product[:self.n_tokens, :]],
                      x=[100],
                      y=[0],
                      dw=[2],
                      dh=[self.n_tokens]))
        img = Image(image="image",
                    x="x",
                    y="y",
                    dw="dw",
                    dh="dh",
                    color_mapper=color_mapper)
        self.p.add_glyph(self.dot_product, img)

        show(self.p, notebook_handle=True)