Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_file_paths(self, file_archive, file_id_array):
        full_list = []
        status_dict = {}
        full_list += file_archive.get_file_paths(
            file_id_array[self.infile_ids])
        full_list += file_archive.get_file_paths(
            file_id_array[self.outfile_ids])
        full_list += file_archive.get_file_paths(
            file_id_array[self.rmfile_ids])
        full_list += file_archive.get_file_paths(
            file_id_array[self.intfile_ids])
        for filepath in full_list:
            handle = file_archive.get_handle(filepath)
            status_dict[filepath] = handle.status
        if self.file_dict is None:
            self.file_dict = FileDict()
        self.file_dict.update(status_dict)

Response: Get the full paths of the files used by this object from the id arrays containing the file ids.


Instruction:
Write a Python 3 script to
fill an array from a list

Response:
def _fill_array_from_list(the_list, the_array):
        """Fill an `array` from a `list`"""
        for i, val in enumerate(the_list):
            the_array[i] = val
        return the_array


Instruction:
Can you create a Python 3 function that
builds a dictionary map int to JobDetails

Response:
def make_dict(cls, table):
        """Build a dictionary map int to `JobDetails` from an `astropy.table.Table`"""
        ret_dict = {}
        for row in table:
            job_details = cls.create_from_row(row)
        ret_dict[job_details.dbkey] = job_details
        return ret_dict


Instruction:
Implement a function in Python 3 to
create a JobDetails object from an astropy. table. row. Row object.

Response:
def create_from_row(cls, table_row):
        """Create a `JobDetails` from an `astropy.table.row.Row` """
        kwargs = {}
        for key in table_row.colnames:
            kwargs[key] = table_row[key]

        infile_refs = kwargs.pop('infile_refs')
        outfile_refs = kwargs.pop('outfile_refs')
        rmfile_refs = kwargs.pop('rmfile_refs')
        intfile_refs = kwargs.pop('intfile_refs')

        kwargs['infile_ids'] = np.arange(infile_refs[0], infile_refs[1])
        kwargs['outfile_ids'] = np.arange(outfile_refs[0], outfile_refs[1])
        kwargs['rmfile_ids'] = np.arange(rmfile_refs[0], rmfile_refs[1])
        kwargs['intfile_ids'] = np.arange(intfile_refs[0], intfile_refs[1])
        return cls(**kwargs)


Instruction:
Write a Python 3 function for
appending this instance to a table.

Response:
def append_to_tables(self, table, table_ids):
        """Add this instance as a row on a `astropy.table.Table` """
        infile_refs = np.zeros((2), int)
        outfile_refs = np.zeros((2), int)
        rmfile_refs = np.zeros((2), int)
        intfile_refs = np.zeros((2), int)
        f_ptr = len(table_ids['file_id'])
        infile_refs[0] = f_ptr
        if self.infile_ids is not None:
            for fid in self.infile_ids:
                table_ids.add_row(dict(file_id=fid))
                f_ptr += 1
        infile_refs[1] = f_ptr
        outfile_refs[0] = f_ptr
        if self.outfile_ids is not None:
            for fid in self.outfile_ids:
                table_ids.add_row(dict(file_id=fid))
                f_ptr += 1
        outfile_refs[1] = f_ptr
        rmfile_refs[0] = f_ptr
        if self.rmfile_ids is not None:
            for fid in self.rmfile_ids:
                table_ids.add_row(dict(file_id=fid))
                f_ptr += 1
        rmfile_refs[1] = f_ptr
        intfile_refs[0] = f_ptr
        if self.intfile_ids is not None:
            for fid in self.intfile_ids:
                table_ids.add_row(dict(file_id=fid))
                f_ptr += 1
        intfile_refs[1] = f_ptr

        table.add_row(dict(dbkey=self.dbkey,
                           jobname=self.jobname,
                           jobkey=self.jobkey,
                           appname=self.appname,
                           logfile=self.logfile,
                           job_config=str(self.job_config),
                           timestamp=self.timestamp,
                           infile_refs=infile_refs,
                           outfile_refs=outfile_refs,
                           rmfile_refs=rmfile_refs,
                           intfile_refs=intfile_refs,
                           status=self.status))


Instruction:
Can you create a Python 3 function that
updates the values of a row in a table.

Response:
def update_table_row(self, table, row_idx):
        """Add this instance as a row on a `astropy.table.Table` """
        try:
            table[row_idx]['timestamp'] = self.timestamp
            table[row_idx]['status'] = self.status
        except IndexError:
            print("Index error", len(table), row_idx)


Instruction:
How would you explain what the following Python 3 function does
def check_status_logfile(self, checker_func):
        self.status = checker_func(self.logfile)
        return self.status

Response: Check on the status of this particular job using the logfile


Instruction:
Can you implement a function in Python 3 that
fills the cache from the table.

Response:
def _fill_cache(self):
        """Fill the cache from the `astropy.table.Table`"""
        for irow in range(len(self._table)):
            job_details = self.make_job_details(irow)
            self._cache[job_details.fullkey] = job_details


Instruction:
Can you tell what is the following Python 3 function doing
def _read_table_file(self, table_file):
        self._table_file = table_file
        if os.path.exists(self._table_file):
            self._table = Table.read(self._table_file, hdu='JOB_ARCHIVE')
            self._table_ids = Table.read(self._table_file, hdu='FILE_IDS')
        else:
            self._table, self._table_ids = JobDetails.make_tables({})
        self._table_id_array = self._table_ids['file_id'].data
        self._fill_cache()

Response: Read an astropy. table. Table from table_file to set up the JobArchive


Instruction:
Can you create a Python 3 function that
creates a JobDetails object from an astropy. table. row. Row

Response:
def make_job_details(self, row_idx):
        """Create a `JobDetails` from an `astropy.table.row.Row` """
        row = self._table[row_idx]
        job_details = JobDetails.create_from_row(row)
        job_details.get_file_paths(self._file_archive, self._table_id_array)
        self._cache[job_details.fullkey] = job_details
        return job_details


Instruction:
Can you generate the documentation for the following Python 3 function
def get_details(self, jobname, jobkey):
        fullkey = JobDetails.make_fullkey(jobname, jobkey)
        return self._cache[fullkey]

Response: Get the JobDetails associated to a particular job instance


Instruction:
Create a Python 3 function to
register a job in this JobArchive.

Response:
def register_job(self, job_details):
        """Register a job in this `JobArchive` """
        # check to see if the job already exists
        try:
            job_details_old = self.get_details(job_details.jobname,
                                               job_details.jobkey)
            if job_details_old.status <= JobStatus.running:
                job_details_old.status = job_details.status
                job_details_old.update_table_row(
                    self._table, job_details_old.dbkey - 1)
            job_details = job_details_old
        except KeyError:
            job_details.dbkey = len(self._table) + 1
            job_details.get_file_ids(
                self._file_archive, creator=job_details.dbkey)
            job_details.append_to_tables(self._table, self._table_ids)
        self._table_id_array = self._table_ids['file_id'].data
        self._cache[job_details.fullkey] = job_details
        return job_details


Instruction:
Can you implement a function in Python 3 that
registers a bunch of jobs in this archive

Response:
def register_jobs(self, job_dict):
        """Register a bunch of jobs in this archive"""
        njobs = len(job_dict)
        sys.stdout.write("Registering %i total jobs: " % njobs)
        for i, job_details in enumerate(job_dict.values()):
            if i % 10 == 0:
                sys.stdout.write('.')
                sys.stdout.flush()
            self.register_job(job_details)
        sys.stdout.write('!\n')


Instruction:
Write a Python 3 script to
register a job in the JobArchive from a Link object

Response:
def register_job_from_link(self, link, key, **kwargs):
        """Register a job in the `JobArchive` from a `Link` object """
        job_config = kwargs.get('job_config', None)
        if job_config is None:
            job_config = link.args
        status = kwargs.get('status', JobStatus.unknown)
        job_details = JobDetails(jobname=link.linkname,
                                 jobkey=key,
                                 appname=link.appname,
                                 logfile=kwargs.get('logfile'),
                                 jobconfig=job_config,
                                 timestamp=get_timestamp(),
                                 file_dict=copy.deepcopy(link.files),
                                 sub_file_dict=copy.deepcopy(link.sub_files),
                                 status=status)
        self.register_job(job_details)
        return job_details


Instruction:
Can you tell what is the following Python 3 function doing
def update_job(self, job_details):
        other = self.get_details(job_details.jobname,
                                 job_details.jobkey)
        other.timestamp = job_details.timestamp
        other.status = job_details.status
        other.update_table_row(self._table, other.dbkey - 1)
        return other

Response: Update a job in the JobArchive


Instruction:
How would you implement a function in Python 3 that
marks all jobs that match a mask as removed

Response:
def remove_jobs(self, mask):
        """Mark all jobs that match a mask as 'removed' """
        jobnames = self.table[mask]['jobname']
        jobkey = self.table[mask]['jobkey']
        self.table[mask]['status'] = JobStatus.removed
        for jobname, jobkey in zip(jobnames, jobkey):
            fullkey = JobDetails.make_fullkey(jobname, jobkey)
            self._cache.pop(fullkey).status = JobStatus.removed
        self.write_table_file()


Instruction:
Given the following Python 3 function, write the documentation
def build_temp_job_archive(cls):
        try:
            os.unlink('job_archive_temp.fits')
            os.unlink('file_archive_temp.fits')
        except OSError:
            pass

        cls._archive = cls(job_archive_table='job_archive_temp.fits',
                           file_archive_table='file_archive_temp.fits',
                           base_path=os.path.abspath('.') + '/')
        return cls._archive

Response:
Build and return a JobArchive using the locations of
        persistent files.


Instruction:
Can you tell what is the following Python 3 function doing
def write_table_file(self, job_table_file=None, file_table_file=None):
        if self._table is None:
            raise RuntimeError("No table to write")
        if self._table_ids is None:
            raise RuntimeError("No ID table to write")
        if job_table_file is not None:
            self._table_file = job_table_file
        if self._table_file is None:
            raise RuntimeError("No output file specified for table")
        write_tables_to_fits(self._table_file, [self._table, self._table_ids], clobber=True,
                             namelist=['JOB_ARCHIVE', 'FILE_IDS'])
        self._file_archive.write_table_file(file_table_file)

Response: Write the table to the file_table_file


Instruction:
Write a Python 3 function that can
update the status of all the jobs in the archive

Response:
def update_job_status(self, checker_func):
        """Update the status of all the jobs in the archive"""
        njobs = len(self.cache.keys())
        status_vect = np.zeros((8), int)
        sys.stdout.write("Updating status of %i jobs: " % njobs)
        sys.stdout.flush()
        for i, key in enumerate(self.cache.keys()):
            if i % 200 == 0:
                sys.stdout.write('.')
                sys.stdout.flush()
            job_details = self.cache[key]
            if job_details.status in [JobStatus.pending, JobStatus.running]:
                if checker_func:
                    job_details.check_status_logfile(checker_func)
            job_details.update_table_row(self._table, job_details.dbkey - 1)
            status_vect[job_details.status] += 1

        sys.stdout.write("!\n")
        sys.stdout.flush()
        sys.stdout.write("Summary:\n")
        sys.stdout.write("  Unknown:   %i\n" % status_vect[JobStatus.unknown])
        sys.stdout.write("  Not Ready: %i\n" %
                         status_vect[JobStatus.not_ready])
        sys.stdout.write("  Ready:     %i\n" % status_vect[JobStatus.ready])
        sys.stdout.write("  Pending:   %i\n" % status_vect[JobStatus.pending])
        sys.stdout.write("  Running:   %i\n" % status_vect[JobStatus.running])
        sys.stdout.write("  Done:      %i\n" % status_vect[JobStatus.done])
        sys.stdout.write("  Failed:    %i\n" % status_vect[JobStatus.failed])
        sys.stdout.write("  Partial:   %i\n" %
                         status_vect[JobStatus.partial_failed])


Instruction:
Write a Python 3 function for
returning the singleton JobArchive instance if needed

Response:
def build_archive(cls, **kwargs):
        """Return the singleton `JobArchive` instance, building it if needed """
        if cls._archive is None:
            cls._archive = cls(**kwargs)
        return cls._archive


Instruction:
Can you implement a function in Python 3 that
gets the elapsed time.

Response:
def elapsed_time(self):
        """Get the elapsed time."""

        # Timer is running
        if self._t0 is not None:
            return self._time + self._get_time()
        else:
            return self._time


Instruction:
Write a Python 3 function for
constructing and return a roi_model. Source object

Response:
def make_spatialmap_source(name, Spatial_Filename, spectrum):
    """Construct and return a `fermipy.roi_model.Source` object
    """
    data = dict(Spatial_Filename=Spatial_Filename,
                ra=0.0, dec=0.0,
                SpatialType='SpatialMap',
                Source_Name=name)
    if spectrum is not None:
        data.update(spectrum)

    return roi_model.Source(name, data)


Instruction:
Given the following Python 3 function, write the documentation
def make_mapcube_source(name, Spatial_Filename, spectrum):
    data = dict(Spatial_Filename=Spatial_Filename)
    if spectrum is not None:
        data.update(spectrum)

    return roi_model.MapCubeSource(name, data)

Response: Construct and return a fermipy. roi_model. MapCubeSource object


Instruction:
Create a Python 3 function for
constructing and return an Isoropic Source object

Response:
def make_isotropic_source(name, Spectrum_Filename, spectrum):
    """Construct and return a `fermipy.roi_model.IsoSource` object
    """
    data = dict(Spectrum_Filename=Spectrum_Filename)
    if spectrum is not None:
        data.update(spectrum)

    return roi_model.IsoSource(name, data)


Instruction:
Can you create a Python 3 function that
constructs and return a fermipy. roi_model. CompositeSource object

Response:
def make_composite_source(name, spectrum):
    """Construct and return a `fermipy.roi_model.CompositeSource` object
    """
    data = dict(SpatialType='CompositeSource',
                SpatialModel='CompositeSource',
                SourceType='CompositeSource')
    if spectrum is not None:
        data.update(spectrum)
    return roi_model.CompositeSource(name, data)


Instruction:
Here you have a function in Python 3, explain what it does
def make_catalog_sources(catalog_roi_model, source_names):
    sources = {}
    for source_name in source_names:
        sources[source_name] = catalog_roi_model[source_name]
    return sources

Response:
Construct and return a dictionary of sources that are a subset of sources
    in catalog_roi_model.


Instruction:
Write a Python 3 function for
making dictionary mapping component keys to a source or set of sources

Response:
def make_sources(comp_key, comp_dict):
    """Make dictionary mapping component keys to a source
    or set of sources

    Parameters
    ----------

    comp_key : str
        Key used to access sources

    comp_dict : dict
        Information used to build sources

    return `OrderedDict` maping comp_key to `fermipy.roi_model.Source`
    """
    srcdict = OrderedDict()
    try:
        comp_info = comp_dict.info
    except AttributeError:
        comp_info = comp_dict
    try:
        spectrum = comp_dict.spectrum
    except AttributeError:
        spectrum = None

    model_type = comp_info.model_type
    if model_type == 'PointSource':
        srcdict[comp_key] = make_point_source(comp_info.source_name,
                                              comp_info.src_dict)
    elif model_type == 'SpatialMap':
        srcdict[comp_key] = make_spatialmap_source(comp_info.source_name,
                                                   comp_info.Spatial_Filename,
                                                   spectrum)
    elif model_type == 'MapCubeSource':
        srcdict[comp_key] = make_mapcube_source(comp_info.source_name,
                                                comp_info.Spatial_Filename,
                                                spectrum)
    elif model_type == 'IsoSource':
        srcdict[comp_key] = make_isotropic_source(comp_info.source_name,
                                                  comp_info.Spectral_Filename,
                                                  spectrum)
    elif model_type == 'CompositeSource':
        srcdict[comp_key] = make_composite_source(comp_info.source_name,
                                                  spectrum)
    elif model_type == 'CatalogSources':
        srcdict.update(make_catalog_sources(comp_info.roi_model,
                                            comp_info.source_names))
    else:
        raise ValueError("Unrecognized model_type %s" % model_type)
    return srcdict


Instruction:
Make a summary of the following Python 3 code
def add_sources(self, source_info_dict):
        self._source_info_dict.update(source_info_dict)
        for key, value in source_info_dict.items():
            self._sources.update(make_sources(key, value))

Response: Add all of the sources in source_info_dict to this factory s _sources list.


Instruction:
Make a summary of the following Python 3 code
def build_catalog(**kwargs):
        catalog_type = kwargs.get('catalog_type')
        catalog_file = kwargs.get('catalog_file')
        catalog_extdir = kwargs.get('catalog_extdir')
        if catalog_type == '2FHL':
            return catalog.Catalog2FHL(fitsfile=catalog_file, extdir=catalog_extdir)
        elif catalog_type == '3FGL':
            return catalog.Catalog3FGL(fitsfile=catalog_file, extdir=catalog_extdir)
        elif catalog_type == '4FGLP':
            return catalog.Catalog4FGLP(fitsfile=catalog_file, extdir=catalog_extdir)
        elif catalog_type == 'FL8Y':
            return catalog.CatalogFL8Y(fitsfile=catalog_file, extdir=catalog_extdir)
        else:
            table = Table.read(catalog_file)
        return catalog.Catalog(table, extdir=catalog_extdir)

Response: Build a fermipy. catalog. Catalog object from a FITS file with catalog tables and extended source templates.


Instruction:
Given the following Python 3 function, write the documentation
def make_fermipy_roi_model_from_catalogs(cataloglist):
        data = dict(catalogs=cataloglist,
                    src_roiwidth=360.)
        return roi_model.ROIModel(data, skydir=SkyCoord(0.0, 0.0, unit='deg'))

Response: Build and return a fermipy. roi_model. ROIModel object from a list of fermipy. catalog. Catalog objects


Instruction:
Can you write a function in Python 3 where it
builds and return a fermipy. roi_model. ROIModel object from a dict with information about the sources

Response:
def make_roi(cls, sources=None):
        """Build and return a `fermipy.roi_model.ROIModel` object from
        a dict with information about the sources
        """
        if sources is None:
            sources = {}
        src_fact = cls()
        src_fact.add_sources(sources)
        ret_model = roi_model.ROIModel(
            {}, skydir=SkyCoord(0.0, 0.0, unit='deg'))
        for source in src_fact.sources.values():
            ret_model.load_source(source,
                                  build_index=False, merge_sources=False)
        return ret_model


Instruction:
Can you implement a function in Python 3 that
builds and return a Fermipy. roi_model. ROIModel object by copying selected sources from another such object .

Response:
def copy_selected_sources(cls, roi, source_names):
        """Build and return a `fermipy.roi_model.ROIModel` object
        by copying selected sources from another such object
        """
        roi_new = cls.make_roi()
        for source_name in source_names:
            try:
                src_cp = roi.copy_source(source_name)
            except Exception:
                continue
            roi_new.load_source(src_cp, build_index=False)
        return roi_new


Instruction:
Can you generate the documentation for the following Python 3 function
def build_from_yamlfile(yamlfile):
        d = yaml.load(open(yamlfile))
        return MktimeFilterDict(d['aliases'], d['selections'])

Response: Build a list of components from a yaml file


Instruction:
Write a Python 3 function for
collecting all the jobs in a list of directories.

Response:
def collect_jobs(dirs, runscript, overwrite=False, max_job_age=90):
    """Construct a list of job dictionaries."""

    jobs = []

    for dirname in sorted(dirs):

        o = dict(cfgfile=os.path.join(dirname, 'config.yaml'),
                 logfile=os.path.join(
                     dirname, os.path.splitext(runscript)[0] + '.log'),
                 runscript=os.path.join(dirname, runscript))

        if not os.path.isfile(o['cfgfile']):
            continue

        if not os.path.isfile(o['runscript']):
            continue

        if not os.path.isfile(o['logfile']):
            jobs.append(o)
            continue

        age = file_age_in_seconds(o['logfile']) / 60.
        job_status = check_log(o['logfile'])

        print(dirname, job_status, age)

        if job_status is False or overwrite:
            jobs.append(o)
        elif job_status == 'Exited':
            print("Job Exited. Resending command.")
            jobs.append(o)
        elif job_status == 'None' and age > max_job_age:
            print(
                "Job did not exit, but no activity on log file for > %.2f min. Resending command." % max_job_age)
            jobs.append(o)
        #        elif job_status is True:
        #            print("Job Completed. Resending command.")
        #            jobs.append(o)

    return jobs


Instruction:
Can you write a function in Python 3 where it
computes the source map for a given model.

Response:
def make_srcmap_old(psf, spatial_model, sigma, npix=500, xpix=0.0, ypix=0.0,
                    cdelt=0.01, rebin=1, psf_scale_fn=None):
    """Compute the source map for a given spatial model.

    Parameters
    ----------
    psf : `~fermipy.irfs.PSFModel`
    spatial_model : str
        Spatial model.
    sigma : float
        Spatial size parameter for extended models.
    xpix : float
        Source position in pixel coordinates in X dimension.
    ypix : float
        Source position in pixel coordinates in Y dimension.
    rebin : int
        Factor by which the original map will be oversampled in the
        spatial dimension when computing the model.
    psf_scale_fn : callable
        Function that evaluates the PSF scaling function.
        Argument is energy in MeV.
    """
    if rebin > 1:
        npix = npix * rebin
        xpix = xpix * rebin + (rebin - 1.0) / 2.
        ypix = ypix * rebin + (rebin - 1.0) / 2.
        cdelt = cdelt / rebin

    if spatial_model == 'RadialGaussian':
        k = utils.make_cgauss_kernel(psf, sigma, npix, cdelt,
                                     xpix, ypix, psf_scale_fn)
    elif spatial_model == 'RadialDisk':
        k = utils.make_cdisk_kernel(psf, sigma, npix, cdelt,
                                    xpix, ypix, psf_scale_fn)
    elif spatial_model == 'PointSource':
        k = utils.make_psf_kernel(psf, npix, cdelt,
                                  xpix, ypix, psf_scale_fn)
    else:
        raise Exception('Unsupported spatial model: %s', spatial_model)

    if rebin > 1:
        k = utils.sum_bins(k, 1, rebin)
        k = utils.sum_bins(k, 2, rebin)

    k *= psf.exp[:, np.newaxis, np.newaxis] * np.radians(cdelt) ** 2
    return k


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_srcmap(psf, exp, spatial_model, sigma, npix=500, xpix=0.0, ypix=0.0,
                cdelt=0.01, psf_scale_fn=None, klims=None, sparse=False):
    """Compute the source map for a given spatial model.

    Parameters
    ----------
    psf : `~fermipy.irfs.PSFModel`

    exp : `~numpy.ndarray`
        Array of exposures.

    spatial_model : str
        Spatial model.

    sigma : float
        Spatial size parameter for extended models.

    xpix : float
        Source position in pixel coordinates in X dimension.

    ypix : float
        Source position in pixel coordinates in Y dimension.

    psf_scale_fn : callable
        Function that evaluates the PSF scaling function.
        Argument is energy in MeV.

    klims : tuple
        Indices of lower and upper range of energy.

    sparse : bool
        Skip pixels in which the source amplitude is small.

    """
    if spatial_model == 'RadialGaussian':
        k = utils.make_radial_kernel(psf, utils.convolve2d_gauss,
                                     sigma / 1.5095921854516636, npix, cdelt,
                                     xpix, ypix, psf_scale_fn, klims=klims,
                                     sparse=sparse)
    elif spatial_model == 'RadialDisk':
        k = utils.make_radial_kernel(psf, utils.convolve2d_disk,
                                     sigma / 0.8246211251235321, npix, cdelt,
                                     xpix, ypix, psf_scale_fn, klims=klims,
                                     sparse=sparse)
    elif spatial_model == 'PointSource':
        k = utils.make_radial_kernel(psf, None, None, npix, cdelt,
                                     xpix, ypix, psf_scale_fn, klims=klims,
                                     sparse=sparse)
    else:
        raise Exception('Unsupported spatial model: %s', spatial_model)

    if klims is not None:
        exp = exp[klims[0]:klims[1] + 1, ...]

    k *= exp[:, np.newaxis, np.newaxis] * np.radians(cdelt) ** 2
    return k

Response: Compute the source map for a given PSF model.


Instruction:
Write a Python 3 script for
deleting a map from a binned analysis source map file if it exists.

Response:
def delete_source_map(srcmap_file, names, logger=None):
    """Delete a map from a binned analysis source map file if it exists.

    Parameters
    ----------
    srcmap_file : str
       Path to the source map file.

    names : list
       List of HDU keys of source maps to be deleted.

    """
    with fits.open(srcmap_file) as hdulist:
        hdunames = [hdu.name.upper() for hdu in hdulist]

        if not isinstance(names, list):
            names = [names]

        for name in names:
            if not name.upper() in hdunames:
                continue
            del hdulist[name.upper()]

        hdulist.writeto(srcmap_file, overwrite=True)


Instruction:
Given the following Python 3 function, write the documentation
def get_offsets(self, pix):

        idx = []
        for i in range(self.ndim):

            if i == 0:
                idx += [0]
            else:
                npix1 = int(self.shape[i])
                pix0 = int(pix[i - 1]) - npix1 // 2
                idx += [pix0]

        return idx

Response:
Get the offsets of the first pixel in each dimension in the the
            global coordinate system.


Instruction:
How would you implement a function in Python 3 that
creates a new map that is shifted to the pixel coordinates pix.

Response:
def shift_to_coords(self, pix, fill_value=np.nan):
        """Create a new map that is shifted to the pixel coordinates
        ``pix``."""

        pix_offset = self.get_offsets(pix)
        dpix = np.zeros(len(self.shape) - 1)
        for i in range(len(self.shape) - 1):
            x = self.rebin * (pix[i] - pix_offset[i + 1]
                              ) + (self.rebin - 1.0) / 2.
            dpix[i] = x - self._pix_ref[i]

        pos = [pix_offset[i] + self.shape[i] // 2
               for i in range(self.data.ndim)]
        s0, s1 = utils.overlap_slices(self.shape_out, self.shape, pos)

        k = np.zeros(self.data.shape)
        for i in range(k.shape[0]):
            k[i] = shift(self._data_spline[i], dpix, cval=np.nan,
                         order=2, prefilter=False)

        for i in range(1, len(self.shape)):
            k = utils.sum_bins(k, i, self.rebin)

        k0 = np.ones(self.shape_out) * fill_value

        if k[s1].size == 0 or k0[s0].size == 0:
            return k0
        k0[s0] = k[s1]
        return k0


Instruction:
Implement a Python 3 function for
creating a new map with reference pixel coordinates shifted to the pixel coordinates pix.

Response:
def create_map(self, pix):
        """Create a new map with reference pixel coordinates shifted
        to the pixel coordinates ``pix``.

        Parameters
        ----------
        pix : `~numpy.ndarray`
            Reference pixel of new map.

        Returns
        -------
        out_map : `~numpy.ndarray`
            The shifted map.
        """
        k0 = self._m0.shift_to_coords(pix)
        k1 = self._m1.shift_to_coords(pix)

        k0[np.isfinite(k1)] = k1[np.isfinite(k1)]
        k0[~np.isfinite(k0)] = 0
        return k0


Instruction:
Create a Python 3 function for
converting git release tag into a form that is PEP440 compliant.

Response:
def render_pep440(vcs):
    """Convert git release tag into a form that is PEP440 compliant."""

    if vcs is None:
        return None

    tags = vcs.split('-')

    # Bare version number
    if len(tags) == 1:
        return tags[0]
    else:
        return tags[0] + '+' + '.'.join(tags[1:])


Instruction:
Write a Python 3 function for
reading the release version from _version. py.

Response:
def read_release_version():
    """Read the release version from ``_version.py``."""
    import re
    dirname = os.path.abspath(os.path.dirname(__file__))

    try:
        f = open(os.path.join(dirname, "_version.py"), "rt")
        for line in f.readlines():

            m = re.match("__version__ = '([^']+)'", line)
            if m:
                ver = m.group(1)
                return ver

    except:
        return None

    return None


Instruction:
Create a Python 3 function to
write the release version to _version. py.

Response:
def write_release_version(version):
    """Write the release version to ``_version.py``."""
    dirname = os.path.abspath(os.path.dirname(__file__))
    f = open(os.path.join(dirname, "_version.py"), "wt")
    f.write("__version__ = '%s'\n" % version)
    f.close()


Instruction:
Implement a Python 3 function for
making a full file path by combining tokens and origname.

Response:
def make_full_path(basedir, outkey, origname):
    """Make a full file path by combining tokens

    Parameters
    -----------

    basedir : str
        The top level output area

    outkey : str
        The key for the particular instance of the analysis

    origname : str
        Template for the output file name

    Returns
    -------

    outpath : str
        This will be <basedir>:<outkey>:<newname>.fits
        Where newname = origname.replace('.fits', '_<outkey>.fits')

    """
    return os.path.join(basedir, outkey,
                        os.path.basename(origname).replace('.fits',
                                                           '_%s.fits' % outkey))


