Instruction:
Can you tell what is the following Python 3 function doing
def get_mode(self, old_mode=None):
        if self.mode is not None:
            return self.mode
        assert self.can_write, "This format does not have a supported output mode."
        if old_mode is None:
            return self.output_modes[0]
        if old_mode in self.output_modes:
            return old_mode
        # now let's get best mode available from supported
        try:
            idx = PILLOW_MODES.index(old_mode)
        except ValueError:
            # maybe some unknown or uncommon mode
            return self.output_modes[0]
        for mode in PILLOW_MODES[idx+1:]:
            if mode in self.output_modes:
                return mode
        # since there is no better one, lets' look for closest one in opposite direction
        opposite = PILLOW_MODES[:idx]
        opposite.reverse()
        for mode in opposite:
            if mode in self.output_modes:
                return mode

Response: Returns output mode. If mode is not set it will try to guess best mode from old mode and if it is not available.


Instruction:
Write a Python 3 script for
finding the token present at the cursor position in the code buffer.

Response:
def token_at_cursor(code, pos=0):
    """
    Find the token present at the passed position in the code buffer
     :return (tuple): a pair (token, start_position)
    """
    l = len(code)
    end = start = pos
    # Go forwards while we get alphanumeric chars
    while end < l and code[end].isalpha():
        end += 1
    # Go backwards while we get alphanumeric chars
    while start > 0 and code[start-1].isalpha():
        start -= 1
    # If previous character is a %, add it (potential magic)
    if start > 0 and code[start-1] == '%':
        start -= 1
    return code[start:end], start


Instruction:
How would you explain what the following Python 3 function does
def _send(self, data, msg_type='ok', silent=False):
        # Data to send back
        if data is not None:
            # log the message
            try:
                self._klog.debug(u"msg to frontend (%d): %.160s...", silent, data)
            except Exception as e:
                self._klog.warn(u"can't log response: %s", e)
            # send it to the frontend
            if not silent:
                if msg_type != 'raw':
                    data = data_msg(data, mtype=msg_type)
                self.send_response(self.iopub_socket, 'display_data', data)

        # Result message
        return {'status': 'error' if msg_type == 'error' else 'ok',
                # The base class will increment the execution count
                'execution_count': self.execution_count,
                'payload': [],
                'user_expressions': {}
                }

Response: Send a response to the frontend and return an execute message


Instruction:
Here you have a function in Python 3, explain what it does
def do_execute(self, code, silent, store_history=True,
                   user_expressions=None, allow_stdin=False):
        """
        Method called to execute a cell
        """
        self._klog.info("[%.30s] [%d] [%s]", code, silent, user_expressions)

        # Split lines and remove empty lines & comments
        code_noc = [line.strip() for line in code.split('\n')
                    if line and line[0] != '#']
        if not code_noc:
            return self._send(None)

        # Process
        try:
            # Detect if we've got magics
            magic_lines = []
            for line in code_noc:
                if line[0] != '%':
                    break
                magic_lines.append(line)

            # Process magics. Once done, remove them from the query buffer
            if magic_lines:
                out = [self._k.magic(line) for line in magic_lines]
                self._send(out, 'multi', silent=silent)
                code = '\n'.join(code_noc[len(magic_lines):])

            # If we have a regular SPARQL query, process it now
            result = self._k.query(code, num=self.execution_count) if code else None

            # Return the result
            return self._send(result, 'raw', silent=silent)

        except Exception as e:
            return self._send(e, 'error', silent=silent)

Response: Execute a cell and return the result


Instruction:
Can you generate the documentation for the following Python 3 function
def escape( x, lb=False ):
    # Insert a linebreak? Roughly around the middle of the string,
    if lb:
        l = len(x)
        if l >= 10:
            l >>= 1                     # middle of the string
            s1 = x.find( ' ', l )       # first ws to the right
            s2 = x.rfind( ' ', 0, l )   # first ws to the left
            if s2 > 0:
                s = s2 if s1<0 or l-s1 > s2-l else s1
                x = x[:s] + '\\n' + x[s+1:]
            elif s1 > 0:
                x = x[:s1] + '\\n' + x[s1+1:]
    # Escape HTML reserved characters
    return x.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)

Response: Escape a string with HTML - reserved characters.


Instruction:
Here you have a function in Python 3, explain what it does
def div( txt, *args, **kwargs ):
    if args:
        txt = txt.format( *args )
    css = kwargs.get('css',HTML_DIV_CLASS)
    return u'
{!s}
'.format( css, txt )

Response: Create & return an HTML div element by wrapping the passed text buffer.


Instruction:
Create a Python 3 function to
return a Jupyter display_data message in both HTML & text formats by joining together all passed messages.

Response:
def data_msglist( msglist ):
    """
    Return a Jupyter display_data message, in both HTML & text formats, by
    joining together all passed messages.

      @param msglist (iterable): an iterable containing a list of tuples
        (message, css_style)

    Each message is either a text string, or a list. In the latter case it is
    assumed to be a format string + parameters.
    """
    txt = html = u''
    for msg, css in msglist:
        if is_collection(msg):
            msg = msg[0].format(*msg[1:])
        html += div( escape(msg).replace('\n','<br/>'), css=css or 'msg' )
        txt += msg + "\n"
    return { 'data': {'text/html' : div(html),
                      'text/plain' : txt },
             'metadata' : {} }


Instruction:
Write a Python 3 function for
returning a Jupyter display_data message in both HTML & text formats by

Response:
def data_msg( msg, mtype=None ):
    """
    Return a Jupyter display_data message, in both HTML & text formats, by
    formatting a given single message. The passed message may be:
      * An exception (including a KrnlException): will generate an error message
      * A list of messages (with \c mtype equal to \c multi)
      * A single message

      @param msg (str,list): a string, or a list of format string + args,
        or an iterable of (msg,mtype)
      @param mtype (str): the message type (used for the CSS class). If
        it's \c multi, then \c msg will be treated as a multi-message. If
        not passed, \c krn-error will be used for exceptions and \c msg for
        everything else
    """
    if isinstance(msg,KrnlException):
        return msg()    # a KrnlException knows how to format itself
    elif isinstance(msg,Exception):
        return KrnlException(msg)()
    elif mtype == 'multi':
        return data_msglist( msg )
    else:
        return data_msglist( [ (msg, mtype) ] )


Instruction:
Write a Python 3 script for
copying a resource file to a destination directory

Response:
def copyresource( resource, filename, destdir ):
    """
    Copy a resource file to a destination
    """
    data = pkgutil.get_data(resource, os.path.join('resources',filename) )
    #log.info( "Installing %s", os.path.join(destdir,filename) )
    with open( os.path.join(destdir,filename), 'wb' ) as fp:
        fp.write(data)


Instruction:
Make a summary of the following Python 3 code
def install_kernel_resources( destdir, resource=PKGNAME, files=None ):
    if files is None:
        files = ['logo-64x64.png', 'logo-32x32.png']
    for filename in files:
        try:
            copyresource( resource, filename, destdir )
        except Exception as e:
            sys.stderr.write(str(e))

Response: Copy the kernel resources to the kernelspec folder.


Instruction:
Can you generate the documentation for the following Python 3 function
def install_custom_css( destdir, cssfile, resource=PKGNAME ):
    ensure_dir_exists( destdir )
    custom = os.path.join( destdir, 'custom.css' )
    prefix = css_frame_prefix(resource)

    # Check if custom.css already includes it. If so, let's remove it first
    exists = False
    if os.path.exists( custom ):
        with io.open(custom) as f:
            for line in f:
                if line.find( prefix ) >= 0:
                    exists = True
                    break
    if exists:
        remove_custom_css( destdir, resource )

    # Fetch the CSS file
    cssfile += '.css'
    data = pkgutil.get_data( resource, os.path.join('resources',cssfile) )
    # get_data() delivers encoded data, str (Python2) or bytes (Python3)

    # Add the CSS at the beginning of custom.css
    # io.open uses unicode strings (unicode in Python2, str in Python3)
    with io.open(custom + '-new', 'wt', encoding='utf-8') as fout:
        fout.write( u'{}START ======================== */\n'.format(prefix))
        fout.write( data.decode('utf-8') )
        fout.write( u'{}END ======================== */\n'.format(prefix))
        if os.path.exists( custom ):
            with io.open( custom, 'rt', encoding='utf-8' ) as fin:
                for line in fin:
                    fout.write( unicode(line) )
    os.rename( custom+'-new',custom)

Response: Installs the kernel CSS file in the specified destination directory.


Instruction:
How would you explain what the following Python 3 function does
def remove_custom_css(destdir, resource=PKGNAME ):

    # Remove the inclusion in the main CSS
    if not os.path.isdir( destdir ):
        return False
    custom = os.path.join( destdir, 'custom.css' )
    copy = True
    found = False
    prefix = css_frame_prefix(resource)
    with io.open(custom + '-new', 'wt') as fout:
        with io.open(custom) as fin:
            for line in fin:
                if line.startswith( prefix + 'START' ):
                    copy = False
                    found = True
                elif line.startswith( prefix + 'END' ):
                    copy = True
                elif copy:
                    fout.write( line )

    if found:
        os.rename( custom+'-new',custom)
    else:
        os.unlink( custom+'-new')

    return found

Response: Remove the kernel CSS from custom. css


Instruction:
Write a Python 3 script for
formatting a result element as an HTML table cell.

Response:
def html_elem(e, ct, withtype=False):
    """
    Format a result element as an HTML table cell.
      @param e (list): a pair \c (value,type)
      @param ct (str): cell type (th or td)
      @param withtype (bool): add an additional cell with the element type
    """
    # Header cell
    if ct == 'th':
        return '{0}	{1}	'.format(*e) if withtype else '{}	'.format(e)
    # Content cell
    if e[1] in ('uri', 'URIRef'):
        html = u'<{0} class=val>{2}</{0}>'.format(ct, e[0], escape(e[0]))
    else:
        html = u'<{0} class=val>{1}</{0}>'.format(ct, escape(e[0]))
    # Create the optional cell for the type
    if withtype:
        html += u'<{0} class=typ>{1}</{0}>'.format(ct, e[1])
    return html


Instruction:
Explain what the following Python 3 code does
def html_table(data, header=True, limit=None, withtype=False):
    if header and limit:
        limit += 1
    ct = 'th' if header else 'td'
    rc = 'hdr' if header else 'odd'

    # import codecs
    # import datetime
    # with codecs.open( '/tmp/dump', 'w', encoding='utf-8') as f:
    #     print( '************', datetime.datetime.now(), file=f )
    #     for n, row in enumerate(data):
    #         print( '-------', n,  file=f )
    #         for n, c in enumerate(row):
    #             print( type(c), repr(c), file=f )

    html = u'
