Instruction:
Create a Python 3 function for
making a dictionary mapping the merged component names to list of template files

Response:
def make_ring_dict(self, galkey):
        """ Make a dictionary mapping the merged component names to list of template files

        Parameters
        ----------

        galkey : str
            Unique key for this ring dictionary

        Returns `model_component.GalpropMergedRingInfo`
        """
        galprop_rings = self.read_galprop_rings_yaml(galkey)
        galprop_run = galprop_rings['galprop_run']
        ring_limits = galprop_rings['ring_limits']
        comp_dict = galprop_rings['diffuse_comp_dict']
        remove_rings = galprop_rings.get('remove_rings', [])
        ring_dict = {}
        nring = len(ring_limits) - 1
        for source_name, source_value in comp_dict.items():
            base_dict = dict(source_name=source_name,
                             galkey=galkey,
                             galprop_run=galprop_run)
            for iring in range(nring):
                sourcekey = "%s_%i" % (source_name, iring)
                if sourcekey in remove_rings:
                    continue
                full_key = "%s_%s" % (sourcekey, galkey)
                rings = range(ring_limits[iring], ring_limits[iring + 1])
                base_dict.update(dict(ring=iring,
                                      sourcekey=sourcekey,
                                      files=self.make_ring_filelist(source_value,
                                                                    rings, galprop_run),
                                      merged_gasmap=self.make_merged_name(sourcekey,
                                                                          galkey, False)))
                ring_dict[full_key] = GalpropMergedRingInfo(**base_dict)
        self._ring_dicts[galkey] = ring_dict
        return ring_dict


Instruction:
Implement a function in Python 3 to
make the information about a single merged component.

Response:
def make_diffuse_comp_info(self, merged_name, galkey):
        """ Make the information about a single merged component

        Parameters
        ----------

        merged_name : str
            The name of the merged component
        galkey : str
            A short key identifying the galprop parameters

        Returns `Model_component.ModelComponentInfo`
        """
        kwargs = dict(source_name=merged_name,
                      source_ver=galkey,
                      model_type='MapCubeSource',
                      Spatial_Filename=self.make_merged_name(
                          merged_name, galkey, fullpath=True),
                      srcmdl_name=self.make_xml_name(merged_name, galkey, fullpath=True))
        return MapCubeComponentInfo(**kwargs)


Instruction:
Write a Python 3 function that can
make a dictionary maping from merged component to information about that component

Response:
def make_diffuse_comp_info_dict(self, galkey):
        """ Make a dictionary maping from merged component to information about that component

        Parameters
        ----------

        galkey : str
            A short key identifying the galprop parameters
        """
        galprop_rings = self.read_galprop_rings_yaml(galkey)
        ring_limits = galprop_rings.get('ring_limits')
        comp_dict = galprop_rings.get('diffuse_comp_dict')
        remove_rings = galprop_rings.get('remove_rings', [])
        diffuse_comp_info_dict = {}
        nring = len(ring_limits) - 1
        for source_key in sorted(comp_dict.keys()):
            for iring in range(nring):
                source_name = "%s_%i" % (source_key, iring)
                if source_name in remove_rings:
                    continue
                full_key = "%s_%s" % (source_name, galkey)
                diffuse_comp_info_dict[full_key] =\
                    self.make_diffuse_comp_info(source_name, galkey)
        self._diffuse_comp_info_dicts[galkey] = diffuse_comp_info_dict
        return diffuse_comp_info_dict


Instruction:
Explain what the following Python 3 code does
def make_template_name(self, model_type, sourcekey):
        format_dict = self.__dict__.copy()
        format_dict['sourcekey'] = sourcekey
        if model_type == 'IsoSource':
            return self._name_factory.spectral_template(**format_dict)
        elif model_type in ['MapCubeSource', 'SpatialMap']:
            return self._name_factory.diffuse_template(**format_dict)
        else:
            raise ValueError("Unexpected model_type %s" % model_type)

Response: Makes the name of a template file for particular component.


Instruction:
Create a Python 3 function for
making the name of an xml file for a model definition of a single component.

Response:
def make_xml_name(self, sourcekey):
        """ Make the name of an xml file for a model definition of a single component

        Parameters
        ----------

        sourcekey : str
            Key to identify this component
        """
        format_dict = self.__dict__.copy()
        format_dict['sourcekey'] = sourcekey
        return self._name_factory.srcmdl_xml(**format_dict)


Instruction:
Create a Python 3 function for
making a dictionary mapping the merged component names to list of template files.

Response:
def make_diffuse_comp_info(self, source_name, source_ver, diffuse_dict,
                               components=None, comp_key=None):
        """ Make a dictionary mapping the merged component names to list of template files

        Parameters
        ----------

        source_name : str
           Name of the source
        source_ver : str
           Key identifying the version of the source
        diffuse_dict : dict
           Information about this component
        comp_key : str
           Used when we need to keep track of sub-components, i.e.,
           for moving and selection dependent sources.

        Returns `model_component.ModelComponentInfo` or
        `model_component.IsoComponentInfo`
        """
        model_type = diffuse_dict['model_type']
        sourcekey = '%s_%s' % (source_name, source_ver)
        if comp_key is None:
            template_name = self.make_template_name(model_type, sourcekey)
            srcmdl_name = self.make_xml_name(sourcekey)
        else:
            template_name = self.make_template_name(
                model_type, "%s_%s" % (sourcekey, comp_key))
            srcmdl_name = self.make_xml_name("%s_%s" % (sourcekey, comp_key))

        template_name = self._name_factory.fullpath(localpath=template_name)
        srcmdl_name = self._name_factory.fullpath(localpath=srcmdl_name)

        kwargs = dict(source_name=source_name,
                      source_ver=source_ver,
                      model_type=model_type,
                      srcmdl_name=srcmdl_name,
                      components=components,
                      comp_key=comp_key)
        kwargs.update(diffuse_dict)
        if model_type == 'IsoSource':
            kwargs['Spectral_Filename'] = template_name
            return IsoComponentInfo(**kwargs)
        elif model_type == 'MapCubeSource':
            kwargs['Spatial_Filename'] = template_name
            return MapCubeComponentInfo(**kwargs)
        elif model_type == 'SpatialMap':
            kwargs['Spatial_Filename'] = template_name
            return SpatialMapComponentInfo(**kwargs)
        else:
            raise ValueError("Unexpected model type %s" % model_type)


Instruction:
Implement a Python 3 function for
making a dictionary mapping from diffuse component to model component info.

Response:
def make_diffuse_comp_info_dict(self, diffuse_sources, components):
        """ Make a dictionary maping from diffuse component to information about that component

        Parameters
        ----------

        diffuse_sources : dict
            Dictionary with diffuse source defintions
        components : dict
            Dictionary with event selection defintions,
            needed for selection depenedent diffuse components

        Returns
        -------

        ret_dict : dict
            Dictionary mapping sourcekey to `model_component.ModelComponentInfo`
        """
        ret_dict = {}
        for key, value in diffuse_sources.items():
            if value is None:
                continue
            model_type = value.get('model_type', 'MapCubeSource')
            if model_type in ['galprop_rings', 'catalog']:
                continue
            selection_dependent = value.get('selection_dependent', False)
            moving = value.get('moving', False)
            versions = value.get('versions', [])
            for version in versions:
                # sourcekey = self._name_factory.sourcekey(source_name=key,
                #                                         source_ver=version)
                comp_dict = None
                if selection_dependent:
                    # For selection dependent diffuse sources we need to split
                    # by binning component
                    comp_dict = {}
                    for comp in components:
                        comp_key = comp.make_key('{ebin_name}_{evtype_name}')
                        comp_dict[comp_key] = self.make_diffuse_comp_info(
                            key, version, value, None, comp_key)
                elif moving:
                    # For moving diffuse sources we need to split by zmax cut
                    comp_dict = {}
                    zmax_dict = {}
                    for comp in components:
                        zmax_dict[int(comp.zmax)] = True
                    zmax_list = sorted(zmax_dict.keys())
                    for zmax in zmax_list:
                        comp_key = "zmax%i" % (zmax)
                        comp_dict[comp_key] = self.make_diffuse_comp_info(
                            key, version, value, None, comp_key)

                comp_info = self.make_diffuse_comp_info(
                    key, version, value, comp_dict)
                ret_dict[comp_info.sourcekey] = comp_info

        self._diffuse_comp_info_dict.update(ret_dict)
        return ret_dict


Instruction:
How would you explain what the following Python 3 function does
def get_unique_match(table, colname, value):
    # FIXME, This is here for python 3.5, where astropy is now returning bytes
    # instead of str
    if table[colname].dtype.kind in ['S', 'U']:
        mask = table[colname].astype(str) == value
    else:
        mask = table[colname] == value

    if mask.sum() != 1:
        raise KeyError("%i rows in column %s match value %s" %
                       (mask.sum(), colname, value))
    return np.argmax(mask)

Response: Get the row matching value for a particular column.


Instruction:
Can you generate the documentation for the following Python 3 function
def latch_file_info(self, args):
        self.file_dict.clear()
        for key, val in self.file_args.items():
            try:
                file_path = args[key]
                if file_path is None:
                    continue
                # 'args' is special
                if key[0:4] == 'args':
                    if isinstance(file_path, list):
                        tokens = file_path
                    elif isinstance(file_path, str):
                        tokens = file_path.split()
                    else:
                        raise TypeError(
                            "Args has type %s, expect list or str" % type(file_path))
                    for token in tokens:
                        self.file_dict[token.replace('.gz', '')] = val
                else:
                    self.file_dict[file_path.replace('.gz', '')] = val
            except KeyError:
                pass

Response: Extract the file paths from a set of arguments and store them in self. file_dict.


Instruction:
Can you implement a function in Python 3 that
updates self with values from a dictionary mapping file path to FileFlags enum

Response:
def update(self, file_dict):
        """Update self with values from a dictionary
        mapping file path [str] to `FileFlags` enum """
        for key, val in file_dict.items():
            if key in self.file_dict:
                self.file_dict[key] |= val
            else:
                self.file_dict[key] = val


Instruction:
Can you generate a brief explanation for the following Python 3 code
def input_files(self):
        ret_list = []
        for key, val in self.file_dict.items():
            # For input files we only want files that were marked as input
            if val & FileFlags.input_mask:
                ret_list.append(key)
        return ret_list

Response: Return a list of the input files needed by this link.


Instruction:
Write a Python 3 script to
return a list of the output files produced by this link.

Response:
def output_files(self):
        """Return a list of the output files produced by this link.

        For `Link` sub-classes this will return the union
        of all the output files of each internal `Link`.

        That is to say this will include files produced by one
        `Link` in a `Chain` and used as input to another `Link` in the `Chain`
        """
        ret_list = []
        for key, val in self.file_dict.items():
            # For output files we only want files that were marked as output
            if val & FileFlags.output_mask:
                ret_list.append(key)
        return ret_list


