Skip to content

Commit

Permalink
Fixed relative import issue for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SamComber committed Jul 31, 2020
1 parent 903a32d commit c9009b1
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 153 deletions.
3 changes: 2 additions & 1 deletion spacv/base_classes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import geopandas as gpd
from .utils import convert_geoseries, convert_geodataframe
from sklearn.model_selection import BaseCrossValidator
from .utils import convert_geoseries, convert_geodataframe

class BaseSpatialCV(BaseCrossValidator):
"""
Expand All @@ -15,6 +15,7 @@ def __init__(
def split(self, XYs, y=None, groups=None):
XYs = convert_geoseries(XYs).reset_index(drop=True)
minx, miny, maxx, maxy = XYs.total_bounds

buffer_radius = self.buffer_radius
if buffer_radius > maxx-minx or buffer_radius > maxy-miny:
raise ValueError(
Expand Down
2 changes: 0 additions & 2 deletions spacv/grid_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from sklearn.neighbors import BallTree
from .utils import convert_geodataframe, geometry_to_2d, convert_numpy



def construct_blocks(XYs, tiles_x, tiles_y, method='unique', shape='square',
direction='diagonal', data=None, n_groups=5, n_sims=10,
distance_metric='euclidean'):
Expand Down
15 changes: 6 additions & 9 deletions spacv/spacv.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ def _iter_test_indices(self, XYs):
)
)
sloo = len(XYs) == self.n_splits
lattice = True
lattice = any(XYs.geom_type == 'Polygon') or any(XYs.geom_type == 'MultiPolygon')

# If K = N, SLOO
if sloo:
num_samples = XYs.shape[0]
indices_from_folds = np.arange(num_samples)

else:
# Partion XYs space into folds
XYs_to_2d = geometry_to_2d(XYs)
Expand All @@ -152,10 +151,10 @@ def _iter_test_indices(self, XYs):
fold_polygon = XYs.loc[test_indices].buffer(self.buffer_radius)
elif lattice:
test_indices = np.array(fold_indices)
fold_polygon = XYs.loc[test_indices].unary_union.buffer(self.buffer_radius)
else:
fold_polygon = XYs.loc[test_indices].unary_union.buffer(self.buffer_radius)
else: # skcv
test_indices = np.array(fold_indices)
fold_polygon = XYs.loc[test_indices].unary_union.convex_hull.buffer(self.radius)
fold_polygon = XYs.loc[test_indices].unary_union.convex_hull.buffer(self.buffer_radius)

test_indices, train_exclude = \
super()._remove_buffered_indices(XYs, test_indices,
Expand Down Expand Up @@ -185,8 +184,7 @@ def __init__(
raise ValueError("Number of repetitions must be of Integral type.")
if n_repeats <= 0:
raise ValueError("Number of repetitions must be greater than 0.")
cv = SKCV
self.cv = cv
self.cv = SKCV
self.n_repeats = n_repeats
self.n_splits = n_splits
self.kwargs = kwargs
Expand Down Expand Up @@ -225,7 +223,6 @@ def _iter_test_indices(self, XYs):
grid = self.custom_polygons
grid['grid_id'] = grid.index
grid_ids = np.unique(grid.grid_id)

XYs = assign_pt_to_grid(XYs, grid, self.distance_metric)

# Yield test indices and optionally training indices within buffer
Expand Down
Binary file removed spacv/tests/__pycache__/test_cv.cpython-37.pyc
Binary file not shown.
Binary file removed spacv/tests/__pycache__/test_grids.cpython-37.pyc
Binary file not shown.
Binary file removed spacv/tests/__pycache__/test_viz.cpython-37.pyc
Binary file not shown.
9 changes: 3 additions & 6 deletions spacv/tests/test_cv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys, os
sys.path.append(os.path.abspath("../.."))
import unittest
import numpy as np
import geopandas as gpd
Expand All @@ -11,7 +9,7 @@ def setUp(self):
x = np.random.randint(0, 3000, 30)
y = np.random.randint(0, 3000, 30)

self.gdf = gpd.GeoSeries(
self.gdf = gpd.GeoDataFrame(
{'geometry' : gpd.points_from_xy(x,y)}
)

Expand All @@ -25,16 +23,15 @@ def setUp(self):

def test_skcv(self):
np.random.seed(10)
scv = spacv.SKCV(folds = 3, buffer_radius = 450, random_state=123)
self.assertEqual(scv.folds, 3)
scv = spacv.SKCV(n_splits = 3, buffer_radius = 450, random_state=123)
self.assertEqual(scv.n_splits, 3)
self.assertEqual(scv.buffer_radius, 450)
self.assertEqual(scv.random_state, 123)

fold_train, fold_test = [], []
for train, test in scv.split(self.gdf):
fold_train.append(train)
fold_test.append(test)
print(test)

scv_fold_one = fold_test[0]
scv_fold_two = fold_test[1]
Expand Down
4 changes: 1 addition & 3 deletions spacv/tests/test_grids.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys, os
sys.path.append(os.path.abspath("../.."))
import unittest
import numpy as np
import geopandas as gpd
Expand Down Expand Up @@ -30,7 +28,7 @@ def setUp(self):
pass

def test_systematic_grid(self):

pass

suite = unittest.TestSuite()
test_classes = [SquareUniqueGrid_Tester]
Expand Down
3 changes: 3 additions & 0 deletions spacv/tests/test_viz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import matplotlib

matplotlib.use("agg") # To prevent plots from using display
8 changes: 6 additions & 2 deletions spacv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def convert_geoseries(XYs):
if isinstance(XYs, gpd.GeoDataFrame):
XYs = XYs.geometry
if isinstance(XYs, np.ndarray):
XYs= gpd.GeoSeries( gpd.points_from_xy(XYs) )
XYs = gpd.GeoSeries( gpd.points_from_xy(XYs) )

if isinstance(XYs, gpd.GeoDataFrame):
if any(XYs.geom_type == 'Polygon') or any(XYs.geom_type == 'MultiPolygon'):
XYs = XYs.geometry.centroid
return XYs

def convert_geodataframe(XYs):
Expand All @@ -19,7 +23,7 @@ def convert_geodataframe(XYs):
if isinstance(XYs, np.ndarray):
XYs = gpd.GeoDataFrame({'geometry': gpd.points_from_xy(XYs) })
if isinstance(XYs, (Point, Polygon, LineString, MultiPolygon)):
XYs = gpd.GeoDataFrame({'geometry': [XYs] })
XYs = gpd.GeoDataFrame({'geometry': [XYs] })
return XYs

def convert_numpy(X):
Expand Down
Loading

0 comments on commit c9009b1

Please sign in to comment.