def run_v2_only(func=None):

    def decorator(f):
        if tf_inspect.isclass(f):
            raise ValueError('`run_v2_only` only supports test methods.')

        def decorated(self, *args, **kwargs):
            if not tf2.enabled():
                self.skipTest('Test is only compatible with v2')
            return f(self, *args, **kwargs)
        return decorated
    if func is not None:
        return decorator(func)
    return decorator

Execute the decorated test only if running in v2 mode.

This function is intended to be applied to tests that exercise v2 only
functionality. If the test is run in v1 mode it will simply be skipped.

See go/tf-test-decorator-cheatsheet for the decorators to use in different
v1/v2/eager/graph combinations.

Args:
func: function to be annotated. If `func` is None, this method returns a
decorator the can be applied to a function. If `func` is not None this
returns the decorator applied to `func`.

Returns:
Returns a decorator that will conditionally skip the decorated test method.


def clear_descendants(self, source, clear_source=True):

        desc = nx.descendants(self, source)
        if clear_source:
            desc.add(source)
        self.remove_nodes_from(desc)
        return desc

Remove all descendants of(reachable from) `source`.

Args:
source: Node descendants
clear_source(bool): Remove origin too if True.
Returns:
set: The removed nodes.


def copy(self, filename, id_=(- 1), pre_callback=None, post_callback=None):
    for repo in self._children:
        if is_package(filename):
            copy_method = repo.copy_pkg
        else:
            copy_method = repo.copy_script
        if pre_callback:
            pre_callback(repo.connection)
        copy_method(filename, id_)
        if post_callback:
            post_callback(repo.connection)

Copy a package or script to all repos.

Determines appropriate location (for file shares) and type based
on file extension.

Args:
filename: String path to the local file to copy.
id_: Package or Script object ID to target. For use with JDS
and CDP DP's only. If uploading a package that does not
have a corresponding object, use id_ of -1, which is the
default.
pre_callback: Func to call before each distribution point
starts copying. Should accept a Repository connection
dictionary as a parameter. Will be called like:
`pre_callback(repo.connection)`
post_callback: Func to call after each distribution point
finishes copying. Should accept a Repository connection
dictionary as a parameter. Will be called like:
`pre_callback(repo.connection)`


def update_latest(bucket_name, version):

    bucket = get_bucket_resource(bucket_name)

    if version:
        new_ver = os.path.join('release', TARBALL_FORMAT.format(version))
        if not s3_file_exists(bucket, new_ver):
            log.error('Target file does not exist')
            return
    else:
        new_ver = max(x.key for x in bucket.objects.filter(Prefix='release'))

    bucket.put_object(
        Body=b'',
        Key=LATEST_TARBALL,
        WebsiteRedirectLocation='/{}'.format(new_ver)
    )
    log.info('Updated {} to point to {}'.format(LATEST_TARBALL, new_ver))

Update the cinq-frontend-latest.tar.gz redirect

Args:
bucket_name (str): Name of the bucket to upload to
version (str): Override build version. Defaults to using SCM based versioning (git tags)


def from_value(value):
    with ops.name_scope('optional') as scope:
        with ops.name_scope('value'):
            element_spec = structure.type_spec_from_value(value)
            encoded_value = structure.to_tensor_list(element_spec, value)
    return _OptionalImpl(gen_optional_ops.optional_from_value(encoded_value, name=scope), element_spec)

Returns a `tf.experimental.Optional` that wraps the given value.

>>> optional = tf.experimental.Optional.from_value(42)
>>> print(optional.has_value())
tf.Tensor(True, shape=(), dtype=bool)
>>> print(optional.get_value())
tf.Tensor(42, shape=(), dtype=int32)

Args:
value: A value to wrap. The value must be convertible to `tf.Tensor` or
`tf.CompositeTensor`.

Returns:
A `tf.experimental.Optional` that wraps `value`.


def ParseFileObject(self, parser_mediator, file_object):

    file_object.seek(0, os.SEEK_SET)
    header = file_object.read(2)
    if not self.BENCODE_RE.match(header):
      raise errors.UnableToParseFile('Not a valid Bencoded file.')

    file_object.seek(0, os.SEEK_SET)
    try:
      data_object = bencode.bdecode(file_object.read())

    except (IOError, bencode.BTFailure) as exception:
      raise errors.UnableToParseFile(
          '[{0:s}] unable to parse file: {1:s} with error: {2!s}'.format(
              self.NAME, parser_mediator.GetDisplayName(), exception))

    if not data_object:
      raise errors.UnableToParseFile(
          '[{0:s}] missing decoded data for file: {1:s}'.format(
              self.NAME, parser_mediator.GetDisplayName()))

    for plugin in self._plugins:
      try:
        plugin.UpdateChainAndProcess(parser_mediator, data=data_object)
      except errors.WrongBencodePlugin as exception:
        logger.debug('[{0:s}] wrong plugin: {1!s}'.format(
            self.NAME, exception))

Parses a bencoded file-like object.

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.


def ParseFileObject(self, parser_mediator, file_object):

    esedb_file = pyesedb.file()

    try:
      esedb_file.open_file_object(file_object)
    except IOError as exception:
      parser_mediator.ProduceExtractionWarning(
          'unable to open file with error: {0!s}'.format(exception))
      return

    cache = ESEDBCache()
    try:
      table_names = frozenset(self._GetTableNames(esedb_file))

      for plugin in self._plugins:
        if parser_mediator.abort:
          break

        if not plugin.required_tables.issubset(table_names):
          continue

        try:
          plugin.UpdateChainAndProcess(
              parser_mediator, cache=cache, database=esedb_file)

        except Exception as exception:
          parser_mediator.ProduceExtractionWarning((
              'plugin: {0:s} unable to parse ESE database with error: '
              '{1!s}').format(plugin.NAME, exception))

    finally:

      esedb_file.close()

Parses an ESE database file-like object.

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


def FoldByteStream(self, mapped_value, **unused_kwargs):

    value = None

    try:
      if self._byte_order == definitions.BYTE_ORDER_BIG_ENDIAN:
        value = mapped_value.bytes
      elif self._byte_order == definitions.BYTE_ORDER_LITTLE_ENDIAN:
        value = mapped_value.bytes_le

    except Exception as exception:
      error_string = (
          'Unable to write: {0:s} to byte stream with error: {1!s}').format(
              self._data_type_definition.name, exception)
      raise errors.FoldingError(error_string)

    return value

Folds the data type into a byte stream.

Args:
mapped_value (object): mapped value.

Returns:
bytes: byte stream.

Raises:
FoldingError: if the data type definition cannot be folded into
the byte stream.


def __init__(self, input_circuit: circuit.QuantumCircuit, name: Union[None, str]=None):
    super().__init__(input_circuit, name)
    self._expectation_layer = tfq.layers.Expectation()

Initialize an AnalyticQuantumInference layer.

Args:
input_circuit: The parameterized quantum circuit on which to do inference.
name: Identifier for this inference engine.


def convert_adaptive_max_pool2d(params, w_name, scope_name, inputs, layers, weights, names):
    print('Converting adaptive_avg_pool2d...')
    if (names == 'short'):
        tf_name = ('APOL' + random_string(4))
    elif (names == 'keep'):
        tf_name = w_name
    else:
        tf_name = (w_name + str(random.random()))
    global_pool = keras.layers.GlobalMaxPooling2D(data_format='channels_first', name=tf_name)
    layers[scope_name] = global_pool(layers[inputs[0]])

    def target_layer(x):
        import keras
        return keras.backend.expand_dims(x)
    lambda_layer = keras.layers.Lambda(target_layer, name=(tf_name + 'E'))
    layers[scope_name] = lambda_layer(layers[scope_name])
    layers[scope_name] = lambda_layer(layers[scope_name])

Convert convert_adaptive_max_pool2d layer.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def cumsum(vari, axis=None):
    if isinstance(vari, Poly):
        core = vari.A.copy()
        for (key, val) in core.items():
            core[key] = cumsum(val, axis)
        return Poly(core, vari.dim, None, vari.dtype)
    return np.cumsum(vari, axis)

Cumulative sum the components of a shapeable quantity along a given axis.

Args:
vari (chaospy.poly.base.Poly, numpy.ndarray):
Input data.
axis (int):
Axis over which the sum is taken. By default ``axis`` is None, and
all elements are summed.

Returns:
(chaospy.poly.base.Poly, numpy.ndarray):
Polynomial array with same shape as ``vari``.

Examples:
>>> poly = cp.prange(3)
>>> print(poly)
[1, q0, q0^2]
>>> print(cp.cumsum(poly))
[1, q0+1, q0^2+q0+1]


def random_string(length=8, charset=None):
    if (length < 1):
        raise ValueError('Length must be > 0')
    if (not charset):
        charset = (string.letters + string.digits)
    return ''.join((random.choice(charset) for unused in xrange(length)))

Generates a string with random characters. If no charset is specified, only
letters and digits are used.

Args:
length (int) length of the returned string
charset (string) list of characters to choose from
Returns:
(str) with random characters from charset
Raises:
-


def get_shape_str(tensors):

    if isinstance(tensors, (list, tuple)):
        for v in tensors:
            assert isinstance(v, (tf.Tensor, tf.Variable)), "Not a tensor: {}".format(type(v))
        shape_str = ",".join(
            map(lambda x: str(x.get_shape().as_list()), tensors))
    else:
        assert isinstance(tensors, (tf.Tensor, tf.Variable)), "Not a tensor: {}".format(type(tensors))
        shape_str = str(tensors.get_shape().as_list())
    return shape_str

Internally used by layer registry, to print shapes of inputs/outputs of layers.

Args:
tensors (list or tf.Tensor): a tensor or a list of tensors
Returns:
str: a string to describe the shape


def run_configure(self, mandatory=True):

        if not has_file(self.working_dir, 'configure'):
            if mandatory:
                raise FileNotFoundError(
                    "Could not find a configure script for execution.")
            else:
                return
        try:
            prog = RunningProgram(self, 'configure')
            prog.expect_exit_status(0)
        except Exception:
            if mandatory:
                raise

Runs the 'configure' program in the working directory.

Args:
mandatory (bool): Throw exception if 'configure' fails or a
'configure' file is missing.


def trees_by_issn(self, issn):
    return set(self.issn_db.get(issn, OOSet()).keys())

Search trees by `issn`.

Args:
issn (str): :attr:`.Tree.issn` property of :class:`.Tree`.

Returns:
set: Set of matching :class:`Tree` instances.


def get_role_arn(role_name, env, region):

    session = boto3.Session(profile_name=env, region_name=region)
    iam_client = session.client('iam')

    LOG.debug('Searching for %s.', role_name)

    role = iam_client.get_role(RoleName=role_name)
    role_arn = role['Role']['Arn']

    LOG.debug("Found role's %s ARN %s", role_name, role_arn)

    return role_arn

Get role ARN given role name.

Args:
role_name (str): Role name to lookup
env (str): Environment in which to lookup
region (str): Region

Returns:
ARN if role found


