ODYNN : Optimization for DYnamic Neural Networks

https://travis-ci.com/MarcusJP/ODYNN.svg?branch=master https://codecov.io/gh/MarcusJP/ODYNN/branch/master/graph/badge.svg Documentation Status

Objective

ODYNN stands for Optimization for DYnamic Neural Network. ODYNN is designed for simulating and optimizing biological neural circuits to model multi-scale behaviors exhibited by the neural dynamics, and to test neuroscience related hypotheses. It is enhanced with features such as performing experiments with different biophysically realistic neuronal models as well as artificial recurrent neural networks (in particular long short term memory), incorporating calcium imaging data, to design arbitrarily structured neural circuits and optimizing them to govern specific behaviors.

Objective

Getting started

Clone a local copy of this repository from Github using:

git clone https://github.com/MarcusJP/ODYNN

Go to the root directory and run:

make init
make install

Some examples

  • Neuron simulation :
from odynn.nsimul import simul
import scipy as sp
t = sp.arange(0., 1200., 0.1)
i = 20. * ((t>400) & (t<800))
simul(t=t, i_inj=i, show=True)
alternate text
  • Neuron optimization :
import numpy as np
from odynn import utils, nsimul, neuron, noptim
#This file defines the model we will use
from odynn.models import cfg_model

dt = 1.
folder = 'Example'

# Function to call to set the target directories for plots and saved files
dir = utils.set_dir(folder)

#Definition of time and 2 input currents
t = np.arange(0., 1200., dt)
i_inj1 = 10. * ((t>200) & (t<600)) + 20. * ((t>800) & (t<1000))
i_inj2 = 5. * ((t>200) & (t<300)) + 30. * ((t>500) & (t<1000))
i_injs = np.stack([i_inj1, i_inj2], axis=-1)

#10 random initial parameters
params = [cfg_model.NEURON_MODEL.get_random() for _ in range(10)]
neuron = neuron.BioNeuronTf(params, dt=dt)

#This function will take the default parameters of the used model if none is given
train = nsimul.simul(t=t, i_inj=i_injs, show=True)

#Optimization
optimizer = noptim.NeuronOpt(neuron)
optimizer.optimize(dir=dir, train=train)

Tutorials

Jupyter notebook tutorials are available. After cloning the repository, go to the tutorial/ folder and run:

jupyter notebook

odynn package

Structure

UML class diagram

ODYNN’s class diagram

Subpackages

odynn.models package

Submodules
odynn.models.celeg module
class odynn.models.celeg.CElegansNeuron(init_p=None, tensors=False, dt=0.1)[source]

Bases: odynn.models.model.BioNeuron

Full Hodgkin-Huxley Model implemented for C. elegans

Attributes:
init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

parameter_names

Methods

calculate(i_inj) Simulate the neuron with input current i_inj and return the state vectors
get_random() Returns a dictionnary of random parameters
parallelize(n) Add a dimension of size n in the initial parameters and initial state
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_results(ts, i_inj_values, results[, …]) plot all dynamics
plot_vars(var_dic[, suffix, show, save, func]) plot variation/comparison/boxplots of all variables organized by categories
plot_vars_gate(name, mdp, scale, tau, fig, …) plot the gates variables
step(X, i_inj) Integrate and update state variable (voltage and possibly others) after one time step
boxplot_vars  
study_vars  
REST_CA = 0.0
static boxplot_vars(var_dic, suffix='', show=False, save=True)[source]
default_init_state = array([-6.0e+01, 0.0e+00, 9.5e-01, 0.0e+00, 0.0e+00, 1.0e+00, 1.0e-07])

initial state for neurons – voltage, rates and $[Ca^{2+}]$

default_params = {'C_m': 20.0, 'E_Ca': 20.0, 'E_K': -60.0, 'E_L': -60.0, 'decay_ca': 110.0, 'e__mdp': -3.36, 'e__scale': 6.75, 'e__tau': 10.0, 'f__mdp': 25.2, 'f__scale': -5.03, 'f__tau': 151.0, 'g_Ca': 3.0, 'g_Kf': 0.07, 'g_Ks': 10.0, 'g_L': 0.005, 'h__alpha': 0.282, 'h__mdp': 6.42, 'h__scale': -1.0, 'n__mdp': 19.9, 'n__scale': 15.9, 'n__tau': 25.0, 'p__mdp': -8.05, 'p__scale': 7.43, 'p__tau': 100.0, 'q__mdp': -15.6, 'q__scale': -9.97, 'q__tau': 150.0, 'rho_ca': 0.23}

default parameters as a dictionnary

static get_random()[source]

Returns a dictionnary of random parameters

plot_results(ts, i_inj_values, results, ca_true=None, suffix='', show=True, save=False)[source]

plot all dynamics

Parameters:
  • ts
  • i_inj_values
  • results
  • ca_true – (Default value = None)
  • suffix – (Default value = “”)
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)

Returns:

classmethod plot_vars(var_dic, suffix='evolution', show=False, save=True, func=<function plot>)[source]

plot variation/comparison/boxplots of all variables organized by categories

