Instruction:
Can you create a Python 3 function that
handles the login action.

Response:
def login(request, template_name='registration/login.html',
          redirect_field_name=REDIRECT_FIELD_NAME,
          authentication_form=AuthenticationForm,
          current_app=None, extra_context=None):
    """
    Displays the login form and handles the login action.
    """
    redirect_to = request.POST.get(redirect_field_name,
                                   request.GET.get(redirect_field_name, ''))

    if request.method == "POST":
        form = authentication_form(data=request.POST, request=request)
        if form.is_valid():
            netloc = urlparse(redirect_to)[1]

            # Use default setting if redirect_to is empty
            if not redirect_to:
                redirect_to = settings.LOGIN_REDIRECT_URL

            # Heavier security check -- don't allow redirection to a different
            # host.
            elif netloc and netloc != request.get_host():
                redirect_to = settings.LOGIN_REDIRECT_URL

            # Okay, security checks complete. Log the user in.
            auth_login(request, form.get_user())

            return redirect(redirect_to)
    else:
        form = authentication_form(request)

    current_site = get_current_site(request)

    context = {
        'form': form,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    }
    if extra_context is not None:
        context.update(extra_context)
    request.current_app = current_app
    return TemplateResponse(request, template_name, context)


Instruction:
Given the following Python 3 function, write the documentation
def package_manager_owns(self, dist):
        #Installed by distutils/setuptools or external package manager?
        #If location is in site-packages dir, check for .egg-info file
        if dist.location.lower() == get_python_lib().lower():
            filename = os.path.join(dist.location, dist.egg_name() + ".egg-info")
        else:
            filename = dist.location

        status, output = getstatusoutput("/usr/bin/acmefile -q %s" % filename)
        #status == 0 (file was installed by Acme)
        #status == 256 (file was not installed by Acme)
        if status == 0:
            return self.name
        else:
            return ""

Response: Returns True if package manager owns file


Instruction:
How would you explain what the following Python 3 function does
def check_proxy_setting():
    try:
        http_proxy = os.environ['HTTP_PROXY']
    except KeyError:
        return

    if not http_proxy.startswith('http://'):
        match = re.match('(http://)?([-_\.A-Za-z]+):(\d+)', http_proxy)
        #if not match:
        #    raise Exception('Proxy format not recognised: [%s]' % http_proxy)
        os.environ['HTTP_PROXY'] = 'http://%s:%s' % (match.group(2),
                match.group(3))
    return

Response: Checks if the environmental variable HTTP_PROXY is set and returns the transport for xmlrpc.


Instruction:
Implement a Python 3 function for
filtering URL of specified file type.

Response:
def filter_url(pkg_type, url):
    """
    Returns URL of specified file type
    'source', 'egg', or 'all'
    """
    bad_stuff = ["?modtime", "#md5="]
    for junk in bad_stuff:
        if junk in url:
            url = url.split(junk)[0]
            break

    #pkg_spec==dev (svn)
    if url.endswith("-dev"):
        url = url.split("#egg=")[0]

    if pkg_type == "all":
        return url

    elif pkg_type == "source":
        valid_source_types = [".tgz", ".tar.gz", ".zip", ".tbz2", ".tar.bz2"]
        for extension in valid_source_types:
            if url.lower().endswith(extension):
                return url

    elif pkg_type == "egg":
        if url.lower().endswith(".egg"):
            return url


Instruction:
Can you generate a brief explanation for the following Python 3 code
def request(self, host, handler, request_body, verbose):
        '''Send xml-rpc request using proxy'''
        #We get a traceback if we don't have this attribute:
        self.verbose = verbose
        url = 'http://' + host + handler
        request = urllib2.Request(url)
        request.add_data(request_body)
        # Note: 'Host' and 'Content-Length' are added automatically
        request.add_header('User-Agent', self.user_agent)
        request.add_header('Content-Type', 'text/xml')
        proxy_handler = urllib2.ProxyHandler()
        opener = urllib2.build_opener(proxy_handler)
        fhandle = opener.open(request)
        return(self.parse_response(fhandle))

Response: Send xml - rpc request using proxy


Instruction:
Can you generate the documentation for the following Python 3 function
def get_cache(self):
        #This is used by external programs that import `CheeseShop` and don't
        #want a cache file written to ~/.pypi and query PyPI every time.
        if self.no_cache:
            self.pkg_list = self.list_packages()
            return

        if not os.path.exists(self.yolk_dir):
            os.mkdir(self.yolk_dir)
        if os.path.exists(self.pkg_cache_file):
            self.pkg_list = self.query_cached_package_list()
        else:
            self.logger.debug("DEBUG: Fetching package list cache from PyPi...")
            self.fetch_pkg_list()

Response: Get a package name list from disk cache or PyPI cache.


Instruction:
Explain what the following Python 3 code does
def get_xmlrpc_server(self):
        check_proxy_setting()
        if os.environ.has_key('XMLRPC_DEBUG'):
            debug = 1
        else:
            debug = 0
        try:
            return xmlrpclib.Server(XML_RPC_SERVER, transport=ProxyTransport(), verbose=debug)
        except IOError:
            self.logger("ERROR: Can't connect to XML-RPC server: %s" \
                    % XML_RPC_SERVER)

Response: Returns PyPI s XML - RPC server instance


Instruction:
Make a summary of the following Python 3 code
def query_versions_pypi(self, package_name):
        if not package_name in self.pkg_list:
            self.logger.debug("Package %s not in cache, querying PyPI..." \
                    % package_name)
            self.fetch_pkg_list()
        #I have to set version=[] for edge cases like "Magic file extensions"
        #but I'm not sure why this happens. It's included with Python or
        #because it has a space in it's name?
        versions = []
        for pypi_pkg in self.pkg_list:
            if pypi_pkg.lower() == package_name.lower():
                if self.debug:
                    self.logger.debug("DEBUG: %s" % package_name)
                versions = self.package_releases(pypi_pkg)
                package_name = pypi_pkg
                break
        return (package_name, versions)

Response: Fetch list of available versions for a package from the CheeseShop


Instruction:
Can you implement a function in Python 3 that
returns list of pickled package names from PYPI

Response:
def query_cached_package_list(self):
        """Return list of pickled package names from PYPI"""
        if self.debug:
            self.logger.debug("DEBUG: reading pickled cache file")
        return cPickle.load(open(self.pkg_cache_file, "r"))


Instruction:
Here you have a function in Python 3, explain what it does
def fetch_pkg_list(self):
        self.logger.debug("DEBUG: Fetching package name list from PyPI")
        package_list = self.list_packages()
        cPickle.dump(package_list, open(self.pkg_cache_file, "w"))
        self.pkg_list = package_list

Response: Fetch and cache master list of package names from PYPI


Instruction:
Implement a Python 3 function for
querying PYPI via XMLRPC interface using search spec

Response:
def search(self, spec, operator):
        '''Query PYPI via XMLRPC interface using search spec'''
        return self.xmlrpc.search(spec, operator.lower())


Instruction:
Can you generate a brief explanation for the following Python 3 code
def release_data(self, package_name, version):
        try:
            return self.xmlrpc.release_data(package_name, version)
        except xmlrpclib.Fault:
            #XXX Raises xmlrpclib.Fault if you give non-existant version
            #Could this be server bug?
            return

Response: Query PYPI via XMLRPC interface for a pkg s metadata


Instruction:
Can you write a function in Python 3 where it
queries PyPI via XMLRPC interface for a pkg s available versions

Response:
def package_releases(self, package_name):
        """Query PYPI via XMLRPC interface for a pkg's available versions"""
        if self.debug:
            self.logger.debug("DEBUG: querying PyPI for versions of " \
                    + package_name)
        return self.xmlrpc.package_releases(package_name)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_download_urls(self, package_name, version="", pkg_type="all"):

        if version:
            versions = [version]
        else:

            #If they don't specify version, show em all.

            (package_name, versions) = self.query_versions_pypi(package_name)

        all_urls = []
        for ver in versions:
            metadata = self.release_data(package_name, ver)
            for urls in self.release_urls(package_name, ver):
                if pkg_type == "source" and urls['packagetype'] == "sdist":
                    all_urls.append(urls['url'])
                elif pkg_type == "egg" and \
                        urls['packagetype'].startswith("bdist"):
                    all_urls.append(urls['url'])
                elif pkg_type == "all":
                    #All
                    all_urls.append(urls['url'])

            #Try the package's metadata directly in case there's nothing
            #returned by XML-RPC's release_urls()
            if metadata and metadata.has_key('download_url') and \
                        metadata['download_url'] != "UNKNOWN" and \
                        metadata['download_url'] != None:
                if metadata['download_url'] not in all_urls:
                    if pkg_type != "all":
                        url = filter_url(pkg_type, metadata['download_url'])
                        if url:
                            all_urls.append(url)
        return all_urls

Response: Query PyPI for pkg download URI for a packge


Instruction:
How would you code a function in Python 3 to
return a deep copy of the event.

Response:
def clone(self) -> "Event":
        """
        Clone the event

        Returns:
            :class:`slack.events.Event`

        """
        return self.__class__(copy.deepcopy(self.event), copy.deepcopy(self.metadata))


Instruction:
Can you tell what is the following Python 3 function doing
def from_rtm(cls, raw_event: MutableMapping) -> "Event":
        if raw_event["type"].startswith("message"):
            return Message(raw_event)
        else:
            return Event(raw_event)

Response: Create an event from a JSON decoded data coming from the RTM API.


Instruction:
Can you generate the documentation for the following Python 3 function
def from_http(
        cls,
        raw_body: MutableMapping,
        verification_token: Optional[str] = None,
        team_id: Optional[str] = None,
    ) -> "Event":
        """
        Create an event with data coming from the HTTP Event API.

        If the event type is a message a :class:`slack.events.Message` is returned.

        Args:
            raw_body: Decoded body of the Event API request
            verification_token: Slack verification token used to verify the request came from slack
            team_id: Verify the event is for the correct team

        Returns:
            :class:`slack.events.Event` or :class:`slack.events.Message`

        Raises:
            :class:`slack.exceptions.FailedVerification`: when `verification_token` or `team_id` does not match the
                                                          incoming event's.
        """
        if verification_token and raw_body["token"] != verification_token:
            raise exceptions.FailedVerification(raw_body["token"], raw_body["team_id"])

        if team_id and raw_body["team_id"] != team_id:
            raise exceptions.FailedVerification(raw_body["token"], raw_body["team_id"])

        if raw_body["event"]["type"].startswith("message"):
            return Message(raw_body["event"], metadata=raw_body)
        else:
            return Event(raw_body["event"], metadata=raw_body)

