Skip to content

Commit c8120ca

Browse files
committed
fix flake8 issues for the learners
1 parent 67a5271 commit c8120ca

10 files changed

Lines changed: 33 additions & 35 deletions

adaptive/learner/average_learner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
import itertools
32
from math import sqrt
43

54
import numpy as np

adaptive/learner/balancing_learner.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self, learners, *, cdims=None, strategy='loss_improvements'):
7676
@property
7777
def strategy(self):
7878
return self._strategy
79-
79+
8080
@strategy.setter
8181
def strategy(self, strategy):
8282
self._strategy = strategy
@@ -87,8 +87,9 @@ def strategy(self, strategy):
8787
elif strategy == 'npoints':
8888
self._ask_and_tell = self._ask_and_tell_based_on_npoints
8989
else:
90-
raise ValueError('Only strategy="loss_improvements",'
91-
' strategy="loss", or strategy="npoints" is implemented.')
90+
raise ValueError(
91+
'Only strategy="loss_improvements", strategy="loss", or'
92+
' strategy="npoints" is implemented.')
9293

9394
def _ask_and_tell_based_on_loss_improvements(self, n):
9495
points = []

adaptive/learner/base_learner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
import abc
3-
import collections
43
from copy import deepcopy
54

65

adaptive/learner/data_saver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import OrderedDict
33
import functools
44

5+
56
class DataSaver:
67
"""Save extra data associated with the values that need to be learned.
78

adaptive/learner/integrator_learner.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def calc_ndiv(self):
206206
div = (self.parent.c00 and self.c00 / self.parent.c00 > 2)
207207
self.ndiv += div
208208

209-
if self.ndiv > ndiv_max and 2*self.ndiv > self.rdepth:
209+
if self.ndiv > ndiv_max and 2 * self.ndiv > self.rdepth:
210210
raise DivergentIntegralError
211211

212212
if div:
@@ -215,7 +215,7 @@ def calc_ndiv(self):
215215

216216
def update_ndiv_recursively(self):
217217
self.ndiv += 1
218-
if self.ndiv > ndiv_max and 2*self.ndiv > self.rdepth:
218+
if self.ndiv > ndiv_max and 2 * self.ndiv > self.rdepth:
219219
raise DivergentIntegralError
220220

221221
for child in self.children:
@@ -418,7 +418,6 @@ def add_ival(self, ival):
418418
self._stack.append(x)
419419
self.ivals.add(ival)
420420

421-
422421
def ask(self, n, tell_pending=True):
423422
"""Choose points for learners."""
424423
if not tell_pending:

adaptive/learner/learner1D.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def default_loss(interval, scale, function_values):
4949
else:
5050
dy = (y_right - y_left) / y_scale
5151
try:
52-
_ = len(dy)
52+
len(dy)
5353
loss = np.hypot(dx, dy).max()
5454
except TypeError:
5555
loss = math.hypot(dx, dy)
@@ -409,7 +409,7 @@ def finite_loss(loss, xs):
409409
# If the loss is infinite we return the
410410
# distance between the two points.
411411
return (loss if not math.isinf(loss)
412-
else (xs[1] - xs[0]) / self._scale[0])
412+
else (xs[1] - xs[0]) / self._scale[0])
413413

414414
quals = [(-finite_loss(loss, x), x, 1)
415415
for x, loss in self.losses_combined.items()]

adaptive/learner/learner2D.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
from collections import OrderedDict
3-
from copy import copy
43
import itertools
54
from math import sqrt
65

@@ -86,13 +85,13 @@ def resolution_loss(ip, min_distance=0, max_distance=1):
8685
"""
8786
A = areas(ip)
8887
dev = np.sum(deviations(ip), axis=0)
89-
88+
9089
# similar to the default_loss
9190
loss = np.sqrt(A) * dev + A
92-
91+
9392
# Setting areas with a small area to zero such that they won't be chosen again
94-
loss[A < min_distance**2] = 0
95-
93+
loss[A < min_distance**2] = 0
94+
9695
# Setting triangles that have a size larger than max_distance to infinite loss
9796
# such that these triangles will be picked
9897
loss[A > max_distance**2] = np.inf
@@ -109,7 +108,7 @@ def minimize_triangle_surface_loss(ip):
109108
110109
Examples
111110
--------
112-
>>> from adaptive.learner.learner2D import minimize_triangle_surface_loss
111+
>>> from adaptive.learner.learner2D import minimize_triangle_surface_loss
113112
>>> def f(xy):
114113
... x, y = xy
115114
... return x**2 + y**2

