Skip to content

Instantly share code, notes, and snippets.

@blu3r4y
Created May 18, 2021 09:39
Show Gist options
  • Save blu3r4y/d536ec45623b8d7a184d3239986d7291 to your computer and use it in GitHub Desktop.
Save blu3r4y/d536ec45623b8d7a184d3239986d7291 to your computer and use it in GitHub Desktop.
Network flow visualization used by Dynatrace - SAL - LIT.AI.JKU in the NAD 2021 challenge
# Copyright 2021
# Dynatrace Research
# SAL Silicon Austria Labs
# LIT Artificial Intelligence Lab
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Dict
import pandas as pd
import datashader as ds
import holoviews as hv
import holoviews.operation.datashader as hd
from IPython.core.display import display, HTML
hv.extension("bokeh")
def _get_data_matrix(df: pd.DataFrame, groups: List[str]) -> pd.DataFrame:
# merge those group columns into a group label and get the group x time grouping
df["group"] = df.groupby(groups).ngroup()
mat = df.groupby(["group", "time"])["label"].agg(["count", "median"]).astype(int)
# sort by group size and re-index groups
num_groups = mat.index.get_level_values(0).nunique()
# add the broadcasted group size to the "group" level
sizes = df.groupby("group").size()
_, sizes = mat.align(sizes, level=0, axis=0)
mat["size"] = sizes
# sort by the group size, then by group ids
mat = mat.reset_index().sort_values(by=["size", "group"])
# modify the group ids so that they are ascending again
mapping = dict(zip(mat["group"].unique(), range(num_groups)))
mat["group"] = mat["group"].map(mapping)
return mat
def shade_network_flow(df: pd.DataFrame, groups: List[str], colors: Dict[int, str], legend: bool = True):
if legend:
html = "<br/>".join([
f"<span style=\"background-color:black;color:{val};font-weight:bold;\">" +
f"{key} : {val}" +
f"</span>"
for key, val in colors.items()
])
display(HTML(html))
matrix = _get_data_matrix(df, groups)
points = hv.Points(matrix, ["group", "time"])
shaded = hd.datashade(points, aggregator=ds.count_cat("median"), color_key=colors)
render = hd.dynspread(shaded, threshold=0.5, max_px=4).opts(
bgcolor="black", xaxis=None, yaxis=None, width=1200, height=600)
return render
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
ipython
bokeh>=2
numpy>=1.19
pandas>=1.2
holoviews>=1.14
datashader>=0.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment