def _ReadTablesArray(self, file_object, tables_array_offset):

    data_type_map = self._GetDataTypeMap('keychain_tables_array')

    tables_array, _ = self._ReadStructureFromFileObject(
        file_object, tables_array_offset, data_type_map)

    tables = collections.OrderedDict()
    for table_offset in tables_array.table_offsets:
      self._ReadTable(tables, file_object, tables_array_offset + table_offset)

    return tables

Reads the tables array.

Args:
file_object (file): file-like object.
tables_array_offset (int): offset of the tables array relative to
the start of the file.

Returns:
dict[int, KeychainDatabaseTable]: tables per identifier.

Raises:
ParseError: if the tables array cannot be read.


def sin(x):
    if any_symbolic_tensors((x,)):
        return Sin().symbolic_call(x)
    return backend.numpy.sin(x)

Trigonometric sine, element-wise.

Arguments:
x: Input tensor.

Returns:
Output tensor of same shape as `x`.


def _setup_test_logger(log_path, prefix=None):

    log = logging.getLogger()
    kill_test_logger(log)
    log.propagate = False
    log.setLevel(logging.DEBUG)

    terminal_format = log_line_format
    if prefix:
        terminal_format = '[%s] %s' % (prefix, log_line_format)
    c_formatter = logging.Formatter(terminal_format, log_line_time_format)
    ch = logging.StreamHandler(sys.stdout)
    ch.setFormatter(c_formatter)
    ch.setLevel(logging.INFO)

    f_formatter = logging.Formatter(log_line_format, log_line_time_format)

    fh_info = logging.FileHandler(
        os.path.join(log_path, records.OUTPUT_FILE_INFO_LOG))
    fh_info.setFormatter(f_formatter)
    fh_info.setLevel(logging.INFO)
    fh_debug = logging.FileHandler(
        os.path.join(log_path, records.OUTPUT_FILE_DEBUG_LOG))
    fh_debug.setFormatter(f_formatter)
    fh_debug.setLevel(logging.DEBUG)
    log.addHandler(ch)
    log.addHandler(fh_info)
    log.addHandler(fh_debug)
    log.log_path = log_path
    logging.log_path = log_path

Customizes the root logger for a test run.

The logger object has a stream handler and a file handler. The stream
handler logs INFO level to the terminal, the file handler logs DEBUG
level to files.

Args:
log_path: Location of the log file.
prefix: A prefix for each log line in terminal.
filename: Name of the log file. The default is the time the logger
is requested.


def standardize(self, x):
    if self.preprocessing_function:
        x = self.preprocessing_function(x)
    if self.rescale:
        x *= self.rescale
    if self.samplewise_center:
        x -= np.mean(x, keepdims=True)
    if self.samplewise_std_normalization:
        x /= np.std(x, keepdims=True) + 1e-06
    if self.featurewise_center:
        if self.mean is not None:
            x -= self.mean
        else:
            warnings.warn("This ImageDataGenerator specifies `featurewise_center`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.")
    if self.featurewise_std_normalization:
        if self.std is not None:
            x /= self.std + 1e-06
        else:
            warnings.warn("This ImageDataGenerator specifies `featurewise_std_normalization`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.")
    if self.zca_whitening:
        if self.zca_whitening_matrix is not None:
            flat_x = x.reshape(-1, np.prod(x.shape[-3:]))
            white_x = flat_x @ self.zca_whitening_matrix
            x = np.reshape(white_x, x.shape)
        else:
            warnings.warn("This ImageDataGenerator specifies `zca_whitening`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.")
    return x

Applies the normalization configuration in-place to a batch of
inputs.

`x` is changed in-place since the function is mainly used internally
to standardize images and feed them to your network. If a copy of `x`
would be created instead it would have a significant performance cost.
If you want to apply this method without changing the input in-place
you can call the method creating a copy before:

standardize(np.copy(x))

Args:
x: Batch of inputs to be normalized.

Returns:
The inputs, normalized.


def set_active(self, username, active_state):

        if active_state not in (True, False):
            raise ValueError("active_state must be True or False")

        user = self.get_user(username)
        if user is None:
            return None

        if user['active'] is active_state:

            return True

        user['active'] = active_state
        response = self._put(self.rest_url + "/user",
                             params={"username": username},
                             data=json.dumps(user))

        if response.status_code == 204:
            return True

        return None

Set the active state of a user

Args:
username: The account username
active_state: True or False

Returns:
True: If successful
None: If no user or failure occurred


async def get_all(self, url, params=None):

        if not params:
            params = {}
        items = []
        next_page_token = None

        while True:
            if next_page_token:
                params['pageToken'] = next_page_token
            response = await self.get_json(url, params=params)

            items.append(response)
            next_page_token = response.get('nextPageToken')
            if not next_page_token:
                break
        return items

Aggregate data from all pages of an API query.

Args:
url (str): Google API endpoint URL.
params (dict): (optional) URL query parameters.

Returns:
list: Parsed JSON query response results.


def typical_or_extreme_period_name(self, value=None):
    if (value is not None):
        try:
            value = str(value)
        except ValueError:
            raise ValueError('value {} need to be of type str for field `typical_or_extreme_period_name`'.format(value))
        if (',' in value):
            raise ValueError('value should not contain a comma for field `typical_or_extreme_period_name`')
    self._typical_or_extreme_period_name = value

Corresponds to IDD Field `typical_or_extreme_period_name`

Args:
value (str): value for IDD Field `typical_or_extreme_period_name`
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


async def get_tournaments(self, subdomain: str=None, force_update: bool=False) -> list:
    if (self.tournaments is None):
        force_update = True
        self._subdomains_searched.append(('' if (subdomain is None) else subdomain))
    elif ((subdomain is None) and ('' not in self._subdomains_searched)):
        force_update = True
        self._subdomains_searched.append('')
    elif ((subdomain is not None) and (subdomain not in self._subdomains_searched)):
        force_update = True
        self._subdomains_searched.append(subdomain)
    if force_update:
        params = {'include_participants': (1 if AUTO_GET_PARTICIPANTS else 0), 'include_matches': (1 if AUTO_GET_MATCHES else 0)}
        if (subdomain is not None):
            params['subdomain'] = subdomain
        res = (await self.connection('GET', 'tournaments', **params))
        if (len(res) == 0):
            self.tournaments = []
        else:
            for t_data in res:
                self._refresh_tournament_from_json(t_data)
    return self.tournaments

gets all user's tournaments

|methcoro|

Args:
subdomain: *optional* subdomain needs to be given explicitely to get tournaments in a subdomain
force_update: *optional* set to True to force the data update from Challonge

Returns:
list[Tournament]: list of all the user tournaments

Raises:
APIException


def sample_node_list(self, low, high, generator):
    statements = []
    for _ in range(np.random.randint(low, high)):
        statements.append(generator())
    return statements

Generate a list of statements of random length.

Args:
low: Fewest number of statements to generate.
high: Highest number of statements to generate.
generator: Function to call to generate nodes.

Returns:
A list of statements.


def add(self, origin):

        digest = self._calc_digest(origin)

        if self.exists(digest):
            self.logger.debug('Added File: [{0}] ( Already exists. Skipping transfer)'.format(digest))
            return digest

        absPath = self.get_file_path(digest)
        absFolderPath = os.path.dirname(absPath)

        self._makedirs(absFolderPath)
        self._copy_content(origin, absPath)

        self.logger.debug('Added file: "{0}" [{1}]'.format(digest, absPath))

        return digest

Add new element to fsdb.

Args:
origin -- could be the path of a file or a readable/seekable object ( fileobject, stream, stringIO...)
Returns:
String rapresenting the digest of the file


def convert_variable_to_constant(self, incoming_edge, tensor_data):
    index = incoming_edge.destination.index
    for edge in self.outgoing_edges:
        if edge.source.index == index:
            edge.destination.convertible.convert_variable_to_constant(edge, tensor_data)
    function = self.converted_self().function
    function.signature.input_arg[index].type = tensor_data.dtype
    if '_input_shapes' in function.attr:
        function.attr['_input_shapes'].list.shape[index].unknown_rank = True
        del function.attr['_input_shapes'].list.shape[index].dim[:]
    arg_attrs = function.arg_attr[index].attr
    if '_output_shapes' in arg_attrs:
        arg_attrs['_output_shapes'].list.shape[0].unknown_rank = True
        del arg_attrs['_output_shapes'].list.shape[0].dim[:]

Converts one function argument into a constant.

Args:
incoming_edge: The edge into the argument to be converted.
tensor_data: The constant value.


def enum(cls):
    assert (cls.__bases__ == (object,))
    d = dict(cls.__dict__)
    new_type = type(cls.__name__, (int,), d)
    new_type.__module__ = cls.__module__
    map_ = {}
    for (key, value) in iteritems(d):
        if ((key.upper() == key) and isinstance(value, integer_types)):
            value_instance = new_type(value)
            setattr(new_type, key, value_instance)
            map_[value] = key

    def str_(self):
        if (self in map_):
            return ('%s.%s' % (type(self).__name__, map_[self]))
        return ('%d' % int(self))

    def repr_(self):
        if (self in map_):
            return ('<%s.%s: %d>' % (type(self).__name__, map_[self], int(self)))
        return ('%d' % int(self))
    setattr(new_type, '__repr__', repr_)
    setattr(new_type, '__str__', str_)
    return new_type

A decorator for creating an int enum class.

Makes the values a subclass of the type and implements repr/str.
The new class will be a subclass of int.

Args:
cls (type): The class to convert to an enum

Returns:
type: A new class

::

@enum
class Foo(object):
FOO = 1
BAR = 2


def trigger(self, attr, old, new, hint=None, setter=None):

        def invoke():
            callbacks = self._callbacks.get(attr)
            if callbacks:
                for callback in callbacks:
                    callback(attr, old, new)
        if hasattr(self, '_document') and self._document is not None:
            self._document._notify_change(self, attr, old, new, hint, setter, invoke)
        else:
            invoke()

Trigger callbacks for ``attr`` on this object.

Args:
attr (str) :
old (object) :
new (object) :

Returns:
None


def AddClient(self, client):

    keywords = self.AnalyzeClient(client)
    keywords.add(self._NormalizeKeyword(client.client_id))

    data_store.REL_DB.AddClientKeywords(client.client_id, keywords)

Adds a client to the index.

Args:
client: A Client object record.


def createDirStruct(paths, verbose=True):

    for k, path in paths.items():
        p = None
        try:
            pathlist = path if type(path) is list else [ path ]
            for p in pathlist:
                os.makedirs(p)
                if verbose:
                    log.info('Creating directory: ' + p)
        except OSError, e:

            if e.errno == errno.EEXIST and os.path.isdir(p):
                pass
            else:
                raise

    return True

Loops ait.config._datapaths from AIT_CONFIG and creates a directory.

Replaces year and doy with the respective year and day-of-year.
If neither are given as arguments, current UTC day and year are used.

Args:
paths:
[optional] list of directory paths you would like to create.
doy and year will be replaced by the datetime day and year, respectively.

datetime:
UTC Datetime string in ISO 8601 Format YYYY-MM-DDTHH:mm:ssZ


def get_max_bond_distance(self, el1_sym, el2_sym):

        return sqrt(
            (self.el_radius[el1_sym] + self.el_radius[el2_sym] + self.tol) ** 2)

Use Jmol algorithm to determine bond length from atomic parameters
Args:
el1_sym: (str) symbol of atom 1
el2_sym: (str) symbol of atom 2

