Skip to content

Instantly share code, notes, and snippets.

@blu3r4y
Created May 18, 2021 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "saving-olive",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from network_flow_visualization import shade_network_flow "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "conventional-mason",
"metadata": {},
"outputs": [],
"source": [
"def get_synthetic_data(size: int = 1_000_000, n_classes: int = 5) -> pd.DataFrame:\n",
" assert size >= 1_000\n",
" assert n_classes >= 2\n",
"\n",
" # sample timestamps from the current day\n",
" today = np.datetime64(\"today\", \"s\")\n",
" tomorrow = today + np.timedelta64(1, \"D\")\n",
" timestamps = np.random.randint(today.astype(int), tomorrow.astype(int), size=size)\n",
" timestamps = np.sort(timestamps)\n",
"\n",
" # sample some labels with a strong bias towards the first class\n",
" weights = np.array([2 * n_classes] + [1] * (n_classes - 1))\n",
" weights = weights / np.sum(weights)\n",
" labels = np.random.choice(range(n_classes), p=weights, size=size)\n",
"\n",
" # sample source and destination port numbers\n",
" spts = np.random.randint(0, 65_535, size=size)\n",
" dpts = np.random.randint(0, 65_535, size=size)\n",
"\n",
" # add a few block-like artifacts\n",
" min_artifact_size = size / 200\n",
" for _ in range(1_000):\n",
" tstart = np.random.randint(size)\n",
" tlength = np.random.randint(0, min(min_artifact_size, size - tstart - 1))\n",
" spts[tstart:tstart + tlength] = np.random.choice(spts)\n",
" labels[tstart:tstart + tlength] = np.random.choice(labels)\n",
"\n",
" return pd.DataFrame({\n",
" \"time\": timestamps,\n",
" \"spt\": spts,\n",
" \"dpt\": dpts,\n",
" \"label\": labels\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "nearby-elder",
"metadata": {},
"outputs": [],
"source": [
"df = get_synthetic_data(n_classes=5)\n",
"\n",
"colors = {0: \"#37474F\", 1: \"#E040FB\", 2: \"#18FFFF\", 3: \"#FFFF00\", 4: \"#C6FF00\"}\n",
"groups = [\"spt\", \"dpt\"]\n",
"\n",
"# to see the interactive plots, install\n",
"# jupyter labextension install @pyviz/jupyterlab_pyviz\n",
"\n",
"shade_network_flow(df, groups, colors, legend=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
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