Instruction:
Write a Python 3 function for
registering outputs and metadata.

Response:
def register(self, new_outputs, *args, **kwargs):
        """
        Register outputs and metadata.

        * ``initial_value`` - used in dynamic calculations
        * ``size`` - number of elements per timestep
        * ``uncertainty`` - in percent of nominal value
        * ``variance`` - dictionary of covariances, diagonal is square of
          uncertianties, no units
        * ``jacobian`` - dictionary of sensitivities dxi/dfj
        * ``isconstant`` - ``True`` if constant, ``False`` if periodic
        * ``isproperty`` - ``True`` if output stays at last value during
          thresholds, ``False`` if reverts to initial value
        * ``timeseries`` - name of corresponding time series output, ``None`` if
          no time series
        * ``output_source`` - name

        :param new_outputs: new outputs to register.
        """
        kwargs.update(zip(self.meta_names, args))
        # call super method
        super(OutputRegistry, self).register(new_outputs, **kwargs)


Instruction:
Write a Python 3 function for
classifying input frames into a list of images and images_info.

Response:
def initial_classification(self, obresult, target_is_sky=False):
        """Classify input frames, """
        # lists of targets and sky frames

        with obresult.frames[0].open() as baseimg:
            # Initial checks
            has_bpm_ext = 'BPM' in baseimg
            self.logger.debug('images have BPM extension: %s', has_bpm_ext)

        images_info = []
        for f in obresult.frames:
            with f.open() as img:
                # Getting some metadata from FITS header
                hdr = img[0].header

                iinfo = ImageInfo(f)

                finfo = {}
                iinfo.metadata = finfo

                finfo['uuid'] = hdr['UUID']
                finfo['exposure'] = hdr['EXPTIME']
                # frame.baseshape = get_image_shape(hdr)
                finfo['airmass'] = hdr['airmass']
                finfo['mjd'] = hdr['tstamp']

                iinfo.label = 'result_image_{}'.format(finfo['uuid'])
                iinfo.mask = nfcom.Extension("BPM")
                # Insert pixel offsets between frames
                iinfo.objmask_data = None
                iinfo.valid_target = False
                iinfo.valid_sky = False

                # FIXME: hardcode itype for the moment
                iinfo.itype = 'TARGET'
                if iinfo.itype == 'TARGET':
                    iinfo.valid_target = True
                    #targetframes.append(iinfo)
                    if target_is_sky:
                        iinfo.valid_sky = True
                        #skyframes.append(iinfo)
                if iinfo.itype == 'SKY':
                    iinfo.valid_sky = True
                    #skyframes.append(iinfo)
                images_info.append(iinfo)

        return images_info


Instruction:
Make a summary of the following Python 3 code
def read_csu_2(barmodel, hdr):
    conf = CSUConf(barmodel)

    conf.conf_f = hdr.get('CSUCONFF', 'UNKNOWN')
    # Read CSUPOS and set position in model
    # The bars
    for idx in conf.bars:
        key = "CSUP{}".format(idx)
        # UNIT of CSUPOS?
        # who knows

        # set CSUPOS for BAR
        # Using the model, this sets the X,Y coordinate
        conf.bars[idx].csupos = hdr[key]
        # print('read_csu {}, position is {}'.format(idx, conf.bars[idx].current_pos))

    # Slits. We dont have keywords to fuse slitlets into larger logical slits
    # so there is one slitslet for each pair of bars
    for idx in range(1, EMIR_NBARS + 1):
        l_ids = [idx]
        r_ids = [idx + EMIR_NBARS]

        lbars = {lid: conf.bars[lid] for lid in l_ids}
        rbars = {rid: conf.bars[rid] for rid in r_ids}

        this = LogicalSlit(idx, lbars=lbars, rbars=rbars)
        # References from header
        try:
            slit_t = hdr["SLIFL%d" % idx]
            this.target_type = TargetType(slit_t)
        except KeyError as err:
            # print('warning', err)
            this.target_type = TargetType.UNKNOWN

        xref = hdr.get("XRSLI%d" % this.idx, -100) - 1
        yref = hdr.get("YRSLI%d" % this.idx, -100) - 1
        this.target_coordinates = (xref, yref)

        xref = hdr.get("XVSLI%d" % this.idx, -100) - 1
        yref = hdr.get("YVSLI%d" % this.idx, -100) - 1
        this.target_coordinates_v = (xref, yref)
        conf.slits[idx] = this

    return conf

Response: Read CSU information and slits from header


Instruction:
How would you explain what the following Python 3 function does
def read_csu_3(barmodel, hdr):
    conf = CSUConf(barmodel)
    conf.set_state(hdr)
    return conf

Response: Read CSU information and slits from header


Instruction:
How would you explain what the following Python 3 function does
def set_state(self, hdr):

        self.conf_f = hdr.get('CSUCONFF', 'UNKNOWN')
        # Read CSUPOS and set position in model
        # The bars
        for idx in self.bars:
            key = "CSUP{}".format(idx)
            # UNIT is mm

            # set CSUPOS for BAR
            # Using the model, this sets the X,Y coordinate
            self.bars[idx].csupos = hdr[key]

        self._update_slits(hdr)

Response: Read CSU information and slits from header and set position in model


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

        # Slits.
        # For the moment we will fuse only reference slits

        # clean existing slits
        self.slits = {}
        mm = []
        for idx in self.LBARS:

            # References from header
            try:
                slit_t = hdr["SLIFL%d" % idx]
                target_type = TargetType(slit_t)
            except KeyError:
                target_type = TargetType.UNKNOWN

            xref = hdr.get("XRSLI%d" % idx, -100) - 1
            yref = hdr.get("YRSLI%d" % idx, -100) - 1
            target_coordinates = (xref, yref)

            xref = hdr.get("XVSLI%d" % idx, -100) - 1
            yref = hdr.get("YVSLI%d" % idx, -100) - 1
            target_coordinates_v = (xref, yref)

            mm.append((idx, target_type, target_coordinates, target_coordinates_v))

        bag = merge_slits(mm, max_slits=3, tol=1e-2)

        for idx, l_ids in bag.items():
            r_ids = [lid + EMIR_NBARS for lid in l_ids]

            lbars = {lid: self.bars[lid] for lid in l_ids}
            rbars = {rid: self.bars[rid] for rid in r_ids}

            this = LogicalSlit(idx, lbars=lbars, rbars=rbars)
            # References from header
            ref = mm[l_ids[0] - 1]
            this.target_type = ref[1]
            this.target_coordinates = ref[2]
            this.target_coordinates_v = ref[3]
            self.slits[idx] = this

Response: Recreate the list of all the slits from header.


Instruction:
Here you have a function in Python 3, explain what it does
def update(self, duration):
        if duration >= 0:
            self.histogram.update(duration)
            self.meter.mark()

Response: Add a recorded duration.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _build_g5k_conf(vmong5k_conf):
    clusters = [m.cluster for m in vmong5k_conf.machines]
    sites = g5k_api_utils.get_clusters_sites(clusters)
    site_names = set(sites.values())
    if len(site_names) > 1:
        raise Exception("Multisite deployment not supported yet")
    site = site_names.pop()
    return _do_build_g5k_conf(vmong5k_conf, site)

Response: Build the g5k conf from the vmong5k conf.


Instruction:
How would you explain what the following Python 3 function does
def copy_model_instance(obj):
    meta = getattr(obj, '_meta')  # make pycharm happy
    # dictionary of model values excluding auto created and related fields
    return {f.name: getattr(obj, f.name)
            for f in meta.get_fields(include_parents=False)
            if not f.auto_created}

Response: Copy Django model instance as a dictionary excluding auto - created and many - to - one reverse relation.


Instruction:
Make a summary of the following Python 3 code
def load_data(self, *args, **kwargs):
        # get positional argument names from parameters and apply them to args
        # update data with additional kwargs
        argpos = {
            v['extras']['argpos']: k for k, v in self.parameters.iteritems()
            if 'argpos' in v['extras']
        }
        data = dict(
            {argpos[n]: a for n, a in enumerate(args)}, **kwargs
        )
        return self.apply_units_to_cache(data)

Response: Loads the data from the parameters and applies units to the cache.


Instruction:
Create a Python 3 function for
applying units to data when a proxy reader is used.

Response:
def apply_units_to_cache(self, data):
        """
        Applies units to data when a proxy reader is used. For example if the
        data is cached as JSON and retrieved using the
        :class:`~simkit.core.data_readers.JSONReader`, then units can be
        applied from the original parameter schema.

        :param data: Data read by proxy reader.
        :return: data with units applied
        """
        # if units key exists then apply
        for k, v in self.parameters.iteritems():
            if v and v.get('units'):
                data[k] = Q_(data[k], v.get('units'))
        return data


Instruction:
Create a Python 3 function for
applying units to model.

Response:
def load_data(self, model_instance, *args, **kwargs):
        """
        Apply units to model.
        :return: data
        """
        model_dict = copy_model_instance(model_instance)
        return super(DjangoModelReader, self).load_data(**model_dict)


Instruction:
Can you generate the documentation for the following Python 3 function
def cached(f):
    @functools.wraps(f)
    def wrapped(*args, **kwargs):
        with _cache_lock:
            identifier = (f.__name__,  args, kwargs)
            key = pickle.dumps(identifier)
            value = cache.get(key)
            if value is not None:
                logger.debug("HIT for %s -> %s" % (str(identifier), value))
            else:
                logger.debug("MISS for %s" % str(identifier))
                value = f(*args, **kwargs)
                cache[key] = value
            return value
    return wrapped

Response: Decorator for caching API calls request.


Instruction:
Can you implement a function in Python 3 that
gets the reference to the API cient singleton.

Response:
def get_api_client():
    """Gets the reference to the API cient (singleton)."""
    with _api_lock:
        global _api_client
        if not _api_client:
            conf_file = os.path.join(os.environ.get("HOME"),
                                     ".python-grid5000.yaml")
            _api_client = Client.from_yaml(conf_file)

        return _api_client


Instruction:
Can you create a Python 3 function that
reloads all running or pending jobs with a given name.

Response:
def grid_reload_from_name(job_name):
    """Reload all running or pending jobs of Grid'5000 with a given name.

    By default all the sites will be searched for jobs with the name
    ``job_name``. Using EnOSlib there can be only one job per site with name
    ``job_name``.

    Note that it honors the ``exluded_sites`` attribute of the client so the
    scan can be reduced.

    Args:
        job_name (str): the job name

    Returns:
        The list of the python-grid5000 jobs retrieved.

    Raises:
        EnosG5kDuplicateJobsError: if there's several jobs with the same name
            on a site.
    """
    gk = get_api_client()
    sites = get_all_sites_obj()
    jobs = []
    for site in [s for s in sites if s.uid not in gk.excluded_site]:
        logger.info("Reloading %s from %s" % (job_name, site.uid))
        _jobs = site.jobs.list(name=job_name,
                               state="waiting,launching,running")
        if len(_jobs) == 1:
            logger.info("Reloading %s from %s" % (_jobs[0].uid, site.uid))
            jobs.append(_jobs[0])
        elif len(_jobs) > 1:
            raise EnosG5kDuplicateJobsError(site, job_name)
    return jobs


Instruction:
How would you implement a function in Python 3 that
reloads all running or pending jobs of Grid 5000 from their ids jobs