Returns: (float) max bond length


def get_cells_iterator(bq_read_client: BigQueryReadClient, table_metadata: TableMetadata, column: str) -> Generator[Any, None, None]:
    if '.' not in column and '[' not in column:
        rows = get_readrows_iterator(bq_read_client, table_metadata, [column], data_format=DataFormat.AVRO)
        for row in rows:
            yield row.get(column)
    else:
        nested_columns = parse_column_path(column)
        parent_column = nested_columns[0][0]
        rows = get_readrows_iterator(bq_read_client, table_metadata, [parent_column], data_format=DataFormat.AVRO)
        for current_value in rows:
            for column_name, key in nested_columns:
                if isinstance(current_value, dict):
                    current_value = current_value.get(column_name)
                elif isinstance(current_value, list) and key:
                    current_value = next((item for item in current_value if item.get(column_name) == key), None)
                if isinstance(current_value, dict) and 'value' in current_value:
                    extracted_value = next((value for key, value in current_value['value'].items() if value is not None), None)
                    current_value = extracted_value if extracted_value is not None else current_value
                if current_value is None:
                    break
            yield current_value

Retrieves an iterator of cell values for a specified column, optimized
for both simple and nested column

access, including handling special value structures with dynamic value types
for nested columns.

Args:
bq_read_client (BigQueryReadClient): The BigQuery Storage API Read client.
table_metadata (TableMetadata): The table's metadata.
column (str): The column name, supporting nested fields and array indices
for complex cases.

Returns:
Generator[Any, None, None]: An iterator over cell values.


def _prefix_from_prefix_int(self, prefixlen):

        if not isinstance(prefixlen, (int, long)):
            raise NetmaskValueError('%r is not an integer' % prefixlen)
        prefixlen = int(prefixlen)
        if not (0 <= prefixlen <= self._max_prefixlen):
            raise NetmaskValueError('%d is not a valid prefix length' %
                                    prefixlen)
        return prefixlen

Validate and return a prefix length integer.

Args:
prefixlen: An integer containing the prefix length.

Returns:
The input, possibly converted from long to int.

Raises:
NetmaskValueError: If the input is not an integer, or out of range.


def easeInOutCubic(n):

    _checkRange(n)
    n = 2 * n
    if n < 1:
        return 0.5 * n**3
    else:
        n = n - 2
        return 0.5 * (n**3 + 2)

A cubic tween function that accelerates, reaches the midpoint, and then decelerates.

Args:
n (float): The time progress, starting at 0.0 and ending at 1.0.

Returns:
(float) The line progress, starting at 0.0 and ending at 1.0. Suitable for passing to getPointOnLine().


def validate_primitive_without_value(fhir_primitive: message.Message) -> None:
    name = fhir_primitive.DESCRIPTOR.full_name
    if len(extensions.get_fhir_extensions(fhir_primitive)) < 2:
        raise fhir_errors.InvalidFhirError(f'{name!r} must have either extensions or a value present.')
    for field, _ in fhir_primitive.ListFields():
        if field.name not in extensions.NON_VALUE_FIELDS:
            raise fhir_errors.InvalidFhirError(f'{name!r} contains PrimitiveHasNoValue but {field.name!r} is set.')

Validates a Message which has the PrimitiveWithoutValue extension.

Given that there is a PrimitiveWithoutValue extension present, there must be
at least one other extension. Otherwise, there is truly no value set other
than id and/or extension (non-value fields).

Args:
fhir_primitive: The FHIR primitive Message to validate.

Raises:
fhir_errors.InvalidFhirError: In the event that there is less than one
extension present, or there are values set other than id and/or extension.


def decorate_event_js(js_code):

    def add_annotation(method):
        setattr(method, '__is_event', True)
        setattr(method, '_js_code', js_code)
        return method
    return add_annotation

setup a method as an event, adding also javascript code to generate

Args:
js_code (str): javascript code to generate the event client-side.
js_code is added to the widget html as
widget.attributes['onclick'] = js_code%{'emitter_identifier':widget.identifier, 'event_name':'onclick'}


def get_reconciler(config, metrics, rrset_channel, changes_channel, **kw):
    builder = reconciler.GDNSReconcilerBuilder(config, metrics, rrset_channel, changes_channel, **kw)
    return builder.build_reconciler()

Get a GDNSReconciler client.

A factory function that validates configuration, creates an auth
and :class:`GDNSClient` instance, and returns a GDNSReconciler
provider.

Args:
config (dict): Google Cloud Pub/Sub-related configuration.
metrics (obj): :interface:`IMetricRelay` implementation.
rrset_channel (asyncio.Queue): Queue from which to consume
record set messages to validate.
changes_channel (asyncio.Queue): Queue to publish message to
make corrections to Cloud DNS.
kw (dict): Additional keyword arguments to pass to the
Reconciler.
Returns:
A :class:`GDNSReconciler` instance.


def build_ann(N_input=None, N_hidden=2, N_output=1, hidden_layer_type='Linear', verbosity=1):
    N_input = (N_input or 1)
    N_output = (N_output or 1)
    N_hidden = (N_hidden or tuple())
    if isinstance(N_hidden, (int, float, basestring)):
        N_hidden = (int(N_hidden),)
    hidden_layer_type = (hidden_layer_type or tuple())
    hidden_layer_type = tuplify(normalize_layer_type(hidden_layer_type))
    if (verbosity > 0):
        print(N_hidden, ' layers of type ', hidden_layer_type)
    assert (len(N_hidden) == len(hidden_layer_type))
    nn = pb.structure.FeedForwardNetwork()
    nn.addInputModule(pb.structure.BiasUnit(name='bias'))
    nn.addInputModule(pb.structure.LinearLayer(N_input, name='input'))
    for (i, (Nhid, hidlaytype)) in enumerate(zip(N_hidden, hidden_layer_type)):
        Nhid = int(Nhid)
        nn.addModule(hidlaytype(Nhid, name=('hidden-{}'.format(i) if i else 'hidden')))
    nn.addOutputModule(pb.structure.LinearLayer(N_output, name='output'))
    nn.addConnection(pb.structure.FullConnection(nn['bias'], (nn['hidden'] if N_hidden else nn['output'])))
    nn.addConnection(pb.structure.FullConnection(nn['input'], (nn['hidden'] if N_hidden else nn['output'])))
    for (i, (Nhid, hidlaytype)) in enumerate(zip(N_hidden[:(- 1)], hidden_layer_type[:(- 1)])):
        Nhid = int(Nhid)
        nn.addConnection(pb.structure.FullConnection(nn[('hidden-{}'.format(i) if i else 'hidden')], nn['hidden-{}'.format((i + 1))]))
    i = (len(N_hidden) - 1)
    nn.addConnection(pb.structure.FullConnection(nn[('hidden-{}'.format(i) if i else 'hidden')], nn['output']))
    nn.sortModules()
    if FAST:
        try:
            nn.convertToFastNetwork()
        except:
            if (verbosity > 0):
                print('Unable to convert slow PyBrain NN to a fast ARAC network...')
    if (verbosity > 0):
        print(nn.connections)
    return nn

Build a neural net with the indicated input, hidden, and outout dimensions

Arguments:
params (dict or PyBrainParams namedtuple):
default: {'N_hidden': 6}
(this is the only parameter that affects the NN build)

Returns:
FeedForwardNetwork with N_input + N_hidden + N_output nodes in 3 layers


def stop(self):
    self._logger.info('Cleaning up remaining connection threads.')
    for thread in threading.enumerate():
        if (thread is not threading.current_thread()):
            try:
                thread.join(10.0)
            except Exception as e:
                self._logger.info('Error occurred while attempting to cleanup thread: {0}'.format(thread.name))
                self._logger.exception(e)
            else:
                if thread.is_alive():
                    self._logger.warning('Cleanup failed for thread: {0}. Thread is still alive'.format(thread.name))
                else:
                    self._logger.info('Cleanup succeeded for thread: {0}'.format(thread.name))
    self._logger.info('Shutting down server socket handler.')
    try:
        self._socket.shutdown(socket.SHUT_RDWR)
        self._socket.close()
    except Exception as e:
        self._logger.exception(e)
        raise exceptions.NetworkingError('Server failed to shutdown socket handler.')
    if hasattr(self, 'policy_monitor'):
        try:
            self.policy_monitor.stop()
            self.policy_monitor.join()
        except Exception as e:
            self._logger.exception(e)
            raise exceptions.ShutdownError('Server failed to clean up the policy monitor.')

Stop the server.

Halt server client connections and clean up any existing connection
threads.

Raises:
NetworkingError: Raised if a failure occurs while sutting down
or closing the TLS server socket.


def do_batch_status(args):

    rest_client = RestClient(args.url, args.user)
    batch_ids = args.batch_ids.split(',')

    if args.wait and args.wait > 0:
        statuses = rest_client.get_statuses(batch_ids, args.wait)
    else:
        statuses = rest_client.get_statuses(batch_ids)

    if args.format == 'yaml':
        fmt.print_yaml(statuses)
    elif args.format == 'json':
        fmt.print_json(statuses)
    else:
        raise AssertionError('Missing handler: {}'.format(args.format))

Runs the batch-status command, printing output to the console

Args:
args: The parsed arguments sent to the command at runtime


def git_checkout(branch_name, create=False):
    log.info('Checking out <33>{}'.format(branch_name))
    shell.run('git checkout {} {}'.format(('-b' if create else ''), branch_name))

Checkout or create a given branch

Args:
branch_name (str):
The name of the branch to checkout or create.
create (bool):
If set to **True** it will create the branch instead of checking it
out.


def __init__(self, sv, sess):
    super(SVSummaryThread, self).__init__(sv.coord, sv.save_summaries_secs)
    self._sv = sv
    self._sess = sess

Create a SVSummaryThread.

Args:
sv: A `Supervisor`.
sess: A `Session`.


def __call__(self, batch: List[List[str]], mean: bool = None) -> List[Union[list, np.ndarray]]:

        batch = [self._encode(sample, mean) for sample in batch]
        if self.pad_zero:
            batch = zero_pad(batch)
        return batch

Embed sentences from batch

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

Returns:
embedded batch


def _GetFlagValues(self, flags):
    event_types = []
    for (event_flag, description) in self._FLAG_VALUES.items():
        if (event_flag & flags):
            event_types.append(description)
    return ', '.join(event_types)

Determines which events are indicated by a set of fsevents flags.

Args:
flags (int): fsevents record flags.

Returns:
str: a comma separated string containing descriptions of the flag values
stored in an fsevents record.


def format_rpc(data):

    address, rpc_id, args, resp, _status = data

    name = rpc_name(rpc_id)

    if isinstance(args, (bytes, bytearray)):
        arg_str = hexlify(args)
    else:
        arg_str = repr(args)

    if isinstance(resp, (bytes, bytearray)):
        resp_str = hexlify(resp)
    else:
        resp_str = repr(resp)

    return "%s called on address %d, payload=%s, response=%s" % (name, address, arg_str, resp_str)

Format an RPC call and response.

Args:
data (tuple): A tuple containing the address, rpc_id, argument and
response payloads and any error code.

Returns:
str: The formated RPC string.


def from_url(cls, path):

        if os.path.isfile(path):
            with open(path) as fd:
                data = fd.read()
        else:
            try:
                response = urllib.urlopen(path)
                if response.code >= 300:
                    raise RuntimeError('Unable to load repo from %s' % path)

                data = response.read()
                response.close()
            except IOError:
                raise RuntimeError(
                    'Unable to load repo from %s (IO error)' % path
                )

        return cls(json.loads(data), path)