def strip_quotes(self, content):

        error_msg = "Following rule is badly quoted: {}"
        if (content.startswith('"') and content.endswith('"')) or \
           (content.startswith("'") and content.endswith("'")):
            return content[1:-1]

        elif (content.startswith('"') and not content.endswith('"')) or \
             (content.startswith("'") and not content.endswith("'")):
            raise InvalidImportRule(error_msg.format(content))

        elif (not content.startswith('"') and content.endswith('"')) or \
             (not content.startswith("'") and content.endswith("'")):
            raise InvalidImportRule(error_msg.format(content))

        return content

Unquote given rule.

Args:
content (str): An import rule.

Raises:
InvalidImportRule: Raise exception if the rule is badly quoted
(not started or not ended quotes).

Returns:
string: The given rule unquoted.


def _ReadRecord(self, tables, file_object, record_offset, record_type):

    table = tables.get(record_type, None)
    if not table:
      raise errors.ParseError(
          'Missing table for relation identifier: 0x{0:08}'.format(record_type))

    record_header = self._ReadRecordHeader(file_object, record_offset)

    record = collections.OrderedDict()

    if table.columns:
      attribute_value_offsets = self._ReadRecordAttributeValueOffset(
          file_object, record_offset + 24, len(table.columns))

    file_offset = file_object.tell()
    record_data_offset = file_offset - record_offset
    record_data_size = record_header.data_size - (file_offset - record_offset)
    record_data = file_object.read(record_data_size)

    if record_header.key_data_size > 0:
      record['_key_'] = record_data[:record_header.key_data_size]

    if table.columns:
      for index, column in enumerate(table.columns):
        attribute_data_read_function = self._ATTRIBUTE_DATA_READ_FUNCTIONS.get(
            column.attribute_data_type, None)
        if attribute_data_read_function:
          attribute_data_read_function = getattr(
              self, attribute_data_read_function, None)

        if not attribute_data_read_function:
          attribute_value = None
        else:
          attribute_value = attribute_data_read_function(
              record_data, record_offset, record_data_offset,
              attribute_value_offsets[index])

        record[column.attribute_name] = attribute_value

    table.records.append(record)

Reads the record.

Args:
tables (dict[int, KeychainDatabaseTable]): tables per identifier.
file_object (file): file-like object.
record_offset (int): offset of the record relative to the start of
the file.
record_type (int): record type, which should correspond to a relation
identifier of a table defined in the schema.

Raises:
ParseError: if the record cannot be read.


def format(self, record):
    level = record.levelno
    if (level >= logging.CRITICAL):
        color = self.CRITICAL
    elif (level >= logging.ERROR):
        color = self.ERROR
    elif (level >= logging.WARNING):
        color = self.WARNING
    elif (level >= logging.INFO):
        color = self.INFO
    elif (level >= logging.DEBUG):
        color = self.DEBUG
    else:
        color = self.DEFAULT
    message = super().format(record)
    if record.args:
        try:
            message = (message % record.args)
        except TypeError:
            pass
    return ((color + message) + self.DEFAULT)

Adds colors to a log record and formats it with the default

Args:
record (logging.LogRecord): log record to format

Returns:
str: The colored and formatted record string


def convert_hardtanh(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting hardtanh (clip) ...')

    def target_layer(x, max_val=float(params['max_val']), min_val=float(params['min_val'])):
        return tf.minimum(max_val, tf.maximum(min_val, x))

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

Convert hardtanh layer.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def setup(app):

    for name, (default, rebuild, _) in ref.CONFIG_VALUES.iteritems():
        app.add_config_value(name, default, rebuild)

    app.add_directive('javaimport', ref.JavarefImportDirective)
    app.add_role('javaref', ref.JavarefRole(app))

    app.connect('builder-inited', initialize_env)
    app.connect('env-purge-doc', ref.purge_imports)
    app.connect('env-merge-info', ref.merge_imports)
    app.connect('build-finished', ref.cleanup)

Register the extension with Sphinx.

Args:
app: The Sphinx application.


def export(self, template_file_name, output_file_name,
               sort="public", data=None, limit=0):

        exportedData = {}
        exportedUsers = self.__exportUsers(sort, limit)

        exportedData["users"] = exportedUsers
        exportedData["extraData"] = data

        with open(template_file_name) as template_file:
            template_raw = template_file.read()

        template = parse(template_raw)
        renderer = Renderer()

        output = renderer.render(template, exportedData)

        with open(output_file_name, "w") as text_file:
            text_file.write(output)

Export ranking to a file.

Args:
template_file_name (str): where is the template
(moustache template)
output_file_name (str): where create the file with the ranking
sort (str): field to sort the users


def _probe_characteristics(self, conn, services, timeout=5.0):

        for service in services.values():
            success, result = self._enumerate_handles(conn, service['start_handle'],
                                                      service['end_handle'])

            if not success:
                return False, None

            attributes = result['attributes']

            service['characteristics'] = {}

            last_char = None
            for handle, attribute in attributes.items():
                if attribute['uuid'].hex[-4:] == '0328':
                    success, result = self._read_handle(conn, handle, timeout)
                    if not success:
                        return False, None

                    value = result['data']
                    char = parse_characteristic_declaration(value)
                    service['characteristics'][char['uuid']] = char
                    last_char = char
                elif attribute['uuid'].hex[-4:] == '0229':
                    if last_char is None:
                        return False, None

                    success, result = self._read_handle(conn, handle, timeout)
                    if not success:
                        return False, None

                    value = result['data']
                    assert len(value) == 2
                    value, = unpack("<H", value)

                    last_char['client_configuration'] = {'handle': handle, 'value': value}

        return True, {'services': services}

Probe gatt services for all associated characteristics in a BLE device

Args:
conn (int): the connection handle to probe
services (dict): a dictionary of services produced by probe_services()
timeout (float): the maximum number of seconds to spend in any single task


def get_dataset(categories: list, split: str='train'):
    labels = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
    label_map = {class_name: class_id for class_id, class_name in enumerate(labels)}
    labels_subset = np.array([label_map[class_name] for class_name in categories])
    emotion_dataset = load_dataset('emotion', download_mode='force_redownload')
    X, y = (np.array(emotion_dataset[split]['text']), np.array(emotion_dataset[split]['label']))
    subclass_idxs = [idx for idx, label in enumerate(y) if label in labels_subset]
    X_subset, y_subset = (X[subclass_idxs], y[subclass_idxs])
    return (X_subset.tolist(), y_subset.tolist())

It takes a list of categories and a split (train/test/dev) and returns the
corresponding subset of the dataset

Args:
categories (list): list of emotion categories to use
split (str): The split of the dataset to use. Can be either "train", "dev", or "test".
Defaults to train

Returns:
A list of text and a list of labels


def _verify_cartesian(s, t):
    if ((s < 0.0) or (t < 0.0) or ((s + t) > 1.0)):
        raise ValueError('Point lies outside reference triangle', s, t)

Verifies that a point is in the reference triangle.

I.e., checks that they sum to <= one and are each non-negative.

Args:
s (float): Parameter along the reference triangle.
t (float): Parameter along the reference triangle.

Raises:
ValueError: If the point lies outside the reference triangle.


def kind(self, value):

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

The kind property.

Args:
value (:class:`DataPointType.measurement`). the property value.


def run_pip_command(command_args, pip_version=None, python_version=None):
    (pip_exe, context) = find_pip(pip_version, python_version)
    command = ([pip_exe] + list(command_args))
    if (context is None):
        return popen(command)
    else:
        return context.execute_shell(command=command, block=False)

Run a pip command.

Args:
command_args (list of str): Args to pip.

Returns:
`subprocess.Popen`: Pip process.


def from_status(cls, status_line, msg=None):
    method = getattr(cls, status_line.lower()[4:].replace(' ', '_'))
    return method(msg)

Returns a class method from bottle.HTTPError.status_line attribute.
Useful for patching `bottle.HTTPError` for web services.

Args:
status_line (str):  bottle.HTTPError.status_line text.
msg: The message data for response.

Returns:
Class method based on status_line arg.

Examples:
>>> status_line = '401 Unauthorized'
>>> error_msg = 'Get out!'
>>> resp = WSResponse.from_status(status_line, error_msg)
>>> resp['errors']
['Get out!']
>>> resp['status_text']
'Unauthorized'


def __init__(self, max_drift=None, to_average=3, enaug_multiply=2):

        self.max_drift = max_drift
        self.to_average = int(to_average)
        self.enaug_multiply = enaug_multiply

Initializes the handler with max drift
Args:
max_drift (float): This defines the max drift. Leaving this at the default of None gets the max_drift from EDFIFFG


def avg_dicts(dictin1, dictin2, dropmissing=True):

    dictout = dict()
    for key in dictin1:
        if key in dictin2:
            dictout[key] = (dictin1[key] + dictin2[key]) / 2
        elif not dropmissing:
            dictout[key] = dictin1[key]
    if not dropmissing:
        for key in dictin2:
            if key not in dictin1:
                dictout[key] = dictin2[key]
    return dictout

Create a new dictionary from two dictionaries by averaging values

Args:
dictin1 (DictUpperBound): First input dictionary
dictin2 (DictUpperBound): Second input dictionary
dropmissing (bool): Whether to drop keys missing in one dictionary. Defaults to True.

Returns:
Dict: Dictionary with values being average of 2 input dictionaries


def task_table(self, task_id=None):

        self._check_connected()
        if task_id is not None:
            task_id = ray.TaskID(hex_to_binary(task_id))
            return self._task_table(task_id)
        else:
            task_table_keys = self._keys(
                ray.gcs_utils.TablePrefix_RAYLET_TASK_string + "*")
            task_ids_binary = [
                key[len(ray.gcs_utils.TablePrefix_RAYLET_TASK_string):]
                for key in task_table_keys
            ]

            results = {}
            for task_id_binary in task_ids_binary:
                results[binary_to_hex(task_id_binary)] = self._task_table(
                    ray.TaskID(task_id_binary))
            return results

Fetch and parse the task table information for one or more task IDs.

Args:
task_id: A hex string of the task ID to fetch information about. If
this is None, then the task object table is fetched.

Returns:
Information from the task table.


def add_file_to_tree(tree, file_path, file_contents, is_executable=False):
    record = {'path': file_path, 'mode': ('100755' if is_executable else '100644'), 'type': 'blob', 'content': file_contents}
    tree.append(record)
    return tree

Add a file to a tree.

Args:

tree
A list of dicts containing info about each blob in a tree.

file_path
The path of the new file in the tree.

file_contents
The (UTF-8 encoded) contents of the new file.

is_executable
If ``True``, the new file will get executable permissions (0755).
Otherwise, it will get 0644 permissions.

Returns:
The provided tree, but with the new file added.


def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple[int, int]]]=None) -> 'torch.Tensor':
    class_queries_logits = outputs.class_queries_logits
    masks_queries_logits = outputs.masks_queries_logits
    masks_classes = class_queries_logits.softmax(dim=-1)[..., :-1]
    masks_probs = masks_queries_logits.sigmoid()
    segmentation = torch.einsum('bqc, bqhw -> bchw', masks_classes, masks_probs)
    batch_size = class_queries_logits.shape[0]
    if target_sizes is not None:
        if batch_size != len(target_sizes):
            raise ValueError('Make sure that you pass in as many target sizes as the batch dimension of the logits')
        semantic_segmentation = []
        for idx in range(batch_size):
            resized_logits = torch.nn.functional.interpolate(segmentation[idx].unsqueeze(dim=0), size=target_sizes[idx], mode='bilinear', align_corners=False)
            semantic_map = resized_logits[0].argmax(dim=0)
            semantic_segmentation.append(semantic_map)
    else:
        semantic_segmentation = segmentation.argmax(dim=1)
        semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])]
    return semantic_segmentation

