[GRASS-SVN] r72593 - grass/trunk/raster/r.kappa

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Apr 6 05:38:39 PDT 2018


Author: ychemin
Date: 2018-04-06 05:38:39 -0700 (Fri, 06 Apr 2018)
New Revision: 72593

Added:
   grass/trunk/raster/r.kappa/prt2csv_mat.c
Modified:
   grass/trunk/raster/r.kappa/local_proto.h
   grass/trunk/raster/r.kappa/main.c
Log:
Added Matrix Report only flag -m

Modified: grass/trunk/raster/r.kappa/local_proto.h
===================================================================
--- grass/trunk/raster/r.kappa/local_proto.h	2018-04-05 15:05:12 UTC (rev 72592)
+++ grass/trunk/raster/r.kappa/local_proto.h	2018-04-06 12:38:39 UTC (rev 72593)
@@ -13,6 +13,9 @@
 /* prt_mat.c */
 void prn_error_mat(int out_cols, int hdr);
 
+/* prt2csv_mat.c */
+void prn2csv_error_mat(int out_cols, int hdr);
+
 /* stats.c */
 int stats(void);
 

Modified: grass/trunk/raster/r.kappa/main.c
===================================================================
--- grass/trunk/raster/r.kappa/main.c	2018-04-05 15:05:12 UTC (rev 72592)
+++ grass/trunk/raster/r.kappa/main.c	2018-04-06 12:38:39 UTC (rev 72593)
@@ -59,7 +59,7 @@
 
     struct
     {
-	struct Flag *n, *w, *h;
+	struct Flag *m, *w, *h;
     } flags;
 
     G_gisinit(argv[0]);
@@ -108,6 +108,11 @@
     flags.h->description = _("No header in the report");
     flags.h->guisection = _("Formatting");
 
+    flags.m = G_define_flag();
+    flags.m->key = 'm';
+    flags.m->description = _("Print Matrix only");
+    flags.m->guisection = _("Output settings");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -125,16 +130,23 @@
     /* run r.stats to obtain statistics of map layers */
     stats();
 
-    /* print header of the output */
-    if (!flags.h->answer)
-	prn_header();
+    if(flags.m->answer)
+    {
+        /* prepare the data for calculation */
+        prn2csv_error_mat(2048, flags.h->answer);
+    } 
+    else 
+    {
+        /* print header of the output */
+        if (!flags.h->answer)
+            prn_header();
 
-    /* prepare the data for calculation */
-    prn_error_mat(flags.w->answer ? 132 : 80, flags.h->answer);
+        /* prepare the data for calculation */
+        prn_error_mat(flags.w->answer ? 132 : 80, flags.h->answer);
 
-    /* generate the error matrix, kappa and variance */
-    calc_kappa();
-
+        /* generate the error matrix, kappa and variance */
+        calc_kappa();
+    }
     return EXIT_SUCCESS;
 }
 

Added: grass/trunk/raster/r.kappa/prt2csv_mat.c
===================================================================
--- grass/trunk/raster/r.kappa/prt2csv_mat.c	                        (rev 0)
+++ grass/trunk/raster/r.kappa/prt2csv_mat.c	2018-04-06 12:38:39 UTC (rev 72593)
@@ -0,0 +1,164 @@
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "kappa.h"
+#include "local_proto.h"
+
+
+static int longcomp(const void *aa, const void *bb);
+static int collapse(long *l, int n);
+
+
+void prn2csv_error_mat(int out_cols, int hdr)
+{
+    int i, j, k;
+    int ncat1, ncat2;
+    long x;
+    long *clst;
+
+    int cndx, rndx;
+    int first_col = 0, last_col = 0;
+    int thisone;
+    long t_row, t_col;
+    long t_rowcount, grand_count;
+    const char *mapone;
+    FILE *fd;
+
+    if (output != NULL) {
+	if (hdr)
+	    fd = fopen(output, "w");
+	else
+	    fd = fopen(output, "a");
+    }
+    else
+	fd = stdout;
+
+    if (fd == NULL) {
+	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");
+	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";
+        for (rndx = 0; rndx < ncat; rndx++) {
+	    /*if (*(mapone) != '\0')
+	        fprintf(fd, " %c %5ld\t", *(mapone)++, rlst[rndx]);
+	    else
+	        fprintf(fd, "   %5ld\t", rlst[rndx]);
+	    */
+	    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 */
+	    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);
+    }
+}
+
+
+/* remove repeated values */
+static int collapse(long *l, int n)
+{
+    long *c;
+    int m;
+
+    c = l;
+    m = 1;
+    while (n-- > 0) {
+	if (*c != *l) {
+	    c++;
+	    *c = *l;
+	    m++;
+	}
+	l++;
+    }
+
+    return m;
+}
+
+
+static int longcomp(const void *aa, const void *bb)
+{
+    const long *a = aa;
+    const long *b = bb;
+
+    return (*a - *b);
+}



More information about the grass-commit mailing list