def postprocess_periodical(marc_xml, mods, uuid, counter, url):
    dom = double_linked_dom(mods)
    add_missing_xml_attributes(dom, counter)
    if uuid:
        add_uuid(dom, uuid)
    return dom.prettify()

Some basic postprocessing of the periodical publications.

Args:
marc_xml (str): Original Aleph record.
mods (str): XML string generated by XSLT template.
uuid (str): UUID of the package.
counter (int): Number of record, is added to XML headers.
url (str): URL of the publication (public or not).

Returns:
str: Updated XML.


def init_from_dataset_and_submissions_write_to_datastore(self, dataset_batches, attack_submission_ids):
    batches_x_attacks = itertools.product(dataset_batches.data.keys(), attack_submission_ids)
    for (idx, (dataset_batch_id, attack_id)) in enumerate(batches_x_attacks):
        adv_batch_id = ADVERSARIAL_BATCH_ID_PATTERN.format(idx)
        self.add_batch(adv_batch_id, {'dataset_batch_id': dataset_batch_id, 'submission_id': attack_id})
    self.write_to_datastore()

Init list of adversarial batches from dataset batches and submissions.

Args:
dataset_batches: instances of DatasetBatches
attack_submission_ids: iterable with IDs of all (targeted and nontargeted)
attack submissions, could be obtains as
CompetitionSubmissions.get_all_attack_ids()


def optimize_hyperparameters(self, problems, parameter_locks=None, smoothing=20, max_iterations=100, _meta_optimizer=None, _low_memory=True):
    if (smoothing <= 0):
        raise ValueError('smoothing must be > 0')
    if isinstance(problems, collections.Iterable):
        for problem in problems:
            if (not isinstance(problem, Problem)):
                raise TypeError('problem must be Problem instance or list of Problem instances')
    elif isinstance(problems, Problem):
        problems = [problems]
    else:
        raise TypeError('problem must be Problem instance or list of Problem instances')
    meta_parameters = copy.deepcopy(self._hyperparameters)
    locked_values = _parse_parameter_locks(self, meta_parameters, parameter_locks)
    solution_size = _get_hyperparameter_solution_size(meta_parameters)
    decode = _make_hyperparameter_decode_func(locked_values, meta_parameters)
    if _low_memory:
        master_fitness_dict = None
    else:
        master_fitness_dict = {}
    additional_parameters = {'_optimizer': self, '_problems': problems, '_runs': smoothing, '_master_fitness_dict': master_fitness_dict}
    META_FITNESS = Problem(_meta_fitness_func, decode_function=decode, fitness_kwargs=additional_parameters)
    if (_meta_optimizer is None):
        from optimal import GenAlg
        _meta_optimizer = GenAlg(solution_size)
    else:
        _meta_optimizer._solution_size = solution_size
    best_parameters = _meta_optimizer.optimize(META_FITNESS, max_iterations=max_iterations)
    self._set_hyperparameters(best_parameters)
    return best_parameters

Optimize hyperparameters for a given problem.

Args:
parameter_locks: a list of strings, each corresponding to a hyperparamter
that should not be optimized.
problems: Either a single problem, or a list of problem instances,
allowing optimization based on multiple similar problems.
smoothing: int; number of runs to average over for each set of hyperparameters.
max_iterations: The number of iterations to optimize before stopping.
_low_memory: disable performance enhancements to save memory
(they use a lot of memory otherwise).


Converts a string character to a decimal number.
Where "A"->10, "B"->11, "C"->12, ...etc

Args:
chr(str): A single character in the form of a string.

Returns:
The integer value of the input string digit.


def remove(self, *l):
    removeList = list(flatten(l))
    self._remove(removeList, self.value)

remove elements from self.value by matching.

Create the exactly same single you want to delete and pass it(them) in.
Normally this method needs to be overwrited by subclass. It only looks inside current instance's value, not recursive. There is no need for a recursive one anyway.

Args:
*l: a single element, a bunch of element seperated by comma, or a list of elements, or any combination. Element is what you match with.


def expose(func):

    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        return func(*args, **kwargs)

    if inspect.isclass(func):
        wrapped._sdkmetaclass = SDKMethod(func.__name__)
    else:
        wrapped._sdkmeta = SDKMethod(func.__name__)
    return wrapped

Decorator to be used with :class:`SDKWrapper`. This decorator indicates
that the wrapped method or class should be exposed in the proxied object.

Args:
func(types.FunctionType/types.MethodType): function to decorate

Returns:
None


def tpu_core_locations_to_ids(self, tpu_core_locations):
    return _pywrap_dtensor_device.TPUCoreLocationsToIDs(context.context()._handle, self._device_info, tpu_core_locations)

Translates TPU core locations to TPU core IDs.

Args:
tpu_core_locations: A list of TPU core locations. Each one is a list of
four unsigned integers, [x, y, z, core].

Returns:
A list of corresponding TPU core IDs.


def dummy_inputs(self):
    if self.config.use_lang_emb and self.config.n_langs > 1:
        return {'input_ids': tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32), 'langs': tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)}
    else:
        return {'input_ids': tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)}

Dummy inputs to build the network.

Returns:
tf.Tensor with dummy inputs


def upload_part(self, request):
    try:
        boto_response = self.client.upload_part(Body=request.bytes, Bucket=request.bucket, Key=request.object, PartNumber=request.part_number, UploadId=request.upload_id)
        response = messages.UploadPartResponse(boto_response['ETag'], request.part_number)
        return response
    except Exception as e:
        raise messages.S3ClientError(str(e), get_http_error_code(e))

Uploads part of a file to S3 during a multipart upload

Args:
request: (UploadPartRequest) input message
Returns:
(UploadPartResponse) The response message.


def update_exit_code(self, code: int):
    if code:
        if self._exit_code:
            self._exit_code = min(self._exit_code, code)
        else:
            self._exit_code = code

Set the exit code if it is serious than before.

Args:
code: The exit code.


def __init__(self, state, critical=False):

    super(BaseModule, self).__init__()
    self.critical = critical
    self.state = state

Initialize the base collector object.

Args:
state: a DFTimewolfState object.
critical: Whether the module is critical or not. If True and the module
encounters an error, then the whole recipe will fail.


def apply_pairwise(self, function, symmetric=True, diagonal=False, block=None, **kwargs):

    steps = self.index
    r = pd.DataFrame(index=steps, columns=steps)
    for i, s1 in enumerate(steps):
        j = range(i+1 if symmetric else len(steps))
        if not diagonal:
            j.remove(i)
        other = set(steps[j])
        if block is not None:
            df = self.reset_index()
            df = df.merge(df, on=block)
            other &= set(df[df.index_x == s1].index_y)

        for s2 in other:
            r.ix[s1, s2] = function(s1, s2, **kwargs)
    return r

Helper function for pairwise apply.
Args:
steps: an ordered collection of steps
function: function to apply, first two positional arguments are steps
symmetric: whether function is symmetric in the two steps
diagonal: whether to apply on the diagonal
block: apply only when the given columns match
kwargs: keyword arguments to pass to the function

Returns:
DataFrame with index and columns equal to the steps argument


def Sign(verifiable, keypair):
    prikey = bytes(keypair.PrivateKey)
    hashdata = verifiable.GetHashData()
    res = Crypto.Default().Sign(hashdata, prikey)
    return res

Sign the `verifiable` object with the private key from `keypair`.

Args:
verifiable:
keypair (neocore.KeyPair):

Returns:
bool: True if successfully signed. False otherwise.


def dump(ofp, *pb_objs, **kwargs):
    mode = 'wb'
    if isinstance(ofp, str):
        ostream = open(ofp, mode=mode, **kwargs)
    else:
        ostream = open(fileobj=ofp, mode=mode, **kwargs)
    with ostream:
        ostream.write(*pb_objs)

Write to a stream.

Args:
ofp (string or file-like object): output stream.
pb_objs (*protobuf.message.Message): list of protobuf message objects
to be written.


def set_site_energies( self, energies ):

        self.site_energies = energies
        for site_label in energies:
            for site in self.sites:
                if site.label == site_label:
                    site.energy = energies[ site_label ]

Set the energies for every site in the lattice according to the site labels.

