8000 Order enforce by ljwolf · Pull Request #951 · pysal/pysal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Order enforce #951

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

Merged
merged 2 commits into from
Jul 12, 2017
Merged
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
62 changes: 38 additions & 24 deletions pysal/weights/Contiguity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def __init__(self, polygons, method='binning', **kw):
:class:`pysal.weights.W`
"""
criterion = 'rook'
ids = kw.pop('ids', None)
neighbors, ids = _build(polygons, criterion=criterion,
ids=ids, method=method)
ids = kw.pop('ids', None)
neighbors, ids = _build(polygons, ids=ids,
criterion=criterion, method=method)
W.__init__(self, neighbors, ids=ids, **kw)

@classmethod
Expand Down Expand Up @@ -82,14 +82,15 @@ def from_shapefile(cls, filepath, idVariable=None, full=False, **kwargs):
ids = get_ids(filepath, idVariable)
else:
ids = None
iterable = FileIO(filepath)
w = cls(FileIO(filepath), ids=ids, **kwargs)
w.set_shapefile(filepath, idVariable=idVariable, full=full)
if sparse:
w = w.to_WSP()
return w

@classmethod
def from_iterable(cls, iterable, **kwargs):
def from_iterable(cls, iterable, sparse=False, **kwargs):
"""
Construct a weights object from a collection of arbitrary polygons. This
will cast the polygons to PySAL polygons, then build the W.
Expand All @@ -109,11 +110,15 @@ def from_iterable(cls, iterable, **kwargs):
:class:`pysal.weights.Rook`
"""
new_iterable = [asShape(shape) for shape in iterable]
return cls(new_iterable, **kwargs)

w = cls(new_iterable, **kwargs)
if sparse:
w = WSP.from_W(w)

return w

@classmethod
def from_dataframe(cls, df, geom_col='geometry',
idVariable=None, ids=None, id_order=None, **kwargs):
def from_dataframe(cls, df, geom_col='geometry', **kwargs):
"""
Construct a weights object from a pandas dataframe with a geometry
column. This will cast the polygons to PySAL polygons, then build the W
Expand Down Expand Up @@ -144,20 +149,27 @@ def from_dataframe(cls, df, geom_col='geometry',
:class:`pysal.weights.W`
:class:`pysal.weights.Rook`
"""
if id_order is not None:
if id_order is True and ((idVariable is not None)
or (ids is not None)):
idVariable = kwargs.pop('idVariable', None)
ids = kwargs.pop('ids', None)
8000 id_order = kwargs.pop('id_order', True)
if id_order is True and ((idVariable is not None)
or (ids is not None)):
# if idVariable is None, we want ids. Otherwise, we want the
# idVariable column
id_order = list(df.get(idVariable, ids))
else:
id_order = df.get(id_order, ids)
ids = list(df.get(idVariable, ids))
id_order = ids
elif isinstance(id_order, str):
ids = df.get(id_order, ids)
id_order = ids
elif idVariable is not None:
ids = df.get(idVariable).tolist()
elif isinstance(ids, str):
ids = df.get(ids).tolist()
return cls.from_iterable(df[geom_col].tolist(), ids=ids,
id_order=id_order, **kwargs)
else:
id_order = list(df.index)
ids = list(df.index)
w = cls.from_iterable(df[geom_col].tolist(), ids=ids, id_order=id_order, **kwargs)
return w

class Queen(W):
def __init__(self, polygons,method='binning', **kw):
Expand Down Expand Up @@ -303,21 +315,23 @@ def from_dataframe(cls, df, geom_col='geometry', **kwargs):
"""
idVariable = kwargs.pop('idVariable', None)
ids = kwargs.pop('ids', None)
id_order = kwargs.pop('id_order', None)
if id_order is not None:
if id_order is True and ((idVariable is not None)
or (ids is not None)):
id_order = kwargs.pop('id_order', True)
if id_order is True and ((idVariable is not None)
or (ids is not None)):
# if idVariable is None, we want ids. Otherwise, we want the
# idVariable column
ids = list(df.get(idVariable, ids))
id_order = ids
elif isinstance(id_order, str):
ids = df.get(id_order, ids)
id_order = ids
ids = list(df.get(idVariable, ids))
id_order = ids
elif isinstance(id_order, str):
ids = df.get(id_order, ids)
id_order = ids
elif idVariable is not None:
ids = df.get(idVariable).tolist()
elif isinstance(ids, str):
ids = df.get(ids).tolist()
else:
id_order = list(df.index)
ids = list(df.index)
w = cls.from_iterable(df[geom_col].tolist(), ids=ids, id_order=id_order, **kwargs)
return w

Expand Down
2 changes: 2 additions & 0 deletions pysal/weights/Distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,8 @@ def from_dataframe(cls, df, threshold, geom_col='geometry', ids=None, **kwargs):
ids = df.index.tolist()
elif isinstance(ids, str):
ids = df[ids].tolist()
else:
ids = df.index.tolist()
return cls(pts, threshold, ids=ids, **kwargs)

def _band(self):
Expand Down
7 changes: 7 additions & 0 deletions pysal/weights/tests/test_Contiguity.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def test_from_dataframe(self):
w = self.cls.from_dataframe(df, geom_col='the_geom', idVariable=self.idVariable)
self.assertEqual(w[self.known_name], self.known_namedw)

# order preserving
permute = df.sample(frac=1)
w = self.cls.from_dataframe(permute, geom_col='the_geom')
with self.assertRaises(AssertionError):
assert w.id_order == df.index.tolist()
self.assertEqual(w.id_order, permute.index.tolist())

class Test_Queen(ut.TestCase, Contiguity_Mixin):
def setUp(self):
Contiguity_Mixin.setUp(self)
Expand Down
18 changes: 17 additions & 1 deletion pysal/weights/tests/test_Distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def test_from_dataframe(self):
w = d.KNN.from_dataframe(df, k=4)
self.assertEqual(w.neighbors[self.known_wi0], self.known_w0)
self.assertEqual(w.neighbors[self.known_wi1], self.known_w1)
perm = df.sample(frac=1)
w = d.KNN.from_dataframe(perm, k=4)
with self.assertRaises(AssertionError):
assert w.id_order == df.index.tolist()
self.assertEqual(perm.index.tolist(), w.id_order)


def test_from_array(self):
w = d.KNN.from_array(self.poly_centroids, k=4)
Expand Down Expand Up @@ -146,6 +152,11 @@ def test_from_dataframe(self):
w = d.DistanceBand.from_dataframe(df, 1)
for k,v in w:
self.assertEquals(v, self.grid_rook_w[k])
perm = df.sample(frac=1)
w = d.DistanceBand.from_dataframe(perm, 1)
with self.assertRaises(AssertionError):
assert w.id_order == df.index.tolist()
self.assertEqual(w.id_order, perm.index.tolist())

##########################
# Function/User tests #
Expand Down Expand Up @@ -252,7 +263,12 @@ def test_from_dataframe(self):
w = d.Kernel.from_dataframe(df)
for k,v in w[self.known_wi5-1].items():
np.testing.assert_allclose(v, self.known_w5[k+1], rtol=RTOL)

perm = df.sample(frac=1)
w = d.Kernel.from_dataframe(perm)
with self.assertRaises(AssertionError):
assert w.id_order == df.index.tolist()
self.assertEqual(w.id_order, perm.index.tolist())

##########################
# Function/User tests #
##########################
Expand Down
1 change: 1 addition & 0 deletions pysal/weights/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def cardinalities(self):
"""Number of neighbors for each observation.

"""
print(self.neighbors)
if 'cardinalities' not in self._cache:
c = {}
for i in self._id_order:
Expand Down
0