00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef MODELNEURON_STRUCTUREPLOT_H_DEFINED
00039 #define MODELNEURON_STRUCTUREPLOT_H_DEFINED
00040
00041 #include "Image/Layout.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Image/DrawOps.H"
00045 #include "Image/ShapeOps.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/Range.H"
00048 #include "Util/StringConversions.H"
00049
00050 #include "ModelNeuron/LayerDecoder.H"
00051 #include "ModelNeuron/Location.H"
00052 #include "ModelNeuron/PlotBuffer.H"
00053
00054 class StructureModule;
00055
00056
00057
00058
00059 enum NormalizeType {SCALE, RANGE, SET_RANGE};
00060
00061 class StructurePlot
00062 {
00063 public:
00064
00065 StructurePlot(const StructureModule& structure,
00066 const NeuralDecoder& decoder,
00067 const NormalizeType normtype = SCALE,
00068 const double& rangemin = 0, const double& rangemax = 0);
00069
00070
00071 StructurePlot(const StructureModule& structure,
00072 const NormalizeType normtype = SCALE,
00073 const double& rangemin = 0, const double& rangemax = 0);
00074
00075
00076 ~StructurePlot();
00077
00078
00079 Layout<PixRGB<byte> > draw(const StructureModule& structure,
00080 const uint w = 320, const uint h = 240,
00081 const Dims dims = Dims(0,1),
00082 const uint depth = 2);
00083
00084
00085 void setProbe(Location probe);
00086
00087 private:
00088
00089 uint countSubs(const StructureModule& structure);
00090
00091
00092 Layout<byte> drawStructure(const StructureModule& structure,
00093 const uint w = 320, const uint h = 240,
00094 const uint depth = 2);
00095
00096
00097 StructurePlot(const StructurePlot& rhs);
00098 StructurePlot& operator=(const StructurePlot& rhs);
00099
00100 NormalizeType itsNormType;
00101 Range<double> itsRange;
00102 std::vector<std::string> itsNames;
00103 int itsTotal, itsCount;
00104 std::vector<LayerDecoder*> itsDecoder;
00105 Location itsProbe;
00106 PlotBufferList itsN;
00107 };
00108
00109
00110
00111
00112 inline
00113 StructurePlot::StructurePlot(const StructureModule& structure,
00114 const NeuralDecoder& decoder,
00115 const NormalizeType normtype,
00116 const double& rangemin, const double& rangemax) :
00117 itsNormType(normtype), itsRange(rangemin, rangemax),
00118 itsNames(), itsTotal(countSubs(structure)+1), itsCount(-1),
00119 itsDecoder(), itsProbe(), itsN()
00120 {
00121 for (int ii = 0; ii < itsTotal; ++ii)
00122 itsDecoder.push_back(new LayerDecoder(decoder, structure.getDims()));
00123 }
00124
00125
00126 inline
00127 StructurePlot::StructurePlot(const StructureModule& structure,
00128 const NormalizeType normtype,
00129 const double& rangemin, const double& rangemax) :
00130 itsNormType(normtype), itsRange(rangemin, rangemax),
00131 itsNames(), itsTotal(countSubs(structure)+1), itsCount(-1), itsDecoder(),
00132 itsProbe(), itsN()
00133 {
00134 }
00135
00136
00137 inline
00138 StructurePlot::~StructurePlot()
00139 {
00140 std::vector<LayerDecoder*>::const_iterator
00141 begin(itsDecoder.begin()), end(itsDecoder.end());
00142 while (begin != end)
00143 delete *begin++;
00144 }
00145
00146
00147 void StructurePlot::setProbe(Location probe)
00148 {
00149 itsN.clear();
00150 itsProbe = probe;
00151 };
00152
00153
00154 inline
00155 Layout<PixRGB<byte> >
00156 StructurePlot::draw(const StructureModule& structure,
00157 const uint w, const uint h, const Dims dims,
00158 const uint depth)
00159 {
00160 Layout<PixRGB<byte> > out;
00161
00162 Image<PixRGB<byte> > ds = drawStructure(structure,w,h,depth).render();
00163
00164 if (itsProbe.getDims() > 0)
00165 {
00166 itsN.push( structure.getUnit(itsProbe) );
00167 out = itsN.draw(false, 640, ds.getHeight(), dims);
00168 }
00169
00170 out = hcat(ds,out);
00171 return out;
00172 }
00173
00174
00175 inline
00176 Layout<byte>
00177 StructurePlot::drawStructure(const StructureModule& structure,
00178 const uint w, const uint h,
00179 const uint depth)
00180 {
00181 Layout<byte> layout;
00182
00183 if (depth != 0)
00184 for (uint i = 0; i < structure.numSubs(); ++i)
00185 {
00186 const StructureModule& temp = structure.getSub(i);
00187 uint mydepth = depth - 1;
00188 layout = hcat(layout, drawStructure(temp, w, h, mydepth));
00189 }
00190
00191
00192 ++itsCount;
00193 Image<double> out;
00194 if (itsDecoder.size() > 0)
00195 {
00196 itsDecoder[itsCount]->push(structure.getOutput());
00197 out = itsDecoder[itsCount]->getOutput();
00198 }
00199 else
00200 out = structure.getOutput();
00201
00202
00203 Image<byte> outb;
00204 if (itsNormType == SCALE)
00205 {
00206 inplaceNormalize(out, 0.0, 255.0);
00207 outb = out;
00208 }
00209 else
00210 {
00211 if (itsNormType == RANGE)
00212 itsRange.merge(rangeOf(out));
00213 outb = remapRange(out, itsRange, Range<double>(0.0, 255.0));
00214 }
00215
00216 outb = rescale(outb, Dims(w,h));
00217 if ((itsNormType == RANGE) || (itsNormType == SET_RANGE))
00218 {
00219 std::string temp = itsNames[itsCount] + " " +
00220 convertToString(itsRange.min()) + ":" +
00221 convertToString(itsRange.max());
00222 writeText(outb, Point2D<int>(0,0), temp.c_str());
00223 }
00224 else
00225 writeText(outb, Point2D<int>(0,0), itsNames[itsCount].c_str());
00226
00227 Rectangle r(Point2D<int>(0,0), Dims(outb.getWidth(),outb.getHeight()));
00228 drawRectSquareCorners(outb ,r , (byte)255, 2);
00229 layout = vcat(outb, layout);
00230
00231 if (itsCount == itsTotal-1)
00232 itsCount = -1;
00233
00234 return layout;
00235 }
00236
00237
00238 inline
00239 uint StructurePlot::countSubs(const StructureModule& structure)
00240 {
00241 uint child = 0;
00242 for (uint i = 0; i < structure.numSubs(); ++i)
00243 {
00244 const StructureModule& temp(structure.getSub(i));
00245 child += countSubs(temp);
00246 }
00247 child += structure.numSubs();
00248 itsNames.push_back(structure.getName());
00249
00250 return child;
00251 }
00252
00253 #endif
00254
00255
00256
00257
00258
00259