def _TravelTimes(self, triplist, index=0):

    def DistanceInTravelTime(dep_secs, arr_secs):
        t_dist = (arr_secs - dep_secs)
        if (t_dist < 0):
            t_dist = self._DUMMY_SEPARATOR
        return t_dist
    if (not triplist):
        return []
    if (0 < index < len(triplist)):
        trip = triplist[index]
    else:
        trip = triplist[0]
    t_dists2 = [DistanceInTravelTime(stop[3], tail[2]) for (stop, tail) in itertools.izip(trip.GetTimeStops(), trip.GetTimeStops()[1:])]
    return t_dists2

Calculate distances and plot stops.

Uses a timetable to approximate distances
between stations

Args:
# Class Trip is defined in transitfeed.py
triplist: [Trip, Trip, ...]
# (Optional) Index of Triplist prefered for timetable Calculation
index: 3

Returns:
# One integer for each pair of stations
# indicating the approximate distance
[0,33,140, ... ,X]


def module_import(module_path):

    try:

        module = __import__(module_path)

        components = module_path.split('.')

        for component in components[1:]:
            module = getattr(module, component)

        return module

    except ImportError:
        raise BadModulePathError(
            'Unable to find module "%s".' % (module_path,))

Imports the module indicated in name

Args:
module_path: string representing a module path such as
'app.config' or 'app.extras.my_module'
Returns:
the module matching name of the last component, ie: for
'app.extras.my_module' it returns a
reference to my_module
Raises:
BadModulePathError if the module is not found


def list(cls, session, endpoint_override=None, data=None):

        cls._check_implements('list')
        return cls(
            endpoint_override or '/%s.json' % cls.__endpoint__,
            data=data,
            session=session,
        )

Return records in a mailbox.

Args:
session (requests.sessions.Session): Authenticated session.
endpoint_override (str, optional): Override the default
endpoint using this.
data (dict, optional): Data to provide as request parameters.

Returns:
RequestPaginator(output_type=helpscout.BaseModel): Results
iterator.


def available_resources(self):
    available_resources_by_id = {}
    subscribe_clients = [redis_client.pubsub(ignore_subscribe_messages=True) for redis_client in self.redis_clients]
    for subscribe_client in subscribe_clients:
        subscribe_client.subscribe(ray.gcs_utils.XRAY_HEARTBEAT_CHANNEL)
    client_ids = self._live_client_ids()
    while (set(available_resources_by_id.keys()) != client_ids):
        for subscribe_client in subscribe_clients:
            raw_message = subscribe_client.get_message()
            if ((raw_message is None) or (raw_message['channel'] != ray.gcs_utils.XRAY_HEARTBEAT_CHANNEL)):
                continue
            data = raw_message['data']
            gcs_entries = ray.gcs_utils.GcsTableEntry.GetRootAsGcsTableEntry(data, 0)
            heartbeat_data = gcs_entries.Entries(0)
            message = ray.gcs_utils.HeartbeatTableData.GetRootAsHeartbeatTableData(heartbeat_data, 0)
            num_resources = message.ResourcesAvailableLabelLength()
            dynamic_resources = {}
            for i in range(num_resources):
                resource_id = decode(message.ResourcesAvailableLabel(i))
                dynamic_resources[resource_id] = message.ResourcesAvailableCapacity(i)
            client_id = ray.utils.binary_to_hex(message.ClientId())
            available_resources_by_id[client_id] = dynamic_resources
        client_ids = self._live_client_ids()
        for client_id in available_resources_by_id.keys():
            if (client_id not in client_ids):
                del available_resources_by_id[client_id]
    total_available_resources = defaultdict(int)
    for available_resources in available_resources_by_id.values():
        for (resource_id, num_available) in available_resources.items():
            total_available_resources[resource_id] += num_available
    for subscribe_client in subscribe_clients:
        subscribe_client.close()
    return dict(total_available_resources)

Get the current available cluster resources.

This is different from `cluster_resources` in that this will return
idle (available) resources rather than total resources.

Note that this information can grow stale as tasks start and finish.

Returns:
A dictionary mapping resource name to the total quantity of that
resource in the cluster.


def __init__(self, label, element=None, state=None):
    self.label = label
    self.state = state
    if element is not None:
        self.set_element(element)

Initialize a processing context object with an element and state.

The element represents one value from a PCollection that will be accessed
by a DoFn object during pipeline execution, and state is an arbitrary object
where counters and other pipeline state information can be passed in.

DoFnProcessContext objects are also used as inputs to PartitionFn instances.

Args:
label: label of the PCollection whose element is being processed.
element: element of a PCollection being processed using this context.
state: a DoFnState object with state to be passed in to the DoFn object.


def __init__(self, prefix, section, filename=None):

        options = dict(prefix=prefix, section=section, filename=filename)

        super(Settings, self).__init__(**options)

Will try to read configuration from environment variables and ini
files, if no value found in either of those ``None`` is
returned.

Args:
prefix: The environment variable prefix.
section: The ini file section this configuration is scoped to
filename: The path to the ini file to use


def first(self):
    try:
        return next(self.data(sorted_by='energy', name='Sample'))
    except StopIteration:
        raise ValueError('{} is empty'.format(self.__class__.__name__))

Sample with the lowest-energy.

Raises:
ValueError: If empty.

Example:

>>> sampleset = dimod.ExactSolver().sample_ising({'a': 1}, {('a', 'b'): 1})
>>> sampleset.first
Sample(sample={'a': -1, 'b': 1}, energy=-2.0, num_occurrences=1)


def GetLayerFromFeatureService(self, fs, layerName='', returnURLOnly=False):
    layers = None
    table = None
    layer = None
    sublayer = None
    try:
        layers = fs.layers
        if (((layers is None) or (len(layers) == 0)) and (fs.url is not None)):
            fs = arcrest.ags.FeatureService(url=fs.url)
            layers = fs.layers
        if (layers is not None):
            for layer in layers:
                if (layer.name == layerName):
                    if returnURLOnly:
                        return ((fs.url + '/') + str(layer.id))
                    else:
                        return layer
                elif (not (layer.subLayers is None)):
                    for sublayer in layer.subLayers:
                        if (sublayer == layerName):
                            return sublayer
        if (fs.tables is not None):
            for table in fs.tables:
                if (table.name == layerName):
                    if returnURLOnly:
                        return ((fs.url + '/') + str(layer.id))
                    else:
                        return table
        return None
    except:
        (line, filename, synerror) = trace()
        raise common.ArcRestHelperError({'function': 'GetLayerFromFeatureService', 'line': line, 'filename': filename, 'synerror': synerror})
    finally:
        layers = None
        table = None
        layer = None
        sublayer = None
        del layers
        del table
        del layer
        del sublayer
        gc.collect()

Obtains a layer from a feature service by feature service reference.

Args:
fs (FeatureService): The feature service from which to obtain the layer.
layerName (str): The name of the layer. Defaults to ``""``.
returnURLOnly (bool): A boolean value to return the URL of the layer. Defaults to ``False``.
Returns:
When ``returnURLOnly`` is ``True``, the URL of the layer is returned.

When ``False``, the result from :py:func:`arcrest.agol.services.FeatureService` or :py:func:`arcrest.ags.services.FeatureService`.


def __init__(
        self,
        names,
        aggregation_type='concat',
        axis=1,
        named_tensors=None,
        scope='input',
        summary_labels=()
    ):

        self.names = names
        self.aggregation_type = aggregation_type
        self.axis = axis
        super(Input, self).__init__(named_tensors=named_tensors, scope=scope, summary_labels=summary_labels)

Input layer.

Args:
names: A list of strings that name the inputs to merge
axis: Axis to merge the inputs


def dbmax20years(self, value=None):

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

        self._dbmax20years = value

Corresponds to IDD Field `dbmax20years`
20-year return period values for maximum extreme dry-bulb temperature

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

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


def __setitem__(self, key, value):

    if key is None:
      key = self.default_key(value)
    if key in self:
      raise KeyError(
          "key %s already registered in registry %s" % (key, self._name))
    if not callable(value):
      raise ValueError("value must be callable")
    self.validate(key, value)
    self._registry[key] = value
    self.on_set(key, value)

Validate, set, and (if successful) call `on_set` for the given item.

Args:
key: key to store value under. If `None`, `self.default_key(value)` is
used.
value: callable stored under the given key.

Raises:
KeyError: if key is already in registry.


def _get_blocks_containing_index(self, axis, index):
    if (not axis):
        ErrorMessage.catch_bugs_and_request_email((index > sum(self.block_widths)))
        cumulative_column_widths = np.array(self.block_widths).cumsum()
        block_idx = int(np.digitize(index, cumulative_column_widths))
        if (block_idx == len(cumulative_column_widths)):
            block_idx -= 1
        internal_idx = (index if (not block_idx) else (index - cumulative_column_widths[(block_idx - 1)]))
    else:
        ErrorMessage.catch_bugs_and_request_email((index > sum(self.block_lengths)))
        cumulative_row_lengths = np.array(self.block_lengths).cumsum()
        block_idx = int(np.digitize(index, cumulative_row_lengths))
        internal_idx = (index if (not block_idx) else (index - cumulative_row_lengths[(block_idx - 1)]))
    return (block_idx, internal_idx)

Convert a global index to a block index and local index.

