Instruction:
Write a Python 3 script for
reading and returns the energy bin edges from a FITs HDU

Response:
def read_energy_bounds(hdu):
    """ Reads and returns the energy bin edges from a FITs HDU
    """
    nebins = len(hdu.data)
    ebin_edges = np.ndarray((nebins + 1))
    try:
        ebin_edges[0:-1] = np.log10(hdu.data.field("E_MIN")) - 3.
        ebin_edges[-1] = np.log10(hdu.data.field("E_MAX")[-1]) - 3.
    except KeyError:
        ebin_edges[0:-1] = np.log10(hdu.data.field("energy_MIN"))
        ebin_edges[-1] = np.log10(hdu.data.field("energy_MAX")[-1])
    return ebin_edges


Instruction:
How would you explain what the following Python 3 function does
def read_spectral_data(hdu):
    ebins = read_energy_bounds(hdu)
    fluxes = np.ndarray((len(ebins)))
    try:
        fluxes[0:-1] = hdu.data.field("E_MIN_FL")
        fluxes[-1] = hdu.data.field("E_MAX_FL")[-1]
        npreds = hdu.data.field("NPRED")
    except:
        fluxes = np.ones((len(ebins)))
        npreds = np.ones((len(ebins)))
    return ebins, fluxes, npreds

Response:
Reads and returns the energy bin edges fluxes and npreds from
    a FITs HDU


Instruction:
Create a Python 3 function to
build and returns a FITs HDU with the energy values

Response:
def make_energies_hdu(energy_vals, extname="ENERGIES"):
    """ Builds and returns a FITs HDU with the energy values

    extname   : The HDU extension name
    """
    cols = [fits.Column("Energy", "D", unit='MeV', array=energy_vals)]
    hdu = fits.BinTableHDU.from_columns(cols, name=extname)
    return hdu


Instruction:
Implement a function in Python 3 to
read a WCS or HPX projection from a FITS file.

Response:
def read_projection_from_fits(fitsfile, extname=None):
    """
    Load a WCS or HPX projection.
    """
    f = fits.open(fitsfile)
    nhdu = len(f)
    # Try and get the energy bounds
    try:
        ebins = find_and_read_ebins(f)
    except:
        ebins = None

    if extname is None:
        # If there is an image in the Primary HDU we can return a WCS-based
        # projection
        if f[0].header['NAXIS'] != 0:
            proj = WCS(f[0].header)
            return proj, f, f[0]
    else:
        if f[extname].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[extname].header)
            return proj, f, f[extname]
        elif extname in ['SKYMAP', 'SKYMAP2']:
            proj = HPX.create_from_hdu(f[extname], ebins)
            return proj, f, f[extname]
        elif f[extname].header['XTENSION'] == 'BINTABLE':
            try:
                if f[extname].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[extname], ebins)
                    return proj, f, f[extname]
            except:
                pass
        return None, f, None

    # Loop on HDU and look for either an image or a table with HEALPix data
    for i in range(1, nhdu):
        # if there is an image we can return a WCS-based projection
        if f[i].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[i].header)
            return proj, f, f[i]
        elif f[i].header['XTENSION'] == 'BINTABLE':
            if f[i].name in ['SKYMAP', 'SKYMAP2']:
                proj = HPX.create_from_hdu(f[i], ebins)
                return proj, f, f[i]
            try:
                if f[i].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[i], ebins)
                    return proj, f, f[i]
            except:
                pass

    return None, f, None


Instruction:
Create a Python 3 function for
writing a list of astropy. table. Table objects to a single FITS file.

Response:
def write_tables_to_fits(filepath, tablelist, clobber=False,
                         namelist=None, cardslist=None, hdu_list=None):
    """
    Write some astropy.table.Table objects to a single fits file
    """
    outhdulist = [fits.PrimaryHDU()]
    rmlist = []
    for i, table in enumerate(tablelist):
        ft_name = "%s._%i" % (filepath, i)
        rmlist.append(ft_name)
        try:
            os.unlink(ft_name)
        except:
            pass
        table.write(ft_name, format="fits")
        ft_in = fits.open(ft_name)
        if namelist:
            ft_in[1].name = namelist[i]
        if cardslist:
            for k, v in cardslist[i].items():
                ft_in[1].header[k] = v
        ft_in[1].update()
        outhdulist += [ft_in[1]]

    if hdu_list is not None:
        for h in hdu_list:
            outhdulist.append(h)

    fits.HDUList(outhdulist).writeto(filepath, overwrite=clobber)
    for rm in rmlist:
        os.unlink(rm)


Instruction:
How would you explain what the following Python 3 function does
def update_docstring(docstring, options_dict):

    options_str = []
    for i, (k, v) in enumerate(sorted(options_dict.items())):

        option_str = ''
        if i == 0:
            option_str += '%s : %s\n' % (k, v[2].__name__)
        else:
            option_str += ' ' * 8 + '%s : %s\n' % (k, v[2].__name__)

        option_doc = v[1]
        option_doc += ' (default : %s)' % v[0]
        option_doc = textwrap.wrap(option_doc, 72 - 12)
        option_str += ' ' * 12 + ('\n' + ' ' * 12).join(option_doc)
        options_str += [option_str]

    options_str = '\n\n'.join(options_str)

    return docstring.format(options=options_str)

Response: Update a method docstring by inserting option docstrings defined in the options dictionary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def convolve_map(m, k, cpix, threshold=0.001, imin=0, imax=None, wmap=None):
    islice = slice(imin, imax)

    o = np.zeros(m[islice, ...].shape)
    ix = int(cpix[0])
    iy = int(cpix[1])

    # Loop over energy
    for i in range(m[islice, ...].shape[0]):

        ks = k[islice, ...][i, ...]
        ms = m[islice, ...][i, ...]

        mx = ks[ix, :] > ks[ix, iy] * threshold
        my = ks[:, iy] > ks[ix, iy] * threshold

        nx = int(max(3, np.round(np.sum(mx) / 2.)))
        ny = int(max(3, np.round(np.sum(my) / 2.)))

        # Ensure that there is an odd number of pixels in the kernel
        # array
        if ix + nx + 1 >= ms.shape[0] or ix - nx < 0:
            nx -= 1
            ny -= 1

        sx = slice(ix - nx, ix + nx + 1)
        sy = slice(iy - ny, iy + ny + 1)

        ks = ks[sx, sy]

#        origin = [0, 0]
#        if ks.shape[0] % 2 == 0: origin[0] += 1
#        if ks.shape[1] % 2 == 0: origin[1] += 1
#        o[i,...] = ndimage.convolve(ms, ks, mode='constant',
#                                     origin=origin, cval=0.0)

        o[i, ...] = scipy.signal.fftconvolve(ms, ks, mode='same')
        if wmap is not None:
            o[i, ...] *= wmap[islice, ...][i, ...]

    return o

Response: This function performs an energy - dependent convolution on a sequence of 3 - D spatial maps.


Instruction:
Create a Python 3 function for
performing an energy - dependent convolution on a sequence of HEALPix maps.

Response:
def convolve_map_hpx_gauss(m, sigmas, imin=0, imax=None, wmap=None):
    """
    Perform an energy-dependent convolution on a sequence of 2-D spatial maps.

    Parameters
    ----------

    m : `HpxMap`
       2-D map containing a sequence of 1-D HEALPix maps.  First
       dimension should be energy.

    sigmas : `~numpy.ndarray`
       1-D map containing a sequence gaussian widths for smoothing

    imin : int
       Minimum index in energy dimension.

    imax : int
       Maximum index in energy dimension.

    wmap :  `~numpy.ndarray`
       2-D map containing a sequence of 1-D HEALPix maps of weights.  First
       dimension should be energy. This map should have the same dimension as m.

    """
    islice = slice(imin, imax)

    o = np.zeros(m.data.shape)

    nside = m.geom.nside
    nest = m.geom.nest

    # Loop over energy
    for i, ms in enumerate(m.data[islice, ...]):
        sigma = sigmas[islice][i]
        # Need to be in RING scheme
        if nest:
            ms = hp.pixelfunc.reorder(ms, n2r=True)

        o[islice, ...][i] = hp.sphtfunc.smoothing(ms, sigma=sigma)
        if nest:
            o[islice, ...][i] = hp.pixelfunc.reorder(
                o[islice, ...][i], r2n=True)
        if wmap is not None:
            o[islice, ...][i] *= wmap.data[islice, ...][i]

    return HpxNDMap(m.geom, o)


Instruction:
How would you explain what the following Python 3 function does
def get_source_kernel(gta, name, kernel=None):

    sm = []
    zs = 0
    for c in gta.components:
        z = c.model_counts_map(name).data.astype('float')
        if kernel is not None:
            shape = (z.shape[0],) + kernel.shape
            z = np.apply_over_axes(np.sum, z, axes=[1, 2]) * np.ones(
                shape) * kernel[np.newaxis, :, :]
            zs += np.sum(z)
        else:
            zs += np.sum(z)

        sm.append(z)

    sm2 = 0
    for i, m in enumerate(sm):
        sm[i] /= zs
        sm2 += np.sum(sm[i] ** 2)

    for i, m in enumerate(sm):
        sm[i] /= sm2

    return sm

Response: Get the PDF for the given source.


Instruction:
Here you have a function in Python 3, explain what it does
def residmap(self, prefix='', **kwargs):
        timer = Timer.create(start=True)
        self.logger.info('Generating residual maps')

        schema = ConfigSchema(self.defaults['residmap'])

        config = schema.create_config(self.config['residmap'], **kwargs)

        # Defining default properties of test source model
        config['model'].setdefault('Index', 2.0)
        config['model'].setdefault('SpectrumType', 'PowerLaw')
        config['model'].setdefault('SpatialModel', 'PointSource')
        config['model'].setdefault('Prefactor', 1E-13)

        o = self._make_residual_map(prefix, **config)

        if config['make_plots']:
            plotter = plotting.AnalysisPlotter(self.config['plotting'],
                                               fileio=self.config['fileio'],
                                               logging=self.config['logging'])

            plotter.make_residmap_plots(o, self.roi)

        self.logger.info('Finished residual maps')

        outfile = utils.format_filename(self.workdir, 'residmap',
                                        prefix=[o['name']])

        if config['write_fits']:
            o['file'] = os.path.basename(outfile) + '.fits'
            self._make_residmap_fits(o, outfile + '.fits')

        if config['write_npy']:
            np.save(outfile + '.npy', o)

        self.logger.info('Execution time: %.2f s', timer.elapsed_time)
        return o