Response: Create an event from the HTTP request body.


Instruction:
Here you have a function in Python 3, explain what it does
def response(self, in_thread: Optional[bool] = None) -> "Message":
        data = {"channel": self["channel"]}

        if in_thread:
            if "message" in self:
                data["thread_ts"] = (
                    self["message"].get("thread_ts") or self["message"]["ts"]
                )
            else:
                data["thread_ts"] = self.get("thread_ts") or self["ts"]
        elif in_thread is None:
            if "message" in self and "thread_ts" in self["message"]:
                data["thread_ts"] = self["message"]["thread_ts"]
            elif "thread_ts" in self:
                data["thread_ts"] = self["thread_ts"]

        return Message(data)

Response: Create a new response message.


Instruction:
Write a Python 3 script to
serialize the message for sending to Slack API

Response:
def serialize(self) -> dict:
        """
        Serialize the message for sending to slack API

        Returns:
            serialized message
        """
        data = {**self}
        if "attachments" in self:
            data["attachments"] = json.dumps(self["attachments"])
        return data


Instruction:
How would you explain what the following Python 3 function does
def register(self, event_type: str, handler: Any, **detail: Any) -> None:
        LOG.info("Registering %s, %s to %s", event_type, detail, handler)
        if len(detail) > 1:
            raise ValueError("Only one detail can be provided for additional routing")
        elif not detail:
            detail_key, detail_value = "*", "*"
        else:
            detail_key, detail_value = detail.popitem()

        if detail_key not in self._routes[event_type]:
            self._routes[event_type][detail_key] = {}

        if detail_value not in self._routes[event_type][detail_key]:
            self._routes[event_type][detail_key][detail_value] = []

        self._routes[event_type][detail_key][detail_value].append(handler)

Response: Register a new handler for a specific event type.


Instruction:
Can you generate the documentation for the following Python 3 function
def dispatch(self, event: Event) -> Iterator[Any]:
        LOG.debug('Dispatching event "%s"', event.get("type"))
        if event["type"] in self._routes:
            for detail_key, detail_values in self._routes.get(
                event["type"], {}
            ).items():
                event_value = event.get(detail_key, "*")
                yield from detail_values.get(event_value, [])
        else:
            return

Response: Dispatches the event to the handlers that match the routing of the incoming event.


Instruction:
Given the following Python 3 function, write the documentation
def register(
        self,
        pattern: str,
        handler: Any,
        flags: int = 0,
        channel: str = "*",
        subtype: Optional[str] = None,
    ) -> None:
        """
        Register a new handler for a specific :class:`slack.events.Message`.

        The routing is based on regex pattern matching the message text and the incoming slack channel.

        Args:
            pattern: Regex pattern matching the message text.
            handler: Callback
            flags: Regex flags.
            channel: Slack channel ID. Use * for any.
            subtype: Message subtype
        """
        LOG.debug('Registering message endpoint "%s: %s"', pattern, handler)
        match = re.compile(pattern, flags)

        if subtype not in self._routes[channel]:
            self._routes[channel][subtype] = dict()

        if match in self._routes[channel][subtype]:
            self._routes[channel][subtype][match].append(handler)
        else:
            self._routes[channel][subtype][match] = [handler]

Response: Register a new handler for a specific message.


Instruction:
Implement a Python 3 function for
yielding handlers matching the routing of the incoming message.

Response:
def dispatch(self, message: Message) -> Iterator[Any]:
        """
        Yields handlers matching the routing of the incoming :class:`slack.events.Message`

        Args:
            message: :class:`slack.events.Message`

        Yields:
            handler
        """
        if "text" in message:
            text = message["text"] or ""
        elif "message" in message:
            text = message["message"].get("text", "")
        else:
            text = ""

        msg_subtype = message.get("subtype")

        for subtype, matchs in itertools.chain(
            self._routes[message["channel"]].items(), self._routes["*"].items()
        ):
            if msg_subtype == subtype or subtype is None:
                for match, endpoints in matchs.items():
                    if match.search(text):
                        yield from endpoints


Instruction:
Make a summary of the following Python 3 code
def query(  # type: ignore
        self,
        url: Union[str, methods],
        data: Optional[MutableMapping] = None,
        headers: Optional[MutableMapping] = None,
        as_json: Optional[bool] = None,
    ) -> dict:
        """
        Query the slack API

        When using :class:`slack.methods` the request is made `as_json` if available

        Args:
            url: :class:`slack.methods` or url string
            data: JSON encodable MutableMapping
            headers: Custom headers
            as_json: Post JSON to the slack API
        Returns:
            dictionary of slack API response data

        """
        url, body, headers = sansio.prepare_request(
            url=url,
            data=data,
            headers=headers,
            global_headers=self._headers,
            token=self._token,
        )
        return self._make_query(url, body, headers)

Response: Query the slack API for the specified resource.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rtm(  # type: ignore
        self, url: Optional[str] = None, bot_id: Optional[str] = None
    ) -> Iterator[events.Event]:
        """
        Iterate over event from the RTM API

        Args:
            url: Websocket connection url
            bot_id: Connecting bot ID

        Returns:
            :class:`slack.events.Event` or :class:`slack.events.Message`

        """
        while True:
            bot_id = bot_id or self._find_bot_id()
            url = url or self._find_rtm_url()
            for event in self._incoming_from_rtm(url, bot_id):
                yield event
            url = None

Response: Iterate over events from the RTM API.


Instruction:
Implement a function in Python 3 to
display the login form for the given HttpRequest.

Response:
def login(self, request, extra_context=None):
        """
        Displays the login form for the given HttpRequest.
        """
        context = {
            'title': _('Log in'),
            'app_path': request.get_full_path(),
        }
        if (REDIRECT_FIELD_NAME not in request.GET and
                REDIRECT_FIELD_NAME not in request.POST):
            context[REDIRECT_FIELD_NAME] = request.get_full_path()
        context.update(extra_context or {})
        defaults = {
            'extra_context': context,
            'current_app': self.name,
            'authentication_form': self.login_form or AdminAuthenticationForm,
            'template_name': self.login_template or 'admin/login.html',
        }
        return login(request, **defaults)


Instruction:
Make a summary of the following Python 3 code
def get_config(config_file):
    def load(fp):
        try:
            return yaml.safe_load(fp)
        except yaml.YAMLError as e:
            sys.stderr.write(text_type(e))
            sys.exit(1)  # TODO document exit codes

    if config_file == '-':
        return load(sys.stdin)
    if not os.path.exists(config_file):
        sys.stderr.write('ERROR: Must either run next to config.yaml or'
            ' specify a config file.\n' + __doc__)
        sys.exit(2)
    with open(config_file) as fp:
        return load(fp)

Response: Get configuration from a file.


Instruction:
Can you tell what is the following Python 3 function doing
def get_options(config_options, local_options, cli_options):
    options = DEFAULT_OPTIONS.copy()
    if config_options is not None:
        options.update(config_options)
    if local_options is not None:
        options.update(local_options)
    if cli_options is not None:
        options.update(cli_options)
    return options

Response: Get the options to use for the base base.


Instruction:
Given the following Python 3 function, write the documentation
def output_results(results, metric, options):
    formatter = options['Formatter']
    context = metric.copy()  # XXX might need to sanitize this
    try:
        context['dimension'] = list(metric['Dimensions'].values())[0]
    except AttributeError:
        context['dimension'] = ''
    for result in results:
        stat_keys = metric['Statistics']
        if not isinstance(stat_keys, list):
            stat_keys = [stat_keys]
        for statistic in stat_keys:
            context['statistic'] = statistic
            # get and then sanitize metric name, first copy the unit name from the
            # result to the context to keep the default format happy
            context['Unit'] = result['Unit']
            metric_name = (formatter % context).replace('/', '.').lower()
            line = '{0} {1} {2}\n'.format(
                metric_name,
                result[statistic],
                timegm(result['Timestamp'].timetuple()),
            )
            sys.stdout.write(line)

Response: Output the results to stdout.


Instruction:
Can you create a Python 3 function that
downloads the file at gsuri to localpath.

Response:
def download_to_path(self, gsuri, localpath, binary_mode=False, tmpdir=None):
        """
        This method is analogous to "gsutil cp gsuri localpath", but in a
        programatically accesible way. The only difference is that we
        have to make a guess about the encoding of the file to not upset
        downstream file operations. If you are downloading a VCF, then
        "False" is great. If this is a BAM file you are asking for, you
        should enable the "binary_mode" to make sure file doesn't get
        corrupted.

        gsuri: full GS-based URI, e.g. gs://cohorts/rocks.txt
        localpath: the path for the downloaded file, e.g. /mnt/cohorts/yep.txt
        binary_mode: (logical) if yes, the binary file operations will be
                     used; if not, standard ascii-based ones.
        """
        bucket_name, gs_rel_path = self.parse_uri(gsuri)
        # And now request the handles for bucket and the file
        bucket = self._client.get_bucket(bucket_name)
        # Just assignment, no downloading (yet)
        ablob = bucket.get_blob(gs_rel_path)
        if not ablob:
            raise GoogleStorageIOError(
                "No such file on Google Storage: '{}'".format(gs_rel_path))

        # A tmp file to serve intermediate phase
        # should be on same filesystem as localpath
        tmp_fid, tmp_file_path = tempfile.mkstemp(text=(not binary_mode),
                                                  dir=tmpdir)
        # set chunk_size to reasonable default
        # https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2222
        ablob.chunk_size = 1<<30
        # Download starts in a sec....
        ablob.download_to_filename(client=self._client, filename=tmp_file_path)
        # ... end download ends. Let's move our finished file over.

        # You will see that below, instead of directly writing to a file
        # we are instead first using a different file and then move it to
        # its final location. We are doing this because we don't want
        # corrupted/incomplete data to be around as much as possible.
        return os.rename(tmp_file_path, localpath)


Instruction:
How would you explain what the following Python 3 function does
def round_float(f, digits, rounding=ROUND_HALF_UP):
    return Decimal(str(f)).quantize(Decimal(10) ** (-1 * digits),
                                    rounding=rounding)

Response: Round a float to a number of digits.


Instruction:
Can you implement a function in Python 3 that
returns a string representing a float.