'
    rn = -1
    for rn, row in enumerate(data):
        html += u'
'.format(rc)
        html += '\n'.join((html_elem(c, ct, withtype) for c in row))
        html += u'
'
        rc = 'even' if rc == 'odd' else 'odd'
        ct = 'td'
        if limit:
            limit -= 1
            if not limit:
                break
    return (0, '') if rn < 0 else (rn+1-header, html+u'
')

Response: Returns a double iterable as an HTML table.


Instruction:
Make a summary of the following Python 3 code
def jtype(c):
    ct = c['type']
    return ct if ct != 'literal' else '{}, {}'.format(ct, c.get('xml:lang'))

Response: Returns the data type of a value for JSON data


Instruction:
Given the following Python 3 function, write the documentation
def gtype(n):
    t = type(n).__name__
    return str(t) if t != 'Literal' else 'Literal, {}'.format(n.language)

Response: Returns the data type of a value for Graph data


Instruction:
Can you generate a brief explanation for the following Python 3 code
def lang_match_json(row, hdr, accepted_languages):
    '''Find if the JSON row contains acceptable language data'''
    if not accepted_languages:
        return True
    languages = set([row[c].get('xml:lang') for c in hdr
                     if c in row and row[c]['type'] == 'literal'])
    return (not languages) or (languages & accepted_languages)

Response: Find if the JSON row contains acceptable language data


Instruction:
Can you create a Python 3 function that
finds if the RDF triple contains acceptable language data

Response:
def lang_match_rdf(triple, accepted_languages):
    '''Find if the RDF triple contains acceptable language data'''
    if not accepted_languages:
        return True
    languages = set([n.language for n in triple if isinstance(n, Literal)])
    return (not languages) or (languages & accepted_languages)


Instruction:
Here you have a function in Python 3, explain what it does
def lang_match_xml(row, accepted_languages):
    '''Find if the XML row contains acceptable language data'''
    if not accepted_languages:
        return True
    column_languages = set()
    for elem in row:
        lang = elem[0].attrib.get(XML_LANG, None)
        if lang:
            column_languages.add(lang)
    return (not column_languages) or (column_languages & accepted_languages)

Response: Find if the XML row contains acceptable language data


Instruction:
Here you have a function in Python 3, explain what it does
def json_iterator(hdr, rowlist, lang, add_vtype=False):
    # Return the header row
    yield hdr if not add_vtype else ((h, 'type') for h in hdr)
    # Now the data rows
    for row in rowlist:
        if lang and not lang_match_json(row, hdr, lang):
            continue
        yield ((row[c]['value'], jtype(row[c])) if c in row else ('', '')
               for c in hdr)

Response: Convert a JSON response into a double iterable by rows and columns


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rdf_iterator(graph, lang, add_vtype=False):
    # Return the header row
    hdr = ('subject', 'predicate', 'object')
    yield hdr if not add_vtype else ((h, 'type') for h in hdr)
    # Now the data rows
    for row in graph:
        if lang and not lang_match_rdf(row, lang):
            continue
        yield ((unicode(c), gtype(c)) for c in row)

Response: Convert a Graph response into a double iterable by triples and elements. lang - Language of the resource to be used. add_vtype - True to add type if not added to the header. lang - Language of the resource to be used to filter the data by language


Instruction:
Can you generate the documentation for the following Python 3 function
def render_json(result, cfg, **kwargs):
    result = json.loads(result.decode('utf-8'))
    head = result['head']
    if 'results' not in result:
        if 'boolean' in result:
            r = u'Result: {}'.format(result['boolean'])
        else:
            r = u'Unsupported result: \n' + unicode(result)
        return {'data': {'text/plain': r},
                'metadata': {}}

    vars = head['vars']
    nrow = len(result['results']['bindings'])
    if cfg.dis == 'table':
        j = json_iterator(vars, result['results']['bindings'], set(cfg.lan),
                          add_vtype=cfg.typ)
        n, data = html_table(j, limit=cfg.lmt, withtype=cfg.typ)
        data += div('Total: {}, Shown: {}', nrow, n, css="tinfo")
        data = {'text/html': div(data)}
    else:
        result = json.dumps(result,
                            ensure_ascii=False, indent=2, sort_keys=True)
        data = {'text/plain': unicode(result)}

    return {'data': data,
            'metadata': {}}

Response: Render to output a result in JSON format


Instruction:
Create a Python 3 function for
converting an XML response into a double iterable by rows and columns

Response:
def xml_iterator(columns, rowlist, lang, add_vtype=False):
    """
    Convert an XML response into a double iterable, by rows and columns
    Options are: filter triples by language (on literals), add element type
    """
    # Return the header row
    yield columns if not add_vtype else ((h, 'type') for h in columns)
    # Now the data rows
    for row in rowlist:
        if not lang_match_xml(row, lang):
            continue
        rowdata = {nam: val for nam, val in xml_row(row, lang)}
        yield (rowdata.get(field, ('', '')) for field in columns)


Instruction:
Implement a Python 3 function for
rendering to output a result in XML format

Response:
def render_xml(result, cfg, **kwargs):
    """
    Render to output a result in XML format
    """
    # Raw mode
    if cfg.dis == 'raw':
        return {'data': {'text/plain': result.decode('utf-8')},
                'metadata': {}}
    # Table
    try:
        import xml.etree.cElementTree as ET
    except ImportError:
        import xml.etree.ElementTree as ET
    root = ET.fromstring(result)
    try:
        ns = {'ns': re.match(r'\{([^}]+)\}', root.tag).group(1)}
    except Exception:
        raise KrnlException('Invalid XML data: cannot get namespace')
    columns = [c.attrib['name'] for c in root.find('ns:head', ns)]
    results = root.find('ns:results', ns)
    nrow = len(results)
    j = xml_iterator(columns, results, set(cfg.lan), add_vtype=cfg.typ)
    n, data = html_table(j, limit=cfg.lmt, withtype=cfg.typ)
    data += div('Total: {}, Shown: {}', nrow, n, css="tinfo")
    return {'data': {'text/html': div(data)},
            'metadata': {}}


Instruction:
How would you explain what the following Python 3 function does
def render_graph(result, cfg, **kwargs):
    # Mapping from MIME types to formats accepted by RDFlib
    rdflib_formats = {'text/rdf+n3': 'n3',
                      'text/turtle': 'turtle',
                      'application/x-turtle': 'turtle',
                      'text/turtle': 'turtle',
                      'application/rdf+xml': 'xml',
                      'text/rdf': 'xml',
                      'application/rdf+xml': 'xml'}

    try:
        got = kwargs.get('format', 'text/rdf+n3')
        fmt = rdflib_formats[got]
    except KeyError:
        raise KrnlException('Unsupported format for graph processing: {!s}', got)

    g = ConjunctiveGraph()
    g.load(StringInputSource(result), format=fmt)

    display = cfg.dis[0] if is_collection(cfg.dis) else cfg.dis
    if display in ('png', 'svg'):
        try:
            literal = len(cfg.dis) > 1 and cfg.dis[1].startswith('withlit')
            opt = {'lang': cfg.lan, 'literal': literal, 'graphviz': []}
            data, metadata = draw_graph(g, fmt=display, options=opt)
            return {'data': data,
                    'metadata': metadata}
        except Exception as e:
            raise KrnlException('Exception while drawing graph: {!r}', e)
    elif display == 'table':
        it = rdf_iterator(g, set(cfg.lan), add_vtype=cfg.typ)
        n, data = html_table(it, limit=cfg.lmt, withtype=cfg.typ)
        data += div('Shown: {}, Total rows: {}', n if cfg.lmt else 'all',
                    len(g), css="tinfo")
        data = {'text/html': div(data)}
    elif len(g) == 0:
        data = {'text/html': div(div('empty graph', css='krn-warn'))}
    else:
        data = {'text/plain': g.serialize(format='nt').decode('utf-8')}

    return {'data': data,
            'metadata': {}}

Response: Render to output a result that can be parsed as an RDF graph


Instruction:
Can you generate the documentation for the following Python 3 function
def query(self, query, num=0, silent=False):
        if self.srv is None:
            raise KrnlException('no endpoint defined')

        # Add to the query all predefined SPARQL prefixes
        if self.cfg.pfx:
            prefix = '\n'.join(('PREFIX {} {}'.format(*v)
                                for v in self.cfg.pfx.items()))
            query = prefix + '\n' + query

        # Prepend to the query all predefined Header entries
        # The header should be before the prefix and other sparql commands
        if self.cfg.hdr:
            query = '\n'.join(self.cfg.hdr) + '\n' + query

        if self.log.isEnabledFor(logging.DEBUG):
            self.log.debug("\n%50s%s", query, '...' if len(query) > 50 else '')

        # Select requested format
        if self.cfg.fmt is not None:
            fmt_req = self.cfg.fmt
        elif re.search(r'\bselect\b', query, re.I):
            fmt_req = SPARQLWrapper.JSON
        elif re.search(r'\b(?:describe|construct)\b', query, re.I):
            fmt_req = SPARQLWrapper.N3
        else:
            fmt_req = False

        # Set the query
        self.srv.resetQuery()
        if self.cfg.aut:
            self.srv.setHTTPAuth(self.cfg.aut[0])
            self.srv.setCredentials(*self.cfg.aut[1:])
        else:
            self.srv.setCredentials(None, None)
        self.log.debug(u'request-format: %s  display: %s', fmt_req, self.cfg.dis)
        if fmt_req:
            self.srv.setReturnFormat(fmt_req)
        if self.cfg.grh:
            self.srv.addParameter("default-graph-uri", self.cfg.grh)
        for p in self.cfg.par.items():
            self.srv.addParameter(*p)
        self.srv.setQuery(query)

        if not silent or self.cfg.out:
            try:
                # Launch query
                start = datetime.datetime.utcnow()
                res = self.srv.query()
                now = datetime.datetime.utcnow()
                self.log.debug(u'response elapsed=%s', now-start)
                start = now

                # See what we got
                info = res.info()
                self.log.debug(u'response info: %s', info)
                fmt_got = info['content-type'].split(';')[0] if 'content-type' in info else None

                # Check we received a MIME type according to what we requested
                if fmt_req and fmt_got not in mime_type[fmt_req]:
                    raise KrnlException(u'Unexpected response format: {} (requested: {})', fmt_got, fmt_req)

                # Get the result
                data = b''.join((line for line in res))

            except KrnlException:
                raise
            except SPARQLWrapperException as e:
                raise KrnlException(u'SPARQL error: {}', touc(e))
            except Exception as e:
                raise KrnlException(u'Query processing error: {!s}', e)

            # Write the raw result to a file
            if self.cfg.out:
                try:
                    outname = self.cfg.out % num
                except TypeError:
                    outname = self.cfg.out
                with io.open(outname, 'wb') as f:
                    f.write(data)

            # Render the result into the desired display format
            try:
                # Data format we will render
                fmt = (fmt_req if fmt_req else
                       SPARQLWrapper.JSON if fmt_got in mime_type[SPARQLWrapper.JSON] else
                       SPARQLWrapper.N3 if fmt_got in mime_type[SPARQLWrapper.N3] else
                       SPARQLWrapper.XML if fmt_got in mime_type[SPARQLWrapper.XML] else
                       'text/plain' if self.cfg.dis == 'raw' else
                       fmt_got if fmt_got in ('text/plain', 'text/html') else
                       'text/plain')
                #self.log.debug(u'format: req=%s got=%s rend=%s',fmt_req,fmt_got,fmt)

                # Can't process? Just write the data as is
                if fmt in ('text/plain', 'text/html'):
                    out = data.decode('utf-8') if isinstance(data, bytes) else data
                    r = {'data': {fmt: out}, 'metadata': {}}
                else:
                    f = render_json if fmt == SPARQLWrapper.JSON else render_xml if fmt == SPARQLWrapper.XML else render_graph
                    r = f(data, self.cfg, format=fmt_got)
                    now = datetime.datetime.utcnow()
                    self.log.debug(u'response formatted=%s', now-start)
                if not silent:
                    return r

            except Exception as e:
                raise KrnlException(u'Response processing error: {}', touc(e))

Response: Launch an SPARQL query and process the results and return them


Instruction:
Explain what the following Python 3 code does
def set_logging( logfilename=None, level=None ):
    if logfilename is None:
        # Find the logging diectory
        logdir = os.environ.get( 'LOGDIR' )
        if logdir is None:
            logdir = os.environ.get( 'LOGDIR_DEFAULT', tempfile.gettempdir() )
        # Define the log filename
        basename = __name__.split('.')[-2]
        logfilename = os.path.join( logdir, basename + '.log' )
    LOGCONFIG['handlers']['default']['filename'] = logfilename

    if level is not None:
        LOGCONFIG['loggers']['sparqlkernel']['level'] = level

    dictConfig( LOGCONFIG )

Response: Set a logging configuration with a rolling file appender.


Instruction:
Can you generate the documentation for the following Python 3 function
def smartfields_get_field_status(self, field_name):
        manager = self._smartfields_managers.get(field_name, None)
        if manager is not None:
            return manager.get_status(self)
        return {'state': 'ready'}

Response: A way to find out a status of a filed.


Instruction:
Make a summary of the following Python 3 code
def get_ext(self, format=None, **kwargs):
        try:
            format = format or self.default_params['format']
            return ".%s" % format.lower()
        except KeyError: pass

Response: Returns new file extension based on a processor s format parameter.


Instruction:
Write a Python 3 script for
creating a temporary file.

Response:
def get_output_file(self, in_file, instance, field, **kwargs):
        """Creates a temporary file. With regular `FileSystemStorage` it does not
        need to be deleted, instaed file is safely moved over. With other cloud
        based storage it is a good idea to set `delete=True`."""
        return NamedTemporaryFile(mode='rb', suffix='_%s_%s%s' % (
            get_model_name(instance), field.name, self.get_ext()), delete=False)


Instruction:
Write a Python 3 function for
returning the best available label for the passed entity in the passed graph.

Response:
def label(x, gr, preferred_languages=None):
    """
      @param x : graph entity
      @param gr (Graph): RDF graph
      @param preferred_languages (iterable)

    Return the best available label in the graph for the passed entity.
    If a set of preferred languages is given, try them in order. If none is
    found, an arbitrary language will be chosen
    """
    # Find all labels & their language
    labels = { l.language : l
               for labelProp in LABEL_PROPERTIES
               for l in gr.objects(x,labelProp) }
    if labels:
        #return repr(preferred_languages) + repr(labels)
        #return u'|'.join(preferred_languages) +  u' -> ' + u'/'.join( u'{}:{}'.format(*i) for i in labels.items() )
        if preferred_languages is not None:
            for l in preferred_languages:
                if l in labels:
                    return labels[l]
        return labels.itervalues().next()

    # No labels available. Try to generate a QNAME, or else, the string itself
    try:
        return gr.namespace_manager.compute_qname(x)[2].replace('_',' ')
    except:
        # Attempt to extract the trailing part of an URI
        m = re.search( '([^/]+)$', x )
        return m.group(1).replace('_',' ') if m else x


Instruction:
How would you implement a function in Python 3 that
converts the RDF graph to DOT format

Response:
def rdf2dot( g, stream, opts={} ):
    """
    Convert the RDF graph to DOT
    Write the dot output to the stream
    """

    accept_lang = set( opts.get('lang',[]) )
    do_literal = opts.get('literal')
    nodes = {}
    links = []

    def node_id(x):
        if x not in nodes:
            nodes[x] = "node%d" % len(nodes)
        return nodes[x]

    def qname(x, g):
        try:
            q = g.compute_qname(x)
            return q[0] + ":" + q[2]
        except:
            return x

    def accept( node ):
        if isinstance( node, (rdflib.URIRef,rdflib.BNode) ):
            return True
        if not do_literal:
            return False
        return (not accept_lang) or (node.language in accept_lang)

    stream.write( u'digraph { \n node [ fontname="DejaVu Sans,Tahoma,Geneva,sans-serif" ] ; \n' )

    # Write all edges. In the process make a list of all nodes
    for s, p, o in g:
        # skip triples for labels
        if p == rdflib.RDFS.label:
            continue

        # Create a link if both objects are graph nodes
        # (or, if literals are also included, if their languages match)
        if not (accept(s) and accept(o)):
            continue

        # add the nodes to the list
        sn = node_id(s)
        on = node_id(o)

        # add the link
        q = qname(p,g)
        if isinstance(p, rdflib.URIRef):
            opstr = u'\t%s -> %s [ arrowhead="open", color="#9FC9E560", fontsize=9, fontcolor="#204080", label="%s", href="%s", target="_other" ] ;\n' % (sn,on,q,p)
        else:
            opstr = u'\t%s -> %s [ arrowhead="open", color="#9FC9E560", fontsize=9, fontcolor="#204080", label="%s" ] ;\n'%(sn,on,q)
        stream.write( opstr )

    # Write all nodes
    for u, n in nodes.items():
        lbl = escape( label(u,g,accept_lang), True )
        if isinstance(u, rdflib.URIRef):
            opstr = u'%s [ shape=none, fontsize=10, fontcolor=%s, label="%s", href="%s", target=_other ] \n' % (n, 'blue', lbl, u )
        else:
            opstr = u'%s [ shape=none, fontsize=10, fontcolor=%s, label="%s" ] \n' % (n, 'black', lbl )
        stream.write( u"# %s %s\n" % (u, n) )
        stream.write( opstr )

    stream.write(u'}\n')


Instruction:
Can you tell what is the following Python 3 function doing
def draw_graph( g, fmt='svg', prg='dot', options={} ):
    # Convert RDF to Graphviz
    buf = StringIO()
    rdf2dot( g, buf, options )

    gv_options = options.get('graphviz',[])
    if fmt == 'png':
        gv_options += [ '-Gdpi=220', '-Gsize=25,10!' ]
        metadata = { "width": 5500, "height": 2200, "unconfined" : True }

    #import codecs
    #with codecs.open('/tmp/sparqlkernel-img.dot','w',encoding='utf-8') as f:
    #    f.write( buf.getvalue() )

    # Now use Graphviz to generate the graph
    image = run_dot( buf.getvalue(), fmt=fmt, options=gv_options, prg=prg )

    #with open('/tmp/sparqlkernel-img.'+fmt,'w') as f:
    #    f.write( image )

    # Return it
    if fmt == 'png':
        return { 'image/png' : base64.b64encode(image).decode('ascii') }, \
               { "image/png" : metadata }
    elif fmt == 'svg':
        return { 'image/svg+xml' : image.decode('utf-8').replace('<svg','<svg class="unconfined"',1) }, \
               { "unconfined" : True }

Response: Draw an RDF graph as an image


Instruction:
Make a summary of the following Python 3 code
def get_SZ(self, psd, geometry):
        if (self._S_table is None) or (self._Z_table is None):
            raise AttributeError(
                "Initialize or load the scattering table first.")

        if (not isinstance(psd, PSD)) or self._previous_psd != psd:
            self._S_dict = {}
            self._Z_dict = {}
            psd_w = psd(self._psd_D)

            for geom in self.geometries:
                self._S_dict[geom] = \
                    trapz(self._S_table[geom] * psd_w, self._psd_D)
                self._Z_dict[geom] = \
                    trapz(self._Z_table[geom] * psd_w, self._psd_D)

            self._previous_psd = psd

        return (self._S_dict[geometry], self._Z_dict[geometry])

Response: Compute the S and Z matrices for the given PSD and geometries.


Instruction:
Can you tell what is the following Python 3 function doing
def init_scatter_table(self, tm, angular_integration=False, verbose=False):
        self._psd_D = np.linspace(self.D_max/self.num_points, self.D_max,
            self.num_points)

        self._S_table = {}
        self._Z_table = {}
        self._previous_psd = None
        self._m_table = np.empty(self.num_points, dtype=complex)
        if angular_integration:
            self._angular_table = {"sca_xsect": {}, "ext_xsect": {},
                "asym": {}}
        else:
            self._angular_table = None

        (old_m, old_axis_ratio, old_radius, old_geom, old_psd_integrator) = \
            (tm.m, tm.axis_ratio, tm.radius, tm.get_geometry(),
                tm.psd_integrator)

        try:
            # temporarily disable PSD integration to avoid recursion
            tm.psd_integrator = None

            for geom in self.geometries:
                self._S_table[geom] = \
                    np.empty((2,2,self.num_points), dtype=complex)
                self._Z_table[geom] = np.empty((4,4,self.num_points))

                if angular_integration:
                    for int_var in ["sca_xsect", "ext_xsect", "asym"]:
                        self._angular_table[int_var][geom] = \
                            np.empty(self.num_points)

            for (i,D) in enumerate(self._psd_D):
                if verbose:
                    print("Computing point {i} at D={D}...".format(i=i, D=D))
                if self.m_func != None:
                    tm.m = self.m_func(D)
                if self.axis_ratio_func != None:
                    tm.axis_ratio = self.axis_ratio_func(D)
                self._m_table[i] = tm.m
                tm.radius = D/2.0
                for geom in self.geometries:
                    tm.set_geometry(geom)
                    (S, Z) = tm.get_SZ_orient()
                    self._S_table[geom][:,:,i] = S
                    self._Z_table[geom][:,:,i] = Z

                    if angular_integration:
                        self._angular_table["sca_xsect"][geom][i] = \
                            scatter.sca_xsect(tm)
                        self._angular_table["ext_xsect"][geom][i] = \
                            scatter.ext_xsect(tm)
                        self._angular_table["asym"][geom][i] = \
                            scatter.asym(tm)
        finally:
            #restore old values
            (tm.m, tm.axis_ratio, tm.radius, tm.psd_integrator) = \
                (old_m, old_axis_ratio, old_radius, old_psd_integrator)
            tm.set_geometry(old_geom)

Response: Initialize the scattering lookup tables for the given object.


Instruction:
How would you explain what the following Python 3 function does
def save_scatter_table(self, fn, description=""):
        data = {
           "description": description,
           "time": datetime.now(),
           "psd_scatter": (self.num_points, self.D_max, self._psd_D,
                self._S_table, self._Z_table, self._angular_table,
                self._m_table, self.geometries),
           "version": tmatrix_aux.VERSION
           }
        pickle.dump(data, file(fn, 'w'), pickle.HIGHEST_PROTOCOL)

Response: Save the state of the scattering lookup tables to a file.


Instruction:
Given the following Python 3 function, write the documentation
def load_scatter_table(self, fn):
        data = pickle.load(file(fn))

        if ("version" not in data) or (data["version"]!=tmatrix_aux.VERSION):
            warnings.warn("Loading data saved with another version.", Warning)

        (self.num_points, self.D_max, self._psd_D, self._S_table,
            self._Z_table, self._angular_table, self._m_table,
            self.geometries) = data["psd_scatter"]
        return (data["time"], data["description"])

Response: Loads the scattering lookup tables from a file.


Instruction:
Make a summary of the following Python 3 code
def gaussian_pdf(std=10.0, mean=0.0):
    norm_const = 1.0
    def pdf(x):
        return norm_const*np.exp(-0.5 * ((x-mean)/std)**2) * \
            np.sin(np.pi/180.0 * x)
    norm_dev = quad(pdf, 0.0, 180.0)[0]
    # ensure that the integral over the distribution equals 1
    norm_const /= norm_dev
    return pdf

Response: Returns the spherical Jacobian - edition standard deviation Gaussian PDF for orientation averaging.


Instruction:
Implement a function in Python 3 to
uniform PDF for orientation averaging.

Response:
def uniform_pdf():
    """Uniform PDF for orientation averaging.

    Returns:
        pdf(x), a function that returns the value of the spherical Jacobian-
        normalized uniform PDF. It is normalized for the interval [0, 180].
    """
    norm_const = 1.0
    def pdf(x):
        return norm_const * np.sin(np.pi/180.0 * x)
    norm_dev = quad(pdf, 0.0, 180.0)[0]
    # ensure that the integral over the distribution equals 1
    norm_const /= norm_dev
    return pdf


Instruction:
Create a Python 3 function to
compute the T - matrix using variable orientation scatterers.

Response:
def orient_averaged_adaptive(tm):
    """Compute the T-matrix using variable orientation scatterers.

    This method uses a very slow adaptive routine and should mainly be used
    for reference purposes. Uses the set particle orientation PDF, ignoring
    the alpha and beta attributes.

    Args:
        tm: TMatrix (or descendant) instance

    Returns:
        The amplitude (S) and phase (Z) matrices.
    """
    S = np.zeros((2,2), dtype=complex)
    Z = np.zeros((4,4))

    def Sfunc(beta, alpha, i, j, real):
        (S_ang, Z_ang) = tm.get_SZ_single(alpha=alpha, beta=beta)
        s = S_ang[i,j].real if real else S_ang[i,j].imag
        return s * tm.or_pdf(beta)

    ind = range(2)
    for i in ind:
        for j in ind:
            S.real[i,j] = dblquad(Sfunc, 0.0, 360.0,
                lambda x: 0.0, lambda x: 180.0, (i,j,True))[0]/360.0
            S.imag[i,j] = dblquad(Sfunc, 0.0, 360.0,
                lambda x: 0.0, lambda x: 180.0, (i,j,False))[0]/360.0

    def Zfunc(beta, alpha, i, j):
        (S_and, Z_ang) = tm.get_SZ_single(alpha=alpha, beta=beta)
        return Z_ang[i,j] * tm.or_pdf(beta)

    ind = range(4)
    for i in ind:
        for j in ind:
            Z[i,j] = dblquad(Zfunc, 0.0, 360.0,
                lambda x: 0.0, lambda x: 180.0, (i,j))[0]/360.0

    return (S, Z)


Instruction:
Write a Python 3 function for
computing the T - matrix using variable orientation scatterers.

Response:
def orient_averaged_fixed(tm):
    """Compute the T-matrix using variable orientation scatterers.

    This method uses a fast Gaussian quadrature and is suitable
    for most use. Uses the set particle orientation PDF, ignoring
    the alpha and beta attributes.

    Args:
        tm: TMatrix (or descendant) instance.

    Returns:
        The amplitude (S) and phase (Z) matrices.
    """
    S = np.zeros((2,2), dtype=complex)
    Z = np.zeros((4,4))
    ap = np.linspace(0, 360, tm.n_alpha+1)[:-1]
    aw = 1.0/tm.n_alpha

    for alpha in ap:
        for (beta, w) in zip(tm.beta_p, tm.beta_w):
            (S_ang, Z_ang) = tm.get_SZ_single(alpha=alpha, beta=beta)
            S += w * S_ang
            Z += w * Z_ang

    sw = tm.beta_w.sum()
    #normalize to get a proper average
    S *= aw/sw
    Z *= aw/sw

    return (S, Z)


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

        if self.radius_type == Scatterer.RADIUS_MAXIMUM:
            # Maximum radius is not directly supported in the original
            # so we convert it to equal volume radius
            radius_type = Scatterer.RADIUS_EQUAL_VOLUME
            radius = self.equal_volume_from_maximum()
        else:
            radius_type = self.radius_type
            radius = self.radius

        self.nmax = pytmatrix.calctmat(radius, radius_type,
            self.wavelength, self.m.real, self.m.imag, self.axis_ratio,
            self.shape, self.ddelt, self.ndgs)
        self._tm_signature = (self.radius, self.radius_type, self.wavelength,
            self.m, self.axis_ratio, self.shape, self.ddelt, self.ndgs)

Response: Initialize the T - matrix.


Instruction:
Given the following Python 3 function, write the documentation
def _init_orient(self):
        if self.orient == orientation.orient_averaged_fixed:
            (self.beta_p, self.beta_w) = quadrature.get_points_and_weights(
                self.or_pdf, 0, 180, self.n_beta)
        self._set_orient_signature()

Response: Retrieve the quadrature points and weights if needed.


Instruction:
Given the following Python 3 function, write the documentation
def _set_scatter_signature(self):
        self._scatter_signature = (self.thet0, self.thet, self.phi0, self.phi,
            self.alpha, self.beta, self.orient)

Response: Mark the amplitude and scattering matrices as up to date.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_SZ_single(self, alpha=None, beta=None):
        if alpha == None:
            alpha = self.alpha
        if beta == None:
            beta = self.beta

        tm_outdated = self._tm_signature != (self.radius, self.radius_type,
            self.wavelength, self.m, self.axis_ratio, self.shape, self.ddelt,
            self.ndgs)
        if tm_outdated:
            self._init_tmatrix()

        scatter_outdated = self._scatter_signature != (self.thet0, self.thet,
            self.phi0, self.phi, alpha, beta, self.orient)

        outdated = tm_outdated or scatter_outdated

        if outdated:
            (self._S_single, self._Z_single) = pytmatrix.calcampl(self.nmax,
                self.wavelength, self.thet0, self.thet, self.phi0, self.phi,
                alpha, beta)
            self._set_scatter_signature()

        return (self._S_single, self._Z_single)

Response: Get the S and Z matrices for a single orientation.


Instruction:
Write a Python 3 function for
getting the S and Z matrices using the specified orientation averaging.

Response:
def get_SZ_orient(self):
        """Get the S and Z matrices using the specified orientation averaging.
        """

        tm_outdated = self._tm_signature != (self.radius, self.radius_type,
            self.wavelength, self.m, self.axis_ratio, self.shape, self.ddelt,
            self.ndgs)
        scatter_outdated = self._scatter_signature != (self.thet0, self.thet,
            self.phi0, self.phi, self.alpha, self.beta, self.orient)

        orient_outdated = self._orient_signature != \
            (self.orient, self.or_pdf, self.n_alpha, self.n_beta)
        if orient_outdated:
            self._init_orient()

        outdated = tm_outdated or scatter_outdated or orient_outdated

        if outdated:
            (self._S_orient, self._Z_orient) = self.orient(self)
            self._set_scatter_signature()

        return (self._S_orient, self._Z_orient)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_SZ(self):
        if self.psd_integrator is None:
            (self._S, self._Z) = self.get_SZ_orient()
        else:
            scatter_outdated = self._scatter_signature != (self.thet0,
                self.thet, self.phi0, self.phi, self.alpha, self.beta,
                self.orient)
            psd_outdated = self._psd_signature != (self.psd,)
            outdated = scatter_outdated or psd_outdated

            if outdated:
                (self._S, self._Z) = self.psd_integrator(self.psd,
                    self.get_geometry())
                self._set_scatter_signature()
                self._set_psd_signature()

        return (self._S, self._Z)

Response: Get the S and Z matrices using the current parameters.


Instruction:
Write a Python 3 script to
scatter intensity for the current setup.

Response:
def sca_intensity(scatterer, h_pol=True):
    """Scattering intensity (phase function) for the current setup.

    Args:
        scatterer: a Scatterer instance.
        h_pol: If True (default), use horizontal polarization.
        If False, use vertical polarization.

    Returns:
        The differential scattering cross section.
    """
    Z = scatterer.get_Z()
    return (Z[0,0] - Z[0,1]) if h_pol else (Z[0,0] + Z[0,1])


Instruction:
Write a Python 3 script to
scatter cross section for the current setup with polarization.

Response:
def sca_xsect(scatterer, h_pol=True):
    """Scattering cross section for the current setup, with polarization.

    Args:
        scatterer: a Scatterer instance.
        h_pol: If True (default), use horizontal polarization.
        If False, use vertical polarization.

    Returns:
        The scattering cross section.
    """

    if scatterer.psd_integrator is not None:
        return scatterer.psd_integrator.get_angular_integrated(
            scatterer.psd, scatterer.get_geometry(), "sca_xsect")

    old_geom = scatterer.get_geometry()

    def d_xsect(thet, phi):
        (scatterer.phi, scatterer.thet) = (phi*rad_to_deg, thet*rad_to_deg)
        Z = scatterer.get_Z()
        I = sca_intensity(scatterer, h_pol)
        return I * np.sin(thet)

    try:
        xsect = dblquad(d_xsect, 0.0, 2*np.pi, lambda x: 0.0,
            lambda x: np.pi)[0]
    finally:
        scatterer.set_geometry(old_geom)

    return xsect


Instruction:
Can you generate the documentation for the following Python 3 function
def ext_xsect(scatterer, h_pol=True):

    if scatterer.psd_integrator is not None:
        try:
            return scatterer.psd_integrator.get_angular_integrated(
                scatterer.psd, scatterer.get_geometry(), "ext_xsect")
        except AttributeError:
            # Fall back to the usual method of computing this from S
            pass

    old_geom = scatterer.get_geometry()
    (thet0, thet, phi0, phi, alpha, beta) = old_geom
    try:
        scatterer.set_geometry((thet0, thet0, phi0, phi0, alpha, beta))
        S = scatterer.get_S()
    finally:
        scatterer.set_geometry(old_geom)

    if h_pol:
        return 2 * scatterer.wavelength * S[1,1].imag
    else:
        return 2 * scatterer.wavelength * S[0,0].imag

Response: Returns the extinction cross section for the current setup.


Instruction:
How would you implement a function in Python 3 that
singles - scattering albedo for the current setup with polarization.

Response:
def ssa(scatterer, h_pol=True):
    """Single-scattering albedo for the current setup, with polarization.

    Args:
        scatterer: a Scatterer instance.
        h_pol: If True (default), use horizontal polarization.
        If False, use vertical polarization.

    Returns:
        The single-scattering albedo.
    """

    ext_xs = ext_xsect(scatterer, h_pol=h_pol)
    return sca_xsect(scatterer, h_pol=h_pol)/ext_xs if ext_xs > 0.0 else 0.0


Instruction:
Make a summary of the following Python 3 code
def asym(scatterer, h_pol=True):

    if scatterer.psd_integrator is not None:
        return scatterer.psd_integrator.get_angular_integrated(
            scatterer.psd, scatterer.get_geometry(), "asym")

    old_geom = scatterer.get_geometry()

    cos_t0 = np.cos(scatterer.thet0 * deg_to_rad)
    sin_t0 = np.sin(scatterer.thet0 * deg_to_rad)
    p0 = scatterer.phi0 * deg_to_rad
    def integrand(thet, phi):
        (scatterer.phi, scatterer.thet) = (phi*rad_to_deg, thet*rad_to_deg)
        cos_T_sin_t = 0.5 * (np.sin(2*thet)*cos_t0 + \
            (1-np.cos(2*thet))*sin_t0*np.cos(p0-phi))
        I = sca_intensity(scatterer, h_pol)
        return I * cos_T_sin_t

    try:
        cos_int = dblquad(integrand, 0.0, 2*np.pi, lambda x: 0.0,
            lambda x: np.pi)[0]
    finally:
        scatterer.set_geometry(old_geom)

    return cos_int/sca_xsect(scatterer, h_pol)

Response: Asymmetry parameter for the current setup.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def radar_xsect(scatterer, h_pol=True):
    Z = scatterer.get_Z()
    if h_pol:
        return 2 * np.pi * \
            (Z[0,0] - Z[0,1] - Z[1,0] + Z[1,1])
    else:
        return 2 * np.pi * \
            (Z[0,0] + Z[0,1] + Z[1,0] + Z[1,1])

Response: Radar cross section for the current setup.


Instruction:
Can you tell what is the following Python 3 function doing
def refl(scatterer, h_pol=True):
    return scatterer.wavelength**4/(np.pi**5*scatterer.Kw_sqr) * \
        radar_xsect(scatterer, h_pol)

Response: Reflectivity of the current setup.


Instruction:
Make a summary of the following Python 3 code
def delta_hv(scatterer):
    Z = scatterer.get_Z()
    return np.arctan2(Z[2,3] - Z[3,2], -Z[2,2] - Z[3,3])

Response: Returns the delta_hv for the current setup.


Instruction:
Create a Python 3 function to
return the Copolarized correlation coefficient for the current setup.

Response:
def rho_hv(scatterer):
    """
    Copolarized correlation (rho_hv) for the current setup.

    Args:
        scatterer: a Scatterer instance.

    Returns:
       rho_hv.
    """
    Z = scatterer.get_Z()
    a = (Z[2,2] + Z[3,3])**2 + (Z[3,2] - Z[2,3])**2
    b = (Z[0,0] - Z[0,1] - Z[1,0] + Z[1,1])
    c = (Z[0,0] + Z[0,1] + Z[1,0] + Z[1,1])
    return np.sqrt(a / (b*c))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def Kdp(scatterer):
    if (scatterer.thet0 != scatterer.thet) or \
        (scatterer.phi0 != scatterer.phi):

        raise ValueError("A forward scattering geometry is needed to " + \
            "compute the specific differential phase.")

    S = scatterer.get_S()
    return 1e-3 * (180.0/np.pi) * scatterer.wavelength * (S[1,1]-S[0,0]).real

Response: Returns the K_dp of the current setup.


Instruction:
Can you generate the documentation for the following Python 3 function
def mg_refractive(m, mix):

    if len(m) == 2:
        cF = float(mix[1]) / (mix[0]+mix[1]) * \
            (m[1]**2-m[0]**2) / (m[1]**2+2*m[0]**2)
        er = m[0]**2 * (1.0+2.0*cF) / (1.0-cF)
        m = np.sqrt(er)
    else:
        m_last = mg_refractive(m[-2:], mix[-2:])
        mix_last = mix[-2] + mix[-1]
        m = mg_refractive(m[:-2] + (m_last,), mix[:-2] + (mix_last,))
    return m

Response:
Maxwell - Garnett approximation for the complex refractive index of
      .


Instruction:
Make a summary of the following Python 3 code
def dsr_thurai_2007(D_eq):

    if D_eq < 0.7:
        return 1.0
    elif D_eq < 1.5:
        return 1.173 - 0.5165*D_eq + 0.4698*D_eq**2 - 0.1317*D_eq**3 - \
            8.5e-3*D_eq**4
    else:
        return 1.065 - 6.25e-2*D_eq - 3.99e-3*D_eq**2 + 7.66e-4*D_eq**3 - \
            4.095e-5*D_eq**4

Response: D_eq is the drop - volume - equivalent diameter of the class in the Thurai 2007.


Instruction:
Write a Python 3 script for
getting and order feature importances from a sklearn model or array - like structure.

Response:
def feature_importances(data, top_n=None, feature_names=None):
    """
    Get and order feature importances from a scikit-learn model
    or from an array-like structure.

    If data is a scikit-learn model with sub-estimators (e.g. RandomForest,
    AdaBoost) the function will compute the standard deviation of each
    feature.

    Parameters
    ----------
    data : sklearn model or array-like structure
        Object to get the data from.
    top_n : int
        Only get results for the top_n features.
    feature_names : array-like
        Feature_names

    Returns
    -------
    numpy structured array
        Returns a numpy structured array with the data. Columns are
        feature_name, importance and std_ if an sklearn model with
        sub-estimators was passed in data.

    """
    # data can be either a sklearn estimator or an iterator with
    # the actual importances values, try to get the values
    try:
        imp = data.feature_importances_
    except:
        imp = np.array(data)

    # in case the user passed an estimator, it may have an estimators_
    # attribute, which includes importnaces for every sub-estimator
    # get them if possible
    try:
        sub_imp = np.array([e.feature_importances_ for e in data.estimators_])
        # calculate std
        std = np.std(sub_imp, axis=0)
    except:
        std = None

    # get the number of features
    n_features = len(imp)

    # check that the data has the correct format
    if top_n and top_n > n_features:
        raise ValueError(('top_n ({}) cannot be greater than the number of'
                          ' features ({})'.format(top_n, n_features)))
    if top_n and top_n < 1:
        raise ValueError('top_n cannot be less than 1')
    if feature_names and len(feature_names) != n_features:
        raise ValueError(('feature_names ({}) must match the number of'
                          ' features ({})'.format(len(feature_names),
                                                  n_features)))

    # if the user did not pass feature names create generic names
    if feature_names is None:
        feature_names = ['Feature {}'.format(n) for n in range(1, n_features+1)]
        feature_names = np.array(feature_names)
    else:
        feature_names = np.array(feature_names)

    # order the data according to the importance for the feature
    idx = np.argsort(imp)[::-1]
    imp = imp[idx]
    feature_names = feature_names[idx]
    if std is not None:
        std = std[idx]

    # build the structured array
    if std is not None:
        names = 'feature_name,importance,std_'
        res = np.core.records.fromarrays([feature_names, imp, std],
                                         names=names)
    else:
        names = 'feature_name,importance'
        res = np.core.records.fromarrays([feature_names, imp],
                                         names=names)

    # get subset if top_n is not none
    if top_n:
        res = res[:top_n]

    return res


Instruction:
Here you have a function in Python 3, explain what it does
def _group_by(data, criteria):
    if isinstance(criteria, str):
        criteria_str = criteria

        def criteria(x):
            return x[criteria_str]

    res = defaultdict(list)
    for element in data:
        key = criteria(element)
        res[key].append(element)
    return res

Response: Group the data by a function or a key


Instruction:
Implement a function in Python 3 to
return a function that takes a list of parameters and returns a tuple of the form k1 k2

Response:
def _get_params_value(params):
    """
        Given an iterator (k1, k2), returns a function that when called
        with an object obj returns a tuple of the form:
        ((k1, obj.parameters[k1]), (k2, obj.parameters[k2]))
    """
    # sort params for consistency
    ord_params = sorted(params)

    def fn(obj):
        l = []
        for p in ord_params:
            try:
                l.append((p, obj.parameters[p]))
            except:
                raise ValueError('{} is not a valid parameter'.format(p))
        return tuple(l)
    return fn


Instruction:
Write a Python 3 function that can
perform the product between two objects even if they don t support iteration

Response:
def _product(k, v):
    """
        Perform the product between two objects
        even if they don't support iteration
    """
    if not _can_iterate(k):
        k = [k]
    if not _can_iterate(v):
        v = [v]
    return list(product(k, v))


Instruction:
Can you create a Python 3 function that
converts a mapping object such as a dictionary to tuple pairs

Response:
def _mapping_to_tuple_pairs(d):
    """
        Convert a mapping object (such as a dictionary) to tuple pairs,
        using its keys and values to generate the pairs and then generating
        all possible combinations between those
        e.g. {1: (1,2,3)} -> (((1, 1),), ((1, 2),), ((1, 3),))
    """
    # order the keys, this will prevent different implementations of Python,
    # return different results from the same dictionary since the order of
    # iteration depends on it
    t = []
    ord_keys = sorted(d.keys())
    for k in ord_keys:
        t.append(_product(k, d[k]))
    return tuple(product(*t))


Instruction:
How would you code a function in Python 3 to
plot a learning curve for the training set and test set.

Response:
def learning_curve(train_scores, test_scores, train_sizes, ax=None):
    """Plot a learning curve

    Plot a metric vs number of examples for the training and test set

    Parameters
    ----------
    train_scores : array-like
        Scores for the training set
    test_scores : array-like
        Scores for the test set
    train_sizes : array-like
        Relative or absolute numbers of training examples used to generate
        the learning curve
    ax : matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    Examples
    --------

    .. plot:: ../../examples/learning_curve.py

    """
    if ax is None:
        ax = plt.gca()

    ax.grid()
    ax.set_title("Learning Curve")
    ax.set_xlabel("Training examples")
    ax.set_ylabel("Score mean")

    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)

    ax.fill_between(train_sizes, train_scores_mean - train_scores_std,
                    train_scores_mean + train_scores_std, alpha=0.1,
                    color="r")
    ax.fill_between(train_sizes, test_scores_mean - test_scores_std,
                    test_scores_mean + test_scores_std, alpha=0.1, color="g")

    ax.plot(train_sizes, train_scores_mean, 'o-', color="r",
            label="Training score")
    ax.plot(train_sizes, test_scores_mean, 'o-', color="g",
            label="Cross-validation score")

    ax.legend(loc="best")
    ax.margins(0.05)
    return ax


