def subtract(x1, x2):
    if any_symbolic_tensors((x1, x2)):
        return Subtract().symbolic_call(x1, x2)
    return backend.numpy.subtract(x1, x2)

Subtract arguments element-wise.

Args:
x1: First input tensor.
x2: Second input tensor.

Returns:
Output tensor, element-wise difference of `x1` and `x2`.


def write_command(self, command: Command):
    _logger.debug('Write command.')
    data = command.to_bytes()
    (yield from self._connection.write(data))
    self._data_event_dispatcher.notify_write(data)

Write a command to the stream.

Args:
command: The command.

Coroutine.


def sql_column_like_drug(self, column_name: str) -> str:

        clauses = [
            "{col} LIKE {fragment}".format(
                col=column_name,
                fragment=sql_string_literal(f))
            for f in self.sql_like_fragments
        ]
        return "({})".format(" OR ".join(clauses))

Returns SQL like

.. code-block:: sql

(column_name LIKE '%drugname1%' OR
column_name LIKE '%drugname2%')

for the drug names that this Drug object knows about.

Args:
column_name: column name, pre-escaped if necessary

Returns:
SQL fragment as above


def export_outputs_for_mode(mode, serving_export_outputs=None, predictions=None, loss=None, metrics=None):
    if mode not in SIGNATURE_KEY_MAP:
        raise ValueError(f'Export output type not found for `mode`: {mode}. Expected one of: {list(SIGNATURE_KEY_MAP.keys())}.')
    signature_key = SIGNATURE_KEY_MAP[mode]
    if mode_keys.is_predict(mode):
        return get_export_outputs(serving_export_outputs, predictions)
    elif mode_keys.is_train(mode):
        return {signature_key: export_output_lib.TrainOutput(loss=loss, predictions=predictions, metrics=metrics)}
    else:
        return {signature_key: export_output_lib.EvalOutput(loss=loss, predictions=predictions, metrics=metrics)}

Util function for constructing a `ExportOutput` dict given a mode.

The returned dict can be directly passed to `build_all_signature_defs` helper
function as the `export_outputs` argument, used for generating a SignatureDef
map.

Args:
mode: A `ModeKeys` specifying the mode.
serving_export_outputs: Describes the output signatures to be exported to
`SavedModel` and used during serving. Should be a dict or None.
predictions: A dict of Tensors or single Tensor representing model
predictions. This argument is only used if serving_export_outputs is not
set.
loss: A dict of Tensors or single Tensor representing calculated loss.
metrics: A dict of (metric_value, update_op) tuples, or a single tuple.
metric_value must be a Tensor, and update_op must be a Tensor or Op

Returns:
Dictionary mapping the key to an `ExportOutput` object.
The key is the expected SignatureDef key for the mode.

Raises:
ValueError: if an appropriate ExportOutput cannot be found for the mode.


def finalize_variable_values(self, var_list):
    if self.use_ema:
        self._overwrite_model_variables_with_average_value(var_list)

Set the final value of model's trainable variables.

Sometimes there are some extra steps before ending the variable updates,
such as overriding the model variables with its average value.

Args:
var_list: list of model variables.


def download(self, resource_id):
    self.resource_id(str(resource_id))
    self._request_uri = '{}/download'.format(self._request_uri)

Update the request URI to download the document for this resource.

Args:
resource_id (integer): The group id.


def ParseFileObject(self, parser_mediator, file_object):
    if (file_object.read(1) != b'{'):
        raise errors.UnableToParseFile('is not a valid JSON file, missing opening brace.')
    file_object.seek(0, os.SEEK_SET)
    file_entry = parser_mediator.GetFileEntry()
    file_system = file_entry.GetFileSystem()
    json_file_path = parser_mediator.GetDisplayName()
    split_path = file_system.SplitPath(json_file_path)
    try:
        if ('containers' in split_path):
            if ('config.json' in split_path):
                self._ParseContainerConfigJSON(parser_mediator, file_object)
            if json_file_path.endswith('-json.log'):
                self._ParseContainerLogJSON(parser_mediator, file_object)
        elif ('graph' in split_path):
            if ('json' in split_path):
                self._ParseLayerConfigJSON(parser_mediator, file_object)
    except ValueError as exception:
        if (exception == 'No JSON object could be decoded'):
            raise errors.UnableToParseFile(exception)
        else:
            raise

Parses various Docker configuration and log files in JSON format.

This methods checks whether the file_object points to a docker JSON config
or log file, and calls the corresponding _Parse* function to generate
Events.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_object (dfvfs.FileIO): a file-like object.

Raises:
UnableToParseFile: when the file cannot be parsed.
ValueError: if the JSON file cannot be decoded.


def added_tokens_decoder(self) -> dict[int, AddedToken]:
    return self._tokenizer.get_added_tokens_decoder()

Returns the added tokens in the vocabulary as a dictionary of index to AddedToken.

Returns:
`Dict[str, int]`: The added tokens.


def ExamineEvent(self, mediator, event):

    pathspec = getattr(event, 'pathspec', None)
    if pathspec is None:
      return
    if self._paths_with_hashes.get(pathspec, None):

      return
    hash_attributes = {}
    for attribute_name, attribute_value in event.GetAttributes():
      if attribute_name.endswith('_hash'):
        hash_attributes[attribute_name] = attribute_value
    self._paths_with_hashes[pathspec] = hash_attributes

Analyzes an event and creates extracts hashes as required.

Args:
mediator (AnalysisMediator): mediates interactions between
analysis plugins and other components, such as storage and dfvfs.
event (EventObject): event to examine.


async def get_json(self, url, json_callback=None, **kwargs):
    if (not json_callback):
        json_callback = json.loads
    response = (await self.request(method='get', url=url, **kwargs))
    return json_callback(response)

Get a URL and return its JSON response.

Args:
url (str): URL to be requested.
json_callback (func): Custom JSON loader function. Defaults
to :meth:`json.loads`.
kwargs (dict): Additional arguments to pass through to the
request.
Returns:
response body returned by :func:`json_callback` function.


def set_headline(self, level, message, timestamp=None, now_reference=None):
    if ((self.headline is not None) and (self.headline.message == message)):
        self.headline.created = monotonic()
        self.headline.count += 1
        return
    msg_object = ServiceMessage(level, message, self._last_message_id, timestamp, now_reference)
    self.headline = msg_object
    self._last_message_id += 1

Set the persistent headline message for this service.

Args:
level (int): The level of the message (info, warning, error)
message (string): The message contents
timestamp (float): An optional monotonic value in seconds for when the message was created
now_reference (float): If timestamp is not relative to monotonic() as called from this
module then this should be now() as seen by whoever created the timestamp.


def clear_agent(self, short_name, client_id):
    if (short_name not in self.services):
        raise ArgumentError('Unknown service name', short_name=short_name)
    if (short_name not in self.agents):
        raise ArgumentError('No agent registered for service', short_name=short_name)
    if (client_id != self.agents[short_name]):
        raise ArgumentError('Client was not registered for service', short_name=short_name, client_id=client_id, current_client=self.agents[short_name])
    del self.agents[short_name]

Remove a client id from being the command handler for a service.

Args:
short_name (str): The name of the service to set an agent
for.
client_id (str): A globally unique id for the client that
should no longer receive commands for this service.


def print(self, tag=None, name=None):
    _name = name
    if (_name is None):
        _name = 'print'
    fn = streamsx.topology.functions.print_flush
    if (tag is not None):
        tag = (str(tag) + ': ')
        fn = (lambda v: streamsx.topology.functions.print_flush((tag + str(v))))
    sp = self.for_each(fn, name=_name)
    sp._op().sl = _SourceLocation(_source_info(), 'print')
    return sp

Prints each tuple to stdout flushing after each tuple.

If `tag` is not `None` then each tuple has "tag: " prepended
to it before printing.

Args:
tag: A tag to prepend to each tuple.
name(str): Name of the resulting stream.
When `None` defaults to a generated name.
Returns:
streamsx.topology.topology.Sink: Stream termination.

.. versionadded:: 1.6.1 `tag`, `name` parameters.

.. versionchanged:: 1.7
Now returns a :py:class:`Sink` instance.


def _add_monomer(self, monomer, mon_vector, move_direction):
    translate_by = (self.molecule.cart_coords[self.end] + (self.link_distance * move_direction))
    monomer.translate_sites(range(len(monomer)), translate_by)
    if (not self.linear_chain):
        self._align_monomer(monomer, mon_vector, move_direction)
    does_cross = False
    for (i, site) in enumerate(monomer):
        try:
            self.molecule.append(site.specie, site.coords, properties=site.properties)
        except:
            does_cross = True
            polymer_length = len(self.molecule)
            self.molecule.remove_sites(range((polymer_length - i), polymer_length))
            break
    if (not does_cross):
        self.length += 1
        self.end += len(self.monomer)

extend the polymer molecule by adding a monomer along mon_vector direction

Args:
monomer (Molecule): monomer molecule
mon_vector (numpy.array): monomer vector that points from head to tail.
move_direction (numpy.array): direction along which the monomer
will be positioned


def verify_dataset_shuffled(x):
    assert isinstance(x, data_types.DatasetV2)
    graph_def = get_dataset_graph_def(x)
    for node in graph_def.node:
        if node.op.startswith('ShuffleDataset'):
            return True
    for function in graph_def.library.function:
        for node in function.node_def:
            if node.op.startswith('ShuffleDataset'):
                return True
    logging.warning('Expected a shuffled dataset but input dataset `x` is not shuffled. Please invoke `shuffle()` on input dataset.')
    return False

Verifies that the dataset is shuffled.

Args:
x: Dataset passed as an input to the model.

Returns:
boolean, whether the input dataset is shuffled or not.


def set_license(self, license, **kwargs):
    data = {'license': license}
    return self.http_post('/license', post_data=data, **kwargs)

Add a new license.

Args:
license (str): The license string
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabPostError: If the server cannot perform the request

Returns:
dict: The new license information


def dense_labels_to_sparse(dense, length):
    flat_values = array_ops.reshape(dense, [-1])
    flat_indices = math_ops.range(array_ops.shape(flat_values, out_type=dtypes.int64)[0])
    mask = array_ops.sequence_mask(length, maxlen=array_ops.shape(dense)[1])
    flat_mask = array_ops.reshape(mask, [-1])
    indices = array_ops.expand_dims(array_ops.boolean_mask(flat_indices, flat_mask), 1)
    values = array_ops.boolean_mask(flat_values, flat_mask)
    sparse = sparse_tensor.SparseTensor(indices=indices, values=math_ops.cast(values, dtypes.int32), dense_shape=array_ops.shape(flat_values, out_type=dtypes.int64))
    reshaped = sparse_ops.sparse_reshape(sparse, array_ops.shape(dense))
    max_length = math_ops.reduce_max(length)
    return sparse_tensor.SparseTensor(indices=reshaped.indices, values=reshaped.values, dense_shape=[math_ops.cast(reshaped.dense_shape[0], dtypes.int64), math_ops.cast(max_length, dtypes.int64)])

Convert dense labels with sequence lengths to sparse tensor.

Args:
dense: tensor of shape [batch, max_length]
length: int tensor of shape [batch] The length of each sequence in dense.

Returns:
tf.sparse.SparseTensor with values only for the valid elements of sequences.


def _send_to_consumer(self, block):

        self._consumer.write(block)
        self._sent += len(block)
        if self._callback:
            self._callback(self._sent, self.length)

Send a block of bytes to the consumer.

Args:
block (str): Block of bytes