Response:
def float_str(f, min_digits=2, max_digits=6):
    """
    Returns a string representing a float, where the number of
    significant digits is min_digits unless it takes more digits
    to hit a non-zero digit (and the number is 0 < x < 1).
    We stop looking for a non-zero digit after max_digits.
    """
    if f >= 1 or f <= 0:
        return str(round_float(f, min_digits))
    start_str = str(round_float(f, max_digits))
    digits = start_str.split(".")[1]
    non_zero_indices = []
    for i, digit in enumerate(digits):
        if digit != "0":
            non_zero_indices.append(i + 1)
    # Only saw 0s.
    if len(non_zero_indices) == 0:
        num_digits = min_digits
    else:
        # Of the non-zero digits, pick the num_digit'th of those (including any zeros)
        min_non_zero_indices = range(non_zero_indices[0], non_zero_indices[-1] + 1)[:min_digits]
        num_digits = min_non_zero_indices[-1]
    return str(round_float(f, num_digits))


Instruction:
Here you have a function in Python 3, explain what it does
def default_format(self):
        user = self.user
        if user.first_name is not None:
            return self.full_name
        elif user.username is not None:
            return user.username
        else:
            return str(user.id)

Response: Returns the default format of the user.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def full_name(self):
        formatted_user = []
        if self.user.first_name is not None:
            formatted_user.append(self.user.first_name)
        if self.user.last_name is not None:
            formatted_user.append(self.user.last_name)
        return " ".join(formatted_user)

Response: Returns the full name of the user in a human readable format.


Instruction:
Create a Python 3 function to
return the full name of the user and the username of the user.

Response:
def full_format(self):
        """
        Returns the full name (first and last parts), and the username between brackets if the user has it.
        If there is no info about the user, returns the user id between < and >.
        """
        formatted_user = self.full_name
        if self.user.username is not None:
            formatted_user += " [" + self.user.username + "]"
        if not formatted_user:
            formatted_user = self._id()
        return formatted_user


Instruction:
Can you generate a brief explanation for the following Python 3 code
def full_data(self):
        data = [
            self.full_name,
            self._username(),
            self._id(),
            self._language_code(),
            self._is_bot()
        ]
        return " ".join(filter(None, data))

Response: Returns the full data for the user in the format of name username id language code bot_or_user.


Instruction:
Write a Python 3 function that can
return the full data for the current user in the format of a chat.

Response:
def full_data(self):
        """
        Returns all the info available for the chat in the following format:
            title [username] (type) <id>
        If any data is not available, it is not added.
        """
        data = [
            self.chat.title,
            self._username(),
            self._type(),
            self._id()
        ]
        return " ".join(filter(None, data))


Instruction:
Given the following Python 3 function, write the documentation
def count_function(func):
    # Fall back to Cohort-level defaults.
    @use_defaults
    @wraps(func)
    def wrapper(row, cohort, filter_fn=None, normalized_per_mb=None, **kwargs):
        per_patient_data = func(row=row,
                                cohort=cohort,
                                filter_fn=filter_fn,
                                normalized_per_mb=normalized_per_mb,
                                **kwargs)
        patient_id = row["patient_id"]
        if patient_id in per_patient_data:
            count = len(per_patient_data[patient_id])
            if normalized_per_mb:
                count /= float(get_patient_to_mb(cohort)[patient_id])
            return count
        return np.nan
    return wrapper

Response: Decorator for functions that return a dict of collections that should be counted up.


Instruction:
Can you implement a function in Python 3 that
returns a function that counts variants that are filtered by the provided filterable_variant_function.

Response:
def count_variants_function_builder(function_name, filterable_variant_function=None):
    """
    Creates a function that counts variants that are filtered by the provided filterable_variant_function.
    The filterable_variant_function is a function that takes a filterable_variant and returns True or False.

    Users of this builder need not worry about applying e.g. the Cohort's default `filter_fn`. That will be applied as well.
    """
    @count_function
    def count(row, cohort, filter_fn, normalized_per_mb, **kwargs):
        def count_filter_fn(filterable_variant, **kwargs):
            assert filter_fn is not None, "filter_fn should never be None, but it is."
            return ((filterable_variant_function(filterable_variant) if filterable_variant_function is not None else True) and
                    filter_fn(filterable_variant, **kwargs))
        patient_id = row["patient_id"]
        return cohort.load_variants(
            patients=[cohort.patient_from_id(patient_id)],
            filter_fn=count_filter_fn,
            **kwargs)
    count.__name__ = function_name
    count.__doc__ = str("".join(inspect.getsourcelines(filterable_variant_function)[0])) if filterable_variant_function is not None else ""
    return count


Instruction:
Can you tell what is the following Python 3 function doing
def count_effects_function_builder(function_name, only_nonsynonymous, filterable_effect_function=None):
    @count_function
    def count(row, cohort, filter_fn, normalized_per_mb, **kwargs):
        def count_filter_fn(filterable_effect, **kwargs):
            assert filter_fn is not None, "filter_fn should never be None, but it is."
            return ((filterable_effect_function(filterable_effect) if filterable_effect_function is not None else True) and
                    filter_fn(filterable_effect, **kwargs))
        # This only loads one effect per variant.
        patient_id = row["patient_id"]
        return cohort.load_effects(
            only_nonsynonymous=only_nonsynonymous,
            patients=[cohort.patient_from_id(patient_id)],
            filter_fn=count_filter_fn,
            **kwargs)
    count.__name__ = function_name
    count.__doc__ = (("only_nonsynonymous=%s\n" % only_nonsynonymous) +
                     str("".join(inspect.getsourcelines(filterable_effect_function)[0])) if filterable_effect_function is not None else "")
    # Keep track of these to be able to query the returned function for these attributes
    count.only_nonsynonymous = only_nonsynonymous
    count.filterable_effect_function = filterable_effect_function
    return count

Response: Creates a function that counts effects that are filtered by the provided filterable_effect_function.


Instruction:
Here you have a function in Python 3, explain what it does
def median_vaf_purity(row, cohort, **kwargs):
    patient_id = row["patient_id"]
    patient = cohort.patient_from_id(patient_id)
    variants = cohort.load_variants(patients=[patient], filter_fn=no_filter)
    if patient_id in variants.keys():
        variants = variants[patient_id]
    else:
        return np.nan
    def grab_vaf(variant):
        filterable_variant = FilterableVariant(variant, variants, patient)
        return variant_stats_from_variant(variant, filterable_variant.variant_metadata).tumor_stats.variant_allele_frequency
    vafs = [grab_vaf(variant) for variant in variants]
    return 2 * pd.Series(vafs).median()

Response: Estimate purity based on 2 * median VAF.


Instruction:
How would you code a function in Python 3 to
calculate the boostrapped AUC for a given column trying to predict a pred_col.

Response:
def bootstrap_auc(df, col, pred_col, n_bootstrap=1000):
    """
    Calculate the boostrapped AUC for a given col trying to predict a pred_col.

    Parameters
    ----------
    df : pandas.DataFrame
    col : str
        column to retrieve the values from
    pred_col : str
        the column we're trying to predict
    n_boostrap : int
        the number of bootstrap samples

    Returns
    -------
    list : AUCs for each sampling
    """
    scores = np.zeros(n_bootstrap)
    old_len = len(df)
    df.dropna(subset=[col], inplace=True)
    new_len = len(df)
    if new_len < old_len:
        logger.info("Dropping NaN values in %s to go from %d to %d rows" % (col, old_len, new_len))
    preds = df[pred_col].astype(int)
    for i in range(n_bootstrap):
        sampled_counts, sampled_pred = resample(df[col], preds)
        if is_single_class(sampled_pred, col=pred_col):
            continue
        scores[i] = roc_auc_score(sampled_pred, sampled_counts)
    return scores


Instruction:
Make a summary of the following Python 3 code
def set_callbacks(self, worker_start_callback: callable, worker_end_callback: callable, are_async: bool = False):
        # We are setting self.worker_start_callback and self.worker_end_callback
        # to lambdas instead of saving them in private vars and moving the lambda logic
        # to a member function for, among other reasons, making callback updates atomic,
        # ie. once a callback has been posted, it will be executed as it was in that
        # moment, any call to set_callbacks will only affect callbacks posted since they
        # were updated, but not to any pending callback.

        # If callback is async, execute the start callback in the calling thread
        scheduler = self.immediate if are_async else self.background
        self.worker_start_callback = lambda worker: scheduler(Work(
            lambda: worker_start_callback(worker), "worker_start_callback:" + worker.name
        ))

        # As the end callback is called *just* before the thread dies,
        # there is no problem running it on the thread
        self.worker_end_callback = lambda worker: self.immediate(Work(
            lambda: worker_end_callback(worker), "worker_end_callback:" + worker.name
        ))

Response: Set the callbacks for the current thread.


Instruction:
How would you explain what the following Python 3 function does
def _start_worker(self, worker: Worker):
        # This function is called from main thread and from worker pools threads to start their children threads
        with self.running_workers_lock:
            self.running_workers.append(worker)
        thread = SchedulerThread(worker, self._worker_ended)
        thread.start()
        # This may or may not be posted to a background thread (see set_callbacks)
        self.worker_start_callback(worker)

Response: Starts a worker thread and sets the callback to the worker thread.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def new_worker(self, name: str):
        if not self.running:
            return self.immediate_worker
        worker = self._new_worker(name)
        self._start_worker(worker)
        return worker

Response: Creates a new Worker and starts a new Thread with it. Returns the Worker.


Instruction:
Can you write a function in Python 3 where it
creates a new worker pool and starts it.

Response:
def new_worker_pool(self, name: str, min_workers: int = 0, max_workers: int = 1,
                        max_seconds_idle: int = DEFAULT_WORKER_POOL_MAX_SECONDS_IDLE):
        """
        Creates a new worker pool and starts it.
        Returns the Worker that schedules works to the pool.
        """
        if not self.running:
            return self.immediate_worker
        worker = self._new_worker_pool(name, min_workers, max_workers, max_seconds_idle)
        self._start_worker_pool(worker)
        return worker


Instruction:
Implement a Python 3 function for
returning this Cohort as a DataFrame and optionally include additional columns using on.

