Go to the documentation of this file.00001 import numpy as np
00002 from scipy.sparse import lil_matrix
00003 from bisect import bisect_left
00004
00005 class PyCloud(lil_matrix):
00006
00007 def __init__(self, arg1, row_step, point_step, shape=None, dtype=None, copy=False):
00008 lil_matrix.__init__(self, arg1, shape=shape, dtype=self.PyPoint, copy=copy)
00009 self.nan = self.PyPoint((np.nan, np.nan, np.nan))
00010 self.format = 'lil'
00011 self.dtype = self.PyPoint
00012
00013
00014 def __getitem__(self, index):
00015 item = super(PyCloud, self).__getitem__(index)
00016 assert type(item) == self.PyPoint or item == 0, 'Expecting %s, got: %s: %s' % (self.dtype.name, str(type(item)), item)
00017
00018 return item.vals if type(item) == self.PyPoint else self.nan
00019
00020 def __setitem__(self, index, item):
00021
00022 if np.ma.extras.issequence(item):
00023 super(PyCloud, self).__setitem__(index, self.PyPoint(item))
00024 item = self.PyPoint(item)
00025 else:
00026 super(PyCloud, self).__setitem__(index, item)
00027
00028 class PyPoint(np.float64):
00029 name = "PyPoint"
00030
00031 def __new__(cls, vals):
00032 return np.float64.__new__(cls, np.nan)
00033
00034 def __init__(self, vals):
00035 np.float64.__init__(self)
00036 self.vals = vals
00037
00038 @classmethod
00039 def type(cls, x):
00040 if np.ma.extras.issequence(x):
00041 return cls(x)
00042 elif(type(x) == cls):
00043 return x
00044 raise TypeError('Unable to convert value (%s) to dtype [%s]' % (x,cls.name))
00045
00046 if __name__ == '__main__':
00047 l = PyCloud((100,100), 8, 8)
00048 l[5,5] = (1,2,3)
00049 assert (1,2,3) == l[5,5], 'Wrong value came out'
00050 assert np.isnan(l[5,7]), 'Unassigned index not nan'