Instruction:
Can you generate the documentation for the following Python 3 function
def chain_input_files(self):
        ret_list = []
        for key, val in self.file_dict.items():
            # For chain input files we only want files that were not marked as output
            # (I.e., not produced by some other step in the chain)
            if val & FileFlags.in_ch_mask == FileFlags.input_mask:
                ret_list.append(key)
        return ret_list

Response: Return a list of the input files needed by this chain.


Instruction:
How would you code a function in Python 3 to
return a list of all the output files produced by this link.

Response:
def chain_output_files(self):
        """Return a list of the all the output files produced by this link.

        For `Link` sub-classes this will return only those files
        that were not marked as internal files or marked for removal.
        """
        ret_list = []
        for key, val in self.file_dict.items():
            # For pure input files we only want output files that were not
            # marked as internal or temp
            if val & FileFlags.out_ch_mask == FileFlags.output_mask:
                ret_list.append(key)
        return ret_list


Instruction:
Implement a function in Python 3 to
return a list of the input files needed by this link.

Response:
def input_files_to_stage(self):
        """Return a list of the input files needed by this link.

        For `Link` sub-classes this will return the union
        of all the input files of each internal `Link`.

        That is to say this will include files produced by one
        `Link` in a `Chain` and used as input to another `Link` in the `Chain`
        """
        ret_list = []
        for key, val in self.file_dict.items():
            # For input files we only want files that were marked as input
            if val & FileFlags.in_stage_mask == FileFlags.in_stage_mask:
                ret_list.append(key)
        return ret_list


Instruction:
Here you have a function in Python 3, explain what it does
def output_files_to_stage(self):
        ret_list = []
        for key, val in self.file_dict.items():
            # For input files we only want files that were marked as input
            if val & FileFlags.out_stage_mask == FileFlags.out_stage_mask:
                ret_list.append(key)
        return ret_list

Response: Return a list of the input files needed by this link.


Instruction:
Make a summary of the following Python 3 code
def internal_files(self):
        ret_list = []
        for key, val in self.file_dict.items():
            # For internal files we only want files that were marked as
            # internal
            if val & FileFlags.internal_mask:
                ret_list.append(key)
        return ret_list

Response: Return a list of the intermediate files produced by this link.


Instruction:
Write a Python 3 script for
returning a list of the temporary files produced by this link. This returns all the temporary files produced by this link.

Response:
def temp_files(self):
        """Return a list of the temporary files produced by this link.

        This returns all files that were explicitly marked for removal.
        """
        ret_list = []
        for key, val in self.file_dict.items():
            # For temp files we only want files that were marked for removal
            if val & FileFlags.rm_mask:
                ret_list.append(key)
        return ret_list


Instruction:
How would you explain what the following Python 3 function does
def gzip_files(self):
        ret_list = []
        for key, val in self.file_dict.items():
            # For temp files we only want files that were marked for removal
            if val & FileFlags.gz_mask:
                ret_list.append(key)
        return ret_list

Response: Return a list of the files compressed by this link.


Instruction:
Create a Python 3 function to
print a summary of the files in this file dict. This version explictly counts the union of all input and output files.

Response:
def print_summary(self, stream=sys.stdout, indent=""):
        """Print a summary of the files in this file dict.

        This version explictly counts the union of all input and output files.
        """
        stream.write("%sTotal files      : %i\n" %
                     (indent, len(self.file_dict)))
        stream.write("%s  Input files    : %i\n" %
                     (indent, len(self.input_files)))
        stream.write("%s  Output files   : %i\n" %
                     (indent, len(self.output_files)))
        stream.write("%s  Internal files : %i\n" %
                     (indent, len(self.internal_files)))
        stream.write("%s  Temp files     : %i\n" %
                     (indent, len(self.temp_files)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def print_chain_summary(self, stream=sys.stdout, indent=""):
        stream.write("%sTotal files      : %i\n" %
                     (indent, len(self.file_dict)))
        stream.write("%s  Input files    : %i\n" %
                     (indent, len(self.chain_input_files)))
        stream.write("%s  Output files   : %i\n" %
                     (indent, len(self.chain_output_files)))
        stream.write("%s  Internal files : %i\n" %
                     (indent, len(self.internal_files)))
        stream.write("%s  Temp files     : %i\n" %
                     (indent, len(self.temp_files)))

Response: Print a summary of the files in this file dict.


Instruction:
Given the following Python 3 function, write the documentation
def split_local_path(self, local_file):
        abspath = os.path.abspath(local_file)
        if abspath.find(self.workdir) >= 0:
            relpath = abspath.replace(self.workdir, '')[1:]
            basename = os.path.basename(relpath)
            dirname = os.path.dirname(relpath)
        else:
            basename = os.path.basename(local_file)
            dirname = ''
        return (dirname, basename)

Response: Split the local path into a directory name and a file name.


Instruction:
Can you tell what is the following Python 3 function doing
def construct_scratch_path(self, dirname, basename):
        return os.path.join(self.scratchdir, dirname, basename)

Response: Construct and return a path in the scratch area.


Instruction:
Write a Python 3 function for
constructing and return a path in the scratch area from a local file.

Response:
def get_scratch_path(self, local_file):
        """Construct and return a path in the scratch area from a local file.
        """
        (local_dirname, local_basename) = self.split_local_path(local_file)
        return self.construct_scratch_path(local_dirname, local_basename)


Instruction:
How would you implement a function in Python 3 that
builds a dictionary mapping local paths to scratch paths.

Response:
def map_files(self, local_files):
        """Build a dictionary mapping local paths to scratch paths.

        Parameters
        ----------

        local_files : list
            List of filenames to be mapped to scratch area

        Returns dict
            Mapping local_file : fullpath of scratch file
        """
        ret_dict = {}
        for local_file in local_files:
            ret_dict[local_file] = self.get_scratch_path(local_file)
        return ret_dict


Instruction:
How would you code a function in Python 3 to
make any directories need in the scratch area

Response:
def make_scratch_dirs(file_mapping, dry_run=True):
        """Make any directories need in the scratch area"""
        scratch_dirs = {}
        for value in file_mapping.values():
            scratch_dirname = os.path.dirname(value)
            scratch_dirs[scratch_dirname] = True
        for scratch_dirname in scratch_dirs:
            if dry_run:
                print("mkdir -f %s" % (scratch_dirname))
            else:
                try:
                    os.makedirs(scratch_dirname)
                except OSError:
                    pass


Instruction:
Here you have a function in Python 3, explain what it does
def copy_to_scratch(file_mapping, dry_run=True):
        for key, value in file_mapping.items():
            if not os.path.exists(key):
                continue
            if dry_run:
                print ("copy %s %s" % (key, value))
            else:
                print ("copy %s %s" % (key, value))
                copyfile(key, value)

        return file_mapping

Response: Copy input files to scratch area


Instruction:
Can you generate a brief explanation for the following Python 3 code
def copy_from_scratch(file_mapping, dry_run=True):
        for key, value in file_mapping.items():
            if dry_run:
                print ("copy %s %s" % (value, key))
            else:
                try:
                    outdir = os.path.dirname(key)
                    os.makedirs(outdir)
                except OSError:
                    pass
                print ("copy %s %s" % (value, key))
                copyfile(value, key)
        return file_mapping

Response: Copy files from scratch area to local file system


Instruction:
Can you implement a function in Python 3 that
builds and return an astropy. table. Table to store the file_dict.

Response:
def make_table(file_dict):
        """Build and return an `astropy.table.Table` to store `FileHandle`"""
        col_key = Column(name='key', dtype=int)
        col_path = Column(name='path', dtype='S256')
        col_creator = Column(name='creator', dtype=int)
        col_timestamp = Column(name='timestamp', dtype=int)
        col_status = Column(name='status', dtype=int)
        col_flags = Column(name='flags', dtype=int)
        columns = [col_key, col_path, col_creator,
                   col_timestamp, col_status, col_flags]
        table = Table(data=columns)
        for val in file_dict.values():
            val.append_to_table(table)
        return table


Instruction:
Implement a Python 3 function for
building and return a dict of FileHandle from an astropy. table. Table

Response:
def make_dict(cls, table):
        """Build and return a dict of `FileHandle` from an `astropy.table.Table`

        The dictionary is keyed by FileHandle.key, which is a unique integer for each file
        """
        ret_dict = {}
        for row in table:
            file_handle = cls.create_from_row(row)
        ret_dict[file_handle.key] = file_handle
        return ret_dict


Instruction:
Can you generate the documentation for the following Python 3 function
def create_from_row(cls, table_row):
        kwargs = {}
        for key in table_row.colnames:
            kwargs[key] = table_row[key]
        try:
            return cls(**kwargs)
        except KeyError:
            print(kwargs)

Response: Build and return a FileHandle from an astropy. table. row. Row


Instruction:
Create a Python 3 function for
checking on the status of this particular file

Response:
def check_status(self, basepath=None):
        """Check on the status of this particular file"""
        if basepath is None:
            fullpath = self.path
        else:
            fullpath = os.path.join(basepath, self.path)

        exists = os.path.exists(fullpath)
        if not exists:
            if self.flags & FileFlags.gz_mask != 0:
                fullpath += '.gz'
                exists = os.path.exists(fullpath)
        if exists:
            if self.status == FileStatus.superseded:
                pass
            else:
                self.status = FileStatus.exists
        else:
            if self.status in [FileStatus.no_file,
                               FileStatus.expected,
                               FileStatus.missing,
                               FileStatus.temp_removed]:
                if self.flags & FileFlags.rmint_mask != 0:
                    self.status = FileStatus.temp_removed
            elif self.status == FileStatus.exists:
                self.status = FileStatus.missing
            elif self.status == FileStatus.exists:
                self.status = FileStatus.temp_removed
        return self.status


Instruction:
Create a Python 3 function to
add this instance to a table.

Response:
def append_to_table(self, table):
        """Add this instance as a row on a `astropy.table.Table` """
        table.add_row(dict(path=self.path,
                           key=self.key,
                           creator=self.creator,
                           timestamp=self.timestamp,
                           status=self.status,
                           flags=self.flags))


Instruction:
Create a Python 3 function for
updating the values in an astropy. table. Table for this instances

Response:
def update_table_row(self, table, row_idx):
        """Update the values in an `astropy.table.Table` for this instances"""
        table[row_idx]['path'] = self.path
        table[row_idx]['key'] = self.key
        table[row_idx]['creator'] = self.creator
        table[row_idx]['timestamp'] = self.timestamp
        table[row_idx]['status'] = self.status
        table[row_idx]['flags'] = self.flags


Instruction:
How would you implement a function in Python 3 that
returns the full path to the file.

Response:
def _get_fullpath(self, filepath):
        """Return filepath with the base_path prefixed """
        if filepath[0] == '/':
            return filepath
        return os.path.join(self._base_path, filepath)


Instruction:
Given the following Python 3 function, write the documentation
def _fill_cache(self):
        for irow in range(len(self._table)):
            file_handle = self._make_file_handle(irow)
            self._cache[file_handle.path] = file_handle

Response: Fill the cache from the table.


Instruction:
Given the following Python 3 function, write the documentation
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)
        else:
            self._table = FileHandle.make_table({})
        self._fill_cache()

Response: Read an astropy. table. Table to set up the archive


Instruction:
Given the following Python 3 function, write the documentation
def _make_file_handle(self, row_idx):
        row = self._table[row_idx]
        return FileHandle.create_from_row(row)

Response: Build and return a FileHandle object from an astropy. table. row. Row


Instruction:
Can you create a Python 3 function that
gets the FileHandle object associated to a particular file

Response:
def get_handle(self, filepath):
        """Get the `FileHandle` object associated to a particular file """
        localpath = self._get_localpath(filepath)
        return self._cache[localpath]


Instruction:
Given the following Python 3 function, write the documentation
def register_file(self, filepath, creator, status=FileStatus.no_file, flags=FileFlags.no_flags):
        # check to see if the file already exists
        try:
            file_handle = self.get_handle(filepath)
            raise KeyError("File %s already exists in archive" % filepath)
        except KeyError:
            pass
        localpath = self._get_localpath(filepath)
        if status == FileStatus.exists:
            # Make sure the file really exists
            fullpath = self._get_fullpath(filepath)
            if not os.path.exists(fullpath):
                print("register_file called on called on mising file %s" % fullpath)
                status = FileStatus.missing
                timestamp = 0
            else:
                timestamp = int(os.stat(fullpath).st_mtime)
        else:
            timestamp = 0
        key = len(self._table) + 1
        file_handle = FileHandle(path=localpath,
                                 key=key,
                                 creator=creator,
                                 timestamp=timestamp,
                                 status=status,
                                 flags=flags)
        file_handle.append_to_table(self._table)
        self._cache[localpath] = file_handle
        return file_handle

Response: Register a file in the archive.


Instruction:
Can you write a function in Python 3 where it
updates a file in the archive with the given filepath and creator and status.

Response:
def update_file(self, filepath, creator, status):
        """Update a file in the archive

        If the file does not exists, this raises a `KeyError`

        Parameters
        ----------

        filepath : str
            The path to the file
        creatror : int
            A unique key for the job that created this file
        status   : `FileStatus`
            Enumeration giving current status of file

        Returns `FileHandle`
        """
        file_handle = self.get_handle(filepath)
        if status in [FileStatus.exists, FileStatus.superseded]:
            # Make sure the file really exists
            fullpath = file_handle.fullpath
            if not os.path.exists(fullpath):
                raise ValueError("File %s does not exist" % fullpath)
            timestamp = int(os.stat(fullpath).st_mtime)
        else:
            timestamp = 0
        file_handle.creator = creator
        file_handle.timestamp = timestamp
        file_handle.status = status
        file_handle.update_table_row(self._table, file_handle.key - 1)
        return file_handle


Instruction:
Can you generate the documentation for the following Python 3 function
def get_file_ids(self, file_list, creator=None,
                     status=FileStatus.no_file, file_dict=None):
        """Get or create a list of file ids based on file names

        Parameters
        ----------

        file_list : list
            The paths to the file
        creatror : int
            A unique key for the job that created these files
        status   : `FileStatus`
            Enumeration giving current status of files
        file_dict : `FileDict`
            Mask giving flags set on this file

        Returns list of integers
        """
        ret_list = []
        for fname in file_list:
            if file_dict is None:
                flags = FileFlags.no_flags
            else:
                flags = file_dict.file_dict[fname]
            try:
                fhandle = self.get_handle(fname)
            except KeyError:
                if creator is None:
                    creator = -1
                    # raise KeyError("Can not register a file %s without a creator"%fname)
                fhandle = self.register_file(fname, creator, status, flags)
            ret_list.append(fhandle.key)
        return ret_list

Response: Get or create a list of file ids based on file names and status


Instruction:
Create a Python 3 function to
get a list of file paths based on a set of ids

Response:
def get_file_paths(self, id_list):
        """Get a list of file paths based of a set of ids

        Parameters
        ----------

        id_list : list
            List of integer file keys

        Returns list of file paths
        """
        if id_list is None:
            return []
        try:
            path_array = self._table[id_list - 1]['path']
        except IndexError:
            print("IndexError ", len(self._table), id_list)
            path_array = []
        return [path for path in path_array]


Instruction:
How would you code a function in Python 3 to
write the table to the table file

Response:
def write_table_file(self, table_file=None):
        """Write the table to self._table_file"""
        if self._table is None:
            raise RuntimeError("No table to write")
        if table_file is not None:
            self._table_file = 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], clobber=True,
                             namelist=['FILE_ARCHIVE'])