Note: This method is primarily used to convert a global index into a
partition index (along the axis provided) and local index (useful
for `iloc` or similar operations.

Args:
axis: The axis along which to get the indices
(0 - columns, 1 - rows)
index: The global index to convert.

Returns:
A tuple containing (block index and internal index).


def _get_job_resources(args):

  logging = param_util.build_logging_param(
      args.logging) if args.logging else None
  timeout = param_util.timeout_in_seconds(args.timeout)
  log_interval = param_util.log_interval_in_seconds(args.log_interval)

  return job_model.Resources(
      min_cores=args.min_cores,
      min_ram=args.min_ram,
      machine_type=args.machine_type,
      disk_size=args.disk_size,
      disk_type=args.disk_type,
      boot_disk_size=args.boot_disk_size,
      preemptible=args.preemptible,
      image=args.image,
      regions=args.regions,
      zones=args.zones,
      logging=logging,
      logging_path=None,
      service_account=args.service_account,
      scopes=args.scopes,
      keep_alive=args.keep_alive,
      cpu_platform=args.cpu_platform,
      network=args.network,
      subnetwork=args.subnetwork,
      use_private_address=args.use_private_address,
      accelerator_type=args.accelerator_type,
      accelerator_count=args.accelerator_count,
      nvidia_driver_version=args.nvidia_driver_version,
      timeout=timeout,
      log_interval=log_interval,
      ssh=args.ssh)

Extract job-global resources requirements from input args.

Args:
args: parsed command-line arguments

Returns:
Resources object containing the requested resources for the job


def get_palette(self, num_labels: int) -> List[Tuple[int, int]]:
    return build_palette(num_labels)

Build a palette to map the prompt mask from a single channel to a 3 channel RGB.

Args:
num_labels (`int`):
Number of classes in the segmentation task (excluding the background).

Returns:
`List[Tuple[int, int]]`: Palette to map the prompt mask from a single channel to a 3 channel RGB.


def run_step(context):

    logger.debug("started")

    context.clear()
    logger.info(f"Context wiped. New context size: {len(context)}")

    logger.debug("done")

Wipe the entire context.

Args:
Context is a dictionary or dictionary-like.
Does not require any specific keys in context.


def from_nested_row_splits(cls, flat_values, nested_row_splits, name=None, validate=True):
    if not isinstance(validate, bool):
        raise TypeError(f'Argument `validate` must have type bool. Received {validate}.')
    if isinstance(nested_row_splits, tensor_lib.Tensor):
        raise TypeError(f'Argument `nested_row_splits` must be a list of Tensors. Received {nested_row_splits}.')
    with ops.name_scope(name, 'RaggedFromNestedRowSplits', [flat_values] + list(nested_row_splits)):
        result = flat_values
        for splits in reversed(nested_row_splits):
            result = cls.from_row_splits(result, splits, validate=validate)
        return result

Creates a `RaggedTensor` from a nested list of `row_splits` tensors.

Equivalent to:

```python
result = flat_values
for row_splits in reversed(nested_row_splits):
result = from_row_splits(result, row_splits)
```

Args:
flat_values: A potentially ragged tensor.
nested_row_splits: A list of 1-D integer tensors.  The `i`th tensor is
used as the `row_splits` for the `i`th ragged dimension.
name: A name prefix for the RaggedTensor (optional).
validate: If true, then use assertions to check that the arguments form
a valid `RaggedTensor`.  Note: these assertions incur a runtime cost,
since they must be checked for each tensor value.

Returns:
A `RaggedTensor` (or `flat_values` if `nested_row_splits` is empty).


def regex(self, *patterns, **kwargs):
    start = kwargs.pop('start', 0)
    stop = kwargs.pop('stop', None)
    keys_only = kwargs.pop('keys_only', False)
    flags = kwargs.pop('flags', 0)
    results = {pattern: [] for pattern in patterns}
    stop = (stop if (stop is not None) else (- 1))
    for (i, line) in enumerate(self[start:stop]):
        for pattern in patterns:
            grps = re.search(pattern, line, flags=flags)
            if (grps and keys_only):
                results[pattern].append(i)
            elif (grps and grps.groups()):
                for group in grps.groups():
                    results[pattern].append((i, group))
            elif grps:
                results[pattern].append((i, line))
    if (len(patterns) == 1):
        return results[patterns[0]]
    return results

Search the editor for lines matching the regular expression.
re.MULTILINE is not currently supported.

Args:
\*patterns: Regular expressions to search each line for
keys_only (bool): Only return keys
flags (re.FLAG): flags passed to re.search

Returns:
results (dict): Dictionary of pattern keys, line values (or groups - default)


def visit_inner_types(type_constraint, visitor, visitor_arg):
    if isinstance(type_constraint, TypeConstraint):
        return type_constraint.visit(visitor, visitor_arg)
    return visitor(type_constraint, visitor_arg)

Visitor pattern to visit all inner types of a type constraint.

Args:
type_constraint: A type constraint or a type.
visitor: A callable invoked for all nodes in the type tree comprising a
composite type. The visitor will be called with the node visited and the
visitor argument specified here.
visitor_arg: Visitor callback second argument.

Note:
Raise and capture a StopIteration to terminate the visit, e.g.

```
def visitor(type_constraint, visitor_arg):
if ...:
raise StopIteration

try:
visit_inner_types(type_constraint, visitor, visitor_arg)
except StopIteration:
pass
```


def set_room_topic(self, room_id, topic, timestamp=None):

        body = {
            "topic": topic
        }
        return self.send_state_event(room_id, "m.room.topic", body, timestamp=timestamp)

Perform PUT /rooms/$room_id/state/m.room.topic
Args:
room_id (str): The room ID
topic (str): The new room topic
timestamp (int): Set origin_server_ts (For application services only)


def clean_output_files(self, follow_parents=True):
    paths = []
    if (self.status != self.S_OK):
        logger.warning('Calling task.clean_output_files on a task whose status != S_OK')
    self.tmpdir.clean()
    except_exts = set()
    for child in self.get_children():
        if (child.status == self.S_OK):
            continue
        i = [dep.node for dep in child.deps].index(self)
        except_exts.update(child.deps[i].exts)
    exts = self.gc.exts.difference(except_exts)
    paths += self.outdir.remove_exts(exts)
    if (not follow_parents):
        return paths
    for parent in self.get_parents():
        ext2nodes = collections.defaultdict(list)
        for child in parent.get_children():
            if (child.status == child.S_OK):
                continue
            i = [d.node for d in child.deps].index(parent)
            for ext in child.deps[i].exts:
                ext2nodes[ext].append(child)
        except_exts = [k for (k, lst) in ext2nodes.items() if lst]
        exts = self.gc.exts.difference(except_exts)
        paths += parent.outdir.remove_exts(exts)
    self.history.info(('Removed files: %s' % paths))
    return paths

This method is called when the task reaches S_OK. It removes all the output files
produced by the task that are not needed by its children as well as the output files
produced by its parents if no other node needs them.

Args:
follow_parents: If true, the output files of the parents nodes will be removed if possible.

Return:
list with the absolute paths of the files that have been removed.


def _distort_color(image, color_ordering=0, scope=None):
    with tf.name_scope(scope, 'distort_color', [image]):
        if (color_ordering == 0):
            image = tf.image.random_brightness(image, max_delta=(32.0 / 255.0))
            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
            image = tf.image.random_hue(image, max_delta=0.2)
            image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
        elif (color_ordering == 1):
            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
            image = tf.image.random_brightness(image, max_delta=(32.0 / 255.0))
            image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
            image = tf.image.random_hue(image, max_delta=0.2)
        elif (color_ordering == 2):
            image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
            image = tf.image.random_hue(image, max_delta=0.2)
            image = tf.image.random_brightness(image, max_delta=(32.0 / 255.0))
            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
        elif (color_ordering == 3):
            image = tf.image.random_hue(image, max_delta=0.2)
            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
            image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
            image = tf.image.random_brightness(image, max_delta=(32.0 / 255.0))
        else:
            raise ValueError('color_ordering must be in [0, 3]')
        return tf.clip_by_value(image, 0.0, 1.0)

Distort the color of a Tensor image.

Each color distortion is non-commutative and thus ordering of the color ops
matters. Ideally we would randomly permute the ordering of the color ops.
Rather then adding that level of complication, we select a distinct ordering
of color ops for each preprocessing thread.

Args:
image: 3-D Tensor containing single image in [0, 1].
color_ordering: Python int, a type of distortion (valid values: 0-3).
scope: Optional scope for name_scope.
Returns:
3-D Tensor color-distorted image on range [0, 1]
Raises:
ValueError: if color_ordering not in [0, 3]


def pretty_repr_top_level(obj: Any, *, force: bool=False) -> str:
    if isinstance(obj, str):
        return repr(obj)
    elif py_utils.is_namedtuple(obj):
        return Lines.make_block(header=obj.__class__.__name__, content={field_name: getattr(obj, field_name) for field_name in type(obj)._fields})
    elif type(obj) in (list, tuple):
        lines = Lines.make_block(content=obj, braces='[' if isinstance(obj, list) else '(')
        if isinstance(obj, tuple) and len(obj) == 1:
            lines = lines.removesuffix(')') + ',)'
        return lines
    elif type(obj) is dict:
        return Lines.make_block(content={repr(k): v for k, v in obj.items()}, braces='{', equal=': ')
    elif _is_datclass(obj, force=force):
        all_fields = dataclasses.fields(obj)
        return Lines.make_block(header=obj.__class__.__name__, content={field.name: getattr(obj, field.name) for field in all_fields if field.repr})
    elif _is_attr(obj, force=force):
        import attr
        all_fields = attr.fields_dict(type(obj))
        return Lines.make_block(header=obj.__class__.__name__, content={field.name: getattr(obj, field.name) for field in all_fields.values() if field.repr})
    else:
        return repr(obj)

Pretty `repr(obj)` for nested list, dict, dataclasses,...

This version do not use `@reprlib.recursive_repr()` to avoid bug when used
inside `__repr__`:

```python
class A:
def __repr__(self):
return epy.pretty_repr_top_level(self)

epy.pretty_repr(A())  # Do not display `...`
```

Args:
obj: Object to display
force: Force the pretty_repr, even if the object has a custom `__repr__`.
This is useful when the `__repr__` implementation itself want to call
`pretty_repr(self)`.

Returns:
Repr


def __init__(self, context_reading_spec, sequence_reading_spec):

    self._context_reading_spec = context_reading_spec
    self._sequence_reading_spec = sequence_reading_spec

Constructs a TFRecordSequenceExampleAdapter.

Args:
context_reading_spec: `dict`, feature name to tf.FixedLenFeature or
tf.VarLenFeature for the context features. Passed to
tf.io.parse_single_sequence_example.
sequence_reading_spec: `dict`, feature name to tf.FixedLenFeature or
tf.VarLenFeature for the sequence features. Passed to
tf.io.parse_single_sequence_example.


def square(times: np.ndarray, amp: complex, period: float, phase: float=0) -> np.ndarray:
    x = ((times / period) + (phase / np.pi))
    return (amp * ((2 * ((2 * np.floor(x)) - np.floor((2 * x)))) + 1).astype(np.complex_))

Continuous square wave.

Args:
times: Times to output wave for.
amp: Pulse amplitude. Wave range is [-amp, amp].
period: Pulse period, units of dt.
phase: Pulse phase.


def openning(input_rasterfilename, times):
    input_raster = RasterUtilClass.read_raster(input_rasterfilename)
    openning_raster = input_raster
    for i in range(times):
        openning_raster = RasterUtilClass.raster_erosion(openning_raster)
    for i in range(times):
        openning_raster = RasterUtilClass.raster_dilation(openning_raster)
    return openning_raster

Do openning.

Openning: Erode firstly, then Dilate.

Args:
input_rasterfilename: input original raster image filename.
times: Erode and Dilate times.

Returns:
openning_raster: raster image after open.


def copy_files(self, files=None, path_patterns=None, symbolic_links=True, root=None, conflicts='fail', **kwargs):
    _files = self.get(return_type='objects', **kwargs)
    if files:
        _files = list(set(files).intersection(_files))
    for f in _files:
        f.copy(path_patterns, symbolic_link=symbolic_links, root=self.root, conflicts=conflicts)

Copies one or more BIDSFiles to new locations defined by each
BIDSFile's entities and the specified path_patterns.

Args:
files (list): Optional list of BIDSFile objects to write out. If
none provided, use files from running a get() query using
remaining **kwargs.
path_patterns (str, list): Write patterns to pass to each file's
write_file method.
symbolic_links (bool): Whether to copy each file as a symbolic link
or a deep copy.
root (str): Optional root directory that all patterns are relative
to. Defaults to current working directory.
conflicts (str):  Defines the desired action when the output path
already exists. Must be one of:
'fail': raises an exception
'skip' does nothing
'overwrite': overwrites the existing file
'append': adds  a suffix to each file copy, starting with 1
kwargs (kwargs): Optional key word arguments to pass into a get()
query.


def __init__(self, initial_loss_scale=2 ** 15, increment_period=2000, multiplier=2.0):
    super(DynamicLossScale, self).__init__()
    self._initial_loss_scale = float(initial_loss_scale)
    self._increment_period = int(increment_period)
    self._multiplier = float(multiplier)
    self._current_loss_scale = self._add_weight(name='current_loss_scale', dtype=dtypes.float32, initial_value=self._initial_loss_scale)
    self._num_good_steps = self._add_weight(name='good_steps', dtype=dtypes.int64, initial_value=0)

Creates the dynamic loss scale.

Args:
initial_loss_scale: A Python float.  The loss scale to use at the
beginning. It's better to start this at a very high number, because a
loss scale that is too high gets lowered far more quickly than a loss
scale that is too low gets raised. The default is 2 ** 15, which is
approximately half the maximum float16 value.
increment_period: Increases loss scale every `increment_period`
consecutive steps that finite gradients are encountered. If a nonfinite
gradient is encountered, the count is reset back to zero.
multiplier: The multiplier to use when increasing or decreasing the loss
scale.


def to_dict(ramons, flatten=False):
    if (type(ramons) is not list):
        ramons = [ramons]
    out_ramons = {}
    for r in ramons:
        out_ramons[r.id] = {'id': r.id, 'type': _reverse_ramon_types[type(r)], 'metadata': vars(r)}
    return out_ramons

Converts a RAMON object list to a JSON-style dictionary. Useful for going
from an array of RAMONs to a dictionary, indexed by ID.

Arguments:
ramons (RAMON[]): A list of RAMON objects
flatten (boolean: False): Not implemented

Returns:
dict: A python dictionary of RAMON objects.


def _ExpandDirectories(filenames):

  expanded = set()
  for filename in filenames:
      if not os.path.isdir(filename):
        expanded.add(filename)
        continue

      for root, _, files in os.walk(filename):
        for loopfile in files:
          fullname = os.path.join(root, loopfile)
          if fullname.startswith('.' + os.path.sep):
            fullname = fullname[len('.' + os.path.sep):]
          expanded.add(fullname)

  filtered = []
  for filename in expanded:
      if os.path.splitext(filename)[1][1:] in GetAllExtensions():
          filtered.append(filename)

  return filtered

Searches a list of filenames and replaces directories in the list with
all files descending from those directories. Files with extensions not in
the valid extensions list are excluded.

Args:
filenames: A list of files or directories

Returns:
A list of all files that are members of filenames or descended from a
directory in filenames


def _execute(self, command, data=None, unpack=True):
    if (not data):
        data = {}
    data.setdefault('element_id', self.element_id)
    return self._driver._execute(command, data, unpack)

Private method to execute command with data.

Args:
command(Command): The defined command.
data(dict): The uri variable and body.

Returns:
The unwrapped value field in the json response.


def _get_ssh_config(config_path='~/.ssh/config'):
    ssh_config = paramiko.SSHConfig()
    try:
        with open(os.path.realpath(os.path.expanduser(config_path))) as f:
            ssh_config.parse(f)
    except IOError:
        pass
    return ssh_config

Extract the configuration located at ``config_path``.

Returns:
paramiko.SSHConfig: the configuration instance.


def status(self):
    if self._future.running():
        _status = JobStatus.RUNNING
    elif self._future.cancelled():
        _status = JobStatus.CANCELLED
    elif self._future.done():
        _status = (JobStatus.DONE if (self._future.exception() is None) else JobStatus.ERROR)
    else:
        _status = JobStatus.INITIALIZING
    return _status

Gets the status of the job by querying the Python's future

Returns:
qiskit.providers.JobStatus: The current JobStatus

Raises:
JobError: If the future is in unexpected state
concurrent.futures.TimeoutError: if timeout occurred.


def resolve_attr(obj, path):

    if not path:
        return obj
    head, _, tail = path.partition('.')
    head_obj = getattr(obj, head)
    return resolve_attr(head_obj, tail)

A recursive version of getattr for navigating dotted paths.

Args:
obj: An object for which we want to retrieve a nested attribute.
path: A dot separated string containing zero or more attribute names.

Returns:
The attribute referred to by obj.a1.a2.a3...

Raises:
AttributeError: If there is no such attribute.


def svds_descending(M, k):

    u, s, vt = svds(M, k=k)

    u = u[:, ::-1]

    s = s[::-1]

    vt = vt[::-1, :]
    return u, np.diag(s), vt.T

In contrast to MATLAB, numpy's svds() arranges the singular
values in ascending order. In order to have matching codes,
we wrap it around by a function which re-sorts the singular
values and singular vectors.
Args:
M: 2D numpy array; the matrix whose SVD is to be computed.
k: Number of singular values to be computed.

Returns:
u, s, vt = svds(M, k=k)


def preprocess_input(features, target, train_config, preprocess_output_dir,
                     model_type):

  target_name = train_config['target_column']
  key_name = train_config['key_column']

  with tf.name_scope('numerical_feature_preprocess'):
    if train_config['numerical_columns']:
      numerical_analysis_file = os.path.join(preprocess_output_dir,
                                             NUMERICAL_ANALYSIS)
      if not file_io.file_exists(numerical_analysis_file):
        raise ValueError('File %s not found in %s' %
                         (NUMERICAL_ANALYSIS, preprocess_output_dir))

      numerical_anlysis = json.loads(
          python_portable_string(
              file_io.read_file_to_string(numerical_analysis_file)))

      for name in train_config['numerical_columns']:
        if name == target_name or name == key_name:
          continue

        transform_config = train_config['transforms'].get(name, {})
        transform_name = transform_config.get('transform', None)
        if transform_name == 'scale':
          value = float(transform_config.get('value', 1.0))
          features[name] = _scale_tensor(
              features[name],
              range_min=numerical_anlysis[name]['min'],
              range_max=numerical_anlysis[name]['max'],
              scale_min=-value,
              scale_max=value)
        elif transform_name == 'identity' or transform_name is None:
          pass
        else:
          raise ValueError(('For numerical variables, only scale '
                            'and identity are supported: '
                            'Error for %s') % name)

  if target is not None:
    with tf.name_scope('target_feature_preprocess'):
      if target_name in train_config['categorical_columns']:
        labels = train_config['vocab_stats'][target_name]['labels']
        table = tf.contrib.lookup.string_to_index_table_from_tensor(labels)
        target = table.lookup(target)

  with tf.name_scope('categorical_feature_preprocess'):
    for name in train_config['categorical_columns']:
      if name == key_name or name == target_name:
        continue
      transform_config = train_config['transforms'].get(name, {})
      transform_name = transform_config.get('transform', None)

      if is_dnn_model(model_type):
        if transform_name == 'embedding' or transform_name == 'one_hot' or transform_name is None:
          map_vocab = True
        else:
          raise ValueError('Unknown transform %s' % transform_name)
      elif is_linear_model(model_type):
        if (transform_name == 'one_hot' or transform_name is None):
          map_vocab = True
        elif transform_name == 'embedding':
          map_vocab = False
        else:
          raise ValueError('Unknown transform %s' % transform_name)
      if map_vocab:
        labels = train_config['vocab_stats'][name]['labels']
        table = tf.contrib.lookup.string_to_index_table_from_tensor(labels)
        features[name] = table.lookup(features[name])

  return features, target

Perform some transformations after reading in the input tensors.

Args:
features: dict of feature_name to tensor
target: tensor
train_config: our training config object
preprocess_output_dir: folder should contain the vocab files.
model_type: the tf model type.

Raises:
ValueError: if wrong transforms are used

Returns:
New features dict and new target tensor.


def ToStream(value):

        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)

        value.Serialize(writer)

        retVal = ms.getvalue()
        StreamManager.ReleaseStream(ms)

        return retVal

