univar_statistics.py 8.0 KB

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