Instantiate a :class:`TemplateRepository` instance from the data in a
file or url

Args:
path (str): Path or url to the json file to load

Returns:
TemplateRepository: A new instance


def find_code_in_transformers(object_name: str, base_path: Optional[str]=None, return_indices: bool=False) -> Union[str, Tuple[List[str], int, int]]:
    parts = object_name.split('.')
    i = 0
    if base_path is None:
        base_path = TRANSFORMERS_PATH
    if base_path == MODEL_TEST_PATH:
        base_path = 'tests'
    module = parts[i]
    while i < len(parts) and (not os.path.isfile(os.path.join(base_path, f'{module}.py'))):
        i += 1
        if i < len(parts):
            module = os.path.join(module, parts[i])
    if i >= len(parts):
        raise ValueError(f'`object_name` should begin with the name of a module of transformers but got {object_name}.')
    with open(os.path.join(base_path, f'{module}.py'), 'r', encoding='utf-8', newline='\n') as f:
        lines = f.readlines()
    indent = ''
    line_index = 0
    for name in parts[i + 1:]:
        while line_index < len(lines) and re.search(f'^{indent}(class|def)\\s+{name}(\\(|\\:)', lines[line_index]) is None:
            line_index += 1
        indent += '    '
        line_index += 1
    if line_index >= len(lines):
        raise ValueError(f' {object_name} does not match any function or class in {module}.')
    start_index = line_index - 1
    end_index = find_block_end(lines, start_index, len(indent))
    code = ''.join(lines[start_index:end_index])
    return (code, (lines, start_index, end_index)) if return_indices else code

Find and return the source code of an object.

Args:
object_name (`str`):
The name of the object we want the source code of.
base_path (`str`, *optional*):
The path to the base folder where files are checked. If not set, it will be set to `TRANSFORMERS_PATH`.
return_indices(`bool`, *optional*, defaults to `False`):
If `False`, will only return the code (as a string), otherwise it will also return the whole lines of the
file where the object specified by `object_name` is defined, together the start/end indices of the block in
the file that defines the object.

Returns:
`Union[str, Tuple[List[str], int, int]]`: If `return_indices=False`, only the source code of the object will be
returned. Otherwise, it also returns the whole lines of the file where the object specified by `object_name` is
defined, together the start/end indices of the block in the file that defines the object.


def _contains_composite_function_call(self, graphdef: graph_pb2.GraphDef) -> bool:
    return any(map(self._is_composite_function, graphdef.library.function))

Determines if the graph def has composite function call.

Args:
graphdef: A GraphDef object.

Returns:
True if and only if the graph def contains a composite function call.


def _single_shard_restore(file_prefix: tensor_lib.Tensor, shardable_tensors: Sequence[sharding_util.ShardableTensor], options: 'checkpoint_options.CheckpointOptions | None'=None) -> sharding_util.Shard:
    options = options or checkpoint_options.CheckpointOptions()
    tensor_names = []
    tensor_dtypes = []
    slice_specs = []
    for shardable_tensor in shardable_tensors:
        if shardable_tensor._tensor_save_spec:
            name = shardable_tensor._tensor_save_spec.name
            spec = shardable_tensor._tensor_save_spec.slice_spec
        else:
            name, spec = (shardable_tensor.checkpoint_key, shardable_tensor.slice_spec)
        tensor_names.append(name)
        slice_specs.append(spec)
        tensor_dtypes.append(shardable_tensor.dtype)
    restore_device = options.experimental_io_device or 'cpu:0'
    with ops.device(restore_device):
        restored_tensors = io_ops.restore_v2(file_prefix, tensor_names, slice_specs, tensor_dtypes)
    restored_tensor_dict = {}
    for shardable_tensor in shardable_tensors:
        restored_tensor = restored_tensors.pop(0)
        restored_tensor_dict.setdefault(shardable_tensor.checkpoint_key, {})[shardable_tensor.slice_spec] = restored_tensor
    return restored_tensor_dict

Restore the saveable objects from a checkpoint with `file_prefix`.

Args:
file_prefix: A string or scalar string Tensor containing the prefix for
files to read from.
shardable_tensors: A list of ShardableTensors to restore.
options: Optional `CheckpointOptions` object.

Returns:
A restored tensor dict (maps checkpoint_key -> slice_spec -> tensor).


def ensure_list_size(list_, size_):
    lendiff = (size_ - len(list_))
    if (lendiff > 0):
        extension = [None for _ in range(lendiff)]
        list_.extend(extension)

Allocates more space if needbe.

Ensures len(``list_``) == ``size_``.

Args:
list_ (list): ``list`` to extend
size_ (int): amount to exent by


def invert_apply(self, pts: torch.Tensor) -> torch.Tensor:
    rot_mats = self.get_rot_mats()
    inv_rot_mats = invert_rot_mat(rot_mats)
    return rot_vec_mul(inv_rot_mats, pts)

The inverse of the apply() method.

Args:
pts:
A [*, 3] set of points
Returns:
[*, 3] inverse-rotated points


def shape(self):
    return self._ragged_shape._to_tensor_shape()

The static shape of this StructuredTensor.

The returned `TensorShape` is guaranteed to have a known rank, but the
individual dimension sizes may be unknown.

Returns:
`tf.TensorShape`


def get(self, catID, includeRelationships=False):
    url = ('%(base_url)s/record/%(catID)s' % {'base_url': self.base_url, 'catID': catID})
    r = self.gbdx_connection.get(url)
    r.raise_for_status()
    return r.json()

Retrieves the strip footprint WKT string given a cat ID.

Args:
catID (str): The source catalog ID from the platform catalog.
includeRelationships (bool): whether to include graph links to related objects.  Default False.

Returns:
record (dict): A dict object identical to the json representation of the catalog record


def _combine_handle_data(handle, initial_value):
    assert handle.dtype == dtypes.resource
    variable_handle_data = get_eager_safe_handle_data(handle)
    if initial_value.dtype != dtypes.variant:
        return variable_handle_data
    extra_handle_data = get_eager_safe_handle_data(initial_value)
    if extra_handle_data is not None and extra_handle_data.is_set:
        if variable_handle_data is None or not variable_handle_data.is_set or len(variable_handle_data.shape_and_type) != 1:
            raise RuntimeError(f"Expected VarHandleOp to return a length==1 shape_and_type, but saw: '{variable_handle_data}'")
        variable_handle_data.shape_and_type.extend(extra_handle_data.shape_and_type)
    return variable_handle_data

Concats HandleData from tensors `handle` and `initial_value`.

Args:
handle: A `Tensor` of dtype `resource`.
initial_value: A `Tensor`.

Returns:
A `CppShapeInferenceResult.HandleData`.  If `initial_value` has dtype
`variant`, the `HandleData` contains the concatenation of the shape_and_type
from both `handle` and `initial_value`.

Raises:
RuntimeError: If handle, which was returned by VarHandleOp, either has
no handle data, or its len(handle_data.shape_and_type) != 1.


def _GetAnalysisPlugins(self, analysis_plugins_string):

    if not analysis_plugins_string:
      return []

    analysis_plugins_list = [
        name.strip() for name in analysis_plugins_string.split(',')]

    analysis_plugins = self._analysis_manager.GetPluginObjects(
        analysis_plugins_list)
    return analysis_plugins.values()

Retrieves analysis plugins.

Args:
analysis_plugins_string (str): comma separated names of analysis plugins
to enable.

Returns:
list[AnalysisPlugin]: analysis plugins.


def _get_job_metadata(provider, user_id, job_name, script, task_ids,
                      user_project, unique_job_id):

  create_time = dsub_util.replace_timezone(datetime.datetime.now(), tzlocal())
  user_id = user_id or dsub_util.get_os_user()
  job_metadata = provider.prepare_job_metadata(script.name, job_name, user_id,
                                               create_time)
  if unique_job_id:
    job_metadata['job-id'] = uuid.uuid4().hex

  job_metadata['create-time'] = create_time
  job_metadata['script'] = script
  job_metadata['user-project'] = user_project
  if task_ids:
    job_metadata['task-ids'] = dsub_util.compact_interval_string(list(task_ids))

  return job_metadata

Allow provider to extract job-specific metadata from command-line args.

Args:
provider: job service provider
user_id: user submitting the job
job_name: name for the job
script: the script to run
task_ids: a set of the task-ids for all tasks in the job
user_project: name of the project to be billed for the request
unique_job_id: generate a unique job id

Returns:
A dictionary of job-specific metadata (such as job id, name, etc.)


def histogram(namespace: Union[Type, str], name: str, bucket_type: 'BucketType', logger: Optional['MetricLogger']=None) -> 'Metrics.DelegatingHistogram':
    namespace = UserMetrics.get_namespace(namespace)
    return Metrics.DelegatingHistogram(MetricName(namespace, name), bucket_type, logger)

Obtains or creates a Histogram metric.

Args:
namespace: A class or string that gives the namespace to a metric
name: A string that gives a unique name to a metric
bucket_type: A type of bucket used in a histogram. A subclass of
apache_beam.utils.histogram.BucketType
logger: MetricLogger for logging locally aggregated metric

Returns:
A Histogram object.


def view_structure(self, only_chains=None, opacity=1.0, recolor=False, gui=False):
    if ssbio.utils.is_ipynb():
        import nglview as nv
    else:
        raise EnvironmentError('Unable to display structure - not running in a Jupyter notebook environment')
    if (not self.structure_file):
        raise ValueError('Structure file not loaded')
    only_chains = ssbio.utils.force_list(only_chains)
    to_show_chains = '( '
    for c in only_chains:
        to_show_chains += ':{} or'.format(c)
    to_show_chains = to_show_chains.strip(' or ')
    to_show_chains += ' )'
    if ((self.file_type == 'mmtf') or (self.file_type == 'mmtf.gz')):
        view = nv.NGLWidget()
        view.add_component(self.structure_path)
    else:
        view = nv.show_structure_file(self.structure_path, gui=gui)
    if recolor:
        view.clear_representations()
        if only_chains:
            view.add_cartoon(selection='{} and (not hydrogen)'.format(to_show_chains), color='silver', opacity=opacity)
        else:
            view.add_cartoon(selection='protein', color='silver', opacity=opacity)
    elif only_chains:
        view.clear_representations()
        view.add_cartoon(selection='{} and (not hydrogen)'.format(to_show_chains), color='silver', opacity=opacity)
    return view

Use NGLviewer to display a structure in a Jupyter notebook

Args:
only_chains (str, list): Chain ID or IDs to display
opacity (float): Opacity of the structure
recolor (bool): If structure should be cleaned and recolored to silver
gui (bool): If the NGLview GUI should show up

Returns:
NGLviewer object


def _from_signer_and_info(cls, signer, info, **kwargs):

        return cls(
            signer,
            service_account_email=info['client_email'],
            token_uri=info['token_uri'],
            project_id=info.get('project_id'), **kwargs)

Creates a Credentials instance from a signer and service account
info.

Args:
signer (google.auth.crypt.Signer): The signer used to sign JWTs.
info (Mapping[str, str]): The service account info.
kwargs: Additional arguments to pass to the constructor.

Returns:
google.auth.jwt.Credentials: The constructed credentials.

Raises:
ValueError: If the info is not in the expected format.