Parameters:
  • var_dic
  • suffix – (Default value = “”)
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)
  • func – (Default value = plot)

Returns:

static plot_vars_gate(name, mdp, scale, tau, fig, pos, labs, func=<function plot>)[source]

plot the gates variables

Parameters:
  • name
  • mdp
  • scale
  • tau
  • fig
  • pos
  • labs
  • func – (Default value = plot)

Returns:

step(X, i_inj)[source]

Integrate and update state variable (voltage and possibly others) after one time step

Parameters:
  • X (ndarray) – State variables
  • i (float) – Input current
Returns:

updated state vector

Return type:

ndarray

classmethod study_vars(p, suffix='', target=None, show=False, save=True)[source]
odynn.models.celeg.give_rand()[source]
odynn.models.cfg_model module
odynn.models.cfg_model.NEURON_MODEL

alias of odynn.models.celeg.CElegansNeuron

odynn.models.hhsimple module
class odynn.models.hhsimple.HodgHuxSimple(init_p, tensors=False, dt=0.1)[source]

Bases: odynn.models.model.BioNeuron

Attributes:
init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

parameter_names

Methods

calculate(i_inj) Simulate the neuron with input current i_inj and return the state vectors
get_random() Return a dictionnary with the same keys as default_params and random values
parallelize(n) Add a dimension of size n in the initial parameters and initial state
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_results(ts, i_inj_values, results[, …]) Function for plotting detailed results of some experiment
step(X, i_inj) Integrate and update state variable (voltage and possibly others) after one time step
default_init_state = array([-60., 0., 1.])
default_params = {'C_m': 1.0, 'E_K': 30.0, 'E_L': -60.0, 'a__mdp': -30.0, 'a__scale': 20.0, 'a__tau': 500.0, 'b__mdp': -5.0, 'b__scale': -3.0, 'b__tau': 30.0, 'g_K': 0.5, 'g_L': 0.1}
static get_random()[source]

Return a dictionnary with the same keys as default_params and random values

plot_results(ts, i_inj_values, results, ca_true=None, suffix='', show=True, save=False)[source]

Function for plotting detailed results of some experiment

step(X, i_inj)[source]

Integrate and update state variable (voltage and possibly others) after one time step

Parameters:
  • X (ndarray) – State variables
  • i (float) – Input current
Returns:

updated state vector

Return type:

ndarray

odynn.models.leakint module
class odynn.models.leakint.LeakyIntegrate(init_p, tensors=False, dt=0.1)[source]

Bases: odynn.models.model.BioNeuron

Attributes:
init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

parameter_names

Methods

calculate(i_inj) Simulate the neuron with input current i_inj and return the state vectors
get_random() Return a dictionnary with the same keys as default_params and random values
parallelize(n) Add a dimension of size n in the initial parameters and initial state
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_results(ts, i_inj_values, X[, ca_true, …]) Function for plotting detailed results of some experiment
step(X, i_inj) Integrate and update state variable (voltage and possibly others) after one time step
default_init_state = array([-60.])
default_params = {'C_m': 1.0, 'E_L': -60.0, 'g_L': 0.1}
static get_random()[source]

Return a dictionnary with the same keys as default_params and random values

plot_results(ts, i_inj_values, X, ca_true=None, suffix='', show=True, save=False)[source]

Function for plotting detailed results of some experiment

step(X, i_inj)[source]

Integrate and update state variable (voltage and possibly others) after one time step

Parameters:
  • X (ndarray) – State variables
  • i (float) – Input current
Returns:

updated state vector

Return type:

ndarray

odynn.models.model module
class odynn.models.model.BioNeuron(init_p=None, tensors=False, dt=0.1)[source]

Bases: odynn.models.model.Neuron

Abstract class to implement for using a new biological model All methods and class variables have to be implemented in order to have the expected behavior

Attributes:
default_init_state
default_params
init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

parameter_names

Methods

calculate(i_inj) Simulate the neuron with input current i_inj and return the state vectors
get_random() Return a dictionnary with the same keys as default_params and random values
parallelize(n) Add a dimension of size n in the initial parameters and initial state
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_results(*args, **kwargs) Function for plotting detailed results of some experiment
step(X, i) Integrate and update state variable (voltage and possibly others) after one time step
__init__(init_p=None, tensors=False, dt=0.1)[source]

Reshape the initial state and parameters for parallelization in case init_p is a list

Parameters:
  • init_p (dict or list of dict) – initial parameters of the neuron(s). If init_p is a list, then this object will model n = len(init_p) neurons
  • tensors (bool) – used in the step function in order to use tensorflow or numpy
  • dt (float) – time step
calculate(i_inj)[source]

Simulate the neuron with input current i_inj and return the state vectors

Parameters:i_inj – input currents of shape [time, batch]
Returns:series of state vectors of shape [time, state, batch]
Return type:ndarray
default_params = None

dict, Default set of parameters for the model, of the form {<param_name> – value}

static get_random()[source]

Return a dictionnary with the same keys as default_params and random values

parallelize(n)[source]

Add a dimension of size n in the initial parameters and initial state

