Instruction:
Write a Python 3 function for
creating a new container with the given root filesystem and mount points.

Response:
def create(self, root_url, mount=None, host_network=False, nics=DefaultNetworking, port=None,
        hostname=None, privileged=False, storage=None, name=None, tags=None, identity=None, env=None,
        cgroups=None):
        """
        Creater a new container with the given root flist, mount points and
        zerotier id, and connected to the given bridges
        :param root_url: The root filesystem flist
        :param mount: a dict with {host_source: container_target} mount points.
                      where host_source directory must exists.
                      host_source can be a url to a flist to mount.
        :param host_network: Specify if the container should share the same network stack as the host.
                             if True, container creation ignores both zerotier, bridge and ports arguments below. Not
                             giving errors if provided.
        :param nics: Configure the attached nics to the container
                     each nic object is a dict of the format
                     {
                        'type': nic_type # one of default, bridge, zerotier, macvlan, passthrough, vlan, or vxlan (note, vlan and vxlan only supported by ovs)
                        'id': id # depends on the type
                            bridge: bridge name,
                            zerotier: network id,
                            macvlan: the parent link name,
                            passthrough: the link name,
                            vlan: the vlan tag,
                            vxlan: the vxlan id
                        'name': name of the nic inside the container (ignored in zerotier type)
                        'hwaddr': Mac address of nic.
                        'config': { # config is only honored for bridge, vlan, and vxlan types
                            'dhcp': bool,
                            'cidr': static_ip # ip/mask
                            'gateway': gateway
                            'dns': [dns]
                        }
                     }
        :param port: A dict of host_port: container_port pairs (only if default networking is enabled)
                       Example:
                        `port={8080: 80, 7000:7000}`
        :param hostname: Specific hostname you want to give to the container.
                         if None it will automatically be set to core-x,
                         x beeing the ID of the container
        :param privileged: If true, container runs in privileged mode.
        :param storage: A Url to the ardb storage to use to mount the root flist (or any other mount that requires g8fs)
                        if not provided, the default one from core0 configuration will be used.
        :param name: Optional name for the container
        :param identity: Container Zerotier identity, Only used if at least one of the nics is of type zerotier
        :param env: a dict with the environment variables needed to be set for the container
        :param cgroups: custom list of cgroups to apply to this container on creation. formated as [(subsystem, name), ...]
                        please refer to the cgroup api for more detailes.
        """

        if nics == self.DefaultNetworking:
            nics = [{'type': 'default'}]
        elif nics is None:
            nics = []

        args = {
            'root': root_url,
            'mount': mount,
            'host_network': host_network,
            'nics': nics,
            'port': port,
            'hostname': hostname,
            'privileged': privileged,
            'storage': storage,
            'name': name,
            'identity': identity,
            'env': env,
            'cgroups': cgroups,
        }

        # validate input
        self._create_chk.check(args)

        response = self._client.raw('corex.create', args, tags=tags)

        return JSONResponse(response)


Instruction:
Can you tell what is the following Python 3 function doing
def find(self, *tags):
        tags = list(map(str, tags))
        return self._client.json('corex.find', {'tags': tags})

Response: Find containers that match set of tags.


Instruction:
Write a Python 3 function for
terminating a container given it s id

Response:
def terminate(self, container):
        """
        Terminate a container given it's id

        :param container: container id
        :return:
        """
        self._client_chk.check(container)
        args = {
            'container': int(container),
        }
        response = self._client.raw('corex.terminate', args)

        result = response.get()
        if result.state != 'SUCCESS':
            raise RuntimeError('failed to terminate container: %s' % result.data)


Instruction:
Can you implement a function in Python 3 that
adds a nic into a container

Response:
def nic_add(self, container, nic):
        """
        Hot plug a nic into a container

        :param container: container ID
        :param nic: {
                        'type': nic_type # one of default, bridge, zerotier, macvlan, passthrough, vlan, or vxlan (note, vlan and vxlan only supported by ovs)
                        'id': id # depends on the type
                            bridge: bridge name,
                            zerotier: network id,
                            macvlan: the parent link name,
                            passthrough: the link name,
                            vlan: the vlan tag,
                            vxlan: the vxlan id
                        'name': name of the nic inside the container (ignored in zerotier type)
                        'hwaddr': Mac address of nic.
                        'config': { # config is only honored for bridge, vlan, and vxlan types
                            'dhcp': bool,
                            'cidr': static_ip # ip/mask
                            'gateway': gateway
                            'dns': [dns]
                        }
                     }
        :return:
        """
        args = {
            'container': container,
            'nic': nic
        }
        self._nic_add.check(args)

        return self._client.json('corex.nic-add', args)


Instruction:
Make a summary of the following Python 3 code
def nic_remove(self, container, index):
        args = {
            'container': container,
            'index': index
        }
        self._nic_remove.check(args)

        return self._client.json('corex.nic-remove', args)

Response: Remove a nic from a container


Instruction:
Write a Python 3 function that can
return a client instance bound to the specified container.

Response:
def client(self, container):
        """
        Return a client instance that is bound to that container.

        :param container: container id
        :return: Client object bound to the specified container id
        Return a ContainerResponse from container.create
        """

        self._client_chk.check(container)
        return ContainerClient(self._client, int(container))


Instruction:
Can you generate the documentation for the following Python 3 function
def backup(self, container, url):

        args = {
            'container': container,
            'url': url,
        }

        return JSONResponse(self._client.raw('corex.backup', args))

Response: Backup a container to the given restic url


Instruction:
Write a Python 3 script for
restoring the current state of a backedup container.

Response:
def restore(self, url, tags=None):
        """
        Full restore of a container backup. This restore method will recreate
        an exact copy of the backedup container (including same network setup, and other
        configurations as defined by the `create` method.

        To just restore the container data, and use new configuration, use the create method instead
        with the `root_url` set to `restic:<url>`

        :param url: Snapshot url, the snapshot ID is passed as a url fragment
                    examples:
                        `file:///path/to/restic/repo?password=<password>#<snapshot-id>`
        :param tags: this will always override the original container tags (even if not set)
        :return:
        """
        args = {
            'url': url,
        }

        return JSONResponse(self._client.raw('corex.restore', args, tags=tags))


Instruction:
Here you have a function in Python 3, explain what it does
def create(self, name, hwaddr=None, network=None, nat=False, settings={}):
        args = {
            'name': name,
            'hwaddr': hwaddr,
            'network': {
                'mode': network,
                'nat': nat,
                'settings': settings,
            }
        }

        self._bridge_create_chk.check(args)

        return self._client.json('bridge.create', args)

Response: Create a new bridge with the given name hwaddr and network and settings.


Instruction:
Write a Python 3 function that can
delete a specific node by name.

Response:
def delete(self, bridge):
        """
        Delete a bridge by name

        :param bridge: bridge name
        :return:
        """
        args = {
            'name': bridge,
        }

        self._bridge_chk.check(args)

        return self._client.json('bridge.delete', args)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def nic_add(self, bridge, nic):

        args = {
            'name': bridge,
            'nic': nic,
        }

        self._nic_add_chk.check(args)

        return self._client.json('bridge.nic-add', args)

Response: Attach a nic to a bridge


Instruction:
Write a Python 3 function that can
detach a nic from a bridge

Response:
def nic_remove(self, nic):
        """
        Detach a nic from a bridge

        :param nic: nic name to detach
        """

        args = {
            'nic': nic,
        }

        self._nic_remove_chk.check(args)

        return self._client.json('bridge.nic-remove', args)


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

        args = {
            'name': bridge,
        }

        self._bridge_chk.check(args)

        return self._client.json('bridge.nic-list', args)

Response: List nics attached to a bridge


Instruction:
Can you tell what is the following Python 3 function doing
def list(self):
        response = self._client.raw('disk.list', {})

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to list disks: %s' % result.stderr)

        if result.level != 20:  # 20 is JSON output.
            raise RuntimeError('invalid response type from disk.list command')

        data = result.data.strip()
        if data:
            return json.loads(data)
        else:
            return {}

Response: List available block devices.


Instruction:
Given the following Python 3 function, write the documentation
def mktable(self, disk, table_type='gpt'):
        args = {
            'disk': disk,
            'table_type': table_type,
        }

        self._mktable_chk.check(args)

        response = self._client.raw('disk.mktable', args)

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to create table: %s' % result.stderr)

Response: Make partition table on block device.


Instruction:
Explain what the following Python 3 code does
def getinfo(self, disk, part=''):
        args = {
            "disk": disk,
            "part": part,
        }

        self._getpart_chk.check(args)

        response = self._client.raw('disk.getinfo', args)

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to get info: %s' % result.data)

        if result.level != 20:  # 20 is JSON output.
            raise RuntimeError('invalid response type from disk.getinfo command')

        data = result.data.strip()
        if data:
            return json.loads(data)
        else:
            return {}

Response: Get more info about a disk or a partition.


Instruction:
Make a summary of the following Python 3 code
def mkpart(self, disk, start, end, part_type='primary'):
        args = {
            'disk': disk,
            'start': start,
            'end': end,
            'part_type': part_type,
        }

        self._mkpart_chk.check(args)

        response = self._client.raw('disk.mkpart', args)

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to create partition: %s' % result.stderr)

Response: Make a partition on the specified disk.


Instruction:
Can you write a function in Python 3 where it
removes partion from disk

Response:
def rmpart(self, disk, number):
        """
        Remove partion from disk
        :param disk: device path (/dev/sda, /dev/sdb, etc...)
        :param number: Partition number (starting from 1)
        """
        args = {
            'disk': disk,
            'number': number,
        }

        self._rmpart_chk.check(args)

        response = self._client.raw('disk.rmpart', args)

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to remove partition: %s' % result.stderr)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def mount(self, source, target, options=[]):

        if len(options) == 0:
            options = ['']

        args = {
            'options': ','.join(options),
            'source': source,
            'target': target,
        }

        self._mount_chk.check(args)
        response = self._client.raw('disk.mount', args)

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to mount partition: %s' % result.stderr)

Response: Mount partion on target


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

        args = {
            'source': source,
        }
        self._umount_chk.check(args)

        response = self._client.raw('disk.umount', args)

        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to umount partition: %s' % result.stderr)

Response:
Unmount partion
        :param source: Full partition path like /dev/sda1


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

        self._seektime_chk.check(args)

        return self._client.json("disk.seektime", args)

Response: Returns seek latency on disk


Instruction:
Create a Python 3 function for
creating a btrfs filesystem with the given label devices and profiles.

Response:
def create(self, label, devices, metadata_profile="", data_profile="", overwrite=False):
        """
        Create a btrfs filesystem with the given label, devices, and profiles
        :param label: name/label
        :param devices : array of devices (/dev/sda1, etc...)
        :metadata_profile: raid0, raid1, raid5, raid6, raid10, dup or single
        :data_profile: same as metadata profile
        :overwrite: force creation of the filesystem. Overwrite any existing filesystem
        """
        args = {
            'label': label,
            'metadata': metadata_profile,
            'data': data_profile,
            'devices': devices,
            'overwrite': overwrite
        }

        self._create_chk.check(args)

        self._client.sync('btrfs.create', args)


Instruction:
Write a Python 3 function that can
add one or more devices to btrfs filesystem mounted under mountpoint.

Response:
def device_add(self, mountpoint, *device):
        """
        Add one or more devices to btrfs filesystem mounted under `mountpoint`

        :param mountpoint: mount point of the btrfs system
        :param devices: one ore more devices to add
        :return:
        """
        if len(device) == 0:
            return

        args = {
            'mountpoint': mountpoint,
            'devices': device,
        }

        self._device_chk.check(args)

        self._client.sync('btrfs.device_add', args)


Instruction:
Write a Python 3 function that can
create a btrfs subvolume in the specified path

Response:
def subvol_create(self, path):
        """
        Create a btrfs subvolume in the specified path
        :param path: path to create
        """
        args = {
            'path': path
        }
        self._subvol_chk.check(args)
        self._client.sync('btrfs.subvol_create', args)


