Instruction:
Can you implement a function in Python 3 that
filters ports by device_owner

Response:
def filter_by_device_owner(query, device_owners=None):
    """Filter ports by device_owner

    Either filter using specified device_owner or using the list of all
    device_owners supported and unsupported by the arista ML2 plugin
    """
    port_model = models_v2.Port
    if not device_owners:
        device_owners = utils.SUPPORTED_DEVICE_OWNERS
    supported_device_owner_filter = [
        port_model.device_owner.ilike('%s%%' % owner)
        for owner in device_owners]
    unsupported_device_owner_filter = [
        port_model.device_owner.notilike('%s%%' % owner)
        for owner in utils.UNSUPPORTED_DEVICE_OWNERS]
    query = (query
             .filter(
                 and_(*unsupported_device_owner_filter),
                 or_(*supported_device_owner_filter)))
    return query


Instruction:
Given the following Python 3 function, write the documentation
def filter_by_device_id(query):
    port_model = models_v2.Port
    unsupported_device_id_filter = [
        port_model.device_id.notilike('%s%%' % id)
        for id in utils.UNSUPPORTED_DEVICE_IDS]
    query = (query
             .filter(and_(*unsupported_device_id_filter)))
    return query

Response: Filter ports attached to devices we don t care about


Instruction:
Explain what the following Python 3 code does
def filter_by_vnic_type(query, vnic_type):
    port_model = models_v2.Port
    binding_model = ml2_models.PortBinding
    dst_binding_model = ml2_models.DistributedPortBinding
    query = (query
             .outerjoin_if_necessary(
                 binding_model,
                 port_model.id == binding_model.port_id)
             .outerjoin_if_necessary(
                 dst_binding_model,
                 port_model.id == dst_binding_model.port_id)
             .filter(
                 (binding_model.vnic_type == vnic_type) |
                 (dst_binding_model.vnic_type == vnic_type)))
    return query

Response: Filter ports by vnic_type


Instruction:
Can you write a function in Python 3 where it
filters ports managed by other ML2 plugins

Response:
def filter_unmanaged_physnets(query):
    """Filter ports managed by other ML2 plugins """
    config = cfg.CONF.ml2_arista
    managed_physnets = config['managed_physnets']

    # Filter out ports bound to segments on physnets that we're not
    # managing
    segment_model = segment_models.NetworkSegment
    if managed_physnets:
        query = (query
                 .join_if_necessary(segment_model)
                 .filter(segment_model.physical_network.in_(
                     managed_physnets)))
    return query


Instruction:
Make a summary of the following Python 3 code
def filter_inactive_ports(query):
    port_model = models_v2.Port
    query = (query
             .filter(port_model.status == n_const.PORT_STATUS_ACTIVE))
    return query

Response: Filter ports that aren t in active status


Instruction:
Can you write a function in Python 3 where it
filters out ports that are not needed on CVX.

Response:
def filter_unnecessary_ports(query, device_owners=None, vnic_type=None,
                             active=True):
    """Filter out all ports are not needed on CVX """
    query = (query
             .filter_unbound_ports()
             .filter_by_device_owner(device_owners)
             .filter_by_device_id()
             .filter_unmanaged_physnets())
    if active:
        query = query.filter_inactive_ports()
    if vnic_type:
        query = query.filter_by_vnic_type(vnic_type)
    return query


Instruction:
How would you explain what the following Python 3 function does
def get_tenants(tenant_id=None):
    if tenant_id == '':
        return []
    session = db.get_reader_session()
    project_ids = set()
    with session.begin():
        for m in [models_v2.Network, models_v2.Port]:
            q = session.query(m.project_id).filter(m.project_id != '')
            if tenant_id:
                q = q.filter(m.project_id == tenant_id)
            project_ids.update(pid[0] for pid in q.distinct())
    return [{'project_id': project_id} for project_id in project_ids]

Response: Returns list of all project ids that may be relevant on CVX


Instruction:
How would you code a function in Python 3 to
return list of all networks that may be relevant on CVX

Response:
def get_networks(network_id=None):
    """Returns list of all networks that may be relevant on CVX"""
    session = db.get_reader_session()
    with session.begin():
        model = models_v2.Network
        networks = session.query(model).filter(model.project_id != '')
        if network_id:
            networks = networks.filter(model.id == network_id)
    return networks.all()


Instruction:
Can you tell what is the following Python 3 function doing
def get_segments(segment_id=None):
    session = db.get_reader_session()
    with session.begin():
        model = segment_models.NetworkSegment
        segments = session.query(model).filter_unnecessary_segments()
        if segment_id:
            segments = segments.filter(model.id == segment_id)
    return segments.all()

Response: Returns list of all network segments that may be relevant on CVX


Instruction:
Make a summary of the following Python 3 code
def get_instances(device_owners=None, vnic_type=None, instance_id=None):
    session = db.get_reader_session()
    with session.begin():
        port_model = models_v2.Port
        binding_model = ml2_models.PortBinding
        instances = (session
                     .query(port_model,
                            binding_model)
                     .outerjoin(
                         binding_model,
                         port_model.id == binding_model.port_id)
                     .distinct(port_model.device_id)
                     .group_by(port_model.device_id)
                     .filter_unnecessary_ports(device_owners, vnic_type))
        if instance_id:
            instances = instances.filter(port_model.device_id == instance_id)
    return instances.all()

Response: Returns a list of all instances in the neutron database


Instruction:
Can you tell what is the following Python 3 function doing
def get_vm_instances(instance_id=None):
    return get_instances(device_owners=[n_const.DEVICE_OWNER_COMPUTE_PREFIX],
                         vnic_type=portbindings.VNIC_NORMAL,
                         instance_id=instance_id)

Response: Returns filtered list of vms that may be relevant on CVX


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_ports(device_owners=None, vnic_type=None, port_id=None, active=True):
    session = db.get_reader_session()
    with session.begin():
        port_model = models_v2.Port
        ports = (session
                 .query(port_model)
                 .filter_unnecessary_ports(device_owners, vnic_type, active))
        if port_id:
            ports = ports.filter(port_model.id == port_id)
    return ports.all()

Response: Returns list of all ports in neutron the db


Instruction:
Implement a function in Python 3 to
return filtered list of vms that may be relevant on CVX

Response:
def get_vm_ports(port_id=None):
    """Returns filtered list of vms that may be relevant on CVX"""
    return get_ports(device_owners=[n_const.DEVICE_OWNER_COMPUTE_PREFIX,
                                    t_const.TRUNK_SUBPORT_OWNER],
                     vnic_type=portbindings.VNIC_NORMAL, port_id=port_id)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_port_bindings(binding_key=None):
    session = db.get_reader_session()
    with session.begin():
        binding_level_model = ml2_models.PortBindingLevel
        aliased_blm = aliased(ml2_models.PortBindingLevel)
        port_binding_model = ml2_models.PortBinding
        dist_binding_model = ml2_models.DistributedPortBinding
        bindings = (session.query(port_binding_model, aliased_blm)
                    .join(binding_level_model,
                          and_(
                              port_binding_model.port_id ==
                              binding_level_model.port_id,
                              port_binding_model.host ==
                              binding_level_model.host))
                    .filter_unnecessary_ports()
                    .join(aliased_blm,
                          and_(port_binding_model.port_id ==
                               aliased_blm.port_id,
                               port_binding_model.host ==
                               aliased_blm.host)))
        dist_bindings = (session.query(dist_binding_model, aliased_blm)
                         .join(
                             binding_level_model,
                             and_(dist_binding_model.port_id ==
                                  binding_level_model.port_id,
                                  dist_binding_model.host ==
                                  binding_level_model.host))
                         .filter_unnecessary_ports()
                         .filter(dist_binding_model.status ==
                                 n_const.PORT_STATUS_ACTIVE)
                         .join(aliased_blm,
                               and_(dist_binding_model.port_id ==
                                    aliased_blm.port_id,
                                    dist_binding_model.host ==
                                    aliased_blm.host)))
        if binding_key:
            port_id = binding_key[0]
            if type(binding_key[1]) == tuple:
                switch_id = binding_key[1][0]
                switch_port = binding_key[1][1]
                bindings = bindings.filter(and_(
                    port_binding_model.port_id == port_id,
                    port_binding_model.profile.ilike('%%%s%%' % switch_id),
                    port_binding_model.profile.ilike('%%%s%%' % switch_port)))
                dist_bindings = dist_bindings.filter(and_(
                    dist_binding_model.port_id == port_id,
                    dist_binding_model.profile.ilike('%%%s%%' % switch_id),
                    dist_binding_model.profile.ilike('%%%s%%' % switch_port)))
            else:
                host_id = binding_key[1]
                bindings = bindings.filter(and_(
                    port_binding_model.port_id == port_id,
                    port_binding_model.host == host_id))
                dist_bindings = dist_bindings.filter(and_(
                    dist_binding_model.port_id == port_id,
                    dist_binding_model.host == host_id))
    binding_levels = collections.defaultdict(list)
    for binding, level in bindings.all() + dist_bindings.all():
        binding_levels[binding].append(level)
    bindings_with_levels = list()
    for binding, levels in binding_levels.items():
        binding.levels = levels
        bindings_with_levels.append(binding)
    return bindings_with_levels

Response: Returns a filtered list of port bindings that may be relevant on CVX.


Instruction:
Can you generate the documentation for the following Python 3 function
def tenant_provisioned(tenant_id):
    session = db.get_reader_session()
    with session.begin():
        res = any(
            session.query(m).filter(m.tenant_id == tenant_id).count()
            for m in [models_v2.Network, models_v2.Port]
        )
    return res

Response: Returns True if any networks or ports exist for a tenant.


Instruction:
Write a Python 3 function that can
return True if any ports exist for an instance.

Response:
def instance_provisioned(device_id):
    """Returns true if any ports exist for an instance."""
    session = db.get_reader_session()
    with session.begin():
        port_model = models_v2.Port
        res = bool(session.query(port_model)
                   .filter(port_model.device_id == device_id).count())
    return res


Instruction:
Make a summary of the following Python 3 code
def port_provisioned(port_id):
    session = db.get_reader_session()
    with session.begin():
        port_model = models_v2.Port
        res = bool(session.query(port_model)
                   .filter(port_model.id == port_id).count())
    return res

Response: Returns true if port is already provisioned.


Instruction:
Given the following Python 3 function, write the documentation
def get_parent(port_id):
    session = db.get_reader_session()
    res = dict()
    with session.begin():
        subport_model = trunk_models.SubPort
        trunk_model = trunk_models.Trunk
        subport = (session.query(subport_model).
                   filter(subport_model.port_id == port_id).first())
        if subport:
            trunk = (session.query(trunk_model).
                     filter(trunk_model.id == subport.trunk_id).first())
            if trunk:
                trunk_port_id = trunk.port.id
                res = get_ports(port_id=trunk_port_id, active=False)[0]
    return res

Response: Get trunk subport s parent port


