Commit 120462aa authored by Martin Reinecke's avatar Martin Reinecke

working, but too slowly

parent 3934ca0c
......@@ -21,7 +21,7 @@ if __name__ == "__main__":
ends = np.random.random((2, n))
return starts, ends
nlos = 100
nlos = 1000
starts, ends = make_random_los(nlos)
R = ift.library.LOSResponse(s_space, starts=starts, ends=ends)
......
......@@ -10,37 +10,35 @@ from .. import dobj
class Pixellister(object):
Rmax = np.sqrt(3.)*0.5
def __init__(self, start, end):
self._start = start
self._end = end
self._dir = end - start
self._dirlen = np.sqrt(np.sum(self._dir*self._dir))
self._dirlen = np.linalg.norm(self._dir)
self._dir *= 1./self._dirlen
def _losdist(self, pos):
t1 = pos - self._start
dotpr = np.sum(self._dir*t1)
dotpr = np.dot(self._dir, t1)
if dotpr < 0:
return np.sqrt(np.sum(t1*t1))
return np.linalg.norm(t1)
elif dotpr <= self._dirlen:
t2 = self._start + self._dir*dotpr
d1 = pos-t2
return np.sqrt(np.sum(d1*d1))
return np.linalg.norm(pos - self._start - self._dir*dotpr)
else:
d3 = pos-self._end
return np.sqrt(np.sum(d3*d3))
return np.linalg.norm(pos-self._end)
def build_pixellist(self, lo, hi, pixels):
Rmax=5.
diff = hi-lo
if np.all(diff==1):
if np.all(diff == 1):
dist = self._losdist(lo)
if dist < Rmax:
if dist < self.Rmax:
pixels.append((lo, dist))
return
mid = (lo+hi-1.)/2.
mid = (lo+hi-1.)*0.5
t1 = mid-lo
if self._losdist(mid) < np.sqrt(np.sum(t1*t1)) + Rmax:
if self._losdist(mid) < np.linalg.norm(t1) + self.Rmax:
dim = np.argmax(diff)
imid = lo[dim]+diff[dim]//2
lmid = lo.copy()
......@@ -50,6 +48,7 @@ class Pixellister(object):
self.build_pixellist(lo, hmid, pixels)
self.build_pixellist(lmid, hi, pixels)
def _comp_traverse(start, end, shp):
nlos = start.shape[1]
inc = np.full(len(shp), 1)
......@@ -59,10 +58,10 @@ def _comp_traverse(start, end, shp):
for i in range(nlos):
print i
pixels = []
pl = Pixellister(start[:,i], end[:,i])
pl = Pixellister(start[:, i], end[:, i])
pl.build_pixellist(np.array(shp)*0, np.array(shp), pixels)
pix1 = [np.sum(inc*p[0]) for p in pixels]
wgt = [np.exp(-p[1]*p[1]) for p in pixels]
pix1 = np.array([np.sum(inc*p[0]) for p in pixels])
wgt = np.array([np.exp(-p[1]*p[1]) for p in pixels])
out[i] = (pix1, wgt)
return out
......@@ -141,19 +140,20 @@ class LOSResponse(LinearOperator):
cnt = 0
for i in w_i:
nval = len(i[1])
ilos[ofs:ofs+nval] = cnt
iarr[ofs:ofs+nval] = i[0]
xwgt[ofs:ofs+nval] = i[1]
fullidx = np.unravel_index(i[0], self._local_shape)
tmp = np.zeros(nval, dtype=np.float64)
fct = 1.
for j in range(ndim):
tmp += (fullidx[j]//boxsz)*fct
fct *= self._local_shape[j]
tmp += cnt/float(nlos)
tmp += iarr[ofs:ofs+nval]/float(nlos*npix)
pri[ofs:ofs+nval] = tmp
ofs += nval
if nval > 0:
ilos[ofs:ofs+nval] = cnt
iarr[ofs:ofs+nval] = i[0]
xwgt[ofs:ofs+nval] = i[1]
fullidx = np.unravel_index(i[0], self._local_shape)
tmp = np.zeros(nval, dtype=np.float64)
fct = 1.
for j in range(ndim):
tmp += (fullidx[j]//boxsz)*fct
fct *= self._local_shape[j]
tmp += cnt/float(nlos)
tmp += iarr[ofs:ofs+nval]/float(nlos*npix)
pri[ofs:ofs+nval] = tmp
ofs += nval
cnt += 1
xtmp = np.argsort(pri)
ilos = ilos[xtmp]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment