Skip to content

Commit 2f33444

Browse files
committed
Add connection mask for maximal distnance for 2d positions
1 parent 2836355 commit 2f33444

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

nestkernel/nestmodule.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,14 +1320,15 @@ NestModule::SetMaxBufferedFunction::execute( SLIInterpreter* i ) const
13201320
void
13211321
NestModule::EnableStructuralPlasticity_Function::execute( SLIInterpreter* i ) const
13221322
{
1323-
i->assert_stack_load( 3 );
1324-
const bool cache_prob = getValue< bool >( i->OStack.pick( 0 ) );
1325-
const double sigma = getValue< double >( i->OStack.pick( 1 ) );
1326-
const bool use_kernel = getValue< bool >( i->OStack.pick( 2 ) );
1323+
i->assert_stack_load( 4 );
1324+
const double max_distance = getValue< double >( i->OStack.pick( 0 ) );
1325+
const bool cache_prob = getValue< bool >( i->OStack.pick( 1 ) );
1326+
const double sigma = getValue< double >( i->OStack.pick( 2 ) );
1327+
const bool use_kernel = getValue< bool >( i->OStack.pick( 3 ) );
13271328

1328-
kernel().sp_manager.enable_structural_plasticity( use_kernel, sigma, cache_prob );
1329+
kernel().sp_manager.enable_structural_plasticity( use_kernel, sigma, cache_prob, max_distance );
13291330

1330-
i->OStack.pop( 3 );
1331+
i->OStack.pop( 4 );
13311332
i->EStack.pop();
13321333
}
13331334
void
@@ -2012,8 +2013,8 @@ NestModule::SelectNodesByMask_g_a_MFunction::execute( SLIInterpreter* i ) const
20122013
MaskedLayer< 2 > ml = MaskedLayer< 2 >( *layer, mask, false, layer_nc );
20132014

20142015
for ( Ntree< 2, size_t >::masked_iterator it = ml.begin( Position< 2 >( anchor[ 0 ], anchor[ 1 ] ) );
2015-
it != ml.end();
2016-
++it )
2016+
it != ml.end();
2017+
++it )
20172018
{
20182019
mask_node_ids.push_back( it->second );
20192020
}
@@ -2029,8 +2030,8 @@ NestModule::SelectNodesByMask_g_a_MFunction::execute( SLIInterpreter* i ) const
20292030
MaskedLayer< 3 > ml = MaskedLayer< 3 >( *layer, mask, false, layer_nc );
20302031

20312032
for ( Ntree< 3, size_t >::masked_iterator it = ml.begin( Position< 3 >( anchor[ 0 ], anchor[ 1 ], anchor[ 2 ] ) );
2032-
it != ml.end();
2033-
++it )
2033+
it != ml.end();
2034+
++it )
20342035
{
20352036
mask_node_ids.push_back( it->second );
20362037
}

nestkernel/sp_manager.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "connector_base.h"
3333
#include "connector_model.h"
3434
#include "kernel_manager.h"
35+
#include "mask.h"
3536
#include "nest_names.h"
3637
#include "sp_manager_impl.h"
3738
#include "spatial.h"
@@ -49,6 +50,8 @@ SPManager::SPManager()
4950
, sp_conn_builders_()
5051
, growthcurve_factories_()
5152
, growthcurvedict_( new Dictionary() )
53+
, structural_plasticity_max_distance_( std::numeric_limits< double >::infinity() )
54+
, distance_mask_template_( nullptr )
5255
{
5356
}
5457

@@ -274,7 +277,7 @@ SPManager::get_neuron_pair_index( int id1, int id2 )
274277
{
275278
int max_id = std::max( id1, id2 );
276279
int min_id = std::min( id1, id2 );
277-
int index = ( ( max_id ) * ( max_id - 1 ) ) / 2 + ( min_id - 1 );
280+
int index = ( ( max_id ) * ( max_id - 1 ) ) / 2 + ( min_id - 1 );
278281
return index;
279282
}
280283

@@ -304,7 +307,7 @@ SPManager::roulette_wheel_selection( const std::vector< double >& probabilities,
304307

305308
// Perform binary search to find the selected index
306309
auto it = std::lower_bound( cumulative.begin(), cumulative.end(), randomValue );
307-
return static_cast< int >( std::distance( cumulative.begin(), it ) );
310+
return static_cast< int >( std::distance( cumulative.begin(), it ) );
308311
}
309312

310313

@@ -365,13 +368,12 @@ SPManager::build_probability_list()
365368
continue;
366369
}
367370

368-
371+
369372
std::vector< double > pos_j(
370373
global_positions.begin() + pos_dim * ( id_j - 1 ), global_positions.begin() + pos_dim * id_j );
371374

