Debian Codegraph @

src/algorithms/ImgRegression.cc

raw symbols SCIP
1 /**********************************************************************
2 ImgRegression.cc: class to calculate regression between two raster datasets
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #include "ImgRegression.h"
22 #include <iostream>
23
24 using namespace imgregression;
25
26 ImgRegression::ImgRegression(void)
27 : m_threshold(0), m_down(1)
28 {}
29
30 ImgRegression::~ImgRegression(void)
31 {}
32
33 double ImgRegression::getRMSE(Jim& imgReader1, Jim& imgReader2, double& c0, double& c1, unsigned int band1, unsigned int band2, short verbose) const{
34 c0=0;
35 c1=1;
36 unsigned int icol1=0,irow1=0;
37 std::vector<double> rowBuffer1(imgReader1.nrOfCol());
38 std::vector<double> rowBuffer2(imgReader2.nrOfCol());
39 std::vector<double> buffer1;
40 std::vector<double> buffer2;
41
42 srand(time(NULL));
43 for(irow1=0;irow1<imgReader1.nrOfRow();++irow1){
44 if(irow1%m_down)
45 continue;
46 icol1=0;
47 double icol2=0,irow2=0;
48 double geox=0,geoy=0;
49 imgReader1.readData(rowBuffer1,irow1,band1);
50 imgReader1.image2geo(icol1,irow1,geox,geoy);
51 imgReader2.geo2image(geox,geoy,icol2,irow2);
52 icol2=static_cast<int>(icol2);
53 irow2=static_cast<int>(irow2);
54 if(irow2<0||irow2>=imgReader2.nrOfRow())
55 continue;
56 imgReader2.readData(rowBuffer2,irow2,band2);
57 for(icol1=0;icol1<imgReader1.nrOfCol();++icol1){
58 if(icol1%m_down)
59 continue;
60 if(m_threshold>0){//percentual value
61 double p=static_cast<double>(rand())/(RAND_MAX);
62 p*=100.0;
63 if(p>m_threshold)
64 continue;//do not select for now, go to next column
65 }
66 imgReader1.image2geo(icol1,irow1,geox,geoy);
67 imgReader2.geo2image(geox,geoy,icol2,irow2);
68 if(icol2<0||icol2>=imgReader2.nrOfCol())
69 continue;
70 icol2=static_cast<int>(icol2);
71 irow2=static_cast<int>(irow2);
72 //check for nodata
73 double value1=rowBuffer1[icol1];
74 double value2=rowBuffer2[icol2];
75 if(imgReader1.isNoData(value1)||imgReader2.isNoData(value2))
76 continue;
77
78 buffer1.push_back(value1);
79 buffer2.push_back(value2);
80 if(verbose>1)
81 std::cout << geox << " " << geoy << " " << icol1 << " " << irow1 << " " << icol2 << " " << irow2 << " " << buffer1.back() << " " << buffer2.back() << std::endl;
82 }
83 }
84 double err=0;
85 if(buffer1.size()&&buffer2.size()){
86 statfactory::StatFactory stat;
87 err=stat.linear_regression_err(buffer1,buffer2,c0,c1);
88 }
89 if(verbose)
90 std::cout << "linear regression based on " << buffer1.size() << " points: " << c0 << "+" << c1 << " * x " << " with rmse: " << err << std::endl;
91 return err;
92 }
93
94 double ImgRegression::getR2(Jim& imgReader1, Jim& imgReader2, double& c0, double& c1, unsigned int band1, unsigned int band2, short verbose) const{
95 c0=0;
96 c1=1;
97 int icol1=0,irow1=0;
98 std::vector<double> rowBuffer1(imgReader1.nrOfCol());
99 std::vector<double> rowBuffer2(imgReader2.nrOfCol());
100 std::vector<double> buffer1;
101 std::vector<double> buffer2;
102
103 srand(time(NULL));
104 for(irow1=0;irow1<imgReader1.nrOfRow();++irow1){
105 if(irow1%m_down)
106 continue;
107 icol1=0;
108 double icol2=0,irow2=0;
109 double geox=0,geoy=0;
110 imgReader1.readData(rowBuffer1,irow1,band1);
111 imgReader1.image2geo(icol1,irow1,geox,geoy);
112 imgReader2.geo2image(geox,geoy,icol2,irow2);
113 icol2=static_cast<int>(icol2);
114 irow2=static_cast<int>(irow2);
115 if(irow2<0||irow2>=imgReader2.nrOfRow())
116 continue;
117 imgReader2.readData(rowBuffer2,irow2,band2);
118 for(icol1=0;icol1<imgReader1.nrOfCol();++icol1){
119 if(icol1%m_down)
120 continue;
121 if(m_threshold>0){//percentual value
122 double p=static_cast<double>(rand())/(RAND_MAX);
123 p*=100.0;
124 if(p>m_threshold)
125 continue;//do not select for now, go to next column
126 }
127 imgReader1.image2geo(icol1,irow1,geox,geoy);
128 imgReader2.geo2image(geox,geoy,icol2,irow2);
129 if(icol2<0||icol2>=imgReader2.nrOfCol())
130 continue;
131 icol2=static_cast<int>(icol2);
132 irow2=static_cast<int>(irow2);
133 //check for nodata
134 double value1=rowBuffer1[icol1];
135 double value2=rowBuffer2[icol2];
136 if(imgReader1.isNoData(value1)||imgReader2.isNoData(value2))
137 continue;
138
139 buffer1.push_back(value1);
140 buffer2.push_back(value2);
141 if(verbose>1)
142 std::cout << geox << " " << geoy << " " << icol1 << " " << irow1 << " " << icol2 << " " << irow2 << " " << buffer1.back() << " " << buffer2.back() << std::endl;
143 }
144 }
145 double r2=0;
146 if(buffer1.size()&&buffer2.size()){
147 statfactory::StatFactory stat;
148 r2=stat.linear_regression(buffer1,buffer2,c0,c1);
149 }
150 if(verbose)
151 std::cout << "linear regression based on " << buffer1.size() << " points: " << c0 << "+" << c1 << " * x " << " with r^2: " << r2 << std::endl;
152 return r2;
153 }
154
155 double ImgRegression::pgetR2(Jim& imgReader1, Jim& imgReader2, double& c0, double& c1, unsigned int band1, unsigned int band2, short verbose) const{
156 c0=0;
157 c1=1;
158 int icol1=0,irow1=0;
159 std::vector<double> rowBuffer1(imgReader1.nrOfCol());
160 std::vector<double> rowBuffer2(imgReader2.nrOfCol());
161 std::vector<double> buffer1;
162 std::vector<double> buffer2;
163
164 srand(time(NULL));
165 for(irow1=0;irow1<imgReader1.nrOfRow();++irow1){
166 if(irow1%m_down)
167 continue;
168 icol1=0;
169 double icol2=0,irow2=0;
170 double geox=0,geoy=0;
171 imgReader1.readData(rowBuffer1,irow1,band1);
172 imgReader1.image2geo(icol1,irow1,geox,geoy);
173 imgReader2.geo2image(geox,geoy,icol2,irow2);
174 icol2=static_cast<int>(icol2);
175 irow2=static_cast<int>(irow2);
176 if(irow2<0||irow2>=imgReader2.nrOfRow())
177 continue;
178 imgReader2.readData(rowBuffer2,irow2,band2);
179 for(icol1=0;icol1<imgReader1.nrOfCol();++icol1){
180 if(icol1%m_down)
181 continue;
182 if(m_threshold>0){//percentual value
183 double p=static_cast<double>(rand())/(RAND_MAX);
184 p*=100.0;
185 if(p>m_threshold)
186 continue;//do not select for now, go to next column
187 }
188 imgReader1.image2geo(icol1,irow1,geox,geoy);
189 imgReader2.geo2image(geox,geoy,icol2,irow2);
190 if(icol2<0||icol2>=imgReader2.nrOfCol())
191 continue;
192 icol2=static_cast<int>(icol2);
193 irow2=static_cast<int>(irow2);
194 //check for nodata
195 double value1=rowBuffer1[icol1];
196 double value2=rowBuffer2[icol2];
197 if(imgReader1.isNoData(value1)||imgReader2.isNoData(value2))
198 continue;
199
200 buffer1.push_back(value1);
201 buffer2.push_back(value2);
202 if(verbose>1)
203 std::cout << geox << " " << geoy << " " << icol1 << " " << irow1 << " " << icol2 << " " << irow2 << " " << buffer1.back() << " " << buffer2.back() << std::endl;
204 }
205 }
206 double r=0;
207 if(buffer1.size()&&buffer2.size()){
208 statfactory::StatFactory stat;
209 r=stat.correlation(buffer1,buffer2);
210 // r=stat.gsl_correlation(buffer1,buffer2);
211 double m1=0;
212 double v1=0;
213 double m2=0;
214 double v2=0;
215 stat.meanVar(buffer1,m1,v1);
216 stat.meanVar(buffer2,m2,v2);
217 if(v1>0){
218 if(r>=0)
219 c1=v2/v1;
220 else
221 c1=-v2/v1;
222 }
223 c0=m2-c1*m1;
224 }
225 if(verbose)
226 std::cout << "orthogonal regression based on " << buffer1.size() << " points: " << c0 << "+" << c1 << " * x " << " with r^2: " << r*r << std::endl;
227 return r*r;
228 }
229
230 double ImgRegression::getRMSE(Jim& imgReader, unsigned int band1, unsigned int band2, double& c0, double& c1, short verbose) const{
231 c0=0;
232 c1=1;
233 int icol=0,irow=0;
234 std::vector<double> rowBuffer1(imgReader.nrOfCol());
235 std::vector<double> rowBuffer2(imgReader.nrOfCol());
236 std::vector<double> buffer1;
237 std::vector<double> buffer2;
238
239 srand(time(NULL));
240 assert(band1>=0);
241 assert(band1<imgReader.nrOfBand());
242 assert(band2>=0);
243 assert(band2<imgReader.nrOfBand());
244 for(irow=0;irow<imgReader.nrOfRow();++irow){
245 if(irow%m_down)
246 continue;
247 icol=0;
248 imgReader.readData(rowBuffer1,irow,band1);
249 imgReader.readData(rowBuffer2,irow,band2);
250 for(icol=0;icol<imgReader.nrOfCol();++icol){
251 if(icol%m_down)
252 continue;
253 if(m_threshold>0){//percentual value
254 double p=static_cast<double>(rand())/(RAND_MAX);
255 p*=100.0;
256 if(p>m_threshold)
257 continue;//do not select for now, go to next column
258 }
259 //check for nodata
260 double value1=rowBuffer1[icol];
261 double value2=rowBuffer2[icol];
262 if(imgReader.isNoData(value1)||imgReader.isNoData(value2))
263 continue;
264
265 buffer1.push_back(value1);
266 buffer2.push_back(value2);
267 if(verbose>1)
268 std::cout << icol << " " << irow << " " << buffer1.back() << " " << buffer2.back() << std::endl;
269 }
270 }
271 double err=0;
272 if(buffer1.size()&&buffer2.size()){
273 statfactory::StatFactory stat;
274 err=stat.linear_regression_err(buffer1,buffer2,c0,c1);
275 }
276 if(verbose)
277 std::cout << "linear regression based on " << buffer1.size() << " points: " << c0 << "+" << c1 << " * x " << " with rmse: " << err << std::endl;
278 return err;
279 }
280
281 double ImgRegression::getR2(Jim& imgReader, unsigned int band1, unsigned int band2, double& c0, double& c1, short verbose) const{
282 c0=0;
283 c1=1;
284 int icol=0,irow=0;
285 std::vector<double> rowBuffer1(imgReader.nrOfCol());
286 std::vector<double> rowBuffer2(imgReader.nrOfCol());
287 std::vector<double> buffer1;
288 std::vector<double> buffer2;
289
290 srand(time(NULL));
291 assert(band1>=0);
292 assert(band1<imgReader.nrOfBand());
293 assert(band2>=0);
294 assert(band2<imgReader.nrOfBand());
295 for(irow=0;irow<imgReader.nrOfRow();++irow){
296 if(irow%m_down)
297 continue;
298 icol=0;
299 imgReader.readData(rowBuffer1,irow,band1);
300 imgReader.readData(rowBuffer2,irow,band2);
301 for(icol=0;icol<imgReader.nrOfCol();++icol){
302 if(icol%m_down)
303 continue;
304 if(m_threshold>0){//percentual value
305 double p=static_cast<double>(rand())/(RAND_MAX);
306 p*=100.0;
307 if(p>m_threshold)
308 continue;//do not select for now, go to next column
309 }
310 //check for nodata
311 double value1=rowBuffer1[icol];
312 double value2=rowBuffer2[icol];
313 if(imgReader.isNoData(value1)||imgReader.isNoData(value2))
314 continue;
315
316 buffer1.push_back(value1);
317 buffer2.push_back(value2);
318 if(verbose>1)
319 std::cout << icol << " " << irow << " " << buffer1.back() << " " << buffer2.back() << std::endl;
320 }
321 }
322 double r2=0;
323 if(buffer1.size()&&buffer2.size()){
324 statfactory::StatFactory stat;
325 r2=stat.linear_regression(buffer1,buffer2,c0,c1);
326 }
327 if(verbose)
328 std::cout << "linear regression based on " << buffer1.size() << " points: " << c0 << "+" << c1 << " * x " << " with r^2: " << r2 << std::endl;
329 return r2;
330 }
331
332 double ImgRegression::pgetR2(Jim& imgReader, unsigned int band1, unsigned int band2, double& c0, double& c1, short verbose) const{
333 c0=0;
334 c1=1;
335 int icol=0,irow=0;
336 std::vector<double> rowBuffer1(imgReader.nrOfCol());
337 std::vector<double> rowBuffer2(imgReader.nrOfCol());
338 std::vector<double> buffer1;
339 std::vector<double> buffer2;
340
341 srand(time(NULL));
342 assert(band1>=0);
343 assert(band1<imgReader.nrOfBand());
344 assert(band2>=0);
345 assert(band2<imgReader.nrOfBand());
346 for(irow=0;irow<imgReader.nrOfRow();++irow){
347 if(irow%m_down)
348 continue;
349 icol=0;
350 imgReader.readData(rowBuffer1,irow,band1);
351 imgReader.readData(rowBuffer2,irow,band2);
352 for(icol=0;icol<imgReader.nrOfCol();++icol){
353 if(icol%m_down)
354 continue;
355 if(m_threshold>0){//percentual value
356 double p=static_cast<double>(rand())/(RAND_MAX);
357 p*=100.0;
358 if(p>m_threshold)
359 continue;//do not select for now, go to next column
360 }
361 //check for nodata
362 double value1=rowBuffer1[icol];
363 double value2=rowBuffer2[icol];
364 if(imgReader.isNoData(value1)||imgReader.isNoData(value2))
365 continue;
366
367 buffer1.push_back(value1);
368 buffer2.push_back(value2);
369 if(verbose>1)
370 std::cout << icol << " " << irow << " " << buffer1.back() << " " << buffer2.back() << std::endl;
371 }
372 }
373 double r=0;
374 if(buffer1.size()&&buffer2.size()){
375 statfactory::StatFactory stat;
376 r=stat.correlation(buffer1,buffer2);
377 // r=stat.gsl_correlation(buffer1,buffer2);
378 double m1=0;
379 double v1=0;
380 double m2=0;
381 double v2=0;
382 stat.meanVar(buffer1,m1,v1);
383 stat.meanVar(buffer2,m2,v2);
384 if(v1>0){
385 if(r>=0)
386 c1=v2/v1;
387 else
388 c1=-v2/v1;
389 }
390 c0=m2-c1*m1;
391 }
392 if(verbose)
393 std::cout << "orthogonal regression based on " << buffer1.size() << " points: " << c0 << "+" << c1 << " * x " << " with r^2: " << r*r << std::endl;
394 return r*r;
395 }