def train(self, resume_from_checkpoint: Optional[Union[str, bool]]=None, trial: Union['optuna.Trial', dict[str, Any], None]=None, ignore_keys_for_eval: Optional[list[str]]=None, **kwargs):
    if resume_from_checkpoint is False:
        resume_from_checkpoint = None
    self._memory_tracker.start()
    args = self.args
    self.is_in_train = True
    if self.neftune_noise_alpha is not None:
        self.model = self._activate_neftune(self.model)
    if (args.fp16_full_eval or args.bf16_full_eval) and (not args.do_train) and (not self.is_model_parallel) and (self.model_init is None):
        self._move_model_to_device(self.model, args.device)
    if 'model_path' in kwargs:
        resume_from_checkpoint = kwargs.pop('model_path')
        warnings.warn('`model_path` is deprecated and will be removed in a future version. Use `resume_from_checkpoint` instead.', FutureWarning)
    if len(kwargs) > 0:
        raise TypeError(f'train() got unexpected keyword arguments: {', '.join(list(kwargs.keys()))}.')
    self._hp_search_setup(trial)
    self._train_batch_size = self.args.train_batch_size
    model_reloaded = False
    if self.model_init is not None:
        enable_full_determinism(self.args.seed) if self.args.full_determinism else set_seed(self.args.seed)
        self.model = self.call_model_init(trial)
        model_reloaded = True
        self.optimizer, self.lr_scheduler = (None, None)
    if isinstance(resume_from_checkpoint, bool) and resume_from_checkpoint:
        resume_from_checkpoint = get_last_checkpoint(args.output_dir)
        if resume_from_checkpoint is None:
            raise ValueError(f'No valid checkpoint found in output directory ({args.output_dir})')
    if resume_from_checkpoint is not None:
        if not is_sagemaker_mp_enabled() and (not self.is_deepspeed_enabled) and (not self.is_fsdp_enabled):
            self._load_from_checkpoint(resume_from_checkpoint)
        state = TrainerState.load_from_json(os.path.join(resume_from_checkpoint, TRAINER_STATE_NAME))
        if state.train_batch_size is not None:
            self._train_batch_size = state.train_batch_size
    if model_reloaded:
        if self.place_model_on_device:
            self._move_model_to_device(self.model, args.device)
        self.model_wrapped = self.model
    inner_training_loop = find_executable_batch_size(self._inner_training_loop, self._train_batch_size, args.auto_find_batch_size)
    if args.push_to_hub:
        try:
            hf_hub_utils.disable_progress_bars()
            return inner_training_loop(args=args, resume_from_checkpoint=resume_from_checkpoint, trial=trial, ignore_keys_for_eval=ignore_keys_for_eval)
        finally:
            hf_hub_utils.enable_progress_bars()
    else:
        return inner_training_loop(args=args, resume_from_checkpoint=resume_from_checkpoint, trial=trial, ignore_keys_for_eval=ignore_keys_for_eval)

Main training entry point.

Args:
resume_from_checkpoint (`str` or `bool`, *optional*):
If a `str`, local path to a saved checkpoint as saved by a previous instance of [`Trainer`]. If a
`bool` and equals `True`, load the last checkpoint in *args.output_dir* as saved by a previous instance
of [`Trainer`]. If present, training will resume from the model/optimizer/scheduler states loaded here.
trial (`optuna.Trial` or `Dict[str, Any]`, *optional*):
The trial run or the hyperparameter dictionary for hyperparameter search.
ignore_keys_for_eval (`List[str]`, *optional*)
A list of keys in the output of your model (if it is a dictionary) that should be ignored when
gathering predictions for evaluation during the training.
kwargs (`Dict[str, Any]`, *optional*):
Additional keyword arguments used to hide deprecated arguments


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

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

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


def _ParseBooleanValue(self, byte_stream):

    if byte_stream == b'\x00':
      return False

    if byte_stream == b'\x01':
      return True

    raise errors.ParseError('Unsupported boolean value.')

Parses a boolean value.

Args:
byte_stream (bytes): byte stream.

Returns:
bool: boolean value.

Raises:
ParseError: when the boolean value cannot be parsed.


def get_entry(self, pathname_name):

        pathname_name = self._normalized_entryname(pathname_name)
        return self.contents[pathname_name]

Retrieves the specified child file or directory entry.

Args:
pathname_name: The basename of the child object to retrieve.

Returns:
The fake file or directory object.

Raises:
KeyError: if no child exists by the specified name.


def predict_next_action(self, state_key, next_action_list):
    if (self.q_df is not None):
        next_action_q_df = self.q_df[(self.q_df.state_key == state_key)]
        next_action_q_df = next_action_q_df[next_action_q_df.action_key.isin(next_action_list)]
        if (next_action_q_df.shape[0] == 0):
            return random.choice(next_action_list)
        else:
            if (next_action_q_df.shape[0] == 1):
                max_q_action = next_action_q_df['action_key'].values[0]
            else:
                next_action_q_df = next_action_q_df.sort_values(by=['q_value'], ascending=False)
                max_q_action = next_action_q_df.iloc[(0, :)]['action_key']
            return max_q_action
    else:
        return random.choice(next_action_list)

Predict next action by Q-Learning.

Args:
state_key:          The key of state in `self.t+1`.
next_action_list:   The possible action in `self.t+1`.

Returns:
The key of action.


def from_array(array):
    try:
        raw_data = blosc.pack_array(array)
    except Exception as e:
        raise ValueError('Could not compress data from array. {}'.format(e))
    return raw_data

Export a numpy array to a blosc array.

Arguments:
array: The numpy array to compress to blosc array

Returns:
Bytes/String. A blosc compressed array


def replace_with_spqr_linear(model, quantization_config=None, modules_to_not_convert=None, current_key_name=None, has_been_replaced=False):
    if modules_to_not_convert is None:
        modules_to_not_convert = []
    if is_accelerate_available():
        from accelerate import init_empty_weights
    if is_spqr_available():
        from spqr_quant import QuantizedLinear
    for name, module in model.named_children():
        if current_key_name is None:
            current_key_name = []
        current_key_name.append(name)
        if isinstance(module, nn.Linear):
            if '.'.join(current_key_name) + '.weight' not in modules_to_not_convert:
                with init_empty_weights():
                    tensor_name = '.'.join(current_key_name)
                    shapes = quantization_config.shapes
                    shapes_keys = shapes.keys()
                    shapes_valid = f'{tensor_name}.dense_weights.shape' in shapes_keys and f'{tensor_name}.row_offsets.shape' in shapes_keys and (f'{tensor_name}.col_vals.shape' in shapes_keys) and (f'{tensor_name}.in_perm.shape' in shapes_keys)
                    if not shapes_valid:
                        raise ValueError(f'The SpQR quantization config does not contain the shape configuration for {tensor_name}. This indicates that the configuration is either invalid or corrupted.')
                    dense_weights_shape = shapes[f'{tensor_name}.dense_weights.shape']
                    row_offsets_shape = shapes[f'{tensor_name}.row_offsets.shape']
                    col_vals_shape = shapes[f'{tensor_name}.col_vals.shape']
                    in_perm_shape = shapes[f'{tensor_name}.in_perm.shape']
                    in_features = module.in_features
                    out_features = module.out_features
                    model._modules[name] = QuantizedLinear.create_placehodler(rows=out_features, cols=in_features, bits=quantization_config.bits, beta1=quantization_config.beta1, beta2=quantization_config.beta2, dense_weights_shape=dense_weights_shape, row_offsets_shape=row_offsets_shape, col_vals_shape=col_vals_shape, in_perm_shape=in_perm_shape)
                    has_been_replaced = True
                    model._modules[name].source_cls = type(module)
                    model._modules[name].requires_grad_(False)
            else:
                pass
        if len(list(module.children())) > 0:
            _, has_been_replaced = replace_with_spqr_linear(module, quantization_config=quantization_config, modules_to_not_convert=modules_to_not_convert, current_key_name=current_key_name, has_been_replaced=has_been_replaced)
        current_key_name.pop(-1)
    return (model, has_been_replaced)

Public method that recursively replaces the Linear layers of the given model with SpQR quantized layers.
`accelerate` is needed to use this method. Returns the converted model and a boolean that indicates if the
conversion has been successful or not.

Args:
model (`torch.nn.Module`):
The model to convert, can be any `torch.nn.Module` instance.
quantization_config (`SpQRConfig`):
The quantization config object that contains the quantization parameters.
modules_to_not_convert (`list[str]`, *optional*):
A list of nn.Linear weights to not convert. If a parameter path is in the list (e.g. `lm_head.weight`), the corresponding module will not be
converted.
current_key_name (`list`, *optional*):
A list that contains the current key name. This is used for recursion and should not be passed by the user.
has_been_replaced (`bool`, *optional*):
A boolean that indicates if the conversion has been successful or not. This is used for recursion and
should not be passed by the user.


def unprotect(self, **kwargs):

        id = self.get_id().replace('/', '%2F')
        path = '%s/%s/unprotect' % (self.manager.path, id)
        self.manager.gitlab.http_put(path, **kwargs)
        self._attrs['protected'] = False

Unprotect the branch.

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

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabProtectError: If the branch could not be unprotected


def _parse_local_interface(self, config):
    match = re.search('local-interface (\\w+)', config)
    value = (match.group(1) if match else None)
    return dict(local_interface=value)

Scans the config block and parses the local-interface value

Args:
config (str): The config block to scan

Returns:
dict: A dict object that is intended to be merged into the
resource dict


def read_excel(
    filename,
    dataset_class=dataset.pandas_dataset.PandasDataset,
    expectations_config=None,
    autoinspect_func=None,
    *args, **kwargs
):

    df = pd.read_excel(filename, *args, **kwargs)
    if isinstance(df, dict):
        for key in df:
            df[key] = _convert_to_dataset_class(
                df[key], dataset_class, expectations_config, autoinspect_func)
    else:
        df = _convert_to_dataset_class(
            df, dataset_class, expectations_config, autoinspect_func)
    return df

Read a file using Pandas read_excel and return a great_expectations dataset.

Args:
filename (string): path to file to read
dataset_class (Dataset class): class to which to convert resulting Pandas df
expectations_config (string): path to great_expectations config file

Returns:
great_expectations dataset or ordered dict of great_expectations datasets,
if multiple worksheets are imported


def reduced_shape(input_shape, axes):
    constant_input_shape = tensor_util.constant_value(input_shape)
    if constant_input_shape is not None:
        constant_axes = tensor_util.constant_value(axes)
        if constant_axes is not None:
            constant_axes = np.array(constant_axes, dtype=np.int32)
            constant_input_shape = np.array(constant_input_shape, dtype=np.int32)
            constant_input_shape[constant_axes] = 1
            return constant_input_shape
    axes = ops.convert_to_tensor(axes)
    input_rank = array_ops.size(input_shape, out_type=axes.dtype)
    axes = (axes + input_rank) % input_rank
    axes_shape = array_ops.shape(axes)
    return gen_data_flow_ops.dynamic_stitch([range(input_rank), axes], [input_shape, array_ops.ones(axes_shape, dtype=input_shape.dtype)])

Helper function for reduction ops.

Args:
input_shape: 1-D Tensor, the shape of the Tensor being reduced.
axes: 1-D Tensor, the reduction axes.

Returns:
A 1-D Tensor, the output shape as if keepdims were set to True.


def __init__(self, key_value_pairs=None):

    if not key_value_pairs:
      raise errors.FormatError('Missing key value pairs value.')

    if not isinstance(key_value_pairs, list):
      raise errors.FormatError('key_value_pairs must be a list')

    for pair in key_value_pairs:
      if not isinstance(pair, dict):
        raise errors.FormatError('key_value_pair must be a dict')

      if set(pair.keys()) != set(['key', 'value']):
        key_value_pairs = ', '.join([
            '{0:s}: {1:s}'.format(key, value) for key, value in key_value_pairs
        ])
        error_message = (
            'key_value_pair missing "key" and "value" keys, got: '
            '{0:s}').format(key_value_pairs)
        raise errors.FormatError(error_message)

      WindowsRegistryKeySourceType.ValidateKey(pair['key'])

    super(WindowsRegistryValueSourceType, self).__init__()
    self.key_value_pairs = key_value_pairs

Initializes a source type.

Args:
key_value_pairs (Optional[list[tuple[str, str]]]): key path and value
name pairs, where key paths are relative to the root of the Windows
Registry.

Raises:
FormatError: when key value pairs is not set.


def is17(msg):
    if allzeros(msg):
        return False
    d = hex2bin(data(msg))
    if (bin2int(d[28:56]) != 0):
        return False
    caps = cap17(msg)
    if ('BDS20' not in caps):
        return False
    return True

Check if a message is likely to be BDS code 1,7

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

Returns:
bool: True or False


def fetch(self, plan_id, data={}, **kwargs):

        return super(Plan, self).fetch(plan_id, data, **kwargs)

Fetch Plan for given Id

Args:
plan_id : Id for which Plan object has to be retrieved

Returns:
Plan dict for given subscription Id


def get_committed_signatures(vcs):
    committed_path = _get_committed_history_path(vcs)
    known_signatures = []
    if os.path.exists(committed_path):
        with open(committed_path, 'r') as f:
            known_signatures = f.read().split()
    return known_signatures

Get the list of committed signatures

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

Returns:
list(basestring) - list of signatures


def find_contacts(self, geoms_1, geoms_2):
    for contact in self.sim.data.contact[0:self.sim.data.ncon]:
        c1_in_g1 = (self.sim.model.geom_id2name(contact.geom1) in geoms_1)
        c2_in_g2 = (self.sim.model.geom_id2name(contact.geom2) in geoms_2)
        c2_in_g1 = (self.sim.model.geom_id2name(contact.geom2) in geoms_1)
        c1_in_g2 = (self.sim.model.geom_id2name(contact.geom1) in geoms_2)
        if ((c1_in_g1 and c2_in_g2) or (c1_in_g2 and c2_in_g1)):
            (yield contact)

Finds contact between two geom groups.

Args:
geoms_1: a list of geom names (string)
geoms_2: another list of geom names (string)

Returns:
iterator of all contacts between @geoms_1 and @geoms_2


def construct_lanczos_params(self):
    self.min_eigen_vec = autograph.to_graph(utils.tf_lanczos_smallest_eigval)

    def _m_vector_prod_fn(x):
        return self.get_psd_product(x, dtype=self.lanczos_dtype)

    def _h_vector_prod_fn(x):
        return self.get_h_product(x, dtype=self.lanczos_dtype)
    self.m_min_vec_estimate = np.zeros(shape=(self.matrix_m_dimension, 1), dtype=np.float64)
    zeros_m = tf.zeros(shape=(self.matrix_m_dimension, 1), dtype=tf.float64)
    self.m_min_vec_ph = tf.placeholder_with_default(input=zeros_m, shape=(self.matrix_m_dimension, 1), name='m_min_vec_ph')
    (self.m_min_eig, self.m_min_vec) = self.min_eigen_vec(_m_vector_prod_fn, self.matrix_m_dimension, self.m_min_vec_ph, self.lzs_params['max_iter'], dtype=self.lanczos_dtype)
    self.m_min_eig = tf.cast(self.m_min_eig, self.nn_dtype)
    self.m_min_vec = tf.cast(self.m_min_vec, self.nn_dtype)
    self.h_min_vec_estimate = np.zeros(shape=((self.matrix_m_dimension - 1), 1), dtype=np.float64)
    zeros_h = tf.zeros(shape=((self.matrix_m_dimension - 1), 1), dtype=tf.float64)
    self.h_min_vec_ph = tf.placeholder_with_default(input=zeros_h, shape=((self.matrix_m_dimension - 1), 1), name='h_min_vec_ph')
    (self.h_min_eig, self.h_min_vec) = self.min_eigen_vec(_h_vector_prod_fn, (self.matrix_m_dimension - 1), self.h_min_vec_ph, self.lzs_params['max_iter'], dtype=self.lanczos_dtype)
    self.h_min_eig = tf.cast(self.h_min_eig, self.nn_dtype)
    self.h_min_vec = tf.cast(self.h_min_vec, self.nn_dtype)

Computes matrices T and V using the Lanczos algorithm.

Args:
k: number of iterations and dimensionality of the tridiagonal matrix
Returns:
eig_vec: eigen vector corresponding to min eigenvalue


def trace_set_format(self, fmt):

        cmd = enums.JLinkTraceCommand.SET_FORMAT
        data = ctypes.c_uint32(fmt)
        res = self._dll.JLINKARM_TRACE_Control(cmd, ctypes.byref(data))
        if (res == 1):
            raise errors.JLinkException('Failed to set trace format.')
        return None

Sets the format for the trace buffer to use.

Args:
self (JLink): the ``JLink`` instance.
fmt (int): format for the trace buffer; this is one of the attributes
of ``JLinkTraceFormat``.

Returns:
``None``


def campaign(self, name, **kwargs):
    group_obj = Campaign(name, **kwargs)
    return self._group(group_obj)

Add Campaign data to Batch object.

Args:
name (str): The name for this Group.
date_added (str, kwargs): The date timestamp the Indicator was created.
first_seen (str, kwargs): The first seen datetime expression for this Group.
xid (str, kwargs): The external id for this Group.

Returns:
obj: An instance of Campaign.


def GetFormattedSources(self, event):

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

    return event_formatter.GetSources(event)

Retrieves the formatted sources related to the event.

Args:
event (EventObject): event.

Returns:
tuple: containing:

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


def _initializer(shape, dtype=dtypes.float32, partition_info=None):
    if dtype != dtypes.float32:
        raise TypeError('Currently, only float32 is supported. Received dtype: {}'.format(dtype))
    if len(shape) != 2:
        raise ValueError('Expected 2-dim shape, but received: {}'.format(shape))
    if shape[0] <= 0:
        raise ValueError('Expected 1st dim of shape to be > 0, but received shape: {}'.format(shape))
    if shape[1] != new_col_vocab_size + num_col_oov_buckets:
        raise ValueError('Expected 2nd dim of shape to be new_col_vocab_size ({}) + num_col_oov_buckets ({}) = {}, but received shape: {}'.format(new_col_vocab_size, num_col_oov_buckets, new_col_vocab_size + num_col_oov_buckets, shape))
    offset = 0
    if partition_info is not None:
        offset = partition_info.single_offset(shape)
    if offset + shape[0] > new_row_vocab_size + num_row_oov_buckets:
        raise ValueError('Trying to initialize {} additional rows after {} rows have already been initialized, which would exceed expected total row count of new_row_vocab_size ({}) + num_row_oov_buckets ({}) = {}.'.format(shape[0], offset, new_row_vocab_size, num_row_oov_buckets, new_row_vocab_size + num_row_oov_buckets))
    row_oov_buckets_to_use = min(shape[0], max(0, offset + shape[0] - new_row_vocab_size))
    num_rows_to_load = shape[0] - row_oov_buckets_to_use
    if offset > new_row_vocab_size:
        if shape[0] != row_oov_buckets_to_use:
            raise ValueError('Partitioned variable offset is greater than new vocab size and not operating on OOV-only partition.')
        return initializer(shape)
    return _load_and_remap_matrix(ckpt_path=ckpt_path, old_tensor_name=old_tensor_name, new_row_vocab_offset=offset, num_rows_to_load=num_rows_to_load, new_col_vocab_size=new_col_vocab_size, initializer=initializer, old_row_vocab_size=old_row_vocab_size, old_row_vocab_file=old_row_vocab_file, new_row_vocab_file=new_row_vocab_file, old_col_vocab_file=old_col_vocab_file, new_col_vocab_file=new_col_vocab_file, num_row_oov_buckets=row_oov_buckets_to_use, num_col_oov_buckets=num_col_oov_buckets, max_rows_in_memory=max_rows_in_memory)

Variable initializer.

Args:
shape: Shape of `Tensor` to return. Should include OOV on both axes.
dtype: Must be float32.
partition_info: variable_scope._PartitionInfo.

Returns:
`Tensor` of shape `shape`.

Raises:
TypeError: If `dtype` is anything other than float32.
ValueError: For shape mismatch upon invocation.


def rotate_capture_handler_log(self, name):

        for sc_key, sc in self._stream_capturers.iteritems():
            for h in sc[0].capture_handlers:
                if h['name'] == name:
                    sc[0]._rotate_log(h)

Force a rotation of a handler's log file

Args:
name:
The name of the handler who's log file should be rotated.


def has_mixture(val: Any) -> bool:
    getter = getattr(val, '_has_mixture_', None)
    result = (NotImplemented if (getter is None) else getter())
    if (result is not NotImplemented):
        return result
    return (mixture(val, None) is not None)

Returns whether the value has a mixture representation.

Returns:
If `val` has a `_has_mixture_` method and its result is not
NotImplemented, that result is returned. Otherwise, if the value
has a `_mixture_` method return True if that has a non-default value.
Returns False if neither function exists.


def run_step(self, context):

        logger.debug("starting")

        self.set_step_input_context(context)

        if self.while_decorator:
            self.while_decorator.while_loop(context,
                                            self.run_foreach_or_conditional)
        else:
            self.run_foreach_or_conditional(context)

        logger.debug("done")

Run a single pipeline step.

Args:
context: (pypyr.context.Context) The pypyr context. This arg will
mutate.


class CounterMetric(Metric):

    def __init__(self, counter_metric, submit_timestamp, metric_id):
        value = counter_metric.result
        super().__init__(submit_timestamp, metric_id, value, counter_metric)

The Counter Metric in ready-to-publish format.

Args:
counter_metric (object): counter metric object from MetricResult
submit_timestamp (float): date-time of saving metric to database
metric_id (uuid): unique id to identify test run


def _WritesString(self, content):
    content_bytes = codecs.encode(content, 'utf-8')
    self._sample_file.write(content_bytes)

Writes a string to the sample file.

Args:
content (str): content to write to the sample file.


def _parse_authors(details):

    authors = details.find(
        "tr",
        {"id": "ctl00_ContentPlaceHolder1_tblRowAutor"}
    )

    if not authors:
        return []

    author_list = []
    for author in authors[0].find("a"):
        author_obj = Author(author.getContent())

        if "href" in author.params:
            author_obj.URL = author.params["href"]

        author_list.append(author_obj)

    return author_list

Parse authors of the book.

Args:
details (obj): HTMLElement containing slice of the page with details.

Returns:
list: List of :class:`structures.Author` objects. Blank if no author \
found.


def remove_long_seq(maxlen, seq, label):
    new_seq, new_label = ([], [])
    for x, y in zip(seq, label):
        if len(x) < maxlen:
            new_seq.append(x)
            new_label.append(y)
    return (new_seq, new_label)

Removes sequences that exceed the maximum length.

Args:
maxlen: Int, maximum length of the output sequences.
seq: List of lists, where each sublist is a sequence.
label: List where each element is an integer.

Returns:
new_seq, new_label: shortened lists for `seq` and `label`.


