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 type signatures to jenksy #119

Merged
merged 6 commits into from
Feb 2, 2022
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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ mapclassify/datasets/calemp/.ropeproject/
mapclassify/tests/.ropeproject/
.DS_Store
.vscode/settings.json
__pycache__
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include LICENSE.txt CHANGELOG.md MANIFEST.in requirements_tests.txt requirements.txt requirements_docs.txt
include LICENSE.txt CHANGELOG.md MANIFEST.in requirements_tests.txt requirements_speedups.txt requirements.txt requirements_docs.txt
include mapclassify/_version.py
include versioneer.py
34 changes: 21 additions & 13 deletions mapclassify/classifiers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
A module of classification schemes for choropleth mapping.
"""
import functools
import numpy as np
import scipy.stats as stats
import copy
Expand Down Expand Up @@ -57,11 +58,17 @@
FMT = "{:.2f}"

try:
from numba import jit
from numba import njit
HAS_NUMBA = True
except ImportError:

def jit(func):
return func
HAS_NUMBA = False
def njit(type, cache):
def decorator_njit(func):
@functools.wraps(func)
def wrapper_decorator(*args, **kwargs):
return func(*args, **kwargs)
return wrapper_decorator
return decorator_njit


def _format_intervals(mc, fmt="{:.0f}"):
Expand Down Expand Up @@ -508,8 +515,8 @@ def natural_breaks(values, k=5, init=10):
return (sids, class_ids, fit, cuts)


@jit
def _fisher_jenks_means(values, classes=5, sort=True):
@njit("f8[:](f8[:], u2)", cache=True)
def _fisher_jenks_means(values, classes=5):
"""
Jenks Optimal (Natural Breaks) algorithm implemented in Python.

Expand All @@ -524,8 +531,6 @@ def _fisher_jenks_means(values, classes=5, sort=True):
assuring heterogeneity among classes.

"""
if sort:
values.sort()
n_data = len(values)
mat1 = np.zeros((n_data + 1, classes + 1), dtype=np.int32)
mat2 = np.zeros((n_data + 1, classes + 1), dtype=np.float32)
Expand Down Expand Up @@ -563,7 +568,7 @@ def _fisher_jenks_means(values, classes=5, sort=True):
id = int(pivot - 2)
kclass[countNum - 1] = values[id]
k = int(pivot - 1)
return kclass
return np.delete(kclass, 0)


class MapClassifier(object):
Expand Down Expand Up @@ -1762,8 +1767,8 @@ class FisherJenks(MapClassifier):
----------
y : array
(n,1), values to classify
k : int
number of classes required
k : int, optional
number of classes, defatuls to 5

Attributes
----------
Expand Down Expand Up @@ -1791,6 +1796,9 @@ class FisherJenks(MapClassifier):
"""

def __init__(self, y, k=K):
if not HAS_NUMBA:
Warn("Numba not installed. Using slow pure python version.",
UserWarning)

nu = len(np.unique(y))
if nu < k:
Expand All @@ -1800,8 +1808,8 @@ def __init__(self, y, k=K):
self.name = "FisherJenks"

def _set_bins(self):
x = self.y.copy()
self.bins = np.array(_fisher_jenks_means(x, classes=self.k)[1:])
x = np.sort(self.y).astype("f8")
self.bins = _fisher_jenks_means(x, classes=self.k)


class FisherJenksSampled(MapClassifier):
Expand Down
1 change: 1 addition & 0 deletions requirements_speedups.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numba
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def get_data_files():
def setup_package():
_groups_files = {
"base": "requirements.txt",
"speedups": "requirements_speedups.txt",
"tests": "requirements_tests.txt",
"docs": "requirements_docs.txt",
}
Expand Down