Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.kappa: Fix failures, garbage output, fallback to category values #2573

Merged
merged 7 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions raster/r.kappa/calc_kappa.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
void calc_kappa(void)
{
int i, j;
int a_i, b_i;
int s, l;
int nl;
size_t ns;
Expand Down Expand Up @@ -37,10 +38,10 @@ void calc_kappa(void)
total = count_sum(&s, l);

/* calculate the parameters of the kappa-calculation */
pi = (double *)G_calloc(ns, sizeof(double));
pj = (double *)G_calloc(ns, sizeof(double));
pii = (double *)G_calloc(ns, sizeof(double));
kpp = (double *)G_calloc(ns, sizeof(double));
pi = (double *)G_calloc(ncat, sizeof(double));
pj = (double *)G_calloc(ncat, sizeof(double));
pii = (double *)G_calloc(ncat, sizeof(double));
kpp = (double *)G_calloc(ncat, sizeof(double));

for (i = 0; i < ncat; i++) {
for (j = 0; j < ns; j++) {
Expand All @@ -65,31 +66,46 @@ void calc_kappa(void)
pC += pi[i] * pj[i];
}

for (i = 0; i < ncat; i++)
if ((pi[i] == 0) || (pj[i] == 0))
for (i = 0; i < ncat; i++) {
if (pi[i] == 0)
kpp[i] = -999;
else
kpp[i] = (pii[i] - pi[i] * pj[i]) / (pi[i] - pi[i] * pj[i]);
}

/* print out the comission and omission accuracy, and conditional kappa */
fprintf(fd, "\nCats\t%% Comission\t%% Omission\tEstimated Kappa\n");
for (i = 0; i < ncat; i++)
if ((kpp[i] == -999) && (i != 0))
fprintf(fd, "%ld\tNA\t\tNA\t\tNA\n", rlst[i]);
for (i = 0; i < ncat; i++) {
fprintf(fd, "%ld\t", rlst[i]);
if (pi[i] == 0)
fprintf(fd, "NA\t\t");
else
fprintf(fd, "%f\t", 100 * (1 - pii[i] / pi[i]));
if (pj[i] == 0)
fprintf(fd, "NA\t\t");
else
fprintf(fd, "%ld\t%f\t%f\t%f\n",
rlst[i], 100 * (1 - pii[i] / pi[i]),
100 * (1 - pii[i] / pj[i]), kpp[i]);
fprintf(fd, "%f\t", 100 * (1 - pii[i] / pj[i]));
if (kpp[i] == -999)
fprintf(fd, "NA\n");
else
fprintf(fd, "%f\n", kpp[i]);
}
fprintf(fd, "\n");

for (i = 0; i < ncat; i++) {
inter1 += pii[i] * pow(((1 - pC) - (1 - p0) * (pi[i] + pj[i])), 2.);
}

for (j = 0; j < ns; j++) {
if (Gstats[j].cats[0] != Gstats[j].cats[1])
inter2 += Gstats[j].count *
pow((pi[Gstats[j].cats[0] - 1] + pj[Gstats[j].cats[1] - 1]),
2.) / total;
if (Gstats[j].cats[0] != Gstats[j].cats[1]) {
for (i = 0; i < ncat; i++) {
if (Gstats[j].cats[0] == rlst[i])
a_i = i;
if (Gstats[j].cats[1] == rlst[i])
b_i = i;
}
inter2 += Gstats[j].count * pow((pi[a_i] + pj[b_i]), 2.) / total;
}
}
kp = (p0 - pC) / (1 - pC);
vkp = (inter1 + pow((1 - p0), 2.) * inter2 -
Expand Down
10 changes: 6 additions & 4 deletions raster/r.kappa/prt2csv_mat.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ void prn2csv_error_mat(int out_cols, int hdr)
if (cl)
G_strip(cl);
if (cl == NULL || *cl == 0)
cl = "NULL";
fprintf(fd, "%s\t", cl);
fprintf(fd, "%ld\t", cats[j]);
else
fprintf(fd, "%s\t", cl);
}
/*for (cndx = first_col; cndx < last_col; cndx++) */
/* fprintf(fd, "%ld\t", rlst[cndx]); */
Expand All @@ -112,8 +113,9 @@ void prn2csv_error_mat(int out_cols, int hdr)
if (cl)
G_strip(cl);
if (cl == NULL || *cl == 0)
cl = "NULL";
fprintf(fd, "%s\t", cl);
fprintf(fd, "%ld\t", cats[rndx]);
else
fprintf(fd, "%s\t", cl);
/* entries */
for (cndx = first_col; cndx < last_col; cndx++) {
thisone = (ncat * rndx) + cndx;
Expand Down
14 changes: 14 additions & 0 deletions raster/r.kappa/testsuite/data/class_1.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
north: 5
south: 0
east: 5
west: 0
rows: 5
cols: 5
null: n
type: int

1 n 3 3 4
5 3 1 4 n
n 1 3 6 6
6 n 6 1 3
4 4 4 4 3
14 changes: 14 additions & 0 deletions raster/r.kappa/testsuite/data/class_2.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
north: 5
south: 0
east: 5
west: 0
rows: 5
cols: 5
null: n
type: int

9 9 9 9 9
9 9 9 9 9
9 9 9 9 9
9 9 9 9 9
9 9 9 9 9
14 changes: 14 additions & 0 deletions raster/r.kappa/testsuite/data/ref_1.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
north: 5
south: 0
east: 5
west: 0
rows: 5
cols: 5
null: n
type: int

1 3 n 3 6
5 3 1 n n
n 1 2 2 2
6 n 6 1 3
1 1 1 n 3
14 changes: 14 additions & 0 deletions raster/r.kappa/testsuite/data/ref_2.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
north: 5
south: 0
east: 5
west: 0
rows: 5
cols: 5
null: n
type: int

0 1 2 1 0
1 2 3 2 1
2 3 4 3 2
1 2 3 2 1
0 1 2 1 0
Loading