def as_string(self) -> str:
    if len(self._messages) != 1:
        raise ValueError('FHIRPath did not evaluate to a single string.')
    if fhir_types.is_type_or_profile_of_code(self._messages[0]):
        return codes.get_code_as_string(self._messages[0])
    return proto_utils.get_value_at_field(self._messages[0], 'value')

Returns the result as a string.

Raises:
ValueError if the `EvaluationResult` is not a single string.


def _generate_bucket_value(self, bucketing_id):

    ratio = float(self._generate_unsigned_hash_code_32_bit(bucketing_id)) / MAX_HASH_VALUE
    return math.floor(ratio * MAX_TRAFFIC_VALUE)

Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE).

Args:
bucketing_id: ID for bucketing.

Returns:
Bucket value corresponding to the provided bucketing ID.


def export(self, name=None):
    with ops.name_scope(name, '%s_lookup_table_export_values' % self.name, [self.resource_handle]):
        with ops.colocate_with(self.resource_handle):
            exported_keys, exported_values = gen_lookup_ops.lookup_table_export_v2(self.resource_handle, self._key_dtype, self._value_dtype)
    return (exported_keys, exported_values)

Returns tensors of all keys and values in the table.

Args:
name: A name for the operation (optional).

Returns:
A pair of tensors with the first tensor containing all keys and the
second tensors containing all values in the table.


def read_elastic_tensor(self):
    header_pattern = 'TOTAL ELASTIC MODULI \\(kBar\\)\\s+Direction\\s+([X-Z][X-Z]\\s+)+\\-+'
    row_pattern = ('[X-Z][X-Z]\\s+' + '\\s+'.join((['(\\-*[\\.\\d]+)'] * 6)))
    footer_pattern = '\\-+'
    et_table = self.read_table_pattern(header_pattern, row_pattern, footer_pattern, postprocess=float)
    self.data['elastic_tensor'] = et_table

Parse the elastic tensor data.

Returns:
6x6 array corresponding to the elastic tensor from the OUTCAR.


def post(self, url=None, post_data={}, parse_data=False, key=None, parameters=None, listener=None):
    return self._fetch('POST', url, post_data=post_data, parse_data=parse_data, key=key, parameters=parameters, listener=listener, full_return=True)

Issue a POST request.

Kwargs:
url (str): Destination URL
post_data (dict): Dictionary of parameter and values
parse_data (bool): If true, parse response data
key (string): If parse_data==True, look for this key when parsing data
parameters (dict): Additional GET parameters to append to the URL
listener (func): callback called when uploading a file

Returns:
dict. Response (a dict with keys: success, data, info, body)

Raises:
AuthenticationError, ConnectionError, urllib2.HTTPError, ValueError, Exception


def convert_to_experiment_list(experiments):

    exp_list = experiments

    if experiments is None:
        exp_list = []
    elif isinstance(experiments, Experiment):
        exp_list = [experiments]
    elif type(experiments) is dict:
        exp_list = [
            Experiment.from_json(name, spec)
            for name, spec in experiments.items()
        ]

    if (type(exp_list) is list
            and all(isinstance(exp, Experiment) for exp in exp_list)):
        if len(exp_list) > 1:
            logger.warning("All experiments will be "
                           "using the same SearchAlgorithm.")
    else:
        raise TuneError("Invalid argument: {}".format(experiments))

    return exp_list

Produces a list of Experiment objects.

Converts input from dict, single experiment, or list of
experiments to list of experiments. If input is None,
will return an empty list.

Arguments:
experiments (Experiment | list | dict): Experiments to run.

Returns:
List of experiments.


def serialize_ndarray_npy(o):

    with io.BytesIO() as f:
        np.save(f, o)
        f.seek(0)
        serialized = json.dumps(f.read().decode('latin-1'))
    return dict(
        _type='np.ndarray',
        npy=serialized)

Serializes a :obj:`numpy.ndarray` using numpy's built-in :obj:`save` function.
This produces totally unreadable (and very un-JSON-like) results (in "npy"
format), but it's basically guaranteed to work in 100% of cases.

Args:
o (:obj:`numpy.ndarray`): :obj:`ndarray` to be serialized.

Returns:
A dictionary that can be passed to :obj:`json.dumps`.


def get_point_group_symbol(self):
    rotations = self._space_group_data['rotations']
    if (len(rotations) == 0):
        return '1'
    return spglib.get_pointgroup(rotations)[0].strip()

Get the point group associated with the structure.

Returns:
(Pointgroup): Point group for structure.


def _destructively_move(self, dest_doc):
    if (dest_doc is self):
        raise RuntimeError('Attempted to overwrite a document with itself')
    dest_doc.clear()
    roots = []
    self._push_all_models_freeze()
    try:
        while self.roots:
            r = next(iter(self.roots))
            self.remove_root(r)
            roots.append(r)
    finally:
        self._pop_all_models_freeze()
    for r in roots:
        if (r.document is not None):
            raise RuntimeError(("Somehow we didn't detach %r" % r))
    if (len(self._all_models) != 0):
        raise RuntimeError(('_all_models still had stuff in it: %r' % self._all_models))
    for r in roots:
        dest_doc.add_root(r)
    dest_doc.title = self.title

Move all data in this doc to the dest_doc, leaving this doc empty.

Args:
dest_doc (Document) :
The Bokeh document to populate with data from this one

Returns:
None


def Draw(self, stoplist=None, triplist=None, height=520):
    output = str()
    if (not triplist):
        triplist = []
    if (not stoplist):
        stoplist = []
    if ((not self._cache) or triplist or stoplist):
        self._gheight = height
        self._tlist = triplist
        self._slist = stoplist
        self._decorators = []
        self._stations = self._BuildStations(stoplist)
        self._cache = ('%s %s %s %s' % (self._DrawBox(), self._DrawHours(), self._DrawStations(), self._DrawTrips(triplist)))
    output = ('%s %s %s %s' % (self._DrawHeader(), self._cache, self._DrawDecorators(), self._DrawFooter()))
    return output

Main interface for drawing the marey graph.

If called without arguments, the data generated in the previous call
will be used. New decorators can be added between calls.

Args:
# Class Stop is defined in transitfeed.py
stoplist: [Stop, Stop, ...]
# Class Trip is defined in transitfeed.py
triplist: [Trip, Trip, ...]

Returns:
# A string that contain a svg/xml web-page with a marey graph.
" <svg  width="1440" height="520" version="1.1" ... "


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 objects_patch(self, bucket, key, info):

    url = Api._ENDPOINT + (Api._OBJECT_PATH % (bucket, Api._escape_key(key)))
    return google.datalab.utils.Http.request(url, method='PATCH', data=info,
                                             credentials=self._credentials)

Updates the metadata associated with an object.

Args:
bucket: the name of the bucket containing the object.
key: the key of the object being updated.
info: the metadata to update.
Returns:
A parsed object information dictionary.
Raises:
Exception if there is an error performing the operation.


def create_jlink(self, args):

        jlink = pylink.JLink()
        jlink.open(args.serial_no, args.ip_addr)

        if hasattr(args, 'tif') and args.tif is not None:
            if args.tif.lower() == 'swd':
                jlink.set_tif(pylink.JLinkInterfaces.SWD)
            else:
                jlink.set_tif(pylink.JLinkInterfaces.JTAG)

        if hasattr(args, 'device') and args.device is not None:
            jlink.connect(args.device)

        return jlink

Creates an instance of a J-Link from the given arguments.

Args:
self (Command): the ``Command`` instance
args (Namespace): arguments to construct the ``JLink`` instance from

Returns:
An instance of a ``JLink``.


def nrows(self):
    if self.rank == 0:
        return None
    return self._ragged_shape[0]

The number of rows in this StructuredTensor (if rank>0).

This means the length of the outer-most dimension of the StructuredTensor.

Notice that if `self.rank > 1`, then this equals the number of rows
of the first row partition. That is,
`self.nrows() == self.row_partitions[0].nrows()`.

Otherwise `self.nrows()` will be the first dimension of the field values.

Returns:
A scalar integer `Tensor` (or `None` if `self.rank == 0`).


def _get_source_chunks(self, input_text, language=None):
    chunks = ChunkList()
    seek = 0
    result = self._get_annotations(input_text, language=language)
    tokens = result['tokens']
    language = result['language']
    for (i, token) in enumerate(tokens):
        word = token['text']['content']
        begin_offset = token['text']['beginOffset']
        label = token['dependencyEdge']['label']
        pos = token['partOfSpeech']['tag']
        if (begin_offset > seek):
            chunks.append(Chunk.space())
            seek = begin_offset
        chunk = Chunk(word, pos, label)
        if (chunk.label in _DEPENDENT_LABEL):
            chunk.dependency = (i < token['dependencyEdge']['headTokenIndex'])
        if chunk.is_punct():
            chunk.dependency = chunk.is_open_punct()
        chunks.append(chunk)
        seek += len(word)
    return (chunks, language)

Returns a chunk list retrieved from Syntax Analysis results.

Args:
input_text (str): Text to annotate.
language (:obj:`str`, optional): Language of the text.

Returns:
A chunk list. (:obj:`budou.chunk.ChunkList`)


def prepare_csv_read(data, field_names, *args, **kwargs):

    if hasattr(data, 'readlines') or isinstance(data, list):
        pass
    elif isinstance(data, basestring):
        data = open(data)
    else:
        raise TypeError('Unable to handle data of type %r' % type(data))
    return csv.DictReader(data, field_names, *args, **kwargs)

Prepare various input types for CSV parsing.

Args:
data (iter): Data to read
field_names (tuple of str): Ordered names to assign to fields

Returns:
csv.DictReader: CSV reader suitable for parsing

Raises:
TypeError: Invalid value for data


def get_botcust2():
    logger.debug('Getting new botcust2')
    params = {'botid': 'f6a012073e345a08', 'amp;skin': 'chat'}
    headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch, br', 'Accept-Language': 'en-US,en;q=0.8', 'Connection': 'keep-alive', 'DNT': '1', 'Host': 'kakko.pandorabots.com', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    logger.debug('Sending POST request')
    response = requests.post(url, params=params, headers=headers)
    logger.debug('POST response {}'.format(response))
    try:
        result = response.headers['set-cookie'][9:25]
        logger.debug('Getting botcust2 successful')
    except IndexError:
        result = False
        logger.critical('Getting botcust2 from html failed')
    return result

Gets a botcust2, used to identify a speaker with Mitsuku

Returns:
botcust2 (str): The botcust2 identifier


def hstack(gctoos, remove_all_metadata_fields=False, error_report_file=None, fields_to_remove=[], reset_ids=False):
    row_meta_dfs = []
    col_meta_dfs = []
    data_dfs = []
    srcs = []
    for g in gctoos:
        row_meta_dfs.append(g.row_metadata_df)
        col_meta_dfs.append(g.col_metadata_df)
        data_dfs.append(g.data_df)
        srcs.append(g.src)
    logger.debug('shapes of row_meta_dfs:  {}'.format([x.shape for x in row_meta_dfs]))
    all_row_metadata_df = assemble_common_meta(row_meta_dfs, fields_to_remove, srcs, remove_all_metadata_fields, error_report_file)
    all_col_metadata_df = assemble_concatenated_meta(col_meta_dfs, remove_all_metadata_fields)
    all_data_df = assemble_data(data_dfs, 'horiz')
    assert (all_data_df.shape[0] == all_row_metadata_df.shape[0]), 'Number of rows in metadata does not match number of rows in data - all_data_df.shape[0]:  {}  all_row_metadata_df.shape[0]:  {}'.format(all_data_df.shape[0], all_row_metadata_df.shape[0])
    assert (all_data_df.shape[1] == all_col_metadata_df.shape[0]), 'Number of columns in data does not match number of columns metadata - all_data_df.shape[1]:  {}  all_col_metadata_df.shape[0]:  {}'.format(all_data_df.shape[1], all_col_metadata_df.shape[0])
    if reset_ids:
        do_reset_ids(all_col_metadata_df, all_data_df, 'horiz')
    logger.info('Build GCToo of all...')
    concated = GCToo.GCToo(row_metadata_df=all_row_metadata_df, col_metadata_df=all_col_metadata_df, data_df=all_data_df)
    return concated

