-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small changes for v1.2.2: scripts to calculate bkg, schema v4.0.0, small bugfixes
- Loading branch information
Showing
10 changed files
with
243 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
fast_response/precomputed_background/alert_bkg_trials_withprior.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env python | ||
|
||
r""" | ||
Run trials for background, with a given prior. | ||
Record TS, best-fit location, and fitted events | ||
around best fit location | ||
""" | ||
import numpy as np | ||
import healpy as hp | ||
import os, argparse | ||
from astropy.time import Time | ||
import pickle | ||
|
||
from fast_response.AlertFollowup import AlertFollowup | ||
|
||
parser = argparse.ArgumentParser(description='Fast Response Analysis') | ||
parser.add_argument('--deltaT', type=float, default=None, | ||
help='Time Window in seconds') | ||
parser.add_argument('--ntrials', type=int, default = 10000, | ||
help='Trials') | ||
parser.add_argument("--alert_mjd", default=60298.0, type=float, | ||
help="mjd of alert event (default Dec 20, 2023)") | ||
parser.add_argument("--skymap", default=None, type=str, | ||
help='skymap link') | ||
parser.add_argument('--seed', default=1, type=int, | ||
help='Unique seed for running on the cluster') | ||
parser.add_argument('--outdir',type=str, default=None, | ||
help='Output directory to save npz (default = FAST_RESPONSE_OUTPUT env variable or cwd)') | ||
args = parser.parse_args() | ||
|
||
if args.outdir == None: | ||
try: | ||
outdir = os.environ.get('FAST_RESPONSE_OUTPUT') | ||
except: | ||
outdir = './' | ||
else: | ||
outdir = args.outdir | ||
if not os.path.exists(outdir+'/trials/'): | ||
os.mkdir(outdir+'/trials/') | ||
outdir=outdir+'/trials/' | ||
|
||
start_mjd = args.alert_mjd - (args.deltaT / 86400. /2.) | ||
stop_mjd = start_mjd + (args.deltaT / 86400.) | ||
start_iso = Time(start_mjd, format='mjd').iso | ||
stop_iso = Time(stop_mjd, format='mjd').iso | ||
deltaT = args.deltaT / 86400. | ||
|
||
f = AlertFollowup('Precompute_trials_test', args.skymap, | ||
start_iso, stop_iso, save=False) | ||
# f.llh.nbackground=args.bkg*args.deltaT/1000. | ||
inj = f.initialize_injector() #just put this here to initialize f.spatial_prior | ||
# print(f.llh.nbackground) | ||
|
||
ntrials = args.ntrials | ||
stop = ntrials * (args.seed+1) | ||
start = stop-ntrials | ||
seed_counter = start | ||
|
||
npix = hp.nside2npix(f.nside) | ||
|
||
ra, dec, TS_list =[], [], [] | ||
ns, seed =[], [] | ||
|
||
for jj in range(ntrials): | ||
# seed_counter += 1 | ||
seed_counter = np.random.randint(0, 1000000) | ||
val = f.llh.scan(0.0, 0.0, scramble=True, seed = seed_counter, | ||
spatial_prior = f.spatial_prior, | ||
time_mask = [deltaT / 2., (start_mjd + stop_mjd) / 2.], | ||
pixel_scan = [f.nside, f._pixel_scan_nsigma], inject = None) | ||
|
||
try: | ||
maxLoc = np.argmax(val['TS_spatial_prior_0']) #pick out max of all likelihood ratios at diff pixels | ||
except ValueError: | ||
continue | ||
|
||
TS_list.append(val['TS_spatial_prior_0'].max()) | ||
ns.append(val['nsignal'][maxLoc]) | ||
ra.append(val['ra'][maxLoc]) | ||
dec.append(val['dec'][maxLoc]) | ||
# gamma.append(val['gamma'][maxLoc]) #fixed gamma in llh | ||
seed.append(seed_counter) | ||
|
||
print("DONE") | ||
|
||
outfilename = outdir+'seed_delta_t_{:.1e}_{}.npz'.format(args.deltaT, seed_counter) | ||
results={ | ||
'TS_List':TS_list, | ||
'ns_fit':ns, | ||
# 'gamma_fit':gamma, | ||
'ra':ra, | ||
'dec':dec, | ||
'seed':seed | ||
} | ||
print('saving everything') | ||
with open(outfilename, 'wb') as f: | ||
pickle.dump(results, f) | ||
|
||
print("Saved to {}".format(outfilename)) |
90 changes: 90 additions & 0 deletions
90
fast_response/precomputed_background/submit_prior_trials.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
''' | ||
Script to submit a bunch of trials with random seeds | ||
with a given spatial prior | ||
As needed for bkg trials in realtime | ||
''' | ||
|
||
import pycondor | ||
import argparse | ||
import pwd, os | ||
import fast_response | ||
|
||
parser = argparse.ArgumentParser( | ||
description='Submit script') | ||
parser.add_argument( | ||
'--outdir', type=str, required=True, | ||
help = 'Output directory (will make and output to a subdirectory in this directory called trials/)') | ||
parser.add_argument( | ||
'--skymap', type=str, required=True, | ||
help='Skymap path or link') | ||
parser.add_argument( | ||
'--alert_mjd', type=float, required=True, | ||
help='MJD of alert event') | ||
parser.add_argument( | ||
'--deltaT', type=float, default=172800., #[-0.1, +14]day: 1218240 | ||
help='time window to use (in sec)') | ||
parser.add_argument( | ||
'--ntrials', type=int, default=10000, | ||
help='Number of precomputed trials to run, default 30,000') | ||
parser.add_argument( | ||
'--seed_start', type=int,default=0, | ||
help='Seed to start with when running trials') | ||
parser.add_argument( | ||
'--n_per_batch', default=500, type=int, | ||
help='Number of trials to run in each set (default: 500)') | ||
# parser.add_argument( | ||
# '--type', default='alert', type=str, | ||
# help='run alert or GW trials. options are alert (default) or gw') | ||
args = parser.parse_args() | ||
|
||
if not os.path.exists(os.path.join(args.outdir, 'trials')): | ||
os.mkdir(os.path.join(args.outdir, 'trials')) | ||
print('Output trials to directory: {}/trials'.format(args.outdir)) | ||
|
||
fra_dir = os.path.dirname(fast_response.__file__) | ||
script = os.path.join(fra_dir, 'precomputed_background/alert_bkg_trials_withprior.py') | ||
deltaT = args.deltaT | ||
skymap = args.skymap | ||
alert_mjd = args.alert_mjd | ||
|
||
username = pwd.getpwuid(os.getuid())[0] | ||
if not os.path.exists(f'/scratch/{username}/'): | ||
os.mkdir(f'/scratch/{username}/') | ||
if not os.path.exists(f'/scratch/{username}/fra/'): | ||
os.mkdir(f'/scratch/{username}/fra/') | ||
if not os.path.exists(f'/scratch/{username}/fra/condor/'): | ||
os.mkdir(f'/scratch/{username}/fra/condor') | ||
|
||
error = f'/scratch/{username}/fra/condor/error' | ||
output = f'/scratch/{username}/fra/condor/output' | ||
log = f'/scratch/{username}/fra/condor/log' | ||
submit = f'/scratch/{username}/fra/condor/submit' | ||
|
||
### Create Dagman to submit jobs to cluster | ||
job = pycondor.Job( | ||
'alert_precomp_trials', | ||
script, | ||
error=error, | ||
output=output, | ||
log=log, | ||
submit=submit, | ||
getenv=True, | ||
universe='vanilla', | ||
verbose=2, | ||
request_cpus=8,#10, | ||
request_memory=8000,#10000, | ||
extra_lines=[ | ||
'should_transfer_files = YES', | ||
'when_to_transfer_output = ON_EXIT'] | ||
) | ||
|
||
for trial in range(int(args.ntrials / args.n_per_batch)): | ||
job.add_arg(f'--deltaT {deltaT} --ntrials {args.n_per_batch} --outdir {args.outdir} --skymap {skymap} --alert_mjd {alert_mjd}') | ||
|
||
dagman = pycondor.Dagman( | ||
'fra_precompbg', | ||
submit=submit, verbose=2) | ||
|
||
dagman.add_job(job) | ||
dagman.build_submit() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.