Args:
energies (Dict(Str:Float): Dictionary of energies for each site label, e.g.::

{ 'A' : 1.0, 'B', 0.0 }

Returns:
None


def _parse_alt_url(html_chunk):
    url_list = html_chunk.find('a', fn=has_param('href'))
    url_list = map((lambda x: x.params['href']), url_list)
    url_list = filter((lambda x: (not x.startswith('autori/'))), url_list)
    if (not url_list):
        return None
    return normalize_url(BASE_URL, url_list[0])

Parse URL from alternative location if not found where it should be.

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

Returns:
str: Book's URL.


def terminate(self, uuid):

        request_url = self._client.base_api_url + self.terminate_url.format(
            id=uuid
        )

        response = self._client.session.post(request_url)

        self.validate_request_success(
            response_text=response.text,
            request_url=request_url,
            status_code=response.status_code,
            expected_status_code=HTTP_202_ACCEPTED,
        )

        return self.response_data_to_model_instance(response.json())

Terminate the task instance with given UUID.

Args:
uuid (str): The UUID of the task instance to terminate.

Returns:
:class:`saltant.models.base_task_instance.BaseTaskInstance`:
A task instance model instance representing the task
instance that was told to terminate.


def centroid_distance(item_a, time_a, item_b, time_b, max_value):

    ax, ay = item_a.center_of_mass(time_a)
    bx, by = item_b.center_of_mass(time_b)
    return np.minimum(np.sqrt((ax - bx) ** 2 + (ay - by) ** 2), max_value) / float(max_value)

Euclidean distance between the centroids of item_a and item_b.

Args:
item_a: STObject from the first set in ObjectMatcher
time_a: Time integer being evaluated
item_b: STObject from the second set in ObjectMatcher
time_b: Time integer being evaluated
max_value: Maximum distance value used as scaling value and upper constraint.

Returns:
Distance value between 0 and 1.


def check_output(expected: List[str]):

    def _check_inner(actual: List[PCollection[str]]):
        formatted_actual = actual | beam.Flatten() | beam.Map(lambda row: str(beam.Row(**row._asdict())))
        assert_matches_stdout(formatted_actual, expected)
    return _check_inner

Helper function to check the output of a pipeline against expected values.

This function takes a list of expected output strings and returns a
callable that can be used within a Beam pipeline to assert that the
actual output matches the expected output.

Args:
expected: A list of strings representing the expected output elements.

Returns:
A callable that takes a list of PCollections and asserts their combined
elements match the expected output.


def VerifyStructure(self, parser_mediator, line):
    try:
        structure = self._LOG_LINE.parseString(line)
    except pyparsing.ParseException:
        logger.debug('Not a Sophos Anti-Virus log file')
        return False
    if (' ' not in (line[8], line[15])):
        logger.debug('Not a Sophos Anti-Virus log file')
        return False
    try:
        dfdatetime_time_elements.TimeElements(time_elements_tuple=structure.date_time)
    except ValueError:
        logger.debug('Not a Sophos Anti-Virus log file, invalid date and time: {0!s}'.format(structure.date_time))
        return False
    return True

Verify that this file is a Sophos Anti-Virus log file.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfVFS.
line (str): line from a text file.

Returns:
bool: True if the line is in the expected format, False if not.


def __init__(self, project_name=None, bq_table=None, bq_dataset=None, publish_to_bq=False, influxdb_options: Optional['InfluxDBMetricsPublisherOptions']=None, namespace=None, filters=None):
    self._namespace = namespace
    self.publishers: List[MetricsPublisher] = []
    self.publishers.append(ConsoleMetricsPublisher())
    bq_check = project_name and bq_table and bq_dataset and publish_to_bq
    if bq_check:
        bq_publisher = BigQueryMetricsPublisher(project_name, bq_table, bq_dataset)
        self.publishers.append(bq_publisher)
    if influxdb_options and influxdb_options.validate():
        self.publishers.append(InfluxDBMetricsPublisher(influxdb_options))
    else:
        _LOGGER.info('Missing InfluxDB options. Metrics will not be published to InfluxDB')
    self.filters = filters

Initializes :class:`MetricsReader` .

Args:
project_name (str): project with BigQuery where metrics will be saved
bq_table (str): BigQuery table where metrics will be saved
bq_dataset (str): BigQuery dataset where metrics will be saved
namespace (str): Namespace of the metrics
filters: MetricFilter to query only filtered metrics


def check_serializable(cls):

    if is_named_tuple(cls):

        return
    if not hasattr(cls, "__new__"):
        print("The class {} does not have a '__new__' attribute and is "
              "probably an old-stye class. Please make it a new-style class "
              "by inheriting from 'object'.")
        raise RayNotDictionarySerializable("The class {} does not have a "
                                           "'__new__' attribute and is "
                                           "probably an old-style class. We "
                                           "do not support this. Please make "
                                           "it a new-style class by "
                                           "inheriting from 'object'."
                                           .format(cls))
    try:
        obj = cls.__new__(cls)
    except Exception:
        raise RayNotDictionarySerializable("The class {} has overridden "
                                           "'__new__', so Ray may not be able "
                                           "to serialize it efficiently."
                                           .format(cls))
    if not hasattr(obj, "__dict__"):
        raise RayNotDictionarySerializable("Objects of the class {} do not "
                                           "have a '__dict__' attribute, so "
                                           "Ray cannot serialize it "
                                           "efficiently.".format(cls))
    if hasattr(obj, "__slots__"):
        raise RayNotDictionarySerializable("The class {} uses '__slots__', so "
                                           "Ray may not be able to serialize "
                                           "it efficiently.".format(cls))

Throws an exception if Ray cannot serialize this class efficiently.

Args:
cls (type): The class to be serialized.

Raises:
Exception: An exception is raised if Ray cannot serialize this class
efficiently.


def _token_to_subtoken_ids(self, token):
    cache_location = (hash(token) % self._cache_size)
    (cache_key, cache_value) = self._cache[cache_location]
    if (cache_key == token):
        return cache_value
    ret = self._escaped_token_to_subtoken_ids(_escape_token(token, self._alphabet))
    self._cache[cache_location] = (token, ret)
    return ret

Converts token to a list of subtoken ids.

Args:
token: a string.
Returns:
a list of integers in the range [0, vocab_size)


def python_value(self, value):
    value = super(JSONField, self).python_value(value)
    if (value is not None):
        return flask.json.loads(value, **self._load_kwargs)

Return the JSON in the database as a ``dict``.

Returns:
dict: The field run through json.loads


def add_chapter(self, c):

        try:
            assert type(c) == chapter.Chapter
        except AssertionError:
            raise TypeError('chapter must be of type Chapter')
        chapter_file_output = os.path.join(self.OEBPS_DIR, self.current_chapter_path)
        c._replace_images_in_chapter(self.OEBPS_DIR)
        c.write(chapter_file_output)
        self._increase_current_chapter_number()
        self.chapters.append(c)

Add a Chapter to your epub.

Args:
c (Chapter): A Chapter object representing your chapter.

Raises:
TypeError: Raised if a Chapter object isn't supplied to this
method.


def not_modified(cls, errors=None):
    if cls.expose_status:
        cls.response.content_type = 'application/json'
        cls.response._status_line = '304 Not Modified'
    return cls(304, None, errors).to_json

Shortcut API for HTTP 304 `Not Modified` response.

Args:
errors (list): Response key/value data.

Returns:
WSResponse Instance.


def memory_usage(self, **kwargs):

    def memory_usage_builder(df, **kwargs):
        return df.memory_usage(**kwargs)
    func = self._build_mapreduce_func(memory_usage_builder, **kwargs)
    return self._full_axis_reduce(0, func)

Returns the memory usage of each column.

Returns:
A new QueryCompiler object containing the memory usage of each column.


class JanusVisionEncoder(nn.Module):

    def __init__(self, config: JanusVisionConfig):
        super().__init__()
        self.config = config
        self.layers = nn.ModuleList([JanusVisionEncoderLayer(config) for _ in range(config.num_hidden_layers)])
        self.gradient_checkpointing = False

    @can_return_tuple
    def forward(self, inputs_embeds, attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None) -> BaseModelOutput:

        output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
        output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        encoder_states = () if output_hidden_states else None
        all_attentions = () if output_attentions else None
        hidden_states = inputs_embeds
        for encoder_layer in self.layers:
            if output_hidden_states:
                encoder_states = encoder_states + (hidden_states,)
            layer_outputs = encoder_layer(hidden_states, attention_mask, output_attentions=output_attentions)
            hidden_states = layer_outputs[0]
            if output_attentions:
                all_attentions = all_attentions + (layer_outputs[1],)
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
[`JanusVisionEncoderLayer`].

Args:
config: JanusVisionConfig


def verifyToken(self, auth):

        if auth in (self.Auth.SkypeToken, self.Auth.Authorize):
            if "skype" not in self.tokenExpiry or datetime.now() >= self.tokenExpiry["skype"]:
                if not hasattr(self, "getSkypeToken"):
                    raise SkypeAuthException("Skype token expired, and no password specified")
                self.getSkypeToken()
        elif auth == self.Auth.RegToken:
            if "reg" not in self.tokenExpiry or datetime.now() >= self.tokenExpiry["reg"]:
                self.getRegToken()

Ensure the authentication token for the given auth method is still valid.

Args:
auth (Auth): authentication type to check

Raises:
.SkypeAuthException: if Skype auth is required, and the current token has expired and can't be renewed


def quick_view(structure, bonds=True, conventional=False, transform=None, show_box=True, bond_tol=0.2, stick_radius=0.1):
    s = structure.copy()
    if conventional:
        s = SpacegroupAnalyzer(s).get_conventional_standard_structure()
    if transform:
        s.make_supercell(transform)
    atom_types = [i.symbol for i in s.species]
    if bonds:
        bonds = []
        for i in range((s.num_sites - 1)):
            sym_i = s[i].specie.symbol
            for j in range((i + 1), s.num_sites):
                sym_j = s[j].specie.symbol
                max_d = ((CovalentRadius.radius[sym_i] + CovalentRadius.radius[sym_j]) + bond_tol)
                if (s.get_distance(i, j, np.array([0, 0, 0])) < max_d):
                    bonds.append((i, j))
    bonds = (bonds if bonds else None)
    mv = MolecularViewer(s.cart_coords, topology={'atom_types': atom_types, 'bonds': bonds})
    if bonds:
        mv.ball_and_sticks(stick_radius=stick_radius)
    for i in s.sites:
        el = i.specie.symbol
        coord = i.coords
        r = CovalentRadius.radius[el]
        mv.add_representation('spheres', {'coordinates': coord.astype('float32'), 'colors': [get_atom_color(el)], 'radii': [(r * 0.5)], 'opacity': 1.0})
    if show_box:
        o = np.array([0, 0, 0])
        (a, b, c) = (s.lattice.matrix[0], s.lattice.matrix[1], s.lattice.matrix[2])
        starts = [o, o, o, a, a, b, b, c, c, (a + b), (a + c), (b + c)]
        ends = [a, b, c, (a + b), (a + c), (b + a), (b + c), (c + a), (c + b), ((a + b) + c), ((a + b) + c), ((a + b) + c)]
        colors = [16777215 for i in range(12)]
        mv.add_representation('lines', {'startCoords': np.array(starts), 'endCoords': np.array(ends), 'startColors': colors, 'endColors': colors})
    return mv

A function to visualize pymatgen Structure objects in jupyter notebook using chemview package.

Args:
structure: pymatgen Structure
bonds: (bool) visualize bonds. Bonds are found by comparing distances
to added covalent radii of pairs. Defaults to True.
conventional: (bool) use conventional cell. Defaults to False.
transform: (list) can be used to make supercells with pymatgen.Structure.make_supercell method
show_box: (bool) unit cell is shown. Defaults to True.
bond_tol: (float) used if bonds=True. Sets the extra distance tolerance when finding bonds.
stick_radius: (float) radius of bonds.
Returns:
A chemview.MolecularViewer object


def _v2_get_resized_embeddings(self, old_embeddings: keras.layers.Embedding, new_num_tokens: int) -> keras.layers.Embedding:
    init_range = 0.02
    potential_initialization_variable_names = ['initializer_range', 'initializer_factor', 'init_std']
    for var_name in potential_initialization_variable_names:
        if hasattr(self.config, var_name):
            init_range = getattr(self.config, var_name)
    new_embeddings = keras.layers.Embedding(input_dim=new_num_tokens, output_dim=old_embeddings.output_dim, embeddings_initializer=keras.initializers.TruncatedNormal(stddev=init_range), name=old_embeddings.embeddings.name[:-13])
    new_embeddings(tf.constant([[0]]))
    if old_embeddings.input_dim >= new_num_tokens:
        init_embeddings = old_embeddings.embeddings[:new_num_tokens]
    else:
        init_embeddings = tf.concat([old_embeddings.embeddings, new_embeddings.embeddings[old_embeddings.input_dim:]], axis=0)
    new_embeddings.embeddings.assign(init_embeddings)
    return new_embeddings

Build a resized Embedding layer from a provided Embedding layer. Increasing the size will add newly initialized
vectors at the end. Reducing the size will remove vectors from the end.

Args:
old_embeddings (`keras.layers.Embedding`):
Old embeddings to be resized.
new_num_tokens (`int`, *optional*):
New number of tokens in the embedding matrix.

Return:
`keras.layers.Embedding`: Resized Embedding layer.


def builder_from_source(source, filename, system_includes,
                        nonsystem_includes, quiet=False):

    return ASTBuilder(tokenize.get_tokens(source),
                      filename,
                      system_includes,
                      nonsystem_includes,
                      quiet=quiet)

Utility method that returns an ASTBuilder from source code.

Args:
source: 'C++ source code'
filename: 'file1'

Returns:
ASTBuilder


def grep(regex, output):

    lines = output.decode('utf-8').strip().splitlines()
    results = []
    for line in lines:
        if re.search(regex, line):
            results.append(line.strip())
    return results

Similar to linux's `grep`, this returns the line in an output stream
that matches a given regex pattern.

It does not rely on the `grep` binary and is not sensitive to line endings,
so it can be used cross-platform.

Args:
regex: string, a regex that matches the expected pattern.
output: byte string, the raw output of the adb cmd.

Returns:
A list of strings, all of which are output lines that matches the
regex pattern.


def run_node(self, node, stim):

        if isinstance(node, string_types):
            node = self.nodes[node]

        result = node.transformer.transform(stim)
        if node.is_leaf():
            return listify(result)

        stim = result

        if len(node.children) > 1 and isgenerator(stim):
            stim = list(stim)
        return list(chain(*[self.run_node(c, stim) for c in node.children]))

Executes the Transformer at a specific node.

Args:
node (str, Node): If a string, the name of the Node in the current
Graph. Otherwise the Node instance to execute.
stim (str, stim, list): Any valid input to the Transformer stored
at the target node.


def start_standing_subprocess(cmd, shell=False, env=None):
    logging.debug('Starting standing subprocess with: %s', cmd)
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, env=env)
    proc.stdin.close()
    proc.stdin = None
    logging.debug('Started standing subprocess %d', proc.pid)
    return proc

