def run(
        self,
        num_episodes=-1,
        max_episode_timesteps=-1,
        episode_finished=None,
        summary_report=None,
        summary_interval=0,
        num_timesteps=None,
        deterministic=False,
        episodes=None,
        max_timesteps=None,
        testing=False,
        sleep=None
    ):

        if episodes is not None:
            num_episodes = episodes
            warnings.warn("WARNING: `episodes` parameter is deprecated, use `num_episodes` instead.",
                          category=DeprecationWarning)
        assert isinstance(num_episodes, int)

        if max_timesteps is not None:
            max_episode_timesteps = max_timesteps
            warnings.warn("WARNING: `max_timesteps` parameter is deprecated, use `max_episode_timesteps` instead.",
                          category=DeprecationWarning)
        assert isinstance(max_episode_timesteps, int)

        if summary_report is not None:
            warnings.warn("WARNING: `summary_report` parameter is deprecated, use `episode_finished` callback "
                          "instead to generate summaries every n episodes.",
                          category=DeprecationWarning)

        self.reset()

        self.global_episode = 0
        self.global_timestep = 0
        self.should_stop = False

        threads = [threading.Thread(target=self._run_single, args=(t, self.agent[t], self.environment[t],),
                                    kwargs={"deterministic": deterministic,
                                            "max_episode_timesteps": max_episode_timesteps,
                                            "episode_finished": episode_finished,
                                            "testing": testing,
                                            "sleep": sleep})
                   for t in range(len(self.agent))]

        self.start_time = time.time()
        [t.start() for t in threads]

        try:
            next_summary = 0
            next_save = 0 if self.save_frequency_unit != "s" else time.time()
            while any([t.is_alive() for t in threads]) and self.global_episode < num_episodes or num_episodes == -1:
                self.time = time.time()

                if summary_report is not None and self.global_episode > next_summary:
                    summary_report(self)
                    next_summary += summary_interval

                if self.save_path and self.save_frequency is not None:
                    do_save = True
                    current = None
                    if self.save_frequency_unit == "e" and self.global_episode > next_save:
                        current = self.global_episode
                    elif self.save_frequency_unit == "s" and self.time > next_save:
                        current = self.time
                    elif self.save_frequency_unit == "t" and self.global_timestep > next_save:
                        current = self.global_timestep
                    else:
                        do_save = False

                    if do_save:
                        self.agent[0].save_model(self.save_path)

                        while next_save < current:
                            next_save += self.save_frequency
                time.sleep(1)

        except KeyboardInterrupt:
            print('Keyboard interrupt, sending stop command to threads')

        self.should_stop = True

        [t.join() for t in threads]
        print('All threads stopped')

Executes this runner by starting all Agents in parallel (each one in one thread).

Args:
episodes (int): Deprecated; see num_episodes.
max_timesteps (int): Deprecated; see max_episode_timesteps.


def easeOutBack(n, s=1.70158):

    _checkRange(n)
    n = n - 1
    return n * n * ((s + 1) * n + s) + 1

A tween function that overshoots the destination a little and then backs into the destination.

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

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


def compare_version(a, b):

  aa = string.split(a, ".")
  bb = string.split(b, ".")
  for i in range(0, 4):
    if aa[i] != bb[i]:
      return cmp(int(aa[i]), int(bb[i]))
  return 0

Compare two version number strings of the form W.X.Y.Z.

The numbers are compared most-significant to least-significant.
For example, 12.345.67.89 > 2.987.88.99.

Args:
a: First version number string to compare
b: Second version number string to compare

Returns:
0 if the numbers are identical, a positive number if 'a' is larger, and
a negative number if 'b' is larger.


def log_jwt_dict_info(log, msg_str, jwt_dict):

    d = ts_to_str(jwt_dict)

    log_list = [(b, d.pop(a)) for a, b, c in CLAIM_LIST if a in d] + [
        (k, d[k]) for k in sorted(d)
    ]
    list(
        map(
            log,
            ['{}:'.format(msg_str)] + ['  {}: {}'.format(k, v) for k, v in log_list],
        )
    )

Dump JWT to log.

Args:
log: Logger
Logger to which to write the message.

msg_str: str
A message to write to the log before the JWT values.

jwt_dict: dict
JWT containing values to log.

Returns:
None


def histogram_pb(tag, data, buckets=None, description=None):
    bucket_count = (DEFAULT_BUCKET_COUNT if (buckets is None) else buckets)
    data = np.array(data).flatten().astype(float)
    if (data.size == 0):
        buckets = np.array([]).reshape((0, 3))
    else:
        min_ = np.min(data)
        max_ = np.max(data)
        range_ = (max_ - min_)
        if (range_ == 0):
            center = min_
            buckets = np.array([[(center - 0.5), (center + 0.5), float(data.size)]])
        else:
            bucket_width = (range_ / bucket_count)
            offsets = (data - min_)
            bucket_indices = np.floor((offsets / bucket_width)).astype(int)
            clamped_indices = np.minimum(bucket_indices, (bucket_count - 1))
            one_hots = (np.array([clamped_indices]).transpose() == np.arange(0, bucket_count))
            assert (one_hots.shape == (data.size, bucket_count)), (one_hots.shape, (data.size, bucket_count))
            bucket_counts = np.sum(one_hots, axis=0)
            edges = np.linspace(min_, max_, (bucket_count + 1))
            left_edges = edges[:(- 1)]
            right_edges = edges[1:]
            buckets = np.array([left_edges, right_edges, bucket_counts]).transpose()
    tensor = tensor_util.make_tensor_proto(buckets, dtype=np.float64)
    summary_metadata = metadata.create_summary_metadata(display_name=None, description=description)
    summary = summary_pb2.Summary()
    summary.value.add(tag=tag, metadata=summary_metadata, tensor=tensor)
    return summary

Create a histogram summary protobuf.

Arguments:
tag: String tag for the summary.
data: A `np.array` or array-like form of any shape. Must have type
castable to `float`.
buckets: Optional positive `int`. The output will have this
many buckets, except in two edge cases. If there is no data, then
there are no buckets. If there is data but all points have the
same value, then there is one bucket whose left and right
endpoints are the same.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.

Returns:
A `summary_pb2.Summary` protobuf object.


def tournament_name2number(self, name):
    tournaments = self.get_tournaments()
    d = {t['name']: t['tournament'] for t in tournaments}
    return d.get(name, None)

Translate tournament name to tournament number.

Args:
name (str): tournament name to translate

Returns:
number (int): number of the tournament or `None` if unknown.

Examples:
>>> NumerAPI().tournament_name2number('delta')
4
>>> NumerAPI().tournament_name2number('foo')
None


def GetEntries(self, parser_mediator, match=None, **unused_kwargs):

    if 'RememberedNetworks' not in match:
      return

    for wifi in match['RememberedNetworks']:
      ssid = wifi.get('SSIDString', 'UNKNOWN_SSID')
      security_type = wifi.get('SecurityType', 'UNKNOWN_SECURITY_TYPE')

      event_data = plist_event.PlistTimeEventData()
      event_data.desc = (
          '[WiFi] Connected to network: <{0:s}> using security {1:s}').format(
              ssid, security_type)
      event_data.key = 'item'
      event_data.root = '/RememberedNetworks'

      datetime_value = wifi.get('LastConnected', None)
      if datetime_value:
        event = time_events.PythonDatetimeEvent(
            datetime_value, definitions.TIME_DESCRIPTION_WRITTEN)

      else:
        date_time = dfdatetime_semantic_time.SemanticTime('Not set')
        event = time_events.DateTimeValuesEvent(
            date_time, definitions.TIME_DESCRIPTION_NOT_A_TIME)

      parser_mediator.ProduceEventWithEventData(event, event_data)

Extracts relevant Airport entries.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
match (Optional[dict[str: object]]): keys extracted from PLIST_KEYS.


def predecesors_pattern(element, root):

    def is_root_container(el):
        return (el.parent.parent.getTagName() == '')
    if ((not element.parent) or (not element.parent.parent) or is_root_container(element)):
        return []
    trail = [[element.parent.parent.getTagName(), _params_or_none(element.parent.parent.params)], [element.parent.getTagName(), _params_or_none(element.parent.params)], [element.getTagName(), _params_or_none(element.params)]]
    match = root.match(*trail)
    if (element in match):
        return [PathCall('match', match.index(element), trail)]

Look for `element` by its predecesors.

Args:
element (obj): HTMLElement instance of the object you are looking for.
root (obj): Root of the `DOM`.

Returns:
list: ``[PathCall()]`` - list with one :class:`PathCall` object (to \
allow use with ``.extend(predecesors_pattern())``).


def _read_message(self):
    line = self._rfile.readline()
    if (not line):
        return None
    content_length = self._content_length(line)
    while (line and line.strip()):
        line = self._rfile.readline()
    if (not line):
        return None
    return self._rfile.read(content_length)

Reads the contents of a message.

Returns:
body of message if parsable else None


def get_signatures_with_results(vcs):
    results_dir = os.path.join(vcs.private_dir(), 'results')
    if (not os.path.exists(results_dir)):
        return []
    rel_paths = os.listdir(results_dir)
    return [p for p in rel_paths if os.path.isdir(os.path.join(results_dir, p))]

Returns the list of signatures for which test results are saved.

Args:
vcs (easyci.vcs.base.Vcs)

Returns:
List[str]


def __init__(self, object_type=None, attributes=None):

        super(ObjectDefaults, self).__init__(tag=enums.Tags.OBJECT_DEFAULTS)

        self._object_type = None
        self._attributes = None

        self.object_type = object_type
        self.attributes = attributes

Construct an ObjectDefaults structure.

Args:
object_type (enum): An ObjectType enumeration identifying the type
to which the defaults pertain. Optional, defaults to None.
Required for read/write.
attributes (structure): An Attributes structure containing
attribute values that are defaults for an object type.
Optional, defaults to None. Required for read/write.


class AsDict(AsSideInput):

    @staticmethod
    def _from_runtime_iterable(it, options):
        return dict(it)

    def _side_input_data(self) -> SideInputData:
        return SideInputData(common_urns.side_inputs.ITERABLE.urn, self._window_mapping_fn, dict)

Marker specifying a PCollection to be used as an indexable side input.

Intended for use in side-argument specification---the same places where
AsSingleton and AsIter are used, but returns an interface that allows
key lookup.

Args:
pcoll: Input pcollection. All elements should be key-value pairs (i.e.
2-tuples) with unique keys.

Returns:
An AsDict-wrapper around a PCollection whose one element is a dict with
entries for uniquely-keyed pairs in pcoll.


def GetDevicePath(device_handle):

  io_service_obj = iokit.IOHIDDeviceGetService(device_handle)
  str_buffer = ctypes.create_string_buffer(DEVICE_PATH_BUFFER_SIZE)
  iokit.IORegistryEntryGetPath(io_service_obj, K_IO_SERVICE_PLANE, str_buffer)

  return str_buffer.value

Obtains the unique path for the device.

Args:
device_handle: reference to the device

Returns:
A unique path for the device, obtained from the IO Registry


def _get_path_for_op_id(self, id: str) -> Optional[str]:
    for (path_key, path_value) in self._get_spec()['paths'].items():
        for method in self.METHODS:
            if (method in path_value):
                if (self.OPERATION_ID_KEY in path_value[method]):
                    if (path_value[method][self.OPERATION_ID_KEY] == id):
                        return path_key
    return None

Searches the spec for a path matching the operation id.

Args:
id: operation id

Returns:
path to the endpoint, or None if not found