def _single_request(self, method, *args, **kwargs):
    _method = self._service
    for item in method.split('.'):
        if method.endswith(item):
            _method = getattr(_method, item)(*args, **kwargs)
        else:
            _method = getattr(_method, item)()
    _method.uri = _method.uri.replace('$ENDPOINT', self._endpoint)
    try:
        return _method.execute(http=self._http)
    except googleapiclient.errors.HttpError as exc:
        response = json.loads(exc.content.decode('utf-8'))['error']
        raise APIError(code=response['code'], message=response['message'], http_error=exc)

Make a single request to the fleet API endpoint

Args:
method (str): A dot delimited string indicating the method to call.  Example: 'Machines.List'
*args: Passed directly to the method being called.
**kwargs: Passed directly to the method being called.

Returns:
dict: The response from the method called.

Raises:
fleet.v1.errors.APIError: Fleet returned a response code >= 400


def stage(self, pipeline_name, stage_name, pipeline_counter=None):

        return Stage(self, pipeline_name, stage_name, pipeline_counter=pipeline_counter)

Returns an instance of :class:`Stage`

Args:
pipeline_name (str): Name of the pipeline the stage belongs to
stage_name (str): Name of the stage to act on
pipeline_counter (int): The pipeline instance the stage is for.

Returns:
Stage: an instantiated :class:`Stage`.


def _CalculateNTFSTimeHash(self, file_entry):
    date_time_values = []
    access_time = getattr(file_entry, 'access_time', None)
    if access_time:
        date_time_string = access_time.CopyToDateTimeString()
        date_time_values.append('atime:{0:s}'.format(date_time_string))
    creation_time = getattr(file_entry, 'creation_time', None)
    if creation_time:
        date_time_string = creation_time.CopyToDateTimeString()
        date_time_values.append('crtime:{0:s}'.format(date_time_string))
    modification_time = getattr(file_entry, 'modification_time', None)
    if modification_time:
        date_time_string = modification_time.CopyToDateTimeString()
        date_time_values.append('mtime:{0:s}'.format(date_time_string))
    change_time = getattr(file_entry, 'change_time', None)
    if change_time:
        date_time_string = change_time.CopyToDateTimeString()
        date_time_values.append('ctime:{0:s}'.format(date_time_string))
    date_time_values = ''.join(date_time_values)
    date_time_values = date_time_values.encode('ascii')
    hash_value = hashlib.md5()
    hash_value.update(date_time_values)
    return hash_value.hexdigest()

Calculates an MD5 from the date and time value of a NTFS file entry.

Args:
file_entry (dfvfs.FileEntry): file entry.

Returns:
str: hexadecimal representation of the MD5 hash value of the date and
time values of the file entry.


def max_sequence_length(self, dataset_split):
    return {problem.DatasetSplit.TRAIN: 64, problem.DatasetSplit.EVAL: 128, problem.DatasetSplit.TEST: 128}[dataset_split]

Determine the maximum sequence length given a dataset_split.

Args:
dataset_split: A problem.DatasetSplit.

Returns:
The maximum length that a sequence can be for this dataset_split.


def resize(x, mode, factor=4):

    assert mode in ['bilinear', 'nearest'], mode
    shp = tf.shape(x)[2:] * factor

    x = tf.transpose(x, [0, 2, 3, 1])
    if mode == 'bilinear':
        x = tf.image.resize_bilinear(x, shp, align_corners=True)
    else:

        x = tf.image.resize_nearest_neighbor(x, shp, align_corners=False)

    return tf.transpose(x, [0, 3, 1, 2])

Resize input tensor with unkown input-shape by a factor

Args:
x (tf.Tensor): tensor NCHW
factor (int, optional): resize factor for H, W

Note:
Differences here against Caffe have huge impacts on the
quality of the predictions.

Returns:
tf.Tensor: resized tensor NCHW


def download_and_extract(self, url_or_urls):
    with self._downloader.tqdm():
        with self._extractor.tqdm():
            return _map_promise(self._download_extract, url_or_urls)

Download and extract given url_or_urls.

Is roughly equivalent to:

```
extracted_paths = dl_manager.extract(dl_manager.download(url_or_urls))
```

Args:
url_or_urls: url or `list`/`dict` of urls to download and extract. Each
url can be a `str` or `tfds.download.Resource`.

If not explicitly specified in `Resource`, the extraction method will
automatically be deduced from downloaded file name.

Returns:
extracted_path(s): `str`, extracted paths of given URL(s).


