Instruction:
Make a summary of the following Python 3 code
def _read_file_header(self):
        header = self.fileobj.readline()
        payload1 = self.fileobj.readline()
        payload2 = self.fileobj.readline()
        version, reserved, organisation = payload1.split(None, 2)
        self.fileobj.readline() # Lose the separator newline
        self.header_read = True
        # print "--------------------------------------------------"
        # print header,"\n", payload1, "\n", payload2,"\n"
        # print "--------------------------------------------------"
        if self.version and int(self.version) != version:
            raise IOError("Version mismatch. Requested version was '%s' but version in file was '%s'"%(self.version, version))

        if version == '1':
            url, ip_address, date, content_type, length = header.split()
            self.file_headers = {"ip_address" : ip_address,
                                 "date" : datetime.datetime.strptime(date, "%Y%m%d%H%M%S"),
                                 "org" : organisation}
            self.version = 1
        elif version == '2':
            url, ip_address, date, content_type, result_code, checksum, location, offset, filename, length  = header.split()
            self.file_headers = {"ip_address" : ip_address,
                                 "date" : datetime.datetime.strptime(date, "%Y%m%d%H%M%S"),
                                 "org" : organisation}
            self.version = 2
        else:
            raise IOError("Unknown ARC version '%s'"%version)

Response: Reads out the file header for the ARC file.


Instruction:
Implement a function in Python 3 to
read out an arc record formats it and returns it

Response:
def _read_arc_record(self):
        "Reads out an arc record, formats it and returns it"
        #XXX:Noufal Stream payload here rather than just read it
        # r = self.fileobj.readline() # Drop the initial newline
        # if r == "":
        #     return None
        # header = self.fileobj.readline()

        # Strip the initial new lines and read first line
        header = self.fileobj.readline()
        while header and header.strip() == "":
            header = self.fileobj.readline()

        if header == "":
            return None

        if int(self.version) == 1:
            arc_header_re = ARC1_HEADER_RE
        elif int(self.version) == 2:
            arc_header_re = ARC2_HEADER_RE

        matches = arc_header_re.search(header)
        headers = matches.groupdict()
        arc_header = ARCHeader(**headers)

        payload = self.fileobj.read(int(headers['length']))

        self.fileobj.readline() # Munge the separator newline.

        return ARCRecord(header = arc_header, payload = payload)


Instruction:
Can you write a function in Python 3 where it
creates a new BIC object from country - and bank - code.

Response:
def from_bank_code(cls, country_code, bank_code):
        """Create a new BIC object from country- and bank-code.

        Examples:
            >>> bic = BIC.from_bank_code('DE', '20070000')
            >>> bic.country_code
            'DE'
            >>> bic.bank_code
            'DEUT'
            >>> bic.location_code
            'HH'

            >>> BIC.from_bank_code('DE', '01010101')
            Traceback (most recent call last):
            ...
            ValueError: Invalid bank code '01010101' for country 'DE'

        Args:
            country_code (str): ISO 3166 alpha2 country-code.
            bank_code (str): Country specific bank-code.

        Returns:
            BIC: a BIC object generated from the given country code and bank code.

        Raises:
            ValueError: If the given bank code wasn't found in the registry

        Note:
            This currently only works for German bank-codes.
        """
        try:
            return cls(registry.get('bank_code')[(country_code, bank_code)]['bic'])
        except KeyError:
            raise ValueError("Invalid bank code {!r} for country {!r}".format(bank_code,
                                                                              country_code))


Instruction:
Can you tell what is the following Python 3 function doing
def generate(cls, country_code, bank_code, account_code):
        spec = _get_iban_spec(country_code)
        bank_code_length = code_length(spec, 'bank_code')
        branch_code_length = code_length(spec, 'branch_code')
        bank_and_branch_code_length = bank_code_length + branch_code_length
        account_code_length = code_length(spec, 'account_code')

        if len(bank_code) > bank_and_branch_code_length:
            raise ValueError(
                "Bank code exceeds maximum size {}".format(bank_and_branch_code_length))

        if len(account_code) > account_code_length:
            raise ValueError(
                "Account code exceeds maximum size {}".format(account_code_length))

        bank_code = bank_code.rjust(bank_and_branch_code_length, '0')
        account_code = account_code.rjust(account_code_length, '0')
        iban = country_code + '??' + bank_code + account_code
        return cls(iban)

Response: Generate an IBAN from it s components.


Instruction:
Make a summary of the following Python 3 code
def formatted(self):
        return ' '.join(self.compact[i:i + 4] for i in range(0, len(self.compact), 4))

Response: str : The IBAN formatted in blocks of 4 digits.


Instruction:
Can you implement a function in Python 3 that
converts tdms datasets to the hdf5 - based. rtdc file format

Response:
def tdms2rtdc():
    """Convert .tdms datasets to the hdf5-based .rtdc file format"""
    parser = tdms2rtdc_parser()
    args = parser.parse_args()

    path_tdms = pathlib.Path(args.tdms_path).resolve()
    path_rtdc = pathlib.Path(args.rtdc_path)

    # Determine whether input path is a tdms file or a directory
    if path_tdms.is_dir():
        files_tdms = fmt_tdms.get_tdms_files(path_tdms)
        if path_rtdc.is_file():
            raise ValueError("rtdc_path is a file: {}".format(path_rtdc))
        files_rtdc = []
        for ff in files_tdms:
            ff = pathlib.Path(ff)
            rp = ff.relative_to(path_tdms)
            # determine output file name (same relative path)
            rpr = path_rtdc / rp.with_suffix(".rtdc")
            files_rtdc.append(rpr)
    else:
        files_tdms = [path_tdms]
        files_rtdc = [path_rtdc]

    for ii in range(len(files_tdms)):
        ff = pathlib.Path(files_tdms[ii])
        fr = pathlib.Path(files_rtdc[ii])

        print_info("Converting {:d}/{:d}: {}".format(
            ii + 1, len(files_tdms), ff))
        # load dataset
        ds = load.load_file(ff)
        # create directory
        if not fr.parent.exists():
            fr.parent.mkdir(parents=True)
        # determine features to export
        features = []
        if args.compute_features:
            tocomp = dfn.feature_names
        else:
            tocomp = ds._events
        for feat in tocomp:
            if feat not in dfn.scalar_feature_names:
                if not ds[feat]:
                    # ignore non-existent contour, image, mask, or trace
                    continue
            elif feat not in ds:
                # ignore non-existent feature
                continue
            features.append(feat)
        # export as hdf5
        ds.export.hdf5(path=fr,
                       features=features,
                       filtered=False,
                       override=True)


Instruction:
Can you generate the documentation for the following Python 3 function
def verify_dataset():
    parser = verify_dataset_parser()
    args = parser.parse_args()
    path_in = pathlib.Path(args.path).resolve()
    viol, aler, info = load.check_dataset(path_in)
    print_info("Checking {}".format(path_in))
    for inf in info:
        print_info(inf)
    for ale in aler:
        print_alert(ale)
    for vio in viol:
        print_violation(vio)
    print_info("Check Complete: {} violations and {} alerts".format(len(viol),
                                                                    len(aler)))

Response: Perform checks on experimental datasets


Instruction:
Create a Python 3 function for
loading the configuration from a file.

Response:
def load_from_file(cfg_file):
    """Load the configuration from a file

    Parameters
    ----------
    cfg_file: str
        Path to configuration file

    Returns
    -------
    cfg : CaseInsensitiveDict
        Dictionary with configuration parameters
    """
    path = pathlib.Path(cfg_file).resolve()
    with path.open('r') as f:
        code = f.readlines()

    cfg = CaseInsensitiveDict()
    for line in code:
        # We deal with comments and empty lines
        # We need to check line length first and then we look for
        # a hash.
        line = line.split("#")[0].strip()
        if len(line) != 0:
            if line.startswith("[") and line.endswith("]"):
                section = line[1:-1].lower()
                if section not in cfg:
                    cfg[section] = CaseInsensitiveDict()
                continue
            var, val = line.split("=", 1)
            var = var.strip().lower()
            val = val.strip("' ").strip('" ').strip()
            # convert parameter value to correct type
            if (section in dfn.config_funcs and
                    var in dfn.config_funcs[section]):
                # standard parameter with known type
                val = dfn.config_funcs[section][var](val)
            else:
                # unknown parameter (e.g. plotting in Shape-Out), guess type
                var, val = keyval_str2typ(var, val)
            if len(var) != 0 and len(str(val)) != 0:
                cfg[section][var] = val
    return cfg


Instruction:
Write a Python 3 script to
convert a variable from a string to its correct type

Response:
def keyval_str2typ(var, val):
    """Convert a variable from a string to its correct type

    Parameters
    ----------
    var: str
        The variable name
    val: str
        The value of the variable represented as a string

    Returns
    -------
    varout: str
        Stripped lowercase `var`
    valout: any type
        The value converted from string to its presumed type

    Notes
    -----
    This method is heuristic and is only intended for usage in
    dclab.

    See Also
    --------
    keyval_typ2str: the opposite
    """
    if not (isinstance(val, str_types)):
        # already a type:
        return var.strip(), val
    var = var.strip().lower()
    val = val.strip()
    # Find values
    if len(var) != 0 and len(val) != 0:
        # check for float
        if val.startswith("[") and val.endswith("]"):
            if len(val.strip("[],")) == 0:
                # empty list
                values = []
            else:
                values = val.strip("[],").split(",")
                values = [float(v) for v in values]
            return var, values
        elif val.lower() in ["true", "y"]:
            return var, True
        elif val.lower() in ["false", "n"]:
            return var, False
        elif val[0] in ["'", '"'] and val[-1] in ["'", '"']:
            return var, val.strip("'").strip('"').strip()
        elif val in dfn.scalar_feature_names:
            return var, val
        else:
            try:
                return var, float(val.replace(",", "."))
            except ValueError:
                return var, val


Instruction:
Can you create a Python 3 function that
converts a variable to a string representation of a set of key - value types

Response:
def keyval_typ2str(var, val):
    """Convert a variable to a string

    Parameters
    ----------
    var: str
        The variable name
    val: any type
        The value of the variable

    Returns
    -------
    varout: str
        Stripped lowercase `var`
    valout: any type
        The value converted to a useful string representation

    See Also
    --------
    keyval_str2typ: the opposite
    """
    varout = var.strip()
    if isinstance(val, list):
        data = ", ".join([keyval_typ2str(var, it)[1] for it in val])
        valout = "["+data+"]"
    elif isinstance(val, float):
        valout = "{:.12f}".format(val)
    else:
        valout = "{}".format(val)
    return varout, valout


