univar_statistics.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 .open_stds import *
  16. import grass.script as gscript
  17. ###############################################################################
  18. def print_gridded_dataset_univar_statistics(type, input, output, where, extended,
  19. no_header=False, fs="|",
  20. rast_region=False):
  21. """Print univariate statistics for a space time raster or raster3d dataset
  22. :param type: Must be "strds" or "str3ds"
  23. :param input: The name of the space time dataset
  24. :param output: Name of the optional output file, if None stdout is used
  25. :param where: A temporal database where statement
  26. :param extended: If True compute extended statistics
  27. :param no_header: Supress the printing of column names
  28. :param fs: Field separator
  29. :param rast_region: If set True ignore the current region settings
  30. and use the raster map regions for univar statistical calculation.
  31. Only available for strds.
  32. """
  33. # We need a database interface
  34. dbif = SQLDatabaseInterfaceConnection()
  35. dbif.connect()
  36. sp = open_old_stds(input, type, dbif)
  37. if output is not None:
  38. out_file = open(output, "w")
  39. rows = sp.get_registered_maps(
  40. "id,start_time,end_time", where, "start_time", dbif)
  41. if not rows:
  42. dbif.close()
  43. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  44. 'sp': sp.get_new_map_instance(None).get_type(),
  45. 'i': sp.get_id()})
  46. if no_header is False:
  47. string = ""
  48. string += "id" + fs + "start" + fs + "end" + fs + "mean" + fs
  49. string += "min" + fs + "max" + fs
  50. string += "mean_of_abs" + fs + "stddev" + fs + "variance" + fs
  51. string += "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  52. if extended is True:
  53. string += fs + "first_quartile" + fs + "median" + fs
  54. string += "third_quartile" + fs + "percentile_90"
  55. if output is None:
  56. print(string)
  57. else:
  58. out_file.write(string + "\n")
  59. for row in rows:
  60. string = ""
  61. id = row["id"]
  62. start = row["start_time"]
  63. end = row["end_time"]
  64. flag = "g"
  65. if extended is True:
  66. flag += "e"
  67. if type == "strds" and rast_region is True:
  68. flag += "r"
  69. if type == "strds":
  70. stats = gscript.parse_command("r.univar", map=id, flags=flag)
  71. elif type == "str3ds":
  72. stats = gscript.parse_command("r3.univar", map=id, flags=flag)
  73. if not stats:
  74. if type == "strds":
  75. gscript.warning(_("Unable to get statistics for raster map "
  76. "<%s>") % id)
  77. elif type == "str3ds":
  78. gscript.warning(_("Unable to get statistics for 3d raster map"
  79. " <%s>") % id)
  80. continue
  81. string += str(id) + fs + str(start) + fs + str(end)
  82. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  83. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  84. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  85. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  86. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  87. if extended is True:
  88. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  89. string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  90. if output is None:
  91. print(string)
  92. else:
  93. out_file.write(string + "\n")
  94. dbif.close()
  95. if output is not None:
  96. out_file.close()
  97. ###############################################################################
  98. def print_vector_dataset_univar_statistics(input, output, twhere, layer, type, column,
  99. where, extended, no_header=False,
  100. fs="|"):
  101. """Print univariate statistics for a space time vector dataset
  102. :param input: The name of the space time dataset
  103. :param output: Name of the optional output file, if None stdout is used
  104. :param twhere: A temporal database where statement
  105. :param layer: The layer number used in case no layer is present
  106. in the temporal dataset
  107. :param type: options: point,line,boundary,centroid,area
  108. :param column: The name of the attribute column
  109. :param where: A temporal database where statement
  110. :param extended: If True compute extended statistics
  111. :param no_header: Supress the printing of column names
  112. :param fs: Field separator
  113. """
  114. # We need a database interface
  115. dbif = SQLDatabaseInterfaceConnection()
  116. dbif.connect()
  117. if output is not None:
  118. out_file = open(output, "w")
  119. mapset = get_current_mapset()
  120. if input.find("@") >= 0:
  121. id = input
  122. else:
  123. id = input + "@" + mapset
  124. sp = dataset_factory("stvds", id)
  125. if sp.is_in_db(dbif) is False:
  126. dbif.close()
  127. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> not found") % {
  128. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  129. sp.select(dbif)
  130. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  131. twhere, "start_time", dbif)
  132. if not rows:
  133. dbif.close()
  134. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  135. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  136. string = ""
  137. if no_header is False:
  138. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  139. fs + "nmissing" + fs + "nnull" + fs
  140. string += "min" + fs + "max" + fs + "range"
  141. if type == "point" or type == "centroid":
  142. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  143. fs + "population_variance" + fs
  144. string += "population_coeff_variation" + fs + \
  145. "sample_stddev" + fs + "sample_variance" + fs
  146. string += "kurtosis" + fs + "skewness"
  147. if extended is True:
  148. string += fs + "first_quartile" + fs + "median" + fs + \
  149. "third_quartile" + fs + "percentile_90"
  150. if output is None:
  151. print(string)
  152. else:
  153. out_file.write(string + "\n")
  154. for row in rows:
  155. id = row["name"] + "@" + row["mapset"]
  156. start = row["start_time"]
  157. end = row["end_time"]
  158. mylayer = row["layer"]
  159. flags = "g"
  160. if extended is True:
  161. flags += "e"
  162. if not mylayer:
  163. mylayer = layer
  164. stats = gscript.parse_command("v.univar", map=id, where=where,
  165. column=column, layer=mylayer,
  166. type=type, flags=flags)
  167. string = ""
  168. if not stats:
  169. gscript.warning(_("Unable to get statistics for vector map <%s>")
  170. % id)
  171. continue
  172. string += str(id) + fs + str(start) + fs + str(end)
  173. string += fs + str(stats["n"]) + fs + str(stats[
  174. "nmissing"]) + fs + str(stats["nnull"])
  175. if "min" in stats:
  176. string += fs + str(stats["min"]) + fs + str(
  177. stats["max"]) + fs + str(stats["range"])
  178. else:
  179. string += fs + fs + fs
  180. if type == "point" or type == "centroid":
  181. if "mean" in stats:
  182. string += fs + str(stats["mean"]) + fs + \
  183. str(stats["mean_abs"]) + fs + \
  184. str(stats["population_stddev"]) + fs + \
  185. str(stats["population_variance"])
  186. string += fs + str(stats["population_coeff_variation"]) + \
  187. fs + str(stats["sample_stddev"]) + fs + \
  188. str(stats["sample_variance"])
  189. string += fs + str(stats["kurtosis"]) + fs + \
  190. str(stats["skewness"])
  191. else:
  192. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  193. if extended is True:
  194. if "first_quartile" in stats:
  195. string += fs + str(stats["first_quartile"]) + fs + \
  196. str(stats["median"]) + fs + \
  197. str(stats["third_quartile"]) + fs + \
  198. str(stats["percentile_90"])
  199. else:
  200. string += fs + fs + fs + fs
  201. if output is None:
  202. print(string)
  203. else:
  204. out_file.write(string + "\n")
  205. dbif.close()
  206. if output is not None:
  207. out_file.close()