Response:
def as_dataframe(self, on=None, join_with=None, join_how=None,
                     return_cols=False, rename_cols=False,
                     keep_paren_contents=True, **kwargs):
        """
        Return this Cohort as a DataFrame, and optionally include additional columns
        using `on`.

        on : str or function or list or dict, optional
            - A column name.
            - Or a function that creates a new column for comparison, e.g. count.snv_count.
            - Or a list of column-generating functions or column names.
            - Or a map of new column names to their column-generating functions or column names.

        If `on` is a function or functions, kwargs is passed to those functions.
        Otherwise kwargs is ignored.

        Other parameters
        ----------------
        `return_cols`: (bool)
            If True, return column names generated via `on` along with the `DataFrame`
            as a `DataFrameHolder` tuple.
        `rename_cols`: (bool)
            If True, then return columns using "stripped" column names
            ("stripped" means lower-case names without punctuation other than `_`)
            See `utils.strip_column_names` for more details
            defaults to False
        `keep_paren_contents`: (bool)
            If True, then contents of column names within parens are kept.
            If False, contents of column names within-parens are dropped.
            Defaults to True
        ----------

        Return : `DataFrame` (or `DataFrameHolder` if `return_cols` is True)
        """
        df = self._as_dataframe_unmodified(join_with=join_with, join_how=join_how)
        if on is None:
            return DataFrameHolder.return_obj(None, df, return_cols)

        if type(on) == str:
            return DataFrameHolder.return_obj(on, df, return_cols)

        def apply_func(on, col, df):
            """
            Sometimes we have functions that, by necessity, have more parameters
            than just `row`. We construct a function with just the `row` parameter
            so it can be sent to `DataFrame.apply`. We hackishly pass `cohort`
            (as `self`) along if the function accepts a `cohort` argument.
            """
            on_argnames = on.__code__.co_varnames
            if "cohort" not in on_argnames:
                func = lambda row: on(row=row, **kwargs)
            else:
                func = lambda row: on(row=row, cohort=self, **kwargs)

            if self.show_progress:
                tqdm.pandas(desc=col)
                df[col] = df.progress_apply(func, axis=1) ## depends on tqdm on prev line
            else:
                df[col] = df.apply(func, axis=1)
            return DataFrameHolder(col, df)

        def func_name(func, num=0):
            return func.__name__ if not is_lambda(func) else "column_%d" % num

        def is_lambda(func):
            return func.__name__ == (lambda: None).__name__

        if type(on) == FunctionType:
            return apply_func(on, func_name(on), df).return_self(return_cols)

        if len(kwargs) > 0:
            logger.warning("Note: kwargs used with multiple functions; passing them to all functions")

        if type(on) == dict:
            cols = []
            for key, value in on.items():
                if type(value) == str:
                    df[key] = df[value]
                    col = key
                elif type(value) == FunctionType:
                    col, df = apply_func(on=value, col=key, df=df)
                else:
                    raise ValueError("A value of `on`, %s, is not a str or function" % str(value))
                cols.append(col)
        if type(on) == list:
            cols = []
            for i, elem in enumerate(on):
                if type(elem) == str:
                    col = elem
                elif type(elem) == FunctionType:
                    col = func_name(elem, i)
                    col, df = apply_func(on=elem, col=col, df=df)
                cols.append(col)

        if rename_cols:
            rename_dict = _strip_column_names(df.columns, keep_paren_contents=keep_paren_contents)
            df.rename(columns=rename_dict, inplace=True)
            cols = [rename_dict[col] for col in cols]
        return DataFrameHolder(cols, df).return_self(return_cols)


Instruction:
Create a Python 3 function for
loading a single DataFrame from the DataFrameLoader with the specified name.

Response:
def load_dataframe(self, df_loader_name):
        """
        Instead of joining a DataFrameJoiner with the Cohort in `as_dataframe`, sometimes
        we may want to just directly load a particular DataFrame.
        """
        logger.debug("loading dataframe: {}".format(df_loader_name))
        # Get the DataFrameLoader object corresponding to this name.
        df_loaders = [df_loader for df_loader in self.df_loaders if df_loader.name == df_loader_name]

        if len(df_loaders) == 0:
            raise ValueError("No DataFrameLoader with name %s" % df_loader_name)
        if len(df_loaders) > 1:
            raise ValueError("Multiple DataFrameLoaders with name %s" % df_loader_name)

        return df_loaders[0].load_dataframe()


Instruction:
Write a Python 3 function for
returning name of function in the cache

Response:
def _get_function_name(self, fn, default="None"):
        """ Return name of function, using default value if function not defined
        """
        if fn is None:
            fn_name = default
        else:
            fn_name = fn.__name__
        return fn_name


Instruction:
Write a Python 3 script for
loading a dictionary of patient_id to VariantCollection that contains all the variants that are present in the given patients.

Response:
def load_variants(self, patients=None, filter_fn=None, **kwargs):
        """Load a dictionary of patient_id to varcode.VariantCollection

        Parameters
        ----------
        patients : str, optional
            Filter to a subset of patients
        filter_fn : function
            Takes a FilterableVariant and returns a boolean. Only variants returning True are preserved.
            Overrides default self.filter_fn. `None` passes through to self.filter_fn.

        Returns
        -------
        merged_variants
            Dictionary of patient_id to VariantCollection
        """
        filter_fn = first_not_none_param([filter_fn, self.filter_fn], no_filter)
        filter_fn_name = self._get_function_name(filter_fn)
        logger.debug("loading variants with filter_fn: {}".format(filter_fn_name))
        patient_variants = {}

        for patient in self.iter_patients(patients):
            variants = self._load_single_patient_variants(patient, filter_fn, **kwargs)
            if variants is not None:
                patient_variants[patient.id] = variants
        return patient_variants


Instruction:
How would you explain what the following Python 3 function does
def _hash_filter_fn(self, filter_fn, **kwargs):
        filter_fn_name = self._get_function_name(filter_fn, default="filter-none")
        logger.debug("Computing hash for filter_fn: {} with kwargs {}".format(filter_fn_name, str(dict(**kwargs))))
        # hash function source code
        fn_source = str(dill.source.getsource(filter_fn))
        pickled_fn_source = pickle.dumps(fn_source) ## encode as byte string
        hashed_fn_source = int(hashlib.sha1(pickled_fn_source).hexdigest(), 16) % (10 ** 11)
        # hash kwarg values
        kw_dict = dict(**kwargs)
        kw_hash = list()
        if not kw_dict:
            kw_hash = ["default"]
        else:
            [kw_hash.append("{}-{}".format(key, h)) for (key, h) in sorted(kw_dict.items())]
        # hash closure vars - for case where filter_fn is defined within closure of filter_fn
        closure = []
        nonlocals = inspect.getclosurevars(filter_fn).nonlocals
        for (key, val) in nonlocals.items():
            ## capture hash for any function within closure
            if inspect.isfunction(val):
                closure.append(self._hash_filter_fn(val))
        closure.sort() # Sorted for file name consistency
        closure_str = "null" if len(closure) == 0 else "-".join(closure)
        # construct final string comprising hashed components
        hashed_fn = ".".join(["-".join([filter_fn_name,
                                        str(hashed_fn_source)]),
                              ".".join(kw_hash),
                              closure_str]
                            )
        return hashed_fn

Response: Construct a hash for a function and keyword arguments


Instruction:
How would you code a function in Python 3 to
load filtered merged variants for a single patient optionally using cache

Response:
def _load_single_patient_variants(self, patient, filter_fn, use_cache=True, **kwargs):
        """ Load filtered, merged variants for a single patient, optionally using cache

            Note that filtered variants are first merged before filtering, and
                each step is cached independently. Turn on debug statements for more
                details about cached files.

            Use `_load_single_patient_merged_variants` to see merged variants without filtering.
        """
        if filter_fn is None:
            use_filtered_cache = False
        else:
            filter_fn_name = self._get_function_name(filter_fn)
            logger.debug("loading variants for patient {} with filter_fn {}".format(patient.id, filter_fn_name))
            use_filtered_cache = use_cache

        ## confirm that we can get cache-name (else don't use filtered cache)
        if use_filtered_cache:
            logger.debug("... identifying filtered-cache file name")
            try:
                ## try to load filtered variants from cache
                filtered_cache_file_name = "%s-variants.%s.pkl" % (self.merge_type,
                                                                   self._hash_filter_fn(filter_fn, **kwargs))
            except:
                logger.warning("... error identifying filtered-cache file name for patient {}: {}".format(
                        patient.id, filter_fn_name))
                use_filtered_cache = False
            else:
                logger.debug("... trying to load filtered variants from cache: {}".format(filtered_cache_file_name))
                try:
                    cached = self.load_from_cache(self.cache_names["variant"], patient.id, filtered_cache_file_name)
                    if cached is not None:
                        return cached
                except:
                    logger.warning("Error loading variants from cache for patient: {}".format(patient.id))
                    pass

        ## get merged variants
        logger.debug("... getting merged variants for: {}".format(patient.id))
        merged_variants = self._load_single_patient_merged_variants(patient, use_cache=use_cache)

        # Note None here is different from 0. We want to preserve None
        if merged_variants is None:
            logger.info("Variants did not exist for patient %s" % patient.id)
            return None

        logger.debug("... applying filters to variants for: {}".format(patient.id))
        filtered_variants = filter_variants(variant_collection=merged_variants,
                                            patient=patient,
                                            filter_fn=filter_fn,
                                            **kwargs)
        if use_filtered_cache:
            logger.debug("... saving filtered variants to cache: {}".format(filtered_cache_file_name))
            self.save_to_cache(filtered_variants, self.cache_names["variant"], patient.id, filtered_cache_file_name)
        return filtered_variants