Serialize the given `value` to a an array of bytes.

Args:
value (neo.IO.Mixins.SerializableMixin): object extending SerializableMixin.

Returns:
bytes: not hexlified


def check_usufy(self, query, **kwargs):

        data = self.launchQueryForMode(query=query, mode="usufy")
        if self._somethingFound(data, mode="usufy"):
            return data
        return None

Verifying a mailfy query in this platform.

This might be redefined in any class inheriting from Platform. The only
condition is that any of this should return a dictionary as defined.

Args:
-----
query: The element to be searched.
kwargs: Dictionary with extra parameters. Just in case.

Return:
-------
Returns the collected data if exists or None if not.


def split_pair(pair_string, separator, nullable_idx=1):

  pair = pair_string.split(separator, 1)
  if len(pair) == 1:
    if nullable_idx == 0:
      return [None, pair[0]]
    elif nullable_idx == 1:
      return [pair[0], None]
    else:
      raise IndexError('nullable_idx should be either 0 or 1.')
  else:
    return pair

Split a string into a pair, which can have one empty value.

Args:
pair_string: The string to be split.
separator: The separator to be used for splitting.
nullable_idx: The location to be set to null if the separator is not in the
input string. Should be either 0 or 1.

Returns:
A list containing the pair.

Raises:
IndexError: If nullable_idx is not 0 or 1.


def _pool_one_shape(features_2d, area_width, area_height, batch_size, width, height, depth, fn=tf.reduce_max, name=None):
    with tf.name_scope(name, default_name='pool_one_shape'):
        images = []
        for y_shift in range(area_height):
            image_height = tf.maximum((((height - area_height) + 1) + y_shift), 0)
            for x_shift in range(area_width):
                image_width = tf.maximum((((width - area_width) + 1) + x_shift), 0)
                area = features_2d[(:, y_shift:image_height, x_shift:image_width, :)]
                flatten_area = tf.reshape(area, [batch_size, (- 1), depth, 1])
                images.append(flatten_area)
        image_tensor = tf.concat(images, axis=3)
        max_tensor = fn(image_tensor, axis=3)
    return max_tensor

Pools for an area in features_2d.

Args:
features_2d: a Tensor in a shape of [batch_size, height, width, depth].
area_width: the max width allowed for an area.
area_height: the max height allowed for an area.
batch_size: the batch size.
width: the width of the memory.
height: the height of the memory.
depth: the depth of the features.
fn: the TF function for the pooling.
name: the op name.
Returns:
pool_tensor: A Tensor of shape [batch_size, num_areas, depth]


def raise_for_status(
    status: int, headers: MutableMapping, data: MutableMapping
) -> None:

    if status != 200:
        if status == 429:

            if isinstance(data, str):
                error = data
            else:
                error = data.get("error", "ratelimited")

            try:
                retry_after = int(headers.get("Retry-After", 1))
            except ValueError:
                retry_after = 1
            raise exceptions.RateLimited(retry_after, error, status, headers, data)
        else:
            raise exceptions.HTTPException(status, headers, data)

Check request response status

Args:
status: Response status
headers: Response headers
data: Response data

Raises:
:class:`slack.exceptions.RateLimited`: For 429 status code
:class:`slack.exceptions:HTTPException`:


def annotate_and_average(self, gpl, expression_column, group_by_column, rename=True, force=False, merge_on_column=None, gsm_on=None, gpl_on=None):
    if ((gpl.name != self.metadata['platform_id'][0]) and (not force)):
        raise KeyError((('Platforms from GSM (%s) and from GPL (%s)' % (gpl.name, self.metadata['platform_id'])) + ' are incompatible. Use force=True to use this GPL.'))
    if ((merge_on_column is None) and (gpl_on is None) and (gsm_on is None)):
        raise Exception('You have to provide one of the two: merge_on_column or gpl_on and gsm_on parameters')
    if merge_on_column:
        logger.info('merge_on_column is not None. Using this option.')
        tmp_data = self.table.merge(gpl.table, on=merge_on_column, how='outer')
        tmp_data = tmp_data.groupby(group_by_column).mean()[[expression_column]]
    else:
        if ((gpl_on is None) or (gsm_on is None)):
            raise Exception('Please provide both gpl_on and gsm_on or provide merge_on_column only')
        tmp_data = self.table.merge(gpl.table, left_on=gsm_on, right_on=gpl_on, how='outer')
        tmp_data = tmp_data.groupby(group_by_column).mean()[[expression_column]]
    if rename:
        tmp_data.columns = [self.name]
    return tmp_data

Annotate GSM table with provided GPL.

Args:
gpl (:obj:`GEOTypes.GPL`): Platform for annotations
expression_column (:obj:`str`): Column name which "expressions"
are represented
group_by_column (:obj:`str`): The data will be grouped and averaged
over this column and only this column will be kept
rename (:obj:`bool`): Rename output column to the
self.name. Defaults to True.
force (:obj:`bool`): If the name of the GPL does not match the platform
name in GSM proceed anyway. Defaults to False.
merge_on_column (:obj:`str`): Column to merge the data
on. Defaults to None.
gsm_on (:obj:`str`): In the case columns to merge are different in GSM
and GPL use this column in GSM. Defaults to None.
gpl_on (:obj:`str`): In the case columns to merge are different in GSM
and GPL use this column in GPL. Defaults to None.

