From 8ef0947390af2128d792a67c265776bfb4b2870c Mon Sep 17 00:00:00 2001 From: Serge Rey Date: Wed, 29 Mar 2023 20:05:32 -0700 Subject: [PATCH] Docs and testing for anchoring StdMean --- mapclassify/classifiers.py | 11 ++-- mapclassify/tests/test_mapclassify.py | 12 ++--- notebooks/06_api.ipynb | 78 +++++++++++++++++++-------- 3 files changed, 68 insertions(+), 33 deletions(-) diff --git a/mapclassify/classifiers.py b/mapclassify/classifiers.py index 5e0de6ba..069ab731 100644 --- a/mapclassify/classifiers.py +++ b/mapclassify/classifiers.py @@ -1543,11 +1543,12 @@ class StdMean(MapClassifier): Notes ----- - If anchor is True, the closed upper bound of the first class is set to - min(y)+std(y), while the open lower bound of the last class is set to - max(y)-std(y). k is ignored if anchor is True, and is determined - by the number of standar deviations required to span the range of - y. multiples is also ignored if anchor is True. + If anchor is True, one of the intervals will its closed upper bound equal to + the mean of y. Intermediate intervals will have widths equal to the standard + deviation of y. The first interval will be closed on the minimum value of + y, and the last interval will be closed on the maximum of y. The first and + last intervals may have widths different from the intermediate intervals. + Examples -------- diff --git a/mapclassify/tests/test_mapclassify.py b/mapclassify/tests/test_mapclassify.py index eac5975c..e99928f9 100644 --- a/mapclassify/tests/test_mapclassify.py +++ b/mapclassify/tests/test_mapclassify.py @@ -623,13 +623,13 @@ def test_UserDefined_invariant(self): numpy.testing.assert_array_almost_equal(ud.counts, numpy.array([0, 3, 0, 0])) -class TestStdMeanAnchor(unittest.TestCase): - def setUp(self): +class TestStdMeanAnchor: + def setup_method(self): self.V = load_example() def test_StdMeanAnchor(self): sm = StdMean(self.V, anchor=True) - bins = np.array( + bins = numpy.array( [ 125.92810345, 672.57333208, @@ -642,9 +642,9 @@ def test_StdMeanAnchor(self): 4111.45, ] ) - counts = np.array([50, 6, 1, 0, 0, 0, 0, 0, 1]) - np.testing.assert_array_almost_equal(sm.bins, bins) - np.testing.assert_array_almost_equal(sm.counts, counts) + counts = numpy.array([50, 6, 1, 0, 0, 0, 0, 0, 1]) + numpy.testing.assert_array_almost_equal(sm.bins, bins) + numpy.testing.assert_array_almost_equal(sm.counts, counts) class TestMaxP: diff --git a/notebooks/06_api.ipynb b/notebooks/06_api.ipynb index bf7a8787..1eae8705 100644 --- a/notebooks/06_api.ipynb +++ b/notebooks/06_api.ipynb @@ -23,7 +23,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.167785Z", "start_time": "2022-11-05T15:10:14.404320Z" - } + }, + "tags": [] }, "outputs": [], "source": [ @@ -46,13 +47,14 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.182165Z", "start_time": "2022-11-05T15:10:19.171353Z" - } + }, + "tags": [] }, "outputs": [ { "data": { "text/plain": [ - "'2.4.2+78.gc62d2d7.dirty'" + "'2.4.2+107.gb97c316a.dirty'" ] }, "execution_count": 2, @@ -71,7 +73,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.586837Z", "start_time": "2022-11-05T15:10:19.187232Z" - } + }, + "tags": [] }, "outputs": [ { @@ -295,7 +298,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.595711Z", "start_time": "2022-11-05T15:10:19.589037Z" - } + }, + "tags": [] }, "outputs": [ { @@ -339,7 +343,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.603460Z", "start_time": "2022-11-05T15:10:19.598526Z" - } + }, + "tags": [] }, "outputs": [ { @@ -374,7 +379,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.611996Z", "start_time": "2022-11-05T15:10:19.608075Z" - } + }, + "tags": [] }, "outputs": [ { @@ -399,7 +405,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.619168Z", "start_time": "2022-11-05T15:10:19.614412Z" - } + }, + "tags": [] }, "outputs": [ { @@ -440,7 +447,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.627988Z", "start_time": "2022-11-05T15:10:19.621853Z" - } + }, + "tags": [] }, "outputs": [ { @@ -474,7 +482,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.634396Z", "start_time": "2022-11-05T15:10:19.629847Z" - } + }, + "tags": [] }, "outputs": [ { @@ -508,7 +517,8 @@ "ExecuteTime": { "end_time": "2022-11-05T15:10:19.641115Z", "start_time": "2022-11-05T15:10:19.636017Z" - } + }, + "tags": [] }, "outputs": [ { @@ -537,18 +547,19 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2022-11-05T15:10:19.691302Z", "start_time": "2022-11-05T15:10:19.645124Z" - } + }, + "tags": [] }, "outputs": [ { "data": { "text/plain": [ - "StdMean \n", + "StdMean\n", "\n", " Interval Count\n", "----------------------\n", @@ -559,29 +570,30 @@ "(75.37, 96.40] | 4" ] }, - "execution_count": 10, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "classify(y, 'Std_Mean')" + "mapclassify.classify(y, 'Std_Mean')" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2022-10-26T03:01:45.977181Z", "start_time": "2022-10-26T03:01:45.931234Z" - } + }, + "tags": [] }, "outputs": [ { "data": { "text/plain": [ - "StdMean \n", + "StdMean\n", "\n", " Interval Count\n", "----------------------\n", @@ -593,13 +605,35 @@ "(93.83, 96.40] | 1" ] }, - "execution_count": 11, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mapclassify.classify(y, 'Std_Mean', anchor=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(38.43622446938775, 18.466069465206047, 17.9, 96.400002)" + ] + }, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "classify(y, 'Std_Mean', anchor=True)" + "y.mean(), y.std(), y.min(), y.max()" ] }, { @@ -626,7 +660,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.10.10" } }, "nbformat": 4,