Horizontally concatenate gctoos.

Args:
gctoos (list of gctoo objects)
remove_all_metadata_fields (bool):  ignore/strip all common metadata when combining gctoos
error_report_file (string):  path to write file containing error report indicating
problems that occurred during hstack, mainly for inconsistencies in common metadata
fields_to_remove (list of strings): fields to be removed from the
common metadata because they don't agree across files
reset_ids (bool): set to True if sample ids are not unique

Return:
concated (gctoo object)


def __init__(self, file_system, path_spec):

    super(SQLiteBlobDirectory, self).__init__(file_system, path_spec)
    self._number_of_entries = None

Initializes a directory.

Args:
file_system (SQLiteBlobFileSystem): file system.
path_spec (SQLiteBlobPathSpec): path specification.


def __call__(self, shape, dtype=None):
    raise NotImplementedError('Initializer subclasses must implement the `__call__()` method.')

Returns a tensor object initialized as specified by the initializer.

Args:
shape: Shape of the tensor.
dtype: Optional dtype of the tensor.


def build_institute(internal_id, display_name, sanger_recipients=None, coverage_cutoff=None, frequency_cutoff=None):
    LOG.info('Building institute %s with display name %s', internal_id, display_name)
    institute_obj = Institute(internal_id=internal_id, display_name=display_name, sanger_recipients=sanger_recipients, coverage_cutoff=coverage_cutoff, frequency_cutoff=frequency_cutoff)
    for key in list(institute_obj):
        if (institute_obj[key] is None):
            institute_obj.pop(key)
    return institute_obj

Build a institute object

Args:
internal_id(str)
display_name(str)
sanger_recipients(list(str)): List with email addresses

Returns:
institute_obj(scout.models.Institute)


def default_memcache_timeout_policy(key):

    timeout = None
    if key is not None and isinstance(key, model.Key):
      modelclass = model.Model._kind_map.get(key.kind())
      if modelclass is not None:
        policy = getattr(modelclass, '_memcache_timeout', None)
        if policy is not None:
          if isinstance(policy, (int, long)):
            timeout = policy
          else:
            timeout = policy(key)
    return timeout

Default memcache timeout policy.

This defers to _memcache_timeout on the Model class.

Args:
key: Key instance.

Returns:
Memcache timeout to use (integer), or None.


def pmap(f, axis_name=None, devices=None):
    if devices is None:
        devices = accelerators()
    if not isinstance(devices, (list, tuple)):
        raise ValueError('Must pass a list or tuple of devices')
    num_devices = len(devices)
    if not num_devices:
        raise ValueError('There must be at least 1 device')
    has_tpu = bool(tpu_devices(devices))
    pmap_fn = _get_pmap_impl(f, devices, has_tpu)

    def wrapper(*args):

        if _pmap_config.devices() is not None:
            raise ValueError('Found a surrounding pmap. Nested pmap is not supported yet.')
        flattened_input_args = nest.flatten(args)
        flattened_per_device_args = [[] for _ in devices]
        for arg in flattened_input_args:
            if isinstance(arg, tensor_lib.Tensor):
                if not arg.shape.rank or arg.shape[0] != len(devices):
                    raise ValueError('Input tensors need to have a first dimension equal to the number of devices; got tensor of shape %s and %s devices' % (arg.shape, len(devices)))
                for j, device in enumerate(devices):
                    updated_arg = array_ops.gather_v2(arg, j)
                    if not has_tpu:
                        with ops.device(device):
                            updated_arg = array_ops.identity(updated_arg)
                    flattened_per_device_args[j].append(updated_arg)
            elif isinstance(arg, ShardedNdArray):
                for device_args, tensor in zip(flattened_per_device_args, arg.tensors):
                    device_args.append(tensor)
            else:
                for device_args in flattened_per_device_args:
                    device_args.append(arg)
        all_per_device_args = [nest.pack_sequence_as(args, device_args) for device_args in flattened_per_device_args]
        with pmap_config(axis_name, devices):
            results = pmap_fn(all_per_device_args)
        flattened_results = [nest.flatten(result) for result in results]
        final_tree = []
        for i in range(len(flattened_results[0])):
            tensors = []
            for j, device in enumerate(devices):
                assert isinstance(flattened_results[j][i], tensor_lib.Tensor), 'currently only tensor return items are supported'
                tensors.append(flattened_results[j][i])
            final_tree.append(ShardedNdArray(tensors))
        return nest.pack_sequence_as(results[0], final_tree)
    return wrapper

Transforms a function into a multi-device function.

The semantics are similar to JAX's pmap.

Args:
f: The function to be converted.
axis_name: Used for nested pmap, which is not supported yet.
devices: The devices over which the returned function will run.

Returns:
A function that runs the underlying function `f` on `devices`. Its arguments
can be `ShardedNdArray`s, tensors or other Python objects, and its return
values are all `ShardedNdArray`s. If an input is a tensor, the length of its
first dimension must equal the number of devices, and the tensor will be
splitted along its first dimension among the devices. If an input is an
unknown Python object, it will be replicated among the devices.


def redo(self):
    trigger_log = self._to_live_trigger_log(state=TRIGGER_LOG_STATE['NEW'])
    trigger_log.save(force_insert=True)
    self.state = TRIGGER_LOG_STATE['REQUEUED']
    self.save(update_fields=['state'])
    return trigger_log

Re-sync the change recorded in this trigger log.

Creates a ``NEW`` live trigger log from the data in this archived trigger log and sets
the state of this archived instance to ``REQUEUED``.

.. seealso:: :meth:`.TriggerLog.redo`

Returns:
The :class:`.TriggerLog` instance that was created from the data of this archived log.


def latent_dirichlet_allocation(concentration, topics_words):
    topics = ed.Dirichlet(concentration=concentration, name='topics')
    word_probs = tf.matmul(topics, topics_words)
    bag_of_words = ed.OneHotCategorical(probs=word_probs, name='bag_of_words')
    return bag_of_words

Latent Dirichlet Allocation in terms of its generative process.

The model posits a distribution over bags of words and is parameterized by
a concentration and the topic-word probabilities. It collapses per-word
topic assignments.

Args:
concentration: A Tensor of shape [1, num_topics], which parameterizes the
Dirichlet prior over topics.
topics_words: A Tensor of shape [num_topics, num_words], where each row
(topic) denotes the probability of each word being in that topic.

Returns:
bag_of_words: A random variable capturing a sample from the model, of shape
[1, num_words]. It represents one generated document as a bag of words.


def apply_encoding_options(self, min_token_count=1, limit_top_tokens=None):
    if (not self.has_vocab):
        raise ValueError('You need to build the vocabulary using `build_vocab` before using `apply_encoding_options`')
    if (min_token_count < 1):
        raise ValueError('`min_token_count` should atleast be 1')
    token_counts = list(self._token_counts.items())
    token_counts = [x for x in token_counts if (x[1] >= min_token_count)]
    if (limit_top_tokens is not None):
        token_counts.sort(key=(lambda x: x[1]), reverse=True)
        filtered_tokens = list(zip(*token_counts))[0]
        filtered_tokens = filtered_tokens[:limit_top_tokens]
    else:
        filtered_tokens = zip(*token_counts)[0]
    self.create_token_indices(filtered_tokens)

Applies the given settings for subsequent calls to `encode_texts` and `decode_texts`. This allows you to
play with different settings without having to re-run tokenization on the entire corpus.

Args:
min_token_count: The minimum token count (frequency) in order to include during encoding. All tokens
below this frequency will be encoded to `0` which corresponds to unknown token. (Default value = 1)
limit_top_tokens: The maximum number of tokens to keep, based their frequency. Only the most common `limit_top_tokens`
tokens will be kept. Set to None to keep everything. (Default value: None)


def __new__(cls, x=None, y=None, ildj=None, kwargs=None):

    return super(_Mapping, cls).__new__(cls, x, y, ildj, kwargs)

Custom __new__ so namedtuple items have defaults.