def _update_linear_bucket_count(a_float, dist):

    buckets = dist.linearBuckets
    if buckets is None:
        raise ValueError(_BAD_UNSET_BUCKETS % (u'linear buckets'))
    bucket_counts = dist.bucketCounts
    num_finite_buckets = buckets.numFiniteBuckets
    if len(bucket_counts) < num_finite_buckets + 2:
        raise ValueError(_BAD_LOW_BUCKET_COUNT)
    width = buckets.width
    lower = buckets.offset
    upper = lower + (num_finite_buckets * width)
    if a_float < lower:
        index = 0
    elif a_float >= upper:
        index = num_finite_buckets + 1
    else:
        index = 1 + int(((a_float - lower) / width))
    bucket_counts[index] += 1
    _logger.debug(u'upper:%f, lower:%f, width:%f, sample:%f, index:%d',
                  upper, lower, width, a_float, index)

Adds `a_float` to `dist`, updating the its linear buckets.

Args:
a_float (float): a new value
dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`):
the Distribution being updated

Raises:
ValueError: if `dist` does not already have linear buckets defined
ValueError: if there are not enough bucket count fields in `dist`


def learn(self, features, labels):
    labels = np.ravel(labels)
    self.__learn_labels(labels)
    if (len(labels) == 0):
        return
    labels = self.labels.transform(labels)
    if ((self.feature_length > 0) and hasattr(self.clf, 'partial_fit')):
        self.clf = self.clf.partial_fit(features, labels)
    else:
        self.clf = self.clf.fit(features, labels)
        self.feature_length = len(features[0])

Fits the classifier

If it's state is empty, the classifier is fitted, if not
the classifier is partially fitted.
See sklearn's SGDClassifier fit and partial_fit methods.

Args:
features (:obj:`list` of :obj:`list` of :obj:`float`)
labels (:obj:`list` of :obj:`str`): Labels for each set of features.
New features are learnt.


def copy(reader, writer, start, stop, insertLocation=None, tsCol=None):

  assert stop >= start
  startRows = []
  copyRows = []
  ts = None
  inc = None
  if tsCol is None:
    tsCol = reader.getTimestampFieldIdx()
  for i, row in enumerate(reader):

    if ts is None:
      ts = row[tsCol]
    elif inc is None:
      inc = row[tsCol] - ts

    if i >= start and i <= stop:
      copyRows.append(row)
    startRows.append(row)

  if insertLocation is None:
    insertLocation = stop + 1
  startRows[insertLocation:insertLocation] = copyRows

  for row in startRows:
    row[tsCol] = ts
    writer.appendRecord(row)
    ts += inc

Copies a range of values to a new location in the data set.

Args:
reader: A FileRecordStream object with input data.
writer: A FileRecordStream object to write output data to.
start: The first row in the range to copy.
stop: The last row in the range to copy.
insertLocation: The location to insert the copied range. If not specified,
the range is inserted immediately following itself.


def longest_one_seg_prefix(self, word):

        match = self.seg_regex.match(word)
        if match:
            return match.group(0)
        else:
            return ''

Return longest IPA Unicode prefix of `word`

Args:
word (unicode): word as IPA string

Returns:
unicode: longest single-segment prefix of `word`


def __init__(self, data=None):

        if data is None:
            data = {}
        self._data = data
        self._len = 0

Instantiate the histogram.

Args:
data (Mapping[str, int]): The data strucure to be used to store
the underlying data. The default is an empty dictionary.
This can be set to a dictionary-like object if required
(for example, if a special object is needed for
concurrency reasons).


def offset(self, num_to_skip):

        query = query_mod.Query(self)
        return query.offset(num_to_skip)

Skip to an offset in a query with this collection as parent.

See
:meth:`~.firestore_v1beta1.query.Query.offset` for
more information on this method.

Args:
num_to_skip (int): The number of results to skip at the beginning
of query results. (Must be non-negative.)

Returns:
~.firestore_v1beta1.query.Query: An offset query.


def add_middleware(middleware: EFBMiddleware):
    global middlewares
    if isinstance(middleware, EFBMiddleware):
        middlewares.append(middleware)
    else:
        raise TypeError('Middleware instance is expected')

Register a middleware with the coordinator.

Args:
middleware (EFBMiddleware): Middleware to register


def _TerminateProcess(self, process):

    pid = process.pid
    logger.warning('Terminating process: (PID: {0:d}).'.format(pid))
    process.terminate()

    process.join(timeout=self._PROCESS_JOIN_TIMEOUT)

    if process.is_alive():
      logger.warning('Killing process: (PID: {0:d}).'.format(pid))
      self._KillProcess(pid)

Terminate a process.

Args:
process (MultiProcessBaseProcess): process to terminate.


def create_cloudwatch_event(app_name, env, region, rules):

    session = boto3.Session(profile_name=env, region_name=region)
    cloudwatch_client = session.client('events')

    rule_name = rules.get('rule_name')
    schedule = rules.get('schedule')
    rule_description = rules.get('rule_description')
    json_input = rules.get('json_input', {})

    if schedule is None:
        LOG.critical('Schedule is required and no schedule is defined!')
        raise InvalidEventConfiguration('Schedule is required and no schedule is defined!')

    if rule_name is None:
        LOG.critical('Rule name is required and no rule_name is defined!')
        raise InvalidEventConfiguration('Rule name is required and no rule_name is defined!')
    else:
        LOG.info('%s and %s', app_name, rule_name)
        rule_name = "{}_{}".format(app_name, rule_name.replace(' ', '_'))

    if rule_description is None:
        rule_description = "{} - {}".format(app_name, rule_name)

    lambda_arn = get_lambda_arn(app=app_name, account=env, region=region)

    account_id = get_env_credential(env=env)['accountId']
    principal = "events.amazonaws.com"
    statement_id = '{}_cloudwatch_{}'.format(app_name, rule_name)
    source_arn = 'arn:aws:events:{}:{}:rule/{}'.format(region, account_id, rule_name)
    add_lambda_permissions(
        function=lambda_arn,
        statement_id=statement_id,
        action='lambda:InvokeFunction',
        principal=principal,
        source_arn=source_arn,
        env=env,
        region=region, )

    cloudwatch_client.put_rule(
        Name=rule_name,
        ScheduleExpression=schedule,
        State='ENABLED',
        Description=rule_description, )

    targets = []

    json_payload = '{}'.format(json.dumps(json_input))

    target = {
        "Id": app_name,
        "Arn": lambda_arn,
        "Input": json_payload,
    }

    targets.append(target)

    put_targets_response = cloudwatch_client.put_targets(Rule=rule_name, Targets=targets)
    LOG.debug('Cloudwatch put targets response: %s', put_targets_response)

    LOG.info('Created Cloudwatch event "%s" with schedule: %s', rule_name, schedule)

Create cloudwatch event for lambda from rules.

Args:
app_name (str): name of the lambda function
env (str): Environment/Account for lambda function
region (str): AWS region of the lambda function
rules (dict): Trigger rules from the settings


def GetMACBRepresentation(self, event):
    data_type = getattr(event, 'data_type', None)
    if (not data_type):
        return '....'
    if (data_type == 'fs:stat'):
        descriptions = event.timestamp_desc.split(';')
        return_characters = ['.', '.', '.', '.']
        for description in descriptions:
            if (description in ('mtime', definitions.TIME_DESCRIPTION_MODIFICATION)):
                return_characters[0] = 'M'
            elif (description in ('atime', definitions.TIME_DESCRIPTION_LAST_ACCESS)):
                return_characters[1] = 'A'
            elif (description in ('ctime', definitions.TIME_DESCRIPTION_CHANGE)):
                return_characters[2] = 'C'
            elif (description in ('crtime', definitions.TIME_DESCRIPTION_CREATION)):
                return_characters[3] = 'B'
        return ''.join(return_characters)
    if (event.timestamp_desc in [definitions.TIME_DESCRIPTION_LAST_ACCESS, definitions.TIME_DESCRIPTION_ACCOUNT_CREATED, definitions.TIME_DESCRIPTION_LAST_VISITED, definitions.TIME_DESCRIPTION_START, definitions.TIME_DESCRIPTION_LAST_SHUTDOWN, definitions.TIME_DESCRIPTION_LAST_LOGIN, definitions.TIME_DESCRIPTION_LAST_PASSWORD_RESET, definitions.TIME_DESCRIPTION_LAST_CONNECTED, definitions.TIME_DESCRIPTION_LAST_RUN, definitions.TIME_DESCRIPTION_LAST_PRINTED]):
        return '.A..'
    if (event.timestamp_desc in [definitions.TIME_DESCRIPTION_MODIFICATION, definitions.TIME_DESCRIPTION_WRITTEN, definitions.TIME_DESCRIPTION_DELETED]):
        return 'M...'
    if (event.timestamp_desc in [definitions.TIME_DESCRIPTION_CREATION, definitions.TIME_DESCRIPTION_ADDED, definitions.TIME_DESCRIPTION_FILE_DOWNLOADED, definitions.TIME_DESCRIPTION_FIRST_CONNECTED]):
        return '...B'
    if (event.timestamp_desc in [definitions.TIME_DESCRIPTION_CHANGE, definitions.TIME_DESCRIPTION_ENTRY_MODIFICATION]):
        return '..C.'
    return '....'

Retrieves the MACB representation.

Args:
event (EventObject): event.

Returns:
str: MACB representation.


def decode(self, decoder_input_ids, encoder_outputs, encoder_attention_mask: Optional[jnp.ndarray]=None, decoder_attention_mask: Optional[jnp.ndarray]=None, decoder_position_ids: Optional[jnp.ndarray]=None, past_key_values: Optional[dict]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, train: bool=False, params: Optional[dict]=None, dropout_rng: PRNGKey=None):
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.return_dict
    encoder_hidden_states = encoder_outputs[0]
    if encoder_attention_mask is None:
        batch_size, sequence_length = encoder_hidden_states.shape[:2]
        encoder_attention_mask = jnp.ones((batch_size, sequence_length))
    batch_size, sequence_length = decoder_input_ids.shape
    if decoder_attention_mask is None:
        decoder_attention_mask = jnp.ones((batch_size, sequence_length))
    if decoder_position_ids is None:
        if past_key_values is not None:
            raise ValueError('Make sure to provide `decoder_position_ids` when passing `past_key_values`.')
        decoder_position_ids = jnp.broadcast_to(jnp.arange(sequence_length)[None, :], (batch_size, sequence_length))
    rngs = {}
    if dropout_rng is not None:
        rngs['dropout'] = dropout_rng
    inputs = {'params': params or self.params}
    if past_key_values:
        inputs['cache'] = past_key_values
        mutable = ['cache']
    else:
        mutable = False

    def _decoder_forward(module, decoder_input_ids, decoder_attention_mask, decoder_position_ids, **kwargs):
        decoder_module = module._get_decoder_module()
        outputs = decoder_module(decoder_input_ids, decoder_attention_mask, decoder_position_ids, **kwargs)
        hidden_states = outputs[0]
        if self.config.tie_word_embeddings:
            shared_embedding = module.model.variables['params']['shared']['embedding']
            lm_logits = module.lm_head.apply({'params': {'kernel': shared_embedding.T}}, hidden_states)
        else:
            lm_logits = module.lm_head(hidden_states)
        lm_logits += module.final_logits_bias.astype(self.dtype)
        return (lm_logits, outputs)
    outputs = self.module.apply(inputs, decoder_input_ids=jnp.array(decoder_input_ids, dtype='i4'), decoder_attention_mask=jnp.array(decoder_attention_mask, dtype='i4'), decoder_position_ids=jnp.array(decoder_position_ids, dtype='i4'), encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=jnp.array(encoder_attention_mask, dtype='i4'), output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, deterministic=not train, rngs=rngs, mutable=mutable, method=_decoder_forward)
    if past_key_values is None:
        lm_logits, decoder_outputs = outputs
    else:
        (lm_logits, decoder_outputs), past = outputs
    if return_dict:
        outputs = FlaxCausalLMOutputWithCrossAttentions(logits=lm_logits, hidden_states=decoder_outputs.hidden_states, attentions=decoder_outputs.attentions, cross_attentions=decoder_outputs.cross_attentions)
    else:
        outputs = (lm_logits,) + decoder_outputs[1:]
    if past_key_values is not None and return_dict:
        outputs['past_key_values'] = unfreeze(past['cache'])
        return outputs
    elif past_key_values is not None and (not return_dict):
        outputs = outputs[:1] + (unfreeze(past['cache']),) + outputs[1:]
    return outputs

Returns:

Example:

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

>>> model = FlaxMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-cc25")

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

>>> decoder_start_token_id = model.config.decoder_start_token_id
>>> decoder_input_ids = jnp.ones((inputs.input_ids.shape[0], 1), dtype="i4") * decoder_start_token_id

>>> outputs = model.decode(decoder_input_ids, encoder_outputs)
>>> logits = outputs.logits
```


