Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 8d6cfb8

Browse files
author
Tanguy Damart
committed
Some import fixes
1 parent 2c03604 commit 8d6cfb8

File tree

8 files changed

+57
-44
lines changed

8 files changed

+57
-44
lines changed

bluepyopt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import bluepyopt.deapext.algorithms
3333
import bluepyopt.stoppingCriteria
3434
import bluepyopt.deapext.optimisations
35+
import bluepyopt.deapext.optimisationsCMA
3536

3637
# Add some backward compatibility for the time when DEAPoptimisation not in
3738
# deapext yet

bluepyopt/deapext/CMA_MO.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
from deap import cma
3232

3333
from .stoppingCriteria import MaxNGen
34-
35-
from .utils import _bound
34+
from . import utils
3635

3736
logger = logging.getLogger('__main__')
3837

@@ -211,7 +210,7 @@ def get_parents(self, to_space):
211210
def generate_new_pop(self, lbounds, ubounds):
212211
"""Generate a new population bounded in the normalized space"""
213212
self.population = self.toolbox.generate()
214-
return _bound(self.population, lbounds, ubounds)
213+
return utils.bound(self.population, lbounds, ubounds)
215214

216215
def update_strategy(self):
217216
self.toolbox.update(self.population)

bluepyopt/deapext/CMA_SO.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .stoppingCriteria import MaxNGen, Stagnation, TolHistFun, EqualFunVals, \
3131
NoEffectAxis, TolUpSigma, TolX, ConditionCov, NoEffectCoor
3232

33-
from .utils import _bound
33+
from . import utils
3434

3535
logger = logging.getLogger('__main__')
3636

@@ -157,7 +157,7 @@ def get_population(self, to_space):
157157
def generate_new_pop(self, lbounds, ubounds):
158158
"""Generate a new population bounded in the normalized space"""
159159
self.population = self.toolbox.generate()
160-
return _bound(self.population, lbounds, ubounds)
160+
return utils.bound(self.population, lbounds, ubounds)
161161

162162
def update_strategy(self):
163163
self.toolbox.update(self.population)

bluepyopt/deapext/algorithms.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import pickle
3333

3434
from .stoppingCriteria import MaxNGen
35-
from .utils import _update_history_and_hof, _record_stats
35+
from . import utils
3636

3737
logger = logging.getLogger('__main__')
3838

@@ -140,8 +140,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
140140
history = deap.tools.History()
141141

142142
invalid_count = _evaluate_invalid_fitness(toolbox, population)
143-
_update_history_and_hof(halloffame, history, population)
144-
_record_stats(stats, logbook, start_gen, population, invalid_count)
143+
utils.update_history_and_hof(halloffame, history, population)
144+
utils.record_stats(stats, logbook, start_gen, population, invalid_count)
145145

146146
stopping_criteria = [MaxNGen(ngen)]
147147

@@ -154,8 +154,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
154154
population = parents + offspring
155155

156156
invalid_count = _evaluate_invalid_fitness(toolbox, offspring)
157-
_update_history_and_hof(halloffame, history, population)
158-
_record_stats(stats, logbook, gen, population, invalid_count)
157+
utils.history_and_hof(halloffame, history, population)
158+
utils.record_stats(stats, logbook, gen, population, invalid_count)
159159

160160
# Select the next generation parents
161161
parents = toolbox.select(population, mu)

bluepyopt/deapext/optimisations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from . import algorithms
3535
from . import tools
36-
from .utils import _reduce_method, _uniform
36+
from . import utils
3737

3838
import bluepyopt.optimisations
3939

@@ -171,7 +171,7 @@ def setup_deap(self):
171171
UPPER.append(parameter.upper_bound)
172172

173173
# Register the 'uniform' function
174-
self.toolbox.register("uniformparams", _uniform, LOWER, UPPER, IND_SIZE)
174+
self.toolbox.register("uniformparams", utils.uniform, LOWER, UPPER, IND_SIZE)
175175

176176
# Register the individual format
177177
# An indiviual is create by WSListIndividual and parameters
@@ -225,7 +225,7 @@ def setup_deap(self):
225225

226226
import copyreg
227227
import types
228-
copyreg.pickle(types.MethodType, _reduce_method)
228+
copyreg.pickle(types.MethodType, utils.reduce_method)
229229

230230
if self.use_scoop:
231231
if self.map_function:

bluepyopt/deapext/optimisationsCMA.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
import numpy
2424
import pickle
2525
import random
26-
from functools import partial
26+
import functools
2727

28-
from deap import tools
28+
import deap.tools
2929