Starts a long-running subprocess.

This is not a blocking call and the subprocess started by it should be
explicitly terminated with stop_standing_subprocess.

For short-running commands, you should use subprocess.check_call, which
blocks.

Args:
cmd: string, the command to start the subprocess with.
shell: bool, True to run this command through the system shell,
False to invoke it directly. See subprocess.Proc() docs.
env: dict, a custom environment to run the standing subprocess. If not
specified, inherits the current environment. See subprocess.Popen()
docs.

Returns:
The subprocess that was started.


def _delegate_property(keras_tensor_cls, property_name):
    property_access = property(lambda self: InstanceProperty(property_name)(self))
    setattr(keras_tensor_cls, property_name, property_access)

Register property on a KerasTensor class.

Calling this multiple times with the same arguments should be a no-op.

This method exposes a property on the KerasTensor class that will use an
`InstanceProperty` layer to access the property on the represented
intermediate values in the model.

Args:
keras_tensor_cls: The KerasTensor subclass that should expose the property.
property_name: The name of the property to expose and delegate to the
represented (Composite)Tensor.


def stat(filename, retry_params=None, _account_id=None):

  common.validate_file_path(filename)
  api = storage_api._get_storage_api(retry_params=retry_params,
                                     account_id=_account_id)
  status, headers, content = api.head_object(
      api_utils._quote_filename(filename))
  errors.check_status(status, [200], filename, resp_headers=headers,
                      body=content)
  file_stat = common.GCSFileStat(
      filename=filename,
      st_size=common.get_stored_content_length(headers),
      st_ctime=common.http_time_to_posix(headers.get('last-modified')),
      etag=headers.get('etag'),
      content_type=headers.get('content-type'),
      metadata=common.get_metadata(headers))

  return file_stat

Get GCSFileStat of a Google Cloud storage file.

Args:
filename: A Google Cloud Storage filename of form '/bucket/filename'.
retry_params: An api_utils.RetryParams for this call to GCS. If None,
the default one is used.
_account_id: Internal-use only.

Returns:
a GCSFileStat object containing info about this file.

Raises:
errors.AuthorizationError: if authorization failed.
errors.NotFoundError: if an object that's expected to exist doesn't.


def handle_message(self, ch, method, properties, body):
    input = {}
    headers = {}
    try:
        self.sessid = method.routing_key
        input = json_decode(body)
        data = input['data']
        if ('path' in data):
            if (data['path'] in VIEW_METHODS):
                data['view'] = data['path']
            else:
                data['wf'] = data['path']
        session = Session(self.sessid)
        headers = {'remote_ip': input['_zops_remote_ip'], 'source': input['_zops_source']}
        if ('wf' in data):
            output = self._handle_workflow(session, data, headers)
        elif ('job' in data):
            self._handle_job(session, data, headers)
            return
        else:
            output = self._handle_view(session, data, headers)
    except HTTPError as e:
        import sys
        if hasattr(sys, '_called_from_test'):
            raise
        output = {'cmd': 'error', 'error': self._prepare_error_msg(e.message), 'code': e.code}
        log.exception('Http error occurred')
    except:
        self.current = Current(session=session, input=data)
        self.current.headers = headers
        import sys
        if hasattr(sys, '_called_from_test'):
            raise
        err = traceback.format_exc()
        output = {'cmd': 'error', 'error': self._prepare_error_msg(err), 'code': 500}
        log.exception(('Worker error occurred with messsage body:\n%s' % body))
    if ('callbackID' in input):
        output['callbackID'] = input['callbackID']
    log.info(('OUTPUT for %s: %s' % (self.sessid, output)))
    output['reply_timestamp'] = time()
    self.send_output(output)

this is a pika.basic_consumer callback
handles client inputs, runs appropriate workflows and views

Args:
ch: amqp channel
method: amqp method
properties:
body: message body


def _simplify_non_context_field_binary_composition(expression):

    if any((isinstance(expression.left, ContextField),
            isinstance(expression.right, ContextField))):
        raise AssertionError(u'Received a BinaryComposition {} with a ContextField '
                             u'operand. This should never happen.'.format(expression))

    if expression.operator == u'||':
        if expression.left == TrueLiteral or expression.right == TrueLiteral:
            return TrueLiteral
        else:
            return expression
    elif expression.operator == u'&&':
        if expression.left == TrueLiteral:
            return expression.right
        if expression.right == TrueLiteral:
            return expression.left
        else:
            return expression
    else:
        return expression

Return a simplified BinaryComposition if either operand is a TrueLiteral.

Args:
expression: BinaryComposition without any ContextField operand(s)

Returns:
simplified expression if the given expression is a disjunction/conjunction
and one of it's operands is a TrueLiteral,
and the original expression otherwise


def zipf_random_sample(distr_map, sample_len):

  u = np.random.random(sample_len)

  return list(np.searchsorted(distr_map, u))

Helper function: Generate a random Zipf sample of given length.

Args:
distr_map: list of float, Zipf's distribution over nbr_symbols.
sample_len: integer, length of sequence to generate.

Returns:
sample: list of integer, Zipf's random sample over nbr_symbols.


def pauli_from_char(ch, n=0):

    ch = ch.upper()
    if ch == "I":
        return I
    if ch == "X":
        return X(n)
    if ch == "Y":
        return Y(n)
    if ch == "Z":
        return Z(n)
    raise ValueError("ch shall be X, Y, Z or I")

Make Pauli matrix from an character.

Args:
ch (str): "X" or "Y" or "Z" or "I".
n (int, optional): Make Pauli matrix as n-th qubits.

Returns:
If ch is "X" => X, "Y" => Y, "Z" => Z, "I" => I

Raises:
ValueError: When ch is not "X", "Y", "Z" nor "I".


def make_tex_table(inputlist, outputfile, close=False, fmt=None, **kwargs):
    output_str = ''
    if (fmt is None):
        fmt = {}
    for row in inputlist:
        for (key, val) in enumerate(row):
            if (val is None):
                output_str += '\\text{{{}}}'.format(str(kwargs.get('nonestring', 'None')))
            else:
                if np.isscalar(val):
                    temp_str_fmt = (('$\\num{{' + fmt.get(key, '{:g}')) + '}}$')
                else:
                    temp_str_fmt = fmt.get(key, '{}')
                temp_str = temp_str_fmt.format(val).replace('+', '')
            output_str += (temp_str + '&')
        output_str = output_str[:(- 1)]
        output_str += '\\\\\n'
    outputfile.write(output_str)
    if close:
        outputfile.close()

Parse table from inputlist

Args:
inputlist: list
List to parse
outputfile: file
.tex file to write
fmt: dictionary
key: integer
column index starting with 0
values: string
format string. eg "{:g}"
**kwargs:
nonestring: string
string when objecttype is None
Returns:
None


def register_shape_checker(left_type, right_type, shape_checker_function):
    key = (left_type, right_type)
    if (key in shape_checkers):
        raise ValueError(('Types %s already mapped to %s' % (key, shape_checkers[key])))
    shape_checkers[key] = shape_checker_function

Register a new shape checking function supporting given types.

Shape checkers are primarily used to make sure that the seed derivatives
passed into generated autodiff functions match their corresponding
primal values.

Args:
left_type: A Python type object. The data type of the left operand
supported by the adder.
right_type: A Python type object. The data type of the right operand
supported by the adder.
shape_checker_function: A binary function that takes two arguments, left and
right, of the types left_type and right_type respectively, and returns
a boolean indicating whether or not they match.

Raises:
ValueError: If the given type pair was already registered.


def _parse_input(incoming):
    codes = dict(((k, v) for (k, v) in _AutoCodes().items() if (('{%s}' % k) in incoming)))
    color_codes = dict(((k, ('' if _AutoCodes.DISABLE_COLORS else '\x1b[{0}m'.format(v))) for (k, v) in codes.items()))
    incoming_padded = _pad_input(incoming)
    output_colors = incoming_padded.format(**color_codes)
    groups = sorted(set(_RE_GROUP_SEARCH.findall(output_colors)), key=len, reverse=True)
    groups_simplified = [[x for n in _RE_NUMBER_SEARCH.findall(i) for x in n.split(';')] for i in groups]
    groups_compiled = ['\x1b[{0}m'.format(';'.join(g)) for g in groups_simplified]
    assert (len(groups_compiled) == len(groups))
    output_colors_simplified = output_colors
    for i in range(len(groups)):
        output_colors_simplified = output_colors_simplified.replace(groups[i], groups_compiled[i])
    output_no_colors = _RE_SPLIT.sub('', output_colors_simplified)
    if _AutoCodes.DISABLE_COLORS:
        output_colors_simplified = _RE_NUMBER_SEARCH.sub('', output_colors_simplified)
    return (output_colors_simplified, output_no_colors)

Performs the actual conversion of tags to ANSI escaped codes.

Provides a version of the input without any colors for len() and other methods.

Positional arguments:
incoming -- the input unicode value.

Returns:
2-item tuple. First item is the parsed output. Second item is a version of the input without any colors.


