def glyph_has_ink(font: TTFont, name: Text) -> bool:
    if ('glyf' in font):
        return ttf_glyph_has_ink(font, name)
    elif (('CFF ' in font) or ('CFF2' in font)):
        return cff_glyph_has_ink(font, name)
    else:
        raise Exception("Could not find 'glyf', 'CFF ', or 'CFF2' table.")

Checks if specified glyph has any ink.

That is, that it has at least one defined contour associated.
Composites are considered to have ink if any of their components have ink.
Args:
font:       the font
glyph_name: The name of the glyph to check for ink.
Returns:
True if the font has at least one contour associated with it.


def code_challenge(verifier):

    digest = hashlib.sha256(verifier).digest()
    return base64.urlsafe_b64encode(digest).rstrip(b'=')

Creates a 'code_challenge' as described in section 4.2 of RFC 7636
by taking the sha256 hash of the verifier and then urlsafe
base64-encoding it.

Args:
verifier: bytestring, representing a code_verifier as generated by
code_verifier().

Returns:
Bytestring, representing a urlsafe base64-encoded sha256 hash digest,
without '=' padding.


def slice_list(in_list, lens):

    if not isinstance(lens, list):
        raise TypeError('"indices" must be a list of integers')
    elif sum(lens) != len(in_list):
        raise ValueError(
            'sum of lens and list length does not match: {} != {}'.format(
                sum(lens), len(in_list)))
    out_list = []
    idx = 0
    for i in range(len(lens)):
        out_list.append(in_list[idx:idx + lens[i]])
        idx += lens[i]
    return out_list

Slice a list into several sub lists by a list of given length.

Args:
in_list (list): The list to be sliced.
lens(int or list): The expected length of each out list.

Returns:
list: A list of sliced list.


def edit_distance_filter(source_target_input, max_equal_to_diff_ratio=0):
    thrown_out_count = 0
    source_target_output = []
    if (not max_equal_to_diff_ratio):
        return (source_target_input, thrown_out_count)
    for src_tgt in source_target_input:
        opcodes = fast_match_sequences(*src_tgt)
        diff_char_count = 0
        equal_char_count = 0
        for (tag, i1, i2, j1, j2) in opcodes:
            if (tag == 'diff'):
                diff_char_count += max((i2 - i1), (j2 - j1))
            else:
                equal_char_count += (i2 - i1)
        if (diff_char_count <= (max_equal_to_diff_ratio * equal_char_count)):
            source_target_output.append(src_tgt)
        else:
            thrown_out_count += 1
    return (source_target_output, thrown_out_count)

Filter out examples that exceed max_edit_ratio between source and target.

Args:
source_target_input:     a list of [source, target] pairs
max_equal_to_diff_ratio: cutoff for ratio of equal chars / diff chars
between source and target

Returns:
source_target_output:    filtered subset of [source, target] input pairs
thrown_out_count:        number of examples filtered out


def get_config_multiline_option(parser: ConfigParser,
                                section: str,
                                option: str,
                                default: List[str] = None) -> List[str]:

    default = default or []
    if not parser.has_section(section):
        raise ValueError("config missing section: " + section)
    try:
        multiline = parser.get(section, option)
        values = [x.strip() for x in multiline.splitlines() if x.strip()]
        return values
    except NoOptionError:
        return default

Retrieves a multi-line string value from a parser as a list of strings
(one per line, ignoring blank lines).

Args:
parser: instance of :class:`ConfigParser`
section: section name within config file
option: option (variable) name within that section
default: value to return if option is absent (``None`` is mapped to
``[]``)

Returns:
list of strings

Raises:
ValueError: if the section is absent


def CreateProductPartition(client, adgroup_id):

  ad_group_criterion_service = client.GetService('AdGroupCriterionService',
                                                 'v201809')
  helper = ProductPartitionHelper(adgroup_id)
  root = helper.CreateSubdivision()

  new_product_canonical_condition = {
      'xsi_type': 'ProductCanonicalCondition',
      'condition': 'NEW'
  }

  used_product_canonical_condition = {
      'xsi_type': 'ProductCanonicalCondition',
      'condition': 'USED'
  }

  other_product_canonical_condition = {
      'xsi_type': 'ProductCanonicalCondition',
  }

  helper.CreateUnit(root, new_product_canonical_condition)
  helper.CreateUnit(root, used_product_canonical_condition)
  helper.CreateUnit(root, other_product_canonical_condition)

  result = ad_group_criterion_service.mutate(helper.operations)
  return result['value']

Creates a ProductPartition tree for the given AdGroup ID.

Args:
client: an AdWordsClient instance.
adgroup_id: a str AdGroup ID.

Returns:
The ProductPartition tree as a sudsobject.


def insert_flux_bias(cur, chain, system, flux_bias, chain_strength, encoded_data=None):
    if (encoded_data is None):
        encoded_data = {}
    insert_chain(cur, chain, encoded_data)
    insert_system(cur, system, encoded_data)
    if ('flux_bias' not in encoded_data):
        encoded_data['flux_bias'] = _encode_real(flux_bias)
    if ('chain_strength' not in encoded_data):
        encoded_data['chain_strength'] = _encode_real(chain_strength)
    if ('insert_time' not in encoded_data):
        encoded_data['insert_time'] = datetime.datetime.now()
    insert = '\n        INSERT OR REPLACE INTO flux_bias(chain_id, system_id, insert_time, flux_bias, chain_strength)\n        SELECT\n            chain.id,\n            system.id,\n            :insert_time,\n            :flux_bias,\n            :chain_strength\n        FROM chain, system\n        WHERE\n            chain.chain_length = :chain_length AND\n            chain.nodes = :nodes AND\n            system.system_name = :system_name;\n        '
    cur.execute(insert, encoded_data)

Insert a flux bias offset into the cache.

Args:
cur (:class:`sqlite3.Cursor`):
An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement.

chain (iterable):
A collection of nodes. Chains in embedding act as one node.

system (str):
The unique name of a system.

flux_bias (float):
The flux bias offset associated with the given chain.

chain_strength (float):
The magnitude of the negative quadratic bias that induces the given chain in an Ising
problem.

encoded_data (dict, optional):
If a dictionary is provided, it will be populated with the serialized data. This is
useful for preventing encoding the same information many times.


def resolve_artifacts_by_builder_compat(
            self, package_names, builder_name, dependencies=False):

        paths = self.compat_builders.get(builder_name)
        if not paths:

            return

        resolver = (

            find_packages_requirements_dists
            if dependencies else

            pkg_names_to_dists
        )
        for distribution in resolver(package_names):
            path = paths.get(distribution.project_name)
            if path:
                yield path

Yield the list of paths to the artifacts in the order of the
dependency resolution

Arguments:

package_names
The names of the packages to probe the dependency graph, to
be provided as a list of strings.
artifact_name
The exact name of the artifact.
dependencies
Trace dependencies.  Default is off.

Returns the path of where the artifact should be if it has been
declared, otherwise None.


def __init__(self, **options):

        self.options = options
        self.logging_level = logging.DEBUG
        self.setup_logging()
        self.logger = Logger.get_logger(__name__)
        self.results = []

Initialize application with command line options.

Args:
options (ApplicationOptions): given command line options.


def _IsWindowsDrivePathSegment(cls, path_segment):

    if (len(path_segment) == 2 and path_segment[1] == ':' and
        path_segment[0].isalpha()):
      return True

    path_segment = path_segment.upper()
    return path_segment in ('%%ENVIRON_SYSTEMDRIVE%%', '%SYSTEMDRIVE%')

Determines if the path segment contains a Windows Drive indicator.

A drive indicator can be a drive letter or %SystemDrive%.

Args:
path_segment (str): path segment.

Returns:
bool: True if the path segment contains a Windows Drive indicator.


def bulk_lookup(self, api_name, keys):
    cached_data = {}
    for key in keys:
        value = self.lookup_value(api_name, key)
        if (value is not None):
            cached_data[key] = value
    return cached_data

Perform lookup on an enumerable of keys.

Args:
api_name: a string name of the API. Keys and values are segmented by api_name.
keys: an enumerable of string keys.


def __init__(self, states, internals, actions, include_next_states, capacity, scope='replay', summary_labels=None):

        super(Replay, self).__init__(
            states=states,
            internals=internals,
            actions=actions,
            include_next_states=include_next_states,
            capacity=capacity,
            scope=scope,
            summary_labels=summary_labels
        )

Replay memory.

Args:
states (dict): States specification.
internals (dict): Internal states specification.
actions (dict): Actions specification.
include_next_states (bool): Include subsequent state if true.
capacity (int): Memory capacity (number of state/internals/action/(next-state)? records).


def index_by_id(cls, target_id, resources):

        for index in range(len(resources)):
            if cls.id_by_index(index, resources) == target_id:
                return index

        raise AssertionError

Helper method to fetch the index of a resource by its id or address

Args:
resources (list of objects): The resources to be paginated
target_id (string): The address or header_signature of the resource

Returns:
integer: The index of the target resource

Raises:
AssertionError: Raised if the target is not found


def import_entities(self, entities):

        edata = Entity.create_payload(entities)
        r = fapi.upload_entities(self.namespace, self.name,
                                 edata, self.api_url)
        fapi._check_response_code(r, 201)

Upload entity objects.

Args:
entities: iterable of firecloud.Entity objects.


def document(self, name, file_name, owner=None, **kwargs):
    return Document(self.tcex, name, file_name, owner=owner, **kwargs)

Create the Document TI object.

Args:
owner:
name:
file_name:
**kwargs:

Return:


def wait_for_fresh_games(self, poll_interval=15.0):
    wait_until_game = self.read_wait_cell()
    if (not wait_until_game):
        return
    latest_game = self.latest_game_number
    last_latest = latest_game
    while (latest_game < wait_until_game):
        utils.dbg('Latest game {} not yet at required game {} (+{}, {:0.3f} games/sec)'.format(latest_game, wait_until_game, (latest_game - last_latest), ((latest_game - last_latest) / poll_interval)))
        time.sleep(poll_interval)
        last_latest = latest_game
        latest_game = self.latest_game_number

Block caller until required new games have been played.

Args:
poll_interval:  number of seconds to wait between checks