Instruction:
Write a Python 3 function that can
delete a btrfs subvolume in the specified path

Response:
def subvol_delete(self, path):
        """
        Delete a btrfs subvolume in the specified path
        :param path: path to delete
        """
        args = {
            'path': path
        }

        self._subvol_chk.check(args)

        self._client.sync('btrfs.subvol_delete', args)


Instruction:
Can you generate the documentation for the following Python 3 function
def subvol_quota(self, path, limit):
        args = {
            'path': path,
            'limit': limit,
        }

        self._subvol_quota_chk.check(args)

        self._client.sync('btrfs.subvol_quota', args)

Response: Apply a quota to a btrfs subvolume in the specified path


Instruction:
Here you have a function in Python 3, explain what it does
def subvol_snapshot(self, source, destination, read_only=False):

        args = {
            "source": source,
            "destination": destination,
            "read_only": read_only,
        }

        self._subvol_snapshot_chk.check(args)
        self._client.sync('btrfs.subvol_snapshot', args)

Response: Take a snapshot of a subvolume


Instruction:
Make a summary of the following Python 3 code
def join(self, network):
        args = {'network': network}
        self._network_chk.check(args)
        response = self._client.raw('zerotier.join', args)
        result = response.get()

        if result.state != 'SUCCESS':
            raise RuntimeError('failed to join zerotier network: %s', result.stderr)

Response: Join a zerotier network


Instruction:
Write a Python 3 function for
creating a new kvm domain.

Response:
def create(self, name, media=None, flist=None, cpu=2, memory=512,
               nics=None, port=None, mount=None, tags=None, config=None):
        """
        :param name: Name of the kvm domain
        :param media: (optional) array of media objects to attach to the machine, where the first object is the boot device
                      each media object is a dict of {url, type} where type can be one of 'disk', or 'cdrom', or empty (default to disk)
                      example: [{'url': 'nbd+unix:///test?socket=/tmp/ndb.socket'}, {'type': 'cdrom', 'url: '/somefile.iso'}]
                      zdb exmpale:
                      [{'url': 'zdb://host:port?size=10G&blocksize=4096'},
                       {'url': 'zdb+unix:///path/to/unix.socket?size=5G'}]
        :param flist: (optional) VM flist. A special bootable flist witch has a correct boot.yaml file
                     example: http://hub.gig.tech/azmy/ubuntu-zesty.flist
        :param cpu: number of vcpu cores
        :param memory: memory in MiB
        :param port: A dict of host_port: container_port pairs
                       Example:
                        `port={8080: 80, 7000:7000}`
                     Only supported if default network is used
        :param nics: Configure the attached nics to the container
                     each nic object is a dict of the format
                     {
                        'type': nic_type # default, bridge, vlan, or vxlan (note, vlan and vxlan only supported by ovs)
                        'id': id # depends on the type, bridge name (bridge type) zerotier network id (zertier type), the vlan tag or the vxlan id
                     }
        :param port: Configure port forwards to vm, this only works if default network nic is added. Is a dict of {host-port: guest-port}
        :param mount: A list of host shared folders in the format {'source': '/host/path', 'target': '/guest/path', 'readonly': True|False}
        :param tags: A list of user defined tags (strings)
        :param config: a map with the config file path as a key and content as a value. This only works when creating a VM from an flist. The
                       config files are written to the machine before booting.
                       Example:
                       config = {'/root/.ssh/authorized_keys': '<PUBLIC KEYS>'}

                       If the machine is not booted from an flist, the config are discarded

        :note: At least one media or an flist must be provided.
        :return: uuid of the virtual machine
        """

        if nics is None:
            nics = []

        args = {
            'name': name,
            'media': media,
            'cpu': cpu,
            'flist': flist,
            'memory': memory,
            'nics': nics,
            'port': port,
            'mount': mount,
            'tags': tags,
            'config': config,
        }
        self._create_chk.check(args)

        if media is None and flist is None:
            raise ValueError('need at least one boot media via media or an flist')

        return self._client.json('kvm.create', args, tags=tags)


Instruction:
Write a Python 3 script to
prepare the migration target for the current kvm domain.

Response:
def prepare_migration_target(self, uuid, nics=None, port=None, tags=None):
        """
        :param name: Name of the kvm domain that will be migrated
        :param port: A dict of host_port: container_port pairs
                       Example:
                        `port={8080: 80, 7000:7000}`
                     Only supported if default network is used
        :param nics: Configure the attached nics to the container
                     each nic object is a dict of the format
                     {
                        'type': nic_type # default, bridge, vlan, or vxlan (note, vlan and vxlan only supported by ovs)
                        'id': id # depends on the type, bridge name (bridge type) zerotier network id (zertier type), the vlan tag or the vxlan id
                     }
        :param uuid: uuid of machine to be migrated on old node
        :return:
        """

        if nics is None:
            nics = []

        args = {
            'nics': nics,
            'port': port,
            'uuid': uuid
        }
        self._migrate_network_chk.check(args)

        self._client.sync('kvm.prepare_migration_target', args, tags=tags)


Instruction:
Make a summary of the following Python 3 code
def destroy(self, uuid):
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        self._client.sync('kvm.destroy', args)

Response: Destroy a kvm domain by uuid


Instruction:
Here you have a function in Python 3, explain what it does
def shutdown(self, uuid):
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        self._client.sync('kvm.shutdown', args)

Response: Shutdown a kvm domain by uuid


Instruction:
How would you implement a function in Python 3 that
resets a kvm domain by uuid

Response:
def reset(self, uuid):
        """
        Reset (Force reboot) a kvm domain by uuid
        :param uuid: uuid of the kvm container (same as the used in create)
        :return:
        """
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        self._client.sync('kvm.reset', args)


Instruction:
Write a Python 3 script for
pausing a kvm domain by uuid

Response:
def pause(self, uuid):
        """
        Pause a kvm domain by uuid
        :param uuid: uuid of the kvm container (same as the used in create)
        :return:
        """
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        self._client.sync('kvm.pause', args)


Instruction:
Write a Python 3 script to
resume a kvm domain by uuid

Response:
def resume(self, uuid):
        """
        Resume a kvm domain by uuid
        :param uuid: uuid of the kvm container (same as the used in create)
        :return:
        """
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        self._client.sync('kvm.resume', args)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def info(self, uuid):
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        return self._client.json('kvm.info', args)

Response: Get info about a kvm domain by uuid


Instruction:
Can you implement a function in Python 3 that
gets info per second about a kvm domain by uuid

Response:
def infops(self, uuid):
        """
        Get info per second about a kvm domain by uuid
        :param uuid: uuid of the kvm container (same as the used in create)
        :return:
        """
        args = {
            'uuid': uuid,
        }
        self._domain_action_chk.check(args)

        return self._client.json('kvm.infops', args)


Instruction:
How would you explain what the following Python 3 function does
def attach_disk(self, uuid, media):
        args = {
            'uuid': uuid,
            'media': media,
        }
        self._man_disk_action_chk.check(args)

        self._client.sync('kvm.attach_disk', args)

Response: Attach a disk to a kvm container


Instruction:
Explain what the following Python 3 code does
def add_nic(self, uuid, type, id=None, hwaddr=None):
        args = {
            'uuid': uuid,
            'type': type,
            'id': id,
            'hwaddr': hwaddr,
        }
        self._man_nic_action_chk.check(args)

        return self._client.json('kvm.add_nic', args)

Response: Add a nic to a kvm container


Instruction:
Make a summary of the following Python 3 code
def limit_disk_io(self, uuid, media, totalbytessecset=False, totalbytessec=0, readbytessecset=False, readbytessec=0, writebytessecset=False,
                      writebytessec=0, totaliopssecset=False, totaliopssec=0, readiopssecset=False, readiopssec=0, writeiopssecset=False, writeiopssec=0,
                      totalbytessecmaxset=False, totalbytessecmax=0, readbytessecmaxset=False, readbytessecmax=0, writebytessecmaxset=False, writebytessecmax=0,
                      totaliopssecmaxset=False, totaliopssecmax=0, readiopssecmaxset=False, readiopssecmax=0, writeiopssecmaxset=False, writeiopssecmax=0,
                      totalbytessecmaxlengthset=False, totalbytessecmaxlength=0, readbytessecmaxlengthset=False, readbytessecmaxlength=0,
                      writebytessecmaxlengthset=False, writebytessecmaxlength=0, totaliopssecmaxlengthset=False, totaliopssecmaxlength=0,
                      readiopssecmaxlengthset=False, readiopssecmaxlength=0, writeiopssecmaxlengthset=False, writeiopssecmaxlength=0, sizeiopssecset=False,
                      sizeiopssec=0, groupnameset=False, groupname=''):
        """
        Remove a nic from a machine
        :param uuid: uuid of the kvm container (same as the used in create)
        :param media: the media to limit the diskio
        :return:
        """
        args = {
            'uuid': uuid,
            'media': media,
            'totalbytessecset': totalbytessecset,
            'totalbytessec': totalbytessec,
            'readbytessecset': readbytessecset,
            'readbytessec': readbytessec,
            'writebytessecset': writebytessecset,
            'writebytessec': writebytessec,
            'totaliopssecset': totaliopssecset,
            'totaliopssec': totaliopssec,
            'readiopssecset': readiopssecset,
            'readiopssec': readiopssec,
            'writeiopssecset': writeiopssecset,
            'writeiopssec': writeiopssec,
            'totalbytessecmaxset': totalbytessecmaxset,
            'totalbytessecmax': totalbytessecmax,
            'readbytessecmaxset': readbytessecmaxset,
            'readbytessecmax': readbytessecmax,
            'writebytessecmaxset': writebytessecmaxset,
            'writebytessecmax': writebytessecmax,
            'totaliopssecmaxset': totaliopssecmaxset,
            'totaliopssecmax': totaliopssecmax,
            'readiopssecmaxset': readiopssecmaxset,
            'readiopssecmax': readiopssecmax,
            'writeiopssecmaxset': writeiopssecmaxset,
            'writeiopssecmax': writeiopssecmax,
            'totalbytessecmaxlengthset': totalbytessecmaxlengthset,
            'totalbytessecmaxlength': totalbytessecmaxlength,
            'readbytessecmaxlengthset': readbytessecmaxlengthset,
            'readbytessecmaxlength': readbytessecmaxlength,
            'writebytessecmaxlengthset': writebytessecmaxlengthset,
            'writebytessecmaxlength': writebytessecmaxlength,
            'totaliopssecmaxlengthset': totaliopssecmaxlengthset,
            'totaliopssecmaxlength': totaliopssecmaxlength,
            'readiopssecmaxlengthset': readiopssecmaxlengthset,
            'readiopssecmaxlength': readiopssecmaxlength,
            'writeiopssecmaxlengthset': writeiopssecmaxlengthset,
            'writeiopssecmaxlength': writeiopssecmaxlength,
            'sizeiopssecset': sizeiopssecset,
            'sizeiopssec': sizeiopssec,
            'groupnameset': groupnameset,
            'groupname': groupname,
        }
        self._limit_disk_io_action_chk.check(args)

        self._client.sync('kvm.limit_disk_io', args)

Response: Limit disk IO for a given kvm container.


Instruction:
Can you write a function in Python 3 where it
migrates a vm to another node

Response:
def migrate(self, uuid, desturi):
        """
        Migrate a vm to another node
        :param uuid: uuid of the kvm container (same as the used in create)
        :param desturi: the uri of the destination node
        :return:
        """
        args = {
            'uuid': uuid,
            'desturi': desturi,
        }
        self._migrate_action_chk.check(args)

        self._client.sync('kvm.migrate', args)


Instruction:
Can you tell what is the following Python 3 function doing
def get(self, uuid):
        args = {'uuid':uuid}
        self._get_chk.check(args)
        return self._client.json('kvm.get', args)

Response: Get a specific key - value pair for a given domain uuid


Instruction:
How would you explain what the following Python 3 function does
def set_level(self, level):
        args = {
            'level': level,
        }
        self._level_chk.check(args)

        return self._client.json('logger.set_level', args)

Response: Set the log level of the g8oscale log file


