%%capture

import pysal.lib
from pysal.explore import segregation
import geopandas as gpd

"""
Explanation: Multigroup aspatial indexes of segregation
This is an example notebook of functionalities for multigroup aspatial indexes of the segregation module. Firstly, we need to import the packages we need:
End of explanation
"""


input_df = gpd.read_file(pysal.lib.examples.get_path("sacramentot2.shp"))
input_df.columns

"""
Explanation: Then it's time to load some data to estimate segregation. We use the data of 2000 Census Tract Data for the metropolitan area of Sacramento, CA, USA. 
We use a geopandas dataframe available in PySAL examples repository.
For more information about the data: https://github.com/pysal/pysal.lib/tree/master/pysal.lib/examples/sacramento2
End of explanation
"""


groups_list = ['WHITE_', 'BLACK_', 'ASIAN_','HISP_']

"""
Explanation: The groups of interest are White, Black, Asian and Hispanic population. Therefore, we create an auxiliary list with only the necessary columns for fitting the index.
End of explanation
"""


import matplotlib.pyplot as plt

for i in range(len(groups_list)):
    input_df['comp_' + groups_list[i]] = input_df[groups_list[i]] / input_df['TOT_POP']

fig, axes = plt.subplots(ncols = 2, nrows = 2, figsize = (17, 10))


input_df.plot(column = 'comp_' + groups_list[0],
              cmap = 'OrRd',
              legend = True, ax = axes[0,0])
axes[0,0].set_title('Composition of ' + groups_list[0])
axes[0,0].set_xticks([])
axes[0,0].set_yticks([])
axes[0,0].set_facecolor('white')


input_df.plot(column = 'comp_' + groups_list[1],
              cmap = 'OrRd',
              legend = True, ax = axes[0,1])
axes[0,1].set_title('Composition of ' + groups_list[1])
axes[0,1].set_xticks([])
axes[0,1].set_yticks([])
axes[0,1].set_facecolor('white')


input_df.plot(column = 'comp_' + groups_list[2],
              cmap = 'OrRd',
              legend = True, ax = axes[1,0])
axes[1,0].set_title('Composition of ' + groups_list[2])
axes[1,0].set_xticks([])
axes[1,0].set_yticks([])
axes[1,0].set_facecolor('white')

input_df.plot(column = 'comp_' + groups_list[3],
              cmap = 'OrRd',
              legend = True, ax = axes[1,1])
axes[1,1].set_title('Composition of ' + groups_list[3])
axes[1,1].set_xticks([])
axes[1,1].set_yticks([])
axes[1,1].set_facecolor('white')

"""
Explanation: We also can plot the spatial distribution of the composition of each of these groups over the tracts of Sacramento:
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiDissim

index = MultiDissim(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Dissimilarity Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiGiniSeg

index = MultiGiniSeg(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Gini Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiNormalizedExposure

index = MultiNormalizedExposure(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Normalized Exposure Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiInformationTheory

index = MultiInformationTheory(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Information Theory Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiRelativeDiversity

index = MultiRelativeDiversity(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Relative Diversity Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiSquaredCoefficientVariation

index = MultiSquaredCoefficientVariation(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Squared Coefficient of Variation Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiDiversity

index = MultiDiversity(input_df, groups_list)
type(index)

index.statistic

# Normalized version of the multigroup diversity index
normalized_index = MultiDiversity(input_df, groups_list, normalized = True)
normalized_index.statistic

"""
Explanation: Multigroup Diversity Index
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import SimpsonsConcentration

index = SimpsonsConcentration(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Simpson's Concentration Index (lambda)
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import SimpsonsInteraction

index = SimpsonsInteraction(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Simpson's Interaction Index (I)
End of explanation
"""


%%capture
from pysal.explore.segregation.aspatial import MultiDivergence

index = MultiDivergence(input_df, groups_list)
type(index)

index.statistic

"""
Explanation: Multigroup Divergence Index
End of explanation
"""