Response:
Generate 2 - D spatial residual maps for the current ROI and the convolution kernel defined with the model
        argument.


Instruction:
Can you tell what is the following Python 3 function doing
def create(appname, **kwargs):

        if appname in LinkFactory._class_dict:
            return LinkFactory._class_dict[appname].create(**kwargs)
        else:
            raise KeyError(
                "Could not create object associated to app %s" % appname)

Response: Create a Link of a particular class using the kwargs as options


Instruction:
Create a Python 3 function for
mapping from the top - level arguments to the arguments provided to the indiviudal links

Response:
def _map_arguments(self, args):
        """Map from the top-level arguments to the arguments provided to
        the indiviudal links """
        comp_file = args.get('comp', None)
        datafile = args.get('data', None)
        if is_null(comp_file):
            return
        if is_null(datafile):
            return

        NAME_FACTORY.update_base_dict(datafile)

        outdir = args.get('outdir', None)
        outkey = args.get('outkey', None)
        ft1file = args['ft1file']

        if is_null(outdir) or is_null(outkey):
            return
        pfiles = os.path.join(outdir, outkey)

        self.comp_dict = yaml.safe_load(open(comp_file))
        coordsys = self.comp_dict.pop('coordsys')
        full_out_dir = make_nfs_path(os.path.join(outdir, outkey))

        for key_e, comp_e in sorted(self.comp_dict.items()):
            emin = math.pow(10., comp_e['log_emin'])
            emax = math.pow(10., comp_e['log_emax'])
            enumbins = comp_e['enumbins']
            zmax = comp_e['zmax']
            zcut = "zmax%i" % comp_e['zmax']
            evclassstr = NAME_FACTORY.base_dict['evclass']
            kwargs_select = dict(zcut=zcut,
                                 ebin=key_e,
                                 psftype='ALL',
                                 coordsys=coordsys,
                                 mktime='none')
            selectfile_energy = make_full_path(outdir, outkey, NAME_FACTORY.select(**kwargs_select))
            linkname = 'select-energy-%s-%s' % (key_e, zcut)
            self._set_link(linkname, Gtlink_select,
                           infile=ft1file,
                           outfile=selectfile_energy,
                           zmax=zmax,
                           emin=emin,
                           emax=emax,
                           evclass=NAME_FACTORY.evclassmask(evclassstr),
                           pfiles=pfiles,
                           logfile=os.path.join(full_out_dir, "%s.log" % linkname))

            if 'evtclasses' in comp_e:
                evtclasslist_vals = comp_e['evtclasses']
            else:
                evtclasslist_vals = [NAME_FACTORY.base_dict['evclass']]

            for evtclassval in evtclasslist_vals:
                for psf_type, psf_dict in sorted(comp_e['psf_types'].items()):
                    linkname_select = 'select-type-%s-%s-%s-%s' % (
                        key_e, zcut, evtclassval, psf_type)
                    linkname_bin = 'bin-%s-%s-%s-%s' % (key_e, zcut, evtclassval, psf_type)
                    hpx_order = psf_dict['hpx_order']
                    kwargs_bin = kwargs_select.copy()
                    kwargs_bin['psftype'] = psf_type
                    selectfile_psf = make_full_path(
                        outdir, outkey, NAME_FACTORY.select(**kwargs_bin))
                    binfile = make_full_path(outdir, outkey, NAME_FACTORY.ccube(**kwargs_bin))
                    self._set_link(linkname_select, Gtlink_select,
                                   infile=selectfile_energy,
                                   outfile=selectfile_psf,
                                   zmax=zmax,
                                   emin=emin,
                                   emax=emax,
                                   evtype=EVT_TYPE_DICT[psf_type],
                                   evclass=NAME_FACTORY.evclassmask(evtclassval),
                                   pfiles=pfiles,
                                   logfile=os.path.join(full_out_dir, "%s.log" % linkname_select))

                    self._set_link(linkname_bin, Gtlink_bin,
                                   coordsys=coordsys,
                                   hpx_order=hpx_order,
                                   evfile=selectfile_psf,
                                   outfile=binfile,
                                   emin=emin,
                                   emax=emax,
                                   enumbins=enumbins,
                                   pfiles=pfiles,
                                   logfile=os.path.join(full_out_dir, "%s.log" % linkname_bin))


Instruction:
Create a Python 3 function to
map from the top - level arguments to the arguments provided to the indiviudal links

Response:
def _map_arguments(self, args):
        """Map from the top-level arguments to the arguments provided to
        the indiviudal links """
        data = args.get('data')
        comp = args.get('comp')
        ft1file = args.get('ft1file')
        scratch = args.get('scratch', None)
        dry_run = args.get('dry_run', None)

        self._set_link('split-and-bin', SplitAndBin_SG,
                       comp=comp, data=data,
                       hpx_order_max=args.get('hpx_order_ccube', 9),
                       ft1file=ft1file,
                       scratch=scratch,
                       dry_run=dry_run)

        self._set_link('coadd-split', CoaddSplit_SG,
                       comp=comp, data=data,
                       ft1file=ft1file)

        self._set_link('expcube2', Gtexpcube2_SG,
                       comp=comp, data=data,
                       hpx_order_max=args.get('hpx_order_expcube', 5),
                       dry_run=dry_run)


Instruction:
Can you tell what is the following Python 3 function doing
def _replace_none(self, aDict):
        for k, v in aDict.items():
            if v is None:
                aDict[k] = 'none'

Response: Replace all None values in a dict with none


Instruction:
Implement a function in Python 3 to
get the name of IFRs associated with a particular dataset

Response:
def irfs(self, **kwargs):
        """ Get the name of IFRs associted with a particular dataset
        """
        dsval = kwargs.get('dataset', self.dataset(**kwargs))
        tokens = dsval.split('_')
        irf_name = "%s_%s_%s" % (DATASET_DICTIONARY['%s_%s' % (tokens[0], tokens[1])],
                                 EVCLASS_NAME_DICTIONARY[tokens[3]],
                                 kwargs.get('irf_ver'))
        return irf_name


Instruction:
Create a Python 3 function to
return a key that specifies the data selection

Response:
def dataset(self, **kwargs):
        """ Return a key that specifies the data selection
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        try:
            return NameFactory.dataset_format.format(**kwargs_copy)
        except KeyError:
            return None


Instruction:
Can you implement a function in Python 3 that
returns a key that specifies data the sub - selection

Response:
def component(self, **kwargs):
        """ Return a key that specifies data the sub-selection
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        try:
            return NameFactory.component_format.format(**kwargs_copy)
        except KeyError:
            return None


Instruction:
Write a Python 3 function that can
return a key that specifies the name and version of a source or component

Response:
def sourcekey(self, **kwargs):
        """ Return a key that specifies the name and version of a source or component
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        try:
            return NameFactory.sourcekey_format.format(**kwargs_copy)
        except KeyError:
            return None


Instruction:
How would you implement a function in Python 3 that
returns the sourcekey for galprop input maps

Response:
def galprop_ringkey(self, **kwargs):
        """ return the sourcekey for galprop input maps : specifies the component and ring
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        try:
            return NameFactory.galprop_ringkey_format.format(**kwargs_copy)
        except KeyError:
            return None


