diff --git a/raster/r.kappa/calc_kappa.c b/raster/r.kappa/calc_kappa.c index 38b974819c0..431a718954b 100644 --- a/raster/r.kappa/calc_kappa.c +++ b/raster/r.kappa/calc_kappa.c @@ -27,11 +27,11 @@ void calc_kappa(void) p0 = pC = 0; if (output == NULL) - fd = stdout; + fd = stdout; else if ((fd = fopen(output, "a")) == NULL) { - G_fatal_error(_("Cannot open file <%s> to write kappa and relevant parameters"), - output); - return; + G_fatal_error(_("Cannot open file <%s> to write kappa and relevant parameters"), + output); + return; } total = count_sum(&s, l); @@ -43,64 +43,64 @@ void calc_kappa(void) kpp = (double *)G_calloc(ns, sizeof(double)); for (i = 0; i < ncat; i++) { - for (j = 0; j < ns; j++) { - if (Gstats[j].cats[0] == rlst[i]) - pi[i] += Gstats[j].count; + for (j = 0; j < ns; j++) { + if (Gstats[j].cats[0] == rlst[i]) + pi[i] += Gstats[j].count; - if (Gstats[j].cats[1] == rlst[i]) - pj[i] += Gstats[j].count; + if (Gstats[j].cats[1] == rlst[i]) + pj[i] += Gstats[j].count; - if ((Gstats[j].cats[0] == Gstats[j].cats[1]) && - (Gstats[j].cats[0] == rlst[i])) - pii[i] += Gstats[j].count; - } - obs += pii[i]; + if ((Gstats[j].cats[0] == Gstats[j].cats[1]) && + (Gstats[j].cats[0] == rlst[i])) + pii[i] += Gstats[j].count; + } + obs += pii[i]; } for (i = 0; i < ncat; i++) { - pi[i] = pi[i] / total; - pj[i] = pj[i] / total; - pii[i] = pii[i] / total; - p0 += pii[i]; - pC += pi[i] * pj[i]; + pi[i] = pi[i] / total; + pj[i] = pj[i] / total; + pii[i] = pii[i] / total; + p0 += pii[i]; + pC += pi[i] * pj[i]; } for (i = 0; i < ncat; i++) - if ((pi[i] == 0) || (pj[i] == 0)) - kpp[i] = -999; - else - kpp[i] = (pii[i] - pi[i] * pj[i]) / (pi[i] - pi[i] * pj[i]); + if ((pi[i] == 0) || (pj[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]); - 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]); + if ((kpp[i] == -999) && (i != 0)) + fprintf(fd, "%ld\tNA\t\tNA\t\tNA\n", rlst[i]); + 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, "\n"); for (i = 0; i < ncat; i++) { - inter1 += pii[i] * pow(((1 - pC) - (1 - p0) * (pi[i] + pj[i])), 2.); + 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]) + inter2 += Gstats[j].count * + pow((pi[Gstats[j].cats[0] - 1] + pj[Gstats[j].cats[1] - 1]), + 2.) / total; } kp = (p0 - pC) / (1 - pC); vkp = (inter1 + pow((1 - p0), 2.) * inter2 - - pow((p0 * pC - 2 * pC + p0), 2.)) / pow((1 - pC), 4.) / total; + pow((p0 * pC - 2 * pC + p0), 2.)) / pow((1 - pC), 4.) / total; fprintf(fd, "Kappa\t\tKappa Variance\n"); fprintf(fd, "%f\t%f\n", kp, vkp); fprintf(fd, "\nObs Correct\tTotal Obs\t%% Observed Correct\n"); fprintf(fd, "%ld\t\t%ld\t\t%f\n", (long)obs, total, (100. * obs / total)); if (output != NULL) - fclose(fd); + fclose(fd); G_free(pi); G_free(pj); G_free(pii); diff --git a/raster/r.kappa/main.c b/raster/r.kappa/main.c index d7c7a83faca..da6e276e0b3 100644 --- a/raster/r.kappa/main.c +++ b/raster/r.kappa/main.c @@ -54,12 +54,12 @@ int main(int argc, char **argv) struct GModule *module; struct { - struct Option *map, *ref, *output, *titles; + struct Option *map, *ref, *output, *titles; } parms; struct { - struct Flag *m, *w, *h; + struct Flag *m, *w, *h; } flags; G_gisinit(argv[0]); @@ -69,23 +69,23 @@ int main(int argc, char **argv) G_add_keyword(_("statistics")); G_add_keyword(_("classification")); module->description = - _("Calculates error matrix and kappa " - "parameter for accuracy assessment of classification result."); + _("Calculates error matrix and kappa " + "parameter for accuracy assessment of classification result."); parms.map = G_define_standard_option(G_OPT_R_INPUT); parms.map->key = "classification"; parms.map->description = - _("Name of raster map containing classification result"); + _("Name of raster map containing classification result"); parms.ref = G_define_standard_option(G_OPT_R_INPUT); parms.ref->key = "reference"; parms.ref->description = - _("Name of raster map containing reference classes"); + _("Name of raster map containing reference classes"); parms.output = G_define_standard_option(G_OPT_F_OUTPUT); parms.output->required = NO; parms.output->label = - _("Name for output file containing error matrix and kappa"); + _("Name for output file containing error matrix and kappa"); parms.output->description = _("If not given write to standard output"); parms.output->guisection = _("Output settings"); @@ -114,14 +114,14 @@ int main(int argc, char **argv) flags.m->guisection = _("Output settings"); if (G_parser(argc, argv)) - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); G_get_window(&window); maps[0] = parms.ref->answer; maps[1] = parms.map->answer; for (i = 0; i < 2; i++) - layer(maps[i]); + layer(maps[i]); output = parms.output->answer; @@ -130,13 +130,11 @@ int main(int argc, char **argv) /* run r.stats to obtain statistics of map layers */ stats(); - if(flags.m->answer) - { + if (flags.m->answer) { /* prepare the data for calculation */ prn2csv_error_mat(2048, flags.h->answer); - } - else - { + } + else { /* print header of the output */ if (!flags.h->answer) prn_header(); @@ -159,7 +157,7 @@ static void layer(const char *s) strcpy(name, s); if ((mapset = G_find_raster2(name, "")) == NULL) - G_fatal_error(_("Raster map <%s> not found"), s); + G_fatal_error(_("Raster map <%s> not found"), s); n = nlayers++; layers = (LAYER *) G_realloc(layers, 2 * sizeof(LAYER)); diff --git a/raster/r.kappa/mask.c b/raster/r.kappa/mask.c index b076138fa98..0f41fb7b8ff 100644 --- a/raster/r.kappa/mask.c +++ b/raster/r.kappa/mask.c @@ -19,10 +19,10 @@ char *maskinfo(void) results = NULL; if (G_find_raster("MASK", G_mapset()) == NULL) - return "none"; + return "none"; if (Rast_get_reclass("MASK", G_mapset(), &reclass) <= 0) { - sprintf(text, "MASK in %s", G_mapset()); - return append(results, text); + sprintf(text, "MASK in %s", G_mapset()); + return append(results, text); } sprintf(text, "%s in %s", reclass.name, reclass.mapset); @@ -30,15 +30,15 @@ char *maskinfo(void) next = 0; first = 1; do { - next = reclass_text(text, &reclass, next); - if (*text == 0) - break; - if (first) { - first = 0; - results = append(results, ", categories"); - } - results = append(results, " "); - results = append(results, text); + next = reclass_text(text, &reclass, next); + if (*text == 0) + break; + if (first) { + first = 0; + results = append(results, ", categories"); + } + results = append(results, " "); + results = append(results, text); } while (next >= 0); Rast_free_reclass(&reclass); @@ -54,21 +54,21 @@ static int reclass_text(char *text, struct Reclass *reclass, int next) n = reclass->num; first = -1; for (i = next; i < n; i++) { - if (reclass->table[i]) { - if (first < 0) - first = i; - } - else if (first >= 0) { - do_text(text, (long)(first + reclass->min), - (long)(i - 1 + reclass->min)); - first = -1; - if (strlen(text) > 60) - return i; - } + if (reclass->table[i]) { + if (first < 0) + first = i; + } + else if (first >= 0) { + do_text(text, (long)(first + reclass->min), + (long)(i - 1 + reclass->min)); + first = -1; + if (strlen(text) > 60) + return i; + } } if (first >= 0) - do_text(text, (long)(first + reclass->min), - (long)(i - 1 + reclass->min)); + do_text(text, (long)(first + reclass->min), + (long)(i - 1 + reclass->min)); return -1; } @@ -79,12 +79,12 @@ static void do_text(char *text, long first, long last) char work[40]; if (*text) - strcat(text, " "); + strcat(text, " "); if (first == last) - sprintf(work, "%ld", first); + sprintf(work, "%ld", first); else - sprintf(work, "%ld-%ld", first, last); + sprintf(work, "%ld-%ld", first, last); strcat(text, work); } @@ -93,7 +93,7 @@ static void do_text(char *text, long first, long last) static char *append(char *results, char *text) { if (results == NULL) - return G_store(text); + return G_store(text); results = G_realloc(results, strlen(results) + strlen(text) + 1); strcat(results, text); diff --git a/raster/r.kappa/prt2csv_mat.c b/raster/r.kappa/prt2csv_mat.c index a7789e75ac7..f870f8a1617 100644 --- a/raster/r.kappa/prt2csv_mat.c +++ b/raster/r.kappa/prt2csv_mat.c @@ -28,122 +28,122 @@ void prn2csv_error_mat(int out_cols, int hdr) char *cl; if (output != NULL) { - if (hdr) - fd = fopen(output, "w"); - else - fd = fopen(output, "a"); + if (hdr) + fd = fopen(output, "w"); + else + fd = fopen(output, "a"); } else - fd = stdout; + fd = stdout; if (fd == NULL) { - G_fatal_error(_("Cannot open file <%s> to write cats and counts (error matrix)"), - output); - return; + G_fatal_error(_("Cannot open file <%s> to write cats and counts (error matrix)"), + output); + return; } else { - /* get the cat lists */ - rlst = (long *)G_calloc(nstats * 2, sizeof(long)); - clst = (long *)G_calloc(nstats, sizeof(long)); - for (i = 0; i < nstats; i++) { - rlst[i] = Gstats[i].cats[0]; - clst[i] = Gstats[i].cats[1]; - } - - /* sort the cat lists */ - qsort(rlst, nstats, sizeof(long), longcomp); - qsort(clst, nstats, sizeof(long), longcomp); - - /* remove repeated cats */ - ncat1 = collapse(rlst, nstats); - ncat2 = collapse(clst, nstats); - - /* copy clst to the end of rlst, remove repeated cats, and free unused memory */ - for (i = 0; i < ncat2; i++) - rlst[ncat1 + i] = clst[i]; - qsort(rlst, ncat1 + ncat2, sizeof(long), longcomp); - ncat = collapse(rlst, ncat1 + ncat2); - rlst = (long *)G_realloc(rlst, ncat * sizeof(long)); - G_free(clst); - - /* allocate matrix and fill in with cats' value */ - matr = (long *)G_malloc(ncat * ncat * sizeof(long)); - for (i = 0; i < ncat * ncat; i++) - matr[i] = 0; - for (i = 0; i < nstats; i++) { - for (j = 0; j < ncat; j++) - if (rlst[j] == Gstats[i].cats[0]) - break; - for (k = 0; k < ncat; k++) - if (rlst[k] == Gstats[i].cats[1]) - break; - /* matrix: reference in columns, classification in rows */ - matr[j * ncat + k] = Gstats[i].count; - } - - /* format and print out the error matrix in panels */ - out_cols = 2048; - t_rowcount = 0; - first_col = 0; - last_col = ncat; - /* name line */ - /*fprintf(fd, "\t\t\t MAP1\n");*/ - /* cat line */ - fprintf(fd, "cat#\t"); - /* print labels MAP1*/ - for (j = 0; j < ncat; j++) { - cats = rlst; - cl = Rast_get_c_cat((CELL *) &(cats[j]), &(layers[0].labels)); - if (cl) - G_strip(cl); - if (cl == NULL || *cl == 0) - cl = "NULL"; - fprintf(fd, "%s\t", cl); - } - /*for (cndx = first_col; cndx < last_col; cndx++) */ - /* fprintf(fd, "%ld\t", rlst[cndx]); */ + /* get the cat lists */ + rlst = (long *)G_calloc(nstats * 2, sizeof(long)); + clst = (long *)G_calloc(nstats, sizeof(long)); + for (i = 0; i < nstats; i++) { + rlst[i] = Gstats[i].cats[0]; + clst[i] = Gstats[i].cats[1]; + } + + /* sort the cat lists */ + qsort(rlst, nstats, sizeof(long), longcomp); + qsort(clst, nstats, sizeof(long), longcomp); + + /* remove repeated cats */ + ncat1 = collapse(rlst, nstats); + ncat2 = collapse(clst, nstats); + + /* copy clst to the end of rlst, remove repeated cats, and free unused memory */ + for (i = 0; i < ncat2; i++) + rlst[ncat1 + i] = clst[i]; + qsort(rlst, ncat1 + ncat2, sizeof(long), longcomp); + ncat = collapse(rlst, ncat1 + ncat2); + rlst = (long *)G_realloc(rlst, ncat * sizeof(long)); + G_free(clst); + + /* allocate matrix and fill in with cats' value */ + matr = (long *)G_malloc(ncat * ncat * sizeof(long)); + for (i = 0; i < ncat * ncat; i++) + matr[i] = 0; + for (i = 0; i < nstats; i++) { + for (j = 0; j < ncat; j++) + if (rlst[j] == Gstats[i].cats[0]) + break; + for (k = 0; k < ncat; k++) + if (rlst[k] == Gstats[i].cats[1]) + break; + /* matrix: reference in columns, classification in rows */ + matr[j * ncat + k] = Gstats[i].count; + } + + /* format and print out the error matrix in panels */ + out_cols = 2048; + t_rowcount = 0; + first_col = 0; + last_col = ncat; + /* name line */ + /*fprintf(fd, "\t\t\t MAP1\n"); */ + /* cat line */ + fprintf(fd, "cat#\t"); + /* print labels MAP1 */ + for (j = 0; j < ncat; j++) { + cats = rlst; + cl = Rast_get_c_cat((CELL *) & (cats[j]), &(layers[0].labels)); + if (cl) + G_strip(cl); + if (cl == NULL || *cl == 0) + cl = "NULL"; + fprintf(fd, "%s\t", cl); + } + /*for (cndx = first_col; cndx < last_col; cndx++) */ + /* fprintf(fd, "%ld\t", rlst[cndx]); */ fprintf(fd, "RowSum"); fprintf(fd, "\n"); /* body of the matrix */ - mapone = "MAP2"; + mapone = "MAP2"; for (rndx = 0; rndx < ncat; rndx++) { cats = rlst; - cl = Rast_get_c_cat((CELL *) &(cats[rndx]),&(layers[1].labels)); - if (cl) - G_strip(cl); - if (cl == NULL || *cl == 0) - cl = "NULL"; - fprintf(fd, "%s\t", cl); - /* entries */ - for (cndx = first_col; cndx < last_col; cndx++) { - thisone = (ncat * rndx) + cndx; - fprintf(fd, "%ld\t", matr[thisone]); - } - /* row marginal summation */ - t_row = 0; - for (k = 0; k < ncat; k++) - t_row += matr[rndx * ncat + k]; - t_rowcount += t_row; - fprintf(fd, "%ld", t_row); - fprintf(fd, "\n"); - } - /* column marginal summation */ - fprintf(fd, "ColSum\t"); + cl = Rast_get_c_cat((CELL *) & (cats[rndx]), &(layers[1].labels)); + if (cl) + G_strip(cl); + if (cl == NULL || *cl == 0) + cl = "NULL"; + fprintf(fd, "%s\t", cl); + /* entries */ + for (cndx = first_col; cndx < last_col; cndx++) { + thisone = (ncat * rndx) + cndx; + fprintf(fd, "%ld\t", matr[thisone]); + } + /* row marginal summation */ + t_row = 0; + for (k = 0; k < ncat; k++) + t_row += matr[rndx * ncat + k]; + t_rowcount += t_row; + fprintf(fd, "%ld", t_row); + fprintf(fd, "\n"); + } + /* column marginal summation */ + fprintf(fd, "ColSum\t"); for (cndx = first_col; cndx < last_col; cndx++) { - t_col = 0; - x = cndx; - for (k = 0; k < ncat; k++) { - t_col += matr[x]; - x += ncat; - } - fprintf(fd, "%ld\t", t_col); - } - /* grand total */ - fprintf(fd, "%ld", t_rowcount); - fprintf(fd, "\n\n"); - G_free(matr); - if (output != NULL) - fclose(fd); + t_col = 0; + x = cndx; + for (k = 0; k < ncat; k++) { + t_col += matr[x]; + x += ncat; + } + fprintf(fd, "%ld\t", t_col); + } + /* grand total */ + fprintf(fd, "%ld", t_rowcount); + fprintf(fd, "\n\n"); + G_free(matr); + if (output != NULL) + fclose(fd); } } @@ -157,12 +157,12 @@ static int collapse(long *l, int n) c = l; m = 1; while (n-- > 0) { - if (*c != *l) { - c++; - *c = *l; - m++; - } - l++; + if (*c != *l) { + c++; + *c = *l; + m++; + } + l++; } return m; diff --git a/raster/r.kappa/prt_hdr.c b/raster/r.kappa/prt_hdr.c index 59859480e5f..baa7378c5f3 100644 --- a/raster/r.kappa/prt_hdr.c +++ b/raster/r.kappa/prt_hdr.c @@ -13,10 +13,10 @@ void prn_header(void) FILE *fd; if (output == NULL) - fd = stdout; + fd = stdout; else if ((fd = fopen(output, "w")) == NULL) { - G_fatal_error(_("Cannot open file <%s> to write header"), output); - return; + G_fatal_error(_("Cannot open file <%s> to write header"), output); + return; } /* print header */ @@ -24,7 +24,7 @@ void prn_header(void) sprintf(buf, "LOCATION: %s\t\t\t\t%s", G_location(), G_date()); fprintf(fd, "%s\n", buf); if ((mask = maskinfo())) - sprintf(buf, "MASK: %s", mask); + sprintf(buf, "MASK: %s", mask); fprintf(fd, "%s\n", buf); fprintf(fd, "MAPS: "); @@ -32,16 +32,16 @@ void prn_header(void) len = strlen(label); for (i = 0; i < nlayers; i++) { - titles = Rast_get_cats_title(&(layers[i].labels)); - if (titles) - G_strip(titles); - if (titles == NULL || *titles == 0) - titles = "(untitled)"; - sprintf(buf, "%*s%-*s%d = %s (%s in %s)", i * 6, "", len, label, - i + 1, titles, layers[i].name, layers[i].mapset); - fprintf(fd, "%s\n", buf); + titles = Rast_get_cats_title(&(layers[i].labels)); + if (titles) + G_strip(titles); + if (titles == NULL || *titles == 0) + titles = "(untitled)"; + sprintf(buf, "%*s%-*s%d = %s (%s in %s)", i * 6, "", len, label, + i + 1, titles, layers[i].name, layers[i].mapset); + fprintf(fd, "%s\n", buf); } if (output != NULL) - fclose(fd); + fclose(fd); } diff --git a/raster/r.kappa/prt_label.c b/raster/r.kappa/prt_label.c index 46cb44536f1..b55f6b4b961 100644 --- a/raster/r.kappa/prt_label.c +++ b/raster/r.kappa/prt_label.c @@ -12,26 +12,26 @@ void prt_label(void) FILE *fd; if (output == NULL) - fd = stdout; + fd = stdout; else if ((fd = fopen(output, "a")) == NULL) { - G_fatal_error(_("Can't open file <%s> to write label"), output); - return; + G_fatal_error(_("Can't open file <%s> to write label"), output); + return; } /* print labels */ for (i = 0; i < nlayers; i++) { - fprintf(fd, "\n"); - fprintf(fd, "MAP%-d Category Description\n", i + 1); - for (j = 0; j < ncat; j++) { - cats = rlst; - cl = Rast_get_c_cat((CELL *) &(cats[j]), &(layers[i].labels)); - if (cl) - G_strip(cl); - if (cl == NULL || *cl == 0) - cl = "(no description)"; - fprintf(fd, "%ld: %s\n", rlst[j], cl); - } + fprintf(fd, "\n"); + fprintf(fd, "MAP%-d Category Description\n", i + 1); + for (j = 0; j < ncat; j++) { + cats = rlst; + cl = Rast_get_c_cat((CELL *) & (cats[j]), &(layers[i].labels)); + if (cl) + G_strip(cl); + if (cl == NULL || *cl == 0) + cl = "(no description)"; + fprintf(fd, "%ld: %s\n", rlst[j], cl); + } } if (output != NULL) - fclose(fd); + fclose(fd); } diff --git a/raster/r.kappa/prt_mat.c b/raster/r.kappa/prt_mat.c index 84fb8fb7c10..1314e9a15c4 100644 --- a/raster/r.kappa/prt_mat.c +++ b/raster/r.kappa/prt_mat.c @@ -27,155 +27,156 @@ void prn_error_mat(int out_cols, int hdr) FILE *fd; if (output != NULL) { - if (hdr) - fd = fopen(output, "w"); - else - fd = fopen(output, "a"); + if (hdr) + fd = fopen(output, "w"); + else + fd = fopen(output, "a"); } else - fd = stdout; + fd = stdout; if (fd == NULL) { - G_fatal_error(_("Cannot open file <%s> to write cats and counts (error matrix)"), - output); - return; + G_fatal_error(_("Cannot open file <%s> to write cats and counts (error matrix)"), + output); + return; } else { - /* get the cat lists */ - rlst = (long *)G_calloc(nstats * 2, sizeof(long)); - clst = (long *)G_calloc(nstats, sizeof(long)); - for (i = 0; i < nstats; i++) { - rlst[i] = Gstats[i].cats[0]; - clst[i] = Gstats[i].cats[1]; - } - - /* sort the cat lists */ - qsort(rlst, nstats, sizeof(long), longcomp); - qsort(clst, nstats, sizeof(long), longcomp); - - /* remove repeated cats */ - ncat1 = collapse(rlst, nstats); - ncat2 = collapse(clst, nstats); - - /* copy clst to the end of rlst, remove repeated cats, and free unused memory */ - for (i = 0; i < ncat2; i++) - rlst[ncat1 + i] = clst[i]; - qsort(rlst, ncat1 + ncat2, sizeof(long), longcomp); - ncat = collapse(rlst, ncat1 + ncat2); - rlst = (long *)G_realloc(rlst, ncat * sizeof(long)); - G_free(clst); - - /* allocate matrix and fill in with cats' value */ - matr = (long *)G_malloc(ncat * ncat * sizeof(long)); - for (i = 0; i < ncat * ncat; i++) - matr[i] = 0; - for (i = 0; i < nstats; i++) { - for (j = 0; j < ncat; j++) - if (rlst[j] == Gstats[i].cats[0]) - break; - for (k = 0; k < ncat; k++) - if (rlst[k] == Gstats[i].cats[1]) - break; - /* matrix: reference in columns, classification in rows */ - matr[j * ncat + k] = Gstats[i].count; - } - - /* format and print out the error matrix in panels */ - out_cols = (out_cols == 132) ? 9 : 5; - num_panels = ncat / out_cols; - if (ncat % out_cols) - num_panels++; - t_rowcount = 0; - fprintf(fd, "\nError Matrix (MAP1: reference, MAP2: classification)\n"); - - for (at_panel = 0; at_panel < num_panels; at_panel++) { - first_col = at_panel * out_cols; - last_col = first_col + out_cols; - if (last_col >= ncat) { - last_col = ncat; - } - /* determine whether room available for row total at the end of last panel */ - addflag = 0; - if (at_panel == (num_panels - 1) && - (last_col - first_col) < (out_cols - 1)) { - addflag = 1; - } - /* panel line */ - fprintf(fd, "Panel #%d of %d\n", at_panel + 1, num_panels); - /* name line */ - fprintf(fd, "\t\t\t MAP1\n"); - /* cat line */ - fprintf(fd, " cat#\t"); - for (cndx = first_col; cndx < last_col; cndx++) - fprintf(fd, "%ld\t", rlst[cndx]); - if (addflag) - fprintf(fd, "Row Sum"); - fprintf(fd, "\n"); - /* body of the matrix */ - mapone = "MAP2"; - for (rndx = 0; rndx < ncat; rndx++) { - if (*(mapone) != '\0') - fprintf(fd, " %c %5ld\t", *(mapone)++, rlst[rndx]); - else - fprintf(fd, " %5ld\t", rlst[rndx]); - /* entries */ - for (cndx = first_col; cndx < last_col; cndx++) { - thisone = (ncat * rndx) + cndx; - fprintf(fd, "%ld\t", matr[thisone]); - } - /* row marginal summation */ - if (addflag) { - t_row = 0; - for (k = 0; k < ncat; k++) - t_row += matr[rndx * ncat + k]; - t_rowcount += t_row; - fprintf(fd, "%ld", t_row); - } - fprintf(fd, "\n"); - } - /* column marginal summation */ - fprintf(fd, "Col Sum\t\t"); - for (cndx = first_col; cndx < last_col; cndx++) { - t_col = 0; - x = cndx; - for (k = 0; k < ncat; k++) { - t_col += matr[x]; - x += ncat; - } - fprintf(fd, "%ld\t", t_col); - } - /* grand total */ - if (addflag) - fprintf(fd, "%ld", t_rowcount); - fprintf(fd, "\n\n"); - } - - /* Marginal summation if no room at the end of the last panel */ - if (!addflag) { - fprintf(fd, "cat#\tRow Sum\n"); - mapone = layers[1].name; - t_row = 0; - t_rowcount = 0; - grand_count = 0; - for (rndx = 0; rndx < ncat; rndx++) { - if (*(mapone) != '\0') - fprintf(fd, "%c %5ld", *(mapone)++, rlst[rndx]); - else - fprintf(fd, " %5ld", rlst[rndx]); - for (cndx = first_col; cndx < last_col; cndx++) { - thisone = (ncat * rndx) + cndx; - fprintf(fd, " %9ld ", matr[thisone]); - t_row += matr[thisone]; - } - t_rowcount += t_row; - grand_count += t_rowcount; - fprintf(fd, "%9ld\n", t_rowcount); - } - fprintf(fd, "%9ld\n", grand_count); - } - G_free(matr); - if (output != NULL) - fclose(fd); + /* get the cat lists */ + rlst = (long *)G_calloc(nstats * 2, sizeof(long)); + clst = (long *)G_calloc(nstats, sizeof(long)); + for (i = 0; i < nstats; i++) { + rlst[i] = Gstats[i].cats[0]; + clst[i] = Gstats[i].cats[1]; + } + + /* sort the cat lists */ + qsort(rlst, nstats, sizeof(long), longcomp); + qsort(clst, nstats, sizeof(long), longcomp); + + /* remove repeated cats */ + ncat1 = collapse(rlst, nstats); + ncat2 = collapse(clst, nstats); + + /* copy clst to the end of rlst, remove repeated cats, and free unused memory */ + for (i = 0; i < ncat2; i++) + rlst[ncat1 + i] = clst[i]; + qsort(rlst, ncat1 + ncat2, sizeof(long), longcomp); + ncat = collapse(rlst, ncat1 + ncat2); + rlst = (long *)G_realloc(rlst, ncat * sizeof(long)); + G_free(clst); + + /* allocate matrix and fill in with cats' value */ + matr = (long *)G_malloc(ncat * ncat * sizeof(long)); + for (i = 0; i < ncat * ncat; i++) + matr[i] = 0; + for (i = 0; i < nstats; i++) { + for (j = 0; j < ncat; j++) + if (rlst[j] == Gstats[i].cats[0]) + break; + for (k = 0; k < ncat; k++) + if (rlst[k] == Gstats[i].cats[1]) + break; + /* matrix: reference in columns, classification in rows */ + matr[j * ncat + k] = Gstats[i].count; + } + + /* format and print out the error matrix in panels */ + out_cols = (out_cols == 132) ? 9 : 5; + num_panels = ncat / out_cols; + if (ncat % out_cols) + num_panels++; + t_rowcount = 0; + fprintf(fd, + "\nError Matrix (MAP1: reference, MAP2: classification)\n"); + + for (at_panel = 0; at_panel < num_panels; at_panel++) { + first_col = at_panel * out_cols; + last_col = first_col + out_cols; + if (last_col >= ncat) { + last_col = ncat; + } + /* determine whether room available for row total at the end of last panel */ + addflag = 0; + if (at_panel == (num_panels - 1) && + (last_col - first_col) < (out_cols - 1)) { + addflag = 1; + } + /* panel line */ + fprintf(fd, "Panel #%d of %d\n", at_panel + 1, num_panels); + /* name line */ + fprintf(fd, "\t\t\t MAP1\n"); + /* cat line */ + fprintf(fd, " cat#\t"); + for (cndx = first_col; cndx < last_col; cndx++) + fprintf(fd, "%ld\t", rlst[cndx]); + if (addflag) + fprintf(fd, "Row Sum"); + fprintf(fd, "\n"); + /* body of the matrix */ + mapone = "MAP2"; + for (rndx = 0; rndx < ncat; rndx++) { + if (*(mapone) != '\0') + fprintf(fd, " %c %5ld\t", *(mapone)++, rlst[rndx]); + else + fprintf(fd, " %5ld\t", rlst[rndx]); + /* entries */ + for (cndx = first_col; cndx < last_col; cndx++) { + thisone = (ncat * rndx) + cndx; + fprintf(fd, "%ld\t", matr[thisone]); + } + /* row marginal summation */ + if (addflag) { + t_row = 0; + for (k = 0; k < ncat; k++) + t_row += matr[rndx * ncat + k]; + t_rowcount += t_row; + fprintf(fd, "%ld", t_row); + } + fprintf(fd, "\n"); + } + /* column marginal summation */ + fprintf(fd, "Col Sum\t\t"); + for (cndx = first_col; cndx < last_col; cndx++) { + t_col = 0; + x = cndx; + for (k = 0; k < ncat; k++) { + t_col += matr[x]; + x += ncat; + } + fprintf(fd, "%ld\t", t_col); + } + /* grand total */ + if (addflag) + fprintf(fd, "%ld", t_rowcount); + fprintf(fd, "\n\n"); + } + + /* Marginal summation if no room at the end of the last panel */ + if (!addflag) { + fprintf(fd, "cat#\tRow Sum\n"); + mapone = layers[1].name; + t_row = 0; + t_rowcount = 0; + grand_count = 0; + for (rndx = 0; rndx < ncat; rndx++) { + if (*(mapone) != '\0') + fprintf(fd, "%c %5ld", *(mapone)++, rlst[rndx]); + else + fprintf(fd, " %5ld", rlst[rndx]); + for (cndx = first_col; cndx < last_col; cndx++) { + thisone = (ncat * rndx) + cndx; + fprintf(fd, " %9ld ", matr[thisone]); + t_row += matr[thisone]; + } + t_rowcount += t_row; + grand_count += t_rowcount; + fprintf(fd, "%9ld\n", t_rowcount); + } + fprintf(fd, "%9ld\n", grand_count); + } + G_free(matr); + if (output != NULL) + fclose(fd); } } @@ -189,12 +190,12 @@ static int collapse(long *l, int n) c = l; m = 1; while (n-- > 0) { - if (*c != *l) { - c++; - *c = *l; - m++; - } - l++; + if (*c != *l) { + c++; + *c = *l; + m++; + } + l++; } return m; @@ -207,7 +208,7 @@ static int longcomp(const void *aa, const void *bb) const long *b = bb; if (*a < *b) - return -1; + return -1; return (*a > *b); } diff --git a/raster/r.kappa/stats.c b/raster/r.kappa/stats.c index db0b57caffd..55d8173d779 100644 --- a/raster/r.kappa/stats.c +++ b/raster/r.kappa/stats.c @@ -30,12 +30,12 @@ int stats(void) strcpy(mname, maps[1]); mmapset = G_find_raster2(mname, ""); if (mmapset == NULL) - G_fatal_error(_("Raster map <%s> not found"), maps[0]); + G_fatal_error(_("Raster map <%s> not found"), maps[0]); strcpy(rname, maps[0]); rmapset = G_find_raster2(rname, ""); if (rmapset == NULL) - G_fatal_error(_("Raster map <%s> not found"), maps[1]); + G_fatal_error(_("Raster map <%s> not found"), maps[1]); stats_file = G_tempfile(); @@ -46,8 +46,8 @@ int stats(void) argv[argc++] = "separator=:"; sprintf(buf, "input=%s,%s", - G_fully_qualified_name(mname, mmapset), - G_fully_qualified_name(rname, rmapset)); + G_fully_qualified_name(mname, mmapset), + G_fully_qualified_name(rname, rmapset)); argv[argc++] = buf; argv[argc++] = SF_REDIRECT_FILE; @@ -58,29 +58,29 @@ int stats(void) argv[argc++] = NULL; if (G_vspawn_ex(argv[0], argv) != 0) { - remove(stats_file); - G_fatal_error("error running r.stats"); + remove(stats_file); + G_fatal_error("error running r.stats"); } fd = fopen(stats_file, "r"); if (fd == NULL) { - unlink(stats_file); - sprintf(buf, "Unable to open result file <%s>\n", stats_file); + unlink(stats_file); + sprintf(buf, "Unable to open result file <%s>\n", stats_file); } while (G_getl(buf, sizeof buf, fd)) { - tokens = G_tokenize(buf, ":"); - i = 0; - ns = nstats++; - Gstats = (GSTATS *) G_realloc(Gstats, nstats * sizeof(GSTATS)); - Gstats[ns].cats = (long *)G_calloc(nlayers, sizeof(long)); - for (nl = 0; nl < nlayers; nl++) { - if (sscanf(tokens[i++], "%ld", &Gstats[ns].cats[nl]) != 1) - die(); - } - if (sscanf(tokens[i++], "%ld", &Gstats[ns].count) != 1) - die(); - G_free_tokens(tokens); + tokens = G_tokenize(buf, ":"); + i = 0; + ns = nstats++; + Gstats = (GSTATS *) G_realloc(Gstats, nstats * sizeof(GSTATS)); + Gstats[ns].cats = (long *)G_calloc(nlayers, sizeof(long)); + for (nl = 0; nl < nlayers; nl++) { + if (sscanf(tokens[i++], "%ld", &Gstats[ns].cats[nl]) != 1) + die(); + } + if (sscanf(tokens[i++], "%ld", &Gstats[ns].count) != 1) + die(); + G_free_tokens(tokens); } fclose(fd); unlink(stats_file); diff --git a/raster/r.kappa/sum.c b/raster/r.kappa/sum.c index a28a341eb07..9ac48fa16aa 100644 --- a/raster/r.kappa/sum.c +++ b/raster/r.kappa/sum.c @@ -20,12 +20,12 @@ long count_sum(int *ns, int nl) count = 0; if (nl >= 0) { - while (n < nstats && same_cats(k, n, nl)) - count += Gstats[n++].count; + while (n < nstats && same_cats(k, n, nl)) + count += Gstats[n++].count; } else { - while (n < nstats) - count += Gstats[n++].count; + while (n < nstats) + count += Gstats[n++].count; } *ns = n; @@ -42,8 +42,8 @@ static int same_cats(int a, int b, int nl) cat_b = Gstats[b].cats; while (nl-- >= 0) - if (*cat_a++ != *cat_b++) - return 0; + if (*cat_a++ != *cat_b++) + return 0; return 1; }