def _render_list(self, items, empty='
<empty>
'):
    if ((not items) or (len(items) == 0)):
        self._segments.append(empty)
        return
    self._segments.append('
')
    for o in items:
        self._segments.append('
')
        self._segments.append(str(o))
        self._segments.append('
')
    self._segments.append('
')

Renders an HTML list with the specified list of strings.

Args:
items: the iterable collection of objects to render.
empty: what to render if the list is None or empty.


def perturb(self, distance):

    def get_rand_vec():
        vector = np.random.randn(3)
        vnorm = np.linalg.norm(vector)
        return (((vector / vnorm) * distance) if (vnorm != 0) else get_rand_vec())
    for i in range(len(self._sites)):
        self.translate_sites([i], get_rand_vec(), frac_coords=False)

Performs a random perturbation of the sites in a structure to break
symmetries.

Args:
distance (float): Distance in angstroms by which to perturb each
site.


def softsign(x):
    return nn.softsign(x)

Softsign of a tensor.

Args:
x: A tensor or variable.

Returns:
A tensor.


def count_matching(self, selector, offset=0):
    if selector.output:
        data = self.streaming_data
    elif selector.buffered:
        data = self.storage_data
    else:
        raise ArgumentError('You can only pass a buffered selector to count_matching', selector=selector)
    count = 0
    for i in range(offset, len(data)):
        reading = data[i]
        stream = DataStream.FromEncoded(reading.stream)
        if selector.matches(stream):
            count += 1
    return count

Count the number of readings matching selector.

Args:
selector (DataStreamSelector): The selector that we want to
count matching readings for.
offset (int): The starting offset that we should begin counting at.

Returns:
int: The number of matching readings.


def update_last_triggered_step(self, step):
    raise NotImplementedError

Update the last triggered time and step number.

Args:
step: The current step.

Returns:
A pair `(elapsed_time, elapsed_steps)`, where `elapsed_time` is the number
of seconds between the current trigger and the last one (a float), and
`elapsed_steps` is the number of steps between the current trigger and
the last one. Both values will be set to `None` on the first trigger.


def __dedup__(self,
                  subject,
                  graph):

        if graph is None:
            return
        for uri in Repository.DEFAULT_ID_URIS:

            for obj_uri in graph.objects(subject=subject, predicate=uri):
                sparql_url = urllib.parse.urljoin(
                    self.base_url,
                    "rest/fcr:sparql")
                sparql_template = Template()
                sparql_query = sparql_template.substitute(
                    uri=uri,
                    obj_uri=obj_uri)
                search_request = urllib.request.Request(
                    sparql_url,
                    data=sparql_query.encode())
                search_request.add_header(
                    "Accept",
                    "text/turtle")
                search_request.add_header(
                    "Content-Type",
                    "application/sparql-query")
                try:
                    search_response = urllib.request.urlopen(search_request)
                    if search_response.code < 400:
                        return rdflib.Graph().parse(
                            data=search_response.read(),
                            format='turtle')
                except urllib.error.HTTPError:
                    print("Error with sparql query:\n{}".format(sparql_query))

Internal method takes a RDF graph, cycles through the RDFS
label and BIBFRAME authorizedAccessPoint triples to see if the graph's
entity already exists in Fedora. As other searchable unique triples are
added from other vocabularies, they should be added to this method.

Args:
subject(rdflib.rdflibURIRef): RDF Subject URI
graph(rdflib.Graph): RDF Graph

Returns:
graph(rdflib.Graph): Existing RDF Graph in Fedora or None


def recode(self, table: pd.DataFrame, validate=False) -> pd.DataFrame:

        series = table[self.name]

        self._check_series_name(series)

        col = self.name

        data = series.copy()

        for recoder in self.recoders.values():
            try:
                data = recoder(data)
            except (BaseException) as err:
                raise RecodingError(col, recoder, err)

        if validate:
            failed_rows = find_failed_rows(self.validate(data.to_frame()))
            if failed_rows.shape[0] > 0:
                raise ValidationError(f"Rows that failed to validate for column '{self.name}':\n{failed_rows}")

        return data.to_frame()

Pass the provided series obj through each recoder function sequentially and return the final result.

Args:
table (pd.DataFrame): A dataframe on which to apply recoding logic.
validate (bool): If ``True``, recoded table must pass validation tests.


def validate(filename=None, ocrd_page=None, ocrd_file=None, strictness='strict', strategy='index1'):

        if ocrd_page:
            validator = PageValidator(ocrd_page, strictness, strategy)
        elif ocrd_file:
            validator = PageValidator(page_from_file(ocrd_file), strictness, strategy)
        elif filename:
            validator = PageValidator(parse(filename, silence=True), strictness, strategy)
        else:
            raise Exception("At least one of ocrd_page, ocrd_file or filename must be set")
        return validator._validate()

Validates a PAGE file for consistency by filename, OcrdFile or passing OcrdPage directly.

Arguments:
filename (string): Path to PAGE
ocrd_page (OcrdPage): OcrdPage instance
ocrd_file (OcrdFile): OcrdFile instance wrapping OcrdPage
strictness (string): 'strict', 'lax', 'fix' or 'off'
strategy (string): Currently only 'index1'

Returns:
report (:class:`ValidationReport`) Report on the validity


def _ragged_getitem_inner_dimensions(rt_input, key_list):
    if not key_list:
        return rt_input
    if not isinstance(rt_input, ragged_tensor.RaggedTensor):
        return rt_input.__getitem__([slice(None, None, None)] + key_list)
    column_key = key_list[0]
    if column_key is Ellipsis:
        expanded_key_list = _expand_ellipsis(key_list, rt_input.values.shape.ndims)
        return _ragged_getitem_inner_dimensions(rt_input, expanded_key_list)
    if column_key is array_ops.newaxis:
        inner_rt = _ragged_getitem_inner_dimensions(rt_input, key_list[1:])
        nsplits = tensor_shape.dimension_at_index(inner_rt.row_splits.shape, 0)
        if nsplits.value is not None:
            nsplits = nsplits.value
        else:
            nsplits = array_ops.shape(inner_rt.row_splits, out_type=inner_rt.row_splits.dtype)[0]
        return ragged_tensor.RaggedTensor.from_uniform_row_length(inner_rt, 1, nrows=nsplits - 1, validate=False)
    if isinstance(column_key, slice):
        if column_key.start is None and column_key.stop is None and (column_key.step is None):
            return rt_input.with_values(_ragged_getitem_inner_dimensions(rt_input.values, key_list[1:]))
        else:
            if not (isinstance(column_key.start, (tensor_lib.Tensor, int, type(None))) and isinstance(column_key.stop, (tensor_lib.Tensor, int, type(None)))):
                raise TypeError('slice offsets must be integers or None')
            starts = rt_input.row_splits[:-1]
            limits = rt_input.row_splits[1:]
            step = 1 if column_key.step is None else column_key.step
            lower_bound = _if_ge_zero(step, lambda: starts, lambda: starts - 1)
            upper_bound = _if_ge_zero(step, lambda: limits, lambda: limits - 1)
            if column_key.start is None:
                inner_rt_starts = _if_ge_zero(step, lambda: starts, lambda: limits - 1)
            else:
                start_offset = math_ops.cast(column_key.start, starts.dtype)
                inner_rt_starts = _if_ge_zero(column_key.start, lambda: math_ops.minimum(starts + start_offset, upper_bound), lambda: math_ops.maximum(limits + start_offset, lower_bound))
            if column_key.stop is None:
                inner_rt_limits = _if_ge_zero(step, lambda: limits, lambda: starts - 1)
            else:
                stop_offset = math_ops.cast(column_key.stop, starts.dtype)
                inner_rt_limits = _if_ge_zero(column_key.stop, lambda: math_ops.minimum(starts + stop_offset, upper_bound), lambda: math_ops.maximum(limits + stop_offset, lower_bound))
            inner_rt = _build_ragged_tensor_from_value_ranges(inner_rt_starts, inner_rt_limits, column_key.step, rt_input.values)
            if rt_input.uniform_row_length is not None:
                new_row_length = _slice_length(rt_input.uniform_row_length, column_key)
                inner_rt = ragged_tensor.RaggedTensor.from_uniform_row_length(inner_rt.values, new_row_length, rt_input.nrows())
            return inner_rt.with_values(_ragged_getitem_inner_dimensions(inner_rt.values, key_list[1:]))
    if rt_input.uniform_row_length is None:
        raise ValueError('Cannot index into an inner ragged dimension.')
    row_length = rt_input.uniform_row_length
    column_key = math_ops.cast(column_key, row_length.dtype)
    oob_err_msg = 'Index out of bounds when indexing into a ragged tensor'
    oob_checks = [check_ops.assert_greater_equal(column_key, -row_length, message=oob_err_msg), check_ops.assert_less(column_key, row_length, message=oob_err_msg)]
    with ops.control_dependencies(oob_checks):
        offset = _if_ge_zero(column_key, lambda: column_key, lambda: row_length + column_key)
        sliced_rt = rt_input.values[offset::row_length]
        return _ragged_getitem_inner_dimensions(sliced_rt, key_list[1:])

Retrieve inner dimensions, keeping outermost dimension unchanged.

Args:
rt_input: The `RaggedTensor` or `Tensor` from which a piece should be
extracted.
key_list: The __getitem__ keys for slicing the inner dimensions.

Returns:
A `RaggedTensor`.

Raises:
ValueError: If key_list is not supported.


def _get_unused_nets(self, vms_to_stop):

        vm_names = [vm.name() for vm in vms_to_stop]
        unused_nets = set()

        for vm in vms_to_stop:
            unused_nets = unused_nets.union(vm.nets())
        for vm in self._vms.values():
            if not vm.running() or vm.name() in vm_names:
                continue
            for net in vm.nets():
                unused_nets.discard(net)
        nets = [self._nets[net] for net in unused_nets]

        return nets

Return a list of nets that used only by the vms in vms_to_stop
Args:
vms_to_stop (list of str): The names of the requested vms
Returns
list of virt.Network: net objects that used only by
vms in vms_to_stop
Raises:
utils.LagoUserException: If a vm name doesn't exist


def idcode(msg):

    if df(msg) not in [5, 21]:
        raise RuntimeError("Message must be Downlink Format 5 or 21.")

    mbin = hex2bin(msg)

    C1 = mbin[19]
    A1 = mbin[20]
    C2 = mbin[21]
    A2 = mbin[22]
    C4 = mbin[23]
    A4 = mbin[24]

    B1 = mbin[26]
    D1 = mbin[27]
    B2 = mbin[28]
    D2 = mbin[29]
    B4 = mbin[30]
    D4 = mbin[31]

    byte1 = int(A4+A2+A1, 2)
    byte2 = int(B4+B2+B1, 2)
    byte3 = int(C4+C2+C1, 2)
    byte4 = int(D4+D2+D1, 2)

    return str(byte1) + str(byte2) + str(byte3) + str(byte4)

Computes identity (squawk code) from DF5 or DF21 message, bit 20-32.
credit: @fbyrkjeland

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

Returns:
string: squawk code


def batch_get_value(tensors):
    if context.executing_eagerly():
        return [x.numpy() for x in tensors]
    elif ops.inside_function():
        raise RuntimeError('Cannot get value inside Tensorflow graph function.')
    if tensors:
        return get_session(tensors).run(tensors)
    else:
        return []

Returns the value of more than one tensor variable.

Args:
tensors: list of ops to run.

Returns:
A list of Numpy arrays.

Raises:
RuntimeError: If this method is called inside defun.


def from_json(cls, raw):

        if raw is None:
            return None

        bcls = None
        _type = raw.get('type')
        try:
            bcls = cls._blob_type_map[BlobType(_type)]
        except (KeyError, ValueError) as e:
            logger.warning('Unknown blob type: %s', _type)
            if DEBUG:
                raise_from(exception.ParseException('Parse error for %s' % (_type), raw), e)
            return None
        blob = bcls()
        blob.load(raw)

        return blob

Helper to construct a blob from a dict.

Args:
raw (dict): Raw blob representation.

Returns:
NodeBlob: A NodeBlob object or None.


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

Checks whether this class can be deleted.

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

Arguments:
cls: A pytd.Class.

Returns:
True if we can delete this class.


def apply(self, pts: torch.Tensor) -> torch.Tensor:
    rotated = self._rots.apply(pts)
    return rotated + self._trans

Applies the transformation to a coordinate tensor.

Args:
pts: A [*, 3] coordinate tensor.
Returns:
The transformed points.


def _cookiecutter_configs_have_changed(template, old_version, new_version):

    temple.check.is_git_ssh_path(template)
    repo_path = temple.utils.get_repo_path(template)
    github_client = temple.utils.GithubClient()
    api = '/repos/{}/contents/cookiecutter.json'.format(repo_path)

    old_config_resp = github_client.get(api, params={'ref': old_version})
    old_config_resp.raise_for_status()
    new_config_resp = github_client.get(api, params={'ref': new_version})
    new_config_resp.raise_for_status()

    return old_config_resp.json()['content'] != new_config_resp.json()['content']

Given an old version and new version, check if the cookiecutter.json files have changed

When the cookiecutter.json files change, it means the user will need to be prompted for
new context

Args:
template (str): The git SSH path to the template
old_version (str): The git SHA of the old version
new_version (str): The git SHA of the new version

Returns:
bool: True if the cookiecutter.json files have been changed in the old and new versions


def _recoverable(self, method, *args, **kwargs):
    while True:
        try:
            return method(*args, **kwargs)
        except Exception as exc:
            with self._operational_lock:
                _LOGGER.debug('Call to retryable %r caused %s.', method, exc)
                if (not self._should_recover(exc)):
                    self.close()
                    _LOGGER.debug('Not retrying %r due to %s.', method, exc)
                    self._finalize(exc)
                    raise exc
                _LOGGER.debug('Re-opening stream from retryable %r.', method)
                self._reopen()

Wraps a method to recover the stream and retry on error.

If a retryable error occurs while making the call, then the stream will
be re-opened and the method will be retried. This happens indefinitely
so long as the error is a retryable one. If an error occurs while
re-opening the stream, then this method will raise immediately and
trigger finalization of this object.

Args:
method (Callable[..., Any]): The method to call.
args: The args to pass to the method.
kwargs: The kwargs to pass to the method.


def f(options, expected_tf_failures=0):
    test_parameters = [{'ksize': [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1], [1, 10, 11, 1]], 'strides': [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1], [1, 10, 11, 1]], 'input_shape': [[], [1, 1, 1, 1], [1, 15, 14, 1], [3, 15, 14, 3]], 'padding': ['SAME', 'VALID'], 'data_format': ['NHWC'], 'fully_quantize': [False], 'quant_16x8': [False]}, {'ksize': [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1], [1, 10, 11, 1]], 'strides': [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1], [1, 10, 11, 1]], 'input_shape': [[], [1, 1, 1, 1], [1, 15, 14, 1], [3, 15, 14, 3]], 'padding': ['SAME', 'VALID'], 'data_format': ['NHWC'], 'fully_quantize': [True], 'quant_16x8': [False]}, {'ksize': [[1, 1, 1, 1]], 'strides': [[1, 1, 1, 1]], 'input_shape': [[1, 1, 1, 1]], 'padding': ['SAME', 'VALID'], 'data_format': ['NHWC'], 'fully_quantize': [True], 'quant_16x8': [True]}]
    if not allow_fully_quantize:
        test_parameters = [test_parameter for test_parameter in test_parameters if True not in test_parameter['fully_quantize']]

    def build_graph(parameters):
        input_tensor = tf.compat.v1.placeholder(dtype=tf.float32, name='input', shape=parameters['input_shape'])
        out = pool_op(input_tensor, ksize=parameters['ksize'], strides=parameters['strides'], data_format=parameters['data_format'], padding=parameters['padding'])
        return ([input_tensor], [out])

    def build_inputs(parameters, sess, inputs, outputs):
        if allow_fully_quantize:
            input_values = create_tensor_data(tf.float32, parameters['input_shape'], min_value=-1, max_value=1)
        else:
            input_values = create_tensor_data(tf.float32, parameters['input_shape'])
        return ([input_values], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))))
    make_zip_of_tests(options, test_parameters, build_graph, build_inputs, expected_tf_failures=expected_tf_failures)

