Browse code

change the 'n' attribute of learners to 'npoints'

Previously this attribute was meant to be an implementation
detail, but it is useful information and should be made public.
Given that it is public information, it should have an informative
name.

Joseph Weston authored on 16/02/2018 17:50:48
Showing 6 changed files
... ...
@@ -32,7 +32,7 @@ class AverageLearner(BaseLearner):
32 32
         self.function = function
33 33
         self.atol = atol
34 34
         self.rtol = rtol
35
-        self.n = 0
35
+        self.npoints = 0
36 36
         self.sum_f = 0
37 37
         self.sum_f_sq = 0
38 38
 
... ...
@@ -56,25 +56,25 @@ class AverageLearner(BaseLearner):
56 56
             self.sum_f += value
57 57
             self.sum_f_sq += value**2
58 58
             if value_is_new:
59
-                self.n += 1
59
+                self.npoints += 1
60 60
             else:
61 61
                 self.sum_f -= value_old
62 62
                 self.sum_f_sq -= value_old**2
63 63
 
64 64
     @property
65 65
     def mean(self):
66
-        return self.sum_f / self.n
66
+        return self.sum_f / self.npoints
67 67
 
68 68
     @property
69 69
     def std(self):
70
-        n = self.n
70
+        n = self.npoints
71 71
         if n < 2:
72 72
             return np.inf
73 73
         return sqrt((self.sum_f_sq - n * self.mean**2) / (n - 1))
74 74
 
75 75
     def loss(self, real=True, *, n=None):
76 76
         if n is None:
77
-            n = self.n if real else self.n_requested
77
+            n = self.npoints if real else self.n_requested
78 78
         else:
79 79
             n = n
80 80
         if n < 2:
... ...
@@ -86,7 +86,7 @@ class AverageLearner(BaseLearner):
86 86
     def loss_improvement(self, n):
87 87
         loss = self.loss()
88 88
         if np.isfinite(loss):
89
-            return loss - self.loss(n=self.n + n)
89
+            return loss - self.loss(n=self.npoints + n)
90 90
         else:
91 91
             return np.inf
92 92
 
... ...
@@ -99,6 +99,6 @@ class AverageLearner(BaseLearner):
99 99
         vals = [v for v in self.data.values() if v is not None]
100 100
         if not vals:
101 101
             return hv.Histogram([[], []])
102
-        num_bins = int(max(5, sqrt(self.n)))
102
+        num_bins = int(max(5, sqrt(self.npoints)))
103 103
         vals = hv.Points(vals)
104 104
         return hv.operation.histogram(vals, num_bins=num_bins, dimension=1)
... ...
@@ -15,7 +15,7 @@ class BaseLearner(metaclass=abc.ABCMeta):
15 15
         'function' evaluated at certain points.
16 16
         The values can be 'None', which indicates that the point
17 17
         will be evaluated, but that we do not have the result yet.
18
-    n : int, optional
18
+    npoints : int, optional
19 19
         The number of evaluated points that have been added to the learner.
20 20
         Subclasses do not *have* to implement this attribute.
21 21
 
... ...
@@ -100,7 +100,7 @@ class Learner1D(BaseLearner):
100 100
         return {**self.data, **self.data_interp}
101 101
 
102 102
     @property
103
-    def n(self):
103
+    def npoints(self):
104 104
         return len(self.data)
105 105
 
106 106
     def loss(self, real=True):
... ...
@@ -178,7 +178,7 @@ class Learner2D(BaseLearner):
178 178
         self.stack_size = 10
179 179
 
180 180
     @property
181
-    def n(self):
181
+    def npoints(self):
182 182
         return len(self.data)
183 183
 
184 184
     @property
... ...
@@ -177,7 +177,7 @@ def _info_html(runner):
177 177
     ]
178 178
 
179 179
     try:
180
-        info.append(('# of points', runner.learner.n))
180
+        info.append(('# of points', runner.learner.npoints))
181 181
     except Exception:
182 182
         pass
183 183
 
... ...
@@ -208,7 +208,7 @@
208 208
    "source": [
209 209
     "def plot(learner):\n",
210 210
     "    plot = learner.plot(tri_alpha=0.2)\n",
211
-    "    title = f'loss={learner._loss:.3f}, n_points={learner.n}'\n",
211
+    "    title = f'loss={learner._loss:.3f}, n_points={learner.npoints}'\n",
212 212
     "    opts = dict(plot=dict(title_format=title))\n",
213 213
     "    return plot.Image + plot.EdgePaths.I.clone().opts(**opts) + plot\n",
214 214
     "\n",
... ...
@@ -223,7 +223,7 @@
223 223
    "source": [
224 224
     "import itertools\n",
225 225
     "learner2 = adaptive.Learner2D(ring, bounds=learner.bounds)\n",
226
-    "n = int(learner.n**0.5)\n",
226
+    "n = int(learner.npoints**0.5)\n",
227 227
     "xs, ys = [np.linspace(*bounds, n) for bounds in learner.bounds]\n",
228 228
     "xys = list(itertools.product(xs, ys))\n",
229 229
     "learner2.add_data(xys, map(partial(ring, wait=False), xys))\n",