Instruction:
Make a summary of the following Python 3 code
def get_port_binding_level(filters):
    session = db.get_reader_session()
    with session.begin():
        return (session.query(ml2_models.PortBindingLevel).
                filter_by(**filters).
                order_by(ml2_models.PortBindingLevel.level).
                all())

Response: Returns entries from PortBindingLevel based on the specified filters.


Instruction:
Can you write a function in Python 3 where it
reads audio data from a sound file.

Response:
def read(file, frames=-1, start=0, stop=None, dtype='float64', always_2d=False,
         fill_value=None, out=None, samplerate=None, channels=None,
         format=None, subtype=None, endian=None, closefd=True):
    """Provide audio data from a sound file as NumPy array.

    By default, the whole file is read from the beginning, but the
    position to start reading can be specified with `start` and the
    number of frames to read can be specified with `frames`.
    Alternatively, a range can be specified with `start` and `stop`.

    If there is less data left in the file than requested, the rest of
    the frames are filled with `fill_value`.
    If no `fill_value` is specified, a smaller array is returned.

    Parameters
    ----------
    file : str or int or file-like object
        The file to read from.  See :class:`SoundFile` for details.
    frames : int, optional
        The number of frames to read. If `frames` is negative, the whole
        rest of the file is read.  Not allowed if `stop` is given.
    start : int, optional
        Where to start reading.  A negative value counts from the end.
    stop : int, optional
        The index after the last frame to be read.  A negative value
        counts from the end.  Not allowed if `frames` is given.
    dtype : {'float64', 'float32', 'int32', 'int16'}, optional
        Data type of the returned array, by default ``'float64'``.
        Floating point audio data is typically in the range from
        ``-1.0`` to ``1.0``.  Integer data is in the range from
        ``-2**15`` to ``2**15-1`` for ``'int16'`` and from ``-2**31`` to
        ``2**31-1`` for ``'int32'``.

        .. note:: Reading int values from a float file will *not*
            scale the data to [-1.0, 1.0). If the file contains
            ``np.array([42.6], dtype='float32')``, you will read
            ``np.array([43], dtype='int32')`` for ``dtype='int32'``.

    Returns
    -------
    audiodata : numpy.ndarray or type(out)
        A two-dimensional (frames x channels) NumPy array is returned.
        If the sound file has only one channel, a one-dimensional array
        is returned.  Use ``always_2d=True`` to return a two-dimensional
        array anyway.

        If `out` was specified, it is returned.  If `out` has more
        frames than available in the file (or if `frames` is smaller
        than the length of `out`) and no `fill_value` is given, then
        only a part of `out` is overwritten and a view containing all
        valid frames is returned.
    samplerate : int
        The sample rate of the audio file.

    Other Parameters
    ----------------
    always_2d : bool, optional
        By default, reading a mono sound file will return a
        one-dimensional array.  With ``always_2d=True``, audio data is
        always returned as a two-dimensional array, even if the audio
        file has only one channel.
    fill_value : float, optional
        If more frames are requested than available in the file, the
        rest of the output is be filled with `fill_value`.  If
        `fill_value` is not specified, a smaller array is returned.
    out : numpy.ndarray or subclass, optional
        If `out` is specified, the data is written into the given array
        instead of creating a new array.  In this case, the arguments
        `dtype` and `always_2d` are silently ignored!  If `frames` is
        not given, it is obtained from the length of `out`.
    samplerate, channels, format, subtype, endian, closefd
        See :class:`SoundFile`.

    Examples
    --------
    >>> import soundfile as sf
    >>> data, samplerate = sf.read('stereo_file.wav')
    >>> data
    array([[ 0.71329652,  0.06294799],
           [-0.26450912, -0.38874483],
           ...
           [ 0.67398441, -0.11516333]])
    >>> samplerate
    44100

    """
    with SoundFile(file, 'r', samplerate, channels,
                   subtype, endian, format, closefd) as f:
        frames = f._prepare_read(start, stop, frames)
        data = f.read(frames, dtype, always_2d, fill_value, out)
    return data, f.samplerate


Instruction:
Implement a Python 3 function for
writing data to a sound file.

Response:
def write(file, data, samplerate, subtype=None, endian=None, format=None,
          closefd=True):
    """Write data to a sound file.

    .. note:: If `file` exists, it will be truncated and overwritten!

    Parameters
    ----------
    file : str or int or file-like object
        The file to write to.  See :class:`SoundFile` for details.
    data : array_like
        The data to write.  Usually two-dimensional (frames x channels),
        but one-dimensional `data` can be used for mono files.
        Only the data types ``'float64'``, ``'float32'``, ``'int32'``
        and ``'int16'`` are supported.

        .. note:: The data type of `data` does **not** select the data
                  type of the written file. Audio data will be
                  converted to the given `subtype`. Writing int values
                  to a float file will *not* scale the values to
                  [-1.0, 1.0). If you write the value ``np.array([42],
                  dtype='int32')``, to a ``subtype='FLOAT'`` file, the
                  file will then contain ``np.array([42.],
                  dtype='float32')``.

    samplerate : int
        The sample rate of the audio data.
    subtype : str, optional
        See :func:`default_subtype` for the default value and
        :func:`available_subtypes` for all possible values.

    Other Parameters
    ----------------
    format, endian, closefd
        See :class:`SoundFile`.

    Examples
    --------
    Write 10 frames of random data to a new file:

    >>> import numpy as np
    >>> import soundfile as sf
    >>> sf.write('stereo_file.wav', np.random.randn(10, 2), 44100, 'PCM_24')

    """
    import numpy as np
    data = np.asarray(data)
    if data.ndim == 1:
        channels = 1
    else:
        channels = data.shape[1]
    with SoundFile(file, 'w', samplerate, channels,
                   subtype, endian, format, closefd) as f:
        f.write(data)


Instruction:
Implement a function in Python 3 to
return a generator for reading audio files in a specific block - wise fashion.

Response:
def blocks(file, blocksize=None, overlap=0, frames=-1, start=0, stop=None,
           dtype='float64', always_2d=False, fill_value=None, out=None,
           samplerate=None, channels=None,
           format=None, subtype=None, endian=None, closefd=True):
    """Return a generator for block-wise reading.

    By default, iteration starts at the beginning and stops at the end
    of the file.  Use `start` to start at a later position and `frames`
    or `stop` to stop earlier.

    If you stop iterating over the generator before it's exhausted,
    the sound file is not closed. This is normally not a problem
    because the file is opened in read-only mode. To close the file
    properly, the generator's ``close()`` method can be called.

    Parameters
    ----------
    file : str or int or file-like object
        The file to read from.  See :class:`SoundFile` for details.
    blocksize : int
        The number of frames to read per block.
        Either this or `out` must be given.
    overlap : int, optional
        The number of frames to rewind between each block.

    Yields
    ------
    numpy.ndarray or type(out)
        Blocks of audio data.
        If `out` was given, and the requested frames are not an integer
        multiple of the length of `out`, and no `fill_value` was given,
        the last block will be a smaller view into `out`.

    Other Parameters
    ----------------
    frames, start, stop
        See :func:`read`.
    dtype : {'float64', 'float32', 'int32', 'int16'}, optional
        See :func:`read`.
    always_2d, fill_value, out
        See :func:`read`.
    samplerate, channels, format, subtype, endian, closefd
        See :class:`SoundFile`.

    Examples
    --------
    >>> import soundfile as sf
    >>> for block in sf.blocks('stereo_file.wav', blocksize=1024):
    >>>     pass  # do something with 'block'

    """
    with SoundFile(file, 'r', samplerate, channels,
                   subtype, endian, format, closefd) as f:
        frames = f._prepare_read(start, stop, frames)
        for block in f.blocks(blocksize, overlap, frames,
                              dtype, always_2d, fill_value, out):
            yield block


Instruction:
Can you implement a function in Python 3 that
returns a dictionary of available subtypes.

Response:
def available_subtypes(format=None):
    """Return a dictionary of available subtypes.

    Parameters
    ----------
    format : str
        If given, only compatible subtypes are returned.

    Examples
    --------
    >>> import soundfile as sf
    >>> sf.available_subtypes('FLAC')
    {'PCM_24': 'Signed 24 bit PCM',
     'PCM_16': 'Signed 16 bit PCM',
     'PCM_S8': 'Signed 8 bit PCM'}

    """
    subtypes = _available_formats_helper(_snd.SFC_GET_FORMAT_SUBTYPE_COUNT,
                                         _snd.SFC_GET_FORMAT_SUBTYPE)
    return dict((subtype, name) for subtype, name in subtypes
                if format is None or check_format(format, subtype))


Instruction:
Can you implement a function in Python 3 that
checks if the combination of format subtype and endian is valid.

Response:
def check_format(format, subtype=None, endian=None):
    """Check if the combination of format/subtype/endian is valid.

    Examples
    --------
    >>> import soundfile as sf
    >>> sf.check_format('WAV', 'PCM_24')
    True
    >>> sf.check_format('FLAC', 'VORBIS')
    False

    """
    try:
        return bool(_format_int(format, subtype, endian))
    except (ValueError, TypeError):
        return False


Instruction:
Can you create a Python 3 function that
returns numeric ID for given format|subtype|endian combo.

Response:
def _format_int(format, subtype, endian):
    """Return numeric ID for given format|subtype|endian combo."""
    result = _check_format(format)
    if subtype is None:
        subtype = default_subtype(format)
        if subtype is None:
            raise TypeError(
                "No default subtype for major format {0!r}".format(format))
    elif not isinstance(subtype, (_unicode, str)):
        raise TypeError("Invalid subtype: {0!r}".format(subtype))
    try:
        result |= _subtypes[subtype.upper()]
    except KeyError:
        raise ValueError("Unknown subtype: {0!r}".format(subtype))
    if endian is None:
        endian = 'FILE'
    elif not isinstance(endian, (_unicode, str)):
        raise TypeError("Invalid endian-ness: {0!r}".format(endian))
    try:
        result |= _endians[endian.upper()]
    except KeyError:
        raise ValueError("Unknown endian-ness: {0!r}".format(endian))

    info = _ffi.new("SF_INFO*")
    info.format = result
    info.channels = 1
    if _snd.sf_format_check(info) == _snd.SF_FALSE:
        raise ValueError(
            "Invalid combination of format, subtype and endian")
    return result


Instruction:
Can you write a function in Python 3 where it
checks if mode is valid and return its integer representation.

Response:
def _check_mode(mode):
    """Check if mode is valid and return its integer representation."""
    if not isinstance(mode, (_unicode, str)):
        raise TypeError("Invalid mode: {0!r}".format(mode))
    mode_set = set(mode)
    if mode_set.difference('xrwb+') or len(mode) > len(mode_set):
        raise ValueError("Invalid mode: {0!r}".format(mode))
    if len(mode_set.intersection('xrw')) != 1:
        raise ValueError("mode must contain exactly one of 'xrw'")

    if '+' in mode_set:
        mode_int = _snd.SFM_RDWR
    elif 'r' in mode_set:
        mode_int = _snd.SFM_READ
    else:
        mode_int = _snd.SFM_WRITE
    return mode_int


