<jupyter_start><jupyter_text>Landuse is not a great resource, but there is a clear link between taxa and use.<jupyter_code>brr = joined[joined.KOD == 'BRR']
counts = brr.cluster.value_counts(normalize=True)

sns.set(context="paper", style="ticks", rc={'patch.force_edgecolor': False})
fig, ax = plt.subplots(figsize=(10, 5))
sns.barplot(ax=ax, x=counts.index, y=counts, order=counts.index, palette=symbology)
sns.despine(offset=10)
plt.ylabel('count')
plt.xlabel('cluster')
bd = joined[joined.KOD == 'BD']
counts = bd.cluster.value_counts(normalize=True)

sns.set(context="paper", style="ticks", rc={'patch.force_edgecolor': False})
fig, ax = plt.subplots(figsize=(10, 5))
sns.barplot(ax=ax, x=counts.index, y=counts, order=counts.index, palette=symbology)
sns.despine(offset=10)
plt.ylabel('count')
plt.xlabel('cluster')
s = joined[joined.KOD == 'PND']

counts = s.cluster.value_counts(normalize=True)

sns.set(context="paper", style="ticks", rc={'patch.force_edgecolor': False})
fig, ax = plt.subplots(figsize=(10, 5))
sns.barplot(ax=ax, x=counts.index, y=counts, order=counts.index, palette=symbology)
sns.despine(offset=10)
plt.ylabel('count')
plt.xlabel('cluster')
buildings = gpd.read_file('/Users/martin/Dropbox/Academia/Data/Geo/Prague/Clustering/geometry.gpkg', layer='buildings')
buildings = buildings.merge(joined, on='uID', how='left')<jupyter_output><empty_output><jupyter_text>get the most frequent use within 3 steps<jupyter_code>import libpysal

from tqdm import tqdm
spatial_weights = libpysal.io.open('/Users/martin/Dropbox/Academia/Data/Geo/Prague/Clustering/queen3_uID.gal', 'r').read()
spatial_weights.neighbors = {int(k): [int(i) for i in v] for k, v in spatial_weights.neighbors.items()}
buildings = buildings.set_index('uID')
results = []
for index, row in tqdm(buildings.iterrows(), total=buildings.shape[0]):
    try:
        neighbours = spatial_weights.neighbors[index].copy()
        neighbours.append(index)

        uses = buildings.loc[neighbours]['KOD']
        results.append(uses.value_counts().index[0])
    except:
        results.append(None)
buildings['land_use_3'] = results    
buildings['land_use_3'].to_csv('/Users/martin/Dropbox/Academia/Data/Geo/Prague/Validation/land_use_interpolated.csv')
interp = pd.read_csv('/Users/martin/Dropbox/Academia/Data/Geo/Prague/Validation/land_use_interpolated.csv', index_col=0)
interp
buildings['land_use_3'] = interp
counts = buildings.land_use_3.value_counts()
counts[counts>1400]
generalised = []
major = counts[counts>1400].index
for _, use in buildings.land_use_3.iteritems():
    if use in major:
        generalised.append(use)
    else:
        generalised.append('other')
buildings['lu_gen'] = generalised
import numpy as np

def show_values_on_bars(axs):
    def _show_on_single_plot(ax):        
        for p in ax.patches:
            _x = p.get_x() + p.get_width() / 2
            _y = p.get_y() + p.get_height() + 0.02
            value = '{:.2f}'.format(p.get_height())
            ax.text(_x, _y, value, ha="center") 

    if isinstance(axs, np.ndarray):
        for idx, ax in np.ndenumerate(axs):
            _show_on_single_plot(ax)
    else:
        _show_on_single_plot(axs)

colors = [(257, 71, 27), (98, 93, 78), (14, 79, 58),
  (75, 90, 85), (347, 72, 60), (246, 79, 60)]