Instruction:
Can you tell what is the following Python 3 function doing
def _init_default_values(self):
        # Do not filter out invalid event values
        self["filtering"]["remove invalid events"] = False
        # Enable filters switch is mandatory
        self["filtering"]["enable filters"] = True
        # Limit events integer to downsample output data
        self["filtering"]["limit events"] = 0
        # Polygon filter list
        self["filtering"]["polygon filters"] = []
        # Defaults to no hierarchy parent
        self["filtering"]["hierarchy parent"] = "none"
        # Check for missing min/max values and set them to zero
        for item in dfn.scalar_feature_names:
            appends = [" min", " max"]
            for a in appends:
                self["filtering"][item + a] = 0

Response: Set default values for the internal structures.


Instruction:
Explain what the following Python 3 code does
def save(self, filename):
        filename = pathlib.Path(filename)
        out = []
        keys = sorted(list(self.keys()))
        for key in keys:
            out.append("[{}]".format(key))
            section = self[key]
            ikeys = list(section.keys())
            ikeys.sort()
            for ikey in ikeys:
                var, val = keyval_typ2str(ikey, section[ikey])
                out.append("{} = {}".format(var, val))
            out.append("")

        with filename.open("w") as f:
            for i in range(len(out)):
                # win-like line endings
                out[i] = out[i]+"\n"
            f.writelines(out)

Response: Save the configuration to a file


Instruction:
Make a summary of the following Python 3 code
def update(self, newcfg):
        for key in newcfg.keys():
            if key not in self._cfg:
                self._cfg[key] = CaseInsensitiveDict()
            for skey in newcfg[key]:
                self._cfg[key][skey] = newcfg[key][skey]

Response: Update the current config with a dictionary


Instruction:
Explain what the following Python 3 code does
def convert(area_um, deform, emodulus,
            channel_width_in, channel_width_out,
            flow_rate_in, flow_rate_out,
            viscosity_in, viscosity_out,
            inplace=False):
    """convert area-deformation-emodulus triplet

    The conversion formula is described in :cite:`Mietke2015`.

    Parameters
    ----------
    area_um: ndarray
        Convex cell area [µm²]
    deform: ndarray
        Deformation
    emodulus: ndarray
        Young's Modulus [kPa]
    channel_width_in: float
        Original channel width [µm]
    channel_width_out: float
        Target channel width [µm]
    flow_rate_in: float
        Original flow rate [µl/s]
    flow_rate_in: float
        Target flow rate [µl/s]
    viscosity_in: float
        Original viscosity [mPa*s]
    viscosity_out: float
        Target viscosity [mPa*s]
    inplace: bool
        If True, override input arrays with corrected data

    Returns
    -------
    area_um_corr: ndarray
        Corrected cell area [µm²]
    deform_corr: ndarray
        Deformation (a copy if `inplace` is False)
    emodulus_corr: ndarray
        Corrected emodulus [kPa]
    """
    copy = not inplace
    # make sure area_um_corr is not an integer array
    area_um_corr = np.array(area_um, dtype=float, copy=copy)
    deform_corr = np.array(deform, copy=copy)
    emodulus_corr = np.array(emodulus, copy=copy)

    if channel_width_in != channel_width_out:
        area_um_corr *= (channel_width_out / channel_width_in)**2

    if (flow_rate_in != flow_rate_out or
            viscosity_in != viscosity_out or
            channel_width_in != channel_width_out):
        emodulus_corr *= (flow_rate_out / flow_rate_in) \
            * (viscosity_out / viscosity_in) \
            * (channel_width_in / channel_width_out)**3

    return area_um_corr, deform_corr, emodulus_corr

Response: convert area - deformation - emodulus triplet


Instruction:
Can you create a Python 3 function that
computes the apparent Young s modulus using a look - up table.

Response:
def get_emodulus(area_um, deform, medium="CellCarrier",
                 channel_width=20.0, flow_rate=0.16, px_um=0.34,
                 temperature=23.0, copy=True):
    """Compute apparent Young's modulus using a look-up table

    Parameters
    ----------
    area_um: float or ndarray
        Apparent (2D image) area [µm²] of the event(s)
    deform: float or ndarray
        The deformation (1-circularity) of the event(s)
    medium: str or float
        The medium to compute the viscosity for. If a string
        in ["CellCarrier", "CellCarrier B"] is given, the viscosity
        will be computed. If a float is given, this value will be
        used as the viscosity in mPa*s.
    channel_width: float
        The channel width [µm]
    flow_rate: float
        Flow rate [µl/s]
    px_um: float
        The detector pixel size [µm] used for pixelation correction.
        Set to zero to disable.
    temperature: float or ndarray
        Temperature [°C] of the event(s)
    copy: bool
        Copy input arrays. If set to false, input arrays are
        overridden.

    Returns
    -------
    elasticity: float or ndarray
        Apparent Young's modulus in kPa

    Notes
    -----
    - The look-up table used was computed with finite elements methods
      according to :cite:`Mokbel2017`.
    - The computation of the Young's modulus takes into account
      corrections for the viscosity (medium, channel width, flow rate,
      and temperature) :cite:`Mietke2015` and corrections for
      pixelation of the area and the deformation which are computed
      from a (pixelated) image :cite:`Herold2017`.

    See Also
    --------
    dclab.features.emodulus_viscosity.get_viscosity: compute viscosity
        for known media
    """
    # copy input arrays so we can use in-place calculations
    deform = np.array(deform, copy=copy, dtype=float)
    area_um = np.array(area_um, copy=copy, dtype=float)
    # Get lut data
    lut_path = resource_filename("dclab.features", "emodulus_lut.txt")
    with pathlib.Path(lut_path).open("rb") as lufd:
        lut = np.loadtxt(lufd)
    # These meta data are the simulation parameters of the lut
    lut_channel_width = 20.0
    lut_flow_rate = 0.04
    lut_visco = 15.0
    # Compute viscosity
    if isinstance(medium, (float, int)):
        visco = medium
    else:
        visco = get_viscosity(medium=medium, channel_width=channel_width,
                              flow_rate=flow_rate, temperature=temperature)
    # Corrections
    # We correct the lut, because it contains less points than
    # the event data. Furthermore, the lut could be cached
    # in the future, if this takes up a lot of time.
    convert(area_um=lut[:, 0],
            deform=lut[:, 1],
            emodulus=lut[:, 2],
            channel_width_in=lut_channel_width,
            channel_width_out=channel_width,
            flow_rate_in=lut_flow_rate,
            flow_rate_out=flow_rate,
            viscosity_in=lut_visco,
            viscosity_out=visco,
            inplace=True)

    if px_um:
        # Correct deformation for pixelation effect (subtract ddelt).
        ddelt = corrpix_deform_delta(area_um=area_um, px_um=px_um)
        deform -= ddelt

    # Normalize interpolation data such that the spacing for
    # area and deformation is about the same during interpolation.
    area_norm = lut[:, 0].max()
    normalize(lut[:, 0], area_norm)
    normalize(area_um, area_norm)

    defo_norm = lut[:, 1].max()
    normalize(lut[:, 1], defo_norm)
    normalize(deform, defo_norm)

    # Perform interpolation
    emod = spint.griddata((lut[:, 0], lut[:, 1]), lut[:, 2],
                          (area_um, deform),
                          method='linear')
    return emod


Instruction:
Can you create a Python 3 function that
creates an exception for a given method and RpcError.

Response:
def make_exception(method, e):
    """Creates an exception for a given method, and RpcError."""
    x = e.details()
    name = x[:x.find(':')].split('.')[-1]
    if name in globals():
        cls = globals()[name]
    else:
        cls = UnknownRpcException  # noqa

    return cls(method, e.code(), e.details())


Instruction:
Can you tell what is the following Python 3 function doing
def text_to_int(text, default_base="hex"):
    if text.startswith("0x"):
        value = int(text[2:], 16)
    elif text.startswith("$"):
        value = int(text[1:], 16)
    elif text.startswith("#"):
        value = int(text[1:], 10)
    elif text.startswith("%"):
        value = int(text[1:], 2)
    else:
        if default_base == "dec":
            value = int(text)
        else:
            value = int(text, 16)
    return value

Response: Convert text to int


Instruction:
Can you generate the documentation for the following Python 3 function
def assign_sector_numbers(self, dirent, sector_list):
        num = len(sector_list)
        order = self.reserve_space(num)
        if len(order) != num:
            raise errors.InvalidFile("VTOC reserved space for %d sectors. Sectors needed: %d" % (len(order), num))
        file_length = 0
        last_sector = None
        for sector, sector_num in zip(sector_list.sectors, order):
            sector.sector_num = sector_num
            sector.file_num = dirent.file_num
            file_length += sector.used
            if last_sector is not None:
                last_sector.next_sector_num = sector_num
            last_sector = sector
        if last_sector is not None:
            last_sector.next_sector_num = 0
        sector_list.file_length = file_length

Response: Map out the sectors and link them together.


Instruction:
Write a Python 3 function that can
content - based downsampling for faster visualization of a 2D scatter plot.