Instruction:
How would you code a function in Python 3 to
check arguments and create SF_INFO struct.

Response:
def _create_info_struct(file, mode, samplerate, channels,
                        format, subtype, endian):
    """Check arguments and create SF_INFO struct."""
    original_format = format
    if format is None:
        format = _get_format_from_filename(file, mode)
        assert isinstance(format, (_unicode, str))
    else:
        _check_format(format)

    info = _ffi.new("SF_INFO*")
    if 'r' not in mode or format.upper() == 'RAW':
        if samplerate is None:
            raise TypeError("samplerate must be specified")
        info.samplerate = samplerate
        if channels is None:
            raise TypeError("channels must be specified")
        info.channels = channels
        info.format = _format_int(format, subtype, endian)
    else:
        if any(arg is not None for arg in (
                samplerate, channels, original_format, subtype, endian)):
            raise TypeError("Not allowed for existing files (except 'RAW'): "
                            "samplerate, channels, format, subtype, endian")
    return info


Instruction:
Create a Python 3 function to
get a format string obtained from a file extension.

Response:
def _get_format_from_filename(file, mode):
    """Return a format string obtained from file (or file.name).

    If file already exists (= read mode), an empty string is returned on
    error.  If not, an exception is raised.
    The return type will always be str or unicode (even if
    file/file.name is a bytes object).

    """
    format = ''
    file = getattr(file, 'name', file)
    try:
        # This raises an exception if file is not a (Unicode/byte) string:
        format = _os.path.splitext(file)[-1][1:]
        # Convert bytes to unicode (raises AttributeError on Python 3 str):
        format = format.decode('utf-8', 'replace')
    except Exception:
        pass
    if format.upper() not in _formats and 'r' not in mode:
        raise TypeError("No format specified and unable to get format from "
                        "file extension: {0!r}".format(file))
    return format


Instruction:
Create a Python 3 function to
return the string representation of a given numeric format.

Response:
def _format_str(format_int):
    """Return the string representation of a given numeric format."""
    for dictionary in _formats, _subtypes, _endians:
        for k, v in dictionary.items():
            if v == format_int:
                return k
    else:
        return 'n/a'


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _format_info(format_int, format_flag=_snd.SFC_GET_FORMAT_INFO):
    format_info = _ffi.new("SF_FORMAT_INFO*")
    format_info.format = format_int
    _snd.sf_command(_ffi.NULL, format_flag, format_info,
                    _ffi.sizeof("SF_FORMAT_INFO"))
    name = format_info.name
    return (_format_str(format_info.format),
            _ffi.string(name).decode('utf-8', 'replace') if name else "")

Response: Return the ID and short description of a given format.


Instruction:
Can you tell what is the following Python 3 function doing
def _check_format(format_str):
    if not isinstance(format_str, (_unicode, str)):
        raise TypeError("Invalid format: {0!r}".format(format_str))
    try:
        format_int = _formats[format_str.upper()]
    except KeyError:
        raise ValueError("Unknown format: {0!r}".format(format_str))
    return format_int

Response: Check if format_str is valid and return format ID.


Instruction:
Can you create a Python 3 function that
checks if file has all the necessary attributes for virtual IO.

Response:
def _has_virtual_io_attrs(file, mode_int):
    """Check if file has all the necessary attributes for virtual IO."""
    readonly = mode_int == _snd.SFM_READ
    writeonly = mode_int == _snd.SFM_WRITE
    return all([
        hasattr(file, 'seek'),
        hasattr(file, 'tell'),
        hasattr(file, 'write') or readonly,
        hasattr(file, 'read') or hasattr(file, 'readinto') or writeonly,
    ])


Instruction:
How would you code a function in Python 3 to
retrieve the extra info generated when opening the file.

Response:
def extra_info(self):
        """Retrieve the log string generated when opening the file."""
        info = _ffi.new("char[]", 2**14)
        _snd.sf_command(self._file, _snd.SFC_GET_LOG_INFO,
                        info, _ffi.sizeof(info))
        return _ffi.string(info).decode('utf-8', 'replace')


Instruction:
Can you create a Python 3 function that
sets the absolute position of the file.

Response:
def seek(self, frames, whence=SEEK_SET):
        """Set the read/write position.

        Parameters
        ----------
        frames : int
            The frame index or offset to seek.
        whence : {SEEK_SET, SEEK_CUR, SEEK_END}, optional
            By default (``whence=SEEK_SET``), `frames` are counted from
            the beginning of the file.
            ``whence=SEEK_CUR`` seeks from the current position
            (positive and negative values are allowed for `frames`).
            ``whence=SEEK_END`` seeks from the end (use negative value
            for `frames`).

        Returns
        -------
        int
            The new absolute read/write position in frames.

        Examples
        --------
        >>> from soundfile import SoundFile, SEEK_END
        >>> myfile = SoundFile('stereo_file.wav')

        Seek to the beginning of the file:

        >>> myfile.seek(0)
        0

        Seek to the end of the file:

        >>> myfile.seek(0, SEEK_END)
        44100  # this is the file length

        """
        self._check_if_closed()
        position = _snd.sf_seek(self._file, frames, whence)
        _error_check(self._errorcode)
        return position


Instruction:
Make a summary of the following Python 3 code
def read(self, frames=-1, dtype='float64', always_2d=False,
             fill_value=None, out=None):
        """Read from the file and return data as NumPy array.

        Reads the given number of frames in the given data format
        starting at the current read/write position.  This advances the
        read/write position by the same number of frames.
        By default, all frames from the current read/write position to
        the end of the file are returned.
        Use :meth:`.seek` to move the current read/write position.

        Parameters
        ----------
        frames : int, optional
            The number of frames to read. If ``frames < 0``, the whole
            rest of the file is read.
        dtype : {'float64', 'float32', 'int32', 'int16'}, optional
            Data type of the returned array, by default ``'float64'``.
            Floating point audio data is typically in the range from
            ``-1.0`` to ``1.0``. Integer data is in the range from
            ``-2**15`` to ``2**15-1`` for ``'int16'`` and from
            ``-2**31`` to ``2**31-1`` for ``'int32'``.

            .. note:: Reading int values from a float file will *not*
                scale the data to [-1.0, 1.0). If the file contains
                ``np.array([42.6], dtype='float32')``, you will read
                ``np.array([43], dtype='int32')`` for
                ``dtype='int32'``.

        Returns
        -------
        audiodata : numpy.ndarray or type(out)
            A two-dimensional NumPy (frames x channels) array is
            returned. If the sound file has only one channel, a
            one-dimensional array is returned. Use ``always_2d=True``
            to return a two-dimensional array anyway.

            If `out` was specified, it is returned. If `out` has more
            frames than available in the file (or if `frames` is
            smaller than the length of `out`) and no `fill_value` is
            given, then only a part of `out` is overwritten and a view
            containing all valid frames is returned. numpy.ndarray or
            type(out)

        Other Parameters
        ----------------
        always_2d : bool, optional
            By default, reading a mono sound file will return a
            one-dimensional array. With ``always_2d=True``, audio data
            is always returned as a two-dimensional array, even if the
            audio file has only one channel.
        fill_value : float, optional
            If more frames are requested than available in the file,
            the rest of the output is be filled with `fill_value`. If
            `fill_value` is not specified, a smaller array is
            returned.
        out : numpy.ndarray or subclass, optional
            If `out` is specified, the data is written into the given
            array instead of creating a new array. In this case, the
            arguments `dtype` and `always_2d` are silently ignored! If
            `frames` is not given, it is obtained from the length of
            `out`.

        Examples
        --------
        >>> from soundfile import SoundFile
        >>> myfile = SoundFile('stereo_file.wav')

        Reading 3 frames from a stereo file:

        >>> myfile.read(3)
        array([[ 0.71329652,  0.06294799],
               [-0.26450912, -0.38874483],
               [ 0.67398441, -0.11516333]])
        >>> myfile.close()

        See Also
        --------
        buffer_read, .write

        """
        if out is None:
            frames = self._check_frames(frames, fill_value)
            out = self._create_empty_array(frames, always_2d, dtype)
        else:
            if frames < 0 or frames > len(out):
                frames = len(out)
        frames = self._array_io('read', out, frames)
        if len(out) > frames:
            if fill_value is None:
                out = out[:frames]
            else:
                out[frames:] = fill_value
        return out

Response: Read from the file and return data as NumPy array.


Instruction:
Explain what the following Python 3 code does
def buffer_read(self, frames=-1, dtype=None):
        frames = self._check_frames(frames, fill_value=None)
        ctype = self._check_dtype(dtype)
        cdata = _ffi.new(ctype + '[]', frames * self.channels)
        read_frames = self._cdata_io('read', cdata, ctype, frames)
        assert read_frames == frames
        return _ffi.buffer(cdata)

Response: Read from the file and return the data as a buffer object.


Instruction:
Can you create a Python 3 function that
reads from the file into a given buffer object.

Response:
def buffer_read_into(self, buffer, dtype):
        """Read from the file into a given buffer object.

        Fills the given `buffer` with frames in the given data format
        starting at the current read/write position (which can be
        changed with :meth:`.seek`) until the buffer is full or the end
        of the file is reached.  This advances the read/write position
        by the number of frames that were read.

        Parameters
        ----------
        buffer : writable buffer
            Audio frames from the file are written to this buffer.
        dtype : {'float64', 'float32', 'int32', 'int16'}
            The data type of `buffer`.

        Returns
        -------
        int
            The number of frames that were read from the file.
            This can be less than the size of `buffer`.
            The rest of the buffer is not filled with meaningful data.

        See Also
        --------
        buffer_read, .read

        """
        ctype = self._check_dtype(dtype)
        cdata, frames = self._check_buffer(buffer, ctype)
        frames = self._cdata_io('read', cdata, ctype, frames)
        return frames


Instruction:
Can you generate the documentation for the following Python 3 function
def write(self, data):
        import numpy as np
        # no copy is made if data has already the correct memory layout:
        data = np.ascontiguousarray(data)
        written = self._array_io('write', data, len(data))
        assert written == len(data)
        self._update_frames(written)

Response: Write audio data from a NumPy array to the file.


Instruction:
Create a Python 3 function to
write audio data from a buffer or bytes object to the file.

Response:
def buffer_write(self, data, dtype):
        """Write audio data from a buffer/bytes object to the file.

        Writes the contents of `data` to the file at the current
        read/write position.
        This also advances the read/write position by the number of
        frames that were written and enlarges the file if necessary.

        Parameters
        ----------
        data : buffer or bytes
            A buffer or bytes object containing the audio data to be
            written.
        dtype : {'float64', 'float32', 'int32', 'int16'}
            The data type of the audio data stored in `data`.

        See Also
        --------
        .write, buffer_read

        """
        ctype = self._check_dtype(dtype)
        cdata, frames = self._check_buffer(data, ctype)
        written = self._cdata_io('write', cdata, ctype, frames)
        assert written == frames
        self._update_frames(written)


