def parse_parameters(parameters, variables_mapping=None, functions_mapping=None):
    variables_mapping = (variables_mapping or {})
    functions_mapping = (functions_mapping or {})
    parsed_parameters_list = []
    parameters = utils.ensure_mapping_format(parameters)
    for (parameter_name, parameter_content) in parameters.items():
        parameter_name_list = parameter_name.split('-')
        if isinstance(parameter_content, list):
            parameter_content_list = []
            for parameter_item in parameter_content:
                if (not isinstance(parameter_item, (list, tuple))):
                    parameter_item = [parameter_item]
                parameter_content_dict = dict(zip(parameter_name_list, parameter_item))
                parameter_content_list.append(parameter_content_dict)
        else:
            parsed_variables_mapping = parse_variables_mapping(variables_mapping)
            parsed_parameter_content = eval_lazy_data(parameter_content, parsed_variables_mapping, functions_mapping)
            if (not isinstance(parsed_parameter_content, list)):
                raise exceptions.ParamsError('parameters syntax error!')
            parameter_content_list = []
            for parameter_item in parsed_parameter_content:
                if isinstance(parameter_item, dict):
                    parameter_dict = {key: parameter_item[key] for key in parameter_name_list}
                elif isinstance(parameter_item, (list, tuple)):
                    parameter_dict = dict(zip(parameter_name_list, parameter_item))
                elif (len(parameter_name_list) == 1):
                    parameter_dict = {parameter_name_list[0]: parameter_item}
                parameter_content_list.append(parameter_dict)
        parsed_parameters_list.append(parameter_content_list)
    return utils.gen_cartesian_product(*parsed_parameters_list)

parse parameters and generate cartesian product.

Args:
parameters (list) parameters: parameter name and value in list
parameter value may be in three types:
(1) data list, e.g. ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
(2) call built-in parameterize function, "${parameterize(account.csv)}"
(3) call custom function in debugtalk.py, "${gen_app_version()}"

variables_mapping (dict): variables mapping loaded from testcase config
functions_mapping (dict): functions mapping loaded from debugtalk.py

Returns:
list: cartesian product list

Examples:
>>> parameters = [
{"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]},
{"username-password": "${parameterize(account.csv)}"},
{"app_version": "${gen_app_version()}"}
]
>>> parse_parameters(parameters)


def __init__(self, json, Api):

        self.json = json
        self._index = 0
        self.Api = Api

Instantiates EventStream instance.

Args:
json(list): List from deserializing txn from homeserver.
Api(func): Generates http api when passed identity=mxid.


def bipartition_indices(N):

    result = []
    if N <= 0:
        return result

    for i in range(2**(N - 1)):
        part = [[], []]
        for n in range(N):
            bit = (i >> n) & 1
            part[bit].append(n)
        result.append((tuple(part[1]), tuple(part[0])))
    return result

Return indices for undirected bipartitions of a sequence.

Args:
N (int): The length of the sequence.

Returns:
list: A list of tuples containing the indices for each of the two
parts.

Example:
>>> N = 3
>>> bipartition_indices(N)
[((), (0, 1, 2)), ((0,), (1, 2)), ((1,), (0, 2)), ((0, 1), (2,))]


def sas_logical_jbod_attachments(self):
    if (not self.__sas_logical_jbod_attachments):
        self.__sas_logical_jbod_attachments = SasLogicalJbodAttachments(self.__connection)
    return self.__sas_logical_jbod_attachments

Gets the SAS Logical JBOD Attachments client.

Returns:
SasLogicalJbodAttachments:


def update(self, rid, data, raise_on_error=True):
    cache_data = {'cache-date': self._dt_to_epoch(datetime.now()), 'cache-data': data}
    return self.ds.put(rid, cache_data, raise_on_error)

Write updated cache data to the DataStore.

Args:
rid (str): The record identifier.
data (dict): The record data.
raise_on_error (bool): If True and not r.ok this method will raise a RunTimeError.

Returns:
object : Python request response.


def convert_bbox_yolo_to_pascal(boxes: torch.Tensor, image_size: tuple[int, int]) -> torch.Tensor:
    boxes = center_to_corners_format(boxes)
    height, width = image_size
    boxes = boxes * torch.tensor([[width, height, width, height]])
    return boxes

Convert bounding boxes from YOLO format (x_center, y_center, width, height) in range [0, 1]
to Pascal VOC format (x_min, y_min, x_max, y_max) in absolute coordinates.

Args:
boxes (torch.Tensor): Bounding boxes in YOLO format
image_size (Tuple[int, int]): Image size in format (height, width)

Returns:
torch.Tensor: Bounding boxes in Pascal VOC format (x_min, y_min, x_max, y_max)


def _ParseToken(self, file_object, file_offset):
    token_type = self._ParseTokenType(file_object, file_offset)
    token_data = None
    token_data_map_name = self._DATA_TYPE_MAP_PER_TOKEN_TYPE.get(token_type, None)
    if token_data_map_name:
        token_data_map = self._GetDataTypeMap(token_data_map_name)
        (token_data, _) = self._ReadStructureFromFileObject(file_object, (file_offset + 1), token_data_map)
    return (token_type, token_data)

Parses a token.

Args:
file_object (dfvfs.FileIO): file-like object.
file_offset (int): offset of the token relative to the start of
the file-like object.

Returns:
tuple: containing:
int: token type
object: token data or None if the token type is not supported.


def StreamMedia(self, callback=None, finish_callback=None, additional_headers=None, use_chunks=True):
    callback = (callback or self.progress_callback)
    finish_callback = (finish_callback or self.finish_callback)
    self.EnsureInitialized()
    while True:
        if (self.__initial_response is not None):
            response = self.__initial_response
            self.__initial_response = None
        else:
            end_byte = self.__ComputeEndByte(self.progress, use_chunks=use_chunks)
            response = self.__GetChunk(self.progress, end_byte, additional_headers=additional_headers)
        if (self.total_size is None):
            self.__SetTotal(response.info)
        response = self.__ProcessResponse(response)
        self._ExecuteCallback(callback, response)
        if ((response.status_code == http_client.OK) or (self.progress >= self.total_size)):
            break
    self._ExecuteCallback(finish_callback, response)

Stream the entire download.

Args:
callback: (default: None) Callback to call as each chunk is
completed.
finish_callback: (default: None) Callback to call when the
download is complete.
additional_headers: (default: None) Additional headers to
include in fetching bytes.
use_chunks: (bool, default: True) If False, ignore self.chunksize
and stream this download in a single request.

Returns:
None. Streams bytes into self.stream.


def __init__(self,
                 nlp,
                 rules: Dict,
                 extractor_name: str) -> None:

        Extractor.__init__(self,
                           input_type=InputType.TEXT,
                           category="spacy_rule_extractor",
                           name=extractor_name)
        self._rules = rules["rules"]
        self._nlp = copy.deepcopy(nlp)
        self._tokenizer = Tokenizer(self._nlp)
        self._matcher = Matcher(self._nlp.vocab)
        self._field_name = rules["field_name"] if "field_name" in rules else extractor_name
        self._rule_lst = {}
        self._hash_map = {}
        for idx, a_rule in enumerate(self._rules):
            this_rule = Rule(a_rule, self._nlp)
            self._rule_lst[this_rule.identifier + "rule_id

Initialize the extractor, storing the rule information and construct spacy rules
Args:
nlp
rules (Dict): spacy rules
extractor_name: str

Returns:


def get_absolute_positions(self, abs_pos_embeddings, has_cls_token, height, width):
    if has_cls_token:
        abs_pos_embeddings = abs_pos_embeddings[:, 1:]
    num_position = abs_pos_embeddings.shape[1]
    size = int(math.sqrt(num_position))
    if size * size != num_position:
        raise ValueError('Absolute position embeddings must be a square number.')
    if torch.jit.is_tracing() or (size != height or size != width):
        new_abs_pos_embeddings = nn.functional.interpolate(abs_pos_embeddings.reshape(1, size, size, -1).permute(0, 3, 1, 2), size=(height, width), mode='bicubic', align_corners=False)
        return new_abs_pos_embeddings.permute(0, 2, 3, 1)
    else:
        return abs_pos_embeddings.reshape(1, height, width, -1)

Calculate absolute positional embeddings. If needed, resize embeddings and remove cls_token dimension for the
original embeddings.

Args:
abs_pos_embeddings (`torch.Tensor`):
Absolute positional embeddings with (1, num_position, num_channels).
has_cls_token (`bool`):
If true, has 1 embedding in abs_pos_embeddings for cls token.
height (`int`):
Height of input image tokens.
width (`int`):
Width of input image tokens.

Returns:
Absolute positional embeddings after processing with shape (1, height, width, num_channels)


class BrosProcessor(ProcessorMixin):
    attributes = ['tokenizer']
    tokenizer_class = ('BertTokenizer', 'BertTokenizerFast')

    def __init__(self, tokenizer=None, **kwargs):
        if tokenizer is None:
            raise ValueError('You need to specify a `tokenizer`.')
        super().__init__(tokenizer)

    def __call__(self, text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]=None, add_special_tokens: bool=True, padding: Union[bool, str, PaddingStrategy]=False, truncation: Union[bool, str, TruncationStrategy]=None, max_length: Optional[int]=None, stride: int=0, pad_to_multiple_of: Optional[int]=None, return_token_type_ids: Optional[bool]=None, return_attention_mask: Optional[bool]=None, return_overflowing_tokens: bool=False, return_special_tokens_mask: bool=False, return_offsets_mapping: bool=False, return_length: bool=False, verbose: bool=True, return_tensors: Optional[Union[str, TensorType]]=None, **kwargs) -> BatchEncoding:

        encoding = self.tokenizer(text=text, add_special_tokens=add_special_tokens, padding=padding, truncation=truncation, max_length=max_length, stride=stride, pad_to_multiple_of=pad_to_multiple_of, return_token_type_ids=return_token_type_ids, return_attention_mask=return_attention_mask, return_overflowing_tokens=return_overflowing_tokens, return_special_tokens_mask=return_special_tokens_mask, return_offsets_mapping=return_offsets_mapping, return_length=return_length, verbose=verbose, return_tensors=return_tensors, **kwargs)
        return encoding

    def batch_decode(self, *args, **kwargs):

        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):

        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        return list(dict.fromkeys(tokenizer_input_names))

Constructs a Bros processor which wraps a BERT tokenizer.

[`BrosProcessor`] offers all the functionalities of [`BertTokenizerFast`]. See the docstring of
[`~BrosProcessor.__call__`] and [`~BrosProcessor.decode`] for more information.