Instruction:
Write a Python 3 script for
plotting precision - recall curve.

Response:
def precision_recall(y_true, y_score, ax=None):
    """
    Plot precision-recall curve.

    Parameters
    ----------
    y_true : array-like, shape = [n_samples]
        Correct target values (ground truth).
    y_score : array-like, shape = [n_samples] or [n_samples, 2] for binary
              classification or [n_samples, n_classes] for multiclass

        Target scores (estimator predictions).
    ax : matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes

    Notes
    -----
    It is assumed that the y_score parameter columns are in order. For example,
    if ``y_true = [2, 2, 1, 0, 0, 1, 2]``, then the first column in y_score
    must countain the scores for class 0, second column for class 1 and so on.

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    Examples
    --------
    .. plot:: ../../examples/precision_recall.py

    """
    if any((val is None for val in (y_true, y_score))):
        raise ValueError('y_true and y_score are needed to plot '
                         'Precision-Recall')

    if ax is None:
        ax = plt.gca()

    # get the number of classes from y_score
    y_score_is_vector = is_column_vector(y_score) or is_row_vector(y_score)
    if y_score_is_vector:
        n_classes = 2
    else:
        _, n_classes = y_score.shape

    # check data shape?

    if n_classes > 2:
        # convert y_true to binary format
        y_true_bin = label_binarize(y_true, classes=np.unique(y_true))
        _precision_recall_multi(y_true_bin, y_score, ax=ax)
        for i in range(n_classes):
            _precision_recall(y_true_bin[:, i], y_score[:, i], ax=ax)
    else:
        if y_score_is_vector:
            _precision_recall(y_true, y_score, ax)
        else:
            _precision_recall(y_true, y_score[:, 1], ax)

    # raise error if n_classes = 1?
    return ax