def prune(self, regex='.*'):
    return filetree(self.root, ignore=self.ignore, regex=regex)

Prune leaves of filetree according to specified
regular expression.

Args:
regex (str): Regular expression to use in pruning tree.


def pred_to_prob(Y_h, k):

    Y_h = Y_h.clone()
    if Y_h.dim() > 1:
        Y_h = Y_h.squeeze()
    assert Y_h.dim() == 1
    assert (Y_h >= 1).all()
    assert (Y_h <= k).all()
    n = Y_h.shape[0]
    Y_s = torch.zeros((n, k), dtype=Y_h.dtype, device=Y_h.device)
    for i, j in enumerate(Y_h):
        Y_s[i, j - 1] = 1.0
    return Y_s

Converts a 1D tensor of predicted labels into a 2D tensor of probabilistic labels

Args:
Y_h: an [n], or [n,1] tensor of predicted (int) labels in {1,...,k}
k: the largest possible label in Y_h
Returns:
Y_s: a torch.FloatTensor of shape [n, k] where Y_s[i, j-1] is the probabilistic
label for item i and label j


def _openfile(instance, filething, filename, fileobj, writable, create):

    assert not create or writable

    if isinstance(filething, FileThing):
        filename = filething.filename
        fileobj = filething.fileobj
        filething = None

    if filething is not None:
        if is_fileobj(filething):
            fileobj = filething
        elif hasattr(filething, "__fspath__"):
            filename = filething.__fspath__()
            if not isinstance(filename, (bytes, text_type)):
                raise TypeError("expected __fspath__() to return a filename")
        else:
            filename = filething

    if instance is not None:

        if not writable:
            instance.filename = filename
        elif filename is None:
            filename = getattr(instance, "filename", None)

    if fileobj is not None:
        verify_fileobj(fileobj, writable=writable)
        yield FileThing(fileobj, filename, filename or fileobj_name(fileobj))
    elif filename is not None:
        verify_filename(filename)

        inmemory_fileobj = False
        try:
            fileobj = open(filename, "rb+" if writable else "rb")
        except IOError as e:
            if writable and e.errno == errno.EOPNOTSUPP:

                try:
                    with open(filename, "rb") as fileobj:
                        fileobj = BytesIO(fileobj.read())
                except IOError as e2:
                    raise MutagenError(e2)
                inmemory_fileobj = True
            elif create and e.errno == errno.ENOENT:
                assert writable
                try:
                    fileobj = open(filename, "wb+")
                except IOError as e2:
                    raise MutagenError(e2)
            else:
                raise MutagenError(e)

        with fileobj as fileobj:
            yield FileThing(fileobj, filename, filename)

            if inmemory_fileobj:
                assert writable
                data = fileobj.getvalue()
                try:
                    with open(filename, "wb") as fileobj:
                        fileobj.write(data)
                except IOError as e:
                    raise MutagenError(e)
    else:
        raise TypeError("Missing filename or fileobj argument")

yields a FileThing

Args:
filething: Either a file name, a file object or None
filename: Either a file name or None
fileobj: Either a file object or None
writable (bool): if the file should be opened
create (bool): if the file should be created if it doesn't exist.
implies writable
Raises:
MutagenError: In case opening the file failed
TypeError: in case neither a file name or a file object is passed


def add_http_endpoint(self, url, request_handler):
    self.app.router.add_route('*', url, request_handler)

This method provides a programatic way of added invidual routes
to the http server.

Args:
url (str): the url to be handled by the request_handler
request_handler (nautilus.network.RequestHandler): The request handler


def unzip(input_layer, split_dim=0, num_splits=2):
    shape = input_layer.shape
    _check_split_dims(num_splits, split_dim, shape)
    splits = functions.unzip(input_layer, split_dim, shape[split_dim], num_splits)
    return input_layer.with_sequence(splits)

Unzips this Tensor along the split_dim into num_splits Equal chunks.

Examples:

* `[1, 2, 3, 4] -> [1, 3], [2, 4]`
* `[[1, 1], [2, 2], [3, 3], [4, 4]] -> [[1, 1], [3, 3]], [[2, 2], [4, 4]]`

Args:
input_layer: The chainable object, supplied.
split_dim: The dimension to split along. Defaults to batch.
num_splits: The number of splits.
Returns:
A list of PrettyTensors.
Raises:
ValueError: If split_dim is out of range or isn't divided evenly by
num_splits.


def Trim(self, flags):
    logger.info('Trimming!')
    flags = bytearray(flags)
    length = (1 << (self.Depth - 1))
    while (len(flags) < length):
        flags.append(0)
    MerkleTree._TrimNode(self.Root, 0, self.Depth, flags)

Trim the nodes from the tree keeping only the root hash.

Args:
flags: "0000" for trimming, any other value for keeping the nodes.


def _get_graph(self):
    with self._lock:
        return self._graph

Returns pydot.Dot object for the pipeline graph.

The purpose of this method is to avoid accessing the graph while it is
updated. No one except for this method should be accessing _graph directly.

Returns:
(pydot.Dot)


def stChromagram(signal, fs, win, step, PLOT=False):

    win = int(win)
    step = int(step)
    signal = numpy.double(signal)
    signal = signal / (2.0 ** 15)
    DC = signal.mean()
    MAX = (numpy.abs(signal)).max()
    signal = (signal - DC) / (MAX - DC)

    N = len(signal)
    cur_p = 0
    count_fr = 0
    nfft = int(win / 2)
    nChroma, nFreqsPerChroma = stChromaFeaturesInit(nfft, fs)
    chromaGram = numpy.array([], dtype=numpy.float64)

    while (cur_p + win - 1 < N):
        count_fr += 1
        x = signal[cur_p:cur_p + win]
        cur_p = cur_p + step
        X = abs(fft(x))
        X = X[0:nfft]
        X = X / len(X)
        chromaNames, C = stChromaFeatures(X, fs, nChroma, nFreqsPerChroma)
        C = C[:, 0]
        if count_fr == 1:
            chromaGram = C.T
        else:
            chromaGram = numpy.vstack((chromaGram, C.T))
    FreqAxis = chromaNames
    TimeAxis = [(t * step) / fs for t in range(chromaGram.shape[0])]

    if (PLOT):
        fig, ax = plt.subplots()
        chromaGramToPlot = chromaGram.transpose()[::-1, :]
        Ratio = int(chromaGramToPlot.shape[1] / (3*chromaGramToPlot.shape[0]))
        if Ratio < 1:
            Ratio = 1
        chromaGramToPlot = numpy.repeat(chromaGramToPlot, Ratio, axis=0)
        imgplot = plt.imshow(chromaGramToPlot)
        fstep = int(nfft / 5.0)

        ax.set_yticks(range(int(Ratio / 2), len(FreqAxis) * Ratio, Ratio))
        ax.set_yticklabels(FreqAxis[::-1])
        TStep = int(count_fr / 3)
        TimeTicks = range(0, count_fr, TStep)
        TimeTicksLabels = ['%.2f' % (float(t * step) / fs) for t in TimeTicks]
        ax.set_xticks(TimeTicks)
        ax.set_xticklabels(TimeTicksLabels)
        ax.set_xlabel('time (secs)')
        imgplot.set_cmap('jet')
        plt.colorbar()
        plt.show()

    return (chromaGram, TimeAxis, FreqAxis)

Short-term FFT mag for spectogram estimation:
Returns:
a numpy array (nFFT x numOfShortTermWindows)
ARGUMENTS:
signal:      the input signal samples
fs:          the sampling freq (in Hz)
win:         the short-term window size (in samples)
step:        the short-term window step (in samples)
PLOT:        flag, 1 if results are to be ploted
RETURNS:


def from_file(cls, path, fields=None, encoding='utf-8'):
    path = _table_filename(path)
    if (fields is None):
        fields = _get_relation_from_table_path(path)
    table = cls(fields)
    table.attach(path, encoding=encoding)
    return table

Instantiate a Table from a database file.

This method instantiates a table attached to the file at *path*.
The file will be opened and traversed to determine the number of
records, but the contents will not be stored in memory unless
they are modified.

Args:
path: the path to the table file
fields: the Relation schema for the table (loaded from the
relations file in the same directory if not given)
encoding: the character encoding of the file at *path*


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

Tries to merge two values which may be None.

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

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

Returns:
The merged value.

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


def get_catalog(self, catalog_id):
    return self._load_data(self.CATALOGS_ENDPOINT, default=[], resource_id=catalog_id)

Return specified course catalog.

Returns:
dict: catalog details if it is available for the user.


async def _auth_login(self, username, password):
    mechanism = 'LOGIN'
    (code, message) = (await self.do_cmd('AUTH', mechanism, SMTP.b64enc(username), success=(334,)))
    try:
        (code, message) = (await self.do_cmd(SMTP.b64enc(password), success=(235, 503)))
    except SMTPCommandFailedError as e:
        raise SMTPAuthenticationError(e.code, e.message, mechanism)
    return (code, message)

Performs an authentication attempt using the LOGIN mechanism.

Protocol:

1. The username is base64-encoded ;
2. The string 'AUTH LOGIN' and a space character are prepended to
the base64-encoded username and sent to the server ;
3. If the server replies with a 334 return code, we can go on:

1) The password is base64-encoded and sent to the server ;
2) If the server replies with a 235 return code, the user is
authenticated.

Args:
username (str): Identifier of the user trying to authenticate.
password (str): Password for the user.

Raises:
ConnectionResetError: If the connection with the server is
unexpectedely lost.
SMTPAuthenticationError: If the authentication attempt fails.

Returns:
(int, str): A (code, message) 2-tuple containing the server
response.


def _create_interval_filter(interval):

    def filter_fn(value):
        if ((not isinstance(value, six.integer_types)) and (not isinstance(value, float))):
            raise error.HParamsError(('Cannot use an interval filter for a value of type: %s, Value: %s' % (type(value), value)))
        return ((interval.min_value <= value) and (value <= interval.max_value))
    return filter_fn

Returns a function that checkes whether a number belongs to an interval.

Args:
interval: A tensorboard.hparams.Interval protobuf describing the interval.
Returns:
A function taking a number (a float or an object of a type in
six.integer_types) that returns True if the number belongs to (the closed)
'interval'.


def find_distinct(self, collection, key):

        obj = getattr(self.db, collection)
        result = obj.distinct(key)
        return result