Response:
def grid_reload_from_ids(oargrid_jobids):
    """Reload all running or pending jobs of Grid'5000 from their ids

    Args:
        oargrid_jobids (list): list of ``(site, oar_jobid)`` identifying the
            jobs on each site

    Returns:
        The list of python-grid5000 jobs retrieved
    """
    gk = get_api_client()
    jobs = []
    for site, job_id in oargrid_jobids:
        jobs.append(gk.sites[site].jobs[job_id])
    return jobs


Instruction:
Can you implement a function in Python 3 that
destroys all the jobs with a given name.

Response:
def grid_destroy_from_name(job_name):
    """Destroy all the jobs with a given name.

    Args:
       job_name (str): the job name
    """
    jobs = grid_reload_from_name(job_name)
    for job in jobs:
        job.delete()
        logger.info("Killing the job (%s, %s)" % (job.site, job.uid))


Instruction:
Can you create a Python 3 function that
destroys all the jobs with corresponding ids

Response:
def grid_destroy_from_ids(oargrid_jobids):
    """Destroy all the jobs with corresponding ids

    Args:
        oargrid_jobids (list): the ``(site, oar_job_id)`` list of tuple
            identifying the jobs for each site. """
    jobs = grid_reload_from_ids(oargrid_jobids)
    for job in jobs:
        job.delete()
        logger.info("Killing the jobs %s" % oargrid_jobids)


Instruction:
How would you explain what the following Python 3 function does
def submit_jobs(job_specs):
    gk = get_api_client()
    jobs = []
    try:
        for site, job_spec in job_specs:
            logger.info("Submitting %s on %s" % (job_spec, site))
            jobs.append(gk.sites[site].jobs.create(job_spec))
    except Exception as e:
        logger.error("An error occured during the job submissions")
        logger.error("Cleaning the jobs created")
        for job in jobs:
            job.delete()
        raise(e)

    return jobs

Response: Submit a list of jobs to the Grid5000 API.


Instruction:
Can you create a Python 3 function that
waits for all the jobs to be runnning.

Response:
def wait_for_jobs(jobs):
    """Waits for all the jobs to be runnning.

    Args:
        jobs(list): list of the python-grid5000 jobs to wait for

    Raises:
        Exception: if one of the job gets in error state.
    """

    all_running = False
    while not all_running:
        all_running = True
        time.sleep(5)
        for job in jobs:
            job.refresh()
            scheduled = getattr(job, "scheduled_at", None)
            if scheduled is not None:
                logger.info("Waiting for %s on %s [%s]" % (job.uid,
                                                           job.site,
                                                           _date2h(scheduled)))
            all_running = all_running and job.state == "running"
            if job.state == "error":
                raise Exception("The job %s is in error state" % job)
    logger.info("All jobs are Running !")


Instruction:
Here you have a function in Python 3, explain what it does
def grid_deploy(site, nodes, options):
    gk = get_api_client()
    environment = options.pop("env_name")
    options.update(environment=environment)
    options.update(nodes=nodes)
    key_path = DEFAULT_SSH_KEYFILE
    options.update(key=key_path.read_text())
    logger.info("Deploying %s with options %s" % (nodes, options))
    deployment = gk.sites[site].deployments.create(options)
    while deployment.status not in ["terminated", "error"]:
        deployment.refresh()
        print("Waiting for the end of deployment [%s]" % deployment.uid)
        time.sleep(10)

    deploy = []
    undeploy = []
    if deployment.status == "terminated":
        deploy = [node for node, v in deployment.result.items()
                  if v["state"] == "OK"]
        undeploy = [node for node, v in deployment.result.items()
                    if v["state"] == "KO"]
    elif deployment.status == "error":
        undeploy = nodes
    return deploy, undeploy

Response: Deploy and wait for the deployment to complete.


Instruction:
Can you implement a function in Python 3 that
sets the interface of the nodes in a specific vlan.

Response:
def set_nodes_vlan(site, nodes, interface, vlan_id):
    """Set the interface of the nodes in a specific vlan.

    It is assumed that the same interface name is available on the node.

    Args:
        site(str): site to consider
        nodes(list): nodes to consider
        interface(str): the network interface to put in the vlan
        vlan_id(str): the id of the vlan
    """
    def _to_network_address(host):
        """Translate a host to a network address
        e.g:
        paranoia-20.rennes.grid5000.fr -> paranoia-20-eth2.rennes.grid5000.fr
        """
        splitted = host.split('.')
        splitted[0] = splitted[0] + "-" + interface
        return ".".join(splitted)

    gk = get_api_client()
    network_addresses = [_to_network_address(n) for n in nodes]
    gk.sites[site].vlans[str(vlan_id)].submit({"nodes": network_addresses})


Instruction:
Can you write a function in Python 3 where it
gets all the corresponding sites of the passed clusters.

Response:
def clusters_sites_obj(clusters):
    """Get all the corresponding sites of the passed clusters.

    Args:
        clusters(list): list of string uid of sites (e.g 'rennes')

    Return:
        dict corresponding to the mapping cluster uid to python-grid5000 site
    """
    result = {}
    all_clusters = get_all_clusters_sites()
    clusters_sites = {c: s for (c, s) in all_clusters.items()
                        if c in clusters}
    for cluster, site in clusters_sites.items():

        # here we want the site python-grid5000 site object
        result.update({cluster: get_site_obj(site)})
    return result


Instruction:
How would you code a function in Python 3 to
get all the cluster of all the sites

Response:
def get_all_clusters_sites():
    """Get all the cluster of all the sites.

    Returns:
        dict corresponding to the mapping cluster uid to python-grid5000 site
    """
    result = {}
    gk = get_api_client()
    sites = gk.sites.list()
    for site in sites:
        clusters = site.clusters.list()
        result.update({c.uid: site.uid for c in clusters})
    return result


Instruction:
Write a Python 3 script to
get all the nodes of a given cluster

Response:
def get_nodes(cluster):
    """Get all the nodes of a given cluster.

    Args:
        cluster(string): uid of the cluster (e.g 'rennes')
    """
    gk = get_api_client()
    site = get_cluster_site(cluster)
    return gk.sites[site].clusters[cluster].nodes.list()


Instruction:
Implement a Python 3 function for
getting the network interfaces that are part of a cluster.

Response:
def get_cluster_interfaces(cluster, extra_cond=lambda nic: True):
    """Get the network interfaces names corresponding to a criteria.

    Note that the cluster is passed (not the individual node names), thus it is
    assumed that all nodes in a cluster have the same interface names same
    configuration. In addition to ``extra_cond``, only the mountable and
    Ehernet interfaces are returned.

    Args:
        cluster(str): the cluster to consider
        extra_cond(lambda): boolean lambda that takes the nic(dict) as
            parameter
    """
    nics = get_nics(cluster)
    # NOTE(msimonin): Since 05/18 nics on g5k nodes have predictable names but
    # the api description keep the legacy name (device key) and the new
    # predictable name (key name).  The legacy names is still used for api
    # request to the vlan endpoint This should be fixed in
    # https://intranet.grid5000.fr/bugzilla/show_bug.cgi?id=9272
    # When its fixed we should be able to only use the new predictable name.
    nics = [(nic['device'], nic['name']) for nic in nics
            if nic['mountable']
            and nic['interface'] == 'Ethernet'
            and not nic['management']
            and extra_cond(nic)]
    nics = sorted(nics)
    return nics


Instruction:
Write a Python 3 function for
returning for each cluster the available cluster interfaces

Response:
def get_clusters_interfaces(clusters, extra_cond=lambda nic: True):
    """ Returns for each cluster the available cluster interfaces

    Args:
        clusters (str): list of the clusters
        extra_cond (lambda): extra predicate to filter network card retrieved
    from the API. E.g lambda nic: not nic['mounted'] will retrieve all the
    usable network cards that are not mounted by default.

    Returns:
        dict of cluster with their associated nic names

    Examples:
        .. code-block:: python

            # pseudo code
            actual = get_clusters_interfaces(["paravance"])
            expected = {"paravance": ["eth0", "eth1"]}
            assertDictEquals(expected, actual)
    """

    interfaces = {}
    for cluster in clusters:
        nics = get_cluster_interfaces(cluster, extra_cond=extra_cond)
        interfaces.setdefault(cluster, nics)

    return interfaces


Instruction:
How would you code a function in Python 3 to
check if a given set of nodes can be started on a given cluster.

Response:
def can_start_on_cluster(nodes_status,
                         nodes,
                         start,
                         walltime):
    """Check if #nodes can be started on a given cluster.

    This is intended to give a good enough approximation.
    This can be use to prefiltered possible reservation dates before submitting
    them on oar.
    """
    candidates = []
    for node, status in nodes_status.items():
        reservations = status.get("reservations", [])
        # we search for the overlapping reservations
        overlapping_reservations = []
        for reservation in reservations:
            queue = reservation.get("queue")
            if queue == "besteffort":
                # ignoring any besteffort reservation
                continue
            r_start = reservation.get("started_at",
                                      reservation.get("scheduled_at"))
            if r_start is None:
                break
            r_start = int(r_start)
            r_end = r_start + int(reservation["walltime"])
            # compute segment intersection
            _intersect = min(r_end, start + walltime) - max(r_start, start)
            if _intersect > 0:
                overlapping_reservations.append(reservation)
        if len(overlapping_reservations) == 0:
            # this node can be accounted for a potential reservation
            candidates.append(node)
    if len(candidates) >= nodes:
        return True
    return False


Instruction:
Make a summary of the following Python 3 code
def chunked(iterable, n):
    return iter(functools.partial(take, n, iter(iterable)), [])

Response: Break an iterable into n lists of length n.


Instruction:
Here you have a function in Python 3, explain what it does
def add_data(self, rawdata):

        for data in rawdata:
            try:
                item = data[0]
                if item[0] == 2:
                    # Flush messages but we got nothing to flush
                    continue
                if item[0] != 0:
                    warnings.warn(f"Unknown message type '{item[0]}'", Warning)
                    continue
                item = item[1]
                # convert target to string because error codes are ints
                target = str(item[0])
                try:
                    data = item[1]
                except IndexError:
                    data = dict()
                try:
                    method = getattr(self, self.__head + target)
                    method(data)
                except AttributeError:
                    self._handle_unhandled(target, data)
            except IndexError:
                LOGGER.warning("Wrongly constructed message received: %r", data)

        self.conn.process_queues()

Response: Add data to given room s state


Instruction:
Write a Python 3 function for
registering a callback that we will have to wait for

Response:
def register_callback(self):
        """Register callback that we will have to wait for"""

        cid = str(self.__cid)
        self.__cid += 1
        event = queue.Queue()
        self.__callbacks[cid] = event
        return cid, event


Instruction:
Create a Python 3 function to
handle Lain being helpful

Response:
def _handle_401(self, data):
        """Handle Lain being helpful"""

        ex = ConnectionError(
            "Can't login to a protected room without a proper password", data
        )
        self.conn.reraise(ex)


Instruction:
Implement a Python 3 function for
handling Lain being helpful

Response:
def _handle_429(self, data):
        """Handle Lain being helpful"""

        ex = IOError("Too fast", data)
        self.conn.reraise(ex)


Instruction:
Here you have a function in Python 3, explain what it does
def _handle_callback(self, data):

        cb_id = data.get("id")
        args = data.get("args")
        event = self.__callbacks.pop(cb_id, None)
        if not event:
            return
        if not args:
            event.put(args)
            return
        err, info = args
        if err is None:
            event.put(info)
        else:
            LOGGER.warning("Callback returned error of %s", str(err))
            event.put(err)

