Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions pysal/esda/mapclassify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,11 +1121,8 @@ class Jenks_Caspall(Map_Classifier):
>>> cal = load_example()
>>> jc = Jenks_Caspall(cal, k = 5)
>>> jc.bins
array([[ 1.81000000e+00],
[ 7.60000000e+00],
[ 2.98200000e+01],
[ 1.81270000e+02],
[ 4.11145000e+03]])
array([ 1.81000000e+00, 7.60000000e+00, 2.98200000e+01,
1.81270000e+02, 4.11145000e+03])
>>> jc.counts
array([14, 13, 14, 10, 7])

Expand Down Expand Up @@ -1161,8 +1158,9 @@ def _set_bins(self):
xb0 = xb
it += 1
q = np.array([np.median(x[xb == i]) for i in rk])
cuts = [max(x[xb == i]) for i in sp.unique(xb)]
self.bins = np.array(cuts)
cuts = np.array([max(x[xb == i]) for i in sp.unique(xb)])
cuts.shape = (len(cuts),)
self.bins = cuts
self.iterations = it


Expand Down Expand Up @@ -1202,17 +1200,9 @@ class Jenks_Caspall_Sampled(Map_Classifier):
>>> jc = Jenks_Caspall(x)
>>> jcs = Jenks_Caspall_Sampled(x)
>>> jc.bins
array([[ 0.19770952],
[ 0.39695769],
[ 0.59588617],
[ 0.79716865],
[ 0.99999425]])
array([ 0.19770952, 0.39695769, 0.59588617, 0.79716865, 0.99999425])
>>> jcs.bins
array([[ 0.18877882],
[ 0.39341638],
[ 0.6028286 ],
[ 0.80070925],
[ 0.99999425]])
array([ 0.18877882, 0.39341638, 0.6028286 , 0.80070925, 0.99999425])
>>> jc.counts
array([19804, 20005, 19925, 20178, 20088])
>>> jcs.counts
Expand Down
27 changes: 13 additions & 14 deletions pysal/esda/tests/test_mapclassify.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,9 @@ def test_Jenks_Caspall(self):
np.testing.assert_array_almost_equal(jc.counts,
np.array([14, 13, 14, 10, 7]))
np.testing.assert_array_almost_equal(jc.bins,
np.array(
[[1.81000000e+00], [7.60000000e+00],
[2.98200000e+01], [1.81270000e+02],
[4.11145000e+03]]))
np.array( [1.81000000e+00, 7.60000000e+00,
2.98200000e+01, 1.81270000e+02,
4.11145000e+03]))


class TestJenksCaspallSampled(unittest.TestCase):
Expand All @@ -285,17 +284,17 @@ def test_Jenks_Caspall_Sampled(self):
jc = Jenks_Caspall(x)
jcs = Jenks_Caspall_Sampled(x)
np.testing.assert_array_almost_equal(jc.bins,
np.array([[0.19718393],
[0.39655886],
[0.59648522],
[0.79780763],
[0.99997979]]))
np.array([0.19718393,
0.39655886,
0.59648522,
0.79780763,
0.99997979]))
np.testing.assert_array_almost_equal(jcs.bins,
np.array([[0.20856569],
[0.41513931],
[0.62457691],
[0.82561423],
[0.99997979]]))
np.array([0.20856569,
0.41513931,
0.62457691,
0.82561423,
0.99997979]))


class TestJenksCaspallForced(unittest.TestCase):
Expand Down
116 changes: 113 additions & 3 deletions pysal/weights/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import gc
import operator

__all__ = ['lat2W', 'regime_weights', 'comb', 'order', 'higher_order', 'shimbel', 'remap_ids', 'full2W', 'full', 'WSP2W', 'insert_diagonal', 'get_ids', 'get_points_array_from_shapefile', 'min_threshold_distance', 'lat2SW', 'w_local_cluster', 'higher_order_sp']
__all__ = ['lat2W', 'regime_weights', 'comb', 'order', 'higher_order', 'shimbel', 'remap_ids', 'full2W', 'full',
'WSP2W', 'insert_diagonal', 'get_ids', 'get_points_array_from_shapefile', 'min_threshold_distance',
'lat2SW', 'w_local_cluster', 'higher_order_sp', 'w_graph_properties']