Search a collection for the distinct key values provided.

Args:
collection: The db collection. See main class documentation.
key: The name of the key to find distinct values. For example with
the indicators collection, the key could be "type".
Returns:
List of distinct values.


def __init__(self, fn, fullargspec=None):
    if not callable(fn):
        raise TypeError('Expected a callable object instead of: %r' % fn)
    self._fn = fn
    self._fullargspec = fullargspec
    if isinstance(fn, (types.BuiltinFunctionType, types.MethodType, types.FunctionType)):
        self.process = fn
    else:
        self.process = lambda element: fn(element)
    super().__init__()

Initializes a CallableWrapperDoFn object wrapping a callable.

Args:
fn: A callable object.

Raises:
TypeError: if fn parameter is not a callable type.


def _GetLines(line_strings):
    lines = []
    for line_string in line_strings:
        line = list(map(int, line_string.split('-', 1)))
        if line[0] < 1:
            raise errors.YapfError('invalid start of line range: %r' % line)
        if line[0] > line[1]:
            raise errors.YapfError('end comes before start in line range: %r' % line)
        lines.append(tuple(line))
    return lines

Parses the start and end lines from a line string like 'start-end'.

Arguments:
line_strings: (array of string) A list of strings representing a line
range like 'start-end'.

Returns:
A list of tuples of the start and end line numbers.

Raises:
ValueError: If the line string failed to parse or was an invalid line range.


def get_cluster_interfaces(cluster, extra_cond=(lambda nic: True)):
    nics = get_nics(cluster)
    nics = [(nic['device'], nic['name']) for nic in nics if (nic['mountable'] and (nic['interface'] == 'Ethernet') and (not nic['management']) and extra_cond(nic))]
    nics = sorted(nics)
    return nics

Get the network interfaces names corresponding to a criteria.

Note that the cluster is passed (not the individual node names), thus it is
assumed that all nodes in a cluster have the same interface names same
configuration. In addition to ``extra_cond``, only the mountable and
Ehernet interfaces are returned.

Args:
cluster(str): the cluster to consider
extra_cond(lambda): boolean lambda that takes the nic(dict) as
parameter


def __init__(self, forward_core, backward_core, name="bidir_rnn"):

    super(BidirectionalRNN, self).__init__(name=name)
    self._forward_core = forward_core
    self._backward_core = backward_core
    def _is_recurrent(core):
      has_rnn_core_interface = (hasattr(core, "initial_state") and
                                hasattr(core, "output_size") and
                                hasattr(core, "state_size"))
      return isinstance(core, rnn_core.RNNCore) or has_rnn_core_interface
    if not(_is_recurrent(forward_core) and _is_recurrent(backward_core)):
      raise ValueError("Forward and backward cores must both be instances of"
                       "RNNCore.")

Construct a Bidirectional RNN core.

Args:
forward_core: callable RNNCore module that computes forward states.
backward_core: callable RNNCore module that computes backward states.
name: name of the module.

Raises:
ValueError: if not all the modules are recurrent.


def _from_components(self, components):
    raise NotImplementedError('%s._from_components()' % type(self).__name__)

Reconstructs a value from a nested structure of Tensor/CompositeTensor.

Args:
components: A nested structure of `tf.Tensor` or `tf.CompositeTensor`,
compatible with `self._component_specs`.  (Caller is responsible for
ensuring compatibility.)

Returns:
A value that is compatible with this `TypeSpec`.


def helper_list(access_token, oid, path):
    if (oid != ''):
        path = ''.join([path, "('", oid, "')"])
    endpoint = ''.join([ams_rest_endpoint, path])
    return do_ams_get(endpoint, path, access_token)

Helper Function to list a URL path.

Args:
access_token (str): A valid Azure authentication token.
oid (str): An OID.
path (str): A URL Path.

Returns:
HTTP response. JSON body.


def unstage_signature(vcs, signature):

    evidence_path = _get_staged_history_path(vcs)
    staged = get_staged_signatures(vcs)
    if signature not in staged:
        raise NotStagedError
    staged.remove(signature)
    string = '\n'.join(staged)
    with open(evidence_path, 'w') as f:
        f.write(string)

Remove `signature` from the list of staged signatures

Args:
vcs (easyci.vcs.base.Vcs)
signature (basestring)

Raises:
NotStagedError


def get_object(cls, api_token, id):

        load_balancer = cls(token=api_token, id=id)
        load_balancer.load()
        return load_balancer

Class method that will return a LoadBalancer object by its ID.

Args:
api_token (str): DigitalOcean API token
id (str): Load Balancer ID


def __init__(self, message):

        super(CryptographicFailure, self).__init__(
            reason=enums.ResultReason.CRYPTOGRAPHIC_FAILURE,
            message=message
        )

Create a CryptographicFailure exception.

Args:
message (string): A string containing information about the error.


def element(self, using, value):

        return self._execute(Command.FIND_CHILD_ELEMENT, {
            'using': using,
            'value': value
        })

find an element in the current element.

Support:
Android iOS Web(WebView)

Args:
using(str): The element location strategy.
value(str): The value of the location strategy.

Returns:
WebElement Object.

Raises:
WebDriverException.


def get_pending_reboot():
    checks = (get_pending_update, get_pending_file_rename, get_pending_servermanager, get_pending_component_servicing, get_reboot_required_witnessed, get_pending_computer_name, get_pending_domain_join)
    for check in checks:
        if check():
            return True
    return False

Determine whether there is a reboot pending.

.. versionadded:: 2016.11.0

Returns:
bool: ``True`` if the system is pending reboot, otherwise ``False``

CLI Example:

.. code-block:: bash

salt '*' system.get_pending_reboot


def _BuildIntersection(self, type_list):
    type_list = tuple(type_list)
    if len(type_list) == 1:
        return type_list[0]
    else:
        return ' and '.join(type_list)

Builds a intersection of the types in type_list.

Args:
type_list: A list of strings representing types.

Returns:
A string representing the intersection of the types in type_list.
Simplifies Intersection[X] to X and Intersection[X, None] to Optional[X].


def get_watermarks(self, applied_ptransform: AppliedPTransform) -> '_TransformWatermarks':
    while applied_ptransform.parts:
        applied_ptransform = applied_ptransform.parts[-1]
    return self._transform_to_watermarks[applied_ptransform]

Gets the input and output watermarks for an AppliedPTransform.

If the applied_ptransform has not processed any elements, return a
watermark with minimum value.

Args:
applied_ptransform: AppliedPTransform to get the watermarks for.

Returns:
A snapshot (TransformWatermarks) of the input watermark and output
watermark for the provided transform.


def _AssertProtoDictEquals(self, expected_dict, actual_dict, verbose=False, update_goldens=False, additional_missing_object_message='', api_version=2):
    diffs = []
    verbose_diffs = []
    expected_keys = set(expected_dict.keys())
    actual_keys = set(actual_dict.keys())
    only_in_expected = expected_keys - actual_keys
    only_in_actual = actual_keys - expected_keys
    all_keys = expected_keys | actual_keys
    updated_keys = []
    for key in all_keys:
        diff_message = ''
        verbose_diff_message = ''
        if key in only_in_expected:
            diff_message = 'Object %s expected but not found (removed). %s' % (key, additional_missing_object_message)
            verbose_diff_message = diff_message
        elif key in only_in_actual:
            diff_message = 'New object %s found (added).' % key
            verbose_diff_message = diff_message
        else:
            self.maxDiff = None
            try:
                self.assertProtoEquals(expected_dict[key], actual_dict[key])
            except AssertionError as e:
                updated_keys.append(key)
                diff_message = 'Change detected in python object: %s.' % key
                verbose_diff_message = str(e)
        if diff_message:
            diffs.append(diff_message)
            verbose_diffs.append(verbose_diff_message)
    if diffs:
        diff_count = len(diffs)
        logging.error(self._test_readme_message)
        logging.error('%d differences found between API and golden.', diff_count)
        if update_goldens:
            logging.warning(self._update_golden_warning)
            for key in only_in_expected:
                filepath = _KeyToFilePath(key, api_version)
                file_io.delete_file(filepath)
            for key in only_in_actual | set(updated_keys):
                filepath = _KeyToFilePath(key, api_version)
                file_io.write_string_to_file(filepath, text_format.MessageToString(actual_dict[key]))
        else:
            for d, verbose_d in zip(diffs, verbose_diffs):
                logging.error('    %s', d)
                logging.error('    %s', verbose_d)
            self.fail('%d differences found between API and golden.' % diff_count)
    else:
        logging.info('No differences found between API and golden.')

Diff given dicts of protobufs and report differences a readable way.

Args:
expected_dict: a dict of TFAPIObject protos constructed from golden files.
actual_dict: a dict of TFAPIObject protos constructed by reading from the
TF package linked to the test.
verbose: Whether to log the full diffs, or simply report which files were
different.
update_goldens: Whether to update goldens when there are diffs found.
additional_missing_object_message: Message to print when a symbol is
missing.
api_version: TensorFlow API version to test.


def wait(animation='elipses', text='', speed=0.2):

    def decorator(func):
        func.animation = animation
        func.speed = speed
        func.text = text

        @wraps(func)
        def wrapper(*args, **kwargs):
            animation = func.animation
            text = func.text
            if not isinstance(animation, (list, tuple)) and \
                    not hasattr(animations, animation):
                text = animation if text == '' else text
                animation = 'elipses'
            wait = Wait(animation=animation, text=text, speed=func.speed)
            wait.start()
            try:
                ret = func(*args, **kwargs)
            finally:
                wait.stop()
            sys.stdout.write('\n')
            return ret
        return wrapper
    return decorator

Decorator for adding wait animation to long running
functions.

Args:
animation (str, tuple): String reference to animation or tuple
with custom animation.
speed (float): Number of seconds each cycle of animation.

Examples:
>>> @animation.wait('bar')
>>> def long_running_function():
>>>     ... 5 seconds later ...
>>>     return


def run_inference(self, batch: Sequence[numpy.ndarray], model: Union[xgboost.Booster, xgboost.XGBModel], inference_args: Optional[dict[str, Any]]=None) -> Iterable[PredictionResult]:
    return self._inference_fn(batch, model, inference_args)

Runs inferences on a batch of 2d numpy arrays.

Args:
batch: A sequence of examples as 2d numpy arrays. Each
row in an array is a single example. The dimensions
must match the dimensions of the data used to train
the model.
model: XGBoost booster or XBGModel (sklearn interface). Must
implement predict(X). Where the parameter X is a 2d numpy array.
inference_args: Any additional arguments for an inference.

Returns:
An Iterable of type PredictionResult.


def _get_data(self) -> BaseFrameManager:

    def iloc(partition, row_internal_indices, col_internal_indices):
        return partition.iloc[(row_internal_indices, col_internal_indices)]
    masked_data = self.parent_data.apply_func_to_indices_both_axis(func=iloc, row_indices=self.index_map.values, col_indices=self.columns_map.values, lazy=False, keep_remaining=False)
    return masked_data

Perform the map step

Returns:
A BaseFrameManager object.


def _wrap_definition_section(source, width):
    index = (source.index('\n') + 1)
    (definitions, max_len) = _get_definitions(source[index:])
    sep = ('\n' + (' ' * (max_len + 4)))
    lines = [source[:index].strip()]
    for (arg, desc) in six.iteritems(definitions):
        wrapped_desc = sep.join(textwrap.wrap(desc, ((width - max_len) - 4)))
        lines.append('  {arg:{size}}  {desc}'.format(arg=arg, size=str(max_len), desc=wrapped_desc))
    return '\n'.join(lines)