Actual function that generates examples.

Args:
options: An Options instance.
expected_tf_failures: number of expected tensorflow failures.


def substitute_params(self, substitutions):

        param_dict = {}

        for k, v in self.param_dict.items():
            param_dict[k] = replace_substitutions(v, substitutions)
        return param_dict

Substitute param values in our param_dict from params

Args:
substitutions (Map or dict): Values to substitute. E.g. Map of
{"name": "me"}

E.g. if self.param_dict is:
{"name": "$(name):pos", "exposure": 1.0}
And substitutions is:
{"name": "me"}
After the call self.param_dict will be:
{"name": "me:pos", "exposure": 1.0}


def get_if_deleted(self, addresses):

        with self._lock:
            results = []
            for add in addresses:
                results.append(self._get_if_deleted(add))
            return results

Returns a list of addresses that have been deleted, or None if it
hasn't been deleted.

Args:
addresses (list of str): The addresses to check if deleted.

Returns:
(list of str): The addresses, if deleted, or None.


def extract_q_df(self, state_key, action_key):

        q = 0.0
        if self.q_df is None:
            self.save_q_df(state_key, action_key, q)
            return q

        q_df = self.q_df[self.q_df.state_key == state_key]
        q_df = q_df[q_df.action_key == action_key]

        if q_df.shape[0]:
            q = float(q_df["q_value"])
        else:
            self.save_q_df(state_key, action_key, q)
        return q

Extract Q-Value from `self.q_df`.

Args:
state_key:      The key of state.
action_key:     The key of action.

Returns:
Q-Value.


def waitAndGet(self, event_name, timeout=DEFAULT_TIMEOUT):
    if timeout:
        if timeout > MAX_TIMEOUT:
            raise Error(self._ad, 'Specified timeout %s is longer than max timeout %s.' % (timeout, MAX_TIMEOUT))
    try:
        raw_event = self._callEventWaitAndGet(self._id, event_name, timeout)
    except Exception as e:
        if 'EventSnippetException: timeout.' in str(e):
            raise TimeoutError(self._ad, 'Timed out after waiting %ss for event "%s" triggered by %s (%s).' % (timeout, event_name, self._method_name, self._id))
        raise
    return snippet_event.from_dict(raw_event)

Blocks until an event of the specified name has been received and
return the event, or timeout.

Args:
event_name: string, name of the event to get.
timeout: float, the number of seconds to wait before giving up.

Returns:
SnippetEvent, the oldest entry of the specified event.

Raises:
Error: If the specified timeout is longer than the max timeout
supported.
TimeoutError: The expected event does not occur within time limit.


def main_op_with_restore(restore_op_name):
    with ops.control_dependencies([main_op()]):
        main_op_with_restore = control_flow_ops.group(restore_op_name)
    return main_op_with_restore

Returns a main op to init variables, tables and restore the graph.

Returns the main op including the group of ops that initializes all
variables, initialize local variables, initialize all tables and the restore
op name.

Args:
restore_op_name: Name of the op to use to restore the graph.

Returns:
The set of ops to be run as part of the main op upon the load operation.


def process_record_dataset(dataset, is_training, batch_size, shuffle_buffer, parse_record_fn, num_epochs=1, num_gpus=None, examples_per_epoch=None, dtype=tf.float32):
    dataset = dataset.prefetch(buffer_size=batch_size)
    if is_training:
        mlperf_log.resnet_print(key=mlperf_log.INPUT_ORDER)
        dataset = dataset.shuffle(buffer_size=shuffle_buffer)
    dataset = dataset.repeat(num_epochs)
    dataset = dataset.apply(tf.contrib.data.map_and_batch((lambda value: parse_record_fn(value, is_training, dtype)), batch_size=batch_size, num_parallel_batches=1))
    dataset = dataset.prefetch(buffer_size=tf.contrib.data.AUTOTUNE)
    return dataset

Given a Dataset with raw records, return an iterator over the records.

Args:
dataset: A Dataset representing raw records
is_training: A boolean denoting whether the input is for training.
batch_size: The number of samples per batch.
shuffle_buffer: The buffer size to use when shuffling records. A larger
value results in better randomness, but smaller values reduce startup
time and use less memory.
parse_record_fn: A function that takes a raw record and returns the
corresponding (image, label) pair.
num_epochs: The number of epochs to repeat the dataset.
num_gpus: The number of gpus used for training.
examples_per_epoch: The number of examples in an epoch.
dtype: Data type to use for images/features.

Returns:
Dataset of (image, label) pairs ready for iteration.


def __getitem__(self, key):

        for mapping in self.maps:
            try:
                value = mapping[key]
                map_value = value
                if isinstance(value, dict):
                    map_value = dict(DeepChainMap(*list(mapping.get(key, {}) for mapping in self.maps)))
                return map_value
            except KeyError:
                pass
        return self.__missing__(key)

Recursively retrieve value for _key_ in dict.

Args:
key (str): dict key to get all items for


def plugin_privileges(self, name):

        params = {
            'remote': name,
        }

        headers = {}
        registry, repo_name = auth.resolve_repository_name(name)
        header = auth.get_config_header(self, registry)
        if header:
            headers['X-Registry-Auth'] = header

        url = self._url('/plugins/privileges')
        return self._result(
            self._get(url, params=params, headers=headers), True
        )

Retrieve list of privileges to be granted to a plugin.

Args:
name (string): Name of the remote plugin to examine. The
``:latest`` tag is optional, and is the default if omitted.

Returns:
A list of dictionaries representing the plugin's
permissions


def __is_json_error(self, status, headers):

    content_header = headers.get('content-type', '')
    content_type, unused_params = cgi.parse_header(content_header)
    return (status.startswith('400') and
            content_type.lower() in _ALL_JSON_CONTENT_TYPES)

Determine if response is an error.

Args:
status: HTTP status code.
headers: Dictionary of (lowercase) header name to value.

Returns:
True if the response was an error, else False.


def get(self, CachableItem):

        return self.session.\
                        query(self.mapper.factory().__class__).\
                        filter(self.mapper.factory().__class__.__dict__[self.mapper.key()]==CachableItem.getId()).\
                        first()

Returns current ICachedItem for ICachableItem

Args:
CachableItem: ICachableItem, used as a reference to find a cached version

Returns: ICachedItem or None, if CachableItem has not been cached


def __getattr__(self, name):
    if self.has_service_by_name(name):
        return self._service_objects[name]
    return self.__getattribute__(name)

Syntactic sugar to enable direct access of service objects by alias.

Args:
name: string, the alias a service object was registered under.


def load_py(stream, filepath=None):

    with add_sys_paths(config.package_definition_build_python_paths):
        return _load_py(stream, filepath=filepath)

Load python-formatted data from a stream.

Args:
stream (file-like object).

Returns:
dict.


def remove_note(self, username, index):

        self.cached_json['users'][username]['ns'].pop(index)

        if len(self.cached_json['users'][username]['ns']) == 0:
            del self.cached_json['users'][username]

        return '"delete note

Remove a single usernote from the usernotes.

Arguments:
username: the user that for whom you're removing a note (str)
index: the index of the note which is to be removed (int)

Returns the update message for the usernotes wiki


def xresnet50_2(pretrained=False, **kwargs):
    model = XResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['xresnet50']))
    return model

Constructs a XResNet-50 model.

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet


def _encode(self, tokens: List[str], mean: bool) -> Union[List[np.ndarray], np.ndarray]:

        embedded_tokens = []
        for t in tokens:
            try:
                emb = self.tok2emb[t]
            except KeyError:
                try:
                    emb = self._get_word_vector(t)
                except KeyError:
                    emb = np.zeros(self.dim, dtype=np.float32)
                self.tok2emb[t] = emb
            embedded_tokens.append(emb)

        if mean is None:
            mean = self.mean

        if mean:
            filtered = [et for et in embedded_tokens if np.any(et)]
            if filtered:
                return np.mean(filtered, axis=0)
            return np.zeros(self.dim, dtype=np.float32)

        return embedded_tokens