Converts the output of [`MaskFormerForInstanceSegmentation`] into semantic segmentation maps. Only supports
PyTorch.

Args:
outputs ([`MaskFormerForInstanceSegmentation`]):
Raw outputs of the model.
target_sizes (`List[Tuple[int, int]]`, *optional*):
List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested
final size (height, width) of each prediction. If left to None, predictions will not be resized.
Returns:
`List[torch.Tensor]`:
A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width)
corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each
`torch.Tensor` correspond to a semantic class id.


def discriminator(self, frames):

    ndf = self.hparams.num_discriminator_filters
    frames = tf.stack(frames)

    frames = common_video.swap_time_and_batch_axes(frames)

    num_outputs = [ndf, ndf*2, ndf*2, ndf*4, ndf*4, ndf*8, ndf*8]
    kernel_sizes = [3, 4, 3, 4, 3, 4, 3]
    strides = [[1, 1, 1], [1, 2, 2], [1, 1, 1], [1, 2, 2], [1, 1, 1],
               [2, 2, 2], [1, 1, 1]]

    names = ["video_sn_conv0_0", "video_sn_conv0_1", "video_sn_conv1_0",
             "video_sn_conv1_1", "video_sn_conv2_0", "video_sn_conv2_1",
             "video_sn_conv3_0"]
    iterable = zip(num_outputs, kernel_sizes, strides, names)
    activations = frames
    for num_filters, kernel_size, stride, name in iterable:
      activations = self.pad_conv3d_lrelu(activations, num_filters, kernel_size,
                                          stride, name)
    num_fc_dimensions = self.get_fc_dimensions(strides, kernel_sizes)
    activations = tf.reshape(activations, (-1, num_fc_dimensions))
    return tf.squeeze(tf.layers.dense(activations, 1))

3-D SNGAN discriminator.

Args:
frames: a list of batch-major tensors indexed by time.

Returns:
logits: 1-D Tensor with shape=batch_size.
Positive logits imply that the discriminator thinks that it
belongs to the true class.


def gauge(namespace: Union[Type, str], name: str) -> 'Metrics.DelegatingGauge':
    namespace = Metrics.get_namespace(namespace)
    return Metrics.DelegatingGauge(MetricName(namespace, name))

Obtains or creates a Gauge metric.

Gauge metrics are restricted to integer-only values.

Args:
namespace: A class or string that gives the namespace to a metric
name: A string that gives a unique name to a metric

Returns:
A Distribution object.


def register_func(self, func_name, func_kwargs):
    self.scan_funcs.append(func_name)
    self.scan_kwargs.append(func_kwargs)

Register a scan function

Args:
func_name: The function name of a scan function.
func_kwargs: A dict containing arguments of the scan function.


def matches(self, node, value):

        if self.skip(value):
            return True

        if not self._valid_value(value):
            msg = "Invalid value {value} passed to filter {name} - ".format(
                value=repr(value),
                name=self.name)

            if self.default is not None:
                warn(msg + "defaulting to {}".format(self.default))
                value = self.default
            else:
                warn(msg + "skipping")
                return True

        return self.func(node, value)

Returns whether the given node matches the filter rule with the given value.

Args:
node (Element): The node to filter.
value (object): The desired value with which the node should be evaluated.

Returns:
bool: Whether the given node matches.


def run_step(context):
    logger.debug('started')
    deprecated(context)
    context.assert_key_has_value(key='fetchYaml', caller=__name__)
    fetch_yaml_input = context.get_formatted('fetchYaml')
    if isinstance(fetch_yaml_input, str):
        file_path = fetch_yaml_input
        destination_key_expression = None
    else:
        context.assert_child_key_has_value(parent='fetchYaml', child='path', caller=__name__)
        file_path = fetch_yaml_input['path']
        destination_key_expression = fetch_yaml_input.get('key', None)
    logger.debug(f'attempting to open file: {file_path}')
    with open(file_path) as yaml_file:
        yaml_loader = yaml.YAML(typ='safe', pure=True)
        payload = yaml_loader.load(yaml_file)
    if destination_key_expression:
        destination_key = context.get_formatted_iterable(destination_key_expression)
        logger.debug(f'yaml file loaded. Writing to context {destination_key}')
        context[destination_key] = payload
    else:
        if (not isinstance(payload, MutableMapping)):
            raise TypeError("yaml input should describe a dictionary at the top level when fetchYamlKey isn't specified. You should have something like \n'key1: value1'\n key2: value2'\nin the yaml top-level, not \n'- value1\n - value2'")
        logger.debug('yaml file loaded. Merging into pypyr context. . .')
        context.update(payload)
    logger.info(f'yaml file written into pypyr context. Count: {len(payload)}')
    logger.debug('done')

Load a yaml file into the pypyr context.

Yaml parsed from the file will be merged into the pypyr context. This will
overwrite existing values if the same keys are already in there.
I.e if file yaml has {'eggs' : 'boiled'} and context {'eggs': 'fried'}
already exists, returned context['eggs'] will be 'boiled'.

Args:
context: pypyr.context.Context. Mandatory.
The following context key must exist
- fetchYaml
- path. path-like. Path to file on disk.
- key. string. If exists, write yaml to this context key.
Else yaml writes to context root.

All inputs support formatting expressions.

Also supports a passing path as string to fetchYaml, but in this case you
won't be able to specify a key.

Returns:
None. updates context arg.

Raises:
FileNotFoundError: take a guess
pypyr.errors.KeyNotInContextError: fetchYamlPath missing in context.
pypyr.errors.KeyInContextHasNoValueError: fetchYamlPath exists but is
None.


def write_line(self, values):
    if self.inner_table is None:
        self.inner_table = [list(values.keys()), list(values.values())]
    else:
        columns = self.inner_table[0]
        for key in values.keys():
            if key not in columns:
                columns.append(key)
        self.inner_table[0] = columns
        if len(self.inner_table) > 1:
            last_values = self.inner_table[-1]
            first_column = self.inner_table[0][0]
            if last_values[0] != values[first_column]:
                self.inner_table.append([values[c] if c in values else 'No Log' for c in columns])
            else:
                new_values = values
                for c in columns:
                    if c not in new_values.keys():
                        new_values[c] = last_values[columns.index(c)]
                self.inner_table[-1] = [new_values[c] for c in columns]
        else:
            self.inner_table.append([values[c] for c in columns])

Write the values in the inner table.

Args:
values (`Dict[str, float]`): The values to display.


def copy(source, destination, ignore=None, adapter=None, fatal=True, logger=LOG.debug):
    return _file_op(source, destination, _copy, adapter, fatal, logger, ignore=ignore)

Copy source -> destination

Args:
source (str | None): Source file or folder
destination (str | None): Destination file or folder
ignore (callable | list | str | None): Names to be ignored
adapter (callable | None): Optional function to call on 'source' before copy
fatal (bool | None): Abort execution on failure if True
logger (callable | None): Logger to use

Returns:
(int): 1 if effectively done, 0 if no-op, -1 on failure


def find(self, vid=None, pid=None, serial=None, interface=None, path=None, release_number=None, manufacturer=None, product=None, usage=None, usage_page=None):
    result = []
    for dev in self.device_list:
        if ((vid not in [0, None]) and (dev.vendor_id != vid)):
            continue
        if ((pid not in [0, None]) and (dev.product_id != pid)):
            continue
        if (serial and (dev.serial_number != serial)):
            continue
        if (path and (dev.path != path)):
            continue
        if (manufacturer and (dev.manufacturer_string != manufacturer)):
            continue
        if (product and (dev.product_string != product)):
            continue
        if ((release_number != None) and (dev.release_number != release_number)):
            continue
        if ((interface != None) and (dev.interface_number != interface)):
            continue
        if ((usage != None) and (dev.usage != usage)):
            continue
        if ((usage_page != None) and (dev.usage_page != usage_page)):
            continue
        result.append(dev)
    return result

Attempts to open a device in this `Enumeration` object. Optional
arguments can be provided to filter the resulting list based on various
parameters of the HID devices.

Args:
vid: filters by USB Vendor ID
pid: filters by USB Product ID
serial: filters by USB serial string (.iSerialNumber)
interface: filters by interface number (bInterfaceNumber)
release_number: filters by the USB release number (.bcdDevice)
manufacturer: filters by USB manufacturer string (.iManufacturer)
product: filters by USB product string (.iProduct)
usage: filters by HID usage
usage_page: filters by HID usage_page
path: filters by HID API path.


def boxed(msg, ch='=', pad=5):
    if (pad > 0):
        msg = (((((pad * ch) + ' ') + msg.strip()) + ' ') + (pad * ch))
    return '\n'.join([(len(msg) * ch), msg, (len(msg) * ch)])

Returns a string in a box

Args:
msg: Input string.
ch: Character used to form the box.
pad: Number of characters ch added before and after msg.

>>> print(boxed("hello", ch="*", pad=2))
***********
** hello **
***********


def boolean(cls, true_code, false_code=None):

    def func(response):
        if (response is not None):
            status_code = response.status
            if (status_code == true_code):
                return True
            if ((false_code is not None) and (status_code == false_code)):
                return False
            raise error_for(response)
    return func

Callback to validate a response code.

The returned callback checks whether a given response has a
``status_code`` that is considered good (``true_code``) and
raise an appropriate error if not.

The optional ``false_code`` allows for a non-successful status
code to return False instead of throwing an error. This is used,
for example in relationship mutation to indicate that the
relationship was not modified.

Args:

true_code(int): The http status code to consider as a success

Keyword Args:

false_code(int): The http status code to consider a failure

Returns:

A function that given a response returns ``True`` if the
response's status code matches the given code. Raises
a :class:`HeliumError` if the response code does not
match.


def prettify(unicode_text):
    import xml.dom.minidom
    reparsed = xml.dom.minidom.parseString(unicode_text.encode('utf-8'))
    return reparsed.toprettyxml(indent='  ', newl='\n')

Return a pretty-printed version of a unicode XML string.

Useful for debugging.

Args:
unicode_text (str): A text representation of XML (unicode,
*not* utf-8).

Returns:
str: A pretty-printed version of the input.


def __init__(self, tpu_cluster_resolver=None, experimental_device_assignment=None, experimental_spmd_xla_partitioning=False):
    super().__init__(TPUExtended(self, tpu_cluster_resolver, device_assignment=experimental_device_assignment, use_spmd_for_xla_partitioning=experimental_spmd_xla_partitioning))
    distribute_lib.distribution_strategy_gauge.get_cell('V2').set('TPUStrategy')
    distribute_lib.distribution_strategy_replica_gauge.get_cell('num_workers').set(self.extended.num_hosts)
    distribute_lib.distribution_strategy_replica_gauge.get_cell('num_replicas_per_worker').set(self.extended.num_replicas_per_host)
    self._enable_packed_variable_in_eager_mode = True

Synchronous training in TPU donuts or Pods.

