<commit_before>/*
 * HyPerConnection.hpp
 *
 *  Created on: Oct 21, 2008
 *      Author: <NAME>
 */

#ifndef HYPERCONN_HPP_
#define HYPERCONN_HPP_

#include "PVConnection.h"
#include "../columns/InterColComm.hpp"
#include "../include/pv_types.h"
#include "../io/PVParams.hpp"
#include "../layers/HyPerLayer.hpp"
#include "../utils/Timer.hpp"

#define PROTECTED_NUMBER 13
#define MAX_ARBOR_LIST (1+MAX_NEIGHBORS)

namespace PV {

class HyPerCol;
class HyPerLayer;
class ConnectionProbe;

/**
 * A PVConnection identifies a connection between two layers
 */
typedef struct {
   int delay; // current output delay in the associated f ring buffer (should equal fixed delay + varible delay for valid connection)
   int fixDelay; // fixed output delay. TODO: should be float
   int varDelayMin; // minimum variable conduction delay
   int varDelayMax; // maximum variable conduction delay
   int numDelay;
   int isGraded; //==1, release is stochastic with prob = (activity <= 1), default is 0 (no graded release)
   float vel;  // conduction velocity in position units (pixels) per time step--added by GTK
   float rmin; // minimum connection distance
   float rmax; // maximum connection distance
} PVConnParams;

class HyPerConn {

public:
   HyPerConn();
   HyPerConn(const char * name, HyPerCol * hc, HyPerLayer * pre, HyPerLayer * post,
             ChannelType channel);
   HyPerConn(const char * name, HyPerCol * hc, HyPerLayer * pre, HyPerLayer * post,
             ChannelType channel, const char * filename);
   virtual ~HyPerConn();

   virtual int deliver(Publisher * pub, PVLayerCube * cube, int neighbor);

   virtual int insertProbe(ConnectionProbe * p);
   virtual int outputState(float time, bool last=false);
   virtual int updateState(float time, float dt);
   virtual int updateWeights(int axonId);

   inline  int numberOfAxonalArborLists()            {return numAxonalArborLists;}
   virtual int numWeightPatches(int arbor);
   virtual int numDataPatches(int arbor);
   virtual int writeWeights(float time, bool last=false);
   virtual int writeWeights(PVPatch ** patches, int numPatches,
                            const char * filename, float time, bool last);
   virtual int writeTextWeights(const char * filename, int k);
   virtual int writePostSynapticWeights(float time, bool last=false);

   int readWeights(const char * filename);
   virtual PVPatch ** readWeights(PVPatch ** patches, int numPatches,
                                  const char * filename);

   virtual PVPatch * getWeights(int kPre, int arbor);
   virtual PVPatch * getPlasticityIncrement(int k, int arbor);

   inline PVLayerCube * getPlasticityDecrement()     {return pDecr;}

   inline PVPatch ** weights(int neighbor)           {return wPatches[neighbor];}

   inline const char * getName()                     {return name;}
   inline int          getDelay()                    {return params->delay;}

   virtual float minWeight()                          {return 0.0;}
   virtual float maxWeight()                          {return wMax;}

   inline int xPatchSize()                           {return nxp;}
   inline int yPatchSize()                           {return nyp;}
   inline int fPatchSize()                           {return nfp;}

   inline PVAxonalArbor * axonalArbor(int kPre, int neighbor)
      {return &axonalArborList[neighbor][kPre];}

   HyPerLayer * preSynapticLayer()     {return pre;}
   HyPerLayer * postSynapticLayer()    {return post;}

   int  getConnectionId()              {return connId;}
   void setConnectionId(int id)        {connId = id;}

   int setParams(PVParams * params, PVConnParams * p);

   PVPatch ** convertPreSynapticWeights(float time);

   int preSynapticPatchHead(int kxPost, int kyPost, int kfPost, int * kxPre, int * kyPre);
   int postSynapticPatchHead(int kPre,
                             int * kxPostOut, int * kyPostOut, int * kfPostOut,
                             int * dxOut, int * dyOut, int * nxpOut, int * nypOut);