Wrap the given definition section string to the current terminal size.

Note:
Auto-adjusts the spacing between terms and definitions.

Args:
source: The section string to wrap.

Returns:
The wrapped section string.


def proportional_char(self, action):

        actions = {'off': 0,
                   'on': 1
                   }
        if action in actions:
            self.send(chr(27)+'p'+action)
        else:
            raise RuntimeError('Invalid action in function proportionalChar')

Specifies proportional characters. When turned on, the character spacing set
with charSpacing.

Args:
action: Turn proportional characters on or off.
Returns:
None
Raises:
RuntimeError: Invalid action.


def _delete_gridfs_data(self, data):

        if isinstance(data, ObjectId):
            if self._gridfs.exists({"_id": data}):
                self._gridfs.delete(data)
            else:
                raise DataStoreGridfsIdInvalid()
        elif isinstance(data, list):
            for item in data:
                self._delete_gridfs_data(item)
        elif isinstance(data, dict):
            for key, item in data.items():
                self._delete_gridfs_data(item)

Delete all GridFS data that is linked by fields in the specified data.

Args:
data: The data that is parsed for MongoDB ObjectIDs. The linked GridFs object
for any ObjectID is deleted.


def TerminateAFF4Flow(cls, flow_id, reason=None, status=None, token=None):
    flow_obj = aff4.FACTORY.Open(flow_id, aff4_type=GRRFlow, mode='rw', token=token)
    if (not flow_obj):
        raise FlowError(('Could not terminate flow %s' % flow_id))
    with flow_obj:
        runner = flow_obj.GetRunner()
        if (not runner.IsRunning()):
            return
        if (token is None):
            token = access_control.ACLToken()
        if (reason is None):
            reason = 'Manual termination by console.'
        runner.Error(reason, status_code=status)
        flow_obj.Log('Terminated by user {0}. Reason: {1}'.format(token.username, reason))
        super_token = token.SetUID()
        children_to_kill = aff4.FACTORY.MultiOpen(flow_obj.ListChildren(), token=super_token, aff4_type=GRRFlow)
        for child_obj in children_to_kill:
            cls.TerminateAFF4Flow(child_obj.urn, reason='Parent flow terminated.', token=super_token)

Terminate a flow.

Args:
flow_id: The flow session_id to terminate.
reason: A reason to log.
status: Status code used in the generated status message.
token: The access token to be used for this request.

Raises:
FlowError: If the flow can not be found.


def hflip(img):
    if (not _is_pil_image(img)):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
    return img.transpose(Image.FLIP_LEFT_RIGHT)

Horizontally flip the given PIL Image.

Args:
img (PIL Image): Image to be flipped.

Returns:
PIL Image:  Horizontall flipped image.


def process_tree(self, root_directory, output_root_directory, copy_other_files):
    if output_root_directory == root_directory:
        return self.process_tree_inplace(root_directory)
    if output_root_directory and os.path.exists(output_root_directory):
        print('Output directory %r must not already exist.' % output_root_directory)
        sys.exit(1)
    norm_root = os.path.split(os.path.normpath(root_directory))
    norm_output = os.path.split(os.path.normpath(output_root_directory))
    if norm_root == norm_output:
        print('Output directory %r same as input directory %r' % (root_directory, output_root_directory))
        sys.exit(1)
    files_to_process = []
    files_to_copy = []
    for dir_name, _, file_list in os.walk(root_directory):
        py_files = [f for f in file_list if f.endswith('.py')]
        copy_files = [f for f in file_list if not f.endswith('.py')]
        for filename in py_files:
            fullpath = os.path.join(dir_name, filename)
            fullpath_output = os.path.join(output_root_directory, os.path.relpath(fullpath, root_directory))
            files_to_process.append((fullpath, fullpath_output))
        if copy_other_files:
            for filename in copy_files:
                fullpath = os.path.join(dir_name, filename)
                fullpath_output = os.path.join(output_root_directory, os.path.relpath(fullpath, root_directory))
                files_to_copy.append((fullpath, fullpath_output))
    file_count = 0
    tree_errors = {}
    report = ''
    report += '=' * 80 + '\n'
    report += 'Input tree: %r\n' % root_directory
    report += '=' * 80 + '\n'
    for input_path, output_path in files_to_process:
        output_directory = os.path.dirname(output_path)
        if not os.path.isdir(output_directory):
            os.makedirs(output_directory)
        if os.path.islink(input_path):
            link_target = os.readlink(input_path)
            link_target_output = os.path.join(output_root_directory, os.path.relpath(link_target, root_directory))
            if (link_target, link_target_output) in files_to_process:
                os.symlink(link_target_output, output_path)
            else:
                report += 'Copying symlink %s without modifying its target %s' % (input_path, link_target)
                os.symlink(link_target, output_path)
            continue
        file_count += 1
        _, l_report, l_errors = self.process_file(input_path, output_path)
        tree_errors[input_path] = l_errors
        report += l_report
    for input_path, output_path in files_to_copy:
        output_directory = os.path.dirname(output_path)
        if not os.path.isdir(output_directory):
            os.makedirs(output_directory)
        shutil.copy(input_path, output_path)
    return (file_count, report, tree_errors)

Processes upgrades on an entire tree of python files in place.

Note that only Python files. If you have custom code in other languages,
you will need to manually upgrade those.

Args:
root_directory: Directory to walk and process.
output_root_directory: Directory to use as base.
copy_other_files: Copy files that are not touched by this converter.

Returns:
A tuple of files processed, the report string for all files, and a dict
mapping filenames to errors encountered in that file.


def list_all_eq_to(list_, val, strict=True):
    if (util_type.HAVE_NUMPY and isinstance(val, np.ndarray)):
        return all([np.all((item == val)) for item in list_])
    try:
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=FutureWarning)
            flags = [(item == val) for item in list_]
            return all([(np.all(flag) if hasattr(flag, '__array__') else flag) for flag in flags])
    except ValueError:
        if (not strict):
            return all([(repr(item) == repr(val)) for item in list_])
        else:
            raise

checks to see if list is equal everywhere to a value

Args:
list_ (list):
val : value to check against

Returns:
True if all items in the list are equal to val


def read_tabular(filepath):
    (_, fn, ext) = splitext2(filepath)
    if (ext == '.h5'):
        return _read_tabular_h5(filepath)
    elif (ext == '.pkl'):
        return _read_tabular_pickle(filepath)
    else:
        raise NotImplementedError

Read tabular object in HDF5 or pickle format

Args:
filepath (path-like): path to read to; must end in '.h5' or '.pkl'


def _deserialize(self, entity, p, unused_depth=1):

    if p.meaning() == entity_pb.Property.EMPTY_LIST:
      self._store_value(entity, [])
      return

    val = self._db_get_value(p.value(), p)
    if val is not None:
      val = _BaseValue(val)

    if self._repeated:
      if self._has_value(entity):
        value = self._retrieve_value(entity)
        assert isinstance(value, list), repr(value)
        value.append(val)
      else:

        value = [val]
    else:
      value = val
    self._store_value(entity, value)

Internal helper to deserialize this property from a protocol buffer.

Subclasses may override this method.

Args:
entity: The entity, a Model (subclass) instance.
p: A Property Message object (a protocol buffer).
depth: Optional nesting depth, default 1 (unused here, but used
by some subclasses that override this method).


def save_parameters(self, path, grad_only=False):

        params = self.get_parameters(grad_only=grad_only)
        nn.save_parameters(path, params)

Save all parameters into a file with the specified format.

Currently hdf5 and protobuf formats are supported.

Args:
path : path or file object
grad_only (bool, optional): Return parameters with `need_grad` option as `True`.


def add_gene_panel(self, panel_obj):

        panel_name = panel_obj['panel_name']
        panel_version = panel_obj['version']
        display_name = panel_obj.get('display_name', panel_name)

        if self.gene_panel(panel_name, panel_version):
            raise IntegrityError("Panel {0} with version {1} already"
                                 " exist in database".format(panel_name, panel_version))
        LOG.info("loading panel {0}, version {1} to database".format(
            display_name, panel_version
        ))
        result = self.panel_collection.insert_one(panel_obj)
        LOG.debug("Panel saved")
        return result.inserted_id

Add a gene panel to the database

Args:
panel_obj(dict)


def get_value_for_datastore(self, model_instance):
    value = super(JsonProperty, self).get_value_for_datastore(model_instance)
    if (not value):
        return None
    json_value = value
    if (not isinstance(value, dict)):
        json_value = value.to_json()
    if (not json_value):
        return None
    return datastore_types.Text(json.dumps(json_value, sort_keys=True, cls=JsonEncoder))

Gets value for datastore.

Args:
model_instance: instance of the model class.

Returns:
datastore-compatible value.


def split_input(cls, job_config):

    reader_params = job_config.input_reader_params
    bucket = reader_params[cls.BUCKET_NAME_PARAM]
    filenames = reader_params[cls.OBJECT_NAMES_PARAM]
    delimiter = reader_params.get(cls.DELIMITER_PARAM)
    account_id = reader_params.get(cls._ACCOUNT_ID_PARAM)
    buffer_size = reader_params.get(cls.BUFFER_SIZE_PARAM)
    path_filter = reader_params.get(cls.PATH_FILTER_PARAM)

    all_filenames = []
    for filename in filenames:
      if filename.endswith("*"):
        all_filenames.extend(
            [file_stat.filename for file_stat in cloudstorage.listbucket(
                "/" + bucket + "/" + filename[:-1], delimiter=delimiter,
                _account_id=account_id)])
      else:
        all_filenames.append("/%s/%s" % (bucket, filename))

    readers = []
    for shard in range(0, job_config.shard_count):
      shard_filenames = all_filenames[shard::job_config.shard_count]
      if shard_filenames:
        readers.append(cls(
            shard_filenames, buffer_size=buffer_size, _account_id=account_id,
            delimiter=delimiter, path_filter=path_filter))
    return readers

Returns a list of input readers.

An equal number of input files are assigned to each shard (+/- 1). If there
are fewer files than shards, fewer than the requested number of shards will
be used. Input files are currently never split (although for some formats
could be and may be split in a future implementation).

Args:
job_config: map_job.JobConfig

Returns:
A list of InputReaders. None when no input data can be found.


def forward(self, context: torch.Tensor, latents: torch.Tensor) -> torch.Tensor:
    context = self.context_layer_norm(context)
    latents = self.latents_layer_norm(latents)
    batch_size, seq_length, embed_dim = context.shape[:3]
    q = self.q_proj(latents)
    k = self.k_proj(torch.cat([context, latents], dim=-2))
    v = self.v_proj(torch.cat([context, latents], dim=-2))
    q, k, v = [x.reshape(batch_size, x.shape[1], self.n_heads, self.head_dim).transpose(1, 2) for x in (q, k, v)]
    if self.qk_layer_norms:
        q = self.q_layer_norm(q)
        k = self.k_layer_norm(k)
    scores = torch.einsum('... i d, ... j d -> ... i j', q * self.qk_scale, k)
    stabilized_scores = scores - scores.amax(dim=-1, keepdim=True).detach()
    attn = stabilized_scores.softmax(dim=-1)
    resampled = torch.einsum('... i j, ... j d -> ... i d', attn, v)
    return self.output_proj(resampled.transpose(1, 2).flatten(-2))

Runs Perceiver Self-Attention, with special (context, latents) appended along the `seq` dimension!

Args:
context (`torch.Tensor`):
Tensor of shape `[bsz, seq, embed_dim]` representing long-form context to resample.
latents (`torch.Tensor`):
Tensor of shape `[bsz, n_latents, embed_dim]` representing fixed length latents to compress to.

