Implement the missing code below - """ import pandas as pd from argparse import…
- User
Implement the missing code below -
""" import pandas as pd from argparse import ArgumentParser from datetime import datetime
DATE = datetime.now().date()
PATH2PROJ = "/home/esther/PycharmProjects/edut710" METADATA_PATH = f"{PATH2PROJ}/data/20250110-testimonies-monday.csv" GENDER_AGE_PATH = f"{PATH2PROJ}/data/year_of_birth_gender.csv"
META_COLS = {'איפה היית ב7/10': 'location', 'עד/ה': 'name'}
def incorporate_metadata(df: pd.DataFrame, path2topics, seg_method) -> pd.DataFrame: """ Incorporate metadata into the dataframe, including: topic_id, gender, age, location and organizations. :param path2topics: :param df: DataFrame with testimonies :return: DataFrame with metadata incorporated """ topic_df = pd.read_csv(path2topics, index_col=0)[['Testimony Identifier', 'text', 'most_probable_topic']] topic_df.rename(columns={'Testimony Identifier': 'tid', 'most_probable_topic': 'topic_id'}, inplace=True) topic_df.tid = topic_df.tid.apply(lambda x: x.split(".")[-1])
code# merge the topic mapping with the predictions df = pd.merge(df, topic_df, how='left', on=['tid', 'text']) # load the metadata meta_data = pd.read_csv(METADATA_PATH, index_col=0) # rename the columns to match the expected format meta_data.rename(columns=META_COLS, inplace=True) meta_data['tid'] = meta_data['Identifier'].apply(lambda x: x.split(".")[-1]) # merge the metadata with the predictions df = pd.merge(df, meta_data, how='left', on=['tid']) gender_age_df = pd.read_csv(GENDER_AGE_PATH, index_col=0) gender_age_df.rename(columns={"מגדר": "gender", "שנת לידה": "year_of_birth"}, inplace=True) gender_age_df['tid'] = gender_age_df['Testimony Identifier'].apply(lambda x: x.split(".")[-1]) df = pd.merge(df, gender_age_df, how='left', on=['tid']) # save df = df[['tid', 'tid_order', 'name', 'text', 'topic_id', 'gender', 'year_of_birth', 'location', ]] # drop any duplicate rows - according to tid and text df.drop_duplicates(subset=['tid', 'text'], inplace=True) df.to_csv(f"domestic_resilience_predictions_{seg_method}_with_metadata_{DATE}.csv") return df - Assistant
To complete your script, the visualizations function needs to be implemented. Here's a full implementation of visualizations(data) that creates:
A heatmap showing the count of each topic_id across year_of_birth.
A bar chart of topic counts, broken down by gender.
pythonCopyEditimport matplotlib.pyplot as plt import seaborn as sns import os
def visualizations(data): """ - create visualizations for the domestic resilience data - plot a heatmap of the topic ids as a function of year_of_birth - plot a bar chart of all topic ids and their counts, separate by gender :param data: DataFrame containing metadata and predictions :return: None """ # Ensure year_of_birth is numeric and drop rows with missing critical fields data = data.dropna(subset=['topic_id', 'year_of_birth', 'gender']) data['year_of_birth'] = data['year_of_birth'].astype(int) data['topic_id'] = data['topic_id'].astype(str)