univar_statistics.py 9.2 KB

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