Args:
tokenizer (`BertTokenizerFast`, *optional*):
An instance of ['BertTokenizerFast`]. The tokenizer is a required input.


def _CreateUserIdentifier(identifier_type=None, value=None):
    if (identifier_type in _HASHED_IDENTIFIER_TYPES):
        value = hashlib.sha256(value.strip().lower()).hexdigest()
    user_identifier = {'userIdentifierType': identifier_type, 'value': value}
    return user_identifier

Creates a user identifier from the specified type and value.

Args:
identifier_type: a str specifying the type of user identifier.
value: a str value of the identifier; to be hashed using SHA-256 if needed.

Returns:
A dict specifying a user identifier, with a value hashed using SHA-256 if
needed.


def graph_argument(*arg_names, **options):
    if (not arg_names):
        arg_names = ['G']
    allow_None = options.pop('allow_None', False)
    if options:
        (key, _) = options.popitem()
        msg = "graph_argument() for an unexpected keyword argument '{}'".format(key)
        raise TypeError(msg)

    def _graph_arg(f):
        argspec = getargspec(f)

        def _enforce_single_arg(name, args, kwargs):
            try:
                G = kwargs[name]
            except KeyError:
                raise TypeError('Graph argument missing')
            if (hasattr(G, 'edges') and hasattr(G, 'nodes')):
                kwargs[name] = (list(G.nodes), list(G.edges))
            elif _is_integer(G):
                kwargs[name] = (list(range(G)), list(itertools.combinations(range(G), 2)))
            elif (isinstance(G, abc.Sequence) and (len(G) == 2)):
                if isinstance(G[0], integer_types):
                    kwargs[name] = (list(range(G[0])), G[1])
            elif (allow_None and (G is None)):
                return G
            else:
                raise ValueError('Unexpected graph input form')
            return

        @wraps(f)
        def new_f(*args, **kwargs):
            bound_args = inspect.getcallargs(f, *args, **kwargs)
            final_args = list(bound_args.pop(argspec.varargs, ()))
            final_kwargs = bound_args.pop(argspec.keywords, {})
            final_kwargs.update(bound_args)
            for name in arg_names:
                _enforce_single_arg(name, final_args, final_kwargs)
            return f(*final_args, **final_kwargs)
        return new_f
    return _graph_arg

Decorator to coerce given graph arguments into a consistent form.

The wrapped function will accept either an integer n, interpreted as a
complete graph of size n, or a nodes/edges pair, or a NetworkX graph. The
argument will then be converted into a nodes/edges 2-tuple.

Args:
*arg_names (optional, default='G'):
The names of the arguments for input graphs.

allow_None (bool, optional, default=False):
Allow None as an input graph in which case it is passed through as
None.


def ReceiveMessagesRelationalFlows(self, client_id, messages):

    now = time.time()
    unprocessed_msgs = []
    message_handler_requests = []
    dropped_count = 0
    for session_id, msgs in iteritems(
        collection.Group(messages, operator.attrgetter("session_id"))):

      leftover_msgs = self.HandleWellKnownFlows(msgs)

      for msg in leftover_msgs:
        if (msg.auth_state != msg.AuthorizationState.AUTHENTICATED and
            msg.session_id != self.unauth_allowed_session_id):
          dropped_count += 1
          continue

        if session_id in queue_manager.session_id_map:
          message_handler_requests.append(
              rdf_objects.MessageHandlerRequest(
                  client_id=msg.source.Basename(),
                  handler_name=queue_manager.session_id_map[session_id],
                  request_id=msg.response_id,
                  request=msg.payload))
        else:
          unprocessed_msgs.append(msg)

    if dropped_count:
      logging.info("Dropped %d unauthenticated messages for %s", dropped_count,
                   client_id)

    if unprocessed_msgs:
      flow_responses = []
      for message in unprocessed_msgs:
        flow_responses.append(
            rdf_flow_objects.FlowResponseForLegacyResponse(message))

      data_store.REL_DB.WriteFlowResponses(flow_responses)

      for msg in unprocessed_msgs:
        if msg.type == rdf_flows.GrrMessage.Type.STATUS:
          stat = rdf_flows.GrrStatus(msg.payload)
          if stat.status == rdf_flows.GrrStatus.ReturnedStatus.CLIENT_KILLED:

            crash_details = rdf_client.ClientCrash(
                client_id=client_id,
                session_id=msg.session_id,
                backtrace=stat.backtrace,
                crash_message=stat.error_message,
                nanny_status=stat.nanny_status,
                timestamp=rdfvalue.RDFDatetime.Now())
            events.Events.PublishEvent(
                "ClientCrash", crash_details, token=self.token)

    if message_handler_requests:
      data_store.REL_DB.WriteMessageHandlerRequests(message_handler_requests)

    logging.debug("Received %s messages from %s in %s sec", len(messages),
                  client_id,
                  time.time() - now)

Receives and processes messages for flows stored in the relational db.

Args:
client_id: The client which sent the messages.
messages: A list of GrrMessage RDFValues.


def _funm_svd(a, func):
    (U, s, Vh) = la.svd(a, lapack_driver='gesvd')
    S = np.diag(func(s))
    return U.dot(S).dot(Vh)

Apply real scalar function to singular values of a matrix.

Args:
a (array_like): (N, N) Matrix at which to evaluate the function.
func (callable): Callable object that evaluates a scalar function f.

Returns:
ndarray: funm (N, N) Value of the matrix function specified by func
evaluated at `A`.


def find_field(item_list, cond, comparator, target_field):
    for item in item_list:
        if (comparator(item, cond) and (target_field in item)):
            return item[target_field]
    return None

Finds the value of a field in a dict object that satisfies certain
conditions.

Args:
item_list: A list of dict objects.
cond: A param that defines the condition.
comparator: A function that checks if an dict satisfies the condition.
target_field: Name of the field whose value to be returned if an item
satisfies the condition.

Returns:
Target value or None if no item satisfies the condition.


def gaussian_square(duration: int, amp: complex, sigma: float, risefall: int, name: str=None) -> SamplePulse:
    center = (duration / 2)
    width = (duration - (2 * risefall))
    zeroed_width = (duration + 2)
    return _sampled_gaussian_square_pulse(duration, amp, center, width, sigma, zeroed_width=zeroed_width, name=name)

Generates gaussian square `SamplePulse`.

Centered at `duration/2` and zeroed at `t=-1` and `t=duration+1` to prevent
large initial/final discontinuities.

Applies `left` sampling strategy to generate discrete pulse from continuous function.

Args:
duration: Duration of pulse. Must be greater than zero.
amp: Pulse amplitude.
sigma: Width (standard deviation) of gaussian rise/fall portion of the pulse.
risefall: Number of samples over which pulse rise and fall happen. Width of
square portion of pulse will be `duration-2*risefall`.
name: Name of pulse.


def set_fig_title(self, title, **kwargs):
    prop_default = {'fontsize': 20}
    for (prop, default) in prop_default.items():
        kwargs[prop] = kwargs.get(prop, default)
    self.figure.fig_title = title
    self.figure.fig_title_kwargs = kwargs
    return

Set overall figure title.

Set title for overall figure. This is not for a specific plot.
It will place the title at the top of the figure with a call to ``fig.suptitle``.

Args:
title (str): Figure title.

Keywork Arguments:
x/y (float, optional): The x/y location of the text in figure coordinates.
Defaults are 0.5 for x and 0.98 for y.
horizontalalignment/ha (str, optional): The horizontal alignment of
the text relative to (x, y). Optionas are 'center', 'left', or 'right'.
Default is 'center'.
verticalalignment/va (str, optional): The vertical alignment of the text
relative to (x, y). Optionas are 'top', 'center', 'bottom',
or 'baseline'. Default is 'top'.
fontsize/size (int, optional): The font size of the text. Default is 20.


def _line_and_col_to_offset(lines, line, col):
    offset = 0
    for (index, contents) in enumerate(lines, 1):
        if (index == line):
            return (offset + col)
        offset += len(contents)
    raise ValueError('Offset {}:{} not found'.format(line, col))

Figure out the offset into a file for a particular line and col.

This can return offsets that don't actually exist in the file. If you
specify a line that exists and a col that is past the end of that line, this
will return a "fake" offset. This is to account for the fact that a
WorkItem's end_pos is one-past the end of a mutation, and hence potentially
one-past the end of a file.

Args:
lines: A sequence of the lines in a file.
line: A one-based index indicating the line in the file.
col: A zero-based index indicating the column on `line`.

Raises: ValueError: If the specified line found in the file.


def connect_direct(self, device, calibration=True):
    if (not isinstance(device, ScanResult)):
        if isinstance(device, str):
            device = ScanResult(device, fmt_addr_raw(device))
        elif isinstance(device, unicode):
            device = device.encode('ascii')
            device = ScanResult(device, fmt_addr_raw(device))
        else:
            logger.warn('Expected ScanResult, found type {} instead!'.format(type(device)))
            return (False, None)
    logger.debug('Connecting directly to device address'.format(device.addr))
    self._set_state(self._STATE_CONNECTING)
    self.api.ble_cmd_gap_connect_direct(device.raw_addr, 0, 6, 14, 100, 50)
    self._wait_for_state(self._STATE_CONNECTING, 5)
    if (self.state != self._STATE_CONNECTED):
        logger.warn('Connection failed!')
        self._set_state(self._STATE_GAP_END)
        self.api.ble_cmd_gap_end_procedure()
        self._wait_for_state(self._STATE_GAP_END)
        return (False, None)
    conn_handle = self.conn_handles[(- 1)]
    logger.info('Connection OK, handle is 0x{:02X}'.format(conn_handle))
    sk8 = SK8(self, conn_handle, device, calibration)
    self._add_device(sk8)
    sk8._discover_services()
    return (True, sk8)

Establish a connection to a single SK8.

Args:
device: either a :class:`ScanResult` or a plain hardware address string
in xx:xx:xx:xx:xx:xx format.
calibration (bool): True to attempt to load calibration data for this
device after connection, False otherwise. See :meth:`SK8.load_calibration`.

Returns:
tuple (`result`, `device`), where `result` is a bool indicating if a
connection was created successfully. If `result` is True, `device` will
be set to a new :class:`SK8` instance. Otherwise it will be None.


def list(self):
    response = self._swimlane.request('get', 'app')
    return [App(self._swimlane, item) for item in response.json()]

Retrieve list of all apps

Returns:
:class:`list` of :class:`~swimlane.core.resources.app.App`: List of all retrieved apps


def _policy_loss(self, old_policy, policy, action, advantage, length):
    with tf.name_scope('policy_loss'):
        kl = tf.contrib.distributions.kl_divergence(old_policy, policy)
        kl = tf.check_numerics(kl, 'kl')
        kl = tf.reduce_mean(self._mask(kl, length), 1)
        policy_gradient = tf.exp((policy.log_prob(action) - old_policy.log_prob(action)))
        surrogate_loss = (- tf.reduce_mean(self._mask((policy_gradient * tf.stop_gradient(advantage)), length), 1))
        surrogate_loss = tf.check_numerics(surrogate_loss, 'surrogate_loss')
        kl_penalty = (self._penalty * kl)
        cutoff_threshold = (self._config.kl_target * self._config.kl_cutoff_factor)
        cutoff_count = tf.reduce_sum(tf.cast((kl > cutoff_threshold), tf.int32))
        with tf.control_dependencies([tf.cond((cutoff_count > 0), (lambda : tf.Print(0, [cutoff_count], 'kl cutoff! ')), int)]):
            kl_cutoff = ((self._config.kl_cutoff_coef * tf.cast((kl > cutoff_threshold), tf.float32)) * ((kl - cutoff_threshold) ** 2))
        policy_loss = ((surrogate_loss + kl_penalty) + kl_cutoff)
        entropy = tf.reduce_mean(policy.entropy(), axis=1)
        if self._config.entropy_regularization:
            policy_loss -= (self._config.entropy_regularization * entropy)
        summary = tf.summary.merge([tf.summary.histogram('entropy', entropy), tf.summary.histogram('kl', kl), tf.summary.histogram('surrogate_loss', surrogate_loss), tf.summary.histogram('kl_penalty', kl_penalty), tf.summary.histogram('kl_cutoff', kl_cutoff), tf.summary.histogram('kl_penalty_combined', (kl_penalty + kl_cutoff)), tf.summary.histogram('policy_loss', policy_loss), tf.summary.scalar('avg_surr_loss', tf.reduce_mean(surrogate_loss)), tf.summary.scalar('avg_kl_penalty', tf.reduce_mean(kl_penalty)), tf.summary.scalar('avg_policy_loss', tf.reduce_mean(policy_loss))])
        policy_loss = tf.reduce_mean(policy_loss, 0)
        return (tf.check_numerics(policy_loss, 'policy_loss'), summary)

Compute the policy loss composed of multiple components.

1. The policy gradient loss is importance sampled from the data-collecting
policy at the beginning of training.
2. The second term is a KL penalty between the policy at the beginning of
training and the current policy.
3. Additionally, if this KL already changed more than twice the target
amount, we activate a strong penalty discouraging further divergence.

Args:
old_policy: Action distribution of the behavioral policy.
policy: Sequences of distribution params of the current policy.
action: Sequences of actions.
advantage: Sequences of advantages.
length: Batch of sequence lengths.

Returns:
Tuple of loss tensor and summary tensor.


def _parse_metadata(self, message):

        metadata = Metadata(source=self.actor_urn).__dict__
        metadata['thread_ts'] = message.get('thread_ts')
        if 'presence' in message:
            metadata['presence'] = message['presence']

        if 'text' in message:
            metadata['text'] = message['text']
        elif 'previous_message' in message:

            if 'text' in message['previous_message']:
                metadata['text'] = message['previous_message']['text']
            else:
                metadata['text'] = None
        else:
            metadata['text'] = None

        if 'user' in message:
            metadata['source_user'] = message['user']
        elif 'bot_id' in message:
            metadata['source_user'] = self.get_userid_from_botid(
                                      message['bot_id'])
        elif 'message' in message and 'user' in message['message']:
            metadata['source_user'] = message['message']['user']
        else:
            metadata['source_user'] = None

        metadata['user_id'] = metadata['source_user']
        metadata['display_name'] = self.get_username(metadata['source_user'])

        if 'channel' in message:
            metadata['source_channel'] = message['channel']

            if message['channel'].startswith('D'):
                metadata['is_private_message'] = True
            else:
                metadata['is_private_message'] = False

        metadata['source_connector'] = 'slack'

        return metadata

Parse incoming messages to build metadata dict
Lots of 'if' statements. It sucks, I know.

Args:
message (dict): JSON dump of message sent from Slack

Returns:
Legobot.Metadata


def process_event(event):
    if (event.type == EventType.ON_CONVERSATION_TURN_STARTED):
        print()
    print(event)
    if ((event.type == EventType.ON_CONVERSATION_TURN_FINISHED) and event.args and (not event.args['with_follow_on_turn'])):
        print()
    if (event.type == EventType.ON_DEVICE_ACTION):
        for (command, params) in event.actions:
            print('Do command', command, 'with params', str(params))

Pretty prints events.

Prints all events that occur with two spaces between each new
conversation and a single space between turns of a conversation.

Args:
event(event.Event): The current event to process.


def add_mapped_chain_ids(self, mapped_chains):
    mapped_chains = ssbio.utils.force_list(mapped_chains)
    for c in mapped_chains:
        if (c not in self.mapped_chains):
            self.mapped_chains.append(c)
            log.debug('{}: added to list of mapped chains'.format(c))
        else:
            log.debug('{}: chain already in list of mapped chains, not adding'.format(c))

Add chains by ID into the mapped_chains attribute

Args:
mapped_chains (str, list): Chain ID or list of IDs


def synthesize(self, duration, freqs_in_hz=[440.]):

        freqs = np.array(freqs_in_hz)
        scaling = 1 / len(freqs)
        sr = int(self.samplerate)
        cps = freqs / sr
        ts = (duration / Seconds(1)) * sr
        ranges = np.array([np.arange(0, ts * c, c) for c in cps])
        raw = (np.sin(ranges * (2 * np.pi)) * scaling).sum(axis=0)
        return AudioSamples(raw, self.samplerate)

Synthesize one or more sine waves

Args:
duration (numpy.timdelta64): The duration of the sound to be
synthesized
freqs_in_hz (list of float): Numbers representing the frequencies
in hz that should be synthesized


def variants_export_header(case_obj):

    header = []
    header = header + EXPORT_HEADER

    for individual in case_obj['individuals']:
        display_name = str(individual['display_name'])
        header.append('AD_reference_'+display_name)
        header.append('AD_alternate_'+display_name)
        header.append('GT_quality_'+display_name)
    return header

Returns a header for the CSV file with the filtered variants to be exported.

Args:
case_obj(scout.models.Case)

Returns:
header: includes the fields defined in scout.constants.variants_export EXPORT_HEADER
+ AD_reference, AD_alternate, GT_quality for each sample analysed for a case


def build_evaluation(variant_specific, variant_id, user_id, user_name,
                     institute_id, case_id, classification, criteria):

    criteria = criteria or []
    evaluation_obj = dict(
        variant_specific = variant_specific,
        variant_id = variant_id,
        institute_id = institute_id,
        case_id = case_id,
        classification = classification,
        user_id = user_id,
        user_name = user_name,
        created_at = datetime.datetime.now(),
    )
    criteria_objs = []
    for info in criteria:
        criteria_obj = {}

        criteria_obj['term'] = info['term']
        if 'comment' in info:
            criteria_obj['comment'] = info['comment']
        if 'links' in info:
            criteria_obj['links'] = info['links']
        criteria_objs.append(criteria_obj)

    evaluation_obj['criteria'] = criteria_objs

    return evaluation_obj

Build a evaluation object ready to be inserted to database

Args:
variant_specific(str): md5 string for the specific variant
variant_id(str): md5 string for the common variant
user_id(str)
user_name(str)
institute_id(str)
case_id(str)
classification(str): The ACMG classification
criteria(list(dict)): A list of dictionaries with ACMG criterias

Returns:
evaluation_obj(dict): Correctly formatted evaluation object


def _ParseMRUListExKey(
      self, parser_mediator, registry_key, codepage='cp1252'):

    try:
      mrulistex = self._ParseMRUListExValue(registry_key)
    except (ValueError, errors.ParseError) as exception:
      parser_mediator.ProduceExtractionWarning(
          'unable to parse MRUListEx value with error: {0!s}'.format(exception))
      return

    if not mrulistex:
      return

    values_dict = {}
    found_terminator = False
    for entry_index, entry_number in enumerate(mrulistex):

      if entry_number == -1:
        break

      if found_terminator:
        parser_mediator.ProduceExtractionWarning((
            'found additional MRUListEx entries after terminator in key: '
            '{0:s}.').format(registry_key.path))

        found_terminator = False

      value_string = self._ParseMRUListExEntryValue(
          parser_mediator, registry_key, entry_index, entry_number,
          codepage=codepage)

      value_text = 'Index: {0:d} [MRU Value {1:d}]'.format(
          entry_index + 1, entry_number)

      values_dict[value_text] = value_string

    event_data = windows_events.WindowsRegistryEventData()
    event_data.key_path = registry_key.path
    event_data.offset = registry_key.offset
    event_data.regvalue = values_dict
    event_data.source_append = self._SOURCE_APPEND

    event = time_events.DateTimeValuesEvent(
        registry_key.last_written_time, definitions.TIME_DESCRIPTION_WRITTEN)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Extract event objects from a MRUListEx Registry key.

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


def should_use_network(self, request):

        return (self.networking and
                all((fn(request) for fn in self.network_filters)))

Verifies if real networking mode should be used for the given
request, passing it to the registered network filters.

Arguments:
request (pook.Request): outgoing HTTP request to test.

Returns:
bool


def _flatten_obs(self, obs_dict, verbose=False):

        ob_lst = []
        for key in obs_dict:
            if key in self.keys:
                if verbose:
                    print("adding key: {}".format(key))
                ob_lst.append(obs_dict[key])
        return np.concatenate(ob_lst)

Filters keys of interest out and concatenate the information.

Args:
obs_dict: ordered dictionary of observations


def _format_variants(self, variant, index, case_obj, add_all_info=False):

        header_line = self.head.header

        vcf_individuals = set([ind_id for ind_id in self.head.individuals])

        info_dict = dict(variant.INFO)

        chrom = variant.CHROM
        if chrom.startswith('chr') or chrom.startswith('CHR'):
            chrom = chrom[3:]

        variant_obj = Variant(
                CHROM=chrom,
                POS=variant.POS,
                ID=variant.ID,
                REF=variant.REF,
                ALT=variant.ALT[0],
                QUAL=variant.QUAL,
                FILTER=variant.FILTER,
            )
        variant_obj._set_variant_id()

        logger.debug("Creating a variant object of variant {0}".format(
            variant_obj.variant_id))

        variant_obj.index = index
        logger.debug("Updating index to: {0}".format(
            index))

        variant_obj.start = variant.start
        variant_obj.stop = variant.end

        if self.variant_type == 'sv':
            variant_obj.stop = int(info_dict.get('END', variant_obj.POS))
            self._add_sv_coordinates(variant_obj)
            variant_obj.sv_type = info_dict.get('SVTYPE')

            occurances = info_dict.get('OCC')
            if occurances:
                logger.debug("Updating occurances to: {0}".format(
                    occurances))
                variant_obj['occurances'] = float(occurances)
                variant_obj.add_frequency('OCC', occurances)

        else:
            self._add_thousand_g(variant_obj, info_dict)
            self._add_cadd_score(variant_obj, info_dict)
            self._add_genetic_models(variant_obj, info_dict)
            self._add_transcripts(variant_obj, info_dict)
            self._add_exac(variant_obj, info_dict)

        self._add_hgnc_symbols(variant_obj)

        if add_all_info:
            self._add_genotype_calls(variant_obj, str(variant), case_obj)
            self._add_compounds(variant_obj, info_dict)
            self._add_gmaf(variant_obj, info_dict)
            self._add_genes(variant_obj)

        self._add_consequences(variant_obj, str(variant))
        self._add_most_severe_consequence(variant_obj)
        self._add_impact_severity(variant_obj)
        self._add_rank_score(variant_obj, info_dict)
        variant_obj.set_max_freq()
        return variant_obj

Return a Variant object

Format variant make a variant that includes enough information for
the variant view.
If add_all_info then all transcripts will be parsed

Args:
variant (cython2.Variant): A variant object
index (int): The index of the variant
case_obj (puzzle.models.Case): A case object


def abort(cls, mapreduce_id, **kwargs):

    cls(key_name="%s:%s" % (mapreduce_id, cls._KEY_NAME),
        command=cls.ABORT).put(**kwargs)

Causes a job to abort.

Args:
mapreduce_id: The job to abort. Not verified as a valid job.


def needkwargs(*argnames):
    required = set(argnames)

    def decorator(func):

        def inner(*args, **kwargs):
            missing = (required - set(kwargs))
            if missing:
                err = ('%s kwargs are missing.' % list(missing))
                raise ValueError(err)
            return func(*args, **kwargs)
        return inner
    return decorator

Function decorator which checks that the decorated function is called
with a set of required kwargs.

Args:
*argnames: String keyword argument names.

Raises:
ValueError: If a required kwarg is missing in the decorated function
call.


def path(self, value):
    if (not value.endswith('/')):
        self._path = '{v}/'.format(v=value)
    else:
        self._path = value

Setter for 'path' property

Args:
value (str): Absolute path to scan


def SetOption(self, section, option, value, overwrite=True):
    if ((not overwrite) and self.config.has_option(section, option)):
        return
    if (not self.config.has_section(section)):
        self.config.add_section(section)
    self.config.set(section, option, str(value))

Set the value of an option in the config file.

Args:
section: string, the section of the config file to check.
option: string, the option to set the value of.
value: string, the value to set the option.
overwrite: bool, True to overwrite an existing value in the config file.


def visit_arithmetic(self, arithmetic: _evaluation.ArithmeticNode) -> _sql_data_types.Select:
    lhs_result = self.visit(arithmetic.left)
    rhs_result = self.visit(arithmetic.right)
    sql_data_type = _sql_data_types.coerce(lhs_result.sql_data_type, rhs_result.sql_data_type)
    lhs_subquery = lhs_result.as_operand()
    rhs_subquery = rhs_result.as_operand()
    if sql_data_type == _sql_data_types.String:
        sql_value = f'CONCAT({lhs_subquery}, {rhs_subquery})'
    elif arithmetic.op == _ast.Arithmetic.Op.MODULO:
        sql_value = f'MOD({lhs_subquery}, {rhs_subquery})'
    elif arithmetic.op == _ast.Arithmetic.Op.TRUNCATED_DIVISION:
        sql_value = f'DIV({lhs_subquery}, {rhs_subquery})'
    else:
        sql_value = f'({lhs_subquery} {arithmetic.op} {rhs_subquery})'
    sql_alias = 'arith_'
    return _sql_data_types.Select(select_part=_sql_data_types.RawExpression(sql_value, _sql_data_type=sql_data_type, _sql_alias=sql_alias), from_part=None)

Translates a FHIRPath arithmetic expression to Standard SQL.

Each operand is expected to be a collection of a single element. Both
operands must be of the same type, or of compatible types according to the
rules of implicit conversion.

Args:
arithmetic: The `_Arithmetic` Expression node.

Returns:
A compiled Standard SQL expression.


def _get_client_by_hostname(self, hostname):

    print('Searching for client: {0:s}'.format(hostname))
    try:
      search_result = self.grr_api.SearchClients(hostname)
    except grr_errors.UnknownError as exception:
      self.state.add_error('Could not search for host {0:s}: {1!s}'.format(
          hostname, exception
      ), critical=True)
      return None

    result = []
    for client in search_result:
      if hostname.lower() in client.data.os_info.fqdn.lower():
        result.append((client.data.last_seen_at, client))

    if not result:
      self.state.add_error(
          'Could not get client_id for {0:s}'.format(hostname), critical=True)
      return None

    last_seen, client = sorted(result, key=lambda x: x[0], reverse=True)[0]

    last_seen_datetime = datetime.datetime.utcfromtimestamp(
        last_seen / 1000000)

    last_seen_seconds = (
        datetime.datetime.utcnow() - last_seen_datetime).total_seconds()
    last_seen_minutes = int(round(last_seen_seconds / 60))

    print('{0:s}: Found active client'.format(client.client_id))
    print('Found active client: {0:s}'.format(client.client_id))
    print('Client last seen: {0:s} ({1:d} minutes ago)'.format(
        last_seen_datetime.strftime('%Y-%m-%dT%H:%M:%S+0000'),
        last_seen_minutes))

    return client

Search GRR by hostname and get the latest active client.

Args:
hostname: hostname to search for.

Returns:
GRR API Client object

Raises:
DFTimewolfError: if no client ID found for hostname.


def do_tortoisehg_report(repos, output):

    import operator
    import xml.etree.ElementTree as ET

    root = ET.Element('reporegistry')
    item = ET.SubElement(root, 'treeitem')

    group = ET.SubElement(item, 'group', attrib=Dict(name='groupname'))

    def fullname_to_shortname(fullname):

        shortname = fullname.replace(os.environ['HOME'], '~')
        shortname = shortname.lstrip('./')
        return shortname

    for repo in sorted(repos, key=operator.attrgetter('fpath')):
        fullname = os.path.join(
            os.path.dirname(repo.fpath),
            os.path.basename(repo.fpath))
        shortname = fullname_to_shortname(fullname)
        if repo.prefix != '.hg':
            shortname = "%s%s" % (shortname, repo.prefix)
        _ = ET.SubElement(group, 'repo',
                          attrib=Dict(
                              root=repo.fpath,
                              shortname=shortname,
                              basenode='0'*40))
        _
    print('<?xml version="1.0" encoding="UTF-8"?>', file=output)
    print("<!-- autogenerated: %s -->" % "TODO", file=output)
    print(ET.dump(root), file=output)

Generate a thg-reporegistry.xml file from a list of repos and print
to output

Args:
repos (iterable): iterable of Repository subclass instances
output (writeable): output stream to which THG XML will be printed


def set(self, option, value=None):
    option = self._container.optionxform(option)
    if (option in self.options()):
        self.__getitem__(option).value = value
    else:
        self.__setitem__(option, value)
    return self

Set an option for chaining.

Args:
option (str): option name
value (str): value, default None


def get_output_shape_at(self, node_index):
    return self._get_node_attribute_at_index(node_index, 'output_shapes', 'output shape')

Retrieves the output shape(s) of a layer at a given node.

Args:
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.

Returns:
A shape tuple
(or list of shape tuples if the layer has multiple outputs).

Raises:
RuntimeError: If called in Eager mode.


def tile_and_concat(image, latent, concat_latent=True):
    if (not concat_latent):
        return image
    image_shape = common_layers.shape_list(image)
    latent_shape = common_layers.shape_list(latent)
    (height, width) = (image_shape[1], image_shape[2])
    latent_dims = latent_shape[1]
    height_multiples = (height
    pad = (height - (height_multiples * latent_dims))
    latent = tf.reshape(latent, ((- 1), latent_dims, 1, 1))
    latent = tf.tile(latent, (1, height_multiples, width, 1))
    latent = tf.pad(latent, [[0, 0], [(pad
    return tf.concat([image, latent], axis=(- 1))

Tile latent and concatenate to image across depth.

Args:
image: 4-D Tensor, (batch_size X height X width X channels)
latent: 2-D Tensor, (batch_size X latent_dims)
concat_latent: If set to False, the image is returned as is.

Returns:
concat_latent: 4-D Tensor, (batch_size X height X width X channels+1)
latent tiled and concatenated to the image across the channels.


def contains_reference_without_get_key(node: AbstractSyntaxTree) -> bool:
    if isinstance(node, Identifier) and isinstance(node.data_type, _fhir_path_data_types.StructureDataType) and (node.data_type.element_type == 'Reference'):
        if not node.has_parent or not isinstance(node.parent, Invocation):
            return True
        if node.parent.lhs == node:
            parent_invocation = node.parent
        else:
            if not node.parent.has_parent or not isinstance(node.parent.parent, Invocation):
                return True
            parent_invocation = node.parent.parent
        if not isinstance(parent_invocation.rhs, Function) or parent_invocation.rhs.identifier.value != 'getReferenceKey':
            return True
    for child in node.children or ():
        if contains_reference_without_get_key(child):
            return True
    return False

Checks if the AST contains a reference without a getReferenceKey call.

Args:
node: The root node of the abstract syntax tree to search.

Returns:
True if the abstract syntax tree contains an identifier to a reference
without a getReferenceKey call against it. False otherwise.


def symmetric_difference(self, other):
    other = self._as_multiset(other)
    result = self.__class__()
    _total = 0
    _elements = result._elements
    self_elements = self._elements
    other_elements = other._elements
    dist_elements = (set(self_elements.keys()) | set(other_elements.keys()))
    for element in dist_elements:
        multiplicity = self_elements.get(element, 0)
        other_multiplicity = other_elements.get(element, 0)
        new_multiplicity = ((multiplicity - other_multiplicity) if (multiplicity > other_multiplicity) else (other_multiplicity - multiplicity))
        _total += new_multiplicity
        if (new_multiplicity > 0):
            _elements[element] = new_multiplicity
    result._total = _total
    return result

r"""Return a new set with elements in either the set or other but not both.

>>> ms = Multiset('aab')
>>> sorted(ms.symmetric_difference('abc'))
['a', 'c']

You can also use the ``^`` operator for the same effect. However, the operator version
will only accept a set as other operator, not any iterable, to avoid errors.

>>> ms = Multiset('aab')
>>> sorted(ms ^ Multiset('aaac'))
['a', 'b', 'c']

For a variant of the operation which modifies the multiset in place see
:meth:`symmetric_difference_update`.

Args:
other: The other set to take the symmetric difference with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].

Returns:
The resulting symmetric difference multiset.


def MakeSuiteFromList(t, name=''):
    hist = MakeHistFromList(t)
    d = hist.GetDict()
    return MakeSuiteFromDict(d)

Makes a suite from an unsorted sequence of values.

Args:
t: sequence of numbers
name: string name for this suite

Returns:
Suite object


def _lease_valid(self, lease):

        if not lease.exist:
            return None

        if lease.has_env:
            return lease.uuid_path
        else:
            self._release(lease)
            return None

Check if the given lease exist and still has a prefix that owns it.
If the lease exist but its prefix isn't, remove the lease from this
store.

Args:
lease (lago.subnet_lease.Lease): Object representation of the
lease

Returns:
str or None: If the lease and its prefix exists, return the path
to the uuid of the prefix, else return None.


async def _get_or_fetch_conversation(self, conv_id):

        conv = self._conv_dict.get(conv_id, None)
        if conv is None:
            logger.info('Fetching unknown conversation %s', conv_id)
            res = await self._client.get_conversation(
                hangouts_pb2.GetConversationRequest(
                    request_header=self._client.get_request_header(),
                    conversation_spec=hangouts_pb2.ConversationSpec(
                        conversation_id=hangouts_pb2.ConversationId(
                            id=conv_id
                        )
                    ), include_event=False
                )
            )
            conv_state = res.conversation_state
            event_cont_token = None
            if conv_state.HasField('event_continuation_token'):
                event_cont_token = conv_state.event_continuation_token
            return self._add_conversation(conv_state.conversation,
                                          event_cont_token=event_cont_token)
        else:
            return conv

Get a cached conversation or fetch a missing conversation.

Args:
conv_id: string, conversation identifier

Raises:
NetworkError: If the request to fetch the conversation fails.

Returns:
:class:`.Conversation` with matching ID.


def weighting_function(max_num_bins: int, up: torch.Tensor, reg_scale: int) -> torch.Tensor:
    upper_bound1 = abs(up[0]) * abs(reg_scale)
    upper_bound2 = abs(up[0]) * abs(reg_scale) * 2
    step = (upper_bound1 + 1) ** (2 / (max_num_bins - 2))
    left_values = [-step ** i + 1 for i in range(max_num_bins
    right_values = [step ** i - 1 for i in range(1, max_num_bins
    values = [-upper_bound2] + left_values + [torch.zeros_like(up[0][None])] + right_values + [upper_bound2]
    values = torch.cat(values, 0)
    return values

Generates the non-uniform Weighting Function W(n) for bounding box regression.

Args:
max_num_bins (int): Max number of the discrete bins.
up (Tensor): Controls upper bounds of the sequence,
where maximum offset is ±up * H / W.
reg_scale (float): Controls the curvature of the Weighting Function.
Larger values result in flatter weights near the central axis W(max_num_bins/2)=0
and steeper weights at both ends.
Returns:
Tensor: Sequence of Weighting Function.


def delete_plan(self, plan_code):

        return self.client._delete(self.url + 'plans/{}'.format(plan_code), headers=self.get_headers())

Delete an entire subscription plan associated with the merchant.

Args:
plan_code: Plan’s identification code for the merchant.

Returns:


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

    return np.minimum(item_a.closest_distance(time_a, item_b, time_b), max_value) / float(max_value)

Euclidean distance between the pixels in item_a and item_b closest to each other.

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

Returns:
Distance value between 0 and 1.


class XLNetPoolerAnswerClass(nn.Module):

    def __init__(self, config: XLNetConfig):
        super().__init__()
        self.dense_0 = nn.Linear(config.hidden_size * 2, config.hidden_size)
        self.activation = nn.Tanh()
        self.dense_1 = nn.Linear(config.hidden_size, 1, bias=False)

    def forward(self, hidden_states: torch.FloatTensor, start_states: Optional[torch.FloatTensor]=None, start_positions: Optional[torch.LongTensor]=None, cls_index: Optional[torch.LongTensor]=None) -> torch.FloatTensor:

        hsz = hidden_states.shape[-1]
        assert start_states is not None or start_positions is not None, 'One of start_states, start_positions should be not None'
        if start_positions is not None:
            start_positions = start_positions[:, None, None].expand(-1, -1, hsz)
            start_states = hidden_states.gather(-2, start_positions).squeeze(-2)
        if cls_index is not None:
            cls_index = cls_index[:, None, None].expand(-1, -1, hsz)
            cls_token_state = hidden_states.gather(-2, cls_index).squeeze(-2)
        else:
            cls_token_state = hidden_states[:, -1, :]
        x = self.dense_0(torch.cat([start_states, cls_token_state], dim=-1))
        x = self.activation(x)
        x = self.dense_1(x).squeeze(-1)
        return x

Compute SQuAD 2.0 answer class from classification and start tokens hidden states.

Args:
config ([`XLNetConfig`]):
The config used by the model, will be used to grab the `hidden_size` of the model.


def log_correction(self, event, action):
    action = str(action)
    self.history.info(action)
    self._corrections.append(dict(event=event.as_dict(), action=action))

This method should be called once we have fixed the problem associated to this event.
It adds a new entry in the correction history of the node.

Args:
event: :class:`AbinitEvent` that triggered the correction.
action (str): Human-readable string with info on the action perfomed to solve the problem.


def _create_and_save_file_init_hash_table_model_tf1(self, output_path: str, tags: Collection[str], signature_def_key: str) -> Tuple[Mapping[str, core.Tensor], Mapping[str, core.Tensor]]:
    with session.Session(graph=ops.Graph()) as sess:
        input_vocabs_placeholder, lookup_tensor, output_tensor = self._create_table_init_from_file_model_tf1(sess)
        inputs = {'input_vocabs': input_vocabs_placeholder}
        outputs = {'lookup': lookup_tensor, 'output': output_tensor}
        self._save_tf1_model(sess, output_path, signature_def_key, tags, inputs=inputs, outputs=outputs, init_op=lookup_ops.tables_initializer(), assets_collection=ops.get_collection(ops.GraphKeys.ASSET_FILEPATHS))
    return (inputs, outputs)

Creates and saves a model that uses a file-initialized table.

The asset file "vocab_file.txt" is used to initialize a hash table.

Args:
output_path: Path to the directory to save the created model.
tags: Set of strings that identifies the saved meta graph.
signature_def_key: Name of the SignatureDef. Used to identify the
SignatureDef within the meta graph.

Returns:
inputs: A mapping of input_key -> input_tensor (placeholder). The input
key is "input_vocabs".
outputs: A mapping of output_key -> output_tensor. The output keys are
"lookup" and "output".


def generate(self, id_or_uri):
    uri = (self._client.build_uri(id_or_uri) + '/generate')
    return self._client.get(uri)

Generates and returns a random range.

Args:
id_or_uri:
ID or URI of range.

Returns:
dict: A dict containing a list with IDs.


def _get_definitions(source):
    max_len = 0
    descs = collections.OrderedDict()
    lines = (s.strip() for s in source.splitlines())
    non_empty_lines = (s for s in lines if s)
    for line in non_empty_lines:
        if line:
            (arg, desc) = re.split('\\s\\s+', line.strip())
            arg_len = len(arg)
            if (arg_len > max_len):
                max_len = arg_len
            descs[arg] = desc
    return (descs, max_len)

Extract a dictionary of arguments and definitions.

Args:
source: The source for a section of a usage string that contains
definitions.

Returns:
A two-tuple containing a dictionary of all arguments and definitions as
well as the length of the longest argument.


def has_same_sumformula(self, other):

        same_atoms = True
        for atom in set(self['atom']):
            own_atom_number = len(self[self['atom'] == atom])
            other_atom_number = len(other[other['atom'] == atom])
            same_atoms = (own_atom_number == other_atom_number)
            if not same_atoms:
                break
        return same_atoms

Determines if ``other``  has the same sumformula

Args:
other (molecule):

Returns:
bool:


def unzip(x, split_dim, current_length, num_splits=2, name=None):

  with tf.name_scope(name, 'unzip', [x]) as scope:
    x = tf.convert_to_tensor(x, name='x')

    all_splits = tf.split(
        value=x, num_or_size_splits=current_length, axis=split_dim, name=scope)
    splits = [[] for _ in xrange(num_splits)]
    for i in xrange(current_length):
      splits[i % num_splits].append(all_splits[i])
    return [tf.concat(s, split_dim) for s in splits]

Splits a tensor by unzipping along the split_dim.

For example the following array split into 2 would be:
[1, 2, 3, 4, 5, 6] -> [1, 3, 5], [2, 4, 6]
and by 3:
[1, 2, 3, 4] -> [1, 4], [2], [3]

Args:
x: The tensor to split.
split_dim: The dimension to split along.
current_length: Current length along the split_dim.
num_splits: The number of splits.
name: Optional name for this op.
Returns:
A length num_splits sequence.


def set_size(self, w, h):

        self.attributes['width'] = str(w)
        self.attributes['height'] = str(h)

Sets the rectangle size.

Args:
w (int): width of the rectangle
h (int): height of the rectangle


def map_exp_ids(self, exp):

        names = self.exp_feature_names
        if self.discretized_feature_names is not None:
            names = self.discretized_feature_names
        return [(names[x[0]], x[1]) for x in exp]

Maps ids to feature names.

Args:
exp: list of tuples [(id, weight), (id,weight)]

Returns:
list of tuples (feature_name, weight)


def download_url(self, url, **kwargs):

        if self.baseurl and ':
            url = join(self.baseurl, url)
        return self.resolver.download_to_directory(self.directory, url, **kwargs)

Download a URL to the workspace.

Args:
url (string): URL to download to directory
**kwargs : See :py:mod:`ocrd.resolver.Resolver`

Returns:
The local filename of the downloaded file


def _file_size(self, field):

        size = 0
        try:
            handle = open(self._files[field], "r")
            size = os.fstat(handle.fileno()).st_size
            handle.close()
        except:
            size = 0
        self._file_lengths[field] = size
        return self._file_lengths[field]

Returns the file size for given file field.

Args:
field (str): File field

Returns:
int. File size


def from_row_and_group(row: int, group: int):

        for sym in _pt_data.keys():
            el = Element(sym)
            if el.row == row and el.group == group:
                return el
        raise ValueError("No element with this row and group!")

Returns an element from a row and group number.

Args:
row (int): Row number
group (int): Group number

.. note::
The 18 group number system is used, i.e., Noble gases are group 18.


def fit_to_structure(self, structure, symprec=0.1):
    sga = SpacegroupAnalyzer(structure, symprec)
    symm_ops = sga.get_symmetry_operations(cartesian=True)
    return (sum([self.transform(symm_op) for symm_op in symm_ops]) / len(symm_ops))

Returns a tensor that is invariant with respect to symmetry
operations corresponding to a structure

Args:
structure (Structure): structure from which to generate
symmetry operations
symprec (float): symmetry tolerance for the Spacegroup Analyzer
used to generate the symmetry operations


def hgnc_id(self, hgnc_symbol, build='37'):

        query = {'hgnc_symbol':hgnc_symbol, 'build':build}
        projection = {'hgnc_id':1, '_id':0}
        res = self.hgnc_collection.find(query, projection)

        if res.count() > 0:
            return res[0]['hgnc_id']
        else:
            return None

Query the genes with a hgnc symbol and return the hgnc id

Args:
hgnc_symbol(str)
build(str)

Returns:
hgnc_id(int)


def _has_old_request_ended(self, shard_state):
    assert (shard_state.slice_start_time is not None)
    assert (shard_state.slice_request_id is not None)
    request_ids = [shard_state.slice_request_id]
    logs = None
    try:
        logs = list(logservice.fetch(request_ids=request_ids))
    except (apiproxy_errors.FeatureNotEnabledError, apiproxy_errors.CapabilityDisabledError) as e:
        logging.warning('Ignoring exception: %s', e)
    if ((not logs) or (not logs[0].finished)):
        return False
    return True

Whether previous slice retry has ended according to Logs API.

Args:
shard_state: shard state.

Returns:
True if the request of previous slice retry has ended. False if it has
not or unknown.


def __init__(self,
                 tablename: str = None,
                 table: Table = None,
                 metadata: MetaData = None) -> None:

        assert table is not None or tablename, "No table information provided"
        assert not (tablename and table is not None), (
            "Specify either table or tablename, not both")
        self._table = table
        self._tablename = tablename
        self._metadata = metadata

Initialize with either ``tablename`` or ``table``, not both.

Args:
tablename: string name of the table
table: SQLAlchemy :class:`Table` object
metadata: optional :class:`MetaData` object


def StopTiming(self, profile_name):

    measurements = self._profile_measurements.get(profile_name)
    if measurements:
      measurements.SampleStop()

      sample = '{0:f}\t{1:s}\t{2:f}\n'.format(
          measurements.start_sample_time, profile_name,
          measurements.total_cpu_time)
      self._WritesString(sample)

Stops timing CPU time.

Args:
profile_name (str): name of the profile to sample.


class FlaxForcedEOSTokenLogitsProcessor(FlaxLogitsProcessor):

    def __init__(self, max_length: int, eos_token_id: int):
        self.max_length = max_length
        self.eos_token_id = eos_token_id

    def __call__(self, input_ids: jnp.ndarray, scores: jnp.ndarray, cur_len: int) -> jnp.ndarray:
        new_scores = jnp.full(scores.shape, -float('inf'))
        apply_penalty = 1 - jnp.bool_(cur_len - self.max_length + 1)
        scores = jnp.where(apply_penalty, new_scores.at[:, self.eos_token_id].set(0), scores)
        return scores

[`FlaxLogitsProcessor`] that enforces the specified token as the last generated token when `max_length` is reached.

Args:
max_length (`int`):
The maximum length of the sequence to be generated.
eos_token_id (`int`):
The id of the token to force as the last generated token when `max_length` is reached.


def package_releases(self, project_name):

        try:
            return self._connection.package_releases(project_name)
        except Exception as err:
            raise PyPIClientError(err)

Retrieve the versions from PyPI by ``project_name``.

Args:
project_name (str): The name of the project we wish to retrieve
the versions of.

Returns:
list: Of string versions.


async def leave_conversation(self, conv_id):
    logger.info('Leaving conversation: {}'.format(conv_id))
    (await self._conv_dict[conv_id].leave())
    del self._conv_dict[conv_id]

Leave a conversation.

Args:
conv_id (str): ID of conversation to leave.


def reduce_to_2d(arr):
    if (not isinstance(arr, np.ndarray)):
        raise ValueError('reduce_to_2d requires a numpy.ndarray')
    ndims = len(arr.shape)
    if (ndims < 2):
        raise ValueError('reduce_to_2d requires an array of dimensionality >=2')
    slices = (([0] * (ndims - 2)) + [slice(None), slice(None)])
    return arr[slices]

Given a np.npdarray with nDims > 2, reduce it to 2d.

It does this by selecting the zeroth coordinate for every dimension greater
than two.

Args:
arr: a numpy ndarray of dimension at least 2.

Returns:
A two-dimensional subarray from the input array.

Raises:
ValueError: If the argument is not a numpy ndarray, or the dimensionality
is too low.


def _MakeCacheInvariant(self, urn, age):

    precondition.AssertType(urn, Text)
    return "%s:%s" % (urn, self.ParseAgeSpecification(age))

Returns an invariant key for an AFF4 object.

The object will be cached based on this key. This function is specifically
extracted to ensure that we encapsulate all security critical aspects of the
AFF4 object so that objects do not leak across security boundaries.

Args:
urn: The urn of the object.
age: The age policy used to build this object. Should be one of
ALL_TIMES, NEWEST_TIME or a range.

Returns:
A key into the cache.


def config(self, name="skype"):

        self.conn("PUT", "{0}/users/ME/endpoints/{1}/presenceDocs/messagingService"
                         .format(self.conn.msgsHost, self.id),
                  auth=SkypeConnection.Auth.RegToken,
                  json={"id": "messagingService",
                        "type": "EndpointPresenceDoc",
                        "selfLink": "uri",
                        "privateInfo": {"epname": name},
                        "publicInfo": {"capabilities": "",
                                       "type": 1,
                                       "skypeNameVersion": "skype.com",
                                       "nodeInfo": "xx",
                                       "version": "908/1.30.0.128"}})

Configure this endpoint to allow setting presence.

Args:
name (str): display name for this endpoint


def _process_query(self, query, prepared=False):

        if prepared is True:
            files = {'query': str(query)}

            logger.debug('About to submit the following query {}'.format(query))

            res, status = self.post(
                self.disambiguate_service,
                files=files,
                headers={'Accept': 'application/json'},
            )

            if status == 200:
                return self.decode(res), status
            else:
                logger.debug('Disambiguation failed.')
                return None, status

        text = query['text']

        sentence_coordinates = [
            {
                "offsetStart": 0,
                "offsetEnd": len(text)
            }
        ]

        total_nb_sentences = len(sentence_coordinates)
        sentences_groups = []

        if len(text) > self.max_text_length:
            res, status_code = self.segment(text)

            if status_code == 200:
                sentence_coordinates = res['sentences']
                total_nb_sentences = len(sentence_coordinates)
            else:
                logger.error('Error during the segmentation of the text.')

            logger.debug(
                'Text too long, split in {} sentences; building groups of {} '
                'sentences.'.format(
                    total_nb_sentences, self.sentences_per_group
                )
            )
            sentences_groups = self._group_sentences(
                total_nb_sentences,
                self.sentences_per_group
            )
        else:
            query['sentence'] = "true"

        if total_nb_sentences > 1:
            query['sentences'] = sentence_coordinates

        if len(sentences_groups) > 0:
            for group in sentences_groups:
                query['processSentence'] = group

                res, status_code = self._process_query(query, prepared=True)

                if status_code == 200:
                    if 'entities' in res:
                        query['entities'] = res[u'entities']
                    query['language'] = res[u'language']
                else:
                    logger.error(
                        "Error when processing the query {}".format(query)
                    )
                    return None, status_code

        else:
            res, status_code = self._process_query(query, prepared=True)

            if status_code == 200:
                query['language'] = res[u'language']
                if 'entities' in res:
                    query['entities'] = res[u'entities']
            else:
                logger.error("Error when processing the query {}".format(query))
                return None, status_code

        return query, status_code

Process query recursively, if the text is too long,
it is split and processed bit a bit.

Args:
query (sdict): Text to be processed.
prepared (bool): True when the query is ready to be submitted via
POST request.
Returns:
str: Body ready to be submitted to the API.


def Execute(cmd, args, time_limit=(- 1), bypass_whitelist=False, daemon=False, use_client_context=False, cwd=None):
    if ((not bypass_whitelist) and (not IsExecutionWhitelisted(cmd, args))):
        logging.info('Execution disallowed by whitelist: %s %s.', cmd, ' '.join(args))
        return (b'', b'Execution disallowed by whitelist.', (- 1), (- 1))
    if daemon:
        pid = os.fork()
        if (pid == 0):
            try:
                os.setsid()
            except OSError:
                pass
            _Execute(cmd, args, time_limit, use_client_context=use_client_context, cwd=cwd)
            os._exit(0)
    else:
        return _Execute(cmd, args, time_limit, use_client_context=use_client_context, cwd=cwd)

Executes commands on the client.

This function is the only place where commands will be executed
by the GRR client. This makes sure that all issued commands are compared to a
white list and no malicious commands are issued on the client machine.

Args:
cmd: The command to be executed.
args: List of arguments.
time_limit: Time in seconds the process is allowed to run.
bypass_whitelist: Allow execution of things that are not in the whitelist.
Note that this should only ever be called on a binary that passes the
VerifySignedBlob check.
daemon: Start the new process in the background.
use_client_context: Run this script in the client's context. Defaults to
system context.
cwd: Current working directory for the command.

Returns:
A tuple of stdout, stderr, return value and time taken.


def _parse_volumes(volume_values: dict) -> str:
    for v_values in volume_values:
        for (v_key, v_value) in v_values.items():
            if (v_key == 'source'):
                if (v_value == '.'):
                    source = os.path.dirname(os.path.abspath(__file__))
                else:
                    source = v_value
            if (v_key == 'target'):
                target = v_value
        volume_spec = [((source + ':') + target)]
        return volume_spec

Parse volumes key.

Args:
volume_values (dict): volume configuration values

Returns:
string, volume specification with mount source and container path


def set_tag(self, key, value, update_session=True):
    existing_tags = {x.key: x for x in self.tags}
    if (key in existing_tags):
        tag = existing_tags[key]
        if (tag.value == value):
            return False
        tag.value = value
    else:
        tag = Tag()
        tag.resource_id = self.id
        tag.key = key
        tag.value = value
        self.tags.append(tag)
    if update_session:
        db.session.add(tag)
    return True

Create or set the value of the tag with `key` to `value`. Returns `True` if the tag was created or updated or
`False` if there were no changes to be made.

Args:
key (str): Key of the tag
value (str): Value of the tag
update_session (bool): Automatically add the change to the SQLAlchemy session. Default: True

Returns:
`bool`


def WriteToPath(obj, filepath):
    with io.open(filepath, mode='w', encoding='utf-8') as filedesc:
        WriteToFile(obj, filedesc)

Serializes and writes given Python object to the specified YAML file.

Args:
obj: A Python object to serialize.
filepath: A path to the file into which the object is to be written.


def run_cgmlst(blast_runner, full=False):

    from sistr.src.serovar_prediction.constants import genomes_to_serovar

    df_cgmlst_profiles = ref_cgmlst_profiles()

    logging.debug('{} distinct cgMLST330 profiles'.format(df_cgmlst_profiles.shape[0]))

    logging.info('Running BLAST on serovar predictive cgMLST330 alleles')
    cgmlst_fasta_path = CGMLST_CENTROID_FASTA_PATH if not full else CGMLST_FULL_FASTA_PATH
    blast_outfile = blast_runner.blast_against_query(cgmlst_fasta_path)
    logging.info('Reading BLAST output file "{}"'.format(blast_outfile))
    blast_reader = BlastReader(blast_outfile)
    if blast_reader.df is None:
        logging.error('No cgMLST330 alleles found!')
        return ({'distance': 1.0,
            'genome_match': None,
            'serovar': None,
            'matching_alleles': 0,
            'subspecies': None,
            'cgmlst330_ST': None,},
                {}, )
    logging.info('Found {} cgMLST330 allele BLAST results'.format(blast_reader.df.shape[0]))

    df_cgmlst_blastn = process_cgmlst_results(blast_reader.df)

    marker_match_results = matches_to_marker_results(df_cgmlst_blastn[df_cgmlst_blastn.is_match])
    contig_blastn_records = alleles_to_retrieve(df_cgmlst_blastn)
    retrieved_marker_alleles = get_allele_sequences(blast_runner.fasta_path,
                                                    contig_blastn_records,
                                                    full=full)
    logging.info('Type retrieved_marker_alleles %s', type(retrieved_marker_alleles))
    all_marker_results = marker_match_results.copy()
    for marker, res in retrieved_marker_alleles.items():
        all_marker_results[marker] = res
    for marker in df_cgmlst_profiles.columns:
        if marker not in all_marker_results:
            all_marker_results[marker] = {'blast_result': None,
                                          'name': None,
                                          'seq': None,}
    cgmlst_results = {}
    for marker, res in all_marker_results.items():
        try:
            cgmlst_results[marker] = int(res['name'])
        except:
            logging.error('Missing cgmlst_results for %s', marker)
            logging.debug(res)

    logging.info('Calculating number of matching alleles to serovar predictive cgMLST330 profiles')
    df_relatives = find_closest_related_genome(cgmlst_results, df_cgmlst_profiles)
    genome_serovar_dict = genomes_to_serovar()
    df_relatives['serovar'] = [genome_serovar_dict[genome] for genome in df_relatives.index]
    logging.debug('Top 5 serovar predictive cgMLST profiles:\n{}'.format(df_relatives.head()))
    spp = None
    subspeciation_tuple = cgmlst_subspecies_call(df_relatives)
    if subspeciation_tuple is not None:
        spp, distance, spp_counter = subspeciation_tuple
        logging.info('Top subspecies by cgMLST is "{}" (min dist={}, Counter={})'.format(spp, distance, spp_counter))
    else:
        logging.warning('Subspeciation by cgMLST was not possible!')

    cgmlst_serovar = None
    cgmlst_matching_genome = None
    cgmlst_matching_alleles = 0
    cgmlst_distance = 1.0
    for idx, row in df_relatives.iterrows():
        cgmlst_distance = row['distance']
        cgmlst_matching_alleles = row['matching']
        cgmlst_serovar = row['serovar'] if cgmlst_distance <= 1.0 else None
        cgmlst_matching_genome = idx if cgmlst_distance <= 1.0 else None
        logging.info('Top serovar by cgMLST profile matching: "{}" with {} matching alleles, distance={:.1%}'.format(
        cgmlst_serovar,
        cgmlst_matching_alleles,
        cgmlst_distance
    ))
        break

    cgmlst_st = None
    cgmlst_markers_sorted = sorted(all_marker_results.keys())
    cgmlst_allele_names = []
    marker = None
    for marker in cgmlst_markers_sorted:
        try:
            aname = all_marker_results[marker]['name']
            if aname:
                cgmlst_allele_names.append(str(aname))
            else:
                break
        except:
            break
    if len(cgmlst_allele_names) == len(cgmlst_markers_sorted):
        cgmlst_st = allele_name('-'.join(cgmlst_allele_names))
        logging.info('cgMLST330 Sequence Type=%s', cgmlst_st)
    else:
        logging.warning('Could not compute cgMLST330 Sequence Type due to missing data (marker %s)', marker)
    return ({'distance': cgmlst_distance,
            'genome_match': cgmlst_matching_genome,
            'serovar': cgmlst_serovar,
            'matching_alleles': cgmlst_matching_alleles,
            'subspecies': spp,
            'cgmlst330_ST': cgmlst_st,},
           all_marker_results, )

Perform in silico cgMLST on an input genome

Args:
blast_runner (sistr.src.blast_wrapper.BlastRunner): blastn runner object with genome fasta initialized

Returns:
dict: cgMLST ref genome match, distance to closest ref genome, subspecies and serovar predictions
dict: marker allele match results (seq, allele name, blastn results)


def save_model(self, fname, pretty=False):

        with open(fname, "w") as f:
            xml_str = ET.tostring(self.root, encoding="unicode")
            if pretty:

                parsed_xml = xml.dom.minidom.parseString(xml_str)
                xml_str = parsed_xml.toprettyxml(newl="")
            f.write(xml_str)

Saves the xml to file.

Args:
fname: output file location
pretty: attempts!! to pretty print the output


def get_restore_path(filename):

    path = os.path.join(RESTORE_DIRECTORY, FILE_STORE_LOCATION)
    if not os.path.exists(path):
        os.makedirs(path)
    return os.path.join(path, filename + '.pickle')

get_restore_path: returns path to directory for restoration points
Args:
filename (str): Name of file to store
Returns: string path to file


def assign_sub(self, variable, value):
    variable.assign_sub(value)

Subtract a value from a variable.

This should be used in optimizers instead of
`variable.assign_sub(value)` to support backend specific optimizations.
Note that the variable can be a model variable or an optimizer variable;
it can be a backend native variable or a Keras variable.

Args:
variable: The variable to update.
value: The value to add to the variable.


def _time_to_datetime(value):
    if (not isinstance(value, datetime.time)):
        raise TypeError(('Cannot convert to datetime expected time value; received %s' % value))
    return datetime.datetime(1970, 1, 1, value.hour, value.minute, value.second, value.microsecond)

Convert a time to a datetime for Cloud Datastore storage.

Args:
value: A datetime.time object.

Returns:
A datetime object with date set to 1970-01-01.


def _write_custom_summaries(self, step, logs=None):
    logs = logs or {}
    if context.executing_eagerly():
        with self.writer.as_default(), summary_ops_v2.record_if(True):
            for name, value in logs.items():
                if isinstance(value, np.ndarray):
                    value = value.item()
                summary_ops_v2.scalar(name, value, step=step)
    else:
        for name, value in logs.items():
            if isinstance(value, np.ndarray):
                value = value.item()
            summary = tf_summary.Summary()
            summary_value = summary.value.add()
            summary_value.simple_value = value
            summary_value.tag = name
            self.writer.add_summary(summary, step)
    self.writer.flush()

Writes metrics out as custom scalar summaries.

Args:
step: the global step to use for TensorBoard.
logs: dict. Keys are scalar summary names, values are
NumPy scalars.


def __init__(self, primitive_handler_: primitive_handler.PrimitiveHandler, generator: _JsonTextGenerator, json_format: _FhirJsonFormat) -> None:
    self.primitive_handler = primitive_handler_
    self.generator = generator
    self.json_format = json_format

Creates a new instance of JsonPrinter.

Note that this is for *internal-use* only. External clients should leverage
one of the available class constructors such as:
`JsonPrinter.pretty_printer(...)`, `JsonPrinter.compact_printer()`, etc.

Args:
primitive_handler_: Responsible for returning PrimitiveWrappers.
generator: The type of _JsonTextGenerator used to handle whitespace and
newline additions.
json_format: The style of FHIR JSON to output.


def ground_temperature_depth(self, value=None):

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

        self._ground_temperature_depth = value

Corresponds to IDD Field `ground_temperature_depth`

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

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


def get_shell_code(self, shell=None, parent_environ=None, style=OutputStyle.file):
    executor = self._create_executor(interpreter=create_shell(shell), parent_environ=parent_environ)
    if (self.load_path and os.path.isfile(self.load_path)):
        executor.env.REZ_RXT_FILE = self.load_path
    self._execute(executor)
    return executor.get_output(style)

Get the shell code resulting from intepreting this context.

Args:
shell (str): Shell type, for eg 'bash'. If None, the current shell
type is used.
parent_environ (dict): Environment to interpret the context within,
defaults to os.environ if None.
style (): Style to format shell code in.


def remove_segments(self, segments_to_remove):
    v_ind = self.vertex_indices_in_segments(segments_to_remove)
    self.segm = {name: faces for (name, faces) in self.segm.iteritems() if (name not in segments_to_remove)}
    self.remove_vertices(v_ind)

Remove the faces and vertices for given segments, keeping all others.

Args:
segments_to_remove: a list of segnments whose vertices will be removed


def minimum(self):
    return min([(x, energy) for (_, x, energy, _, _) in self.get_kinks()], key=(lambda i: i[1]))

Finds the minimum reaction energy E_min and corresponding
mixing ratio x_min.

Returns:
Tuple (x_min, E_min).


class Identity(Initializer):

    def __init__(self, gain=1.0):
        self.gain = gain

    def __call__(self, shape, dtype=None, **kwargs):

        _validate_kwargs(self.__class__.__name__, kwargs, support_partition=False)
        dtype = _assert_float_dtype(_get_dtype(dtype))
        if len(shape) != 2:
            raise ValueError('Identity matrix initializer can only be used for 2D matrices.')
        initializer = linalg_ops.eye(*shape, dtype=dtype)
        return self.gain * initializer

    def get_config(self):
        return {'gain': self.gain}

Initializer that generates the identity matrix.

Also available via the shortcut function `tf.keras.initializers.identity`.

Only usable for generating 2D matrices.

Examples:

>>> # Standalone usage:
>>> initializer = tf.keras.initializers.Identity()
>>> values = initializer(shape=(2, 2))

>>> # Usage in a Keras layer:
>>> initializer = tf.keras.initializers.Identity()
>>> layer = tf.keras.layers.Dense(3, kernel_initializer=initializer)

Args:
gain: Multiplicative factor to apply to the identity matrix.


def connect(self, **kwargs):
    self.app = self._app.connect(**kwargs)
    try:
        self._top_window = self.app.top_window().wrapper_object()
        self.set_foreground()
    except RuntimeError:
        self._top_window = None

Connect to window and set it foreground

Args:
**kwargs: optional arguments

Returns:
None


def fromFile(cls, filename):

		oFile = open(filename)

		dDetails = JSON.decodef(oFile)

		return cls(dDetails)

From File

Loads a JSON file and creates a Node instance from it

Args:
filename (str): The filename to load

Returns:
_NodeInterface


def parse(file_contents, file_name):
    try:
        yaml.load(file_contents)
    except Exception:
        (_, exc_value, _) = sys.exc_info()
        return 'Cannot Parse: {file_name}: \n {exc_value}'.format(file_name=file_name, exc_value=exc_value)

This takes a list of filenames and their paths of expected yaml files and
tried to parse them, erroring if there are any parsing issues.

Args:
file_contents (str): Contents of a yml file

Raises:
yaml.parser.ParserError: Raises an error if the file contents cannot be
parsed and interpreted as yaml


def show_in_external_file_explorer(fnames=None):

    if not isinstance(fnames, (tuple, list)):
        fnames = [fnames]
    for fname in fnames:
        open_file_in_external_explorer(fname)

Show files in external file explorer

Args:
fnames (list): Names of files to show.


def solid_angle(center, coords):

    r = [np.subtract(c, center) for c in coords]

    r_norm = [np.linalg.norm(i) for i in r]

    angle = 0
    for i in range(1, len(r) - 1):
        j = i + 1
        tp = np.abs(np.dot(r[0], np.cross(r[i], r[j])))
        de = r_norm[0] * r_norm[i] * r_norm[j] + \
             r_norm[j] * np.dot(r[0], r[i]) + \
             r_norm[i] * np.dot(r[0], r[j]) + \
             r_norm[0] * np.dot(r[i], r[j])
        if de == 0:
            my_angle = 0.5 * pi if tp > 0 else -0.5 * pi
        else:
            my_angle = np.arctan(tp / de)
        angle += (my_angle if my_angle > 0 else my_angle + np.pi) * 2

    return angle

Helper method to calculate the solid angle of a set of coords from the
center.

Args:
center (3x1 array): Center to measure solid angle from.
coords (Nx3 array): List of coords to determine solid angle.

Returns:
The solid angle.


def add_dimension(self, dimension, dim_pos, dim_val, vdim=False, **kwargs):
    dimension = asdim(dimension)
    if (dimension in self.dimensions()):
        raise Exception('{dim} dimension already defined'.format(dim=dimension.name))
    if (vdim and self._deep_indexable):
        raise Exception('Cannot add value dimension to object that is deep indexable')
    if vdim:
        dims = self.vdims[:]
        dims.insert(dim_pos, dimension)
        dimensions = dict(vdims=dims)
        dim_pos += self.ndims
    else:
        dims = self.kdims[:]
        dims.insert(dim_pos, dimension)
        dimensions = dict(kdims=dims)
    if (isinstance(dim_val, basestring) or (not hasattr(dim_val, '__iter__'))):
        dim_val = cycle([dim_val])
    elif (not (len(dim_val) == len(self))):
        raise ValueError('Added dimension values must be same lengthas existing keys.')
    items = OrderedDict()
    for (dval, (key, val)) in zip(dim_val, self.data.items()):
        if vdim:
            new_val = list(val)
            new_val.insert(dim_pos, dval)
            items[key] = tuple(new_val)
        else:
            new_key = list(key)
            new_key.insert(dim_pos, dval)
            items[tuple(new_key)] = val
    return self.clone(items, **dict(dimensions, **kwargs))

Adds a dimension and its values to the object

Requires the dimension name or object, the desired position in
the key dimensions and a key value scalar or sequence of the
same length as the existing keys.

Args:
dimension: Dimension or dimension spec to add
dim_pos (int) Integer index to insert dimension at
dim_val (scalar or ndarray): Dimension value(s) to add
vdim: Disabled, this type does not have value dimensions
**kwargs: Keyword arguments passed to the cloned element

Returns:
Cloned object containing the new dimension


def csv_to_transactions(handle, source_encoding='latin1', date_format='%d-%m-%Y', thousand_sep='.', decimal_sep=','):
    trans = Transactions()
    rows = csv.reader(handle, delimiter=';', quotechar='"')
    for (index, row) in enumerate(rows):
        trans.append(Parse.csv_row_to_transaction(index, row))
    return trans

Parses CSV data from stream and returns ``Transactions``.

Args:
index: The index of this row in the original CSV file. Used for
sorting ``Transaction``s by their order of appearance.

row: The row containing strings for [transfer_date, posted_date,
message, money_amount, money_total].

source_encoding: The encoding that will be used to decode strings
to UTF-8.

date_format: The format of dates in this row.

thousand_sep: The thousand separator in money amounts.

decimal_sep: The decimal separator in money amounts.

Returns:
A ``Transactions`` object.


def _cancel_grpc(operations_stub, operation_name):

    request_pb = operations_pb2.CancelOperationRequest(name=operation_name)
    operations_stub.CancelOperation(request_pb)

Cancel an operation using a gRPC client.

Args:
operations_stub (google.longrunning.operations_pb2.OperationsStub):
The gRPC operations stub.
operation_name (str): The name of the operation.


def disconnect(signal, receiver):
    inputkey = __make_id(receiver)
    with __lock:
        __purge()
        receivers = __receivers.get(signal)
        for idx in six.moves.range(len(receivers)):
            connected = receivers[idx]()
            if (inputkey != __make_id(connected)):
                continue
            del receivers[idx]
            return True
    return False

Disconnect the receiver `func` from the signal, identified by
`signal_id`.

Args:
signal: The signal identifier.
receiver: The callable receiver to disconnect.

Returns:
True if the receiver was successfully disconnected. False otherwise.


def reset_spent_time(self, **kwargs):
    path = ('%s/%s/reset_spent_time' % (self.manager.path, self.get_id()))
    return self.manager.gitlab.http_post(path, **kwargs)

Resets the time spent working on the object.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabTimeTrackingError: If the time tracking update cannot be done


def add_all_transport_reactions(model, boundaries, allow_duplicates=False):
    all_reactions = {}
    if (not allow_duplicates):
        for rxnid in model.database.reactions:
            rx = model.database.get_reaction(rxnid)
            all_reactions[rx] = rxnid
    boundary_pairs = set()
    for (source, dest) in boundaries:
        if (source != dest):
            boundary_pairs.add(tuple(sorted((source, dest))))
    added = set()
    added_pairs = set()
    initial_compounds = set(model.compounds)
    reactions = set(model.database.reactions)
    for compound in initial_compounds:
        for (c1, c2) in boundary_pairs:
            compound1 = compound.in_compartment(c1)
            compound2 = compound.in_compartment(c2)
            pair = (compound1, compound2)
            if (pair in added_pairs):
                continue
            rxnid_tp = create_transport_id(reactions, compound1, compound2)
            reaction_tp = Reaction(Direction.Both, {compound1: (- 1), compound2: 1})
            if (reaction_tp not in all_reactions):
                model.database.set_reaction(rxnid_tp, reaction_tp)
                reactions.add(rxnid_tp)
            else:
                rxnid_tp = all_reactions[reaction_tp]
            if (not model.has_reaction(rxnid_tp)):
                added.add(rxnid_tp)
            model.add_reaction(rxnid_tp)
            added_pairs.add(pair)
    return added

Add all transport reactions to database and to model.

Add transport reactions for all boundaries. Boundaries are defined
by pairs (2-tuples) of compartment IDs. Transport reactions are
added for all compounds in the model, not just for compounds in the
two boundary compartments.

Args:
model: :class:`psamm.metabolicmodel.MetabolicModel`.
boundaries: Set of compartment boundary pairs.

Returns:
Set of IDs of reactions that were added.


def get_help_commands(server_prefix):

    datapacks = []

    _dir = os.path.realpath(
        os.path.join(os.getcwd(), os.path.dirname(__file__)))
    for module_name in os.listdir("{}/../".format(_dir)):
        if not module_name.startswith("_") and not module_name.startswith("!"):
            help_command = "`{}help {}`".format(server_prefix, module_name)
            datapacks.append((module_name, help_command, True))

    return datapacks

Get the help commands for all modules

Args:
server_prefix: The server command prefix

Returns:
datapacks (list): A list of datapacks for the help commands for all the modules


def pipelines(self, **kwargs):
    path = ('%s/%s/pipelines' % (self.manager.path, self.get_id()))
    return self.manager.gitlab.http_get(path, **kwargs)

List the merge request pipelines.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabListError: If the list could not be retrieved

Returns:
RESTObjectList: List of changes


def AnsiText(text, command_list=None, reset=True):

  command_list = command_list or ['reset']
  if reset:
    return '%s%s%s' % (_AnsiCmd(command_list), text, _AnsiCmd(['reset']))
  else:
    return '%s%s' % (_AnsiCmd(command_list), text)

Wrap text in ANSI/SGR escape codes.

Args:
text: String to encase in sgr escape sequence.
command_list: List of strings, each string represents an sgr value.
e.g. 'fg_blue', 'bg_yellow'
reset: Boolean, if to add a reset sequence to the suffix of the text.

Returns:
String with sgr characters added.


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

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


def get_and_check_single_upstream_artifact_full_path(context, task_id, path):

    abs_path = get_single_upstream_artifact_full_path(context, task_id, path)
    if not os.path.exists(abs_path):
        raise ScriptWorkerTaskException(
            'upstream artifact with path: {}, does not exist'.format(abs_path)
        )

    return abs_path

Return the full path where an upstream artifact is located on disk.

Args:
context (scriptworker.context.Context): the scriptworker context.
task_id (str): the task id of the task that published the artifact
path (str): the relative path of the artifact

Returns:
str: absolute path to the artifact

Raises:
scriptworker.exceptions.ScriptWorkerTaskException: when an artifact doesn't exist.


def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
    vision_data = {}
    if image_sizes is not None:
        num_image_tokens = [self.image_seq_length] * len(image_sizes)
        num_image_patches = [1] * len(image_sizes)
        vision_data.update({'num_image_tokens': num_image_tokens, 'num_image_patches': num_image_patches})
    return MultiModalData(**vision_data)

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

Args:
image_sizes (List[List[str]], *optional*):
The input sizes formatted as (height, width) per each image.
Returns:
Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio")
to a list containing the number of placeholder tokens required. If the model doesn't accept
a certain modality or no input sizes are provided, the dict value is set to an empty list.


def _matches(field, params):
    fieldattrs = six.iteritems(params)
    return all(((getattr(field, attr) == val) for (attr, val) in fieldattrs))

Return True if the input TypedField `field` contains instance attributes
that match the input parameters.

Args:
field: A TypedField instance.
params: A dictionary of TypedField instance attribute-to-value mappings.

Returns:
True if the input TypedField matches the input parameters.


def match_file(self, f, update_file=False):

        if self.map_func is not None:
            val = self.map_func(f)
        else:
            m = self.regex.search(f.path)
            val = m.group(1) if m is not None else None

        return self._astype(val)

Determine whether the passed file matches the Entity.

Args:
f (File): The File instance to match against.

Returns: the matched value if a match was found, otherwise None.


def StatEntryFromStat(stat, pathspec, ext_attrs=True):
    result = rdf_client_fs.StatEntry(pathspec=pathspec)
    for attr in _STAT_ATTRS:
        value = getattr(stat.GetRaw(), attr, None)
        if (value is None):
            continue
        value = int(value)
        if (value < 0):
            value &= 4294967295
        setattr(result, attr, value)
    result.st_flags_linux = stat.GetLinuxFlags()
    result.st_flags_osx = stat.GetOsxFlags()
    if ext_attrs:
        result.ext_attrs = list(GetExtAttrs(stat.GetPath()))
    return result

Build a stat entry object from a given stat object.

Args:
stat: A `Stat` object.
pathspec: A `PathSpec` from which `stat` was obtained.
ext_attrs: Whether to include extended file attributes in the result.

Returns:
`StatEntry` object.


def get_poi(self, **kwargs):
    params = {'coordinateX': kwargs.get('longitude'), 'coordinateY': kwargs.get('latitude'), 'tipos': util.ints_to_string(kwargs.get('types')), 'Radius': kwargs.get('radius'), 'cultureInfo': util.language_code(kwargs.get('lang'))}
    result = self.make_request('geo', 'get_poi', **params)
    if (not util.check_result(result, 'poiList')):
        return (False, 'UNKNOWN ERROR')
    values = util.response_list(result, 'poiList')
    return (True, [emtype.Poi(**a) for a in values])

Obtain a list of POI in the given radius.

Args:
latitude (double): Latitude in decimal degrees.
longitude (double): Longitude in decimal degrees.
types (list[int] | int): POI IDs (or empty list to get all).
radius (int): Radius (in meters) of the search.
lang (str): Language code (*es* or *en*).

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


def new_vertex(self):
    vertex = Vertex(len(self.vertices))
    self.vertices.append(vertex)
    return vertex

Creates and returns a new vertex.

Returns:
A new Vertex instance with a unique index.


def index(self, nurest_object):

        for index, obj in enumerate(self):
            if obj.equals(nurest_object):
                return index

        raise ValueError("%s is  not in %s" % (nurest_object, self))

Get index of the given item
Args:
nurest_object (bambou.NURESTObject): the NURESTObject object to verify

Returns:
Returns the position of the object.

Raises:
Raise a ValueError exception if object is not present


def from_json(cls, json, _reader=blobstore.BlobReader):

    return cls(json[cls.BLOB_KEY_PARAM],
               json[cls.START_FILE_INDEX_PARAM],
               json[cls.END_FILE_INDEX_PARAM],
               json[cls.OFFSET_PARAM],
               _reader)

Creates an instance of the InputReader for the given input shard state.

Args:
json: The InputReader state as a dict-like object.
_reader: For dependency injection.

Returns:
An instance of the InputReader configured using the values of json.


def list_metric_defs_for_resource(access_token, subscription_id, resource_group, resource_provider, resource_type, resource_name):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', resource_group, '/providers/', resource_provider, '/', resource_type, '/', resource_name, '/providers/microsoft.insights', '/metricdefinitions?api-version=', INSIGHTS_METRICS_API])
    return do_get(endpoint, access_token)

List the monitoring metric definitions for a resource.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
resource_provider (str): Type of resource provider.
resource_type (str): Type of resource.
resource_name (str): Name of resource.

Returns:
HTTP response. JSON body of metric definitions.


def plot_generated_images(images, fname):
    fig = plt.figure(figsize=(4, 4))
    canvas = backend_agg.FigureCanvasAgg(fig)
    for (i, image) in enumerate(images):
        ax = fig.add_subplot(4, 4, (i + 1))
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.imshow(image.reshape(IMAGE_SHAPE[:(- 1)]), cmap='Greys_r')
    fig.tight_layout()
    plt.subplots_adjust(wspace=0.05, hspace=0.05)
    canvas.print_figure(fname, format='png')

Save a synthetic image as a PNG file.

Args:
images: samples of synthetic images generated by the generative network.
fname: Python `str`, filename to save the plot to.