code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
# No pixel index, so build one if self.hpx._ipix is None: if self.data.ndim == 2: summed = self.counts.sum(0) if pixels is None: nz = summed.nonzero()[0] else: nz = pixels data_ou...
def explicit_counts_map(self, pixels=None)
return a counts map with explicit index scheme Parameters ---------- pixels : `np.ndarray` or None If set, grab only those pixels. If none, grab only non-zero pixels
4.186845
4.282212
0.97773
if self.hpx._ipix is None: flatarray = self.data.flattern() else: flatarray = self.expanded_counts_map() nz = flatarray.nonzero()[0] data_out = flatarray[nz] return (nz, data_out)
def sparse_counts_map(self)
return a counts map with sparse index scheme
7.8626
7.454524
1.054742
if ebins is None: ebins = self.ebins ectr = self.ectr else: ectr = np.exp(utils.edge_to_center(np.log(ebins))) skydir_cel = skydir.transform_to('icrs') skydir_gal = skydir.transform_to('galactic') sig = [] bkg = [] b...
def compute_counts(self, skydir, fn, ebins=None)
Compute signal and background counts for a point source at position ``skydir`` with spectral parameterization ``fn``. Parameters ---------- skydir : `~astropy.coordinates.SkyCoord` ebins : `~numpy.ndarray` Returns ------- sig : `~numpy.ndarray` ...
2.158823
2.131874
1.012641
sig, bkg, bkg_fit = self.compute_counts(skydir, fn) norms = irfs.compute_norm(sig, bkg, ts_thresh, min_counts, sum_axes=[2, 3], rebin_axes=[10, 1], bkg_fit=bkg_fit) npred = np.squeeze...
def diff_flux_threshold(self, skydir, fn, ts_thresh, min_counts)
Compute the differential flux threshold for a point source at position ``skydir`` with spectral parameterization ``fn``. Parameters ---------- skydir : `~astropy.coordinates.SkyCoord` Sky coordinates at which the sensitivity will be evaluated. fn : `~fermipy.spectru...
3.353403
3.326029
1.00823
ebins = 10**np.linspace(np.log10(self.ebins[0]), np.log10(self.ebins[-1]), 33) ectr = np.sqrt(ebins[0] * ebins[-1]) sig, bkg, bkg_fit = self.compute_counts(skydir, fn, ebins) norms = irfs.compute_norm(sig, bkg, ts_thresh, ...
def int_flux_threshold(self, skydir, fn, ts_thresh, min_counts)
Compute the integral flux threshold for a point source at position ``skydir`` with spectral parameterization ``fn``.
2.246155
2.27228
0.988503
phi = np.radians(lon) theta = (np.pi / 2) - np.radians(lat) sin_t = np.sin(theta) cos_t = np.cos(theta) xVals = sin_t * np.cos(phi) yVals = sin_t * np.sin(phi) zVals = cos_t # Stack them into the output array out = np.vstack((xVals, yVals, zVals)).swapaxes(0, 1) return out
def coords_to_vec(lon, lat)
Converts longitute and latitude coordinates to a unit 3-vector return array(3,n) with v_x[i],v_y[i],v_z[i] = directional cosines
2.423054
2.482932
0.975884
order = int(np.log2(nside)) if order < 0 or order > 13: raise ValueError('HEALPix order must be between 0 to 13 %i' % order) return HPX_ORDER_TO_PIXSIZE[order]
def get_pixel_size_from_nside(nside)
Returns an estimate of the pixel size from the HEALPix nside coordinate This just uses a lookup table to provide a nice round number for each HEALPix order.
4.356793
3.688331
1.181237
x = h.ebins z = np.arange(npix[-1] + 1) return x, z
def hpx_to_axes(h, npix)
Generate a sequence of bin edge vectors corresponding to the axes of a HPX object.
8.942138
8.883152
1.00664
x, z = hpx_to_axes(h, shape) x = np.sqrt(x[0:-1] * x[1:]) z = z[:-1] + 0.5 x = np.ravel(np.ones(shape) * x[:, np.newaxis]) z = np.ravel(np.ones(shape) * z[np.newaxis, :]) return np.vstack((x, z))
def hpx_to_coords(h, shape)
Generate an N x D list of pixel center coordinates where N is the number of pixels and D is the dimensionality of the map.
2.911006
3.100132
0.938994
npix = (int(wcs.wcs.crpix[0] * 2), int(wcs.wcs.crpix[1] * 2)) mult_val = np.ones(npix).T.flatten() sky_crds = hpx.get_sky_coords() pix_crds = wcs.wcs_world2pix(sky_crds, 0).astype(int) ipixs = -1 * np.ones(npix, int).T.flatten() pix_index = npix[1] * pix_crds[0:, 0] + pix_crds[0:, 1] if...
def make_hpx_to_wcs_mapping_centers(hpx, wcs)
Make the mapping data needed to from from HPX pixelization to a WCS-based array Parameters ---------- hpx : `~fermipy.hpx_utils.HPX` The healpix mapping (an HPX object) wcs : `~astropy.wcs.WCS` The wcs mapping (a pywcs.wcs object) Returns ------- ipixs : ar...
2.654225
2.483519
1.068736
npix = (int(wcs.wcs.crpix[0] * 2), int(wcs.wcs.crpix[1] * 2)) pix_crds = np.dstack(np.meshgrid(np.arange(npix[0]), np.arange(npix[1]))).swapaxes(0, 1).reshape((npix[0] * npix[1], 2)) if wcs.wcs.naxis == 2: sky_crds = wcs.wcs_pix2world(pix_crds, 0) else: ...
def make_hpx_to_wcs_mapping(hpx, wcs)
Make the mapping data needed to from from HPX pixelization to a WCS-based array Parameters ---------- hpx : `~fermipy.hpx_utils.HPX` The healpix mapping (an HPX object) wcs : `~astropy.wcs.WCS` The wcs mapping (a pywcs.wcs object) Returns ------- ipixs : ar...
2.940483
2.763163
1.064173
m = re.match(r'([A-Za-z\_]*?)\((.*?)\)', region) if m is None: raise Exception('Failed to parse hpx region string.') if not m.group(1): return re.split(',', m.group(2)) else: return [m.group(1)] + re.split(',', m.group(2))
def parse_hpxregion(region)
Parse the HPX_REG header keyword into a list of tokens.
3.048781
2.906848
1.048827
nside = np.power(2, np.floor(np.log2(upix / 4)) / 2).astype(int) pix = upix - 4 * np.power(nside, 2) return pix, nside
def upix_to_pix(upix)
Get the nside from a unique pixel number.
3.822077
3.229403
1.183524
return cls(nside, nest, coordsys, order, ebins, region=region, conv=conv, pixels=pixels)
def create_hpx(cls, nside, nest, coordsys='CEL', order=-1, ebins=None, region=None, conv=HPX_Conv('FGST_CCUBE'), pixels=None)
Create a HPX object. Parameters ---------- nside : int HEALPix nside paramter nest : bool True for HEALPix "NESTED" indexing scheme, False for "RING" scheme. coordsys : str "CEL" or "GAL" order : int nside = 2**ord...
2.090409
4.704554
0.444337
# Hopefully the file contains the HPX_CONV keyword specifying # the convention used try: return header['HPX_CONV'] except KeyError: pass indxschm = header.get('INDXSCHM', None) # Try based on the EXTNAME keyword extname = header....
def identify_HPX_convention(header)
Identify the convention used to write this file
4.800047
4.787224
1.002679
convname = HPX.identify_HPX_convention(header) conv = HPX_FITS_CONVENTIONS[convname] if conv.convname not in ['GALPROP', 'GALPROP2']: if header["PIXTYPE"] != "HEALPIX": raise Exception("PIXTYPE != HEALPIX") if header["PIXTYPE"] != "HEALPIX": ...
def create_from_header(cls, header, ebins=None, pixels=None)
Creates an HPX object from a FITS header. header : The FITS header ebins : Energy bin edges [optional]
3.084068
3.138178
0.982757
convname = HPX.identify_HPX_convention(hdu.header) conv = HPX_FITS_CONVENTIONS[convname] try: pixels = hdu.data[conv.idxstring] except KeyError: pixels = None return cls.create_from_header(hdu.header, ebins, pixels)
def create_from_hdu(cls, hdu, ebins=None)
Creates an HPX object from a FITS header. hdu : The FITS hdu ebins : Energy bin edges [optional]
6.560545
7.508748
0.87372
cards = [fits.Card("TELESCOP", "GLAST"), fits.Card("INSTRUME", "LAT"), fits.Card(self._conv.coordsys, self._coordsys), fits.Card("PIXTYPE", "HEALPIX"), fits.Card("ORDERING", self.ordering), fits.Card("ORDER", self._ord...
def make_header(self)
Builds and returns FITS header for this HEALPix map
3.299965
3.060696
1.078175
shape = data.shape extname = kwargs.get('extname', self.conv.extname) if shape[-1] != self._npix: raise Exception( "Size of data array does not match number of pixels") cols = [] if self._ipix is not None: cols.append(fits.Column...
def make_hdu(self, data, **kwargs)
Builds and returns a FITs HDU with input data data : The data begin stored Keyword arguments ------------------- extname : The HDU extension name colbase : The prefix for column names
2.610653
2.659608
0.981593
if self._ebins is None: return None cols = [fits.Column("CHANNEL", "I", array=np.arange(1, len(self._ebins + 1))), fits.Column("E_MIN", "1E", unit='keV', array=1000 * self._ebins[0:-1]), fits.Column("E_MAX", "1E", unit='keV...
def make_energy_bounds_hdu(self, extname="EBOUNDS")
Builds and returns a FITs HDU with the energy bin boundries extname : The HDU extension name
2.838362
2.956691
0.959979
if self._evals is None: return None cols = [fits.Column("ENERGY", "1E", unit='MeV', array=self._evals)] hdu = fits.BinTableHDU.from_columns( cols, self.make_header(), name=extname) return hdu
def make_energies_hdu(self, extname="ENERGIES")
Builds and returns a FITs HDU with the energy bin boundries extname : The HDU extension name
3.93822
4.48019
0.87903
hdu_prim = fits.PrimaryHDU() hdu_hpx = self.make_hdu(data, extname=extname) hl = [hdu_prim, hdu_hpx] if self.conv.energy_hdu == 'EBOUNDS': hdu_energy = self.make_energy_bounds_hdu() elif self.conv.energy_hdu == 'ENERGIES': hdu_energy = self.make_e...
def write_fits(self, data, outfile, extname="SKYMAP", clobber=True)
Write input data to a FITS file data : The data begin stored outfile : The name of the output file extname : The HDU extension name clobber : True -> overwrite existing files
2.487215
2.662815
0.934055
tokens = parse_hpxregion(region) if tokens[0] == 'DISK': vec = coords_to_vec(float(tokens[1]), float(tokens[2])) ilist = hp.query_disc(nside, vec[0], np.radians(float(tokens[3])), inclusive=False, nest=nest) elif tokens[0] == 'D...
def get_index_list(nside, nest, region)
Returns the list of pixels indices for all the pixels in a region nside : HEALPix nside parameter nest : True for 'NESTED', False = 'RING' region : HEALPix region string
2.653024
2.530991
1.048216
if region is None: if coordsys == "GAL": c = SkyCoord(0., 0., frame=Galactic, unit="deg") elif coordsys == "CEL": c = SkyCoord(0., 0., frame=ICRS, unit="deg") return c tokens = parse_hpxregion(region) if tokens[0] in [...
def get_ref_dir(region, coordsys)
Finds and returns the reference direction for a given HEALPix region string. region : a string describing a HEALPix region coordsys : coordinate system, GAL | CEL
2.167297
2.111082
1.026628
if region is None: return 180. tokens = parse_hpxregion(region) if tokens[0] in ['DISK', 'DISK_INC']: return float(tokens[3]) elif tokens[0] == 'HPX_PIXEL': pixel_size = get_pixel_size_from_nside(int(tokens[2])) return 2. * pixel_s...
def get_region_size(region)
Finds and returns the approximate size of region (in degrees) from a HEALPix region string.
5.333633
3.932266
1.356377
w = WCS(naxis=naxis) skydir = self.get_ref_dir(self._region, self.coordsys) if self.coordsys == 'CEL': w.wcs.ctype[0] = 'RA---%s' % (proj) w.wcs.ctype[1] = 'DEC--%s' % (proj) w.wcs.crval[0] = skydir.ra.deg w.wcs.crval[1] = skydir.dec.deg ...
def make_wcs(self, naxis=2, proj='CAR', energies=None, oversample=2)
Make a WCS projection appropirate for this HPX pixelization
2.002733
1.985182
1.008841
if self._ipix is None: theta, phi = hp.pix2ang( self._nside, list(range(self._npix)), self._nest) else: theta, phi = hp.pix2ang(self._nside, self._ipix, self._nest) lat = np.degrees((np.pi / 2) - theta) lon = np.degrees(phi) retur...
def get_sky_coords(self)
Get the sky coordinates of all the pixels in this pixelization
3.011593
3.070489
0.980819
theta = np.radians(90. - lats) phi = np.radians(lons) return hp.ang2pix(self.nside, theta, phi, self.nest)
def get_pixel_indices(self, lats, lons)
"Return the indices in the flat array corresponding to a set of coordinates
3.215683
3.213001
1.000835
if self.coordsys in ['CEL', 'EQU']: skydir = skydir.transform_to('icrs') lon = skydir.ra.deg lat = skydir.dec.deg else: skydir = skydir.transform_to('galactic') lon = skydir.l.deg lat = skydir.b.deg return self.get...
def skydir_to_pixel(self, skydir)
Return the pixel index of a SkyCoord object.
2.47321
2.356677
1.049448
from fermipy.skymap import Map hpx_header = self._hpx.make_header() index_map = Map(self.ipixs, self.wcs) mult_map = Map(self.mult_val, self.wcs) prim_hdu = index_map.create_primary_hdu() mult_hdu = index_map.create_image_hdu() for key in ['COORDSYS', 'OR...
def write_to_fitsfile(self, fitsfile, clobber=True)
Write this mapping to a FITS file, to avoid having to recompute it
3.265838
3.287287
0.993475
from fermipy.skymap import Map index_map = Map.create_from_fits(fitsfile) mult_map = Map.create_from_fits(fitsfile, hdu=1) ff = fits.open(fitsfile) hpx = HPX.create_from_hdu(ff[0]) mapping_data = dict(ipixs=index_map.counts, mult_val=m...
def create_from_fitsfile(cls, fitsfile)
Read a fits file and use it to make a mapping
4.414606
4.578504
0.964203
# FIXME, there really ought to be a better way to do this hpx_naxis = len(hpx_data.shape) wcs_naxis = len(wcs_data.shape) if hpx_naxis + 1 != wcs_naxis: raise ValueError("HPX.fill_wcs_map_from_hpx_data: HPX naxis should be 1 less that WCS naxis: %i, %i"%(hp...
def fill_wcs_map_from_hpx_data(self, hpx_data, wcs_data, normalize=True)
Fills the wcs map from the hpx data using the pre-calculated mappings hpx_data : the input HEALPix data wcs_data : the data array being filled normalize : True -> perserve integral by splitting HEALPix values between bins
2.474333
2.513815
0.984294
wcs_data = np.zeros(wcs.npix) self.fill_wcs_map_from_hpx_data(hpx_data, wcs_data, normalize) return wcs_data
def make_wcs_data_from_hpx_data(self, hpx_data, wcs, normalize=True)
Creates and fills a wcs map from the hpx data using the pre-calculated mappings hpx_data : the input HEALPix data wcs : the WCS object normalize : True -> perserve integral by splitting HEALPix values between bins
2.941787
3.314463
0.887561
config = yaml.safe_load(open(configfile)) emin = config['selection']['emin'] emax = config['selection']['emax'] log_emin = np.log10(emin) log_emax = np.log10(emax) ndec = log_emax - log_emin binsperdec = config['binning']['binsperdec'] nebins = int(np.round(binsperdec * ndec)) ...
def _get_enum_bins(configfile)
Get the number of energy bin in the SED Parameters ---------- configfile : str Fermipy configuration file. Returns ------- nbins : int The number of energy bins
2.57969
2.745755
0.93952
nfiles = len(filelist) shape = (nbins, nfiles) outdict = {} for c in collist: outdict[c['name']] = np.ndarray(shape) sys.stdout.write('Working on %i files: ' % nfiles) sys.stdout.flush() for i, f in enumerate(filelist): sys.stdout.write('.') sys.stdout.flush() ...
def fill_output_table(filelist, hdu, collist, nbins)
Fill the arrays from the files in filelist Parameters ---------- filelist : list List of the files to get data from. hdu : str Name of the HDU containing the table with the input data. colllist : list List of the column names nbins : int Number of bins in the...
2.016013
2.177315
0.925917
nfiles = len(filelist) out_tables = [] out_names = [] for hdu in hdus: sys.stdout.write('Working on %i files for %s: ' % (nfiles, hdu)) sys.stdout.flush() tlist = [] for f in filelist: try: tab = Table.read(f, hdu) tlist.ap...
def vstack_tables(filelist, hdus)
vstack a set of HDUs from a set of files Parameters ---------- filelist : list List of the files to get data from. hdus : list Names of the HDU containing the table with the input data. Returns ------- out_tables : list A list with the table with all the requeste...
2.196976
2.298064
0.956012
mean = np.mean(data, axis=0) std = np.std(data, axis=0) median = np.median(data, axis=0) q02, q16, q84, q97 = np.percentile(data, [2.5, 16, 84, 97.5], axis=0) o = dict(mean=mean, std=std, median=median, q02=q02, q16=q16, q84=q84,...
def collect_summary_stats(data)
Collect summary statisitics from an array This creates a dictionry of output arrays of summary statistics, with the input array dimension reducted by one. Parameters ---------- data : `numpy.ndarray` Array with the collected input data Returns ------- output : dict ...
1.789443
1.668093
1.072748
for col in colnames: col_in = table_in[col] stats = collect_summary_stats(col_in.data) for k, v in stats.items(): out_name = "%s_%s" % (col, k) col_out = Column(data=np.vstack( [v]), name=out_name, dtype=col_in.dtype, shape=v.shape, unit=col_in.un...
def add_summary_stats_to_table(table_in, table_out, colnames)
Collect summary statisitics from an input table and add them to an output table Parameters ---------- table_in : `astropy.table.Table` Table with the input data. table_out : `astropy.table.Table` Table with the output data. colnames : list List of the column names to get...
2.758394
2.716614
1.01538
del_cols = ['dnde', 'dnde_err', 'dnde_errp', 'dnde_errn', 'dnde_ul', 'e2dnde', 'e2dnde_err', 'e2dnde_errp', 'e2dnde_errn', 'e2dnde_ul', 'norm', 'norm_err', 'norm_errp', 'norm_errn', 'norm_ul', 'ts'] stats_cols = ['dnde', 'dnde_ul', 'e2dnde',...
def summarize_sed_results(sed_table)
Build a stats summary table for a table that has all the SED results
2.235179
2.170545
1.029778
args = self._parser.parse_args(argv) sedfile = args.sed_file if is_not_null(args.config): configfile = os.path.join(os.path.dirname(sedfile), args.config) else: configfile = os.path.join(os.path.dirname(sedfile), 'config.yaml') nbins = _get_en...
def run_analysis(self, argv)
Run this analysis
4.033673
4.05137
0.995632
job_configs = {} ttype = args['ttype'] (targets_yaml, sim) = NAME_FACTORY.resolve_targetfile( args, require_sim_name=True) if targets_yaml is None: return job_configs write_full = args['write_full'] targets = load_yaml(targets_yaml) ...
def build_job_configs(self, args)
Hook to build job configurations
3.558013
3.548303
1.002737
self.base_dict.update(**yaml.safe_load(open(yamlfile)))
def update_base_dict(self, yamlfile)
Update the values in baseline dictionary used to resolve names
4.415
4.50611
0.979781
kwargs_copy = self.base_dict.copy() kwargs_copy.update(**kwargs) localpath = format_string.format(**kwargs_copy) if kwargs.get('fullpath', False): return self.fullpath(localpath=localpath) return localpath
def _format_from_dict(self, format_string, **kwargs)
Return a formatted file name dictionary components
4.146026
3.879538
1.068691
if 'seed' not in kwargs: kwargs['seed'] = 'SEED' return self._format_from_dict(NameFactory.sim_sedfile_format, **kwargs)
def sim_sedfile(self, **kwargs)
Return the name for the simulated SED file for a particular target
7.543884
7.478336
1.008765
kwargs_copy = self.base_dict.copy() kwargs_copy.update(**kwargs) return NameFactory.stamp_format.format(**kwargs_copy)
def stamp(self, **kwargs)
Return the path for a stamp file for a scatter gather job
6.878784
5.910426
1.163839
sys.stderr.write('Target type must be specified') return (None, None) sim = args.get('sim') if is_null(sim): if require_sim_name: sys.stderr.write('Simulation scenario must be specified') return (None, None) else: ...
def resolve_targetfile(self, args, require_sim_name=False): # x ttype = args.get('ttype') if is_null(ttype)
Get the name of the targetfile based on the job arguments
3.391123
3.41327
0.993512
ttype = args.get('ttype') if is_null(ttype): sys.stderr.write('Target type must be specified') return None name_keys = dict(target_type=ttype, fullpath=True) randconfig = self.randconfig(**name_keys) rand_override = args.g...
def resolve_randconfig(self, args)
Get the name of the specturm file based on the job arguments
4.601144
4.384315
1.049455
usage = "usage: %(prog)s [options] " description = "Run gtselect and gtmktime on one or more FT1 files. " "Note that gtmktime will be skipped if no FT2 file is provided." parser = argparse.ArgumentParser(usage=usage, description=description) add_lsf_args(parser) parser.add_argument('--zmax', ...
def main()
Note that gtmktime will be skipped if no FT2 file is provided.
2.800202
2.667516
1.049741
# Update Column names for colname in list(tab.columns.keys()): newname = colname.lower() newname = newname.replace('dfde', 'dnde') if tab.columns[colname].name == newname: continue tab.columns[colname].name = newname return tab
def convert_sed_cols(tab)
Cast SED column names to lowercase.
4.831482
4.733324
1.020738
from scipy.interpolate import splev return splev(x, self._sp, der=der)
def derivative(self, x, der=1)
return the derivative a an array of input values x : the inputs der : the order of derivative
6.397968
5.979826
1.069925
min_y = np.min(self._interp.y) if self._interp.y[0] == min_y: self._mle = self._interp.x[0] elif self._interp.y[-1] == min_y: self._mle = self._interp.x[-1] else: argmin_y = np.argmin(self._interp.y) ix0 = max(argmin_y - 4, 0) ...
def _compute_mle(self)
Compute the maximum likelihood estimate. Calls `scipy.optimize.brentq` to find the roots of the derivative.
2.063796
1.962
1.051884
mle_val = self.mle() # A little bit of paranoia to avoid zeros if mle_val <= 0.: mle_val = self._interp.xmin if mle_val <= 0.: mle_val = self._interp.x[1] log_mle = np.log10(mle_val) lnl_max = self.fn_mle() # This ultra-safe code ...
def getDeltaLogLike(self, dlnl, upper=True)
Find the point at which the log-likelihood changes by a given value with respect to its value at the MLE.
4.234731
4.129528
1.025476
dlnl = onesided_cl_to_dlnl(1.0 - alpha) return self.getDeltaLogLike(dlnl, upper=upper)
def getLimit(self, alpha, upper=True)
Evaluate the limits corresponding to a C.L. of (1-alpha)%. Parameters ---------- alpha : limit confidence level. upper : upper or lower limits.
12.17295
14.491697
0.839995
dlnl = twosided_cl_to_dlnl(1.0 - alpha) lo_lim = self.getDeltaLogLike(dlnl, upper=False) hi_lim = self.getDeltaLogLike(dlnl, upper=True) return (lo_lim, hi_lim)
def getInterval(self, alpha)
Evaluate the interval corresponding to a C.L. of (1-alpha)%. Parameters ---------- alpha : limit confidence level.
5.411427
5.611829
0.964289
convert_sed_cols(tab_e) try: emin = np.array(tab_e['e_min'].to(u.MeV)) emax = np.array(tab_e['e_max'].to(u.MeV)) except: emin = np.array(tab_e['e_min']) emax = np.array(tab_e['e_max']) ne = len(emin) try: ref_...
def create_from_table(cls, tab_e)
Parameters ---------- tab_e : `~astropy.table.Table` EBOUNDS table.
1.855549
1.835969
1.010665
cols = [ Column(name="E_MIN", dtype=float, data=self._emin, unit='MeV'), Column(name="E_MAX", dtype=float, data=self._emax, unit='MeV'), Column(name="E_REF", dtype=float, data=self._eref, unit='MeV'), Column(name="REF_DNDE", dtype=float, data=self._ref_dn...
def build_ebound_table(self)
Build and return an EBOUNDS table with the encapsulated data.
2.146639
2.090705
1.026754
if len(x.shape) == 1: der_val = np.zeros((1)) else: der_val = np.zeros((x.shape[1:])) for i, xv in enumerate(x): der_val += self._loglikes[i].interp.derivative(xv, der=der) return der_val
def derivative(self, x, der=1)
Return the derivate of the log-like summed over the energy bins Parameters ---------- x : `~numpy.ndarray` Array of N x M values der : int Order of the derivate Returns ------- der_val : `~numpy.ndarray` Array of negat...
3.956387
3.582422
1.104389
mle_vals = np.ndarray((self._nx)) for i in range(self._nx): mle_vals[i] = self._loglikes[i].mle() return mle_vals
def mles(self)
return the maximum likelihood estimates for each of the energy bins
3.92603
3.584162
1.095383
ts_vals = np.ndarray((self._nx)) for i in range(self._nx): ts_vals[i] = self._loglikes[i].TS() return ts_vals
def ts_vals(self)
returns test statistic values for each energy bin
5.372554
4.294322
1.251083
chi2_vals = np.ndarray((self._nx)) for i in range(self._nx): mle = self._loglikes[i].mle() nll0 = self._loglikes[i].interp(mle) nll1 = self._loglikes[i].interp(x[i]) chi2_vals[i] = 2.0 * np.abs(nll0 - nll1) return chi2_vals
def chi2_vals(self, x)
Compute the difference in the log-likelihood between the MLE in each energy bin and the normalization predicted by a global best-fit model. This array can be summed to get a goodness-of-fit chi2 for the model. Parameters ---------- x : `~numpy.ndarray` ...
3.378073
3.628624
0.930952
limit_vals = np.ndarray((self._nx)) for i in range(self._nx): limit_vals[i] = self._loglikes[i].getLimit(alpha, upper) return limit_vals
def getLimits(self, alpha, upper=True)
Evaluate the limits corresponding to a C.L. of (1-alpha)%. Parameters ---------- alpha : float limit confidence level. upper : bool upper or lower limits. returns an array of values, one for each energy bin
4.580461
5.249269
0.87259
limit_vals_lo = np.ndarray((self._nx)) limit_vals_hi = np.ndarray((self._nx)) for i in range(self._nx): lo_lim, hi_lim = self._loglikes[i].getInterval(alpha) limit_vals_lo[i] = lo_lim limit_vals_hi[i] = hi_lim return limit_vals_lo, limit_val...
def getIntervals(self, alpha)
Evaluate the two-sided intervals corresponding to a C.L. of (1-alpha)%. Parameters ---------- alpha : float limit confidence level. Returns ------- limit_vals_hi : `~numpy.ndarray` An array of lower limit values. limit_vals_lo : ...
2.920476
2.768911
1.054738
from scipy.optimize import brentq def fDeriv(x): return self.norm_derivative(specVals, x) try: result = brentq(fDeriv, xlims[0], xlims[1]) except: check_underflow = self.__call__(specVals * xlims[0]) < \ self.__call__(specVals * xlims[1])...
def fitNormalization(self, specVals, xlims)
Fit the normalization given a set of spectral values that define a spectral shape This version is faster, and solves for the root of the derivatvie Parameters ---------- specVals : an array of (nebin values that define a spectral shape xlims : fit limits ...
3.579072
3.626995
0.986787
from scipy.optimize import fmin def fToMin(x): return self.__call__(specVals * x) result = fmin(fToMin, 0., disp=False, xtol=1e-6) return result
def fitNorm_v2(self, specVals)
Fit the normalization given a set of spectral values that define a spectral shape. This version uses `scipy.optimize.fmin`. Parameters ---------- specVals : an array of (nebin values that define a spectral shape xlims : fit limits Returns ---...
4.855884
5.358172
0.906258
if not isinstance(specFunc, SEDFunctor): specFunc = self.create_functor(specFunc, initPars, scale=specFunc.scale) if freePars is None: freePars = np.empty(len(initPars), dtype=bool) freePars.fill(True) init...
def fit_spectrum(self, specFunc, initPars, freePars=None)
Fit for the free parameters of a spectral function Parameters ---------- specFunc : `~fermipy.spectrum.SpectralFunction` The Spectral Function initPars : `~numpy.ndarray` The initial values of the parameters freePars : `~numpy.ndarray` ...
2.713484
2.412549
1.124737
shape = self._norm_vals.shape col_norm = Column(name="norm", dtype=float) col_normv = Column(name="norm_scan", dtype=float, shape=shape) col_dll = Column(name="dloglike_scan", dtype=float, shape=shape) tab = Table(data=...
def build_scandata_table(self)
Build an `astropy.table.Table` object from these data.
4.001551
3.64466
1.097922
n_bins = shape[0] n_vals = shape[1] if weights is None: weights = np.ones((len(components))) norm_vals = np.zeros(shape) nll_vals = np.zeros(shape) nll_offsets = np.zeros((n_bins)) for i in range(n_bins): log_min = np.log10(ylims...
def stack_nll(shape, components, ylims, weights=None)
Combine the log-likelihoods from a number of components. Parameters ---------- shape : tuple The shape of the return array components : `~fermipy.castro.CastroData_Base` The components to be stacked weights : array-like Returns ------...
2.822684
2.723472
1.036429
data = load_yaml(yamlfile) nebins = len(data) emin = np.array([data[i]['emin'] for i in range(nebins)]) emax = np.array([data[i]['emax'] for i in range(nebins)]) ref_flux = np.array([data[i]['flux'][1] for i in range(nebins)]) ref_eflux = np.array([data[i]['eflux...
def create_from_yamlfile(cls, yamlfile)
Create a Castro data object from a yaml file contains the likelihood data.
2.621757
2.570505
1.019938
tab = Table.read(txtfile, format='ascii.ecsv') dnde_unit = u.ph / (u.MeV * u.cm ** 2 * u.s) loge = np.log10(np.array(tab['e_ref'].to(u.MeV))) norm = np.array(tab['norm'].to(dnde_unit)) norm_errp = np.array(tab['norm_errp'].to(dnde_unit)) norm_errn = np.array(tab...
def create_from_flux_points(cls, txtfile)
Create a Castro data object from a text file containing a sequence of differential flux points.
2.651246
2.634306
1.00643
if norm_type in ['flux', 'eflux', 'dnde']: norm_vals = np.array(tab_s['norm_scan'] * tab_e['ref_%s' % norm_type][:, np.newaxis]) elif norm_type == "norm": norm_vals = np.array(tab_s['norm_scan']) else: raise Exception(...
def create_from_tables(cls, norm_type='eflux', tab_s="SCANDATA", tab_e="EBOUNDS")
Create a CastroData object from two tables Parameters ---------- norm_type : str Type of normalization to use. Valid options are: * norm : Normalization w.r.t. to test source * flux : Flux of the test source ( ph cm^-2 s^-1 ) * eflux: Energy Flu...
3.626628
3.649611
0.993703
if irow is not None: tab_s = Table.read(fitsfile, hdu=hdu_scan)[irow] else: tab_s = Table.read(fitsfile, hdu=hdu_scan) tab_e = Table.read(fitsfile, hdu=hdu_energies) tab_s = convert_sed_cols(tab_s) tab_e = convert_sed_cols(tab_e) return ...
def create_from_fits(cls, fitsfile, norm_type='eflux', hdu_scan="SCANDATA", hdu_energies="EBOUNDS", irow=None)
Create a CastroData object from a tscube FITS file. Parameters ---------- fitsfile : str Name of the fits file norm_type : str Type of normalization to use. Valid options are: * norm : Normalization w.r.t. to test source * flux : Flux ...
2.086322
2.314486
0.901419
tab_s = Table.read(fitsfile, hdu=1) tab_s = convert_sed_cols(tab_s) if norm_type in ['flux', 'eflux', 'dnde']: ref_colname = 'ref_%s' % norm_type norm_vals = np.array(tab_s['norm_scan'] * tab_s[ref_colname][:, np.newaxis]) ...
def create_from_sedfile(cls, fitsfile, norm_type='eflux')
Create a CastroData object from an SED fits file Parameters ---------- fitsfile : str Name of the fits file norm_type : str Type of normalization to use, options are: * norm : Normalization w.r.t. to test source * flux : Flux of the te...
3.34353
3.437164
0.972758
if len(components) == 0: return None norm_vals, nll_vals, nll_offsets = CastroData_Base.stack_nll(shape, components, ylims, ...
def create_from_stack(cls, shape, components, ylims, weights=None)
Combine the log-likelihoods from a number of components. Parameters ---------- shape : tuple The shape of the return array components : [~fermipy.castro.CastroData_Base] The components to be stacked weights : array-like Returns ------...
7.457897
6.68621
1.115415
sfn = self.create_functor(specType, scale)[0] return self.__call__(sfn(params))
def spectrum_loglike(self, specType, params, scale=1E3)
return the log-likelihood for a particular spectrum Parameters ---------- specTypes : str The type of spectrum to try params : array-like The spectral parameters scale : float The energy scale or 'pivot' energy
12.533042
18.329472
0.683764
emin = self._refSpec.emin emax = self._refSpec.emax fn = SpectralFunction.create_functor(specType, self.norm_type, emin, emax, ...
def create_functor(self, specType, initPars=None, scale=1E3)
Create a functor object that computes normalizations in a sequence of energy bins for a given spectral model. Parameters ---------- specType : str The type of spectrum to use. This can be a string corresponding to the spectral model class name or a ...
2.862354
2.72143
1.051783
tsmap = WcsNDMap.read(fitsfile) tab_e = Table.read(fitsfile, 'EBOUNDS') tab_s = Table.read(fitsfile, 'SCANDATA') tab_f = Table.read(fitsfile, 'FITDATA') tab_e = convert_sed_cols(tab_e) tab_s = convert_sed_cols(tab_s) tab_f = convert_sed_cols(tab_f) ...
def create_from_fits(cls, fitsfile, norm_type='flux')
Build a TSCube object from a fits file created by gttscube Parameters ---------- fitsfile : str Path to the tscube FITS file. norm_type : str String specifying the quantity used for the normalization
2.884163
2.959054
0.974691
# pix = utils.skydir_to_pix if colwise: ipix = self._tsmap.ipix_swap_axes(ipix, colwise) norm_d = self._norm_vals[ipix] nll_d = self._nll_vals[ipix] return CastroData(norm_d, nll_d, self._refSpec, self._norm_type)
def castroData_from_ipix(self, ipix, colwise=False)
Build a CastroData object for a particular pixel
7.881957
7.486078
1.052882
ipix = self._tsmap.xy_pix_to_ipix(xy, colwise) return self.castroData_from_ipix(ipix)
def castroData_from_pix_xy(self, xy, colwise=False)
Build a CastroData object for a particular pixel
5.469325
5.47583
0.998812
if use_cumul: theMap = self._ts_cumul else: theMap = self._tsmap peaks = find_peaks(theMap, threshold, min_separation) for peak in peaks: o, skydir = fit_error_ellipse(theMap, (peak['ix'], peak['iy']), ...
def find_and_refine_peaks(self, threshold, min_separation=1.0, use_cumul=False)
Run a simple peak-finding algorithm, and fit the peaks to paraboloids to extract their positions and error ellipses. Parameters ---------- threshold : float Peak threshold in TS. min_separation : float Radius of region size in degrees. Sets the minimum ...
5.124745
4.46765
1.147078
lats = np.degrees(np.arcsin(cvects[2])) lons = np.degrees(np.arctan2(cvects[0], cvects[1])) return np.hstack([lats, lons])
def make_lat_lons(cvects)
Convert from directional cosines to latitidue and longitude Parameters ---------- cvects : directional cosine (i.e., x,y,z component) values returns (np.ndarray(2,nsrc)) with the directional cosine (i.e., x,y,z component) values
1.95396
2.248373
0.869055
lon_rad = np.radians(lon_vect) lat_rad = np.radians(lat_vect) cvals = np.cos(lat_rad) xvals = cvals * np.sin(lon_rad) yvals = cvals * np.cos(lon_rad) zvals = np.sin(lat_rad) cvects = np.vstack([xvals, yvals, zvals]) return cvects
def make_cos_vects(lon_vect, lat_vect)
Convert from longitude (RA or GLON) and latitude (DEC or GLAT) values to directional cosines Parameters ---------- lon_vect,lat_vect : np.ndarray(nsrc) Input values returns (np.ndarray(3,nsrc)) with the directional cosine (i.e., x,y,z component) values
1.926178
1.958291
0.983601
dist_rad = np.radians(cut_dist) cos_t_cut = np.cos(dist_rad) nsrc = cos_vects.shape[1] match_dict = {} for i, v1 in enumerate(cos_vects.T): cos_t_vect = (v1 * cos_vects.T).sum(1) cos_t_vect[cos_t_vect < -1.0] = -1.0 cos_t_vect[cos_t_vect > 1.0] = 1.0 mask = cos_t...
def find_matches_by_distance(cos_vects, cut_dist)
Find all the pairs of sources within a given distance of each other. Parameters ---------- cos_vects : np.ndarray(e,nsrc) Directional cosines (i.e., x,y,z component) values of all the sources cut_dist : float Angular cut in degrees that will be used to select pairs ...
3.059635
2.935464
1.0423
match_dict = {} sig_2_vect = unc_vect * unc_vect for i, v1 in enumerate(cos_vects.T): cos_t_vect = (v1 * cos_vects.T).sum(1) cos_t_vect[cos_t_vect < -1.0] = -1.0 cos_t_vect[cos_t_vect > 1.0] = 1.0 sig_2_i = sig_2_vect[i] acos_t_vect = np.degrees(np.arccos(cos_t_v...
def find_matches_by_sigma(cos_vects, unc_vect, cut_sigma)
Find all the pairs of sources within a given distance of each other. Parameters ---------- cos_vects : np.ndarray(3,nsrc) Directional cosines (i.e., x,y,z component) values of all the sources unc_vect : np.ndarray(nsrc) Uncertainties on the source positions cut_sigma : flo...
2.592814
2.784271
0.931236
e_matrix = np.zeros((nsrcs, nsrcs)) for k, v in match_dict.items(): e_matrix[k[0], k[1]] = v return e_matrix
def fill_edge_matrix(nsrcs, match_dict)
Create and fill a matrix with the graph 'edges' between sources. Parameters ---------- nsrcs : int number of sources (used to allocate the size of the matrix) match_dict : dict((int,int):float) Each entry gives a pair of source indices, and the corresponding measure (eit...
1.954373
2.13327
0.916139
rev_dict = {} for k, v in cdict.items(): if k in rev_dict: rev_dict[k][k] = True else: rev_dict[k] = {k: True} for vv in v.keys(): if vv in rev_dict: rev_dict[vv][k] = True else: rev_dict[vv] = {k: True}...
def make_rev_dict_unique(cdict)
Make a reverse dictionary Parameters ---------- in_dict : dict(int:dict(int:True)) A dictionary of clusters. Each cluster is a source index and the dictionary of other sources in the cluster. Returns ------- rev_dict : dict(int:dict(int:True)) A dictionary pointin...
1.737533
1.800366
0.9651
iv0, iv1 = span_tree.nonzero() # This is the dictionary of all the pairings for each source match_dict = {} for i0, i1 in zip(iv0, iv1): d = span_tree[i0, i1] # Cut on the link distance if d > cut_value: continue imin = int(min(i0, i1)) imax = ...
def make_clusters(span_tree, cut_value)
Find clusters from the spanning tree Parameters ---------- span_tree : a sparse nsrcs x nsrcs array Filled with zeros except for the active edges, which are filled with the edge measures (either distances or sigmas cut_value : float Value used to cluster group. All links with mea...
2.982648
2.902023
1.027782
best_idx = idx_key best_measure = measure_vect[idx_key] out_list = [idx_key] + idx_list for idx, measure in zip(idx_list, measure_vect[idx_list]): if measure < best_measure: best_idx = idx best_measure = measure out_list.remove(best_idx) return best_idx, out_...
def select_from_cluster(idx_key, idx_list, measure_vect)
Select a single source from a cluster and make it the new cluster key Parameters ---------- idx_key : int index of the current key for a cluster idx_list : [int,...] list of the other source indices in the cluster measure_vect : np.narray((nsrc),float) vector of the measure used...
2.234154
2.067178
1.080775
if weights is None: weighted = cvects.T[idx_list].sum(0) sum_weights = float(len(idx_list)) else: weighted = ((cvects * weights).T[idx_list]).sum(0) sum_weights = weights.sum(0) weighted /= sum_weights # make sure it is normalized norm = np.sqrt((weighted * weigh...
def find_centroid(cvects, idx_list, weights=None)
Find the centroid for a set of vectors Parameters ---------- cvects : ~numpy.ndarray(3,nsrc) with directional cosine (i.e., x,y,z component) values idx_list : [int,...] list of the source indices in the cluster weights : ~numpy.ndarray(nsrc) with the weights to use. None for equal weightin...
3.049527
3.091002
0.986582
ret_val = np.zeros((n_src), int) for i in range(n_src): try: key = rev_dict[i] except KeyError: key = i try: n = len(cdict[key]) except: n = 0 ret_val[i] = n return ret_val
def count_sources_in_cluster(n_src, cdict, rev_dict)
Make a vector of sources in each cluster Parameters ---------- n_src : number of sources cdict : dict(int:[int,]) A dictionary of clusters. Each cluster is a source index and the list of other source in the cluster. rev_dict : dict(int:int) A single valued dictio...
2.390785
2.293396
1.042465
centroid = find_centroid(cvects, idx_list, weights) dist_vals = np.degrees(np.arccos((centroid * cvects.T[idx_list]).sum(1))) return dist_vals, centroid
def find_dist_to_centroid(cvects, idx_list, weights=None)
Find the centroid for a set of vectors Parameters ---------- cvects : ~numpy.ndarray(3,nsrc) with directional cosine (i.e., x,y,z component) values idx_list : [int,...] list of the source indices in the cluster weights : ~numpy.ndarray(nsrc) with the weights to use. None for equal weightin...
3.57546
4.248931
0.841496
distances = np.zeros((cvects.shape[1])) cent_dict = {} for k, v in cluster_dict.items(): l = [k] + v distances[l], centroid = find_dist_to_centroid(cvects, l, weights) cent_dict[k] = make_lat_lons(centroid) return distances, cent_dict
def find_dist_to_centroids(cluster_dict, cvects, weights=None)
Find the centroids and the distances to the centroid for all sources in a set of clusters Parameters ---------- cluster_dict : dict(int:[int,...]) Each cluster is a source index and the list of other sources in the cluster. cvects : np.ndarray(3,nsrc) Directional cosines (i.e., x...
3.611482
3.510965
1.02863
out_dict = {} for idx_key, idx_list in cluster_dict.items(): out_idx, out_list = select_from_cluster( idx_key, idx_list, measure_vect) out_dict[out_idx] = out_list return out_dict
def select_from_clusters(cluster_dict, measure_vect)
Select a single source from each cluster and make it the new cluster key cluster_dict : dict(int:[int,]) A dictionary of clusters. Each cluster is a source index and the list of other source in the cluster. measure_vect : np.narray((nsrc),float) vector of the measure used to select ...
2.926822
2.895704
1.010746
out_dict = {} for k, v in in_dict.items(): for vv in v: if vv in out_dict: if warn: print("Dictionary collision %i" % vv) out_dict[vv] = k return out_dict
def make_reverse_dict(in_dict, warn=True)
Build a reverse dictionary from a cluster dictionary Parameters ---------- in_dict : dict(int:[int,]) A dictionary of clusters. Each cluster is a source index and the list of other source in the cluster. Returns ------- out_dict : dict(int:int) A single valued d...
2.652851
3.815572
0.69527
out_array = -1 * np.ones((n_src), int) for k, v in rev_dict.items(): out_array[k] = v # We need this to make sure the see source points at itself out_array[v] = v return out_array
def make_cluster_vector(rev_dict, n_src)
Converts the cluster membership dictionary to an array Parameters ---------- rev_dict : dict(int:int) A single valued dictionary pointing from source index to cluster key for each source in a cluster. n_src : int Number of source in the array Returns ------- o...
5.85651
6.257675
0.935892
out_array = np.where(cluster_vect >= 0, src_names[cluster_vect], "") return out_array
def make_cluster_name_vector(cluster_vect, src_names)
Converts the cluster membership dictionary to an array Parameters ---------- cluster_vect : `numpy.ndarray' An array filled with the index of the seed of a cluster if a source belongs to a cluster, and with -1 if it does not. src_names : An array with the source names Ret...
3.931229
3.555522
1.105668
out_dict = {} for i, k in enumerate(in_array): if k < 0: continue try: out_dict[k].append(i) except KeyError: out_dict[k] = [i] return out_dict
def make_dict_from_vector(in_array)
Converts the cluster membership array stored in a fits file back to a dictionary Parameters ---------- in_array : `np.ndarray' An array filled with the index of the seed of a cluster if a source belongs to a cluster, and with -1 if it does not. Returns ------- returns dict(int:...
2.122726
2.368407
0.896268
nsrcs = len(tab) mask = np.zeros((nsrcs), '?') mask[to_remove] = True inv_mask = np.invert(mask) out_tab = tab[inv_mask] return out_tab
def filter_and_copy_table(tab, to_remove)
Filter and copy a FITS table. Parameters ---------- tab : FITS Table object to_remove : [int ...} list of indices to remove from the table returns FITS Table object
4.003784
4.275804
0.936381
gta.free_sources(False) gta.write_roi('base_roi', make_plots=make_plots) gta.free_sources(True, minmax_npred=[1e3, np.inf]) gta.optimize() gta.free_sources(False) gta.print_roi()
def baseline_roi_fit(gta, make_plots=False, minmax_npred=[1e3, np.inf])
Do baseline fitting for a target Region of Interest Parameters ---------- gta : `fermipy.gtaanalysis.GTAnalysis` The analysis object make_plots : bool Flag to make standard analysis plots minmax_npred : tuple or list Range of number of predicted coutns for which to free s...
4.302209
4.490058
0.958163
# for s in gta.roi.sources: if not src['SpatialModel'] == 'PointSource': continue if src['offset_roi_edge'] > -0.1: continue gta.localize(src.name, **kwargs) gta.optimize() gta.print_roi()
def localize_sources(gta, **kwargs): # Localize all point sources for src in sorted(gta.roi.sources, key=lambda t: t['ts'], reverse=True)
Relocalize sources in the region of interest Parameters ---------- gta : `fermipy.gtaanalysis.GTAnalysis` The analysis object kwargs : These are passed to the gta.localize function
7.450588
8.825889
0.844174
if gta.roi.has_source(name): gta.zero_source(name) gta.update_source(name) test_src_name = "%s_test" % name else: test_src_name = name gta.add_source(test_src_name, src_dict) gta.free_norm(test_src_name) gta.free_shape(test_src_name, free=False) fit_resu...
def add_source_get_correlated(gta, name, src_dict, correl_thresh=0.25, non_null_src=False)
Add a source and get the set of correlated sources Parameters ---------- gta : `fermipy.gtaanalysis.GTAnalysis` The analysis object name : str Name of the source we are adding src_dict : dict Dictionary of the source parameters correl_thresh : float Threshold...
3.133033
2.768089
1.131839