Parameters:n (int) – size of the new dimension
parameter_names = None

names of parameters from the model

static plot_results(*args, **kwargs)[source]

Function for plotting detailed results of some experiment

class odynn.models.model.Neuron(dt=0.1)[source]

Bases: abc.ABC

Attributes:
default_init_state
init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

Methods

calculate(i) Iterate over i (current) and return the state variables obtained after each step
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
step(X, i) Integrate and update state variable (voltage and possibly others) after one time step
V_pos = 0

int, Default position of the voltage in state vectors

calculate(i)[source]

Iterate over i (current) and return the state variables obtained after each step

Parameters:i (ndarray) – input current, dimension [time, (batch, (self.num))]
Returns:state vectors concatenated [i.shape[0], len(self.init_state)(, i.shape[1], (i.shape[2]))]
Return type:ndarray
default_init_state = None

array, Initial values for the vector of state variables

init_state

ndarray, Initial state vector

ions = {}
num

int, Number of neurons being modeled in this object

classmethod plot_output(ts, i_inj, states, y_states=None, suffix='', show=True, save=False, l=1, lt=1, targstyle='-')[source]

Plot voltage and ion concentrations, potentially compared to a target model

Parameters:
  • ts (ndarray of dimension [time]) – time steps of the measurements
  • i_inj (ndarray of dimension [time]) – input current
  • states (ndarray of dimension [time, state_var, nb_neuron]) –
  • y_states (list of ndarray [time, nb_neuron], optional) – list of values for the target model, each element is an ndarray containing the recordings of one state variable (Default value = None)
  • suffix (str) – suffix for the name of the saved file (Default value = “”)
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)
  • l (float) – width of the main lines (Default value = 1)
  • lt (float) – width of the target lines (Default value = 1)
  • targstyle (str) – style of the target lines (Default value = ‘-‘)
step(X, i)[source]

Integrate and update state variable (voltage and possibly others) after one time step

Parameters:
  • X (ndarray) – State variables
  • i (float) – Input current
Returns:

updated state vector

Return type:

ndarray

Module contents

Submodules

odynn.circuit module

class odynn.circuit.Circuit(neurons, synapses={}, gaps={}, tensors=False, labels=None, sensors=set(), commands=set())[source]

Bases: object

Attributes:
init_state

(ndarray) Initial states of neurons

neurons

Neurons contained in the circuit

num

Number of circuits contained in the object, used to train in parallel

Methods

calculate(i_inj) Simulate the circuit with a given input current.
plot([show, save]) Plot the circuit using networkx :param show: If True, show the figure :type show: bool :param save: If True, save the figure :type save: bool
plots_output_mult(ts, i_inj, states[, …]) plot multiple voltages and Ca2+ concentration
step(hprev, curs) run one time step
plot_output  
calculate(i_inj)[source]

Simulate the circuit with a given input current.

Parameters:i_inj (ndarray) – input current
Returns:state vector and synaptical currents
Return type:ndarray, ndarray
init_state

(ndarray) Initial states of neurons

neurons

Neurons contained in the circuit

num

Number of circuits contained in the object, used to train in parallel

parameter_names = ['scale', 'mdp', 'E', 'G', 'G_gap']

Circuit of neurons with synapses and gap junctions

plot(show=True, save=False)[source]

Plot the circuit using networkx :param show: If True, show the figure :type show: bool :param save: If True, save the figure :type save: bool

plot_output(*args, **kwargs)[source]
plots_output_mult(ts, i_inj, states, i_syn=None, suffix='', show=True, save=False, l=1, trace=True)[source]

plot multiple voltages and Ca2+ concentration

Parameters:
  • ts (ndarray) – time sequence
  • i_inj (ndarray) – input currents
  • states (ndarray) – series of neural states
  • i_syn (ndarray, optional) – synaptic currents (Default value = None)
  • suffix (str, optional) – Suffix for the file name (Default value = “”)
  • show (bool, optional) – If True, show the figure (Default value = True)
  • save (bool, optional) – If True, save the figure (Default value = False)
step(hprev, curs)[source]

run one time step

For tensor :

Parameters:
  • hprev (ndarray or tf.Tensor) – previous state vector
  • curs (ndarray or tf.Tensor) – input currents
Returns:

updated state vector

Return type:

ndarray or tf.Tensor

class odynn.circuit.CircuitTf(neurons, synapses={}, gaps={}, labels=None, sensors=set(), commands=set())[source]

Bases: odynn.circuit.Circuit, odynn.optim.Optimized

Circuit using tensorflow

Attributes:
init_params

(dict), initial model parameters

init_state

(ndarray) Initial states of neurons

neurons

Neurons contained in the circuit

num

Number of circuits contained in the object, used to train in parallel

variables

dict, current Tf variables

Methods