Response:
def downsample_grid(a, b, samples, ret_idx=False):
    """Content-based downsampling for faster visualization

    The arrays `a` and `b` make up a 2D scatter plot with high
    and low density values. This method takes out points at
    indices with high density.

    Parameters
    ----------
    a, b: 1d ndarrays
        The input arrays to downsample
    samples: int
        The desired number of samples
    remove_invalid: bool
        Remove nan and inf values before downsampling
    ret_idx: bool
        Also return a boolean array that corresponds to the
        downsampled indices in `a` and `b`.

    Returns
    -------
    dsa, dsb: 1d ndarrays of shape (samples,)
        The arrays `a` and `b` downsampled by evenly selecting
        points and pseudo-randomly adding or removing points
        to match `samples`.
    idx: 1d boolean array with same shape as `a`
        Only returned if `ret_idx` is True.
        A boolean array such that `a[idx] == dsa`
    """
    # fixed random state for this method
    rs = np.random.RandomState(seed=47).get_state()

    samples = int(samples)

    if samples and samples < a.size:
        # The events to keep
        keep = np.zeros_like(a, dtype=bool)

        # 1. Produce evenly distributed samples
        # Choosing grid-size:
        # - large numbers tend to show actual structures of the sample,
        #   which is not desired for plotting
        # - small numbers tend will not result in too few samples and,
        #   in order to reach the desired samples, the data must be
        #   upsampled again.
        # 300 is about the size of the plot in marker sizes and yields
        # good results.
        grid_size = 300
        xpx = norm(a, a, b) * grid_size
        ypx = norm(b, b, a) * grid_size
        # The events on the grid to process
        toproc = np.ones((grid_size, grid_size), dtype=bool)

        for ii in range(xpx.size):
            xi = xpx[ii]
            yi = ypx[ii]
            # filter for overlapping events
            if valid(xi, yi) and toproc[int(xi-1), int(yi-1)]:
                toproc[int(xi-1), int(yi-1)] = False
                # include event
                keep[ii] = True

        # 2. Make sure that we reach `samples` by adding or
        # removing events.
        diff = np.sum(keep) - samples
        if diff > 0:
            # Too many samples
            rem_indices = np.where(keep)[0]
            np.random.set_state(rs)
            rem = np.random.choice(rem_indices,
                                   size=diff,
                                   replace=False)
            keep[rem] = False
        elif diff < 0:
            # Not enough samples
            add_indices = np.where(~keep)[0]
            np.random.set_state(rs)
            add = np.random.choice(add_indices,
                                   size=abs(diff),
                                   replace=False)
            keep[add] = True

        assert np.sum(keep) == samples, "sanity check"
        asd = a[keep]
        bsd = b[keep]
        assert np.allclose(a[keep], asd, equal_nan=True), "sanity check"
        assert np.allclose(b[keep], bsd, equal_nan=True), "sanity check"
    else:
        keep = np.ones_like(a, dtype=bool)
        asd = a
        bsd = b

    if ret_idx:
        return asd, bsd, keep
    else:
        return asd, bsd


Instruction:
Make a summary of the following Python 3 code
def valid(a, b):
    return ~(np.isnan(a) | np.isinf(a) | np.isnan(b) | np.isinf(b))

Response: Check whether a and b are not inf or nan


Instruction:
Can you generate a brief explanation for the following Python 3 code
def norm(a, ref1, ref2):
    ref = ref1[valid(ref1, ref2)]
    return (a-ref.min())/(ref.max()-ref.min())

Response: Normalize a with min / max values of ref1 using all elements of ref1 where the ref1 and ref2 are not nan inf


Instruction:
Given the following Python 3 function, write the documentation
def gaussian(h, Xi, x):
    return (1. / np.sqrt(2 * np.pi)) * np.exp(-(Xi - x)**2 / (h**2 * 2.))

Response: Returns the kernel function for continuous variables.


Instruction:
Can you implement a function in Python 3 that
parses the RT - DC configuration of an hdf5 file

Response:
def parse_config(h5path):
        """Parse the RT-DC configuration of an hdf5 file"""
        with h5py.File(h5path, mode="r") as fh5:
            h5attrs = dict(fh5.attrs)

        # Convert byte strings to unicode strings
        # https://github.com/h5py/h5py/issues/379
        for key in h5attrs:
            if isinstance(h5attrs[key], bytes):
                h5attrs[key] = h5attrs[key].decode("utf-8")

        config = Configuration()
        for key in h5attrs:
            section, pname = key.split(":")
            if pname not in dfn.config_funcs[section]:
                # Add the value as a string but issue a warning
                config[section][pname] = h5attrs[key]
                msg = "Unknown key '{}' in section [{}]!".format(
                    pname, section)
                warnings.warn(msg, UnknownKeyWarning)
            else:
                typ = dfn.config_funcs[section][pname]
                config[section][pname] = typ(h5attrs[key])
        return config


Instruction:
Can you create a Python 3 function that
hashes value based on file name and content

Response:
def hash(self):
        """Hash value based on file name and content"""
        if self._hash is None:
            tohash = [self.path.name]
            # Hash a maximum of ~1MB of the hdf5 file
            tohash.append(hashfile(self.path, blocksize=65536, count=20))
            self._hash = hashobj(tohash)
        return self._hash


Instruction:
How would you code a function in Python 3 to
compute number of bins based on Doane s formula

Response:
def bin_num_doane(a):
    """Compute number of bins based on Doane's formula"""
    bad = np.isnan(a) | np.isinf(a)
    data = a[~bad]
    acc = bin_width_doane(a)
    num = np.int(np.round((data.max() - data.min()) / acc))
    return num


Instruction:
Write a Python 3 function that can
compute accuracy of a Doane s formula

Response:
def bin_width_doane(a):
    """Compute accuracy (bin width) based on Doane's formula

    References
    ----------
    - `<https://en.wikipedia.org/wiki/Histogram#Number_of_bins_and_width>`_
    - `<https://stats.stackexchange.com/questions/55134/
      doanes-formula-for-histogram-binning>`_
    """
    bad = np.isnan(a) | np.isinf(a)
    data = a[~bad]
    n = data.size
    g1 = skew(data)
    sigma_g1 = np.sqrt(6 * (n - 2) / ((n + 1) * (n + 3)))
    k = 1 + np.log2(n) + np.log2(1 + np.abs(g1) / sigma_g1)
    acc = (data.max() - data.min()) / k
    return acc


Instruction:
Explain what the following Python 3 code does
def ignore_nan_inf(kde_method):
    def new_kde_method(events_x, events_y, xout=None, yout=None,
                       *args, **kwargs):
        bad_in = get_bad_vals(events_x, events_y)
        if xout is None:
            density = np.zeros_like(events_x, dtype=float)
            bad_out = bad_in
            xo = yo = None
        else:
            density = np.zeros_like(xout, dtype=float)
            bad_out = get_bad_vals(xout, yout)
            xo = xout[~bad_out]
            yo = yout[~bad_out]
        # Filter events
        ev_x = events_x[~bad_in]
        ev_y = events_y[~bad_in]
        density[~bad_out] = kde_method(ev_x, ev_y,
                                       xo, yo,
                                       *args, **kwargs)
        density[bad_out] = np.nan
        return density

    doc_add = "\n    Notes\n" +\
              "    -----\n" +\
              "    This is a wrapped version that ignores nan and inf values."
    new_kde_method.__doc__ = kde_method.__doc__ + doc_add

    return new_kde_method

Response: Returns a new KDE method that ignores nans and infs from the input data.


Instruction:
Given the following Python 3 function, write the documentation
def kde_gauss(events_x, events_y, xout=None, yout=None):
    valid_combi = ((xout is None and yout is None) or
                   (xout is not None and yout is not None)
                   )
    if not valid_combi:
        raise ValueError("Both `xout` and `yout` must be (un)set.")

    if yout is None and yout is None:
        xout = events_x
        yout = events_y

    try:
        estimator = gaussian_kde([events_x.flatten(), events_y.flatten()])
        density = estimator.evaluate([xout.flatten(), yout.flatten()])
    except np.linalg.LinAlgError:
        # LinAlgError occurs when matrix to solve is singular (issue #117)
        density = np.zeros(xout.shape)*np.nan
    return density.reshape(xout.shape)

Response: Compute the kernel density for the given set of points.


Instruction:
Make a summary of the following Python 3 code
def kde_histogram(events_x, events_y, xout=None, yout=None, bins=None):
    valid_combi = ((xout is None and yout is None) or
                   (xout is not None and yout is not None)
                   )
    if not valid_combi:
        raise ValueError("Both `xout` and `yout` must be (un)set.")

    if yout is None and yout is None:
        xout = events_x
        yout = events_y

    if bins is None:
        bins = (max(5, bin_num_doane(events_x)),
                max(5, bin_num_doane(events_y)))

    # Compute the histogram
    hist2d, xedges, yedges = np.histogram2d(x=events_x,
                                            y=events_y,
                                            bins=bins,
                                            normed=True)
    xip = xedges[1:]-(xedges[1]-xedges[0])/2
    yip = yedges[1:]-(yedges[1]-yedges[0])/2

    estimator = RectBivariateSpline(x=xip, y=yip, z=hist2d)
    density = estimator.ev(xout, yout)
    density[density < 0] = 0

    return density.reshape(xout.shape)

Response: Compute the KDE for the given set of events.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def kde_none(events_x, events_y, xout=None, yout=None):
    valid_combi = ((xout is None and yout is None) or
                   (xout is not None and yout is not None)
                   )
    if not valid_combi:
        raise ValueError("Both `xout` and `yout` must be (un)set.")

    if yout is None and yout is None:
        xout = events_x
        yout = events_y

    return np.ones(xout.shape)

Response: No Kernel Density Estimation for the given set of points.


Instruction:
Can you generate the documentation for the following Python 3 function
def kde_multivariate(events_x, events_y, xout=None, yout=None, bw=None):
    valid_combi = ((xout is None and yout is None) or
                   (xout is not None and yout is not None)
                   )
    if not valid_combi:
        raise ValueError("Both `xout` and `yout` must be (un)set.")

    if yout is None and yout is None:
        xout = events_x
        yout = events_y
    if bw is None:
        # divide by 2 to make it comparable to histogram KDE
        bw = (bin_width_doane(events_x) / 2,
              bin_width_doane(events_y) / 2)

    positions = np.vstack([xout.flatten(), yout.flatten()])
    estimator_ly = KDEMultivariate(data=[events_x.flatten(),
                                         events_y.flatten()],
                                   var_type='cc', bw=bw)

    density = estimator_ly.pdf(positions)
    return density.reshape(xout.shape)

Response: Compute the multivariate kernel density estimate for the given set of points.


Instruction:
Create a Python 3 function for
redirecting from legacy content id to new content uuid@version.