Instruction:
How would you explain what the following Python 3 function does
def galprop_sourcekey(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        try:
            return NameFactory.galprop_sourcekey_format.format(**kwargs_copy)
        except KeyError:
            return None

Response: return the sourcekey for merged galprop maps


Instruction:
Write a Python 3 function that can
return the sourcekey for merged sets of point sources

Response:
def merged_sourcekey(self, **kwargs):
        """ return the sourcekey for merged sets of point sources :
        specifies the catalog and merging rule
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        try:
            return NameFactory.merged_sourcekey_format.format(**kwargs_copy)
        except KeyError:
            return None


Instruction:
Create a Python 3 function to
return the file name for Galprop input gasmaps

Response:
def galprop_gasmap(self, **kwargs):
        """ return the file name for Galprop input gasmaps
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.galprop_gasmap_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Write a Python 3 function for
returning the file name for Galprop merged gasmaps

Response:
def merged_gasmap(self, **kwargs):
        """ return the file name for Galprop merged gasmaps
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.merged_gasmap_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Can you generate a brief explanation for the following Python 3 code
def diffuse_template(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.diffuse_template_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the file name for other diffuse map templates


Instruction:
Can you generate a brief explanation for the following Python 3 code
def spectral_template(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        localpath = NameFactory.spectral_template_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the file name for spectral templates


Instruction:
Can you generate the documentation for the following Python 3 function
def srcmdl_xml(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        localpath = NameFactory.srcmdl_xml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the file name for source model xml files


Instruction:
Implement a function in Python 3 to
return the file name for source model xml files of nested sources

Response:
def nested_srcmdl_xml(self, **kwargs):
        """ return the file name for source model xml files of nested sources
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.nested_srcmdl_xml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Can you tell what is the following Python 3 function doing
def ft1file(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.ft1file_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of the input ft1 file list


Instruction:
Can you tell what is the following Python 3 function doing
def ft2file(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['data_time'] = kwargs.get(
            'data_time', self.dataset(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.ft2file_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of the input ft2 file list


Instruction:
Create a Python 3 function for
returning the name of a livetime cube file

Response:
def ltcube(self, **kwargs):
        """ return the name of a livetime cube file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        localpath = NameFactory.ltcube_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Write a Python 3 script to
return the name of a selected events ft1file

Response:
def select(self, **kwargs):
        """ return the name of a selected events ft1file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.select_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Can you tell what is the following Python 3 function doing
def mktime(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.mktime_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a selected events ft1file


Instruction:
How would you explain what the following Python 3 function does
def ccube(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        localpath = NameFactory.ccube_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a counts cube file


Instruction:
Implement a Python 3 function for
returning the name of a binned exposure cube file

Response:
def bexpcube(self, **kwargs):
        """ return the name of a binned exposure cube file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.bexpcube_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Implement a function in Python 3 to
return the name of a source map file

Response:
def srcmaps(self, **kwargs):
        """ return the name of a source map file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.srcmaps_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Write a Python 3 function that can
return the name of a model cube file

Response:
def mcube(self, **kwargs):
        """ return the name of a model cube file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.mcube_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Here you have a function in Python 3, explain what it does
def ltcube_sun(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.ltcubesun_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a livetime cube file


Instruction:
Explain what the following Python 3 code does
def ltcube_moon(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.ltcubemoon_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a livetime cube file


Instruction:
Can you write a function in Python 3 where it
returns the name of a binned exposure cube file

Response:
def bexpcube_sun(self, **kwargs):
        """ return the name of a binned exposure cube file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.bexpcubesun_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Write a Python 3 function for
returning the name of a binned exposure cube file

Response:
def bexpcube_moon(self, **kwargs):
        """ return the name of a binned exposure cube file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.bexpcubemoon_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Here you have a function in Python 3, explain what it does
def angprofile(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.angprofile_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the file name for sun or moon angular profiles


Instruction:
Create a Python 3 function for
returning the file name for sun or moon template files

Response:
def template_sunmoon(self, **kwargs):
        """ return the file name for sun or moon template files
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.templatesunmoon_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Here you have a function in Python 3, explain what it does
def residual_cr(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        localpath = NameFactory.residual_cr_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: Return the name of the residual CR analysis output files


Instruction:
Make a summary of the following Python 3 code
def galprop_rings_yaml(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.galprop_rings_yaml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a galprop rings merging yaml file


Instruction:
Can you generate the documentation for the following Python 3 function
def catalog_split_yaml(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.catalog_split_yaml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a catalog split yaml file


Instruction:
Make a summary of the following Python 3 code
def model_yaml(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.model_yaml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a model yaml file


Instruction:
Can you generate the documentation for the following Python 3 function
def merged_srcmaps(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.merged_srcmaps_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a source map file that is merged with another source map file


Instruction:
Given the following Python 3 function, write the documentation
def master_srcmdl_xml(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        localpath = NameFactory.master_srcmdl_xml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath

Response: return the name of a master source model file


Instruction:
Write a Python 3 script for
returning the name of a source model file

Response:
def comp_srcmdl_xml(self, **kwargs):
        """ return the name of a source model file
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        localpath = NameFactory.comp_srcmdl_xml_format.format(**kwargs_copy)
        if kwargs.get('fullpath', False):
            return self.fullpath(localpath=localpath)
        return localpath


Instruction:
Can you tell what is the following Python 3 function doing
def fullpath(self, **kwargs):
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        self._replace_none(kwargs_copy)
        return NameFactory.fullpath_format.format(**kwargs_copy)

Response: Return a full path name for a given file


Instruction:
Create a Python 3 function for
returning a generic filename for a given dataset and component

Response:
def generic(self, input_string, **kwargs):
        """ return a generic filename for a given dataset and component
        """
        kwargs_copy = self.base_dict.copy()
        kwargs_copy.update(**kwargs)
        kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs))
        kwargs_copy['component'] = kwargs.get(
            'component', self.component(**kwargs))
        self._replace_none(kwargs_copy)
        return input_string.format(**kwargs_copy)


Instruction:
Write a Python 3 function for
making a dictionary of filenames for various types

Response:
def make_filenames(self, **kwargs):
        """ Make a dictionary of filenames for various types
        """
        out_dict = dict(ft1file=self.ft1file(**kwargs),
                        ltcube=self.ltcube(**kwargs),
                        ccube=self.ccube(**kwargs),
                        bexpcube=self.bexpcube(**kwargs),
                        srcmaps=self.srcmaps(**kwargs),
                        mcube=self.mcube(**kwargs))
        return out_dict


Instruction:
Create a Python 3 function to
map from the top - level arguments to the arguments provided to the indiviudal links

Response:
def _map_arguments(self, args):
        """Map from the top-level arguments to the arguments provided to
        the indiviudal links """
        data = args.get('data')
        comp = args.get('comp')
        library = args.get('library')
        dry_run = args.get('dry_run', False)

        self._set_link('sum-rings', SumRings_SG,
                       library=library,
                       outdir=args['outdir'],
                       dry_run=dry_run)

        self._set_link('srcmaps-diffuse', SrcmapsDiffuse_SG,
                       comp=comp, data=data,
                       library=library,
                       make_xml=args['make_xml'],
                       dry_run=dry_run)

        self._set_link('vstack-diffuse', Vstack_SG,
                       comp=comp, data=data,
                       library=library,
                       dry_run=dry_run)


Instruction:
Create a Python 3 function to
map from the top - level arguments to the arguments provided to the indiviudal links

Response:
def _map_arguments(self, args):
        """Map from the top-level arguments to the arguments provided to
        the indiviudal links """
        data = args.get('data')
        comp = args.get('comp')
        library = args.get('library')
        dry_run = args.get('dry_run', False)

        self._set_link('srcmaps-catalog', SrcmapsCatalog_SG,
                       comp=comp, data=data,
                       library=library,
                       nsrc=args.get('nsrc', 500),
                       dry_run=dry_run)

        self._set_link('gather-srcmaps', GatherSrcmaps_SG,
                       comp=comp, data=data,
                       library=library,
                       dry_run=dry_run)

        self._set_link('merge-srcmaps', MergeSrcmaps_SG,
                       comp=comp, data=data,
                       library=library,
                       dry_run=dry_run)


Instruction:
Implement a function in Python 3 to
map from the top - level arguments to the arguments provided to the indiviudal links

Response:
def _map_arguments(self, args):
        """Map from the top-level arguments to the arguments provided to
        the indiviudal links """
        config_yaml = args['config']
        config_dict = load_yaml(config_yaml)

        dry_run = args.get('dry_run', False)

        data = config_dict.get('data')
        comp = config_dict.get('comp')
        library = config_dict.get('library')
        models = config_dict.get('models')
        scratch = config_dict.get('scratch')

        self._set_link('prepare', SplitAndBinChain,
                       comp=comp, data=data,
                       ft1file=config_dict.get('ft1file'),
                       hpx_order_ccube=config_dict.get('hpx_order_ccube'),
                       hpx_order_expcube=config_dict.get('hpx_order_expcube'),
                       scratch=scratch,
                       dry_run=dry_run)

        self._set_link('diffuse-comp', DiffuseCompChain,
                       comp=comp, data=data,
                       library=library,
                       make_xml=config_dict.get('make_diffuse_comp_xml', False),
                       outdir=config_dict.get('merged_gasmap_dir', 'merged_gasmap'),
                       dry_run=dry_run)

        self._set_link('catalog-comp', CatalogCompChain,
                       comp=comp, data=data,
                       library=library,
                       make_xml=config_dict.get('make_catalog_comp_xml', False),
                       nsrc=config_dict.get('catalog_nsrc', 500),
                       dry_run=dry_run)

        self._set_link('assemble-model', AssembleModelChain,
                       comp=comp, data=data,
                       library=library,
                       models=models,
                       hpx_order=config_dict.get('hpx_order_fitting'),
                       dry_run=dry_run)


Instruction:
How would you explain what the following Python 3 function does
def create_spectrum_from_dict(spectrum_type, spectral_pars, fn=None):

    if fn is None:
        fn = pyLike.SourceFactory_funcFactory().create(str(spectrum_type))

    if spectrum_type == 'PiecewisePowerLaw':
        build_piecewise_powerlaw(fn, spectral_pars)

    for k, v in spectral_pars.items():

        v.setdefault('scale', 1.0)
        v.setdefault('min', v['value'] * 1E-3)
        v.setdefault('max', v['value'] * 1E3)

        par = fn.getParam(str(k))

        vmin = min(float(v['value']), float(v['min']))
        vmax = max(float(v['value']), float(v['max']))

        par.setValue(float(v['value']))
        par.setBounds(vmin, vmax)
        par.setScale(float(v['scale']))

        if 'free' in v and int(v['free']) != 0:
            par.setFree(True)
        else:
            par.setFree(False)
        fn.setParam(par)

    return fn

Response: Create a Function object from a dictionary of spectral parameters.


Instruction:
Make a summary of the following Python 3 code
def gtlike_spectrum_to_dict(spectrum):
    parameters = pyLike.ParameterVector()
    spectrum.getParams(parameters)
    d = dict(spectrum_type=spectrum.genericName())
    for p in parameters:

        pname = p.getName()
        pval = p.getTrueValue()
        perr = abs(p.error() * p.getScale()) if p.isFree() else np.nan
        d[pname] = np.array([pval, perr])

        if d['spectrum_type'] == 'FileFunction':
            ff = pyLike.FileFunction_cast(spectrum)
            d['file'] = ff.filename()
    return d

Response:
Convert a pyLikelihood object to a python dictionary which can
        be easily saved to a file.


Instruction:
Can you write a function in Python 3 where it
converts a pyLikelihood object to a python dictionary which can be easily saved to a file.

Response:
def gtlike_spectrum_to_vectors(spectrum):
    """ Convert a pyLikelihood object to a python dictionary which can
        be easily saved to a file."""

    parameters = pyLike.ParameterVector()
    spectrum.getParams(parameters)

    npar = max(parameters.size(), 10)
    o = {'param_names': np.zeros(npar, dtype='S32'),
         'param_values': np.empty(npar, dtype=float) * np.nan,
         'param_errors': np.empty(npar, dtype=float) * np.nan,
         }

    for i, p in enumerate(parameters):
        o['param_names'][i] = p.getName()
        o['param_values'][i] = p.getTrueValue()
        perr = abs(p.error() * p.getScale()) if p.isFree() else np.nan
        o['param_errors'][i] = perr

    return o


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_function_pars(fn):

    pars = []
    par_names = pyLike.StringVector()
    fn.getParamNames(par_names)

    for pname in par_names:

        par = fn.getParam(pname)
        bounds = par.getBounds()
        perr = par.error() if par.isFree() else np.nan
        pars += [dict(name=pname,
                      value=par.getValue(),
                      error=perr,
                      min=bounds[0],
                      max=bounds[1],
                      free=par.isFree(),
                      scale=par.getScale())]

    return pars

Response:
Extract the parameters of a pyLikelihood function object
   .


Instruction:
Given the following Python 3 function, write the documentation
def get_priors(like):

    npar = len(like.params())

    vals = np.ones(npar)
    errs = np.ones(npar)
    has_prior = np.array([False] * npar)

    for i, p in enumerate(like.params()):

        prior = like[i].log_prior()

        if prior is None:
            continue

        par_names = pyLike.StringVector()
        prior.getParamNames(par_names)

        if not 'Mean' in par_names:
            raise Exception('Failed to find Mean in prior parameters.')

        if not 'Sigma' in par_names:
            raise Exception('Failed to find Sigma in prior parameters.')

        for t in par_names:

            if t == 'Mean':
                vals[i] = prior.parameter(t).getValue()

            if t == 'Sigma':
                errs[i] = prior.parameter(t).getValue()

        has_prior[i] = True

    return vals, errs, has_prior

Response: Extract priors from a likelihood object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_source_pars(src):

    fnmap = src.getSrcFuncs()

    keys = fnmap.keys()

    if 'Position' in keys:
        ppars = get_function_pars(src.getSrcFuncs()[str('Position')])
    elif 'SpatialDist' in keys:
        ppars = get_function_pars(src.getSrcFuncs()[str('SpatialDist')])
    else:
        raise Exception('Failed to extract spatial parameters.')

    fn = src.getSrcFuncs()[str('Spectrum')]
    spars = get_function_pars(fn)

    for i, p in enumerate(ppars):
        ppars[i]['is_norm'] = False

    for i, p in enumerate(spars):

        if fn.normPar().getName() == p['name']:
            spars[i]['is_norm'] = True
        else:
            spars[i]['is_norm'] = False

    return spars, ppars

Response: Extract the parameters associated with a pyLikelihood Source object.


Instruction:
Can you implement a function in Python 3 that
counts the number of free parameters in the active model.

Response:
def nFreeParams(self):
        """Count the number of free parameters in the active model."""
        nF = 0
        pars = self.params()
        for par in pars:
            if par.isFree():
                nF += 1
        return nF


Instruction:
Implement a function in Python 3 to
compute the TS value for a source indicated by srcName.

Response:
def Ts2(self, srcName, reoptimize=False, approx=True,
            tol=None, MaxIterations=10, verbosity=0):
        """Computes the TS value for a source indicated by "srcName."

        If "reoptimize=True" is selected this function will reoptimize
        the model up to "MaxIterations" given the tolerance "tol"
        (default is the tolerance selected for the overall fit).  If
        "appox=True" is selected (the default) it will renormalize the
        model (see _renorm).
        """

        saved_state = LikelihoodState(self)
        if verbosity > 0:
            print("*** Start Ts_dl ***")
        source_attributes = self.getExtraSourceAttributes()
        self.logLike.syncParams()
        src = self.logLike.getSource(srcName)
        self._ts_src = src
        freeParams = pyLike.DoubleVector()
        self.logLike.getFreeParamValues(freeParams)
        logLike1 = self.logLike.value()
        self.scaleSource(srcName, 1E-10)
        logLike0 = self.logLike.value()
        if tol is None:
            tol = self.tol
        if reoptimize:
            if verbosity > 0:
                print("** Do reoptimize")
            optFactory = pyLike.OptimizerFactory_instance()
            myOpt = optFactory.create(self.optimizer, self.logLike)
            Niter = 1
            while Niter <= MaxIterations:
                try:
                    myOpt.find_min(0, tol)
                    break
                except RuntimeError as e:
                    print(e)
                if verbosity > 0:
                    print("** Iteration :", Niter)
                Niter += 1
        else:
            if approx:
                try:
                    self._renorm()
                except ZeroDivisionError:
                    pass
        self.logLike.syncParams()
        logLike0 = max(self.logLike.value(), logLike0)
        Ts_value = 2 * (logLike1 - logLike0)
        self.scaleSource(srcName, 1E10)

        self.logLike.setFreeParamValues(freeParams)
        self.model = SourceModel(self.logLike)
        for src in source_attributes:
            self.model[src].__dict__.update(source_attributes[src])
        saved_state.restore()
        self.logLike.value()
        return Ts_value


Instruction:
Create a Python 3 function to
hook to inster the name of a logfile into the input config

Response:
def _make_scatter_logfile_name(cls, key, linkname, job_config):
        """Hook to inster the name of a logfile into the input config """
        logfile = job_config.get('logfile', "%s_%s_%s.log" %
                                 (cls.default_prefix_logfile, linkname, key))
        job_config['logfile'] = logfile


Instruction:
Given the following Python 3 function, write the documentation
def create(cls, **kwargs):
        linkname = kwargs.setdefault('linkname', cls.clientclass.linkname_default)
        # Don't use setdefault b/c we don't want to build a JobArchive
        # Unless it is needed
        job_archive = kwargs.get('job_archive', None)
        if job_archive is None:
            job_archive = JobArchive.build_temp_job_archive()
            kwargs.setdefault('job_archive', job_archive)
        kwargs_client = dict(linkname=linkname,
                             link_prefix=kwargs.get('link_prefix', ''),
                             file_stage=kwargs.get('file_stage', None),
                             job_archive=job_archive)
        link = cls.clientclass.create(**kwargs_client)
        sg = cls(link, **kwargs)
        return sg

Response: Build and return a ScatterGather object


Instruction:
Implement a function in Python 3 to
build a dictionary of JobDetails objects for the internal Link.

Response:
def _build_job_dict(self):
        """Build a dictionary of `JobDetails` objects for the internal `Link`"""
        if self.args['dry_run']:
            status = JobStatus.unknown
        else:
            status = JobStatus.not_ready

        base_config = self.scatter_link.args

        for jobkey, job_config in sorted(self._job_configs.items()):
            full_job_config = base_config.copy()
            full_job_config.update(job_config)
            ScatterGather._make_scatter_logfile_name(jobkey,
                                                     self.linkname, full_job_config)
            logfile = job_config.get('logfile')
            self._scatter_link._register_job(key=jobkey,
                                             job_config=full_job_config,
                                             logfile=logfile,
                                             status=status)


Instruction:
Make a summary of the following Python 3 code
def _invoke(self, argv, stream=sys.stdout, resubmit_failed=False):
        args = self._run_argparser(argv)

        if args.action not in ACTIONS:
            sys.stderr.write(
                "Unrecognized action %s, options are %s\n" % (args.action, ACTIONS))

        if args.action == 'skip':
            return JobStatus.no_job
        elif args.action in ['run', 'resubmit', 'check_status', 'config']:
            self._job_configs = self.build_job_configs(args.__dict__)

        self._interface._dry_run = args.dry_run

        if args.action == 'run':
            status_vect = self.run_jobs(stream, resubmit_failed=resubmit_failed)
        elif args.action == 'resubmit':
            status_vect = self.resubmit(stream, resubmit_failed=resubmit_failed)
        elif args.action == 'check_status':
            self._build_job_dict()
            status_vect = self.check_status(stream)
        elif args.action == 'config':
            self._build_job_dict()
            status_vect = JobStatusVector()
            status_vect[JobStatus.done] += 1

        return status_vect

Response: Invoke the object to preform a particular action.


Instruction:
Write a Python 3 function that can
update the arguments used to invoke the application.

Response:
def update_args(self, override_args):
        """Update the arguments used to invoke the application

        Note that this will also update the dictionary of input and output files

        Parameters
        ----------

        override_args : dict
            dictionary of arguments to override the current values
        """
        self.args = extract_arguments(override_args, self.args)
        self._job_configs = self.build_job_configs(self.args)
        if not self._scatter_link.jobs:
            self._build_job_dict()
        self._latch_file_info()


Instruction:
Can you tell what is the following Python 3 function doing
def clear_jobs(self, recursive=True):
        if recursive:
            self._scatter_link.clear_jobs(recursive)
        self.jobs.clear()

Response:
Clear the self. jobs dictionary that contains information
        about jobs associated with this ScatterGather.


Instruction:
Implement a function in Python 3 to
return a dictionary with all the jobs in this link.

Response:
def get_jobs(self, recursive=True):
        """Return a dictionary with all the jobs

        If recursive is True this will include jobs from all internal `Link`
        """
        if recursive:
            ret_dict = self.jobs.copy()
            ret_dict.update(self._scatter_link.get_jobs(recursive))
            return ret_dict
        return self.jobs


Instruction:
How would you explain what the following Python 3 function does
def check_status(self, stream=sys.stdout,
                     check_once=False,
                     fail_pending=False, fail_running=False,
                     no_wait=False, do_print=True,
                     write_status=False):
        """Loop to check on the status of all the jobs in job dict.

        Parameters
        -----------
        stream : `file`
            Stream that this function will print to,
            Must have 'write' function.

        check_once : bool
            Check status once and exit loop.

        fail_pending : `bool`
            If True, consider pending jobs as failed

        fail_running : `bool`
            If True, consider running jobs as failed

        no_wait : bool
            Do not sleep before checking jobs.

        do_print : bool
            Print summary stats.

        write_status : bool
            Write the status the to log file.

        Returns
        -------
        status_vect : `JobStatusVector`
            Vector that summarize the number of jobs in various states.
        """
        running = True
        first = True

        if not check_once:
            if stream != sys.stdout:
                sys.stdout.write('Checking status (%is): ' %
                                 self.args['job_check_sleep'])
                sys.stdout.flush()

        status_vect = JobStatusVector()
        while running:
            if first:
                first = False
            elif self.args['dry_run']:
                break
            elif no_wait:
                pass
            else:
                stream.write("Sleeping %.0f seconds between status checks\n" %
                             self.args['job_check_sleep'])
                if stream != sys.stdout:
                    sys.stdout.write('.')
                    sys.stdout.flush()
                time.sleep(self.args['job_check_sleep'])

            status_vect = self._check_link_completion(self._scatter_link,
                                                      fail_pending, fail_running)
            if self.args['check_status_once'] or check_once or no_wait:
                if do_print:
                    self.print_update(stream, status_vect)
                break

            if self.args['print_update']:
                if do_print:
                    self.print_update(stream, status_vect)

            if self._job_archive is not None:
                self._job_archive.write_table_file()

            n_total = status_vect.n_total
            n_done = status_vect.n_done
            n_failed = status_vect.n_failed
            if n_done + n_failed == n_total:
                running = False

        status = status_vect.get_status()
        if status in [JobStatus.failed, JobStatus.partial_failed]:
            if do_print:
                self.print_update(stream, status_vect)
                self.print_failed(stream)
            if write_status:
                self._write_status_to_log(status, stream)
        else:
            if write_status:
                self._write_status_to_log(0, stream)

        self._set_status_self(status=status)
        if not check_once:
            if stream != sys.stdout:
                sys.stdout.write("! %s\n" % (JOB_STATUS_STRINGS[status]))

        if self._job_archive is not None:
            self._job_archive.write_table_file()

        return status_vect

Response: Loop to check the status of all the jobs in the job dict.


Instruction:
Can you generate the documentation for the following Python 3 function
def run_jobs(self, stream=sys.stdout, resubmit_failed=False):
        self._build_job_dict()

        self._interface._dry_run = self.args['dry_run']
        scatter_status = self._interface.submit_jobs(self.scatter_link,
                                                     job_archive=self._job_archive,
                                                     stream=stream)
        if scatter_status == JobStatus.failed:
            return JobStatus.failed

        status_vect = self.check_status(stream, write_status=True)
        status = status_vect.get_status()

        if status == JobStatus.partial_failed:
            if resubmit_failed:
                sys.write("Resubmitting partially failed link %s\n" %
                          self.full_linkname)
                status_vect = self.resubmit(stream=stream, fail_running=False, resubmit_failed=True)
            else:
                sys.stdout.write("NOT resubmitting partially failed link %s\n" %
                                 self.full_linkname)
        return status_vect

Response: Function to dipatch jobs and collect results


Instruction:
Can you generate the documentation for the following Python 3 function
def resubmit(self, stream=sys.stdout, fail_running=False, resubmit_failed=False):
        self._build_job_dict()
        status_vect = self.check_status(stream, check_once=True, fail_pending=True,
                                        fail_running=fail_running)
        status = status_vect.get_status()
        if status == JobStatus.done:
            return status

        failed_jobs = self._scatter_link.get_failed_jobs(True, True)
        if failed_jobs:
            scatter_status = self._interface.submit_jobs(self._scatter_link, failed_jobs,
                                                         job_archive=self._job_archive,
                                                         stream=stream)
            if scatter_status == JobStatus.failed:
                return JobStatus.failed

        status_vect = self.check_status(stream, write_status=True)
        status = status_vect.get_status()

        if status == JobStatus.partial_failed:
            if resubmit_failed:
                sys.stdout.write("Resubmitting partially failed link %s\n" %
                                 self.full_linkname)
                status_vect = self.resubmit(stream=stream, fail_running=False, resubmit_failed=False)
            else:
                sys.stdout.write("NOT resubmitting partially failed link %s\n" %
                                 self.full_linkname)

        if self.args['dry_run']:
            return JobStatus.unknown
        return status_vect

Response: Function to resubmit failed jobs and collect results


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clean_jobs(self, recursive=False):
        self._interface.clean_jobs(self.scatter_link,
                                   clean_all=recursive)

Response: Clean up all the jobs associated with this object.


Instruction:
Given the following Python 3 function, write the documentation
def print_summary(self, stream=sys.stdout, indent="", recurse_level=2):
        Link.print_summary(self, stream, indent, recurse_level)
        if recurse_level > 0:
            recurse_level -= 1
            indent += "  "
            stream.write("\n")
            self._scatter_link.print_summary(stream, indent, recurse_level)

Response: Print a summary of the activity done by this Link.


Instruction:
Can you tell what is the following Python 3 function doing
def print_update(self, stream=sys.stdout, job_stats=None):
        if job_stats is None:
            job_stats = JobStatusVector()
            job_det_list = []
            job_det_list += self._scatter_link.jobs.values()

            for job_dets in job_det_list:
                if job_dets.status == JobStatus.no_job:
                    continue
                job_stats[job_dets.status] += 1

        stream.write("Status :\n  Total  : %i\n  Unknown: %i\n" %
                     (job_stats.n_total, job_stats[JobStatus.unknown]))
        stream.write("  Not Ready: %i\n  Ready: %i\n" %
                     (job_stats[JobStatus.not_ready], job_stats[JobStatus.ready]))
        stream.write("  Pending: %i\n  Running: %i\n" %
                     (job_stats[JobStatus.pending], job_stats[JobStatus.running]))
        stream.write("  Done: %i\n  Failed: %i\n" %
                     (job_stats[JobStatus.done], job_stats[JobStatus.failed]))

Response: Print an update about the current number of jobs running


Instruction:
Can you generate the documentation for the following Python 3 function
def print_failed(self, stream=sys.stderr):
        for job_key, job_details in sorted(self.scatter_link.jobs.items()):
            if job_details.status == JobStatus.failed:
                stream.write("Failed job %s\n  log = %s\n" %
                             (job_key, job_details.logfile))

Response: Print the list of failed jobs


Instruction:
Can you create a Python 3 function that
opens a numpy pickle file and read all the new sources into a dictionary

Response:
def read_sources_from_numpy_file(npfile):
    """ Open a numpy pickle file and read all the new sources into a dictionary

    Parameters
    ----------
    npfile : file name
       The input numpy pickle file

    Returns
    -------
    tab : `~astropy.table.Table`
    """
    srcs = np.load(npfile).flat[0]['sources']

    roi = ROIModel()
    roi.load_sources(srcs.values())
    return roi.create_table()


Instruction:
Can you write a function in Python 3 where it
opens a yaml file and read all the new sources into a dictionary

Response:
def read_sources_from_yaml_file(yamlfile):
    """ Open a yaml file and read all the new sources into a dictionary

    Parameters
    ----------
    yaml : file name
       The input yaml file

    Returns
    -------
    tab : `~astropy.table.Table`
    """
    f = open(yamlfile)
    dd = yaml.load(f)
    srcs = dd['sources']
    f.close()
    roi = ROIModel()
    roi.load_sources(srcs.values())
    return roi.create_table()


Instruction:
Create a Python 3 function to
append the sources in a table into another table.

Response:
def merge_source_tables(src_tab, tab, all_sources=False, prefix="", suffix="",
                        roi_idx=None):
    """Append the sources in a table into another table.

    Parameters
    ----------
    src_tab : `~astropy.table.Table`
       Master source table that will be appended with the sources in
       ``tab``.

    tab : `~astropy.table.Table`
       Table to be merged into ``src_tab``.

    all_sources : bool
       If true, then all the sources get added to the table.
       if false, then only the sources that start with 'PS' get added

    prefix : str
       Prepended to all source names

    suffix : str
       Appended to all source names

    Returns
    -------
    tab : `~astropy.table.Table`

    """
    if roi_idx is not None and 'roi' not in tab.columns:
        tab.add_column(Column(name='roi', data=len(tab) * [roi_idx]))

    remove_rows = []
    for i, row in enumerate(tab):

        if not all_sources and row['name'].find("PS") != 0:
            remove_rows += [i]
            continue
        sname = "%s%s%s" % (prefix, row['name'], suffix)
        row['name'] = sname

    tab.remove_rows(remove_rows)

    if src_tab is None:
        src_tab = tab
    else:
        src_tab = vstack([src_tab, tab], join_type='outer')

    return src_tab


Instruction:
How would you explain what the following Python 3 function does
def lightcurve(self, name, **kwargs):

        name = self.roi.get_source_by_name(name).name

        # Create schema for method configuration
        schema = ConfigSchema(self.defaults['lightcurve'],
                              optimizer=self.defaults['optimizer'])
        schema.add_option('prefix', '')
        config = utils.create_dict(self.config['lightcurve'],
                                   optimizer=self.config['optimizer'])
        config = schema.create_config(config, **kwargs)

        self.logger.info('Computing Lightcurve for %s' % name)

        o = self._make_lc(name, **config)
        filename = utils.format_filename(self.workdir, 'lightcurve',
                                         prefix=[config['prefix'],
                                                 name.lower().replace(' ', '_')])

        o['file'] = None
        if config['write_fits']:
            o['file'] = os.path.basename(filename) + '.fits'
            self._make_lc_fits(o, filename + '.fits', **config)

        if config['write_npy']:
            np.save(filename + '.npy', o)

        self.logger.info('Finished Lightcurve')

        return o

Response: Generate a lightcurve for the named source.


Instruction:
Here you have a function in Python 3, explain what it does
def fill_livetime_hist(skydir, tab_sc, tab_gti, zmax, costh_edges):

    if len(tab_gti) == 0:
        shape = (len(costh_edges) - 1, len(skydir))
        return (np.zeros(shape), np.zeros(shape))

    m = (tab_sc['START'] < tab_gti['STOP'][-1])
    m &= (tab_sc['STOP'] > tab_gti['START'][0])
    tab_sc = tab_sc[m]

    cos_zmax = np.cos(np.radians(zmax))
    sc_t0 = np.array(tab_sc['START'].data)
    sc_t1 = np.array(tab_sc['STOP'].data)
    sc_live = np.array(tab_sc['LIVETIME'].data)
    sc_lfrac = sc_live / (sc_t1 - sc_t0)

    sc_xyz = angle_to_cartesian(np.radians(tab_sc['RA_SCZ'].data),
                                np.radians(tab_sc['DEC_SCZ'].data))
    zn_xyz = angle_to_cartesian(np.radians(tab_sc['RA_ZENITH'].data),
                                np.radians(tab_sc['DEC_ZENITH'].data))

    tab_gti_t0 = np.array(tab_gti['START'].data)
    tab_gti_t1 = np.array(tab_gti['STOP'].data)

    # Index of the closest GTI interval
    idx = np.digitize(sc_t0, tab_gti_t0) - 1

    # start/stop time of closest GTI interval
    gti_t0 = np.zeros_like(sc_t0)
    gti_t1 = np.zeros_like(sc_t1)
    gti_t0[idx >= 0] = tab_gti_t0[idx[idx >= 0]]
    gti_t1[idx >= 0] = tab_gti_t1[idx[idx >= 0]]

    nbin = len(costh_edges) - 1

    lt = np.zeros((nbin,) + skydir.shape)
    lt_wt = np.zeros((nbin,) + skydir.shape)

    m0 = (idx >= 0) & (sc_t0 >= gti_t0) & (sc_t1 <= gti_t1)

    xyz = angle_to_cartesian(skydir.ra.rad, skydir.dec.rad)

    for i, t in enumerate(xyz):
        cos_sep = utils.dot_prod(t, sc_xyz)
        cos_zn = utils.dot_prod(t, zn_xyz)
        m = m0 & (cos_zn > cos_zmax) & (cos_sep > 0.0)
        bins = np.digitize(cos_sep[m], bins=costh_edges) - 1
        bins = np.clip(bins, 0, nbin - 1)
        lt[:, i] = np.bincount(bins, weights=sc_live[m], minlength=nbin)
        lt_wt[:, i] = np.bincount(bins, weights=sc_live[m] * sc_lfrac[m],
                                  minlength=nbin)

    return lt, lt_wt

Response: Generate a sequence of livetime distributions at the given skydir.


Instruction:
Given the following Python 3 function, write the documentation
def create(cls, ltfile):

        if not re.search('\.txt?', ltfile) is None:
            files = np.loadtxt(ltfile, unpack=True, dtype='str')
        elif not isinstance(ltfile, list):
            files = glob.glob(ltfile)

        ltc = cls.create_from_fits(files[0])
        for f in files[1:]:
            ltc.load_ltfile(f)

        return ltc

Response: Create a livetime cube from a single file or list of files.


Instruction:
How would you explain what the following Python 3 function does
def create_empty(cls, tstart, tstop, fill=0.0, nside=64):
        cth_edges = np.linspace(0, 1.0, 41)
        domega = utils.edge_to_width(cth_edges) * 2.0 * np.pi
        hpx = HPX(nside, True, 'CEL', ebins=cth_edges)
        data = np.ones((len(cth_edges) - 1, hpx.npix)) * fill
        return cls(data, hpx, cth_edges, tstart=tstart, tstop=tstop)

Response: Create an empty livetime cube.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_skydir_lthist(self, skydir, cth_bins):
        ra = skydir.ra.deg
        dec = skydir.dec.deg

        npts = 1
        bins = utils.split_bin_edges(cth_bins, npts)

        center = edge_to_center(bins)
        width = edge_to_width(bins)
        ipix = hp.ang2pix(self.hpx.nside, np.pi / 2. - np.radians(dec),
                          np.radians(ra), nest=self.hpx.nest)
        lt = np.histogram(self._cth_center,
                          weights=self.data[:, ipix], bins=bins)[0]
        lt = np.sum(lt.reshape(-1, npts), axis=1)
        return lt

Response: Get the livetime distribution for a given sky - location.


Instruction:
Given the following Python 3 function, write the documentation
def create_skydir_ltcube(self, skydir, tab_sc, tab_gti, zmax):

        skydir = SkyCoord(np.array([skydir.ra.deg]),
                          np.array([skydir.dec.deg]), unit='deg')

        lt, lt_wt = fill_livetime_hist(skydir, tab_sc, tab_gti, zmax,
                                       self.costh_edges)

        ipix = self.hpx.skydir_to_pixel(skydir)

        lt_scale = np.ones_like(lt)
        lt_wt_scale = np.ones_like(lt_wt)
        m = self.data[:, ipix] > 0.0

        lt_scale[m] = lt[m] / self.data[:, ipix][m]
        lt_wt_scale[m] = lt_wt[m] / self._data_wt[:, ipix][m]
        data = self.data * lt_scale
        data_wt = self._data_wt * lt_wt_scale
        return LTCube(data, copy.deepcopy(self.hpx), self.costh_edges,
                      # tstart=np.min(tab_gti_t0),
                      # tstop=np.max(tab_gti_t1),
                      zmax=zmax, data_wt=data_wt)

Response:
This method creates a new livetime cube by scaling this one by the the
        observing profile ratio in the direction skydir.


Instruction:
Can you create a Python 3 function that
writes the livetime cube to a FITS file.

Response:
def write(self, outfile):
        """Write the livetime cube to a FITS file."""

        hdu_pri = fits.PrimaryHDU()

        hdu_exp = self._create_exp_hdu(self.data)
        hdu_exp.name = 'EXPOSURE'
        hdu_exp_wt = self._create_exp_hdu(self._data_wt)
        hdu_exp_wt.name = 'WEIGHTED_EXPOSURE'

        cols = [Column(name='CTHETA_MIN', dtype='f4',
                       data=self.costh_edges[:-1][::-1]),
                Column(name='CTHETA_MAX',  dtype='f4',
                       data=self.costh_edges[1:][::-1]), ]
        hdu_bnds = fits.table_to_hdu(Table(cols))
        hdu_bnds.name = 'CTHETABOUNDS'

        hdu_gti = fits.table_to_hdu(self._tab_gti)
        hdu_gti.name = 'GTI'

        hdus = [hdu_pri, hdu_exp, hdu_exp_wt,
                hdu_bnds, hdu_gti]

        for hdu in hdus:
            hdu.header['TSTART'] = self.tstart
            hdu.header['TSTOP'] = self.tstop

        with fits.HDUList(hdus) as hdulist:
            hdulist.writeto(outfile, clobber=True)


Instruction:
How would you explain what the following Python 3 function does
def main():
    usage = "usage: %(prog)s [options] "
    description = "Merge a set of Fermi-LAT files."

    parser = argparse.ArgumentParser(usage=usage, description=description)

    parser.add_argument('-o', '--output', default=None, type=str,
                        help='Output file.')
    parser.add_argument('--clobber', default=False, action='store_true',
                        help='Overwrite output file.')
    parser.add_argument('--hdu', default=None, type=str,
                        help='HDU name.')
    parser.add_argument('--gzip', action='store_true',
                        help='Compress output file')
    parser.add_argument('--rm', action='store_true',
                        help='Remove input files.')
    parser.add_argument('files', nargs='+', default=None,
                        help='List of input files.')

    args = parser.parse_args()

    hpx_map = merge_utils.stack_energy_planes_hpx(args.files, hdu=args.hdu)

    if args.output:
        hpx_map.hpx.write_fits(hpx_map.counts, args.output,
                               extname=args.hdu, clobber=args.clobber)

        if args.gzip:
            os.system('gzip -9 %s' % args.output)

        if args.rm:
            for farg in args.files:
                flist = glob.glob(farg)
                for ffound in flist:
                    os.path.unlink(ffound)

Response: This function is called by the command line interface to merge a set of Fermi - LAT files.


Instruction:
Make a summary of the following Python 3 code
def make_cube_slice(map_in, loge_bounds):
    # FIXME: This functionality should be moved into a slice method of
    # gammapy.maps
    axis = map_in.geom.axes[0]
    i0 = utils.val_to_edge(axis.edges, 10**loge_bounds[0])[0]
    i1 = utils.val_to_edge(axis.edges, 10**loge_bounds[1])[0]
    new_axis = map_in.geom.axes[0].slice(slice(i0, i1))
    geom = map_in.geom.to_image()
    geom = geom.to_cube([new_axis])
    map_out = WcsNDMap(geom, map_in.data[slice(i0, i1), ...].copy())
    return map_out

Response: Extract a slice from a map cube object.


Instruction:
Can you generate the documentation for the following Python 3 function
def plot_sed(sed, showlnl=False, **kwargs):

        ax = kwargs.pop('ax', plt.gca())
        cmap = kwargs.get('cmap', 'BuGn')

        annotate_name(sed, ax=ax)
        SEDPlotter.plot_flux_points(sed, **kwargs)

        if np.any(sed['ts'] > 9.):

            if 'model_flux' in sed:
                SEDPlotter.plot_model(sed['model_flux'],
                                      noband=showlnl, **kwargs)

        if showlnl:
            SEDPlotter.plot_lnlscan(sed, **kwargs)

        ax.set_yscale('log')
        ax.set_xscale('log')
        ax.set_xlabel('Energy [MeV]')
        ax.set_ylabel('E$^{2}$dN/dE [MeV cm$^{-2}$ s$^{-1}$]')

Response: Render a plot of a spectral energy distribution.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_residmap_plots(self, maps, roi=None, **kwargs):

        fmt = kwargs.get('format', self.config['format'])
        figsize = kwargs.get('figsize', self.config['figsize'])
        workdir = kwargs.pop('workdir', self.config['fileio']['workdir'])
        use_weights = kwargs.pop('use_weights', False)
        # FIXME, how to set this:
        no_contour = False
        zoom = kwargs.get('zoom', None)

        kwargs.setdefault('graticule_radii', self.config['graticule_radii'])
        kwargs.setdefault('label_ts_threshold',
                          self.config['label_ts_threshold'])
        cmap = kwargs.setdefault('cmap', self.config['cmap'])
        cmap_resid = kwargs.pop('cmap_resid', self.config['cmap_resid'])
        kwargs.setdefault('catalogs', self.config['catalogs'])
        if no_contour:
            sigma_levels = None
        else:
            sigma_levels = [-5, -3, 3, 5, 7] + list(np.logspace(1, 3, 17))

        load_bluered_cmap()

        prefix = maps['name']
        mask = maps['mask']
        if use_weights:
            sigma_hist_data = maps['sigma'].data[maps['mask'].data.astype(
                bool)]
            maps['sigma'].data *= maps['mask'].data
            maps['data'].data *= maps['mask'].data
            maps['model'].data *= maps['mask'].data
            maps['excess'].data *= maps['mask'].data
        else:
            sigma_hist_data = maps['sigma'].data

        fig = plt.figure(figsize=figsize)
        p = ROIPlotter(maps['sigma'], roi=roi, **kwargs)
        p.plot(vmin=-5, vmax=5, levels=sigma_levels,
               cb_label='Significance [$\sigma$]', interpolation='bicubic',
               cmap=cmap_resid, zoom=zoom)
        plt.savefig(utils.format_filename(workdir,
                                          'residmap_sigma',
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

        # make and draw histogram
        fig, ax = plt.subplots(figsize=figsize)
        nBins = np.linspace(-6, 6, 121)
        data = np.nan_to_num(sigma_hist_data)
        # find best fit parameters
        mu, sigma = norm.fit(data.flatten())
        # make and draw the histogram
        data[data > 6.0] = 6.0
        data[data < -6.0] = -6.0

        n, bins, patches = ax.hist(data.flatten(), nBins, density=True,
                                   histtype='stepfilled',
                                   facecolor='green', alpha=0.75)
        # make and draw best fit line
        y = norm.pdf(bins, mu, sigma)
        ax.plot(bins, y, 'r--', linewidth=2)
        y = norm.pdf(bins, 0.0, 1.0)
        ax.plot(bins, y, 'k', linewidth=1)

        # labels and such
        ax.set_xlabel(r'Significance ($\sigma$)')
        ax.set_ylabel('Probability')
        paramtext = 'Gaussian fit:\n'
        paramtext += '$\\mu=%.2f$\n' % mu
        paramtext += '$\\sigma=%.2f$' % sigma
        ax.text(0.05, 0.95, paramtext, verticalalignment='top',
                horizontalalignment='left', transform=ax.transAxes)

        plt.savefig(utils.format_filename(workdir,
                                          'residmap_sigma_hist',
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

        vmax = max(np.max(maps['data'].data), np.max(maps['model'].data))
        vmin = min(np.min(maps['data'].data), np.min(maps['model'].data))

        fig = plt.figure(figsize=figsize)
        p = ROIPlotter(maps['data'], roi=roi, **kwargs)
        p.plot(cb_label='Counts', interpolation='bicubic',
               cmap=cmap, zscale='sqrt', vmin=vmin, vmax=vmax)
        plt.savefig(utils.format_filename(workdir,
                                          'residmap_data',
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

        fig = plt.figure(figsize=figsize)
        p = ROIPlotter(maps['model'], roi=roi, **kwargs)
        p.plot(cb_label='Counts', interpolation='bicubic',
               cmap=cmap, zscale='sqrt', vmin=vmin, vmax=vmax)
        plt.savefig(utils.format_filename(workdir,
                                          'residmap_model',
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

        fig = plt.figure(figsize=figsize)
        p = ROIPlotter(maps['excess'], roi=roi, **kwargs)
        p.plot(cb_label='Counts', interpolation='bicubic',
               cmap=cmap_resid)
        plt.savefig(utils.format_filename(workdir,
                                          'residmap_excess',
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

Response: Make plots from the output of a GTAnalysis. ResidualMap.


Instruction:
Can you implement a function in Python 3 that
makes plots from the output of `~fermipy.gtanalysis.GTAnalysis.tsmap` or `~fermipy.gtanalysis.GTAnalysis.tscube`. This method generates a 2D sky map for the best-fit test source in sqrt(TS) and Npred. Parameters ---------- maps : dict Output dictionary of `~fermipy.gtanalysis.GTAnalysis.tsmap` or `~fermipy.gtanalysis.GTAnalysis.tscube`. roi : `~fermipy.roi_model.ROIModel` ROI Model object. Generate markers at the positions of the sources in this ROI. zoom : float Crop the image by this factor. If None then no crop is applied.

Response:
def make_tsmap_plots(self, maps, roi=None, **kwargs):
        """Make plots from the output of
        `~fermipy.gtanalysis.GTAnalysis.tsmap` or
        `~fermipy.gtanalysis.GTAnalysis.tscube`.  This method
        generates a 2D sky map for the best-fit test source in
        sqrt(TS) and Npred.

        Parameters
        ----------
        maps : dict
            Output dictionary of
            `~fermipy.gtanalysis.GTAnalysis.tsmap` or
            `~fermipy.gtanalysis.GTAnalysis.tscube`.

        roi : `~fermipy.roi_model.ROIModel`
            ROI Model object.  Generate markers at the positions of
            the sources in this ROI.

        zoom : float
            Crop the image by this factor.  If None then no crop is
            applied.
        """
        kwargs.setdefault('graticule_radii', self.config['graticule_radii'])
        kwargs.setdefault('label_ts_threshold',
                          self.config['label_ts_threshold'])
        kwargs.setdefault('cmap', self.config['cmap'])
        kwargs.setdefault('catalogs', self.config['catalogs'])
        fmt = kwargs.get('format', self.config['format'])
        figsize = kwargs.get('figsize', self.config['figsize'])
        workdir = kwargs.pop('workdir', self.config['fileio']['workdir'])
        suffix = kwargs.pop('suffix', 'tsmap')
        zoom = kwargs.pop('zoom', None)

        if 'ts' not in maps:
            return

        sigma_levels = [3, 5, 7] + list(np.logspace(1, 3, 17))
        prefix = maps['name']
        fig = plt.figure(figsize=figsize)
        p = ROIPlotter(maps['sqrt_ts'], roi=roi, **kwargs)
        p.plot(vmin=0, vmax=5, levels=sigma_levels,
               cb_label='Sqrt(TS) [$\sigma$]', interpolation='bicubic',
               zoom=zoom)
        plt.savefig(utils.format_filename(workdir,
                                          '%s_sqrt_ts' % suffix,
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

        fig = plt.figure(figsize=figsize)
        p = ROIPlotter(maps['npred'], roi=roi, **kwargs)
        p.plot(vmin=0, cb_label='NPred [Counts]', interpolation='bicubic',
               zoom=zoom)
        plt.savefig(utils.format_filename(workdir,
                                          '%s_npred' % suffix,
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)

        # make and draw histogram
        fig, ax = plt.subplots(figsize=figsize)
        bins = np.linspace(0, 25, 101)

        data = np.nan_to_num(maps['ts'].data.T)
        data[data > 25.0] = 25.0
        data[data < 0.0] = 0.0
        n, bins, patches = ax.hist(data.flatten(), bins, density=True,
                                   histtype='stepfilled',
                                   facecolor='green', alpha=0.75)
        # ax.plot(bins,(1-chi2.cdf(x,dof))/2.,**kwargs)
        ax.plot(bins, 0.5 * chi2.pdf(bins, 1.0), color='k',
                label=r"$\chi^2_{1} / 2$")
        ax.set_yscale('log')
        ax.set_ylim(1E-4)
        ax.legend(loc='upper right', frameon=False)

        # labels and such
        ax.set_xlabel('TS')
        ax.set_ylabel('Probability')
        plt.savefig(utils.format_filename(workdir,
                                          '%s_ts_hist' % suffix,
                                          prefix=[prefix],
                                          extension=fmt))
        plt.close(fig)


Instruction:
Can you tell what is the following Python 3 function doing
def make_roi_plots(self, gta, mcube_tot, **kwargs):

        fmt = kwargs.get('format', self.config['format'])
        figsize = kwargs.get('figsize', self.config['figsize'])
        prefix = kwargs.get('prefix', '')
        loge_bounds = kwargs.get('loge_bounds', None)
        weighted = kwargs.get('weighted', False)

        roi_kwargs = {}
        roi_kwargs.setdefault('loge_bounds', loge_bounds)
        roi_kwargs.setdefault(
            'graticule_radii', self.config['graticule_radii'])
        roi_kwargs.setdefault('label_ts_threshold',
                              self.config['label_ts_threshold'])
        roi_kwargs.setdefault('cmap', self.config['cmap'])
        roi_kwargs.setdefault('catalogs', self._catalogs)

        if loge_bounds is None:
            loge_bounds = (gta.log_energies[0], gta.log_energies[-1])
        esuffix = '_%.3f_%.3f' % (loge_bounds[0], loge_bounds[1])

        mcube_diffuse = gta.model_counts_map('diffuse')
        counts_map = gta.counts_map()

        if weighted:
            wmap = gta.weight_map()
            counts_map = copy.deepcopy(counts_map)
            mcube_tot = copy.deepcopy(mcube_tot)
            counts_map.data *= wmap.data
            mcube_tot.data *= wmap.data
            mcube_diffuse.data *= wmap.data

        # colors = ['k', 'b', 'g', 'r']
        data_style = {'marker': 's', 'linestyle': 'None'}

        fig = plt.figure(figsize=figsize)

        if gta.projtype == "WCS":
            xmin = -1
            xmax = 1
        elif gta.projtype == "HPX":
            hpx2wcs = counts_map.make_wcs_mapping(proj='CAR', oversample=2)
            counts_map = counts_map.to_wcs(hpx2wcs=hpx2wcs)
            mcube_tot = mcube_tot.to_wcs(hpx2wcs=hpx2wcs)
            mcube_diffuse = mcube_diffuse.to_wcs(hpx2wcs=hpx2wcs)
            xmin = None
            xmax = None

        fig = plt.figure(figsize=figsize)
        rp = ROIPlotter(mcube_tot, roi=gta.roi, **roi_kwargs)
        rp.plot(cb_label='Counts', zscale='pow', gamma=1. / 3.)
        plt.savefig(os.path.join(gta.config['fileio']['workdir'],
                                 '%s_model_map%s.%s' % (
                                     prefix, esuffix, fmt)))
        plt.close(fig)

        rp = ROIPlotter(counts_map, roi=gta.roi, **roi_kwargs)

        rp.plot(cb_label='Counts', zscale='sqrt')
        plt.savefig(os.path.join(gta.config['fileio']['workdir'],
                                 '%s_counts_map%s.%s' % (
                                     prefix, esuffix, fmt)))
        plt.close(fig)

        for iaxis, xlabel, psuffix in zip([0, 1],
                                          ['LON Offset [deg]', 'LAT Offset [deg]'],
                                          ['xproj', 'yproj']):

            fig = plt.figure(figsize=figsize)
            rp.plot_projection(iaxis, label='Data', color='k',
                               xmin=xmin, xmax=xmax, **data_style)
            rp.plot_projection(iaxis, data=mcube_tot, label='Model', xmin=xmin, xmax=xmax,
                               noerror=True)
            rp.plot_projection(iaxis, data=mcube_diffuse, label='Diffuse', xmin=xmin, xmax=xmax,
                               noerror=True)
            plt.gca().set_ylabel('Counts')
            plt.gca().set_xlabel(xlabel)
            plt.gca().legend(frameon=False)
            annotate(loge_bounds=loge_bounds)
            plt.savefig(os.path.join(gta.config['fileio']['workdir'],
                                     '%s_counts_map_%s%s.%s' % (prefix, psuffix,
                                                                esuffix, fmt)))
            plt.close(fig)

Response: Make various diagnostic plots for the 1D and 2D counts and model distributions.


Instruction:
Here you have a function in Python 3, explain what it does
def extract_parameters(pil, keys=None):
    out_dict = {}
    if keys is None:
        keys = pil.keys()
    for key in keys:
        try:
            out_dict[key] = pil[key]
        except ValueError:
            out_dict[key] = None
    return out_dict

Response: Extract and return parameter names and values from a pil object


Instruction:
Can you generate the documentation for the following Python 3 function
def update_gtapp(gtapp, **kwargs):
    for key, val in kwargs.items():
        if key in ['pfiles', 'scratch']:
            continue
        if val is None:
            continue
        try:
            gtapp[key] = val
        except ValueError:
            raise ValueError(
                "gtapp failed to set parameter %s %s" % (key, val))
        except KeyError:
            raise KeyError("gtapp failed to set parameter %s %s" % (key, val))

Response: Update the parameters of the object that can run ScienceTools applications in question


Instruction:
Implement a function in Python 3 to
set the PFILES env var to the value of the current PFILES envar .

Response:
def _set_pfiles(dry_run, **kwargs):
    """Set the PFILES env var

    Parameters
    ----------

    dry_run : bool
        Don't actually run

    Keyword arguments
    -----------------

    pfiles : str
        Value to set PFILES

    Returns
    -------

    pfiles_orig : str
        Current value of PFILES envar
    """
    pfiles_orig = os.environ['PFILES']
    pfiles = kwargs.get('pfiles', None)
    if pfiles:
        if dry_run:
            print("mkdir %s" % pfiles)
        else:
            try:
                os.makedirs(pfiles)
            except OSError:
                pass
        pfiles = "%s:%s" % (pfiles, pfiles_orig)
        os.environ['PFILES'] = pfiles
    return pfiles_orig


Instruction:
Write a Python 3 function that can
build an object that can run ScienceTools application in question

Response:
def build_gtapp(appname, dry_run, **kwargs):
    """Build an object that can run ScienceTools application

    Parameters
    ----------
    appname : str
        Name of the application (e.g., gtbin)

    dry_run : bool
        Print command but do not run it

    kwargs : arguments used to invoke the application

    Returns `GtApp.GtApp` object that will run the application in question
    """
    pfiles_orig = _set_pfiles(dry_run, **kwargs)
    gtapp = GtApp.GtApp(appname)
    update_gtapp(gtapp, **kwargs)
    _reset_pfiles(pfiles_orig)
    return gtapp


Instruction:
Make a summary of the following Python 3 code
def run_gtapp(gtapp, stream, dry_run, **kwargs):
    if stream is None:
        stream = sys.stdout

    pfiles_orig = _set_pfiles(dry_run, **kwargs)
    update_gtapp(gtapp, **kwargs)

    stream.write("%s\n" % gtapp.command())
    stream.flush()
    if dry_run:
        _reset_pfiles(pfiles_orig)
        return 0

    try:
        stdin, stdout = gtapp.runWithOutput(print_command=False)
        for line in stdout:
            stream.write(line.strip())
        stream.flush()
        return_code = 0
    except:
        stream.write('Exited with exit code -1\n')
        return_code = -1

    _reset_pfiles(pfiles_orig)
    return return_code

Response:
Runs one on the ScienceTools apps
            object


Instruction:
Here you have a function in Python 3, explain what it does
def update_args(self, override_args):
        Link.update_args(self, override_args)
        dry_run = override_args.get('dry_run', False)
        if self.__app is None:
            self.__app = build_gtapp(self.appname, dry_run, **self.args)
#except:
#                raise ValueError("Failed to build link %s %s %s" %
#                                 (self.linkname, self.appname, self.args))
        else:
            update_gtapp(self.__app, **self.args)

Response: Update the argument used to invoke the application


Instruction:
Can you generate the documentation for the following Python 3 function
def run_command(self, stream=sys.stdout, dry_run=False):
        return run_gtapp(self.__app, stream, dry_run, **self.args)

Response: Runs the command for this link.


Instruction:
Explain what the following Python 3 code does
def command_template(self):
        com_out = self.appname
        for key, val in self.args.items():
            if key in self._options:
                com_out += ' %s={%s}' % (key, key)
            else:
                com_out += ' %s=%s' % (key, val)
        return com_out

Response:
Build and return a string that can be used as a template invoking
        this chain from the command line.


Instruction:
Here you have a function in Python 3, explain what it does
def run_analysis(self, argv):
        args = self._parser.parse_args(argv)
        components = Component.build_from_yamlfile(args.comp)
        NAME_FACTORY.update_base_dict(args.data)
        model_dict = make_library(**args.__dict__)
        model_manager = model_dict['ModelManager']
        models = load_yaml(args.models)
        data = args.data
        hpx_order = args.hpx_order
        for modelkey in models:
            model_manager.make_srcmap_manifest(modelkey, components, data)
            model_manager.make_fermipy_config_yaml(modelkey, components, data,
                                                   hpx_order=hpx_order,
                                                   irf_ver=NAME_FACTORY.irf_ver())

Response: Build the manifest for all the models


Instruction:
How would you explain what the following Python 3 function does
def copy_ccube(ccube, outsrcmap, hpx_order):
        sys.stdout.write("  Copying counts cube from %s to %s\n" % (ccube, outsrcmap))
        try:
            hdulist_in = fits.open(ccube)
        except IOError:
            hdulist_in = fits.open("%s.gz" % ccube)

        hpx_order_in = hdulist_in[1].header['ORDER']

        if hpx_order_in > hpx_order:
            hpxmap = HpxMap.create_from_hdulist(hdulist_in)
            hpxmap_out = hpxmap.ud_grade(hpx_order, preserve_counts=True)
            hpxlist_out = hdulist_in
            #hpxlist_out['SKYMAP'] = hpxmap_out.create_image_hdu()
            hpxlist_out[1] = hpxmap_out.create_image_hdu()
            hpxlist_out[1].name = 'SKYMAP'
            hpxlist_out.writeto(outsrcmap)
            return hpx_order
        else:
            os.system('cp %s %s' % (ccube, outsrcmap))
            #os.system('cp %s.gz %s.gz' % (ccube, outsrcmap))
            #os.system('gunzip -f %s.gz' % (outsrcmap))
        return None

Response: Copy a counts cube into outsrcmap file with HEALPix order to hpx_order.


Instruction:
Make a summary of the following Python 3 code
def append_hdus(hdulist, srcmap_file, source_names, hpx_order):
        sys.stdout.write("  Extracting %i sources from %s" % (len(source_names), srcmap_file))
        try:
            hdulist_in = fits.open(srcmap_file)
        except IOError:
            try:
                hdulist_in = fits.open('%s.gz' % srcmap_file)
            except IOError:
                sys.stdout.write("  Missing file %s\n" % srcmap_file)
                return

        for source_name in source_names:
            sys.stdout.write('.')
            sys.stdout.flush()
            if hpx_order is None:
                hdulist.append(hdulist_in[source_name])
            else:
                try:
                    hpxmap = HpxMap.create_from_hdulist(hdulist_in, hdu=source_name)
                except IndexError:
                    print("  Index error on source %s in file %s" % (source_name, srcmap_file))
                    continue
                except KeyError:
                    print("  Key error on source %s in file %s" % (source_name, srcmap_file))
                    continue
                hpxmap_out = hpxmap.ud_grade(hpx_order, preserve_counts=True)
                hdulist.append(hpxmap_out.create_image_hdu(name=source_name))
        sys.stdout.write("\n")
        hdulist.flush()
        hdulist_in.close()

Response: Append HEALPix maps to a list of HEALPix maps


Instruction:
Create a Python 3 function for
assembling the source map file for one binning component.

Response:
def assemble_component(compname, compinfo, hpx_order):
        """Assemble the source map file for one binning component

        Parameters
        ----------

        compname : str
            The key for this component (e.g., E0_PSF3)
        compinfo : dict
            Information about this component
        hpx_order : int
            Maximum order for maps

        """
        sys.stdout.write("Working on component %s\n" % compname)
        ccube = compinfo['ccube']
        outsrcmap = compinfo['outsrcmap']
        source_dict = compinfo['source_dict']

        hpx_order = AssembleModel.copy_ccube(ccube, outsrcmap, hpx_order)
        hdulist = AssembleModel.open_outsrcmap(outsrcmap)

        for comp_name in sorted(source_dict.keys()):
            source_info = source_dict[comp_name]
            source_names = source_info['source_names']
            srcmap_file = source_info['srcmap_file']
            AssembleModel.append_hdus(hdulist, srcmap_file,
                                      source_names, hpx_order)
        sys.stdout.write("Done!\n")


Instruction:
Given the following Python 3 function, write the documentation
def run_analysis(self, argv):
        args = self._parser.parse_args(argv)
        manifest = yaml.safe_load(open(args.input))

        compname = args.compname
        value = manifest[compname]
        self.assemble_component(compname, value, args.hpx_order)

Response: Assemble the source map file for one binning component FIXME - use this method instead of the base class