3030
from .CMA_SO import CMA_SO
3131
from .CMA_MO import CMA_MO
32-
from .utils import _update_history_and_hof, _record_stats, _reduce_method, \
33-
_uniform
32+
from . import utils
33+
34+
import bluepyopt.optimisations
3435

3536
logger = logging.getLogger('__main__')
3637

@@ -166,9 +167,9 @@ def __init__(self,
166167
self.to_space = []
167168
for r, m in zip(bounds_radius, bounds_mean):
168169
self.to_norm.append(
169-
partial(lambda param, bm, br: (param - bm) / br, bm=m, br=r))
170+
functools.partial(lambda param, bm, br: (param - bm) / br, bm=m, br=r))
170171
self.to_space.append(
171-
partial(lambda param, bm, br: (param * br) + bm, bm=m, br=r))
172+
functools.partial(lambda param, bm, br: (param * br) + bm, bm=m, br=r))
172173

173174
# Overwrite the bounds with -1. and 1.
174175
self.lbounds = numpy.full(self.problem_size, -1.)
@@ -191,7 +192,9 @@ def setup_deap(self):
191192
numpy.random.seed(self.seed)
192193

193194
# Register the 'uniform' function
194-
self.toolbox.register("uniformparams", _uniform, self.lbounds,
195+
self.toolbox.register("uniformparams",
196+
utils.uniform,
197+
self.lbounds,
195198
self.ubounds,
196199
self.ind_size)
197200

@@ -222,8 +225,10 @@ def setup_deap(self):
222225

223226
# Register the evaluation function for the individuals
224227
self.toolbox.register("evaluate", self.evaluator.evaluate_with_lists)
225-
226-
copyreg.pickle(types.MethodType, _reduce_method)
228+
229+
import copyreg
230+
import types
231+
copyreg.pickle(types.MethodType, utils.reduce_method)
227232

228233
if self.use_scoop:
229234
if self.map_function:
@@ -266,8 +271,8 @@ def run(self,
266271
CMA_es.map_function = self.map_function
267272

268273
else:
269-
history = tools.History()
270-
logbook = tools.Logbook()
274+
history = deap.tools.History()
275+
logbook = deap.tools.Logbook()
271276
logbook.header = ["gen", "nevals"] + stats.fields
272277

273278
# Instantiate the CMA strategies centered on the centroids
@@ -309,8 +314,8 @@ def run(self,
309314

310315
# Update the hall of fame, history and logbook
311316
pop = CMA_es.get_population(self.to_space)
312-
_update_history_and_hof(self.hof, history, pop)
313-
record = _record_stats(stats, logbook, gen, pop, nevals)
317+
utils.update_history_and_hof(self.hof, history, pop)
318+
record = utils.record_stats(stats, logbook, gen, pop, nevals)
314319
logger.info(logbook.stream)
315320

316321
# Update the CMA strategy using the new fitness and check if
@@ -345,4 +350,4 @@ def get_stats(self):
345350
stats.register("std", numpy.std)
346351
stats.register("min", numpy.min)
347352
stats.register("max", numpy.max)
348-
return stats
353+
return stats

bluepyopt/deapext/stoppingCriteria.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import logging
2121
import numpy
2222

23+
from collections import deque
24+
2325
import bluepyopt.stoppingCriteria
2426

2527
logger = logging.getLogger('__main__')
@@ -42,7 +44,7 @@ def check(self, kwargs):
4244
self.criteria_met = True
4345

4446

45-
class Stagnation(StoppingCriteria):
47+
class Stagnation(bluepyopt.stoppingCriteria.StoppingCriteria):
4648
"""Stagnation stopping criteria class"""
4749
name = "Stagnation"
4850

@@ -78,7 +80,7 @@ def check(self, kwargs):
7880
self.criteria_met = True
7981

8082

81-
class TolHistFun(StoppingCriteria):
83+
class TolHistFun(bluepyopt.stoppingCriteria.StoppingCriteria):
8284
"""TolHistFun stopping criteria class"""
8385
name = "TolHistFun"
8486

@@ -100,7 +102,7 @@ def check(self, kwargs):
100102
self.criteria_met = True
101103

102104

103-
class EqualFunVals(StoppingCriteria):
105+
class EqualFunVals(bluepyopt.stoppingCriteria.StoppingCriteria):
104106
"""EqualFunVals stopping criteria class"""
105107
name = "EqualFunVals"
106108

@@ -131,7 +133,7 @@ def check(self, kwargs):
131133
self.criteria_met = True
132134

133135

134-
class TolX(StoppingCriteria):
136+
class TolX(bluepyopt.stoppingCriteria.StoppingCriteria):
135137
"""TolX stopping criteria class"""
136138
name = "TolX"
137139

@@ -150,7 +152,7 @@ def check(self, kwargs):
150152
self.criteria_met = True
151153

152154

153-
class TolUpSigma(StoppingCriteria):
155+
class TolUpSigma(bluepyopt.stoppingCriteria.StoppingCriteria):
154156
"""TolUpSigma stopping criteria class"""
155157
name = "TolUpSigma"
156158

@@ -169,7 +171,7 @@ def check(self, kwargs):
169171
self.criteria_met = True
170172

171173

172-
class ConditionCov(StoppingCriteria):
174+
class ConditionCov(bluepyopt.stoppingCriteria.StoppingCriteria):
173175
"""ConditionCov stopping criteria class"""
174176
name = "ConditionCov"
175177

@@ -187,7 +189,7 @@ def check(self, kwargs):
187189
self.criteria_met = True
188190

189191

190-
class NoEffectAxis(StoppingCriteria):
192+
class NoEffectAxis(bluepyopt.stoppingCriteria.StoppingCriteria):
191193
"""NoEffectAxis stopping criteria class"""
192194
name = "NoEffectAxis"
193195

@@ -212,7 +214,7 @@ def check(self, kwargs):
212214
self.criteria_met = True
213215

214216

215-
class NoEffectCoor(StoppingCriteria):
217+
class NoEffectCoor(bluepyopt.stoppingCriteria.StoppingCriteria):
216218
"""NoEffectCoor stopping criteria class"""
217219
name = "NoEffectCoor"
218220

@@ -227,4 +229,4 @@ def check(self, kwargs):
227229
C = kwargs.get("C")
228230

229231
if any(centroid == centroid + 0.2 * sigma * numpy.diag(C)):
230-
self.criteria_met = True
232+
self.criteria_met = True

bluepyopt/deapext/utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
"""
1717

1818
import numpy
19-
19+
import random
2020

2121
# pylint: disable=R0914, R0912
2222

2323

24-
def _update_history_and_hof(halloffame, history, population):
24+
def update_history_and_hof(halloffame, history, population):
2525
"""Update the hall of fame with the generated individuals
2626
Note: History and Hall-of-Fame behave like dictionaries
2727
"""
@@ -31,13 +31,13 @@ def _update_history_and_hof(halloffame, history, population):
3131
history.update(population)
3232

3333

34-
def _record_stats(stats, logbook, gen, population, invalid_count):
34+
def record_stats(stats, logbook, gen, population, invalid_count):
3535
"""Update the statistics with the new population"""
3636
record = stats.compile(population) if stats is not None else {}
3737
logbook.record(gen=gen, nevals=invalid_count, **record)
3838

3939

40-
def _closest_feasible(individual, lbounds, ubounds):
40+
def closest_feasible(individual, lbounds, ubounds):
4141
"""Returns the closest individual in the parameter bounds"""
4242
# TO DO: Fix 1e-9 hack
4343
for i, (u, l, el) in enumerate(zip(ubounds, lbounds, individual)):
@@ -48,23 +48,29 @@ def _closest_feasible(individual, lbounds, ubounds):
4848
return individual
4949

5050

51-
def _bound(population, lbounds, ubounds):
51+
def bound(population, lbounds, ubounds):
5252
"""Bounds the population based on lower and upper parameter bounds."""
5353
n_out = 0
5454
for i, ind in enumerate(population):
5555
if numpy.any(numpy.less(ind, lbounds)) or numpy.any(
5656
numpy.greater(ind, ubounds)):
57-
population[i] = _closest_feasible(ind, lbounds, ubounds)
57+
population[i] = closest_feasible(ind, lbounds, ubounds)
5858
n_out += 1
5959
return n_out
6060

6161

62-
def _uniform(lower_list, upper_list, dimensions):
62+
def uniform(lower_list, upper_list, dimensions):
6363
"""Uniformly pick an individual"""
6464

6565
if hasattr(lower_list, '__iter__'):
6666
return [random.uniform(lower, upper) for lower, upper in
6767
zip(lower_list, upper_list)]
6868
else:
6969
return [random.uniform(lower_list, upper_list)
70-
for _ in range(dimensions)]
70+
for _ in range(dimensions)]
71+
72+
73+
def reduce_method(meth):
74+
"""Overwrite reduce"""
75+
return (getattr, (meth.__self__, meth.__func__.__name__))
76+

0 commit comments

Comments
 (0)