%matplotlib inline

import geopandas as gpd
from pysal.explore import segregation
import pysal.lib
from pysal.explore.segregation.compute_all import ComputeAllAspatialSegregation, ComputeAllSpatialSegregation, ComputeAllSegregation

"""
Explanation: Example of use of computing several measures of the PySAL segregation module: using ComputeAll classes
The Compute_All classes comprises simple and quick functions to assess multiple segregation measures at once in a dataset. It uses all the default parameters and returns an object that has an attribute (.computed) of a dictionary with summary of all values fitted.
The wrappers have currently three classes: ComputeAllAspatialSegregation, ComputeAllSpatialSegregation and ComputeAllSegregation which comprises all the measures available.
Firstly, we need to import the libraries and functions to be used.
End of explanation
"""


s_map = gpd.read_file(pysal.lib.examples.get_path("sacramentot2.shp"))
s_map.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
"""


gdf = s_map[['geometry', 'HISP_', 'TOT_POP']]

"""
Explanation: The data have several demographic variables. We are going to assess the segregation of the Hispanic Population (variable 'HISP_'). For this, we only extract some columns of the geopandas dataframe.
End of explanation
"""


aspatial_fit = ComputeAllAspatialSegregation(gdf, 'HISP_', 'TOT_POP')
aspatial_fit.computed

"""
Explanation: Compute All Aspatial Measures
End of explanation
"""


spatial_fit = ComputeAllSpatialSegregation(gdf, 'HISP_', 'TOT_POP')
spatial_fit.computed

"""
Explanation: Compute All Spatial Measures
End of explanation
"""


segregation_fit = ComputeAllSegregation(gdf, 'HISP_', 'TOT_POP')
segregation_fit.computed

"""
Explanation: Compute All Segregation Measures
End of explanation
"""


