/*
 *  test_DataConnect.sli
 *
 *  This file is part of NEST.
 *
 *  Copyright (C) 2004 The NEST Initiative
 *
 *  NEST is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  NEST 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NEST.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/* BeginDocumentation
   Name: testsuite::test_DataConnect - sli script to test if DataConnect works as expected

   Synopsis: (test_DataConnect) run

   Description:

   This test generates 100 iaf_neurons and creates a connectivity vector per neuron

   SeeAlso: DataConnect

   FirstVersion: May 2012
   Author: Marc-Oliver Gewaltig
 */

(unittest) run
/unittest using

% Create Random number generator
rngdict /MT19937 get 1234 CreateRNG /rng Set

rng rdevdict /uniform_int get CreateRDV /unidv Set
rng rdevdict /normal get CreateRDV /normdv_w Set
rng rdevdict /normal get CreateRDV /normdv_d Set

unidv
<< 
  /low 1 
  /high 100
>> SetStatus

normdv_w
<< 
  /mu 10. 
  /sigma 0.5
>> SetStatus

normdv_d
<< 
  /mu 1. 
  /sigma 0.1
>> SetStatus

/n_neurons 100 def
/n_syn 10 def

/iaf_neuron n_neurons Create /n Set

/synapses
[
 n_neurons 
 {
  <<
    /target unidv n_syn RandomArray 1.0 mul
    /weight normdv_w n_syn RandomArray 
    /delay  normdv_d n_syn RandomArray { dup 0.1 lt { pop 0.1 } if } Map % clip small values
  >>  
 } repeat
] def

1 1 n_neurons
{
 dup synapses exch 1 sub get /static_synapse DataConnect
} for

(Done Connecting) =

% Check for one source neuron
/nrn 5 def
 << /source [nrn] /synapse_model /static_synapse >> GetConnections /conn Set
 conn {GetStatus} Map /syn5 Set

 syn5 { /weight get } Map /weights Set
 syn5 { /delay get} Map /delays Set
 syn5 { /target get cvd } Map /targets Set

 synapses nrn 1 sub get /target get doublevector2array
 targets  eq assert_or_die

% Now the second variant of dataconnect which takes a list of status dicts.

% Step 1: read out all connections and retrieve the synapse status

<< /synapse_model /static_synapse >> GetConnections {GetStatus} Map
/connectome Set

% Step 2: reset the kernel 
ResetKernel

% Step 3: rebuild the neurons
/iaf_neuron n_neurons Create /n Set

% Step 4: Rebuild the connections
% Pass the connectome to DataConnect
connectome DataConnect

% Step 5: Check for one source neuron
/nrn 5 def
 << /source [nrn] /synapse_model /static_synapse >> GetConnections /conn Set
 conn {GetStatus} Map /syn5 Set

 syn5 { /weight get } Map /weights Set
 syn5 { /delay get} Map /delays Set
 syn5 { /target get cvd } Map /targets Set

 synapses nrn 1 sub get /target get doublevector2array
 targets  eq assert_or_die

endusing
