/modules/hs/analysis/utils.py

https://github.com/opevans/Sahana-Timeline
Python | 192 lines | 88 code | 26 blank | 78 comment | 8 complexity | 95813e0431c89be43936c068f1853bca MD5 | raw file
  1. """
  2. Healthscapes Geolytics Module
  3. @author: Nico Preston <nicopresto@gmail.com>
  4. @author: Colin Burreson <kasapo@gmail.com>
  5. @author: Zack Krejci <zack.krejci@gmail.com>
  6. @copyright: (c) 2010 Healthscapes
  7. @license: MIT
  8. Permission is hereby granted, free of charge, to any person
  9. obtaining a copy of this software and associated documentation
  10. files (the "Software"), to deal in the Software without
  11. restriction, including without limitation the rights to use,
  12. copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the
  14. Software is furnished to do so, subject to the following
  15. conditions:
  16. The above copyright notice and this permission notice shall be
  17. included in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. OTHER DEALINGS IN THE SOFTWARE.
  26. """
  27. import rpy2.rinterface as r
  28. class R:
  29. _running = False
  30. _library = None
  31. _execute = None
  32. @staticmethod
  33. def _start ():
  34. if not R._running:
  35. R._running = True
  36. r.initr ()
  37. R._execute = r.globalEnv.get
  38. R._library = R._execute ('library')
  39. @staticmethod
  40. def _toVector (arg):
  41. return r.FloatSexpVector(arg)
  42. @staticmethod
  43. def importLibrary (string):
  44. R._start ()
  45. vector = r.StrSexpVector([string])
  46. R._library (vector)
  47. @staticmethod
  48. def function (fname, *args):
  49. R._start ()
  50. Rargs = []
  51. for a in args:
  52. v = R._toVector (a)
  53. Rargs.append (v)
  54. return R._execute (fname)(*Rargs)
  55. class Vector:
  56. def __init__ (self, x, y):
  57. self.x = x
  58. self.y = y
  59. """class Keygen:
  60. _lookup = []
  61. @staticmethod
  62. def gen (name):
  63. current = len(Keygen._lookup)
  64. Keygen.append (name)
  65. return current
  66. @staticmethod
  67. def get (id):
  68. return Keygen.lookup[id]"""
  69. class Key:
  70. def __init__ (self, data):
  71. self._data = data
  72. def decode (self):
  73. return self._data
  74. def __str__ (self):
  75. return str (self._data)
  76. def keygen (data):
  77. return Key (data)
  78. def decode (key):
  79. return key.decode ()
  80. groupCounter = 0
  81. def Group (arg=None):
  82. global groupCounter
  83. if arg is None:
  84. string = 'Group ' + str (groupCounter)
  85. else:
  86. string = arg
  87. groupCounter += 1
  88. return keygen (string)
  89. class BoundingBox (list):
  90. def __init__ (self, maxPair, minPair):
  91. self.append (Vector(maxPair[0], maxPair[1]))
  92. self.append (Vector(minPair[0], maxPair[1]))
  93. self.append (Vector (minPair[0], minPair[1]))
  94. self.append (Vector(maxPair[0], minPair[1]))
  95. def contains (self, point):
  96. if point.x > self[0].x:
  97. return False
  98. if point.x < self[2].x:
  99. return False
  100. if point.y > self[0].y:
  101. return False
  102. if point.y < self[2].y:
  103. return False
  104. return True
  105. """class Dictionary (dict):
  106. def __init__ (self):
  107. self._keys = []
  108. #self._sorted = True
  109. dict.__init__(self)
  110. def update (self, pairs):
  111. try:
  112. for key, value in pairs.iteritems ():
  113. if not self.has_key (key):
  114. self._keys.append (key)
  115. except AttributeError:
  116. for pair in pairs:
  117. if not self.has_key (pair[0]):
  118. self._keys.append (pair[0])
  119. #for key in pairs:
  120. # print key
  121. # try:
  122. # self[key]
  123. # except KeyError:
  124. # self._keys.append (key)
  125. #self._sorted = False
  126. dict.update (self, pairs)
  127. def __iter__ (self):
  128. return DictIterator (self, iter(self._keys))
  129. class DictIterator:
  130. def __init__ (self, dictionary, listIter):
  131. self._dictionary = dictionary
  132. self._listIter = listIter
  133. def __iter__ (self):
  134. return self
  135. def next (self):
  136. key = self._listIter.next ()
  137. return (key, self._dictionary[key])
  138. class DefaultDictionary (Dictionary):
  139. def __init__ (self, default):
  140. self.default = default
  141. Dictionary.__init__ (self)
  142. def __getitem__ (self, item):
  143. try:
  144. return dict.__getitem__ (self, item)
  145. except KeyError:
  146. if not self.default is None:
  147. return self.default
  148. else:
  149. raise KeyError ()"""