def __getitem__(self, key): l=len(self) if type(key)==slice: st=key.start if st<0: st=l+st if st<0: st=0 elif st>l-1: return zeros((0, self.width), self.dtype) sp= key.stop if sp<0: sp=l+sp if sp<1: return zeros((0, self.width), self.dtype) ne=min(sp, l) ne=ne-st self.file.seek(4+4*self.width*st) a=fromstring(self.file.read(self.width*4*ne), self.dtype) if self.invert: a.byteswap() a=reshape(a, (ne, self.width)) else: if key<0: key=l+key self.file.seek(4+4*self.width*key) s=self.file.read(self.width*4) a=fromstring(s, self.dtype) if self.invert: a.byteswap() return a
def handle(self): hl=struct.unpack("<l", self.rfile.read(4))[0] h=cPickle.loads(self.rfile.read(hl)) a=fromstring(self.rfile.read(), "<f4") a=reshape(a, h['ashape']) del(h['ashape']) self.server.plot(a,h) self.wfile.write("ok")
def take(self, ai): '''Return an array containing all the rows specified in the index array ai. Ai may specify multiple occurances of the same index. This function is probably slower than numpy.take(self.toarray(), ai), but will use much less memory if ai is short and len(self) is large.''' ret=zeros((ai.shape[0],self.width), self.dtype) for i in ai: self.file.seek(4+4*self.width*i) s=self.file.read(self.width*4) ret[i,:]=fromstring(s, self.dtype) if self.invert: ret.byteswap() return ret
def getColumn(self, i): if i>=self.width: raise IndexError("Column index exceeds width") self.file.seek(0, 2) fl=self.file.tell() c=[] adv=self.width*4 pos=4+i*4 while pos<fl-4: self.file.seek(pos) c.append(self.file.read(4)) pos+=adv a=fromstring(''.join(c), self.dtype) if self.invert: a.byteswap() return a
def readMD(f, **kwargs): dats={} cat=f.read(16) while len(cat)==16: pl, hl, dl = struct.unpack("<IIQ", cat) path=f.read(pl) head=eval(f.read(hl)) dat=None if dl: ct=f.read(1) if ct in ['<', '>', '|', "{"]: ct=ct+f.read(6) dti, nd = struct.unpack("<3sI", ct) if dti.startswith("{"): dti=dti[1:] dti=dtype(dti) else: print "warning, old mdat. May not be platform portable" nd = struct.unpack("<I", f.read(4))[0] dti=dtype("<"+ct) sh=struct.unpack("<"+"Q"*nd, f.read(8*nd)) dat=f.read(dl) dat=fromstring(dat, dti) dti=dti.str if DTS!=dti[0]: dtil=dtype(DTS+dti[1:]) dat=dat.astype(dtil) dat=reshape(dat, sh) dats[path]=(dat, head) cat=f.read(16) try: url="file://%s" % (f.name,) except: try: url=f.geturl() except: url=None if kwargs.get('return_raw_hash'): return dats else: return hash2doc(dats, url)
def toarray(self): self.file.seek(4) a=fromstring(self.file.read(), self.dtype) if self.invert: a.byteswap() return reshape(a, (-1, self.width))