BlueBrain / BluePyOpt

Blue Brain Python Optimisation Library
https://bluepyopt.readthedocs.io/en/latest/
Other
198 stars 96 forks source link

BluePyOpte error during TM-fitting #457

Closed NataliBarros closed 1 year ago

NataliBarros commented 1 year ago

I would like to replicate the multiple frequency TM-fitting for my data as it is done in here: https://github.com/BlueBrain/BluePyOpt/blob/master/examples/tsodyksmarkramstp/tsodyksmarkramstp_multiplefreqs.ipynb

However when I run the code in my venv I get the following error when running the second cell with code:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 13>()
     11 # Create BluePyOpt optimization and run 
     12 evaluator = tmevaluator.TsodyksMarkramEvaluator(data, optconf)
---> 13 opt = bpop.optimisations.DEAPOptimisation(evaluator, offspring_size=500, map_function=pool.map,
     14                                           eta=20, mutpb=0.3, cxpb=0.7)
     15 pop, hof, log, history = opt.run(max_ngen=50)
     17 # Get best individual

File ~/venv_jupyterhub/lib/python3.8/site-packages/bluepyopt/deapext/optimisations.py:146, in DEAPOptimisation.__init__(self, evaluator, use_scoop, seed, offspring_size, eta, mutpb, cxpb, map_function, hof, selector_name)
    143 # Create a DEAP toolbox
    144 self.toolbox = deap.base.Toolbox()
--> 146 self.setup_deap()

File ~/venv_jupyterhub/lib/python3.8/site-packages/bluepyopt/deapext/optimisations.py:204, in DEAPOptimisation.setup_deap(self)
    194 self.toolbox.register(
    195     "population",
    196     deap.tools.initRepeat,
    197     list,
    198     self.toolbox.Individual)
    200 # Register the evaluation function for the individuals
    201 # import deap_efel_eval1
    202 self.toolbox.register(
    203     "evaluate",
--> 204     self.evaluator.init_simulator_and_evaluate_with_lists
    205 )
    207 # Register the mate operator
    208 self.toolbox.register(
    209     "mate",
    210     deap.tools.cxSimulatedBinaryBounded,
    211     eta=ETA,
    212     low=LOWER,
    213     up=UPPER)

AttributeError: 'TsodyksMarkramEvaluator' object has no attribute 'init_simulator_and_evaluate_with_lists'

Can anybody help me with this issue? Thanks.

wvangeit commented 1 year ago

@anilbey , @AurelienJaquier , @DrTaDa , do you know if some recent change could have triggered this?

AurelienJaquier commented 1 year ago

We added this new method to the Cellevaluator, init_simulator_and_evaluate_with_lists, lately (the LFPy merge, or the Arbor merge, I don't remember exactly). Since the evaluator in this example does not have a simulator to evaluate, implementing a init_simulator_and_evaluate_with_lists simply calling evaluate_with_lists should do the trick. I'll open a PR.

AurelienJaquier commented 1 year ago

Fixed with PR #458

NataliBarros commented 1 year ago

Is this issue suppose to be fixed? I'm still having the same error when trying to run the code in my venv and after pip install --upgrade bluepyopt

AurelienJaquier commented 1 year ago

Strange, it is working on my side.

You are running the notebook, right? Is the BluePyOpt version from which you run the noteboook also updated? There is a tmevaluator.py file in the same folder as the notebook. Does this file have the method init_simulator_and_evaluate_with_lists (line 86)?

NataliBarros commented 1 year ago

Yes, I downloaded the notebook and the required files from the github repo and uploaded them into a folder in my venv. Then I upgraded bluepyopt and run it.

The filetmevaluator.py doesn't have the method init_simulator_and_evaluate_with_lists this is its content:

"""Tsodyks-Markram model Evaluator.
This module contains an evaluator for the Tsodyks-Markram model.

@author: Giuseppe Chindemi
@remark: Copyright (c) 2017, EPFL/Blue Brain Project
         This file is part of BluePyOpt
         <https://github.com/BlueBrain/BluePyOpt>
         This library is free software; you can redistribute it and/or modify
         it under the terms of the GNU Lesser General Public License version
         3.0 as published by the Free Software Foundation.
         This library is distributed in the hope that it will be useful, but
         WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
         See the GNU Lesser General Public License for more details.
         You should have received a copy of the GNU Lesser General Public
         License along with this library; if not, write to the Free Software
         Foundation, Inc.,
         51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import bluepyopt as bpop
import numpy as np
import tmodeint

try:
    xrange
except NameError:
    xrange = range

class TsodyksMarkramEvaluator(bpop.evaluators.Evaluator):
    def __init__(self, t, v, tstim, params):
        """
        Parameters
        ----------
        t : numpy.ndarray
            Time vector (sec).
        v : numpy.ndarray
            Voltage vector (V), must have same dimension of t.
        tstim : numpy.ndarray
            Time of the stimuli.
        params : list
            List of parameters to fit. Every entry must be a tuple
            (name, lower bound, upper bound).
        """
        super(TsodyksMarkramEvaluator, self).__init__()
        self.v = v
        self.t = t
        self.stimidx = np.searchsorted(t, tstim)
        self.dx = t[1] - t[0]
        self.nsamples = len(v)
        # Find voltage baseline
        bs_stop = np.searchsorted(t, tstim[0])
        self.vrest = np.mean(v[:bs_stop])
        # Compute time windows where to compare model and data
        offset = 0.005  # s
        window = 0.04  # s
        window_samples = int(np.round(window / self.dx))
        psp_start = np.searchsorted(t, tstim + offset)
        psp_stop = psp_start + window_samples
        psp_stop[-1] += 2 * window_samples  # Extend last psp window (RTR case)
        self.split_idx = list(zip(psp_start, psp_stop))
        # Parameters to be optimized
        self.params = [bpop.parameters.Parameter(name, bounds=(minval, maxval))
                       for name, minval, maxval in params]
        # Objectives
        self.objectives = [bpop.objectives.Objective('interval_%d' % (i,))
                           for i in xrange(len(self.split_idx))]

    def generate_model(self, individual):
        """Calls numerical integrator `tmodeint.py` and returns voltage trace
        based on the input parameters"""

        v, _ = tmodeint.integrate(self.stimidx, self.nsamples, self.dx,
                                  self.vrest, *individual)
        return v

    def evaluate_with_lists(self, individual):
        """Errors used by BluePyOpt for the optimization"""

        candidate_v = self.generate_model(individual)
        errors = [np.linalg.norm(self.v[t0:t1] - candidate_v[t0:t1])
                  for t0, t1 in self.split_idx]
        return errors
AurelienJaquier commented 1 year ago

Then this is the reason it is not working on your side. This file (and also tmevaluator_multiplefreqs.py) have the init_simulator_and_evaluate_with_lists in up-to-date bluepyopt: https://github.com/BlueBrain/BluePyOpt/blob/master/examples/tsodyksmarkramstp/tmevaluator.py#L86

You should re-download the example folder from the github repo because the files in it have been updated.

NataliBarros commented 1 year ago

Yes, now it works. Thank you!

AurelienJaquier commented 1 year ago

Great! Then I'll close this issue. :)