From b212ead382b82eb55a2eb452a588491b41f09f7c Mon Sep 17 00:00:00 2001 From: cheginit Date: Thu, 9 Dec 2021 21:02:36 -0600 Subject: [PATCH 1/5] ENH: Ignore the pycache folder --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 48ebe956..ad8665a7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ mapclassify/datasets/calemp/.ropeproject/ mapclassify/tests/.ropeproject/ .DS_Store .vscode/settings.json +__pycache__ From 134f2dc41215906ef83bfb05a437ab9143836221 Mon Sep 17 00:00:00 2001 From: cheginit Date: Thu, 9 Dec 2021 21:03:15 -0600 Subject: [PATCH 2/5] ENH: Add numba as an extra requirement --- requirements_speedups.txt | 1 + setup.py | 1 + 2 files changed, 2 insertions(+) create mode 100644 requirements_speedups.txt diff --git a/requirements_speedups.txt b/requirements_speedups.txt new file mode 100644 index 00000000..c3db4451 --- /dev/null +++ b/requirements_speedups.txt @@ -0,0 +1 @@ +numba diff --git a/setup.py b/setup.py index c8b50a44..7eef40d8 100644 --- a/setup.py +++ b/setup.py @@ -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", } From 77da3295a502c59b326a806d59b9f10d77c2ee59 Mon Sep 17 00:00:00 2001 From: cheginit Date: Thu, 9 Dec 2021 21:05:11 -0600 Subject: [PATCH 3/5] ENH: Add type signatures to the jenksy function to speed it up. Also, add a warning if numba is not installed. (#118) --- mapclassify/classifiers.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/mapclassify/classifiers.py b/mapclassify/classifiers.py index e34530af..60fbbd4e 100644 --- a/mapclassify/classifiers.py +++ b/mapclassify/classifiers.py @@ -57,10 +57,11 @@ FMT = "{:.2f}" try: - from numba import jit + from numba import njit + HAS_NUMBA = True except ImportError: - - def jit(func): + HAS_NUMBA = False + def njit(func): return func @@ -507,8 +508,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. @@ -523,8 +524,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) @@ -562,7 +561,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): @@ -1761,8 +1760,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 ---------- @@ -1790,6 +1789,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: @@ -1799,8 +1801,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): From 7f621ca6bd787180c496228d3994450f26648601 Mon Sep 17 00:00:00 2001 From: cheginit Date: Fri, 10 Dec 2021 11:22:27 -0600 Subject: [PATCH 4/5] BUG: Fix the decorator function issue when numba is not installed --- mapclassify/classifiers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mapclassify/classifiers.py b/mapclassify/classifiers.py index 60fbbd4e..9ab6433c 100644 --- a/mapclassify/classifiers.py +++ b/mapclassify/classifiers.py @@ -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 @@ -61,8 +62,13 @@ HAS_NUMBA = True except ImportError: HAS_NUMBA = False - def njit(func): - return func + 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}"): From b45c9adb5250bfa7d09bfc38e8b804858a178c33 Mon Sep 17 00:00:00 2001 From: cheginit Date: Sat, 11 Dec 2021 00:23:31 -0600 Subject: [PATCH 5/5] ENH: Add the speedup req file so it can be installed via pip --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index fd8a2264..05afe7df 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -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