Instruction:
Can you generate the documentation for the following Python 3 function
def subscribe(self, queue=None, *levels):
        args = {
            'queue': queue,
            'levels': list(levels),
        }

        self._subscribe_chk.check(args)

        return self._client.json('logger.subscribe', args)

Response: Subscribe to the aggregated log stream.


Instruction:
Can you tell what is the following Python 3 function doing
def drop_port(self, port, interface=None, subnet=None):
        args = {
            'port': port,
            'interface': interface,
            'subnet': subnet,
        }
        self._port_chk.check(args)

        return self._client.json('nft.drop_port', args)

Response: Drop an opened port


Instruction:
Can you generate a brief explanation for the following Python 3 code
def query(self, key=None, **tags):
        args = {
            'key': key,
            'tags': tags,
        }
        self._query_chk.check(args)

        return self._client.json('aggregator.query', args)

Response: Query zero - os aggregator for current state object of monitored metrics.


Instruction:
Can you implement a function in Python 3 that
starts rtinfo - client

Response:
def start(self, host="localhost", port=8999, disks=None):
        """
        Start rtinfo-client
        :param host: str rtinfod host address
        :param port: int rtinfod client port
        :param disks: list of prefixes of wathable disks (e.g ["sd"])

        """
        disks = [] if disks is None else disks

        args = {
            "host": host,
            "port": port,
            "disks": disks
        }
        self._rtinfo_start_params_chk.check(args)

        return self._client.json("rtinfo.start", args)


Instruction:
Can you generate the documentation for the following Python 3 function
def stop(self, host, port):

        args = {
            "host": host,
            "port": port,
        }
        self._rtinfo_stop_params_chk.check(args)

        return self._client.json("rtinfo.stop", args)

Response: Stop rtinfo - client


Instruction:
How would you code a function in Python 3 to
create a cgroup if it doesn t exist under the specified subsystem and the given name.

Response:
def ensure(self, subsystem, name):
        """
        Creates a cgroup if it doesn't exist under the specified subsystem
        and the given name

        :param subsystem: the cgroup subsystem (currently support 'memory', and 'cpuset')
        :param name: name of the cgroup to delete
        """
        args = {
            'subsystem': subsystem,
            'name': name,
        }

        self._cgroup_chk.check(args)
        return self._client.json('cgroup.ensure', args)


Instruction:
Write a Python 3 script to
add a process to a cgroup

Response:
def task_add(self, subsystem, name, pid):
        """
        Add process (with pid) to a cgroup

        :param subsystem: the cgroup subsystem (currently support 'memory', and 'cpuset')
        :param name: name of the cgroup
        :param pid: PID to add
        """

        args = {
            'subsystem': subsystem,
            'name': name,
            'pid': pid,
        }

        self._task_chk.check(args)
        return self._client.json('cgroup.task-add', args)


Instruction:
How would you implement a function in Python 3 that
sets the memory cgroup specification and limits for both mem and swap.

Response:
def memory(self, name, mem=0, swap=0):
        """
        Set/Get memory cgroup specification/limitation
        the call to this method will always GET the current set values for both mem and swap.
        If mem is not zero, the memory will set the memory limit to the given value, and swap to the given value (even 0)

        :param mem: Set memory limit to the given value (in bytes), ignore if 0
        :param swap: Set swap limit to the given value (in bytes) (only if mem is not zero)

        :return: current memory limitation
        """

        args = {
            'name': name,
            'mem': mem,
            'swap': swap,
        }

        self._memory_spec.check(args)
        return self._client.json('cgroup.memory.spec', args)


Instruction:
Write a Python 3 script for
setting the cpuset cgroup specification and limitation

Response:
def cpuset(self, name, cpus=None, mems=None):
        """
        Set/Get cpuset cgroup specification/limitation
        the call to this method will always GET the current set values for both cpus and mems
        If cpus, or mems is NOT NONE value it will be set as the spec for that attribute

        :param cpus: Set cpus affinity limit to the given value (0, 1, 0-10, etc...)
        :param mems: Set mems affinity limit to the given value (0, 1, 0-10, etc...)

        :return: current cpuset
        """

        args = {
            'name': name,
            'cpus': cpus,
            'mems': mems,
        }

        self._cpuset_spec.check(args)
        return self._client.json('cgroup.cpuset.spec', args)


Instruction:
Write a Python 3 function for
implementing the low level command call, this needs to build the command structure and push it on the correct queue. :param command: Command name to execute supported by the node (ex: core.system, info.cpu, etc...) check documentation for list of built in commands :param arguments: A dict of required command arguments depends on the command name. :param queue: command queue (commands on the same queue are executed sequentially) :param max_time: kill job server side if it exceeded this amount of seconds :param stream: If True, process stdout and stderr are pushed to a special queue (stream:<id>) so client can stream output :param tags: job tags :param id: job id. Generated if not supplied :return: Response object

Response:
def raw(self, command, arguments, queue=None, max_time=None, stream=False, tags=None, id=None):
        """
        Implements the low level command call, this needs to build the command structure
        and push it on the correct queue.

        :param command: Command name to execute supported by the node (ex: core.system, info.cpu, etc...)
                        check documentation for list of built in commands
        :param arguments: A dict of required command arguments depends on the command name.
        :param queue: command queue (commands on the same queue are executed sequentially)
        :param max_time: kill job server side if it exceeded this amount of seconds
        :param stream: If True, process stdout and stderr are pushed to a special queue (stream:<id>) so
            client can stream output
        :param tags: job tags
        :param id: job id. Generated if not supplied
        :return: Response object
        """
        if not id:
            id = str(uuid.uuid4())

        payload = {
            'id': id,
            'command': command,
            'arguments': arguments,
            'queue': queue,
            'max_time': max_time,
            'stream': stream,
            'tags': tags,
        }

        self._raw_chk.check(payload)
        flag = 'result:{}:flag'.format(id)
        self._redis.rpush('core:default', json.dumps(payload))
        if self._redis.brpoplpush(flag, flag, DefaultTimeout) is None:
            TimeoutError('failed to queue job {}'.format(id))
        logger.debug('%s >> g8core.%s(%s)', id, command, ', '.join(("%s=%s" % (k, v) for k, v in arguments.items())))

        return Response(self, id)


Instruction:
Here you have a function in Python 3, explain what it does
def calczeta(phi1, phi2, theta1, theta2):

    zeta = 0.0

    if phi1 == phi2 and theta1 == theta2:
        zeta = 0.0
    else:
        argument = sin(theta1)*sin(theta2)*cos(phi1-phi2) + \
          cos(theta1)*cos(theta2)

        if argument < -1:
            zeta = np.pi
        elif argument > 1:
            zeta = 0.0
        else:
            zeta = acos(argument)

    return zeta

Response: Calculates the angular separation between two positions.


Instruction:
Write a Python 3 function that can
return value of d^l_mk as defined in allen ottewill 97.

Response:
def dlmk(l,m,k,theta1):
    """
    returns value of d^l_mk as defined in allen, ottewill 97.
    Called by Dlmk

    """

    if m >= k:

        factor = sqrt(factorial(l-k)*factorial(l+m)/factorial(l+k)/factorial(l-m))
        part2 = (cos(theta1/2))**(2*l+k-m)*(-sin(theta1/2))**(m-k)/factorial(m-k)
        part3 = sp.hyp2f1(m-l,-k-l,m-k+1,-(tan(theta1/2))**2)

        return factor*part2*part3

    else:

        return (-1)**(m-k) * dlmk(l,k,m,theta1)


Instruction:
Implement a function in Python 3 to
return D^l_mk for a given l m k and phi1 and phi2

Response:
def Dlmk(l,m,k,phi1,phi2,theta1,theta2):
    """
    returns value of D^l_mk as defined in allen, ottewill 97.

    """

    return exp(complex(0.,-m*phi1)) * dlmk(l,m,k,theta1) * \
      exp(complex(0.,-k*gamma(phi1,phi2,theta1,theta2)))


Instruction:
Implement a function in Python 3 to
calculate third rotation angle for a single object

Response:
def gamma(phi1,phi2,theta1,theta2):
    """
    calculate third rotation angle
    inputs are angles from 2 pulsars
    returns the angle.

    """

    if phi1 == phi2 and theta1 == theta2:
        gamma = 0
    else:
        gamma = atan( sin(theta2)*sin(phi2-phi1) / \
                      (cos(theta1)*sin(theta2)*cos(phi1-phi2) - \
                       sin(theta1)*cos(theta2)) )

    dummy_arg = (cos(gamma)*cos(theta1)*sin(theta2)*cos(phi1-phi2) + \
                 sin(gamma)*sin(theta2)*sin(phi2-phi1) - \
                 cos(gamma)*sin(theta1)*cos(theta2))

    if dummy_arg >= 0:
        return gamma
    else:
        return pi + gamma


Instruction:
Can you generate the documentation for the following Python 3 function
def rotated_Gamma_ml(m,l,phi1,phi2,theta1,theta2,gamma_ml):

    rotated_gamma = 0

    for ii in range(2*l+1):
        rotated_gamma += Dlmk(l,m,ii-l,phi1,phi2,theta1,theta2).conjugate()*gamma_ml[ii]

    return rotated_gamma

Response: This function rotates any gamma in the computational frame to the cosmic frame.


Instruction:
Write a Python 3 function that can
return the total chisq for the current timing solution.

Response:
def chisq(psr,formbats=False):
    """Return the total chisq for the current timing solution,
    removing noise-averaged mean residual, and ignoring deleted points."""

    if formbats:
        psr.formbats()

    res, err = psr.residuals(removemean=False)[psr.deleted == 0], psr.toaerrs[psr.deleted == 0]

    res -= numpy.sum(res/err**2) / numpy.sum(1/err**2)

    return numpy.sum(res * res / (1e-12 * err * err))


Instruction:
Create a Python 3 function for
returning gradient of total chisq for the current timing solution.

Response:
def dchisq(psr,formbats=False,renormalize=True):
    """Return gradient of total chisq for the current timing solution,
    after removing noise-averaged mean residual, and ignoring deleted points."""

    if formbats:
        psr.formbats()

    res, err = psr.residuals(removemean=False)[psr.deleted == 0], psr.toaerrs[psr.deleted == 0]

    res -= numpy.sum(res/err**2) / numpy.sum(1/err**2)

    # bats already updated by residuals(); skip constant-phase column
    M = psr.designmatrix(updatebats=False,fixunits=True,fixsigns=True)[psr.deleted==0,1:]

    # renormalize design-matrix columns
    if renormalize:
        norm = numpy.sqrt(numpy.sum(M**2,axis=0))
        M /= norm
    else:
        norm = 1.0

    # compute chisq derivative, de-renormalize
    dr = -2 * numpy.dot(M.T,res / (1e-12 * err**2)) * norm

    return dr


Instruction:
Can you implement a function in Python 3 that
uses scipy. optimize. minimize to find minimum - chisq timing solution.

Response:
def findmin(psr,method='Nelder-Mead',history=False,formbats=False,renormalize=True,bounds={},**kwargs):
    """Use scipy.optimize.minimize to find minimum-chisq timing solution,
    passing through all extra options. Resets psr[...].val to the final solution,
    and returns the final chisq. Will use chisq gradient if method requires it.
    Ignores deleted points."""

    ctr, err = psr.vals(), psr.errs()

    # to avoid losing precision, we're searching in units of parameter errors

    if numpy.any(err == 0.0):
        print("Warning: one or more fit parameters have zero a priori error, and won't be searched.")

    hloc, hval = [], []

    def func(xs):
        psr.vals([c + x*e for x,c,e in zip(xs,ctr,err)])

        ret = chisq(psr,formbats=formbats)

        if numpy.isnan(ret):
            print("Warning: chisq is nan at {0}.".format(psr.vals()))

        if history:
            hloc.append(psr.vals())
            hval.append(ret)

        return ret

    def dfunc(xs):
        psr.vals([c + x*e for x,c,e in zip(xs,ctr,err)])

        dc = dchisq(psr,formbats=formbats,renormalize=renormalize)
        ret = numpy.array([d*e for d,e in zip(dc,err)],'d')

        return ret

    opts = kwargs.copy()

    if method not in ['Nelder-Mead','Powell']:
        opts['jac'] = dfunc

    if method in ['L-BFGS-B']:
        opts['bounds'] = [(float((bounds[par][0] - ctr[i])/err[i]),
                           float((bounds[par][1] - ctr[i])/err[i])) if par in bounds else (None,None)
                          for i,par in enumerate(psr.pars())]

    res = scipy.optimize.minimize(func,[0.0]*len(ctr),method=method,**opts)

    if hasattr(res,'message'):
        print(res.message)

    # this will also set parameters to the minloc
    minchisq = func(res.x)

    if history:
        return minchisq, numpy.array(hval), numpy.array(hloc)
    else:
        return minchisq


