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

lint & format io/iohandlers{a-d}.py #637

Merged
merged 1 commit into from
Nov 9, 2023
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
24 changes: 13 additions & 11 deletions libpysal/io/iohandlers/arcgis_dbf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .. import fileio
from ...weights.weights import W
# ruff: noqa: N802, N803, N806, N815

from ...weights.util import remap_ids
from ...weights.weights import W
from .. import fileio

__author__ = "Myunghwa Hwang <mhwang4@gmail.com>"
__all__ = ["ArcGISDbfIO"]
Expand Down Expand Up @@ -29,7 +31,7 @@ class ArcGISDbfIO(fileio.FileIO):
record numbers, instead of original ids.

An exemplary structure of an ArcGIS dbf file is as follows:

```
[Line 1] Field1 RECORD_ID NID WEIGHT
[Line 2] 0 72 76 1
Expand All @@ -41,7 +43,7 @@ class ArcGISDbfIO(fileio.FileIO):

References
----------

http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Convert_Spatial_Weights_Matrix_to_Table_(Spatial_Statistics)

"""
Expand All @@ -64,7 +66,7 @@ def _get_varName(self):

varName = property(fget=_get_varName, fset=_set_varName)

def read(self, n=-1):
def read(self, n=-1): # noqa ARG002
self._complain_ifclosed(self.closed)
return self._read()

Expand All @@ -74,12 +76,12 @@ def seek(self, pos):

def _read(self):
"""Reads ArcGIS dbf file

Returns
-------
w : libpysal.weights.W
A ``libpysal.weights.W`` object.

Raises
------
StopIteration
Expand All @@ -88,7 +90,7 @@ def _read(self):
Raised when the weights data structure is incorrect.
TypeError
Raised when the IDs are not integers.

Examples
--------

Expand Down Expand Up @@ -237,10 +239,10 @@ def write(self, obj, useIdIndex=False):
id_spec = ("N", len(str(max(obj.id_order))), 0)
self.file.field_spec = [id_spec, id_spec, ("N", 13, 6)]

for id in obj.id_order:
neighbors = list(zip(obj.neighbors[id], obj.weights[id]))
for id_ in obj.id_order:
neighbors = list(zip(obj.neighbors[id_], obj.weights[id_], strict=True))
for neighbor, weight in neighbors:
self.file.write([id, neighbor, weight])
self.file.write([id_, neighbor, weight])
self.pos = self.file.pos

else:
Expand Down
77 changes: 40 additions & 37 deletions libpysal/io/iohandlers/arcgis_swm.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import numpy as np
# ruff: noqa: N802, N806, N815, SIM115

from struct import pack, unpack
from .. import fileio

import numpy as np

from ...weights import W
from ...weights.util import remap_ids
from .. import fileio

__author__ = "Myunghwa Hwang <mhwang4@gmail.com>"
__all__ = ["ArcGISSwmIO"]
Expand All @@ -13,7 +17,7 @@ class ArcGISSwmIO(fileio.FileIO):
Spatial weights objects in the ArcGIS ``.swm`` format are used in ArcGIS
Spatial Statistics tools. Particularly, this format can be directly used
with the tools under the category of Mapping Clusters.

The values for``ORG_i`` and ``DST_i`` should be integers, as ArcGIS Spatial
Statistics tools support only unique integer IDs. For the case where a
weights object uses non-integer IDs, `ArcGISSwmIO` allows users to use
Expand All @@ -22,22 +26,22 @@ class ArcGISSwmIO(fileio.FileIO):

.. table:: ArcGIS SWM Components
============ ============ ==================================== ================================
Part Data type Description Length
Part Data type Description Length
============ ============ ==================================== ================================
ID_VAR_NAME ASCII TEXT ID variable name Flexible (Up to the 1st ;)
ID_VAR_NAME ASCII TEXT ID variable name Flexible (Up to the 1st ;)
ESRI_SRS ASCII TEXT ESRI spatial reference system Flexible (Btw the 1st ; and \\n)
NO_OBS l.e. int Number of observations 4
ROW_STD l.e. int Whether or not row-standardized 4
WGT_i
ORG_i l.e. int ID of observaiton i 4
NO_NGH_i l.e. int Number of neighbors for obs. i (m) 4
NGHS_i
DSTS_i l.e. int IDs of all neighbors of obs. i 4*m
WS_i l.e. float Weights for obs. i and its neighbors 8*m
W_SUM_i l.e. float Sum of weights for " 8
NO_OBS l.e. int Number of observations 4
ROW_STD l.e. int Whether or not row-standardized 4
WGT_i
ORG_i l.e. int ID of observaiton i 4
NO_NGH_i l.e. int Number of neighbors for obs. i (m) 4
NGHS_i
DSTS_i l.e. int IDs of all neighbors of obs. i 4*m
WS_i l.e. float Weights for obs. i and its neighbors 8*m
W_SUM_i l.e. float Sum of weights for " 8
============ ============ ==================================== ================================

"""
""" # noqa E501

FORMATS = ["swm"]
MODES = ["r", "w"]
Expand Down Expand Up @@ -66,7 +70,7 @@ def _get_srs(self):

srs = property(fget=_get_srs, fset=_set_srs)

def read(self, n=-1):
def read(self, n=-1): # noqa ARG002
self._complain_ifclosed(self.closed)
return self._read()

Expand All @@ -77,17 +81,17 @@ def seek(self, pos):

def _read(self):
"""Read an ArcGIS ``.swm`` file.

Returns
-------
w : libpysal.weights.W
A PySAL `W` object.

Raises
------
StopIteration
Raised at the EOF.

Examples
--------

Expand Down Expand Up @@ -131,17 +135,17 @@ def _read(self):

def read_old_version(self, header):
"""Read the old version of ArcGIS(<10.1) ``.swm`` file.

Parameters
----------
header : str
The first line of the ``.swm`` file.

Returns
-------
w : libpysal.weights.W
A PySAL `W` object.

"""

id_var, srs = header[:-1].split(";")
Expand All @@ -152,7 +156,7 @@ def read_old_version(self, header):
neighbors = {}
weights = {}

for i in range(no_obs):
for _ in range(no_obs):
origin, no_nghs = tuple(unpack("<2l", self.file.read(8)))
neighbors[origin] = []
weights[origin] = []
Expand All @@ -164,7 +168,7 @@ def read_old_version(self, header):
weights[origin] = list(
unpack("<%id" % no_nghs, self.file.read(8 * no_nghs))
)
w_sum = list(unpack("<d", self.file.read(8)))[0]
_ = list(unpack("<d", self.file.read(8)))[0]

self.pos += 1

Expand All @@ -175,19 +179,19 @@ def read_old_version(self, header):
def read_new_version(self, header_line):
"""Read the new version of ArcGIS(<10.1) ``.swm`` file, which contains
more parameters and records weights in two ways, fixed or variable.

Parameters
----------
header_line : str
The first line of the ``.swm`` file, which contains a lot of
parameters. The parameters are divided by semicolons (';') and
the key-value of each parameter is divided by at marks ('@').

Returns
-------
w : libpysal.weights.W
A PySAL `W` object.

"""

headerDict = {}
Expand All @@ -205,12 +209,11 @@ def read_new_version(self, header_line):
fixedWeights = headerDict["FIXEDWEIGHTS"].upper().strip() == "TRUE"

no_obs, row_std = tuple(unpack("<2l", self.file.read(8)))
is_row_standard = row_std == 1

neighbors = {}
weights = {}

for i in range(no_obs):
for _ in range(no_obs):
origin, no_nghs = tuple(unpack("<2l", self.file.read(8)))
neighbors[origin] = []
weights[origin] = []
Expand All @@ -226,15 +229,15 @@ def read_new_version(self, header_line):
weights[origin] = list(
unpack("<%id" % no_nghs, self.file.read(8 * no_nghs))
)
w_sum = list(unpack("<d", self.file.read(8)))[0]
_ = list(unpack("<d", self.file.read(8)))[0]

self.pos += 1

w = W(neighbors, weights)

return w

def write(self, obj, useIdIndex=False):
def write(self, obj, useIdIndex=False): # noqa N803
"""Writes a spatial weights matrix data file in ``.swm`` format.

Parameters
Expand All @@ -250,7 +253,7 @@ def write(self, obj, useIdIndex=False):
Raised when the input ``obj`` is not a PySAL `W`.
TypeError
Raised when the IDs in input ``obj`` are not integers.

Examples
--------

Expand Down Expand Up @@ -296,22 +299,22 @@ def write(self, obj, useIdIndex=False):
Clean up the temporary file created for this example.

>>> os.remove(fname)

"""

self._complain_ifclosed(self.closed)

if not issubclass(type(obj), W):
raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj)))

if not (type(obj.id_order[0]) in (np.int32, np.int64, int)) and not useIdIndex:
if (type(obj.id_order[0]) not in (np.int32, np.int64, int)) and not useIdIndex:
raise TypeError("ArcGIS SWM files support only integer IDs.")

if useIdIndex:
id2i = obj.id2i
obj = remap_ids(obj, id2i)

unk = str("%s;%s\n" % (self.varName, self.srs)).encode()
unk = str(f"{self.varName};{self.srs}\n").encode()
self.file.write(unk)
self.file.write(pack("<l", obj.n))
self.file.write(pack("<l", obj.transform.upper() == "R"))
Expand All @@ -320,8 +323,8 @@ def write(self, obj, useIdIndex=False):
self.file.write(pack("<l", obs))
no_nghs = len(obj.weights[obs])
self.file.write(pack("<l", no_nghs))
self.file.write(pack("<%il" % no_nghs, *obj.neighbors[obs]))
self.file.write(pack("<%id" % no_nghs, *obj.weights[obs]))
self.file.write(pack(f"<{no_nghs}l", *obj.neighbors[obs]))
self.file.write(pack(f"<{no_nghs}d", *obj.weights[obs]))
self.file.write(pack("<d", sum(obj.weights[obs])))

self.pos += 1
Expand Down
Loading