positive
stringlengths
100
30.3k
anchor
stringlengths
1
15k
def reconall(subjfile, subjID=None, subjdir=None, runreconall=True): """ Carries out Freesurfer's reconall on T1 nifti file WARNING: Reconall takes very long to run!! http://nipy.sourceforge.net/nipype/users/examples/smri_freesurfer.html Parameters ---------- subjfile: nifti file Path to subject's T1 nifti file subjID: string optional name for subject's output folder subjdir: string The directory to where segmentation results should be saved. Defaults to same directory as subjfile. runreconall: boolean If set to true, runs reconall, otherwise just converts assorted mgz files to nii """ T1dir = os.path.dirname(subjfile) filename = os.path.basename(subjfile) # subject ID if subjID==None: m=re.search('(\w+?)_*_', subjfile) subjID=m.group(0) + 'seg' # Tell freesurfer what subjects directory to use if subjdir==None: subjdir=T1dir fs.FSCommand.set_default_subjects_dir(subjdir) segdir=subjdir+'/'+subjID+'/' print('saving to ' + subjdir) # check if file exists if os.path.isfile(subjfile): print('running recon-all on ' + filename) else: raise ValueError("File: %s does not exist!"%filename) # check if nifti format ext=filename.split('.')[1].lower() if ext != "nii": raise ValueError("File: %s is not a nifti file!"%filename) wf = pe.Workflow(name="segment") wf.base_dir = T1dir if runreconall: # run recon-all reconall = pe.Node(interface=fs.ReconAll(), name='reconall') reconall.inputs.subject_id = subjID reconall.inputs.directive = 'all' reconall.inputs.subjects_dir = subjdir reconall.inputs.T1_files = subjfile wf.add_nodes([reconall]) result = wf.run() # convert mgz to nii wf2 = pe.Workflow(name="convertmgz") wf2.base_dir = T1dir convertmgz = pe.Node(interface=fs.MRIConvert(), name='convertmgz') convertmgz.inputs.in_file = segdir+'mri/aseg.auto.mgz' convertmgz.inputs.out_orientation='LPS' convertmgz.inputs.resample_type= 'nearest' convertmgz.inputs.reslice_like= subjfile convertmgz.inputs.out_file=segdir+subjID+'_aseg.nii.gz' wf2.add_nodes([convertmgz]) result2 = wf2.run() if runreconall: return (result, result2) else: return (result2)
Carries out Freesurfer's reconall on T1 nifti file WARNING: Reconall takes very long to run!! http://nipy.sourceforge.net/nipype/users/examples/smri_freesurfer.html Parameters ---------- subjfile: nifti file Path to subject's T1 nifti file subjID: string optional name for subject's output folder subjdir: string The directory to where segmentation results should be saved. Defaults to same directory as subjfile. runreconall: boolean If set to true, runs reconall, otherwise just converts assorted mgz files to nii
def solve(succ, orien, i, direc): """Can a laser leaving mirror i in direction direc reach exit ? :param i: mirror index :param direc: direction leaving mirror i :param orient: orient[i]=orientation of mirror i :param succ: succ[i][direc]=succ mirror reached when leaving i in direction direc """ assert orien[i] is not None j = succ[i][direc] if j is None: # basic case return False if j == len(orien) - 1: return True if orien[j] is None: # try both orientations for x in [0, 1]: orien[j] = x if solve(succ, orien, j, reflex[direc][x]): return True orien[j] = None return False else: return solve(succ, orien, j, reflex[direc][orien[j]])
Can a laser leaving mirror i in direction direc reach exit ? :param i: mirror index :param direc: direction leaving mirror i :param orient: orient[i]=orientation of mirror i :param succ: succ[i][direc]=succ mirror reached when leaving i in direction direc
def notifyBlock(self, queue, blocked): ''' Internal notify for sub-queues been blocked ''' if blocked: if self.prioritySet[-1] == queue.priority: self.prioritySet.pop() else: pindex = bisect_left(self.prioritySet, queue.priority) if pindex < len(self.prioritySet) and self.prioritySet[pindex] == queue.priority: del self.prioritySet[pindex] else: if queue.canPop(): pindex = bisect_left(self.prioritySet, queue.priority) if pindex >= len(self.prioritySet) or self.prioritySet[pindex] != queue.priority: self.prioritySet.insert(pindex, queue.priority) newblocked = not self.canPop() if newblocked != self.blocked: self.blocked = newblocked if self.parent is not None: self.parent.notifyBlock(self, newblocked)
Internal notify for sub-queues been blocked
def is_resource_protected(self, request, **kwargs): """ Determines if a resource should be protected. Returns true if and only if the resource's access_state matches an entry in the return value of get_protected_states(). """ access_state = self._get_resource_access_state(request) protected_states = self.get_protected_states() return access_state in protected_states
Determines if a resource should be protected. Returns true if and only if the resource's access_state matches an entry in the return value of get_protected_states().
def status_code(self): """ Get the http status code for the response """ try: return self._status_code except AttributeError: self._status_code = self._response.getcode() return self._status_code
Get the http status code for the response
def _ParseInformationalOptions(self, options): """Parses the informational options. Args: options (argparse.Namespace): command line arguments. Raises: BadConfigOption: if the options are invalid. """ super(PsortTool, self)._ParseInformationalOptions(options) self._quiet_mode = getattr(options, 'quiet', False) helpers_manager.ArgumentHelperManager.ParseOptions( options, self, names=['status_view'])
Parses the informational options. Args: options (argparse.Namespace): command line arguments. Raises: BadConfigOption: if the options are invalid.
def numeric_columns(self, include_bool=True): """Returns the numeric columns of the Manager. Returns: List of index names. """ columns = [] for col, dtype in zip(self.columns, self.dtypes): if is_numeric_dtype(dtype) and ( include_bool or (not include_bool and dtype != np.bool_) ): columns.append(col) return columns
Returns the numeric columns of the Manager. Returns: List of index names.
def neg_int(i): """ Simple negative integer validation. """ try: if isinstance(i, string_types): i = int(i) if not isinstance(i, int) or i > 0: raise Exception() except: raise ValueError("Not a negative integer") return i
Simple negative integer validation.
def add_namespace(self, namespace, interface=None, check_extras=True, load_now=False): """ register given namespace in global database of plugins in case it's already registered, return the registration """ tempo = self._namespaces.get(namespace) if tempo is None: tempo = _Plugins(namespace, interface, check_extras) self._namespaces[namespace] = tempo if load_now: tempo.load() return tempo
register given namespace in global database of plugins in case it's already registered, return the registration
def load_spectrum_fits_messed_x(filename, sp_ref=None): """Loads FITS file spectrum that does not have the proper headers. Returns a Spectrum""" import f311.filetypes as ft # First tries to load as usual f = load_with_classes(filename, (ft.FileSpectrumFits,)) if f is not None: ret = f.spectrum else: hdul = fits.open(filename) hdu = hdul[0] if not hdu.header.get("CDELT1"): hdu.header["CDELT1"] = 1 if sp_ref is None else sp_ref.delta_lambda if not hdu.header.get("CRVAL1"): hdu.header["CRVAL1"] = 0 if sp_ref is None else sp_ref.x[0] ret = ft.Spectrum() ret.from_hdu(hdu) ret.filename = filename original_shape = ret.y.shape # Shape of data before squeeze # Squeezes to make data of shape e.g. (1, 1, 122) into (122,) ret.y = ret.y.squeeze() if len(ret.y.shape) > 1: raise RuntimeError( "Data contains more than 1 dimension (shape is {0!s}), " "FITS file is not single spectrum".format(original_shape)) return ret
Loads FITS file spectrum that does not have the proper headers. Returns a Spectrum
def Pcn(x, dsz, Nv, dimN=2, dimC=1, crp=False, zm=False): """Constraint set projection for convolutional dictionary update problem. Parameters ---------- x : array_like Input array dsz : tuple Filter support size(s), specified using the same format as the `dsz` parameter of :func:`bcrop` Nv : tuple Sizes of problem spatial indices dimN : int, optional (default 2) Number of problem spatial indices dimC : int, optional (default 1) Number of problem channel indices crp : bool, optional (default False) Flag indicating whether the result should be cropped to the support of the largest filter in the dictionary. zm : bool, optional (default False) Flag indicating whether the projection function should include filter mean subtraction Returns ------- y : ndarray Projection of input onto constraint set """ if crp: def zpadfn(x): return x else: def zpadfn(x): return zpad(x, Nv) if zm: def zmeanfn(x): return zeromean(x, dsz, dimN) else: def zmeanfn(x): return x return normalise(zmeanfn(zpadfn(bcrop(x, dsz, dimN))), dimN + dimC)
Constraint set projection for convolutional dictionary update problem. Parameters ---------- x : array_like Input array dsz : tuple Filter support size(s), specified using the same format as the `dsz` parameter of :func:`bcrop` Nv : tuple Sizes of problem spatial indices dimN : int, optional (default 2) Number of problem spatial indices dimC : int, optional (default 1) Number of problem channel indices crp : bool, optional (default False) Flag indicating whether the result should be cropped to the support of the largest filter in the dictionary. zm : bool, optional (default False) Flag indicating whether the projection function should include filter mean subtraction Returns ------- y : ndarray Projection of input onto constraint set
def register_user(self, user, allow_login=None, send_email=None, _force_login_without_confirmation=False): """ Service method to register a user. Sends signal `user_registered`. Returns True if the user has been logged in, False otherwise. """ should_login_user = (not self.security.confirmable or self.security.login_without_confirmation or _force_login_without_confirmation) should_login_user = (should_login_user if allow_login is None else allow_login and should_login_user) if should_login_user: user.active = True # confirmation token depends on having user.id set, which requires # the user be committed to the database self.user_manager.save(user, commit=True) confirmation_link, token = None, None if self.security.confirmable and not _force_login_without_confirmation: token = self.security_utils_service.generate_confirmation_token(user) confirmation_link = url_for('security_controller.confirm_email', token=token, _external=True) user_registered.send(app._get_current_object(), user=user, confirm_token=token) if (send_email or ( send_email is None and app.config.SECURITY_SEND_REGISTER_EMAIL)): self.send_mail(_('flask_unchained.bundles.security:email_subject.register'), to=user.email, template='security/email/welcome.html', user=user, confirmation_link=confirmation_link) if should_login_user: return self.login_user(user, force=_force_login_without_confirmation) return False
Service method to register a user. Sends signal `user_registered`. Returns True if the user has been logged in, False otherwise.
def pause(self): """Set the execution mode to paused """ if self.state_machine_manager.active_state_machine_id is None: logger.info("'Pause' is not a valid action to initiate state machine execution.") return if self.state_machine_manager.get_active_state_machine() is not None: self.state_machine_manager.get_active_state_machine().root_state.recursively_pause_states() logger.debug("Pause execution ...") self.set_execution_mode(StateMachineExecutionStatus.PAUSED)
Set the execution mode to paused
def write_recovery(page, injList): """ Write injection recovery plots to markup.page object page """ th = ['']+injList td = [] plots = ['sky_error_time','sky_error_mchirp','sky_error_distance'] text = { 'sky_error_time':'Sky error vs time',\ 'sky_error_mchirp':'Sky error vs mchirp',\ 'sky_error_distance':'Sky error vs distance' } for row in plots: pTag = text[row] d = [pTag] for inj in injList: plot = markup.page() plot = markup.page() p = "%s/efficiency_OFFTRIAL_1/found_%s.png" % (inj, row) plot.a(href=p, title=pTag) plot.img(src=p) plot.a.close() d.append(plot()) td.append(d) page = write_table(page, th, td) return page
Write injection recovery plots to markup.page object page
def find_results_gen(search_term, field='title'): ''' Return a generator of the results returned by a search of the protein data bank. This generator is used internally. Parameters ---------- search_term : str The search keyword field : str The type of information to record about each entry Examples -------- >>> result_gen = find_results_gen('bleb') >>> pprint.pprint([item for item in result_gen][:5]) ['MYOSIN II DICTYOSTELIUM DISCOIDEUM MOTOR DOMAIN S456Y BOUND WITH MGADP-BEFX', 'MYOSIN II DICTYOSTELIUM DISCOIDEUM MOTOR DOMAIN S456Y BOUND WITH MGADP-ALF4', 'DICTYOSTELIUM DISCOIDEUM MYOSIN II MOTOR DOMAIN S456E WITH BOUND MGADP-BEFX', 'MYOSIN II DICTYOSTELIUM DISCOIDEUM MOTOR DOMAIN S456E BOUND WITH MGADP-ALF4', 'The structural basis of blebbistatin inhibition and specificity for myosin ' 'II'] ''' scan_params = make_query(search_term, querytype='AdvancedKeywordQuery') search_result_ids = do_search(scan_params) all_titles = [] for pdb_result in search_result_ids: result= describe_pdb(pdb_result) if field in result.keys(): yield result[field]
Return a generator of the results returned by a search of the protein data bank. This generator is used internally. Parameters ---------- search_term : str The search keyword field : str The type of information to record about each entry Examples -------- >>> result_gen = find_results_gen('bleb') >>> pprint.pprint([item for item in result_gen][:5]) ['MYOSIN II DICTYOSTELIUM DISCOIDEUM MOTOR DOMAIN S456Y BOUND WITH MGADP-BEFX', 'MYOSIN II DICTYOSTELIUM DISCOIDEUM MOTOR DOMAIN S456Y BOUND WITH MGADP-ALF4', 'DICTYOSTELIUM DISCOIDEUM MYOSIN II MOTOR DOMAIN S456E WITH BOUND MGADP-BEFX', 'MYOSIN II DICTYOSTELIUM DISCOIDEUM MOTOR DOMAIN S456E BOUND WITH MGADP-ALF4', 'The structural basis of blebbistatin inhibition and specificity for myosin ' 'II']
def _get_create_kwargs(skip_translate=None, ignore_collisions=False, validate_ip_addrs=True, client_args=None, **kwargs): ''' Take input kwargs and return a kwargs dict to pass to docker-py's create_container() function. ''' networks = kwargs.pop('networks', {}) if kwargs.get('network_mode', '') in networks: networks = {kwargs['network_mode']: networks[kwargs['network_mode']]} else: networks = {} kwargs = __utils__['docker.translate_input']( salt.utils.docker.translate.container, skip_translate=skip_translate, ignore_collisions=ignore_collisions, validate_ip_addrs=validate_ip_addrs, **__utils__['args.clean_kwargs'](**kwargs)) if networks: kwargs['networking_config'] = _create_networking_config(networks) if client_args is None: try: client_args = get_client_args(['create_container', 'host_config']) except CommandExecutionError as exc: log.error('docker.create: Error getting client args: \'%s\'', exc.__str__(), exc_info=True) raise CommandExecutionError( 'Failed to get client args: {0}'.format(exc)) full_host_config = {} host_kwargs = {} create_kwargs = {} # Using list() becausee we'll be altering kwargs during iteration for arg in list(kwargs): if arg in client_args['host_config']: host_kwargs[arg] = kwargs.pop(arg) continue if arg in client_args['create_container']: if arg == 'host_config': full_host_config.update(kwargs.pop(arg)) else: create_kwargs[arg] = kwargs.pop(arg) continue create_kwargs['host_config'] = \ _client_wrapper('create_host_config', **host_kwargs) # In the event that a full host_config was passed, overlay it on top of the # one we just created. create_kwargs['host_config'].update(full_host_config) # The "kwargs" dict at this point will only contain unused args return create_kwargs, kwargs
Take input kwargs and return a kwargs dict to pass to docker-py's create_container() function.
def newchild(self,chld=False): """Like givebirth(), but also appends the new child to the list of children.""" if not chld: chld = self.givebirth() lchld=[chld] if type(chld)!=list else chld for chldx in lchld: chldx.parent=self self.children.append(chld) return chld
Like givebirth(), but also appends the new child to the list of children.
def u64_to_hex16le(val): """! @brief Create 16-digit hexadecimal string from 64-bit register value""" return ''.join("%02x" % (x & 0xFF) for x in ( val, val >> 8, val >> 16, val >> 24, val >> 32, val >> 40, val >> 48, val >> 56, ))
! @brief Create 16-digit hexadecimal string from 64-bit register value
def prepare_shell_data(self, shells, key, entry): """Prepare one shell or docker task.""" if self.can_process_shell(entry): if key in ['python']: entry['type'] = key if 'with' in entry and isinstance(entry['with'], str): rendered_with = ast.literal_eval(render(entry['with'], variables=self.pipeline.variables, model=self.pipeline.model, env=self.get_merged_env(include_os=True))) elif 'with' in entry: rendered_with = entry['with'] else: rendered_with = [''] for item in rendered_with: shells.append({ 'id': self.next_task_id, 'creator': key, 'entry': entry, 'model': self.pipeline.model, 'env': self.get_merged_env(), 'item': item, 'dry_run': self.pipeline.options.dry_run, 'debug': self.pipeline.options.debug, 'strict': self.pipeline.options.strict, 'variables': self.pipeline.variables, 'temporary_scripts_path': self.pipeline.options.temporary_scripts_path}) self.next_task_id += 1
Prepare one shell or docker task.
def lowest_common_hypernyms(self,target_synset): """Returns the common hypernyms of the synset and the target synset, which are furthest from the closest roots. Parameters ---------- target_synset : Synset Synset with which the common hypernyms are sought. Returns ------- list of Synsets Common synsets which are the furthest from the closest roots. """ self_hypernyms = self._recursive_hypernyms(set()) other_hypernyms = target_synset._recursive_hypernyms(set()) common_hypernyms = self_hypernyms.intersection(other_hypernyms) annot_common_hypernyms = [(hypernym, hypernym._min_depth()) for hypernym in common_hypernyms] annot_common_hypernyms.sort(key = lambda annot_hypernym: annot_hypernym[1],reverse=True) max_depth = annot_common_hypernyms[0][1] if len(annot_common_hypernyms) > 0 else None if max_depth != None: return [annot_common_hypernym[0] for annot_common_hypernym in annot_common_hypernyms if annot_common_hypernym[1] == max_depth] else: return None
Returns the common hypernyms of the synset and the target synset, which are furthest from the closest roots. Parameters ---------- target_synset : Synset Synset with which the common hypernyms are sought. Returns ------- list of Synsets Common synsets which are the furthest from the closest roots.
def get_first_result(threads): """ this blocks, waiting for the first result that returns from a thread :type threads: list[Thread] """ while True: for thread in threads: if not thread.is_alive(): return thread.queue.get()
this blocks, waiting for the first result that returns from a thread :type threads: list[Thread]
def _get_internal_slot(slot_key=None, filler_pipeline_key=None, slot_dict=None): """Gets information about a _SlotRecord for display in UI. Args: slot_key: The db.Key of the slot to fetch. filler_pipeline_key: In the case the slot has not yet been filled, assume that the given db.Key (for a _PipelineRecord) will be the filler of the slot in the future. slot_dict: The slot JSON dictionary. Returns: Dictionary with the keys: status: Slot status: 'filled' or 'waiting' fillTimeMs: Time in milliseconds since the epoch of when it was filled. value: The current value of the slot, which is a slot's JSON dictionary. fillerPipelineId: The pipeline ID of what stage has or should fill this slot. Raises: PipelineStatusError if any input is bad. """ if slot_dict is None: slot_dict = {} slot_record = slot_dict.get(slot_key) if slot_record is None: raise PipelineStatusError( 'Could not find data for output slot key "%s".' % slot_key) output = {} if slot_record.status == _SlotRecord.FILLED: output['status'] = 'filled' output['fillTimeMs'] = _get_timestamp_ms(slot_record.fill_time) output['value'] = slot_record.value filler_pipeline_key = ( _SlotRecord.filler.get_value_for_datastore(slot_record)) else: output['status'] = 'waiting' if filler_pipeline_key: output['fillerPipelineId'] = filler_pipeline_key.name() return output
Gets information about a _SlotRecord for display in UI. Args: slot_key: The db.Key of the slot to fetch. filler_pipeline_key: In the case the slot has not yet been filled, assume that the given db.Key (for a _PipelineRecord) will be the filler of the slot in the future. slot_dict: The slot JSON dictionary. Returns: Dictionary with the keys: status: Slot status: 'filled' or 'waiting' fillTimeMs: Time in milliseconds since the epoch of when it was filled. value: The current value of the slot, which is a slot's JSON dictionary. fillerPipelineId: The pipeline ID of what stage has or should fill this slot. Raises: PipelineStatusError if any input is bad.
def verify(self, connection_type=None): """ Verifies and update the remote system settings. :param connection_type: same as the one in `create` method. """ req_body = self._cli.make_body(connectionType=connection_type) resp = self.action('verify', **req_body) resp.raise_if_err() return resp
Verifies and update the remote system settings. :param connection_type: same as the one in `create` method.
def make_qemu_dirs(max_qemu_id, output_dir, topology_name): """ Create Qemu VM working directories if required :param int max_qemu_id: Number of directories to create :param str output_dir: Output directory :param str topology_name: Topology name """ if max_qemu_id is not None: for i in range(1, max_qemu_id + 1): qemu_dir = os.path.join(output_dir, topology_name + '-files', 'qemu', 'vm-%s' % i) os.makedirs(qemu_dir)
Create Qemu VM working directories if required :param int max_qemu_id: Number of directories to create :param str output_dir: Output directory :param str topology_name: Topology name
def update_mutation_inputs(service): """ Args: service : The service being updated by the mutation Returns: (list) : a list of all of the fields availible for the service. Pk is a required field in order to filter the results """ # grab the default list of field summaries inputs = _service_mutation_summaries(service) # visit each field for field in inputs: # if we're looking at the id field if field['name'] == 'id': # make sure its required field['required'] = True # but no other field else: # is required field['required'] = False # return the final list return inputs
Args: service : The service being updated by the mutation Returns: (list) : a list of all of the fields availible for the service. Pk is a required field in order to filter the results
def to_dictionary(self): """Serialize an object into dictionary form. Useful if you have to serialize an array of objects into JSON. Otherwise, if you call the :meth:`to_json` method on each object in the list and then try to dump the array, you end up with an array with one string.""" d = {'start': self.start.isoformat(), 'end': self.end.isoformat(), 'tz': self.tz, 'summary': self.summary.to_dictionary(), 'series': self.series.to_dictionary() } return d
Serialize an object into dictionary form. Useful if you have to serialize an array of objects into JSON. Otherwise, if you call the :meth:`to_json` method on each object in the list and then try to dump the array, you end up with an array with one string.
def clear_stale_pids(pids, pid_dir='/tmp', prefix='', multi=False): 'check for and remove any pids which have no corresponding process' if isinstance(pids, (int, float, long)): pids = [pids] pids = str2list(pids, map_=unicode) procs = map(unicode, os.listdir('/proc')) running = [pid for pid in pids if pid in procs] logger.warn( "Found %s pids running: %s" % (len(running), running)) prefix = prefix.rstrip('.') if prefix else None for pid in pids: if prefix: _prefix = prefix else: _prefix = unicode(pid) # remove non-running procs if pid in running: continue if multi: pid_file = '%s%s.pid' % (_prefix, pid) else: pid_file = '%s.pid' % (_prefix) path = os.path.join(pid_dir, pid_file) if os.path.exists(path): logger.debug("Removing pidfile: %s" % path) try: remove_file(path) except OSError as e: logger.warn(e) return running
check for and remove any pids which have no corresponding process
def remove_security_group(self, name): """ Remove a security group from container """ for group in self.security_groups: if group.isc_name == name: group.delete()
Remove a security group from container
def config_md5(self, source_config): """Compute MD5 hash of file.""" file_contents = source_config + "\n" # Cisco IOS automatically adds this file_contents = file_contents.encode("UTF-8") return hashlib.md5(file_contents).hexdigest()
Compute MD5 hash of file.
def _build_query_dict(self, formdata=None): """ Take submitted data from form and create a query dict to be used in a Q object (or filter) """ if self.is_valid() and formdata is None: formdata = self.cleaned_data key = "{field}__{operator}".format(**formdata) if formdata['operator'] == "isnull": return {key: None} elif formdata['operator'] == "istrue": return {formdata['field']: True} elif formdata['operator'] == "isfalse": return {formdata['field']: False} return {key: formdata['value']}
Take submitted data from form and create a query dict to be used in a Q object (or filter)
def deaccent(text): """ Remove accentuation from the given string. Input text is either a unicode string or utf8 encoded bytestring. Return input string with accents removed, as unicode. >>> deaccent("Šéf chomutovských komunistů dostal poštou bílý prášek") u'Sef chomutovskych komunistu dostal postou bily prasek' """ if not isinstance(text, unicode): # assume utf8 for byte strings, use default (strict) error handling text = text.decode('utf8') norm = unicodedata.normalize("NFD", text) result = u('').join(ch for ch in norm if unicodedata.category(ch) != 'Mn') return unicodedata.normalize("NFC", result)
Remove accentuation from the given string. Input text is either a unicode string or utf8 encoded bytestring. Return input string with accents removed, as unicode. >>> deaccent("Šéf chomutovských komunistů dostal poštou bílý prášek") u'Sef chomutovskych komunistu dostal postou bily prasek'
def get_success_url(self): """ Returns the success URL to redirect the user to. """ return reverse( 'forum_conversation:topic', kwargs={ 'forum_slug': self.object.forum.slug, 'forum_pk': self.object.forum.pk, 'slug': self.object.slug, 'pk': self.object.pk, }, )
Returns the success URL to redirect the user to.
def getBitmapFromRect(self, x, y, w, h): """ Capture the specified area of the (virtual) screen. """ min_x, min_y, screen_width, screen_height = self._getVirtualScreenRect() img = self._getVirtualScreenBitmap() # TODO # Limit the coordinates to the virtual screen # Then offset so 0,0 is the top left corner of the image # (Top left of virtual screen could be negative) x1 = min(max(min_x, x), min_x+screen_width) - min_x y1 = min(max(min_y, y), min_y+screen_height) - min_y x2 = min(max(min_x, x+w), min_x+screen_width) - min_x y2 = min(max(min_y, y+h), min_y+screen_height) - min_y return numpy.array(img.crop((x1, y1, x2, y2)))
Capture the specified area of the (virtual) screen.
def process_args(): """ Parse command-line arguments. """ parser = argparse.ArgumentParser(description="A file for converting NeuroML v2 files into POVRay files for 3D rendering") parser.add_argument('neuroml_file', type=str, metavar='<NeuroML file>', help='NeuroML (version 2 beta 3+) file to be converted to PovRay format (XML or HDF5 format)') parser.add_argument('-split', action='store_true', default=False, help="If this is specified, generate separate pov files for cells & network. Default is false") parser.add_argument('-background', type=str, metavar='<background colour>', default=_WHITE, help='Colour of background, e.g. <0,0,0,0.55>') parser.add_argument('-movie', action='store_true', default=False, help="If this is specified, generate a ini file for generating a sequence of frames for a movie of the 3D structure") parser.add_argument('-inputs', action='store_true', default=False, help="If this is specified, show the locations of (synaptic, current clamp, etc.) inputs into the cells of the network") parser.add_argument('-conns', action='store_true', default=False, help="If this is specified, show the connections present in the network with lines") parser.add_argument('-conn_points', action='store_true', default=False, help="If this is specified, show the end points of the connections present in the network") parser.add_argument('-v', action='store_true', default=False, help="Verbose output") parser.add_argument('-frames', type=int, metavar='<frames>', default=36, help='Number of frames in movie') parser.add_argument('-posx', type=float, metavar='<position offset x>', default=0, help='Offset position in x dir (0 is centre, 1 is top)') parser.add_argument('-posy', type=float, metavar='<position offset y>', default=0, help='Offset position in y dir (0 is centre, 1 is top)') parser.add_argument('-posz', type=float, metavar='<position offset z>', default=0, help='Offset position in z dir (0 is centre, 1 is top)') parser.add_argument('-viewx', type=float, metavar='<view offset x>', default=0, help='Offset viewing point in x dir (0 is centre, 1 is top)') parser.add_argument('-viewy', type=float, metavar='<view offset y>', default=0, help='Offset viewing point in y dir (0 is centre, 1 is top)') parser.add_argument('-viewz', type=float, metavar='<view offset z>', default=0, help='Offset viewing point in z dir (0 is centre, 1 is top)') parser.add_argument('-scalex', type=float, metavar='<scale position x>', default=1, help='Scale position from network in x dir') parser.add_argument('-scaley', type=float, metavar='<scale position y>', default=1.5, help='Scale position from network in y dir') parser.add_argument('-scalez', type=float, metavar='<scale position z>', default=1, help='Scale position from network in z dir') parser.add_argument('-mindiam', type=float, metavar='<minimum diameter dendrites/axons>', default=0, help='Minimum diameter for dendrites/axons (to improve visualisations)') parser.add_argument('-plane', action='store_true', default=False, help="If this is specified, add a 2D plane below cell/network") parser.add_argument('-segids', action='store_true', default=False, help="Show segment ids") return parser.parse_args()
Parse command-line arguments.
def state_tomography_programs(state_prep, qubits=None, rotation_generator=tomography.default_rotations): """ Yield tomographic sequences that prepare a state with Quil program `state_prep` and then append tomographic rotations on the specified `qubits`. If `qubits is None`, it assumes all qubits in the program should be tomographically rotated. :param Program state_prep: The program to prepare the state to be tomographed. :param list|NoneType qubits: A list of Qubits or Numbers, to perform the tomography on. If `None`, performs it on all in state_prep. :param generator rotation_generator: A generator that yields tomography rotations to perform. :return: Program for state tomography. :rtype: Program """ if qubits is None: qubits = state_prep.get_qubits() for tomography_program in rotation_generator(*qubits): state_tomography_program = Program(Pragma("PRESERVE_BLOCK")) state_tomography_program.inst(state_prep) state_tomography_program.inst(tomography_program) state_tomography_program.inst(Pragma("END_PRESERVE_BLOCK")) yield state_tomography_program
Yield tomographic sequences that prepare a state with Quil program `state_prep` and then append tomographic rotations on the specified `qubits`. If `qubits is None`, it assumes all qubits in the program should be tomographically rotated. :param Program state_prep: The program to prepare the state to be tomographed. :param list|NoneType qubits: A list of Qubits or Numbers, to perform the tomography on. If `None`, performs it on all in state_prep. :param generator rotation_generator: A generator that yields tomography rotations to perform. :return: Program for state tomography. :rtype: Program
def log(self, file, batch): """ Log that a migration was run. :type file: str :type batch: int """ record = {"migration": file, "batch": batch} self.table().insert(**record)
Log that a migration was run. :type file: str :type batch: int
def event_hint_with_exc_info(exc_info=None): # type: (ExcInfo) -> Dict[str, Optional[ExcInfo]] """Creates a hint with the exc info filled in.""" if exc_info is None: exc_info = sys.exc_info() else: exc_info = exc_info_from_error(exc_info) if exc_info[0] is None: exc_info = None return {"exc_info": exc_info}
Creates a hint with the exc info filled in.
def format(self): """ Get format according to algorithm defined in RFC 5646 section 2.1.1. :return: formatted tag string. """ tag = self.data['tag'] subtags = tag.split('-') if len(subtags) == 1: return subtags[0] formatted_tag = subtags[0] private_tag = False for i, subtag in enumerate(subtags[1:]): if len(subtags[i]) == 1 or private_tag: formatted_tag += '-' + subtag private_tag = True elif len(subtag) == 2: formatted_tag += '-' + subtag.upper() elif len(subtag) == 4: formatted_tag += '-' + subtag.capitalize() else: formatted_tag += '-' + subtag return formatted_tag
Get format according to algorithm defined in RFC 5646 section 2.1.1. :return: formatted tag string.
def _validate(self): """ Enforce some structure to the config file """ # This could be done with a default config # Check that specific keys exist sections = odict([ ('catalog',['dirname','basename', 'lon_field','lat_field','objid_field', 'mag_1_band', 'mag_1_field', 'mag_err_1_field', 'mag_2_band', 'mag_2_field', 'mag_err_2_field', ]), ('mask',[]), ('coords',['nside_catalog','nside_mask','nside_likelihood', 'nside_pixel','roi_radius','roi_radius_annulus', 'roi_radius_interior','coordsys', ]), ('likelihood',[]), ('output',[]), ('batch',[]), ]) keys = np.array(list(sections.keys())) found = np.in1d(keys,list(self.keys())) if not np.all(found): msg = 'Missing sections: '+str(keys[~found]) raise Exception(msg) for section,keys in sections.items(): keys = np.array(keys) found = np.in1d(keys,list(self[section].keys())) if not np.all(found): msg = 'Missing keys in %s: '%(section)+str(keys[~found]) raise Exception(msg)
Enforce some structure to the config file
def _process_all_any(self, func, **kwargs): """Calculates if any or all the values are true. Return: A new QueryCompiler object containing boolean values or boolean. """ axis = kwargs.get("axis", 0) axis = 0 if axis is None else axis kwargs["axis"] = axis builder_func = self._build_mapreduce_func(func, **kwargs) return self._full_reduce(axis, builder_func)
Calculates if any or all the values are true. Return: A new QueryCompiler object containing boolean values or boolean.
def rm_known_host(user=None, hostname=None, config=None, port=None): ''' Remove all keys belonging to hostname from a known_hosts file. CLI Example: .. code-block:: bash salt '*' ssh.rm_known_host <user> <hostname> ''' if not hostname: return {'status': 'error', 'error': 'hostname argument required'} full = _get_known_hosts_file(config=config, user=user) if isinstance(full, dict): return full if not os.path.isfile(full): return {'status': 'error', 'error': 'Known hosts file {0} does not exist'.format(full)} ssh_hostname = _hostname_and_port_to_ssh_hostname(hostname, port) cmd = ['ssh-keygen', '-R', ssh_hostname, '-f', full] cmd_result = __salt__['cmd.run'](cmd, python_shell=False) if not salt.utils.platform.is_windows(): # ssh-keygen creates a new file, thus a chown is required. if os.geteuid() == 0 and user: uinfo = __salt__['user.info'](user) os.chown(full, uinfo['uid'], uinfo['gid']) return {'status': 'removed', 'comment': cmd_result}
Remove all keys belonging to hostname from a known_hosts file. CLI Example: .. code-block:: bash salt '*' ssh.rm_known_host <user> <hostname>
def _repr_html_(self, **kwargs): """Produce HTML for Jupyter Notebook""" from jinja2 import Template from markdown import markdown as convert_markdown extensions = [ 'markdown.extensions.extra', 'markdown.extensions.admonition' ] return convert_markdown(self.markdown, extensions)
Produce HTML for Jupyter Notebook
def close(self, child): """ Close a child position - alias for rebalance(0, child). This will also flatten (close out all) the child's children. Args: * child (str): Child, specified by name. """ c = self.children[child] # flatten if children not None if c.children is not None and len(c.children) != 0: c.flatten() if c.value != 0. and not np.isnan(c.value): c.allocate(-c.value)
Close a child position - alias for rebalance(0, child). This will also flatten (close out all) the child's children. Args: * child (str): Child, specified by name.
def _edit_main(self, request): """Adds the link to the new unit testing results on the repo's main wiki page. """ self.prefix = "{}_Pull_Request_{}".format(request.repo.name, request.pull.number) if not self.testmode: page = site.pages[self.basepage] text = page.text() else: text = "This is a fake wiki page.\n\n<!--@CI:Placeholder-->" self.newpage = self.prefix link = "Pull Request #{}".format(request.pull.number) text = text.replace("<!--@CI:Placeholder-->", "* [[{}|{}]]\n<!--@CI:Placeholder-->".format(self.newpage, link)) if not self.testmode: result = page.save(text, summary="Added {} unit test link.".format(link), minor=True, bot=True) return result[u'result'] == u'Success' else: return text
Adds the link to the new unit testing results on the repo's main wiki page.
def unfederate(self, serverId): """ This operation unfederates an ArcGIS Server from Portal for ArcGIS """ url = self._url + "/servers/{serverid}/unfederate".format( serverid=serverId) params = {"f" : "json"} return self._get(url=url, param_dict=params, proxy_port=self._proxy_port, proxy_url=self._proxy_ur)
This operation unfederates an ArcGIS Server from Portal for ArcGIS
def sentence_starts(self): """The list of start positions representing ``sentences`` layer elements.""" if not self.is_tagged(SENTENCES): self.tokenize_sentences() return self.starts(SENTENCES)
The list of start positions representing ``sentences`` layer elements.
def stop_all(self, run_order=-1): """Runs stop method on all modules less than the passed-in run_order. Used when target is exporting itself mid-build, so we clean up state before committing run files etc. """ shutit_global.shutit_global_object.yield_to_draw() # sort them so they're stopped in reverse order for module_id in self.module_ids(rev=True): shutit_module_obj = self.shutit_map[module_id] if run_order == -1 or shutit_module_obj.run_order <= run_order: if self.is_installed(shutit_module_obj): if not shutit_module_obj.stop(self): self.fail('failed to stop: ' + module_id, shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('target_child').shutit_pexpect_child)
Runs stop method on all modules less than the passed-in run_order. Used when target is exporting itself mid-build, so we clean up state before committing run files etc.
def append_faces(vertices_seq, faces_seq): """ Given a sequence of zero- indexed faces and vertices combine them into a single array of faces and a single array of vertices. Parameters ----------- vertices_seq : (n, ) sequence of (m, d) float Multiple arrays of verticesvertex arrays faces_seq : (n, ) sequence of (p, j) int Zero indexed faces for matching vertices Returns ---------- vertices : (i, d) float Points in space faces : (j, 3) int Reference vertex indices """ # the length of each vertex array vertices_len = np.array([len(i) for i in vertices_seq]) # how much each group of faces needs to be offset face_offset = np.append(0, np.cumsum(vertices_len)[:-1]) new_faces = [] for offset, faces in zip(face_offset, faces_seq): if len(faces) == 0: continue # apply the index offset new_faces.append(faces + offset) # stack to clean (n, 3) float vertices = vstack_empty(vertices_seq) # stack to clean (n, 3) int faces = vstack_empty(new_faces) return vertices, faces
Given a sequence of zero- indexed faces and vertices combine them into a single array of faces and a single array of vertices. Parameters ----------- vertices_seq : (n, ) sequence of (m, d) float Multiple arrays of verticesvertex arrays faces_seq : (n, ) sequence of (p, j) int Zero indexed faces for matching vertices Returns ---------- vertices : (i, d) float Points in space faces : (j, 3) int Reference vertex indices
def transpose(vari): """ Transpose a shapeable quantety. Args: vari (chaospy.poly.base.Poly, numpy.ndarray): Quantety of interest. Returns: (chaospy.poly.base.Poly, numpy.ndarray): Same type as ``vari``. Examples: >>> P = chaospy.reshape(chaospy.prange(4), (2,2)) >>> print(P) [[1, q0], [q0^2, q0^3]] >>> print(chaospy.transpose(P)) [[1, q0^2], [q0, q0^3]] """ if isinstance(vari, Poly): core = vari.A.copy() for key in vari.keys: core[key] = transpose(core[key]) return Poly(core, vari.dim, vari.shape[::-1], vari.dtype) return numpy.transpose(vari)
Transpose a shapeable quantety. Args: vari (chaospy.poly.base.Poly, numpy.ndarray): Quantety of interest. Returns: (chaospy.poly.base.Poly, numpy.ndarray): Same type as ``vari``. Examples: >>> P = chaospy.reshape(chaospy.prange(4), (2,2)) >>> print(P) [[1, q0], [q0^2, q0^3]] >>> print(chaospy.transpose(P)) [[1, q0^2], [q0, q0^3]]
def unregister(self, name, func): """ Remove a previously registered callback :param str name: Hook name :param callable func: A function reference\ that was registered previously """ try: templatehook = self._registry[name] except KeyError: return templatehook.unregister(func)
Remove a previously registered callback :param str name: Hook name :param callable func: A function reference\ that was registered previously
def to_yellow(self, on: bool=False): """ Change the LED to yellow (on or off) :param on: True or False :return: None """ self._on = on if on: self._load_new(led_yellow_on) if self._toggle_on_click: self._canvas.bind('<Button-1>', lambda x: self.to_yellow(False)) else: self._load_new(led_yellow) if self._toggle_on_click: self._canvas.bind('<Button-1>', lambda x: self.to_yellow(True))
Change the LED to yellow (on or off) :param on: True or False :return: None
def encrypt_with_caching(kms_cmk_arn, max_age_in_cache, cache_capacity): """Encrypts a string using an AWS KMS customer master key (CMK) and data key caching. :param str kms_cmk_arn: Amazon Resource Name (ARN) of the KMS customer master key :param float max_age_in_cache: Maximum time in seconds that a cached entry can be used :param int cache_capacity: Maximum number of entries to retain in cache at once """ # Data to be encrypted my_data = "My plaintext data" # Security thresholds # Max messages (or max bytes per) data key are optional MAX_ENTRY_MESSAGES = 100 # Create an encryption context encryption_context = {"purpose": "test"} # Create a master key provider for the KMS customer master key (CMK) key_provider = aws_encryption_sdk.KMSMasterKeyProvider(key_ids=[kms_cmk_arn]) # Create a local cache cache = aws_encryption_sdk.LocalCryptoMaterialsCache(cache_capacity) # Create a caching CMM caching_cmm = aws_encryption_sdk.CachingCryptoMaterialsManager( master_key_provider=key_provider, cache=cache, max_age=max_age_in_cache, max_messages_encrypted=MAX_ENTRY_MESSAGES, ) # When the call to encrypt data specifies a caching CMM, # the encryption operation uses the data key cache specified # in the caching CMM encrypted_message, _header = aws_encryption_sdk.encrypt( source=my_data, materials_manager=caching_cmm, encryption_context=encryption_context ) return encrypted_message
Encrypts a string using an AWS KMS customer master key (CMK) and data key caching. :param str kms_cmk_arn: Amazon Resource Name (ARN) of the KMS customer master key :param float max_age_in_cache: Maximum time in seconds that a cached entry can be used :param int cache_capacity: Maximum number of entries to retain in cache at once
def parse_tibiacom_content(content, *, html_class="BoxContent", tag="div", builder="lxml"): """Parses HTML content from Tibia.com into a BeautifulSoup object. Parameters ---------- content: :class:`str` The raw HTML content from Tibia.com html_class: :class:`str` The HTML class of the parsed element. The default value is ``BoxContent``. tag: :class:`str` The HTML tag select. The default value is ``div``. builder: :class:`str` The builder to use. The default value is ``lxml``. Returns ------- :class:`bs4.BeautifulSoup`, optional The parsed content. """ return bs4.BeautifulSoup(content.replace('ISO-8859-1', 'utf-8'), builder, parse_only=bs4.SoupStrainer(tag, class_=html_class))
Parses HTML content from Tibia.com into a BeautifulSoup object. Parameters ---------- content: :class:`str` The raw HTML content from Tibia.com html_class: :class:`str` The HTML class of the parsed element. The default value is ``BoxContent``. tag: :class:`str` The HTML tag select. The default value is ``div``. builder: :class:`str` The builder to use. The default value is ``lxml``. Returns ------- :class:`bs4.BeautifulSoup`, optional The parsed content.
def get_and_order(self, ids, column=None, table=None): """Get specific entries and order them in the same way.""" command = """ SELECT rowid, * from "data" WHERE rowid in (%s) ORDER BY CASE rowid %s END; """ ordered = ','.join(map(str,ids)) rowids = '\n'.join("WHEN '%s' THEN %s" % (row,i) for i,row in enumerate(ids)) command = command % (ordered, rowids)
Get specific entries and order them in the same way.
def batch_for_variantcall(samples): """Prepare a set of samples for parallel variant calling. CWL input target that groups samples into batches and variant callers for parallel processing. If doing joint calling, with `tools_on: [gvcf]`, split the sample into individuals instead of combining into a batch. """ sample_order = [dd.get_sample_name(utils.to_single_data(x)) for x in samples] to_process, extras = _dup_samples_by_variantcaller(samples, require_bam=False) batch_groups = collections.defaultdict(list) to_process = [utils.to_single_data(x) for x in to_process] for data in cwlutils.samples_to_records(to_process): vc = get_variantcaller(data, require_bam=False) batches = dd.get_batches(data) or dd.get_sample_name(data) if not isinstance(batches, (list, tuple)): batches = [batches] for b in batches: batch_groups[(b, vc)].append(utils.deepish_copy(data)) batches = [] for cur_group in batch_groups.values(): joint_calling = any([is_joint(d) for d in cur_group]) if joint_calling: for d in cur_group: batches.append([d]) else: batches.append(cur_group) def by_original_order(xs): return (min([sample_order.index(dd.get_sample_name(x)) for x in xs]), min([dd.get_variantcaller_order(x) for x in xs])) return sorted(batches + extras, key=by_original_order)
Prepare a set of samples for parallel variant calling. CWL input target that groups samples into batches and variant callers for parallel processing. If doing joint calling, with `tools_on: [gvcf]`, split the sample into individuals instead of combining into a batch.
def max_len(iterable, minimum=0): """Return the len() of the longest item in ``iterable`` or ``minimum``. >>> max_len(['spam', 'ham']) 4 >>> max_len([]) 0 >>> max_len(['ham'], 4) 4 """ try: result = max(map(len, iterable)) except ValueError: result = minimum return minimum if result < minimum else result
Return the len() of the longest item in ``iterable`` or ``minimum``. >>> max_len(['spam', 'ham']) 4 >>> max_len([]) 0 >>> max_len(['ham'], 4) 4
def add(self, watch_key, tensor_value): """Add a tensor value. Args: watch_key: A string representing the debugger tensor watch, e.g., 'Dense_1/BiasAdd:0:DebugIdentity'. tensor_value: The value of the tensor as a numpy.ndarray. """ if watch_key not in self._tensor_data: self._tensor_data[watch_key] = _WatchStore( watch_key, mem_bytes_limit=self._watch_mem_bytes_limit) self._tensor_data[watch_key].add(tensor_value)
Add a tensor value. Args: watch_key: A string representing the debugger tensor watch, e.g., 'Dense_1/BiasAdd:0:DebugIdentity'. tensor_value: The value of the tensor as a numpy.ndarray.
def _process_css_text(self, css_text, index, rules, head): """processes the given css_text by adding rules that can be in-lined to the given rules list and adding any that cannot be in-lined to the given `<head>` element. """ these_rules, these_leftover = self._parse_style_rules(css_text, index) rules.extend(these_rules) if head is not None and (these_leftover or self.keep_style_tags): style = etree.Element("style") style.attrib["type"] = "text/css" if self.keep_style_tags: style.text = css_text else: style.text = self._css_rules_to_string(these_leftover) head.append(style)
processes the given css_text by adding rules that can be in-lined to the given rules list and adding any that cannot be in-lined to the given `<head>` element.
def manifest(self, values, *paths, filename: str = None) -> Dict: """Load a manifest file and apply template values """ filename = filename or self.filename(*paths) with open(filename, 'r') as fp: template = Template(fp.read()) return yaml.load(template.render(values))
Load a manifest file and apply template values
def transpose(self, name=None): """Returns transpose batch reshape.""" if name is None: name = self.module_name + "_transpose" return BatchReshape(shape=lambda: self.input_shape, preserve_dims=self._preserve_dims, name=name)
Returns transpose batch reshape.
def message_upperbound(self, tree, spins, subtheta): """Determine an upper bound on the energy of the elimination tree. Args: tree (dict): The current elimination tree spins (dict): The current fixed spins subtheta (dict): Theta with spins fixed. Returns: The formula for the energy of the tree. """ energy_sources = set() for v, subtree in tree.items(): assert all(u in spins for u in self._ancestors[v]) # build an iterable over all of the energies contributions # that we can exactly determine given v and our known spins # in these contributions we assume that v is positive def energy_contributions(): yield subtheta.linear[v] for u, bias in subtheta.adj[v].items(): if u in spins: yield Times(limitReal(spins[u]), bias) energy = Plus(energy_contributions()) # if there are no more variables in the order, we can stop # otherwise we need the next message variable if subtree: spins[v] = 1. plus = self.message_upperbound(subtree, spins, subtheta) spins[v] = -1. minus = self.message_upperbound(subtree, spins, subtheta) del spins[v] else: plus = minus = limitReal(0.0) # we now need a real-valued smt variable to be our message m = FreshSymbol(REAL) self.assertions.update({LE(m, Plus(energy, plus)), LE(m, Plus(Times(energy, limitReal(-1.)), minus))}) energy_sources.add(m) return Plus(energy_sources)
Determine an upper bound on the energy of the elimination tree. Args: tree (dict): The current elimination tree spins (dict): The current fixed spins subtheta (dict): Theta with spins fixed. Returns: The formula for the energy of the tree.
def do_list_cap(self, line): """list_cap <peer> """ def f(p, args): for i in p.netconf.server_capabilities: print(i) self._request(line, f)
list_cap <peer>
def delete_record_set(self, record_set): """Append a record set to the 'deletions' for the change set. :type record_set: :class:`google.cloud.dns.resource_record_set.ResourceRecordSet` :param record_set: the record set to append. :raises: ``ValueError`` if ``record_set`` is not of the required type. """ if not isinstance(record_set, ResourceRecordSet): raise ValueError("Pass a ResourceRecordSet") self._deletions += (record_set,)
Append a record set to the 'deletions' for the change set. :type record_set: :class:`google.cloud.dns.resource_record_set.ResourceRecordSet` :param record_set: the record set to append. :raises: ``ValueError`` if ``record_set`` is not of the required type.
def match(self, messy_data, threshold=0.5, n_matches=1, generator=False): # pragma: no cover """Identifies pairs of records that refer to the same entity, returns tuples containing a set of record ids and a confidence score as a float between 0 and 1. The record_ids within each set should refer to the same entity and the confidence score is the estimated probability that the records refer to the same entity. This method should only used for small to moderately sized datasets for larger data, use matchBlocks Arguments: messy_data -- Dictionary of records from messy dataset, where the keys are record_ids and the values are dictionaries with the keys being field names threshold -- Number between 0 and 1 (default is .5). We will consider records as potential duplicates if the predicted probability of being a duplicate is above the threshold. Lowering the number will increase recall, raising it will increase precision n_matches -- Maximum number of possible matches from the canonical record set to match against each record in the messy record set """ blocked_pairs = self._blockData(messy_data) clusters = self.matchBlocks(blocked_pairs, threshold, n_matches) clusters = (cluster for cluster in clusters if len(cluster)) if generator: return clusters else: return list(clusters)
Identifies pairs of records that refer to the same entity, returns tuples containing a set of record ids and a confidence score as a float between 0 and 1. The record_ids within each set should refer to the same entity and the confidence score is the estimated probability that the records refer to the same entity. This method should only used for small to moderately sized datasets for larger data, use matchBlocks Arguments: messy_data -- Dictionary of records from messy dataset, where the keys are record_ids and the values are dictionaries with the keys being field names threshold -- Number between 0 and 1 (default is .5). We will consider records as potential duplicates if the predicted probability of being a duplicate is above the threshold. Lowering the number will increase recall, raising it will increase precision n_matches -- Maximum number of possible matches from the canonical record set to match against each record in the messy record set
def capture(cls, eval_env=0, reference=0): """Capture an execution environment from the stack. If `eval_env` is already an :class:`EvalEnvironment`, it is returned unchanged. Otherwise, we walk up the stack by ``eval_env + reference`` steps and capture that function's evaluation environment. For ``eval_env=0`` and ``reference=0``, the default, this captures the stack frame of the function that calls :meth:`capture`. If ``eval_env + reference`` is 1, then we capture that function's caller, etc. This somewhat complicated calling convention is designed to be convenient for functions which want to capture their caller's environment by default, but also allow explicit environments to be specified. See the second example. Example:: x = 1 this_env = EvalEnvironment.capture() assert this_env.namespace["x"] == 1 def child_func(): return EvalEnvironment.capture(1) this_env_from_child = child_func() assert this_env_from_child.namespace["x"] == 1 Example:: # This function can be used like: # my_model(formula_like, data) # -> evaluates formula_like in caller's environment # my_model(formula_like, data, eval_env=1) # -> evaluates formula_like in caller's caller's environment # my_model(formula_like, data, eval_env=my_env) # -> evaluates formula_like in environment 'my_env' def my_model(formula_like, data, eval_env=0): eval_env = EvalEnvironment.capture(eval_env, reference=1) return model_setup_helper(formula_like, data, eval_env) This is how :func:`dmatrix` works. .. versionadded: 0.2.0 The ``reference`` argument. """ if isinstance(eval_env, cls): return eval_env elif isinstance(eval_env, numbers.Integral): depth = eval_env + reference else: raise TypeError("Parameter 'eval_env' must be either an integer " "or an instance of patsy.EvalEnvironment.") frame = inspect.currentframe() try: for i in range(depth + 1): if frame is None: raise ValueError("call-stack is not that deep!") frame = frame.f_back return cls([frame.f_locals, frame.f_globals], frame.f_code.co_flags & _ALL_FUTURE_FLAGS) # The try/finally is important to avoid a potential reference cycle -- # any exception traceback will carry a reference to *our* frame, which # contains a reference to our local variables, which would otherwise # carry a reference to some parent frame, where the exception was # caught...: finally: del frame
Capture an execution environment from the stack. If `eval_env` is already an :class:`EvalEnvironment`, it is returned unchanged. Otherwise, we walk up the stack by ``eval_env + reference`` steps and capture that function's evaluation environment. For ``eval_env=0`` and ``reference=0``, the default, this captures the stack frame of the function that calls :meth:`capture`. If ``eval_env + reference`` is 1, then we capture that function's caller, etc. This somewhat complicated calling convention is designed to be convenient for functions which want to capture their caller's environment by default, but also allow explicit environments to be specified. See the second example. Example:: x = 1 this_env = EvalEnvironment.capture() assert this_env.namespace["x"] == 1 def child_func(): return EvalEnvironment.capture(1) this_env_from_child = child_func() assert this_env_from_child.namespace["x"] == 1 Example:: # This function can be used like: # my_model(formula_like, data) # -> evaluates formula_like in caller's environment # my_model(formula_like, data, eval_env=1) # -> evaluates formula_like in caller's caller's environment # my_model(formula_like, data, eval_env=my_env) # -> evaluates formula_like in environment 'my_env' def my_model(formula_like, data, eval_env=0): eval_env = EvalEnvironment.capture(eval_env, reference=1) return model_setup_helper(formula_like, data, eval_env) This is how :func:`dmatrix` works. .. versionadded: 0.2.0 The ``reference`` argument.
def _parse_vars(self, tokens): """ Given an iterable of tokens, returns variables and their values as a dictionary. For example: ['dtap=prod', 'comment=some comment'] Returns: {'dtap': 'prod', 'comment': 'some comment'} """ key_values = {} for token in tokens: if token.startswith('#'): # End parsing if we encounter a comment, which lasts # until the end of the line. break else: k, v = token.split('=', 1) key = k.strip() key_values[key] = v.strip() return key_values
Given an iterable of tokens, returns variables and their values as a dictionary. For example: ['dtap=prod', 'comment=some comment'] Returns: {'dtap': 'prod', 'comment': 'some comment'}
def calc_b_value(magnitudes, completeness, max_mag=None, plotvar=True): """ Calculate the b-value for a range of completeness magnitudes. Calculates a power-law fit to given magnitudes for each completeness magnitude. Plots the b-values and residuals for the fitted catalogue against the completeness values. Computes fits using numpy.polyfit, which uses a least-squares technique. :type magnitudes: list :param magnitudes: Magnitudes to compute the b-value for. :type completeness: list :param completeness: list of completeness values to compute b-values for. :type max_mag: float :param max_mag: Maximum magnitude to attempt to fit in magnitudes. :type plotvar: bool :param plotvar: Turn plotting on or off. :rtype: list :return: List of tuples of (completeness, b-value, residual,\ number of magnitudes used) .. rubric:: Example >>> from obspy.clients.fdsn import Client >>> from obspy import UTCDateTime >>> from eqcorrscan.utils.mag_calc import calc_b_value >>> client = Client('IRIS') >>> t1 = UTCDateTime('2012-03-26T00:00:00') >>> t2 = t1 + (3 * 86400) >>> catalog = client.get_events(starttime=t1, endtime=t2, minmagnitude=3) >>> magnitudes = [event.magnitudes[0].mag for event in catalog] >>> b_values = calc_b_value(magnitudes, completeness=np.arange(3, 7, 0.2), ... plotvar=False) >>> round(b_values[4][1]) 1.0 >>> # We can set a maximum magnitude: >>> b_values = calc_b_value(magnitudes, completeness=np.arange(3, 7, 0.2), ... plotvar=False, max_mag=5) >>> round(b_values[4][1]) 1.0 """ b_values = [] # Calculate the cdf for all magnitudes counts = Counter(magnitudes) cdf = np.zeros(len(counts)) mag_steps = np.zeros(len(counts)) for i, magnitude in enumerate(sorted(counts.keys(), reverse=True)): mag_steps[i] = magnitude if i > 0: cdf[i] = cdf[i - 1] + counts[magnitude] else: cdf[i] = counts[magnitude] if not max_mag: max_mag = max(magnitudes) for m_c in completeness: if m_c >= max_mag or m_c >= max(magnitudes): warnings.warn('Not computing completeness at %s, above max_mag' % str(m_c)) break complete_mags = [] complete_freq = [] for i, mag in enumerate(mag_steps): if mag >= m_c <= max_mag: complete_mags.append(mag) complete_freq.append(np.log10(cdf[i])) if len(complete_mags) < 4: warnings.warn('Not computing completeness above ' + str(m_c) + ', fewer than 4 events') break fit = np.polyfit(complete_mags, complete_freq, 1, full=True) # Calculate the residuals according to the Wiemer & Wys 2000 definition predicted_freqs = [fit[0][1] - abs(fit[0][0] * M) for M in complete_mags] r = 100 - ((np.sum([abs(complete_freq[i] - predicted_freqs[i]) for i in range(len(complete_freq))]) * 100) / np.sum(complete_freq)) b_values.append((m_c, abs(fit[0][0]), r, str(len(complete_mags)))) if plotvar: fig, ax1 = plt.subplots() b_vals = ax1.scatter(list(zip(*b_values))[0], list(zip(*b_values))[1], c='k') resid = ax1.scatter(list(zip(*b_values))[0], [100 - b for b in list(zip(*b_values))[2]], c='r') ax1.set_ylabel('b-value and residual') plt.xlabel('Completeness magnitude') ax2 = ax1.twinx() ax2.set_ylabel('Number of events used in fit') n_ev = ax2.scatter(list(zip(*b_values))[0], list(zip(*b_values))[3], c='g') fig.legend((b_vals, resid, n_ev), ('b-values', 'residuals', 'number of events'), 'lower right') ax1.set_title('Possible completeness values') plt.show() return b_values
Calculate the b-value for a range of completeness magnitudes. Calculates a power-law fit to given magnitudes for each completeness magnitude. Plots the b-values and residuals for the fitted catalogue against the completeness values. Computes fits using numpy.polyfit, which uses a least-squares technique. :type magnitudes: list :param magnitudes: Magnitudes to compute the b-value for. :type completeness: list :param completeness: list of completeness values to compute b-values for. :type max_mag: float :param max_mag: Maximum magnitude to attempt to fit in magnitudes. :type plotvar: bool :param plotvar: Turn plotting on or off. :rtype: list :return: List of tuples of (completeness, b-value, residual,\ number of magnitudes used) .. rubric:: Example >>> from obspy.clients.fdsn import Client >>> from obspy import UTCDateTime >>> from eqcorrscan.utils.mag_calc import calc_b_value >>> client = Client('IRIS') >>> t1 = UTCDateTime('2012-03-26T00:00:00') >>> t2 = t1 + (3 * 86400) >>> catalog = client.get_events(starttime=t1, endtime=t2, minmagnitude=3) >>> magnitudes = [event.magnitudes[0].mag for event in catalog] >>> b_values = calc_b_value(magnitudes, completeness=np.arange(3, 7, 0.2), ... plotvar=False) >>> round(b_values[4][1]) 1.0 >>> # We can set a maximum magnitude: >>> b_values = calc_b_value(magnitudes, completeness=np.arange(3, 7, 0.2), ... plotvar=False, max_mag=5) >>> round(b_values[4][1]) 1.0
def setDefaultThread(self, thread_id, thread_type): """ Sets default thread to send messages to :param thread_id: User/Group ID to default to. See :ref:`intro_threads` :param thread_type: See :ref:`intro_threads` :type thread_type: models.ThreadType """ self._default_thread_id = thread_id self._default_thread_type = thread_type
Sets default thread to send messages to :param thread_id: User/Group ID to default to. See :ref:`intro_threads` :param thread_type: See :ref:`intro_threads` :type thread_type: models.ThreadType
def _db(self): """Database client for accessing storage. :returns: :class:`livebridge.storages.base.BaseStorage` """ if not hasattr(self, "_db_client") or getattr(self, "_db_client") is None: self._db_client = get_db_client() return self._db_client
Database client for accessing storage. :returns: :class:`livebridge.storages.base.BaseStorage`
def home_abbreviation(self): """ Returns a ``string`` of the home team's abbreviation, such as 'KAN'. """ abbr = re.sub(r'.*/teams/', '', str(self._home_name)) abbr = re.sub(r'/.*', '', abbr) return abbr
Returns a ``string`` of the home team's abbreviation, such as 'KAN'.
def _get_key_value(string): """Return the (key, value) as a tuple from a string.""" # Normally all properties look like this: # Unique Identifier: 600508B1001CE4ACF473EE9C826230FF # Disk Name: /dev/sda # Mount Points: None key = '' value = '' try: key, value = string.split(': ') except ValueError: # This handles the case when the property of a logical drive # returned is as follows. Here we cannot split by ':' because # the disk id has colon in it. So if this is about disk, # then strip it accordingly. # Mirror Group 0: physicaldrive 6I:1:5 string = string.lstrip(' ') if string.startswith('physicaldrive'): fields = string.split(' ') # Include fields[1] to key to avoid duplicate pairs # with the same 'physicaldrive' key key = fields[0] + " " + fields[1] value = fields[1] else: # TODO(rameshg87): Check if this ever occurs. return string.strip(' '), None return key.strip(' '), value.strip(' ')
Return the (key, value) as a tuple from a string.
def keyboard(self, default=None, heading=None, hidden=False): '''Displays the keyboard input window to the user. If the user does not cancel the modal, the value entered by the user will be returned. :param default: The placeholder text used to prepopulate the input field. :param heading: The heading for the window. Defaults to the current addon's name. If you require a blank heading, pass an empty string. :param hidden: Whether or not the input field should be masked with stars, e.g. a password field. ''' if heading is None: heading = self.addon.getAddonInfo('name') if default is None: default = '' keyboard = xbmc.Keyboard(default, heading, hidden) keyboard.doModal() if keyboard.isConfirmed(): return keyboard.getText()
Displays the keyboard input window to the user. If the user does not cancel the modal, the value entered by the user will be returned. :param default: The placeholder text used to prepopulate the input field. :param heading: The heading for the window. Defaults to the current addon's name. If you require a blank heading, pass an empty string. :param hidden: Whether or not the input field should be masked with stars, e.g. a password field.
def run_steps_from_string(self, spec, language_name='en'): """ Called from within step definitions to run other steps. """ caller = inspect.currentframe().f_back line = caller.f_lineno - 1 fname = caller.f_code.co_filename steps = parse_steps(spec, fname, line, load_language(language_name)) for s in steps: self.run_step(s)
Called from within step definitions to run other steps.
def relabel(self, label=None, group=None, depth=1): """Clone object and apply new group and/or label. Applies relabeling to children up to the supplied depth. Args: label (str, optional): New label to apply to returned object group (str, optional): New group to apply to returned object depth (int, optional): Depth to which relabel will be applied If applied to container allows applying relabeling to contained objects up to the specified depth Returns: Returns relabelled object """ relabelled = super(DynamicMap, self).relabel(label, group, depth) if depth > 0: from ..util import Dynamic def dynamic_relabel(obj, **dynkwargs): return obj.relabel(group=group, label=label, depth=depth-1) dmap = Dynamic(self, streams=self.streams, operation=dynamic_relabel) dmap.data = relabelled.data with util.disable_constant(dmap): dmap.group = relabelled.group dmap.label = relabelled.label return dmap return relabelled
Clone object and apply new group and/or label. Applies relabeling to children up to the supplied depth. Args: label (str, optional): New label to apply to returned object group (str, optional): New group to apply to returned object depth (int, optional): Depth to which relabel will be applied If applied to container allows applying relabeling to contained objects up to the specified depth Returns: Returns relabelled object
def paint( self, painter, option, widget ): """ Paints this item on the painter. :param painter | <QPainter> option | <QStyleOptionGraphicsItem> widget | <QWidget> """ if ( self._rebuildRequired ): self.rebuild() # set the coloring options painter.setPen(self.borderColor()) if ( self.isSelected() ): painter.setBrush(self.highlightColor()) else: painter.setBrush(self.fillColor()) hints = painter.renderHints() if ( not self.isAllDay() ): painter.setRenderHint(painter.Antialiasing) pen = painter.pen() pen.setWidthF(0.25) painter.setPen(pen) painter.drawPath(self.path()) # draw the text in the different rect locations title = self.title() painter.setPen(self.textColor()) for data in self._textData: painter.drawText(*data) painter.setRenderHints(hints)
Paints this item on the painter. :param painter | <QPainter> option | <QStyleOptionGraphicsItem> widget | <QWidget>
def find_mismatch(self, other, indent=''): """ Used in debugging unittests """ mismatch = super(Tree, self).find_mismatch(other, indent) sub_indent = indent + ' ' if len(list(self.subjects)) != len(list(other.subjects)): mismatch += ('\n{indent}mismatching subject lengths ' '(self={} vs other={}): ' '\n{indent} self={}\n{indent} other={}' .format(len(list(self.subjects)), len(list(other.subjects)), list(self.subjects), list(other.subjects), indent=sub_indent)) else: for s, o in zip(self.subjects, other.subjects): mismatch += s.find_mismatch(o, indent=sub_indent) if len(list(self.visits)) != len(list(other.visits)): mismatch += ('\n{indent}mismatching visit lengths ' '(self={} vs other={}): ' '\n{indent} self={}\n{indent} other={}' .format(len(list(self.visits)), len(list(other.visits)), list(self.visits), list(other.visits), indent=sub_indent)) else: for s, o in zip(self.visits, other.visits): mismatch += s.find_mismatch(o, indent=sub_indent) return mismatch
Used in debugging unittests
def update(self): """Update the FS stats using the input method.""" # Init new stats stats = self.get_init_value() if self.input_method == 'local': # Update stats using the standard system lib # Grab the stats using the psutil disk_partitions # If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys) # and ignore all others (e.g. memory partitions such as /dev/shm) try: fs_stat = psutil.disk_partitions(all=False) except UnicodeDecodeError: return self.stats # Optionnal hack to allow logicals mounts points (issue #448) # Ex: Had to put 'allow=zfs' in the [fs] section of the conf file # to allow zfs monitoring for fstype in self.get_conf_value('allow'): try: fs_stat += [f for f in psutil.disk_partitions(all=True) if f.fstype.find(fstype) >= 0] except UnicodeDecodeError: return self.stats # Loop over fs for fs in fs_stat: # Do not take hidden file system into account if self.is_hide(fs.mountpoint): continue # Grab the disk usage try: fs_usage = psutil.disk_usage(fs.mountpoint) except OSError: # Correct issue #346 # Disk is ejected during the command continue fs_current = { 'device_name': fs.device, 'fs_type': fs.fstype, # Manage non breaking space (see issue #1065) 'mnt_point': u(fs.mountpoint).replace(u'\u00A0', ' '), 'size': fs_usage.total, 'used': fs_usage.used, 'free': fs_usage.free, 'percent': fs_usage.percent, 'key': self.get_key()} stats.append(fs_current) elif self.input_method == 'snmp': # Update stats using SNMP # SNMP bulk command to get all file system in one shot try: fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) except KeyError: fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'], bulk=True) # Loop over fs if self.short_system_name in ('windows', 'esxi'): # Windows or ESXi tips for fs in fs_stat: # Memory stats are grabbed in the same OID table (ignore it) if fs == 'Virtual Memory' or fs == 'Physical Memory' or fs == 'Real Memory': continue size = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit']) used = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit']) percent = float(used * 100 / size) fs_current = { 'device_name': '', 'mnt_point': fs.partition(' ')[0], 'size': size, 'used': used, 'percent': percent, 'key': self.get_key()} stats.append(fs_current) else: # Default behavior for fs in fs_stat: fs_current = { 'device_name': fs_stat[fs]['device_name'], 'mnt_point': fs, 'size': int(fs_stat[fs]['size']) * 1024, 'used': int(fs_stat[fs]['used']) * 1024, 'percent': float(fs_stat[fs]['percent']), 'key': self.get_key()} stats.append(fs_current) # Update the stats self.stats = stats return self.stats
Update the FS stats using the input method.
def verify_url(url, secret_key, **kwargs): """ Verify a signed URL (excluding the domain and scheme). :param url: URL to sign :param secret_key: Secret key :rtype: bool :raises: URLError """ result = urlparse(url) query_args = MultiValueDict(parse_qs(result.query)) return verify_url_path(result.path, query_args, secret_key, **kwargs)
Verify a signed URL (excluding the domain and scheme). :param url: URL to sign :param secret_key: Secret key :rtype: bool :raises: URLError
def parse_GFF_attribute_string(attrStr, extra_return_first_value=False): """Parses a GFF attribute string and returns it as a dictionary. If 'extra_return_first_value' is set, a pair is returned: the dictionary and the value of the first attribute. This might be useful if this is the ID. """ if attrStr.endswith("\n"): attrStr = attrStr[:-1] d = {} first_val = "_unnamed_" for (i, attr) in itertools.izip( itertools.count(), _HTSeq.quotesafe_split(attrStr)): if _re_attr_empty.match(attr): continue if attr.count('"') not in (0, 2): raise ValueError( "The attribute string seems to contain mismatched quotes.") mo = _re_attr_main.match(attr) if not mo: raise ValueError("Failure parsing GFF attribute line") val = mo.group(2) if val.startswith('"') and val.endswith('"'): val = val[1:-1] d[intern(mo.group(1))] = intern(val) if extra_return_first_value and i == 0: first_val = val if extra_return_first_value: return (d, first_val) else: return d
Parses a GFF attribute string and returns it as a dictionary. If 'extra_return_first_value' is set, a pair is returned: the dictionary and the value of the first attribute. This might be useful if this is the ID.
def push_external_commands(self, commands): """Send a HTTP request to the satellite (POST /r_un_external_commands) to send the external commands to the satellite :param results: Results list to send :type results: list :return: True on success, False on failure :rtype: bool """ logger.debug("Pushing %d external commands", len(commands)) return self.con.post('_run_external_commands', {'cmds': commands}, wait=True)
Send a HTTP request to the satellite (POST /r_un_external_commands) to send the external commands to the satellite :param results: Results list to send :type results: list :return: True on success, False on failure :rtype: bool
def get_or_none(cls, mp, part_number): """Get part number.""" return cls.query.filter_by( upload_id=mp.upload_id, part_number=part_number ).one_or_none()
Get part number.
async def release_forks(self, philosopher): '''The ``philosopher`` has just eaten and is ready to release both forks. This method releases them, one by one, by sending the ``put_down`` action to the monitor. ''' forks = self.forks self.forks = [] self.started_waiting = 0 for fork in forks: philosopher.logger.debug('Putting down fork %s', fork) await philosopher.send('monitor', 'putdown_fork', fork) await sleep(self.cfg.waiting_period)
The ``philosopher`` has just eaten and is ready to release both forks. This method releases them, one by one, by sending the ``put_down`` action to the monitor.
def p_expr_BXOR_expr(p): """ expr : expr BXOR expr """ p[0] = make_binary(p.lineno(2), 'BXOR', p[1], p[3], lambda x, y: x ^ y)
expr : expr BXOR expr
def copy_reference(resource, doc, env, *args, **kwargs): """A row-generating function that yields from a reference. This permits an upstream package to be copied and modified by this package, while being formally referenced as a dependency The function will generate rows from a reference that has the same name as the resource term """ yield from doc.reference(resource.name)
A row-generating function that yields from a reference. This permits an upstream package to be copied and modified by this package, while being formally referenced as a dependency The function will generate rows from a reference that has the same name as the resource term
def pin_rm(self, path, *paths, **kwargs): """Removes a pinned object from local storage. Removes the pin from the given object allowing it to be garbage collected if needed. .. code-block:: python >>> c.pin_rm('QmfZY61ukoQuCX8e5Pt7v8pRfhkyxwZKZMTodAtmvyGZ5d') {'Pins': ['QmfZY61ukoQuCX8e5Pt7v8pRfhkyxwZKZMTodAtmvyGZ5d']} Parameters ---------- path : str Path to object(s) to be unpinned recursive : bool Recursively unpin the object linked to by the specified object(s) Returns ------- dict : List of IPFS objects that have been unpinned """ #PY2: No support for kw-only parameters after glob parameters if "recursive" in kwargs: kwargs.setdefault("opts", {"recursive": kwargs["recursive"]}) del kwargs["recursive"] args = (path,) + paths return self._client.request('/pin/rm', args, decoder='json', **kwargs)
Removes a pinned object from local storage. Removes the pin from the given object allowing it to be garbage collected if needed. .. code-block:: python >>> c.pin_rm('QmfZY61ukoQuCX8e5Pt7v8pRfhkyxwZKZMTodAtmvyGZ5d') {'Pins': ['QmfZY61ukoQuCX8e5Pt7v8pRfhkyxwZKZMTodAtmvyGZ5d']} Parameters ---------- path : str Path to object(s) to be unpinned recursive : bool Recursively unpin the object linked to by the specified object(s) Returns ------- dict : List of IPFS objects that have been unpinned
def property(func): """Wrap a function as a property. This differs from attribute by identifying properties explicitly listed in the class definition rather than named attributes defined on instances of a class at init time. """ attr = abc.abstractmethod(func) attr.__iproperty__ = True attr = Property(attr) return attr
Wrap a function as a property. This differs from attribute by identifying properties explicitly listed in the class definition rather than named attributes defined on instances of a class at init time.
def weekend_to_monday(dt): """ If holiday falls on Sunday or Saturday, use day thereafter (Monday) instead. Needed for holidays such as Christmas observation in Europe """ if dt.weekday() == 6: return dt + timedelta(1) elif dt.weekday() == 5: return dt + timedelta(2) return dt
If holiday falls on Sunday or Saturday, use day thereafter (Monday) instead. Needed for holidays such as Christmas observation in Europe
def _parseCounters(self, data): """Parse simple stats list of key, value pairs. @param data: Multiline data with one key-value pair in each line. @return: Dictionary of stats. """ info_dict = util.NestedDict() for line in data.splitlines(): mobj = re.match('^\s*([\w\.]+)\s*=\s*(\S.*)$', line) if mobj: (key, value) = mobj.groups() klist = key.split('.') info_dict.set_nested(klist, parse_value(value)) return info_dict
Parse simple stats list of key, value pairs. @param data: Multiline data with one key-value pair in each line. @return: Dictionary of stats.
def sign_with_privkey( digest: bytes, privkey: Ed25519PrivateKey, global_pubkey: Ed25519PublicPoint, nonce: int, global_commit: Ed25519PublicPoint, ) -> Ed25519Signature: """Create a CoSi signature of `digest` with the supplied private key. This function needs to know the global public key and global commitment. """ h = _ed25519.H(privkey) a = _ed25519.decodecoord(h) S = (nonce + _ed25519.Hint(global_commit + global_pubkey + digest) * a) % _ed25519.l return Ed25519Signature(_ed25519.encodeint(S))
Create a CoSi signature of `digest` with the supplied private key. This function needs to know the global public key and global commitment.
def rmsd_eval(rmsd_params): """Builds a model and runs profit against a reference model. Parameters ---------- rmsd_params Returns ------- rmsd: float rmsd against reference model as calculated by profit. """ specification, sequence, parsed_ind, reference_pdb = rmsd_params model = specification(*parsed_ind) model.pack_new_sequences(sequence) ca, bb, aa = run_profit(model.pdb, reference_pdb, path1=False, path2=False) return bb
Builds a model and runs profit against a reference model. Parameters ---------- rmsd_params Returns ------- rmsd: float rmsd against reference model as calculated by profit.
def undo_sign_out(entry, session=None): """Sign in a signed out entry. :param entry: `models.Entry` object. The entry to sign back in. :param session: (optional) SQLAlchemy session through which to access the database. """ # noqa if session is None: session = Session() else: session = session entry_to_sign_in = ( session .query(Entry) .filter(Entry.uuid == entry.uuid) .one_or_none() ) if entry_to_sign_in: logger.info('Undo sign out: {}'.format(entry_to_sign_in.user_id)) logger.debug('Undo sign out: {}'.format(entry_to_sign_in)) entry_to_sign_in.time_out = None session.add(entry_to_sign_in) session.commit() else: error_message = 'Entry not found: {}'.format(entry) logger.error(error_message) raise ValueError(error_message)
Sign in a signed out entry. :param entry: `models.Entry` object. The entry to sign back in. :param session: (optional) SQLAlchemy session through which to access the database.
def censor_entity_types(self, entity_types): # type: (set) -> TermDocMatrixFactory ''' Entity types to exclude from feature construction. Terms matching specificed entities, instead of labeled by their lower case orthographic form or lemma, will be labeled by their entity type. Parameters ---------- entity_types : set of entity types outputted by spaCy 'TIME', 'WORK_OF_ART', 'PERSON', 'MONEY', 'ORG', 'ORDINAL', 'DATE', 'CARDINAL', 'LAW', 'QUANTITY', 'GPE', 'PERCENT' Returns --------- self ''' assert type(entity_types) == set self._entity_types_to_censor = entity_types self._feats_from_spacy_doc = FeatsFromSpacyDoc( use_lemmas=self._use_lemmas, entity_types_to_censor=self._entity_types_to_censor ) return self
Entity types to exclude from feature construction. Terms matching specificed entities, instead of labeled by their lower case orthographic form or lemma, will be labeled by their entity type. Parameters ---------- entity_types : set of entity types outputted by spaCy 'TIME', 'WORK_OF_ART', 'PERSON', 'MONEY', 'ORG', 'ORDINAL', 'DATE', 'CARDINAL', 'LAW', 'QUANTITY', 'GPE', 'PERCENT' Returns --------- self
def create_append(filename: str, layers: Union[np.ndarray, Dict[str, np.ndarray], loompy.LayerManager], row_attrs: Dict[str, np.ndarray], col_attrs: Dict[str, np.ndarray], *, file_attrs: Dict[str, str] = None, fill_values: Dict[str, np.ndarray] = None) -> None: """ **DEPRECATED** - Use `new` instead; see https://github.com/linnarsson-lab/loompy/issues/42 """ deprecated("'create_append' is deprecated. See https://github.com/linnarsson-lab/loompy/issues/42") if os.path.exists(filename): with connect(filename) as ds: ds.add_columns(layers, col_attrs, fill_values=fill_values) else: create(filename, layers, row_attrs, col_attrs, file_attrs=file_attrs)
**DEPRECATED** - Use `new` instead; see https://github.com/linnarsson-lab/loompy/issues/42
def AppendSource(self, type_indicator, attributes): """Appends a source. If you want to implement your own source type you should create a subclass in source_type.py and change the AppendSource method to handle the new subclass. This function raises FormatError if an unsupported source type indicator is encountered. Args: type_indicator (str): source type indicator. attributes (dict[str, object]): source attributes. Returns: SourceType: a source type. Raises: FormatError: if the type indicator is not set or unsupported, or if required attributes are missing. """ if not type_indicator: raise errors.FormatError('Missing type indicator.') try: source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType( type_indicator, attributes) except (AttributeError, TypeError) as exception: raise errors.FormatError(( 'Unable to create source type: {0:s} for artifact definition: {1:s} ' 'with error: {2!s}').format(type_indicator, self.name, exception)) self.sources.append(source_object) return source_object
Appends a source. If you want to implement your own source type you should create a subclass in source_type.py and change the AppendSource method to handle the new subclass. This function raises FormatError if an unsupported source type indicator is encountered. Args: type_indicator (str): source type indicator. attributes (dict[str, object]): source attributes. Returns: SourceType: a source type. Raises: FormatError: if the type indicator is not set or unsupported, or if required attributes are missing.
def drain(self): """ Read until there is nothing more to be read. Only intended for test code/debugging! @returns: True on success @rtype: bool """ try: unlock = self.stick.acquire() return self.stick.drain() finally: unlock()
Read until there is nothing more to be read. Only intended for test code/debugging! @returns: True on success @rtype: bool
def set_bucket_props(self, transport, bucket, props): """ set_bucket_props(bucket, props) Sets bucket properties for the given bucket. .. note:: This request is automatically retried :attr:`retries` times if it fails due to network error. :param bucket: the bucket whose properties will be set :type bucket: RiakBucket :param props: the properties to set :type props: dict """ _validate_bucket_props(props) return transport.set_bucket_props(bucket, props)
set_bucket_props(bucket, props) Sets bucket properties for the given bucket. .. note:: This request is automatically retried :attr:`retries` times if it fails due to network error. :param bucket: the bucket whose properties will be set :type bucket: RiakBucket :param props: the properties to set :type props: dict
def poll_devices(self): """Request status updates from each device.""" for addr in self.devices: device = self.devices[addr] if not device.address.is_x10: device.async_refresh_state()
Request status updates from each device.
def get_value_as_list(self, dictionary, key): """Helper function to check and convert a value to list. Helper function to check and convert a value to json list. This helps the ribcl data to be generalized across the servers. :param dictionary: a dictionary to check in if key is present. :param key: key to be checked if thats present in the given dictionary. :returns the data converted to a list. """ if key not in dictionary: return None value = dictionary[key] if not isinstance(value, list): return [value] else: return value
Helper function to check and convert a value to list. Helper function to check and convert a value to json list. This helps the ribcl data to be generalized across the servers. :param dictionary: a dictionary to check in if key is present. :param key: key to be checked if thats present in the given dictionary. :returns the data converted to a list.
def _get_indicators(modules): """For all modules or classes listed, return the children that are instances of xclim.utils.Indicator. modules : sequence Sequence of modules to inspect. """ out = [] for obj in modules: for key, val in obj.__dict__.items(): if isinstance(val, xcu.Indicator): out.append(val) return out
For all modules or classes listed, return the children that are instances of xclim.utils.Indicator. modules : sequence Sequence of modules to inspect.
def Wm(self): """Return the smoothing regularization matrix Wm of the grid """ centroids = self.get_element_centroids() Wm = scipy.sparse.csr_matrix( (self.nr_of_elements, self.nr_of_elements)) # Wm = np.zeros((self.nr_of_elements, self.nr_of_elements)) for i, nb in enumerate(self.element_neighbors): for j, edges in zip(nb, self.element_neighbors_edges[i]): # side length edge_coords = self.nodes['presort'][edges][:, 1:] edge_length = np.linalg.norm( edge_coords[1, :] - edge_coords[0, :] ) distance = np.linalg.norm(centroids[i] - centroids[j]) # main diagonal Wm[i, i] += edge_length / distance # side diagonals Wm[i, j] -= edge_length / distance return Wm
Return the smoothing regularization matrix Wm of the grid