Returns:
`torch.Tensor`: Tensor of shape `[bsz, n_latents, embed_dim]` representing attention over latents w/ cross
from context.


def _wait_after(provider, job_ids, poll_interval, stop_on_failure):

  job_ids_to_check = {j for j in job_ids if j != dsub_util.NO_JOB}
  error_messages = []
  while job_ids_to_check and (not error_messages or not stop_on_failure):
    print('Waiting for: %s.' % (', '.join(job_ids_to_check)))

    jobs_left = _wait_for_any_job(provider, job_ids_to_check, poll_interval)

    jobs_completed = job_ids_to_check.difference(jobs_left)

    tasks_completed = provider.lookup_job_tasks({'*'}, job_ids=jobs_completed)

    dominant_job_tasks = _dominant_task_for_jobs(tasks_completed)
    if len(dominant_job_tasks) != len(jobs_completed):

      jobs_found = dsub_util.tasks_to_job_ids(dominant_job_tasks)
      jobs_not_found = jobs_completed.difference(jobs_found)
      for j in jobs_not_found:
        error = '%s: not found' % j
        print_error('  %s' % error)
        error_messages += [error]

    for t in dominant_job_tasks:
      job_id = t.get_field('job-id')
      status = t.get_field('task-status')
      print('  %s: %s' % (str(job_id), str(status)))
      if status in ['FAILURE', 'CANCELED']:
        error_messages += [provider.get_tasks_completion_messages([t])]

    job_ids_to_check = jobs_left

  return error_messages

Print status info as we wait for those jobs.

Blocks until either all of the listed jobs succeed,
or one of them fails.

Args:
provider: job service provider
job_ids: a set of job IDs (string) to wait for
poll_interval: integer seconds to wait between iterations
stop_on_failure: whether to stop waiting if one of the tasks fails.

Returns:
Empty list if there was no error,
a list of error messages from the failed tasks otherwise.


def clone(self, name=None):

    if name is None:
      name = self.module_name + "_clone"
    return MLP(
        name=name,
        output_sizes=self.output_sizes,
        activation=self.activation,
        activate_final=self.activate_final,
        initializers=self.initializers,
        partitioners=self.partitioners,
        regularizers=self.regularizers,
        use_bias=self.use_bias,
        use_dropout=self.use_dropout)

Creates a new MLP with the same structure.

Args:
name: Optional string specifying the name of the new module. The default
name is constructed by appending "_clone" to the original name.

Returns:
A cloned `MLP` module.


def dump_json(json_info, json_file, overwrite=True):

    if overwrite:
        mode = "w"
    else:
        mode = "w+"

    try:
        with open(json_file, mode) as f:
            f.write(json.dumps(json_info))
    except BaseException as e:
        logging.error(e.message)

Dump a whole json record into the given file.

Overwrite the file if the overwrite flag set.

Args:
json_info (dict): Information dict to be dumped.
json_file (str): File path to be dumped to.
overwrite(boolean)


def _dump_to_pages(dump):

  pos = 0
  ret = []
  start_tag = u"<page>\n"
  end_tag = u"</page>\n"
  while True:
    start_pos = dump.find(start_tag, pos)
    if start_pos == -1:
      break
    start_pos += len(start_tag)
    end_pos = dump.find(end_tag, start_pos)
    if end_pos == -1:
      break
    ret.append(dump[start_pos:end_pos])
    pos = end_pos + len(end_tag)
  return ret

Extract pages from an xml dump.

Args:
dump: a unicode string
Returns:
a list of unicode strings


def generate_output_whois_nets(self, json_data=None, hr=True, show_name=False, colorize=True):
    if (json_data is None):
        json_data = {}
    output = generate_output(line='0', short=(HR_WHOIS['nets']['_short'] if hr else 'nets'), name=(HR_WHOIS['nets']['_name'] if (hr and show_name) else None), is_parent=True, colorize=colorize)
    count = 0
    for net in json_data['nets']:
        if (count > 0):
            output += self.generate_output_newline(line='1', colorize=colorize)
        count += 1
        output += generate_output(line='1', short=net['handle'], is_parent=True, colorize=colorize)
        for (key, val) in net.items():
            if (val and ('\n' in val)):
                output += generate_output(line='2', short=(HR_WHOIS['nets'][key]['_short'] if hr else key), name=(HR_WHOIS['nets'][key]['_name'] if (hr and show_name) else None), is_parent=(False if ((val is None) or (len(val) == 0)) else True), value=('None' if ((val is None) or (len(val) == 0)) else None), colorize=colorize)
                for v in val.split('\n'):
                    output += generate_output(line='3', value=v, colorize=colorize)
            else:
                output += generate_output(line='2', short=(HR_WHOIS['nets'][key]['_short'] if hr else key), name=(HR_WHOIS['nets'][key]['_name'] if (hr and show_name) else None), value=val, colorize=colorize)
    return output

The function for generating CLI output Legacy Whois networks results.

Args:
json_data (:obj:`dict`): The data to process. Defaults to None.
hr (:obj:`bool`): Enable human readable key translations. Defaults
to True.
show_name (:obj:`bool`): Show human readable name (default is to
only show short). Defaults to False.
colorize (:obj:`bool`): Colorize the console output with ANSI
colors. Defaults to True.

Returns:
str: The generated output.


def set_wallpaper(image):

	desktop_env = system.get_name()

	if desktop_env in ['gnome', 'unity', 'cinnamon', 'pantheon', 'mate']:
		uri = 'file:

		SCHEMA = 'org.gnome.desktop.background'
		KEY = 'picture-uri'

		if desktop_env == 'mate':
			uri = image

			SCHEMA = 'org.mate.background'
			KEY = 'picture-filename'

		try:
			from gi.repository import Gio

			gsettings = Gio.Settings.new(SCHEMA)
			gsettings.set_string(KEY, uri)
		except ImportError:
			try:
				gsettings_proc = sp.Popen(
					['gsettings', 'set', SCHEMA, KEY, uri])
			except:
				sp.Popen(['mateconftool-2',
						  '-t',
						  'string',
						  '--set',
						  '/desktop/mate/background/picture_filename',
						  '%s' % image],
						 stdout=sp.PIPE)
			finally:
				gsettings_proc.communicate()

				if gsettings_proc.returncode != 0:
					sp.Popen(['mateconftool-2',
							  '-t',
							  'string',
							  '--set',
							  '/desktop/mate/background/picture_filename',
							  '%s' % image])

	elif desktop_env == 'gnome2':
		sp.Popen(
			['gconftool-2',
			 '-t',
			 'string',
			 '--set',
			 '/desktop/gnome/background/picture_filename',
			 image]
		)

	elif desktop_env == 'kde':

		kde_script = dedent(
		).format(image)

		sp.Popen(
				['dbus-send',
				 '--session',
				 '--dest=org.kde.plasmashell',
				 '--type=method_call',
				 '/PlasmaShell',
				 'org.kde.PlasmaShell.evaluateScript',
				 'string:{}'.format(kde_script)]
		)

	elif desktop_env in ['kde3', 'trinity']:
		args = 'dcop kdesktop KBackgroundIface setWallpaper 0 "%s" 6' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'xfce4':

		list_of_properties = system.get_cmd_out(
				['xfconf-query',
				 '-R',
				 '-l',
				 '-c',
				 'xfce4-desktop',
				 '-p',
				 '/backdrop']
		)

		for i in list_of_properties.split('\n'):
			if i.endswith('last-image'):

				sp.Popen(
					['xfconf-query -c xfce4-desktop -p %s -s "%s"' %
						(i, image)],
					shell=True)

				sp.Popen(['xfdesktop --reload'], shell=True)

	elif desktop_env == 'razor-qt':
		desktop_conf = configparser.ConfigParser()

		desktop_conf_file = os.path.join(
			get_config_dir('razor')[0], 'desktop.conf')

		if os.path.isfile(desktop_conf_file):
			config_option = r'screens\1\desktops\1\wallpaper'

		else:
			desktop_conf_file = os.path.join(
				os.path.expanduser('~'), '.razor/desktop.conf')
			config_option = r'desktops\1\wallpaper'

		desktop_conf.read(os.path.join(desktop_conf_file))
		try:
			if desktop_conf.has_option('razor', config_option):
				desktop_conf.set('razor', config_option, image)
				with codecs.open(desktop_conf_file, 'w', encoding='utf-8', errors='replace') as f:
					desktop_conf.write(f)
		except:
			pass

	elif desktop_env in ['fluxbox', 'jwm', 'openbox', 'afterstep', 'i3']:
		try:
			args = ['feh', '--bg-scale', image]
			sp.Popen(args)
		except:
			sys.stderr.write('Error: Failed to set wallpaper with feh!')
			sys.stderr.write('Please make sre that You have feh installed.')

	elif desktop_env == 'icewm':
		args = ['icewmbg', image]
		sp.Popen(args)

	elif desktop_env == 'blackbox':
		args = ['bsetbg', '-full', image]
		sp.Popen(args)

	elif desktop_env == 'lxde':
		args = 'pcmanfm --set-wallpaper %s --wallpaper-mode=scaled' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'lxqt':
		args = 'pcmanfm-qt --set-wallpaper %s --wallpaper-mode=scaled' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'windowmaker':
		args = 'wmsetbg -s -u %s' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'enlightenment':
		args = 'enlightenment_remote -desktop-bg-add 0 0 0 0 %s' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'awesome':
		with sp.Popen("awesome-client", stdin=sp.PIPE) as awesome_client:
			command = ('local gears = require("gears"); for s = 1,'
						' screen.count() do gears.wallpaper.maximized'
						'("%s", s, true); end;') % image
			awesome_client.communicate(input=bytes(command, 'UTF-8'))

	elif desktop_env == 'windows':
		WINDOWS_SCRIPT = dedent() % image

		windows_script_file = os.path.join(
			tempfile.gettempdir(), 'wallscript.bat')

		with open(windows_script_file, 'w') as f:
			f.write(WINDOWS_SCRIPT)

		sp.Popen([windows_script_file], shell=True)

		SPI_SETDESKWALLPAPER = 20
		ctypes.windll.user32.SystemParametersInfoA(
			SPI_SETDESKWALLPAPER, 0, image, 0)

	elif desktop_env == 'mac':
		try:
			from appscript import app, mactypes
			app('Finder').desktop_picture.set(mactypes.File(image))
		except ImportError:
			OSX_SCRIPT = dedent(
				) % image

			sp.Popen(['osascript', OSX_SCRIPT])
	else:
		try:
			sp.Popen(['feh', '--bg-scale', image])

		except:
			pass

Set the desktop wallpaper.

Sets the desktop wallpaper to an image.

Args:
image (str): The path to the image to be set as wallpaper.


def midpoint(self):
    midpoints = []
    for segment in self:
        if (len(segment) < 2):
            midpoints.append([])
        else:
            midpoints.append(segment.midpoint())
    return midpoints

Calculate the midpoint between locations in segments.

Returns:
list of Point: Groups of midpoint between points in segments


def get_task_ops(task_type=TaskType.ALG_CTRL):
    try:
        return LearnToExecuteState.TASK_TYPE_OPS[task_type]
    except KeyError:
        raise KeyError(("Bad task_type '%s', check config." % task_type))

Returns an operations list based on the specified task index.

Args:
task_type: indicates the task type used.

Returns:
List of the eligible ops.


