univar_statistics.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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(type, input, output, where, extended,
  21. no_header=False, fs="|",
  22. rast_region=False):
  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(
  42. "id,start_time,end_time", where, "start_time", dbif)
  43. if not rows:
  44. dbif.close()
  45. err = "Space time %(sp)s dataset <%(i)s> is empty"
  46. if where:
  47. err += " or where condition is wrong"
  48. gscript.fatal(_(err) % {'sp': sp.get_new_map_instance(None).get_type(),
  49. 'i': sp.get_id()})
  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(_("Unable to get statistics for raster map "
  81. "<%s>") % id)
  82. elif type == "str3ds":
  83. gscript.warning(_("Unable to get statistics for 3d raster map"
  84. " <%s>") % id)
  85. continue
  86. string += str(id) + fs + str(start) + fs + str(end)
  87. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  88. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  89. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  90. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  91. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  92. string += fs + str(int(stats["cells"]) - int(stats["null_cells"]))
  93. if extended is True:
  94. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  95. string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  96. if output is None:
  97. print(string)
  98. else:
  99. out_file.write(string + "\n")
  100. dbif.close()
  101. if output is not None:
  102. out_file.close()
  103. ###############################################################################
  104. def print_vector_dataset_univar_statistics(input, output, twhere, layer, type, column,
  105. where, extended, no_header=False,
  106. fs="|"):
  107. """Print univariate statistics for a space time vector dataset
  108. :param input: The name of the space time dataset
  109. :param output: Name of the optional output file, if None stdout is used
  110. :param twhere: A temporal database where statement
  111. :param layer: The layer number used in case no layer is present
  112. in the temporal dataset
  113. :param type: options: point,line,boundary,centroid,area
  114. :param column: The name of the attribute column
  115. :param where: A temporal database where statement
  116. :param extended: If True compute extended statistics
  117. :param no_header: Suppress the printing of column names
  118. :param fs: Field separator
  119. """
  120. # We need a database interface
  121. dbif = SQLDatabaseInterfaceConnection()
  122. dbif.connect()
  123. if output is not None:
  124. out_file = open(output, "w")
  125. mapset = get_current_mapset()
  126. if input.find("@") >= 0:
  127. id = input
  128. else:
  129. id = input + "@" + mapset
  130. sp = dataset_factory("stvds", id)
  131. if sp.is_in_db(dbif) is False:
  132. dbif.close()
  133. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> not found") % {
  134. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  135. sp.select(dbif)
  136. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  137. twhere, "start_time", dbif)
  138. if not rows:
  139. dbif.close()
  140. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  141. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  142. string = ""
  143. if no_header is False:
  144. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  145. fs + "nmissing" + fs + "nnull" + fs
  146. string += "min" + fs + "max" + fs + "range"
  147. if type == "point" or type == "centroid":
  148. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  149. fs + "population_variance" + fs
  150. string += "population_coeff_variation" + fs + \
  151. "sample_stddev" + fs + "sample_variance" + fs
  152. string += "kurtosis" + fs + "skewness"
  153. if extended is True:
  154. string += fs + "first_quartile" + fs + "median" + fs + \
  155. "third_quartile" + fs + "percentile_90"
  156. if output is None:
  157. print(string)
  158. else:
  159. out_file.write(string + "\n")
  160. for row in rows:
  161. id = row["name"] + "@" + row["mapset"]
  162. start = row["start_time"]
  163. end = row["end_time"]
  164. mylayer = row["layer"]
  165. flags = "g"
  166. if extended is True:
  167. flags += "e"
  168. if not mylayer:
  169. mylayer = layer
  170. stats = gscript.parse_command("v.univar", map=id, where=where,
  171. column=column, layer=mylayer,
  172. type=type, flags=flags)
  173. string = ""
  174. if not stats:
  175. gscript.warning(_("Unable to get statistics for vector map <%s>")
  176. % id)
  177. continue
  178. string += str(id) + fs + str(start) + fs + str(end)
  179. string += fs + str(stats["n"]) + fs + str(stats[
  180. "nmissing"]) + fs + str(stats["nnull"])
  181. if "min" in stats:
  182. string += fs + str(stats["min"]) + fs + str(
  183. stats["max"]) + fs + str(stats["range"])
  184. else:
  185. string += fs + fs + fs
  186. if type == "point" or type == "centroid":
  187. if "mean" in stats:
  188. string += fs + str(stats["mean"]) + fs + \
  189. str(stats["mean_abs"]) + fs + \
  190. str(stats["population_stddev"]) + fs + \
  191. str(stats["population_variance"])
  192. string += fs + str(stats["population_coeff_variation"]) + \
  193. fs + str(stats["sample_stddev"]) + fs + \
  194. str(stats["sample_variance"])
  195. string += fs + str(stats["kurtosis"]) + fs + \
  196. str(stats["skewness"])
  197. else:
  198. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  199. if extended is True:
  200. if "first_quartile" in stats:
  201. string += fs + str(stats["first_quartile"]) + fs + \
  202. str(stats["median"]) + fs + \
  203. str(stats["third_quartile"]) + fs + \
  204. str(stats["percentile_90"])
  205. else:
  206. string += fs + fs + fs + fs
  207. if output is None:
  208. print(string)
  209. else:
  210. out_file.write(string + "\n")
  211. dbif.close()
  212. if output is not None:
  213. out_file.close()