Instruction:
Make a summary of the following Python 3 code
def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
               always_2d=False, fill_value=None, out=None):
        """Return a generator for block-wise reading.

        By default, the generator yields blocks of the given
        `blocksize` (using a given `overlap`) until the end of the file
        is reached; `frames` can be used to stop earlier.

        Parameters
        ----------
        blocksize : int
            The number of frames to read per block. Either this or `out`
            must be given.
        overlap : int, optional
            The number of frames to rewind between each block.
        frames : int, optional
            The number of frames to read.
            If ``frames < 0``, the file is read until the end.
        dtype : {'float64', 'float32', 'int32', 'int16'}, optional
            See :meth:`.read`.

        Yields
        ------
        numpy.ndarray or type(out)
            Blocks of audio data.
            If `out` was given, and the requested frames are not an
            integer multiple of the length of `out`, and no
            `fill_value` was given, the last block will be a smaller
            view into `out`.

        Other Parameters
        ----------------
        always_2d, fill_value, out
            See :meth:`.read`.
        fill_value : float, optional
            See :meth:`.read`.
        out : numpy.ndarray or subclass, optional
            If `out` is specified, the data is written into the given
            array instead of creating a new array. In this case, the
            arguments `dtype` and `always_2d` are silently ignored!

        Examples
        --------
        >>> from soundfile import SoundFile
        >>> with SoundFile('stereo_file.wav') as f:
        >>>     for block in f.blocks(blocksize=1024):
        >>>         pass  # do something with 'block'

        """
        import numpy as np

        if 'r' not in self.mode and '+' not in self.mode:
            raise RuntimeError("blocks() is not allowed in write-only mode")

        if out is None:
            if blocksize is None:
                raise TypeError("One of {blocksize, out} must be specified")
            out = self._create_empty_array(blocksize, always_2d, dtype)
            copy_out = True
        else:
            if blocksize is not None:
                raise TypeError(
                    "Only one of {blocksize, out} may be specified")
            blocksize = len(out)
            copy_out = False

        overlap_memory = None
        frames = self._check_frames(frames, fill_value)
        while frames > 0:
            if overlap_memory is None:
                output_offset = 0
            else:
                output_offset = len(overlap_memory)
                out[:output_offset] = overlap_memory

            toread = min(blocksize - output_offset, frames)
            self.read(toread, dtype, always_2d, fill_value, out[output_offset:])

            if overlap:
                if overlap_memory is None:
                    overlap_memory = np.copy(out[-overlap:])
                else:
                    overlap_memory[:] = out[-overlap:]

            if blocksize > frames + overlap and fill_value is None:
                block = out[:frames + overlap]
            else:
                block = out
            yield np.copy(block) if copy_out else block
            frames -= toread

Response: Return a generator for reading audio files in a specific block size.


Instruction:
Write a Python 3 script for
truncating the file to a given number of frames.

Response:
def truncate(self, frames=None):
        """Truncate the file to a given number of frames.

        After this command, the read/write position will be at the new
        end of the file.

        Parameters
        ----------
        frames : int, optional
            Only the data before `frames` is kept, the rest is deleted.
            If not specified, the current read/write position is used.

        """
        if frames is None:
            frames = self.tell()
        err = _snd.sf_command(self._file, _snd.SFC_FILE_TRUNCATE,
                              _ffi.new("sf_count_t*", frames),
                              _ffi.sizeof("sf_count_t"))
        if err:
            raise RuntimeError("Error truncating the file")
        self._info.frames = frames


Instruction:
Implement a Python 3 function for
closing the file. Can be called multiple times.

Response:
def close(self):
        """Close the file.  Can be called multiple times."""
        if not self.closed:
            # be sure to flush data to disk before closing the file
            self.flush()
            err = _snd.sf_close(self._file)
            self._file = None
            _error_check(err)


Instruction:
Can you generate the documentation for the following Python 3 function
def _open(self, file, mode_int, closefd):
        if isinstance(file, (_unicode, bytes)):
            if _os.path.isfile(file):
                if 'x' in self.mode:
                    raise OSError("File exists: {0!r}".format(self.name))
                elif set(self.mode).issuperset('w+'):
                    # truncate the file, because SFM_RDWR doesn't:
                    _os.close(_os.open(file, _os.O_WRONLY | _os.O_TRUNC))
            openfunction = _snd.sf_open
            if isinstance(file, _unicode):
                if _sys.platform == 'win32':
                    openfunction = _snd.sf_wchar_open
                else:
                    file = file.encode(_sys.getfilesystemencoding())
            file_ptr = openfunction(file, mode_int, self._info)
        elif isinstance(file, int):
            file_ptr = _snd.sf_open_fd(file, mode_int, self._info, closefd)
        elif _has_virtual_io_attrs(file, mode_int):
            file_ptr = _snd.sf_open_virtual(self._init_virtual_io(file),
                                            mode_int, self._info, _ffi.NULL)
        else:
            raise TypeError("Invalid file: {0!r}".format(self.name))
        _error_check(_snd.sf_error(file_ptr),
                     "Error opening {0!r}: ".format(self.name))
        if mode_int == _snd.SFM_WRITE:
            # Due to a bug in libsndfile version <= 1.0.25, frames != 0
            # when opening a named pipe in SFM_WRITE mode.
            # See http://github.com/erikd/libsndfile/issues/77.
            self._info.frames = 0
            # This is not necessary for "normal" files (because
            # frames == 0 in this case), but it doesn't hurt, either.
        return file_ptr

Response: Open the file and return the file handle.


Instruction:
Implement a Python 3 function for
initializing the virtual io for the current version of the file.

Response:
def _init_virtual_io(self, file):
        """Initialize callback functions for sf_open_virtual()."""
        @_ffi.callback("sf_vio_get_filelen")
        def vio_get_filelen(user_data):
            curr = file.tell()
            file.seek(0, SEEK_END)
            size = file.tell()
            file.seek(curr, SEEK_SET)
            return size

        @_ffi.callback("sf_vio_seek")
        def vio_seek(offset, whence, user_data):
            file.seek(offset, whence)
            return file.tell()

        @_ffi.callback("sf_vio_read")
        def vio_read(ptr, count, user_data):
            # first try readinto(), if not available fall back to read()
            try:
                buf = _ffi.buffer(ptr, count)
                data_read = file.readinto(buf)
            except AttributeError:
                data = file.read(count)
                data_read = len(data)
                buf = _ffi.buffer(ptr, data_read)
                buf[0:data_read] = data
            return data_read

        @_ffi.callback("sf_vio_write")
        def vio_write(ptr, count, user_data):
            buf = _ffi.buffer(ptr, count)
            data = buf[:]
            written = file.write(data)
            # write() returns None for file objects in Python <= 2.7:
            if written is None:
                written = count
            return written

        @_ffi.callback("sf_vio_tell")
        def vio_tell(user_data):
            return file.tell()

        # Note: the callback functions must be kept alive!
        self._virtual_io = {'get_filelen': vio_get_filelen,
                            'seek': vio_seek,
                            'read': vio_read,
                            'write': vio_write,
                            'tell': vio_tell}

        return _ffi.new("SF_VIRTUAL_IO*", self._virtual_io)


Instruction:
Can you implement a function in Python 3 that
reduces frames to no more than are available in the file.

Response:
def _check_frames(self, frames, fill_value):
        """Reduce frames to no more than are available in the file."""
        if self.seekable():
            remaining_frames = self.frames - self.tell()
            if frames < 0 or (frames > remaining_frames and
                              fill_value is None):
                frames = remaining_frames
        elif frames < 0:
            raise ValueError("frames must be specified for non-seekable files")
        return frames


Instruction:
Can you tell what is the following Python 3 function doing
def _check_buffer(self, data, ctype):
        assert ctype in _ffi_types.values()
        if not isinstance(data, bytes):
            data = _ffi.from_buffer(data)
        frames, remainder = divmod(len(data),
                                   self.channels * _ffi.sizeof(ctype))
        if remainder:
            raise ValueError("Data size must be a multiple of frame size")
        return data, frames

Response: Convert buffer to cdata and check for valid size.


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_empty_array(self, frames, always_2d, dtype):
        import numpy as np
        if always_2d or self.channels > 1:
            shape = frames, self.channels
        else:
            shape = frames,
        return np.empty(shape, dtype, order='C')

Response: Create an empty array with appropriate shape.


Instruction:
Implement a Python 3 function for
checking if dtype string is valid and return ctype string.

Response:
def _check_dtype(self, dtype):
        """Check if dtype string is valid and return ctype string."""
        try:
            return _ffi_types[dtype]
        except KeyError:
            raise ValueError("dtype must be one of {0!r} and not {1!r}".format(
                sorted(_ffi_types.keys()), dtype))


Instruction:
Given the following Python 3 function, write the documentation
def _array_io(self, action, array, frames):
        if (array.ndim not in (1, 2) or
                array.ndim == 1 and self.channels != 1 or
                array.ndim == 2 and array.shape[1] != self.channels):
            raise ValueError("Invalid shape: {0!r}".format(array.shape))
        if not array.flags.c_contiguous:
            raise ValueError("Data must be C-contiguous")
        ctype = self._check_dtype(array.dtype.name)
        assert array.dtype.itemsize == _ffi.sizeof(ctype)
        cdata = _ffi.cast(ctype + '*', array.__array_interface__['data'][0])
        return self._cdata_io(action, cdata, ctype, frames)

Response: Check array and call low - level IO function.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _cdata_io(self, action, data, ctype, frames):
        assert ctype in _ffi_types.values()
        self._check_if_closed()
        if self.seekable():
            curr = self.tell()
        func = getattr(_snd, 'sf_' + action + 'f_' + ctype)
        frames = func(self._file, data, frames)
        _error_check(self._errorcode)
        if self.seekable():
            self.seek(curr + frames, SEEK_SET)  # Update read & write position
        return frames

Response: Call one of libsndfile s read and write functions.


Instruction:
Can you implement a function in Python 3 that
updates self. frames after writing.

Response:
def _update_frames(self, written):
        """Update self.frames after writing."""
        if self.seekable():
            curr = self.tell()
            self._info.frames = self.seek(0, SEEK_END)
            self.seek(curr, SEEK_SET)
        else:
            self._info.frames += written


Instruction:
How would you explain what the following Python 3 function does
def _prepare_read(self, start, stop, frames):
        if start != 0 and not self.seekable():
            raise ValueError("start is only allowed for seekable files")
        if frames >= 0 and stop is not None:
            raise TypeError("Only one of {frames, stop} may be used")

        start, stop, _ = slice(start, stop).indices(self.frames)
        if stop < start:
            stop = start
        if frames < 0:
            frames = stop - start
        if self.seekable():
            self.seek(start, SEEK_SET)
        return frames