Response: Handle a callback. Only used with getFileinfo so far.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _handle_userCount(self, data):

        self.room.user_count = data
        self.conn.enqueue_data("user_count", self.room.user_count)

Response: Handle user count changes


Instruction:
Explain what the following Python 3 code does
def _handle_userInfo(self, data):

        for k, v in data.items():
            if k == "nick":
                if v == "None":
                    v = "Volaphile"
                setattr(self.room.user, k, v)
                self.conn.enqueue_data(k, self.room.user.nick)
            elif k != "profile":
                if not hasattr(self.room, k):
                    warnings.warn(f"Skipping unset property {k}", ResourceWarning)
                    continue
                setattr(self.room, k, v)
                self.conn.enqueue_data(k, getattr(self.room, k))
            self.room.user_info = k, v
        self.conn.enqueue_data("user_info", self.room.user_info)

Response: Handle user info from the server


Instruction:
Create a Python 3 function for
handling initial config push and config changes

Response:
def _handle_config(self, data):
        """Handle initial config push and config changes"""

        self.room.config.update(data)
        self.conn.enqueue_data("config", data)


Instruction:
Write a Python 3 function for
handling new files being uploaded

Response:
def _handle_files(self, data):
        """Handle new files being uploaded"""

        initial = data.get("set", False)
        files = data["files"]
        for f in files:
            try:
                fobj = File(
                    self.room,
                    self.conn,
                    f[0],
                    f[1],
                    type=f[2],
                    size=f[3],
                    expire_time=int(f[4]) / 1000,
                    uploader=f[6].get("nick") or f[6].get("user"),
                )
                self.room.filedict = fobj.fid, fobj
                if not initial:
                    self.conn.enqueue_data("file", fobj)
            except Exception:
                import pprint

                LOGGER.exception("bad file")
                pprint.pprint(f)
        if initial:
            self.conn.enqueue_data("initial_files", self.room.files)


Instruction:
Can you implement a function in Python 3 that
handles files being removed

Response:
def _handle_delete_file(self, data):
        """Handle files being removed"""

        file = self.room.filedict.get(data)
        if file:
            self.room.filedict = data, None
            self.conn.enqueue_data("delete_file", file)


Instruction:
Here you have a function in Python 3, explain what it does
def _handle_chat_name(self, data):

        self.room.user.nick = data
        self.conn.enqueue_data("user", self.room.user)

Response: Handle user name changes


Instruction:
Can you implement a function in Python 3 that
gets the corrector for intensity flat

Response:
def get_corrector_f(rinput, meta, ins, datamodel):
    """Corrector for intensity flat"""
    from emirdrp.processing.flatfield import FlatFieldCorrector
    flat_info = meta['master_flat']
    with rinput.master_flat.open() as hdul:
        _logger.info('loading intensity flat')
        _logger.debug('flat info: %s', flat_info)
        mflat = hdul[0].data
        # Check NaN and Ceros
        mask1 = mflat < 0
        mask2 = ~numpy.isfinite(mflat)
        if numpy.any(mask1):
            _logger.warning('flat has %d values below 0', mask1.sum())
        if numpy.any(mask2):
            _logger.warning('flat has %d NaN', mask2.sum())
        flat_corrector = FlatFieldCorrector(mflat,
                                            datamodel=datamodel,
                                            calibid=datamodel.get_imgid(hdul))

    return flat_corrector


Instruction:
Write a Python 3 function for
logging the contents of Recipe Input

Response:
def loginfo(method):
    """Log the contents of Recipe Input"""

    def loginfo_method(self, rinput):

        klass = rinput.__class__

        for key in klass.stored():
            val = getattr(rinput, key)
            if isinstance(val, DataFrame):
                self.logger.debug("DataFrame %s", info.gather_info_dframe(val))
            elif isinstance(val, ObservationResult):
                for f in val.images:
                    self.logger.debug("OB DataFrame %s" , info.gather_info_dframe(f))
            else:
                pass

        result = method(self, rinput)

        return result

    return loginfo_method


Instruction:
How would you code a function in Python 3 to
copy or coerce to a Host.

Response:
def to_host(self):
        """Copy or coerce to a Host."""
        return Host(self.address,
                    alias=self.alias,
                    user=self.user,
                    keyfile=self.keyfile,
                    port=self.port,
                    extra=self.extra)


Instruction:
Given the following Python 3 function, write the documentation
def get_table_info(*table_names):
    ret = {}
    if table_names:
        for table_name in table_names:
            for name, inter in get_interfaces().items():
                if inter.has_table(table_name):
                    yield table_name, inter, inter.get_fields(table_name)

    else:
        for name, inter in get_interfaces().items():
            table_names = inter.get_tables()
            for table_name in table_names:
                yield table_name, inter, inter.get_fields(table_name)

Response: Returns a dict with table_name keys mapped to the Interface that table exists in


Instruction:
Can you write a function in Python 3 where it
converts virtual pixel to real pixel

Response:
def exvp(pos_x, pos_y):
    """Convert virtual pixel to real pixel"""
    pos_x = numpy.asarray(pos_x)
    pos_y = numpy.asarray(pos_y)
    # convert virtual pixel to real pixel
    # convert world coordinate to pixel
    center = [1024.5, 1024.5]
    cf  = EMIR_PLATESCALE_RADS
    pos_base_x = pos_x - center[0]
    pos_base_y = pos_y - center[1]
    ra = numpy.hypot(pos_base_x, pos_base_y)
    thet = numpy.arctan2(pos_base_y, pos_base_x)

    r  = cf * ra

    rr1 = 1 + 14606.7 * r**2 + 1739716115.1 * r**4

    nx1 = rr1 * ra * numpy.cos(thet) + center[0]
    ny1 = rr1 * ra * numpy.sin(thet) + center[1]
    return nx1, ny1


Instruction:
Here you have a function in Python 3, explain what it does
def match_sp_sep(first, second):
    if isinstance(first, list):
        one = [set(v.split(" ")) for v in first]
    else:
        one = [{v} for v in first.split(" ")]

    if isinstance(second, list):
        other = [set(v.split(" ")) for v in second]
    else:
        other = [{v} for v in second.split(" ")]

    # all values in one must appear in other
    if any(rt not in other for rt in one):
        return False
    return True

Response: Verify that all the values in first appear in second.


Instruction:
Write a Python 3 function for
verifying that the sector identifier_uri is reachable and that it contains redirect_uri s.

Response:
def _verify_sector_identifier(self, request):
        """
        Verify `sector_identifier_uri` is reachable and that it contains
        `redirect_uri`s.

        :param request: Provider registration request
        :return: si_redirects, sector_id
        :raises: InvalidSectorIdentifier
        """
        si_url = request["sector_identifier_uri"]
        try:
            res = self.endpoint_context.httpc.get(si_url)
        except Exception as err:
            logger.error(err)
            res = None

        if not res:
            raise InvalidSectorIdentifier("Couldn't read from sector_identifier_uri")

        logger.debug("sector_identifier_uri => %s", sanitize(res.text))

        try:
            si_redirects = json.loads(res.text)
        except ValueError:
            raise InvalidSectorIdentifier(
                "Error deserializing sector_identifier_uri content")

        if "redirect_uris" in request:
            logger.debug("redirect_uris: %s", request["redirect_uris"])
            for uri in request["redirect_uris"]:
                if uri not in si_redirects:
                    raise InvalidSectorIdentifier(
                        "redirect_uri missing from sector_identifiers")

        return si_redirects, si_url


Instruction:
Write a Python 3 script to
read and analyse the next line of the SExtractor file and return a dictionary.

Response:
def readline(self):
        """
        Read and analyse the next line of the SExtractor catalog
        and return a dictionary {'param1': value, 'param2': value, ...}.
        """
        if not(self._firstline):
            self._line = self._file.readline()

        self._firstline = False

        if not(self._line):
            return None
        __ll = (self._line).replace('\n', '')
        __values = __ll.split()
        self._outdict.update(dict(zip(self._keys, __values)))
        for i in self._keys:
            self._outdict[i] = (
                SExtractorfile._SE_keys[i]["infunc"](self._outdict[i]))

        return self._outdict.copy()


Instruction:
Can you tell what is the following Python 3 function doing
def read(self):
        __result = []

        __ll = self.readline()
        while __ll:
            __result.append(__ll)
            __ll = self.readline()

        return list(__result)

Response: Read the file until EOF and return a list of dictionaries.


Instruction:
Given the following Python 3 function, write the documentation
def close(self):
        if self._file:
            if not(self._file.closed):
                self._file.close()
        self.closed = True

Response: Close the SExtractor file.


Instruction:
Can you create a Python 3 function that
builds an instance of the driver to interact with G5K

Response:
def get_driver(configuration):
    """Build an instance of the driver to interact with G5K
    """
    resources = configuration["resources"]
    machines = resources["machines"]
    networks = resources["networks"]
    oargrid_jobids = configuration.get("oargrid_jobids")

    if oargrid_jobids:
        logger.debug("Loading the OargridStaticDriver")
        return OargridStaticDriver(oargrid_jobids)
    else:
        job_name = configuration.get("job_name", DEFAULT_JOB_NAME)
        walltime = configuration.get("walltime", DEFAULT_WALLTIME)
        job_type = configuration.get("job_type", JOB_TYPE_DEPLOY)
        reservation_date = configuration.get("reservation", False)
        # NOTE(msimonin): some time ago asimonet proposes to auto-detect
        # the queues and it was quiet convenient
        # see https://github.com/BeyondTheClouds/enos/pull/62
        queue = configuration.get("queue", None)
        logger.debug("Loading the OargridDynamicDriver")

        return OargridDynamicDriver(
            job_name,
            walltime,
            job_type,
            reservation_date,
            queue,
            machines,
            networks
        )


Instruction:
Explain what the following Python 3 code does
def ver_dec_content(parts, sign_key=None, enc_key=None, sign_alg='SHA256'):

    if parts is None:
        return None
    elif len(parts) == 3:
        # verify the cookie signature
        timestamp, load, b64_mac = parts
        mac = base64.b64decode(b64_mac)
        verifier = HMACSigner(algorithm=sign_alg)
        if verifier.verify(load.encode('utf-8') + timestamp.encode('utf-8'),
                           mac, sign_key.key):
            return load, timestamp
        else:
            raise VerificationError()
    elif len(parts) == 4:
        b_timestamp = parts[0]
        iv = base64.b64decode(parts[1])
        ciphertext = base64.b64decode(parts[2])
        tag = base64.b64decode(parts[3])

        decrypter = AES_GCMEncrypter(key=enc_key.key)
        msg = decrypter.decrypt(ciphertext, iv, tag=tag)
        p = lv_unpack(msg.decode('utf-8'))
        load = p[0]
        timestamp = p[1]
        if len(p) == 3:
            verifier = HMACSigner(algorithm=sign_alg)
            if verifier.verify(load.encode('utf-8') + timestamp.encode('utf-8'),
                               base64.b64decode(p[2]), sign_key.key):
                return load, timestamp
        else:
            return load, timestamp
    return None

Response: Verifies the value of a cookie and returns the load and timestamp.


Instruction:
Implement a function in Python 3 to
create and return a SimpleCookie instance for the cookie.

