univar_statistics.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(), '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 == 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 == 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 <%s>") % id)
  62. elif type == "str3ds":
  63. gscript.warning(_("Unable to get statistics for 3d raster map <%s>") % id)
  64. continue
  65. string += str(id) + fs + str(start) + fs + str(end)
  66. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  67. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  68. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  69. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  70. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  71. if extended == True:
  72. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  73. string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  74. print string
  75. dbif.close()
  76. ###############################################################################
  77. def print_vector_dataset_univar_statistics(input, twhere, layer, type, column,
  78. where, extended, no_header=False, fs="|"):
  79. """Print univariate statistics for a space time vector dataset
  80. :param input: The name of the space time dataset
  81. :param twhere: A temporal database where statement
  82. :param layer: The layer number used in case no layer is present
  83. in the temporal dataset
  84. :param type: options: point,line,boundary,centroid,area
  85. :param column: The name of the attribute column
  86. :param where: A temporal database where statement
  87. :param extended: If True compute extended statistics
  88. :param no_header: Supress the printing of column names
  89. :param fs: Field separator
  90. """
  91. # We need a database interface
  92. dbif = SQLDatabaseInterfaceConnection()
  93. dbif.connect()
  94. mapset = get_current_mapset()
  95. if input.find("@") >= 0:
  96. id = input
  97. else:
  98. id = input + "@" + mapset
  99. sp = dataset_factory("stvds", id)
  100. if sp.is_in_db(dbif) == False:
  101. dbif.close()
  102. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> not found") % {
  103. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  104. sp.select(dbif)
  105. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  106. twhere, "start_time", dbif)
  107. if not rows:
  108. dbif.close()
  109. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  110. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  111. string = ""
  112. if no_header is False:
  113. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  114. fs + "nmissing" + fs + "nnull" + fs
  115. string += "min" + fs + "max" + fs + "range"
  116. if type == "point" or type == "centroid":
  117. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  118. fs + "population_variance" + fs
  119. string += "population_coeff_variation" + fs + \
  120. "sample_stddev" + fs + "sample_variance" + fs
  121. string += "kurtosis" + fs + "skewness"
  122. if extended == True:
  123. string += fs + "first_quartile" + fs + "median" + fs + \
  124. "third_quartile" + fs + "percentile_90"
  125. print string
  126. for row in rows:
  127. id = row["name"] + "@" + row["mapset"]
  128. start = row["start_time"]
  129. end = row["end_time"]
  130. mylayer = row["layer"]
  131. flags = "g"
  132. if extended == True:
  133. flags += "e"
  134. if not mylayer:
  135. mylayer = layer
  136. stats = gscript.parse_command("v.univar", map=id, where=where,
  137. column=column, layer=mylayer,
  138. type=type, flags=flags)
  139. string = ""
  140. if not stats:
  141. gscript.warning(_("Unable to get statistics for vector map <%s>") % id)
  142. continue
  143. string += str(id) + fs + str(start) + fs + str(end)
  144. string += fs + str(stats["n"]) + fs + str(stats[
  145. "nmissing"]) + fs + str(stats["nnull"])
  146. if "min" in stats:
  147. string += fs + str(stats["min"]) + fs + str(
  148. stats["max"]) + fs + str(stats["range"])
  149. else:
  150. string += fs + fs + fs
  151. if type == "point" or type == "centroid":
  152. if "mean" in stats:
  153. string += fs + str(stats["mean"]) + fs + \
  154. str(stats["mean_abs"]) + fs + \
  155. str(stats["population_stddev"]) + fs + \
  156. str(stats["population_variance"])
  157. string += fs + str(stats["population_coeff_variation"]) + \
  158. fs + str(stats["sample_stddev"]) + fs + \
  159. str(stats["sample_variance"])
  160. string += fs + str(stats["kurtosis"]) + fs + \
  161. str(stats["skewness"])
  162. else:
  163. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  164. if extended == True:
  165. if "first_quartile" in stats:
  166. string += fs + str(stats["first_quartile"]) + fs + \
  167. str(stats["median"]) + fs + \
  168. str(stats["third_quartile"]) + fs + \
  169. str(stats["percentile_90"])
  170. else:
  171. string += fs + fs + fs + fs
  172. print string
  173. dbif.close()