apply_constraints(session) Apply the constraints and call the apply_constraints function of the neurons
build_graph([batch]) Build the tensorflow graph.
calculate(i) Iterate over i (current) and return the state variables obtained after each step
plot([show, save]) Plot the circuit using networkx :param show: If True, show the figure :type show: bool :param save: If True, save the figure :type save: bool
plot_vars(var_dic[, suffix, show, save, func]) plot variation/comparison/boxplots of synaptic variables
plots_output_mult(ts, i_inj, states[, …]) plot multiple voltages and Ca2+ concentration
reset() prepare the variables as tensors, prepare the constraints, call reset for self._neurons
settings() Returns(str): string describing the object
step(hprev, curs) run one time step
apply_init  
create_random  
plot_output  
predump  
study_vars  
__init__(neurons, synapses={}, gaps={}, labels=None, sensors=set(), commands=set())[source]
Parameters:
  • labels
  • synapses (dict) – initial parameters for the synapses
  • neurons (NeuronModel) – if not None, all other parameters except conns are ignores
apply_constraints(session)[source]

Apply the constraints and call the apply_constraints function of the neurons

Parameters:session – tensorflow session
apply_init(session)[source]
build_graph(batch=1)[source]

Build the tensorflow graph. Take care of the loop and the initial state.

calculate(i)[source]

Iterate over i (current) and return the state variables obtained after each step

Parameters:i (ndarray) – input current, [time, batch, neuron, model]
Returns:state vectors concatenated [i.shape[0], len(self.init_state)(, i.shape[1]), self.num]
Return type:ndarray
classmethod create_random(n_neuron, syn_keys={}, gap_keys={}, n_rand=10, dt=0.1, labels=None, sensors=set(), commands=set(), fixed=(), groups=None, neurons=None)[source]
init_params

(dict), initial model parameters

plot_vars(var_dic, suffix='', show=True, save=False, func=<function plot>)[source]

plot variation/comparison/boxplots of synaptic variables

Parameters:
  • var_dic (dict) – synaptic parameters, each value of size [time, n_synapse, parallelization]
  • suffix – (Default value = “”)
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)
  • func – (Default value = plot)
reset()[source]

prepare the variables as tensors, prepare the constraints, call reset for self._neurons

settings()[source]

Returns(str): string describing the object

study_vars(p, loss=None, show=False, save=True)[source]
variables

dict, current Tf variables

odynn.circuit.const_E(exc=True)[source]
odynn.circuit.get_gap_rand()[source]

Give random parameters dictionnary for a gap junction

Returns:random parameters for a gap junction
Return type:dict
odynn.circuit.get_syn_rand(exc=True)[source]

Give random parameters dictionnary for a chemical synapse

Parameters:exc (bool) – If True, give an excitatory synapse (Default value = True)
Returns:random parameters for a chemical synapse
Return type:dict
odynn.circuit.give_constraints(conns)[source]

Give constraints for synaptic parameters

Parameters:conns (dict) – dictionnary of chemical synapse parameters
Returns:constraints
Return type:dict
odynn.circuit.give_constraints_gap()[source]

Give constraints for gap junction parameters

Returns:constraints
Return type:dict
odynn.circuit.give_constraints_syn(conns)[source]

Give constraints for chemical synapse parameters

Parameters:conns (dict) – dictionnary of synapse parameters
Returns:constraints
Return type:dict

odynn.coptim module

class odynn.coptim.CircuitOpt(circuit)[source]

Bases: odynn.optim.Optimizer

Class for optimization of a neuronal circuit

Methods

optimize(subdir[, train, test, w, w_n, …]) Optimize the neuron parameters
settings(w, train) Give the settings of the optimization
plot_out  
__init__(circuit)[source]
Parameters:circuit (CircuitTf) – Circuit to be optimized
optimize(subdir, train=None, test=None, w=(1, 0), w_n=None, epochs=700, l_rate=(0.9, 9, 0.95), suffix='', n_out=[1], evol_var=True, plot=True)[source]

Optimize the neuron parameters