def lat2W(nrows=5, ncols=5, rook=True, id_type='int'):
Expand Down Expand Up @@ -107,7 +109,6 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int'):
gc.enable()
return pysal.weights.W(w, weights, ids)


def regime_weights(regimes):
"""
Construct spatial weights for regime neighbors.
Expand Down Expand Up @@ -361,7 +362,7 @@ def higher_order_sp(wsp, k=2):
{1: 1.0, 3: 1.0, 5: 1.0, 9: 1.0, 15: 1.0, 19: 1.0, 21: 1.0, 23: 1.0}
>>> pysal.weights.WSP2W(ws25o3)[12]
{1: 1.0, 3: 1.0, 5: 1.0, 9: 1.0, 15: 1.0, 19: 1.0, 21: 1.0, 23: 1.0}
>>>
>>>
"""


Expand Down Expand Up @@ -1020,6 +1021,115 @@ def write_gal(file, k=10):
f.write(" ".join(map(str, neighs)))
f.close()

def w_graph_properties(w):
"""
Report graph theory based properties of the neighbor relations for a W object

Arguments
=========

w: Spatial Weights Object

Returns
=======
results: dict
'cycles': list of lists of simple cycles
'n_cycles': number of cycles
'components': list of lists of connected components
'n_compotnents: number of components

Example
=======
>>> c = {0:[1,2], 1:[0,3], 2:[0,3], 3:[1,2]}
>>> c[4] = [5]
>>> c[5] = [4]
>>> pysal.weights.util.w_graph_properties(pysal.W(c))
{'cycles': [[0, 1, 3, 2]], 'n_cycles': 1, 'n_components': 2, 'components': [[0, 1, 2, 3], [4, 5]]}
"""


results = {}
results['cycles'] = _find_all_cycles(w.neighbors)
results['n_cycles'] = len(results['cycles'])
results['components'] = _get_components(w.neighbors)
results['n_components'] = len(results['components'])
return results



# These will likely move elsewhere in PySAL to be used more generally. For now
# we make them private as they are the implementation for here for w_graph_properties(w):


def _single_source_shortest_path(neighbors, source):
level = 0
next_level = {source:1}
paths = {source: [source]}
while next_level:
this_level = next_level
next_level = {}
for v in this_level:
for w in neighbors[v]:
if w not in paths:
paths[w] = paths[v]+[w]
next_level[w] = 1
level = level + 1
return paths

def _get_components(neighbors):
"""
Get connected components of the neighbors adjacency graph
"""
nodes = neighbors.keys()
seen = {}
components = []
for node in nodes:
if node not in seen:
c = _single_source_shortest_path(neighbors, node)
components.append(c.keys())
seen.update(c)
components.sort(key=len, reverse=True)
return components


def _find_cycle_to_ancestor(spanning_tree, node, ancestor):
path = []
while (node != ancestor):
if node is None:
return []
path.append(node)
node = spanning_tree[node]
path.append(node)
path.reverse()
return path

def _find_all_cycles(graph):
"""
Find simple cycles in the graph
"""
def dfs(node):
visited.add(node)
for each in graph[node]:
if each not in visited:
spanning_tree[each] = node
dfs(each)
else:
if (spanning_tree[node] != each):
cycle = _find_cycle_to_ancestor(spanning_tree, node, each)
if cycle:
cycles.append(cycle)
visited = set()
spanning_tree = {}
cycles = []
for each in graph:
if each not in visited:
spanning_tree[each] = None
dfs(each)
return cycles




if __name__ == "__main__":
from pysal import lat2W

Expand Down