def __init__(self, tensor, slice_spec, name, dtype=None, device=None):
    self._tensor = tensor
    self.slice_spec = slice_spec
    self.name = name
    if callable(self._tensor):
        if dtype is None or device is None:
            raise AssertionError('When passing a callable `tensor` to a SaveSpec, an explicit dtype and device must be provided.')
        self.dtype = dtype
        self.device = device
    else:
        self.dtype = tensor.dtype
        if device is not None:
            self.device = device
        else:
            self.device = tensor.device

Creates a `SaveSpec` object.

Args:
        tensor: the tensor to save or callable that produces a tensor to save.
        If the value is `None`, the `SaveSpec` is ignored.
        slice_spec: the slice to be saved. See `Variable.SaveSliceInfo`.
        name: the name to save the tensor under.
        dtype: The data type of the Tensor. Required if `tensor` is callable.
        Used for error checking in the restore op.
        device: The device generating and consuming this tensor. Required if
        `tensor` is callable. Used to group objects to save by device.


def split(tensor, split_dimension, num_devices, assign_tuple_sharding=False, use_sharding_op=False, input_shape=None):
    return Sharding.split(tensor, split_dimension, num_devices, input_shape).apply_to_tensor(tensor, assign_tuple_sharding=assign_tuple_sharding, use_sharding_op=use_sharding_op)

Returns a tensor that is split along the given dimension.

Args:
        tensor: A tf.Tensor to split.
        split_dimension: The dimension to split.
        num_devices: The number of devices to partition the dimension.
        assign_tuple_sharding: If the sharding type should be a tuple.
        use_sharding_op: If true, adds a sharding op to set the sharding.
        input_shape: The full shape of the input tensor.


def toInteger(self) -> 'Builder':
    return self._to_builder(_evaluation.ToIntegerFunction(self.node.context, self.node, []))

The FHIRPath toInteger() function.

    Casts its operand to an integer.
    Returns an empty collection if the operand can not be coerced to an integer.
    Raises a ValueError if the operand collection contains more than one
    element.

Returns:
        An integer representation of its operand.


def _set_graph_parents(self, graph_parents):
    graph_parents = [] if graph_parents is None else graph_parents
    for i, t in enumerate(graph_parents):
        if t is None or not (linear_operator_util.is_ref(t) or tensor_util.is_tf_type(t)):
            raise ValueError('Graph parent item %d is not a Tensor; %s.' % (i, t))
    self._graph_parents = graph_parents