Args:
x: `Tensor` or None. Input to forward; output of inverse.
y: `Tensor` or None. Input to inverse; output of forward.
ildj: `Tensor`. This is the (un-reduce_sum'ed) inverse log det jacobian.
kwargs: Python dictionary. Extra args supplied to forward/inverse/etc
functions.

Returns:
mapping: New instance of _Mapping.


def vae(x, z_size, name=None):

  with tf.variable_scope(name, default_name="vae"):
    mu = tf.layers.dense(x, z_size, name="mu")
    log_sigma = tf.layers.dense(x, z_size, name="log_sigma")
    shape = common_layers.shape_list(x)
    epsilon = tf.random_normal([shape[0], shape[1], 1, z_size])
    z = mu + tf.exp(log_sigma / 2) * epsilon
    kl = 0.5 * tf.reduce_mean(
        tf.expm1(log_sigma) + tf.square(mu) - log_sigma, axis=-1)
    free_bits = z_size
    kl_loss = tf.reduce_mean(tf.maximum(kl - free_bits, 0.0))
    return z, kl_loss, mu, log_sigma

Simple variational autoencoder without discretization.

Args:
x: Input to the discretization bottleneck.
z_size: Number of bits, where discrete codes range from 1 to 2**z_size.
name: Name for the bottleneck scope.

Returns:
Embedding function, latent, loss, mu and log_simga.


def from_json(cls, json):
    if (json['name'] in _KEYRANGES_CLASSES):
        return _KEYRANGES_CLASSES[json['name']].from_json(json)
    raise ValueError('Invalid json %s', json)

Deserialize from json.

Args:
json: a dict of json compatible fields.

Returns:
a KeyRanges object.

Raises:
ValueError: if the json is invalid.


def contrast(x, severity=1):
    c = [0.4, 0.3, 0.2, 0.1, 0.05][(severity - 1)]
    x = (np.array(x) / 255.0)
    means = np.mean(x, axis=(0, 1), keepdims=True)
    x_clip = (np.clip((((x - means) * c) + means), 0, 1) * 255)
    return around_and_astype(x_clip)

Change contrast of images.

Args:
x: numpy array, uncorrupted image, assumed to have uint8 pixel in [0,255].
severity: integer, severity of corruption.

Returns:
numpy array, image with uint8 pixels in [0,255]. Changed contrast.


def _update_album_art_to_full_uri(self, item):
    if getattr(item, 'album_art_uri', False):
        item.album_art_uri = self.build_album_art_full_uri(item.album_art_uri)

Update an item's Album Art URI to be an absolute URI.

Args:
item: The item to update the URI for


async def _on_state_update(self, state_update):
    notification_type = state_update.WhichOneof('state_update')
    if state_update.HasField('conversation'):
        try:
            (await self._handle_conversation_delta(state_update.conversation))
        except exceptions.NetworkError:
            logger.warning('Discarding %s for %s: Failed to fetch conversation', notification_type.replace('_', ' '), state_update.conversation.conversation_id.id)
            return
    if (notification_type == 'typing_notification'):
        (await self._handle_set_typing_notification(state_update.typing_notification))
    elif (notification_type == 'watermark_notification'):
        (await self._handle_watermark_notification(state_update.watermark_notification))
    elif (notification_type == 'event_notification'):
        (await self._on_event(state_update.event_notification.event))

Receive a StateUpdate and fan out to Conversations.

Args:
state_update: hangouts_pb2.StateUpdate instance


def speed(self):
    if self._stalled:
        return 0
    time_sum = 0
    data_len_sum = 0
    for (time_diff, data_len) in self._samples:
        time_sum += time_diff
        data_len_sum += data_len
    if time_sum:
        return (data_len_sum / time_sum)
    else:
        return 0

Return the current transfer speed.

Returns:
int: The speed in bytes per second.


def _measure_list_profile_column_widths(self, profile_data):
    num_columns = len(profile_data.column_names())
    widths = [len(column_name) for column_name in profile_data.column_names()]
    for row in range(profile_data.row_count()):
        for col in range(num_columns):
            widths[col] = max(widths[col], len(str(profile_data.row_values(row)[col])) + 2)
    return widths

Determine the maximum column widths for each data list.

Args:
profile_data: list of ProfileDatum objects.

Returns:
List of column widths in the same order as columns in data.


def encode(self, input_ids: jnp.ndarray, attention_mask: Optional[jnp.ndarray]=None, position_ids: Optional[jnp.ndarray]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, train: bool=False, params: Optional[dict]=None, dropout_rng: PRNGKey=None):
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.return_dict
    if attention_mask is None:
        attention_mask = jnp.ones_like(input_ids)
    if position_ids is None:
        batch_size, sequence_length = input_ids.shape
        position_ids = jnp.broadcast_to(jnp.arange(sequence_length)[None, :], (batch_size, sequence_length))
    rngs = {}
    if dropout_rng is not None:
        rngs['dropout'] = dropout_rng

    def _encoder_forward(module, input_ids, attention_mask, position_ids, **kwargs):
        encode_module = module._get_encoder_module()
        return encode_module(input_ids, attention_mask, position_ids, **kwargs)
    return self.module.apply({'params': params or self.params}, input_ids=jnp.array(input_ids, dtype='i4'), attention_mask=jnp.array(attention_mask, dtype='i4'), position_ids=jnp.array(position_ids, dtype='i4'), output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, deterministic=not train, rngs=rngs, method=_encoder_forward)

Returns:

Example:

```python
>>> from transformers import AutoTokenizer, FlaxBlenderbotSmallForConditionalGeneration

>>> model = FlaxBlenderbotSmallForConditionalGeneration.from_pretrained("facebook/blenderbot_small-90M")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot_small-90M")

>>> text = "My friends are cool but they eat too many carbs."
>>> inputs = tokenizer(text, max_length=1024, return_tensors="np")
>>> encoder_outputs = model.encode(**inputs)
```


def plot(self, data, height=1000, render_large_data=False):

    import IPython

    if not isinstance(data, pd.DataFrame):
      raise ValueError('Expect a DataFrame.')

    if (len(data) > 10000 and not render_large_data):
      raise ValueError('Facets dive may not work well with more than 10000 rows. ' +
                       'Reduce data or set "render_large_data" to True.')

    jsonstr = data.to_json(orient='records')
    html_id = 'f' + datalab.utils.commands.Html.next_id()
    HTML_TEMPLATE =
    html = HTML_TEMPLATE.format(html_id=html_id, jsonstr=jsonstr, height=height)
    return IPython.core.display.HTML(html)

Plots a detail view of data.

Args:
data: a Pandas dataframe.
height: the height of the output.


def brightness(im):

    im_hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(im_hsv)
    height, weight = v.shape[:2]
    total_bright = 0
    for i in v:
        total_bright = total_bright+sum(i)
    return float(total_bright)/(height*weight)

Return the brightness of an image
Args:
im(numpy): image

Returns:
float, average brightness of an image


def format(sql, args=None):

    resolved_vars = {}
    code = []
    SqlStatement._find_recursive_dependencies(sql, args, code=code,
                                              resolved_vars=resolved_vars)

    parts = []
    for (escape, placeholder, _, literal) in SqlStatement._get_tokens(sql):
      if escape:
        parts.append('$')
      elif placeholder:
        variable = placeholder[1:]
        try:
          value = resolved_vars[variable]
        except KeyError as e:
          raise Exception('Invalid sql. Unable to substitute $%s.' % e.args[0])

        if isinstance(value, types.ModuleType):
          value = _utils.get_default_query_from_module(value)

        if isinstance(value, SqlStatement):
          sql = value.format(value._sql, resolved_vars)
          value = '(%s)' % sql
        elif '_repr_sql_' in dir(value):

          value = value._repr_sql_()
        elif isinstance(value, basestring):
          value = SqlStatement._escape_string(value)
        elif isinstance(value, list) or isinstance(value, tuple):
          if isinstance(value, tuple):
            value = list(value)
          expansion = '('
          for v in value:
            if len(expansion) > 1:
              expansion += ', '
            if isinstance(v, basestring):
              expansion += SqlStatement._escape_string(v)
            else:
              expansion += str(v)
          expansion += ')'
          value = expansion
        else:
          value = str(value)
        parts.append(value)
      elif literal:
        parts.append(literal)

    expanded = ''.join(parts)
    return expanded

Resolve variable references in a query within an environment.

This computes and resolves the transitive dependencies in the query and raises an
exception if that fails due to either undefined or circular references.

Args:
sql: query to format.
args: a dictionary of values to use in variable expansion.

Returns:
The resolved SQL text with variables expanded.

Raises:
Exception on failure.


def _FormatHumanReadableSize(self, size):

    magnitude_1000 = 0
    size_1000 = float(size)
    while size_1000 >= 1000:
      size_1000 /= 1000
      magnitude_1000 += 1

    magnitude_1024 = 0
    size_1024 = float(size)
    while size_1024 >= 1024:
      size_1024 /= 1024
      magnitude_1024 += 1

    size_string_1000 = None
    if 0 < magnitude_1000 <= 7:
      size_string_1000 = '{0:.1f}{1:s}'.format(
          size_1000, self._UNITS_1000[magnitude_1000])

    size_string_1024 = None
    if 0 < magnitude_1024 <= 7:
      size_string_1024 = '{0:.1f}{1:s}'.format(
          size_1024, self._UNITS_1024[magnitude_1024])

    if not size_string_1000 or not size_string_1024:
      return '{0:d} B'.format(size)

    return '{0:s} / {1:s} ({2:d} B)'.format(
        size_string_1024, size_string_1000, size)

Represents a number of bytes as a human readable string.

Args:
size (int): size in bytes.

Returns:
str: human readable string of the size.


def chmod(target):

    assert isinstance(target, str)
    assert os.path.exists(target)

    file_mode = stat.S_IRUSR | stat.S_IWUSR
    folder_mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR

    remove_immutable_attribute(target)

    if os.path.isfile(target):
        os.chmod(target, file_mode)

    elif os.path.isdir(target):

        os.chmod(target, folder_mode)

        for root, dirs, files in os.walk(target):
            for cur_dir in dirs:
                os.chmod(os.path.join(root, cur_dir), folder_mode)
            for cur_file in files:
                os.chmod(os.path.join(root, cur_file), file_mode)

    else:
        raise ValueError("Unsupported file type: {}".format(target))

Recursively set the chmod for files to 0600 and 0700 for folders.

It's ok unless we need something more specific.

Args:
target (str): Root file or folder


def get_crate(version, crate_root=None):
    if (not crate_root):
        crate_root = _crates_cache()
        _remove_old_crates(crate_root)
    if _is_project_repo(version):
        return _extract_tarball(_build_tarball(version))
    m = BRANCH_VERSION_RE.match(version)
    if m:
        return _build_from_release_branch(m.group(0), crate_root)
    uri = _lookup_uri(version)
    crate_dir = _download_and_extract(uri, crate_root)
    return crate_dir

Retrieve a Crate tarball, extract it and return the path.

Args:
version: The Crate version to get.
Can be specified in different ways:

- A concrete version like '0.55.0'
- A version including a `x` as wildcards. Like: '1.1.x' or '1.x.x'.
This will use the latest version that matches.
- Release branch, like `3.1`
- An alias: 'latest-stable' or 'latest-testing'
- A URI pointing to a crate tarball
crate_root: Where to extract the tarball to.
If this isn't specified ``$XDG_CACHE_HOME/.cache/cr8/crates``
will be used.


def is_scalar_batch(self, name='is_scalar_batch'):
    with self._name_scope(name):
        return ops.convert_to_tensor(self._is_scalar_helper(self.batch_shape, self.batch_shape_tensor), name='is_scalar_batch')

Indicates that `batch_shape == []`.

Args:
name: Python `str` prepended to names of ops created by this function.

Returns:
is_scalar_batch: `bool` scalar `Tensor`.


def download_extract_tar(tar_url, folder, tar_filename=''):

    try:
        makedirs(folder)
    except OSError:
        if not isdir(folder):
            raise
    data_file = tar_filename
    if not data_file:
        fd, data_file = mkstemp('.tar.gz')
        download(tar_url, os.fdopen(fd, 'wb'))
    else:
        download(tar_url, data_file)

    with tarfile.open(data_file) as tar:
        tar.extractall(path=folder)

Download and extract the tar at the url to the given folder

Args:
tar_url (str): URL of tar file to download
folder (str): Location of parent directory to extract to. Doesn't have to exist
tar_filename (str): Location to download tar. Default is to a temp file


def btemp_threshold(img, min_in, max_in, threshold, threshold_out=None, **kwargs):
    threshold_out = (threshold_out if (threshold_out is not None) else (176 / 255.0))
    low_factor = ((threshold_out - 1.0) / (min_in - threshold))
    low_offset = (1.0 + (low_factor * min_in))
    high_factor = (threshold_out / (max_in - threshold))
    high_offset = (high_factor * max_in)

    def _bt_threshold(band_data):
        return da.where((band_data >= threshold), (high_offset - (high_factor * band_data)), (low_offset - (low_factor * band_data)))
    return apply_enhancement(img.data, _bt_threshold, pass_dask=True)

Scale data linearly in two separate regions.

This enhancement scales the input data linearly by splitting the data
into two regions; min_in to threshold and threshold to max_in. These
regions are mapped to 1 to threshold_out and threshold_out to 0
respectively, resulting in the data being "flipped" around the
threshold. A default threshold_out is set to `176.0 / 255.0` to
match the behavior of the US National Weather Service's forecasting
tool called AWIPS.

Args:
img (XRImage): Image object to be scaled
min_in (float): Minimum input value to scale
max_in (float): Maximum input value to scale
threshold (float): Input value where to split data in to two regions
threshold_out (float): Output value to map the input `threshold`
to. Optional, defaults to 176.0 / 255.0.


def _create_hparam_extractor(hparam_name):

    def extractor_fn(session_group):
        if (hparam_name in session_group.hparams):
            return _value_to_python(session_group.hparams[hparam_name])
        return None
    return extractor_fn

Returns an extractor function that extracts an hparam from a session group.

Args:
hparam_name: str. Identies the hparam to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup protobuffer and
returns the value, as a native Python object, of the hparam identified by
'hparam_name'.


def save_page(self, path=None):

        path = _prepare_path(path, "html")

        with open(path, "wb") as f:
            f.write(encode_string(self.body))

        return path

Save a snapshot of the page.

If invoked without arguments, it will save a file to :data:`capybara.save_path` and the
file will be given a randomly generated filename. If invoked with a relative path, the path
will be relative to :data:`capybara.save_path`.

Args:
path (str, optional): The path to where it should be saved.

Returns:
str: The path to which the file was saved.


def nodeid(self, iv, quantifier=False):

        return next(iter(self.nodeids(ivs=[iv], quantifier=quantifier)), None)

Return the nodeid of the predication selected by *iv*.

Args:
iv: the intrinsic variable of the predication to select
quantifier: if `True`, treat *iv* as a bound variable and
find its quantifier; otherwise the non-quantifier will
be returned


def execute_command(self, command):

        self.info_log("executing command: %s" % command)

        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

            k = paramiko.RSAKey.from_private_key_file(
                self.browser_config.get('ssh_key_path')
            )
            ssh.connect(
                self.private_ip,
                username=self.browser_config.get('username'),
                pkey=k
            )

            sleep_time = 0.1
            stdout = []
            stderr = []

            ssh_transport = ssh.get_transport()
            channel = ssh_transport.open_session()
            channel.setblocking(0)
            channel.exec_command(command)

            while True:

                while channel.recv_ready():
                    stdout.append(channel.recv(1000))

                while channel.recv_stderr_ready():
                    stderr.append(channel.recv_stderr(1000))

                if channel.exit_status_ready():
                    break

                sleep(sleep_time)

            ssh_transport.close()

            ssh.close()

            return b''.join(stdout), b''.join(stderr)

        except Exception as e:
            msg = "Execute_command exception: %s" % str(e)
            self.error_log(msg)
            raise Exception(msg)

Execute a command on the node

Args:
command (str)


def FromTrimmedData(data, index):

        header = Header()

        ms = StreamManager.GetStream(data)

        reader = BinaryReader(ms)
        header.DeserializeUnsigned(reader)
        reader.ReadByte()

        witness = Witness()
        witness.Deserialize(reader)
        header.Script = witness

        StreamManager.ReleaseStream(ms)

        return header

Deserialize into a Header object from the provided data.

Args:
data (bytes):
index: UNUSED

Returns:
Header:


def get_by_uri(self, uri):
    self._helper.validate_resource_uri(uri)
    data = self._helper.do_get(uri)
    if data:
        new_resource = self.new(self._connection, data)
    else:
        new_resource = None
    return new_resource

Retrieves a resource by its URI

Args:
uri: URI of the resource

Returns:
Resource object


def xmoe_tr_dense_2k():
    hparams = mtf_transformer2.mtf_bitransformer_base()
    hparams.encoder_layers = (['self_att', 'drd'] * 4)
    hparams.decoder_layers = (['self_att', 'enc_att', 'drd'] * 4)
    hparams.batch_size = 64
    hparams.shared_embedding_and_softmax_weights = True
    hparams.mesh_shape = 'batch:8'
    return hparams

Series of architectural experiments on Translation.

# run on 8-core setup

119M params, einsum=0.95e13

Returns:
a hparams


def UploadUsers(self, hash_algorithm, hash_key, accounts):
    return self.rpc_helper.UploadAccount(hash_algorithm, base64.urlsafe_b64encode(hash_key), [GitkitUser.ToRequest(i) for i in accounts])

Uploads multiple users to Gitkit server.

Args:
hash_algorithm: string, the hash algorithm.
hash_key: array, raw key of the hash algorithm.
accounts: list of GitkitUser.

Returns:
A dict of failed accounts. The key is the index of the 'accounts' list,
starting from 0.


def create(self, callback_url):

        resource = self.resource.create({'subscribed_to': 'address',
                                         'callback_url': callback_url})
        subscription = self.wrap(resource)
        self.add(subscription)
        return subscription

Register a new Subscription on this collection's parent object.

Args:
callback_url (str): URI of an active endpoint which can receive
notifications.

Returns:
A round.Subscription object if successful.


def _verify_pipeline_uuid(self, pipeline_uuid):
    try:
        uuid.UUID(pipeline_uuid)
    except ValueError as ve:
        raise ValueError(f"Incorrect pipeline uuid: '{pipeline_uuid}'") from ve

Verify the received pipeline_uuid format

Args:
pipeline_uuid: uuid of the pipeline

Returns:
If pipeline ID is not verified, will raise an exception


def _preprocess_movie_lens(ratings_df):
    ratings_df['data'] = 1.0
    num_timestamps = ratings_df[['userId', 'timestamp']].groupby('userId').nunique()
    last_user_timestamp = ratings_df[['userId', 'timestamp']].groupby('userId').max()
    ratings_df['numberOfTimestamps'] = ratings_df['userId'].apply((lambda x: num_timestamps['timestamp'][x]))
    ratings_df['lastTimestamp'] = ratings_df['userId'].apply((lambda x: last_user_timestamp['timestamp'][x]))
    ratings_df = ratings_df[(ratings_df['numberOfTimestamps'] > 2)]
    ratings_df = _create_row_col_indices(ratings_df)
    train_ratings_df = ratings_df[(ratings_df['timestamp'] < ratings_df['lastTimestamp'])]
    test_ratings_df = ratings_df[(ratings_df['timestamp'] == ratings_df['lastTimestamp'])]
    return (ratings_df, train_ratings_df, test_ratings_df)

Separate the rating datafram into train and test sets.

Filters out users with less than two distinct timestamps. Creates train set
and test set. The test set contains all the last interactions of users with
more than two distinct timestamps.

Args:
ratings_df: pandas dataframe with columns 'userId', 'movieId', 'rating',
'timestamp'.

Returns:
tuple of dataframes (filtered_ratings, train_ratings, test_ratings).


class BlipProcessor(ProcessorMixin):
    attributes = ['image_processor', 'tokenizer']
    image_processor_class = ('BlipImageProcessor', 'BlipImageProcessorFast')
    tokenizer_class = ('BertTokenizer', 'BertTokenizerFast')

    def __init__(self, image_processor, tokenizer, **kwargs):
        tokenizer.return_token_type_ids = False
        super().__init__(image_processor, tokenizer)
        self.current_processor = self.image_processor

    def __call__(self, images: ImageInput=None, text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]]=None, audio=None, videos=None, **kwargs: Unpack[BlipProcessorKwargs]) -> BatchEncoding:

        if images is None and text is None:
            raise ValueError('You have to specify either images or text.')
        text_encoding = None
        output_kwargs = self._merge_kwargs(BlipProcessorKwargs, tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs)
        if text is not None:
            text_encoding = self.tokenizer(text, **output_kwargs['text_kwargs'])
        if images is not None:
            encoding_image_processor = self.image_processor(images, **output_kwargs['images_kwargs'])
            if text_encoding is not None:
                encoding_image_processor.update(text_encoding)
            return encoding_image_processor
        return text_encoding

    def batch_decode(self, *args, **kwargs):

        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):

        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        image_processor_input_names = self.image_processor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))