Embed one text sample

Args:
tokens: tokenized text sample
mean: whether to return mean embedding of tokens per sample

Returns:
list of embedded tokens or array of mean values


def to_dd(degrees, minutes, seconds=0):

    sign = -1 if any(i < 0 for i in (degrees, minutes, seconds)) else 1
    return sign * (abs(degrees) + abs(minutes) / 60 + abs(seconds) / 3600)

Convert degrees, minutes and optionally seconds to decimal angle.

Args:
degrees (float): Number of degrees
minutes (float): Number of minutes
seconds (float): Number of seconds

Returns:
float: Angle converted to decimal degrees


def is_valid(self, tol: float=DISTANCE_TOLERANCE) -> bool:
    if (len(self.sites) == 1):
        return True
    all_dists = self.distance_matrix[np.triu_indices(len(self), 1)]
    return bool((np.min(all_dists) > tol))

True if SiteCollection does not contain atoms that are too close
together. Note that the distance definition is based on type of
SiteCollection. Cartesian distances are used for non-periodic
Molecules, while PBC is taken into account for periodic structures.

Args:
tol (float): Distance tolerance. Default is 0.5A.

Returns:
(bool) True if SiteCollection does not contain atoms that are too
close together.


def raise_io_error(self, errno, filename=None):
    raise IOError(errno, self._error_message(errno), filename)

Raises IOError.
The error message is constructed from the given error code and shall
start with the error in the real system.

Args:
errno: A numeric error code from the C variable errno.
filename: The name of the affected file, if any.


def sun_events(self, date=None, zenith=None):

        return (segment.sun_events(date, zenith) for segment in self)

Calculate sunrise/sunset times for locations.

Args:
date (datetime.date): Calculate rise or set for given date
zenith (str): Calculate rise/set events, or twilight times

Returns:
list of list of 2-tuple of datetime.datetime: The time for the
sunrise and sunset events for each point in each segment


def nondominated_sort(inputs: List[pg.DNA]) -> List[List[pg.DNA]]:
    dependency_graph = [[] for i in range(len(inputs))]
    indegree = [0] * len(inputs)
    queue = []
    for i in range(len(inputs)):
        for j in range(len(inputs)):
            if dominates(base.get_fitness(inputs[i]), base.get_fitness(inputs[j])):
                dependency_graph[i].append(j)
            elif dominates(base.get_fitness(inputs[j]), base.get_fitness(inputs[i])):
                indegree[i] += 1
        if indegree[i] == 0:
            queue.append(i)
    result = []
    while queue:
        l = len(queue)
        frontier = []
        for i in range(l):
            parent = queue.pop(0)
            frontier.append(inputs[parent])
            for child in dependency_graph[parent]:
                indegree[child] -= 1
                if indegree[child] == 0:
                    queue.append(child)
        result.append(frontier)
    return result

Algorithm fast-non-dominated-sort implementation using topological sort.

Please see section III A in the original paper.
Args:
inputs: A list of DNA that need to be sorted.

Returns:
A list of sorted frontier (a list of DNA).


def _import_object(self, path, look_for_cls_method):
    last_nth = (2 if look_for_cls_method else 1)
    path = path.split('.')
    module_path = '.'.join(path[:(- last_nth)])
    class_name = path[(- last_nth)]
    module = importlib.import_module(module_path)
    if (look_for_cls_method and (path[(- last_nth):][0] == path[(- last_nth)])):
        class_method = path[(- last_nth):][1]
    else:
        class_method = None
    return (getattr(module, class_name), class_name, class_method)

Imports the module that contains the referenced method.

Args:
path: python path of class/function
look_for_cls_method (bool): If True, treat the last part of path as class method.

Returns:
Tuple. (class object, class name, method to be called)


def process_alias_import_namespace(namespace):

    if is_url(namespace.alias_source):
        alias_source = retrieve_file_from_url(namespace.alias_source)

        _validate_alias_file_content(alias_source, url=namespace.alias_source)
    else:
        namespace.alias_source = os.path.abspath(namespace.alias_source)
        _validate_alias_file_path(namespace.alias_source)
        _validate_alias_file_content(namespace.alias_source)

Validate input arguments when the user invokes 'az alias import'.

Args:
namespace: argparse namespace object.


def _AnnotateIndents(tree):
    if tree.parent is None:
        pytree_utils.SetNodeAnnotation(tree, pytree_utils.Annotation.CHILD_INDENT, '')
    for child in tree.children:
        if child.type == token.INDENT:
            child_indent = pytree_utils.GetNodeAnnotation(tree, pytree_utils.Annotation.CHILD_INDENT)
            if child_indent is not None and child_indent != child.value:
                raise RuntimeError('inconsistent indentation for child', (tree, child))
            pytree_utils.SetNodeAnnotation(tree, pytree_utils.Annotation.CHILD_INDENT, child.value)
        _AnnotateIndents(child)

Annotate the tree with child_indent annotations.

A child_indent annotation on a node specifies the indentation (as a string,
like "  ") of its children. It is inferred from the INDENT child of a node.

Arguments:
tree: root of a pytree. The pytree is modified to add annotations to nodes.

Raises:
RuntimeError: if the tree is malformed.


def offer_answer(pool, answer, rationale, student_id, algo, options):
    if (algo['name'] == 'simple'):
        offer_simple(pool, answer, rationale, student_id, options)
    elif (algo['name'] == 'random'):
        offer_random(pool, answer, rationale, student_id, options)
    else:
        raise UnknownChooseAnswerAlgorithm()

submit a student answer to the answer pool

The answer maybe selected to stay in the pool depending on the selection algorithm

Args:
pool (dict): answer pool
Answer pool format:
{
option1_index: {
'student_id': { can store algorithm specific info here },
...
}
option2_index: ...
}
answer (int): the option student selected
rationale (str): the rationale text
student_id (str): student identifier
algo (str): the selection algorithm
options (dict): the options available in the question

Raises:
UnknownChooseAnswerAlgorithm: when we don't know the algorithm


def extract_possible_actions(self, state_key):

        x, y = state_key
        if self.__map_arr[y][x] == self.__wall_label:
            raise ValueError("It is the wall. (x, y)=(%d, %d)" % (x, y))

        around_map = [(x, y-1), (x, y+1), (x-1, y), (x+1, y)]
        possible_actoins_list = [(_x, _y) for _x, _y in around_map if self.__map_arr[_y][_x] != self.__wall_label and self.__map_arr[_y][_x] != self.__start_point_label]
        return possible_actoins_list

Concreat method.

Args:
state_key       The key of state. this value is point in map.

Returns:
[(x, y)]


def reinterpret(value, new_type):
    if not isinstance(value, ExtensionType):
        raise ValueError(f'reinterpret expects `value` to be a tf.ExtensionType instance; got {value!r}')
    if not (isinstance(new_type, type) and issubclass(new_type, ExtensionType)):
        raise ValueError(f'reinterpret expects `new_type` to be a subclass of tf.ExtensionType; got {new_type!r}')
    fields = [item for item in value.__dict__.items() if not extension_type_field.ExtensionTypeField.is_reserved_name(item[0])]
    new_value = _create_object_from_type_and_dict(new_type, fields)
    new_value._tf_extension_type_convert_fields()
    new_value.__validate__()
    return new_value

Converts a given `ExtensionType` to a new type with compatible fields.

In particular, this can be used to convert a concrete subclass of
`ExtensionType` to an `AnonymousExtensionType`, or vice versa.  When
converting to a non-anonymous ExtensionType, field values are type-checked to
ensure they are consistent with `new_type`'s type annotations, and validated
with `new_type.__validate__`.

Args:
value: An instance of a subclass of `tf.ExtensionType`
new_type: A subclass of `tf.ExtensionType`

Returns:
An instance of `new_type`, whose fields are copied from `value`.


def load_virt_stream(virt_fd):
    try:
        virt_conf = json.load(virt_fd)
    except ValueError:
        virt_fd.seek(0)
        virt_conf = yaml.load(virt_fd)
    return deepcopy(virt_conf)

Loads the given conf stream into a dict, trying different formats if
needed

Args:
virt_fd (str): file like objcect with the virt config to load

Returns:
dict: Loaded virt config


def _SanitizeField(self, field):

    if self._field_delimiter and isinstance(field, py2to3.STRING_TYPES):
      return field.replace(self._field_delimiter, ' ')
    return field

Sanitizes a field for output.

This method replaces any field delimiters with a space.

Args:
field (str): name of the field to sanitize.

Returns:
str: value of the field.


def add_filter(self, package_filter):
    filters = (self.filters + [package_filter])
    self.filters = sorted(filters, key=(lambda x: x.cost))

Add a filter to the list.

Args:
package_filter (`PackageFilter`): Filter to add.


def read(self, save_path, options=None):
    return self.restore(save_path, options)

Restore the checkpointed variables.

This method has exactly the same logic as restore(). This method is
implemented only to fulfill the duty of subclassing tf.train.Checkpoint.

Args:
save_path: The full name of the checkpoint file to be restored.
options: CheckpointOption instance.

Returns:
A load status object, which can be used to make assertions about the
status of a checkpoint restoration. See tf.train.Checkpoint.restore()
for more details.


def _get_args_name_from_parser(parser):

    return [action.dest for action in parser._actions if not
            isinstance(action, argparse._HelpAction)]

Retrieve the name of the function argument linked to the given parser.

Args:
parser: a function parser


def publish_state(self, state):

        message = json.dumps({'state': {'reported': state}})
        self.client.publish(self.topic, message)
        self._state = state

Publish thing state to AWS IoT.

Args:
state (dict): object state. Must be JSON serializable (i.e., not
have circular references).


def run_excel_to_html():

    parser = argparse.ArgumentParser(prog='excel_to_html')
    parser.add_argument('-p', nargs='?', help='Path to an excel file for conversion.')
    parser.add_argument(
        '-s',
        nargs='?',
        help='The name of a sheet in our excel file. Defaults to "Sheet1".',
    )
    parser.add_argument(
        '-css', nargs='?', help='Space separated css classes to append to the table.'
    )
    parser.add_argument(
        '-m', action='store_true', help='Merge, attempt to combine merged cells.'
    )
    parser.add_argument(
        '-c', nargs='?', help='Caption for creating an accessible table.'
    )
    parser.add_argument(
        '-d',
        nargs='?',
        help='Two strings separated by a | character. The first string \
        is for the html "summary" attribute and the second string is for the html "details" attribute. \
        both values must be provided and nothing more.',
    )
    parser.add_argument(
        '-r', action='store_true', help='Row headers. Does the table have row headers?'
    )

    args = parser.parse_args()
    inputs = {
        'p': args.p,
        's': args.s,
        'css': args.css,
        'm': args.m,
        'c': args.c,
        'd': args.d,
        'r': args.r,
    }

    p = inputs['p']
    s = inputs['s'] if inputs['s'] else 'Sheet1'
    css = inputs['css'] if inputs['css'] else ''
    m = inputs['m'] if inputs['m'] else False
    c = inputs['c'] if inputs['c'] else ''
    d = inputs['d'].split('|') if inputs['d'] else []
    r = inputs['r'] if inputs['r'] else False

    html = fp.excel_to_html(
        p, sheetname=s, css_classes=css, caption=c, details=d, row_headers=r, merge=m
    )

    print(html)