Instruction:
Write a Python 3 script 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 """
        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(args['data'])

        outdir = args.get('outdir')
        outkey = args.get('outkey')
        ft1file = args['ft1file']
        ft2file = args['ft2file']
        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)
            linkname = 'select-energy-%s-%s' % (key_e, zcut)
            selectfile_energy = make_full_path(outdir, outkey, NAME_FACTORY.select(**kwargs_select))
            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 'mktimefilters' in comp_e:
                mktimefilters = comp_e['mktimefilters']
            else:
                mktimefilters = ['none']

            for mktimekey in mktimefilters:
                kwargs_mktime = kwargs_select.copy()
                kwargs_mktime['mktime'] = mktimekey
                filterstring = MKTIME_DICT[mktimekey]
                mktime_file = make_full_path(outdir, outkey, NAME_FACTORY.mktime(**kwargs_mktime))
                ltcube_file = make_full_path(outdir, outkey, NAME_FACTORY.ltcube(**kwargs_mktime))
                linkname_mktime = 'mktime-%s-%s-%s' % (key_e, zcut, mktimekey)
                linkname_ltcube = 'ltcube-%s-%s-%s' % (key_e, zcut, mktimekey)

                self._set_link(linkname_mktime, Gtlink_mktime,
                               evfile=selectfile_energy,
                               outfile=mktime_file,
                               scfile=ft2file,
                               filter=filterstring,
                               pfiles=pfiles,
                               logfile=os.path.join(full_out_dir, "%s.log" % linkname_mktime))
                self._set_link(linkname_ltcube, Gtlink_ltcube,
                               evfile=mktime_file,
                               outfile=ltcube_file,
                               scfile=ft2file,
                               zmax=zmax,
                               pfiles=pfiles,
                               logfile=os.path.join(full_out_dir, "%s.log" % linkname_ltcube))

                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-%s' % (
                            key_e, zcut, mktimekey, evtclassval, psf_type)
                        linkname_bin = 'bin-%s-%s-%s-%s-%s' % (key_e,
                                                               zcut, mktimekey,
                                                               evtclassval, psf_type)
                        kwargs_bin = kwargs_mktime.copy()
                        kwargs_bin['psftype'] = psf_type
                        kwargs_bin['coordsys'] = coordsys
                        kwargs_bin['evclass'] = evtclassval
                        selectfile_psf = make_full_path(
                            outdir, outkey, NAME_FACTORY.select(**kwargs_bin))
                        binfile_psf = make_full_path(
                            outdir, outkey, NAME_FACTORY.ccube(**kwargs_bin))
                        hpx_order_psf = min(args['hpx_order_max'], psf_dict['hpx_order'])
                        linkname_select = 'select-type-%s-%s-%s-%s-%s' % (key_e, zcut,
                                                                          mktimekey, evtclassval,
                                                                          psf_type)
                        linkname_bin = 'bin-%s-%s-%s-%s-%s' % (key_e, zcut, mktimekey,
                                                               evtclassval, psf_type)

                        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_psf,
                                       evfile=selectfile_psf,
                                       outfile=binfile_psf,
                                       emin=emin,
                                       emax=emax,
                                       enumbins=enumbins,
                                       pfiles=pfiles,
                                       logfile=os.path.join(full_out_dir, "%s.log" % linkname_bin))


Instruction:
Make a summary of the following Python 3 code
def build_job_configs(self, args):
        job_configs = {}

        comp_file = args.get('comp', None)
        if comp_file is not None:
            comp_dict = yaml.safe_load(open(comp_file))
            coordsys = comp_dict.pop('coordsys')
            for v in comp_dict.values():
                v['coordsys'] = coordsys
        else:
            return job_configs

        datafile = args['data']
        if datafile is None or datafile == 'None':
            return job_configs
        NAME_FACTORY.update_base_dict(args['data'])

        inputfiles = create_inputlist(args['ft1file'])
        outdir_base = os.path.join(NAME_FACTORY.base_dict['basedir'], 'counts_cubes')
        data_ver = NAME_FACTORY.base_dict['data_ver']

        for idx, infile in enumerate(inputfiles):
            key = "%06i" % idx
            key_scfile = "%03i" % (idx + 1)
            output_dir = os.path.join(outdir_base, key)
            try:
                os.mkdir(output_dir)
            except OSError:
                pass
            scfile = args['ft2file'].replace('.lst', '_%s.fits' % key_scfile)
            logfile = make_nfs_path(os.path.join(output_dir,
                                                 'scatter_mk_%s_%s.log' % (data_ver, key)))

            job_configs[key] = comp_dict.copy()
            job_configs[key].update(dict(ft1file=infile,
                                         scfile=scfile,
                                         comp=args['comp'],
                                         hpx_order_max=args['hpx_order_max'],
                                         outdir=outdir_base,
                                         outkey=key,
                                         logfile=logfile,
                                         pfiles=output_dir))

        return job_configs

Response: Hook to build job configurations based on input arguments


Instruction:
Write a Python 3 function for
mapping from the top - level arguments to the top - level 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')
        ft2file = args.get('ft2file')
        scratch = args.get('scratch', None)
        dry_run = args.get('dry_run', None)

        self._set_link('split-and-mktime', SplitAndMktime_SG,
                       comp=comp, data=data,
                       hpx_order_max=args.get('hpx_order_ccube', 9),
                       ft1file=ft1file,
                       ft2file=ft2file,
                       do_ltsum=args.get('do_ltsum', False),
                       scratch=scratch,
                       dry_run=dry_run)

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

        self._set_link('ltsum', Gtltsum_SG,
                       comp=comp, data=data,
                       ft1file=args['ft1file'],
                       dry_run=dry_run)

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


Instruction:
Write a Python 3 function that can
load python data structure from either a YAML or numpy file.

Response:
def load_data(infile, workdir=None):
    """Load python data structure from either a YAML or numpy file. """
    infile = resolve_path(infile, workdir=workdir)
    infile, ext = os.path.splitext(infile)

    if os.path.isfile(infile + '.npy'):
        infile += '.npy'
    elif os.path.isfile(infile + '.yaml'):
        infile += '.yaml'
    else:
        raise Exception('Input file does not exist.')

    ext = os.path.splitext(infile)[1]

    if ext == '.npy':
        return infile, load_npy(infile)
    elif ext == '.yaml':
        return infile, load_yaml(infile)
    else:
        raise Exception('Unrecognized extension.')


Instruction:
Implement a function in Python 3 to
resolve the path of each file name in the file pathlist and write the updated paths to a new file.

Response:
def resolve_file_path_list(pathlist, workdir, prefix='',
                           randomize=False):
    """Resolve the path of each file name in the file ``pathlist`` and
    write the updated paths to a new file.
    """
    files = []
    with open(pathlist, 'r') as f:
        files = [line.strip() for line in f]

    newfiles = []
    for f in files:
        f = os.path.expandvars(f)
        if os.path.isfile(f):
            newfiles += [f]
        else:
            newfiles += [os.path.join(workdir, f)]

    if randomize:
        _, tmppath = tempfile.mkstemp(prefix=prefix, dir=workdir)
    else:
        tmppath = os.path.join(workdir, prefix)

    tmppath += '.txt'

    with open(tmppath, 'w') as tmpfile:
        tmpfile.write("\n".join(newfiles))
    return tmppath


Instruction:
Can you generate the documentation for the following Python 3 function
def match_regex_list(patterns, string):

    for p in patterns:

        if re.findall(p, string):
            return True

    return False

Response: Perform a regex match of a string against a list of patterns. Returns True if the string matches at least one pattern in the list otherwise False.


Instruction:
Given the following Python 3 function, write the documentation
def find_rows_by_string(tab, names, colnames=['assoc']):
    mask = np.empty(len(tab), dtype=bool)
    mask.fill(False)
    names = [name.lower().replace(' ', '') for name in names]

    for colname in colnames:

        if colname not in tab.columns:
            continue

        col = tab[[colname]].copy()
        col[colname] = defchararray.replace(defchararray.lower(col[colname]).astype(str),
                                        ' ', '')
        for name in names:
            mask |= col[colname] == name
    return mask

Response:
Find the rows in a table that match at least one of the the
    strings in names.


Instruction:
How would you implement a function in Python 3 that
evaluates the cosine of the angular separation between two dynamical components.

Response:
def separation_cos_angle(lon0, lat0, lon1, lat1):
    """Evaluate the cosine of the angular separation between two
    direction vectors."""
    return (np.sin(lat1) * np.sin(lat0) + np.cos(lat1) * np.cos(lat0) *
            np.cos(lon1 - lon0))


Instruction:
Implement a Python 3 function for
converting spherical coordinates to cartesian unit vectors.

Response:
def angle_to_cartesian(lon, lat):
    """Convert spherical coordinates to cartesian unit vectors."""
    theta = np.array(np.pi / 2. - lat)
    return np.vstack((np.sin(theta) * np.cos(lon),
                      np.sin(theta) * np.sin(lon),
                      np.cos(theta))).T


Instruction:
How would you explain what the following Python 3 function does
def create_model_name(src):
    o = ''
    spatial_type = src['SpatialModel'].lower()
    o += spatial_type

    if spatial_type == 'gaussian':
        o += '_s%04.2f' % src['SpatialWidth']

    if src['SpectrumType'] == 'PowerLaw':
        o += '_powerlaw_%04.2f' % float(src.spectral_pars['Index']['value'])
    else:
        o += '_%s' % (src['SpectrumType'].lower())

    return o

Response:
Generate a name for a source object given its spatial and spectral
    properties.


Instruction:
Here you have a function in Python 3, explain what it does
def cov_to_correlation(cov):
    err = np.sqrt(np.diag(cov))
    errinv = np.ones_like(err) * np.nan
    m = np.isfinite(err) & (err != 0)
    errinv[m] = 1. / err[m]
    corr = np.array(cov)
    return corr * np.outer(errinv, errinv)

Response: Compute the correlation matrix given the covariance matrix.


Instruction:
Write a Python 3 function for
computing the covariance matrix x and y given the standard deviation along the semi - major axes and the rotation angle of the error ellipse.

Response:
def ellipse_to_cov(sigma_maj, sigma_min, theta):
    """Compute the covariance matrix in two variables x and y given
    the std. deviation along the semi-major and semi-minor axes and
    the rotation angle of the error ellipse.

    Parameters
    ----------
    sigma_maj : float
        Std. deviation along major axis of error ellipse.

    sigma_min : float
        Std. deviation along minor axis of error ellipse.

    theta : float
        Rotation angle in radians from x-axis to ellipse major axis.
    """
    cth = np.cos(theta)
    sth = np.sin(theta)
    covxx = cth**2 * sigma_maj**2 + sth**2 * sigma_min**2
    covyy = sth**2 * sigma_maj**2 + cth**2 * sigma_min**2
    covxy = cth * sth * sigma_maj**2 - cth * sth * sigma_min**2
    return np.array([[covxx, covxy], [covxy, covyy]])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def onesided_cl_to_dlnl(cl):
    alpha = 1.0 - cl
    return 0.5 * np.power(np.sqrt(2.) * special.erfinv(1 - 2 * alpha), 2.)

Response: Compute the delta - loglikelihood value that corresponds to an animal s upper limit of the given confidence level.


Instruction:
Can you implement a function in Python 3 that
finds the root of a function in the interval encompassed by x0 and xb.

Response:
def find_function_root(fn, x0, xb, delta=0.0, bounds=None):
    """Find the root of a function: f(x)+delta in the interval encompassed
    by x0 and xb.

    Parameters
    ----------

    fn : function
       Python function.

    x0 : float
       Fixed bound for the root search.  This will either be used as
       the lower or upper bound depending on the relative value of xb.

    xb : float
       Upper or lower bound for the root search.  If a root is not
       found in the interval [x0,xb]/[xb,x0] this value will be
       increased/decreased until a change in sign is found.

    """

    if x0 == xb:
        return np.nan

    for i in range(10):
        if np.sign(fn(xb) + delta) != np.sign(fn(x0) + delta):
            break
        if bounds is not None and (xb < bounds[0] or xb > bounds[1]):
            break
        if xb < x0:
            xb *= 0.5
        else:
            xb *= 2.0

    # Failed to find a root
    if np.sign(fn(xb) + delta) == np.sign(fn(x0) + delta):
        return np.nan

    if x0 == 0:
        xtol = 1e-10 * np.abs(xb)
    else:
        xtol = 1e-10 * np.abs(xb + x0)

    return brentq(lambda t: fn(t) + delta, x0, xb, xtol=xtol)


Instruction:
Can you write a function in Python 3 where it
computes upper and lower limits peak position and 1 - sigma errors from a 1 - D likelihood function.

Response:
def get_parameter_limits(xval, loglike, cl_limit=0.95, cl_err=0.68269, tol=1E-2,
                         bounds=None):
    """Compute upper/lower limits, peak position, and 1-sigma errors
    from a 1-D likelihood function.  This function uses the
    delta-loglikelihood method to evaluate parameter limits by
    searching for the point at which the change in the log-likelihood
    value with respect to the maximum equals a specific value.  A
    cubic spline fit to the log-likelihood values is used to
    improve the accuracy of the calculation.

    Parameters
    ----------

    xval : `~numpy.ndarray`
       Array of parameter values.

    loglike : `~numpy.ndarray`
       Array of log-likelihood values.

    cl_limit : float
       Confidence level to use for limit calculation.

    cl_err : float
       Confidence level to use for two-sided confidence interval
       calculation.

    tol : float
       Absolute precision of likelihood values.

    Returns
    -------

    x0 : float
        Coordinate at maximum of likelihood function.

    err_lo : float
        Lower error for two-sided confidence interval with CL
        ``cl_err``.  Corresponds to point (x < x0) at which the
        log-likelihood falls by a given value with respect to the
        maximum (0.5 for 1 sigma).  Set to nan if the change in the
        log-likelihood function at the lower bound of the ``xval``
        input array is less than than the value for the given CL.

    err_hi : float
        Upper error for two-sided confidence interval with CL
        ``cl_err``. Corresponds to point (x > x0) at which the
        log-likelihood falls by a given value with respect to the
        maximum (0.5 for 1 sigma).  Set to nan if the change in the
        log-likelihood function at the upper bound of the ``xval``
        input array is less than the value for the given CL.

    err : float
        Symmetric 1-sigma error.  Average of ``err_lo`` and ``err_hi``
        if both are defined.

    ll : float
        Lower limit evaluated at confidence level ``cl_limit``.

    ul : float
        Upper limit evaluated at confidence level ``cl_limit``.

    lnlmax : float
        Log-likelihood value at ``x0``.

    """

    dlnl_limit = onesided_cl_to_dlnl(cl_limit)
    dlnl_err = twosided_cl_to_dlnl(cl_err)

    try:
        # Pad the likelihood function
        # if len(xval) >= 3 and np.max(loglike) - loglike[-1] < 1.5*dlnl_limit:
        #    p = np.polyfit(xval[-3:], loglike[-3:], 2)
        #    x = np.linspace(xval[-1], 10 * xval[-1], 3)[1:]
        #    y = np.polyval(p, x)
        #    x = np.concatenate((xval, x))
        #    y = np.concatenate((loglike, y))
        # else:
        x, y = xval, loglike
        spline = UnivariateSpline(x, y, k=2,
                                  #k=min(len(xval) - 1, 3),
                                  w=(1 / tol) * np.ones(len(x)))
    except:
        print("Failed to create spline: ", xval, loglike)
        return {'x0': np.nan, 'ul': np.nan, 'll': np.nan,
                'err_lo': np.nan, 'err_hi': np.nan, 'err': np.nan,
                'lnlmax': np.nan}

    sd = spline.derivative()

    imax = np.argmax(loglike)
    ilo = max(imax - 1, 0)
    ihi = min(imax + 1, len(xval) - 1)

    # Find the peak
    x0 = xval[imax]

    # Refine the peak position
    if np.sign(sd(xval[ilo])) != np.sign(sd(xval[ihi])):
        x0 = find_function_root(sd, xval[ilo], xval[ihi])

    lnlmax = float(spline(x0))

    def fn(t): return spline(t) - lnlmax
    fn_val = fn(xval)
    if np.any(fn_val[imax:] < -dlnl_limit):
        xhi = xval[imax:][fn_val[imax:] < -dlnl_limit][0]
    else:
        xhi = xval[-1]
    # EAC: brute force check that xhi is greater than x0
    # The fabs is here in case x0 is negative
    if xhi <= x0:
        xhi = x0 + np.fabs(x0)

    if np.any(fn_val[:imax] < -dlnl_limit):
        xlo = xval[:imax][fn_val[:imax] < -dlnl_limit][-1]
    else:
        xlo = xval[0]
    # EAC: brute force check that xlo is less than x0
    # The fabs is here in case x0 is negative
    if xlo >= x0:
        xlo = x0 - 0.5*np.fabs(x0)

    ul = find_function_root(fn, x0, xhi, dlnl_limit, bounds=bounds)
    ll = find_function_root(fn, x0, xlo, dlnl_limit, bounds=bounds)
    err_lo = np.abs(x0 - find_function_root(fn, x0, xlo, dlnl_err,
                                            bounds=bounds))
    err_hi = np.abs(x0 - find_function_root(fn, x0, xhi, dlnl_err,
                                            bounds=bounds))

    err = np.nan
    if np.isfinite(err_lo) and np.isfinite(err_hi):
        err = 0.5 * (err_lo + err_hi)
    elif np.isfinite(err_hi):
        err = err_hi
    elif np.isfinite(err_lo):
        err = err_lo

    o = {'x0': x0, 'ul': ul, 'll': ll,
         'err_lo': err_lo, 'err_hi': err_hi, 'err': err,
         'lnlmax': lnlmax}
    return o


Instruction:
Implement a Python 3 function for
evaluating a 2D parabola given by the input tuple xy.

Response:
def parabola(xy, amplitude, x0, y0, sx, sy, theta):
    """Evaluate a 2D parabola given by:

    f(x,y) = f_0 - (1/2) * \delta^T * R * \Sigma * R^T * \delta

    where

    \delta = [(x - x_0), (y - y_0)]

    and R is the matrix for a 2D rotation by angle \theta and \Sigma
    is the covariance matrix:

    \Sigma = [[1/\sigma_x^2, 0           ],
              [0           , 1/\sigma_y^2]]

    Parameters
    ----------
    xy : tuple
       Tuple containing x and y arrays for the values at which the
       parabola will be evaluated.

    amplitude : float
       Constant offset value.

    x0 : float
       Centroid in x coordinate.

    y0 : float
       Centroid in y coordinate.

    sx : float
       Standard deviation along first axis (x-axis when theta=0).

    sy : float
       Standard deviation along second axis (y-axis when theta=0).

    theta : float
       Rotation angle in radians.

    Returns
    -------
    vals : `~numpy.ndarray`
       Values of the parabola evaluated at the points defined in the
       `xy` input tuple.

    """

    x = xy[0]
    y = xy[1]

    cth = np.cos(theta)
    sth = np.sin(theta)
    a = (cth ** 2) / (2 * sx ** 2) + (sth ** 2) / (2 * sy ** 2)
    b = -(np.sin(2 * theta)) / (4 * sx ** 2) + (np.sin(2 * theta)) / (
        4 * sy ** 2)
    c = (sth ** 2) / (2 * sx ** 2) + (cth ** 2) / (2 * sy ** 2)
    vals = amplitude - (a * ((x - x0) ** 2) +
                        2 * b * (x - x0) * (y - y0) +
                        c * ((y - y0) ** 2))

    return vals


Instruction:
Can you write a function in Python 3 where it
gets mask of connected region within delta of max ( z ).

Response:
def get_region_mask(z, delta, xy=None):
    """Get mask of connected region within delta of max(z)."""

    if xy is None:
        ix, iy = np.unravel_index(np.argmax(z), z.shape)
    else:
        ix, iy = xy

    mz = (z > z[ix, iy] - delta)
    labels = label(mz)[0]
    mz &= labels == labels[ix, iy]
    return mz


Instruction:
Can you create a Python 3 function that
fits a parabola to a 2D numpy array.

Response:
def fit_parabola(z, ix, iy, dpix=3, zmin=None):
    """Fit a parabola to a 2D numpy array.  This function will fit a
    parabola with the functional form described in
    `~fermipy.utils.parabola` to a 2D slice of the input array `z`.
    The fit region encompasses pixels that are within `dpix` of the
    pixel coordinate (iz,iy) OR that have a value relative to the peak
    value greater than `zmin`.

    Parameters
    ----------
    z : `~numpy.ndarray`

    ix : int
       X index of center pixel of fit region in array `z`.

    iy : int
       Y index of center pixel of fit region in array `z`.

    dpix : int
       Max distance from center pixel of fit region.

    zmin : float

    """
    offset = make_pixel_distance(z.shape, iy, ix)
    x, y = np.meshgrid(np.arange(z.shape[0]), np.arange(z.shape[1]),
                       indexing='ij')

    m = (offset <= dpix)
    if np.sum(m) < 9:
        m = (offset <= dpix + 0.5)

    if zmin is not None:
        m |= get_region_mask(z, np.abs(zmin), (ix, iy))

    sx = get_bounded_slice(ix, dpix, z.shape[0])
    sy = get_bounded_slice(iy, dpix, z.shape[1])

    coeffx = poly_to_parabola(np.polyfit(x[sx, iy], z[sx, iy], 2))
    coeffy = poly_to_parabola(np.polyfit(y[ix, sy], z[ix, sy], 2))
    #p0 = [coeffx[2], coeffx[0], coeffy[0], coeffx[1], coeffy[1], 0.0]
    p0 = [coeffx[2], float(ix), float(iy), coeffx[1], coeffy[1], 0.0]

    o = {'fit_success': True, 'p0': p0}

    def curve_fit_fn(*args):
        return np.ravel(parabola(*args))

    try:
        bounds = (-np.inf * np.ones(6), np.inf * np.ones(6))
        bounds[0][1] = -0.5
        bounds[0][2] = -0.5
        bounds[1][1] = z.shape[0] - 0.5
        bounds[1][2] = z.shape[1] - 0.5
        popt, pcov = scipy.optimize.curve_fit(curve_fit_fn,
                                              (np.ravel(x[m]), np.ravel(y[m])),
                                              np.ravel(z[m]), p0, bounds=bounds)
    except Exception:
        popt = copy.deepcopy(p0)
        o['fit_success'] = False

    fm = parabola((x[m], y[m]), *popt)
    df = fm - z[m]
    rchi2 = np.sum(df ** 2) / len(fm)

    o['rchi2'] = rchi2
    o['x0'] = popt[1]
    o['y0'] = popt[2]
    o['sigmax'] = np.abs(popt[3])
    o['sigmay'] = np.abs(popt[4])
    o['sigma'] = np.sqrt(o['sigmax'] ** 2 + o['sigmay'] ** 2)
    o['z0'] = popt[0]
    o['theta'] = popt[5]
    o['popt'] = popt
    o['mask'] = m

    a = max(o['sigmax'], o['sigmay'])
    b = min(o['sigmax'], o['sigmay'])

    o['eccentricity'] = np.sqrt(1 - b ** 2 / a ** 2)
    o['eccentricity2'] = np.sqrt(a ** 2 / b ** 2 - 1)

    return o


Instruction:
Here you have a function in Python 3, explain what it does
def split_bin_edges(edges, npts=2):
    if npts < 2:
        return edges

    x = (edges[:-1, None] +
         (edges[1:, None] - edges[:-1, None]) *
         np.linspace(0.0, 1.0, npts + 1)[None, :])
    return np.unique(np.ravel(x))

Response: Subdivide an array of bins by splitting each bin into npts subintervals.


Instruction:
How would you code a function in Python 3 to
convert axis coordinate to bin index.

Response:
def val_to_bin(edges, x):
    """Convert axis coordinate to bin index."""
    ibin = np.digitize(np.array(x, ndmin=1), edges) - 1
    return ibin


Instruction:
Write a Python 3 script for
converting axis coordinate to bin index.

Response:
def val_to_edge(edges, x):
    """Convert axis coordinate to bin index."""
    edges = np.array(edges)
    w = edges[1:] - edges[:-1]
    w = np.insert(w, 0, w[0])
    ibin = np.digitize(np.array(x, ndmin=1), edges - 0.5 * w) - 1
    ibin[ibin < 0] = 0
    return ibin


Instruction:
Can you generate the documentation for the following Python 3 function
def val_to_bin_bounded(edges, x):
    nbins = len(edges) - 1
    ibin = val_to_bin(edges, x)
    ibin[ibin < 0] = 0
    ibin[ibin > nbins - 1] = nbins - 1
    return ibin

Response: Convert axis coordinate to bin index.


Instruction:
Make a summary of the following Python 3 code
def extend_array(edges, binsz, lo, hi):

    numlo = int(np.ceil((edges[0] - lo) / binsz))
    numhi = int(np.ceil((hi - edges[-1]) / binsz))

    edges = copy.deepcopy(edges)
    if numlo > 0:
        edges_lo = np.linspace(edges[0] - numlo * binsz, edges[0], numlo + 1)
        edges = np.concatenate((edges_lo[:-1], edges))

    if numhi > 0:
        edges_hi = np.linspace(edges[-1], edges[-1] + numhi * binsz, numhi + 1)
        edges = np.concatenate((edges, edges_hi[1:]))

    return edges

Response: Extend an array to encompass lo and hi values.


Instruction:
Create a Python 3 function to
convert a FITS recarray to a python dictionary.

Response:
def fits_recarray_to_dict(table):
    """Convert a FITS recarray to a python dictionary."""

    cols = {}
    for icol, col in enumerate(table.columns.names):

        col_data = table.data[col]
        if type(col_data[0]) == np.float32:
            cols[col] = np.array(col_data, dtype=float)
        elif type(col_data[0]) == np.float64:
            cols[col] = np.array(col_data, dtype=float)
        elif type(col_data[0]) == str:
            cols[col] = np.array(col_data, dtype=str)
        elif type(col_data[0]) == np.string_:
            cols[col] = np.array(col_data, dtype=str)
        elif type(col_data[0]) == np.int16:
            cols[col] = np.array(col_data, dtype=int)
        elif type(col_data[0]) == np.ndarray:
            cols[col] = np.array(col_data)
        else:
            raise Exception(
                'Unrecognized column type: %s %s' % (col, str(type(col_data))))

    return cols


Instruction:
Can you write a function in Python 3 where it
returns a pretty - printed XML string for the Element.

Response:
def prettify_xml(elem):
    """Return a pretty-printed XML string for the Element.
    """
    from xml.dom import minidom
    import xml.etree.cElementTree as et

    rough_string = et.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def tolist(x):
    if isinstance(x, list):
        return map(tolist, x)
    elif isinstance(x, dict):
        return dict((tolist(k), tolist(v)) for k, v in x.items())
    elif isinstance(x, np.ndarray) or isinstance(x, np.number):
        # note, call tolist again to convert strings of numbers to numbers
        return tolist(x.tolist())
    elif isinstance(x, OrderedDict):
        return dict(x)
    elif isinstance(x, np.bool_):
        return bool(x)
    elif isstr(x) or isinstance(x, np.str):
        x = str(x)  # convert unicode & numpy strings
        try:
            return int(x)
        except:
            try:
                return float(x)
            except:
                if x == 'True':
                    return True
                elif x == 'False':
                    return False
                else:
                    return x
    else:
        return x

Response: a convenience function that takes in a nested structure of lists and dictionaries and returns a list of base objects.


Instruction:
Create a Python 3 function for
evaluating the convolution f ( r ) = f ( r ) * g ( r ) where r is azimuthally symmetric function in two dimensions and g is a step function given by nstep.

Response:
def convolve2d_disk(fn, r, sig, nstep=200):
    """Evaluate the convolution f'(r) = f(r) * g(r) where f(r) is
    azimuthally symmetric function in two dimensions and g is a
    step function given by:

    g(r) = H(1-r/s)

    Parameters
    ----------

    fn : function
      Input function that takes a single radial coordinate parameter.

    r :  `~numpy.ndarray`
      Array of points at which the convolution is to be evaluated.

    sig : float
      Radius parameter of the step function.

    nstep : int
      Number of sampling point for numeric integration.
    """

    r = np.array(r, ndmin=1)
    sig = np.array(sig, ndmin=1)

    rmin = r - sig
    rmax = r + sig
    rmin[rmin < 0] = 0
    delta = (rmax - rmin) / nstep

    redge = rmin[..., np.newaxis] + \
        delta[..., np.newaxis] * np.linspace(0, nstep, nstep + 1)
    rp = 0.5 * (redge[..., 1:] + redge[..., :-1])
    dr = redge[..., 1:] - redge[..., :-1]
    fnv = fn(rp)

    r = r.reshape(r.shape + (1,))
    cphi = -np.ones(dr.shape)
    m = ((rp + r) / sig < 1) | (r == 0)

    rrp = r * rp
    sx = r ** 2 + rp ** 2 - sig ** 2
    cphi[~m] = sx[~m] / (2 * rrp[~m])
    dphi = 2 * np.arccos(cphi)
    v = rp * fnv * dphi * dr / (np.pi * sig * sig)
    s = np.sum(v, axis=-1)

    return s


Instruction:
Can you generate a brief explanation for the following Python 3 code
def convolve2d_gauss(fn, r, sig, nstep=200):
    r = np.array(r, ndmin=1)
    sig = np.array(sig, ndmin=1)

    rmin = r - 10 * sig
    rmax = r + 10 * sig
    rmin[rmin < 0] = 0
    delta = (rmax - rmin) / nstep

    redge = (rmin[..., np.newaxis] +
             delta[..., np.newaxis] *
             np.linspace(0, nstep, nstep + 1))

    rp = 0.5 * (redge[..., 1:] + redge[..., :-1])
    dr = redge[..., 1:] - redge[..., :-1]
    fnv = fn(rp)

    r = r.reshape(r.shape + (1,))
    sig2 = sig * sig
    x = r * rp / (sig2)

    if 'je_fn' not in convolve2d_gauss.__dict__:
        t = 10 ** np.linspace(-8, 8, 1000)
        t = np.insert(t, 0, [0])
        je = special.ive(0, t)
        convolve2d_gauss.je_fn = UnivariateSpline(t, je, k=2, s=0)

    je = convolve2d_gauss.je_fn(x.flat).reshape(x.shape)
    #je2 = special.ive(0,x)
    v = (rp * fnv / (sig2) * je * np.exp(x - (r * r + rp * rp) /
                                         (2 * sig2)) * dr)
    s = np.sum(v, axis=-1)

    return s

Response: Evaluate the convolution f ( r ) = f ( r ) * g ( r ) where r is azimuthally symmetric function in two dimensions and g is a gaussian with standard deviation s given by 2 * pi * s^2.


Instruction:
Write a Python 3 function for
filling a 2D array with dimensions shape with the distance of each pixel from a reference direction xpix ypix in pixel coordinates.

Response:
def make_pixel_distance(shape, xpix=None, ypix=None):
    """Fill a 2D array with dimensions `shape` with the distance of each
    pixel from a reference direction (xpix,ypix) in pixel coordinates.
    Pixel coordinates are defined such that (0,0) is located at the
    center of the corner pixel.

    """
    if np.isscalar(shape):
        shape = [shape, shape]

    if xpix is None:
        xpix = (shape[1] - 1.0) / 2.

    if ypix is None:
        ypix = (shape[0] - 1.0) / 2.

    dx = np.linspace(0, shape[1] - 1, shape[1]) - xpix
    dy = np.linspace(0, shape[0] - 1, shape[0]) - ypix
    dxy = np.zeros(shape)
    dxy += np.sqrt(dx[np.newaxis, :] ** 2 + dy[:, np.newaxis] ** 2)

    return dxy


Instruction:
Here you have a function in Python 3, explain what it does
def make_gaussian_kernel(sigma, npix=501, cdelt=0.01, xpix=None, ypix=None):

    sigma /= cdelt

    def fn(t, s): return 1. / (2 * np.pi * s ** 2) * np.exp(
        -t ** 2 / (s ** 2 * 2.0))
    dxy = make_pixel_distance(npix, xpix, ypix)
    k = fn(dxy, sigma)
    k /= (np.sum(k) * np.radians(cdelt) ** 2)

    return k

Response: Make a 2D gaussian kernel.


Instruction:
Can you create a Python 3 function that
makes kernel for a 2D disk.

Response:
def make_disk_kernel(radius, npix=501, cdelt=0.01, xpix=None, ypix=None):
    """Make kernel for a 2D disk.

    Parameters
    ----------

    radius : float
      Disk radius in deg.
    """

    radius /= cdelt

    def fn(t, s): return 0.5 * (np.sign(s - t) + 1.0)

    dxy = make_pixel_distance(npix, xpix, ypix)
    k = fn(dxy, radius)
    k /= (np.sum(k) * np.radians(cdelt) ** 2)

    return k


Instruction:
Write a Python 3 script for
making a kernel for a PSF - convolved 2D disk.

Response:
def make_cdisk_kernel(psf, sigma, npix, cdelt, xpix, ypix, psf_scale_fn=None,
                      normalize=False):
    """Make a kernel for a PSF-convolved 2D disk.

    Parameters
    ----------

    psf : `~fermipy.irfs.PSFModel`

    sigma : float
      68% containment radius in degrees.
    """

    sigma /= 0.8246211251235321

    dtheta = psf.dtheta
    egy = psf.energies

    x = make_pixel_distance(npix, xpix, ypix)
    x *= cdelt

    k = np.zeros((len(egy), npix, npix))
    for i in range(len(egy)):
        def fn(t): return psf.eval(i, t, scale_fn=psf_scale_fn)
        psfc = convolve2d_disk(fn, dtheta, sigma)
        k[i] = np.interp(np.ravel(x), dtheta, psfc).reshape(x.shape)

    if normalize:
        k /= (np.sum(k, axis=0)[np.newaxis, ...] * np.radians(cdelt) ** 2)

    return k


Instruction:
Can you create a Python 3 function that
makes a kernel for a general radially symmetric 2D function. Parameters ---------- psf : `~fermipy.irfs.PSFModel` fn : callable Function that evaluates the kernel at a radial coordinate r. sigma : float 68% containment radius in degrees.

Response:
def make_radial_kernel(psf, fn, sigma, npix, cdelt, xpix, ypix, psf_scale_fn=None,
                       normalize=False, klims=None, sparse=False):
    """Make a kernel for a general radially symmetric 2D function.

    Parameters
    ----------

    psf : `~fermipy.irfs.PSFModel`

    fn : callable
        Function that evaluates the kernel at a radial coordinate r.

    sigma : float
        68% containment radius in degrees.
    """

    if klims is None:
        egy = psf.energies
    else:
        egy = psf.energies[klims[0]:klims[1] + 1]
    ang_dist = make_pixel_distance(npix, xpix, ypix) * cdelt
    max_ang_dist = np.max(ang_dist) + cdelt
    #dtheta = np.linspace(0.0, (np.max(ang_dist) * 1.05)**0.5, 200)**2.0
    # z = create_kernel_function_lookup(psf, fn, sigma, egy,
    #                                  dtheta, psf_scale_fn)

    shape = (len(egy), npix, npix)
    k = np.zeros(shape)

    r99 = psf.containment_angle(energies=egy, fraction=0.997)
    r34 = psf.containment_angle(energies=egy, fraction=0.34)

    rmin = np.maximum(r34 / 4., 0.01)
    rmax = np.maximum(r99, 0.1)
    if sigma is not None:
        rmin = np.maximum(rmin, 0.5 * sigma)
        rmax = np.maximum(rmax, 2.0 * r34 + 3.0 * sigma)
    rmax = np.minimum(rmax, max_ang_dist)

    for i in range(len(egy)):

        rebin = min(int(np.ceil(cdelt / rmin[i])), 8)
        if sparse:
            dtheta = np.linspace(0.0, rmax[i]**0.5, 100)**2.0
        else:
            dtheta = np.linspace(0.0, max_ang_dist**0.5, 200)**2.0

        z = eval_radial_kernel(psf, fn, sigma, i, dtheta, psf_scale_fn)
        xdist = make_pixel_distance(npix * rebin,
                                    xpix * rebin + (rebin - 1.0) / 2.,
                                    ypix * rebin + (rebin - 1.0) / 2.)
        xdist *= cdelt / float(rebin)
        #x = val_to_pix(dtheta, np.ravel(xdist))

        if sparse:
            m = np.ravel(xdist) < rmax[i]
            kk = np.zeros(xdist.size)
            #kk[m] = map_coordinates(z, [x[m]], order=2, prefilter=False)
            kk[m] = np.interp(np.ravel(xdist)[m], dtheta, z)
            kk = kk.reshape(xdist.shape)
        else:
            kk = np.interp(np.ravel(xdist), dtheta, z).reshape(xdist.shape)
            # kk = map_coordinates(z, [x], order=2,
            #                     prefilter=False).reshape(xdist.shape)

        if rebin > 1:
            kk = sum_bins(kk, 0, rebin)
            kk = sum_bins(kk, 1, rebin)

        k[i] = kk / float(rebin)**2

    k = k.reshape((len(egy),) + ang_dist.shape)
    if normalize:
        k /= (np.sum(k, axis=0)[np.newaxis, ...] * np.radians(cdelt) ** 2)

    return k


Instruction:
How would you code a function in Python 3 to
generate a kernel for a point - source.

Response:
def make_psf_kernel(psf, npix, cdelt, xpix, ypix, psf_scale_fn=None, normalize=False):
    """
    Generate a kernel for a point-source.

    Parameters
    ----------

    psf : `~fermipy.irfs.PSFModel`

    npix : int
        Number of pixels in X and Y dimensions.

    cdelt : float
        Pixel size in degrees.

    """

    egy = psf.energies
    x = make_pixel_distance(npix, xpix, ypix)
    x *= cdelt

    k = np.zeros((len(egy), npix, npix))
    for i in range(len(egy)):
        k[i] = psf.eval(i, x, scale_fn=psf_scale_fn)

    if normalize:
        k /= (np.sum(k, axis=0)[np.newaxis, ...] * np.radians(cdelt) ** 2)

    return k


Instruction:
Write a Python 3 script to
return a tuple of slices that can be used to extract the overlapping part of a large array and a small array at the given position.

Response:
def overlap_slices(large_array_shape, small_array_shape, position):
    """
    Modified version of `~astropy.nddata.utils.overlap_slices`.

    Get slices for the overlapping part of a small and a large array.

    Given a certain position of the center of the small array, with
    respect to the large array, tuples of slices are returned which can be
    used to extract, add or subtract the small array at the given
    position. This function takes care of the correct behavior at the
    boundaries, where the small array is cut of appropriately.

    Parameters
    ----------
    large_array_shape : tuple
        Shape of the large array.
    small_array_shape : tuple
        Shape of the small array.
    position : tuple
        Position of the small array's center, with respect to the large array.
        Coordinates should be in the same order as the array shape.

    Returns
    -------
    slices_large : tuple of slices
        Slices in all directions for the large array, such that
        ``large_array[slices_large]`` extracts the region of the large array
        that overlaps with the small array.
    slices_small : slice
        Slices in all directions for the small array, such that
        ``small_array[slices_small]`` extracts the region that is inside the
        large array.
    """
    # Get edge coordinates
    edges_min = [int(pos - small_shape // 2) for (pos, small_shape) in
                 zip(position, small_array_shape)]
    edges_max = [int(pos + (small_shape - small_shape // 2)) for
                 (pos, small_shape) in
                 zip(position, small_array_shape)]

    # Set up slices
    slices_large = tuple(slice(max(0, edge_min), min(large_shape, edge_max))
                         for (edge_min, edge_max, large_shape) in
                         zip(edges_min, edges_max, large_array_shape))
    slices_small = tuple(slice(max(0, -edge_min),
                               min(large_shape - edge_min,
                                   edge_max - edge_min))
                         for (edge_min, edge_max, large_shape) in
                         zip(edges_min, edges_max, large_array_shape))

    return slices_large, slices_small


Instruction:
Create a Python 3 function to
build and return a ModelManager object and fill the associated model library

Response:
def make_library(**kwargs):
    """Build and return a ModelManager object and fill the associated model library
    """
    library_yaml = kwargs.pop('library', 'models/library.yaml')
    comp_yaml = kwargs.pop('comp', 'config/binning.yaml')
    basedir = kwargs.pop('basedir', os.path.abspath('.'))

    model_man = kwargs.get('ModelManager', ModelManager(basedir=basedir))

    model_comp_dict = model_man.make_library(library_yaml, library_yaml, comp_yaml)

    return dict(model_comp_dict=model_comp_dict,
                ModelManager=model_man)


Instruction:
Implement a function in Python 3 to
return the list of source for which energy dispersion should be turned off

Response:
def edisp_disable_list(self):
        """ Return the list of source for which energy dispersion should be turned off """
        l = []
        for model_comp in self.model_components.values():
            if model_comp.edisp_disable:
                l += [model_comp.info.source_name]
        return l


Instruction:
Given the following Python 3 function, write the documentation
def make_srcmap_manifest(self, components, name_factory):
        ret_dict = {}

        for comp in components:
            compkey = comp.make_key('{ebin_name}_{evtype_name}')
            zcut = "zmax%i" % comp.zmax
            name_keys = dict(modelkey=self.model_name,
                             zcut=zcut,
                             ebin=comp.ebin_name,
                             mktime='none',
                             psftype=comp.evtype_name,
                             coordsys=comp.coordsys)
            outsrcmap = name_factory.merged_srcmaps(**name_keys)
            ccube = name_factory.ccube(**name_keys)
            src_dict = {}
            for comp_name, model_comp in self.model_components.items():
                comp_info = model_comp.info
                model_type = comp_info.model_type
                name_keys['sourcekey'] = comp_name
                if model_type in ['CatalogSources']:
                    #sourcekey = comp_info.comp_key
                    sources = comp_info.source_names
                    name_keys['sourcekey'] = comp_info.catalog_info.catalog_name
                elif model_type in ['CompositeSource']:
                    #sourcekey = comp_info.sourcekey
                    name_keys['sourcekey'] = comp_info.sourcekey
                    sources = [comp_info.source_name]
                else:
                    #sourcekey = comp_name
                    sources = [comp_info.source_name]
                src_dict[comp_name] = dict(sourcekey=comp_name,
                                           srcmap_file=name_factory.srcmaps(**name_keys),
                                           source_names=sources)
            comp_dict = dict(outsrcmap=outsrcmap,
                             ccube=ccube,
                             source_dict=src_dict)
            ret_dict[compkey] = comp_dict

        return ret_dict

Response: Builds a manifest of the source maps for each component of the analysis.


Instruction:
How would you code a function in Python 3 to
create the ROI model objects for each of the binning components.

Response:
def make_model_rois(self, components, name_factory):
        """ Make the fermipy roi_model objects for each of a set of binning components """
        ret_dict = {}

        # Figure out which sources need to be split by components
        master_roi_source_info = {}
        sub_comp_sources = {}
        for comp_name, model_comp in self.model_components.items():
            comp_info = model_comp.info
            if comp_info.components is None:
                master_roi_source_info[comp_name] = model_comp
            else:
                sub_comp_sources[comp_name] = model_comp

        # Build the xml for the master
        master_roi = SourceFactory.make_roi(master_roi_source_info)
        master_xml_mdl = name_factory.master_srcmdl_xml(
            modelkey=self.model_name)
        print("Writing master ROI model to %s" % master_xml_mdl)
        master_roi.write_xml(master_xml_mdl)
        ret_dict['master'] = master_roi

        # Now deal with the components
        for comp in components:
            zcut = "zmax%i" % comp.zmax
            compkey = "%s_%s" % (zcut, comp.make_key(
                '{ebin_name}_{evtype_name}'))
            # name_keys = dict(zcut=zcut,
            #                 modelkey=self.model_name,
            #                 component=compkey)
            comp_roi_source_info = {}
            for comp_name, model_comp in sub_comp_sources.items():
                comp_info = model_comp.info
                if comp_info.selection_dependent:
                    key = comp.make_key('{ebin_name}_{evtype_name}')
                elif comp_info.moving:
                    key = zcut
                info_clone = comp_info.components[key].clone_and_merge_sub(key)
                comp_roi_source_info[comp_name] =\
                    ModelComponent(info=info_clone,
                                   spectrum=model_comp.spectrum)

            # Build the xml for the component
            comp_roi = SourceFactory.make_roi(comp_roi_source_info)
            comp_xml_mdl = name_factory.comp_srcmdl_xml(modelkey=self.model_name,
                                                        component=compkey)
            print("Writing component ROI model to %s" % comp_xml_mdl)
            comp_roi.write_xml(comp_xml_mdl)
            ret_dict[compkey] = comp_roi

        return ret_dict


Instruction:
Write a Python 3 function that can
read the model yaml file for the diffuse components

Response:
def read_model_yaml(self, modelkey):
        """ Read the yaml file for the diffuse components
        """
        model_yaml = self._name_factory.model_yaml(modelkey=modelkey,
                                                   fullpath=True)
        model = yaml.safe_load(open(model_yaml))
        return model


Instruction:
Create a Python 3 function for
building the library of all the components

Response:
def make_library(self, diffuse_yaml, catalog_yaml, binning_yaml):
        """ Build up the library of all the components

        Parameters
        ----------

        diffuse_yaml : str
            Name of the yaml file with the library of diffuse component definitions
        catalog_yaml : str
            Name of the yaml file width the library of catalog split definitions
        binning_yaml : str
            Name of the yaml file with the binning definitions
        """
        ret_dict = {}
        #catalog_dict = yaml.safe_load(open(catalog_yaml))
        components_dict = Component.build_from_yamlfile(binning_yaml)
        diffuse_ret_dict = make_diffuse_comp_info_dict(GalpropMapManager=self._gmm,
                                                       DiffuseModelManager=self._dmm,
                                                       library=diffuse_yaml,
                                                       components=components_dict)
        catalog_ret_dict = make_catalog_comp_dict(library=catalog_yaml,
                                                  CatalogSourceManager=self._csm)
        ret_dict.update(diffuse_ret_dict['comp_info_dict'])
        ret_dict.update(catalog_ret_dict['comp_info_dict'])
        self._library.update(ret_dict)
        return ret_dict


Instruction:
Implement a Python 3 function for
building a dictionary with the information for a particular model.

Response:
def make_model_info(self, modelkey):
        """ Build a dictionary with the information for a particular model.

        Parameters
        ----------

        modelkey : str
            Key used to identify this particular model

        Return `ModelInfo`
        """
        model = self.read_model_yaml(modelkey)
        sources = model['sources']
        components = OrderedDict()
        spec_model_yaml = self._name_factory.fullpath(localpath=model['spectral_models'])
        self._spec_lib.update(yaml.safe_load(open(spec_model_yaml)))
        for source, source_info in sources.items():
            model_type = source_info.get('model_type', None)
            par_overrides = source_info.get('par_overides', None)
            version = source_info['version']
            spec_type = source_info['SpectrumType']
            edisp_disable = source_info.get('edisp_disable', False)
            sourcekey = "%s_%s" % (source, version)
            if model_type == 'galprop_rings':
                comp_info_dict = self.gmm.diffuse_comp_info_dicts(version)
                def_spec_type = spec_type['default']
                for comp_key, comp_info in comp_info_dict.items():
                    model_comp = ModelComponent(info=comp_info,
                                                spectrum=\
                                                    self._spec_lib[spec_type.get(comp_key,
                                                                                def_spec_type)],
                                                par_overrides=par_overrides,
                                                edisp_disable=edisp_disable)
                    components[comp_key] = model_comp
            elif model_type == 'Catalog':
                comp_info_dict = self.csm.split_comp_info_dict(source, version)
                def_spec_type = spec_type['default']
                for comp_key, comp_info in comp_info_dict.items():
                    model_comp = ModelComponent(info=comp_info,
                                                spectrum=\
                                                    self._spec_lib[spec_type.get(comp_key,
                                                                                def_spec_type)],
                                                par_overrides=par_overrides,
                                                edisp_disable=edisp_disable)
                    components[comp_key] = model_comp
            else:
                comp_info = self.dmm.diffuse_comp_info(sourcekey)
                model_comp = ModelComponent(info=comp_info,
                                            spectrum=self._spec_lib[spec_type],
                                            par_overrides=par_overrides,
                                            edisp_disable=edisp_disable)
                components[sourcekey] = model_comp
        ret_val = ModelInfo(model_name=modelkey,
                            model_components=components)
        self._models[modelkey] = ret_val
        return ret_val


Instruction:
Can you tell what is the following Python 3 function doing
def make_srcmap_manifest(self, modelkey, components, data):
        try:
            model_info = self._models[modelkey]
        except KeyError:
            model_info = self.make_model_info(modelkey)
        self._name_factory.update_base_dict(data)
        outfile = os.path.join('analysis', 'model_%s' %
                               modelkey, 'srcmap_manifest_%s.yaml' % modelkey)
        manifest = model_info.make_srcmap_manifest(
            components, self._name_factory)

        outdir = os.path.dirname(outfile)
        try:
            os.makedirs(outdir)
        except OSError:
            pass
        utils.write_yaml(manifest, outfile)

Response: Builds a yaml file that specfies how to make the srcmap files for a particular model.


Instruction:
How would you explain what the following Python 3 function does
def make_fermipy_config_yaml(self, modelkey, components, data, **kwargs):
        model_dir = os.path.join('analysis', 'model_%s' % modelkey)
        hpx_order = kwargs.get('hpx_order', 9)
        self._name_factory.update_base_dict(data)
        try:
            model_info = self._models[modelkey]
        except KeyError:
            model_info = self.make_model_info(modelkey)

        model_rois = model_info.make_model_rois(components, self._name_factory)

        #source_names = model_info.component_names

        master_xml_mdl = self._name_factory.master_srcmdl_xml(modelkey=modelkey, fullpath=True)

        master_data = dict(scfile=self._name_factory.ft2file(fullpath=True),
                           cacheft1=False)
        master_binning = dict(projtype='HPX',
                              roiwidth=360.,
                              binsperdec=4,
                              coordsys='GAL',
                              hpx_ordering_scheme="RING",
                              hpx_order=hpx_order,
                              hpx_ebin=True)
        # master_fileio = dict(outdir=model_dir,
        #                     logfile=os.path.join(model_dir, 'fermipy.log'))
        master_fileio = dict(logfile='fermipy.log')
        master_gtlike = dict(irfs=self._name_factory.irfs(**kwargs),
                             edisp_disable=model_info.edisp_disable_list(),
                             use_external_srcmap=True)
        master_selection = dict(glat=0., glon=0., radius=180.)
        master_model = dict(catalogs=[os.path.join(model_dir, master_xml_mdl)])
        master_plotting = dict(label_ts_threshold=1e9)

        master = dict(data=master_data,
                      binning=master_binning,
                      fileio=master_fileio,
                      selection=master_selection,
                      gtlike=master_gtlike,
                      model=master_model,
                      plotting=master_plotting,
                      components=[])

        fermipy_dict = master

        #comp_rois = {}

        for comp in components:
            zcut = "zmax%i" % comp.zmax
            compkey = "%s_%s" % (zcut, comp.make_key(
                '{ebin_name}_{evtype_name}'))
            comp_roi = model_rois[compkey]
            name_keys = dict(zcut=zcut,
                             modelkey=modelkey,
                             component=compkey,
                             mktime='none',
                             coordsys=comp.coordsys,
                             fullpath=True)
            comp_data = dict(ltcube=self._name_factory.ltcube(**name_keys))
            comp_selection = dict(logemin=comp.log_emin,
                                  logemax=comp.log_emax,
                                  zmax=comp.zmax,
                                  evtype=comp.evtype)
            comp_binning = dict(enumbins=comp.enumbins,
                                hpx_order=min(comp.hpx_order, hpx_order),
                                coordsys=comp.coordsys)
            comp_gtlike = dict(srcmap=self._name_factory.merged_srcmaps(**name_keys),
                               bexpmap=self._name_factory.bexpcube(**name_keys),
                               use_external_srcmap=True)
            comp_diffuse_xml = [self._name_factory.comp_srcmdl_xml(**name_keys)]
            #comp_roi_source_info = {}
            #diffuse_srcs = []
            #for src in comp_roi.diffuse_sources:
            #    if isinstance(src, MapCubeSource):
            #        diffuse_srcs.append(dict(name=src.name, file=src.mapcube))
            #    elif isinstance(src, IsoSource):
            #        diffuse_srcs.append(dict(name=src.name, file=src.fileFunction))
            #    else:
            #        pass

            comp_model = dict(diffuse_xml=comp_diffuse_xml)
            sub_dict = dict(data=comp_data,
                            binning=comp_binning,
                            selection=comp_selection,
                            gtlike=comp_gtlike,
                            model=comp_model)
            fermipy_dict['components'].append(sub_dict)

        # Add placeholder diffuse sources
        #fermipy_dict['model']['diffuse'] = diffuse_srcs

        outfile = os.path.join(model_dir, 'config.yaml')
        print("Writing fermipy config file %s" % outfile)
        utils.write_yaml(fermipy_dict, outfile)
        return fermipy_dict

Response: Build a fermipy top - level yaml configuration file for a particular model.


Instruction:
How would you explain what the following Python 3 function does
def get_sub_comp_info(source_info, comp):
        sub_comps = source_info.get('components', None)
        if sub_comps is None:
            return source_info.copy()
        moving = source_info.get('moving', False)
        selection_dependent = source_info.get('selection_dependent', False)
        if selection_dependent:
            key = comp.make_key('{ebin_name}_{evtype_name}')
        elif moving:
            key = "zmax%i" % comp.zmax
        ret_dict = source_info.copy()
        ret_dict.update(sub_comps[key])
        return ret_dict

Response: Build and return information about a sub - component for a particular selection


Instruction:
Write a Python 3 function that can
substitute aliases in a cut dictionary.

Response:
def replace_aliases(cut_dict, aliases):
    """Substitute aliases in a cut dictionary."""
    for k, v in cut_dict.items():
        for k0, v0 in aliases.items():
            cut_dict[k] = cut_dict[k].replace(k0, '(%s)' % v0)


Instruction:
Create a Python 3 function for
extracting a list of file paths from a list containing both paths and file lists with one path per line.

Response:
def get_files(files, extnames=['.root']):
    """Extract a list of file paths from a list containing both paths
    and file lists with one path per line."""

    files_out = []
    for f in files:

        mime = mimetypes.guess_type(f)
        if os.path.splitext(f)[1] in extnames:
            files_out += [f]
        elif mime[0] == 'text/plain':
            files_out += list(np.loadtxt(f, unpack=True, dtype='str'))
        else:
            raise Exception('Unrecognized input type.')

    return files_out


Instruction:
Can you create a Python 3 function that
extracts event selection strings from the XML file.

Response:
def get_cuts_from_xml(xmlfile):
    """Extract event selection strings from the XML file."""

    root = ElementTree.ElementTree(file=xmlfile).getroot()
    event_maps = root.findall('EventMap')
    alias_maps = root.findall('AliasDict')[0]

    event_classes = {}
    event_types = {}
    event_aliases = {}

    for m in event_maps:
        if m.attrib['altName'] == 'EVENT_CLASS':
            for c in m.findall('EventCategory'):
                event_classes[c.attrib['name']] = strip(
                    c.find('ShortCut').text)
        elif m.attrib['altName'] == 'EVENT_TYPE':
            for c in m.findall('EventCategory'):
                event_types[c.attrib['name']] = strip(c.find('ShortCut').text)

    for m in alias_maps.findall('Alias'):
        event_aliases[m.attrib['name']] = strip(m.text)

    replace_aliases(event_aliases, event_aliases.copy())
    replace_aliases(event_aliases, event_aliases.copy())
    replace_aliases(event_classes, event_aliases)
    replace_aliases(event_types, event_aliases)

    event_selections = {}
    event_selections.update(event_classes)
    event_selections.update(event_types)
    event_selections.update(event_aliases)

    return event_selections


Instruction:
Can you tell what is the following Python 3 function doing
def set_event_list(tree, selection=None, fraction=None, start_fraction=None):
    import ROOT

    elist = rand_str()

    if selection is None:
        cuts = ''
    else:
        cuts = selection

    if fraction is None or fraction >= 1.0:
        n = tree.Draw(">>%s" % elist, cuts, "goff")
        tree.SetEventList(ROOT.gDirectory.Get(elist))
    elif start_fraction is None:
        nentries = int(tree.GetEntries())
        first_entry = min(int((1.0 - fraction) * nentries), nentries)
        n = tree.Draw(">>%s" % elist, cuts, "goff", nentries, first_entry)
        tree.SetEventList(ROOT.gDirectory.Get(elist))
    else:
        nentries = int(tree.GetEntries())
        first_entry = min(int(start_fraction * nentries), nentries)
        n = first_entry + int(nentries * fraction)
        n = tree.Draw(">>%s" % elist, cuts, "goff",
                      n - first_entry, first_entry)
        tree.SetEventList(ROOT.gDirectory.Get(elist))

    return n

Response: Sets the event list for a tree or chain.


Instruction:
Here you have a function in Python 3, explain what it does
def localize(self, name, **kwargs):
        timer = Timer.create(start=True)
        name = self.roi.get_source_by_name(name).name

        schema = ConfigSchema(self.defaults['localize'],
                              optimizer=self.defaults['optimizer'])
        schema.add_option('use_cache', True)
        schema.add_option('prefix', '')
        config = utils.create_dict(self.config['localize'],
                                   optimizer=self.config['optimizer'])
        config = schema.create_config(config, **kwargs)

        self.logger.info('Running localization for %s' % name)

        free_state = FreeParameterState(self)
        loc = self._localize(name, **config)
        free_state.restore()

        self.logger.info('Finished localization.')

        if config['make_plots']:
            self._plotter.make_localization_plots(loc, self.roi,
                                                  prefix=config['prefix'])

        outfile = \
            utils.format_filename(self.workdir, 'loc',
                                  prefix=[config['prefix'],
                                          name.lower().replace(' ', '_')])

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

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

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

Response: This function finds the best fit position of a source and returns a dictionary containing the results of the localization analysis.