Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

neuralregionmanager.h

Go to the documentation of this file.
00001 
00007 #ifndef __NEURALREGIONMANAGER_H__
00008 #define __NEURALREGIONMANAGER_H__
00009 
00010 #include "neuralregion.h"
00011 #include "matrixadapter.h"
00012 #include "plotspecification.h"
00013 #include "parametermap.h"
00014 #include "ppm_draw.h"
00015 
00016 
00017 
00019 class NeuralRegionManager {
00020   typedef string bStr;
00021   typedef const string cStr;
00022   typedef InternalNeuralRegion::Weights Weights;
00023   typedef Scale::Linear<double> ScaleType;
00024   
00025 public:
00033   NeuralRegionManager(NeuralRegion* region, ParamMap& params_,
00034                       bool owns_params=true, const bool register_maps=true)
00035     : r(region), params(&params_,owns_params) {
00036     if (register_maps) {
00037       const NeuralRegion::Table& table = r->table();
00038       for (NeuralRegion::Table::const_iterator i=table.begin(); i!=table.end(); i++) {
00039         const string name = i->first;
00040         define_plot("Activity", name, name);    
00041       }
00042       region->table_set("Activity", new NeuralRegion::ActivityAdapter(region->const_activity()));
00043       define_plot("Activity", "Activity", "Activity");
00044 
00045       region->table_set("Backprojection", new NeuralRegion::ActivityAdapter(region->backprojection()));
00046       define_plot("Backprojection", "Backprojection", "Backprojection");
00047 
00048       region->table_set("Residual", new NeuralRegion::ActivityAdapter(region->residual_activity()));
00049       define_plot("Residual", "Residual", "Residual");
00050     }
00051 
00052     /* Declare useful parameters.  Should probably also mark them read-only. */
00053     ParameterMap<>& p = *dynamic_cast< ParameterMap<>* >(params.valueptr());
00054     p.set_local("plastic", (region->is_plastic()?  True : False ));
00055     p.set_local("internal",(region->is_internal()? True : False ));
00056   }
00057   
00058   virtual ~NeuralRegionManager() {  }
00059 
00066   void map_add(const string& name, NeuralRegion::MapBaseType* map) {
00067     assert (int(map->nrows())==int(r->const_activity().nrows()));
00068     assert (int(map->ncols())==int(r->const_activity().ncols()));
00069     r->table_set(name,map);
00070   }
00071 
00073   typedef Plot::PlotSpecification<NeuralRegion::MapBaseType,bStr,Scale::Linear<>,
00074                                   NeuralRegion::Table> PlotSpec;
00075   typedef std::vector<PlotSpec*>                     PlotSpecList;
00076   typedef PointerMap<PlotSpecList>              PlotSpecListTable;
00077 
00079   virtual PlotSpecList::const_iterator plotspeclist_begin(const string& group) const {
00080     const PlotSpecList* p=plotspecs.getptr(group);
00081     return (p? plotspecs.getptr(group)->begin() : PlotSpecList::const_iterator());
00082   }
00083 
00085   virtual PlotSpecList::const_iterator plotspeclist_end(const string& group) const {
00086     const PlotSpecList* p=plotspecs.getptr(group);
00087     return (p? plotspecs.getptr(group)->end() : PlotSpecList::const_iterator());
00088   }
00089 
00092   inline PlotSpec* define_plot(const string& group, cStr& plotname,
00093                                cStr& strength, cStr& color="", cStr& confidence="")
00094     {  return define_plot(group,newPlotSpec(plotname,strength,color,confidence));  }
00095 
00096 
00098   inline bool add_maps_for_unit(PlotSpec::MatrixTable& maptable,
00099                                 const PlotSpec& spec,
00100                                 const int ui, const int uj) const 
00101     {  return add_maps_for_unit(maptable,spec.name(),spec.name(),spec,ui,uj);  }
00102     
00103   inline bool add_maps_for_unit(PlotSpec::MatrixTable& maptable,
00104                                 const string& weightname,
00105                                 const string& mapname,
00106                                 const PlotSpec& spec,
00107                                 const int ui, const int uj) const {
00108     
00109     const InternalNeuralRegion* p = internalregion();  assert(p);
00110 
00111     /* Error checking */
00112     if (ui>=p->nrows() || uj>=p->ncols()) {
00113       message(Msg::Error,"Unit (" +
00114               String::stringrep(ui) + "," +
00115               String::stringrep(uj) + ") is out of range of region " + p->name() );
00116       return false;
00117     }
00118 
00119     /* Add a map for the specified weights.  Currently does not supply
00120        any "reference count" arguments to the weight_scale command,
00121        since those are tough to determine in general. */
00122     const Scale::Linear<double> scale = weight_scale(spec, p->weights(weightname,ui,uj));
00123     maptable.set(mapname,p->weights(weightname,ui,uj).as_map(*(spec.reference_mapptr()),scale));
00124 
00125     return true;
00126   }
00127 
00128 
00134   Scale::Linear<double> weight_scale(const PlotSpec& spec, const Weights& weights, int reference_count=-1) const {
00135     double factor=1.0;
00136     double offset=0.0;
00137     
00138     const WtScale::Type type       = WtScale::Type(spec.lookup_param("ppm_weight_scale_type",       int(WtScale::Relative)));
00139     const double        fixed_weight_multiplier  = spec.lookup_param("ppm_weight_fixed_multiplier", 1.0);
00140     const double        fixed_weight_offset      = spec.lookup_param("ppm_weight_fixed_offset",     1.0);
00141     const double        threshold                = spec.lookup_param("ppm_weight_threshold",        1.0);
00142     
00143     /* Simplest case */
00144     if (type == WtScale::Fixed) {
00145       factor = fixed_weight_multiplier;
00146       offset = fixed_weight_offset;
00147       }
00148     else {
00149       /* Compute scaling from max value, min value, number of connections, and/or num active */
00150 
00151       /* Initialize counts and totals to minimum or maximum possible, as appropriate */
00152       double max    = 0.0;
00153       double min    = 1.0;
00154       int    count  =   0;
00155       int    active =   0;
00156 
00157       typedef Weights::Matrix   Matrix;
00158       typedef Weights::Bounds   Bounds;
00159       typedef Matrix::size_type Subscript;
00160       
00161       const Matrix&   A  = weights.matrix();
00162       const Bounds&   b  = weights.bounds();
00163       const Subscript ro = b.aarectangle().corners().xl;
00164       const Subscript co = b.aarectangle().corners().yl;
00165       
00166       for   (Subscript rr=0; rr<A.nrows(); rr++)
00167         for (Subscript cc=0; cc<A.ncols(); cc++)
00168           if (b.inside(rr+ro,cc+co)) {
00169             count++;
00170             const double value = mat::elem(A,rr,cc);
00171             if (value > threshold) {
00172               active++;
00173               if (value > max) max = value;
00174               if (value < min) min = value;
00175             }
00176           }
00177           
00178       /* Override actual count if appropriate */
00179       if (reference_count>0) count = reference_count;
00180 
00181       switch (type) {
00182       case WtScale::Relative:     factor = ((max-min)>0 ? 1.0/(max-min) : 1.0);
00183                                   offset = -min;                    break;
00184       case WtScale::PeakRelative: factor = (max>0 ? 1.0/max : 1.0); break;
00185       case WtScale::Active:       factor = 1.0*active;              break; 
00186       case WtScale::Count:        factor = 1.0*count;               break;
00187       default:
00188         /* It would be nice to limit the number of these messages generated... */
00189         message(Msg::Error,"Unknown WtScale type: " + String::stringrep(int(type)));
00190         factor = 0; break;
00191       }
00192     }
00193     
00194     //cout << "plot: (" << factor << "," << offset << ")" << endl;
00195     return ScaleType(factor,offset);
00196   }
00197 
00198   
00200   inline NeuralRegion* region() {  return r;  }
00201 
00203   inline const NeuralRegion* region() const {  return r;  }
00204 
00208   inline InternalNeuralRegion* internalregion()
00209     {  return dynamic_cast<InternalNeuralRegion*>(r);  }
00210 
00212   inline const InternalNeuralRegion* internalregion() const
00213     {  return dynamic_cast<const InternalNeuralRegion*>(r);  }
00214 
00215 
00222   PlotSpec* add_input(const string& name,
00223                       NeuralRegionManager& upstream_manager,
00224                       InternalNeuralRegion::WeightFunction& fn,
00225                       NeuralRegion::Length size_scale=1.0) {
00226     assert (region());
00227     assert (internalregion());
00228     internalregion()->add_input(name,*(upstream_manager.region()), fn, size_scale);
00229     PlotSpec* p=upstream_manager.newPlotSpec(name,name);
00230     assert (p);
00231     define_plot("Weights",p);
00232     return p;
00233   }
00234 
00236   int default_ui() const {  return std::max(0,region()->nrows()/2-1);  }
00237   int default_uj() const {  return std::max(0,region()->ncols()/2-1);  }
00238 
00239   static void message(Msg::MessageLevel m, const string& s)
00240     {  PlotSpec::message(m,s);  }
00241 
00242   ParamMap&       param_map()       {  return *(params.valueptr());  }
00243   const ParamMap& param_map() const {  return *params;               }
00244   
00245 private:  
00246 
00248   PlotSpec* newPlotSpec(cStr& plotname, cStr& strength, cStr& color="", cStr& confidence="") const {
00249     // Can the dynamic_cast be eliminated?
00250     /* Specific type is needed here for set_local; requires that the
00251        call originally used a ParameterMap<>. */
00252     ParameterMap<>* p = dynamic_cast< ParameterMap<>* >(params.valueptr());
00253     assert (p);
00254     return new PlotSpec(r->name(),r->table(),plotname,*p,strength,color,confidence);
00255   }
00256     
00258   PlotSpec* newPlotSpec(PlotSpec& o, cStr& plotname, cStr& strength, cStr& color="", cStr& confidence="") const {
00259     // Can the dynamic_cast be eliminated?
00260     ParameterMap<>* p= dynamic_cast<ParameterMap<>*>(&(o.param_map()));
00261     assert (p);
00262     return new PlotSpec(r->name(),r->table(),plotname,*p,strength,color,confidence);
00263   }
00264 
00266   inline PlotSpecList& plotspec_group(const string& name) {
00267     PlotSpecList* p=plotspecs.getptr(name);
00268     if (!p) {
00269       p=new PlotSpecList;
00270       plotspecs.set(name,p);
00271     }
00272     return *p;
00273   }
00274 
00278   inline PlotSpec* define_plot(const string& group,PlotSpec* spec) {
00279     Generic::insert_named(plotspec_group(group), spec->name(), spec);
00280     return spec;
00281   }
00282 
00284   PlotSpecListTable plotspecs;
00285 
00287   NeuralRegion* r;
00288 
00290   OwningPointer<ParamMap> params;
00291 };
00292 
00293 
00294 
00295 #endif /* __NEURALREGIONMANAGER_H__ */
00296 

Generated on Mon Jan 20 02:35:45 2003 for RF-LISSOM by doxygen1.3-rc2