def get_atlas_per_gene_mutation_df(self, gene_id):
    g = self.reference_gempro.genes.get_by_id(gene_id)
    (single, fingerprint) = g.protein.sequence_mutation_summary(alignment_type='seqalign')
    structure_type_suffix = 'NA'
    appender = []
    for (k, strains) in single.items():
        to_append = {}
        orig_res = k[0]
        resnum = int(k[1])
        mutated_res = k[2]
        num_strains_mutated = len(strains)
        strain_ids = [str(x.split((g.id + '_'))[1]) for x in strains]
        to_append['ref_residue'] = orig_res
        to_append['ref_resnum'] = resnum
        to_append['strain_residue'] = mutated_res
        to_append['num_strains_mutated'] = num_strains_mutated
        to_append['strains_mutated'] = ';'.join(strain_ids)
        to_append['at_disulfide_bridge'] = False
        origres_props = ssbio.protein.sequence.properties.residues.residue_biochemical_definition(orig_res)
        mutres_props = ssbio.protein.sequence.properties.residues.residue_biochemical_definition(mutated_res)
        to_append['ref_residue_prop'] = origres_props
        to_append['strain_residue_prop'] = mutres_props
        (grantham_s, grantham_txt) = ssbio.protein.sequence.properties.residues.grantham_score(orig_res, mutated_res)
        to_append['grantham_score'] = grantham_s
        to_append['grantham_annotation'] = grantham_txt
        to_append.update(g.protein.get_residue_annotations(seq_resnum=resnum, use_representatives=True))
        if g.protein.representative_structure:
            if g.protein.representative_structure.is_experimental:
                to_append['structure_type'] = 'EXP'
            else:
                to_append['structure_type'] = 'HOM'
            repchain = g.protein.representative_chain
            repchain_annotations = g.protein.representative_structure.chains.get_by_id(repchain).seq_record.annotations
            if ('SSBOND-biopython' in repchain_annotations):
                structure_resnum = g.protein.map_seqprop_resnums_to_structprop_resnums(resnums=resnum, use_representatives=True)
                if (resnum in structure_resnum):
                    ssbonds = repchain_annotations['SSBOND-biopython']
                    ssbonds_res = []
                    for x in ssbonds:
                        ssbonds_res.append(x[0])
                        ssbonds_res.append(x[1])
                    if (structure_resnum in ssbonds_res):
                        to_append['at_disulfide_bridge'] = True
        appender.append(to_append)
    if (not appender):
        return pd.DataFrame()
    cols = ['ref_residue', 'ref_resnum', 'strain_residue', 'num_strains_mutated', 'strains_mutated', 'ref_residue_prop', 'strain_residue_prop', 'grantham_score', 'grantham_annotation', 'at_disulfide_bridge', 'seq_SS-sspro', 'seq_SS-sspro8', 'seq_RSA-accpro', 'seq_RSA-accpro20', 'seq_TM-tmhmm', 'struct_SS-dssp', 'struct_RSA-dssp', 'struct_ASA-dssp', 'struct_CA_DEPTH-msms', 'struct_RES_DEPTH-msms', 'struct_PHI-dssp', 'struct_PSI-dssp', 'struct_resnum', 'struct_residuestrains_mutated']
    df_gene_summary = pd.DataFrame.from_records(appender, columns=cols)
    df_gene_summary.dropna(axis=1, how='all', inplace=True)
    df_gene_summary.sort_values(by='ref_resnum', inplace=True)
    df_gene_summary = df_gene_summary.set_index('ref_resnum')
    return df_gene_summary

Create a single data frame which summarizes a gene and its mutations.

Args:
gene_id (str): Gene ID in the base model

Returns:
DataFrame: Pandas DataFrame of the results