   virtual int gauss2DCalcWeights(PVPatch * wp, int kPre, int noPost,
                          int numFlanks, float shift, float rotate, float aspect, float sigma,
                          float r2Max, float strength);

   virtual int cocircCalcWeights(PVPatch * wp, int kPre, int noPre, int noPost,
         float sigma_cocirc, float sigma_kurve, float sigma_chord, float delta_theta_max,
         float cocirc_self, float delta_radius_curvature, int numFlanks, float shift,
         float aspect, float rotate, float sigma, float r2Max, float strength);

   virtual PVPatch ** normalizeWeights(PVPatch ** patches, int numPatches);

   virtual int kernelIndexToPatchIndex(int kernelIndex, int * kxPatchIndex = NULL,
         int * kyPatchIndex = NULL, int * kfPatchIndex = NULL);

   virtual int patchIndexToKernelIndex(int patchIndex, int * kxKernelIndex = NULL,
         int * kyKernelIndex = NULL, int * kfKernelIndex = NULL);

protected:
   HyPerLayer     * pre;
   HyPerLayer     * post;
   HyPerCol       * parent;
   PVLayerCube    * pDecr;      // plasticity decrement variable (Mi) for pre-synaptic layer
   PVPatch       ** pIncr;      // list of stdp patches Psij variable
   PVPatch       ** wPatches[MAX_ARBOR_LIST]; // list of weight patches, one set per neighbor
   PVPatch       ** wPostPatches;  // post-synaptic linkage of weights
   PVAxonalArbor  * axonalArborList[MAX_ARBOR_LIST]; // list of axonal arbors for each neighbor

   ChannelType channel;              // which channel of the post to update (e.g. inhibit)
   int connId;               // connection id<commit_msg>Modified class to include local Wmax variables, and their dynamic evolution, in the post synaptic layer.<commit_after>/*
 * HyPerConnection.hpp
 *
 *  Created on: Oct 21, 2008
 *      Author: <NAME>
 */

#ifndef HYPERCONN_HPP_
#define HYPERCONN_HPP_

#include "PVConnection.h"
#include "../columns/InterColComm.hpp"
#include "../include/pv_types.h"
#include "../io/PVParams.hpp"
#include "../layers/HyPerLayer.hpp"
#include "../utils/Timer.hpp"

#define PROTECTED_NUMBER 13
#define MAX_ARBOR_LIST (1+MAX_NEIGHBORS)

namespace PV {

class HyPerCol;
class HyPerLayer;
class ConnectionProbe;

/**
 * A PVConnection identifies a connection between two layers
 */
typedef struct {
   int delay; // current output delay in the associated f ring buffer (should equal fixed delay + varible delay for valid connection)
   int fixDelay; // fixed output delay. TODO: should be float
   int varDelayMin; // minimum variable conduction delay
   int varDelayMax; // maximum variable conduction delay
   int numDelay;
   int isGraded; //==1, release is stochastic with prob = (activity <= 1), default is 0 (no graded release)
   float vel;  // conduction velocity in position units (pixels) per time step--added by GTK
   float rmin; // minimum connection distance
   float rmax; // maximum connection distance
} PVConnParams;

class HyPerConn {

public:
   HyPerConn();
   HyPerConn(const char * name, HyPerCol * hc, HyPerLayer * pre, HyPerLayer * post,
             ChannelType channel);
   HyPerConn(const char * name, HyPerCol * hc, HyPerLayer * pre, HyPerLayer * post,
             ChannelType channel, const char * filename);
   virtual ~HyPerConn();

   virtual int deliver(Publisher * pub, PVLayerCube * cube, int neighbor);

   virtual int insertProbe(ConnectionProbe * p);
   virtual int outputState(float time, bool last=false);
   virtual int updateState(float time, float dt);
   virtual int updateWmax();
   virtual int updateWeights(int axonId);