Args:
tpu_cluster_resolver: A
`tf.distribute.cluster_resolver.TPUClusterResolver` instance, which
provides information about the TPU cluster. If None, it will assume
running on a local TPU worker.
experimental_device_assignment: Optional
`tf.tpu.experimental.DeviceAssignment` to specify the placement of
replicas on the TPU cluster.
experimental_spmd_xla_partitioning: If True, enable the SPMD (Single
Program Multiple Data) mode in XLA compiler. This flag only affects the
performance of XLA compilation and the HBM requirement of the compiled
TPU program. Ceveat: if this flag is True, calling
`tf.distribute.TPUStrategy.experimental_assign_to_logical_device` will
result in a ValueError.


def get_cpus_by_arch(cls, arch):
    with open('/usr/share/libvirt/cpu_map.xml', 'r') as cpu_map:
        cpu_xml = ET.parse(cpu_map)
    try:
        return cpu_xml.xpath('/cpus/arch[@name="{0}"]'.format(arch))[0]
    except IndexError:
        raise LagoException('No such arch: {0}'.format(arch))

Get all CPUs info by arch

Args:
arch(str): CPU architecture

Returns:
lxml.etree.element: CPUs by arch XML

Raises:
:exc:`~LagoException`: If no such ARCH is found


def __init__(self, feature_names, feature_values, scaled_row,
                 categorical_features, discretized_feature_names=None):

        self.exp_feature_names = feature_names
        self.discretized_feature_names = discretized_feature_names
        self.feature_names = feature_names
        self.feature_values = feature_values
        self.scaled_row = scaled_row
        self.all_categorical = len(categorical_features) == len(scaled_row)
        self.categorical_features = categorical_features

Init.

Args:
feature_names: list of feature names, in order
feature_values: list of strings with the values of the original row
scaled_row: scaled row
categorical_features: list of categorical features ids (ints)


def pb(name, data, display_name=None, description=None):
    import tensorflow.compat.v1 as tf
    try:
        tensor = tf.make_tensor_proto(data, dtype=tf.string)
    except TypeError as e:
        raise ValueError(e)
    if (display_name is None):
        display_name = name
    summary_metadata = metadata.create_summary_metadata(display_name=display_name, description=description)
    tf_summary_metadata = tf.SummaryMetadata.FromString(summary_metadata.SerializeToString())
    summary = tf.Summary()
    summary.value.add(tag=('%s/text_summary' % name), metadata=tf_summary_metadata, tensor=tensor)
    return summary

Create a legacy text summary protobuf.