Run the excel_to_html function from the
command-line.

Args:
-p path to file
-s name of the sheet to convert
-css classes to apply
-m attempt to combine merged cells
-c caption for accessibility
-su summary for accessibility
-d details for accessibility

Example use:

excel_to_html -p myfile.xlsx -s SheetName -css diablo-python -m true


def master(self, task_type=None, task_id=None, rpc_layer=None):
    if task_type is not None and task_id is not None:
        master = self.cluster_spec().task_address(task_type, task_id)
    else:
        master = self._master
    return format_master_url(master, rpc_layer=rpc_layer or self._rpc_layer)

Returns the master address to use when creating a session.

Note: this is only useful for TensorFlow 1.x.

Args:
task_type: (Optional) The type of the TensorFlow task of the master.
task_id: (Optional) The index of the TensorFlow task of the master.
rpc_layer: (Optional) The RPC used by distributed TensorFlow.

Returns:
The name or URL of the session master.

If a task_type and task_id is given, this will override the `master`
string passed into the initialization function.


def is_smart(self, value):
    self.set_bool('is_smart', value)
    if (value is True):
        if (self.find('criteria') is None):
            self.criteria = ElementTree.SubElement(self, 'criteria')

Set group is_smart property to value.

Args:
value: Boolean.


def _compute_gradients(self, loss_fn, x, unused_optim_state):
    assert ((len(x) == 1) and isinstance(x, list)), 'x should be a list and contain only one image tensor'
    x = x[0]
    loss = reduce_mean(loss_fn(x), axis=0)
    return tf.gradients(loss, x)

Compute a new value of `x` to minimize `loss_fn`.

Args:
loss_fn: a callable that takes `x`, a batch of images, and returns
a batch of loss values. `x` will be optimized to minimize
`loss_fn(x)`.
x: A list of Tensors, the values to be updated. This is analogous
to the `var_list` argument in standard TF Optimizer.
unused_optim_state: A (possibly nested) dict, containing any state
info needed for the optimizer.

Returns:
new_x: A list of Tensors, the same length as `x`, which are updated
new_optim_state: A dict, with the same structure as `optim_state`,
which have been updated.


def __sub__(self, period_tensor):
    return self + periods.PeriodTensor(-period_tensor.quantity(), period_tensor.period_type())

Subtracts a tensor of periods.

When subtracting months or years, the resulting day of the month is
decreased to the largest valid value if necessary. E.g. 31.03.2020 - 1 month
= 29.02.2020, 29.02.2020 - 1 year = 28.02.2019.

Args:
period_tensor: a PeriodTensor object broadcastable to the shape of "self".

Returns:
The new instance of DateTensor.


def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
    vision_data = {}
    if image_sizes is not None:
        size = kwargs.get('size', None) or self.image_processor.size
        padded_height, padded_width = (size['height'], size['width'])
        num_image_tokens = []
        num_image_patches = [1] * len(image_sizes)
        for image_size in image_sizes:
            height_scale_factor = padded_height / image_size[0]
            width_scale_factor = padded_width / image_size[1]
            optimal_scale_factor = min(height_scale_factor, width_scale_factor)
            image_unpadded_h = min(int(image_size[0] * optimal_scale_factor), image_size[0])
            image_unpadded_w = min(int(image_size[0] * optimal_scale_factor), image_size[0])
            model_image_input = self.image_processor.preprocess_with_tokenizer_info(image_input=torch.zeros(1, 1, 3, padded_height, padded_width), image_present=torch.ones(1, 1, 1), image_unpadded_h=torch.tensor([[image_unpadded_h]]), image_unpadded_w=torch.tensor([[image_unpadded_w]]), image_placeholder_id=0, image_newline_id=0, variable_sized=True)
            num_image_tokens.append(model_image_input['image_input_ids'][0][0].shape[-1])
        vision_data.update({'num_image_tokens': num_image_tokens, 'num_image_patches': num_image_patches})
    return MultiModalData(**vision_data)

Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

Args:
image_sizes (`List[List[int]]`, *optional*):
The input sizes formatted as (height, width) per each image.