372375
double prob = gaussian_kernel( pos_i, pos_j, structural_plasticity_gaussian_kernel_sigma_ );
373376
probability_list[ index ] = prob;
374-
375377
}
376378
}
377379
}
@@ -905,6 +907,16 @@ SPManager::global_shuffle_spatial( std::vector< size_t >& pre_ids,
905907
size_t pre_id = pre_ids.back();
906908
pre_ids.pop_back();
907909

910+
911+
// build an AnchoredMask about this pre‐neuron’s position
912+
std::vector< double > pre_pos(
913+
global_positions.begin() + ( pre_id - 1 ) * pos_dim, global_positions.begin() + pre_id * pos_dim );
914+
915+
AnchoredMask< 2 > distance_mask( *distance_mask_template_, // the BallMask centered at 0
916+
pre_pos // now anchored at pre_pos
917+
);
918+
919+
908920
std::vector< double > probabilities;
909921
std::vector< size_t > valid_post_ids;
910922
double rnd;
@@ -915,6 +927,16 @@ SPManager::global_shuffle_spatial( std::vector< size_t >& pre_ids,
915927
continue; // Skip self-connections
916928
}
917929

930+
// fetch post position
931+
std::vector< double > post_pos(
932+
global_positions.begin() + ( post_id - 1 ) * pos_dim, global_positions.begin() + post_id * pos_dim );
933+
934+
// HARD cutoff via the Mask:
935+
if ( !distance_mask.inside( post_pos ) )
936+
{
937+
continue; // distance > max_distance -> masked out entirely
938+
}
939+
918940
double prob;
919941
if ( structural_plasticity_cache_probabilities_ )
920942
{
@@ -929,13 +951,14 @@ SPManager::global_shuffle_spatial( std::vector< size_t >& pre_ids,
929951
}
930952
else
931953
{
954+
/**
932955
size_t pre_index = pre_id - 1;
933956
std::vector< double > pre_pos(
934957
global_positions.begin() + pre_index * pos_dim, global_positions.begin() + ( pre_index + 1 ) * pos_dim );
935958
936959
size_t post_index = post_id - 1;
937960
std::vector< double > post_pos(
938-
global_positions.begin() + post_index * pos_dim, global_positions.begin() + ( post_index + 1 ) * pos_dim );
961+
global_positions.begin() + post_index * pos_dim, global_positions.begin() + ( post_index + 1 ) * pos_dim );**/
939962

940963
prob = gaussian_kernel( pre_pos, post_pos, structural_plasticity_gaussian_kernel_sigma_ );
941964
}
@@ -973,7 +996,8 @@ SPManager::global_shuffle_spatial( std::vector< size_t >& pre_ids,
973996
void
974997
nest::SPManager::enable_structural_plasticity( bool use_gaussian_kernel,
975998
double gaussian_kernel_sigma,
976-
bool cache_probabilities )
999+
bool cache_probabilities,
1000+
double max_distance )
9771001
{
9781002
if ( kernel().vp_manager.get_num_threads() > 1 )
9791003
{
@@ -996,6 +1020,7 @@ nest::SPManager::enable_structural_plasticity( bool use_gaussian_kernel,
9961020
structural_plasticity_cache_probabilities_ = cache_probabilities;
9971021
structural_plasticity_enabled_ = true;
9981022

1023+
distance_mask_template_ = std::make_unique< BallMask< 2 > >( Position< 2 > { 0, 0 }, max_distance );
9991024
if ( use_gaussian_kernel )
10001025
{
10011026
gather_global_positions_and_ids();

nestkernel/sp_manager.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
// Includes from nestkernel:
3434
#include "growth_curve_factory.h"
35+
#include "mask.h"
3536
#include "nest_time.h"
3637
#include "nest_types.h"
3738
#include "node_collection.h"
@@ -127,7 +128,10 @@ class SPManager : public ManagerInterface
127128
/**
128129
* Enable structural plasticity
129130
*/
130-
void enable_structural_plasticity( bool use_gaussian_kernel, double gaussian_kernel_sigma, bool cache_probabilities );
131+
void enable_structural_plasticity( bool use_gaussian_kernel,
132+
double gaussian_kernel_sigma,
133+
bool cache_probabilities,
134+
double max_distance = 10000000 );
131135

132136
/**
133137
* Disable structural plasticity
@@ -284,6 +288,10 @@ class SPManager : public ManagerInterface
284288
*/
285289
int pos_dim;
286290

291+
292+
double structural_plasticity_max_distance_;
293+
std::unique_ptr< Mask< 2 > > distance_mask_template_;
294+
287295
/**
288296
* List of precomputed probabilities for neuron connections, indexed
289297
* by neuron pair indices for efficient lookup.

pynest/nest/lib/hl_api_simulation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def Install(module_name):
333333

334334
@check_stack
335335
def EnableStructuralPlasticity(
336-
use_gaussian_kernel=False, gaussian_kernel_sigma=0.0, cache_probabilites=False
336+
use_gaussian_kernel=False, gaussian_kernel_sigma=0.0, cache_probabilites=False, max_distance=float('inf'),
337337
):
338338
"""Enable structural plasticity for the network simulation
339339
@@ -346,6 +346,7 @@ def EnableStructuralPlasticity(
346346
sps(bool(use_gaussian_kernel))
347347
sps(float(gaussian_kernel_sigma))
348348
sps(bool(cache_probabilites))
349+
sps(float(max_distance))
349350

350351
sr("EnableStructuralPlasticity")
351352

0 commit comments

Comments
 (0)