def add_action_to(cls, parser, action, subactions, level):

        p = parser.add_parser(action.name,
                              description=action.description,
                              argument_default=argparse.SUPPRESS)
        for arg in action.args:
            arg.add_argument_to(p)

        if subactions:
            subparsers = cls._add_subparsers_required(p,
                dest=settings.SUBASSISTANT_N_STRING.format(level),
                title=cls.subactions_str,
                description=cls.subactions_desc)
            for subact, subsubacts in sorted(subactions.items(), key=lambda x: x[0].name):
                cls.add_action_to(subparsers, subact, subsubacts, level + 1)

Adds given action to given parser

Args:
parser: instance of devassistant_argparse.ArgumentParser
action: devassistant.actions.Action subclass
subactions: dict with subactions - {SubA: {SubB: {}}, SubC: {}}


def search(self, search_phrase, limit=None):

        query_parts = [
            'SELECT identifier, type, name, similarity(name, :word) AS sml',
            'FROM identifier_index',
            'WHERE name % :word',
            'ORDER BY sml DESC, name']

        query_params = {
            'word': search_phrase}

        if limit:
            query_parts.append('LIMIT :limit')
            query_params['limit'] = limit

        query_parts.append(';')

        query = text('\n'.join(query_parts))

        self.backend.library.database.set_connection_search_path()

        results = self.execute(query, **query_params).fetchall()

        for result in results:
            vid, type, name, score = result
            yield IdentifierSearchResult(
                score=score, vid=vid,
                type=type, name=name)

Finds identifiers by search phrase.

Args:
search_phrase (str or unicode):
limit (int, optional): how many results to return. None means without limit.

Returns:
list of IdentifierSearchResult instances.


def expo(base=2, factor=1, max_value=None):

    n = 0
    while True:
        a = factor * base ** n
        if max_value is None or a < max_value:
            yield a
            n += 1
        else:
            yield max_value

Generator for exponential decay.

Args:
base: The mathematical base of the exponentiation operation
factor: Factor to multiply the exponentation by.
max_value: The maximum value to yield. Once the value in the
true exponential sequence exceeds this, the value
of max_value will forever after be yielded.


async def async_fetch(url: str, **kwargs) -> Selector:
    kwargs.setdefault('headers', DEFAULT_HEADERS)
    async with aiohttp.ClientSession(**kwargs) as ses:
        async with ses.get(url, **kwargs) as res:
            html = (await res.text())
            tree = Selector(text=html)
            return tree

Do the fetch in an async style.

Args:
url (str): The url of the site.

Returns:
Selector: allows you to select parts of HTML text using CSS or XPath expressions.


def scan_servos():

    servos = []
    for servo_id in range(0x00, 0xFE):
        model = get_model(servo_id)
        if model:
            servos += [(servo_id, model)]
    return servos

Scan for the herkulex servos connected

This function will scan for all the herkulex servos connected
to the bus.

Args:
none
Returns:
list: a list of tuples of the form [(id, model)]


def init_from_wave_file(wavpath):

        try:
            samplerate, data =  SW.read(wavpath)
            nframes = data.shape[0]
        except:

            try:
                w = wave.open(wavpath)
                samplerate = w.getframerate()
                nframes = w.getnframes()
            except:
                raise Exception('Cannot decode wavefile ' + wavpath)

        return SVEnv(samplerate, nframes, wavpath)

Init a sonic visualiser environment structure based the analysis
of the main audio file. The audio file have to be encoded in wave

Args:
wavpath(str): the full path to the wavfile


def _get_char_input_ids(self, input_ids, subwords_batch, char_count_per_id, pad_token_id=0, unk_token_id=1):
    if not hasattr(self.generation_config, 'char_to_id'):
        raise ValueError("This model generation config doesn't have a `char_to_id` key which maps\n                characters to character ids. Make sure to load the right generation config.")
    batch_size = input_ids.shape[0]
    max_len = int(char_count_per_id.sum(1).max().item())
    char_seqs = input_ids.new_zeros((batch_size, max_len)).fill_(pad_token_id)
    subword_lens = input_ids.ne(pad_token_id).sum(1)
    for batch_id in range(batch_size):
        total = 0
        subword_indices = input_ids[batch_id, :subword_lens[batch_id]]
        subwords = subwords_batch[batch_id][:subword_lens[batch_id]]
        for subword_idx, subword in zip(subword_indices, subwords):
            if subword_idx == unk_token_id:
                char_ids = [unk_token_id]
            else:
                char_ids = [self.generation_config.char_to_id.get(ch, unk_token_id) for ch in list(subword)]
            char_seq_len = len(char_ids)
            char_seqs[batch_id, total:total + char_seq_len] = torch.tensor(char_ids).to(char_seqs)
            total += char_seq_len
    return char_seqs

Returns the corresponding character input id for each character of `subwords_batch`.

Args:
input_ids (`torch.Tensor` of shape `(batch_size, sequence_length)`):
Indices of input sequence tokens in the vocabulary.
subwords_batch (`List[List[str]]` of shape `(batch_size, sequence_length)`):
Corresponding text string for each input id.
char_count_per_id (`torch.Tensor` of shape `(batch_size, sequence_length)`):
Number of characters per input id.
pad_token_id (`int`, *optional*, defaults to 0):
The id of the _padding_ text token. If it is encountered when calculating the length of a subword
sample, the lengths of subsequent subwords will be set to 0.
unk_token_id (`int`, *optional*, defaults to 1):
The id of the _unknown_ text token. Associated to a subword of length 1.
Returns:
`torch.Tensor`: Tensor of shape `(batch_size, char_sequence_length)` containing the id of each character.


def _get_colors(n):

    import matplotlib.pyplot as plt
    from matplotlib.colors import rgb2hex as r2h
    from numpy import linspace

    cols = linspace(0.05, .95, n)
    cmap = plt.get_cmap('nipy_spectral')
    return [r2h(cmap(i)) for i in cols]

Returns n unique and "evenly" spaced colors for the backgrounds
of the projects.

Args:
n (int): The number of unique colors wanted.

Returns:
colors (list of str): The colors in hex form.


def convert_ini(config_dict):

    config_lines = []

    for env, configs in sorted(config_dict.items()):
        for resource, app_properties in sorted(configs.items()):
            try:
                for app_property, value in sorted(app_properties.items()):
                    variable = '{env}_{resource}_{app_property}'.format(
                        env=env, resource=resource, app_property=app_property).upper()

                    if isinstance(value, (dict, DeepChainMap)):
                        safe_value = "'{0}'".format(json.dumps(dict(value)))
                    else:
                        safe_value = json.dumps(value)

                    line = "{variable}={value}".format(variable=variable, value=safe_value)

                    LOG.debug('INI line: %s', line)
                    config_lines.append(line)
            except AttributeError:
                resource = resource.upper()
                app_properties = "'{}'".format(json.dumps(app_properties))
                line = '{0}={1}'.format(resource, app_properties)

                LOG.debug('INI line: %s', line)
                config_lines.append(line)
    return config_lines

Convert _config_dict_ into a list of INI formatted strings.

Args:
config_dict (dict): Configuration dictionary to be flattened.

Returns:
(list) Lines to be written to a file in the format of KEY1_KEY2=value.


def AsDict(self, dt=True):
    data = {}
    if self.body:
        data['body'] = self.body
    if self.posted_at:
        data['posted_at'] = self.posted_at
    if self.user:
        data['user'] = self.user.AsDict()
    return data

A dict representation of this Comment instance.

The return value uses the same key names as the JSON representation.

Args:
dt (bool): If True, return dates as python datetime objects. If
False, return dates as ISO strings.

Return:
A dict representing this Comment instance


def num_connected_components(self, unitary_only=False):
    reg_offset = 0
    reg_map = {}
    if unitary_only:
        regs = self.qregs
    else:
        regs = (self.qregs + self.cregs)
    for reg in regs:
        reg_map[reg.name] = reg_offset
        reg_offset += reg.size
    sub_graphs = [[bit] for bit in range(reg_offset)]
    num_sub_graphs = len(sub_graphs)
    for (instr, qargs, cargs) in self.data:
        if unitary_only:
            args = qargs
            num_qargs = len(args)
        else:
            args = (qargs + cargs)
            num_qargs = (len(args) + (1 if instr.control else 0))
        if ((num_qargs >= 2) and (instr.name not in ['barrier', 'snapshot'])):
            graphs_touched = []
            num_touched = 0
            if (instr.control and (not unitary_only)):
                creg = instr.control[0]
                creg_int = reg_map[creg.name]
                for coff in range(creg.size):
                    temp_int = (creg_int + coff)
                    for k in range(num_sub_graphs):
                        if (temp_int in sub_graphs[k]):
                            graphs_touched.append(k)
                            num_touched += 1
                            break
            for item in args:
                reg_int = (reg_map[item[0].name] + item[1])
                for k in range(num_sub_graphs):
                    if (reg_int in sub_graphs[k]):
                        if (k not in graphs_touched):
                            graphs_touched.append(k)
                            num_touched += 1
                            break
            if (num_touched > 1):
                connections = []
                for idx in graphs_touched:
                    connections.extend(sub_graphs[idx])
                _sub_graphs = []
                for idx in range(num_sub_graphs):
                    if (idx not in graphs_touched):
                        _sub_graphs.append(sub_graphs[idx])
                _sub_graphs.append(connections)
                sub_graphs = _sub_graphs
                num_sub_graphs -= (num_touched - 1)
        if (num_sub_graphs == 1):
            break
    return num_sub_graphs

How many non-entangled subcircuits can the circuit be factored to.

Args:
unitary_only (bool): Compute only unitary part of graph.

Returns:
int: Number of connected components in circuit.


def _get_contrib_features(module):
    if isinstance(module, types.ModuleType):
        if hasattr(module, '__path__'):
            (yield from _get_contrib_features_from_package(module))
        else:
            (yield _get_contrib_feature_from_module(module))
    else:
        raise ValueError('Input is not a module')

Get contributed features from within given module

Be very careful with untrusted code. The module/package will be
walked, every submodule will be imported, and all the code therein will be
executed. But why would you be trying to import from an untrusted package
anyway?

Args:
contrib (module): module (standalone or package) that contains feature
definitions

Returns:
List[Feature]: list of features


async def create(self, coro: Coroutine) -> asyncio.Task:
    task = asyncio.get_event_loop().create_task(coro)
    self._tasks.add(task)
    return task

Starts execution of a coroutine.

The created asyncio.Task is returned, and added to managed tasks.
The scheduler guarantees that it is cancelled during application shutdown,
regardless of whether it was already cancelled manually.

Args:
coro (Coroutine):
The coroutine to be wrapped in a task, and executed.

Returns:
asyncio.Task: An awaitable Task object.
During Aiohttp shutdown, the scheduler will attempt to cancel and await this task.
The task can be safely cancelled manually, or using `TaskScheduler.cancel(task)`.


def set_all_tiers(key, value, django_cache_timeout=DEFAULT_TIMEOUT):
    DEFAULT_REQUEST_CACHE.set(key, value)
    django_cache.set(key, value, django_cache_timeout)

Caches the value for the provided key in both the request cache and the
django cache.

Args:
key (string)
value (object)
django_cache_timeout (int): (Optional) Timeout used to determine
if and for how long to cache in the django cache. A timeout of
0 will skip the django cache. If timeout is provided, use that
timeout for the key; otherwise use the default cache timeout.


def validate(self, value):

        cast_callback = self.cast_callback if self.cast_callback else self.cast_type

        try:
            return value if isinstance(value, self.cast_type) else cast_callback(value)

        except Exception:
            raise NodeTypeError('Invalid value `{}` for {}.'.format(value, self.cast_type))

Base validation method. Check if type is valid, or try brute casting.

