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

Add spenc as a lib rather than using via an import #93

Merged
merged 2 commits into from
Jan 11, 2021
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
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