Instruction:
Can you tell what is the following Python 3 function doing
def _load_single_patient_merged_variants(self, patient, use_cache=True):
        logger.debug("loading merged variants for patient {}".format(patient.id))
        no_variants = False
        try:
            # get merged-variants from cache
            if use_cache:
                ## load unfiltered variants into list of collections
                variant_cache_file_name = "%s-variants.pkl" % (self.merge_type)
                merged_variants = self.load_from_cache(self.cache_names["variant"], patient.id, variant_cache_file_name)
                if merged_variants is not None:
                    return merged_variants
            # get variant collections from file
            variant_collections = []
            optional_maf_cols = ["t_ref_count", "t_alt_count", "n_ref_count", "n_alt_count"]
            if self.additional_maf_cols is not None:
                optional_maf_cols.extend(self.additional_maf_cols)
            for patient_variants in patient.variants_list:
                if type(patient_variants) == str:
                    if ".vcf" in patient_variants:
                        try:
                            variant_collections.append(varcode.load_vcf_fast(patient_variants))
                        # StopIteration is thrown for empty VCFs. For an empty VCF, don't append any variants,
                        # and don't throw an error. But do record a warning, in case the StopIteration was
                        # thrown for another reason.
                        except StopIteration as e:
                            logger.warning("Empty VCF (or possibly a VCF error) for patient {}: {}".format(
                                patient.id, str(e)))
                    elif ".maf" in patient_variants:
                        # See variant_stats.maf_somatic_variant_stats
                        variant_collections.append(
                            varcode.load_maf(
                                patient_variants,
                                optional_cols=optional_maf_cols,
                                encoding="latin-1"))
                    else:
                        raise ValueError("Don't know how to read %s" % patient_variants)
                elif type(patient_variants) == VariantCollection:
                    variant_collections.append(patient_variants)
                else:
                    raise ValueError("Don't know how to read %s" % patient_variants)
            # merge variant-collections
            if len(variant_collections) == 0:
                no_variants = True
            elif len(variant_collections) == 1:
                # There is nothing to merge
                variants = variant_collections[0]
                merged_variants = variants
            else:
                merged_variants = self._merge_variant_collections(variant_collections, self.merge_type)
        except IOError:
            no_variants = True

        # Note that this is the number of variant collections and not the number of
        # variants. 0 variants will lead to 0 neoantigens, for example, but 0 variant
        # collections will lead to NaN variants and neoantigens.
        if no_variants:
            print("Variants did not exist for patient %s" % patient.id)
            merged_variants = None

        # save merged variants to file
        if use_cache:
            self.save_to_cache(merged_variants, self.cache_names["variant"], patient.id, variant_cache_file_name)
        return merged_variants

Response: Load merged variants for a single patient.


Instruction:
Can you generate the documentation for the following Python 3 function
def load_polyphen_annotations(self, as_dataframe=False,
                                  filter_fn=None):
        """Load a dataframe containing polyphen2 annotations for all variants

        Parameters
        ----------
        database_file : string, sqlite
            Path to the WHESS/Polyphen2 SQLite database.
            Can be downloaded and bunzip2"ed from http://bit.ly/208mlIU
        filter_fn : function
            Takes a FilterablePolyphen and returns a boolean.
            Only annotations returning True are preserved.
            Overrides default self.filter_fn. `None` passes through to self.filter_fn.

        Returns
        -------
        annotations
            Dictionary of patient_id to a DataFrame that contains annotations
        """
        filter_fn = first_not_none_param([filter_fn, self.filter_fn], no_filter)
        patient_annotations = {}
        for patient in self:
            annotations = self._load_single_patient_polyphen(
                patient,
                filter_fn=filter_fn)
            if annotations is not None:
                annotations["patient_id"] = patient.id
                patient_annotations[patient.id] = annotations
        if as_dataframe:
            return pd.concat(patient_annotations.values())
        return patient_annotations

Response: Load a dataframe containing polyphen2 annotations for all variants.


Instruction:
Given the following Python 3 function, write the documentation
def load_effects(self, patients=None, only_nonsynonymous=False,
                     all_effects=False, filter_fn=None, **kwargs):
        """Load a dictionary of patient_id to varcode.EffectCollection

        Note that this only loads one effect per variant.

        Parameters
        ----------
        patients : str, optional
            Filter to a subset of patients
        only_nonsynonymous : bool, optional
            If true, load only nonsynonymous effects, default False
        all_effects : bool, optional
            If true, return all effects rather than only the top-priority effect per variant
        filter_fn : function
            Takes a FilterableEffect and returns a boolean. Only effects returning True are preserved.
            Overrides default self.filter_fn. `None` passes through to self.filter_fn.

        Returns
        -------
        effects
             Dictionary of patient_id to varcode.EffectCollection
        """
        filter_fn = first_not_none_param([filter_fn, self.filter_fn], no_filter)
        filter_fn_name = self._get_function_name(filter_fn)
        logger.debug("loading effects with filter_fn {}".format(filter_fn_name))
        patient_effects = {}
        for patient in self.iter_patients(patients):
            effects = self._load_single_patient_effects(
                patient, only_nonsynonymous, all_effects, filter_fn, **kwargs)
            if effects is not None:
                patient_effects[patient.id] = effects
        return patient_effects

Response: Loads all effects for a set of patients.


Instruction:
Explain what the following Python 3 code does
def load_kallisto(self):
        kallisto_data = pd.concat(
            [self._load_single_patient_kallisto(patient) for patient in self],
            copy=False
        )

        if self.kallisto_ensembl_version is None:
            raise ValueError("Required a kallisto_ensembl_version but none was specified")

        ensembl_release = cached_release(self.kallisto_ensembl_version)

        kallisto_data["gene_name"] = \
            kallisto_data["target_id"].map(lambda t: ensembl_release.gene_name_of_transcript_id(t))

        # sum counts across genes
        kallisto_data = \
            kallisto_data.groupby(["patient_id", "gene_name"])[["est_counts"]].sum().reset_index()

        return kallisto_data

Response: Load Kallisto transcript quantification data for a cohort s patients.


Instruction:
Explain what the following Python 3 code does
def _load_single_patient_kallisto(self, patient):
        data = pd.read_csv(patient.tumor_sample.kallisto_path, sep="\t")
        data["patient_id"] = patient.id
        return data

Response: Load Kallisto gene quantification given a patient


Instruction:
Explain what the following Python 3 code does
def _load_single_patient_cufflinks(self, patient, filter_ok):
        data = pd.read_csv(patient.tumor_sample.cufflinks_path, sep="\t")
        data["patient_id"] = patient.id

        if filter_ok:
            # Filter to OK FPKM counts
            data = data[data["FPKM_status"] == "OK"]
        return data

Response: Load Cufflinks gene quantification given a patient


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_filtered_isovar_epitopes(self, epitopes, ic50_cutoff):
        mutant_binding_predictions = []
        for binding_prediction in epitopes:
            peptide = binding_prediction.peptide
            peptide_offset = binding_prediction.offset
            isovar_row = dict(binding_prediction.source_sequence_key)
            is_mutant = contains_mutant_residues(
                peptide_start_in_protein=peptide_offset,
                peptide_length=len(peptide),
                mutation_start_in_protein=isovar_row["variant_aa_interval_start"],
                mutation_end_in_protein=isovar_row["variant_aa_interval_end"])
            if is_mutant and binding_prediction.value <= ic50_cutoff:
                mutant_binding_predictions.append(binding_prediction)
        return EpitopeCollection(mutant_binding_predictions)

Response: This method returns a list of epitope collections that are only filtered by the Ic50 cutoff.


Instruction:
Write a Python 3 script for
plotting an ROC curve for benefit and a given variable

Response:
def plot_roc_curve(self, on, bootstrap_samples=100, ax=None, **kwargs):
        """Plot an ROC curve for benefit and a given variable

        Parameters
        ----------
        on : str or function or list or dict
            See `cohort.load.as_dataframe`
        bootstrap_samples : int, optional
            Number of boostrap samples to use to compute the AUC
        ax : Axes, default None
            Axes to plot on

        Returns
        -------
        (mean_auc_score, plot): (float, matplotlib plot)
            Returns the average AUC for the given predictor over `bootstrap_samples`
            and the associated ROC curve
        """
        plot_col, df = self.as_dataframe(on, return_cols=True, **kwargs)
        df = filter_not_null(df, "benefit")
        df = filter_not_null(df, plot_col)
        df.benefit = df.benefit.astype(bool)
        return roc_curve_plot(df, plot_col, "benefit", bootstrap_samples, ax=ax)


Instruction:
Given the following Python 3 function, write the documentation
def plot_benefit(self, on, benefit_col="benefit", label="Response", ax=None,
                     alternative="two-sided", boolean_value_map={},
                     order=None, **kwargs):
        """Plot a comparison of benefit/response in the cohort on a given variable
        """
        no_benefit_plot_name = "No %s" % self.benefit_plot_name
        boolean_value_map = boolean_value_map or {True: self.benefit_plot_name, False: no_benefit_plot_name}
        order = order or [no_benefit_plot_name, self.benefit_plot_name]

        return self.plot_boolean(on=on,
                                 boolean_col=benefit_col,
                                 alternative=alternative,
                                 boolean_label=label,
                                 boolean_value_map=boolean_value_map,
                                 order=order,
                                 ax=ax,
                                 **kwargs)

Response: Plot a comparison of benefit and response in the cohort on a given variable


Instruction:
How would you explain what the following Python 3 function does
def plot_boolean(self,
                     on,
                     boolean_col,
                     plot_col=None,
                     boolean_label=None,
                     boolean_value_map={},
                     order=None,
                     ax=None,
                     alternative="two-sided",
                     **kwargs):
        """Plot a comparison of `boolean_col` in the cohort on a given variable via
        `on` or `col`.

        If the variable (through `on` or `col`) is binary this will compare
        odds-ratios and perform a Fisher's exact test.

        If the variable is numeric, this will compare the distributions through
        a Mann-Whitney test and plot the distributions with box-strip plot

        Parameters
        ----------
        on : str or function or list or dict
            See `cohort.load.as_dataframe`
        plot_col : str, optional
            If on has many columns, this is the one whose values we are plotting.
            If on has a single column, this is unnecessary.
            We might want many columns if, e.g. we're generating boolean_col from a
            function as well.
        boolean_col : str
            Column name of boolean column to plot or compare against.
        boolean_label : None, optional
            Label to give boolean column in the plot
        boolean_value_map : dict, optional
            Map of conversions for values in the boolean column, i.e. {True: 'High', False: 'Low'}
        order : None, optional
            Order of the labels on the x-axis
        ax : None, optional
            Axes to plot on
        alternative : str, optional
            Choose the sidedness of the mannwhitneyu or Fisher's Exact test.

        Returns
        -------
        (Test statistic, p-value): (float, float)

        """
        cols, df = self.as_dataframe(on, return_cols=True, **kwargs)
        plot_col = self.plot_col_from_cols(cols=cols, plot_col=plot_col)
        df = filter_not_null(df, boolean_col)
        df = filter_not_null(df, plot_col)

        if boolean_label:
            df[boolean_label] = df[boolean_col]
            boolean_col = boolean_label

        condition_value = None
        if boolean_value_map:
            assert set(boolean_value_map.keys()) == set([True, False]), \
                "Improper mapping of boolean column provided"
            df[boolean_col] = df[boolean_col].map(lambda v: boolean_value_map[v])
            condition_value = boolean_value_map[True]

        if df[plot_col].dtype == "bool":
            results = fishers_exact_plot(
                data=df,
                condition1=boolean_col,
                condition2=plot_col,
                condition1_value=condition_value,
                alternative=alternative,
                order=order,
                ax=ax)
        else:
            results = mann_whitney_plot(
                data=df,
                condition=boolean_col,
                distribution=plot_col,
                condition_value=condition_value,
                alternative=alternative,
                order=order,
                ax=ax)
        return results