Returns:
:obj:`pandas.DataFrame`: Annotated data


def coalesce(*series):
    series = [pd.Series(s) for s in series]
    coalescer = pd.concat(series, axis=1)
    min_nonna = np.argmin(pd.isnull(coalescer).values, axis=1)
    min_nonna = [coalescer.columns[i] for i in min_nonna]
    return coalescer.lookup(np.arange(coalescer.shape[0]), min_nonna)

Takes the first non-NaN value in order across the specified series,
returning a new series. Mimics the coalesce function in dplyr and SQL.

Args:
*series: Series objects, typically represented in their symbolic form
(like X.series).

Example:
df = pd.DataFrame({
'a':[1,np.nan,np.nan,np.nan,np.nan],
'b':[2,3,np.nan,np.nan,np.nan],
'c':[np.nan,np.nan,4,5,np.nan],
'd':[6,7,8,9,np.nan]
})
df >> transmute(coal=coalesce(X.a, X.b, X.c, X.d))

coal
0       1
1       3
2       4
3       5
4  np.nan


def make_parser():
    parser = argparse.ArgumentParser(usage='%(prog)s [options] input [input ...]')
    parser.register('action', 'flatten', _FlattenAction)
    modes = parser.add_mutually_exclusive_group()
    modes.add_argument('--tree', dest='tree', action='store_true', default=False, help='Display import tree.')
    modes.add_argument('--unresolved', dest='unresolved', action='store_true', default=False, help='Display unresolved dependencies.')
    modes.add_argument('--generate-config', dest='generate_config', type=str, action='store', default='', help='Write out a dummy configuration file.')
    parser.add_argument('-v', '--verbosity', dest='verbosity', type=int, action='store', default=1, help='Set logging level: 0=ERROR, 1=WARNING (default), 2=INFO.')
    parser.add_argument('--config', dest='config', type=str, action='store', default='', help='Configuration file.')
    parser.add_argument('--version', action='store_true', dest='version', default=None, help='Display pytype version and exit.')
    types = config.make_converters()
    for option in [(('-x', '--exclude'), {'nargs': '*', 'action': 'flatten'}), (('inputs',), {'metavar': 'input', 'nargs': '*', 'action': 'flatten'}), (('-k', '--keep-going'), {'action': 'store_true', 'type': None}), (('-j', '--jobs'), {'action': 'store', 'metavar': 'N'}), (('--platform',),), (('-P', '--pythonpath'),), (('-V', '--python-version'),)]:
        _add_file_argument(parser, types, *option)
    output = parser.add_mutually_exclusive_group()
    _add_file_argument(output, types, ('-o', '--output'))
    output.add_argument('-n', '--no-cache', dest='no_cache', action='store_true', default=False, help='Send pytype output to a temporary directory.')
    wrapper = datatypes.ParserWrapper(parser)
    pytype_config.add_basic_options(wrapper)
    pytype_config.add_feature_flags(wrapper)
    return Parser(parser, pytype_single_args=wrapper.actions)

Make parser for command line args.

Returns:
A Parser object.


def decode_jwt_payload(self, access_token=None):

        c = self.get_credentials()
        jwt = access_token or c.access_token
        try:
            _, payload, _ = jwt.split('.')
            rem = len(payload) % 4
            if rem > 0:
                payload += '=' * (4 - rem)
            try:
                decoded_jwt = b64decode(payload).decode("utf-8")
            except TypeError as e:
                raise PanCloudError(
                    "Failed to base64 decode JWT: %s" % e)
            else:
                try:
                    x = loads(decoded_jwt)
                except ValueError as e:
                    raise PanCloudError("Invalid JSON: %s" % e)
        except (AttributeError, ValueError) as e:
            raise PanCloudError("Invalid JWT: %s" % e)

        return x

Extract payload field from JWT.

Args:
access_token (str): Access token to decode. Defaults to ``None``.

Returns:
dict: JSON object that contains the claims conveyed by the JWT.