Response:
def redirect_legacy_content(request):
    """Redirect from legacy /content/id/version to new /contents/uuid@version.

    Handles collection context (book) as well.
    """
    routing_args = request.matchdict
    objid = routing_args['objid']
    objver = routing_args.get('objver')
    filename = routing_args.get('filename')

    id, version = _convert_legacy_id(objid, objver)

    if not id:
        raise httpexceptions.HTTPNotFound()

    # We always use 301 redirects (HTTPMovedPermanently) here
    # because we want search engines to move to the newer links
    # We cache these redirects only briefly because, even when versioned,
    # legacy collection versions don't include the minor version,
    # so the latest archive url could change
    if filename:
        with db_connect() as db_connection:
            with db_connection.cursor() as cursor:
                args = dict(id=id, version=version, filename=filename)
                cursor.execute(SQL['get-resourceid-by-filename'], args)
                try:
                    res = cursor.fetchone()
                    resourceid = res[0]

                    raise httpexceptions.HTTPMovedPermanently(
                         request.route_path('resource', hash=resourceid,
                                            ignore=u'/{}'.format(filename)),
                         headers=[("Cache-Control", "max-age=60, public")])
                except TypeError:  # None returned
                    raise httpexceptions.HTTPNotFound()

    ident_hash = join_ident_hash(id, version)
    params = request.params
    if params.get('collection'):  # page in book
        objid, objver = split_legacy_hash(params['collection'])
        book_uuid, book_version = _convert_legacy_id(objid, objver)
        if book_uuid:
            id, ident_hash = \
                _get_page_in_book(id, version, book_uuid, book_version)

    raise httpexceptions.HTTPMovedPermanently(
        request.route_path('content', ident_hash=ident_hash),
        headers=[("Cache-Control", "max-age=60, public")])


Instruction:
Can you write a function in Python 3 where it
adds isoelastics to the internal list of store items.

Response:
def add(self, isoel, col1, col2, channel_width,
            flow_rate, viscosity, method):
        """Add isoelastics

        Parameters
        ----------
        isoel: list of ndarrays
            Each list item resembles one isoelastic line stored
            as an array of shape (N,3). The last column contains
            the emodulus data.
        col1: str
            Name of the first feature of all isoelastics
            (e.g. isoel[0][:,0])
        col2: str
            Name of the second feature of all isoelastics
            (e.g. isoel[0][:,1])
        channel_width: float
            Channel width in µm
        flow_rate: float
            Flow rate through the channel in µl/s
        viscosity: float
            Viscosity of the medium in mPa*s
        method: str
            The method used to compute the isoelastics
            (must be one of `VALID_METHODS`).

        Notes
        -----
        The following isoelastics are automatically added for
        user convenience:
        - isoelastics with `col1` and `col2` interchanged
        - isoelastics for circularity if deformation was given
        """
        if method not in VALID_METHODS:
            validstr = ",".join(VALID_METHODS)
            raise ValueError("`method` must be one of {}!".format(validstr))
        for col in [col1, col2]:
            if col not in dfn.scalar_feature_names:
                raise ValueError("Not a valid feature name: {}".format(col))

        meta = [channel_width, flow_rate, viscosity]

        # Add the feature data
        self._add(isoel, col1, col2, method, meta)

        # Also add the feature data for circularity
        if "deform" in [col1, col2]:
            col1c, col2c = col1, col2
            if col1c == "deform":
                deform_ax = 0
                col1c = "circ"
            else:
                deform_ax = 1
                col2c = "circ"
            iso_circ = []
            for iso in isoel:
                iso = iso.copy()
                iso[:, deform_ax] = 1 - iso[:, deform_ax]
                iso_circ.append(iso)
            self._add(iso_circ, col1c, col2c, method, meta)


Instruction:
Implement a Python 3 function for
adding pixelation error to the set of isoealsticity lines in the experimental data.

Response:
def add_px_err(isoel, col1, col2, px_um, inplace=False):
        """Undo pixelation correction

        Isoelasticity lines are already corrected for pixelation
        effects as described in

        Mapping of Deformation to Apparent Young's Modulus
        in Real-Time Deformability Cytometry
        Christoph Herold, arXiv:1704.00572 [cond-mat.soft] (2017)
        https://arxiv.org/abs/1704.00572.

        If the isoealsticity lines are displayed with deformation data
        that are not corrected, then the lines must be "un"-corrected,
        i.e. the pixelation error must be added to the lines to match
        the experimental data.

        Parameters
        ----------
        isoel: list of 2d ndarrays of shape (N, 3)
            Each item in the list corresponds to one isoelasticity
            line. The first column is defined by `col1`, the second
            by `col2`, and the third column is the emodulus.
        col1, col2: str
            Define the fist to columns of each isoelasticity line.
            One of ["area_um", "circ", "deform"]
        px_um: float
            Pixel size [µm]
        """
        Isoelastics.check_col12(col1, col2)
        if "deform" in [col1, col2]:
            # add error for deformation
            sign = +1
        else:
            # subtract error for circularity
            sign = -1
        if col1 == "area_um":
            area_ax = 0
            deci_ax = 1
        else:
            area_ax = 1
            deci_ax = 0

        new_isoel = []
        for iso in isoel:
            iso = np.array(iso, copy=not inplace)
            ddeci = feat_emod.corrpix_deform_delta(area_um=iso[:, area_ax],
                                                   px_um=px_um)
            iso[:, deci_ax] += sign * ddeci
            new_isoel.append(iso)
        return new_isoel


Instruction:
Here you have a function in Python 3, explain what it does
def convert(isoel, col1, col2,
                channel_width_in, channel_width_out,
                flow_rate_in, flow_rate_out,
                viscosity_in, viscosity_out,
                inplace=False):
        """Convert isoelastics in area_um-deform space

        Parameters
        ----------
        isoel: list of 2d ndarrays of shape (N, 3)
            Each item in the list corresponds to one isoelasticity
            line. The first column is defined by `col1`, the second
            by `col2`, and the third column is the emodulus.
        col1, col2: str
            Define the fist to columns of each isoelasticity line.
            One of ["area_um", "circ", "deform"]
        channel_width_in: float
            Original channel width [µm]
        channel_width_out: float
            Target channel width [µm]
        flow_rate_in: float
            Original flow rate [µl/s]
        flow_rate_in: float
            Target flow rate [µl/s]
        viscosity_in: float
            Original viscosity [mPa*s]
        viscosity_out: float
            Target viscosity [mPa*s]

        Notes
        -----
        If only the positions of the isoelastics are of interest and
        not the value of the elastic modulus, then it is sufficient
        to supply values for the channel width and set the values
        for flow rate and viscosity to a constant (e.g. 1).

        See Also
        --------
        dclab.features.emodulus.convert: conversion method used
        """
        Isoelastics.check_col12(col1, col2)
        if col1 == "area_um":
            area_ax = 0
            defo_ax = 1
        else:
            area_ax = 1
            defo_ax = 0

        new_isoel = []

        for iso in isoel:
            iso = np.array(iso, copy=not inplace)
            feat_emod.convert(area_um=iso[:, area_ax],
                              deform=iso[:, defo_ax],
                              emodulus=iso[:, 2],
                              channel_width_in=channel_width_in,
                              channel_width_out=channel_width_out,
                              flow_rate_in=flow_rate_in,
                              flow_rate_out=flow_rate_out,
                              viscosity_in=viscosity_in,
                              viscosity_out=viscosity_out,
                              inplace=True)
            new_isoel.append(iso)
        return new_isoel

Response: Convert isoelastics in area_um - circ - deform space to area_um - deform space.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, col1, col2, method, channel_width, flow_rate=None,
            viscosity=None, add_px_err=False, px_um=None):
        """Get isoelastics

        Parameters
        ----------
        col1: str
            Name of the first feature of all isoelastics
            (e.g. isoel[0][:,0])
        col2: str
            Name of the second feature of all isoelastics
            (e.g. isoel[0][:,1])
        method: str
            The method used to compute the isoelastics
            (must be one of `VALID_METHODS`).
        channel_width: float
            Channel width in µm
        flow_rate: float or `None`
            Flow rate through the channel in µl/s. If set to
            `None`, the flow rate of the imported data will
            be used (only do this if you do not need the
            correct values for elastic moduli).
        viscosity: float or `None`
            Viscosity of the medium in mPa*s. If set to
            `None`, the flow rate of the imported data will
            be used (only do this if you do not need the
            correct values for elastic moduli).
        add_px_err: bool
            If True, add pixelation errors according to
            C. Herold (2017), https://arxiv.org/abs/1704.00572
        px_um: float
            Pixel size [µm], used for pixelation error computation

        See Also
        --------
        dclab.features.emodulus.convert: conversion in-between
            channel sizes and viscosities
        dclab.features.emodulus.corrpix_deform_delta: pixelation
            error that is applied to the deformation data
        """
        if method not in VALID_METHODS:
            validstr = ",".join(VALID_METHODS)
            raise ValueError("`method` must be one of {}!".format(validstr))
        for col in [col1, col2]:
            if col not in dfn.scalar_feature_names:
                raise ValueError("Not a valid feature name: {}".format(col))

        if "isoelastics" not in self._data[method][col2][col1]:
            msg = "No isoelastics matching {}, {}, {}".format(col1, col2,
                                                              method)
            raise KeyError(msg)

        isoel = self._data[method][col1][col2]["isoelastics"]
        meta = self._data[method][col1][col2]["meta"]

        if flow_rate is None:
            flow_rate = meta[1]

        if viscosity is None:
            viscosity = meta[2]

        isoel_ret = self.convert(isoel, col1, col2,
                                 channel_width_in=meta[0],
                                 channel_width_out=channel_width,
                                 flow_rate_in=meta[1],
                                 flow_rate_out=flow_rate,
                                 viscosity_in=meta[2],
                                 viscosity_out=viscosity,
                                 inplace=False)

        if add_px_err:
            self.add_px_err(isoel=isoel_ret,
                            col1=col1,
                            col2=col2,
                            px_um=px_um,
                            inplace=True)

        return isoel_ret

Response: Get ISO elastics and isoelastics from the original data.


Instruction:
Write a Python 3 script for
loading isoelastics from a text file.