Response: Plot a comparison of boolean_col in the cohort on a given variable via the Mann - Whitney test and perform a Fisher s Exact test.


Instruction:
Implement a Python 3 function for
plotting a Kaplan Meier survival curve by splitting the cohort into two groups.

Response:
def plot_survival(self,
                      on,
                      how="os",
                      survival_units="Days",
                      strata=None,
                      ax=None,
                      ci_show=False,
                      with_condition_color="#B38600",
                      no_condition_color="#A941AC",
                      with_condition_label=None,
                      no_condition_label=None,
                      color_map=None,
                      label_map=None,
                      color_palette="Set2",
                      threshold=None, **kwargs):
        """Plot a Kaplan Meier survival curve by splitting the cohort into two groups
        Parameters
        ----------
        on : str or function or list or dict
            See `cohort.load.as_dataframe`
        how : {"os", "pfs"}, optional
            Whether to plot OS (overall survival) or PFS (progression free survival)
        survival_units : str
            Unit of time for the survival measure, i.e. Days or Months
        strata : str
            (optional) column name of stratifying variable
        ci_show : bool
            Display the confidence interval around the survival curve
        threshold : int, "median", "median-per-strata" or None (optional)
            Threshold of `col` on which to split the cohort
        """
        assert how in ["os", "pfs"], "Invalid choice of survival plot type %s" % how
        cols, df = self.as_dataframe(on, return_cols=True, **kwargs)
        plot_col = self.plot_col_from_cols(cols=cols, only_allow_one=True)
        df = filter_not_null(df, plot_col)
        results = plot_kmf(
            df=df,
            condition_col=plot_col,
            xlabel=survival_units,
            ylabel="Overall Survival (%)" if how == "os" else "Progression-Free Survival (%)",
            censor_col="deceased" if how == "os" else "progressed_or_deceased",
            survival_col=how,
            strata_col=strata,
            threshold=threshold,
            ax=ax,
            ci_show=ci_show,
            with_condition_color=with_condition_color,
            no_condition_color=no_condition_color,
            with_condition_label=with_condition_label,
            no_condition_label=no_condition_label,
            color_palette=color_palette,
            label_map=label_map,
            color_map=color_map,
        )
        return results


Instruction:
Given the following Python 3 function, write the documentation
def plot_correlation(self, on, x_col=None, plot_type="jointplot", stat_func=pearsonr, show_stat_func=True, plot_kwargs={}, **kwargs):
        if plot_type not in ["boxplot", "barplot", "jointplot", "regplot"]:
            raise ValueError("Invalid plot_type %s" % plot_type)
        plot_cols, df = self.as_dataframe(on, return_cols=True, **kwargs)
        if len(plot_cols) != 2:
            raise ValueError("Must be comparing two columns, but there are %d columns" % len(plot_cols))
        for plot_col in plot_cols:
            df = filter_not_null(df, plot_col)
        if x_col is None:
            x_col = plot_cols[0]
            y_col = plot_cols[1]
        else:
            if x_col == plot_cols[0]:
                y_col = plot_cols[1]
            else:
                y_col = plot_cols[0]
        series_x = df[x_col]
        series_y = df[y_col]
        coeff, p_value = stat_func(series_x, series_y)
        if plot_type == "jointplot":
            plot = sb.jointplot(data=df, x=x_col, y=y_col,
                                stat_func=stat_func if show_stat_func else None,
                                **plot_kwargs)
        elif plot_type == "regplot":
            plot = sb.regplot(data=df, x=x_col, y=y_col,
                              **plot_kwargs)
        elif plot_type == "boxplot":
            plot = stripboxplot(data=df, x=x_col, y=y_col, **plot_kwargs)
        else:
            plot = sb.barplot(data=df, x=x_col, y=y_col, **plot_kwargs)
        return CorrelationResults(coeff=coeff, p_value=p_value, stat_func=stat_func,
                                  series_x=series_x, series_y=series_y, plot=plot)

Response: Plot the correlation between two variables.


Instruction:
Given the following Python 3 function, write the documentation
def _list_patient_ids(self):
        results = []
        for patient in self:
            results.append(patient.id)
        return(results)

Response: Utility function to return a list of patient ids in the Cohort


Instruction:
Can you generate a brief explanation for the following Python 3 code
def summarize_provenance_per_cache(self):
        provenance_summary = {}
        df = self.as_dataframe()
        for cache in self.cache_names:
            cache_name = self.cache_names[cache]
            cache_provenance = None
            num_discrepant = 0
            this_cache_dir = path.join(self.cache_dir, cache_name)
            if path.exists(this_cache_dir):
                for patient_id in self._list_patient_ids():
                    patient_cache_dir = path.join(this_cache_dir, patient_id)
                    try:
                        this_provenance = self.load_provenance(patient_cache_dir = patient_cache_dir)
                    except:
                        this_provenance = None
                    if this_provenance:
                        if not(cache_provenance):
                            cache_provenance = this_provenance
                        else:
                            num_discrepant += compare_provenance(this_provenance, cache_provenance)
                if num_discrepant == 0:
                    provenance_summary[cache_name] = cache_provenance
                else:
                    provenance_summary[cache_name] = None
        return(provenance_summary)

Response: Utility function to summarize provenance files for all items in a cache.


Instruction:
Explain what the following Python 3 code does
def summarize_dataframe(self):
        if self.dataframe_hash:
            return(self.dataframe_hash)
        else:
            df = self._as_dataframe_unmodified()
            return(self.dataframe_hash)

Response: Summarize the default dataframe for this cohort using a hash function.


Instruction:
How would you explain what the following Python 3 function does
def summarize_data_sources(self):
        provenance_file_summary = self.summarize_provenance()
        dataframe_hash = self.summarize_dataframe()
        results = {
            "provenance_file_summary": provenance_file_summary,
            "dataframe_hash": dataframe_hash
        }
        return(results)

Response: Utility function to summarize data sources for this Cohort


Instruction:
Here you have a function in Python 3, explain what it does
def strelka_somatic_variant_stats(variant, variant_metadata):

    sample_info = variant_metadata["sample_info"]
    # Ensure there are exactly two samples in the VCF, a tumor and normal
    assert len(sample_info) == 2, "More than two samples found in the somatic VCF"
    tumor_stats = _strelka_variant_stats(variant, sample_info["TUMOR"])
    normal_stats = _strelka_variant_stats(variant, sample_info["NORMAL"])
    return SomaticVariantStats(tumor_stats=tumor_stats, normal_stats=normal_stats)

Response: Parse out the variant calling statistics for a given variant from a Strelka VCF and return a SomaticVariantStats object


Instruction:
Given the following Python 3 function, write the documentation
def _strelka_variant_stats(variant, sample_info):

    if variant.is_deletion or variant.is_insertion:
        # ref: https://sites.google.com/site/strelkasomaticvariantcaller/home/somatic-variant-output
        ref_depth = int(sample_info['TAR'][0]) # number of reads supporting ref allele (non-deletion)
        alt_depth = int(sample_info['TIR'][0]) # number of reads supporting alt allele (deletion)
        depth = ref_depth + alt_depth
    else:
        # Retrieve the Tier 1 counts from Strelka
        ref_depth = int(sample_info[variant.ref+"U"][0])
        alt_depth = int(sample_info[variant.alt+"U"][0])
        depth = alt_depth + ref_depth
    if depth > 0:
        vaf = float(alt_depth) / depth
    else:
        # unclear how to define vaf if no reads support variant
        # up to user to interpret this (hopefully filtered out in QC settings)
        vaf = None

    return VariantStats(depth=depth, alt_depth=alt_depth, variant_allele_frequency=vaf)

Response: Parse a single sample s variant calling statistics based on Strelka VCF output


Instruction:
Here you have a function in Python 3, explain what it does
def mutect_somatic_variant_stats(variant, variant_metadata):

    sample_info = variant_metadata["sample_info"]
    # Ensure there are exactly two samples in the VCF, a tumor and normal
    assert len(sample_info) == 2, "More than two samples found in the somatic VCF"

    # Find the sample with the genotype field set to variant in the VCF
    tumor_sample_infos = [info for info in sample_info.values() if info["GT"] == "0/1"]

    # Ensure there is only one such sample
    assert len(tumor_sample_infos) == 1, "More than one tumor sample found in the VCF file"

    tumor_sample_info = tumor_sample_infos[0]
    normal_sample_info = [info for info in sample_info.values() if info["GT"] != "0/1"][0]

    tumor_stats = _mutect_variant_stats(variant, tumor_sample_info)
    normal_stats = _mutect_variant_stats(variant, normal_sample_info)
    return SomaticVariantStats(tumor_stats=tumor_stats, normal_stats=normal_stats)

Response: Parse out the variant calling statistics for a given variant in a Mutect VCF and return a SomaticVariantStats object


Instruction:
Implement a function in Python 3 to
parse a single sample s variant calling statistics based on Mutect s V1 VCF output

Response:
def _mutect_variant_stats(variant, sample_info):
    """Parse a single sample"s variant calling statistics based on Mutect"s (v1) VCF output

    Parameters
    ----------
    variant : varcode.Variant
    sample_info : dict
        Dictionary of Mutect-specific variant calling fields

    Returns
    -------
    VariantStats
    """

    # Parse out the AD (or allele depth field), which is an array of [REF_DEPTH, ALT_DEPTH]
    ref_depth, alt_depth = sample_info["AD"]
    depth = int(ref_depth) + int(alt_depth)
    vaf = float(alt_depth) / depth

    return VariantStats(depth=depth, alt_depth=alt_depth, variant_allele_frequency=vaf)


Instruction:
Write a Python 3 script for
parsing out the variant calling statistics for a given variant from a MAF file.

Response:
def maf_somatic_variant_stats(variant, variant_metadata):
    """
    Parse out the variant calling statistics for a given variant from a MAF file

    Assumes the MAF format described here: https://www.biostars.org/p/161298/#161777

    Parameters
    ----------
    variant : varcode.Variant
    variant_metadata : dict
        Dictionary of metadata for this variant

    Returns
    -------
    SomaticVariantStats
    """
    tumor_stats = None
    normal_stats = None
    if "t_ref_count" in variant_metadata:
        tumor_stats = _maf_variant_stats(variant, variant_metadata, prefix="t")
    if "n_ref_count" in variant_metadata:
        normal_stats = _maf_variant_stats(variant, variant_metadata, prefix="n")
    return SomaticVariantStats(tumor_stats=tumor_stats, normal_stats=normal_stats)