Arguments:
name: A name for the generated node. Will also serve as a series name in
TensorBoard.
data: A Python bytestring (of type bytes), or Unicode string. Or a numpy
data array of those types.
display_name: Optional name for this summary in TensorBoard, as a
`str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.

Raises:
ValueError: If the type of the data is unsupported.

Returns:
A `tf.Summary` protobuf object.


def has_button(self, button):
    rc = self._libinput.libinput_device_pointer_has_button(self._handle, button)
    assert (rc >= 0), 'This device is not a pointer device'
    return bool(rc)

Check if this device has a given button.

Args:
button (int): Button to check for, see ``input.h`` for button
definitions.
Returns:
bool: :obj:`True` if the device has this button, :obj:`False` if
it does not.
Raises:
AssertionError


def _inv_hessian_control_inputs(inv_hessian):

  is_positive_definite = tf.reduce_all(
      input_tensor=tf.math.is_finite(tf.linalg.cholesky(inv_hessian)),
      axis=[-1, -2])

  is_symmetric = tf.equal(bfgs_utils.norm(
      inv_hessian - _batch_transpose(inv_hessian), dims=2), 0)

  return [tf.Assert(is_positive_definite,
                    ['Initial inverse Hessian is not positive definite.',
                     inv_hessian]),
          tf.Assert(is_symmetric,
                    ['Initial inverse Hessian is not symmetric',
                     inv_hessian])]

Computes control inputs to validate a provided inverse Hessian.

These ensure that the provided inverse Hessian is positive definite and
symmetric.

Args:
inv_hessian: The starting estimate for the inverse of the Hessian at the
initial point.

Returns:
A list of tf.Assert ops suitable for use with tf.control_dependencies.


def stop_worker(config, *, worker_ids=None):

    if worker_ids is not None and not isinstance(worker_ids, list):
        worker_ids = [worker_ids]

    celery_app = create_app(config)
    celery_app.control.shutdown(destination=worker_ids)

Stop a worker process.

Args:
config (Config): Reference to the configuration object from which the
settings for the worker are retrieved.
worker_ids (list): An optional list of ids for the worker that should be stopped.


def validate(self, scope: ValidationScope=ValidationScope.all, ctype: ContentType=ContentType.config) -> None:
    self.schema_node._validate(self, scope, ctype)

Validate the receiver's value.

Args:
scope: Scope of the validation (syntax, semantics or all).
ctype: Receiver's content type.

Raises:
SchemaError: If the value doesn't conform to the schema.
SemanticError: If the value violates a semantic constraint.
YangTypeError: If the value is a scalar of incorrect type.


def identicaltest(tc, init1, init2, shape=None):
    if shape is None:
        shape = [100]
    with tc.test_session(graph=ops.Graph()):
        t1 = init1(shape).eval()
    with tc.test_session(graph=ops.Graph()):
        t2 = init2(shape).eval()
    return np.allclose(t1, t2, rtol=1e-15, atol=1e-15)

Tests if two initializations are identical to within tiny tolerances.

Args:
tc: An instance of TensorFlowTestCase.
init1: An Initializer that generates a tensor of a given shape
init2: An Initializer that generates a tensor of a given shape
shape: Shape of the tensor to initialize or `None` to use a vector of length
100.

Returns:
True or False as determined by test.


def coordination_number_delta_E(self):
    initial_site_neighbours = [s for s in self.initial_site.p_neighbours if s.is_occupied]
    final_site_neighbours = [s for s in self.final_site.p_neighbours if (s.is_occupied and (s is not self.initial_site))]
    initial_cn_occupation_energy = ((self.initial_site.cn_occupation_energy() + sum([site.cn_occupation_energy() for site in initial_site_neighbours])) + sum([site.cn_occupation_energy() for site in final_site_neighbours]))
    final_cn_occupation_energy = ((self.final_site.cn_occupation_energy(delta_occupation={self.initial_site.label: (- 1)}) + sum([site.cn_occupation_energy(delta_occupation={self.initial_site.label: (- 1)}) for site in initial_site_neighbours])) + sum([site.cn_occupation_energy(delta_occupation={self.final_site.label: (+ 1)}) for site in final_site_neighbours]))
    return (final_cn_occupation_energy - initial_cn_occupation_energy)

Coordination-number dependent energy conrtibution to the change in system energy if this jump were accepted.

Args:
None

Returns:
(Float): delta E (coordination-number)


def to_env_var(env_var: str, value) -> str:

    val = to_yaml(value)
    ret_val = "%s=%s" % (env_var, escape_yaml(val))
    return ret_val

Create an environment variable from a name and a value.

This generates a shell-compatible representation of an
environment variable that is assigned a YAML representation of
a value.

Args:
env_var (str): Name of the environment variable.
value (Any): A value we convert from.


def to_agraph(
        self,
        indicators: bool = False,
        indicator_values: bool = False,
        nodes_to_highlight=None,
        *args,
        **kwargs,
    ):

        from delphi.utils.misc import choose_font

        FONT = choose_font()
        A = nx.nx_agraph.to_agraph(self)
        A.graph_attr.update(
            {
                "dpi": 227,
                "fontsize": 20,
                "rankdir": kwargs.get("rankdir", "TB"),
                "fontname": FONT,
                "overlap": "scale",
                "splines": True,
            }
        )

        A.node_attr.update(
            {
                "shape": "rectangle",
                "color": "black",

                "style": "rounded",
                "fontname": FONT,
            }
        )

        nodes_with_indicators = [
            n
            for n in self.nodes(data=True)
            if n[1].get("indicators") is not None
        ]

        n_max = max(
            [
                sum([len(s.evidence) for s in e[2]["InfluenceStatements"]])
                for e in self.edges(data=True)
            ]
        )

        nodeset = {n.split("/")[-1] for n in self.nodes}

        simplified_labels = len(nodeset) == len(self)
        color_str = "
        for n in self.nodes(data=True):
            if kwargs.get("values"):
                node_label = (
                    n[0].capitalize().replace("_", " ")
                    + " ("
                    + str(np.mean(n[1]["rv"].dataset))
                    + ")"
                )
            else:
                node_label = (
                    n[0].split("/")[-1].replace("_", " ").capitalize()
                    if simplified_labels
                    else n[0]
                )
            A.add_node(n[0], label=node_label)

        if list(self.edges(data=True))[0][2].get("βs") is not None:
            max_median_betas = max(
                [abs(np.median(e[2]["βs"])) for e in self.edges(data=True)]
            )

        for e in self.edges(data=True):

            sts = e[2]["InfluenceStatements"]
            total_evidence_pieces = sum([len(s.evidence) for s in sts])
            reinforcement = (
                sum(
                    [
                        stmt.overall_polarity() * len(stmt.evidence)
                        for stmt in sts
                    ]
                )
                / total_evidence_pieces
            )
            opacity = total_evidence_pieces / n_max
            h = (opacity * 255).hex()

            if list(self.edges(data=True))[0][2].get("βs") is not None:
                penwidth = 3 * abs(np.median(e[2]["βs"]) / max_median_betas)
                cmap = cm.Greens if reinforcement > 0 else cm.Reds
                c_str = (
                    matplotlib.colors.rgb2hex(cmap(abs(reinforcement)))
                    + h[4:6]
                )
            else:
                penwidth = 1
                c_str = "black"

            A.add_edge(e[0], e[1], color=c_str, penwidth=penwidth)

        if indicators:
            for n in nodes_with_indicators:
                for indicator_name, ind in n[1]["indicators"].items():
                    node_label = _insert_line_breaks(
                        ind.name.replace("_", " "), 30
                    )
                    if indicator_values:
                        if ind.unit is not None:
                            units = f" {ind.unit}"
                        else:
                            units = ""

                        if ind.mean is not None:
                            ind_value = "{:.2f}".format(ind.mean)
                            node_label = (
                                f"{node_label}\n{ind_value} {ind.unit}"
                                f"\nSource: {ind.source}"
                                f"\nAggregation axes: {ind.aggaxes}"
                                f"\nAggregation method: {ind.aggregation_method}"
                            )

                    A.add_node(
                        indicator_name,
                        style="rounded, filled",
                        fillcolor="lightblue",
                        label=node_label,
                    )
                    A.add_edge(n[0], indicator_name, color="royalblue4")

        if nodes_to_highlight is not None:
            nodes = kwargs.pop("nodes_to_highlight")
            if isinstance(nodes, list):
                for n in nodes:
                    if n in A.nodes():
                        A.add_node(n, fontcolor="royalblue")
            elif isinstance(nodes, str):
                if n in A.nodes():
                    A.add_node(nodes, fontcolor="royalblue")

        if kwargs.get("graph_label") is not None:
            A.graph_attr["label"] = kwargs["graph_label"]

        return A

Exports the CAG as a pygraphviz AGraph for visualization.

Args:
indicators: Whether to display indicators in the AGraph
indicator_values: Whether to display indicator values in the AGraph
nodes_to_highlight: Nodes to highlight in the AGraph.
Returns:
A PyGraphviz AGraph representation of the AnalysisGraph.


def declare(self, name):

        if name in self._data:
            raise KeyError("Declared name {} that already existed".format(name))

        self._data[name] = self._loop.create_future()

Declare that a key will be set in the future.

This will create a future for the key that is used to
hold its result and allow awaiting it.

Args:
name (str): The unique key that will be used.


def _update_trial_info(self, expr_dir):

        trial_id = expr_dir[-8:]

        meta_file = os.path.join(expr_dir, EXPR_META_FILE)
        meta = parse_json(meta_file)

        result_file = os.path.join(expr_dir, EXPR_RESULT_FILE)
        offset = self._result_offsets.get(trial_id, 0)
        results, new_offset = parse_multiple_json(result_file, offset)
        self._add_results(results, trial_id)
        self._result_offsets[trial_id] = new_offset

        if meta:
            TrialRecord.objects \
                .filter(trial_id=trial_id) \
                .update(trial_status=meta["status"],
                        end_time=timestamp2date(meta.get("end_time", None)))
        elif len(results) > 0:
            metrics = {
                "episode_reward": results[-1].get("episode_reward_mean", None),
                "accuracy": results[-1].get("mean_accuracy", None),
                "loss": results[-1].get("loss", None)
            }
            if results[-1].get("done"):
                TrialRecord.objects \
                    .filter(trial_id=trial_id) \
                    .update(trial_status="TERMINATED",
                            end_time=results[-1].get("date", None),
                            metrics=str(metrics))
            else:
                TrialRecord.objects \
                    .filter(trial_id=trial_id) \
                    .update(metrics=str(metrics))

Update information for given trial.

Meta file will be loaded if exists, and the trial information
in db backend will be updated.

Args:
expr_dir(str)


def update_configuration(self, id_or_uri, timeout=(- 1)):
    uri = (self._client.build_uri(id_or_uri) + '/configuration')
    return self._client.update_with_zero_body(uri, timeout=timeout)

Reapplies the appliance's configuration on the interconnect. This includes running the same configure steps
that were performed as part of the interconnect add by the enclosure.

Args:
id_or_uri: Can be either the resource ID or the resource URI.
timeout: Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
in OneView; it just stops waiting for its completion.

Returns:
Interconnect


def _maybe_download_corpus(tmp_dir, vocab_type):

  if vocab_type == text_problems.VocabType.CHARACTER:

    dataset_url = ("https:
                   "/wikitext-103-raw-v1.zip")
    dir_name = "wikitext-103-raw"
  else:
    dataset_url = ("https:
                   "/wikitext-103-v1.zip")
    dir_name = "wikitext-103"

  fname = os.path.basename(dataset_url)
  compressed_filepath = generator_utils.maybe_download(tmp_dir, fname,
                                                       dataset_url)
  zip_ref = zipfile.ZipFile(compressed_filepath, "r")
  zip_ref.extractall(tmp_dir)
  zip_ref.close()

  files = os.path.join(tmp_dir, dir_name, "*")
  train_file, valid_file, test_file = None, None, None
  for f in tf.gfile.Glob(files):
    fname = os.path.basename(f)
    if "train" in fname:
      train_file = f
    elif "valid" in fname:
      valid_file = f
    elif "test" in fname:
      test_file = f

  assert train_file, "Training file not found"
  assert valid_file, "Validation file not found"
  assert test_file, "Testing file not found"

  return train_file, valid_file, test_file

Download and unpack the corpus.

Args:
tmp_dir: directory containing dataset.
vocab_type: which vocabulary are we using.

Returns:
The list of names of files.


def nth(self, index):

        def _transform(xs):
            try:
                return [next(islice(iter(xs), index, None))]

            except (StopIteration, ValueError):
                return []

        return self.transform(_transform, 'nth')

Return a query that selects the element at `index` (starts from 0).
If no elements are available, returns a query with no results.

Example usage:

.. code:: python

>> q = Query(lambda: list(range(5)))
>> q.nth(2).results
[2]

Args:
index (int): The index of the element to select (starts from 0)

Returns:
Query


def equivalent_to(std_function):

    def decorate(cos_function):

        @wraps(cos_function)
        def decorated(path, *args, **kwargs):

            path = fsdecode(path).replace('\\', '/')

            if is_storage(path):
                with handle_os_exceptions():
                    return cos_function(path, *args, **kwargs)

            return std_function(path, *args, **kwargs)

        return decorated

    return decorate

Decorates a cloud object compatible function
to provides fall back to standard function if
used on local files.

Args:
std_function (function): standard function to
used with local files.

Returns:
function: new function


def get_full_url(self, url):
    request = Request('GET', url)
    preparedrequest = self.session.prepare_request(request)
    return preparedrequest.url

Get full url including any additional parameters

Args:
url (str): URL for which to get full url

Returns:
str: Full url including any additional parameters


def tas50(msg):
    d = hex2bin(data(msg))
    if (d[45] == '0'):
        return None
    tas = (bin2int(d[46:56]) * 2)
    return tas

Aircraft true airspeed, BDS 5,0 message

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

Returns:
int: true airspeed in knots


def get_equivalents(term_id: str) -> List[Mapping[str, Union[str, bool]]]:

    try:
        errors = []
        terms = get_terms(term_id)
        if len(terms) == 0:
            return {"equivalents": [], "errors": errors}
        elif len(terms) > 1:
            errors.append(
                f'Too many primary IDs returned. Given term_id: {term_id} matches these term_ids: {[term["id"] for term in terms]}'
            )
            return {"equivalents": [], "errors": errors}
        else:
            term_id = terms[0]["id"]

        term_id_key = bel.db.arangodb.arango_id_to_key(term_id)

        equivalents = []
        query = f

        cursor = belns_db.aql.execute(query, count=True, batch_size=20)
        for doc in cursor:
            if doc.get("term_id", False):
                equivalents.append(doc)

        equivalents.append(
            {"term_id": term_id, "namespace": term_id.split(":")[0], "primary": True}
        )

        return {"equivalents": equivalents, "errors": errors}

    except Exception as e:
        log.error(f"Problem getting term equivalents for {term_id} msg: {e}")
        return {"equivalents": [], "errors": [f"Unexpected error {e}"]}

Get equivalents given ns:id

Args:
term_id (str): term id
Returns:
List[Mapping[str, Union[str, bool]]]: e.g. [{'term_id': 'HGNC:5', 'namespace': 'HGNC'}, 'primary': False]


def sas_logical_interconnects(self):
    if (not self.__sas_logical_interconnects):
        self.__sas_logical_interconnects = SasLogicalInterconnects(self.__connection)
    return self.__sas_logical_interconnects

Gets the SasLogicalInterconnects API client.

Returns:
SasLogicalInterconnects:


def consume(self, message):

        if 'jsonrpc' not in message or message['jsonrpc'] != JSONRPC_VERSION:
            log.warn("Unknown message type %s", message)
            return

        if 'id' not in message:
            log.debug("Handling notification from client %s", message)
            self._handle_notification(message['method'], message.get('params'))
        elif 'method' not in message:
            log.debug("Handling response from client %s", message)
            self._handle_response(message['id'], message.get('result'), message.get('error'))
        else:
            try:
                log.debug("Handling request from client %s", message)
                self._handle_request(message['id'], message['method'], message.get('params'))
            except JsonRpcException as e:
                log.exception("Failed to handle request %s", message['id'])
                self._consumer({
                    'jsonrpc': JSONRPC_VERSION,
                    'id': message['id'],
                    'error': e.to_dict()
                })
            except Exception:
                log.exception("Failed to handle request %s", message['id'])
                self._consumer({
                    'jsonrpc': JSONRPC_VERSION,
                    'id': message['id'],
                    'error': JsonRpcInternalError.of(sys.exc_info()).to_dict()
                })

Consume a JSON RPC message from the client.

Args:
message (dict): The JSON RPC message sent by the client


def create(self, uri, local_path):
    matches = self.schema_pattern.search(uri)
    if (not matches):
        logger.error("Unknown uri schema: '%s'. Added schemas: %s", uri, list(self.handlers.keys()))
        return None
    schema = matches.group(1)
    url = matches.group(2)
    return self.handlers[schema](url, local_path)

Create a project handler

Args:
uri (str): schema://something formatted uri
local_path (str): the project configs directory

Return:
ProjectHandler derived class instance


def set_banner(self, banner_type, value=None, default=False, disable=False):
    command_string = ('banner %s' % banner_type)
    if ((default is True) or (disable is True)):
        cmd = self.command_builder(command_string, value=None, default=default, disable=disable)
        return self.configure(cmd)
    else:
        if (not value.endswith('\n')):
            value = (value + '\n')
        command_input = dict(cmd=command_string, input=value)
        return self.configure([command_input])

Configures system banners

Args:
banner_type(str): banner to be changed (likely login or motd)
value(str): value to set for the banner
default (bool): Controls the use of the default keyword
disable (bool): Controls the use of the no keyword`

Returns:
bool: True if the commands completed successfully otherwise False


def match(self, name):
    if (self.method == Ex.Method.PREFIX):
        return name.startswith(self.value)
    elif (self.method == Ex.Method.SUFFIX):
        return name.endswith(self.value)
    elif (self.method == Ex.Method.CONTAINS):
        return (self.value in name)
    elif (self.method == Ex.Method.EXACT):
        return (self.value == name)
    elif (self.method == Ex.Method.REGEX):
        return re.search(self.value, name)
    return False

Check if given name matches.

Args:
name (str): name to check.

Returns:
bool: matches name.


def generic_type_args(type_: Type) -> List[Type]:

    if hasattr(type_, '__union_params__'):

        return list(type_.__union_params__)
    return list(type_.__args__)

Gets the type argument list for the given generic type.

If you give this function List[int], it will return [int], and
if you give it Union[int, str] it will give you [int, str]. Note
that on Python < 3.7, Union[int, bool] collapses to Union[int] and
then to int; this is already done by the time this function is
called, so it does not help with that.

Args:
type_: The type to get the arguments list of.

Returns:
A list of Type objects.


def recipe_bigquery_to_sheet(config, auth_read, sheet, tab, range, dataset, query, legacy):
    bigquery(config, {'auth': auth_read, 'from': {'auth': 'service', 'dataset': dataset, 'query': query, 'legacy': legacy}, 'to': {'sheet': sheet, 'tab': tab, 'range': range}})

Copy the contents of a query into a Google Sheet.

Args:
auth_read (authentication) - Credentials used for reading data.
sheet (string) - Either sheet url or sheet name.
tab (string) - Name of the tab where to put the data.
range (string) - Range in the sheet to place the data, leave blank for whole sheet.
dataset (string) - Existing BigQuery dataset.
query (text) - Query to pull data from the table.
legacy (boolean) - Use Legacy SQL


def cancelled(self):
    if (self.type not in {EventType.GESTURE_SWIPE_END, EventType.GESTURE_PINCH_END}):
        raise AttributeError(_wrong_prop.format(self.type))
    return self._libinput.libinput_event_gesture_get_cancelled(self._handle)