Response:
def load_data(self, path):
        """Load isoelastics from a text file

        The text file is loaded with `numpy.loadtxt` and must have
        three columns, representing the two data columns and the
        elastic modulus with units defined in `definitions.py`.
        The file header must have a section defining meta data of the
        content like so:

            # [...]
            #
            # - column 1: area_um
            # - column 2: deform
            # - column 3: emodulus
            # - channel width [um]: 20
            # - flow rate [ul/s]: 0.04
            # - viscosity [mPa*s]: 15
            # - method: analytical
            #
            # [...]

        Parameters
        ----------
        path: str
            Path to a isoelastics text file
        """
        path = pathlib.Path(path).resolve()
        # Get metadata
        meta = {}
        with path.open() as fd:
            while True:
                line = fd.readline().strip()
                if line.startswith("# - "):
                    line = line.strip("#- ")
                    var, val = line.split(":")
                    if val.strip().replace(".", "").isdigit():
                        # channel width, flow rate, viscosity
                        val = float(val)
                    else:
                        # columns, calculation
                        val = val.strip().lower()
                    meta[var.strip()] = val
                elif line and not line.startswith("#"):
                    break

        assert meta["column 1"] in dfn.scalar_feature_names
        assert meta["column 2"] in dfn.scalar_feature_names
        assert meta["column 3"] == "emodulus"
        assert meta["method"] in VALID_METHODS

        # Load isoelasics
        with path.open("rb") as isfd:
            isodata = np.loadtxt(isfd)

        # Slice out individual isoelastics
        emoduli = np.unique(isodata[:, 2])
        isoel = []
        for emod in emoduli:
            where = isodata[:, 2] == emod
            isoel.append(isodata[where])

        # Add isoelastics to instance
        self.add(isoel=isoel,
                 col1=meta["column 1"],
                 col2=meta["column 2"],
                 channel_width=meta["channel width [um]"],
                 flow_rate=meta["flow rate [ul/s]"],
                 viscosity=meta["viscosity [mPa*s]"],
                 method=meta["method"])


Instruction:
Write a Python 3 script to
return a simple don t index me robots. txt file.

Response:
def robots(request):
    """Return a simple "don't index me" robots.txt file."""

    resp = request.response
    resp.status = '200 OK'
    resp.content_type = 'text/plain'
    resp.body = """
User-Agent: *
Disallow: /
"""
    return resp


Instruction:
Can you generate the documentation for the following Python 3 function
def get_compensation_matrix(ct21, ct31, ct12, ct32, ct13, ct23):
    ct11 = 1
    ct22 = 1
    ct33 = 1

    if ct21 < 0:
        raise ValueError("ct21 matrix element must not be negative!")

    if ct31 < 0:
        raise ValueError("ct31 matrix element must not be negative!")

    if ct12 < 0:
        raise ValueError("ct12 matrix element must not be negative!")

    if ct32 < 0:
        raise ValueError("ct32 matrix element must not be negative!")

    if ct13 < 0:
        raise ValueError("ct13 matrix element must not be negative!")

    if ct23 < 0:
        raise ValueError("ct23 matrix element must not be negative!")

    crosstalk = np.array([[ct11, ct12, ct13],
                          [ct21, ct22, ct23],
                          [ct31, ct32, ct33],
                          ])
    return np.linalg.inv(crosstalk)

Response: Compute the crosstalk inversion matrix for a given set of channel elements.


Instruction:
Explain what the following Python 3 code does
def correct_crosstalk(fl1, fl2, fl3, fl_channel,
                      ct21=0, ct31=0, ct12=0, ct32=0, ct13=0, ct23=0):
    """Perform crosstalk correction

    Parameters
    ----------
    fli: int, float, or np.ndarray
        Measured fluorescence signals
    fl_channel: int (1, 2, or 3)
        The channel number for which the crosstalk-corrected signal
        should be computed
    cij: float
        Spill (crosstalk or bleed-through) from channel i to channel j
        This spill is computed from the fluorescence signal of e.g.
        single-stained positive control cells; It is defined by the
        ratio of the fluorescence signals of the two channels, i.e
        cij = flj / fli.

    See Also
    --------
    get_compensation_matrix: compute the inverse crosstalk matrix

    Notes
    -----
    If there are only two channels (e.g. fl1 and fl2), then the
    crosstalk to and from the other channel (ct31, ct32, ct13, ct23)
    should be set to zero.
    """
    fl_channel = int(fl_channel)
    if fl_channel not in [1, 2, 3]:
        raise ValueError("`fl_channel` must be 1, 2, or 3!")

    minv = get_compensation_matrix(ct21=ct21, ct31=ct31, ct12=ct12,
                                   ct32=ct32, ct13=ct13, ct23=ct23)

    col = minv[:, fl_channel - 1].flatten()
    flout = col[0] * fl1 + col[1] * fl2 + col[2] * fl3
    return flout

Response: Perform crosstalk correction on fluorescence signals and return the correct crosstalk.


Instruction:
Can you tell what is the following Python 3 function doing
def cont_moments_cv(cont,
                    flt_epsilon=1.19209e-07,
                    dbl_epsilon=2.2204460492503131e-16):
    """Compute the moments of a contour

    The moments are computed in the same way as they are computed
    in OpenCV's `contourMoments` in `moments.cpp`.

    Parameters
    ----------
    cont: array of shape (N,2)
        The contour for which to compute the moments.
    flt_epsilon: float
        The value of ``FLT_EPSILON`` in OpenCV/gcc.
    dbl_epsilon: float
        The value of ``DBL_EPSILON`` in OpenCV/gcc.

    Returns
    -------
    moments: dict
        A dictionary of moments. If the moment `m00` is smaller
        than half of `flt_epsilon`, `None` is returned.
    """
    # Make sure we have no unsigned integers
    if np.issubdtype(cont.dtype, np.unsignedinteger):
        cont = cont.astype(np.int)

    xi = cont[:, 0]
    yi = cont[:, 1]

    xi_1 = np.roll(xi, -1)
    yi_1 = np.roll(yi, -1)

    xi_12 = xi_1**2
    yi_12 = yi_1**2

    xi2 = xi**2
    yi2 = yi**2

    dxy = xi_1 * yi - xi * yi_1

    xii_1 = xi_1 + xi
    yii_1 = yi_1 + yi

    a00 = np.sum(dxy)
    a10 = np.sum(dxy * xii_1)
    a01 = np.sum(dxy * yii_1)
    a20 = np.sum(dxy * (xi_1 * xii_1 + xi2))
    a11 = np.sum(dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi)))
    a02 = np.sum(dxy * (yi_1 * yii_1 + yi2))
    a30 = np.sum(dxy * xii_1 * (xi_12 + xi2))
    a03 = np.sum(dxy * yii_1 * (yi_12 + yi2))
    a21 = np.sum(dxy * (xi_12 * (3 * yi_1 + yi) + 2 *
                        xi * xi_1 * yii_1 + xi2 * (yi_1 + 3 * yi)))
    a12 = np.sum(dxy * (yi_12 * (3 * xi_1 + xi) + 2 *
                        yi * yi_1 * xii_1 + yi2 * (xi_1 + 3 * xi)))

    if abs(a00) > flt_epsilon:
        db1_2 = 0.5
        db1_6 = 0.16666666666666666666666666666667
        db1_12 = 0.083333333333333333333333333333333
        db1_24 = 0.041666666666666666666666666666667
        db1_20 = 0.05
        db1_60 = 0.016666666666666666666666666666667

        if a00 < 0:
            db1_2 *= -1
            db1_6 *= -1
            db1_12 *= -1
            db1_24 *= -1
            db1_20 *= -1
            db1_60 *= -1

        m = dict(m00=a00 * db1_2,
                 m10=a10 * db1_6,
                 m01=a01 * db1_6,
                 m20=a20 * db1_12,
                 m11=a11 * db1_24,
                 m02=a02 * db1_12,
                 m30=a30 * db1_20,
                 m21=a21 * db1_60,
                 m12=a12 * db1_60,
                 m03=a03 * db1_20,
                 )

        if m["m00"] > dbl_epsilon:
            # Center of gravity
            cx = m["m10"]/m["m00"]
            cy = m["m01"]/m["m00"]
        else:
            cx = 0
            cy = 0

        # central second order moments
        m["mu20"] = m["m20"] - m["m10"]*cx
        m["mu11"] = m["m11"] - m["m10"]*cy
        m["mu02"] = m["m02"] - m["m01"]*cy

        m["mu30"] = m["m30"] - cx*(3*m["mu20"] + cx*m["m10"])
        m["mu21"] = m["m21"] - cx*(2*m["mu11"] + cx*m["m01"]) - cy*m["mu20"]
        m["mu12"] = m["m12"] - cy*(2*m["mu11"] + cy*m["m10"]) - cx*m["mu02"]
        m["mu03"] = m["m03"] - cy*(3*m["mu02"] + cy*m["m01"])
        return m
    else:
        return None

Response: Compute the moments of a contour in OpenCV s ContourMoments. cpp file.


Instruction:
Explain what the following Python 3 code does
def get_inert_ratio_cvx(cont):
    if isinstance(cont, np.ndarray):
        # If cont is an array, it is not a list of contours,
        # because contours can have different lengths.
        cont = [cont]
        ret_list = False
    else:
        ret_list = True

    length = len(cont)

    inert_ratio_cvx = np.zeros(length, dtype=float) * np.nan

    for ii in range(length):
        try:
            chull = ssp.ConvexHull(cont[ii])
        except ssp.qhull.QhullError:
            pass
        else:
            hull = cont[ii][chull.vertices, :]
            inert_ratio_cvx[ii] = get_inert_ratio_raw(hull)

    if not ret_list:
        inert_ratio_cvx = inert_ratio_cvx[0]

    return inert_ratio_cvx

Response: Compute the inertia ratio of the convex hull of a contour.


Instruction:
Make a summary of the following Python 3 code
def get_inert_ratio_prnc(cont):
    if isinstance(cont, np.ndarray):
        # If cont is an array, it is not a list of contours,
        # because contours can have different lengths.
        cont = [cont]
        ret_list = False
    else:
        ret_list = True

    length = len(cont)

    inert_ratio_prnc = np.zeros(length, dtype=float) * np.nan

    for ii in range(length):
        moments = cont_moments_cv(cont[ii])
        if moments is not None:
            # orientation of the contour
            orient = 0.5 * np.arctan2(2 * moments['mu11'],
                                      moments['mu02'] - moments['mu20'])
            # require floating point array (only copy if necessary)
            cc = np.array(cont[ii], dtype=float, copy=False)
            # rotate contour
            rho = np.sqrt(cc[:, 0]**2 + cc[:, 1]**2)
            phi = np.arctan2(cc[:, 1], cc[:, 0]) + orient + np.pi / 2
            cc[:, 0] = rho * np.cos(phi)
            cc[:, 1] = rho * np.sin(phi)
            # compute inertia ratio of rotated contour
            mprnc = cont_moments_cv(cc)
            inert_ratio_prnc[ii] = np.sqrt(mprnc["mu20"] / mprnc["mu02"])

    if not ret_list:
        inert_ratio_prnc = inert_ratio_prnc[0]

    return inert_ratio_prnc

Response: Compute the principal inertia ratio of a contour.


Instruction:
Implement a function in Python 3 to
compute the inertia ratio of a contour.