Response:
def make_cookie_content(name, load, sign_key, domain=None, path=None,
                        timestamp="", enc_key=None, max_age=0,
                        sign_alg='SHA256'):
    """
    Create and return a cookies content

    If you only provide a `seed`, a HMAC gets added to the cookies value
    and this is checked, when the cookie is parsed again.

    If you provide both `seed` and `enc_key`, the cookie gets protected
    by using AEAD encryption. This provides both a MAC over the whole cookie
    and encrypts the `load` in a single step.

    The `seed` and `enc_key` parameters should be byte strings of at least
    16 bytes length each. Those are used as cryptographic keys.

    :param name: Cookie name
    :type name: text
    :param load: Cookie load
    :type load: text
    :param sign_key: A sign_key key for payload signing
    :type sign_key: A :py:class:`cryptojwt.jwk.hmac.SYMKey` instance
    :param domain: The domain of the cookie
    :param path: The path specification for the cookie
    :param timestamp: A time stamp
    :type timestamp: text
    :param enc_key: The key to use for payload encryption.
    :type enc_key: A :py:class:`cryptojwt.jwk.hmac.SYMKey` instance
    :param max_age: The time in seconds for when a cookie will be deleted
    :type max_age: int
    :return: A SimpleCookie instance
    """
    if not timestamp:
        timestamp = str(int(time.time()))

    _cookie_value = sign_enc_payload(load, timestamp, sign_key=sign_key,
                                     enc_key=enc_key, sign_alg=sign_alg)

    content = {name: {"value": _cookie_value}}
    if path is not None:
        content[name]["path"] = path
    if domain is not None:
        content[name]["domain"] = domain

    content[name]['httponly'] = True

    if max_age:
        content[name]["expires"] = in_a_while(seconds=max_age)

    return content


Instruction:
Can you create a Python 3 function that
returns a list of parts of the cookie payload

Response:
def cookie_parts(name, kaka):
    """
    Give me the parts of the cookie payload

    :param name: A name of a cookie object
    :param kaka: The cookie
    :return: A list of parts or None if there is no cookie object with the
        given name
    """
    cookie_obj = SimpleCookie(as_unicode(kaka))
    morsel = cookie_obj.get(name)
    if morsel:
        return morsel.value.split("|")
    else:
        return None


Instruction:
Can you write a function in Python 3 where it
parses and verifies a cookie value.

Response:
def parse_cookie(name, sign_key, kaka, enc_key=None, sign_alg='SHA256'):
    """Parses and verifies a cookie value

    Parses a cookie created by `make_cookie` and verifies
    it has not been tampered with.

    You need to provide the same `sign_key` and `enc_key`
    used when creating the cookie, otherwise the verification
    fails. See `make_cookie` for details about the verification.

    :param sign_key: A signing key used to create the signature
    :type sign_key: A :py:class:`cryptojwt.jwk.hmac.SYMKey` instance
    :param kaka: The cookie
    :param enc_key: The encryption key used.
    :type enc_key: A :py:class:`cryptojwt.jwk.hmac.SYMKey` instance or None
    :raises InvalidCookieSign: When verification fails.
    :return: A tuple consisting of (payload, timestamp) or None if parsing fails
    """
    if not kaka:
        return None

    parts = cookie_parts(name, kaka)

    return ver_dec_content(parts, sign_key, enc_key, sign_alg)


Instruction:
How would you explain what the following Python 3 function does
def delete_cookie(self, cookie_name=None):
        if cookie_name is None:
            cookie_name = self.default_value['name']

        return self.create_cookie("", "", cookie_name=cookie_name, kill=True)

Response: Create a cookie that will immediately expire when it hits the otherCOOKIE.


Instruction:
Make a summary of the following Python 3 code
def get_cookie_value(self, cookie=None, cookie_name=None):
        if cookie_name is None:
            cookie_name = self.default_value['name']

        if cookie is None or cookie_name is None:
            return None
        else:
            try:
                info, timestamp = parse_cookie(cookie_name, self.sign_key, cookie,
                                               self.enc_key, self.sign_alg)
            except (TypeError, AssertionError):
                return None
            else:
                value, _ts, typ = info.split("::")
                if timestamp == _ts:
                    return value, _ts, typ
        return None

Response: Get the value of a cookie.


Instruction:
Can you write a function in Python 3 where it
adds a cookie to a SimpleCookie instance.

Response:
def append_cookie(self, cookie, name, payload, typ, domain=None, path=None,
                      timestamp="", max_age=0):
        """
        Adds a cookie to a SimpleCookie instance

        :param cookie:
        :param name:
        :param payload:
        :param typ:
        :param domain:
        :param path:
        :param timestamp:
        :param max_age:
        :return:
        """
        timestamp = str(int(time.time()))

        # create cookie payload
        try:
            _payload = "::".join([payload, timestamp, typ])
        except TypeError:
            _payload = "::".join([payload[0], timestamp, typ])

        content = make_cookie_content(name, _payload, self.sign_key, domain=domain,
                                      path=path, timestamp=timestamp,
                                      enc_key=self.enc_key, max_age=max_age,
                                      sign_alg=self.sign_alg)

        for name, args in content.items():
            cookie[name] = args['value']
            for key, value in args.items():
                if key == 'value':
                    continue
                cookie[name][key] = value

        return cookie


Instruction:
How would you implement a function in Python 3 that
calculates AC power of a node.

Response:
def f_ac_power(inverter, v_mp, p_mp):
    """
    Calculate AC power

    :param inverter:
    :param v_mp:
    :param p_mp:
    :return: AC power [W]
    """
    return pvlib.pvsystem.snlinverter(v_mp, p_mp, inverter).flatten()


Instruction:
Write a Python 3 script for
calculating DC power using Sandia Performance model

Response:
def f_dc_power(effective_irradiance, cell_temp, module):
    """
    Calculate DC power using Sandia Performance model

    :param effective_irradiance: effective irradiance [suns]
    :param cell_temp: PV cell temperature [degC]
    :param module: PV module dictionary or pandas data frame
    :returns: i_sc, i_mp, v_oc, v_mp, p_mp
    """
    dc = pvlib.pvsystem.sapm(effective_irradiance, cell_temp, module)
    fields = ('i_sc', 'i_mp', 'v_oc', 'v_mp', 'p_mp')
    return tuple(dc[field] for field in fields)


Instruction:
Given the following Python 3 function, write the documentation
def f_effective_irradiance(poa_direct, poa_diffuse, am_abs, aoi, module):
    Ee = pvlib.pvsystem.sapm_effective_irradiance(poa_direct, poa_diffuse,
                                                  am_abs, aoi, module)
    return Ee.reshape(1, -1)

Response: Calculates effective irradiance for Sandia Performance model


Instruction:
Write a Python 3 script to
calculate cell temperature. :param poa_global: plane of array global irradiance [W/m**2] :param wind_speed: wind speed [m/s] :param air_temp: ambient dry bulb air temperature [degC] :return: cell temperature [degC]

Response:
def f_cell_temp(poa_global, wind_speed, air_temp):
    """
    Calculate cell temperature.

    :param poa_global: plane of array global irradiance [W/m**2]
    :param wind_speed: wind speed [m/s]
    :param air_temp: ambient dry bulb air temperature [degC]
    :return: cell temperature [degC]
    """
    temps = pvlib.pvsystem.sapm_celltemp(poa_global, wind_speed, air_temp)
    return temps['temp_cell'].values, temps['temp_module'].values


Instruction:
Make a summary of the following Python 3 code
def f_aoi(surface_tilt, surface_azimuth, solar_zenith, solar_azimuth):
    return pvlib.irradiance.aoi(surface_tilt, surface_azimuth,
                                solar_zenith, solar_azimuth)

Response: Calculate angle of incidence on a node in a node list.


Instruction:
Can you tell what is the following Python 3 function doing
def find_version(filename):
    with open(filename, "r") as filep:
        version_file = filep.read()
        version_match = re.search(
            r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M
        )
        if version_match:
            return version_match.group(1)
        raise RuntimeError("Unable to find version string.")

Response: Find the version string in given file and return it.


Instruction:
Can you generate the documentation for the following Python 3 function
def save_ndarray_to_fits(array=None, file_name=None,
                         main_header=None,
                         cast_to_float=True,
                         crpix1=None, crval1=None, cdelt1=None,
                         overwrite=True):
    """Save numpy array(s) into a FITS file with the provided filename.

    Parameters
    ----------
    array : numpy array or list of numpy arrays
        Array(s) to be exported as the FITS file. If the input is
        a list, a multi-extension FITS file is generated assuming
        that the list contains a list of arrays.
    file_name : string
        File name for the output FITS file.
    main_header : astropy FITS header
        Header to be introduced in the primary HDU.
    cast_to_float : bool or list of booleans
        If True, the array(s) data are save as float. If a list of
        arrays has been provided, this parameter must be either a list
        (with the same length) of booleans or None.
    crpix1 : float, list of floats or None
        If not None, this value is used for the keyword CRPIX1.
        If a list of arrays has been provided, this paramater must be
        either a list (with the same length) of floats or None.
    crval1 : float, list of floats or None
        If not None, this value is used for the keyword CRVAL1.
        If a list of arrays has been provided, this paramater must be
        either a list (with the same length) of floats or None.
    cdelt1 : float, list of floats or None
        If not None, this value is used for the keyword CDELT1.
        If a list of arrays has been provided, this paramater must be
        either a list (with the same length) of floats or None.
    overwrite : bool
        If True, the file is overwritten (in the case it already
        exists).

    """

    # protections
    if file_name is None:
        raise ValueError("File_name is not defined in save_ndarray_to_fits")

    if type(array) is list:
        list_of_arrays = array
        narrays = len(list_of_arrays)
        # cast_to_float must be a list of bools
        if type(cast_to_float) is not list:
            raise ValueError("Expected list of cast_to_float not found!")
        else:
            if len(cast_to_float) != narrays:
                raise ValueError("Unexpected length of cast_to_float")
        list_cast_to_float = cast_to_float
        # check that the additional associated lists have been provided
        # and that they have the expected length (or they are None)
        for ldum, cdum in zip([crpix1, crval1, cdelt1],
                              ['crpix1', 'crval1', 'cdelt1']):
            if ldum is not None:
                if type(ldum) is not list:
                    raise ValueError("Expected list of " + cdum +
                                     " not found!")
                else:
                    if len(ldum) != narrays:
                        raise ValueError("Unexpected length of " + cdum)
        if crpix1 is None:
            list_crpix1 = [None] * narrays
        else:
            list_crpix1 = crpix1
        if crval1 is None:
            list_crval1 = [None] * narrays
        else:
            list_crval1 = crval1
        if cdelt1 is None:
            list_cdelt1 = [None] * narrays
        else:
            list_cdelt1 = cdelt1
    else:
        list_of_arrays = [array]
        list_cast_to_float = [cast_to_float]
        list_crpix1 = [crpix1]
        list_crval1 = [crval1]
        list_cdelt1 = [cdelt1]

    hdulist = fits.HDUList()

    for ihdu, tmp_array in enumerate(list_of_arrays):
        if type(tmp_array) is not np.ndarray:
            raise ValueError("Array#" + str(ihdu) + "=" + str(tmp_array) +
                             " must be a numpy.ndarray")
        if ihdu == 0:
            if list_cast_to_float[ihdu]:
                hdu = fits.PrimaryHDU(data=tmp_array.astype(np.float),
                                      header=main_header)
            else:
                hdu = fits.PrimaryHDU(data=tmp_array, header=main_header)
        else:
            if list_cast_to_float[ihdu]:
                hdu = fits.ImageHDU(data=tmp_array.astype(np.float))
            else:
                hdu = fits.ImageHDU(data=tmp_array)

        # set additional FITS keywords if requested
        tmp_crpix1 = list_crpix1[ihdu]
        if tmp_crpix1 is not None:
            hdu.header.set('CRPIX1', tmp_crpix1, 'Reference pixel')
        tmp_crval1 = list_crval1[ihdu]
        if tmp_crval1 is not None:
            hdu.header.set('CRVAL1', tmp_crval1,
                           'Reference wavelength corresponding to CRPIX1')
        tmp_cdelt1 = list_cdelt1[ihdu]
        if tmp_cdelt1 is not None:
            hdu.header.set('CDELT1', tmp_cdelt1,
                           'Linear dispersion (angstrom/pixel)')

        # add HDU to HDUList
        hdulist.append(hdu)

    # write output file
    hdulist.writeto(file_name, overwrite=overwrite)

