|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "fd2d413a-666f-49d4-b86c-7d6898a496a5", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import pandas as pd\n", |
| 11 | + "import numpy as np\n", |
| 12 | + "import json\n", |
| 13 | + "import re, pytz, os, requests, sys\n", |
| 14 | + "from pathlib import Path\n", |
| 15 | + "from datetime import datetime\n", |
| 16 | + "import sys\n", |
| 17 | + "sys.path.append(\"/workspaces/service-data\")\n", |
| 18 | + "\n", |
| 19 | + "from src.clean import clean_percentage, clean_fiscal_yr, normalize_string, standardize_column_names\n", |
| 20 | + "from src.load import load_csv\n", |
| 21 | + "from src.export import export_to_csv\n", |
| 22 | + "from src.merge import merge_si, merge_ss\n", |
| 23 | + "from src.utils import dept_list, program_list\n", |
| 24 | + "from main import get_config\n", |
| 25 | + "\n", |
| 26 | + "import pandas as pd\n", |
| 27 | + "import numpy as np\n", |
| 28 | + "import pytz\n", |
| 29 | + "from pathlib import Path\n", |
| 30 | + "\n", |
| 31 | + "\n", |
| 32 | + "\n", |
| 33 | + "base_dir = Path.cwd()\n", |
| 34 | + "parent_dir = base_dir.parent\n", |
| 35 | + "config = get_config()\n" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 5, |
| 41 | + "id": "e4c339d6", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "\"\"\"Builds a structured data dictionary from a JSON file, processes nested data, \n", |
| 46 | + "renames columns, standardizes names, and exports to CSV.\"\"\"\n", |
| 47 | + "\n", |
| 48 | + "INPUT_DIR = config['input_dir']\n", |
| 49 | + "file_path = INPUT_DIR / 'service_data_dict.json'\n", |
| 50 | + "\n", |
| 51 | + "# Load JSON file into a dictionary\n", |
| 52 | + "with open(file_path, \"r\", encoding=\"utf-8\") as file:\n", |
| 53 | + " data = json.load(file)\n", |
| 54 | + "\n", |
| 55 | + "# Initial normalization of json file\n", |
| 56 | + "data_dict = pd.json_normalize(data)\n", |
| 57 | + "\n", |
| 58 | + "# Explode and normalize the 'resources' portion\n", |
| 59 | + "data_dict = data_dict.explode('resources').reset_index(drop=True)\n", |
| 60 | + "data_dict = pd.json_normalize(data_dict['resources'])\n", |
| 61 | + "\n", |
| 62 | + "# Explode the 'fields' portion\n", |
| 63 | + "data_dict = data_dict.explode('fields').reset_index(drop=True)\n", |
| 64 | + "\n", |
| 65 | + "# Tie the resource fields to the 'fields portion'\n", |
| 66 | + "data_dict_fields = pd.json_normalize(data_dict['fields'])\n", |
| 67 | + "data_dict = data_dict.merge(data_dict_fields, left_index=True, right_index=True)\n", |
| 68 | + "\n", |
| 69 | + "# List of field names and details about their type and requirements\n", |
| 70 | + "dd_field_names = data_dict.loc[:, ~data_dict.columns.str.startswith('choices.')].drop(columns=['fields'])\n", |
| 71 | + "\n", |
| 72 | + "# List of translated code labels for fields with restricted input choices\n", |
| 73 | + "dd_choices = data_dict.melt(\n", |
| 74 | + " id_vars = ['resource_name', 'title.en', 'title.fr','id','label.en', 'label.fr'], \n", |
| 75 | + " value_vars=[col for col in data_dict.columns if col.startswith('choices.')]\n", |
| 76 | + ")\n", |
| 77 | + "\n", |
| 78 | + "dd_choices.dropna(subset=['value'], inplace=True)\n", |
| 79 | + "\n", |
| 80 | + "dd_choices['code'] = dd_choices['variable'].str.split('.').str[1]\n", |
| 81 | + "dd_choices['en_fr'] = dd_choices['variable'].str.split('.').str[2]\n", |
| 82 | + "dd_choices = dd_choices.dropna(subset='en_fr')\n", |
| 83 | + "dd_choices = dd_choices.loc[dd_choices['en_fr'].isin(['en', 'fr'])]\n", |
| 84 | + "\n", |
| 85 | + "dd_choices = dd_choices.pivot(index=['resource_name', 'id', 'code'], columns='en_fr', values='value')\n", |
| 86 | + "dd_choices = dd_choices.reset_index()\n", |
| 87 | + "\n", |
| 88 | + "# Keep dd_choices tidy by removing program_id and splitting into its own file (dd_program)\n", |
| 89 | + "dd_program = dd_choices.loc[dd_choices['id'] == 'program_id']\n", |
| 90 | + "dd_choices = dd_choices.loc[dd_choices['id'] != 'program_id']\n", |
| 91 | + "\n", |
| 92 | + "# Standardize column names\n", |
| 93 | + "dd_field_names = standardize_column_names(dd_field_names)\n", |
| 94 | + "dd_program = standardize_column_names(dd_program)\n", |
| 95 | + "dd_choices = standardize_column_names(dd_choices)\n", |
| 96 | + "\n", |
| 97 | + "data_dictionary_file_dict = {\n", |
| 98 | + " 'dd_field_names': dd_field_names,\n", |
| 99 | + " 'dd_program': dd_program,\n", |
| 100 | + " 'dd_choices': dd_choices\n", |
| 101 | + "}\n" |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "metadata": { |
| 106 | + "kernelspec": { |
| 107 | + "display_name": "Python 3", |
| 108 | + "language": "python", |
| 109 | + "name": "python3" |
| 110 | + }, |
| 111 | + "language_info": { |
| 112 | + "codemirror_mode": { |
| 113 | + "name": "ipython", |
| 114 | + "version": 3 |
| 115 | + }, |
| 116 | + "file_extension": ".py", |
| 117 | + "mimetype": "text/x-python", |
| 118 | + "name": "python", |
| 119 | + "nbconvert_exporter": "python", |
| 120 | + "pygments_lexer": "ipython3", |
| 121 | + "version": "3.12.1" |
| 122 | + } |
| 123 | + }, |
| 124 | + "nbformat": 4, |
| 125 | + "nbformat_minor": 5 |
| 126 | +} |
0 commit comments