Parameters:
  • dir (str) – path to the directory for the saved files
  • train (list of ndarray) –

    list containing [time, input, voltage, ion_concentration] that will be used fitted dimensions : - time : [time]

    • input, voltage and concentration : [time, batch, neuron]
  • test (list of ndarray) – same as train for the dimensions These arrays will be used fo testing the model (Default value = None)
  • w (list) – list of weights for the loss, the first value is for the voltage and the following ones for the ion concentrations defined in the model. (Default value = [1, 0]:
  • epochs (int) – Number of training steps (Default value = 700)
  • l_rate (tuple) – Parameters for an exponential decreasing learning rate : (start, number of constant steps, exponent) (Default value = [0.1, 9, 0.92]:
  • suffix (str) – suffix for the saved files (Default value = ‘’)
  • n_out (list of int) – list of neurons corresponding to the data in train and test
Returns:

neuron attribute after optimization

Return type:

NeuronTf

plot_out(X, results, res_targ, suffix, step, name, i)[source]
settings(w, train)[source]

Give the settings of the optimization

Parameters:w (tuple) – weights for the loss of voltage and ions concentrations
Returns:settings
Return type:str
odynn.coptim.plot_heatmap(m, name, suffix, labels, n_out=None)[source]

odynn.csimul module

odynn.csimul.simul(t, i_injs, pars=None, synapses={}, gaps={}, circuit=None, n_out=[0], suffix='', show=False, save=True, labels=None)[source]

Simulate a circuit with input current i_injs and return the outputs of neurons contained in n_out

Parameters:
  • t (ndarray) – time
  • i_injs (ndarray) – input currents of shape [time, batch, neuron]
  • pars (dict or list) – parameters for the neurons
  • synapses (dict or list) – parameters of the chemical synapses
  • gaps (dict or list) – parameters of the gap junctions
  • circuit( – obj: Circuit): if not None, ignore the 3 previous arguments
  • n_out (list) – neurons which output have to be saved
  • suffix (str) – suffix for the plots
  • show (bool) – If True, show the plot
  • save (bool) – If True, save the plot
  • labels – labels for the circuit’s neurons
Returns:

measurements as a list [time, input currents, [voltage(, calcium)]]

Return type:

list

odynn.datas module

odynn.datas.check_alpha(show=True)[source]

study the hill equation

odynn.datas.full4(dt=0.1, nb_neuron_zero=None, max_t=1200.0)[source]
odynn.datas.full4_test(dt=0.1, nb_neuron_zero=None, max_t=1200.0)[source]
odynn.datas.get_real_data(delta=500, final_time=4000.0, dt=0.2, show=False)[source]

dump real data into our format

Parameters:
  • delta – (Default value = 500)
  • final_time – (Default value = 4000.)
  • dt (float) – time step (Default value = 0.2)

Returns:

odynn.datas.get_real_data_norm(file='data/AVAL{}.csv')[source]
odynn.datas.give_periodic(t, max_i, size, freq)[source]
odynn.datas.give_test(dt=0.1, max_t=1200.0)[source]

time and currents for optimization

Parameters:dt (float) – time step (Default value = DT)

Returns:

odynn.datas.give_train(dt=0.1, nb_neuron_zero=None, max_t=1200.0)[source]

time and currents for optimization

Parameters:
  • dt (float) – time step (Default value = DT)
  • nb_neuron_zero – (Default value = None)
  • max_t – (Default value = 1200.)

Returns:

odynn.datas.give_train2(dt=0.1)[source]
odynn.datas.rd()

random() -> x in the interval [0, 1).

odynn.datas.test()[source]

odynn.neuron module

class odynn.neuron.BioNeuronTf(init_p=None, dt=0.1, fixed=(), constraints=None, groups=None, n_rand=None)[source]

Bases: odynn.models.celeg.CElegansNeuron, odynn.neuron.NeuronTf

Class representing a neuron, implemented using Tensorflow. This class allows simulation and optimization, alone and in a Circuit. It can contain several neurons at the same time. Which in turn can be optimized in parallel, or be used to represent the entire neurons in a Circuit.

Attributes:
groups

list indicating the group of each neuron

hidden_init_state

For behavioral models eg LSTM

init_params

initial model parameters

init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

parameter_names
trainable

True if the object can be optimized

variables

Current variables of the models

Methods

apply_constraints(session) Apply the constraints to the object variables
build_graph([batch]) Build a tensorflow graph for running the neuron(s) on a series of input :param batch: dimension of the batch :type batch: int
calculate(i) Iterate over i (current) and return the state variables obtained after each step
get_random() Returns a dictionnary of random parameters
init(batch) Method to implement whe initialization is needed, will be called before reset
parallelize(n) Add a dimension of size n in the initial parameters and initial state
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_results(ts, i_inj_values, results[, …]) plot all dynamics
plot_vars(var_dic[, suffix, show, save, func]) plot variation/comparison/boxplots of all variables organized by categories
plot_vars_gate(name, mdp, scale, tau, fig, …) plot the gates variables
reset() rebuild tf variable graph
settings() Returns(str): string describing the object
step(X, i_inj) Integrate and update state variable (voltage and possibly others) after one time step
apply_init  
boxplot_vars  
predump  
set_init_param  
study_vars  
__init__(init_p=None, dt=0.1, fixed=(), constraints=None, groups=None, n_rand=None)[source]

Initializer :param init_p: initial parameters of the neuron(s). If init_p is a list, then this object

will model n = len(init_p) neurons
Parameters:
  • dt (float) – time step
  • fixed (set) – parameters that are fixed and will stay constant in case of optimization. if fixed == ‘all’, all parameters will be constant
  • constraints (dict of ndarray) – keys as parameters name, and values as [lower_bound, upper_bound]
apply_constraints(session)[source]

Apply the constraints to the object variables

Parameters:session – tensorflow session
build_graph(batch=None)[source]

Build a tensorflow graph for running the neuron(s) on a series of input :param batch: dimension of the batch :type batch: int

Returns:input placeholder and results of the run
Return type:tf.placeholder, tf.Tensor
calculate(i)[source]

Iterate over i (current) and return the state variables obtained after each step

Parameters:i (ndarray) – input current
Returns:state vectors concatenated [i.shape[0], len(self.init_state)(, i.shape[1]), self.num]
Return type:ndarray
groups

list indicating the group of each neuron Neurons with the same group share the same parameters

init_params

initial model parameters

parallelize(n)[source]

Add a dimension of size n in the initial parameters and initial state

Parameters:n (int) – size of the new dimension
reset()[source]

rebuild tf variable graph

set_init_param(name, value=None)[source]
settings()[source]

Returns(str): string describing the object

trainable

True if the object can be optimized

variables

Current variables of the models

class odynn.neuron.NeuronLSTM(nb_layer=1, layer_size=50, extra_ca=0, dt=0.1, vars_init=None)[source]

Bases: odynn.neuron.NeuronTf

Behavior model of a neuron using an LSTM network

Attributes:
groups

list indicating the group of each neuron

hidden_init_state

Give the initial state needed for the LSTM network

init_params

Initial model parameters

init_state

ndarray, Initial state vector

num

Number of neurons contained in the object, always 1 here

trainable

boolean stating if the neuron can be optimized

variables

dict, current Tf variables

Methods

apply_constraints(session) Apply necessary constraints to the optimized variables
apply_init(sess) Initialize the variables if loaded object
build_graph([batch]) Build the tensorflow graph.
calculate(i) Iterate over i (current) and return the state variables obtained after each step
init(batch) Method to implement whe initialization is needed, will be called before reset
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_vars(var_dic, suffix, show, save) A function to plot the variables of the optimized object
settings() Returns(str): string describing the object
step(X, hprev, i_inj) Update function
predump  
reset  
study_vars  
apply_init(sess)[source]

Initialize the variables if loaded object

Parameters:sess – tf.Session
build_graph(batch=1)[source]

Build the tensorflow graph. Take care of the loop and the initial state.

calculate(i)[source]

Iterate over i (current) and return the state variables obtained after each step

Parameters:i (ndarray) – input current
Returns:state vectors concatenated [i.shape[0], len(self.init_state)(, i.shape[1]), self.num]
Return type:ndarray
hidden_init_state

Give the initial state needed for the LSTM network

init(batch)[source]

Method to implement whe initialization is needed, will be called before reset

Parameters:batch (int) – number of batches
init_params

Initial model parameters

num

Number of neurons contained in the object, always 1 here

predump(sess)[source]
reset()[source]
settings()[source]

Returns(str): string describing the object

step(X, hprev, i_inj)[source]

Update function

Parameters:
  • X (Tensor) – not used here, classical state
  • hprev (tuple of LSTMStateTuple) – previous LSTM state
  • i_inj (Tensor) – array of input currents, dimension [batch]
Returns:

Tensor containing the voltages in the first position

Return type:

Tensor

class odynn.neuron.NeuronTf(dt=0.1)[source]

Bases: odynn.models.model.Neuron, odynn.optim.Optimized

Abstract class whose implementation allow single optimization as well as in a Circuit

Attributes:
groups

list indicating the group of each neuron

hidden_init_state

For behavioral models eg LSTM

init_params

dict, initial parameters

init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

trainable

boolean stating if the neuron can be optimized

variables

dict, current Tf variables

Methods

apply_constraints(session) Apply necessary constraints to the optimized variables
build_graph([batch]) Build the tensorflow graph.
calculate(i) Iterate over i (current) and return the state variables obtained after each step
init(batch) Method to implement whe initialization is needed, will be called before reset
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_vars(var_dic, suffix, show, save) A function to plot the variables of the optimized object
settings() Give a string describing the settings Returns(str): description
step(X, i) Integrate and update state variable (voltage and possibly others) after one time step
apply_init  
predump  
study_vars  
default_init_state = array([-6.0e+01, 0.0e+00, 9.5e-01, 0.0e+00, 0.0e+00, 1.0e+00, 1.0e-07])
groups

list indicating the group of each neuron Neurons with the same group share the same parameters

hidden_init_state

For behavioral models eg LSTM

init(batch)[source]

Method to implement whe initialization is needed, will be called before reset

Parameters:batch (int) – number of batches
nb = 0
trainable

boolean stating if the neuron can be optimized

class odynn.neuron.Neurons(neurons)[source]

Bases: odynn.neuron.NeuronTf

This class allow to use neurons from different classes inheriting NeuronTf in a same Circuit

Attributes:
groups

list indicating the group of each neuron

hidden_init_state

For behavioral models eg LSTM

init_params

dict, initial parameters

init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

trainable

boolean stating if the neuron can be optimized

variables

dict, current Tf variables

Methods

apply_constraints(session) Apply the constraints to the object variables
build_graph() Build the tensorflow graph.
calculate(i) Iterate over i (current) and return the state variables obtained after each step
init(batch) call init method for all contained neuron objects
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_vars(var_dic, suffix, show, save) A function to plot the variables of the optimized object
settings() Returns(str): string describing the object
step(X, hidden, i) Share the state and the input current into its embedded neurons
apply_init  
predump  
reset  
study_vars  
__init__(neurons)[source]
Parameters:neurons (list) – list of NeuronTf objects
Raises:AttributeError – If all neurons don’t share the same dt
apply_constraints(session)[source]

Apply the constraints to the object variables

Parameters:session – tensorflow session
apply_init(session)[source]
build_graph()[source]

Build the tensorflow graph. Take care of the loop and the initial state.

calculate(i)[source]

Iterate over i (current) and return the state variables obtained after each step

Parameters:i (ndarray) – input current
Returns:state vectors concatenated [i.shape[0], len(self.init_state)(, i.shape[1]), self.num]
Return type:ndarray
hidden_init_state

For behavioral models eg LSTM

init(batch)[source]

call init method for all contained neuron objects

predump(sess)[source]
reset()[source]
settings()[source]

Returns(str): string describing the object

step(X, hidden, i)[source]

Share the state and the input current into its embedded neurons

Parameters:
  • X (tf.Tensor) – precedent state vector
  • i (tf.Tensor) – input current
Returns:

next state vector

Return type:

ndarray

class odynn.neuron.PyBioNeuron(init_p=None, dt=0.1)[source]

Bases: odynn.models.celeg.CElegansNeuron

Class representing a neuron, implemented only in Python This class allows simulation but not optimization

Attributes:
init_state

ndarray, Initial state vector

num

int, Number of neurons being modeled in this object

parameter_names

Methods

calculate(i_inj) Simulate the neuron with input current i_inj and return the state vectors
get_random() Returns a dictionnary of random parameters
parallelize(n) Add a dimension of size n in the initial parameters and initial state
plot_output(ts, i_inj, states[, y_states, …]) Plot voltage and ion concentrations, potentially compared to a target model
plot_results(ts, i_inj_values, results[, …]) plot all dynamics
plot_vars(var_dic[, suffix, show, save, func]) plot variation/comparison/boxplots of all variables organized by categories
plot_vars_gate(name, mdp, scale, tau, fig, …) plot the gates variables
step(X, i_inj) Integrate and update state variable (voltage and possibly others) after one time step
boxplot_vars  
study_vars  

odynn.noptim module

class odynn.noptim.NeuronOpt(neuron)[source]

Bases: odynn.optim.Optimizer

Class for optimization of a neuron

Methods

optimize(dir, train[, test, w, epochs, …]) Optimize the neuron parameters
settings(w, train) Give the settings of the optimization
plot_out  
__init__(neuron)[source]

Initializer, takes a NeuronTf object as argument

Parameters:neuron (NeuronTf) – Neuron to be optimized
optimize(dir, train, test=None, w=(1, 0), epochs=700, l_rate=(0.1, 9, 0.92), suffix='', step=None, reload=False, reload_dir=None, evol_var=True, plot=True)[source]

Optimize the neuron parameters

Parameters:
  • dir (str) – path to the directory for the saved files
  • train (list of ndarray) –

    list containing [time, input, voltage, ion_concentration] that will be used fitted dimensions : - time : [time]

    • input, voltage and concentration : [time, batch]
  • test (list of ndarray) – same as train for the dimensions These arrays will be used fo testing the model (Default value = None)
  • w (list) – list of weights for the loss, the first value is for the voltage and the following ones for the ion concentrations defined in the model. (Default value = [1, 0]:
  • epochs (int) – Number of training steps (Default value = 700)
  • l_rate (tuple) – Parameters for an exponential decreasing learning rate : (start, number of constant steps, exponent) (Default value = [0.1, 9, 0.92]:
  • suffix (str) – suffix for the saved files (Default value = ‘’)
  • step – (Default value = None)
  • reload (bool) – If True, will reload the graph saved in reload_dir (Default value = False)
  • reload_dir (str) – The path to the directory of the experience to reload (Default value = None)
Returns:

neuron attribute after optimization

Return type:

NeuronTf

plot_out(X, results, res_targ, suffix, step, name, i)[source]

odynn.nsimul module

odynn.nsimul.comp_neuron_trace(neuron, trace, i_inj=array([0., 0., 0., ..., 0., 0., 0.]), scale=False, suffix='', show=True, save=False)[source]

Compare a neuron with a given measured trace after scaling

Parameters:
  • neuron (NeuronModel object) – neuron to compare
  • trace – recordings to plot
  • dt (float) – time step
  • i_inj (ndarray) – input currents
  • scale – (Default value = False)
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)
odynn.nsimul.comp_neurons(neurons, i_inj=array([0., 0., 0., ..., 0., 0., 0.]), suffix='', show=True, save=False)[source]

Compare different neurons on the same experiment

Parameters:
  • neurons (list of object NeuronModel) – neurons to compare
  • dt (float) – time step
  • i_inj (ndarray) – input currents
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)
odynn.nsimul.comp_pars(ps, t=None, dt=0.1, i_inj=array([0., 0., 0., ..., 0., 0., 0.]), suffix='', show=True, save=False)[source]

Compare different parameter sets on the same experiment

Parameters:
  • ps (list of dict) – list of parameters to compare
  • dt (float) – time step
  • i_inj (ndarray) – input currents
  • show (bool) – If True, show the figure (Default value = True)
  • save (bool) – If True, save the figure (Default value = False)
odynn.nsimul.comp_pars_targ(p, p_targ, t=None, dt=0.1, i_inj=array([0., 0., 0., ..., 0., 0., 0.]), suffix='', save=False, show=True)[source]

Compare parameter sets with a target

Parameters:
  • p (dict or list of dict) – parameter(s) to compare with the target
  • p_targ (dict) – target parameters
  • dt (float) – time step
  • i_inj (ndarray) – input currents
  • suffix (str) – suffix for the saved figure (Default value = ‘’)
  • save (bool) – If True, save the figure (Default value = False)
  • show (bool) – If True, show the figure (Default value = True)
odynn.nsimul.simul(p=None, neuron=None, t=None, dt=0.1, i_inj=array([0., 0., 0., ..., 0., 0., 0.]), suffix='', show=False, save=True, ca_true=None)[source]

Main demo for the Hodgkin Huxley neuron model

Parameters:
  • p (dict) – parameters of the neuron to simulate
  • neuron (NeuronModel object) – neuron to simulate
  • dt – time step
Returns:

records

Return type:

list

odynn.optim module

class odynn.optim.Optimized(dt)[source]

Bases: abc.ABC

Abstract class for object to be optimized. It could represent on or a set of neurons, or a circuit.

Attributes:
init_params

dict, initial parameters

num

int, number of models

variables

dict, current Tf variables

Methods

apply_constraints(session) Apply necessary constraints to the optimized variables
build_graph([batch]) Build the tensorflow graph.
plot_vars(var_dic, suffix, show, save) A function to plot the variables of the optimized object
settings() Give a string describing the settings Returns(str): description
apply_init  
predump  
study_vars  
apply_constraints(session)[source]

Apply necessary constraints to the optimized variables

Parameters:session (tf.Session) –
apply_init(session)[source]
build_graph(batch=1)[source]

Build the tensorflow graph. Take care of the loop and the initial state.

init_params

dict, initial parameters

ions = {}
num

int, number of models

static plot_vars(var_dic, suffix, show, save)[source]

A function to plot the variables of the optimized object

Parameters:
  • var_dic
  • suffix
  • show
  • save
predump(sess)[source]
settings()[source]

Give a string describing the settings Returns(str): description

study_vars(p, *args, **kwargs)[source]
variables

dict, current Tf variables

class odynn.optim.Optimizer(optimized, frequency=30)[source]

Bases: abc.ABC

Methods

settings(w, train) Give the settings of the optimization
optimize  
plot_out  
__init__(optimized, frequency=30)[source]
Parameters:
  • optimized (Optimized) –
  • epochs
  • frequency
optimize(dir, train_=None, test_=None, w=None, epochs=700, l_rate=(0.1, 9, 0.92), suffix='', step='', reload=False, reload_dir=None, yshape=None, evol_var=True, plot=True)[source]
plot_out(*args, **kwargs)[source]
settings(w, train)[source]

Give the settings of the optimization

Parameters:w (tuple) – weights for the loss of voltage and ions concentrations
Returns:settings
Return type:str
odynn.optim.get_best_result(dir, i=-1, loss=False)[source]
Parameters:dir (str) – path to the directory i: (Default value = -1)

Returns:

odynn.optim.get_data(dir)[source]
odynn.optim.get_model(dir)[source]
odynn.optim.get_vars(dir, i=-1, loss=False)[source]

get dic of vars from dumped file

Parameters:dir (str) – path to the directory i: (Default value = -1)

Returns:

odynn.optim.get_vars_all(dir, i=-1, losses=False)[source]

get dic of vars from dumped file

Parameters:dir (str) – path to the directory i: (Default value = -1)

Returns:

odynn.optim.plot_loss_rate(losses, rates, losses_test=None, parallel=1, suffix='', show=False, save=True)[source]

plot loss (log10) and learning rate

Parameters:
  • losses
  • rates
  • losses_test – (Default value = None)
  • parallel – (Default value = 1)
  • suffix – (Default value = “”)
  • show (bool) – If True, show the figure (Default value = False)
  • save – (Default value = True)

Returns:

odynn.utils module

odynn.utils.bar(ax, var, good_val=None)[source]
odynn.utils.box(df, cols, labels)[source]
odynn.utils.boxplot(ax, var)[source]
odynn.utils.clamp(val, minimum=0, maximum=255)[source]
Clamp val between minimum and maximum
Parameters:
  • val (float) – value to clamp
  • minimum (int) – minimum
  • maximum (int) – maximum
Returns:

clamped value

Return type:

int

class odynn.utils.classproperty(fget)[source]

Bases: object

odynn.utils.colorscale(hexstr, scalefactor)[source]

Scales a hex string by scalefactor. Returns scaled hex string.

odynn.utils.plot(ax, var, good_val=None)[source]
odynn.utils.save_show(show, save, name='', dpi=500)[source]

Show and/or save the current plot in utils.current_dir/name :param show: If True, show the plot :type show: bool :param save: If True, save the plot :type save: bool :param name: Name for the saved file :type name: str :param dpi: quality :type dpi: int

odynn.utils.set_dir(subdir)[source]

Set directory for saving files to utils.RES_DIR`+`subdir and create subfolders

Parameters:subdir (str) – name of the directory
Returns:complete path to the new directory
Return type:str

Module contents

Indices and tables