Instruction:
Implement a function in Python 3 to
solve local GLS problem using scipy. linalg. cholesky. Update psr. val and psr. err from solution.

Response:
def glsfit(psr,renormalize=True):
    """Solve local GLS problem using scipy.linalg.cholesky.
    Update psr[...].val and psr[...].err from solution.
    If renormalize=True, normalize each design-matrix column by its norm."""

    mask = psr.deleted == 0
    res, err = psr.residuals(removemean=False)[mask], psr.toaerrs[mask]
    M = psr.designmatrix(updatebats=False,fixunits=True,fixsigns=True)[mask,:]

    C = numpy.diag((err * 1e-6)**2)

    if renormalize:
        norm = numpy.sqrt(numpy.sum(M**2,axis=0))
        M /= norm
    else:
        norm = np.ones_like(M[0,:])

    mtcm = numpy.dot(M.T,numpy.dot(numpy.linalg.inv(C),M))
    mtcy = numpy.dot(M.T,numpy.dot(numpy.linalg.inv(C),res))

    xvar = numpy.linalg.inv(mtcm)

    c = scipy.linalg.cho_factor(mtcm)
    xhat = scipy.linalg.cho_solve(c,mtcy)

    sol = psr.vals()
    psr.vals(sol + xhat[1:] / norm[1:])
    psr.errs(numpy.sqrt(numpy.diag(xvar)[1:]) / norm[1:])

    return chisq(psr)


Instruction:
Here you have a function in Python 3, explain what it does
def create_fourier_design_matrix(t, nmodes, freq=False, Tspan=None,
                                 logf=False, fmin=None, fmax=None):
    """
    Construct fourier design matrix from eq 11 of Lentati et al, 2013

    :param t: vector of time series in seconds
    :param nmodes: number of fourier coefficients to use
    :param freq: option to output frequencies
    :param Tspan: option to some other Tspan
    :param logf: use log frequency spacing
    :param fmin: lower sampling frequency
    :param fmax: upper sampling frequency

    :return: F: fourier design matrix
    :return: f: Sampling frequencies (if freq=True)
    """

    N = len(t)
    F = np.zeros((N, 2 * nmodes))

    if Tspan is not None:
        T = Tspan
    else:
        T = t.max() - t.min()

    # define sampling frequencies
    if fmin is not None and fmax is not None:
        f = np.linspace(fmin, fmax, nmodes)
    else:
        f = np.linspace(1 / T, nmodes / T, nmodes)
    if logf:
        f = np.logspace(np.log10(1 / T), np.log10(nmodes / T), nmodes)

    Ffreqs = np.zeros(2 * nmodes)
    Ffreqs[0::2] = f
    Ffreqs[1::2] = f

    F[:,::2] = np.sin(2*np.pi*t[:,None]*f[None,:])
    F[:,1::2] = np.cos(2*np.pi*t[:,None]*f[None,:])

    if freq:
        return F, Ffreqs
    else:
        return F

Response: Create a fourier design matrix from eq 11 of Lentati et al 2013 2013


Instruction:
Can you generate the documentation for the following Python 3 function
def powerlaw(f, log10_A=-16, gamma=5):

    fyr = 1 / 3.16e7
    return (10**log10_A)**2 / 12.0 / np.pi**2 * fyr**(gamma-3) * f**(-gamma)

Response: Power - law PSD.


Instruction:
Given the following Python 3 function, write the documentation
def add_gwb(psr, dist=1, ngw=1000, seed=None, flow=1e-8, fhigh=1e-5,
            gwAmp=1e-20, alpha=-0.66, logspacing=True):
    """Add a stochastic background from inspiraling binaries, using the tempo2
    code that underlies the GWbkgrd plugin.

    Here 'dist' is the pulsar distance [in kpc]; 'ngw' is the number of binaries,
    'seed' (a negative integer) reseeds the GWbkgrd pseudorandom-number-generator,
    'flow' and 'fhigh' [Hz] determine the background band, 'gwAmp' and 'alpha'
    determine its amplitude and exponent, and setting 'logspacing' to False
    will use linear spacing for the individual sources.

    It is also possible to create a background object with

    gwb = GWB(ngw,seed,flow,fhigh,gwAmp,alpha,logspacing)

    then call the method gwb.add_gwb(pulsar[i],dist) repeatedly to get a
    consistent background for multiple pulsars.

    Returns the GWB object
    """

    gwb = GWB(ngw,seed,flow,fhigh,gwAmp,alpha,logspacing)
    gwb.add_gwb(psr,dist)

    return gwb

Response: Add a stochastic background from inspiraling binaries using the GWbkgrd pseudorandom - number - generator.


Instruction:
Can you create a Python 3 function that
adds a stochastic background from inspiraling binaries distributed by a pure dipole distribution.

Response:
def add_dipole_gwb(psr, dist=1, ngw=1000, seed=None, flow=1e-8,
                   fhigh=1e-5, gwAmp=1e-20,  alpha=-0.66,
                   logspacing=True, dipoleamps=None,
                   dipoledir=None, dipolemag=None):

    """Add a stochastic background from inspiraling binaries distributed
    according to a pure dipole distribution, using the tempo2
    code that underlies the GWdipolebkgrd plugin.

    The basic use is identical to that of 'add_gwb':
    Here 'dist' is the pulsar distance [in kpc]; 'ngw' is the number of binaries,
    'seed' (a negative integer) reseeds the GWbkgrd pseudorandom-number-generator,
    'flow' and 'fhigh' [Hz] determine the background band, 'gwAmp' and 'alpha'
    determine its amplitude and exponent, and setting 'logspacing' to False
    will use linear spacing for the individual sources.

    Additionally, the dipole component can be specified by using one of two
    methods:
    1) Specify the dipole direction as three dipole amplitudes, in the vector
    dipoleamps
    2) Specify the direction of the dipole as a magnitude dipolemag, and a vector
    dipoledir=[dipolephi, dipoletheta]

    It is also possible to create a background object with

    gwb = GWB(ngw,seed,flow,fhigh,gwAmp,alpha,logspacing)

    then call the method gwb.add_gwb(pulsar[i],dist) repeatedly to get a
    consistent background for multiple pulsars.

    Returns the GWB object
    """

    gwb = GWB(ngw, seed, flow, fhigh, gwAmp, alpha, logspacing,
              dipoleamps, dipoledir, dipolemag)
    gwb.add_gwb(psr,dist)

    return gwb


Instruction:
How would you explain what the following Python 3 function does
def fakepulsar(parfile, obstimes, toaerr, freq=1440.0, observatory='AXIS',
               flags='', iters=3):
    """Returns a libstempo tempopulsar object corresponding to a noiseless set
    of observations for the pulsar specified in 'parfile', with observations
    happening at times (MJD) given in the array (or list) 'obstimes', with
    measurement errors given by toaerr (us).

    A new timfile can then be saved with pulsar.savetim(). Re the other parameters:
    - 'toaerr' needs to be either a common error, or a list of errors
       of the same length of 'obstimes';
    - 'freq' can be either a common observation frequency in MHz, or a list;
       it defaults to 1440;
    - 'observatory' can be either a common observatory name, or a list;
       it defaults to the IPTA MDC 'AXIS';
    - 'flags' can be a string (such as '-sys EFF.EBPP.1360') or a list of strings;
       it defaults to an empty string;
    - 'iters' is the number of iterative removals of computed residuals from TOAs
      (which is how the fake pulsar is made...)"""

    import tempfile
    outfile = tempfile.NamedTemporaryFile(delete=False)

    outfile.write(b'FORMAT 1\n')
    outfile.write(b'MODE 1\n')

    obsname = 'fake_' + os.path.basename(parfile)
    if obsname[-4:] == '.par':
        obsname = obsname[:-4]

    for i,t in enumerate(obstimes):
        outfile.write('{0} {1} {2} {3} {4} {5}\n'.format(
            obsname,_geti(freq,i),t,_geti(toaerr,i),_geti(observatory,i),_geti(flags,i)
            ).encode('ascii'))

    timfile = outfile.name
    outfile.close()

    pulsar = libstempo.tempopulsar(parfile,timfile,dofit=False)

    for i in range(iters):
        pulsar.stoas[:] -= pulsar.residuals() / 86400.0
        pulsar.formbats()

    os.remove(timfile)

    return pulsar

Response: Returns a libstempo tempopulsar object corresponding to a noiseless set of observations for the specified parfile.


Instruction:
Given the following Python 3 function, write the documentation
def add_efac(psr, efac=1.0, flagid=None, flags=None, seed=None):

    if seed is not None:
        N.random.seed(seed)

    # default efacvec
    efacvec = N.ones(psr.nobs)

    # check that efac is scalar if flags is None
    if flags is None:
        if not N.isscalar(efac):
            raise ValueError('ERROR: If flags is None, efac must be a scalar')
        else:
            efacvec = N.ones(psr.nobs) * efac

    if flags is not None and flagid is not None and not N.isscalar(efac):
        if len(efac) == len(flags):
            for ct, flag in enumerate(flags):
                ind = flag == N.array(psr.flagvals(flagid))
                efacvec[ind] = efac[ct]

    psr.stoas[:] += efacvec * psr.toaerrs * (1e-6 / day) * N.random.randn(psr.nobs)

Response: Add nominal TOA errors multiplied by efac factor.


Instruction:
Can you generate the documentation for the following Python 3 function
def add_equad(psr, equad, flagid=None, flags=None, seed=None):

    if seed is not None:
        N.random.seed(seed)

    # default equadvec
    equadvec = N.zeros(psr.nobs)

    # check that equad is scalar if flags is None
    if flags is None:
        if not N.isscalar(equad):
            raise ValueError('ERROR: If flags is None, equad must be a scalar')
        else:
            equadvec = N.ones(psr.nobs) * equad

    if flags is not None and flagid is not None and not N.isscalar(equad):
        if len(equad) == len(flags):
            for ct, flag in enumerate(flags):
                ind = flag == N.array(psr.flagvals(flagid))
                equadvec[ind] = equad[ct]

    psr.stoas[:] += (equadvec / day) * N.random.randn(psr.nobs)