Instruction:
How would you explain what the following Python 3 function does
def _precision_recall(y_true, y_score, ax=None):
    precision, recall, _ = precision_recall_curve(y_true, y_score)
    average_precision = average_precision_score(y_true, y_score)

    if ax is None:
        ax = plt.gca()

    ax.plot(recall, precision, label=('Precision-Recall curve: AUC={0:0.2f}'
                                      .format(average_precision)))
    _set_ax_settings(ax)
    return ax

Response: Plot precision - recall curve.


Instruction:
Explain what the following Python 3 code does
def _precision_recall_multi(y_true, y_score, ax=None):
    # Compute micro-average ROC curve and ROC area
    precision, recall, _ = precision_recall_curve(y_true.ravel(),
                                                  y_score.ravel())

    avg_prec = average_precision_score(y_true, y_score, average="micro")

    if ax is None:
        ax = plt.gca()

    ax.plot(recall, precision,
            label=('micro-average Precision-recall curve (area = {0:0.2f})'
                   .format(avg_prec)))
    _set_ax_settings(ax)
    return ax

Response: Plots precision - recall curve for a single base class.


Instruction:
Explain what the following Python 3 code does
def precision_at(y_true, y_score, proportion, ignore_nas=False):
    '''
    Calculates precision at a given proportion.
    Only supports binary classification.
    '''
    # Sort scores in descending order
    scores_sorted = np.sort(y_score)[::-1]

    # Based on the proportion, get the index to split the data
    # if value is negative, return 0
    cutoff_index = max(int(len(y_true) * proportion) - 1, 0)
    # Get the cutoff value
    cutoff_value = scores_sorted[cutoff_index]

    # Convert scores to binary, by comparing them with the cutoff value
    scores_binary = np.array([int(y >= cutoff_value) for y in y_score])
    # Calculate precision using sklearn function
    if ignore_nas:
        precision = __precision(y_true, scores_binary)
    else:
        precision = precision_score(y_true, scores_binary)

    return precision, cutoff_value