Return if the gesture ended normally, or if it was cancelled.

For gesture events that are not of type
:attr:`~libinput.constant.EventType.GESTURE_SWIPE_END` or
:attr:`~libinput.constant.EventType.GESTURE_PINCH_END`, this property
raises :exc:`AttributeError`.

Returns:
bool: :obj:`True` indicating that the gesture was cancelled.
Raises:
AttributeError


def determine_size(self, capture):
    width = 640
    height = 480
    if (capture and hasattr(capture, 'get')):
        width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
    else:
        self.frame_offset += 1
        (ret, frame) = capture.read()
        if ret:
            width = frame.shape[1]
            height = frame.shape[0]
    return (int(height), int(width))

Determines the height and width of the image source.

If no dimensions are available, this method defaults to a resolution of
640x480, thus returns (480, 640).
If capture has a get method it is assumed to understand
`cv2.CAP_PROP_FRAME_WIDTH` and `cv2.CAP_PROP_FRAME_HEIGHT` to get the
information. Otherwise it reads one frame from the source to determine
image dimensions.

Args:
capture: the source to read from.

Returns:
A tuple containing integers of height and width (simple casts).


def __init__(self, hidden_size: int, num_heads: int, mask_feature_size: torch.Tensor):
    super().__init__()
    self.hidden_size = hidden_size
    self.num_heads = num_heads
    self.mask_embedder = Mask2FormerMLPPredictionHead(self.hidden_size, self.hidden_size, mask_feature_size)

This class is used to get the predicted mask for a given Mask2FormerMaskedAttentionDecoder layer. It also
generates the binarized attention mask associated with the given predicted mask. The attention mask obtained
using predicted mask of the (l-1)th decoder layer is fed to the cross(masked)-attention block of the next
decoder layer as input.

Args:
hidden_size (`int`):
The feature dimension of the Mask2FormerMaskedAttentionDecoder
num_heads (`int`):
The number of heads used in the Mask2FormerMaskedAttentionDecoder
mask_feature_size (`torch.Tensor`):
one of the output dimensions of the predicted masks for each query


def merge_svg_layers(svg_sources, share_transform=True):
    ((width, height), layers) = get_svg_layers(svg_sources)
    if share_transform:
        transforms = [layer_i.attrib['transform'] for layer_i in layers if ('transform' in layer_i.attrib)]
        if (len(transforms) > 1):
            raise ValueError('Transform can only be shared if *exactly one* layer has a transform ({} layers have `transform` attributes)'.format(len(transforms)))
        elif transforms:
            for layer_i in layers:
                layer_i.attrib['transform'] = transforms[0]
    dwg = svgwrite.Drawing(profile='tiny', debug=False, size=(width, height))
    output_svg_root = etree.fromstring(dwg.tostring())
    output_svg_root.extend(layers)
    output = StringIO.StringIO()
    output.write(etree.tostring(output_svg_root))
    output.seek(0)
    return output

Merge layers from input svg sources into a single XML document.

Args:

svg_sources (list) : A list of file-like objects, each containing
one or more XML layers.
share_transform (bool) : If exactly one layer has a transform, apply it
to *all* other layers as well.

Returns:

StringIO.StringIO : File-like object containing merge XML document.


def revert_to(self):
    response = self.resource.repo.api.http_request('PATCH', self.uri)
    if (response.status_code == 204):
        logger.debug(('reverting to previous version of resource, %s' % self.uri))
        self._current_resource.refresh()
    else:
        raise Exception(('HTTP %s, could not revert to resource version, %s' % (response.status_code, self.uri)))

method to revert resource to this version by issuing PATCH

Args:
None

Returns:
None: sends PATCH request, and refreshes parent resource


def __init__(self, *, node_url, headers=None):

        self.node_url = node_url
        self.session = Session()
        if headers:
            self.session.headers.update(headers)

        self._retries = 0
        self.backoff_time = None

Initializes a :class:`~bigchaindb_driver.connection.Connection`
instance.

Args:
node_url (str):  Url of the node to connect to.
headers (dict): Optional headers to send with each request.


def get_open_clinvar_submission(self, user_id, institute_id):
    LOG.info("Retrieving an open clinvar submission for user '%s' and institute %s", user_id, institute_id)
    query = dict(user_id=user_id, institute_id=institute_id, status='open')
    submission = self.clinvar_submission_collection.find_one(query)
    if (submission is None):
        submission_id = self.create_submission(user_id, institute_id)
        submission = self.clinvar_submission_collection.find_one({'_id': submission_id})
    return submission

Retrieve the database id of an open clinvar submission for a user and institute,
if none is available then create a new submission and return it

Args:
user_id(str): a user ID
institute_id(str): an institute ID

Returns:
submission(obj) : an open clinvar submission object


def find_customer(cls, session, mailbox, customer):
    return cls(('/mailboxes/%d/customers/%s/conversations.json' % (mailbox.id, customer.id)), session=session)

Return conversations for a specific customer in a mailbox.

Args:
session (requests.sessions.Session): Authenticated session.
mailbox (helpscout.models.Mailbox): Mailbox to search.
customer (helpscout.models.Customer): Customer to search for.

Returns:
RequestPaginator(output_type=helpscout.models.Conversation):
Conversations iterator.


def _promote_helper(self, source_path, new_parent_path):
    current_field = self.field_value(source_path)
    new_parent_rank = self.field_value(new_parent_path).rank
    parent_rank = self.field_value(source_path[:-1]).rank
    if new_parent_rank == parent_rank:
        return current_field
    current_field_rank = current_field.shape.rank
    if current_field_rank is None:
        raise ValueError('Cannot determine if dimensions should be merged.')
    inner_dim = min(parent_rank, current_field_rank - 1)
    if inner_dim <= new_parent_rank:
        return current_field
    return _merge_dims_generic(current_field, new_parent_rank, inner_dim)

Creates a promoted field without adding it to the structure.

Args:
source_path: the source path in the structured tensor.
new_parent_path: the new parent path. Must be a prefix of source_path.

Returns:
a composite tensor of source_path promoted.
Raises:
ValueError: if the shape of the field is unknown and the right strategy
cannot be determined.


def business_days_between(self, from_dates, to_dates):
    cumul_bus_days_table = self._compute_cumul_bus_days_table()
    ordinals_1, ordinals_2 = (from_dates.ordinal(), to_dates.ordinal())
    with tf.control_dependencies(self._assert_ordinals_in_bounds(ordinals_1) + self._assert_ordinals_in_bounds(ordinals_2)):
        ordinals_2 = tf.broadcast_to(ordinals_2, ordinals_1.shape)
        cumul_bus_days_1 = self._gather(cumul_bus_days_table, ordinals_1 - self._ordinal_offset + 1)
        cumul_bus_days_2 = self._gather(cumul_bus_days_table, ordinals_2 - self._ordinal_offset + 1)
        return tf.math.maximum(cumul_bus_days_2 - cumul_bus_days_1, 0)

Calculates number of business between pairs of dates.

For each pair, the initial date is included in the difference, and the final
date is excluded. If the final date is the same or earlier than the initial
date, zero is returned.

Args:
from_dates: DateTensor of initial dates.
to_dates: DateTensor of final dates, should be broadcastable to
`from_dates`.

Returns:
An int32 Tensor with the number of business days between the
corresponding pairs of dates.


def _read_feather_columns(path, columns, num_splits):
    from pyarrow import feather
    df = feather.read_feather(path, columns=columns)
    return (_split_result_for_readers(0, num_splits, df) + [len(df.index)])

Use a Ray task to read columns from Feather into a Pandas DataFrame.

Note: Ray functions are not detected by codecov (thus pragma: no cover)

Args:
path: The path of the Feather file.
columns: The list of column names to read.
num_splits: The number of partitions to split the column into.

Returns:
A list containing the split Pandas DataFrames and the Index as the last
element. If there is not `index_col` set, then we just return the length.
This is used to determine the total length of the DataFrame to build a
default Index.


def _get_decoratables(self, atype):

        result = []
        defmsg = "Skipping {}; not decoratable or already decorated."
        for varname in self.shell.run_line_magic("who_ls", atype):
            varobj = self.shell.user_ns.get(varname, None)
            decorate = False

            if varobj is None:
                continue

            if atype in ["classobj", "type"]:

                if (not hasattr(varobj, "__acorn__") and
                    hasattr(varobj, "__module__") and
                    varobj.__module__ == "__main__" and
                    not hasattr(varobj, "__file__")):
                    decorate = True
                else:
                    msg.std(defmsg.format(varname), 3)

            elif atype in ["function", "staticmethod"]:

                func = None
                if atype == "staticmethod" and hasattr(varobj, "__func__"):
                    func = varobj.__func__
                elif atype == "function":
                    func = varobj

                if (func is not None and
                    not hasattr(func, "__acorn__") and
                    hasattr(func, "__code__") and
                    "<ipython-input" in func.__code__.co_filename):
                    decorate = True
                else:
                    msg.std(defmsg.format(varname), 3)

            if decorate:
                self.entities[atype][varname] = varobj
                result.append((varname, varobj))

        return result

Returns a list of the objects that need to be decorated in the
current user namespace based on their type.

Args:
atype (str): one of the values in :attr:`atypes`. Specifies the type of
object to search.


def forward(self, tokens, audio_conditioning=None, metadata_conditioning=None, last_encoder_hidden_states=None, get_preds=False, get_acts=False, get_sep_loss=False):
    batch_size = tokens.shape[0]
    with torch.no_grad():
        tokens = tokens.view(batch_size, -1).long()
    if not self.audio_conditioning:
        audio_conditioning = torch.zeros((batch_size, 1, self.width), device=tokens.device, dtype=self.transformer._attn_mods[0].mlp.c_fc.weight.dtype)
    target = tokens
    hidden_states = self.embed_tokens(tokens)
    hidden_states = torch.cat((hidden_states[:, -1:], hidden_states[:, :-1]), dim=1)
    if self.metadata_conditioning:
        hidden_states[:, 0] = metadata_conditioning.view(batch_size, self.width)
    else:
        hidden_states[:, 0] = self.start_token
    hidden_states = self.embed_tokens_dropout(hidden_states) + self.pos_emb_dropout(self.pos_emb()) + audio_conditioning
    hidden_states = self.transformer(hidden_states, last_encoder_hidden_states=last_encoder_hidden_states)
    if self.add_cond_after_transformer:
        hidden_states = hidden_states + audio_conditioning
    activations = hidden_states
    if self.is_encoder:
        return hidden_states
    hidden_states = self.fc_proj_out(hidden_states)
    loss_fn = nn.CrossEntropyLoss()
    if get_sep_loss:
        lyric_hidden_states = hidden_states[:, :self.encoder_len].reshape(-1, self.embed_dim)
        token_hidden_states = hidden_states[:, self.encoder_len:].reshape(-1, self.embed_dim)
        lyric_loss = loss_fn(lyric_hidden_states, target[:, :self.encoder_len].reshape(-1)) / np.log(2.0)
        music_token_loss = loss_fn(token_hidden_states, target[:, self.encoder_len:].reshape(-1)) / np.log(2.0)
        loss = (lyric_loss, music_token_loss)
    else:
        loss = loss_fn(hidden_states.view(-1, self.embed_dim), target.view(-1)) / np.log(2.0)
    if get_preds:
        return (loss, hidden_states)
    elif get_acts:
        return (loss, activations)
    else:
        return (loss, None)