Response: Add quadrature noise of rms equad [ s.


Instruction:
Can you write a function in Python 3 where it
adds correlated quadrature noise of rms ecorr of rms flagid and flags.

Response:
def add_jitter(psr, ecorr ,flagid=None, flags=None, coarsegrain=0.1,
               seed=None):
    """Add correlated quadrature noise of rms `ecorr` [s],
    with coarse-graining time `coarsegrain` [days].
    Optionally take a pseudorandom-number-generator seed."""

    if seed is not None:
        N.random.seed(seed)

    if flags is None:
        t, U = quantize_fast(N.array(psr.toas(),'d'), dt=coarsegrain)
    elif flags is not None and flagid is not None:
        t, f, U = quantize_fast(N.array(psr.toas(),'d'),
                                N.array(psr.flagvals(flagid)),
                                dt=coarsegrain)

    # default jitter value
    ecorrvec = N.zeros(len(t))

    # check that jitter is scalar if flags is None
    if flags is None:
        if not N.isscalar(ecorr):
            raise ValueError('ERROR: If flags is None, jitter must be a scalar')
        else:
            ecorrvec = N.ones(len(t)) * ecorr

    if flags is not None and flagid is not None and not N.isscalar(ecorr):
        if len(ecorr) == len(flags):
            for ct, flag in enumerate(flags):
                ind = flag == N.array(f)
                ecorrvec[ind] = ecorr[ct]

    psr.stoas[:] += (1 / day) * N.dot(U*ecorrvec, N.random.randn(U.shape[1]))


Instruction:
Explain what the following Python 3 code does
def add_rednoise(psr,A,gamma,components=10,seed=None):

    if seed is not None:
        N.random.seed(seed)

    t = psr.toas()
    minx, maxx = N.min(t), N.max(t)
    x = (t - minx) / (maxx - minx)
    T = (day/year) * (maxx - minx)

    size = 2*components
    F = N.zeros((psr.nobs,size),'d')
    f = N.zeros(size,'d')

    for i in range(components):
        F[:,2*i]   = N.cos(2*math.pi*(i+1)*x)
        F[:,2*i+1] = N.sin(2*math.pi*(i+1)*x)

        f[2*i] = f[2*i+1] = (i+1) / T

    norm = A**2 * year**2 / (12 * math.pi**2 * T)
    prior = norm * f**(-gamma)

    y = N.sqrt(prior) * N.random.randn(size)
    psr.stoas[:] += (1.0/day) * N.dot(F,y)

Response: Add red noise with P = A^ - gamma components = 10


Instruction:
Implement a Python 3 function for
adding a line of frequency f and amplitude A through the dataset.

Response:
def add_line(psr,f,A,offset=0.5):
    """
    Add a line of frequency `f` [Hz] and amplitude `A` [s],
    with origin at a fraction `offset` through the dataset.
    """

    t = psr.toas()
    t0 = offset * (N.max(t) - N.min(t))
    sine = A * N.cos(2 * math.pi * f * day * (t - t0))

    psr.stoas[:] += sine / day


Instruction:
Make a summary of the following Python 3 code
def add_glitch(psr, epoch, amp):

    # Define the heaviside function
    heaviside = lambda x: 0.5 * (N.sign(x) + 1)

    # Glitches are spontaneous spin-up events.
    # Thus TOAs will be advanced, and resiudals will be negative.

    psr.stoas[:] -= amp * heaviside(psr.toas() - epoch) * \
      (psr.toas() - epoch)*86400.0

Response: Adds a glitch to the internal list of the object that is at a given epoch.


Instruction:
Can you implement a function in Python 3 that
functions to create GW - induced residuals from an SMBMB

Response:
def add_cgw(psr, gwtheta, gwphi, mc, dist, fgw, phase0, psi, inc, pdist=1.0, \
                        pphase=None, psrTerm=True, evolve=True, \
                        phase_approx=False, tref=0):
    """
    Function to create GW-induced residuals from a SMBMB as
    defined in Ellis et. al 2012,2013. Tries to be smart about it...

    :param psr: pulsar object
    :param gwtheta: Polar angle of GW source in celestial coords [radians]
    :param gwphi: Azimuthal angle of GW source in celestial coords [radians]
    :param mc: Chirp mass of SMBMB [solar masses]
    :param dist: Luminosity distance to SMBMB [Mpc]
    :param fgw: Frequency of GW (twice the orbital frequency) [Hz]
    :param phase0: Initial Phase of GW source [radians]
    :param psi: Polarization of GW source [radians]
    :param inc: Inclination of GW source [radians]
    :param pdist: Pulsar distance to use other than those in psr [kpc]
    :param pphase: Use pulsar phase to determine distance [radian]
    :param psrTerm: Option to include pulsar term [boolean]
    :param evolve: Option to exclude evolution [boolean]
    :param tref: Fidicuial time at which initial parameters are referenced

    :returns: Vector of induced residuals
    """

    # convert units
    mc *= eu.SOLAR2S         # convert from solar masses to seconds
    dist *= eu.MPC2S    # convert from Mpc to seconds

    # define initial orbital frequency
    w0 = N.pi * fgw
    phase0 /= 2 # orbital phase
    w053 = w0**(-5/3)

    # define variable for later use
    cosgwtheta, cosgwphi = N.cos(gwtheta), N.cos(gwphi)
    singwtheta, singwphi = N.sin(gwtheta), N.sin(gwphi)
    sin2psi, cos2psi = N.sin(2*psi), N.cos(2*psi)
    incfac1, incfac2 = 0.5*(3+N.cos(2*inc)), 2*N.cos(inc)

    # unit vectors to GW source
    m = N.array([singwphi, -cosgwphi, 0.0])
    n = N.array([-cosgwtheta*cosgwphi, -cosgwtheta*singwphi, singwtheta])
    omhat = N.array([-singwtheta*cosgwphi, -singwtheta*singwphi, -cosgwtheta])

    # various factors invloving GW parameters
    fac1 = 256/5 * mc**(5/3) * w0**(8/3)
    fac2 = 1/32/mc**(5/3)
    fac3 = mc**(5/3)/dist

    # pulsar location
    if 'RAJ' and 'DECJ' in psr.pars():
        ptheta = N.pi/2 - psr['DECJ'].val
        pphi = psr['RAJ'].val
    elif 'ELONG' and 'ELAT' in psr.pars():
        fac = 180./N.pi
        coords = ephem.Equatorial(ephem.Ecliptic(str(psr['ELONG'].val*fac),
                                                 str(psr['ELAT'].val*fac)))

        ptheta = N.pi/2 - float(repr(coords.dec))
        pphi = float(repr(coords.ra))

    # use definition from Sesana et al 2010 and Ellis et al 2012
    phat = N.array([N.sin(ptheta)*N.cos(pphi), N.sin(ptheta)*N.sin(pphi),\
            N.cos(ptheta)])

    fplus = 0.5 * (N.dot(m, phat)**2 - N.dot(n, phat)**2) / (1+N.dot(omhat, phat))
    fcross = (N.dot(m, phat)*N.dot(n, phat)) / (1 + N.dot(omhat, phat))
    cosMu = -N.dot(omhat, phat)

    # get values from pulsar object
    toas = psr.toas()*86400 - tref
    if pphase is not None:
        pd = pphase/(2*N.pi*fgw*(1-cosMu)) / eu.KPC2S
    else:
        pd = pdist

    # convert units
    pd *= eu.KPC2S   # convert from kpc to seconds

    # get pulsar time
    tp = toas-pd*(1-cosMu)

    # evolution
    if evolve:

        # calculate time dependent frequency at earth and pulsar
        omega = w0 * (1 - fac1 * toas)**(-3/8)
        omega_p = w0 * (1 - fac1 * tp)**(-3/8)

        # calculate time dependent phase
        phase = phase0 + fac2 * (w053 - omega**(-5/3))
        phase_p = phase0 + fac2 * (w053 - omega_p**(-5/3))

    # use approximation that frequency does not evlolve over observation time
    elif phase_approx:

        # frequencies
        omega = w0
        omega_p = w0 * (1 + fac1 * pd*(1-cosMu))**(-3/8)

        # phases
        phase = phase0 + omega * toas
        phase_p = phase0 + fac2 * (w053 - omega_p**(-5/3)) + omega_p*toas

    # no evolution
    else:

        # monochromatic
        omega = w0
        omega_p = omega

        # phases
        phase = phase0 + omega * toas
        phase_p = phase0 + omega * tp

    # define time dependent coefficients
    At = N.sin(2*phase) * incfac1
    Bt = N.cos(2*phase) * incfac2
    At_p = N.sin(2*phase_p) * incfac1
    Bt_p = N.cos(2*phase_p) * incfac2

    # now define time dependent amplitudes
    alpha = fac3 / omega**(1/3)
    alpha_p = fac3 / omega_p**(1/3)

    # define rplus and rcross
    rplus = alpha * (At*cos2psi + Bt*sin2psi)
    rcross = alpha * (-At*sin2psi + Bt*cos2psi)
    rplus_p = alpha_p * (At_p*cos2psi + Bt_p*sin2psi)
    rcross_p = alpha_p * (-At_p*sin2psi + Bt_p*cos2psi)

    # residuals
    if psrTerm:
        res = fplus*(rplus_p-rplus)+fcross*(rcross_p-rcross)
    else:
        res = -fplus*rplus - fcross*rcross

    psr.stoas[:] += res/86400


Instruction:
Implement a Python 3 function for
adding an eccentric GW to the end of an existing GW file.

Response:
def add_ecc_cgw(psr, gwtheta, gwphi, mc, dist, F, inc, psi, gamma0,
                e0, l0, q, nmax=100, nset=None, pd=None, periEv=True,
                psrTerm=True, tref=0, check=True, useFile=True):
    """
    Simulate GW from eccentric SMBHB. Waveform models from
    Taylor et al. (2015) and Barack and Cutler (2004).

    WARNING: This residual waveform is only accurate if the
    GW frequency is not significantly evolving over the
    observation time of the pulsar.

    :param psr: pulsar object
    :param gwtheta: Polar angle of GW source in celestial coords [radians]
    :param gwphi: Azimuthal angle of GW source in celestial coords [radians]
    :param mc: Chirp mass of SMBMB [solar masses]
    :param dist: Luminosity distance to SMBMB [Mpc]
    :param F: Orbital frequency of SMBHB [Hz]
    :param inc: Inclination of GW source [radians]
    :param psi: Polarization of GW source [radians]
    :param gamma0: Initial angle of periastron [radians]
    :param e0: Initial eccentricity of SMBHB
    :param l0: Initial mean anomaly [radians]
    :param q: Mass ratio of SMBHB
    :param nmax: Number of harmonics to use in waveform decomposition
    :param nset: Fix the number of harmonics to be injected
    :param pd: Pulsar distance [kpc]
    :param periEv: Evolve the position of periapsis [boolean]
    :param psrTerm: Option to include pulsar term [boolean]
    :param tref: Fiducial time at which initial parameters are referenced [s]
    :param check: Check if frequency evolves significantly over obs. time
    :param useFile: Use pre-computed table of number of harmonics vs eccentricity

    :returns: Vector of induced residuals
    """

    # define variable for later use
    cosgwtheta, cosgwphi = N.cos(gwtheta), N.cos(gwphi)
    singwtheta, singwphi = N.sin(gwtheta), N.sin(gwphi)
    sin2psi, cos2psi = N.sin(2*psi), N.cos(2*psi)

    # unit vectors to GW source
    m = N.array([singwphi, -cosgwphi, 0.0])
    n = N.array([-cosgwtheta*cosgwphi, -cosgwtheta*singwphi, singwtheta])
    omhat = N.array([-singwtheta*cosgwphi, -singwtheta*singwphi, -cosgwtheta])

    # pulsar location
    if 'RAJ' and 'DECJ' in psr.pars():
        ptheta = N.pi/2 - psr['DECJ'].val
        pphi = psr['RAJ'].val
    elif 'ELONG' and 'ELAT' in psr.pars():
        fac = 180./N.pi
        coords = ephem.Equatorial(ephem.Ecliptic(str(psr['ELONG'].val*fac), str(psr['ELAT'].val*fac)))

        ptheta = N.pi/2 - float(repr(coords.dec))
        pphi = float(repr(coords.ra))

    # use definition from Sesana et al 2010 and Ellis et al 2012
    phat = N.array([N.sin(ptheta)*N.cos(pphi), N.sin(ptheta)*N.sin(pphi),\
            N.cos(ptheta)])

    fplus = 0.5 * (N.dot(m, phat)**2 - N.dot(n, phat)**2) / (1+N.dot(omhat, phat))
    fcross = (N.dot(m, phat)*N.dot(n, phat)) / (1 + N.dot(omhat, phat))
    cosMu = -N.dot(omhat, phat)

    # get values from pulsar object
    toas = N.double(psr.toas())*86400 - tref

    if check:
        # check that frequency is not evolving significantly over obs. time
        y = eu.solve_coupled_ecc_solution(F, e0, gamma0, l0, mc, q,
                                          N.array([0.0,toas.max()]))

        # initial and final values over observation time
        Fc0, ec0, gc0, phic0 = y[0,:]
        Fc1, ec1, gc1, phic1 = y[-1,:]

        # observation time
        Tobs = 1/(toas.max()-toas.min())

        if N.abs(Fc0-Fc1) > 1/Tobs:
            print('WARNING: Frequency is evolving over more than one frequency bin.')
            print('F0 = {0}, F1 = {1}, delta f = {2}'.format(Fc0, Fc1, 1/Tobs))

    # get gammadot for earth term
    if periEv==False:
        gammadot = 0.0
    else:
        gammadot = eu.get_gammadot(F, mc, q, e0)

    if nset is not None:
        nharm = nset
    elif useFile:
        if e0 > 0.001 and e0 < 0.999:
            nharm = min(int(ecc_interp(e0)), nmax) + 1
        elif e0 < 0.001:
            nharm = 3
        else:
            nharm = nmax
    else:
        nharm = nmax

    ##### earth term #####
    splus, scross = eu.calculate_splus_scross(nharm, mc, dist, F, e0, toas,
                                              l0, gamma0, gammadot, inc)

    ##### pulsar term #####
    if psrTerm:

        # convert units
        pd *= eu.KPC2S   # convert from kpc to seconds

        # get pulsar time
        tp = toas - pd * (1-cosMu)

        # solve coupled system of equations to get pulsar term values
        y = eu.solve_coupled_ecc_solution(F, e0, gamma0, l0, mc, q,
                                          N.array([0.0, tp.min()]))

        # get pulsar term values
        if N.any(y):
            Fp, ep, gp, lp = y[-1,:]

            # get gammadot at pulsar term
            gammadotp = eu.get_gammadot(Fp, mc, q, ep)

            if useFile:
                if ep > 0.001 and ep < 0.999:
                    nharm = min(int(ecc_interp(ep)), nmax)
                elif ep < 0.001:
                    nharm = 3
                else:
                    nharm = nmax
            else:
                nharm = nmax

            splusp, scrossp = eu.calculate_splus_scross(nharm, mc, dist, Fp,
                                                        ep, toas, lp, gp,
                                                        gammadotp, inc)

            rr = (fplus*cos2psi - fcross*sin2psi) * (splusp - splus) + \
                    (fplus*sin2psi + fcross*cos2psi) * (scrossp - scross)

        else:
            rr = N.zeros(len(p.toas))

    else:
        rr = - (fplus*cos2psi - fcross*sin2psi) * splus - \
                (fplus*sin2psi + fcross*cos2psi) * scross

    psr.stoas[:] += rr/86400


Instruction:
Can you implement a function in Python 3 that
functions to extend an interpolation function to an anonymization function to extrapolation anonymization function.

Response:
def extrap1d(interpolator):
    """
    Function to extend an interpolation function to an
    extrapolation function.

    :param interpolator: scipy interp1d object

    :returns ufunclike: extension of function to extrapolation
    """

    xs = interpolator.x
    ys = interpolator.y

    def pointwise(x):
        if x < xs[0]:
            return ys[0] # +(x-xs[0])*(ys[1]-ys[0])/(xs[1]-xs[0])
        elif x > xs[-1]:
            return ys[-1] # +(x-xs[-1])*(ys[-1]-ys[-2])/(xs[-1]-xs[-2])
        else:
            return interpolator(x)

    def ufunclike(xs):
        return N.array(map(pointwise, N.array(xs)))

    return ufunclike


Instruction:
Can you generate the documentation for the following Python 3 function
def createGWB(psr, Amp, gam, noCorr=False, seed=None, turnover=False,
              clm=[N.sqrt(4.0*N.pi)], lmax=0, f0=1e-9, beta=1,
              power=1, userSpec=None, npts=600, howml=10):
    """
    Function to create GW-induced residuals from a stochastic GWB as defined
    in Chamberlin, Creighton, Demorest, et al. (2014).

    :param psr: pulsar object for single pulsar
    :param Amp: Amplitude of red noise in GW units
    :param gam: Red noise power law spectral index
    :param noCorr: Add red noise with no spatial correlations
    :param seed: Random number seed
    :param turnover: Produce spectrum with turnover at frequency f0
    :param clm: coefficients of spherical harmonic decomposition of GW power
    :param lmax: maximum multipole of GW power decomposition
    :param f0: Frequency of spectrum turnover
    :param beta: Spectral index of power spectram for f << f0
    :param power: Fudge factor for flatness of spectrum turnover
    :param userSpec: User-supplied characteristic strain spectrum
                     (first column is freqs, second is spectrum)
    :param npts: Number of points used in interpolation
    :param howml: Lowest frequency is 1/(howml * T)

    :returns: list of residuals for each pulsar
    """

    if seed is not None:
        N.random.seed(seed)

    # number of pulsars
    Npulsars = len(psr)

    # gw start and end times for entire data set
    start = N.min([p.toas().min()*86400 for p in psr]) - 86400
    stop = N.max([p.toas().max()*86400 for p in psr]) + 86400

    # duration of the signal
    dur = stop - start

    # get maximum number of points
    if npts is None:
        # default to cadence of 2 weeks
        npts = dur/(86400*14)

    # make a vector of evenly sampled data points
    ut = N.linspace(start, stop, npts)

    # time resolution in days
    dt = dur/npts

    # compute the overlap reduction function
    if noCorr:
        ORF = N.diag(N.ones(Npulsars)*2)
    else:
        psrlocs = N.zeros((Npulsars,2))

        for ii in range(Npulsars):
            if 'RAJ' and 'DECJ' in psr[ii].pars():
                psrlocs[ii] = N.double(psr[ii]['RAJ'].val), N.double(psr[ii]['DECJ'].val)
            elif 'ELONG' and 'ELAT' in psr[ii].pars():
                fac = 180./N.pi
                # check for B name
                if 'B' in psr[ii].name:
                    epoch = '1950'
                else:
                    epoch = '2000'
                coords = ephem.Equatorial(ephem.Ecliptic(str(psr[ii]['ELONG'].val*fac),
                                                         str(psr[ii]['ELAT'].val*fac)),
                                                         epoch=epoch)
                psrlocs[ii] = float(repr(coords.ra)), float(repr(coords.dec))

        psrlocs[:,1] = N.pi/2. - psrlocs[:,1]
        anisbasis = N.array(anis.CorrBasis(psrlocs,lmax))
        ORF = sum(clm[kk]*anisbasis[kk] for kk in range(len(anisbasis)))
        ORF *= 2.0

    # Define frequencies spanning from DC to Nyquist.
    # This is a vector spanning these frequencies in increments of 1/(dur*howml).
    f = N.arange(0, 1/(2*dt), 1/(dur*howml))
    f[0] = f[1] # avoid divide by 0 warning
    Nf = len(f)

    # Use Cholesky transform to take 'square root' of ORF
    M = N.linalg.cholesky(ORF)

    # Create random frequency series from zero mean, unit variance, Gaussian distributions
    w = N.zeros((Npulsars, Nf), complex)
    for ll in range(Npulsars):
        w[ll,:] = N.random.randn(Nf) + 1j*N.random.randn(Nf)

    # strain amplitude
    if userSpec is None:

        f1yr = 1/3.16e7
        alpha = -0.5 * (gam-3)
        hcf = Amp * (f/f1yr)**(alpha)
        if turnover:
            si = alpha - beta
            hcf /= (1+(f/f0)**(power*si))**(1/power)

    elif userSpec is not None:

        freqs = userSpec[:,0]
        if len(userSpec[:,0]) != len(freqs):
            raise ValueError("Number of supplied spectral points does not match number of frequencies!")
        else:
            fspec_in = interp.interp1d(N.log10(freqs), N.log10(userSpec[:,1]), kind='linear')
            fspec_ex = extrap1d(fspec_in)
            hcf = 10.0**fspec_ex(N.log10(f))

    C = 1 / 96 / N.pi**2 * hcf**2 / f**3 * dur * howml

    ### injection residuals in the frequency domain
    Res_f = N.dot(M, w)
    for ll in range(Npulsars):
        Res_f[ll] = Res_f[ll] * C**(0.5)    # rescale by frequency dependent factor
        Res_f[ll,0] = 0			    # set DC bin to zero to avoid infinities
        Res_f[ll,-1] = 0		    # set Nyquist bin to zero also

    # Now fill in bins after Nyquist (for fft data packing) and take inverse FT
    Res_f2 = N.zeros((Npulsars, 2*Nf-2), complex)
    Res_t = N.zeros((Npulsars, 2*Nf-2))
    Res_f2[:,0:Nf] = Res_f[:,0:Nf]
    Res_f2[:, Nf:(2*Nf-2)] = N.conj(Res_f[:,(Nf-2):0:-1])
    Res_t = N.real(N.fft.ifft(Res_f2)/dt)

    # shorten data and interpolate onto TOAs
    Res = N.zeros((Npulsars, npts))
    res_gw = []
    for ll in range(Npulsars):
        Res[ll,:] = Res_t[ll, 10:(npts+10)]
        f = interp.interp1d(ut, Res[ll,:], kind='linear')
        res_gw.append(f(psr[ll].toas()*86400))

    #return res_gw
    ct = 0
    for p in psr:
        p.stoas[:] += res_gw[ct]/86400.0
        ct += 1

Response: Function to create GW - induced residuals from a single pulsar object with red noise and power law.


Instruction:
Can you generate the documentation for the following Python 3 function
def computeORFMatrix(psr):

    # begin loop over all pulsar pairs and calculate ORF
    npsr = len(psr)
    ORF = N.zeros((npsr, npsr))
    phati = N.zeros(3)
    phatj = N.zeros(3)
    ptheta = [N.pi/2 - p['DECJ'].val for p in psr]
    pphi = [p['RAJ'].val for p in psr]
    for ll in range(0, npsr):
        phati[0] = N.cos(pphi[ll]) * N.sin(ptheta[ll])
        phati[1] = N.sin(pphi[ll]) * N.sin(ptheta[ll])
        phati[2] = N.cos(ptheta[ll])

        for kk in range(0, npsr):
            phatj[0] = N.cos(pphi[kk]) * N.sin(ptheta[kk])
            phatj[1] = N.sin(pphi[kk]) * N.sin(ptheta[kk])
            phatj[2] = N.cos(ptheta[kk])

            if ll != kk:
                xip = (1.-N.sum(phati*phatj)) / 2.
                ORF[ll, kk] = 3.*( 1./3. + xip * ( N.log(xip) -1./6.) )
            else:
                ORF[ll, kk] = 2.0

    return ORF

Response: Compute the ORF matrix for the given list of pulsar objects.


Instruction:
Create a Python 3 function to
plot residuals compute unweighted rms residual.

Response:
def plotres(psr,deleted=False,group=None,**kwargs):
    """Plot residuals, compute unweighted rms residual."""

    res, t, errs = psr.residuals(), psr.toas(), psr.toaerrs

    if (not deleted) and N.any(psr.deleted != 0):
        res, t, errs = res[psr.deleted == 0], t[psr.deleted == 0], errs[psr.deleted == 0]
        print("Plotting {0}/{1} nondeleted points.".format(len(res),psr.nobs))

    meanres = math.sqrt(N.mean(res**2)) / 1e-6

    if group is None:
        i = N.argsort(t)
        P.errorbar(t[i],res[i]/1e-6,yerr=errs[i],fmt='x',**kwargs)
    else:
        if (not deleted) and N.any(psr.deleted):
            flagmask = psr.flagvals(group)[~psr.deleted]
        else:
            flagmask = psr.flagvals(group)

        unique = list(set(flagmask))

        for flagval in unique:
            f = (flagmask == flagval)
            flagres, flagt, flagerrs = res[f], t[f], errs[f]
            i = N.argsort(flagt)
            P.errorbar(flagt[i],flagres[i]/1e-6,yerr=flagerrs[i],fmt='x',**kwargs)

        P.legend(unique,numpoints=1,bbox_to_anchor=(1.1,1.1))

    P.xlabel('MJD'); P.ylabel('res [us]')
    P.title("{0} - rms res = {1:.2f} us".format(psr.name,meanres))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def plotgwsrc(gwb):
    theta, phi, omega, polarization = gwb.gw_dist()

    rho = phi-N.pi
    eta = 0.5*N.pi - theta

    # I don't know how to get rid of the RuntimeWarning -- RvH, Oct 10, 2014:
    #     /Users/vhaaster/env/dev/lib/python2.7/site-packages/matplotlib/projections/geo.py:485:
    #     RuntimeWarning: invalid value encountered in arcsin theta = np.arcsin(y / np.sqrt(2))
    #old_settings = N.seterr(invalid='ignore')

    P.title("GWB source population")
    ax = P.axes(projection='mollweide')

    foo = P.scatter(rho, eta, marker='.', s=1)
    #bar = N.seterr(**old_settings)

    return foo

Response: Plot a GWB source population as a mollweide projection.


Instruction:
How would you explain what the following Python 3 function does
def loglike(pulsar,efac=1.0,equad=None,jitter=None,Ared=None,gammared=None,marginalize=True,normalize=True,redcomponents=10,usedeleted=True):

    mask = Mask(pulsar,usedeleted)

    err = 1.0e-6 * mask(pulsar.toaerrs)
    Cdiag = (efac*err)**2

    if equad:
        Cdiag = Cdiag + (1e-6*equad)**2 * N.ones(len(err))

    if Ared:
        redf, F = _setuprednoise(pulsar,redcomponents)
        F = mask(F)
        phi = Ared**2 * redf**(-gammared)

    if jitter:
        # quantize at 1 second; U plays the role of redF
        t, U = _quantize(86400.0 * mask(pulsar.toas()),1.0)
        phi_j = (1e-6*jitter)**2 * N.ones(U.shape[1])

        # stack the basis arrays if we're also doing red noise
        phi = N.hstack((phi,phi_j)) if Ared else phi_j
        F   = N.hstack((F,U))       if Ared else U

    if Ared or jitter:
        # Lentati formulation for correlated noise
        invphi = N.diag(1/phi)
        Ninv = N.diag(1/Cdiag)
        NinvF = dot(Ninv,F)          # could be accelerated
        X = invphi + dot(F.T,NinvF)  # invphi + FTNinvF

        Cinv = Ninv - dot(NinvF,N.linalg.inv(X),NinvF.T)
        logCdet = N.sum(N.log(Cdiag)) + N.sum(N.log(phi)) + N.linalg.slogdet(X)[1] # check
    else:
        # noise is all diagonal
        Cinv = N.diag(1/Cdiag)
        logCdet = N.sum(N.log(Cdiag))

    if marginalize:
        M = mask(pulsar.designmatrix())
        res = mask(N.array(pulsar.residuals(updatebats=False),'d'))

        CinvM = N.dot(Cinv,M)
        A = dot(M.T,CinvM)

        invA = N.linalg.inv(A)
        CinvMres = dot(res,CinvM)

        ret = -0.5 * dot(res,Cinv,res) + 0.5 * dot(CinvMres,invA,CinvMres.T)

        if normalize:
            ret = ret - 0.5 * logCdet - 0.5 * N.linalg.slogdet(A)[1] - 0.5 * (M.shape[0] - M.shape[1]) * math.log(2.0*math.pi)
    else:
        res = mask(N.array(pulsar.residuals(),'d'))

        ret = -0.5 * dot(res,Cinv,res)

        if normalize:
            ret = ret - 0.5 * logCdet - 0.5 * len(res) * math.log(2.0*math.pi)

    return ret

Response: Returns the log - likelihood for the given pulsar parameters.


Instruction:
Given the following Python 3 function, write the documentation
def expandranges(parlist):

    ret = []

    for par in parlist:
        # match anything of the form XXX{number1-number2}
        m = re.match('(.*)\{([0-9]+)\-([0-9]+)\}',par)

        if m is None:
            ret.append(par)
        else:
            # (these are strings)
            root, number1, number2 = m.group(1), m.group(2), m.group(3)

            # if number1 begins with 0s, number parameters as 00, 01, 02, ...,
            # otherwise go with 0, 1, 2, ...
            fmt = '{{0}}{{1:0{0}d}}'.format(len(number1)) if number1[0] == '0' else '{0}{1:d}'

            ret = ret + [fmt.format(root,i) for i in range(int(m.group(2)),int(m.group(3))+1)]

    return ret

Response:
Rewrite a list of parameters by expanding ranges ( e. g. log10_efac1 - 10 into
    individual parameters.


Instruction:
Can you tell what is the following Python 3 function doing
def _findrange(parlist,roots=['JUMP','DMXR1_','DMXR2_','DMX_','efac','log10_efac']):

    rootdict = {root: [] for root in roots}

    res = []
    for par in parlist:
        found = False
        for root in roots:
            if len(par) > len(root) and par[:len(root)] == root:
                rootdict[root].append(int(par[len(root):]))
                found = True
        if not found:
            res.append(par)

    for root in roots:
        if rootdict[root]:
            if len(rootdict[root]) > 1:
                rmin, rmax = min(rootdict[root]), max(rootdict[root])
                res.append('{0}{{{1}-{2}}}{3}'.format(root,rmin,rmax,
                                                  '(incomplete)' if rmax - rmin != len(rootdict[root]) - 1 else ''))
            else:
                res.append('{0}{1}'.format(root,rootdict[root][0]))
    return res

Response: Rewrite a list of parameters name by detecting ranges ( e. g. JUMP1 JUMP2... and log10_efac.


Instruction:
How would you implement a function in Python 3 that
merges one every skip clouds into a single emcee population.

Response:
def merge(data,skip=50,fraction=1.0):
    """Merge one every 'skip' clouds into a single emcee population,
    using the later 'fraction' of the run."""

    w,s,d = data.chains.shape

    start = int((1.0 - fraction) * s)
    total = int((s - start) / skip)

    return data.chains[:,start::skip,:].reshape((w*total,d))


Instruction:
Implement a Python 3 function for
sieving an emcee clouds by excluding walkers with search variable index smaller than min or larger than max.

Response:
def cull(data,index,min=None,max=None):
    """Sieve an emcee clouds by excluding walkers with search variable 'index'
    smaller than 'min' or larger than 'max'."""

    ret = data

    if min is not None:
        ret = ret[ret[:,index] > min,:]

    if max is not None:
        ret = ret[ret[:,index] < max,:]

    return ret


Instruction:
How would you implement a function in Python 3 that
makes interpolation function from eccentricity file to determine number of harmonics to use for a given eccentricity.

Response:
def make_ecc_interpolant():
    """
    Make interpolation function from eccentricity file to
    determine number of harmonics to use for a given
    eccentricity.

    :returns: interpolant
    """
    pth = resource_filename(Requirement.parse('libstempo'),
                            'libstempo/ecc_vs_nharm.txt')

    fil = np.loadtxt(pth)

    return interp1d(fil[:,0], fil[:,1])


Instruction:
Can you write a function in Python 3 where it
computes eccentricity derivative from Taylor et al. 2015

Response:
def get_edot(F, mc, e):
    """
    Compute eccentricity derivative from Taylor et al. (2015)

    :param F: Orbital frequency [Hz]
    :param mc: Chirp mass of binary [Solar Mass]
    :param e: Eccentricity of binary

    :returns: de/dt

    """

    # chirp mass
    mc *= SOLAR2S

    dedt = -304/(15*mc) * (2*np.pi*mc*F)**(8/3) * e * \
        (1 + 121/304*e**2) / ((1-e**2)**(5/2))

    return dedt


Instruction:
Write a Python 3 script to
compute frequency derivative from Taylor et al. 2015

Response:
def get_Fdot(F, mc, e):
    """
    Compute frequency derivative from Taylor et al. (2015)

    :param F: Orbital frequency [Hz]
    :param mc: Chirp mass of binary [Solar Mass]
    :param e: Eccentricity of binary

    :returns: dF/dt

    """

    # chirp mass
    mc *= SOLAR2S

    dFdt = 48 / (5*np.pi*mc**2) * (2*np.pi*mc*F)**(11/3) * \
        (1 + 73/24*e**2 + 37/96*e**4) / ((1-e**2)**(7/2))

    return dFdt


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_gammadot(F, mc, q, e):

    # chirp mass
    mc *= SOLAR2S

    #total mass
    m = (((1+q)**2)/q)**(3/5) * mc

    dgdt = 6*np.pi*F * (2*np.pi*F*m)**(2/3) / (1-e**2) * \
        (1 + 0.25*(2*np.pi*F*m)**(2/3)/(1-e**2)*(26-15*e**2))

    return dgdt

Response: Compute gamma dot from Barack and Cutler 2004


Instruction:
Here you have a function in Python 3, explain what it does
def get_coupled_ecc_eqns(y, t, mc, q):

    F = y[0]
    e = y[1]
    gamma = y[2]
    phase = y[3]

    #total mass
    m = (((1+q)**2)/q)**(3/5) * mc

    dFdt = get_Fdot(F, mc, e)
    dedt = get_edot(F, mc, e)
    dgdt = get_gammadot(F, mc, q, e)
    dphasedt = 2*np.pi*F

    return np.array([dFdt, dedt, dgdt, dphasedt])

Response: This function computes the coupled differential and non - differential equations from Peters and Barack & Cutler.


Instruction:
Implement a Python 3 function for
solving the coupled ECC problem at a given time.

Response:
def solve_coupled_ecc_solution(F0, e0, gamma0, phase0, mc, q, t):
    """
    Compute the solution to the coupled system of equations
    from from Peters (1964) and Barack & Cutler (2004) at
    a given time.

    :param F0: Initial orbital frequency [Hz]
    :param e0: Initial orbital eccentricity
    :param gamma0: Initial angle of precession of periastron [rad]
    :param mc: Chirp mass of binary [Solar Mass]
    :param q: Mass ratio of binary
    :param t: Time at which to evaluate solution [s]

    :returns: (F(t), e(t), gamma(t), phase(t))

    """

    y0 = np.array([F0, e0, gamma0, phase0])

    y, infodict = odeint(get_coupled_ecc_eqns, y0, t, args=(mc,q), full_output=True)

    if infodict['message'] == 'Integration successful.':
        ret = y
    else:
        ret = 0

    return ret


Instruction:
How would you explain what the following Python 3 function does
def get_an(n, mc, dl, F, e):

    # convert to seconds
    mc *= SOLAR2S
    dl *= MPC2S

    omega = 2 * np.pi * F

    amp = n * mc**(5/3) * omega**(2/3) / dl

    ret = -amp * (ss.jn(n-2,n*e) - 2*e*ss.jn(n-1,n*e) +
                  (2/n)*ss.jn(n,n*e) + 2*e*ss.jn(n+1,n*e) -
                  ss.jn(n+2,n*e))

    return ret

Response: Compute a_n from Eq. 22 of Taylor et al. 2015.


Instruction:
Can you create a Python 3 function that
computes b_n from Eq. 22 of Taylor et al. 2015.

Response:
def get_bn(n, mc, dl, F, e):
    """
    Compute b_n from Eq. 22 of Taylor et al. (2015).

    :param n: Harmonic number
    :param mc: Chirp mass of binary [Solar Mass]
    :param dl: Luminosity distance [Mpc]
    :param F: Orbital frequency of binary [Hz]
    :param e: Orbital Eccentricity

    :returns: b_n

    """

    # convert to seconds
    mc *= SOLAR2S
    dl *= MPC2S

    omega = 2 * np.pi * F

    amp = n * mc**(5/3) * omega**(2/3) / dl

    ret = -amp * np.sqrt(1-e**2) *(ss.jn(n-2,n*e) - 2*ss.jn(n,n*e) +
                  ss.jn(n+2,n*e))

    return ret


Instruction:
Given the following Python 3 function, write the documentation
def get_cn(n, mc, dl, F, e):

    # convert to seconds
    mc *= SOLAR2S
    dl *= MPC2S

    omega = 2 * np.pi * F

    amp = 2 * mc**(5/3) * omega**(2/3) / dl

    ret = amp * ss.jn(n,n*e) / (n * omega)

    return ret

Response: Compute c_n from Eq. 22 of Taylor et al. 2015.


Instruction:
How would you explain what the following Python 3 function does
def calculate_splus_scross(nmax, mc, dl, F, e, t, l0, gamma, gammadot, inc):

    n = np.arange(1, nmax)

    # time dependent amplitudes
    an = get_an(n, mc, dl, F, e)
    bn = get_bn(n, mc, dl, F, e)
    cn = get_cn(n, mc, dl, F, e)

    # time dependent terms
    omega = 2*np.pi*F
    gt = gamma + gammadot * t
    lt = l0 + omega * t

    # tiled phase
    phase1 = n * np.tile(lt, (nmax-1,1)).T
    phase2 = np.tile(gt, (nmax-1,1)).T
    phasep = phase1 + 2*phase2
    phasem = phase1 - 2*phase2

    # intermediate terms
    sp = np.sin(phasem)/(n*omega-2*gammadot) + \
            np.sin(phasep)/(n*omega+2*gammadot)
    sm = np.sin(phasem)/(n*omega-2*gammadot) - \
            np.sin(phasep)/(n*omega+2*gammadot)
    cp = np.cos(phasem)/(n*omega-2*gammadot) + \
            np.cos(phasep)/(n*omega+2*gammadot)
    cm = np.cos(phasem)/(n*omega-2*gammadot) - \
            np.cos(phasep)/(n*omega+2*gammadot)

    splus_n = -0.5 * (1+np.cos(inc)**2) * (an*sp - bn*sm) + \
            (1-np.cos(inc)**2)*cn * np.sin(phase1)
    scross_n = np.cos(inc) * (an*cm - bn*cp)

    return np.sum(splus_n, axis=1), np.sum(scross_n, axis=1)

Response: Calculates the splus and scross summed over all harmonics.


Instruction:
Here you have a function in Python 3, explain what it does
def run(LogLikelihood,
    Prior,
    n_dims,
    n_params = None,
    n_clustering_params = None, wrapped_params = None,
    importance_nested_sampling = True,
    multimodal = True, const_efficiency_mode = False, n_live_points = 400,
    evidence_tolerance = 0.5, sampling_efficiency = 0.8,
    n_iter_before_update = 100, null_log_evidence = -1e90,
    max_modes = 100, mode_tolerance = -1e90,
    outputfiles_basename = "./multinest-", seed = -1, verbose = False,
    resume = True, context = None, write_output = True, log_zero = -1e100,
    max_iter = 0, init_MPI = True, dump_callback = None):
    """
    Runs MultiNest

    The most important parameters are the two log-probability functions Prior
    and LogLikelihood. They are called by MultiNest.

    Prior should transform the unit cube into the parameter cube. Here
    is an example for a uniform prior::

        def Prior(cube, ndim, nparams):
            for i in range(ndim):
                cube[i] = cube[i] * 10 * math.pi

    The LogLikelihood function gets this parameter cube and should
    return the logarithm of the likelihood.
    Here is the example for the eggbox problem::

        def Loglike(cube, ndim, nparams):
            chi = 1.

            for i in range(ndim):
                chi *= math.cos(cube[i] / 2.)
            return math.pow(2. + chi, 5)

    Some of the parameters are explained below. Otherwise consult the
    MultiNest documentation.

    @param importance_nested_sampling:
        If True, Multinest will use Importance Nested Sampling (INS). Read http://arxiv.org/abs/1306.2144
        for more details on INS. Please read the MultiNest README file before using the INS in MultiNest v3.0.

    @param n_params:
        Total no. of parameters, should be equal to ndims in most cases
        but if you need to store some additional
        parameters with the actual parameters then you need to pass
        them through the likelihood routine.

    @param sampling_efficiency:
        defines the sampling efficiency. 0.8 and 0.3 are recommended
        for parameter estimation & evidence evalutation
        respectively.
        use 'parameter' or 'model' to select the respective default
        values

    @param mode_tolerance:
        MultiNest can find multiple modes & also specify which samples belong to which mode. It might be
        desirable to have separate samples & mode statistics for modes with local log-evidence value greater than a
        particular value in which case Ztol should be set to that value. If there isn't any particularly interesting
        Ztol value, then Ztol should be set to a very large negative number (e.g. -1e90).

    @param evidence_tolerance:
        A value of 0.5 should give good enough accuracy.

    @param n_clustering_params:
        If mmodal is T, MultiNest will attempt to separate out the
        modes. Mode separation is done through a clustering
        algorithm. Mode separation can be done on all the parameters
        (in which case nCdims should be set to ndims) & it
        can also be done on a subset of parameters (in which case
        nCdims < ndims) which might be advantageous as
        clustering is less accurate as the dimensionality increases.
        If nCdims < ndims then mode separation is done on
        the first nCdims parameters.

    @param null_log_evidence:
        If mmodal is T, MultiNest can find multiple modes & also specify
        which samples belong to which mode. It might be
        desirable to have separate samples & mode statistics for modes
        with local log-evidence value greater than a
        particular value in which case nullZ should be set to that
        value. If there isn't any particulrly interesting
        nullZ value, then nullZ should be set to a very large negative
        number (e.g. -1.d90).

    @param init_MPI:
        initialize MPI routines?, relevant only if compiling with MPI

    @param log_zero:
        points with loglike < logZero will be ignored by MultiNest

    @param max_iter:
        maximum number of iterations. 0 is unlimited.

    @param write_output:
        write output files? This is required for analysis.

    @param dump_callback:
        a callback function for dumping the current status

    """

    if n_params == None:
        n_params = n_dims
    if n_clustering_params == None:
        n_clustering_params = n_dims
    if wrapped_params == None:
        wrapped_params = [0] * n_dims

    WrappedType = c_int * len(wrapped_params)
    wraps = WrappedType(*wrapped_params)

    if sampling_efficiency == 'parameter':
        sampling_efficiency = 0.8
    if sampling_efficiency == 'model':
        sampling_efficiency = 0.3

    # MV 20130923

    loglike_type = CFUNCTYPE(c_double,
                             POINTER(c_double),c_int,c_int,c_void_p)

    dumper_type  = CFUNCTYPE(c_void_p,
                             c_int,c_int,c_int,
                             POINTER(c_double),POINTER(c_double),POINTER(c_double),
                             c_double,c_double,c_double,c_void_p)

    if hasattr(LogLikelihood,'loglike') and hasattr(Prior,'remap') and hasattr(Prior,'prior'):
        def loglike(cube,ndim,nparams,nullcontext):
            # we're not using context with libstempo.like objects

            pprior = Prior.premap(cube)

            # mappers are supposed to throw a ValueError if they get out of range
            try:
                pars = Prior.remap(cube)
            except ValueError:
                return -N.inf

            prior = pprior * Prior.prior(pars)

            return -N.inf if not prior else math.log(prior) + LogLikelihood.loglike(pars)
    else:
        def loglike(cube,ndim,nparams,nullcontext):
            # it's actually easier to use the context, if any, at the Python level
            # and pass a null pointer to MultiNest...

            args = [cube,ndim,nparams] + ([] if context is None else context)

            if Prior:
                Prior(*args)

            return LogLikelihood(*args)

    def dumper(nSamples,nlive,nPar,
               physLive,posterior,paramConstr,
               maxLogLike,logZ,logZerr,nullcontext):

        if dump_callback:
            # It's not clear to me what the desired PyMultiNest dumper callback
            # syntax is... but this should pass back the right numpy arrays,
            # without copies. Untested!
            pc =  as_array(paramConstr,shape=(nPar,4))

            dump_callback(nSamples,nlive,nPar,
                          as_array(physLive,shape=(nPar+1,nlive)).T,
                          as_array(posterior,shape=(nPar+2,nSamples)).T,
                          (pc[0,:],pc[1,:],pc[2,:],pc[3,:]),    # (mean,std,bestfit,map)
                          maxLogLike,logZ,logZerr)

    # MV 20130923: currently we support only multinest 3.2 (24 parameters),
    # but it would not be a problem to build up the parameter list dynamically

    lib.run(c_bool(importance_nested_sampling),c_bool(multimodal),c_bool(const_efficiency_mode),
            c_int(n_live_points),c_double(evidence_tolerance),
            c_double(sampling_efficiency),c_int(n_dims),c_int(n_params),
            c_int(n_clustering_params),c_int(max_modes),
            c_int(n_iter_before_update),c_double(mode_tolerance),
            create_string_buffer(outputfiles_basename.encode()),    # MV 20130923: need a regular C string
            c_int(seed),wraps,
            c_bool(verbose),c_bool(resume),
            c_bool(write_output),c_bool(init_MPI),
            c_double(log_zero),c_int(max_iter),
            loglike_type(loglike),dumper_type(dumper),
            c_void_p(0))

Response: Runs the MultiNest algorithm.


Instruction:
Given the following Python 3 function, write the documentation
def best_kmers(dt, response, sequence, k=6, consider_shift=True, n_cores=1,
               seq_align="start", trim_seq_len=None):
    """
    Find best k-mers for CONCISE initialization.

    Args:
        dt (pd.DataFrame): Table containing response variable and sequence.
        response (str): Name of the column used as the reponse variable.
        sequence (str): Name of the column storing the DNA/RNA sequences.
        k (int): Desired k-mer length.
        n_cores (int): Number of cores to use for computation. It can use up to 3 cores.
        consider_shift (boolean): When performing stepwise k-mer selection. Is TATTTA similar to ATTTAG?
        seq_align (str): one of ``{"start", "end"}``. To which end should we align sequences?
        trim_seq_len (int): Consider only first `trim_seq_len` bases of each sequence when generating the sequence design matrix. If :python:`None`, set :py:attr:`trim_seq_len` to the longest sequence length, hence whole sequences are considered.

    Returns:
        string list: Best set of motifs for this dataset sorted with respect to
                     confidence (best candidate occuring first).

    Details:
        First a lasso model gets fitted to get a set of initial motifs. Next, the best
        subset of unrelated motifs is selected by stepwise selection.
    """
    y = dt[response]
    seq = dt[sequence]

    if trim_seq_len is not None:
        seq = pad_sequences(seq, align=seq_align, maxlen=trim_seq_len)
        seq = [s.replace("N", "") for s in seq]

    dt_kmer = kmer_count(seq, k)
    Xsp = csc_matrix(dt_kmer)
    en = ElasticNet(alpha=1, standardize=False, n_splits=3)
    en.fit(Xsp, y)
    # which coefficients are nonzero?=
    nonzero_kmers = dt_kmer.columns.values[en.coef_ != 0].tolist()

    # perform stepwise selection
    #
    # TODO - how do we deal with the intercept?

    # largest number of motifs where they don't differ by more than 1 k-mer
    def find_next_best(dt_kmer, y, selected_kmers, to_be_selected_kmers, consider_shift=True):
        """
        perform stepwise model selection while preventing to add a motif similar to the
        already selected motifs.
        """
        F, pval = f_regression(dt_kmer[to_be_selected_kmers], y)
        kmer = to_be_selected_kmers.pop(pval.argmin())
        selected_kmers.append(kmer)

        def select_criterion(s1, s2, consider_shift=True):
            if hamming_distance(s1, s2) <= 1:
                return False
            if consider_shift and hamming_distance(s1[1:], s2[:-1]) == 0:
                return False
            if consider_shift and hamming_distance(s1[:-1], s2[1:]) == 0:
                return False
            return True

        to_be_selected_kmers = [ckmer for ckmer in to_be_selected_kmers
                                if select_criterion(ckmer, kmer, consider_shift)]
        if len(to_be_selected_kmers) == 0:
            return selected_kmers
        else:
            # regress out the new feature
            lm = LinearRegression()
            lm.fit(dt_kmer[selected_kmers], y)
            y_new = y - lm.predict(dt_kmer[selected_kmers])
            return find_next_best(dt_kmer, y_new, selected_kmers, to_be_selected_kmers, consider_shift)

    selected_kmers = find_next_best(dt_kmer, y, [], nonzero_kmers, consider_shift)

    return selected_kmers

Response: This function finds the k - mers for CONCISE initialization.