main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /***************************************************************
  2. *
  3. * MODULE: v.normal
  4. *
  5. * AUTHOR(S): James Darrell McCauley darrell@mccauley-usa.com
  6. * OGR support by Martin Landa <landa.martin gmail.com> (2009)
  7. *
  8. * PURPOSE: GRASS program for distributional testing (based on s.normal)
  9. *
  10. * COPYRIGHT: (C) 2001-2009 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Public License (>=v2). Read the file COPYING that
  14. * comes with GRASS for details.
  15. * Modification History:
  16. * <23 Jan 2001> - added field parameter, fixed reading of sites (MN)
  17. * <27 Aug 1994> - began coding. Adapted cdh.f from statlib (jdm)
  18. * <30 Sep 1994> - finished alpha version of cdh-c (jdm)
  19. * <10 Oct 1994> - announced version 0.1B on pasture.ecn.purdue.edu (jdm)
  20. * <02 Jan 1995> - cleaned man page, added html page (jdm)
  21. * <25 Feb 1995> - cleaned 'gcc -Wall' warnings (jdm)
  22. * <21 Jun 1995> - adapted to use new sites API (jdm)
  23. * <13 Sep 2000> - released under GPL
  24. *
  25. **************************************************************/
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <grass/gis.h>
  29. #include <grass/dbmi.h>
  30. #include <grass/vector.h>
  31. #include <grass/cdhc.h>
  32. #include <grass/glocale.h>
  33. int scan_cats(char *, long *, long *);
  34. int main(int argc, char **argv)
  35. {
  36. int i, nsites, warn_once = 0;
  37. int all;
  38. long x, y;
  39. struct Cell_head window;
  40. struct GModule *module;
  41. struct
  42. {
  43. struct Option *input, *tests, *dfield, *layer;
  44. } parm;
  45. struct
  46. {
  47. struct Flag *q, *l, *region;
  48. } flag;
  49. double *w, *z;
  50. struct Map_info Map;
  51. int line, nlines, npoints;
  52. int field;
  53. struct line_pnts *Points;
  54. struct line_cats *Cats;
  55. struct bound_box box;
  56. /* Attributes */
  57. int nrecords;
  58. int ctype;
  59. struct field_info *Fi;
  60. dbDriver *Driver;
  61. dbCatValArray cvarr;
  62. G_gisinit(argv[0]);
  63. module = G_define_module();
  64. G_add_keyword(_("vector"));
  65. G_add_keyword(_("statistics"));
  66. G_add_keyword(_("points"));
  67. module->description = _("Tests for normality for vector points.");
  68. parm.input = G_define_standard_option(G_OPT_V_MAP);
  69. parm.layer = G_define_standard_option(G_OPT_V_FIELD);
  70. parm.tests = G_define_option();
  71. parm.tests->key = "tests";
  72. parm.tests->key_desc = "range";
  73. parm.tests->type = TYPE_STRING;
  74. parm.tests->multiple = YES;
  75. parm.tests->required = YES;
  76. parm.tests->label = _("Lists of tests (1-15)");
  77. parm.tests->description = _("E.g. 1,3-8,13");
  78. parm.dfield = G_define_standard_option(G_OPT_DB_COLUMN);
  79. parm.dfield->required = YES;
  80. flag.region = G_define_flag();
  81. flag.region->key = 'r';
  82. flag.region->description = _("Use only points in current region");
  83. flag.l = G_define_flag();
  84. flag.l->key = 'l';
  85. flag.l->description = _("Lognormality instead of normality");
  86. if (G_parser(argc, argv))
  87. exit(EXIT_FAILURE);
  88. all = flag.region->answer ? 0 : 1;
  89. /* Open input */
  90. Vect_set_open_level(2);
  91. Vect_open_old2(&Map, parm.input->answer, "", parm.layer->answer);
  92. field = Vect_get_field_number(&Map, parm.layer->answer);
  93. /* Read attributes */
  94. Fi = Vect_get_field(&Map, field);
  95. if (Fi == NULL) {
  96. G_fatal_error("Database connection not defined for layer %d", field);
  97. }
  98. Driver = db_start_driver_open_database(Fi->driver, Fi->database);
  99. if (Driver == NULL)
  100. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  101. Fi->database, Fi->driver);
  102. nrecords = db_select_CatValArray(Driver, Fi->table, Fi->key, parm.dfield->answer,
  103. NULL, &cvarr);
  104. G_debug(1, "nrecords = %d", nrecords);
  105. ctype = cvarr.ctype;
  106. if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
  107. G_fatal_error(_("Only numeric column type supported"));
  108. if (nrecords < 0)
  109. G_fatal_error(_("Unable to select data from table"));
  110. G_verbose_message(_("%d records selected from table"), nrecords);
  111. db_close_database_shutdown_driver(Driver);
  112. /* Read points */
  113. npoints = Vect_get_num_primitives(&Map, GV_POINT);
  114. z = (double *)G_malloc(npoints * sizeof(double));
  115. G_get_window(&window);
  116. Vect_region_box(&window, &box);
  117. Points = Vect_new_line_struct();
  118. Cats = Vect_new_cats_struct();
  119. nlines = Vect_get_num_lines(&Map);
  120. nsites = 0;
  121. for (line = 1; line <= nlines; line++) {
  122. int type, cat, ret, cval;
  123. double dval;
  124. G_debug(3, "line = %d", line);
  125. type = Vect_read_line(&Map, Points, Cats, line);
  126. if (!(type & GV_POINT))
  127. continue;
  128. if (!all) {
  129. if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &box))
  130. continue;
  131. }
  132. Vect_cat_get(Cats, 1, &cat);
  133. G_debug(3, "cat = %d", cat);
  134. /* find actual value */
  135. if (ctype == DB_C_TYPE_INT) {
  136. ret = db_CatValArray_get_value_int(&cvarr, cat, &cval);
  137. if (ret != DB_OK) {
  138. G_warning(_("No record for cat %d"), cat);
  139. continue;
  140. }
  141. dval = cval;
  142. }
  143. else if (ctype == DB_C_TYPE_DOUBLE) {
  144. ret = db_CatValArray_get_value_double(&cvarr, cat, &dval);
  145. if (ret != DB_OK) {
  146. G_warning(_("No record for cat %d"), cat);
  147. continue;
  148. }
  149. }
  150. G_debug(3, "dval = %e", dval);
  151. z[nsites] = dval;
  152. nsites++;
  153. }
  154. G_verbose_message(_("Number of points: %d"), nsites);
  155. if (nsites <= 0)
  156. G_fatal_error(_("No points found"));
  157. if (nsites < 4)
  158. G_warning(_("Too small sample"));
  159. if (flag.l->answer) {
  160. warn_once = 0;
  161. for (i = 0; i < nsites; ++i) {
  162. if (z[i] > 1.0e-10)
  163. z[i] = log10(z[i]);
  164. else if (!warn_once) {
  165. G_warning(_("Negative or very small point values set to -10.0"));
  166. z[i] = -10.0;
  167. warn_once = 1;
  168. }
  169. }
  170. }
  171. for (i = 0; parm.tests->answers[i]; i++)
  172. if (!scan_cats(parm.tests->answers[i], &x, &y)) {
  173. G_usage();
  174. exit(EXIT_FAILURE);
  175. }
  176. for (i = 0; parm.tests->answers[i]; i++) {
  177. scan_cats(parm.tests->answers[i], &x, &y);
  178. while (x <= y)
  179. switch (x++) {
  180. case 1: /* moments */
  181. fprintf(stdout, _("Moments \\sqrt{b_1} and b_2: "));
  182. w = omnibus_moments(z, nsites);
  183. fprintf(stdout, "%g %g\n", w[0], w[1]);
  184. break;
  185. case 2: /* geary */
  186. fprintf(stdout, _("Geary's a-statistic & an approx. normal: "));
  187. w = geary_test(z, nsites);
  188. fprintf(stdout, "%g %g\n", w[0], w[1]);
  189. break;
  190. case 3: /* extreme deviates */
  191. fprintf(stdout, _("Extreme normal deviates: "));
  192. w = extreme(z, nsites);
  193. fprintf(stdout, "%g %g\n", w[0], w[1]);
  194. break;
  195. case 4: /* D'Agostino */
  196. fprintf(stdout, _("D'Agostino's D & an approx. normal: "));
  197. w = dagostino_d(z, nsites);
  198. fprintf(stdout, "%g %g\n", w[0], w[1]);
  199. break;
  200. case 5: /* Kuiper */
  201. fprintf(stdout,
  202. _("Kuiper's V (regular & modified for normality): "));
  203. w = kuipers_v(z, nsites);
  204. fprintf(stdout, "%g %g\n", w[1], w[0]);
  205. break;
  206. case 6: /* Watson */
  207. fprintf(stdout,
  208. _("Watson's U^2 (regular & modified for normality): "));
  209. w = watson_u2(z, nsites);
  210. fprintf(stdout, "%g %g\n", w[1], w[0]);
  211. break;
  212. case 7: /* Durbin */
  213. fprintf(stdout,
  214. _("Durbin's Exact Test (modified Kolmogorov): "));
  215. w = durbins_exact(z, nsites);
  216. fprintf(stdout, "%g\n", w[0]);
  217. break;
  218. case 8: /* Anderson-Darling */
  219. fprintf(stdout,
  220. _("Anderson-Darling's A^2 (regular & modified for normality): "));
  221. w = anderson_darling(z, nsites);
  222. fprintf(stdout, "%g %g\n", w[1], w[0]);
  223. break;
  224. case 9: /* Cramer-Von Mises */
  225. fprintf(stdout,
  226. _("Cramer-Von Mises W^2(regular & modified for normality): "));
  227. w = cramer_von_mises(z, nsites);
  228. fprintf(stdout, "%g %g\n", w[1], w[0]);
  229. break;
  230. case 10: /* Kolmogorov-Smirnov */
  231. fprintf(stdout,
  232. _("Kolmogorov-Smirnov's D (regular & modified for normality): "));
  233. w = kolmogorov_smirnov(z, nsites);
  234. fprintf(stdout, "%g %g\n", w[1], w[0]);
  235. break;
  236. case 11: /* chi-square */
  237. fprintf(stdout,
  238. _("Chi-Square stat (equal probability classes) and d.f.: "));
  239. w = chi_square(z, nsites);
  240. fprintf(stdout, "%g %d\n", w[0], (int)w[1]);
  241. break;
  242. case 12: /* Shapiro-Wilk */
  243. if (nsites > 50) {
  244. G_warning(_("Shapiro-Wilk's W cannot be used for n > 50"));
  245. if (nsites < 99)
  246. G_message(_("Use Weisberg-Binghams's W''"));
  247. }
  248. else {
  249. fprintf(stdout, _("Shapiro-Wilk W: "));
  250. w = shapiro_wilk(z, nsites);
  251. fprintf(stdout, "%g\n", w[0]);
  252. }
  253. break;
  254. case 13: /* Weisberg-Bingham */
  255. if (nsites > 99 || nsites < 50)
  256. G_warning(_("Weisberg-Bingham's W'' cannot be used for n < 50 or n > 99"));
  257. else {
  258. fprintf(stdout, _("Weisberg-Bingham's W'': "));
  259. w = weisberg_bingham(z, nsites);
  260. fprintf(stdout, "%g\n", w[0]);
  261. }
  262. break;
  263. case 14: /* Royston */
  264. if (nsites > 2000)
  265. G_warning(_("Royston only extended Shapiro-Wilk's W up to n = 2000"));
  266. else {
  267. fprintf(stdout, _("Shapiro-Wilk W'': "));
  268. w = royston(z, nsites);
  269. fprintf(stdout, "%g\n", w[0]);
  270. }
  271. break;
  272. case 15: /* Kotz */
  273. fprintf(stdout, _("Kotz' T'_f (Lognormality vs. Normality): "));
  274. w = kotz_families(z, nsites);
  275. fprintf(stdout, "%g\n", w[0]);
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. exit(EXIT_SUCCESS);
  282. }