Response: Calculates precision at a given proportion.


Instruction:
Create a Python 3 function for
computing the precision of the data in the cluster hierarchy.

Response:
def __precision(y_true, y_pred):
    '''
        Precision metric tolerant to unlabeled data in y_true,
        NA values are ignored for the precision calculation
    '''
    # make copies of the arrays to avoid modifying the original ones
    y_true = np.copy(y_true)
    y_pred = np.copy(y_pred)

    # precision = tp/(tp+fp)
    # True nehatives do not affect precision value, so for every missing
    # value in y_true, replace it with 0 and also replace the value
    # in y_pred with 0
    is_nan = np.isnan(y_true)
    y_true[is_nan] = 0
    y_pred[is_nan] = 0
    precision = precision_score(y_true, y_pred)
    return precision


Instruction:
Can you generate the documentation for the following Python 3 function
def labels_at(y_true, y_score, proportion, normalize=False):
    '''
        Return the number of labels encountered in the top  X proportion
    '''
    # Get indexes of scores sorted in descending order
    indexes = np.argsort(y_score)[::-1]

    # Sort true values in the same order
    y_true_sorted = y_true[indexes]

    # Grab top x proportion of true values
    cutoff_index = max(int(len(y_true_sorted) * proportion) - 1, 0)
    # add one to index to grab values including that index
    y_true_top = y_true_sorted[:cutoff_index+1]

    # Count the number of non-nas in the top x proportion
    # we are returning a count so it should be an int
    values = int((~np.isnan(y_true_top)).sum())

    if normalize:
        values = float(values)/(~np.isnan(y_true)).sum()

    return values

