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: r.kappa indentation update cherry-picked from #2544 #2641

Merged
merged 1 commit into from
Nov 10, 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
72 changes: 36 additions & 36 deletions raster/r.kappa/calc_kappa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
28 changes: 13 additions & 15 deletions raster/r.kappa/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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");

Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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));
Expand Down
58 changes: 29 additions & 29 deletions raster/r.kappa/mask.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ 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);
results = append(results, text);
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);

Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
Loading