def train_step( X, y, model, optimizer, scheduler, # generic reg_weight, geometry, epsilon, alpha, lower_limit, upper_limit, attack_iters, criterion # experiment-specific ): """TRADES algorithm [https://arxiv.org/abs/1901.08573]. """ ### adversarial perturbation # init perturbation delta = torch.zeros_like(X) for i in range(len(epsilon)): delta[:, i, :, :].uniform_(-epsilon[i][0][0].item(), epsilon[i][0][0].item()) delta.data = clamp(delta, lower_limit - X, upper_limit - X) # find approx optimal perturbation criterion_kl = nn.KLDivLoss(reduction='sum') with toggle_eval(model): # turn off batchnorm stat tracking for _ in range(attack_iters): with toggle_requires_grad(delta, True): grad = torch.autograd.grad(outputs=criterion_kl( F.log_softmax(model(X + delta), dim=1), F.softmax(model(X), dim=1)), inputs=delta, only_inputs=True)[0] delta = project(delta + alpha * grad.sign(), epsilon, geometry=geometry) delta = clamp(delta, lower_limit - X, upper_limit - X) ### adversarial loss model.apply(zero_grad) # disable batchnorm stat tracking to avoid distribution shift from adversarial examples with disable_batchnorm_tracking(model): loss = criterion_kl(F.log_softmax(model(X + delta), dim=1), F.softmax(model(X), dim=1)) loss *= (reg_weight / X.shape[0]) loss.backward() ### clean loss # batchnorm stat tracking is fine here output = model(X) loss = criterion(output, y) ### update model loss.backward() optimizer.step() scheduler.step() return loss, output
def EoM_term(self, term, output_label=True): # cast params as constant functions so that, if they are set to 0, FEniCS still understand # what is being integrated mu, M = Constant(self.physics.mu), Constant(self.physics.M) lam = Constant(self.physics.lam) mn = Constant(self.mn) D = self.physics.D Phi = self.Phi varPhi = self.varPhi # define r for use in the computation of the Laplacian r = Expression('x[0]', degree=self.fem.func_degree) if term == 1: Term = (Constant(D - 1.) / r * Phi.dx(0) + Phi.dx(0).dx(0)) * mn**2 label = r"$\nabla^2\phi$" elif term == 2: Term = mu**2 * varPhi label = r"$m^2\phi$" elif term == 3: Term = -lam * varPhi**3 label = r"$-\lambda \phi^3$" elif term == 4: Term = mn**D / M**2 * self.source.rho * varPhi label = r"$\frac{\rho}{M^2}\phi$" Term = project(Term, self.fem.dS, self.physics.D, self.fem.func_degree) if output_label: return Term, label else: return Term
def staticmap(): width = int(request.args.get('width', 600)) height = int(request.args.get('height', 600)) bbox = [float(x) for x in request.args['bbox'].split(',')] bbox = mapnik.Coord(bbox[0], bbox[1]), mapnik.Coord(bbox[2], bbox[3]) bbox = project(bbox, projection) return Response(render_map(map, bbox, 600, 600), content_type='image/png')
def compute_screening_factor(self): self.compute_yukawa_force() screening_factor = self.Phi.dx(0) / self.yukawa.dx(0) self.screening_factor = project(screening_factor, self.fem.dS, self.physics.D, self.fem.func_degree)
def _draw_track(self, dr: svgwrite.Drawing, tr: Track, size: XY, offset: XY): color = self.color(self.poster.length_range, tr.length, tr.special) str_length = utils.format_float(self.poster.m2u(tr.length)) date_title = f"{str(tr.start_time)[:10]} {str_length}km" for line in utils.project(tr.bbox(), size, offset, tr.polylines): distance1 = self.poster.special_distance["special_distance"] distance2 = self.poster.special_distance["special_distance2"] has_special = distance1 < tr.length / 1000 < distance2 color = self.color(self.poster.length_range_by_date, tr.length, has_special) if tr.length / 1000 >= distance2: color = self.poster.colors.get( "special2") or self.poster.colors.get("special") polyline = dr.polyline( points=line, stroke=color, fill="none", stroke_width=0.5, stroke_linejoin="round", stroke_linecap="round", ) polyline.set_desc(title=date_title) dr.add(polyline)
def optimize_coverage_multilinear(P, w, verbose=True, k=10, c=1., minibatch_size = None): ''' Run some variant of SGD for the coverage problem with given coverage probabilities P and weights w ''' import torch from utils import project_uniform_matroid_boundary as project #objective which will provide gradient evaluations coverage = CoverageInstanceMultilinear(P, w, verbose) #decision variables x = torch.zeros(P.shape[0], requires_grad = True) #set up the optimizer learning_rate = 0.1 optimizer = torch.optim.SGD([x], momentum = 0.9, lr = learning_rate, nesterov=True) #take projected stochastic gradient steps for t in range(10): loss = -coverage(x) if verbose: print(t, -loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() x.data = torch.from_numpy(project(x.data.numpy(), k, 1/c)).float() return x
def strong_residual_form(self, sol, units): if units == 'rescaled': resc = 1. elif units == 'physical': resc = self.mn**2 * self.mf else: message = "Invalid choice of units: valid choices are 'physical' or 'rescaled'." raise ValueError, message # cast params as constant functions so that, if they are set to 0, FEniCS still understand # what is being integrated mu, M = Constant(self.physics.mu), Constant(self.physics.M) lam = Constant(self.physics.lam) mn, mf = Constant(self.mn), Constant(self.mf) D = self.physics.D # define r for use in the computation of the Laplacian r = Expression('x[0]', degree=self.fem.func_degree) # I expand manually the Laplacian into (D-1)/r df/dr + d2f/dr2 f = sol.dx(0).dx(0) + Constant(D-1.)/r * sol.dx(0) + (mu/mn)**2*sol \ - lam*(mf/mn)**2*sol**3 - (mn**(D-2.)/(mf*M))*self.source.rho f *= resc F = project(f, self.fem.dS, self.physics.D, self.fem.func_degree) return F
def scalar_force(self): grad = self.grad(self.Phi) force = -grad / Constant(self.physics.M) force = project(force, self.fem.dS, self.physics.D, self.fem.func_degree) return force
def project(self, rate, years): d = dict() d = self.income fv = utils.project(d, rate, years) t = Tax(d) t.credits = self.credits t.calculate() utils.report("Projected Tax", t.credits, t.total)
def _normTight(self): """Produce a tight-fitted Gaussian normalization image (width x width)""" GI = np.zeros((self.width, self.width)) r = self.width / 2.0 for i in range(self.N - 1, -1, -1): GI = project(self.coeff[0, i], GI, self.loc[i, :2][::-1] + r) self._gaussNormTight = GI
def lsqr_optim(self): obj_b = (self.cw * self.v + self.tw * self.target_v).flatten() div_b = np.zeros((self.h - 2) * (self.w - 2)) b = np.concatenate((obj_b, div_b)) # Solve v_soln = sparse.linalg.lsqr(self.lsqr_A, b, iter_lim=1e3)[0] v_soln = v_soln.reshape(self.h, self.w, 2) return utils.project(v_soln, self.project_solve)
def prepare(self, shape, fix): """Pre-compute fixation specific Gaussian normalization image """ self.validate() self._normFixation = fix GI = np.zeros(shape[:2]) GI = project(self._gaussNormTight, GI, fix) self._gaussNorm = GI if self._cudaRetina: self._cudaRetina.set_samplingfields(self.loc, self.coeff)
def create_project(is_new=False): pip("django mezzanine pep8 pyflakes django-model-utils") with activate_venv(): # /vagrant is the shared mounted folder between vagrant and your local filesystem with cd("/vagrant"): sudo("mezzanine-project %s" % env.proj_name) with project(): sudo("pip freeze > requirements.txt") sudo("%s startapp %s" % (env.manage, env.app_name)) settings_path = "settings.py" if is_new: settings_path = "{}/{}".format(env.proj_name, settings_path) get(settings_path, "remote_settings.py") Helper().add_line_to_list("remote_settings.py", "settings.py.tmp", "INSTALLED_APPS = (", ' "%s",' % env.app_name) put("settings.py.tmp", "settings.py", use_sudo=True) sed("settings.py", "USE_SOUTH = True", "USE_SOUTH = False", use_sudo=True, backup="", shell=True) put("%s/vagrant_settings.py" % os.path.dirname(os.path.realpath(__file__)), "deploy/vagrant_settings.py", use_sudo=True) put("%s/celeryd.conf" % os.path.dirname(os.path.realpath(__file__)), "deploy/celeryd.conf", use_sudo=True) # Add the appropriate fabric settings for local and development deployment put("fabric_settings.py", "fabric_settings.py", use_sudo=True) with open("%s/fabric_import.py" % os.path.dirname(os.path.realpath(__file__))) as f: file_path = _expand_path("settings.py") sudo("echo '%s' >> %s" % ("", file_path)) for line in f: sudo("echo '%s' >> %s" % (line.rstrip('\n').replace("'", r"'\\''"), file_path)) # Set fabric settings according to user's input sed("fabric_settings.py", "\"DB_PASS\": \"vagrant\"", "\"DB_PASS\": \"%s\"" % env.db_pass, use_sudo=True, backup="", shell=True) sed("fabric_settings.py", "\"DB_PASS\": \"\"", "\"DB_PASS\": \"%s\"" % env.db_pass, use_sudo=True, backup="", shell=True) sed("fabric_settings.py", "\"PROJECT_NAME\": \"\"", "\"PROJECT_NAME\": \"%s\"" % env.proj_name, use_sudo=True, backup="", shell=True) sed("fabric_settings.py", "\"REPO_URL\": \"\"", "\"REPO_URL\": \"%s\"" % env.repo_url, use_sudo=True, backup="", shell=True) sed("fabric_settings.py", "\"PROJECT_PATH\": \"\"", "\"PROJECT_PATH\": \"%s\"" % env.proj_path, use_sudo=True, backup="", shell=True) # We will be using the manticore fabfile not Mezzanine's sudo("rm fabfile.py") # Change Mezzanine project to be compatible with this fabfile sed("deploy/local_settings.py.template", "\"HOST\": \"127.0.0.1\"", "\"HOST\": \"%s\"" % "%(primary_database_host)s", use_sudo=True, backup="", shell=True) append("deploy/local_settings.py.template", "\n# Django 1.5+ requires a set of allowed hosts\nALLOWED_HOSTS = [%(allowed_hosts)s]\n\n# Celery configuration (if django-celery is installed in requirements/requirements.txt)\nBROKER_URL = 'amqp://%(proj_name)s:%(admin_pass)[email protected]:5672/%(proj_name)s'\n\n") #TODO: Install and Link manticore-django fabfile package? run("cp deploy/local_settings.py.template deploy/development_settings.py") run("cp deploy/local_settings.py.template deploy/staging_settings.py") run("cp deploy/local_settings.py.template deploy/production_settings.py") run("rm deploy/local_settings.py.template") local("rm settings.py.tmp remote_settings.py")
def project(self, years): fd = dict() fv = 0.0 for n, v in self.data.items(): d = dict() (val, r) = v d[n] = val fv += utils.project(d, r, years) fd[n] = d[n] utils.report("Projected Assets", fd, fv) return fd
def init_git(): with project(): run("git init") run("echo '.idea/' >> .gitignore") run("echo 'last.commit' >> .gitignore") run("echo 'gunicorn.pid' >> .gitignore") run("git add .") run("git commit -m'init'") #TODO: We shouldn't assume unfuddle here with settings(warn_only=True): run("git remote add unfuddle %s" % env.repo_url) run("git config remote.unfuddle.push refs/heads/master:refs/heads/master") run("git push unfuddle master")
def initial_optim(self): # [Variables] Solve for a velocity field v = cp.Variable(self.v.size) # [Objective] objective_term1 = cp.sum_squares(v - self.v.flatten()) objective_term2 = cp.sum_squares(v - self.target_v.flatten()) objective = cp.Minimize(objective_term1 + objective_term2) problem = cp.Problem(objective) # Optimize, perform projection result = problem.solve(solver=cp.SCS) v_soln = v.value.reshape(self.h, self.w, 2) return utils.project(v_soln, self.project_solve)
def step(self): N = self.size visc = self.visc diff = self.diff dt = self.dt Vx = self.Vx Vy = self.Vy Vx0 = self.Vx0 Vy0 = self.Vy0 s = self.s density = self.density diffuse(1, Vx0, Vx, visc, dt) diffuse(2, Vy0, Vy, visc, dt) project(Vx0, Vy0, Vx, Vy) advect(1, Vx, Vx0, Vx0, Vy0, dt) advect(2, Vy, Vy0, Vx0, Vy0, dt) project(Vx, Vy, Vx0, Vy0) diffuse(0, s, density, diff, dt) advect(0, density, s, Vx, Vy, dt)
def draw(self, dr: svgwrite.Drawing, size: XY, offset: XY): """Draw the heatmap based on tracks.""" bbox = self._determine_bbox() for tr in self.poster.tracks: color = self.color(self.poster.length_range, tr.length, tr.special) for line in utils.project(bbox, size, offset, tr.polylines): for opacity, width in [(0.1, 5.0), (0.2, 2.0), (1.0, 0.3)]: dr.add( dr.polyline( points=line, stroke=color, stroke_opacity=opacity, fill="none", stroke_width=width, stroke_linejoin="round", stroke_linecap="round", ))
def train_step( X, y, model, optimizer, scheduler, # generic epsilon, alpha, lower_limit, upper_limit, criterion, # experiment-specific ) -> tuple: """FGSM with configuration from Fast is Better than Free [https://arxiv.org/abs/2001.03994]. """ ### adversarial perturbation # init delta = torch.zeros_like(X) for j in range(len(epsilon)): delta[:, j, :, :].uniform_(-epsilon[j][0][0].item(), epsilon[j][0][0].item()) delta.data = clamp(delta, lower_limit - X, upper_limit - X) # perturb with toggle_eval(model), toggle_requires_grad( delta, True): # turn off batch norm statistics updating grad = torch.autograd.grad(outputs=criterion(model(X + delta), y), inputs=delta, only_inputs=True)[0] delta = project(delta + alpha * torch.sign(grad), epsilon, geometry='linf') delta = clamp(delta, lower_limit - X, upper_limit - X) ### adversarial loss output = model(X + delta) loss = criterion(output, y) ### update model model.apply(zero_grad) loss.backward() optimizer.step() scheduler.step() # return clean predictions with toggle_eval(model), torch.no_grad(): logits = model(X) loss = criterion(logits, y) return loss, logits
def backproject(self, V, shape, fix): """Backproject the image vector onto a blank matrix equal in size to the input image""" self.validate() if fix != self._normFixation or shape[:2] != self._gaussNorm.shape: self.prepare(shape, fix) if self._cudaRetina: # TODO: helper self._cudaRetina.image_width = shape[1] self._cudaRetina.image_height = shape[0] self._cudaRetina.rgb = len(shape) == 3 and shape[-1] == 3 self._cudaRetina.center_x = fix[1] self._cudaRetina.center_y = fix[0] self._cudaRetina.set_gauss_norm(self._gaussNorm) if self._cudaRetina: return self._cudaRetina.backproject(V) rgb = len(shape) == 3 and shape[-1] == 3 m = shape[:2] if rgb: I1 = np.zeros((m[0], m[1], 3)) else: I1 = np.zeros(m) w = self.width I1 = pad(I1, w, False) I = np.zeros(m) for i in range(self.N - 1, -1, -1): c = self.coeff[0, i] if rgb: c = np.dstack((c, c, c)) I1 = project(c * V[i], I1, self.loc[i, :2][::-1] + fix + w) I1 = I1[w:-w, w:-w] GI = self._gaussNorm if rgb: GI = np.dstack((GI, GI, GI)) I = np.uint8(np.divide(I1, GI)) self._backproj = I return I
def train_step( X, y, model, optimizer, scheduler, # generic geometry, epsilon, alpha, lower_limit, upper_limit, attack_iters, criterion # experiment-specific ) -> tuple: """Madry PGD algorithm [https://arxiv.org/abs/1706.06083] """ ### adversarial perturbation # init delta = torch.zeros_like(X) for i in range(len(epsilon)): delta[:, i, :, :].uniform_(-epsilon[i][0][0].item(), epsilon[i][0][0].item()) delta.data = clamp(delta, lower_limit - X, upper_limit - X) # perturb with toggle_eval(model): # turn off batch normalization statistics updating for _ in range(attack_iters): with toggle_requires_grad(delta, True): grad = torch.autograd.grad( outputs =criterion(model(X + delta), y), inputs =delta, only_inputs=True)[0] delta = project(delta + alpha * grad.sign(), epsilon, geometry=geometry) delta = clamp(delta, lower_limit - X, upper_limit - X) ### compute loss output = model(X + delta) loss = criterion(output, y) ### update model model.apply(zero_grad) loss.backward() optimizer.step() scheduler.step() with toggle_eval(model), torch.no_grad(): logits = model(X) loss = criterion(logits, y) return loss, logits
def pd_optim(self): """PD optimization step. Ref: Inglis et al. (https://arxiv.org/pdf/1611.03677.pdf).""" tau = self.pd_params['tau'] sigma = self.pd_params['sigma'] theta = self.pd_params['theta'] w = self.pd_params['guiding_w'] max_iters = self.pd_params['max_iters'] for k in range(max_iters): prox_f_val = self.prox_f(sigma, (self.pd_x + self.pd_y) / sigma, w) self.pd_x += sigma * (self.pd_y - prox_f_val) pd_z_next = utils.project(self.pd_z - tau * self.pd_x, self.project_solve) delta_pdz = pd_z_next - self.pd_z self.pd_z = pd_z_next self.pd_y = self.pd_z + theta * delta_pdz # Termination criterion eps = 1e-3 * (np.sqrt(2) + np.linalg.norm(self.pd_z)) if np.linalg.norm(delta_pdz) <= eps: break return self.pd_z
def backproject_tight(self, V, shape, fix): """Produce a tight-fitted backprojection (width x width, lens only)""" #TODO: look at the weird artifacts at edges when the lens is too big for the frame. Might be a small bug self.validate() rgb = len(shape) == 3 and shape[-1] == 3 m = self.width r = m / 2.0 if rgb: I1 = np.zeros((m, m, 3)) else: I1 = np.zeros((m, m)) I = np.zeros_like(I1) for i in range(self.N - 1, -1, -1): c = self.coeff[0, i] if rgb: c = np.dstack((c, c, c)) I1 = project(c * V[i], I1, self.loc[i, :2][::-1] + r) GI = self._gaussNormTight if rgb: GI = np.dstack((GI, GI, GI)) #TODO: fix invalid value warnings I = np.uint8(np.divide(I1, GI)) self._backprojTight = I return I
# http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'en-us' SITE_ID = 1 # If you set this to False, Django will make some optimizations so as not # to load the internationalization machinery. USE_I18N = True # If you set this to False, Django will not format dates, numbers and # calendars according to the current locale USE_L10N = True # Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/home/media/media.lawrence.com/media/" MEDIA_ROOT = project('media/') # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" # MEDIA_URL = '/static/' # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" STATIC_ROOT = project('static/') # URL prefix for static files. # Example: "http://media.lawrence.com/static/" STATIC_URL = '/static/'
def init_db(): with project(): manage("syncdb --noinput") manage("migrate")
K = np.load("../camMatrix_720p.npy") dist = np.zeros(shape=5) ########################################## Case-1 ########################################## ## Here we shall place two cameras, one at origin and the other somehwere ie R2 and X_cam2. ## We shall manually project a set of 3d points on two different cameras. ## We shall use the 2d-projected points from both the cameras, calculate the Essential-Mat, get the R and t. ## This R and t from essential must be same as our predefined R2, t2. # This is the first camera with same orientation and same place as origin. X_cam1 = np.asarray([0, 0, 0]).reshape(3, -1) R1 = utils.rotate() t1 = -R1.dot(X_cam1) # t will be R_transpose.cam-centre P1 = utils.P_from_krt(K, R1, t1) pts2d_1 = utils.project(P1, Points) pts2d_1 = utils.hom_to_euc(pts2d_1) # print("Finally points are ", pts2d_1) # This is the 2nd camera oriented and shifted to some other place. X_cam2 = np.asarray([10, 25, 7]).reshape(3, -1) R12 = utils.rotate(thetax=49) t12 = -R12.dot( X_cam2 ) # t1 will be R_transpose.cam-centre . t2 is essentially location of cam1 wrt cam2. t12_norm = t12 / np.linalg.norm(t12) P2 = utils.P_from_krt(K, R12, t12) pts2d_2 = utils.project(P2, Points) pts2d_2 = utils.hom_to_euc(pts2d_2)
def project_velocity(self): self.v = utils.project(self.v, self.project_solve)
def project(self, rate, years): d = dict() d = self.data fv = utils.project(d, rate, years) utils.report("Projected Expenses", d, fv)
def second(message): bot.send_message(message.chat.id, "*Text about Категория 2*\n\n", parse_mode="Markdown", reply_markup=utils.project())
def init_db(): with project(): manage("syncdb --noinput") manage("schemamigration %s --initial" % env.app_name) manage("migrate")
# TO BE PROVED # Take three cameras with any arbitrary place. # Take three more camers with same config except that their t is unit-length. # Verify if u get the distance correctly in the 2nd camera systems. # These will be the centres. c1 = np.asarray([0, 0, 0]).reshape(3, -1) c2 = np.asarray([70, 70, 70]).reshape(3, -1) c3 = np.asarray([30, 15, 40]).reshape(3, -1) c4 = np.asarray([25, 45, 25]).reshape(3, -1) # Let these be the first set of cameras Cam1 = Camera(K, Rc=utils.rotate(), center=c1) pts2d_1 = utils.hom_to_euc(utils.project(Cam1.P, pts3d_11)) Cam2 = Camera(K, Rc=utils.rotate(thetay=169), center=c2) pts2d_2 = utils.hom_to_euc(utils.project(Cam2.P, pts3d_11)) Cam3 = Camera(K, Rc=utils.rotate(thetaz=90), center=c3) pts2d_3 = utils.hom_to_euc(utils.project(Cam3.P, pts3d_11)) Cam4 = Camera(K, Rc=utils.rotate(thetax=90), center=c4) pts2d_4 = utils.hom_to_euc(utils.project(Cam4.P, pts3d_11)) # Let these be the second set of cameras at just unit-distance from world UnitCam1 = Camera(K, Rc=utils.rotate(), center=utils.norm(c1)) unit_pts2d_1 = utils.hom_to_euc(utils.project(UnitCam1.P, pts3d_11)) UnitCam2 = Camera(K, Rc=utils.rotate(thetay=169), center=utils.norm(c2)) unit_pts2d_2 = utils.hom_to_euc(utils.project(UnitCam2.P, pts3d_11)) UnitCam3 = Camera(K, Rc=utils.rotate(thetaz=90), center=utils.norm(c3)) unit_pts2d_3 = utils.hom_to_euc(utils.project(UnitCam3.P, pts3d_11)) UnitCam4 = Camera(K, Rc=utils.rotate(thetax=90), center=utils.norm(c4))
return '7' elif 5 <= togo < 7: return '5' elif 3 <= togo < 5: return '3' elif 1 <= togo < 3: return '1' else: return '0' def _normalize_yrd(yrdline): return str(floor((100 - float(yrdline)) / 10)) get_info = project('off', 'down', 'togo', 'ydline', 'description') def get_play(row): """ This doesn't set TO on downs, because it would need a lookahead into the next play. """ atkr, down, togo, yrdline, desc = get_info(row) play = {'playtype': get_playtype(desc)} try: play.update({ 'down': down, 'togo': _normalize_togo(togo), 'yrd': _normalize_yrd(yrdline), 'atkr': atkr, })
def __init__(self, ions = None, molfrac_in = None, molfrac_out = None, decay = True, isobars = False, elements = False, isotones = False, isotopes = True, solprod = False, stable = False, solions = False, sort = True, ionlist = None, addions = None, keepions = None, stabions = None, orgions = None, solabu = None, keepall = True, decayfile = '~/kepler/local_data/decay.dat', isomers = None, silent = False, debug = False, ): """ Initialize decay map. ions - list of input ions. Currently np.array of isotope.Ion. Probably should allow (or requre?) composition instead? Allow any string, array of strings, ... Same for all ion lists. decay - whether to do decay if disabled and no other option is used, the matrix will just do sorting, if specified molfrac_in - whether input is mass or abundance. molfrac_out - whether output is mass or abundance. TODO - add molfrac, used for both isobars - return result mapped to isobars [only stable or undecayed isotopes] elements - return result mapped to elements [only stable or undecayed isotopes] isotones - return result mapped to isotones [only stable or undecayed isotopes] isotopes - return result mapped to isotopes - if processing isomers ADD - isotopes - return result mapped to isotopes; isomers otherwise ADD - decay - do decay or not solabu - solar abundance set to use: filename, token, or isotope.SolAbu solprod - return result in terms of solar production factor [only stable isotopes] solions - return exactly ions in solar abundace set [plus addions, stabions, keepions] ionlist - output *exactly* these isotops stable - output only stable isotopes, discard chains addions - output these extra ions, but not add to EL/A sums keepions - ions to keep as stable but not add ion EL/A sums stabions - ions to fully treat as stable - may conflict with solprod orgions - ions for which to return the initial value w/o decays sort - sort output ions (Z > A > E), mostly relevant when customized ions are provided. keepall - if set to false, do not keep all ions even if stable set is used. decayfile - decay file to use. [should add allowing to pass object] isomers - process as isomers if input is isotopes silent - whether to be verbose or not """ self.setup_logger(silent) # ions = np.array([ion('pb220')]) # ions = np.array([ion('n13'), ion('o14'), ion('pb209')]) # ions = np.array([ion('n13'), ion('o14')]) ions, molfrac_in, molfrac_out = self.input2ionarr( ions, molfrac_in, molfrac_out) if isomers is None: isomers = np.any(ufunc_is_isomer(ions)) if not isomers and np.any(ufunc_is_isomer(ions)): new_ions = assert_isotope(ions) assert len(np.unique(ufunc_idx(new_ions))) == len(ions), 'Failed to project ions uniquely' ions = new_ions if isomers and not np.all(ufunc_is_isomer(ions)): ions = assert_isomer(ions) # all need to be the same. TODO - more sophisticated assert np.all(ufunc_is_isomer(ions) == ions[0].is_isomer()) if molfrac_in is None: molfrac_in = False if molfrac_out is None: molfrac_out = molfrac_in assert isinstance(ions, np.ndarray), "ions need to be np.array type" assert ions.ndim == 1, "ions need to be 1D array" assert isinstance(ions[0], Ion), "ions must be of type Ion" # find out whether we need list of stable ions need_stable = stable # TODO - check ionlist should exclude isobars, isotopes, ... need_isotopes = isotopes need_isobars = isobars need_isotones = isotones need_elements = elements # add things from ionlist if ionlist: need_isotopes |= np.any(ufunc_is_isotope(ionlist)) need_isobars |= np.any(ufunc_is_isobar(ionlist)) need_isotones |= np.any(ufunc_is_isotone(ionlist)) need_elements |= np.any(ufunc_is_element(ionlist)) if addions: need_isotopes |= np.any(ufunc_is_isotope(addions)) need_isobars |= np.any(ufunc_is_isobar(addions)) need_isotones |= np.any(ufunc_is_isotone(addions)) need_elements |= np.any(ufunc_is_element(addions)) need_orgions = orgions or not decay ### [WORK HERE] # THIS IS WRONG - ONLY IF DECAY # if need_elements or need_isobars or need_isotones or (need_isotopes and isomers): # need_stable = decay if need_elements or need_isobars or need_isotones: need_stable = decay self.ions = ions self.amax = np.max(ufunc_A(ions)) self.zmax = np.max(ufunc_Z(ions)) # check keepions if keepions is not None: if isinstance(keepions, IonSet): keepions = np.array(keepions) keepions = np.atleast_1d(keepions) assert isinstance(keepions, np.ndarray), "keepions need to be np.array type" assert keepions.ndim == 1, "keepions need to be 1D array" assert issubclass(type(keepions[0]),Ion), "keepions must be of type Ion" self.amax = max(self.amax, np.max(ufunc_A(keepions))) self.zmax = max(self.zmax, np.max(ufunc_Z(keepions))) # check stabions if stabions is not None: if isinstance(stabions, IonSet): stabions = np.array(stabions) stabions = np.atleast_1d(stabions) assert isinstance(stabions, np.ndarray), "stabions need to be np.array type" assert stabions.ndim == 1, "stabions need to be 1D array" assert issubclass(type(stabions[0]),Ion), "stabions must be of type Ion" self.amax = max(self.amax, np.max(ufunc_A(stabions))) self.zmax = max(self.zmax, np.max(ufunc_Z(stabions))) # check ionlist if ionlist is not None: if not isinstance(ionlist, np.ndarray): ionlist = np.array(ionlist) ionlist = np.atleast_1d(sn.squeeze(ionlist)) assert ionlist.ndim == 1, "ionlist need to be 1D array" assert issubclass(type(ionlist[0]),Ion), "ionlist must be of type Ion" self.amax = max(self.amax, np.max(ufunc_A(ionlist))) self.zmax = max(self.zmax, np.max(ufunc_Z(ionlist))) # generate decay data self.decdata = DecayData( filename = decayfile, amax = self.amax, zmax = self.zmax, isomers = isomers, silent = silent, debug = debug, ) # self.decdata = DecayData(decayfile, isomers = isomers, silent = silent) # now let's add keepions - not sure about this one - probably wrong if keepions is not None: # the strategy is to modify the decdata for ix in keepions: self.decdata.add_stable(ix) # ...and stabions if stabions is not None: # the strategy is modify the decdata for ix in stabions: self.decdata.add_stable(ix) # construct table from ions self.dectable = {} if decay: d0 = [self.iter_add_dectable(ix) for ix in self.ions] else: d0 = [self.identity_dectable(ix) for ix in self.ions] # let us just use indices into the array to speed things up self.d = np.ndarray(len(self.dectable), dtype = object) for dec in self.dectable.values(): self.d[dec[0][1]] = dec self.decions = np.array([ix for ix in self.dectable.keys()], dtype = object) self.indices = np.array([dec[0][1] for dec in self.dectable.values()], dtype = np.int64) # construct decay matrix - this should be its separate method!!! nions = len(ions) ndec = len(self.dectable) self.decmatrix = np.zeros([nions,ndec], dtype=np.float64) # compute needed decay matrix # # currently this is fixed when isomers are mapped to isotopes, # but this loses the 'maxradio' info for isomers. # # Instead, in the future, a full square decay matrix needs to # be constractued that can invert the isomer submatrix for # isotopes, and similar for all cases, releasing the # requirement for stable or raw isotopes only. # # the extra isotopes should be added after a first full decay # attempt. # # not sure this will ever be useful other than for isotopes. # if need_isotopes and not stable and decay and isomers: self.revind = np.argsort(self.indices) for i,ix in enumerate(self.ions): self.iter_add_decmatrix_iso(i, np.float64(1), d0[i]) else: for i,ix in enumerate(self.ions): self.iter_add_decmatrix(i, np.float64(1), d0[i]) m = self.decions.argsort() self.decions = self.decions[m] self.decmatrix = self.decmatrix[:,self.indices[m]] self.molfrac = [molfrac_in, molfrac_out] self.molfrac_convert(self.decmatrix) # we need to check whether we need solar abundances. need_solabu = solions or solprod # since we can get the list of stable ions from the decay table, # we do not really need this for definition of stable isotopes. if need_solabu: if isinstance(solabu, str): solabu = SolAbu(solabu) elif solabu is None: solabu = SolAbu() assert isinstance(solabu, SolAbu), "Need solar abundace data." # let us assure solar abundance pattern is sorted assert not np.any(solabu.iso.argsort() - np.arange(len(solabu))), "Solar pattern not sorted." # now we construct the map to solar, smap, and the map # from solabu to decions, rmap k = 0 smap = [] rmap = [] for i,iso in enumerate(solabu.iso): while self.decions[k] < iso: k += 1 if k == len(self.decions): break if k == len(self.decions): break if self.decions[k] == iso: smap.append(k) rmap.append(i) # why this ... not need_solabu ??? # why not have need_solabu inply need_stable? if need_stable and not need_solabu: # here we are overwring smap from above!!! # WHY??? # DELETE ??? smap = [i for i,ix in enumerate(self.decions) if len(self.dectable[ix]) == 1] # before we do any of the following, we still need to make # sure to only include stable isotopes. # proably best to keep old array and construct new one piece # by piece. if need_stable or need_solabu: stable_decions = self.decions[smap] stable_decmatrix = self.decmatrix[:,smap] # add missing solar ions # ??? UPDATE to include stab... if solions and len(solabu) > len(stable_decions): ndec = len(solabu) d = np.zeros([nions,ndec], dtype=np.float64) d[:,rmap] = stable_decmatrix[:,:] stable_decions = solabu.iso stable_decmatrix = d ### add missing ions from ion list # .... # can ion list be elements, isobars, isotones? # yes! # # 1) the stuff below needs to be termined which to compute # 2) store computed values separately - not right in decions # 3) then select the ecessary ones for final decions # maybe able to construct decay/isotop map? if need_isotopes and not stable and decay and isomers: # construct a decay matrix that does not double-count. # The key is here a function that finds entries with # identical projected values decidx = ufunc_isotope_idx(self.decions) idx = np.unique( decidx, ) decmatrix = self.decmatrix.copy() # but how to discard double counts??? # ??? mat coeff in range, -I, subtract entire column (mat mult) # submatrix = np.zeros([len(self.decions)]*2) for i in idx: ii = np.where(i == decidx)[0] if len(ii) > 1: deciso = self.decions[ii] # do we need to assume isomeric states are ordered, or that order matters? ions = [] iions = [] for jj,ix in enumerate(deciso): j = np.where(self.ions == ix)[0] if len(j) == 1: ions += [j[0]] iions += [ii[jj]] elif len(j) > 1: raise Exception('something is wrong here') print(self.decmatrix[np.ix_(ions,ii)].transpose()) # next: sort by chain? # find things that depend on each other, in order # then subtract in order # maybe subtract where things depend on self? # OK, for this to work we need to ensure all ions # on 'out' channels are also on 'in' channel so # the matrix can be 'inverted'. assert len(ii) == len(ions), 'matrix cannot be inverted' # currently, further up, we create a differnt # matrix for this case that does not require this # correction. In practice, we want to have both # options, the 'maxradio' for all sub-sets and # supersets. raise NotImplementedError() # *** temporary fix: if need_stable: raw_ions = stable_decions raw_matrix = stable_decmatrix else: raw_ions = self.decions raw_matrix = self.decmatrix if need_elements: decionz = ufunc_Z(raw_ions) elements_decmatrix, z = project(raw_matrix, decionz, return_values = True, axis = -1) elements_decions = np.array([ion(Z = Z) for Z in z]) if need_isobars: deciona = ufunc_A(raw_ions) isobars_decmatrix, a = project(raw_matrix, deciona, return_values = True, axis = -1) isobars_decions = np.array([ion(A = A) for A in a]) if need_isotones: decionn = ufunc_N(raw_ions) isotones_decmatrix, n = project(raw_matrix, decionn, return_values = True, axis = -1) isotones_decions = np.array([ion(N = N) for N in n]) # *** likely, this should not use raw_ions but self.decions if need_isotopes: deciiso = ufunc_isotope_idx(raw_ions) isotopes_decmatrix, i = project(raw_matrix, deciiso, return_values = True, axis = -1) isotopes_decions = np.array([ion(idx = I) for I in i]) if need_orgions: orgions_decmatrix = np.identity(len(self.ions)) orgions_decions = self.ions if solprod: # TODO raise NotImplementedError('solprod') # compile final decions set; make it an IonList if isobars: decmatrix = isobars_decmatrix decions = isobars_decions elif isotones: decmatrix = isotones_decmatrix decions = isotones_decions elif elements: decmatrix = elements_decmatrix decions = elements_decions elif isotopes: decmatrix = isotopes_decmatrix decions = isotopes_decions elif ionlist is not None: # TODO raise NotImplementedError('ionlist') else: decmatrix = self.decmatrix decions = self.decions # clean up self.decions = decions self.map = decmatrix del self.decmatrix self.close_logger(timing = 'matrix constructed in')
def output_term(self, term='LHS', norm='none', units='rescaled', output_label=False): # cast params as constant functions so that, if they are set to 0, FEniCS still understand # what is being integrated mu, M = Constant(self.physics.mu), Constant(self.physics.M) lam = Constant(self.physics.lam) mn, mf = Constant(self.mn), Constant(self.mf) D = self.physics.D if units == 'rescaled': resc = 1. str_phi = '\\hat{\\phi}' str_nabla2 = '\\hat{\\nabla}^2' str_m2 = '\\left( \\frac{\\mu}{m_n} \\right)^2' str_lambdaphi3 = '\\lambda\\left(\\frac{m_f}{m_n}\\right)^2\\hat{\\phi}^3' str_rho = '\\frac{m_n^{D-2}}{m_f}\\frac{\\hat{\\rho}}{M}' elif units == 'physical': resc = self.mn**2 * self.mf str_phi = '\\phi' str_nabla2 = '\\nabla^2' str_m2 = '\\mu^2' str_lambdaphi3 = '\\lambda\\phi^3' str_rho = '\\frac{\\rho}{M}' else: message = "Invalid choice of units: valid choices are 'physical' or 'rescaled'." raise ValueError, message phi = self.phi # define r for use in the computation of the Laplacian r = Expression('x[0]', degree=self.fem.func_degree) if term == 'LHS': # I expand manually the Laplacian into (D-1)/r df/dr + d2f/dr2 Term = Constant(D-1.)/r * phi.dx(0) + phi.dx(0).dx(0) \ + (mu/mn)**2*phi - lam*(mf/mn)**2*phi**3 label = r"$%s%s + %s%s - %s$" % (str_nabla2, str_phi, str_m2, str_phi, str_lambdaphi3) elif term == 'RHS': Term = (mn**(D - 2.) / (mf * M)) * self.source.rho label = r"$%s$" % (str_rho) elif term == 1: Term = Constant(D - 1.) / r * phi.dx(0) + phi.dx(0).dx(0) label = r"$%s%s$" % (str_nabla2, str_phi) elif term == 2: Term = +(mu / mn)**2 * phi label = r"$%s%s$" % (str_m2, str_phi) elif term == 3: Term = -lam * (mf / mn)**2 * phi**3 label = r"$-%s$" % (str_lambdaphi3) elif term == 4: Term = (mn**(D - 2.) / (mf * M)) * self.source.rho label = r"$%s$" % (str_rho) # rescale if needed to get physical units Term *= resc Term = project(Term, self.fem.dS, self.physics.D, self.fem.func_degree) # 'none' = return function, not norm if norm == 'none': result = Term # from here on return a norm. This nested if is to preserve the structure of the original # built-in FEniCS norm function elif norm == 'linf': # infinity norm, i.e. max abs value at vertices result = rD_norm(Term.vector(), self.physics.D, self.fem.func_degree, norm_type=norm) else: result = rD_norm(Term, self.physics.D, self.fem.func_degree, norm_type=norm) if output_label: return result, label else: return result
from utils import Camera # np.random.seed(79) np.set_printoptions(precision=3) thetaX = np.random.randint(0, 360) thetaY = np.random.randint(0, 360) thetaZ = np.random.randint(0, 360) pts3d_11 = np.random.randint(11, 50, size=(8, 3)).astype(np.float32) # 8-points K = np.load("../camMatrix_720p.npy") dist = np.zeros(shape=5) # This is the first camera with same orientation and same place as origin. Cam11 = Camera(K) pts2d_11 = utils.project(Cam11.P, pts3d_11) pts2d_11 = utils.hom_to_euc(pts2d_11) # This is second camera. Cam12 = Camera(K, Rc=utils.rotate(thetax=thetaX), center=np.asarray([10, 25, 7]).reshape(3, -1)) Cam12_R = Cam12.R Cam12_c = Cam12.center Cam12_t = Cam12.t pts3d_12 = np.matmul(Cam12.Rt, utils.euc_to_hom(pts3d_11).T).T pts2d_12 = utils.project(Cam12.P, pts3d_11) pts2d_12 = utils.hom_to_euc(pts2d_12) # This is third camera. Cam13 = Camera(K, Rc=utils.rotate(thetay=thetaY), center=np.asarray([15, 25, 10]).reshape(3, -1)) Cam13_R = Cam13.R Cam13_c = Cam13.center
def project(self, rate, years): d = dict() d = self.data fv = utils.project(d, rate, years) report("Projected Income", d, fv)