Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/recstatistics.cc
[mareframe] / trunk / gadget / recstatistics.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/recstatistics.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "recstatistics.h"
2 :     #include "commentstream.h"
3 :     #include "readfunc.h"
4 :     #include "readword.h"
5 :     #include "errorhandler.h"
6 :     #include "areatime.h"
7 :     #include "fleet.h"
8 :     #include "stock.h"
9 :     #include "stockprey.h"
10 :     #include "mathfunc.h"
11 :     #include "readaggregation.h"
12 :     #include "gadget.h"
13 :     #include "global.h"
14 :    
15 :     RecStatistics::RecStatistics(CommentStream& infile, const AreaClass* const Area,
16 :     const TimeClass* const TimeInfo, double weight, TagPtrVector Tag, const char* name)
17 :     : Likelihood(RECSTATISTICSLIKELIHOOD, weight, name), aggregator(0), alptr(0) {
18 :    
19 :     char text[MaxStrLength];
20 :     strncpy(text, "", MaxStrLength);
21 :     int i, j, check, numarea = 0;
22 :    
23 :     char datafilename[MaxStrLength];
24 :     char aggfilename[MaxStrLength];
25 :     strncpy(datafilename, "", MaxStrLength);
26 :     strncpy(aggfilename, "", MaxStrLength);
27 :     ifstream datafile;
28 :     CommentStream subdata(datafile);
29 :    
30 :     functionname = new char[MaxStrLength];
31 :     strncpy(functionname, "", MaxStrLength);
32 :     readWordAndValue(infile, "datafile", datafilename);
33 :     readWordAndValue(infile, "function", functionname);
34 :    
35 :     if ((strcasecmp(functionname, "lengthcalcvar") == 0) || (strcasecmp(functionname, "lengthcalcstddev") == 0))
36 :     functionnumber = 1;
37 :     else if ((strcasecmp(functionname, "lengthgivenvar") == 0) || (strcasecmp(functionname, "lengthgivenstddev") == 0))
38 :     functionnumber = 2;
39 :     else if ((strcasecmp(functionname, "lengthnovar") == 0) || (strcasecmp(functionname, "lengthnostddev") == 0))
40 :     functionnumber = 3;
41 :     else
42 :     handle.logFileMessage(LOGFAIL, "\nError in recstatistics - unrecognised function", functionname);
43 :    
44 :     //read in area aggregation from file
45 :     readWordAndValue(infile, "areaaggfile", aggfilename);
46 :     datafile.open(aggfilename, ios::in);
47 :     handle.checkIfFailure(datafile, aggfilename);
48 :     handle.Open(aggfilename);
49 :     numarea = readAggregation(subdata, areas, areaindex);
50 :     handle.Close();
51 :     datafile.close();
52 :     datafile.clear();
53 :    
54 :     //Must change from outer areas to inner areas.
55 :     for (i = 0; i < areas.Nrow(); i++)
56 :     for (j = 0; j < areas.Ncol(i); j++)
57 :     areas[i][j] = Area->getInnerArea(areas[i][j]);
58 :    
59 :     //read in the fleetnames
60 :     i = 0;
61 :     infile >> text >> ws;
62 :     if (strcasecmp(text, "fleetnames") != 0)
63 :     handle.logFileUnexpected(LOGFAIL, "fleetnames", text);
64 :     infile >> text;
65 :     while (!infile.eof() && (strcasecmp(text, "[component]") != 0)) {
66 :     infile >> ws;
67 :     fleetnames.resize(new char[strlen(text) + 1]);
68 :     strcpy(fleetnames[i++], text);
69 :     infile >> text;
70 :     }
71 :     if (fleetnames.Size() == 0)
72 :     handle.logFileMessage(LOGFAIL, "\nError in recstatistics - failed to read fleets");
73 :     handle.logMessage(LOGMESSAGE, "Read fleet data - number of fleets", fleetnames.Size());
74 :    
75 :     //We have now read in all the data from the main likelihood file
76 :     //But we have to read in the statistics data from datafilename
77 :     datafile.open(datafilename, ios::in);
78 :     handle.checkIfFailure(datafile, datafilename);
79 :     handle.Open(datafilename);
80 :     readStatisticsData(subdata, TimeInfo, numarea, Tag);
81 :     handle.Close();
82 :     datafile.close();
83 :     datafile.clear();
84 :    
85 :     for (j = 0; j < tagnames.Size(); j++) {
86 :     check = 0;
87 :     for (i = 0; i < Tag.Size(); i++) {
88 :     if (strcasecmp(tagnames[j], Tag[i]->getName()) == 0) {
89 :     check++;
90 :     tagvec.resize(Tag[i]);
91 :     }
92 :     }
93 :     if (check == 0)
94 :     handle.logMessage(LOGFAIL, "Error in recstatistics - failed to match tag", tagnames[j]);
95 :     }
96 :     }
97 :    
98 :     void RecStatistics::readStatisticsData(CommentStream& infile,
99 :     const TimeClass* TimeInfo, int numarea, TagPtrVector Tag) {
100 :    
101 :     double tmpnumber, tmpmean, tmpstddev;
102 :     char tmparea[MaxStrLength], tmptag[MaxStrLength];
103 :     strncpy(tmparea, "", MaxStrLength);
104 :     strncpy(tmptag, "", MaxStrLength);
105 :     int keepdata, needvar, readvar;
106 :     int i, timeid, tagid, areaid, tmpindex;
107 :     int year, step, count, reject;
108 :     char* tagName;
109 :    
110 :     readvar = 0;
111 :     if (functionnumber == 2)
112 :     readvar = 1;
113 :     needvar = 0;
114 :     if (functionnumber == 1)
115 :     needvar = 1;
116 :    
117 :     //Check the number of columns in the inputfile
118 :     infile >> ws;
119 :     if ((readvar) && (countColumns(infile) != 7))
120 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 7");
121 :     else if ((!readvar) && (countColumns(infile) != 6))
122 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 6");
123 :    
124 :     year = step = count = reject = 0;
125 :     while (!infile.eof()) {
126 :     keepdata = 1;
127 :     if (readvar)
128 :     infile >> tmptag >> year >> step >> tmparea >> tmpnumber >> tmpmean >> tmpstddev >> ws;
129 :     else
130 :     infile >> tmptag >> year >> step >> tmparea >> tmpnumber >> tmpmean >> ws;
131 :    
132 :     //crude check to see if something has gone wrong and avoid infinite loops
133 :     if (strlen(tmparea) == 0)
134 :     handle.logFileMessage(LOGFAIL, "failed to read data from file");
135 :    
136 :     //if tmparea is in areaindex find areaid, else dont keep the data
137 :     areaid = -1;
138 :     for (i = 0; i < areaindex.Size(); i++)
139 :     if (strcasecmp(areaindex[i], tmparea) == 0)
140 :     areaid = i;
141 :    
142 :     if (areaid == -1)
143 :     keepdata = 0;
144 :    
145 :     //check if the year and step are in the simulation
146 :     timeid = -1;
147 :     tagid = -1;
148 :     if ((TimeInfo->isWithinPeriod(year, step)) && (keepdata == 1)) {
149 :     for (i = 0; i < tagvec.Size(); i++)
150 :     if (strcasecmp(tagvec[i]->getName(), tmptag) == 0)
151 :     tagid = i;
152 :    
153 :     if (tagid == -1) {
154 :     tmpindex = -1;
155 :     for (i = 0; i < Tag.Size(); i++)
156 :     if (strcasecmp(Tag[i]->getName(), tmptag) == 0)
157 :     tmpindex = i;
158 :    
159 :     if (tmpindex == -1) {
160 :     keepdata = 0;
161 :     } else {
162 :     tagName = new char[strlen(tmptag) + 1];
163 :     strcpy(tagName, tmptag);
164 :     tagnames.resize(tagName);
165 :     tagid = tagnames.Size() - 1;
166 :     Years.AddRows(1, 1, year);
167 :     Steps.AddRows(1, 1, step);
168 :     timeid = 0;
169 :     numbers.resize(new DoubleMatrix(1, numarea, 0.0));
170 :     obsMean.resize(new DoubleMatrix(1, numarea, 0.0));
171 :     modelMean.resize(new DoubleMatrix(1, numarea, 0.0));
172 :     if (needvar)
173 :     modelStdDev.resize(new DoubleMatrix(1, numarea, 0.0));
174 :     if (readvar)
175 :     obsStdDev.resize(new DoubleMatrix(1, numarea, 0.0));
176 :     }
177 :    
178 :     } else {
179 :     for (i = 0; i < Years.Ncol(tagid); i++)
180 :     if ((Years[tagid][i] == year) && (Steps[tagid][i] == step))
181 :     timeid = i;
182 :    
183 :     //if this is a new timestep, resize to store the data
184 :     if (timeid == -1) {
185 :     Years[tagid].resize(1, year);
186 :     Steps[tagid].resize(1, step);
187 :     (*numbers[tagid]).AddRows(1, numarea, 0.0);
188 :     (*obsMean[tagid]).AddRows(1, numarea, 0.0);
189 :     if (readvar)
190 :     (*obsStdDev[tagid]).AddRows(1, numarea, 0.0);
191 :     timeid = Years.Ncol(tagid) - 1;
192 :     }
193 :     }
194 :    
195 :     } else
196 :     keepdata = 0;
197 :    
198 :     if (keepdata == 1) {
199 :     //statistics data is required, so store it
200 :     count++;
201 :     (*numbers[tagid])[timeid][areaid] = tmpnumber;
202 :     (*obsMean[tagid])[timeid][areaid] = tmpmean;
203 :     if (readvar)
204 :     (*obsStdDev[tagid])[timeid][areaid] = tmpstddev;
205 :     } else
206 :     reject++; //count number of rejected data points read from file
207 :     }
208 :    
209 :     timeindex.resize(tagvec.Size(), -1);
210 :     if (count == 0)
211 :     handle.logMessage(LOGWARN, "Warning in recstatistics - found no data in the data file for", this->getName());
212 :     if (reject != 0)
213 :     handle.logMessage(LOGMESSAGE, "Discarded invalid recstatistics data - number of invalid entries", reject);
214 :     handle.logMessage(LOGMESSAGE, "Read recstatistics data file - number of entries", count);
215 :     }
216 :    
217 :     RecStatistics::~RecStatistics() {
218 :     int i;
219 :     for (i = 0; i < fleetnames.Size(); i++)
220 :     delete[] fleetnames[i];
221 :     for (i = 0; i < tagnames.Size(); i++)
222 :     delete[] tagnames[i];
223 :     for (i = 0; i < areaindex.Size(); i++)
224 :     delete[] areaindex[i];
225 :     for (i = 0; i < numbers.Size(); i++) {
226 :     delete numbers[i];
227 :     delete obsMean[i];
228 :     delete modelMean[i];
229 :     }
230 :     for (i = 0; i < obsStdDev.Size(); i++)
231 :     delete obsStdDev[i];
232 :     for (i = 0; i < modelStdDev.Size(); i++)
233 :     delete modelStdDev[i];
234 :     delete[] functionname;
235 :     if (aggregator != 0) {
236 :     for (i = 0; i < tagvec.Size(); i++)
237 :     delete aggregator[i];
238 :     delete[] aggregator;
239 :     aggregator = 0;
240 :     }
241 :     delete LgrpDiv;
242 :     }
243 :    
244 :     void RecStatistics::Reset(const Keeper* const keeper) {
245 :     int i;
246 :     Likelihood::Reset(keeper);
247 :     for (i = 0; i < timeindex.Size(); i++)
248 :     timeindex[i] = -1;
249 :     for (i = 0; i < modelMean.Size(); i++)
250 :     (*modelMean[i]).setToZero();
251 :     if (handle.getLogLevel() >= LOGMESSAGE)
252 :     handle.logMessage(LOGMESSAGE, "Reset recstatistics component", this->getName());
253 :     }
254 :    
255 :     void RecStatistics::Print(ofstream& outfile) const {
256 :     int i;
257 :    
258 :     outfile << "\nRecapture Statistics " << this->getName() << " - likelihood value " << likelihood
259 :     << "\n\tFunction " << functionname;
260 :     outfile << "\n\tFleet names:";
261 :     for (i = 0; i < fleetnames.Size(); i++)
262 :     outfile << sep << fleetnames[i];
263 :     outfile << endl;
264 :     for (i = 0; i < tagnames.Size(); i++) {
265 :     outfile << "\tTagging experiment:\t" << tagnames[i] << endl;
266 :     aggregator[i]->Print(outfile);
267 :     outfile << endl;
268 :     }
269 :     outfile.flush();
270 :     }
271 :    
272 :     void RecStatistics::printSummary(ofstream& outfile) {
273 :     //JMB to start with just print a summary of all likelihood scores
274 :     //This needs to be changed to print something for each timestep and area
275 :     //but we need to sort out how to get the information in the same format as other components
276 :     if (!(isZero(likelihood))) {
277 :     outfile << "all all all" << sep << setw(largewidth) << this->getName() << sep
278 :     << setprecision(smallprecision) << setw(smallwidth) << weight << sep
279 :     << setprecision(largeprecision) << setw(largewidth) << likelihood << endl;
280 :     outfile.flush();
281 :     }
282 :     }
283 :    
284 :     void RecStatistics::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
285 :     int i, j, t, found, minage, maxage;
286 :     FleetPtrVector fleets;
287 :     StockPtrVector stocks;
288 :     CharPtrVector stocknames;
289 :    
290 :     for (i = 0; i < fleetnames.Size(); i++) {
291 :     found = 0;
292 :     for (j = 0; j < Fleets.Size(); j++)
293 :     if (strcasecmp(fleetnames[i], Fleets[j]->getName()) == 0) {
294 :     found++;
295 :     fleets.resize(Fleets[j]);
296 :     }
297 :    
298 :     if (found == 0)
299 :     handle.logMessage(LOGFAIL, "Error in recstatistics - unrecognised fleet", fleetnames[i]);
300 :     }
301 :    
302 :     for (i = 0; i < fleets.Size(); i++)
303 :     for (j = 0; j < fleets.Size(); j++)
304 :     if ((strcasecmp(fleets[i]->getName(), fleets[j]->getName()) == 0) && (i != j))
305 :     handle.logMessage(LOGFAIL, "Error in recstatistics - repeated fleet", fleets[i]->getName());
306 :    
307 :     aggregator = new RecAggregator*[tagvec.Size()];
308 :     for (t = 0; t < tagvec.Size(); t++) {
309 :     stocks.Reset();
310 :     stocknames = tagvec[t]->getStockNames();
311 :     for (i = 0; i < stocknames.Size(); i++) {
312 :     found = 0;
313 :     for (j = 0; j < Stocks.Size(); j++)
314 :     if (Stocks[j]->isEaten())
315 :     if (strcasecmp(stocknames[i], Stocks[j]->getName()) == 0) {
316 :     found++;
317 :     stocks.resize(Stocks[j]);
318 :     }
319 :    
320 :     if (found == 0)
321 :     handle.logMessage(LOGFAIL, "Error in recstatistics - unrecognised stock", stocknames[i]);
322 :     }
323 :    
324 :     //Check the stock has been tagged
325 :     for (j = 0; j < stocks.Size(); j++)
326 :     if (!stocks[j]->isTagged())
327 :     handle.logMessage(LOGFAIL, "Error in recstatistics - stocks hasnt been tagged", stocks[j]->getName());
328 :    
329 :     LgrpDiv = new LengthGroupDivision(*(stocks[0]->getPrey()->getLengthGroupDiv()));
330 :     for (i = 1; i < stocks.Size(); i++)
331 :     if (!LgrpDiv->Combine(stocks[i]->getPrey()->getLengthGroupDiv()))
332 :     handle.logMessage(LOGFAIL, "Error in recstatistics - length groups not compatible");
333 :    
334 :     if (LgrpDiv->Error())
335 :     handle.logMessage(LOGFAIL, "Error in recstatistics - failed to create length group");
336 :    
337 :     minage = 999;
338 :     maxage = 0;
339 :     for (i = 0; i < stocks.Size(); i++) {
340 :     minage = min(stocks[i]->minAge(), minage);
341 :     maxage = max(stocks[i]->maxAge(), maxage);
342 :     }
343 :    
344 :     IntMatrix ages(1, 0, 0);
345 :     for (i = 0; i <= maxage - minage; i++)
346 :     ages[0].resize(1, minage + i);
347 :     aggregator[t] = new RecAggregator(fleets, stocks, LgrpDiv, areas, ages, tagvec[t]);
348 :     }
349 :     }
350 :    
351 :     void RecStatistics::addLikelihood(const TimeClass* const TimeInfo) {
352 :     int t, i, check;
353 :     double l = 0.0;
354 :    
355 :     check = 0;
356 :     for (t = 0; t < tagvec.Size(); t++) {
357 :     timeindex[t] = -1;
358 :     if (tagvec[t]->isWithinPeriod(TimeInfo->getYear(), TimeInfo->getStep())) {
359 :     for (i = 0; i < Years.Ncol(t); i++) {
360 :     if (Years[t][i] == TimeInfo->getYear() && Steps[t][i] == TimeInfo->getStep()) {
361 :     if (check == 0)
362 :     if (handle.getLogLevel() >= LOGMESSAGE)
363 :     handle.logMessage(LOGMESSAGE, "Calculating likelihood score for recstatistics component", this->getName());
364 :     timeindex[t] = i;
365 :     aggregator[t]->Sum();
366 :     check++;
367 :     }
368 :     }
369 :     }
370 :     }
371 :    
372 :     if (check > 0) {
373 :     l = calcLikSumSquares();
374 :     likelihood += l;
375 :     if (handle.getLogLevel() >= LOGMESSAGE)
376 :     handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
377 :     }
378 :     }
379 :    
380 :     double RecStatistics::calcLikSumSquares() {
381 :     double lik = 0.0;
382 :     int t, area;
383 :     double simvar, simdiff;
384 :    
385 :     for (t = 0; t < tagvec.Size(); t++) {
386 :     if (timeindex[t] > -1) {
387 :     alptr = &aggregator[t]->getSum();
388 :     for (area = 0; area < alptr->Size(); area++) {
389 :     simvar = 0.0;
390 :     ps.calcStatistics((*alptr)[area][0], aggregator[t]->getLengthGroupDiv(), 0);
391 :     (*modelMean[t])[timeindex[t]][area] = ps.meanLength();
392 :    
393 :     switch (functionnumber) {
394 :     case 1:
395 :     (*modelStdDev[t])[timeindex[t]][area] = ps.sdevLength();
396 :     simvar = (*modelStdDev[t])[timeindex[t]][area] * (*modelStdDev[t])[timeindex[t]][area];
397 :     break;
398 :     case 2:
399 :     simvar = (*obsStdDev[t])[timeindex[t]][area] * (*obsStdDev[t])[timeindex[t]][area];
400 :     break;
401 :     case 3:
402 :     simvar = 1.0;
403 :     break;
404 :     default:
405 :     handle.logMessage(LOGWARN, "Warning in recstatistics - unrecognised function", functionname);
406 :     break;
407 :     }
408 :    
409 :     if (!(isZero(simvar))) {
410 :     simdiff = (*modelMean[t])[timeindex[t]][area] - (*obsMean[t])[timeindex[t]][area];
411 :     lik += simdiff * simdiff * (*numbers[t])[timeindex[t]][area] / simvar;
412 :     }
413 :     }
414 :     }
415 :     }
416 :     return lik;
417 :     }
418 :    
419 :     void RecStatistics::printLikelihood(ofstream& outfile, const TimeClass* const TimeInfo) {
420 :     int year = TimeInfo->getYear();
421 :     int step = TimeInfo->getStep();
422 :     int t, ti, timeid, area;
423 :    
424 :     for (t = 0; t < tagvec.Size(); t++) {
425 :     if (tagvec[t]->isWithinPeriod(year, step)) {
426 :     timeid = -1;
427 :     for (ti = 0; ti < Years.Ncol(t); ti++)
428 :     if (Years[t][ti] == year && Steps[t][ti] == step)
429 :     timeid = ti;
430 :    
431 :     if (timeid > -1) {
432 :     for (area = 0; area < areaindex.Size(); area++) {
433 :     outfile << setw(printwidth) << tagnames[t] << sep << setw(lowwidth)
434 :     << year << sep << setw(lowwidth) << step << sep << setw(printwidth)
435 :     << areaindex[area] << sep << setprecision(printprecision)
436 :     << setw(printwidth) << (*numbers[t])[timeid][area] << sep
437 :     << setprecision(largeprecision) << setw(largewidth);
438 :    
439 :     //JMB crude filter to remove the 'silly' values from the output
440 :     if ((*modelMean[t])[timeid][area] < rathersmall)
441 :     outfile << 0;
442 :     else
443 :     outfile << (*modelMean[t])[timeid][area];
444 :    
445 :     switch (functionnumber) {
446 :     case 1:
447 :     outfile << sep << setprecision(printprecision) << setw(printwidth)
448 :     << (*modelStdDev[t])[timeid][area] << endl;
449 :     break;
450 :     case 2:
451 :     outfile << sep << setprecision(printprecision) << setw(printwidth)
452 :     << (*obsStdDev[t])[timeid][area] << endl;
453 :     break;
454 :     case 3:
455 :     outfile << endl;
456 :     break;
457 :     default:
458 :     handle.logMessage(LOGWARN, "Warning in recstatistics - unrecognised function", functionname);
459 :     break;
460 :     }
461 :     }
462 :     }
463 :     }
464 :     }
465 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge