SENeC-Initiative / DeNSE

Simulator for the Development of Neurons in Spatial Environments
https://dense.readthedocs.io/en/latest/
GNU General Public License v2.0
9 stars 4 forks source link

Problem with dense.set_object_properties() #41

Open SBottani opened 4 years ago

SBottani commented 4 years ago

A new issue when updating neurite parameters with dense.set_object_properties(), occuring possibly only with environment enabled (example program worked fine on april 9th): Continuing the simulation after parameter update leads to segmentation fault.

# Two successive simulation runs : ok
ds.simulate(1*day)
ds.simulate(1*day)
# Now update of parameters (error persist even if neurite_parameters unchanged)
  dense.set_object_properties(gids, neurite_params=neurite_params)
# Addition simulation run
ds.simulate(1*day)   # leads to segmentation fault

m Repeating simulation steps without calling dense.set_object_properties() works

    for i in range(3):
        ds.simulate(1*day)

Example code:

import os
import shutil
import time
import numpy as np
import matplotlib.pyplot as plt
import nngt
import dense as ds
from dense.units import *

current_dir = os.path.abspath(os.path.dirname(__file__))
main_dir = current_dir[:current_dir.rfind("/")]

def CleanFolder(tmp_dir, make=True):
    if os.path.isdir(tmp_dir):
        shutil.rmtree(tmp_dir)
    if make:
        os.mkdir(tmp_dir)
    return tmp_dir

def step(n, loop_n, plot=True):
    print("simulating", n)
    ds.simulate(n)
    print("done")
    if plot:
        _, ax = plt.subplots()
        ds.plot.plot_neurons(mode='mixed', subsample=1, axis=ax,
                             show_nodes=True, show_neuron_id=True, show=True)

'''
Main parameters
'''
np.random.seed(0)

soma_radius = 5.
num_neurons = 10

gc_model = 'cst_po_nwa'
use_vp = True

neuron_params = {
    "soma_radius": soma_radius * um,
}

dendrite_params = {
    "use_van_pelt": True,
    "growth_cone_model": gc_model,
    "speed_growth_cone": 0.06 * um / minute,
    "filopodia_wall_affinity": 0.01,
    "persistence_length": 200. * um,
    "B": 6. * cph,
    "T": 1000. * minute,
    "E": 1.,
}

axon_params = {
    "growth_cone_model": gc_model,
    "use_uniform_branching": False,
    "sensing_angle": 70.*deg,
    "speed_growth_cone": 0.1 * um / minute,
    "filopodia_wall_affinity": 10.,
    "filopodia_finger_length": 10. * um,
    "filopodia_min_number": 30,
    "affinity_axon_axon_other_neuron": 100.,

    "persistence_length": 500.*um,
    "taper_rate": 1./400.,
    "diameter_ratio_avg": 0.5,
    # branching
    "use_van_pelt": True,
    "use_uniform_branching": False,
    "filopodia_wall_affinity": 10.
}

neurite_params = {
    "axon": axon_params,
    "dendrites": dendrite_params
}

'''
Simulation
'''

if __name__ == '__main__':
    num_omp = 10
    kernel = {
              "seeds": range(num_omp),
              "num_local_threads": num_omp,
              "environment_required": True,
              "resolution": 5. * minute,
              "interactions": True}

    np.random.seed(118239)  # seeds for the neuron positions

    ds.set_kernel_status(kernel, simulation_id="ID")
    gids, culture = None, None

    if kernel["environment_required"]:
        shape = ds.environment.Shape.disk(300.)
        culture = ds.set_environment(shape)
        # generate the neurons inside the left chamber
        # pos_left = culture.seed_neurons(
        # neurons=100, xmax=540, soma_radius=soma_radius)
        neuron_params['position'] = culture.seed_neurons(
            neurons=num_neurons, soma_radius=soma_radius)
    else:
        neuron_params['position'] = \
            np.random.uniform(-1000, 1000, (num_neurons, 2))*um

    print("\nCreating neurons\n")
    gids = ds.create_neurons(n=num_neurons,
                             culture=culture,
                             params=neuron_params,
                             neurite_params=neurite_params,
                             num_neurites=3)

    rec = ds.create_recorders(gids, "num_growth_cones")
    start = time.time()
    for i in range(3):
        step(1 * day, 0, True)

    # dendrite_params.update({"speed_growth_cone": 0.04 * um / minute,
    #                         "use_van_pelt": False})

    # axon_params.update({"speed_growth_cone": 0.1 * um / minute,
    #                     "use_van_pelt": False,
    #                     "use_uniform_branching": True,
    #                     "uniform_branching_rate": 0.1 * cph,})
    # NB dictionary neurite_params is updated
    print("update parameters")
    ds.set_object_properties(gids, neurite_params=neurite_params)
    print("parameters updated")
    print("extension run")
    step(1 * day, 0, True)
    print("extension run done")
    duration = time.time() - start
    print(ds.get_kernel_status("time"))
    plt.show(block=True)
    print("SIMULATION ENDED")
Silmathoron commented 4 years ago

This bug is very weird... may take me a while to debug...

Silmathoron commented 4 years ago

could you confirm that the bug does not occur if you use a single thread?

SBottani commented 4 years ago

You're right ! Setting only ONE threat it runs OK, bug does not occur !

tfardet commented 3 years ago

OK, solved the issue, it was actually a pretty bad problem in the way parameters were set in parallel. Will create some tests to make sure it does not happen again and create a PR