Response:
def get_inert_ratio_raw(cont):
    """Compute the inertia ratio of a contour

    The inertia ratio is computed from the central second order of moments
    along x (mu20) and y (mu02) via `sqrt(mu20/mu02)`.

    Parameters
    ----------
    cont: ndarray or list of ndarrays of shape (N,2)
        A 2D array that holds the contour of an event (in pixels)
        e.g. obtained using `mm.contour` where  `mm` is an instance
        of `RTDCBase`. The first and second columns of `cont`
        correspond to the x- and y-coordinates of the contour.

    Returns
    -------
    inert_ratio_raw: float or ndarray of size N
        The inertia ratio of the contour

    Notes
    -----
    The contour moments mu20 and mu02 are computed the same way they
    are computed in OpenCV's `moments.cpp`.

    See Also
    --------
    get_inert_ratio_cvx: Compute inertia ratio of the convex hull of
                         a contour

    References
    ----------
    - `<https://en.wikipedia.org/wiki/Image_moment#Central_moments>`__
    - `<https://github.com/opencv/opencv/blob/
      f81370232a651bdac5042efe907bcaa50a66c487/modules/imgproc/src/
      moments.cpp#L93>`__
    """
    if isinstance(cont, np.ndarray):
        # If cont is an array, it is not a list of contours,
        # because contours can have different lengths.
        cont = [cont]
        ret_list = False
    else:
        ret_list = True

    length = len(cont)

    inert_ratio_raw = np.zeros(length, dtype=float) * np.nan

    for ii in range(length):
        moments = cont_moments_cv(cont[ii])
        if moments is not None:
            inert_ratio_raw[ii] = np.sqrt(moments["mu20"]/moments["mu02"])

    if not ret_list:
        inert_ratio_raw = inert_ratio_raw[0]

    return inert_ratio_raw


Instruction:
Can you tell what is the following Python 3 function doing
def get_tilt(cont):
    if isinstance(cont, np.ndarray):
        # If cont is an array, it is not a list of contours,
        # because contours can have different lengths.
        cont = [cont]
        ret_list = False
    else:
        ret_list = True

    length = len(cont)

    tilt = np.zeros(length, dtype=float) * np.nan

    for ii in range(length):
        moments = cont_moments_cv(cont[ii])
        if moments is not None:
            # orientation of the contour
            oii = 0.5 * np.arctan2(2 * moments['mu11'],
                                   moments['mu02'] - moments['mu20'])
            # +PI/2 because relative to channel axis
            tilt[ii] = oii + np.pi/2

    # restrict to interval [0,PI/2]
    tilt = np.mod(tilt, np.pi)
    tilt[tilt > np.pi/2] -= np.pi
    tilt = np.abs(tilt)

    if not ret_list:
        tilt = tilt[0]

    return tilt

Response: Compute tilt of raw contour relative to channel axis


Instruction:
Can you tell what is the following Python 3 function doing
def tag(*tags):
    '''
    Constructs a decorator that tags a function with specified
    strings (@tags). The tags on the decorated function are
    available via fn.tags
    '''
    def dfn(fn):
        _tags = getattr(fn, 'tags', set())
        _tags.update(tags)
        fn.tags = _tags
        return fn
    return dfn

Response: Returns a function that tags a function with specified tags.


Instruction:
Create a Python 3 function to
return a decorator that marks the given function as raw response format

Response:
def raw(mime='application/octet-stream'):
    '''
    Constructs a decorator that marks the fn
    as raw response format
    '''
    def dfn(fn):
        tags = getattr(fn, 'tags', set())
        tags.add('raw')
        fn.tags = tags
        fn.mime = getattr(fn, 'mime', mime)
        return fn
    return dfn


Instruction:
Can you tell what is the following Python 3 function doing
def open(self, pysession_id):
        '''
        Called when client opens connection. Initialization
        is done here.
        '''
        self.id = id(self)
        self.funcserver = self.application.funcserver
        self.pysession_id = pysession_id

        # register this connection with node
        self.state = self.funcserver.websocks[self.id] = {'id': self.id, 'sock': self}

Response: Called when client opens a new connection. This method is called when the client opens a new connection.


Instruction:
Can you generate the documentation for the following Python 3 function
def on_message(self, msg):
        '''
        Called when client sends a message.

        Supports a python debugging console. This forms
        the "eval" part of a standard read-eval-print loop.

        Currently the only implementation of the python
        console is in the WebUI but the implementation
        of a terminal based console is planned.
        '''

        msg = json.loads(msg)

        psession = self.funcserver.pysessions.get(self.pysession_id, None)
        if psession is None:
            interpreter = PyInterpreter(self.funcserver.define_python_namespace())
            psession = dict(interpreter=interpreter, socks=set([self.id]))
            self.funcserver.pysessions[self.pysession_id] = psession
        else:
            interpreter = psession['interpreter']
            psession['socks'].add(self.id)

        code = msg['code']
        msg_id = msg['id']

        stdout = sys.stdout
        try:
            sys.stdout = cStringIO.StringIO()
            interpreter.runsource(code)
            output = sys.stdout.getvalue() or interpreter.output
            if isinstance(output, list): output = ''.join(output)
            interpreter.output = []
        finally:
            sys.stdout = stdout

        msg = {'type': MSG_TYPE_CONSOLE, 'id': msg_id, 'data': output}
        self.send_message(msg)

Response: Called when client sends a message.


Instruction:
Write a Python 3 function that can
call when client closes this connection. Cleanup is done here.

Response:
def on_close(self):
        '''
        Called when client closes this connection. Cleanup
        is done here.
        '''

        if self.id in self.funcserver.websocks:
            self.funcserver.websocks[self.id] = None
            ioloop = tornado.ioloop.IOLoop.instance()
            ioloop.add_callback(lambda: self.funcserver.websocks.pop(self.id, None))

        psession = self.funcserver.pysessions.get(self.pysession_id, None)
        if psession:
            psession['socks'].remove(self.id)
            if not psession['socks']:
                del self.funcserver.pysessions[self.pysession_id]


Instruction:
Here you have a function in Python 3, explain what it does
def _clean_kwargs(self, kwargs, fn):
        '''
        Remove unexpected keyword arguments from the
        set of received keyword arguments.
        '''
        # Do not do the cleaning if server config
        # doesnt ask to ignore
        if not self.server.IGNORE_UNEXPECTED_KWARGS:
            return kwargs

        expected_kwargs = set(inspect.getargspec(fn).args)
        got_kwargs = set(kwargs.keys())
        unexpected_kwargs = got_kwargs - expected_kwargs
        for k in unexpected_kwargs:
            del kwargs[k]

        return kwargs

Response: Remove unexpected keyword arguments from the set of received keyword arguments.


Instruction:
Can you create a Python 3 function that
dumps the stack of all threads.

Response:
def dump_stacks(self):
        '''
        Dumps the stack of all threads. This function
        is meant for debugging. Useful when a deadlock happens.

        borrowed from: http://blog.ziade.org/2012/05/25/zmq-and-gevent-debugging-nightmares/
        '''

        dump = []

        # threads
        threads = dict([(th.ident, th.name)
                            for th in threading.enumerate()])

        for thread, frame in sys._current_frames().items():
            if thread not in threads: continue
            dump.append('Thread 0x%x (%s)\n' % (thread, threads[thread]))
            dump.append(''.join(traceback.format_stack(frame)))
            dump.append('\n')

        return ''.join(dump)


Instruction:
Implement a Python 3 function for
adding a hook to send to websocket if the run command was selected

Response:
def define_log_pre_format_hooks(self):
        """
        adds a hook to send to websocket if the run command was selected
        """
        hooks = super(Server, self).define_log_pre_format_hooks()
        # NOTE enabling logs only on debug mode
        if self.args.func == self.run and self.args.debug:
            hooks.append(self._send_log_to_ws)

        return hooks


Instruction:
Can you implement a function in Python 3 that
prepares the api and starts the tornado funcserver

Response:
def run(self):
        """ prepares the api and starts the tornado funcserver """
        self.log_id = 0

        # all active websockets and their state
        self.websocks = {}

        # all active python interpreter sessions
        self.pysessions = {}

        if self.DISABLE_REQUESTS_DEBUG_LOGS:
            disable_requests_debug_logs()

        self.threadpool = ThreadPool(self.THREADPOOL_WORKERS)

        self.api = None

        # tornado app object
        base_handlers = self.prepare_base_handlers()
        handlers = self.prepare_handlers()
        self.template_loader = TemplateLoader([resolve_path(self.TEMPLATE_PATH)])
        _ = self.prepare_template_loader(self.template_loader)
        if _ is not None: self.template_loader = _

        shclass = CustomStaticFileHandler
        shclass.PATHS.append(resolve_path(self.STATIC_PATH))
        _ = self.prepare_static_paths(shclass.PATHS)
        if _ is not None: shclass.PATHS = _

        self.static_handler_class = shclass

        self.nav_tabs = [('Home', '/')]
        if self.args.debug:
            self.nav_tabs += [('Console', '/console'), ('Logs', '/logs')]
        self.nav_tabs = self.prepare_nav_tabs(self.nav_tabs)

        settings = {
            'static_path': '<DUMMY-INEXISTENT-PATH>',
            'static_handler_class': self.static_handler_class,
            'template_loader': self.template_loader,
            'compress_response': True,
            'debug': self.args.debug,
        }

        all_handlers = handlers + base_handlers
        self.app = self.APP_CLASS(**settings)
        self.app.add_handlers(self.VIRTUAL_HOST, all_handlers)

        sys.funcserver = self.app.funcserver = self

        self.api = self.prepare_api()
        if self.api is not None and not hasattr(self.api, 'log'):
            self.api.log = self.log

        if self.args.port != 0:
            self.app.listen(self.args.port)

        tornado.ioloop.IOLoop.instance().start()


Instruction:
Here you have a function in Python 3, explain what it does
def _get_sql(filename):
    with open(os.path.join(SQL_DIR, filename), 'r') as f:
        return f.read()

Response: Returns the contents of the sql file from the given filename.


Instruction:
Write a Python 3 script to
verify that the identified content exists in the database.

Response:
def verify_id_n_version(id, version):
    """Given an ``id`` and ``version``, verify the identified content exists.

    """
    stmt = _get_sql('verify-id-and-version.sql')
    args = dict(id=id, version=version)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            try:
                valid = cursor.fetchone()[0]
            except TypeError:
                raise NotFound(join_ident_hash(id, version))
    return True