Response: Seek to start frame and calculate length.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ensure_dir(dirname):
  try:
    # Tries to create the directory
    os.makedirs(dirname)
  except OSError:
    # Check that the directory exists
    if os.path.isdir(dirname): pass
    else: raise

Response: Creates the directory dirname if it does not already exist.


Instruction:
Here you have a function in Python 3, explain what it does
def probes_used_generate_vector(probe_files_full, probe_files_model):
  import numpy as np
  C_probesUsed = np.ndarray((len(probe_files_full),), 'bool')
  C_probesUsed.fill(False)
  c=0
  for k in sorted(probe_files_full.keys()):
    if probe_files_model.has_key(k): C_probesUsed[c] = True
    c+=1
  return C_probesUsed

Response: Generates boolean matrices indicating which are the probes for each model in the probe_files_full.


Instruction:
How would you implement a function in Python 3 that
extracts a matrix of scores for a model given a probes_used row vector of boolean

Response:
def probes_used_extract_scores(full_scores, same_probes):
  """Extracts a matrix of scores for a model, given a probes_used row vector of boolean"""
  if full_scores.shape[1] != same_probes.shape[0]: raise "Size mismatch"
  import numpy as np
  model_scores = np.ndarray((full_scores.shape[0],np.sum(same_probes)), 'float64')
  c=0
  for i in range(0,full_scores.shape[1]):
    if same_probes[i]:
      for j in range(0,full_scores.shape[0]):
        model_scores[j,c] = full_scores[j,i]
      c+=1
  return model_scores


Instruction:
Implement a Python 3 function for
applying a unit mean and variance normalization to an arrayset

Response:
def normalize_std_array(vector):
  """Applies a unit mean and variance normalization to an arrayset"""

  # Initializes variables
  length = 1
  n_samples = len(vector)
  mean = numpy.ndarray((length,), 'float64')
  std = numpy.ndarray((length,), 'float64')
  mean.fill(0)
  std.fill(0)

  # Computes mean and variance
  for array in vector:
    x = array.astype('float64')
    mean += x
    std += (x ** 2)

  mean /= n_samples
  std /= n_samples
  std -= (mean ** 2)
  std = std ** 0.5
  arrayset = numpy.ndarray(shape=(n_samples,mean.shape[0]), dtype=numpy.float64)

  for i in range (0, n_samples):
    arrayset[i,:] = (vector[i]-mean) / std
  return arrayset


Instruction:
Explain what the following Python 3 code does
def smoothing(labels, smoothing_window):

  if numpy.sum(labels)< smoothing_window:
    return labels
  segments = []
  for k in range(1,len(labels)-1):
    if labels[k]==0 and labels[k-1]==1 and labels[k+1]==1 :
      labels[k]=1
  for k in range(1,len(labels)-1):
    if labels[k]==1 and labels[k-1]==0 and labels[k+1]==0 :
      labels[k]=0

  seg = numpy.array([0,0,labels[0]])
  for k in range(1,len(labels)):
    if labels[k] != labels[k-1]:
      seg[1]=k-1
      segments.append(seg)
      seg = numpy.array([k,k,labels[k]])
  seg[1]=len(labels)-1
  segments.append(seg)

  if len(segments) < 2:
    return labels

  curr = segments[0]
  next = segments[1]

  # Look at the first segment. If it's short enough, just change its labels
  if (curr[1]-curr[0]+1) < smoothing_window and (next[1]-next[0]+1) > smoothing_window:
    if curr[2]==1:
      labels[curr[0] : (curr[1]+1)] = numpy.zeros(curr[1] - curr[0] + 1)
      curr[2]=0
    else: #curr[2]==0
      labels[curr[0] : (curr[1]+1)] = numpy.ones(curr[1] - curr[0] + 1)
      curr[2]=1

  for k in range(1,len(segments)-1):
    prev = segments[k-1]
    curr = segments[k]
    next = segments[k+1]

    if (curr[1]-curr[0]+1) < smoothing_window and (prev[1]-prev[0]+1) > smoothing_window and (next[1]-next[0]+1) > smoothing_window:
      if curr[2]==1:
        labels[curr[0] : (curr[1]+1)] = numpy.zeros(curr[1] - curr[0] + 1)
        curr[2]=0
      else: #curr[2]==0
        labels[curr[0] : (curr[1]+1)] = numpy.ones(curr[1] - curr[0] + 1)
        curr[2]=1

  prev = segments[-2]
  curr = segments[-1]

  if (curr[1]-curr[0]+1) < smoothing_window and (prev[1]-prev[0]+1) > smoothing_window:
    if curr[2]==1:
      labels[curr[0] : (curr[1]+1)] = numpy.zeros(curr[1] - curr[0] + 1)
      curr[2]=0
    else: #if curr[2]==0
      labels[curr[0] : (curr[1]+1)] = numpy.ones(curr[1] - curr[0] + 1)
      curr[2]=1

  return labels

Response: Applies a smoothing on VAD


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _conversion(self, input_signal, vad_file):

    e = bob.ap.Energy(rate_wavsample[0], self.win_length_ms, self.win_shift_ms)
    energy_array = e(rate_wavsample[1])
    labels = self.use_existing_vad(energy_array, vad_file)

    return labels

Response: Convert an external VAD to a Spear label.


Instruction:
Create a Python 3 function to
compute and returns the 4Hz modulation energy features for the given input wave file

Response:
def mod_4hz(self, rate_wavsample):
    """Computes and returns the 4Hz modulation energy features for the given input wave file"""

    # Set parameters
    wl = self.win_length_ms
    ws = self.win_shift_ms
    nf = self.n_filters
    f_min = self.f_min
    f_max = self.f_max
    pre = self.pre_emphasis_coef

    c = bob.ap.Spectrogram(rate_wavsample[0], wl, ws, nf, f_min, f_max, pre)
    c.energy_filter=True
    c.log_filter=False
    c.energy_bands=True

    sig =  rate_wavsample[1]
    energy_bands = c(sig)
    filtering_res = self.pass_band_filtering(energy_bands, rate_wavsample[0])
    mod_4hz = self.modulation_4hz(filtering_res, rate_wavsample)
    mod_4hz = self.averaging(mod_4hz)
    e = bob.ap.Energy(rate_wavsample[0], wl, ws)
    energy_array = e(rate_wavsample[1])
    labels = self._voice_activity_detection(energy_array, mod_4hz)
    labels = utils.smoothing(labels,self.smoothing_window) # discard isolated speech less than 100ms
    logger.info("After Mod-4Hz based VAD there are %d frames remaining over %d", numpy.sum(labels), len(labels))
    return labels, energy_array, mod_4hz


Instruction:
Can you tell what is the following Python 3 function doing
def read_matlab_files(self, biofile, directory, extension):
        import bob.io.matlab
        # return the numpy array read from the data_file
        data_path = biofile.make_path(directory, extension)
        return bob.io.base.load(data_path)

Response: Read pre - computed CQCC Matlab features here


Instruction:
Here you have a function in Python 3, explain what it does
def write_data(self, data, data_file, compression=0):
    f = bob.io.base.HDF5File(data_file, 'w')
    f.set("rate", data[0], compression=compression)
    f.set("data", data[1], compression=compression)
    f.set("labels", data[2], compression=compression)

Response: Writes the given preprocessed data to a file with the given name.


Instruction:
Explain what the following Python 3 code does
def command_line_arguments(command_line_parameters):

  # create parser
  parser = argparse.ArgumentParser(description='Execute baseline algorithms with default parameters', formatter_class=argparse.ArgumentDefaultsHelpFormatter)

  # add parameters
  # - the algorithm to execute
  parser.add_argument('-a', '--algorithms', choices = all_algorithms, default = ('gmm-voxforge',), nargs = '+', help = 'Select one (or more) algorithms that you want to execute.')
  parser.add_argument('--all', action = 'store_true', help = 'Select all algorithms.')
  # - the database to choose
  parser.add_argument('-d', '--database', choices = available_databases, default = 'voxforge', help = 'The database on which the baseline algorithm is executed.')
  # - the database to choose
  parser.add_argument('-b', '--baseline-directory', default = 'baselines', help = 'The sub-directory, where the baseline results are stored.')
  # - the directory to write
  parser.add_argument('-f', '--directory', help = 'The directory to write the data of the experiment into. If not specified, the default directories of the verify.py script are used (see verify.py --help).')

  # - use the Idiap grid -- option is only useful if you are at Idiap
  parser.add_argument('-g', '--grid', action = 'store_true', help = 'Execute the algorithm in the SGE grid.')
  # - run in parallel on the local machine
  parser.add_argument('-l', '--parallel', type=int, help = 'Run the algorithms in parallel on the local machine, using the given number of parallel threads')
  # - perform ZT-normalization
  parser.add_argument('-z', '--zt-norm', action = 'store_false', help = 'Compute the ZT norm for the files (might not be availabe for all databases).')

  # - just print?
  parser.add_argument('-q', '--dry-run', action = 'store_true', help = 'Just print the commands, but do not execute them.')

  # - evaluate the algorithm (after it has finished)
  parser.add_argument('-e', '--evaluate', nargs='+', choices = ('EER', 'HTER', 'ROC', 'DET', 'CMC', 'RR'), help = 'Evaluate the results of the algorithms (instead of running them) using the given evaluation techniques.')
  # TODO: add MIN-DCT measure
  # - other parameters that are passed to the underlying script
  parser.add_argument('parameters', nargs = argparse.REMAINDER, help = 'Parameters directly passed to the verify.py script.')

  bob.core.log.add_command_line_option(parser)
  args = parser.parse_args(command_line_parameters)
  if args.all:
    args.algorithms = all_algorithms

  bob.core.log.set_verbosity_level(logger, args.verbose)

  return args

Response: Defines the command line arguments that are accepted by the user.


Instruction:
Can you create a Python 3 function that
computes the energy of the audio sample

Response:
def _compute_energy(self, rate_wavsample):
    """retreive the speech / non speech labels for the speech sample given by the tuple (rate, wave signal)"""

    e = bob.ap.Energy(rate_wavsample[0], self.win_length_ms, self.win_shift_ms)
    energy_array = e(rate_wavsample[1])
    labels = self._voice_activity_detection(energy_array)
    # discard isolated speech a number of frames defined in smoothing_window
    labels = utils.smoothing(labels,self.smoothing_window)

    logger.info("After 2 Gaussian Energy-based VAD there are %d frames remaining over %d", numpy.sum(labels), len(labels))
    return labels


Instruction:
Here you have a function in Python 3, explain what it does
def calc_mean(c0, c1=[]):
    if c1 != []:
        return (numpy.mean(c0, 0) + numpy.mean(c1, 0)) / 2.
    else:
        return numpy.mean(c0, 0)

Response: Calculates the mean of the data.


Instruction:
How would you explain what the following Python 3 function does
def calc_std(c0, c1=[]):
    if c1 == []:
        return numpy.std(c0, 0)
    prop = float(len(c0)) / float(len(c1))
    if prop < 1:
        p0 = int(math.ceil(1 / prop))
        p1 = 1
    else:
        p0 = 1
        p1 = int(math.ceil(prop))
    return numpy.std(numpy.vstack(p0 * [c0] + p1 * [c1]), 0)

