code
stringlengths
75
104k
docstring
stringlengths
1
46.9k
def workdir_is_clean(self, quiet=False): """ Check for uncommitted changes, return `True` if everything is clean. Inspired by http://stackoverflow.com/questions/3878624/. """ # Update the index self.run('git update-index -q --ignore-submodules --refresh', **RUN_KWARGS) unchanged = True # Disallow unstaged changes in the working tree try: self.run('git diff-files --quiet --ignore-submodules --', report_error=False, **RUN_KWARGS) except exceptions.Failure: unchanged = False if not quiet: notify.warning('You have unstaged changes!') self.run('git diff-files --name-status -r --ignore-submodules -- >&2', **RUN_KWARGS) # Disallow uncommitted changes in the index try: self.run('git diff-index --cached --quiet HEAD --ignore-submodules --', report_error=False, **RUN_KWARGS) except exceptions.Failure: unchanged = False if not quiet: notify.warning('Your index contains uncommitted changes!') self.run('git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2', **RUN_KWARGS) return unchanged
Check for uncommitted changes, return `True` if everything is clean. Inspired by http://stackoverflow.com/questions/3878624/.
def load_response_microservices(plugin_path, plugins, internal_attributes, base_url): """ Loads response micro services (handling outgoing responses). :type plugin_path: list[str] :type plugins: list[str] :type internal_attributes: dict[string, dict[str, str | list[str]]] :type base_url: str :rtype satosa.micro_service.service_base.ResponseMicroService :param plugin_path: Path to the plugin directory :param plugins: A list with the name of the plugin files :param: base_url: base url of the SATOSA server :return: Response micro service """ response_services = _load_microservices(plugin_path, plugins, _response_micro_service_filter, internal_attributes, base_url) logger.info("Loaded response micro services: %s" % [type(k).__name__ for k in response_services]) return response_services
Loads response micro services (handling outgoing responses). :type plugin_path: list[str] :type plugins: list[str] :type internal_attributes: dict[string, dict[str, str | list[str]]] :type base_url: str :rtype satosa.micro_service.service_base.ResponseMicroService :param plugin_path: Path to the plugin directory :param plugins: A list with the name of the plugin files :param: base_url: base url of the SATOSA server :return: Response micro service
def _validate(self, key, cls=None): """Verify the manifest schema.""" if key not in self.manifest: raise ValueError("Manifest %s requires '%s'." % (self.manifest_path, key)) if cls: if not isinstance(self.manifest[key], cls): raise TypeError("Manifest value '%s' should be %s, not %s" % (key, cls, type(self.manifest[key])))
Verify the manifest schema.
def _rescale_and_convert_field_inplace(self, array, name, scale, zero): """ Apply fits scalings. Also, convert bool to proper numpy boolean values """ self._rescale_array(array[name], scale, zero) if array[name].dtype == numpy.bool: array[name] = self._convert_bool_array(array[name]) return array
Apply fits scalings. Also, convert bool to proper numpy boolean values
def get_plugin_actions(self): """Return a list of actions related to plugin""" quit_action = create_action(self, _("&Quit"), icon=ima.icon('exit'), tip=_("Quit"), triggered=self.quit) self.register_shortcut(quit_action, "_", "Quit", "Ctrl+Q") run_action = create_action(self, _("&Run..."), None, ima.icon('run_small'), _("Run a Python script"), triggered=self.run_script) environ_action = create_action(self, _("Environment variables..."), icon=ima.icon('environ'), tip=_("Show and edit environment variables" " (for current session)"), triggered=self.show_env) syspath_action = create_action(self, _("Show sys.path contents..."), icon=ima.icon('syspath'), tip=_("Show (read-only) sys.path"), triggered=self.show_syspath) buffer_action = create_action(self, _("Buffer..."), None, tip=_("Set maximum line count"), triggered=self.change_max_line_count) exteditor_action = create_action(self, _("External editor path..."), None, None, _("Set external editor executable path"), triggered=self.change_exteditor) wrap_action = create_action(self, _("Wrap lines"), toggled=self.toggle_wrap_mode) wrap_action.setChecked(self.get_option('wrap')) codecompletion_action = create_action(self, _("Automatic code completion"), toggled=self.toggle_codecompletion) codecompletion_action.setChecked(self.get_option('codecompletion/auto')) option_menu = QMenu(_('Internal console settings'), self) option_menu.setIcon(ima.icon('tooloptions')) add_actions(option_menu, (buffer_action, wrap_action, codecompletion_action, exteditor_action)) plugin_actions = [None, run_action, environ_action, syspath_action, option_menu, MENU_SEPARATOR, quit_action, self.undock_action] return plugin_actions
Return a list of actions related to plugin
def stats_shooting(self, kind='R', summary=False): """Returns a DataFrame of shooting stats.""" return self._get_stats_table('shooting', kind=kind, summary=summary)
Returns a DataFrame of shooting stats.
def check_all_servers(): """Checks all servers, removing any that Modis isn't part of any more""" data = datatools.get_data() for server_id in data["discord"]["servers"]: is_in_client = False for client_server in client.servers: if server_id == client_server.id: is_in_client = True break if not is_in_client: remove_server_data(server_id)
Checks all servers, removing any that Modis isn't part of any more
def _validate_sort_field(self, sort_by): """ :param sort_by: string :raises: pybomb.exceptions.InvalidSortFieldException """ if ( sort_by not in self.RESPONSE_FIELD_MAP or not self.RESPONSE_FIELD_MAP[sort_by].is_sort ): raise InvalidSortFieldException( '"{0}" is an invalid sort field'.format(sort_by) )
:param sort_by: string :raises: pybomb.exceptions.InvalidSortFieldException
def metafetcher(bamfile, metacontig2contig, metatag): ''' return reads in order of metacontigs''' for metacontig in metacontig2contig: for contig in metacontig2contig[metacontig]: for read in bamfile.fetch(contig): read.set_tag(metatag, metacontig) yield read
return reads in order of metacontigs
def _get_headers(self): """Built headers for request to IPinfo API.""" headers = { 'user-agent': 'IPinfoClient/Python{version}/1.0'.format(version=sys.version_info[0]), 'accept': 'application/json' } if self.access_token: headers['authorization'] = 'Bearer {}'.format(self.access_token) return headers
Built headers for request to IPinfo API.
def encode_exception(exception): """Encode exception to a form that can be passed around and serialized. This will grab the stack, then strip off the last two calls which are encode_exception and the function that called it. """ import sys return AsyncException(unicode(exception), exception.args, sys.exc_info(), exception)
Encode exception to a form that can be passed around and serialized. This will grab the stack, then strip off the last two calls which are encode_exception and the function that called it.
def from_metadata(self, db_path, db_name='engine_metadata.db'): """ Registers in the current session the views of the MetadataSource so the data is obtained from the metadata database instead of reading the repositories with the DefaultSource. :param db_path: path to the folder that contains the database. :type db_path: str :param db_name: name of the database file (engine_metadata.db) by default. :type db_name: str :returns: the same instance of the engine :rtype: Engine """ self.__engine.fromMetadata(db_path, db_name) return self
Registers in the current session the views of the MetadataSource so the data is obtained from the metadata database instead of reading the repositories with the DefaultSource. :param db_path: path to the folder that contains the database. :type db_path: str :param db_name: name of the database file (engine_metadata.db) by default. :type db_name: str :returns: the same instance of the engine :rtype: Engine
def p2th_address(self) -> Optional[str]: '''P2TH address of this deck''' if self.id: return Kutil(network=self.network, privkey=bytearray.fromhex(self.id)).address else: return None
P2TH address of this deck
def long_description(*filenames): """Provide a long description.""" res = [''] for filename in filenames: with open(filename) as fp: for line in fp: res.append(' ' + line) res.append('') res.append('\n') return EMPTYSTRING.join(res)
Provide a long description.
def _shrink_update(self, rmstart: int, rmstop: int) -> None: """Update self._type_to_spans according to the removed span. Warning: If an operation involves both _shrink_update and _insert_update, you might wanna consider doing the _insert_update before the _shrink_update as this function can cause data loss in self._type_to_spans. """ # Note: The following algorithm won't work correctly if spans # are not sorted. # Note: No span should be removed from _type_to_spans. for spans in self._type_to_spans.values(): i = len(spans) - 1 while i >= 0: s, e = span = spans[i] if rmstop <= s: # rmstart <= rmstop <= s <= e rmlength = rmstop - rmstart span[:] = s - rmlength, e - rmlength i -= 1 continue break else: continue while True: if rmstart <= s: if rmstop < e: # rmstart < s <= rmstop < e span[:] = rmstart, e + rmstart - rmstop i -= 1 if i < 0: break s, e = span = spans[i] continue # rmstart <= s <= e < rmstop spans.pop(i)[:] = -1, -1 i -= 1 if i < 0: break s, e = span = spans[i] continue break while i >= 0: if e <= rmstart: # s <= e <= rmstart <= rmstop i -= 1 if i < 0: break s, e = span = spans[i] continue # s <= rmstart <= rmstop <= e span[1] -= rmstop - rmstart i -= 1 if i < 0: break s, e = span = spans[i] continue
Update self._type_to_spans according to the removed span. Warning: If an operation involves both _shrink_update and _insert_update, you might wanna consider doing the _insert_update before the _shrink_update as this function can cause data loss in self._type_to_spans.
def configfield_ref_role(name, rawtext, text, lineno, inliner, options=None, content=None): """Process a role that references the Task configuration field nodes created by the ``lsst-config-fields``, ``lsst-task-config-subtasks``, and ``lsst-task-config-subtasks`` directives. Parameters ---------- name The role name used in the document. rawtext The entire markup snippet, with role. text The text marked with the role. lineno The line number where ``rawtext`` appears in the input. inliner The inliner instance that called us. options Directive options for customization. content The directive content for customization. Returns ------- nodes : `list` List of nodes to insert into the document. messages : `list` List of system messages. See also -------- `format_configfield_id` `pending_configfield_xref` `process_pending_configfield_xref_nodes` """ node = pending_configfield_xref(rawsource=text) return [node], []
Process a role that references the Task configuration field nodes created by the ``lsst-config-fields``, ``lsst-task-config-subtasks``, and ``lsst-task-config-subtasks`` directives. Parameters ---------- name The role name used in the document. rawtext The entire markup snippet, with role. text The text marked with the role. lineno The line number where ``rawtext`` appears in the input. inliner The inliner instance that called us. options Directive options for customization. content The directive content for customization. Returns ------- nodes : `list` List of nodes to insert into the document. messages : `list` List of system messages. See also -------- `format_configfield_id` `pending_configfield_xref` `process_pending_configfield_xref_nodes`
def plot_mean_field_conv(N=1, n=0.5, Uspan=np.arange(0, 3.6, 0.5)): """Generates the plot on the convergenge of the mean field in single site spin hamiltonian under with N degenerate half-filled orbitals """ sl = Spinon(slaves=2*N, orbitals=N, avg_particles=2*n, hopping=[0.5]*2*N, orbital_e=[0]*2*N) hlog = solve_loop(sl, Uspan, [0.])[1] f, (ax1, ax2) = plt.subplots(2, sharex=True) for field in hlog: field = np.asarray(field) ax1.semilogy(abs(field[1:]-field[:-1])) ax2.plot(field)#, label = 'h, U = {}'.format(Uint)) plt.title('Convergence of selfconsintent mean field') ax1.set_ylabel('$\\Delta h$') ax2.set_ylabel('mean field $h$') plt.xlabel('iterations') return hlog
Generates the plot on the convergenge of the mean field in single site spin hamiltonian under with N degenerate half-filled orbitals
def sanitize_and_wrap(self, task_id, args, kwargs): """This function should be called **ONLY** when all the futures we track have been resolved. If the user hid futures a level below, we will not catch it, and will (most likely) result in a type error. Args: task_id (uuid str) : Task id func (Function) : App function args (List) : Positional args to app function kwargs (Dict) : Kwargs to app function Return: partial function evaluated with all dependencies in args, kwargs and kwargs['inputs'] evaluated. """ dep_failures = [] # Replace item in args new_args = [] for dep in args: if isinstance(dep, Future): try: new_args.extend([dep.result()]) except Exception as e: if self.tasks[dep.tid]['status'] in FINAL_FAILURE_STATES: dep_failures.extend([e]) else: new_args.extend([dep]) # Check for explicit kwargs ex, fu_1=<fut> for key in kwargs: dep = kwargs[key] if isinstance(dep, Future): try: kwargs[key] = dep.result() except Exception as e: if self.tasks[dep.tid]['status'] in FINAL_FAILURE_STATES: dep_failures.extend([e]) # Check for futures in inputs=[<fut>...] if 'inputs' in kwargs: new_inputs = [] for dep in kwargs['inputs']: if isinstance(dep, Future): try: new_inputs.extend([dep.result()]) except Exception as e: if self.tasks[dep.tid]['status'] in FINAL_FAILURE_STATES: dep_failures.extend([e]) else: new_inputs.extend([dep]) kwargs['inputs'] = new_inputs return new_args, kwargs, dep_failures
This function should be called **ONLY** when all the futures we track have been resolved. If the user hid futures a level below, we will not catch it, and will (most likely) result in a type error. Args: task_id (uuid str) : Task id func (Function) : App function args (List) : Positional args to app function kwargs (Dict) : Kwargs to app function Return: partial function evaluated with all dependencies in args, kwargs and kwargs['inputs'] evaluated.
def import_keypair(kwargs=None, call=None): ''' Import an SSH public key. .. versionadded:: 2015.8.3 ''' if call != 'function': log.error( 'The import_keypair function must be called with -f or --function.' ) return False if not kwargs: kwargs = {} if 'keyname' not in kwargs: log.error('A keyname is required.') return False if 'file' not in kwargs: log.error('A public key file is required.') return False params = {'Action': 'ImportKeyPair', 'KeyName': kwargs['keyname']} public_key_file = kwargs['file'] if os.path.exists(public_key_file): with salt.utils.files.fopen(public_key_file, 'r') as fh_: public_key = salt.utils.stringutils.to_unicode(fh_.read()) if public_key is not None: params['PublicKeyMaterial'] = base64.b64encode(public_key) data = aws.query(params, return_url=True, location=get_location(), provider=get_provider(), opts=__opts__, sigver='4') return data
Import an SSH public key. .. versionadded:: 2015.8.3
def find_sdl_attrs(prefix: str) -> Iterator[Tuple[str, Any]]: """Return names and values from `tcod.lib`. `prefix` is used to filter out which names to copy. """ from tcod._libtcod import lib if prefix.startswith("SDL_"): name_starts_at = 4 elif prefix.startswith("SDL"): name_starts_at = 3 else: name_starts_at = 0 for attr in dir(lib): if attr.startswith(prefix): yield attr[name_starts_at:], getattr(lib, attr)
Return names and values from `tcod.lib`. `prefix` is used to filter out which names to copy.
def read_array(self, key, start=None, stop=None): """ read an array for the specified node (off of group """ import tables node = getattr(self.group, key) attrs = node._v_attrs transposed = getattr(attrs, 'transposed', False) if isinstance(node, tables.VLArray): ret = node[0][start:stop] else: dtype = getattr(attrs, 'value_type', None) shape = getattr(attrs, 'shape', None) if shape is not None: # length 0 axis ret = np.empty(shape, dtype=dtype) else: ret = node[start:stop] if dtype == 'datetime64': # reconstruct a timezone if indicated ret = _set_tz(ret, getattr(attrs, 'tz', None), coerce=True) elif dtype == 'timedelta64': ret = np.asarray(ret, dtype='m8[ns]') if transposed: return ret.T else: return ret
read an array for the specified node (off of group
def get_range(self): """ Return the possible types for this parameter's value. (I.e. return {name: <class>} for all classes that are concrete_descendents() of self.class_.) Only classes from modules that have been imported are added (see concrete_descendents()). """ classes = concrete_descendents(self.class_) d=OrderedDict((name,class_) for name,class_ in classes.items()) if self.allow_None: d['None']=None return d
Return the possible types for this parameter's value. (I.e. return {name: <class>} for all classes that are concrete_descendents() of self.class_.) Only classes from modules that have been imported are added (see concrete_descendents()).
def simple_moving_matrix(x, n=10): """ Create simple moving matrix. Parameters ---------- x : ndarray A numpy array n : integer The number of sample points used to make average Returns ------- ndarray A n x n numpy array which will be useful for calculating confidentail interval of simple moving average """ if x.ndim > 1 and len(x[0]) > 1: x = np.average(x, axis=1) h = n / 2 o = 0 if h * 2 == n else 1 xx = [] for i in range(h, len(x) - h): xx.append(x[i-h:i+h+o]) return np.array(xx)
Create simple moving matrix. Parameters ---------- x : ndarray A numpy array n : integer The number of sample points used to make average Returns ------- ndarray A n x n numpy array which will be useful for calculating confidentail interval of simple moving average
def get_templates(self): """ Get list of templates this object use :return: list of templates :rtype: list """ use = getattr(self, 'use', '') if isinstance(use, list): return [n.strip() for n in use if n.strip()] return [n.strip() for n in use.split(',') if n.strip()]
Get list of templates this object use :return: list of templates :rtype: list
def local_assortativity_wu_sign(W): ''' Local assortativity measures the extent to which nodes are connected to nodes of similar strength. Adapted from Thedchanamoorthy et al. 2014 formula to allowed weighted/signed networks. Parameters ---------- W : NxN np.ndarray undirected connection matrix with positive and negative weights Returns ------- loc_assort_pos : Nx1 np.ndarray local assortativity from positive weights loc_assort_neg : Nx1 np.ndarray local assortativity from negative weights ''' n = len(W) np.fill_diagonal(W, 0) r_pos = assortativity_wei(W * (W > 0)) r_neg = assortativity_wei(W * (W < 0)) str_pos, str_neg, _, _ = strengths_und_sign(W) loc_assort_pos = np.zeros((n,)) loc_assort_neg = np.zeros((n,)) for curr_node in range(n): jp = np.where(W[curr_node, :] > 0) loc_assort_pos[curr_node] = np.sum(np.abs(str_pos[jp] - str_pos[curr_node])) / str_pos[curr_node] jn = np.where(W[curr_node, :] < 0) loc_assort_neg[curr_node] = np.sum(np.abs(str_neg[jn] - str_neg[curr_node])) / str_neg[curr_node] loc_assort_pos = ((r_pos + 1) / n - loc_assort_pos / np.sum(loc_assort_pos)) loc_assort_neg = ((r_neg + 1) / n - loc_assort_neg / np.sum(loc_assort_neg)) return loc_assort_pos, loc_assort_neg
Local assortativity measures the extent to which nodes are connected to nodes of similar strength. Adapted from Thedchanamoorthy et al. 2014 formula to allowed weighted/signed networks. Parameters ---------- W : NxN np.ndarray undirected connection matrix with positive and negative weights Returns ------- loc_assort_pos : Nx1 np.ndarray local assortativity from positive weights loc_assort_neg : Nx1 np.ndarray local assortativity from negative weights
def add_message(self, text, type=None): """Add a message with an optional type.""" key = self._msg_key self.setdefault(key, []) self[key].append(message(type, text)) self.save()
Add a message with an optional type.
def decode_transformer(encoder_output, encoder_decoder_attention_bias, targets, hparams, name, task=None, causal=True): """Original Transformer decoder.""" orig_hparams = hparams with tf.variable_scope(name): if task is None: task = hparams.task if task == "translate": targets = common_layers.flatten4d3d(targets) decoder_input, decoder_self_bias = ( transformer.transformer_prepare_decoder(targets, hparams)) decoder_input = tf.nn.dropout(decoder_input, 1.0 - hparams.layer_prepostprocess_dropout) if not causal: decoder_self_bias *= 0. decoder_output = transformer.transformer_decoder( decoder_input, encoder_output, decoder_self_bias, encoder_decoder_attention_bias, hparams) decoder_output = tf.expand_dims(decoder_output, axis=2) else: assert task == "image" inputs = None # have to reshape targets as b, 32, 32, 3 * hidden size] beacuse otherwise # prepare_image will choke targets = tf.reshape(targets, [tf.shape(targets)[0], hparams.img_len, hparams.img_len, hparams.num_channels*hparams.hidden_size]) # Prepare decoder inputs and bias. # TODO(nikip): Make prepare_decoder return bias decoder_input, _, _ = cia.prepare_decoder(targets, hparams) bias = None # Add class label to decoder input. if not hparams.drop_inputs: decoder_input += tf.reshape( inputs, [common_layers.shape_list(targets)[0], 1, 1, hparams.hidden_size]) decoder_output = cia.transformer_decoder_layers( decoder_input, encoder_output=None, num_layers=hparams.num_decoder_layers or hparams.num_hidden_layers, hparams=hparams, self_attention_bias=bias, attention_type=hparams.dec_attention_type, name="decoder") decoder_output_shape = common_layers.shape_list(decoder_output) decoder_output = tf.reshape(decoder_output, [decoder_output_shape[0], -1, 1, hparams.hidden_size]) # Expand since t2t expects 4d tensors. hparams = orig_hparams return decoder_output
Original Transformer decoder.
def cover(ctx, html=False): '''Run tests suite with coverage''' header('Run tests suite with coverage') cmd = 'pytest --cov udata --cov-report term' if html: cmd = ' '.join((cmd, '--cov-report html:reports/python/cover')) with ctx.cd(ROOT): ctx.run(cmd, pty=True)
Run tests suite with coverage
def global_maxpooling(attrs, inputs, proto_obj): """Performs max pooling on the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'max'}) return 'Pooling', new_attrs, inputs
Performs max pooling on the input.
def buffered_write(self, buf): """Appends a bytes like object to the transport write buffer. Raises BufferOverflowError if buf would cause the buffer to grow beyond the specified maximum. buf -- bytes to send """ if self.closed: raise ConnectionClosed() if len(buf) + len(self.write_buffer) > self.max_size: raise BufferOverflowError() else: self.write_buffer.extend(buf)
Appends a bytes like object to the transport write buffer. Raises BufferOverflowError if buf would cause the buffer to grow beyond the specified maximum. buf -- bytes to send
def full_name(self, gender: Optional[Gender] = None, reverse: bool = False) -> str: """Generate a random full name. :param reverse: Return reversed full name. :param gender: Gender's enum object. :return: Full name. :Example: Johann Wolfgang. """ if gender is None: gender = get_random_item(Gender, rnd=self.random) if gender and isinstance(gender, Gender): gender = gender else: raise NonEnumerableError(Gender) fmt = '{1} {0}' if reverse else '{0} {1}' return fmt.format( self.name(gender), self.surname(gender), )
Generate a random full name. :param reverse: Return reversed full name. :param gender: Gender's enum object. :return: Full name. :Example: Johann Wolfgang.
def index(credentials=None): """Get list of projects""" user, oauth_access_token = parsecredentials(credentials) if not settings.ADMINS or user not in settings.ADMINS: return flask.make_response('You shall not pass!!! You are not an administrator!',403) usersprojects = {} totalsize = {} for f in glob.glob(settings.ROOT + "projects/*"): if os.path.isdir(f): u = os.path.basename(f) usersprojects[u], totalsize[u] = getprojects(u) usersprojects[u].sort() return withheaders(flask.make_response(flask.render_template('admin.html', version=VERSION, system_id=settings.SYSTEM_ID, system_name=settings.SYSTEM_NAME, system_description=settings.SYSTEM_DESCRIPTION, system_author=settings.SYSTEM_AUTHOR, system_version=settings.SYSTEM_VERSION, system_email=settings.SYSTEM_EMAIL, user=user, url=getrooturl(), usersprojects = sorted(usersprojects.items()), totalsize=totalsize, allow_origin=settings.ALLOW_ORIGIN, oauth_access_token=oauth_encrypt(oauth_access_token) )), "text/html; charset=UTF-8", {'allow_origin':settings.ALLOW_ORIGIN})
Get list of projects
def failover(self, sync=None, force=None): """ Fails over a replication session. :param sync: True - sync the source and destination resources before failing over the asynchronous replication session or keep them in sync after failing over the synchronous replication session. False - don't sync. :param force: True - skip pre-checks on file system(s) replication sessions of a NAS server when a replication failover is issued from the source NAS server. False - don't skip pre-checks. """ req_body = self._cli.make_body(sync=sync, force=force) resp = self.action('failover', **req_body) resp.raise_if_err() return resp
Fails over a replication session. :param sync: True - sync the source and destination resources before failing over the asynchronous replication session or keep them in sync after failing over the synchronous replication session. False - don't sync. :param force: True - skip pre-checks on file system(s) replication sessions of a NAS server when a replication failover is issued from the source NAS server. False - don't skip pre-checks.
def process_config(self): """ Intended to put any code that should be run after any config reload event """ if 'byte_unit' in self.config: if isinstance(self.config['byte_unit'], basestring): self.config['byte_unit'] = self.config['byte_unit'].split() if 'enabled' in self.config: self.config['enabled'] = str_to_bool(self.config['enabled']) if 'measure_collector_time' in self.config: self.config['measure_collector_time'] = str_to_bool( self.config['measure_collector_time']) # Raise an error if both whitelist and blacklist are specified if ((self.config.get('metrics_whitelist', None) and self.config.get('metrics_blacklist', None))): raise DiamondException( 'Both metrics_whitelist and metrics_blacklist specified ' + 'in file %s' % self.configfile) if self.config.get('metrics_whitelist', None): self.config['metrics_whitelist'] = re.compile( self.config['metrics_whitelist']) elif self.config.get('metrics_blacklist', None): self.config['metrics_blacklist'] = re.compile( self.config['metrics_blacklist'])
Intended to put any code that should be run after any config reload event
def main( gpus:Param("The GPUs to use for distributed training", str)='all', script:Param("Script to run", str, opt=False)='', args:Param("Args to pass to script", nargs='...', opt=False)='' ): "PyTorch distributed training launch helper that spawns multiple distributed processes" # Loosely based on torch.distributed.launch current_env = os.environ.copy() gpus = list(range(torch.cuda.device_count())) if gpus=='all' else list(gpus) current_env["WORLD_SIZE"] = str(len(gpus)) current_env["MASTER_ADDR"] = '127.0.0.1' current_env["MASTER_PORT"] = '29500' processes = [] for i,gpu in enumerate(gpus): current_env["RANK"] = str(i) cmd = [sys.executable, "-u", script, f"--gpu={gpu}"] + args process = subprocess.Popen(cmd, env=current_env) processes.append(process) for process in processes: process.wait()
PyTorch distributed training launch helper that spawns multiple distributed processes
def _pluck_feature_pacts(pr_body: str) -> FrozenSet[str]: """ # Returns set with one feature pact if specified >>> body = 'gitlab: mygitlaburl.com\\r\\nfeature-pacts: zh-feature-a' >>> _pluck_feature_pacts(body) == frozenset({'zh-feature-a'}) True # Returns set with multiple feature pacts if specified >>> body = 'gitlab: mygitlaburl.com\\r\\nfeature-pacts: zh-feature-a eh-feature-b\\r\\nurgent!' >>> _pluck_feature_pacts(body) == frozenset({'zh-feature-a', 'eh-feature-b'}) True # Returns empty set if no feature-pacts line >>> body = 'gitlab: mygitlaburl.com\\r\\n\\r\\nJust adding some documentation' >>> _pluck_feature_pacts(body) frozenset() # Returns empty set if feature-pacts line with no tags >>> body = 'gitlab: mygitlaburl.com\\r\\n\\r\\nfeature-pacts:\\r\\nJust adding some docs' >>> _pluck_feature_pacts(body) frozenset() # Raises a GithubPrError if multiple feature-pact lines found >>> body = 'feature-pacts: zh-feature-a\\r\\nfeature-pacts: eh-feature-b' >>> _pluck_feature_pacts(body) Traceback (most recent call last): ... faaspact_verifier.delivery.github_prs.GithubPrError: ... """ feature_pact_lines = [line for line in pr_body.split('\r\n') if line.startswith('feature-pacts:')] if not feature_pact_lines: return frozenset() if len(feature_pact_lines) > 1: raise GithubPrError(f'There should only be one feature-pacts line. "{feature_pact_lines}"') return frozenset(feature_pact_lines[0].split()[1:])
# Returns set with one feature pact if specified >>> body = 'gitlab: mygitlaburl.com\\r\\nfeature-pacts: zh-feature-a' >>> _pluck_feature_pacts(body) == frozenset({'zh-feature-a'}) True # Returns set with multiple feature pacts if specified >>> body = 'gitlab: mygitlaburl.com\\r\\nfeature-pacts: zh-feature-a eh-feature-b\\r\\nurgent!' >>> _pluck_feature_pacts(body) == frozenset({'zh-feature-a', 'eh-feature-b'}) True # Returns empty set if no feature-pacts line >>> body = 'gitlab: mygitlaburl.com\\r\\n\\r\\nJust adding some documentation' >>> _pluck_feature_pacts(body) frozenset() # Returns empty set if feature-pacts line with no tags >>> body = 'gitlab: mygitlaburl.com\\r\\n\\r\\nfeature-pacts:\\r\\nJust adding some docs' >>> _pluck_feature_pacts(body) frozenset() # Raises a GithubPrError if multiple feature-pact lines found >>> body = 'feature-pacts: zh-feature-a\\r\\nfeature-pacts: eh-feature-b' >>> _pluck_feature_pacts(body) Traceback (most recent call last): ... faaspact_verifier.delivery.github_prs.GithubPrError: ...
def __process_warc_gz_file(self, path_name): """ Iterates all transactions in one WARC file and for each transaction tries to extract an article object. Afterwards, each article is checked against the filter criteria and if all are passed, the function on_valid_article_extracted is invoked with the article object. :param path_name: :return: """ counter_article_total = 0 counter_article_passed = 0 counter_article_discarded = 0 start_time = time.time() with open(path_name, 'rb') as stream: for record in ArchiveIterator(stream): # try: if record.rec_type == 'response': counter_article_total += 1 # if the article passes filter tests, we notify the user filter_pass, article = self.__filter_record(record) if filter_pass: counter_article_passed += 1 if not article: article = NewsPlease.from_warc(record) self.__logger.info('article pass (%s; %s; %s)', article.source_domain, article.date_publish, article.title) self.__callback_on_article_extracted(article) else: counter_article_discarded += 1 if article: self.__logger.info('article discard (%s; %s; %s)', article.source_domain, article.date_publish, article.title) else: self.__logger.info('article discard (%s)', record.rec_headers.get_header('WARC-Target-URI')) if counter_article_total % 10 == 0: elapsed_secs = time.time() - start_time secs_per_article = elapsed_secs / counter_article_total self.__logger.info('statistics') self.__logger.info('pass = %i, discard = %i, total = %i', counter_article_passed, counter_article_discarded, counter_article_total) self.__logger.info('extraction from current WARC file started %s; %f s/article', human(start_time), secs_per_article) # except: # if self.__continue_after_error: # self.__logger.error('Unexpected error: %s', sys.exc_info()[0]) # pass # else: # raise # cleanup if self.__delete_warc_after_extraction: os.remove(path_name) self.__register_fully_extracted_warc_file(self.__warc_download_url)
Iterates all transactions in one WARC file and for each transaction tries to extract an article object. Afterwards, each article is checked against the filter criteria and if all are passed, the function on_valid_article_extracted is invoked with the article object. :param path_name: :return:
def _getData(self, data): """ Check that data is acceptable and return it. Default behavior is that the data has to be of type `dict`. In derived classes this method could for example allow `None` or empty strings and just return empty dictionary. :raises: ``ValidationError`` if data is missing or wrong type :return: the data to be validated """ if not isinstance(data, dict): raise ValidationError( 'data is not a valid dictionary: %s' % (str(type(data)),)) return data
Check that data is acceptable and return it. Default behavior is that the data has to be of type `dict`. In derived classes this method could for example allow `None` or empty strings and just return empty dictionary. :raises: ``ValidationError`` if data is missing or wrong type :return: the data to be validated
def getAnalogChannelData(self,ChNumber): """ Returns an array of numbers containing the data values of the channel number "ChNumber". ChNumber is the number of the channal as in .cfg file. """ if not self.DatFileContent: print "No data file content. Use the method ReadDataFile first" return 0 if (ChNumber > self.A): print "Channel number greater than the total number of channels." return 0 # Fomating string for struct module: str_struct = "ii%dh" %(self.A + int(numpy.ceil((float(self.D)/float(16))))) # Number of bytes per sample: NB = 4 + 4 + self.A*2 + int(numpy.ceil((float(self.D)/float(16))))*2 # Number of samples: N = self.getNumberOfSamples() # Empty column vector: values = numpy.empty((N,1)) ch_index = self.An.index(ChNumber) # Reading the values from DatFileContent string: for i in range(N): data = struct.unpack(str_struct,self.DatFileContent[i*NB:(i*NB)+NB]) values[i] = data[ChNumber+1] # The first two number ar the sample index and timestamp values = values * self.a[ch_index] # a factor values = values + self.b[ch_index] # b factor return values
Returns an array of numbers containing the data values of the channel number "ChNumber". ChNumber is the number of the channal as in .cfg file.
def fixLabel(label, maxlen, delim=None, repl='', truncend=True): """Truncate long graph and field labels. @param label: Label text. @param maxlen: Maximum field label length in characters. No maximum field label length is enforced by default. @param delim: Delimiter for field labels field labels longer than maxlen will preferably be truncated at delimiter. @param repl: Replacement string for truncated part. @param truncend: Truncate the end of label name if True. (Default) The beginning part of label will be truncated if False. """ if len(label) <= maxlen: return label else: maxlen -= len(repl) if delim is not None: if truncend: end = label.rfind(delim, 0, maxlen) if end > 0: return label[:end+1] + repl else: start = label.find(delim, len(label) - maxlen) if start > 0: return repl + label[start:] if truncend: return label[:maxlen] + repl else: return repl + label[-maxlen:]
Truncate long graph and field labels. @param label: Label text. @param maxlen: Maximum field label length in characters. No maximum field label length is enforced by default. @param delim: Delimiter for field labels field labels longer than maxlen will preferably be truncated at delimiter. @param repl: Replacement string for truncated part. @param truncend: Truncate the end of label name if True. (Default) The beginning part of label will be truncated if False.
def get_special_scen_code(regions, emissions): """ Get special code for MAGICC6 SCEN files. At the top of every MAGICC6 and MAGICC5 SCEN file there is a two digit number. The first digit, the 'scenfile_region_code' tells MAGICC how many regions data is being provided for. The second digit, the 'scenfile_emissions_code', tells MAGICC which gases are in the SCEN file. The variables which are part of ``PART_OF_SCENFILE_WITH_EMISSIONS_CODE_1`` are the emissions species which are expected when scenfile_emissions_code is 1. Similarly, ``PART_OF_SCENFILE_WITH_EMISSIONS_CODE_0`` defines the emissions species which are expected when scenfile_emissions_code is 0. Having these definitions allows Pymagicc to check that the right set of emissions has been provided before writing SCEN files. Parameters ---------- region : list_like Regions to get code for. emissions : list-like Emissions to get code for. Raises ------ ValueError If the special scen code cannot be determined. Returns ------- int The special scen code for the regions-emissions combination provided. """ if sorted(set(PART_OF_SCENFILE_WITH_EMISSIONS_CODE_0)) == sorted(set(emissions)): scenfile_emissions_code = 0 elif sorted(set(PART_OF_SCENFILE_WITH_EMISSIONS_CODE_1)) == sorted(set(emissions)): scenfile_emissions_code = 1 else: msg = "Could not determine scen special code for emissions {}".format(emissions) raise ValueError(msg) if set(regions) == set(["WORLD"]): scenfile_region_code = 1 elif set(regions) == set(["WORLD", "OECD90", "REF", "ASIA", "ALM"]): scenfile_region_code = 2 elif set(regions) == set(["WORLD", "R5OECD", "R5REF", "R5ASIA", "R5MAF", "R5LAM"]): scenfile_region_code = 3 elif set(regions) == set( ["WORLD", "R5OECD", "R5REF", "R5ASIA", "R5MAF", "R5LAM", "BUNKERS"] ): scenfile_region_code = 4 try: return scenfile_region_code * 10 + scenfile_emissions_code except NameError: msg = "Could not determine scen special code for regions {}".format(regions) raise ValueError(msg)
Get special code for MAGICC6 SCEN files. At the top of every MAGICC6 and MAGICC5 SCEN file there is a two digit number. The first digit, the 'scenfile_region_code' tells MAGICC how many regions data is being provided for. The second digit, the 'scenfile_emissions_code', tells MAGICC which gases are in the SCEN file. The variables which are part of ``PART_OF_SCENFILE_WITH_EMISSIONS_CODE_1`` are the emissions species which are expected when scenfile_emissions_code is 1. Similarly, ``PART_OF_SCENFILE_WITH_EMISSIONS_CODE_0`` defines the emissions species which are expected when scenfile_emissions_code is 0. Having these definitions allows Pymagicc to check that the right set of emissions has been provided before writing SCEN files. Parameters ---------- region : list_like Regions to get code for. emissions : list-like Emissions to get code for. Raises ------ ValueError If the special scen code cannot be determined. Returns ------- int The special scen code for the regions-emissions combination provided.
def update_record(self, name, recordid, content, username, password): ''' Update record ''' # headers = {'key': username, 'secret': password} req = requests.put(self.api_server + '/api/' + name + '/' + str(recordid), data=json.dumps(content), auth=(username, password)) return req
Update record
def make_cell(table, span, widths, heights, use_headers): """ Convert the contents of a span of the table to a grid table cell Parameters ---------- table : list of lists of str The table of rows containg strings to convert to a grid table span : list of lists of int list of [row, column] pairs that make up a span in the table widths : list of int list of the column widths of the table heights : list of int list of the heights of each row in the table use_headers : bool Whether or not to use headers in the table Returns ------- cell : dashtable.data2rst.Cell """ width = get_span_char_width(span, widths) height = get_span_char_height(span, heights) text_row = span[0][0] text_column = span[0][1] text = table[text_row][text_column] lines = text.split("\n") for i in range(len(lines)): width_difference = width - len(lines[i]) lines[i] = ''.join([lines[i], " " * width_difference]) height_difference = height - len(lines) empty_lines = [] for i in range(0, height_difference): empty_lines.append(" " * width) lines.extend(empty_lines) output = [ ''.join(["+", (width * "-") + "+"]) ] for i in range(0, height): output.append("|" + lines[i] + "|") if use_headers and span[0][0] == 0: symbol = "=" else: symbol = "-" output.append( ''.join(["+", width * symbol, "+"]) ) text = "\n".join(output) row_count = get_span_row_count(span) column_count = get_span_column_count(span) cell = Cell(text, text_row, text_column, row_count, column_count) return cell
Convert the contents of a span of the table to a grid table cell Parameters ---------- table : list of lists of str The table of rows containg strings to convert to a grid table span : list of lists of int list of [row, column] pairs that make up a span in the table widths : list of int list of the column widths of the table heights : list of int list of the heights of each row in the table use_headers : bool Whether or not to use headers in the table Returns ------- cell : dashtable.data2rst.Cell
def contourf_to_geojson_overlap(contourf, geojson_filepath=None, min_angle_deg=None, ndigits=5, unit='', stroke_width=1, fill_opacity=.9, geojson_properties=None, strdump=False, serialize=True): """Transform matplotlib.contourf to geojson with overlapping filled contours.""" polygon_features = [] contourf_idx = 0 for collection in contourf.collections: color = collection.get_facecolor() for path in collection.get_paths(): for coord in path.to_polygons(): if min_angle_deg: coord = keep_high_angle(coord, min_angle_deg) coord = np.around(coord, ndigits) if ndigits else coord polygon = Polygon(coordinates=[coord.tolist()]) fcolor = rgb2hex(color[0]) properties = set_contourf_properties(stroke_width, fcolor, fill_opacity, contourf.levels, contourf_idx, unit) if geojson_properties: properties.update(geojson_properties) feature = Feature(geometry=polygon, properties=properties) polygon_features.append(feature) contourf_idx += 1 feature_collection = FeatureCollection(polygon_features) return _render_feature_collection(feature_collection, geojson_filepath, strdump, serialize)
Transform matplotlib.contourf to geojson with overlapping filled contours.
def printData(self, output = sys.stdout): """Output all the file data to be written to any writable output""" self.printDatum("Name : ", self.fileName, output) self.printDatum("Author : ", self.author, output) self.printDatum("Repository : ", self.repository, output) self.printDatum("Category : ", self.category, output) self.printDatum("Downloads : ", self.downloads, output) self.printDatum("Date Uploaded : ", self.fileDate, output) self.printDatum("File Size : ", self.fileSize, output) self.printDatum("Documentation : ", self.documentation, output) self.printDatum("Source Code : ", self.sourceCode, output) self.printDatum("Description : ", self.description, output) # print("\n", output) print >> output, "\n\n"
Output all the file data to be written to any writable output
async def update_object(obj, only=None): """Update object asynchronously. :param obj: object to update :param only: list or tuple of fields to updata, is `None` then all fields updated This function does the same as `Model.save()`_ for already saved object, but it doesn't invoke ``save()`` method on model class. That is important to know if you overrided save method for your model. .. _Model.save(): http://peewee.readthedocs.io/en/latest/peewee/ api.html#Model.save """ # Here are private calls involved: # # - obj._data # - obj._meta # - obj._prune_fields() # - obj._pk_expr() # - obj._dirty.clear() # warnings.warn("update_object() is deprecated, Manager.update() " "should be used instead", DeprecationWarning) field_dict = dict(obj.__data__) pk_field = obj._meta.primary_key if only: field_dict = obj._prune_fields(field_dict, only) if not isinstance(pk_field, peewee.CompositeKey): field_dict.pop(pk_field.name, None) else: field_dict = obj._prune_fields(field_dict, obj.dirty_fields) rows = await update(obj.update(**field_dict).where(obj._pk_expr())) obj._dirty.clear() return rows
Update object asynchronously. :param obj: object to update :param only: list or tuple of fields to updata, is `None` then all fields updated This function does the same as `Model.save()`_ for already saved object, but it doesn't invoke ``save()`` method on model class. That is important to know if you overrided save method for your model. .. _Model.save(): http://peewee.readthedocs.io/en/latest/peewee/ api.html#Model.save
def detokenize(self, inputs, delim=' '): """ Detokenizes single sentence and removes token separator characters. :param inputs: sequence of tokens :param delim: tokenization delimiter returns: string representing detokenized sentence """ detok = delim.join([self.idx2tok[idx] for idx in inputs]) detok = detok.replace(self.separator + ' ', '') detok = detok.replace(self.separator, '') detok = detok.replace(config.BOS_TOKEN, '') detok = detok.replace(config.EOS_TOKEN, '') detok = detok.replace(config.PAD_TOKEN, '') detok = detok.strip() return detok
Detokenizes single sentence and removes token separator characters. :param inputs: sequence of tokens :param delim: tokenization delimiter returns: string representing detokenized sentence
def perform_request(self, request): ''' Sends request to cloud service server and return the response. ''' connection = self.get_connection(request) try: connection.putrequest(request.method, request.path) self.send_request_headers(connection, request.headers) self.send_request_body(connection, request.body) if DEBUG_REQUESTS and request.body: print('request:') try: print(request.body) except: # pylint: disable=bare-except pass resp = connection.getresponse() status = int(resp.status) message = resp.reason respheaders = resp.getheaders() # for consistency across platforms, make header names lowercase for i, value in enumerate(respheaders): respheaders[i] = (value[0].lower(), value[1]) respbody = None if resp.length is None: respbody = resp.read() elif resp.length > 0: respbody = resp.read(resp.length) if DEBUG_RESPONSES and respbody: print('response:') try: print(respbody) except: # pylint: disable=bare-except pass response = HTTPResponse( status, resp.reason, respheaders, respbody) if status == 307: new_url = urlparse(dict(respheaders)['location']) request.host = new_url.hostname request.path = new_url.path request.path, request.query = self._update_request_uri_query(request) return self.perform_request(request) if status >= 300: raise HTTPError(status, message, respheaders, respbody) return response finally: connection.close()
Sends request to cloud service server and return the response.
def DateStringToDateObject(date_string): """Return a date object for a string "YYYYMMDD".""" # If this becomes a bottleneck date objects could be cached if re.match('^\d{8}$', date_string) == None: return None try: return datetime.date(int(date_string[0:4]), int(date_string[4:6]), int(date_string[6:8])) except ValueError: return None
Return a date object for a string "YYYYMMDD".
def _load(self, filename=None): """Load the Himawari AHI RSR data for the band requested """ if not filename: filename = self.filename wb_ = open_workbook(filename) self.rsr = {} sheet_names = [] for sheet in wb_.sheets(): if sheet.name in ['Title', ]: continue ch_name = AHI_BAND_NAMES.get( sheet.name.strip(), sheet.name.strip()) sheet_names.append(sheet.name.strip()) self.rsr[ch_name] = {'wavelength': None, 'response': None} wvl = np.array( sheet.col_values(0, start_rowx=5, end_rowx=5453)) resp = np.array( sheet.col_values(2, start_rowx=5, end_rowx=5453)) self.rsr[ch_name]['wavelength'] = wvl self.rsr[ch_name]['response'] = resp
Load the Himawari AHI RSR data for the band requested
def get(remote_file, local_file): """ Retrieve a file from the board. Get will download a file from the board and print its contents or save it locally. You must pass at least one argument which is the path to the file to download from the board. If you don't specify a second argument then the file contents will be printed to standard output. However if you pass a file name as the second argument then the contents of the downloaded file will be saved to that file (overwriting anything inside it!). For example to retrieve the boot.py and print it out run: ampy --port /board/serial/port get boot.py Or to get main.py and save it as main.py locally run: ampy --port /board/serial/port get main.py main.py """ # Get the file contents. board_files = files.Files(_board) contents = board_files.get(remote_file) # Print the file out if no local file was provided, otherwise save it. if local_file is None: print(contents.decode("utf-8")) else: local_file.write(contents)
Retrieve a file from the board. Get will download a file from the board and print its contents or save it locally. You must pass at least one argument which is the path to the file to download from the board. If you don't specify a second argument then the file contents will be printed to standard output. However if you pass a file name as the second argument then the contents of the downloaded file will be saved to that file (overwriting anything inside it!). For example to retrieve the boot.py and print it out run: ampy --port /board/serial/port get boot.py Or to get main.py and save it as main.py locally run: ampy --port /board/serial/port get main.py main.py
def get_relative_abundance(biomfile): """ Return arcsine transformed relative abundance from a BIOM format file. :type biomfile: BIOM format file :param biomfile: BIOM format file used to obtain relative abundances for each OTU in a SampleID, which are used as node sizes in network plots. :type return: Dictionary of dictionaries. :return: Dictionary keyed on SampleID whose value is a dictionarykeyed on OTU Name whose value is the arc sine tranfsormed relative abundance value for that SampleID-OTU Name pair. """ biomf = biom.load_table(biomfile) norm_biomf = biomf.norm(inplace=False) rel_abd = {} for sid in norm_biomf.ids(): rel_abd[sid] = {} for otuid in norm_biomf.ids("observation"): otuname = oc.otu_name(norm_biomf.metadata(otuid, axis="observation")["taxonomy"]) otuname = " ".join(otuname.split("_")) abd = norm_biomf.get_value_by_ids(otuid, sid) rel_abd[sid][otuname] = abd ast_rel_abd = bc.arcsine_sqrt_transform(rel_abd) return ast_rel_abd
Return arcsine transformed relative abundance from a BIOM format file. :type biomfile: BIOM format file :param biomfile: BIOM format file used to obtain relative abundances for each OTU in a SampleID, which are used as node sizes in network plots. :type return: Dictionary of dictionaries. :return: Dictionary keyed on SampleID whose value is a dictionarykeyed on OTU Name whose value is the arc sine tranfsormed relative abundance value for that SampleID-OTU Name pair.
def pool(arr, block_size, func, cval=0, preserve_dtype=True): """ Resize an array by pooling values within blocks. dtype support:: * ``uint8``: yes; fully tested * ``uint16``: yes; tested * ``uint32``: yes; tested (2) * ``uint64``: no (1) * ``int8``: yes; tested * ``int16``: yes; tested * ``int32``: yes; tested (2) * ``int64``: no (1) * ``float16``: yes; tested * ``float32``: yes; tested * ``float64``: yes; tested * ``float128``: yes; tested (2) * ``bool``: yes; tested - (1) results too inaccurate (at least when using np.average as func) - (2) Note that scikit-image documentation says that the wrapped pooling function converts inputs to float64. Actual tests showed no indication of that happening (at least when using preserve_dtype=True). Parameters ---------- arr : (H,W) ndarray or (H,W,C) ndarray Image-like array to pool. Ideally of datatype ``numpy.float64``. block_size : int or tuple of int Spatial size of each group of values to pool, aka kernel size. If a single integer, then a symmetric block of that size along height and width will be used. If a tuple of two values, it is assumed to be the block size along height and width of the image-like, with pooling happening per channel. If a tuple of three values, it is assumed to be the block size along height, width and channels. func : callable Function to apply to a given block in order to convert it to a single number, e.g. :func:`numpy.average`, :func:`numpy.min`, :func:`numpy.max`. cval : number, optional Value to use in order to pad the array along its border if the array cannot be divided by `block_size` without remainder. preserve_dtype : bool, optional Whether to convert the array back to the input datatype if it is changed away from that in the pooling process. Returns ------- arr_reduced : (H',W') ndarray or (H',W',C') ndarray Array after pooling. """ # TODO find better way to avoid circular import from . import dtypes as iadt iadt.gate_dtypes(arr, allowed=["bool", "uint8", "uint16", "uint32", "int8", "int16", "int32", "float16", "float32", "float64", "float128"], disallowed=["uint64", "uint128", "uint256", "int64", "int128", "int256", "float256"], augmenter=None) do_assert(arr.ndim in [2, 3]) is_valid_int = is_single_integer(block_size) and block_size >= 1 is_valid_tuple = is_iterable(block_size) and len(block_size) in [2, 3] \ and [is_single_integer(val) and val >= 1 for val in block_size] do_assert(is_valid_int or is_valid_tuple) if is_single_integer(block_size): block_size = [block_size, block_size] if len(block_size) < arr.ndim: block_size = list(block_size) + [1] input_dtype = arr.dtype arr_reduced = skimage.measure.block_reduce(arr, tuple(block_size), func, cval=cval) if preserve_dtype and arr_reduced.dtype.type != input_dtype: arr_reduced = arr_reduced.astype(input_dtype) return arr_reduced
Resize an array by pooling values within blocks. dtype support:: * ``uint8``: yes; fully tested * ``uint16``: yes; tested * ``uint32``: yes; tested (2) * ``uint64``: no (1) * ``int8``: yes; tested * ``int16``: yes; tested * ``int32``: yes; tested (2) * ``int64``: no (1) * ``float16``: yes; tested * ``float32``: yes; tested * ``float64``: yes; tested * ``float128``: yes; tested (2) * ``bool``: yes; tested - (1) results too inaccurate (at least when using np.average as func) - (2) Note that scikit-image documentation says that the wrapped pooling function converts inputs to float64. Actual tests showed no indication of that happening (at least when using preserve_dtype=True). Parameters ---------- arr : (H,W) ndarray or (H,W,C) ndarray Image-like array to pool. Ideally of datatype ``numpy.float64``. block_size : int or tuple of int Spatial size of each group of values to pool, aka kernel size. If a single integer, then a symmetric block of that size along height and width will be used. If a tuple of two values, it is assumed to be the block size along height and width of the image-like, with pooling happening per channel. If a tuple of three values, it is assumed to be the block size along height, width and channels. func : callable Function to apply to a given block in order to convert it to a single number, e.g. :func:`numpy.average`, :func:`numpy.min`, :func:`numpy.max`. cval : number, optional Value to use in order to pad the array along its border if the array cannot be divided by `block_size` without remainder. preserve_dtype : bool, optional Whether to convert the array back to the input datatype if it is changed away from that in the pooling process. Returns ------- arr_reduced : (H',W') ndarray or (H',W',C') ndarray Array after pooling.
def decode(self, bytes, raw=False): """decode(bytearray, raw=False) -> value Decodes the given bytearray according the corresponding EVR Definition (:class:`EVRDefn`) for the underlying 'MSB_U16' EVR code. If the optional parameter ``raw`` is ``True``, the EVR code itself will be returned instead of the EVR Definition (:class:`EVRDefn`). """ code = super(EVRType, self).decode(bytes) result = None if raw: result = code elif code in self.evrs.codes: result = self.evrs.codes[code] else: result = code log.warn('Unrecognized EVR code: %d' % code) return result
decode(bytearray, raw=False) -> value Decodes the given bytearray according the corresponding EVR Definition (:class:`EVRDefn`) for the underlying 'MSB_U16' EVR code. If the optional parameter ``raw`` is ``True``, the EVR code itself will be returned instead of the EVR Definition (:class:`EVRDefn`).
def simple_newton(f, x0, lb=None, ub=None, infos=False, verbose=False, maxit=50, tol=1e-8, eps=1e-8, numdiff=True): '''Solves many independent systems f(x)=0 simultaneously using a simple gradient descent. :param f: objective function to be solved with values p x N . The second output argument represents the derivative with values in (p x p x N) :param x0: initial value ( p x N ) :return: solution x such that f(x) = 0 ''' precision = x0.dtype # default tolerance should depend on precision from numpy.linalg import solve err = 1 it = 0 while err > tol and it <= maxit: if not numdiff: [res,dres] = f(x0) else: res = f(x0) dres = numpy.zeros( (res.shape[0], x0.shape[0]), dtype=precision ) for i in range(x0.shape[0]): xi = x0.copy() xi[i] += eps resi = f(xi) dres[:,i] = (resi - res)/eps dx = - solve(dres,res) x = x0 + dx print('x0 : {}'.format(x0)) err = abs(res).max() print('iteration {} {}'.format(it, err)) x0 = x it += 1 if not infos: return x else: return [x, it]
Solves many independent systems f(x)=0 simultaneously using a simple gradient descent. :param f: objective function to be solved with values p x N . The second output argument represents the derivative with values in (p x p x N) :param x0: initial value ( p x N ) :return: solution x such that f(x) = 0
def construct_user_list(raw_users=None): """Construct a list of User objects from a list of dicts.""" users = Users(oktypes=User) for user_dict in raw_users: public_keys = None if user_dict.get('public_keys'): public_keys = [PublicKey(b64encoded=x, raw=None) for x in user_dict.get('public_keys')] users.append(User(name=user_dict.get('name'), passwd=user_dict.get('passwd'), uid=user_dict.get('uid'), gid=user_dict.get('gid'), home_dir=user_dict.get('home_dir'), gecos=user_dict.get('gecos'), shell=user_dict.get('shell'), public_keys=public_keys, sudoers_entry=user_dict.get('sudoers_entry'))) return users
Construct a list of User objects from a list of dicts.
def bond_canonical_statistics( microcanonical_statistics, convolution_factors, **kwargs ): """ canonical cluster statistics for a single run and a single probability Parameters ---------- microcanonical_statistics : ndarray Return value of `bond_microcanonical_statistics` convolution_factors : 1-D array_like The coefficients of the convolution for the given probabilty ``p`` and for each occupation number ``n``. Returns ------- ret : ndarray of size ``1`` Structured array with dtype as returned by `canonical_statistics_dtype` ret['percolation_probability'] : ndarray of float The "percolation probability" of this run at the value of ``p``. Only exists if `microcanonical_statistics` argument has the ``has_spanning_cluster`` field. ret['max_cluster_size'] : ndarray of int Weighted size of the largest cluster (absolute number of sites) ret['moments'] : 1-D :py:class:`numpy.ndarray` of float Array of size ``5``. The ``k``-th entry is the weighted ``k``-th raw moment of the (absolute) cluster size distribution, with ``k`` ranging from ``0`` to ``4``. See Also -------- bond_microcanonical_statistics canonical_statistics_dtype """ # initialize return array spanning_cluster = ( 'has_spanning_cluster' in microcanonical_statistics.dtype.names ) ret = np.empty(1, dtype=canonical_statistics_dtype(spanning_cluster)) # compute percolation probability if spanning_cluster: ret['percolation_probability'] = np.sum( convolution_factors * microcanonical_statistics['has_spanning_cluster'] ) # convolve maximum cluster size ret['max_cluster_size'] = np.sum( convolution_factors * microcanonical_statistics['max_cluster_size'] ) # convolve moments ret['moments'] = np.sum( convolution_factors[:, np.newaxis] * microcanonical_statistics['moments'], axis=0, ) # return convolved cluster statistics return ret
canonical cluster statistics for a single run and a single probability Parameters ---------- microcanonical_statistics : ndarray Return value of `bond_microcanonical_statistics` convolution_factors : 1-D array_like The coefficients of the convolution for the given probabilty ``p`` and for each occupation number ``n``. Returns ------- ret : ndarray of size ``1`` Structured array with dtype as returned by `canonical_statistics_dtype` ret['percolation_probability'] : ndarray of float The "percolation probability" of this run at the value of ``p``. Only exists if `microcanonical_statistics` argument has the ``has_spanning_cluster`` field. ret['max_cluster_size'] : ndarray of int Weighted size of the largest cluster (absolute number of sites) ret['moments'] : 1-D :py:class:`numpy.ndarray` of float Array of size ``5``. The ``k``-th entry is the weighted ``k``-th raw moment of the (absolute) cluster size distribution, with ``k`` ranging from ``0`` to ``4``. See Also -------- bond_microcanonical_statistics canonical_statistics_dtype
def _add_to_index(index, obj): """Adds the given object ``obj`` to the given ``index``""" id_set = index.value_map.setdefault(indexed_value(index, obj), set()) if index.unique: if len(id_set) > 0: raise UniqueConstraintError() id_set.add(obj.id)
Adds the given object ``obj`` to the given ``index``
def highlight_text(text, lexer_name='python', **kwargs): r""" SeeAlso: color_text """ # Resolve extensions to languages lexer_name = { 'py': 'python', 'h': 'cpp', 'cpp': 'cpp', 'c': 'cpp', }.get(lexer_name.replace('.', ''), lexer_name) if lexer_name in ['red', 'yellow', 'blue', 'green']: # hack for coloring return color_text(text, lexer_name) import utool as ut if ENABLE_COLORS: try: import pygments import pygments.lexers import pygments.formatters #from pygments import highlight #from pygments.lexers import get_lexer_by_name #from pygments.formatters import TerminalFormatter #if ut.WIN32: # assert False # #formater = pygments.formatters.terminal256.Terminal256Formatter() # import pygments.formatters.terminal256 # formater = pygments.formatters.terminal256.Terminal256Formatter() #else: import pygments.formatters.terminal formater = pygments.formatters.terminal.TerminalFormatter(bg='dark') lexer = pygments.lexers.get_lexer_by_name(lexer_name, **kwargs) return pygments.highlight(text, lexer, formater) except Exception: if ut.SUPER_STRICT: raise return text return text
r""" SeeAlso: color_text
def datasets_view(self, owner_slug, dataset_slug, **kwargs): # noqa: E501 """Show details about a dataset # noqa: E501 This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.datasets_view(owner_slug, dataset_slug, async_req=True) >>> result = thread.get() :param async_req bool :param str owner_slug: Dataset owner (required) :param str dataset_slug: Dataset name (required) :return: Result If the method is called asynchronously, returns the request thread. """ kwargs['_return_http_data_only'] = True if kwargs.get('async_req'): return self.datasets_view_with_http_info(owner_slug, dataset_slug, **kwargs) # noqa: E501 else: (data) = self.datasets_view_with_http_info(owner_slug, dataset_slug, **kwargs) # noqa: E501 return data
Show details about a dataset # noqa: E501 This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.datasets_view(owner_slug, dataset_slug, async_req=True) >>> result = thread.get() :param async_req bool :param str owner_slug: Dataset owner (required) :param str dataset_slug: Dataset name (required) :return: Result If the method is called asynchronously, returns the request thread.
def p_expr_LE_expr(p): """ expr : expr LE expr """ p[0] = make_binary(p.lineno(2), 'LE', p[1], p[3], lambda x, y: x <= y)
expr : expr LE expr
def __convertRlocToRouterId(self, xRloc16): """mapping Rloc16 to router id Args: xRloc16: hex rloc16 short address Returns: actual router id allocated by leader """ routerList = [] routerList = self.__sendCommand(WPANCTL_CMD + 'getprop -v Thread:RouterTable') print routerList print xRloc16 for line in routerList: if re.match('\[|\]', line): continue if re.match(WPAN_CARRIER_PROMPT, line, re.M|re.I): break router = [] router = self.__stripValue(line).split(',') for item in router: if 'RouterId' in item: routerid = item.split(':')[1] elif 'RLOC16' in line: rloc16 = line.split(':')[1] else: pass # process input rloc16 if isinstance(xRloc16, str): rloc16 = '0x' + rloc16 if rloc16 == xRloc16: return routerid elif isinstance(xRloc16, int): if int(rloc16, 16) == xRloc16: return routerid else: pass return None
mapping Rloc16 to router id Args: xRloc16: hex rloc16 short address Returns: actual router id allocated by leader
def on_menu_exit(self, event): """ Runs whenever Thellier GUI exits """ if self.close_warning: TEXT = "Data is not saved to a file yet!\nTo properly save your data:\n1) Analysis --> Save current interpretations to a redo file.\nor\n1) File --> Save MagIC tables.\n\n Press OK to exit without saving." dlg1 = wx.MessageDialog( None, caption="Warning:", message=TEXT, style=wx.OK | wx.CANCEL | wx.ICON_EXCLAMATION) if self.show_dlg(dlg1) == wx.ID_OK: dlg1.Destroy() self.GUI_log.close() self.Destroy() # if a custom quit event is specified, fire it if self.evt_quit: event = self.evt_quit(self.GetId()) self.GetEventHandler().ProcessEvent(event) if self.standalone: sys.exit() else: self.GUI_log.close() self.Destroy() # if a custom quit event is specified, fire it if self.evt_quit: event = self.evt_quit(self.GetId()) self.GetEventHandler().ProcessEvent(event) if self.standalone: sys.exit()
Runs whenever Thellier GUI exits
def count_function(func): """ Decorator for functions that return a collection (technically a dict of collections) that should be counted up. Also automatically falls back to the Cohort-default filter_fn and normalized_per_mb if not specified. """ # Fall back to Cohort-level defaults. @use_defaults @wraps(func) def wrapper(row, cohort, filter_fn=None, normalized_per_mb=None, **kwargs): per_patient_data = func(row=row, cohort=cohort, filter_fn=filter_fn, normalized_per_mb=normalized_per_mb, **kwargs) patient_id = row["patient_id"] if patient_id in per_patient_data: count = len(per_patient_data[patient_id]) if normalized_per_mb: count /= float(get_patient_to_mb(cohort)[patient_id]) return count return np.nan return wrapper
Decorator for functions that return a collection (technically a dict of collections) that should be counted up. Also automatically falls back to the Cohort-default filter_fn and normalized_per_mb if not specified.
def index_based_complete(self, text: str, line: str, begidx: int, endidx: int, index_dict: Mapping[int, Union[Iterable, Callable]], all_else: Union[None, Iterable, Callable] = None) -> List[str]: """ Tab completes based on a fixed position in the input string :param text: the string prefix we are attempting to match (all returned matches must begin with it) :param line: the current input line with leading whitespace removed :param begidx: the beginning index of the prefix text :param endidx: the ending index of the prefix text :param index_dict: dictionary whose structure is the following: keys - 0-based token indexes into command line that determine which tokens perform tab completion values - there are two types of values 1. iterable list of strings to match against (dictionaries, lists, etc.) 2. function that performs tab completion (ex: path_complete) :param all_else: an optional parameter for tab completing any token that isn't at an index in index_dict :return: a list of possible tab completions """ # Get all tokens through the one being completed tokens, _ = self.tokens_for_completion(line, begidx, endidx) if not tokens: return [] matches = [] # Get the index of the token being completed index = len(tokens) - 1 # Check if token is at an index in the dictionary if index in index_dict: match_against = index_dict[index] else: match_against = all_else # Perform tab completion using a Collection if isinstance(match_against, Collection): matches = self.basic_complete(text, line, begidx, endidx, match_against) # Perform tab completion using a function elif callable(match_against): matches = match_against(text, line, begidx, endidx) return matches
Tab completes based on a fixed position in the input string :param text: the string prefix we are attempting to match (all returned matches must begin with it) :param line: the current input line with leading whitespace removed :param begidx: the beginning index of the prefix text :param endidx: the ending index of the prefix text :param index_dict: dictionary whose structure is the following: keys - 0-based token indexes into command line that determine which tokens perform tab completion values - there are two types of values 1. iterable list of strings to match against (dictionaries, lists, etc.) 2. function that performs tab completion (ex: path_complete) :param all_else: an optional parameter for tab completing any token that isn't at an index in index_dict :return: a list of possible tab completions
def from_record(cls, record, crs): """Load vector from record.""" if 'type' not in record: raise TypeError("The data isn't a valid record.") return cls(to_shape(record), crs)
Load vector from record.
def prepend(cls, d, s, filter=Filter()): """ Prepend schema object's from B{s}ource list to the B{d}estination list while applying the filter. @param d: The destination list. @type d: list @param s: The source list. @type s: list @param filter: A filter that allows items to be prepended. @type filter: L{Filter} """ i = 0 for x in s: if x in filter: d.insert(i, x) i += 1
Prepend schema object's from B{s}ource list to the B{d}estination list while applying the filter. @param d: The destination list. @type d: list @param s: The source list. @type s: list @param filter: A filter that allows items to be prepended. @type filter: L{Filter}
def send_document(chat_id, document, reply_to_message_id=None, reply_markup=None, **kwargs): """ Use this method to send general files. :param chat_id: Unique identifier for the message recipient — User or GroupChat id :param document: File to send. You can either pass a file_id as String to resend a file that is already on the Telegram servers, or upload a new file using multipart/form-data. :param reply_to_message_id: If the message is a reply, ID of the original message :param reply_markup: Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. :param \*\*kwargs: Args that get passed down to :class:`TelegramBotRPCRequest` :type chat_id: int :type document: InputFile or str :type reply_to_message_id: int :type reply_markup: ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply :returns: On success, the sent Message is returned. :rtype: TelegramBotRPCRequest """ files = None if isinstance(document, InputFile): files = [document] document = None elif not isinstance(document, str): raise Exception('document must be instance of InputFile or str') # required args params = dict( chat_id=chat_id, document=document ) # optional args params.update( _clean_params( reply_to_message_id=reply_to_message_id, reply_markup=reply_markup ) ) return TelegramBotRPCRequest('sendDocument', params=params, files=files, on_result=Message.from_result, **kwargs)
Use this method to send general files. :param chat_id: Unique identifier for the message recipient — User or GroupChat id :param document: File to send. You can either pass a file_id as String to resend a file that is already on the Telegram servers, or upload a new file using multipart/form-data. :param reply_to_message_id: If the message is a reply, ID of the original message :param reply_markup: Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. :param \*\*kwargs: Args that get passed down to :class:`TelegramBotRPCRequest` :type chat_id: int :type document: InputFile or str :type reply_to_message_id: int :type reply_markup: ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply :returns: On success, the sent Message is returned. :rtype: TelegramBotRPCRequest
def get_login_redirect(self, provider, account): """Return url to redirect authenticated users.""" info = self.model._meta.app_label, self.model._meta.model_name # inline import to prevent circular imports. from .admin import PRESERVED_FILTERS_SESSION_KEY preserved_filters = self.request.session.get(PRESERVED_FILTERS_SESSION_KEY, None) redirect_url = reverse('admin:%s_%s_changelist' % info) if preserved_filters: redirect_url = add_preserved_filters( {'preserved_filters': preserved_filters, 'opts': self.model._meta}, redirect_url) return redirect_url
Return url to redirect authenticated users.
def delete(self, name, **kwargs): """Delete a Label on the server. Args: name: The name of the label **kwargs: Extra options to send to the server (e.g. sudo) Raises: GitlabAuthenticationError: If authentication is not correct GitlabDeleteError: If the server cannot perform the request """ self.gitlab.http_delete(self.path, query_data={'name': name}, **kwargs)
Delete a Label on the server. Args: name: The name of the label **kwargs: Extra options to send to the server (e.g. sudo) Raises: GitlabAuthenticationError: If authentication is not correct GitlabDeleteError: If the server cannot perform the request
def _normalize_path(self, path): """ Normalizes a file path so that it returns a path relative to the root repo directory. """ norm_path = os.path.normpath(path) return os.path.relpath(norm_path, start=self._get_working_dir())
Normalizes a file path so that it returns a path relative to the root repo directory.
def plot(self, series, series_diff=None, label='', color=None, style=None): ''' :param pandas.Series series: The series to be plotted, all values must be positive if stacked is True. :param pandas.Series series_diff: The series representing the diff that will be plotted in the bottom part. :param string label: The label for the series. :param integer/string color: Color for the plot. Can be an index for the color from COLORS or a key(string) from CNAMES. :param string style: Style forwarded to the plt.plot. ''' color = self.get_color(color) if series_diff is None and self.autodiffs: series_diff = series.diff() if self.stacked: series += self.running_sum self.ax1.fill_between(series.index, self.running_sum, series, facecolor=ALPHAS[color]) self.running_sum = series self.ax1.set_ylim(bottom=0, top=int(series.max() * 1.05)) series.plot(label=label, c=COLORS[color], linewidth=2, style=style, ax=self.ax1) if series_diff is not None: series_diff.plot(label=label, c=COLORS[color], linewidth=2, style=style, ax=self.ax2)
:param pandas.Series series: The series to be plotted, all values must be positive if stacked is True. :param pandas.Series series_diff: The series representing the diff that will be plotted in the bottom part. :param string label: The label for the series. :param integer/string color: Color for the plot. Can be an index for the color from COLORS or a key(string) from CNAMES. :param string style: Style forwarded to the plt.plot.
def verify(self, tool): """ check that the tool exists """ if os.path.isfile(tool['file']): print('Toolbox: program exists = TOK :: ' + tool['file']) return True else: print('Toolbox: program exists = FAIL :: ' + tool['file']) return False
check that the tool exists
def template(self): """ Create a rules file in ipset --restore format """ s = Template(self._IPSET_TEMPLATE) return s.substitute(sets='\n'.join(self.sets), date=datetime.today())
Create a rules file in ipset --restore format
def add_training_sample(self, text=u'', lang=''): """ Initial step for adding new sample to training data. You need to call `save_training_samples()` afterwards. :param text: Sample text to be added. :param lang: Language label for the input text. """ self.trainer.add(text=text, lang=lang)
Initial step for adding new sample to training data. You need to call `save_training_samples()` afterwards. :param text: Sample text to be added. :param lang: Language label for the input text.
async def send_rpc(self, conn_id, address, rpc_id, payload, timeout): """Send an RPC to a device. See :meth:`AbstractDeviceAdapter.send_rpc`. """ adapter_id = self._get_property(conn_id, 'adapter') return await self.adapters[adapter_id].send_rpc(conn_id, address, rpc_id, payload, timeout)
Send an RPC to a device. See :meth:`AbstractDeviceAdapter.send_rpc`.
def users_get_avatar(self, user_id=None, username=None, **kwargs): """Gets the URL for a user’s avatar.""" if user_id: return self.__call_api_get('users.getAvatar', userId=user_id, kwargs=kwargs) elif username: return self.__call_api_get('users.getAvatar', username=username, kwargs=kwargs) else: raise RocketMissingParamException('userID or username required')
Gets the URL for a user’s avatar.
def _at_if(self, calculator, rule, scope, block): """ Implements @if and @else if """ # "@if" indicates whether any kind of `if` since the last `@else` has # succeeded, in which case `@else if` should be skipped if block.directive != '@if': if '@if' not in rule.options: raise SyntaxError("@else with no @if (%s)" % (rule.file_and_line,)) if rule.options['@if']: # Last @if succeeded; stop here return condition = calculator.calculate(block.argument) if condition: inner_rule = rule.copy() inner_rule.unparsed_contents = block.unparsed_contents if not self.should_scope_loop_in_rule(inner_rule): # DEVIATION: Allow not creating a new namespace inner_rule.namespace = rule.namespace self.manage_children(inner_rule, scope) rule.options['@if'] = condition
Implements @if and @else if
def run_in_transaction(self, func, *args, **kw): """Perform a unit of work in a transaction, retrying on abort. :type func: callable :param func: takes a required positional argument, the transaction, and additional positional / keyword arguments as supplied by the caller. :type args: tuple :param args: additional positional arguments to be passed to ``func``. :type kw: dict :param kw: optional keyword arguments to be passed to ``func``. If passed, "timeout_secs" will be removed and used to override the default timeout. :rtype: :class:`datetime.datetime` :returns: timestamp of committed transaction """ # Sanity check: Is there a transaction already running? # If there is, then raise a red flag. Otherwise, mark that this one # is running. if getattr(self._local, "transaction_running", False): raise RuntimeError("Spanner does not support nested transactions.") self._local.transaction_running = True # Check out a session and run the function in a transaction; once # done, flip the sanity check bit back. try: with SessionCheckout(self._pool) as session: return session.run_in_transaction(func, *args, **kw) finally: self._local.transaction_running = False
Perform a unit of work in a transaction, retrying on abort. :type func: callable :param func: takes a required positional argument, the transaction, and additional positional / keyword arguments as supplied by the caller. :type args: tuple :param args: additional positional arguments to be passed to ``func``. :type kw: dict :param kw: optional keyword arguments to be passed to ``func``. If passed, "timeout_secs" will be removed and used to override the default timeout. :rtype: :class:`datetime.datetime` :returns: timestamp of committed transaction
def get_album_songs(self, album_id): """Get a album's all songs. warning: use old api. :params album_id: album id. :return: a list of Song object. """ url = 'http://music.163.com/api/album/{}/'.format(album_id) result = self.get_request(url) songs = result['album']['songs'] songs = [Song(song['id'], song['name']) for song in songs] return songs
Get a album's all songs. warning: use old api. :params album_id: album id. :return: a list of Song object.
def resolve_symbol(self, symbol, bCaseSensitive = False): """ Resolves a debugging symbol's address. @type symbol: str @param symbol: Name of the symbol to resolve. @type bCaseSensitive: bool @param bCaseSensitive: C{True} for case sensitive matches, C{False} for case insensitive. @rtype: int or None @return: Memory address of symbol. C{None} if not found. """ if bCaseSensitive: for (SymbolName, SymbolAddress, SymbolSize) in self.iter_symbols(): if symbol == SymbolName: return SymbolAddress else: symbol = symbol.lower() for (SymbolName, SymbolAddress, SymbolSize) in self.iter_symbols(): if symbol == SymbolName.lower(): return SymbolAddress
Resolves a debugging symbol's address. @type symbol: str @param symbol: Name of the symbol to resolve. @type bCaseSensitive: bool @param bCaseSensitive: C{True} for case sensitive matches, C{False} for case insensitive. @rtype: int or None @return: Memory address of symbol. C{None} if not found.
def _fix_unsafe(shell_input): """Find characters used to escape from a string into a shell, and wrap them in quotes if they exist. Regex pilfered from Python3 :mod:`shlex` module. :param str shell_input: The input intended for the GnuPG process. """ _unsafe = re.compile(r'[^\w@%+=:,./-]', 256) try: if len(_unsafe.findall(shell_input)) == 0: return shell_input.strip() else: clean = "'" + shell_input.replace("'", "'\"'\"'") + "'" return clean except TypeError: return None
Find characters used to escape from a string into a shell, and wrap them in quotes if they exist. Regex pilfered from Python3 :mod:`shlex` module. :param str shell_input: The input intended for the GnuPG process.
def padded_to_same_length(seq1, seq2, item=0): """Return a pair of sequences of the same length by padding the shorter sequence with ``item``. The padded sequence is a tuple. The unpadded sequence is returned as-is. """ len1, len2 = len(seq1), len(seq2) if len1 == len2: return (seq1, seq2) elif len1 < len2: return (cons.ed(seq1, yield_n(len2-len1, item)), seq2) else: return (seq1, cons.ed(seq2, yield_n(len1-len2, item)))
Return a pair of sequences of the same length by padding the shorter sequence with ``item``. The padded sequence is a tuple. The unpadded sequence is returned as-is.
def validate(self): """Checks integrity of workflow and reports any problems with it. Detects: - loops (tasks that wait on each other in a loop) :returns: empty list if valid, a list of errors if not """ results = [] from ..specs import Join def recursive_find_loop(task, history): current = history[:] current.append(task) if isinstance(task, Join): if task in history: msg = "Found loop with '%s': %s then '%s' again" % ( task.name, '->'.join([p.name for p in history]), task.name) raise Exception(msg) for predecessor in task.inputs: recursive_find_loop(predecessor, current) for parent in task.inputs: recursive_find_loop(parent, current) for task_id, task in list(self.task_specs.items()): # Check for cyclic waits try: recursive_find_loop(task, []) except Exception as exc: results.append(exc.__str__()) # Check for disconnected tasks if not task.inputs and task.name not in ['Start', 'Root']: if task.outputs: results.append("Task '%s' is disconnected (no inputs)" % task.name) else: LOG.debug("Task '%s' is not being used" % task.name) return results
Checks integrity of workflow and reports any problems with it. Detects: - loops (tasks that wait on each other in a loop) :returns: empty list if valid, a list of errors if not
def install(): """Basic Ceph client installation.""" ceph_dir = "/etc/ceph" if not os.path.exists(ceph_dir): os.mkdir(ceph_dir) apt_install('ceph-common', fatal=True)
Basic Ceph client installation.
def probe_image(self, labels, instance, column_name=None, num_scaled_images=50, top_percent=10): """ Get pixel importance of the image. It performs pixel sensitivity analysis by showing only the most important pixels to a certain label in the image. It uses integrated gradients to measure the importance of each pixel. Args: labels: labels to compute gradients from. instance: the prediction instance. It needs to conform to model's input. Can be a csv line string, or a dict. img_column_name: the name of the image column to probe. If there is only one image column it can be None. num_scaled_images: Number of scaled images to get grads from. For example, if 10, the image will be scaled by 0.1, 0.2, ..., 0,9, 1.0 and it will produce 10 images for grads computation. top_percent: The percentile of pixels to show only. for example, if 10, only top 10% impactful pixels will be shown and rest of the pixels will be black. Returns: A tuple. First is the resized original image (299x299x3). Second is a list of the visualization with same size that highlights the most important pixels, one per each label. """ if len(self._image_columns) > 1 and not column_name: raise ValueError('There are multiple image columns in the input of the model. ' + 'Please specify "column_name".') elif column_name and column_name not in self._image_columns: raise ValueError('Specified column_name "%s" not found in the model input.' % column_name) image_column_name = column_name if column_name else self._image_columns[0] if isinstance(instance, six.string_types): instance = next(csv.DictReader([instance], fieldnames=self._headers)) image_path = instance[image_column_name] with file_io.FileIO(image_path, 'rb') as fi: im = Image.open(fi) resized_image = im.resize((299, 299)) # Produce a list of scaled images, create instances (csv lines) from these images. step = 1. / num_scaled_images scales = np.arange(0.0, 1.0, step) + step csv_lines = [] for s in scales: pixels = (np.asarray(resized_image) * s).astype('uint8') scaled_image = Image.fromarray(pixels) buf = io.BytesIO() scaled_image.save(buf, "JPEG") encoded_image = base64.urlsafe_b64encode(buf.getvalue()).decode('ascii') instance_copy = dict(instance) instance_copy[image_column_name] = encoded_image buf = six.StringIO() writer = csv.DictWriter(buf, fieldnames=self._headers, lineterminator='') writer.writerow(instance_copy) csv_lines.append(buf.getvalue()) integrated_gradients_images = [] for label in labels: # Send to tf model to get gradients. grads = self._image_gradients(csv_lines, label, image_column_name) integrated_grads = resized_image * np.average(grads, axis=0) # Gray scale the grads by removing color dimension. # abs() is for getting the most impactful pixels regardless positive or negative. grayed = np.average(abs(integrated_grads), axis=2) grayed = np.transpose([grayed, grayed, grayed], axes=[1, 2, 0]) # Only show the most impactful pixels. p = np.percentile(grayed, 100 - top_percent) viz_window = np.where(grayed > p, 1, 0) vis = resized_image * viz_window im_vis = Image.fromarray(np.uint8(vis)) integrated_gradients_images.append(im_vis) return resized_image, integrated_gradients_images
Get pixel importance of the image. It performs pixel sensitivity analysis by showing only the most important pixels to a certain label in the image. It uses integrated gradients to measure the importance of each pixel. Args: labels: labels to compute gradients from. instance: the prediction instance. It needs to conform to model's input. Can be a csv line string, or a dict. img_column_name: the name of the image column to probe. If there is only one image column it can be None. num_scaled_images: Number of scaled images to get grads from. For example, if 10, the image will be scaled by 0.1, 0.2, ..., 0,9, 1.0 and it will produce 10 images for grads computation. top_percent: The percentile of pixels to show only. for example, if 10, only top 10% impactful pixels will be shown and rest of the pixels will be black. Returns: A tuple. First is the resized original image (299x299x3). Second is a list of the visualization with same size that highlights the most important pixels, one per each label.
def seek_to_end(self, *partitions): """Seek to the most recent available offset for partitions. Arguments: *partitions: Optionally provide specific TopicPartitions, otherwise default to all assigned partitions. Raises: AssertionError: If any partition is not currently assigned, or if no partitions are assigned. """ if not all([isinstance(p, TopicPartition) for p in partitions]): raise TypeError('partitions must be TopicPartition namedtuples') if not partitions: partitions = self._subscription.assigned_partitions() assert partitions, 'No partitions are currently assigned' else: for p in partitions: assert p in self._subscription.assigned_partitions(), 'Unassigned partition' for tp in partitions: log.debug("Seeking to end of partition %s", tp) self._subscription.need_offset_reset(tp, OffsetResetStrategy.LATEST)
Seek to the most recent available offset for partitions. Arguments: *partitions: Optionally provide specific TopicPartitions, otherwise default to all assigned partitions. Raises: AssertionError: If any partition is not currently assigned, or if no partitions are assigned.
def write_header(self, out_strm, delim, f1_num_fields, f2_num_fields, f1_header=None, f2_header=None, missing_val=None): """ Write the header for a joined file. If headers are provided for one or more of the input files, then a header is generated for the output file. Otherwise, this does not output anything. :param out_strm: write to this stream :param delim: :param f1_num_fields: the number of columns in the first file :param f2_num_fields: the number of columns in the second file :param f1_header: :param f2_header: :param missing_val: """ mm = f1_header != f2_header one_none = f1_header is None or f2_header is None if mm and one_none and missing_val is None: raise InvalidHeaderError("Cannot generate output header when one " + "input file is missing a header and no " + "missing value was provided to replace " + "unknown entries.") if f1_header is not None and f2_header is not None: out_strm.write(delim.join(f1_header) + delim + delim.join(f2_header) + "\n") elif f1_header is None and f2_header is not None: dummy_h = f1_num_fields * [missing_val] out_strm.write(delim.join(dummy_h) + delim + delim.join(f2_header) + "\n") elif f1_header is not None and f2_header is None: dummy_h = f2_num_fields * [missing_val] out_strm.write(delim.join(f1_header) + delim + delim.join(dummy_h) + "\n")
Write the header for a joined file. If headers are provided for one or more of the input files, then a header is generated for the output file. Otherwise, this does not output anything. :param out_strm: write to this stream :param delim: :param f1_num_fields: the number of columns in the first file :param f2_num_fields: the number of columns in the second file :param f1_header: :param f2_header: :param missing_val:
def updateFromKwargs(self, kwargs, properties, collector, **kw): """By default, the child values will be collapsed into a dictionary. If the parent is anonymous, this dictionary is the top-level properties.""" yield self.collectChildProperties(kwargs=kwargs, properties=properties, collector=collector, **kw) # default behavior is to set a property # -- use setdefault+update in order to collapse 'anonymous' nested # parameters correctly if self.name: d = properties.setdefault(self.name, {}) else: # if there's no name, collapse this nest all the way d = properties d.update(kwargs[self.fullName])
By default, the child values will be collapsed into a dictionary. If the parent is anonymous, this dictionary is the top-level properties.
def md2rst(md_lines): 'Only converts headers' lvl2header_char = {1: '=', 2: '-', 3: '~'} for md_line in md_lines: if md_line.startswith('#'): header_indent, header_text = md_line.split(' ', 1) yield header_text header_char = lvl2header_char[len(header_indent)] yield header_char * len(header_text) else: yield md_line
Only converts headers
def overlap(self, x, ctrs, kdtree=None): """Check how many balls `x` falls within. Uses a K-D Tree to perform the search if provided.""" q = len(self.within(x, ctrs, kdtree=kdtree)) return q
Check how many balls `x` falls within. Uses a K-D Tree to perform the search if provided.
def GET_subdomain_ops(self, path_info, txid): """ Get all subdomain operations processed in a given transaction. Returns the list of subdomains on success (can be empty) Returns 502 on failure to get subdomains """ blockstackd_url = get_blockstackd_url() subdomain_ops = None try: subdomain_ops = blockstackd_client.get_subdomain_ops_at_txid(txid, hostport=blockstackd_url) except ValueError: return self._reply_json({'error': 'Invalid argument: not a well-formed txid'}, status_code=400) if json_is_error(subdomain_ops): log.error('Failed to get subdomain operations at {}: {}'.format(txid, subdomain_ops['error'])) return self._reply_json({'error': 'Failed to get subdomain operations'}, status_code=subdomain_ops.get('http_status', 500)) return self._reply_json(subdomain_ops)
Get all subdomain operations processed in a given transaction. Returns the list of subdomains on success (can be empty) Returns 502 on failure to get subdomains
def create_srv_record(self, name, values, ttl=60): """ Creates a SRV record attached to this hosted zone. :param str name: The fully qualified name of the record to add. :param list values: A list of value strings for the record. :keyword int ttl: The time-to-live of the record (in seconds). :rtype: tuple :returns: A tuple in the form of ``(rrset, change_info)``, where ``rrset`` is the newly created SRVResourceRecordSet instance. """ self._halt_if_already_deleted() # Grab the params/kwargs here for brevity's sake. values = locals() del values['self'] return self._add_record(SRVResourceRecordSet, **values)
Creates a SRV record attached to this hosted zone. :param str name: The fully qualified name of the record to add. :param list values: A list of value strings for the record. :keyword int ttl: The time-to-live of the record (in seconds). :rtype: tuple :returns: A tuple in the form of ``(rrset, change_info)``, where ``rrset`` is the newly created SRVResourceRecordSet instance.
def pickleFile(self, name, minPartitions=None): """ Load an RDD previously saved using L{RDD.saveAsPickleFile} method. >>> tmpFile = NamedTemporaryFile(delete=True) >>> tmpFile.close() >>> sc.parallelize(range(10)).saveAsPickleFile(tmpFile.name, 5) >>> sorted(sc.pickleFile(tmpFile.name, 3).collect()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.objectFile(name, minPartitions), self)
Load an RDD previously saved using L{RDD.saveAsPickleFile} method. >>> tmpFile = NamedTemporaryFile(delete=True) >>> tmpFile.close() >>> sc.parallelize(range(10)).saveAsPickleFile(tmpFile.name, 5) >>> sorted(sc.pickleFile(tmpFile.name, 3).collect()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def sort_key_for_numeric_suffixes(path, sep='.', suffix_index=-2): """ Sort files taking into account potentially absent suffixes like somefile.dcd somefile.1000.dcd somefile.2000.dcd To be used with sorted(..., key=callable). """ chunks = path.split(sep) # Remove suffix from path and convert to int if chunks[suffix_index].isdigit(): return sep.join(chunks[:suffix_index] + chunks[suffix_index+1:]), int(chunks[suffix_index]) return path, 0
Sort files taking into account potentially absent suffixes like somefile.dcd somefile.1000.dcd somefile.2000.dcd To be used with sorted(..., key=callable).
def get_sum(path, form='sha256'): ''' Return the checksum for the given file. The following checksum algorithms are supported: * md5 * sha1 * sha224 * sha256 **(default)** * sha384 * sha512 path path to the file or directory form desired sum format CLI Example: .. code-block:: bash salt '*' file.get_sum /etc/passwd sha512 ''' path = os.path.expanduser(path) if not os.path.isfile(path): return 'File not found' return salt.utils.hashutils.get_hash(path, form, 4096)
Return the checksum for the given file. The following checksum algorithms are supported: * md5 * sha1 * sha224 * sha256 **(default)** * sha384 * sha512 path path to the file or directory form desired sum format CLI Example: .. code-block:: bash salt '*' file.get_sum /etc/passwd sha512
def _check_valid(key, val, valid): """Helper to check valid options""" if val not in valid: raise ValueError('%s must be one of %s, not "%s"' % (key, valid, val))
Helper to check valid options
def read_group_info(self): """Get information about groups directly from the widget.""" self.groups = [] for i in range(self.tabs.count()): one_group = self.tabs.widget(i).get_info() # one_group['name'] = self.tabs.tabText(i) self.groups.append(one_group)
Get information about groups directly from the widget.
async def validate(self, request: web.Request): """ Returns parameters extract from request and multidict errors :param request: Request :return: tuple of parameters and errors """ parameters = {} files = {} errors = self.errors_factory() body = None if request.method in request.POST_METHODS: try: body = await self._content_receiver.receive(request) except ValueError as e: errors[request.content_type].add(str(e)) except TypeError: errors[request.content_type].add('Not supported content type') for name, param in self._parameters.items(): where = param['in'] schema = param.get('schema', param) vtype = schema['type'] is_array = vtype == 'array' if where == 'query': source = request.query elif where == 'header': source = request.headers elif where == 'path': source = request.match_info elif body is None: source = () elif where == 'formData': source = body elif where == 'body': if isinstance(body, BaseException): errors[name].add(str(body)) else: parameters[name] = body continue else: raise ValueError(where) if is_array and hasattr(source, 'getall'): collection_format = param.get('collectionFormat') default = param.get('default', []) value = get_collection(source, name, collection_format, default) if param.get('minItems') and not value \ and name not in self._required: continue elif isinstance(source, Mapping) and name in source and ( vtype not in ('number', 'integer') or source[name] != '' ): value = source[name] elif 'default' in param: parameters[name] = param['default'] continue elif name in self._required: errors[name].add('Required') if isinstance(source, BaseException): errors[name].add(str(body)) continue else: continue if is_array: vtype = schema['items']['type'] vformat = schema['items'].get('format') else: vformat = schema.get('format') if source is body and isinstance(body, dict): pass elif vtype not in ('string', 'file'): value = convert(name, value, vtype, vformat, errors) if vtype == 'file': files[name] = value else: parameters[name] = value parameters = self._validate(parameters, errors) parameters.update(files) return parameters, errors
Returns parameters extract from request and multidict errors :param request: Request :return: tuple of parameters and errors
def write_training_data(self, features, targets): """ Writes data dictionary to filename """ assert len(features) == len(targets) data = dict(zip(features, targets)) with open(os.path.join(self.repopath, 'training.pkl'), 'w') as fp: pickle.dump(data, fp)
Writes data dictionary to filename