def compose_tree_url(tree, issn_url=False):
    url = compose_tree_path(tree, issn_url)
    if (WEB_PORT == 80):
        return ('%s:
    return ('%s:

Compose full url for given `tree`, with protocol, server's address and
port.

Args:
tree (obj): :class:`.Tree` instance.
issn_url (bool, default False): Compose URL using ISSN.

Returns:
str: URL of the tree


def get_channel(self, chan_name, coll_name, exp_name):

        chan = ChannelResource(chan_name, coll_name, exp_name)
        return self.get_project(chan)

Helper that gets a fully initialized ChannelResource for an *existing* channel.

Args:
chan_name (str): Name of channel.
coll_name (str): Name of channel's collection.
exp_name (str): Name of channel's experiment.

Returns:
(intern.resource.boss.ChannelResource)


def FindFileByName(self, file_name):

    try:
      return self._file_descriptors[file_name]
    except KeyError:
      pass

    try:
      file_proto = self._internal_db.FindFileByName(file_name)
    except KeyError as error:
      if self._descriptor_db:
        file_proto = self._descriptor_db.FindFileByName(file_name)
      else:
        raise error
    if not file_proto:
      raise KeyError('Cannot find a file named %s' % file_name)
    return self._ConvertFileProtoToFileDescriptor(file_proto)

Gets a FileDescriptor by file name.

Args:
file_name: The path to the file to get a descriptor for.

Returns:
A FileDescriptor for the named file.

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


def _create_trial_info(self, expr_dir):

        meta = self._build_trial_meta(expr_dir)

        self.logger.debug("Create trial for %s" % meta)

        trial_record = TrialRecord.from_json(meta)
        trial_record.save()

Create information for given trial.

Meta file will be loaded if exists, and the trial information
will be saved in db backend.

Args:
expr_dir (str): Directory path of the experiment.


def bbox_line_intersect(nodes, line_start, line_end):
    (left, right, bottom, top) = _helpers.bbox(nodes)
    if (_helpers.in_interval(line_start[0], left, right) and _helpers.in_interval(line_start[1], bottom, top)):
        return BoxIntersectionType.INTERSECTION
    if (_helpers.in_interval(line_end[0], left, right) and _helpers.in_interval(line_end[1], bottom, top)):
        return BoxIntersectionType.INTERSECTION
    (s_bottom, t_bottom, success) = segment_intersection(np.asfortranarray([left, bottom]), np.asfortranarray([right, bottom]), line_start, line_end)
    if (success and _helpers.in_interval(s_bottom, 0.0, 1.0) and _helpers.in_interval(t_bottom, 0.0, 1.0)):
        return BoxIntersectionType.INTERSECTION
    (s_right, t_right, success) = segment_intersection(np.asfortranarray([right, bottom]), np.asfortranarray([right, top]), line_start, line_end)
    if (success and _helpers.in_interval(s_right, 0.0, 1.0) and _helpers.in_interval(t_right, 0.0, 1.0)):
        return BoxIntersectionType.INTERSECTION
    (s_top, t_top, success) = segment_intersection(np.asfortranarray([right, top]), np.asfortranarray([left, top]), line_start, line_end)
    if (success and _helpers.in_interval(s_top, 0.0, 1.0) and _helpers.in_interval(t_top, 0.0, 1.0)):
        return BoxIntersectionType.INTERSECTION
    return BoxIntersectionType.DISJOINT

r"""Determine intersection of a bounding box and a line.

We do this by first checking if either the start or end node of the
segment are contained in the bounding box. If they aren't, then
checks if the line segment intersects any of the four sides of the
bounding box.

.. note::

This function is "half-finished". It makes no distinction between
"tangent" intersections of the box and segment and other types
of intersection. However, the distinction is worthwhile, so this
function should be "upgraded" at some point.

Args:
nodes (numpy.ndarray): Points (``2 x N``) that determine a
bounding box.
line_start (numpy.ndarray): Beginning of a line segment (1D
``2``-array).
line_end (numpy.ndarray): End of a line segment (1D ``2``-array).

Returns:
int: Enum from ``BoxIntersectionType`` indicating the type of
bounding box intersection.


def allocate(self, amount, child=None, update=True):
    if (child is not None):
        if (child not in self.children):
            c = SecurityBase(child)
            c.setup(self._universe)
            c.update(self.now)
            self._add_child(c)
        self.children[child].allocate(amount)
    else:
        if (self.parent == self):
            self.parent.adjust((- amount), update=False, flow=True)
        else:
            self.parent.adjust((- amount), update=False, flow=False)
        self.adjust(amount, update=False, flow=True)
        if (self.children is not None):
            [c.allocate((amount * c._weight), update=False) for c in self._childrenv]
        if update:
            self.root.stale = True

Allocate capital to Strategy. By default, capital is allocated
recursively down the children, proportionally to the children's
weights.  If a child is specified, capital will be allocated
to that specific child.

Allocation also have a side-effect. They will deduct the same amount
from the parent's "account" to offset the allocation. If there is
remaining capital after allocation, it will remain in Strategy.

Args:
* amount (float): Amount to allocate.
* child (str): If specified, allocation will be directed to child
only. Specified by name.
* update (bool): Force update.


def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
    local_stream = utils.BytearrayStream()
    if self._unique_identifier:
        self._unique_identifier.write(local_stream, kmip_version=kmip_version)
    if self._cryptographic_parameters:
        self._cryptographic_parameters.write(local_stream, kmip_version=kmip_version)
    if self._data:
        self._data.write(local_stream, kmip_version=kmip_version)
    if self._digested_data:
        self._digested_data.write(local_stream, kmip_version=kmip_version)
    if self._signature_data:
        self._signature_data.write(local_stream, kmip_version=kmip_version)
    if self._correlation_value:
        self._correlation_value.write(local_stream, kmip_version=kmip_version)
    if self._init_indicator:
        self._init_indicator.write(local_stream, kmip_version=kmip_version)
    if self._final_indicator:
        self._final_indicator.write(local_stream, kmip_version=kmip_version)
    self.length = local_stream.length()
    super(SignatureVerifyRequestPayload, self).write(output_stream, kmip_version=kmip_version)
    output_stream.write(local_stream.buffer)

Write the data encoding the SignatureVerify request payload to a
stream.

Args:
output_stream (stream): A data stream in which to encode object
data, supporting a write method; usually a BytearrayStream
object.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 1.0.

Raises:
ValueError: Raised if the data attribute is not defined.


def util_granulate_time_series(time_series, scale):
    n = len(time_series)
    b = int(np.fix((n / scale)))
    temp = np.reshape(time_series[0:(b * scale)], (b, scale))
    cts = np.mean(temp, axis=1)
    return cts

Extract coarse-grained time series

Args:
time_series: Time series
scale: Scale factor

Returns:
Vector of coarse-grained time series with given scale factor


class RunEnsembleDetector(beam.PTransform[beam.PCollection[NestedKeyedInputT], beam.PCollection[NestedKeyedOutputT]]):

    def __init__(self, ensemble_detector: EnsembleAnomalyDetector):
        self._ensemble_detector = ensemble_detector

    def expand(self, input: beam.PCollection[NestedKeyedInputT]) -> beam.PCollection[NestedKeyedOutputT]:
        model_uuid = f'{self._ensemble_detector._model_id}:{uuid.uuid4().hex[:6]}'
        assert self._ensemble_detector._sub_detectors is not None
        if not self._ensemble_detector._sub_detectors:
            raise ValueError(f'No detectors found at {model_uuid}')
        results = []
        for idx, detector in enumerate(self._ensemble_detector._sub_detectors):
            if isinstance(detector, EnsembleAnomalyDetector):
                results.append(input | f'Run Ensemble Detector at index {idx} ({model_uuid})' >> RunEnsembleDetector(detector))
            elif isinstance(detector, OfflineDetector):
                results.append(input | f'Run Offline Detector at index {idx} ({model_uuid})' >> RunOfflineDetector(detector))
            else:
                results.append(input | f'Run One Detector at index {idx} ({model_uuid})' >> RunOneDetector(detector))
        if self._ensemble_detector._aggregation_strategy is None:
            aggregation_type = 'Simple'
        else:
            aggregation_type = 'Custom'
        ret = results | beam.Flatten() | f'Run {aggregation_type} Aggregation Strategy ({model_uuid})' >> RunAggregationStrategy(self._ensemble_detector._aggregation_strategy, self._ensemble_detector._model_id)
        if self._ensemble_detector._threshold_criterion:
            ret = ret | f'Run Threshold Criterion ({model_uuid})' >> RunThresholdCriterion(self._ensemble_detector._threshold_criterion)
        return ret

Runs an ensemble of anomaly detectors on a PCollection of data.

This PTransform applies an `EnsembleAnomalyDetector` to the input data,
running each sub-detector and aggregating the results.

Args:
ensemble_detector: The `EnsembleAnomalyDetector` to run.


def _page_to_title(page):

  start_tag = u"<title>"
  end_tag = u"</title>"
  start_pos = page.find(start_tag)
  end_pos = page.find(end_tag)
  assert start_pos != -1
  assert end_pos != -1
  start_pos += len(start_tag)
  return page[start_pos:end_pos]

Extract the title from a page.

Args:
page: a unicode string
Returns:
a unicode string


def get_course_certificate(self, course_id, username):
    return self.client.certificates(username).courses(course_id).get()

Retrieve the certificate for the given username for the given course_id.

Args:
* ``course_id`` (str): The string value of the course's unique identifier
* ``username`` (str): The username ID identifying the user for which to retrieve the certificate

Raises:

HttpNotFoundError if no certificate found for the given user+course.

Returns:

a dict containing:

* ``username``: A string representation of an user's username passed in the request.
* ``course_id``: A string representation of a Course ID.
* ``certificate_type``: A string representation of the certificate type.
* ``created_date`: Datetime the certificate was created (tz-aware).
* ``status``: A string representation of the certificate status.
* ``is_passing``: True if the certificate has a passing status, False if not.
* ``download_url``: A string representation of the certificate url.
* ``grade``: A string representation of a float for the user's course grade.


def delete(self, *names: str, pipeline=False):

        if pipeline:
            self._pipeline.delete(*names)
        else:
            self._db.delete(*names)

Delete one or more keys specified by names.

Args:
names (str): Names of keys to delete
pipeline (bool): True, start a transaction block. Default false.


def write_to_fp(self, fp):

        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        text_parts = self._tokenize(self.text)
        log.debug("text_parts: %i", len(text_parts))
        assert text_parts, 'No text to send to TTS API'

        for idx, part in enumerate(text_parts):
            try:

                part_tk = self.token.calculate_token(part)
            except requests.exceptions.RequestException as e:
                log.debug(str(e), exc_info=True)
                raise gTTSError(
                    "Connection error during token calculation: %s" %
                    str(e))

            payload = {'ie': 'UTF-8',
                       'q': part,
                       'tl': self.lang,
                       'ttsspeed': self.speed,
                       'total': len(text_parts),
                       'idx': idx,
                       'client': 'tw-ob',
                       'textlen': _len(part),
                       'tk': part_tk}

            log.debug("payload-%i: %s", idx, payload)

            try:

                r = requests.get(self.GOOGLE_TTS_URL,
                                 params=payload,
                                 headers=self.GOOGLE_TTS_HEADERS,
                                 proxies=urllib.request.getproxies(),
                                 verify=False)

                log.debug("headers-%i: %s", idx, r.request.headers)
                log.debug("url-%i: %s", idx, r.request.url)
                log.debug("status-%i: %s", idx, r.status_code)

                r.raise_for_status()
            except requests.exceptions.HTTPError:

                raise gTTSError(tts=self, response=r)
            except requests.exceptions.RequestException as e:

                raise gTTSError(str(e))

            try:

                for chunk in r.iter_content(chunk_size=1024):
                    fp.write(chunk)
                log.debug("part-%i written to %s", idx, fp)
            except (AttributeError, TypeError) as e:
                raise TypeError(
                    "'fp' is not a file-like object or it does not take bytes: %s" %
                    str(e))

Do the TTS API request and write bytes to a file-like object.

Args:
fp (file object): Any file-like object to write the ``mp3`` to.

Raises:
:class:`gTTSError`: When there's an error with the API request.
TypeError: When ``fp`` is not a file-like object that takes bytes.


def parse_default_property_value(property_name, property_type_id, default_value_string):
    if ((property_type_id == PROPERTY_TYPE_EMBEDDED_SET_ID) and (default_value_string == '{}')):
        return set()
    elif ((property_type_id == PROPERTY_TYPE_EMBEDDED_LIST_ID) and (default_value_string == '[]')):
        return list()
    elif ((property_type_id == PROPERTY_TYPE_STRING_ID) and isinstance(default_value_string, six.string_types)):
        return default_value_string
    elif (property_type_id == PROPERTY_TYPE_BOOLEAN_ID):
        return _parse_bool_default_value(property_name, default_value_string)
    elif (property_type_id == PROPERTY_TYPE_DATETIME_ID):
        return _parse_datetime_default_value(property_name, default_value_string)
    elif (property_type_id == PROPERTY_TYPE_DATE_ID):
        return _parse_date_default_value(property_name, default_value_string)
    else:
        raise AssertionError(u'Unsupported default value for property "{}" with type id {}: {}'.format(property_name, property_type_id, default_value_string))

Parse the default value string into its proper form given the property type ID.

Args:
property_name: string, the name of the property whose default value is being parsed.
Used primarily to construct meaningful error messages, should the default
value prove invalid.
property_type_id: int, one of the property type ID constants defined in this file that
OrientDB uses to designate the native type of a given property.
default_value_string: string, the textual representation of the default value for
for the property, as returned by OrientDB's schema introspection code.

Returns:
an object of type matching the property that can be used as the property's default value.
For example, if the property is of string type, the return type will be a string, and if
the property is of list type, the return type will be a list.

Raises:
AssertionError, if the default value is not supported or does not match the
property's declared type (e.g. if a default of "[]" is set on an integer property).


def image_preprocessing(image_buffer, bbox, train, thread_id=0):
    if (bbox is None):
        raise ValueError('Please supply a bounding box.')
    image = decode_jpeg(image_buffer)
    height = FLAGS.image_size
    width = FLAGS.image_size
    if train:
        image = distort_image(image, height, width, bbox, thread_id)
    else:
        image = eval_image(image, height, width)
    image = tf.subtract(image, 0.5)
    image = tf.multiply(image, 2.0)
    return image

Decode and preprocess one image for evaluation or training.

Args:
image_buffer: JPEG encoded string Tensor
bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords]
where each coordinate is [0, 1) and the coordinates are arranged as
[ymin, xmin, ymax, xmax].
train: boolean
thread_id: integer indicating preprocessing thread

Returns:
3-D float Tensor containing an appropriately scaled image

Raises:
ValueError: if user does not provide bounding box


def camel_to_title(name):
    split = re.findall('[A-Z]?[a-z0-9]+|[A-Z]+(?=[A-Z]|$)', name)
    ret = ' '.join(split)
    ret = (ret[0].upper() + ret[1:])
    return ret

Takes a camelCaseFieldName and returns an Title Case Field Name

Args:
name (str): E.g. camelCaseFieldName

Returns:
str: Title Case converted name. E.g. Camel Case Field Name


def sorted(field_name, ascending=True, fields=None, count=5):

    if field_name is None:
      raise Exception('Sort field must be specified')
    direction = '' if ascending else ' DESC'
    projection = Sampling._create_projection(fields)
    return lambda sql: 'SELECT %s FROM (%s) ORDER BY %s%s LIMIT %d' % (projection, sql, field_name,
                                                                       direction, count)

Provides a sampling strategy that picks from an ordered set of rows.

Args:
field_name: the name of the field to sort the rows by.
ascending: whether to sort in ascending direction or not.
fields: an optional list of field names to retrieve.
count: optional number of rows to limit the sampled results to.
Returns:
A sampling function that can be applied to get the initial few rows.


def process_cgmlst_results(df):
    assert isinstance(df, pd.DataFrame)
    markers = []
    alleles = []
    for x in df['qseqid']:
        (marker, allele) = x.split('|')
        markers.append(marker)
        alleles.append(int(allele))
    df.loc[(:, 'marker')] = markers
    df.loc[(:, 'allele')] = alleles
    df.loc[(:, 'is_match')] = (((df['coverage'] >= 1.0) & (df['pident'] >= 90.0)) & (~ df['is_trunc']))
    df.loc[(:, 'allele_name')] = df.apply((lambda x: allele_name(x.sseq.replace('-', ''))), axis=1)
    df.loc[(:, 'is_perfect')] = ((df['coverage'] == 1.0) & (df['pident'] == 100.0))
    df_perf = df[df['is_perfect']]
    perf_markers = df_perf['marker'].unique()
    df.loc[(:, 'has_perfect_match')] = df['marker'].isin(perf_markers)
    (start_idxs, end_idxs, needs_revcomps, trunc, is_extended) = extend_subj_match_vec(df)
    df.loc[(:, 'start_idx')] = start_idxs
    df.loc[(:, 'end_idx')] = end_idxs
    df.loc[(:, 'needs_revcomp')] = needs_revcomps
    df.loc[(:, 'trunc')] = trunc
    df.loc[(:, 'is_extended')] = is_extended
    df.loc[(:, 'sseq_msa_gaps')] = np.zeros(df.shape[0], dtype=np.int64)
    df.loc[(:, 'sseq_msa_p_gaps')] = np.zeros(df.shape[0], dtype=np.float64)
    df.loc[(:, 'too_many_gaps')] = trunc
    return df

Append informative fields to cgMLST330 BLAST results DataFrame

The `qseqid` column must contain cgMLST330 query IDs with `{marker name}|{allele number}` format.
The `qseqid` parsed allele numbers and marker names are appended as new fields.

`is_perfect` column contains boolean values for whether an allele result is 100% identity and coverage.
`has_perfect_match` denotes if a cgMLST330 marker has a perfect allele match.
The top result with the largest bitscore for a marker with no perfect match is used to retrieve the allele present
at that marker locus.

Args:
df (pandas.DataFrame): DataFrame of cgMLST330 BLAST results

Returns:
pandas.DataFrame: cgMLST330 BLAST results DataFrame with extra fields (`marker`, `allele`, `is_perfect`, `has_perfect_match`)


def metar_to_speech(metar: str) -> str:
    LOGGER.info('getting speech text from METAR: %s', metar)
    (metar_data, metar_units) = emiz.avwx.metar.parse_in(metar)
    speech = emiz.avwx.speech.metar(metar_data, metar_units)
    speech = str(speech).replace('Altimeter', 'Q N H')
    LOGGER.debug('resulting speech: %s', speech)
    return speech

Creates a speakable text from a METAR

Args:
metar: METAR string to use

Returns: speakable METAR for TTS


def title_of_design_condition(self, value=None):

        if value is not None:
            try:
                value = str(value)
            except ValueError:
                raise ValueError(
                    'value {} need to be of type str '
                    'for field `title_of_design_condition`'.format(value))
            if ',' in value:
                raise ValueError('value should not contain a comma '
                                 'for field `title_of_design_condition`')

        self._title_of_design_condition = value

Corresponds to IDD Field `title_of_design_condition`

Args:
value (str): value for IDD Field `title_of_design_condition`
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def __init__(self, context=None):

    if context is None:
      context = google.datalab.Context.default()
    self._context = context
    self._api = _api.Api(context)
    self._project_id = context.project_id if context else self._api.project_id

Initializes an instance of a BucketList.

Args:
context: an optional Context object providing project_id and credentials. If a specific
project id or credentials are unspecified, the default ones configured at the global
level are used.


def _MergeOptional(self, a, b):

    if a and b:
      if a != b:
        raise MergeError("values must be identical if both specified "
                         "('%s' vs '%s')" % (transitfeed.EncodeUnicode(a),
                                             transitfeed.EncodeUnicode(b)))
    return a or b

Tries to merge two values which may be None.

If both values are not None, they are required to be the same and the
merge is trivial. If one of the values is None and the other is not None,
the merge results in the one which is not None. If both are None, the merge
results in None.

Args:
a: The first value.
b: The second value.

Returns:
The merged value.

Raises:
MergeError: If both values are not None and are not the same.


def list_events(self):
    event_dir_dict = collections.defaultdict(set)
    for event_file in self._glob_events_files(self._paths, recursive=True):
        dir = os.path.dirname(event_file)
        try:
            for record in tf_record.tf_record_iterator(event_file):
                event = event_pb2.Event.FromString(record)
                if ((event.summary is None) or (event.summary.value is None)):
                    continue
                for value in event.summary.value:
                    if ((value.simple_value is None) or (value.tag is None)):
                        continue
                    event_dir_dict[value.tag].add(dir)
        except tf.errors.DataLossError:
            continue
    return dict(event_dir_dict)

List all scalar events in the directory.

Returns:
A dictionary. Key is the name of a event. Value is a set of dirs that contain that event.


def get_options_from_str(obj_str, **kwargs):

    if isinstance(obj_str, list):
        return obj_str
    try:
        obj = get_obj_frm_str(obj_str, **kwargs)
        if obj:
            return list(obj)
    except AttributeError:
        pass
    return []

Returns a list of options from a python object string

args:
obj_str: python list of options or a python object path
Example: "rdfframework.connections.ConnManager[{param1}]"

kwargs:
* kwargs used to format the 'obj_str'


def _set_subject(self, subject):

    def test_uri(value):
        ' test to see if the value is a uri or bnode\n\n            Returns: Uri or Bnode '
        if (not isinstance(value, (Uri, BlankNode))):
            try:
                if value.startswith('_:'):
                    return BlankNode(value)
                else:
                    return Uri(value)
            except:
                return BlankNode()
        else:
            return value
    if isinstance(subject, dict):
        self.subject = test_uri(subject['s'])
        if isinstance(subject['o'], list):
            for item in subject['o']:
                self.add_property(subject['p'], item)
        else:
            self.add_property(subject['p'], subject['o'])
    else:
        self.subject = test_uri(subject)

sets the subject value for the class instance

Args:
subject(dict, Uri, str): the subject for the class instance


def is_subtype_of(self, other):
    try:
        self.sanity_check_type(other)
        nest.assert_same_structure(self._element_spec, other._element_spec)
    except (TypeError, ValueError):
        return False
    self_elements = nest.flatten(self._element_spec)
    other_elements = nest.flatten(other._element_spec)
    return all((self_element.is_subtype_of(other_element) for self_element, other_element in zip(self_elements, other_elements)))

Returns True if `self` is subtype of `other`.

Args:
other: A `TypeSpec`.


class InstructBlipEncoder(nn.Module):

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

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

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

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

Args:
config (`InstructBlipConfig`):
The corresponding vision configuration for the `InstructBlipEncoder`.


def CheckKeyCompatibility(cls, key_path):
    key_path_upper = key_path.upper()
    for key_path_prefix in cls._COMPATIBLE_REGISTRY_KEY_PATH_PREFIXES:
        if key_path_upper.startswith(key_path_prefix):
            return True
    logger.warning('Key path: "{0:s}" is currently not supported'.format(key_path))
    return False

Checks if a Windows Registry key path is supported by dfWinReg.

Args:
key_path (str): path of the Windows Registry key.

Returns:
bool: True if key is compatible or False if not.


def addFixedEffect(self, F=None, A=None, Ftest=None):

        if A is None:
            A = sp.eye(self.P)
        if F is None:
            F = sp.ones((self.N,1))
            if self.Ntest is not None:
                Ftest = sp.ones((self.Ntest,1))

        assert A.shape[1]==self.P, 'VarianceDecomposition:: A has incompatible shape'
        assert F.shape[0]==self.N, 'VarianceDecimposition:: F has incompatible shape'

        if Ftest is not None:
            assert self.Ntest is not None, 'VarianceDecomposition:: specify Ntest for predictions (method VarianceDecomposition::setTestSampleSize)'
            assert Ftest.shape[0]==self.Ntest, 'VarianceDecimposition:: Ftest has incompatible shape'
            assert Ftest.shape[1]==F.shape[1], 'VarianceDecimposition:: Ftest has incompatible shape'

        self.sample_designs.append(F)
        self.sample_test_designs.append(Ftest)
        self.trait_designs.append(A)

        self._desync()

add fixed effect term to the model

Args:
F:     sample design matrix for the fixed effect [N,K]
A:     trait design matrix for the fixed effect (e.g. sp.ones((1,P)) common effect; sp.eye(P) any effect) [L,P]
Ftest: sample design matrix for test samples [Ntest,K]


def __init__(self, _args):

        super(TcExProfile, self).__init__(_args)

        self._input_permutations = []
        self._output_permutations = []
        self.data_dir = os.path.join(self.args.outdir, 'data')
        self.profile_dir = os.path.join(self.args.outdir, 'profiles')
        self.profiles = {}

Initialize Class properties.

Args:
_args (namespace): The argparser args Namespace.


def get(self, uid: int) -> FrozenSet[Flag]:
    recent = (_recent_set if (uid in self._recent) else frozenset())
    flags = self._flags.get(uid)
    return (recent if (flags is None) else (flags | recent))

Return the session flags for the mailbox session.

Args:
uid: The message UID value.


def filter(self, limit=None, to=None, category=None):

        if category and not to:
            msg_slice = itertools.islice((x for x in self.store if x[2] == category), limit)
        elif to and not category:
            to = JID.fromstr(to)
            msg_slice = itertools.islice((x for x in self.store if _agent_in_msg(to, x[1])), limit)
        elif to and category:
            to = JID.fromstr(to)
            msg_slice = itertools.islice((x for x in self.store if _agent_in_msg(to, x[1]) and x[2] == category), limit)
        else:
            msg_slice = self.all(limit=limit)
            return msg_slice

        return list(msg_slice)[::-1]

Returns the events that match the filters

Args:
limit (int, optional): the max length of the events to return (Default value = None)
to (str, optional): only events that have been sent or received by 'to' (Default value = None)
category (str, optional): only events belonging to the category (Default value = None)

Returns:
list: a list of filtered events


def _save_tensor_value_to_cache_op(self, cache_idx, updates, graph):
    updates = self._merge_tensor_signatures(updates)
    updates = array_ops.reshape(updates, [1, self._num_signature_dimensions()])
    indices = constant_op.constant([cache_idx])
    cache = self._create_or_get_tensor_values_cache(_TT_SUMMARY_TAG, graph)
    return state_ops.scatter_update(cache, indices, updates).op

Returns an op that will save the given updates to an entry in the cache.

Args:
cache_idx: The cache index of the tensor within the cache.
updates: A dictionary of the signature updates.
graph: A TensorFlow graph.
Returns:
Cache update operation.


def forward_loss(self, pixel_values, pred, mask, interpolate_pos_encoding: bool=False):
    target = self.patchify(pixel_values, interpolate_pos_encoding=interpolate_pos_encoding)
    if self.config.norm_pix_loss:
        mean = tf.reduce_mean(target, axis=-1, keepdims=True)
        var = tf.math.reduce_variance(target, axis=-1, keepdims=True)
        target = (target - mean) / (var + 1e-06) ** 0.5
    loss = (pred - target) ** 2
    loss = tf.reduce_mean(loss, axis=-1)
    loss = tf.reduce_sum(loss * mask) / tf.reduce_sum(mask)
    loss = tf.reshape(loss, (1,))
    return loss

Args:
pixel_values (`tf.Tensor` of shape `(batch_size, height, width, num_channels)`):
Pixel values.
pred (`tf.Tensor` of shape `(batch_size, num_patches, patch_size**2 * num_channels)`:
Predicted pixel values.
mask (`tf.Tensor` of shape `(batch_size, sequence_length)`):
Tensor indicating which patches are masked (1) and which are not (0).
interpolate_pos_encoding (`bool`, *optional*, default `False`):
interpolation flag passed during the forward pass.

Returns:
`tf.Tensor`: Pixel reconstruction loss.


def add_vlan_int(self, vlan_id):
    config = ET.Element('config')
    vlinterface = ET.SubElement(config, 'interface-vlan', xmlns='urn:brocade.com:mgmt:brocade-interface')
    interface = ET.SubElement(vlinterface, 'interface')
    vlan = ET.SubElement(interface, 'vlan')
    name = ET.SubElement(vlan, 'name')
    name.text = vlan_id
    try:
        self._callback(config)
        return True
    except Exception as error:
        logging.error(error)
        return False

Add VLAN Interface. VLAN interfaces are required for VLANs even when
not wanting to use the interface for any L3 features.

Args:
vlan_id: ID for the VLAN interface being created. Value of 2-4096.

Returns:
True if command completes successfully or False if not.

Raises:
None


def get_num_bytes(self, batch: Sequence[scipy.sparse.csr_matrix]) -> int:
    return sum((sys.getsizeof(element) for element in batch))

Returns:
The number of bytes of data for a batch.


def change_wavelength(self, wavelength):

        for axis in self.axes:
            if issubclass(type(axis), Slabs):
                axis.change_wavelength(wavelength)
        self.xx, self.xy, self.yx, self.yy, self.zz = self.axes
        self._wl = wavelength

Changes the wavelength of the structure.

This will affect the mode solver and potentially
the refractive indices used (provided functions
were provided as refractive indices).

Args:
wavelength (float): The new wavelength.


def setup_components_and_tf_funcs(self, custom_getter=None):

        if custom_getter is None:
            def custom_getter(getter, name, registered=False, **kwargs):

                if registered:
                    self.registered_variables.add(name)
                elif name in self.registered_variables:
                    registered = True

                variable = getter(name=name, **kwargs)
                if registered:
                    pass
                elif name in self.all_variables:
                    assert variable is self.all_variables[name]
                    if kwargs.get('trainable', True):
                        assert variable is self.variables[name]
                        if 'variables' in self.summary_labels:
                            tf.contrib.summary.histogram(name=name, tensor=variable)
                else:
                    self.all_variables[name] = variable
                    if kwargs.get('trainable', True):
                        self.variables[name] = variable
                        if 'variables' in self.summary_labels:
                            tf.contrib.summary.histogram(name=name, tensor=variable)
                return variable

        self.fn_initialize = tf.make_template(
            name_='initialize',
            func_=self.tf_initialize,
            custom_getter_=custom_getter
        )
        self.fn_preprocess = tf.make_template(
            name_='preprocess',
            func_=self.tf_preprocess,
            custom_getter_=custom_getter
        )
        self.fn_actions_and_internals = tf.make_template(
            name_='actions-and-internals',
            func_=self.tf_actions_and_internals,
            custom_getter_=custom_getter
        )
        self.fn_observe_timestep = tf.make_template(
            name_='observe-timestep',
            func_=self.tf_observe_timestep,
            custom_getter_=custom_getter
        )
        self.fn_action_exploration = tf.make_template(
            name_='action-exploration',
            func_=self.tf_action_exploration,
            custom_getter_=custom_getter
        )

        return custom_getter

Allows child models to create model's component objects, such as optimizer(s), memory(s), etc..
Creates all tensorflow functions via tf.make_template calls on all the class' "tf_"-methods.

Args:
custom_getter: The `custom_getter_` object to use for `tf.make_template` when creating TensorFlow functions.
If None, use a default custom_getter_.

Returns: The custom_getter passed in (or a default one if custom_getter was None).


def onTagDel(self, name, func):

        if '*' in name:
            self.ontagdelglobs.add(name, func)
        else:
            self.ontagdels[name].append(func)

Register a callback for tag deletion.

Args:
name (str): The name of the tag or tag glob.
func (function): The callback func(node, tagname, tagval).


def get_course_and_course_run(self, course_run_id):

        course_id = parse_course_key(course_run_id)

        course = self.get_course_details(course_id)

        course_run = None
        if course:

            course_run = None
            course_runs = [course_run for course_run in course['course_runs'] if course_run['key'] == course_run_id]
            if course_runs:
                course_run = course_runs[0]

        return course, course_run

Return the course and course run metadata for the given course run ID.

Arguments:
course_run_id (str): The course run ID.

Returns:
tuple: The course metadata and the course run metadata.


def _find_penultimate_layer(model, layer_idx, penultimate_layer_idx):

    if penultimate_layer_idx is None:
        for idx, layer in utils.reverse_enumerate(model.layers[:layer_idx - 1]):
            if isinstance(layer, Wrapper):
                layer = layer.layer
            if isinstance(layer, (_Conv, _Pooling1D, _Pooling2D, _Pooling3D)):
                penultimate_layer_idx = idx
                break

    if penultimate_layer_idx is None:
        raise ValueError('Unable to determine penultimate `Conv` or `Pooling` '
                         'layer for layer_idx: {}'.format(layer_idx))

    if layer_idx < 0:
        layer_idx = len(model.layers) + layer_idx
    if penultimate_layer_idx > layer_idx:
        raise ValueError('`penultimate_layer_idx` needs to be before `layer_idx`')

    return model.layers[penultimate_layer_idx]

Searches for the nearest penultimate `Conv` or `Pooling` layer.

Args:
model: The `keras.models.Model` instance.
layer_idx: The layer index within `model.layers`.
penultimate_layer_idx: The pre-layer to `layer_idx`. If set to None, the nearest penultimate
`Conv` or `Pooling` layer is used.

Returns:
The penultimate layer.


def from_text_vision_configs(cls, text_config: Siglip2TextConfig, vision_config: Siglip2VisionConfig, **kwargs):
    return cls(text_config=text_config.to_dict(), vision_config=vision_config.to_dict(), **kwargs)

Instantiate a [`Siglip2Config`] (or a derived class) from siglip2 text model configuration and siglip2 vision
model configuration.

Returns:
[`Siglip2Config`]: An instance of a configuration object


def grab_data(self, f_start=None, f_stop=None, t_start=None, t_stop=None, if_id=0):
    self.freqs = self.populate_freqs()
    self.timestamps = self.populate_timestamps()
    if (f_start is None):
        f_start = self.freqs[0]
    if (f_stop is None):
        f_stop = self.freqs[(- 1)]
    i0 = np.argmin(np.abs((self.freqs - f_start)))
    i1 = np.argmin(np.abs((self.freqs - f_stop)))
    if (i0 < i1):
        plot_f = self.freqs[i0:(i1 + 1)]
        plot_data = np.squeeze(self.data[(t_start:t_stop, ..., i0:(i1 + 1))])
    else:
        plot_f = self.freqs[i1:(i0 + 1)]
        plot_data = np.squeeze(self.data[(t_start:t_stop, ..., i1:(i0 + 1))])
    return (plot_f, plot_data)

Extract a portion of data by frequency range.

Args:
f_start (float): start frequency in MHz
f_stop (float): stop frequency in MHz
if_id (int): IF input identification (req. when multiple IFs in file)

Returns:
(freqs, data) (np.arrays): frequency axis in MHz and data subset


def _ConvertAttributeValueToDict(cls, attribute_value):
    if isinstance(attribute_value, py2to3.BYTES_TYPE):
        encoded_value = binascii.b2a_qp(attribute_value)
        encoded_value = codecs.decode(encoded_value, 'ascii')
        attribute_value = {'__type__': 'bytes', 'stream': '{0:s}'.format(encoded_value)}
    elif isinstance(attribute_value, (list, tuple)):
        json_list = []
        for list_element in attribute_value:
            json_dict = cls._ConvertAttributeValueToDict(list_element)
            json_list.append(json_dict)
        if isinstance(attribute_value, list):
            attribute_value = json_list
        else:
            attribute_value = {'__type__': 'tuple', 'values': json_list}
    elif isinstance(attribute_value, collections.Counter):
        attribute_value = cls._ConvertCollectionsCounterToDict(attribute_value)
    elif isinstance(attribute_value, dfvfs_path_spec.PathSpec):
        attribute_value = cls._ConvertPathSpecToDict(attribute_value)
    elif isinstance(attribute_value, containers_interface.AttributeContainer):
        attribute_value = cls._ConvertAttributeContainerToDict(attribute_value)
    return attribute_value

Converts an attribute value into a JSON dictionary.

Args:
attribute_value (object): an attribute value.

Returns:
dict|list: The JSON serialized object which can be a dictionary or a list.


def AddClass(self, class_name, gtfs_class):
    if (class_name in self._class_mapping):
        raise problems.DuplicateMapping(class_name)
    self._class_mapping[class_name] = gtfs_class

Adds an entry to the list of known classes.

Args:
class_name: A string with name through which gtfs_class is to be made
accessible.
gtfs_class: The class to be added.
Raises:
DuplicateMapping if class_name is already present in the class mapping.


def argument_parser(args):
    parser = argparse.ArgumentParser(prog='nagaram', description='Finds Scabble anagrams.', formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False)
    parser.add_argument('-h', '--help', dest='help', action='store_true', default=False)
    parser.add_argument('--sowpods', dest='sowpods', action='store_true', default=False)
    parser.add_argument('--length', '-l', dest='length', action='store_true', default=False)
    parser.add_argument('--starts-with', '-s', dest='starts_with', metavar='chars', default='', nargs=1, type=str)
    parser.add_argument('--ends-with', '-e', dest='ends_with', metavar='chars', default='', nargs=1, type=str)
    parser.add_argument('--version', '-v', action='version', version='Nagaram {0} (Released: {1})'.format(nagaram.__version__, nagaram.__release_date__))
    parser.add_argument(dest='wordlist', metavar='letters to find anagrams with (? for anything, _ for blanks)', nargs=argparse.REMAINDER)
    settings = parser.parse_args(args)
    if settings.help:
        raise SystemExit(nagaram.__doc__.strip())
    if (not settings.wordlist):
        raise SystemExit(parser.print_usage())
    if settings.starts_with:
        settings.starts_with = settings.starts_with[0]
    if settings.ends_with:
        settings.ends_with = settings.ends_with[0]
    return (settings.wordlist, settings.sowpods, settings.length, settings.starts_with, settings.ends_with)

Argparse logic, command line options.

Args:
args: sys.argv[1:], everything passed to the program after its name

Returns:
A tuple of:
a list of words/letters to search
a boolean to declare if we want to use the sowpods words file
a boolean to declare if we want to output anagrams by length
a string of starting characters to find anagrams based on
a string of ending characters to find anagrams based on

Raises:
SystemExit if the user passes invalid arguments, --version or --help


def get_weights(model_hparams, vocab_size, hidden_dim=None):

  if hidden_dim is None:
    hidden_dim = model_hparams.hidden_size
  num_shards = model_hparams.symbol_modality_num_shards
  shards = []
  for i in range(num_shards):
    shard_size = (vocab_size
        1 if i < vocab_size % num_shards else 0)
    var_name = "weights_%d" % i
    shards.append(
        tf.get_variable(
            var_name, [shard_size, hidden_dim],
            initializer=tf.random_normal_initializer(0.0, hidden_dim**-0.5)))
  if num_shards == 1:
    ret = shards[0]
  else:
    ret = tf.concat(shards, 0)

  if not tf.executing_eagerly():
    ret = common_layers.convert_gradient_to_tensor(ret)
  return ret

Create or get concatenated embedding or softmax variable.

Args:
model_hparams: HParams, model hyperparmeters.
vocab_size: int, vocabulary size.
hidden_dim: dim of the variable. Defaults to _model_hparams' hidden_size

Returns:
a list of num_shards Tensors.


def _check_job_status(self, job, desc, status_key_name):
    status = desc[status_key_name]
    status = _STATUS_CODE_TABLE.get(status, status)
    if ((status != 'Completed') and (status != 'Stopped')):
        reason = desc.get('FailureReason', '(No reason provided)')
        job_type = status_key_name.replace('JobStatus', ' job')
        raise ValueError('Error for {} {}: {} Reason: {}'.format(job_type, job, status, reason))

Check to see if the job completed successfully and, if not, construct and
raise a ValueError.

Args:
job (str): The name of the job to check.
desc (dict[str, str]): The result of ``describe_training_job()``.
status_key_name (str): Status key name to check for.

Raises:
ValueError: If the training job fails.


def detect_extracellular_compartment(model):
    extracellular_key = Counter()
    for reaction in model.reactions:
        equation = reaction.equation
        if (equation is None):
            continue
        if (len(equation.compounds) == 1):
            (compound, _) = equation.compounds[0]
            compartment = compound.compartment
            extracellular_key[compartment] += 1
    if (len(extracellular_key) == 0):
        return None
    else:
        (best_key, _) = extracellular_key.most_common(1)[0]
    logger.info('{} is extracellular compartment'.format(best_key))
    return best_key

Detect the identifier for equations with extracellular compartments.

Args:
model: :class:`NativeModel`.


def CheckDefaultLambdaCaptures(filename, clean_lines, linenum, error):
    line = clean_lines.elided[linenum]
    match = Match('^(.*)\\[\\s*(?:=|&[^\\w])', line)
    if match:
        (line, _, pos) = CloseExpression(clean_lines, linenum, len(match.group(1)))
        if ((pos >= 0) and Match('^\\s*[{(]', line[pos:])):
            error(filename, linenum, 'build/c++11', 4, 'Default lambda captures are an unapproved C++ feature.')

Check that default lambda captures are not used.

Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
error: The function to call with any errors found.


def GetNames(cls):
    names = []
    for plugin_class in cls._plugins.values():
        name = getattr(plugin_class, 'ARTIFACT_DEFINITION_NAME', None)
        if name:
            names.append(name)
    return names

Retrieves the names of the registered artifact definitions.

Returns:
list[str]: registered artifact definitions names.


def _WriteFileEntry(self, file_entry, data_stream_name, destination_file):
    source_file_object = file_entry.GetFileObject(data_stream_name=data_stream_name)
    if (not source_file_object):
        return
    try:
        with open(destination_file, 'wb') as destination_file_object:
            source_file_object.seek(0, os.SEEK_SET)
            data = source_file_object.read(self._COPY_BUFFER_SIZE)
            while data:
                destination_file_object.write(data)
                data = source_file_object.read(self._COPY_BUFFER_SIZE)
    finally:
        source_file_object.close()

Writes the contents of the source file entry to a destination file.

Note that this function will overwrite an existing file.

Args:
file_entry (dfvfs.FileEntry): file entry whose content is to be written.
data_stream_name (str): name of the data stream whose content is to be
written.
destination_file (str): path of the destination file.


def pop_callback(self, callback):
    return self.callback_handler.pop_callback(callback)

Remove a callback from the current list of [`~transformers.TrainerCallback`] and returns it.

If the callback is not found, returns `None` (and no error is raised).

Args:
callback (`type` or [`~transformers.TrainerCallback]`):
A [`~transformers.TrainerCallback`] class or an instance of a [`~transformers.TrainerCallback`]. In the
first case, will pop the first member of that class found in the list of callbacks.

Returns:
[`~transformers.TrainerCallback`]: The callback removed, if found.


def log2(value: types.FloatTensor) -> types.FloatTensor:
    return tf.math.log(value) / tf.math.log(tf.constant(2, dtype=value.dtype))

Returns the point-wise base-2 logarithm a given `Tensor`.

```python
import tensorflow as tf
import tf_quant_finance as tff

# Example: Computing the base-2 logarithm of a given vector.

tff.math.qmc.utils.log2(tf.constant([1, 2, 4, 8, 16], dtype=tf.float32))
# ==> tf.Tensor([0., 1., 2., 3., 4.], shape=(5,), dtype=float32)
```

Args:
value: Positive scalar `Tensor` of real values.

Returns:
`Tensor` with the same `shape` and `dtype` as `value` equal to `ln(value) /
ln(2)`.


def _build_job_meta(cls, job_dir):

        meta_file = os.path.join(job_dir, JOB_META_FILE)
        meta = parse_json(meta_file)

        if not meta:
            job_name = job_dir.split("/")[-1]
            user = os.environ.get("USER", None)
            meta = {
                "job_id": job_name,
                "job_name": job_name,
                "user": user,
                "type": "ray",
                "start_time": os.path.getctime(job_dir),
                "end_time": None,
                "best_trial_id": None,
            }

        if meta.get("start_time", None):
            meta["start_time"] = timestamp2date(meta["start_time"])

        return meta

Build meta file for job.

Args:
job_dir (str): Directory path of the job.

Return:
A dict of job meta info.


def aggregate_gradients_using_hierarchical_copy(avail_devices, replica_grads):
    agg_grads = []
    num_devices = len(avail_devices)
    group_size = num_devices
    for i, single_grads in enumerate(zip(*replica_grads)):
        group_0_main_device = i % num_devices
        group_1_main_device = (group_0_main_device + group_size) % num_devices
        if group_0_main_device < group_size:
            group_0_begin = 0
            group_1_begin = group_size
        else:
            group_0_begin = group_size
            group_1_begin = 0
        group_0_device_grads = single_grads[group_0_begin:group_0_begin + group_size]
        with ops.device(avail_devices[group_0_main_device]):
            group_0_agg_grads, _ = aggregate_single_gradient_using_copy(group_0_device_grads, False, False)
        group_1_device_grads = single_grads[group_1_begin:group_1_begin + group_size]
        with ops.device(avail_devices[group_1_main_device]):
            group_1_agg_grads, _ = aggregate_single_gradient_using_copy(group_1_device_grads, False, False)
        with ops.device(avail_devices[group_0_main_device]):
            (agg_total_grads, _), _ = aggregate_single_gradient_using_copy([group_0_agg_grads, group_1_agg_grads], False, False)
        with ops.device(avail_devices[group_0_main_device]):
            group_0_agg_grads_bcast = array_ops.identity(agg_total_grads)
        with ops.device(avail_devices[group_1_main_device]):
            group_1_agg_grads_bcast = array_ops.identity(agg_total_grads)
        agg_grads_bcast = []
        for j in range(len(single_grads)):
            with ops.device(avail_devices[j]):
                if (group_0_main_device < group_size) == (j < group_size):
                    src_device_grad = group_0_agg_grads_bcast
                else:
                    src_device_grad = group_1_agg_grads_bcast
                agg_grads_bcast.append(array_ops.identity(src_device_grad))
        agg_grads.append([(g, v) for g, (_, v) in zip(agg_grads_bcast, single_grads)])
    agg_grads = list(zip(*agg_grads))
    return agg_grads

Aggregate gradients using hierarchical copies.

Args:
avail_devices: available GPU devices.
replica_grads: List of lists of (gradient, variable) tuples. The outer list
is over replicas. The inner list is over individual gradients.

Returns:
The list of (aggregated_gradient, variable), where the gradient has been
summed across all replicas and the variable is chosen from the first
replica.


def attachment_to_multidim_measurement(attachment, name=None):
    data = json.loads(attachment.data)
    name = (name or data.get('name'))
    attachment_dims = data.get('dimensions', [])
    attachment_values = data.get('value')
    attachment_outcome_str = data.get('outcome')
    if (attachment_outcome_str not in TEST_RUN_STATUS_NAME_TO_MEASUREMENT_OUTCOME):
        try:
            attachment_outcome_str = test_runs_pb2.Status.Name(int(attachment_outcome_str))
        except ValueError:
            attachment_outcome_str = None
    outcome = TEST_RUN_STATUS_NAME_TO_MEASUREMENT_OUTCOME.get(attachment_outcome_str)
    _lazy_load_units_by_code()
    dims = []
    for d in attachment_dims:
        unit = UNITS_BY_CODE.get(d.get('uom_code'), units.NONE)
        description = d.get('name', '')
        dims.append(measurements.Dimension(description=description, unit=unit))
    if (attachment_values and (len(dims) == len(attachment_values[0]))):
        units_ = dims[(- 1)].unit
        dimensions = dims[:(- 1)]
    else:
        units_ = None
        dimensions = dims
    measured_value = measurements.DimensionedMeasuredValue(name=name, num_dimensions=len(dimensions))
    for row in attachment_values:
        coordinates = tuple(row[:(- 1)])
        val = row[(- 1)]
        measured_value[coordinates] = val
    measurement = measurements.Measurement(name=name, units=units_, dimensions=tuple(dimensions), measured_value=measured_value, outcome=outcome)
    return measurement

Convert an OpenHTF test record attachment to a multi-dim measurement.

This is a best effort attempt to reverse, as some data is lost in converting
from a multidim to an attachment.

Args:
attachment: an `openhtf.test_record.Attachment` from a multi-dim.
name: an optional name for the measurement.  If not provided will use the
name included in the attachment.

Returns:
An multi-dim `openhtf.Measurement`.


def save_spectre_plot(self, filename="spectre.pdf", img_format="pdf",
                          sigma=0.05, step=0.01):

        d, plt = self.get_spectre_plot(sigma, step)
        plt.savefig(filename, format=img_format)

Save matplotlib plot of the spectre to a file.

Args:
filename: Filename to write to.
img_format: Image format to use. Defaults to EPS.
sigma: Full width at half maximum in eV for normal functions.
step: bin interval in eV


def _remove_squeezable_dimensions(predictions, labels, weights):
    predictions = ops.convert_to_tensor(predictions)
    if labels is not None:
        labels, predictions = confusion_matrix.remove_squeezable_dimensions(labels, predictions)
        predictions.get_shape().assert_is_compatible_with(labels.get_shape())
    if weights is None:
        return (predictions, labels, None)
    weights = ops.convert_to_tensor(weights)
    weights_shape = weights.get_shape()
    weights_rank = weights_shape.ndims
    if weights_rank == 0:
        return (predictions, labels, weights)
    predictions_shape = predictions.get_shape()
    predictions_rank = predictions_shape.ndims
    if predictions_rank is not None and weights_rank is not None:
        if weights_rank - predictions_rank == 1:
            weights = array_ops.squeeze(weights, [-1])
        elif predictions_rank - weights_rank == 1:
            weights = array_ops.expand_dims(weights, [-1])
    else:
        weights_rank_tensor = array_ops.rank(weights)
        rank_diff = weights_rank_tensor - array_ops.rank(predictions)

        def _maybe_expand_weights():
            return cond.cond(math_ops.equal(rank_diff, -1), lambda: array_ops.expand_dims(weights, [-1]), lambda: weights)
        if weights_rank is not None and (not weights_shape.dims[-1].is_compatible_with(1)):
            maybe_squeeze_weights = lambda: weights
        else:
            maybe_squeeze_weights = lambda: array_ops.squeeze(weights, [-1])

        def _maybe_adjust_weights():
            return cond.cond(math_ops.equal(rank_diff, 1), maybe_squeeze_weights, _maybe_expand_weights)
        weights = cond.cond(math_ops.equal(weights_rank_tensor, 0), lambda: weights, _maybe_adjust_weights)
    return (predictions, labels, weights)

Squeeze or expand last dim if needed.

Squeezes last dim of `predictions` or `labels` if their rank differs by 1
(using confusion_matrix.remove_squeezable_dimensions).
Squeezes or expands last dim of `weights` if its rank differs by 1 from the
new rank of `predictions`.

If `weights` is scalar, it is kept scalar.

This will use static shape if available. Otherwise, it will add graph
operations, which could result in a performance hit.

Args:
predictions: Predicted values, a `Tensor` of arbitrary dimensions.
labels: Optional label `Tensor` whose dimensions match `predictions`.
weights: Optional weight scalar or `Tensor` whose dimensions match
`predictions`.

Returns:
Tuple of `predictions`, `labels` and `weights`. Each of them possibly has
the last dimension squeezed, `weights` could be extended by one dimension.


def _make_request(self, method, path, data=None, **kwargs):

        _logger.debug("Method for request is %s" % method)
        url = self._construct_full_url(path)
        _logger.debug("URL for request is %s" % url)
        self._auth_info.populate_request_data(kwargs)
        _logger.debug("The arguments are %s" % kwargs)

        if self._auth_info._headers:
            kwargs.setdefault('headers', {}).update(self._auth_info._headers)

        res = requests.request(method, url, data=data, **kwargs)

        if res.ok:
            _logger.debug("Request was successful.")
            return res.content.decode('utf-8')

        if hasattr(res, 'content'):
            _logger.debug("Response was %s:%s", res.status_code, res.content)
            raise self._exception_for(res.status_code)(
                res.content, http_code=res.status_code
            )
        else:
            msg = "No response from URL: %s" % res.request.url
            _logger.error(msg)
            raise NoResponseError(msg)

Make a request.

Use the `requests` module to actually perform the request.

Args:
`method`: The method to use.
`path`: The path to the resource.
`data`: Any data to send (for POST and PUT requests).
`kwargs`: Other parameters for `requests`.
Returns:
The content of the response.
Raises:
An exception depending on the HTTP status code of the response.


class AverageScore(ScoreAggregation):

    def __init__(self, **kwargs):
        super().__init__(agg_func=statistics.mean, **kwargs)

Aggregates anomaly scores by calculating their average.

This `AggregationFn` computes the average of the anomaly scores from a
collection of `AnomalyPrediction` objects.

Args:
**kwargs: Additional keyword arguments to pass to the base
`ScoreAggregation` class.