def generate_jacobian_lut(luts, axes): """ generates a jacobian-lut at exactly the same positions (axes points) as the original luts """ func = l2f.lut2func(luts, axes) if isinstance(luts, tuple): ny = len(luts) elif isinstance(luts, np.ndarray): ny = luts.shape[-1] else: print "Luts is not of right type" return None nx = len(axes) dimi = np.array([len(ax) for ax in (axes)]) dimj = [len(ax) for ax in (axes)] dimj.append(ny * nx) out = np.zeros(dimj) a = np.zeros(len(axes)) b = np.zeros(len(axes)) a = np.array([ax.min() for ax in axes]) b = np.array([ax.max() for ax in axes]) def njac(x): return numerical_jacoby(a, b, x, func, nx, ny, delta=0.01) print ("Filling the jacobian ...") progress = wln.wln(dimi.prod(), "Progress", modulo=10000) for i in np.arange(dimi.prod()): idx = np.unravel_index(i, dimi) wo = np.array([ax[i] for i, ax in zip(idx, axes)]) out[idx] = njac(wo).ravel() progress.event() print ("") print ("Done") return {"lut": out, "ny": ny, "nx": nx, "axes": axes}
def generate_jacobian_lut(luts, axes): ''' generates a jacobian-lut at exactly the same positions (axes points) as the original luts ''' func = l2f.lut2func(luts, axes) if isinstance(luts, tuple): ny = len(luts) elif isinstance(luts, np.ndarray): ny = luts.shape[-1] else: print 'Luts is not of right type' return None nx = len(axes) dimi = np.array([len(ax) for ax in (axes)]) dimj = [len(ax) for ax in (axes)] dimj.append(ny * nx) out = np.zeros(dimj) a = np.zeros(len(axes)) b = np.zeros(len(axes)) a = np.array([ax.min() for ax in axes]) b = np.array([ax.max() for ax in axes]) def njac(x): return numerical_jacoby(a, b, x, func, nx, ny, delta=0.01) print('Filling the jacobian ...') progress = wln.wln(dimi.prod(), 'Progress', modulo=10000) for i in np.arange(dimi.prod()): idx = np.unravel_index(i, dimi) wo = np.array([ax[i] for i, ax in zip(idx, axes)]) out[idx] = njac(wo).ravel() progress.event() print('') print('Done') return {'lut': out, 'ny': ny, 'nx': nx, 'axes': axes}
days = int(rem)/86400 if days >= 1: days=str(days)+"d " else: days="" # Convert to nice string rem = " Remaining: " +days+time.strftime("%H:%M:%S",time.gmtime(rem)) #test 0057/1000 1.12 s (1.31 s +- 0.25 s) Remaining: 00:20:39 out = out + rem print self.name, out ,' \r', sys.stdout.flush() if __name__ == '__main__': import wln import time import random nitter = 40 tt = 2 a=wln.wln(nitter,name='name',modulo=1) for i in xrange(nitter): # do something slow and often ttt=tt*random.random() time.sleep(ttt) a.event()
def generate_jacobian_lut(luts, axes, xidx=None): ''' generates a jacobian-lut at exactly the same positions (axes points) as the original luts xidx gives the positions, where the jacobian will be calulated, the other positions are ignored (e.g. geometry in LUTs ) ''' func = l2f.lut2func(luts, axes) if isinstance(luts, tuple): ny = len(luts) dtype = luts[0].dtype elif isinstance(luts, np.ndarray): ny = luts.shape[-1] dtype = luts.dtype else: print('Lut is not of right type') return None if xidx is None: nx = len(axes) else: nx = len(xidx) dimi = np.array([len(ax) for ax in (axes)]) dimj = [len(ax) for ax in (axes)] dimj.append(ny * nx) out = np.zeros(dimj, dtype=dtype) a = np.zeros(len(axes)) b = np.zeros(len(axes)) a = np.array([ax.min() for ax in axes]) b = np.array([ax.max() for ax in axes]) def njac(x, dx=None): return numerical_jacoby(a, b, x, func, nx, ny, delta=0.01, xidx=xidx, dx=dx) def optimal_dx(i, ax): if i == 0: dx = np.abs(ax[1] - ax[0]) / 2. elif i == ax.size - 1: dx = np.abs(ax[-1] - ax[-2]) / 2. else: dx = min(np.abs(ax[i + 1] - ax[i]), np.abs(ax[i] - ax[i - 1])) / 2. return dx print('Filling the jacobian ...') progress = wln(dimi.prod(), 'Progress', modulo=10000) for i in np.arange(dimi.prod()): idx = np.unravel_index(i, dimi) wo = np.array([ax[i] for i, ax in zip(idx, axes)]) dx = np.array([optimal_dx(i, ax) for i, ax in zip(idx, axes)]) out[idx] = njac(wo, dx).ravel() progress.event() print('') print('Done') return {'lut': out, 'ny': ny, 'nx': nx, 'axes': axes, 'xidx': xidx}
rem = (self.nitter - self.count) * self.mean # check if more than a day days = int(rem) / 86400 if days >= 1: days = str(days) + "d " else: days = "" # Convert to nice string rem = " Remaining: " + days + time.strftime( "%H:%M:%S", time.gmtime(rem)) #test 0057/1000 1.12 s (1.31 s +- 0.25 s) Remaining: 00:20:39 out = out + rem print self.name, out, ' \r', sys.stdout.flush() if __name__ == '__main__': import wln import time import random nitter = 40 tt = 2 a = wln.wln(nitter, name='name', modulo=1) for i in xrange(nitter): # do something slow and often ttt = tt * random.random() time.sleep(ttt) a.event()