-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint & format
io/iohandlers{g-m}
.py (#638)
* lint&format io/iohandlers{g-m}.py * refactor GeoDaTxtReader
- Loading branch information
Showing
6 changed files
with
129 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from .. import fileio | ||
from ...weights.weights import W, WSP | ||
from scipy import sparse | ||
# ruff: noqa: SIM115 | ||
|
||
import numpy as np | ||
from scipy import sparse | ||
|
||
from ...weights.weights import WSP, W | ||
from .. import fileio | ||
|
||
__author__ = "Charles R Schmidt <[email protected]>" | ||
__all__ = ["GalIO"] | ||
|
@@ -18,17 +21,17 @@ def __init__(self, *args, **kwargs): | |
fileio.FileIO.__init__(self, *args, **kwargs) | ||
self.file = open(self.dataPath, self.mode) | ||
|
||
def read(self, n=-1, sparse=False): | ||
def read(self, n=-1, sparse=False): # noqa ARG002 | ||
"""Read in a ``.gal`` file. | ||
Parameters | ||
---------- | ||
n : int | ||
Read at most ``n`` objects. Default is ``-1``. | ||
sparse: bool | ||
If ``True`` return a ``scipy`` sparse object. If ``False`` | ||
return PySAL `W` object. Default is ``False``. | ||
Returns | ||
------- | ||
w : {libpysal.weights.W, libpysal.weights.WSP} | ||
|
@@ -52,12 +55,12 @@ def _get_data_type(self): | |
|
||
def _set_data_type(self, typ): | ||
""" | ||
Raises | ||
------ | ||
TypeError | ||
Raised when ``typ`` is not a callable. | ||
""" | ||
if callable(typ): | ||
self._typ = typ | ||
|
@@ -73,12 +76,12 @@ def _read(self): | |
------- | ||
w : {libpysal.weights.W, libpysal.weights.WSP} | ||
A PySAL `W` object or a thin PySAL `WSP`. | ||
Raises | ||
------ | ||
StopIteration | ||
Raised at the EOF. | ||
Examples | ||
-------- | ||
|
@@ -93,10 +96,10 @@ def _read(self): | |
>>> w = testfile.read() | ||
>>> w.n == 100 | ||
True | ||
>>> print(round(w.sd,6)) | ||
1.515124 | ||
>>> testfile = libpysal.io.open(libpysal.examples.get_path('sids2.gal'), 'r') | ||
Return a sparse matrix for the `W` information. | ||
|
@@ -108,7 +111,6 @@ def _read(self): | |
""" | ||
|
||
if self._sparse: | ||
|
||
if self.pos > 0: | ||
raise StopIteration | ||
|
||
|
@@ -128,18 +130,18 @@ def _read(self): | |
counter = 0 | ||
typ = self.data_type | ||
|
||
for i in range(n): | ||
id, n_neighbors = self.file.readline().strip().split() | ||
id = typ(id) | ||
for _ in range(n): | ||
id_, n_neighbors = self.file.readline().strip().split() | ||
id_ = typ(id) | ||
n_neighbors = int(n_neighbors) | ||
neighbors_i = list(map(typ, self.file.readline().strip().split())) | ||
nn = len(neighbors_i) | ||
extend([id] * nn) | ||
extend([id_] * nn) | ||
counter += nn | ||
|
||
for id_neigh in neighbors_i: | ||
append(id_neigh) | ||
idsappend(id) | ||
idsappend(id_) | ||
|
||
self.pos += 1 | ||
row = np.array(row) | ||
|
@@ -153,7 +155,6 @@ def _read(self): | |
w = WSP(spmat) | ||
|
||
else: | ||
|
||
if self.pos > 0: | ||
raise StopIteration | ||
|
||
|
@@ -171,13 +172,13 @@ def _read(self): | |
w = {} | ||
typ = self.data_type | ||
|
||
for i in range(n): | ||
id, n_neighbors = self.file.readline().strip().split() | ||
id = typ(id) | ||
for _ in range(n): | ||
id_, n_neighbors = self.file.readline().strip().split() | ||
id_ = typ(id_) | ||
n_neighbors = int(n_neighbors) | ||
neighbors_i = list(map(typ, self.file.readline().strip().split())) | ||
neighbors[id] = neighbors_i | ||
ids.append(id) | ||
neighbors[id_] = neighbors_i | ||
ids.append(id_) | ||
|
||
self.pos += 1 | ||
|
||
|
@@ -197,7 +198,7 @@ def write(self, obj): | |
------ | ||
TypeError | ||
Raised when the input ``obj`` is not a PySAL `W`. | ||
Examples | ||
-------- | ||
|
@@ -238,18 +239,18 @@ def write(self, obj): | |
Clean up the temporary file created for this example. | ||
>>> os.remove(fname) | ||
""" | ||
|
||
self._complain_ifclosed(self.closed) | ||
|
||
if issubclass(type(obj), W): | ||
IDS = obj.id_order | ||
ids = obj.id_order | ||
self.file.write("%d\n" % (obj.n)) | ||
|
||
for id in IDS: | ||
neighbors = obj.neighbors[id] | ||
self.file.write("%s %d\n" % (str(id), len(neighbors))) | ||
for id_ in ids: | ||
neighbors = obj.neighbors[id_] | ||
self.file.write("%s %d\n" % (str(id_), len(neighbors))) | ||
self.file.write(" ".join(map(str, neighbors)) + "\n") | ||
self.pos += 1 | ||
else: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from .. import fileio | ||
# ruff: noqa: SIM115 | ||
|
||
from ...weights import W | ||
from .. import fileio | ||
|
||
__author__ = "Myunghwa Hwang <[email protected]>" | ||
__all__ = ["GeoBUGSTextIO"] | ||
|
@@ -11,11 +13,11 @@ class GeoBUGSTextIO(fileio.FileIO): | |
`GeoBUGS` generates a spatial weights matrix as an R object and writes | ||
it out as an ASCII text representation of the R object. An exemplary | ||
`GeoBUGS` text file is as follows. | ||
``` | ||
list([CARD], [ADJ], [WGT], [SUMNUMNEIGH]) | ||
``` | ||
where ``[CARD]`` and ``[ADJ]`` are required but the others are optional. | ||
PySAL assumes ``[CARD]`` and ``[ADJ]`` always exist in an input text file. | ||
It can read a `GeoBUGS` text file, even when its content is not written | ||
|
@@ -31,7 +33,7 @@ class GeoBUGSTextIO(fileio.FileIO): | |
[ADJ]: | ||
adj = c ([a list of comma-splitted neighbor IDs]) | ||
If caridnality is zero, neighbor IDs are skipped. The ordering of | ||
observations is the same in both ``[CARD]`` and ``[ADJ]``. | ||
Neighbor IDs are record numbers starting from one. | ||
|
@@ -48,15 +50,15 @@ class GeoBUGSTextIO(fileio.FileIO): | |
Notes | ||
----- | ||
For the files generated from R the ``spdep``, ``nb2WB``, and ``dput`` | ||
functions. It is assumed that the value for the control parameter of | ||
the ``dput`` function is ``NULL``. Please refer to R ``spdep`` and | ||
``nb2WB`` functions help files. | ||
References | ||
---------- | ||
* **Thomas, A., Best, N., Lunn, D., Arnold, R., and Spiegelhalter, D.** | ||
(2004) GeoBUGS User Manual. R spdep nb2WB function help file. | ||
|
@@ -70,7 +72,7 @@ def __init__(self, *args, **kwargs): | |
fileio.FileIO.__init__(self, *args, **kwargs) | ||
self.file = open(self.dataPath, self.mode) | ||
|
||
def read(self, n=-1): | ||
def read(self, n=-1): # noqa ARG002 | ||
"""Read a GeoBUGS text file. | ||
Returns | ||
|
@@ -119,17 +121,17 @@ def seek(self, pos) -> int: | |
|
||
def _read(self): | ||
"""Reads in a `GeoBUGSTextIO` object. | ||
Raises | ||
------ | ||
StopIteration | ||
Raised at the EOF. | ||
Returns | ||
------- | ||
w : libpysal.weights.W | ||
A PySAL `W` object. | ||
""" | ||
|
||
if self.pos > 0: | ||
|
@@ -214,7 +216,7 @@ def write(self, obj): | |
------ | ||
TypeError | ||
Raised when the input ``obj`` is not a PySAL `W`. | ||
Examples | ||
-------- | ||
|
@@ -240,7 +242,7 @@ def write(self, obj): | |
>>> o = libpysal.io.open(fname, 'w', 'geobugs_text') | ||
Write the Weights object into the open file. | ||
Write the Weights object into the open file. | ||
>>> o.write(w) | ||
>>> o.close() | ||
|
@@ -263,7 +265,6 @@ def write(self, obj): | |
self._complain_ifclosed(self.closed) | ||
|
||
if issubclass(type(obj), W): | ||
|
||
cardinalities, neighbors, weights = [], [], [] | ||
for i in obj.id_order: | ||
cardinalities.append(obj.cardinalities[i]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,40 @@ | ||
# ruff: noqa: N802, N806, SIM115 | ||
|
||
from .. import tables | ||
|
||
__author__ = "Charles R Schmidt <[email protected]>" | ||
__all__ = ["GeoDaTxtReader"] | ||
|
||
from typing import Union | ||
|
||
|
||
class GeoDaTxtReader(tables.DataTable): | ||
"""GeoDa Text File Export Format. | ||
Examples | ||
-------- | ||
>>> import libpysal | ||
>>> f = libpysal.io.open(libpysal.examples.get_path('stl_hom.txt'),'r') | ||
>>> f.header | ||
['FIPSNO', 'HR8488', 'HR8893', 'HC8488'] | ||
>>> len(f) | ||
78 | ||
>>> f.dat[0] | ||
['17107', '1.290722', '1.624458', '2'] | ||
>>> f.dat[-1] | ||
['29223', '0', '8.451537', '0'] | ||
>>> f._spec | ||
[int, float, float, int] | ||
""" | ||
|
||
__doc__ = tables.DataTable.__doc__ | ||
|
||
FORMATS = ["geoda_txt"] | ||
MODES = ["r"] | ||
|
||
def __init__(self, *args, **kwargs): | ||
|
||
tables.DataTable.__init__(self, *args, **kwargs) | ||
self.__idx = {} | ||
self.__len = None | ||
|
@@ -46,17 +43,16 @@ def __init__(self, *args, **kwargs): | |
|
||
def _open(self): | ||
""" | ||
Raises | ||
------ | ||
TypeError | ||
Raised when the input 'geoda_txt' is not valid. | ||
""" | ||
|
||
if self.mode == "r": | ||
|
||
self.fileObj = open(self.dataPath, "r") | ||
self.fileObj = open(self.dataPath) | ||
n, k = self.fileObj.readline().strip().split(",") | ||
n, k = int(n), int(k) | ||
header = self.fileObj.readline().strip().split(",") | ||
|
@@ -65,7 +61,7 @@ def _open(self): | |
try: | ||
assert len(self.header) == k | ||
except AssertionError: | ||
raise TypeError("This is not a valid 'geoda_txt' file.") | ||
raise TypeError("This is not a valid 'geoda_txt' file.") from None | ||
|
||
dat = self.fileObj.readlines() | ||
|
||
|
@@ -76,7 +72,7 @@ def _open(self): | |
def __len__(self) -> int: | ||
return self.__len | ||
|
||
def _read(self) -> Union[list, None]: | ||
def _read(self) -> list | None: | ||
if self.pos < len(self): | ||
row = self.dat[self.pos] | ||
self.pos += 1 | ||
|
@@ -114,3 +110,6 @@ def _determineSpec(data) -> list: | |
spec.append(str) | ||
|
||
return spec | ||
|
||
|
||
GeoDaTxtReader.__doc__ = tables.DataTable.__doc__ |
Oops, something went wrong.