Skip to content

Commit

Permalink
Merge pull request #93 from sjsrey/spenc
Browse files Browse the repository at this point in the history
Add spenc as a lib rather than using via an import
  • Loading branch information
jGaboardi committed Jan 11, 2021
2 parents 8e2b907 + beddc7e commit 46b9056
Show file tree
Hide file tree
Showing 6 changed files with 701 additions and 43 deletions.
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ dependencies:
- geopandas>=0.7.0
- matplotlib
- scikit-learn=0.22
- spenc
- pytest
- pytest-cov
- pulp
Expand Down
85 changes: 43 additions & 42 deletions spopt/region/spenc.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
from ..BaseClass import BaseSpOptHeuristicSolver
import numpy as np
from warnings import warn
from spenc import SPENC
from .spenclib import SPENC


class Spenc(BaseSpOptHeuristicSolver):
"""
Spatially encouraged spectral clustering.
:cite:`wolf2018`
"""
def __init__(self, gdf, w, attrs_name, n_clusters=5, random_state=None, gamma=1):
"""
Parameters
----------
gdf : geopandas.GeoDataFrame
w : libpywal.weights.W instance
spatial weights matrix
attrs_name : list
Strings for attribute names (cols of ``geopandas.GeoDataFrame``).
n_clusters : int, optional, default: 5
The number of clusters to form.
gamma: int, default:1
"""
self.gdf = gdf
self.w = w
self.attrs_name = attrs_name
self.n_clusters = n_clusters
self.gamma = gamma
self.random_state = random_state

def solve(self):
"""Solve the spenc"""
data = self.gdf
X = data[self.attrs_name].values
#_import_tryer("spenc", "SPENC", "spenc")
model = SPENC(n_clusters=self.n_clusters, random_state=self.random_state, gamma=self.gamma)
model.fit(X, self.w.sparse)
self.labels_ = model.labels_
"""
Spatially encouraged spectral clustering.
:cite:`wolf2018`
"""

def __init__(self, gdf, w, attrs_name, n_clusters=5, random_state=None,
gamma=1):
"""
Parameters
----------
gdf : geopandas.GeoDataFrame
w : libpywal.weights.W instance
spatial weights matrix
attrs_name : list
Strings for attribute names (cols of ``geopandas.GeoDataFrame``).
n_clusters : int, optional, default: 5
The number of clusters to form.
gamma: int, default:1
"""
self.gdf = gdf
self.w = w
self.attrs_name = attrs_name
self.n_clusters = n_clusters
self.gamma = gamma
self.random_state = random_state

def solve(self):
"""Solve the spenc"""
data = self.gdf
X = data[self.attrs_name].values
model = SPENC(n_clusters=self.n_clusters,
random_state=self.random_state,
gamma=self.gamma)
model.fit(X, self.w.sparse)
self.labels_ = model.labels_
1 change: 1 addition & 0 deletions spopt/region/spenclib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .abstracts import SPENC
Loading

0 comments on commit 46b9056

Please sign in to comment.