Args:
value (object): A value for validation.

Returns:
Base_type instance.

Raises:
SchemaError, if validation or type casting fails.


def prepare_loss_weights(training_endpoints, loss_weights=None):
    if loss_weights is None:
        for e in training_endpoints:
            e.loss_weight = 1.0
    elif isinstance(loss_weights, collections.abc.Mapping):
        generic_utils.check_for_unexpected_keys('loss_weights', loss_weights, [e.output_name for e in training_endpoints])
        for e in training_endpoints:
            e.loss_weight = loss_weights.get(e.output_name, 1.0)
    elif isinstance(loss_weights, list):
        if len(loss_weights) != len(training_endpoints):
            raise ValueError('When passing a list as loss_weights, it should have one entry per model output. The model has ' + str(len(training_endpoints)) + ' outputs, but you passed loss_weights=' + str(loss_weights))
        for w, e in zip(loss_weights, training_endpoints):
            e.loss_weight = w
    else:
        raise TypeError('Could not interpret loss_weights argument: ' + str(loss_weights) + ' - expected a list of dicts.')

Converts loss weights to a list of loss weights.

The result loss weights will be populated on the training endpoint.

Args:
training_endpoints: List of model training endpoints.
loss_weights: Optional list or dictionary specifying scalar coefficients
(Python floats) to weight the loss contributions of different model
outputs. The loss value that will be minimized by the model will then be
the *weighted sum* of all individual losses, weighted by the
`loss_weights` coefficients. If a list, it is expected to have a 1:1
mapping to the model's outputs. If a dict, it is expected to map
output names (strings) to scalar coefficients.

Raises:
ValueError: If loss weight is a dict with key not in model output names,
or if loss is a list with len not equal to model outputs.


def match(self, request):

        for test in self.filters:
            if not test(request, self):
                return False

        for mapper in self.mappers:
            request = mapper(request, self)
            if not request:
                raise ValueError('map function must return a request object')

        match_errors = []

        for mock in self.mocks[:]:
            try:

                matches, errors = mock.match(request.copy())
                if len(errors):
                    match_errors += errors
                if matches:
                    return mock
            except PookExpiredMock:

                self.mocks.remove(mock)

        if not self.should_use_network(request):
            msg = 'pook error!\n\n'

            msg += (
                '=> Cannot match any mock for the '
                'following request:\n{}'.format(request)
            )

            if self.debug:
                err = '\n\n'.join([str(err) for err in match_errors])
                if err:
                    msg += '\n\n=> Detailed matching errors:\n{}\n'.format(err)

            raise PookNoMatches(msg)

        self.unmatched_reqs.append(request)

Matches a given Request instance contract against the registered mocks.

If a mock passes all the matchers, its response will be returned.

Arguments:
request (pook.Request): Request contract to match.

Raises:
pook.PookNoMatches: if networking is disabled and no mock matches
with the given request contract.

Returns:
pook.Response: the mock response to be used by the interceptor.


def get_alignment_df_from_file(alignment_file, a_seq_id=None, b_seq_id=None):

    alignments = list(AlignIO.parse(alignment_file, "emboss"))
    alignment_df = pd.DataFrame(columns=['id_a', 'id_b', 'type', 'id_a_aa', 'id_a_pos', 'id_b_aa', 'id_b_pos'])

    for alignment in alignments:
        if not a_seq_id:
            a_seq_id = list(alignment)[0].id
        a_seq = str(list(alignment)[0].seq)
        if not b_seq_id:
            b_seq_id = list(alignment)[1].id
        b_seq = str(list(alignment)[1].seq)

        df = get_alignment_df(a_seq, b_seq, a_seq_id, b_seq_id)
        alignment_df = alignment_df.append(df).reset_index(drop=True)

    return alignment_df

Get a Pandas DataFrame of the Needle alignment results. Contains all positions of the sequences.

Args:
alignment_file:
a_seq_id: Optional specification of the ID of the reference sequence
b_seq_id: Optional specification of the ID of the aligned sequence

Returns:
Pandas DataFrame: all positions in the alignment


def to_json(self):
    cursor = self._get_cursor()
    cursor_object = False
    if (cursor and isinstance(cursor, datastore_query.Cursor)):
        cursor = cursor.to_websafe_string()
        cursor_object = True
    return {'key_range': self._key_range.to_json(), 'query_spec': self._query_spec.to_json(), 'cursor': cursor, 'cursor_object': cursor_object}

Serializes all states into json form.

Returns:
all states in json-compatible map.


def __init__(self, minimum=-18446744073709551616, maximum=18446744073709551615, singleStep=1, parent=None):

        super(BigIntSpinboxDelegate, self).__init__(parent)
        self.minimum = minimum
        self.maximum = maximum
        self.singleStep = singleStep

construct a new instance of a BigIntSpinboxDelegate.

Args:
maximum (int or long, optional): minimum allowed number in BigIntSpinbox. defaults to -18446744073709551616.
minimum (int or long, optional): maximum allowed number in BigIntSpinbox. defaults to 18446744073709551615.
singleStep (int, optional): amount of steps to stepUp BigIntSpinbox. defaults to 1.


def images(self, **kwargs):

        path = self._get_series_id_season_number_path('images')

        response = self._GET(path, kwargs)
        self._set_attrs_to_values(response)
        return response

Get the images (posters) that we have stored for a TV season by season
number.

Args:
language: (optional) ISO 639 code.
include_image_language: (optional) Comma separated, a valid
ISO 69-1.

Returns:
A dict respresentation of the JSON returned from the API.


def add_note(self, note):
    notes = self.cached_json
    if (not note.moderator):
        note.moderator = self.r.user.me().name
    try:
        mod_index = notes['constants']['users'].index(note.moderator)
    except ValueError:
        notes['constants']['users'].append(note.moderator)
        mod_index = notes['constants']['users'].index(note.moderator)
    try:
        warn_index = notes['constants']['warnings'].index(note.warning)
    except ValueError:
        if (note.warning in Note.warnings):
            notes['constants']['warnings'].append(note.warning)
            warn_index = notes['constants']['warnings'].index(note.warning)
        else:
            raise ValueError(('Warning type not valid: ' + note.warning))
    new_note = {'n': note.note, 't': note.time, 'm': mod_index, 'l': note.link, 'w': warn_index}
    try:
        notes['users'][note.username]['ns'].insert(0, new_note)
    except KeyError:
        notes['users'][note.username] = {'ns': [new_note]}
    return '"create new note on user {}" via puni'.format(note.username)

Add a note to the usernotes wiki page.

Arguments:
note: the note to be added (Note)

Returns the update message for the usernotes wiki

Raises:
ValueError when the warning type of the note can not be found in the
stored list of warnings.


def GetFormattedSources(self, event):
    event_formatter = self.GetEventFormatter(event)
    if (not event_formatter):
        return (None, None)
    return event_formatter.GetSources(event)

Retrieves the formatted sources related to the event.

Args:
event (EventObject): event.

Returns:
tuple: containing:

str: full source string or None if no event formatter was found.
str: short source string or None if no event formatter was found.


def create_endpoints_csv_file(self, timeout=-1):

        uri = "{}/endpoints/".format(self.data["uri"])
        return self._helper.do_post(uri, {}, timeout, None)

Creates an endpoints CSV file for a SAN.

Args:
timeout:
Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation in
OneView, just stops waiting for its completion.

Returns:
dict: Endpoint CSV File Response.


def compute_author_match_score(x_authors, y_authors):

    if not x_authors or not y_authors:
        return 0.0

    matches = get_number_of_author_matches(x_authors, y_authors)
    max_length = max(len(x_authors), len(y_authors))

    return matches / float(max_length)

Return the matching score of 2 given lists of authors.

Args:
x_authors (list(dict)): first schema-compliant list of authors.
y_authors (list(dict)): second schema-compliant list of authors.

Returns:
float: matching score of authors.


def set_time(self, value: float):
    if (value < 0):
        value = 0
    self.controller.row = (self.rps * value)

Set the current time jumping in the timeline.

Args:
value (float): The new time


def run_ui(self, init_command=None, title=None, title_color=None, enable_mouse_on_start=True):
    raise NotImplementedError('run_ui() is not implemented in BaseUI')

Run the UI until user- or command- triggered exit.

Args:
init_command: (str) Optional command to run on CLI start up.
title: (str) Optional title to display in the CLI.
title_color: (str) Optional color of the title, e.g., "yellow".
enable_mouse_on_start: (bool) Whether the mouse mode is to be enabled on
start-up.

Returns:
An exit token of arbitrary type. Can be None.


def _SetHashers(self, hasher_names_string):
    if ((not hasher_names_string) or (hasher_names_string == 'none')):
        return
    analyzer_object = analyzers_manager.AnalyzersManager.GetAnalyzerInstance('hashing')
    analyzer_object.SetHasherNames(hasher_names_string)
    self._analyzers.append(analyzer_object)

Sets the hasher names.

Args:
hasher_names_string (str): comma separated names of the hashers
to enable, where 'none' disables the hashing analyzer.


def GetHashers(cls, hasher_names):

    hashers = []
    for hasher_name, hasher_class in iter(cls._hasher_classes.items()):
      if hasher_name in hasher_names:
        hashers.append(hasher_class())

    return hashers

Retrieves instances for all the specified hashers.

Args:
hasher_names (list[str]): names of the hashers to retrieve.

Returns:
list[BaseHasher]: hashers.


def verify_dataset(X, y):
    (X_shape, y_shape) = (np.array(X).shape, np.array(y).shape)
    if (len(X_shape) != 2):
        raise exceptions.UserError('X must be 2-dimensional array')
    if (len(y_shape) != 1):
        raise exceptions.UserError('y must be 1-dimensional array')
    if (X_shape[0] != y_shape[0]):
        raise exceptions.UserError('X must have same number of elements as y')
    return dict(features_shape=X_shape, labels_shape=y_shape)

Verifies if a dataset is valid for use i.e. scikit-learn format

Used to verify a dataset by returning shape and basic statistics of
returned data. This will also provide quick and dirty check on
capability of host machine to process the data.

Args:
X (array-like): Features array

y (array-like): Label array

Returns:
X_shape (2-tuple of int): Shape of X returned

y_shape (1-tuple of int): Shape of y returned

Raises:
AssertionError: `X_shape` must be of length 2 and `y_shape` must be of
length 1. `X` must have the same number of elements as `y`
i.e. X_shape[0] == y_shape[0]. If any of these conditions are not met,
an AssertionError is raised.


def call(self):
    (headers, data) = self.prepare()
    if _LOG.isEnabledFor(logging.DEBUG):
        _LOG.debug('Sending %s, %s', headers, prettify(data))
    response = requests.post(self.endpoint, headers=headers, data=data.encode('utf-8'), **self.request_args)
    _LOG.debug('Received %s, %s', response.headers, response.text)
    status = response.status_code
    if (status == 200):
        tree = XML.fromstring(response.content)
        body = tree.find('{http:
        return body
    elif (status == 500):
        tree = XML.fromstring(response.content)
        fault = tree.find('.
        if (fault is None):
            response.raise_for_status()
        faultcode = fault.findtext('faultcode')
        faultstring = fault.findtext('faultstring')
        faultdetail = fault.find('detail')
        raise SoapFault(faultcode, faultstring, faultdetail)
    else:
        response.raise_for_status()
    return None

Call the SOAP method on the server.

Returns:
str: the decapusulated SOAP response from the server,
still encoded as utf-8.

Raises:
SoapFault: if a SOAP error occurs.
~requests.exceptions.HTTPError: if an http error occurs.