Constructs a BLIP processor which wraps a BERT tokenizer and BLIP image processor into a single processor.

[`BlipProcessor`] offers all the functionalities of [`BlipImageProcessor`] and [`BertTokenizerFast`]. See the
docstring of [`~BlipProcessor.__call__`] and [`~BlipProcessor.decode`] for more information.

Args:
image_processor (`BlipImageProcessor`):
An instance of [`BlipImageProcessor`]. The image processor is a required input.
tokenizer (`BertTokenizerFast`):
An instance of ['BertTokenizerFast`]. The tokenizer is a required input.


def evalAsync(self, amplstatements, callback, **kwargs):
    if (self._langext is not None):
        amplstatements = self._langext.translate(amplstatements, **kwargs)

    def async_call():
        self._lock.acquire()
        try:
            self._impl.eval(amplstatements)
            self._errorhandler_wrapper.check()
        except Exception:
            self._lock.release()
            raise
        else:
            self._lock.release()
            callback.run()
    Thread(target=async_call).start()

Interpret the given AMPL statement asynchronously.

Args:
amplstatements: A collection of AMPL statements and declarations to
be passed to the interpreter.

callback: Callback to be executed when the statement has been
interpreted.

Raises:
RuntimeError: if the input is not a complete AMPL statement (e.g.
if it does not end with semicolon) or if the underlying
interpreter is not running.


def get_nn_info(self, structure, n):
    site = structure[n]
    neighs_dists = structure.get_neighbors(site, self.cutoff)
    siw = []
    if (self.get_all_sites == True):
        for (s, dist) in neighs_dists:
            w = dist
            siw.append({'site': s, 'image': self._get_image(structure, s), 'weight': w, 'site_index': self._get_original_site(structure, s)})
    else:
        min_dist = min([dist for (neigh, dist) in neighs_dists])
        for (s, dist) in neighs_dists:
            if (dist < ((1.0 + self.tol) * min_dist)):
                w = (min_dist / dist)
                siw.append({'site': s, 'image': self._get_image(structure, s), 'weight': w, 'site_index': self._get_original_site(structure, s)})
    return siw

Get all near-neighbor sites as well as the associated image locations
and weights of the site with index n using the closest neighbor
distance-based method.

Args:
structure (Structure): input structure.
n (integer): index of site for which to determine near
neighbors.

Returns:
siw (list of tuples (Site, array, float)): tuples, each one
of which represents a neighbor site, its image location,
and its weight.


def _sparse_tensor(self, data, batch_size=-1):
    indices = []
    values = []
    max_col_count = 0
    for batch, batch_ix in zip(data, range(len(data))):
        for column, column_ix in zip(batch, range(len(batch))):
            indices.append([batch_ix, column_ix])
            values.append(column)
            max_col_count = max(max_col_count, column_ix + 1)
    shape = [batch_size if batch_size != -1 else len(data), max_col_count]
    value_type = dtypes.string if not values or isinstance(values[0], str) else dtypes.int64
    return sparse_tensor.SparseTensor(constant_op.constant(indices, dtypes.int64, [len(indices), 2]), constant_op.constant(values, value_type, [len(indices)]), constant_op.constant(shape, dtypes.int64))

Generates a SparseTensor.

Args:
data: Should be a list of list of strings or int64. Each item of the outer
list represents a batch. Each item of the batch is a feature of a
specific feature column.
batch_size: optional batch size, especially for cases when data has no
entry for some batches.

Returns:
A SparseTensor.


def download_artifact_bundle(self, id_or_uri, file_path):

        uri = self.DOWNLOAD_PATH + '/' + extract_id_from_uri(id_or_uri)
        return self._client.download(uri, file_path)

Download the Artifact Bundle.

Args:
id_or_uri: ID or URI of the Artifact Bundle.
file_path(str): Destination file path.

Returns:
bool: Successfully downloaded.


def post(cls, payload):
    if (not isinstance(payload, dict)):
        raise ValueError("The 'payload' parameter must be provided a dictionary object.")
    payload = cls.set_id_in_fkeys(payload)
    payload = cls.check_boolean_fields(payload)
    payload = cls.add_model_name_to_payload(payload)
    payload = cls.prepost_hooks(payload)
    cls.debug_logger.debug('POSTING payload {}'.format(json.dumps(payload, indent=4)))
    res = requests.post(url=cls.URL, json=payload, headers=HEADERS, verify=False)
    cls.write_response_html_to_file(res, 'bob.html')
    if (not res.ok):
        cls.log_error(res.text)
        res_json = res.json()
        if ('exception' in res_json):
            exc_type = res_json['exception']
            if (exc_type == 'ActiveRecord::RecordNotUnique'):
                raise RecordNotUnique()
    res.raise_for_status()
    res = res.json()
    cls.log_post(res)
    cls.debug_logger.debug('Success')
    return res

Posts the data to the specified record.

Args:
payload: `dict`. This will be JSON-formatted prior to sending the request.

Returns:
`dict`. The JSON formatted response.

Raises:
`Requests.exceptions.HTTPError`: The status code is not ok.
`RecordNotUnique`: The Rails server returned the exception ActiveRecord::RecordNotUnique.


def set_token(self, token):
    self.token = token
    self.set_header('Authorization', 'Bearer {}'.format(token))

Set the token for the v20 context

Args:
token: The token used to access the v20 REST api


def save_r_df(self, state_key, r_value, action_key=None):

        if action_key is not None:
            add_r_df = pd.DataFrame([(state_key, action_key, r_value)], columns=["state_key", "action_key", "r_value"])
        else:
            add_r_df = pd.DataFrame([(state_key, r_value)], columns=["state_key", "r_value"])

        if self.r_df is not None:
            self.r_df = pd.concat([add_r_df, self.r_df])
            if action_key is not None:
                self.r_df = self.r_df.drop_duplicates(["state_key", "action_key"])
            else:
                self.r_df = self.r_df.drop_duplicates(["state_key"])
        else:
            self.r_df = add_r_df

Insert or update R-Value in `self.r_df`.

Args:
state_key:     The key of state.
r_value:       R-Value(Reward).
action_key:    The key of action if it is nesesary for the parametar of value function.

Exceptions:
TypeError:      If the type of `r_value` is not float.


def read(self, n):

        d = b''
        while n:
            try:
                block = self._process.stdout.read(n)
            except ValueError:
                block = None
            if not block:
                self._process.poll()
                raise EOFError('Process ended')
            d += block
            n -= len(block)
        return d

Read *n* bytes from the subprocess' output channel.

Args:
n(int): The number of bytes to read.

Returns:
bytes: *n* bytes of output.

Raises:
EOFError: If the process exited.


def get_es_value(obj, def_obj):

    def get_dict_val(item):

        if isinstance(item, dict):
            return str(item.get('value'))
        return str(item)

    value_flds = []
    if def_obj.es_defs.get('kds_esValue'):
        value_flds = def_obj.es_defs['kds_esValue'].copy()
    else:

        value_flds = set(obj).difference(__ALL_IGN__)
        value_flds = list(value_flds)
    value_flds += __COMBINED__
    try:
        obj['value'] = [obj.get(label) for label in value_flds
                        if obj.get(label)][0]
    except IndexError:
        obj['value'] = ", ".join(["%s: %s" % (value.get('label'),
                                              value.get('value'))
                                  for prop, value in obj.items()
                                  if isinstance(value, dict) and \
                                  value.get('label')])

    if isinstance(obj['value'], list):
        obj['value'] = ", ".join([get_dict_val(item) for item in obj['value']])
    else:
        obj['value'] = get_dict_val(obj['value'])
    if str(obj['value']).strip().endswith("/"):
        obj['value'] = str(obj['value']).strip()[:-1].strip()
    if not obj['value']:
        obj['value'] = obj.get('uri', '')
    return obj

Returns the value for an object that goes into the elacticsearch 'value'
field

args:
obj: data object to update
def_obj: the class instance that has defintion values


def dict_itemstr_list(dict_, **dictkw):
    r
    import utool as ut

    explicit = dictkw.get('explicit', False)
    dictkw['explicit'] = _rectify_countdown_or_bool(explicit)

    dosort = dictkw.get('sorted_', None)
    if dosort is None:
        dosort = True

    if dosort and not isinstance(dict_, collections.OrderedDict):
        key_order = dictkw.get('key_order', None)
        def iteritems(d):
            if key_order is None:

                try:
                    return iter(sorted(six.iteritems(d)))
                except TypeError:

                    return six.iteritems(d)
            else:

                unordered_keys = list(d.keys())
                other_keys = sorted(list(set(unordered_keys) - set(key_order)))
                keys = key_order + other_keys
                return ((key, d[key]) for key in keys)
    else:
        iteritems = six.iteritems

    _valstr = _make_valstr(**dictkw)

    precision = dictkw.get('precision', None)
    kvsep = dictkw.get('kvsep', ': ')
    if explicit:
        kvsep = '='

    def make_item_str(key, val):
        if explicit or dictkw.get('strkeys', False):
            key_str = six.text_type(key)
        else:
            key_str = repr2(key, precision=precision)

        prefix = key_str + kvsep
        val_str = _valstr(val)

        if util_type.HAVE_NUMPY and isinstance(val, np.ndarray):
            item_str = hz_str(prefix, val_str)
        else:

            item_str = prefix + val_str
        return item_str

    itemstr_list = [make_item_str(key, val)
                    for (key, val) in iteritems(dict_)]

    reverse = False
    key_order_metric = dictkw.get('key_order_metric', None)
    if key_order_metric is not None:
        if key_order_metric.startswith('-'):
            key_order_metric = key_order_metric[1:]
            reverse = True

    if key_order_metric == 'strlen':
        metric_list = [len(itemstr) for itemstr in itemstr_list]
        itemstr_list = ut.sortedby(itemstr_list, metric_list, reverse=reverse)
    elif key_order_metric == 'val':
        metric_list = [val for (key, val) in iteritems(dict_)]
        itemstr_list = ut.sortedby(itemstr_list, metric_list, reverse=reverse)

    maxlen = dictkw.get('maxlen', None)
    if maxlen is not None and len(itemstr_list) > maxlen:
        itemstr_list = itemstr_list[0:maxlen]
    return itemstr_list

r"""
Returns:
list: a list of human-readable dictionary items

Args:
explicit : if True uses dict(key=val,...) format instead of {key:val,...}


def get_testcase_io(testcase):

    test_runner = testcase.runner
    variables = testcase.config.get("variables", {})
    output_list = testcase.config.get("output", [])
    output_mapping = test_runner.extract_output(output_list)

    return {
        "in": variables,
        "out": output_mapping
    }

get and print testcase input(variables) and output.

Args:
testcase (unittest.suite.TestSuite): corresponding to one YAML/JSON file, it has been set two attributes:
config: parsed config block
runner: initialized runner.Runner() with config
Returns:
dict: input(variables) and output mapping.


def line_similarity(p1a, p1b, p2a, p2b, T=CLOSE_DISTANCE_THRESHOLD):

    d = line_distance_similarity(p1a, p1b, p2a, p2b, T=T)
    a = abs(angle_similarity(normalize(line(p1a, p1b)), normalize(line(p2a, p2b))))
    return d * a

Similarity between two lines

Args:
p1a ([float, float]): x and y coordinates. Line A start
p1b ([float, float]): x and y coordinates. Line A end
p2a ([float, float]): x and y coordinates. Line B start
p2b ([float, float]): x and y coordinates. Line B end
Returns:
float: between 0 and 1. Where 1 is very similar and 0 is completely different


def register_for_auto_class(cls, auto_class='AutoFeatureExtractor'):
    if not isinstance(auto_class, str):
        auto_class = auto_class.__name__
    import transformers.models.auto as auto_module
    if not hasattr(auto_module, auto_class):
        raise ValueError(f'{auto_class} is not a valid auto class.')
    cls._auto_class = auto_class

Register this class with a given auto class. This should only be used for custom feature extractors as the ones
in the library are already mapped with `AutoFeatureExtractor`.

Args:
auto_class (`str` or `type`, *optional*, defaults to `"AutoFeatureExtractor"`):
The auto class to register this new feature extractor with.


def __init__(self, id, buckets=None, **kwargs):

        buckets = buckets or []
        super(Song, self).__init__(id, buckets, **kwargs)

Song class

Args:
id (str): a song ID

Kwargs:
buckets (list): A list of strings specifying which buckets to retrieve

Returns:
A Song object

Example:

>>> s = song.Song('SOPEXHZ12873FD2AC7', buckets=['song_hotttnesss', 'artist_hotttnesss'])
>>> s.song_hotttnesss
0.58602500000000002
>>> s.artist_hotttnesss
0.80329715999999995
>>>


def info(self, **kwargs):

        path = self._get_series_id_season_number_episode_number_path('info')

        response = self._GET(path, kwargs)
        self._set_attrs_to_values(response)
        return response

Get the primary information about a TV episode by combination of a
season and episode number.

Args:
language: (optional) ISO 639 code.
append_to_response: (optional) Comma separated, any TV series
method.

Returns:
A dict respresentation of the JSON returned from the API.


def embed(self, url, format='json', **opt):

        if format not in ['json', 'xml']:
            raise OEmbedInvalidRequest('Format must be json or xml')
        opt['format'] = format
        return self._request(url, **opt)

Get an OEmbedResponse from one of the providers configured in this
consumer according to the resource url.

Args:
url: The url of the resource to get.
format: Desired response format.
**opt: Optional parameters to pass in the url to the provider.

Returns:
OEmbedResponse object.


def __init__(
        self,
        learning_rate,
        cg_max_iterations=20,
        cg_damping=1e-3,
        cg_unroll_loop=False,
        scope='natural-gradient',
        summary_labels=()
    ):

        assert learning_rate > 0.0
        self.learning_rate = learning_rate

        self.solver = ConjugateGradient(
            max_iterations=cg_max_iterations,
            damping=cg_damping,
            unroll_loop=cg_unroll_loop
        )

        super(NaturalGradient, self).__init__(scope=scope, summary_labels=summary_labels)

Creates a new natural gradient optimizer instance.

Args:
learning_rate: Learning rate, i.e. KL-divergence of distributions between optimization steps.
cg_max_iterations: Conjugate gradient solver max iterations.
cg_damping: Conjugate gradient solver damping factor.
cg_unroll_loop: Unroll conjugate gradient loop if true.


def validate_yaml(self, properties):
    validator = OurValidator(schema)
    if (not validator.validate(properties)):
        for (key, value) in validator.errors.items():
            if any([('unallowed value' in v) for v in value]):
                print('{key} has an illegal value. Allowed values are {values} and are case sensitive.'.format(key=key, values=schema[key]['allowed']))
        raise ValueError(validator.errors)

Validate the parsed YAML file for adherance to the ChemKED format.

Arguments:
properties (`dict`): Dictionary created from the parsed YAML file

Raises:
`ValueError`: If the YAML file cannot be validated, a `ValueError` is raised whose
string contains the errors that are present.


def _add_sample_measure(self, measure_params, num_samples):
    measured_qubits = list({qubit for (qubit, cmembit) in measure_params})
    num_measured = len(measured_qubits)
    axis = list(range(self._number_of_qubits))
    for qubit in reversed(measured_qubits):
        axis.remove(((self._number_of_qubits - 1) - qubit))
    probabilities = np.reshape(np.sum((np.abs(self._statevector) ** 2), axis=tuple(axis)), (2 ** num_measured))
    samples = self._local_random.choice(range((2 ** num_measured)), num_samples, p=probabilities)
    memory = []
    for sample in samples:
        classical_memory = self._classical_memory
        for (count, (qubit, cmembit)) in enumerate(sorted(measure_params)):
            qubit_outcome = int(((sample & (1 << count)) >> count))
            membit = (1 << cmembit)
            classical_memory = ((classical_memory & (~ membit)) | (qubit_outcome << cmembit))
        value = bin(classical_memory)[2:]
        memory.append(hex(int(value, 2)))
    return memory

Generate memory samples from current statevector.

Args:
measure_params (list): List of (qubit, cmembit) values for
measure instructions to sample.
num_samples (int): The number of memory samples to generate.

Returns:
list: A list of memory values in hex format.


def assert_no_text(self, *args, **kwargs):

        query = TextQuery(*args, **kwargs)

        @self.synchronize(wait=query.wait)
        def assert_no_text():
            count = query.resolve_for(self)

            if matches_count(count, query.options) and (
                   count > 0 or expects_none(query.options)):
                raise ExpectationNotMet(query.negative_failure_message)

            return True

        return assert_no_text()

Asserts that the page or current node doesn't have the given text content, ignoring any
HTML tags.

Args:
*args: Variable length argument list for :class:`TextQuery`.
**kwargs: Arbitrary keyword arguments for :class:`TextQuery`.

Returns:
True

Raises:
ExpectationNotMet: If the assertion hasn't succeeded during the wait time.


def on_core_metadata_event(self, event):

    core_metadata = json.loads(event.log_message.message)
    input_names = ','.join(core_metadata['input_names'])
    output_names = ','.join(core_metadata['output_names'])
    target_nodes = ','.join(core_metadata['target_nodes'])

    self._run_key = RunKey(input_names, output_names, target_nodes)
    if not self._graph_defs:
      self._graph_defs_arrive_first = False
    else:
      for device_name in self._graph_defs:
        self._add_graph_def(device_name, self._graph_defs[device_name])

    self._outgoing_channel.put(_comm_metadata(self._run_key, event.wall_time))

    logger.info('on_core_metadata_event() waiting for client ack (meta)...')
    self._incoming_channel.get()
    logger.info('on_core_metadata_event() client ack received (meta).')

Implementation of the core metadata-carrying Event proto callback.

Args:
event: An Event proto that contains core metadata about the debugged
Session::Run() in its log_message.message field, as a JSON string.
See the doc string of debug_data.DebugDumpDir.core_metadata for details.


def run_cell(self, cell):
    globals = self.ipy_shell.user_global_ns
    locals = self.ipy_shell.user_ns
    globals.update({'__ipy_scope__': None})
    try:
        with redirect_stdout(self.stdout):
            self.run(cell, globals, locals)
    except:
        self.code_error = True
        if self.options.debug:
            raise BdbQuit
    finally:
        self.finalize()

Run the Cell code using the IPython globals and locals

Args:
cell (str): Python code to be executed


def add_site_property(self, property_name, values):

        if len(values) != len(self.sites):
            raise ValueError("Values must be same length as sites.")
        for site, val in zip(self.sites, values):
            site.properties[property_name] = val

Adds a property to a site.

Args:
property_name (str): The name of the property to add.
values (list): A sequence of values. Must be same length as
number of sites.


def normalize_genotypes(genotypes):
    genotypes = genotypes.genotypes
    return ((genotypes - np.nanmean(genotypes)) / np.nanstd(genotypes))

Normalize the genotypes.

Args:
genotypes (Genotypes): The genotypes to normalize.

Returns:
numpy.array: The normalized genotypes.


def check(self, namespace, level, explicit=False):
    return ((self.get_permissions(namespace, explicit=explicit) & level) != 0)

Checks if the permset has permission to the specified namespace
at the specified level

Arguments:

namespace -- permissioning namespace (str)
level -- permissioning level (int) (PERM_READ for example)
explicit -- require explicitly set permissions to the provided namespace

Returns:

bool


def __init__(self, image_true_sampler):

        if isinstance(image_true_sampler, ImageTrueSampler) is False:
            raise TypeError()
        self.__image_true_sampler = image_true_sampler

Init.

Args:
image_true_sampler:     is-a `ImageTrueSampler`.


def put(self, key, value):

    key = self._service_key(key)
    self._service_ops['put'](key, value)

Stores the object `value` named by `key` in `service`.

Args:
key: Key naming `value`.
value: the object to store.


def _update_general_statistics(a_float, dist):

    if not dist.count:
        dist.count = 1
        dist.maximum = a_float
        dist.minimum = a_float
        dist.mean = a_float
        dist.sumOfSquaredDeviation = 0
    else:
        old_count = dist.count
        old_mean = dist.mean
        new_mean = ((old_count * old_mean) + a_float) / (old_count + 1)
        delta_sum_squares = (a_float - old_mean) * (a_float - new_mean)
        dist.count += 1
        dist.mean = new_mean
        dist.maximum = max(a_float, dist.maximum)
        dist.minimum = min(a_float, dist.minimum)
        dist.sumOfSquaredDeviation += delta_sum_squares

Adds a_float to distribution, updating the statistics fields.

Args:
a_float (float): a new value
dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`):
the Distribution being updated


def skip_log_prefix(func):
    if callable(func):
        func_code = getattr(func, '__code__', None)
        if (func_code is None):
            raise ValueError('Input callable does not have a function code object.')
        file_name = func_code.co_filename
        func_name = func_code.co_name
        func_lineno = func_code.co_firstlineno
    elif isinstance(func, six.string_types):
        file_name = get_absl_logger().findCaller()[0]
        func_name = func
        func_lineno = None
    else:
        raise TypeError('Input is neither callable nor a string.')
    ABSLLogger.register_frame_to_skip(file_name, func_name, func_lineno)
    return func

Skips reporting the prefix of a given function or name by ABSLLogger.

This is a convenience wrapper function / decorator for
`ABSLLogger.register_frame_to_skip`.

If a callable function is provided, only that function will be skipped.
If a function name is provided, all functions with the same name in the
file that this is called in will be skipped.

This can be used as a decorator of the intended function to be skipped.

Args:
func: Callable function or its name as a string.

Returns:
func (the input, unchanged).

Raises:
ValueError: The input is callable but does not have a function code object.
TypeError: The input is neither callable nor a string.


def create_empty_output_dir(output_directory: str, overwrite: bool=True) -> None:
    if overwrite and file_io.file_exists_v2(output_directory):
        logging.info('Deleting existing output directory: %s .', output_directory)
        file_io.delete_recursively_v2(output_directory)
    file_io.recursive_create_dir_v2(output_directory)

Creates the `output_directory`.

If `output_directory` already exists, it recursively deletes all contents
inside the directory.

Also creates the parent & intermediate directories.

Args:
output_directory: Output directory.
overwrite: Where to clean the output directory if exists.


def __init__(self, timestamp, family=None, reverse=False):

        self.timestamp = timestamp
        self.reverse = reverse
        self._family = family

Create a timestamp rule.

Args:
timestamp (int): Epoch time.
family (str): Package family to apply the rule to.
reverse (bool): If True, reverse the logic so that packages released
*after* the timestamp are matched.


def expand_value_set_url(self, value_set_url: str) -> value_set_pb2.ValueSet:
    value_set_url, value_set_version = url_utils.parse_url_version(value_set_url)
    base_url, terminology_service_url = _expansion_request_url_for_value_set_url(value_set_url)
    auth = self.auth_per_terminology_server.get(base_url)
    return self._expand_value_set_url_using_service(value_set_url=value_set_url, value_set_version=value_set_version, terminology_service_url=terminology_service_url, auth=auth)

Expands the value set using a terminology server.

Requests an expansion of the value set from the appropriate terminology
server for the given URL and version if present on the URL. The terminology
service is chosen based on the domain of `value_set_url`.

Retrieves the current definition of the value set from the terminology
service as well as its expansion.

Args:
value_set_url: The url of the value set to expand.

Raises:
ValueError: If a terminology service can not be found for `value_set_url`.

Returns:
The current definition of the value set from the server with its expanded
codes present.


def verify_oauth2_token(id_token, request, audience=None):
    return verify_token(id_token, request, audience=audience, certs_url=_GOOGLE_OAUTH2_CERTS_URL)

Verifies an ID Token issued by Google's OAuth 2.0 authorization server.

Args:
id_token (Union[str, bytes]): The encoded token.
request (google.auth.transport.Request): The object used to make
HTTP requests.
audience (str): The audience that this token is intended for. This is
typically your application's OAuth 2.0 client ID. If None then the
audience is not verified.

Returns:
Mapping[str, Any]: The decoded token.


def merge(self, other_rel):

        if other_rel.thresholds.size == self.thresholds.size and np.all(other_rel.thresholds == self.thresholds):
            self.frequencies += other_rel.frequencies
        else:
            print("Input table thresholds do not match.")

Ingest another DistributedReliability and add its contents to the current object.

Args:
other_rel: a Distributed reliability object.


def object(self, key):
    return _object.Object(self._name, key, context=self._context)

Retrieves a Storage Object for the specified key in this bucket.

The object need not exist.

Args:
key: the key of the object within the bucket.
Returns:
An Object instance representing the specified key.


def AddRow(self, values):

    if self._number_of_columns and len(values) != self._number_of_columns:
      raise ValueError('Number of values is out of bounds.')

    if not self._column_sizes and self._columns:
      self._column_sizes = [len(column) for column in self._columns]

    value_strings = []
    for value_index, value_string in enumerate(values):
      if not isinstance(value_string, py2to3.UNICODE_TYPE):
        value_string = '{0!s}'.format(value_string)
      value_strings.append(value_string)

      self._column_sizes[value_index] = max(
          self._column_sizes[value_index], len(value_string))

    self._rows.append(value_strings)

    if not self._number_of_columns:
      self._number_of_columns = len(value_strings)

Adds a row of values.

Args:
values (list[object]): values.

Raises:
ValueError: if the number of values is out of bounds.


def success(self, value):

        if value == self._defaults['success'] and 'success' in self._values:
            del self._values['success']
        else:
            self._values['success'] = value

The success property.

Args:
value (bool). the property value.


def vert_quality(script, min_quality=0.0, max_quality=0.05, inclusive=True):
    filter_xml = ''.join(['  <filter name="Select by Vertex Quality">\n', '    <Param name="minQ" ', 'value="{}" '.format(min_quality), 'description="Min Quality" ', 'min="0" ', 'max="{}" '.format((2 * max_quality)), 'type="RichDynamicFloat" ', '/>\n', '    <Param name="maxQ" ', 'value="{}" '.format(max_quality), 'description="Max Quality" ', 'min="0" ', 'max="{}" '.format((2 * max_quality)), 'type="RichDynamicFloat" ', '/>\n', '    <Param name="Inclusive" ', 'value="{}" '.format(str(inclusive).lower()), 'description="Inclusive Sel." ', 'type="RichBool" ', '/>\n', '  </filter>\n'])
    util.write_filter(script, filter_xml)
    return None

Select all the faces and vertexes within the specified vertex quality
range.

Args:
script: the FilterScript object or script filename to write
the filter] to.
min_quality (float): Minimum acceptable quality value.
max_quality (float): Maximum acceptable quality value.
inclusive (bool): If True only the faces with ALL the vertices within
the specified range are selected. Otherwise any face with at least
one vertex within the range is selected.

Layer stack:
No impacts

MeshLab versions:
2016.12
1.3.4BETA


def dates(self):
    return _gen_periodic_schedule(self._start_date, self._end_date, self._tenor, holiday_calendar=self._holiday_calendar, roll_convention=self._roll_convention, backward=self._backward, end_of_month=self._end_of_month)

Returns the dates as computed from the schedule as a DateTensor.

Constructs the date schedule from the supplied data. For more details see
the initializer docstring.

Returns:
`DateTensor` of rank one more than `start_date` or `end_date`
(depending on `backwards`), representing schedules for each element
of the input.


def update_q(self, predicted_q_arr, reward_value_arr, next_max_q_arr):

        return predicted_q_arr + (self.alpha_value * (reward_value_arr + (self.gamma_value * next_max_q_arr) - predicted_q_arr))

Update Q.

Args:
predicted_q_arr:    `np.ndarray` of predicted Q-Values.
reward_value_arr:   `np.ndarray` of reward values.
next_max_q_arr:     `np.ndarray` of maximum Q-Values in next time step.

Returns:
`np.ndarray` of real Q-Values.


def verify_bitcoin(message, signature, address):

        magic_sig = base64.b64decode(signature)

        magic = magic_sig[0]
        sig = Signature.from_bytes(magic_sig[1:])
        sig.recovery_id = (magic - 27) & 0x3
        compressed = ((magic - 27) & 0x4) != 0

        msg = b"\x18Bitcoin Signed Message:\n" + bytes([len(message)]) + message
        msg_hash = hashlib.sha256(msg).digest()

        derived_public_key = PublicKey.from_signature(msg_hash, sig)
        if derived_public_key is None:
            raise ValueError("Could not recover public key from the provided signature.")

        ver, h160 = address_to_key_hash(address)
        hash160 = derived_public_key.hash160(compressed)
        if hash160 != h160:
            return False

        return derived_public_key.verify(msg_hash, sig)

Verifies a message signed using PrivateKey.sign_bitcoin()
or any of the bitcoin utils (e.g. bitcoin-cli, bx, etc.)

Args:
message(bytes): The message that the signature corresponds to.
signature (bytes or str): A Base64 encoded signature
address (str): Base58Check encoded address.

Returns:
bool: True if the signature verified properly, False otherwise.


def GetDevicePath(device_handle):
    io_service_obj = iokit.IOHIDDeviceGetService(device_handle)
    str_buffer = ctypes.create_string_buffer(DEVICE_PATH_BUFFER_SIZE)
    iokit.IORegistryEntryGetPath(io_service_obj, K_IO_SERVICE_PLANE, str_buffer)
    return str_buffer.value

Obtains the unique path for the device.

Args:
device_handle: reference to the device

Returns:
A unique path for the device, obtained from the IO Registry


def vq_gating(x,
              num_experts,
              k,
              bneck,
              hparams=None,
              name="vq_gating"):

  with tf.variable_scope(name, reuse=tf.AUTO_REUSE):

    if hparams.use_scales:
      scales = tf.get_variable(
          "scales", [num_experts],
          tf.float32,
          initializer=tf.ones_initializer())
      scales = tf.nn.softmax(scales)
      hparams.scales = scales
    input_size = x.get_shape().as_list()[-1]
    batch_size = common_layers.shape_list(x)[0]

    if k > 1:

      x = tf.layers.dense(x, input_size * k)

      x = tf.reshape(x, [batch_size * k, input_size])
    inputs = tf.expand_dims(x, axis=1)
    inputs = tf.expand_dims(inputs, axis=1)

    hparams.z_size = int(math.log(num_experts, 2))
    hparams.hidden_size = input_size
    hparams.top_k = k
    d = bneck.discrete_bottleneck(inputs)
    centroids = None
    exp_discrete = d["discrete"]
    embed_lookup = d["embed"]
    extra_loss = d["loss"]
    if hparams.residual_centroids:
      centroids = embed_lookup(exp_discrete)
    top_k_indices = tf.squeeze(exp_discrete, axis=1)
    tf.summary.histogram("discrete_counts", top_k_indices)

    if k > 1:
      top_k_indices = tf.reshape(top_k_indices, [batch_size, k])

    top_k_gates = tf.ones([batch_size, k])

    gates = _rowwise_unsorted_segment_sum(top_k_gates, top_k_indices,
                                          num_experts)

    count_per_expert = tf.reduce_sum(gates, axis=0)
    if hparams.use_scales:
      scale_loss = tf.reduce_mean(tf.to_float(count_per_expert) * scales)
      extra_loss += scale_loss
    if common_layers.should_generate_summaries():
      tf.summary.histogram("vq_loss", extra_loss)
      tf.summary.historgram("scale_loss", scale_loss)
    return gates, extra_loss, centroids

VQ gating.

Args:
x: input Tensor with shape [batch_size, input_size]
num_experts: an integer
k: an integer - number of experts per example
bneck: a bottleneck object
hparams: optional hparams
name: an optional string

Returns:
gates: a Tensor with shape [batch_size, num_experts]
load: a Tensor with shape [num_experts]


def get_num_filters(layer):
    if (K.ndim(layer.output) == 2):
        return K.int_shape(layer.output)[(- 1)]
    channel_idx = (1 if (K.image_data_format() == 'channels_first') else (- 1))
    return K.int_shape(layer.output)[channel_idx]

Determines the number of filters within the given `layer`.

Args:
layer: The keras layer to use.

Returns:
Total number of filters within `layer`.
For `keras.layers.Dense` layer, this is the total number of outputs.


def get_sites_in_sphere(self, pt, r, include_index=False, include_image=False):
    site_fcoords = np.mod(self.frac_coords, 1)
    neighbors = []
    for (fcoord, dist, i, img) in self._lattice.get_points_in_sphere(site_fcoords, pt, r):
        nnsite = PeriodicSite(self[i].species, fcoord, self._lattice, properties=self[i].properties)
        nn_data = ((nnsite, dist) if (not include_index) else (nnsite, dist, i))
        if include_image:
            nn_data += (img,)
        neighbors.append(nn_data)
    return neighbors

Find all sites within a sphere from the point. This includes sites
in other periodic images.

Algorithm:

1. place sphere of radius r in crystal and determine minimum supercell
(parallelpiped) which would contain a sphere of radius r. for this
we need the projection of a_1 on a unit vector perpendicular
to a_2 & a_3 (i.e. the unit vector in the direction b_1) to
determine how many a_1"s it will take to contain the sphere.

Nxmax = r * length_of_b_1 / (2 Pi)

2. keep points falling within r.

Args:
pt (3x1 array): cartesian coordinates of center of sphere.
r (float): Radius of sphere.
include_index (bool): Whether the non-supercell site index
is included in the returned data
include_image (bool): Whether to include the supercell image
is included in the returned data

Returns:
[(site, dist) ...] since most of the time, subsequent processing
requires the distance.


def __init__(
      self, resolver_context, file_system, path_spec, is_root=False,
      is_virtual=False):

    gzip_file = resolver.Resolver.OpenFileObject(
        path_spec, resolver_context=resolver_context)
    if not gzip_file:
      raise errors.BackEndError('Missing gzip file.')

    super(GzipFileEntry, self).__init__(
        resolver_context, file_system, path_spec, is_root=is_root,
        is_virtual=is_virtual)
    self._gzip_file = gzip_file
    self.entry_type = definitions.FILE_ENTRY_TYPE_FILE

Initializes a file entry.

Args:
resolver_context (Context): resolver context.
file_system (FileSystem): file system.
path_spec (PathSpec): path specification.
is_root (Optional[bool]): True if the file entry is the root file entry
of the corresponding file system.
is_virtual (Optional[bool]): True if the file entry is a virtual file

Raises:
BackEndError: when the gzip file is missing.


def _ConvertDateTimeToOffset(self, date_time_value):

    date_time_obj = datetime.datetime(int(date_time_value['date']['year']),
                                      int(date_time_value['date']['month']),
                                      int(date_time_value['date']['day']),
                                      int(date_time_value['hour']),
                                      int(date_time_value['minute']),
                                      int(date_time_value['second']))

    if self._version > 'v201808':
      time_zone_str = 'timeZoneId'
    else:
      time_zone_str = 'timeZoneID'
    date_time_str = pytz.timezone(
        date_time_value[time_zone_str]).localize(date_time_obj).isoformat()

    if date_time_str[-5:] == '00:00':
      return date_time_str[:-6] + 'Z'
    else:
      return date_time_str

Converts the PQL formatted response for a dateTime object.

Output conforms to ISO 8061 format, e.g. 'YYYY-MM-DDTHH:MM:SSz.'

Args:
date_time_value: dict The date time value from the PQL response.

Returns:
str: A string representation of the date time value uniform to
ReportService.


def sia_bipartitions(nodes, node_labels=None):
    if config.CUT_ONE_APPROXIMATION:
        bipartitions = directed_bipartition_of_one(nodes)
    else:
        bipartitions = directed_bipartition(nodes, nontrivial=True)
    return [Cut(bipartition[0], bipartition[1], node_labels) for bipartition in bipartitions]

Return all |big_phi| cuts for the given nodes.

This value changes based on :const:`config.CUT_ONE_APPROXIMATION`.

Args:
nodes (tuple[int]): The node indices to partition.
Returns:
list[Cut]: All unidirectional partitions.


def write(name, value):

    def wrapped(func):
        @functools.wraps(func)
        def _decorator(*args, **kwargs):
            existing_env = core.read(name, allow_none=True)
            core.write(name, value)
            func_val = func(*args, **kwargs)
            core.write(name, existing_env)
            return func_val
        return _decorator
    return wrapped

Temporarily change or set the environment variable during the execution of a function.

Args:
name: The name of the environment variable
value: A value to set for the environment variable

Returns:
The function return value.


def _obtain_health_pills_at_step(self, events_directory, node_names, step):
    pattern = os.path.join(events_directory, _DEBUGGER_EVENTS_GLOB_PATTERN)
    file_paths = glob.glob(pattern)
    if (not file_paths):
        raise IOError(('No events files found that matches the pattern %r.' % pattern))
    file_paths.sort()
    mapping = collections.defaultdict(list)
    node_name_set = frozenset(node_names)
    for file_path in file_paths:
        should_stop = self._process_health_pill_event(node_name_set, mapping, step, file_path)
        if should_stop:
            break
    return mapping

Reads disk to obtain the health pills for a run at a specific step.

This could be much slower than the alternative path of just returning all
health pills sampled by the event multiplexer. It could take tens of minutes
to complete this call for large graphs for big step values (in the
thousands).

Args:
events_directory: The directory containing events for the desired run.
node_names: A list of node names for which to retrieve health pills.
step: The step to obtain health pills for.

Returns:
A dictionary mapping from node name to a list of health pill objects (see
docs for _serve_health_pills_handler for properties of those objects).

Raises:
IOError: If no files with health pill events could be found.


def stack1d(*points):

    result = np.empty((2, len(points)), order="F")
    for index, point in enumerate(points):
        result[:, index] = point
    return result

Fill out the columns of matrix with a series of points.

This is because ``np.hstack()`` will just make another 1D vector
out of them and ``np.vstack()`` will put them in the rows.

Args:
points (Tuple[numpy.ndarray, ...]): Tuple of 1D points (i.e.
arrays with shape ``(2,)``.

Returns:
numpy.ndarray: The array with each point in ``points`` as its
columns.


def _get_flow_for_token(csrf_token, request):

    flow_pickle = request.session.get(_FLOW_KEY.format(csrf_token), None)
    return None if flow_pickle is None else jsonpickle.decode(flow_pickle)

Looks up the flow in session to recover information about requested
scopes.

Args:
csrf_token: The token passed in the callback request that should
match the one previously generated and stored in the request on the
initial authorization view.

Returns:
The OAuth2 Flow object associated with this flow based on the
CSRF token.


def __delitem__(self, key):

        path = self.keypath(key)
        if fs.exists(path):
            fs.rm(path)
        else:
            raise KeyError(key)

Delete cached file.

Arguments:
key: Key.

Raises:
KeyError: If file not in cache.