Skip to content

Commit

Permalink
Add option to disable histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmckinsey1 committed Nov 15, 2024
1 parent 6ee258a commit af9ccf7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
64 changes: 33 additions & 31 deletions thicket/external/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def render(self, roots, dataframe, **kwargs):
self.min_value = kwargs["min_value"]
self.max_value = kwargs["max_value"]
self.indices = kwargs["indices"]
self.full_df = kwargs["full_df"]
self.hist_data = kwargs["hist_data"]
self.histogram = kwargs["histogram"]

if self.color:
self.colors = self.colors_enabled
Expand Down Expand Up @@ -274,36 +275,37 @@ def render_frame(self, node, dataframe, indent="", child_indent=""):
indent=indent, metric_str=metric_str, name_str=name_str
)

nprofs = len(self.full_df.loc[df_index])
# Auto choose num bins
nintervals = math.ceil(math.sqrt(nprofs))
# Add min/max to tree
min = self.full_df.loc[df_index].min()
max = self.full_df.loc[df_index].max()
result += f" ({min:.{self.precision}f}, {max:.{self.precision}f})"
# Define unicode bars
bar_list = [
"_",
"\u2581",
"\u2582",
"\u2583",
"\u2584",
"\u2585",
"\u2586",
"\u2587",
"\u2588",
]
# Compute histogram intervals using pandas binning
binned = pd.cut(self.full_df.loc[df_index], bins=nintervals)
hist = binned.value_counts().sort_index()
# Normalize values to the number of bars
normalized_hist = (
(len(bar_list) - 1) * (hist - hist.min()) / (hist.max() - hist.min())
)
normalized_hist = normalized_hist.apply(np.ceil).astype(int)
# Add histogram to tree
for idx in normalized_hist.values:
result += bar_list[idx]
if self.histogram:
nprofs = len(self.hist_data.loc[df_index])
# Auto choose num bins
nintervals = math.ceil(math.sqrt(nprofs))
# Add min/max to tree
min = self.hist_data.loc[df_index].min()
max = self.hist_data.loc[df_index].max()
result += f" ({min:.{self.precision}f}, {max:.{self.precision}f}) "
# Define unicode bars
bar_list = [
"_",
"\u2581",
"\u2582",
"\u2583",
"\u2584",
"\u2585",
"\u2586",
"\u2587",
"\u2588",
]
# Compute histogram intervals using pandas binning
binned = pd.cut(self.hist_data.loc[df_index], bins=nintervals)
hist = binned.value_counts().sort_index()
# Normalize values to the number of bars
normalized_hist = (
(len(bar_list) - 1) * (hist - hist.min()) / (hist.max() - hist.min())
)
normalized_hist = normalized_hist.apply(np.ceil).astype(int)
# Add histogram to tree
for idx in normalized_hist.values:
result += bar_list[idx]

if self.context in dataframe.columns:
result += " {c.faint}{context}{c.end}\n".format(
Expand Down
5 changes: 4 additions & 1 deletion thicket/thicket.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ def tree(
min_value=None,
max_value=None,
indices=None,
histogram=True,
):
"""Visualize the Thicket as a tree
Expand All @@ -1011,6 +1012,7 @@ def tree(
min_value (int, optional): Overwrites the min value for the coloring legend. Defaults to None.
max_value (int, optional): Overwrites the max value for the coloring legend. Defaults to None.
indices(tuple, list, optional): Index/indices to display on the DataFrame. Defaults to None.
histogram (bool, optional): Whether to show a histogram next to each node of the data for all profiles. Defaults to True.
Returns:
(str): String representation of the tree, ready to print
Expand Down Expand Up @@ -1122,7 +1124,8 @@ def tree(
min_value=min_value,
max_value=max_value,
indices=idx_dict,
full_df=self.dataframe[metric_column],
hist_data=self.dataframe[metric_column],
histogram=histogram,
)

@staticmethod
Expand Down

0 comments on commit af9ccf7

Please sign in to comment.