def run(self, path_or_tests, dot_env_path=None, mapping=None):
    if validator.is_testcase_path(path_or_tests):
        return self.run_path(path_or_tests, dot_env_path, mapping)
    elif validator.is_testcases(path_or_tests):
        return self.run_tests(path_or_tests)
    else:
        raise exceptions.ParamsError('Invalid testcase path or testcases: {}'.format(path_or_tests))

main interface.

Args:
path_or_tests:
str: testcase/testsuite file/foler path
dict: valid testcase/testsuite data


def compare_python_to_reference_murmur3_64(data: Any, seed: int = 0) -> None:

    assert mmh3, "Need mmh3 module"
    c_data = to_str(data)
    c_signed_low, c_signed_high = mmh3.hash64(c_data, seed=seed,
                                              x64arch=IS_64_BIT)
    py_data = to_bytes(c_data)
    py_signed_low, py_signed_high = pymmh3_hash64(py_data, seed=seed)
    preamble = "Hashing {data} with MurmurHash3/64-bit values from 128-bit " \
               "hash/seed={seed}".format(data=repr(data), seed=seed)
    if c_signed_low == py_signed_low and c_signed_high == py_signed_high:
        print(preamble + " -> (low={low}, high={high}): OK".format(
            low=c_signed_low, high=c_signed_high))
    else:
        raise AssertionError(
            preamble +
            "; mmh3 says {c_data} -> (low={c_low}, high={c_high}), Python "
            "version says {py_data} -> (low={py_low}, high={py_high})".format(
                c_data=repr(c_data),
                c_low=c_signed_low,
                c_high=c_signed_high,
                py_data=repr(py_data),
                py_low=py_signed_low,
                py_high=py_signed_high))

Checks the pure Python implementation of 64-bit murmur3 against the
``mmh3`` C-based module.

Args:
data: data to hash
seed: seed

Raises:
AssertionError: if the two calculations don't match


def columns(self, dimensions=None):

        if dimensions is None:
            dimensions = self.kdims
        else:
            dimensions = [self.get_dimension(d, strict=True) for d in dimensions]
        vdims = [d for d in dimensions if d in self.vdims]
        if vdims:
            raise ValueError('%s element does not hold data for value '
                             'dimensions. Could not return data for %s '
                             'dimension(s).' %
                             (type(self).__name__, ', '.join([d.name for d in vdims])))
        return OrderedDict([(d.name, self.dimension_values(d)) for d in dimensions])

Convert dimension values to a dictionary.

Returns a dictionary of column arrays along each dimension
of the element.

Args:
dimensions: Dimensions to return as columns

Returns:
Dictionary of arrays for each dimension


def __init__(self, module_map, self_name=None, module_alias_map=None):
    super().__init__(allow_singletons=False)
    self._module_map = module_map
    self._module_alias_map = module_alias_map or {}
    self.name = self_name
    self._alias_names = []
    self._in_generic_type = 0
    self._star_imports = set()
    self._unit = module_map.get(self_name)

Create this visitor.

Args:
module_map: A dictionary mapping module names to symbol tables.
self_name: The name of the current module. If provided, then the visitor
will ignore nodes with this module name.
module_alias_map: A dictionary mapping module aliases to real module
names. If the source contains "import X as Y", module_alias_map should
contain an entry mapping "Y": "X".


def set_attributes(self, **kwargs):

        if self._subresource_map:
            self.set_subresources(**kwargs)
            for key in self._subresource_map.keys():

                kwargs.pop(key, None)
        for field, value in kwargs.items():
            if field in self.Meta.attributes:
                setattr(self, field, value)

Set the resource attributes from the kwargs.
Only sets items in the `self.Meta.attributes` white list.

Subclass this method to customise attributes.

Args:
kwargs: Keyword arguements passed into the init of this class


def split_cl_function(cl_str):

    class Semantics:

        def __init__(self):
            self._return_type = ''
            self._function_name = ''
            self._parameter_list = []
            self._cl_body = ''

        def result(self, ast):
            return self._return_type, self._function_name, self._parameter_list, self._cl_body

        def address_space(self, ast):
            self._return_type = ast.strip() + ' '
            return ast

        def data_type(self, ast):
            self._return_type += ''.join(ast).strip()
            return ast

        def function_name(self, ast):
            self._function_name = ast.strip()
            return ast

        def arglist(self, ast):
            if ast != '()':
                self._parameter_list = ast
            return ast

        def body(self, ast):
            def join(items):
                result = ''
                for item in items:
                    if isinstance(item, str):
                        result += item
                    else:
                        result += join(item)
                return result

            self._cl_body = join(ast).strip()[1:-1]
            return ast

    return _split_cl_function_parser.parse(cl_str, semantics=Semantics())

Split an CL function into a return type, function name, parameters list and the body.

Args:
cl_str (str): the CL code to parse and plit into components

Returns:
tuple: string elements for the return type, function name, parameter list and the body


def quantstr(typestr, num, plural_suffix='s'):
    return ((six.text_type(num) + ' ') + pluralize(typestr, num, plural_suffix))

