Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add robust and informative condition for large magnitude projections (tests only off-diagonal) #113

Merged
merged 2 commits into from Jul 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 30 additions & 22 deletions segregation/spatial/spatial_indexes.py
Expand Up @@ -1030,13 +1030,14 @@ def _distance_decay_isolation(data,
'c_lons': c_lons
})) # This needs to be latitude first!

np.fill_diagonal(dist, val=(alpha * data.area)**(beta))
c = np.exp(-dist)

Pij = np.multiply(c, t) / np.sum(np.multiply(c, t), axis=1)

if np.isnan(Pij).sum() > 0:
raise ValueError('It not possible to determine the distance between, at least, one pair of units. This is probably due to the magnitude of the number of the centroids. We recommend to reproject the geopandas DataFrame.')
if c.sum() < 10 ** (-15):
raise ValueError('It not possible to determine accurately the exponential of the negative distances. This is probably due to the large magnitude of the centroids numbers. It is recommended to reproject the geopandas DataFrame. Also, if this is a not lat-long CRS, it is recommended to set metric to \'haversine\'')

np.fill_diagonal(c, val = np.exp(-(alpha * data.area)**(beta)))

Pij = np.multiply(c, t) / np.sum(np.multiply(c, t), axis=1)

DDxPx = (np.array(x / X) *
np.nansum(np.multiply(Pij, np.array(x / t)), axis=1)).sum()
Expand Down Expand Up @@ -1261,13 +1262,14 @@ def _distance_decay_exposure(data,
'c_lons': c_lons
})) # This needs to be latitude first!

np.fill_diagonal(dist, val=(alpha * data.area)**(beta))
c = np.exp(-dist)

Pij = np.multiply(c, t) / np.sum(np.multiply(c, t), axis=1)

if np.isnan(Pij).sum() > 0:
raise ValueError('It not possible to determine the distance between, at least, one pair of units. This is probably due to the magnitude of the number of the centroids. We recommend to reproject the geopandas DataFrame.')
if c.sum() < 10 ** (-15):
raise ValueError('It not possible to determine accurately the exponential of the negative distances. This is probably due to the large magnitude of the centroids numbers. It is recommended to reproject the geopandas DataFrame. Also, if this is a not lat-long CRS, it is recommended to set metric to \'haversine\'')

np.fill_diagonal(c, val = np.exp(-(alpha * data.area)**(beta)))

Pij = np.multiply(c, t) / np.sum(np.multiply(c, t), axis=1)

DDxPy = (x / X * np.nansum(np.multiply(Pij, y / t), axis=1)).sum()

Expand Down Expand Up @@ -1489,16 +1491,17 @@ def _spatial_proximity(data,
'c_lons': c_lons
})) # This needs to be latitude first!

np.fill_diagonal(dist, val=(alpha * data.area)**(beta))
c = np.exp(-dist)


if c.sum() < 10 ** (-15):
raise ValueError('It not possible to determine accurately the exponential of the negative distances. This is probably due to the large magnitude of the centroids numbers. It is recommended to reproject the geopandas DataFrame. Also, if this is a not lat-long CRS, it is recommended to set metric to \'haversine\'')

np.fill_diagonal(c, val = np.exp(-(alpha * data.area)**(beta)))

Pxx = ((np.array(data.xi) * c).T * np.array(data.xi)).sum() / X**2
Pyy = ((np.array(data.yi) * c).T * np.array(data.yi)).sum() / Y**2
Ptt = ((np.array(data.ti) * c).T * np.array(data.ti)).sum() / T**2
SP = (X * Pxx + Y * Pyy) / (T * Ptt)

if np.isnan(SP):
raise ValueError('It not possible to determine the distance between, at least, one pair of units. This is probably due to the magnitude of the number of the centroids. We recommend to reproject the geopandas DataFrame.')

core_data = data[['group_pop_var', 'total_pop_var', 'geometry']]

Expand Down Expand Up @@ -1714,14 +1717,15 @@ def _absolute_clustering(data,
'c_lons': c_lons
})) # This needs to be latitude first!

np.fill_diagonal(dist, val=(alpha * data.area)**(beta))
c = np.exp(-dist)


if c.sum() < 10 ** (-15):
raise ValueError('It not possible to determine accurately the exponential of the negative distances. This is probably due to the large magnitude of the centroids numbers. It is recommended to reproject the geopandas DataFrame. Also, if this is a not lat-long CRS, it is recommended to set metric to \'haversine\'')

np.fill_diagonal(c, val = np.exp(-(alpha * data.area)**(beta)))

ACL = ((((x/X) * (c * x).sum(axis = 1)).sum()) - ((X / n**2) * c.sum())) / \
((((x/X) * (c * t).sum(axis = 1)).sum()) - ((X / n**2) * c.sum()))

if np.isnan(ACL):
raise ValueError('It not possible to determine the distance between, at least, one pair of units. This is probably due to the magnitude of the number of the centroids. We recommend to reproject the geopandas DataFrame.')

core_data = data[['group_pop_var', 'total_pop_var', 'geometry']]

Expand Down Expand Up @@ -1927,9 +1931,13 @@ def _relative_clustering(data,
'c_lons': c_lons
})) # This needs to be latitude first!

np.fill_diagonal(dist, val=(alpha * data.area)**(beta))
c = np.exp(-dist)


if c.sum() < 10 ** (-15):
raise ValueError('It not possible to determine accurately the exponential of the negative distances. This is probably due to the large magnitude of the centroids numbers. It is recommended to reproject the geopandas DataFrame. Also, if this is a not lat-long CRS, it is recommended to set metric to \'haversine\'')

np.fill_diagonal(c, val = np.exp(-(alpha * data.area)**(beta)))

Pxx = ((np.array(data.xi) * c).T * np.array(data.xi)).sum() / X**2
Pyy = ((np.array(data.yi) * c).T * np.array(data.yi)).sum() / Y**2
RCL = Pxx / Pyy - 1
Expand Down