Repository navigation
feat: add score visualizations for career domains and skills #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import math | ||
| from typing import Any, cast | ||
|
|
||
| import altair as alt | ||
| import pandas as pd # type: ignore[import-untyped] | ||
|
|
||
| SCORE_MAX = 100.0 | ||
|
|
||
| BAND_HIGH = "High (75-100)" | ||
| BAND_MEDIUM = "Medium (50-74)" | ||
| BAND_LOW = "Low (0-49)" | ||
|
|
||
| BAND_ORDER: list[str] = [BAND_HIGH, BAND_MEDIUM, BAND_LOW] | ||
| BAND_COLORS: dict[str, str] = { | ||
| BAND_HIGH: "#0ca30c", | ||
| BAND_MEDIUM: "#fab219", | ||
| BAND_LOW: "#d03b3b", | ||
| } | ||
|
|
||
|
|
||
| def _coerce_score(value: Any) -> float | None: | ||
| if isinstance(value, bool): | ||
| return None | ||
| try: | ||
| score = float(value) | ||
| except (TypeError, ValueError): | ||
| return None | ||
| if math.isnan(score) or math.isinf(score): | ||
| return None | ||
| return min(max(score, 0.0), SCORE_MAX) | ||
|
|
||
|
|
||
| def score_band(score: float) -> str: | ||
| if score >= 75.0: | ||
| return BAND_HIGH | ||
| if score >= 50.0: | ||
| return BAND_MEDIUM | ||
| return BAND_LOW | ||
|
|
||
|
|
||
| def build_score_frame( | ||
| items: list[Any] | None, | ||
| label_key: str, | ||
| score_key: str = "score", | ||
| ) -> pd.DataFrame: | ||
| rows: list[dict[str, Any]] = [] | ||
| for item in items or []: | ||
| if not isinstance(item, dict): | ||
| continue | ||
| label = str(item.get(label_key) or "").strip() | ||
| score = _coerce_score(item.get(score_key)) | ||
| if not label or score is None: | ||
| continue | ||
| rows.append({"label": label, "score": score, "band": score_band(score)}) | ||
| frame = pd.DataFrame(rows, columns=["label", "score", "band"]) | ||
| return frame.sort_values("score", ascending=False, kind="stable").reset_index( | ||
| drop=True | ||
| ) | ||
|
|
||
|
|
||
| def _score_bar_chart(frame: pd.DataFrame, label_title: str) -> alt.Chart: | ||
| chart = ( | ||
| alt.Chart(frame) | ||
| .mark_bar(cornerRadiusEnd=4, height=18) | ||
| .encode( | ||
| x=alt.X( | ||
| "score:Q", | ||
| title="Score", | ||
| scale=alt.Scale(domain=[0, SCORE_MAX]), | ||
| ), | ||
| y=alt.Y("label:N", title=None, sort="-x"), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Give repeated labels separate chart rows. If two domain or competency entries have the same label, both map to one y-axis category. Vega-Lite stacks their colored bars by default. For example, scores of 90 and 30 can appear as a combined bar that exceeds the fixed 0–100 axis, rather than as two scores. Give each entry a unique chart category while retaining its original label in the tooltip. (vega.github.io)
|
||
| color=alt.Color( | ||
| "band:N", | ||
| title="Score band", | ||
| scale=alt.Scale( | ||
| domain=BAND_ORDER, | ||
| range=[BAND_COLORS[band] for band in BAND_ORDER], | ||
| ), | ||
| legend=alt.Legend(orient="bottom"), | ||
| ), | ||
| tooltip=[ | ||
| alt.Tooltip("label:N", title=label_title), | ||
| alt.Tooltip("score:Q", title="Score"), | ||
| alt.Tooltip("band:N", title="Band"), | ||
| ], | ||
| ) | ||
| ) | ||
| return cast(alt.Chart, chart) | ||
|
|
||
|
|
||
| def competency_scores_chart( | ||
| competency_scores: list[dict[str, Any]] | None, | ||
| ) -> alt.Chart | None: | ||
| frame = build_score_frame(competency_scores, "category") | ||
| if frame.empty: | ||
| return None | ||
| return _score_bar_chart(frame, "Competency") | ||
|
|
||
|
|
||
| def domain_scores_chart( | ||
| domain_scores: list[dict[str, Any]] | None, | ||
| ) -> alt.Chart | None: | ||
| frame = build_score_frame(domain_scores, "domain") | ||
| if frame.empty: | ||
| return None | ||
| return _score_bar_chart(frame, "Domain") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| from typing import Any | ||
|
|
||
| import altair as alt | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from levelup.app import display_competencies_tab, display_domains_tab | ||
| from levelup.charts import ( | ||
| BAND_HIGH, | ||
| BAND_LOW, | ||
| BAND_MEDIUM, | ||
| BAND_ORDER, | ||
| build_score_frame, | ||
| competency_scores_chart, | ||
| domain_scores_chart, | ||
| score_band, | ||
| ) | ||
|
|
||
| SAMPLE_DOMAINS: list[dict[str, Any]] = [ | ||
| {"domain": "Data Science", "score": 88, "justification": "Strong ML background"}, | ||
| {"domain": "Software Engineering", "score": 61, "justification": "Some backend"}, | ||
| {"domain": "Product Management", "score": 42, "justification": "Limited exposure"}, | ||
| ] | ||
|
|
||
| SAMPLE_COMPETENCIES: list[dict[str, Any]] = [ | ||
| {"category": "Core Skills", "score": 90, "strength": "X", "observation": "Y"}, | ||
| {"category": "Communication", "score": 55, "strength": "X", "observation": "Y"}, | ||
| {"category": "Leadership", "score": 30, "strength": "X", "observation": "Y"}, | ||
| ] | ||
|
|
||
|
|
||
| def test_score_band_boundaries() -> None: | ||
| assert score_band(0) == BAND_LOW | ||
| assert score_band(49.9) == BAND_LOW | ||
| assert score_band(50) == BAND_MEDIUM | ||
| assert score_band(74.9) == BAND_MEDIUM | ||
| assert score_band(75) == BAND_HIGH | ||
| assert score_band(100) == BAND_HIGH | ||
|
|
||
|
|
||
| def test_build_score_frame_sorts_descending() -> None: | ||
| frame = build_score_frame( | ||
| [ | ||
| {"domain": "B", "score": 40}, | ||
| {"domain": "A", "score": 90}, | ||
| {"domain": "C", "score": 70}, | ||
| ], | ||
| "domain", | ||
| ) | ||
| assert list(frame["label"]) == ["A", "C", "B"] | ||
| assert list(frame["score"]) == [90.0, 70.0, 40.0] | ||
| assert list(frame["band"]) == [BAND_HIGH, BAND_MEDIUM, BAND_LOW] | ||
|
|
||
|
|
||
| def test_build_score_frame_drops_invalid_entries() -> None: | ||
| frame = build_score_frame( | ||
| [ | ||
| {"domain": "Valid", "score": "75"}, | ||
| {"domain": "", "score": 80}, | ||
| {"domain": "NoScore"}, | ||
| {"domain": "BadScore", "score": "n/a"}, | ||
| {"domain": "BoolScore", "score": True}, | ||
| {"domain": "NoneScore", "score": None}, | ||
| "not-a-dict", | ||
| ], | ||
| "domain", | ||
| ) | ||
| assert list(frame["label"]) == ["Valid"] | ||
| assert list(frame["score"]) == [75.0] | ||
|
|
||
|
|
||
| def test_build_score_frame_clamps_out_of_range_scores() -> None: | ||
| frame = build_score_frame( | ||
| [ | ||
| {"domain": "TooHigh", "score": 150}, | ||
| {"domain": "TooLow", "score": -20}, | ||
| ], | ||
| "domain", | ||
| ) | ||
| assert list(frame["score"]) == [100.0, 0.0] | ||
|
|
||
|
|
||
| def test_build_score_frame_empty_inputs() -> None: | ||
| assert build_score_frame([], "domain").empty | ||
| assert build_score_frame(None, "domain").empty | ||
|
|
||
|
|
||
| def test_domain_scores_chart_returns_chart() -> None: | ||
| chart = domain_scores_chart(SAMPLE_DOMAINS) | ||
| assert isinstance(chart, alt.Chart) | ||
| spec = chart.to_dict() | ||
| assert spec["encoding"]["x"]["field"] == "score" | ||
| assert spec["encoding"]["x"]["scale"]["domain"] == [0, 100.0] | ||
| assert spec["encoding"]["y"]["field"] == "label" | ||
| assert spec["encoding"]["color"]["field"] == "band" | ||
| assert spec["encoding"]["color"]["scale"]["domain"] == BAND_ORDER | ||
|
|
||
|
|
||
| def test_domain_scores_chart_empty_returns_none() -> None: | ||
| assert domain_scores_chart([]) is None | ||
| assert domain_scores_chart(None) is None | ||
| assert domain_scores_chart([{"domain": "X", "score": "bad"}]) is None | ||
|
|
||
|
|
||
| def test_competency_scores_chart_returns_chart() -> None: | ||
| chart = competency_scores_chart(SAMPLE_COMPETENCIES) | ||
| assert isinstance(chart, alt.Chart) | ||
| frame = chart.data | ||
| assert list(frame["label"]) == ["Core Skills", "Communication", "Leadership"] | ||
| assert list(frame["band"]) == [BAND_HIGH, BAND_MEDIUM, BAND_LOW] | ||
|
|
||
|
|
||
| def test_competency_scores_chart_empty_returns_none() -> None: | ||
| assert competency_scores_chart([]) is None | ||
| assert competency_scores_chart(None) is None | ||
|
|
||
|
|
||
| def test_display_domains_tab_renders_chart(mocker: MockerFixture) -> None: | ||
| mock_st = mocker.patch("levelup.app.st") | ||
|
|
||
| display_domains_tab({"domain_scores": SAMPLE_DOMAINS}) | ||
| mock_st.altair_chart.assert_called_once() | ||
| mock_st.dataframe.assert_called_once() | ||
|
|
||
| mock_st.reset_mock() | ||
| display_domains_tab({}) | ||
| mock_st.altair_chart.assert_not_called() | ||
| mock_st.info.assert_called_once() | ||
|
|
||
|
|
||
| def test_display_competencies_tab_renders_chart(mocker: MockerFixture) -> None: | ||
| mock_st = mocker.patch("levelup.app.st") | ||
|
|
||
| display_competencies_tab({"competency_scores": SAMPLE_COMPETENCIES}) | ||
| mock_st.altair_chart.assert_called_once() | ||
| mock_st.dataframe.assert_called_once() | ||
|
|
||
| mock_st.reset_mock() | ||
| display_competencies_tab({"competency_scores": []}) | ||
| mock_st.altair_chart.assert_not_called() | ||
| mock_st.info.assert_called_once() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the band labels match fractional scores.
score_band(74.9)returnsBAND_MEDIUM, but the legend says “50-74”. The low band has the same mismatch at 49.9. Use bounds such as50–<75and0–<50so the legend describes the scores the chart displays.Proposed label change
Committable suggestion