Response: Calculates the variance of the data.


Instruction:
Can you tell what is the following Python 3 function doing
def calc_mean_std(c0, c1=[], nonStdZero=False):
    mi = calc_mean(c0, c1)
    std = calc_std(c0, c1)
    if (nonStdZero):
        std[std == 0] = 1

    return mi, std

Response: Calculates both the mean and standard deviation of the data.


Instruction:
How would you explain what the following Python 3 function does
def vad_filter_features(vad_labels, features, filter_frames="trim_silence"):

    if not features.size:
        raise ValueError("vad_filter_features(): data sample is empty, no features extraction is possible")

    vad_labels = numpy.asarray(vad_labels, dtype=numpy.int8)
    features = numpy.asarray(features, dtype=numpy.float64)
    features = numpy.reshape(features, (vad_labels.shape[0], -1))

    #    logger.info("RatioVectorExtractor, vad_labels shape: %s", str(vad_labels.shape))
    #    print ("RatioVectorExtractor, features max: %f and min: %f" %(numpy.max(features), numpy.min(features)))

    # first, take the whole thing, in case there are problems later
    filtered_features = features

    # if VAD detection worked on this sample
    if vad_labels is not None and filter_frames != "no_filter":
        # make sure the size of VAD labels and sectrogram lenght match
        if len(vad_labels) == len(features):

            # take only speech frames, as in VAD speech frames are 1 and silence are 0
            speech, = numpy.nonzero(vad_labels)
            silences = None
            if filter_frames == "silence_only":
                # take only silent frames - those for which VAD gave zeros
                silences, = numpy.nonzero(vad_labels == 0)

            if len(speech):
                nzstart = speech[0]  # index of the first non-zero
                nzend = speech[-1]  # index of the last non-zero

                if filter_frames == "silence_only":  # extract only silent frames
                    # take only silent frames in-between the speech
                    silences = silences[silences > nzstart]
                    silences = silences[silences < nzend]
                    filtered_features = features[silences, :]
                elif filter_frames == "speech_only":
                    filtered_features = features[speech, :]
                else:  # when we take all
                    filtered_features = features[nzstart:nzend + 1, :]  # numpy slicing is a non-closed interval [)
        else:
            logger.error("vad_filter_features(): VAD labels should be the same length as energy bands")

        logger.info("vad_filter_features(): filtered_features shape: %s", str(filtered_features.shape))

    return filtered_features

Response: Filter the features of a VAD file.


Instruction:
Can you tell what is the following Python 3 function doing
def solve_sweep_structure(
        self,
        structures,
        sweep_param_list,
        filename="structure_n_effs.dat",
        plot=True,
        x_label="Structure number",
        fraction_mode_list=[],
    ):
        """
        Find the modes of many structures.

        Args:
            structures (list): A list of `Structures` to find the modes
                of.
            sweep_param_list (list): A list of the parameter-sweep sweep
                that was used.  This is for plotting purposes only.
            filename (str): The nominal filename to use when saving the
                effective indices.  Defaults to 'structure_n_effs.dat'.
            plot (bool): `True` if plots should be generates,
                otherwise `False`.  Default is `True`.
            x_label (str): x-axis text to display in the plot.
            fraction_mode_list (list): A list of mode indices of the modes
                that should be included in the TE/TM mode fraction plot.
                If the list is empty, all modes will be included.  The list
                is empty by default.

        Returns:
            list: A list of the effective indices found for each structure.
        """
        n_effs = []
        mode_types = []
        fractions_te = []
        fractions_tm = []
        for s in tqdm.tqdm(structures, ncols=70):
            self.solve(s)
            n_effs.append(np.real(self.n_effs))
            mode_types.append(self._get_mode_types())
            fractions_te.append(self.fraction_te)
            fractions_tm.append(self.fraction_tm)

        if filename:
            self._write_n_effs_to_file(
                n_effs, self._modes_directory + filename, sweep_param_list
            )

            with open(self._modes_directory + "mode_types.dat", "w") as fs:
                header = ",".join(
                    "Mode%i" % i for i, _ in enumerate(mode_types[0])
                )
                fs.write("# " + header + "\n")
                for mt in mode_types:
                    txt = ",".join("%s %.2f" % pair for pair in mt)
                    fs.write(txt + "\n")

            with open(self._modes_directory + "fraction_te.dat", "w") as fs:
                header = "fraction te"
                fs.write("# param sweep," + header + "\n")
                for param, fte in zip(sweep_param_list, fractions_te):
                    txt = "%.6f," % param
                    txt += ",".join("%.2f" % f for f in fte)
                    fs.write(txt + "\n")

            with open(self._modes_directory + "fraction_tm.dat", "w") as fs:
                header = "fraction tm"
                fs.write("# param sweep," + header + "\n")
                for param, ftm in zip(sweep_param_list, fractions_tm):
                    txt = "%.6f," % param
                    txt += ",".join("%.2f" % f for f in ftm)
                    fs.write(txt + "\n")

            if plot:
                if MPL:
                    title = "$n_{eff}$ vs %s" % x_label
                    y_label = "$n_{eff}$"
                else:
                    title = "n_{effs} vs %s" % x_label
                    y_label = "n_{eff}"
                self._plot_n_effs(
                    self._modes_directory + filename, self._modes_directory + "fraction_te.dat", x_label, y_label, title
                )

                title = "TE Fraction vs %s" % x_label
                self._plot_fraction(
                    self._modes_directory + "fraction_te.dat",
                    x_label,
                    "TE Fraction [%]",
                    title,
                    fraction_mode_list,
                )

                title = "TM Fraction vs %s" % x_label
                self._plot_fraction(
                    self._modes_directory + "fraction_tm.dat",
                    x_label,
                    "TM Fraction [%]",
                    title,
                    fraction_mode_list,
                )

        return n_effs

Response: Solve the structures and save the effective indices in the mode types.


Instruction:
Can you generate the documentation for the following Python 3 function
def solve_sweep_wavelength(
        self,
        structure,
        wavelengths,
        filename="wavelength_n_effs.dat",
        plot=True,
    ):
        """
        Solve for the effective indices of a fixed structure at
        different wavelengths.

        Args:
            structure (Slabs): The target structure to solve
                for modes.
            wavelengths (list): A list of wavelengths to sweep
                over.
            filename (str): The nominal filename to use when saving the
                effective indices.  Defaults to 'wavelength_n_effs.dat'.
            plot (bool): `True` if plots should be generates,
                otherwise `False`.  Default is `True`.

        Returns:
            list: A list of the effective indices found for each wavelength.
        """
        n_effs = []
        for w in tqdm.tqdm(wavelengths, ncols=70):
            structure.change_wavelength(w)
            self.solve(structure)
            n_effs.append(np.real(self.n_effs))

        if filename:
            self._write_n_effs_to_file(
                n_effs, self._modes_directory + filename, wavelengths
            )
            if plot:
                if MPL:
                    title = "$n_{eff}$ vs Wavelength"
                    y_label = "$n_{eff}$"
                else:
                    title = "n_{effs} vs Wavelength" % x_label
                    y_label = "n_{eff}"
                self._plot_n_effs(
                    self._modes_directory + filename,
                    self._modes_directory + "fraction_te.dat",
                    "Wavelength",
                    "n_{eff}",
                    title,
                )

        return n_effs

Response:
Solve for the effective indices of a fixed structure at
        different wavelengths.


Instruction:
Here you have a function in Python 3, explain what it does
def solve_ng(self, structure, wavelength_step=0.01, filename="ng.dat"):
        wl_nom = structure._wl

        self.solve(structure)
        n_ctrs = self.n_effs

        structure.change_wavelength(wl_nom - wavelength_step)
        self.solve(structure)
        n_bcks = self.n_effs

        structure.change_wavelength(wl_nom + wavelength_step)
        self.solve(structure)
        n_frws = self.n_effs

        n_gs = []
        for n_ctr, n_bck, n_frw in zip(n_ctrs, n_bcks, n_frws):
            n_gs.append(
                n_ctr - wl_nom * (n_frw - n_bck) / (2 * wavelength_step)
            )

        if filename:
            with open(self._modes_directory + filename, "w") as fs:
                fs.write("# Mode idx, Group index\n")
                for idx, n_g in enumerate(n_gs):
                    fs.write("%i,%.3f\n" % (idx, np.round(n_g.real, 3)))

        return n_gs

Response: r Solve for the group index and the group index of a structure at a particular wavelength.


Instruction:
Create a Python 3 function for
writing the mode fields to a file and optionally plots them.

Response:
def write_modes_to_file(self, filename="mode.dat", plot=True, analyse=True):
        """
        Writes the mode fields to a file and optionally plots them.

        Args:
            filename (str): The nominal filename to use for the saved
                data.  The suffix will be automatically be changed to
                identifiy each mode number.  Default is 'mode.dat'
            plot (bool): `True` if plots should be generates,
                otherwise `False`.  Default is `True`.
            analyse (bool): `True` if an analysis on the fundamental
                mode should be performed.  The analysis adds to the
                plot of the fundamental mode the power mode-field
                diameter (MFD) and marks it on the output, and it
                marks with a cross the maximum E-field value.
                Default is `True`.

        Returns:
            dict: A dictionary containing the effective indices
            and mode field profiles (if solved for).
        """
        modes_directory = "./modes_semi_vec/"
        if not os.path.isdir(modes_directory):
            os.mkdir(modes_directory)
        filename = modes_directory + filename

        for i, mode in enumerate(self._ms.modes):
            filename_mode = self._get_mode_filename(
                self._semi_vectorial_method, i, filename
            )
            self._write_mode_to_file(np.real(mode), filename_mode)

            if plot:
                if i == 0 and analyse:
                    A, centre, sigma_2 = anal.fit_gaussian(
                        self._structure.xc, self._structure.yc, np.abs(mode)
                    )
                    subtitle = (
                        "E_{max} = %.3f, (x_{max}, y_{max}) = (%.3f, %.3f), MFD_{x} = %.3f, "
                        "MFD_{y} = %.3f"
                    ) % (A, centre[0], centre[1], sigma_2[0], sigma_2[1])
                    self._plot_mode(
                        self._semi_vectorial_method,
                        i,
                        filename_mode,
                        self.n_effs[i],
                        subtitle,
                        sigma_2[0],
                        sigma_2[1],
                        centre[0],
                        centre[1],
                        wavelength=self._structure._wl,
                    )
                else:
                    self._plot_mode(
                        self._semi_vectorial_method,
                        i,
                        filename_mode,
                        self.n_effs[i],
                        wavelength=self._structure._wl,
                    )

        return self.modes


Instruction:
Can you write a function in Python 3 where it
writes the mode fields to a file.

