univar_statistics.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. """
  2. Univariate statistic function for space time datasets
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. tgis.print_gridded_dataset_univar_statistics(type, input, output, where, extended, no_header, fs, rast_region)
  7. ..
  8. (C) 2012-2013 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. :authors: Soeren Gebbert
  13. """
  14. from __future__ import print_function
  15. from .core import SQLDatabaseInterfaceConnection, get_current_mapset
  16. from .factory import dataset_factory
  17. from .open_stds import open_old_stds
  18. import grass.script as gscript
  19. ###############################################################################
  20. def print_gridded_dataset_univar_statistics(
  21. type, input, output, where, extended, no_header=False, fs="|", rast_region=False
  22. ):
  23. """Print univariate statistics for a space time raster or raster3d dataset
  24. :param type: Must be "strds" or "str3ds"
  25. :param input: The name of the space time dataset
  26. :param output: Name of the optional output file, if None stdout is used
  27. :param where: A temporal database where statement
  28. :param extended: If True compute extended statistics
  29. :param no_header: Suppress the printing of column names
  30. :param fs: Field separator
  31. :param rast_region: If set True ignore the current region settings
  32. and use the raster map regions for univar statistical calculation.
  33. Only available for strds.
  34. """
  35. # We need a database interface
  36. dbif = SQLDatabaseInterfaceConnection()
  37. dbif.connect()
  38. sp = open_old_stds(input, type, dbif)
  39. if output is not None:
  40. out_file = open(output, "w")
  41. rows = sp.get_registered_maps("id,start_time,end_time", where, "start_time", dbif)
  42. if not rows:
  43. dbif.close()
  44. err = "Space time %(sp)s dataset <%(i)s> is empty"
  45. if where:
  46. err += " or where condition is wrong"
  47. gscript.fatal(
  48. _(err) % {"sp": sp.get_new_map_instance(None).get_type(), "i": sp.get_id()}
  49. )
  50. if no_header is False:
  51. string = ""
  52. string += "id" + fs + "start" + fs + "end" + fs + "mean" + fs
  53. string += "min" + fs + "max" + fs
  54. string += "mean_of_abs" + fs + "stddev" + fs + "variance" + fs
  55. string += "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  56. string += fs + "non_null_cells"
  57. if extended is True:
  58. string += fs + "first_quartile" + fs + "median" + fs
  59. string += "third_quartile" + fs + "percentile_90"
  60. if output is None:
  61. print(string)
  62. else:
  63. out_file.write(string + "\n")
  64. for row in rows:
  65. string = ""
  66. id = row["id"]
  67. start = row["start_time"]
  68. end = row["end_time"]
  69. flag = "g"
  70. if extended is True:
  71. flag += "e"
  72. if type == "strds" and rast_region is True:
  73. flag += "r"
  74. if type == "strds":
  75. stats = gscript.parse_command("r.univar", map=id, flags=flag)
  76. elif type == "str3ds":
  77. stats = gscript.parse_command("r3.univar", map=id, flags=flag)
  78. if not stats:
  79. if type == "strds":
  80. gscript.warning(
  81. _("Unable to get statistics for raster map " "<%s>") % id
  82. )
  83. elif type == "str3ds":
  84. gscript.warning(
  85. _("Unable to get statistics for 3d raster map" " <%s>") % id
  86. )
  87. continue
  88. string += str(id) + fs + str(start) + fs + str(end)
  89. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  90. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  91. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  92. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  93. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  94. string += fs + str(int(stats["cells"]) - int(stats["null_cells"]))
  95. if extended is True:
  96. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  97. string += (
  98. fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  99. )
  100. if output is None:
  101. print(string)
  102. else:
  103. out_file.write(string + "\n")
  104. dbif.close()
  105. if output is not None:
  106. out_file.close()
  107. ###############################################################################
  108. def print_vector_dataset_univar_statistics(
  109. input, output, twhere, layer, type, column, where, extended, no_header=False, fs="|"
  110. ):
  111. """Print univariate statistics for a space time vector dataset
  112. :param input: The name of the space time dataset
  113. :param output: Name of the optional output file, if None stdout is used
  114. :param twhere: A temporal database where statement
  115. :param layer: The layer number used in case no layer is present
  116. in the temporal dataset
  117. :param type: options: point,line,boundary,centroid,area
  118. :param column: The name of the attribute column
  119. :param where: A temporal database where statement
  120. :param extended: If True compute extended statistics
  121. :param no_header: Suppress the printing of column names
  122. :param fs: Field separator
  123. """
  124. # We need a database interface
  125. dbif = SQLDatabaseInterfaceConnection()
  126. dbif.connect()
  127. if output is not None:
  128. out_file = open(output, "w")
  129. mapset = get_current_mapset()
  130. if input.find("@") >= 0:
  131. id = input
  132. else:
  133. id = input + "@" + mapset
  134. sp = dataset_factory("stvds", id)
  135. if sp.is_in_db(dbif) is False:
  136. dbif.close()
  137. gscript.fatal(
  138. _("Space time %(sp)s dataset <%(i)s> not found")
  139. % {"sp": sp.get_new_map_instance(None).get_type(), "i": id}
  140. )
  141. sp.select(dbif)
  142. rows = sp.get_registered_maps(
  143. "id,name,mapset,start_time,end_time,layer", twhere, "start_time", dbif
  144. )
  145. if not rows:
  146. dbif.close()
  147. gscript.fatal(
  148. _("Space time %(sp)s dataset <%(i)s> is empty")
  149. % {"sp": sp.get_new_map_instance(None).get_type(), "i": id}
  150. )
  151. string = ""
  152. if no_header is False:
  153. string += (
  154. "id"
  155. + fs
  156. + "start"
  157. + fs
  158. + "end"
  159. + fs
  160. + "n"
  161. + fs
  162. + "nmissing"
  163. + fs
  164. + "nnull"
  165. + fs
  166. )
  167. string += "min" + fs + "max" + fs + "range"
  168. if type == "point" or type == "centroid":
  169. string += (
  170. fs
  171. + "mean"
  172. + fs
  173. + "mean_abs"
  174. + fs
  175. + "population_stddev"
  176. + fs
  177. + "population_variance"
  178. + fs
  179. )
  180. string += (
  181. "population_coeff_variation"
  182. + fs
  183. + "sample_stddev"
  184. + fs
  185. + "sample_variance"
  186. + fs
  187. )
  188. string += "kurtosis" + fs + "skewness"
  189. if extended is True:
  190. string += (
  191. fs
  192. + "first_quartile"
  193. + fs
  194. + "median"
  195. + fs
  196. + "third_quartile"
  197. + fs
  198. + "percentile_90"
  199. )
  200. if output is None:
  201. print(string)
  202. else:
  203. out_file.write(string + "\n")
  204. for row in rows:
  205. id = row["name"] + "@" + row["mapset"]
  206. start = row["start_time"]
  207. end = row["end_time"]
  208. mylayer = row["layer"]
  209. flags = "g"
  210. if extended is True:
  211. flags += "e"
  212. if not mylayer:
  213. mylayer = layer
  214. stats = gscript.parse_command(
  215. "v.univar",
  216. map=id,
  217. where=where,
  218. column=column,
  219. layer=mylayer,
  220. type=type,
  221. flags=flags,
  222. )
  223. string = ""
  224. if not stats:
  225. gscript.warning(_("Unable to get statistics for vector map <%s>") % id)
  226. continue
  227. string += str(id) + fs + str(start) + fs + str(end)
  228. string += (
  229. fs
  230. + str(stats["n"])
  231. + fs
  232. + str(stats["nmissing"])
  233. + fs
  234. + str(stats["nnull"])
  235. )
  236. if "min" in stats:
  237. string += (
  238. fs
  239. + str(stats["min"])
  240. + fs
  241. + str(stats["max"])
  242. + fs
  243. + str(stats["range"])
  244. )
  245. else:
  246. string += fs + fs + fs
  247. if type == "point" or type == "centroid":
  248. if "mean" in stats:
  249. string += (
  250. fs
  251. + str(stats["mean"])
  252. + fs
  253. + str(stats["mean_abs"])
  254. + fs
  255. + str(stats["population_stddev"])
  256. + fs
  257. + str(stats["population_variance"])
  258. )
  259. string += (
  260. fs
  261. + str(stats["population_coeff_variation"])
  262. + fs
  263. + str(stats["sample_stddev"])
  264. + fs
  265. + str(stats["sample_variance"])
  266. )
  267. string += fs + str(stats["kurtosis"]) + fs + str(stats["skewness"])
  268. else:
  269. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  270. if extended is True:
  271. if "first_quartile" in stats:
  272. string += (
  273. fs
  274. + str(stats["first_quartile"])
  275. + fs
  276. + str(stats["median"])
  277. + fs
  278. + str(stats["third_quartile"])
  279. + fs
  280. + str(stats["percentile_90"])
  281. )
  282. else:
  283. string += fs + fs + fs + fs
  284. if output is None:
  285. print(string)
  286. else:
  287. out_file.write(string + "\n")
  288. dbif.close()
  289. if output is not None:
  290. out_file.close()