Instruction:
Explain what the following Python 3 code does
def get_type(ident_hash):
    id, version = get_id_n_version(ident_hash)

    stmt = _get_sql('get-type.sql')
    args = dict(id=id, version=version)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            type = cursor.fetchone()[0]
    return type

Response: Return the database type for the given identifier.


Instruction:
Write a Python 3 script for
returning the dictionary of metadata for a given entity.

Response:
def get_metadata(ident_hash):
    """Return the dictionary of metadata from the database.
    This data is keyed using the cnx-epub data structure.

    """
    id, version = get_id_n_version(ident_hash)

    stmt = _get_sql('get-metadata.sql')
    args = dict(id=id, version=version)

    # FIXME The license_url and license_text metadata attributes need to
    # change to a License structure similar to what is used in cnx-authoring.

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            try:
                metadata = cursor.fetchone()[0]
            except TypeError:
                raise NotFound(ident_hash)
    return metadata


Instruction:
Make a summary of the following Python 3 code
def get_content(ident_hash, context=None):
    id, version = get_id_n_version(ident_hash)
    filename = 'index.cnxml.html'

    if context is not None:
        stmt = _get_sql('get-baked-content.sql')
        args = dict(id=id, version=version, context=context)
    else:
        stmt = _get_sql('get-content.sql')
        args = dict(id=id, version=version, filename=filename)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            try:
                content, _ = cursor.fetchone()
            except TypeError:
                raise ContentNotFound(ident_hash, context, filename)
    return content[:]

Response: Returns the content for the given ident_hash.


Instruction:
Implement a Python 3 function for
returning information about the file identified by hash.

Response:
def get_file_info(hash, context=None):
    """Returns information about the file, identified by ``hash``.
    If the `context` (an ident-hash) is supplied,
    the information returned will be specific to that context.

    """
    if context is None:
        stmt = _get_sql('get-file-info.sql')
        args = dict(hash=hash)
    else:
        stmt = _get_sql('get-file-info-in-context.sql')
        id, version = get_id_n_version(context)
        args = dict(hash=hash, id=id, version=version)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            try:
                filename, media_type = cursor.fetchone()
            except TypeError:
                raise FileNotFound(hash)
    return filename, media_type


Instruction:
Can you tell what is the following Python 3 function doing
def get_file(hash):
    stmt = _get_sql('get-file.sql')
    args = dict(hash=hash)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            try:
                file, _ = cursor.fetchone()
            except TypeError:
                raise FileNotFound(hash)
    return memoryview(file[:])

Response: Return the contents of the file as a memoryview.


Instruction:
Implement a Python 3 function for
returning a list of SHA1 hashes for registered file entries identified by the given module ident_hash.

Response:
def get_registered_files(ident_hash):
    """Returns a list SHA1 hashes for registered file entries
    identified by the given module ``ident_hash``.

    Note, it's possible for a module to reference a file without having
    a registered file entry for it.

    Note, all files are included, including the raw form of the content.

    """
    id, version = get_id_n_version(ident_hash)

    stmt = _get_sql('get-registered-files-info.sql')
    args = dict(id=id, version=version)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            rows = cursor.fetchall()
    if rows is None:
        rows = []
    hashes = list(set([sha1 for sha1, _, __ in rows]))
    return hashes


Instruction:
How would you implement a function in Python 3 that
returns a tree structure of the Collection

Response:
def get_tree(ident_hash, baked=False):
    """Return a tree structure of the Collection"""
    id, version = get_id_n_version(ident_hash)

    stmt = _get_sql('get-tree.sql')
    args = dict(id=id, version=version, baked=baked)

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(stmt, args)
            try:
                tree = cursor.fetchone()[0]
            except TypeError:
                raise NotFound(ident_hash)
    if tree is None:
        raise NotFound(ident_hash)

    return tree


Instruction:
Explain what the following Python 3 code does
def guess_media_type(filepath):
    o = subprocess.check_output(['file', '--mime-type', '-Lb', filepath])
    o = o.strip()
    return o

Response: Returns the media - type of the file at the given filepath


Instruction:
Make a summary of the following Python 3 code
def lookup_module_ident(id, version):
    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute(
                "SELECT module_ident FROM modules "
                "WHERE uuid = %s "
                "AND CONCAT_WS('.', major_version, minor_version) = %s",
                (id, version))
            try:
                mident = cursor.fetchone()[0]
            except (IndexError, TypeError):
                ident_hash = join_ident_hash(id, version)
                raise RuntimeError("Content at {} does not exist."
                                   .format(ident_hash))
    return mident

Response: Look up the module_ident for the given id & version.


Instruction:
How would you explain what the following Python 3 function does
def insert_file(file, media_type):
    resource_hash = get_file_sha1(file)
    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute("SELECT fileid FROM files WHERE sha1 = %s",
                           (resource_hash,))
            try:
                fileid = cursor.fetchone()[0]
            except (IndexError, TypeError):
                cursor.execute("INSERT INTO files (file, media_type) "
                               "VALUES (%s, %s)"
                               "RETURNING fileid",
                               (psycopg2.Binary(file.read()), media_type,))
                fileid = cursor.fetchone()[0]
    return fileid, resource_hash

Response: Upsert the file and media_type into the files table. Returns the fileid and sha1 of the upserted file.


Instruction:
Here you have a function in Python 3, explain what it does
def upsert_module_file(module_ident, fileid, filename):
    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute("SELECT true FROM module_files "
                           "WHERE module_ident = %s "
                           "AND filename = %s",
                           (module_ident, filename,))
            try:
                cursor.fetchone()[0]
            except (IndexError, TypeError):
                cursor.execute("INSERT INTO module_files "
                               "(module_ident, fileid, filename) "
                               "VALUES (%s, %s, %s)",
                               (module_ident, fileid, filename,))
            else:
                cursor.execute("UPDATE module_files "
                               "SET (fileid) = (%s) "
                               "WHERE module_ident = %s AND filename = %s",
                               (fileid, module_ident, filename,))

Response: Upsert a file associated with fileid with filename as a module_files entry associated with module_ident.


Instruction:
How would you code a function in Python 3 to
inject the contents of file into the database as filename with media_type in association with the content at ident_hash.

Response:
def inject_resource(ident_hash, file, filename, media_type):
    """Injects the contents of ``file`` (a file-like object) into the database
    as ``filename`` with ``media_type`` in association with the content
    at ``ident_hash``.

    """
    resource_hash = get_file_sha1(file)
    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            s_ident_hash = split_ident_hash(ident_hash)
            module_ident = lookup_module_ident(*s_ident_hash)
            fileid, resource_hash = insert_file(file, media_type)
            upsert_module_file(module_ident, fileid, filename)
    return resource_hash


Instruction:
Can you implement a function in Python 3 that
computes the image contour from a binary array of pixels.

Response:
def get_contour(mask):
    """Compute the image contour from a mask

    The contour is computed in a very inefficient way using scikit-image
    and a conversion of float coordinates to pixel coordinates.

    Parameters
    ----------
    mask: binary ndarray of shape (M,N) or (K,M,N)
        The mask outlining the pixel positions of the event.
        If a 3d array is given, then `K` indexes the individual
        contours.

    Returns
    -------
    cont: ndarray or list of K ndarrays of shape (J,2)
        A 2D array that holds the contour of an event (in pixels)
        e.g. obtained using `mm.contour` where  `mm` is an instance
        of `RTDCBase`. The first and second columns of `cont`
        correspond to the x- and y-coordinates of the contour.
    """
    if isinstance(mask, np.ndarray) and len(mask.shape) == 2:
        mask = [mask]
        ret_list = False
    else:
        ret_list = True
    contours = []

    for mi in mask:
        c0 = find_contours(mi.transpose(),
                           level=.9999,
                           positive_orientation="low",
                           fully_connected="high")[0]
        # round all coordinates to pixel values
        c1 = np.asarray(np.round(c0), int)
        # remove duplicates
        c2 = remove_duplicates(c1)
        contours.append(c2)
    if ret_list:
        return contours
    else:
        return contours[0]


Instruction:
Implement a Python 3 function for
scanning the local network for TVs.

Response:
def scan(self, stop_on_first=True, base_ip=0):
        """Scans the local network for TVs."""
        tvs = []

        # Check if base_ip has been passed
        if base_ip == 0:
            # Find IP address of computer pymitv is running on
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            sock.connect(("8.8.8.8", 80))
            ip = sock.getsockname()[0]
            sock.close()

            # Get IP and compose a base like 192.168.1.xxx
            ip_parts = ip.split('.')
            base_ip = ip_parts[0] + '.' + ip_parts[1] + '.' + ip_parts[2]

        # Loop through every IP and check if TV is alive
        for ip_suffix in range(2, 256):
            ip_check = '{}.{}'.format(base_ip, ip_suffix)

            if self.check_ip(ip_check):
                tvs.append(ip_check)

                if stop_on_first:
                    break

        return tvs


Instruction:
Here you have a function in Python 3, explain what it does
def check_ip(ip, log=False):
        if log:
            print('Checking ip: {}...'.format(ip))

        request_timeout = 0.1

        try:
            tv_url = 'http://{}:6095/request?action=isalive'.format(ip)
            request = requests.get(tv_url, timeout=request_timeout)
        except requests.exceptions.ConnectTimeout:
            return False

        return request.status_code == 200

Response: Attempts a connection to the TV and checks if there really is a TV.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_field_type(f):
    types = (t[5:] for t in dir(f) if t[:4] == 'TYPE' and
             getattr(f, t) == f.type)
    return next(types)

Response: Obtain the type name of a GRPC Message field.


Instruction:
Implement a function in Python 3 to
get the type description of a GRPC Message field.

Response:
def get_field_description(f):
    """Get the type description of a GRPC Message field."""
    type_name = get_field_type(f)
    if type_name == 'MESSAGE' and \
            {sf.name for sf in f.message_type.fields} == {'key', 'value'}:
        return 'map<string, string>'
    elif type_name == 'MESSAGE':
        return f.message_type.full_name
    elif type_name == 'ENUM':
        return f.enum_type.full_name
    else:
        return type_name.lower()


