5
5
from collections import OrderedDict
6
6
from copy import copy
7
7
from math import sqrt
8
- from typing import Callable , Iterable
8
+ from typing import TYPE_CHECKING , Callable , Iterable
9
9
10
10
import cloudpickle
11
11
import numpy as np
22
22
partial_function_from_dataframe ,
23
23
)
24
24
25
+ if TYPE_CHECKING :
26
+ import holoviews
27
+
25
28
try :
26
29
import pandas
27
30
@@ -40,11 +43,11 @@ def deviations(ip: LinearNDInterpolator) -> list[np.ndarray]:
40
43
41
44
Parameters
42
45
----------
43
- ip : `scipy.interpolate.LinearNDInterpolator` instance
46
+ ip
44
47
45
48
Returns
46
49
-------
47
- deviations : list
50
+ deviations
48
51
The deviation per triangle.
49
52
"""
50
53
values = ip .values / (ip .values .ptp (axis = 0 ).max () or 1 )
@@ -79,11 +82,11 @@ def areas(ip: LinearNDInterpolator) -> np.ndarray:
79
82
80
83
Parameters
81
84
----------
82
- ip : `scipy.interpolate.LinearNDInterpolator` instance
85
+ ip
83
86
84
87
Returns
85
88
-------
86
- areas : numpy.ndarray
89
+ areas
87
90
The area per triangle in ``ip.tri``.
88
91
"""
89
92
p = ip .tri .points [ip .tri .simplices ]
@@ -99,11 +102,11 @@ def uniform_loss(ip: LinearNDInterpolator) -> np.ndarray:
99
102
100
103
Parameters
101
104
----------
102
- ip : `scipy.interpolate.LinearNDInterpolator` instance
105
+ ip
103
106
104
107
Returns
105
108
-------
106
- losses : numpy.ndarray
109
+ losses
107
110
Loss per triangle in ``ip.tri``.
108
111
109
112
Examples
@@ -136,7 +139,7 @@ def resolution_loss_function(
136
139
137
140
Returns
138
141
-------
139
- loss_function : callable
142
+ loss_function
140
143
141
144
Examples
142
145
--------
@@ -173,11 +176,11 @@ def minimize_triangle_surface_loss(ip: LinearNDInterpolator) -> np.ndarray:
173
176
174
177
Parameters
175
178
----------
176
- ip : `scipy.interpolate.LinearNDInterpolator` instance
179
+ ip
177
180
178
181
Returns
179
182
-------
180
- losses : numpy.ndarray
183
+ losses
181
184
Loss per triangle in ``ip.tri``.
182
185
183
186
Examples
@@ -217,11 +220,11 @@ def default_loss(ip: LinearNDInterpolator) -> np.ndarray:
217
220
218
221
Parameters
219
222
----------
220
- ip : `scipy.interpolate.LinearNDInterpolator` instance
223
+ ip
221
224
222
225
Returns
223
226
-------
224
- losses : numpy.ndarray
227
+ losses
225
228
Loss per triangle in ``ip.tri``.
226
229
"""
227
230
dev = np .sum (deviations (ip ), axis = 0 )
@@ -241,15 +244,15 @@ def choose_point_in_triangle(triangle: np.ndarray, max_badness: int) -> np.ndarr
241
244
242
245
Parameters
243
246
----------
244
- triangle : numpy.ndarray
247
+ triangle
245
248
The coordinates of a triangle with shape (3, 2).
246
- max_badness : int
249
+ max_badness
247
250
The badness at which the point is either chosen on a edge or
248
251
in the middle.
249
252
250
253
Returns
251
254
-------
252
- point : numpy.ndarray
255
+ point
253
256
The x and y coordinate of the suggested new point.
254
257
"""
255
258
a , b , c = triangle
@@ -267,17 +270,17 @@ def choose_point_in_triangle(triangle: np.ndarray, max_badness: int) -> np.ndarr
267
270
return point
268
271
269
272
270
- def triangle_loss (ip ) :
273
+ def triangle_loss (ip : LinearNDInterpolator ) -> list [ float ] :
271
274
r"""Computes the average of the volumes of the simplex combined with each
272
275
neighbouring point.
273
276
274
277
Parameters
275
278
----------
276
- ip : `scipy.interpolate.LinearNDInterpolator` instance
279
+ ip
277
280
278
281
Returns
279
282
-------
280
- triangle_loss : list
283
+ triangle_loss
281
284
The mean volume per triangle.
282
285
283
286
Notes
@@ -311,13 +314,13 @@ class Learner2D(BaseLearner):
311
314
312
315
Parameters
313
316
----------
314
- function : callable
317
+ function
315
318
The function to learn. Must take a tuple of two real
316
319
parameters and return a real number.
317
- bounds : list of 2-tuples
320
+ bounds
318
321
A list ``[(a1, b1), (a2, b2)]`` containing bounds,
319
322
one per dimension.
320
- loss_per_triangle : callable, optional
323
+ loss_per_triangle
321
324
A function that returns the loss for every triangle.
322
325
If not provided, then a default is used, which uses
323
326
the deviation from a linear estimate, as well as
@@ -424,19 +427,19 @@ def to_dataframe(
424
427
425
428
Parameters
426
429
----------
427
- with_default_function_args : bool, optional
430
+ with_default_function_args
428
431
Include the ``learner.function``'s default arguments as a
429
432
column, by default True
430
- function_prefix : str, optional
433
+ function_prefix
431
434
Prefix to the ``learner.function``'s default arguments' names,
432
435
by default "function."
433
- seed_name : str, optional
436
+ seed_name
434
437
Name of the seed parameter, by default "seed"
435
- x_name : str, optional
438
+ x_name
436
439
Name of the input x value, by default "x"
437
- y_name : str, optional
440
+ y_name
438
441
Name of the input y value, by default "y"
439
- z_name : str, optional
442
+ z_name
440
443
Name of the output value, by default "z"
441
444
442
445
Returns
@@ -475,18 +478,18 @@ def load_dataframe(
475
478
476
479
Parameters
477
480
----------
478
- df : pandas.DataFrame
481
+ df
479
482
The data to load.
480
- with_default_function_args : bool, optional
483
+ with_default_function_args
481
484
The ``with_default_function_args`` used in ``to_dataframe()``,
482
485
by default True
483
- function_prefix : str, optional
486
+ function_prefix
484
487
The ``function_prefix`` used in ``to_dataframe``, by default "function."
485
- x_name : str, optional
488
+ x_name
486
489
The ``x_name`` used in ``to_dataframe``, by default "x"
487
- y_name : str, optional
490
+ y_name
488
491
The ``y_name`` used in ``to_dataframe``, by default "y"
489
- z_name : str, optional
492
+ z_name
490
493
The ``z_name`` used in ``to_dataframe``, by default "z"
491
494
"""
492
495
data = df .set_index ([x_name , y_name ])[z_name ].to_dict ()
@@ -538,7 +541,7 @@ def interpolated_on_grid(
538
541
539
542
Parameters
540
543
----------
541
- n : int, optional
544
+ n
542
545
Number of points in x and y. If None (default) this number is
543
546
evaluated by looking at the size of the smallest triangle.
544
547
@@ -611,14 +614,14 @@ def interpolator(self, *, scaled: bool = False) -> LinearNDInterpolator:
611
614
612
615
Parameters
613
616
----------
614
- scaled : bool
617
+ scaled
615
618
Use True if all points are inside the
616
619
unit-square [(-0.5, 0.5), (-0.5, 0.5)] or False if
617
620
the data points are inside the ``learner.bounds``.
618
621
619
622
Returns
620
623
-------
621
- interpolator : `scipy.interpolate.LinearNDInterpolator`
624
+ interpolator
622
625
623
626
Examples
624
627
--------
@@ -755,7 +758,9 @@ def remove_unfinished(self) -> None:
755
758
if p not in self .data :
756
759
self ._stack [p ] = np .inf
757
760
758
- def plot (self , n = None , tri_alpha = 0 ):
761
+ def plot (
762
+ self , n : int = None , tri_alpha : float = 0
763
+ ) -> holoviews .Overlay | holoviews .HoloMap :
759
764
r"""Plot the Learner2D's current state.
760
765
761
766
This plot function interpolates the data on a regular grid.
@@ -764,16 +769,16 @@ def plot(self, n=None, tri_alpha=0):
764
769
765
770
Parameters
766
771
----------
767
- n : int
772
+ n
768
773
Number of points in x and y. If None (default) this number is
769
774
evaluated by looking at the size of the smallest triangle.
770
- tri_alpha : float
775
+ tri_alpha
771
776
The opacity ``(0 <= tri_alpha <= 1)`` of the triangles overlayed
772
777
on top of the image. By default the triangulation is not visible.
773
778
774
779
Returns
775
780
-------
776
- plot : `holoviews.core.Overlay` or `holoviews.core.HoloMap`
781
+ plot
777
782
A `holoviews.core.Overlay` of
778
783
``holoviews.Image * holoviews.EdgePaths``. If the
779
784
`learner.function` returns a vector output, a
0 commit comments