Instruction:
Can you tell what is the following Python 3 function doing
def update_file_status(self):
        nfiles = len(self.cache.keys())
        status_vect = np.zeros((6), int)
        sys.stdout.write("Updating status of %i files: " % nfiles)
        sys.stdout.flush()
        for i, key in enumerate(self.cache.keys()):
            if i % 200 == 0:
                sys.stdout.write('.')
                sys.stdout.flush()
            fhandle = self.cache[key]
            fhandle.check_status(self._base_path)
            fhandle.update_table_row(self._table, fhandle.key - 1)
            status_vect[fhandle.status] += 1

        sys.stdout.write("!\n")
        sys.stdout.flush()
        sys.stdout.write("Summary:\n")
        sys.stdout.write("  no_file:      %i\n" % status_vect[0])
        sys.stdout.write("  expected:     %i\n" % status_vect[1])
        sys.stdout.write("  exists:       %i\n" % status_vect[2])
        sys.stdout.write("  missing:      %i\n" % status_vect[3])
        sys.stdout.write("  superseded:   %i\n" % status_vect[4])
        sys.stdout.write("  temp_removed: %i\n" % status_vect[5])

Response: Update the status of all the files in the archive


Instruction:
How would you implement a function in Python 3 that
makes the list of input files for a particular energy bin X psf type

Response:
def _make_ltcube_file_list(ltsumfile, num_files):
    """Make the list of input files for a particular energy bin X psf type """
    outbasename = os.path.basename(ltsumfile)
    lt_list_file = ltsumfile.replace('fits', 'lst')
    outfile = open(lt_list_file, 'w')
    for i in range(num_files):
        split_key = "%06i" % i
        output_dir = os.path.join(NAME_FACTORY.base_dict['basedir'], 'counts_cubes', split_key)
        filepath = os.path.join(output_dir, outbasename.replace('.fits', '_%s.fits' % split_key))
        outfile.write(filepath)
        outfile.write("\n")
    outfile.close()
    return '@' + lt_list_file


Instruction:
Write a Python 3 script to
register these classes with the LinkFactory class.

Response:
def register_classes():
    """Register these classes with the `LinkFactory` """
    Gtlink_select.register_class()
    Gtlink_bin.register_class()
    Gtlink_expcube2.register_class()
    Gtlink_scrmaps.register_class()
    Gtlink_mktime.register_class()
    Gtlink_ltcube.register_class()
    Link_FermipyCoadd.register_class()
    Link_FermipyGatherSrcmaps.register_class()
    Link_FermipyVstack.register_class()
    Link_FermipyHealview.register_class()
    Gtexpcube2_SG.register_class()
    Gtltsum_SG.register_class()
    SumRings_SG.register_class()
    Vstack_SG.register_class()
    GatherSrcmaps_SG.register_class()
    Healview_SG.register_class()


Instruction:
Write a Python 3 script to
hook to build job configurations from a YAML file.

Response:
def build_job_configs(self, args):
        """Hook to build job configurations
        """
        job_configs = {}

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

        for comp in components:
            zcut = "zmax%i" % comp.zmax

            mktimelist = copy.copy(comp.mktimefilters)
            if not mktimelist:
                mktimelist.append('none')
            evtclasslist_keys = copy.copy(comp.evtclasses)
            if not evtclasslist_keys:
                evtclasslist_vals = [NAME_FACTORY.base_dict['evclass']]
            else:
                evtclasslist_vals = copy.copy(evtclasslist_keys)

            for mktimekey in mktimelist:
                for evtclassval in evtclasslist_vals:
                    fullkey = comp.make_key(
                        '%s_%s_{ebin_name}_%s_{evtype_name}' %
                        (evtclassval, zcut, mktimekey))
                    name_keys = dict(zcut=zcut,
                                     ebin=comp.ebin_name,
                                     psftype=comp.evtype_name,
                                     coordsys=comp.coordsys,
                                     irf_ver=NAME_FACTORY.irf_ver(),
                                     mktime=mktimekey,
                                     evclass=evtclassval,
                                     fullpath=True)

                    outfile = NAME_FACTORY.bexpcube(**name_keys)
                    cmap = NAME_FACTORY.ccube(**name_keys)
                    infile = NAME_FACTORY.ltcube(**name_keys)
                    logfile = make_nfs_path(outfile.replace('.fits', '.log'))
                    job_configs[fullkey] = dict(cmap=cmap,
                                                infile=infile,
                                                outfile=outfile,
                                                irfs=NAME_FACTORY.irfs(**name_keys),
                                                hpx_order=min(
                                                    comp.hpx_order, args['hpx_order_max']),
                                                evtype=comp.evtype,
                                                logfile=logfile)

        return job_configs


Instruction:
Here you have a function in Python 3, explain what it does
def build_job_configs(self, args):
        job_configs = {}

        gmm = make_ring_dicts(library=args['library'], basedir='.')

        for galkey in gmm.galkeys():
            ring_dict = gmm.ring_dict(galkey)
            for ring_key, ring_info in ring_dict.items():
                output_file = ring_info.merged_gasmap
                file_string = ""
                for fname in ring_info.files:
                    file_string += " %s" % fname
                logfile = make_nfs_path(output_file.replace('.fits', '.log'))
                job_configs[ring_key] = dict(output=output_file,
                                             args=file_string,
                                             logfile=logfile)

        return job_configs

Response: Hook to build job configurations for all the modules in the GEMMAP


Instruction:
Create a Python 3 function to
hook to build job configurations from the given arguments

Response:
def build_job_configs(self, args):
        """Hook to build job configurations
        """
        job_configs = {}

        components = Component.build_from_yamlfile(args['comp'])
        NAME_FACTORY.update_base_dict(args['data'])

        ret_dict = make_diffuse_comp_info_dict(components=components,
                                               library=args['library'],
                                               basedir=NAME_FACTORY.base_dict['basedir'])
        diffuse_comp_info_dict = ret_dict['comp_info_dict']

        for diffuse_comp_info_key in sorted(diffuse_comp_info_dict.keys()):
            diffuse_comp_info_value = diffuse_comp_info_dict[diffuse_comp_info_key]

            for comp in components:
                zcut = "zmax%i" % comp.zmax
                key = comp.make_key('{ebin_name}_{evtype_name}')
                if diffuse_comp_info_value.components is None:
                    sub_comp_info = diffuse_comp_info_value
                else:
                    sub_comp_info = diffuse_comp_info_value.get_component_info(comp)
                name_keys = dict(zcut=zcut,
                                 sourcekey=sub_comp_info.sourcekey,
                                 ebin=comp.ebin_name,
                                 psftype=comp.evtype_name,
                                 mktime='none',
                                 coordsys=comp.coordsys,
                                 irf_ver=NAME_FACTORY.irf_ver(),
                                 fullpath=True)

                outfile = NAME_FACTORY.srcmaps(**name_keys)
                outfile_tokens = os.path.splitext(outfile)
                infile_regexp = "%s_*.fits*" % outfile_tokens[0]
                full_key = "%s_%s" % (sub_comp_info.sourcekey, key)
                logfile = make_nfs_path(outfile.replace('.fits', '.log'))
                job_configs[full_key] = dict(output=outfile,
                                             args=infile_regexp,
                                             hdu=sub_comp_info.source_name,
                                             logfile=logfile)

        return job_configs


