def rem_run(id, confirm=True): run = core.get_default_session().query(Creator).filter_by(id=id).first() if run is None: raise ValueError(" Run %i does not exist" % id) print("You want to delete everything created by the following run:") run.print_info() if confirm: print(""">>> type "yes" to continue""") if (not confirm) or input(":").lower() == "yes": #for y in run.halolinks: # core.get_default_session().delete(y) run.halolinks.delete() run.halos.delete() run.properties.delete() run.timesteps.delete() for s in run.simulations: print(s) core.get_default_session().delete(s) core.get_default_session().commit() core.get_default_session().delete(run) core.get_default_session().commit() print("OK") else: print("aborted")
def rem_simulation_timesteps(options): basename = options.sims from tangos.util.terminalcontroller import term sim = core.get_default_session().query(Simulation).filter_by( basename=basename).first() if sim == None: print(term.GREEN + "Simulation does not exist", term.NORMAL) else: print(term.RED + "Delete simulation", sim, term.NORMAL) core.get_default_session().delete(sim)
def rollback(options): if len(options.ids)>0: for run_id in options.ids: rem_run(run_id, not options.force) else: most_recent_id = core.get_default_session().query(Creator).order_by( Creator.id.desc()).first().id rem_run(most_recent_id, not options.force)
def _erase_run_content(run): run.halolinks.delete() run.halos.delete() run.properties.delete() run.timesteps.delete() for s in run.simulations: core.get_default_session().delete(s) core.get_default_session().commit() core.get_default_session().delete(run) core.get_default_session().commit()
def db_import(options): sims = options.sims remote_db = options.file global internal_session engine2 = create_engine('sqlite:///' + remote_db, echo=False) ext_session = sessionmaker(bind=engine2)() _db_import_export(core.get_default_session(), ext_session, *sims)
def update_tracker_halos(sim=None): from tangos import get_simulation from tangos.core import get_default_session from tangos.util.terminalcontroller import heading if sim is None: x = get_default_session().query(TrackData).all() else: x = get_simulation(sim).trackers.all() for y in x: heading(repr(y)) y.create_objects()
def rem_run(id, confirm=True): run = core.get_default_session().query(Creator).filter_by(id=id).first() if run is None: raise ValueError(" Run %i does not exist" % id) print("You want to delete everything created by the following run:") run.print_info() if confirm: print(""">>> type "yes" to continue""") if (not confirm) or input(":").lower() == "yes": _erase_run_content(run) print("OK") else: print("aborted")
def setup_module(): testing.init_blank_db_for_testing() generator = SimulationGeneratorForTests() generator.add_timestep() generator.add_objects_to_timestep(10) generator.add_properties_to_halos(Mvir=lambda i: 1. * i) halo = tangos.get_halo(1) session = core.get_default_session() px = create_property(halo, "Mvir", -1., session) session.add(px, session) px = create_property(halo, "Mvir", -2., session) session.add(px, session) session.commit() # Also create links between halos, including duplicates halo2 = tangos.get_halo(2) halo3 = tangos.get_halo(3) halo9 = tangos.get_halo(9) # two links between halo 1 to halo 2 with the same weight and name (maximal duplicate) d_test = tangos.core.get_or_create_dictionary_item(session, "test") l_obj = link.HaloLink(halo, halo2, d_test, 1.0) session.add(l_obj) l_obj = link.HaloLink(halo, halo2, d_test, 1.0) session.add(l_obj) # and another time but with same weight but different name (not a duplicate) diff_name = tangos.core.get_or_create_dictionary_item( session, "other_test") l_obj = link.HaloLink(halo, halo2, diff_name, 1.0) session.add(l_obj) # and another time, with same name but different weight # (this is a non-maximal duplicate, oldest addition gets deleted and we keep the most recent link) l_obj = link.HaloLink(halo, halo2, d_test, 0.5) session.add(l_obj) # and another time, with same weight and name as previous but linking to a different halo (not a duplicate) l_obj = link.HaloLink(halo, halo3, d_test, 1.0) session.add(l_obj) # and now a completely independent link between halo 2 to halo 9 l_obj = link.HaloLink(halo2, halo9, d_test, 1.0) session.add(l_obj)
def db_export(remote_db, *sims): global internal_session engine2 = create_engine('sqlite:///' + remote_db, echo=False) int_session = core.get_default_session() ext_session = sessionmaker(bind=engine2)() Base.metadata.create_all(engine2) _xcurrent_creator = core.creator.get_creator() core.set_default_session(ext_session) core.set_creator(ext_session.merge(Creator())) _db_import_export(ext_session, int_session, *sims) core.set_creator(_xcurrent_creator) core.set_default_session(int_session)
def list_recent_runs(opts): n = opts.num run = core.get_default_session().query(Creator).order_by( Creator.id.desc()).limit(n).all() for r in run: r.print_info()
def grep_run_info(opts): matching_runs = core.get_default_session().query(Creator).filter( Creator.command_line.like("%" + opts.query + "%")).all() for r in matching_runs: r.print_info() return matching_runs
def grep_run_id(opts): matching_runs = core.get_default_session().query(Creator).filter( Creator.command_line.like("%" + opts.query + "%")).all() for r in matching_runs: print(r.id, end=' ') print('\n')
def add_tracker(halo, size=None): import pynbody try: halo = get_halo(halo) except: sim = get_simulation(halo) print("Adding tracker for isolated run", sim) halo = None if halo is not None: # check we can open the tracker file hfile = halo.load() hfile.physical_units() use_iord = True try: hfile.dm['iord'] except: use_iord = False # get the centre cen = halo.get('SSC', None) if cen is None: cen = pynbody.analysis.halo.shrink_sphere_center(hfile.dm) hfile.ancestor.dm['pos'] -= cen if size is None: size = '500 pc' size = pynbody.units.Unit(size) try: size.in_units("kpc") X = hfile.ancestor.dm[pynbody.filt.Sphere(size)] except: size.in_units("kpc km s^-1") X = hfile.ancestor.dm[pynbody.filt.LowPass("j2", size ** 2)] size = str(size.in_units("kpc km s^-1")) + "_kks" if len(X) < 2: print("Alert! Track data is too short") import pdb pdb.set_trace() # setup the tracker tx = TrackData(halo.timestep.simulation) print("Tracker halo ID is", tx.halo_number) print("Length of track data is", len(X)) tx.select(X, use_iord) ts_trigger = halo.timestep else: f = sim.timesteps[0].load() tx = TrackData(sim) if tx.halo_number != 1: print("Already have a tracker for this simulation -> abort") return print("Tracker halo ID is", tx.halo_number) tx.particles = np.array( np.arange(0, len(f.dm) - 1, 1), dtype=int) tx.use_iord = False ts_trigger = None core.get_default_session().add(tx) tx.create_objects(first_timestep=ts_trigger) if halo is not None: targ = halo.timestep.halos.filter_by( object_typecode=1, halo_number=tx.halo_number).first() size = str(size).replace(" ", "") halo["dm_central_" + size] = targ print("Copying SSC...", end=' ') sys.stdout.flush() while halo is not None: try: targ['SSC'] = halo['SSC'] except KeyError: pass halo = halo.next targ = targ.next print("done") core.get_default_session().commit()
def __init__(self, session=None): self.session = session or core.get_default_session()
def grep_run(st): run = core.get_default_session().query(Creator).filter( Creator.command_line.like("%" + st + "%")).all() for r in run: print(r.id, end=' ')