def iplot_state_qsphere(rho, figsize=None):
    html_template = Template('\n
\n
\n

\n
\n
\n    ')
    javascript_template = Template('\n

Create a Q sphere representation.

Graphical representation of the input array, using a Q sphere for each
eigenvalue.

Args:
rho (array): State vector or density matrix.
figsize (tuple): Figure size in pixels.


def get_content(self, url):
    cache_path = self._url_to_path(url)
    try:
        with open(cache_path, 'rb') as f:
            return f.read()
    except IOError:
        return None

Returns the content of a cached resource.

Args:
url: The url of the resource

Returns:
The content of the cached resource or None if not in the cache


def wv45(msg):
    d = hex2bin(data(msg))
    if (d[12] == '0'):
        return None
    ws = bin2int(d[13:15])
    return ws

Wake vortex.

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

Returns:
int: Wake vortex level. 0=NIL, 1=Light, 2=Moderate, 3=Severe


async def get_compile_output(self, pipeline_uuid: str) -> str:
    self._verify_pipeline_uuid(pipeline_uuid)
    request = api_pb2.GetCompileOutputRequest(pipeline_uuid=pipeline_uuid)
    response = await self._stub.GetCompileOutput(request, **self._kwargs)
    return response.output

Get the result of pipeline compilation.

Args:
pipeline_uuid: uuid of the pipeline

Returns:
output: contain the result of pipeline compilation


def make_message(self, text, channel):

        try:
            channel_id = self.slack.channel_from_name(channel)['id']
        except ValueError:
            channel_id = channel
        return pack({
            'text': text,
            'type': 'message',
            'channel': channel_id,
            'id': self.message_id,
        })

High-level function for creating messages. Return packed bytes.

Args:
text: {str}
channel: {str} Either name or ID


def decompress_file(filepath):
    toks = filepath.split('.')
    file_ext = toks[(- 1)].upper()
    from monty.io import zopen
    if (file_ext in ['BZ2', 'GZ', 'Z']):
        with open('.'.join(toks[0:(- 1)]), 'wb') as f_out, zopen(filepath, 'rb') as f_in:
            f_out.writelines(f_in)
        os.remove(filepath)

Decompresses a file with the correct extension. Automatically detects
gz, bz2 or z extension.

Args:
filepath (str): Path to file.
compression (str): A compression mode. Valid options are "gz" or
"bz2". Defaults to "gz".


def set(self, entity, keys, item):
    if not entity in self._store:
        self._store[entity] = {}
    for key in keys:
        self._store[entity][str(key)] = item

Sets an item in the cache.

Args:
entity: The name of the entity cache to use.
keys: The keys to set this item to. Typically this will contain the ext id
and the actual CM id.
item: The item to cache.


def set_json(self, reason='', new_page=False):
    compressed_json = json.dumps(self._compress_json(self.cached_json))
    if (len(compressed_json) > self.max_page_size):
        raise OverflowError('Usernotes page is too large (>{0} characters)'.format(self.max_page_size))
    if new_page:
        self.subreddit.wiki.create(self.page_name, compressed_json, reason)
        self.subreddit.wiki[self.page_name].mod.update(False, permlevel=2)
    else:
        self.subreddit.wiki[self.page_name].edit(compressed_json, reason)

Send the JSON from the cache to the usernotes wiki page.

Arguments:
reason: the change reason that will be posted to the wiki changelog
(str)
Raises:
OverflowError if the new JSON data is greater than max_page_size


def read_int64(self, little_endian=True):

        if little_endian:
            endian = "<"
        else:
            endian = ">"
        return self.unpack('%sq' % endian, 8)

Read 8 bytes as a signed integer value from the stream.

Args:
little_endian (bool): specify the endianness. (Default) Little endian.

Returns:
int:


def update_model_in_repo_based_on_filename(self, model):

        if model._tx_filename is None:
            for fn in self.all_models.filename_to_model:
                if self.all_models.filename_to_model[fn] == model:
                    return fn
            i = 0
            while self.all_models.has_model("anonymous{}".format(i)):
                i += 1
            myfilename = "anonymous{}".format(i)
            self.all_models.filename_to_model[myfilename] = model
        else:
            myfilename = model._tx_filename
            if (not self.all_models.has_model(myfilename)):
                self.all_models.filename_to_model[myfilename] = model
        return myfilename

Adds a model to the repo (not initially visible)
Args:
model: the model to be added. If the model
has no filename, a name is invented
Returns: the filename of the model added to the repo


def reset(self, indices=None):

    return tf.cond(
        tf.cast(tf.reduce_sum(indices + 1), tf.bool),
        lambda: self._reset_non_empty(indices),
        lambda: tf.cast(0, self.observ_dtype))

Reset the batch of environments.

Args:
indices: The batch indices of the environments to reset.

Returns:
Batch tensor of the new observations.


def speak(self, message):
    campfire = self.get_campfire()
    if (not isinstance(message, Message)):
        message = Message(campfire, message)
    result = self._connection.post(('room/%s/speak' % self.id), {'message': message.get_data()}, parse_data=True, key='message')
    if result['success']:
        return Message(campfire, result['data'])
    return result['success']

Post a message.

Args:
message (:class:`Message` or string): Message

Returns:
bool. Success


def ParseFloat(text):
    try:
        return float(text)
    except ValueError:
        if _FLOAT_INFINITY.match(text):
            if (text[0] == '-'):
                return float('-inf')
            else:
                return float('inf')
        elif _FLOAT_NAN.match(text):
            return float('nan')
        else:
            try:
                return float(text.rstrip('f'))
            except ValueError:
                raise ValueError(("Couldn't parse float: %s" % text))

Parse a floating point number.

Args:
text: Text to parse.

Returns:
The number parsed.

Raises:
ValueError: If a floating point number couldn't be parsed.


def _prepare_headers(self, request, filter=None, order_by=None, group_by=[], page=None, page_size=None):
    if filter:
        request.set_header('X-Nuage-Filter', filter)
    if order_by:
        request.set_header('X-Nuage-OrderBy', order_by)
    if (page is not None):
        request.set_header('X-Nuage-Page', str(page))
    if page_size:
        request.set_header('X-Nuage-PageSize', str(page_size))
    if (len(group_by) > 0):
        header = ', '.join(group_by)
        request.set_header('X-Nuage-GroupBy', 'true')
        request.set_header('X-Nuage-Attributes', header)

Prepare headers for the given request

Args:
request: the NURESTRequest to send
filter: string
order_by: string
group_by: list of names
page: int
page_size: int


def set(pb_or_dict, key, value):
    if (not isinstance(pb_or_dict, (collections.MutableMapping, Message))):
        raise TypeError(('Tried to set a key %s on an invalid object; expected a dict or protobuf message.' % key))
    (key, subkey) = _resolve_subkeys(key)
    if (subkey is not None):
        if isinstance(pb_or_dict, collections.MutableMapping):
            pb_or_dict.setdefault(key, {})
        set(get(pb_or_dict, key), subkey, value)
        return
    if isinstance(pb_or_dict, collections.MutableMapping):
        pb_or_dict[key] = value
    elif isinstance(value, (collections.MutableSequence, tuple)):
        while getattr(pb_or_dict, key):
            getattr(pb_or_dict, key).pop()
        for item in value:
            if isinstance(item, collections.Mapping):
                getattr(pb_or_dict, key).add(**item)
            else:
                getattr(pb_or_dict, key).extend([item])
    elif isinstance(value, collections.Mapping):
        for (item_key, item_value) in value.items():
            set(getattr(pb_or_dict, key), item_key, item_value)
    elif isinstance(value, Message):
        for (item_key, item_value) in value.ListFields():
            set(getattr(pb_or_dict, key), item_key.name, item_value)
    else:
        setattr(pb_or_dict, key, value)

Set the given key on the object.

Args:
pb_or_dict (Union[~google.protobuf.message.Message, Mapping]): the
object.
key (str): The key on the object in question.
value (Any): The value to set.

Raises:
TypeError: If pb_or_dict is not a Message or Mapping.


def worker_task(work_item, config):

    global _workspace

    _ensure_workspace(config)

    result = worker(
        work_item.module_path,
        config.python_version,
        work_item.operator_name,
        work_item.occurrence,
        config.test_command,
        config.timeout)
    return work_item.job_id, result

The celery task which performs a single mutation and runs a test suite.

This runs `cosmic-ray worker` in a subprocess and returns the results,
passing `config` to it via stdin.

Args:
work_item: A dict describing a WorkItem.
config: The configuration to use for the test execution.

Returns: An updated WorkItem


def square(x):
    if any_symbolic_tensors((x,)):
        return Square().symbolic_call(x)
    return backend.numpy.square(x)

Return the element-wise square of the input.

Args:
x: Input tensor.

Returns:
Output tensor, the square of `x`.


def get_variant(variant_handle, context=None):

    if isinstance(variant_handle, dict):
        variant_handle = ResourceHandle.from_dict(variant_handle)

    variant_resource = package_repository_manager.get_resource_from_handle(variant_handle)
    variant = Variant(variant_resource, context=context)
    return variant

Create a variant given its handle (or serialized dict equivalent)

Args:
variant_handle (`ResourceHandle` or dict): Resource handle, or
equivalent serialized dict representation from
ResourceHandle.to_dict
context (`ResolvedContext`): The context this variant is associated
with, if any.

Returns:
`Variant`.


def copy(self, datasets=None):
    new_scn = self.__class__()
    new_scn.attrs = self.attrs.copy()
    new_scn.dep_tree = self.dep_tree.copy()
    for ds_id in (datasets or self.keys()):
        new_scn.datasets[ds_id] = self[ds_id]
    if (not datasets):
        new_scn.wishlist = self.wishlist.copy()
    else:
        new_scn.wishlist = set([DatasetID.from_dict(ds.attrs) for ds in new_scn])
    return new_scn

Create a copy of the Scene including dependency information.

Args:
datasets (list, tuple): `DatasetID` objects for the datasets
to include in the new Scene object.


def set_save_itrs_root(setting):

    setting = bool(setting)
    f.root.times.save_itrs = setting
    return setting

Adjust the root timer save_itrs setting, such as for use in
multiprocessing, when a root timer may become a parallel subdivision (see
subdivide()).

Args:
setting (bool): Save individual iterations data, passed through bool()

Returns:
bool: Implemented setting value.


def predict_proba(self, a, b, **kwargs):
    a = scale(a).reshape(((- 1), 1))
    b = scale(b).reshape(((- 1), 1))
    return (self.anm_score(b, a) - self.anm_score(a, b))

Prediction method for pairwise causal inference using the ANM model.

Args:
a (numpy.ndarray): Variable 1
b (numpy.ndarray): Variable 2

Returns:
float: Causation score (Value : 1 if a->b and -1 if b->a)


def bootstrap_results(self, init_state):

    with tf.compat.v1.name_scope(
        name=mcmc_util.make_name(self.name, 'mh', 'bootstrap_results'),
        values=[init_state]):
      pkr = self.inner_kernel.bootstrap_results(init_state)
      if not has_target_log_prob(pkr):
        raise ValueError(
            '"target_log_prob" must be a member of `inner_kernel` results.')
      x = pkr.target_log_prob
      return MetropolisHastingsKernelResults(
          accepted_results=pkr,
          is_accepted=tf.ones_like(x, dtype=tf.bool),
          log_accept_ratio=tf.zeros_like(x),
          proposed_state=init_state,
          proposed_results=pkr,
          extra=[],
      )

Returns an object with the same type as returned by `one_step`.

Args:
init_state: `Tensor` or Python `list` of `Tensor`s representing the
initial state(s) of the Markov chain(s).

Returns:
kernel_results: A (possibly nested) `tuple`, `namedtuple` or `list` of
`Tensor`s representing internal calculations made within this function.

Raises:
ValueError: if `inner_kernel` results doesn't contain the member
"target_log_prob".


def _package_transform(package, fqdn, start=1, *args, **kwargs):
    node = _obj_getattr(package, fqdn, start)
    if ((node is not None) and hasattr(node, '__call__')):
        return node(*args, **kwargs)
    else:
        return args

Applies the specified package transform with `fqdn` to the package.

Args:
package: imported package object.
fqdn (str): fully-qualified domain name of function in the package. If it
does not include the package name, then set `start=0`.
start (int): in the '.'-split list of identifiers in `fqdn`, where to start
looking in the package. E.g., `numpy.linalg.norm` has `start=1` since
`package=numpy`; however, `linalg.norm` would have `start=0`.


def format(self, record):
    if (record.levelno >= logging.ERROR):
        color = colorama.Fore.RED
    elif (record.levelno >= logging.WARNING):
        color = colorama.Fore.YELLOW
    elif (record.levelno >= logging.INFO):
        color = colorama.Fore.RESET
    else:
        color = colorama.Fore.CYAN
    format_template = '{}{}%(levelname)s{} [%(asctime)s][%(name)s]{} %(message)s'
    if sys.stdout.isatty():
        self._fmt = format_template.format(colorama.Style.BRIGHT, color, colorama.Fore.RESET, colorama.Style.RESET_ALL)
    else:
        self._fmt = format_template.format(*([''] * 4))
    if six.PY3:
        self._style._fmt = self._fmt
    return super(_LogColorFormatter, self).format(record)

Format the log record with timestamps and level based colors.

Args:
record: The log record to format.

Returns:
The formatted log record.


def list_group_members(self, name):

        self.project_service.set_auth(self._token_project)
        return self.project_service.list_group_members(name)

Get the members of a group.

Args:
name (string): Name of group to query.

Returns:
(list[string]): List of member names.

Raises:
requests.HTTPError on failure.


def _CanSkipContentExtraction(self, file_entry):
    location = getattr(file_entry.path_spec, 'location', None)
    if (not location):
        return False
    data_stream_name = getattr(file_entry.path_spec, 'data_stream', None)
    if data_stream_name:
        return False
    file_system = file_entry.GetFileSystem()
    path_segments = file_system.SplitPath(location)
    if (not path_segments):
        return False
    if self._CHROME_CACHE_DATA_FILE_RE.match(path_segments[(- 1)]):
        location_segments = path_segments[:(- 1)]
        location_segments.append('index')
        location = file_system.JoinPath(location_segments)
        index_path_spec = path_spec_factory.Factory.NewPathSpec(file_entry.type_indicator, location=location, parent=file_entry.path_spec.parent)
        if file_system.FileEntryExistsByPathSpec(index_path_spec):
            return True
    elif self._FIREFOX_CACHE_DATA_FILE_RE.match(path_segments[(- 1)]):
        location_segments = path_segments[:(- 4)]
        location_segments.append('_CACHE_MAP_')
        location = file_system.JoinPath(location_segments)
        cache_map_path_spec = path_spec_factory.Factory.NewPathSpec(file_entry.type_indicator, location=location, parent=file_entry.path_spec.parent)
        if file_system.FileEntryExistsByPathSpec(cache_map_path_spec):
            return True
    elif self._FIREFOX_CACHE2_DATA_FILE_RE.match(path_segments[(- 1)]):
        location_segments = path_segments[:(- 2)]
        location_segments.append('index')
        location = file_system.JoinPath(location_segments)
        index_path_spec = path_spec_factory.Factory.NewPathSpec(file_entry.type_indicator, location=location, parent=file_entry.path_spec.parent)
        if file_system.FileEntryExistsByPathSpec(index_path_spec):
            return True
    elif ((len(path_segments) == 1) and (path_segments[0].lower() in ('hiberfil.sys', 'pagefile.sys', 'swapfile.sys'))):
        return True
    return False

Determines if content extraction of a file entry can be skipped.

Args:
file_entry (dfvfs.FileEntry): file entry of which to determine content
extraction can be skipped.

Returns:
bool: True if content extraction can be skipped.


async def _async_loop(self, urls):

        results = []
        async with aiohttp.ClientSession(
            connector=aiohttp.TCPConnector(ssl=False)
        ) as session:
            for url in urls:
                result = asyncio.ensure_future(self._get_async(url, session))
                results.append(result)
            responses = await asyncio.gather(*results)
        return responses

Asynchronous internal method used to request multiple URLs

Args:
urls (list): URLs to fetch

Returns:
responses (obj): All URL requests' response coroutines


def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
    original_shape = hidden_states.shape
    hidden_states = hidden_states.view(-1, hidden_states.size(-1))
    logits = self.router(hidden_states)
    top_logits, top_indices = torch.topk(logits, k=self.config.moe_topk, dim=1)
    scores = nn.functional.softmax(top_logits, dim=-1)
    original_dtype = top_indices.dtype
    tokens_per_expert = torch.histc(top_indices.flatten().to(torch.float32), bins=self.config.moe_num_experts, min=0, max=self.config.moe_num_experts - 1).to(original_dtype)
    indices = top_indices
    flatten_indices = indices.view(-1)
    sorted_indices = torch.argsort(flatten_indices)
    permuted_tokens = hidden_states.index_select(0, sorted_indices
    expert_output = self.experts(permuted_tokens, tokens_per_expert)
    unpermuted_tokens = torch.zeros((scores.shape[0] * self.config.moe_topk, expert_output.size(1)), dtype=expert_output.dtype, device=expert_output.device)
    unpermuted_tokens.index_copy_(0, sorted_indices, expert_output)
    unpermuted_tokens = unpermuted_tokens.view(-1, self.config.moe_topk, expert_output.size(1))
    output = (unpermuted_tokens * scores.unsqueeze(-1)).sum(dim=1).view(original_shape)
    shared_expert_output = self.shared_experts(hidden_states.view(original_shape))
    return output + shared_expert_output

Forward pass of the MoE Layer.

Args:
hidden_states (`torch.Tensor`):
Input tensor of shape (batch_size, sequence_length, hidden_size).

Returns:
torch.Tensor: Output tensor after passing through the MoE layer.

Process:
1. Route tokens to experts using the router.
2. Permute tokens based on routing decisions.
3. Process tokens through experts.
4. Unpermute and combine expert outputs.
5. Add shared expert output to the final result.


def call(self, hidden_states: tf.Tensor, attention_mask: tf.Tensor, causal_attention_mask: tf.Tensor, output_attentions: bool, training: bool=False) -> Tuple[tf.Tensor]:
    residual = hidden_states
    hidden_states = self.layer_norm1(inputs=hidden_states)
    attention_outputs = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, causal_attention_mask=causal_attention_mask, output_attentions=output_attentions, training=training)
    hidden_states = attention_outputs[0]
    hidden_states = residual + hidden_states
    residual = hidden_states
    hidden_states = self.layer_norm2(inputs=hidden_states)
    hidden_states = self.mlp(hidden_states=hidden_states)
    hidden_states = residual + hidden_states
    outputs = (hidden_states,) + attention_outputs[1:]
    return outputs

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


def normalize_words(self, ord=2, inplace=False):

    if ord == 2:
      ord = None
    vectors = self.vectors.T / np.linalg.norm(self.vectors, ord, axis=1)
    if inplace:
      self.vectors = vectors.T
      return self
    return Embedding(vectors=vectors.T, vocabulary=self.vocabulary)

Normalize embeddings matrix row-wise.

Args:
ord: normalization order. Possible values {1, 2, 'inf', '-inf'}


def clip_by_average_norm(t, clip_norm, name=None):
    with ops.name_scope(name, 'clip_by_average_norm', [t, clip_norm]) as name:
        t = ops.convert_to_tensor(t, name='t')
        n_element = math_ops.cast(array_ops.size(t), dtypes.float32)
        l2norm_inv = math_ops.rsqrt(math_ops.reduce_sum(t * t, math_ops.range(array_ops.rank(t))))
        tclip = array_ops.identity(t * clip_norm * math_ops.minimum(l2norm_inv * n_element, constant_op.constant(1.0) / clip_norm), name=name)
    return tclip

Clips tensor values to a maximum average L2-norm.

Given a tensor `t`, and a maximum clip value `clip_norm`, this operation
normalizes `t` so that its average L2-norm is less than or equal to
`clip_norm`. Specifically, if the average L2-norm is already less than or
equal to `clip_norm`, then `t` is not modified. If the average L2-norm is
greater than `clip_norm`, then this operation returns a tensor of the same
type and shape as `t` with its values set to:

`t * clip_norm / l2norm_avg(t)`

In this case, the average L2-norm of the output tensor is `clip_norm`.

This operation is typically used to clip gradients before applying them with
an optimizer.

Args:
t: A `Tensor`.
clip_norm: A 0-D (scalar) `Tensor` > 0. A maximum clipping value.
name: A name for the operation (optional).

Returns:
A clipped `Tensor`.


def AddFileReadData(self, path, file_data_path):

    if self.file_system.FileEntryExistsByPath(path):
      raise ValueError('Path: {0:s} already set.'.format(path))

    with open(file_data_path, 'rb') as file_object:
      file_data = file_object.read()

    self._AddParentDirectories(path)
    self.file_system.AddFileEntry(path, file_data=file_data)

Adds a "regular" file to the fake file system.

Args:
path (str): path of the file within the fake file system.
file_data_path (str): path of the file to read the file data from.

Raises:
ValueError: if the path is already set.


def split(x, indices_or_sections, axis=0):
    if any_symbolic_tensors((x,)):
        return Split(indices_or_sections, axis=axis).symbolic_call(x)
    return backend.numpy.split(x, indices_or_sections, axis=axis)

Split a tensor into chunks.

Args:
x: Input tensor.
indices_or_sections: If an integer, N, the tensor will be split into N
equal sections along `axis`. If a 1-D array of sorted integers,
the entries indicate indices at which the tensor will be split
along `axis`.
axis: Axis along which to split. Defaults to `0`.

Note:
A split does not have to result in equal division when using
Torch backend.

Returns:
A list of tensors.


def _is_in_outmost_while_loop(self, op):
    ctxt = self._get_op_control_flow_context(op)
    outer_while_context = control_flow_util.GetContainingWhileContext(ctxt)
    return outer_while_context == control_flow_util.GetContainingWhileContext(self._outmost_context)

Returns true if the op is at the same level with the training loop.

Returns false if the op is in an inner while loop or if it is outside of the
training loop.
Args:
op: tf.Operation

Returns:
A boolean.


def _expect(self, expected, times=50):

        print '[%s] Expecting [%s]' % (self.port, expected)

        retry_times = 10
        while times > 0 and retry_times > 0:
            line = self._readline()
            print '[%s] Got line [%s]' % (self.port, line)

            if line == expected:
                print '[%s] Expected [%s]' % (self.port, expected)
                return

            if not line:
                retry_times -= 1
                time.sleep(0.1)

            times -= 1

        raise Exception('failed to find expected string[%s]' % expected)

Find the `expected` line within `times` trials.

Args:
expected    str: the expected string
times       int: number of trials


def _scale_boxes(boxes, target_sizes):
    if isinstance(target_sizes, (list, tuple)):
        image_height = torch.tensor([i[0] for i in target_sizes])
        image_width = torch.tensor([i[1] for i in target_sizes])
    elif isinstance(target_sizes, torch.Tensor):
        image_height, image_width = target_sizes.unbind(1)
    else:
        raise ValueError('`target_sizes` must be a list, tuple or torch.Tensor')
    scale_factor = torch.stack([image_width, image_height, image_width, image_height], dim=1)
    scale_factor = scale_factor.unsqueeze(1).to(boxes.device)
    boxes = boxes * scale_factor
    return boxes

Scale batch of bounding boxes to the target sizes.

Args:
boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`):
Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format.
target_sizes (`List[Tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`):
Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format.

Returns:
`torch.Tensor` of shape `(batch_size, num_boxes, 4)`: Scaled bounding boxes.


def build_logits(data_ops, embed_layer, rnn_core, output_linear, name_prefix):
    embedded_input_seq = snt.BatchApply(embed_layer, name='input_embed_seq')(data_ops.sparse_obs)
    initial_rnn_state = nest.map_structure((lambda t: tf.get_local_variable('{}/rnn_state/{}'.format(name_prefix, t.op.name), initializer=t)), rnn_core.initial_state(FLAGS.batch_size))
    assign_zero_rnn_state = nest.map_structure((lambda x: x.assign(tf.zeros_like(x))), initial_rnn_state)
    assign_zero_rnn_state = tf.group(*nest.flatten(assign_zero_rnn_state))
    (rnn_output_seq, rnn_final_state) = tf.nn.dynamic_rnn(cell=rnn_core, inputs=embedded_input_seq, initial_state=initial_rnn_state, time_major=True)
    update_rnn_state = nest.map_structure(tf.assign, initial_rnn_state, rnn_final_state)
    with tf.control_dependencies(nest.flatten(update_rnn_state)):
        rnn_output_seq = tf.identity(rnn_output_seq, name='rnn_output_seq')
    output_logits = snt.BatchApply(output_linear, name='output_embed_seq')(rnn_output_seq)
    return (output_logits, assign_zero_rnn_state)

This is the core model logic.

Unrolls a Bayesian RNN over the given sequence.

Args:
data_ops: A `sequence_data.SequenceDataOps` namedtuple.
embed_layer: A `snt.Embed` instance.
rnn_core: A `snt.RNNCore` instance.
output_linear: A `snt.Linear` instance.
name_prefix: A string to use to prefix local variable names.

Returns:
A 3D time-major tensor representing the model's logits for a sequence of
predictions. Shape `[time_steps, batch_size, vocab_size]`.


def _pad_for_batching(self, pixel_values: List['torch.Tensor']) -> List['torch.Tensor']:
    max_patch = max((len(x) for x in pixel_values))
    pixel_values = [torch.nn.functional.pad(image, pad=[0, 0, 0, 0, 0, 0, 0, max_patch - image.shape[0]]) for image in pixel_values]
    return pixel_values

Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches.

Args:
pixel_values (`List[torch.Tensor]`):
An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`)

Returns:
List[`torch.Tensor`]: The padded images.


def _Test(self, nccl_reduce, numpy_fn, device_sets=(['/device:GPU:1', '/device:GPU:2', '/device:GPU:0'], ['/device:GPU:1', '/device:GPU:0'])):
    for dtype in [np.float16, np.float32, np.int32, np.int64, np.float64]:
        with self.test_session():
            for devices in device_sets:
                shape = (3, 4)
                random = (np.random.random_sample(shape) - 0.5) * 1024
                tensors = []
                for _ in devices:
                    tensors.append(random.astype(dtype))
                np_ans = tensors[0]
                for t in tensors[1:]:
                    np_ans = numpy_fn(np_ans, t)
                reduce_tensors = nccl_reduce(tensors, devices)
                self.assertNotEmpty(reduce_tensors)
                for r in reduce_tensors:
                    self.assertEqual(shape, r.get_shape())
                result_tensors = [array_ops.identity(t) for t in reduce_tensors]
                if not test.is_gpu_available():
                    continue
                for t in self.evaluate(result_tensors):
                    self.assertAllClose(t, np_ans)

Tests that nccl_reduce does the same as reduction with numpy_fn.

Args:
nccl_reduce: A function taking a list of tensors and a list of devices,
and returns a list of reduced tensors and a list of ops to perform the
reduction.
numpy_fn: A function taking two tensors and returning the reduction of the
two.
device_sets: Tuple of virtual devices to run test on.


def EncoderLayer(feature_depth, feedforward_depth, num_heads, dropout, mode):
    encoder_block = layers.Serial(layers.Residual(layers.Parallel(layers.LayerNorm(), layers.Identity()), layers.MultiHeadedAttention(feature_depth, num_heads=num_heads, dropout=dropout, mode=mode), layers.Dropout(rate=dropout, mode=mode), shortcut=layers.FirstBranch()), ResidualFeedForward(feature_depth, feedforward_depth, dropout, mode=mode))
    return layers.Serial(layers.Reorder(output=((0, 1), 1)), layers.Parallel(encoder_block, layers.Identity()))

Transformer encoder layer.

The input to the encoder is a pair (embedded source, mask) where
the mask is created from the original source to prevent attending
to the padding part of the input.

Args:
feature_depth: int:  depth of embedding
feedforward_depth: int: depth of feed-forward layer
num_heads: int: number of attention heads
dropout: float: dropout rate (how much to drop out)
mode: str: 'train' or 'eval'

Returns:
the layer, returning a pair (actiavtions, mask).


def CheckHost(host_data, os_name=None, cpe=None, labels=None, exclude_checks=None, restrict_checks=None):
    kb = host_data.get('KnowledgeBase')
    if (os_name is None):
        os_name = kb.os
    if (cpe is None):
        pass
    if (labels is None):
        pass
    return CheckRegistry.Process(host_data, os_name=os_name, cpe=cpe, labels=labels, restrict_checks=restrict_checks, exclude_checks=exclude_checks)

Perform all checks on a host using acquired artifacts.

Checks are selected based on the artifacts available and the host attributes
(e.g. os_name/cpe/labels) provided as either parameters, or in the
knowledgebase artifact.

A KnowledgeBase artifact should be provided that contains, at a minimum:
- OS
- Hostname or IP
Other knowldegebase attributes may be required for specific checks.

CPE is currently unused, pending addition of a CPE module in the GRR client.

Labels are arbitrary string labels attached to a client.

Args:
host_data: A dictionary with artifact names as keys, and rdf data as values.
os_name: An OS name (optional).
cpe: A CPE string (optional).
labels: An iterable of labels (optional).
exclude_checks: A list of check ids not to run. A check id in this list
will not get run even if included in restrict_checks.
restrict_checks: A list of check ids that may be run, if appropriate.

Returns:
A CheckResults object that contains results for all checks that were
performed on the host.


def __init__(self, runner: Optional[Union[str, PipelineRunner]]=None, options: Optional[PipelineOptions]=None, argv: Optional[List[str]]=None, display_data: Optional[Dict[str, Any]]=None):
    logging.basicConfig()
    if options is not None:
        if isinstance(options, PipelineOptions):
            saved_runner = options.view_as(StandardOptions).runner
            options.view_as(StandardOptions).runner = None
            self._options = copy.deepcopy(options)
            self._options.view_as(StandardOptions).runner = saved_runner
            options.view_as(StandardOptions).runner = saved_runner
        else:
            raise ValueError('Parameter options, if specified, must be of type PipelineOptions. Received : %r' % options)
    elif argv is not None:
        if isinstance(argv, list):
            self._options = PipelineOptions(argv)
        else:
            raise ValueError('Parameter argv, if specified, must be a list. Received : %r' % argv)
    else:
        self._options = PipelineOptions([])
    FileSystems.set_options(self._options)
    if runner is None:
        runner = self._options.view_as(StandardOptions).runner
        if runner is None:
            runner = StandardOptions.DEFAULT_RUNNER
            logging.info('Missing pipeline option (runner). Executing pipeline using the default runner: %s.', runner)
    if isinstance(runner, str):
        runner = create_runner(runner)
    elif not isinstance(runner, PipelineRunner):
        raise TypeError('Runner %s is not a PipelineRunner object or the name of a registered runner.' % runner)
    if self._options.view_as(SetupOptions).pickle_library == 'default' and runner.default_pickle_library_override():
        logging.info('Runner defaulting to pickling library: %s.', runner.default_pickle_library_override())
        self._options.view_as(SetupOptions).pickle_library = runner.default_pickle_library_override()
    pickler.set_library(self._options.view_as(SetupOptions).pickle_library)
    errors = PipelineOptionsValidator(self._options, runner).validate()
    if errors:
        raise ValueError('Pipeline has validations errors: \n' + '\n'.join(errors))
    if runner.is_fnapi_compatible():
        experiments = self._options.view_as(DebugOptions).experiments or []
        if not 'beam_fn_api' in experiments:
            experiments.append('beam_fn_api')
            self._options.view_as(DebugOptions).experiments = experiments
    self.local_tempdir = tempfile.mkdtemp(prefix='beam-pipeline-temp')
    self.runner = runner
    self.transforms_stack = [AppliedPTransform(None, None, '', None, None, None)]
    self.applied_labels = set()
    self._root_transform().resource_hints = resource_hints_from_options(options)
    self.component_id_map = ComponentIdMap()
    self.contains_external_transforms = False
    self._display_data = display_data or {}
    self._error_handlers = []
    self._annotations_stack = [{}]

Initialize a pipeline object.

Args:
runner (~apache_beam.runners.runner.PipelineRunner): An object of
type :class:`~apache_beam.runners.runner.PipelineRunner` that will be
used to execute the pipeline. For registered runners, the runner name
can be specified, otherwise a runner object must be supplied.
options (~apache_beam.options.pipeline_options.PipelineOptions):
A configured
:class:`~apache_beam.options.pipeline_options.PipelineOptions` object
containing arguments that should be used for running the Beam job.
argv (List[str]): a list of arguments (such as :data:`sys.argv`)
to be used for building a
:class:`~apache_beam.options.pipeline_options.PipelineOptions` object.
This will only be used if argument **options** is :data:`None`.
display_data (Dict[str, Any]): a dictionary of static data associated
with this pipeline that can be displayed when it runs.

Raises:
ValueError: if either the runner or options argument is not
of the expected type.


def run_analysis(args):
    import google.datalab.bigquery as bq
    if args.bigquery_table:
        table = bq.Table(args.bigquery_table)
        schema_list = table.schema._bq_schema
    else:
        schema_list = json.loads(file_io.read_file_to_string(args.schema_file).decode())
        table = bq.ExternalDataSource(source=args.input_file_pattern, schema=bq.Schema(schema_list))
    for col_schema in schema_list:
        col_type = col_schema['type'].lower()
        if ((col_type != 'string') and (col_type != 'integer') and (col_type != 'float')):
            raise ValueError(('Schema contains an unsupported type %s.' % col_type))
    run_numerical_analysis(table, schema_list, args)
    run_categorical_analysis(table, schema_list, args)
    file_io.write_string_to_file(os.path.join(args.output_dir, SCHEMA_FILE), json.dumps(schema_list, indent=2, separators=(',', ': ')))

Builds an analysis file for training.

Uses BiqQuery tables to do the analysis.

Args:
args: command line args

Raises:
ValueError if schema contains unknown types.


def _kl_gumbel_gumbel(a, b, name=None):

  with tf.name_scope(name or "kl_gumbel_gumbel"):

    return (tf.math.log(b.scale) - tf.math.log(a.scale) + np.euler_gamma *
            (a.scale / b.scale - 1.) +
            tf.math.expm1((b.loc - a.loc) / b.scale +
                          tf.math.lgamma(a.scale / b.scale + 1.)) +
            (a.loc - b.loc) / b.scale)

Calculate the batched KL divergence KL(a || b) with a and b Gumbel.

Args:
a: instance of a Gumbel distribution object.
b: instance of a Gumbel distribution object.
name: (optional) Name to use for created operations.
default is "kl_gumbel_gumbel".

Returns:
Batchwise KL(a || b)


def abort_class_if(expr, reason, extras=None):
    if expr:
        abort_class(reason, extras)

Abort all subsequent tests within the same test class in one iteration,
if expression evaluates to True.

If one test class is requested multiple times in a test run, this can
only abort one of the requested executions, NOT all.

Args:
expr: The expression that is evaluated.
reason: The reason to abort.
extras: An optional field for extra information to be included in
test result.

Raises:
signals.TestAbortClass: Abort all subsequent tests in a test class.


def bake(self, start_response):

        if isinstance(self._content, six.text_type):
            self._content = self._content.encode('utf8')

        if self._content_length is None:
            self._content_length = len(self._content)

        self._headers[HttpResponseHeaders.CONTENT_LENGTH] = \
            str(self._content_length)
        headers = list(self._headers.items())
        cookies = [(HttpResponseHeaders.SET_COOKIE, v.OutputString())
                   for _, v in self._cookies.items()]

        if len(cookies):
            headers = list(headers) + cookies
        start_response(self._status_code, headers)

        if isinstance(self._content, six.binary_type):
            return [self._content]

        return self._content

Bakes the response and returns the content.

Args:
start_response (:obj:`callable`): Callback method that accepts
status code and a list of tuples (pairs) containing headers'
key and value respectively.


def Search(self, artifact=None, os_name=None, cpe=None, label=None):

    return [
        c for c in self.conditions if c.Search(artifact, os_name, cpe, label)
    ]

Find the host attributes that trigger data collection.

Args:
artifact: An artifact name.
os_name: An OS string.
cpe: A CPE string.
label: A label string.

Returns:
A list of conditions that contain the specified attributes.


def sort_elements_by_child_values(obj_pyxb, child_name_list):

    obj_pyxb.sort(key=lambda x: [get_auto(getattr(x, n)) for n in child_name_list])

In-place sort simple or complex elements in a PyXB object by values they contain
in child elements.

Args:
obj_pyxb: PyXB object

child_name_list: list of str
List of element names that are direct children of the PyXB object.


def make_transform_from_reference(n_xyz: torch.Tensor, ca_xyz: torch.Tensor, c_xyz: torch.Tensor, eps: float=1e-20) -> Rigid:
    translation = -1 * ca_xyz
    n_xyz = n_xyz + translation
    c_xyz = c_xyz + translation
    c_x, c_y, c_z = [c_xyz[..., i] for i in range(3)]
    norm = torch.sqrt(eps + c_x ** 2 + c_y ** 2)
    sin_c1 = -c_y / norm
    cos_c1 = c_x / norm
    c1_rots = sin_c1.new_zeros((*sin_c1.shape, 3, 3))
    c1_rots[..., 0, 0] = cos_c1
    c1_rots[..., 0, 1] = -1 * sin_c1
    c1_rots[..., 1, 0] = sin_c1
    c1_rots[..., 1, 1] = cos_c1
    c1_rots[..., 2, 2] = 1
    norm = torch.sqrt(eps + c_x ** 2 + c_y ** 2 + c_z ** 2)
    sin_c2 = c_z / norm
    cos_c2 = torch.sqrt(c_x ** 2 + c_y ** 2) / norm
    c2_rots = sin_c2.new_zeros((*sin_c2.shape, 3, 3))
    c2_rots[..., 0, 0] = cos_c2
    c2_rots[..., 0, 2] = sin_c2
    c2_rots[..., 1, 1] = 1
    c2_rots[..., 2, 0] = -1 * sin_c2
    c2_rots[..., 2, 2] = cos_c2
    c_rots = rot_matmul(c2_rots, c1_rots)
    n_xyz = rot_vec_mul(c_rots, n_xyz)
    _, n_y, n_z = [n_xyz[..., i] for i in range(3)]
    norm = torch.sqrt(eps + n_y ** 2 + n_z ** 2)
    sin_n = -n_z / norm
    cos_n = n_y / norm
    n_rots = sin_c2.new_zeros((*sin_c2.shape, 3, 3))
    n_rots[..., 0, 0] = 1
    n_rots[..., 1, 1] = cos_n
    n_rots[..., 1, 2] = -1 * sin_n
    n_rots[..., 2, 1] = sin_n
    n_rots[..., 2, 2] = cos_n
    rots = rot_matmul(n_rots, c_rots)
    rots = rots.transpose(-1, -2)
    translation = -1 * translation
    rot_obj = Rotation(rot_mats=rots, quats=None)
    return Rigid(rot_obj, translation)

Returns a transformation object from reference coordinates.

Note that this method does not take care of symmetries. If you provide the atom positions in the non-standard
way, the N atom will end up not at [-0.527250, 1.359329, 0.0] but instead at [-0.527250, -1.359329, 0.0]. You
need to take care of such cases in your code.

Args:
n_xyz: A [*, 3] tensor of nitrogen xyz coordinates.
ca_xyz: A [*, 3] tensor of carbon alpha xyz coordinates.
c_xyz: A [*, 3] tensor of carbon xyz coordinates.
Returns:
A transformation object. After applying the translation and rotation to the reference backbone, the
coordinates will approximately equal to the input coordinates.


def decode(byte_str, allow_none=False):

    if byte_str is None and allow_none:
        return ""

    if not isinstance(byte_str, bytes):
        raise ValueError(
            "The argument {} must be a bytes object.".format(byte_str))
    if sys.version_info >= (3, 0):
        return byte_str.decode("ascii")
    else:
        return byte_str

Make this unicode in Python 3, otherwise leave it as bytes.

Args:
byte_str: The byte string to decode.
allow_none: If true, then we will allow byte_str to be None in which
case we will return an empty string. TODO(rkn): Remove this flag.
This is only here to simplify upgrading to flatbuffers 1.10.0.

Returns:
A byte string in Python 2 and a unicode string in Python 3.


def ces(subsystem, mechanisms=False, purviews=False, cause_purviews=False, effect_purviews=False, parallel=False):
    if (mechanisms is False):
        mechanisms = utils.powerset(subsystem.node_indices, nonempty=True)
    engine = ComputeCauseEffectStructure(mechanisms, subsystem, purviews, cause_purviews, effect_purviews)
    return CauseEffectStructure(engine.run((parallel or config.PARALLEL_CONCEPT_EVALUATION)), subsystem=subsystem)

Return the conceptual structure of this subsystem, optionally restricted
to concepts with the mechanisms and purviews given in keyword arguments.

If you don't need the full |CauseEffectStructure|, restricting the possible
mechanisms and purviews can make this function much faster.

Args:
subsystem (Subsystem): The subsystem for which to determine the
|CauseEffectStructure|.

Keyword Args:
mechanisms (tuple[tuple[int]]): Restrict possible mechanisms to those
in this list.
purviews (tuple[tuple[int]]): Same as in |Subsystem.concept()|.
cause_purviews (tuple[tuple[int]]): Same as in |Subsystem.concept()|.
effect_purviews (tuple[tuple[int]]): Same as in |Subsystem.concept()|.
parallel (bool): Whether to compute concepts in parallel. If ``True``,
overrides :data:`config.PARALLEL_CONCEPT_EVALUATION`.

Returns:
CauseEffectStructure: A tuple of every |Concept| in the cause-effect
structure.


def flatlist_dropdup(list_of_lists):
    return list(set([str(item) for sublist in list_of_lists for item in sublist]))

Make a single list out of a list of lists, and drop all duplicates.

Args:
list_of_lists: List of lists.

Returns:
list: List of single objects.


def _parse_description(self, config):

        value = DESCRIPTION_RE.search(config).group('value')
        return dict(description=value)

_parse_description scans the provided configuration block and
extracts the vrf description value. The return dict is intended to
be merged into the response dict.

Args:
config (str): The vrf configuration block from the nodes
running configuration

Returns:
dict: resource dict attribute


def new_inertial(name=None, pos=(0, 0, 0), mass=None, **kwargs):

    if mass is not None:
        kwargs["mass"] = str(mass)
    kwargs["pos"] = array_to_string(pos)
    element = ET.Element("inertial", attrib=kwargs)
    return element

Creates a inertial element with attributes specified by @**kwargs.

Args:
mass: The mass of inertial


def __init__(self, resolver_context):

    super(LVMFile, self).__init__(resolver_context)
    self._file_system = None
    self._vslvm_logical_volume = None

Initializes a file-like object.

Args:
resolver_context (Context): resolver context.


def load_ast(nodes, indentation='  ', include_source_map=False, delete_on_exit=True):
    if not isinstance(nodes, (list, tuple)):
        nodes = (nodes,)
    source = parser.unparse(nodes, indentation=indentation)
    module, _ = load_source(source, delete_on_exit)
    if include_source_map:
        source_map = origin_info.create_source_map(nodes, source, module.__file__)
    else:
        source_map = None
    return (module, source, source_map)

Loads the given AST as a Python module.

Compiling the AST code this way ensures that the source code is readable by
e.g. `pdb` or `inspect`.

Args:
nodes: Union[ast.AST, Iterable[ast.AST]], the code to compile, as an AST
object.
indentation: Text, the string to use for indentation.
include_source_map: bool, whether return a source map.
delete_on_exit: bool, whether to delete the temporary file used for
compilation on exit.

Returns:
Tuple[module, Text, Dict[LineLocation, OriginInfo]], containing:
the module containing the unparsed nodes, the source code corresponding to
nodes, and the source map. Is include_source_map is False, the source map
will be None.


def blank_tiles(input_word):

    blanks = 0
    questions = 0
    input_letters = []
    for letter in input_word:
        if letter == "_":
            blanks += 1
        elif letter == "?":
            questions += 1
        else:
            input_letters.append(letter)
    return input_letters, blanks, questions

Searches a string for blank tile characters ("?" and "_").

Args:
input_word: the user supplied string to search through

Returns:
a tuple of:
input_word without blanks
integer number of blanks (no points)
integer number of questions (points)


def _CompareFields(field, other_field):
    field_attrs = _GetFieldAttributes(field)
    other_field_attrs = _GetFieldAttributes(other_field)
    if (field_attrs != other_field_attrs):
        return False
    return (field.__class__ == other_field.__class__)

Checks if two ProtoRPC fields are "equal".

Compares the arguments, rather than the id of the elements (which is
the default __eq__ behavior) as well as the class of the fields.

Args:
field: A ProtoRPC message field to be compared.
other_field: A ProtoRPC message field to be compared.

Returns:
Boolean indicating whether the fields are equal.


def __init__(self, sv, sess):
    super(SVTimerCheckpointThread, self).__init__(sv.coord, sv.save_model_secs)
    self._sv = sv
    self._sess = sess

Create a `SVTimerCheckpointThread`.

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


def drift(data, n=3, **kwargs):

    yi = data[-n]
    yf = data[-1]
    slope = (yf - yi) / (n - 1)
    forecast = yf + slope
    return forecast

The drift forecast for the next point is a linear extrapolation from the previous ``n``
points in the series.

Args:
data (np.array): Observed data, presumed to be ordered in time.
n (int): period over which to calculate linear model for extrapolation

Returns:
float: a single-valued forecast for the next value in the series.


def run_configure(self, mandatory=True):
    if (not has_file(self.working_dir, 'configure')):
        if mandatory:
            raise FileNotFoundError('Could not find a configure script for execution.')
        else:
            return
    try:
        prog = RunningProgram(self, 'configure')
        prog.expect_exit_status(0)
    except Exception:
        if mandatory:
            raise

Runs the 'configure' program in the working directory.

Args:
mandatory (bool): Throw exception if 'configure' fails or a
'configure' file is missing.


def find_workflow_path(self):
    for pth in settings.WORKFLOW_PACKAGES_PATHS:
        path = ('%s/%s.bpmn' % (pth, self.current.workflow_name))
        if os.path.exists(path):
            return path
    err_msg = ('BPMN file cannot found: %s' % self.current.workflow_name)
    log.error(err_msg)
    raise RuntimeError(err_msg)

Tries to find the path of the workflow diagram file
in `WORKFLOW_PACKAGES_PATHS`.

Returns:
Path of the workflow spec file (BPMN diagram)


def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
    return False

Determines if the input is numeric

Args:
s: The value to check.
Returns:
bool: ``True`` if the input is numeric, ``False`` otherwise.


def create_from_checkpoint(cls, path: str):

Create factory to create an Adapter from checkpoint.

Args:
path: Path to checkpoint.


def to_dict(self):
    output = asdict(self)
    output['trunk'] = self.trunk.to_dict()
    return output

Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`].

Returns:
`Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,


def ReadFromCsv(path: str, *, splittable: bool=True, **kwargs):
    from apache_beam.dataframe.io import ReadViaPandas
    return 'ReadFromCsv' >> ReadViaPandas('csv', path, splittable=splittable, **kwargs)

A PTransform for reading comma-separated values (csv) files into a
PCollection.

Args:
path (str): The file path to read from.  The path can contain glob
characters such as ``*`` and ``?``.
splittable (bool): Whether the csv files are splittable at line
boundaries, i.e. each line of this file represents a complete record.
This should be set to False if single records span multiple lines (e.g.
a quoted field has a newline inside of it).  Setting this to false may
disable liquid sharding.
**kwargs: Extra arguments passed to `pandas.read_csv` (see below).


def enable_output_all_intermediates(fn: _F) -> _F:

    def wrapper(*args, **kwargs):
        output_all_intermediates_old = control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE
        control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE = True
        try:
            return fn(*args, **kwargs)
        finally:
            control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE = output_all_intermediates_old
    return wrapper

Force-enable outputting all intermediates from functional control flow ops.

Args:
fn: the function to be wrapped

Returns:
The wrapped function


def help(self, print_output=True):
    help_text = self._rpc('help')
    if print_output:
        print(help_text)
    else:
        return help_text

Calls the help RPC, which returns the list of RPC calls available.

This RPC should normally be used in an interactive console environment
where the output should be printed instead of returned. Otherwise,
newlines will be escaped, which will make the output difficult to read.

Args:
print_output: A bool for whether the output should be printed.

Returns:
A str containing the help output otherwise None if print_output
wasn't set.


def _create_deployment_object(self, job_name, job_image, deployment_name, port=80, replicas=1, cmd_string=None, engine_json_file='~/.ipython/profile_default/security/ipcontroller-engine.json', engine_dir='.'):
    security_context = None
    if ('security' in self.config['execution']):
        security_context = client.V1SecurityContext(run_as_group=self.group_id, run_as_user=self.user_id, run_as_non_root=self.run_as_non_root)
    environment_vars = client.V1EnvVar(name='TEST', value='SOME DATA')
    launch_args = ['-c', '{0}; /app/deploy.sh;'.format(cmd_string)]
    print(launch_args)
    container = None
    if security_context:
        container = client.V1Container(name=job_name, image=job_image, ports=[client.V1ContainerPort(container_port=port)], command=['/bin/bash'], args=launch_args, env=[environment_vars], security_context=security_context)
    else:
        container = client.V1Container(name=job_name, image=job_image, ports=[client.V1ContainerPort(container_port=port)], command=['/bin/bash'], args=launch_args, env=[environment_vars])
    secret = None
    if self.secret:
        secret = client.V1LocalObjectReference(name=self.secret)
    template = client.V1PodTemplateSpec(metadata=client.V1ObjectMeta(labels={'app': job_name}), spec=client.V1PodSpec(containers=[container], image_pull_secrets=[secret]))
    spec = client.ExtensionsV1beta1DeploymentSpec(replicas=replicas, template=template)
    deployment = client.ExtensionsV1beta1Deployment(api_version='extensions/v1beta1', kind='Deployment', metadata=client.V1ObjectMeta(name=deployment_name), spec=spec)
    return deployment

Create a kubernetes deployment for the job.

Args:
- job_name (string) : Name of the job and deployment
- job_image (string) : Docker image to launch

KWargs:
- port (integer) : Container port
- replicas : Number of replica containers to maintain

Returns:
- True: The deployment object to launch


def validate(self,
                 controls: Iterable[Tuple[str, str]],
                 subcontrol: str = None) -> Any:

        try:
            return super().validate(controls, subcontrol)
        except ValidationFailure as e:
            if DEBUG_FORM_VALIDATION:
                log.warning("Validation failure: {!r}; {}",
                            e, self._get_form_errors())
            self._show_hidden_widgets_for_fields_with_errors(self)
            raise

Validates the form.

Args:
controls: an iterable of ``(key, value)`` tuples
subcontrol:

Returns:
a Colander ``appstruct``

Raises:
ValidationFailure: on failure


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

List merge requests that will close the issue when merged.

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

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetErrot: If the merge requests could not be retrieved

Returns:
list: The list of merge requests.


def from_3_points(p_neg_x_axis: torch.Tensor, origin: torch.Tensor, p_xy_plane: torch.Tensor, eps: float=1e-08) -> Rigid:
    p_neg_x_axis_unbound = torch.unbind(p_neg_x_axis, dim=-1)
    origin_unbound = torch.unbind(origin, dim=-1)
    p_xy_plane_unbound = torch.unbind(p_xy_plane, dim=-1)
    e0 = [c1 - c2 for c1, c2 in zip(origin_unbound, p_neg_x_axis_unbound)]
    e1 = [c1 - c2 for c1, c2 in zip(p_xy_plane_unbound, origin_unbound)]
    denom = torch.sqrt(sum((c * c for c in e0)) + eps * torch.ones_like(e0[0]))
    e0 = [c / denom for c in e0]
    dot = sum((c1 * c2 for c1, c2 in zip(e0, e1)))
    e1 = [c2 - c1 * dot for c1, c2 in zip(e0, e1)]
    denom = torch.sqrt(sum((c * c for c in e1)) + eps * torch.ones_like(e1[0]))
    e1 = [c / denom for c in e1]
    e2 = [e0[1] * e1[2] - e0[2] * e1[1], e0[2] * e1[0] - e0[0] * e1[2], e0[0] * e1[1] - e0[1] * e1[0]]
    rots = torch.stack([c for tup in zip(e0, e1, e2) for c in tup], dim=-1)
    rots = rots.reshape(rots.shape[:-1] + (3, 3))
    rot_obj = Rotation(rot_mats=rots, quats=None)
    return Rigid(rot_obj, torch.stack(origin_unbound, dim=-1))

Implements algorithm 21. Constructs transformations from sets of 3 points using the Gram-Schmidt algorithm.

Args:
p_neg_x_axis: [*, 3] coordinates
origin: [*, 3] coordinates used as frame origins
p_xy_plane: [*, 3] coordinates
eps: Small epsilon value
Returns:
A transformation object of shape [*]


def get_registered_kernels_for_op(name):
    buf = c_api.TF_GetRegisteredKernelsForOp(name)
    data = c_api.TF_GetBuffer(buf)
    kernel_list = kernel_def_pb2.KernelList()
    kernel_list.ParseFromString(compat.as_bytes(data))
    return kernel_list

Returns a KernelList proto of registered kernels for a given op.

Args:
name: A string representing the name of the op whose kernels to retrieve.