Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/plot.py
Show All 12 Lines | |||||
The size of the directory is defined by the total number of contents whereas | The size of the directory is defined by the total number of contents whereas | ||||
the color gradient is generated relying on the percentage of contents known. | the color gradient is generated relying on the percentage of contents known. | ||||
""" | """ | ||||
from typing import List, Dict, Tuple | from typing import List, Dict, Tuple | ||||
from pathlib import PosixPath | from pathlib import PosixPath | ||||
from plotly.offline import offline # type: ignore | from plotly.offline import offline | ||||
import plotly.graph_objects as go # type: ignore | import plotly.graph_objects as go | ||||
import pandas as pd # type: ignore | import pandas as pd # type: ignore | ||||
import numpy as np # type: ignore | import numpy as np # type: ignore | ||||
def build_hierarchical_df( | def build_hierarchical_df( | ||||
dirs_dataframe: pd.DataFrame, | dirs_dataframe: pd.DataFrame, | ||||
levels: List[str], | levels: List[str], | ||||
metrics_columns: List[str], | metrics_columns: List[str], | ||||
▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | ) -> pd.DataFrame: | ||||
) | ) | ||||
df["contents"] = pd.to_numeric(df["contents"]) | df["contents"] = pd.to_numeric(df["contents"]) | ||||
df["known"] = pd.to_numeric(df["known"]) | df["known"] = pd.to_numeric(df["known"]) | ||||
return df | return df | ||||
def sunburst(directories: Dict[PosixPath, Tuple[int, int]], root: PosixPath) -> None: | def generate_sunburst( | ||||
"""Show the sunburst chart from the directories given in input. | directories: Dict[PosixPath, Tuple[int, int]], root: PosixPath | ||||
) -> go.Sunburst: | |||||
"""Generate a sunburst chart from the directories given in input. | |||||
""" | """ | ||||
max_depth = compute_max_depth(list(directories.keys()), root) | max_depth = compute_max_depth(list(directories.keys()), root) | ||||
metrics_columns = ["contents", "known"] | metrics_columns = ["contents", "known"] | ||||
levels_columns = ["lev" + str(i) for i in range(max_depth)] | levels_columns = ["lev" + str(i) for i in range(max_depth)] | ||||
df_columns = levels_columns + metrics_columns | df_columns = levels_columns + metrics_columns | ||||
dirs_df = generate_df_from_dirs(directories, df_columns, root, max_depth) | dirs_df = generate_df_from_dirs(directories, df_columns, root, max_depth) | ||||
hierarchical_df = build_hierarchical_df( | hierarchical_df = build_hierarchical_df( | ||||
dirs_df, levels_columns, metrics_columns, str(root) | dirs_df, levels_columns, metrics_columns, str(root) | ||||
) | ) | ||||
fig = go.Figure() | sunburst = go.Sunburst( | ||||
fig.add_trace( | |||||
go.Sunburst( | |||||
labels=hierarchical_df["id"], | labels=hierarchical_df["id"], | ||||
parents=hierarchical_df["parent"], | parents=hierarchical_df["parent"], | ||||
values=hierarchical_df["contents"], | values=hierarchical_df["contents"], | ||||
branchvalues="total", | branchvalues="total", | ||||
marker=dict( | marker=dict( | ||||
colors=hierarchical_df["known"], | colors=hierarchical_df["known"], | ||||
colorscale="matter", | colorscale="matter", | ||||
cmid=50, | cmid=50, | ||||
showscale=True, | showscale=True, | ||||
), | ), | ||||
hovertemplate="""<b>%{label}</b> | hovertemplate="""<b>%{label}</b> | ||||
<br>Files: %{value} | <br>Files: %{value} | ||||
<br>Known: <b>%{color:.2f}%</b>""", | <br>Known: <b>%{color:.2f}%</b>""", | ||||
name="", | name="", | ||||
) | ) | ||||
) | |||||
offline.plot(fig, filename="sunburst.html") | return sunburst | ||||
def offline_plot(graph_object: go): | |||||
"""Plot a graph object to an html file | |||||
""" | |||||
fig = go.Figure() | |||||
fig.add_trace(graph_object) | |||||
offline.plot(fig, filename="chart.html") |