From 6862eb4011b6363bb4cf3701007adcff1441b2b6 Mon Sep 17 00:00:00 2001 From: Werner Van Geit Date: Tue, 21 Feb 2017 15:00:52 +0100 Subject: [PATCH] Fix division by zero in IBEA --- bluepyopt/deapext/tools/selIBEA.py | 14 +++++++++----- bluepyopt/tests/test_l5pc.py | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/bluepyopt/deapext/tools/selIBEA.py b/bluepyopt/deapext/tools/selIBEA.py index d4cb923..459aa3b 100644 --- a/bluepyopt/deapext/tools/selIBEA.py +++ b/bluepyopt/deapext/tools/selIBEA.py @@ -72,21 +72,25 @@ def _calc_fitness_components(population, kappa): box_ranges = (numpy.max(population_matrix, axis=0) - numpy.min(population_matrix, axis=0)) + # Replace all possible zeros to avoid division by zero + # Basically 0/0 is replaced by 0/1 + box_ranges[box_ranges == 0] = 1.0 + components_matrix = numpy.zeros((pop_len, pop_len)) for i in xrange(0, pop_len): diff = population_matrix - population_matrix[i, :] components_matrix[i, :] = numpy.max( - numpy.divide( - diff, - box_ranges), + numpy.divide(diff, box_ranges), axis=1) # Calculate max of absolute value of all elements in matrix max_absolute_indicator = numpy.max(numpy.abs(components_matrix)) # Normalisation - components_matrix = numpy.exp((-1.0 / (kappa * max_absolute_indicator)) * - components_matrix.T) + if max_absolute_indicator != 0: + components_matrix = numpy.exp( + (-1.0 / (kappa * max_absolute_indicator)) * components_matrix.T) + return components_matrix diff --git a/bluepyopt/tests/test_l5pc.py b/bluepyopt/tests/test_l5pc.py index 3f0cd74..1d3acb5 100644 --- a/bluepyopt/tests/test_l5pc.py +++ b/bluepyopt/tests/test_l5pc.py @@ -170,6 +170,8 @@ def stdout_redirector(stream): @attr('slow') def test_exec(): """L5PC Notebook: test execution""" + import numpy + numpy.seterr(all='raise') old_cwd = os.getcwd() output = StringIO() try: