def _calculate_aggregation_loss(logits_aggregation, aggregate_mask, aggregation_labels, use_answer_as_supervision, num_aggregation_labels, aggregation_loss_weight):
    per_example_aggregation_loss = _calculate_aggregation_loss_known(logits_aggregation, aggregate_mask, aggregation_labels, use_answer_as_supervision, num_aggregation_labels)
    if use_answer_as_supervision:
        per_example_aggregation_loss += _calculate_aggregation_loss_unknown(logits_aggregation, aggregate_mask)
    return aggregation_loss_weight * per_example_aggregation_loss

Calculates the aggregation loss per example.

Args:
logits_aggregation (`tf.Tensor` of shape `(batch_size, num_aggregation_labels)`):
Logits per aggregation operation.
aggregate_mask (`tf.Tensor` of shape `(batch_size, )`):
A mask set to 1 for examples that should use aggregation functions.
aggregation_labels (`tf.Tensor` of shape `(batch_size, )`):
Aggregation function id for every example in the batch.
use_answer_as_supervision (`bool`, *optional*):
Whether to use the answer as the only supervision for aggregation examples.
num_aggregation_labels (`int`, *optional*, defaults to 0):
The number of aggregation operators to predict.
aggregation_loss_weight (`float`, *optional*, defaults to 1.0):
Importance weight for the aggregation loss.

Returns:
aggregation_loss (`tf.Tensor` of shape `(batch_size,)`): Aggregation loss per example.


def _MakeEnumValueDescriptor(self, value_proto, index):

    return descriptor.EnumValueDescriptor(
        name=value_proto.name,
        index=index,
        number=value_proto.number,
        options=_OptionsOrNone(value_proto),
        type=None)

Creates a enum value descriptor object from a enum value proto.

Args:
value_proto: The proto describing the enum value.
index: The index of the enum value.

Returns:
An initialized EnumValueDescriptor object.


def __init__(self, length=None, group_type=None, group_id=None,
                 buckets=None):

        super().__init__()
        self.length = length
        self.group_type = group_type
        self.group_id = group_id
        self.buckets = buckets

Create a GroupDescStats with the optional parameters below.

Args:
length (int): Length of this entry.
group_type (|GroupType_v0x04|): One of OFPGT_*.
group_id (int): Group identifier.
buckets (|ListOfBuckets_v0x04|): List of buckets in group.


def convert_args_to_laid_out_tensors(xs):
    ret = []
    for x in xs:
        if hasattr(x, 'to_laid_out_tensor'):
            ret.append(x.to_laid_out_tensor())
        else:
            ret.append(x)
    return ret

Convert list elements to laid-out-tensors when possible.

Args:
xs: a list
Returns:
a list


def __init__(self, latitude, longitude, name):

        super(Waypoint, self).__init__(latitude, longitude)
        self.name = name.upper()

Initialise a new ``Waypoint`` object.

Args:
latitude (float): Waypoint's latitude
longitude (float): Waypoint's longitude
name (str): Comment for waypoint


def pool2d(x, pool_size, strides=(1, 1), padding='valid', data_format=None, pool_mode='max'):
    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))
    if len(pool_size) != 2:
        raise ValueError('`pool_size` must be a tuple of 2 integers.')
    if len(strides) != 2:
        raise ValueError('`strides` must be a tuple of 2 integers.')
    x, tf_data_format = _preprocess_conv2d_input(x, data_format)
    padding = _preprocess_padding(padding)
    if tf_data_format == 'NHWC':
        strides = (1,) + strides + (1,)
        pool_size = (1,) + pool_size + (1,)
    else:
        strides = (1, 1) + strides
        pool_size = (1, 1) + pool_size
    if pool_mode == 'max':
        x = nn.max_pool(x, pool_size, strides, padding=padding, data_format=tf_data_format)
    elif pool_mode == 'avg':
        x = nn.avg_pool(x, pool_size, strides, padding=padding, data_format=tf_data_format)
    else:
        raise ValueError('Invalid pooling mode: ' + str(pool_mode))
    if data_format == 'channels_first' and tf_data_format == 'NHWC':
        x = array_ops.transpose(x, (0, 3, 1, 2))
    return x

2D Pooling.

Args:
x: Tensor or variable.
pool_size: tuple of 2 integers.
strides: tuple of 2 integers.
padding: string, `"same"` or `"valid"`.
data_format: string, `"channels_last"` or `"channels_first"`.
pool_mode: string, `"max"` or `"avg"`.

Returns:
A tensor, result of 2D pooling.

Raises:
ValueError: if `data_format` is neither `"channels_last"` or
`"channels_first"`.
ValueError: if `pool_size` is not a tuple of 2 integers.
ValueError: if `strides` is not a tuple of 2 integers.
ValueError: if `pool_mode` is neither `"max"` or `"avg"`.


def import_user_module():
    return importlib.import_module(FLAGS.wrapped_tpu_test_module_relative, calculate_parent_python_path(sys.argv[0]))

Imports the flag-specified user test code.

This runs all top-level statements in the user module, specifically flag
definitions.

Returns:
The user test module.


def _new_convolution(self, use_bias):

    def clean_dict(input_dict):
        if (input_dict and (not use_bias)):
            cleaned_dict = input_dict.copy()
            cleaned_dict.pop('b', None)
            return cleaned_dict
        return input_dict
    return self._conv_class(output_channels=(4 * self._output_channels), kernel_shape=self._kernel_shape, stride=self._stride, rate=self._rate, padding=self._padding, use_bias=use_bias, initializers=clean_dict(self._initializers), partitioners=clean_dict(self._partitioners), regularizers=clean_dict(self._regularizers), name='conv')

Returns new convolution.

Args:
use_bias: Use bias in convolutions. If False, clean_dict removes bias
entries from initializers, partitioners and regularizers passed to
the constructor of the convolution.


def simplify_U(theta, phi, lam):
    gate = U3Gate(theta, phi, lam)
    if (abs((gate.params[0] % (2.0 * math.pi))) < _CUTOFF_PRECISION):
        gate = U1Gate(((gate.params[0] + gate.params[1]) + gate.params[2]))
    if isinstance(gate, U3Gate):
        if (abs(((gate.params[0] - (math.pi / 2)) % (2.0 * math.pi))) < _CUTOFF_PRECISION):
            gate = U2Gate(gate.params[1], (gate.params[2] + (gate.params[0] - (math.pi / 2))))
        if (abs(((gate.params[0] + (math.pi / 2)) % (2.0 * math.pi))) < _CUTOFF_PRECISION):
            gate = U2Gate((gate.params[1] + math.pi), ((gate.params[2] - math.pi) + (gate.params[0] + (math.pi / 2))))
    if (isinstance(gate, U1Gate) and (abs((gate.params[0] % (4.0 * math.pi))) < _CUTOFF_PRECISION)):
        gate = IdGate()
    return gate

Return the gate u1, u2, or u3 implementing U with the fewest pulses.

The returned gate implements U exactly, not up to a global phase.

Args:
theta, phi, lam: input Euler rotation angles for a general U gate

Returns:
Gate: one of IdGate, U1Gate, U2Gate, U3Gate.


def extraterrestrial_direct_normal_radiation(self, value=9999.0):
    if (value is not None):
        try:
            value = float(value)
        except ValueError:
            raise ValueError('value {} need to be of type float for field `extraterrestrial_direct_normal_radiation`'.format(value))
        if (value < 0.0):
            raise ValueError('value need to be greater or equal 0.0 for field `extraterrestrial_direct_normal_radiation`')
    self._extraterrestrial_direct_normal_radiation = value

Corresponds to IDD Field `extraterrestrial_direct_normal_radiation`

Args:
value (float): value for IDD Field `extraterrestrial_direct_normal_radiation`
Unit: Wh/m2
value >= 0.0
Missing value: 9999.0
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def get_answers_for_student(student_item):

    submissions = sub_api.get_submissions(student_item)
    if not submissions:
        return Answers()

    latest_submission = submissions[0]
    latest_answer_item = latest_submission.get('answer', {})
    return Answers(latest_answer_item.get(ANSWER_LIST_KEY, []))

Retrieve answers from backend for a student and question

Args:
student_item (dict): The location of the problem this submission is
associated with, as defined by a course, student, and item.

Returns:
Answers: answers for the student


def populate_audit_fields(self, event):
    event.updated = self._data
    event.original = self.get_original()._data

Populates the the audit JSON fields with raw data from the model, so
all changes can be tracked and diffed.

Args:
event (Event): The Event instance to attach the data to
instance (fleaker.db.Model): The newly created/updated model


def createNote(self, title=None, text=None):

        node = _node.Note()
        if title is not None:
            node.title = title
        if text is not None:
            node.text = text
        self.add(node)
        return node

Create a new managed note. Any changes to the note will be uploaded when :py:meth:`sync` is called.

Args:
title (str): The title of the note.
text (str): The text of the note.

Returns:
gkeepapi.node.List: The new note.


def skip_magic(code_line, magic_list):
    for magic in magic_list:
        if code_line.startswith(magic):
            return True
    return False

Checks if the cell has magic, that is not Python-based.

Args:
code_line: A line of Python code
magic_list: A list of jupyter "magic" exceptions

Returns:
If the line jupyter "magic" line, not Python line

>>> skip_magic('!ls -laF', ['%', '!', '?'])
True


def _validate_type(self, properties_spec, value):

        if 'type' not in properties_spec.keys():

            def_name = self.get_definition_name_from_ref(properties_spec['$ref'])
            return self.validate_definition(def_name, value)

        elif properties_spec['type'] == 'array':
            if not isinstance(value, list):
                return False

            if ('type' in properties_spec['items'].keys() and
                    any(not self.check_type(item, properties_spec['items']['type']) for item in value)):
                return False

            elif ('$ref' in properties_spec['items'].keys()):
                def_name = self.get_definition_name_from_ref(properties_spec['items']['$ref'])
                if any(not self.validate_definition(def_name, item) for item in value):
                    return False

        else:
            if not self.check_type(value, properties_spec['type']):
                return False

        return True

Validate the given value with the given property spec.

Args:
properties_dict: specification of the property to check (From definition not route).
value: value to check.

Returns:
True if the value is valid for the given spec.


def exists(self, file_path, check_link=False):
    if (check_link and self.islink(file_path)):
        return True
    file_path = make_string_path(file_path)
    if (file_path is None):
        raise TypeError
    if (not file_path):
        return False
    if (file_path == self.dev_null.name):
        return (not self.is_windows_fs)
    try:
        if self.is_filepath_ending_with_separator(file_path):
            return False
        file_path = self.resolve_path(file_path)
    except (IOError, OSError):
        return False
    if (file_path == self.root.name):
        return True
    path_components = self._path_components(file_path)
    current_dir = self.root
    for component in path_components:
        current_dir = self._directory_content(current_dir, component)[1]
        if (not current_dir):
            return False
    return True

Return true if a path points to an existing file system object.

Args:
file_path:  The path to examine.

Returns:
(bool) True if the corresponding object exists.

Raises:
TypeError: if file_path is None.


def make_worksheet(self, sheet_name=None):
    if (sheet_name is None):
        sheet_name = self.table_name
    if (not sheet_name):
        sheet_name = ''
    self._stream = self.workbook.add_worksheet(sheet_name)
    self._current_data_row = self._first_data_row

Make a worksheet to the current workbook.

Args:
sheet_name (str):
Name of the worksheet to create. The name will be automatically generated
(like ``"Sheet1"``) if the ``sheet_name`` is empty.


def maybe_load_initial_epoch_from_ckpt(self, initial_epoch, mode):
    epoch = backend.eval(self._ckpt_saved_epoch)
    if mode == mode_keys.ModeKeys.TRAIN and epoch >= 0:
        return epoch + 1
    return initial_epoch

Maybe load initial epoch from ckpt considering possible worker recovery.

When `_ckpt_saved_epoch` attribute exists and is not
`CKPT_SAVED_EPOCH_UNUSED_VALUE`, this is under multi-worker training setting
and indicates the worker is recovering from previous failure. In this case,
infer `initial_epoch` from `self._ckpt_saved_epoch` to continue previous
unfinished training from certain epoch.

Args:
initial_epoch: The original initial_epoch user passes in in `fit()`.
mode: The mode for running `model.fit()`.

Returns:
If the training is recovering from previous failure under multi-worker
training setting, return the epoch the training is supposed to continue
at. Otherwise, return the `initial_epoch` the user passes in.


def remove(self, future):

        if self._loop.get_debug():
            logger.debug("Removing %s from the linked list.", future)
        if future.prev is None:
            assert future is self.head
            self.head = future.next
            if self.head is None:
                self.tail = None
                if not self.cancelled():
                    self.set_result(None)
            else:
                self.head.prev = None
        elif future.next is None:
            assert future is self.tail
            self.tail = future.prev
            if self.tail is None:
                self.head = None
                if not self.cancelled():
                    self.set_result(None)
            else:
                self.tail.prev = None

Remove an object from the linked list.

Args:
future (PlasmaObjectFuture): A PlasmaObjectFuture instance.


def filter_spent_outputs(self, outputs):
    links = [o.to_dict() for o in outputs]
    txs = list(query.get_spending_transactions(self.connection, links))
    spends = {TransactionLink.from_dict(input_['fulfills']) for tx in txs for input_ in tx['inputs']}
    return [ff for ff in outputs if (ff not in spends)]

Remove outputs that have been spent

Args:
outputs: list of TransactionLink


class AriaTextDecoderLayer(GradientCheckpointingLayer):

    def __init__(self, config: AriaTextConfig, layer_idx: int):
        super().__init__()
        self.hidden_size = config.hidden_size
        self.self_attn = AriaTextAttention(config=config, layer_idx=layer_idx)
        self.mlp = AriaTextMoELayer(config)
        self.input_layernorm = AriaTextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.post_attention_layernorm = AriaTextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)

    def forward(self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor]=None, position_ids: Optional[torch.LongTensor]=None, past_key_value: Optional[Cache]=None, output_attentions: Optional[bool]=False, use_cache: Optional[bool]=False, cache_position: Optional[torch.LongTensor]=None, position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, **kwargs: Unpack[FlashAttentionKwargs]) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
        residual = hidden_states
        hidden_states = self.input_layernorm(hidden_states)
        hidden_states, self_attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, cache_position=cache_position, position_embeddings=position_embeddings, **kwargs)
        hidden_states = residual + hidden_states
        residual = hidden_states
        hidden_states = self.post_attention_layernorm(hidden_states)
        hidden_states = self.mlp(hidden_states)
        hidden_states = residual + hidden_states
        outputs = (hidden_states,)
        if output_attentions:
            outputs += (self_attn_weights,)
        return outputs

Aria Text Decoder Layer.

This class defines a single decoder layer in the language model, incorporating self-attention and Mixture of Experts (MoE) feed-forward network.

Args:
config (`AriaTextConfig`):
Configuration object for the text component of the model.
layer_idx (`int`):
Index of the layer.


def get_gradient_components(self, value):
    raise NotImplementedError(f'{type(self).__name__}.get_gradient_components()')

Returns the components of `value` that should be included in gradients.

This method may not call TensorFlow ops, since any new ops added to the
graph would not be properly tracked by the gradient mechanisms.

Args:
value: A `CompositeTensor` value.

Returns:
A nested structure of `Tensor` or `IndexedSlices`.


def print_map(map_source, x, y, zoom=14, width=297, height=210, dpi=300, format='pdf'):
    bbox = get_print_bbox(x, y, zoom, width, height, dpi)
    tiles = [get_tiles(tile_layer, bbox) for tile_layer in map_source.layers if (tile_layer.min_zoom <= zoom <= tile_layer.max_zoom)]
    img = stitch_map(tiles, width, height, bbox, dpi)
    outfile = NamedTemporaryFile(delete=False)
    img.save(outfile, format, quality=100, dpi=(dpi, dpi))
    outfile.close()
    return outfile.name

Download map tiles and stitch them together in a single image, ready for printing.

Args:
map_source (MapSource): Map to download
x (float): map center x-coordinate in Mercator projection (EPSG:4326)
y (float): map center y-coordinate in Mercator projection (EPSG:4326)
zoom (int): tile zoom level to use for printing
width (float): page width in mm
height (float): page height in mm
dpi (int): resolution in dots per inch
format (str): output format. Anything supported by ``Pillow.Image.save``. E.g. "pdf", "jpeg", "png".

Returns:
str: path of temporary output file.


def GetSshkeyMap(self, since=None):
    return SshkeyUpdateGetter().GetUpdates(self._GetClient(), self.conf['bucket'], self.conf['sshkey_object'], since)

Return the ssh map from this source.

Args:
since: Get data only changed since this timestamp (inclusive) or None
for all data.

Returns:
instance of shadow.SSHMap