Response:
def write_modes_to_file(
        self,
        filename="mode.dat",
        plot=True,
        fields_to_write=("Ex", "Ey", "Ez", "Hx", "Hy", "Hz"),
    ):
        """
        Writes the mode fields to a file and optionally plots them.

        Args:
            filename (str): The nominal filename to use for the saved
                data.  The suffix will be automatically be changed to
                identifiy each field and mode number.  Default is
                'mode.dat'
            plot (bool): `True` if plots should be generates,
                otherwise `False`.  Default is `True`.
            fields_to_write (tuple): A tuple of strings where the
                strings can be 'Ex', 'Ey', 'Ez', 'Hx', 'Hy' and 'Hz'
                defining what part of the mode should be saved and
                plotted.  By default, all six components are written
                and plotted.

        Returns:
            dict: A dictionary containing the effective indices
            and mode field profiles (if solved for).
        """
        modes_directory = self._modes_directory

        # Mode info file.
        with open(modes_directory + "mode_info", "w") as fs:
            fs.write("# Mode idx, Mode type, % in major direction, n_eff\n")
            for i, (n_eff, (mode_type, percentage)) in enumerate(
                zip(self.n_effs, self.mode_types)
            ):
                mode_idx = str(i)
                line = "%s,%s,%.2f,%.3f" % (
                    mode_idx,
                    mode_type,
                    percentage,
                    n_eff.real,
                )
                fs.write(line + "\n")

        # Mode field plots.
        for i, (mode, areas) in enumerate(zip(self._ms.modes, self.overlaps)):
            mode_directory = "%smode_%i/" % (modes_directory, i)
            if not os.path.isdir(mode_directory):
                os.mkdir(mode_directory)
            filename_full = mode_directory + filename

            for (field_name, field_profile), area in zip(
                mode.fields.items(), areas
            ):
                if field_name in fields_to_write:
                    filename_mode = self._get_mode_filename(
                        field_name, i, filename_full
                    )
                    self._write_mode_to_file(
                        np.real(field_profile), filename_mode
                    )
                    if plot:
                        self._plot_mode(
                            field_name,
                            i,
                            filename_mode,
                            self.n_effs[i],
                            area=area,
                            wavelength=self._structure._wl,
                        )

        return self.modes


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_required(field, render_kw=None, force=False):
    if render_kw is None:
        render_kw = {}
    if 'required' in render_kw and not force:
        return render_kw
    if field.flags.required:
        render_kw['required'] = True
    return render_kw

Response: Sets the required key for the given field.


Instruction:
Can you create a Python 3 function that
sets the invalid CSS class for the a ArcGIS field.

Response:
def set_invalid(field, render_kw=None):
    """
    Returns *render_kw* with `invalid` added to *class* on validation errors.

    Set (or appends) 'invalid' to the fields CSS class(es), if the *field* got
    any errors. 'invalid' is also set by browsers if they detect errors on a
    field.

    """
    if render_kw is None:
        render_kw = {}
    if field.errors:
        classes = render_kw.get('class') or render_kw.pop('class_', '')
        if classes:
            render_kw['class'] = 'invalid {}'.format(classes)
        else:
            render_kw['class'] = 'invalid'
    return render_kw


Instruction:
Write a Python 3 script for
setting min and max keys for a SequenceField.

Response:
def set_minmax(field, render_kw=None, force=False):
    """
    Returns *render_kw* with *min* and *max* set if validators use them.

    Sets *min* and / or *max* keys if a `Length` or `NumberRange` validator is
    using them.

    ..note::

        This won't change keys already present unless *force* is used.

    """
    if render_kw is None:
        render_kw = {}
    for validator in field.validators:
        if isinstance(validator, MINMAX_VALIDATORS):
            if 'min' not in render_kw or force:
                v_min = getattr(validator, 'min', -1)
                if v_min not in (-1, None):
                    render_kw['min'] = v_min
            if 'max' not in render_kw or force:
                v_max = getattr(validator, 'max', -1)
                if v_max not in (-1, None):
                    render_kw['max'] = v_max
    return render_kw


Instruction:
Create a Python 3 function for
setting the title of the page.

Response:
def set_title(field, render_kw=None):
    """
    Returns *render_kw* with *min* and *max* set if required.

    If the field got a *description* but no *title* key is set, the *title* is
    set to *description*.

    """
    if render_kw is None:
        render_kw = {}
    if 'title' not in render_kw and getattr(field, 'description'):
        render_kw['title'] = '{}'.format(field.description)
    return render_kw


Instruction:
Write a Python 3 script for
returning a dictionary containing keyword arguments for the HTML5 language.

Response:
def get_html5_kwargs(field, render_kw=None, force=False):
    """
    Returns a copy of *render_kw*  with keys added for a bound *field*.

    If some *render_kw* are given, the new keys are added to a copy of them,
    which is then returned. If none are given, a dictionary containing only
    the automatically generated keys is returned.

    .. important::

        This might add new keys but won't changes any values if a key is
        already in *render_kw*,  unless *force* is used.

    Raises:

        ValueError: if *field* is an :cls:`UnboundField`.

    The following keys are set automatically:

    :required:
        Sets the *required* key if the `required` flag is set for the
        field (this is mostly the case if it is set by validators). The
        `required` attribute is used by browsers to indicate a required field.

    :invalid:
        Set (or appends) 'invalid' to the fields CSS class(es), if the *field*
        got any errors. 'invalid' is also set by browsers if they detect
        errors on a field.

    :min / max:
        Sets *min* and / or *max* keys if a `Length` or `NumberRange`
        validator is using them.

    :title:
        If the field got a *description* but no *title* key is set, the
        *title* is set to *description*.

    """
    if isinstance(field, UnboundField):
        msg = 'This function needs a bound field not: {}'
        raise ValueError(msg.format(field))
    kwargs = render_kw.copy() if render_kw else {}
    kwargs = set_required(field, kwargs, force)  # is field required?
    kwargs = set_invalid(field, kwargs)  # is field invalid?
    kwargs = set_minmax(field, kwargs, force)  # check validators for min/max
    kwargs = set_title(field, kwargs)  # missing tile?
    return kwargs


Instruction:
Write a Python 3 script for
returning the rendered field after adding auto–attributes.

Response:
def render_field(self, field, render_kw):
        """
        Returns the rendered field after adding auto–attributes.

        Calls the field`s widget with the following kwargs:

        1. the *render_kw* set on the field are used as based
        2. and are updated with the *render_kw* arguments from the render call
        3. this is used as an argument for a call to `get_html5_kwargs`
        4. the return value of the call is used as final *render_kw*

        """
        field_kw = getattr(field, 'render_kw', None)
        if field_kw is not None:
            render_kw = dict(field_kw, **render_kw)
        render_kw = get_html5_kwargs(field, render_kw)
        return field.widget(field, **render_kw)


Instruction:
Make a summary of the following Python 3 code
def x(self):
        '''
        np.array: The grid points in x.
        '''
        if None not in (self.x_min, self.x_max, self.x_step) and \
                self.x_min != self.x_max:
            x = np.arange(self.x_min, self.x_max+self.x_step-self.y_step*0.1, self.x_step)
        else:
            x = np.array([])
        return x

Response: np. array : The grid points in x.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def y(self):
        '''
        np.array: The grid points in y.
        '''
        if None not in (self.y_min, self.y_max, self.y_step) and \
                self.y_min != self.y_max:
            y = np.arange(self.y_min, self.y_max-self.y_step*0.1, self.y_step)
        else:
            y = np.array([])
        return y

Response: np. array : The grid points in y.


Instruction:
How would you explain what the following Python 3 function does
def eps_func(self):
        '''
        function: a function that when passed a `x` and `y` values,
            returns the permittivity profile of the structure,
            interpolating if necessary.
        '''
        interp_real = interpolate.interp2d(self.x, self.y, self.eps.real)
        interp_imag = interpolate.interp2d(self.x, self.y, self.eps.imag)
        interp = lambda x, y: interp_real(x, y) + 1.j*interp_imag(x, y)
        return interp

Response: function that returns the permittivity profile of the structure


Instruction:
Given the following Python 3 function, write the documentation
def n_func(self):
        '''
        function: a function that when passed a `x` and `y` values,
            returns the refractive index profile of the structure,
            interpolating if necessary.
        '''
        return interpolate.interp2d(self.x, self.y, self.n)

Response: function that returns the refractive index profile of the structure


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write_to_file(self, filename='material_index.dat', plot=True):
        '''
        Write the refractive index profile to file.

        Args:
            filename (str): The nominal filename the refractive
                index data should be saved to.
            plot (bool): `True` if plots should be generates,
                otherwise `False`.  Default is `True`.
        '''
        path = os.path.dirname(sys.modules[__name__].__file__) + '/'

        with open(filename, 'w') as fs:
            for n_row in np.abs(self.n[::-1]):
                n_str = ','.join([str(v) for v in n_row])
                fs.write(n_str+'\n')

        if plot:
            filename_image_prefix, _ = os.path.splitext(filename)
            filename_image = filename_image_prefix + '.png'
            args = {
                'title': 'Refractive Index Profile',
                'x_pts': self.x_pts,
                'y_pts': self.y_pts,
                'x_min': self.x_min,
                'x_max': self.x_max,
                'y_min': self.y_min,
                'y_max': self.y_max,
                'filename_data': filename,
                'filename_image': filename_image
            }

            if MPL:
                heatmap = np.loadtxt(args['filename_data'], delimiter=',')
                plt.clf()
                plt.title(args['title'])
                plt.xlabel('$x$')
                plt.ylabel('$y$')
                plt.imshow(np.flipud(heatmap),
                           extent=(args['x_min'], args['x_max'], args['y_min'], args['y_max']),
                           aspect="auto")
                plt.colorbar()
                plt.savefig(filename_image)
            else:
                gp.gnuplot(path+'structure.gpi', args)

