def from_bulk_and_miller(cls, structure, miller_index, min_slab_size=8.0, min_vacuum_size=10.0, max_normal_search=None, center_slab=True, selective_dynamics=False, undercoord_threshold=0.09):
    vnn_bulk = VoronoiNN(tol=0.05)
    bulk_coords = [len(vnn_bulk.get_nn(structure, n)) for n in range(len(structure))]
    struct = structure.copy(site_properties={'bulk_coordinations': bulk_coords})
    slabs = generate_all_slabs(struct, max_index=max(miller_index), min_slab_size=min_slab_size, min_vacuum_size=min_vacuum_size, max_normal_search=max_normal_search, center_slab=center_slab)
    slab_dict = {slab.miller_index: slab for slab in slabs}
    if (miller_index not in slab_dict):
        raise ValueError('Miller index not in slab dict')
    this_slab = slab_dict[miller_index]
    vnn_surface = VoronoiNN(tol=0.05, allow_pathological=True)
    (surf_props, undercoords) = ([], [])
    this_mi_vec = get_mi_vec(this_slab)
    mi_mags = [np.dot(this_mi_vec, site.coords) for site in this_slab]
    average_mi_mag = np.average(mi_mags)
    for (n, site) in enumerate(this_slab):
        bulk_coord = this_slab.site_properties['bulk_coordinations'][n]
        slab_coord = len(vnn_surface.get_nn(this_slab, n))
        mi_mag = np.dot(this_mi_vec, site.coords)
        undercoord = ((bulk_coord - slab_coord) / bulk_coord)
        undercoords += [undercoord]
        if ((undercoord > undercoord_threshold) and (mi_mag > average_mi_mag)):
            surf_props += ['surface']
        else:
            surf_props += ['subsurface']
    new_site_properties = {'surface_properties': surf_props, 'undercoords': undercoords}
    new_slab = this_slab.copy(site_properties=new_site_properties)
    return cls(new_slab, selective_dynamics)

This method constructs the adsorbate site finder from a bulk
structure and a miller index, which allows the surface sites
to be determined from the difference in bulk and slab coordination,
as opposed to the height threshold.

Args:
structure (Structure): structure from which slab
input to the ASF is constructed
miller_index (3-tuple or list): miller index to be used
min_slab_size (float): min slab size for slab generation
min_vacuum_size (float): min vacuum size for slab generation
max_normal_search (int): max normal search for slab generation
center_slab (bool): whether to center slab in slab generation
selective dynamics (bool): whether to assign surface sites
to selective dynamics
undercoord_threshold (float): threshold of "undercoordation"
to use for the assignment of surface sites.  Default is
0.1, for which surface sites will be designated if they
are 10% less coordinated than their bulk counterpart


def byte_swap_tensor_content(tensor, from_endiness, to_endiness):
    if tensor.dtype in byte_swappable:
        tshape = tensor.tensor_shape.dim
        tensor_bytes = tensor.tensor_content
        if tensor_bytes:
            tensor_size = 1
            for sz in tshape:
                if sz.size != 0:
                    tensor_size *= sz.size
            chunksize = len(tensor_bytes)
            to_swap = [tensor_bytes[i:i + chunksize] for i in range(0, len(tensor_bytes), chunksize)]
            tensor.tensor_content = b''.join([int.from_bytes(byteswap, from_endiness).to_bytes(chunksize, to_endiness) for byteswap in to_swap])

Byte swaps.

Args:
tensor: Target tensor to change endiness.
from_endiness: The original endianness format. "big" or "little"
to_endiness: The target endianness format. "big" or "little"


def slice(filename, number_tiles=None, col=None, row=None, save=True):

    im = Image.open(filename)
    im_w, im_h = im.size

    columns = 0
    rows = 0
    if not number_tiles is None:
        validate_image(im, number_tiles)
        columns, rows = calc_columns_rows(number_tiles)
        extras = (columns * rows) - number_tiles
    else:
        validate_image_col_row(im, col, row)
        columns = col
        rows = row
        extras = (columns * rows) - number_tiles

    tile_w, tile_h = int(floor(im_w / columns)), int(floor(im_h / rows))

    tiles = []
    number = 1
    for pos_y in range(0, im_h - rows, tile_h):
        for pos_x in range(0, im_w - columns, tile_w):
            area = (pos_x, pos_y, pos_x + tile_w, pos_y + tile_h)
            image = im.crop(area)
            position = (int(floor(pos_x / tile_w)) + 1,
                        int(floor(pos_y / tile_h)) + 1)
            coords = (pos_x, pos_y)
            tile = Tile(image, number, position, coords)
            tiles.append(tile)
            number += 1
    if save:
        save_tiles(tiles,
                   prefix=get_basename(filename),
                   directory=os.path.dirname(filename))
    return tuple(tiles)

Split an image into a specified number of tiles.

Args:
filename (str):  The filename of the image to split.
number_tiles (int):  The number of tiles required.

Kwargs:
save (bool): Whether or not to save tiles to disk.

Returns:
Tuple of :class:`Tile` instances.


def inflate_plugins(self, plugins_definition, inflate_method):

        if isinstance(plugins_definition, list):
            return self.inflate_plugin_list(plugins_definition, inflate_method)
        elif isinstance(plugins_definition, dict):
            return self.inflate_plugin_dict(plugins_definition, inflate_method)
        else:
            raise ValueError('%s type is not supported for a plugin list, '
                             'use list or dict' % type(plugins_definition))

Inflate multiple plugins based on a list/dict definition.

Args:
plugins_definition (list/dict): the plugins definitions.
inflate_method (method): the method to indlate each plugin.

Returns:
list: a list of plugin instances.

Raises:
ValueError: when the definition type is not list or dict.