def create_statement_inspection_table(sts: List[Influence]):

    columns = [
        "un_groundings",
        "subj_polarity",
        "obj_polarity",
        "Sentence",
        "Source API",
    ]

    polarity_to_str = lambda x: "+" if x == 1 else "-" if x == -1 else "None"
    l = []
    for s in sts:
        subj_un_grounding = s.subj.db_refs["UN"][0][0].split("/")[-1]
        obj_un_grounding = s.obj.db_refs["UN"][0][0].split("/")[-1]
        subj_polarity = s.subj_delta["polarity"]
        obj_polarity = s.obj_delta["polarity"]
        subj_adjectives = s.subj_delta["adjectives"]
        for e in s.evidence:
            l.append(
                (
                    (subj_un_grounding, obj_un_grounding),
                    subj_polarity,
                    obj_polarity,
                    e.text,
                    e.source_api,
                )
            )

    df = pd.DataFrame(l, columns=columns)
    df = df.pivot_table(index=["un_groundings", "Source API", "Sentence"])

    def hover(hover_color="
        return dict(
            selector="tr:hover",
            props=[("background-color", "%s" % hover_color)],
        )

    styles = [
        hover(),
        dict(props=[("font-size", "100%"), ("font-family", "Gill Sans")]),
    ]

    return df.style.set_table_styles(styles)

Display an HTML representation of a table with INDRA statements to
manually inspect for validity.

Args:
sts: A list of INDRA statements to be manually inspected for validity.


def notify_owner(func):

    def wrapper(self, *args, **kwargs):
        old = self._saved_copy()
        result = func(self, *args, **kwargs)
        self._notify_owners(old)
        return result
    wrapper.__doc__ = ('Container method ``%s`` instrumented to notify property owners' % func.__name__)
    return wrapper

A decorator for mutating methods of property container classes
that notifies owners of the property container about mutating changes.

Args:
func (callable) : the container method to wrap in a notification

Returns:
wrapped method

Examples:

A ``__setitem__`` could be wrapped like this:

.. code-block:: python

# x[i] = y
@notify_owner
def __setitem__(self, i, y):
return super(PropertyValueDict, self).__setitem__(i, y)

The returned wrapped method will have a docstring indicating what
original method it is wrapping.


def write_to_file(src, dst):

    n = 0
    for block in src:
        dst.write(block)
        n += len(block)
    return n

Write data from `src` into `dst`.

Args:
src (iterable): iterable that yields blocks of data to write
dst (file-like object): file-like object that must support
.write(block)

Returns:
number of bytes written to `dst`


def get_user(self, user_id):

        try:
            return self._user_dict[user_id]
        except KeyError:
            logger.warning('UserList returning unknown User for UserID %s',
                           user_id)
            return User(user_id, None, None, None, [], False)

Get a user by its ID.

Args:
user_id (~hangups.user.UserID): The ID of the user.

Raises:
KeyError: If no such user is known.

Returns:
:class:`~hangups.user.User` with the given ID.


def _sign_operation(op):

    md5 = hashlib.md5()
    md5.update(op.consumerId.encode('utf-8'))
    md5.update(b'\x00')
    md5.update(op.operationName.encode('utf-8'))
    if op.labels:
        signing.add_dict_to_hash(md5, encoding.MessageToPyValue(op.labels))
    return md5.digest()

Obtains a signature for an operation in a ReportRequest.

Args:
op (:class:`endpoints_management.gen.servicecontrol_v1_messages.Operation`): an
operation used in a `ReportRequest`

Returns:
string: a unique signature for that operation


def handler_for_name(fq_name):
    resolved_name = for_name(fq_name)
    if isinstance(resolved_name, (type, types.ClassType)):
        return resolved_name()
    elif isinstance(resolved_name, types.MethodType):
        return getattr(resolved_name.im_class(), resolved_name.__name__)
    else:
        return resolved_name

Resolves and instantiates handler by fully qualified name.

First resolves the name using for_name call. Then if it resolves to a class,
instantiates a class, if it resolves to a method - instantiates the class and
binds method to the instance.

Args:
fq_name: fully qualified name of something to find.

Returns:
handler instance which is ready to be called.


def htmlcolor_to_rgb(str_color):

    if not (str_color.startswith('
        raise ValueError("Bad html color format. Expected: '
    result = [1.0 * int(n, 16) / 255 for n in (str_color[1:3], str_color[3:5], str_color[5:])]
    return result

function to convert HTML-styly color string to RGB values

Args:
s: Color in HTML format

Returns:
list of three RGB color components


def trim_wav_ms(in_path: Path, out_path: Path, start_time: int, end_time: int) -> None:
    try:
        trim_wav_sox(in_path, out_path, start_time, end_time)
    except FileNotFoundError:
        trim_wav_pydub(in_path, out_path, start_time, end_time)
    except subprocess.CalledProcessError:
        trim_wav_pydub(in_path, out_path, start_time, end_time)

Extracts part of a WAV File.

First attempts to call sox. If sox is unavailable, it backs off to
pydub+ffmpeg.

Args:
in_path: A path to the source file to extract a portion of
out_path: A path describing the to-be-created WAV file.
start_time: The point in the source WAV file at which to begin
extraction.
end_time: The point in the source WAV file at which to end extraction.


def UnlockScanNode(self, path_spec):

    if not self.HasScanNode(path_spec):
      raise KeyError('Scan node does not exist.')

    if path_spec not in self._locked_scan_nodes:
      raise KeyError('Scan node is not locked.')

    del self._locked_scan_nodes[path_spec]

    self._scan_nodes[path_spec].scanned = False

Marks a scan node as unlocked.

Args:
path_spec (PathSpec): path specification.

Raises:
KeyError: if the scan node does not exists or is not locked.


def get_tensor_file_paths(self, node_name, output_slot, debug_op, device_name=None):
    device_name = self._infer_device_name(device_name, node_name)
    watch_key = _get_tensor_watch_key(node_name, output_slot, debug_op)
    if watch_key not in self._watch_key_to_datum[device_name]:
        raise WatchKeyDoesNotExistInDebugDumpDirError('Watch key "%s" does not exist in the debug dump of device %s' % (watch_key, device_name))
    return [datum.file_path for datum in self._watch_key_to_datum[device_name][watch_key]]

Get the file paths from a debug-dumped tensor.

Args:
node_name: (`str`) name of the node that the tensor is produced by.
output_slot: (`int`) output slot index of tensor.
debug_op: (`str`) name of the debug op.
device_name: (`str`) name of the device. If there is only one device or if
the specified debug_watch_key exists on only one device, this argument
is optional.

Returns:
List of file path(s) loaded. This is a list because each debugged tensor
may be dumped multiple times.

Raises:
WatchKeyDoesNotExistInDebugDumpDirError: If the tensor does not exist in
the debug-dump data.


def load_actor_class(self, driver_id, function_descriptor):
    function_id = function_descriptor.function_id
    actor_class = self._loaded_actor_classes.get(function_id, None)
    if (actor_class is None):
        if self._worker.load_code_from_local:
            driver_id = ray.DriverID.nil()
            actor_class = self._load_actor_from_local(driver_id, function_descriptor)
        else:
            actor_class = self._load_actor_class_from_gcs(driver_id, function_descriptor)
        self._loaded_actor_classes[function_id] = actor_class
        module_name = function_descriptor.module_name
        actor_class_name = function_descriptor.class_name
        actor_methods = inspect.getmembers(actor_class, predicate=is_function_or_method)
        for (actor_method_name, actor_method) in actor_methods:
            method_descriptor = FunctionDescriptor(module_name, actor_method_name, actor_class_name)
            method_id = method_descriptor.function_id
            executor = self._make_actor_method_executor(actor_method_name, actor_method, actor_imported=True)
            self._function_execution_info[driver_id][method_id] = FunctionExecutionInfo(function=executor, function_name=actor_method_name, max_calls=0)
            self._num_task_executions[driver_id][method_id] = 0
        self._num_task_executions[driver_id][function_id] = 0
    return actor_class

Load the actor class.

Args:
driver_id: Driver ID of the actor.
function_descriptor: Function descriptor of the actor constructor.

Returns:
The actor class.


def SetFlushInterval(self, flush_interval):
    self._flush_interval = flush_interval
    logger.debug('Elasticsearch flush interval: {0:d}'.format(flush_interval))

Set the flush interval.

Args:
flush_interval (int): number of events to buffer before doing a bulk
insert.


def intersection_update(self, *others):
    for other in map(self._as_mapping, others):
        for (element, current_count) in list(self.items()):
            multiplicity = other.get(element, 0)
            if (multiplicity < current_count):
                self[element] = multiplicity

r"""Update the multiset, keeping only elements found in it and all others.

>>> ms = Multiset('aab')
>>> ms.intersection_update('bc')
>>> sorted(ms)
['b']

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

>>> ms = Multiset('aabc')
>>> ms &= Multiset('abbd')
>>> sorted(ms)
['a', 'b']

For a variant of the operation which does not modify the multiset, but returns a new
multiset instead see :meth:`intersection`.

Args:
others: The other sets to intersect this multiset with. Can also be any :class:`~typing.Iterable`\[~T]
or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T].


def proc_val(key, val):

        list_keys = ("LDAUU", "LDAUL", "LDAUJ", "MAGMOM", "DIPOL",
                     "LANGEVIN_GAMMA", "QUAD_EFG", "EINT")
        bool_keys = ("LDAU", "LWAVE", "LSCALU", "LCHARG", "LPLANE", "LUSE_VDW",
                     "LHFCALC", "ADDGRID", "LSORBIT", "LNONCOLLINEAR")
        float_keys = ("EDIFF", "SIGMA", "TIME", "ENCUTFOCK", "HFSCREEN",
                      "POTIM", "EDIFFG", "AGGAC", "PARAM1", "PARAM2")
        int_keys = ("NSW", "NBANDS", "NELMIN", "ISIF", "IBRION", "ISPIN",
                    "ICHARG", "NELM", "ISMEAR", "NPAR", "LDAUPRINT", "LMAXMIX",
                    "ENCUT", "NSIM", "NKRED", "NUPDOWN", "ISPIND", "LDAUTYPE",
                    "IVDW")

        def smart_int_or_float(numstr):
            if numstr.find(".") != -1 or numstr.lower().find("e") != -1:
                return float(numstr)
            else:
                return int(numstr)

        try:
            if key in list_keys:
                output = []
                toks = re.findall(
                    r"(-?\d+\.?\d*)\*?(-?\d+\.?\d*)?\*?(-?\d+\.?\d*)?", val)
                for tok in toks:
                    if tok[2] and "3" in tok[0]:
                        output.extend(
                            [smart_int_or_float(tok[2])] * int(tok[0])
                            * int(tok[1]))
                    elif tok[1]:
                        output.extend([smart_int_or_float(tok[1])] *
                                      int(tok[0]))
                    else:
                        output.append(smart_int_or_float(tok[0]))
                return output
            if key in bool_keys:
                m = re.match(r"^\.?([T|F|t|f])[A-Za-z]*\.?", val)
                if m:
                    if m.group(1) == "T" or m.group(1) == "t":
                        return True
                    else:
                        return False
                raise ValueError(key + " should be a boolean type!")

            if key in float_keys:
                return float(re.search(r"^-?\d*\.?\d*[e|E]?-?\d*", val).group(0))

            if key in int_keys:
                return int(re.match(r"^-?[0-9]+", val).group(0))

        except ValueError:
            pass

        try:
            val = int(val)
            return val
        except ValueError:
            pass

        try:
            val = float(val)
            return val
        except ValueError:
            pass

        if "true" in val.lower():
            return True

        if "false" in val.lower():
            return False

        return val.strip().capitalize()

Static helper method to convert INCAR parameters to proper types, e.g.,
integers, floats, lists, etc.

Args:
key: INCAR parameter key
val: Actual value of INCAR parameter.


def api_request(self, method_name, params):
    url = self._method_url(method_name)
    data = json.dumps(params)
    return self._make_request(url=url, method='post', data=data)

Execute an arbitrary method.

Args:
method_name (str): include the controller name: 'devices/search'
params (dict): the method parameters
Returns:
A dict with the response
Raises:
requests.exceptions.HTTPError


def ensure_app_data_dir(appname, *args):

    from ubelt import util_path
    dpath = get_app_data_dir(appname, *args)
    util_path.ensuredir(dpath)
    return dpath

Calls `get_app_data_dir` but ensures the directory exists.

Args:
appname (str): the name of the application
*args: any other subdirectories may be specified

SeeAlso:
get_app_data_dir

Example:
>>> import ubelt as ub
>>> dpath = ub.ensure_app_data_dir('ubelt')
>>> assert exists(dpath)


def _get_schema(cls, schema):
    if isinstance(schema, string_types):
        schema = cls._get_object_from_python_path(schema)
    if isclass(schema):
        schema = schema()
    if (not isinstance(schema, Schema)):
        raise TypeError('The schema must be a path to a Marshmallow schema or a Marshmallow schema.')
    return schema

Method that will fetch a Marshmallow schema flexibly.

Args:
schema (marshmallow.Schema|str): Either the schema class, an
instance of a schema, or a Python path to a schema.

Returns:
marshmallow.Schema: The desired schema.

Raises:
TypeError: This is raised if the provided object isn't
a Marshmallow schema.


def fwd(self, x_data):
    x_data = numpy.asfarray(x_data)
    shape = x_data.shape
    x_data = x_data.reshape(len(self), (- 1))
    (lower, upper) = evaluation.evaluate_bound(self, x_data)
    q_data = numpy.zeros(x_data.shape)
    indices = (x_data > upper)
    q_data[indices] = 1
    indices = ((~ indices) & (x_data >= lower))
    q_data[indices] = numpy.clip(evaluation.evaluate_forward(self, x_data), a_min=0, a_max=1)[indices]
    q_data = q_data.reshape(shape)
    return q_data

Forward Rosenblatt transformation.

Args:
x_data (numpy.ndarray):
Location for the distribution function. ``x_data.shape`` must
be compatible with distribution shape.

Returns:
(numpy.ndarray):
Evaluated distribution function values, where
``out.shape==x_data.shape``.


def delete_asset(self, asset_id, asset_type):
    return self.asset(asset_id, asset_type=asset_type, action='DELETE')

Delete the asset with the provided asset_id.

Args:
asset_id: The id of the asset.
asset_type: The asset type.

Returns:


def translate_node_id(self, ni: PrefName, sctx: SchemaContext) -> QualName:

        p, s, loc = ni.partition(":")
        if not s:
            return (ni, sctx.default_ns)
        try:
            mdata = self.modules[sctx.text_mid]
        except KeyError:
            raise ModuleNotRegistered(*sctx.text_mid) from None
        try:
            return (loc, self.namespace(mdata.prefix_map[p]))
        except KeyError:
            raise UnknownPrefix(p, sctx.text_mid) from None

Translate node identifier to a qualified name.

Args:
ni: Node identifier (with optional prefix).
sctx: SchemaContext.

Raises:
ModuleNotRegistered: If `mid` is not registered in the data model.
UnknownPrefix: If the prefix specified in `ni` is not declared.


def generate(self, descriptors):

        model_ids = self.search_tree.adj_list.keys()

        target_graph = None
        father_id = None
        descriptors = deepcopy(descriptors)
        elem_class = Elem
        if self.optimizemode is OptimizeMode.Maximize:
            elem_class = ReverseElem

        pq = PriorityQueue()
        temp_list = []
        for model_id in model_ids:
            metric_value = self.searcher.get_metric_value_by_id(model_id)
            temp_list.append((metric_value, model_id))
        temp_list = sorted(temp_list)
        for metric_value, model_id in temp_list:
            graph = self.searcher.load_model_by_id(model_id)
            graph.clear_operation_history()
            graph.clear_weights()
            pq.put(elem_class(metric_value, model_id, graph))

        t = 1.0
        t_min = self.t_min
        alpha = 0.9
        opt_acq = self._get_init_opt_acq_value()
        while not pq.empty() and t > t_min:
            elem = pq.get()
            if self.optimizemode is OptimizeMode.Maximize:
                temp_exp = min((elem.metric_value - opt_acq) / t, 1.0)
            else:
                temp_exp = min((opt_acq - elem.metric_value) / t, 1.0)
            ap = math.exp(temp_exp)
            if ap >= random.uniform(0, 1):
                for temp_graph in transform(elem.graph):
                    if contain(descriptors, temp_graph.extract_descriptor()):
                        continue

                    temp_acq_value = self.acq(temp_graph)
                    pq.put(elem_class(temp_acq_value, elem.father_id, temp_graph))
                    descriptors.append(temp_graph.extract_descriptor())
                    if self._accept_new_acq_value(opt_acq, temp_acq_value):
                        opt_acq = temp_acq_value
                        father_id = elem.father_id
                        target_graph = deepcopy(temp_graph)
            t *= alpha

        if father_id is None:
            return None, None
        nm_graph = self.searcher.load_model_by_id(father_id)
        for args in target_graph.operation_history:
            getattr(nm_graph, args[0])(*list(args[1:]))
        return nm_graph, father_id

Generate new architecture.
Args:
descriptors: All the searched neural architectures.
Returns:
graph: An instance of Graph. A morphed neural network with weights.
father_id: The father node ID in the search tree.


def read_as_base64(fn):
    with open(fn) as unpacked_file:
        with tempfile.TemporaryFile() as b64_file:
            base64.encode(unpacked_file, b64_file)
            b64_file.flush()
            b64_file.seek(0)
            return b64_file.read()

Convert given `fn` to base64 and return it. This method does the process
in not-so-much memory consuming way.

Args:
fn (str): Path to the file which should be converted.

Returns:
str: File encoded as base64.


def __init__(self, cumulative=IGNORED, name=IGNORED, scalar=IGNORED, kind=IGNORED):
    if name != IGNORED and (not isinstance(name, MetricStructuredNameMatcher)):
        raise ValueError('name must be a MetricStructuredNameMatcher.')
    self.cumulative = cumulative
    self.name = name
    self.scalar = scalar
    self.kind = kind

Creates a MetricUpdateMatcher.

Any property not passed in to the constructor will be ignored when matching.

Args:
cumulative: A boolean.
name: A MetricStructuredNameMatcher object that matches the name.
scalar: An integer with the metric update.
kind: A string defining the kind of counter.


def get_config(self):
    all_args = tf_inspect.getfullargspec(self.__init__).args
    config = {'name': self.name, 'trainable': self.trainable}
    if hasattr(self, '_batch_input_shape'):
        config['batch_input_shape'] = self._batch_input_shape
    config['dtype'] = policy.serialize(self._dtype_policy)
    if hasattr(self, 'dynamic'):
        if self.dynamic:
            config['dynamic'] = self.dynamic
        elif 'dynamic' in all_args:
            all_args.remove('dynamic')
    expected_args = config.keys()
    extra_args = [arg for arg in all_args if arg not in expected_args]
    if len(extra_args) > 1 and hasattr(self.get_config, '_is_default'):
        raise NotImplementedError('Layers with arguments in `__init__` must override `get_config`.')
    return config

Returns the config of the layer.

A layer config is a Python dictionary (serializable)
containing the configuration of a layer.
The same layer can be reinstantiated later
(without its trained weights) from this configuration.

The config of a layer does not include connectivity
information, nor the layer class name. These are handled
by `Network` (one layer of abstraction above).

Returns:
Python dictionary.


def _apply_credentials(auto_refresh=True, credentials=None, headers=None):
    token = credentials.get_credentials().access_token
    if (auto_refresh is True):
        if (token is None):
            token = credentials.refresh(access_token=None, timeout=10)
        elif credentials.jwt_is_expired():
            token = credentials.refresh(timeout=10)
    headers.update({'Authorization': 'Bearer {}'.format(token)})

Update Authorization header.

Update request headers with latest `access_token`. Perform token
`refresh` if token is ``None``.

Args:
auto_refresh (bool): Perform token refresh if access_token is ``None`` or expired. Defaults to ``True``.
credentials (class): Read-only credentials.
headers (class): Requests `CaseInsensitiveDict`.


def set_large_file_size(self, st_size):
    self._check_positive_int(st_size)
    if self.st_size:
        self.size = 0
    if self.filesystem:
        self.filesystem.change_disk_usage(st_size, self.name, self.st_dev)
    self.st_size = st_size
    self._byte_contents = None

Sets the self.st_size attribute and replaces self.content with None.

Provided specifically to simulate very large files without regards
to their content (which wouldn't fit in memory).
Note that read/write operations with such a file raise
:py:class:`FakeLargeFileIoException`.

Args:
st_size: (int) The desired file size

Raises:
IOError: if the st_size is not a non-negative integer,
or if st_size exceeds the available file system space


def _attach_debugger_logic(model, debug_path: Optional[str]='.', do_prune_layers: Optional[bool]=True, use_repr: bool=True):
    class_name = model.__class__.__name__
    model._call_tree = {'module_path': class_name, 'inputs': None, 'outputs': None, 'children': []}
    model._debugger_model_call_stack = []
    model._debugger_module_dump_name = class_name
    if debug_path:
        try:
            os.makedirs(debug_path, exist_ok=True)
        except Exception as e:
            raise ValueError(f'Unexpected or existing debug_path={debug_path}.') from e

    def wrap_forward(module, full_path):
        orig_forward = module.forward

        @functools.wraps(orig_forward)
        def wrapped_forward(*inps, **kws):
            if _is_rank_zero():
                dict_inputs = {'args': inps, 'kwargs': kws}
                dict_inputs = {k: dict_inputs[k] for k in dict_inputs if len(dict_inputs[k]) > 0}
                node = {'module_path': full_path, 'inputs': _serialize_io(dict_inputs, debug_path=debug_path, use_repr=use_repr, path_to_value=f'{full_path}_inputs'), 'outputs': None, 'children': []}
                model._debugger_model_call_stack.append(node)
            with torch.no_grad():
                out = orig_forward(*inps, **kws)
            if _is_rank_zero():
                if sum((1 for _ in module.named_children())) > 0:
                    node['outputs'] = None
                else:
                    node['outputs'] = _serialize_io(out, debug_path=debug_path, use_repr=use_repr, path_to_value=f'{full_path}_outputs')
                finished = model._debugger_model_call_stack.pop()
                if not finished['children']:
                    finished.pop('children')
                if model._debugger_model_call_stack:
                    model._debugger_model_call_stack[-1]['children'].append(finished)
            return out
        module.forward = wrapped_forward
    for name, submodule in model.named_modules():
        if name == '':
            continue
        wrap_forward(submodule, f'{class_name}.{name}')
    real_top_forward = model.forward

    @functools.wraps(real_top_forward)
    def top_wrapped_forward(*inps, **kws):
        if _is_rank_zero():
            top_node = {'module_path': f'{class_name} (top-level)', 'inputs': _serialize_io({'args': inps, 'kwargs': kws}, debug_path=debug_path, use_repr=use_repr, path_to_value=f'{class_name}_inputs'), 'outputs': None, 'children': []}
            model._debugger_model_call_stack.append(top_node)
        out = real_top_forward(*inps, **kws)
        if _is_rank_zero() and model._debugger_model_call_stack:
            top_node['outputs'] = _serialize_io(out, debug_path=debug_path, use_repr=use_repr, path_to_value=f'{class_name}_outputs')
            finished = model._debugger_model_call_stack.pop()
            model._call_tree['inputs'] = finished['inputs']
            model._call_tree['outputs'] = finished['outputs']
            model._call_tree['children'] = finished['children']
            [model._call_tree.pop(k, None) for k in list(model._call_tree.keys()) if not model._call_tree[k]]
            if do_prune_layers:
                prune_intermediate_layers(model._call_tree)
            log_model_debug_trace(debug_path=debug_path, model=model)
        return out
    model.forward = top_wrapped_forward

Attaches a debugging wrapper to every module in the model.

This records structured inputs and outputs during the forward pass into a call tree.

Args:
model (`PreTrainedModel`, `nn.Module`): Model to wrap.
debug_path (`str`): Optional directory to dump debug JSON files.
do_prune_layers (`bool`, *optional*, defaults to `True`): Whether to prune intermediate layers.
use_repr (bool, *optional*, defaults to `True`): Whether to save a `repr()`-ized version of the tensors as the
`value` property in the asscoiated FULL_TENSORS.json file, or to store full tensors in separate SafeTensors
files and store the relative path to that file in the `value` property.


def save(self, clean=True):
    ret = {}
    if clean:
        self._dirty = False
    else:
        ret['_dirty'] = self._dirty
    return ret

Serialize into raw representation. Clears the dirty bit by default.

Args:
clean (bool): Whether to clear the dirty bit.

Returns:
dict: Raw.


def evaluate(self, node: InstanceNode) -> XPathValue:

        return self._eval(XPathContext(node, node, 1, 1))

Evaluate the receiver and return the result.

Args:
node: Context node for XPath evaluation.

Raises:
XPathTypeError: If a subexpression of the receiver is of a wrong
type.


def _rewrite_input_as_indexed_slices(body_grad_graph, grad_output_slices, forward_input, loop_vars):
    init_slices = _create_grad_indexed_slices_init(grad_output_slices, forward_input)
    with body_grad_graph.as_default():
        input_slices = indexed_slices.IndexedSlices(values=body_grad_graph.capture(init_slices.values, allowlisted=True), indices=body_grad_graph.capture(init_slices.indices, allowlisted=True), dense_shape=body_grad_graph.capture(init_slices.dense_shape, allowlisted=True))
        for t in _flatten(init_slices):
            captured_t = body_grad_graph.captures.pop(t)
            body_grad_graph.inputs.remove(captured_t)
        new_output_slices = _rewrite_grad_indexed_slices_output(grad_output_slices, input_slices)
    return _update_indexed_slices_param(body_grad_graph, loop_vars, init_slices, input_slices, new_output_slices, grad_output_slices)

Rewrites grad_output_slices's corresponding input to be an IndexedSlices.

This rewrite requires that forward_input was captured in the forward loop,
i.e. is not a user-specified loop variable. This is important because the
rewrite assumes that forward_input is passed through to its corresponding
output unchanged. This assumption is used in _rewrite_input_as_indexed_slices,
which depends on the exact gradient structure produced by the input's fanout.

This can yield a more efficient computation than using
_rewrite_output_as_tensor, since it preserves the IndexedSlices structure
instead of converting the IndexedSlices to a dense Tensor.

Args:
body_grad_graph: _WhileBodyGradFuncGraph.
grad_output_slices: IndexedSlices output of body_grad_graph.
forward_input: the corresponding Tensor input to the forward loop.
loop_vars: list of Tensors. The inputs to body_grad_graph.

Returns:
The new loop_vars to pass to body_grad_graph.


def chat(self, id):

        json = self.skype.conn("GET", "{0}/users/ME/conversations/{1}".format(self.skype.conn.msgsHost, id),
                               auth=SkypeConnection.Auth.RegToken, params={"view": "msnp24Equivalent"}).json()
        cls = SkypeSingleChat
        if "threadProperties" in json:
            info = self.skype.conn("GET", "{0}/threads/{1}".format(self.skype.conn.msgsHost, json.get("id")),
                                   auth=SkypeConnection.Auth.RegToken, params={"view": "msnp24Equivalent"}).json()
            json.update(info)
            cls = SkypeGroupChat
        return self.merge(cls.fromRaw(self.skype, json))

Get a single conversation by identifier.

Args:
id (str): single or group chat identifier


def get_sequence_sliding_window_properties(self, scale, window, representative_only=True):
    if representative_only:
        if (not self.representative_sequence):
            log.warning('{}: no representative sequence set, cannot get sequence properties'.format(self.id))
            return
        if (not self.representative_sequence.seq):
            log.warning('{}: representative sequence {} set, but no sequence stored. Cannot get sequence properties.'.format(self.id, self.representative_sequence.id))
            return
        self.representative_sequence.get_sliding_window_properties(scale=scale, window=window)
    if (not representative_only):
        for s in self.sequences:
            if (not s.seq):
                log.warning('{}: no sequence stored. Cannot get sequence properties.'.format(s.id))
                continue
            else:
                s.get_sliding_window_properties(scale=scale, window=window)

Run Biopython ProteinAnalysis with a sliding window to calculate a given property.
Results are stored in the protein's respective SeqProp objects at ``.letter_annotations``

Args:
scale (str): Scale name
window (int): Sliding window size
representative_only (bool): If analysis should only be run on the representative sequence


def range_dimension_tensor(self, name='range_dimension_tensor'):
    with self._name_scope(name):
        return self._range_dimension_tensor()

Dimension (in the sense of vector spaces) of the range of this operator.

Determined at runtime.

If this operator acts like the batch matrix `A` with
`A.shape = [B1,...,Bb, M, N]`, then this returns `M`.

Args:
name:  A name for this `Op`.

Returns:
`int32` `Tensor`


def create_trial_from_spec(spec, output_path, parser, **trial_kwargs):
    try:
        args = parser.parse_args(to_argv(spec))
    except SystemExit:
        raise TuneError('Error parsing args, see above message', spec)
    if ('resources_per_trial' in spec):
        trial_kwargs['resources'] = json_to_resources(spec['resources_per_trial'])
    return Trial(trainable_name=spec['run'], config=spec.get('config', {}), local_dir=os.path.join(args.local_dir, output_path), stopping_criterion=spec.get('stop', {}), checkpoint_freq=args.checkpoint_freq, checkpoint_at_end=args.checkpoint_at_end, keep_checkpoints_num=args.keep_checkpoints_num, checkpoint_score_attr=args.checkpoint_score_attr, export_formats=spec.get('export_formats', []), restore_path=spec.get('restore'), upload_dir=args.upload_dir, trial_name_creator=spec.get('trial_name_creator'), loggers=spec.get('loggers'), sync_function=spec.get('sync_function'), max_failures=args.max_failures, **trial_kwargs)

Creates a Trial object from parsing the spec.

Arguments:
spec (dict): A resolved experiment specification. Arguments should
The args here should correspond to the command line flags
in ray.tune.config_parser.
output_path (str); A specific output path within the local_dir.
Typically the name of the experiment.
parser (ArgumentParser): An argument parser object from
make_parser.
trial_kwargs: Extra keyword arguments used in instantiating the Trial.

Returns:
A trial object with corresponding parameters to the specification.


def from_text_files(cls, path, field, train, validation, test=None, bs=64, bptt=70, **kwargs):
    (trn_ds, val_ds, test_ds) = ConcatTextDataset.splits(path, text_field=field, train=train, validation=validation, test=test)
    return cls(path, field, trn_ds, val_ds, test_ds, bs, bptt, **kwargs)

Method used to instantiate a LanguageModelData object that can be used for a
supported nlp task.

Args:
path (str): the absolute path in which temporary model data will be saved
field (Field): torchtext field
train (str): file location of the training data
validation (str): file location of the validation data
test (str): file location of the testing data
bs (int): batch size to use
bptt (int): back propagation through time hyper-parameter
kwargs: other arguments

Returns:
a LanguageModelData instance, which most importantly, provides us the datasets for training,
validation, and testing

Note:
The train, validation, and test path can be pointed to any file (or folder) that contains a valid
text corpus.


def remove_file(profile, branch, file_path, commit_message=None):
    branch_sha = get_branch_sha(profile, branch)
    tree = get_files_in_branch(profile, branch_sha)
    new_tree = remove_file_from_tree(tree, file_path)
    data = trees.create_tree(profile, new_tree)
    sha = data.get('sha')
    if (not commit_message):
        commit_message = (('Deleted ' + file_path) + '.')
    parents = [branch_sha]
    commit_data = commits.create_commit(profile, commit_message, sha, parents)
    commit_sha = commit_data.get('sha')
    ref_data = refs.update_ref(profile, ('heads/' + branch), commit_sha)
    return ref_data

Remove a file from a branch.

Args:

profile
A profile generated from ``simplygithub.authentication.profile``.
Such profiles tell this module (i) the ``repo`` to connect to,
and (ii) the ``token`` to connect with.

branch
The name of a branch.

file_path
The path of the file to delete.

commit_message
A commit message to give to the commit.

Returns:
A dict with data about the branch's new ref (it includes the new SHA
the branch's HEAD points to, after committing the new file).


def resolve_topic(topic):
    try:
        (module_name, _, class_name) = topic.partition('
        module = importlib.import_module(module_name)
    except ImportError as e:
        raise TopicResolutionError('{}: {}'.format(topic, e))
    try:
        cls = resolve_attr(module, class_name)
    except AttributeError as e:
        raise TopicResolutionError('{}: {}'.format(topic, e))
    return cls

Return class described by given topic.

Args:
topic: A string describing a class.

Returns:
A class.

Raises:
TopicResolutionError: If there is no such class.


def copy_function(func, name=None):

    code = func.__code__
    newname = name or func.__name__
    newcode = CodeType(
        code.co_argcount,
        code.co_kwonlyargcount,
        code.co_nlocals,
        code.co_stacksize,
        code.co_flags,
        code.co_code,
        code.co_consts,
        code.co_names,
        code.co_varnames,
        code.co_filename,
        newname,
        code.co_firstlineno,
        code.co_lnotab,
        code.co_freevars,
        code.co_cellvars,
    )
    newfunc = FunctionType(
        newcode,
        func.__globals__,
        newname,
        func.__defaults__,
        func.__closure__,
    )
    newfunc.__dict__.update(func.__dict__)
    return newfunc

Copy a function object with different name.

Args:
func (function): Function to be copied.
name (string, optional): Name of the new function.
If not spacified, the same name of `func` will be used.

Returns:
newfunc (function): New function with different name.


def __init__(self, experimenter=None, exp_type=None):

        super().__init__()
        self.experimenter = experimenter
        self.exp_type = exp_type

Create a ExperimenterMultipartHeader with the parameters below.

Args:
experimenter: Experimenter ID which takes the same form as in
struct ofp_experimenter_header (
:class:`~pyof.v0x04.symmetric.experimenter.ExperimenterHeader`)
exp_type: Experimenter defined.


def get_symmetry_operations(self, cartesian=False):
    (rotation, translation) = self._get_symmetry()
    symmops = []
    mat = self._structure.lattice.matrix.T
    invmat = np.linalg.inv(mat)
    for (rot, trans) in zip(rotation, translation):
        if cartesian:
            rot = np.dot(mat, np.dot(rot, invmat))
            trans = np.dot(trans, self._structure.lattice.matrix)
        op = SymmOp.from_rotation_and_translation(rot, trans)
        symmops.append(op)
    return symmops

Return symmetry operations as a list of SymmOp objects.
By default returns fractional coord symmops.
But cartesian can be returned too.

Returns:
([SymmOp]): List of symmetry operations.


def bloom_gelu_forward(x: torch.Tensor) -> torch.Tensor:
    return x * 0.5 * (1.0 + torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)))

Custom bias GELU function. Adapted from Megatron-DeepSpeed code. Here we use a simple implementation (inference) to
make the model jitable.

Args:
x (`torch.tensor`):
input hidden states


def add_listener(self, callback, event_type=None):

        listener_uid = uuid4()

        self.listeners.append(
            {
                'uid': listener_uid,
                'callback': callback,
                'event_type': event_type
            }
        )
        return listener_uid

Add a listener that will send a callback when the client recieves
an event.

Args:
callback (func(roomchunk)): Callback called when an event arrives.
event_type (str): The event_type to filter for.

Returns:
uuid.UUID: Unique id of the listener, can be used to identify the listener.


def find_certs() -> str:
    bundle = path.realpath(path.dirname(httplib2.CA_CERTS))
    if (not bundle.startswith(path.dirname(httplib2.__file__))):
        return bundle
    for (platform, files) in PLATFORM_FILES.items():
        if sys.platform.startswith(platform):
            for cert_file in files:
                if path.exists(cert_file):
                    return cert_file
    if path.exists(getenv('CURL_CA_BUNDLE', '')):
        return getenv('CURL_CA_BUNDLE')
    if ALLOW_FALLBACK:
        warnings.warn('No system certs detected, falling back to bundled', RuntimeWarning)
        return httplib2.CA_CERTS
    else:
        raise RuntimeError('No system certs detected!')

Find suitable certificates for ``httplib2``.

Warning:
The default behaviour is to fall back to the bundled certificates when
no system certificates can be found.  If you're packaging ``jnrbase``
*please* set ``ALLOW_FALLBACK`` to ``False`` to disable this very much
unwanted behaviour, but please maintain the option so that downstream
users can inspect the configuration easily.

See also: :pypi:`httplib2`

Returns:
Path to SSL certificates
Raises:
RuntimeError: When no suitable certificates are found


def find_wells_without_curve(self, mnemonic, alias=None):
    return Project([w for w in self if (w.get_curve(mnemonic, alias=alias) is None)])

Returns a new Project with only the wells which DO NOT have the named curve.

Args:
menmonic (str): the name of the curve to look for.
alias (dict): a welly alias dictionary.

Returns:
project.


def __init__(self, path, auto_reboot_args=None, keep_explorer=False, add_all_devices=False):

        super(SimpleTestResult, self).__init__()
        self.path = path
        self.auto_reboot_args = auto_reboot_args
        self.result = json.load(open(self.path, 'r'))
        self.log_handler = None
        self.started = None
        self.keep_explorer = keep_explorer
        self.add_all_devices = add_all_devices
        SimpleTestResult.executions += 1
        logger.info('Initial state is %s', json.dumps(self.result, indent=2))

Record test results in json file

Args:
path (str): File path to record the results
auto_reboot (bool): Whether reboot when harness die


def group_sub_entities(self, entities: List[dict]) -> dict:
    entity = entities[0]['entity'].split('-', 1)[-1]
    scores = np.nanmean([entity['score'] for entity in entities])
    tokens = [entity['word'] for entity in entities]
    entity_group = {'entity_group': entity, 'score': np.mean(scores), 'word': self.tokenizer.convert_tokens_to_string(tokens), 'start': entities[0]['start'], 'end': entities[-1]['end']}
    return entity_group

Group together the adjacent tokens with the same entity predicted.

Args:
entities (`dict`): The entities predicted by the pipeline.


def assertRaisesWithPredicateMatch(self, exception_type, expected_err_re_or_predicate):
    if callable(expected_err_re_or_predicate):
        predicate = expected_err_re_or_predicate
    else:

        def predicate(e):
            if isinstance(e, errors.OpError):
                e = cast(errors.OpError, e)
                err_str = cast(str, e.message)
                op = e.op
            else:
                err_str = str(e)
                op = None
            while op is not None:
                err_str += '\nCaused by: ' + op.name
                op = op._original_op
            logging.info("Searching within error strings: '%s' within '%s'", expected_err_re_or_predicate, err_str)
            return re.search(expected_err_re_or_predicate, err_str)
    try:
        yield
        self.fail(exception_type.__name__ + ' not raised')
    except Exception as e:
        if not isinstance(e, exception_type) or not predicate(e):
            raise AssertionError('Exception of type %s: %s' % (str(type(e)), str(e)))

Returns a context manager to enclose code expected to raise an exception.

If the exception is an OpError, the op stack is also included in the message
predicate search.

Args:
exception_type: The expected type of exception that should be raised.
expected_err_re_or_predicate: If this is callable, it should be a function
of one argument that inspects the passed-in exception and returns True
(success) or False (please fail the test). Otherwise, the error message
is expected to match this regular expression partially.

Returns:
A context manager to surround code that is expected to raise an
exception.


def __call__(self, request: Union[Chunk, List[Chunk]], *args, **kwargs) -> List[Tuple[Chunk, Dict[str, Any]]]:
    requests = request if isinstance(request, list) else [request]
    query = self.vector_search_parameters.format_query(requests)
    if self.log_query:
        _LOGGER.info('Executing query %s', query)
    query_job = self.client.query(query)
    results = query_job.result()
    results_by_id = {}
    for result_row in results:
        result_dict = dict(result_row.items())
        results_by_id[result_row.id] = result_dict
    response = []
    for chunk in requests:
        result_dict = results_by_id.get(chunk.id, {})
        response.append((chunk, result_dict))
    return response

Process request(s) using BigQuery vector search.

Args:
request: Single Chunk with embedding or list of Chunk's with
embeddings to process

Returns:
Chunk(s) where chunk.metadata['enrichment_output'] contains the
data retrieved via BigQuery VECTOR_SEARCH.


def min_count(self, n=1):

    word_count = {w:c for w,c in iteritems(self.word_count) if c >= n}
    return CountedVocabulary(word_count=word_count)

Returns a vocabulary after eliminating the words that appear < `n`.

Args:
n (integer): specifies the minimum word frequency allowed.


def rename(self, source_file_names, destination_file_names):
    raise NotImplementedError

Rename the files at the source list to the destination list.
Source and destination lists should be of the same size.

Args:
source_file_names: List of file paths that need to be moved
destination_file_names: List of destination_file_names for the files

Raises:
``BeamIOError``: if any of the rename operations fail


def _init_metadata_service(self, version):

        metadata_cfg = self._load_config_section(CONFIG_METADATA_SECTION)
        self._token_metadata = metadata_cfg[CONFIG_TOKEN]
        proto = metadata_cfg[CONFIG_PROTOCOL]
        host = metadata_cfg[CONFIG_HOST]

        self._metadata = MetadataService(host, version)
        self._metadata.base_protocol = proto
        self._metadata.set_auth(self._token_metadata)

Method to initialize the Metadata Service from the config data

Args:
version (string): Version of Boss API to use.

Returns:
None

Raises:
(KeyError): if given invalid version.


def get_op_name(tensor_name):
    if not tensor_name:
        raise ValueError(f'Tensor name cannot be empty or None. Received: {tensor_name}.')
    if tensor_name.startswith('^'):
        tensor_name = tensor_name[1:]
    if ':' in tensor_name:
        op_name, _ = tensor_name.split(':')
        return op_name
    return tensor_name

Extract the Op name from a Tensor name.

The Op name is everything before a colon, if present,
not including any ^ prefix denoting a control dependency.

Args:
tensor_name: the full name of a Tensor in the graph.
Returns:
The name of the Op of which the given Tensor is an output.
Raises:
ValueError: if tensor_name is None or empty.


def job_history(backend):

    year = widgets.Output(layout=widgets.Layout(display='flex-inline',
                                                align_items='center',
                                                min_height='400px'))

    month = widgets.Output(layout=widgets.Layout(display='flex-inline',
                                                 align_items='center',
                                                 min_height='400px'))

    week = widgets.Output(layout=widgets.Layout(display='flex-inline',
                                                align_items='center',
                                                min_height='400px'))

    tabs = widgets.Tab(layout=widgets.Layout(max_height='620px'))
    tabs.children = [year, month, week]
    tabs.set_title(0, 'Year')
    tabs.set_title(1, 'Month')
    tabs.set_title(2, 'Week')
    tabs.selected_index = 1

    _build_job_history(tabs, backend)
    return tabs

Widget for displaying job history

Args:
backend (IBMQbackend): The backend.

Returns:
Tab: A tab widget for history images.


def create(cls, **kwargs):

        try:
            return cls.add(cls.new(**kwargs))
        except:
            cls.session.rollback()
            raise

Initializes a new instance, adds it to the db and commits
the transaction.

Args:

**kwargs: The keyword arguments for the init constructor.

Examples:

>>> user = User.create(name="Vicky", email="vicky@h.com")
>>> user.id
35


def getSwarmModelParams(modelID):
    cjDAO = ClientJobsDAO.get()
    (jobID, description) = cjDAO.modelsGetFields(modelID, ['jobId', 'genDescription'])
    (baseDescription,) = cjDAO.jobGetFields(jobID, ['genBaseDescription'])
    descriptionDirectory = tempfile.mkdtemp()
    try:
        baseDescriptionFilePath = os.path.join(descriptionDirectory, 'base.py')
        with open(baseDescriptionFilePath, mode='wb') as f:
            f.write(baseDescription)
        descriptionFilePath = os.path.join(descriptionDirectory, 'description.py')
        with open(descriptionFilePath, mode='wb') as f:
            f.write(description)
        expIface = helpers.getExperimentDescriptionInterfaceFromModule(helpers.loadExperimentDescriptionScriptFromDir(descriptionDirectory))
        return json.dumps(dict(modelConfig=expIface.getModelDescription(), inferenceArgs=expIface.getModelControl().get('inferenceArgs', None)))
    finally:
        shutil.rmtree(descriptionDirectory, ignore_errors=True)

Retrieve the Engine-level model params from a Swarm model

Args:
modelID - Engine-level model ID of the Swarm model

Returns:
JSON-encoded string containing Model Params


def close(self):
    if self._session and (not self._closed):
        self._closed = True
        tf_session.TF_CloseSession(self._session)

Closes this session.

Calling this method frees all resources associated with the session.

Raises:
tf.errors.OpError: Or one of its subclasses if an error occurs while
closing the TensorFlow session.


def y_score(estimator, X):

    try:
        y = estimator.predict_proba(X)
        return y[:, 1]
    except(AttributeError):
        return estimator.decision_function(X)

Score examples from a new matrix X
Args:
estimator: an sklearn estimator object
X: design matrix with the same features that the estimator was trained on

Returns: a vector of scores of the same length as X

Note that estimator.predict_proba is preferred but when unavailable
(e.g. SVM without probability calibration) decision_function is used.


def has_inf_or_nan(datum, tensor):
    _ = datum
    if isinstance(tensor, InconvertibleTensorProto):
        return False
    elif np.issubdtype(tensor.dtype, np.floating) or np.issubdtype(tensor.dtype, np.complexfloating) or np.issubdtype(tensor.dtype, np.integer):
        return np.any(np.isnan(tensor)) or np.any(np.isinf(tensor))
    else:
        return False

A predicate for whether a tensor consists of any bad numerical values.

This predicate is common enough to merit definition in this module.
Bad numerical values include `nan`s and `inf`s.
The signature of this function follows the requirement of the method
`DebugDumpDir.find()`.

Args:
datum: (`DebugTensorDatum`) Datum metadata.
tensor: (`numpy.ndarray` or None) Value of the tensor. None represents
an uninitialized tensor.

Returns:
(`bool`) True if and only if tensor consists of any nan or inf values.


def _handle_port_request(self, client_data, writer):

        try:
            pid = int(client_data)
        except ValueError as error:
            self._client_request_errors += 1
            log.warning('Could not parse request: %s', error)
            return

        log.info('Request on behalf of pid %d.', pid)
        log.info('cmdline: %s', _get_process_command_line(pid))

        if not _should_allocate_port(pid):
            self._denied_allocations += 1
            return

        port = self._port_pool.get_port_for_process(pid)
        if port > 0:
            self._total_allocations += 1
            writer.write('{:d}\n'.format(port).encode('utf-8'))
            log.debug('Allocated port %d to pid %d', port, pid)
        else:
            self._denied_allocations += 1

Given a port request body, parse it and respond appropriately.

Args:
client_data: The request bytes from the client.
writer: The asyncio Writer for the response to be written to.


def GetShadowMap(self, since=None):
    return ShadowUpdateGetter().GetUpdates(self._GetClient(), self.conf['bucket'], self.conf['shadow_object'], since)

Return the shadow map from this source.

Args:
since: Get data only changed since this timestamp (inclusive) or None
for all data.

Returns:
instance of shadow.ShadowMap


def _write_session(self):
    base_name = ('%ssession' % self._product_accronym.lower())
    filename = ('%s%s.py' % (self._class_prefix.lower(), base_name))
    override_content = self._extract_override_content(base_name)
    self.write(destination=self.output_directory, filename=filename, template_name='session.py.tpl', version=self.api_version, product_accronym=self._product_accronym, class_prefix=self._class_prefix, root_api=self.api_root, api_prefix=self.api_prefix, override_content=override_content, header=self.header_content)

Write SDK session file

Args:
version (str): the version of the server


def input_waiting(self):
    buf = array.array('I', [0])
    try:
        fcntl.ioctl(self._fd, termios.TIOCINQ, buf, True)
    except OSError as e:
        raise SerialError(e.errno, ('Querying input waiting: ' + e.strerror))
    return buf[0]

Query the number of bytes waiting to be read from the serial port.

Returns:
int: number of bytes waiting to be read.

Raises:
SerialError: if an I/O or OS error occurs.


def loss(logits, labels):

  labels = tf.to_int64(labels)
  cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
      logits=logits, labels=labels, name='xentropy')
  return tf.reduce_mean(cross_entropy, name='xentropy_mean')

Calculates the loss from the logits and the labels.

Args:
logits: Logits tensor, float - [batch_size, NUM_CLASSES].
labels: Labels tensor, int32 - [batch_size].
Returns:
loss: Loss tensor of type float.


def path(self, goal):

        if goal == self.name:
            return [self]

        if goal not in self.routes:
            raise ValueError("Unknown '{0}'".format(goal))

        obj = self
        path = [obj]
        while True:
            obj = obj.routes[goal].direction
            path.append(obj)
            if obj.name == goal:
                break
        return path

Get the shortest way between two nodes of the graph

Args:
goal (str): Name of the targeted node
Return:
list of Node


def _lookup_model(cls, kind, default_model=None):

    modelclass = cls._kind_map.get(kind, default_model)
    if modelclass is None:
      raise KindError(
          "No model class found for kind '%s'. Did you forget to import it?" %
          kind)
    return modelclass

Get the model class for the kind.

Args:
kind: A string representing the name of the kind to lookup.
default_model: The model class to use if the kind can't be found.

Returns:
The model class for the requested kind.
Raises:
KindError: The kind was not found and no default_model was provided.


def vectorize(density_matrix, method='col'):
    density_matrix = np.array(density_matrix)
    if (method == 'col'):
        return density_matrix.flatten(order='F')
    elif (method == 'row'):
        return density_matrix.flatten(order='C')
    elif (method in ['pauli', 'pauli_weights']):
        num = int(np.log2(len(density_matrix)))
        if (len(density_matrix) != (2 ** num)):
            raise Exception('Input state must be n-qubit state')
        if (method == 'pauli_weights'):
            pgroup = pauli_group(num, case='weight')
        else:
            pgroup = pauli_group(num, case='tensor')
        vals = [np.trace(np.dot(p.to_matrix(), density_matrix)) for p in pgroup]
        return np.array(vals)
    return None

Flatten an operator to a vector in a specified basis.

Args:
density_matrix (ndarray): a density matrix.
method (str): the method of vectorization. Allowed values are
- 'col' (default) flattens to column-major vector.
- 'row' flattens to row-major vector.
- 'pauli'flattens in the n-qubit Pauli basis.
- 'pauli-weights': flattens in the n-qubit Pauli basis ordered by
weight.

Returns:
ndarray: the resulting vector.
Raises:
Exception: if input state is not a n-qubit state


def has_request(self, request):
    queue_item = QueueItem(request, Response(request.url))
    key = queue_item.get_hash()
    for status in QueueItem.STATUSES:
        if (key in self.__get_var(('items_' + status)).keys()):
            return True
    return False

Check if the given request already exists in the queue.

Args:
request (:class:`nyawc.http.Request`): The request to check.

Returns:
bool: True if already exists, False otherwise.


def command(self, cmd_name, callback, *args):
    cmd = JLinkCommand(cmd_name, args, callback)
    self._commands.put(cmd)

Run an asynchronous command.

Args:
cmd_name (int): The unique code for the command to execute.
callback (callable): The optional callback to run when the command finishes.
The signature should be callback(cmd_name, result, exception)
*args: Any arguments that are passed to the underlying command handler


def get_model(self, opt_fn, emb_sz, n_hid, n_layers, **kwargs):
    m = get_language_model(self.nt, emb_sz, n_hid, n_layers, self.pad_idx, **kwargs)
    model = SingleModel(to_gpu(m))
    return RNN_Learner(self, model, opt_fn=opt_fn)

Method returns a RNN_Learner object, that wraps an instance of the RNN_Encoder module.

Args:
opt_fn (Optimizer): the torch optimizer function to use
emb_sz (int): embedding size
n_hid (int): number of hidden inputs
n_layers (int): number of hidden layers
kwargs: other arguments

Returns:
An instance of the RNN_Learner class.


def prefetch_users(persistent_course_grades):

        users = User.objects.filter(
            id__in=[grade.user_id for grade in persistent_course_grades]
        )
        return {
            user.id: user for user in users
        }

Prefetch Users from the list of user_ids present in the persistent_course_grades.

Arguments:
persistent_course_grades (list): A list of PersistentCourseGrade.

Returns:
(dict): A dictionary containing user_id to user mapping.


def Deserialize(self, reader):

        self.name = reader.ReadVarString().decode('utf-8')
        self.symbol = reader.ReadVarString().decode('utf-8')
        self.decimals = reader.ReadUInt8()

Read serialized data from byte stream
Args:
reader (neocore.IO.BinaryReader): reader to read byte data from


def from_file(cls, filename, directory=None,
                  format=None, engine=None, encoding=File._encoding):

        filepath = os.path.join(directory or '', filename)
        if encoding is None:
            encoding = locale.getpreferredencoding()
        with io.open(filepath, encoding=encoding) as fd:
            source = fd.read()
        return cls(source, filename, directory, format, engine, encoding)

Return an instance with the source string read from the given file.

Args:
filename: Filename for loading/saving the source.
directory: (Sub)directory for source loading/saving and rendering.
format: Rendering output format (``'pdf'``, ``'png'``, ...).
engine: Layout command used (``'dot'``, ``'neato'``, ...).
encoding: Encoding for loading/saving the source.


def train(self, X_train, Y_train, X_test, Y_test):
    while True:
        print(1)
        time.sleep(1)
        if (random.randint(0, 9) >= 5):
            break

Train and validate the LR on a train and test dataset

Args:
X_train (np.array): Training data
Y_train (np.array): Training labels
X_test (np.array): Test data
Y_test (np.array): Test labels


def stitch_map(tiles, width, height, bbox, dpi):
    size = (int((width * dpi_to_dpmm(dpi))), int((height * dpi_to_dpmm(dpi))))
    background = Image.new('RGBA', size, (255, 255, 255))
    for layer in tiles:
        layer_img = Image.new('RGBA', size)
        for ((x, y), tile_path) in layer.items():
            tile = Image.open(tile_path)
            layer_img.paste(tile, (((x - bbox.min.x) * TILE_SIZE), ((y - bbox.min.y) * TILE_SIZE)))
        background = Image.alpha_composite(background, layer_img)
    add_scales_bar(background, bbox)
    return background.convert('RGB')

Merge tiles together into one image.

Args:
tiles (list of dict of file): tiles for each layer
width (float): page width in mm
height (height): page height in mm
dpi (dpi): resolution in dots per inch

Returns:
PIL.Image: merged map.


def subproc_call(cmd, timeout=None):
    try:
        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True, timeout=timeout)
        return (output, 0)
    except subprocess.TimeoutExpired as e:
        logger.warn("Command '{}' timeout!".format(cmd))
        logger.warn(e.output.decode('utf-8'))
        return (e.output, (- 1))
    except subprocess.CalledProcessError as e:
        logger.warn("Command '{}' failed, return code={}".format(cmd, e.returncode))
        logger.warn(e.output.decode('utf-8'))
        return (e.output, e.returncode)
    except Exception:
        logger.warn("Command '{}' failed to run.".format(cmd))
        return ('', (- 2))

Execute a command with timeout, and return STDOUT and STDERR

Args:
cmd(str): the command to execute.
timeout(float): timeout in seconds.

Returns:
output(bytes), retcode(int). If timeout, retcode is -1.


def get_variant_type(variant_source):

    file_type = get_file_type(variant_source)
    variant_type = 'sv'
    if file_type == 'vcf':
        variants = VCF(variant_source)
    elif file_type == 'gemini':
        variants = GeminiQuery(variant_source)
        gemini_query = "SELECT * from variants"
        variants.run(gemini_query)

    for i,variant in enumerate(variants):
        if file_type == 'vcf':
            if variant.is_snp:
                variant_type = 'snv'
        elif file_type == 'gemini':
            if variant['type'] == 'snp':
                variant_type = 'snv'

        if i > 1000:
            break

    return variant_type

Try to find out what type of variants that exists in a variant source

Args:
variant_source (str): Path to variant source
source_mode (str): 'vcf' or 'gemini'

Returns:
variant_type (str): 'sv' or 'snv'


async def client_event_handler(self, client_id, event_tuple, user_data):
    (conn_string, event_name, event) = event_tuple
    if (event_name == 'report'):
        report = event.serialize()
        report['encoded_report'] = base64.b64encode(report['encoded_report'])
        msg_payload = dict(connection_string=conn_string, serialized_report=report)
        msg_name = OPERATIONS.NOTIFY_REPORT
    elif (event_name == 'trace'):
        encoded_payload = base64.b64encode(event)
        msg_payload = dict(connection_string=conn_string, payload=encoded_payload)
        msg_name = OPERATIONS.NOTIFY_TRACE
    elif (event_name == 'progress'):
        msg_payload = dict(connection_string=conn_string, operation=event.get('operation'), done_count=event.get('finished'), total_count=event.get('total'))
        msg_name = OPERATIONS.NOTIFY_PROGRESS
    elif (event_name == 'device_seen'):
        msg_payload = event
        msg_name = OPERATIONS.NOTIFY_DEVICE_FOUND
    elif (event_name == 'broadcast'):
        report = event.serialize()
        report['encoded_report'] = base64.b64encode(report['encoded_report'])
        msg_payload = dict(connection_string=conn_string, serialized_report=report)
        msg_name = OPERATIONS.NOTIFY_BROADCAST
    else:
        self._logger.debug('Not forwarding unknown event over websockets: %s', event_tuple)
        return
    try:
        self._logger.debug('Sending event %s: %s', msg_name, msg_payload)
        (await self.server.send_event(user_data, msg_name, msg_payload))
    except websockets.exceptions.ConnectionClosed:
        self._logger.debug('Could not send notification because connection was closed for client %s', client_id)

Forward an event on behalf of a client.

This method is called by StandardDeviceServer when it has an event that
should be sent to a client.

Args:
client_id (str): The client that we should send this event to
event_tuple (tuple): The conn_string, event_name and event
object passed from the call to notify_event.
user_data (object): The user data passed in the call to
:meth:`setup_client`.


def _write_session(self):

        base_name = "%ssession" % self._product_accronym.lower()
        filename = "%s%s.py" % (self._class_prefix.lower(), base_name)
        override_content = self._extract_override_content(base_name)

        self.write(destination=self.output_directory, filename=filename, template_name="session.py.tpl",
                   version=self.api_version,
                   product_accronym=self._product_accronym,
                   class_prefix=self._class_prefix,
                   root_api=self.api_root,
                   api_prefix=self.api_prefix,
                   override_content=override_content,
                   header=self.header_content)

Write SDK session file

Args:
version (str): the version of the server


def get_port_from_port_server(portserver_address, pid=None):
    if (not portserver_address):
        return None
    if (portserver_address[0] == '@'):
        portserver_address = ('\x00' + portserver_address[1:])
    if (pid is None):
        pid = os.getpid()
    try:
        if hasattr(socket, 'AF_UNIX'):
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(portserver_address)
            sock.sendall(('%d\n' % pid).encode('ascii'))
            buf = sock.recv(1024)
        finally:
            sock.close()
    except socket.error as e:
        print('Socket error when connecting to portserver:', e, file=sys.stderr)
        return None
    try:
        port = int(buf.split(b'\n')[0])
    except ValueError:
        print('Portserver failed to find a port.', file=sys.stderr)
        return None
    _owned_ports.add(port)
    return port

Request a free a port from a system-wide portserver.

This follows a very simple portserver protocol:
The request consists of our pid (in ASCII) followed by a newline.
The response is a port number and a newline, 0 on failure.

This function is an implementation detail of pick_unused_port().
It should not normally be called by code outside of this module.

Args:
portserver_address: The address (path) of a unix domain socket
with which to connect to the portserver.  A leading '@'
character indicates an address in the "abstract namespace."
On systems without socket.AF_UNIX, this is an AF_INET address.
pid: The PID to tell the portserver to associate the reservation with.
If None, the current process's PID is used.

Returns:
The port number on success or None on failure.


def __init__(self, interface, logger, base_configs=None):
    raise NotImplementedError('Base class should not be called directly!')

The constructor for the Sniffer. It constructs a sniffer and
configures it to be ready for capture.

Args:
interface: A string specifying the interface used to configure the
sniffer.
logger: Mobly logger object.
base_configs: A dictionary containing baseline configurations of the
sniffer. These can be overridden when staring a capture. The
keys are specified by Sniffer.CONFIG_KEY_*.

Returns:
self: A configured sniffer.

Raises:
InvalidDataError: if the config_path is invalid.
NoPermissionError: if an error occurs while configuring the
sniffer.


def flatten_per_replica_values(distribution_strategy, per_replica_values):
    return [e for flattened in nest.flatten(per_replica_values) for e in distribution_strategy.unwrap(flattened)]

Unwraps and flattens a nest of PerReplica parameters.

PerReplica values have one value associated with each device. Each entry in
the PerReplica dict has a device `key` and the corresponding value on the
device as the `value`. In this function we take a PerReplica value or a list
of PerReplica values and return all the values in the PerReplica dict.

Args:
distribution_strategy: DistributionStrategy used to distribute training and
validation.
per_replica_values: List of PerReplica object or a single PerReplica object.

Returns:
List of values of all the PerReplica objects.


def rename_style(self, old_name, new_name):
    if (old_name not in self.styles):
        raise KeyError(('Style %r not found' % old_name))
    if (new_name in self.styles):
        raise ValueError(('There is already a style called %r' % new_name))
    if (not is_valid_field_content(new_name)):
        raise ValueError(('%r is not a valid name' % new_name))
    self.styles[new_name] = self.styles[old_name]
    del self.styles[old_name]
    for line in self:
        if (line.style == old_name):
            line.style = new_name

Rename a style, including references to it.

Arguments:
old_name (str): Style to be renamed.
new_name (str): New name for the style (must be unused).

Raises:
KeyError: No style named old_name.
ValueError: new_name is not a legal name (cannot use commas)
or new_name is taken.


def ssh(cmd=''):

    with settings(warn_only=True):
        local('ssh -A -o StrictHostKeyChecking=no -i "%s" %s@%s "%s"' % (
            env.key_filename, env.user, env.host, cmd))

SSH into the server(s) (sequentially if more than one)

Args:
cmd (str) ='': Command to run on the server


def encode_corpus(self, corpus, output_path):

        out_container = containers.Container(output_path)
        out_container.open()

        for utterance in corpus.utterances.values():
            data = self.encode_utterance(utterance, corpus=corpus)
            out_container.set(utterance.idx, data)

        out_container.close()
        return out_container

Encode all utterances of the given corpus and store them in a :class:`audiomate.container.Container`.

Args:
corpus (Corpus): The corpus to process.
output_path (str): The path to store the container with the encoded data.

Returns:
Container: The container with the encoded data.


def intersect(df, other, index=False, keep='first'):

    validate_set_ops(df, other)
    if index:
        df_reset_index = df.reset_index()
        other_reset_index = other.reset_index()
        index_cols = [col for col in df_reset_index.columns if col not in df.columns]
        df_index_names = df.index.names
        return_df = (pd.merge(df_reset_index, other_reset_index,
                              how='inner',
                              left_on=df_reset_index.columns.values.tolist(),
                              right_on=df_reset_index.columns.values.tolist())
                     .set_index(index_cols))
        return_df.index.names = df_index_names
        return_df = return_df.drop_duplicates(keep=keep)
        return return_df
    else:
        return_df = pd.merge(df, other,
                             how='inner',
                             left_on=df.columns.values.tolist(),
                             right_on=df.columns.values.tolist())
        return_df = return_df.drop_duplicates(keep=keep)
        return return_df

Returns rows that appear in both DataFrames.

Args:
df (pandas.DataFrame): data passed in through the pipe.
other (pandas.DataFrame): other DataFrame to use for set operation with
the first.

Kwargs:
index (bool): Boolean indicating whether to consider the pandas index
as part of the set operation (default `False`).
keep (str): Indicates which duplicate should be kept. Options are `'first'`
and `'last'`.