# gust99-glitch/capstone_mortgage-project architecture

> Architecture diagram of the GitHub repository gust99-glitch/capstone_mortgage-project, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gust99-glitch/capstone_mortgage-project
- Repository: https://github.com/gust99-glitch/capstone_mortgage-project

## Overview

1. This repository is a Python data pipeline and analysis toolkit for HMDA mortgage data, focused on North Carolina from 2019–2024. The core architecture is a staged ETL flow: fetch raw public data, standardize it, apply stricter cleaning, then run analysis/modeling on the resulting dataset.

2. The ingestion boundary is `mortgage.py`. It connects to the FFIEC HMDA Data Browser API, downloads yearly CSVs, streams them with retry behavior, and keeps only a curated set of loan, borrower, geography, and census-tract columns. This script is the main bridge from external data service to local storage.

3. The first persisted layer is per-year cleaned data plus a combined annual file under `data_hmda_nc/` (gitignored). The combined dataset is the handoff point between raw acquisition and downstream analytics. `merge_datasets.py` likely supports or duplicates the combine step, but the repo’s documented pipeline treats this as part of the ingestion/standardization layer.

4. `data.py` is the final data-conditioning stage. It reads the combined HMDA dataset and removes records that are not suitable for mortgage analysis: business/commercial loans, invalid demographic placeholders, invalid debt-to-income or conforming-limit values, and rows missing required fields. It writes the analysis-ready output to `cleaned_data/nc_2019_2024_cleaned.csv`.

5. The cleaned dataset is the central contract for the rest of the repository. Anything downstream should be treated as read-only consumers of that file, which keeps the boundary between data preparation and analysis fairly clean.

6. Several scripts are analysis/visualization consumers of that dataset, including `LoanHeatmap.py`, `LoanvsInterestMap.py`, and `CompaniesHeapmapNC.py`. These appear to generate exploratory charts or heatmaps, likely saved into the `charts/` directory or related output folders.

7. `action_taken_by_year_outputs/` and `action_taken_distribution.csv` are analysis artifacts rather than source code. They suggest a reporting layer focused on HMDA action outcomes over time, with yearly breakdowns and an aggregate distribution.

8. There is a separate modeling layer made up of `lasso_selector.py`, `rf_model.py`, and `xgboost_model.py`, with `rankings.py` and `denied.py` likely supporting feature ranking, lender comparisons, or denial analysis. Architecturally, this is an experimental ML tier built on top of the cleaned dataset, not part of the ingestion pipeline.

9. `app.py` and `main.py` are top-level entrypoints, but the repo is not organized like a single monolithic application. It looks more like a collection of runnable scripts for pipeline execution, plotting, and modeling, with each script owning one task.

10. Operationally, the repo is a local Python project managed with `pyproject.toml`, `.python-version`, and `uv.lock`, and it includes a `workspace.ipynb` notebook for ad hoc exploration. The key runtime dependency boundary is the FFIEC API; after that, everything is file-based CSV processing within the repository.

11. For an architecture diagram, the main flow is: FFIEC HMDA API → `mortgage.py` → yearly cleaned CSVs / combined NC dataset → `data.py` → final cleaned dataset → visualization scripts, reporting outputs, and ML experiments. The main storage boundaries are the two gitignored data directories plus chart/output folders.

Main entry points: [`mortgage.py`](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/mortgage.py) (mortgage), [`data.py`](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/data.py) (data prep), [`pyproject.toml`](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/pyproject.toml) (uv env), [`LoanHeatmap.py`](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/LoanHeatmap.py) (loan heatmap)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_ingest["Ingestion"]
  node_n_api[("FFIEC API<br/>external data source")]
  node_n_mortgage["mortgage<br/>ETL fetcher<br/>[mortgage.py]"]
end

subgraph group_g_clean["Data prep"]
  node_n_merge["merge data<br/>combine step<br/>[merge_datasets.py]"]
  node_n_raw_store[("NC data<br/>staged CSVs")]
  node_n_data["data prep<br/>cleaning stage<br/>[data.py]"]
  node_n_cleaned[("cleaned data<br/>analysis-ready CSV")]
end

