univar_statistics.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, where, extended, no_header, fs)
  7. (C) 2012-2013 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. :authors: Soeren Gebbert
  12. """
  13. from open_stds import *
  14. import grass.script as gscript
  15. ###############################################################################
  16. def print_gridded_dataset_univar_statistics(type, input, where, extended,
  17. no_header=False, fs="|"):
  18. """Print univariate statistics for a space time raster or raster3d dataset
  19. :param type: Must be "strds" or "str3ds"
  20. :param input: The name of the space time dataset
  21. :param where: A temporal database where statement
  22. :param extended: If True compute extended statistics
  23. :param no_header: Supress the printing of column names
  24. :param fs: Field separator
  25. """
  26. # We need a database interface
  27. dbif = SQLDatabaseInterfaceConnection()
  28. dbif.connect()
  29. sp = open_old_stds(input, type, dbif)
  30. rows = sp.get_registered_maps(
  31. "id,start_time,end_time", where, "start_time", dbif)
  32. if not rows:
  33. dbif.close()
  34. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  35. 'sp': sp.get_new_map_instance(None).get_type(),
  36. 'i': sp.get_id()})
  37. if no_header is False:
  38. string = ""
  39. string += "id" + fs + "start" + fs + "end" + fs + "mean" + fs
  40. string += "min" + fs + "max" + fs
  41. string += "mean_of_abs" + fs + "stddev" + fs + "variance" + fs
  42. string += "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  43. if extended is True:
  44. string += fs + "first_quartile" + fs + "median" + fs
  45. string += "third_quartile" + fs + "percentile_90"
  46. print string
  47. for row in rows:
  48. string = ""
  49. id = row["id"]
  50. start = row["start_time"]
  51. end = row["end_time"]
  52. flag = "g"
  53. if extended is True:
  54. flag += "e"
  55. if type == "strds":
  56. stats = gscript.parse_command("r.univar", map=id, flags=flag)
  57. elif type == "str3ds":
  58. stats = gscript.parse_command("r3.univar", map=id, flags=flag)
  59. if not stats:
  60. if type == "strds":
  61. gscript.warning(_("Unable to get statistics for raster map "
  62. "<%s>") % id)
  63. elif type == "str3ds":
  64. gscript.warning(_("Unable to get statistics for 3d raster map"
  65. " <%s>") % id)
  66. continue
  67. string += str(id) + fs + str(start) + fs + str(end)
  68. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  69. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  70. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  71. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  72. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  73. if extended is True:
  74. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  75. string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  76. print string
  77. dbif.close()
  78. ###############################################################################
  79. def print_vector_dataset_univar_statistics(input, twhere, layer, type, column,
  80. where, extended, no_header=False,
  81. fs="|"):
  82. """Print univariate statistics for a space time vector dataset
  83. :param input: The name of the space time dataset
  84. :param twhere: A temporal database where statement
  85. :param layer: The layer number used in case no layer is present
  86. in the temporal dataset
  87. :param type: options: point,line,boundary,centroid,area
  88. :param column: The name of the attribute column
  89. :param where: A temporal database where statement
  90. :param extended: If True compute extended statistics
  91. :param no_header: Supress the printing of column names
  92. :param fs: Field separator
  93. """
  94. # We need a database interface
  95. dbif = SQLDatabaseInterfaceConnection()
  96. dbif.connect()
  97. mapset = get_current_mapset()
  98. if input.find("@") >= 0:
  99. id = input
  100. else:
  101. id = input + "@" + mapset
  102. sp = dataset_factory("stvds", id)
  103. if sp.is_in_db(dbif) is False:
  104. dbif.close()
  105. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> not found") % {
  106. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  107. sp.select(dbif)
  108. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  109. twhere, "start_time", dbif)
  110. if not rows:
  111. dbif.close()
  112. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  113. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  114. string = ""
  115. if no_header is False:
  116. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  117. fs + "nmissing" + fs + "nnull" + fs
  118. string += "min" + fs + "max" + fs + "range"
  119. if type == "point" or type == "centroid":
  120. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  121. fs + "population_variance" + fs
  122. string += "population_coeff_variation" + fs + \
  123. "sample_stddev" + fs + "sample_variance" + fs
  124. string += "kurtosis" + fs + "skewness"
  125. if extended is True:
  126. string += fs + "first_quartile" + fs + "median" + fs + \
  127. "third_quartile" + fs + "percentile_90"
  128. print string
  129. for row in rows:
  130. id = row["name"] + "@" + row["mapset"]
  131. start = row["start_time"]
  132. end = row["end_time"]
  133. mylayer = row["layer"]
  134. flags = "g"
  135. if extended is True:
  136. flags += "e"
  137. if not mylayer:
  138. mylayer = layer
  139. stats = gscript.parse_command("v.univar", map=id, where=where,
  140. column=column, layer=mylayer,
  141. type=type, flags=flags)
  142. string = ""
  143. if not stats:
  144. gscript.warning(_("Unable to get statistics for vector map <%s>")
  145. % id)
  146. continue
  147. string += str(id) + fs + str(start) + fs + str(end)
  148. string += fs + str(stats["n"]) + fs + str(stats[
  149. "nmissing"]) + fs + str(stats["nnull"])
  150. if "min" in stats:
  151. string += fs + str(stats["min"]) + fs + str(
  152. stats["max"]) + fs + str(stats["range"])
  153. else:
  154. string += fs + fs + fs
  155. if type == "point" or type == "centroid":
  156. if "mean" in stats:
  157. string += fs + str(stats["mean"]) + fs + \
  158. str(stats["mean_abs"]) + fs + \
  159. str(stats["population_stddev"]) + fs + \
  160. str(stats["population_variance"])
  161. string += fs + str(stats["population_coeff_variation"]) + \
  162. fs + str(stats["sample_stddev"]) + fs + \
  163. str(stats["sample_variance"])
  164. string += fs + str(stats["kurtosis"]) + fs + \
  165. str(stats["skewness"])
  166. else:
  167. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  168. if extended is True:
  169. if "first_quartile" in stats:
  170. string += fs + str(stats["first_quartile"]) + fs + \
  171. str(stats["median"]) + fs + \
  172. str(stats["third_quartile"]) + fs + \
  173. str(stats["percentile_90"])
  174. else:
  175. string += fs + fs + fs + fs
  176. print string
  177. dbif.close()