def case_report_content(store, institute_obj, case_obj):
    variant_types = {'causatives_detailed': 'causatives', 'suspects_detailed': 'suspects', 'classified_detailed': 'acmg_classification', 'tagged_detailed': 'manual_rank', 'dismissed_detailed': 'dismiss_variant', 'commented_detailed': 'is_commented'}
    data = case_obj
    for individual in data['individuals']:
        try:
            sex = int(individual.get('sex', 0))
        except ValueError as err:
            sex = 0
        individual['sex_human'] = SEX_MAP[sex]
        individual['phenotype_human'] = PHENOTYPE_MAP.get(individual['phenotype'])
    data['comments'] = store.events(institute_obj, case=case_obj, comments=True)
    data['manual_rank_options'] = MANUAL_RANK_OPTIONS
    data['dismissed_options'] = DISMISS_VARIANT_OPTIONS
    data['genetic_models'] = dict(GENETIC_MODELS)
    data['report_created_at'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
    evaluated_variants = {}
    for vt in variant_types:
        evaluated_variants[vt] = []
    for var_type in ['causatives', 'suspects']:
        vt = '_'.join([var_type, 'detailed'])
        for var_id in case_obj.get(var_type, []):
            variant_obj = store.variant(var_id)
            if (not variant_obj):
                continue
            evaluated_variants[vt].append(variant_obj)
    for var_obj in store.evaluated_variants(case_id=case_obj['_id']):
        for vt in variant_types:
            keyword = variant_types[vt]
            if (keyword in var_obj):
                evaluated_variants[vt].append(var_obj)
    for var_type in evaluated_variants:
        decorated_variants = []
        for var_obj in evaluated_variants[var_type]:
            if (var_obj['category'] == 'snv'):
                decorated_info = variant_decorator(store=store, institute_obj=institute_obj, case_obj=case_obj, variant_id=None, variant_obj=var_obj, add_case=False, add_other=False, get_overlapping=False)
            else:
                decorated_info = sv_variant(store=store, institute_id=institute_obj['_id'], case_name=case_obj['display_name'], variant_obj=var_obj, add_case=False, get_overlapping=False)
            decorated_variants.append(decorated_info['variant'])
        data[var_type] = decorated_variants
    return data

Gather contents to be visualized in a case report

Args:
store(adapter.MongoAdapter)
institute_obj(models.Institute)
case_obj(models.Case)

Returns:
data(dict)


def AddArguments(cls, argument_group):

    argument_group.add_argument(
        '--disable_zeromq', '--disable-zeromq', action='store_false',
        dest='use_zeromq', default=True, help=(
            'Disable queueing using ZeroMQ. A Multiprocessing queue will be '
            'used instead.'))

Adds command line arguments to an argument group.

This function takes an argument parser or an argument group object and adds
to it all the command line arguments this helper supports.

Args:
argument_group (argparse._ArgumentGroup|argparse.ArgumentParser):
argparse group.


def sticky_attribute_assignment(trackable, name, value):
    if isinstance(value, (tracking.TrackedList, tracking.TrackedDict, tracking.TrackedSet)) and hasattr(trackable, '_tracked'):
        trackable._tracked.append(name)
    if not tracking.is_tracking_enabled():
        return value
    if isinstance(value, tf.__internal__.tracking.Trackable):
        trackable._track_trackable(value, name=name, overwrite=True)
    return value

Adds dependencies, called from __setattr__.

Args:
trackable: The object to add dependencies to (generally the one having
an attribute assigned).
name: The attribute name being assigned.
value: The value being assigned. Not necessarily a trackable object.

Returns:
The value which should be stored in the attribute.


def get_values_and_permissible(values: Iterable[Tuple[(Any, str)]], add_none: bool=False, none_description: str='[None]') -> Tuple[(List[Tuple[(Any, str)]], List[Any])]:
    permissible_values = list((x[0] for x in values))
    if add_none:
        none_tuple = (SERIALIZED_NONE, none_description)
        values = ([none_tuple] + list(values))
    return (values, permissible_values)

Used when building Colander nodes.

Args:
values: an iterable of tuples like ``(value, description)`` used in
HTML forms

add_none: add a tuple ``(None, none_description)`` at the start of
``values`` in the result?

none_description: the description used for ``None`` if ``add_none``
is set

Returns:
a tuple ``(values, permissible_values)``, where

- ``values`` is what was passed in (perhaps with the addition of the
"None" tuple at the start)
- ``permissible_values`` is a list of all the ``value`` elements of
the original ``values``


def convert_upsample(params, w_name, scope_name, inputs, layers, weights, names):
    print('Converting upsample...')
    if (params['mode'] != 'nearest'):
        raise AssertionError('Cannot convert non-nearest upsampling')
    if (names == 'short'):
        tf_name = ('UPSL' + random_string(4))
    elif (names == 'keep'):
        tf_name = w_name
    else:
        tf_name = (w_name + str(random.random()))
    if ('height_scale' in params):
        scale = (params['height_scale'], params['width_scale'])
    elif (len(inputs) == 2):
        scale = layers[(inputs[(- 1)] + '_np')][(- 2):]
    upsampling = keras.layers.UpSampling2D(size=scale, name=tf_name)
    layers[scope_name] = upsampling(layers[inputs[0]])

Convert nearest upsampling 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 parse_outputtrans(path_dir):
    run_type = None
    warning = None
    efermi = None
    gap = None
    doping_levels = []
    with open(os.path.join(path_dir, 'boltztrap.outputtrans'), 'r') as f:
        for line in f:
            if ('WARNING' in line):
                warning = line
            elif ('Calc type:' in line):
                run_type = line.split()[(- 1)]
            elif line.startswith('VBM'):
                efermi = Energy(line.split()[1], 'Ry').to('eV')
            elif line.startswith('Egap:'):
                gap = Energy(float(line.split()[1]), 'Ry').to('eV')
            elif line.startswith('Doping level number'):
                doping_levels.append(float(line.split()[6]))
    return (run_type, warning, efermi, gap, doping_levels)

Parses .outputtrans file

Args:
path_dir: dir containing boltztrap.outputtrans

Returns:
tuple - (run_type, warning, efermi, gap, doping_levels)


def _Open(self, path_spec=None, mode='rb'):

    if not path_spec:
      raise ValueError('Missing path specification.')

    store_index = vshadow.VShadowPathSpecGetStoreIndex(path_spec)
    if store_index is None:
      raise errors.PathSpecError(
          'Unable to retrieve store index from path specification.')

    self._file_system = resolver.Resolver.OpenFileSystem(
        path_spec, resolver_context=self._resolver_context)
    vshadow_volume = self._file_system.GetVShadowVolume()

    if (store_index < 0 or
        store_index >= vshadow_volume.number_of_stores):
      raise errors.PathSpecError((
          'Unable to retrieve VSS store: {0:d} from path '
          'specification.').format(store_index))

    vshadow_store = vshadow_volume.get_store(store_index)
    if not vshadow_store.has_in_volume_data():
      raise IOError((
          'Unable to open VSS store: {0:d} without in-volume stored '
          'data.').format(store_index))

    self._vshadow_store = vshadow_store

Opens the file-like object defined by path specification.

Args:
path_spec (PathSpec): path specification.
mode (Optional[str]): file access mode.

Raises:
AccessError: if the access to open the file was denied.
IOError: if the file-like object could not be opened.
OSError: if the file-like object could not be opened.
PathSpecError: if the path specification is incorrect.
ValueError: if the path specification is invalid.


def unflatten(guide, falttened_input):
    return [(unflatten(sub_list, falttened_input) if isinstance(sub_list, list) else next(falttened_input)) for sub_list in guide]

Unflatten a falttened generator.

Args:
guide: A guide list to follow the structure
falttened_input: A flattened iterator object

Usage:

guide = [["a"], ["b","c","d"], [["e"]], ["f"]]
input_list = [0, 1, 2, 3, 4, 5, 6, 7]
unflatten(guide, iter(input_list))
>> [[0], [1, 2, 3], [[4]], [5]]


def _patch_expand_path(self, settings, name, value):

        if os.path.isabs(value):
            return os.path.normpath(value)

        value = os.path.expanduser(value)

        if not os.path.isabs(value) and self.projectdir:
            value = os.path.join(self.projectdir, value)

        return os.path.normpath(value)

Patch a path to expand home directory and make absolute path.

Args:
settings (dict): Current settings.
name (str): Setting name.
value (str): Path to patch.

Returns:
str: Patched path to an absolute path.


def __init__(self, map_name, timestamp_dir, cache_options, automount_mountpoint=None):
    updater.Updater.__init__(self, map_name, timestamp_dir, cache_options, automount_mountpoint)
    self.local_master = False
    if self.OPT_LOCAL_MASTER in cache_options:
        if cache_options[self.OPT_LOCAL_MASTER] == 'yes':
            self.local_master = True

Initialize automount-specific updater options.

Args:
map_name: A string representing the type of the map we are an Updater for.
timestamp_dir: A string with the directory containing our timestamp files.
cache_options: A dict containing the options for any caches we create.
automount_mountpoint: An optional string containing automount path info.


def _fact_to_tuple(self, fact):
    if fact.category:
        category = fact.category.name
    else:
        category = ''
    description = (fact.description or '')
    return FactTuple(start=fact.start.strftime(self.datetime_format), end=fact.end.strftime(self.datetime_format), activity=text_type(fact.activity.name), duration=fact.get_string_delta(format='%M'), category=text_type(category), description=text_type(description))

Convert a ``Fact`` to its normalized tuple.

This is where all type conversion for ``Fact`` attributes to strings as
well as any normalization happens.

Note:
Because different writers may require different types, we need to
do this individually.

Args:
fact (hamster_lib.Fact): Fact to be converted.

Returns:
FactTuple: Tuple representing the original ``Fact``.


def zip_ll(data, means, M):
    (genes, cells) = data.shape
    clusters = means.shape[1]
    ll = np.zeros((cells, clusters))
    d0 = (data == 0)
    d1 = (data > 0)
    for i in range(clusters):
        means_i = np.tile(means[(:, i)], (cells, 1))
        means_i = means_i.transpose()
        L_i = np.tile(M[(:, i)], (cells, 1))
        L_i = L_i.transpose()
        ll_0 = np.log((L_i + ((1 - L_i) * np.exp((- means_i)))))
        ll_0 = np.where(((L_i == 0) & (means_i == 0)), (- means_i), ll_0)
        ll_1 = ((np.log((1 - L_i)) + xlogy(data, means_i)) - means_i)
        ll_0 = np.where(d0, ll_0, 0.0)
        ll_1 = np.where(d1, ll_1, 0.0)
        ll[(:, i)] = np.sum((ll_0 + ll_1), 0)
    return ll

Calculates the zero-inflated Poisson log-likelihood.

Args:
data (array): genes x cells
means (array): genes x k
M (array): genes x k - this is the zero-inflation parameter.

Returns:
cells x k array of log-likelihood for each cell/cluster pair.


def remove_item(self, val):

        return cache.lrem(self.key, json.dumps(val))

Removes given item from the list.

Args:
val: Item

Returns:
Cache backend response.


def watch(self, key, pipeline=False):

        if pipeline:
            self._pipeline.watch(key)
        else:
            self._db.watch(key)

Watch the given key.

Marks the given key to be watch for conditional execution
of a transaction.

Args:
key (str): Key that needs to be watched
pipeline (bool): True, start a transaction block. Default false.


async def teardown_client(self, client_id):

        client_info = self._client_info(client_id)

        self.adapter.remove_monitor(client_info['monitor'])
        conns = client_info['connections']

        for conn_string, conn_id in conns.items():
            try:
                self._logger.debug("Disconnecting client %s from conn %s at teardown", client_id, conn_string)
                await self.adapter.disconnect(conn_id)
            except:
                self._logger.exception("Error disconnecting device during teardown_client: conn_string=%s", conn_string)

        del self._clients[client_id]

Release all resources held by a client.

This method must be called and awaited whenever a client is
disconnected.  It ensures that all of the client's resources are
properly released and any devices they have connected to are
disconnected cleanly.

Args:
client_id (str): The client that we should tear down.

Raises:
ArgumentError: The client_id is unknown.


def call(func, args):
    assert hasattr(func, '__call__'), 'Cannot call func: {}'.format(func.__name__)
    raw_func = (func if isinstance(func, FunctionType) else func.__class__.__call__)
    hints = collections.defaultdict((lambda : Any), get_type_hints(raw_func))
    argspec = _getargspec(raw_func)
    named_args = {}
    varargs = ()
    for (k, nk, v) in _normalize(args):
        if (nk == argspec.varargs):
            hints[nk] = Tuple[(hints[nk], ...)]
        elif ((nk not in argspec.args) and (argspec.varkw in hints)):
            hints[nk] = hints[argspec.varkw]
        try:
            value = cast(hints[nk], v)
        except TypeError as e:
            _LOGGER.exception(e)
            six.raise_from(exc.InvalidCliValueError(k, v), e)
        if (nk == argspec.varargs):
            varargs = value
        elif (((nk in argspec.args) or argspec.varkw) and ((nk not in named_args) or (named_args[nk] is None))):
            named_args[nk] = value
    return func(*varargs, **named_args)

Call the function with args normalized and cast to the correct types.

Args:
func: The function to call.
args: The arguments parsed by docopt.

Returns:
The return value of func.


def setup_colorbars(self, plot_call_sign):
    self.fig.colorbar(plot_call_sign, cax=self.cbar_ax, ticks=self.cbar_ticks, orientation=self.cbar_orientation)
    getattr(self.cbar_ax, (('set_' + self.cbar_var) + 'ticklabels'))(self.cbar_tick_labels, fontsize=self.cbar_ticks_fontsize)
    getattr(self.cbar_ax, (('set_' + self.cbar_var) + 'label'))(self.cbar_label, fontsize=self.cbar_label_fontsize, labelpad=self.cbar_label_pad)
    return

Setup colorbars for each type of plot.

Take all of the optional performed during ``__init__`` method and makes the colorbar.

Args:
plot_call_sign (obj): Plot instance of ax.contourf with colormapping to
add as a colorbar.


def convert_response(check_response, project_id):

    if not check_response or not check_response.checkErrors:
        return _IS_OK

    theError = check_response.checkErrors[0]
    error_tuple = _CHECK_ERROR_CONVERSION.get(theError.code, _IS_UNKNOWN)
    if error_tuple[1].find(u'{') == -1:
        return error_tuple

    updated_msg = error_tuple[1].format(project_id=project_id, detail=theError.detail or u'')
    return error_tuple[0], updated_msg, error_tuple[2]

Computes a http status code and message `CheckResponse`

The return value a tuple (code, message, api_key_is_bad) where

code: is the http status code
message: is the message to return
api_key_is_bad: indicates that a given api_key is bad

Args:
check_response (:class:`endpoints_management.gen.servicecontrol_v1_messages.CheckResponse`):
the response from calling an api

Returns:
tuple(code, message, bool)


def xmlrpc_notify(self, app_id, token_or_token_list, aps_dict_or_list):

    d = self.apns_service(app_id).write(
      encode_notifications(
        [t.replace(' ', '') for t in token_or_token_list]
          if (type(token_or_token_list) is list)
          else token_or_token_list.replace(' ', ''),
        aps_dict_or_list))
    if d:
      def _finish_err(r):

        raise xmlrpc.Fault(500, 'Connection to the APNS server could not be made.')
      return d.addCallbacks(lambda r: None, _finish_err)

Sends push notifications to the Apple APNS server. Multiple
notifications can be sent by sending pairing the token/notification
arguments in lists [token1, token2], [notification1, notification2].

Arguments:
app_id                provisioned app_id to send to
token_or_token_list   token to send the notification or a list of tokens
aps_dict_or_list      notification dicts or a list of notifications
Returns:
None


def set_internal_tacking_values(self,
                                    min_non_zero_index,
                                    max_index,
                                    total_added):

        if max_index >= 0:
            self.max_value = self.get_highest_equivalent_value(self.get_value_from_index(max_index))
        if min_non_zero_index >= 0:
            self.min_value = self.get_value_from_index(min_non_zero_index)
        self.total_count = total_added

Called during decoding and add to adjust the new min/max value and
total count

Args:
min_non_zero_index min nonzero index of all added counts (-1 if none)
max_index max index of all added counts (-1 if none)


def _are_scopes_sufficient(authorized_scopes, sufficient_scopes):

  for sufficient_scope_set in sufficient_scopes:
    if sufficient_scope_set.issubset(authorized_scopes):
      return True
  return False

Check if a list of authorized scopes satisfies any set of sufficient scopes.

Args:
authorized_scopes: a list of strings, return value from oauth.get_authorized_scopes
sufficient_scopes: a set of sets of strings, return value from _process_scopes


def get(self, key):
    lock.acquire()
    try:
        if (key not in self):
            return None
        current_time = time.time()
        if (self[key].expire > current_time):
            return self[key].value
        deletes = []
        for (k, val) in self.items():
            if (val.expire <= current_time):
                deletes.append(k)
        for k in deletes:
            del self[k]
        return None
    finally:
        lock.release()

Get an object from the cache

Arguments:
key (str): Cache key

Returns:
Cached object


def post_process_object_detection(self, outputs, threshold: float=0.5, target_sizes: Union[TensorType, List[Tuple]]=None, nms_threshold: float=0.7):
    out_logits, out_bbox = (outputs.logits, outputs.pred_boxes)
    batch_size, num_queries, num_labels = out_logits.shape
    if target_sizes is not None:
        if len(out_logits) != len(target_sizes):
            raise ValueError('Make sure that you pass in as many target sizes as the batch dimension of the logits')
    prob = out_logits.sigmoid()
    all_scores = prob.view(batch_size, num_queries * num_labels).to(out_logits.device)
    all_indexes = torch.arange(num_queries * num_labels)[None].repeat(batch_size, 1).to(out_logits.device)
    all_boxes = torch.div(all_indexes, out_logits.shape[2], rounding_mode='floor')
    all_labels = all_indexes % out_logits.shape[2]
    boxes = center_to_corners_format(out_bbox)
    boxes = torch.gather(boxes, 1, all_boxes.unsqueeze(-1).repeat(1, 1, 4))
    if target_sizes is not None:
        if isinstance(target_sizes, List):
            img_h = torch.Tensor([i[0] for i in target_sizes])
            img_w = torch.Tensor([i[1] for i in target_sizes])
        else:
            img_h, img_w = target_sizes.unbind(1)
        scale_fct = torch.stack([img_w, img_h, img_w, img_h], dim=1).to(boxes.device)
        boxes = boxes * scale_fct[:, None, :]
    results = []
    for b in range(batch_size):
        box = boxes[b]
        score = all_scores[b]
        lbls = all_labels[b]
        pre_topk = score.topk(min(10000, num_queries * num_labels)).indices
        box = box[pre_topk]
        score = score[pre_topk]
        lbls = lbls[pre_topk]
        keep_inds = batched_nms(box, score, lbls, nms_threshold)[:100]
        score = score[keep_inds]
        lbls = lbls[keep_inds]
        box = box[keep_inds]
        results.append({'scores': score[score > threshold], 'labels': lbls[score > threshold], 'boxes': box[score > threshold]})
    return results

Converts the output of [`DetaForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
bottom_right_x, bottom_right_y) format. Only supports PyTorch.

Args:
outputs ([`DetrObjectDetectionOutput`]):
Raw outputs of the model.
threshold (`float`, *optional*, defaults to 0.5):
Score threshold to keep object detection predictions.
target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*):
Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size
(height, width) of each image in the batch. If left to None, predictions will not be resized.
nms_threshold (`float`, *optional*, defaults to 0.7):
NMS threshold.

Returns:
`List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
in the batch as predicted by the model.


def setup(config_root=''):
    config = _load_config(root=config_root)
    logging_config = config.get('core', {}).get('logging', {})
    log_level = logging_config.get('level', 'INFO').upper()
    log_handlers = (logging_config.get('handlers') or ['syslog'])
    ulogger.setup_logging(progname='gordon-janitor', level=log_level, handlers=log_handlers)
    return config

Service configuration and logging setup.

Configuration defined in ``gordon-janitor-user.toml`` will overwrite
``gordon-janitor.toml``.

Args:
config_root (str): where configuration should load from,
defaults to current working directory.
Returns:
A dict for Gordon service configuration


def from_dict(cls, data):
    args = []
    if (('id' in data) and ('data' in data)):
        measurement_class = CanMessage
        args.append(('Bus %s: 0x%x' % (data.get('bus', '?'), data['id'])))
        args.append(data['data'])
    else:
        measurement_class = cls._class_from_name(data['name'])
        if (measurement_class == Measurement):
            args.append(data['name'])
        args.append(data['value'])
    return measurement_class(*args, event=data.get('event', None), override_unit=True)

Create a new Measurement subclass instance using the given dict.

If Measurement.name_from_class was previously called with this data's
associated Measurement sub-class in Python, the returned object will be
an instance of that sub-class. If the measurement name in ``data`` is
unrecognized, the returned object will be of the generic ``Measurement``
type.

Args:
data (dict): the data for the new measurement, including at least a
name and value.


def ParseDict(js_dict, message, ignore_unknown_fields=False):
    parser = _Parser(ignore_unknown_fields)
    parser.ConvertMessage(js_dict, message)
    return message

Parses a JSON dictionary representation into a message.

Args:
js_dict: Dict representation of a JSON message.
message: A protocol buffer message to merge into.
ignore_unknown_fields: If True, do not raise errors for unknown fields.

Returns:
The same message passed as argument.


def _GenDiscoveryDoc(service_class_names, output_path, hostname=None, application_path=None):
    output_files = []
    service_configs = GenApiConfig(service_class_names, hostname=hostname, config_string_generator=discovery_generator.DiscoveryGenerator(), application_path=application_path)
    for (api_name_version, config) in service_configs.iteritems():
        discovery_name = (api_name_version + '.discovery')
        output_files.append(_WriteFile(output_path, discovery_name, config))
    return output_files

Write discovery documents generated from the service classes to file.

Args:
service_class_names: A list of fully qualified ProtoRPC service names.
output_path: The directory to output the discovery docs to.
hostname: A string hostname which will be used as the default version
hostname. If no hostname is specificied in the @endpoints.api decorator,
this value is the fallback. Defaults to None.
application_path: A string containing the path to the AppEngine app.

Returns:
A list of discovery doc filenames.


def _joined_array(num_dims, reduce_dim):
    formatter = '{:0%db}' % (num_dims - 1)
    result = np.zeros(shape=[2] * (num_dims - 1), dtype='S%d' % (2 * num_dims))
    flat = result.ravel()
    for i in range(2 ** (num_dims - 1)):
        dims = formatter.format(i)
        flat[i] = ''.join([(dims[:reduce_dim] + '%d' + dims[reduce_dim:]) % j for j in range(2)])
    return result

Creates an ndarray with the result from reduce_join on input_array.

Args:
num_dims: The number of dimensions of the original input array.
reduce_dim: The dimension to reduce.

Returns:
An ndarray of shape [2] * (num_dims - 1).


def fetch_result(self, trial):
    trial_future = self._find_item(self._running, trial)
    if (not trial_future):
        raise ValueError('Trial was not running.')
    self._running.pop(trial_future[0])
    with warn_if_slow('fetch_result'):
        result = ray.get(trial_future[0])
    if isinstance(result, _LocalWrapper):
        result = result.unwrap()
    return result

Fetches one result of the running trials.

Returns:
Result of the most recent trial training run.


def __init__(self, submission_id, submissions, storage_bucket):

    super(AttackSubmission, self).__init__(submission_id, submissions,
                                           storage_bucket)
    if (self.type != TYPE_TARGETED) and (self.type != TYPE_NONTARGETED):
      raise WorkerError('Incorrect attack type for submission "{0}"'.format(
          submission_id))

Initializes AttackSubmission.

Args:
submission_id: ID of the submission
submissions: instance of CompetitionSubmissions with all submissions
storage_bucket: storage bucket where all submissions are stored

Raises:
WorkerError: if submission has incorrect type


def watch(self, path, recursive=False):
    self._logger.info('Initializing watcher for path "%s"', path)
    handler = FileHandler(self)
    self._observer = Observer()
    self._observer.schedule(handler, path, recursive)
    self._logger.info('Starting watcher')
    self._observer.start()
    self._watch = True
    try:
        self._logger.info('Waiting for file events')
        while self._watch:
            time.sleep(1)
    except KeyboardInterrupt:
        self.stop_watching()
    self._observer.join()

Watch for files in a directory and apply normalizations.

Watch for new or changed files in a directory and apply
normalizations over them.

Args:
path: Path to the directory.
recursive: Whether to find files recursively or not.


def _GetVSSStoreIdentifiers(self, scan_node):

    if not scan_node or not scan_node.path_spec:
      raise errors.SourceScannerError('Invalid scan node.')

    volume_system = vshadow_volume_system.VShadowVolumeSystem()
    volume_system.Open(scan_node.path_spec)

    volume_identifiers = self._source_scanner.GetVolumeIdentifiers(
        volume_system)
    if not volume_identifiers:
      return []

    if self._vss_stores:
      if self._vss_stores == 'all':
        vss_stores = range(1, volume_system.number_of_volumes + 1)
      else:
        vss_stores = self._vss_stores

      selected_volume_identifiers = self._NormalizedVolumeIdentifiers(
          volume_system, vss_stores, prefix='vss')

      if not set(selected_volume_identifiers).difference(volume_identifiers):
        return selected_volume_identifiers

    try:
      volume_identifiers = self._PromptUserForVSSStoreIdentifiers(
          volume_system, volume_identifiers)

    except KeyboardInterrupt:
      raise errors.UserAbort('File system scan aborted.')

    return self._NormalizedVolumeIdentifiers(
        volume_system, volume_identifiers, prefix='vss')

Determines the VSS store identifiers.

Args:
scan_node (dfvfs.SourceScanNode): scan node.

Returns:
list[str]: VSS store identifiers.

Raises:
SourceScannerError: if the format of or within the source is not
supported or the scan node is invalid.
UserAbort: if the user requested to abort.


def __init__(self, index, height):

        self.index = index
        self.height = height

Create an instance.

Args:
index (int):
height (int):


def ParseRow(self, parser_mediator, row_offset, row):

    filename = row.get('name', None)
    md5_hash = row.get('md5', None)
    mode = row.get('mode_as_string', None)

    inode_number = row.get('inode', None)
    if '-' in inode_number:
      inode_number, _, _ = inode_number.partition('-')

    try:
      inode_number = int(inode_number, 10)
    except (TypeError, ValueError):
      inode_number = None

    data_size = self._GetIntegerValue(row, 'size')
    user_uid = self._GetIntegerValue(row, 'uid')
    user_gid = self._GetIntegerValue(row, 'gid')

    event_data = MactimeEventData()
    event_data.filename = filename
    event_data.inode = inode_number
    event_data.md5 = md5_hash
    event_data.mode_as_string = mode
    event_data.offset = row_offset
    event_data.size = data_size
    event_data.user_gid = user_gid

    if user_uid is None:
      event_data.user_sid = None
    else:

      event_data.user_sid = '{0:d}'.format(user_uid)

    for value_name, timestamp_description in iter(
        self._TIMESTAMP_DESC_MAP.items()):
      posix_time = self._GetIntegerValue(row, value_name)

      if not posix_time:
        continue

      date_time = dfdatetime_posix_time.PosixTime(timestamp=posix_time)
      event = time_events.DateTimeValuesEvent(date_time, timestamp_description)
      parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a line of the log file and produces events.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
row_offset (int): number of the corresponding line.
row (dict[str, str]): fields of a single row, as specified in COLUMNS.


def _align_hydrogen_atoms(mol1, mol2, heavy_indices1,
                              heavy_indices2):

        num_atoms = mol2.NumAtoms()
        all_atom = set(range(1, num_atoms+1))
        hydrogen_atoms1 = all_atom - set(heavy_indices1)
        hydrogen_atoms2 = all_atom - set(heavy_indices2)
        label1 = heavy_indices1 + tuple(hydrogen_atoms1)
        label2 = heavy_indices2 + tuple(hydrogen_atoms2)

        cmol1 = ob.OBMol()
        for i in label1:
            oa1 = mol1.GetAtom(i)
            a1 = cmol1.NewAtom()
            a1.SetAtomicNum(oa1.GetAtomicNum())
            a1.SetVector(oa1.GetVector())
        cmol2 = ob.OBMol()
        for i in label2:
            oa2 = mol2.GetAtom(i)
            a2 = cmol2.NewAtom()
            a2.SetAtomicNum(oa2.GetAtomicNum())
            a2.SetVector(oa2.GetVector())

        aligner = ob.OBAlign(False, False)
        aligner.SetRefMol(cmol1)
        aligner.SetTargetMol(cmol2)
        aligner.Align()
        aligner.UpdateCoords(cmol2)

        hydrogen_label2 = []
        hydrogen_label1 = list(range(len(heavy_indices1) + 1, num_atoms + 1))
        for h2 in range(len(heavy_indices2) + 1, num_atoms + 1):
            distance = 99999.0
            idx = hydrogen_label1[0]
            a2 = cmol2.GetAtom(h2)
            for h1 in hydrogen_label1:
                a1 = cmol1.GetAtom(h1)
                d = a1.GetDistance(a2)
                if d < distance:
                    distance = d
                    idx = h1
            hydrogen_label2.append(idx)
            hydrogen_label1.remove(idx)

        hydrogen_orig_idx2 = label2[len(heavy_indices2):]
        hydrogen_canon_orig_map2 = [(canon, orig) for canon, orig
                                    in zip(hydrogen_label2,
                                           hydrogen_orig_idx2)]
        hydrogen_canon_orig_map2.sort(key=lambda m: m[0])
        hydrogen_canon_indices2 = [x[1] for x in hydrogen_canon_orig_map2]

        canon_label1 = label1
        canon_label2 = heavy_indices2 + tuple(hydrogen_canon_indices2)

        return canon_label1, canon_label2

Align the label of topologically identical atoms of second molecule
towards first molecule

Args:
mol1: First molecule. OpenBabel OBMol object
mol2: Second molecule. OpenBabel OBMol object
heavy_indices1: inchi label map of the first molecule
heavy_indices2: label map of the second molecule

Return:
corrected label map of all atoms of the second molecule


async def set_headline(self, name, level, message):

        if name not in self.services:
            raise ArgumentError("Unknown service name", short_name=name)

        self.services[name]['state'].set_headline(level, message)

        headline = self.services[name]['state'].headline.to_dict()
        await self._notify_update(name, 'new_headline', headline)

Set the sticky headline for a service.

Args:
name (string): The short name of the service to query
level (int): The level of the message (info, warning, error)
message (string): The message contents


def read(self, size=None):

    if not self._is_open:
      raise IOError('Not opened.')

    if self._range_offset < 0 or self._range_size < 0:
      raise IOError('Invalid data range.')

    if self._current_offset < 0:
      raise IOError(
          'Invalid current offset: {0:d} value less than zero.'.format(
              self._current_offset))

    if self._current_offset >= self._range_size:
      return b''

    if size is None:
      size = self._range_size
    if self._current_offset + size > self._range_size:
      size = self._range_size - self._current_offset

    self._file_object.seek(
        self._range_offset + self._current_offset, os.SEEK_SET)

    data = self._file_object.read(size)

    self._current_offset += len(data)

    return data

Reads a byte string from the file-like object at the current offset.

The function will read a byte string of the specified size or
all of the remaining data if no size was specified.

Args:
size (Optional[int]): number of bytes to read, where None is all
remaining data.

Returns:
bytes: data read.

Raises:
IOError: if the read failed.
OSError: if the read failed.


def SendEmail(self, to_addresses, from_address, subject, message, attachments=None, is_html=True, cc_addresses=None, message_id=None, headers=None):
    headers = (headers or {})
    msg = MIMEMultipart('alternative')
    if is_html:
        text = self.RemoveHtmlTags(message)
        part1 = MIMEText(text, 'plain')
        msg.attach(part1)
        part2 = MIMEText(message, 'html')
        msg.attach(part2)
    else:
        part1 = MIMEText(message, 'plain')
        msg.attach(part1)
    if attachments:
        for (file_name, file_data) in iteritems(attachments):
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(file_data)
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', ('attachment; filename="%s"' % file_name))
            msg.attach(part)
    msg['Subject'] = subject
    from_address = self.AddEmailDomain(from_address)
    to_addresses = self.SplitEmailsAndAppendEmailDomain(to_addresses)
    cc_addresses = self.SplitEmailsAndAppendEmailDomain((cc_addresses or ''))
    msg['From'] = from_address
    msg['To'] = ','.join(to_addresses)
    if cc_addresses:
        msg['CC'] = ','.join(cc_addresses)
    if message_id:
        msg.add_header('Message-ID', message_id)
    for (header, value) in iteritems(headers):
        msg.add_header(header, value)
    try:
        s = smtplib.SMTP(config.CONFIG['Worker.smtp_server'], int(config.CONFIG['Worker.smtp_port']))
        s.ehlo()
        if config.CONFIG['Worker.smtp_starttls']:
            s.starttls()
            s.ehlo()
        if (config.CONFIG['Worker.smtp_user'] and config.CONFIG['Worker.smtp_password']):
            s.login(config.CONFIG['Worker.smtp_user'], config.CONFIG['Worker.smtp_password'])
        s.sendmail(from_address, (to_addresses + cc_addresses), msg.as_string())
        s.quit()
    except (socket.error, smtplib.SMTPException) as e:
        raise RuntimeError(('Could not connect to SMTP server to send email. Please check config option Worker.smtp_server. Currently set to %s. Error: %s' % (config.CONFIG['Worker.smtp_server'], e)))

This method sends an email notification.

Args:
to_addresses: blah@mycompany.com string, list of addresses as csv string,
or rdf_standard.DomainEmailAddress
from_address: blah@mycompany.com string
subject: email subject string
message: message contents string, as HTML or plain text
attachments: iterable of filename string and file data tuples,
e.g. {"/file/name/string": filedata}
is_html: true if message is in HTML format
cc_addresses: blah@mycompany.com string, or list of addresses as
csv string
message_id: smtp message_id. Used to enable conversation threading
headers: dict of str-> str, headers to set
Raises:
RuntimeError: for problems connecting to smtp server.


def raise_for_status(status: int, headers: MutableMapping, data: MutableMapping) -> None:
    if (status != 200):
        if (status == 429):
            if isinstance(data, str):
                error = data
            else:
                error = data.get('error', 'ratelimited')
            try:
                retry_after = int(headers.get('Retry-After', 1))
            except ValueError:
                retry_after = 1
            raise exceptions.RateLimited(retry_after, error, status, headers, data)
        else:
            raise exceptions.HTTPException(status, headers, data)

Check request response status

Args:
status: Response status
headers: Response headers
data: Response data

Raises:
:class:`slack.exceptions.RateLimited`: For 429 status code
:class:`slack.exceptions:HTTPException`:


def copydb(self, sourcedb, destslab, destdbname=None, progresscb=None):
    destdb = destslab.initdb(destdbname, sourcedb.dupsort)
    statdict = destslab.stat(db=destdb)
    if (statdict['entries'] > 0):
        raise s_exc.DataAlreadyExists()
    rowcount = 0
    for chunk in s_common.chunks(self.scanByFull(db=sourcedb), COPY_CHUNKSIZE):
        (ccount, acount) = destslab.putmulti(chunk, dupdata=True, append=True, db=destdb)
        if ((ccount != len(chunk)) or (acount != len(chunk))):
            raise s_exc.BadCoreStore(mesg='Unexpected number of values written')
        rowcount += len(chunk)
        if ((progresscb is not None) and (0 == (rowcount % PROGRESS_PERIOD))):
            progresscb(rowcount)
    return rowcount

Copy an entire database in this slab to a new database in potentially another slab.

Args:
sourcedb (LmdbDatabase): which database in this slab to copy rows from
destslab (LmdbSlab): which slab to copy rows to
destdbname (str): the name of the database to copy rows to in destslab
progresscb (Callable[int]):  if not None, this function will be periodically called with the number of rows
completed

Returns:
(int): the number of rows copied

Note:
If any rows already exist in the target database, this method returns an error.  This means that one cannot
use destdbname=None unless there are no explicit databases in the destination slab.


def get_num_code_systems_per_value_set(engine: sqlalchemy.engine.base.Engine, table: sqlalchemy.sql.expression.TableClause) -> Dict[str, int]:
    query = sqlalchemy.select([table.c.valueseturi, table.c.valuesetversion, sqlalchemy.func.array_agg(sqlalchemy.distinct(table.c.system)).label('systems')]).group_by(table.c.valueseturi, table.c.valuesetversion)
    with engine.connect() as connection:
        systems_per_value_set = connection.execute(query)
        return _query_results_to_code_system_counts(systems_per_value_set.fetchall())

Queries `table` for the code systems referenced by each value set.

Looks up the code systems referenced by each value set decribed in the
valueset_codes `table`. Returns counts for the number of code systems
referenced by each value set.

If the value sets' URL contains a "|version" suffix, reports the number of
code systems referenced by that value set and version.
If the url does not contain a "|version" suffix, the number of code systems
across all versions of the value set will be reported.

Args:
engine: The SqlAlachemy engine to use when performing queries.
table: The SqlAlchemy table to query.

Returns:
A CodeSystemCounts object for accessing code systems information.


def unload(self, keepables=None):
    to_del = [ds_id for (ds_id, projectable) in self.datasets.items() if ((ds_id not in self.wishlist) and ((not keepables) or (ds_id not in keepables)))]
    for ds_id in to_del:
        LOG.debug('Unloading dataset: %r', ds_id)
        del self.datasets[ds_id]

Unload all unneeded datasets.

Datasets are considered unneeded if they weren't directly requested
or added to the Scene by the user or they are no longer needed to
generate composites that have yet to be generated.

Args:
keepables (iterable): DatasetIDs to keep whether they are needed
or not.


def get_synth_input_fn(height, width, num_channels, num_classes):

    def input_fn(is_training, data_dir, batch_size, *args, **kwargs):
        images = tf.zeros((batch_size, height, width, num_channels), tf.float32)
        labels = tf.zeros((batch_size, num_classes), tf.int32)
        return tf.data.Dataset.from_tensors((images, labels)).repeat()
    return input_fn

Returns an input function that returns a dataset with zeroes.

This is useful in debugging input pipeline performance, as it removes all
elements of file reading and image preprocessing.

Args:
height: Integer height that will be used to create a fake image tensor.
width: Integer width that will be used to create a fake image tensor.
num_channels: Integer depth that will be used to create a fake image tensor.
num_classes: Number of classes that should be represented in the fake labels
tensor

Returns:
An input_fn that can be used in place of a real one to return a dataset
that can be used for iteration.


def getSet(self, name):
    return lock_and_call((lambda : Set(self._impl.getSet(name))), self._lock)

Get the set with the corresponding name.

Args:
name: Name of the set to be found.

Raises:
TypeError: if the specified set does not exist.


def update_vlan(self, name, vid, vni):

        cmd = 'vxlan vlan %s vni %s' % (vid, vni)
        return self.configure_interface(name, cmd)

Adds a new vlan to vni mapping for the interface

EosVersion:
4.13.7M

Args:
vlan (str, int): The vlan id to map to the vni
vni (str, int): The vni value to use

Returns:
True if the command completes successfully


def _add_asset_to_collection(asset_filename, asset_tensor):
    asset_proto = meta_graph_pb2.AssetFileDef()
    asset_proto.filename = asset_filename
    asset_proto.tensor_info.name = asset_tensor.name
    asset_any_proto = Any()
    asset_any_proto.Pack(asset_proto)
    ops.add_to_collection(constants.ASSETS_KEY, asset_any_proto)

Builds an asset proto and adds it to the asset collection of the graph.

Args:
asset_filename: The filename of the asset to be added.
asset_tensor: The asset tensor used to populate the tensor info of the
asset proto.


def __init__(self, path):

        self.path = path
        self.on_create = None
        self.on_modify = None
        self.on_delete = None
        self.jobs = None

Initialize the Directory Watcher
Args:
path: path of the directory to watch


def find(self, *strings, **kwargs):
    start = kwargs.pop('start', 0)
    stop = kwargs.pop('stop', None)
    keys_only = kwargs.pop('keys_only', False)
    results = {string: [] for string in strings}
    stop = (len(self) if (stop is None) else stop)
    for (i, line) in enumerate(self[start:stop]):
        for string in strings:
            if (string in line):
                if keys_only:
                    results[string].append(i)
                else:
                    results[string].append((i, line))
    if (len(strings) == 1):
        return results[strings[0]]
    return results

Search the entire editor for lines that match the string.

.. code-block:: Python

string = '''word one
word two
three'''
ed = Editor(string)
ed.find('word')          # [(0, "word one"), (1, "word two")]
ed.find('word', 'three') # {'word': [...], 'three': [(2, "three")]}

Args:
strings (str): Any number of strings to search for
keys_only (bool): Only return keys
start (int): Optional line to start searching on
stop (int): Optional line to stop searching on

Returns:
results: If multiple strings searched a dictionary of string key, (line number, line) values (else just values)


def _parse_authors(authors):

    link = authors.find("a")
    link = link[0].params.get("href") if link else None

    author_list = _strip_content(authors)

    if "(" in author_list:
        author_list = author_list.split("(")[0]

    if not author_list.strip():
        return []

    return map(
        lambda author: Author(author.strip(), link),
        author_list.strip().split(",")
    )

Parse informations about authors of the book.

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

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


def set_result(self, result, from_tree=False):

        if self._read_only:
            if not from_tree:
                LOGGER.warning("Tried to set address %s on a"
                               " read-only context.",
                               self.address)
            return

        with self._condition:
            if self._read_only:
                if not from_tree:
                    LOGGER.warning("Tried to set address %s on a"
                                   " read-only context.",
                                   self.address)
                return
            if from_tree:

                if not self._result_set_in_context:
                    self._result = result
                    self._tree_has_set = True
            else:
                self._result = result
                self._result_set_in_context = True
                self._deleted = False

            self._condition.notify_all()

Set the addresses's value unless the future has been declared
read only.

Args:
result (bytes): The value at an address.
from_tree (bool): Whether the value is being set by a read from
the merkle tree.

Returns:
None


def fetch_json(self, uri_path, http_method='GET', query_params=None, body=None, headers=None):
    query_params = (query_params or {})
    headers = (headers or {})
    query_params = self.add_authorisation(query_params)
    uri = self.build_uri(uri_path, query_params)
    allowed_methods = ('POST', 'PUT', 'DELETE')
    if ((http_method in allowed_methods) and ('Content-Type' not in headers)):
        headers['Content-Type'] = 'application/json'
    headers['Accept'] = 'application/json'
    (response, content) = self.client.request(uri=uri, method=http_method, body=body, headers=headers)
    self.check_errors(uri, response)
    return json.loads(content.decode('utf-8'))

Make a call to Trello API and capture JSON response. Raises an error
when it fails.

Returns:
dict: Dictionary with the JSON data


def update_ethernet_settings(self, configuration, force=False, timeout=(- 1)):
    uri = '{}/ethernetSettings'.format(self.data['uri'])
    return self._helper.update(configuration, uri=uri, force=force, timeout=timeout)

Updates the Ethernet interconnect settings for the logical interconnect.

Args:
configuration:  Ethernet interconnect settings.
force: If set to true, the operation completes despite any problems with network connectivity or errors
on the resource itself. The default is false.
timeout: Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
in OneView; it just stops waiting for its completion.

Returns:
dict: Logical Interconnect.


def __init__(self, sync_optimizer, is_chief, num_tokens):
    self._sync_optimizer = sync_optimizer
    self._is_chief = is_chief
    self._num_tokens = num_tokens

Creates hook to handle SyncReplicasOptimizer initialization ops.

Args:
sync_optimizer: `SyncReplicasOptimizer` which this hook will initialize.
is_chief: `Bool`, whether is this a chief replica or not.
num_tokens: Number of tokens to add to the queue.


def distance_between(self, u, v):
    if (not isinstance(u, Node)):
        raise TypeError('u must be a Node')
    if (not isinstance(v, Node)):
        raise TypeError('v must be a Node')
    if (u == v):
        return 0.0
    u_dists = {u: 0.0}
    v_dists = {v: 0.0}
    c = u
    p = u.parent
    while (p is not None):
        u_dists[p] = u_dists[c]
        if (c.edge_length is not None):
            u_dists[p] += c.edge_length
        c = p
        p = p.parent
    c = v
    p = v.parent
    while (p is not None):
        v_dists[p] = v_dists[c]
        if (c.edge_length is not None):
            v_dists[p] += c.edge_length
        if (p in u_dists):
            return (u_dists[p] + v_dists[p])
        c = p
        p = p.parent
    raise RuntimeError('u and v are not in the same Tree')

Return the distance between nodes ``u`` and ``v`` in this ``Tree``

Args:
``u`` (``Node``): Node ``u``

``v`` (``Node``): Node ``v``

Returns:
``float``: The distance between nodes ``u`` and ``v``


def track_progress(func, tasks, bar_width=50, **kwargs):

    if isinstance(tasks, tuple):
        assert len(tasks) == 2
        assert isinstance(tasks[0], collections_abc.Iterable)
        assert isinstance(tasks[1], int)
        task_num = tasks[1]
        tasks = tasks[0]
    elif isinstance(tasks, collections_abc.Iterable):
        task_num = len(tasks)
    else:
        raise TypeError(
            '"tasks" must be an iterable object or a (iterator, int) tuple')
    prog_bar = ProgressBar(task_num, bar_width)
    results = []
    for task in tasks:
        results.append(func(task, **kwargs))
        prog_bar.update()
    sys.stdout.write('\n')
    return results

Track the progress of tasks execution with a progress bar.

Tasks are done with a simple for-loop.

Args:
func (callable): The function to be applied to each task.
tasks (list or tuple[Iterable, int]): A list of tasks or
(tasks, total num).
bar_width (int): Width of progress bar.

Returns:
list: The task results.


def add_capture(self, tensor, placeholder):
    self._function_captures.add_or_replace(key=id(tensor), external=tensor, internal=placeholder, is_by_ref=False)
    self.inputs.append(placeholder)

Capture a specific tensor and utilize the provided placeholder.

Args:
tensor: Tensor to captures.
placeholder: Provided placeholder for the tensor.


def to_voxels(array):
    if (type(array) is not numpy.ndarray):
        raise ValueError('array argument must be of type numpy.ndarray')
    return numpy.argwhere(array)

Converts an array to its voxel list.

Arguments:
array (numpy.ndarray): A numpy nd array. This must be boolean!

Returns:
A list of n-tuples


def sort_response(response: Dict[(str, Any)]) -> OrderedDict:
    root_order = ['jsonrpc', 'result', 'error', 'id']
    error_order = ['code', 'message', 'data']
    req = OrderedDict(sorted(response.items(), key=(lambda k: root_order.index(k[0]))))
    if ('error' in response):
        req['error'] = OrderedDict(sorted(response['error'].items(), key=(lambda k: error_order.index(k[0]))))
    return req

Sort the keys in a JSON-RPC response object.

This has no effect other than making it nicer to read. Useful in Python 3.5 only,
dictionaries are already sorted in newer Python versions.

Example::

>>> json.dumps(sort_response({'id': 2, 'result': 5, 'jsonrpc': '2.0'}))
{"jsonrpc": "2.0", "result": 5, "id": 1}

Args:
response: Deserialized JSON-RPC response.

Returns:
The same response, sorted in an OrderedDict.


def __init__(self, value: Union[None, int, float, str, List[Any], Tuple[Any]]=None, children: Optional[List['DNA']]=None, spec: Optional[DNASpec]=None, metadata: Optional[Dict[str, Any]]=None, *, allow_partial: bool=False, **kwargs):
    value, children, metadata = self._parse_value_and_children(value, children, metadata, spec)
    super().__init__(value=value, children=children, metadata=metadata or symbolic.Dict(), allow_partial=allow_partial, **kwargs)
    self._decision_by_id_cache = None
    self._named_decisions = None
    self._userdata = AttributeDict()
    self._cloneable_metadata_keys = set()
    self._cloneable_userdata_keys = set()
    self._spec = None
    if spec:
        self.use_spec(spec)

Constructor.

Args:
value: Value for current node.
children: Child DNA(s).
spec: DNA spec that constraint current node.
metadata: Optional dict as controller metadata for the DNA.
allow_partial: If True, allow the object to be partial.
**kwargs: keyword arguments that will be passed through to
symbolic.Object.


def p45(msg):

    d = hex2bin(data(msg))
    if d[26] == '0':
        return None
    p = bin2int(d[27:38])
    return p

Average static pressure.

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

Returns:
int: static pressure in hPa


def _get_prob_original_static(initial_dist_t, target_dist_t):
    init_static = tensor_util.constant_value(initial_dist_t)
    target_static = tensor_util.constant_value(target_dist_t)
    if init_static is None or target_static is None:
        return None
    else:
        return np.min(target_static / init_static)

Returns the static probability of sampling from the original.

`tensor_util.constant_value(prob_of_original)` returns `None` if it encounters
an Op that it isn't defined for. We have some custom logic to avoid this.

Args:
initial_dist_t: A tensor of the initial distribution.
target_dist_t: A tensor of the target distribution.

Returns:
The probability of sampling from the original distribution as a constant,
if it is a constant, or `None`.


def _get_default_configurable_parameter_values(fn, whitelist, blacklist):
    arg_vals = _ARG_DEFAULTS_CACHE.get(fn)
    if (arg_vals is not None):
        return arg_vals.copy()
    arg_spec = _get_cached_arg_spec(fn)
    if arg_spec.defaults:
        default_kwarg_names = arg_spec.args[(- len(arg_spec.defaults)):]
        arg_vals = dict(zip(default_kwarg_names, arg_spec.defaults))
    else:
        arg_vals = {}
    if (six.PY3 and arg_spec.kwonlydefaults):
        arg_vals.update(arg_spec.kwonlydefaults)
    for k in list(six.iterkeys(arg_vals)):
        whitelist_fail = (whitelist and (k not in whitelist))
        blacklist_fail = (blacklist and (k in blacklist))
        representable = _is_literally_representable(arg_vals[k])
        if (whitelist_fail or blacklist_fail or (not representable)):
            del arg_vals[k]
    _ARG_DEFAULTS_CACHE[fn] = arg_vals
    return arg_vals.copy()

Retrieve all default values for configurable parameters of a function.

Any parameters included in the supplied blacklist, or not included in the
supplied whitelist, are excluded.

Args:
fn: The function whose parameter values should be retrieved.
whitelist: The whitelist (or `None`) associated with the function.
blacklist: The blacklist (or `None`) associated with the function.

Returns:
A dictionary mapping configurable parameter names to their default values.


def _sample_tag_values(cls, sample_names, rformat, sample_fields):
    sample_tag_values = OrderedDict()
    tag_names = VcfRecord._format_list(rformat)
    for (i, sample_field) in enumerate(sample_fields):
        tag_values = (sample_field.split(':') if sample_field else '.')
        sample_tag_values[sample_names[i]] = OrderedDict(zip(tag_names, tag_values))
    return sample_tag_values

Creates a sample dict of tag-value dicts for a single variant record.

Args:
sample_names: list of sample name strings.
rformat: record format string (from VCF record).
sample_fields: list of strings where each string is the ';'
seperated format values for an individual sample.

Returns:
An dict of samples, where each key is a sample and each value
is an dict of format-values. See attribute below for example.
Will return '.' if no values for sampe field.


def poisson_cluster(data, k, init=None, max_iters=100):
    (genes, cells) = data.shape
    if (sparse.issparse(data) and (not sparse.isspmatrix_csc(data))):
        data = sparse.csc_matrix(data)
    (init, assignments) = kmeans_pp(data, k, centers=init)
    centers = np.copy(init)
    assignments = np.zeros(cells)
    for it in range(max_iters):
        lls = poisson_ll(data, centers)
        new_assignments = np.argmax(lls, 1)
        if np.equal(assignments, new_assignments).all():
            return (new_assignments, centers)
        for c in range(k):
            if sparse.issparse(data):
                if (data[(:, (new_assignments == c))].shape[0] == 0):
                    (new_c, _) = kmeans_pp(data, k, centers[(:, :c)])
                    centers[(:, c)] = new_c[(:, c)]
                else:
                    centers[(:, c)] = np.asarray(data[(:, (new_assignments == c))].mean(1)).flatten()
            elif (len(data[(:, (new_assignments == c))]) == 0):
                (new_c, _) = kmeans_pp(data, k, centers[(:, :c)])
                centers[(:, c)] = new_c[(:, c)]
            else:
                centers[(:, c)] = np.mean(data[(:, (new_assignments == c))], 1)
        assignments = new_assignments
    return (assignments, centers)

Performs Poisson hard EM on the given data.

Args:
data (array): A 2d array- genes x cells. Can be dense or sparse; for best performance, sparse matrices should be in CSC format.
k (int): Number of clusters
init (array, optional): Initial centers - genes x k array. Default: None, use kmeans++
max_iters (int, optional): Maximum number of iterations. Default: 100

Returns:
a tuple of two arrays: a cells x 1 vector of cluster assignments,
and a genes x k array of cluster means.


def kmeans_pp(data, k, centers=None):
    (genes, cells) = data.shape
    if (sparse.issparse(data) and (not sparse.isspmatrix_csc(data))):
        data = sparse.csc_matrix(data)
    num_known_centers = 0
    if (centers is None):
        centers = np.zeros((genes, k))
    else:
        num_known_centers = centers.shape[1]
        centers = np.concatenate((centers, np.zeros((genes, (k - num_known_centers)))), 1)
    distances = np.zeros((cells, k))
    distances[:] = np.inf
    if (num_known_centers == 0):
        init = np.random.randint(0, cells)
        if sparse.issparse(data):
            centers[(:, 0)] = data[(:, init)].toarray().flatten()
        else:
            centers[(:, 0)] = data[(:, init)]
        num_known_centers += 1
    available_cells = list(range(cells))
    for c in range(num_known_centers, k):
        c2 = (c - 1)
        if sparse.issparse(data):
            lls = poisson_ll(data, centers[(:, c2:(c2 + 1))]).flatten()
            distances[(:, c2)] = ((1 + lls.max()) - lls)
            distances[(:, c2)] /= distances[(:, c2)].max()
        else:
            for cell in range(cells):
                distances[(cell, c2)] = poisson_dist(data[(:, cell)], centers[(:, c2)])
        min_distances = np.min(distances, 1)
        min_distances = (min_distances ** 2)
        min_distances = min_distances[available_cells]
        min_dist = np.random.choice(available_cells, p=(min_distances / min_distances.sum()))
        available_cells.pop(available_cells.index(min_dist))
        if sparse.issparse(data):
            centers[(:, c)] = data[(:, min_dist)].toarray().flatten()
        else:
            centers[(:, c)] = data[(:, min_dist)]
    lls = poisson_ll(data, centers)
    new_assignments = np.argmax(lls, 1)
    centers[(centers == 0.0)] = eps
    return (centers, new_assignments)

Generates kmeans++ initial centers.

Args:
data (array): A 2d array- genes x cells
k (int): Number of clusters
centers (array, optional): if provided, these are one or more known cluster centers. 2d array of genes x number of centers (<=k).

Returns:
centers - a genes x k array of cluster means.
assignments - a cells x 1 array of cluster assignments


def check_interactive_docker_worker(link):

    errors = []
    log.info("Checking for {} {} interactive docker-worker".format(link.name, link.task_id))
    try:
        if link.task['payload']['features'].get('interactive'):
            errors.append("{} is interactive: task.payload.features.interactive!".format(link.name))
        if link.task['payload']['env'].get('TASKCLUSTER_INTERACTIVE'):
            errors.append("{} is interactive: task.payload.env.TASKCLUSTER_INTERACTIVE!".format(link.name))
    except KeyError:
        errors.append("check_interactive_docker_worker: {} task definition is malformed!".format(link.name))
    return errors

Given a task, make sure the task was not defined as interactive.

* ``task.payload.features.interactive`` must be absent or False.
* ``task.payload.env.TASKCLUSTER_INTERACTIVE`` must be absent or False.

Args:
link (LinkOfTrust): the task link we're checking.

Returns:
list: the list of error errors.  Success is an empty list.


def ported_string(raw_data, encoding='utf-8', errors='ignore'):
    if (not raw_data):
        return six.text_type()
    if isinstance(raw_data, six.text_type):
        return raw_data.strip()
    if six.PY2:
        try:
            return six.text_type(raw_data, encoding, errors).strip()
        except LookupError:
            return six.text_type(raw_data, 'utf-8', errors).strip()
    if six.PY3:
        try:
            return six.text_type(raw_data, encoding).strip()
        except (LookupError, UnicodeDecodeError):
            return six.text_type(raw_data, 'utf-8', errors).strip()

Give as input raw data and output a str in Python 3
and unicode in Python 2.

Args:
raw_data: Python 2 str, Python 3 bytes or str to porting
encoding: string giving the name of an encoding
errors: his specifies the treatment of characters
which are invalid in the input encoding

Returns:
str (Python 3) or unicode (Python 2)


def __init__(self, queue_id=None, port=None, length=None, properties=None):

        super().__init__()
        self.queue_id = queue_id
        self.port = port
        self.length = length
        self.properties = [] if properties is None else properties

Create a PacketQueue with the optional parameters below.

Args:
queue_id (int): ID of the specific queue.
port (int): Port his queue is attached to.
length (int): Length in bytes of this queue desc.
properties(~pyof.v0x04.common.queue.ListOfProperties):
Queue's list of properties. Default is an empty list.


def import_family(self, rfa_file):

        self._add_entry(templates.IMPORT_FAMILY
                                 .format(family_file=rfa_file))

Append a import family entry to the journal.

This instructs Revit to import a family into the opened model.

Args:
rfa_file (str): full path of the family file


def parse_source(info):

    if "extractor_key" in info:
        source = info["extractor_key"]
        lower_source = source.lower()

        for key in SOURCE_TO_NAME:
            lower_key = key.lower()
            if lower_source == lower_key:
                source = SOURCE_TO_NAME[lower_key]

        if source != "Generic":
            return source

    if "url" in info and info["url"] is not None:
        p = urlparse(info["url"])
        if p and p.netloc:
            return p.netloc

    return "Unknown"

Parses the source info from an info dict generated by youtube-dl

Args:
info (dict): The info dict to parse

Returns:
source (str): The source of this song


def add_cookie(self, cookie_dict):

        if not isinstance(cookie_dict, dict):
            raise TypeError('Type of the cookie must be a dict.')
        if not cookie_dict.get(
            'name', None
        ) or not cookie_dict.get(
            'value', None):
            raise KeyError('Missing required keys, \'name\' and \'value\' must be provided.')
        self._execute(Command.ADD_COOKIE, {'cookie': cookie_dict})

Set a cookie.

Support:
Web(WebView)

Args:
cookie_dict: A dictionary contain keys: "name", "value",
["path"], ["domain"], ["secure"], ["httpOnly"], ["expiry"].

Returns:
WebElement Object.


def b_fit_score(self, x, y):

        x = np.reshape(minmax_scale(x), (-1, 1))
        y = np.reshape(minmax_scale(y), (-1, 1))
        poly = PolynomialFeatures(degree=self.degree)
        poly_x = poly.fit_transform(x)

        poly_x[:,1] = 0
        poly_x[:,2] = 0

        regressor = LinearRegression()
        regressor.fit(poly_x, y)

        y_predict = regressor.predict(poly_x)
        error = mean_squared_error(y_predict, y)

        return error

Compute the RECI fit score

Args:
x (numpy.ndarray): Variable 1
y (numpy.ndarray): Variable 2

Returns:
float: RECI fit score


def intersect(self, other):

        if not isinstance(other, self.__class__):
            m = "You can only intersect striplogs with each other."
            raise StriplogError(m)

        result = []
        for iv in self:
            for jv in other:
                try:
                    result.append(iv.intersect(jv))
                except IntervalError:

                    pass
        return Striplog(result)

Makes a striplog of all intersections.

Args:
Striplog. The striplog instance to intersect with.

Returns:
Striplog. The result of the intersection.


def http(self, *args, **kwargs):
    return self.credentials.authorize(transport.get_http_object(*args, **kwargs))

Returns an authorized http instance.

Must only be called from within an @oauth_required decorated method, or
from within an @oauth_aware decorated method where has_credentials()
returns True.

Args:
*args: Positional arguments passed to httplib2.Http constructor.
**kwargs: Positional arguments passed to httplib2.Http constructor.


def check(self, dsm, **kwargs):

        logger.debug('Entities = %s' % dsm.entities)
        messages = []
        code_clean = True
        threshold = kwargs.pop('threshold', 1)
        rows, _ = dsm.size
        for i in range(0, rows):
            if dsm.data[i][0] > threshold:
                messages.append(
                    'Number of issues (%d) in module %s '
                    '> threshold (%d)' % (
                        dsm.data[i][0], dsm.entities[i], threshold))
                code_clean = False

        return code_clean, '\n'.join(messages)

Check code clean.

Args:
dsm (:class:`DesignStructureMatrix`): the DSM to check.

Returns:
bool, str: True if code clean else False, messages


def make_one_shot_iterator(self):
    return self._make_one_shot_iterator()

Get a one time use iterator for DistributedDatasetV1.

Note: This API is deprecated. Please use `for ... in dataset:` to iterate
over the dataset or `iter` to create an iterator.

Returns:
A DistributedIteratorV1 instance.


def json(cls, status_code, process):

    def func(response):
        ret = None
        if cls.boolean(status_code)(response):
            ret = (response.json() or {})
        return process(ret)
    return func

Callback to validate and extract a JSON object.

The returned callback checks a given response for the given
status_code using :function:`response_boolean`. On success the
response JSON is parsed and returned.

Args:

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

Returns:

A function that given a response returns the JSON object
in the given response. Raises a :class:`HeliumError` if
the response code does not match.


def _GetConsoleEncoding(self):
    console_encoding = getattr(sys.stdout, 'encoding', None)
    if not console_encoding:
        return None
    console_encoding = console_encoding.lower()
    if 'utf-8' in console_encoding:
        return 'utf8'
    elif 'cp437' in console_encoding:
        return 'cp437'
    return None

Gets the encoding as declared by the stdout stream.

Returns:
str, The encoding name or None if it could not be determined.


def list_items(system_wide=False):

	desktop_env = system.get_name()

	result = []

	if desktop_env == 'windows':
		sys_startup_dir = os.path.join(winreg.ExpandEnvironmentStrings('%PROGRAMDATA%'), 'Microsoft\\Windows\\Start Menu\\Programs\\Startup')
		user_startup_dir = os.path.join(get_config_dir()[0], 'Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup')

		startup_dir = sys_startup_dir if system_wide else user_startup_dir

		for file in os.listdir(startup_dir):
			file_path = os.path.join(startup_dir, file)

			result.append({ 'name': file, 'command': os.path.join(startup_dir, file) })

	elif desktop_env == 'mac':
		items_list = system.get_cmd_out('launchtl list | awk \'{print $3}\'')
		for item in items_list.split('\n'):

			launchd_plist_paths = ['~/Library/LaunchAgents',
									'/Library/LaunchAgents',
									'/Library/LaunchDaemons',
									'/System/Library/LaunchAgents',
									'/System/Library/LaunchDaemons']

			for path in launchd_plist_paths:
				if item + '.plist' in os.listdir(path):
					plist_file = os.path.join(path, item + '.plist')

			if sys.version_info.major == 2:
				plist_parsed = plistlib.readPlist(plist_file)
			else:
				with open(plist_file) as f:
					plist_parsed = plistlib.load(f)

			if 'Program' in plist_parsed:
				cmd = plist_parsed['Program']

				if 'ProgramArguments' in plist_parsed:
					cmd += ' '.join(plist_parsed['ProgramArguments'])

			elif 'ProgramArguments' in plist_parsed:
				cmd = ' '.join(plist_parsed['ProgramArguments'])

			else:
				cmd = ''

			result.append({ 'name': item, 'command': cmd })

	else:

		profile = os.path.expanduser('~/.profile')

		if os.path.isfile(profile):
			with open(profile) as f:
				for line in f:
					if system.is_in_path(line.lstrip().split(' ')[0]):
						cmd_name = line.lstrip().split(' ')[0]

						result.append({ 'name': cmd_name, 'command': line.strip() })

		if system_wide:
			if os.path.isdir('/etc/profile.d'):
				for file in os.listdir('/etc/profile.d'):
					file_path = os.path.join('/etc/profile.d', file)
					result.append({ 'name': file, 'command': 'sh %s' % file_path })

		try:
			startup_dir = directories.get_config_dir('autostart', system_wide=system_wide)[0]

			for file in os.listdir(startup_dir):
				file_parsed = desktopfile.parse(os.path.join(startup_dir, file))

				if 'Name' in file_parsed:
					name = file_parsed['Name']

				else:
					name = file.replace('.desktop', '')

				if 'Exec' in file_parsed:
					if file_parsed['Terminal']:
						cmd = applications.terminal(exec_=file_parsed['Exec'],
														return_cmd=True)
					else:
						cmd = file_parsed['Exec']

				else:
					cmd = ''

				if not file_parsed.get('Hidden', False):
					result.append({ 'name': name, 'command': cmd })

		except IndexError:
			pass

	return result

List startup programs.

List the programs set to run at startup.

Args:
system_wide (bool): Gets the programs that run at system-wide startup.

Returns:
list: A list of dictionaries in this format:

.. code-block:: python

{
'name': 'The name of the entry.',
'command': 'The command used to run it.'
}


def pauli_string_half(circuit: circuits.Circuit) -> circuits.Circuit:

    return circuits.Circuit.from_ops(
            _pull_non_clifford_before(circuit),
            strategy=circuits.InsertStrategy.EARLIEST)

Return only the non-Clifford part of a circuit.  See
convert_and_separate_circuit().

Args:
circuit: A Circuit with the gate set {SingleQubitCliffordGate,
PauliInteractionGate, PauliStringPhasor}.

Returns:
A Circuit with only PauliStringPhasor operations.


def fetch(args: List[str], env: Dict[(str, str)]=None, encoding: str=sys.getdefaultencoding()) -> str:
    (stdout, _) = run(args, env=env, capture_stdout=True, echo_stdout=False, encoding=encoding)
    log.debug(stdout)
    return stdout

Run a command and returns its stdout.

Args:
args: the command-line arguments
env: the operating system environment to use
encoding: the encoding to use for ``stdout``

Returns:
the command's ``stdout`` output


def ParseVideoRow(self, parser_mediator, query, row, **unused_kwargs):
    query_hash = hash(query)
    event_data = KodiVideoEventData()
    event_data.filename = self._GetRowValue(query_hash, row, 'strFilename')
    event_data.play_count = self._GetRowValue(query_hash, row, 'playCount')
    event_data.query = query
    timestamp = self._GetRowValue(query_hash, row, 'lastPlayed')
    date_time = dfdatetime_time_elements.TimeElements()
    date_time.CopyFromDateTimeString(timestamp)
    event = time_events.DateTimeValuesEvent(date_time, definitions.TIME_DESCRIPTION_LAST_VISITED)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a Video row.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
query (str): query that created the row.
row (sqlite3.Row): row.


class ConvNextV2Layer(nn.Module):

    def __init__(self, config, dim, drop_path=0):
        super().__init__()
        self.dwconv = nn.Conv2d(dim, dim, kernel_size=7, padding=3, groups=dim)
        self.layernorm = ConvNextV2LayerNorm(dim, eps=1e-06)
        self.pwconv1 = nn.Linear(dim, 4 * dim)
        self.act = ACT2FN[config.hidden_act]
        self.grn = ConvNextV2GRN(4 * dim)
        self.pwconv2 = nn.Linear(4 * dim, dim)
        self.drop_path = ConvNextV2DropPath(drop_path) if drop_path > 0.0 else nn.Identity()

    def forward(self, hidden_states: torch.FloatTensor) -> torch.Tensor:
        input = hidden_states
        x = self.dwconv(hidden_states)
        x = x.permute(0, 2, 3, 1)
        x = self.layernorm(x)
        x = self.pwconv1(x)
        x = self.act(x)
        x = self.grn(x)
        x = self.pwconv2(x)
        x = x.permute(0, 3, 1, 2)
        x = input + self.drop_path(x)
        return x

This corresponds to the `Block` class in the original implementation.

There are two equivalent implementations: [DwConv, LayerNorm (channels_first), Conv, GELU,1x1 Conv]; all in (N, C,
H, W) (2) [DwConv, Permute to (N, H, W, C), LayerNorm (channels_last), Linear, GELU, Linear]; Permute back

The authors used (2) as they find it slightly faster in PyTorch.

Args:
config ([`ConvNextV2Config`]): Model configuration class.
dim (`int`): Number of input channels.
drop_path (`float`): Stochastic depth rate. Default: 0.0.


def deploy_template(access_token, subscription_id, resource_group, deployment_name, template, parameters):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourcegroups/', resource_group, '/providers/Microsoft.Resources/deployments/', deployment_name, '?api-version=', DEPLOYMENTS_API])
    properties = {'template': template}
    properties['mode'] = 'Incremental'
    properties['parameters'] = parameters
    template_body = {'properties': properties}
    body = json.dumps(template_body)
    return do_put(endpoint, body, access_token)

Deploy a template referenced by a JSON string, with parameters as a JSON string.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
deployment_name (str): A name you give to the deployment.
template (str): String representatipn of a JSON template body.
parameters (str): String representation of a JSON template parameters body.

Returns:
HTTP response.


def _build_shuffle_gather(input_tensors, gather_devices, red_op, un_op=None):
    num_source_devices = len(input_tensors)
    num_gather_devices = len(gather_devices)
    shape = input_tensors[0].shape
    if len(shape) != 1:
        raise ValueError('input_tensors must be 1D')
    shards_by_source = []
    for d in range(0, num_source_devices):
        with ops.colocate_with(input_tensors[d]):
            shards_by_source.append(_ragged_split(input_tensors[d], num_gather_devices))
    reduced_shards = []
    for d in range(0, num_gather_devices):
        with ops.device(gather_devices[d]):
            values = [s[d] for s in shards_by_source]
            red_shard = red_op(values)
            if un_op:
                red_shard = un_op(red_shard)
            reduced_shards.append(red_shard)
    return reduced_shards

Construct the gather (concentrate and reduce) phase of shuffle all-reduce.

Args:
input_tensors: list of `tf.Tensor` values to be reduced.
gather_devices: list of names of devices on which reduction shards
should be placed.
red_op: the binary reduction Op
un_op: optional elementwise unary Op to be applied to fully-reduced values.

Returns:
list of `tf.Tensor` which are the fully reduced shards.

Raises:
ValueError: inputs not well-formed.


def __init__(self, definitions_registry):

    super(DataTypeMapFactory, self).__init__()
    self._definitions_registry = definitions_registry

Initializes a data type maps factory.

Args:
definitions_registry (DataTypeDefinitionsRegistry): data type definitions
registry.


def create_accumulator(self, *args, **kwargs):
    raise NotImplementedError(str(self))

Return a fresh, empty accumulator for the combine operation.

Args:
*args: Additional arguments and side inputs.
**kwargs: Additional arguments and side inputs.


def automatic_parser(result, dtypes={}, converters={}):
    np.seterr(all='raise')
    parsed = {}
    for (filename, contents) in result['output'].items():
        if (dtypes.get(filename) is None):
            dtypes[filename] = None
        if (converters.get(filename) is None):
            converters[filename] = None
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            parsed[filename] = np.genfromtxt(io.StringIO(contents), dtype=dtypes[filename], converters=converters[filename]).tolist()
    return parsed

Try and automatically convert strings formatted as tables into nested
list structures.

Under the hood, this function essentially applies the genfromtxt function
to all files in the output, and passes it the additional kwargs.

Args:
result (dict): the result to parse.
dtypes (dict): a dictionary containing the dtype specification to perform
parsing for each available filename. See the numpy genfromtxt
documentation for more details on how to format these.


def set_memory(self, total=None, static=None):

        if total:
            self.params["rem"]["mem_total"] = total
        if static:
            self.params["rem"]["mem_static"] = static

Set the maxium allowed memory.

Args:
total: The total memory. Integer. Unit: MBytes. If set to None,
this parameter will be neglected.
static: The static memory. Integer. Unit MBytes. If set to None,
this parameterwill be neglected.


def convert_elementwise_add(params, w_name, scope_name, inputs, layers, weights, names):
    print('Converting elementwise_add ...')
    if ('broadcast' in params):
        model0 = layers[inputs[0]]
        model1 = layers[inputs[1]]
        if (names == 'short'):
            tf_name = ('A' + random_string(7))
        elif (names == 'keep'):
            tf_name = w_name
        else:
            tf_name = (w_name + str(random.random()))

        def target_layer(x):
            layer = tf.add(x[0], x[1])
            return layer
        lambda_layer = keras.layers.Lambda(target_layer, name=tf_name)
        layers[scope_name] = lambda_layer([layers[inputs[0]], layers[inputs[1]]])
    else:
        model0 = layers[inputs[0]]
        model1 = layers[inputs[1]]
        if (names == 'short'):
            tf_name = ('A' + random_string(7))
        elif (names == 'keep'):
            tf_name = w_name
        else:
            tf_name = (w_name + str(random.random()))
        add = keras.layers.Add(name=tf_name)
        layers[scope_name] = add([model0, model1])

Convert elementwise addition.

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 tokenize(self, text):
    output_tokens = []
    for token in whitespace_tokenize(text):
        chars = list(token)
        if len(chars) > self.max_input_chars_per_word:
            output_tokens.append(self.unk_token)
            continue
        is_bad = False
        start = 0
        sub_tokens = []
        while start < len(chars):
            end = len(chars)
            cur_substr = None
            while start < end:
                substr = ''.join(chars[start:end])
                if start > 0:
                    substr = '
                if substr in self.vocab:
                    cur_substr = substr
                    break
                end -= 1
            if cur_substr is None:
                is_bad = True
                break
            sub_tokens.append(cur_substr)
            start = end
        if is_bad:
            output_tokens.append(self.unk_token)
        else:
            output_tokens.extend(sub_tokens)
    return output_tokens

Tokenizes a piece of text into its word pieces. This uses a greedy longest-match-first algorithm to perform
tokenization using the given vocabulary.

For example, `input = "unaffable"` will return as output `["un", "##aff", "##able"]`.

Args:
text: A single token or whitespace separated tokens. This should have
already been passed through *BasicTokenizer*.

Returns:
A list of wordpiece tokens.