Instruction:
Can you create a Python 3 function that
returns True if variant_file given is in strelka format

Response:
def _vcf_is_strelka(variant_file, variant_metadata):
    """Return True if variant_file given is in strelka format
    """
    if "strelka" in variant_file.lower():
        return True
    elif "NORMAL" in variant_metadata["sample_info"].keys():
        return True
    else:
        vcf_reader = vcf.Reader(open(variant_file, "r"))
        try:
            vcf_type = vcf_reader.metadata["content"]
        except KeyError:
            vcf_type = ""
        if "strelka" in vcf_type.lower():
            return True
    return False


Instruction:
Write a Python 3 function that can
parse the variant calling stats from a single variant calling file.

Response:
def variant_stats_from_variant(variant,
                               metadata,
                               merge_fn=(lambda all_stats: \
                                max(all_stats, key=(lambda stats: stats.tumor_stats.depth)))):
    """Parse the variant calling stats from a variant called from multiple variant files. The stats are merged
    based on `merge_fn`

    Parameters
    ----------
    variant : varcode.Variant
    metadata : dict
        Dictionary of variant file to variant calling metadata from that file
    merge_fn : function
        Function from list of SomaticVariantStats to single SomaticVariantStats.
        This is used if a variant is called by multiple callers or appears in multiple VCFs.
        By default, this uses the data from the caller that had a higher tumor depth.

    Returns
    -------
    SomaticVariantStats
    """
    all_stats = []
    for (variant_file, variant_metadata) in metadata.items():
        if _vcf_is_maf(variant_file=variant_file):
            stats = maf_somatic_variant_stats(variant, variant_metadata)
        elif _vcf_is_strelka(variant_file=variant_file,
                             variant_metadata=variant_metadata):
            stats = strelka_somatic_variant_stats(variant, variant_metadata)
        elif _vcf_is_mutect(variant_file=variant_file,
                            variant_metadata=variant_metadata):
            stats = mutect_somatic_variant_stats(variant, variant_metadata)
        else:
            raise ValueError("Cannot parse sample fields, variant file {} is from an unsupported caller.".format(variant_file))
        all_stats.append(stats)
    return merge_fn(all_stats)


Instruction:
Create a Python 3 function for
getting a new item from the queue and execute it.

Response:
def _get_and_execute(self):
        """
        :return: True if it should continue running, False if it should end its execution.
        """
        try:
            work = self.queue.get(timeout=self.max_seconds_idle)
        except queue.Empty:
            # max_seconds_idle has been exhausted, exiting
            self.end_notify()
            return False
        else:
            self._work(work)
            self.queue.task_done()
            return True


Instruction:
How would you explain what the following Python 3 function does
def format(self, full_info: bool = False):
        chat = self.api_object
        if full_info:
            self.__format_full(chat)
        else:
            self.__format_simple(chat)

Response:
:param full_info: If True, adds more info about the chat. Please, note that this additional info requires
            to make up to THREE synchronous api calls.


Instruction:
Write a Python 3 function that can
return a list of all the settings that are set in the current session

Response:
def list(self):
        """
        :rtype: list(setting_name, value, default_value, is_set, is_supported)
        """
        settings = []
        for setting in _SETTINGS:
            value = self.get(setting)
            is_set = self.is_set(setting)
            default_value = self.get_default_value(setting)
            is_supported = True
            settings.append((setting, value, default_value, is_set, is_supported))
        for setting in sorted(self.settings_state.list_keys()):
            if not self.is_supported(setting):
                value = self.get(setting)
                default_value = None
                is_set = True
                is_supported = False
                settings.append((setting, value, default_value, is_set, is_supported))
        return settings


Instruction:
Make a summary of the following Python 3 code
def load_ensembl_coverage(cohort, coverage_path, min_tumor_depth, min_normal_depth=0,
                          pageant_dir_fn=None):
    """
    Load in Pageant CoverageDepth results with Ensembl loci.

    coverage_path is a path to Pageant CoverageDepth output directory, with
    one subdirectory per patient and a `cdf.csv` file inside each patient subdir.

    If min_normal_depth is 0, calculate tumor coverage. Otherwise, calculate
    join tumor/normal coverage.

    pageant_dir_fn is a function that takes in a Patient and produces a Pageant
    dir name.

    Last tested with Pageant CoverageDepth version 1ca9ed2.
    """
    # Function to grab the pageant file name using the Patient
    if pageant_dir_fn is None:
        pageant_dir_fn = lambda patient: patient.id

    columns_both = [
        "depth1", # Normal
        "depth2", # Tumor
        "onBP1",
        "onBP2",
        "numOnLoci",
        "fracBPOn1",
        "fracBPOn2",
        "fracLociOn",
        "offBP1",
        "offBP2",
        "numOffLoci",
        "fracBPOff1",
        "fracBPOff2",
        "fracLociOff",
    ]
    columns_single = [
        "depth",
        "onBP",
        "numOnLoci",
        "fracBPOn",
        "fracLociOn",
        "offBP",
        "numOffLoci",
        "fracBPOff",
        "fracLociOff"
    ]
    if min_normal_depth < 0:
        raise ValueError("min_normal_depth must be >= 0")
    use_tumor_only = (min_normal_depth == 0)
    columns = columns_single if use_tumor_only else columns_both
    ensembl_loci_dfs = []
    for patient in cohort:
        patient_ensembl_loci_df = pd.read_csv(
            path.join(coverage_path, pageant_dir_fn(patient), "cdf.csv"),
            names=columns,
            header=1)
        # pylint: disable=no-member
        # pylint gets confused by read_csv
        if use_tumor_only:
            depth_mask = (patient_ensembl_loci_df.depth == min_tumor_depth)
        else:
            depth_mask = (
                (patient_ensembl_loci_df.depth1 == min_normal_depth) &
                (patient_ensembl_loci_df.depth2 == min_tumor_depth))
        patient_ensembl_loci_df = patient_ensembl_loci_df[depth_mask]
        assert len(patient_ensembl_loci_df) == 1, (
            "Incorrect number of tumor={}, normal={} depth loci results: {} for patient {}".format(
                min_tumor_depth, min_normal_depth, len(patient_ensembl_loci_df), patient))
        patient_ensembl_loci_df["patient_id"] = patient.id
        ensembl_loci_dfs.append(patient_ensembl_loci_df)
    ensembl_loci_df = pd.concat(ensembl_loci_dfs)
    ensembl_loci_df["MB"] = ensembl_loci_df.numOnLoci / 1000000.0
    return ensembl_loci_df[["patient_id", "numOnLoci", "MB"]]

Response: Load the ensembl loci coverage data from a path to a pageant coverage file.


Instruction:
How would you implement a function in Python 3 that
returns a fraction of the vertical size of the plot.

Response:
def vertical_percent(plot, percent=0.1):
    """
    Using the size of the y axis, return a fraction of that size.
    """
    plot_bottom, plot_top = plot.get_ylim()
    return percent * (plot_top - plot_bottom)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def hide_ticks(plot, min_tick_value=None, max_tick_value=None):
    for tick, tick_value in zip(plot.get_yticklabels(), plot.get_yticks()):
        tick_label = as_numeric(tick_value)
        if tick_label:
            if (min_tick_value is not None and tick_label < min_tick_value or
                 max_tick_value is not None and tick_label > max_tick_value):
                tick.set_visible(False)

Response: Hide all the ticks that are outside of the given limits.


Instruction:
Here you have a function in Python 3, explain what it does
def add_significance_indicator(plot, col_a=0, col_b=1, significant=False):
    plot_bottom, plot_top = plot.get_ylim()
    # Give the plot a little room for the significance indicator
    line_height = vertical_percent(plot, 0.1)
    # Add some extra spacing below the indicator
    plot_top = plot_top + line_height
    # Add some extra spacing above the indicator
    plot.set_ylim(top=plot_top + line_height * 2)
    color = "black"
    line_top = plot_top + line_height
    plot.plot([col_a, col_a, col_b, col_b], [plot_top, line_top, line_top, plot_top], lw=1.5, color=color)
    indicator = "*" if significant else "ns"
    plot.text((col_a + col_b) * 0.5, line_top, indicator, ha="center", va="bottom", color=color)

Response: Add a p - value significance indicator.


Instruction:
Here you have a function in Python 3, explain what it does
def stripboxplot(x, y, data, ax=None, significant=None, **kwargs):
    ax = sb.boxplot(
        x=x,
        y=y,
        data=data,
        ax=ax,
        fliersize=0,
        **kwargs
    )

    plot = sb.stripplot(
        x=x,
        y=y,
        data=data,
        ax=ax,
        jitter=kwargs.pop("jitter", 0.05),
        color=kwargs.pop("color", "0.3"),
        **kwargs
    )

    if data[y].min() >= 0:
        hide_negative_y_ticks(plot)
    if significant is not None:
        add_significance_indicator(plot=plot, significant=significant)

    return plot

Response: Overlay a stripplot on top of a boxplot.


Instruction:
Implement a function in Python 3 to
perform a Fisher s Exact test on two binary columns.

Response:
def fishers_exact_plot(data, condition1, condition2, ax=None,
                       condition1_value=None,
                       alternative="two-sided", **kwargs):
    """
    Perform a Fisher's exact test to compare to binary columns

    Parameters
    ----------
    data: Pandas dataframe
        Dataframe to retrieve information from

    condition1: str
        First binary column to compare (and used for test sidedness)

    condition2: str
        Second binary column to compare

    ax : Axes, default None
        Axes to plot on

    condition1_value:
        If `condition1` is not a binary column, split on =/!= to condition1_value

    alternative:
        Specify the sidedness of the test: "two-sided", "less"
        or "greater"
    """
    plot = sb.barplot(
        x=condition1,
        y=condition2,
        ax=ax,
        data=data,
        **kwargs
    )

    plot.set_ylabel("Percent %s" % condition2)
    condition1_mask = get_condition_mask(data, condition1, condition1_value)
    count_table = pd.crosstab(data[condition1], data[condition2])
    print(count_table)
    oddsratio, p_value = fisher_exact(count_table, alternative=alternative)
    add_significance_indicator(plot=plot, significant=p_value <= 0.05)
    only_percentage_ticks(plot)

    if alternative != "two-sided":
        raise ValueError("We need to better understand the one-sided Fisher's Exact test")
    sided_str = "two-sided"
    print("Fisher's Exact Test: OR: {}, p-value={} ({})".format(oddsratio, p_value, sided_str))
    return FishersExactResults(oddsratio=oddsratio,
                               p_value=p_value,
                               sided_str=sided_str,
                               with_condition1_series=data[condition1_mask][condition2],
                               without_condition1_series=data[~condition1_mask][condition2],
                               plot=plot)


