code
stringlengths
20
4.93k
docstring
stringlengths
33
1.27k
source
stringclasses
3 values
def outer_graph(self): current = self._weak_outer_graph() if current is None: return self._fallback_outer_graph return current
The Graph this FuncGraph is nested in. Functions may capture Tensors from graphs they are nested in (transitive). Returns: A Graph object. Initially set to the current default graph when the FuncGraph was created. If the previous `outer_graph` was deleted because the function that owns it was deleted, `outer_graph` is reset to the outermost default graph active when the FuncGraph was created. This FuncGraph won't have captured anything from the new `outer_graph` (and likely not from the previous setting, since that would have created a strong reference), but it is returned so that FuncGraphs always have a parent.
github-repos
def to_value_set_codes(self, fhir_context: context.FhirPathContext) -> Optional[ValueSetCodes]: if self.code_values is not None: return ValueSetCodes(self.value_set_url, self.value_set_version, self.code_values) value_set_proto = fhir_context.get_value_set(self.value_set_url) if value_set_proto is None: return None return ValueSetCodes(self.value_set_url, value_set_proto.version.value or None, to_code_values(value_set_proto))
Builds a representation of the value set given to the memberOf call. If memberOf was called with a value set proto, returns a ValueSetCodes object using the fields from that proto. If memberOf was called with a URL string, attempt to retrieve a value set proto from `fhir_context` and use it to build the ValueSetCodes object. If the URL string can not be resolved in the given `fhir_context`, returns None. Args: fhir_context: The context to use when looking for value set definitions. Returns: The value set referenced by the memberOf call or None if the value set URL can not be resolved.
github-repos
def from_b58check(private_key): b58dec = base58.b58decode_check(private_key) version = b58dec[0] assert (version in [PrivateKey.TESTNET_VERSION, PrivateKey.MAINNET_VERSION]) return PrivateKey(int.from_bytes(b58dec[1:], 'big'))
Decodes a Base58Check encoded private-key. Args: private_key (str): A Base58Check encoded private key. Returns: PrivateKey: A PrivateKey object
codesearchnet
def find_all(self, selector, **kwargs): self.debug_log(('Finding elements with selector: %s' % selector)) raise_exception = kwargs.get('raise_exception', BROME_CONFIG['proxy_driver']['raise_exception']) self.debug_log(('effective raise_exception: %s' % raise_exception)) wait_until_present = kwargs.get('wait_until_present', BROME_CONFIG['proxy_driver']['wait_until_present_before_find']) self.debug_log(('effective wait_until_present: %s' % wait_until_present)) wait_until_visible = kwargs.get('wait_until_visible', BROME_CONFIG['proxy_driver']['wait_until_visible_before_find']) self.debug_log(('effective wait_until_visible: %s' % wait_until_visible)) _selector = Selector(self, selector) found = False if wait_until_visible: found = self.wait_until_visible(selector, raise_exception=False) if (wait_until_present and (not found)): found = self.wait_until_present(selector, raise_exception=raise_exception) if (not found): self.debug_log(('find_all (%s): No element found' % _selector)) return [] try: elements = getattr(self._driver, _selector.find_function)(_selector.get_selector()) except exceptions.NoSuchElementException: self.debug_log(('find_all (%s): No element found' % _selector)) self.print_javascript_error() if raise_exception: raise exceptions.NoSuchElementException(_selector) else: return [] if (type(elements) == list): if len(elements): self.debug_log(('find_all (%s): Element found' % _selector)) return ProxyElementList(elements, _selector, self) else: msg = ('find_all (%s): No element found' % _selector) self.debug_log(msg) self.print_javascript_error() if raise_exception: raise exceptions.NoSuchElementException(msg) else: return [] else: self.debug_log(('find_all (%s): Element found' % _selector)) return [ProxyElement(elements, _selector, self)]
Return all the elements found with a selector Args: selector (str): the selector used to find the element Kwargs: wait_until_present (bool) default configurable via proxy_driver:wait_until_present_before_find wait_until_visible (bool) default configurable via proxy_driver:wait_until_visible_before_find raise_exception (bool) default configurable via proxy_driver:raise_exception Returns: empty list if no element was found proxy_element_list when element are found Raises: this function might raise an exception depending on the raise_exception kwargs or the config proxy_driver:raise_exception
codesearchnet
def patch_fromText(self, textline): if type(textline) == unicode: textline = textline.encode("ascii") patches = [] if not textline: return patches text = textline.split('\n') while len(text) != 0: m = re.match("^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$", text[0]) if not m: raise ValueError("Invalid patch string: " + text[0]) patch = patch_obj() patches.append(patch) patch.start1 = int(m.group(1)) if m.group(2) == '': patch.start1 -= 1 patch.length1 = 1 elif m.group(2) == '0': patch.length1 = 0 else: patch.start1 -= 1 patch.length1 = int(m.group(2)) patch.start2 = int(m.group(3)) if m.group(4) == '': patch.start2 -= 1 patch.length2 = 1 elif m.group(4) == '0': patch.length2 = 0 else: patch.start2 -= 1 patch.length2 = int(m.group(4)) del text[0] while len(text) != 0: if text[0]: sign = text[0][0] else: sign = '' line = urllib.unquote(text[0][1:]) line = line.decode("utf-8") if sign == '+': patch.diffs.append((self.DIFF_INSERT, line)) elif sign == '-': patch.diffs.append((self.DIFF_DELETE, line)) elif sign == ' ': patch.diffs.append((self.DIFF_EQUAL, line)) elif sign == '@': break elif sign == '': pass else: raise ValueError("Invalid patch mode: '%s'\n%s" % (sign, line)) del text[0] return patches
Parse a textual representation of patches and return a list of patch objects. Args: textline: Text representation of patches. Returns: Array of Patch objects. Raises: ValueError: If invalid input.
juraj-google-style
def extract_channel(k, cdim): n = cdim perm = tuple(list(range(k)) + [n - 1] + list(range(k, n - 1))) return CPermutation.create(perm)
Create a :class:`CPermutation` that extracts channel `k` Return a permutation circuit that maps the k-th (zero-based) input to the last output, while preserving the relative order of all other channels. Args: k (int): Extracted channel index cdim (int): The circuit dimension (number of channels) Returns: Circuit: Permutation circuit
juraj-google-style
def install_requirements(self, path, index=None): cmd = 'install -r {0}'.format(path) if index: cmd = 'install --index-url {0} -r {1}'.format(index, path) self.pip(cmd)
Install packages from a requirements.txt file. Args: path (str): The path to the requirements file. index (str): The URL for a pypi index to use.
codesearchnet
def eulers_totient(n): if (not isinstance(n, int)): raise TypeError('Expecting a strictly positive integer') if (n <= 0): raise ValueError('Expecting a strictly positive integer') if (n == 1): return 1 result = 0 for i in range(1, n): if (gcd(i, n) == 1): result += 1 return result
Calculate the value of Euler's totient for a given integer Args: n (int): strictly positive integer Returns: The value of Euler's totient for n Raises: TypeError: If either n or k is not an integer ValueError: If either n or k is negative, or if k is strictly greater than n
codesearchnet
def parse_arguments(*args, **options): days = options.get('days', 1) enterprise_customer_uuid = options.get('enterprise_customer_uuid') enterprise_customer = None if enterprise_customer_uuid: try: enterprise_customer = EnterpriseCustomer.objects.get(uuid=enterprise_customer_uuid) except EnterpriseCustomer.DoesNotExist: raise CommandError('Enterprise customer with uuid "{enterprise_customer_uuid}" does not exist.'.format(enterprise_customer_uuid=enterprise_customer_uuid)) return (days, enterprise_customer)
Parse and validate arguments for send_course_enrollments command. Arguments: *args: Positional arguments passed to the command **options: optional arguments passed to the command Returns: A tuple containing parsed values for 1. days (int): Integer showing number of days to lookup enterprise enrollments, course completion etc and send to xAPI LRS 2. enterprise_customer_uuid (EnterpriseCustomer): Enterprise Customer if present then send xAPI statements just for this enterprise.
codesearchnet
def post_message(self, level, message, count=1, timestamp=None, now_reference=None): if ((len(self.messages) > 0) and (self.messages[(- 1)].message == message)): self.messages[(- 1)].count += 1 else: msg_object = ServiceMessage(level, message, self._last_message_id, timestamp, now_reference) msg_object.count = count self.messages.append(msg_object) self._last_message_id += 1 return self.messages[(- 1)]
Post a new message for service. Args: level (int): The level of the message (info, warning, error) message (string): The message contents count (int): The number of times the message has been repeated timestamp (float): An optional monotonic value in seconds for when the message was created now_reference (float): If timestamp is not relative to monotonic() as called from this module then this should be now() as seen by whoever created the timestamp. Returns: ServiceMessage: The posted message
codesearchnet
def get_sample(self, md5): sample = self.data_store.get_sample(md5) if not sample: return {'sample_set': {'md5_list': self.get_sample_set(md5)}} return {'sample': sample}
Get a sample from the DataStore. Args: md5: the md5 of the sample Returns: A dictionary of meta data about the sample which includes a ['raw_bytes'] key that contains the raw bytes. Raises: Workbench.DataNotFound if the sample is not found.
juraj-google-style
def make_list_of_op(tops, check_graph=True, allow_graph=True, ignore_ts=False): if isinstance(tops, ops.Graph): if allow_graph: return tops.get_operations() else: raise TypeError('allow_graph is False: cannot convert a tf.Graph.') else: if not is_iterable(tops): tops = [tops] if not tops: return [] if check_graph: check_types = None if ignore_ts else ops.Operation get_unique_graph(tops, check_types=check_types) return [op for op in tops if isinstance(op, ops.Operation)]
Convert ops to a list of `tf.Operation`. Args: tops: can be an iterable of `tf.Operation`, a `tf.Graph` or a single operation. check_graph: if `True` check if all the operations belong to the same graph. allow_graph: if `False` a `tf.Graph` cannot be converted. ignore_ts: if True, silently ignore `tf.Tensor`. Returns: A newly created list of `tf.Operation`. Raises: TypeError: if tops cannot be converted to a list of `tf.Operation` or, if `check_graph` is `True`, if all the ops do not belong to the same graph.
github-repos
def isdir(path): system = get_instance(path) return system.isdir(system.ensure_dir_path(path))
Return True if path is an existing directory. Equivalent to "os.path.isdir". Args: path (path-like object): Path or URL. Returns: bool: True if directory exists.
juraj-google-style
def create_base_for_fuse_batchnorm(self, pattern_match_mode='MATCH_ALL'): with self.cached_session() as sess: data_format = 'NHWC' if pattern_match_mode == 'MISMATCH_FORMAT': data_format = 'NCHW' inputs = [1, 4, 2, 5, 3, 6, -1, -4, -2, -5, -3, -6] input_op = constant_op.constant(np.array(inputs), shape=[1, 1, 6, 2] if data_format == 'NHWC' else [1, 2, 1, 6], dtype=dtypes.float32) weights = [1, 2, 3, 4, 0.1, 0.2, 0.3, 0.4] weights_op = constant_op.constant(np.array(weights), shape=[1, 2, 2, 2], dtype=dtypes.float32) conv_op = nn_ops.conv2d(input_op, weights_op, [1, 1, 1, 1], data_format=data_format, padding='SAME', name='conv_op') const_op_1 = None const_op_2 = constant_op.constant(1e-05, dtype=dtypes.float32) const_op_3 = None const_op_4 = None const_op_5 = None const_op_6 = None if data_format == 'NHWC': const_op_1 = constant_op.constant(np.array([0.25, 0.5]), shape=[2], dtype=dtypes.float32) const_op_3 = constant_op.constant(np.array([10, 20]), shape=[2], dtype=dtypes.float32) const_op_4 = constant_op.constant(np.array([0.1, 0.6]), shape=[2], dtype=dtypes.float32) const_op_5 = constant_op.constant(np.array([1.0, 2.0]), shape=[2], dtype=dtypes.float32) const_op_6 = constant_op.constant(np.array([0.2, 0.5]), shape=[2], dtype=dtypes.float32) else: const_op_1 = constant_op.constant(np.array([0.25, 0.5, 0.6, 0.7, 0.8, 0.9]), shape=[6], dtype=dtypes.float32) const_op_3 = constant_op.constant(np.array([10, 20, 30, 40, 50, 60]), shape=[6], dtype=dtypes.float32) const_op_4 = constant_op.constant(np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]), shape=[6], dtype=dtypes.float32) const_op_5 = constant_op.constant(np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]), shape=[6], dtype=dtypes.float32) const_op_6 = constant_op.constant(np.array([0.2, 0.4, 0.5, 0.6, 0.7, 0.8]), shape=[6], dtype=dtypes.float32) add_op_1 = gen_math_ops.add(const_op_1, const_op_2) rsqrt_op = math_ops.rsqrt(add_op_1) variable_op = None if pattern_match_mode == 'MATCH_NO_GAMMA': variable_op = rsqrt_op else: variable_op = math_ops.multiply(rsqrt_op, const_op_5) mul_op_1 = math_ops.multiply(conv_op, variable_op) mul_op_2 = None if pattern_match_mode == 'MISMATCH_PATTERN': mul_op_2 = math_ops.multiply(const_op_3, const_op_6) else: mul_op_2 = math_ops.multiply(const_op_3, variable_op) sub_op = math_ops.subtract(const_op_4, mul_op_2) if pattern_match_mode == 'MATCH_SWITCH_ORDER': gen_math_ops.add(sub_op, mul_op_1, name='output') else: gen_math_ops.add(mul_op_1, sub_op, name='output') test_util.set_producer_version(ops.get_default_graph(), 8) original_graph = sess.graph_def original_result = sess.run(['output:0']) return (original_graph, original_result)
Create testing graph and compute the result from original graph. Args: pattern_match_mode: A label string to indicate which batchnorm composition pattern to create in the resulting graph. "MATCH_ALL" - Create a graph matching the decomposed batchnorm pattern with full set of primitive ops. "MATCH_NO_GAMMA" - Create a graph matching the decomposed batchnorm pattern when gamma factor is 1 and multiplication with gamma is omitted. "MATCH_SWITCH_ORDER" - Create a graph matching the decomposed batchnorm pattern with a different order of inputs to the root Add node. "MISMATCH_PATTERN" - Create a graph with same set of primitive ops which makes up the decomposed batchnorm, but not matching the pattern. "MISMATCH_FORMAT" - Create a graph with NCHW format as input. Returns: A GraphDef as original graph to run the decomposed batchnorm test cases. Computation result from executing the original graph defined by GraphDef.
github-repos
def _run_query(client, query, job_config=None): start_time = time.time() query_job = client.query(query, job_config=job_config) print('Executing query with job ID: {}'.format(query_job.job_id)) while True: print('\rQuery executing: {:0.2f}s'.format((time.time() - start_time)), end='') try: query_job.result(timeout=0.5) break except futures.TimeoutError: continue print('\nQuery complete after {:0.2f}s'.format((time.time() - start_time))) return query_job
Runs a query while printing status updates Args: client (google.cloud.bigquery.client.Client): Client to bundle configuration needed for API requests. query (str): SQL query to be executed. Defaults to the standard SQL dialect. Use the ``job_config`` parameter to change dialects. job_config (google.cloud.bigquery.job.QueryJobConfig, optional): Extra configuration options for the job. Returns: google.cloud.bigquery.job.QueryJob: the query job created Example: >>> client = bigquery.Client() >>> _run_query(client, "SELECT 17") Executing query with job ID: bf633912-af2c-4780-b568-5d868058632b Query executing: 1.66s Query complete after 2.07s 'bf633912-af2c-4780-b568-5d868058632b'
codesearchnet
def get_video_transcript_data(video_id, language_code): video_transcript = VideoTranscript.get_or_none(video_id, language_code) if video_transcript: try: return dict(file_name=video_transcript.filename, content=video_transcript.transcript.file.read()) except Exception: logger.exception('[edx-val] Error while retrieving transcript for video=%s -- language_code=%s', video_id, language_code) raise
Get video transcript data Arguments: video_id(unicode): An id identifying the Video. language_code(unicode): it will be the language code of the requested transcript. Returns: A dict containing transcript file name and its content.
codesearchnet
def _JoinKeyPath(self, path_segments): path_segments = [ segment.split(definitions.KEY_PATH_SEPARATOR) for segment in path_segments] path_segments = [ element for sublist in path_segments for element in sublist] path_segments = filter(None, path_segments) return definitions.KEY_PATH_SEPARATOR.join(path_segments)
Joins the path segments into key path. Args: path_segments (list[str]): Windows Registry key path segments. Returns: str: key path.
juraj-google-style
def from_label(cls, label): z = np.zeros(len(label), dtype=np.bool) x = np.zeros(len(label), dtype=np.bool) for (i, char) in enumerate(label): if (char == 'X'): x[((- i) - 1)] = True elif (char == 'Z'): z[((- i) - 1)] = True elif (char == 'Y'): z[((- i) - 1)] = True x[((- i) - 1)] = True elif (char != 'I'): raise QiskitError("Pauli string must be only consisted of 'I', 'X', 'Y' or 'Z' but you have {}.".format(char)) return cls(z=z, x=x)
r"""Take pauli string to construct pauli. The qubit index of pauli label is q_{n-1} ... q_0. E.g., a pauli is $P_{n-1} \otimes ... \otimes P_0$ Args: label (str): pauli label Returns: Pauli: the constructed pauli Raises: QiskitError: invalid character in the label
codesearchnet
def GetName(self): if (self.AssetType == AssetType.GoverningToken): return 'NEO' elif (self.AssetType == AssetType.UtilityToken): return 'NEOGas' if (type(self.Name) is bytes): return self.Name.decode('utf-8') return self.Name
Get the asset name based on its type. Returns: str: 'NEO' or 'NEOGas'
codesearchnet
def box(self, x0, y0, width, height): assert width > 1 assert height > 1 width -= 1 height -= 1 for x in range(x0, x0 + width): self.point(x, y0, "-") self.point(x, y0 + height, "-") for y in range(y0, y0 + height): self.point(x0, y, "|") self.point(x0 + width, y, "|") self.point(x0, y0, "+") self.point(x0 + width, y0, "+") self.point(x0, y0 + height, "+") self.point(x0 + width, y0 + height, "+")
Create a box on ASCII canvas. Args: x0 (int): x coordinate of the box corner. y0 (int): y coordinate of the box corner. width (int): box width. height (int): box height.
juraj-google-style
def Process(self, parser_mediator, root_item=None, **kwargs): super(SummaryInformationOLECFPlugin, self).Process( parser_mediator, **kwargs) if not root_item: raise ValueError('Root item not set.') root_creation_time, root_modification_time = self._GetTimestamps(root_item) for item_name in self.REQUIRED_ITEMS: item = root_item.get_sub_item_by_name(item_name) if not item: continue summary_information = OLECFSummaryInformation(item) event_data = summary_information.GetEventData( data_type='olecf:summary_info') event_data.name = 'Summary Information' for property_name, date_time in iter( summary_information.date_time_properties.items()): date_time_description = self._DATE_TIME_DESCRIPTIONS.get( property_name, definitions.TIME_DESCRIPTION_UNKNOWN) event = OLECFSummaryInformationEvent(date_time, date_time_description) parser_mediator.ProduceEventWithEventData(event, event_data) if root_creation_time: date_time = dfdatetime_filetime.Filetime( timestamp=root_creation_time) event = OLECFSummaryInformationEvent( date_time, definitions.TIME_DESCRIPTION_CREATION) parser_mediator.ProduceEventWithEventData(event, event_data) if root_modification_time: date_time = dfdatetime_filetime.Filetime( timestamp=root_modification_time) event = OLECFSummaryInformationEvent( date_time, definitions.TIME_DESCRIPTION_MODIFICATION) parser_mediator.ProduceEventWithEventData(event, event_data)
Parses a summary information OLECF item. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. root_item (Optional[pyolecf.item]): root item of the OLECF file. Raises: ValueError: If the root item is not set.
juraj-google-style
def rollaxis(a, axis, start=0): if isinstance(a, np.ndarray): return np.rollaxis(a, axis, start) if axis not in range(a.ndim): raise ValueError( 'rollaxis: axis (%d) must be >=0 and < %d' % (axis, a.ndim)) if start not in range(a.ndim + 1): raise ValueError( 'rollaxis: start (%d) must be >=0 and < %d' % (axis, a.ndim+1)) axes = list(range(a.ndim)) axes.remove(axis) axes.insert(start, axis) return transpose(a, axes)
Roll the specified axis backwards, until it lies in a given position. Args: a (array_like): Input array. axis (int): The axis to roll backwards. The positions of the other axes do not change relative to one another. start (int, optional): The axis is rolled until it lies before this position. The default, 0, results in a "complete" roll. Returns: res (ndarray)
juraj-google-style
def symm_group_cubic(mat): sym_group = np.zeros([24, 3, 3]) sym_group[0, :] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] sym_group[1, :] = [[1, 0, 0], [0, -1, 0], [0, 0, -1]] sym_group[2, :] = [[-1, 0, 0], [0, 1, 0], [0, 0, -1]] sym_group[3, :] = [[-1, 0, 0], [0, -1, 0], [0, 0, 1]] sym_group[4, :] = [[0, -1, 0], [-1, 0, 0], [0, 0, -1]] sym_group[5, :] = [[0, -1, 0], [1, 0, 0], [0, 0, 1]] sym_group[6, :] = [[0, 1, 0], [-1, 0, 0], [0, 0, 1]] sym_group[7, :] = [[0, 1, 0], [1, 0, 0], [0, 0, -1]] sym_group[8, :] = [[-1, 0, 0], [0, 0, -1], [0, -1, 0]] sym_group[9, :] = [[-1, 0, 0], [0, 0, 1], [0, 1, 0]] sym_group[10, :] = [[1, 0, 0], [0, 0, -1], [0, 1, 0]] sym_group[11, :] = [[1, 0, 0], [0, 0, 1], [0, -1, 0]] sym_group[12, :] = [[0, 1, 0], [0, 0, 1], [1, 0, 0]] sym_group[13, :] = [[0, 1, 0], [0, 0, -1], [-1, 0, 0]] sym_group[14, :] = [[0, -1, 0], [0, 0, 1], [-1, 0, 0]] sym_group[15, :] = [[0, -1, 0], [0, 0, -1], [1, 0, 0]] sym_group[16, :] = [[0, 0, 1], [1, 0, 0], [0, 1, 0]] sym_group[17, :] = [[0, 0, 1], [-1, 0, 0], [0, -1, 0]] sym_group[18, :] = [[0, 0, -1], [1, 0, 0], [0, -1, 0]] sym_group[19, :] = [[0, 0, -1], [-1, 0, 0], [0, 1, 0]] sym_group[20, :] = [[0, 0, -1], [0, -1, 0], [-1, 0, 0]] sym_group[21, :] = [[0, 0, -1], [0, 1, 0], [1, 0, 0]] sym_group[22, :] = [[0, 0, 1], [0, -1, 0], [1, 0, 0]] sym_group[23, :] = [[0, 0, 1], [0, 1, 0], [-1, 0, 0]] mat = np.atleast_2d(mat) all_vectors = [] for sym in sym_group: for vec in mat: all_vectors.append(np.dot(sym, vec)) return np.unique(np.array(all_vectors), axis=0)
obtain cubic symmetric eqivalents of the list of vectors. Args: matrix (lattice matrix, n by 3 array/matrix) Return: cubic symmetric eqivalents of the list of vectors.
juraj-google-style
def __init__(self, line, line_num=-1, var_map=None): self.match = '' self.regex = '' self.regex_obj = None self.line_op = '' self.record_op = '' self.new_state = '' self.line_num = line_num line = line.strip() if not line: raise TextFSMTemplateError('Null data in FSMRule. Line: %s' % self.line_num) match_action = self.MATCH_ACTION.match(line) if match_action: self.match = match_action.group('match') else: self.match = line self.regex = self.match if var_map: try: self.regex = string.Template(self.match).substitute(var_map) except (ValueError, KeyError): raise TextFSMTemplateError( "Duplicate or invalid variable substitution: '%s'. Line: %s." % (self.match, self.line_num)) try: self.regex_obj = CopyableRegexObject(self.regex) except re.error: raise TextFSMTemplateError( "Invalid regular expression: '%s'. Line: %s." % (self.regex, self.line_num)) if not match_action: return action_re = self.ACTION_RE.match(match_action.group('action')) if not action_re: action_re = self.ACTION2_RE.match(match_action.group('action')) if not action_re: action_re = self.ACTION3_RE.match(match_action.group('action')) if not action_re: raise TextFSMTemplateError("Badly formatted rule '%s'. Line: %s." % (line, self.line_num)) if 'ln_op' in action_re.groupdict() and action_re.group('ln_op'): self.line_op = action_re.group('ln_op') if 'rec_op' in action_re.groupdict() and action_re.group('rec_op'): self.record_op = action_re.group('rec_op') if 'new_state' in action_re.groupdict() and action_re.group('new_state'): self.new_state = action_re.group('new_state') if self.line_op == 'Continue' and self.new_state: raise TextFSMTemplateError( "Action '%s' with new state %s specified. Line: %s." % (self.line_op, self.new_state, self.line_num)) if self.line_op != 'Error' and self.new_state: if not re.match(r'\w+', self.new_state): raise TextFSMTemplateError( 'Alphanumeric characters only in state names. Line: %s.' % (self.line_num))
Initialise a new rule object. Args: line: (str), a template rule line to parse. line_num: (int), Optional line reference included in error reporting. var_map: Map for template (${var}) substitutions. Raises: TextFSMTemplateError: If 'line' is not a valid format for a Value entry.
juraj-google-style
def __init__(self, dataset, class_weight=None, distribution=None): from keras.src.utils.module_utils import tensorflow as tf if not isinstance(dataset, (tf.data.Dataset, tf.distribute.DistributedDataset)): raise ValueError(f'Expected argument `dataset` to be a tf.data.Dataset. Received: {dataset}') if class_weight is not None: dataset = dataset.map(make_class_weight_map_fn(class_weight)).prefetch(tf.data.AUTOTUNE) if distribution is not None: dataset = distribution.distribute_dataset(dataset) self._dataset = dataset
Initialize the TFDatasetAdapter. Args: dataset: The input `tf.data.Dataset` instance. class_weight: A map where the keys are integer class ids and values are the class weights, e.g. `{0: 0.2, 1: 0.6, 2: 0.3}`. distribution: A `keras.distribution.Distribution` instance. Used to shard the input dataset into per worker/process dataset instance.
github-repos
def increase_volume(percentage): if ((percentage > 100) or (percentage < 0)): raise ValueError('percentage must be an integer between 0 and 100') if (system.get_name() == 'windows'): pass elif (system.get_name() == 'mac'): volume_int = (percentage / 10) old_volume = get() new_volume = (old_volume + volume_int) if (new_volume > 10): new_volume = 10 set_volume((new_volume * 10)) else: formatted = ('%d%%+' % percentage) sp.Popen(['amixer', '--quiet', 'sset', 'Master', formatted]).wait()
Increase the volume. Increase the volume by a given percentage. Args: percentage (int): The percentage (as an integer between 0 and 100) to increase the volume by. Raises: ValueError: if the percentage is >100 or <0.
codesearchnet
def filesizes(images): while True: img = yield marv.pull(images) if img is None: break yield marv.push(img.size)
Stat filesize of files. Args: images: stream of marv image files Returns: Stream of filesizes
juraj-google-style
def _print_results(file, status): file_color = c.Fore.GREEN status_color = c.Fore.RED if status == 'Success': status_color = c.Fore.GREEN elif status == 'Skipped': status_color = c.Fore.YELLOW print( '{}{!s:<13}{}{!s:<35}{}{!s:<8}{}{}'.format( c.Fore.CYAN, 'Downloading:', file_color, file, c.Fore.CYAN, 'Status:', status_color, status, ) )
Print the download results. Args: file (str): The filename. status (str): The file download status.
juraj-google-style
def exception(self, timeout=None): if (not self._completed.wait(timeout=timeout)): raise exceptions.TimeoutError('Timed out waiting for result.') if (self._result != self._SENTINEL): return None return self._exception
Return the exception raised by the call, if any. This blocks until the message has successfully been published, and returns the exception. If the call succeeded, return None. Args: timeout (Union[int, float]): The number of seconds before this call times out and raises TimeoutError. Raises: TimeoutError: If the request times out. Returns: Exception: The exception raised by the call, if any.
codesearchnet
def summarize_dist_params(dist, name, name_scope="dist_params"): with tf.compat.v1.name_scope(name_scope): tf.compat.v2.summary.histogram( name="{}/{}".format(name, "mean"), data=dist.mean(), step=tf.compat.v1.train.get_or_create_global_step()) tf.compat.v2.summary.histogram( name="{}/{}".format(name, "stddev"), data=dist.stddev(), step=tf.compat.v1.train.get_or_create_global_step())
Summarize the parameters of a distribution. Args: dist: A Distribution object with mean and standard deviation parameters. name: The name of the distribution. name_scope: The name scope of this summary.
juraj-google-style
def exists(self, filename): result = True for repo in self._children: if (not repo.exists(filename)): result = False return result
Report whether a file exists on all distribution points. Determines file type by extension. Args: filename: Filename you wish to check. (No path! e.g.: "AdobeFlashPlayer-14.0.0.176.pkg") Returns: Boolean
codesearchnet
def _set_mtu_to_nics(self, conf): for (dom_name, dom_spec) in conf.get('domains', {}).items(): for (idx, nic) in enumerate(dom_spec.get('nics', [])): net = self._get_net(conf, dom_name, nic) mtu = net.get('mtu', 1500) if (mtu != 1500): nic['mtu'] = mtu
For all the nics of all the domains in the conf that have MTU set, save the MTU on the NIC definition. Args: conf (dict): Configuration spec to extract the domains from Returns: None
codesearchnet
def transform_framerate(self, in_fps, out_fps): if in_fps <= 0 or out_fps <= 0: raise ValueError("Framerates must be positive, cannot transform %f -> %f" % (in_fps, out_fps)) ratio = in_fps / out_fps for line in self: line.start = int(round(line.start * ratio)) line.end = int(round(line.end * ratio))
Rescale all timestamps by ratio of in_fps/out_fps. Can be used to fix files converted from frame-based to time-based with wrongly assumed framerate. Arguments: in_fps (float) out_fps (float) Raises: ValueError: Non-positive framerate given.
juraj-google-style
def AssertIterableType(iterable, expected_item_type): if isinstance(iterable, collections.Iterator): message = 'Expected iterable container but got iterator `%s` instead' message %= iterable raise TypeError(message) AssertType(iterable, collections.Iterable) for item in iterable: AssertType(item, expected_item_type)
Ensures that given iterable container has certain type. Args: iterable: An iterable container to assert the type for. expected_item_type: An expected type of the container items. Raises: TypeError: If given container does is not an iterable or its items do not have the expected type.
codesearchnet
def builtin(cls, name): names = {'nsdoe': LEGEND__NSDOE, 'canstrat': LEGEND__Canstrat, 'nagmdm__6_2': LEGEND__NAGMDM__6_2, 'nagmdm__6_1': LEGEND__NAGMDM__6_1, 'nagmdm__4_3': LEGEND__NAGMDM__4_3, 'sgmc': LEGEND__SGMC} return cls.from_csv(text=names[name.lower()])
Generate a default legend. Args: name (str): The name of the legend you want. Not case sensitive. 'nsdoe': Nova Scotia Dept. of Energy 'canstrat': Canstrat 'nagmdm__6_2': USGS N. Am. Geol. Map Data Model 6.2 'nagmdm__6_1': USGS N. Am. Geol. Map Data Model 6.1 'nagmdm__4_3': USGS N. Am. Geol. Map Data Model 4.3 'sgmc': USGS State Geologic Map Compilation Default 'nagmdm__6_2'. Returns: Legend: The legend stored in `defaults.py`.
codesearchnet
def GetAttributeValuesString(self): attributes = [] for (attribute_name, attribute_value) in sorted(self.__dict__.items()): if ((attribute_name[0] == '_') or (attribute_value is None)): continue if isinstance(attribute_value, dict): attribute_value = sorted(attribute_value.items()) elif isinstance(attribute_value, py2to3.BYTES_TYPE): attribute_value = repr(attribute_value) attribute_string = '{0:s}: {1!s}'.format(attribute_name, attribute_value) attributes.append(attribute_string) return ', '.join(attributes)
Retrieves a comparable string of the attribute values. Returns: str: comparable string of the attribute values.
codesearchnet
def prepare_for_translation(localization_bundle_path): logging.info('Preparing for translation..') for strings_file in os.listdir(os.path.join(localization_bundle_path, DEFAULT_LANGUAGE_DIRECTORY_NAME)): if (not strings_file.endswith('.strings')): continue strings_path = os.path.join(localization_bundle_path, DEFAULT_LANGUAGE_DIRECTORY_NAME, strings_file) for lang_dir in os.listdir(localization_bundle_path): if ((lang_dir == DEFAULT_LANGUAGE_DIRECTORY_NAME) or lang_dir.startswith('.')): continue dest_strings_path = os.path.join(localization_bundle_path, lang_dir, strings_file) pending_path = (dest_strings_path + '.pending') excluded_path = (dest_strings_path + '.excluded') if (not os.path.exists(dest_strings_path)): open_strings_file(dest_strings_path, 'a').close() logging.info('Preparing diff for %s in %s', lang_dir, pending_path) localization_diff(strings_path, dest_strings_path, excluded_path, pending_path)
Prepares the localization bundle for translation. This means, after creating the strings files using genstrings.sh, this will produce '.pending' files, that contain the files that are yet to be translated. Args: localization_bundle_path (str): The path to the localization bundle.
codesearchnet
def __call__(self, environ, start_response): start_response('200 OK', [('Content-type', 'text/plain')]) self.last_request_uri = wsgiref.util.request_uri(environ) return [self._success_message.encode('utf-8')]
WSGI Callable. Args: environ (Mapping[str, Any]): The WSGI environment. start_response (Callable[str, list]): The WSGI start_response callable. Returns: Iterable[bytes]: The response body.
juraj-google-style
def flipcheck(content): punct = tamperdict = str.maketrans('', '', punct) tamperproof = content.translate(tamperdict) if "(╯°□°)╯︵" in tamperproof: if "┻┻" in tamperproof: length = 0 for letter in content: if letter == "━": length += 1.36 elif letter == "─": length += 1 elif letter == "-": length += 0.50 putitback = "┬" for i in range(int(length)): putitback += "─" putitback += "┬ ノ( ゜-゜ノ)" return putitback else: flipdict = str.maketrans( 'abcdefghijklmnopqrstuvwxyzɐqɔpǝɟbɥıظʞןɯuodbɹsʇnʌʍxʎz😅🙃😞😟😠😡☹🙁😱😨😰😦😧😢😓😥😭', 'ɐqɔpǝɟbɥıظʞןɯuodbɹsʇnʌʍxʎzabcdefghijklmnopqrstuvwxyz😄🙂🙂🙂🙂🙂🙂😀😀🙂😄🙂🙂😄😄😄😁' ) flipstart = content.index('︵') flipped = content[flipstart+1:] flipped = str.lower(flipped).translate(flipdict) putitback = ''.join(list(reversed(list(flipped)))) putitback += "ノ( ゜-゜ノ)" return putitback else: return False
Checks a string for anger and soothes said anger Args: content (str): The message to be flipchecked Returns: putitback (str): The righted table or text
juraj-google-style
def get_metalpdb_info(metalpdb_lig_file): pdb_metals = ['CU', 'ZN', 'MN', 'FE', 'MG', 'CO', 'SE', 'YB', 'SF4', 'FES', 'F3S', 'NI', 'FE2'] coordination_number = 0 endogenous_ligands = [] exogenous_ligands = [] ss = StructProp(ident='metalpdb', structure_path=metalpdb_lig_file, file_type='pdb') chain_id = op.basename(metalpdb_lig_file)[5] metal_id = (op.basename(metalpdb_lig_file).split('_')[2], op.basename(metalpdb_lig_file).split('_')[3]) for r in ss.parse_structure().first_model.get_residues(): return_id = (r.get_id(), r.get_resname()) if r.get_id()[0] != ' ': if not r.resname.strip() in pdb_metals and r.resname != 'HOH': exogenous_ligands.append(return_id) else: endogenous_ligands.append(return_id) for a in r.get_atom(): if not a.element in pdb_metals: coordination_number += 1 infodict = {metal_id: {'endogenous_ligands' : endogenous_ligands, 'exogenous_ligands' : exogenous_ligands, 'coordination_number': coordination_number}} return chain_id, infodict
Parse a MetalPDB .lig file and return a tuple of the chain ID it represents, along with metal binding information. Args: metalpdb_lig_file (str): Path to .lig file Returns: tuple: (str, dict) of the chain ID and the parsed metal binding site information
juraj-google-style
def create_html_from_fragment(tag): try: assert isinstance(tag, bs4.element.Tag) except AssertionError: raise TypeError try: assert (tag.find_all('body') == []) except AssertionError: raise ValueError soup = BeautifulSoup('<html><head></head><body></body></html>', 'html.parser') soup.body.append(tag) return soup
Creates full html tree from a fragment. Assumes that tag should be wrapped in a body and is currently not Args: tag: a bs4.element.Tag Returns:" bs4.element.Tag: A bs4 tag representing a full html document
codesearchnet
def repository_blob(self, sha, **kwargs): path = '/projects/%s/repository/blobs/%s' % (self.get_id(), sha) return self.manager.gitlab.http_get(path, **kwargs)
Return a file by blob SHA. Args: sha(str): ID of the blob **kwargs: Extra options to send to the server (e.g. sudo) Raises: GitlabAuthenticationError: If authentication is not correct GitlabGetError: If the server failed to perform the request Returns: dict: The blob content and metadata
juraj-google-style
def add_unref(self, timestamp: int) -> None: self._unref_times.append(timestamp)
Adds an unref to this tensor with the specified timestamp. Args: timestamp: Timestamp of object unreference as an integer.
github-repos
def run_scratch(self, path_to_scratch, num_cores=1, outname=None, outdir=None, force_rerun=False): if not outname: outname = self.project_name if not outdir: outdir = '' outname = op.join(outdir, outname) self.out_sspro = '{}.ss'.format(outname) self.out_sspro8 = '{}.ss8'.format(outname) self.out_accpro = '{}.acc'.format(outname) self.out_accpro20 = '{}.acc20'.format(outname) ssbio.utils.command_runner( shell_command='{} {} {} {}'.format(path_to_scratch, self.seq_file, outname, num_cores), force_rerun_flag=force_rerun, outfile_checker='{}.ss'.format(outname))
Run SCRATCH on the sequence_file that was loaded into the class. Args: path_to_scratch: Path to the SCRATCH executable, run_SCRATCH-1D_predictors.sh outname: Prefix to name the output files outdir: Directory to store the output files force_rerun: Flag to force rerunning of SCRATCH even if the output files exist Returns:
juraj-google-style
def _post(self, url, data, scope): self._create_session(scope) response = self.session.post(url, data=data) return response.status_code, response.text
Make a POST request using the session object to a Degreed endpoint. Args: url (str): The url to send a POST request to. data (str): The json encoded payload to POST. scope (str): Must be one of the scopes Degreed expects: - `CONTENT_PROVIDER_SCOPE` - `COMPLETION_PROVIDER_SCOPE`
juraj-google-style
def extract_changelog_items(text, tags): patterns = {x['header']: tag_re(x['tag']) for x in tags} items = {x['header']: [] for x in tags} curr_tag = None curr_text = '' for line in text.splitlines(): if not line.strip(): if curr_tag is not None: items[curr_tag].append(curr_text) curr_text = '' curr_tag = None for tag in tags: m = patterns[tag['header']].match(line) if m: if curr_tag is not None: items[curr_tag].append(curr_text) curr_text = '' curr_tag = tag['header'] line = m.group('text') break if curr_tag is not None: curr_text = '{} {}'.format(curr_text.strip(), line.strip()).strip() if curr_tag is not None: items[curr_tag].append(curr_text) return items
Extract all tagged items from text. Args: text (str): Text to extract the tagged items from. Each tagged item is a paragraph that starts with a tag. It can also be a text list item. Returns: tuple[list[str], list[str], list[str]]: A tuple of `(features, changes, fixes)` extracted from the given text. The tagged items are usually features/changes/fixes but it can be configured through `pelconf.yaml`.
juraj-google-style
def initialize_from_assignments(assignments, k, max_assign_weight=0.75): cells = len(assignments) init_W = np.zeros((k, cells)) for (i, a) in enumerate(assignments): init_W[(a, i)] = max_assign_weight for a2 in range(k): if (a2 != a): init_W[(a2, i)] = ((1 - max_assign_weight) / (k - 1)) return (init_W / init_W.sum(0))
Creates a weight initialization matrix from Poisson clustering assignments. Args: assignments (array): 1D array of integers, of length cells k (int): number of states/clusters max_assign_weight (float, optional): between 0 and 1 - how much weight to assign to the highest cluster. Default: 0.75 Returns: init_W (array): k x cells
codesearchnet
def ParseContainersTable(self, parser_mediator, database=None, table=None, **unused_kwargs): if (database is None): raise ValueError('Missing database value.') if (table is None): raise ValueError('Missing table value.') for esedb_record in table.records: if parser_mediator.abort: break record_values = self._GetRecordValues(parser_mediator, table.name, esedb_record) event_data = MsieWebCacheContainersEventData() event_data.container_identifier = record_values.get('ContainerId', None) event_data.directory = record_values.get('Directory', None) event_data.name = record_values.get('Name', None) event_data.set_identifier = record_values.get('SetId', None) timestamp = record_values.get('LastScavengeTime', None) if timestamp: date_time = dfdatetime_filetime.Filetime(timestamp=timestamp) event = time_events.DateTimeValuesEvent(date_time, 'Last Scavenge Time') parser_mediator.ProduceEventWithEventData(event, event_data) timestamp = record_values.get('LastAccessTime', None) if timestamp: date_time = dfdatetime_filetime.Filetime(timestamp=timestamp) event = time_events.DateTimeValuesEvent(date_time, definitions.TIME_DESCRIPTION_LAST_ACCESS) parser_mediator.ProduceEventWithEventData(event, event_data) container_identifier = record_values.get('ContainerId', None) container_name = record_values.get('Name', None) if ((not container_identifier) or (not container_name)): continue table_name = 'Container_{0:d}'.format(container_identifier) esedb_table = database.get_table_by_name(table_name) if (not esedb_table): parser_mediator.ProduceExtractionWarning('Missing table: {0:s}'.format(table_name)) continue self._ParseContainerTable(parser_mediator, esedb_table, container_name)
Parses the Containers table. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. database (Optional[pyesedb.file]): ESE database. table (Optional[pyesedb.table]): table. Raises: ValueError: if the database or table value is missing.
codesearchnet
def profile_update(self, profile): if (profile.get('install_json') is None): print('{}{}Missing install_json parameter for profile {}.'.format(c.Style.BRIGHT, c.Fore.YELLOW, profile.get('profile_name'))) self.profile_update_args_v2(profile) self.profile_update_args_v3(profile) self.profile_update_schema(profile)
Update an existing profile with new parameters or remove deprecated parameters. Args: profile (dict): The dictionary containting the profile settings.
codesearchnet
def exponential(x): return ops.exp(x)
Exponential activation function. Args: x: Input tensor.
github-repos
def ensure_dir_path(self, path, relative=False): if not relative: rel_path = self.relpath(path) else: rel_path = path if self.is_locator(rel_path, relative=True): path = path.rstrip('/') elif rel_path: path = path.rstrip('/') + '/' return path
Ensure the path is a dir path. Should end with '/' except for schemes and locators. Args: path (str): Path or URL. relative (bool): Path is relative to current root. Returns: path: dir path
juraj-google-style
def get_items_for_config_file_output(self, source_to_settings, parsed_namespace): config_file_items = OrderedDict() for source, settings in source_to_settings.items(): if source == _COMMAND_LINE_SOURCE_KEY: _, existing_command_line_args = settings[''] for action in self._actions: config_file_keys = self.get_possible_config_keys(action) if config_file_keys and not action.is_positional_arg and \ already_on_command_line(existing_command_line_args, action.option_strings): value = getattr(parsed_namespace, action.dest, None) if value is not None: if isinstance(value, bool): value = str(value).lower() config_file_items[config_file_keys[0]] = value elif source == _ENV_VAR_SOURCE_KEY: for key, (action, value) in settings.items(): config_file_keys = self.get_possible_config_keys(action) if config_file_keys: value = getattr(parsed_namespace, action.dest, None) if value is not None: config_file_items[config_file_keys[0]] = value elif source.startswith(_CONFIG_FILE_SOURCE_KEY): for key, (action, value) in settings.items(): config_file_items[key] = value elif source == _DEFAULTS_SOURCE_KEY: for key, (action, value) in settings.items(): config_file_keys = self.get_possible_config_keys(action) if config_file_keys: value = getattr(parsed_namespace, action.dest, None) if value is not None: config_file_items[config_file_keys[0]] = value return config_file_items
Converts the given settings back to a dictionary that can be passed to ConfigFormatParser.serialize(..). Args: source_to_settings: the dictionary described in parse_known_args() parsed_namespace: namespace object created within parse_known_args() Returns: an OrderedDict where keys are strings and values are either strings or lists
juraj-google-style
def save(tiff_filename, numpy_data): tiff_filename = os.path.expanduser(tiff_filename) if type(numpy_data) is str: fp = open(png_filename, "wb") fp.write(numpy_data) fp.close() return png_filename try: img = tiff.imsave(tiff_filename, numpy_data) except Exception as e: raise ValueError("Could not save TIFF file {0}.".format(tiff_filename)) return tiff_filename
Export a numpy array to a TIFF file. Arguments: tiff_filename: A filename to which to save the TIFF data numpy_data: The numpy array to save to TIFF Returns: String. The expanded filename that now holds the TIFF data
juraj-google-style
def _ReadParserPresetValues(self, preset_definition_values): if (not preset_definition_values): raise errors.MalformedPresetError('Missing preset definition values.') name = preset_definition_values.get('name', None) if (not name): raise errors.MalformedPresetError('Invalid preset definition missing name.') parsers = preset_definition_values.get('parsers', None) if (not parsers): raise errors.MalformedPresetError('Invalid preset definition missing parsers.') parser_preset = ParserPreset(name, parsers) for operating_system_values in preset_definition_values.get('operating_systems', []): operating_system = self._ReadOperatingSystemArtifactValues(operating_system_values) parser_preset.operating_systems.append(operating_system) return parser_preset
Reads a parser preset from a dictionary. Args: preset_definition_values (dict[str, object]): preset definition values. Returns: ParserPreset: a parser preset. Raises: MalformedPresetError: if the format of the preset definition is not set or incorrect, or the preset of a specific operating system has already been set.
codesearchnet
def compute_inv_covariance(L_aug, Y, k, p): return np.linalg.inv(compute_covariance(L_aug, Y, k, p))
Given label matrix L and labels Y, compute the covariance. Args: L: (np.array) [n, d] The augmented (indicator) label matrix Y: (np.array int) [n] The true labels in {1,...,k}
codesearchnet
def enhance_pubmed_annotations(pubmed: Mapping[str, Any]) -> Mapping[str, Any]: text = pubmed["title"] + pubmed["abstract"] annotations = {} for nsarg in pubmed["annotations"]: url = f'{config["bel_api"]["servers"]["api_url"]}/terms/{url_path_param_quoting(nsarg)}' log.info(f"URL: {url}") r = get_url(url) log.info(f"Result: {r}") new_nsarg = "" if r and r.status_code == 200: term = r.json() new_nsarg = bel_utils.convert_nsarg(term["id"], decanonicalize=True) pubmed["annotations"][nsarg]["name"] = term["name"] pubmed["annotations"][nsarg]["label"] = term["label"] pubmed["annotations"][nsarg]["entity_types"] = list( set( pubmed["annotations"][nsarg]["entity_types"] + term.get("entity_types", []) ) ) pubmed["annotations"][nsarg]["annotation_types"] = list( set( pubmed["annotations"][nsarg]["annotation_types"] + term.get("annotation_types", []) ) ) if new_nsarg != nsarg: annotations[new_nsarg] = copy.deepcopy(pubmed["annotations"][nsarg]) else: annotations[nsarg] = copy.deepcopy(pubmed["annotations"][nsarg]) for nsarg in annotations: for idx, span in enumerate(annotations[nsarg]["spans"]): string = text[span["begin"] - 1 : span["end"] - 1] annotations[nsarg]["spans"][idx]["text"] = string pubmed["annotations"] = copy.deepcopy(annotations) return pubmed
Enhance pubmed namespace IDs Add additional entity and annotation types to annotations Use preferred id for namespaces as needed Add strings from Title, Abstract matching Pubtator BioConcept spans NOTE - basically duplicated code with bel_api:api.services.pubmed Args: pubmed Returns: pubmed object
juraj-google-style
def set_server_def_retries(retries): context().set_server_def_retries(retries)
Set the number of retries to use when calling SetServerDef. In cases where many servers run in high-preemption environments, jobs could be preempted during startup and initial connection via SetServerDef. Retries allow for more robust connection in these environments. Args: retries: int specifying the number of connection retries before failing. Retries follow an exponential backoff waiting period with min value 1ms, max value 10s, and exponent 1.3.
github-repos
def create_sns_event(app_name, env, region, rules): session = boto3.Session(profile_name=env, region_name=region) sns_client = session.client('sns') topic_name = rules.get('topic') lambda_alias_arn = get_lambda_alias_arn(app=app_name, account=env, region=region) topic_arn = get_sns_topic_arn(topic_name=topic_name, account=env, region=region) protocol = 'lambda' statement_id = '{}_sns_{}'.format(app_name, topic_name) principal = 'sns.amazonaws.com' add_lambda_permissions( function=lambda_alias_arn, statement_id=statement_id, action='lambda:InvokeFunction', principal=principal, source_arn=topic_arn, env=env, region=region) sns_client.subscribe(TopicArn=topic_arn, Protocol=protocol, Endpoint=lambda_alias_arn) LOG.debug("SNS Lambda event created") LOG.info("Created SNS event subscription on topic %s", topic_name)
Create SNS lambda event from rules. Args: app_name (str): name of the lambda function env (str): Environment/Account for lambda function region (str): AWS region of the lambda function rules (str): Trigger rules from the settings
juraj-google-style
def phase_crossings(ts, phi=0.0): ts = ts.squeeze() if (ts.ndim is not 1): raise ValueError('Currently can only use on single variable timeseries') ts = mod2pi((ts - phi)) tsa = ts[0:(- 1)] tsb = ts[1:] p2 = (np.pi / 2) zc = (np.nonzero((((((tsa > (- p2)) & (tsa < 0)) & (tsb >= 0)) & (tsb < p2)) | ((((tsa < p2) & (tsa > 0)) & (tsb <= 0)) & (tsb > (- p2)))))[0] + 1) va = ts[(zc - 1)] vb = ts[zc] ct = (((np.abs(vb) * ts.tspan[(zc - 1)]) + (np.abs(va) * ts.tspan[zc])) / np.abs((vb - va))) if (ts[0] == 0.0): zc = np.r_[(np.array([0]), zc)] ct = np.r_[(np.array([ts.tspan[0]]), ct)] pc = (np.nonzero((((tsa > p2) & (tsb < (- p2))) | ((tsa < (- p2)) & (tsb > p2))))[0] + 1) splice = np.searchsorted(pc, zc) which_zc = np.r_[(np.array([0]), (np.nonzero((splice[0:(- 1)] - splice[1:]))[0] + 1))] if (ct.shape[0] is 0): return ct else: return ct[which_zc]
For a single variable timeseries representing the phase of an oscillator, find the times at which the phase crosses angle phi, with the condition that the phase must visit phi+pi between crossings. (Thus if noise causes the phase to wander back and forth across angle phi without the oscillator doing a full revolution, then this is recorded as a single crossing event, giving the time of the earliest arrival.) If the timeseries begins (or ends) exactly at phi, then time zero (or the ending time) is also included as a crossing event, so that the boundaries of the first and last oscillations are included. If the actual crossing time falls between two time steps, linear interpolation is used to estimate the crossing time. Arguments: ts: Timeseries (single variable) The timeseries of an angle variable (radians) phi (float): Critical phase angle (radians) at which to report crossings. Returns: array of float
codesearchnet
def get_processors(processor_cat, prop_defs, data_attr=None): processor_defs = prop_defs.get(processor_cat, []) processor_list = [] for processor in processor_defs: proc_class = PropertyProcessor[processor['rdf_type'][0]] processor_list.append(proc_class(processor.get('kds_params', [{}]), data_attr)) return processor_list
reads the prop defs and adds applicable processors for the property Args: processor_cat(str): The category of processors to retreive prop_defs: property defintions as defined by the rdf defintions data_attr: the attr to manipulate during processing. Returns: list: a list of processors
codesearchnet
def load(self, cellpy_file, parent_level="CellpyData"): try: self.logger.debug("loading cellpy-file (hdf5):") self.logger.debug(cellpy_file) new_datasets = self._load_hdf5(cellpy_file, parent_level) self.logger.debug("cellpy-file loaded") except AttributeError: new_datasets = [] self.logger.warning("This cellpy-file version is not supported by" "current reader (try to update cellpy).") if new_datasets: for dataset in new_datasets: self.datasets.append(dataset) else: self.logger.warning("Could not load") self.logger.warning(str(cellpy_file)) self.number_of_datasets = len(self.datasets) self.status_datasets = self._validate_datasets() self._invent_a_name(cellpy_file) return self
Loads a cellpy file. Args: cellpy_file (path, str): Full path to the cellpy file. parent_level (str, optional): Parent level
juraj-google-style
def format_info(variant, variant_type='snv'): observations = variant.get('observations',0) homozygotes = variant.get('homozygote') hemizygotes = variant.get('hemizygote') vcf_info = f"Obs={observations}" if homozygotes: vcf_info += f";Hom={homozygotes}" if hemizygotes: vcf_info += f";Hem={hemizygotes}" if variant_type == 'sv': end = int((variant['end_left'] + variant['end_right'])/2) vcf_info += f";SVTYPE={variant['sv_type']};END={end};SVLEN={variant['length']}" return vcf_info
Format the info field for SNV variants Args: variant(dict) variant_type(str): snv or sv Returns: vcf_info(str): A VCF formated info field
juraj-google-style
def default(self, obj): if isinstance(obj, datetime): return obj.isoformat() if issubclass(obj.__class__, Enum.__class__): return obj.value to_json = getattr(obj, 'to_json', None) if to_json: out = obj.to_json() if issubclass(obj.__class__, Model): out.update({'__type': obj.__class__.__name__}) return out return JSONEncoder.default(self, obj)
Default object encoder function Args: obj (:obj:`Any`): Object to be serialized Returns: JSON string
codesearchnet
def umask(self, new_mask): if not is_int_type(new_mask): raise TypeError('an integer is required') old_umask = self.filesystem.umask self.filesystem.umask = new_mask return old_umask
Change the current umask. Args: new_mask: (int) The new umask value. Returns: The old umask. Raises: TypeError: if new_mask is of an invalid type.
juraj-google-style
def write_int8(self, value, little_endian=True): if little_endian: endian = "<" else: endian = ">" return self.pack('%sb' % endian, value)
Pack the value as a signed byte and write 1 byte to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
juraj-google-style
def absent(name): ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''} comment_bridge_deleted = 'Bridge {0} deleted.'.format(name) comment_bridge_notdeleted = 'Unable to delete bridge: {0}.'.format(name) comment_bridge_notexists = 'Bridge {0} does not exist.'.format(name) changes_bridge_deleted = {name: {'old': 'Bridge {0} exists.'.format(name), 'new': 'Bridge {0} deleted.'.format(name), } } bridge_exists = __salt__['openvswitch.bridge_exists'](name) if __opts__['test']: if not bridge_exists: ret['result'] = True ret['comment'] = comment_bridge_notexists else: ret['result'] = None ret['comment'] = comment_bridge_deleted return ret if not bridge_exists: ret['result'] = True ret['comment'] = comment_bridge_notexists else: bridge_delete = __salt__['openvswitch.bridge_delete'](name) if bridge_delete: ret['result'] = True ret['comment'] = comment_bridge_deleted ret['changes'] = changes_bridge_deleted else: ret['result'] = False ret['comment'] = comment_bridge_notdeleted return ret
Ensures that the named bridge does not exist, eventually deletes it. Args: name: The name of the bridge.
juraj-google-style
def downsample(data, percent): n_genes = data.shape[0] n_cells = data.shape[1] new_data = data.copy() total_count = float(data.sum()) to_remove = (total_count * percent) cell_sums = data.sum(0).astype(float) cell_gene_probs = (data / cell_sums) cell_probs = np.array((cell_sums / total_count)).flatten() cells_selected = np.random.multinomial(to_remove, pvals=cell_probs) for (i, num_selected) in enumerate(cells_selected): cell_gene = np.array(cell_gene_probs[(:, i)]).flatten() genes_selected = np.random.multinomial(num_selected, pvals=cell_gene) if sparse.issparse(data): genes_selected = sparse.csc_matrix(genes_selected).T new_data[(:, i)] -= genes_selected new_data[(new_data < 0)] = 0 return new_data
downsample the data by removing a given percentage of the reads. Args: data: genes x cells array or sparse matrix percent: float between 0 and 1
codesearchnet
def read_header(self, file_handle, nextdata_offset=0): header = {'FCS format': file_handle.read(6)} file_handle.read(4) for field in ('text start', 'text end', 'data start', 'data end', 'analysis start', 'analysis end'): s = file_handle.read(8) try: field_value = int(s) except ValueError: field_value = 0 header[field] = (field_value + nextdata_offset) for k in ('text start', 'text end'): if (header[k] == 0): raise ValueError(u'The FCS file "{}" seems corrupted. (Parser cannot locate information about the "{}" segment.)'.format(self.path, k)) elif (header[k] > self._file_size): raise ValueError(u'The FCS file "{}" is corrupted. "{}" segment is larger than file size'.format(self.path, k)) else: pass self._data_start = header['data start'] self._data_end = header['data start'] if ((header['analysis end'] - header['analysis start']) != 0): warnings.warn(u'There appears to be some information in the ANALYSIS segment of file {0}. However, it might not be read correctly.'.format(self.path)) self.annotation['__header__'] = header
Read the header of the FCS file. The header specifies where the annotation, data and analysis are located inside the binary file. Args: file_handle: buffer containing FCS file. nextdata_offset: byte offset of a set header from file start specified by $NEXTDATA
codesearchnet
def _flatten_beam_dim(tensor): shape = _shape_list(tensor) shape[0] *= shape[1] shape.pop(1) return tf.reshape(tensor, shape)
Reshapes first two dimensions in to single dimension. Args: tensor: Tensor to reshape of shape [A, B, ...] Returns: Reshaped tensor of shape [A*B, ...]
juraj-google-style
def update_hash(a_hash, mv): if mv.labels: signing.add_dict_to_hash(a_hash, encoding.MessageToPyValue(mv.labels)) money_value = mv.get_assigned_value(u'moneyValue') if money_value is not None: a_hash.update(b'\x00') a_hash.update(money_value.currencyCode.encode('utf-8'))
Adds ``mv`` to ``a_hash`` Args: a_hash (`Hash`): the secure hash, e.g created by hashlib.md5 mv (:class:`MetricValue`): the instance to add to the hash
juraj-google-style
def CheckTaskToMerge(self, task): with self._lock: is_abandoned = (task.identifier in self._tasks_abandoned) is_processing = (task.identifier in self._tasks_processing) is_queued = (task.identifier in self._tasks_queued) if ((not is_queued) and (not is_processing) and (not is_abandoned)): raise KeyError('Status of task {0:s} is unknown.'.format(task.identifier)) return (is_queued or is_processing or (is_abandoned and (not task.has_retry)))
Checks if the task should be merged. Args: task (Task): task. Returns: bool: True if the task should be merged. Raises: KeyError: if the task was not queued, processing or abandoned.
codesearchnet
def timestamp(method='iso8601'): if method == 'iso8601': tz_hour = time.timezone utc_offset = str(tz_hour) if tz_hour < 0 else '+' + str(tz_hour) stamp = time.strftime('%Y-%m-%dT%H%M%S') + utc_offset return stamp else: raise ValueError('only iso8601 is accepted for now')
make an iso8601 timestamp Args: method (str): type of timestamp Example: >>> stamp = timestamp() >>> print('stamp = {!r}'.format(stamp)) stamp = ...-...-...T...
juraj-google-style
def collapse_phenotypes(self, input_phenotype_labels, output_phenotype_label, verbose=True): if isinstance(input_phenotype_labels, str): input_phenotype_labels = [input_phenotype_labels] bad_phenotypes = (set(input_phenotype_labels) - set(self.phenotypes)) if (len(bad_phenotypes) > 0): raise ValueError((('Error phenotype(s) ' + str(bad_phenotypes)) + ' are not in the data.')) data = self.copy() if (len(input_phenotype_labels) == 0): return data def _swap_in(d, inputs, output): overlap = set(d.keys()).intersection(inputs) if (len(overlap) == 0): return d keepers = [(k, v) for (k, v) in d.items() if (k not in inputs)] return dict((keepers + [(output_phenotype_label, max([d[x] for x in overlap]))])) data['phenotype_calls'] = data.apply((lambda x: _swap_in(x['phenotype_calls'], input_phenotype_labels, output_phenotype_label)), 1) def _set_label(d): vals = [k for (k, v) in d.items() if (v == 1)] return (np.nan if (len(vals) == 0) else vals[0]) data['phenotype_label'] = data.apply((lambda x: _set_label(x['phenotype_calls'])), 1) return data
Rename one or more input phenotypes to a single output phenotype Args: input_phenotype_labels (list): A str name or list of names to combine output_phenotype_label (list): A str name to change the phenotype names to verbose (bool): output more details Returns: CellDataFrame: The CellDataFrame modified.
codesearchnet
def track_metric(self, name, value, type=None, count=None, min=None, max=None, std_dev=None, properties=None): dataPoint = channel.contracts.DataPoint() dataPoint.name = (name or NULL_CONSTANT_STRING) dataPoint.value = (value or 0) dataPoint.kind = (type or channel.contracts.DataPointType.aggregation) dataPoint.count = count dataPoint.min = min dataPoint.max = max dataPoint.std_dev = std_dev data = channel.contracts.MetricData() data.metrics.append(dataPoint) if properties: data.properties = properties self.track(data, self._context)
Send information about a single metric data point that was captured for the application. Args: name (str). the name of the metric that was captured.\n value (float). the value of the metric that was captured.\n type (:class:`channel.contracts.DataPointType`). the type of the metric. (defaults to: :func:`channel.contracts.DataPointType.aggregation`)\n count (int). the number of metrics that were aggregated into this data point. (defaults to: None)\n min (float). the minimum of all metrics collected that were aggregated into this data point. (defaults to: None)\n max (float). the maximum of all metrics collected that were aggregated into this data point. (defaults to: None)\n std_dev (float). the standard deviation of all metrics collected that were aggregated into this data point. (defaults to: None)\n properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)
codesearchnet
def get_cytoband_coord(chrom, pos): chrom = chrom.strip('chr') pos = int(pos) result = None logger.debug("Finding Cytoband for chrom:{0} pos:{1}".format(chrom, pos)) if chrom in CYTOBANDS: for interval in CYTOBANDS[chrom][pos]: result = "{0}{1}".format(chrom, interval.data) return result
Get the cytoband coordinate for a position Args: chrom(str): A chromosome pos(int): The position Returns: cytoband
juraj-google-style
def run_one_step(self, eig_init_vec_val, eig_num_iter_val, smooth_val, penalty_val, learning_rate_val): step_feed_dict = {self.eig_init_vec_placeholder: eig_init_vec_val, self.eig_num_iter_placeholder: eig_num_iter_val, self.smooth_placeholder: smooth_val, self.penalty_placeholder: penalty_val, self.learning_rate: learning_rate_val} if self.params['eig_type'] == 'SCIPY': current_eig_vector, self.current_eig_val_estimate = self.get_scipy_eig_vec() step_feed_dict.update({ self.eig_vec_estimate: current_eig_vector }) elif self.params['eig_type'] == 'LZS': step_feed_dict.update({ self.dual_object.m_min_vec_ph: self.dual_object.m_min_vec_estimate }) self.sess.run(self.train_step, feed_dict=step_feed_dict) [ _, self.dual_object.m_min_vec_estimate, self.current_eig_val_estimate ] = self.sess.run([ self.proj_step, self.eig_vec_estimate, self.eig_val_estimate ], feed_dict=step_feed_dict) if self.current_step % self.params['print_stats_steps'] == 0: [self.current_total_objective, self.current_unconstrained_objective, self.dual_object.m_min_vec_estimate, self.current_eig_val_estimate, self.current_nu] = self.sess.run( [self.total_objective, self.dual_object.unconstrained_objective, self.eig_vec_estimate, self.eig_val_estimate, self.dual_object.nu], feed_dict=step_feed_dict) stats = { 'total_objective': float(self.current_total_objective), 'unconstrained_objective': float(self.current_unconstrained_objective), 'min_eig_val_estimate': float(self.current_eig_val_estimate) } tf.logging.info('Current inner step: %d, optimization stats: %s', self.current_step, stats) if self.params['stats_folder'] is not None: stats = json.dumps(stats) filename = os.path.join(self.params['stats_folder'], str(self.current_step) + '.json') with tf.gfile.Open(filename) as file_f: file_f.write(stats) if self.current_step % self.params['projection_steps'] == 0 and self.current_unconstrained_objective < 0: nu = self.sess.run(self.dual_object.nu) dual_feed_dict = { self.dual_object.h_min_vec_ph: self.dual_object.h_min_vec_estimate } _, min_eig_val_h_lz = self.dual_object.get_lanczos_eig(compute_m=False, feed_dict=dual_feed_dict) projected_dual_feed_dict = { self.dual_object.projected_dual.nu: nu, self.dual_object.projected_dual.min_eig_val_h: min_eig_val_h_lz } if self.dual_object.projected_dual.compute_certificate(self.current_step, projected_dual_feed_dict): return True return False
Run one step of gradient descent for optimization. Args: eig_init_vec_val: Start value for eigen value computations eig_num_iter_val: Number of iterations to run for eigen computations smooth_val: Value of smoothness parameter penalty_val: Value of penalty for the current step learning_rate_val: Value of learning rate Returns: found_cert: True is negative certificate is found, False otherwise
juraj-google-style
async def complete_task(context, result): args = [get_task_id(context.claim_task), get_run_id(context.claim_task)] reversed_statuses = get_reversed_statuses(context) try: if result == 0: log.info("Reporting task complete...") response = await context.temp_queue.reportCompleted(*args) elif result != 1 and result in reversed_statuses: reason = reversed_statuses[result] log.info("Reporting task exception {}...".format(reason)) payload = {"reason": reason} response = await context.temp_queue.reportException(*args, payload) else: log.info("Reporting task failed...") response = await context.temp_queue.reportFailed(*args) log.debug("Task status response:\n{}".format(pprint.pformat(response))) except taskcluster.exceptions.TaskclusterRestFailure as exc: if exc.status_code == 409: log.info("409: not reporting complete/failed.") else: raise
Mark the task as completed in the queue. Decide whether to call reportCompleted, reportFailed, or reportException based on the exit status of the script. If the task has expired or been cancelled, we'll get a 409 status. Args: context (scriptworker.context.Context): the scriptworker context. Raises: taskcluster.exceptions.TaskclusterRestFailure: on non-409 error.
juraj-google-style
def Filter(fn, *args, **kwargs): if not callable(fn): raise TypeError('Filter can be used only with callable objects. Received %r instead.' % fn) wrapper = lambda x, *args, **kwargs: [x] if fn(x, *args, **kwargs) else [] label = 'Filter(%s)' % ptransform.label_from_callable(fn) if hasattr(fn, '__name__'): wrapper.__name__ = fn.__name__ fn_type_hints = typehints.decorators.IOTypeHints.from_callable(fn) if fn_type_hints is not None: fn_type_hints = fn_type_hints.with_output_types() type_hints = get_type_hints(fn).with_defaults(fn_type_hints) if type_hints.input_types is not None: wrapper = with_input_types(*type_hints.input_types[0], **type_hints.input_types[1])(wrapper) output_hint = type_hints.simple_output_type(label) if output_hint is None and get_type_hints(wrapper).input_types and get_type_hints(wrapper).input_types[0]: output_hint = get_type_hints(wrapper).input_types[0][0] if output_hint: wrapper = with_output_types(typehints.Iterable[_strip_output_annotations(output_hint)])(wrapper) wrapper._argspec_fn = fn pardo = FlatMap(wrapper, *args, **kwargs) pardo.label = label return pardo
:func:`Filter` is a :func:`FlatMap` with its callable filtering out elements. Filter accepts a function that keeps elements that return True, and filters out the remaining elements. Args: fn (``Callable[..., bool]``): a callable object. First argument will be an element. *args: positional arguments passed to the transform callable. **kwargs: keyword arguments passed to the transform callable. Returns: ~apache_beam.pvalue.PCollection: A :class:`~apache_beam.pvalue.PCollection` containing the :func:`Filter` outputs. Raises: TypeError: If the **fn** passed as argument is not a callable. Typical error is to pass a :class:`DoFn` instance which is supported only for :class:`ParDo`.
github-repos
def build_relative_position(query_size, key_size, bucket_size=-1, max_position=-1, device=None): q_ids = torch.arange(0, query_size, device=device) k_ids = torch.arange(0, key_size, device=device) rel_pos_ids = q_ids[:, None] - k_ids[None, :] if bucket_size > 0 and max_position > 0: rel_pos_ids = make_log_bucket_position(rel_pos_ids, bucket_size, max_position) rel_pos_ids = rel_pos_ids.to(torch.long) rel_pos_ids = rel_pos_ids[:query_size, :] rel_pos_ids = rel_pos_ids.unsqueeze(0) return rel_pos_ids
Build relative position according to the query and key We assume the absolute position of query \(P_q\) is range from (0, query_size) and the absolute position of key \(P_k\) is range from (0, key_size), The relative positions from query to key is \(R_{q \rightarrow k} = P_q - P_k\) Args: query_size (int): the length of query key_size (int): the length of key bucket_size (int): the size of position bucket max_position (int): the maximum allowed absolute position device (`torch.device`): the device on which tensors will be created. Return: `torch.LongTensor`: A tensor with shape [1, query_size, key_size]
github-repos
def __init__(self, coord, timer_interval_secs, target=None, args=None, kwargs=None): if not isinstance(coord, Coordinator): raise ValueError("'coord' argument must be a Coordinator: %s" % coord) super(LooperThread, self).__init__() self.daemon = True self._coord = coord self._timer_interval_secs = timer_interval_secs self._target = target if self._target: self._args = args or () self._kwargs = kwargs or {} elif args or kwargs: raise ValueError("'args' and 'kwargs' argument require that you also pass 'target'") self._coord.register_thread(self)
Create a LooperThread. Args: coord: A Coordinator. timer_interval_secs: Time boundaries at which to call Run(), or None if it should be called back to back. target: Optional callable object that will be executed in the thread. args: Optional arguments to pass to `target` when calling it. kwargs: Optional keyword arguments to pass to `target` when calling it. Raises: ValueError: If one of the arguments is invalid.
github-repos
def nearest_neighbors(self, word, top_k=10): point = self[word] diff = (self.vectors - point) distances = np.linalg.norm(diff, axis=1) top_ids = distances.argsort()[1:(top_k + 1)] return [self.vocabulary.id_word[i] for i in top_ids]
Return the nearest k words to the given `word`. Args: word (string): single word. top_k (integer): decides how many neighbors to report. Returns: A list of words sorted by the distances. The closest is the first. Note: L2 metric is used to calculate distances.
codesearchnet
def load_pickled_model(filename, dirname=None): if dirname is None: pkg_filename = pkgutil.get_loader('dragnet').get_filename('dragnet') pkg_dirname = os.path.dirname(pkg_filename) dirname = os.path.join(pkg_dirname, 'pickled_models', model_path) filepath = os.path.join(dirname, filename) return joblib.load(filepath)
Load a pickled ``Extractor`` model from disk. Args: filename (str): Name of pickled model file under ``dirname``. dirname (str): Name of directory on disk containing the pickled model. If None, dragnet's default pickled model directory is used: /path/to/dragnet/pickled_models/[PY_VERSION]_[SKLEARN_VERSION] Returns: :class:`dragnet.extractor.Extractor`
juraj-google-style
def start_vm(access_token, subscription_id, resource_group, vm_name): endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', resource_group, '/providers/Microsoft.Compute/virtualMachines/', vm_name, '/start', '?api-version=', COMP_API]) return do_post(endpoint, '', access_token)
Start a virtual machine. Args: access_token (str): A valid Azure authentication token. subscription_id (str): Azure subscription id. resource_group (str): Azure resource group name. vm_name (str): Name of the virtual machine. Returns: HTTP response.
codesearchnet
def _padded_shape_to_tensor(padded_shape, input_component_shape): try: padded_shape_as_shape = tensor_shape.as_shape(padded_shape) ret = ops.convert_to_tensor([dim if dim is not None else -1 for dim in padded_shape_as_shape.as_list()], dtype=dtypes.int64) except (TypeError, ValueError) as e: ret = ops.convert_to_tensor(padded_shape, preferred_dtype=dtypes.int64) if ret.shape.dims is not None and len(ret.shape.dims) != 1: raise ValueError(f'Padded shape {padded_shape} must be a `tf.int64` vector tensor, but its shape was {ret.shape}.') from e if ret.dtype != dtypes.int64: raise TypeError(f'Padded shape {padded_shape} must be a `tf.int64` vector tensor, but its element type was {ret.dtype.name}.') from e padded_shape_as_shape = tensor_util.constant_value_as_shape(ret) if not _is_padded_shape_compatible_with(padded_shape_as_shape, input_component_shape): raise ValueError(f'The padded shape {padded_shape_as_shape} is not compatible with the shape {input_component_shape} of the corresponding input component.') return ret
Converts `padded_shape` to a `tf.Tensor` representing that shape. Args: padded_shape: A shape-like object, which may be a `tf.TensorShape`, a Python sequence, or a 1-D `tf.Tensor` of `tf.int64` elements. input_component_shape: A `tf.TensorShape`, with which `padded_shape` must be compatible. Returns: A 1-D `tf.Tensor` of `tf.int64` elements, representing `padded_shape`. Raises: ValueError: If `padded_shape` is not a shape or not compatible with `input_component_shape`. TypeError: If `padded_shape` is not convertible to a `tf.int64` tensor.
github-repos
def env_problem(env_problem_name, **kwargs): ep_cls = Registries.env_problems[env_problem_name] ep = ep_cls() ep.initialize(**kwargs) return ep
Get and initialize the `EnvProblem` with the given name and batch size. Args: env_problem_name: string name of the registered env problem. **kwargs: forwarded to env problem's initialize method. Returns: an initialized EnvProblem with the given batch size.
codesearchnet
def get_host(self, retry_count=3): try: default_timeout_set = False if (not socket.getdefaulttimeout()): socket.setdefaulttimeout(self.timeout) default_timeout_set = True log.debug('Host query for {0}'.format(self.address_str)) ret = socket.gethostbyaddr(self.address_str) if default_timeout_set: socket.setdefaulttimeout(None) results = namedtuple('get_host_results', 'hostname, aliaslist, ipaddrlist') return results(ret) except (socket.timeout, socket.error) as e: log.debug('Host query socket error: {0}'.format(e)) if (retry_count > 0): log.debug('Host query retrying (count: {0})'.format(str(retry_count))) return self.get_host((retry_count - 1)) else: raise HostLookupError('Host lookup failed for {0}.'.format(self.address_str)) except: raise HostLookupError('Host lookup failed for {0}.'.format(self.address_str))
The function for retrieving host information for an IP address. Args: retry_count (:obj:`int`): The number of times to retry in case socket errors, timeouts, connection resets, etc. are encountered. Defaults to 3. Returns: namedtuple: :hostname (str): The hostname returned mapped to the given IP address. :aliaslist (list): Alternate names for the given IP address. :ipaddrlist (list): IPv4/v6 addresses mapped to the same hostname. Raises: HostLookupError: The host lookup failed.
codesearchnet
def visit(self, node): method = 'visit_' + node.__class__.__name__ if not hasattr(self, method): raise ValueError('Unknown node type: %s' % node.__class__.__name__) visitor = getattr(self, method) if anno.hasanno(node, 'active_in'): self.active_variables = anno.getanno(node, 'active_in') pri, adj = visitor(node) if isinstance(pri, gast.AST): anno.setdefaultanno(pri, 'adj', adj) else: for node in pri: anno.setdefaultanno(node, 'adj', adj) if isinstance(adj, gast.AST): anno.setdefaultanno(adj, 'pri', pri) else: for node in adj: anno.setdefaultanno(node, 'pri', pri) return pri, adj
Visit a node. This method is largely modelled after the ast.NodeTransformer class. Args: node: The node to visit. Returns: A tuple of the primal and adjoint, each of which is a node or a list of nodes.
juraj-google-style
def PopTask(self): try: (_, task) = heapq.heappop(self._heap) except IndexError: return None self._task_identifiers.remove(task.identifier) return task
Retrieves and removes the first task from the heap. Returns: Task: the task or None if the heap is empty.
codesearchnet
def linear_set_layer(layer_size, inputs, context=None, activation_fn=tf.nn.relu, dropout=0.0, name=None): with tf.variable_scope(name, default_name='linear_set_layer', values=[inputs]): outputs = conv1d(inputs, layer_size, 1, activation=None, name='set_conv') if (context is not None): if (len(context.get_shape().as_list()) == 2): context = tf.expand_dims(context, axis=1) cont_tfm = conv1d(context, layer_size, 1, activation=None, name='cont_conv') outputs += cont_tfm if (activation_fn is not None): outputs = activation_fn(outputs) if (dropout != 0.0): outputs = tf.nn.dropout(outputs, (1.0 - dropout)) return outputs
Basic layer type for doing funky things with sets. Applies a linear transformation to each element in the input set. If a context is supplied, it is concatenated with the inputs. e.g. One can use global_pool_1d to get a representation of the set which can then be used as the context for the next layer. TODO: Add bias add (or control the biases used). Args: layer_size: Dimension to transform the input vectors to. inputs: A tensor of shape [batch_size, sequence_length, input_dims] containing the sequences of input vectors. context: A tensor of shape [batch_size, context_dims] containing a global statistic about the set. activation_fn: The activation function to use. dropout: Dropout probability. name: name. Returns: Tensor of shape [batch_size, sequence_length, output_dims] containing the sequences of transformed vectors.
codesearchnet
def complete(self, stream): assert (not self.is_complete()) self._marker.addInputPort(outputPort=stream.oport) self.stream.oport.schema = stream.oport.schema self._pending_schema._set(self.stream.oport.schema) stream.oport.operator._start_op = True
Complete the pending stream. Any connections made to :py:attr:`stream` are connected to `stream` once this method returns. Args: stream(Stream): Stream that completes the connection.
codesearchnet
def square(x): return math_ops.square(x)
Element-wise square. Args: x: Tensor or variable. Returns: A tensor.
github-repos
def __init__(self, cluster_resolver=None, communication_options=None): if communication_options is None: communication_options = collective_util.Options() super(CollectiveAllReduceStrategy, self).__init__(CollectiveAllReduceExtended(self, cluster_resolver=cluster_resolver, communication_options=communication_options)) distribute_lib.distribution_strategy_gauge.get_cell('V2').set('MultiWorkerMirroredStrategy') distribute_lib.distribution_strategy_replica_gauge.get_cell('num_workers').set(self.extended._num_workers) distribute_lib.distribution_strategy_replica_gauge.get_cell('num_replicas_per_worker').set(self.extended._num_devices_per_worker)
Creates the strategy. Args: cluster_resolver: optional `tf.distribute.cluster_resolver.ClusterResolver`. If `None`, `tf.distribute.cluster_resolver.TFConfigClusterResolver` is used. communication_options: optional `tf.distribute.experimental.CommunicationOptions`. This configures the default options for cross device communications. It can be overridden by options provided to the communication APIs like `tf.distribute.ReplicaContext.all_reduce`. See `tf.distribute.experimental.CommunicationOptions` for details.
github-repos
def inversion(origin=(0, 0, 0)): mat = -np.eye(4) mat[3, 3] = 1 mat[0:3, 3] = 2 * np.array(origin) return SymmOp(mat)
Inversion symmetry operation about axis. Args: origin (3x1 array): Origin of the inversion operation. Defaults to [0, 0, 0]. Returns: SymmOp representing an inversion operation about the origin.
juraj-google-style
def __init__(self, tensor: bk.TensorLike, qubits: Qubits = None, memory: Dict[Addr, Any] = None) -> None: if qubits is None: tensor = bk.astensorproduct(tensor) bits = bk.rank(tensor) qubits = range(bits) self.vec = QubitVector(tensor, qubits) self._memory = memory if memory is not None else {}
Create a new State from a tensor of qubit amplitudes Args: tensor: A vector or tensor of state amplitudes qubits: A sequence of qubit names. (Defaults to integer indices, e.g. [0, 1, 2] for 3 qubits) memory: Classical memory.
juraj-google-style
def __init__(self, actions=None): super().__init__(InstructionType.OFPIT_WRITE_ACTIONS) self.actions = actions if actions else []
Create a InstructionWriteAction with the optional parameters below. Args: actions (:class:`~.actions.ListOfActions`): Actions associated with OFPIT_WRITE_ACTIONS.
juraj-google-style
def reply(self, text): data = {'text': text, 'vchannel_id': self['vchannel_id']} if self.is_p2p(): data['type'] = RTMMessageType.P2PMessage data['to_uid'] = self['uid'] else: data['type'] = RTMMessageType.ChannelMessage data['channel_id'] = self['channel_id'] return RTMMessage(data)
Replys a text message Args: text(str): message content Returns: RTMMessage
juraj-google-style
def taper_rate(p0, p1): return ((2 * abs((p0[COLS.R] - p1[COLS.R]))) / point_dist(p0, p1))
Compute the taper rate between points p0 and p1 Args: p0, p1: iterables with first 4 components containing (x, y, z, r) Returns: The taper rate, defined as the absolute value of the difference in the diameters of p0 and p1 divided by the euclidian distance between them.
codesearchnet
def profile_update_schema(profile): if profile.get('autoclear') is None: print( '{}{}Profile Update: Adding new "autoclear" parameter.'.format( c.Style.BRIGHT, c.Fore.YELLOW ) ) profile['autoclear'] = True for validation in profile.get('validations') or []: if validation.get('data_type') is None: print( '{}{}Profile Update: Adding new "data_type" parameter.'.format( c.Style.BRIGHT, c.Fore.YELLOW ) ) validation['data_type'] = 'redis' if profile.get('install_json') is not None and profile.get('script') is not None: print( '{}{}Removing deprecated "script" parameter.'.format(c.Style.BRIGHT, c.Fore.YELLOW) ) profile.pop('script')
Update profile to latest schema. Args: profile (dict): The dictionary containting the profile settings.
juraj-google-style
def __init__(self, api_key: str, model: str, config: genai_types.GenerateContentConfig, output_dict: dict[EventTransition, content_api.ProcessorContentTypes | None], sensitivity: Optional[dict[EventTransition, int]]=None, max_images: int=5): self._client = genai.Client(api_key=api_key) self._model = model self._config = config if not config.response_schema: raise ValueError('Response schema is required for event detection.') self._sensitivity = sensitivity self._output_dict = {} self._init_output_dict(output_dict) self._last_transition = (START_STATE, START_STATE) self._transition_counter = (self._last_transition, 0) self._images = collections.deque[tuple[ProcessorPart, float]](maxlen=max_images)
Initializes the event detection processor. Args: api_key: The API key to use for the event detection model. model: The model to use for the event detection. config: The configuration to use for the event detection model. This configuration should contain the response schema for the event detection model. output_dict: A dictionary of transitions between events to the output to return when the transition is detected. A transition is a pair of event names `(from_event_state, to_event_state)`, where `from_event_state` can be the start state `START_STATE`, an event name, or the wild card `"*"` to define all transitions from any state (including the start state) to the event state. When the output is None, the transition is detected but no output is returned. sensitivity: A dictionary of transitions to the number of detection in a row that should happen before the event detection processor sends a detection output. By default, the sensitivity is 1 for a transition. max_images: The maximum number of images to keep in the input stream.
github-repos
def predict(self, df_data, graph=None, **kwargs): if (graph is None): return self.create_graph_from_data(df_data, **kwargs) elif isinstance(graph, nx.DiGraph): return self.orient_directed_graph(df_data, graph, **kwargs) elif isinstance(graph, nx.Graph): return self.orient_undirected_graph(df_data, graph, **kwargs) else: print('Unknown Graph type') raise ValueError
Orient a graph using the method defined by the arguments. Depending on the type of `graph`, this function process to execute different functions: 1. If ``graph`` is a ``networkx.DiGraph``, then ``self.orient_directed_graph`` is executed. 2. If ``graph`` is a ``networkx.Graph``, then ``self.orient_undirected_graph`` is executed. 3. If ``graph`` is a ``None``, then ``self.create_graph_from_data`` is executed. Args: df_data (pandas.DataFrame): DataFrame containing the observational data. graph (networkx.DiGraph or networkx.Graph or None): Prior knowledge on the causal graph. .. warning:: Requirement : Name of the nodes in the graph must correspond to the name of the variables in df_data
codesearchnet