diff --git a/analysis/collab_patterns_outcomes.ipynb b/analysis/collab_patterns_outcomes.ipynb new file mode 100644 index 00000000..c4298941 --- /dev/null +++ b/analysis/collab_patterns_outcomes.ipynb @@ -0,0 +1,267 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Analysis of Collaboration Patterns and Outcomes:\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/tmp/ipykernel_824605/3927317048.py:16: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", + " collab_analysis = pd.read_sql(\"\"\"\n", + "/tmp/ipykernel_824605/3927317048.py:32: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", + " temporal_analysis = pd.read_sql(\"\"\"\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " program_name project_count \\\n", + "0 Sustainable Agricultural Lands Conservation Pr... 96 \n", + "1 Water-Energy Efficiency 35 \n", + "2 Forest Health Program 147 \n", + "3 Low Carbon Transit Operations Program 770 \n", + "4 Low-Income Weatherization Program 51 \n", + "5 Climate Resilience Planning 74 \n", + "6 Climate Smart Agriculture 1318 \n", + "7 Coastal Resilience Planning 17 \n", + "8 Community Air Protection 222 \n", + "9 Community Assistance for Climate Equity Program 33 \n", + "10 Fire Prevention Program 600 \n", + "11 Fluorinated Gases Emission Reduction Incentives 15 \n", + "12 Food Production Investment Program 56 \n", + "13 Forest Carbon Plan Implementation 388 \n", + "14 Funding Agricultural Replacement Measures for ... 30 \n", + "15 Low-Carbon Economy Workforce 30 \n", + "16 Low-Carbon Fuels Production Program 4 \n", + "17 Low Carbon Transportation 129 \n", + "18 Prescribed Fire and Smoke Monitoring Program 51 \n", + "19 Regional Forest and Fire Capacity 7 \n", + "20 Renewable Energy for Agriculture Program 45 \n", + "21 Safe and Affordable Drinking Water Fund 67 \n", + "22 SB 1383 Local Assistance Grant Program 467 \n", + "23 Training and Workforce Development Program 766 \n", + "24 Transformative Climate Communities 99 \n", + "25 Transit and Intercity Rail Capital Program 52 \n", + "26 Transition to a Carbon-Neutral Economy 2 \n", + "27 Urban and Community Forestry Program 115 \n", + "28 Urban Greening Program 88 \n", + "29 Waste Diversion 121 \n", + "30 Wetlands and Watershed Restoration 8 \n", + "31 Wildfire Response and Readiness 63 \n", + "32 Woodsmoke Reduction Program 85 \n", + "33 Active Transportation Program 3 \n", + "34 Workforce Training and Development 14 \n", + "35 Affordable Housing and Sustainable Communities... 94 \n", + "36 Climate Adaptation and Resiliency Program 17 \n", + "37 Climate Change Research Program 22 \n", + "38 Climate Ready Program 11 \n", + "\n", + " avg_ghg_efficiency dac_rate avg_partners total_funding \n", + "0 0.072917 0.010417 0.250000 1.224242e+08 \n", + "1 0.066094 0.440761 0.000000 5.247043e+07 \n", + "2 0.027211 0.000000 0.000000 2.094100e+08 \n", + "3 0.002597 0.092208 0.127273 7.787979e+08 \n", + "4 0.001898 0.917273 0.000000 1.583588e+08 \n", + "5 0.000000 0.000000 0.000000 8.680291e+06 \n", + "6 0.000000 0.053111 0.028832 3.474239e+08 \n", + "7 0.000000 0.000000 0.000000 2.667413e+06 \n", + "8 0.000000 0.000000 0.000193 5.295232e+08 \n", + "9 0.000000 0.081081 0.135135 9.250430e+06 \n", + "10 0.000000 0.000000 0.003333 5.962741e+08 \n", + "11 0.000000 0.000000 0.000000 1.000001e+06 \n", + "12 0.000000 0.000000 0.142857 1.177915e+08 \n", + "13 0.000000 0.000000 0.002577 1.051691e+08 \n", + "14 0.000000 0.000000 0.000000 2.540456e+08 \n", + "15 0.000000 0.000000 0.000000 2.442836e+07 \n", + "16 0.000000 0.000000 0.000000 1.250000e+07 \n", + "17 0.000000 0.370603 0.004289 1.840180e+09 \n", + "18 0.000000 0.000000 0.000000 3.903695e+06 \n", + "19 0.000000 0.000000 0.000000 6.244191e+06 \n", + "20 0.000000 0.000000 0.000000 9.500000e+06 \n", + "21 0.000000 0.000000 0.356436 1.206498e+08 \n", + "22 0.000000 0.000000 0.025696 5.655802e+07 \n", + "23 0.000000 0.000000 0.084856 4.716136e+07 \n", + "24 0.000000 0.000000 0.000000 2.027377e+08 \n", + "25 0.000000 0.184397 0.170213 8.145985e+08 \n", + "26 0.000000 0.000000 0.000000 2.560940e+06 \n", + "27 0.000000 0.183369 0.000000 5.919797e+07 \n", + "28 0.000000 0.183908 0.000000 1.387790e+08 \n", + "29 0.000000 0.089844 0.074219 1.185714e+08 \n", + "30 0.000000 0.000000 0.000000 1.617130e+07 \n", + "31 0.000000 0.000000 0.000000 3.103736e+07 \n", + "32 0.000000 0.000000 0.000000 7.895909e+06 \n", + "33 0.000000 0.666667 0.000000 1.000000e+07 \n", + "34 0.000000 0.000000 0.000000 1.000000e+06 \n", + "35 0.000000 0.159574 0.021277 1.211578e+09 \n", + "36 0.000000 0.000000 0.058824 1.257480e+07 \n", + "37 0.000000 0.000000 0.136364 3.063090e+07 \n", + "38 0.000000 0.000000 0.454545 1.271824e+07 \n", + "\n", + "Temporal Trends Analysis:\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt\n", + "\n", + "# Create connection\n", + "conn = psycopg2.connect(\n", + " dbname='calif_equity',\n", + " user='postgres',\n", + " password='MandyLinkToby3',\n", + " host='192.168.0.74',\n", + " port=5432 \n", + ")\n", + "\n", + "# Collaboration patterns - modified to handle duplicates\n", + "print(\"Analysis of Collaboration Patterns and Outcomes:\")\n", + "collab_analysis = pd.read_sql(\"\"\"\n", + " SELECT \n", + " p.program_name,\n", + " COUNT(DISTINCT p.project_id) as project_count,\n", + " AVG(p.ghg_efficiency) as avg_ghg_efficiency,\n", + " AVG(CASE WHEN p.dac_benefit THEN 1 ELSE 0 END) as dac_rate,\n", + " AVG(p.partner_count) as avg_partners,\n", + " SUM(p.total_funding) as total_funding\n", + " FROM project_efficiency p\n", + " GROUP BY p.program_name\n", + " ORDER BY avg_ghg_efficiency DESC\n", + "\"\"\", conn)\n", + "print(collab_analysis)\n", + "\n", + "# Temporal analysis - modified to handle duplicates\n", + "print(\"\\nTemporal Trends Analysis:\")\n", + "temporal_analysis = pd.read_sql(\"\"\"\n", + " SELECT \n", + " EXTRACT(YEAR FROM cp.date_operational) as year,\n", + " COUNT(DISTINCT cp.project_id) as project_count,\n", + " AVG(pe.ghg_efficiency) as avg_ghg_efficiency,\n", + " AVG(CASE WHEN pe.dac_benefit THEN 1 ELSE 0 END) as dac_rate,\n", + " AVG(pe.partner_count) as avg_partners,\n", + " SUM(DISTINCT pe.total_funding) as total_funding\n", + " FROM project_efficiency pe\n", + " JOIN cci_projects cp ON pe.project_id = cp.project_id\n", + " WHERE cp.date_operational BETWEEN '2015-01-01' AND '2024-12-31'\n", + " GROUP BY EXTRACT(YEAR FROM cp.date_operational)\n", + " ORDER BY year\n", + "\"\"\", conn)\n", + "print(temporal_analysis)\n", + "\n", + "# Regional analysis - modified to handle duplicates\n", + "print(\"\\nRegional Efficiency Analysis:\")\n", + "regional_analysis = pd.read_sql(\"\"\"\n", + " WITH distinct_projects AS (\n", + " SELECT DISTINCT ON (pe.project_id)\n", + " pe.project_id,\n", + " pe.ghg_efficiency,\n", + " pe.dac_benefit,\n", + " pe.partner_count,\n", + " pe.total_funding,\n", + " cp.county\n", + " FROM project_efficiency pe\n", + " JOIN cci_projects cp ON pe.project_id = cp.project_id\n", + " WHERE cp.county NOT LIKE '%,%'\n", + " )\n", + " SELECT \n", + " CASE \n", + " WHEN county IN ('Los Angeles', 'San Francisco', 'Alameda', 'San Diego', 'Orange')\n", + " THEN 'Urban'\n", + " WHEN county IN ('Fresno', 'Kern', 'Kings', 'Madera', 'Merced', 'San Joaquin', 'Stanislaus', 'Tulare')\n", + " THEN 'Central Valley'\n", + " ELSE 'Other'\n", + " END as region_type,\n", + " COUNT(*) as project_count,\n", + " AVG(ghg_efficiency) as avg_ghg_efficiency,\n", + " AVG(CASE WHEN dac_benefit THEN 1 ELSE 0 END) as dac_rate,\n", + " AVG(partner_count) as avg_partners,\n", + " SUM(total_funding) as total_funding\n", + " FROM distinct_projects\n", + " GROUP BY \n", + " CASE \n", + " WHEN county IN ('Los Angeles', 'San Francisco', 'Alameda', 'San Diego', 'Orange')\n", + " THEN 'Urban'\n", + " WHEN county IN ('Fresno', 'Kern', 'Kings', 'Madera', 'Merced', 'San Joaquin', 'Stanislaus', 'Tulare')\n", + " THEN 'Central Valley'\n", + " ELSE 'Other'\n", + " END\n", + "\"\"\", conn)\n", + "print(regional_analysis)\n", + "\n", + "# Create visualizations\n", + "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 12))\n", + "\n", + "# 1. Temporal trends\n", + "if not temporal_analysis.empty:\n", + " temporal_analysis.plot(x='year', y=['avg_ghg_efficiency', 'dac_rate'], \n", + " kind='line', ax=ax1)\n", + " ax1.set_title('Efficiency and Equity Over Time')\n", + " ax1.set_ylabel('Rate')\n", + "\n", + "# 2. Project funding by region\n", + "if not regional_analysis.empty:\n", + " regional_analysis.plot(kind='bar', x='region_type', y='total_funding', \n", + " ax=ax2)\n", + " ax2.set_title('Total Funding by Region')\n", + " ax2.set_ylabel('Total Funding ($)')\n", + "\n", + "# 3. Program performance\n", + "if not collab_analysis.empty:\n", + " # Sort by efficiency and take top 10 for better visualization\n", + " top_programs = collab_analysis.nlargest(10, 'avg_ghg_efficiency')\n", + " top_programs.plot(kind='bar', x='program_name', \n", + " y=['avg_ghg_efficiency', 'dac_rate'],\n", + " ax=ax3)\n", + " ax3.set_title('Top 10 Programs by GHG Efficiency')\n", + " ax3.set_ylabel('Rate')\n", + " plt.setp(ax3.xaxis.get_majorticklabels(), rotation=45, ha='right')\n", + "\n", + "# 4. Partners vs Efficiency scatter\n", + "if not temporal_analysis.empty:\n", + " temporal_analysis.plot(kind='scatter', x='avg_partners', \n", + " y='avg_ghg_efficiency', ax=ax4)\n", + " ax4.set_title('Partnership Size vs Efficiency')\n", + " ax4.set_ylabel('GHG Efficiency')\n", + "\n", + "plt.tight_layout()\n", + "plt.show()\n", + "\n", + "# Close connection\n", + "conn.close()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}