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(¶ms_,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
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
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
00120
00121
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
00144 if (type == WtScale::Fixed) {
00145 factor = fixed_weight_multiplier;
00146 offset = fixed_weight_offset;
00147 }
00148 else {
00149
00150
00151
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
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
00189 message(Msg::Error,"Unknown WtScale type: " + String::stringrep(int(type)));
00190 factor = 0; break;
00191 }
00192 }
00193
00194
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
00250
00251
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
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
00296