pal = [husl.husl_to_hex(*color) for color in colors]
# save all clusters
for cl in range(20):
    data = buildings.loc[buildings['cluster'].isin([cl])]['lu_gen'].value_counts(sort=False, normalize=True)

    sns.set(context="paper", style="ticks", rc={'patch.force_edgecolor': False})
    fig, ax = plt.subplots(figsize=(10, 5))
    sns.barplot(ax=ax, x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'],
                palette=pal)
    sns.despine(offset=10)
    ax.set_xticklabels(['Multi-family housing','Single-family housing','Villas','Industry small','Industry large','Other'])
    plt.ylabel('frequency')
    plt.xlabel('land use category')
    plt.ylim(0, 1)
    show_values_on_bars(ax)
    for ext in ['pdf', 'png']:
        plt.savefig('figures/PRG_cluster_' + str(cl) + '_landuse.' + ext, bbox_inches='tight')
    plt.close()
fig, ax = plt.subplots(2, 2, figsize=(14, 10))
labels = ['Multi-family','Single-family','Villas','Industry small','Industry large','Other']

data = buildings.loc[buildings['cluster'].isin([11, 15, 5])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[0, 0], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'],
                palette=pal)
sns.despine(offset=10)
ax[0,0].set_ylabel('frequency')
ax[0,0].set_title('compact city')
ax[0,0].set_ylim(0, 1)
show_values_on_bars(ax[0, 0])
ax[0,0].set_xticklabels(labels, rotation=45)

data = buildings.loc[buildings['cluster'].isin([3, 0, 8, 9, 13, 17])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[0, 1], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], palette=pal)
sns.despine(offset=10)
ax[0,1].set_ylabel('frequency')
ax[0,1].set_title('low-rise city')
ax[0,1].set_xticklabels(labels)
ax[0,1].set_ylim(0, 1)
show_values_on_bars(ax[0, 1])

data = buildings.loc[buildings['cluster'].isin([1, 19])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[1, 0], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], palette=pal)
sns.despine(offset=10)
ax[1,0].set_ylabel('frequency')
ax[1,0].set_xticklabels(labels)

ax[1,0].set_title('industrial city')
ax[1,0].set_ylim(0, 1)
show_values_on_bars(ax[1, 0])

data = buildings.loc[buildings['cluster'].isin([12, 14, 2, 10])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[1, 1], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], palette=pal)
sns.despine(offset=10)
ax[1,1].set_ylabel('frequency')
ax[1,1].set_xticklabels(labels)
ax[1,1].set_title('heterogenous dense city branch')
ax[1,1].set_ylim(0, 1)
show_values_on_bars(ax[1, 1])

plt.tight_layout()
plt.savefig('figures/PRG_branch_landuse_subplot.pdf')
fig, ax = plt.subplots(2, 2, figsize=(14, 10))
labels = ['Multi-family','Single-family','Villas','Industry small','Industry large','Other']

data = buildings.loc[buildings['cluster'].isin([11])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[0, 0], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'],
                palette=pal)
sns.despine(offset=10)
ax[0,0].set_ylabel('frequency')
ax[0,0].set_title('cluster 11')
ax[0,0].set_ylim(0, 1)
show_values_on_bars(ax[0, 0])
ax[0,0].set_xticklabels(labels, rotation=45)

data = buildings.loc[buildings['cluster'].isin([5])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[0, 1], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], palette=pal)
sns.despine(offset=10)
ax[0,1].set_ylabel('frequency')
ax[0,1].set_title('cluster 5')
ax[0,1].set_xticklabels(labels)
ax[0,1].set_ylim(0, 1)
show_values_on_bars(ax[0, 1])

data = buildings.loc[buildings['cluster'].isin([12])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[1, 0], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], palette=pal)
sns.despine(offset=10)
ax[1,0].set_ylabel('frequency')
ax[1,0].set_xticklabels(labels)

ax[1,0].set_title('cluster 12')
ax[1,0].set_ylim(0, 1)
show_values_on_bars(ax[1, 0])