Response: Writes the refractive index profile to file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_slab(self, height, n_background=1., position='top'):
        '''
        Creates and adds a :class:`Slab` object.

        Args:
            height (float): Height of the slab.
            n_background (float): The nominal refractive
                index of the slab.  Default is 1 (air).

        Returns:
            str: The name of the slab.
        '''
        assert position in ('top', 'bottom')

        name = str(self.slab_count)

        if not callable(n_background):
            n_back = lambda wl: n_background
        else:
            n_back = n_background

        height_discretised = self.y_step*((height // self.y_step) + 1)

        y_min = self._next_start
        y_max = y_min + height_discretised
        self.slabs[name] = Slab(name, self.x_step, self.y_step, self.x_max,
                                 y_max, self.x_min, y_min, n_back, self._wl)

        self.y_max = y_max
        self._next_start = y_min + height_discretised
        self.slab_count += 1

        if position == 'bottom':
            slabs = {}
            for k in self.slabs.keys():
                slabs[str(int(k)+1)] = self.slabs[k]
            slabs['0'] = slabs.pop(str(self.slab_count))
            self.slabs = slabs

        return name

Response: Creates and adds a new slab object.


Instruction:
Implement a function in Python 3 to
change the wavelength of the structure.

Response:
def change_wavelength(self, wavelength):
        '''
        Changes the wavelength of the structure.

        This will affect the mode solver and potentially
        the refractive indices used (provided functions
        were provided as refractive indices).

        Args:
            wavelength (float): The new wavelength.
        '''
        for name, slab in self.slabs.items():
            const_args = slab._const_args
            mat_args = slab._mat_params

            const_args[8] = wavelength

            s = Slab(*const_args)
            for mat_arg in mat_args:
                s.add_material(*mat_arg)

            self.slabs[name] = s

        self._wl = wavelength


Instruction:
Can you tell what is the following Python 3 function doing
def n(self):
        '''
        np.array: The refractive index profile matrix
        of the current slab.
        '''
        try:
            n_mat = self.slabs['0'].n
            for s in range(1, self.slab_count):
                n_mat = np.vstack((self.slabs[str(s)].n, n_mat))
        except KeyError:
            n_mat = None
        return n_mat

Response: Return the refractive index profile matrix of the current slab.


Instruction:
How would you code a function in Python 3 to
add a material to the structure.

Response:
def add_material(self, x_min, x_max, n, angle=0):
        '''
        Add a refractive index between two x-points.

        Args:
            x_min (float): The start x-point.
            x_max (float): The stop x-point.
            n (float, function):  Refractive index between
                 `x_min` and `x_max`.  Either a constant (`float`), or
                 a function that accepts one parameters, the
                 wavelength, and returns a float of the refractive
                 index.  This is useful when doing wavelength
                 sweeps and solving for the group velocity.  The
                 function provided could be a Sellmeier equation.
            angle (float): Angle in degrees of the slope of the
                sidewalls at `x_min` and `x_max`.  This is useful
                for defining a ridge with angled sidewalls.
        '''
        self._mat_params.append([x_min, x_max, n, angle])

        if not callable(n):
            n_mat = lambda wl: n
        else:
            n_mat = n

        Structure._add_material(self, x_min, self.y_min, x_max, self.y_max, n_mat(self._wl), angle)
        return self.n


Instruction:
Write a Python 3 function for
writing the refractive index profile to file.

Response:
def write_to_file(self, filename='material_index.dat', plot=True):
        '''
        Write the refractive index profile to file.

        Args:
            filename (str): The nominal filename the refractive
                index data should be saved to.
            plot (bool): `True` if plots should be generates,
                otherwise `False`.  Default is `True`.
        '''
        path = os.path.dirname(sys.modules[__name__].__file__) + '/'

        dir_plot = 'material_index/'
        if not os.path.exists(dir_plot):
            os.makedirs(dir_plot)

        for axis, name in zip(self.axes, self.axes_str):
            root, ext = os.path.splitext(filename)
            fn = dir_plot + root + '_'+ name + ext
            with open(fn, 'w') as fs:
                for n_row in np.abs(axis.n[::-1]):
                    n_str = ','.join([str(v) for v in n_row])
                    fs.write(n_str+'\n')

            if plot:
                filename_image_prefix, _ = os.path.splitext(fn)
                filename_image = filename_image_prefix + '.png'
                args = {
                    'title': 'Refractive Index Profile: %s' % name,
                    'x_pts': self.xx.x_pts,
                    'y_pts': self.xx.y_pts,
                    'x_min': self.xx.x_min,
                    'x_max': self.xx.x_max,
                    'y_min': self.xx.y_min,
                    'y_max': self.xx.y_max,
                    'filename_data': fn,
                    'filename_image': filename_image
                }
                if MPL:
                    heatmap = np.loadtxt(args['filename_data'], delimiter=',')
                    plt.clf()
                    plt.title(args['title'])
                    plt.xlabel('$x$')
                    plt.ylabel('$y$')
                    plt.imshow(np.flipud(heatmap),
                               extent=(args['x_min'], args['x_max'], args['y_min'], args['y_max']),
                               aspect="auto")
                    plt.colorbar()
                    plt.savefig(filename_image)
                else:
                    gp.gnuplot(path+'structure.gpi', args, silent=False)


Instruction:
Write a Python 3 script for
changing the wavelength of the structure.

Response:
def change_wavelength(self, wavelength):
        '''
        Changes the wavelength of the structure.

        This will affect the mode solver and potentially
        the refractive indices used (provided functions
        were provided as refractive indices).

        Args:
            wavelength (float): The new wavelength.
        '''
        for axis in self.axes:
            if issubclass(type(axis), Slabs):
                axis.change_wavelength(wavelength)
        self.xx, self.xy, self.yx, self.yy, self.zz = self.axes
        self._wl = wavelength


Instruction:
Can you generate a brief explanation for the following Python 3 code
def covstr(s):
  try:
    ret = int(s)
  except ValueError:
    ret = float(s)
  return ret

Response: convert string to int or float.


Instruction:
How would you explain what the following Python 3 function does
def output(self):
    #re = "{%(time)s} %(name)s %(stock_no)s %(c)s %(range)+.2f(%(pp)+.2f%%) %(value)s" % {
    '''
    re = """

%(name)s	%(c)s	%(range)+.2f(%(pp)+.2f%%)

%(stock_no)s	%(value)s	%(time)s

""" % {
    '''
    if covstr(self.g['range']) > 0:
      css = "red"
    elif covstr(self.g['range']) < 0:
      css = "green"
    else:
      css = "gray"

    re = {
      'name': self.g['name'],
      'stock_no': self.g['no'],
      'time': self.g['time'],
      'open': self.g['open'],
      'h': self.g['h'],
      'l': self.g['l'],
      'c': self.g['c'],
      'max': self.g['max'],
      'min': self.g['min'],
      'range': covstr(self.g['range']),
      'ranges': self.g['ranges'],
      'value': self.g['value'],
      'pvalue': self.g['pvalue'],
      'pp': covstr(self.g['pp']),
      'top5buy': self.g['top5buy'],
      'top5sell': self.g['top5sell'],
      'crosspic': self.g['crosspic'],
      'css': css
    }
    return re

Response: Return the output string for the sequence of n - stocks.


Instruction:
Can you tell what is the following Python 3 function doing
def Rt_display(stock_no):
  a = twsk(stock_no).real
  if a:
    re = "{%(time)s} %(stock_no)s %(c)s %(range)+.2f(%(pp)+.2f%%) %(value)s" % {
        'stock_no': stock_no,
        'time': a['time'],
        'c': a['c'],
        'range': covstr(a['range']),
        'value': a['value'],
        'pp': covstr(a['pp'])
      }
    return re
  else:
    return a

Response:
For real time stock display
      即時盤用，顯示目前查詢各股的股的資訊。


Instruction:
Can you write a function in Python 3 where it
checks the value is date or not

Response:
def ckinv(self,oo):
    """ check the value is date or not
        檢查是否為日期格式
    """
    pattern = re.compile(r"[0-9]{2}/[0-9]{2}/[0-9]{2}")
    b = re.search(pattern, oo[0])
    try:
      b.group()
      return True
    except:
      return False


Instruction:
Implement a Python 3 function for
returning ↑↓ - for high or low or equal.

Response:
def high_or_low(self,one,two,rev=0):
    """ Return ↑↓- for high, low or equal.
        回傳漲跌標示
        rev = 0
          回傳 ↑↓-
        rev = 1
          回傳 1 -1 0
    """
    if rev == 0:
      if one > two:
        re = '↑'.decode('utf-8')
      elif one < two:
        re = '↓'.decode('utf-8')
      else:
        re = '-'.decode('utf-8')
    else:
      if one > two:
        re = 1
      elif one < two:
        re = -1
      else:
        re = 0
    return re


Instruction:
Can you generate a brief explanation for the following Python 3 code
def goback(self,days = 1):
    for i in xrange(days):
      self.raw_data.pop()
      self.data_date.pop()
      self.stock_range.pop()
      self.stock_vol.pop()
      self.stock_open.pop()
      self.stock_h.pop()
      self.stock_l.pop()

Response:
Go back days
        刪除最新天數資料數據
        days 代表刪除多少天數（倒退幾天）


Instruction:
Write a Python 3 function that can
fetch data from twse. com. tw

Response:
def fetch_data(self, stock_no, nowdatetime, firsttime = 1):
    """ Fetch data from twse.com.tw
        return list.
        從 twse.com.tw 下載資料，回傳格式為 list
    """
    url = 'http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report%(year)d%(mon)02d/%(year)d%(mon)02d_F3_1_8_%(stock)s.php&type=csv&r=%(rand)s' % {'year': nowdatetime.year, 'mon': nowdatetime.month, 'stock': stock_no, 'rand': random.randrange(1,1000000)}
    self.debug_print(url)
    logging.info(url)
    #print cc.info().headers

    # set memcache expire
    now = TWTime().now
    if now >= datetime(now.year, now.month, now.day, 14, 45):
      addday = 1
    else:
      addday = 0
    endtime = datetime(now.year, now.month, now.day, 14, 00) + timedelta(days = addday) ## change from 13:35 to 14:00
    logging.info('endtime: %s' % str(endtime))

    if firsttime == 0:
      if endtime <= now:
        expire = 'ALUP' ## always update.
      else:
        expire = (endtime - now).seconds
    else:
      expire = 0 ## never expire.
    logging.info('expire: %s' % expire)

    ## get memcache
    memname = '%(stock)s%(year)d%(mon)02d' % {'year': nowdatetime.year, 'mon': nowdatetime.month,'stock': stock_no}
    stkm = memcache.get(memname)
    if stkm:
      csv_read = csv.reader(stkm)
      logging.info('#MemcacheGet: %s' % memname)
    else:
      cc = urllib2.urlopen(url)
      cc_read = cc.readlines()
      csv_read = csv.reader(cc_read)
      if expire != 'ALUP':
        memcache.add(memname, cc_read, expire)
      else:
        memcache.delete(memname)
      memcache.add('time%s' % memname, '%s %s' % (now, expire))
      logging.info('#MemcacheAdd: %s' % memname)

    return csv_read


Instruction:
Write a Python 3 function that can
range percentage of the last two words

Response:
def range_per(self):
    """ Range percentage
        計算最新日之漲跌幅度百分比
    """
    rp = float((self.raw_data[-1] - self.raw_data[-2]) / self.raw_data[-2] * 100)
    return rp


Instruction:
How would you explain what the following Python 3 function does
def SD(self, days=45):
    if len(self.raw_data) >= days:
      data = self.raw_data[-days:]
      data_avg = float(sum(data) / days)
      data2 = []
      for x in data:
        data2.append((x - data_avg ) ** 2)

      return math.sqrt(sum(data2) / len(data2))
    else:
      return 0

Response:
Standard Deviation.
        計算 days 日內之標準差，預設 45 日


Instruction:
Can you tell what is the following Python 3 function doing
def SDAVG(self, days=45):
    if len(self.raw_data) >= days:
      data = self.raw_data[-days:]
      data_avg = float(sum(data) / days)
      return data_avg
    else:
      return 0

Response: the last 45 days average.