Skip to content

Commit 5c2a4ba

Browse files
committed
Fix plotly kaleido orjson type issues
1 parent de38e79 commit 5c2a4ba

3 files changed

Lines changed: 54 additions & 34 deletions

File tree

domains/anomaly-detection/treemapVisualizations.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ def query_cypher_to_data_frame(query: LiteralString, parameters: Optional[Dict[s
166166
# Ignore kaleido logging noise when writing images
167167
logging.getLogger("kaleido").setLevel(logging.WARNING)
168168

169+
170+
def plotly_values(data: pd.Series | pd.DataFrame) -> list:
171+
"""Returns pandas column(s) as a plain Python list for Plotly/orjson serialization."""
172+
return data.to_numpy().tolist()
173+
174+
169175
def get_plotly_figure_write_image_settings(name: str, path: str):
170176
"""
171177
Returns the settings for the plotly figure write_image method
@@ -187,10 +193,10 @@ def create_treemap_settings(data_frame: pd.DataFrame, element_path_column: str =
187193
return :plotly_graph_objects.Treemap : The prepared Plotly Treemap
188194
"""
189195
return plotly_graph_objects.Treemap(
190-
labels=data_frame[element_name_column],
191-
parents=data_frame['directoryParentPath'],
192-
ids=data_frame[element_path_column],
193-
customdata=data_frame[['fileCount', 'absoluteAnomalyScore', 'normalizedBridgeRank', 'normalizedOutlierRank', 'elementPath']],
196+
labels=plotly_values(data_frame[element_name_column]),
197+
parents=plotly_values(data_frame['directoryParentPath']),
198+
ids=plotly_values(data_frame[element_path_column]),
199+
customdata=plotly_values(data_frame[['fileCount', 'absoluteAnomalyScore', 'normalizedBridgeRank', 'normalizedOutlierRank', 'elementPath']]),
194200
hovertemplate='<b>%{label}</b><br>Highlighted anomalies: %{customdata[0]}<br>Anomaly Score: %{customdata[1]:.4f}<br>Bridge: %{customdata[2]}, Outlier: %{customdata[3]}<br>Path: %{customdata[4]}',
195201
maxdepth=-1,
196202
root_color="lightgrey",
@@ -530,7 +536,7 @@ def query_data() -> pd.DataFrame:
530536
create_treemap_settings(anomaly_file_paths),
531537
marker=dict(
532538
**plotly_treemap_marker_base_color_scale,
533-
colors=anomaly_file_paths['absoluteAnomalyScore'],
539+
colors=plotly_values(anomaly_file_paths['absoluteAnomalyScore']),
534540
colorbar={"title": "score"},
535541
),
536542
))

domains/archetypes/treemapVisualizations.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ def query_cypher_to_data_frame(query: LiteralString, parameters: Optional[Dict[s
156156
# Ignore kaleido logging noise when writing images
157157
logging.getLogger("kaleido").setLevel(logging.WARNING)
158158

159+
160+
def plotly_values(data: pd.Series | pd.DataFrame) -> list:
161+
"""Returns pandas column(s) as a plain Python list for Plotly/orjson serialization."""
162+
return data.to_numpy().tolist()
163+
164+
159165
def get_plotly_figure_write_image_settings(name: str, path: str):
160166
return {
161167
"file": path + "/" + name + "." + image_rendering_settings['format'],
@@ -167,10 +173,10 @@ def get_plotly_figure_write_image_settings(name: str, path: str):
167173

168174
def create_treemap_settings(data_frame: pd.DataFrame, element_path_column: str = 'elementPath', element_name_column: str = "elementName") -> plotly_graph_objects.Treemap:
169175
return plotly_graph_objects.Treemap(
170-
labels=data_frame[element_name_column],
171-
parents=data_frame['directoryParentPath'],
172-
ids=data_frame[element_path_column],
173-
customdata=data_frame[['fileCount', 'normalizedAuthorityRank', 'normalizedBottleneckRank', 'normalizedHubRank', 'elementPath']],
176+
labels=plotly_values(data_frame[element_name_column]),
177+
parents=plotly_values(data_frame['directoryParentPath']),
178+
ids=plotly_values(data_frame[element_path_column]),
179+
customdata=plotly_values(data_frame[['fileCount', 'normalizedAuthorityRank', 'normalizedBottleneckRank', 'normalizedHubRank', 'elementPath']]),
174180
hovertemplate='<b>%{label}</b><br>Highlighted archetypes: %{customdata[0]}<br>Authority: %{customdata[1]}, Bottleneck: %{customdata[2]}, Hub: %{customdata[3]}<br>Path: %{customdata[4]}',
175181
maxdepth=-1,
176182
root_color="lightgrey",

domains/git-history/gitHistoryCharts.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636

3737
SCRIPT_NAME = "gitHistoryCharts"
3838

39+
40+
# ── Plotly serialization ──────────────────────────────────────────────────────
41+
42+
def plotly_values(data: pd.Series | pd.DataFrame) -> list:
43+
"""Returns pandas column(s) as a plain Python list for Plotly/orjson serialization."""
44+
return data.to_numpy().tolist()
45+
3946
# ── Plotly layout constants ───────────────────────────────────────────────────
4047

4148
PLOTLY_MAIN_LAYOUT_BASE_SETTINGS: dict[str, Any] = dict(
@@ -337,11 +344,11 @@ def prepare_directory_commit_statistics(commit_statistics_data: pd.DataFrame) ->
337344

338345
def create_treemap_commit_statistics_settings(data_frame: pd.DataFrame) -> plotly_graph_objects.Treemap:
339346
"""Creates a Plotly Treemap with the given settings and data frame."""
340-
return plotly_graph_objects.Treemap(
341-
labels=data_frame["directoryName"],
342-
parents=data_frame["directoryParentPath"],
343-
ids=data_frame["directoryPath"],
344-
customdata=data_frame[
347+
params = dict(
348+
labels=plotly_values(data_frame["directoryName"]),
349+
parents=plotly_values(data_frame["directoryParentPath"]),
350+
ids=plotly_values(data_frame["directoryPath"]),
351+
customdata=plotly_values(data_frame[
345352
[
346353
"fileCount",
347354
"mostFrequentFileExtension",
@@ -357,7 +364,7 @@ def create_treemap_commit_statistics_settings(data_frame: pd.DataFrame) -> plotl
357364
"daysSinceLastModification",
358365
"directoryPath",
359366
]
360-
],
367+
]),
361368
hovertemplate=(
362369
"<b>%{label}</b><br>"
363370
"Files: %{customdata[0]} (%{customdata[1]})<br>"
@@ -372,20 +379,21 @@ def create_treemap_commit_statistics_settings(data_frame: pd.DataFrame) -> plotl
372379
root_color="lightgrey",
373380
marker=dict(**PLOTLY_TREEMAP_MARKER_BASE_STYLE),
374381
)
382+
return plotly_graph_objects.Treemap(**params)
375383

376384

377385
def create_rank_colorbar_for_graph_objects_treemap_marker(data_frame: pd.DataFrame, name_column: str, rank_column: str) -> dict:
378386
"""Creates a plotly graph_objects.Treemap marker object for a colorbar representing ranked names."""
379387
inverse_ranked = data_frame[rank_column].max() + 1 - data_frame[rank_column]
380388
return dict(
381389
cornerradius=5,
382-
colors=inverse_ranked,
390+
colors=plotly_values(inverse_ranked),
383391
colorscale=plotly_colors.qualitative.G10,
384392
colorbar=dict(
385393
title="Rank",
386394
tickmode="array",
387-
ticktext=data_frame[name_column],
388-
tickvals=inverse_ranked,
395+
ticktext=plotly_values(data_frame[name_column]),
396+
tickvals=plotly_values(inverse_ranked),
389397
tickfont_size=10,
390398
),
391399
)
@@ -410,7 +418,7 @@ def generate_directory_commit_statistic_treemaps(
410418
# 1. Number of files per directory
411419
figure = plotly_graph_objects.Figure(plotly_graph_objects.Treemap(
412420
create_treemap_commit_statistics_settings(git_files_with_commit_statistics),
413-
values=git_files_with_commit_statistics["fileCount"],
421+
values=plotly_values(git_files_with_commit_statistics["fileCount"]),
414422
))
415423
figure.update_layout(**PLOTLY_MAIN_LAYOUT_BASE_SETTINGS, title="Directories and their file count")
416424
write_image_and_log(figure, report_directory, "NumberOfFilesPerDirectory", verbose)
@@ -437,7 +445,7 @@ def generate_directory_commit_statistic_treemaps(
437445
create_treemap_commit_statistics_settings(git_commit_count_per_directory),
438446
marker=dict(
439447
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
440-
colors=git_commit_count_per_directory["commitCount_limited"],
448+
colors=plotly_values(git_commit_count_per_directory["commitCount_limited"]),
441449
colorbar=dict(title="Commits"),
442450
),
443451
))
@@ -450,7 +458,7 @@ def generate_directory_commit_statistic_treemaps(
450458
create_treemap_commit_statistics_settings(git_commit_authors_per_directory),
451459
marker=dict(
452460
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
453-
colors=git_commit_authors_per_directory["authorCount_limited"],
461+
colors=plotly_values(git_commit_authors_per_directory["authorCount_limited"]),
454462
colorbar=dict(title="Authors"),
455463
),
456464
))
@@ -459,13 +467,13 @@ def generate_directory_commit_statistic_treemaps(
459467

460468
# 5. Directories with very few different authors (low bus-factor, focus = few authors)
461469
git_commit_authors_per_directory_low_focus = add_quantile_limited_column(git_files_with_commit_statistics, "authorCount", 0.33)
462-
author_count_top_limit = git_commit_authors_per_directory_low_focus["authorCount_limited"].max().astype(int).astype(str)
470+
author_count_top_limit = str(int(git_commit_authors_per_directory_low_focus["authorCount_limited"].max()))
463471
author_count_top_limit_label_alias = {author_count_top_limit: author_count_top_limit + " or more"}
464472
figure = plotly_graph_objects.Figure(plotly_graph_objects.Treemap(
465473
create_treemap_commit_statistics_settings(git_commit_authors_per_directory_low_focus),
466474
marker=dict(
467475
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
468-
colors=git_commit_authors_per_directory_low_focus["authorCount_limited"],
476+
colors=plotly_values(git_commit_authors_per_directory_low_focus["authorCount_limited"]),
469477
colorbar=dict(
470478
title="Authors",
471479
tickmode="auto",
@@ -518,7 +526,7 @@ def generate_directory_commit_statistic_treemaps(
518526
create_treemap_commit_statistics_settings(git_commit_days_since_last_commit_per_directory),
519527
marker=dict(
520528
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
521-
colors=git_commit_days_since_last_commit_per_directory["daysSinceLastCommit_limited"],
529+
colors=plotly_values(git_commit_days_since_last_commit_per_directory["daysSinceLastCommit_limited"]),
522530
colorbar=dict(title="Days"),
523531
),
524532
))
@@ -531,7 +539,7 @@ def generate_directory_commit_statistic_treemaps(
531539
create_treemap_commit_statistics_settings(git_commit_days_since_last_commit_per_directory),
532540
marker=dict(
533541
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
534-
colors=git_commit_days_since_last_commit_per_directory["daysSinceLastCommit_rank"],
542+
colors=plotly_values(git_commit_days_since_last_commit_per_directory["daysSinceLastCommit_rank"]),
535543
colorbar=dict(title="Rank"),
536544
),
537545
))
@@ -544,7 +552,7 @@ def generate_directory_commit_statistic_treemaps(
544552
create_treemap_commit_statistics_settings(git_commit_days_since_last_file_creation_per_directory),
545553
marker=dict(
546554
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
547-
colors=git_commit_days_since_last_file_creation_per_directory["daysSinceLastCreation_limited"],
555+
colors=plotly_values(git_commit_days_since_last_file_creation_per_directory["daysSinceLastCreation_limited"]),
548556
colorbar=dict(title="Days"),
549557
),
550558
))
@@ -557,7 +565,7 @@ def generate_directory_commit_statistic_treemaps(
557565
create_treemap_commit_statistics_settings(git_commit_days_since_last_file_creation_per_directory),
558566
marker=dict(
559567
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
560-
colors=git_commit_days_since_last_file_creation_per_directory["daysSinceLastCreation_rank"],
568+
colors=plotly_values(git_commit_days_since_last_file_creation_per_directory["daysSinceLastCreation_rank"]),
561569
colorbar=dict(title="Rank"),
562570
),
563571
))
@@ -570,7 +578,7 @@ def generate_directory_commit_statistic_treemaps(
570578
create_treemap_commit_statistics_settings(git_commit_days_since_last_file_modification_per_directory),
571579
marker=dict(
572580
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
573-
colors=git_commit_days_since_last_file_modification_per_directory["daysSinceLastModification_limited"],
581+
colors=plotly_values(git_commit_days_since_last_file_modification_per_directory["daysSinceLastModification_limited"]),
574582
colorbar=dict(title="Days"),
575583
),
576584
))
@@ -583,7 +591,7 @@ def generate_directory_commit_statistic_treemaps(
583591
create_treemap_commit_statistics_settings(git_commit_days_since_last_file_modification_per_directory),
584592
marker=dict(
585593
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
586-
colors=git_commit_days_since_last_file_modification_per_directory["daysSinceLastModification_rank"],
594+
colors=plotly_values(git_commit_days_since_last_file_modification_per_directory["daysSinceLastModification_rank"]),
587595
colorbar=dict(title="Rank"),
588596
),
589597
))
@@ -633,7 +641,7 @@ def generate_cochange_treemaps(
633641
create_treemap_commit_statistics_settings(data_to_display),
634642
marker=dict(
635643
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
636-
colors=data_to_display["pairwiseChangeCommitCount_limited"],
644+
colors=plotly_values(data_to_display["pairwiseChangeCommitCount_limited"]),
637645
colorbar=dict(title="Co-Changes"),
638646
),
639647
))
@@ -646,7 +654,7 @@ def generate_cochange_treemaps(
646654
create_treemap_commit_statistics_settings(data_to_display),
647655
marker=dict(
648656
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
649-
colors=data_to_display["pairwiseChangeMaxLift_limited"],
657+
colors=plotly_values(data_to_display["pairwiseChangeMaxLift_limited"]),
650658
colorbar=dict(title="Co-Change Lift"),
651659
),
652660
))
@@ -662,7 +670,7 @@ def generate_cochange_treemaps(
662670
create_treemap_commit_statistics_settings(data_to_display),
663671
marker=dict(
664672
**PLOTLY_TREEMAP_MARKER_BASE_COLORSCALE,
665-
colors=data_to_display["pairwiseChangeAverageLift_limited"],
673+
colors=plotly_values(data_to_display["pairwiseChangeAverageLift_limited"]),
666674
colorbar=dict(title="Co-Change Lift"),
667675
),
668676
))
@@ -685,8 +693,8 @@ def generate_files_per_commit_bar_chart(
685693
print(f"{SCRIPT_NAME}: Skipping files-per-commit bar chart — no data")
686694
return
687695
figure = plotly_graph_objects.Figure(plotly_graph_objects.Bar(
688-
x=git_file_count_per_commit["filesPerCommit"].head(30),
689-
y=git_file_count_per_commit["commitCount"].head(30),
696+
x=plotly_values(git_file_count_per_commit["filesPerCommit"].head(30)),
697+
y=plotly_values(git_file_count_per_commit["commitCount"].head(30)),
690698
))
691699
figure.update_layout(
692700
**PLOTLY_MAIN_LAYOUT_BASE_SETTINGS,

0 commit comments

Comments
 (0)