Response: Save numpy array into a FITS file with the provided filename.


Instruction:
Make a summary of the following Python 3 code
def find_position(edges, prow, bstart, bend, total=5):

    nt = total // 2

    # This bar is too near the border
    if prow-nt < 0 or prow + nt >= edges.shape[0]:
        return []

    s2edges = edges[prow-nt:prow+nt+1, bstart:bend]

    structure = morph.generate_binary_structure(2,2) # 8 way conection
    har, num_f = mes.label(s2edges, structure=structure)

    cen_of_mass = mes.center_of_mass(s2edges, labels=har, index=range(1, num_f + 1))

    # center_of_mass returns y, x coordinates

    cen_of_mass_off = [(x + bstart, prow-nt + y) for y,x in cen_of_mass]

    return cen_of_mass_off

Response: Find a EMIR CSU bar position in a 2d edge image.


Instruction:
Can you implement a function in Python 3 that
computes the FWHM of the maximum image in the given axis

Response:
def calc_fwhm(img, region, fexpand=3, axis=0):
    """Compute the FWHM in the direction given by axis"""

    # We compute know the FWHM of the slit
    # Given the computed position of the slit
    # Expand 'fexpand' pixels around
    # and cut an slice in the median filtered image

    xpregion = expand_region(region, fexpand, fexpand)
    cslit = img[xpregion]

    # Collapse it
    pslit = cslit.mean(axis=axis)

    # Estimate the background as a flat line
    # starting in pslit[0] and ending in pslit[-1]
    x2 = len(pslit)
    y1, y2 = pslit[0], pslit[-1]
    mslope = (y2-y1) / x2
    # background estimation
    backstim = mslope*numpy.arange(x2) + y1

    # We subtract background
    qslit = pslit-backstim
    # and find the pixel of the maximum
    pidx = numpy.argmax(qslit)
    peak, fwhm = fmod.compute_fwhm_1d_simple(qslit, pidx)
    return fwhm


Instruction:
Create a Python 3 function to
find the first peak to the right of start

Response:
def simple_prot(x, start):
    """Find the first peak to the right of start"""

    # start must b >= 1

    for i in range(start,len(x)-1):
        a,b,c =  x[i-1], x[i], x[i+1]
        if b - a > 0 and b -c >= 0:
            return i
    else:
        return None


Instruction:
Create a Python 3 function for
finding the position where the value is half of the peak

Response:
def position_half_h(pslit, cpix, backw=4):
    """Find the position where the value is half of the peak"""

    # Find the first peak to the right of cpix
    next_peak = simple_prot(pslit, cpix)

    if next_peak is None:
        raise ValueError

    dis_peak = next_peak - cpix

    wpos2 = cpix - dis_peak
    wpos1 = wpos2 - backw

    # Compute background in a window of width backw
    # in a position simetrical to the peak
    # around cpix
    left_background = pslit[wpos1:wpos2].min()

    # height of the peak
    height = pslit[next_peak] - left_background

    half_height = left_background + 0.5 * height

    # Position at halg peak, linear interpolation
    vv = pslit[wpos1:next_peak+1] - half_height

    res1, =  numpy.nonzero(numpy.diff(vv > 0))
    i1 = res1[0]

    xint = wpos1 + i1 + (0 - vv[i1]) / (vv[i1+1] - vv[i1])

    return xint, next_peak, wpos1, wpos2, left_background, half_height


Instruction:
Implement a function in Python 3 to
fine position of the left CSU bar

Response:
def locate_bar_l(icut, epos):
    """Fine position of the left CSU bar"""
    def swap_coor(x):
        return x

    def swap_line(tab):
        return tab

    return _locate_bar_gen(icut, epos,
                           transform1=swap_coor,
                           transform2=swap_line
                           )


Instruction:
Implement a Python 3 function for
fining position of the right CSU bar

Response:
def locate_bar_r(icut, epos):
    """Fine position of the right CSU bar"""
    sm = len(icut)

    def swap_coor(x):
        return sm - 1 - x

    def swap_line(tab):
        return tab[::-1]

    return _locate_bar_gen(icut, epos, transform1=swap_coor,
                           transform2=swap_line)


Instruction:
Here you have a function in Python 3, explain what it does
def overlap(intv1, intv2):
    return max(0, min(intv1[1], intv2[1]) - max(intv1[0], intv2[0]))

Response: Overlaps of two intervals


Instruction:
How would you code a function in Python 3 to
convert virtual pixel to real pixel.

Response:
def exvp_scalar(x, y, x0, y0, c2, c4, theta0, ff):
    """Convert virtual pixel to real pixel.

    Parameters
    ----------
    x : array-like of floats
        X coordinate (pixel).
    y : array-like of floats
        Y coordinate (pixel).
    x0 : float
        X coordinate of reference pixel, in units of 1E3.
    y0 : float
        Y coordinate of reference pixel, in units of 1E3.
    c2 : float
        Coefficient corresponding to the term r**2 in distortion
        equation, in units of 1E4.
    c4 : float
        Coefficient corresponding to the term r**4 in distortion
        equation, in units of 1E9
    theta0 : float
        Additional rotation angle (radians).
    ff : float
        Scaling factor to be applied to the Y axis.

    Returns
    -------
    xdist, ydist : tuple of floats
        Distorted coordinates.

    """

    # plate scale: 0.1944 arcsec/pixel
    # conversion factor (in radian/pixel)
    factor = 0.1944 * np.pi/(180.0*3600)
    # distance from image center (pixels)
    r_pix = np.sqrt((x - x0*1000)**2 + (y - y0*1000)**2)
    # distance from imagen center (radians)
    r_rad = factor * r_pix
    # radial distortion: this number is 1.0 for r=0 and increases
    # slightly (reaching values around 1.033) for r~sqrt(2)*1024
    # (the distance to the corner of the detector measured from the
    # center)
    rdist = (1 +
             c2 * 1.0E4 * r_rad**2 +
             c4 * 1.0E9 * r_rad**4)
    # angle measured from the Y axis towards the X axis
    theta = np.arctan((x - x0*1000)/(y - y0*1000))
    if y < y0*1000:
        theta = theta - np.pi
    # distorted coordinates
    xdist = (rdist * r_pix * np.sin(theta+theta0)) + x0*1000
    ydist = (ff * rdist * r_pix * np.cos(theta+theta0)) + y0*1000

    return xdist, ydist


Instruction:
Here you have a function in Python 3, explain what it does
def exvp(x, y, x0, y0, c2, c4, theta0, ff):

    if all([np.isscalar(x), np.isscalar(y)]):
        xdist, ydist = exvp_scalar(x, y, x0=x0, y0=y0,
                                   c2=c2, c4=c4, theta0=theta0, ff=ff)
        return xdist, ydist
    elif any([np.isscalar(x), np.isscalar(y)]):
        raise ValueError("invalid mixture of scalars and arrays")
    else:
        xdist = []
        ydist = []
        for x_, y_ in zip(x, y):
            xdist_, ydist_ = exvp_scalar(x_, y_, x0=x0, y0=y0,
                                         c2=c2, c4=c4, theta0=theta0, ff=ff)
            xdist.append(xdist_)
            ydist.append(ydist_)
        return np.array(xdist), np.array(ydist)

Response: This function converts virtual pixel to real pixel.


Instruction:
Can you generate the documentation for the following Python 3 function
def return_params(islitlet, csu_bar_slit_center, params, parmodel):

    if parmodel == "longslit":
        # set each variable in EXPECTED_PARAMETER_LIST to the value
        # transferred through 'params'
        c2 = params['c2'].value
        c4 = params['c4'].value
        ff = params['ff'].value
        slit_gap = params['slit_gap'].value
        slit_height = params['slit_height'].value
        theta0_origin = params['theta0_origin'].value
        theta0_slope = params['theta0_slope'].value
        x0 = params['x0'].value
        y0 = params['y0'].value
        y_baseline = params['y_baseline'].value
    else:
        # set each variable in EXPECTED_PARAMETER_LIST_EXTENDED to the value
        # transferred through 'params'
        c2_a0s = params['c2_a0s'].value
        c2_a1s = params['c2_a1s'].value / 1E3
        c2_a2s = params['c2_a2s'].value / 1E6
        c2 = c2_a0s + \
             c2_a1s * csu_bar_slit_center + \
             c2_a2s * csu_bar_slit_center ** 2
        # ---
        c4_a0s = params['c4_a0s'].value
        c4_a1s = params['c4_a1s'].value / 1E3
        c4_a2s = params['c4_a2s'].value / 1E6
        c4 = c4_a0s + \
             c4_a1s * csu_bar_slit_center + \
             c4_a2s * csu_bar_slit_center ** 2
        # ---
        ff_a0s = params['ff_a0s'].value
        ff_a1s = params['ff_a1s'].value / 1E3
        ff_a2s = params['ff_a2s'].value / 1E6
        ff = ff_a0s + \
             ff_a1s * csu_bar_slit_center + \
             ff_a2s * csu_bar_slit_center ** 2
        # ---
        slit_gap_a0s = params['slit_gap_a0s'].value
        slit_gap_a1s = params['slit_gap_a1s'].value / 1E3
        slit_gap_a2s = params['slit_gap_a2s'].value / 1E6
        slit_gap = slit_gap_a0s + \
                   slit_gap_a1s * csu_bar_slit_center + \
                   slit_gap_a2s * csu_bar_slit_center ** 2
        # ---
        slit_height_a0s = params['slit_height_a0s'].value
        slit_height_a1s = params['slit_height_a1s'].value / 1E3
        slit_height_a2s = params['slit_height_a2s'].value / 1E6
        slit_height = slit_height_a0s + \
                      slit_height_a1s * csu_bar_slit_center + \
                      slit_height_a2s * csu_bar_slit_center ** 2
        # ---
        theta0_origin_a0s = params['theta0_origin_a0s'].value
        theta0_origin_a1s = params['theta0_origin_a1s'].value / 1E3
        theta0_origin_a2s = params['theta0_origin_a2s'].value / 1E6
        theta0_origin = theta0_origin_a0s + \
                        theta0_origin_a1s * csu_bar_slit_center + \
                        theta0_origin_a2s * csu_bar_slit_center ** 2
        # ---
        theta0_slope_a0s = params['theta0_slope_a0s'].value
        theta0_slope_a1s = params['theta0_slope_a1s'].value / 1E3
        theta0_slope_a2s = params['theta0_slope_a2s'].value / 1E6
        theta0_slope = theta0_slope_a0s + \
                       theta0_slope_a1s * csu_bar_slit_center + \
                       theta0_slope_a2s * csu_bar_slit_center ** 2
        # ---
        x0_a0s = params['x0_a0s'].value
        x0_a1s = params['x0_a1s'].value / 1E3
        x0_a2s = params['x0_a2s'].value / 1E6
        x0 = x0_a0s + \
             x0_a1s * csu_bar_slit_center + \
             x0_a2s * csu_bar_slit_center ** 2
        # ---
        y0_a0s = params['y0_a0s'].value
        y0_a1s = params['y0_a1s'].value / 1E3
        y0_a2s = params['y0_a2s'].value / 1E6
        y0 = y0_a0s + \
             y0_a1s * csu_bar_slit_center + \
             y0_a2s * csu_bar_slit_center ** 2
        # ---
        y_baseline_a0s = params['y_baseline_a0s'].value
        y_baseline_a1s = params['y_baseline_a1s'].value / 1E3
        y_baseline_a2s = params['y_baseline_a2s'].value / 1E6
        y_baseline = y_baseline_a0s + \
                     y_baseline_a1s * csu_bar_slit_center + \
                     y_baseline_a2s * csu_bar_slit_center ** 2

    theta0 = theta0_origin / 1E3 + theta0_slope / 1E4 * islitlet

    return c2, c4, ff, slit_gap, slit_height, theta0, x0, y0, y_baseline