Instruction:
Can you tell what is the following Python 3 function doing
def make_static_request(method, *args, **kwargs):
    if args and not use_signature:
        raise NotImplementedError("Only keyword arguments allowed in Python2")

    if use_signature:
        new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()}
        new_args = tuple(unwrap(value) for value in args)
        bound_args = method.signature.bind(
            None, *new_args, **new_kwargs).arguments

        # if we encounter any Enum arguments, replace them with their value
        for k in bound_args:
            if isinstance(bound_args[k], Enum):
                bound_args[k] = bound_args[k].value

        new_kwargs = {kw: v for kw, v in bound_args.items() if kw != 'cls'}

    else:
        new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()}

    return method.request_type(**new_kwargs)

Response: Creates a request from a static method function call.


Instruction:
Can you generate the documentation for the following Python 3 function
def make_request(self, method, *args, **kwargs):
    if args and not use_signature:
        raise NotImplementedError("Only keyword arguments allowed in Python2")

    new_kwargs = {kw: unwrap(value) for kw, value in kwargs.items()}

    if use_signature:
        new_args = tuple(unwrap(value) for value in args)
        bound_args = method.signature.bind(
            unwrap(self), *new_args, **new_kwargs).arguments

        # if we encounter any Enum arguments, replace them with their value
        def translate_enum(arg):
            return arg.value if isinstance(arg, Enum) else arg

        for k in bound_args:
            if isinstance(bound_args[k], str):
                continue
            if isinstance(bound_args[k], dict):
                continue

            try:
                x = [translate_enum(arg) for arg in bound_args[k]]
                bound_args[k] = x
            except TypeError:
                bound_args[k] = translate_enum(bound_args[k])

        # replace `self` with the correct keyword
        new_kwargs = {(kw if kw != 'self' else method.field_name): v
                      for kw, v in bound_args.items()}
        # args = tuple(x.value if isinstance(x, Enum) else x for x in args)

    else:
        new_kwargs[self.field_name] = unwrap(self)

    return method.request_type(**new_kwargs)

Response: Creates a request from a method function call.


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

    if m.is_simple:
        def simple_method(self):
            """TODO: no docstring!"""
            return apply_transform(
                self.__service__, m.output_transform,
                grpc_call(self.__service__, m, unwrap(self)))

        return simple_method

    elif m.input_transform is not None:
        def transform_method(self, *args, **kwargs):
            """TODO: no docstring!"""
            request = m.input_transform(self, *args, **kwargs)
            return apply_transform(
                self.__service__, m.output_transform,
                grpc_call(self.__service__, m, request))

        return transform_method

    elif m.static:
        def static_method(cls, *args, **kwargs):
            """TODO: no docstring!"""
            request = make_static_request(m, *args, **kwargs)
            return apply_transform(
                cls.__stub__(__server__), m.output_transform,
                grpc_call(cls.__stub__(__server__), m, request))

        return static_method

    else:
        def request_method(self, *args, **kwargs):
            """TODO: no docstring!"""
            request = make_request(self, m, *args, **kwargs)
            return apply_transform(
                self.__service__, m.output_transform,
                grpc_call(self.__service__, m, request))

        return request_method

Response: Generates a method from a GrpcMethod definition.


Instruction:
Explain what the following Python 3 code does
def request_name(self):
        if self.static and not self.uses_request:
            return 'Empty'

        if not self.uses_request:
            return None

        if isinstance(self.uses_request, str):
            return self.uses_request

        return to_camel_case(self.name) + "Request"

Response: Generate the name of the request.


Instruction:
Create a Python 3 function for
retrieving the type of the request from the XMPP protocol.

Response:
def request_type(self):
        """Retrieve the type of the request, by fetching it from
        `xenon.proto.xenon_pb2`."""
        if self.static and not self.uses_request:
            return getattr(xenon_pb2, 'Empty')

        if not self.uses_request:
            return None

        return getattr(xenon_pb2, self.request_name)


Instruction:
How would you implement a function in Python 3 that
creates a signature for this method only in Python > 3. 4.

Response:
def signature(self):
        """Create a signature for this method, only in Python > 3.4"""
        if not use_signature:
            raise NotImplementedError("Python 3 only.")

        if self.static:
            parameters = \
                (Parameter(name='cls',
                           kind=Parameter.POSITIONAL_ONLY),)

        else:
            parameters = \
                (Parameter(name='self',
                           kind=Parameter.POSITIONAL_ONLY),)

        if self.input_transform:
            return signature(self.input_transform)

        if self.uses_request:
            fields = get_fields(self.request_type)
            if not self.static:
                if self.field_name not in fields:
                    raise NameError("field '{}' not found in {}".format(
                        self.field_name, self.request_name))
                fields.remove(self.field_name)
            parameters += tuple(
                Parameter(name=name, kind=Parameter.POSITIONAL_OR_KEYWORD,
                          default=None)
                for name in fields)

        return Signature(parameters)


Instruction:
Explain what the following Python 3 code does
def docstring(self, servicer):
        s = getattr(servicer, to_lower_camel_case(self.name)).__doc__ \
            or "TODO: no docstring in .proto file"

        if self.uses_request:
            s += "\n"
            for field in get_fields(self.request_type):
                if field != self.field_name:
                    type_info = get_field_description(
                        self.request_type.DESCRIPTOR.fields_by_name[field])
                    s += "    :param {}: {}\n".format(field, field)
                    s += "    :type {0}: {1}\n".format(field, type_info)

        return s

Response: Generate a doc - string.


Instruction:
Can you write a function in Python 3 where it
returns a slug for the unicode_string.

Response:
def slugify(string):
    """Return a slug for the unicode_string.

    (lowercase, only letters and numbers, hyphens replace spaces)
    """
    filtered_string = []
    if isinstance(string, str):
        string = unicode(string, 'utf-8')
    for i in unicodedata.normalize('NFKC', string):
        cat = unicodedata.category(i)[0]
        # filter out all the non letter and non number characters from the
        # input (L is letter and N is number)
        if cat in 'LN' or i in '-_':
            filtered_string.append(i)
        elif cat in 'Z':
            filtered_string.append(' ')
    return re.sub('\s+', '-', ''.join(filtered_string)).lower()


Instruction:
Can you write a function in Python 3 where it
changes all python2 str instances to unicode / python3 str.

Response:
def utf8(item):
    """Change all python2 str/bytes instances to unicode/python3 str."""
    if isinstance(item, list):
        return [utf8(i) for i in item]
    if isinstance(item, tuple):
        return tuple([utf8(i) for i in item])
    if isinstance(item, dict):
        return {utf8(k): utf8(v) for k, v in item.items()}
    try:
        return item.decode('utf-8')
    except:  # bare except since this method is supposed to be safe anywhere
        return item


Instruction:
Given the following Python 3 function, write the documentation
def update(self, force=[]):

        # These lists may help us become very fast in the future
        newkeys = []
        oldvals = []
        newvals = []

        cfg_cur = self.rtdc_ds.config["filtering"]
        cfg_old = self._old_config

        # Determine which data was updated
        for skey in list(cfg_cur.keys()):
            if skey not in cfg_old:
                cfg_old[skey] = None
            if cfg_cur[skey] != cfg_old[skey]:
                newkeys.append(skey)
                oldvals.append(cfg_old[skey])
                newvals.append(cfg_cur[skey])

        # 1. Filter all feature min/max values.
        # This line gets the feature names that must be filtered.
        col2filter = []
        for k in newkeys:
            # k[:-4] because we want to crop " min" and " max"
            if k[:-4] in dfn.scalar_feature_names:
                col2filter.append(k[:-4])

        for f in force:
            # Manually add forced features
            if f in dfn.scalar_feature_names:
                col2filter.append(f)
            else:
                # Make sure the feature name is valid.
                raise ValueError("Unknown feature name {}".format(f))

        col2filter = np.unique(col2filter)

        for col in col2filter:
            if col in self.rtdc_ds:
                fstart = col + " min"
                fend = col + " max"
                # Get the current feature filter
                col_filt = self[col]
                # If min and max exist and if they are not identical:
                if (fstart in cfg_cur and
                    fend in cfg_cur and
                        cfg_cur[fstart] != cfg_cur[fend]):
                    # TODO: speedup
                    # Here one could check for smaller values in the
                    # lists oldvals/newvals that we defined above.
                    # Be sure to check against force in that case!
                    ivalstart = cfg_cur[fstart]
                    ivalend = cfg_cur[fend]
                    if ivalstart > ivalend:
                        msg = "inverting filter: {} > {}".format(fstart, fend)
                        warnings.warn(msg)
                        ivalstart, ivalend = ivalend, ivalstart
                    data = self.rtdc_ds[col]
                    col_filt[:] = (ivalstart <= data)*(data <= ivalend)
                else:
                    col_filt[:] = True

        # 2. Filter with polygon filters
        # check if something has changed
        pf_id = "polygon filters"
        if (
            (pf_id in cfg_cur and pf_id not in cfg_old) or
            (pf_id in cfg_cur and pf_id in cfg_old and
             cfg_cur[pf_id] != cfg_old[pf_id])):
            self.polygon[:] = True
            # perform polygon filtering
            for p in PolygonFilter.instances:
                if p.unique_id in cfg_cur[pf_id]:
                    # update self.polygon
                    # iterate through axes
                    datax = self.rtdc_ds[p.axes[0]]
                    datay = self.rtdc_ds[p.axes[1]]
                    self.polygon *= p.filter(datax, datay)

        # 3. Invalid filters
        self.invalid[:] = True
        if cfg_cur["remove invalid events"]:
            for col in dfn.scalar_feature_names:
                if col in self.rtdc_ds:
                    data = self.rtdc_ds[col]
                    invalid = np.isinf(data) | np.isnan(data)
                    self.invalid *= ~invalid

        # 4. Finally combine all filters
        # get a list of all filters
        self.all[:] = True

        if cfg_cur["enable filters"]:
            for col in self._filters:
                self.all[:] *= self._filters[col]

            self.all[:] *= self.invalid
            self.all[:] *= self.manual
            self.all[:] *= self.polygon

            # Filter with configuration keyword argument "limit events".
            # This additional step limits the total number of events in
            # self.all.
            if cfg_cur["limit events"] > 0:
                limit = cfg_cur["limit events"]
                sub = self.all[self.all]
                _f, idx = downsampling.downsample_rand(sub,
                                                       samples=limit,
                                                       ret_idx=True)
                sub[~idx] = False
                self.all[self.all] = sub

        # Actual filtering is then done during plotting
        self._old_config = self.rtdc_ds.config.copy()["filtering"]

Response: Update the filters according to self. rtdc_ds. config. filtering.