Instruction:
Can you create a Python 3 function that
hooks to build job configurations from a yaml file

Response:
def build_job_configs(self, args):
        """Hook to build job configurations
        """
        job_configs = {}

        components = Component.build_from_yamlfile(args['comp'])
        NAME_FACTORY.update_base_dict(args['data'])

        ret_dict = make_catalog_comp_dict(library=args['library'],
                                          basedir=NAME_FACTORY.base_dict['basedir'])
        catalog_info_dict = ret_dict['catalog_info_dict']

        for catalog_name  in catalog_info_dict:
            for comp in components:
                zcut = "zmax%i" % comp.zmax
                key = comp.make_key('{ebin_name}_{evtype_name}')
                name_keys = dict(zcut=zcut,
                                 sourcekey=catalog_name,
                                 ebin=comp.ebin_name,
                                 psftype=comp.evtype_name,
                                 coordsys=comp.coordsys,
                                 irf_ver=NAME_FACTORY.irf_ver(),
                                 mktime='none',
                                 fullpath=True)

                outfile = NAME_FACTORY.srcmaps(**name_keys)
                outfile_tokens = os.path.splitext(outfile)
                infile_regexp = "%s_*.fits" % outfile_tokens[0]
                logfile = make_nfs_path(outfile.replace('.fits', '.log'))
                job_configs[key] = dict(output=outfile,
                                        args=infile_regexp,
                                        logfile=logfile)

        return job_configs


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ln_norm(x, mu, sigma=1.0):
    return np.log(stats.norm(loc=mu, scale=sigma).pdf(x))

Response: Natural log of scipy norm function truncated at zero


Instruction:
Write a Python 3 function for
logging - normal function from scipy

Response:
def lognorm(x, mu, sigma=1.0):
    """ Log-normal function from scipy """
    return stats.lognorm(sigma, scale=mu).pdf(x)


Instruction:
Here you have a function in Python 3, explain what it does
def log10norm(x, mu, sigma=1.0):
    return stats.lognorm(sigma * np.log(10), scale=mu).pdf(x)

Response: Scale scipy lognorm from natural log to base 10


Instruction:
How would you explain what the following Python 3 function does
def lgauss(x, mu, sigma=1.0, logpdf=False):
    x = np.array(x, ndmin=1)

    lmu = np.log10(mu)
    s2 = sigma * sigma

    lx = np.zeros(x.shape)
    v = np.zeros(x.shape)

    lx[x > 0] = np.log10(x[x > 0])

    v = 1. / np.sqrt(2 * s2 * np.pi) * np.exp(-(lx - lmu)**2 / (2 * s2))

    if not logpdf:
        v /= (x * np.log(10.))

    v[x <= 0] = -np.inf

    return v

