Skip to content

Commit 65e08c3

Browse files
authored
Merge pull request #170 from weikang9009/warning
(docs, bug) silence warning for disconnected components and islands
2 parents 1647032 + 5b2998c commit 65e08c3

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

libpysal/weights/weights.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,11 @@ class W(object):
3737
lexicographical ordering is used to iterate and the
3838
id_order_set property will return False. This can be
3939
set after creation by setting the 'id_order' property.
40-
silent_island_warning: boolean
40+
silence_warnings : boolean
4141
By default libpysal will print a warning if the
42-
dataset contains any disconnected observations or
42+
dataset contains any disconnected components or
4343
islands. To silence this warning set this
4444
parameter to True.
45-
silent_connected_components : boolean
46-
By default PySAL will print a warning if the
47-
dataset contains any disconnected components in the
48-
adjacency matrix. These are disconnected *groups*
49-
of islands. To silence this warning set this
50-
parameter to True.
5145
ids : list
5246
Values to use for keys of the neighbors and weights dicts.
5347
@@ -129,15 +123,16 @@ class W(object):
129123
>>> from libpysal.weights import W
130124
>>> w = W({1:[0],0:[1],2:[], 3:[]})
131125
132-
WARNING: there are 2 disconnected observations
133-
Island ids: [2, 3]
126+
UserWarning: The weights matrix is not fully connected:
127+
There are 3 disconnected components.
128+
There are 2 islands with ids: 2, 3.
134129
135130
"""
136131

137132
def __init__(self, neighbors, weights=None, id_order=None,
138-
silence_warnings=False, ids=None):
139-
self.silent_island_warning = silence_warnings
140-
self.silent_connected_components = silence_warnings
133+
silence_warnings=False,
134+
ids=None):
135+
self.silence_warnings = silence_warnings
141136
self.transformations = {}
142137
self.neighbors = neighbors
143138
if not weights:
@@ -156,18 +151,22 @@ def __init__(self, neighbors, weights=None, id_order=None,
156151
self._id_order_set = True
157152
self._reset()
158153
self._n = len(self.weights)
159-
if self.islands and not self.silent_island_warning:
154+
if not self.silence_warnings:
155+
message = ""
156+
if self.n_components > 1:
157+
message = message + \
158+
"The weights matrix is not fully connected: " \
159+
"\n There are %d disconnected components." % \
160+
self.n_components
160161
ni = len(self.islands)
161162
if ni == 1:
162-
warnings.warn("There is one disconnected observation"
163-
" (no neighbors).\nIsland id: {}"
164-
.format(str(self.islands[0])),
165-
stacklevel=2)
166-
else:
167-
warnings.warn("There are %d disconnected observations" % ni + ' \n '
168-
" Island ids: %s" % ', '.join(str(island) for island in self.islands))
169-
if self.n_components > 1 and not self.islands and not self.silent_connected_components:
170-
warnings.warn("The weights matrix is not fully connected. There are %d components" % self.n_components)
163+
message = message + "\n There is one island with id: " \
164+
"%s."% (str(self.islands[0]))
165+
elif ni > 1:
166+
message = message + "\n There are %d islands with ids: %s." % (
167+
ni, ', '.join(str(island) for island in self.islands))
168+
if len(message):
169+
warnings.warn(message)
171170

172171
def _reset(self):
173172
"""Reset properties.

0 commit comments

Comments
 (0)