Args:
tokens (`torch.tensor`):
Can represent music tokens, lyrics tokens or both, depending on the configuration.


def get_config(self, slot, config_id):
    if (slot not in self.config_database):
        raise ArgumentError('No config variables have been set on specified slot', slot=slot)
    if (config_id not in self.config_database[slot]):
        raise ArgumentError('Config variable has not been set on specified slot', slot=slot, config_id=config_id)
    return self.config_database[slot][config_id]

Get a config variable assignment previously set on this sensor graph.

Args:
slot (SlotIdentifier): The slot that we are setting this config variable
on.
config_id (int): The 16-bit config variable identifier.

Returns:
(str, str|int): Returns a tuple with the type of the config variable and
the value that is being set.

Raises:
ArgumentError: If the config variable is not currently set on the specified
slot.


def padded_urlsafe_b64decode(value):
    b64string = to_bytes(value)
    padded = (b64string + (b'=' * ((- len(b64string)) % 4)))
    return base64.urlsafe_b64decode(padded)

Decodes base64 strings lacking padding characters.

Google infrastructure tends to omit the base64 padding characters.

Args:
value (Union[str, bytes]): The encoded value.

Returns:
bytes: The decoded value


def acc_difference(points):
    data = [0]
    for (before, after) in pairwise(points):
        data.append((before.acc - after.acc))
    return data

Computes the accelaration difference between each adjacent point

Args:
points (:obj:`Point`)
Returns:
:obj:`list` of int: Indexes of changepoints


def recall_at_k(y_true: List[int], y_pred: List[List[np.ndarray]], k: int):

    num_examples = float(len(y_pred))
    predictions = np.array(y_pred)
    predictions = np.flip(np.argsort(predictions, -1), -1)[:, :k]
    num_correct = 0
    for el in predictions:
        if 0 in el:
            num_correct += 1
    return float(num_correct) / num_examples

Calculates recall at k ranking metric.

Args:
y_true: Labels. Not used in the calculation of the metric.
y_predicted: Predictions.
Each prediction contains ranking score of all ranking candidates for the particular data sample.
It is supposed that the ranking score for the true candidate goes first in the prediction.

Returns:
Recall at k


def get_parameter_definitions(self):
    output = {}
    for (var_name, attrs) in self.defined_variables().items():
        var_type = attrs.get('type')
        if isinstance(var_type, CFNType):
            cfn_attrs = copy.deepcopy(attrs)
            cfn_attrs['type'] = var_type.parameter_type
            output[var_name] = cfn_attrs
    return output

Get the parameter definitions to submit to CloudFormation.

Any variable definition whose `type` is an instance of `CFNType` will
be returned as a CloudFormation Parameter.

Returns:
dict: parameter definitions. Keys are parameter names, the values
are dicts containing key/values for various parameter
properties.


def all(self, data={}, **kwargs):
    if ('payment_id' in data):
        url = '/payments/{}/transfers'.format(data['payment_id'])
        del data['payment_id']
        return self.get_url(url, data, **kwargs)
    return super(Transfer, self).all(data, **kwargs)

Fetch all Transfer entities

Returns:
Dictionary of Transfer data


def chartspan(cls, start, end):
    return cls(Lnk.CHARTSPAN, (int(start), int(end)))

Create a Lnk object for a chart span.

Args:
start: the initial chart vertex
end: the final chart vertex


