Instruction:
Make a summary of the following Python 3 code
def revdocs2reverts(rev_docs, radius=defaults.RADIUS, use_sha1=False,
                    resort=False, verbose=False):
    """
    Converts a sequence of page-partitioned revision documents into a sequence
    of reverts.

    :Params:
        rev_docs : `iterable` ( `dict` )
            a page-partitioned sequence of revision documents
        radius : `int`
            The maximum number of revisions that a revert can reference.
        use_sha1 : `bool`
            Use the sha1 field as the checksum for comparison.
        resort : `bool`
            If True, re-sort the revisions of each page.
        verbose : `bool`
            Print dots and stuff
    """

    page_rev_docs = groupby(rev_docs, lambda rd: rd.get('page'))

    for page_doc, rev_docs in page_rev_docs:
        if verbose:
            sys.stderr.write(page_doc.get('title') + ": ")
            sys.stderr.flush()

        if resort:
            if verbose:
                sys.stderr.write("(sorting) ")
                sys.stderr.flush()
            rev_docs = sorted(
                rev_docs, key=lambda r: (r.get('timestamp'), r.get('id')))

        detector = Detector(radius=radius)
        for rev_doc in rev_docs:
            if not use_sha1 and 'text' not in rev_doc:
                logger.warn("Skipping {0}: 'text' field not found in {0}"
                            .format(rev_doc['id'], rev_doc))
                continue

            if use_sha1:
                checksum = rev_doc.get('sha1') or DummyChecksum()
            elif 'text' in rev_doc:
                text_bytes = bytes(rev_doc['text'], 'utf8', 'replace')
                checksum = hashlib.sha1(text_bytes).digest()

            revert = detector.process(checksum, rev_doc)

            if revert:
                yield revert.to_json()
                if verbose:
                    sys.stderr.write("r")
                    sys.stderr.flush()
            else:
                if verbose:
                    sys.stderr.write(".")
                    sys.stderr.flush()

        if verbose:
            sys.stderr.write("\n")
            sys.stderr.flush()

Response: Takes a sequence of page - partitioned revision documents and returns a sequence of reverts.


Instruction:
Here you have a function in Python 3, explain what it does
def spm_hrf_compat(t,
                   peak_delay=6,
                   under_delay=16,
                   peak_disp=1,
                   under_disp=1,
                   p_u_ratio=6,
                   normalize=True,
                   ):
    """ SPM HRF function from sum of two gamma PDFs

    This function is designed to be partially compatible with SPMs `spm_hrf.m`
    function.

    The SPN HRF is a *peak* gamma PDF (with location `peak_delay` and
    dispersion `peak_disp`), minus an *undershoot* gamma PDF (with location
    `under_delay` and dispersion `under_disp`, and divided by the `p_u_ratio`).

    Parameters
    ----------
    t : array-like
        vector of times at which to sample HRF
    peak_delay : float, optional
        delay of peak
    peak_disp : float, optional
        width (dispersion) of peak
    under_delay : float, optional
        delay of undershoot
    under_disp : float, optional
        width (dispersion) of undershoot
    p_u_ratio : float, optional
        peak to undershoot ratio.  Undershoot divided by this value before
        subtracting from peak.
    normalize : {True, False}, optional
        If True, divide HRF values by their sum before returning. SPM does this
        by default.

    Returns
    -------
    hrf : array
        vector length ``len(t)`` of samples from HRF at times `t`

    Notes
    -----
    See ``spm_hrf.m`` in the SPM distribution.
    """
    if len([v for v in [peak_delay, peak_disp, under_delay, under_disp]
            if v <= 0]):
        raise ValueError("delays and dispersions must be > 0")
    # gamma.pdf only defined for t > 0
    hrf = np.zeros(t.shape, dtype=np.float)
    pos_t = t[t > 0]
    peak = sps.gamma.pdf(pos_t,
                         peak_delay / peak_disp,
                         loc=0,
                         scale=peak_disp)
    undershoot = sps.gamma.pdf(pos_t,
                               under_delay / under_disp,
                               loc=0,
                               scale=under_disp)
    hrf[t > 0] = peak - undershoot / p_u_ratio
    if not normalize:
        return hrf
    return hrf / np.max(hrf)

Response: This function is designed to be partially compatible with SPMs SPM HRF function from sum of two gamma PDFs at times t.


Instruction:
Given the following Python 3 function, write the documentation
def dspmt(t):
    t = np.asarray(t)
    return spmt(t) - spmt(t - 1)

Response: SPM canonical HRF derivative values for time values t