data = buildings.loc[buildings['cluster'].isin([13])]['lu_gen'].value_counts(sort=False, normalize=True)
sns.barplot(ax=ax[1, 1], x=data.index, y=data, order=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], palette=pal)
sns.despine(offset=10)
ax[1,1].set_ylabel('frequency')
ax[1,1].set_xticklabels(labels)
ax[1,1].set_title('cluster 13')
ax[1,1].set_ylim(0, 1)
show_values_on_bars(ax[1, 1])

plt.tight_layout()
plt.savefig('figures/PRG_cluster_landuse_subplot.pdf')
colors = [(257, 71, 27), (98, 93, 78), (14, 79, 58), (26, 0, 50),
          (75, 90, 85), (347, 72, 60), (246, 79, 60)]
color = (257, 71, 27)  # here for arrow, title, scalebar

# plotting
c = husl.husl_to_hex(*color)

cmap = ListedColormap([husl.husl_to_hex(*color) for color in colors])  
for cl in tqdm(range(20), total=20):

    # requires geopandas PR 1159
    sub = buildings[buildings.cluster == cl]
    bounds = sub.total_bounds
    ax = sub.plot('lu_gen', categorical=True, figsize=(30, 30), cmap=cmap, zorder=2,
                  categories=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'], legend=True,
                    legend_kwds=dict(loc='center right', frameon=False))
    buildings.cx[bounds[0]:bounds[2], bounds[1]:bounds[3]].plot('lu_gen',ax=ax, categorical=True, cmap=cmap,
                    alpha=.2, zorder=1,
                    categories=['BD', 'BRR', 'BRV', 'PND', 'PRR', 'other'])
    ax.set_axis_off()


    # add scalebar
    scalebar = ScaleBar(dx=1,
                        color=c,
                        location=1,
                        height_fraction=0.001,
                        #fixed_value=1000,
                        label='cluster {} and historical period'.format(cl),
                        label_loc='bottom'
                        )
    ax.add_artist(scalebar)

    # add arrow
    north_arrow(plt.gcf(), ax, -7.5, legend_size=(.04,.04), outline=1, edgecolor=c, facecolor=c)
    for ext in ['pdf', 'png']:
        plt.savefig('figures/PRG_cluster_{}_landuse_map.'.format(cl) + ext, bbox_inches='tight')
    plt.close()<jupyter_output><empty_output><jupyter_text>statistics<jupyter_code>buildings[['cluster', 'lu_gen']]
import scipy.stats as ss

def cramers_v(x, y):
    confusion_matrix = pd.crosstab(x,y)
    chi2 = ss.chi2_contingency(confusion_matrix)[0]
    n = confusion_matrix.sum().sum()
    phi2 = chi2/n
    r,k = confusion_matrix.shape
    phi2corr = max(0, phi2-((k-1)*(r-1))/(n-1))
    rcorr = r-((r-1)**2)/(n-1)
    kcorr = k-((k-1)**2)/(n-1)
    return np.sqrt(phi2corr/min((kcorr-1),(rcorr-1)))
cramers_v(buildings.cluster, buildings.lu_gen)<jupyter_output><empty_output><jupyter_text>The resulting value of 0.5 indicates moderate to high relationship between clustering and generalised land use.  Chi-square test of independence of variables in a contingency table<jupyter_code>confusion_matrix = pd.crosstab(buildings.cluster, buildings.lu_gen)
chi, p, dof, exp = ss.chi2_contingency(confusion_matrix)
chi
p
dof<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19008825526137135, 'fraction_numerical': 0.03656289399670255, 'mean_word_length': 4.998836532867946, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '560474', 'license': 'MIT', 'n_tokens_mistral': 4331, 'n_tokens_neox': 4023, 'n_words': 675, 'path': 'source/notebooks/chapter78/Chapter 8 - Comparison of taxa and land use.ipynb', 'repo_name': 'martinfleis/phd-thesis', 'size': '10311'}