Response:
Log10 normal distribution...

    x     : Parameter of interest for scanning the pdf
    mu    : Peak of the lognormal distribution (mean of the underlying
            normal distribution is log10(mu)
    sigma : Standard deviation of the underlying normal distribution


Instruction:
Can you write a function in Python 3 where it
creates a prior_functor from a dictionary.

Response:
def create_prior_functor(d):
    """Build a prior from a dictionary.

    Parameters
    ----------
    d     :  A dictionary, it must contain:
       d['functype'] : a recognized function type
                       and all of the required parameters for the
                       prior_functor of the desired type

    Returns
    ----------
    A sub-class of '~fermipy.stats_utils.prior_functor'

    Recognized types are:

    'lognorm'       : Scipy lognormal distribution
    'norm'          : Scipy normal distribution
    'gauss'         : Gaussian truncated at zero
    'lgauss'        : Gaussian in log-space
    'lgauss_like'   : Gaussian in log-space, with arguments reversed.
    'lgauss_logpdf' : ???
    """
    functype = d.get('functype', 'lgauss_like')
    j_ref = d.get('j_ref', 1.0)
    if functype == 'norm':
        return norm_prior(d['mu'], d['sigma'], j_ref)
    elif functype == 'lognorm':
        return lognorm_prior(d['mu'], d['sigma'], j_ref)
    elif functype == 'gauss':
        return function_prior(functype, d['mu'], d['sigma'], gauss, lngauss, j_ref)
    elif functype == 'lgauss':
        return function_prior(functype, d['mu'], d['sigma'], lgauss, lnlgauss, j_ref)
    elif functype in ['lgauss_like', 'lgauss_lik']:
        def fn(x, y, s): return lgauss(y, x, s)

        def lnfn(x, y, s): return lnlgauss(y, x, s)
        return function_prior(functype, d['mu'], d['sigma'], fn, lnfn, j_ref)
    elif functype == 'lgauss_log':
        def fn(x, y, s): return lgauss(x, y, s, logpdf=True)

        def lnfn(x, y, s): return lnlgauss(x, y, s, logpdf=True)
        return function_prior(functype, d['mu'], d['sigma'], fn, lnfn, j_ref)
    else:
        raise KeyError("Unrecognized prior_functor type %s" % functype)


Instruction:
Can you generate the documentation for the following Python 3 function
def marginalization_bins(self):
        log_mean = np.log10(self.mean())
        # Default is to marginalize over two decades,
        # centered on mean, using 1000 bins
        return np.logspace(-1. + log_mean, 1. + log_mean, 1001)/self._j_ref

Response: Return the marginalization bins for the current set of items


Instruction:
Explain what the following Python 3 code does
def profile_bins(self):
        log_mean = np.log10(self.mean())
        log_half_width = max(5. * self.sigma(), 3.)
        # Default is to profile over +-5 sigma,
        # centered on mean, using 100 bins
        return np.logspace(log_mean - log_half_width,
                           log_mean + log_half_width, 101)/self._j_ref

Response: The binning of the species in the log10 distribution


Instruction:
Can you generate a brief explanation for the following Python 3 code
def normalization(self):
        norm_r = self.normalization_range()
        return quad(self, norm_r[0]*self._j_ref, norm_r[1]*self._j_ref)[0]

Response: The normalization of the entry set


Instruction:
Given the following Python 3 function, write the documentation
def init_return(self, ret_type):
        if self._ret_type == ret_type:
            return
        if ret_type == "straight":
            self._interp = self._lnlfn.interp
        if ret_type == "profile":
            self._profile_loglike_spline(self._lnlfn.interp.x)
            #self._profile_loglike(self._lnlfn.interp.x)
            self._interp = self._prof_interp
        elif ret_type == "marginal":
            self._marginal_loglike(self._lnlfn.interp.x)
            self._interp = self._marg_interp
        elif ret_type == "posterior":
            self._posterior(self._lnlfn.interp.x)
            self._interp = self._post_interp
        else:
            raise ValueError("Did not recognize return type %s" % ret_type)

        self._ret_type = ret_type

Response: Initializes the object with the requested return type.


Instruction:
Write a Python 3 script to
remove all of the cached values and interpolators

Response:
def clear_cached_values(self):
        """Removes all of the cached values and interpolators
        """
        self._prof_interp = None
        self._prof_y = None
        self._prof_z = None
        self._marg_interp = None
        self._marg_z = None
        self._post = None
        self._post_interp = None
        self._interp = None
        self._ret_type = None


Instruction:
How would you implement a function in Python 3 that
evaluates the 2 - D likelihood in the x / y parameter space.

Response:
def like(self, x, y):
        """Evaluate the 2-D likelihood in the x/y parameter space.

        The dimension of the two input arrays should be the same.

        Parameters
        ----------
        x : array_like
            Array of coordinates in the `x` parameter.

        y : array_like
            Array of coordinates in the `y` nuisance parameter.
        """
        # This is the negative log-likelihood
        z = self._lnlfn.interp(x * y)
        return np.exp(-z) * self._nuis_pdf(y) / self._nuis_norm


Instruction:
Make a summary of the following Python 3 code
def loglike(self, x, y):
        nuis = self._nuis_pdf(y)
        log_nuis = np.where( nuis > 0., np.log(nuis), -1e2)
        vals = -self._lnlfn.interp(x * y ) + \
            log_nuis - \
            self._nuis_log_norm
        return vals

Response: Evaluate the 2 - D log - likelihood in the x / y parameter space.


Instruction:
Can you write a function in Python 3 where it
profiles log - likelihood.

Response:
def profile_loglike(self, x):
        """Profile log-likelihood.

        Returns ``L_prof(x,y=y_min|z')``  : where y_min is the
                                            value of y that minimizes
                                            L for a given x.

        This will used the cached '~fermipy.castro.Interpolator' object
        if possible, and construct it if needed.
        """
        if self._prof_interp is None:
            # This calculates values and caches the spline
            return self._profile_loglike(x)[1]

        x = np.array(x, ndmin=1)
        return self._prof_interp(x)


Instruction:
Make a summary of the following Python 3 code
def marginal_loglike(self, x):
        if self._marg_interp is None:
            # This calculates values and caches the spline
            return self._marginal_loglike(x)

        x = np.array(x, ndmin=1)
        return self._marg_interp(x)

Response: Marginal log - likelihood.


Instruction:
How would you explain what the following Python 3 function does
def _profile_loglike_spline(self, x):
        z = []
        y = []

        yv = self._nuis_pdf.profile_bins()
        nuis_vals = self._nuis_pdf.log_value(yv) - self._nuis_log_norm
        for xtmp in x:
            zv = -1. * self._lnlfn.interp(xtmp * yv) + nuis_vals
            sp = splrep(yv, zv, k=2, s=0)

            def rf(t): return splev(t, sp, der=1)
            ix = np.argmax(splev(yv, sp))
            imin, imax = max(0, ix - 3), min(len(yv) - 1, ix + 3)
            try:
                y0 = opt.brentq(rf, yv[imin], yv[imax], xtol=1e-10)
            except:
                y0 = yv[ix]
            z0 = self.loglike(xtmp, y0)
            z.append(z0)
            y.append(y0)

        self._prof_y = np.array(y)
        self._prof_z = np.array(z)
        self._prof_z = self._prof_z.max() - self._prof_z

        self._prof_interp = castro.Interpolator(x, self._prof_z)
        return self._prof_y, self._prof_z

Response: Internal function to calculate and cache the profile likelihood


Instruction:
How would you explain what the following Python 3 function does
def _marginal_loglike(self, x):
        yedge = self._nuis_pdf.marginalization_bins()
        yw = yedge[1:] - yedge[:-1]
        yc = 0.5 * (yedge[1:] + yedge[:-1])

        s = self.like(x[:, np.newaxis], yc[np.newaxis, :])

        # This does the marginalization integral
        z = 1. * np.sum(s * yw, axis=1)
        self._marg_z = np.zeros(z.shape)
        msk = z > 0
        self._marg_z[msk] = -1 * np.log(z[msk])

        # Extrapolate to unphysical values
        # FIXME, why is this needed
        dlogzdx = (np.log(z[msk][-1]) - np.log(z[msk][-2])
                   ) / (x[msk][-1] - x[msk][-2])
        self._marg_z[~msk] = self._marg_z[msk][-1] + \
            (self._marg_z[~msk] - self._marg_z[msk][-1]) * dlogzdx
        self._marg_interp = castro.Interpolator(x, self._marg_z)
        return self._marg_z

Response: Internal function to calculate and cache the marginal likelihood


Instruction:
How would you explain what the following Python 3 function does
def _compute_mle(self):
        xmax = self._lnlfn.interp.xmax
        x0 = max(self._lnlfn.mle(), xmax * 1e-5)
        ret = opt.fmin(lambda x: np.where(
            xmax > x > 0, -self(x), np.inf), x0, disp=False)
        mle = float(ret[0])
        return mle

Response: Compute the maximum likelihood estimator.


Instruction:
How would you explain what the following Python 3 function does
def build_from_energy_dict(cls, ebin_name, input_dict):
        psf_types = input_dict.pop('psf_types')
        output_list = []
        for psf_type, val_dict in sorted(psf_types.items()):
            fulldict = input_dict.copy()
            fulldict.update(val_dict)
            fulldict['evtype_name'] = psf_type
            fulldict['ebin_name'] = ebin_name
            component = cls(**fulldict)
            output_list += [component]
        return output_list

Response: Build a list of components from a dictionary for a single energy range


Instruction:
Write a Python 3 script for
building a list of components from a yaml string.

Response:
def build_from_yamlstr(cls, yamlstr):
        """ Build a list of components from a yaml string
        """
        top_dict = yaml.safe_load(yamlstr)
        coordsys = top_dict.pop('coordsys')
        output_list = []
        for e_key, e_dict in sorted(top_dict.items()):
            if e_key == 'coordsys':
                continue
            e_dict = top_dict[e_key]
            e_dict['coordsys'] = coordsys
            output_list += cls.build_from_energy_dict(e_key, e_dict)
        return output_list


Instruction:
Can you tell what is the following Python 3 function doing
def _match_cubes(ccube_clean, ccube_dirty,
                     bexpcube_clean, bexpcube_dirty,
                     hpx_order):
        """ Match the HEALPIX scheme and order of all the input cubes

        return a dictionary of cubes with the same HEALPIX scheme and order
        """
        if hpx_order == ccube_clean.hpx.order:
            ccube_clean_at_order = ccube_clean
        else:
            ccube_clean_at_order = ccube_clean.ud_grade(hpx_order, preserve_counts=True)

        if hpx_order == ccube_dirty.hpx.order:
            ccube_dirty_at_order = ccube_dirty
        else:
            ccube_dirty_at_order = ccube_dirty.ud_grade(hpx_order, preserve_counts=True)

        if hpx_order == bexpcube_clean.hpx.order:
            bexpcube_clean_at_order = bexpcube_clean
        else:
            bexpcube_clean_at_order = bexpcube_clean.ud_grade(hpx_order, preserve_counts=True)

        if hpx_order == bexpcube_dirty.hpx.order:
            bexpcube_dirty_at_order = bexpcube_dirty
        else:
            bexpcube_dirty_at_order = bexpcube_dirty.ud_grade(hpx_order, preserve_counts=True)

        if ccube_dirty_at_order.hpx.nest != ccube_clean.hpx.nest:
            ccube_dirty_at_order = ccube_dirty_at_order.swap_scheme()

        if bexpcube_clean_at_order.hpx.nest != ccube_clean.hpx.nest:
            bexpcube_clean_at_order = bexpcube_clean_at_order.swap_scheme()

        if bexpcube_dirty_at_order.hpx.nest != ccube_clean.hpx.nest:
            bexpcube_dirty_at_order = bexpcube_dirty_at_order.swap_scheme()

        ret_dict = dict(ccube_clean=ccube_clean_at_order,
                        ccube_dirty=ccube_dirty_at_order,
                        bexpcube_clean=bexpcube_clean_at_order,
                        bexpcube_dirty=bexpcube_dirty_at_order)
        return ret_dict

Response:
Match the HEALPIX scheme and order of all the input cubes
Interrupt and return a dictionary of cubes with the same HEALPIX scheme and order of all the input cubes
Interrupt and order of all the input cubes
Interrupt.


Instruction:
Can you generate the documentation for the following Python 3 function
def _compute_intensity(ccube, bexpcube):
        bexp_data = np.sqrt(bexpcube.data[0:-1, 0:] * bexpcube.data[1:, 0:])
        intensity_data = ccube.data / bexp_data
        intensity_map = HpxMap(intensity_data, ccube.hpx)
        return intensity_map

Response: Compute the intensity map from the given Cube and Bexpcube.


Instruction:
How would you explain what the following Python 3 function does
def _compute_mean(map1, map2):
        data = (map1.data + map2.data) / 2.
        return HpxMap(data, map1.hpx)

Response: Compute the mean of two maps


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _compute_ratio(top, bot):
        data = np.where(bot.data > 0, top.data / bot.data, 0.)
        return HpxMap(data, top.hpx)

Response: Compute the ratio of two maps.


Instruction:
Here you have a function in Python 3, explain what it does
def _compute_diff(map1, map2):
        data = map1.data - map2.data
        return HpxMap(data, map1.hpx)

Response: Compute the difference between two maps


Instruction:
Explain what the following Python 3 code does
def _compute_product(map1, map2):
        data = map1.data * map2.data
        return HpxMap(data, map1.hpx)

Response: Compute the product of two maps.


Instruction:
Write a Python 3 function for
computing the counts map from the intensity and bexpcube

Response:
def _compute_counts_from_intensity(intensity, bexpcube):
        """ Make the counts map from the intensity
        """
        data = intensity.data * np.sqrt(bexpcube.data[1:] * bexpcube.data[0:-1])
        return HpxMap(data, intensity.hpx)


Instruction:
Create a Python 3 function to
compute the counts maps from the model and bexpcube

Response:
def _compute_counts_from_model(model, bexpcube):
        """ Make the counts maps from teh mdoe
        """
        data = model.data * bexpcube.data
        ebins = model.hpx.ebins
        ratio = ebins[1:] / ebins[0:-1]
        half_log_ratio = np.log(ratio) / 2.
        int_map = ((data[0:-1].T * ebins[0:-1]) + (data[1:].T * ebins[1:])) * half_log_ratio
        return HpxMap(int_map.T, model.hpx)


Instruction:
Can you tell what is the following Python 3 function doing
def _make_bright_pixel_mask(intensity_mean, mask_factor=5.0):
        mask = np.zeros((intensity_mean.data.shape), bool)
        nebins = len(intensity_mean.data)
        sum_intensity = intensity_mean.data.sum(0)
        mean_intensity = sum_intensity.mean()
        for i in range(nebins):
            mask[i, 0:] = sum_intensity > (mask_factor * mean_intensity)
        return HpxMap(mask, intensity_mean.hpx)

Response: Make a mask of all the brightest pixels


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_aeff_corrections(intensity_ratio, mask):
        nebins = len(intensity_ratio.data)
        aeff_corrections = np.zeros((nebins))
        for i in range(nebins):
            bright_pixels_intensity = intensity_ratio.data[i][mask.data[i]]
            mean_bright_pixel = bright_pixels_intensity.mean()
            aeff_corrections[i] = 1. / mean_bright_pixel

        print("Aeff correction: ", aeff_corrections)
        return aeff_corrections

Response: Compute a correction for the effective area from the brighter pixesl


Instruction:
Make a summary of the following Python 3 code
def _apply_aeff_corrections(intensity_map, aeff_corrections):
        data = aeff_corrections * intensity_map.data.T
        return HpxMap(data.T, intensity_map.hpx)

Response: Multipy a map by the effective area corrections.


Instruction:
Make a summary of the following Python 3 code
def _fill_masked_intensity_resid(intensity_resid, bright_pixel_mask):
        filled_intensity = np.zeros((intensity_resid.data.shape))
        nebins = len(intensity_resid.data)
        for i in range(nebins):
            masked = bright_pixel_mask.data[i]
            unmasked = np.invert(masked)
            mean_intensity = intensity_resid.data[i][unmasked].mean()
            filled_intensity[i] = np.where(masked, mean_intensity, intensity_resid.data[i])
        return HpxMap(filled_intensity, intensity_resid.hpx)

Response: Fill the pixels used to compute the effective area correction with the mean intensity


Instruction:
Can you write a function in Python 3 where it
smooths a healpix map using a Gaussian

Response:
def _smooth_hpx_map(hpx_map, sigma):
        """ Smooth a healpix map using a Gaussian
        """
        if hpx_map.hpx.ordering == "NESTED":
            ring_map = hpx_map.swap_scheme()
        else:
            ring_map = hpx_map
        ring_data = ring_map.data.copy()
        nebins = len(hpx_map.data)
        smoothed_data = np.zeros((hpx_map.data.shape))
        for i in range(nebins):
            smoothed_data[i] = healpy.sphtfunc.smoothing(
                ring_data[i], sigma=np.radians(sigma), verbose=False)

        smoothed_data.clip(0., 1e99)
        smoothed_ring_map = HpxMap(smoothed_data, ring_map.hpx)
        if hpx_map.hpx.ordering == "NESTED":
            return smoothed_ring_map.swap_scheme()
        return smoothed_ring_map


Instruction:
Write a Python 3 function that can
convert an integral quantity to differential quantity.

Response:
def _intergral_to_differential(hpx_map, gamma=-2.0):
        """ Convert integral quantity to differential quantity

        Here we are assuming the spectrum is a powerlaw with index gamma and we
        are using log-log-quadrature to compute the integral quantities.
        """
        nebins = len(hpx_map.data)
        diff_map = np.zeros((nebins + 1, hpx_map.hpx.npix))
        ebins = hpx_map.hpx.ebins
        ratio = ebins[1:] / ebins[0:-1]
        half_log_ratio = np.log(ratio) / 2.
        ratio_gamma = np.power(ratio, gamma)
        #ratio_inv_gamma = np.power(ratio, -1. * gamma)

        diff_map[0] = hpx_map.data[0] / ((ebins[0] + ratio_gamma[0] * ebins[1]) * half_log_ratio[0])
        for i in range(nebins):
            diff_map[i + 1] = (hpx_map.data[i] / (ebins[i + 1] *
                                                  half_log_ratio[i])) - (diff_map[i] / ratio[i])
        return HpxMap(diff_map, hpx_map.hpx)


Instruction:
Write a Python 3 function for
converting a differential map to an integral map.

Response:
def _differential_to_integral(hpx_map):
        """ Convert a differential map to an integral map

        Here we are using log-log-quadrature to compute the integral quantities.
        """
        ebins = hpx_map.hpx.ebins
        ratio = ebins[1:] / ebins[0:-1]
        half_log_ratio = np.log(ratio) / 2.
        int_map = ((hpx_map.data[0:-1].T * ebins[0:-1]) +
                   (hpx_map.data[1:].T * ebins[1:])) * half_log_ratio
        return HpxMap(int_map.T, hpx_map.hpx)


Instruction:
Can you generate the documentation for the following Python 3 function
def run_analysis(self, argv):
        args = self._parser.parse_args(argv)

        # Read the input maps
        ccube_dirty = HpxMap.create_from_fits(args.ccube_dirty, hdu='SKYMAP')
        bexpcube_dirty = HpxMap.create_from_fits(args.bexpcube_dirty, hdu='HPXEXPOSURES')
        ccube_clean = HpxMap.create_from_fits(args.ccube_clean, hdu='SKYMAP')
        bexpcube_clean = HpxMap.create_from_fits(args.bexpcube_clean, hdu='HPXEXPOSURES')

        # Decide what HEALPix order to work at
        if args.hpx_order:
            hpx_order = args.hpx_order
        else:
            hpx_order = ccube_dirty.hpx.order

        # Cast all the input maps to match ccube_clean
        cube_dict = ResidualCR._match_cubes(ccube_clean, ccube_dirty,
                                            bexpcube_clean, bexpcube_dirty, hpx_order)

        # Intenstiy maps
        intensity_clean = ResidualCR._compute_intensity(cube_dict['ccube_clean'],
                                                        cube_dict['bexpcube_clean'])
        intensity_dirty = ResidualCR._compute_intensity(cube_dict['ccube_dirty'],
                                                        cube_dict['bexpcube_dirty'])
        # Mean & ratio of intensity maps
        intensity_mean = ResidualCR._compute_mean(intensity_dirty,
                                                  intensity_clean)
        intensity_ratio = ResidualCR._compute_ratio(intensity_dirty,
                                                    intensity_clean)
        # Selecting the bright pixels for Aeff correction and to mask when filling output map
        bright_pixel_select = ResidualCR._make_bright_pixel_mask(intensity_mean,
                                                                 args.select_factor)
        bright_pixel_mask = ResidualCR._make_bright_pixel_mask(intensity_mean,
                                                               args.mask_factor)
        # Compute thte Aeff corrections using the brightest pixels
        aeff_corrections = ResidualCR._get_aeff_corrections(intensity_ratio,
                                                            bright_pixel_select)
        # Apply the Aeff corrections and get the intensity residual
        corrected_dirty = ResidualCR._apply_aeff_corrections(intensity_dirty,
                                                             aeff_corrections)
        corrected_ratio = ResidualCR._compute_ratio(corrected_dirty,
                                                    intensity_clean)
        intensity_resid = ResidualCR._compute_diff(corrected_dirty,
                                                   intensity_clean)
        # Replace the masked pixels with the map mean to avoid features associates with sources
        filled_resid = ResidualCR._fill_masked_intensity_resid(intensity_resid,
                                                               bright_pixel_mask)
        # Smooth the map
        smooth_resid = ResidualCR._smooth_hpx_map(filled_resid,
                                                  args.sigma)
        # Convert to a differential map
        out_model = ResidualCR._intergral_to_differential(smooth_resid)

        # Make the ENERGIES HDU
        out_energies = ccube_dirty.hpx.make_energies_hdu()

        # Write the maps
        cubes = dict(SKYMAP=out_model)
        fits_utils.write_maps(None, cubes,
                              args.outfile, energy_hdu=out_energies)

        if args.full_output:
            # Some diagnostics
            check = ResidualCR._differential_to_integral(out_model)
            check_resid = ResidualCR._compute_diff(smooth_resid, check)
            counts_resid =\
                ResidualCR._compute_counts_from_intensity(intensity_resid,
                                                          cube_dict['bexpcube_dirty'])
            pred_counts\
                = ResidualCR._compute_counts_from_model(out_model,
                                                        cube_dict['bexpcube_dirty'])
            pred_resid = ResidualCR._compute_diff(pred_counts, counts_resid)

            out_ebounds = ccube_dirty.hpx.make_energy_bounds_hdu()
            cubes = dict(INTENSITY_CLEAN=intensity_clean,
                         INTENSITY_DIRTY=intensity_dirty,
                         INTENSITY_RATIO=intensity_ratio,
                         CORRECTED_DIRTY=corrected_dirty,
                         CORRECTED_RATIO=corrected_ratio,
                         INTENSITY_RESID=intensity_resid,
                         PIXEL_SELECT=bright_pixel_select,
                         PIXEL_MASK=bright_pixel_mask,
                         FILLED_RESID=filled_resid,
                         SMOOTH_RESID=smooth_resid,
                         CHECK=check,
                         CHECK_RESID=check_resid,
                         COUNTS_RESID=counts_resid,
                         PRED_COUNTS=pred_counts,
                         PRED_RESID=pred_resid)

            fits_utils.write_maps(None, cubes,
                                  args.outfile.replace('.fits', '_full.fits'),
                                  energy_hdu=out_ebounds)

Response: Run this analysis on the input maps and return the result of the analysis.


Instruction:
Create a Python 3 function to
hook to build job configurations from the given arguments

Response:
def build_job_configs(self, args):
        """Hook to build job configurations
        """
        job_configs = {}

        components = Component.build_from_yamlfile(args['comp'])
        NAME_FACTORY.update_base_dict(args['data'])
        NAME_FACTORY_CLEAN.update_base_dict(args['data'])
        NAME_FACTORY_DIRTY.update_base_dict(args['data'])

        NAME_FACTORY_CLEAN.base_dict['evclass'] = args['clean']
        NAME_FACTORY_DIRTY.base_dict['evclass'] = args['dirty']

        for comp in components:
            zcut = "zmax%i" % comp.zmax
            key = comp.make_key('{ebin_name}_{evtype_name}')
            name_keys = dict(zcut=zcut,
                             ebin=comp.ebin_name,
                             psftype=comp.evtype_name,
                             coordsys=comp.coordsys,
                             irf_ver=NAME_FACTORY.irf_ver(),
                             mktime=args['mktimefilter'],
                             fullpath=True)
            outfile = NAME_FACTORY.residual_cr(**name_keys)
            if args['hpx_order']:
                hpx_order = min(comp.hpx_order, args['hpx_order'])
            else:
                hpx_order = comp.hpx_order
            job_configs[key] = dict(bexpcube_dirty=NAME_FACTORY_DIRTY.bexpcube(**name_keys),
                                    ccube_dirty=NAME_FACTORY_DIRTY.ccube(**name_keys),
                                    bexpcube_clean=NAME_FACTORY_CLEAN.bexpcube(**name_keys),
                                    ccube_clean=NAME_FACTORY_CLEAN.ccube(**name_keys),
                                    outfile=outfile,
                                    hpx_order=hpx_order,
                                    full_output=args['full_output'],
                                    logfile=make_nfs_path(outfile.replace('.fits', '.log')))

        return job_configs


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 """

        config_yaml = args['config']
        config_dict = load_yaml(config_yaml)

        data = config_dict.get('data')
        comp = config_dict.get('comp')
        dry_run = args.get('dry_run', False)

        self._set_link('prepare', SplitAndMktimeChain,
                       comp=comp, data=data,
                       ft1file=config_dict['ft1file'],
                       ft2file=config_dict['ft2file'],
                       hpx_order_ccube=config_dict.get('hpx_order_ccube', 7),
                       hpx_order_expcube=config_dict.get('hpx_order_expcube', 7),
                       mktime=config_dict.get('mktimefitler', None),
                       do_ltsum=config_dict.get('do_ltsum', False),
                       scratch=config_dict.get('scratch', None),
                       dry_run=dry_run)

        self._set_link('residual-cr', ResidualCR_SG,
                       comp=comp, data=data,
                       mktimefilter=config_dict.get('mktimefitler', None),
                       hpx_order=config_dict.get('hpx_order_fitting', 4),
                       clean=config_dict.get('clean_class', None),
                       dirty=config_dict.get('dirty_class', None),
                       select_factor=config_dict.get('select_factor', None),
                       mask_factor=config_dict.get('mask_factor', None),
                       sigma=config_dict.get('sigma', None),
                       full_output=config_dict.get('full_output', False),
                       dry_run=dry_run)


Instruction:
Create a Python 3 function to
reduce a 3D counts cube to a 2D counts map

Response:
def sum_over_energy(self):
        """ Reduce a 3D counts cube to a 2D counts map
        """
        # Note that the array is using the opposite convention from WCS
        # so we sum over axis 0 in the array, but drop axis 2 in the WCS object
        return Map(np.sum(self.counts, axis=0), self.wcs.dropaxis(2))


Instruction:
Can you create a Python 3 function that
returns the flattened pixel indices from an array multi - dimensional pixel indices.

Response:
def xypix_to_ipix(self, xypix, colwise=False):
        """Return the flattened pixel indices from an array multi-dimensional
        pixel indices.

        Parameters
        ----------
        xypix : list
            List of pixel indices in the order (LON,LAT,ENERGY).

        colwise : bool
            Use column-wise pixel indexing.
        """
        return np.ravel_multi_index(xypix, self.npix,
                                    order='F' if colwise else 'C',
                                    mode='raise')


Instruction:
Can you tell what is the following Python 3 function doing
def ipix_to_xypix(self, ipix, colwise=False):
        return np.unravel_index(ipix, self.npix,
                                order='F' if colwise else 'C')

Response: Return array multi - dimensional pixel indices from flattened index.


Instruction:
Write a Python 3 function for
returning the transposed pixel index from the pixel xy coordinates ipix

Response:
def ipix_swap_axes(self, ipix, colwise=False):
        """ Return the transposed pixel index from the pixel xy coordinates

        if colwise is True (False) this assumes the original index was
        in column wise scheme
        """
        xy = self.ipix_to_xypix(ipix, colwise)
        return self.xypix_to_ipix(xy, not colwise)


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

        xpix = np.linspace(0, self.npix[0] - 1., self.npix[0])
        ypix = np.linspace(0, self.npix[1] - 1., self.npix[1])
        xypix = np.meshgrid(xpix, ypix, indexing='ij')
        return SkyCoord.from_pixel(np.ravel(xypix[0]),
                                   np.ravel(xypix[1]), self.wcs)

Response: Get a list of sky coordinates for the centers of every pixel.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_pixel_indices(self, lons, lats, ibin=None):
        lons = np.array(lons, ndmin=1)
        lats = np.array(lats, ndmin=1)

        if len(lats) != len(lons):
            raise RuntimeError('Map.get_pixel_indices, input lengths '
                               'do not match %i %i' % (len(lons), len(lats)))
        if len(self._npix) == 2:
            pix_x, pix_y = self._wcs.wcs_world2pix(lons, lats, 0)
            pixcrd = [np.floor(pix_x).astype(int), np.floor(pix_y).astype(int)]
        elif len(self._npix) == 3:
            all_lons = np.expand_dims(lons, -1)
            all_lats = np.expand_dims(lats, -1)
            if ibin is None:
                all_bins = (np.expand_dims(
                    np.arange(self.npix[2]), -1) * np.ones(lons.shape)).T
            else:
                all_bins = ibin

            l = self.wcs.wcs_world2pix(all_lons, all_lats, all_bins, 0)
            pix_x = l[0]
            pix_y = l[1]
            pixcrd = [np.floor(l[0]).astype(int), np.floor(l[1]).astype(int),
                      all_bins.astype(int)]

        return pixcrd

Response: Return the indices in the flat array corresponding to a set of coordinates.


Instruction:
Here you have a function in Python 3, explain what it does
def get_map_values(self, lons, lats, ibin=None):
        pix_idxs = self.get_pixel_indices(lons, lats, ibin)
        idxs = copy.copy(pix_idxs)

        m = np.empty_like(idxs[0], dtype=bool)
        m.fill(True)
        for i, p in enumerate(pix_idxs):
            m &= (pix_idxs[i] >= 0) & (pix_idxs[i] < self._npix[i])
            idxs[i][~m] = 0

        vals = self.counts.T[idxs]
        vals[~m] = np.nan
        return vals

Response: Return the values corresponding to a set of coordinates.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_from_hdu(cls, hdu, ebins):
        hpx = HPX.create_from_hdu(hdu, ebins)
        colnames = hdu.columns.names
        cnames = []
        if hpx.conv.convname == 'FGST_SRCMAP_SPARSE':
            pixs = hdu.data.field('PIX')
            chans = hdu.data.field('CHANNEL')
            keys = chans * hpx.npix + pixs
            vals = hdu.data.field('VALUE')
            nebin = len(ebins)
            data = np.zeros((nebin, hpx.npix))
            data.flat[keys] = vals
        else:
            for c in colnames:
                if c.find(hpx.conv.colstring) == 0:
                    cnames.append(c)
            nebin = len(cnames)
            data = np.ndarray((nebin, hpx.npix))
            for i, cname in enumerate(cnames):
                data[i, 0:] = hdu.data.field(cname)

        return cls(data, hpx)

Response: Creates and returns an HpxMap object from a FITS HDU.


Instruction:
Can you generate the documentation for the following Python 3 function
def create_from_hdulist(cls, hdulist, **kwargs):
        extname = kwargs.get('hdu', hdulist[1].name)
        ebins = fits_utils.find_and_read_ebins(hdulist)
        return cls.create_from_hdu(hdulist[extname], ebins)

Response: Creates and returns an HpxMap object from a FITS HDUList


Instruction:
Can you tell what is the following Python 3 function doing
def make_wcs_from_hpx(self, sum_ebins=False, proj='CAR', oversample=2,
                          normalize=True):
        """Make a WCS object and convert HEALPix data into WCS projection

        NOTE: this re-calculates the mapping, if you have already
        calculated the mapping it is much faster to use
        convert_to_cached_wcs() instead

        Parameters
        ----------
        sum_ebins  : bool
           sum energy bins over energy bins before reprojecting

        proj       : str
           WCS-projection

        oversample : int
           Oversampling factor for WCS map

        normalize  : bool
           True -> perserve integral by splitting HEALPix values between bins

        returns (WCS object, np.ndarray() with reprojected data)

        """
        self._wcs_proj = proj
        self._wcs_oversample = oversample
        self._wcs_2d = self.hpx.make_wcs(2, proj=proj, oversample=oversample)
        self._hpx2wcs = HpxToWcsMapping(self.hpx, self._wcs_2d)
        wcs, wcs_data = self.convert_to_cached_wcs(self.counts, sum_ebins,
                                                   normalize)
        return wcs, wcs_data

Response: Make a WCS object and convert HEALPix data into WCS projection


Instruction:
How would you code a function in Python 3 to
convert HEALPix data into a WCS object.

Response:
def convert_to_cached_wcs(self, hpx_in, sum_ebins=False, normalize=True):
        """ Make a WCS object and convert HEALPix data into WCS projection

        Parameters
        ----------
        hpx_in     : `~numpy.ndarray`
           HEALPix input data
        sum_ebins  : bool
           sum energy bins over energy bins before reprojecting
        normalize  : bool
           True -> perserve integral by splitting HEALPix values between bins

        returns (WCS object, np.ndarray() with reprojected data)
        """
        if self._hpx2wcs is None:
            raise Exception('HpxMap.convert_to_cached_wcs() called '
                            'before make_wcs_from_hpx()')

        if len(hpx_in.shape) == 1:
            wcs_data = np.ndarray(self._hpx2wcs.npix)
            loop_ebins = False
            hpx_data = hpx_in
        elif len(hpx_in.shape) == 2:
            if sum_ebins:
                wcs_data = np.ndarray(self._hpx2wcs.npix)
                hpx_data = hpx_in.sum(0)
                loop_ebins = False
            else:
                wcs_data = np.ndarray((self.counts.shape[0],
                                       self._hpx2wcs.npix[0],
                                       self._hpx2wcs.npix[1]))
                hpx_data = hpx_in
                loop_ebins = True
        else:
            raise Exception('Wrong dimension for HpxMap %i' %
                            len(hpx_in.shape))

        if loop_ebins:
            for i in range(hpx_data.shape[0]):
                self._hpx2wcs.fill_wcs_map_from_hpx_data(
                    hpx_data[i], wcs_data[i], normalize)
                pass
            wcs_data.reshape((self.counts.shape[0], self._hpx2wcs.npix[
                             0], self._hpx2wcs.npix[1]))
            # replace the WCS with a 3D one
            wcs = self.hpx.make_wcs(3, proj=self._wcs_proj,
                                    energies=np.log10(self.hpx.ebins),
                                    oversample=self._wcs_oversample)
        else:
            self._hpx2wcs.fill_wcs_map_from_hpx_data(
                hpx_data, wcs_data, normalize)
            wcs_data.reshape(self._hpx2wcs.npix)
            wcs = self._wcs_2d

        return wcs, wcs_data


Instruction:
Write a Python 3 script for
getting a list of sky coordinates for the centers of every pixel.

Response:
def get_pixel_skydirs(self):
        """Get a list of sky coordinates for the centers of every pixel. """
        sky_coords = self._hpx.get_sky_coords()
        if self.hpx.coordsys == 'GAL':
            return SkyCoord(l=sky_coords.T[0], b=sky_coords.T[1], unit='deg', frame='galactic')
        else:
            return SkyCoord(ra=sky_coords.T[0], dec=sky_coords.T[1], unit='deg', frame='icrs')


Instruction:
Write a Python 3 function that can
reduce a counts cube to a counts map

Response:
def sum_over_energy(self):
        """ Reduce a counts cube to a counts map """
        # We sum over axis 0 in the array, and drop the energy binning in the
        # hpx object
        return HpxMap(np.sum(self.counts, axis=0), self.hpx.copy_and_drop_energy())


Instruction:
Implement a function in Python 3 to
return the values of the pixels in the flat array corresponding to a set of coordinates lons and lats.

Response:
def get_map_values(self, lons, lats, ibin=None):
        """Return the indices in the flat array corresponding to a set of coordinates

        Parameters
        ----------
        lons  : array-like
           'Longitudes' (RA or GLON)

        lats  : array-like
           'Latitidues' (DEC or GLAT)

        ibin : int or array-like
           Extract data only for a given energy bin.  None -> extract data for all bins

        Returns
        ----------
        vals : numpy.ndarray((n))
           Values of pixels in the flattened map, np.nan used to flag
           coords outside of map
        """
        theta = np.pi / 2. - np.radians(lats)
        phi = np.radians(lons)

        pix = hp.ang2pix(self.hpx.nside, theta, phi, nest=self.hpx.nest)

        if self.data.ndim == 2:
            return self.data[:, pix] if ibin is None else self.data[ibin, pix]
        else:
            return self.data[pix]


Instruction:
Can you generate the documentation for the following Python 3 function
def interpolate(self, lon, lat, egy=None, interp_log=True):

        if self.data.ndim == 1:
            theta = np.pi / 2. - np.radians(lat)
            phi = np.radians(lon)
            return hp.pixelfunc.get_interp_val(self.counts, theta,
                                               phi, nest=self.hpx.nest)
        else:
            return self._interpolate_cube(lon, lat, egy, interp_log)

Response: Interpolate the map values.


Instruction:
Can you write a function in Python 3 where it
performs interpolation on a healpix cube.

Response:
def _interpolate_cube(self, lon, lat, egy=None, interp_log=True):
        """Perform interpolation on a healpix cube.  If egy is None
        then interpolation will be performed on the existing energy
        planes.

        """

        shape = np.broadcast(lon, lat, egy).shape
        lon = lon * np.ones(shape)
        lat = lat * np.ones(shape)
        theta = np.pi / 2. - np.radians(lat)
        phi = np.radians(lon)
        vals = []
        for i, _ in enumerate(self.hpx.evals):
            v = hp.pixelfunc.get_interp_val(self.counts[i], theta,
                                            phi, nest=self.hpx.nest)
            vals += [np.expand_dims(np.array(v, ndmin=1), -1)]

        vals = np.concatenate(vals, axis=-1)

        if egy is None:
            return vals.T

        egy = egy * np.ones(shape)

        if interp_log:
            xvals = utils.val_to_pix(np.log(self.hpx.evals), np.log(egy))
        else:
            xvals = utils.val_to_pix(self.hpx.evals, egy)

        vals = vals.reshape((-1, vals.shape[-1]))
        xvals = np.ravel(xvals)
        v = map_coordinates(vals, [np.arange(vals.shape[0]), xvals],
                            order=1)
        return v.reshape(shape)


Instruction:
Given the following Python 3 function, write the documentation
def expanded_counts_map(self):
        if self.hpx._ipix is None:
            return self.counts

        output = np.zeros(
            (self.counts.shape[0], self.hpx._maxpix), self.counts.dtype)
        for i in range(self.counts.shape[0]):
            output[i][self.hpx._ipix] = self.counts[i]
        return output

Response: return the full counts map


Instruction:
Make a summary of the following Python 3 code
def explicit_counts_map(self, pixels=None):
        # No pixel index, so build one
        if self.hpx._ipix is None:
            if self.data.ndim == 2:
                summed = self.counts.sum(0)
                if pixels is None:
                    nz = summed.nonzero()[0]
                else:
                    nz = pixels
                data_out = np.vstack(self.data[i].flat[nz]
                                     for i in range(self.data.shape[0]))
            else:
                if pixels is None:
                    nz = self.data.nonzero()[0]
                else:
                    nz = pixels
                data_out = self.data[nz]
            return (nz, data_out)
        else:
            if pixels is None:
                return (self.hpx._ipix, self.data)
        # FIXME, can we catch this
        raise RuntimeError(
            'HPX.explicit_counts_map called with pixels for a map that already has pixels')

Response: return a counts map with explicit index scheme


Instruction:
Given the following Python 3 function, write the documentation
def sparse_counts_map(self):
        if self.hpx._ipix is None:
            flatarray = self.data.flattern()
        else:
            flatarray = self.expanded_counts_map()
        nz = flatarray.nonzero()[0]
        data_out = flatarray[nz]
        return (nz, data_out)

Response: return a counts map with sparse index scheme


Instruction:
Create a Python 3 function to
compute signal and background counts for a point source at the given skydir with spectral parameterization fn.

Response:
def compute_counts(self, skydir, fn, ebins=None):
        """Compute signal and background counts for a point source at
        position ``skydir`` with spectral parameterization ``fn``.

        Parameters
        ----------
        skydir : `~astropy.coordinates.SkyCoord`

        ebins : `~numpy.ndarray`

        Returns
        -------
        sig : `~numpy.ndarray`
            Signal counts array.  Dimensions are energy, angular
            separation, and event type.

        bkg : `~numpy.ndarray`
            Background counts array.  Dimensions are energy, angular
            separation, and event type.

        """

        if ebins is None:
            ebins = self.ebins
            ectr = self.ectr
        else:
            ectr = np.exp(utils.edge_to_center(np.log(ebins)))

        skydir_cel = skydir.transform_to('icrs')
        skydir_gal = skydir.transform_to('galactic')

        sig = []
        bkg = []
        bkg_fit = None
        if self._gdiff_fit is not None:
            bkg_fit = []

        for psf, exp in zip(self._psf, self._exp):

            coords0 = np.meshgrid(*[skydir_cel.ra.deg, ectr], indexing='ij')
            coords1 = np.meshgrid(*[skydir_cel.dec.deg, ectr], indexing='ij')

            # expv = exp.interpolate(skydir_cel.icrs.ra.deg,
            #                       skydir_cel.icrs.dec.deg,
            #                       ectr)

            expv = exp.interpolate(coords0[0], coords1[0], coords0[1])

            coords0 = np.meshgrid(*[skydir_gal.l.deg, ectr], indexing='ij')
            coords1 = np.meshgrid(*[skydir_gal.b.deg, ectr], indexing='ij')

            bkgv = self._gdiff.interpolate(np.ravel(coords0[0]),
                                           np.ravel(coords1[0]),
                                           np.ravel(coords0[1]))
            bkgv = bkgv.reshape(expv.shape)

            # bkgv = self._gdiff.interpolate(
            #    skydir_gal.l.deg, skydir_gal.b.deg, ectr)

            isov = np.exp(np.interp(np.log(ectr), np.log(self._iso[0]),
                                    np.log(self._iso[1])))
            bkgv += isov

            s0, b0 = irfs.compute_ps_counts(ebins, expv, psf, bkgv, fn,
                                            egy_dim=1,
                                            spatial_model=self.spatial_model,
                                            spatial_size=self.spatial_size)

            sig += [s0]
            bkg += [b0]

            if self._iso_fit is not None:
                isov_fit = np.exp(np.interp(np.log(ectr), np.log(self._iso_fit[0]),
                                            np.log(self._iso_fit[1])))
            else:
                isov_fit = isov

            if self._gdiff_fit is not None:
                bkgv_fit = self._gdiff_fit.interpolate(np.ravel(coords0[0]),
                                                       np.ravel(coords1[0]),
                                                       np.ravel(coords0[1]))
                bkgv_fit = bkgv_fit.reshape(expv.shape)
                bkgv_fit += isov_fit
                s0, b0 = irfs.compute_ps_counts(ebins, expv, psf,
                                                bkgv_fit, fn, egy_dim=1,
                                                spatial_model=self.spatial_model,
                                                spatial_size=self.spatial_size)
                bkg_fit += [b0]

        sig = np.concatenate([np.expand_dims(t, -1) for t in sig])
        bkg = np.concatenate([np.expand_dims(t, -1) for t in bkg])
        if self._gdiff_fit is not None:
            bkg_fit = np.concatenate([np.expand_dims(t, -1) for t in bkg_fit])

        return sig, bkg, bkg_fit


Instruction:
Given the following Python 3 function, write the documentation
def diff_flux_threshold(self, skydir, fn, ts_thresh, min_counts):

        sig, bkg, bkg_fit = self.compute_counts(skydir, fn)
        norms = irfs.compute_norm(sig, bkg, ts_thresh,
                                  min_counts, sum_axes=[2, 3],
                                  rebin_axes=[10, 1],
                                  bkg_fit=bkg_fit)

        npred = np.squeeze(np.apply_over_axes(np.sum, norms * sig, [2, 3]))
        norms = np.squeeze(norms)

        flux = norms * fn.flux(self.ebins[:-1], self.ebins[1:])
        eflux = norms * fn.eflux(self.ebins[:-1], self.ebins[1:])
        dnde = norms * fn.dnde(self.ectr)
        e2dnde = self.ectr**2 * dnde

        return dict(e_min=self.ebins[:-1], e_max=self.ebins[1:],
                    e_ref=self.ectr,
                    npred=npred, flux=flux, eflux=eflux,
                    dnde=dnde, e2dnde=e2dnde)

Response: Compute the differential flux threshold for a point source atthe given skydir with spectral parameterization fn.


Instruction:
Can you create a Python 3 function that
computes the integral flux threshold for a point source at the given skydir with spectral parameterization fn.

Response:
def int_flux_threshold(self, skydir, fn, ts_thresh, min_counts):
        """Compute the integral flux threshold for a point source at
        position ``skydir`` with spectral parameterization ``fn``.

        """

        ebins = 10**np.linspace(np.log10(self.ebins[0]),
                                np.log10(self.ebins[-1]), 33)
        ectr = np.sqrt(ebins[0] * ebins[-1])

        sig, bkg, bkg_fit = self.compute_counts(skydir, fn, ebins)

        norms = irfs.compute_norm(sig, bkg, ts_thresh,
                                  min_counts, sum_axes=[1, 2, 3], bkg_fit=bkg_fit,
                                  rebin_axes=[4, 10, 1])

        npred = np.squeeze(np.apply_over_axes(np.sum, norms * sig, [1, 2, 3]))
        npred = np.array(npred, ndmin=1)
        flux = np.squeeze(norms) * fn.flux(ebins[0], ebins[-1])
        eflux = np.squeeze(norms) * fn.eflux(ebins[0], ebins[-1])
        dnde = np.squeeze(norms) * fn.dnde(ectr)
        e2dnde = ectr**2 * dnde

        o = dict(e_min=self.ebins[0], e_max=self.ebins[-1], e_ref=ectr,
                 npred=npred, flux=flux, eflux=eflux,
                 dnde=dnde, e2dnde=e2dnde)

        sig, bkg, bkg_fit = self.compute_counts(skydir, fn)

        npred = np.squeeze(np.apply_over_axes(np.sum, norms * sig,
                                              [2, 3]))
        flux = np.squeeze(np.squeeze(norms, axis=(1, 2, 3))[:, None] *
                          fn.flux(self.ebins[:-1], self.ebins[1:]))
        eflux = np.squeeze(np.squeeze(norms, axis=(1, 2, 3))[:, None] *
                           fn.eflux(self.ebins[:-1], self.ebins[1:]))
        dnde = np.squeeze(np.squeeze(norms, axis=(1, 2, 3))
                          [:, None] * fn.dnde(self.ectr))
        e2dnde = ectr**2 * dnde

        o['bins'] = dict(npred=npred,
                         flux=flux,
                         eflux=eflux,
                         dnde=dnde,
                         e2dnde=e2dnde,
                         e_min=self.ebins[:-1], e_max=self.ebins[1:],
                         e_ref=self.ectr)

        return o


Instruction:
Can you tell what is the following Python 3 function doing
def coords_to_vec(lon, lat):
    phi = np.radians(lon)
    theta = (np.pi / 2) - np.radians(lat)
    sin_t = np.sin(theta)
    cos_t = np.cos(theta)

    xVals = sin_t * np.cos(phi)
    yVals = sin_t * np.sin(phi)
    zVals = cos_t

    # Stack them into the output array
    out = np.vstack((xVals, yVals, zVals)).swapaxes(0, 1)
    return out

Response: Converts longitute and latitude coordinates to a unit 3 - vector


Instruction:
How would you implement a function in Python 3 that
returns an estimate of the pixel size from the HEALPix nside coordinate

Response:
def get_pixel_size_from_nside(nside):
    """ Returns an estimate of the pixel size from the HEALPix nside coordinate

    This just uses a lookup table to provide a nice round number for each
    HEALPix order.
    """
    order = int(np.log2(nside))
    if order < 0 or order > 13:
        raise ValueError('HEALPix order must be between 0 to 13 %i' % order)

    return HPX_ORDER_TO_PIXSIZE[order]


Instruction:
Here you have a function in Python 3, explain what it does
def hpx_to_axes(h, npix):
    x = h.ebins
    z = np.arange(npix[-1] + 1)

    return x, z

Response:
Generates a sequence of bin edge vectors corresponding to the the
    axes of a HPX object.


Instruction:
Can you write a function in Python 3 where it
generates an N x D list of pixel center coordinates where N is the number of pixels and D is the dimensionality of the map.

Response:
def hpx_to_coords(h, shape):
    """ Generate an N x D list of pixel center coordinates where N is
    the number of pixels and D is the dimensionality of the map."""

    x, z = hpx_to_axes(h, shape)

    x = np.sqrt(x[0:-1] * x[1:])
    z = z[:-1] + 0.5

    x = np.ravel(np.ones(shape) * x[:, np.newaxis])
    z = np.ravel(np.ones(shape) * z[np.newaxis, :])

    return np.vstack((x, z))


Instruction:
How would you code a function in Python 3 to
make the mapping data needed to from HPX pixelization to WCS - based array.

Response:
def make_hpx_to_wcs_mapping_centers(hpx, wcs):
    """ Make the mapping data needed to from from HPX pixelization to a
    WCS-based array

    Parameters
    ----------
    hpx     : `~fermipy.hpx_utils.HPX`
       The healpix mapping (an HPX object)

    wcs     : `~astropy.wcs.WCS`
       The wcs mapping (a pywcs.wcs object)

    Returns
    -------
      ipixs    :  array(nx,ny) of HEALPix pixel indices for each wcs pixel
                  -1 indicates the wcs pixel does not contain the center of a HEALpix pixel
      mult_val :  array(nx,ny) of 1.
      npix     :  tuple(nx,ny) with the shape of the wcs grid

    """
    npix = (int(wcs.wcs.crpix[0] * 2), int(wcs.wcs.crpix[1] * 2))
    mult_val = np.ones(npix).T.flatten()
    sky_crds = hpx.get_sky_coords()
    pix_crds = wcs.wcs_world2pix(sky_crds, 0).astype(int)
    ipixs = -1 * np.ones(npix, int).T.flatten()
    pix_index = npix[1] * pix_crds[0:, 0] + pix_crds[0:, 1]
    if hpx._ipix is None:
        for ipix, pix_crd in enumerate(pix_index):
            ipixs[pix_crd] = ipix
    else:
        for pix_crd, ipix in zip(pix_index, hpx._ipix):
            ipixs[pix_crd] = ipix
    ipixs = ipixs.reshape(npix).T.flatten()
    return ipixs, mult_val, npix