Response: Return the number of labels encountered in the top X proportion


Instruction:
Can you write a function in Python 3 where it
plots a validation curve for the training set and test set.

Response:
def validation_curve(train_scores, test_scores, param_range, param_name=None,
                     semilogx=False, ax=None):
    """Plot a validation curve

    Plot a metric vs hyperpameter values for the training and test set

    Parameters
    ----------
    train_scores : array-like
        Scores for the training set
    test_scores : array-like
        Scores for the test set
    param_range : array-like
        Hyperparameter values used to generate the curve
    param_range : str
        Hyperparameter name
    semilgo : bool
        Sets a log scale on the x axis
    ax : matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    Examples
    --------

    .. plot:: ../../examples/validation_curve.py

    """

    if ax is None:
        ax = plt.gca()

    if semilogx:
        ax.set_xscale('log')

    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)

    ax.set_title("Validation Curve")
    ax.set_ylabel("Score mean")

    if param_name:
        ax.set_xlabel(param_name)

    ax.plot(param_range, train_scores_mean, label="Training score", color="r")
    ax.plot(param_range, test_scores_mean, label="Cross-validation score",
            color="g")

    ax.fill_between(param_range, train_scores_mean - train_scores_std,
                    train_scores_mean + train_scores_std, alpha=0.2, color="r")
    ax.fill_between(param_range, test_scores_mean - test_scores_std,
                    test_scores_mean + test_scores_std, alpha=0.2, color="g")

    ax.legend(loc="best")
    ax.margins(0.05)
    return ax


Instruction:
Can you generate a brief explanation for the following Python 3 code
def confusion_matrix(y_true, y_pred, target_names=None, normalize=False,
                     cmap=None, ax=None):
    """
    Plot confustion matrix.

    Parameters
    ----------
    y_true : array-like, shape = [n_samples]
        Correct target values (ground truth).
    y_pred : array-like, shape = [n_samples]
        Target predicted classes (estimator predictions).
    target_names : list
        List containing the names of the target classes. List must be in order
        e.g. ``['Label for class 0', 'Label for class 1']``. If ``None``,
        generic labels will be generated e.g. ``['Class 0', 'Class 1']``
    ax: matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes
    normalize : bool
        Normalize the confusion matrix
    cmap : matplotlib Colormap
        If ``None`` uses a modified version of matplotlib's OrRd colormap.

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    Examples
    --------
    .. plot:: ../../examples/confusion_matrix.py

    """
    if any((val is None for val in (y_true, y_pred))):
        raise ValueError("y_true and y_pred are needed to plot confusion "
                         "matrix")

    # calculate how many names you expect
    values = set(y_true).union(set(y_pred))
    expected_len = len(values)

    if target_names and (expected_len != len(target_names)):
        raise ValueError(('Data cointains {} different values, but target'
                         ' names contains {} values.'.format(expected_len,
                                                             len(target_names)
                                                             )))

    # if the user didn't pass target_names, create generic ones
    if not target_names:
        values = list(values)
        values.sort()
        target_names = ['Class {}'.format(v) for v in values]

    cm = sk_confusion_matrix(y_true, y_pred)
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    np.set_printoptions(precision=2)

    if ax is None:
        ax = plt.gca()

    # this (y, x) may sound counterintuitive. The reason is that
    # in a matrix cell (i, j) is in row=i and col=j, translating that
    # to an x, y plane (which matplotlib uses to plot), we need to use
    # i as the y coordinate (how many steps down) and j as the x coordinate
    # how many steps to the right.
    for (y, x), v in np.ndenumerate(cm):
        try:
            label = '{:.2}'.format(v)
        except:
            label = v
        ax.text(x, y, label, horizontalalignment='center',
                verticalalignment='center')

    if cmap is None:
        cmap = default_heatmap()

    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.colorbar(im, ax=ax)
    tick_marks = np.arange(len(target_names))
    ax.set_xticks(tick_marks)
    ax.set_xticklabels(target_names)
    ax.set_yticks(tick_marks)
    ax.set_yticklabels(target_names)

    title = 'Confusion matrix'
    if normalize:
        title += ' (normalized)'
    ax.set_title(title)

    ax.set_ylabel('True label')
    ax.set_xlabel('Predicted label')
    return ax

