Skip to content

Commit

Permalink
Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
sjsrey committed Dec 29, 2024
1 parent d174843 commit aff0c27
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 192 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@

# (Optional) PySAL favicon
html_favicon = "_static/images/pysal_favicon.ico"
html_logo = '_static/images/pysal_logo.png'
icon_links_label = 'inequality'
html_logo = "_static/images/pysal_logo.png"
icon_links_label = "inequality"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand Down
162 changes: 102 additions & 60 deletions docs/user-guide/measure/gini.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -104,69 +104,95 @@
"\n",
"# we will use greedy from mapclassify\n",
"# states to ensure contiguous states are of a different color\n",
"sgdf = gdf.sort_values(by='NAME')\n",
"sgdf = gdf.sort_values(by=\"NAME\")\n",
"sgdf.reset_index(inplace=True)\n",
"sgdf['label'] = range(1, 33)\n",
"sgdf['greedy'] = mapclassify.greedy(sgdf)\n",
"sgdf[\"label\"] = range(1, 33)\n",
"sgdf[\"greedy\"] = mapclassify.greedy(sgdf)\n",
"\n",
"\n",
"font_size = 9\n",
"outside = [9, 29]\n",
"oc = [(-103, 17.5), (-95, 22.5)]\n",
"oe = [(-102.55, 17.49),(-95.5, 22.1)] \n",
"oe = [(-102.55, 17.49), (-95.5, 22.1)]\n",
"oinfo = zip(outside, oc)\n",
"\n",
"\n",
"from shapely.geometry import LineString\n",
"\n",
"\n",
"import matplotlib.pyplot as plt\n",
"sgdf['centroid'] = sgdf.centroid\n",
"\n",
"sgdf[\"centroid\"] = sgdf.centroid\n",
"ax = sgdf.plot(\n",
" figsize=(8, 12),\n",
" column=\"greedy\",\n",
" categorical=True,\n",
" cmap=\"Set3\",\n",
" #legend=True,\n",
" # legend=True,\n",
" edgecolor=\"w\",\n",
")\n",
"\n",
"\n",
"table = []\n",
"for idx, row in sgdf.iterrows():\n",
" centroid = row['centroid']\n",
" centroid = row[\"centroid\"]\n",
" table.append(f'{idx+1:2d} {row[\"NAME\"]}')\n",
" if idx+1 not in outside:\n",
" ax.text(centroid.x, centroid.y, str(idx+1), ha='center',\n",
" va='center', fontsize=font_size, color='black')\n",
" if idx + 1 not in outside:\n",
" ax.text(\n",
" centroid.x,\n",
" centroid.y,\n",
" str(idx + 1),\n",
" ha=\"center\",\n",
" va=\"center\",\n",
" fontsize=font_size,\n",
" color=\"black\",\n",
" )\n",
"\n",
"\n",
"i = 0\n",
"for out in oinfo:\n",
" idx, coords = out\n",
" ax.text(coords[0], coords[1], str(idx), ha='center',\n",
" va='center', fontsize=font_size, color='black')\n",
" ax.text(\n",
" coords[0],\n",
" coords[1],\n",
" str(idx),\n",
" ha=\"center\",\n",
" va=\"center\",\n",
" fontsize=font_size,\n",
" color=\"black\",\n",
" )\n",
" start_point = coords\n",
" end_point = sgdf.centroid[idx-1]\n",
" end_point = sgdf.centroid[idx - 1]\n",
"\n",
" \n",
" start_point = oe[i]\n",
" line = LineString([start_point, end_point])\n",
" \n",
" \n",
"\n",
" line_gdf = gpd.GeoSeries([line])\n",
" \n",
" \n",
" line_gdf.plot(ax=ax, color='red', linewidth=2)\n",
" i+=1\n",
"\n",
" line_gdf.plot(ax=ax, color=\"red\", linewidth=2)\n",
" i += 1\n",
"\n",
"for i, label in enumerate(table):\n",
" if i < 16:\n",
" ax.text(-120, 20-i*1, label, ha='left',\n",
" va='center', fontsize=font_size, color='black');\n",
" ax.text(\n",
" -120,\n",
" 20 - i * 1,\n",
" label,\n",
" ha=\"left\",\n",
" va=\"center\",\n",
" fontsize=font_size,\n",
" color=\"black\",\n",
" )\n",
" else:\n",
" ax.text(-110, 20-(i-16)*1, label, ha='left',\n",
" va='center', fontsize=font_size, color='black');\n",
" ax.text(\n",
" -110,\n",
" 20 - (i - 16) * 1,\n",
" label,\n",
" ha=\"left\",\n",
" va=\"center\",\n",
" fontsize=font_size,\n",
" color=\"black\",\n",
" )\n",
"ax.set_axis_off()"
]
},
Expand All @@ -193,13 +219,19 @@
"fig, axes = plt.subplots(1, 2, figsize=(8, 4)) # Two subplots in one row\n",
"\n",
"# Left column: Choropleth map\n",
"ax_map = gdf.plot(column=\"PCGDP1940\", k=5, scheme=\"Quantiles\", legend=True,\n",
" legend_kwds={'fmt': \"{:.0f}\"}, ax=axes[0])\n",
"ax_map = gdf.plot(\n",
" column=\"PCGDP1940\",\n",
" k=5,\n",
" scheme=\"Quantiles\",\n",
" legend=True,\n",
" legend_kwds={\"fmt\": \"{:.0f}\"},\n",
" ax=axes[0],\n",
")\n",
"ax_map.set_axis_off()\n",
"ax_map.set_title(\"PC GDP 1940\")\n",
"\n",
"# Right column: Kernel density plot\n",
"sns.kdeplot(data=gdf['PCGDP1940'], ax=axes[1], fill=True, bw_adjust=0.5)\n",
"sns.kdeplot(data=gdf[\"PCGDP1940\"], ax=axes[1], fill=True, bw_adjust=0.5)\n",
"axes[1].set_title(\"Kernel Density: PC GDP 1940\")\n",
"axes[1].set_xlabel(\"Per Capita GDP\")\n",
"axes[1].set_ylabel(\"Density\")\n",
Expand Down Expand Up @@ -257,7 +289,8 @@
"outputs": [],
"source": [
"from inequality.schutz import Schutz\n",
"s = Schutz(gdf, 'PCGDP1940')\n",
"\n",
"s = Schutz(gdf, \"PCGDP1940\")\n",
"s.plot()"
]
},
Expand All @@ -276,7 +309,7 @@
"metadata": {},
"outputs": [],
"source": [
"inequality.gini.Gini(gdf['PCGDP2000']).g"
"inequality.gini.Gini(gdf[\"PCGDP2000\"]).g"
]
},
{
Expand All @@ -285,7 +318,7 @@
"metadata": {},
"outputs": [],
"source": [
"s = Schutz(gdf, 'PCGDP2000')\n",
"s = Schutz(gdf, \"PCGDP2000\")\n",
"s.plot()"
]
},
Expand All @@ -307,7 +340,8 @@
"decades = range(1940, 2010, 10)\n",
"ginis = [inequality.gini.Gini(gdf[\"PCGDP%s\" % decade]).g for decade in decades]\n",
"import pandas as pd\n",
"ginis_df = pd.DataFrame(data=ginis, columns=['Gini'], index = list(decades))\n",
"\n",
"ginis_df = pd.DataFrame(data=ginis, columns=[\"Gini\"], index=list(decades))\n",
"ginis_df.plot();"
]
},
Expand All @@ -334,7 +368,7 @@
"outputs": [],
"source": [
"numpy.random.seed(12345)\n",
"gdf['PCGDP1940r'] = numpy.random.permutation(gdf.PCGDP1940)"
"gdf[\"PCGDP1940r\"] = numpy.random.permutation(gdf.PCGDP1940)"
]
},
{
Expand All @@ -350,26 +384,36 @@
"\n",
"fig, axes = plt.subplots(2, 2, figsize=(12, 10))\n",
"\n",
"gdf.plot(column='PCGDP1940', ax=axes[0, 0], legend=True, scheme='quantiles',k=4,cmap='viridis',\n",
" legend_kwds={\n",
" \"fmt\": \"{:.0f}\"}\n",
" )\n",
"axes[0, 0].set_title('PCGDP1940')\n",
"axes[0,0].axis('off')\n",
"\n",
"gdf.plot(column='PCGDP1940r', ax=axes[0, 1], legend=True, scheme='quantiles', k=4, cmap='viridis',\n",
" legend_kwds={\n",
" \"fmt\": \"{:.0f}\"}\n",
" )\n",
"axes[0, 1].set_title('PCGDP1940r')\n",
"axes[0,1].axis('off')\n",
"gdf.plot(\n",
" column=\"PCGDP1940\",\n",
" ax=axes[0, 0],\n",
" legend=True,\n",
" scheme=\"quantiles\",\n",
" k=4,\n",
" cmap=\"viridis\",\n",
" legend_kwds={\"fmt\": \"{:.0f}\"},\n",
")\n",
"axes[0, 0].set_title(\"PCGDP1940\")\n",
"axes[0, 0].axis(\"off\")\n",
"\n",
"gdf.plot(\n",
" column=\"PCGDP1940r\",\n",
" ax=axes[0, 1],\n",
" legend=True,\n",
" scheme=\"quantiles\",\n",
" k=4,\n",
" cmap=\"viridis\",\n",
" legend_kwds={\"fmt\": \"{:.0f}\"},\n",
")\n",
"axes[0, 1].set_title(\"PCGDP1940r\")\n",
"axes[0, 1].axis(\"off\")\n",
"\n",
"\n",
"sns.kdeplot(gdf['PCGDP1940'], ax=axes[1, 0], fill=True, color='blue')\n",
"axes[1, 0].set_title('Kernel Density of PCGDP1940')\n",
"sns.kdeplot(gdf[\"PCGDP1940\"], ax=axes[1, 0], fill=True, color=\"blue\")\n",
"axes[1, 0].set_title(\"Kernel Density of PCGDP1940\")\n",
"\n",
"sns.kdeplot(gdf['PCGDP1940r'], ax=axes[1, 1], fill=True, color='orange')\n",
"axes[1, 1].set_title('Kernel Density of PCGDP1940r')\n",
"sns.kdeplot(gdf[\"PCGDP1940r\"], ax=axes[1, 1], fill=True, color=\"orange\")\n",
"axes[1, 1].set_title(\"Kernel Density of PCGDP1940r\")\n",
"\n",
"plt.tight_layout()\n",
"\n",
Expand All @@ -382,7 +426,7 @@
"metadata": {},
"outputs": [],
"source": [
"inequality.gini.Gini(gdf['PCGDP1940']).g == inequality.gini.Gini(gdf['PCGDP1940r']).g"
"inequality.gini.Gini(gdf[\"PCGDP1940\"]).g == inequality.gini.Gini(gdf[\"PCGDP1940r\"]).g"
]
},
{
Expand Down Expand Up @@ -584,8 +628,8 @@
"\n",
"# Plot\n",
"plt.figure(figsize=(10, 6))\n",
"plt.plot(x_range, kde_values, label='Kernel Density Estimate')\n",
"plt.axvline(x=obs, color='red', linestyle='--', label=f'Value = {obs:.2f}')\n",
"plt.plot(x_range, kde_values, label=\"Kernel Density Estimate\")\n",
"plt.axvline(x=obs, color=\"red\", linestyle=\"--\", label=f\"Value = {obs:.2f}\")\n",
"plt.title(\"Gini Spatial Polarization Index 1940\")\n",
"plt.xlabel(\"Values\")\n",
"plt.ylabel(\"Density\")\n",
Expand Down Expand Up @@ -645,8 +689,8 @@
"\n",
"# Plot\n",
"plt.figure(figsize=(10, 6))\n",
"plt.plot(x_range, kde_values, label='Kernel Density Estimate')\n",
"plt.axvline(x=obs, color='red', linestyle='--', label=f'Value = {obs:.2f}')\n",
"plt.plot(x_range, kde_values, label=\"Kernel Density Estimate\")\n",
"plt.axvline(x=obs, color=\"red\", linestyle=\"--\", label=f\"Value = {obs:.2f}\")\n",
"plt.title(\"Gini Spatial Polarization Index 1940 (Random)\")\n",
"\n",
"plt.xlabel(\"Values\")\n",
Expand Down Expand Up @@ -701,8 +745,6 @@
"gsh.p_sim\n",
"\n",
"\n",
"\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy.stats import gaussian_kde\n",
Expand All @@ -720,8 +762,8 @@
"\n",
"# Plot\n",
"plt.figure(figsize=(10, 6))\n",
"plt.plot(x_range, kde_values, label='Kernel Density Estimate')\n",
"plt.axvline(x=obs, color='red', linestyle='--', label=f'Value = {obs}')\n",
"plt.plot(x_range, kde_values, label=\"Kernel Density Estimate\")\n",
"plt.axvline(x=obs, color=\"red\", linestyle=\"--\", label=f\"Value = {obs}\")\n",
"plt.title(\"Gini Spatial Polarization 1940 (Hanson)\")\n",
"plt.xlabel(\"Values\")\n",
"plt.ylabel(\"Density\")\n",
Expand Down Expand Up @@ -773,8 +815,8 @@
"\n",
"# Plot\n",
"plt.figure(figsize=(10, 6))\n",
"plt.plot(x_range, kde_values, label='Kernel Density')\n",
"plt.axvline(x=obs, color='red', linestyle='--', label=f'Value = {obs:.2f}')\n",
"plt.plot(x_range, kde_values, label=\"Kernel Density\")\n",
"plt.axvline(x=obs, color=\"red\", linestyle=\"--\", label=f\"Value = {obs:.2f}\")\n",
"plt.title(\"Gini Spatial Polarization 1940 (Hanson Random)\")\n",
"\n",
"plt.xlabel(\"Values\")\n",
Expand Down
20 changes: 11 additions & 9 deletions docs/user-guide/measure/theil.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
}
],
"source": [
"ax = gdf.plot(edgecolor='grey')\n",
"ax = gdf.plot(edgecolor=\"grey\")\n",
"ax.set_axis_off()"
]
},
Expand Down Expand Up @@ -432,8 +432,9 @@
}
],
"source": [
"ax = gdf.plot(column=\"PCGDP1960\", k=5, scheme=\"Quantiles\", \n",
" edgecolor='grey',legend=True)\n",
"ax = gdf.plot(\n",
" column=\"PCGDP1960\", k=5, scheme=\"Quantiles\", edgecolor=\"grey\", legend=True\n",
")\n",
"ax.set_axis_off()\n",
"ax.set_title(\"PC GDP 1960\");\n",
"# plt.savefig(\"1940.png\")"
Expand Down Expand Up @@ -549,8 +550,7 @@
}
],
"source": [
"ax = gdf.plot(column=\"HANSON98\", categorical=True,\n",
" edgecolor='grey')\n",
"ax = gdf.plot(column=\"HANSON98\", categorical=True, edgecolor=\"grey\")\n",
"ax.set_title(\"Regions\")\n",
"ax.set_axis_off()\n",
"# plt.savefig(\"regions.png\")"
Expand All @@ -568,7 +568,7 @@
"outputs": [],
"source": [
"numpy.random.seed(12345)\n",
"ts = inequality.theil.TheilD(gdf['PCGDP1960'], regimes)"
"ts = inequality.theil.TheilD(gdf[\"PCGDP1960\"], regimes)"
]
},
{
Expand Down Expand Up @@ -689,6 +689,7 @@
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"np.random.seed(10)"
]
},
Expand All @@ -698,7 +699,7 @@
"metadata": {},
"outputs": [],
"source": [
"ts = inequality.theil.TheilDSim(gdf['PCGDP1960'], regimes, permutations=999)"
"ts = inequality.theil.TheilDSim(gdf[\"PCGDP1960\"], regimes, permutations=999)"
]
},
{
Expand Down Expand Up @@ -760,11 +761,12 @@
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"kdeplot = sbn.kdeplot(ts.bg, fill=False, legend=False)\n",
"x_vals = kdeplot.lines[0].get_xdata()\n",
"y_vals = kdeplot.lines[0].get_ydata()\n",
"plt.fill_between(x_vals, y_vals, where=(x_vals > ts.bg[0]), color='red')\n",
"plt.xlabel('Between Regions Inequality 1960');"
"plt.fill_between(x_vals, y_vals, where=(x_vals > ts.bg[0]), color=\"red\")\n",
"plt.xlabel(\"Between Regions Inequality 1960\");"
]
},
{
Expand Down
Loading

0 comments on commit aff0c27

Please sign in to comment.