Add career stage visualization
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python
|
||||
from pathlib import Path
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
ANALYSIS_ROOT = Path(__file__).resolve().parent
|
||||
RESULTS_CSV = ANALYSIS_ROOT / "career_stage_results.csv"
|
||||
PLOTS_DIR = ANALYSIS_ROOT / "plots"
|
||||
PLOTS_DIR.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
def load_data() -> pd.DataFrame:
|
||||
if not RESULTS_CSV.exists():
|
||||
raise FileNotFoundError(f"Missing results file: {RESULTS_CSV}")
|
||||
|
||||
df = pd.read_csv(RESULTS_CSV)
|
||||
numeric_columns = [
|
||||
"career_age",
|
||||
"active_span",
|
||||
"publication_count",
|
||||
"avg_publications_per_year",
|
||||
]
|
||||
for column in numeric_columns:
|
||||
df[column] = pd.to_numeric(df[column], errors="coerce")
|
||||
return df
|
||||
|
||||
|
||||
def save_plot(fig, filename: str) -> None:
|
||||
output_path = PLOTS_DIR / filename
|
||||
fig.tight_layout()
|
||||
fig.savefig(output_path, dpi=200)
|
||||
print(f"Saved plot: {output_path}")
|
||||
|
||||
|
||||
def career_age_by_status(df: pd.DataFrame) -> None:
|
||||
grouped = df.groupby("known_status")["career_age"].mean().sort_values()
|
||||
fig, ax = plt.subplots(figsize=(8, 5))
|
||||
grouped.plot(kind="bar", ax=ax, color="#4C72B0")
|
||||
ax.set_title("Average Career Age by Known Status")
|
||||
ax.set_xlabel("Known Status")
|
||||
ax.set_ylabel("Average Career Age (years)")
|
||||
save_plot(fig, "career_age_by_status.png")
|
||||
|
||||
|
||||
def publication_count_by_status(df: pd.DataFrame) -> None:
|
||||
grouped = df.groupby("known_status")["publication_count"].mean().sort_values()
|
||||
fig, ax = plt.subplots(figsize=(8, 5))
|
||||
grouped.plot(kind="bar", ax=ax, color="#DD8452")
|
||||
ax.set_title("Average Publication Count by Known Status")
|
||||
ax.set_xlabel("Known Status")
|
||||
ax.set_ylabel("Average Publication Count")
|
||||
save_plot(fig, "publication_count_by_status.png")
|
||||
|
||||
|
||||
def career_age_vs_publication_count(df: pd.DataFrame) -> None:
|
||||
fig, ax = plt.subplots(figsize=(8, 6))
|
||||
for status, group in df.groupby("known_status"):
|
||||
ax.scatter(
|
||||
group["career_age"],
|
||||
group["publication_count"],
|
||||
label=status,
|
||||
alpha=0.75,
|
||||
s=80,
|
||||
edgecolor="black",
|
||||
linewidth=0.5,
|
||||
)
|
||||
|
||||
ax.set_title("Career Age vs Publication Count")
|
||||
ax.set_xlabel("Career Age (years)")
|
||||
ax.set_ylabel("Publication Count")
|
||||
ax.legend(title="Known Status")
|
||||
save_plot(fig, "career_age_vs_publication_count.png")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
df = load_data()
|
||||
if df.empty:
|
||||
raise ValueError(f"No data found in {RESULTS_CSV}")
|
||||
|
||||
df = df.dropna(subset=["known_status"])
|
||||
if df.empty:
|
||||
raise ValueError("No rows with known_status in the results file.")
|
||||
|
||||
career_age_by_status(df)
|
||||
publication_count_by_status(df)
|
||||
career_age_vs_publication_count(df)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user