adaptive/learner/learnerND.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def default_loss(simplex, ys):
5959
def choose_point_in_simplex(simplex, transform=None):
6060
"""Choose a new point in inside a simplex.
6161
62-
Pick the center of the simplex if the shape is nice (that is, the
63-
circumcenter lies within the simplex). Otherwise take the middle of the
62+
Pick the center of the simplex if the shape is nice (that is, the
63+
circumcenter lies within the simplex). Otherwise take the middle of the
6464
longest edge.
6565
6666
Parameters
@@ -94,7 +94,7 @@ def choose_point_in_simplex(simplex, transform=None):
9494

9595
if transform is not None:
9696
point = np.linalg.solve(transform, point) # undo the transform
97-
97+
9898
return point
9999

100100

@@ -180,7 +180,7 @@ def __init__(self, func, bounds, loss_per_simplex=None):
180180
# create a private random number generator with fixed seed
181181
self._random = random.Random(1)
182182

183-
# all real triangles that have not been subdivided and the pending
183+
# all real triangles that have not been subdivided and the pending
184184
# triangles heap of tuples (-loss, real simplex, sub_simplex or None)
185185

186186
# _simplex_queue is a heap of tuples (-loss, real_simplex, sub_simplex)
@@ -268,7 +268,7 @@ def _simplex_exists(self, simplex):
268268

269269
def inside_bounds(self, point):
270270
return all(mn <= p <= mx for p, (mn, mx) in zip(point, self.bounds))
271-
271+
272272
def tell_pending(self, point, *, simplex=None):
273273
point = tuple(point)
274274
if not self.inside_bounds(point):
@@ -354,21 +354,21 @@ def _pop_highest_existing_simplex(self):
354354
# find the simplex with the highest loss, we do need to check that the
355355
# simplex hasn't been deleted yet
356356
while len(self._simplex_queue):
357-
loss, simplex, subsimplex = heapq.heappop(self._simplex_queue)
357+
loss, simplex, subsimplex = heapq.heappop(self._simplex_queue)
358358
if (subsimplex is None
359-
and simplex in self.tri.simplices
360-
and simplex not in self._subtriangulations):
359+
and simplex in self.tri.simplices
360+
and simplex not in self._subtriangulations):
361361
return abs(loss), simplex, subsimplex
362362
if (simplex in self._subtriangulations
363-
and simplex in self.tri.simplices
364-
and subsimplex in self._subtriangulations[simplex].simplices):
363+
and simplex in self.tri.simplices
364+
and subsimplex in self._subtriangulations[simplex].simplices):
365365
return abs(loss), simplex, subsimplex
366366

367367
# Could not find a simplex, this code should never be reached
368368
assert self.tri is not None
369369
raise AssertionError(
370-
"Could not find a simplex to subdivide. Yet there should always be"
371-
"a simplex available if LearnerND.tri() is not None."
370+
"Could not find a simplex to subdivide. Yet there should always"
371+
" be a simplex available if LearnerND.tri() is not None."
372372
)
373373

374374
def _ask_best_point(self):
@@ -435,8 +435,8 @@ def update_losses(self, to_delete: set, to_add: set):
435435
heapq.heappush(self._simplex_queue, (-loss, simplex, None))
436436
continue
437437

438-
self._update_subsimplex_losses(simplex,
439-
self._subtriangulations[simplex].simplices)
438+
self._update_subsimplex_losses(
439+
simplex, self._subtriangulations[simplex].simplices)
440440

441441
def losses(self):
442442
"""Get the losses of each simplex in the current triangulation, as dict

adaptive/learner/skopt_learner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .base_learner import BaseLearner
66
from ..notebook_integration import ensure_holoviews
7-
from ..utils import restore, cache_latest
7+
from ..utils import cache_latest
88

99

1010
class SKOptLearner(Optimizer, BaseLearner):

adaptive/learner/triangulation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def simplex_volume_in_embedding(vertices) -> float:
214214
# Heron's formula
215215
a, b, c = scipy.spatial.distance.pdist(vertices, metric='euclidean')
216216
s = 0.5 * (a + b + c)
217-
return math.sqrt(s*(s-a)*(s-b)*(s-c))
217+
return math.sqrt(s * (s - a) * (s - b) * (s - c))
218218

219219
# β_ij = |v_i - v_k|²
220220
sq_dists = scipy.spatial.distance.pdist(vertices, metric='sqeuclidean')
@@ -494,13 +494,13 @@ def bowyer_watson(self, pt_index, containing_simplex=None, transform=None):
494494

495495
# Get all simplices that share at least a point with the simplex
496496
neighbours = set.union(*[self.vertex_to_simplices[p]
497-
for p in todo_points])
497+
for p in todo_points])
498498
# Filter out the already evaluated simplices
499499
neighbours = neighbours - done_simplices
500500

501501
# Keep only the simplices sharing a whole face with the current simplex
502502
neighbours = set(
503-
simpl for simpl in neighbours
503+
simpl for simpl in neighbours
504504
if len(set(simpl) & set(simplex)) == self.dim # they share a face
505505
)
506506
queue.update(neighbours)
@@ -568,7 +568,7 @@ def add_point(self, point, simplex=None, transform=None):
568568
else:
569569
reduced_simplex = self.get_reduced_simplex(point, simplex)
570570
if not reduced_simplex:
571-
self.vertex_to_simplices.pop() # revert adding vertex
571+
self.vertex_to_simplices.pop() # revert adding vertex
572572
raise ValueError('Point lies outside of the specified simplex.')
573573
else:
574574
simplex = reduced_simplex

0 commit comments

Comments
 (0)