def read(cls, data):

        if isinstance(data, pd.DataFrame):
            output = OrderedDict({})
            output['version'] = '2.0'
            output['class'] = 'dimension'
            [label] = [x for x in list(data.columns.values) if
                       x not in ['id', 'index']]
            output['label'] = label
            output['category'] = OrderedDict({})
            output['category']['index'] = data.id.tolist()
            output['category']['label'] = OrderedDict(
                zip(data.id.values, data[label].values))
            return cls(output)
        elif isinstance(data, OrderedDict):
            return cls(data)
        elif isinstance(data, basestring) and data.startswith(("http:
                                                               "https:
                                                               "ftp:
                                                               "ftps:
            return cls(request(data))
        elif isinstance(data,basestring):
            try:
                json_dict = json.loads(data, object_pairs_hook=OrderedDict)
                return cls(json_dict)
            except ValueError:
                raise
        else:
            try:
                json_dict = json.load(data, object_pairs_hook=OrderedDict)
                return cls(json_dict)
            except ValueError:
                raise

Reads data from URL, Dataframe, JSON string, JSON file
or OrderedDict.
Args:
data: can be a Pandas Dataframe, a JSON string, a JSON file,
an OrderedDict or a URL pointing to a JSONstat file.

Returns:
An object of class Dimension populated with data.


def _get_new_finished_state(self, state, new_seq, new_log_probs):
    i = state[_StateKeys.CUR_INDEX]
    finished_seq = state[_StateKeys.FINISHED_SEQ]
    finished_scores = state[_StateKeys.FINISHED_SCORES]
    finished_flags = state[_StateKeys.FINISHED_FLAGS]
    finished_seq = tf.concat([finished_seq, tf.zeros([self.batch_size, self.beam_size, 1], tf.int32)], axis=2)
    length_norm = _length_normalization(self.alpha, (i + 1))
    new_scores = (new_log_probs / length_norm)
    new_finished_flags = tf.equal(new_seq[(:, :, (- 1))], self.eos_id)
    new_scores += ((1.0 - tf.to_float(new_finished_flags)) * (- INF))
    finished_seq = tf.concat([finished_seq, new_seq], axis=1)
    finished_scores = tf.concat([finished_scores, new_scores], axis=1)
    finished_flags = tf.concat([finished_flags, new_finished_flags], axis=1)
    (top_finished_seq, top_finished_scores, top_finished_flags) = _gather_topk_beams([finished_seq, finished_scores, finished_flags], finished_scores, self.batch_size, self.beam_size)
    return {_StateKeys.FINISHED_SEQ: top_finished_seq, _StateKeys.FINISHED_SCORES: top_finished_scores, _StateKeys.FINISHED_FLAGS: top_finished_flags}

Combine new and old finished sequences, and gather the top k sequences.

Args:
state: A dictionary with the current loop state.
new_seq: New sequences generated by growing the current alive sequences
int32 tensor with shape [batch_size, beam_size, i + 1]
new_log_probs: Log probabilities of new sequences
float32 tensor with shape [batch_size, beam_size]

Returns:
Dictionary with finished keys from _StateKeys:
{Top beam_size finished sequences based on score,
Scores of finished sequences,
Finished flags of finished sequences}


def load_and_use(path):
    example_cond, example_a, example_b = _get_example_tensors()
    restored = tf.saved_model.load(path)
    return restored.use_multiplex(example_cond, example_a, example_b)

Load and used a model that was previously created by `save()`.

Args:
path: Directory to load model from, typically the same directory that was
used by save().

Returns:
A tensor that is the result of using the multiplex op that is
tf.constant([1, 20, 3, 40, 5], dtype=tf.int64).


def rename_document(self, did, name):
    payload = {'name': name}
    return self._api.request('post', ('/api/documents/' + did), body=payload)

Renames the specified document.

Args:
- did (str): Document ID
- name (str): New document name

Returns:
- requests.Response: Onshape response data


def is_link(path):

    if sys.getwindowsversion().major < 6:
        raise SaltInvocationError('Symlinks are only supported on Windows Vista or later.')

    try:
        return salt.utils.path.islink(path)
    except Exception as exc:
        raise CommandExecutionError(exc)

Check if the path is a symlink

This is only supported on Windows Vista or later.

Inline with Unix behavior, this function will raise an error if the path
is not a symlink, however, the error raised will be a SaltInvocationError,
not an OSError.

Args:
path (str): The path to a file or directory

Returns:
bool: True if path is a symlink, otherwise False

CLI Example:

.. code-block:: bash

salt '*' file.is_link /path/to/link


def _identify_eds_ing(first, second):

        A = set([first.L, first.R])
        A.update(first.D)

        B = set([second.L, second.R])
        B.update(second.D)

        depend_set = A & B
        left, right = sorted(list(A ^ B))

        return left, right, depend_set

Find nodes connecting adjacent edges.

Args:
first(Edge): Edge object representing the first edge.
second(Edge): Edge object representing the second edge.

Returns:
tuple[int, int, set[int]]: The first two values represent left and right node
indicies of the new edge. The third value is the new dependence set.


def wait_for_js(function):

    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        if (len(args) < 1):
            return function(*args, **kwargs)
        else:
            self = args[0]
            if hasattr(self, 'wait_for_js'):
                self.wait_for_js()
            return function(*args, **kwargs)
    return wrapper

Method decorator that waits for JavaScript dependencies before executing `function`.
If the function is not a method, the decorator has no effect.

Args:
function (callable): Method to decorate.

Returns:
Decorated method


def _calc_block_mean_variance(image, mask, blocksize):
    I = image.copy()
    I_f = (I.astype(np.float32) / 255.0)
    result = np.zeros(((image.shape[0] / blocksize), (image.shape[1] / blocksize)), dtype=np.float32)
    for i in xrange(0, (image.shape[0] - blocksize), blocksize):
        for j in xrange(0, (image.shape[1] - blocksize), blocksize):
            patch = I_f[(i:((i + blocksize) + 1), j:((j + blocksize) + 1))]
            mask_patch = mask[(i:((i + blocksize) + 1), j:((j + blocksize) + 1))]
            tmp1 = np.zeros((blocksize, blocksize))
            tmp2 = np.zeros((blocksize, blocksize))
            (mean, std_dev) = cv2.meanStdDev(patch, tmp1, tmp2, mask_patch)
            value = 0
            if (std_dev[0][0] > MEAN_VARIANCE_THRESHOLD):
                value = mean[0][0]
            result[((i / blocksize), (j / blocksize))] = value
    small_image = cv2.resize(I, ((image.shape[1] / blocksize), (image.shape[0] / blocksize)))
    (res, inpaintmask) = cv2.threshold(result, 0.02, 1, cv2.THRESH_BINARY)
    inpainted = cv2.inpaint(small_image, inpaintmask.astype(np.uint8), 5, cv2.INPAINT_TELEA)
    res = cv2.resize(inpainted, (image.shape[1], image.shape[0]))
    return res

Adaptively determines image background.

Args:
image: image converted 1-channel image.
mask: 1-channel mask, same size as image.
blocksize: adaptive algorithm parameter.

Returns:
image of same size as input with foreground inpainted with background.


def train(self, debug=True, force=False, single_thread=False, timeout=20):
    if ((not self.must_train) and (not force)):
        return
    self.padaos.compile()
    self.train_thread = Thread(target=self._train, kwargs=dict(debug=debug, single_thread=single_thread, timeout=timeout), daemon=True)
    self.train_thread.start()
    self.train_thread.join(timeout)
    self.must_train = False
    return (not self.train_thread.is_alive())

Trains all the loaded intents that need to be updated
If a cache file exists with the same hash as the intent file,
the intent will not be trained and just loaded from file

Args:
debug (bool): Whether to print a message to stdout each time a new intent is trained
force (bool): Whether to force training if already finished
single_thread (bool): Whether to force running in a single thread
timeout (float): Seconds before cancelling training
Returns:
bool: True if training succeeded without timeout


def model_fn(features, labels, mode, params, config):

  del labels, config

  if params["analytic_kl"] and params["mixture_components"] != 1:
    raise NotImplementedError(
        "Using `analytic_kl` is only supported when `mixture_components = 1` "
        "since there's no closed form otherwise.")

  encoder = make_encoder(params["activation"],
                         params["latent_size"],
                         params["base_depth"])
  decoder = make_decoder(params["activation"],
                         params["latent_size"],
                         IMAGE_SHAPE,
                         params["base_depth"])
  latent_prior = make_mixture_prior(params["latent_size"],
                                    params["mixture_components"])

  image_tile_summary(
      "input", tf.cast(features, dtype=tf.float32), rows=1, cols=16)

  approx_posterior = encoder(features)
  approx_posterior_sample = approx_posterior.sample(params["n_samples"])
  decoder_likelihood = decoder(approx_posterior_sample)
  image_tile_summary(
      "recon/sample",
      tf.cast(decoder_likelihood.sample()[:3, :16], dtype=tf.float32),
      rows=3,
      cols=16)
  image_tile_summary(
      "recon/mean",
      decoder_likelihood.mean()[:3, :16],
      rows=3,
      cols=16)

  distortion = -decoder_likelihood.log_prob(features)
  avg_distortion = tf.reduce_mean(input_tensor=distortion)
  tf.compat.v1.summary.scalar("distortion", avg_distortion)

  if params["analytic_kl"]:
    rate = tfd.kl_divergence(approx_posterior, latent_prior)
  else:
    rate = (approx_posterior.log_prob(approx_posterior_sample)
            - latent_prior.log_prob(approx_posterior_sample))
  avg_rate = tf.reduce_mean(input_tensor=rate)
  tf.compat.v1.summary.scalar("rate", avg_rate)

  elbo_local = -(rate + distortion)

  elbo = tf.reduce_mean(input_tensor=elbo_local)
  loss = -elbo
  tf.compat.v1.summary.scalar("elbo", elbo)

  importance_weighted_elbo = tf.reduce_mean(
      input_tensor=tf.reduce_logsumexp(input_tensor=elbo_local, axis=0) -
      tf.math.log(tf.cast(params["n_samples"], dtype=tf.float32)))
  tf.compat.v1.summary.scalar("elbo/importance_weighted",
                              importance_weighted_elbo)

  random_image = decoder(latent_prior.sample(16))
  image_tile_summary(
      "random/sample",
      tf.cast(random_image.sample(), dtype=tf.float32),
      rows=4,
      cols=4)
  image_tile_summary("random/mean", random_image.mean(), rows=4, cols=4)

  global_step = tf.compat.v1.train.get_or_create_global_step()
  learning_rate = tf.compat.v1.train.cosine_decay(
      params["learning_rate"], global_step, params["max_steps"])
  tf.compat.v1.summary.scalar("learning_rate", learning_rate)
  optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate)
  train_op = optimizer.minimize(loss, global_step=global_step)

  return tf.estimator.EstimatorSpec(
      mode=mode,
      loss=loss,
      train_op=train_op,
      eval_metric_ops={
          "elbo":
              tf.compat.v1.metrics.mean(elbo),
          "elbo/importance_weighted":
              tf.compat.v1.metrics.mean(importance_weighted_elbo),
          "rate":
              tf.compat.v1.metrics.mean(avg_rate),
          "distortion":
              tf.compat.v1.metrics.mean(avg_distortion),
      },
  )

Builds the model function for use in an estimator.

Arguments:
features: The input features for the estimator.
labels: The labels, unused here.
mode: Signifies whether it is train or test or predict.
params: Some hyperparameters as a dictionary.
config: The RunConfig, unused here.

Returns:
EstimatorSpec: A tf.estimator.EstimatorSpec instance.


def num_connected_components(self, unitary_only=False):

        reg_offset = 0
        reg_map = {}

        if unitary_only:
            regs = self.qregs
        else:
            regs = self.qregs+self.cregs

        for reg in regs:
            reg_map[reg.name] = reg_offset
            reg_offset += reg.size

        sub_graphs = [[bit] for bit in range(reg_offset)]

        num_sub_graphs = len(sub_graphs)

        for instr, qargs, cargs in self.data:
            if unitary_only:
                args = qargs
                num_qargs = len(args)
            else:
                args = qargs+cargs
                num_qargs = len(args) + (1 if instr.control else 0)

            if num_qargs >= 2 and instr.name not in ['barrier', 'snapshot']:
                graphs_touched = []
                num_touched = 0

                if instr.control and not unitary_only:
                    creg = instr.control[0]
                    creg_int = reg_map[creg.name]
                    for coff in range(creg.size):
                        temp_int = creg_int+coff
                        for k in range(num_sub_graphs):
                            if temp_int in sub_graphs[k]:
                                graphs_touched.append(k)
                                num_touched += 1
                                break

                for item in args:
                    reg_int = reg_map[item[0].name]+item[1]
                    for k in range(num_sub_graphs):
                        if reg_int in sub_graphs[k]:
                            if k not in graphs_touched:
                                graphs_touched.append(k)
                                num_touched += 1
                                break

                if num_touched > 1:
                    connections = []
                    for idx in graphs_touched:
                        connections.extend(sub_graphs[idx])
                    _sub_graphs = []
                    for idx in range(num_sub_graphs):
                        if idx not in graphs_touched:
                            _sub_graphs.append(sub_graphs[idx])
                    _sub_graphs.append(connections)
                    sub_graphs = _sub_graphs
                    num_sub_graphs -= (num_touched-1)

            if num_sub_graphs == 1:
                break
        return num_sub_graphs

How many non-entangled subcircuits can the circuit be factored to.

Args:
unitary_only (bool): Compute only unitary part of graph.

Returns:
int: Number of connected components in circuit.


def __init__(self, event_type: str):

        if not isinstance(event_type, str) or event_type == "":
            raise TypeError("Invalid event type: {}".format(event_type))

        self._event_type: str = event_type
        self._target: EventDispatcherBase = None

Constructor.

Args:
event_type (str): The type - string identifier - of the event.
Must not be `None` or empty string.


def init_c_overturn(step):
    (rbot, rtop) = misc.get_rbounds(step)
    xieut = step.sdat.par['tracersin']['fe_eut']
    k_fe = step.sdat.par['tracersin']['k_fe']
    xi0l = step.sdat.par['tracersin']['fe_cont']
    xi0s = (k_fe * xi0l)
    xired = (xi0l / xieut)
    rsup = (((rtop ** 3) - ((xired ** (1 / (1 - k_fe))) * ((rtop ** 3) - (rbot ** 3)))) ** (1 / 3))

    def initprof(rpos):
        'Theoretical initial profile.'
        if (rpos < rsup):
            return (xi0s * ((((rtop ** 3) - (rbot ** 3)) / ((rtop ** 3) - (rpos ** 3))) ** (1 - k_fe)))
        return xieut
    rad = np.linspace(rbot, rtop, 500)
    initprof = np.vectorize(initprof)
    return (initprof(rad), rad)

Initial concentration.

This compute the resulting composition profile if fractional
crystallization of a SMO is assumed.

Args:
step (:class:`~stagpy.stagyydata._Step`): a step of a StagyyData
instance.
Returns:
tuple of :class:`numpy.array`: the composition and the radial position
at which it is evaluated.


def get_first_content(el_list, alt=None, strip=True):

    if not el_list:
        return alt

    content = el_list[0].getContent()

    if strip:
        content = content.strip()

    if not content:
        return alt

    return content

Return content of the first element in `el_list` or `alt`. Also return `alt`
if the content string of first element is blank.

Args:
el_list (list): List of HTMLElement objects.
alt (default None): Value returner when list or content is blank.
strip (bool, default True): Call .strip() to content.

Returns:
str or alt: String representation of the content of the first element \
or `alt` if not found.


def forward(self, hidden_states):
    hidden_states = hidden_states.transpose(-1, 1)
    hidden_states = self.conv1(hidden_states)
    hidden_states = torch.relu(hidden_states)
    hidden_states = self.dropout(hidden_states)
    hidden_states = self.conv2(hidden_states)
    hidden_states = hidden_states.transpose(-1, 1)
    return hidden_states

Calculate forward propagation.

Args:
hidden_states (torch.Tensor): Batch of input tensors (batch_size, time, input_channels).

Returns:
torch.Tensor: Batch of output tensors (batch_size, time, hidden_channels).


def parse_data_types_and_routes_from_doc_ref(api, doc, namespace_context, ignore_missing_entries=False):
    assert (doc is not None)
    data_types = set()
    routes = defaultdict(set)
    for match in doc_ref_re.finditer(doc):
        try:
            tag = match.group('tag')
            val = match.group('val')
            supplied_namespace = api.namespaces[namespace_context]
            if (tag == 'field'):
                if ('.' in val):
                    (type_name, __) = val.split('.', 1)
                    doc_type = supplied_namespace.data_type_by_name[type_name]
                    data_types.add(doc_type)
                else:
                    pass
            elif (tag == 'route'):
                if ('.' in val):
                    (namespace_name, val) = val.split('.', 1)
                    namespace = api.namespaces[namespace_name]
                else:
                    namespace = supplied_namespace
                try:
                    (route_name, version) = parse_route_name_and_version(val)
                except ValueError as ex:
                    raise KeyError(str(ex))
                route = namespace.routes_by_name[route_name].at_version[version]
                routes[namespace.name].add(route)
            elif (tag == 'type'):
                if ('.' in val):
                    (namespace_name, val) = val.split('.', 1)
                    doc_type = api.namespaces[namespace_name].data_type_by_name[val]
                    data_types.add(doc_type)
                else:
                    doc_type = supplied_namespace.data_type_by_name[val]
                    data_types.add(doc_type)
        except KeyError:
            if (not ignore_missing_entries):
                raise
    return (data_types, routes)

Given a documentation string, parse it and return all references to other
data types and routes.

Args:
- api: The API containing this doc ref.
- doc: The documentation string to parse.
- namespace_context: The namespace name relative to this documentation.
- ignore_missing_entries: If set, this will skip references to nonexistent data types instead
of raising an exception.

Returns:
- a tuple of referenced data types and routes


def get_or_create(self, defaults=None, **kwargs):
    try:
        return (self.get(**kwargs), False)
    except ObjectDoesNotExist:
        pass
    data = (defaults or {})
    data.update(kwargs)
    return (self._model_class(**data).blocking_save(), True)

Looks up an object with the given kwargs, creating a new one if necessary.

Args:
defaults (dict): Used when we create a new object. Must map to fields
of the model.
\*\*kwargs: Used both for filtering and new object creation.

Returns:
A tuple of (object, created), where created is a boolean variable
specifies whether the object was newly created or not.

Example:
In the following example, *code* and *name* fields are used to query the DB.

.. code-block:: python

obj, is_new = Permission.objects.get_or_create({'description': desc},
code=code, name=name)

{description: desc} dict is just for new creations. If we can't find any
records by filtering on *code* and *name*, then we create a new object by
using all of the inputs.


def __init__(self, retriever):

    self._page_token = None
    self._first_page = True
    self._retriever = retriever
    self._count = 0

Initializes an instance of an Iterator.

Args:
retriever: a function that can retrieve the next page of items.


def impad_to_multiple(img, divisor, pad_val=0):

    pad_h = int(np.ceil(img.shape[0] / divisor)) * divisor
    pad_w = int(np.ceil(img.shape[1] / divisor)) * divisor
    return impad(img, (pad_h, pad_w), pad_val)

Pad an image to ensure each edge to be multiple to some number.

Args:
img (ndarray): Image to be padded.
divisor (int): Padded image edges will be multiple to divisor.
pad_val (number or sequence): Same as :func:`impad`.

Returns:
ndarray: The padded image.


def getShareInfo(item):
    key = f'_syn_sharinfo_{item.__class__.__module__}_{item.__class__.__qualname__}'
    info = getattr(item, key, None)
    if (info is not None):
        return info
    meths = {}
    info = {'meths': meths}
    for name in dir(item):
        if name.startswith('_'):
            continue
        attr = getattr(item, name, None)
        if (not callable(attr)):
            continue
        wrapped = getattr(attr, '__syn_wrapped__', None)
        if (wrapped in unwraps):
            real = inspect.unwrap(attr)
            if inspect.isasyncgenfunction(real):
                meths[name] = {'genr': True}
                continue
        if inspect.isasyncgenfunction(attr):
            meths[name] = {'genr': True}
    try:
        setattr(item, key, info)
    except Exception as e:
        logger.exception(f'Failed to set magic on {item}')
    try:
        setattr(item.__class__, key, info)
    except Exception as e:
        logger.exception(f'Failed to set magic on {item.__class__}')
    return info

Get a dictionary of special annotations for a Telepath Proxy.

Args:
item:  Item to inspect.

Notes:
This will set the ``_syn_telemeth`` attribute on the item
and the items class, so this data is only computed once.

Returns:
dict: A dictionary of methods requiring special handling by the proxy.


class XGBoostModelHandlerDatatable(XGBoostModelHandler[datatable.Frame, PredictionResult, Union[xgboost.Booster, xgboost.XGBModel]]):

    def run_inference(self, batch: Sequence[datatable.Frame], model: Union[xgboost.Booster, xgboost.XGBModel], inference_args: Optional[dict[str, Any]]=None) -> Iterable[PredictionResult]:

        return self._inference_fn(batch, model, inference_args)

    def get_num_bytes(self, batch: Sequence[datatable.Frame]) -> int:

        return sum((sys.getsizeof(element) for element in batch))

Implementation of the ModelHandler interface for XGBoost
using datatable dataframes as input.

Example Usage::

pcoll | RunInference(
XGBoostModelHandlerDatatable(
model_class="XGBoost Model Class",
model_state="my_model_state.json")))