Instruction:
Here you have a function in Python 3, explain what it does
def cnvlTc(idxPrc,
           aryPrfTcChunk,
           lstHrf,
           varTr,
           varNumVol,
           queOut,
           varOvsmpl=10,
           varHrfLen=32,
           ):
    """
    Convolution of time courses with HRF model.
    """

    # *** prepare hrf time courses for convolution
    print("---------Process " + str(idxPrc) +
          ": Prepare hrf time courses for convolution")
    # get frame times, i.e. start point of every volume in seconds
    vecFrms = np.arange(0, varTr * varNumVol, varTr)
    # get supersampled frames times, i.e. start point of every volume in
    # seconds, since convolution takes place in temp. upsampled space
    vecFrmTms = np.arange(0, varTr * varNumVol, varTr / varOvsmpl)
    # get resolution of supersampled frame times
    varRes = varTr / float(varOvsmpl)

    # prepare empty list that will contain the arrays with hrf time courses
    lstBse = []
    for hrfFn in lstHrf:
        # needs to be a multiple of oversample
        vecTmpBse = hrfFn(np.linspace(0, varHrfLen,
                                      (varHrfLen // varTr) * varOvsmpl))
        lstBse.append(vecTmpBse)

    # *** prepare pixel time courses for convolution
    print("---------Process " + str(idxPrc) +
          ": Prepare pixel time courses for convolution")

    # adjust the input, if necessary, such that input is 2D, with last dim time
    tplInpShp = aryPrfTcChunk.shape
    aryPrfTcChunk = aryPrfTcChunk.reshape((-1, aryPrfTcChunk.shape[-1]))

    # Prepare an empty array for ouput
    aryConv = np.zeros((aryPrfTcChunk.shape[0], len(lstHrf),
                        aryPrfTcChunk.shape[1]))
    print("---------Process " + str(idxPrc) +
          ": Convolve")
    # Each time course is convolved with the HRF separately, because the
    # numpy convolution function can only be used on one-dimensional data.
    # Thus, we have to loop through time courses:
    for idxTc in range(0, aryConv.shape[0]):

        # Extract the current time course:
        vecTc = aryPrfTcChunk[idxTc, :]

        # upsample the pixel time course, so that it matches the hrf time crs
        vecTcUps = np.zeros(int(varNumVol * varTr/varRes))
        vecOns = vecFrms[vecTc.astype(bool)]
        vecInd = np.round(vecOns / varRes).astype(np.int)
        vecTcUps[vecInd] = 1.

        # *** convolve
        for indBase, base in enumerate(lstBse):
            # perform the convolution
            col = np.convolve(base, vecTcUps, mode='full')[:vecTcUps.size]
            # get function for downsampling
            f = interp1d(vecFrmTms, col)
            # downsample to original space and assign to ary
            aryConv[idxTc, indBase, :] = f(vecFrms)

    # determine output shape
    tplOutShp = tplInpShp[:-1] + (len(lstHrf), ) + (tplInpShp[-1], )

    # Create list containing the convolved timecourses, and the process ID:
    lstOut = [idxPrc,
              aryConv.reshape(tplOutShp)]

    # Put output to queue:
    queOut.put(lstOut)

Response: convolves the time courses in a single hrf or pixel time courses in a single pixel time courses


Instruction:
Can you write a function in Python 3 where it
creates double gamma function.

Response:
def funcHrf(varNumVol, varTr):
    """Create double gamma function.

    Source:
    http://www.jarrodmillman.com/rcsds/lectures/convolution_background.html
    """
    vecX = np.arange(0, varNumVol, 1)

    # Expected time of peak of HRF [s]:
    varHrfPeak = 6.0 / varTr
    # Expected time of undershoot of HRF [s]:
    varHrfUndr = 12.0 / varTr
    # Scaling factor undershoot (relative to peak):
    varSclUndr = 0.35

    # Gamma pdf for the peak
    vecHrfPeak = gamma.pdf(vecX, varHrfPeak)
    # Gamma pdf for the undershoot
    vecHrfUndr = gamma.pdf(vecX, varHrfUndr)
    # Combine them
    vecHrf = vecHrfPeak - varSclUndr * vecHrfUndr

    # Scale maximum of HRF to 1.0:
    vecHrf = np.divide(vecHrf, np.max(vecHrf))

    return vecHrf


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cnvlTcOld(idxPrc,
              aryPrfTcChunk,
              varTr,
              varNumVol,
              queOut):
    """
    Old version:
    Convolution of time courses with one canonical HRF model.
    """
    # Create 'canonical' HRF time course model:
    vecHrf = funcHrf(varNumVol, varTr)

    # adjust the input, if necessary, such that input is 2D, with last dim time
    tplInpShp = aryPrfTcChunk.shape
    aryPrfTcChunk = aryPrfTcChunk.reshape((-1, aryPrfTcChunk.shape[-1]))

    # Prepare an empty array for ouput
    aryConv = np.zeros(np.shape(aryPrfTcChunk))

    # Each time course is convolved with the HRF separately, because the
    # numpy convolution function can only be used on one-dimensional data.
    # Thus, we have to loop through time courses:
    for idxTc, vecTc in enumerate(aryPrfTcChunk):

        # In order to avoid an artefact at the end of the time series, we have
        # to concatenate an empty array to both the design matrix and the HRF
        # model before convolution.
        vecTc = np.append(vecTc, np.zeros(100))
        vecHrf = np.append(vecHrf, np.zeros(100))

        # Convolve design matrix with HRF model:
        aryConv[idxTc, :] = np.convolve(vecTc, vecHrf,
                                        mode='full')[:varNumVol]

    # determine output shape
    tplOutShp = tplInpShp[:-1] + (1, ) + (tplInpShp[-1], )

    # Create list containing the convolved timecourses, and the process ID:
    lstOut = [idxPrc,
              aryConv.reshape(tplOutShp)]

    # Put output to queue:
    queOut.put(lstOut)

Response: This function is used to create a new time course model for a given time series.


Instruction:
Can you generate the documentation for the following Python 3 function
async def main() -> None:
    logging.basicConfig(level=logging.INFO)
    async with ClientSession() as websession:
        try:
            # Create a client:
            client = Client(websession)

            # Get user data for the client's latitude/longitude:
            user_coord_resp = await client.user_reports.status_by_coordinates(
                LATITUDE, LONGITUDE)
            _LOGGER.info(
                'User data by latitude/longitude (%s, %s): %s', LATITUDE,
                LONGITUDE, user_coord_resp)

            # Get user data for the a specific ZIP code:
            user_zip_resp = await client.user_reports.status_by_zip(ZIP_CODE)
            _LOGGER.info(
                'User data by ZIP code (%s): %s', ZIP_CODE, user_zip_resp)

            # Get CDC data for the client's latitude/longitude:
            cdc_coord_resp = await client.cdc_reports.status_by_coordinates(
                LATITUDE, LONGITUDE)
            _LOGGER.info(
                'CDC data by latitude/longitude (%s, %s): %s', LATITUDE,
                LONGITUDE, cdc_coord_resp)

            # Get CDC data for North Dakota
            cdc_state_resp = await client.cdc_reports.status_by_state(STATE)
            _LOGGER.info(
                'CDC data by state name (%s): %s', STATE, cdc_state_resp)
        except FluNearYouError as err:
            print(err)

Response: Create the aiohttp session and run the example.


Instruction:
Can you generate the documentation for the following Python 3 function
def call(cls, method, params=None, timeout=600):

        params = [] if params is None else params

        return cls.make_request(SERVER_ADDRESS, method, params, timeout=timeout)

Response: Makes a call to the LBRY API


Instruction:
How would you code a function in Python 3 to
get the state of a job.

Response:
def getJobState(self, CorpNum, JobID, UserID=None):
        """ 수집 상태 확인
            args
                CorpNum : 팝빌회원 사업자번호
                JobID : 작업아이디
                UserID : 팝빌회원 아이디
            return
                수집 상태 정보
            raise
                PopbillException
        """
        if JobID == None or len(JobID) != 18:
            raise PopbillException(-99999999, "작업아이디(jobID)가 올바르지 않습니다.")

        return self._httpget('/HomeTax/Taxinvoice/' + JobID + '/State', CorpNum, UserID)


Instruction:
Can you create a Python 3 function that
searches Tax in Aleph

Response:
def search(self, CorpNum, JobID, Type, TaxType, PurposeType, TaxRegIDType, TaxRegIDYN, TaxRegID, Page, PerPage,
               Order, UserID=None):
        """ 수집 결과 조회
            args
                CorpNum : 팝빌회원 사업자번호
                JobID : 작업아이디
                Type : 문서형태 배열, N-일반전자세금계산서, M-수정전자세금계산서
                TaxType : 과세형태 배열, T-과세, N-면세, Z-영세
                PurposeType : 영수/청구, R-영수, C-청구, N-없음
                TaxRegIDType : 종사업장번호 사업자유형, S-공급자, B-공급받는자, T-수탁자
                TaxRegIDYN : 종사업장번호 유무, 공백-전체조회, 0-종사업장번호 없음, 1-종사업장번호 있음
                TaxRegID : 종사업장번호, 콤마(",")로 구분 하여 구성 ex) '0001,0002'
                Page : 페이지 번호
                PerPage : 페이지당 목록 개수, 최대 1000개
                Order : 정렬 방향, D-내림차순, A-오름차순
                UserID : 팝빌회원 아이디
            return
                수집 결과 정보
            raise
                PopbillException
        """
        if JobID == None or len(JobID) != 18:
            raise PopbillException(-99999999, "작업아이디(jobID)가 올바르지 않습니다.")

        uri = '/HomeTax/Taxinvoice/' + JobID
        uri += '?Type=' + ','.join(Type)
        uri += '&TaxType=' + ','.join(TaxType)
        uri += '&PurposeType=' + ','.join(PurposeType)
        uri += '&TaxRegIDType=' + TaxRegIDType
        uri += '&TaxRegID=' + TaxRegID
        uri += '&Page=' + str(Page)
        uri += '&PerPage=' + str(PerPage)
        uri += '&Order=' + Order

        if TaxRegIDYN != '':
            uri += '&TaxRegIDYN=' + TaxRegIDYN

        return self._httpget(uri, CorpNum, UserID)


Instruction:
How would you explain what the following Python 3 function does
def getTaxinvoice(self, CorpNum, NTSConfirmNum, UserID=None):
        if NTSConfirmNum == None or len(NTSConfirmNum) != 24:
            raise PopbillException(-99999999, "국세청승인번호(NTSConfirmNum)가 올바르지 않습니다.")

        return self._httpget('/HomeTax/Taxinvoice/' + NTSConfirmNum, CorpNum, UserID)

Response:
전자세금계산서 상세정보 확인
            args
                CorpNum : 팝빌회원 사업자번호
                NTSConfirmNum : 국세청 승인번호
                UserID : 팝빌회원 아이디
            return
                전자세금계산서 정보객체
            raise
                PopbillException


Instruction:
Make a summary of the following Python 3 code
def make_document(self, titlestring):
        #root = etree.XML('''<?xml version="1.0"?>\
#<!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.1//EN'  'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>\
#<html xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xmlns:ops="http://www.idpf.org/2007/ops">\
#</html>''')

        root = etree.XML('''<?xml version="1.0"?>\
<!DOCTYPE html>\
<html xmlns="http://www.w3.org/1999/xhtml">\
</html>''')

        document = etree.ElementTree(root)
        html = document.getroot()
        head = etree.SubElement(html, 'head')
        etree.SubElement(html, 'body')
        title = etree.SubElement(head, 'title')
        title.text = titlestring
        #The href for the css stylesheet is a standin, can be overwritten
        etree.SubElement(head,
                         'link',
                         {'href': 'css/default.css',
                          'rel': 'stylesheet',
                          'type': 'text/css'})

        return document

Response:
This method creates a new document for writing as xml
        to the OPS subdirectory of the ePub structure.


Instruction:
Explain what the following Python 3 code does
def write_document(self, name, document):
        with open(name, 'wb') as out:
            out.write(etree.tostring(document,
                                     encoding='utf-8',
                                     pretty_print=True))

Response: This function will write a document to an XML file.


Instruction:
How would you explain what the following Python 3 function does
def format_date_string(self, date_tuple):
        months = ['', 'January', 'February', 'March', 'April', 'May', 'June',
                  'July', 'August', 'September', 'October', 'November', 'December']
        date_string = ''
        if date_tuple.season:
            return '{0}, {1}'.format(date_tuple.season, date_tuple.year)
        else:
            if not date_tuple.month and not date_tuple.day:
                return '{0}'.format(date_tuple.year)
            if date_tuple.month:
                date_string += months[int(date_tuple.month)]
            if date_tuple.day:
                date_string += ' ' + date_tuple.day
            return ', '.join([date_string, date_tuple.year])

Response: Formats a date_tuple object and returns a string that can be used to place the article content in the article.


Instruction:
Given the following Python 3 function, write the documentation
def has_out_of_flow_tables(self):
        if self.article.body is None:
            return False
        for table_wrap in self.article.body.findall('.//table-wrap'):
            graphic = table_wrap.xpath('./graphic | ./alternatives/graphic')
            table = table_wrap.xpath('./table | ./alternatives/table')
            if graphic and table:
                return True
        return False

Response: Returns True if the article has out - of - flow HTML tables False otherwise.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def process(self, article):
        if self.article is not None and not self.collection:
            log.warning('Could not process additional article. Navigation only \
handles one article unless collection mode is set.')
            return False

        if article.publisher is None:
            log.error('''Navigation cannot be generated for an Article \
without a publisher!''')
            return
        self.article = article
        self.article_doi = self.article.doi.split('/')[1]
        self.all_dois.append(self.article.doi)
        if self.collection:
            pass
        else:
            self.title = self.article.publisher.nav_title()
        for author in self.article.publisher.nav_contributors():
            self.contributors.add(author)

        #Analyze the structure of the article to create internal mapping
        self.map_navigation()

Response: Processes an article and creates the internal mapping.


Instruction:
Given the following Python 3 function, write the documentation
def recursive_article_navmap(self, src_element, depth=0, first=True):
        if depth > self.nav_depth:
            self.nav_depth = depth
        navpoints = []
        tagnames = ['sec', 'fig', 'table-wrap']
        for child in src_element:
            try:
                tagname = child.tag
            except AttributeError:
                continue
            else:
                if tagname not in tagnames:
                    continue

            #Safely handle missing id attributes
            if 'id' not in child.attrib:
                child.attrib['id'] = self.auto_id

            #If in collection mode, we'll prepend the article DOI to avoid
            #collisions
            if self.collection:
                child_id = '-'.join([self.article_doi,
                                     child.attrib['id']])
            else:
                child_id = child.attrib['id']

            #Attempt to infer the correct text as a label
            #Skip the element if we cannot
            child_title = child.find('title')
            if child_title is None:
                continue  # If there is no immediate title, skip this element
            label = element_methods.all_text(child_title)
            if not label:
                continue  # If no text in the title, skip this element
            source = 'main.{0}.xhtml#{1}'.format(self.article_doi,
                                               child.attrib['id'])
            if tagname == 'sec':
                children = self.recursive_article_navmap(child, depth=depth + 1)
                navpoints.append(navpoint(child_id,
                                          label,
                                          self.play_order,
                                          source,
                                          children))
            #figs and table-wraps do not have children
            elif tagname == 'fig':  # Add navpoints to list_of_figures
                self.figures_list.append(navpoint(child.attrib['id'],
                                                  label,
                                                  None,
                                                  source,
                                                  []))
            elif tagname == 'table-wrap':  # Add navpoints to list_of_tables
                self.tables_list.append(navpoint(child.attrib['id'],
                                                 label,
                                                 None,
                                                 source,
                                                 []))
        return navpoints

Response: This function recursively traverses the content of an article and adds the correct elements to the NCX file s navMap and Lists.


Instruction:
Can you implement a function in Python 3 that
creates the NCX element for EPUB2.

Response:
def render_EPUB2(self, location):
        """
        Creates the NCX specified file for EPUB2
        """

        def make_navlabel(text):
            """
            Creates and returns a navLabel element with the supplied text.
            """
            navlabel = etree.Element('navLabel')
            navlabel_text = etree.SubElement(navlabel, 'text')
            navlabel_text.text = text
            return navlabel

        def make_navMap(nav=None):
            if nav is None:
                nav_element = etree.Element('navMap')
                for nav_point in self.nav:
                    nav_element.append(make_navMap(nav=nav_point))
            else:
                nav_element = etree.Element('navPoint')
                nav_element.attrib['id'] = nav.id
                nav_element.attrib['playOrder'] = nav.playOrder
                nav_element.append(make_navlabel(nav.label))
                content_element = etree.SubElement(nav_element, 'content')
                content_element.attrib['src'] = nav.source
                for child in nav.children:
                    nav_element.append(make_navMap(nav=child))
            return nav_element
        root = etree.XML('''\
<?xml version="1.0"?>\
<ncx version="2005-1" xmlns="http://www.daisy.org/z3986/2005/ncx/">\
<head>\
<meta name="dtb:uid" content="{uid}"/>\
<meta name="dtb:depth" content="{depth}"/>\
<meta name="dtb:totalPageCount" content="0"/>\
<meta name="dtb:maxPageNumber" content="0"/>\
<meta name="dtb:generator" content="OpenAccess_EPUB {version}"/>\
</head>\
</ncx>'''.format(**{'uid': ','.join(self.all_dois),
                    'depth': self.nav_depth,
                    'version': __version__}))
        document = etree.ElementTree(root)
        ncx = document.getroot()

        #Create the docTitle element
        doctitle = etree.SubElement(ncx, 'docTitle')
        doctitle_text = etree.SubElement(doctitle, 'text')
        doctitle_text.text = self.title

        #Create the docAuthor elements
        for contributor in self.contributors:
            if contributor.role == 'author':
                docauthor = etree.SubElement(ncx, 'docAuthor')
                docauthor_text = etree.SubElement(docauthor, 'text')
                docauthor_text.text = contributor.name

        #Create the navMap element
        ncx.append(make_navMap())

        if self.figures_list:
            navlist = etree.SubElement(ncx, 'navList')
            navlist.append(make_navlabel('List of Figures'))
            for nav_pt in self.figures_list:
                navtarget = etree.SubElement(navlist, 'navTarget')
                navtarget.attrib['id'] = nav_pt.id
                navtarget.append(self.make_navlabel(nav_pt.label))
                content = etree.SubElement(navtarget, 'content')
                content.attrib['src'] = nav_pt.source

        if self.tables_list:
            navlist = etree.SubElement(ncx, 'navList')
            navlist.append(make_navlabel('List of Tables'))
            for nav_pt in self.tables_list:
                navtarget = etree.SubElement(navlist, 'navTarget')
                navtarget.attrib['id'] = nav_pt.id
                navtarget.append(self.make_navlabel(nav_pt.label))
                content = etree.SubElement(navtarget, 'content')
                content.attrib['src'] = nav_pt.source

        with open(os.path.join(location, 'EPUB', 'toc.ncx'), 'wb') as output:
            output.write(etree.tostring(document, encoding='utf-8', pretty_print=True))


Instruction:
Write a Python 3 script to
create 1D Gaussian. Source : http://mathworld. wolfram. com / GaussianFunction. html

Response:
def funcGauss1D(x, mu, sig):
    """ Create 1D Gaussian. Source:
    http://mathworld.wolfram.com/GaussianFunction.html
    """
    arrOut = np.exp(-np.power((x - mu)/sig, 2.)/2)
    # normalize
#    arrOut = arrOut/(np.sqrt(2.*np.pi)*sig)
    # normalize (laternative)
    arrOut = arrOut/np.sum(arrOut)
    return arrOut


Instruction:
Can you generate the documentation for the following Python 3 function
def funcGauss2D(varSizeX, varSizeY, varPosX, varPosY, varSd):

    """ Create 2D Gaussian kernel. Source:
    http://mathworld.wolfram.com/GaussianFunction.html
    """

    varSizeX = int(varSizeX)
    varSizeY = int(varSizeY)

    # aryX and aryY are in reversed order, this seems to be necessary:
    aryY, aryX = sp.mgrid[0:varSizeX,
                          0:varSizeY]

    # The actual creation of the Gaussian array:
    aryGauss = (
        (
            np.power((aryX - varPosX), 2.0) +
            np.power((aryY - varPosY), 2.0)
        ) /
        (2.0 * np.power(varSd, 2.0))
        )
    aryGauss = np.exp(-aryGauss)
    # normalize
    # aryGauss = aryGauss/(2*np.pi*np.power(varSd, 2))

    return aryGauss

Response: Create 2D Gaussian kernel. Source : mathworld. wolfram. com / GaussianFunction. html


Instruction:
Make a summary of the following Python 3 code
def funcConvPar(aryDm,
                vecHrf,
                varNumVol):

    """
    Function for convolution of pixel-wise 'design matrix' with HRF model.
    """
    # In order to avoid an artefact at the end of the time series, we have to
    # concatenate an empty array to both the design matrix and the HRF model
    # before convolution.
    aryDm = np.concatenate((aryDm, np.zeros((aryDm.shape[0], 100))), axis=1)
    vecHrf = np.concatenate((vecHrf, np.zeros((100,))))

    aryDmConv = np.empty((aryDm.shape[0], varNumVol))
    for idx in range(0, aryDm.shape[0]):
        vecDm = aryDm[idx, :]
        # Convolve design matrix with HRF model:
        aryDmConv[idx, :] = np.convolve(vecDm, vecHrf,
                                        mode='full')[:varNumVol]
    return aryDmConv

Response: Function for convolution of pixel - wise design matrix with HRF model.


Instruction:
Create a Python 3 function for
simulating an AR1 model using the parameters beta c and sigma.

Response:
def simulateAR1(n,
                beta,
                sigma,
                c,
                burnin,
                varNumCNR,
                varNumTP,
                ):
    """
    Simulates an AR(1) model using the parameters beta, c, and sigma.
    Returns an array with length n

    n := number of time points
    beta := correlation weight
    sigma := standard deviation of the noise, can be a vector
    c := constant added to the noise, default 0

    based on:
    source1: https://github.com/ndronen/misc/blob/master/python/ar1.py
    source2: http://stats.stackexchange.com/questions/22742/
             problem-simulating-ar2-process
    source3: https://kurtverstegen.wordpress.com/2013/12/07/simulation/
    """
    # Output array with noise time courses
    noise = np.empty((varNumCNR, varNumTP))
    if burnin == 1:
        burnin = 100
        n = n + burnin

    noiseTemp = c + sp.random.normal(0, 1, n)
    sims = np.zeros(n)
    sims[0] = noiseTemp[0]
    for i in range(1, n):
        sims[i] = beta*sims[i-1] + noiseTemp[i]
    sims = sims[burnin:]
    noise = sigma[:, np.newaxis]*sp.stats.mstats.zscore(sims)
    return noise


Instruction:
Can you generate a brief explanation for the following Python 3 code
def funcNrlTcMotPred(idxPrc,
                     varPixX,
                     varPixY,
                     NrlMdlChunk,
                     varNumTP,
                     aryBoxCar,  # aryCond
                     path,
                     varNumNrlMdls,
                     varNumMtDrctn,
                     varPar,
                     queOut):
    """
    Function for creating neural time course models.
    This function should be used to create neural models if different
    predictors for every motion direction are included.
    """

#    # if hd5 method is used: open file for reading
#    filename = 'aryBoxCar' + str(idxPrc) + '.hdf5'
#    hdf5_path = os.path.join(path, filename)
#    fileH = tables.openFile(hdf5_path, mode='r')

    # Output array with pRF model time courses at all modelled standard
    # deviations for current pixel position:
    aryOut = np.empty((len(NrlMdlChunk), varNumTP, varNumMtDrctn),
                      dtype='float32')

    # Prepare status indicator if this is the first of the parallel processes:
    if idxPrc == 1:

        # We create a status indicator for the time consuming pRF model finding
        # algorithm. Number of steps of the status indicator:
        varStsStpSze = 20

        # Number of pRF models to fit:
        varNumLoops = varNumNrlMdls/varPar

        # Vector with pRF values at which to give status feedback:
        vecStatus = np.linspace(0,
                                varNumLoops,
                                num=(varStsStpSze+1),
                                endpoint=True)
        vecStatus = np.ceil(vecStatus)
        vecStatus = vecStatus.astype(int)

        # Vector with corresponding percentage values at which to give status
        # feedback:
        vecStatusPrc = np.linspace(0,
                                   100,
                                   num=(varStsStpSze+1),
                                   endpoint=True)
        vecStatusPrc = np.ceil(vecStatusPrc)
        vecStatusPrc = vecStatusPrc.astype(int)

        # Counter for status indicator:
        varCntSts01 = 0
        varCntSts02 = 0

    # Loop through all Gauss parameters that are in this chunk
    for idx, NrlMdlTrpl in enumerate(NrlMdlChunk):

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 1:

            # Status indicator:
            if varCntSts02 == vecStatus[varCntSts01]:

                # Prepare status message:
                strStsMsg = ('---------Progress: ' +
                             str(vecStatusPrc[varCntSts01]) +
                             ' % --- ' +
                             str(vecStatus[varCntSts01]) +
                             ' loops out of ' +
                             str(varNumLoops))

                print(strStsMsg)

                # Only increment counter if the last value has not been
                # reached yet:
                if varCntSts01 < varStsStpSze:
                    varCntSts01 = varCntSts01 + int(1)

        # x pos of Gauss model: NrlMdlTrpl[0]
        # y pos of Gauss model: NrlMdlTrpl[1]
        # std of Gauss model: NrlMdlTrpl[2]
        # index of tng crv model: NrlMdlTrpl[3]
        varTmpX = int(np.around(NrlMdlTrpl[0], 0))
        varTmpY = int(np.around(NrlMdlTrpl[1], 0))

        # Create pRF model (2D):
        aryGauss = funcGauss2D(varPixX,
                               varPixY,
                               varTmpX,
                               varTmpY,
                               NrlMdlTrpl[2])

        # Multiply pixel-wise box car model with Gaussian pRF models:
        aryNrlTcTmp = np.multiply(aryBoxCar, aryGauss[:, :, None, None])

        # Calculate sum across x- and y-dimensions - the 'area under the
        # Gaussian surface'. This is essentially an unscaled version of the
        # neural time course model (i.e. not yet scaled for the size of
        # the pRF).
        aryNrlTcTmp = np.sum(aryNrlTcTmp, axis=(0, 1))

        # Normalise the nrl time course model to the size of the pRF. This
        # gives us the ratio of 'activation' of the pRF at each time point,
        # or, in other words, the neural time course model.
        aryNrlTcTmp = np.divide(aryNrlTcTmp,
                                np.sum(aryGauss, axis=(0, 1)))

        # Put model time courses into the function's output array:
        aryOut[idx, :, :] = aryNrlTcTmp

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 1:
            # Increment status indicator counter:
            varCntSts02 = varCntSts02 + 1

    # Output list:
    lstOut = [idxPrc,
              aryOut,
              ]

    queOut.put(lstOut)

Response: This function is used to create neural time courses for every motion direction.


Instruction:
Can you implement a function in Python 3 that
functions for finding best pRF model for voxel time course. This function should be used if there is only one predictor.

Response:
def funcFindPrf(idxPrc,
                aryFuncChnk,
                aryPrfTc,
                aryMdls,
                queOut):

    """
    Function for finding best pRF model for voxel time course.
    This function should be used if there is only one predictor.
    """

    # Number of voxels to be fitted in this chunk:
    varNumVoxChnk = aryFuncChnk.shape[0]

    # Number of volumes:
    varNumVol = aryFuncChnk.shape[1]

    # Vectors for pRF finding results [number-of-voxels times one]:
    vecBstXpos = np.zeros(varNumVoxChnk)
    vecBstYpos = np.zeros(varNumVoxChnk)
    vecBstSd = np.zeros(varNumVoxChnk)
    # vecBstR2 = np.zeros(varNumVoxChnk)

    # Vector for best R-square value. For each model fit, the R-square value is
    # compared to this, and updated if it is lower than the best-fitting
    # solution so far. We initialise with an arbitrary, high value
    vecBstRes = np.add(np.zeros(varNumVoxChnk),
                       100000.0)

    # We reshape the voxel time courses, so that time goes down the column,
    # i.e. from top to bottom.
    aryFuncChnk = aryFuncChnk.T

    # Constant term for the model:
    vecConst = np.ones((varNumVol), dtype=np.float32)

    # Change type to float 32:
    aryFuncChnk = aryFuncChnk.astype(np.float32)
    aryPrfTc = aryPrfTc.astype(np.float32)

    # Number of pRF models to fit:
    varNumMdls = len(aryMdls)

    # Prepare status indicator if this is the first of the parallel processes:
    if idxPrc == 0:

        # We create a status indicator for the time consuming pRF model finding
        # algorithm. Number of steps of the status indicator:
        varStsStpSze = 20

        # Vector with pRF values at which to give status feedback:
        vecStatPrf = np.linspace(0,
                                 varNumMdls,
                                 num=(varStsStpSze+1),
                                 endpoint=True)
        vecStatPrf = np.ceil(vecStatPrf)
        vecStatPrf = vecStatPrf.astype(int)

        # Vector with corresponding percentage values at which to give status
        # feedback:
        vecStatPrc = np.linspace(0,
                                 100,
                                 num=(varStsStpSze+1),
                                 endpoint=True)
        vecStatPrc = np.ceil(vecStatPrc)
        vecStatPrc = vecStatPrc.astype(int)

        # Counter for status indicator:
        varCntSts01 = 0
        varCntSts02 = 0

    # Loop through pRF models:
    for idxMdls in range(0, varNumMdls):

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 0:

            # Status indicator:
            if varCntSts02 == vecStatPrf[varCntSts01]:

                # Prepare status message:
                strStsMsg = ('---------Progress: ' +
                             str(vecStatPrc[varCntSts01]) +
                             ' % --- ' +
                             str(vecStatPrf[varCntSts01]) +
                             ' pRF models out of ' +
                             str(varNumMdls))

                print(strStsMsg)

                # Only increment counter if the last value has not been
                # reached yet:
                if varCntSts01 < varStsStpSze:
                    varCntSts01 = varCntSts01 + int(1)

        # Current pRF time course model:
        vecMdlTc = aryPrfTc[idxMdls, :].flatten()

        # We create a design matrix including the current pRF time
        # course model, and a constant term:
        aryDsgn = np.vstack([vecMdlTc,
                             vecConst]).T

        # Calculation of the ratio of the explained variance (R square)
        # for the current model for all voxel time courses.

#                print('------------np.linalg.lstsq on pRF: ' +
#                      str(idxX) +
#                      'x ' +
#                      str(idxY) +
#                      'y ' +
#                      str(idxSd) +
#                      'z --- START')
#                varTmeTmp01 = time.time()

        # Change type to float32:
        # aryDsgn = aryDsgn.astype(np.float32)

        # Calculate the least-squares solution for all voxels:
        vecTmpRes = np.linalg.lstsq(aryDsgn, aryFuncChnk)[1]

#                varTmeTmp02 = time.time()
#                varTmeTmp03 = np.around((varTmeTmp02 - varTmeTmp01),
#                                        decimals=2)
#                print('------------np.linalg.lstsq on pRF: ' +
#                      str(idxX) +
#                      'x ' +
#                      str(idxY) +
#                      'y ' +
#                      str(idxSd) +
#                      'z --- DONE elapsed time: ' +
#                      str(varTmeTmp03) +
#                      's')

        # Check whether current residuals are lower than previously
        # calculated ones:
        vecLgcTmpRes = np.less(vecTmpRes, vecBstRes)

        # Replace best x and y position values, and SD values.
        vecBstXpos[vecLgcTmpRes] = aryMdls[idxMdls][0]
        vecBstYpos[vecLgcTmpRes] = aryMdls[idxMdls][1]
        vecBstSd[vecLgcTmpRes] = aryMdls[idxMdls][2]

        # Replace best residual values:
        vecBstRes[vecLgcTmpRes] = vecTmpRes[vecLgcTmpRes]

#                varTmeTmp04 = time.time()
#                varTmeTmp05 = np.around((varTmeTmp04 - varTmeTmp02),
#                                        decimals=2)
#                print('------------selection of best-fitting pRF model: ' +
#                      str(idxX) +
#                      'x ' +
#                      str(idxY) +
#                      'y ' +
#                      str(idxSd) +
#                      'z --- elapsed time: ' +
#                      str(varTmeTmp05) +
#                      's')

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 0:

            # Increment status indicator counter:
            varCntSts02 = varCntSts02 + 1

    # After finding the best fitting model for each voxel, we still have to
    # calculate the coefficient of determination (R-squared) for each voxel. We
    # start by calculating the total sum of squares (i.e. the deviation of the
    # data from the mean). The mean of each time course:
    vecFuncMean = np.mean(aryFuncChnk, axis=0)
    # Deviation from the mean for each datapoint:
    vecFuncDev = np.subtract(aryFuncChnk, vecFuncMean[None, :])
    # Sum of squares:
    vecSsTot = np.sum(np.power(vecFuncDev,
                               2.0),
                      axis=0)
    # Coefficient of determination:
    vecBstR2 = np.subtract(1.0,
                           np.divide(vecBstRes,
                                     vecSsTot))

    # Output list:
    lstOut = [idxPrc,
              vecBstXpos,
              vecBstYpos,
              vecBstSd,
              vecBstR2]

    queOut.put(lstOut)


Instruction:
How would you explain what the following Python 3 function does
def funcFindPrfMltpPrdXVal(idxPrc,
                           aryFuncChnkTrn,
                           aryFuncChnkTst,
                           aryPrfMdlsTrnConv,
                           aryPrfMdlsTstConv,
                           aryMdls,
                           queOut):
    """
    Function for finding best pRF model for voxel time course.
    This function should be used if there are several predictors.
    """

    # Number of voxels to be fitted in this chunk:
    varNumVoxChnk = aryFuncChnkTrn.shape[0]

    # Number of volumes:
    varNumVolTrn = aryFuncChnkTrn.shape[2]
    varNumVolTst = aryFuncChnkTst.shape[2]

    # get number of cross validations
    varNumXval = aryPrfMdlsTrnConv.shape[2]

    # Vectors for pRF finding results [number-of-voxels times one]:
    vecBstXpos = np.zeros(varNumVoxChnk)
    vecBstYpos = np.zeros(varNumVoxChnk)
    vecBstSd = np.zeros(varNumVoxChnk)
    # vecBstR2 = np.zeros(varNumVoxChnk)

    # Vector for temporary residuals values that are obtained during
    # the different loops of cross validation
    vecTmpResXVal = np.empty((varNumVoxChnk, varNumXval), dtype='float32')

    # Vector for best residual values.
    vecBstRes = np.add(np.zeros(varNumVoxChnk),
                       100000.0)

    # Constant term for the model:
    vecConstTrn = np.ones((varNumVolTrn), dtype=np.float32)
    vecConstTst = np.ones((varNumVolTst), dtype=np.float32)

    # Change type to float 32:
    aryPrfMdlsTrnConv = aryPrfMdlsTrnConv.astype(np.float32)
    aryPrfMdlsTstConv = aryPrfMdlsTstConv.astype(np.float32)

    # Number of pRF models to fit:
    varNumMdls = len(aryMdls)

    # Prepare status indicator if this is the first of the parallel processes:
    if idxPrc == 0:

        # We create a status indicator for the time consuming pRF model finding
        # algorithm. Number of steps of the status indicator:
        varStsStpSze = 20

        # Vector with pRF values at which to give status feedback:
        vecStatPrf = np.linspace(0,
                                 varNumMdls,
                                 num=(varStsStpSze+1),
                                 endpoint=True)
        vecStatPrf = np.ceil(vecStatPrf)
        vecStatPrf = vecStatPrf.astype(int)

        # Vector with corresponding percentage values at which to give status
        # feedback:
        vecStatPrc = np.linspace(0,
                                 100,
                                 num=(varStsStpSze+1),
                                 endpoint=True)
        vecStatPrc = np.ceil(vecStatPrc)
        vecStatPrc = vecStatPrc.astype(int)

        # Counter for status indicator:
        varCntSts01 = 0
        varCntSts02 = 0

    # Loop through pRF models:
    for idxMdls in range(0, varNumMdls):

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 0:

            # Status indicator:
            if varCntSts02 == vecStatPrf[varCntSts01]:

                # Prepare status message:
                strStsMsg = ('---------Progress: ' +
                             str(vecStatPrc[varCntSts01]) +
                             ' % --- ' +
                             str(vecStatPrf[varCntSts01]) +
                             ' pRF models out of ' +
                             str(varNumMdls))

                print(strStsMsg)

                # Only increment counter if the last value has not been
                # reached yet:
                if varCntSts01 < varStsStpSze:
                    varCntSts01 = varCntSts01 + int(1)

        # Loop through different cross validations
        for idxXval in range(0, varNumXval):
            # Current pRF time course model:
            vecMdlTrn = aryPrfMdlsTrnConv[idxMdls, :, idxXval, :]
            vecMdlTst = aryPrfMdlsTstConv[idxMdls, :, idxXval, :]

            # We create a design matrix including the current pRF time
            # course model, and a constant term:
            aryDsgnTrn = np.vstack([vecMdlTrn,
                                    vecConstTrn]).T

            aryDsgnTst = np.vstack([vecMdlTst,
                                    vecConstTst]).T

            # Calculate the least-squares solution for all voxels
            # and get parameter estimates from the training fit
            aryTmpPrmEst = np.linalg.lstsq(aryDsgnTrn,
                                           aryFuncChnkTrn[:, idxXval, :].T)[0]
            # calculate predicted model fit based on training data
            aryTmpMdlTc = np.dot(aryDsgnTst, aryTmpPrmEst)
            # calculate residual sum of squares between test data and
            # predicted model fit based on training data
            vecTmpResXVal[:, idxXval] = np.sum(
                (np.subtract(aryFuncChnkTst[:, idxXval, :].T,
                             aryTmpMdlTc))**2, axis=0)

        vecTmpRes = np.mean(vecTmpResXVal, axis=1)
        # Check whether current residuals are lower than previously
        # calculated ones:
        vecLgcTmpRes = np.less(vecTmpRes, vecBstRes)

        # Replace best x and y position values, and SD values.
        vecBstXpos[vecLgcTmpRes] = aryMdls[idxMdls][0]
        vecBstYpos[vecLgcTmpRes] = aryMdls[idxMdls][1]
        vecBstSd[vecLgcTmpRes] = aryMdls[idxMdls][2]

        # Replace best residual values:
        vecBstRes[vecLgcTmpRes] = vecTmpRes[vecLgcTmpRes]

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 0:

            # Increment status indicator counter:
            varCntSts02 = varCntSts02 + 1

    # Output list:
    lstOut = [idxPrc,
              vecBstXpos,
              vecBstYpos,
              vecBstSd,
              ]

    queOut.put(lstOut)

Response: This function finds the best pRF model for voxel time course.


Instruction:
Explain what the following Python 3 code does
def move_images_to_cache(source, destination):
    if os.path.isdir(destination):
        log.debug('Cached images for this article already exist')
        return
    else:
        log.debug('Cache location: {0}'.format(destination))
        try:
            shutil.copytree(source, destination)
        except:
            log.exception('Images could not be moved to cache')
        else:
            log.info('Moved images to cache'.format(destination))

Response: Handles the movement of images to the cache.


Instruction:
Here you have a function in Python 3, explain what it does
def image_cache(article_cache, img_dir):
    log.debug('Looking for image directory in the cache')
    if os.path.isdir(article_cache):
        log.info('Cached image directory found: {0}'.format(article_cache))
        shutil.copytree(article_cache, img_dir)
        return True
    return False

Response: This method is used by get_images to copy images out of the cache.


Instruction:
Make a summary of the following Python 3 code
def make_image_cache(img_cache):
    log.info('Initiating the image cache at {0}'.format(img_cache))
    if not os.path.isdir(img_cache):
        utils.mkdir_p(img_cache)
        utils.mkdir_p(os.path.join(img_cache, '10.1371'))
        utils.mkdir_p(os.path.join(img_cache, '10.3389'))

Response: Create the image cache if it does not exist


Instruction:
How would you implement a function in Python 3 that
fetches the images from Frontiers website and store them in output_dir.

Response:
def fetch_frontiers_images(doi, output_dir):
    """
    Fetch the images from Frontiers' website. This method may fail to properly
    locate all the images and should be avoided if the files can be accessed
    locally. Downloading the images to an appropriate directory in the cache,
    or to a directory specified by passed argument are the preferred means to
    access images.
    """
    log.info('Fetching Frontiers images')
    log.warning('This method may fail to locate all images.')

    def download_image(fetch, img_file):
        try:
            image = urllib.request.urlopen(fetch)
        except urllib.error.HTTPError as e:
            if e.code == 503:  # Server overloaded
                time.sleep(1)  # Wait one second
                try:
                    image = urllib.request.urlopen(fetch)
                except:
                    return None
            elif e.code == 500:
                print('urllib.error.HTTPError {0}'.format(e.code))
            return None
        else:
            with open(img_file, 'wb') as outimage:
                outimage.write(image.read())
        return True

    def check_equation_completion(images):
        """
        In some cases, equations images are not exposed in the fulltext (hidden
        behind a rasterized table). This attempts to look for gaps and fix them
        """
        log.info('Checking for complete equations')
        files = os.listdir(output_dir)
        inline_equations = []
        for e in files:
            if e[0] == 'i':
                inline_equations.append(e)
        missing = []
        highest = 0
        if inline_equations:
            inline_equations.sort()
            highest = int(inline_equations[-1][1:4])
            i = 1
            while i < highest:
                name = 'i{0}.gif'.format(str(i).zfill(3))
                if name not in inline_equations:
                    missing.append(name)
                i += 1
        get = images[0][:-8]
        for m in missing:
            loc = os.path.join(output_dir, m)
            download_image(get + m, loc)
            print('Downloaded image {0}'.format(loc))
        #It is possible that we need to go further than the highest
        highest += 1
        name = 'i{0}.gif'.format(str(highest).zfill(3))
        loc = os.path.join(output_dir, name)
        while download_image(get + name, loc):
            print('Downloaded image {0}'.format(loc))
            highest += 1
            name = 'i{0}.gif'.format(str(highest).zfill(3))

    print('Processing images for {0}...'.format(doi))
    #We use the DOI of the article to locate the page.
    doistr = 'http://dx.doi.org/{0}'.format(doi)
    logging.debug('Accessing DOI address-{0}'.format(doistr))
    page = urllib.request.urlopen(doistr)
    if page.geturl()[-8:] == 'abstract':
        full = page.geturl()[:-8] + 'full'
    elif page.geturl()[-4:] == 'full':
        full = page.geturl()
    print(full)
    page = urllib.request.urlopen(full)
    with open('temp', 'w') as temp:
        temp.write(page.read())
    images = []
    with open('temp', 'r') as temp:
        for l in temp.readlines():
            images += re.findall('<a href="(?P<href>http://\w{7}.\w{3}.\w{3}.rackcdn.com/\d{5}/f\w{4}-\d{2}-\d{5}-HTML/image_m/f\w{4}-\d{2}-\d{5}-\D{1,2}\d{3}.\D{3})', l)
            images += re.findall('<a href="(?P<href>http://\w{7}.\w{3}.\w{3}.rackcdn.com/\d{5}/f\w{4}-\d{2}-\d{5}-r2/image_m/f\w{4}-\d{2}-\d{5}-\D{1,2}\d{3}.\D{3})', l)
            images += re.findall('<img src="(?P<src>http://\w{7}.\w{3}.\w{3}.rackcdn.com/\d{5}/f\w{4}-\d{2}-\d{5}-HTML/image_n/f\w{4}-\d{2}-\d{5}-\D{1,2}\d{3}.\D{3})', l)
    os.remove('temp')
    for i in images:
        loc = os.path.join(output_dir, i.split('-')[-1])
        download_image(i, loc)
        print('Downloaded image {0}'.format(loc))
    if images:
        check_equation_completion(images)
    print("Done downloading images")


Instruction:
Can you tell what is the following Python 3 function doing
def fetch_plos_images(article_doi, output_dir, document):
    log.info('Processing images for {0}...'.format(article_doi))

    #A dict of URLs for PLoS subjournals
    journal_urls = {'pgen': 'http://www.plosgenetics.org/article/{0}',
                    'pcbi': 'http://www.ploscompbiol.org/article/{0}',
                    'ppat': 'http://www.plospathogens.org/article/{0}',
                    'pntd': 'http://www.plosntds.org/article/{0}',
                    'pmed': 'http://www.plosmedicine.org/article/{0}',
                    'pbio': 'http://www.plosbiology.org/article/{0}',
                    'pone': 'http://www.plosone.org/article/{0}',
                    'pctr': 'http://clinicaltrials.ploshubs.org/article/{0}'}

    #Identify subjournal name for base URL
    subjournal_name = article_doi.split('.')[1]
    base_url = journal_urls[subjournal_name]

    #Acquire <graphic> and <inline-graphic> xml elements
    graphics = document.document.getroot().findall('.//graphic')
    graphics += document.document.getroot().findall('.//inline-graphic')

    #Begin to download
    log.info('Downloading images, this may take some time...')
    for graphic in graphics:
        nsmap = document.document.getroot().nsmap
        xlink_href = graphic.attrib['{' + nsmap['xlink'] + '}' + 'href']

        #Equations are handled a bit differently than the others
        #Here we decide that an image name starting with "e" is an equation
        if xlink_href.split('.')[-1].startswith('e'):
            resource = 'fetchObject.action?uri=' + xlink_href + '&representation=PNG'
        else:
            resource = xlink_href + '/largerimage'
        full_url = base_url.format(resource)
        try:
            image = urllib.request.urlopen(full_url)
        except urllib.error.HTTPError as e:
            if e.code == 503:  # Server overload error
                time.sleep(1)  # Wait a second
                try:
                    image = urllib.request.urlopen(full_url)
                except:
                    return False  # Happened twice, give up
            else:
                log.error('urllib.error.HTTPError {0}'.format(e.code))
                return False
        else:
            img_name = xlink_href.split('.')[-1] + '.png'
            img_path = os.path.join(output_dir, img_name)
            with open(img_path, 'wb') as output:
                output.write(image.read())
            log.info('Downloaded image {0}'.format(img_name))
    log.info('Done downloading images')
    return True

Response: Fetch the images for a PLoS article from the internet.


Instruction:
Explain what the following Python 3 code does
def save_tc_to_nii(strCsvCnfg, lgcTest=False, lstRat=None, lgcMdlRsp=False,
                   strPathHrf=None, lgcSaveRam=False):
    """
    Save empirical and fitted time courses to nii file format.

    Parameters
    ----------
    strCsvCnfg : str
        Absolute file path of config file used for pRF fitting.
    lgcTest : boolean
        Whether this is a test (pytest). If yes, absolute path of pyprf libary
        will be prepended to config file paths.
    lstRat : None or list
        Ratio of size of center to size of suppressive surround.
    lgcMdlRsp : boolean
        Should the aperture responses for the winner model also be saved?
    strPathHrf : str or None:
        Path to npy file with custom hrf parameters. If None, defaults
        parameters were used.
    lgcSaveRam : boolean
        Whether to also save a nii file that uses little RAM.

    Notes
    -----
    This function does not return any arguments but, instead, saves nii files
    to disk.

    """

    # %% Load configuration settings that were used for fitting

    # Load config parameters from csv file into dictionary:
    dicCnfg = load_config(strCsvCnfg, lgcTest=lgcTest)

    # Load config parameters from dictionary into namespace:
    cfg = cls_set_config(dicCnfg)

    # if fitting was done with custom hrf, make sure to retrieve results with
    # '_hrf' appendix
    if strPathHrf is not None:
        cfg.strPathOut = cfg.strPathOut + '_hrf'

    # If suppressive surround flag is on, make sure to retrieve results with
    # '_supsur' appendix
    if lstRat is not None:
        cfg.strPathOut = cfg.strPathOut + '_supsur'
        cfg.strPathMdl = cfg.strPathMdl + '_supsur'
        # Append 1.0 as the first entry, which is the key for fitting without
        # surround (only centre)
        lstRat.insert(0, 1.0)

    # %% Load previous pRF fitting results

    # Derive paths to the x, y, sigma winner parameters from pyprf_feature
    lstWnrPrm = [cfg.strPathOut + '_x_pos.nii.gz',
                 cfg.strPathOut + '_y_pos.nii.gz',
                 cfg.strPathOut + '_SD.nii.gz']

    # Check if fitting has been performed, i.e. whether parameter files exist
    # Throw error message if they do not exist.
    errorMsg = 'Files that should have resulted from fitting do not exist. \
                \nPlease perform pRF fitting first, calling  e.g.: \
                \npyprf_feature -config /path/to/my_config_file.csv'
    assert os.path.isfile(lstWnrPrm[0]), errorMsg
    assert os.path.isfile(lstWnrPrm[1]), errorMsg
    assert os.path.isfile(lstWnrPrm[2]), errorMsg

    # Load the x, y, sigma winner parameters from pyprf_feature
    aryIntGssPrm = load_res_prm(lstWnrPrm,
                                lstFlsMsk=[cfg.strPathNiiMask])[0][0]

    # Load beta parameters estimates, aka weights for time courses
    lstPathBeta = [cfg.strPathOut + '_Betas.nii.gz']
    aryBetas = load_res_prm(lstPathBeta, lstFlsMsk=[cfg.strPathNiiMask])[0][0]
    assert os.path.isfile(lstPathBeta[0]), errorMsg

    # Load ratio image, if fitting was obtained with suppressive surround
    if lstRat is not None:
        lstPathRatio = [cfg.strPathOut + '_Ratios.nii.gz']
        aryRatio = load_res_prm(lstPathRatio,
                                lstFlsMsk=[cfg.strPathNiiMask])[0][0]
        assert os.path.isfile(lstPathRatio[0]), errorMsg

    # Some voxels were excluded because they did not have sufficient mean
    # and/or variance - exclude their initial parameters, too
    # Get inclusion mask and nii header
    aryLgcMsk, aryLgcVar, hdrMsk, aryAff, aryFunc, tplNiiShp = prep_func(
        cfg.strPathNiiMask, cfg.lstPathNiiFunc, varAvgThr=-100)
    # Apply inclusion mask
    aryIntGssPrm = aryIntGssPrm[aryLgcVar, :]
    aryBetas = aryBetas[aryLgcVar, :]
    if lstRat is not None:
        aryRatio = aryRatio[aryLgcVar, :]

    # Get array with model parameters that were fitted on a grid
    # [x positions, y positions, sigmas]
    aryMdlParams = crt_mdl_prms((int(cfg.varVslSpcSzeX),
                                int(cfg.varVslSpcSzeY)), cfg.varNum1,
                                cfg.varExtXmin, cfg.varExtXmax, cfg.varNum2,
                                cfg.varExtYmin, cfg.varExtYmax,
                                cfg.varNumPrfSizes, cfg.varPrfStdMin,
                                cfg.varPrfStdMax, kwUnt='deg',
                                kwCrd=cfg.strKwCrd)
    # Load logical for parameter exclusion in unstimulated area
    lgcMdlInc = np.load(cfg.strPathMdl + '_lgcMdlInc.npy')
    # Apply logical
    aryMdlParams = aryMdlParams[lgcMdlInc, :]

    # Get corresponding pRF model time courses
    aryPrfTc = np.load(cfg.strPathMdl + '.npy')

    # The model time courses will be preprocessed such that they are smoothed
    # (temporally) with same factor as the data and that they will be z-scored:
    aryPrfTc = prep_models(aryPrfTc, varSdSmthTmp=cfg.varSdSmthTmp)

    if lgcMdlRsp:
        aryMdlRsp = np.load(cfg.strPathMdl + '_mdlRsp.npy')

    # %% Derive fitted time course models for all voxels

    # Initialize array that will collect the fitted time courses
    aryFitTc = np.zeros((aryFunc.shape), dtype=np.float32)
    # If desired, initiliaze array that will collect model responses underlying
    # the fitted time course
    if lgcMdlRsp:
        if lstRat is not None:
            aryFitMdlRsp = np.zeros((aryIntGssPrm.shape[0], aryMdlRsp.shape[1],
                                     aryMdlRsp.shape[3]),
                                    dtype=np.float32)
        else:
            aryFitMdlRsp = np.zeros((aryIntGssPrm.shape[0],
                                     aryMdlRsp.shape[1]), dtype=np.float32)

    # create vector that allows to check whether every voxel is visited
    # exactly once
    vecVxlTst = np.zeros(aryIntGssPrm.shape[0])

    # Find unique rows of fitted model parameters
    aryUnqRows, aryUnqInd = fnd_unq_rws(aryIntGssPrm, return_index=False,
                                        return_inverse=True)

    # Loop over all best-fitting model parameter combinations found
    print('---Assign models to voxels')
    for indRow, vecPrm in enumerate(aryUnqRows):
        # Get logical for voxels for which this prm combi was the best
        lgcVxl = [aryUnqInd == indRow][0]
        if np.all(np.invert(lgcVxl)):
            print('---No voxel found')
        # Mark those voxels that were visited
        vecVxlTst[lgcVxl] += 1

        # Get logical index for the model number
        # This can only be 1 index, so we directly get 1st entry of array
        lgcMdl = np.where(np.isclose(aryMdlParams, vecPrm,
                                     atol=0.01).all(axis=1))[0][0]
        # Tell user if no model was found
        if lgcMdl is None:
            print('---No model found')

        # Get model time courses
        aryMdlTc = aryPrfTc[lgcMdl, ...]
        # Get beta parameter estimates
        aryWeights = aryBetas[lgcVxl, :]

        # If fitting was done with surround suppression, find ratios for voxels
        # and the indices of these ratios in lstRat
        if lstRat is not None:
            aryVxlRatio = aryRatio[lgcVxl, :]
            indRat = [ind for ind, rat1 in enumerate(lstRat) for rat2 in
                      aryVxlRatio[:, 0] if np.isclose(rat1, rat2)]
            indVxl = range(len(indRat))

        # Combine model time courses and weights to yield fitted time course
        if lstRat is not None:
            aryFitTcTmp = np.tensordot(aryWeights, aryMdlTc, axes=([1], [0]))
            aryFitTc[lgcVxl, :] = aryFitTcTmp[indVxl, indRat, :]
        else:
            aryFitTc[lgcVxl, :] = np.dot(aryWeights, aryMdlTc)

        # If desired by user, also save the model responses per voxels
        if lgcMdlRsp:
            # If desired also save the model responses that won
            if lstRat is not None:
                aryFitMdlRsp[lgcVxl, :] = aryMdlRsp[lgcMdl, :, indRat, :]
            else:
                aryFitMdlRsp[lgcVxl, :] = aryMdlRsp[lgcMdl, :]

    # check that every voxel was visited exactly once
    errMsg = 'At least one voxel visited more than once for tc recreation'
    assert len(vecVxlTst) == np.sum(vecVxlTst), errMsg

    # %% Export preprocessed voxel time courses as nii

    # List with name suffices of output images:
    lstNiiNames = ['_EmpTc']

    # Create full path names from nii file names and output path
    lstNiiNames = [cfg.strPathOut + strNii + '.nii.gz' for strNii in
                   lstNiiNames]

    # export aryFunc as a single 4D nii file
    print('---Save empirical time courses')
    export_nii(aryFunc, lstNiiNames, aryLgcMsk, aryLgcVar, tplNiiShp,
               aryAff, hdrMsk, outFormat='4D')
    print('------Done.')

    # If desired by user, also save RAM-saving version of nii
    if lgcSaveRam:
        strPthRamOut = cfg.strPathOut + '_EmpTc_saveRAM' + '.nii.gz'
        imgNii = nb.Nifti1Image(np.expand_dims(np.expand_dims(aryFunc, axis=1),
                                               axis=1),
                                affine=np.eye(4))
        nb.save(imgNii, strPthRamOut)

    # %% Export fitted time courses and, if desired, model responses as nii

    # List with name suffices of output images:
    lstNiiNames = ['_FitTc']

    # Create full path names from nii file names and output path
    lstNiiNames = [cfg.strPathOut + strNii + '.nii.gz' for strNii in
                   lstNiiNames]

    # export aryFitTc as a single 4D nii file
    print('---Save fitted time courses')
    export_nii(aryFitTc, lstNiiNames, aryLgcMsk, aryLgcVar, tplNiiShp,
               aryAff, hdrMsk, outFormat='4D')
    print('------Done.')

    if lgcMdlRsp:

        # Create full path name
        strNpyName = cfg.strPathOut + '_FitMdlRsp' + '.npy'

        # Save aryFitMdlRsp as npy file
        print('---Save fitted model responses')
        np.save(strNpyName, aryFitMdlRsp)
        print('------Done.')

        # Save the mask so we know which voxels these parameters belonged to
        strNpyMskName = cfg.strPathOut + '_FitMdlRsp_Mask' + '.npy'
        aryLgcMsk[aryLgcMsk] = aryLgcVar
        print('---Save mask for fitted model responses')
        np.save(strNpyMskName, aryLgcMsk)
        print('------Done.')

    # If desired by user, also save RAM-saving version of nii
    if lgcSaveRam:
        strPthRamOut = cfg.strPathOut + '_FitTc_saveRAM' + '.nii.gz'
        imgNii = nb.Nifti1Image(np.expand_dims(np.expand_dims(aryFitTc,
                                                              axis=1),
                                               axis=1),
                                affine=np.eye(4))
        nb.save(imgNii, strPthRamOut)

Response: Save empirical and fitted time courses to nii file format.


Instruction:
Can you write a function in Python 3 where it
simulates pRF response given pRF parameters and stimulus apertures.

Response:
def pyprf_sim(strPrior, strStmApr, lgcNoise=False, lgcRtnNrl=True,
              lstRat=None, lgcTest=False):
    """
    Simulate pRF response given pRF parameters and stimulus apertures.

    Parameters
    ----------
    strPrior : str
        Absolute file path of config file used for pRF fitting.
    strStmApr : str
        Absolute file path to stimulus aperture used in in-silico experiment.
    lgcNoise : boolean
        Should noise be added to the simulated pRF time course. By default, no
        noise is added.
    lgcRtnNrl : boolean
        Should neural time course, unconvolved with hrf, be returned as well?
    lstRat : None or list
        Ratio of size of center to size of suppressive surround.
    lgcTest : boolean
        Whether this is a test (pytest). If yes, absolute path of pyprf libary
        will be prepended to config file paths.

    Notes
    -----
    [1] This function does not return any arguments but, instead, saves nii
        filex to disk.
    [2] strStmApr should be a path to a npy file that contains a 3D numpy
        array. This arrays consists of binary images in boolean array from that
        represent the stimulus aperture. Images are stacked along last axis.

    """

    # %% Load configuration settings that were used for fitting

    # Load config parameters from csv file into dictionary:
    dicCnfg = load_config(strPrior, lgcTest=lgcTest)

    # Load config parameters from dictionary into namespace:
    cfg = cls_set_config(dicCnfg)

    # If suppressive surround flag is on, make sure to retrieve results from
    # that fitting
    if lstRat is not None:
        cfg.strPathOut = cfg.strPathOut + '_supsur'

    # %% Load previous pRF fitting results

    # Derive paths to the x, y, sigma winner parameters from pyprf_feature
    lstWnrPrm = [cfg.strPathOut + '_x_pos.nii.gz',
                 cfg.strPathOut + '_y_pos.nii.gz',
                 cfg.strPathOut + '_SD.nii.gz']

    # Check if fitting has been performed, i.e. whether parameter files exist
    # Throw error message if they do not exist.
    errorMsg = 'Files that should have resulted from fitting do not exist. \
                \nPlease perform pRF fitting first, calling  e.g.: \
                \npyprf_feature -config /path/to/my_config_file.csv'
    assert os.path.isfile(lstWnrPrm[0]), errorMsg
    assert os.path.isfile(lstWnrPrm[1]), errorMsg
    assert os.path.isfile(lstWnrPrm[2]), errorMsg

    # Load the x, y, sigma winner parameters from pyprf_feature
    aryIntGssPrm = load_res_prm(lstWnrPrm,
                                lstFlsMsk=[cfg.strPathNiiMask])[0][0]

    # Also load suppresive surround params if suppressive surround flag was on
    if lstRat is not None:
        # Load beta parameters estimates, aka weights, this is later needed to
        # scale responses of the center wrt to the surround
        lstPathBeta = [cfg.strPathOut + '_Betas.nii.gz']
        aryBetas = load_res_prm(lstPathBeta,
                                lstFlsMsk=[cfg.strPathNiiMask])[0][0]
        # Load ratio of prf sizes
        lstPathRat = [cfg.strPathOut + '_Ratios.nii.gz']
        aryRat = load_res_prm(lstPathRat, lstFlsMsk=[cfg.strPathNiiMask])[0][0]

    # Some voxels were excluded because they did not have sufficient mean
    # and/or variance - exclude their initial parameters, too
    # Get inclusion mask and nii header
    aryLgcMsk, aryLgcVar, hdrMsk, aryAff, _, tplNiiShp = prep_func(
        cfg.strPathNiiMask, cfg.lstPathNiiFunc, varAvgThr=100.)
    # Apply inclusion mask
    aryIntGssPrm = aryIntGssPrm[aryLgcVar, :]
    if lstRat is not None:
        aryBetas = aryBetas[aryLgcVar, :]
        aryRat = aryRat[aryLgcVar]

    # %% Load stimulus aperture and create model responses to stimuli

    # Load stimulus aperture
    aryStmApr = np.load(strStmApr)
    # Which dimensions does the representation have in pixel space?
    tplStmApr = aryStmApr.shape[:2]

    # Convert winner parameters from degrees of visual angle to pixel
    vecIntX, vecIntY, vecIntSd = rmp_deg_pixel_xys(aryIntGssPrm[:, 0],
                                                   aryIntGssPrm[:, 1],
                                                   aryIntGssPrm[:, 2],
                                                   tplStmApr,
                                                   cfg.varExtXmin,
                                                   cfg.varExtXmax,
                                                   cfg.varExtYmin,
                                                   cfg.varExtYmax)

    aryIntGssPrmPxl = np.column_stack((vecIntX, vecIntY, vecIntSd))

    # Create 2D Gauss model responses to spatial conditions.
    print('---Create 2D Gauss model responses to spatial conditions')
    aryMdlRsp = crt_mdl_rsp(aryStmApr, tplStmApr, aryIntGssPrmPxl, cfg.varPar)

    # If supsur flag was provided, also create responses with supsur params
    # and combine positive center response with negative surround response
    if lstRat is not None:
        aryIntGssPrmPxlSur = np.copy(aryIntGssPrmPxl)
        # Adjust pRF sizes using the ratio of pRF sizes
        aryIntGssPrmPxlSur[:, 2] = np.multiply(aryIntGssPrmPxlSur[:, 2],
                                               aryRat)
        aryMdlRspSur = crt_mdl_rsp(aryStmApr, tplStmApr, aryIntGssPrmPxlSur,
                                   cfg.varPar)
        # Now the responses of the center and the surround need to be combined
        # in a meaningful way. One way this could be done is to take the ratio
        # of gain parameters that were found when fitting (i.e. betas)
        varGainRat = np.divide(aryBetas[:, 0], aryBetas[:, 1])
        aryMdlRsp = np.subtract(aryMdlRsp,
                                np.multiply(varGainRat, aryMdlRspSur))

    # %% Convolve time courses with hrf function

    # First temporally upsamle the model response
    aryMdlRspUps = np.repeat(aryMdlRsp, cfg.varTmpOvsmpl, axis=-1)
    # Convolve with hrf function
    arySimRsp = crt_prf_tc(aryMdlRspUps, aryMdlRsp.shape[-1], cfg.varTr,
                           cfg.varTmpOvsmpl, 1, tplStmApr, cfg.varPar)
    # Squeeze simulated reponse. This step is necessary because crt_prf_tc is,
    # in principle, capable of convolving with deriavtes of canonical function
    if arySimRsp.shape[1] > 1:
        print('***WARNING: pyprf_sim expects 1 hrf function, currently***')
    arySimRsp = np.squeeze(arySimRsp)
    # Save memory by deleting upsampled time course
    del(aryMdlRspUps)

    # %% Add auto-correlated noise

    if lgcNoise:
        print('***Adding noise feature not yet implemented***')

    # %% Export simulated prf, and if desired neural, time courses as nii

    # List with name suffices of output images:
    lstNiiNames = ['_SimPrfTc']

    # Create full path names from nii file names and output path
    lstNiiNames = [cfg.strPathOut + strNii + '.nii.gz' for strNii in
                   lstNiiNames]

    # export beta parameter as a single 4D nii file
    print('---Save simulated pRF time courses')
    export_nii(arySimRsp, lstNiiNames, aryLgcMsk, aryLgcVar, tplNiiShp,
               aryAff, hdrMsk, outFormat='4D')
    print('------Done.')

    if lgcRtnNrl:

        # List with name suffices of output images:
        lstNiiNames = ['_SimNrlTc']

        # Create full path names from nii file names and output path
        lstNiiNames = [cfg.strPathOut + strNii + '.nii.gz' for strNii in
                       lstNiiNames]

        # export beta parameter as a single 4D nii file
        print('---Save simulated neural time courses')
        export_nii(aryMdlRsp, lstNiiNames, aryLgcMsk, aryLgcVar, tplNiiShp,
                   aryAff, hdrMsk, outFormat='4D')
        print('------Done.')


Instruction:
Make a summary of the following Python 3 code
def get_nearest_by_numeric_key(data: dict, key: int) -> Any:
    return data.get(key, data[min(data.keys(), key=lambda k: abs(k - key))])

Response: Return the dict element whose numeric key is closest to a target.


Instruction:
Explain what the following Python 3 code does
def resource(self, uri, methods=frozenset({'GET'}), host=None,
                 strict_slashes=None, stream=False, version=None, name=None,
                 **kwargs):
        """
        Create a blueprint resource route from a decorated function.

        :param uri: endpoint at which the route will be accessible.
        :param methods: list of acceptable HTTP methods.
        :param host:
        :param strict_slashes:
        :param version:
        :param name: user defined route name for url_for
        :return: function or class instance

        Accepts any keyword argument that will be passed to the app resource.
        """
        if strict_slashes is None:
            strict_slashes = self.strict_slashes

        def decorator(handler):
            self.resources.append((
                FutureRoute(handler, uri, methods, host, strict_slashes,
                            stream, version, name),
                kwargs))

            return handler
        return decorator

Response: Create a blueprint resource route from a function.


Instruction:
Implement a Python 3 function for
adding a blueprint resource route to the app.

Response:
def add_resource(self, handler, uri, methods=frozenset({'GET'}),
                     host=None, strict_slashes=None, version=None, name=None,
                     **kwargs):
        """
        Create a blueprint resource route from a function.

        :param uri: endpoint at which the route will be accessible.
        :param methods: list of acceptable HTTP methods.
        :param host:
        :param strict_slashes:
        :param version:
        :param name: user defined route name for url_for
        :return: function or class instance

        Accepts any keyword argument that will be passed to the app resource.
        """
        self.resource(uri=uri, methods=methods, host=host,
                      strict_slashes=strict_slashes, version=version,
                      name=name, **kwargs)(handler)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_db():
    from .settings import settings
    mongo = settings.MONGODB

    if 'URI' in mongo and mongo['URI']:
        uri = mongo['URI']
    else:
        uri = 'mongodb://'

        if all(mongo.get(key) for key in ('USERNAME', 'PASSWORD')):
            uri += '{0}:{1}@'.format(mongo['USERNAME'], mongo['PASSWORD'])

        if 'HOSTS' in mongo and mongo['HOSTS']:
            uri += ','.join(
                '{0}:{1}'.format(host, port)
                for (host, port) in zip(mongo['HOSTS'], mongo['PORTS']),
            )
        else:
            uri += '{0}:{1}'.format(mongo['HOST'], mongo.get('PORT', 27017))

        uri += '/' + mongo['DATABASE']

        if 'OPTIONS' in mongo and mongo['OPTIONS']:
            uri += '?{0}'.format('&'.join(mongo['OPTIONS']))

    client = ConnectionFailureProxy(MongoClient(uri, connect=False))
    database = client[parse_uri(uri)['database']]

    return database

Response: Returns the connection to the database using the settings. MONGODB


Instruction:
Can you create a Python 3 function that
calculates voxel betas and R^2 for the best model.

Response:
def getBetas(idxPrc,
             aryPrfTc,
             lstAllMdlInd,
             aryFuncChnk,
             aryBstIndChnk,
             betaSw,
             queOut):
    """Calculate voxel betas and R^2 for the best model.

    Parameters
    ----------
    idxPrc : TODO
        (?)
    aryPrfTc : np.array, shape (?)
        Population receptive field time courses.
    lstAllMdlInd : list
        List of the indices of all models.
    aryFuncChnk : TODO
        Chunk of something(?)
    aryBstIndChnk : np.array, shape (?)
        Points for every voxel to the index of the best model
    betaSw : str, iterator, or np.array, shape (?)
        Best beta correlation coefficients found in training.
    queOut : TODO
        Queue output (?)

    Notes
    -----
    This is done after fitting with cross validation, since during the
    fitting process, we never fit the model to the entire data.
    """

    # get number of motion directions
    varNumMtnDrctns = aryPrfTc.shape[3]
    varNumVoxChnk = aryBstIndChnk.shape[0]
    # prepare array for best beta weights
    if type(betaSw) is sklearn.model_selection._split.KFold:
        aryEstimMtnCrvTrn = np.zeros((varNumVoxChnk, varNumMtnDrctns,
                                      betaSw.get_n_splits()), dtype='float32')
        aryEstimMtnCrvTst = np.zeros((varNumVoxChnk, varNumMtnDrctns,
                                      betaSw.get_n_splits()), dtype='float32')
        resTrn = np.zeros((varNumVoxChnk, betaSw.get_n_splits()),
                          dtype='float32')
        resTst = np.zeros((varNumVoxChnk, betaSw.get_n_splits()),
                          dtype='float32')
        aryErrorTrn = np.zeros((varNumVoxChnk), dtype='float32')
        aryErrorTst = np.zeros((varNumVoxChnk), dtype='float32')
        contrast = np.array([
                     [1, 0, 0, 0, 0],
                     [0, 1, 0, 0, 0],
                     [0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0],
                     ])
        denomTrn = np.zeros((varNumVoxChnk, betaSw.get_n_splits(),
                             len(contrast)), dtype='float32')
        denomTst = np.zeros((varNumVoxChnk, betaSw.get_n_splits(),
                             len(contrast)), dtype='float32')

    elif type(betaSw) is np.ndarray and betaSw.dtype == 'bool':
        aryEstimMtnCrvTrn = np.zeros((varNumVoxChnk, varNumMtnDrctns,
                                      ), dtype='float32')
        aryEstimMtnCrvTst = np.zeros((varNumVoxChnk, varNumMtnDrctns,
                                      ), dtype='float32')
        resTrn = np.zeros((varNumVoxChnk),
                          dtype='float32')
        resTst = np.zeros((varNumVoxChnk),
                          dtype='float32')
        aryErrorTrn = np.zeros((varNumVoxChnk), dtype='float32')
        aryErrorTst = np.zeros((varNumVoxChnk), dtype='float32')
        contrast = np.array([
                     [1, 0, 0, 0, 0],
                     [0, 1, 0, 0, 0],
                     [0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0],
                     ])
        denomTrn = np.zeros((varNumVoxChnk,
                             len(contrast)), dtype='float32')
        denomTst = np.zeros((varNumVoxChnk,
                             len(contrast)), dtype='float32')

    else:
        aryEstimMtnCrv = np.zeros((varNumVoxChnk, varNumMtnDrctns),
                                  dtype='float32')
    # prepare array for best residuals
    vecBstRes = np.zeros(varNumVoxChnk, dtype='float32')
    vecBstRes[:] = np.inf
    # prepare counter to check that every voxel is matched to one winner mdl
    vecLgcCounter = np.zeros(varNumVoxChnk, dtype='float32')

    # We reshape the voxel time courses, so that time goes down the column
    aryFuncChnk = aryFuncChnk.T

    # Change type to float 32:
    aryFuncChnk = aryFuncChnk.astype(np.float32)
    aryPrfTc = aryPrfTc.astype(np.float32)

    # Prepare status indicator if this is the first of the parallel processes:
    if idxPrc == 0:

        # We create a status indicator for the time consuming pRF model finding
        # algorithm. Number of steps of the status indicator:
        varStsStpSze = 20

        # Number of pRF models to fit:
        varNumMdls = len(lstAllMdlInd)

        # Vector with pRF values at which to give status feedback:
        vecStatPrf = np.linspace(0,
                                 varNumMdls,
                                 num=(varStsStpSze+1),
                                 endpoint=True)
        vecStatPrf = np.ceil(vecStatPrf)
        vecStatPrf = vecStatPrf.astype(int)

        # Vector with corresponding percentage values at which to give status
        # feedback:
        vecStatPrc = np.linspace(0,
                                 100,
                                 num=(varStsStpSze+1),
                                 endpoint=True)
        vecStatPrc = np.ceil(vecStatPrc)
        vecStatPrc = vecStatPrc.astype(int)

        # Counter for status indicator:
        varCntSts01 = 0
        varCntSts02 = 0

    # Loop through pRF models:
    for idx, mdlInd in enumerate(lstAllMdlInd):

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 0:

            # Status indicator:
            if varCntSts02 == vecStatPrf[varCntSts01]:

                # Prepare status message:
                strStsMsg = ('---------Progress: ' +
                             str(vecStatPrc[varCntSts01]) +
                             ' % --- ' +
                             str(vecStatPrf[varCntSts01]) +
                             ' pRF models out of ' +
                             str(varNumMdls))

                print(strStsMsg)

                # Only increment counter if the last value has not been
                # reached yet:
                if varCntSts01 < varStsStpSze:
                    varCntSts01 = varCntSts01 + int(1)

        # check whether any model had this particular x, y, sigma combination
        # as its best model
        lgcTemp = [aryBstIndChnk == idx][0]

        if np.greater(np.sum(lgcTemp), 0):
            # get current design matrix
            aryDsgnTmp = aryPrfTc[mdlInd].T

            if betaSw is 'train':  # training

                aryTmpPrmEst, aryTmpRes = np.linalg.lstsq(
                    aryDsgnTmp, aryFuncChnk[:, lgcTemp])[0:2]
                aryEstimMtnCrv[lgcTemp, :] = aryTmpPrmEst.T
                vecBstRes[lgcTemp] = aryTmpRes

            elif type(betaSw) is np.ndarray and betaSw.dtype == 'float':
                # get beta weights for axis of motion tuning curves
                aryEstimMtnCrv[lgcTemp, :] = np.linalg.lstsq(
                    aryDsgnTmp, aryFuncChnk[:, lgcTemp])[0].T
                # calculate prediction
                aryPredTc = np.dot(aryDsgnTmp,
                                   betaSw[lgcTemp, :].T)
                # Sum of squares:
                vecBstRes[lgcTemp] = np.sum((aryFuncChnk[:, lgcTemp] -
                                             aryPredTc) ** 2,  axis=0)

            elif type(betaSw) is np.ndarray and betaSw.dtype == 'bool':
                # get beta weights for training
                betas, resTrn[lgcTemp] = np.linalg.lstsq(
                    aryDsgnTmp[betaSw, :], aryFuncChnk[betaSw][:, lgcTemp])[0:2]
                aryEstimMtnCrvTrn[lgcTemp, :] = betas.T
                # get beta weights for validation
                betas, resTst[lgcTemp] = np.linalg.lstsq(
                    aryDsgnTmp[~betaSw, :], aryFuncChnk[~betaSw][:, lgcTemp])[0:2]
                aryEstimMtnCrvTrn[lgcTemp, :] = betas.T
                # calculate CC for training
                aryCcTrn = np.linalg.pinv(
                    np.dot(aryDsgnTmp[betaSw, :].T,
                           aryDsgnTmp[betaSw, :]))
                aryCcTst = np.linalg.pinv(
                    np.dot(aryDsgnTmp[~betaSw, :].T,
                           aryDsgnTmp[~betaSw, :]))
                # calculate Error for training
                aryErrorTrn[lgcTemp] = np.var(
                    np.subtract(aryFuncChnk[betaSw][:, lgcTemp],
                                np.dot(aryDsgnTmp[betaSw, :],
                                       aryEstimMtnCrvTrn[lgcTemp, :].T)), axis=0)
                # calculate Error for test
                aryErrorTst[lgcTemp] = np.var(
                    np.subtract(aryFuncChnk[~betaSw][:, lgcTemp],
                                np.dot(aryDsgnTmp[~betaSw, :],
                                       aryEstimMtnCrvTst[lgcTemp, :].T)), axis=0)
                # calculate denominator for training
                for indContr, contr in enumerate(contrast):
                    denomTrn[lgcTemp, indContr] = np.sqrt(
                        aryErrorTrn[lgcTemp] * np.dot(
                            np.dot(contr, aryCcTrn), contr.T))
                    denomTst[lgcTemp, indContr] = np.sqrt(
                        aryErrorTst[lgcTemp] * np.dot(
                            np.dot(contr, aryCcTst), contr.T))

            elif type(betaSw) is sklearn.model_selection._split.KFold:
                for idxCV, (idxTrn, idxVal) in enumerate(betaSw.split(aryDsgnTmp)):
                    # get beta weights for training
                    betas, resTrn[lgcTemp, idxCV] = np.linalg.lstsq(
                        aryDsgnTmp[idxTrn], aryFuncChnk[idxTrn][:, lgcTemp])[0:2]
                    aryEstimMtnCrvTrn[lgcTemp, :, idxCV] = betas.T
                    # get beta weights for validation
                    betas, resTst[lgcTemp, idxCV] = np.linalg.lstsq(
                        aryDsgnTmp[idxVal], aryFuncChnk[idxVal][:, lgcTemp])[0:2]
                    aryEstimMtnCrvTst[lgcTemp, :, idxCV] = betas.T
                    # calculate CC for training
                    aryCcTrn = np.linalg.pinv(
                        np.dot(aryDsgnTmp[idxTrn].T,
                               aryDsgnTmp[idxTrn]))
                    aryCcTst = np.linalg.pinv(
                        np.dot(aryDsgnTmp[idxVal].T,
                               aryDsgnTmp[idxVal]))
                    # calculate Error for training
                    aryErrorTrn[lgcTemp] = np.var(
                        np.subtract(aryFuncChnk[idxTrn][:, lgcTemp],
                                    np.dot(aryDsgnTmp[idxTrn],
                                           aryEstimMtnCrvTrn[lgcTemp, :, idxCV].T)), axis=0)
                    # calculate Error for test
                    aryErrorTst[lgcTemp] = np.var(
                        np.subtract(aryFuncChnk[idxVal][:, lgcTemp],
                                    np.dot(aryDsgnTmp[idxVal],
                                           aryEstimMtnCrvTst[lgcTemp, :, idxCV].T)), axis=0)
                    # calculate denominator for training
                    for indContr, contr in enumerate(contrast):
                        denomTrn[lgcTemp, idxCV, indContr] = np.sqrt(
                            aryErrorTrn[lgcTemp] * np.dot(
                                np.dot(contr, aryCcTrn), contr.T))
                        denomTst[lgcTemp, idxCV, indContr] = np.sqrt(
                            aryErrorTst[lgcTemp] * np.dot(
                                np.dot(contr, aryCcTst), contr.T))

            # increase logical counter to verify later that every voxel
            # was visited only once
            vecLgcCounter[lgcTemp] += 1

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 0:
            # Increment status indicator counter:
            varCntSts02 = varCntSts02 + 1

    # check that every voxel was visited only once
    strErrMsg = ('It looks like at least voxel was revisted more than once. ' +
                 'Check whether the R2 was calculated correctly')
    assert np.sum(vecLgcCounter) == len(vecLgcCounter), strErrMsg

    if type(betaSw) is sklearn.model_selection._split.KFold:

        # calculate t-values
        aryTvalsTrn = np.empty((varNumVoxChnk, contrast.shape[0],
                                betaSw.get_n_splits()))
        aryTvalsTst = np.empty((varNumVoxChnk, contrast.shape[0],
                                betaSw.get_n_splits()))

        for ind1, contr in enumerate(contrast):
            for ind2 in range(betaSw.get_n_splits()):
                aryTvalsTrn[:, ind1, ind2] = np.divide(
                    np.dot(contr, aryEstimMtnCrvTrn[:, :, ind2].T),
                    denomTrn[:, ind2, ind1])
                aryTvalsTst[:, ind1, ind2] = np.divide(
                    np.dot(contr, aryEstimMtnCrvTst[:, :, ind2].T),
                    denomTst[:, ind2, ind1])

        # Output list:
        lstOut = [idxPrc,
                  aryEstimMtnCrvTrn,
                  aryEstimMtnCrvTst,
                  aryTvalsTrn,
                  aryTvalsTst,
                  ]
        queOut.put(lstOut)

    elif type(betaSw) is np.ndarray and betaSw.dtype == 'bool':
        # calculate t-values
        aryTvalsTrn = np.empty((varNumVoxChnk, contrast.shape[0],
                                ))
        aryTvalsTst = np.empty((varNumVoxChnk, contrast.shape[0],
                                ))

        for ind1, contr in enumerate(contrast):
                aryTvalsTrn[:, ind1] = np.divide(
                    np.dot(contr, aryEstimMtnCrvTrn.T),
                    denomTrn[:, ind1])
                aryTvalsTst[:, ind1] = np.divide(
                    np.dot(contr, aryEstimMtnCrvTst.T),
                    denomTst[:, ind1])

        # Output list:
        lstOut = [idxPrc,
                  aryEstimMtnCrvTrn,
                  aryEstimMtnCrvTst,
                  aryTvalsTrn,
                  aryTvalsTst,
                  ]
        queOut.put(lstOut)

    else:
        # After finding the best fitting model for each voxel, we still have to
        # calculate the coefficient of determination (R-squared) for each voxel. We
        # start by calculating the total sum of squares (i.e. the deviation of the
        # data from the mean). The mean of each time course:
        vecFuncMean = np.mean(aryFuncChnk, axis=0)
        # Deviation from the mean for each datapoint:
        vecFuncDev = np.subtract(aryFuncChnk, vecFuncMean[None, :])
        # Sum of squares:
        vecSsTot = np.sum(np.power(vecFuncDev,
                                   2.0),
                          axis=0)
        # Coefficient of determination:
        vecBstR2 = np.subtract(1.0,
                               np.divide(vecBstRes,
                                         vecSsTot))
        # Output list:
        lstOut = [idxPrc,
                  vecBstR2,
                  aryEstimMtnCrv]
        queOut.put(lstOut)


Instruction:
Here you have a function in Python 3, explain what it does
def nav_creators(self, article):
        creator_list = []
        for contrib_group in article.metadata.front.article_meta.contrib_group:
            for contrib in contrib_group.contrib:
                if not contrib.attrs['contrib-type'] == 'author':
                    continue
                if contrib.collab:
                    auth = etree.tostring(contrib.collab[0].node, method='text', encoding='utf-8')
                    file_as = auth
                elif contrib.anonymous:
                    auth = 'Anonymous'
                    file_as = auth
                else:
                    name = contrib.name[0]  # Work with only first name listed
                    surname = name.surname.text
                    given = name.given_names
                    if given:  # Given is optional
                        if given.text:  # Odd instances of empty tags
                            auth = ' '.join([surname, given.text])
                            given_initial = given.text[0]
                            file_as = ', '.join([surname, given_initial])
                        else:
                            auth = surname
                            file_as = auth
                    else:
                        auth = surname
                        file_as = auth
                new_creator = creator(auth, 'aut', file_as)
                creator_list.append(new_creator)
        return creator_list

Response: This method returns the list of creator objects that are available for the article.


Instruction:
Here you have a function in Python 3, explain what it does
def nav_title(self, article):
        article_title = article.metadata.front.article_meta.title_group.article_title.node
        return str(etree.tostring(article_title, method='text', encoding='utf-8'), encoding='utf-8')

Response: This method returns the string representation of the article s title.


Instruction:
Can you tell what is the following Python 3 function doing
def format_citation(citation, citation_type=None):
    cite_types = {'book': self.format_book_citation,
                  'commun': self.format_commun_citation,
                  'confproc': self.format_confproc_citation,
                  'discussion': self.format_discussion_citation,
                  'gov': self.format_gov_citation,
                  'journal': self.format_journal_citation,
                  'list': self.format_list_citation,
                  'other': self.format_other_citation,
                  'patent': self.format_patent_citation,
                  'thesis': self.format_thesis_citation,
                  'web': self.format_web_citation,
                  '': self.format_other_citation,  # Empty becomes 'other'
                  None: self.format_other_citation}  # None becomes 'other'

    #Only check if no citation_type value is passed
    if citation_type is None:
        #Get the citation-type attribute value
        if 'citation-type' in nlm_citation.attrib:
            citation_type = nlm_citation.attrib['citation-type']

    #Pass the citation to the appropriate function and return result
    return cite_types[citation_type](citation)

    @staticmethod
    def format_book_citation(self, citation):
        """
        citation-type=\"book\"
        """

        #Get the count of authors
        author_group_count = int(citation.xpath('count(person-group) + count(collab)'))
        #Detect if there are non-authors
        if citation.xpath('person-group[@person-group-type!=\'author\']'):
            non_authors = True
        else:
            non_authors= False
        #Detect article-title
        if citation.xpath('article-title'):
            article_title = True
        else:
            article_title = False
        #Find out if there is at least one author or compiler
        auth_or_comp = False
        for person_group in citation.findall('person-group'):
            if 'person-group-type' in person_group.attrib:
                if person_group.attrib['person-group-type'] in ['author', 'compiler']:
                    auth_or_comp = True
                    break

        #These pieces of information allow us to provide two special use cases
        #and one general use case.
        #First special case:
        if author_group_count > 0 and non_authors and article_title:
            pass

        #Second special case
        elif auth_or_comp:
            pass

        #General case
        else:
            pass

    @staticmethod
    def format_commun_citation(self, citation):
        """
        citation-type=\"commun\"
        """

    @staticmethod
    def format_confproc_citation(self, citation):
        """
        citation-type=\"confproc\"
        """

    @staticmethod
    def format_discussion_citation(self, citation):
        """
        citation-type=\"discussion\"
        """

    @staticmethod
    def format_gov_citation(self, citation):
        """
        citation-type=\"gov\"
        """

    @staticmethod
    def format_journal_citation(self, citation):
        """
        citation-type=\"journal\"
        """

    @staticmethod
    def format_list_citation(self, citation):
        """
        citation-type=\"list\"
        """
    @staticmethod
    def format_other_citation(self, citation):
        """
        citation-type=\"other\"
        """

    @staticmethod
    def format_patent_citation(self, citation):
        """
        citation-type=\"patent\"
        """

    @staticmethod
    def format_thesis_citation(self, citation):
        """
        citation-type=\"thesis\"
        """
        #Treat the same as "book"
        return format_book_citation(citation)

    @staticmethod
    def format_web_citation(self, citation):
        """
        citation-type=\"web\"
        """

Response: This method formats the given citation into a single - level citation list.


Instruction:
Can you generate the documentation for the following Python 3 function
def plos_doi_to_xmlurl(doi_string):
    #Create URL to request DOI resolution from http://dx.doi.org
    doi_url = 'http://dx.doi.org/{0}'.format(doi_string)
    log.debug('DOI URL: {0}'.format(doi_url))
    #Open the page, follow the redirect
    try:
        resolved_page = urllib.request.urlopen(doi_url)
    except urllib.error.URLError as err:
        print('Unable to resolve DOI URL, or could not connect')
        raise err
    else:
        #Given the redirection, attempt to shape new request for PLoS servers
        resolved_address = resolved_page.geturl()
        log.debug('DOI resolved to {0}'.format(resolved_address))
        parsed = urllib.parse.urlparse(resolved_address)
        xml_url = '{0}://{1}'.format(parsed.scheme, parsed.netloc)
        xml_url += '/article/fetchObjectAttachment.action?uri='
        xml_path = parsed.path.replace(':', '%3A').replace('/', '%2F')
        xml_path = xml_path.split('article%2F')[1]
        xml_url += '{0}{1}'.format(xml_path, '&representation=XML')
        log.debug('Shaped PLoS request for XML {0}'.format(xml_url))
        #Return this url to the calling function
        return xml_url

Response: Given a PLoS DOI into a URL path to the XML file.