Response: Return individual model parameters from object of type Parameters.


Instruction:
Can you implement a function in Python 3 that
returns the expected distorted boundaries of a given set of parameters.

Response:
def expected_distorted_boundaries(islitlet, csu_bar_slit_center,
                                  borderlist, params, parmodel,
                                  numpts, deg, debugplot=0):
    """Return expected SpectrumTrail instances associated to a given slitlet.

    Several SpectrumTrail objects can be computed for the considered
    slitlet. The parameter borderlist is a list of floats, ranging
    from 0 to 1, indicating the spatial location of the spectrum trail
    within the slitlet: 0 means the lower boundary and 1 corresponds
    to the upper bounday. Any value in (0,1) will provide the
    spectrum trail located in between accordingly.

    Parameters
    ----------
    islitlet : int
        Number of slitlet.
    csu_bar_slit_center : float
        CSU bar slit center, in mm.
    borderlist : list of floats
        Each float provides the fractional vertical location of the
        spectrum trail relative to the lower boundary. In other words,
        0.0 corresponds to the lower boundary, 1.0 to the upper
        boundary, and any number in the interval (0,1) will be a
        spectral trail in between.
    params : :class:`~lmfit.parameter.Parameters`
        Parameters to be employed in the prediction of the distorted
        boundaries.
    parmodel : str
        Model to be assumed. Allowed values are 'longslit' and
        'multislit'.
    numpts : int
        Number of points in which the X-range interval is subdivided
        before fitting the returned polynomial(s).
    deg : int
        Degree of the fitted polynomial.
    debugplot : int
        Debugging level for messages and plots. For details see
        'numina.array.display.pause_debugplot.py'.

    Returns
    -------
    list_spectrails : list of SpectrumTrail objects
        List containing the fitted spectrum trails.

    """

    c2, c4, ff, slit_gap, slit_height, theta0, x0, y0, y_baseline = \
        return_params(islitlet, csu_bar_slit_center, params, parmodel)

    xp = np.linspace(1, EMIR_NAXIS1, numpts)
    slit_dist = (slit_height * 10) + slit_gap

    # undistorted (constant) y-coordinate of the lower and upper boundaries
    ybottom = y_baseline * 100 + (islitlet - 1) * slit_dist
    ytop = ybottom + (slit_height * 10)

    list_spectrails = []
    for borderval in borderlist:
        yvalue = ybottom + borderval * (ytop - ybottom)
        # undistorted boundary
        yp_value = np.ones(numpts) * yvalue
        # distorted boundary
        xdist, ydist = exvp(xp, yp_value, x0=x0, y0=y0,
                            c2=c2, c4=c4, theta0=theta0, ff=ff)
        spectrail = SpectrumTrail()  # declare SpectrumTrail instance
        spectrail.fit(x=xdist, y=ydist, deg=deg, debugplot=debugplot)
        list_spectrails.append(spectrail)

    return list_spectrails


Instruction:
Write a Python 3 script to
function to be minimised.

Response:
def fun_residuals(params, parmodel, bounddict,
                  shrinking_factor, numresolution,
                  islitmin, islitmax, debugplot):
    """Function to be minimised.

    Parameters
    ----------
    params : :class:`~lmfit.parameter.Parameters`
        Parameters to be employed in the prediction of the distorted
        boundaries.
    parmodel : str
        Model to be assumed. Allowed values are 'longslit' and
        'multislit'.
    bounddict : JSON structure
        Structure employed to store bounddict information.
    shrinking_factor : float
        Fraction of the detected X range (specrtral) to be employed
        in the fit. This must be a number verifying
        0 < shrinking_factor <= 1. The resulting interval will be
        centered within the original one.
    numresolution : int
        Number of points in which the X-range interval is subdivided
        before computing the residuals.
    islitmin : int
        Minimum slitlet number.
    islitmax : int
        Maximum slitlet number.
    debugplot : int
        Debugging level for messages and plots. For details see
        'numina.array.display.pause_debugplot.py'.

    Returns
    -------
    global_residual : float
        Squared root of the averaged sum of squared residuals.

    """

    global FUNCTION_EVALUATIONS
    global_residual = 0.0
    nsummed = 0

    read_slitlets = list(bounddict['contents'].keys())
    # read_slitlets.sort()  # this is not really necessary
    for tmp_slitlet in read_slitlets:
        islitlet = int(tmp_slitlet[7:])
        if islitmin <= islitlet <= islitmax:
            read_dateobs = list(bounddict['contents'][tmp_slitlet].keys())
            # read_dateobs.sort()  # this is not really necessary
            for tmp_dateobs in read_dateobs:
                tmp_dict = bounddict['contents'][tmp_slitlet][tmp_dateobs]
                csu_bar_slit_center = tmp_dict['csu_bar_slit_center']
                # expected boundaries using provided parameters
                list_spectrails = expected_distorted_boundaries(
                        islitlet, csu_bar_slit_center,
                        [0, 1], params, parmodel,
                        numpts=numresolution, deg=5, debugplot=0
                    )
                poly_lower_expected = list_spectrails[0].poly_funct
                poly_upper_expected = list_spectrails[1].poly_funct
                # measured lower boundary
                poly_lower_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_lower']
                )
                xmin_lower_bound = tmp_dict['boundary_xmin_lower']
                xmax_lower_bound = tmp_dict['boundary_xmax_lower']
                dx = (xmax_lower_bound - xmin_lower_bound) * \
                     (1 - shrinking_factor) / 2
                xdum_lower = np.linspace(xmin_lower_bound + dx,
                                         xmax_lower_bound - dx,
                                         num=numresolution)
                # distance between expected and measured polynomials
                poly_diff = poly_lower_expected - poly_lower_measured
                global_residual += np.sum(poly_diff(xdum_lower)**2)
                nsummed += numresolution
                # measured upper boundary
                poly_upper_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_upper']
                )
                xmin_upper_bound = tmp_dict['boundary_xmin_upper']
                xmax_upper_bound = tmp_dict['boundary_xmax_upper']
                dx = (xmax_lower_bound - xmin_lower_bound) * \
                     (1 - shrinking_factor) / 2
                xdum_upper = np.linspace(xmin_upper_bound + dx,
                                         xmax_upper_bound - dx,
                                         num=numresolution)
                # distance between expected and measured polynomials
                poly_diff = poly_upper_expected - poly_upper_measured
                global_residual += np.sum(poly_diff(xdum_upper)**2)
                nsummed += numresolution

    if nsummed > 0:
        global_residual = np.sqrt(global_residual/nsummed)
    if debugplot >= 10:
        FUNCTION_EVALUATIONS += 1
        print('-' * 79)
        print('>>> Number of function evaluations:', FUNCTION_EVALUATIONS)
        print('>>> global residual...............:', global_residual)
        params.pretty_print()
    return global_residual


Instruction:
Explain what the following Python 3 code does
def overplot_boundaries_from_bounddict(ax, bounddict, micolors, linetype='-'):

    for islitlet in range(1, EMIR_NBARS + 1):
        tmpcolor = micolors[islitlet % 2]
        tmp_slitlet = 'slitlet' + str(islitlet).zfill(2)
        if tmp_slitlet in bounddict['contents'].keys():
            read_dateobs = list(bounddict['contents'][tmp_slitlet].keys())
            read_dateobs.sort()
            for tmp_dateobs in read_dateobs:
                tmp_dict = bounddict['contents'][tmp_slitlet][tmp_dateobs]
                # lower boundary
                pol_lower_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_lower']
                )
                xdum = np.linspace(1, EMIR_NAXIS1, num=EMIR_NAXIS1)
                ydum = pol_lower_measured(xdum)
                ax.plot(xdum, ydum, tmpcolor + linetype)
                pol_upper_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_upper']
                )
                ydum = pol_upper_measured(xdum)
                ax.plot(xdum, ydum, tmpcolor + linetype)

Response: Overplot boundaries on current plot.


Instruction:
Explain what the following Python 3 code does
def save_boundaries_from_bounddict_ds9(bounddict, ds9_filename, numpix=100):

    ds9_file = open(ds9_filename, 'w')

    ds9_file.write('# Region file format: DS9 version 4.1\n')
    ds9_file.write('global color=green dashlist=2 4 width=2 '
                   'font="helvetica 10 normal roman" select=1 '
                   'highlite=1 dash=1 fixed=0 edit=1 '
                   'move=1 delete=1 include=1 source=1\n')
    ds9_file.write('physical\n')

    uuid = bounddict['uuid']
    spfilter = bounddict['tags']['filter']
    grism = bounddict['tags']['grism']

    ds9_file.write('#\n# uuid (boundict file): {0}\n'.format(uuid))
    ds9_file.write('# filter..............: {0}\n'.format(spfilter))
    ds9_file.write('# grism...............: {0}\n'.format(grism))

    colorbox = ['green', 'green']
    for islitlet in range(1, EMIR_NBARS + 1):
        tmp_slitlet = 'slitlet' + str(islitlet).zfill(2)
        if tmp_slitlet in bounddict['contents'].keys():
            ds9_file.write('#\n# islitlet: {0}\n'.format(tmp_slitlet))
            read_dateobs = list(bounddict['contents'][tmp_slitlet].keys())
            read_dateobs.sort()
            for tmp_dateobs in read_dateobs:
                ds9_file.write('#\n# date-obs: {0}\n'.format(tmp_dateobs))
                tmp_dict = bounddict['contents'][tmp_slitlet][tmp_dateobs]
                # lower boundary
                pol_lower_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_lower']
                )
                xmin_lower = tmp_dict['boundary_xmin_lower']
                xmax_lower = tmp_dict['boundary_xmax_lower']
                xdum = np.linspace(xmin_lower, xmax_lower, num=numpix)
                ydum = pol_lower_measured(xdum)
                for i in range(len(xdum) - 1):
                    ds9_file.write(
                        'line {0} {1} {2} {3}'.format(xdum[i], ydum[i],
                                                      xdum[i + 1], ydum[i + 1])
                    )
                    ds9_file.write(
                        ' # color={0}\n'.format(colorbox[islitlet % 2]))
                # upper boundary
                pol_upper_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_upper']
                )
                xmin_upper = tmp_dict['boundary_xmin_upper']
                xmax_upper = tmp_dict['boundary_xmax_upper']
                xdum = np.linspace(xmin_upper, xmax_upper, num=numpix)
                ydum = pol_upper_measured(xdum)
                for i in range(len(xdum) - 1):
                    ds9_file.write(
                        'line {0} {1} {2} {3}'.format(xdum[i], ydum[i],
                                                      xdum[i + 1], ydum[i + 1])
                    )
                    ds9_file.write(
                        ' # color={0}\n'.format(colorbox[islitlet % 2]))
                # slitlet label
                xlabel = xmax_lower + xmax_upper + xmin_lower + xmin_upper
                xlabel /= 4
                yc_lower = pol_lower_measured(xlabel)
                yc_upper = pol_upper_measured(xlabel)
                ds9_file.write('text {0} {1} {{{2}}} # color={3} '
                               'font="helvetica 10 bold '
                               'roman"\n'.format(xlabel,
                                                 (yc_lower + yc_upper) / 2,
                                                 islitlet,
                                                 colorbox[islitlet % 2]))

    ds9_file.close()