Args:
model_class: class of the XGBoost model that defines the model
structure.
model_state: path to a json file that contains the model's
configuration.
inference_fn: the inference function to use during RunInference.
default=default_xgboost_inference_fn


def update(self, friendly_name=None, description=None, query=None):
    self._table._load_info()
    if (query is not None):
        if isinstance(query, _query.Query):
            query = query.sql
        self._table._info['view'] = {'query': query}
    self._table.update(friendly_name=friendly_name, description=description)

Selectively updates View information.

Any parameters that are None (the default) are not applied in the update.

Args:
friendly_name: if not None, the new friendly name.
description: if not None, the new description.
query: if not None, a new query string for the View.


def default_peek(python_type, exposes):

    with_args = False
    make = python_type
    try:
        make()
    except (SystemExit, KeyboardInterrupt):
        raise
    except:
        make = lambda: python_type.__new__(python_type)
        try:
            make()
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            make = lambda args: python_type.__new__(python_type, *args)
            with_args = True
    def missing(attr):
        return AttributeError("can't set attribute '{}' ({})".format(attr, python_type))
    if with_args:
        def peek(store, container, _stack=None):
            state = []
            for attr in exposes:

                if attr in container:
                    state.append(store.peek(attr, container, _stack=_stack))
                else:
                    state.append(None)
            return make(state)
    elif '__dict__' in exposes:
        def peek(store, container, _stack=None):
            obj = make()
            for attr in container:
                val = store.peek(attr, container, _stack=_stack)
                try:
                    setattr(obj, attr, val)
                except AttributeError:
                    raise missing(attr)
            return obj
    else:
        def peek(store, container, _stack=None):
            obj = make()
            for attr in exposes:

                if attr in container:
                    val = store.peek(attr, container, _stack=_stack)
                else:
                    val = None
                try:
                    setattr(obj, attr, val)
                except AttributeError:
                    raise missing(attr)
            return obj
    return peek

Autoserializer factory.

Works best in Python 3.

Arguments:

python_type (type): type constructor.

exposes (iterable): sequence of attributes.

Returns:

callable: deserializer (`peek` routine).


def _unify_call_signature(i, dist_fn):
    if distribution_util.is_distribution_instance(dist_fn):
        return ((lambda *_: dist_fn), None)
    if (not callable(dist_fn)):
        raise TypeError('{} must be either `tfd.Distribution`-like or `callable`.'.format(dist_fn))
    args = _get_required_args(dist_fn)
    if (not args):
        return ((lambda *_: dist_fn()), ())

    @functools.wraps(dist_fn)
    def dist_fn_wrapped(*xs):
        'Calls `dist_fn` with reversed and truncated args.'
        if (i != len(xs)):
            raise ValueError('Internal Error: Unexpected number of inputs provided to {}-th distribution maker (dist_fn: {}, expected: {}, saw: {}).'.format(i, dist_fn, i, len(xs)))
        if (len(xs) < len(args)):
            raise ValueError('Internal Error: Too few inputs provided to {}-th distribution maker (dist_fn: {}, expected: {}, saw: {}).'.format(i, dist_fn, len(args), len(xs)))
        return dist_fn(*reversed(xs[(- len(args)):]))
    return (dist_fn_wrapped, args)

Creates `dist_fn_wrapped` which calls `dist_fn` with all prev nodes.

Args:
i: Python `int` corresponding to position in topologically sorted DAG.
dist_fn: Python `callable` which takes a subset of previously constructed
distributions (in reverse order) and produces a new distribution instance.

Returns:
dist_fn_wrapped: Python `callable` which takes all previous distributions
(in non reverse order) and produces a  new distribution instance.
args: `tuple` of `str` representing the arg names of `dist_fn` (and in non
wrapped, "natural" order). `None` is returned only if the input is not a
`callable`.