Instruction:
Implement a function in Python 3 to
create a box plot comparing a binary column and perform Mann - Whitney test.

Response:
def mann_whitney_plot(data,
                      condition,
                      distribution,
                      ax=None,
                      condition_value=None,
                      alternative="two-sided",
                      skip_plot=False,
                      **kwargs):
    """
    Create a box plot comparing a condition and perform a
    Mann Whitney test to compare the distribution in condition A v B

    Parameters
    ----------
    data: Pandas dataframe
        Dataframe to retrieve information from

    condition: str
        Column to use as the splitting criteria

    distribution: str
        Column to use as the Y-axis or distribution in the test

    ax : Axes, default None
        Axes to plot on

    condition_value:
        If `condition` is not a binary column, split on =/!= to condition_value

    alternative:
        Specify the sidedness of the Mann-Whitney test: "two-sided", "less"
        or "greater"

    skip_plot:
        Calculate the test statistic and p-value, but don't plot.
    """
    condition_mask = get_condition_mask(data, condition, condition_value)
    U, p_value = mannwhitneyu(
        data[condition_mask][distribution],
        data[~condition_mask][distribution],
        alternative=alternative
    )

    plot = None
    if not skip_plot:
        plot = stripboxplot(
            x=condition,
            y=distribution,
            data=data,
            ax=ax,
            significant=p_value <= 0.05,
            **kwargs
        )

    sided_str = sided_str_from_alternative(alternative, condition)
    print("Mann-Whitney test: U={}, p-value={} ({})".format(U, p_value, sided_str))
    return MannWhitneyResults(U=U,
                              p_value=p_value,
                              sided_str=sided_str,
                              with_condition_series=data[condition_mask][distribution],
                              without_condition_series=data[~condition_mask][distribution],
                              plot=plot)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def roc_curve_plot(data, value_column, outcome_column, bootstrap_samples=100, ax=None):
    scores = bootstrap_auc(df=data,
                           col=value_column,
                           pred_col=outcome_column,
                           n_bootstrap=bootstrap_samples)
    mean_bootstrap_auc = scores.mean()
    print("{}, Bootstrap (samples = {}) AUC:{}, std={}".format(
        value_column, bootstrap_samples, mean_bootstrap_auc, scores.std()))

    outcome = data[outcome_column].astype(int)
    values = data[value_column]
    fpr, tpr, thresholds = roc_curve(outcome, values)

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

    roc_plot = ax.plot(fpr, tpr, lw=1, label=value_column)

    ax.set_xlim([-0.05, 1.05])
    ax.set_ylim([-0.05, 1.05])
    ax.set_xlabel('False Positive Rate')
    ax.set_ylabel('True Positive Rate')
    ax.legend(loc=2, borderaxespad=0.)
    ax.set_title('{} ROC Curve (n={})'.format(value_column, len(values)))

    return (mean_bootstrap_auc, roc_plot)

Response: Create a ROC curve and compute the bootstrap AUC for the given variable and outcome


Instruction:
Can you tell what is the following Python 3 function doing
def get_cache_dir(cache_dir, cache_root_dir=None, *args, **kwargs):
    cache_dir = cache_dir.format(*args, **kwargs)
    if path.isabs(cache_dir):
        if cache_root_dir is not None:
            logger.warning('cache_dir ({}) is a full path; ignoring cache_root_dir'.format(cache_dir))
        return cache_dir
    if cache_root_dir is not None:
        return path.join(cache_root_dir, cache_dir)
    else:
        logger.warning("cache dir is not full path & cache_root_dir not given. Caching may not work as expected!")
    return None

Response: Return full cache_dir according to given args and kwargs


Instruction:
Implement a function in Python 3 to
give an object and a dictionary give the object new attributes from that dictionary.

Response:
def set_attributes(obj, additional_data):
    """
    Given an object and a dictionary, give the object new attributes from that dictionary.

    Uses _strip_column_name to git rid of whitespace/uppercase/special characters.
    """
    for key, value in additional_data.items():
        if hasattr(obj, key):
            raise ValueError("Key %s in additional_data already exists in this object" % key)
        setattr(obj, _strip_column_name(key), value)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def return_obj(cols, df, return_cols=False):
        df_holder = DataFrameHolder(cols=cols, df=df)
        return df_holder.return_self(return_cols=return_cols)

Response: Construct a DataFrameHolder and then return either that or the DataFrame.


Instruction:
Here you have a function in Python 3, explain what it does
def _plot_kmf_single(df,
                     condition_col,
                     survival_col,
                     censor_col,
                     threshold,
                     title,
                     xlabel,
                     ylabel,
                     ax,
                     with_condition_color,
                     no_condition_color,
                     with_condition_label,
                     no_condition_label,
                     color_map,
                     label_map,
                     color_palette,
                     ci_show,
                     print_as_title):
    """
    Helper function to produce a single KM survival plot, among observations in df by groups defined by condition_col.

    All inputs are required - this function is intended to be called by `plot_kmf`.
    """
    # make color inputs consistent hex format
    if colors.is_color_like(with_condition_color):
        with_condition_color = colors.to_hex(with_condition_color)
    if colors.is_color_like(no_condition_color):
        no_condition_color = colors.to_hex(no_condition_color)
    ## prepare data to be plotted; producing 3 outputs:
    # - `condition`, series containing category labels to be plotted
    # - `label_map` (mapping condition values to plot labels)
    # - `color_map` (mapping condition values to plotted colors)
    if threshold is not None:
        is_median = threshold == "median"
        if is_median:
            threshold = df[condition_col].median()
        label_suffix = float_str(threshold)
        condition = df[condition_col] > threshold
        default_label_no_condition = "%s ≤ %s" % (condition_col, label_suffix)
        if is_median:
            label_suffix += " (median)"
        default_label_with_condition = "%s > %s" % (condition_col, label_suffix)
        with_condition_label = with_condition_label or default_label_with_condition
        no_condition_label = no_condition_label or default_label_no_condition
        if not label_map:
            label_map = {False: no_condition_label,
                         True: with_condition_label}
        if not color_map:
            color_map = {False: no_condition_color,
                         True: with_condition_color}
    elif df[condition_col].dtype == 'O' or df[condition_col].dtype.name == "category":
        condition = df[condition_col].astype("category")
        if not label_map:
            label_map = dict()
            [label_map.update({condition_value: '{} = {}'.format(condition_col,
                                                        condition_value)})
                     for condition_value in condition.unique()]
        if not color_map:
            rgb_values = sb.color_palette(color_palette, len(label_map.keys()))
            hex_values = [colors.to_hex(col) for col in rgb_values]
            color_map = dict(zip(label_map.keys(), hex_values))
    elif df[condition_col].dtype == 'bool':
        condition = df[condition_col]
        default_label_with_condition = "= {}".format(condition_col)
        default_label_no_condition = "¬ {}".format(condition_col)
        with_condition_label = with_condition_label or default_label_with_condition
        no_condition_label = no_condition_label or default_label_no_condition
        if not label_map:
            label_map = {False: no_condition_label,
                         True: with_condition_label}
        if not color_map:
            color_map = {False: no_condition_color,
                         True: with_condition_color}
    else:
        raise ValueError('Don\'t know how to plot data of type\
                         {}'.format(df[condition_col].dtype))

    # produce kmf plot for each category (group) identified above
    kmf = KaplanMeierFitter()
    grp_desc = list()
    grp_survival_data = dict()
    grp_event_data = dict()
    grp_names = list(condition.unique())
    for grp_name, grp_df in df.groupby(condition):
        grp_survival = grp_df[survival_col]
        grp_event = (grp_df[censor_col].astype(bool))
        grp_label = label_map[grp_name]
        grp_color = color_map[grp_name]
        kmf.fit(grp_survival, grp_event, label=grp_label)
        desc_str = "# {}: {}".format(grp_label, len(grp_survival))
        grp_desc.append(desc_str)
        grp_survival_data[grp_name] = grp_survival
        grp_event_data[grp_name] = grp_event
        if ax:
            ax = kmf.plot(ax=ax, show_censors=True, ci_show=ci_show, color=grp_color)
        else:
            ax = kmf.plot(show_censors=True, ci_show=ci_show, color=grp_color)

    ## format the plot
    # Set the y-axis to range 0 to 1
    ax.set_ylim(0, 1)
    y_tick_vals = ax.get_yticks()
    ax.set_yticklabels(["%d" % int(y_tick_val * 100) for y_tick_val in y_tick_vals])
    # plot title
    if title:
        ax.set_title(title)
    elif print_as_title:
        ax.set_title(' | '.join(grp_desc))
    else:
        [print(desc) for desc in grp_desc]
    # axis labels
    if xlabel:
        ax.set_xlabel(xlabel)
    if ylabel:
        ax.set_ylabel(ylabel)

    ## summarize analytical version of results
    ## again using same groups as are plotted
    if len(grp_names) == 2:
        # use log-rank test for 2 groups
        results = logrank_test(grp_survival_data[grp_names[0]],
                               grp_survival_data[grp_names[1]],
                               event_observed_A=grp_event_data[grp_names[0]],
                               event_observed_B=grp_event_data[grp_names[1]])
    elif len(grp_names) == 1:
        # no analytical result for 1 or 0 groups
        results = NullSurvivalResults()
    else:
        # cox PH fitter for >2 groups
        cf = CoxPHFitter()
        cox_df = patsy.dmatrix('+'.join([condition_col, survival_col,
                                         censor_col]),
                               df, return_type='dataframe')
        del cox_df['Intercept']
        results = cf.fit(cox_df, survival_col, event_col=censor_col)
        results.print_summary()
    # add metadata to results object so caller can print them
    results.survival_data_series = grp_survival_data
    results.event_data_series = grp_event_data
    results.desc = grp_desc
    return results

Response: Helper function to produce a single KM survival plot.