Response: Export to ds9 region file the boundaries in bounddict.


Instruction:
Given the following Python 3 function, write the documentation
def save_boundaries_from_params_ds9(params, parmodel,
                                    list_islitlet,
                                    list_csu_bar_slit_center,
                                    uuid, grism, spfilter,
                                    ds9_filename, numpix=100,
                                    global_offset_x_pix=0,
                                    global_offset_y_pix=0):
    """Export to ds9 region file the boundaries parametrised with params.

    Parameters
    ----------
    params : :class:`~lmfit.parameter.Parameters`
        Parameters to be employed in the prediction of the distorted
        boundaries.
    parmodel : str
        Model to be assumed. Allowed values are 'longslit' and
        'multislit'.
    list_islitlet : list (integers)
        Slitlet numbers to be considered.
    list_csu_bar_slit_center : list of floats
        CSU bar slit centers of the considered slitlets.
    uuid: str
        UUID associated to the parameters 'params'.
    grism : str
        Employed grism.
    spfilter : str
        Employed filter.
    ds9_filename : str
        Output file name for the ds9 region file.
    numpix : int
        Number of points in which the X-range interval is subdivided
        in order to save each boundary as a connected set of line
        segments.
    global_offset_x_pix : int or float
        Global offset in the X direction to be applied after computing
        the expected location.
    global_offset_y_pix : int or float
        Global offset in the Y direction to be applied after computing
        the expected location.

    """

    ds9_file = open(ds9_filename, 'w')

    ds9_file.write('# Region file format: DS9 version 4.1\n')
    ds9_file.write('global color=green dashlist=2 4 width=2 '
                   'font="helvetica 10 normal roman" select=1 '
                   'highlite=1 dash=1 fixed=0 edit=1 '
                   'move=1 delete=1 include=1 source=1\n')
    ds9_file.write('physical\n#\n')

    ds9_file.write('#\n# uuid..: {0}\n'.format(uuid))
    ds9_file.write('# filter: {0}\n'.format(spfilter))
    ds9_file.write('# grism.: {0}\n'.format(grism))

    ds9_file.write('#\n# global_offset_x_pix: {0}\n'.format(
        global_offset_x_pix))
    ds9_file.write('# global_offset_y_pix: {0}\n#\n'.format(
        global_offset_y_pix))

    # duplicate to shorten the variable names
    xoff = float(global_offset_x_pix)
    yoff = float(global_offset_y_pix)

    if parmodel == "longslit":
        for dumpar in EXPECTED_PARAMETER_LIST:
            parvalue = params[dumpar].value
            ds9_file.write('# {0}: {1}\n'.format(dumpar, parvalue))
    else:
        for dumpar in EXPECTED_PARAMETER_LIST_EXTENDED:
            parvalue = params[dumpar].value
            ds9_file.write('# {0}: {1}\n'.format(dumpar, parvalue))

    for islitlet, csu_bar_slit_center in \
            zip(list_islitlet, list_csu_bar_slit_center):
        if islitlet % 2 == 0:
            colorbox = '#ff00ff'  # '#ff77ff'
        else:
            colorbox = '#00ffff'  # '#4444ff'

        ds9_file.write(
            '#\n# islitlet...........: {0}\n'.format(islitlet)
        )
        ds9_file.write(
            '# csu_bar_slit_center: {0}\n'.format(csu_bar_slit_center)
        )
        pol_lower_expected = expected_distorted_boundaries(
            islitlet, csu_bar_slit_center, [0], params, parmodel,
            numpts=101, deg=5, debugplot=0
        )[0].poly_funct
        pol_upper_expected = expected_distorted_boundaries(
            islitlet, csu_bar_slit_center, [1], params, parmodel,
            numpts=101, deg=5, debugplot=0
        )[0].poly_funct
        xdum = np.linspace(1, EMIR_NAXIS1, num=numpix)
        ydum = pol_lower_expected(xdum)
        for i in range(len(xdum)-1):
            ds9_file.write(
                'line {0} {1} {2} {3}'.format(xdum[i]+xoff, ydum[i]+yoff,
                                              xdum[i+1]+xoff, ydum[i+1]+yoff)
            )
            ds9_file.write(' # color={0}\n'.format(colorbox))
        ydum = pol_upper_expected(xdum)
        for i in range(len(xdum)-1):
            ds9_file.write(
                'line {0} {1} {2} {3}'.format(xdum[i]+xoff, ydum[i]+yoff,
                                              xdum[i+1]+xoff, ydum[i+1]+yoff)
            )
            ds9_file.write(' # color={0}\n'.format(colorbox))
        # slitlet label
        yc_lower = pol_lower_expected(EMIR_NAXIS1 / 2 + 0.5)
        yc_upper = pol_upper_expected(EMIR_NAXIS1 / 2 + 0.5)
        ds9_file.write('text {0} {1} {{{2}}} # color={3} '
                       'font="helvetica 10 bold '
                       'roman"\n'.format(EMIR_NAXIS1 / 2 + 0.5 + xoff,
                                         (yc_lower + yc_upper) / 2 + yoff,
                                         islitlet,
                                         colorbox))

    ds9_file.close()

Response: Export to ds9 region file the boundaries parametrised with params.


Instruction:
Write a Python 3 script for
defining ~lmfit. parameter. Parameters object from dictionary.

Response:
def bound_params_from_dict(bound_param_dict):
    """Define `~lmfit.parameter.Parameters` object from dictionary.

    Parameters
    ----------
    bound_param_dict : dictionary
        Dictionary containing the JSON contents of a boundary
        parameter file.

    Returns
    -------
    params : :class:`~lmfit.parameter.Parameters`
        Parameters object.

    """

    params = Parameters()
    for mainpar in EXPECTED_PARAMETER_LIST:
        if mainpar not in bound_param_dict['contents'].keys():
            raise ValueError('Parameter ' + mainpar + ' not found!')
        if bound_param_dict['meta_info']['parmodel'] == "longslit":
            dumdict = bound_param_dict['contents'][mainpar]
            params.add(mainpar, value=dumdict["value"],
                       vary=dumdict["vary"])
        elif bound_param_dict['meta_info']['parmodel'] == 'multislit':
            for subpar in ['a0s', 'a1s', 'a2s']:
                if subpar not in bound_param_dict['contents'][mainpar].keys():
                    raise ValueError('Subparameter ' + subpar + ' not found' +
                                     ' under parameter ' + mainpar)
                cpar = mainpar + '_' + subpar
                dumdict = bound_param_dict['contents'][mainpar][subpar]
                params.add(cpar, value=dumdict["value"],
                           vary=dumdict["vary"])
        else:
            print('parmodel: ', bound_param_dict['meta_info']['parmodel'])
            raise ValueError('Unexpected parmodel')
    return params


Instruction:
Given the following Python 3 function, write the documentation
def set_base_headers(self, hdr):
        hdr = super(StareImageRecipe2, self).set_base_headers(hdr)
        # Set EXP to 0
        hdr['EXP'] = 0
        return hdr

Response: Set metadata in FITS headers.


Instruction:
How would you code a function in Python 3 to
set metadata in FITS headers.

Response:
def set_base_headers(self, hdr):
        """Set metadata in FITS headers."""
        hdr = super(StareImageBaseRecipe, self).set_base_headers(hdr)
        # Update EXP to 0
        hdr['EXP'] = 0
        return hdr