Set self._graph_parents.  Called during derived class init.

    This method allows derived classes to set graph_parents, without triggering
    a deprecation warning (which is invoked if `graph_parents` is passed during
    `__init__`.

Args:
        graph_parents: Iterable over Tensors.


def _create_vocab_table_lookup_model_tf1(self, sess: session.Session) -> Tuple[core.Tensor, core.Tensor, core.Tensor]:
    asset_dir = self.create_tempdir('assets').full_path
    asset_file = os.path.join(asset_dir, 'vocab_file.txt')
    file_io.write_string_to_file(filename=asset_file, file_content='hello,model,quantization\n')
    vocab_file = asset.Asset(asset_file)
    raw_vocab = io_ops.read_file(vocab_file)
    vocabs = ragged_string_ops.string_split_v2(string_ops.string_strip(raw_vocab), sep=',')
    kv_init = lookup_ops.KeyValueTensorInitializer(keys=vocabs, values=np.array([0, 1, 2]), value_dtype=dtypes.int64)
    table = lookup_ops.StaticVocabularyTable(kv_init, num_oov_buckets=5)
    input_vocabs_placeholder = array_ops.placeholder(dtypes.string, shape=(None,), name='input_vocabs')
    lookup_vals = math_ops.cast(table.lookup(input_vocabs_placeholder), dtypes.float32)
    matmul_input = array_ops_stack.stack([lookup_vals, lookup_vals])
    weight_row = array_ops.ones(shape=array_ops.shape(input_vocabs_placeholder), dtype=dtypes.float32)
    weight = array_ops.transpose_v2(array_ops_stack.stack([weight_row, weight_row]))
    output_tensor = math_ops.matmul(matmul_input, weight)
    return (input_vocabs_placeholder, lookup_vals, output_tensor)

Creates a simple model that initializes and lookups a vocab table.

    This model creates an asset file at "vocab_file.txt" containing
    comma-separated vocabularies.  It also initializes a `StaticVocabularyTable`
    and performs a lookup with the input vocabs, which is a 1D tensor of
    strings.

Args:
        sess: Tensorflow Session to create the model in.

Returns:
        (input_vocabs_placeholder, lookup_vals, output_tensor), where
        * input_vocabs_placeholder is a placeholder tensor of 1D strings
        * lookup_vals is an output tensor that is a direct result of table lookup
        * output_tensor is a float 2x2 matrix


def binary_crossentropy(target, output, from_logits=False):
    target = tf.convert_to_tensor(target)
    output = tf.convert_to_tensor(output)
    if len(target.shape) != len(output.shape):
        raise ValueError(f'Arguments `target` and `output` must have the same rank (ndim). Received: target.shape={target.shape}, output.shape={output.shape}')
    for e1, e2 in zip(target.shape, output.shape):
        if e1 is not None and e2 is not None and (e1 != e2):
            raise ValueError(f'Arguments `target` and `output` must have the same shape. Received: target.shape={target.shape}, output.shape={output.shape}')
    output, from_logits = _get_logits(output, from_logits, 'Sigmoid', 'binary_crossentropy')
    if from_logits:
        return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
    output = tf.clip_by_value(output, backend.epsilon(), 1.0 - backend.epsilon())
    bce = target * tf.math.log(output)
    bce += (1 - target) * tf.math.log(1 - output)
    return -bce

Binary crossentropy between an output tensor and a target tensor.

Args:
        target: A tensor with the same shape as `output`.
        output: A tensor.
        from_logits: Whether `output` is expected to be a logits tensor.
            By default, we consider that `output`
            encodes a probability distribution.

Returns:
        A tensor.


def _build_shuffle_scatter(reduced_shards, dst_devices):
    num_devices = len(dst_devices)
    out_tensors = []
    for d in range(0, num_devices):
        with ops.device(dst_devices[d]):
            out_tensors.append(array_ops.concat(reduced_shards, 0))
    return out_tensors

Build the scatter phase of shuffle all-reduce.

Args:
        reduced_shards:  list of `tf.Tensor` fully reduced shards
        dst_devices: list of names of devices at which the fully-reduced value
        should be reconstituted.

Returns:
        list of `tf.Tensor` scattered tensors.


def _pad_for_batching(self, pixel_values: List[np.ndarray], image_sizes: List[List[int]], data_format: Optional[Union[str, ChannelDimension]]=None, input_data_format: Optional[Union[str, ChannelDimension]]=None):
    max_shape = (max([size[0] for size in image_sizes]), max([size[1] for size in image_sizes]))
    pixel_values = [pad(image, padding=((0, max_shape[0] - size[0]), (0, max_shape[1] - size[1])), data_format=data_format, input_data_format=input_data_format) for image, size in zip(pixel_values, image_sizes)]
    return pixel_values

Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches.

Args:
        pixel_values (`List[np.ndarray]`):
            An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`)
        image_sizes (`List[List[int]]`):
            A list of sizes for each image in `pixel_values` in (height, width) format.
        data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use same as the input image.
        input_data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use the inferred format of the input image.

Returns:
        List[`np.ndarray`]: The padded images.


def compiler_ir_generator(stage='hlo', device_name=None, platform_name=None):
    if device_name is not None:
        if platform_name is not None:
            raise ValueError('device_name and platform_name cannot be provided at the same time.')
        warnings.warn('device_name is being deprecated. Use platform_name.')
    device_name = maybe_get_device_name(device_name)
    res_bytes = context.context().get_compiler_ir(device_name=device_name, platform_name=platform_name, function_name=fn_name, flat_args=filtered_flat_specs, captured_inputs=concrete_fn.captured_inputs, stage=stage)
    if stage in ('stablehlo_serialized', 'hlo_serialized', 'optimized_hlo_serialized', 'optimized_hlo_proto_serialized'):
        return res_bytes
    else:
        return res_bytes.decode('utf-8')

Gets the compiler IR bytes.

Args:
        stage: The exported stage for the given function.
        device_name: The name of the device with the form as
        "/job:localhost/replica:0/task:0/device:CPU:0", "/device:TPU:0" etc.
        When this is used, actual device is needed for getting the compiler IR.
        platform_name: The name of the platform, e.g. "TPU". See the comment in
        `get_compiler_ir` in `context.py`.

Returns:
        The compiler IR bytes.


def mlir_sparsify(input_data_str):
    return wrap_converter.wrapped_experimental_mlir_sparsify(input_data_str)

Sparsify `input_data_str` to encode sparse tensor with proper format.

Args:
        input_data_str: Input data in serialized form (e.g. a TFLITE model).

Returns:
        Sparsified model in serialized form (e.g. a TFLITE model).


def make_initializer(self, dataset, name=None):
    with ops.name_scope(name, 'make_initializer') as name:
        dataset_output_types = nest.map_structure(lambda component_spec: component_spec._to_legacy_output_types(), dataset.element_spec)
        dataset_output_shapes = nest.map_structure(lambda component_spec: component_spec._to_legacy_output_shapes(), dataset.element_spec)
        dataset_output_classes = nest.map_structure(lambda component_spec: component_spec._to_legacy_output_classes(), dataset.element_spec)
        nest.assert_same_structure(self.output_types, dataset_output_types)
        nest.assert_same_structure(self.output_shapes, dataset_output_shapes)
        for iterator_class, dataset_class in zip(nest.flatten(self.output_classes), nest.flatten(dataset_output_classes)):
            if iterator_class is not dataset_class:
                raise TypeError(f'Expected output classes {self.output_classes!r} but got dataset with output classes {dataset_output_classes!r}.')
        for iterator_dtype, dataset_dtype in zip(nest.flatten(self.output_types), nest.flatten(dataset_output_types)):
            if iterator_dtype != dataset_dtype:
                raise TypeError(f'Expected output types {self.output_types!r} but got dataset with output types {dataset_output_types!r}.')
        for iterator_shape, dataset_shape in zip(nest.flatten(self.output_shapes), nest.flatten(dataset_output_shapes)):
            if not iterator_shape.is_compatible_with(dataset_shape):
                raise TypeError(f'Expected output shapes compatible with {self.output_shapes!r} but got dataset with output shapes {dataset_output_shapes!r}.')
    with ops.colocate_with(self._iterator_resource):
        return gen_dataset_ops.make_iterator(dataset._variant_tensor, self._iterator_resource, name=name)

Returns a `tf.Operation` that initializes this iterator on `dataset`.

Args:
        dataset: A `Dataset` whose `element_spec` if compatible with this
        iterator.
        name: (Optional.) A name for the created operation.

Returns:
        A `tf.Operation` that can be run to initialize this iterator on the given
        `dataset`.

Raises:
        TypeError: If `dataset` and this iterator do not have a compatible
        `element_spec`.


def test_noninlined_funcdef(self, mode):
    self._maybe_skip(mode)
    with ops.device(_get_device(mode)):
        random_seed.set_random_seed(0)
        x = _input([8, 8])
        y = _matmul_act(x)
        y = _example_noninlined_funcdef(y)
        optimizer = gradient_descent.GradientDescentOptimizer(learning_rate=0.01)
        g = optimizer.compute_gradients(y, [x])
        output = (g, y)
    output_val_ref, output_val, cost_graph = self._run(mode, output)
    node_map = _build_node_map(cost_graph.node)
    self._assert_output_f16(mode, node_map, 'MatMul')
    tol = 0.01 if mode == 'mkl' else 0.001
    atol = 0.01 if test.is_built_with_rocm() else tol
    self.assertAllClose(output_val_ref, output_val, atol=atol, rtol=tol)

Test graph with non-inlined function subgraph.

    This requires the grappler pass to handle an OpDef that only appears in the
    graph's function registry instead of the global op registry.

Args:
        mode: Either 'cuda' or 'mkl'.


def resize(self, images: 'torch.Tensor', size: SizeDict, interpolation: Optional['F.InterpolationMode']=None, size_divisor: Optional[int]=None) -> 'torch.Tensor':
    if interpolation is None:
        interpolation = self.resample
    shorter = size.shortest_edge
    longer = int(MAX_LONGER_EDGE / MAX_SHORTER_EDGE * shorter)
    heights = images.shape[-2]
    widths = images.shape[-1]
    if heights < widths:
        new_heights = shorter
        new_widths = widths * (shorter / heights)
    else:
        new_heights = heights * (shorter / widths)
        new_widths = shorter
    if max(new_heights, new_widths) > longer:
        scale = longer / max(new_heights, new_widths)
        new_heights = new_heights * scale
        new_widths = new_widths * scale
    new_heights = int(new_heights + 0.5)
    new_widths = int(new_widths + 0.5)
    if size_divisor is not None:
        new_heights = new_heights // size_divisor * size_divisor
        new_widths = new_widths // size_divisor * size_divisor
    return F.resize(images, [new_heights, new_widths], interpolation=interpolation)

Resize an image or batch of images to specified size.

Args:
        images (`torch.Tensor`): Image or batch of images to resize.
        size (`Dict[str, int]`): Size dictionary with shortest_edge key.
        interpolation (`F.InterpolationMode`, *optional*): Interpolation method to use.
        size_divisor (`int`, *optional*): Value to ensure height/width are divisible by.

Returns:
        `torch.Tensor`: Resized image or batch of images.


def report_file(config, auth, account, report_id=None, name=None, timeout=60, chunksize=DCM_CHUNK_SIZE):
    account_id, advertiser_id = parse_account(config, auth, account)
    file_json = report_fetch(config, auth, account, report_id, name, timeout)
    if file_json == False:
        return (None, None)
    elif file_json == True:
        return ('report_running.csv', None)
    else:
        filename = '%s_%s.csv' % (file_json['fileName'], file_json['dateRange']['endDate'].replace('-', ''))
        if chunksize:
            return (filename, media_download(API_DCM(config, auth).files().get_media(reportId=file_json['reportId'], fileId=file_json['id']).execute(False), chunksize, 'utf-8'))
        else:
            return (filename, StringIO(API_DCM(config, auth).files().get_media(reportId=file_json['reportId'], fileId=file_json['id']).execute().decode('utf-8')))

Retrieves most recent DCM file by name or ID, if in progress, waits for it to complete.

    Bulletproofing:
    https://developers.google.com/doubleclick-advertisers/v3.2/files/get

    Timeout is in minutes ( retries will happen at 1 minute interval, default
    total time is 60 minutes )
    If chunksize is set to 0 then the whole file is downloaded at once.

Args:
        * auth: (string) Either user or service.
        * account: (string) [account:advertiser@profile] token.
        * report_id: (int) ID of DCm report to fetch ( either or name ).
        * name: (string) Name of report to fetch ( either or report_id ).
        * timeout: (int) Minutes to wait for in progress report before giving up.
        * chunksize: (int) number of bytes to download at a time, for memory
        constrained systems.

Returns:
        * (filename, iterator) if file exists and is ready to download in chunks.
        * (filename, file) if file exists and chunking is off.
        * ('report_running.csv', None) if report is in progress.
        * (None, None) if file does not exist.


def populate_conversion_metadata(model_object, metadata):
    try:
        metadata_builder = flatbuffers.Builder(0)
        metadata_builder.Finish(metadata.Pack(metadata_builder))
        buffer_field = schema_fb.BufferT()
        buffer_field.data = metadata_builder.Output()
        if not model_object.metadata:
            model_object.metadata = []
        else:
            for meta in model_object.metadata:
                if meta.name.decode('utf-8') == CONVERSION_METADATA_FIELD_NAME:
                    model_object.buffers[meta.buffer] = buffer_field
                    return model_object
        if not model_object.buffers:
            model_object.buffers = []
        model_object.buffers.append(buffer_field)
        metadata_field = schema_fb.MetadataT()
        metadata_field.name = CONVERSION_METADATA_FIELD_NAME
        metadata_field.buffer = len(model_object.buffers) - 1
        model_object.metadata.append(metadata_field)
        return model_object
    except Exception:
        return model_object

Add or update conversion metadata to a tflite model.

Args:
        model_object: A tflite model in object form.
        metadata: The conversion metadata.

Returns:
        A tflite model object with embedded conversion metadata.


def map_structure(func, *structure, **check_types_dict):
    return nest_util.map_structure(nest_util.Modality.DATA, func, *structure, **check_types_dict)

Applies `func` to each entry in `structure` and returns a new structure.

    Applies `func(x[0], x[1], ...)` where x[i] is an entry in
    `structure[i]`.  All structures in `structure` must have the same arity,
    and the return value will contain the results in the same structure.

Args:
        func: A callable that accepts as many arguments are there are structures.
        *structure: scalar, or tuple or list of constructed scalars and/or other
        tuples/lists, or scalars.  Note: numpy arrays are considered scalars.
        **check_types_dict: only valid keyword argument is `check_types`. If set to
        `True` (default) the types of iterables within the structures have to be
        same (e.g. `map_structure(func, [1], (1,))` raises a `TypeError`
        exception). To allow this set this argument to `False`.

Returns:
        A new structure with the same arity as `structure`, whose values correspond
        to `func(x[0], x[1], ...)` where `x[i]` is a value in the corresponding
        location in `structure[i]`. If there are different sequence types and
        `check_types` is `False` the sequence types of the first structure will be
        used.

Raises:
        TypeError: If `func` is not callable or if the structures do not match
        each other by depth tree.
        ValueError: If no structure is provided or if the structures do not match
        each other by type.
        ValueError: If wrong keyword arguments are provided.


def binary_accuracy(y_true, y_pred, threshold=0.5):
    y_pred = tensor_conversion.convert_to_tensor_v2_with_dispatch(y_pred)
    threshold = math_ops.cast(threshold, y_pred.dtype)
    y_pred = math_ops.cast(y_pred > threshold, y_pred.dtype)
    return backend.mean(math_ops.equal(y_true, y_pred), axis=-1)

Calculates how often predictions match binary labels.

    Standalone usage:
    >>> y_true = [[1], [1], [0], [0]]
    >>> y_pred = [[1], [1], [0], [0]]
    >>> m = tf.keras.metrics.binary_accuracy(y_true, y_pred)
    >>> assert m.shape == (4,)
    >>> m.numpy()
    array([1., 1., 1., 1.], dtype=float32)

Args:
        y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.
        threshold: (Optional) Float representing the threshold for deciding whether
        prediction values are 1 or 0.

Returns:
        Binary accuracy values. shape = `[batch_size, d0, .. dN-1]`


def __init__(self, gain=1.0):
    self.gain = gain

Initializer that generates the identity matrix.

    Only usable for generating 2D matrices.

Example:
    >>> # Standalone usage:
    >>> initializer = Identity()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = Identity()
    >>> layer = Dense(3, kernel_initializer=initializer)

Args:
        gain: Multiplicative factor to apply to the identity matrix.


def save_pcoder(self, pcoder, *labels):
    raise NotImplementedError

Saves pcoder for given PCollection.

    Correct reading of PCollection from Cache requires PCoder to be known.
    This method saves desired PCoder for PCollection that will subsequently
    be used by sink(...), source(...), and, most importantly, read(...) method.
    The latter must be able to read a PCollection written by Beam using
    non-Beam IO.

Args:
        pcoder: A PCoder to be used for reading and writing a PCollection.
        *labels: List of labels for PCollection instance.


def __init__(self, mask_value=0.0, **kwargs):
    super().__init__(**kwargs)
    if isinstance(mask_value, dict) and mask_value.get('config', None):
        mask_value = deserialize_keras_object(mask_value)
    self.mask_value = mask_value
    self.supports_masking = True
    self._build_at_init()

Masks a sequence by using a mask value to skip timesteps.

    For each timestep in the input tensor (dimension #1 in the tensor),
    if all values in the input tensor at that timestep
    are equal to `mask_value`, then the timestep will be masked (skipped)
    in all downstream layers (as long as they support masking).

    If any downstream layer does not support masking yet receives such
    an input mask, an exception will be raised.

Example:
    Consider a NumPy data array `x` of shape `(samples, timesteps, features)`,
    to be fed to an LSTM layer. You want to mask timestep #3 and #5 because you
    lack data for these timesteps. You can:

    - Set `x[:, 3, :] = 0.` and `x[:, 5, :] = 0.`
    - Insert a `Masking` layer with `mask_value=0.` before the LSTM layer:

    ```python
    samples, timesteps, features = 32, 10, 8
    inputs = np.random.random([samples, timesteps, features]).astype(np.float32)
    inputs[:, 3, :] = 0.
    inputs[:, 5, :] = 0.

    model = keras.models.Sequential()
    model.add(keras.layers.Masking(mask_value=0.0))
    model.add(keras.layers.LSTM(32))
    output = model(inputs)
    # The time step 3 and 5 will be skipped from LSTM calculation.
    ```

    Note: in the Keras masking convention, a masked timestep is denoted by
    a mask value of `False`, while a non-masked (i.e. usable) timestep
    is denoted by a mask value of `True`.


def get_bool(self):
    return self.fdp.ConsumeBool()

Consume a bool.

Returns:
        Consumed a bool based on input bytes and constraints.


def value(self, row, col, device_name_filter=None, node_name_filter=None, op_type_filter=None):
    menu_item = None
    if col == 0:
        text = self._profile_datum_list[row].node_exec_stats.node_name
    elif col == 1:
        text = self._profile_datum_list[row].op_type
    elif col == 2:
        text = str(self.formatted_start_time[row])
    elif col == 3:
        text = str(self.formatted_op_time[row])
    elif col == 4:
        text = str(self.formatted_exec_time[row])
    elif col == 5:
        command = 'ps'
        if device_name_filter:
            command += ' --%s %s' % (_DEVICE_NAME_FILTER_FLAG, device_name_filter)
        if node_name_filter:
            command += ' --%s %s' % (_NODE_NAME_FILTER_FLAG, node_name_filter)
        if op_type_filter:
            command += ' --%s %s' % (_OP_TYPE_FILTER_FLAG, op_type_filter)
        command += ' %s --init_line %d' % (self._profile_datum_list[row].file_path, self._profile_datum_list[row].line_number)
        menu_item = debugger_cli_common.MenuItem(None, command)
        text = self._profile_datum_list[row].file_line_func
    else:
        raise IndexError('Invalid column index %d.' % col)
    return RL(text, font_attr=menu_item)

Get the content of a cell of the table.

Args:
        row: (int) row index.
        col: (int) column index.
        device_name_filter: Regular expression to filter by device name.
        node_name_filter: Regular expression to filter by node name.
        op_type_filter: Regular expression to filter by op type.

Returns:
        A debuggre_cli_common.RichLine object representing the content of the
        cell, potentially with a clickable MenuItem.

Raises:
        IndexError: if row index is out of range.


def forward(self, pixel_values: torch.FloatTensor, audio_values: torch.FloatTensor, pixel_mask: Optional[torch.FloatTensor]=None, audio_mask: Optional[torch.FloatTensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, labels: Optional[torch.LongTensor]=None) -> Union[Tuple[torch.FloatTensor], SequenceClassifierOutput]:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    outputs = self.tvlt(pixel_values, audio_values, pixel_mask=pixel_mask, audio_mask=audio_mask, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    sequence_output = outputs[0][:, 0]
    logits = self.classifier(sequence_output)
    loss = None
    if labels is not None:
        if self.config.loss_type == 'regression':
            loss_fct = MSELoss()
            loss = loss_fct(logits, labels)
        elif self.config.loss_type == 'classification':
            loss_fct = CrossEntropyLoss()
            loss = loss_fct(logits, labels)
    if not return_dict:
        output = (logits,) + outputs[4:]
        return (loss,) + output if loss is not None else output
    return SequenceClassifierOutput(loss=loss, logits=logits, hidden_states=outputs.hidden_states, attentions=outputs.attentions)

labels (`torch.LongTensor` of shape `(batch_size, num_labels)`, *optional*):
    Labels for computing the audiovisual loss. Indices should be in `[0, ..., num_classes-1]` where num_classes
    refers to the number of classes in audiovisual tasks.

Returns:

Example:
    ```python
    >>> from transformers import TvltProcessor, TvltForAudioVisualClassification
    >>> import numpy as np
    >>> import torch

    >>> num_frames = 8
    >>> images = list(np.random.randn(num_frames, 3, 224, 224))
    >>> audio = list(np.random.randn(10000))
    >>> processor = TvltProcessor.from_pretrained("ZinengTang/tvlt-base")
    >>> model = TvltForAudioVisualClassification.from_pretrained("ZinengTang/tvlt-base")
    >>> input_dict = processor(images, audio, sampling_rate=44100, return_tensors="pt")

    >>> outputs = model(**input_dict)
    >>> loss = outputs.loss
    ```


def to(self, device: Union[str, 'torch.device'], *, non_blocking: bool=False) -> 'BatchEncoding':
    requires_backends(self, ['torch'])
    import torch
    if isinstance(device, str) or is_torch_device(device) or isinstance(device, int):
        self.data = {k: v.to(device=device, non_blocking=non_blocking) if isinstance(v, torch.Tensor) else v for k, v in self.data.items()}
    else:
        logger.warning(f'Attempting to cast a BatchEncoding to type {str(device)}. This is not supported.')
    return self

Send all values to device by calling `v.to(device, non_blocking=non_blocking)` (PyTorch only).

Args:
        device (`str` or `torch.device`): The device to put the tensors on.
        non_blocking (`bool`): Whether to perform the copy asynchronously.

Returns:
        [`BatchEncoding`]: The same instance after modification.


def get_libstdcpp_version():
    key = 'libstdcpp_ver'
    out, err = run_shell_cmd(cmds_all[PLATFORM.lower()][key])
    if err and FLAGS.debug:
        print('Error in detecting libstdc++ version:\n %s' % str(err))
    ver = out.split(b'_')[-1].replace(b'\n', b'')
    return ver

Retrieves version of libstdc++ detected.

Returns:
        String that is the version of libstdc++.
        e.g. '3.4.25'


def write(tag, tensor, step=None, metadata=None, name=None):
    with ops.name_scope(name, 'write_summary') as scope:
        if _summary_state.writer is None:
            return constant_op.constant(False)
        if step is None:
            step = get_step()
        if metadata is None:
            serialized_metadata = b''
        elif hasattr(metadata, 'SerializeToString'):
            serialized_metadata = metadata.SerializeToString()
        else:
            serialized_metadata = metadata

        def record():
            """Record the actual summary and return True."""
            if step is None:
                raise ValueError('No step set. Please specify one either through the `step` argument or through tf.summary.experimental.set_step()')
            with ops.device('cpu:0'):
                summary_tensor = tensor() if callable(tensor) else array_ops.identity(tensor)
                writer = _summary_state.writer
                summary_value = _maybe_convert_tensor_to_dtensor(writer, summary_tensor)
                step_value = _maybe_convert_tensor_to_dtensor(writer, step)
                write_summary_op = gen_summary_ops.write_summary(writer._resource, step_value, summary_value, tag, serialized_metadata, name=scope)
                with ops.control_dependencies([write_summary_op]):
                    return constant_op.constant(True)
        op = smart_cond.smart_cond(should_record_summaries(), record, _nothing, name='summary_cond')
        if not context.executing_eagerly():
            ops.add_to_collection(ops.GraphKeys._SUMMARY_COLLECTION, op)
        return op

Writes a generic summary to the default SummaryWriter if one exists.

    This exists primarily to support the definition of type-specific summary ops
    like scalar() and image(), and is not intended for direct use unless defining
    a new type-specific summary op.

Args:
        tag: string tag used to identify the summary (e.g. in TensorBoard), usually
        generated with `tf.summary.summary_scope`
        tensor: the Tensor holding the summary data to write or a callable that
        returns this Tensor. If a callable is passed, it will only be called when
        a default SummaryWriter exists and the recording condition specified by
        `record_if()` is met.
        step: Explicit `int64`-castable monotonic step value for this summary. If
        omitted, this defaults to `tf.summary.experimental.get_step()`, which must
        not be None.
        metadata: Optional SummaryMetadata, as a proto or serialized bytes
        name: Optional string name for this op.

Returns:
        True on success, or false if no summary was written because no default
        summary writer was available.

Raises:
        ValueError: if a default writer exists, but no step was provided and
        `tf.summary.experimental.get_step()` is None.


def conv2d(x, kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1)):
    if data_format is None:
        data_format = image_data_format()
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Unknown data_format: ' + str(data_format))
    x, tf_data_format = _preprocess_conv2d_input(x, data_format)
    padding = _preprocess_padding(padding)
    x = nn.convolution(input=x, filter=kernel, dilation_rate=dilation_rate, strides=strides, padding=padding, data_format=tf_data_format)
    if data_format == 'channels_first' and tf_data_format == 'NHWC':
        x = array_ops.transpose(x, (0, 3, 1, 2))
    return x

2D convolution.

Args:
        x: Tensor or variable.
        kernel: kernel tensor.
        strides: strides tuple.
        padding: string, `"same"` or `"valid"`.
        data_format: `"channels_last"` or `"channels_first"`.
        dilation_rate: tuple of 2 integers.

Returns:
        A tensor, result of 2D convolution.

Raises:
        ValueError: if `data_format` is neither `channels_last` or
        `channels_first`.


def _TensorArraySplitGrad(op: ops.Operation, flow):
    handle = op.inputs[0]
    dtype = op.get_attr('T')
    grad_source = _GetGradSource(flow)
    flow_out = array_ops.identity(op.outputs[0], 'flow_out')
    with ops.control_dependencies([flow_out]):
        flow = array_ops.identity(flow, 'write_barrier')
    g = tensor_array_ops.TensorArray(dtype=dtype, handle=handle, flow=flow, colocate_with_first_write_call=False).grad(source=grad_source, flow=flow)
    grad = g.concat()
    return [None, grad, None, flow]

Gradient for TensorArraySplit.

Args:
        op: Forward TensorArraySplit op.
        flow: Gradient `Tensor` flow to TensorArraySplit.

Returns:
        A grad `Tensor`, the gradient created in upstream ReadGrads or PackGrad.


def as_graph_element(self, obj, allow_tensor=True, allow_operation=True) -> Union[tensor_lib.Tensor, 'Operation']:
    if self._finalized:
        return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
    with self._lock:
        return self._as_graph_element_locked(obj, allow_tensor, allow_operation)

Returns the object referred to by `obj`, as an `Operation` or `Tensor`.

    This function validates that `obj` represents an element of this
    graph, and gives an informative error message if it is not.

    This function is the canonical way to get/validate an object of
    one of the allowed types from an external argument reference in the
    Session API.

    This method may be called concurrently from multiple threads.

Args:
        obj: A `Tensor`, an `Operation`, or the name of a tensor or operation. Can
        also be any object with an `_as_graph_element()` method that returns a
        value of one of these types. Note: `_as_graph_element` will be called
        inside the graph's lock and so may not modify the graph.
        allow_tensor: If true, `obj` may refer to a `Tensor`.
        allow_operation: If true, `obj` may refer to an `Operation`.

Returns:
        The `Tensor` or `Operation` in the Graph corresponding to `obj`.

Raises:
        TypeError: If `obj` is not a type we support attempting to convert
        to types.
        ValueError: If `obj` is of an appropriate type but invalid. For
        example, an invalid string.
        KeyError: If `obj` is not an object in the graph.


def infer_return_type(c, input_types, debug=False, depth=5):
    try:
        if hashable(c) and c in known_return_types:
            return known_return_types[c]
        elif isinstance(c, types.FunctionType):
            return infer_return_type_func(c, input_types, debug, depth)
        elif isinstance(c, types.MethodType):
            if c.__self__ is not None:
                input_types = [Const(c.__self__)] + input_types
            return infer_return_type_func(c.__func__, input_types, debug, depth)
        elif isinstance(c, BoundMethod):
            input_types = [c.type] + input_types
            return infer_return_type_func(c.func, input_types, debug, depth)
        elif inspect.isclass(c):
            if c in typehints.DISALLOWED_PRIMITIVE_TYPES:
                return {list: typehints.List[Any], set: typehints.Set[Any], frozenset: typehints.FrozenSet[Any], tuple: typehints.Tuple[Any, ...], dict: typehints.Dict[Any, Any]}[c]
            return c
        elif c == getattr and len(input_types) == 2 and isinstance(input_types[1], Const):
            from apache_beam.typehints import opcodes
            return opcodes._getattr(input_types[0], input_types[1].value)
        elif isinstance(c, python_callable.PythonCallableWithSource):
            return infer_return_type(c._callable, input_types, debug, depth)
        else:
            return Any
    except TypeInferenceError:
        if debug:
            traceback.print_exc()
        return Any
    except Exception:
        if debug:
            sys.stdout.flush()
            raise
        else:
            return Any

Analyses a callable to deduce its return type.

Args:
        c: A Python callable to infer the return type of.
        input_types: A sequence of inputs corresponding to the input types.
        debug: Whether to print verbose debugging information.
        depth: Maximum inspection depth during type inference.

Returns:
        A TypeConstraint that that the return value of this function will (likely)
        satisfy given the specified inputs.


def _CanDelete(self, cls):
    if not self._processed_count[cls.name]:
        return False
    return self._processed_count[cls.name] == self._total_count[cls.name]

Checks whether this class can be deleted.

    Returns whether all occurrences of this class as a type were due to
    constants we removed.

Args:
        cls: A pytd.Class.

Returns:
        True if we can delete this class.


def _neg(x, name=None):
    return negative(x, name)

Computes numerical negative value element-wise.

    I.e., \(y = -x\).

Args:
        x: A `Tensor` or `SparseTensor`. Must be one of the following types: `half`,
        `float32`, `float64`, `int32`, `int64`, `complex64`, `complex128`.
        name: A name for the operation (optional).

Returns:
        A `Tensor` or `SparseTensor`, respectively. Has the same type as `x`.


def _sub_entity_map(self, assignments, item, campaign):
    for assignment in assignments:
        placement = self._placement_dao.get(assignment, required=True)
        event_tag = self._event_tag_dao.get(assignment, required=True)
        creative = self._creative_dao.get(assignment, required=True)
        landing_page = None
        if assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') != 'CAMPAIGN_DEFAULT':
            landing_page = self._landing_page_dao.get(assignment, required=True)
        if landing_page:
            assignment[FieldMap.AD_LANDING_PAGE_ID] = landing_page['id']
        if item:
            assignment[FieldMap.AD_ID] = item['id']
            assignment[FieldMap.AD_NAME] = item['name']
        if campaign:
            assignment[FieldMap.CAMPAIGN_ID] = campaign['id']
            assignment[FieldMap.CAMPAIGN_NAME] = campaign['name']
        if placement:
            assignment[FieldMap.PLACEMENT_ID] = placement['id']
            assignment[FieldMap.PLACEMENT_NAME] = placement['name']
        if creative:
            assignment[FieldMap.CREATIVE_ID] = creative['id']
            assignment[FieldMap.CREATIVE_NAME] = creative['name']
        if event_tag:
            assignment[FieldMap.EVENT_TAG_ID] = event_tag['id']
            assignment[FieldMap.EVENT_TAG_NAME] = event_tag['name']

Maps ids and names of sub entities so they can be updated in the Bulkdozer feed.

    When Bulkdozer is done processing an item, it writes back the updated names
    and ids of related objects, this method makes sure those are updated in the
    ad feed.

Args:
        assignments: List of child feeds to map.
        item: The DCM ad object that was updated or created.
        campaign: The campaign object associated with the ad.


def __init__(self, output_size: Union[int, Iterable[int]], reduce_function: Callable=tf.reduce_mean, data_format: Optional[str]=None, **kwargs) -> None:
    self.data_format = normalize_data_format(data_format)
    self.reduce_function = reduce_function
    self.output_size = (output_size,) if isinstance(output_size, int) else tuple(output_size)
    super().__init__(**kwargs)

Args:
    Average 1D Pooling with adaptive kernel size.
        output_size: An integer or tuple/list of a single integer, specifying pooled_features.
        The new size of output channels.
        data_format: A string,
        one of `channels_last` (default) or `channels_first`. The ordering of the dimensions in the inputs.
        `channels_last` corresponds to inputs with shape `(batch, steps, channels)` while `channels_first` corresponds
        to inputs with shape `(batch, channels, steps)`.
    Input shape:
        - If `data_format='channels_last'`: 3D tensor with shape `(batch, steps, channels)`.
        - If `data_format='channels_first'`: 3D tensor with shape `(batch, channels, steps)`.
    Output shape:
        - If `data_format='channels_last'`: 3D tensor with shape `(batch_size, pooled_steps, channels)`.
        - If `data_format='channels_first'`: 3D tensor with shape `(batch_size, channels, pooled_steps)`.

    Adapted from [tensorflow-addon's adaptive pooling.py](
        https://github.com/tensorflow/addons/blob/8cec33fcaaf1cf90aec7bdd55a0fcdbb251ce5c2/tensorflow_addons/layers/adaptive_pooling.py#L90-L120
    )


def _subsample_labels(self, label):
    pos_idx, neg_idx = subsample_labels(label, self.batch_size_per_image, self.positive_fraction, 0)
    label.fill_(-1)
    label.scatter_(0, pos_idx, 1)
    label.scatter_(0, neg_idx, 0)
    return label

Randomly sample a subset of positive and negative examples, and overwrite the label vector to the ignore value
    (-1) for all elements that are not included in the sample.

Args:
        labels (Tensor): a vector of -1, 0, 1. Will be modified in-place and returned.


def transform_and_print_file(self, file_path: str, transformation: Optional[Callable[[Iterator[str]], Iterator[str]]]=None, output_stream: io.TextIOBase=cast(io.TextIOBase, sys.stdout)) -> None:
    if transformation is None:
        transformation = self.annotate_test_file
    if file_path == _STANDARD_IO_STREAMS:
        output_stream.writelines(transformation(sys.stdin))
    else:
        with open(file_path, 'r') as file_contents:
            output_stream.writelines(transformation(file_contents))

Reads from `file_path`, applies a transformation, and prints to `stdout`.

Args:
        file_path: The path to the input file. If this is equal to the constant
        `_STANDARD_IO_STREAMS` (i.e. the string "-"), the input will come from
        `stdin`.
        transformation: A function that takes an iterator over the lines of an HLO
        file and returns an iterator over the lines of the transformed file. If
        this is left as `None`, `self.annotate_test_file` will be used.
        output_stream: The stream to which the transformed file should be written.
        Defaults to `stdout`.


def affine_transform(boxes, angle, translate_x, translate_y, scale, shear_x, shear_y, height, width, center_x=None, center_y=None, bounding_box_format='xyxy'):
    if bounding_box_format != 'xyxy':
        raise NotImplementedError
    box_utils = BoundingBox()
    if backend_utils.in_tf_graph():
        box_utils.backend.set_backend('tensorflow')
    boxes = box_utils.affine(boxes, angle, translate_x, translate_y, scale, shear_x, shear_y, height, width, center_x=center_x, center_y=center_y)
    box_utils.backend.reset()
    return boxes

Applies an affine transformation to the bounding boxes.

    The `height` and `width` parameters are used to normalize the
    translation and scaling factors.

Args:
        boxes: The bounding boxes to transform, a tensor/array of shape
            `(N, 4)` or `(batch_size, N, 4)`.
        angle: Rotation angle in degrees.
        translate_x: Horizontal translation fraction.
        translate_y: Vertical translation fraction.
        scale: Scaling factor.
        shear_x: Shear angle in x-direction (degrees).
        shear_y: Shear angle in y-direction (degrees).
        height: Height of the image/data.
        width: Width of the image/data.
        center_x:  x-coordinate of the transformation center (fraction).
        center_y: y-coordinate of the transformation center (fraction).
        bounding_box_format: The format of the input bounding boxes. Defaults to
            `"xyxy"`.

Returns:
        The transformed bounding boxes, a tensor/array with the same shape
        as the input `boxes`.


def verbosity_from(options):
    level = logging.getLogger().getEffectiveLevel()
    _set_verbosity(options.verbosity, options.timestamp_logs, options.debug_logs)
    try:
        yield
    finally:
        logging.getLogger().setLevel(level)

Sets the logging level to options.verbosity and restores it afterwards.

    If you directly call any of pytype's internal methods,
    like analyze.infer_types, use this contextmanager to set the logging
    verbosity. Consider using one of the top-level methods in pytype.io instead,
    which take care of this detail for you.

Args:
        options: A config.Options object.

Yields:
        Nothing.


def _assert_sparse_indices_are_ragged_right(indices):
    index_prefix = indices[:, :-1]
    index_suffix = indices[:, -1]
    index_prefix_changed = math_ops.reduce_any(math_ops.not_equal(index_prefix[1:], index_prefix[:-1]), axis=1)
    index_ok = array_ops.where(index_prefix_changed, math_ops.equal(index_suffix[1:], 0), math_ops.equal(index_suffix[1:], index_suffix[:-1] + 1))
    sparse_indices_are_ragged_right = math_ops.logical_and(math_ops.reduce_all(math_ops.equal(index_suffix[:1], 0)), math_ops.reduce_all(index_ok))
    message = ['SparseTensor is not right-ragged', 'SparseTensor.indices =', indices]
    return [control_flow_assert.Assert(sparse_indices_are_ragged_right, message)]

Checks that the given SparseTensor.indices tensor is ragged-right.

    Example: `indices = [[0, 0], [0, 1], [2, 0], [3, 1]]` is not ragged right
    because the entry `[3, 1]` skips a cell.

Args:
        indices: The SparseTensor indices to check.

Returns:
        A list of control dependency op tensors.


def _validate_alias_file_path(alias_file_path):

    if not os.path.exists(alias_file_path):
        raise CLIError(ALIAS_FILE_NOT_FOUND_ERROR)

    if os.path.isdir(alias_file_path):
        raise CLIError(ALIAS_FILE_DIR_ERROR.format(alias_file_path))

Make sure the alias file path is neither non-existant nor a directory

Args:
        The alias file path to import aliases from.


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 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

    # Compare the list of available plugin objects.
    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:  # pylint: disable=broad-except
          parser_mediator.ProduceExtractionWarning((
              'plugin: {0:s} unable to parse ESE database with error: '
              '{1!s}').format(plugin.NAME, exception))

    finally:
      # TODO: explicitly clean up cache.

      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 __init__(self, dataframe, map_info):

        self.df = dataframe
        self.map_info = map_info

Reads genotypes from a pandas DataFrame.

Args:
        dataframe (pandas.DataFrame): The data.
        map_info (pandas.DataFrame): The mapping information.

    Note
    ====
        The index of the dataframe should be the sample IDs. The index of
        the map_info should be the variant name, and there should be
        columns named chrom and pos.


def info_qry(tickers, flds) -> str:

    full_list = '\n'.join([f'tickers: {tickers[:8]}'] + [
        f'         {tickers[n:(n + 8)]}' for n in range(8, len(tickers), 8)
    ])
    return f'{full_list}\nfields:  {flds}'

Logging info for given tickers and fields

Args:
        tickers: tickers
        flds: fields

Returns:
        str

Example:
        >>> print(info_qry(
        ...     tickers=['NVDA US Equity'], flds=['Name', 'Security_Name']
        ... ))
        tickers: ['NVDA US Equity']
        fields:  ['Name', 'Security_Name']


def __init__(self, pos=(0, 0, 0, -100), branches=None, sigma=(0, 0)):

        self.pos = pos
        self.length = sqrt((pos[2]-pos[0])**2+(pos[3]-pos[1])**2)
        self.branches = branches
        self.sigma = sigma

        self.comp = len(self.branches)
        self.age = 0

        self.nodes = [
            [Node(pos[2:])]
        ]

The contructor.

Args:
        pos (tupel): A tupel, holding the start and end point of the tree. (x1, y1, x2, y2)
        branches (tupel/array): Holding array/s with scale and angle for every branch.
        sigma (tuple): Holding the branch and angle sigma. e.g.(0.1, 0.2)


def _build_kernel(self, kernel_source, compile_flags=()):

        return cl.Program(self._cl_context, kernel_source).build(' '.join(compile_flags))

Convenience function for building the kernel for this worker.

Args:
        kernel_source (str): the kernel source to use for building the kernel

Returns:
        cl.Program: a compiled CL kernel


def start_proc_mask_signal(proc):

    if not isinstance(proc, list):
        proc = [proc]

    with mask_sigint():
        for p in proc:
            if isinstance(p, mp.Process):
                if sys.version_info < (3, 4) or mp.get_start_method() == 'fork':
                    log_once(
"Starting a process with 'fork' method is not safe and may consume unnecessary extra memory."
" Use 'forkserver' method (available after Py3.4) instead if you run into any issues. "
"See https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods",
'warn')  # noqa
            p.start()

Start process(es) with SIGINT ignored.

Args:
        proc: (mp.Process or list)

Note:
        The signal mask is only applied when called from main thread.


def load_many(self, fobjs=None):

        if fobjs is not None:
            # tolerance
            if not hasattr(fobjs, "__iter__"):
                fobjs = [fobjs]

            for index, (fobj, page) in enumerate(zip(fobjs, self.pages)):
                if fobj is None:
                    continue
                elif isinstance(fobj, ft.DataFile):
                    self.load(fobj, index)
                elif isinstance(fobj, str):
                    self.load_filename(fobj, index)
                else:
                    raise TypeError("Invalid object of class '{}'".format(fobj.__class__.__name__))

Loads as many files as the number of pages

Args:
        fobjs: [filename or DataFile obj, ...]


def __init__(self, grr_worker=None):

    self.grr_worker = grr_worker
    self.response_id = INITIAL_RESPONSE_ID
    self.cpu_used = None
    self.nanny_controller = None
    self.status = rdf_flows.GrrStatus(
        status=rdf_flows.GrrStatus.ReturnedStatus.OK)
    self._last_gc_run = rdfvalue.RDFDatetime.Now()
    self._gc_frequency = config.CONFIG["Client.gc_frequency"]
    self.proc = psutil.Process()
    self.cpu_start = self.proc.cpu_times()
    self.cpu_limit = rdf_flows.GrrMessage().cpu_limit

Initializes the action plugin.

Args:
        grr_worker:  The grr client worker object which may be used to e.g. send
        new actions on.


def get_numeric_features_to_observed_range(examples):

  observed_features = collections.defaultdict(list)  # name -> [value, ]
  for example in examples:
    for feature_name in get_numeric_feature_names(example):
      original_feature = parse_original_feature_from_example(
          example, feature_name)
      observed_features[feature_name].extend(original_feature.original_value)
  return {
      feature_name: {
          'observedMin': min(feature_values),
          'observedMax': max(feature_values),
      }
      for feature_name, feature_values in iteritems(observed_features)
  }

Returns numerical features and their observed ranges.

Args:
        examples: Examples to read to get ranges.

Returns:
        A dict mapping feature_name -> {'observedMin': 'observedMax': } dicts,
        with a key for each numerical feature.


def register_event(self, direction, verb, child_fn, priority=10):

        if verb not in self._event_handlers:
            self._event_handlers[verb] = []

        self._event_handlers[verb].append({
            'handler': child_fn,
            'direction': direction,
            'priority': priority,
        })

        for name, server in self.servers.items():
            server.register_event(direction, verb, child_fn, priority=priority)

Register an event with all servers.

Args:
        direction (str): `in`, `out`, `both`, `raw`.
        verb (str): Event name.
        child_fn (function): Handler function.
        priority (int): Handler priority (lower priority executes first).


def range(self, start_row=0, max_rows=None):

    fetcher = self._get_row_fetcher(start_row=start_row, max_rows=max_rows)
    return iter(datalab.utils.Iterator(fetcher))

Get an iterator to iterate through a set of table rows.

Args:
        start_row: the row of the table at which to start the iteration (default 0)
        max_rows: an upper limit on the number of rows to iterate through (default None)

Returns:
        A row iterator.


def add_droplets(self, droplet_ids):

        return self.get_data(
            "load_balancers/%s/droplets/" % self.id,
            type=POST,
            params={"droplet_ids": droplet_ids}
        )

Assign a LoadBalancer to a Droplet.

Args:
        droplet_ids (obj:`list` of `int`): A list of Droplet IDs


def reset_spent_time(self, **kwargs):

        path = '%s/%s/reset_spent_time' % (self.manager.path, self.get_id())
        return self.manager.gitlab.http_post(path, **kwargs)

Resets the time spent working on the object.

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

Raises:
        GitlabAuthenticationError: If authentication is not correct
        GitlabTimeTrackingError: If the time tracking update cannot be done


def env_or_default(var, default=None):

    if var in os.environ:
        return os.environ[var]
    return default

Get environment variable or provide default.

Args:
        var (str): environment variable to search for
        default (optional(str)): default to return


def _force_disconnect_action(self, action):

        conn_key = action.data['id']
        if self._get_connection_state(conn_key) == self.Disconnected:
            return

        data = self._get_connection(conn_key)

        # If there are any operations in progress, cancel them cleanly
        if data['state'] == self.Connecting:
            callback = data['action'].data['callback']
            callback(data['connection_id'], self.id, False, 'Unexpected disconnection')
        elif data['state'] == self.Disconnecting:
            callback = data['action'].data['callback']
            callback(data['connection_id'], self.id, True, None)
        elif data['state'] == self.InProgress:
            callback = data['action'].data['callback']
            if data['microstate'] == 'rpc':
                callback(False, 'Unexpected disconnection', 0xFF, None)
            elif data['microstate'] == 'open_interface':
                callback(False, 'Unexpected disconnection')
            elif data['microstate'] == 'close_interface':
                callback(False, 'Unexpected disconnection')

        connection_id = data['connection_id']
        internal_id = data['internal_id']
        del self._connections[connection_id]
        del self._int_connections[internal_id]

Forcibly disconnect a device.

Args:
        action (ConnectionAction): the action object describing what we are
            forcibly disconnecting


def __init__(self, pub_key=None):

        if pub_key is not None and type(pub_key) is not EllipticCurve.ECPoint:
            raise Exception("Pubkey must be ECPoint Instance")

        self.PublicKey = pub_key

Create an instance.

Args:
        pub_key (EllipticCurve.ECPoint):

Raises:
        Exception: if `pub_key` is not a valid ECPoint.


def calculate_bv_sum_unordered(site, nn_list, scale_factor=1):

    # If the site "site" has N partial occupations as : f_{site}_0,
    # f_{site}_1, ... f_{site}_N of elements
    # X_{site}_0, X_{site}_1, ... X_{site}_N, and each neighbors nn_i in nn
    # has N_{nn_i} partial occupations as :
    # f_{nn_i}_0, f_{nn_i}_1, ..., f_{nn_i}_{N_{nn_i}}, then the bv sum of
    # site "site" is obtained as :
    # \sum_{nn} \sum_j^N \sum_k^{N_{nn}} f_{site}_j f_{nn_i}_k vij_full
    # where vij_full is the valence bond of the fully occupied bond
    bvsum = 0
    for specie1, occu1 in site.species.items():
        el1 = Element(specie1.symbol)
        for (nn, dist) in nn_list:
            for specie2, occu2 in nn.species.items():
                el2 = Element(specie2.symbol)
                if (el1 in ELECTRONEG or el2 in ELECTRONEG) and el1 != el2:
                    r1 = BV_PARAMS[el1]["r"]
                    r2 = BV_PARAMS[el2]["r"]
                    c1 = BV_PARAMS[el1]["c"]
                    c2 = BV_PARAMS[el2]["c"]
                    R = r1 + r2 - r1 * r2 * (sqrt(c1) - sqrt(c2)) ** 2 / \
                        (c1 * r1 + c2 * r2)
                    vij = exp((R - dist * scale_factor) / 0.31)
                    bvsum += occu1 * occu2 * vij * (1 if el1.X < el2.X else -1)
    return bvsum

Calculates the BV sum of a site for unordered structures.

Args:
        site:
            The site
        nn_list:
            List of nearest neighbors in the format [(nn_site, dist), ...].
        scale_factor:
            A scale factor to be applied. This is useful for scaling distance,
            esp in the case of calculation-relaxed structures which may tend
            to under (GGA) or over bind (LDA).


def pairwise_alignment_stats(reference_seq_aln, other_seq_aln):

    if len(reference_seq_aln) != len(other_seq_aln):
        raise ValueError('Sequence lengths not equal - was an alignment run?')

    reference_seq_aln = ssbio.protein.sequence.utils.cast_to_str(reference_seq_aln)
    other_seq_aln = ssbio.protein.sequence.utils.cast_to_str(other_seq_aln)

    infodict = {}

    # Percent identity to the reference sequence
    stats_percent_ident = get_percent_identity(a_aln_seq=reference_seq_aln, b_aln_seq=other_seq_aln)
    infodict['percent_identity'] = stats_percent_ident

    # Other alignment results
    aln_df = get_alignment_df(a_aln_seq=reference_seq_aln, b_aln_seq=other_seq_aln)
    infodict['deletions'] = get_deletions(aln_df)
    infodict['insertions'] = get_insertions(aln_df)
    infodict['mutations'] = get_mutations(aln_df)
    infodict['unresolved'] = get_unresolved(aln_df)

    return infodict

Get a report of a pairwise alignment.

Args:
        reference_seq_aln (str, Seq, SeqRecord): Reference sequence, alignment form
        other_seq_aln (str, Seq, SeqRecord): Other sequence, alignment form

Returns:
        dict: Dictionary of information on mutations, insertions, sequence identity, etc.


def run(self, dag):

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

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

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

        for qreg in new_qregs:
            dag.add_qreg(qreg)

        return dag

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

Args:
        dag (DAGCircuit): DAG to extend.

Returns:
        DAGCircuit: An extended DAG.

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


def to_title_caps(underscore_case):
    r
    words = underscore_case.split('_')
    words2 = [
        word[0].upper() + word[1:]
        for count, word in enumerate(words)
    ]
    title_str = ' '.join(words2)
    return title_str

r"""

Args:
        underscore_case (?):

Returns:
        str: title_str

    CommandLine:
        python -m utool.util_str --exec-to_title_caps

Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_str import *  # NOQA
        >>> underscore_case = 'the_foo_bar_func'
        >>> title_str = to_title_caps(underscore_case)
        >>> result = ('title_str = %s' % (str(title_str),))
        >>> print(result)
        title_str = The Foo Bar Func


def get_ytvideos(query, ilogger):

    queue = []

    # Search YouTube
    search_result = ytdiscoveryapi.search().list(
            q=query,
            part="id,snippet",
            maxResults=1,
            type="video,playlist"
    ).execute()

    if not search_result["items"]:
        return []

    # Get video/playlist title
    title = search_result["items"][0]["snippet"]["title"]
    ilogger.info("Queueing {}".format(title))

    # Queue video if video
    if search_result["items"][0]["id"]["kind"] == "youtube#video":
        # Get ID of video
        videoid = search_result["items"][0]["id"]["videoId"]

        # Append video to queue
        queue.append(["https://www.youtube.com/watch?v={}".format(videoid), title])

    # Queue playlist if playlist
    elif search_result["items"][0]["id"]["kind"] == "youtube#playlist":
        queue = get_queue_from_playlist(search_result["items"][0]["id"]["playlistId"])

    return queue

Gets either a list of videos from a playlist or a single video, using the
    first result of a YouTube search

Args:
        query (str): The YouTube search query
        ilogger (logging.logger): The logger to log API calls to

Returns:
        queue (list): The items obtained from the YouTube search


def label_TM_tmhmm_residue_numbers_and_leaflets(tmhmm_seq):

    TM_number_dict = {}
    T_index = []
    T_residue = []

    residue_count = 1
    for residue_label in tmhmm_seq:
        if residue_label == 'T':
            T_residue.append(residue_count)

        residue_count = residue_count + 1
    TM_number_dict.update({'T_residue': T_residue})

    # finding the TM boundaries
    T_residue_list = TM_number_dict['T_residue']

    count = 0
    max_count = len(T_residue_list) - 1
    TM_helix_count = 0
    TM_boundary_dict = {}

    while count <= max_count:
        # first residue = TM start
        if count == 0:
            TM_start = T_residue_list[count]
            count = count + 1
            continue
        # Last residue = TM end
        elif count == max_count:
            TM_end = T_residue_list[count]
            TM_helix_count = TM_helix_count + 1
            TM_boundary_dict.update({'TM_helix_' + str(TM_helix_count): [TM_start, TM_end]})
            break
        # middle residues need to be start or end
        elif T_residue_list[count] != T_residue_list[count + 1] - 1:
            TM_end = T_residue_list[count]
            TM_helix_count = TM_helix_count + 1
            TM_boundary_dict.update({'TM_helix_' + str(TM_helix_count): [TM_start, TM_end]})
            # new TM_start
            TM_start = T_residue_list[count + 1]
        count = count + 1
    # assign leaflet to proper TM residues O or I
    leaflet_dict = {}
    for leaflet in ['O', 'I']:
        leaflet_list = []
        for TM_helix, TM_residues in TM_boundary_dict.items():
            for residue_num in TM_residues:
                tmhmm_seq_index = residue_num - 1
                previous_residue = tmhmm_seq_index - 1
                next_residue = tmhmm_seq_index + 1
                # identify if the previous or next residue closest to the TM helix start/end is the proper leaflet
                if tmhmm_seq[previous_residue] == leaflet or tmhmm_seq[next_residue] == leaflet:
                    leaflet_list.append(residue_num)
        leaflet_dict.update({'tmhmm_leaflet_' + leaflet: leaflet_list})

    return TM_boundary_dict, leaflet_dict

Determine the residue numbers of the TM-helix residues that cross the membrane and label them by leaflet.

Args:
        tmhmm_seq: g.protein.representative_sequence.seq_record.letter_annotations['TM-tmhmm']

Returns:
        leaflet_dict: a dictionary with leaflet_variable : [residue list] where the variable is inside or outside
        TM_boundary dict: outputs a dictionar with : TM helix number : [TM helix residue start , TM helix residue end]

    TODO:
        untested method!


def insert(self, iterable, index=0, data=None, weight=1.0):

        if index == len(iterable):
            self.is_terminal = True
            self.key = iterable
            self.weight = weight
            if data:
                self.data.add(data)
        else:
            if iterable[index] not in self.children:
                self.children[iterable[index]] = TrieNode()
            self.children[iterable[index]].insert(iterable, index + 1, data)

Insert new node into tree

Args:
        iterable(hashable): key used to find in the future.
        data(object): data associated with the key
        index(int): an index used for insertion.
        weight(float): the wait given for the item added.


def __init__(self, arm_id_list):

        [self.__beta_dist_dict.setdefault(key, BetaDist()) for key in arm_id_list]

Initialization

Args:
        arm_id_list:    List of arms Master id.


def _complete_batch(self, batch, error, base_offset, timestamp_ms=None):

        # Standardize no-error to None
        if error is Errors.NoError:
            error = None

        if error is not None and self._can_retry(batch, error):
            # retry
            log.warning("Got error produce response on topic-partition %s,"
                        " retrying (%d attempts left). Error: %s",
                        batch.topic_partition,
                        self.config['retries'] - batch.attempts - 1,
                        error)
            self._accumulator.reenqueue(batch)
            self._sensors.record_retries(batch.topic_partition.topic, batch.record_count)
        else:
            if error is Errors.TopicAuthorizationFailedError:
                error = error(batch.topic_partition.topic)

            # tell the user the result of their request
            batch.done(base_offset, timestamp_ms, error)
            self._accumulator.deallocate(batch)
            if error is not None:
                self._sensors.record_errors(batch.topic_partition.topic, batch.record_count)

        if getattr(error, 'invalid_metadata', False):
            self._metadata.request_update()

        # Unmute the completed partition.
        if self.config['guarantee_message_order']:
            self._accumulator.muted.remove(batch.topic_partition)

Complete or retry the given batch of records.

Args:
        batch (RecordBatch): The record batch
        error (Exception): The error (or None if none)
        base_offset (int): The base offset assigned to the records if successful
        timestamp_ms (int, optional): The timestamp returned by the broker for this batch


def _model_to_dict(model, ignore):

        return {attr: value for attr, value in model.__dict__.items()
                if not attr.startswith('_') and attr not in ignore}

Convert OSS model to dict.

Args:
        model (oss2.models.RequestResult): Model.
        ignore (tuple of str): Keys to not insert to dict.

Returns:
        dict: Model dict version.


def get_iterator_type(script_settings, subscripts={}):

        if 'iterator_type' in script_settings:
            # figure out the iterator type
            if script_settings['iterator_type'] == 'Loop':
                iterator_type = 'loop'
            elif script_settings['iterator_type'] == 'Parameter Sweep':
                iterator_type = 'sweep'
            else:
                raise TypeError('unknown iterator type')
        else:
            # asign the correct iterator script type
            if 'sweep_param' in script_settings:
                iterator_type = 'sweep'
            elif 'num_loops' in script_settings:
                iterator_type = 'loop'
            else:
                raise TypeError('unknown iterator type')

        return iterator_type

figures out the iterator type based on the script settings and (optionally) subscripts

Args:
        script_settings: iterator_type
        subscripts: subscripts
    Returns:


def set_y_grid_info(self, y_low, y_high, num_y, yscale, yval_name):

        self._set_grid_info('y', y_low, y_high, num_y, yscale, yval_name)
        return

Set the grid values for y.

    Create information for the grid of y values.

Args:
        num_y (int): Number of points on axis.
        y_low/y_high (float): Lowest/highest value for the axis.
        yscale (str): Scale of the axis. Choices are 'log' or 'lin'.
        yval_name (str): Name representing the axis. See GenerateContainer documentation
            for options for the name.


def _SimpleEncoder(wire_type, encode_value, compute_value_size):

  def SpecificEncoder(field_number, is_repeated, is_packed):
    if is_packed:
      tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
      local_EncodeVarint = _EncodeVarint
      def EncodePackedField(write, value):
        write(tag_bytes)
        size = 0
        for element in value:
          size += compute_value_size(element)
        local_EncodeVarint(write, size)
        for element in value:
          encode_value(write, element)
      return EncodePackedField
    elif is_repeated:
      tag_bytes = TagBytes(field_number, wire_type)
      def EncodeRepeatedField(write, value):
        for element in value:
          write(tag_bytes)
          encode_value(write, element)
      return EncodeRepeatedField
    else:
      tag_bytes = TagBytes(field_number, wire_type)
      def EncodeField(write, value):
        write(tag_bytes)
        return encode_value(write, value)
      return EncodeField

  return SpecificEncoder

Return a constructor for an encoder for fields of a particular type.

Args:
        wire_type:  The field's wire type, for encoding tags.
        encode_value:  A function which encodes an individual value, e.g.
        _EncodeVarint().
        compute_value_size:  A function which computes the size of an individual
        value, e.g. _VarintSize().


def valid_paths(self, *args):

        for i, path in enumerate(args, start=0):
            cp = list(args)
            current = cp.pop(i)
            if current in cp:
                raise SettingsInvalidError("Multiple occurences finded for "
                                           "path: {}".format(current))

        return True

Validate that given paths are not the same.

Args:
        (string): Path to validate.

Raises:
        boussole.exceptions.SettingsInvalidError: If there is more than one
            occurence of the same path.

Returns:
        bool: ``True`` if paths are validated.


def image_format(value):

    if value.image.format.upper() not in constants.ALLOWED_IMAGE_FORMATS:
        raise ValidationError(MESSAGE_INVALID_IMAGE_FORMAT)

Confirms that the uploaded image is of supported format.

Args:
        value (File): The file with an `image` property containing the image

Raises:
        django.forms.ValidationError


def _get_object_from_python_path(python_path):

        # Dissect the path
        python_path = python_path.split('.')
        module_path = python_path[:-1]
        object_class = python_path[-1]

        if isinstance(module_path, list):
            module_path = '.'.join(module_path)

        # Grab the object
        module = import_module(module_path)
        schema = getattr(module, object_class)

        if isclass(schema):
            schema = schema()

        return schema

Method that will fetch a Marshmallow schema from a path to it.

Args:
        python_path (str): The string path to the Marshmallow schema.

Returns:
        marshmallow.Schema: The schema matching the provided path.

Raises:
        TypeError: This is raised if the specified object isn't
            a Marshmallow schema.


def _parse_authors(details):

    authors = details.find(
        "tr",
        {"id": "ctl00_ContentPlaceHolder1_tblRowAutor"}
    )

    if not authors:
        return []  # book with unspecified authors

    # parse authors from HTML and convert them to Author objects
    author_list = []
    for author in authors[0].find("a"):
        author_obj = Author(author.getContent())

        if "href" in author.params:
            author_obj.URL = author.params["href"]

        author_list.append(author_obj)

    return author_list

Parse authors of the book.

Args:
        details (obj): HTMLElement containing slice of the page with details.

Returns:
        list: List of :class:`structures.Author` objects. Blank if no author \
            found.


def note_versions(self, updater_id=None, post_id=None, note_id=None):

        params = {
            'search[updater_id]': updater_id,
            'search[post_id]': post_id,
            'search[note_id]': note_id
            }
        return self._get('note_versions.json', params)

Get list of note versions.

    Parameters:
        updater_id (int):
        post_id (int):
        note_id (int):


def add(self, aspect, ifpresent="error"):

        # If aspect is a contextualization, it is trated separately
        if isinstance(aspect, contextualize):
            self.contextualize.update(aspect)
            return True

        classification = [(network, self.networks), (system, self.systems), (ansible, self.ansible_hosts),
                          (deploy, self.deploys), (configure, self.configures)]
        aspect_list = [l for t, l in classification if isinstance(aspect, t)]
        assert len(aspect_list) == 1, "Unexpected aspect for RADL."
        aspect_list = aspect_list[0]

        old_aspect = [a for a in aspect_list if a.getId() == aspect.getId()]
        if old_aspect:
            # If some aspect with the same id is found
            if ifpresent == "error":
                raise Exception("Aspect with the same id was found.")
            elif ifpresent == "replace":
                for i, elem in enumerate(aspect_list):
                    if elem.getId() == old_aspect[0].getId():
                        del aspect_list[i]
                        break
                aspect_list.append(aspect)
                return True
            elif ifpresent == "ignore":
                return False
            else:
                raise ValueError
        else:
            # Otherwise add aspect
            aspect_list.append(aspect)
            return True

Add a network, ansible_host, system, deploy, configure or contextualize.

Args:
    - aspect(network, system, deploy, configure or contextualize): thing to add.
    - ifpresent(str): if it has been defined, do:

        - ``"ignore"``: not add the aspect.
        - ``"replace"``: replace by the old defined.
        - ``"error"``: raise an error.

    Return(bool): True if aspect was added.


def delete_metadata(self, resource, keys):

        self.metadata_service.set_auth(self._token_metadata)
        self.metadata_service.delete(resource, keys)

Deletes the given key-value pairs associated with the given resource.

    Will attempt to delete all key-value pairs even if some fail.

Args:
        resource (intern.resource.boss.BossResource)
        keys (list)

Raises:
        HTTPErrorList on failure.


def full_url(self):

        if self.link == '':
            return None
        else:
            return Note._expand_url(self.link, self.subreddit)

Return the full reddit URL associated with the usernote.

Args:
        subreddit: the subreddit name for the note (PRAW Subreddit object)


def get_lambda_alias_arn(app, account, region):

    session = boto3.Session(profile_name=account, region_name=region)
    lambda_client = session.client('lambda')

    lambda_aliases = lambda_client.list_aliases(FunctionName=app)

    matched_alias = None
    for alias in lambda_aliases['Aliases']:
        if alias['Name'] == account:
            lambda_alias_arn = alias['AliasArn']
            LOG.info('Found ARN for alias %s for function %s', account, app)
            matched_alias = lambda_alias_arn
            break
    else:
        fatal_message = 'Lambda alias {0} of function {1} not found'.format(account, app)
        LOG.fatal(fatal_message)
        raise LambdaAliasDoesNotExist(fatal_message)
    return matched_alias

Get lambda alias ARN. Assumes that account name is equal to alias name.

Args:
        account (str): AWS account name.
        region (str): Region name, e.g. us-east-1
        app (str): Lambda function name

Returns:
        str: ARN for requested lambda alias


def getContextsForTerm(self, term, getFingerprint=None, startIndex=0, maxResults=5):

        return self._terms.getContextsForTerm(self._retina, term, getFingerprint, startIndex, maxResults)

Get the contexts for a given term

Args:
        term, str: A term in the retina (required)
        getFingerprint, bool: Configure if the fingerprint should be returned as part of the results (optional)
        startIndex, int: The start-index for pagination (optional)
        maxResults, int: Max results per page (optional)

Returns:
        list of Context

Raises:
        CorticalioException: if the request was not successful


def properties(self, var_or_nodeid, as_list=False):

        props = []
        if var_or_nodeid in self._vars:
            props = self._vars[var_or_nodeid]['props']
        elif var_or_nodeid in self._eps:
            var = self._eps[var_or_nodeid][3].get(IVARG_ROLE)
            props = self._vars.get(var, {}).get('props', [])
        else:
            raise KeyError(var_or_nodeid)
        if not as_list:
            props = dict(props)
        return props

Return a dictionary of variable properties for *var_or_nodeid*.

Args:
        var_or_nodeid: if a variable, return the properties
            associated with the variable; if a nodeid, return the
            properties associated with the intrinsic variable of the
            predication given by the nodeid


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

    print('Converting transposed convolution ...')

    if names == 'short':
        tf_name = 'C' + random_string(7)
    elif names == 'keep':
        tf_name = w_name
    else:
        tf_name = w_name + str(random.random())

    bias_name = '{0}.bias'.format(w_name)
    weights_name = '{0}.weight'.format(w_name)

    if len(weights[weights_name].numpy().shape) == 4:
        W = weights[weights_name].numpy().transpose(2, 3, 1, 0)
        height, width, n_filters, channels = W.shape

        n_groups = params['group']
        if n_groups > 1:
            raise AssertionError('Cannot convert conv1d with groups != 1')

        if params['dilations'][0] > 1:
            raise AssertionError('Cannot convert conv1d with dilation_rate != 1')

        if bias_name in weights:
            biases = weights[bias_name].numpy()
            has_bias = True
        else:
            biases = None
            has_bias = False

        input_name = inputs[0]

        if has_bias:
            weights = [W, biases]
        else:
            weights = [W]

        conv = keras.layers.Conv2DTranspose(
            filters=n_filters,
            kernel_size=(height, width),
            strides=(params['strides'][0], params['strides'][1]),
            padding='valid',
            output_padding=0,
            weights=weights,
            use_bias=has_bias,
            activation=None,
            dilation_rate=params['dilations'][0],
            bias_initializer='zeros', kernel_initializer='zeros',
            name=tf_name
        )

        layers[scope_name] = conv(layers[input_name])

        # Magic ad-hoc.
        # See the Keras issue: https://github.com/keras-team/keras/issues/6777
        layers[scope_name].set_shape(layers[scope_name]._keras_shape)

        pads = params['pads']
        if pads[0] > 0:
            assert(len(pads) == 2 or (pads[2] == pads[0] and pads[3] == pads[1]))

            crop = keras.layers.Cropping2D(
                pads[:2],
                name=tf_name + '_crop'
            )
            layers[scope_name] = crop(layers[scope_name])
    else:
        raise AssertionError('Layer is not supported for now')

Convert transposed convolution 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 _create_datadict(cls, internal_name):

        if internal_name == "LOCATION":
            return Location()
        if internal_name == "DESIGN CONDITIONS":
            return DesignConditions()
        if internal_name == "TYPICAL/EXTREME PERIODS":
            return TypicalOrExtremePeriods()
        if internal_name == "GROUND TEMPERATURES":
            return GroundTemperatures()
        if internal_name == "HOLIDAYS/DAYLIGHT SAVINGS":
            return HolidaysOrDaylightSavings()
        if internal_name == "COMMENTS 1":
            return Comments1()
        if internal_name == "COMMENTS 2":
            return Comments2()
        if internal_name == "DATA PERIODS":
            return DataPeriods()
        raise ValueError(
            "No DataDictionary known for {}".format(internal_name))

Creates an object depending on `internal_name`

Args:
        internal_name (str): IDD name

Raises:
        ValueError: if `internal_name` cannot be matched to a data dictionary object


def __init__(self, meter_id=Meter.OFPM_ALL):

        super().__init__()
        self.meter_id = meter_id

Create a MeterMultipartRequest with the optional parameters below.

Args:
        meter_id(Meter): Meter Indentify.The value Meter.OFPM_ALL is used
                        to refer to all Meters on the switch.


def assert_key_has_value(self, key, caller):

        assert key, ("key parameter must be specified.")
        self.assert_key_exists(key, caller)

        if self[key] is None:
            raise KeyInContextHasNoValueError(
                f"context['{key}'] must have a value for {caller}.")

Assert that context contains key which also has a value.

Args:
        key: validate this key exists in context AND has a value that isn't
            None.
        caller: string. calling function name - this used to construct
                error messages

Raises:
        KeyNotInContextError: Key doesn't exist
        KeyInContextHasNoValueError: context[key] is None
        AssertionError: if key is None


def DEFINE_alias(name, original_name, flag_values=FLAGS, module_name=None):  # pylint: disable=g-bad-name

  if original_name not in flag_values:
    raise UnrecognizedFlagError(original_name)
  flag = flag_values[original_name]

  class _Parser(ArgumentParser):

    def parse(self, argument):
      flag.parse(argument)
      return flag.value

  class _FlagAlias(Flag):

    @property
    def value(self):
      return flag.value

    @value.setter
    def value(self, value):
      flag.value = value

  help_msg = 'Alias for --%s.' % flag.name
  # If alias_name has been used, gflags.DuplicatedFlag will be raised.
  DEFINE_flag(_FlagAlias(_Parser(), flag.serializer, name, flag.default,
                         help_msg, boolean=flag.boolean),
              flag_values, module_name)

Defines an alias flag for an existing one.

Args:
        name: A string, name of the alias flag.
        original_name: A string, name of the original flag.
        flag_values: FlagValues object with which the flag will be registered.
        module_name: A string, the name of the module that defines this flag.

Raises:
        gflags.FlagError:
        UnrecognizedFlagError: if the referenced flag doesn't exist.
        DuplicateFlagError: if the alias name has been used by some existing flag.


def _buckets_nearly_equal(a_dist, b_dist):

    a_type, a_buckets = _detect_bucket_option(a_dist)
    b_type, b_buckets = _detect_bucket_option(b_dist)
    if a_type != b_type:
        return False
    elif a_type == u'linearBuckets':
        return _linear_buckets_nearly_equal(a_buckets, b_buckets)
    elif a_type == u'exponentialBuckets':
        return _exponential_buckets_nearly_equal(a_buckets, b_buckets)
    elif a_type == u'explicitBuckets':
        return _explicit_buckets_nearly_equal(a_buckets, b_buckets)
    else:
        return False

Determines whether two `Distributions` are nearly equal.

Args:
        a_dist (:class:`Distribution`): an instance
        b_dist (:class:`Distribution`): another instance

Returns:
        boolean: `True` if the two instances are approximately equal, otherwise
        False


def is_commutable(expr1, expr2, eps=0.00000001):

    return sum((x * x.conjugate()).real for x in commutator(expr1, expr2).coeffs()) < eps

Test whether expr1 and expr2 are commutable.

Args:
        expr1 (Expr, Term or Pauli operator): Pauli's expression.
        expr2 (Expr, Term or Pauli operator): Pauli's expression.
        eps (float, optional): Machine epsilon.
            If |[expr1, expr2]| < eps, consider it is commutable.

Returns:
        bool: if expr1 and expr2 are commutable, returns True, otherwise False.


def GetSoapXMLForComplexType(self, type_name, value):

    element = self.schema.get_element(
        '{%s}%s' % (self._namespace_override, type_name))
    result_element = self._element_maker(element.qname.localname)
    element_value = element(**value)
    element.type.render(result_element, element_value)
    data = lxml.etree.tostring(result_element).strip()
    return data

Return an XML string representing a SOAP complex type.

Args:
        type_name: The name of the type with namespace prefix if necessary.
        value: A python dictionary to hydrate the type instance with.

Returns:
        A string containing the SOAP XML for the type.


def nr_cases(self, snv_cases=None, sv_cases=None):

        query = {}

        if snv_cases:
            query = {'vcf_path': {'$exists':True}}
        if sv_cases:
            query = {'vcf_sv_path': {'$exists':True}}
        if snv_cases and sv_cases:
            query = None

        return self.db.case.count_documents(query)

Return the number of cases in the database

Args:
        snv_cases(bool): If only snv cases should be searched
        sv_cases(bool): If only snv cases should be searched

Returns:
        cases (Iterable(Case)): A iterable with mongo cases


def send(self, response):

        self._connection.connection.set('{}:{}'.format(SIGNAL_REDIS_PREFIX, response.uid),
                                        pickle.dumps(response))

Send a response back to the client that issued a request.

Args:
        response (Response): Reference to the response object that should be sent.


def _ParseFieldsMetadata(self, structure):

    fields = structure.fields.split(' ')

    log_line_structure = pyparsing.Empty()
    if fields[0] == 'date' and fields[1] == 'time':
      log_line_structure += self.DATE_TIME.setResultsName('date_time')
      fields = fields[2:]

    for member in fields:
      log_line_structure += self._LOG_LINE_STRUCTURES.get(member, self.URI)

    updated_structures = []
    for line_structure in self._line_structures:
      if line_structure[0] != 'logline':
        updated_structures.append(line_structure)
    updated_structures.append(('logline', log_line_structure))
    # TODO: self._line_structures is a work-around and this needs
    # a structural fix.
    self._line_structures = updated_structures

Parses the fields metadata and updates the log line definition to match.

Args:
        structure (pyparsing.ParseResults): structure parsed from the log file.


def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):

        super(RevokeRequestPayload, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        self.unique_identifier = attributes.UniqueIdentifier()
        self.unique_identifier.read(tstream, kmip_version=kmip_version)

        self.revocation_reason = objects.RevocationReason()
        self.revocation_reason.read(tstream, kmip_version=kmip_version)

        if self.is_tag_next(enums.Tags.COMPROMISE_OCCURRENCE_DATE, tstream):
            self.compromise_occurrence_date = primitives.DateTime(
                tag=enums.Tags.COMPROMISE_OCCURRENCE_DATE)
            self.compromise_occurrence_date.read(
                tstream,
                kmip_version=kmip_version
            )

        self.is_oversized(tstream)
        self.validate()

Read the data encoding the RevokeRequestPayload object and decode it
    into its constituent parts.

Args:
        istream (Stream): A data stream containing encoded object data,
            supporting a read method; usually a BytearrayStream object.
        kmip_version (KMIPVersion): An enumeration defining the KMIP
            version with which the object will be decoded. Optional,
            defaults to KMIP 1.0.


def __init__(self, data_type_definition):

    super(PaddingMap, self).__init__(data_type_definition)
    self.byte_size = None

Initializes a padding data type map.

Args:
        data_type_definition (DataTypeDefinition): data type definition.

Raises:
        FormatError: if the data type map cannot be determined from the data
        type definition.


def from_api_repr(cls, resource):

        version = resource.get("version")
        etag = resource.get("etag")
        policy = cls(etag, version)
        for binding in resource.get("bindings", ()):
            role = binding["role"]
            members = sorted(binding["members"])
            policy[role] = members
        return policy

Factory: create a policy from a JSON resource.

Args:
        resource (dict): policy resource returned by ``getIamPolicy`` API.

Returns:
        :class:`Policy`: the parsed policy


def _get_create_query(partition, tablename, include=None):

        TYPE_MAP = {
            'int': 'INTEGER',
            'float': 'REAL',
            six.binary_type.__name__: 'TEXT',
            six.text_type.__name__: 'TEXT',
            'date': 'DATE',
            'datetime': 'TIMESTAMP WITHOUT TIME ZONE'
        }
        columns_types = []
        if not include:
            include = []
        for column in sorted(partition.datafile.reader.columns, key=lambda x: x['pos']):
            if include and column['name'] not in include:
                continue
            sqlite_type = TYPE_MAP.get(column['type'])
            if not sqlite_type:
                raise Exception('Do not know how to convert {} to sql column.'.format(column['type']))
            columns_types.append('    "{}" {}'.format(column['name'], sqlite_type))
        columns_types_str = ',\n'.join(columns_types)
        query = 'CREATE TABLE IF NOT EXISTS {}(\n{})'.format(tablename, columns_types_str)
        return query

Creates and returns `CREATE TABLE ...` sql statement for given mprows.

Args:
        partition (orm.Partition):
        tablename (str): name of the table in the return create query.
        include (list of str, optional): list of columns to include to query.

Returns:
        str: create table query.


def record_value(self, value, count=1):

        if value < 0:
            return False
        counts_index = self._counts_index_for(value)
        if (counts_index < 0) or (self.counts_len <= counts_index):
            return False
        self.counts[counts_index] += count
        self.total_count += count
        self.min_value = min(self.min_value, value)
        self.max_value = max(self.max_value, value)
        return True

Record a new value into the histogram

Args:
        value: the value to record (must be in the valid range)
        count: incremental count (defaults to 1)


def vcf_records(self, format_tags=None, qualified=False):

        if qualified:
            sample_names = self.qualified_sample_names
        else:
            sample_names = self.sample_names

        for line in self._file_reader.read_lines():
            if line.startswith("#"):
                continue
            vcf_record = vcf.VcfRecord.parse_record(line, sample_names)
            if format_tags:
                vcf_record = self.modify_format_tag(vcf_record, format_tags)
            yield vcf_record

Generates parsed VcfRecord objects.

    Typically called in a for loop to process each vcf record in a
    VcfReader. VcfReader must be opened in advanced and closed when
    complete. Skips all headers.

Args:
        qualified: When True, sample names are prefixed with file name

Returns:
        Parsed VcfRecord

Raises:
        StopIteration: when reader is exhausted.
        TypeError: if reader is closed.


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 generate_data(self, data_dir, tmp_dir, task_id=-1):

    tf.logging.info("generate_data task_id=%s" % task_id)
    encoder = self.get_or_create_vocab(data_dir, tmp_dir)
    assert task_id >= 0 and task_id < self.num_generate_tasks
    if task_id < self.num_train_shards:
      out_file = self.training_filepaths(
          data_dir, self.num_train_shards, shuffled=False)[task_id]
    else:
      out_file = self.dev_filepaths(
          data_dir, self.num_dev_shards,
          shuffled=False)[task_id - self.num_train_shards]
    generator_utils.generate_files(
        self.example_generator(encoder, tmp_dir, task_id), [out_file])
    generator_utils.shuffle_dataset([out_file])

Generates training/dev data.

Args:
        data_dir: a string
        tmp_dir: a string
        task_id: an optional integer

Returns:
        shard or shards for which data was generated.


def from_input(cls, input, workdir=None, manager=None):

        return cls(input, workdir=workdir, manager=manager)

Create an instance of `AbinitTask` from an ABINIT input.

Args:
        ainput: `AbinitInput` object.
        workdir: Path to the working directory.
        manager: :class:`TaskManager` object.


def validate(self, *args, **kwargs): # pylint: disable=arguments-differ

        return super(ParameterValidator, self)._validate(*args, **kwargs)

Validate a parameter dict against a parameter schema from an ocrd-tool.json

Args:
        obj (dict):
        schema (dict):


Description:
    Top 3 alternatives 16 moment conditions values calculation
    Parameters:
    votes: ordinal preference data (numpy ndarray of integers)


def mock(self, slot, rpc_id, value):

        address = slot.address

        if address not in self.mock_rpcs:
            self.mock_rpcs[address] = {}

        self.mock_rpcs[address][rpc_id] = value

Store a mock return value for an RPC

Args:
        slot (SlotIdentifier): The slot we are mocking
        rpc_id (int): The rpc we are mocking
        value (int): The value that should be returned
            when the RPC is called.


def _ParseRecordLogline(self, parser_mediator, structure):

    date_time = dfdatetime_time_elements.TimeElementsInMilliseconds()

    try:
      datetime_iso8601 = self._GetISO8601String(structure.date_time)
      date_time.CopyFromStringISO8601(datetime_iso8601)
    except ValueError:
      parser_mediator.ProduceExtractionWarning(
          'invalid date time value: {0!s}'.format(structure.date_time))
      return

    event_data = GoogleDriveSyncLogEventData()
    event_data.log_level = structure.log_level
    event_data.pid = structure.pid
    event_data.thread = structure.thread
    event_data.source_code = structure.source_code
    # Replace newlines with spaces in structure.message to preserve output.
    event_data.message = structure.message.replace('\n', ' ')

    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_ADDED)

    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a logline record structure and produces events.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        structure (pyparsing.ParseResults): structure of tokens derived from
        a line of a text file.


def _kl_laplace_laplace(a, b, name=None):

  with tf.name_scope(name or "kl_laplace_laplace"):
    # Consistent with
    # http://www.mast.queensu.ca/~communications/Papers/gil-msc11.pdf, page 38
    distance = tf.abs(a.loc - b.loc)
    ratio = a.scale / b.scale

    return (-tf.math.log(ratio) - 1 + distance / b.scale +
            ratio * tf.exp(-distance / a.scale))

Calculate the batched KL divergence KL(a || b) with a and b Laplace.

Args:
        a: instance of a Laplace distribution object.
        b: instance of a Laplace distribution object.
        name: (optional) Name to use for created operations.
        default is "kl_laplace_laplace".

Returns:
        Batchwise KL(a || b)