def generate_paillier_keypair(private_keyring=None, n_length=DEFAULT_KEYSIZE):

    p = q = n = None
    n_len = 0
    while n_len != n_length:
        p = getprimeover(n_length
        q = p
        while q == p:
            q = getprimeover(n_length
        n = p * q
        n_len = n.bit_length()

    public_key = PaillierPublicKey(n)
    private_key = PaillierPrivateKey(public_key, p, q)

    if private_keyring is not None:
        private_keyring.add(private_key)

    return public_key, private_key

Return a new :class:`PaillierPublicKey` and :class:`PaillierPrivateKey`.

Add the private key to *private_keyring* if given.

Args:
private_keyring (PaillierPrivateKeyring): a
:class:`PaillierPrivateKeyring` on which to store the private
key.
n_length: key size in bits.

Returns:
tuple: The generated :class:`PaillierPublicKey` and
:class:`PaillierPrivateKey`


def _pvalue_to_value(self, side_input, values):
    return sideinputs.SideInputMap(type(side_input), side_input._view_options(), values)

Given a side input, returns the associated value in its requested form.

Args:
side_input: _UnpickledSideInput object.
values: Iterable values associated with the side input.

Returns:
The side input in its requested form.

Raises:
ValueError: If values cannot be converted into the requested form.


def __init__(self, tensors, every_n_iter=None, every_n_secs=None, at_end=False, formatter=None):
    only_log_at_end = at_end and every_n_iter is None and (every_n_secs is None)
    if not only_log_at_end and (every_n_iter is None) == (every_n_secs is None):
        raise ValueError('either at_end and/or exactly one of every_n_iter and every_n_secs must be provided.')
    if every_n_iter is not None and every_n_iter <= 0:
        raise ValueError('invalid every_n_iter=%s.' % every_n_iter)
    if not isinstance(tensors, dict):
        self._tag_order = tensors
        tensors = {item: item for item in tensors}
    else:
        self._tag_order = sorted(tensors.keys())
    self._tensors = tensors
    self._formatter = formatter
    self._timer = NeverTriggerTimer() if only_log_at_end else SecondOrStepTimer(every_secs=every_n_secs, every_steps=every_n_iter)
    self._log_at_end = at_end

Initializes a `LoggingTensorHook`.

Args:
tensors: `dict` that maps string-valued tags to tensors/tensor names, or
`iterable` of tensors/tensor names.
every_n_iter: `int`, print the values of `tensors` once every N local
steps taken on the current worker.
every_n_secs: `int` or `float`, print the values of `tensors` once every N
seconds. Exactly one of `every_n_iter` and `every_n_secs` should be
provided.
at_end: `bool` specifying whether to print the values of `tensors` at the
end of the run.
formatter: function, takes dict of `tag`->`Tensor` and returns a string.
If `None` uses default printing all tensors.

Raises:
ValueError: if `every_n_iter` is non-positive.


def get_learning_rate(self, iter):

        return self.init_lr * ((math.cos(iter * 1.0 / (self.max_iter) * math.pi) + 1.0) * 0.5)

Get learning rate with cosine decay based on current iteration.

Args:
iter (int): Current iteration (starting with 0).

Returns:
float: Learning rate


def split(input_file, file_1, file_2, no_in_first_file):
    with open(input_file) as f:
        feat_collection = geojson.load(f)
    features = feat_collection['features']
    feat_collection_1 = geojson.FeatureCollection(features[0:no_in_first_file])
    feat_collection_2 = geojson.FeatureCollection(features[no_in_first_file:])
    with open(file_1, 'w') as f:
        geojson.dump(feat_collection_1, f)
    with open(file_2, 'w') as f:
        geojson.dump(feat_collection_2, f)

Split a geojson in two separate files.

Args:
input_file (str): Input filename.
file_1 (str): Output file name 1.
file_2 (str): Output file name 2.
no_features (int): Number of features in input_file to go to file_1.
output_file (str): Output file name.


def read_ppdesc(self, filename):
    if filename.endswith('.xml'):
        raise self.Error('XML pseudo not supported yet')
    else:
        lines = _read_nlines(filename, 80)
        for (lineno, line) in enumerate(lines):
            if (lineno == 2):
                try:
                    tokens = line.split()
                    (pspcod, pspxc) = map(int, tokens[:2])
                except:
                    msg = ('%s: Cannot parse pspcod, pspxc in line\n %s' % (filename, line))
                    logger.critical(msg)
                    return None
                if (pspcod not in self._PSPCODES):
                    raise self.Error(("%s: Don't know how to handle pspcod %s\n" % (filename, pspcod)))
                ppdesc = self._PSPCODES[pspcod]
                if (pspcod == 7):
                    tokens = lines[(lineno + 1)].split()
                    (pspfmt, creatorID) = tokens[:2]
                    ppdesc = ppdesc._replace(format=pspfmt)
                return ppdesc
        return None

Read the pseudopotential descriptor from file filename.

Returns:
Pseudopotential descriptor. None if filename is not a valid pseudopotential file.

Raises:
`PseudoParserError` if fileformat is not supported.


def build_classes(self, strict=False, named_only=False, standardize_names=True):
    kw = {'strict': strict}
    builder = classbuilder.ClassBuilder(self.resolver)
    for (nm, defn) in iteritems(self.schema.get('definitions', {})):
        uri = python_jsonschema_objects.util.resolve_ref_uri(self.resolver.resolution_scope, ('
        builder.construct(uri, defn, **kw)
    if standardize_names:
        name_transform = (lambda t: inflection.camelize(inflection.parameterize(six.text_type(t), '_')))
    else:
        name_transform = (lambda t: t)
    nm = (self.schema['title'] if ('title' in self.schema) else self.schema['id'])
    nm = inflection.parameterize(six.text_type(nm), '_')
    builder.construct(nm, self.schema, **kw)
    self._resolved = builder.resolved
    classes = {}
    for (uri, klass) in six.iteritems(builder.resolved):
        title = getattr(klass, '__title__', None)
        if (title is not None):
            classes[name_transform(title)] = klass
        elif (not named_only):
            classes[name_transform(uri.split('/')[(- 1)])] = klass
    return python_jsonschema_objects.util.Namespace.from_mapping(classes)

Build all of the classes named in the JSONSchema.

Class names will be transformed using inflection by default, so names
with spaces in the schema will be camelcased, while names without
spaces will have internal capitalization dropped. Thus "Home Address"
becomes "HomeAddress", while "HomeAddress" becomes "Homeaddress" To
disable this behavior, pass standardize_names=False, but be aware
that accessing names with spaces from the namespace can be
problematic.

Args:
strict: (bool) use this to validate required fields while creating the class
named_only: (bool) If true, only properties with an actual title attribute will
be included in the resulting namespace (although all will be generated).
standardize_names: (bool) If true (the default), class names will be tranformed
by camel casing

Returns:
A namespace containing all the generated classes


def rouge_2_fscore(logits, labels):

  predictions = tf.to_int32(tf.argmax(logits, axis=-1))

  rouge_2_f_score = tf.py_func(rouge_n, (predictions, labels), tf.float32)
  return rouge_2_f_score, tf.constant(1.0)

ROUGE-2 F1 score computation between labels and predictions.

This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.

Args:
logits: tensor, model predictions
labels: tensor, gold output.

Returns:
rouge2_fscore: approx rouge-2 f1 score.


def _data_to_json(data):

        if type(data) not in [str, unicode]:
            data = json.dumps(data)
        return data

Convert to json if it isn't already a string.

Args:
data (str): data to convert to json


def get_unique_named_object_in_all_models(root, name):

    if hasattr(root, '_tx_model_repository'):
        src = list(
            root._tx_model_repository.local_models.filename_to_model.values())
        if root not in src:
            src.append(root)
    else:
        src = [root]

    a = []
    for m in src:
        print("analyzing {}".format(m._tx_filename))
        a = a + get_children(
            lambda x: hasattr(x, 'name') and x.name == name, m)

    assert len(a) == 1
    return a[0]

retrieves a unique named object (no fully qualified name)

Args:
root: start of search (if root is a model all known models are searched
as well)
name: name of object

Returns:
the object (if not unique, raises an error)


def _best_subset(self, n_qubits):

        if n_qubits == 1:
            return np.array([0])

        device_qubits = self.coupling_map.size()

        cmap = np.asarray(self.coupling_map.get_edges())
        data = np.ones_like(cmap[:, 0])
        sp_cmap = sp.coo_matrix((data, (cmap[:, 0], cmap[:, 1])),
                                shape=(device_qubits, device_qubits)).tocsr()
        best = 0
        best_map = None

        for k in range(sp_cmap.shape[0]):
            bfs = cs.breadth_first_order(sp_cmap, i_start=k, directed=False,
                                         return_predecessors=False)

            connection_count = 0
            sub_graph = []
            for i in range(n_qubits):
                node_idx = bfs[i]
                for j in range(sp_cmap.indptr[node_idx],
                               sp_cmap.indptr[node_idx + 1]):
                    node = sp_cmap.indices[j]
                    for counter in range(n_qubits):
                        if node == bfs[counter]:
                            connection_count += 1
                            sub_graph.append([node_idx, node])
                            break

            if connection_count > best:
                best = connection_count
                best_map = bfs[0:n_qubits]

                mapping = {}
                for edge in range(best_map.shape[0]):
                    mapping[best_map[edge]] = edge
                new_cmap = [[mapping[c[0]], mapping[c[1]]] for c in sub_graph]
                rows = [edge[0] for edge in new_cmap]
                cols = [edge[1] for edge in new_cmap]
                data = [1]*len(rows)
                sp_sub_graph = sp.coo_matrix((data, (rows, cols)),
                                             shape=(n_qubits, n_qubits)).tocsr()
                perm = cs.reverse_cuthill_mckee(sp_sub_graph)
                best_map = best_map[perm]
        return best_map

Computes the qubit mapping with the best connectivity.

Args:
n_qubits (int): Number of subset qubits to consider.

Returns:
ndarray: Array of qubits to use for best connectivity mapping.


def _GetFlagValues(self, flags):

    event_types = []
    for event_flag, description in self._FLAG_VALUES.items():
      if event_flag & flags:
        event_types.append(description)
    return ', '.join(event_types)

Determines which events are indicated by a set of fsevents flags.

Args:
flags (int): fsevents record flags.

Returns:
str: a comma separated string containing descriptions of the flag values
stored in an fsevents record.


def get_meas_los(self, user_lo_config):
    try:
        _m_los = self.default_meas_los.copy()
    except KeyError:
        raise PulseError('Default measurement frequencies not exist.')
    for (channel, lo_freq) in user_lo_config.meas_lo_dict().items():
        _m_los[channel.index] = lo_freq
    if (_m_los == self.default_meas_los):
        return None
    return _m_los

Embed default meas LO frequencies from backend and format them to list object.
If configured lo frequency is the same as default, this method returns `None`.

Args:
user_lo_config (LoConfig): A dictionary of LOs to format.

Returns:
list: A list of meas LOs.

Raises:
PulseError: when LO frequencies are missing.


def __init__(self, duplicate_file_check=False):

    super(PathSpecExtractor, self).__init__()
    self._duplicate_file_check = duplicate_file_check
    self._hashlist = {}

Initializes a path specification extractor.

The source collector discovers all the file entries in the source.
The source can be a single file, directory or a volume within
a storage media image or device.

Args:
duplicate_file_check (Optional[bool]): True if duplicate files should
be ignored.


def filter_records(df: pd.DataFrame, query: str) -> pd.DataFrame:
    expr = parse_one(query)
    orderby_clause = expr.find(exp.Order)
    limit_clause = expr.find(exp.Limit)
    offset_clause = expr.find(exp.Offset)
    if orderby_clause:
        df = apply_orderby(orderby_clause, df)
    start_loc = 0
    if offset_clause:
        start_loc = int(offset_clause.expression.args['this'])
        df = df.iloc[start_loc:]
    if limit_clause:
        limit = int(limit_clause.expression.args['this'])
        df = df.iloc[:start_loc + limit]
    return df

Filter records in an xarray Dataset based on a given query.

Args:
ds (xr.Dataset): The xarray Dataset to filter.
query (str): The query string for filtering the dataset.

Returns:
pd.DataFrame: A pandas DataFrame containing the filtered records.


def parse_requires(__fname: str) -> List[str]:

    deps = []
    with open(__fname) as req_file:
        entries = [s.split('
        for dep in entries:
            if not dep:
                continue
            elif dep.startswith('-r '):
                include = dep.split()[1]
                if '/' not in include:
                    include = path.join(path.dirname(__fname), include)
                deps.extend(parse_requires(include))
                continue
            elif ';' in dep:
                dep, marker = [s.strip() for s in dep.split(';')]

                match = re.fullmatch(r, marker, re.VERBOSE)
                if not match:
                    raise ValueError('Invalid marker {!r}'.format(marker))
                env = {
                    '__builtins__': {},
                    'python_version': '{}.{}'.format(*version_info[:2]),
                }
                if not eval(marker, env):
                    continue
            deps.append(dep)
    return deps

Parse ``pip``-style requirements files.

This is a *very* naïve parser, but very few packages make use of the more
advanced features.  Support for other features will be added only when
packages in the wild depend on them.

Args:
__fname: Base file to pass
Returns:
Parsed dependencies


def fit(fqdn, result, *argl, **argd):
    global _machines
    out = None
    if (len(argl) > 0):
        machine = argl[0]
        key = id(machine)
        _machines[key] = (machine, argl[0], argl[1])
        if isclassifier(machine):
            out = classify_fit(fqdn, result, *argl, **argd)
        elif isregressor(machine):
            out = regress_fit(fqdn, result, *argl, **argd)
    return out

Analyzes the result of a generic fit operation performed by `sklearn`.

Args:
fqdn (str): full-qualified name of the method that was called.
result: result of calling the method with `fqdn`.
argl (tuple): positional arguments passed to the method call.
argd (dict): keyword arguments passed to the method call.


def manual_shuffle(self, axis, shuffle_func, lengths):
    if axis:
        partitions = self.row_partitions
    else:
        partitions = self.column_partitions
    func = self.preprocess_func(shuffle_func)
    result = np.array([part.shuffle(func, lengths) for part in partitions])
    return (self.__constructor__(result) if axis else self.__constructor__(result.T))

Shuffle the partitions based on the `shuffle_func`.

Args:
axis: The axis to shuffle across.
shuffle_func: The function to apply before splitting the result.
lengths: The length of each partition to split the result into.

Returns:
A new BaseFrameManager object, the type of object that called this.


def chdir(directory):

    directory = os.path.abspath(directory)
    logger.info("chdir -> %s" % directory)
    try:
        if not os.path.isdir(directory):
            logger.error(
                "chdir -> %s failed! Directory does not exist!", directory
            )
            return False
        os.chdir(directory)
        return True
    except Exception as e:
        logger.error("chdir -> %s failed! %s" % (directory, e))
        return False

Change the current working directory.

Args:
directory (str): Directory to go to.


def db_dp020(self, value=None):

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

        self._db_dp020 = value

Corresponds to IDD Field `db_dp020`
mean coincident dry-bulb temperature to
Dew-point temperature corresponding to 2.0% annual cumulative frequency of occurrence

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

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


def makedirs(path):

    if path:
        if not os.path.exists(path):
            log.debug("makedirs({})".format(path))
            os.makedirs(path)
        else:
            realpath = os.path.realpath(path)
            if not os.path.isdir(realpath):
                raise ScriptWorkerException(
                    "makedirs: {} already exists and is not a directory!".format(path)
                )

Equivalent to mkdir -p.

Args:
path (str): the path to mkdir -p

Raises:
ScriptWorkerException: if path exists already and the realpath is not a dir.


def rts_smooth(kalman_filter, state_count=None):
    if (state_count is None):
        state_count = kalman_filter.state_count
    state_count = int(state_count)
    if ((state_count < 0) or (state_count > kalman_filter.state_count)):
        raise ValueError('Invalid state count: {}'.format(state_count))
    if (state_count == 0):
        return []
    states = ([None] * state_count)
    states[(- 1)] = kalman_filter.posterior_state_estimates[(- 1)]
    priors = kalman_filter.prior_state_estimates
    posteriors = kalman_filter.posterior_state_estimates
    for k in range((state_count - 2), (- 1), (- 1)):
        process_mat = kalman_filter.process_matrices[(k + 1)]
        cmat = posteriors[k].cov.dot(process_mat.T).dot(np.linalg.inv(priors[(k + 1)].cov))
        states[k] = MultivariateNormal(mean=(posteriors[k].mean + cmat.dot((states[(k + 1)].mean - priors[(k + 1)].mean))), cov=(posteriors[k].cov + cmat.dot((states[(k + 1)].cov - priors[(k + 1)].cov)).dot(cmat.T)))
    return states

Compute the Rauch-Tung-Striebel smoothed state estimates and estimate
covariances for a Kalman filter.

Args:
kalman_filter (KalmanFilter): Filter whose smoothed states should be
returned
state_count (int or None): Number of smoothed states to return.
If None, use ``kalman_filter.state_count``.

Returns:
(list of MultivariateNormal): List of multivariate normal distributions.
The mean of the distribution is the estimated state and the covariance
is the covariance of the estimate.


def get_bytesize(self, key, default=None, minimum=None, maximum=None, default_unit=None, base=DEFAULT_BASE):
    value = to_bytesize(self.get_str(key), default_unit, base)
    if (value is None):
        return to_bytesize(default, default_unit, base)
    return capped(value, to_bytesize(minimum, default_unit, base), to_bytesize(maximum, default_unit, base))

Size in bytes expressed by value configured under 'key'

Args:
key (str | unicode): Key to lookup
default (int | str | unicode | None): Default to use if key is not configured
minimum (int | str | unicode | None): If specified, result can't be below this minimum
maximum (int | str | unicode | None): If specified, result can't be above this maximum
default_unit (str | unicode | None): Default unit for unqualified values (see UNITS)
base (int): Base to use (usually 1024)

Returns:
(int): Size in bytes


def put(self, obj):

        self._queue.put(obj, block=True, timeout=self._queue_put_timeout)
        if obj is _SHUTDOWNREQUEST:
            return

Put request into queue.

Args:
obj (cheroot.server.HTTPConnection): HTTP connection
waiting to be processed


def load_yaml(stream, **kwargs):

    content = stream.read()
    try:
        return yaml.load(content) or {}
    except Exception, e:
        if stream.name and stream.name != '<string>':
            for mark_name in 'context_mark', 'problem_mark':
                mark = getattr(e, mark_name, None)
                if mark is None:
                    continue
                if getattr(mark, 'name') == '<string>':
                    mark.name = stream.name
        raise e

Load yaml-formatted data from a stream.

Args:
stream (file-like object).

Returns:
dict.