def load(self): # self.storage_2d_losvd.load() #solution = self.solution_known.load() #self.true_rho2d = numpy.tensordot(self.storage_2d_losvd.masses, solution, axes=[(0,),(0,)]) Rborders = self.storage_2d_losvd.Rborders Rmax = self.storage_2d_losvd.Rborders[-1] #print "Rmax", Rmax #dsa N = len(Rborders) - 1 self.counts = array([ self.light_profile.cumdensityR(Rborders[i], Rborders[i + 1], M=1.) for i in range(N) ]) #print self.counts, sum(self.counts), len(self.counts) #dsa self.mass_matrixN = self.storage_2d_losvd.masses * 1. / self.storage_2d_losvd.delta_R self.totalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) #self.counts = self.true_rho2d self.counts /= sum(self.counts) self.counts *= self.N self.optimizer = gdfast_schw.OptimizationMatrixN( self.mass_matrixN, self.counts, self.totalmass_matrix) self.totalmass = self.light_profile.cumdensityR(0, Rmax, M=1.) #print self.totalmass self.mass_vector = sum(self.storage_2d_losvd.masses, axis=1) #print "mass vector", self.mass_vector self.optimizer_extra = gdfast_schw.OptimizationNormalizeMass( self.mass_vector, self.totalmass, 0.0001)
def load(self): self.sub_scope_true.load() self.storage_2d_losvd_true = self.sub_scope_true.subscope[ "storage_2d_losvd"] self.storage_2d_losvd_true.load() self.storage_2d_losvd.load() solution = self.solution_known.load() self.true_rho2d = numpy.tensordot(self.storage_2d_losvd_true.masses, solution, axes=[(0, ), (0, )]) self.mass_matrixN = self.storage_2d_losvd.masses * 1. / self.storage_2d_losvd.delta_R self.totalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) self.counts = self.true_rho2d self.counts /= sum(self.counts) self.counts *= self.N self.optimizer = gdfast_schw.OptimizationMatrixN( self.mass_matrixN, self.counts, self.totalmass_matrix) Rmax = self.storage_2d_losvd.Rborders[-1] self.totalmass = self.light_profile.cumdensityR(0, Rmax, M=1.) #print self.totalmass self.mass_vector = sum(self.storage_2d_losvd.masses, axis=1) #print "mass vector", self.mass_vector self.optimizer_extra = gdfast_schw.OptimizationNormalizeMass( self.mass_vector, self.totalmass, 0.0001)
def load(self): self.sub_scope_true.load() self.storage_2d_losvd_true = self.sub_scope_true.subscope[ "storage_2d_losvd"] self.storage_2d_losvd_true.load() self.storage_2d_losvd.load() solution = self.solution_known.load() self.true_rho2d = numpy.tensordot(self.storage_2d_losvd_true.masses, solution, axes=[(0, ), (0, )]) self.mass_matrixN = self.storage_2d_losvd.masses * 1. / self.storage_2d_losvd.delta_R self.totalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) self.counts = self.true_rho2d self.counts /= sum(self.counts) self.counts *= self.N self.optimizer = gdfast_schw.OptimizationMatrixN( self.mass_matrixN, self.counts, self.totalmass_matrix)
def fit(self, target, vectors): #x, error = scipy.optimize.nnls(vectors.T, target) #print x, #return x #mask = target > 0 print target.shape, vectors.shape if 0: mass_matrix = vectors[:, mask] * 1 totalmass_matrix = sum(vectors[:, mask], axis=1) * 1 else: mass_matrix = vectors * 1 totalmass_matrix = sum(vectors, axis=1) * 1 mass_matrix = array(mass_matrix, copy=True, order='C') totalmass_matrix = array(totalmass_matrix, copy=True, order='C') target = array(target, copy=True, order='C') print totalmass_matrix.shape, mass_matrix.shape print totalmass_matrix.dtype, mass_matrix.dtype from mab.gd import gdfast_schw import pyublas #opt_matrix_mass = gdfast_schw.OptimizationMatrix(mass_matrix, totalmass_matrix* 0 + 1) opt_matrix_mass = gdfast_schw.OptimizationMatrixN( mass_matrix, target, totalmass_matrix) #opt_matrix_mass = gdfast_schw.OptimizationMatrix(pyublas.why_not(mass_matrix), pyublas.why_not(totalmass_matrix)) weights = ones(vectors.shape[0]) x = weights logL1 = opt_matrix_mass.logp(x) result = dot(x, vectors) logL2 = sum(target * log(result)) #import pdb #pdb.set_trace() def f_(weight): weight = 10**weight result = dot(weight, vectors) chisq = sum(((target - result)**2)) return chisq def f(u): #x = weight global last_fitweights x = weight = exp(u) #weight = array(weight, copy=True, order='C') #result = dot(weight, vectors) #logL = sum(target[mask] * log(result[mask])) #print logL logL = opt_matrix_mass.logp(x) grad = weight * 0 opt_matrix_mass.dlogpdx(x, grad) grad = grad * x print logL, grad[0:10] last_fitweights = u return -logL, -grad weights = ones(vectors.shape[0]) bounds = None try: fitweights = scipy.optimize.fmin_l_bfgs_b(f, log(weights), None, bounds=bounds, approx_grad=False, iprint=1, factr=1e-2, maxfun=200000)[0] except KeyboardInterrupt: fitweights = exp(last_fitweights) else: fitweights = exp(fitweights) print fitweights return fitweights
def solve(self, scope): stars = self.observation.stars self.logger.info("using %d stars/observations" % len(stars)) #stars_inrange = stars.filter(lambda star: self.storage_2d.aperture.inrange(star.xi, star.eta)) stars_inrange = stars.filter(lambda star: self.storage_2d_losvd. aperture.inrange(star.xi, star.eta)) self.logger.info("stars in aperture range : %d" % len(stars_inrange)) self.logger.info("stars outside aperture range: %d" % (len(stars) - len(stars_inrange))) vmax = self.storage_2d_losvd.vmax #print "vmax", vmax delta_v = 2 * vmax / self.storage_2d_losvd.Nv #print "res", delta_v for star in stars: star.aperture_index = self.storage_2d_losvd.aperture.findindex( star.xi, star.eta) losvds = self.storage_2d_losvd.losvds stars_invrange = stars.filter(lambda star: abs(star.vlos) < vmax) self.logger.info("stars in velocity range : %d" % len(stars_invrange)) self.logger.info("stars outside velocity range: %d" % (len(stars) - len(stars_invrange))) stars = stars_invrange sigma_v = 2.01 numpy.random.seed(8) for star in stars: star.vlos = star.vlos_true # + numpy.random.normal(0, sigma_v) #star.vlos = star.vlos_true + numpy.random.normal(0, sigma_v) #print star.vlos, vmax, self.storage_2d_losvd.Nv star.v_index = int( ((star.vlos + vmax) / (2 * vmax)) * self.storage_2d_losvd.Nv) outlier = True for losvd in losvds: if losvd[star.v_index, star.aperture_index] != 0: outlier = False break star.is_outlier = outlier stars_no_outlier = stars.filter(lambda star: not star.is_outlier) self.logger.info("non-outlier stars : %d" % len(stars_no_outlier)) self.logger.info("outlier stars : %d" % (len(stars) - len(stars_no_outlier))) Rborders = arange(self.storage_2d_losvd.NR + 1) / ( 0.0 + self.storage_2d_losvd.NR) * (self.storage_2d_losvd.Rmax) R1s = Rborders[0:-1] R2s = Rborders[1:] dRs = R2s - R1s delta_R = R2s[0] - R1s[0] assert all(abs(dRs - delta_R) < 1e-10), "no constant dR" #print Rborders self.rho2d_target = array([ self.light_model.cumdensityR(R1, R2, M=1.) for R1, R2 in zip(R1s, R2s) ]) rho2ds = sum(losvds, axis=1) rho2dmatrix = sum(losvds, axis=1) rho3dmatrix = self.storage_3d.moments3d[:, 0, :] #rho2dmatrix = self.storage_2d_m0.moments[:,0,:] r1s = self.storage_3d.rborders[:-1] r2s = self.storage_3d.rborders[1:] delta_r = r2s[0] - r1s[0] #R1s = self.storage_2d.rborders[:-1] #R2s = self.storage_2d.rborders[1:] self.rho3d_target = array([ self.light_model.cumdensityr(r1, r2, M=1.) for r1, r2 in zip(r1s, r2s) ]) #for i in range(losvds.shape[0]): #for j in range(losvds.shape[1]): # print i, sum(losvds[i]), for i in range(losvds.shape[0]): #print sum(losvds[i]) for j in range(losvds.shape[2]): #dens = sum(losvds[i,:,j]) #if dens > 0: #print self.rho2d_target.shape #print losvds.shape #losvds[i,j,:] /= self.rho2d_target #losvds[i,:,j] = scipy.ndimage.gaussian_filter(losvds[i,:,j], sigma_v/delta_v, mode='constant') pass losvds[i] = scipy.ndimage.gaussian_filter( losvds[i], [sigma_v / delta_v, 0.51]) losvds[i] /= (delta_v * delta_R) #print #print losvds.shape, delta_v, delta_R, Rborders[0], Rborders[-1] #print Rborders #for i in range(losvds.shape[0]): #for j in range(losvds.shape[1]): # print i, sum(losvds[i]*delta_v*delta_R), v_indices = [star.v_index for star in stars] aperture_indices = [star.aperture_index for star in stars] #print losvds.shape pmatrix = array( list((losvds / (self.rho2d_target / delta_R))[:, v_indices, aperture_indices])) pmatrix = array(list((losvds)[:, v_indices, aperture_indices])) pmatrix = pmatrix * 1. #print pmatrix.shape rho2d_error = self.rho2d_target.max() * 0.000001 * 0.5 * 0.1 error_x = 1e-3 if 1: filename = os.path.join(self.modelpath, "df/orbitweights_tang.npy") orbitweights = load(filename) c = orbitweights.flatten() c /= sum(c) x = c xtrue = x if 0: self.x0 = x self.true_losvd = numpy.tensordot(self.storage_2d_losvd.losvds, c, axes=[(0, ), (0, )]) self.true_rho2d = numpy.tensordot(self.storage_2d_losvd.masses, c, axes=[(0, ), (0, )]) #self.true_rho2d = numpy.tensordot(rho2ds, c, axes=[(0,),(0,)]) self.true_rho3d = numpy.tensordot(rho3dmatrix, c, axes=[(0, ), (0, )]) filename = os.path.join(self.modelpath, "df/losvd_tang.npy") save(filename, self.true_losvd) filename = os.path.join(self.modelpath, "df/masses_tang.npy") save(filename, self.true_rho2d) dsa if 0: #graph(self.true_rho3d) #graph(self.rho3d_target, color="red") #avg = graph((self.true_rho3d - self.rho3d_target) / self.rho3d_target.max(), color="red") draw() #import pdb; pdb.set_trace() else: filename = os.path.join(self.modelpath, "df/losvd_tang.npy") self.true_losvd = load(filename) filename = os.path.join(self.modelpath, "df/masses_tang.npy") self.true_rho2d = load(filename) self.true_losvd = scipy.ndimage.gaussian_filter( self.true_losvd, [sigma_v / delta_v, 0.51]) self.true_losvd /= (delta_v * delta_R) debug = False if 0: filename = os.path.join( self.dirname, "results/orbitweights" + self.postfix + ".npy") x = numpy.load(filename) logger.info("loading orbitweights %s" % filename) else: x = x * 0 + 1. x = random.random(len(x)) x /= sum(x) u = log(x) #print rho2dmatrix.shape rho3dmatrix = rho3dmatrix * 1 #rhoerror = maximum(self.rho3d_target*rho2d_error, self.rho3d_target.max() * 0.001) rhoerror = self.rho3d_target * rho2d_error s = self.rho3d_target / self.rho3d_target.max() rhoerror = self.rho3d_target.max() * 0.05 * maximum( 0.1, s) + self.rho3d_target * 0 rhoerror = self.rho3d_target.max() * 0.07 * maximum( 0.1 / 7, s) + self.rho3d_target * 0 rhoerror = maximum( self.rho3d_target.max() * 0.01 * 1.5, self.rho3d_target * 0.01 * 2) + self.rho3d_target * 0 #rhoerror = maximum(rhoerror*1e-4, rho2d_error) #self.opt = gdfast_schw.OptimizationProblemSchw(pmatrix, rho3dmatrix, x, self.rho3d_target, rhoerror, error_x, True, False, True) fit_mass_3d = False #fit_mass_3d = True if fit_mass_3d: mass_matrix = rho3dmatrix mass_target = self.rho3d_target mass_error = rhoerror else: rhoerror = maximum(self.rho2d_target.max() * 0.01 * 0.05, self.rho2d_target * 0.001) # + self.rho3d_target * 0 mass_matrix = rho2dmatrix mass_target = self.rho2d_target mass_error = rhoerror entropy_scale = 1e-20 self.opt = gdfast_schw.OptimizationProblemSchw(pmatrix, mass_matrix, x, mass_target, mass_error, error_x, entropy_scale, True, True, True) #print "true L?", self.opt.likelihood(log(xtrue)) self.opt_kin = gdfast_schw.OptimizationProblemSchw( pmatrix, mass_matrix, x, mass_target, mass_error, error_x, entropy_scale, True, False, False) self.opts = [ gdfast_schw.OptimizationProblemSchw(pmatrix, mass_matrix, x, mass_target, mass_error, error_x, 0, True, False, False), gdfast_schw.OptimizationProblemSchw(pmatrix, mass_matrix, x, mass_target, mass_error, error_x, 0, False, True, False), gdfast_schw.OptimizationProblemSchw(pmatrix, mass_matrix, x, mass_target, mass_error, error_x, 0, False, False, True), gdfast_schw.OptimizationProblemSchw(pmatrix, mass_matrix, x, mass_target, mass_error, error_x, entropy_scale, False, False, False), ] debug = False #debug = True if 1: #x = numpy.load("xlast.npy") #print dir(self.light_model) N = 250000 light_profile = self.light_model.light_profile rs = light_profile.sample_r(N=N, rmax=100.) costheta = numpy.random.random(N) * 2 - 1 phi = numpy.random.random(N) * 2 * pi eta = numpy.random.random(N) * 2 * pi theta = numpy.arccos(costheta) #sintheta = numpy.sqrt(1-costheta**2) sintheta = numpy.sin(theta) #print r.shape, sintheta.shape, phi.shape, len(dt) xp = x x = rs * sintheta * numpy.cos(phi) y = rs * sintheta * numpy.sin(phi) Rs = sqrt(x**2 + y**2) x = xp #ps = self. Rs = Rs[Rs < 1.5] rs = rs[rs < 1.5] #rs = rs[rs>0.1] #normal = scipy.integrate.quad(lambda R: light_profile.densityR(R,M=1.)*2*pi*R, 0, 1.5)[0] normal = scipy.integrate.quad( lambda r: light_profile.densityr(r, M=1.) * 4 * pi * r**2, 0, 1.5)[0] if debug: print "normal", normal #normal = 1. if fit_mass_3d: ps = [ log( light_profile.densityr(r, M=1.) * 4 * pi * r**2 / normal) for r in rs ] else: ps = [ log(light_profile.densityR(R, M=1.) * 2 * pi * R / normal) for R in Rs ] N = len(ps) if debug: print N print "tot p", sum(ps) print "mean p", mean(ps) print rho3dmatrix.shape if fit_mass_3d: mass_indices = [int(r / 1.5 * 100) for r in rs] mass_matrix = rho3dmatrix[:, mass_indices] * 1. / delta_r mass_matrixN = rho3dmatrix * 1. / delta_r totalmass_matrix = sum(rho3dmatrix, axis=1) ptotalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) counts, bins = numpy.histogram(rs, 100, [0, 1.5], new=True) else: mass_indices = [int(R / 1.5 * 30) for R in Rs] mass_matrix = self.storage_2d_losvd.masses[:, mass_indices] * 1. / delta_R mass_matrixN = self.storage_2d_losvd.masses * 1. / delta_R totalmass_matrix = ptotalmass_matrix = sum( self.storage_2d_losvd.masses, axis=1) counts, bins = numpy.histogram(Rs, 30, [0, 1.5]) counts /= sum(counts) counts = counts / 2000 if debug: print "2d, delta_R", delta_R #mass = dot(self.storage_2d_losvd.masses.T, xtrue) if debug: print "total 3d", sum(dot(rho3dmatrix.T, xtrue)) print "total 2d", sum( dot(self.storage_2d_losvd.masses.T, xtrue)) print "normal check", dot(xtrue, totalmass_matrix) opt_matrix_mass = gdfast_schw.OptimizationMatrix( mass_matrix, totalmass_matrix) counts = array(counts).astype(float64) # * 1. #print counts, sum(counts) rho3dmatrix = rho3dmatrix * 1. #print "-->", rho3dmatrix.shape, counts.shape #print rho3dmatrix.dtype, counts.dtype counts = self.true_rho2d counts /= sum(counts) counts *= 200000 opt_matrix_massN = gdfast_schw.OptimizationMatrixN( mass_matrixN, counts, totalmass_matrix) if debug: print "logp", opt_matrix_mass.logp( xtrue), opt_matrix_mass.logp(x) print "logp", opt_matrix_massN.logp( xtrue), opt_matrix_massN.logp(x) print opt_matrix_mass.logp(xtrue) / N print sum(self.rho3d_target) #print (self.rho3d_target-mass)/self.rho3d_target #print "x =", x, sum(x) if 0: box() #mask = (rho3dmatrix/delta_r) > 1 #rho3dmatrix[mask] = 1000 I = rho3dmatrix * 1. I /= I.max() I = log10(I) I[I < -6] = -6 indexedimage(I) draw() diff = log(dot(x, mass_matrix)) - log(dot(xtrue, mass_matrix)) indices = argsort(diff) #import pdb #pdb.set_trace() #sysdsa if 1: #x = xtrue #print "sum(x)", sum(x) opt_matrix_kin = gdfast_schw.OptimizationMatrix( pmatrix, ptotalmass_matrix) counts_kin, binsx, biny = numpy.histogram2d( [star.v_index for star in stars], [star.aperture_index for star in stars], bins=[30, 30], range=[(0, 30), (0, 30)]) if 0: counts_kin2 = numpy.zeros((30, 30)) for star in stars: counts_kin2[star.v_index, star.aperture_index] += 1 mozaic(2, 2, box) indexedimage(counts_kin) select(0, 1) indexedimage(counts_kin2) draw() mask = counts_kin > 0 counts_kin = counts_kin[mask] counts_kin = counts_kin * 1. pmatrixN = losvds[:, mask] #debug = True if 1: pmatrixN = losvds * 1.0 pmatrixN = pmatrixN.reshape((pmatrixN.shape[0], -1)) * 1. counts_kin = self.true_losvd * 1. counts_kin = counts_kin.reshape(-1) * 1. counts_kin /= sum(counts_kin) counts_kin *= 2000 #@import pdb #pdb.set_trace() if debug: print "%d versus %d speedup: %f" % (pmatrixN.shape[1], len(stars), len(stars) * 1. / pmatrixN.shape[1]) #pmatrixN = array(list((losvds/(self.rho2d_target/delta_R))[:,v_indices, aperture_indices])) #pmatrix = array(list((losvds)[:,v_indices, aperture_indices])) pmatrixN = pmatrixN * 1.0 #print sum(counts_kin == 0) opt_matrix_kinN = gdfast_schw.OptimizationMatrixN( pmatrixN, counts_kin, ptotalmass_matrix) for k in [pmatrixN, counts_kin, ptotalmass_matrix]: print k.min(), k.max(), k.sum(), k.std() dsa opt_norm = gdfast_schw.OptimizationNormalize(1. - .000001, 0.001) opt_entropy = gdfast_schw.OptimizationEntropy(1.e-2) opt = self.opt_kin u = log(x) if debug: for i in [0, 1]: x1 = x * 1. x2 = x * 1. dx = 1e-8 x2[i] += dx grad = x * 0 for opt in [ opt_matrix_kin, opt_matrix_kinN, opt_entropy ]: #, opt_matrix_mass, opt_matrix_massN, opt_norm]: grad = x * 0 print u.shape, grad.shape opt.dlogpdx(x, grad) #sys.exit(0) w1 = opt.logp(x1) w2 = opt.logp(x2) print "w", opt.logp(x) print "grad", grad[i] print "grad", (w2 - w1) / dx #/x[i] print print #opt_matrix_kin.dlogpdx(x, grad) #grad *= x #print "grad3", grad[i] #print "logp", opt.likelihood(u) #print "logp", opt_matrix_kin.logp(x) print sys.exit(0) #x = x * 0 + 1 #x /= sum(x) global calls calls = 0 debug = False debug = True opts = [opt_matrix_kinN, opt_matrix_massN, opt_norm] def f_and_g(x): global calls #if calls > 10: # numpy.save("xlast.npy", x) # dsa #calls += 1 if 1: grad = x * 0 logp = opt_matrix_kinN.logp(x)*1 +\ opt_matrix_massN.logp(x)*0 +\ opt_norm.logp(x) * 1 #+\ #opt_entropy.logp(x) * 1. opt_matrix_kinN.dlogpdx(x, grad) #opt_matrix_massN.dlogpdx(x, grad) opt_norm.dlogpdx(x, grad) #opt_entropy.dlogpdx(x, grad) if debug: print "%10f %10f %10f %10f %10f" % ( logp, sum(x), dot(totalmass_matrix, x / sum(x)), dot(ptotalmass_matrix, x / sum(x)), dot(losvds.T, x).sum() * delta_R * delta_v / dot(ptotalmass_matrix, x / sum(x))) #print if 0: print ".", sum(x), dot(totalmass_matrix, x / sum(x)) print logp for i in [0, 10, 100]: x1 = x * 1. #/sum(x) x2 = x * 1. #/sum(x) dx = 1e-7 x2[i] += dx #for opt in [opt_matrix_kin, opt_matrix_massN, opt_norm]: for opt in [opt_matrix_massN]: grad = x * 0 opt.dlogpdx(x, grad) w1 = opt.logp(x1) w2 = opt.logp(x2) print "grad", grad[i] print "grad man", (w2 - w1) / dx #/x[i] print return -logp, -grad u = log(x) #print u w = -self.opt.likelihood(u) grad = u * 0 self.opt.dfdx(u, grad) #print w if 0: print w for i in [0, 1]: u1 = u * 1. u2 = u * 1. du = 1e-4 u2[i] += du for opt in self.opts: grad = u * 0 opt.dfdx(u, grad) w1 = -opt.likelihood(u1) w2 = -opt.likelihood(u2) print "grad", grad[i] print "grad man", (w2 - w1) / du #/x[i] print if 0: mass = numpy.tensordot(rho3dmatrix, x, axes=[(0, ), (0, )]) mass_true = numpy.tensordot(rho3dmatrix, xtrue, axes=[(0, ), (0, )]) print sum(((mass - self.rho3d_target)**2 / rhoerror**2)), sum( ((mass_true - self.rho3d_target)**2 / rhoerror**2)) return w, grad / x if 1: #f_and_g(x) #dsa #u = scipy.optimize.fmin_l_bfgs_b(f_and_g, log(x+1e-9), None, iprint=1,factr=1e3,maxfun=1000)[0] bounds = [(1e-10, 1) for i in range(len(x))] #x = xtrue #bounds = None approx_grad = False if 0: #scope["options"].mcmctest: filename = os.path.join( self.dirname, "results/orbitweights" + self.postfix + ".npy") x = numpy.load(filename) if 0: cdf = cumsum(dot(mass_matrixN.T, x)) cdf /= cdf.max() values = cdf[mass_indices] borders = array([0] + list(cdf)) counts, _ = numpy.histogram(values, borders) dcdf = borders[1:] - borders[:-1] density = counts / dcdf box() #histogram(values, binwidth=0.01) histogramline(borders, density, binwidth=0.01) draw() import pdb pdb.set_trace() else: domcmc(x, opts) hessian = zeros((N, N)) for i in range(N): grad1 = zeros(N) grad2 = zeros(N) dx = 1e-8 x1 = x * 1. x2 = x * 1. x2[i] += dx for opt in opts: opt.dlogpdx(x1, grad1) opt.dlogpdx(x2, grad2) if i == 0: print grad1 print grad2 print grad2 - grad1 hessian[i] = (grad2 - grad1) / dx print hessian[:4, :4] u, s, vd = numpy.linalg.svd(hessian) self.u = u self.vd = vd self.s = s print s S = zeros((len(s), len(s))) for i in range(50): S[-1 - i, -1 - i] = 1 / s[-1 - i] #S[i,i] = 1/s[i] #print S #self.hessian2 = dot(u, dot(S, vd)) #self.cov2 = numpy.matrix(self.hessian2).I self.cov2 = numpy.matrix(vd).T * S * numpy.matrix(u).T mozaic(2, 2, box) select(0, 0) indexedimage(x.reshape(8, 20)) select(1, 0) indexedimage(self.cov2) draw() import pdb pdb.set_trace() else: x = ones(len(x)) x /= sum(x) x = scipy.optimize.fmin_l_bfgs_b(f_and_g, x, None, bounds=bounds, approx_grad=approx_grad, iprint=-1, factr=1e-2, maxfun=200000)[0] #x /= sum(x) u = log(x) if debug: print "res:", [ opt.logp(x) for opt in [opt_matrix_kinN, opt_matrix_massN, opt_norm] ] elif 1: #def myfunc(x, grad): def myfunc(x, grad): #print "%%%" #print x #print sum(x) u = log(x) #print u #x = exp(u) if grad.size > 0: self.opt.dfdx(u, grad) #grad[:] = grad#/x grad /= x w = -self.opt.likelihood(u) if 0: print w print u for i in [0, 1]: u1 = u * 1. u2 = u * 1. du = 1e-4 u2[i] += du w1 = -self.opt.likelihood(u1) w2 = -self.opt.likelihood(u2) print "grad", grad[i] print "grad man", (w2 - w1) / du #/x[i] #print sum(x), w return w epsilon = 0.01 def normconstraint1(x, grad, *a): #print "test" if grad.size > 0: #print "error, no grad possible" grad[:] = 1. #print ">>>>>>>", x return -(sum(x) - 1. + epsilon) def normconstraint2(x, grad, *a): #print "test" if grad.size > 0: #print "error, no grad possible" grad[:] = 1. #print ">>>>>>>", x return -(sum(x) - 1. - epsilon) import nlopt dim = x.shape[0] opt = nlopt.opt(nlopt.LD_MMA, dim) opt = nlopt.opt(nlopt.LD_LBFGS, dim) opt = nlopt.opt(nlopt.LD_TNEWTON_PRECOND_RESTART, dim) #print dir(nlopt) #opt = nlopt.opt(nlopt.LN_COBYLA, dim) opt.set_lower_bounds([1e-10] * dim) #opt.set_lower_bounds([0, float('inf')] * dim) opt.set_min_objective(myfunc) #opt.add_inequality_constraint(normconstraint, 1e-5) #opt.add_inequality_constraint(lambda x,grad: normconstraint1(x,grad), 1e-2) #opt.add_equality_constraint(lambda x,grad: normconstraint1(x,grad), 1e-3) #opt.set_xtol_rel(1e-4) #opt.set_xtol_rel(1e-8) #opt.set_ftol_abs(1e-15) opt.set_ftol_abs(1e-6) opt.set_maxtime(60 * 20) try: #x = opt.optimize(x) x = opt.optimize(x) except: logger.error("oops, opt failed, result code = %d " % opt.last_optimize_result()) #raise #x = opt.last_optimize_result() #import pdb; pdb.set_trace() #print x logger.debug("sum orbit weight: %f " % sum(x)) u = log(x) #x = exp(u) minf = opt.last_optimum_value() #print "optimum at ", x #print "minimum value = ", minf else: #x = x * 0 + 1 #x /= sum(x) u = log(x) #print u self.opt.optimize(10000, 10, u) x = exp(u) x /= sum(x) u = log(x) #numpy.save(filename, x) #print u logL = self.opt.likelihood(u) logL = -f_and_g(x)[0] #logL = self.opt_kin.likelihood(u) refLogL = -29244.4240491 #refLogL = -7392.92252529 refLogL = -7392.92252529 refLogL = -7406.08983537 - 22200.0538537 #-927.128433428 refLogL = -35247.3864887 refLogL = -35161.7892221 refLogL = -7495.98083661 refLogL = -6.06900E+03 refLogL = -5552.10049844 + 24210.9986633 + 80 if isinf(logL): logL = -100000 if isnan(logL): logL = -100000 if debug: print "log L", logL, logL - refLogL, "sum(x)", sum(x), [ opt.likelihood(u) for opt in self.opts ] logL -= refLogL if debug: if fit_mass_3d: mass = numpy.tensordot(rho3dmatrix, x, axes=[(0, ), (0, )]) mass_true = numpy.tensordot(rho3dmatrix, xtrue, axes=[(0, ), (0, )]) #print sum(((mass-self.rho3d_target)**2/rhoerror**2)), sum(((mass_true-self.rho3d_target)**2/rhoerror**2)) if debug: print "mass", sum(mass), sum(mass_true) diff = mass_true - mass print diff print argsort(diff) diff /= rhoerror print diff print argsort(diff) else: mass = numpy.tensordot(rho2dmatrix, x, axes=[(0, ), (0, )]) mass_true = numpy.tensordot(rho2dmatrix, xtrue, axes=[(0, ), (0, )]) #print sum(((mass-self.rho3d_target)**2/rhoerror**2)), sum(((mass_true-self.rho3d_target)**2/rhoerror**2)) if debug: print "mass", sum(mass), sum(mass_true), sum( self.rho2d_target) diff = mass_true - mass print diff print argsort(diff) diff /= rhoerror print diff print argsort(diff) orbitweights = x orbitweights /= sum(orbitweights) mozaic(2, 2, box) x = x.reshape(self.dfgrid.n_I2, self.dfgrid.n_I1) #s = x.std(axis=0).reshape(self.dfgrid.n_I2, self.dfgrid.n_I1) select(0, 0) indexedimage(x) #draw() filename = os.path.join(self.dirname, "results/orbitweights" + self.postfix + ".npy") numpy.save(filename, orbitweights) logger.info("saving orbitweights %s" % filename) moments_m0 = tensordot(orbitweights, self.storage_2d_m0.projectedmoments, axes=[(0, ), (0, )]) if debug: print "sanity check mass", sum(moments_m0[0]) #import pdb; #pdb.set_trace() moments_m2 = tensordot(orbitweights, self.storage_2d_m2.projectedmoments, axes=[(0, ), (0, )]) moments_m4 = tensordot(orbitweights, self.storage_2d_m4.projectedmoments, axes=[(0, ), (0, )]) moments3d = tensordot(orbitweights, self.storage_3d.moments3d, axes=[(0, ), (0, )]) moments_m0[1:] /= moments_m0[0] moments_m2[1:] /= self.binned_data_m2.moments[0] moments_m4[1:] /= self.binned_data_m4.moments[0] moments3d[1:] /= moments3d[0] self.moments2d_solution_m2 = moments_m2 self.moments2d_solution_m4 = moments_m4 self.moments3d_solution = moments3d filename = os.path.join( self.dirname, "results/solution_projectedmoments" + self.postfix + "_m0.npy") numpy.save(filename, moments_m0) logger.debug("saving %s" % filename) filename = os.path.join( self.dirname, "results/solution_projectedmoments" + self.postfix + "_m2.npy") numpy.save(filename, moments_m2) logger.debug("saving %s" % filename) filename = os.path.join( self.dirname, "results/solution_projectedmoments" + self.postfix + "_m4.npy") numpy.save(filename, moments_m4) logger.debug("saving %s" % filename) filename = os.path.join( self.dirname, "results/solution_moments3d" + self.postfix + ".npy") numpy.save(filename, moments3d) logger.debug("saving %s" % filename) chisq = -logL * 2 chisqs = [(name, chisq) for name in "m2 m4 reg kin".split()] f = file( os.path.join(self.dirname, "results/probability" + self.postfix + ".txt"), "w") print >> f, "%e" % exp(-0.5 * chisq) f = file( os.path.join(self.dirname, "results/logprobability" + self.postfix + ".txt"), "w") print >> f, "%e" % (-0.5 * chisq) logps = [(name, -0.5 * chisq) for name, chisq in chisqs] f = file( os.path.join( self.dirname, "results/logprobability_seperate" + self.postfix + ".txt"), "w") print >> f, repr(logps)
def solve(self): #mass_matrix = self.storage_2d_losvd.masses[:,mass_indices] * 1. / delta_R Rborders = arange(self.storage_2d_losvd.NR + 1) / ( 0.0 + self.storage_2d_losvd.NR) * (self.storage_2d_losvd.Rmax) R1s = Rborders[0:-1] R2s = Rborders[1:] dRs = R2s - R1s delta_R = R2s[0] - R1s[0] mass_matrixN = self.storage_2d_losvd.masses * 1. / delta_R totalmass_matrix = ptotalmass_matrix = sum( self.storage_2d_losvd.masses, axis=2) counts = self.photometry.grid print mass_matrixN.shape, counts.shape, totalmass_matrix.shape shape = mass_matrixN.shape newshape = (shape[0] * shape[1], ) + shape[2:] print "shape:", shape, newshape mass_matrixN = mass_matrixN.reshape(newshape) * 1. shape = totalmass_matrix.shape newshape = (shape[0] * shape[1], ) + shape[2:] print "shape:", shape, newshape totalmass_matrix = totalmass_matrix.reshape(newshape) * 1. mask = counts == 0 print sum(mask) #dsa mass_matrixN = mass_matrixN[:, ~mask] * 1 counts = counts[~mask] * 1. print mass_matrixN.shape, counts.shape, totalmass_matrix.shape opt_matrix_massN = gdfast_schw.OptimizationMatrixN( mass_matrixN, counts, totalmass_matrix) x = zeros(newshape[0]) + 1. x /= sum(x) def f_and_g(x): grad = x * 0 logp = opt_matrix_massN.logp(x) * 1 opt_matrix_massN.dlogpdx(x, grad) if debug: print "%10f %10f %10f" % (logp, sum(x), dot(totalmass_matrix, x / sum(x))) return -logp, -grad u = log(x) #print u w = -self.opt.likelihood(u) grad = u * 0 self.opt.dfdx(u, grad) #print w if 0: print w for i in [0, 1]: u1 = u * 1. u2 = u * 1. du = 1e-4 u2[i] += du for opt in self.opts: grad = u * 0 opt.dfdx(u, grad) w1 = -opt.likelihood(u1) w2 = -opt.likelihood(u2) print "grad", grad[i] print "grad man", (w2 - w1) / du #/x[i] print bounds = [(1e-10, 1) for i in range(len(x))] approx_grad = False x = scipy.optimize.fmin_l_bfgs_b(f_and_g, x, None, bounds=bounds, approx_grad=approx_grad, iprint=-1, factr=1e-2, maxfun=200000)[0] print x u = log(x) #if debug: # print "res:", [opt.logp(x) for opt in [opt_matrix_kinN, opt_matrix_massN, opt_norm]] filename = os.path.join(self.dirname, "results/orbitweights" + self.postfix + ".npy") numpy.save(filename, x) logger.debug("saving %s" % filename)
def load(self): self.sub_scope_true.load() self.storage_2d_losvd_true = self.sub_scope_true.subscope[ "storage_2d_losvd"] self.storage_2d_losvd.load() self.storage_2d_losvd_true.load() solution = self.solution_known.load() losvds = self.storage_2d_losvd.losvds * 1. # copy losvds_true = self.storage_2d_losvd_true.losvds * 1. # copy for i in range(losvds.shape[0]): #for j in range(losvds.shape[2]): # pass losvds[i] = scipy.ndimage.gaussian_filter( losvds[i], [self.sigma_v / self.storage_2d_losvd.delta_v, 0.0051], mode="constant") losvds[i] /= (self.storage_2d_losvd.delta_v * self.storage_2d_losvd.delta_R) losvds_true[i] = scipy.ndimage.gaussian_filter( losvds_true[i], [self.sigma_v / self.storage_2d_losvd.delta_v, 0.0051], mode="constant") losvds_true[i] /= (self.storage_2d_losvd.delta_v * self.storage_2d_losvd.delta_R) if self.v_index_clip is not None: index_min = self.storage_2d_losvd.Nv / 2 - self.v_index_clip index_max = self.storage_2d_losvd.Nv / 2 + self.v_index_clip losvds = losvds[:, index_min:index_max, :] losvds_true = losvds[:, index_min:index_max, :] print "clip at v", self.v_index_clip * self.storage_2d_losvd.delta_v, "delta v is", self.storage_2d_losvd.delta_v, "Nv is", self.storage_2d_losvd.Nv #dsa else: index_min = 0 index_max = self.storage_2d_losvd.Nv self.index_min = index_min self.index_max = index_max test = solution * 0 + 1 test /= sum(test) self.testx = test # 'observation' if 1: self.true_losvd = numpy.tensordot(losvds_true, solution, axes=[(0, ), (0, )]) else: self.true_losvd = numpy.tensordot(self.storage_2d_losvd.losvds, solution, axes=[(0, ), (0, )]) #filename = os.path.join(self.storage_2d_losvd.modelpath, "df/losvd_tang.npy") #self.true_losvd = load(filename) self.true_losvd = scipy.ndimage.gaussian_filter( self.true_losvd, [self.sigma_v / self.storage_2d_losvd.delta_v, 0.0051], constant="constant") self.true_losvd /= (self.storage_2d_losvd.delta_v * self.storage_2d_losvd.delta_R) self.true_losvd = self.true_losvd[index_min:index_max, :] self.losvds_test = losvds counts_kin = self.true_losvd * 1. #self.counts_kin #for Rindex in range(self.true_losvd.shape[1]): # counts_kin[:,Rindex] /= sum(counts_kin[:,Rindex]) if self.bias_function: assert False assert len(self.storage_2d_losvd.Rcenters) == counts_kin.shape[1] for Rindex in range(counts_kin.shape[1]): # first, normalize the pdf at each radius, then weight it R = self.storage_2d_losvd.Rcenters[Rindex] counts = sum(counts_kin[:, Rindex]) weight = self.bias_function(R, counts, Rindex) counts_kin[:, Rindex] *= 1. / sum(counts_kin[:, Rindex]) * weight #if Rindex != 0: # counts_kin[:,Rindex] = 0 # always make sure it is normalized counts_kin /= sum(counts_kin) # then rescale by the fake number of observations counts_kin *= self.N #counts_kin[:,0] *= 10 counts_kinR = sum(counts_kin, axis=0) counts_kin = counts_kin.reshape(-1) * 1. self.counts_kin = counts_kin * 1 self.counts_kinR = counts_kinR * 1 #print self.counts_kinR.sum(), self.counts_kin.sum() #dsa if 0: #a = self.true_losvd2 - self.true_losvd #print abs(a).max() box() #print self.true_losvd.shape indexedimage(counts_kin.reshape((index_max - index_min, 30))) draw() dsa # probability matrix pmatrixN = losvds * 1.0 pmatrixN = pmatrixN.reshape((pmatrixN.shape[0], -1)) * 1. self.pmatrixN = pmatrixN * 1.0 #print losvds.shape #adsa self.ptotalmass_matrix = sum(sum(losvds, axis=1), axis=1) #self.ptotalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) #print self.ptotalmass_matrix.shape self.optimizer = gdfast_schw.OptimizationMatrixN( self.pmatrixN, self.counts_kin, self.ptotalmass_matrix) # probability matrix pmatrixRN = sum(losvds * 1.0 * self.storage_2d_losvd.delta_v, axis=1) #pmatrixRN = sum(losvds * 1.0, axis=1) #pmatrixRN = pmatrixRN.reshape((pmatrixRN.shape[0], -1)) * 1. self.pmatrixRN = pmatrixRN * 1.0 #print self.pmatrixRN.shape #dsa self.optimizer_extra = gdfast_schw.OptimizationMatrixN( self.pmatrixRN, self.counts_kinR, self.ptotalmass_matrix) if 0: for k in [self.pmatrixN, self.counts_kin, self.ptotalmass_matrix]: print k.min(), k.max(), k.sum(), k.std() #dsa #opt_norm = gdfast_schw.OptimizationNormalize(1.-.000001, 0.001) #opt_entropy = gdfast_schw.OptimizationEntropy(1.e-2) #import pdb #pdb.set_trace() #debugger() #print self.pmatrixRN.shape x = ones((self.pmatrixRN.shape[0]))
def load(self): self.storage_2d_losvd.load() stars = self.observation.load() logger.info("using %d stars/observations" % len(stars)) #stars_inrange = stars.filter(lambda star: self.storage_2d.aperture.inrange(star.xi, star.eta)) stars_inrange = stars.filter(lambda star: self.storage_2d_losvd. aperture.inrange(star.xi, star.eta)) logger.info("stars in aperture range : %d" % len(stars_inrange)) logger.info("stars outside aperture range: %d" % (len(stars) - len(stars_inrange))) vmax = self.storage_2d_losvd.vmax #print "vmax", vmax delta_v = 2 * vmax / self.storage_2d_losvd.Nv #print "res", delta_v losvds = self.storage_2d_losvd.losvds if self.v_index_clip is not None: index_min = self.storage_2d_losvd.Nv / 2 - self.v_index_clip index_max = self.storage_2d_losvd.Nv / 2 + self.v_index_clip losvds = losvds[:, index_min:index_max, :] print "clip at v", self.v_index_clip * self.storage_2d_losvd.delta_v, "delta v is", self.storage_2d_losvd.delta_v, "Nv is", self.storage_2d_losvd.Nv #dsa else: index_min = 0 index_max = self.storage_2d_losvd.Nv self.losvds = losvds for star in stars: star.aperture_index = self.storage_2d_losvd.aperture.findindex( star.xi, star.eta) #if 0: # stars_invrange = stars.filter(lambda star: abs(star.vlos) < vmax) # logger.info("stars in velocity range : %d" % len(stars_invrange)) # logger.info("stars outside velocity range: %d" % (len(stars)-len(stars_invrange))) # stars = stars_invrange self.used_stars = stars sigma_v = 2.01 numpy.random.seed(8) for star in stars: #star.vlos = star.vlos_true# + numpy.random.normal(0, sigma_v) #star.vlos = star.vlos_true + numpy.random.normal(0, sigma_v) #print star.vlos, vmax, self.storage_2d_losvd.Nv star.v_index = int( ((star.vlos + vmax) / (2 * vmax)) * self.storage_2d_losvd.Nv) - index_min outlier = True if star.v_index < 0: outlier = True elif star.v_index >= (index_max - index_min): outlier = True else: for losvd in losvds: if losvd[star.v_index, star.aperture_index] != 0: outlier = False break #print star.v_index, outlier star.is_outlier = outlier stars_no_outlier = stars.filter(lambda star: not star.is_outlier) logger.info("non-outlier stars : %d" % len(stars_no_outlier)) logger.info("outlier stars : %d" % (len(stars) - len(stars_no_outlier))) stars = stars_no_outlier self.v_indices = v_indices = [star.v_index for star in stars] self.aperture_indices = aperture_indices = [ star.aperture_index for star in stars ] counts = losvds[0, :] * 0 for vi, ai in zip(v_indices, aperture_indices): counts[vi, ai] += 1 mask = counts > 0 counts2d = counts self.counts_kin = counts[mask].reshape(-1) self.pmatrixN = (losvds[:, mask]).reshape((losvds.shape[0], -1)) * 1. #print len(self.counts_kin), self.counts_kin.sum() self.ptotalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) #self.ptotalmass_matrix = sum(sum(losvds, axis=1), axis=1) #print self.ptotalmass_matrix.shape #import pdb; #pdb.set_trace() self.pmatrixN = ascontiguousarray(self.pmatrixN).copy() self.counts_kin = ascontiguousarray(self.counts_kin).copy() self.ptotalmass_matrix = ascontiguousarray( self.ptotalmass_matrix).copy() self.optimizer = gdfast_schw.OptimizationMatrixN( self.pmatrixN, self.counts_kin, self.ptotalmass_matrix) counts = losvds[0, 0, :] * 0 for ai in aperture_indices: counts[ai] += 1 mask = counts > 0 self.counts_R = counts[mask].reshape(-1) #self.pmatrixRN = (losvds[:,mask]).reshape((losvds.shape[0], -1)) * 1. #print len(self.counts_R), self.counts_R.sum() self.pmatrixRN = sum(losvds * 1.0 * self.storage_2d_losvd.delta_v, axis=1)[:, mask] * 1. self.pmatrixRN = ascontiguousarray(self.pmatrixRN).copy() self.optimizer_extra = gdfast_schw.OptimizationMatrixN( self.pmatrixRN, self.counts_R, self.ptotalmass_matrix) #print self.storage_2d_losvd.vmax, self.storage_2d_losvd.delta_v, self.storage_2d_losvd.vmax/self.storage_2d_losvd.delta_v #ds if 0: box() indexedimage(counts2d) draw()
def load(self): self.storage_2d_losvd.load() stars = self.observation.load() print self.bootstrap_seed if self.bootstrap_seed: logger.info("bootstrapping kinematic sample with seed %d" % self.bootstrap_seed) import random random.seed(self.bootstrap_seed) N = len(stars) stars_bs = [] for i in range(N): ri = int(random.random() * N) stars_bs.append(stars[ri]) stars = mab.cvsfile.CsvObject(stars_bs) #import pdb #pdb.set_trace() logger.info("using %d stars/observations" % len(stars)) #stars_inrange = stars.filter(lambda star: self.storage_2d.aperture.inrange(star.xi, star.eta)) stars_inrange = stars.filter(lambda star: self.storage_2d_losvd. aperture.inrange(star.xi, star.eta)) logger.info("stars in aperture range : %d" % len(stars_inrange)) logger.info("stars outside aperture range: %d" % (len(stars) - len(stars_inrange))) vmax = self.storage_2d_losvd.vmax #print "vmax", vmax delta_v = 2 * vmax / self.storage_2d_losvd.Nv #print "res", delta_v losvds = self.storage_2d_losvd.losvds sigma_v = 2.01 for i in range(losvds.shape[0]): #for j in range(losvds.shape[2]): # pass #losvds[i,:,j] = scipy.ndimage.gaussian_filter(losvds[i,:,j], [self.sigma_v/self.storage_2d_losvd.delta_v], mode='constant') losvds[i] = scipy.ndimage.gaussian_filter( losvds[i], [self.sigma_v / self.storage_2d_losvd.delta_v, 0.0051], mode="constant") losvds[i] /= (self.storage_2d_losvd.delta_v * self.storage_2d_losvd.delta_R) if self.v_index_clip is not None: index_min = self.storage_2d_losvd.Nv / 2 - self.v_index_clip index_max = self.storage_2d_losvd.Nv / 2 + self.v_index_clip losvds = losvds[:, index_min:index_max, :] print "clip at v", self.v_index_clip * self.storage_2d_losvd.delta_v, "delta v is", self.storage_2d_losvd.delta_v, "Nv is", self.storage_2d_losvd.Nv #dsa else: index_min = 0 index_max = self.storage_2d_losvd.Nv self.losvds = losvds for star in stars: star.aperture_index = self.storage_2d_losvd.aperture.findindex( star.xi, star.eta) #if 0: # stars_invrange = stars.filter(lambda star: abs(star.vlos) < vmax) # logger.info("stars in velocity range : %d" % len(stars_invrange)) # logger.info("stars outside velocity range: %d" % (len(stars)-len(stars_invrange))) # stars = stars_invrange self.used_stars = stars numpy.random.seed(8) for star in stars: #star.vlos = star.vlos_true# + numpy.random.normal(0, sigma_v) #star.vlos = star.vlos_true + numpy.random.normal(0, sigma_v) #print star.vlos, vmax, self.storage_2d_losvd.Nv star.v_index = int( ((star.vlos + vmax) / (2 * vmax)) * self.storage_2d_losvd.Nv) - index_min outlier = True if star.v_index < 0: outlier = True elif star.v_index >= (index_max - index_min): outlier = True else: for losvd in losvds: if losvd[star.v_index, star.aperture_index] != 0: outlier = False break #print star.v_index, outlier star.is_outlier = outlier stars_no_outlier = stars.filter(lambda star: not star.is_outlier) logger.info("non-outlier stars : %d" % len(stars_no_outlier)) logger.info("outlier stars : %d" % (len(stars) - len(stars_no_outlier))) stars = stars_no_outlier self.v_indices = v_indices = [star.v_index for star in stars] self.aperture_indices = aperture_indices = [ star.aperture_index for star in stars ] counts = losvds[0, :] * 0 for vi, ai in zip(v_indices, aperture_indices): counts[vi, ai] += 1 mask = counts > 0 counts2d = counts self.counts_kin = counts[mask].reshape(-1) self.pmatrixN = (losvds[:, mask]).reshape((losvds.shape[0], -1)) * 1. #print len(self.counts_kin), self.counts_kin.sum() self.ptotalmass_matrix = sum(self.storage_2d_losvd.masses, axis=1) #self.ptotalmass_matrix = sum(sum(losvds, axis=1), axis=1) #print self.ptotalmass_matrix.shape #import pdb; #pdb.set_trace() self.pmatrixN = ascontiguousarray(self.pmatrixN).copy() self.counts_kin = ascontiguousarray(self.counts_kin).copy() self.ptotalmass_matrix = ascontiguousarray( self.ptotalmass_matrix).copy() self.optimizer = gdfast_schw.OptimizationMatrixN( self.pmatrixN, self.counts_kin, self.ptotalmass_matrix) counts = losvds[0, 0, :] * 0 for ai in aperture_indices: counts[ai] += 1 mask = counts > 0 self.counts_R = counts[mask].reshape(-1) #self.pmatrixRN = (losvds[:,mask]).reshape((losvds.shape[0], -1)) * 1. #print len(self.counts_R), self.counts_R.sum() self.pmatrixRN = sum(losvds * 1.0 * self.storage_2d_losvd.delta_v, axis=1)[:, mask] * 1. self.pmatrixRN = ascontiguousarray(self.pmatrixRN).copy() self.optimizer_extra = gdfast_schw.OptimizationMatrixN( self.pmatrixRN, self.counts_R, self.ptotalmass_matrix) #print self.storage_2d_losvd.vmax, self.storage_2d_losvd.delta_v, self.storage_2d_losvd.vmax/self.storage_2d_losvd.delta_v #ds if 0: box() indexedimage(counts2d) draw() #dsa if 0: print "testing gradient normal" testgrad(self) sys.exit(-1)