subgraph group_g_analysis["Analysis"]
  node_n_heatmap["loan heatmap<br/>viz script<br/>[LoanHeatmap.py]"]
  node_n_rate_map["interest map<br/>viz script"]
  node_n_companies["company heatmap<br/>viz script"]
  node_n_action_csv["action outputs<br/>report artifacts"]
  node_n_action_dist["action dist<br/>summary artifact"]
end

subgraph group_g_ml["Modeling"]
  node_n_lasso["lasso selector<br/>feature selection<br/>[lasso_selector.py]"]
  node_n_rf["random forest<br/>model script<br/>[rf_model.py]"]
  node_n_xgb["xgboost model<br/>model script<br/>[xgboost_model.py]"]
  node_n_rank["rankings<br/>analysis helper<br/>[rankings.py]"]
  node_n_denied["denied analysis<br/>analysis helper<br/>[denied.py]"]
end

subgraph group_g_runtime["Project runtime"]
  node_n_app["app entry<br/>entrypoint<br/>[app.py]"]
  node_n_main["main entry<br/>entrypoint<br/>[main.py]"]
  node_n_env["uv env<br/>project config<br/>[pyproject.toml]"]
end

node_n_api -->|"HMDA download"| node_n_mortgage
node_n_mortgage -->|"yearly CSVs"| node_n_raw_store
node_n_merge -->|"combine"| node_n_raw_store
node_n_raw_store -->|"combined HMDA"| node_n_data
node_n_data -->|"filtered output"| node_n_cleaned
node_n_cleaned -->|"read-only input"| node_n_heatmap
node_n_cleaned -->|"read-only input"| node_n_rate_map
node_n_cleaned -->|"read-only input"| node_n_companies
node_n_cleaned -->|"train data"| node_n_lasso
node_n_cleaned -->|"train data"| node_n_rf
node_n_cleaned -->|"train data"| node_n_xgb
node_n_cleaned -->|"analysis input"| node_n_rank
node_n_cleaned -->|"analysis input"| node_n_denied
node_n_heatmap -->|"exports"| node_n_action_csv
node_n_rate_map -->|"summaries"| node_n_action_dist
node_n_app -->|"run pipeline"| node_n_mortgage
node_n_main -->|"run prep"| node_n_data
node_n_env -.->|"runtime"| node_n_app
node_n_env -.->|"runtime"| node_n_main