   inline  int numberOfAxonalArborLists()            {return numAxonalArborLists;}
   virtual int numWeightPatches(int arbor);
   virtual int numDataPatches(int arbor);
   virtual int writeWeights(float time, bool last=false);
   virtual int writeWeights(PVPatch ** patches, int numPatches,
                            const char * filename, float time, bool last);
   virtual int writeTextWeights(const char * filename, int k);
   virtual int writePostSynapticWeights(float time, bool last=false);

   int readWeights(const char * filename);
   virtual PVPatch ** readWeights(PVPatch ** patches, int numPatches,
                                  const char * filename);

   virtual PVPatch * getWeights(int kPre, int arbor);
   virtual PVPatch * getPlasticityIncrement(int k, int arbor);

   inline PVLayerCube * getPlasticityDecrement()     {return pDecr;}

   inline PVPatch ** weights(int neighbor)           {return wPatches[neighbor];}

   inline const char * getName()                     {return name;}
   inline int          getDelay()                    {return params->delay;}

   virtual float minWeight()                          {return 0.0;}
   virtual float maxWeight()                          {return wMax;}

   inline int xPatchSize()                           {return nxp;}
   inline int yPatchSize()                           {return nyp;}
   inline int fPatchSize()                           {return nfp;}

   inline PVAxonalArbor * axonalArbor(int kPre, int neighbor)
      {return &axonalArborList[neighbor][kPre];}

   HyPerLayer * preSynapticLayer()     {return pre;}
   HyPerLayer * postSynapticLayer()    {return post;}

   int  getConnectionId()              {return connId;}
   void setConnectionId(int id)        {connId = id;}

   int setParams(PVParams * params, PVConnParams * p);

   PVPatch ** convertPreSynapticWeights(float time);

   int preSynapticPatchHead(int kxPost, int kyPost, int kfPost, int * kxPre, int * kyPre);
   int postSynapticPatchHead(int kPre,
                             int * kxPostOut, int * kyPostOut, int * kfPostOut,
                             int * dxOut, int * dyOut, int * nxpOut, int * nypOut);

   virtual int gauss2DCalcWeights(PVPatch * wp, int kPre, int noPost,
                          int numFlanks, float shift, float rotate, float aspect, float sigma,
                          float r2Max, float strength);

   virtual int cocircCalcWeights(PVPatch * wp, int kPre, int noPre, int noPost,
         float sigma_cocirc, float sigma_kurve, float sigma_chord, float delta_theta_max,
         float cocirc_self, float delta_radius_curvature, int numFlanks, float shift,
         float aspect, float rotate, float sigma, float r2Max, float strength);

   virtual PVPatch ** normalizeWeights(PVPatch ** patches, int numPatches);

   virtual int kernelIndexToPatchIndex(int kernelIndex, int * kxPatchIndex = NULL,
         int * kyPatchIndex = NULL, int * kfPatchIndex = NULL);

   virtual int patchIndexToKernelIndex(int patchIndex, int * kxKernelIndex = NULL,
         int * kyKernelIndex = NULL, int * kfKernelIndex = NULL);

protected:
   HyPerLayer     * pre;
   HyPerLayer     * post;
   HyPerCol       * parent;
   PVLayerCube    * pDecr;      // plasticity decrement variable (Mi) for pre-synaptic layer
   PVPatch       ** pIncr;      // list of stdp patches Psij variable
   PVPatch       ** wPatches[MAX_ARBOR_LIST]; // list of weight patches, one set per neighbor
   PVPatch       ** wPostPatches;  // post-synaptic linkage of weights
   PVAxonalArbor  * axonalArborList[MAX_ARBOR_LIST]; // list of axonal arbors for each neighbor

   bool     localWmaxFlag;  // presence of rate dependent wMax;
   pvdata_t * Wmax;  // adaptive upper STDP weight boundary
   float    alphaW;  // params in Wmax dynamics.
   float    gammaW;
   float    averageR; // predefined average rate

   FILE     * wmaxFP;


   ChannelType channel;              // which channel of the post to update (e.g. inhibit)
   int connId;               // connection id
{'fraction_non_alphanumeric': 0.08560783294341912, 'fraction_numerical': 0.0029460185425872975, 'mean_word_length': 2.789231779382797, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7531934', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3250, 'n_words': 1092}