Returns:
`MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
input modalities, along with other useful data.


def _find_elements(self, result, elements):

    element_mapping = {}
    result = StringIO.StringIO(result)
    for _, e in ET.iterparse(result, events=('end',)):
      if not elements:
        break
      if e.tag in elements:
        element_mapping[e.tag] = e.text
        elements.remove(e.tag)
    return element_mapping

Find interesting elements from XML.

This function tries to only look for specified elements
without parsing the entire XML. The specified elements is better
located near the beginning.

Args:
result: response XML.
elements: a set of interesting element tags.

Returns:
A dict from element tag to element value.


def __init__(self, artifact_name):

    precondition.AssertType(artifact_name, Text)
    self._artifact_name = artifact_name

Initializes the artifact parser factory.

Args:
artifact_name: A name of the artifact this factory is supposed to provide
parser instances for.


def static(cls):
  r
  for attr in dir(cls):
      im_func = getattr(getattr(cls, attr), 'im_func', None)
      if im_func:
        setattr(cls, attr, staticmethod(im_func))

  return cls

r"""Converts the given class into a static one, by changing all the methods of it into static methods.

Args:
cls (class): The class to be converted.


def add_execution_data(self, context_id, data):

        if context_id not in self._contexts:
            LOGGER.warning("Context_id not in contexts, %s", context_id)
            return False

        context = self._contexts.get(context_id)
        context.add_execution_data(data)
        return True

Within a context, append data to the execution result.

Args:
context_id (str): the context id returned by create_context
data (bytes): data to append

Returns:
(bool): True if the operation is successful, False if
the context_id doesn't reference a known context.


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

Returns a tensor that is split along the given dimension.

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


def merge_wells(self, right, keys=None):
    wells = []
    for w in self:
        rw = right.get_well(w.uwi)
        if (rw is not None):
            if (keys is None):
                keys = list(rw.data.keys())
            for k in keys:
                try:
                    w.data[k] = rw.data[k]
                except:
                    pass
        wells.append(w)
    return Project(wells)

Returns a new Project object containing wells from self where
curves from the wells on the right have been added. Matching between
wells in self and right is based on uwi match and ony wells in self
are considered

Args:
uwi (string): the UWI string for the well.

Returns:
project


def BalanceFor(self, assetId):

        for key, fixed8 in self.Balances.items():
            if key == assetId:
                return fixed8
        return Fixed8(0)

Get the balance for a given asset id.

Args:
assetId (UInt256):

Returns:
Fixed8: balance value.


def inflate_plugin_list(plugin_list, inflate_plugin):

        plugins = []
        for plugin_def in plugin_list:
            if isinstance(plugin_def, str):
                try:
                    plugins.append(inflate_plugin(plugin_def))
                except PluginNotFoundError as e:
                    logger.error('Could not import plugin identified by %s. '
                                 'Exception: %s.', plugin_def, e)
            elif isinstance(plugin_def, dict):
                if len(plugin_def) > 1:
                    raise ValueError(
                        'When using a plugin list, each dictionary item '
                        'must contain only one key.')
                identifier = list(plugin_def.keys())[0]
                definition = plugin_def[identifier]
                try:
                    plugins.append(inflate_plugin(identifier, definition))
                except PluginNotFoundError as e:
                    logger.error('Could not import plugin identified by %s. '
                                 'Inflate method: %s. Exception: %s.',
                                 identifier, inflate_plugin, e)
        return plugins

Inflate a list of strings/dictionaries to a list of plugin instances.

Args:
plugin_list (list): a list of str/dict.
inflate_plugin (method): the method to inflate the plugin.

Returns:
list: a plugin instances list.

Raises:
ValueError: when a dictionary item contains more than one key.


def longitude(self, value=0.0):
    if (value is not None):
        try:
            value = float(value)
        except ValueError:
            raise ValueError('value {} need to be of type float for field `longitude`'.format(value))
        if (value < (- 180.0)):
            raise ValueError('value need to be greater or equal -180.0 for field `longitude`')
        if (value > 180.0):
            raise ValueError('value need to be smaller 180.0 for field `longitude`')
    self._longitude = value

Corresponds to IDD Field `longitude`

- is West, + is East, degree minutes represented in decimal (i.e. 30 minutes is .5)

Args:
value (float): value for IDD Field `longitude`
Unit: deg
Default value: 0.0
value >= -180.0
value <= 180.0
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def from_specification(specification, env_prefix=None, separator='.', parent_names=None):
    items = {}
    for (item_name, item_info) in six.iteritems(specification):
        names = (copy.copy(parent_names) if parent_names else [])
        items[item_name] = _generate_item(item_name, item_info, env_prefix, separator, names)
    return items

Used to create YapconfItems from a specification dictionary.

Args:
specification (dict): The specification used to
initialize ``YapconfSpec``
env_prefix (str): Prefix to add to environment names
separator (str): Separator for nested items
parent_names (list): Parents names of any given item

Returns:
A dictionary of names to YapconfItems


def memory(self):

    class GpuMemoryInfo(Structure):
        _fields_ = [('total', c_ulonglong), ('free', c_ulonglong), ('used', c_ulonglong)]
    c_memory = GpuMemoryInfo()
    _check_return(_NVML.get_function('nvmlDeviceGetMemoryInfo')(self.hnd, byref(c_memory)))
    return {'total': c_memory.total, 'free': c_memory.free, 'used': c_memory.used}

Memory information in bytes

Example:

>>> print(ctx.device(0).memory())
{'total': 4238016512L, 'used': 434831360L, 'free': 3803185152L}

Returns:
total/used/free memory in bytes


def integer_value_convert(dictin, dropfailedvalues=False):
    return key_value_convert(dictin, valuefn=int, dropfailedvalues=dropfailedvalues)

Convert values of dictionary to integers

Args:
dictin (DictUpperBound): Input dictionary
dropfailedvalues (bool): Whether to drop dictionary entries where key conversion fails. Defaults to False.

Returns:
Dict: Dictionary with values converted to integers


def FromJsonString(self, value):

    timezone_offset = value.find('Z')
    if timezone_offset == -1:
      timezone_offset = value.find('+')
    if timezone_offset == -1:
      timezone_offset = value.rfind('-')
    if timezone_offset == -1:
      raise ParseError(
          'Failed to parse timestamp: missing valid timezone offset.')
    time_value = value[0:timezone_offset]

    point_position = time_value.find('.')
    if point_position == -1:
      second_value = time_value
      nano_value = ''
    else:
      second_value = time_value[:point_position]
      nano_value = time_value[point_position + 1:]
    date_object = datetime.strptime(second_value, _TIMESTAMPFOMAT)
    td = date_object - datetime(1970, 1, 1)
    seconds = td.seconds + td.days * _SECONDS_PER_DAY
    if len(nano_value) > 9:
      raise ParseError(
          'Failed to parse Timestamp: nanos {0} more than '
          '9 fractional digits.'.format(nano_value))
    if nano_value:
      nanos = round(float('0.' + nano_value) * 1e9)
    else:
      nanos = 0

    if value[timezone_offset] == 'Z':
      if len(value) != timezone_offset + 1:
        raise ParseError('Failed to parse timestamp: invalid trailing'
                         ' data {0}.'.format(value))
    else:
      timezone = value[timezone_offset:]
      pos = timezone.find(':')
      if pos == -1:
        raise ParseError(
            'Invalid timezone offset value: {0}.'.format(timezone))
      if timezone[0] == '+':
        seconds -= (int(timezone[1:pos])*60+int(timezone[pos+1:]))*60
      else:
        seconds += (int(timezone[1:pos])*60+int(timezone[pos+1:]))*60

    self.seconds = int(seconds)
    self.nanos = int(nanos)

Parse a RFC 3339 date string format to Timestamp.

Args:
value: A date string. Any fractional digits (or none) and any offset are
accepted as long as they fit into nano-seconds precision.
Example of accepted format: '1972-01-01T10:00:20.021-05:00'

Raises:
ParseError: On parsing problems.


def digitize(x, bins):
    if any_symbolic_tensors((x, bins)):
        return Digitize().symbolic_call(x, bins)
    return backend.numpy.digitize(x, bins)

Returns the indices of the bins to which each value in `x` belongs.

Args:
x: Input array to be binned.
bins: Array of bins. It has to be one-dimensional and monotonically
increasing.

Returns:
Output array of indices, of same shape as `x`.

Example:
>>> x = np.array([0.0, 1.0, 3.0, 1.6])
>>> bins = np.array([0.0, 3.0, 4.5, 7.0])
>>> keras.ops.digitize(x, bins)
array([1, 1, 2, 1])


def sholl_crossings(neurites, center, radii):

    def _count_crossings(neurite, radius):

        r2 = radius ** 2
        count = 0
        for start, end in iter_segments(neurite):
            start_dist2, end_dist2 = (morphmath.point_dist2(center, start),
                                      morphmath.point_dist2(center, end))

            count += int(start_dist2 <= r2 <= end_dist2 or
                         end_dist2 <= r2 <= start_dist2)

        return count

    return np.array([sum(_count_crossings(neurite, r)
                         for neurite in iter_neurites(neurites))
                     for r in radii])

calculate crossings of neurites

Args:
nrn(morph): morphology on which to perform Sholl analysis
radii(iterable of floats): radii for which crossings will be counted

Returns:
Array of same length as radii, with a count of the number of crossings
for the respective radius


def GetHelper(cls, type_indicator):

    if type_indicator not in cls._resolver_helpers:
      raise KeyError(
          'Resolver helper not set for type indicator: {0:s}.'.format(
              type_indicator))

    return cls._resolver_helpers[type_indicator]

Retrieves the path specification resolver helper for the specified type.

Args:
type_indicator (str): type indicator.

Returns:
ResolverHelper: a resolver helper.

Raises:
KeyError: if resolver helper is not set for the corresponding type
indicator.


def read(self):
    try:
        buf = os.read(self._fd, 2)
    except OSError as e:
        raise GPIOError(e.errno, ('Reading GPIO: ' + e.strerror))
    try:
        os.lseek(self._fd, 0, os.SEEK_SET)
    except OSError as e:
        raise GPIOError(e.errno, ('Rewinding GPIO: ' + e.strerror))
    if (buf[0] == b'0'[0]):
        return False
    elif (buf[0] == b'1'[0]):
        return True
    raise GPIOError(None, ('Unknown GPIO value: "%s"' % buf[0]))

Read the state of the GPIO.

Returns:
bool: ``True`` for high state, ``False`` for low state.

Raises:
GPIOError: if an I/O or OS error occurs.


def AddArguments(cls, argument_group):

    argument_group.add_argument(
        '--status_view', '--status-view', dest='status_view_mode',
        choices=['linear', 'none', 'window'], action='store',
        metavar='TYPE', default=status_view.StatusView.MODE_WINDOW, help=(
            'The processing status view mode: "linear", "none" or "window".'))

Adds command line arguments to an argument group.

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

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


def _radix_int_handler_factory(radix_indicators, charset, parse_func):

    def assertion(c, ctx):
        return c in radix_indicators and \
               ((len(ctx.value) == 1 and ctx.value[0] == _ZERO) or
                (len(ctx.value) == 2 and ctx.value[0] == _MINUS and ctx.value[1] == _ZERO)) and \
               ctx.ion_type == IonType.INT
    return _numeric_handler_factory(charset, lambda prev, c, ctx, trans: _illegal_character(c, ctx),
                                    assertion, radix_indicators, parse_func, illegal_at_end=radix_indicators)

Generates a handler co-routine which tokenizes a integer of a particular radix.

Args:
radix_indicators (sequence): The set of ordinals of characters that indicate the radix of this int.
charset (sequence): Set of ordinals of legal characters for this radix.
parse_func (callable): Called upon ending the numeric value. Accepts the current token value and returns a
thunk that lazily parses the token.


def write_vasp_input(self, vasp_input_set=MPRelaxSet, output_dir='.', create_directory=True, **kwargs):
    vasp_input_set(self.final_structure, **kwargs).write_input(output_dir, make_dir_if_not_present=create_directory)
    with open(os.path.join(output_dir, 'transformations.json'), 'w') as fp:
        json.dump(self.as_dict(), fp)

Writes VASP input to an output_dir.

Args:
vasp_input_set:
pymatgen.io.vaspio_set.VaspInputSet like object that creates
vasp input files from structures
output_dir: Directory to output files
create_directory: Create the directory if not present. Defaults to
True.
\\*\\*kwargs: All keyword args supported by the VASP input set.


def GetGroupMap(self, since=None):
    return GroupUpdateGetter().GetUpdates(self._GetClient(), self.conf['bucket'], self.conf['group_object'], since)

Return the group map from this source.

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

Returns:
instance of group.GroupMap


def parse_dataset_name(name, project_id=None):
    _project_id = _dataset_id = None
    if isinstance(name, basestring):
        m = re.match(_ABS_DATASET_NAME_PATTERN, name, re.IGNORECASE)
        if (m is not None):
            (_project_id, _dataset_id) = m.groups()
        else:
            m = re.match(_REL_DATASET_NAME_PATTERN, name)
            if (m is not None):
                groups = m.groups()
                _dataset_id = groups[0]
    elif isinstance(name, dict):
        try:
            _dataset_id = name['dataset_id']
            _project_id = name['project_id']
        except KeyError:
            pass
    elif (len(name) == 2):
        (_project_id, _dataset_id) = name
    elif (len(name) == 1):
        _dataset_id = name[0]
    if (not _dataset_id):
        raise Exception(('Invalid dataset name: ' + str(name)))
    if (not _project_id):
        _project_id = project_id
    return DatasetName(_project_id, _dataset_id)

Parses a dataset name into its individual parts.

Args:
name: the name to parse, or a tuple, dictionary or array containing the parts.
project_id: the expected project ID. If the name does not contain a project ID,
this will be used; if the name does contain a project ID and it does not match
this, an exception will be thrown.
Returns:
A DatasetName named tuple for the dataset.
Raises:
Exception: raised if the name doesn't match the expected formats or a project_id was
specified that does not match that in the name.


def connect_socket(root_dir):
    config_dir = os.path.join(root_dir, '.config/pueue')
    try:
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        socket_path = os.path.join(config_dir, 'pueue.sock')
        if os.path.exists(socket_path):
            client.connect(socket_path)
        else:
            print("Socket doesn't exist")
            raise Exception
    except:
        print('Error connecting to socket. Make sure the daemon is running')
        sys.exit(1)
    return client

Connect to a daemon's socket.

Args:
root_dir (str): The directory that used as root by the daemon.

Returns:
socket.socket: A socket that is connected to the daemon.


def _parse_line(cls, line):
    try:
        (pkg, rest) = line.split(None, 1)
    except ValueError:
        rpm = cls._parse_package(line.strip())
        return rpm
    rpm = cls._parse_package(pkg)
    rest = rest.split('\t')
    for (i, value) in enumerate(rest):
        rpm[cls.SOSREPORT_KEYS[i]] = value
    return rpm

Helper method for parsing package line with or without SOS report information.

Args:
line (str): package line with or without SOS report information

Returns:
dict: dictionary containing 'name', 'version', 'release' and 'arch' keys plus
additionally 'installtime', 'buildtime', 'vendor', 'buildserver', 'pgpsig',
'pgpsig_short' if these are present.


def __init__(self, model_content, custom_op_registerers_by_name=None, custom_op_registerers_by_func=None):
    if not model_content:
        raise ValueError('`model_content` must be specified.')
    if custom_op_registerers_by_name is None:
        custom_op_registerers_by_name = []
    if custom_op_registerers_by_func is None:
        custom_op_registerers_by_func = []
    try:
        self._calibrator = _calibration_wrapper.CalibrationWrapper(model_content, custom_op_registerers_by_name, custom_op_registerers_by_func)
        self._model_content = model_content
    except Exception as e:
        raise ValueError('Failed to parse the model: %s.' % e)
    if not self._calibrator:
        raise ValueError('Failed to parse the model.')
    self._interpreter = None

Constructor.

Args:
model_content: Content of a TF-Lite Flatbuffer file.
custom_op_registerers_by_name: List of str (symbol names) that take a
pointer to a MutableOpResolver and register custom ops.
custom_op_registerers_by_func: List of functions that take a pointer to a
MutableOpResolver and register custom ops.

Raises:
ValueError: If the calibrator was unable to open the model.


def as_span(cls, lower_version=None, upper_version=None,
                lower_inclusive=True, upper_inclusive=True):

        lower = (None if lower_version is None
                 else _LowerBound(lower_version, lower_inclusive))
        upper = (None if upper_version is None
                 else _UpperBound(upper_version, upper_inclusive))
        bound = _Bound(lower, upper)

        range = cls(None)
        range.bounds = [bound]
        return range

Create a range from lower_version..upper_version.

Args:
lower_version: Version object representing lower bound of the range.
upper_version: Version object representing upper bound of the range.

Returns:
`VersionRange` object.


def dispatch(self, state_change: StateChange) -> List[Event]:
    assert isinstance(state_change, StateChange)
    next_state = deepcopy(self.current_state)
    iteration = self.state_transition(next_state, state_change)
    assert isinstance(iteration, TransitionResult)
    self.current_state = iteration.new_state
    events = iteration.events
    assert isinstance(self.current_state, (State, type(None)))
    assert all((isinstance(e, Event) for e in events))
    return events

Apply the `state_change` in the current machine and return the
resulting events.

Args:
state_change: An object representation of a state
change.

Return:
A list of events produced by the state transition.
It's the upper layer's responsibility to decided how to handle
these events.


def structure_transform(self, original_structure, new_structure, refine_rotation=True):
    sm = StructureMatcher()
    if (not sm.fit(original_structure, new_structure)):
        warnings.warn('original and new structures do not match!')
    trans_1 = self.get_ieee_rotation(original_structure, refine_rotation)
    trans_2 = self.get_ieee_rotation(new_structure, refine_rotation)
    new = self.rotate(trans_1)
    new = new.rotate(np.transpose(trans_2))
    return new

Transforms a tensor from one basis for an original structure
into a new basis defined by a new structure.

Args:
original_structure (Structure): structure corresponding
to the basis of the current tensor
new_structure (Structure): structure corresponding to the
desired basis
refine_rotation (bool): whether to refine the rotations
generated in get_ieee_rotation

Returns:
Tensor that has been transformed such that its basis
corresponds to the new_structure's basis