click node_n_mortgage "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/mortgage.py"
click node_n_merge "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/merge_datasets.py"
click node_n_data "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/data.py"
click node_n_heatmap "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/LoanHeatmap.py"
click node_n_rate_map "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/LoanvsInterestMap.py"
click node_n_companies "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/CompaniesHeapmapNC.py"
click node_n_action_csv "https://github.com/gust99-glitch/capstone_mortgage-project/tree/main/action_taken_by_year_outputs"
click node_n_action_dist "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/action_taken_distribution.csv"
click node_n_lasso "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/lasso_selector.py"
click node_n_rf "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/rf_model.py"
click node_n_xgb "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/xgboost_model.py"
click node_n_rank "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/rankings.py"
click node_n_denied "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/denied.py"
click node_n_app "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/app.py"
click node_n_main "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/main.py"
click node_n_env "https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_api,node_n_mortgage toneBlue
class node_n_merge,node_n_raw_store,node_n_data,node_n_cleaned toneAmber
class node_n_heatmap,node_n_rate_map,node_n_companies,node_n_action_csv,node_n_action_dist toneMint
class node_n_lasso,node_n_rf,node_n_xgb,node_n_rank,node_n_denied toneRose
class node_n_app,node_n_main,node_n_env toneIndigo
```

## Components

### Ingestion

Fetches HMDA data and builds the raw/annual working dataset.

- **FFIEC API**: external data source · Public HMDA Data Browser API used as the upstream data source.
- [mortgage](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/mortgage.py): ETL fetcher · Downloads yearly HMDA CSVs, streams them with retries, and selects curated columns. · `mortgage.py`

### Data prep

Combines and filters HMDA records into analysis-ready CSVs.

- [merge data](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/merge_datasets.py): combine step · Supports or duplicates the annual dataset merge/aggregation step. · `merge_datasets.py`
- **NC data**: staged CSVs · Gitignored per-year cleaned files and combined North Carolina HMDA dataset.
- [data prep](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/data.py): cleaning stage · Removes invalid loans and placeholder fields, producing the final cleaned dataset. · `data.py`
- **cleaned data**: analysis-ready CSV · Final dataset consumed by analysis and modeling scripts.

### Analysis

Exploratory charts, summaries, and reporting outputs from the cleaned dataset.

- [loan heatmap](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/LoanHeatmap.py): viz script · Generates exploratory loan distribution heatmaps. · `LoanHeatmap.py`
- [interest map](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/LoanvsInterestMap.py): viz script · Maps loans against interest-rate related views. · `LoanvsInterestMap.py`
- [company heatmap](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/CompaniesHeapmapNC.py): viz script · Produces lender/company-focused heatmaps for North Carolina. · `CompaniesHeapmapNC.py`
- [action outputs](https://github.com/gust99-glitch/capstone_mortgage-project/tree/main/action_taken_by_year_outputs): report artifacts · Yearly and aggregate action-taken distribution CSV outputs. · `action_taken_by_year_outputs`
- [action dist](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/action_taken_distribution.csv): summary artifact · Aggregate action-taken distribution summary file. · `action_taken_distribution.csv`

### Modeling

Experimental feature ranking and predictive modeling scripts.

- [lasso selector](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/lasso_selector.py): feature selection · Linear modeling script for selecting informative features. · `lasso_selector.py`
- [random forest](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/rf_model.py): model script · Tree-based experimental model built on the cleaned dataset. · `rf_model.py`
- [xgboost model](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/xgboost_model.py): model script · Gradient-boosted experimental model built on the cleaned dataset. · `xgboost_model.py`
- [rankings](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/rankings.py): analysis helper · Supports feature or lender ranking workflows. · `rankings.py`
- [denied analysis](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/denied.py): analysis helper · Supports denial-focused comparisons or reporting. · `denied.py`

### Project runtime

Repository-level entrypoints and local execution environment.

- [app entry](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/app.py): entrypoint · Top-level runnable script for one project workflow. · `app.py`
- [main entry](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/main.py): entrypoint · Top-level runnable script for one project workflow. · `main.py`
- [uv env](https://github.com/gust99-glitch/capstone_mortgage-project/blob/main/pyproject.toml): project config · Local Python project/runtime configuration managed by uv and pyproject metadata. · `pyproject.toml`

## Connections

- FFIEC API → mortgage: HMDA download · Mortgage ingestion pulls raw CSVs from the FFIEC service.
- mortgage → NC data: yearly CSVs · Fetched and curated data is written into the staged North Carolina dataset area.
- merge data → NC data: combine · Combines yearly data into a shared intermediate dataset.
- NC data → data prep: combined HMDA · Final conditioning stage reads the combined annual dataset.
- data prep → cleaned data: filtered output · Writes the analysis-ready cleaned CSV.
- cleaned data → loan heatmap: read-only input · Visualization scripts consume the cleaned dataset.
- cleaned data → interest map: read-only input · Visualization scripts consume the cleaned dataset.
- cleaned data → company heatmap: read-only input · Visualization scripts consume the cleaned dataset.
- cleaned data → lasso selector: train data · Modeling scripts build on the cleaned dataset.
- cleaned data → random forest: train data · Modeling scripts build on the cleaned dataset.
- cleaned data → xgboost model: train data · Modeling scripts build on the cleaned dataset.
- cleaned data → rankings: analysis input · Ranking utilities operate on the cleaned dataset or derived features.
- cleaned data → denied analysis: analysis input · Denial analysis operates on the cleaned dataset or derived features.
- loan heatmap → action outputs: exports · Some analyses write yearly distribution artifacts.
- interest map → action dist: summaries · Reporting layer produces aggregate action distribution outputs.
- app entry → mortgage: run pipeline · Top-level entrypoint can launch the ingestion workflow.
- main entry → data prep: run prep · Top-level entrypoint can launch the data-conditioning workflow.
- uv env → app entry: runtime · Project metadata and local environment support script execution.
- uv env → main entry: runtime · Project metadata and local environment support script execution.
