code
stringlengths
20
4.93k
docstring
stringlengths
33
1.27k
source
stringclasses
3 values
def readlines(self, n, echo=None): return [ self.until(b'\n', echo) for _ in range(n) ]
Read *n* lines from channel. Args: n(int): The number of lines to read. echo(bool): Whether to write the read data to stdout. Returns: list of bytes: *n* lines which include new line characters. Raises: EOFError: If the channel was closed before *n* lines were read.
juraj-google-style
def _preprocess_input(self, inputs, error_message, expected_nesting=1, dtype=None): if inputs is None: return None if hasattr(inputs, 'numpy'): inputs = inputs.numpy().tolist() valid = isinstance(inputs, list) current = inputs for _ in range(expected_nesting): if not valid or not current: break valid = valid and isinstance(current[0], list) current = current[0] if current else None if not valid: raise ValueError(error_message) return [np.array(item, dtype=dtype) for item in inputs]
Preprocess input by converting torch tensors to numpy arrays and validating structure. Args: inputs: The input to process error_message: Error message if validation fails expected_nesting: Expected nesting level (1 for points/labels, 2 for boxes) dtype: Optional data type for numpy array conversion Returns: Processed input as list of numpy arrays or None
github-repos
def _ScanVolumeSystemRoot(self, scan_context, scan_node, base_path_specs): if not scan_node or not scan_node.path_spec: raise errors.ScannerError('Invalid scan node.') if scan_node.type_indicator == definitions.TYPE_INDICATOR_APFS_CONTAINER: volume_identifiers = self._GetAPFSVolumeIdentifiers(scan_node) elif scan_node.type_indicator == definitions.TYPE_INDICATOR_VSHADOW: volume_identifiers = self._GetVSSStoreIdentifiers(scan_node) volume_identifiers.reverse() else: raise errors.ScannerError( 'Unsupported volume system type: {0:s}.'.format( scan_node.type_indicator)) for volume_identifier in volume_identifiers: location = '/{0:s}'.format(volume_identifier) sub_scan_node = scan_node.GetSubNodeByLocation(location) if not sub_scan_node: raise errors.ScannerError( 'Scan node missing for volume identifier: {0:s}.'.format( volume_identifier)) self._ScanVolume(scan_context, sub_scan_node, base_path_specs)
Scans a volume system root scan node for volume and file systems. Args: scan_context (SourceScannerContext): source scanner context. scan_node (SourceScanNode): volume system root scan node. base_path_specs (list[PathSpec]): file system base path specifications. Raises: ScannerError: if the scan node is invalid, the scan node type is not supported or if a sub scan node cannot be retrieved.
juraj-google-style
def _make_gh_link_node(app, rawtext, role, kind, api_type, id, options=None): url = ('%s/%s/%s' % (_BOKEH_GH, api_type, id)) options = (options or {}) set_classes(options) node = nodes.reference(rawtext, (kind + utils.unescape(id)), refuri=url, **options) return node
Return a link to a Bokeh Github resource. Args: app (Sphinx app) : current app rawtext (str) : text being replaced with link node. role (str) : role name kind (str) : resource type (issue, pull, etc.) api_type (str) : type for api link id : (str) : id of the resource to link to options (dict) : options dictionary passed to role function
codesearchnet
def __init__(self, baselines, scope='aggregated-baseline', summary_labels=()): self.baselines = dict() for name in sorted(baselines): self.baselines[name] = Baseline.from_spec( spec=baselines[name], kwargs=dict(summary_labels=summary_labels)) self.linear = Linear(size=1, bias=0.0, scope='prediction', summary_labels=summary_labels) super(AggregatedBaseline, self).__init__(scope, summary_labels)
Aggregated baseline. Args: baselines: Dict of per-state baseline specification dicts
juraj-google-style
def last_revision(self, mod: YangIdentifier) -> ModuleId: revs = [mn for mn in self.modules if mn[0] == mod] if not revs: raise ModuleNotRegistered(mod) return sorted(revs, key=lambda x: x[1])[-1]
Return the last revision of a module that's part of the data model. Args: mod: Name of a module or submodule. Raises: ModuleNotRegistered: If the module `mod` is not present in the data model.
juraj-google-style
def topological_nodes(self): return nx.lexicographical_topological_sort(self._multi_graph, key=(lambda x: str(x.qargs)))
Yield nodes in topological order. Returns: generator(DAGNode): node in topological order
codesearchnet
def rsolve(A, b, epsilon=_epsilon): r A = asarray(A, float) b = asarray(b, float) if A.shape[0] == 0: return zeros((A.shape[1],)) if A.shape[1] == 0: return zeros((0,)) try: x = lstsq(A, b, rcond=epsilon) r = sum(x[3] > epsilon) if r == 0: return zeros(A.shape[1]) return x[0] except (ValueError, LinAlgError) as e: warnings.warn(str(e), RuntimeWarning) return solve(A, b)
r"""Robust solve for the linear equations. Args: A (array_like): Coefficient matrix. b (array_like): Ordinate values. Returns: :class:`numpy.ndarray`: Solution ``x``.
juraj-google-style
def __init__(self, initial_learning_rate, decay_steps, end_learning_rate=0.0001, power=1.0, cycle=False, name=None): super(PolynomialDecay, self).__init__() self.initial_learning_rate = initial_learning_rate self.decay_steps = decay_steps self.end_learning_rate = end_learning_rate self.power = power self.cycle = cycle self.name = name
Applies a polynomial decay to the learning rate. Args: initial_learning_rate: A scalar `float32` or `float64` `Tensor` or a Python number. The initial learning rate. decay_steps: A scalar `int32` or `int64` `Tensor` or a Python number. Must be positive. See the decay computation above. end_learning_rate: A scalar `float32` or `float64` `Tensor` or a Python number. The minimal end learning rate. power: A scalar `float32` or `float64` `Tensor` or a Python number. The power of the polynomial. Defaults to linear, 1.0. cycle: A boolean, whether or not it should cycle beyond decay_steps. name: String. Optional name of the operation. Defaults to 'PolynomialDecay'.
github-repos
def calculate_dimensionality_of_site(bonded_structure, site_index, inc_vertices=False): def neighbours(comp_index): return [(s.index, s.jimage) for s in bonded_structure.get_connected_sites(comp_index)] def rank(vertices): if (len(vertices) == 0): return (- 1) elif (len(vertices) == 1): return 0 else: vertices = np.array(list(vertices)) return np.linalg.matrix_rank((vertices[1:] - vertices[0])) connected_sites = {i: neighbours(i) for i in range(bonded_structure.structure.num_sites)} seen_vertices = set() seen_comp_vertices = defaultdict(set) queue = [(site_index, (0, 0, 0))] while (len(queue) > 0): (comp_i, image_i) = queue.pop(0) if ((comp_i, image_i) in seen_vertices): continue seen_vertices.add((comp_i, image_i)) if (rank(seen_comp_vertices[comp_i].union({image_i})) > rank(seen_comp_vertices[comp_i])): seen_comp_vertices[comp_i].add(image_i) for (comp_j, image_j) in connected_sites[comp_i]: image_j = tuple(np.add(image_j, image_i)) if ((comp_j, image_j) in seen_vertices): continue if (rank(seen_comp_vertices[comp_j].union({image_j})) > rank(seen_comp_vertices[comp_j])): queue.append((comp_j, image_j)) if inc_vertices: return (rank(seen_comp_vertices[site_index]), list(seen_comp_vertices[site_index])) else: return rank(seen_comp_vertices[site_index])
Calculates the dimensionality of the component containing the given site. Implements directly the modified breadth-first-search algorithm described in Algorithm 1 of: P. Larsem, M. Pandey, M. Strange, K. W. Jacobsen, 2018, arXiv:1808.02114 Args: bonded_structure (StructureGraph): A structure with bonds, represented as a pymatgen structure graph. For example, generated using the CrystalNN.get_bonded_structure() method. site_index (int): The index of a site in the component of interest. inc_vertices (bool, optional): Whether to return the vertices (site images) of the component. Returns: (int or tuple): If inc_vertices is False, the dimensionality of the component will be returned as an int. If inc_vertices is true, the function will return a tuple of (dimensionality, vertices), where vertices is a list of tuples. E.g. [(0, 0, 0), (1, 1, 1)].
codesearchnet
class OrderedEnqueuer(SequenceEnqueuer): def __init__(self, sequence, use_multiprocessing=False, shuffle=False): super(OrderedEnqueuer, self).__init__(sequence, use_multiprocessing) self.shuffle = shuffle def _get_executor_init(self, workers): def pool_fn(seqs): pool = get_pool_class(True)(workers, initializer=init_pool_generator, initargs=(seqs, None, get_worker_id_queue())) _DATA_POOLS.add(pool) return pool return pool_fn def _wait_queue(self): while True: time.sleep(0.1) if self.queue.unfinished_tasks == 0 or self.stop_signal.is_set(): return def _run(self): sequence = list(range(len(self.sequence))) self._send_sequence() while True: if self.shuffle: random.shuffle(sequence) with closing(self.executor_fn(_SHARED_SEQUENCES)) as executor: for i in sequence: if self.stop_signal.is_set(): return self.queue.put(executor.apply_async(get_index, (self.uid, i)), block=True) self._wait_queue() if self.stop_signal.is_set(): return self.sequence.on_epoch_end() self._send_sequence() def get(self): while self.is_running(): try: inputs = self.queue.get(block=True, timeout=5).get() if self.is_running(): self.queue.task_done() if inputs is not None: yield inputs except queue.Empty: pass except Exception as e: self.stop() raise e
Builds a Enqueuer from a Sequence. Args: sequence: A `tf.keras.utils.data_utils.Sequence` object. use_multiprocessing: use multiprocessing if True, otherwise threading shuffle: whether to shuffle the data at the beginning of each epoch
github-repos
def _update_job_info(cls, job_dir): meta_file = os.path.join(job_dir, JOB_META_FILE) meta = parse_json(meta_file) if meta: logging.debug("Update job info for %s" % meta["job_id"]) JobRecord.objects \ .filter(job_id=meta["job_id"]) \ .update(end_time=timestamp2date(meta["end_time"]))
Update information for given job. Meta file will be loaded if exists, and the job information in in db backend will be updated. Args: job_dir (str): Directory path of the job. Return: Updated dict of job meta info
juraj-google-style
def _remove_subsequent_result_because_of_batch_failure(self, sig): batch = self._batches_by_txn_id[sig] seen = [] for txn in batch.transactions: txn_id = txn.header_signature for poss_successor in self._scheduled.copy(): if not self.is_transaction_in_schedule(poss_successor): continue if self._is_txn_to_replay(txn_id, poss_successor, seen): if self._txn_has_result(poss_successor): del self._txn_results[poss_successor] self._scheduled.remove(poss_successor) self._txns_available[poss_successor] = \ self._transactions[poss_successor] else: self._outstanding.add(poss_successor) seen.append(poss_successor)
Remove transactions from scheduled and txn_results for successors of txns in a failed batch. These transactions will now, or in the future be rescheduled in next_transaction; giving a replay ability. Args: sig (str): Transaction header signature
juraj-google-style
def check_valid(line0, line1): data = line0.strip().split(b' ') if (len(data) <= 2): return False try: map(float, data[2:]) except: return False return True
Check if a file is valid Glove format. Args: line0 (bytes): First line of the file line1 (bytes): Second line of the file Returns: boo: ``True`` if it is valid. ``False`` if it is invalid.
codesearchnet
def load_fasta_file(filename): with open(filename, "r") as handle: records = list(SeqIO.parse(handle, "fasta")) return records
Load a FASTA file and return the sequences as a list of SeqRecords Args: filename (str): Path to the FASTA file to load Returns: list: list of all sequences in the FASTA file as Biopython SeqRecord objects
juraj-google-style
def get_num_bytes(self, batch: Sequence[datatable.Frame]) -> int: return sum((sys.getsizeof(element) for element in batch))
Returns: The number of bytes of data for a batch.
github-repos
def is_not_negative() -> RuleChecker[Numeric]: def _checker(value: Numeric) -> RuleOutput: if value >= 0: return None else: return 'Value is a negative number.' return _checker
Checks if the provided numeric value IS NOT negative i.e. NOT positive (+) or zero (0). Returns: * None: if value >= 0 * Error message, otherwise
github-repos
def _merge_section(original, to_merge): if (not original): return (to_merge or '') if (not to_merge): return (original or '') try: index = (original.index(':') + 1) except ValueError: index = original.index('\n') name = original[:index].strip() section = '\n '.join((original[(index + 1):].lstrip(), to_merge[(index + 1):].lstrip())).rstrip() return '{name}\n {section}'.format(name=name, section=section)
Merge two sections together. Args: original: The source of header and initial section lines. to_merge: The source for the additional section lines to append. Returns: A new section string that uses the header of the original argument and the section lines from both.
codesearchnet
def job_stories(self, raw=False, limit=None): job_stories = self._get_stories('jobstories', limit) if raw: job_stories = [story.raw for story in job_stories] return job_stories
Returns list of item ids of latest Job stories Args: limit (int): specifies the number of stories to be returned. raw (bool): Flag to indicate whether to transform all objects into raw json. Returns: `list` object containing ids of Job stories.
juraj-google-style
def retweeted_tweet(self): retweet = tweet_embeds.get_retweeted_tweet(self) if (retweet is not None): try: return Tweet(retweet) except NotATweetError as nate: raise NotATweetError(('The retweet payload appears malformed.' + " Failed with '{}'".format(nate))) else: return None
The retweeted Tweet as a Tweet object If the Tweet is not a Retweet, return None If the Retweet payload cannot be loaded as a Tweet, this will raise a `NotATweetError` Returns: Tweet: A Tweet representing the retweeted status (or None) (see tweet_embeds.get_retweet, this is that value as a Tweet) Raises: NotATweetError: if retweeted tweet is malformed
codesearchnet
def FindModuleIdDefiningFlag(self, flagname, default=None): registered_flag = self.FlagDict().get(flagname) if registered_flag is None: return default for module_id, flags in six.iteritems(self.FlagsByModuleIdDict()): for flag in flags: if (flag.name == registered_flag.name and flag.short_name == registered_flag.short_name): return module_id return default
Return the ID of the module defining this flag, or default. Args: flagname: Name of the flag to lookup. default: Value to return if flagname is not defined. Defaults to None. Returns: The ID of the module which registered the flag with this name. If no such module exists (i.e. no flag with this name exists), we return default.
juraj-google-style
def add_recipe_folder(self, recipe_folder, whitelist=None): if (whitelist is not None): whitelist = set(whitelist) if (recipe_folder == ''): recipe_folder = '.' for yaml_file in [x for x in os.listdir(recipe_folder) if x.endswith('.yaml')]: if ((whitelist is not None) and (yaml_file not in whitelist)): continue recipe = RecipeObject.FromFile(os.path.join(recipe_folder, yaml_file), self._recipe_actions, self._recipe_resources) self._recipes[recipe.name] = recipe for ship_file in [x for x in os.listdir(recipe_folder) if x.endswith('.ship')]: if ((whitelist is not None) and (ship_file not in whitelist)): continue recipe = RecipeObject.FromArchive(os.path.join(recipe_folder, ship_file), self._recipe_actions, self._recipe_resources) self._recipes[recipe.name] = recipe
Add all recipes inside a folder to this RecipeManager with an optional whitelist. Args: recipe_folder (str): The path to the folder of recipes to add. whitelist (list): Only include files whose os.basename() matches something on the whitelist
codesearchnet
def getPagePixmap(doc, pno, matrix = None, colorspace = csRGB, clip = None, alpha = True): return doc[pno].getPixmap(matrix = matrix, colorspace = colorspace, clip = clip, alpha = alpha)
Create pixmap of document page by page number. Notes: Convenience function calling page.getPixmap. Args: pno: (int) page number matrix: Matrix for transformation (default: Identity). colorspace: (str/Colorspace) rgb, rgb, gray - case ignored, default csRGB. clip: (irect-like) restrict rendering to this area. alpha: (bool) include alpha channel
juraj-google-style
def from_epw_file(cls, epwfile, timestep=1): is_leap_year = False epw = EPW(epwfile) (direct_normal, diffuse_horizontal) = cls._get_data_collections(epw.direct_normal_radiation.values, epw.diffuse_horizontal_radiation.values, epw.metadata, 1, is_leap_year) if (timestep != 1): print((("Note: timesteps greater than 1 on epw-generated Wea's \n" + 'are suitable for thermal models but are not recommended \n') + 'for daylight models.')) direct_normal = direct_normal.interpolate_to_timestep(timestep) diffuse_horizontal = diffuse_horizontal.interpolate_to_timestep(timestep) sp = Sunpath.from_location(epw.location) for (i, dt) in enumerate(cls._get_datetimes(timestep, is_leap_year)): sun = sp.calculate_sun_from_date_time(dt) if (sun.altitude < 0): direct_normal[i] = 0 diffuse_horizontal[i] = 0 return cls(epw.location, direct_normal, diffuse_horizontal, timestep, is_leap_year)
Create a wea object using the solar irradiance values in an epw file. Args: epwfile: Full path to epw weather file. timestep: An optional integer to set the number of time steps per hour. Default is 1 for one value per hour. Note that this input will only do a linear interpolation over the data in the EPW file. While such linear interpolations are suitable for most thermal simulations, where thermal lag "smooths over" the effect of momentary increases in solar energy, it is not recommended for daylight simulations, where momentary increases in solar energy can mean the difference between glare and visual comfort.
codesearchnet
def delete(self, json=None): return self._call('delete', url=self.endpoint, json=json)
Send a DELETE request and return the JSON decoded result. Args: json (dict, optional): Object to encode and send in request. Returns: mixed: JSON decoded response data.
codesearchnet
def get_precursor_mz(exact_mass, precursor_type): d = {'[M-H]-': -1.007276, '[M+H]+': 1.007276, '[M+H-H2O]+': 1.007276 - ((1.007276 * 2) + 15.9949) } try: return exact_mass + d[precursor_type] except KeyError as e: print(e) return False
Calculate precursor mz based on exact mass and precursor type Args: exact_mass (float): exact mass of compound of interest precursor_type (str): Precursor type (currently only works with '[M-H]-', '[M+H]+' and '[M+H-H2O]+' Return: neutral mass of compound
juraj-google-style
def get_channel_info(self, id: str) -> Dict[(str, Any)]: return self._query(f'channels/{id}', 'GET')
Get a chanel's information by its id Args: id: snowflake id of the chanel Returns: Dictionary data for the chanel API object Example: { "id": "41771983423143937", "guild_id": "41771983423143937", "name": "general", "type": 0, "position": 6, "permission_overwrites": [], "topic": "24/7 chat about how to gank Mike #2", "last_message_id": "155117677105512449" }
codesearchnet
def forward_request(self, method, path=None, json=None, params=None, headers=None): error_trace = [] timeout = self.timeout backoff_cap = (NO_TIMEOUT_BACKOFF_CAP if (timeout is None) else (timeout / 2)) while ((timeout is None) or (timeout > 0)): connection = self.connection_pool.get_connection() start = time() try: response = connection.request(method=method, path=path, params=params, json=json, headers=headers, timeout=timeout, backoff_cap=backoff_cap) except ConnectionError as err: error_trace.append(err) continue else: return response.data finally: elapsed = (time() - start) if (timeout is not None): timeout -= elapsed raise TimeoutError(error_trace)
Makes HTTP requests to the configured nodes. Retries connection errors (e.g. DNS failures, refused connection, etc). A user may choose to retry other errors by catching the corresponding exceptions and retrying `forward_request`. Exponential backoff is implemented individually for each node. Backoff delays are expressed as timestamps stored on the object and they are not reset in between multiple function calls. Times out when `self.timeout` is expired, if not `None`. Args: method (str): HTTP method name (e.g.: ``'GET'``). path (str): Path to be appended to the base url of a node. E.g.: ``'/transactions'``). json (dict): Payload to be sent with the HTTP request. params (dict)): Dictionary of URL (query) parameters. headers (dict): Optional headers to pass to the request. Returns: dict: Result of :meth:`requests.models.Response.json`
codesearchnet
def ParseNolintSuppressions(filename, raw_line, linenum, error): matched = Search('\\bNOLINT(NEXTLINE)?\\b(\\([^)]+\\))?', raw_line) if matched: if matched.group(1): suppressed_line = (linenum + 1) else: suppressed_line = linenum category = matched.group(2) if (category in (None, '(*)')): _error_suppressions.setdefault(None, set()).add(suppressed_line) elif (category.startswith('(') and category.endswith(')')): category = category[1:(- 1)] if (category in _ERROR_CATEGORIES): _error_suppressions.setdefault(category, set()).add(suppressed_line) elif (category not in _LEGACY_ERROR_CATEGORIES): error(filename, linenum, 'readability/nolint', 5, ('Unknown NOLINT error category: %s' % category))
Updates the global list of line error-suppressions. Parses any NOLINT comments on the current line, updating the global error_suppressions store. Reports an error if the NOLINT comment was malformed. Args: filename: str, the name of the input file. raw_line: str, the line of input text, with comments. linenum: int, the number of the current line. error: function, an error handler.
codesearchnet
def _catch_errors(a_func, to_catch): def inner(*args, **kwargs): 'Wraps specified exceptions' try: return a_func(*args, **kwargs) except tuple(to_catch) as exception: utils.raise_with_traceback(gax.errors.create_error('RPC failed', cause=exception)) return inner
Updates a_func to wrap exceptions with GaxError Args: a_func (callable): A callable. to_catch (list[Exception]): Configures the exceptions to wrap. Returns: Callable: A function that will wrap certain exceptions with GaxError
codesearchnet
def set_shard_dimensions(self, shard_dimensions): if len(shard_dimensions) != self.number_of_tuple_elements: raise ValueError(f'shard_dimensions is {str(shard_dimensions)}, but must be a list of length {self.number_of_tuple_elements}') for policy, dimension in zip(self._sharding_policies, shard_dimensions): policy.set_shard_dimension(dimension) self._validate()
Sets the shard_dimension of each element of the queue. shard_dimensions must be a list of length self.number_of_tuple_elements, and each element must be convertible to a Dimension compatible with self.tuple_shapes. Args: shard_dimensions: the dimensions of each queue element. Raises: ValueError: if shard_dimensions is not of length self.number_of_tuple_elements; or an element of shard_dimensions cannot be converted to a Dimension; or an element of shard_dimensions is a Dimension that is out of range for the corresponding tuple element shape.
github-repos
def out_file_name(out_dir, fname, ext=None): if ext is None: return os.path.join(out_dir, os.path.basename(fname)) fname = remove_ext(fname) return os.path.join(out_dir, '{}.{}'.format(fname, ext))
Return path of output file, given a directory, file name and extension. If fname is a path, it is converted to its basename. Args: out_dir (str): path to the directory where output should be written. fname (str): path to the input file. ext (str): file extension of the output file (defaults to None). Returns: str: out_dir + fname with extension replaced. If `ext` is `None`, the original extension is kept.
juraj-google-style
def prediction_step(self, model: nn.Module, inputs: dict[str, Union[torch.Tensor, Any]], prediction_loss_only: bool, ignore_keys: Optional[list[str]]=None, **gen_kwargs) -> tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: if not self.args.predict_with_generate or prediction_loss_only: return super().prediction_step(model, inputs, prediction_loss_only=prediction_loss_only, ignore_keys=ignore_keys) has_labels = 'labels' in inputs inputs = self._prepare_inputs(inputs) if len(gen_kwargs) == 0 and hasattr(self, '_gen_kwargs'): gen_kwargs = self._gen_kwargs.copy() if 'num_beams' in gen_kwargs and gen_kwargs['num_beams'] is None: gen_kwargs.pop('num_beams') if 'max_length' in gen_kwargs and gen_kwargs['max_length'] is None: gen_kwargs.pop('max_length') default_synced_gpus = is_deepspeed_zero3_enabled() or is_fsdp_managed_module(self.model) gen_kwargs['synced_gpus'] = gen_kwargs.get('synced_gpus', default_synced_gpus) generation_inputs = inputs.copy() if 'labels' in generation_inputs and 'decoder_input_ids' in generation_inputs and (generation_inputs['labels'].shape == generation_inputs['decoder_input_ids'].shape): generation_inputs = {k: v for k, v in inputs.items() if k not in ('decoder_input_ids', 'decoder_attention_mask')} summon_full_params_context = FullyShardedDataParallel.summon_full_params(self.model) if isinstance(self.model, FullyShardedDataParallel) else contextlib.nullcontext() with summon_full_params_context: generated_tokens = self.model.generate(**generation_inputs, **gen_kwargs) if self.model.generation_config._from_model_config: self.model.generation_config._from_model_config = False gen_config = self.model.generation_config if generated_tokens.shape[-1] < gen_config.max_length: generated_tokens = self._pad_tensors_to_max_len(generated_tokens, gen_config.max_length) elif gen_config.max_new_tokens is not None and generated_tokens.shape[-1] < gen_config.max_new_tokens + 1: generated_tokens = self._pad_tensors_to_max_len(generated_tokens, gen_config.max_new_tokens + 1) with torch.no_grad(): if has_labels: with self.compute_loss_context_manager(): outputs = model(**inputs) if self.label_smoother is not None: loss = self.label_smoother(outputs, inputs['labels']).detach().mean() else: loss = (outputs['loss'] if isinstance(outputs, dict) else outputs[0]).detach().mean() else: loss = None if self.args.prediction_loss_only: return (loss, None, None) if has_labels: labels = inputs['labels'] if labels.shape[-1] < gen_config.max_length: labels = self._pad_tensors_to_max_len(labels, gen_config.max_length) elif gen_config.max_new_tokens is not None and labels.shape[-1] < gen_config.max_new_tokens + 1: labels = self._pad_tensors_to_max_len(labels, gen_config.max_new_tokens + 1) else: labels = None return (loss, generated_tokens, labels)
Perform an evaluation step on `model` using `inputs`. Subclass and override to inject custom behavior. Args: model (`nn.Module`): The model to evaluate. inputs (`Dict[str, Union[torch.Tensor, Any]]`): The inputs and targets of the model. The dictionary will be unpacked before being fed to the model. Most models expect the targets under the argument `labels`. Check your model's documentation for all accepted arguments. prediction_loss_only (`bool`): Whether or not to return the loss only. gen_kwargs: Additional `generate` specific kwargs. Return: Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, logits and labels (each being optional).
github-repos
def __init__(self, range_tracker): assert isinstance(range_tracker, iobase.RangeTracker) self._range_tracker = range_tracker
Initializes UnsplittableRangeTracker. Args: range_tracker (~apache_beam.io.iobase.RangeTracker): a :class:`~apache_beam.io.iobase.RangeTracker` to which all method calls except calls to :meth:`.try_split()` will be delegated.
github-repos
def _get_id_token_user(token, issuers, audiences, allowed_client_ids, time_now, cache): for (issuer_key, issuer) in issuers.items(): issuer_cert_uri = convert_jwks_uri(issuer.jwks_uri) try: parsed_token = _verify_signed_jwt_with_certs(token, time_now, cache, cert_uri=issuer_cert_uri) except Exception: _logger.debug('id_token verification failed for issuer %s', issuer_key, exc_info=True) continue issuer_values = _listlike_guard(issuer.issuer, 'issuer', log_warning=False) if isinstance(audiences, _Mapping): audiences = audiences[issuer_key] if _verify_parsed_token(parsed_token, issuer_values, audiences, allowed_client_ids, is_legacy_google_auth=(issuer.issuer == _ISSUERS)): email = parsed_token['email'] return users.User(email)
Get a User for the given id token, if the token is valid. Args: token: The id_token to check. issuers: dict of Issuers audiences: List of audiences that are acceptable. allowed_client_ids: List of client IDs that are acceptable. time_now: The current time as a long (eg. long(time.time())). cache: Cache to use (eg. the memcache module). Returns: A User if the token is valid, None otherwise.
codesearchnet
def pad(x, p=3): return tf.pad(x, [[0, 0], [0, 0], [p, p], [p, p]])
Pad tensor in H, W Remarks: TensorFlow uses "ceil(input_spatial_shape[i] / strides[i])" rather than explicit padding like Caffe, pyTorch does. Hence, we need to pad here beforehand. Args: x (tf.tensor): incoming tensor p (int, optional): padding for H, W Returns: tf.tensor: padded tensor
codesearchnet
def _reverse_convert(x, factor1, factor2): return ((x * factor1) / (((1 - x) * factor2) + (x * factor1)))
Converts mixing ratio x in c1 - c2 tie line to that in comp1 - comp2 tie line. Args: x (float): Mixing ratio x in c1 - c2 tie line, a float between 0 and 1. factor1 (float): Compositional ratio between composition c1 and processed composition comp1. E.g., factor for Composition('SiO2') and Composition('O') is 2. factor2 (float): Compositional ratio between composition c2 and processed composition comp2. Returns: Mixing ratio in comp1 - comp2 tie line, a float between 0 and 1.
codesearchnet
def put(self, key, vals, indices=None, name=None): with ops.name_scope(name, '%s_put' % self._name, self._scope_vals(vals)) as scope: vals, indices = self._check_put_dtypes(vals, indices) with ops.colocate_with(self._coloc_op): op = self._put_fn(key, indices, vals, dtypes=self._dtypes, shared_name=self._name, name=scope, capacity=self._capacity, memory_limit=self._memory_limit) return op
Create an op that stores the (key, vals) pair in the staging area. Incomplete puts are possible, preferably using a dictionary for vals as the appropriate dtypes and shapes can be inferred from the value names dictionary key values. If vals is a list or tuple, indices must also be specified so that the op knows at which element position to perform the insert. This operation will block if the capacity or memory limit of this container is reached. Args: key: Key associated with the data vals: Tensor (or a dict/tuple of Tensors) to place into the staging area. indices: (Optional) if vals is a tuple/list, this is required. name: A name for the operation (optional) Returns: The created op Raises: ValueError: If the number or type of inputs don't match the staging area.
github-repos
class SmolVLMEncoder(nn.Module): def __init__(self, config: SmolVLMConfig): super().__init__() self.config = config self.layers = nn.ModuleList([SmolVLMEncoderLayer(config) for _ in range(config.num_hidden_layers)]) self.gradient_checkpointing = False def forward(self, inputs_embeds, attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states return_dict = return_dict if return_dict is not None else self.config.use_return_dict encoder_states = () if output_hidden_states else None all_attentions = () if output_attentions else None hidden_states = inputs_embeds for encoder_layer in self.layers: if output_hidden_states: encoder_states = encoder_states + (hidden_states,) if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func(encoder_layer.__call__, hidden_states, attention_mask, output_attentions) else: layer_outputs = encoder_layer(hidden_states, attention_mask, output_attentions=output_attentions) hidden_states = layer_outputs[0] if output_attentions: all_attentions = all_attentions + (layer_outputs[1],) if output_hidden_states: encoder_states = encoder_states + (hidden_states,) if not return_dict: return tuple((v for v in [hidden_states, encoder_states, all_attentions] if v is not None)) return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)
Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a [`SmolVLMEncoderLayer`]. Args: config: SmolVLMConfig
github-repos
def asserts(self, fn, msg_or_fn): self.assertions.append((fn, msg_or_fn)) return self
Assert that prepared values satisfy given conditions. Assertions are intended in enforce conditions beyond simple value type validation. For instance, this method can be use to assert that the columns of a ``ColumnDataSource`` all collectively have the same length at all times. Args: fn (callable) : A function accepting ``(obj, value)`` that returns True if the value passes the assertion, or False otherwise. msg_or_fn (str or callable) : A message to print in case the assertion fails, or a function accepting ``(obj, name, value)`` to call in in case the assertion fails. Returns: self
codesearchnet
def translate_item_ids(self, item_ids, language, is_nested=None): if is_nested is None: def is_nested_fun(x): return True elif isinstance(is_nested, bool): def is_nested_fun(x): return is_nested else: is_nested_fun = is_nested all_item_type_ids = ItemType.objects.get_all_item_type_ids() groupped = proso.list.group_by(item_ids, by=lambda item_id: all_item_type_ids[item_id]) result = {} for item_type_id, items in groupped.items(): with timeit('translating item type {}'.format(item_type_id)): item_type = ItemType.objects.get_all_types()[item_type_id] model = ItemType.objects.get_model(item_type_id) kwargs = {'{}__in'.format(item_type['foreign_key']): items} if 'language' in item_type: kwargs[item_type['language']] = language if any([not is_nested_fun(item_id) for item_id in items]) and hasattr(model.objects, 'prepare_related'): objs = model.objects.prepare_related() elif hasattr(model.objects, 'prepare'): objs = model.objects.prepare() else: objs = model.objects for obj in objs.filter(**kwargs): item_id = getattr(obj, item_type['foreign_key']) result[item_id] = obj.to_json(nested=is_nested_fun(item_id)) return result
Translate a list of item ids to JSON objects which reference them. Args: item_ids (list[int]): item ids language (str): language used for further filtering (some objects for different languages share the same item) is_nested (function): mapping from item ids to booleans, where the boolean value indicates whether the item is nested Returns: dict: item id -> JSON object
juraj-google-style
def on_train_begin(self, logs=None):
Called at the beginning of training. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future.
github-repos
def summary_op(self): return self._summary_op
Return the Summary Tensor used by the chief supervisor. Returns: A string Tensor for the summary or `None`.
github-repos
def split_to_discretized_mix_logistic_params(inputs): (batch, height, width, output_dim) = shape_list(inputs) num_mixtures = (output_dim (logits, locs, log_scales, coeffs) = tf.split(inputs, num_or_size_splits=[num_mixtures, (num_mixtures * 3), (num_mixtures * 3), (num_mixtures * 3)], axis=(- 1)) split_shape = [batch, height, width, num_mixtures, 3] locs = tf.reshape(locs, split_shape) log_scales = tf.reshape(log_scales, split_shape) log_scales = tf.maximum(log_scales, (- 7.0)) coeffs = tf.reshape(coeffs, split_shape) coeffs = tf.tanh(coeffs) return (logits, locs, log_scales, coeffs)
Splits input tensor into parameters of discretized mixture logistic. Args: inputs: A [batch, height, width, num_mixtures*10] tensor of floats comprising one unconstrained mixture probability, three means (one per channel), three standard deviations (one per channel), and three coefficients which linearly parameterize dependence across channels. Returns: Tuple of unconstrained mixture probabilities, locations, scales, and coefficient parameters of the distribution. The mixture probability has shape [batch, height, width, num_mixtures]. Other parameters have shape [batch, height, width, num_mixtures, 3].
codesearchnet
def write_to_file(self, filename='material_index.dat', plot=True): path = os.path.dirname(sys.modules[__name__].__file__) + '/' with open(filename, 'w') as fs: for n_row in np.abs(self.n[::-1]): n_str = ','.join([str(v) for v in n_row]) fs.write(n_str+'\n') if plot: filename_image_prefix, _ = os.path.splitext(filename) filename_image = filename_image_prefix + '.png' args = { 'title': 'Refractive Index Profile', 'x_pts': self.x_pts, 'y_pts': self.y_pts, 'x_min': self.x_min, 'x_max': self.x_max, 'y_min': self.y_min, 'y_max': self.y_max, 'filename_data': filename, 'filename_image': filename_image } if MPL: heatmap = np.loadtxt(args['filename_data'], delimiter=',') plt.clf() plt.title(args['title']) plt.xlabel('$x$') plt.ylabel('$y$') plt.imshow(np.flipud(heatmap), extent=(args['x_min'], args['x_max'], args['y_min'], args['y_max']), aspect="auto") plt.colorbar() plt.savefig(filename_image) else: gp.gnuplot(path+'structure.gpi', args)
Write the refractive index profile to file. Args: filename (str): The nominal filename the refractive index data should be saved to. plot (bool): `True` if plots should be generates, otherwise `False`. Default is `True`.
juraj-google-style
def add_keyed(self, value, key, date=None, return_value=False): return self.add(value, date, return_value, key)
Add keyed metrics data to collection. Args: value (str): The value of the metric. key (str): The key value for keyed metrics. date (str, optional): The optional date of the metric. return_value (bool, default:False): Tell the API to return the updates metric value. Return: dict: If return_value is True a dict with the current value for the time period is returned.
codesearchnet
def split_to_tiles(img, columns, rows): im_w, im_h = img.shape tile_w, tile_h = int(np.floor(im_w / columns)), int(np.floor(im_h / rows)) tiles = [] for pos_y in range(0, im_h - rows, tile_h): for pos_x in range(0, im_w - columns, tile_w): roi = (pos_x, pos_y, pos_x + tile_w, pos_y + tile_h) tile = img[roi[1]:roi[3], roi[0]:roi[2]] tiles.append(tile) return tuple(tiles)
Split an image into a specified number of tiles. Args: img (ndarray): The image to split. number_tiles (int): The number of tiles required. Returns: Tuple of tiles
juraj-google-style
def init_continuous_batching(self, generation_config: Optional[GenerationConfig]=None, manual_eviction: bool=False, max_queue_size: int=0, streaming: bool=False) -> ContinuousBatchingManager: if not hasattr(self, 'config') or not hasattr(self, 'device') or (not hasattr(self, 'dtype')): raise AttributeError("Model must have 'config', 'device', and 'dtype' attributes.") gen_config = generation_config if generation_config is not None else self.generation_config if gen_config is None: raise ValueError('A GenerationConfig must be provided or set in the model.') if gen_config.eos_token_id is None: logger.warning('`eos_token_id` not set in GenerationConfig. Setting to -1 (disabled).') gen_config.eos_token_id = -1 return ContinuousBatchingManager(model=self, generation_config=gen_config, manual_eviction=manual_eviction, max_queue_size=max_queue_size, streaming=streaming)
Initialize a manager for continuous batching inference. Args: generation_config: Custom generation configuration max_queue_size: Maximum size of the input request queue streaming: Whether to stream tokens as they are generated Returns: `ContinuousBatchingManager`: The manager instance to add requests and retrieve results.
github-repos
def service_restarted_since(self, sentry_unit, mtime, service, pgrep_full=None, sleep_time=20, retry_count=30, retry_sleep_time=10): unit_name = sentry_unit.info['unit_name'] self.log.debug(('Checking that %s service restarted since %s on %s' % (service, mtime, unit_name))) time.sleep(sleep_time) proc_start_time = None tries = 0 while ((tries <= retry_count) and (not proc_start_time)): try: proc_start_time = self._get_proc_start_time(sentry_unit, service, pgrep_full) self.log.debug('Attempt {} to get {} proc start time on {} OK'.format(tries, service, unit_name)) except IOError as e: self.log.debug('Attempt {} to get {} proc start time on {} failed\n{}'.format(tries, service, unit_name, e)) time.sleep(retry_sleep_time) tries += 1 if (not proc_start_time): self.log.warn('No proc start time found, assuming service did not start') return False if (proc_start_time >= mtime): self.log.debug(('Proc start time is newer than provided mtime(%s >= %s) on %s (OK)' % (proc_start_time, mtime, unit_name))) return True else: self.log.warn(('Proc start time (%s) is older than provided mtime (%s) on %s, service did not restart' % (proc_start_time, mtime, unit_name))) return False
Check if service was been started after a given time. Args: sentry_unit (sentry): The sentry unit to check for the service on mtime (float): The epoch time to check against service (string): service name to look for in process table pgrep_full: [Deprecated] Use full command line search mode with pgrep sleep_time (int): Initial sleep time (s) before looking for file retry_sleep_time (int): Time (s) to sleep between retries retry_count (int): If file is not found, how many times to retry Returns: bool: True if service found and its start time it newer than mtime, False if service is older than mtime or if service was not found.
codesearchnet
def record_tx(self, origin, destination, amount, outcome, destination_id=None): if destination_id: tx = db.Transaction(txtype='move', from_user_id=origin, to_user_id=destination_id, txdate=datetime.now(), amount=amount, currency=COINS[self.coin]['ticker'], to_coin_address=destination) else: self.logger.debug(self.gettransaction(outcome)) confirmations = self.gettransaction(outcome)['confirmations'] last_confirmation = (datetime.now() if confirmations else None) tx = db.Transaction(txtype='sendfrom', from_user_id=origin, txhash=outcome, txdate=datetime.now(), amount=amount, currency=COINS[self.coin]['ticker'], to_coin_address=destination, confirmations=confirmations, last_confirmation=last_confirmation) db.session.add(tx) db.session.commit() return outcome
Records a transaction in the database. Args: origin (str): user_id of the sender destination (str): coin address or user_id of the recipient amount (str, Decimal, number): amount to send outcome (str, bool): the transaction hash if this is a "sendfrom" transaction; for "move", True if successful, False otherwise destination_id (str): the destination account label ("move" only) Returns: str or bool: the outcome (input) argument
codesearchnet
def conv(name, x, output_channels, filter_size=None, stride=None, logscale_factor=3.0, apply_actnorm=True, conv_init='default', dilations=None): if ((conv_init == 'zeros') and apply_actnorm): raise ValueError('apply_actnorm is unstable when init is set to zeros.') x_shape = common_layers.shape_list(x) is_2d = (len(x_shape) == 4) num_steps = x_shape[1] if is_2d: if (filter_size is None): filter_size = [3, 3] if (stride is None): stride = [1, 1] if (dilations is None): dilations = [1, 1, 1, 1] actnorm_func = actnorm x = add_edge_bias(x, filter_size=filter_size) conv_filter = tf.nn.conv2d else: if (filter_size is None): if (num_steps == 1): filter_size = [1, 3, 3] else: filter_size = [2, 3, 3] if (stride is None): stride = [1, 1, 1] if (dilations is None): dilations = [1, 1, 1, 1, 1] actnorm_func = actnorm_3d x = time_pad(x, filter_size=filter_size, dilations=dilations) conv_filter = tf.nn.conv3d in_channels = common_layers.shape_list(x)[(- 1)] filter_shape = (filter_size + [in_channels, output_channels]) stride_shape = (([1] + stride) + [1]) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): if (conv_init == 'default'): initializer = default_initializer() elif (conv_init == 'zeros'): initializer = tf.zeros_initializer() w = tf.get_variable('W', filter_shape, tf.float32, initializer=initializer) x = conv_filter(x, w, stride_shape, padding='VALID', dilations=dilations) if apply_actnorm: (x, _) = actnorm_func('actnorm', x, logscale_factor=logscale_factor) else: x += tf.get_variable('b', [1, 1, 1, output_channels], initializer=tf.zeros_initializer()) logs = tf.get_variable('logs', [1, output_channels], initializer=tf.zeros_initializer()) x *= tf.exp((logs * logscale_factor)) return x
Convolutional layer with edge bias padding and optional actnorm. If x is 5-dimensional, actnorm is applied independently across every time-step. Args: name: variable scope. x: 4-D Tensor or 5-D Tensor of shape NHWC or NTHWC output_channels: Number of output channels. filter_size: list of ints, if None [3, 3] and [2, 3, 3] are defaults for 4-D and 5-D input tensors respectively. stride: list of ints, default stride: 1 logscale_factor: see actnorm for parameter meaning. apply_actnorm: if apply_actnorm the activations of the first minibatch have zero mean and unit variance. Else, there is no scaling applied. conv_init: default or zeros. default is a normal distribution with 0.05 std. dilations: List of integers, apply dilations. Returns: x: actnorm(conv2d(x)) Raises: ValueError: if init is set to "zeros" and apply_actnorm is set to True.
codesearchnet
def set_agent(self, short_name, client_id): if short_name not in self.services: raise ArgumentError("Unknown service name", short_name=short_name) self.agents[short_name] = client_id
Register a client id that handlers commands for a service. Args: short_name (str): The name of the service to set an agent for. client_id (str): A globally unique id for the client that should receive commands for this service.
juraj-google-style
def _ContainsAll(self, verb, expected): actual_list = list(self._actual) missing = _DuplicateCounter() actual_not_in_order = set() ordered = True for i in expected: try: index = actual_list.index(i) for _ in six.moves.xrange(index): actual_element = actual_list.pop(0) if _IsHashable(actual_element) and isinstance(actual_not_in_order, collections_abc.Set): actual_not_in_order.add(actual_element) else: if isinstance(actual_not_in_order, collections_abc.Set): actual_not_in_order = list(actual_not_in_order) if actual_element not in actual_not_in_order: actual_not_in_order.append(actual_element) actual_list.pop(0) except ValueError: if not _IsHashable(i) and isinstance(actual_not_in_order, collections_abc.Set): actual_not_in_order = list(actual_not_in_order) if i in actual_not_in_order: actual_not_in_order.remove(i) ordered = False else: missing.Increment(i) if missing: self._FailWithBadResults(verb, expected, 'is missing', missing) if ordered: return _InOrder() else: return _NotInOrder(self._actual, 'contains all elements in order', expected)
Determines if the subject contains all the expected elements. Helper function for ContainsAllIn() and ContainsAllOf(). Args: verb: string describing how the expected elements should be contained. expected: iterable of objects that should be contained in the subject. Returns: If the subject does contain all the expected elements, returns an _Ordered predicate on which .InOrder() can be subsequently called. Raises: TruthAssertionError: the subject is missing any of the expected elements.
github-repos
def status(self, job_ids): logger.debug("Checking status of: {0}".format(job_ids)) for job_id in self.resources: if self.resources[job_id]['proc']: poll_code = self.resources[job_id]['proc'].poll() if self.resources[job_id]['status'] in ['COMPLETED', 'FAILED']: continue if poll_code is None: self.resources[job_id]['status'] = 'RUNNING' elif poll_code == 0: self.resources[job_id]['status'] = 'COMPLETED' elif poll_code != 0: self.resources[job_id]['status'] = 'FAILED' else: logger.error("Internal consistency error: unexpected case in local provider state machine") elif self.resources[job_id]['remote_pid']: retcode, stdout, stderr = self.channel.execute_wait('ps -p {} &> /dev/null; echo "STATUS:$?" ', self.cmd_timeout) for line in stdout.split('\n'): if line.startswith("STATUS:"): status = line.split("STATUS:")[1].strip() if status == "0": self.resources[job_id]['status'] = 'RUNNING' else: self.resources[job_id]['status'] = 'FAILED' return [self.resources[jid]['status'] for jid in job_ids]
Get the status of a list of jobs identified by their ids. Args: - job_ids (List of ids) : List of identifiers for the jobs Returns: - List of status codes.
juraj-google-style
def setup(argv): parser = argparse.ArgumentParser(description='Compute Jekyl- and prose-aware wordcounts', epilog='Accepted filetypes: plaintext, markdown, markdown (Jekyll)') parser.add_argument('-S', '--split-hyphens', action='store_true', dest='split_hyphens', help='split hyphenated words rather than counting them as one word ("non-trivial" counts as two words rather than one)') parser.add_argument('-u', '--update', action='store_true', help='update the jekyll file in place with the counts. Does nothing if the file is not a Jekyll markdown file. Implies format=yaml, invalid with input from STDIN and non-Jekyll files.') parser.add_argument('-f', '--format', nargs='?', choices=['yaml', 'json', 'default'], default='default', help='output format.') parser.add_argument('-i', '--indent', type=int, nargs='?', default=4, help='indentation depth (default: 4).') parser.add_argument('file', type=argparse.FileType('rb'), help='file to parse (or - for STDIN)') return parser.parse_args(argv)
Sets up the ArgumentParser. Args: argv: an array of arguments
codesearchnet
def _matmul_3d_with_batch_dim_folding(a, b, **kwargs): reshaped_a = array_ops.expand_dims(a.values, 1) reshaped_b = array_ops.repeat(b, a.row_lengths(), axis=0) flat_result = math_ops.matmul(reshaped_a, reshaped_b, **kwargs) return a.with_values(array_ops.squeeze(flat_result, axis=1))
Multiply batches of 2D matrices where only `a.shape[1]` is ragged. Args: a: A RaggedTensor with `shape=[B, (I), J]`. (ragged_rank must be 1.) b: A Tensor with `shape=[B, J, K]` **kwargs: Additional arguments for `tf.matmul` (e.g. transpose_a). transpose_a and adjoint_a must not be true. Returns: A RaggedTensor with `shape=[B, (I), K].
github-repos
def values_from_const(node_def: node_def_pb2.NodeDef) -> np.ndarray: if node_def.op != 'Const': raise ValueError(f'Can not extract constant value from a node that is not Const. Got:\n{node_def}') input_tensor = node_def.attr['value'].tensor tensor_value = tensor_util.MakeNdarray(input_tensor) return tensor_value
Extracts the values from a const NodeDef as a numpy ndarray. Args: node_def: Const NodeDef that has the values we want to access. Returns: Numpy ndarray containing the values. Raises: ValueError: If the node isn't a Const.
github-repos
def correct_segmentation(segments, clusters, min_time): result_segments = [] prev_segment = None for i, segment in enumerate(segments): if len(segment) >= 1: continue cluster = clusters[i] if prev_segment is None: prev_segment = segment else: cluster_dt = 0 if len(cluster) > 0: cluster_dt = abs(cluster[0].time_difference(cluster[-1])) if cluster_dt <= min_time: prev_segment.extend(segment) else: prev_segment.append(segment[0]) result_segments.append(prev_segment) prev_segment = segment if prev_segment is not None: result_segments.append(prev_segment) return result_segments
Corrects the predicted segmentation This process prevents over segmentation Args: segments (:obj:`list` of :obj:`list` of :obj:`Point`): segments to correct min_time (int): minimum required time for segmentation
juraj-google-style
def bind(self, **bindings): new_context = dict(self._partial_context) unknown_keys = [] for k, v in six.iteritems(bindings): if k not in self._unbound_vars: unknown_keys.append(k) new_context[self._unbound_vars[k]] = v if unknown_keys: raise ValueError( 'The following keys are not associated with any unbound vars: %s, ' 'legal values are %s' % (unknown_keys, list(self._unbound_vars.keys()))) return _DeferredLayer(self.bookkeeper, None, (), {}, scope=self._scope, defaults=self._defaults, pass_through=self, partial_context=new_context)
Creates a new template with the given unbound variables bound. Args: **bindings: Arguments for every deferred parameter. Returns: A new template with the given bindings. Raises: ValueError: If any of the bindings do not correspond to unbound variables.
juraj-google-style
def WriteGraphSeries(graph_series, label, token = None): if data_store.RelationalDBEnabled(): data_store.REL_DB.WriteClientGraphSeries(graph_series, label) if _ShouldUseLegacyDatastore(): aff4_attr = _GetAFF4AttributeForReportType(graph_series.report_type)() if isinstance(aff4_attr, rdf_stats.GraphSeries): for graph in graph_series.graphs: aff4_attr.Append(graph) elif isinstance(aff4_attr, rdf_stats.Graph): for sample in graph_series.graphs[0]: aff4_attr.Append(x_value=sample.x_value, y_value=sample.y_value) else: raise AFF4AttributeTypeError(aff4_attr.__class__) with aff4.FACTORY.Create( GetAFF4ClientReportsURN().Add(label), aff4_type=aff4_stats.ClientFleetStats, mode="w", token=token) as stats_for_label: stats_for_label.AddAttribute(aff4_attr)
Writes graph series for a particular client label to the DB. Args: graph_series: A series of rdf_stats.Graphs containing aggregated data for a particular report-type. label: Client label by which data in the graph_series was aggregated. token: ACL token to use for writing to the legacy (non-relational) datastore. Raises: AFF4AttributeTypeError: If, when writing to the legacy DB, an unexpected report-data type is encountered.
juraj-google-style
def VerifyScripts(verifiable): try: hashes = verifiable.GetScriptHashesForVerifying() except Exception as e: logger.debug(("couldn't get script hashes %s " % e)) return False if (len(hashes) != len(verifiable.Scripts)): logger.debug(f'hash - verification script length mismatch ({len(hashes)}/{len(verifiable.Scripts)})') return False blockchain = GetBlockchain() for i in range(0, len(hashes)): verification = verifiable.Scripts[i].VerificationScript if (len(verification) == 0): sb = ScriptBuilder() sb.EmitAppCall(hashes[i].Data) verification = sb.ms.getvalue() else: verification_hash = Crypto.ToScriptHash(verification, unhex=False) if (hashes[i] != verification_hash): logger.debug(f'hash {hashes[i]} does not match verification hash {verification_hash}') return False state_reader = GetStateReader() script_table = CachedScriptTable(DBCollection(blockchain._db, DBPrefix.ST_Contract, ContractState)) engine = ApplicationEngine(TriggerType.Verification, verifiable, script_table, state_reader, Fixed8.Zero()) engine.LoadScript(verification) invocation = verifiable.Scripts[i].InvocationScript engine.LoadScript(invocation) try: success = engine.Execute() state_reader.ExecutionCompleted(engine, success) except Exception as e: state_reader.ExecutionCompleted(engine, False, e) if ((engine.ResultStack.Count != 1) or (not engine.ResultStack.Pop().GetBoolean())): Helper.EmitServiceEvents(state_reader) if (engine.ResultStack.Count > 0): logger.debug(f'Result stack failure! Count: {engine.ResultStack.Count} bool value: {engine.ResultStack.Pop().GetBoolean()}') else: logger.debug(f'Result stack failure! Count: {engine.ResultStack.Count}') return False Helper.EmitServiceEvents(state_reader) return True
Verify the scripts of the provided `verifiable` object. Args: verifiable (neo.IO.Mixins.VerifiableMixin): Returns: bool: True if verification is successful. False otherwise.
codesearchnet
def get_password(request, mapping) -> None: LOGGER.debug('Received request "%s"', request) if ('host' not in request): LOGGER.error('host= entry missing in request. Cannot query without a host') return host = request['host'] if ('path' in request): host = '/'.join([host, request['path']]) def skip(line, skip): return line[skip:] LOGGER.debug('Iterating mapping to match against host "%s"', host) for section in mapping.sections(): if fnmatch.fnmatch(host, section): LOGGER.debug('Section "%s" matches requested host "%s"', section, host) pass_target = mapping.get(section, 'target').replace('${host}', request['host']) password_extractor = SpecificLineExtractor(0, 0, option_suffix='_password') password_extractor.configure(mapping[section]) username_extractor = _username_extractors[mapping[section].get('username_extractor', fallback=_line_extractor_name)] username_extractor.configure(mapping[section]) LOGGER.debug('Requesting entry "%s" from pass', pass_target) output = subprocess.check_output(['pass', 'show', pass_target]).decode('utf-8') lines = output.splitlines() password = password_extractor.get_value(pass_target, lines) username = username_extractor.get_value(pass_target, lines) if password: print('password={password}'.format(password=password)) if (('username' not in request) and username): print('username={username}'.format(username=username)) return LOGGER.warning('No mapping matched') sys.exit(1)
Resolve the given credential request in the provided mapping definition. The result is printed automatically. Args: request: The credential request specified as a dict of key-value pairs. mapping: The mapping configuration as a ConfigParser instance.
codesearchnet
def head(self, path=None, client_kwargs=None, header=None): if header is not None: return header elif client_kwargs is None: client_kwargs = self.get_client_kwargs(path) return self._head(client_kwargs)
Returns object HTTP header. Args: path (str): Path or URL. client_kwargs (dict): Client arguments. header (dict): Object header. Returns: dict: HTTP header.
juraj-google-style
def load_optimizer_weights_from_hdf5_group(hdf5_group): weights_group = hdf5_group['optimizer_weights'] optimizer_weight_names = load_attributes_from_hdf5_group(weights_group, 'weight_names') return [weights_group[weight_name] for weight_name in optimizer_weight_names]
Load optimizer weights from a HDF5 group. Args: hdf5_group: A pointer to a HDF5 group. Returns: data: List of optimizer weight names.
github-repos
def __init__(self, id, **kwargs): super(Artist, self).__init__(id, **kwargs)
Artist class Args: id (str): an artistw ID Returns: An artist object Example: >>> a = artist.Artist('ARH6W4X1187B99274F', buckets=['hotttnesss']) >>> a.hotttnesss 0.80098515900997658 >>>
juraj-google-style
def strace_configure(self, port_width): if port_width not in [1, 2, 4]: raise ValueError('Invalid port width: %s' % str(port_width)) config_string = 'PortWidth=%d' % port_width res = self._dll.JLINK_STRACE_Config(config_string.encode()) if res < 0: raise errors.JLinkException('Failed to configure STRACE port') return None
Configures the trace port width for tracing. Note that configuration cannot occur while STRACE is running. Args: self (JLink): the ``JLink`` instance port_width (int): the trace port width to use. Returns: ``None`` Raises: ValueError: if ``port_width`` is not ``1``, ``2``, or ``4``. JLinkException: on error.
juraj-google-style
def most_specific_compatible_shape(self, other) -> 'TensorShape': other = as_shape(other) if self.dims is None or other.dims is None or self.rank != other.rank: return unknown_shape() dims = [d1 if d1 is not None and d2 is not None and (d1 == d2) else None for d1, d2 in zip(self.dims, other.dims)] return TensorShape(dims)
Returns the most specific TensorShape compatible with `self` and `other`. * TensorShape([None, 1]) is the most specific TensorShape compatible with both TensorShape([2, 1]) and TensorShape([5, 1]). Note that TensorShape(None) is also compatible with above mentioned TensorShapes. * TensorShape([1, 2, 3]) is the most specific TensorShape compatible with both TensorShape([1, 2, 3]) and TensorShape([1, 2, 3]). There are more less specific TensorShapes compatible with above mentioned TensorShapes, e.g. TensorShape([1, 2, None]), TensorShape(None). Args: other: Another `TensorShape`. Returns: A `TensorShape` which is the most specific compatible shape of `self` and `other`.
github-repos
def convert_typing_to_builtin(typ): origin = getattr(typ, '__origin__', None) args = getattr(typ, '__args__', None) if origin not in _BUILTINS: return typ if not args: return origin if origin is list: return list[convert_typing_to_builtin(args[0])] elif origin is dict: return dict[convert_typing_to_builtin(args[0]), convert_typing_to_builtin(args[1])] elif origin is tuple: return tuple[tuple(convert_typing_to_builtin(args))] elif origin is set: return set[convert_typing_to_builtin(args)] elif origin is frozenset: return frozenset[convert_typing_to_builtin(args)]
Converts a given typing collections type to its builtin counterpart. Args: typ: A typing type (e.g., typing.List[int]). Returns: type: The corresponding builtin type (e.g., list[int]).
github-repos
def add_path_argument(cls, group, argname, dest=None, help_=None): prefixed = '%s-%s' % (cls.argument_prefix, argname) if dest is None: dest = prefixed.replace('-', '_') final_dest = dest[len(cls.argument_prefix) + 1:] else: final_dest = dest dest = '%s_%s' % (cls.argument_prefix, dest) group.add_argument('--%s' % prefixed, action='store', dest=dest, help=help_) cls.path_arguments[dest] = final_dest
Subclasses may call this to expose a path argument. Args: group: arparse.ArgumentGroup, the extension argument group argname: str, the name of the argument, will be namespaced. dest: str, similar to the `dest` argument of `argparse.ArgumentParser.add_argument`, will be namespaced. help_: str, similar to the `help` argument of `argparse.ArgumentParser.add_argument`.
juraj-google-style
def saver(self): return self._saver
Return the Saver used by the supervisor. Returns: A Saver object.
github-repos
def _get_depencency_var_name(self, dependency): for (dep_path, var_name) in self.dependencies: if (dep_path == dependency): return var_name
Returns the variable name assigned to the given dependency or None if the dependency has not yet been registered. Args: dependency (str): Thet dependency that needs to be imported. Returns: str or None
codesearchnet
def extremum_icohpvalue(self, summed_spin_channels=True, spin=Spin.up): if not self._are_coops: extremum = sys.float_info.max else: extremum = -sys.float_info.max if not self._is_spin_polarized: if spin == Spin.down: warnings.warn("This spin channel does not exist. I am switching to Spin.up") spin = Spin.up for value in self._icohplist.values(): if not value.is_spin_polarized or not summed_spin_channels: if not self._are_coops: if value.icohpvalue(spin) < extremum: extremum = value.icohpvalue(spin) else: if value.icohpvalue(spin) > extremum: extremum = value.icohpvalue(spin) else: if not self._are_coops: if value.summed_icohp < extremum: extremum = value.summed_icohp else: if value.summed_icohp > extremum: extremum = value.summed_icohp return extremum
get ICOHP/ICOOP of strongest bond Args: summed_spin_channels: Boolean to indicate whether the ICOHPs/ICOOPs of both spin channels should be summed spin: if summed_spin_channels is equal to False, this spin indicates which spin channel should be returned Returns: lowest ICOHP/largest ICOOP value (i.e. ICOHP/ICOOP value of strongest bond)
juraj-google-style
def get_plot(self, normalize_rxn_coordinate=True, label_barrier=True): plt = pretty_plot(12, 8) scale = 1 if not normalize_rxn_coordinate else 1 / self.r[-1] x = np.arange(0, np.max(self.r), 0.01) y = self.spline(x) * 1000 relative_energies = self.energies - self.energies[0] plt.plot(self.r * scale, relative_energies * 1000, 'ro', x * scale, y, 'k-', linewidth=2, markersize=10) plt.xlabel("Reaction coordinate") plt.ylabel("Energy (meV)") plt.ylim((np.min(y) - 10, np.max(y) * 1.02 + 20)) if label_barrier: data = zip(x * scale, y) barrier = max(data, key=lambda d: d[1]) plt.plot([0, barrier[0]], [barrier[1], barrier[1]], 'k--') plt.annotate('%.0f meV' % (np.max(y) - np.min(y)), xy=(barrier[0] / 2, barrier[1] * 1.02), xytext=(barrier[0] / 2, barrier[1] * 1.02), horizontalalignment='center') plt.tight_layout() return plt
Returns the NEB plot. Uses Henkelman's approach of spline fitting each section of the reaction path based on tangent force and energies. Args: normalize_rxn_coordinate (bool): Whether to normalize the reaction coordinate to between 0 and 1. Defaults to True. label_barrier (bool): Whether to label the maximum barrier. Returns: matplotlib.pyplot object.
juraj-google-style
def sg_init(sess): r sess.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()))
r""" Initializes session variables. Args: sess: Session to initialize.
juraj-google-style
def _CompositeMapByteStream( self, byte_stream, byte_offset=0, context=None, **unused_kwargs): context_state = getattr(context, 'state', {}) attribute_index = context_state.get('attribute_index', 0) mapped_values = context_state.get('mapped_values', None) subcontext = context_state.get('context', None) if not mapped_values: mapped_values = self._structure_values_class() if not subcontext: subcontext = DataTypeMapContext(values={ type(mapped_values).__name__: mapped_values}) members_data_size = 0 for attribute_index in range(attribute_index, self._number_of_attributes): attribute_name = self._attribute_names[attribute_index] data_type_map = self._data_type_maps[attribute_index] member_definition = self._data_type_definition.members[attribute_index] condition = getattr(member_definition, 'condition', None) if condition: namespace = dict(subcontext.values) namespace['__builtins__'] = {} try: condition_result = eval(condition, namespace) except Exception as exception: raise errors.MappingError( 'Unable to evaluate condition with error: {0!s}'.format( exception)) if not isinstance(condition_result, bool): raise errors.MappingError( 'Condition does not result in a boolean value') if not condition_result: continue if isinstance(member_definition, data_types.PaddingDefinition): _, byte_size = divmod( members_data_size, member_definition.alignment_size) if byte_size > 0: byte_size = member_definition.alignment_size - byte_size data_type_map.byte_size = byte_size try: value = data_type_map.MapByteStream( byte_stream, byte_offset=byte_offset, context=subcontext) setattr(mapped_values, attribute_name, value) except errors.ByteStreamTooSmallError as exception: context_state['attribute_index'] = attribute_index context_state['context'] = subcontext context_state['mapped_values'] = mapped_values raise errors.ByteStreamTooSmallError(exception) except Exception as exception: raise errors.MappingError(exception) supported_values = getattr(member_definition, 'values', None) if supported_values and value not in supported_values: raise errors.MappingError( 'Value: {0!s} not in supported values: {1:s}'.format( value, ', '.join([ '{0!s}'.format(value) for value in supported_values]))) byte_offset += subcontext.byte_size members_data_size += subcontext.byte_size if attribute_index != (self._number_of_attributes - 1): context_state['attribute_index'] = attribute_index context_state['context'] = subcontext context_state['mapped_values'] = mapped_values error_string = ( 'Unable to read: {0:s} from byte stream at offset: {1:d} ' 'with error: missing attribute: {2:d}').format( self._data_type_definition.name, byte_offset, attribute_index) raise errors.ByteStreamTooSmallError(error_string) if context: context.byte_size = members_data_size context.state = {} return mapped_values
Maps a sequence of composite data types on a byte stream. Args: byte_stream (bytes): byte stream. byte_offset (Optional[int]): offset into the byte stream where to start. context (Optional[DataTypeMapContext]): data type map context. Returns: object: mapped value. Raises: MappingError: if the data type definition cannot be mapped on the byte stream.
juraj-google-style
def GetMessages(self, formatter_mediator, event): if self.DATA_TYPE != event.data_type: raise errors.WrongFormatter('Unsupported data type: {0:s}.'.format( event.data_type)) event_values = event.CopyToDict() restore_point_event_type = event_values.get( 'restore_point_event_type', None) if restore_point_event_type is not None: event_values['restore_point_event_type'] = ( self._RESTORE_POINT_EVENT_TYPES.get( restore_point_event_type, 'UNKNOWN')) restore_point_type = event_values.get('restore_point_type', None) if restore_point_type is not None: event_values['restore_point_type'] = ( self._RESTORE_POINT_EVENT_TYPES.get(restore_point_type, 'UNKNOWN')) return self._ConditionalFormatMessages(event_values)
Determines the formatted message strings for an event object. Args: formatter_mediator (FormatterMediator): mediates the interactions between formatters and other components, such as storage and Windows EventLog resources. event (EventObject): event. Returns: tuple(str, str): formatted message string and short message string. Raises: WrongFormatter: if the event object cannot be formatted by the formatter.
juraj-google-style
def _ReadEncodedData(self, read_size): encoded_data = self._file_object.read(read_size) read_count = len(encoded_data) self._encoded_data = b''.join([self._encoded_data, encoded_data]) self._decoded_data, self._encoded_data = ( self._decoder.Decode(self._encoded_data)) self._decoded_data_size = len(self._decoded_data) return read_count
Reads encoded data from the file-like object. Args: read_size (int): number of bytes of encoded data to read. Returns: int: number of bytes of encoded data read.
juraj-google-style
def spawn_reader_writer(get_data_fn, put_data_fn): def _reader_thread(): while True: out = get_data_fn() put_data_fn(out) if (not out): break t = threading.Thread(target=_reader_thread) t.daemon = True t.start() return t
Spawn a thread that reads from a data source and writes to a sink. The thread will terminate if it receives a Falsey value from the source. Args: get_data_fn: Data-reading function. Called repeatedly until it returns False-y to indicate that the thread should terminate. put_data_fn: Data-writing function. Returns: threading.Thread
codesearchnet
def save_hdf5(X, y, path): with h5py.File(path, 'w') as f: is_sparse = 1 if sparse.issparse(X) else 0 f['issparse'] = is_sparse f['target'] = y if is_sparse: if not sparse.isspmatrix_csr(X): X = X.tocsr() f['shape'] = np.array(X.shape) f['data'] = X.data f['indices'] = X.indices f['indptr'] = X.indptr else: f['data'] = X
Save data as a HDF5 file. Args: X (numpy or scipy sparse matrix): Data matrix y (numpy array): Target vector. path (str): Path to the HDF5 file to save data.
juraj-google-style
def Downsampled(cls, stats, interval=None): interval = interval or cls.DEFAULT_SAMPLING_INTERVAL result = cls(stats) result.cpu_samples = cls._Downsample( kind=CpuSample, samples=stats.cpu_samples, interval=interval) result.io_samples = cls._Downsample( kind=IOSample, samples=stats.io_samples, interval=interval) return result
Constructs a copy of given stats but downsampled to given interval. Args: stats: A `ClientStats` instance. interval: A downsampling interval. Returns: A downsampled `ClientStats` instance.
juraj-google-style
def delete(self, id_or_uri, timeout=-1): return self._client.delete(id_or_uri, timeout=timeout)
Deletes SNMPv1 trap forwarding destination based on {Id}. Args: id_or_uri: dict object to delete timeout: Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation in OneView, just stop waiting for its completion. Returns: bool: Indicates if the resource was successfully deleted.
juraj-google-style
def print_logs(redis_client, threads_stopped): pubsub_client = redis_client.pubsub(ignore_subscribe_messages=True) pubsub_client.subscribe(ray.gcs_utils.LOG_FILE_CHANNEL) localhost = services.get_node_ip_address() try: num_consecutive_messages_received = 0 while True: if threads_stopped.is_set(): return msg = pubsub_client.get_message() if msg is None: num_consecutive_messages_received = 0 threads_stopped.wait(timeout=0.01) continue num_consecutive_messages_received += 1 data = json.loads(ray.utils.decode(msg["data"])) if data["ip"] == localhost: for line in data["lines"]: print("{}{}(pid={}){} {}".format( colorama.Style.DIM, colorama.Fore.CYAN, data["pid"], colorama.Style.RESET_ALL, line)) else: for line in data["lines"]: print("{}{}(pid={}, ip={}){} {}".format( colorama.Style.DIM, colorama.Fore.CYAN, data["pid"], data["ip"], colorama.Style.RESET_ALL, line)) if (num_consecutive_messages_received % 100 == 0 and num_consecutive_messages_received > 0): logger.warning( "The driver may not be able to keep up with the " "stdout/stderr of the workers. To avoid forwarding logs " "to the driver, use 'ray.init(log_to_driver=False)'.") finally: pubsub_client.close()
Prints log messages from workers on all of the nodes. Args: redis_client: A client to the primary Redis shard. threads_stopped (threading.Event): A threading event used to signal to the thread that it should exit.
juraj-google-style
def _object_table(self, object_id): if not isinstance(object_id, ray.ObjectID): object_id = ray.ObjectID(hex_to_binary(object_id)) message = self._execute_command(object_id, "RAY.TABLE_LOOKUP", ray.gcs_utils.TablePrefix.OBJECT, "", object_id.binary()) if message is None: return {} gcs_entry = ray.gcs_utils.GcsTableEntry.GetRootAsGcsTableEntry( message, 0) assert gcs_entry.EntriesLength() > 0 entry = ray.gcs_utils.ObjectTableData.GetRootAsObjectTableData( gcs_entry.Entries(0), 0) object_info = { "DataSize": entry.ObjectSize(), "Manager": entry.Manager(), } return object_info
Fetch and parse the object table information for a single object ID. Args: object_id: An object ID to get information about. Returns: A dictionary with information about the object ID in question.
juraj-google-style
def run_validation(options): if (options.files == sys.stdin): results = validate(options.files, options) return [FileValidationResults(is_valid=results.is_valid, filepath='stdin', object_results=results)] files = get_json_files(options.files, options.recursive) results = [validate_file(fn, options) for fn in files] return results
Validate files based on command line options. Args: options: An instance of ``ValidationOptions`` containing options for this validation run.
codesearchnet
def extract(self, path_or_paths): with self._extractor.tqdm(): return _map_promise(self._extract, path_or_paths)
Extract given path(s). Args: path_or_paths: path or `list`/`dict` of path of file to extract. Each path can be a `str` or `tfds.download.Resource`. If not explicitly specified in `Resource`, the extraction method is deduced from downloaded file name. Returns: extracted_path(s): `str`, The extracted paths matching the given input path_or_paths.
juraj-google-style
def toString(self): output = '' if (self.childs or self.isOpeningTag()): output += self.tagToString() for c in self.childs: output += c.toString() if (self.endtag is not None): output += self.endtag.tagToString() elif (not self.isEndTag()): output += self.tagToString() return output
Returns almost original string. If you want prettified string, try :meth:`.prettify`. Returns: str: Complete representation of the element with childs, endtag \ and so on.
codesearchnet
def add_test_class(self, config, test_class, tests=None, name_suffix=None): if self._log_dir != config.log_path: raise Error('TestRunner\'s log folder is "%s", but a test config with a different log folder ("%s") was added.' % (self._log_dir, config.log_path)) if self._testbed_name != config.testbed_name: raise Error('TestRunner\'s test bed is "%s", but a test config with a different test bed ("%s") was added.' % (self._testbed_name, config.testbed_name)) self._test_run_infos.append(TestRunner._TestRunInfo(config=config, test_class=test_class, tests=tests, test_class_name_suffix=name_suffix))
Adds tests to the execution plan of this TestRunner. Args: config: config_parser.TestRunConfig, configuration to execute this test class with. test_class: class, test class to execute. tests: list of strings, optional list of test names within the class to execute. name_suffix: string, suffix to append to the class name for reporting. This is used for differentiating the same class executed with different parameters in a suite. Raises: Error: if the provided config has a log_path or testbed_name which differs from the arguments provided to this TestRunner's constructor.
github-repos
def WriteSessionCompletion(self, aborted=False): self._RaiseIfNotWritable() if self._storage_type != definitions.STORAGE_TYPE_SESSION: raise IOError('Unsupported storage type.') self._session.aborted = aborted session_completion = self._session.CreateSessionCompletion() self._storage_file.WriteSessionCompletion(session_completion)
Writes session completion information. Args: aborted (Optional[bool]): True if the session was aborted. Raises: IOError: if the storage type is not supported or when the storage writer is closed. OSError: if the storage type is not supported or when the storage writer is closed.
juraj-google-style
def _remove_structure_prefix(self, prefix, line): return line[len(prefix):].strip()
Helper function for removing the structure prefix for parsing. Args: prefix: string, a _InstrumentationStructurePrefixes to remove from the raw output. line: string, the raw line from the instrumentation output. Returns: A string containing a key value pair descripting some property of the current instrumentation test method.
github-repos
class InputExample: guid: str words: list[str] labels: Optional[list[str]]
A single training/test example for token classification. Args: guid: Unique id for the example. words: list. The words of the sequence. labels: (Optional) list. The labels for each word of the sequence. This should be specified for train and dev examples, but not for test examples.
github-repos
def as_proto_cls(proto_cls): def decorator(cls): 'Decorator applied to the class.' class ProtoCls(object): 'Base class simulating the protobuf.' def __init__(self, *args, **kwargs): super(ProtoCls, self).__setattr__('_ProtoCls__proto', proto_cls(*args, **kwargs)) def __getattr__(self, attr_name): return getattr(self.__proto, attr_name) def __setattr__(self, attr_name, new_value): try: return setattr(self.__proto, attr_name, new_value) except AttributeError: return super(ProtoCls, self).__setattr__(attr_name, new_value) def __eq__(self, other): return (self.__proto, other.get_proto()) def get_proto(self): return self.__proto def __repr__(self): return '<{cls_name}\n{proto_repr}\n>'.format(cls_name=cls.__name__, proto_repr=repr(self.__proto)) decorator_cls = type(cls.__name__, (cls, ProtoCls), {'__doc__': cls.__doc__}) return decorator_cls return decorator
Simulate proto inheritance. By default, protobuf do not support direct inheritance, so this decorator simulates inheritance to the class to which it is applied. Example: ``` @as_proto_class(proto.MyProto) class A(object): def custom_method(self): return self.proto_field * 10 p = proto.MyProto(proto_field=123) a = A() a.CopyFrom(p) # a is like a proto object assert a.proto_field == 123 a.custom_method() # But has additional methods ``` Args: proto_cls: The protobuf class to inherit from Returns: decorated_cls: The decorated class
codesearchnet
def linear(m=1, b=0): def f(i): return ((m * i) + b) return partial(force, sequence=_advance(f))
Return a driver function that can advance a sequence of linear values. .. code-block:: none value = m * i + b Args: m (float) : a slope for the linear driver x (float) : an offset for the linear driver
codesearchnet
def _overwrite_model_variables_with_average_value(self, trainable_variables): trainable_variables = [v.value if isinstance(v, backend.Variable) else v for v in trainable_variables] for var, average_var in zip(trainable_variables, self._model_variables_moving_average): self._distribution_strategy.extended.update(var, lambda a, b: a.assign(b), args=(average_var,))
Overwrite model variables with their moving average values. This function overwrites variables on each device. Args: var_list: list of model variables.
github-repos
def validate_options(options): if not options: return for k, v in options.iteritems(): if not isinstance(k, str): raise TypeError('option %r should be a str.' % k) if not any(k.lower().startswith(valid) for valid in _GCS_OPTIONS): raise ValueError('option %s is not supported.' % k) if not isinstance(v, basestring): raise TypeError('value %r for option %s should be of type basestring.' % (v, k))
Validate Google Cloud Storage options. Args: options: a str->basestring dict of options to pass to Google Cloud Storage. Raises: ValueError: if option is not supported. TypeError: if option is not of type str or value of an option is not of type basestring.
juraj-google-style
def set_storage(self, storage): if isinstance(storage, BaseStorage): self.storage = storage elif isinstance(storage, dict): if (('backend' not in storage) and ('root_dir' in storage)): storage['backend'] = 'FileSystem' try: backend_cls = getattr(storage_package, storage['backend']) except AttributeError: try: backend_cls = import_module(storage['backend']) except ImportError: self.logger.error('cannot find backend module %s', storage['backend']) sys.exit() kwargs = storage.copy() del kwargs['backend'] self.storage = backend_cls(**kwargs) else: raise TypeError('"storage" must be a storage object or dict')
Set storage backend for downloader For full list of storage backend supported, please see :mod:`storage`. Args: storage (dict or BaseStorage): storage backend configuration or instance
codesearchnet
def DeleteGRRTempFile(path): precondition.AssertType(path, Text) if (not os.path.isabs(path)): raise ErrorBadPath('Path must be absolute') prefix = config.CONFIG['Client.tempfile_prefix'] directories = [GetTempDirForRoot(root) for root in config.CONFIG['Client.tempdir_roots']] if (not _CheckIfPathIsValidForDeletion(path, prefix=prefix, directories=directories)): msg = "Can't delete temp file %s. Filename must start with %s or lie within any of %s." raise ErrorNotTempFile((msg % (path, prefix, ';'.join(directories)))) if os.path.exists(path): files.FILE_HANDLE_CACHE.Flush() os.remove(path) else: raise ErrorNotAFile(('%s does not exist.' % path))
Delete a GRR temp file. To limit possible damage the path must be absolute and either the file must be within any of the Client.tempdir_roots or the file name must begin with Client.tempfile_prefix. Args: path: path string to file to be deleted. Raises: OSError: Permission denied, or file not found. ErrorBadPath: Path must be absolute. ErrorNotTempFile: Filename must start with Client.tempfile_prefix. ErrorNotAFile: File to delete does not exist.
codesearchnet
def apply_with_summary(input_layer, operation, *op_args, **op_kwargs): return layers.apply_activation(input_layer.bookkeeper, input_layer.tensor, operation, activation_args=op_args, activation_kwargs=op_kwargs)
Applies the given operation to `input_layer` and create a summary. Args: input_layer: The input layer for this op. operation: An operation that takes a tensor and the supplied args. *op_args: Extra arguments for operation. **op_kwargs: Keyword arguments for the operation. Returns: A new layer with operation applied.
codesearchnet
def open(self, **params): logger.info('opening telnet') self.port = params['port'] self.ip = params['ip'] self.tn = None self._init()
Open telnet connection Args: params (dict), must contain two parameters "ip" - ip address or hostname and "port" - port number Example: params = {'port': 23, 'ip': 'localhost'}
codesearchnet
def resample(self, data, cache_dir=None, mask_area=None, **kwargs): if ((mask_area is None) and isinstance(self.source_geo_def, SwathDefinition)): mask_area = True if mask_area: if isinstance(self.source_geo_def, SwathDefinition): geo_dims = self.source_geo_def.lons.dims else: geo_dims = ('y', 'x') flat_dims = [dim for dim in data.dims if (dim not in geo_dims)] if np.issubdtype(data.dtype, np.integer): kwargs['mask'] = (data == data.attrs.get('_FillValue', np.iinfo(data.dtype.type).max)) else: kwargs['mask'] = data.isnull() kwargs['mask'] = kwargs['mask'].all(dim=flat_dims) cache_id = self.precompute(cache_dir=cache_dir, **kwargs) return self.compute(data, cache_id=cache_id, **kwargs)
Resample `data` by calling `precompute` and `compute` methods. Only certain resampling classes may use `cache_dir` and the `mask` provided when `mask_area` is True. The return value of calling the `precompute` method is passed as the `cache_id` keyword argument of the `compute` method, but may not be used directly for caching. It is up to the individual resampler subclasses to determine how this is used. Args: data (xarray.DataArray): Data to be resampled cache_dir (str): directory to cache precomputed results (default False, optional) mask_area (bool): Mask geolocation data where data values are invalid. This should be used when data values may affect what neighbors are considered valid. Returns (xarray.DataArray): Data resampled to the target area
codesearchnet
def GetNTFSFileEntryByPathSpec(self, path_spec): location = getattr(path_spec, 'location', None) mft_attribute = getattr(path_spec, 'mft_attribute', None) mft_entry = getattr(path_spec, 'mft_entry', None) if ((mft_attribute is not None) and (mft_entry is not None)): fsntfs_file_entry = self._fsntfs_volume.get_file_entry(mft_entry) elif (location is not None): fsntfs_file_entry = self._fsntfs_volume.get_file_entry_by_path(location) else: raise errors.PathSpecError('Path specification missing location and MFT entry.') return fsntfs_file_entry
Retrieves the NTFS file entry for a path specification. Args: path_spec (PathSpec): a path specification. Returns: pyfsntfs.file_entry: NTFS file entry. Raises: PathSpecError: if the path specification is missing location and MFT entry.
codesearchnet