r"""
Heuristically generates an english phrase relating to the quantity of
something.  This is useful for writing user messages.

Args:
typestr (str): singular form of the word
num (int): quanity of the type
plural_suffix (str): heurstic plural form (default = 's')

Returns:
str: quantity phrase

CommandLine:
python -m utool.util_str quantity_str

Example:
>>> # DISABLE_DOCTEST
>>> from utool.util_str import *  # NOQA
>>> items = [1, 2, 3]
>>> result = 'The list contains ' + (quantstr('item', len(items)))
>>> items = [1]
>>> result += '\nThe list contains ' + (quantstr('item', len(items)))
>>> items = []
>>> result += '\nThe list contains ' + (quantstr('item', len(items)))
>>> print(result)
The list contains 3 items
The list contains 1 item
The list contains 0 items


def typewrite(message, interval=0.0, pause=None, _pause=True):
    interval = float(interval)
    _failSafeCheck()
    for c in message:
        if (len(c) > 1):
            c = c.lower()
        press(c, _pause=False)
        time.sleep(interval)
        _failSafeCheck()
    _autoPause(pause, _pause)

Performs a keyboard key press down, followed by a release, for each of
the characters in message.

The message argument can also be list of strings, in which case any valid
keyboard name can be used.

Since this performs a sequence of keyboard presses and does not hold down
keys, it cannot be used to perform keyboard shortcuts. Use the hotkey()
function for that.

Args:
message (str, list): If a string, then the characters to be pressed. If a
list, then the key names of the keys to press in order. The valid names
are listed in KEYBOARD_KEYS.
interval (float, optional): The number of seconds in between each press.
0.0 by default, for no pause in between presses.

Returns:
None


def update_default(self, new_default, respect_none=False):
    if (new_default is not None):
        self.default = new_default
    elif ((new_default is None) and respect_none):
        self.default = None

Update our current default with the new_default.

Args:
new_default: New default to set.
respect_none: Flag to determine if ``None`` is a valid value.


def create_antithetic_variates(samples, axes=()):
    samples = numpy.asfarray(samples)
    assert (numpy.all((samples <= 1)) and numpy.all((samples >= 0))), 'all samples assumed on interval [0, 1].'
    if (len(samples.shape) == 1):
        samples = samples.reshape(1, (- 1))
    inverse_samples = (1 - samples)
    dims = len(samples)
    if (not len(axes)):
        axes = (True,)
    axes = numpy.asarray(axes, dtype=bool).flatten()
    indices = {tuple((axes * idx)) for idx in numpy.ndindex(((2,) * dims))}
    indices = sorted(indices, reverse=True)
    indices = sorted(indices, key=(lambda idx: sum(idx)))
    out = [numpy.where(idx, inverse_samples.T, samples.T).T for idx in indices]
    out = numpy.dstack(out).reshape(dims, (- 1))
    return out

Generate antithetic variables.

Args:
samples (numpy.ndarray):
The samples, assumed to be on the [0, 1]^D hyper-cube, to be
reflected.
axes (tuple):
Boolean array of which axes to reflect. If This to limit the number
of points created in higher dimensions by reflecting all axes at
once.

Returns (numpy.ndarray):
Same as ``samples``, but with samples internally reflected. roughly
equivalent to ``numpy.vstack([samples, 1-samples])`` in one dimensions.


def sync(self, resync=False):
    if resync:
        self._clear()
    while True:
        logger.debug('Starting reminder sync: %s', self._reminder_version)
        changes = self._reminders_api.list()
        if ('task' in changes):
            self._parseTasks(changes['task'])
        self._reminder_version = changes['storageVersion']
        logger.debug('Finishing sync: %s', self._reminder_version)
        history = self._reminders_api.history(self._reminder_version)
        if (self._reminder_version == history['highestStorageVersion']):
            break
    while True:
        logger.debug('Starting keep sync: %s', self._keep_version)
        labels_updated = any((i.dirty for i in self._labels.values()))
        changes = self._keep_api.changes(target_version=self._keep_version, nodes=[i.save() for i in self._findDirtyNodes()], labels=([i.save() for i in self._labels.values()] if labels_updated else None))
        if changes.get('forceFullResync'):
            raise exception.ResyncRequiredException('Full resync required')
        if changes.get('upgradeRecommended'):
            raise exception.UpgradeRecommendedException('Upgrade recommended')
        if ('userInfo' in changes):
            self._parseUserInfo(changes['userInfo'])
        if ('nodes' in changes):
            self._parseNodes(changes['nodes'])
        self._keep_version = changes['toVersion']
        logger.debug('Finishing sync: %s', self._keep_version)
        if (not changes['truncated']):
            break
    if _node.DEBUG:
        self._clean()

Sync the local Keep tree with the server. If resyncing, local changes will be detroyed. Otherwise, local changes to notes, labels and reminders will be detected and synced up.

Args:
resync (bool): Whether to resync data.

Raises:
SyncException: If there is a consistency issue.


def load_json(task: Task, file: str) -> Result:

    kwargs: Dict[str, Type[MutableMapping[str, Any]]] = {}
    with open(file, "r") as f:
        data = json.loads(f.read(), **kwargs)

    return Result(host=task.host, result=data)

Loads a json file.

Arguments:
file: path to the file containing the json file to load

Examples:

Simple example with ``ordered_dict``::

> nr.run(task=load_json,
file="mydata.json")

file: path to the file containing the json file to load

Returns:
Result object with the following attributes set:
* result (``dict``): dictionary with the contents of the file


def _compare(info1, info2):
    if (info1.size != info2.size):
        return True
    date1 = info1.modified
    date2 = info2.modified
    return ((date1 is None) or (date2 is None) or (date1 > date2))

Compare two `Info` objects to see if they should be copied.

Returns:
bool: `True` if the `Info` are different in size or mtime.


def example_protos_from_path(path, num_examples=10, start_index=0, parse_examples=True, sampling_odds=1, example_class=tf.train.Example):

    def append_examples_from_iterable(iterable, examples):
        for value in iterable:
            if ((sampling_odds >= 1) or (random.random() < sampling_odds)):
                examples.append((example_class.FromString(value) if parse_examples else value))
                if (len(examples) >= num_examples):
                    return
    examples = []
    if path.endswith('.csv'):

        def are_floats(values):
            for value in values:
                try:
                    float(value)
                except ValueError:
                    return False
            return True
        csv.register_dialect('CsvDialect', skipinitialspace=True)
        rows = csv.DictReader(open(path), dialect='CsvDialect')
        for row in rows:
            if ((sampling_odds < 1) and (random.random() > sampling_odds)):
                continue
            example = tf.train.Example()
            for col in row.keys():
                values = [val.strip() for val in row[col].split('|')]
                if are_floats(values):
                    example.features.feature[col].float_list.value.extend([float(val) for val in values])
                else:
                    example.features.feature[col].bytes_list.value.extend([val.encode('utf-8') for val in values])
            examples.append((example if parse_examples else example.SerializeToString()))
            if (len(examples) >= num_examples):
                break
        return examples
    filenames = filepath_to_filepath_list(path)
    compression_types = ['', 'GZIP', 'ZLIB']
    current_compression_idx = 0
    current_file_index = 0
    while ((current_file_index < len(filenames)) and (current_compression_idx < len(compression_types))):
        try:
            record_iterator = tf.compat.v1.python_io.tf_record_iterator(path=filenames[current_file_index], options=tf.io.TFRecordOptions(compression_types[current_compression_idx]))
            append_examples_from_iterable(record_iterator, examples)
            current_file_index += 1
            if (len(examples) >= num_examples):
                break
        except tf.errors.DataLossError:
            current_compression_idx += 1
        except (IOError, tf.errors.NotFoundError) as e:
            raise common_utils.InvalidUserInputError(e)
    if examples:
        return examples
    else:
        raise common_utils.InvalidUserInputError((('No examples found at ' + path) + '. Valid formats are TFRecord files.'))

Returns a number of examples from the provided path.

Args:
path: A string path to the examples.
num_examples: The maximum number of examples to return from the path.
parse_examples: If true then parses the serialized proto from the path into
proto objects. Defaults to True.
sampling_odds: Odds of loading an example, used for sampling. When >= 1
(the default), then all examples are loaded.
example_class: tf.train.Example or tf.train.SequenceExample class to load.
Defaults to tf.train.Example.

Returns:
A list of Example protos or serialized proto strings at the path.

Raises:
InvalidUserInputError: If examples cannot be procured from the path.


def sample(self, fields=None, count=5, sampling=None, use_cache=True, dialect=None, billing_tier=None):
    return self._table.sample(fields=fields, count=count, sampling=sampling, use_cache=use_cache, dialect=dialect, billing_tier=billing_tier)

Retrieves a sampling of data from the view.

Args:
fields: an optional list of field names to retrieve.
count: an optional count of rows to retrieve which is used if a specific
sampling is not specified.
sampling: an optional sampling strategy to apply to the view.
use_cache: whether to use cached results or not.
dialect : {'legacy', 'standard'}, default 'legacy'
'legacy' : Use BigQuery's legacy SQL dialect.
'standard' : Use BigQuery's standard SQL (beta), which is
compliant with the SQL 2011 standard.
billing_tier: Limits the billing tier for this job. Queries that have resource
usage beyond this tier will fail (without incurring a charge). If unspecified, this
will be set to your project default. This can also be used to override your
project-wide default billing tier on a per-query basis.
Returns:
A QueryResultsTable object containing the resulting data.
Raises:
Exception if the sample query could not be executed or the query response was malformed.


def add_property(self, set_property, name, starting_value, tag_name=None):

        def del_property(self, tag_name):
            try:
                del self._content[tag_name]
            except KeyError:
                pass

        def get_property(self, tag_name):
            try:
                return self._content[tag_name]
            except KeyError:
                return None

        tag_name = (name if tag_name is None else tag_name)
        fget = lambda self: get_property(self, tag_name)
        fdel = lambda self: del_property(self, tag_name)
        fset = lambda self, value: set_property(value)
        setattr(self.__class__, name, property(fget, fset, fdel))
        set_property(starting_value)

Set properies of atributes stored in content using stored common fdel and fget and given fset.

Args:
set_property -- Function that sets given property.
name -- Name of the atribute this property must simulate. Used as key in content dict by default.
starting_value -- Starting value of given property.

Keyword args:
tag_name -- The tag name stored in conted dict as a key if different to name.


def get_maintainer(self):
    return hdx.data.user.User.read_from_hdx(self.data['maintainer'], configuration=self.configuration)

Get the dataset's maintainer.

Returns:
User: Dataset's maintainer


def check_empty_response(self, orig_request, method_config, start_response):
    response_config = method_config.get('response', {}).get('body')
    if (response_config == 'empty'):
        cors_handler = self._create_cors_handler(orig_request)
        return util.send_wsgi_no_content_response(start_response, cors_handler)

If the response from the backend is empty, return a HTTP 204 No Content.

Args:
orig_request: An ApiRequest, the original request from the user.
method_config: A dict, the API config of the method to be called.
start_response: A function with semantics defined in PEP-333.

Returns:
If the backend response was empty, this returns a string containing the
response body that should be returned to the user.  If the backend
response wasn't empty, this returns None, indicating that we should not
exit early with a 204.


def __init__(self, t: typing.Union[typing.Type[typing.Any], class_schema.ForwardRef, str], default: typing.Any=MISSING_VALUE, transform: typing.Optional[typing.Callable[[typing.Any], typing.Any]]=None, is_noneable: bool=False, frozen: bool=False):
    if t is None:
        raise TypeError('"cls" for Object spec cannot be None.')
    forward_ref = None
    type_args = []
    if isinstance(t, class_schema.ForwardRef):
        forward_ref = t
    elif isinstance(t, str):
        forward_ref = class_schema.ForwardRef(_get_spec_callsite_module(), t)
    elif isinstance(t, type):
        if t is object:
            raise TypeError("<class 'object'> is too general for Object spec.")
    elif not pg_inspect.is_generic(t):
        raise TypeError('"cls" for Object spec should be a type or str.')
    self._forward_ref = forward_ref
    self._type_args = type_args
    super().__init__(t, default, transform, is_noneable=is_noneable, frozen=frozen)

Constructor.

Args:
t: Class of the object. Objects of subclass of this class is acceptable.
default: (Optional) default value of this spec.
transform: (Optional) user-defined function to be called on the input
of `apply`. It could be used as a type converter or a custom
validator which may raise errors.
is_noneable: If True, None is acceptable.
frozen: If True, values other than the default value is not accceptable.


def from_function_and_signature(cls, python_function, input_signature, is_pure=False, jit_compile=None):
    function_type, default_values = make_function_type(python_function, input_signature)
    while isinstance(python_function, functools.partial):
        python_function = python_function.func
    name = getattr(python_function, '__name__', 'f')
    return FunctionSpec(function_type, default_values, is_pure=is_pure, jit_compile=jit_compile, name=name)

Creates a FunctionSpec instance given a python function and signature.

Args:
python_function: a function to inspect
input_signature: a signature of the function (None, if variable)
is_pure: if True all input arguments (including variables and constants)
will be converted to tensors and no variable changes allowed.
jit_compile: see `tf.function`

Returns:
instance of FunctionSpec


def create(self, request, desc, files, public=False):
    request.data = json.dumps({'description': desc, 'public': public, 'files': files})
    return self.send(request).json()['html_url']

Creates a gist

Arguments:
request: an initial request object
desc:    the gist description
files:   a list of files to add to the gist
public:  a flag to indicate whether the gist is public or not

Returns:
The URL to the newly created gist.


def serialize(optimizer):
    return serialize_keras_object(optimizer)

Serialize the optimizer configuration to JSON compatible python dict.

The configuration can be used for persistence and reconstruct the `Optimizer`
instance again.

>>> tf.keras.optimizers.serialize(tf.keras.optimizers.SGD())
{'class_name': 'SGD', 'config': {'name': 'SGD', 'learning_rate': 0.01,
'decay': 0.0, 'momentum': 0.0,
'nesterov': False}}

Args:
optimizer: An `Optimizer` instance to serialize.

Returns:
Python dict which contains the configuration of the input optimizer.


def Pull(self, device_filename, dest_file=None, timeout_ms=None, progress_callback=None):
    if (not dest_file):
        dest_file = io.BytesIO()
    elif isinstance(dest_file, str):
        dest_file = open(dest_file, 'wb')
    elif isinstance(dest_file, file):
        pass
    else:
        raise ValueError('destfile is of unknown type')
    conn = self.protocol_handler.Open(self._handle, destination=b'sync:', timeout_ms=timeout_ms)
    self.filesync_handler.Pull(conn, device_filename, dest_file, progress_callback)
    conn.Close()
    if isinstance(dest_file, io.BytesIO):
        return dest_file.getvalue()
    else:
        dest_file.close()
        if hasattr(dest_file, 'name'):
            return os.path.exists(dest_file.name)
        return True

Pull a file from the device.

Args:
device_filename: Filename on the device to pull.
dest_file: If set, a filename or writable file-like object.
timeout_ms: Expected timeout for any part of the pull.
progress_callback: callback method that accepts filename, bytes_written and total_bytes,
total_bytes will be -1 for file-like objects

Returns:
The file data if dest_file is not set. Otherwise, True if the destination file exists


def _PrintVSSStoreIdentifiersOverview(self, volume_system, volume_identifiers):
    header = 'The following Volume Shadow Snapshots (VSS) were found:\n'
    self._output_writer.Write(header)
    column_names = ['Identifier', 'Creation Time']
    table_view = views.CLITabularTableView(column_names=column_names)
    for volume_identifier in volume_identifiers:
        volume = volume_system.GetVolumeByIdentifier(volume_identifier)
        if (not volume):
            raise errors.SourceScannerError('Volume missing for identifier: {0:s}.'.format(volume_identifier))
        volume_attribute = volume.GetAttribute('creation_time')
        filetime = dfdatetime_filetime.Filetime(timestamp=volume_attribute.value)
        creation_time = filetime.CopyToDateTimeString()
        if volume.HasExternalData():
            creation_time = '{0:s}\tWARNING: data stored outside volume'.format(creation_time)
        table_view.AddRow([volume.identifier, creation_time])
    self._output_writer.Write('\n')
    table_view.Write(self._output_writer)
    self._output_writer.Write('\n')

Prints an overview of VSS store identifiers.

Args:
volume_system (dfvfs.VShadowVolumeSystem): volume system.
volume_identifiers (list[str]): allowed volume identifiers.

Raises:
SourceScannerError: if a volume cannot be resolved from the volume
identifier.


def _construct_w(self, inputs):

    weight_shape = self._kernel_shape + (1, 1)

    if "w" not in self._initializers:
      self._initializers["w"] = create_weight_initializer(weight_shape[:2],
                                                          dtype=inputs.dtype)

    w = tf.get_variable("w",
                        shape=weight_shape,
                        dtype=inputs.dtype,
                        initializer=self._initializers["w"],
                        partitioner=self._partitioners.get("w", None),
                        regularizer=self._regularizers.get("w", None))
    return w

Construct the convolution weight matrix.

Figures out the shape of the weight matrix, initialize it, and return it.

Args:
inputs: A Tensor of shape `data_format` and of type `tf.float16`,
`tf.bfloat16` or `tf.float32`.

Returns:
w: A weight matrix of the same type as `inputs` and of shape
[kernel_shape, 1, 1].


def map_(input_layer, fn):
    if (not input_layer.is_sequence()):
        raise ValueError('Can only map a sequence.')
    return [fn(x) for x in input_layer]

Maps the given function across this sequence.

To map an entire template across the sequence, use the `as_fn` method on the
template.

Args:
input_layer: The input tensor.
fn: A function of 1 argument that is applied to each item in the sequence.
Returns:
A new sequence Pretty Tensor.
Raises:
ValueError: If the input_layer does not hold a sequence.


def encode(self, input_ids: jnp.ndarray, attention_mask: Optional[jnp.ndarray]=None, position_ids: Optional[jnp.ndarray]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, train: bool=False, params: Optional[dict]=None, dropout_rng: PRNGKey=None):
    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
    return_dict = return_dict if return_dict is not None else self.config.return_dict
    if attention_mask is None:
        attention_mask = jnp.ones_like(input_ids)
    if position_ids is None:
        batch_size, sequence_length = input_ids.shape
        position_ids = jnp.broadcast_to(jnp.arange(sequence_length)[None, :], (batch_size, sequence_length))
    rngs = {}
    if dropout_rng is not None:
        rngs['dropout'] = dropout_rng

    def _encoder_forward(module, input_ids, attention_mask, position_ids, **kwargs):
        encode_module = module._get_encoder_module()
        return encode_module(input_ids, attention_mask, position_ids, **kwargs)
    return self.module.apply({'params': params or self.params}, input_ids=jnp.array(input_ids, dtype='i4'), attention_mask=jnp.array(attention_mask, dtype='i4'), position_ids=jnp.array(position_ids, dtype='i4'), output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, deterministic=not train, rngs=rngs, method=_encoder_forward)

Returns:

Example:

```python
>>> from transformers import AutoTokenizer, FlaxBartForConditionalGeneration

>>> model = FlaxBartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")

>>> text = "My friends are cool but they eat too many carbs."
>>> inputs = tokenizer(text, max_length=1024, return_tensors="jax")
>>> encoder_outputs = model.encode(**inputs)
```


def search_by_user(self, screen_name, count=100):
    results = self._api.user_timeline(screen_name=screen_name, count=count)
    return results

Search tweets by user.

Args:
screen_name: screen name
count: the number of tweets

Returns:
list: tweet list


def get_request_message(cls, remote_info):
    if (remote_info in cls.__remote_info_cache):
        return cls.__remote_info_cache[remote_info]
    else:
        return remote_info.request_type()

Gets request message or container from remote info.

Args:
remote_info: Instance of protorpc.remote._RemoteMethodInfo corresponding
to a method.

Returns:
Either an instance of the request type from the remote or the
ResourceContainer that was cached with the remote method.


def show(self, frame):

        if len(frame.shape) != 3:
            raise ValueError('frame should have shape with only 3 dimensions')

        if not self.is_open:
            self.open()

        self._window.clear()
        self._window.switch_to()
        self._window.dispatch_events()

        image = ImageData(
            frame.shape[1],
            frame.shape[0],
            'RGB',
            frame.tobytes(),
            pitch=frame.shape[1]*-3
        )

        image.blit(0, 0, width=self._window.width, height=self._window.height)
        self._window.flip()

Show an array of pixels on the window.

Args:
frame (numpy.ndarray): the frame to show on the window

Returns:
None


def str_to_v1_str(xml_str):
    if str_is_v1(xml_str):
        return xml_str
    etree_obj = str_to_etree(xml_str)
    strip_v2_elements(etree_obj)
    etree_replace_namespace(etree_obj, d1_common.types.dataoneTypes_v1.Namespace)
    return etree_to_str(etree_obj)

Convert a API v2 XML doc to v1 XML doc.

Removes elements that are only valid for v2 and changes namespace to v1.

If doc is already v1, it is returned unchanged.

Args:
xml_str : str
API v2 XML doc. E.g.: ``SystemMetadata v2``.

Returns:
str : API v1 XML doc. E.g.: ``SystemMetadata v1``.


def buckingham_potential(self, structure, val_dict=None):
    if (not val_dict):
        try:
            el = [site.specie.symbol for site in structure]
            valences = [site.specie.oxi_state for site in structure]
            val_dict = dict(zip(el, valences))
        except AttributeError:
            bv = BVAnalyzer()
            el = [site.specie.symbol for site in structure]
            valences = bv.get_valences(structure)
            val_dict = dict(zip(el, valences))
    bpb = BuckinghamPotential('bush')
    bpl = BuckinghamPotential('lewis')
    gin = ''
    for key in val_dict.keys():
        use_bush = True
        el = re.sub('[1-9,+,\\-]', '', key)
        if (el not in bpb.species_dict.keys()):
            use_bush = False
        elif (val_dict[key] != bpb.species_dict[el]['oxi']):
            use_bush = False
        if use_bush:
            gin += 'species \n'
            gin += bpb.species_dict[el]['inp_str']
            gin += 'buckingham \n'
            gin += bpb.pot_dict[el]
            gin += 'spring \n'
            gin += bpb.spring_dict[el]
            continue
        if (el != 'O'):
            k = (((el + '_') + str(int(val_dict[key]))) + '+')
            if (k not in bpl.species_dict.keys()):
                raise GulpError('Element {} not in library'.format(k))
            gin += 'species\n'
            gin += bpl.species_dict[k]
            gin += 'buckingham\n'
            gin += bpl.pot_dict[k]
        else:
            gin += 'species\n'
            k = 'O_core'
            gin += bpl.species_dict[k]
            k = 'O_shel'
            gin += bpl.species_dict[k]
            gin += 'buckingham\n'
            gin += bpl.pot_dict[key]
            gin += 'spring\n'
            gin += bpl.spring_dict[key]
    return gin

Generate species, buckingham, and spring options for an oxide structure
using the parameters in default libraries.

Ref:
1. G.V. Lewis and C.R.A. Catlow, J. Phys. C: Solid State Phys.,
18, 1149-1161 (1985)
2. T.S.Bush, J.D.Gale, C.R.A.Catlow and P.D. Battle,
J. Mater Chem., 4, 831-837 (1994)

Args:
structure: pymatgen.core.structure.Structure
val_dict (Needed if structure is not charge neutral): {El:valence}
dict, where El is element.


def forward(self, x, prev_bin, prev_bin_embedding=None, interpolate=True):
    if prev_bin_embedding is not None:
        if interpolate:
            prev_bin_embedding = nn.functional.interpolate(prev_bin_embedding, x.shape[-2:], mode='bilinear', align_corners=True)
        x = x + prev_bin_embedding
    x = self.conv1(x)
    x = self.act1(x)
    x = self.conv2(x)
    attractors = self.act2(x)
    attractors = attractors + 0.001
    batch_size, _, height, width = attractors.shape
    attractors = attractors.view(batch_size, self.n_attractors, 2, height, width)
    attractors_normed = attractors[:, :, 0, ...]
    bin_centers = nn.functional.interpolate(prev_bin, (height, width), mode='bilinear', align_corners=True)
    if not self.memory_efficient:
        func = {'mean': torch.mean, 'sum': torch.sum}[self.kind]
        delta_c = func(inv_attractor(attractors_normed.unsqueeze(2) - bin_centers.unsqueeze(1)), dim=1)
    else:
        delta_c = torch.zeros_like(bin_centers, device=bin_centers.device)
        for i in range(self.n_attractors):
            delta_c += inv_attractor(attractors_normed[:, i, ...].unsqueeze(1) - bin_centers)
        if self.kind == 'mean':
            delta_c = delta_c / self.n_attractors
    bin_new_centers = bin_centers + delta_c
    bin_centers = (self.max_depth - self.min_depth) * bin_new_centers + self.min_depth
    bin_centers, _ = torch.sort(bin_centers, dim=1)
    bin_centers = torch.clip(bin_centers, self.min_depth, self.max_depth)
    return (bin_new_centers, bin_centers)

The forward pass of the attractor layer. This layer predicts the new bin centers based on the previous bin centers
and the attractor points (the latter are predicted by the MLP).

Args:
x (`torch.Tensor` of shape `(batch_size, num_channels, height, width)`):
Feature block.
prev_bin (`torch.Tensor` of shape `(batch_size, prev_number_of_bins, height, width)`):
Previous bin centers normed.
prev_bin_embedding (`torch.Tensor`, *optional*):
Optional previous bin embeddings.
interpolate (`bool`, *optional*, defaults to `True`):
Whether to interpolate the previous bin embeddings to the size of the input features.

Returns:
`Tuple[`torch.Tensor`, `torch.Tensor`]:
New bin centers normed and scaled.


def crop(self, extent, copy=False):
    try:
        if (extent[0] is None):
            extent = (self.start.z, extent[1])
        if (extent[1] is None):
            extent = (extent[0], self.stop.z)
    except:
        m = 'You must provide a 2-tuple for the new extents. Use None for'
        m += ' the existing start or stop.'
        raise StriplogError(m)
    first_ix = self.read_at(extent[0], index=True)
    last_ix = self.read_at(extent[1], index=True)
    first = self[first_ix].split_at(extent[0])[1]
    last = self[last_ix].split_at(extent[1])[0]
    new_list = self.__list[first_ix:(last_ix + 1)].copy()
    new_list[0] = first
    new_list[(- 1)] = last
    if copy:
        return Striplog(new_list)
    else:
        self.__list = new_list
        return

Crop to a new depth range.

Args:
extent (tuple): The new start and stop depth. Must be 'inside'
existing striplog.
copy (bool): Whether to operate in place or make a copy.

Returns:
Operates in place by deault; if copy is True, returns a striplog.


def FindEnumTypeByName(self, full_name):

    full_name = _NormalizeFullyQualifiedName(full_name)
    if full_name not in self._enum_descriptors:
      self._FindFileContainingSymbolInDb(full_name)
    return self._enum_descriptors[full_name]

Loads the named enum descriptor from the pool.

Args:
full_name: The full name of the enum descriptor to load.

Returns:
The enum descriptor for the named type.

Raises:
KeyError: if the enum cannot be found in the pool.


def generate_parsers(config, paths):
    output = '
    output += (inspect.getsource(conf_reader._get_source) + '\n\n')
    output += (inspect.getsource(utils._get_encoding) + '\n\n')
    output += (inspect.getsource(utils.handle_encodnig) + '\n\n')
    output += (inspect.getsource(utils.is_equal_tag) + '\n\n')
    output += (inspect.getsource(utils.has_neigh) + '\n\n')
    output += '
    for (name, path) in paths.items():
        path = path[0]
        required = config[0]['vars'][name].get('required', False)
        notfoundmsg = config[0]['vars'][name].get('notfoundmsg', '')
        output += _generate_parser(name, path, required, notfoundmsg)
    output += '
    output += _unittest_template(config)
    output += '
    output += "if __name__ == '__main__':\n"
    output += (IND + 'test_parsers()')
    return output

Generate parser for all `paths`.

Args:
config (dict): Original configuration dictionary used to get matches
for unittests. See
:mod:`~harvester.autoparser.conf_reader` for details.
paths (dict): Output from :func:`.select_best_paths`.

Returns:
str: Python code containing all parsers for `paths`.


def _InstallRpm(self, path):
    pid = os.fork()
    if (pid == 0):
        cmd = '/bin/rpm'
        cmd_args = [cmd, '-U', '--replacepkgs', '--replacefiles', path]
        env = os.environ.copy()
        env.pop('LD_LIBRARY_PATH', None)
        env.pop('PYTHON_PATH', None)
        os.execve(cmd, cmd_args, env)
    else:
        time.sleep(1000)

Client update for rpm based distros.

Upgrading rpms is a bit more tricky than upgrading deb packages since there
is a preinstall script that kills the running GRR daemon and, thus, also
the installer process. We need to make sure we detach the child process
properly and therefore cannot use client_utils_common.Execute().

Args:
path: Path to the .rpm.


def StubOutWithMock(self, obj, attr_name, use_mock_anything=False):
    attr_to_replace = getattr(obj, attr_name)
    if ((type(attr_to_replace) in self._USE_MOCK_OBJECT) and (not use_mock_anything)):
        stub = self.CreateMock(attr_to_replace)
    else:
        stub = self.CreateMockAnything()
    self.stubs.Set(obj, attr_name, stub)

Replace a method, attribute, etc. with a Mock.

This will replace a class or module with a MockObject, and everything else
(method, function, etc) with a MockAnything.  This can be overridden to
always use a MockAnything by setting use_mock_anything to True.

Args:
obj: A Python object (class, module, instance, callable).
attr_name: str.  The name of the attribute to replace with a mock.
use_mock_anything: bool. True if a MockAnything should be used regardless
of the type of attribute.


def expand_dims(a, axis):
    if (hasattr(a, 'expand_dims') and hasattr(type(a), '__array_interface__')):
        return a.expand_dims(axis)
    else:
        return np.expand_dims(a, axis)

Insert a new axis, corresponding to a given position in the array shape

Args:
a (array_like): Input array.
axis (int): Position (amongst axes) where new axis is to be inserted.


def wait_until_clickable(self, selector, **kwargs):
    self.info_log(('Waiting until clickable (%s)' % selector))
    timeout = kwargs.get('timeout', BROME_CONFIG['proxy_driver']['default_timeout'])
    self.debug_log(('effective timeout: %s' % timeout))
    raise_exception = kwargs.get('raise_exception', BROME_CONFIG['proxy_driver']['raise_exception'])
    self.debug_log(('effective raise_exception: %s' % raise_exception))
    _selector = Selector(self, selector)
    try:
        WebDriverWait(self._driver, timeout).until(EC.element_to_be_clickable((getattr(By, _selector.find_by), _selector.get_selector())))
        self.debug_log(('wait_until_clickable (%s): element is clickable' % _selector))
        return True
    except exceptions.TimeoutException:
        msg = ('wait_until_clickable: element (%s) is still not clickable' % _selector)
        self.debug_log(msg)
        self.print_javascript_error()
        if raise_exception:
            if self.bot_diary:
                self.bot_diary.add_auto_entry('I waited for the clickability of', target=_selector.get_human_readable())
            raise exceptions.TimeoutException(msg)
        else:
            return False

Wait until an element is clickable

Args:
selector (str): the selector used to find the element

Kwargs:
timeout (int) second before a timeout exception is raise
raise_exception (bool) raise an exception or return a bool

Returns:
bool True if element is clickable before the timeout,
False otherwise (raise_exception = False)

Raises:
this function might raise an exception depending on the
raise_exception kwargs
or
the config proxy_driver:raise_exception


def mkdir_p(dirname):

    assert dirname is not None
    if dirname == '' or os.path.isdir(dirname):
        return
    try:
        os.makedirs(dirname)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise e

Like "mkdir -p", make a dir recursively, but do nothing if the dir exists

Args:
dirname(str):


def VerifyStructure(self, parser_mediator, lines):
    match = self._PARSING_COMPONENTS['msg_left_delimiter'].match
    return (match in lines)

Verifies whether content corresponds to an SCCM log file.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
lines (str): one or more lines from the text file.

Returns:
bool: True if this is the correct parser, False otherwise.


def scan_file(path):
    path = os.path.abspath(path)
    assert os.path.exists(path), ("Unreachable file '%s'." % path)
    try:
        cd = pyclamd.ClamdUnixSocket()
        cd.ping()
    except pyclamd.ConnectionError:
        cd = pyclamd.ClamdNetworkSocket()
        try:
            cd.ping()
        except pyclamd.ConnectionError:
            raise ValueError("Couldn't connect to clamd server using unix/network socket.")
    cd = pyclamd.ClamdUnixSocket()
    assert cd.ping(), 'clamd server is not reachable!'
    result = cd.scan_file(path)
    return (result if result else {})

Scan `path` for viruses using ``clamd`` antivirus daemon.

Args:
path (str): Relative or absolute path of file/directory you need to
scan.

Returns:
dict: ``{filename: ("FOUND", "virus type")}`` or blank dict.

Raises:
ValueError: When the server is not running.
AssertionError: When the internal file doesn't exists.


def _dict_to_feed(self, parse=True):
    if not self.raw_feed:
        return []
    headers = self.raw_feed[0]
    row = 1
    for item in self.feed:
        for key in iter(item.keys()):
            if key in headers:
                column = headers.index(key)
                value = self._parse_value(item[key]) if parse else item[key]
                while column >= len(self.raw_feed[row]):
                    self.raw_feed[row].append('')
                self.raw_feed[row][column] = value
        row += 1
    return self.raw_feed

Turns a feed into a list of strings to be written back to the feed.

Args:
feed: Dictionary list to convert into a list of lists of strings.

Returns:
List of list of strings representing the values of the feed.


def mesh_split_sharding(device_mesh, tensor_split_dims_mapping, manual_mesh_dims=None):
    manual_mesh_dims = manual_mesh_dims or []
    permutation = [d for d in tensor_split_dims_mapping if d >= 0] + manual_mesh_dims
    if len(permutation) > len(device_mesh.shape):
        raise ValueError('Number of tensor split dimensions (%r) is larger than device mesh rank (%r). tensor_split_dims_mapping: %r, device_mesh.shape: %r' % (len(permutation), len(device_mesh.shape), tensor_split_dims_mapping, device_mesh.shape))
    transpose_permutation = permutation + [d for d in range(len(device_mesh.shape)) if d not in permutation]
    tile_assignment = _np.transpose(device_mesh, transpose_permutation)
    tile_shape = [1 if d < 0 else device_mesh.shape[d] for d in tensor_split_dims_mapping + manual_mesh_dims]
    subgroup_modes = [xla_data_pb2.OpSharding.MANUAL] * len(manual_mesh_dims)
    partial = len(permutation) < len(device_mesh.shape)
    if partial:
        tile_shape.append(_np.prod(device_mesh.shape)
        subgroup_modes.append(xla_data_pb2.OpSharding.REPLICATED)
    tile_assignment = _np.reshape(tile_assignment, tile_shape)
    if manual_mesh_dims:
        return Sharding.subgroup_tile(tile_assignment, subgroup_modes)
    if partial:
        return Sharding.partial_tile(tile_assignment)
    return Sharding.tile(tile_assignment)

Returns a Sharding object representing sharding along multiple dimensions.

Args:
device_mesh: An np.ndarray describing the topology of the device mesh and
each element is the ID of the device in the topology.
tensor_split_dims_mapping: A list of integers that map each tensor axis to
the device mesh axis along which it is sharded. Its length is the tensor
rank, and tensor_split_dims_mapping[i] is device mesh axis for tensor
dimension i. Use -1 for tensor dimensions that are not sharded.
manual_mesh_dims: An optional list of mesh dims for manual subgroups.

Raises:
ValueError: The number of tensor split dimensions is larger than device mesh
rank.


def wait_any(jobs, timeout=None):

    return Job._wait(jobs, timeout, concurrent.futures.FIRST_COMPLETED)

Return when at least one of the specified jobs has completed or timeout expires.

Args:
jobs: a Job or list of Jobs to wait on.
timeout: a timeout in seconds to wait for. None (the default) means no timeout.
Returns:
A list of the jobs that have now completed or None if there were no jobs.


def rectify_acquaintance_strategy(circuit: circuits.Circuit, acquaint_first: bool=True) -> None:
    if (not is_acquaintance_strategy(circuit)):
        raise TypeError('not is_acquaintance_strategy(circuit)')
    rectified_moments = []
    for moment in circuit:
        gate_type_to_ops = collections.defaultdict(list)
        for op in moment.operations:
            gate_type_to_ops[isinstance(op.gate, AcquaintanceOpportunityGate)].append(op)
        if (len(gate_type_to_ops) == 1):
            rectified_moments.append(moment)
            continue
        for acquaint_first in sorted(gate_type_to_ops.keys(), reverse=acquaint_first):
            rectified_moments.append(ops.Moment(gate_type_to_ops[acquaint_first]))
    circuit._moments = rectified_moments

Splits moments so that they contain either only acquaintance gates
or only permutation gates. Orders resulting moments so that the first one
is of the same type as the previous one.

Args:
circuit: The acquaintance strategy to rectify.
acquaint_first: Whether to make acquaintance moment first in when
splitting the first mixed moment.


def cancelMktData(self, contract: Contract):
    ticker = self.ticker(contract)
    reqId = self.wrapper.endTicker(ticker, 'mktData')
    if reqId:
        self.client.cancelMktData(reqId)
    else:
        self._logger.error(f'cancelMktData: No reqId found for contract {contract}')

Unsubscribe from realtime streaming tick data.

Args:
contract: The exact contract object that was used to
subscribe with.


def get_subscription(self, subscription_id):

        return self.client._put(self.url + 'subscriptions/{}'.format(subscription_id), headers=self.get_headers())

Check the basic information associated with the specified subscription.

Args:
subscription_id: Identification of the subscription.

Returns:


def fit_anonymous(self, struct1, struct2, niggli=True):

        struct1, struct2 = self._process_species([struct1, struct2])
        struct1, struct2, fu, s1_supercell = self._preprocess(struct1, struct2,
                                                              niggli)

        matches = self._anonymous_match(struct1, struct2, fu, s1_supercell,
                                        break_on_match=True, single_match=True)

        if matches:
            return True
        else:
            return False

Performs an anonymous fitting, which allows distinct species in one
structure to map to another. E.g., to compare if the Li2O and Na2O
structures are similar.

Args:
struct1 (Structure): 1st structure
struct2 (Structure): 2nd structure

Returns:
True/False: Whether a species mapping can map struct1 to stuct2


def _convert_to_example(filename, image_buffer, label, text, height, width):

  colorspace = 'RGB'
  channels = 3
  image_format = 'JPEG'

  example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(height),
      'image/width': _int64_feature(width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/class/label': _int64_feature(label),
      'image/class/text': _bytes_feature(tf.compat.as_bytes(text)),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/filename': _bytes_feature(tf.compat.as_bytes(os.path.basename(filename))),
      'image/encoded': _bytes_feature(tf.compat.as_bytes(image_buffer))}))
  return example

Build an Example proto for an example.

Args:
filename: string, path to an image file, e.g., '/path/to/example.JPG'
image_buffer: string, JPEG encoding of RGB image
label: integer, identifier for the ground truth for the network
text: string, unique human-readable, e.g. 'dog'
height: integer, image height in pixels
width: integer, image width in pixels
Returns:
Example proto


def _truncate_wildcard_from_date(date_value):
    if (' ' in date_value):
        date_parts = date_value.split(' ')
    elif ('-' in date_value):
        date_parts = date_value.split('-')
    else:
        raise ValueError('Erroneous date value: %s.', date_value)
    if (GenericValue.WILDCARD_TOKEN in date_parts[(- 1)]):
        del date_parts[(- 1)]
    return '-'.join(date_parts)

Truncate wildcard from date parts.

Returns:
(str) The truncated date.

Raises:
ValueError, on either unsupported date separator (currently only ' ' and '-' are supported), or if there's a
wildcard in the year.

Notes:
Either whole date part is wildcard, in which we ignore it and do a range query on the
remaining parts, or some numbers are wildcards, where again, we ignore this part.


def _build_state_value(request_handler, user):

    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token

Composes the value for the 'state' parameter.

Packs the current request URI and an XSRF token into an opaque string that
can be passed to the authentication server via the 'state' parameter.

Args:
request_handler: webapp.RequestHandler, The request.
user: google.appengine.api.users.User, The current user.

Returns:
The state value as a string.


def export_model(model_path):

    estimator = tf.estimator.Estimator(model_fn, model_dir=FLAGS.work_dir,
                                       params=FLAGS.flag_values_dict())
    latest_checkpoint = estimator.latest_checkpoint()
    all_checkpoint_files = tf.gfile.Glob(latest_checkpoint + '*')
    for filename in all_checkpoint_files:
        suffix = filename.partition(latest_checkpoint)[2]
        destination_path = model_path + suffix
        print("Copying {} to {}".format(filename, destination_path))
        tf.gfile.Copy(filename, destination_path)

Take the latest checkpoint and copy it to model_path.

Assumes that all relevant model files are prefixed by the same name.
(For example, foo.index, foo.meta and foo.data-00000-of-00001).

Args:
model_path: The path (can be a gs:// path) to export model


def enrich_app(self, name, value):

        if type(value) == type(self.enrich_app):
            raise ValueError("enrich_app can't add method")

        setattr(self.app, name, value)

Add a new property to the app (with setattr)

Args:
name (str): the name of the new property
value (any): the value of the new property


def pad_tensor(tensor, length, padding_index=DEFAULT_PADDING_INDEX):
    n_padding = (length - tensor.shape[0])
    assert (n_padding >= 0)
    if (n_padding == 0):
        return tensor
    padding = tensor.new(n_padding, *tensor.shape[1:]).fill_(padding_index)
    return torch.cat((tensor, padding), dim=0)

Pad a ``tensor`` to ``length`` with ``padding_index``.

Args:
tensor (torch.Tensor [n, ...]): Tensor to pad.
length (int): Pad the ``tensor`` up to ``length``.
padding_index (int, optional): Index to pad tensor with.

Returns
(torch.Tensor [length, ...]) Padded Tensor.


def incr(self, key, value, noreply=False):

        key = self.check_key(key)
        cmd = b'incr ' + key + b' ' + six.text_type(value).encode('ascii')
        if noreply:
            cmd += b' noreply'
        cmd += b'\r\n'
        results = self._misc_cmd([cmd], b'incr', noreply)
        if noreply:
            return None
        if results[0] == b'NOT_FOUND':
            return None
        return int(results[0])

The memcached "incr" command.

Args:
key: str, see class docs for details.
value: int, the amount by which to increment the value.
noreply: optional bool, False to wait for the reply (the default).

Returns:
If noreply is True, always returns None. Otherwise returns the new
value of the key, or None if the key wasn't found.


Merge several accumulators to a single accumulator.

This method takes the partial values in several accumulators and combines
them into a single accumulator. This computation must not be order-specific
(that is, merge([a, b]) must return the same result as merge([b, a]).

Args:
accumulators: the accumulators to merge, as a list.

Returns:
A merged accumulator.


def l1_l2_regularizer(weight_l1=1.0, weight_l2=1.0, scope=None):

  def regularizer(tensor):
    with tf.name_scope(scope, 'L1L2Regularizer', [tensor]):
      weight_l1_t = tf.convert_to_tensor(weight_l1,
                                         dtype=tensor.dtype.base_dtype,
                                         name='weight_l1')
      weight_l2_t = tf.convert_to_tensor(weight_l2,
                                         dtype=tensor.dtype.base_dtype,
                                         name='weight_l2')
      reg_l1 = tf.multiply(weight_l1_t, tf.reduce_sum(tf.abs(tensor)),
                      name='value_l1')
      reg_l2 = tf.multiply(weight_l2_t, tf.nn.l2_loss(tensor),
                      name='value_l2')
      return tf.add(reg_l1, reg_l2, name='value')
  return regularizer

Define a L1L2 regularizer.

Args:
weight_l1: scale the L1 loss by this factor.
weight_l2: scale the L2 loss by this factor.
scope: Optional scope for name_scope.

Returns:
a regularizer function.


def fbresnet152(num_classes=1000, pretrained='imagenet'):
    model = FBResNet(Bottleneck, [3, 8, 36, 3], num_classes=num_classes)
    if (pretrained is not None):
        settings = pretrained_settings['fbresnet152'][pretrained]
        assert (num_classes == settings['num_classes']), 'num_classes should be {}, but is {}'.format(settings['num_classes'], num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        model.mean = settings['mean']
        model.std = settings['std']
    return model

Constructs a ResNet-152 model.

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


def reset(self, indices=None):

    if indices is None:
      indices = tf.range(len(self._batch_env))
    observ_dtype = self._parse_dtype(self._batch_env.observation_space)
    observ = tf.py_func(
        self._batch_env.reset, [indices], observ_dtype, name='reset')
    observ = tf.check_numerics(observ, 'observ')
    reward = tf.zeros_like(indices, tf.float32)
    done = tf.zeros_like(indices, tf.bool)
    with tf.control_dependencies([
        tf.scatter_update(self._observ, indices, observ),
        tf.scatter_update(self._reward, indices, reward),
        tf.scatter_update(self._done, indices, done)]):
      return tf.identity(observ)

Reset the batch of environments.

Args:
indices: The batch indices of the environments to reset; defaults to all.

Returns:
Batch tensor of the new observations.


def create_model(self):
    properties = {'name': self.name, 'default_flux_limit': 1000}
    objective = self.get_active_objective()
    if (objective is not None):
        reactions = dict(objective.reactions)
        if (len(reactions) == 1):
            (reaction, value) = next(iteritems(reactions))
            if (((value < 0) and (objective.type == 'minimize')) or ((value > 0) and (objective.type == 'maximize'))):
                properties['biomass'] = reaction
    model = NativeModel(properties)
    for compartment in self.compartments:
        model.compartments.add_entry(compartment)
    for compound in self.species:
        model.compounds.add_entry(compound)
    for reaction in self.reactions:
        model.reactions.add_entry(reaction)
    for reaction in model.reactions:
        model.model[reaction.id] = None
    for reaction in model.reactions:
        props = reaction.properties
        if (('lower_flux' in props) or ('upper_flux' in props)):
            lower = props.get('lower_flux')
            upper = props.get('upper_flux')
            model.limits[reaction.id] = (reaction.id, lower, upper)
    limits_lower = {}
    limits_upper = {}
    for bounds in self.flux_bounds:
        reaction = bounds.reaction
        if (reaction in model.limits):
            continue
        if (bounds.operation == SBMLFluxBoundEntry.LESS_EQUAL):
            if (reaction not in limits_upper):
                limits_upper[reaction] = bounds.value
            else:
                raise ParseError('Conflicting bounds for {}'.format(reaction))
        elif (bounds.operation == SBMLFluxBoundEntry.GREATER_EQUAL):
            if (reaction not in limits_lower):
                limits_lower[reaction] = bounds.value
            else:
                raise ParseError('Conflicting bounds for {}'.format(reaction))
        elif (bounds.operation == SBMLFluxBoundEntry.EQUAL):
            if ((reaction not in limits_lower) and (reaction not in limits_upper)):
                limits_lower[reaction] = bounds.value
                limits_upper[reaction] = bounds.value
            else:
                raise ParseError('Conflicting bounds for {}'.format(reaction))
    for reaction in model.reactions:
        if ((reaction.id in limits_lower) or (reaction.id in limits_upper)):
            lower = limits_lower.get(reaction.id, None)
            upper = limits_upper.get(reaction.id, None)
            model.limits[reaction.id] = (reaction.id, lower, upper)
    return model

Create model from reader.

Returns:
:class:`psamm.datasource.native.NativeModel`.


def __init__(self, label_name, value_type, kind, update_label_func):

        self.label_name = label_name
        self.kind = kind
        self.update_label_func = update_label_func
        self.value_type = value_type

Constructor.

update_label_func is used when updating a label in an `Operation` from a
`ReportRequestInfo`.

Args:
label_name (str): the name of the label descriptor
value_type (:class:`ValueType`): the `value type` of the described metric
kind (:class:`Kind`): the ``kind`` of the described metric
update_op_func (function): the func to update an operation


def determine_alert(self, action_schedule, issue_creation_time, last_alert):
    issue_age = (time.time() - issue_creation_time)
    alert_schedule_lookup = {pytimeparse.parse(action_time): action_time for action_time in action_schedule}
    alert_schedule = sorted(alert_schedule_lookup.keys())
    last_alert_time = pytimeparse.parse(last_alert)
    for alert_time in alert_schedule:
        if ((last_alert_time < alert_time <= issue_age) and (last_alert_time != alert_time)):
            return alert_schedule_lookup[alert_time]
    else:
        return None

Determine if we need to trigger an alert

Args:
action_schedule (`list`): A list contains the alert schedule
issue_creation_time (`int`): Time we create the issue
last_alert (`str`): Time we sent the last alert

Returns:
(`None` or `str`)
None if no alert should be sent. Otherwise return the alert we should send


def encode(self, inputs, attention_bias):
    with tf.name_scope('encode'):
        embedded_inputs = self.embedding_softmax_layer(inputs)
        inputs_padding = model_utils.get_padding(inputs)
        with tf.name_scope('add_pos_encoding'):
            length = tf.shape(embedded_inputs)[1]
            pos_encoding = model_utils.get_position_encoding(length, self.params.hidden_size)
            encoder_inputs = (embedded_inputs + pos_encoding)
        if self.train:
            mlperf_log.transformer_print(key=mlperf_log.MODEL_HP_LAYER_POSTPROCESS_DROPOUT, value=self.params.layer_postprocess_dropout)
            encoder_inputs = tf.nn.dropout(encoder_inputs, (1 - self.params.layer_postprocess_dropout))
        return self.encoder_stack(encoder_inputs, attention_bias, inputs_padding)

Generate continuous representation for inputs.

Args:
inputs: int tensor with shape [batch_size, input_length].
attention_bias: float tensor with shape [batch_size, 1, 1, input_length]

Returns:
float tensor with shape [batch_size, input_length, hidden_size]


def scale(self, new_volume: float) -> "Lattice":

        versors = self.matrix / self.abc

        geo_factor = abs(dot(np.cross(versors[0], versors[1]), versors[2]))

        ratios = np.array(self.abc) / self.c

        new_c = (new_volume / (geo_factor * np.prod(ratios))) ** (1 / 3.0)

        return Lattice(versors * (new_c * ratios))

Return a new Lattice with volume new_volume by performing a
scaling of the lattice vectors so that length proportions and angles
are preserved.

Args:
new_volume:
New volume to scale to.

Returns:
New lattice with desired volume.


def _and_join(self, terms):

        if len(terms) > 1:
            return ' AND '.join([self._or_join(t) for t in terms])
        else:
            return self._or_join(terms[0])

Joins terms using AND operator.

Args:
terms (list): terms to join

Examples:
self._and_join(['term1']) -> 'term1'
self._and_join(['term1', 'term2']) -> 'term1 AND term2'
self._and_join(['term1', 'term2', 'term3']) -> 'term1 AND term2 AND term3'

Returns:
str


def get_dense_tensor(self, transformation_cache, state_manager):
    if isinstance(self.categorical_column, SequenceCategoricalColumn):
        raise ValueError('In embedding_column: {}. categorical_column must not be of type SequenceCategoricalColumn. Suggested fix A: If you wish to use DenseFeatures, use a non-sequence categorical_column_with_*. Suggested fix B: If you wish to create sequence input, use SequenceFeatures instead of DenseFeatures. Given (type {}): {}'.format(self.name, type(self.categorical_column), self.categorical_column))
    sparse_tensors = self.categorical_column.get_sparse_tensors(transformation_cache, state_manager)
    return self._get_dense_tensor_internal(sparse_tensors, state_manager)

Returns tensor after doing the embedding lookup.

Args:
transformation_cache: A `FeatureTransformationCache` object to access
features.
state_manager: A `StateManager` to create / access resources such as
lookup tables.

Returns:
Embedding lookup tensor.

Raises:
ValueError: `categorical_column` is SequenceCategoricalColumn.


def ParseText(self, text, eof=True):

    lines = []
    if text:
      lines = text.splitlines()

    for line in lines:
      self._CheckLine(line)
      if self._cur_state_name in ('End', 'EOF'):
        break

    if self._cur_state_name != 'End' and 'EOF' not in self.states and eof:

      self._AppendRecord()

    return self._result

Passes CLI output through FSM and returns list of tuples.

First tuple is the header, every subsequent tuple is a row.

Args:
text: (str), Text to parse with embedded newlines.
eof: (boolean), Set to False if we are parsing only part of the file.
Suppresses triggering EOF state.

Raises:
TextFSMError: An error occurred within the FSM.

Returns:
List of Lists.


def relpath_to_modname(relpath):
    p = pathlib.Path(relpath)
    if (p.name == '__init__.py'):
        p = p.parent
    elif (p.suffix == '.py'):
        p = p.with_suffix('')
    else:
        msg = 'Cannot convert a non-python file to a modname'
        msg_detail = 'The relpath given is: {}'.format(relpath)
        logger.error(((msg + '\n') + msg_detail))
        raise ValueError(msg)
    return '.'.join(p.parts)

Convert relative path to module name

Within a project, a path to the source file is uniquely identified with a
module name. Relative paths of the form 'foo/bar' are *not* converted to
module names 'foo.bar', because (1) they identify directories, not regular
files, and (2) already 'foo/bar/__init__.py' would claim that conversion.

Args:
relpath (str): Relative path from some location on sys.path

Example:
>>> relpath_to_modname('ballet/util/_util.py')
'ballet.util._util'


def get_inventory(self, keys=None):

        inventory = defaultdict(list)
        keys = keys or ['vm-type', 'groups', 'vm-provider']
        vms = self.prefix.get_vms().values()

        for vm in vms:
            entry = self._generate_entry(vm)
            vm_spec = vm.spec
            for key in keys:
                value = self.get_key(key, vm_spec)
                if value is None:
                    continue
                if isinstance(value, list):
                    for sub_value in value:
                        inventory['{}={}'.format(key, sub_value)].append(entry)
                else:
                    inventory['{}={}'.format(key, value)].append(entry)

            for group in vm_spec.get('groups', []):
                inventory[group].append(entry)

        return inventory

Create an Ansible inventory based on python dicts and lists.
The returned value is a dict in which every key represents a group
and every value is a list of entries for that group.

Args:
keys (list of str): Path to the keys that will be used to
create groups.

Returns:
dict: dict based Ansible inventory


def _EscapeGlobCharacters(path):
    (drive, path) = os.path.splitdrive(path)
    return ('%s%s' % (drive, _ESCAPE_GLOB_CHARACTERS_REGEX.sub('[\\1]', path)))

Escapes the glob characters in a path.

Python 3 has a glob.escape method, but python 2 lacks it, so we manually
implement this method.

Args:
path: The absolute path to escape.

Returns:
The escaped path string.


def read_tables(fstream):

    table = read_table(fstream)
    while table is not None:
        yield table
        table = read_table(fstream)

Read all tables from likwid's file stream.

Args:
fstream: Likwid's output file stream.

Returns:
A generator that can be used to iterate over all tables in the fstream.


Iterator for different convolution shapes, strides and explicit paddings.

Returns:
List of tuples (input_size, filter_size, out_size, stride, padding,
dilations), the depthwise convolution parameters.


def GetGroupMap(self, since=None):
    return GroupUpdateGetter().GetUpdates(self, self.conf['group_url'], 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 quaternion_from_euler(angles, order='yzy'):
    angles = np.asarray(angles, dtype=float)
    quat = (quaternion_from_axis_rotation(angles[0], order[0]) * (quaternion_from_axis_rotation(angles[1], order[1]) * quaternion_from_axis_rotation(angles[2], order[2])))
    quat.normalize(inplace=True)
    return quat

Generate a quaternion from a set of Euler angles.

Args:
angles (array_like): Array of Euler angles.
order (str): Order of Euler rotations.  'yzy' is default.

Returns:
Quaternion: Quaternion representation of Euler rotation.


def __init__(self, duration: int = None, name: str = None):

        if isinstance(duration, int):
            self._duration = duration
        else:
            raise PulseError('Pulse duration should be integer.')

        if name:
            self._name = name
        else:
            self._name = 'p%d' % Command.pulseIndex
            Command.pulseIndex += 1

Create a new command.

Args:
duration (int): Duration of this command.
name (str): Name of this command.
Raises:
PulseError: when duration is not number of points.


def smart_cond(pred, true_fn=None, false_fn=None, name=None):
    if not callable(true_fn):
        raise TypeError(f'Argument `true_fn` must be callable. Received {true_fn}')
    if not callable(false_fn):
        raise TypeError(f'Argument `false_fn` must be callable. Received {false_fn}')
    pred_value = smart_constant_value(pred)
    if pred_value is not None:
        if pred_value:
            return true_fn()
        else:
            return false_fn()
    else:
        return cond.cond(pred, true_fn=true_fn, false_fn=false_fn, name=name)

Return either `true_fn()` if predicate `pred` is true else `false_fn()`.

If `pred` is a bool or has a constant value, we return either `true_fn()`
or `false_fn()`, otherwise we use `tf.cond` to dynamically route to both.

Args:
pred: A scalar determining whether to return the result of `true_fn` or
`false_fn`.
true_fn: The callable to be performed if pred is true.
false_fn: The callable to be performed if pred is false.
name: Optional name prefix when using `tf.cond`.

Returns:
Tensors returned by the call to either `true_fn` or `false_fn`.

Raises:
TypeError: If `true_fn` or `false_fn` is not callable.


def extract_google_doc_id(url):
    pattern = '/(document|spreadsheets)/d/([a-zA-Z0-9-_]+)'
    match = re.search(pattern, url)
    if match:
        return match.group(2)
    else:
        return None

Extracts the unique ID of a Google Doc or Google Sheet from a given URL.

Args:
url: The URL of the Google Doc or Google Sheet.

Returns:
The unique ID of the Google Doc or Google Sheet, or None if the ID could not be extracted.


def set_voltage(self, volt, ramp=False):

        if ramp:
            self.mon.RampVoltage(self.mon.start_voltage, volt)
        else:
            self.mon.SetVoltage(volt)

Sets the output voltage of monsoon.

Args:
volt: Voltage to set the output to.
ramp: If true, the output voltage will be increased gradually to
prevent tripping Monsoon overvoltage.


def get_end(pos, alt, category, snvend=None, svend=None, svlen=None):

    end = pos

    if category in ('snv', 'indel', 'cancer'):
        end = snvend

    elif category == 'sv':

        end = svend

        if svend == pos:
            if svlen:
                end = pos + svlen

        if ':' in alt:
            match = BND_ALT_PATTERN.match(alt)
            if match:
                end = int(match.group(2))

    return end

Return the end coordinate for a variant

Args:
pos(int)
alt(str)
category(str)
snvend(str)
svend(int)
svlen(int)

Returns:
end(int)


def __auth_descriptor(self, api_info):
    if (api_info.auth is None):
        return None
    auth_descriptor = {}
    if (api_info.auth.allow_cookie_auth is not None):
        auth_descriptor['allowCookieAuth'] = api_info.auth.allow_cookie_auth
    if api_info.auth.blocked_regions:
        auth_descriptor['blockedRegions'] = api_info.auth.blocked_regions
    return auth_descriptor

Builds an auth descriptor from API info.

Args:
api_info: An _ApiInfo object.

Returns:
A dictionary with 'allowCookieAuth' and/or 'blockedRegions' keys.


def SetTimelineName(self, timeline_name):
    self._timeline_name = timeline_name
    logger.info('Timeline name: {0:s}'.format(self._timeline_name))

Sets the timeline name.

Args:
timeline_name (str): timeline name.


def _unmerge_beam_dim(tensor, batch_size, beam_size):

  shape = common_layers.shape_list(tensor)
  new_shape = [batch_size] + [beam_size] + shape[1:]
  return tf.reshape(tensor, new_shape)

Reshapes first dimension back to [batch_size, beam_size].

Args:
tensor: Tensor to reshape of shape [batch_size*beam_size, ...]
batch_size: Tensor, original batch size.
beam_size: int, original beam size.

Returns:
Reshaped tensor of shape [batch_size, beam_size, ...]


def login_with_password(self, username, password, limit=10):

        warn("login_with_password is deprecated. Use login with sync=True.",
             DeprecationWarning)
        return self.login(username, password, limit, sync=True)

Deprecated. Use ``login`` with ``sync=True``.

Login to the homeserver.

Args:
username (str): Account username
password (str): Account password
limit (int): Deprecated. How many messages to return when syncing.
This will be replaced by a filter API in a later release.

Returns:
str: Access token

Raises:
MatrixRequestError


def flip(script, force_flip=False, selected=False):
    filter_xml = ''.join(['  <filter name="Invert Faces Orientation">\n', '    <Param name="forceFlip" ', 'value="{}" '.format(str(force_flip).lower()), 'description="Force Flip" ', 'type="RichBool" ', '/>\n', '    <Param name="onlySelected" ', 'value="{}" '.format(str(selected).lower()), 'description="Flip only selected faces" ', 'type="RichBool" ', '/>\n', '  </filter>\n'])
    util.write_filter(script, filter_xml)
    return None

Invert faces orientation, flipping the normals of the mesh.

If requested, it tries to guess the right orientation; mainly it decides to
flip all the faces if the minimum/maximum vertexes have not outward point
normals for a few directions. Works well for single component watertight
objects.

Args:
script: the FilterScript object or script filename to write
the filter to.
force_flip (bool): If selected, the normals will always be flipped;
otherwise, the filter tries to set them outside.
selected (bool): If selected, only selected faces will be affected.

Layer stack:
No impacts

MeshLab versions:
2016.12
1.3.4BETA


def name_to_vector(name):

    if not isinstance(name, unicode):
        name = name.decode("utf-8")

    name = name.lower()
    name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore')
    name = "".join(filter(lambda x: x.isalpha() or x == " ", list(name)))

    return sorted(name.split(), key=lambda x: len(x), reverse=True)

Convert `name` to the ASCII vector.

Example:
>>> name_to_vector("ing. Franta Putšálek")
['putsalek', 'franta', 'ing']

Args:
name (str): Name which will be vectorized.

Returns:
list: Vector created from name.


def __fixed_interpolation(self, line):
    STARTPOINT = [(line.xy[0][0] - self._minx), (line.xy[1][0] - self._miny)]
    ENDPOINT = [(line.xy[0][(- 1)] - self._minx), (line.xy[1][(- 1)] - self._miny)]
    count = self._interpolation_dist
    newline = [STARTPOINT]
    while (count < line.length):
        point = line.interpolate(count)
        newline.append([(point.x - self._minx), (point.y - self._miny)])
        count += self._interpolation_dist
    newline.append(ENDPOINT)
    return newline

Place additional points on the border at the specified distance.

By default the distance is 0.5 (meters) which means that the first
point will be placed 0.5 m from the starting point, the second
point will be placed at the distance of 1.0 m from the first
point, etc. The loop breaks when the summarized distance exceeds
the length of the line.

Args:
line (shapely.geometry.LineString): object

Returns:
list: a list of points where each point is represented by
a list of its reduced coordinates

Example:
[[X1, Y1], [X2, Y2], ..., [Xn, Yn]


def load_exons(adapter, exon_lines, build='37', ensembl_genes=None):
    ensembl_genes = (ensembl_genes or adapter.ensembl_genes(build))
    hgnc_id_transcripts = adapter.id_transcripts_by_gene(build=build)
    if isinstance(exon_lines, DataFrame):
        exons = parse_ensembl_exon_request(exon_lines)
        nr_exons = exon_lines.shape[0]
    else:
        exons = parse_ensembl_exons(exon_lines)
        nr_exons = 1000000
    start_insertion = datetime.now()
    loaded_exons = 0
    LOG.info('Loading exons...')
    with progressbar(exons, label='Loading exons', length=nr_exons) as bar:
        for exon in bar:
            ensg_id = exon['gene']
            enst_id = exon['transcript']
            gene_obj = ensembl_genes.get(ensg_id)
            if (not gene_obj):
                continue
            hgnc_id = gene_obj['hgnc_id']
            if (not (enst_id in hgnc_id_transcripts[hgnc_id])):
                continue
            exon['hgnc_id'] = hgnc_id
            exon_obj = build_exon(exon, build)
            adapter.load_exon(exon_obj)
            loaded_exons += 1
    LOG.info('Number of exons in build {0}: {1}'.format(build, nr_exons))
    LOG.info('Number loaded: {0}'.format(loaded_exons))
    LOG.info('Time to load exons: {0}'.format((datetime.now() - start_insertion)))

Load all the exons

Transcript information is from ensembl.
Check that the transcript that the exon belongs to exists in the database

Args:
adapter(MongoAdapter)
exon_lines(iterable): iterable with ensembl exon lines
build(str)
ensembl_transcripts(dict): Existing ensembl transcripts


def _GetMessage(self, event_object):

    formatter_mediator = formatters_mediator.FormatterMediator()

    result = ''
    try:
      result, _ = formatters_manager.FormattersManager.GetMessageStrings(
          formatter_mediator, event_object)
    except KeyError as exception:
      logging.warning(
          'Unable to correctly assemble event with error: {0!s}'.format(
              exception))

    return result

Returns a properly formatted message string.

Args:
event_object: the event object (instance od EventObject).

Returns:
A formatted message string.


def _str_dotted_getattr(obj, name):
    for part in name.split('.'):
        obj = getattr(obj, part)
    return (str(obj) if obj else None)

Expands extends getattr to allow dots in x to indicate nested objects.

Args:
obj (object): an object.
name (str): a name for a field in the object.

Returns:
Any: the value of named attribute.

Raises:
AttributeError: if the named attribute does not exist.


def interm_fluent_variables(self) -> FluentParamsList:
    fluents = self.domain.intermediate_fluents
    ordering = self.domain.interm_fluent_ordering
    return self._fluent_params(fluents, ordering)

Returns the instantiated intermediate fluents in canonical order.

Returns:
Sequence[Tuple[str, List[str]]]: A tuple of pairs of fluent name
and a list of instantiated fluents represented as strings.


def check_R_package(self, package):
    test_package = (not bool(launch_R_script('{}/R_templates/test_import.R'.format(os.path.dirname(os.path.realpath(__file__))), {'{package}': package}, verbose=True)))
    return test_package

Execute a subprocess to check the package's availability.

Args:
package (str): Name of the package to be tested.

Returns:
bool: `True` if the package is available, `False` otherwise


def remove(self, path):

        norm_path = self.absnormpath(path)
        if self.ends_with_path_separator(path):
            self._handle_broken_link_with_trailing_sep(norm_path)
        if self.exists(norm_path):
            obj = self.resolve(norm_path)
            if S_IFMT(obj.st_mode) == S_IFDIR:
                link_obj = self.lresolve(norm_path)
                if S_IFMT(link_obj.st_mode) != S_IFLNK:
                    if self.is_windows_fs:
                        error = errno.EACCES
                    elif self.is_macos:
                        error = errno.EPERM
                    else:
                        error = errno.EISDIR
                    self.raise_os_error(error, norm_path)

                norm_path = make_string_path(norm_path)
                if path.endswith(self.path_separator):
                    if self.is_windows_fs:
                        error = errno.EACCES
                    elif self.is_macos:
                        error = errno.EPERM
                    else:
                        error = errno.ENOTDIR
                    self.raise_os_error(error, norm_path)
            else:
                self.raise_for_filepath_ending_with_separator(path, obj)

        try:
            self.remove_object(norm_path)
        except IOError as exc:
            self.raise_os_error(exc.errno, exc.filename)

Remove the FakeFile object at the specified file path.

Args:
path: Path to file to be removed.

Raises:
OSError: if path points to a directory.
OSError: if path does not exist.
OSError: if removal failed.


def invert(self, copy=False):

        if copy:
            return Striplog([i.invert(copy=True) for i in self])
        else:
            for i in self:
                i.invert()
            self.__sort()
            o = self.order
            self.order = {'depth': 'elevation', 'elevation': 'depth'}[o]
            return

Inverts the striplog, changing its order and the order of its contents.

Operates in place by default.

Args:
copy (bool): Whether to operate in place or make a copy.

Returns:
None if operating in-place, or an inverted copy of the striplog
if not.


def get_consensus_module(module_name):

        module_package = module_name
        if module_name == 'genesis':
            module_package = (
                'sawtooth_validator.journal.consensus.genesis.'
                'genesis_consensus'
            )
        elif module_name == 'devmode':
            module_package = (
                'sawtooth_validator.journal.consensus.dev_mode.'
                'dev_mode_consensus'
            )

        try:
            return importlib.import_module(module_package)
        except ImportError:
            raise UnknownConsensusModuleError(
                'Consensus module "{}" does not exist.'.format(module_name))

Returns a consensus module by name.

Args:
module_name (str): The name of the module to load.

Returns:
module: The consensus module.

Raises:
UnknownConsensusModuleError: Raised if the given module_name does
not correspond to a consensus implementation.


def get_max_capgrav(self, remove=True, insert=True):

        weight = self.comp.weight
        if insert:
            weight += self.max_cation_insertion * self.cation.atomic_mass
        return self._get_max_cap_ah(remove, insert) / (weight / 1000)

Give max capacity in mAh/g for inserting and removing a charged cation
Note that the weight is normalized to the most lithiated state,
thus removal of 1 Li from LiFePO4 gives the same capacity as insertion of 1 Li into FePO4.

Args:
remove: (bool) whether to allow cation removal
insert: (bool) whether to allow cation insertion

Returns:
max grav capacity in mAh/g


def _asset_path_from_tensor(path_tensor):
    if not isinstance(path_tensor, tensor.Tensor):
        raise TypeError(f'Asset path tensor {path_tensor} must be a Tensor.')
    if path_tensor.op.type != 'Const':
        raise TypeError(f'Asset path tensor {path_tensor} must be of type constant.Has type {path_tensor.op.type} instead.')
    if path_tensor.dtype != dtypes.string:
        raise TypeError(f'Asset path tensor {path_tensor}` must be of dtype string.Has type {path_tensor.dtype} instead.')
    str_values = path_tensor.op.get_attr('value').string_val
    if len(str_values) != 1:
        raise TypeError(f'Asset path tensor {path_tensor} must be a scalar.')
    return str_values[0]

Returns the filepath value stored in constant `path_tensor`.

Args:
path_tensor: Tensor of a file-path.

Returns:
The string value i.e. path of the tensor, if valid.

Raises:
TypeError if tensor does not match expected op type, dtype or value.


def _list(self, request, start_response):

    configs = []
    generator = directory_list_generator.DirectoryListGenerator(request)
    for config in self._config_manager.configs.itervalues():
      if config != self.API_CONFIG:
        configs.append(config)
    directory = generator.pretty_print_config_to_json(configs)
    if not directory:
      _logger.error('Failed to get API directory')

      return util.send_wsgi_not_found_response(start_response)
    return self._send_success_response(directory, start_response)

Sends HTTP response containing the API directory.

This calls start_response and returns the response body.

Args:
request: An ApiRequest, the transformed request sent to the Discovery API.
start_response: A function with semantics defined in PEP-333.

Returns:
A string containing the response body.


def ReadLines(self, file_object):

    lines_size = len(self.lines)
    if lines_size < self._buffer_size:
      lines_size = self._buffer_size - lines_size
      while lines_size > 0:
        line = self._ReadLine(file_object)
        if not line:
          break

        self.lines = ''.join([self.lines, line])
        lines_size -= len(line)

Reads lines into the lines buffer.

Args:
file_object (dfvfs.FileIO): file-like object.


def unalias_tool(self, context_name, tool_name):

        data = self._context(context_name)
        aliases = data["tool_aliases"]
        if tool_name in aliases:
            del aliases[tool_name]
            self._flush_tools()

Deregister an alias for a specific tool.

Args:
context_name (str): Context containing the tool.
tool_name (str): Name of tool to unalias.


def solidity_resolve_address(hex_code, library_symbol, library_address):

    if library_address.startswith('0x'):
        raise ValueError('Address should not contain the 0x prefix')

    try:
        decode_hex(library_address)
    except TypeError:
        raise ValueError(
            'library_address contains invalid characters, it must be hex encoded.')

    if len(library_symbol) != 40 or len(library_address) != 40:
        raise ValueError('Address with wrong length')

    return hex_code.replace(library_symbol, library_address)

Change the bytecode to use the given library address.

Args:
hex_code (bin): The bytecode encoded in hexadecimal.
library_name (str): The library that will be resolved.
library_address (str): The address of the library.

Returns:
bin: The bytecode encoded in hexadecimal with the library references
resolved.


def CopyFromDateTimeString(self, time_string):
    date_time_values = self._CopyDateTimeFromString(time_string)
    year = date_time_values.get('year', 0)
    month = date_time_values.get('month', 0)
    day_of_month = date_time_values.get('day_of_month', 0)
    hours = date_time_values.get('hours', 0)
    minutes = date_time_values.get('minutes', 0)
    seconds = date_time_values.get('seconds', 0)
    microseconds = date_time_values.get('microseconds', 0)
    self._timestamp = self._GetNumberOfSecondsFromElements(year, month, day_of_month, hours, minutes, seconds)
    self.fraction_of_second = microseconds
    if (pytsk3.TSK_VERSION_NUM >= 67240191):
        self.fraction_of_second *= 1000
    else:
        self.fraction_of_second *= 10
    self._normalized_timestamp = None
    self.is_local_time = False

Copies a SleuthKit timestamp from a date and time string.

Args:
time_string (str): date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######[+-]##:##

Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The time of day, seconds
fraction and time zone offset are optional. The default time zone
is UTC.


def __write_to_hdf5_heavy(self, filename_out, *args, **kwargs):

        block_size = 0

        chunk_dim = self.__get_chunk_dimensions()
        blob_dim = self.__get_blob_dimensions(chunk_dim)
        n_blobs = self.container.calc_n_blobs(blob_dim)

        with h5py.File(filename_out, 'w') as h5:

            h5.attrs[b'CLASS'] = b'FILTERBANK'
            h5.attrs[b'VERSION'] = b'1.0'

            if HAS_BITSHUFFLE:
                bs_compression = bitshuffle.h5.H5FILTER
                bs_compression_opts = (block_size, bitshuffle.h5.H5_COMPRESS_LZ4)
            else:
                bs_compression = None
                bs_compression_opts = None
                logger.warning("Warning: bitshuffle not found. No compression applied.")

            dset = h5.create_dataset('data',
                            shape=self.selection_shape,
                            chunks=chunk_dim,
                            compression=bs_compression,
                            compression_opts=bs_compression_opts,
                            dtype=self.data.dtype)

            dset_mask = h5.create_dataset('mask',
                            shape=self.selection_shape,
                            chunks=chunk_dim,
                            compression=bs_compression,
                            compression_opts=bs_compression_opts,
                            dtype='uint8')

            dset.dims[0].label = b"frequency"
            dset.dims[1].label = b"feed_id"
            dset.dims[2].label = b"time"

            dset_mask.dims[0].label = b"frequency"
            dset_mask.dims[1].label = b"feed_id"
            dset_mask.dims[2].label = b"time"

            for key, value in self.header.items():
                dset.attrs[key] = value

            if blob_dim[self.freq_axis] < self.selection_shape[self.freq_axis]:

                logger.info('Using %i n_blobs to write the data.'% n_blobs)
                for ii in range(0, n_blobs):
                    logger.info('Reading %i of %i' % (ii + 1, n_blobs))

                    bob = self.container.read_blob(blob_dim,n_blob=ii)

                    c_start = self.container.chan_start_idx + ii*blob_dim[self.freq_axis]
                    t_start = self.container.t_start + (c_start/self.selection_shape[self.freq_axis])*blob_dim[self.time_axis]
                    t_stop = t_start + blob_dim[self.time_axis]

                    c_start = (c_start)%self.selection_shape[self.freq_axis]
                    c_stop = c_start + blob_dim[self.freq_axis]

                    logger.debug(t_start,t_stop,c_start,c_stop)

                    dset[t_start:t_stop,0,c_start:c_stop] = bob[:]

            else:

                logger.info('Using %i n_blobs to write the data.'% n_blobs)
                for ii in range(0, n_blobs):
                    logger.info('Reading %i of %i' % (ii + 1, n_blobs))

                    bob = self.container.read_blob(blob_dim,n_blob=ii)
                    t_start = self.container.t_start + ii*blob_dim[self.time_axis]

                    if (ii+1)*blob_dim[self.time_axis] > self.n_ints_in_file:
                        t_stop = self.n_ints_in_file
                    else:
                        t_stop = (ii+1)*blob_dim[self.time_axis]

                    dset[t_start:t_stop] = bob[:]

Write data to HDF5 file.

Args:
filename_out (str): Name of output file


def GetArtifact(self, name):

    self._CheckDirty()
    result = self._artifacts.get(name)
    if not result:
      raise rdf_artifacts.ArtifactNotRegisteredError(
          "Artifact %s missing from registry. You may need to sync the "
          "artifact repo by running make in the artifact directory." % name)
    return result

Get artifact by name.

Args:
name: artifact name string.

Returns:
artifact object.
Raises:
ArtifactNotRegisteredError: if artifact doesn't exist in the registy.


def maybe_download_from_drive(directory, filename, url):

  if not tf.gfile.Exists(directory):
    tf.logging.info("Creating directory %s" % directory)
    tf.gfile.MakeDirs(directory)
  filepath = os.path.join(directory, filename)
  confirm_token = None
  if tf.gfile.Exists(filepath):
    tf.logging.info("Not downloading, file already found: %s" % filepath)
    return filepath

  confirm_token = None
  session = requests.Session()
  response = session.get(url, stream=True)
  for k, v in response.cookies.items():
    if k.startswith("download_warning"):
      confirm_token = v

  if confirm_token:
    url = url + "&confirm=" + confirm_token
  tf.logging.info("Downloading %s to %s" % (url, filepath))

  response = session.get(url, stream=True)

  chunk_size = 16 * 1024
  with open(filepath, "wb") as f:
    for chunk in response.iter_content(chunk_size):
      if chunk:
        f.write(chunk)

  print()
  statinfo = os.stat(filepath)
  tf.logging.info("Successfully downloaded %s, %s bytes." % (filename,
                                                             statinfo.st_size))
  return filepath

Download filename from Google drive unless it's already in directory.

Args:
directory: path to the directory that will be used.
filename: name of the file to download to (do nothing if it already exists).
url: URL to download from.

Returns:
The path to the downloaded file.


def scalarize(function):

    def decorated(self, X, *args, **kwargs):
        scalar = not isinstance(X, np.ndarray)

        if scalar:
            X = np.array([X])

        result = function(self, X, *args, **kwargs)
        if scalar:
            result = result[0]

        return result

    decorated.__doc__ = function.__doc__
    return decorated

Allow methods that only accepts 1-d vectors to work with scalars.

Args:
function(callable): Function that accepts and returns vectors.

Returns:
callable: Decorated function that accepts and returns scalars.


def parse_transcripts(transcript_lines):

    LOG.info("Parsing transcripts")

    if isinstance(transcript_lines, DataFrame):
        transcripts = parse_ensembl_transcript_request(transcript_lines)
    else:
        transcripts = parse_ensembl_transcripts(transcript_lines)

    parsed_transcripts = {}

    for tx in transcripts:
        tx_id = tx['ensembl_transcript_id']
        ens_gene_id = tx['ensembl_gene_id']

        if not tx_id in parsed_transcripts:
            tx_info = {
                'chrom': tx['chrom'],
                'transcript_start': tx['transcript_start'],
                'transcript_end': tx['transcript_end'],
                'mrna': set(),
                'mrna_predicted': set(),
                'nc_rna': set(),
                'ensembl_gene_id': ens_gene_id,
                'ensembl_transcript_id': tx_id,
            }
            parsed_transcripts[tx_id] = tx_info

        tx_info = parsed_transcripts[tx_id]

        if tx.get('refseq_mrna_predicted'):
            tx_info['mrna_predicted'].add(tx['refseq_mrna_predicted'])
        if tx.get('refseq_mrna'):
            tx_info['mrna'].add(tx['refseq_mrna'])
        if tx.get('refseq_ncrna'):
            tx_info['nc_rna'].add(tx['refseq_ncrna'])

    return parsed_transcripts

Parse and massage the transcript information

There could be multiple lines with information about the same transcript.
This is why it is necessary to parse the transcripts first and then return a dictionary
where all information has been merged.

Args:
transcript_lines(): This could be an iterable with strings or a pandas.DataFrame

Returns:
parsed_transcripts(dict): Map from enstid -> transcript info


def Get(self, request, global_params=None):
    config = self.GetMethodConfig('Get')
    return self._RunMethod(config, request, global_params=global_params)

Retrieve a GitHubEnterpriseConfig.

Args:
request: (CloudbuildProjectsLocationsGithubEnterpriseConfigsGetRequest) input message
global_params: (StandardQueryParameters, default: None) global arguments
Returns:
(GitHubEnterpriseConfig) The response message.


def _valid_dtypes(self):
    return _DEFAULT_VALID_DTYPES

Valid types for loss, variables and gradients.

Subclasses should override to allow other float types.

Returns:
Valid types for loss, variables and gradients.


def get_special_tokens_mask(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None, already_has_special_tokens: bool=False) -> List[int]:
    if already_has_special_tokens:
        return super().get_special_tokens_mask(token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True)
    if token_ids_1 is not None:
        return [1] + [0] * len(token_ids_0) + [1] + [0] * len(token_ids_1) + [1]
    return [1] + [0] * len(token_ids_0) + [1]

Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer `prepare_for_model` method.

Args:
token_ids_0 (`List[int]`):
List of IDs.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
Whether or not the token list is already formatted with special tokens for the model.

Returns:
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.


def run(self, args, options):

        self.pre_run(args, options)
        args_and_options = args.copy()
        args_and_options.update(options)
        uploadchannel(self, **args_and_options)

This function calls uploadchannel which performs all the run steps:
- Create ChannelNode
- Pupulate Tree with TopicNodes, ContentNodes, and associated File objects
- .
- ..
- ...

Args:
args (dict): ricecooker command line arguments
options (dict): additional compatibility mode options given on command line


def shape2d(a):
    if (type(a) == int):
        return [a, a]
    if isinstance(a, (list, tuple)):
        assert (len(a) == 2)
        return list(a)
    raise RuntimeError('Illegal shape: {}'.format(a))

Ensure a 2D shape.

Args:
a: a int or tuple/list of length 2

Returns:
list: of length 2. if ``a`` is a int, return ``[a, a]``.


def batch_reduce_and_verify(self, inputs, expect, options):

    def replica_fn():
        CollectiveReplicaLauncher._prefer_unique_instance_key = options.prefer_unique_instance_key
        collective, devices, pid = self.make_collective(options.num_processes, options.gpus_per_process)

        def batch_reduce_fn():
            batch_size = len(inputs[0])
            value_dst_pairs = []
            for i in range(batch_size):

                def value_fn(device_idx, idx=i):
                    return inputs[pid * len(devices) + device_idx][idx]
                per_replica_value = make_per_replica_value(value_fn, devices)
                value_dst_pairs.append((per_replica_value, per_replica_value))
            reduced_values = collective.batch_reduce(options.reduce_op, value_dst_pairs, options.communication_options)
            if options.gpus_per_process > 1:
                for v in reduced_values:
                    self.assertIsInstance(v, value_lib.Mirrored)
            reduced_values = [self.as_list(v) for v in reduced_values]
            for v in reduced_values:
                self.assertAllEqual(devices, [t.device for t in v])
            return nest.map_structure(ops.convert_to_tensor, reduced_values)
        per_replica_expect = nest.map_structure(lambda x: [ops.convert_to_tensor(x)] * len(devices), expect)
        if 'eager' in options.mode:
            got = batch_reduce_fn()
            self.assertAllClose(got, per_replica_expect)
        if 'func_graph' in options.mode:
            got = def_function.function(batch_reduce_fn)()
            self.assertAllClose(got, per_replica_expect)
    get_global_mpr(options.num_processes).run(replica_fn)

Batch reduce the given `inputs` and verify the output matches `expect`.

Args:
inputs: a 2-level nested list of `Tensor` or `IndexedSlices`, where i-th
value will be fed to i-th replica.
expect: a list of `Tensor` or `IndexedSlices`. This should be the expected
value for one replica.
options: a `RunOpotions` instance.


def unpack(self, buff, offset=0):

        header = UBInt16()
        header.unpack(buff[offset:offset+2])
        self.tlv_type = header.value >> 9
        length = header.value & 511
        begin, end = offset + 2, offset + 2 + length
        self._value = BinaryData(buff[begin:end])

Unpack a binary message into this object's attributes.

Unpack the binary value *buff* and update this object attributes based
on the results.

Args:
buff (bytes): Binary data package to be unpacked.
offset (int): Where to begin unpacking.

Raises:
Exception: If there is a struct unpacking error.