Response: Plots the confusion matrix for the current language.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def feature_importances(data, top_n=None, feature_names=None, ax=None):
    if data is None:
        raise ValueError('data is needed to plot feature importances. '
                         'When plotting using the evaluator you need to pass '
                         'an estimator ')

    # If no feature_names is provided, assign numbers
    res = compute.feature_importances(data, top_n, feature_names)
    # number of features returned
    n_feats = len(res)

    if ax is None:
        ax = plt.gca()

    ax.set_title("Feature importances")

    try:
        ax.bar(range(n_feats), res.importance, yerr=res.std_, color='red',
               align="center")
    except:
        ax.bar(range(n_feats), res.importance, color='red',
               align="center")

    ax.set_xticks(range(n_feats))
    ax.set_xticklabels(res.feature_name)
    ax.set_xlim([-1, n_feats])
    return ax

Response: Returns a matplotlib Axes object containing the feature importances of each feature in the top_n features.


Instruction:
How would you code a function in Python 3 to
plot precision values at different proportions.

Response:
def precision_at_proportions(y_true, y_score, ax=None):
    """
    Plot precision values at different proportions.

    Parameters
    ----------
    y_true : array-like
        Correct target values (ground truth).
    y_score : array-like
        Target scores (estimator predictions).
    ax : matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    """
    if any((val is None for val in (y_true, y_score))):
        raise ValueError('y_true and y_score are needed to plot precision at '
                         'proportions')

    if ax is None:
        ax = plt.gca()

    y_score_is_vector = is_column_vector(y_score) or is_row_vector(y_score)
    if not y_score_is_vector:
        y_score = y_score[:, 1]

    # Calculate points
    proportions = [0.01 * i for i in range(1, 101)]
    precs_and_cutoffs = [precision_at(y_true, y_score, p) for p in proportions]
    precs, cutoffs = zip(*precs_and_cutoffs)

    # Plot and set nice defaults for title and axis labels
    ax.plot(proportions, precs)
    ax.set_title('Precision at various proportions')
    ax.set_ylabel('Precision')
    ax.set_xlabel('Proportion')
    ticks = [0.1 * i for i in range(1, 11)]
    ax.set_xticks(ticks)
    ax.set_xticklabels(ticks)
    ax.set_yticks(ticks)
    ax.set_yticklabels(ticks)
    ax.set_ylim([0, 1.0])
    ax.set_xlim([0, 1.0])
    return ax


Instruction:
How would you code a function in Python 3 to
return a table with the feature importances for the given data.

Response:
def feature_importances(data, top_n=None, feature_names=None):
    """
    Get and order feature importances from a scikit-learn model
    or from an array-like structure.

    If data is a scikit-learn model with sub-estimators (e.g. RandomForest,
    AdaBoost) the function will compute the standard deviation of each
    feature.

    Parameters
    ----------
    data : sklearn model or array-like structure
        Object to get the data from.
    top_n : int
        Only get results for the top_n features.
    feature_names : array-like
        Feature_names

    Returns
    -------
    table
        Table object with the data. Columns are
        feature_name, importance (`std_` only included for models with
        sub-estimators)

    """
    if data is None:
        raise ValueError('data is needed to tabulate feature importances. '
                         'When plotting using the evaluator you need to pass '
                         'an estimator ')

    res = compute.feature_importances(data, top_n, feature_names)
    return Table(res, res.dtype.names)


Instruction:
Can you write a function in Python 3 where it
parts is a string of ipython text, comprised of at most one input, one output, comments, and blank lines. The block parser parses the text into a list of:: blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and data is, depending on the type of token:: COMMENT : the comment string INPUT: the (DECORATOR, INPUT_LINE, REST) where DECORATOR: the input decorator (or None) INPUT_LINE: the input as string (possibly multi-line) REST : any stdout generated by the input line (not OUTPUT) OUTPUT: the output string, possibly multi-line

Response:
def block_parser(part, rgxin, rgxout, fmtin, fmtout):
    """
    part is a string of ipython text, comprised of at most one
    input, one output, comments, and blank lines.  The block parser
    parses the text into a list of::

      blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]

    where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
    data is, depending on the type of token::

      COMMENT : the comment string

      INPUT: the (DECORATOR, INPUT_LINE, REST) where
         DECORATOR: the input decorator (or None)
         INPUT_LINE: the input as string (possibly multi-line)
         REST : any stdout generated by the input line (not OUTPUT)

      OUTPUT: the output string, possibly multi-line

    """
    block = []
    lines = part.split('\n')
    N = len(lines)
    i = 0
    decorator = None
    while 1:

        if i==N:
            # nothing left to parse -- the last line
            break

        line = lines[i]
        i += 1
        line_stripped = line.strip()
        if line_stripped.startswith('#'):
            block.append((COMMENT, line))
            continue

        if line_stripped.startswith('@'):
            # Here is where we assume there is, at most, one decorator.
            # Might need to rethink this.
            decorator = line_stripped
            continue

        # does this look like an input line?
        matchin = rgxin.match(line)
        if matchin:
            lineno, inputline = int(matchin.group(1)), matchin.group(2)

            # the ....: continuation string
            continuation = '   %s:'%''.join(['.']*(len(str(lineno))+2))
            Nc = len(continuation)
            # input lines can continue on for more than one line, if
            # we have a '\' line continuation char or a function call
            # echo line 'print'.  The input line can only be
            # terminated by the end of the block or an output line, so
            # we parse out the rest of the input line if it is
            # multiline as well as any echo text

            rest = []
            while i<N:

                # look ahead; if the next line is blank, or a comment, or
                # an output line, we're done

                nextline = lines[i]
                matchout = rgxout.match(nextline)
                #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation))
                if matchout or nextline.startswith('#'):
                    break
                elif nextline.startswith(continuation):
                    # The default ipython_rgx* treat the space following the colon as optional.
                    # However, If the space is there we must consume it or code
                    # employing the cython_magic extension will fail to execute.
                    #
                    # This works with the default ipython_rgx* patterns,
                    # If you modify them, YMMV.
                    nextline = nextline[Nc:]
                    if nextline and nextline[0] == ' ':
                        nextline = nextline[1:]

                    inputline += '\n' +  nextline
                else:
                    rest.append(nextline)
                i+= 1

            block.append((INPUT, (decorator, inputline, '\n'.join(rest))))
            continue

        # if it looks like an output line grab all the text to the end
        # of the block
        matchout = rgxout.match(line)
        if matchout:
            lineno, output = int(matchout.group(1)), matchout.group(2)
            if i<N-1:
                output = '\n'.join([output] + lines[i:])

            block.append((OUTPUT, output))
            break

    return block


Instruction:
Write a Python 3 function for
processing a block from the block parser and return a list of processed lines

Response:
def process_block(self, block):
        """
        process block from the block_parser and return a list of processed lines
        """
        ret = []
        output = None
        input_lines = None
        lineno = self.IP.execution_count

        input_prompt = self.promptin % lineno
        output_prompt = self.promptout % lineno
        image_file = None
        image_directive = None

        found_input = False
        for token, data in block:
            if token == COMMENT:
                out_data = self.process_comment(data)
            elif token == INPUT:
                found_input = True
                (out_data, input_lines, output, is_doctest,
                 decorator, image_file, image_directive) = \
                          self.process_input(data, input_prompt, lineno)
            elif token == OUTPUT:
                if not found_input:

                    TAB = ' ' * 4
                    linenumber = 0
                    source = 'Unavailable'
                    content = 'Unavailable'
                    if self.directive:
                        linenumber = self.directive.state.document.current_line
                        source = self.directive.state.document.current_source
                        content = self.directive.content
                        # Add tabs and join into a single string.
                        content = '\n'.join([TAB + line for line in content])

                    e = ('\n\nInvalid block: Block contains an output prompt '
                         'without an input prompt.\n\n'
                         'Document source: {0}\n\n'
                         'Content begins at line {1}: \n\n{2}\n\n'
                         'Problematic block within content: \n\n{TAB}{3}\n\n')
                    e = e.format(source, linenumber, content, block, TAB=TAB)

                    # Write, rather than include in exception, since Sphinx
                    # will truncate tracebacks.
                    sys.stdout.write(e)
                    raise RuntimeError('An invalid block was detected.')

                out_data = \
                    self.process_output(data, output_prompt, input_lines,
                                        output, is_doctest, decorator,
                                        image_file)
                if out_data:
                    # Then there was user submitted output in verbatim mode.
                    # We need to remove the last element of `ret` that was
                    # added in `process_input`, as it is '' and would introduce
                    # an undesirable newline.
                    assert(ret[-1] == '')
                    del ret[-1]

            if out_data:
                ret.extend(out_data)

        # save the image files
        if image_file is not None:
            self.save_image(image_file)

        return ret, image_directive


Instruction:
Write a Python 3 script to
plot a sklearn grid search for a set of items in the order of the items in the list of named tuples.

Response:
def grid_search(grid_scores, change, subset=None, kind='line', cmap=None,
                ax=None):
    """
    Plot results from a sklearn grid search by changing two parameters at most.

    Parameters
    ----------
    grid_scores : list of named tuples
        Results from a sklearn grid search (get them using the
        `grid_scores_` parameter)
    change : str or iterable with len<=2
        Parameter to change
    subset : dictionary-like
        parameter-value(s) pairs to subset from grid_scores.
        (e.g. ``{'n_estimartors': [1, 10]}``), if None all combinations will be
        used.
    kind : ['line', 'bar']
        This only applies whe change is a single parameter. Changes the
        type of plot
    cmap : matplotlib Colormap
        This only applies when change are two parameters. Colormap used for
        the matrix. If None uses a modified version of matplotlib's OrRd
        colormap.
    ax: matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    Examples
    --------

    .. plot:: ../../examples/grid_search.py

    """
    if change is None:
        raise ValueError(('change can\'t be None, you need to select at least'
                          ' one value to make the plot.'))

    if ax is None:
        ax = plt.gca()

    if cmap is None:
        cmap = default_heatmap()

    if isinstance(change, string_types) or len(change) == 1:
        return _grid_search_single(grid_scores, change, subset, kind, ax)
    elif len(change) == 2:
        return _grid_search_double(grid_scores, change, subset, cmap, ax)
    else:
        raise ValueError('change must have length 1 or 2 or be a string')