Instruction:
Can you generate a brief explanation for the following Python 3 code
def apply_rectwv_coeff(reduced_image,
                       rectwv_coeff,
                       args_resampling=2,
                       args_ignore_dtu_configuration=True,
                       debugplot=0):
    """Compute rectification and wavelength calibration coefficients.

    Parameters
    ----------
    reduced_image : HDUList object
        Image with preliminary basic reduction: bpm, bias, dark and
        flatfield.
    rectwv_coeff : RectWaveCoeff instance
        Rectification and wavelength calibration coefficients for the
        particular CSU configuration.
    args_resampling : int
        1: nearest neighbour, 2: flux preserving interpolation.
    args_ignore_dtu_configuration : bool
        If True, ignore differences in DTU configuration.
    debugplot : int
        Debugging level for messages and plots. For details see
        'numina.array.display.pause_debugplot.py'.

    Returns
    -------
    rectwv_image : HDUList object
        Rectified and wavelength calibrated image.

    """

    logger = logging.getLogger(__name__)

    # header and data array (use deepcopy to avoid modifying
    # reduced_image[0].header as a side effect)
    header = copy.deepcopy(reduced_image[0].header)
    image2d = reduced_image[0].data

    # apply global offsets
    image2d = apply_integer_offsets(
        image2d=image2d,
        offx=rectwv_coeff.global_integer_offset_x_pix,
        offy=rectwv_coeff.global_integer_offset_y_pix
    )

    # check grism and filter
    filter_name = header['filter']
    logger.info('Filter: ' + filter_name)
    if filter_name != rectwv_coeff.tags['filter']:
        raise ValueError('Filter name does not match!')
    grism_name = header['grism']
    logger.info('Grism: ' + grism_name)
    if grism_name != rectwv_coeff.tags['grism']:
        raise ValueError('Grism name does not match!')

    # read the DTU configuration from the image header
    dtu_conf = DtuConfiguration.define_from_header(header)

    # retrieve DTU configuration from RectWaveCoeff object
    dtu_conf_calib = DtuConfiguration.define_from_dictionary(
        rectwv_coeff.meta_info['dtu_configuration']
    )
    # check that the DTU configuration employed to obtain the calibration
    # corresponds to the DTU configuration in the input FITS file
    if dtu_conf != dtu_conf_calib:
        if args_ignore_dtu_configuration:
            logger.warning('DTU configuration differences found!')
        else:
            logger.warning('DTU configuration from image header:')
            logger.warning(dtu_conf)
            logger.warning('DTU configuration from master calibration:')
            logger.warning(dtu_conf_calib)
            raise ValueError("DTU configurations do not match!")
    else:
        logger.info('DTU configuration match!')

    # valid slitlet numbers
    list_valid_islitlets = list(range(1, EMIR_NBARS + 1))
    for idel in rectwv_coeff.missing_slitlets:
        list_valid_islitlets.remove(idel)
    logger.debug('Valid slitlet numbers:\n' + str(list_valid_islitlets))

    # ---

    # relevant wavelength calibration parameters for rectified and wavelength
    # calibrated image
    wv_parameters = set_wv_parameters(filter_name, grism_name)
    crpix1_enlarged = wv_parameters['crpix1_enlarged']
    crval1_enlarged = wv_parameters['crval1_enlarged']
    cdelt1_enlarged = wv_parameters['cdelt1_enlarged']
    naxis1_enlarged = wv_parameters['naxis1_enlarged']

    # initialize rectified and wavelength calibrated image
    naxis2_enlarged = EMIR_NBARS * EMIR_NPIXPERSLIT_RECTIFIED
    image2d_rectwv = np.zeros((naxis2_enlarged, naxis1_enlarged),
                              dtype='float32')

    # main loop

    logger.info('Applying rectification and wavelength calibration')
    logger.info('RectWaveCoeff uuid={}'.format(rectwv_coeff.uuid))

    cout = '0'
    for islitlet in range(1, EMIR_NBARS + 1):

        if islitlet in list_valid_islitlets:

            # define Slitlet2D object
            slt = Slitlet2D(islitlet=islitlet,
                            rectwv_coeff=rectwv_coeff,
                            debugplot=debugplot)

            # extract (distorted) slitlet from the initial image
            slitlet2d = slt.extract_slitlet2d(image2d)

            # rectify slitlet
            slitlet2d_rect = slt.rectify(slitlet2d, resampling=args_resampling)

            # wavelength calibration of the rectifed slitlet
            slitlet2d_rect_wv = resample_image2d_flux(
                image2d_orig=slitlet2d_rect,
                naxis1=naxis1_enlarged,
                cdelt1=cdelt1_enlarged,
                crval1=crval1_enlarged,
                crpix1=crpix1_enlarged,
                coeff=slt.wpoly
            )

            # minimum and maximum useful row in the full 2d rectified image
            # (starting from 0)
            i1 = slt.iminslt - 1
            i2 = slt.imaxslt

            # minimum and maximum scan in the rectified slitlet
            # (in pixels, from 1 to NAXIS2)
            ii1 = slt.min_row_rectified
            ii2 = slt.max_row_rectified + 1

            # save rectified slitlet in its corresponding location within
            # the full 2d rectified image
            image2d_rectwv[i1:i2, :] = slitlet2d_rect_wv[ii1:ii2, :]

            # include scan range in FITS header
            header['imnslt' + str(islitlet).zfill(2)] = \
                slt.iminslt, 'minimum Y pixel of useful slitlet region'
            header['imxslt' + str(islitlet).zfill(2)] = \
                slt.imaxslt, 'maximum Y pixel of useful slitlet region'

            # determine useful channel region in each spectrum and include
            # that information in FITS header
            jminslt = []
            jmaxslt = []
            for idum in range(ii1, ii2 + 1):
                jminmax = find_pix_borders(
                    slitlet2d_rect_wv[idum, :],
                    sought_value=0
                )
                if jminmax != (-1, naxis1_enlarged):
                    jminslt.append(jminmax[0])
                    jmaxslt.append(jminmax[1])
            if len(jminslt) > 0:
                slt.jminslt = min(jminslt) + 1
                slt.jmaxslt = max(jmaxslt) + 1
            header['jmnslt' + str(islitlet).zfill(2)] = \
                slt.jminslt, 'minimum X pixel of useful slitlet region'
            header['jmxslt' + str(islitlet).zfill(2)] = \
                slt.jmaxslt, 'maximum X pixel of useful slitlet region'

            cout += '.'

        else:

            # include scan and channel range in FITS header
            header['imnslt' + str(islitlet).zfill(2)] = \
                0, 'minimum Y pixel of useful slitlet region'
            header['imxslt' + str(islitlet).zfill(2)] = \
                0, 'maximum Y pixel of useful slitlet region'
            header['jmnslt' + str(islitlet).zfill(2)] = \
                0, 'minimum X pixel of useful slitlet region'
            header['jmxslt' + str(islitlet).zfill(2)] = \
                0, 'maximum X pixel of useful slitlet region'

            cout += 'i'

        if islitlet % 10 == 0:
            if cout != 'i':
                cout = str(islitlet // 10)

        logger.info(cout)

    # update wavelength calibration in FITS header
    logger.info('Updating image header')
    for keyword in ['crval1', 'crpix1', 'crval2', 'crpix2']:
        if keyword in header:
            header.remove(keyword)
    header['crpix1'] = (crpix1_enlarged, 'reference pixel')
    header['crval1'] = (crval1_enlarged, 'central wavelength at crpix1')
    header['cdelt1'] = (cdelt1_enlarged, 'linear dispersion (Angstrom/pixel)')
    header['cunit1'] = ('Angstrom', 'units along axis1')
    header['ctype1'] = 'WAVELENGTH'
    header['crpix2'] = (0.0, 'reference pixel')
    header['crval2'] = (0.0, 'central value at crpix2')
    header['cdelt2'] = (1.0, 'increment')
    header['ctype2'] = 'PIXEL'
    header['cunit2'] = ('Pixel', 'units along axis2')
    for keyword in ['cd1_1', 'cd1_2', 'cd2_1', 'cd2_2',
                    'PCD1_1', 'PCD1_2', 'PCD2_1', 'PCD2_2',
                    'PCRPIX1', 'PCRPIX2']:
        if keyword in header:
            header.remove(keyword)

    # update history in FITS header
    header['history'] = 'Boundary parameters uuid:' + \
                        rectwv_coeff.meta_info['origin']['bound_param'][4:]
    if 'master_rectwv' in rectwv_coeff.meta_info['origin']:
        header['history'] = \
            'MasterRectWave uuid:' + \
            rectwv_coeff.meta_info['origin']['master_rectwv'][4:]
    header['history'] = 'RectWaveCoeff uuid:' + rectwv_coeff.uuid
    header['history'] = 'Rectification and wavelength calibration time ' \
                        + datetime.now().isoformat()

    logger.info('Generating rectified and wavelength calibrated image')

    rectwv_image = fits.PrimaryHDU(data=image2d_rectwv, header=header)

    return fits.HDUList([rectwv_image])

Response: Compute rectification and wavelength calibration coefficients for a given image.


Instruction:
Implement a Python 3 function for
starting a transaction this will increment transaction semaphore and pass it to _transaction_start()

Response:
def transaction_start(self, name):
        """
        start a transaction

        this will increment transaction semaphore and pass it to _transaction_start()
        """
        if not name:
            raise ValueError("Transaction name cannot be empty")
            #uid = id(self)

        self.transaction_count += 1
        logger.debug("{}. Start transaction {}".format(self.transaction_count, name))
        if self.transaction_count == 1:
            self._transaction_start()
        else:
            self._transaction_started(name)

        return self.transaction_count


Instruction:
Can you implement a function in Python 3 that
stops a transaction if ready

Response:
def transaction_stop(self):
        """stop/commit a transaction if ready"""
        if self.transaction_count > 0:
            logger.debug("{}. Stop transaction".format(self.transaction_count))
            if self.transaction_count == 1:
                self._transaction_stop()

            self.transaction_count -= 1

        return self.transaction_count


Instruction:
Make a summary of the following Python 3 code
def connect(self, connection_config=None, *args, **kwargs):
        if self.connected: return self.connected

        if connection_config: self.connection_config = connection_config

        self.connected = True
        try:
            self._connect(self.connection_config)

        except Exception as e:
            self.connected = False
            self.raise_error(e)

        self.log("Connected {}", self.connection_config.interface_name)
        return self.connected

Response: connect to the interface


Instruction:
Write a Python 3 function that can
close an open connection

Response:
def close(self):
        """close an open connection"""
        if not self.connected: return True

        self._close()
        self.connected = False
        self.log("Closed Connection {}", self.connection_config.interface_name)
        return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def query(self, query_str, *query_args, **query_options):
        with self.connection(**query_options) as connection:
            query_options['connection'] = connection
            return self._query(query_str, query_args, **query_options)

Response: run a raw query on the database


Instruction:
How would you explain what the following Python 3 function does
def transaction(self, connection=None, **kwargs):
        with self.connection(connection) as connection:
            name = connection.transaction_name()
            connection.transaction_start(name)
            try:
                yield connection
                connection.transaction_stop()

            except Exception as e:
                connection.transaction_fail(name)
                self.raise_error(e)

Response: A context manager that creates a transaction for a bunch of db calls.


Instruction:
Can you write a function in Python 3 where it
adds the table to the database

Response:
def set_table(self, schema, **kwargs):
        """
        add the table to the db

        schema -- Schema() -- contains all the information about the table
        """
        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            if self.has_table(str(schema), **kwargs): return True

            try:
                with self.transaction(**kwargs):
                    self._set_table(schema, **kwargs)

                    for index_name, index in schema.indexes.items():
                        self.set_index(
                            schema,
                            name=index.name,
                            fields=index.fields,
                            connection=connection,
                            **index.options
                        )

            except InterfaceError:
                # check to see if this table now exists, it might have been created
                # in another thread
                if not self.has_table(schema, **kwargs):
                    raise


Instruction:
Write a Python 3 function for
checking to see if a table exists in the db

Response:
def has_table(self, table_name, **kwargs):
        """
        check to see if a table is in the db

        table_name -- string -- the table to check
        return -- boolean -- True if the table exists, false otherwise
        """
        with self.connection(kwargs.get('connection', None)) as connection:
            kwargs['connection'] = connection
            tables = self.get_tables(table_name, **kwargs)
            return len(tables) > 0


Instruction:
Implement a function in Python 3 to
get all the tables of the currently connected db

Response:
def get_tables(self, table_name="", **kwargs):
        """
        get all the tables of the currently connected db

        table_name -- string -- if you would like to filter the tables list to only include matches with this name
        return -- list -- a list of table names
        """
        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            return self._get_tables(str(table_name), **kwargs)


Instruction:
Can you write a function in Python 3 where it
deletes a table from the database

Response:
def delete_table(self, schema, **kwargs):
        """
        remove a table matching schema from the db

        schema -- Schema()
        """
        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            if not self.has_table(str(schema), **kwargs): return True
            with self.transaction(**kwargs):
                self._delete_table(schema, **kwargs)

        return True


Instruction:
Here you have a function in Python 3, explain what it does
def delete_tables(self, **kwargs):
        if not kwargs.get('disable_protection', False):
            raise ValueError('In order to delete all the tables, pass in disable_protection=True')

        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            self._delete_tables(**kwargs)

Response: Delete all the tables in the database


Instruction:
Given the following Python 3 function, write the documentation
def get_indexes(self, schema, **kwargs):
        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            return self._get_indexes(schema, **kwargs)

Response: get all the indexes in a specific schema


Instruction:
Can you implement a function in Python 3 that
adds an index to the table

Response:
def set_index(self, schema, name, fields, **index_options):
        """
        add an index to the table

        schema -- Schema()
        name -- string -- the name of the index
        fields -- array -- the fields the index should be on
        **index_options -- dict -- any index options that might be useful to create the index
        """
        with self.transaction(**index_options) as connection:
            index_options['connection'] = connection
            self._set_index(schema, name, fields, **index_options)

        return True