class Permute(Layer):

    def __init__(self, dims, **kwargs):
        super(Permute, self).__init__(**kwargs)
        self.dims = tuple(dims)
        if sorted(dims) != list(range(1, len(dims) + 1)):
            raise ValueError('Invalid permutation `dims` for Permute Layer: %s. The set of indices in `dims` must be consecutive and start from 1.' % (dims,))
        self.input_spec = InputSpec(ndim=len(self.dims) + 1)

    def compute_output_shape(self, input_shape):
        input_shape = tensor_shape.TensorShape(input_shape).as_list()
        output_shape = copy.copy(input_shape)
        for i, dim in enumerate(self.dims):
            target_dim = input_shape[dim]
            output_shape[i + 1] = target_dim
        return tensor_shape.TensorShape(output_shape)

    def call(self, inputs):
        return array_ops.transpose(inputs, perm=(0,) + self.dims)

    def get_config(self):
        config = {'dims': self.dims}
        base_config = super(Permute, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

Permutes the dimensions of the input according to a given pattern.

Useful e.g. connecting RNNs and convnets.

Example:

```python
model = Sequential()
model.add(Permute((2, 1), input_shape=(10, 64)))
# now: model.output_shape == (None, 64, 10)
# note: `None` is the batch dimension
```

Args:
dims: Tuple of integers. Permutation pattern does not include the
samples dimension. Indexing starts at 1.
For instance, `(2, 1)` permutes the first and second dimensions
of the input.

Input shape:
Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.

Output shape:
Same as the input shape, but with the dimensions re-ordered according
to the specified pattern.


def to_representation(self, value):

        if not value:
            return None

        image = get_thumbnail(value, self.geometry_string, **self.options)

        try:
            request = self.context.get('request', None)
            return request.build_absolute_uri(image.url)
        except:
            try:
                return super(HyperlinkedSorlImageField, self).to_representation(image)
            except AttributeError:
                return super(HyperlinkedSorlImageField, self).to_native(image.url)

Perform the actual serialization.

Args:
value: the image to transform
Returns:
a url pointing at a scaled and cached image


def highlight(text: str, color_code: int, bold: bool=False) -> str:
    return '{}\x1b[{}m{}\x1b[0m'.format(('\x1b[1m' if bold else ''), color_code, text)

Wraps the given string with terminal color codes.

Args:
text: The content to highlight.
color_code: The color to highlight with, e.g. 'shelltools.RED'.
bold: Whether to bold the content in addition to coloring.

Returns:
The highlighted string.


def List(self, request, global_params=None):
    config = self.GetMethodConfig('List')
    return self._RunMethod(config, request, global_params=global_params)

List all GitHubEnterpriseConfigs for a given project.

Args:
request: (CloudbuildProjectsGithubEnterpriseConfigsListRequest) input message
global_params: (StandardQueryParameters, default: None) global arguments
Returns:
(ListGithubEnterpriseConfigsResponse) The response message.


def _EnforceProcessMemoryLimit(self, memory_limit):
    if resource:
        if (memory_limit is None):
            memory_limit = (((4 * 1024) * 1024) * 1024)
        elif (memory_limit == 0):
            memory_limit = resource.RLIM_INFINITY
        resource.setrlimit(resource.RLIMIT_DATA, (memory_limit, memory_limit))

Enforces a process memory limit.

Args:
memory_limit (int): maximum number of bytes the process is allowed
to allocate, where 0 represents no limit and None a default of
4 GiB.


def run(self, dag):

        self.layout = self.layout or self.property_set['layout']

        if self.layout is None:
            raise TranspilerError("EnlargeWithAncilla requires property_set[\"layout\"] or"
                                  " \"layout\" parameter to run")

        layout_virtual_qubits = self.layout.get_virtual_bits().keys()
        new_qregs = set(virtual_qubit[0] for virtual_qubit in layout_virtual_qubits
                        if virtual_qubit not in dag.wires)

        for qreg in new_qregs:
            dag.add_qreg(qreg)

        return dag

Extends dag with virtual qubits that are in layout but not in the circuit yet.

Args:
dag (DAGCircuit): DAG to extend.

Returns:
DAGCircuit: An extended DAG.

Raises:
TranspilerError: If there is not layout in the property set or not set at init time.


def disable_control_flow_v2(unused_msg: str) -> Callable[[_F], _F]:

    def wrapper(func: _F) -> _F:
        func._disable_control_flow_v2 = True
        return func
    return wrapper

Decorator for a function in a with_control_flow_v2 enabled test class.

Blocks the function from being run with v2 control flow ops.

Args:
unused_msg: Reason for disabling.

Returns:
The wrapped function with _disable_control_flow_v2 attr set to True.


def xml(self):
    self.pendingvalidation()
    E = ElementMaker(namespace='http:
    attribs = {}
    attribs['{http:
    attribs['version'] = FOLIAVERSION
    attribs['generator'] = ('pynlpl.formats.folia-v' + LIBVERSION)
    metadataattribs = {}
    metadataattribs[(('{' + NSFOLIA) + '}type')] = self.metadatatype
    if isinstance(self.metadata, ExternalMetaData):
        metadataattribs[(('{' + NSFOLIA) + '}src')] = self.metadata.url
    e = E.FoLiA(E.metadata(E.annotations(*self.xmldeclarations()), *self.xmlmetadata(), **metadataattribs), **attribs)
    for text in self.data:
        e.append(text.xml())
    return e

Serialise the document to XML.

Returns:
lxml.etree.Element

See also:
:meth:`Document.xmlstring`


def traverse_nodes(self, node_set, depth=0):
    tab = '  '
    result = list()
    for n in node_set:
        repr = (n if (self.nodes[n]['type'] == 'variable') else f"{n}{inspect.signature(self.nodes[n]['lambda_fn'])}")
        result.append(f'{(tab * depth)}{repr}')
        result.extend(self.traverse_nodes(self.successors(n), depth=(depth + 1)))
    return result

BFS traversal of nodes that returns name traversal as large string.

Args:
node_set: Set of input nodes to begin traversal.
depth: Current traversal depth for child node viewing.

Returns:
type: String containing tabbed traversal view.


def CheckTaskToMerge(self, task):

    with self._lock:
      is_abandoned = task.identifier in self._tasks_abandoned
      is_processing = task.identifier in self._tasks_processing
      is_queued = task.identifier in self._tasks_queued

      if not is_queued and not is_processing and not is_abandoned:
        raise KeyError('Status of task {0:s} is unknown.'.format(
            task.identifier))

      return is_queued or is_processing or is_abandoned and not task.has_retry

Checks if the task should be merged.

Args:
task (Task): task.

Returns:
bool: True if the task should be merged.

Raises:
KeyError: if the task was not queued, processing or abandoned.


def put_many(self, type: Type[T], items: Iterable[T]) -> None:

        LOGGER.info("Getting SinkHandlers for \"{type}\"".format(type=type.__name__))
        try:
            handlers = self._put_types[type]
        except KeyError:
            try:
                LOGGER.info("Building new SinkHandlers for \"{type}\"".format(type=type.__name__))
                handlers = self._put_handlers(type)
            except NoConversionError:
                handlers = None
            self._get_types[type] = handlers

        LOGGER.info("Creating new PipelineContext")
        context = self._new_context()

        LOGGER.info("Sending items \"{items}\" to SourceHandlers".format(items=items))
        if handlers is not None:
            items = list(items)
            for handler in handlers:
                handler.put_many(items, context)

Puts multiple objects of the same type into the data sink. The objects may be transformed into a new type for insertion if necessary.

Args:
items: An iterable (e.g. list) of objects to be inserted into the data pipeline.


def CreateAdsWithCustomizations(client, adgroup_ids, feed_name):
    adgroup_ad_service = client.GetService('AdGroupAdService', 'v201809')
    expanded_text_ad = {'xsi_type': 'ExpandedTextAd', 'headlinePart1': ('Luxury Cruise to {=%s.Name}' % feed_name), 'headlinePart2': ('Only {=%s.Price}' % feed_name), 'description': ('Offer ends in {=countdown(%s.Date)}!' % feed_name), 'finalUrls': ['http:
    operations = [{'operator': 'ADD', 'operand': {'adGroupId': adgroup, 'ad': expanded_text_ad}} for adgroup in adgroup_ids]
    response = adgroup_ad_service.mutate(operations)
    if (response and ('value' in response)):
        for ad in response['value']:
            print(('Created an ad with ID "%s", type "%s", and status "%s".' % (ad['ad']['id'], ad['ad']['Ad.Type'], ad['status'])))
    else:
        raise errors.GoogleAdsError('No ads were added.')

Creates ExpandedTextAds that use ad customizations for specified AdGroups.

Args:
client: an AdWordsClient instance.
adgroup_ids: a list containing the AdGroup ids to add ExpandedTextAds to.
feed_name: the name of the feed used to apply customizations.

Raises:
GoogleAdsError: if no ExpandedTextAds were added.


def structure_np_to_list(data):
    if isinstance(data, np.ndarray):
        return data.tolist()
    if isinstance(data, dict):
        return {key: structure_np_to_list(value) for key, value in data.items()}
    if isinstance(data, list):
        return [structure_np_to_list(item) for item in data]
    if isinstance(data, (int, float, str, bytes)):
        return data
    raise ValueError(f'Non supported type {type(data)}')

Apply a function to a recursive structure of dict and list.

Args:
data: The data to apply the function to.

Returns:
The data with the function applied.


def _AddHeader(self, fp):
    text = textwrap.wrap(textwrap.dedent(self.config_header), break_on_hyphens=False)
    fp.write('\n'.join([('
    fp.write('\n\n')

Create a file header in the config.

Args:
fp: int, a file pointer for writing the header.


def rot90(array, k=1, axes=(0, 1)):
    array = convert_to_tensor(array)
    if array.ndim < 2:
        raise ValueError(f'Input array must have at least 2 dimensions. Received: array.ndim={array.ndim}')
    if len(axes) != 2 or axes[0] == axes[1]:
        raise ValueError(f'Invalid axes: {axes}. Axes must be a tuple of two different dimensions.')
    axes = tuple((axis if axis >= 0 else array.ndim + axis for axis in axes))
    if not builtins.all((0 <= axis < array.ndim for axis in axes)):
        raise ValueError(f'Invalid axes {axes} for tensor with {array.ndim} dimensions')
    rotated = torch.rot90(array, k=k, dims=axes)
    if isinstance(array, np.ndarray):
        rotated = rotated.cpu().numpy()
    return rotated

Rotate an array by 90 degrees in the specified plane using PyTorch.

Args:
array: Input tensor
k: Number of 90-degree rotations (default=1)
axes: Tuple of two axes that define the
plane of rotation (defaults to `(0, 1)`).

Returns:
Rotated tensor


def heightmap_clamp(hm: np.ndarray, mi: float, ma: float) -> None:
    hm.clip(mi, ma)

Clamp all values on this heightmap between ``mi`` and ``ma``

Args:
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions.
mi (float): The lower bound to clamp to.
ma (float): The upper bound to clamp to.

.. deprecated:: 2.0
Do ``hm.clip(mi, ma)`` instead.


def dprintx(passeditem, special=False):
    if DEBUGALL:
        if special:
            from pprint import pprint
            pprint(passeditem)
        else:
            print(('%s%s%s' % (C_TI, passeditem, C_NORM)))

Print Text if DEBUGALL set, optionally with PrettyPrint.

Args:
passeditem (str): item to print
special (bool): determines if item prints with PrettyPrint
or regular print.


def get_parameters(params=None, path='', grad_only=True):
    global current_scope
    if (params is None):
        params = OrderedDict()
    for (k, v) in iteritems(current_scope):
        if isinstance(v, dict):
            with parameter_scope(k):
                params = get_parameters(params, ('/'.join([path, k]) if path else k), grad_only=grad_only)
        else:
            assert isinstance(v, nn.Variable)
            if ((not grad_only) or v.need_grad):
                params[('/'.join([path, k]) if path else k)] = v
    return params

Get parameter Variables under the current parameter scope.

Args:
params (dict): Internal use. User doesn't set it manually.
path (str): Internal use.  User doesn't set it manually.
grad_only (bool): Retrieve all parameters under the current scope if
False, while only parameters with need_grad=True are retrieved
if True.

Returns:
dict: {:obj:`str` : :obj:`~nnabla.Variable`}


def depth_april_average_ground_temperature(self, value=None):

        if value is not None:
            try:
                value = float(value)
            except ValueError:
                raise ValueError(
                    'value {} need to be of type float '
                    'for field `depth_april_average_ground_temperature`'.format(value))

        self._depth_april_average_ground_temperature = value

Corresponds to IDD Field `depth_april_average_ground_temperature`

Args:
value (float): value for IDD Field `depth_april_average_ground_temperature`
Unit: C
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

Raises:
ValueError: if `value` is not a valid value


def __init__(self, component=None, action=None, target=None, args=None, filename=None, lineno=None, error=None, capacity=None):
    self.component = component
    self._action = action
    self._target = target
    self.args = args
    self._filename = filename
    self._lineno = lineno
    self._error = error
    self._separator = False
    self._capacity = capacity

Instantiates a FireTraceElement.

Args:
component: The result of this element of the trace.
action: The type of action (e.g. instantiating a class) taking place.
target: (string) The name of the component being acted upon.
args: The args consumed by the represented action.
filename: The file in which the action is defined, or None if N/A.
lineno: The line number on which the action is defined, or None if N/A.
error: The error represented by the action, or None if N/A.
capacity: (bool) Whether the action could have accepted additional args.


def input_fn(is_training, data_dir, batch_size, num_epochs=1, num_gpus=None, dtype=tf.float32):
    mlperf_log.resnet_print(key=mlperf_log.INPUT_ORDER)
    filenames = get_filenames(is_training, data_dir)
    dataset = tf.data.Dataset.from_tensor_slices(filenames)
    if is_training:
        dataset = dataset.shuffle(buffer_size=_NUM_TRAIN_FILES)
    dataset = dataset.flat_map(tf.data.TFRecordDataset)
    return resnet_run_loop.process_record_dataset(dataset=dataset, is_training=is_training, batch_size=batch_size, shuffle_buffer=_SHUFFLE_BUFFER, parse_record_fn=parse_record, num_epochs=num_epochs, num_gpus=num_gpus, examples_per_epoch=(_NUM_IMAGES['train'] if is_training else None), dtype=dtype)

Input function which provides batches for train or eval.

Args:
is_training: A boolean denoting whether the input is for training.
data_dir: The directory containing the input data.
batch_size: The number of samples per batch.
num_epochs: The number of epochs to repeat the dataset.
num_gpus: The number of gpus used for training.
dtype: Data type to use for images/features

Returns:
A dataset that can be used for iteration.


def is_chief(cluster_spec=None, task_type=None, task_id=None):
    if has_worker_context():
        return dc_context.get_current_worker_context().is_chief
    _validate_cluster_spec(cluster_spec, task_type, task_id)
    cluster_spec = normalize_cluster_spec(cluster_spec).as_dict()
    if task_type == 'chief' or task_type == 'evaluator':
        return True
    if 'chief' not in cluster_spec and task_type == 'worker' and (task_id == 0):
        return True
    return False

Returns whether the given task is chief in the cluster.

Since there is at most one evaluator and the evaluator itself should be
independent of the training cluster, the evaluator job is also a chief job on
its own.

If this is currently running under a `_WorkerContext` of distribute
coordinator, the arguments can be omitted as the result is already available.

Args:
cluster_spec: a dict, `ClusterDef` or `ClusterSpec` object specifying the
cluster configurations.
task_type: the task type in the cluster.
task_id: the task id in the cluster.

Returns:
a boolean indicating whether the given task is chief.

Raises:
ValueError: if `task_type` is not in the `cluster_spec` or `task_id` exceeds
the maximum id of the `task_type`.


def ToScriptHash(self, address):

        if len(address) == 34:
            if address[0] == 'A':
                data = b58decode(address)
                if data[0] != self.AddressVersion:
                    raise ValueError('Not correct Coin Version')

                checksum = Crypto.Default().Hash256(data[:21])[:4]
                if checksum != data[21:]:
                    raise Exception('Address format error')
                return UInt160(data=data[1:21])
            else:
                raise Exception('Address format error')
        else:
            raise ValueError('Not correct Address, wrong length.')

Retrieve the script_hash based from an address.

Args:
address (str): a base58 encoded address.

Raises:
ValuesError: if an invalid address is supplied or the coin version is incorrect
Exception: if the address string does not start with 'A' or the checksum fails

Returns:
UInt160: script hash.


def set_metadata(self, entity_type, entity_id, metadata):
    if (not is_valid_uuid(entity_id)):
        raise StorageArgumentException('Invalid UUID for entity_id: {0}'.format(entity_id))
    if (not isinstance(metadata, dict)):
        raise StorageArgumentException('The metadata was not provided as a dictionary')
    return self._authenticated_request.to_endpoint('{}/{}/metadata/'.format(entity_type, entity_id)).with_json_body(metadata).return_body().post()

Set metadata for an entity.

Args:
entity_type (str): Type of the entity. Admitted values: ['project',
'folder', 'file'].
entity_id (str): The UUID of the entity to be modified.
metadata (dict): A dictionary of key/value pairs to be written as
metadata.

Warning:
It will replace all existing metadata with the provided dictionary.

Returns:
A dictionary of the updated metadata::

{
u'bar': u'200',
u'foo': u'100'
}

Raises:
StorageArgumentException: Invalid arguments
StorageForbiddenException: Server response code 403
StorageNotFoundException: Server response code 404
StorageException: other 400-600 error codes


def get_file_list(self):
    if os.path.isdir(self.root_path):
        return [os.path.join(self.root_path, f) for f in os.listdir(self.root_path) if os.path.isfile(os.path.join(self.root_path, f))]
    else:
        return [self.root_path]

Retrieve the list of absolute paths to all the files in this data source.

Returns:
List[str] List of absolute paths.


def allzeros(msg):

    d = hex2bin(data(msg))

    if bin2int(d) > 0:
        return False
    else:
        return True

check if the data bits are all zeros

Args:
msg (String): 28 bytes hexadecimal message string

Returns:
bool: True or False


def run_census(flags_obj, ctx):

  train_file = os.path.join(flags_obj.data_dir, census_dataset.TRAINING_FILE)
  test_file = os.path.join(flags_obj.data_dir, census_dataset.EVAL_FILE)

  def train_input_fn():
    return census_dataset.input_fn(
        train_file, flags_obj.epochs_between_evals, True, flags_obj.batch_size)

  def eval_input_fn():
    return census_dataset.input_fn(test_file, 1, False, flags_obj.batch_size)

  tensors_to_log = {
      'average_loss': '{loss_prefix}head/truediv',
      'loss': '{loss_prefix}head/weighted_loss/Sum'
  }

  model_helpers.apply_clean(flags.FLAGS)
  model = build_estimator(
      model_dir=flags_obj.model_dir, model_type=flags_obj.model_type,
      model_column_fn=census_dataset.build_model_columns,
      inter_op=flags_obj.inter_op_parallelism_threads,
      intra_op=flags_obj.intra_op_parallelism_threads,
      ctx=ctx)

  loss_prefix = LOSS_PREFIX.get(flags_obj.model_type, '')
  tensors_to_log = {k: v.format(loss_prefix=loss_prefix)
                    for k, v in tensors_to_log.items()}
  train_hooks = hooks_helper.get_train_hooks(
      flags_obj.hooks, model_dir=flags_obj.model_dir,
      batch_size=flags_obj.batch_size, tensors_to_log=tensors_to_log)

  train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, hooks=train_hooks)
  eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)
  tf.estimator.train_and_evaluate(model, train_spec, eval_spec)

Construct all necessary functions and call run_loop.

Args:
flags_obj: Object containing user specified flags.


def check_file(self, fs, info):
    if ((self.exclude is not None) and fs.match(self.exclude, info.name)):
        return False
    return fs.match(self.filter, info.name)

Check if a filename should be included.

Override to exclude files from the walk.

Arguments:
fs (FS): A filesystem instance.
info (Info): A resource info object.

Returns:
bool: `True` if the file should be included.


def expo(base=2, factor=1, max_value=None):
    n = 0
    while True:
        a = (factor * (base ** n))
        if ((max_value is None) or (a < max_value)):
            (yield a)
            n += 1
        else:
            (yield max_value)

Generator for exponential decay.

Args:
base: The mathematical base of the exponentiation operation
factor: Factor to multiply the exponentation by.
max_value: The maximum value to yield. Once the value in the
true exponential sequence exceeds this, the value
of max_value will forever after be yielded.


def convert_drive(self, shift, instruction):

        command_dict = {
            'name': instruction.command.name,
            't0': shift+instruction.start_time,
            'ch': instruction.channels[0].name
        }
        return self._qobj_model(**command_dict)

Return converted `PulseInstruction`.

Args:
shift(int): Offset time.
instruction (PulseInstruction): drive instruction.
Returns:
dict: Dictionary of required parameters.


def ApplyParsersToResponses(parser_factory, responses, flow_obj):
    knowledge_base = flow_obj.state.knowledge_base
    parsed_responses = []
    if parser_factory.HasSingleResponseParsers():
        for response in responses:
            for parser in parser_factory.SingleResponseParsers():
                parsed_responses.extend(parser.ParseResponse(knowledge_base, response, flow_obj.args.path_type))
    for parser in parser_factory.MultiResponseParsers():
        parsed_responses.extend(parser.ParseResponses(knowledge_base, responses))
    has_single_file_parsers = parser_factory.HasSingleFileParsers()
    has_multi_file_parsers = parser_factory.HasMultiFileParsers()
    if (has_single_file_parsers or has_multi_file_parsers):
        precondition.AssertIterableType(responses, rdf_client_fs.StatEntry)
        pathspecs = [response.pathspec for response in responses]
        if data_store.RelationalDBEnabled():
            filedescs = []
            for pathspec in pathspecs:
                client_path = db.ClientPath.FromPathSpec(flow_obj.client_id, pathspec)
                filedescs.append(file_store.OpenFile(client_path))
        else:
            filedescs = MultiOpenAff4File(flow_obj, pathspecs)
    if has_single_file_parsers:
        for (response, filedesc) in zip(responses, filedescs):
            for parser in parser_factory.SingleFileParsers():
                parsed_responses.extend(parser.ParseFile(knowledge_base, response.pathspec, filedesc))
    if has_multi_file_parsers:
        for parser in parser_factory.MultiFileParsers():
            parsed_responses.extend(parser.ParseFiles(knowledge_base, pathspecs, filedescs))
    return (parsed_responses or responses)

Parse responses with applicable parsers.

Args:
parser_factory: A parser factory for specific artifact.
responses: A list of responses from the client.
flow_obj: An artifact collection flow.

Returns:
A list of (possibly parsed) responses.


def get_route_lines_route(self, **kwargs):
    select_date = ('%02d/%02d/%d' % (kwargs.get('day', '01'), kwargs.get('month', '01'), kwargs.get('year', '1970')))
    params = {'SelectDate': select_date, 'Lines': util.ints_to_string(kwargs.get('lines', []))}
    result = self.make_request('geo', 'get_route_lines_route', **params)
    if (not util.check_result(result)):
        return (False, result.get('resultDescription', 'UNKNOWN ERROR'))
    values = util.response_list(result, 'resultValues')
    return (True, [emtype.RouteLinesItem(**a) for a in values])

Obtain itinerary for one or more lines in the given date.

Args:
day (int): Day of the month in format DD.
The number is automatically padded if it only has one digit.
month (int): Month number in format MM.
The number is automatically padded if it only has one digit.
year (int): Year number in format YYYY.
lines (list[int] | int): Lines to query, may be empty to get
all the lines.

Returns:
Status boolean and parsed response (list[RouteLinesItem]), or message
string in case of error.


def generate(self):

        result = self._gen(self.optimized, self.splitstring)
        if self.splitstring and result is not None:
            result = result[1:]
        return result

Generates a new random string from the start symbol
Args:
None
Returns:
str: The generated string


def match(self, url):
    try:
        urlSchemes = self._urlSchemes.itervalues()
    except AttributeError:
        urlSchemes = self._urlSchemes.values()
    for urlScheme in urlSchemes:
        if urlScheme.match(url):
            return True
    return False

Try to find if url matches against any of the schemes within this
endpoint.

Args:
url: The url to match against each scheme

Returns:
True if a matching scheme was found for the url, False otherwise


def parse_vep_header(vcf_obj):

    vep_header = []

    if 'CSQ' in vcf_obj:

        csq_info = vcf_obj['CSQ']
        format_info = parse_header_format(csq_info['Description'])
        vep_header = [key.upper() for key in format_info.split('|')]

    return vep_header

Return a list with the VEP header

The vep header is collected from CSQ in the vcf file
All keys are capitalized

Args:
vcf_obj(cyvcf2.VCF)

Returns:
vep_header(list)


def plot_grid(step):
    rad = get_rprof(step, 'r')[0]
    drad = get_rprof(step, 'dr')[0]
    (_, unit) = step.sdat.scale(1, 'm')
    if unit:
        unit = ' ({})'.format(unit)
    (fig, (ax1, ax2)) = plt.subplots(2, sharex=True)
    ax1.plot(rad, '-ko')
    ax1.set_ylabel(('$r$' + unit))
    ax2.plot(drad, '-ko')
    ax2.set_ylabel(('$dr$' + unit))
    ax2.set_xlim([(- 0.5), (len(rad) - 0.5)])
    ax2.set_xlabel('Cell number')
    misc.saveplot(fig, 'grid', step.istep)

Plot cell position and thickness.

The figure is call grid_N.pdf where N is replace by the step index.

Args:
step (:class:`~stagpy.stagyydata._Step`): a step of a StagyyData
instance.


def to_polars(evset: EventSet, tp_string_to_pl_string: bool=True, timestamp_to_datetime: bool=True, timestamps: bool=True) -> 'pl.DataFrame':
    pl = import_pl()
    timestamp_key = 'timestamp'
    index_names = evset.schema.index_names()
    feature_names = evset.schema.feature_names()
    column_names = index_names + feature_names
    if timestamps:
        column_names += [timestamp_key]
    data_dict = {column_name: [] for column_name in column_names}
    for index, data in evset.data.items():
        assert isinstance(index, tuple)
        if timestamps:
            timestamps_data = data.timestamps
            if evset.schema.is_unix_timestamp and timestamp_to_datetime:
                datetime_series = pl.from_epoch(pl.Series(timestamps_data), time_unit='s')
                data_dict[timestamp_key].extend(datetime_series)
            else:
                data_dict[timestamp_key].extend(timestamps_data)
        for feature_name, feature in zip(feature_names, data.features):
            data_dict[feature_name].extend(feature)
        num_timestamps = len(data.timestamps)
        for index_name, index_item in zip(index_names, index):
            data_dict[index_name].extend([index_item] * num_timestamps)
    for col_name, col_data in data_dict.items():
        data_dict[col_name] = pl.Series(col_data)
    if tp_string_to_pl_string:
        for feature in evset.schema.features:
            if feature.dtype == DType.STRING:
                data_dict[feature.name] = data_dict[feature.name].cast(pl.Utf8)
        for index in evset.schema.indexes:
            if index.dtype == DType.STRING:
                data_dict[index.name] = data_dict[index.name].cast(pl.Utf8)
    return pl.DataFrame(data_dict)

Converts an  [`EventSet`][temporian.EventSet] to a Polars DataFrame.

Usage example:
```python
>>> from datetime import datetime

>>> evset = tp.event_set(
...     timestamps=[datetime(2015, 1, 1), datetime(2015, 1, 2)],
...     features={
...         "feature_1": [0.5, 0.6],
...         "my_index": ["red", "yellow"],
...    },
...    indexes=["my_index"],
... )

>>> df = tp.to_polars(evset)

```

Args:
evset: Input EventSet.
timestamp_to_datetime: If true, convert epoch timestamps to Polars Date objects.
timestamps: If true, include the timestamps as a column in the DataFrame.
tp_string_to_pl_string: If true, cast Temporian strings to Polars Object.

Returns:
A Polars DataFrame created from the EventSet.


def adversary(self, name, **kwargs):

        group_obj = Adversary(name, **kwargs)
        return self._group(group_obj)

Add Adversary data to Batch object.

Args:
name (str): The name for this Group.
date_added (str, kwargs): The date timestamp the Indicator was created.
xid (str, kwargs): The external id for this Group.

Returns:
obj: An instance of Adversary.


def get_data_location(self, catalog_id):
    try:
        record = self.get(catalog_id)
    except:
        return None
    if (('Landsat8' in record['type']) and ('LandsatAcquisition' in record['type'])):
        bucket = record['properties']['bucketName']
        prefix = record['properties']['bucketPrefix']
        return ((('s3:
    if ('DigitalGlobeAcquisition' in record['type']):
        o = Ordering()
        res = o.location([catalog_id])
        return res['acquisitions'][0]['location']
    return None

Find and return the S3 data location given a catalog_id.

Args:
catalog_id: The catalog ID

Returns:
A string containing the s3 location of the data associated with a catalog ID.  Returns
None if the catalog ID is not found, or if there is no data yet associated with it.


def add_update_resources(self, resources, ignore_datasetid=False):

        if not isinstance(resources, list):
            raise HDXError('Resources should be a list!')
        for resource in resources:
            self.add_update_resource(resource, ignore_datasetid)

Add new or update existing resources with new metadata to the dataset

Args:
resources (List[Union[hdx.data.resource.Resource,Dict,str]]): A list of either resource ids or resources metadata from either Resource objects or dictionaries
ignore_datasetid (bool): Whether to ignore dataset id in the resource. Defaults to False.

Returns:
None


def one_or_more(e, delimiter=None):

    if delimiter is None:
        delimiter = lambda s, grm, pos: (s, Ignore, (pos, pos))
    msg = 'Expected one or more of: {}'.format(repr(e))
    def match_one_or_more(s, grm=None, pos=0):
        start = pos
        s, obj, span = e(s, grm, pos)
        pos = span[1]
        data = [] if obj is Ignore else [obj]
        try:
            while True:
                s, obj, span = delimiter(s, grm, pos)
                pos = span[1]
                if obj is not Ignore:
                    data.append(obj)
                s, obj, span = e(s, grm, pos)
                pos = span[1]
                if obj is not Ignore:
                    data.append(obj)
        except PegreError:
            pass
        return PegreResult(s, data, (start, pos))
    return match_one_or_more

Create a PEG function to match one or more expressions.

Args:
e: the expression to match
delimiter: an optional expression to match between the
primary *e* matches.


def _evaluateTFLiteModel(self, tflite_model, input_data, input_shapes=None):
    interpreter = Interpreter(model_content=tflite_model)
    input_details = interpreter.get_input_details()
    if input_shapes:
        for idx, (shape_signature, final_shape) in enumerate(input_shapes):
            self.assertTrue((input_details[idx]['shape_signature'] == shape_signature).all())
            index = input_details[idx]['index']
            interpreter.resize_tensor_input(index, final_shape, strict=True)
    interpreter.allocate_tensors()
    output_details = interpreter.get_output_details()
    input_details = interpreter.get_input_details()
    for input_tensor, tensor_data in zip(input_details, input_data):
        interpreter.set_tensor(input_tensor['index'], tensor_data.numpy())
    interpreter.invoke()
    return [interpreter.get_tensor(details['index']) for details in output_details]

Evaluates the model on the `input_data`.

Args:
tflite_model: TensorFlow Lite model.
input_data: List of EagerTensor const ops containing the input data for
each input tensor.
input_shapes: List of tuples representing the `shape_signature` and the
new shape of each input tensor that has unknown dimensions.

Returns:
[np.ndarray]


def add_imported_namespace(self, namespace, imported_alias=False, imported_data_type=False, imported_annotation=False, imported_annotation_type=False):
    assert (self.name != namespace.name), 'Namespace cannot import itself.'
    reason = self._imported_namespaces.setdefault(namespace, _ImportReason())
    if imported_alias:
        reason.alias = True
    if imported_data_type:
        reason.data_type = True
    if imported_annotation:
        reason.annotation = True
    if imported_annotation_type:
        reason.annotation_type = True

Keeps track of namespaces that this namespace imports.

Args:
namespace (Namespace): The imported namespace.
imported_alias (bool): Set if this namespace references an alias
in the imported namespace.
imported_data_type (bool): Set if this namespace references a
data type in the imported namespace.
imported_annotation (bool): Set if this namespace references a
annotation in the imported namespace.
imported_annotation_type (bool): Set if this namespace references an
annotation in the imported namespace, possibly indirectly (by
referencing an annotation elsewhere that has this type).


def match_tracks(self, model_tracks, obs_tracks, unique_matches=True, closest_matches=False):
    if unique_matches:
        pairings = self.track_matcher.match_tracks(model_tracks, obs_tracks, closest_matches=closest_matches)
    else:
        pairings = self.track_matcher.neighbor_matches(model_tracks, obs_tracks)
    return pairings

Match forecast and observed tracks.

Args:
model_tracks:
obs_tracks:
unique_matches:
closest_matches:

Returns:


def run_shell_cmd(args):
    proc = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return proc.communicate()

Executes shell commands and returns output.

Args:
args: String of shell commands to run.

Returns:
Tuple output (stdoutdata, stderrdata) from running the shell commands.


def merge(self, status: 'Status[Input, Output]') -> 'Status[Input, Output]':
    if ((status is None) or (status.farthest is None)):
        pass
    elif (self.farthest is None):
        self.farthest = status.farthest
        self.expected = status.expected
    elif (status.farthest.position < self.farthest.position):
        pass
    elif (status.farthest.position > self.farthest.position):
        self.farthest = status.farthest
        self.expected = status.expected
    else:
        self.expected = (status.expected + self.expected)
    return self

Merge the failure message from another status into this one.

Whichever status represents parsing that has gone the farthest is
retained. If both statuses have gone the same distance, then the
expected values from both are retained.

Args:
status: The status to merge into this one.

Returns:
This ``Status`` which may have ``farthest`` and ``expected``
updated accordingly.


def _get_mpr_table(self, connection, partition):
    virtual_table = partition.vid
    table = '{}_v'.format(virtual_table)
    logger.debug('Looking for materialized table of the partition.\n    partition: {}'.format(partition.name))
    table_exists = self._relation_exists(connection, table)
    if table_exists:
        logger.debug('Materialized table of the partition found.\n    partition: {}, table: {}'.format(partition.name, table))
        return table
    logger.debug('Looking for a virtual table of the partition.\n    partition: {}'.format(partition.name))
    virtual_exists = self._relation_exists(connection, virtual_table)
    if virtual_exists:
        logger.debug('Virtual table of the partition found.\n    partition: {}, table: {}'.format(partition.name, table))
        return virtual_table
    raise MissingTableError('sqlite database does not have table for mpr of {} partition.'.format(partition.vid))

Returns name of the sqlite table who stores mpr data.

Args:
connection (apsw.Connection): connection to sqlite database who stores mpr data.
partition (orm.Partition):

Returns:
str:

Raises:
MissingTableError: if partition table not found in the db.


def MakeType(name, base_classes, namespace):
    precondition.AssertType(name, str)
    if PY2:
        name = name.encode('ascii')
    return type(name, base_classes, namespace)

A compatibility wrapper for the `type` built-in function.

In Python 2 `type` (used as a type constructor) requires the name argument to
be a `bytes` object whereas in Python 3 it is required to be an `unicode`
object. Since class name is human readable text rather than arbitrary stream
of bytes, the Python 3 behaviour is considered to be the sane one.

Once support for Python 2 is dropped all invocations of this call can be
replaced with the `type` built-in.

Args:
name: A name of the type to create.
base_classes: A tuple of base classes that the returned type is supposed to
derive from.
namespace: A dictionary of methods and fields that the returned type is
supposed to contain.

Returns:
A new type with specified parameters.


def SetParseFn(fn, *arguments):

    def _Decorator(func):
        parse_fns = GetParseFns(func)
        if not arguments:
            parse_fns['default'] = fn
        else:
            for argument in arguments:
                parse_fns['named'][argument] = fn
        _SetMetadata(func, FIRE_PARSE_FNS, parse_fns)
        return func
    return _Decorator

Sets the fn for Fire to use to parse args when calling the decorated fn.

Args:
fn: The function to be used for parsing arguments.
*arguments: The arguments for which to use the parse fn. If none are listed,
then this will set the default parse function.
Returns:
The decorated function, which now has metadata telling Fire how to perform.


def __init__(self, org=None, library=None, branch=None, version_guid=None, **kwargs):

        if 'offering' in kwargs:
            raise ValueError("'offering' is not a valid field for a LibraryLocator.")

        if 'course' in kwargs:
            if library is not None:
                raise ValueError("Cannot specify both 'library' and 'course'")
            warnings.warn(
                "For LibraryLocators, use 'library' instead of 'course'.",
                DeprecationWarning,
                stacklevel=2
            )
            library = kwargs.pop('course')

        run = kwargs.pop('run', self.RUN)
        if run != self.RUN:
            raise ValueError("Invalid run. Should be '{}' or None.".format(self.RUN))

        if version_guid:
            version_guid = self.as_object_id(version_guid)

        for name, value in [['org', org], ['library', library], ['branch', branch]]:
            if not (value is None or self.ALLOWED_ID_RE.match(value)):
                raise InvalidKeyError(self.__class__,
                                      u"Special characters not allowed in field {}: '{}'".format(name, value))

        if kwargs.get('deprecated', False):
            raise InvalidKeyError(self.__class__, 'LibraryLocator cannot have deprecated=True')

        super(LibraryLocator, self).__init__(
            org=org,
            library=library,
            branch=branch,
            version_guid=version_guid,
            **kwargs
        )

        if self.version_guid is None and (self.org is None or self.library is None):
            raise InvalidKeyError(self.__class__, "Either version_guid or org and library should be set")

Construct a LibraryLocator

Args:
version_guid (string or ObjectId): optional unique id for the version
org, library: the standard definition. Optional only if version_guid given.
branch (string): the optional branch such as 'draft', 'published', 'staged', 'beta'


def flatten(vari):
    if isinstance(vari, Poly):
        shape = int(numpy.prod(vari.shape))
        return reshape(vari, (shape,))
    return numpy.array(vari).flatten()

Flatten a shapeable quantity.

Args:
vari (chaospy.poly.base.Poly, numpy.ndarray):
Shapeable input quantity.

Returns:
(chaospy.poly.base.Poly, numpy.ndarray):
Same type as ``vari`` with `len(Q.shape)==1`.

Examples:
>>> P = chaospy.reshape(chaospy.prange(4), (2,2))
>>> print(P)
[[1, q0], [q0^2, q0^3]]
>>> print(chaospy.flatten(P))
[1, q0, q0^2, q0^3]


def play_match(black_model, white_model, games, sgf_dir):
    with utils.logged_timer('Loading weights'):
        black_net = dual_net.DualNetwork(black_model)
        white_net = dual_net.DualNetwork(white_model)
    readouts = FLAGS.num_readouts
    black = MCTSPlayer(black_net, two_player_mode=True)
    white = MCTSPlayer(white_net, two_player_mode=True)
    black_name = os.path.basename(black_net.save_file)
    white_name = os.path.basename(white_net.save_file)
    for i in range(games):
        num_move = 0
        for player in [black, white]:
            player.initialize_game()
            first_node = player.root.select_leaf()
            (prob, val) = player.network.run(first_node.position)
            first_node.incorporate_results(prob, val, first_node)
        while True:
            start = time.time()
            active = (white if (num_move % 2) else black)
            inactive = (black if (num_move % 2) else white)
            current_readouts = active.root.N
            while (active.root.N < (current_readouts + readouts)):
                active.tree_search()
            if (FLAGS.verbose >= 3):
                print(active.root.position)
            if active.should_resign():
                active.set_result(((- 1) * active.root.position.to_play), was_resign=True)
                inactive.set_result(active.root.position.to_play, was_resign=True)
            if active.is_done():
                fname = '{:d}-{:s}-vs-{:s}-{:d}.sgf'.format(int(time.time()), white_name, black_name, i)
                active.set_result(active.root.position.result(), was_resign=False)
                with gfile.GFile(os.path.join(sgf_dir, fname), 'w') as _file:
                    sgfstr = sgf_wrapper.make_sgf(active.position.recent, active.result_string, black_name=black_name, white_name=white_name)
                    _file.write(sgfstr)
                print('Finished game', i, active.result_string)
                break
            move = active.pick_move()
            active.play_move(move)
            inactive.play_move(move)
            dur = (time.time() - start)
            num_move += 1
            if ((FLAGS.verbose > 1) or ((FLAGS.verbose == 1) and ((num_move % 10) == 9))):
                timeper = ((dur / readouts) * 100.0)
                print(active.root.position)
                print(('%d: %d readouts, %.3f s/100. (%.2f sec)' % (num_move, readouts, timeper, dur)))

Plays matches between two neural nets.

Args:
black_model: Path to the model for black player
white_model: Path to the model for white player


def guess_leb_size(path):
    f = open(path, 'rb')
    f.seek(0, 2)
    file_size = (f.tell() + 1)
    f.seek(0)
    block_size = None
    for _ in range(0, file_size, FILE_CHUNK_SZ):
        buf = f.read(FILE_CHUNK_SZ)
        for m in re.finditer(UBIFS_NODE_MAGIC, buf):
            start = m.start()
            chdr = nodes.common_hdr(buf[start:(start + UBIFS_COMMON_HDR_SZ)])
            if (chdr and (chdr.node_type == UBIFS_SB_NODE)):
                sb_start = (start + UBIFS_COMMON_HDR_SZ)
                sb_end = (sb_start + UBIFS_SB_NODE_SZ)
                if (chdr.len != len(buf[sb_start:sb_end])):
                    f.seek(sb_start)
                    buf = f.read(UBIFS_SB_NODE_SZ)
                else:
                    buf = buf[sb_start:sb_end]
                sbn = nodes.sb_node(buf)
                block_size = sbn.leb_size
                f.close()
                return block_size
    f.close()
    return block_size

Get LEB size from superblock

Arguments:
Str:path    -- Path to file.

Returns:
Int         -- LEB size.

Searches file for superblock and retrieves leb size.


def on_deleted(self, event):
    if (not self._event_error):
        self.logger.info(u'Change detected from deletion of: %s', event.src_path)
        self.compile_dependencies(event.src_path, include_self=False)

Called when a file or directory is deleted.

Todo:
May be bugged with inspector and sass compiler since the does not
exists anymore.

Args:
event: Watchdog event, ``watchdog.events.DirDeletedEvent`` or
``watchdog.events.FileDeletedEvent``.


def _check_approval_wrapper(self, grr_object, grr_function, *args, **kwargs):

    approval_sent = False

    while True:
      try:
        return grr_function(*args, **kwargs)
      except grr_errors.AccessForbiddenError as exception:
        print('No valid approval found: {0!s}'.format(exception))

        if approval_sent:
          print('Approval not yet granted, waiting {0:d}s'.format(
              self._CHECK_APPROVAL_INTERVAL_SEC))
          time.sleep(self._CHECK_APPROVAL_INTERVAL_SEC)
          continue

        if not self.approvers:
          message = ('GRR needs approval but no approvers specified '
                     '(hint: use --approvers)')
          self.state.add_error(message, critical=True)
          return None

        grr_object.CreateApproval(
            reason=self.reason, notified_users=self.approvers)
        approval_sent = True
        print('{0!s}: approval request sent to: {1!s} (reason: {2:s})'.format(
            grr_object, self.approvers, self.reason))

Wraps a call to GRR functions checking for approval.

Args:
grr_object: the GRR object to create the eventual approval on.
grr_function: The GRR function requiring approval.
*args: Positional arguments that are to be passed to `grr_function`.
**kwargs: Keyword arguments that are to be passed to `grr_function`.

Returns:
The return value of the execution of grr_function(*args, **kwargs).


def load(self, languages=[]):
    duckling_load = self.clojure.var('duckling.core', 'load!')
    clojure_hashmap = self.clojure.var('clojure.core', 'hash-map')
    clojure_list = self.clojure.var('clojure.core', 'list')
    if languages:
        iso_languages = [Language.convert_to_iso(lang) for lang in languages]
        duckling_load.invoke(clojure_hashmap.invoke(self.clojure.read(':languages'), clojure_list.invoke(*iso_languages)))
    else:
        duckling_load.invoke()
    self._is_loaded = True

Loads the Duckling corpus.

Languages can be specified, defaults to all.

Args:
languages: Optional parameter to specify languages,
e.g. [Duckling.ENGLISH, Duckling.FRENCH] or supported ISO 639-1 Codes (e.g. ["en", "fr"])


def _delete_from_hdx(self, object_type, id_field_name):

        if id_field_name not in self.data:
            raise HDXError('No %s field (mandatory) in %s!' % (id_field_name, object_type))
        self._save_to_hdx('delete', id_field_name)

Helper method to deletes a resource from HDX

Args:
object_type (str): Description of HDX object type (for messages)
id_field_name (str): Name of field containing HDX object identifier

Returns:
None


def decorate(self, record):
    color = 'gray'
    if (record.levelno == logging.WARNING):
        color = 'yellow'
    if (record.levelno == logging.INFO):
        color = 'green'
    if (record.levelno == logging.DEBUG):
        color = 'gray'
    if (record.levelno >= logging.ERROR):
        color = 'red'
    notify = False
    if (record.levelno >= logging.ERROR):
        nofiy = True
    payload = {'color': color, 'notify': notify, 'message_format': 'text'}
    return payload

Build up HipChat specific values for log record

Args:
record (:obj:`logging.record`): log message object

Returns:
dict: params for POST request


def get_compatible_generator_action(self, filename):

        for action in self.__generator_actions:
            if action.act_on_file(filename):
                return action

        return None

Return the **first** compatible :class:`GeneratorAction` for a given filename or ``None`` if none is found.

Args:
filename (str): The filename of the template to process.


def set_column_sizes(self, values):

        self.style['grid-template-columns'] = ' '.join(map(lambda value: (str(value) if str(value).endswith('%') else str(value) + '%') , values))

Sets the size value for each column

Args:
values (iterable of int or str): values are treated as percentage.


def __init__(self, scopes, service_account_id=None, token_maker=None,
               retry_params=None):

    if isinstance(scopes, basestring):
      scopes = [scopes]
    self.scopes = scopes
    self.service_account_id = service_account_id
    self.make_token_async = token_maker or _config.TOKEN_MAKER
    if not retry_params:
      retry_params = api_utils._get_default_retry_params()
    self.retry_params = retry_params
    self.user_agent = {'User-Agent': retry_params._user_agent}
    self.expiration_headroom = random.randint(60, 240)

Constructor.

Args:
scopes: A scope or a list of scopes.
service_account_id: Internal use only.
token_maker: An asynchronous function of the form
(scopes, service_account_id) -> (token, expires).
retry_params: An instance of api_utils.RetryParams. If None, the
default for current thread will be used.


def get_key(key, data_structure):

        if key == '/':
            return data_structure

        path = key.split('/')

        path[0] or path.pop(0)
        current_value = data_structure
        while path:
            current_key = path.pop(0)
            try:
                current_key = int(current_key)
            except ValueError:
                pass

            try:
                current_value = current_value[current_key]
            except (KeyError, IndexError):
                LOGGER.debug('failed to extract path {}'.format(key))
                return None

        return current_value

Helper method for extracting values from a nested data structure.

Args:
key (str): The path to the vales (a series of keys and indexes
separated by '/')
data_structure (dict or list): The data structure from which the
value will be extracted.

Returns:
str: The values associated with key


def from_join(cls, join: Join) -> 'ConditionalJoin':

        return cls(
            join.table_name,
            join.parent_alias,
            join.table_alias,
            join.join_type,
            join.join_field,
            join.nullable
        )

Creates a new :see:ConditionalJoin from the
specified :see:Join object.

Arguments:
join:
The :see:Join object to create the
:see:ConditionalJoin object from.

Returns:
A :see:ConditionalJoin object created from
the :see:Join object.


def set_all_pattern_variables(self, patternnumber, sp0, ti0, sp1, ti1, sp2, ti2, sp3, ti3, sp4, ti4, sp5, ti5, sp6, ti6, sp7, ti7, actual_step, additional_cycles, link_pattern):
    _checkPatternNumber(patternnumber)
    self.set_pattern_step_setpoint(patternnumber, 0, sp0)
    self.set_pattern_step_setpoint(patternnumber, 1, sp1)
    self.set_pattern_step_setpoint(patternnumber, 2, sp2)
    self.set_pattern_step_setpoint(patternnumber, 3, sp3)
    self.set_pattern_step_setpoint(patternnumber, 4, sp4)
    self.set_pattern_step_setpoint(patternnumber, 5, sp5)
    self.set_pattern_step_setpoint(patternnumber, 6, sp6)
    self.set_pattern_step_setpoint(patternnumber, 7, sp7)
    self.set_pattern_step_time(patternnumber, 0, ti0)
    self.set_pattern_step_time(patternnumber, 1, ti1)
    self.set_pattern_step_time(patternnumber, 2, ti2)
    self.set_pattern_step_time(patternnumber, 3, ti3)
    self.set_pattern_step_time(patternnumber, 4, ti4)
    self.set_pattern_step_time(patternnumber, 5, ti5)
    self.set_pattern_step_time(patternnumber, 6, ti6)
    self.set_pattern_step_time(patternnumber, 7, ti7)
    self.set_pattern_additional_cycles(patternnumber, additional_cycles)
    self.set_pattern_link_topattern(patternnumber, link_pattern)
    self.set_pattern_actual_step(patternnumber, actual_step)

Set all variables for a given pattern at one time.

Args:
* patternnumber (integer): 0-7
* sp[*n*] (float): setpoint value for step *n*
* ti[*n*] (integer??): step time for step *n*, 0-900
* actual_step (int): ?
* additional_cycles(int): ?
* link_pattern(int): ?


def inner_text(node):
    from lxml import etree
    parts = [node.text]
    for child in node.getchildren():
        parts.append(etree.tostring(child, encoding='utf-8', method='text'))
        parts.append(child.tail)
    return ''.join(map(decode_bytes, filter(None, parts)))

Returns the inner text of a given XML node, excluding tags.

Args:
node: (lxml.etree.Element): The node whose inner text is desired.

Returns:
str: The inner text of the node.


def fts_contrast2(self, fs, ft_name, inv):
    inv_fts = [self.fts(x) for x in inv if (set(fs) <= self.fts(x))]
    for a in inv_fts:
        for b in inv_fts:
            if (a != b):
                diff = (a ^ b)
                if (len(diff) == 2):
                    if all([(nm == ft_name) for (_, nm) in diff]):
                        return True
    return False

Return `True` if there is a segment in `inv` that contrasts in feature
`ft_name`.

Args:
fs (list): feature specifications used to filter `inv`.
ft_name (str): name of the feature where contrast must be present.
inv (list): collection of segments represented as Unicode segments.

Returns:
bool: `True` if two segments in `inv` are identical in features except
for feature `ft_name`


Called at the beginning of evaluation or validation.

Subclasses should override for any actions to run.

Args:
logs: Dict. Currently no data is passed to this argument for this
method but that may change in the future.


def set_sig_figs(n=4):
    u.default_format = (('.' + str(n)) + 'g')
    pd.options.display.float_format = (('{:,.' + str(n)) + '}').format

Set the number of significant figures used to print Pint, Pandas, and
NumPy quantities.

Args:
n (int): Number of significant figures to display.


def derive_annotations(self, annotations):
    cls = type(self)
    return cls(self[0], self[1], self[2], self[3], annotations, self[5])

Derives a new event from this one setting the ``annotations`` attribute.

Args:
annotations: (Sequence[Union[amazon.ion.symbols.SymbolToken, unicode]]):
The annotations associated with the derived event.

Returns:
IonEvent: The newly generated event.


def _ProduceContent(self, mods, showprivate=False, showinh=False):
    result = ''
    nestedresult = ''
    for mod in mods:
        try:
            all = mod[1].__all__
        except AttributeError:
            raise RuntimeError(('Module (%s) MUST have `__all__` defined.' % mod[1].__name__))
        if ((not showprivate) and (mod[0][0:1] == '_')):
            continue
        if (mod[0][0:2] == '__'):
            continue
        result += self._ProduceSingleContent(mod, showprivate, showinh)
    return result

An internal helper to create pages for several modules that do not have nested modules.
This will automatically generate the needed RSF to document each module module
and save the module to its own page appropriately.

Args:
mods (module): The modules to document that do not contain nested modules
showprivate (bool): A flag for whether or not to display private members

Returns:
str: The file names ready to be appended to a toctree


def _freeze_keras_model(self, output_dir):
    try:
        self._keras_model.save(output_dir, save_format='tf')
    except Exception:
        return None
    tag_set = set([_tag_constants.SERVING])
    signature_key = _signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
    graph_def, input_tensors, output_tensors, sess_graph = _freeze_saved_model(output_dir, None, None, None, tag_set, signature_key)
    self.saved_model_dir = output_dir
    self._saved_model_tags = tag_set
    self._saved_model_exported_names = [signature_key]
    self._parse_saved_model_args()
    if self.saved_model_dir:
        self._graph_def = graph_def
        self._input_tensors = input_tensors
        self._output_tensors = output_tensors
        self._debug_info_func = _build_debug_info_func(sess_graph)

Save Keras model to Saved Model format.

Args:
output_dir: The output directory to save the SavedModel.


def build(self, client, nobuild=False, usecache=True, pull=False):
    if (not nobuild):
        self.update_source_images(client, usecache=usecache, pull=pull)
    width = utils.get_console_width()
    cprint(('\n' + ('=' * width)), color='white', attrs=['bold'])
    line = ('STARTING BUILD for "%s" (image definition "%s" from %s)\n' % (self.targetname, self.imagename, self.steps[(- 1)].sourcefile))
    cprint(_centered(line, width), color='blue', attrs=['bold'])
    for (istep, step) in enumerate(self.steps):
        print(colored('* Step', 'blue'), colored(('%d/%d' % ((istep + 1), len(self.steps))), 'blue', attrs=['bold']), colored('for image', color='blue'), colored(self.imagename, color='blue', attrs=['bold']))
        if (not nobuild):
            if step.bust_cache:
                stackkey = self._get_stack_key(istep)
                if (stackkey in _rebuilt):
                    step.bust_cache = False
            step.build(client, usecache=usecache)
            print(colored('* Created intermediate image', 'green'), colored(step.buildname, 'green', attrs=['bold']), end='\n\n')
            if step.bust_cache:
                _rebuilt.add(stackkey)
    finalimage = step.buildname
    if (not nobuild):
        self.finalizenames(client, finalimage)
        line = ('FINISHED BUILDING "%s" (image definition "%s" from %s)' % (self.targetname, self.imagename, self.steps[(- 1)].sourcefile))
        cprint(_centered(line, width), color='green', attrs=['bold'])
        cprint(('=' * width), color='white', attrs=['bold'], end='\n\n')

Drives the build of the final image - get the list of steps and execute them.

Args:
client (docker.Client): docker client object that will build the image
nobuild (bool): just create dockerfiles, don't actually build the image
usecache (bool): use docker cache, or rebuild everything from scratch?
pull (bool): try to pull new versions of repository images?


def stop(self, wait=True):
    assert (not self._stopped), 'Already stopped'
    self._stopped = True
    self._tornado.stop(wait)
    self._http.stop()

Stop the Bokeh Server.

This stops and removes all Bokeh Server ``IOLoop`` callbacks, as well
as stops the ``HTTPServer`` that this instance was configured with.

Args:
fast (bool):
Whether to wait for orderly cleanup (default: True)

Returns:
None


def _CompositeMapByteStream(
      self, byte_stream, byte_offset=0, context=None, **unused_kwargs):

    elements_data_size = None
    elements_terminator = None
    number_of_elements = None

    if self._HasElementsDataSize():
      elements_data_size = self._EvaluateElementsDataSize(context)

      element_byte_size = self._element_data_type_definition.GetByteSize()
      if element_byte_size is not None:
        number_of_elements, _ = divmod(elements_data_size, element_byte_size)
      else:
        elements_terminator = (
            self._element_data_type_definition.elements_terminator)

    elif self._HasElementsTerminator():
      elements_terminator = self._data_type_definition.elements_terminator

    elif self._HasNumberOfElements():
      number_of_elements = self._EvaluateNumberOfElements(context)

    if elements_terminator is None and number_of_elements is None:
      raise errors.MappingError(
          'Unable to determine element terminator or number of elements')

    context_state = getattr(context, 'state', {})

    elements_data_offset = context_state.get('elements_data_offset', 0)
    element_index = context_state.get('element_index', 0)
    element_value = None
    mapped_values = context_state.get('mapped_values', [])
    size_hints = context_state.get('size_hints', {})
    subcontext = context_state.get('context', None)

    if not subcontext:
      subcontext = DataTypeMapContext()

    try:
      while byte_stream[byte_offset:]:
        if (number_of_elements is not None and
            element_index == number_of_elements):
          break

        if (elements_data_size is not None and
            elements_data_offset >= elements_data_size):
          break

        element_value = self._element_data_type_map.MapByteStream(
            byte_stream, byte_offset=byte_offset, context=subcontext)

        byte_offset += subcontext.byte_size
        elements_data_offset += subcontext.byte_size
        element_index += 1
        mapped_values.append(element_value)

        if (elements_terminator is not None and
            element_value == elements_terminator):
          break

    except errors.ByteStreamTooSmallError as exception:
      context_state['context'] = subcontext
      context_state['elements_data_offset'] = elements_data_offset
      context_state['element_index'] = element_index
      context_state['mapped_values'] = mapped_values
      raise errors.ByteStreamTooSmallError(exception)

    except Exception as exception:
      raise errors.MappingError(exception)

    if number_of_elements is not None and element_index != number_of_elements:
      context_state['context'] = subcontext
      context_state['elements_data_offset'] = elements_data_offset
      context_state['element_index'] = element_index
      context_state['mapped_values'] = mapped_values

      error_string = (
          'Unable to read: {0:s} from byte stream at offset: {1:d} '
          'with error: missing element: {2:d}').format(
              self._data_type_definition.name, byte_offset, element_index - 1)
      raise errors.ByteStreamTooSmallError(error_string)

    if (elements_terminator is not None and
        element_value != elements_terminator and (
            elements_data_size is None or
            elements_data_offset < elements_data_size)):
      byte_stream_size = len(byte_stream)

      size_hints[self._data_type_definition.name] = DataTypeMapSizeHint(
          byte_stream_size - byte_offset)

      context_state['context'] = subcontext
      context_state['elements_data_offset'] = elements_data_offset
      context_state['element_index'] = element_index
      context_state['mapped_values'] = mapped_values
      context_state['size_hints'] = size_hints

      error_string = (
          'Unable to read: {0:s} from byte stream at offset: {1:d} '
          'with error: unable to find elements terminator').format(
              self._data_type_definition.name, byte_offset)
      raise errors.ByteStreamTooSmallError(error_string)

    if context:
      context.byte_size = elements_data_offset
      context.state = {}

    return tuple(mapped_values)

Maps a sequence of composite data types on a byte stream.

Args:
byte_stream (bytes): byte stream.
byte_offset (Optional[int]): offset into the byte stream where to start.
context (Optional[DataTypeMapContext]): data type map context.

Returns:
tuple[object, ...]: mapped values.

Raises:
ByteStreamTooSmallError: if the byte stream is too small.
MappingError: if the data type definition cannot be mapped on
the byte stream.


def with_rank_at_least(x, rank):
    return type(x)(tf.TensorShape(x).with_rank_at_least(rank))

Returns a shape based on `x` with at least the given `rank`.

For more details, see `help(tf.TensorShape.with_rank_at_least)`.

Args:
x: object representing a shape; convertible to `tf.TensorShape`.
rank: An `int` representing the minimum rank of `x` or else an assertion is
raised.

Returns:
shape: a shape having `type(x)` but guaranteed to have at least the given
rank (or else an assertion was raised).

Raises:
ValueError: If `x` does not represent a shape with at least the given
`rank`.


def _GetISO8601String(self, structure):

    time_zone_offset = structure.time_zone_offset

    try:
      time_zone_offset_hours = int(time_zone_offset[1:3], 10)
      time_zone_offset_minutes = int(time_zone_offset[3:5], 10)
    except (IndexError, TypeError, ValueError) as exception:
      raise ValueError(
          'unable to parse time zone offset with error: {0!s}.'.format(
              exception))

    try:
      iso8601 = (
          '{0:04d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}.{6:03d}'
          '{7:s}{8:02d}:{9:02d}').format(
              structure.year, structure.month, structure.day,
              structure.hours, structure.minutes, structure.seconds,
              structure.microseconds, time_zone_offset[0],
              time_zone_offset_hours, time_zone_offset_minutes)
    except ValueError as exception:
      raise ValueError(
          'unable to format date time string with error: {0!s}.'.format(
              exception))

    return iso8601

Retrieves an ISO 8601 date time string from the structure.

The date and time values in Google Drive Sync log files are formatted as:
"2018-01-24 18:25:08,454 -0800".

Args:
structure (pyparsing.ParseResults): structure of tokens derived from a
line of a text file.

Returns:
str: ISO 8601 date time string.

Raises:
ValueError: if the structure cannot be converted into a date time string.