Instruction:
Make a summary of the following Python 3 code
def roc(self):
        return plot.roc(self.y_true, self.y_score, ax=_gen_ax())

Response: ROC plot for this object


Instruction:
Can you generate the documentation for the following Python 3 function
def precision_recall(self):
        return plot.precision_recall(self.y_true, self.y_score, ax=_gen_ax())

Response: Precision - recall plot


Instruction:
Can you generate a brief explanation for the following Python 3 code
def precision_at_proportions(self):
        return plot.precision_at_proportions(self.y_true, self.y_score,
                                             ax=_gen_ax())

Response: Precision at proportions plot


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_report(self, template, path=None, style=None):
        from .report import generate

        return generate(self, template, path, style)

Response: Generate the HTML report for the current configuration.


Instruction:
How would you explain what the following Python 3 function does
def roc(y_true, y_score, ax=None):
    if any((val is None for val in (y_true, y_score))):
        raise ValueError("y_true and y_score are needed to plot ROC")

    if ax is None:
        ax = plt.gca()

    # get the number of classes based on the shape of y_score
    y_score_is_vector = is_column_vector(y_score) or is_row_vector(y_score)
    if y_score_is_vector:
        n_classes = 2
    else:
        _, n_classes = y_score.shape

    # check data shape?

    if n_classes > 2:
        # convert y_true to binary format
        y_true_bin = label_binarize(y_true, classes=np.unique(y_true))
        _roc_multi(y_true_bin, y_score, ax=ax)
        for i in range(n_classes):
            _roc(y_true_bin[:, i], y_score[:, i], ax=ax)
    else:
        if y_score_is_vector:
            _roc(y_true, y_score, ax)
        else:
            _roc(y_true, y_score[:, 1], ax)

    # raise error if n_classes = 1?
    return ax

Response: Plots ROC curve for the current binary entry set.


Instruction:
Make a summary of the following Python 3 code
def _roc(y_true, y_score, ax=None):
    # check dimensions

    fpr, tpr, _ = roc_curve(y_true, y_score)
    roc_auc = auc(fpr, tpr)

    ax.plot(fpr, tpr, label=('ROC curve (area = {0:0.2f})'.format(roc_auc)))
    _set_ax_settings(ax)
    return ax

Response: Plots ROC curve for binary classification.


Instruction:
Create a Python 3 function for
plotting ROC curve for multi classification.

Response:
def _roc_multi(y_true, y_score, ax=None):
    """
    Plot ROC curve for multi classification.

    Parameters
    ----------
    y_true : array-like, shape = [n_samples, n_classes]
        Correct target values (ground truth).
    y_score : array-like, shape = [n_samples, n_classes]
        Target scores (estimator predictions).
    ax: matplotlib Axes
        Axes object to draw the plot onto, otherwise uses current Axes

    Returns
    -------
    ax: matplotlib Axes
        Axes containing the plot

    """
    # Compute micro-average ROC curve and ROC area
    fpr, tpr, _ = roc_curve(y_true.ravel(), y_score.ravel())
    roc_auc = auc(fpr, tpr)

    if ax is None:
        ax = plt.gca()

    ax.plot(fpr, tpr, label=('micro-average ROC curve (area = {0:0.2f})'
                             .format(roc_auc)))
    _set_ax_settings(ax)
    return ax


Instruction:
How would you explain what the following Python 3 function does
async def get_connection(self, container):
        '''
        Get an exclusive connection, useful for blocked commands and transactions.

        You must call release or shutdown (not recommanded) to return the connection after use.

        :param container: routine container

        :returns: RedisClientBase object, with some commands same as RedisClient like execute_command,
                  batch_execute, register_script etc.
        '''
        if self._connpool:
            conn = self._connpool.pop()
            return RedisClientBase(conn, self)
        else:
            conn = self._create_client(container)
            await RedisClientBase._get_connection(self, container, conn)
            await self._protocol.send_command(conn, container, 'SELECT', str(self.db))
            return RedisClientBase(conn, self)

Response: Get an exclusive connection.


Instruction:
Create a Python 3 function for
executing a command on Redis server.

Response:
async def execute_command(self, container, *args):
        '''
        Execute command on Redis server:
          - For (P)SUBSCRIBE/(P)UNSUBSCRIBE, the command is sent to the subscribe connection.
            It is recommended to use (p)subscribe/(p)unsubscribe method instead of directly call the command
          - For BLPOP, BRPOP, BRPOPLPUSH, the command is sent to a separated connection. The connection is
            recycled after command returns.
          - For other commands, the command is sent to the default connection.
        '''
        if args:
            cmd = _str(args[0]).upper()
            if cmd in ('SUBSCRIBE', 'UNSUBSCRIBE', 'PSUBSCRIBE', 'PUNSUBSCRIBE'):
                await self._get_subscribe_connection(container)
                return await self._protocol.execute_command(self._subscribeconn, container, *args)
            elif cmd in ('BLPOP', 'BRPOP', 'BRPOPLPUSH'):
                c = await self.get_connection(container)
                with c.context(container):
                    return await c.execute_command(container, *args)
        return await RedisClientBase.execute_command(self, container, *args)


Instruction:
Implement a Python 3 function for
subscribing to the specified channels.

Response:
async def subscribe(self, container, *keys):
        '''
        Subscribe to specified channels

        :param container: routine container

        :param *keys: subscribed channels

        :returns: list of event matchers for the specified channels
        '''
        await self._get_subscribe_connection(container)
        realkeys = []
        for k in keys:
            count = self._subscribecounter.get(k, 0)
            if count == 0:
                realkeys.append(k)
            self._subscribecounter[k] = count + 1
        if realkeys:
            await self._protocol.execute_command(self._subscribeconn, container, 'SUBSCRIBE', *realkeys)
        return [self._protocol.subscribematcher(self._subscribeconn, k, None, RedisSubscribeMessageEvent.MESSAGE) for k in keys]


Instruction:
Given the following Python 3 function, write the documentation
async def unsubscribe(self, container, *keys):
        '''
        Unsubscribe specified channels. Every subscribed key should be unsubscribed exactly once, even if duplicated subscribed.

        :param container: routine container

        :param \*keys: subscribed channels
        '''
        await self._get_subscribe_connection(container)
        realkeys = []
        for k in keys:
            count = self._subscribecounter.get(k, 0)
            if count <= 1:
                realkeys.append(k)
                try:
                    del self._subscribecounter[k]
                except KeyError:
                    pass
            else:
                self._subscribecounter[k] = count - 1
        if realkeys:
            await self._protocol.execute_command(self._subscribeconn, container, 'UNSUBSCRIBE', *realkeys)

Response:
Unsubscribe specified channels. Every subscribed key should be unsubscribed exactly once, even if duplicated subscribed.

        :param container: routine container

        :param \*keys: subscribed channels


Instruction:
Create a Python 3 function for
subscribing to specified globs

Response:
async def psubscribe(self, container, *keys):
        '''
        Subscribe to specified globs

        :param container: routine container

        :param \*keys: subscribed globs

        :returns: list of event matchers for the specified globs
        '''
        await self._get_subscribe_connection(container)
        realkeys = []
        for k in keys:
            count = self._psubscribecounter.get(k, 0)
            if count == 0:
                realkeys.append(k)
            self._psubscribecounter[k] = count + 1
        await self._protocol.execute_command(self._subscribeconn, container, 'PSUBSCRIBE', *realkeys)
        return [self._protocol.subscribematcher(self._subscribeconn, k, None, RedisSubscribeMessageEvent.PMESSAGE) for k in keys]


Instruction:
Can you generate the documentation for the following Python 3 function
def subscribe_state_matcher(self, container, connected = True):
        '''
        Return a matcher to match the subscribe connection status.

        :param container: a routine container. NOTICE: this method is not a routine.

        :param connected: if True, the matcher matches connection up. If False, the matcher matches
               connection down.

        :returns: an event matcher.
        '''
        if not self._subscribeconn:
            self._subscribeconn = self._create_client(container)
        return RedisConnectionStateEvent.createMatcher(
                    RedisConnectionStateEvent.CONNECTION_UP if connected else RedisConnectionStateEvent.CONNECTION_DOWN,
                    self._subscribeconn
                    )

Response: Returns a matcher to match the subscribe connection status.


Instruction:
Write a Python 3 function for
escaping a path in a bytestring.

Response:
def _escape_path(key):
    '''
    Replace '/', '\\' in key
    '''
    return _tobytes(key).replace(b'$', b'$_').replace(b'/', b'$+').replace(b'\\', b'$$')


Instruction:
How would you explain what the following Python 3 function does
def reassemble_options(payload):
    '''
    Reassemble partial options to options, returns a list of dhcp_option

    DHCP options are basically `|tag|length|value|` structure. When an
    option is longer than 255 bytes, it can be splitted into multiple
    structures with the same tag. The splitted structures must be
    joined back to get the original option.

    `dhcp_option_partial` is used to present the splitted options,
    and `dhcp_option` is used for reassembled option.
    '''
    options = []
    option_indices = {}
    def process_option_list(partials):
        for p in partials:
            if p.tag == OPTION_END:
                break
            if p.tag == OPTION_PAD:
                continue
            if p.tag in option_indices:
                # Reassemble the data
                options[option_indices[p.tag]][1].append(p.data)
            else:
                options.append((p.tag, [p.data]))
                option_indices[p.tag] = len(options) - 1
    # First process options field
    process_option_list(payload.options)
    if OPTION_OVERLOAD in option_indices:
        # There is an overload option
        data = b''.join(options[option_indices[OPTION_OVERLOAD]][1])
        overload_option = dhcp_overload.create(data)
        if overload_option & OVERLOAD_FILE:
            process_option_list(dhcp_option_partial[0].create(payload.file))
        if overload_option & OVERLOAD_SNAME:
            process_option_list(dhcp_option_partial[0].create(payload.sname))
    def _create_dhcp_option(tag, data):
        opt = dhcp_option(tag = tag)
        opt._setextra(data)
        opt._autosubclass()
        return opt
    return [_create_dhcp_option(tag, b''.join(data)) for tag,data in options]

Response: Reassemble partial options to options returns a list of dhcp_option objects