If the cell `table_state=metadata:wait_for_game_number` exists,
then block the caller, checking every `poll_interval` seconds,
until `table_state=metadata:game_counter is at least the value
in that cell.


def __init__(
            self, symbol: str = '', exchange: str = '', currency: str = '',
            **kwargs):

        Contract.__init__(
            self, secType='STK', symbol=symbol,
            exchange=exchange, currency=currency, **kwargs)

Stock contract.

Args:
symbol: Symbol name.
exchange: Destination exchange.
currency: Underlying currency.


def verify_file_exists(file_name, file_location):

    return __os.path.isfile(__os.path.join(file_location, file_name))

Function to verify if a file exists
Args:
file_name: The name of file to check
file_location: The location of the file, derive from the os module

Returns: returns boolean True or False


def main(argv: Optional[Sequence[str]] = None) -> None:

    args = parse_arguments(argv=argv)

    if args.logging:
        logging.basicConfig(level=logging.DEBUG)

    handle_skip()

    action = args.action
    request = parse_request()
    LOGGER.debug('Received action %s with request:\n%s',
                 action, request)

    try:
        mapping = parse_mapping(args.mapping)
    except Exception as error:
        LOGGER.critical('Unable to parse mapping file', exc_info=True)
        print(
            'Unable to parse mapping file: {error}'.format(
                error=error),
            file=sys.stderr)
        sys.exit(1)

    if action == 'get':
        get_password(request, mapping)
    else:
        LOGGER.info('Action %s is currently not supported', action)
        sys.exit(1)

Start the pass-git-helper script.

Args:
argv:
If not ``None``, use the provided command line arguments for
parsing. Otherwise, extract them automatically.


def find_model_patch_tracks(self):
    self.model_grid.load_data()
    tracked_model_objects = []
    model_objects = []
    if (self.model_grid.data is None):
        print('No model output found')
        return tracked_model_objects
    min_orig = self.model_ew.min_thresh
    max_orig = self.model_ew.max_thresh
    data_increment_orig = self.model_ew.data_increment
    self.model_ew.min_thresh = 0
    self.model_ew.data_increment = 1
    self.model_ew.max_thresh = 100
    for (h, hour) in enumerate(self.hours):
        print('Finding {0} objects for run {1} Hour: {2:02d}'.format(self.ensemble_member, self.run_date.strftime('%Y%m%d%H'), hour))
        if (self.mask is not None):
            model_data = (self.model_grid.data[h] * self.mask)
        else:
            model_data = self.model_grid.data[h]
        model_data[:self.patch_radius] = 0
        model_data[(- self.patch_radius):] = 0
        model_data[(:, :self.patch_radius)] = 0
        model_data[(:, (- self.patch_radius):)] = 0
        scaled_data = np.array(rescale_data(model_data, min_orig, max_orig))
        hour_labels = label_storm_objects(scaled_data, 'ew', self.model_ew.min_thresh, self.model_ew.max_thresh, min_area=self.size_filter, max_area=self.model_ew.max_size, max_range=self.model_ew.delta, increment=self.model_ew.data_increment, gaussian_sd=self.gaussian_window)
        model_objects.extend(extract_storm_patches(hour_labels, model_data, self.model_grid.x, self.model_grid.y, [hour], dx=self.model_grid.dx, patch_radius=self.patch_radius))
        for model_obj in model_objects[(- 1)]:
            dims = model_obj.timesteps[(- 1)].shape
            if (h > 0):
                model_obj.estimate_motion(hour, self.model_grid.data[(h - 1)], dims[1], dims[0])
        del scaled_data
        del model_data
        del hour_labels
    tracked_model_objects.extend(track_storms(model_objects, self.hours, self.object_matcher.cost_function_components, self.object_matcher.max_values, self.object_matcher.weights))
    self.model_ew.min_thresh = min_orig
    self.model_ew.max_thresh = max_orig
    self.model_ew.data_increment = data_increment_orig
    return tracked_model_objects

Identify storms in gridded model output and extract uniform sized patches around the storm centers of mass.

Returns:


def infer_channel_dimension_format(image: np.ndarray, num_channels: Optional[Union[int, tuple[int, ...]]]=None) -> ChannelDimension:
    num_channels = num_channels if num_channels is not None else (1, 3)
    num_channels = (num_channels,) if isinstance(num_channels, int) else num_channels
    if image.ndim == 3:
        first_dim, last_dim = (0, 2)
    elif image.ndim == 4:
        first_dim, last_dim = (1, 3)
    elif image.ndim == 5:
        first_dim, last_dim = (2, 4)
    else:
        raise ValueError(f'Unsupported number of image dimensions: {image.ndim}')
    if image.shape[first_dim] in num_channels and image.shape[last_dim] in num_channels:
        logger.warning(f'The channel dimension is ambiguous. Got image shape {image.shape}. Assuming channels are the first dimension. Use the [input_data_format](https:
        return ChannelDimension.FIRST
    elif image.shape[first_dim] in num_channels:
        return ChannelDimension.FIRST
    elif image.shape[last_dim] in num_channels:
        return ChannelDimension.LAST
    raise ValueError('Unable to infer channel dimension format')

Infers the channel dimension format of `image`.

Args:
image (`np.ndarray`):
The image to infer the channel dimension of.
num_channels (`int` or `Tuple[int, ...]`, *optional*, defaults to `(1, 3)`):
The number of channels of the image.

Returns:
The channel dimension of the image.


def delete_storage_account(access_token, subscription_id, rgname, account_name):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourcegroups/', rgname, '/providers/Microsoft.Storage/storageAccounts/', account_name, '?api-version=', STORAGE_API])
    return do_delete(endpoint, access_token)

Delete a storage account in the specified resource group.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
rgname (str): Azure resource group name.
account_name (str): Name of the new storage account.

Returns:
HTTP response.


def set_reprompt_ssml(self, ssml):

        self.response.reprompt.outputSpeech.type = 'SSML'
        self.response.reprompt.outputSpeech.ssml = ssml

Set response reprompt output speech as SSML type.

Args:
ssml: str. Response speech used when type is 'SSML', should be formatted
with Speech Synthesis Markup Language. Cannot exceed 8,000
characters.


def __init__(self, params, train):

    self.train = train
    self.params = params

    self.embedding_softmax_layer = embedding_layer.EmbeddingSharedWeights(
        params.vocab_size, params.hidden_size)
    self.encoder_stack = EncoderStack(params, train)
    self.decoder_stack = DecoderStack(params, train)

Initialize layers to build Transformer model.

Args:
params: hyperparameter object defining layer sizes, dropout values, etc.
train: boolean indicating whether the model is in training mode. Used to
determine if dropout layers should be added.


def price(self, market: pmd.ProcessedMarketData, name: Optional[str]=None):
    name = name or self._name + '_price'
    with tf.name_scope(name):
        pay_cf = self._pay_leg.price(market)
        receive_cf = self._receive_leg.price(market)
        return receive_cf - pay_cf

Returns the present value of the stream on the valuation date.

Args:
market: An instance of `ProcessedMarketData`.
name: Python str. The name to give to the ops created by this function.
Default value: `None` which maps to 'price'.

Returns:
A `Tensor` of shape `batch_shape`  containing the modeled price of each
IRS contract based on the input market data.


def get_variable_value_for_variation(self, variable, variation):

    if not variable or not variation:
      return None

    if variation.id not in self.variation_variable_usage_map:
      self.logger.error('Variation with ID "%s" is not in the datafile.' % variation.id)
      return None

    variable_usages = self.variation_variable_usage_map[variation.id]

    variable_usage = None
    if variable_usages:
      variable_usage = variable_usages.get(variable.id)

    if variable_usage:
      variable_value = variable_usage.value
      self.logger.info('Value for variable "%s" for variation "%s" is "%s".' % (
        variable.key,
        variation.key,
        variable_value
      ))

    else:
      variable_value = variable.defaultValue
      self.logger.info('Variable "%s" is not used in variation "%s". Assigning default value "%s".' % (
        variable.key,
        variation.key,
        variable_value
      ))

    return variable_value

Get the variable value for the given variation.

Args:
variable: The Variable for which we are getting the value.
variation: The Variation for which we are getting the variable value.

Returns:
The variable value or None if any of the inputs are invalid.


def _GetPlistRootKey(self, file_entry):

    file_object = file_entry.GetFileObject()

    try:
      plist_file = plist.PlistFile()
      plist_file.Read(file_object)

    except IOError as exception:
      location = getattr(file_entry.path_spec, 'location', '')
      raise errors.PreProcessFail(
          'Unable to read plist file: {0:s} with error: {1!s}'.format(
              location, exception))

    finally:
      file_object.close()

    return plist_file.root_key

Retrieves the root key of a plist file.

Args:
file_entry (dfvfs.FileEntry): file entry of the plist.

Returns:
dict[str, object]: plist root key.

Raises:
errors.PreProcessFail: if the preprocessing fails.


def load_variant(self, variant_obj):
    try:
        result = self.variant_collection.insert_one(variant_obj)
    except DuplicateKeyError as err:
        raise IntegrityError('Variant %s already exists in database', variant_obj['_id'])
    return result

Load a variant object

Args:
variant_obj(dict)

Returns:
inserted_id


def decompose(P):
    P = P.copy()
    if (not P):
        return P
    out = [Poly({key: P.A[key]}) for key in P.keys]
    return Poly(out, None, None, None)

Decompose a polynomial to component form.

In array missing values are padded with 0 to make decomposition compatible
with ``chaospy.sum(Q, 0)``.

Args:
P (Poly) : Input data.

Returns:
(Poly) : Decomposed polynomial with `P.shape==(M,)+Q.shape` where
`M` is the number of components in `P`.

Examples:
>>> q = cp.variable()
>>> P = cp.Poly([q**2-1, 2])
>>> print(P)
[q0^2-1, 2]
>>> print(cp.decompose(P))
[[-1, 2], [q0^2, 0]]
>>> print(cp.sum(cp.decompose(P), 0))
[q0^2-1, 2]


def forward(self, x, encoder_padding_mask, layer_head_mask, output_attentions=False):
    residual = x
    x, attn_weights = self.self_attn(query=x, key=x, key_padding_mask=encoder_padding_mask, layer_head_mask=layer_head_mask, output_attentions=output_attentions)
    x = nn.functional.dropout(x, p=self.dropout, training=self.training)
    x = residual + x
    x = self.self_attn_layer_norm(x)
    residual = x
    x = self.activation_fn(self.fc1(x))
    x = nn.functional.dropout(x, p=self.activation_dropout, training=self.training)
    x = self.fc2(x)
    x = nn.functional.dropout(x, p=self.dropout, training=self.training)
    x = residual + x
    x = self.final_layer_norm(x)
    return (x, attn_weights)

Args:
x (`torch.Tensor`): input to the layer of shape *(seq_len, batch, embed_dim)*
encoder_padding_mask (`torch.ByteTensor`): binary ByteTensor of shape
*(batch, src_len)* where padding elements are indicated by `1`.
for t_tgt, t_src is excluded (or masked out), =0 means it is
included in attention
layer_head_mask (`torch.FloatTensor`): mask for attention heads in a given layer of size
*(config.encoder_attention_heads,)*.

Returns:
encoded output of shape *(seq_len, batch, embed_dim)*


def create_model() -> tf.keras.Model:
    model = tf.keras.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10, activation='softmax')])
    return model

Create model for training.

Create a simple tf.keras model for training.

Returns:
The model to use for training.


def addUrlScheme(self, url):
    if (not isinstance(url, str)):
        raise TypeError('url must be a string value')
    if (not (url in self._urlSchemes)):
        self._urlSchemes[url] = OEmbedUrlScheme(url)

Add a url scheme to this endpoint. It takes a url string and create
the OEmbedUrlScheme object internally.

Args:
url: The url string that represents a url scheme to add.


def _get_ngrams(n, text):

  ngram_set = set()
  text_length = len(text)
  max_index_ngram_start = text_length - n
  for i in range(max_index_ngram_start + 1):
    ngram_set.add(tuple(text[i:i + n]))
  return ngram_set

Calculates n-grams.

Args:
n: which n-grams to calculate
text: An array of tokens

Returns:
A set of n-grams


def post_headline(self, name, level, message):

        self.post_command(OPERATIONS.CMD_SET_HEADLINE,
                          {'name': name, 'level': level, 'message': message})

Asynchronously update the sticky headline for a service.

Args:
name (string): The name of the service
level (int): A message level in states.*_LEVEL
message (string): The user facing error message that will be stored
for the service and can be queried later.


def get_layer_opt(self, lrs, wds):
    return LayerOptimizer(self.opt_fn, self.get_layer_groups(), lrs, wds)

Method returns an instance of the LayerOptimizer class, which
allows for setting differential learning rates for different
parts of the model.

An example of how a model maybe differentiated into different parts
for application of differential learning rates and weight decays is
seen in ../.../courses/dl1/fastai/conv_learner.py, using the dict
'model_meta'. Currently, this seems supported only for convolutional
networks such as VGG-19, ResNet-XX etc.

Args:
lrs (float or list(float)): learning rate(s) for the model

wds (float or list(float)): weight decay parameter(s).

Returns:
An instance of a LayerOptimizer


def ExtractEvents(
      self, parser_mediator, registry_key, codepage='cp1252', **kwargs):

    self._ParseMRUListExKey(parser_mediator, registry_key, codepage=codepage)

Extracts events from a Windows Registry key.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
registry_key (dfwinreg.WinRegistryKey): Windows Registry key.
codepage (Optional[str]): extended ASCII string codepage.


def list_types_poi(self, **kwargs):

        url_args = {'language': util.language_code(kwargs.get('lang'))}

        result = self.make_request('list_poi_types', url_args)

        if not util.check_result(result):
            return False, result.get('message', 'UNKNOWN ERROR')

        values = util.response_list(result, 'Data')
        return True, [emtype.ParkingPoiType(**a) for a in values]

Obtain a list of families, types and categories of POI.

Args:
lang (str): Language code (*es* or *en*).

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


def parse_ensembl_line(line, header):

    line = line.rstrip().split('\t')
    header = [head.lower() for head in header]
    raw_info = dict(zip(header, line))

    ensembl_info = {}

    for word in raw_info:
        value = raw_info[word]

        if not value:
            continue

        if 'chromosome' in word:
            ensembl_info['chrom'] = value

        if 'gene' in word:
            if 'id' in word:
                ensembl_info['ensembl_gene_id'] = value
            elif 'start' in word:
                ensembl_info['gene_start'] = int(value)
            elif 'end' in word:
                ensembl_info['gene_end'] = int(value)

        if 'hgnc symbol' in word:
            ensembl_info['hgnc_symbol'] = value
        if "gene name" in word:
            ensembl_info['hgnc_symbol'] = value

        if 'hgnc id' in word:
            ensembl_info['hgnc_id'] = int(value.split(':')[-1])

        if 'transcript' in word:
            if 'id' in word:
                ensembl_info['ensembl_transcript_id'] = value
            elif 'start' in word:
                ensembl_info['transcript_start'] = int(value)
            elif 'end' in word:
                ensembl_info['transcript_end'] = int(value)

        if 'exon' in word:
            if 'start' in word:
                ensembl_info['exon_start'] = int(value)
            elif 'end' in word:
                ensembl_info['exon_end'] = int(value)
            elif 'rank' in word:
                ensembl_info['exon_rank'] = int(value)

        if 'utr' in word:

            if 'start' in word:
                if '5' in word:
                    ensembl_info['utr_5_start'] = int(value)
                elif '3' in word:
                    ensembl_info['utr_3_start'] = int(value)
            elif 'end' in word:
                if '5' in word:
                    ensembl_info['utr_5_end'] = int(value)
                elif '3' in word:
                    ensembl_info['utr_3_end'] = int(value)

        if 'strand' in word:
            ensembl_info['strand'] = int(value)

        if 'refseq' in word:
            if 'mrna' in word:
                if 'predicted' in word:
                    ensembl_info['refseq_mrna_predicted'] = value
                else:
                    ensembl_info['refseq_mrna'] = value

            if 'ncrna' in word:
                ensembl_info['refseq_ncrna'] = value

    return ensembl_info

Parse an ensembl formated line

Args:
line(list): A list with ensembl gene info
header(list): A list with the header info

Returns:
ensembl_info(dict): A dictionary with the relevant info


def changed(self, path_info, checksum_info):
    logger.debug("checking if '{}'('{}') has changed.".format(path_info, checksum_info))
    if (not self.exists(path_info)):
        logger.debug("'{}' doesn't exist.".format(path_info))
        return True
    checksum = checksum_info.get(self.PARAM_CHECKSUM)
    if (checksum is None):
        logger.debug("checksum for '{}' is missing.".format(path_info))
        return True
    if self.changed_cache(checksum):
        logger.debug("cache for '{}'('{}') has changed.".format(path_info, checksum))
        return True
    actual = self.save_info(path_info)[self.PARAM_CHECKSUM]
    if (checksum != actual):
        logger.debug("checksum '{}'(actual '{}') for '{}' has changed.".format(checksum, actual, path_info))
        return True
    logger.debug("'{}' hasn't changed.".format(path_info))
    return False

Checks if data has changed.

A file is considered changed if:
- It doesn't exist on the working directory (was unlinked)
- Checksum is not computed (saving a new file)
- The checkusm stored in the State is different from the given one
- There's no file in the cache

Args:
path_info: dict with path information.
checksum: expected checksum for this data.

Returns:
bool: True if data has changed, False otherwise.


def update_pipeline_field(self, pipeline_key, field):

		uri = '/'.join([
						self.api_uri,
						self.pipelines_suffix,
						pipeline_key,
						self.fields_suffix
						])
		return self._update_field(uri, field)

Upates pipeline field as specified
Args:
pipeline_key		key for pipeline where the fields lives
field 				StreakField object with fresh data
returns				(status code, updated field dict)


def validate_addr(self, address, id=None, endpoint=None):
    return self._call_endpoint(VALIDATE_ADDR, params=[address], id=id, endpoint=endpoint)

returns whether or not addr string is valid

Args:
address: (str) address to lookup ( in format 'AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK')
id: (int, optional) id to use for response tracking
endpoint: (RPCEndpoint, optional) endpoint to specify to use

Returns:
json object of the result or the error encountered in the RPC call


def _process_name_or_alias_filter_directive(filter_operation_info, location, context, parameters):
    filtered_field_type = filter_operation_info.field_type
    if isinstance(filtered_field_type, GraphQLUnionType):
        raise GraphQLCompilationError(u'Cannot apply "name_or_alias" to union type {}'.format(filtered_field_type))
    current_type_fields = filtered_field_type.fields
    name_field = current_type_fields.get('name', None)
    alias_field = current_type_fields.get('alias', None)
    if ((not name_field) or (not alias_field)):
        raise GraphQLCompilationError(u'Cannot apply "name_or_alias" to type {} because it lacks a "name" or "alias" field.'.format(filtered_field_type))
    name_field_type = strip_non_null_from_type(name_field.type)
    alias_field_type = strip_non_null_from_type(alias_field.type)
    if (not isinstance(name_field_type, GraphQLScalarType)):
        raise GraphQLCompilationError(u'Cannot apply "name_or_alias" to type {} because its "name" field is not a scalar.'.format(filtered_field_type))
    if (not isinstance(alias_field_type, GraphQLList)):
        raise GraphQLCompilationError(u'Cannot apply "name_or_alias" to type {} because its "alias" field is not a list.'.format(filtered_field_type))
    alias_field_inner_type = strip_non_null_from_type(alias_field_type.of_type)
    if (alias_field_inner_type != name_field_type):
        raise GraphQLCompilationError(u'Cannot apply "name_or_alias" to type {} because the "name" field and the inner type of the "alias" field do not match: {} vs {}'.format(filtered_field_type, name_field_type, alias_field_inner_type))
    argument_inferred_type = name_field_type
    (argument_expression, non_existence_expression) = _represent_argument(location, context, parameters[0], argument_inferred_type)
    check_against_name = expressions.BinaryComposition(u'=', expressions.LocalField('name'), argument_expression)
    check_against_alias = expressions.BinaryComposition(u'contains', expressions.LocalField('alias'), argument_expression)
    filter_predicate = expressions.BinaryComposition(u'||', check_against_name, check_against_alias)
    if (non_existence_expression is not None):
        filter_predicate = expressions.BinaryComposition(u'||', non_existence_expression, filter_predicate)
    return blocks.Filter(filter_predicate)

Return a Filter basic block that checks for a match against an Entity's name or alias.

Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
context: dict, various per-compilation data (e.g. declared tags, whether the current block
is optional, etc.). May be mutated in-place in this function!
parameters: list of 1 element, containing the value to check the name or alias against;
if the parameter is optional and missing, the check will return True

Returns:
a Filter basic block that performs the check against the name or alias


async def tag(self, name: str, repo: str, *, tag: str = None) -> bool:

        params = {"repo": repo}

        if tag:
            params["tag"] = tag

        await self.docker._query(
            "images/{name}/tag".format(name=name),
            "POST",
            params=params,
            headers={"content-type": "application/json"},
        )
        return True

Tag the given image so that it becomes part of a repository.

Args:
repo: the repository to tag in
tag: the name for the new tag


def get_meta_graph_def(saved_model_dir, tag_set):
    saved_model = read_saved_model(saved_model_dir)
    set_of_tags = set([tag for tag in tag_set.split(',') if tag])
    valid_tags = []
    for meta_graph_def in saved_model.meta_graphs:
        meta_graph_tags = set(meta_graph_def.meta_info_def.tags)
        if meta_graph_tags == set_of_tags:
            return meta_graph_def
        else:
            valid_tags.append(','.join(meta_graph_tags))
    raise RuntimeError(f'MetaGraphDef associated with tag-set {tag_set} could not be found in the SavedModel. Please use one of the following tag-sets: {valid_tags}')

Gets MetaGraphDef from SavedModel.

Returns the MetaGraphDef for the given tag-set and SavedModel directory.

Args:
saved_model_dir: Directory containing the SavedModel to inspect.
tag_set: Group of tag(s) of the MetaGraphDef to load, in string format,
separated by ','. The empty string tag is ignored so that passing ''
means the empty tag set. For tag-set contains multiple tags, all tags
must be passed in.

Raises:
RuntimeError: An error when the given tag-set does not exist in the
SavedModel.

Returns:
A MetaGraphDef corresponding to the tag-set.


def check_models_are_tested(module: types.ModuleType, test_file: str) -> List[str]:
    defined_models = get_models(module)
    tested_models = find_tested_models(test_file)
    if tested_models is None:
        if test_file.replace(os.path.sep, '/') in TEST_FILES_WITH_NO_COMMON_TESTS:
            return
        return [f'{test_file} should define `all_model_classes` to apply common tests to the models it tests. ' + 'If this intentional, add the test filename to `TEST_FILES_WITH_NO_COMMON_TESTS` in the file ' + '`utils/check_repo.py`.']
    failures = []
    for model_name, _ in defined_models:
        if model_name not in tested_models and should_be_tested(model_name):
            failures.append(f'{model_name} is defined in {module.__name__} but is not tested in ' + f'{os.path.join(PATH_TO_TESTS, test_file)}. Add it to the all_model_classes in that file.' + 'If common tests should not applied to that model, add its name to `IGNORE_NON_TESTED`' + 'in the file `utils/check_repo.py`.')
    return failures

Check models defined in a module are all tested in a given file.

Args:
module (`types.ModuleType`): The module in which we get the models.
test_file (`str`): The path to the file where the module is tested.

Returns:
`List[str]`: The list of error messages corresponding to models not tested.


def predict(self, version_name, data):
    full_version_name = ('%s/versions/%s' % (self._full_model_name, version_name))
    request = self._api.projects().predict(body={'instances': data}, name=full_version_name)
    request.headers['user-agent'] = 'GoogleCloudDataLab/1.0'
    result = request.execute()
    if ('predictions' not in result):
        raise Exception('Invalid response from service. Cannot find "predictions" in response.')
    return result['predictions']

Get prediction results from features instances.

Args:
version_name: the name of the version used for prediction.
data: typically a list of instance to be submitted for prediction. The format of the
instance depends on the model. For example, structured data model may require
a csv line for each instance.
Note that online prediction only works on models that take one placeholder value,
such as a string encoding a csv line.
Returns:
A list of prediction results for given instances. Each element is a dictionary representing
output mapping from the graph.
An example:
[{"predictions": 1, "score": [0.00078, 0.71406, 0.28515]},
{"predictions": 1, "score": [0.00244, 0.99634, 0.00121]}]


def forward(self, g_values: torch.Tensor, mask: torch.Tensor, labels: Optional[torch.Tensor]=None, loss_batch_weight=1, return_dict=False) -> BayesianWatermarkDetectorModelOutput:
    likelihoods_watermarked = self.likelihood_model_watermarked(g_values)
    likelihoods_unwatermarked = 0.5 * torch.ones_like(g_values)
    out = self._compute_posterior(likelihoods_watermarked=likelihoods_watermarked, likelihoods_unwatermarked=likelihoods_unwatermarked, mask=mask, prior=self.prior)
    loss = None
    if labels is not None:
        loss_fct = BCELoss()
        loss_unwweight = torch.sum(self.likelihood_model_watermarked.delta ** 2)
        loss_weight = loss_unwweight * loss_batch_weight
        loss = loss_fct(torch.clamp(out, 1e-05, 1 - 1e-05), labels) + loss_weight
    if not return_dict:
        return (out,) if loss is None else (out, loss)
    return BayesianWatermarkDetectorModelOutput(loss=loss, posterior_probabilities=out)

Computes the watermarked posterior P(watermarked|g_values).

Args:
g_values (`torch.Tensor` of shape `(batch_size, seq_len, watermarking_depth, ...)`):
g-values (with values 0 or 1)
mask:
A binary array shape [batch_size, seq_len] indicating which g-values should be used. g-values with mask
value 0 are discarded.

Returns:
p(watermarked | g_values), of shape [batch_size].


def _prepare_babi_data(tmp_dir, data_dir):

  if not tf.gfile.Exists(data_dir):
    tf.gfile.MakeDirs(data_dir)

  file_path = os.path.join(tmp_dir, _TAR)
  headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) "
                           "AppleWebKit/537.36 (KHTML, like Gecko) "
                           "Chrome/63.0.3239.132 Safari/537.36"}
  resp = requests.get(_URL, headers=headers)
  with open(file_path, "wb") as f:
    f.write(resp.content)

  tar = tarfile.open(file_path)
  tar.extractall(tmp_dir)
  tar.close()

  return tmp_dir

Downloads and extracts the dataset.

Args:
tmp_dir: temp directory to download and extract the dataset
data_dir: The base directory where data and vocab files are stored.

Returns:
tmp_dir: temp directory containing the raw data.


def extractUnits(self, inp):

        inp = self._preprocess(inp)

        units = []
        description = ""
        for w in inp.split(' '):
            if self.isValidUnit(w) or w == '/':
                if description:
                    description += " "
                description += w
            else:
                if description:
                    units.append(description)
                description = ""

        if description:
            units.append(description)
        return units

Collects all the valid units from an inp string. Works by
appending consecutive words from the string and cross-referncing
them with a set of valid units.

Args:
inp (str): Some text which hopefully contains descriptions
of different units.

Returns:
A list of strings, each entry in which is a valid quantities
unit.


def get_servo_position(self):

        data = []
        data.append(0x09)
        data.append(self.servoid)
        data.append(RAM_READ_REQ)
        data.append(CALIBRATED_POSITION_RAM)
        data.append(BYTE2)
        send_data(data)
        rxdata = []
        try:
            rxdata = SERPORT.read(13)
            if (self.servomodel==0x06) or (self.servomodel == 0x04):
                return ((ord(rxdata[10])&0xff)<<8) | (ord(rxdata[9])&0xFF)
            else:

                return ((ord(rxdata[10])&0x03)<<8) | (ord(rxdata[9])&0xFF)

        except HerkulexError:
            print "Could not read from the servos. Check connection"

Gets the current position of Herkulex

Args:
none

Returns:
int: position of the servo- 0 to 1023

Raises:
SerialException: Error occured while opening serial port


def broadcast_recv(shape, dtype, group_size, group_key, instance_key, communication_hint='auto', timeout=0):
    if group_size <= 1:
        raise ValueError(f'Parameter `group_size` to broadcast_send must be at least 2. Received: {group_size}.')
    return gen_collective_ops.collective_bcast_recv(shape=shape, T=dtype, group_size=group_size, group_key=group_key, instance_key=instance_key, communication_hint=communication_hint.lower(), timeout_seconds=timeout)

Receives a broadcasts tensor, across devices.

Args:
shape: Shape of the tensor to be received.
dtype: Type of the tensor to be received.
group_size: one plus the number of receiving tensors, i.e. the total
number of devices participating.  Each tensor must reside on a
different device.
group_key: an integer identifying the group of devices.
instance_key: an integer identifying the participating group of Ops.
communication_hint: preferred collective communication.  The implementation
may fall back to another mechanism.  Options include `auto`, `ring`, and
`nccl`.
timeout: If set to a non zero, set a completion timeout to detect staleness.
If the timer goes off, a DeadlineExceededError is raised.
The timeout value in seconds. This feature is experimental.

Returns:
An Op implementing the broadcast receive.

Raises:
ValueError: if any of the input parameter constraints are not met.


def make_full_document(text, title=None, preamp_decl={}, preamb_extra=None):
    import utool as ut
    doc_preamb = ut.codeblock('\n    %\\documentclass{article}\n    \\documentclass[10pt,twocolumn,letterpaper]{article}\n    % \\usepackage[utf8]{inputenc}\n    \\usepackage[T1]{fontenc}\n\n    \\usepackage{times}\n    \\usepackage{epsfig}\n    \\usepackage{graphicx}\n    \\usepackage{amsmath,amsthm,amssymb}\n    \\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}\n    \\usepackage{multirow}\n    \\usepackage{subcaption}\n    \\usepackage{booktabs}\n\n    %\\pagenumbering{gobble}\n    ')
    if (preamb_extra is not None):
        if isinstance(preamb_extra, (list, tuple)):
            preamb_extra = '\n'.join(preamb_extra)
        doc_preamb += (('\n' + preamb_extra) + '\n')
    if (title is not None):
        preamp_decl['title'] = title
    decl_lines = ['\\{key}{{{val}}}'.format(key=key, val=val) for (key, val) in preamp_decl.items()]
    doc_decllines = '\n'.join(decl_lines)
    doc_header = ut.codeblock('\n        \\begin{document}\n        ')
    if (preamp_decl.get('title') is not None):
        doc_header += '\\maketitle'
    doc_footer = ut.codeblock('\n        \\end{document}\n        ')
    text_ = '\n'.join((doc_preamb, doc_decllines, doc_header, text, doc_footer))
    return text_

r"""
dummy preamble and document to wrap around latex fragment

Args:
text (str):
title (str):

Returns:
str: text_

CommandLine:
python -m utool.util_latex --test-make_full_document

Example:
>>> # DISABLE_DOCTEST
>>> from utool.util_latex import *  # NOQA
>>> text = 'foo'
>>> title = 'title'
>>> preamp_decl = {}
>>> text_ = make_full_document(text, title)
>>> result = str(text_)
>>> print(result)


def define_batch_env(constructor, num_agents, env_processes):
    with tf.variable_scope('environments'):
        if env_processes:
            envs = [tools.wrappers.ExternalProcess(constructor) for _ in range(num_agents)]
        else:
            envs = [constructor() for _ in range(num_agents)]
        batch_env = tools.BatchEnv(envs, blocking=(not env_processes))
        batch_env = tools.InGraphBatchEnv(batch_env)
    return batch_env

Create environments and apply all desired wrappers.

Args:
constructor: Constructor of an OpenAI gym environment.
num_agents: Number of environments to combine in the batch.
env_processes: Whether to step environment in external processes.

Returns:
In-graph environments object.


def _load_methods(package):

    global _methods
    _methods[package] = None

    from acorn.config import settings
    from acorn.logging.descriptors import _obj_getattr
    spack = settings(package)
    if spack is not None:
        if spack.has_section("analysis.methods"):
            _methods[package] = {}

            from importlib import import_module
            mappings = dict(spack.items("analysis.methods"))
            for fqdn, target in mappings.items():
                rootname = target.split('.')[0]
                root = import_module(rootname)
                caller = _obj_getattr(root, target)
                _methods[package][fqdn] = caller

Loads the mappings from method call result to analysis.

Args:
package (str): name of the package to load for.


def _binary_2d_label_to_1d_sparse_value(labels):
    indices = []
    values = []
    batch = 0
    for row in labels:
        label = 0
        xi = 0
        for x in row:
            if x == 1:
                indices.append([batch])
                values.append(label)
                xi += 1
            else:
                assert x == 0
            label += 1
        batch += 1
    if indices != [[i] for i in range(len(labels))]:
        raise ValueError('Expected 1 label/example, got %s.' % indices)
    shape = [len(labels)]
    return sparse_tensor.SparseTensorValue(np.array(indices, np.int64), np.array(values, np.int64), np.array(shape, np.int64))

Convert dense 2D binary indicator to sparse ID.

Only 1 values in `labels` are included in result.

Args:
labels: Dense 2D binary indicator, shape [batch_size, num_classes]. Each
row must contain exactly 1 `1` value.

Returns:
`SparseTensorValue` of shape [batch_size]. Values are indices of `1` values
along the last dimension of `labels`.

Raises:
ValueError: if there is not exactly 1 `1` value per row of `labels`.


def allreduce_ring(xs, devices, reduction_fn_string="SUM"):

  n = len(xs)
  if len(devices) != n:
    raise ValueError("devices must be a list of length len(xs)")
  if n == 1:
    return xs
  shape = xs[0].shape.as_list()

  size = None if None in shape else mtf.list_product(shape)
  if size is None or size < 1024 or size % n != 0:
    return allreduce_ring_single_shard(xs, devices, reduction_fn_string)

  def _circular_shift(l, n):
    n %= len(l)
    return l[-n:] + l[:-n]
  def _flatten_and_split(x):

    return tf.split(tf.reshape(x, [-1]), n)
  def _concat_and_reshape(xs):
    return tf.reshape(tf.concat(xs, 0), shape)

  x_split = mtf.parallel(devices, _flatten_and_split, xs)
  x_split_t = mtf.transpose_list_of_lists(x_split)

  y_split_t = []
  for shard in xrange(n):
    shard_xs = _circular_shift(x_split_t[shard], shard)
    shard_devices = _circular_shift(devices, shard)
    shard_ys = allreduce_ring_single_shard(
        shard_xs, shard_devices, reduction_fn_string)
    y_split_t.append(_circular_shift(shard_ys, -shard))
  y_split = mtf.transpose_list_of_lists(y_split_t)
  ys = mtf.parallel(devices, _concat_and_reshape, y_split)
  return ys

Compute the reduction of all Tensors and put the result everywhere.

Performance-optimized for a ring of devices.

Args:
xs: a list of n tf.Tensors
devices: a list of strings
reduction_fn_string: "SUM" or "MAX"

Returns:
a list of n Tensors
Raises:
ValueError: if devices is not a list of n strings


def to_view(self, view_name):

    from . import _view
    return _view.View(view_name, self._context).create(self._sql)

Create a View from this Query.

Args:
view_name: the name of the View either as a string or a 3-part tuple
(projectid, datasetid, name).

Returns:
A View for the Query.


def __init__(self, type, data):

        if not isinstance(type, int):
            raise TypeError("ext type is not type integer")

        elif sys.version_info[0] == 3 and not isinstance(data, bytes):
            raise TypeError("ext data is not type \'bytes\'")
        elif sys.version_info[0] == 2 and not isinstance(data, str):
            raise TypeError("ext data is not type \'str\'")
        self.type = type
        self.data = data

Construct a new Ext object.

Args:
type: application-defined type integer
data: application-defined data byte array

Example:
>>> foo = umsgpack.Ext(0x05, b"\x01\x02\x03")
>>> umsgpack.packb({u"special stuff": foo, u"awesome": True})
'\x82\xa7awesome\xc3\xadspecial stuff\xc7\x03\x05\x01\x02\x03'
>>> bar = umsgpack.unpackb(_)
>>> print(bar["special stuff"])
Ext Object (Type: 0x05, Data: 01 02 03)
>>>


def __init__(
            self, summary=AverageSummary, alpha=1,
            credibility=WeightedCredibility, reviewer=Reviewer, product=Product):

        self.alpha = alpha
        self.graph = nx.DiGraph()
        self.reviewers = []
        self.products = []

        self._summary_cls = summary
        self._review_cls = summary.review_class()

        self.credibility = credibility(self)
        self._reviewer_cls = reviewer
        self._product_cls = product

Construct bipartite graph.

Args:
summary_type: specify summary type class, default value is AverageSummary.
alpha: used to compute weight of anomalous scores, default value is 1.
credibility: credibility class to be used in this graph.
(Default: WeightedCredibility)
reviewer: Class of reviewers.
product: Class of products.


def take_grad(self, num_required, name=None):
    return gen_data_flow_ops.sparse_accumulator_take_gradient(self._accumulator_ref, num_required, dtype=self._dtype, name=name)

Attempts to extract the average gradient from the accumulator.

The operation blocks until sufficient number of gradients have been
successfully applied to the accumulator.

Once successful, the following actions are also triggered:
- Counter of accumulated gradients is reset to 0.
- Aggregated gradient is reset to 0 tensor.
- Accumulator's internal time step is incremented by 1.

Args:
num_required: Number of gradients that needs to have been aggregated
name: Optional name for the operation

Returns:
A tuple of indices, values, and shape representing the average gradient.

Raises:
InvalidArgumentError: If `num_required` < 1


def set_keras_mask(x, mask):
    set_tensor_attr(x, '_keras_mask', mask)

Sets the Keras mask attribute for the given tensor in-place.

Args:
x: Input tensor.
mask: The mask tensor to be set. If `None`, the `_keras_mask` attribute
will be cleared.


def _CreateCommentsFromPrefix(comment_prefix, comment_lineno, comment_column, standalone=False):
    comments = []
    lines = comment_prefix.split('\n')
    index = 0
    while index < len(lines):
        comment_block = []
        while index < len(lines) and lines[index].lstrip().startswith('
            comment_block.append(lines[index].strip())
            index += 1
        if comment_block:
            new_lineno = comment_lineno + index - 1
            comment_block[0] = comment_block[0].strip()
            comment_block[-1] = comment_block[-1].strip()
            comment_leaf = pytree.Leaf(type=token.COMMENT, value='\n'.join(comment_block), context=('', (new_lineno, comment_column)))
            comment_node = comment_leaf if not standalone else pytree.Node(pygram.python_symbols.simple_stmt, [comment_leaf])
            comments.append(comment_node)
        while index < len(lines) and (not lines[index].lstrip()):
            index += 1
    return comments

Create pytree nodes to represent the given comment prefix.

Args:
comment_prefix: (unicode) the text of the comment from the node's prefix.
comment_lineno: (int) the line number for the start of the comment.
comment_column: (int) the column for the start of the comment.
standalone: (bool) determines if the comment is standalone or not.

Returns:
The simple_stmt nodes if this is a standalone comment, otherwise a list of
new COMMENT leafs. The prefix may consist of multiple comment blocks,
separated by blank lines. Each block gets its own leaf.


def set_symbols(self, symbols, functional=None, sym_potcar_map=None):
    del self[:]
    if sym_potcar_map:
        for el in symbols:
            self.append(PotcarSingle(sym_potcar_map[el]))
    else:
        for el in symbols:
            p = PotcarSingle.from_symbol_and_functional(el, functional)
            self.append(p)

Initialize the POTCAR from a set of symbols. Currently, the POTCARs can
be fetched from a location specified in .pmgrc.yaml. Use pmg config
to add this setting.

Args:
symbols ([str]): A list of element symbols
functional (str): The functional to use. If None, the setting
PMG_DEFAULT_FUNCTIONAL in .pmgrc.yaml is used, or if this is
not set, it will default to PBE.
sym_potcar_map (dict): A map of symbol:raw POTCAR string. If
sym_potcar_map is specified, POTCARs will be generated from
the given map data rather than the config file location.


def from_schema(cls, schema: class_schema.Schema, module_name: str, name: str, qualname: Optional[str]=None, is_method: bool=True) -> 'Signature':
    arg_names = list(schema.metadata.get('init_arg_list', []))
    if arg_names and arg_names[-1].startswith('*'):
        vararg_name = arg_names[-1][1:]
        arg_names.pop(-1)
    else:
        vararg_name = None

    def get_arg_spec(arg_name):
        field = schema.get_field(arg_name)
        if not field:
            raise ValueError(f'Argument {arg_name!r} is not a symbolic field.')
        return field.value
    args = []
    if is_method:
        args.append(Argument.from_annotation('self', Argument.Kind.POSITIONAL_OR_KEYWORD))
    args.extend([Argument(n, Argument.Kind.POSITIONAL_OR_KEYWORD, get_arg_spec(n)) for n in arg_names])
    varargs = None
    if vararg_name:
        varargs = Argument(vararg_name, Argument.Kind.VAR_POSITIONAL, get_arg_spec(vararg_name))
    existing_names = set(arg_names)
    if vararg_name:
        existing_names.add(vararg_name)
    kwonlyargs = []
    varkw = None
    for key, field in schema.fields.items():
        if key not in existing_names and (not field.frozen):
            if key.is_const:
                kwonlyargs.append(Argument(str(key), Argument.Kind.KEYWORD_ONLY, field.value))
            else:
                varkw = Argument(schema.metadata.get('varkw_name', None) or 'kwargs', Argument.Kind.VAR_KEYWORD, class_schema.ValueSpec.DictType(field.value))
    return Signature(callable_type=CallableType.FUNCTION, name=name, module_name=module_name, qualname=qualname, description=schema.description, args=args, kwonlyargs=kwonlyargs, varargs=varargs, varkw=varkw, return_value=schema.metadata.get('returns', None))

Creates a signature from a schema object.

Args:
schema: A `pg.typing.Schema` object associated with a `pg.Object`.
module_name: Module name for the signature.
name: Function or method name of the signature.
qualname: Qualname of the signature.
is_method: If True, `self` will be added in the signature as the first
argument.

Returns:
A signature object from the schema.


def index(self, text, terms=None, **kwargs):

        self.clear()

        terms = terms or text.terms.keys()

        pairs = combinations(terms, 2)
        count = comb(len(terms), 2)

        for t1, t2 in bar(pairs, expected_size=count, every=1000):

            score = text.score_braycurtis(t1, t2, **kwargs)
            self.set_pair(t1, t2, score)

Index all term pair distances.

Args:
text (Text): The source text.
terms (list): Terms to index.


def assert_call(self, expected, left, right):
    name_map = {left: 'left', right: 'right'}
    node, result = self._is_instance.call(self._node, None, function.Args((left, right), self.new_dict(), None, None))
    self.assertIn(node, self._node.outgoing)
    result_map = {}
    for b in result.bindings:
        terms = set()
        for o in b.origins:
            self.assertEqual(node, o.where)
            for sources in o.source_sets:
                terms.add(' '.join(sorted(('%s:%d' % (name_map[b.variable], b.variable.bindings.index(b)) for b in sources))))
        result_map[b.data] = terms
    self.assertEqual(expected, result_map)

Check that call() returned the desired results.

Args:
expected: A dict from values to source sets, where a source set is
represented by the sorted binding names separated by spaces, for example
"left:0 right:1" would indicate binding #0 of variable "left" and
binding #1 of variable "right".
left: A Variable to use as the first arg to call().
right: A Variable to use as the second arg to call().


def upper_bound(fm, nr_subs=None, scale_factor=1):
    nr_subs_total = len(np.unique(fm.SUBJECTINDEX))
    if (not nr_subs):
        nr_subs = (nr_subs_total - 1)
    assert (nr_subs < nr_subs_total)
    intersub_scores = []
    for measure in range(len(measures.scores)):
        res_dict = {}
        result_vectors = [(np.empty(nr_subs_total) + np.nan) for _ in np.unique(fm.category)]
        res_dict.update(list(zip(np.unique(fm.category), result_vectors)))
        intersub_scores.append(res_dict)
    for fm_cat in fm.by_field('category'):
        cat = fm_cat.category[0]
        for (sub_counter, sub) in enumerate(np.unique(fm_cat.SUBJECTINDEX)):
            image_scores = []
            for fm_single in fm_cat.by_field('filenumber'):
                predicting_subs = np.setdiff1d(np.unique(fm_single.SUBJECTINDEX), [sub])
                np.random.shuffle(predicting_subs)
                predicting_subs = predicting_subs[0:nr_subs]
                predicting_fm = fm_single[ismember(fm_single.SUBJECTINDEX, predicting_subs)]
                predicted_fm = fm_single[(fm_single.SUBJECTINDEX == sub)]
                try:
                    predicting_fdm = compute_fdm(predicting_fm, scale_factor=scale_factor)
                except RuntimeError:
                    predicting_fdm = None
                image_scores.append(measures.prediction_scores(predicting_fdm, predicted_fm))
            for (measure, score) in enumerate(nanmean(image_scores, 0)):
                intersub_scores[measure][cat][sub_counter] = score
    return intersub_scores

compute the inter-subject consistency upper bound for a fixmat.

Input:
fm : a fixmat instance
nr_subs : the number of subjects used for the prediction. Defaults
to the total number of subjects in the fixmat minus 1
scale_factor : the scale factor of the FDMs. Default is 1.
Returns:
A list of scores; the list contains one dictionary for each measure.
Each dictionary contains one key for each category and corresponding
values is an array with scores for each subject.


def parse_line(self, line):
    toks = shlex.split(line)
    return (toks[0], ([] if (len(toks) == 1) else toks[1:]))

Parse a line of input.

The input line is tokenized using the same rules as the way bash shell
tokenizes inputs. All quoting and escaping rules from the bash shell
apply here too.

The following cases are handled by __exec_line__():
1.  Empty line.
2.  The input line is completely made of whitespace characters.
3.  The input line is the EOF character.
4.  The first token, as tokenized by shlex.split(), is '!'.
5.  Internal commands, i.e., commands registered with internal =
True

Arguments:
The line to parse.

Returns:
A tuple (cmd, args). The first element cmd must be a python3 string.
The second element is, by default, a list of strings representing
the arguments, as tokenized by shlex.split().

How to overload parse_line():
1.  The signature of the method must be the same.
2.  The return value must be a tuple (cmd, args), where the cmd is
a string representing the first token, and args is a list of
strings.


def __get_favorites(self, favorite_type, start=0, max_items=100):

        if favorite_type not in (RADIO_SHOWS, RADIO_STATIONS):
            favorite_type = SONOS_FAVORITES

        response = self.contentDirectory.Browse([
            ('ObjectID',
             'FV:2' if favorite_type is SONOS_FAVORITES
             else 'R:0/{0}'.format(favorite_type)),
            ('BrowseFlag', 'BrowseDirectChildren'),
            ('Filter', '*'),
            ('StartingIndex', start),
            ('RequestedCount', max_items),
            ('SortCriteria', '')
        ])
        result = {}
        favorites = []
        results_xml = response['Result']

        if results_xml != '':

            metadata = XML.fromstring(really_utf8(results_xml))

            for item in metadata.findall(
                    '{urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/}container'
                    if favorite_type == RADIO_SHOWS else
                    '{urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/}item'):
                favorite = {}
                favorite['title'] = item.findtext(
                    '{http:
                favorite['uri'] = item.findtext(
                    '{urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/}res')
                if favorite_type == SONOS_FAVORITES:
                    favorite['meta'] = item.findtext(
                        '{urn:schemas-rinconnetworks-com:metadata-1-0/}resMD')
                favorites.append(favorite)

        result['total'] = response['TotalMatches']
        result['returned'] = len(favorites)
        result['favorites'] = favorites

        return result

Helper method for `get_favorite_radio_*` methods.

Args:
favorite_type (str): Specify either `RADIO_STATIONS` or
`RADIO_SHOWS`.
start (int): Which number to start the retrieval from. Used for
paging.
max_items (int): The total number of results to return.


def Contains(self, value):
    self._awql = self._CreateSingleValueCondition(value, 'CONTAINS')
    return self._query_builder

Sets the type of the WHERE clause as "contains".

Args:
value: The value to be used in the WHERE condition.

Returns:
The query builder that this WHERE builder links to.


def ToDebugString(self, indentation_level=1):

    indentation = '  ' * indentation_level

    text_parts = ['{0:s}path segment index: {1:d}\n'.format(
        indentation, self.path_segment_index)]

    for path_segment, scan_object in self._path_segments.items():
      text_parts.append('{0:s}path segment: {1:s}\n'.format(
          indentation, path_segment))

      if isinstance(scan_object, PathFilterScanTreeNode):
        text_parts.append('{0:s}scan tree node:\n'.format(indentation))
        text_parts.append(scan_object.ToDebugString(indentation_level + 1))

      elif isinstance(scan_object, py2to3.STRING_TYPES):
        text_parts.append('{0:s}path: {1:s}\n'.format(
            indentation, scan_object))

    text_parts.append('{0:s}default value:\n'.format(indentation))

    if isinstance(self.default_value, PathFilterScanTreeNode):
      text_parts.append('{0:s}scan tree node:\n'.format(indentation))
      text_parts.append(self.default_value.ToDebugString(indentation_level + 1))

    elif isinstance(self.default_value, py2to3.STRING_TYPES):
      text_parts.append('{0:s}pattern: {1:s}\n'.format(
          indentation, self.default_value))

    text_parts.append('\n')

    return ''.join(text_parts)

Converts the path filter scan tree node into a debug string.

Args:
indentation_level: an integer containing the text indentation level.

Returns:
A string containing a debug representation of the path filter scan
tree node.


def path_is_ignored(self, path):
    try:
        self.run('check-ignore', '--quiet', path)
    except CommandError as e:
        if (e.retcode == 1):
            return False
        else:
            raise e
    return True

Given a path, check if the path would be ignored.

Returns:
boolean


def file_content(self, file_content, update_if_exists=True):

        if not self.can_update():
            self._tcex.handle_error(910, [self.type])

        self._data['fileContent'] = file_content
        return self.tc_requests.upload(
            self.api_type,
            self.api_sub_type,
            self.unique_id,
            file_content,
            update_if_exists=update_if_exists,
        )

Updates the file content.

Args:
file_content: The file_content to upload.
update_if_exists:

Returns:


def list(name, default=None, allow_none=False, fallback=None, separator=','):

    value = read(name, default, allow_none, fallback=fallback)
    if isinstance(value, builtins.list):
        return value
    elif isinstance(value, builtins.str):
        return _str_to_list(value, separator)
    elif value is None and allow_none:
        return None
    else:
        return [builtins.str(value)]

Get a list of strings or the default.

The individual list elements are whitespace-stripped.

Args:
name: The environment variable name
default: The default value to use if no environment variable is found
allow_none: If the return value can be `None` (i.e. optional)
separator: The list item separator character or pattern


def get_metric_by_name(self, metric_name, **kwargs):

        return self._get_object_by_name(self._METRIC_ENDPOINT_SUFFIX,
                                        metric_name,
                                        **kwargs)

get a metric by name

Args:
metric_name (string): name of metric

Returns:
dictionary of response


def format_level_0_memory(memory):

    formatted_memory = _list_to_complex_array(memory)

    if not 2 <= len(formatted_memory.shape) <= 3:
        raise QiskitError('Level zero memory is not of correct shape.')
    return formatted_memory

Format an experiment result memory object for measurement level 0.

Args:
memory (list): Memory from experiment with `meas_level==1`. `avg` or
`single` will be inferred from shape of result memory.

Returns:
np.ndarray: Measurement level 0 complex numpy array

Raises:
QiskitError: If the returned numpy array does not have 2 (avg) or 3 (single)
indicies.


def run_one_step(self, eig_init_vec_val, eig_num_iter_val, smooth_val, penalty_val, learning_rate_val):
    step_feed_dict = {self.eig_init_vec_placeholder: eig_init_vec_val, self.eig_num_iter_placeholder: eig_num_iter_val, self.smooth_placeholder: smooth_val, self.penalty_placeholder: penalty_val, self.learning_rate: learning_rate_val}
    if (self.params['eig_type'] == 'SCIPY'):
        (current_eig_vector, self.current_eig_val_estimate) = self.get_scipy_eig_vec()
        step_feed_dict.update({self.eig_vec_estimate: current_eig_vector})
    elif (self.params['eig_type'] == 'LZS'):
        step_feed_dict.update({self.dual_object.m_min_vec_ph: self.dual_object.m_min_vec_estimate})
    self.sess.run(self.train_step, feed_dict=step_feed_dict)
    [_, self.dual_object.m_min_vec_estimate, self.current_eig_val_estimate] = self.sess.run([self.proj_step, self.eig_vec_estimate, self.eig_val_estimate], feed_dict=step_feed_dict)
    if ((self.current_step % self.params['print_stats_steps']) == 0):
        [self.current_total_objective, self.current_unconstrained_objective, self.dual_object.m_min_vec_estimate, self.current_eig_val_estimate, self.current_nu] = self.sess.run([self.total_objective, self.dual_object.unconstrained_objective, self.eig_vec_estimate, self.eig_val_estimate, self.dual_object.nu], feed_dict=step_feed_dict)
        stats = {'total_objective': float(self.current_total_objective), 'unconstrained_objective': float(self.current_unconstrained_objective), 'min_eig_val_estimate': float(self.current_eig_val_estimate)}
        tf.logging.info('Current inner step: %d, optimization stats: %s', self.current_step, stats)
        if (self.params['stats_folder'] is not None):
            stats = json.dumps(stats)
            filename = os.path.join(self.params['stats_folder'], (str(self.current_step) + '.json'))
            with tf.gfile.Open(filename) as file_f:
                file_f.write(stats)
    if (((self.current_step % self.params['projection_steps']) == 0) and (self.current_unconstrained_objective < 0)):
        nu = self.sess.run(self.dual_object.nu)
        dual_feed_dict = {self.dual_object.h_min_vec_ph: self.dual_object.h_min_vec_estimate}
        (_, min_eig_val_h_lz) = self.dual_object.get_lanczos_eig(compute_m=False, feed_dict=dual_feed_dict)
        projected_dual_feed_dict = {self.dual_object.projected_dual.nu: nu, self.dual_object.projected_dual.min_eig_val_h: min_eig_val_h_lz}
        if self.dual_object.projected_dual.compute_certificate(self.current_step, projected_dual_feed_dict):
            return True
    return False

Run one step of gradient descent for optimization.

Args:
eig_init_vec_val: Start value for eigen value computations
eig_num_iter_val: Number of iterations to run for eigen computations
smooth_val: Value of smoothness parameter
penalty_val: Value of penalty for the current step
learning_rate_val: Value of learning rate
Returns:
found_cert: True is negative certificate is found, False otherwise


def print_error_messages_raylet(task_error_queue, threads_stopped):
    while True:
        if threads_stopped.is_set():
            return
        try:
            (error, t) = task_error_queue.get(block=False)
        except queue.Empty:
            threads_stopped.wait(timeout=0.01)
            continue
        while ((t + UNCAUGHT_ERROR_GRACE_PERIOD) > time.time()):
            threads_stopped.wait(timeout=1)
            if threads_stopped.is_set():
                break
        if (t < (last_task_error_raise_time + UNCAUGHT_ERROR_GRACE_PERIOD)):
            logger.debug('Suppressing error from worker: {}'.format(error))
        else:
            logger.error('Possible unhandled error from worker: {}'.format(error))

Prints message received in the given output queue.

This checks periodically if any un-raised errors occured in the background.

Args:
task_error_queue (queue.Queue): A queue used to receive errors from the
thread that listens to Redis.
threads_stopped (threading.Event): A threading event used to signal to
the thread that it should exit.


def to_hdf5(ramon, hdf5=None):

    if issubclass(type(ramon), RAMONBase) is False:
        raise InvalidRAMONError("Invalid RAMON supplied to ramon.to_hdf5.")

    import h5py
    import numpy

    if hdf5 is None:
        tmpfile = tempfile.NamedTemporaryFile(delete=False)
    else:
        tmpfile = hdf5

    with h5py.File(tmpfile.name, "a") as hdf5:

        grp = hdf5.create_group(str(ramon.id))

        grp.create_dataset("ANNOTATION_TYPE", (1,),
                           numpy.uint32,
                           data=AnnotationType.get_int(type(ramon)))

        if hasattr(ramon, 'cutout'):
            if ramon.cutout is not None:
                grp.create_dataset('CUTOUT', ramon.cutout.shape,
                                   ramon.cutout.dtype, data=ramon.cutout)
                grp.create_dataset('RESOLUTION', (1,),
                                   numpy.uint32, data=ramon.resolution)
                grp.create_dataset('XYZOFFSET', (3,),
                                   numpy.uint32, data=ramon.xyz_offset)

        metadata = grp.create_group('METADATA')

        metadata.create_dataset('AUTHOR', (1,),
                                dtype=h5py.special_dtype(vlen=str),
                                data=ramon.author)

        fstring = StringIO()
        csvw = csv.writer(fstring, delimiter=',')
        csvw.writerows([r for r in six.iteritems(ramon.kvpairs)])

        metadata.create_dataset('KVPAIRS', (1,),
                                dtype=h5py.special_dtype(vlen=str),
                                data=fstring.getvalue())
        metadata.create_dataset('CONFIDENCE', (1,), numpy.float,
                                data=ramon.confidence)
        metadata.create_dataset('STATUS', (1,), numpy.uint32,
                                data=ramon.status)

        if hasattr(ramon, 'segments'):
            metadata.create_dataset('SEGMENTS',
                                    data=numpy.asarray(ramon.segments,
                                                       dtype=numpy.uint32))

        if hasattr(ramon, 'synapse_type'):
            metadata.create_dataset('SYNAPSE_TYPE', (1,), numpy.uint32,
                                    data=ramon.synapse_type)

        if hasattr(ramon, 'weight'):
            metadata.create_dataset('WEIGHT', (1,),
                                    numpy.float, data=ramon.weight)

        if hasattr(ramon, 'neuron'):
            metadata.create_dataset('NEURON', (1,),
                                    numpy.uint32, data=ramon.neuron)

        if hasattr(ramon, 'segmentclass'):
            metadata.create_dataset('SEGMENTCLASS', (1,), numpy.uint32,
                                    data=ramon.segmentclass)

        if hasattr(ramon, 'synapses'):
            metadata.create_dataset('SYNAPSES', (len(ramon.synapses),),
                                    numpy.uint32, data=ramon.synapses)

        if hasattr(ramon, 'organelles'):
            metadata.create_dataset('ORGANELLES',
                                    (len(ramon.organelles),),
                                    numpy.uint32,
                                    data=ramon.organelles)

        if hasattr(ramon, 'organelle_class'):
            metadata.create_dataset('ORGANELLECLASS', (1,),
                                    numpy.uint32,
                                    data=ramon.organelle_class)
        hdf5.flush()
        tmpfile.seek(0)
        return tmpfile
    return False

Exports a RAMON object to an HDF5 file object.

Arguments:
ramon (RAMON): A subclass of RAMONBase
hdf5 (str): Export filename

Returns:
hdf5.File

Raises:
InvalidRAMONError: if you pass a non-RAMON object


def sync_entities(*model_objs):

    if sync_entities.defer:

        if not model_objs:
            sync_entities.buffer[None] = None
        else:

            for model_obj in model_objs:
                sync_entities.buffer[(model_obj.__class__, model_obj.pk)] = model_obj

        return False

    EntitySyncer(*model_objs).sync()

Syncs entities

Args:
model_objs (List[Model]): The model objects to sync. If empty, all entities will be synced


def __or__(self, other: 'TensorFluent') -> 'TensorFluent':

        return self._binary_op(self, other, tf.logical_or, tf.bool)

Returns a TensorFluent for the or logical operator.

Args:
self: The first operand.
other: The second operand.

Returns:
A TensorFluent wrapping the operator's output.


def wiki_2x2_base():
    hparams = mtf_transformer.mtf_transformer_base_lm()
    hparams.shared_embedding_and_softmax_weights = False
    hparams.attention_dropout = 0.0
    hparams.relu_dropout = 0.0
    hparams.layer_prepostprocess_dropout = 0.0
    hparams.max_length = 1024
    hparams.batch_size = 32
    hparams.learning_rate_schedule = 'rsqrt_decay'
    hparams.mesh_shape = 'all:8'
    hparams.layout = 'batch:all;experts:all'
    moe.set_default_moe_hparams(hparams)
    hparams.moe_num_experts = 16
    hparams.moe_hidden_size = 8192
    hparams.decoder_layers = (['att', 'drd'] * 6)
    hparams.d_model = 1024
    hparams.d_ff = 2048
    hparams.d_kv = 128
    hparams.num_heads = 4
    return hparams

Set of architectural experiments - language model on wikipedia on a 2x2.

1 epoch = ~180k steps at batch size 32 - we may never finish an epoch!

Returns:
a hparams


class DetaHungarianMatcher(nn.Module):

    def __init__(self, class_cost: float=1, bbox_cost: float=1, giou_cost: float=1):
        super().__init__()
        requires_backends(self, ['scipy'])
        self.class_cost = class_cost
        self.bbox_cost = bbox_cost
        self.giou_cost = giou_cost
        if class_cost == 0 and bbox_cost == 0 and (giou_cost == 0):
            raise ValueError("All costs of the Matcher can't be 0")

    @torch.no_grad()
    def forward(self, outputs, targets):

        batch_size, num_queries = outputs['logits'].shape[:2]
        out_prob = outputs['logits'].flatten(0, 1).sigmoid()
        out_bbox = outputs['pred_boxes'].flatten(0, 1)
        target_ids = torch.cat([v['class_labels'] for v in targets])
        target_bbox = torch.cat([v['boxes'] for v in targets])
        alpha = 0.25
        gamma = 2.0
        neg_cost_class = (1 - alpha) * out_prob ** gamma * -(1 - out_prob + 1e-08).log()
        pos_cost_class = alpha * (1 - out_prob) ** gamma * -(out_prob + 1e-08).log()
        class_cost = pos_cost_class[:, target_ids] - neg_cost_class[:, target_ids]
        bbox_cost = torch.cdist(out_bbox, target_bbox, p=1)
        giou_cost = -generalized_box_iou(center_to_corners_format(out_bbox), center_to_corners_format(target_bbox))
        cost_matrix = self.bbox_cost * bbox_cost + self.class_cost * class_cost + self.giou_cost * giou_cost
        cost_matrix = cost_matrix.view(batch_size, num_queries, -1).cpu()
        sizes = [len(v['boxes']) for v in targets]
        indices = [linear_sum_assignment(c[i]) for i, c in enumerate(cost_matrix.split(sizes, -1))]
        return [(torch.as_tensor(i, dtype=torch.int64), torch.as_tensor(j, dtype=torch.int64)) for i, j in indices]

This class computes an assignment between the targets and the predictions of the network.

For efficiency reasons, the targets don't include the no_object. Because of this, in general, there are more
predictions than targets. In this case, we do a 1-to-1 matching of the best predictions, while the others are
un-matched (and thus treated as non-objects).

Args:
class_cost:
The relative weight of the classification error in the matching cost.
bbox_cost:
The relative weight of the L1 error of the bounding box coordinates in the matching cost.
giou_cost:
The relative weight of the giou loss of the bounding box in the matching cost.


def __init__(self, variable_name, inferred_type):

        variable_name = ensure_unicode_string(variable_name)
        super(Variable, self).__init__(variable_name, inferred_type)
        self.variable_name = variable_name
        self.inferred_type = inferred_type
        self.validate()

Construct a new Variable object for the given variable name.

Args:
variable_name: string, should start with '$' and then obey variable naming rules
(see validate_safe_string())
inferred_type: GraphQL type object, specifying the inferred type of the variable

Returns:
new Variable object


def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_3):
    if (kmip_version < enums.KMIPVersion.KMIP_1_3):
        raise exceptions.VersionNotSupported('KMIP {} does not support the ProfileInformation object.'.format(kmip_version.value))
    super(ProfileInformation, self).read(input_buffer, kmip_version=kmip_version)
    local_buffer = utils.BytearrayStream(input_buffer.read(self.length))
    if self.is_tag_next(enums.Tags.PROFILE_NAME, local_buffer):
        profile_name = primitives.Enumeration(enums.ProfileName, tag=enums.Tags.PROFILE_NAME)
        profile_name.read(local_buffer, kmip_version=kmip_version)
        self._profile_name = profile_name
    else:
        raise exceptions.InvalidKmipEncoding('The ProfileInformation encoding is missing the profile name.')
    if self.is_tag_next(enums.Tags.SERVER_URI, local_buffer):
        server_uri = primitives.TextString(tag=enums.Tags.SERVER_URI)
        server_uri.read(local_buffer, kmip_version=kmip_version)
        self._server_uri = server_uri
    if self.is_tag_next(enums.Tags.SERVER_PORT, local_buffer):
        server_port = primitives.Integer(tag=enums.Tags.SERVER_PORT)
        server_port.read(local_buffer, kmip_version=kmip_version)
        self._server_port = server_port
    self.is_oversized(local_buffer)

Read the data encoding the ProfileInformation structure and decode it
into its constituent parts.

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

Raises:
InvalidKmipEncoding: Raised if the profile name is missing from
the encoding.
VersionNotSupported: Raised when a KMIP version is provided that
does not support the ProfileInformation structure.


def ReadClientMetadata(self, client_id):
    result = self.MultiReadClientMetadata([client_id])
    try:
        return result[client_id]
    except KeyError:
        raise UnknownClientError(client_id)

Reads the ClientMetadata record for a single client.

Args:
client_id: A GRR client id string, e.g. "C.ea3b2b71840d6fa7".

Returns:
An rdfvalues.object.ClientMetadata object.

Raises:
UnknownClientError: if no client with corresponding id was found.


def _format_device(var):
    if var.dtype.name.endswith('_ref'):
        resource_var_annotation = '(legacy)'
    else:
        resource_var_annotation = '(resource)'
    if var.device:
        return '{} {}'.format(var.device, resource_var_annotation)
    else:
        return resource_var_annotation

Returns the device with an annotation specifying `ResourceVariable`.

"legacy" means a normal tf.Variable while "resource" means a ResourceVariable.

For example:
`(legacy)`
`(resource)`
`/job:learner/task:0/device:CPU:* (legacy)`
`/job:learner/task:0/device:CPU:* (resource)`

Args:
var: The Tensorflow Variable to print.


def rename_dimension(x, old_name, new_name):
    return reshape(x, x.shape.rename_dimension(old_name, new_name))

Reshape a Tensor, renaming one dimension.

Args:
x: a Tensor
old_name: a string
new_name: a string

Returns:
a Tensor


def absolute_name(self, depth=0):

        node, node_depth = self, self.depth
        if depth < 1:
            depth = node_depth
        while node_depth > depth and node.package is not None:
            node = node.package
            node_depth -= 1
        names = []
        while node is not None:
            names.append(node.name)
            node = node.package
        return '.'.join(reversed(names))

Return the absolute name of the node.

Concatenate names from root to self within depth.

Args:
depth (int): maximum depth to go to.

Returns:
str: absolute name of the node (until given depth is reached).


def economic_qs(K, epsilon=sqrt(finfo(float).eps)):
    (S, Q) = eigh(K)
    nok = (abs(max(Q[0].min(), Q[0].max(), key=abs)) < epsilon)
    nok = (nok and (abs(max(K.min(), K.max(), key=abs)) >= epsilon))
    if nok:
        from scipy.linalg import eigh as sp_eigh
        (S, Q) = sp_eigh(K)
    ok = (S >= epsilon)
    nok = logical_not(ok)
    S0 = S[ok]
    Q0 = Q[(:, ok)]
    Q1 = Q[(:, nok)]
    return ((Q0, Q1), S0)

r"""Economic eigen decomposition for symmetric matrices.

A symmetric matrix ``K`` can be decomposed in
:math:`\mathrm Q_0 \mathrm S_0 \mathrm Q_0^\intercal + \mathrm Q_1\
\mathrm S_1 \mathrm Q_1^ \intercal`, where :math:`\mathrm S_1` is a zero
matrix with size determined by ``K``'s rank deficiency.

Args:
K (array_like): Symmetric matrix.
epsilon (float): Eigen value threshold. Default is
``sqrt(finfo(float).eps)``.

Returns:
tuple: ``((Q0, Q1), S0)``.


def generate_sample_set(self, tags=None):
    if isinstance(tags, str):
        tags = [tags]
    md5_list = self.data_store.tag_match(tags)
    return self.store_sample_set(md5_list)

Generate a sample_set that maches the tags or all if tags are not specified.

Args:
tags: Match samples against this tag list (or all if not specified)

Returns:
The sample_set of those samples matching the tags


def get_rbounds(step):

    if step.geom is not None:
        rcmb = step.geom.rcmb
    else:
        rcmb = step.sdat.par['geometry']['r_cmb']
        if step.sdat.par['geometry']['shape'].lower() == 'cartesian':
            rcmb = 0
    rcmb = max(rcmb, 0)
    return rcmb, rcmb + 1

Radial or vertical position of boundaries.

Args:
step (:class:`~stagpy.stagyydata._Step`): a step of a StagyyData
instance.
Returns:
tuple of floats: radial or vertical positions of boundaries of the
domain.


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

Corresponds to IDD Field `ws004c`

Args:
value (float): value for IDD Field `ws004c`
Unit: m/s
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 read_cs_g0_contribution(self):
    header_pattern = '^\\s+G\\=0 CONTRIBUTION TO CHEMICAL SHIFT \\(field along BDIR\\)\\s+$\\n^\\s+-{50,}$\\n^\\s+BDIR\\s+X\\s+Y\\s+Z\\s*$\\n^\\s+-{50,}\\s*$\\n'
    row_pattern = ('(?:\\d+)\\s+' + '\\s+'.join((['([-]?\\d+\\.\\d+)'] * 3)))
    footer_pattern = '\\s+-{50,}\\s*$'
    self.read_table_pattern(header_pattern, row_pattern, footer_pattern, postprocess=float, last_one_only=True, attribute_name='cs_g0_contribution')

Parse the  G0 contribution of NMR chemical shielding.

Returns:
G0 contribution matrix as list of list.


def get_atoms(structure, **kwargs):

        if not structure.is_ordered:
            raise ValueError("ASE Atoms only supports ordered structures")
        symbols = [str(site.specie.symbol) for site in structure]
        positions = [site.coords for site in structure]
        cell = structure.lattice.matrix
        return Atoms(symbols=symbols, positions=positions, pbc=True,
                     cell=cell, **kwargs)

Returns ASE Atoms object from pymatgen structure.

Args:
structure: pymatgen.core.structure.Structure
**kwargs: other keyword args to pass into the ASE Atoms constructor

Returns:
ASE Atoms object


def get_redirect(paths):

    if isinstance(paths, str):
        paths = [paths]

    for path in paths:
        url, permanent = get_alias(path)
        if url:
            return redirect(url, 301 if permanent else 302)

        url, permanent = current_app.get_path_regex(path)
        if url:
            return redirect(url, 301 if permanent else 302)

    return None

Get a redirect from a path or list of paths

Arguments:

paths -- either a single path string, or a list of paths to check

Returns: a flask.redirect() result


def _recover_shape_information(self, inputs, outputs):
    batch_size_value = inputs.get_shape()[0]
    if self._data_format.startswith('NC'):
        output_shape_value = ((batch_size_value, self.output_channels) + self.output_shape)
    elif (self._data_format.startswith('N') and self._data_format.endswith('C')):
        output_shape_value = (((batch_size_value,) + self.output_shape) + (self.output_channels,))
    outputs.set_shape(output_shape_value)
    return outputs

Recover output tensor shape value to enable shape inference.

The batch size of `inputs` isn't preserved by the convolution op. Calculate
what the proper output shape will be for `outputs`.

Args:
inputs: A Tensor of shape `data_format` and of type `tf.float16`,
`tf.bfloat16` or `tf.float32`.
outputs: A Tensor of shape `data_format` and of type `tf.float16`,
`tf.bfloat16` or `tf.float32`. The output of `inputs` from a transpose
convolution op.

Returns:
outputs: The passed-in `outputs` with all shape information filled in.


def to_string(cls, error_code):

        if error_code == cls.ZONE_NOT_FOUND_ERROR:
            return 'Zone not found'
        return super(JLinkWriteErrors, cls).to_string(error_code)

Returns the string message for the given ``error_code``.

Args:
cls (JLinkWriteErrors): the ``JLinkWriteErrors`` class
error_code (int): error code to convert

Returns:
An error string corresponding to the error code.

Raises:
ValueError: if the error code is invalid.


def GetPreviousNonBlankLine(clean_lines, linenum):
    prevlinenum = (linenum - 1)
    while (prevlinenum >= 0):
        prevline = clean_lines.elided[prevlinenum]
        if (not IsBlankLine(prevline)):
            return (prevline, prevlinenum)
        prevlinenum -= 1
    return ('', (- 1))

Return the most recent non-blank line and its line number.

Args:
clean_lines: A CleansedLines instance containing the file contents.
linenum: The number of the line to check.

Returns:
A tuple with two elements.  The first element is the contents of the last
non-blank line before the current line, or the empty string if this is the
first non-blank line.  The second is the line number of that line, or -1
if this is the first non-blank line.


def process(self, body, url=None, sig=None):
    self.request = RequestBody()
    self.response = ResponseBody()
    self.request.parse(body)
    app_id = self.request.session.application.application_id
    stamp = self.request.request.timestamp
    if (not self.valid.request(app_id, body, stamp, url, sig)):
        return False
    self.pass_session_attributes()
    self.dispatch()
    if (self.request.request.type == 'SessionEndedRequest'):
        self.terminate()
    return self.response.to_json()

Process request body given skill logic.

To validate a request, both, url and sig are required.

Attributes received through body will be automatically added to the
response.

Args:
body: str. HTTP request body.
url: str. SignatureCertChainUrl header value sent by request.
PEM-encoded X.509 certificate chain that Alexa used to sign the
message.
sig: str. Signature header value sent by request. Base64-encoded
signature of the request body.

Return:
str or bool: HTTP response body or False if the request is invalid.


def segment_to_vector(self, seg):

        ft_dict = {ft: val for (val, ft) in self.fts(seg)}
        return [ft_dict[name] for name in self.names]

Given a Unicode IPA segment, return a list of feature specificiations
in cannonical order.

Args:
seg (unicode): IPA consonant or vowel

Returns:
list: feature specifications ('+'/'-'/'0') in the order from
`FeatureTable.names`


def from_maybe_serialized(source: Union[Any, str], value_type: Optional[Type[Any]]=None) -> Any:
    if isinstance(source, str):
        if source.endswith('.json'):
            value = symbolic.load(source)
        else:
            value = symbolic.from_json_str(source)
    else:
        value = source
    if value_type is not None and (not isinstance(value, value_type)):
        raise TypeError(f'Loaded value {value!r} is not an instance of {value_type!r}.')
    return value

Load value from maybe serialized form (e.g. JSON file or JSON string).

Args:
source: Source of value. It can be value (non-string type) itself, or a
filepath, or a JSON string from where the value will be loaded.
value_type: An optional type to constrain the value.

Returns:
Value from source.


def _CalculateHashDataStream(self, file_entry, data_stream_name):

    hash_context = hashlib.sha256()

    try:
      file_object = file_entry.GetFileObject(data_stream_name=data_stream_name)
    except IOError as exception:
      logging.warning((
          'Unable to open path specification:\n{0:s}'
          'with error: {1!s}').format(
              file_entry.path_spec.comparable, exception))
      return None

    if not file_object:
      return None

    try:
      data = file_object.read(self._READ_BUFFER_SIZE)
      while data:
        hash_context.update(data)
        data = file_object.read(self._READ_BUFFER_SIZE)
    except IOError as exception:
      logging.warning((
          'Unable to read from path specification:\n{0:s}'
          'with error: {1!s}').format(
              file_entry.path_spec.comparable, exception))
      return None

    finally:
      file_object.close()

    return hash_context.hexdigest()

Calculates a message digest hash of the data of the file entry.

Args:
file_entry (dfvfs.FileEntry): file entry.
data_stream_name (str): name of the data stream.

Returns:
bytes: digest hash or None.


def set_dataset_date(self, dataset_date, dataset_end_date=None, date_format=None):
    parsed_date = self._parse_date(dataset_date, date_format)
    if (dataset_end_date is None):
        self.set_dataset_date_from_datetime(parsed_date)
    else:
        parsed_end_date = self._parse_date(dataset_end_date, date_format)
        self.set_dataset_date_from_datetime(parsed_date, parsed_end_date)

Set dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.

Args:
dataset_date (str): Dataset date string
dataset_end_date (Optional[str]): Dataset end date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.

Returns:
None


def call(self, hidden_states: tf.Tensor, attention_mask: tf.Tensor, causal_attention_mask: tf.Tensor, output_attentions: Optional[bool]=False) -> Tuple[tf.Tensor]:
    residual = hidden_states
    hidden_states = self.layer_norm1(hidden_states)
    hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, causal_attention_mask=causal_attention_mask, output_attentions=output_attentions)
    hidden_states = residual + hidden_states
    residual = hidden_states
    hidden_states = self.layer_norm2(hidden_states)
    hidden_states = self.mlp(hidden_states)
    hidden_states = residual + hidden_states
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (attn_weights,)
    return outputs

Args:
hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`tf.Tensor`): attention mask of size
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
`(config.encoder_attention_heads,)`.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def past_stop_threshold(stop_threshold, eval_metric):

  if stop_threshold is None:
    return False

  if not isinstance(stop_threshold, numbers.Number):
    raise ValueError("Threshold for checking stop conditions must be a number.")
  if not isinstance(eval_metric, numbers.Number):
    raise ValueError("Eval metric being checked against stop conditions "
                     "must be a number.")

  if eval_metric >= stop_threshold:
    tf.logging.info(
        "Stop threshold of {} was passed with metric value {}.".format(
            stop_threshold, eval_metric))
    return True

  return False

Return a boolean representing whether a model should be stopped.

Args:
stop_threshold: float, the threshold above which a model should stop
training.
eval_metric: float, the current value of the relevant metric to check.

Returns:
True if training should stop, False otherwise.

Raises:
ValueError: if either stop_threshold or eval_metric is not a number


def __init__(self, value_set_codes_table: Optional[bigquery.TableReference]=None, value_set_codes_definitions: Optional[fhir_package.FhirPackageManager]=None) -> None:
    self._value_set_codes_table = value_set_codes_table
    self._value_set_codes_definitions = value_set_codes_definitions
    self._use_resource_alias = None

Creates a BigQuerySqlInterpreter.

Args:
value_set_codes_table: The name of the database table containing value set
code definitions. Used when building SQL for memberOf expressions. If
given, value set definitions needed for memberOf expressions will be
retrieved from this table if they can not be found in
`value_set_codes_definitions`. If neither this nor
`value_set_codes_definitions` is given, no memberOf SQL will be
generated.
value_set_codes_definitions: A package manager containing value set
definitions which can be used to build SQL for memberOf expressions.
These value set definitions can be consulted in favor of using an
external `value_set_codes_table`. If neither this nor
`value_set_codes_definitions` is given, no memberOf SQL will be
generated.


def get(self, blocking=True):
    if self.closed:
        raise PoolAlreadyClosedError('Connection pool is already closed.')
    if (not self.limiter.acquire(blocking=blocking)):
        return None
    c = None
    try:
        c = self.idle_conns.pop()
    except IndexError:
        try:
            c = self.connect_func()
        except Exception:
            self.limiter.release()
            raise
    return _ConnectionProxy(self, c)

Gets a connection.

Args:
blocking: Whether to block when max_size connections are already in use.
If false, may return None.

Returns:
A connection to the database.

Raises:
PoolAlreadyClosedError: if close() method was already called on
this pool.


def create_multiple_expectations(df, columns, expectation_type, *args, **kwargs):
    expectation = getattr(df, expectation_type)
    results = list()
    for column in columns:
        results.append(expectation(column, *args, **kwargs))
    return results

Creates an identical expectation for each of the given columns with the specified arguments, if any.

Args:
df (great_expectations.dataset): A great expectations dataset object.
columns (list): A list of column names represented as strings.
expectation_type (string): The expectation type.

Raises:
KeyError if the provided column does not exist.
AttributeError if the provided expectation type does not exist or df is not a valid great expectations dataset.

Returns:
A list of expectation results.