def shortest_undirected_path(self, physical_qubit1, physical_qubit2):

        try:
            return nx.shortest_path(self.graph.to_undirected(as_view=True), source=physical_qubit1,
                                    target=physical_qubit2)
        except nx.exception.NetworkXNoPath:
            raise CouplingError(
                "Nodes %s and %s are not connected" % (str(physical_qubit1), str(physical_qubit2)))

Returns the shortest undirected path between physical_qubit1 and physical_qubit2.

Args:
        physical_qubit1 (int): A physical qubit
        physical_qubit2 (int): Another physical qubit

Returns:
        List: The shortest undirected path

Raises:
        CouplingError: When there is no path between physical_qubit1, physical_qubit2.


def combine(path1, path2):
    # type: (Text, Text) -> Text

    if not path1:
        return path2.lstrip()
    return "{}/{}".format(path1.rstrip("/"), path2.lstrip("/"))

Join two paths together.

    This is faster than :func:`~fs.path.join`, but only works when the
    second path is relative, and there are no back references in either
    path.

Args:
        path1 (str): A PyFilesytem path.
        path2 (str): A PyFilesytem path.

Returns:
        str: The joint path.

Example:
        >>> combine("foo/bar", "baz")
        'foo/bar/baz'


def _max_retries_for_error(self, error):

        status = error.get("status")
        if status == "ABORTED" and get_transactions() > 0:
            # Avoids retrying Conflicts when inside a transaction.
            return None
        return self._MAX_RETRIES.get(status)

Handles Datastore response errors according to their documentation.

    Parameters:
        error(dict)

Returns:
        int or None: The max number of times this error should be
        retried or None if it shouldn't.

    See also:
        https://cloud.google.com/datastore/docs/concepts/errors


def __init__(self, access_token, access_token_type, refresh_token=None, expires_in=None, state=None):

        self.access_token = access_token
        self.access_token_type = access_token_type
        self.refresh_token = refresh_token
        self.expires_in = expires_in
        self.state = state

Initialziation of the object

Args:
        access_token (str): Access token
        access_token_type (str): Access token type
        refresh_token (str):
        expires_in (int): Seconds after which the token will expire
        state (str):


def get_icohp_dict_by_bondlengths(self, minbondlength=0.0, maxbondlength=8.0):

        newicohp_dict = {}
        for value in self._icohplist.values():
            if value._length >= minbondlength and value._length <= maxbondlength:
                newicohp_dict[value._label] = value
        return newicohp_dict

get a dict of IcohpValues corresponding to certaind bond lengths

Args:
        minbondlength: defines the minimum of the bond lengths of the bonds
        maxbondlength: defines the maximum of the bond lengths of the bonds

Returns:
        dict of IcohpValues, the keys correspond to the values from the initial list_labels


def set(self, key, data):

        self.raise_error_if_not_open()

        if key in self._file:
            del self._file[key]

        self._file.create_dataset(key, data=data)

Set the given data to the container with the given key.
    Any existing data for the given key is discarded/overwritten.

Args:
        key (str): A key to store the data for.
        data (numpy.ndarray): Array-like data.

Note:
        The container has to be opened in advance.


def set_wallpaper(image):

	desktop_env = system.get_name()

	if desktop_env in ['gnome', 'unity', 'cinnamon', 'pantheon', 'mate']:
		uri = 'file://%s' % image

		SCHEMA = 'org.gnome.desktop.background'
		KEY = 'picture-uri'

		if desktop_env == 'mate':
			uri = image

			SCHEMA = 'org.mate.background'
			KEY = 'picture-filename'

		try:
			from gi.repository import Gio

			gsettings = Gio.Settings.new(SCHEMA)
			gsettings.set_string(KEY, uri)
		except ImportError:
			try:
				gsettings_proc = sp.Popen(
					['gsettings', 'set', SCHEMA, KEY, uri])
			except:  # MATE < 1.6
				sp.Popen(['mateconftool-2',
						  '-t',
						  'string',
						  '--set',
						  '/desktop/mate/background/picture_filename',
						  '%s' % image],
						 stdout=sp.PIPE)
			finally:
				gsettings_proc.communicate()

				if gsettings_proc.returncode != 0:
					sp.Popen(['mateconftool-2',
							  '-t',
							  'string',
							  '--set',
							  '/desktop/mate/background/picture_filename',
							  '%s' % image])

	elif desktop_env == 'gnome2':
		sp.Popen(
			['gconftool-2',
			 '-t',
			 'string',
			 '--set',
			 '/desktop/gnome/background/picture_filename',
			 image]
		)

	elif desktop_env == 'kde':
		# This probably only works in Plasma 5+

		kde_script = dedent(
		).format(image)

		sp.Popen(
				['dbus-send',
				 '--session',
				 '--dest=org.kde.plasmashell',
				 '--type=method_call',
				 '/PlasmaShell',
				 'org.kde.PlasmaShell.evaluateScript',
				 'string:{}'.format(kde_script)]
		)

	elif desktop_env in ['kde3', 'trinity']:
		args = 'dcop kdesktop KBackgroundIface setWallpaper 0 "%s" 6' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'xfce4':
		# XFCE4's image property is not image-path but last-image (What?)

		list_of_properties = system.get_cmd_out(
				['xfconf-query',
				 '-R',
				 '-l',
				 '-c',
				 'xfce4-desktop',
				 '-p',
				 '/backdrop']
		)

		for i in list_of_properties.split('\n'):
			if i.endswith('last-image'):
				# The property given is a background property
				sp.Popen(
					['xfconf-query -c xfce4-desktop -p %s -s "%s"' %
						(i, image)],
					shell=True)

				sp.Popen(['xfdesktop --reload'], shell=True)

	elif desktop_env == 'razor-qt':
		desktop_conf = configparser.ConfigParser()
		# Development version

		desktop_conf_file = os.path.join(
			get_config_dir('razor')[0], 'desktop.conf')

		if os.path.isfile(desktop_conf_file):
			config_option = r'screens\1\desktops\1\wallpaper'

		else:
			desktop_conf_file = os.path.join(
				os.path.expanduser('~'), '.razor/desktop.conf')
			config_option = r'desktops\1\wallpaper'

		desktop_conf.read(os.path.join(desktop_conf_file))
		try:
			if desktop_conf.has_option('razor', config_option):
				desktop_conf.set('razor', config_option, image)
				with codecs.open(desktop_conf_file, 'w', encoding='utf-8', errors='replace') as f:
					desktop_conf.write(f)
		except:
			pass

	elif desktop_env in ['fluxbox', 'jwm', 'openbox', 'afterstep', 'i3']:
		try:
			args = ['feh', '--bg-scale', image]
			sp.Popen(args)
		except:
			sys.stderr.write('Error: Failed to set wallpaper with feh!')
			sys.stderr.write('Please make sre that You have feh installed.')

	elif desktop_env == 'icewm':
		args = ['icewmbg', image]
		sp.Popen(args)

	elif desktop_env == 'blackbox':
		args = ['bsetbg', '-full', image]
		sp.Popen(args)

	elif desktop_env == 'lxde':
		args = 'pcmanfm --set-wallpaper %s --wallpaper-mode=scaled' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'lxqt':
		args = 'pcmanfm-qt --set-wallpaper %s --wallpaper-mode=scaled' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'windowmaker':
		args = 'wmsetbg -s -u %s' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'enlightenment':
		args = 'enlightenment_remote -desktop-bg-add 0 0 0 0 %s' % image
		sp.Popen(args, shell=True)

	elif desktop_env == 'awesome':
		with sp.Popen("awesome-client", stdin=sp.PIPE) as awesome_client:
			command = ('local gears = require("gears"); for s = 1,'
						' screen.count() do gears.wallpaper.maximized'
						'("%s", s, true); end;') % image
			awesome_client.communicate(input=bytes(command, 'UTF-8'))

	elif desktop_env == 'windows':
		WINDOWS_SCRIPT = dedent() % image

		windows_script_file = os.path.join(
			tempfile.gettempdir(), 'wallscript.bat')

		with open(windows_script_file, 'w') as f:
			f.write(WINDOWS_SCRIPT)

		sp.Popen([windows_script_file], shell=True)

		# Sometimes the method above works
		# and sometimes the one below

		SPI_SETDESKWALLPAPER = 20
		ctypes.windll.user32.SystemParametersInfoA(
			SPI_SETDESKWALLPAPER, 0, image, 0)

	elif desktop_env == 'mac':
		try:
			from appscript import app, mactypes
			app('Finder').desktop_picture.set(mactypes.File(image))
		except ImportError:
			OSX_SCRIPT = dedent(
				) % image

			sp.Popen(['osascript', OSX_SCRIPT])
	else:
		try:
			sp.Popen(['feh', '--bg-scale', image])
			# feh is nearly a catch-all for Linux WMs
		except:
			pass

Set the desktop wallpaper.

    Sets the desktop wallpaper to an image.

Args:
        image (str): The path to the image to be set as wallpaper.


def voronoi(script, hole_num=50, target_layer=None, sample_layer=None, thickness=0.5, backward=True):

    if target_layer is None:
        target_layer = script.current_layer()
    if sample_layer is None:
        # Current layer is currently not changed after poisson_disk is run
        sampling.poisson_disk(script, sample_num=hole_num)
        sample_layer = script.last_layer()

    vert_color.voronoi(script, target_layer=target_layer, source_layer=sample_layer, backward=backward)
    select.vert_quality(script, min_quality=0.0, max_quality=thickness)
    if backward:
        select.invert(script)
    delete.selected(script)
    smooth.laplacian(script, iterations=3)

    return None

Turn a model into a surface with Voronoi style holes in it

    References:
    http://meshlabstuff.blogspot.com/2009/03/creating-voronoi-sphere.html
    http://meshlabstuff.blogspot.com/2009/04/creating-voronoi-sphere-2.html

    Requires FilterScript object

Args:
        script: the FilterScript object to write the filter to. Does not
            work with a script filename.

    Layer stack:
        No impacts

    MeshLab versions:
        2016.12
        1.3.4BETA


def audio(self, audio, sample_rate, name=None, subdir=''):

        from chainerui.report.audio_report import check_available
        if not check_available():
            return
        from chainerui.report.audio_report import report as _audio

        col_name = self.get_col_name(name, 'audio')
        out_dir, rel_out_dir = self.get_subdir(subdir)
        filename, _ = _audio(audio, sample_rate, out_dir, col_name)
        self.audios[col_name] = os.path.join(rel_out_dir, filename)

        self.count += 1

Summary audio to listen on web browser.

Args:
        audio (:class:`numpy.ndarray` or :class:`cupy.ndarray` or \
            :class:`chainer.Variable`): sampled wave array.
        sample_rate (int): sampling rate.
        name (str): name of image. set as column name. when not setting,
            assigned ``'audio'`` + sequential number.
        subdir (str): sub-directory path of output.


def get_op_traceback(self, op_name):

    if not self._graph_traceback:
      raise ValueError('No graph traceback has been received yet.')
    for op_log_entry in self._graph_traceback.log_entries:
      if op_log_entry.name == op_name:
        return self._code_def_to_traceback_list(op_log_entry.code_def)
    raise ValueError(
        'No op named "%s" can be found in the graph of the latest version '
        ' (%d).' % (op_name, self._graph_version))

Get the traceback of an op in the latest version of the TF graph.

Args:
        op_name: Name of the op.

Returns:
        Creation traceback of the op, in the form of a list of 2-tuples:
        (file_path, lineno)

Raises:
        ValueError: If the op with the given name cannot be found in the latest
        version of the graph that this SourceManager instance has received, or
        if this SourceManager instance has not received any graph traceback yet.


def DeregisterMountPoint(cls, mount_point):

    if mount_point not in cls._mount_points:
      raise KeyError('Mount point: {0:s} not set.'.format(mount_point))

    del cls._mount_points[mount_point]

Deregisters a path specification mount point.

Args:
        mount_point (str): mount point identifier.

Raises:
        KeyError: if the corresponding mount point is not set.


def create_view(operations, operation):

    operations.execute("CREATE VIEW %s AS %s" % (
        operation.target.name,
        operation.target.sqltext
    ))

Implements ``CREATE VIEW``.

Args:
        operations: instance of ``alembic.operations.base.Operations``
        operation: instance of :class:`.ReversibleOp`

Returns:
        ``None``


def __init__(self, desired_capabilities, url='http://127.0.0.1:3456/wd/hub'):

        self.session_id = None
        self.capabilities = None
        self.desired_capabilities = desired_capabilities
        self.remote_invoker = RemoteInvoker(url)

Initialize the WebDriver

Args:
        desired_capabilities(dict): The desired capabilities requested by
            the local end.
        url(str): The url of remote server, default: localhost:3456/wd/hub.


def get_graph(graph, conn, **kwargs):

    sparql = render_without_request("sparqlGraphDataTemplate.rq",
                                    prefix=NSM.prefix(),
                                    graph=graph)
    return conn.query(sparql, **kwargs)

Returns all the triples for a specific are graph

Args:
        graph: the URI of the graph to retreive
        conn: the rdfframework triplestore connection


def percent_point(self, U):

        self.check_fit()

        return scipy.optimize.brentq(self._brentq_cdf(U), -1000.0, 1000.0)

Given a cdf value, returns a value in original space.

Args:
        U(numpy.array): cdf values in [0,1]

Returns:
        numpy.array: value in original space


def setDocuments(self, documenting_pid, documented_pid):

        self._check_initialized()
        documenting_id = self.getObjectByPid(documenting_pid)
        documented_id = self.getObjectByPid(documented_pid)
        self.add((documenting_id, CITO.documents, documented_id))

Add a CiTO, the Citation Typing Ontology, triple asserting that
    ``documenting_pid`` documents ``documented_pid``.

    Adds assertion: ``documenting_pid cito:documents documented_pid``

Args:
        documenting_pid: str
        PID of a Science Object that documents ``documented_pid``.

        documented_pid: str
        PID of a Science Object that is documented by ``documenting_pid``.


def export_dae(filename, cutout, level=0):

    if ".dae" not in filename:
        filename = filename + ".dae"

    vs, fs = mcubes.marching_cubes(cutout, level)
    mcubes.export_mesh(vs, fs, filename, "ndioexport")

Converts a dense annotation to a DAE, using Marching Cubes (PyMCubes).

Args:
        filename (str): The filename to write out to
        cutout (numpy.ndarray): The dense annotation
        level (int): The level at which to run mcubes

Returns:
        boolean success


def _add_genotype_calls(self, variant_obj, variant_line, case_obj):

        variant_line = variant_line.split('\t')
        #if there is gt calls we have no individuals to add
        if len(variant_line) > 8:
            gt_format = variant_line[8].split(':')
            for individual in case_obj.individuals:
                sample_id = individual.ind_id
                index = individual.ind_index

                gt_call = variant_line[9+index].split(':')

                raw_call = dict(zip(gt_format, gt_call))

                genotype = Genotype(**raw_call)

                variant_obj.add_individual(puzzle_genotype(
                    sample_id = sample_id,
                    genotype = genotype.genotype,
                    case_id = case_obj.name,
                    phenotype = individual.phenotype,
                    ref_depth = genotype.ref_depth,
                    alt_depth = genotype.alt_depth,
                    genotype_quality = genotype.genotype_quality,
                    depth = genotype.depth_of_coverage,
                    supporting_evidence = genotype.supporting_evidence,
                    pe_support = genotype.pe_support,
                    sr_support = genotype.sr_support,
                ))

Add the genotype calls for the variant

Args:
        variant_obj (puzzle.models.Variant)
        variant_dict (dict): A variant dictionary
        case_obj (puzzle.models.Case)


def _CopyFromDateTimeValues(self, date_time_values):

    year = date_time_values.get('year', 0)
    month = date_time_values.get('month', 0)
    day_of_month = date_time_values.get('day_of_month', 0)
    hours = date_time_values.get('hours', 0)
    minutes = date_time_values.get('minutes', 0)
    seconds = date_time_values.get('seconds', 0)
    microseconds = date_time_values.get('microseconds', 0)

    precision_helper = precisions.PrecisionHelperFactory.CreatePrecisionHelper(
        self._precision)

    fraction_of_second = precision_helper.CopyMicrosecondsToFractionOfSecond(
        microseconds)

    self._normalized_timestamp = None
    self._number_of_seconds = self._GetNumberOfSecondsFromElements(
        year, month, day_of_month, hours, minutes, seconds)
    self._time_elements_tuple = (
        year, month, day_of_month, hours, minutes, seconds)
    self.fraction_of_second = fraction_of_second
    self.is_local_time = False

Copies time elements from date and time values.

Args:
        date_time_values  (dict[str, int]): date and time values, such as year,
        month, day of month, hours, minutes, seconds, microseconds.

Raises:
        ValueError: if no helper can be created for the current precision.


def parse_elements(elements):

        if not len(elements) in (14, 15):
            raise ValueError('Invalid GGA fix data')
        time = datetime.time(*[int(elements[0][i:i + 2])
                               for i in range(0, 6, 2)])
        # Latitude and longitude are checked for validity during Fix
        # instantiation
        latitude = parse_latitude(elements[1], elements[2])
        longitude = parse_longitude(elements[3], elements[4])
        quality = int(elements[5])
        if not 0 <= quality <= 9:
            raise ValueError('Invalid quality value %r' % quality)
        satellites = int(elements[6])
        if not 0 <= satellites <= 12:
            raise ValueError('Invalid number of satellites %r'
                             % satellites)
        dilution = float(elements[7])
        altitude = float(elements[8])
        if elements[9] == 'F':
            altitude = altitude * 3.2808399
        elif not elements[9] == 'M':
            raise ValueError('Unknown altitude unit %r' % elements[9])
        if elements[10] in ('-', ''):
            geoid_delta = False
            logging.warning('Altitude data could be incorrect, as the geoid '
                            'difference has not been provided')
        else:
            geoid_delta = float(elements[10])
        if elements[11] == 'F':
            geoid_delta = geoid_delta * 3.2808399
        elif geoid_delta and not elements[11] == 'M':
            raise ValueError('Unknown geoid delta unit %r' % elements[11])
        dgps_delta = float(elements[12]) if elements[12] else None
        dgps_station = int(elements[13]) if elements[13] else None
        mode = elements[14] if len(elements) == 15 else None
        return Fix(time, latitude, longitude, quality, satellites, dilution,
                   altitude, geoid_delta, dgps_delta, dgps_station, mode)

Parse essential fix's data elements.

Args:
        elements (list): Data values for fix

Returns:
        Fix: Fix object representing data


def on_conflict(self, fields: List[Union[str, Tuple[str]]], action, index_predicate: str=None):

        self.conflict_target = fields
        self.conflict_action = action
        self.index_predicate = index_predicate

        return self

Sets the action to take when conflicts arise when attempting
    to insert/create a new row.

Args:
        fields:
            The fields the conflicts can occur in.

        action:
            The action to take when the conflict occurs.

        index_predicate:
            The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
            conflicts)


def read_hdf(cls,path,key=None):

        df = pd.read_hdf(path,key)
        df['scored_calls'] = df['scored_calls'].apply(lambda x: json.loads(x))
        df['channel_values'] = df['channel_values'].apply(lambda x: json.loads(x))
        df['regions'] = df['regions'].apply(lambda x: json.loads(x))
        df['phenotype_calls'] = df['phenotype_calls'].apply(lambda x: json.loads(x))
        df['neighbors'] = df['neighbors'].apply(lambda x: json.loads(x))
        df['neighbors'] = df['neighbors'].apply(lambda x:
                np.nan if not isinstance(x,dict) else dict(zip([int(y) for y in x.keys()],x.values()))
            )
        df['frame_shape'] = df['frame_shape'].apply(lambda x: tuple(json.loads(x)))
        df = cls(df)
        f = h5py.File(path,'r')
        mpp = f[key].attrs["microns_per_pixel"]
        if not np.isnan(mpp): df.microns_per_pixel = mpp
        f.close()
        return df

Read a CellDataFrame from an hdf5 file.

Args:
        path (str): the path to read from
        key (str): the name of the location to read from

Returns:
        CellDataFrame


def _determine_api_url(self, platform, service, action):

        base_uri = settings.BASE_PAL_URL.format(platform)
        if service == "Recurring":
            api_version = settings.API_RECURRING_VERSION
        elif service == "Payout":
            api_version = settings.API_PAYOUT_VERSION
        else:
            api_version = settings.API_PAYMENT_VERSION
        return '/'.join([base_uri, service, api_version, action])

This returns the Adyen API endpoint based on the provided platform,
    service and action.

Args:
        platform (str): Adyen platform, ie 'live' or 'test'.
        service (str): API service to place request through.
        action (str): the API action to perform.


def Decrypt(self, encrypted_data):

    index_split = -(len(encrypted_data) % Blowfish.block_size)
    if index_split:
      remaining_encrypted_data = encrypted_data[index_split:]
      encrypted_data = encrypted_data[:index_split]
    else:
      remaining_encrypted_data = b''

    decrypted_data = self._blowfish_cipher.decrypt(encrypted_data)

    return decrypted_data, remaining_encrypted_data

Decrypts the encrypted data.

Args:
        encrypted_data (bytes): encrypted data.

Returns:
        tuple[bytes,bytes]: decrypted data and remaining encrypted data.


def from_file(filename, file_format="xyz"):

        mols = list(pb.readfile(str(file_format), str(filename)))
        return BabelMolAdaptor(mols[0].OBMol)

Uses OpenBabel to read a molecule from a file in all supported formats.

Args:
        filename: Filename of input file
        file_format: String specifying any OpenBabel supported formats.

Returns:
        BabelMolAdaptor object


def read(self, size=None):

        data = self.rfile.read(size)
        self.bytes_read += len(data)
        self._check_length()
        return data

Read a chunk from rfile buffer and return it.

Args:
        size (int): amount of data to read

Returns:
        bytes: Chunk from rfile, limited by size if specified.


def connection_made(self, transport: asyncio.transports.Transport):

        self._transport = transport
        self._remote_host = self._transport.get_extra_info('peername')
        self._extra = {"client": str(self._remote_host)}
        self.connections.add(self)

        self._stream_reader = asyncio.StreamReader(loop=self._loop)
        self._stream_writer = asyncio.StreamWriter(transport, self,
                                                   self._stream_reader,
                                                   self._loop)
        super().connection_made(transport)
        if self.timeout:
            self._timeout_handler = self._loop.call_soon(
                self.timeout_callback)
        self._handlertask = asyncio.ensure_future(self.query_handler())
        if self.debug:
            access_logger.info("connected", extra=self._extra)

连接建立起来触发的回调函数.

    用于设定一些参数,并将监听任务放入事件循环,如果设置了timeout,也会将timeout_callback放入事件循环

    Parameters:
        transport (asyncio.Transports): - 连接的传输对象


def getWindow(title, exact=False):

    titles = getWindows()
    hwnd = titles.get(title, None)
    if not hwnd and not exact:
        for k, v in titles.items():
            if title in k:
                hwnd = v
                break
    if hwnd:
        return Window(hwnd)
    else:
        return None

Return Window object if 'title' or its part found in visible windows titles, else return None

    Return only 1 window found first

Args:
        title: unicode string
        exact (bool): True if search only exact match


def get_tokens(self, *, payer_id, credit_card_token_id, start_date, end_date):

        payload = {
            "language": self.client.language.value,
            "command": PaymentCommand.GET_TOKENS.value,
            "merchant": {
                "apiLogin": self.client.api_login,
                "apiKey": self.client.api_key
            },
            "creditCardTokenInformation": {
                "payerId": payer_id,
                "creditCardTokenId": credit_card_token_id,
                "startDate": start_date.strftime('%Y-%m-%dT%H:%M:%S'),
                "endDate": end_date.strftime('%Y-%m-%dT%H:%M:%S')
            },
            "test": self.client.is_test
        }
        return self.client._post(self.url, json=payload)

With this functionality you can query previously the Credit Cards Token.

Args:
        payer_id:
        credit_card_token_id:
        start_date:
        end_date:

    Returns:


def forward_feed(self, amount):

        if amount <= 255 and amount >=0:
            self.send(chr(27)+'J'+chr(amount))
        else:
            raise RuntimeError('Invalid foward feed, must be less than 255 and >= 0')

Calling this function finishes input of the current line, then moves the vertical
    print position forward by x/300 inch.

Args:
        amount: how far foward you want the position moved. Actual movement is calculated as
        amount/300 inches.

Returns:
        None

Raises:
        RuntimeError: Invalid foward feed.


def add_to_buffer(self, content, read_position):

        self.read_position = read_position
        if self.read_buffer is None:
            self.read_buffer = content
        else:
            self.read_buffer = content + self.read_buffer

Add additional bytes content as read from the read_position.

Args:
        content (bytes): data to be added to buffer working BufferWorkSpac.
        read_position (int): where in the file pointer the data was read from.


def SendCommand(self, command, arg=None):

        if arg is not None:
            if not isinstance(arg, bytes):
                arg = arg.encode('utf8')
            command = b'%s:%s' % (command, arg)

        self._Write(io.BytesIO(command), len(command))

Sends a command to the device.

Args:
        command: The command to send.
        arg: Optional argument to the command.


def padding_to_length(padding):

  non_padding = 1.0 - padding
  return tf.to_int32(tf.reduce_sum(non_padding, axis=-1))

Calculate the length of mask based on padding.

Args:
        padding: a Tensor with shape [..., length].

Returns:
        a Tensor with shape [...].


def model(self, inputs, mode='train'):

        # Extract features
        training = (mode == 'train')
        with tf.variable_scope('conv1') as scope:
            conv = tf.layers.conv2d(inputs=inputs, filters=16, kernel_size=[3, 3], padding='SAME')
            bn = tf.layers.batch_normalization(inputs=conv, training=training)
            bn = tf.nn.relu(bn)
            conv = tf.layers.conv2d(inputs=bn, filters=16, kernel_size=[3, 3], padding='SAME')
            bn = tf.layers.batch_normalization(inputs=conv, training=training)
            bn = tf.nn.relu(bn)
            pool = tf.layers.max_pooling2d(bn, pool_size=[2, 2], strides=2, padding='SAME', name=scope.name)

        with tf.variable_scope('conv2') as scope:
            conv = tf.layers.conv2d(inputs=pool, filters=32, kernel_size=[3, 3], padding='SAME')
            bn = tf.layers.batch_normalization(inputs=conv, training=training)
            bn = tf.nn.relu(bn)
            conv = tf.layers.conv2d(inputs=bn, filters=32, kernel_size=[3, 3], padding='SAME')
            bn = tf.layers.batch_normalization(inputs=conv, training=training)
            bn = tf.nn.relu(bn)
            pool = tf.layers.max_pooling2d(bn, pool_size=[2, 2], strides=2, padding='SAME', name=scope.name)

        with tf.variable_scope('conv3') as scope:
            conv = tf.layers.conv2d(inputs=pool, filters=32, kernel_size=[3, 3], padding='SAME')
            bn = tf.layers.batch_normalization(inputs=conv, training=training)
            bn = tf.nn.relu(bn)
            conv = tf.layers.conv2d(inputs=bn, filters=32, kernel_size=[3, 3], padding='SAME')
            bn = tf.layers.batch_normalization(inputs=conv, training=training)
            bn = tf.nn.relu(bn)
            pool = tf.layers.max_pooling2d(bn, pool_size=[2, 2], strides=2, padding='SAME', name=scope.name)

        # Classify
        with tf.variable_scope('fc') as scope:
            flat = tf.layers.flatten(pool)
            fc = tf.layers.dense(inputs=flat, units=32, activation=tf.nn.relu)
            softmax = tf.layers.dense(inputs=fc, units=self.num_classes, activation=tf.nn.softmax)

        return softmax

Build a simple convnet (BN before ReLU).

Args:
        inputs: a tensor of size [batch_size, height, width, channels]
        mode: string in ['train', 'test']

Returns:
        the last op containing the predictions

Note:
        Best score
        Step:  7015 - Epoch: 18/20 - best batch acc: 0.8984 - loss: 1.5656
        Worst score
        Step:  7523 - Epoch: 20/20 - best batch acc: 0.7734 - loss: 1.6874


def GaussianBlur(X, ksize_width, ksize_height, sigma_x, sigma_y):

    return image_transform(
        X,
        cv2.GaussianBlur,
        ksize=(ksize_width, ksize_height),
        sigmaX=sigma_x,
        sigmaY=sigma_y
    )

Apply Gaussian blur to the given data.

Args:
        X: data to blur
        kernel_size: Gaussian kernel size
        stddev: Gaussian kernel standard deviation (in both X and Y directions)


def delete_assets(self, service_id, url=None, all=False):

        self._services_manager.delete_assets(service_id, url, all)

Delete CDN assets

Args:
    service_id: The ID of the service to delete from.
    url: The URL at which to delete assets
    all: When True, delete all assets associated with the service_id.

    You cannot specifiy both url and all.


def stepBy(self, steps):

        self.setValue(self.value() + steps*self.singleStep())

steps value up/down by a single step. Single step is defined in singleStep().

Args:
        steps (int): positiv int steps up, negativ steps down


def write_temp_bird_conf(dummy_ip_prefix,
                         config_file,
                         variable_name,
                         prefixes):

    log = logging.getLogger(PROGRAM_NAME)
    comment = ("# {i} is a dummy IP Prefix. It should NOT be used and "
               "REMOVED from the constant.".format(i=dummy_ip_prefix))

    # the temporary file must be on the same filesystem as the bird config
    # as we use os.rename to perform an atomic update on the bird config.
    # Thus, we create it in the same directory that bird config is stored.
    tm_file = os.path.join(os.path.dirname(config_file), str(time.time()))
    log.debug("going to write to %s", tm_file)

    try:
        with open(tm_file, 'w') as tmpf:
            tmpf.write("# Generated {t} by {n} (pid={p})\n"
                       .format(t=datetime.datetime.now(),
                               n=PROGRAM_NAME,
                               p=os.getpid()))
            tmpf.write("{c}\n".format(c=comment))
            tmpf.write("define {n} =\n".format(n=variable_name))
            tmpf.write("{s}[\n".format(s=4 * ' '))
            # all entries of the array need a trailing comma except the last
            # one. A single element array doesn't need a trailing comma.
            tmpf.write(',\n'.join([' '*8 + n for n in prefixes]))
            tmpf.write("\n{s}];\n".format(s=4 * ' '))
    except OSError as error:
        log.critical("failed to write temporary file %s: %s. This is a FATAL "
                     "error, this exiting main program", tm_file, error)
        sys.exit(1)
    else:
        return tm_file

Write in a temporary file the list of IP-Prefixes.

    A failure to create and write the temporary file will exit main program.

Args:
        dummy_ip_prefix (str): The dummy IP prefix, which must be always
        config_file (str): The file name of bird configuration
        variable_name (str): The name of the variable set in bird configuration
        prefixes (list): The list of IP-Prefixes to write

Returns:
        The filename of the temporary file


def b_fit_score(self, x, y):

        x = np.reshape(minmax_scale(x), (-1, 1))
        y = np.reshape(minmax_scale(y), (-1, 1))
        poly = PolynomialFeatures(degree=self.degree)
        poly_x = poly.fit_transform(x)

        poly_x[:,1] = 0
        poly_x[:,2] = 0

        regressor = LinearRegression()
        regressor.fit(poly_x, y)

        y_predict = regressor.predict(poly_x)
        error = mean_squared_error(y_predict, y)

        return error

Compute the RECI fit score

Args:
        x (numpy.ndarray): Variable 1
        y (numpy.ndarray): Variable 2

Returns:
        float: RECI fit score


def _convert_pandas_csv_options(pandas_options, columns):

    _columns = pandas_options.pop('names', columns)
    header = pandas_options.pop('header', None)
    pandas_options.pop('encoding', None)

    if header == 'infer':
        header_line_number = 0 if not bool(_columns) else None
    else:
        header_line_number = header

    return _columns, header_line_number

Translate `pd.read_csv()` options into `pd.DataFrame()` especially for header.

Args:
        pandas_option (dict):
            pandas options like {'header': None}.
        columns (list):
            list of column name.


def impersonate(self, user, enterprise):

        if not user or not enterprise:
            raise ValueError('You must set a user name and an enterprise name to begin impersonification')

        self._is_impersonating = True
        self._impersonation = "%s@%s" % (user, enterprise)

Impersonate a user in a enterprise

Args:
        user: the name of the user to impersonate
        enterprise: the name of the enterprise where to use impersonation


def __init__(self, source_urn=None, token=None):

    super(InstantOutputPlugin, self).__init__()

    if not source_urn:
      raise ValueError("source_urn can't be empty.")

    if not token:
      raise ValueError("token can't be empty.")

    self.source_urn = source_urn
    self.token = token

OutputPlugin constructor.

Args:
        source_urn: URN identifying source of the data (hunt or flow).
        token: Security token.

Raises:
        ValueError: If one of the keyword arguments is empty.


def enable_store_parameters_in_results(kernel):

  kernel_stack = []
  while hasattr(kernel, 'parameters') and 'inner_kernel' in kernel.parameters:
    kernel_stack.append(kernel)
    kernel = kernel.parameters['inner_kernel']

  def _recreate_kernel(kernel, parameters):
    new_parameters = kernel.parameters.copy()
    new_parameters.update(parameters)
    if 'store_parameters_in_results' in new_parameters:
      new_parameters['store_parameters_in_results'] = True
    with deprecation.silence():
      return type(kernel)(**new_parameters)

  if hasattr(kernel, 'parameters'):
    kernel = _recreate_kernel(kernel, {})

  for outer_kernel in reversed(kernel_stack):
    outer_kernel = _recreate_kernel(outer_kernel, {'inner_kernel': kernel})
    kernel = outer_kernel

  return kernel

Enables the `store_parameters_in_results` parameter in a chain of kernels.

    This is a temporary utility for use during the transition period of the
    parameter storage methods.

Args:
        kernel: A TransitionKernel.

Returns:
        kernel: The same kernel, but recreated with `store_parameters_in_results`
        recursively set to `True` in its parameters and its inner kernels (as
        appropriate).


def result_to_dict(raw_result):

    result = {}

    for channel_index, channel in enumerate(raw_result):
        channel_id, channel_name = channel[0], channel[1]
        channel_result = {
            'id': channel_id,
            'name': channel_name,
            'movies': []
        }
        for movie in channel[2]:
            channel_result['movies'].append({
                'title': movie[1],
                'start_time': datetime.fromtimestamp(movie[2]),
                'end_time': datetime.fromtimestamp(movie[2] + movie[3]),
                'inf': True if movie[3] else False,
            })
        result[channel_id] = channel_result

    return result

Parse raw result from fetcher into readable dictionary

Args:
        raw_result (dict) - raw data from `fetcher`

Returns:
        dict - readable dictionary


def add_document(self, key, url, **kwargs):

        document = self._check_metadata_for_file(key=key, url=url, **kwargs)

        for dict_key in (
            'description',
            'fulltext',
            'hidden',
            'material',
            'original_url',
            'url',
            'filename',
        ):
            if kwargs.get(dict_key):
                document[dict_key] = kwargs[dict_key]

        if key_already_there(document, self.record.get('documents', ())):
            raise ValueError(
                'There\'s already a document with the key %s.'
                % document['key']
            )

        self._append_to('documents', document)

Adds document to record

Args:
        key (string): document key
        url (string): document url
    Keyword Args:
        description (string): simple description
        fulltext (bool): mark if this is a full text
        hidden (bool): is document should be hidden
        material (string):
        original_url (string): original url
        filename (string): current url

    Returns: None


def ListAttrs(cls):

  precondition.AssertType(cls, type)

  if PY2:
    # TODO(user): once https://github.com/google/pytype/issues/127 is fixed,
    # pytype should be able to tell that this line is unreachable in py3.
    return [item.decode("ascii") for item in dir(cls)]  # pytype: disable=attribute-error
  else:
    return dir(cls)

A compatibility wrapper for listing class attributes.

    This method solves similar Python 2 compatibility issues for `dir` function as
    `GetName` does for `__name__` invocations. See documentation for `GetName` for
    more details.

    Once support for Python 2 is dropped all invocations of this function should
    be replaced with ordinary `dir` calls.

Args:
        cls: A class object to list the attributes for.

Returns:
        A list of attribute names as unicode strings.


def changeTo(self, path):

        dictionary = DictSingle(Pair('PATH', StringSingle(path)))
        self.value = [dictionary]

change value

Args:
        path (str): the new environment path


def _is_autocomplete_valid(cur_commands, alias_command):

    parent_command = ' '.join(cur_commands[1:])
    with open(GLOBAL_ALIAS_TAB_COMP_TABLE_PATH, 'r') as tab_completion_table_file:
        try:
            tab_completion_table = json.loads(tab_completion_table_file.read())
            return alias_command in tab_completion_table and parent_command in tab_completion_table[alias_command]
        except Exception:  # pylint: disable=broad-except
            return False

Determine whether autocomplete can be performed at the current state.

Args:
        parser: The current CLI parser.
        cur_commands: The current commands typed in the console.
        alias_command: The alias command.

Returns:
        True if autocomplete can be performed.


def __init__(self, entities=[], metadata={}):

        self.entities = entities
        self.metadata = metadata

Initialize ContextManagerFrame

Args:
        entities(list): List of Entities...
        metadata(object): metadata to describe context?


def _PrintAnalysisReportsDetails(self, storage_reader):

    if not storage_reader.HasAnalysisReports():
      self._output_writer.Write('No analysis reports stored.\n\n')
      return

    for index, analysis_report in enumerate(
        storage_reader.GetAnalysisReports()):
      title = 'Analysis report: {0:d}'.format(index)
      table_view = views.ViewsFactory.GetTableView(
          self._views_format_type, title=title)

      table_view.AddRow(['String', analysis_report.GetString()])

      table_view.Write(self._output_writer)

Prints the details of the analysis reports.

Args:
        storage_reader (StorageReader): storage reader.


def get_destination(self, filepath, targetdir=None):

        dst = self.change_extension(filepath, 'css')
        if targetdir:
            dst = os.path.join(targetdir, dst)
        return dst

Return destination path from given source file path.

    Destination is allways a file with extension ``.css``.

Args:
        filepath (str): A file path. The path is allways relative to
            sources directory. If not relative, ``targetdir`` won't be
            joined.
        absolute (bool): If given will be added at beginning of file
            path.

Returns:
        str: Destination filepath.


def schedule_saved(sender, instance, **kwargs):

    from contentstore.tasks import sync_schedule

    sync_schedule.delay(str(instance.id))

Fires off the celery task to ensure that this schedule is in the scheduler

Args:
        sender {class} -- The model class, always Schedule
        instance {Schedule} --
            The instance of the Schedule that we want to sync


def reverse_transform(self, col):

        output = pd.DataFrame()
        new_name = '?' + self.col_name

        col.loc[col[new_name] == 0, self.col_name] = np.nan
        output[self.col_name] = col[self.col_name]
        return output

Converts data back into original format.

Args:
        col(pandas.DataFrame): Data to transform.

Returns:
        pandas.DataFrame


def dump_in_memory_result(self, result, output_path):

        file_count = 0
        logger.debug("Dumping in-memory processing results to output folder: %s", output_path)
        for k, v in iteritems(result):
            cur_output_path = os.path.join(output_path, k)

            if isinstance(v, dict):
                file_count += self.dump_in_memory_result(v, cur_output_path)
            else:
                if not os.path.isdir(output_path):
                    os.makedirs(output_path)

                filename = os.path.join(output_path, k)
                logger.debug("Writing output file: %s", filename)
                # dump the contents of the file
                with open(filename, 'wt', encoding=self.config.encoding) as f:
                    f.write(v)

                file_count += 1

        return file_count

Recursively dumps the result of our processing into files within the
    given output path.

Args:
        result: The in-memory result of our processing.
        output_path: Full path to the folder into which to dump the files.

Returns:
        The number of files generated (integer).


def defocus_blur(x, severity=1):

  c = [(3, 0.1), (4, 0.5), (6, 0.5), (8, 0.5), (10, 0.5)][severity - 1]
  x = np.array(x) / 255.
  kernel = disk(radius=c[0], alias_blur=c[1])
  channels = []
  for d in range(3):
    channels.append(tfds.core.lazy_imports.cv2.filter2D(x[:, :, d], -1, kernel))
  channels = np.array(channels).transpose((1, 2, 0))  # 3x224x224 -> 224x224x3
  x_clip = np.clip(channels, 0, 1) * 255
  return around_and_astype(x_clip)

Defocus blurring to images.

    Apply defocus blurring to images using Gaussian kernel.

Args:
        x: numpy array, uncorrupted image, assumed to have uint8 pixel in [0,255].
        severity: integer, severity of corruption.

Returns:
        numpy array, image with uint8 pixels in [0,255]. Applied defocus blur.


def switch_to_window(self, window_name):

        data = {
            'name': window_name
        }
        self._execute(Command.SWITCH_TO_WINDOW, data)

Switch to the given window.

    Support:
        Web(WebView)

Args:
        window_name(str): The window to change focus to.

Returns:
        WebDriver Object.


def __setitem__(self, complete_selector, value):

    if not SELECTOR_RE.match(complete_selector):
      raise ValueError("Invalid selector '{}'.".format(complete_selector))

    selector_components = complete_selector.split('.')
    node = self._selector_tree

    # Iterate backwards over the components of the selector.
    for component in selector_components[::-1]:
      node = node.setdefault(component, {})
    node[_TERMINAL_KEY] = complete_selector
    self._selector_map[complete_selector] = value

Associates a value with `complete_selector`.

    This function also performs some additional bookkeeping to facilitate
    partial matching of selectors.

Args:
        complete_selector: The (complete) selector to associate a value with.
        value: The value to associate.

Raises:
        ValueError: If `complete_selector` isn't a string consisting of valid
        Python identifiers separated by periods.


def sils(T,f,c,d,h):

    model = Model("single item lotsizing")
    Ts = range(1,T+1)
    M = sum(d[t] for t in Ts)
    y,x,I = {},{},{}
    for t in Ts:
        y[t] = model.addVar(vtype="I", ub=1, name="y(%s)"%t)
        x[t] = model.addVar(vtype="C", ub=M, name="x(%s)"%t)
        I[t] = model.addVar(vtype="C", name="I(%s)"%t)
    I[0] = 0

    for t in Ts:
        model.addCons(x[t] <= M*y[t], "ConstrUB(%s)"%t)
        model.addCons(I[t-1] + x[t] == I[t] + d[t], "FlowCons(%s)"%t)

    model.setObjective(\
        quicksum(f[t]*y[t] + c[t]*x[t] + h[t]*I[t] for t in Ts),\
        "minimize")

    model.data = y,x,I
    return model

sils -- LP lotsizing for the single item lot sizing problem
    Parameters:
        - T: number of periods
        - P: set of products
        - f[t]: set-up costs (on period t)
        - c[t]: variable costs
        - d[t]: demand values
        - h[t]: holding costs
    Returns a model, ready to be solved.


def retry(exceptions, tries=5, delay=1, backoff=2, logger=None):

    def deco_retry(func):
        @wraps(func)
        async def f_retry(self, *args, **kwargs):
            if not iscoroutine(func):
                f = coroutine(func)
            else:
                f = func

            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return await f(self, *args, **kwargs)
                except exceptions:
                    if logger:
                        logger.info('Retrying %s after %s seconds', f.__name__, mdelay)
                    sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return await f(self, *args, **kwargs)

        return f_retry

    return deco_retry

Retry calling the decorated function using an exponential backoff.

Args:
        exceptions: The exception to check. may be a tuple of
            exceptions to check.
        tries: Number of times to try (not retry) before giving up.
        delay: Initial delay between retries in seconds.
        backoff: Backoff multiplier (e.g. value of 2 will double the delay
            each retry).
        logger: Logger to use. If None, print.


def _extract_dir(self, dir_not_exists, output):

    if not dir_not_exists:
        lst = output.dir_cache
        return {i["relpath"]: i["md5"] for i in lst}
    return {}

Extract the content of dvc tree file

Args:
        self(object) - Repo class instance
        dir_not_exists(bool) - flag for directory existence
        output(object) - OutputLOCAL class instance

Returns:
        dict - dictionary with keys - paths to file in .dvc/cache
                            values -checksums for that files


def __openAndInitCSVFile(self, modelInfo):

    # Get the base path and figure out the path of the report file.
    basePath = self.__outputDirAbsPath

    # Form the name of the output csv file that will contain all the results
    reportCSVName = "%s_Report.csv" % (self.__outputLabel,)
    reportCSVPath = self.__reportCSVPath = os.path.join(basePath, reportCSVName)

    # If a report CSV file already exists, back it up
    backupCSVPath = None
    if os.path.exists(reportCSVPath):
      backupCSVPath = self.__backupCSVPath = _backupFile(reportCSVPath)

    # Open report file
    if self.__replaceReport:
      mode = "w"
    else:
      mode = "a"
    csv = self.__csvFileObj = open(reportCSVPath, mode)

    # If we are appending, add some blank line separators
    if not self.__replaceReport and backupCSVPath:
      print >> csv
      print >> csv

    # Print the column names
    print >> csv, "jobID, ",
    print >> csv, "modelID, ",
    print >> csv, "status, " ,
    print >> csv, "completionReason, ",
    print >> csv, "startTime, ",
    print >> csv, "endTime, ",
    print >> csv, "runtime(s), " ,
    print >> csv, "expDesc, ",
    print >> csv, "numRecords, ",

    for key in self.__sortedVariableNames:
      print >> csv, "%s, " % key,
    for key in self.__sortedMetricsKeys:
      print >> csv, "%s, " % key,
    print >> csv

- Backs up old report csv file;
    - opens the report csv file in append or overwrite mode (per
        self.__replaceReport);
    - emits column fields;
    - sets up self.__sortedVariableNames, self.__csvFileObj,
        self.__backupCSVPath, and self.__reportCSVPath

    Parameters:
    ----------------------------------------------------------------------
    modelInfo:      First _NupicModelInfo instance passed to emit()
    retval:         nothing


def save_config(config, logdir=None):

  if logdir:
    with config.unlocked:
      config.logdir = logdir
    message = 'Start a new run and write summaries and checkpoints to {}.'
    tf.logging.info(message.format(config.logdir))
    tf.gfile.MakeDirs(config.logdir)
    config_path = os.path.join(config.logdir, 'config.yaml')
    with tf.gfile.FastGFile(config_path, 'w') as file_:
      yaml.dump(config, file_, default_flow_style=False)
  else:
    message = (
        'Start a new run without storing summaries and checkpoints since no '
        'logging directory was specified.')
    tf.logging.info(message)
  return config

Save a new configuration by name.

    If a logging directory is specified, is will be created and the configuration
    will be stored there. Otherwise, a log message will be printed.

Args:
        config: Configuration object.
        logdir: Location for writing summaries and checkpoints if specified.

Returns:
        Configuration object.


def filter_prop_defs(prop_defs, hierarchy, cls_names):

    def _is_valid(test_list, valid_list):

        for test in test_list:
            if test in valid_list:
                return True
        return False

    new_dict = {}
    valid_classes = [Uri('kdr_AllClasses')] + cls_names + hierarchy
    for def_name, value in prop_defs.items():
        new_dict[def_name] = []
        empty_def = []
        try:
            for item in value:
                if item.get('kds_appliesToClass'):
                    if _is_valid(item['kds_appliesToClass'], valid_classes):
                        new_dict[def_name].append(item)
                else:
                    empty_def.append(item)
            if not new_dict[def_name]:
                new_dict[def_name] = empty_def
        except AttributeError:
            new_dict[def_name] = value
    return new_dict

Reads through the prop_defs and returns a dictionary filtered by the
    current class

Args:
    prop_defs: the defintions from the rdf vocabulary defintion
    cls_object: the class object to tie the property
    cls_names: the name of the classes


def itersplit_to_fields(str_,
                        fsep=DEFAULT_FSEP,
                        revtuple=None,
                        fields=[],
                        preparse=None):

    if preparse:
        str_ = preparse(str_)
    _fields = itersplit(str_, fsep)

    if revtuple is not None:
        try:
            values = (t[1] for t in izip_longest(revtuple._fields, _fields))
            return revtuple(*values)
        except Exception as e:
            log.error(revtuple)
            log.error(_fields)
            log.exception(e)
            raise

    return tuple(izip_longest(fields, _fields, fillvalue=None))

Itersplit a string into a (named, if specified) tuple.

Args:
        str_ (str): string to split
        fsep (str): field separator (delimiter to split by)
        revtuple (object): namedtuple (or class with a ``._fields`` attr)
            (optional)
        fields (list of str): field names (if revtuple is not specified)
        preparse (callable): function to parse str with before itersplitting

Returns:
        tuple or revtuple: fields as a tuple or revtuple, if specified


def create_rank_dicts(self, alt_scores):

        self.alts_to_ranks = dict()
        cur_score = max(alt_scores.values())
        cur_rank = 0
        self.ranks_to_alts = {cur_rank:[]}
        for i in sorted(alt_scores.keys(), key=lambda x: -alt_scores[x]):
            if alt_scores[i] == cur_score:
                self.ranks_to_alts[cur_rank].append(i)
            elif alt_scores[i] < cur_score:
                cur_rank += 1
                cur_score = alt_scores[i]
                self.ranks_to_alts[cur_rank] = [i]
            self.alts_to_ranks[i] = cur_rank

Description:
    Takes in the scores of the alternatives in the form alt:score and
    generates the dictionaries mapping alternatives to rankings and
    rankings to alternatives.
    Parameters:
    alt_scores: dictionary of the scores of every alternative


def update_model_in_repo_based_on_filename(self, model):

        if model._tx_filename is None:
            for fn in self.all_models.filename_to_model:
                if self.all_models.filename_to_model[fn] == model:
                    return fn
            i = 0
            while self.all_models.has_model("anonymous{}".format(i)):
                i += 1
            myfilename = "anonymous{}".format(i)
            self.all_models.filename_to_model[myfilename] = model
        else:
            myfilename = model._tx_filename
            if (not self.all_models.has_model(myfilename)):
                self.all_models.filename_to_model[myfilename] = model
        return myfilename

Adds a model to the repo (not initially visible)

Args:
        model: the model to be added. If the model
        has no filename, a name is invented
    Returns: the filename of the model added to the repo


def __validate(self, value, validate_element):

        if not self.repeated:
            return validate_element(value)
        else:
            # Must be a list or tuple, may not be a string.
            if isinstance(value, (list, tuple)):
                result = []
                for element in value:
                    if element is None:
                        try:
                            name = self.name
                        except AttributeError:
                            raise ValidationError(
                                'Repeated values for %s '
                                'may not be None' % self.__class__.__name__)
                        else:
                            raise ValidationError(
                                'Repeated values for field %s '
                                'may not be None' % name)
                    result.append(validate_element(element))
                return result
            elif value is not None:
                try:
                    name = self.name
                except AttributeError:
                    raise ValidationError('%s is repeated. Found: %s' % (
                        self.__class__.__name__, value))
                else:
                    raise ValidationError(
                        'Field %s is repeated. Found: %s' % (name, value))
        return value

Internal validation function.

    Validate an internal value using a function to validate
    individual elements.

Args:
        value: Value to validate.
        validate_element: Function to use to validate individual elements.

Raises:
        ValidationError if value is not expected type.


def layout(self, dimensions=None, **kwargs):

        return self.groupby(dimensions, container_type=NdLayout, **kwargs)

Groups data by supplied dimension(s) laying the groups along
    the dimension(s) out in a NdLayout.

Args:
    dimensions: Dimension/str or list
        Dimension or list of dimensions to group by

Returns:
    layout: NdLayout
        NdLayout with supplied dimensions


def try_to_create_directory(directory_path):

    logger = logging.getLogger("ray")
    directory_path = os.path.expanduser(directory_path)
    if not os.path.exists(directory_path):
        try:
            os.makedirs(directory_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise e
            logger.warning(
                "Attempted to create '{}', but the directory already "
                "exists.".format(directory_path))
        # Change the log directory permissions so others can use it. This is
        # important when multiple people are using the same machine.
    try:
        os.chmod(directory_path, 0o0777)
    except OSError as e:
        # Silently suppress the PermissionError that is thrown by the chmod.
        # This is done because the user attempting to change the permissions
        # on a directory may not own it. The chmod is attempted whether the
        # directory is new or not to avoid race conditions.
        # ray-project/ray/#3591
        if e.errno in [errno.EACCES, errno.EPERM]:
            pass
        else:
            raise

Attempt to create a directory that is globally readable/writable.

Args:
        directory_path: The path of the directory to create.


def _add_consequences(self, variant_obj):

        consequences = set()
        for transcript in variant_obj.transcripts:
            for consequence in transcript.consequence.split('&'):
                consequences.add(consequence)

        variant_obj.consequences = list(consequences)

Add the consequences found in all transcripts

Args:
        variant_obj (puzzle.models.Variant)


def put_many(self, type: Type[T], items: Iterable[T]) -> None:

        LOGGER.info("Getting SinkHandlers for \"{type}\"".format(type=type.__name__))
        try:
            handlers = self._put_types[type]
        except KeyError:
            try:
                LOGGER.info("Building new SinkHandlers for \"{type}\"".format(type=type.__name__))
                handlers = self._put_handlers(type)
            except NoConversionError:
                handlers = None
            self._get_types[type] = handlers

        LOGGER.info("Creating new PipelineContext")
        context = self._new_context()

        LOGGER.info("Sending items \"{items}\" to SourceHandlers".format(items=items))
        if handlers is not None:
            items = list(items)
            for handler in handlers:
                handler.put_many(items, context)

Puts multiple objects of the same type into the data sink. The objects may be transformed into a new type for insertion if necessary.

Args:
        items: An iterable (e.g. list) of objects to be inserted into the data pipeline.


def __init__(self, chgcar):

        self.chgcar = chgcar
        self.structure = chgcar.structure
        self.extrema_coords = []  # list of frac_coords of local extrema
        self.extrema_type = None  # "local maxima" or "local minima"
        self._extrema_df = None  # extrema frac_coords - chg density table
        self._charge_distribution_df = None

Initialization.

Args:
        chgcar (pmg.Chgcar): input Chgcar object.


def query_band(self, value):

        self._query_band = value
        # If query band is None we remove the element and don't write it to XML
        if value is None:
            try:
                del self._connectionXML.attrib['query-band-spec']
            except KeyError:
                pass
        else:
            self._connectionXML.set('query-band-spec', value)

Set the connection's query_band property.

Args:
        value:  New query_band value. String.

Returns:
        Nothing.


def new_result(self, loss, budget, parameters, update_model=True):

        if loss is None:
            # One could skip crashed results, but we decided
            # assign a +inf loss and count them as bad configurations
            loss = np.inf

        if budget not in self.configs.keys():
            self.configs[budget] = []
            self.losses[budget] = []

        # skip model building if we already have a bigger model
        if max(list(self.kde_models.keys()) + [-np.inf]) > budget:
            return

        # We want to get a numerical representation of the configuration in the original space
        conf = ConfigSpace.Configuration(self.configspace, parameters)
        self.configs[budget].append(conf.get_array())
        self.losses[budget].append(loss)

        # skip model building:
        # a) if not enough points are available
        if len(self.configs[budget]) <= self.min_points_in_model - 1:
            logger.debug("Only %i run(s) for budget %f available, need more than %s \
            -> can't build model!"%(len(self.configs[budget]), budget, self.min_points_in_model+1))
            return
        # b) during warnm starting when we feed previous results in and only update once
        if not update_model:
            return

        train_configs = np.array(self.configs[budget])
        train_losses = np.array(self.losses[budget])

        n_good = max(self.min_points_in_model, (self.top_n_percent * train_configs.shape[0])//100)
        n_bad = max(self.min_points_in_model, ((100-self.top_n_percent)*train_configs.shape[0])//100)

        # Refit KDE for the current budget
        idx = np.argsort(train_losses)

        train_data_good = self.impute_conditional_data(train_configs[idx[:n_good]])
        train_data_bad = self.impute_conditional_data(train_configs[idx[n_good:n_good+n_bad]])

        if train_data_good.shape[0] <= train_data_good.shape[1]:
            return
        if train_data_bad.shape[0] <= train_data_bad.shape[1]:
            return

        #more expensive crossvalidation method
        #bw_estimation = 'cv_ls'
        # quick rule of thumb
        bw_estimation = 'normal_reference'

        bad_kde = sm.nonparametric.KDEMultivariate(data=train_data_bad, var_type=self.kde_vartypes, bw=bw_estimation)
        good_kde = sm.nonparametric.KDEMultivariate(data=train_data_good, var_type=self.kde_vartypes, bw=bw_estimation)

        bad_kde.bw = np.clip(bad_kde.bw, self.min_bandwidth, None)
        good_kde.bw = np.clip(good_kde.bw, self.min_bandwidth, None)

        self.kde_models[budget] = {
            'good': good_kde,
            'bad' : bad_kde
        }

        # update probs for the categorical parameters for later sampling
        logger.debug('done building a new model for budget %f based on %i/%i split\nBest loss for this budget:%f\n'
                     %(budget, n_good, n_bad, np.min(train_losses)))

Function to register finished runs. Every time a run has finished, this function should be called
    to register it with the loss.

    Parameters:
    -----------
    loss: float
        the loss of the parameters
    budget: float
        the budget of the parameters
    parameters: dict
        the parameters of this trial
    update_model: bool
        whether use this parameter to update BP model

    Returns
    -------
    None


def _set_unique_id(self, json_response):

        self.unique_id = (
            json_response.get('md5')
            or json_response.get('sha1')
            or json_response.get('sha256')
            or ''
        )

Sets the unique_id provided a json response.

Args:
        json_response:


def from_string(cls, string_input):

        correlation_grid = {}
        Exc_DFT_option = {}
        COHSEX_options = {}
        GW_options = {}
        BSE_TDDFT_options = {}

        lines = string_input.strip().split("\n")

        # number of atoms and species
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        nat = toks[0]
        nsp = toks[1]
        # number of valence bands
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        nvbands = toks[0]

        # correlation_grid
        # number of points and spacing in eV for correlation grid
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        correlation_grid['n_grid'] = toks[0]
        correlation_grid['dE_grid'] = toks[1]

        # Exc DFT
        # relire=1 ou recalculer=0 Exc DFT
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        Exc_DFT_option['rdVxcpsi'] = toks[0]

        # COHSEX
        # number of COHSEX corrected occp and unoccp bands: C=COHSEX  H=HF
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        COHSEX_options['nv_cohsex'] = toks[0]
        COHSEX_options['nc_cohsex'] = toks[1]
        COHSEX_options['eigMethod'] = toks[2]
        # number of COHSEX iter, scf on wfns, mixing coeff; V=RI-V  I=RI-D
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        COHSEX_options['nit_cohsex'] = toks[0]
        COHSEX_options['resMethod'] = toks[1]
        COHSEX_options['scf_cohsex_wf'] = toks[2]
        COHSEX_options['mix_cohsex'] = toks[3]

        # GW
        # number of GW corrected occp and unoccp bands
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        GW_options['nv_corr'] = toks[0]
        GW_options['nc_corr'] = toks[1]
        # number of GW iterations
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        GW_options['nit_gw'] = toks[0]

        # BSE
        # dumping for BSE and TDDFT
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        BSE_TDDFT_options['do_bse'] = toks[0]
        BSE_TDDFT_options['do_tddft'] = toks[1]
        # number of occp. and virtual bands fo BSE: nocore and up to 40 eVs
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        BSE_TDDFT_options['nv_bse'] = toks[0]
        BSE_TDDFT_options['nc_bse'] = toks[1]
        # number of excitations needed and number of iterations
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        BSE_TDDFT_options['npsi_bse'] = toks[0]
        BSE_TDDFT_options['nit_bse'] = toks[1]

        # Molecule
        # list of symbols in order
        lines.pop(0)
        atname = []
        i = int(nsp)
        while i != 0:
            l = lines.pop(0).strip()
            toks = l.split()
            atname.append(toks[0])
            i -= 1

        # scaling factor
        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        scale = toks[0]
        # atoms x,y,z cartesian .. will be multiplied by scale
        lines.pop(0)
        # Parse geometry
        species = []
        coords = []
        i = int(nat)
        while i != 0:
            l = lines.pop(0).strip()
            toks = l.split()
            coords.append([float(j) for j in toks[0:3]])
            species.append(atname[int(toks[3]) - 1])
            i -= 1

        mol = Molecule(species, coords)

        return FiestaInput(mol=mol, correlation_grid=correlation_grid,
                           Exc_DFT_option=Exc_DFT_option,
                           COHSEX_options=COHSEX_options,
                           GW_options=GW_options,
                           BSE_TDDFT_options=BSE_TDDFT_options)

Read an FiestaInput from a string. Currently tested to work with
    files generated from this class itself.

Args:
        string_input: string_input to parse.

Returns:
        FiestaInput object


def __init__(self, connect_func, max_size=10):

    self.connect_func = connect_func
    self.limiter = threading.BoundedSemaphore(max_size)
    self.idle_conns = []  # Atomic access only!!
    self.closed = False

Creates a ConnectionPool.

Args:
    connect_func: A closure which returns a new connection to the underlying
        database, i.e. a MySQLdb.Connection. Should raise or block if the
        database is unavailable.
    max_size: The maximum number of simultaneous connections.


def __add__(self, other):

        if not all(np.equal(self.energies, other.energies)):
            raise ValueError("Energies of both COHP are not compatible.")
        populations = {spin: self.populations[spin] + other.populations[spin]
                       for spin in self.cohp}
        if self.icohp is not None and other.icohp is not None:
            int_pop = {spin: self.icohp[spin] + other.icohp[spin]
                       for spin in self.icohp}
        else:
            if self.icohp is not None or other.icohp is not None:
                warnings.warn("One of the COHP objects does not contain "
                              "ICOHPs. Setting ICOHP to None.")
            int_pop = None
        return Cohp(self.efermi, self.energies, populations, icohp=int_pop)

Adds two COHP together. Checks that energy scales are the same.
    Otherwise, it raises a ValueError. It also adds ICOHP if present.
    If ICOHP is only present in one object, it displays a warning and
    will not add ICOHP.

Args:
        other: Another COHP object.

Returns:
        Sum of the two COHPs as a COHP object.


def GetAllPluginInformation(cls, show_all=True):

    results = []
    for plugin_class in iter(cls._plugin_classes.values()):
      plugin_object = plugin_class()
      if not show_all and not plugin_class.ENABLE_IN_EXTRACTION:
        continue

      # TODO: Use a specific description variable, not the docstring.
      doc_string, _, _ = plugin_class.__doc__.partition('\n')
      type_string = cls._PLUGIN_TYPE_STRINGS.get(plugin_object.plugin_type)
      information_tuple = (plugin_object.plugin_name, doc_string, type_string)
      results.append(information_tuple)

    return sorted(results)

Retrieves a list of the registered analysis plugins.

Args:
        show_all (Optional[bool]): True if all analysis plugin names should
        be listed.

Returns:
        list[tuple[str, str, str]]: the name, docstring and type string of each
        analysis plugin in alphabetical order.


def select(self, condition):

        return self.__class__([p for p in self if condition(p)])

Select only those pseudopotentials for which condition is True.
    Return new class:`PseudoTable` object.

Args:
        condition:
            Function that accepts a :class:`Pseudo` object and returns True or False.


def contains(self, value, equality_comparer=operator.eq):

        if self.closed():
            raise ValueError("Attempt to call contains() on a "
                             "closed Queryable.")

        if not is_callable(equality_comparer):
            raise TypeError("contains() parameter equality_comparer={0} is "
                "not callable".format(repr(equality_comparer)))

        if equality_comparer is operator.eq:
            return value in self._iterable

        for item in self:
            if equality_comparer(value, item):
                return True
        return False

Determines whether the sequence contains a particular value.

    Execution is immediate. Depending on the type of the sequence, all or
    none of the sequence may be consumed by this operation.

    Note: This method uses immediate execution.

Args:
        value: The value to test for membership of the sequence

Returns:
        True if value is in the sequence, otherwise False.

Raises:
        ValueError: If the Queryable has been closed.


def match(self, set_a, set_b):

        track_step_matches = [[] * len(set_a)]

        costs = self.cost_matrix(set_a, set_b)
        valid_costs = np.all(costs < 1, axis=2)
        set_a_matches, set_b_matches = np.where(valid_costs)
        s = 0
        track_pairings = pd.DataFrame(index=np.arange(costs.shape[0]),
                                      columns=["Track", "Step", "Time", "Matched", "Pairings"], dtype=object)
        set_b_info = []
        for trb, track_b in enumerate(set_b):
            for t, time in enumerate(track_b.times):
                set_b_info.append((trb, t))
        set_b_info_arr = np.array(set_b_info, dtype=int)
        for tr, track_a in enumerate(set_a):
            for t, time in enumerate(track_a.times):
                track_pairings.loc[s, ["Track", "Step", "Time"]] = [tr, t, time]
                track_pairings.loc[s, "Matched"] = 1 if np.count_nonzero(set_a_matches == s) > 0 else 0
                if track_pairings.loc[s, "Matched"] == 1:
                    track_pairings.loc[s, "Pairings"] = set_b_info_arr[set_b_matches[set_a_matches == s]]
                else:
                    track_pairings.loc[s, "Pairings"] = np.array([])
                s += 1
        return track_pairings

For each step in each track from set_a, identify all steps in all tracks from set_b that meet all
    cost function criteria

Args:
        set_a: List of STObjects
        set_b: List of STObjects

Returns:
        track_pairings: pandas.DataFrame


def QA_SU_save_future_day(engine, client=DATABASE):

    engine = select_save_engine(engine)
    engine.QA_SU_save_future_day(client=client)

save future_day

Args:
        engine {[type]} -- [description]

    Keyword Arguments:
        client {[type]} -- [description] (default: {DATABASE})


def compute_distance(a, b):

    # check simple cases first
    if not a:
        return len(b)
    if not b:
        return len(a)
    if a == b or str.lower(a) == str.lower(b):
        return 0

    # lowercase each string
    a = str.lower(a)
    b = str.lower(b)

    # create empty vectors to store costs
    vector_1 = [-1] * (len(b) + 1)
    vector_2 = [-1] * (len(b) + 1)

    # set default values
    for i in range(len(vector_1)):
        vector_1[i] = i

    # compute distance
    for i in range(len(a)):
        vector_2[0] = i + 1

        for j in range(len(b)):
            penalty = 0 if a[i] == b[j] else compute_qwerty_distance(a[i], b[j])
            vector_2[j + 1] = min(vector_2[j] + 1, vector_1[j + 1] + 1, vector_1[j] + penalty)

        for j in range(len(vector_1)):
            vector_1[j] = vector_2[j]

    return vector_2[len(b)]

Computes a modified Levenshtein distance between two strings, comparing the
    lowercase versions of each string and accounting for QWERTY distance.

Args:
        - a (str) String to compare to 'b'
        - b (str) String to compare to 'a'

Returns:
        - (int) Number representing closeness of 'a' and 'b' (lower is better)


def ismount(self, path):

        path = make_string_path(path)
        if not path:
            return False
        normed_path = self.filesystem.absnormpath(path)
        sep = self.filesystem._path_separator(path)
        if self.filesystem.is_windows_fs:
            if self.filesystem.alternative_path_separator is not None:
                path_seps = (
                    sep, self.filesystem._alternative_path_separator(path)
                )
            else:
                path_seps = (sep, )
            drive, rest = self.filesystem.splitdrive(normed_path)
            if drive and drive[:1] in path_seps:
                return (not rest) or (rest in path_seps)
            if rest in path_seps:
                return True
        for mount_point in self.filesystem.mount_points:
            if normed_path.rstrip(sep) == mount_point.rstrip(sep):
                return True
        return False

Return true if the given path is a mount point.

Args:
        path: Path to filesystem object to be checked

Returns:
        `True` if path is a mount point added to the fake file system.
        Under Windows also returns True for drive and UNC roots
        (independent of their existence).


def get_customer(self, customer_id):

        return self.client._get(self.url + 'customers/{}'.format(customer_id), headers=self.get_headers())

Queries the information related to the customer.

Args:
        customer_id: Identifier of the client from which you want to find the associated information.

    Returns:


def _compute_equations(self, x, verbose=False):

        # compute the error and the normals.
        normals = []
        values = []
        signs = []
        error = 0.0
        if verbose:
            print()
            print(' '.join('% 10.3e' % val for val in x), end=' ')
            active_str = ''
        for i, (sign, equation) in enumerate(self.equations):
            value, normal = equation(x)
            if (i < len(self.lock) and self.lock[i]) or \
               (sign==-1 and value > -self.threshold) or \
               (sign==0) or (sign==1 and value < self.threshold):
                values.append(value)
                normals.append(normal)
                signs.append(sign)
                error += value**2
                if verbose:
                    active_str += 'X'
                if i < len(self.lock):
                    self.lock[i] = True
            elif verbose:
                active_str += '-'
        error = np.sqrt(error)
        normals = np.array(normals, float)
        values = np.array(values, float)
        signs = np.array(signs, int)
        if verbose:
            print('[%s]' % active_str, end=' ')
            if error < self.threshold:
                print('OK')
            else:
                print('%.5e' % error)
        return normals, values, error, signs

Compute the values and the normals (gradients) of active constraints.

Args:
    | ``x`` -- The unknowns.


def merge_corpus(self, corpus):

        # Create a copy, so objects aren't changed in the original merging corpus
        merging_corpus = Corpus.from_corpus(corpus)

        self.import_tracks(corpus.tracks.values())
        self.import_issuers(corpus.issuers.values())
        utterance_idx_mapping = self.import_utterances(corpus.utterances.values())

        for subview_idx, subview in merging_corpus.subviews.items():
            for filter in subview.filter_criteria:
                if isinstance(filter, subset.MatchingUtteranceIdxFilter):
                    new_filtered_utt_ids = set()
                    for utt_idx in filter.utterance_idxs:
                        new_filtered_utt_ids.add(utterance_idx_mapping[utt_idx].idx)
                    filter.utterance_idxs = new_filtered_utt_ids

            new_idx = naming.index_name_if_in_list(subview_idx, self.subviews.keys())
            self.import_subview(new_idx, subview)

        for feat_container_idx, feat_container in merging_corpus.feature_containers.items():
            self.new_feature_container(feat_container_idx, feat_container.path)

Merge the given corpus into this corpus. All assets (tracks, utterances, issuers, ...) are copied into
    this corpus. If any ids (utt-idx, track-idx, issuer-idx, subview-idx, ...) are occurring in both corpora,
    the ids from the merging corpus are suffixed by a number (starting from 1 until no other is matching).

Args:
        corpus (CorpusView): The corpus to merge.


def __call__(self, data):

        if isinstance(data, dict):
            # convert each value in dict from a numpy array to a list if necessary, so they can be json serialized
            return json.dumps({k: _ndarray_to_list(v) for k, v in six.iteritems(data)})

        # files and buffers
        if hasattr(data, 'read'):
            return _json_serialize_from_buffer(data)

        return json.dumps(_ndarray_to_list(data))

Take data of various formats and serialize them into the expected request body.
    This uses information about supported input formats for the deployed model.

Args:
        data (object): Data to be serialized.

Returns:
        object: Serialized data used for the request.


def clean_output_files(self, follow_parents=True):

        paths = []
        if self.status != self.S_OK:
            logger.warning("Calling task.clean_output_files on a task whose status != S_OK")

        # Remove all files in tmpdir.
        self.tmpdir.clean()

        # Find the file extensions that should be preserved since these files are still
        # needed by the children who haven't reached S_OK
        except_exts = set()
        for child in self.get_children():
            if child.status == self.S_OK: continue
            # Find the position of self in child.deps and add the extensions.
            i = [dep.node for dep in child.deps].index(self)
            except_exts.update(child.deps[i].exts)

        # Remove the files in the outdir of the task but keep except_exts.
        exts = self.gc.exts.difference(except_exts)
        #print("Will remove its extensions: ", exts)
        paths += self.outdir.remove_exts(exts)
        if not follow_parents: return paths

        # Remove the files in the outdir of my parents if all the possible dependencies have been fulfilled.
        for parent in self.get_parents():

            # Here we build a dictionary file extension --> list of child nodes requiring this file from parent
            # e.g {"WFK": [node1, node2]}
            ext2nodes = collections.defaultdict(list)
            for child in parent.get_children():
                if child.status == child.S_OK: continue
                i = [d.node for d in child.deps].index(parent)
                for ext in child.deps[i].exts:
                    ext2nodes[ext].append(child)

            # Remove extension only if no node depends on it!
            except_exts = [k for k, lst in ext2nodes.items() if lst]
            exts = self.gc.exts.difference(except_exts)
            #print("%s removes extensions %s from parent node %s" % (self, exts, parent))
            paths += parent.outdir.remove_exts(exts)

        self.history.info("Removed files: %s" % paths)
        return paths

This method is called when the task reaches S_OK. It removes all the output files
    produced by the task that are not needed by its children as well as the output files
    produced by its parents if no other node needs them.

Args:
        follow_parents: If true, the output files of the parents nodes will be removed if possible.

Returns:
        list with the absolute paths of the files that have been removed.


def BreachDepressions(
  dem,
  in_place = False,
  topology = 'D8'
):

  if type(dem) is not rdarray:
    raise Exception("A richdem.rdarray or numpy.ndarray is required!")

  if topology not in ['D8','D4']:
    raise Exception("Unknown topology!")

  if not in_place:
    dem = dem.copy()

  _AddAnalysis(dem, "BreachDepressions(dem)")

  demw = dem.wrap()

  if topology=='D8':
    _richdem.rdBreachDepressionsD8(demw)
  elif topology=='D4':
    _richdem.rdBreachDepressionsD4(demw)

  dem.copyFromWrapped(demw)

  if not in_place:
    return dem

Breaches all depressions in a DEM.

Args:
        dem     (rdarray): An elevation model
        in_place (bool):   If True, the DEM is modified in place and there is
                        no return; otherwise, a new, altered DEM is returned.
        topology (string): A topology indicator

Returns:
        DEM without depressions.


def pad_blank_lines(indent_texts):

    current_indent = 0
    result = []
    for indent, text in indent_texts:
        if len(text) > 0:
            current_indent = indent
        result.append((current_indent, text))
    return result

Give blank (empty) lines the same indent level as the preceding line.

Args:
        indent_texts: An iterable of tuples each containing an integer in the
            first element and a string in the second element.

Returns:
        A list of tuples each containing an integer in the first element and a
        string in the second element.


def parse_cl_function(cl_code, dependencies=()):

    from mot.lib.cl_function import SimpleCLFunction

    def separate_cl_functions(input_str):

        class Semantics:

            def __init__(self):
                self._functions = []

            def result(self, ast):
                return self._functions

            def arglist(self, ast):
                return '({})'.format(', '.join(ast))

            def function(self, ast):
                def join(items):
                    result = ''
                    for item in items:
                        if isinstance(item, str):
                            result += item
                        else:
                            result += join(item)
                    return result

                self._functions.append(join(ast).strip())
                return ast

        return _extract_cl_functions_parser.parse(input_str, semantics=Semantics())

    functions = separate_cl_functions(cl_code)
    return SimpleCLFunction.from_string(functions[-1], dependencies=list(dependencies or []) + [
        SimpleCLFunction.from_string(s) for s in functions[:-1]])

Parse the given OpenCL string to a single SimpleCLFunction.

    If the string contains more than one function, we will return only the last, with all the other added as a
    dependency.

Args:
        cl_code (str): the input string containing one or more functions.
        dependencies (Iterable[CLCodeObject]): The list of CL libraries this function depends on

Returns:
        mot.lib.cl_function.SimpleCLFunction: the CL function for the last function in the given strings.


def __init__(self, context):

    self._event_multiplexer = context.multiplexer
    self._logdir = context.logdir
    self._debugger_data_server = None
    self._grpc_port = None

Constructs a debugger plugin for TensorBoard.

    This plugin adds handlers for retrieving debugger-related data. The plugin
    also starts a debugger data server once the log directory is passed to the
    plugin via the call to get_plugin_apps.

Args:
        context: A base_plugin.TBContext instance.


def generate_poisson_states(n_states, n_cells, n_genes):

    W = np.random.dirichlet([1]*n_states, size=(n_cells,))
    W = W.T
    M = np.random.random((n_genes, n_states))*100
    return M, W

Generates means and weights for the Poisson Convex Mixture Model.
    Weights are distributed Dirichlet(1,1,...), means are rand(0, 100).
    Returned values can be passed to generate_state_data(M, W).

Args:
        n_states (int): number of states or clusters
        n_cells (int): number of cells
        n_genes (int): number of genes

Returns:
        M - genes x clusters
        W - clusters x cells


def _load_from_file_object(self, f):

    subtoken_strings = []
    for line in f:
      s = line.strip()
      # Some vocab files wrap words in single quotes, but others don't
      if ((s.startswith("'") and s.endswith("'")) or
          (s.startswith("\"") and s.endswith("\""))):
        s = s[1:-1]
      subtoken_strings.append(native_to_unicode(s))
    self._init_subtokens_from_list(subtoken_strings)
    self._init_alphabet_from_tokens(subtoken_strings)

Load from a file object.

Args:
        f: File object to load vocabulary from


def make_fixture(model_class, **kwargs):

    all_fields = get_fields(model_class)

    fields_for_random_generation = map(
        lambda x: getattr(model_class, x), all_fields
    )

    overrides = {}

    for kwarg, value in kwargs.items():
        if kwarg in all_fields:
            kwarg_field = getattr(model_class, kwarg)
            fields_for_random_generation.remove(kwarg_field)
            overrides.update({kwarg_field: value})

    random_values = get_random_values(fields_for_random_generation)

    values = dict(overrides, **random_values)

    assert len(all_fields) == len(values), (
        "Mismatch in values, {} != {}".format(
            len(all_fields), len(values)
        )
    )
    data = {k.name: v for k, v in values.items()}
    return model_class(**data)

Take the model_klass and generate a fixure for it

Args:
        model_class (MongoEngine Document): model for which a fixture
            is needed
        kwargs (dict): any overrides instead of random values

Returns:
        dict for now, other fixture types are not implemented yet


def remove_indirect_links(g, alg="aracne", **kwargs):

    alg = {"aracne": aracne,
           "nd": network_deconvolution,
           "clr": clr}[alg]
    mat = np.array(nx.adjacency_matrix(g).todense())
    return nx.relabel_nodes(nx.DiGraph(alg(mat, **kwargs)),
                            {idx: i for idx, i in enumerate(list(g.nodes()))})

Apply deconvolution to a networkx graph.

Args:
        g (networkx.Graph): Graph to apply deconvolution to
        alg (str): Algorithm to use ('aracne', 'clr', 'nd')
        kwargs (dict): extra options for algorithms

Returns:
        networkx.Graph: graph with undirected links removed.


def publish(self, channel, message, pipeline=False):

        if pipeline:
            self._pipeline.publish(channel, message)
        else:
            self._db.publish(channel, message)

Post a message to a given channel.

Args:
        channel (str): Channel where the message will be published
        message (str): Message to publish
        pipeline (bool): True, start a transaction block. Default false.


def delete_repo(self, repo_name=None, force=False, all=False):

        if not all:
            if repo_name:
                req = proto.DeleteRepoRequest(repo=proto.Repo(name=repo_name), force=force)
                self.stub.DeleteRepo(req, metadata=self.metadata)
            else:
                raise ValueError("Either a repo_name or all=True needs to be provided")
        else:
            if not repo_name:
                req = proto.DeleteRepoRequest(force=force, all=all)
                self.stub.DeleteRepo(req, metadata=self.metadata)
            else:
                raise ValueError("Cannot specify a repo_name if all=True")

Deletes a repo and reclaims the storage space it was using.

    Params:
    * repo_name: The name of the repo.
    * force: If set to true, the repo will be removed regardless of
    errors. This argument should be used with care.
    * all: Delete all repos.


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

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

Returns rows that appear in both DataFrames.

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

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


def __init__(self, broker, queue_output, backend=None,
                 max_tasks_in_memory=None, max_workers_in_memory=None):

        self._app = Celery(broker=broker, backend=backend)
        self._queue_output = queue_output

        from celery.backends.base import DisabledBackend
        self._use_result_backend = not isinstance(self._app.backend, DisabledBackend)

        logger.info('Creating %s: max_tasks=%d; max_workers=%d',
                    EventListener.__name__, max_tasks_in_memory, max_workers_in_memory)
        logger.info('Celery broker=%s; backend=%s; using_result_backend=%s',
                    broker, backend, self._use_result_backend)

        # events handling: storage and filling missing states.
        self.memory = State(
            max_tasks_in_memory=max_tasks_in_memory,
            max_workers_in_memory=max_workers_in_memory,
        )  # type: State

        # running engine (should be asyncio in the future)
        self._listener_thread = None  # type:threading.Thread
        self._celery_receiver = None  # type:EventReceiver

        # concurrency control
        self._wait_event = threading.Event()

        # detect shutdown.
        def sigterm_handler(_signo, _stack_frame):  # pragma: no cover
            self.__stop()

        signal.signal(signal.SIGTERM, sigterm_handler)
        self.__start()

Constructs an event listener instance.

Args:
        broker (str): the broker being used by the celery system.
        queue_output (Queue): to send to streaming dispatcher.
        backend (str): the result backend being used by the celery system.
        max_tasks_in_memory (int): max tasks stored
        max_workers_in_memory (int): max workers stored


def __call__(self, obj, **cfg):

        depth = self.config.depth or 0
        seen_on_path = self.config.seen_on_path or frozenset()
        cfg.setdefault('depth', depth + 1)
        cfg['seen_on_path'] = seen_on_path | {id(obj)}
        h = self.with_config(cfg)
        max_depth = h.config.max_depth

        if h.config.preprocess:
            obj = h.config.preprocess(obj, hrepr)

        if id(obj) in seen_on_path:
            # This object is a child of itself, so we display a neat
            # little loop to avoid busting the stack.
            result = self.H.span['hrepr-circular']('⥁')
        elif max_depth is not None and depth >= max_depth:
            result = h._hrepr(obj, self.type_handlers_short,
                              ['__hrepr_short__'], self.stdrepr_short)
        else:
            result = h._hrepr(obj, self.type_handlers,
                              ['__hrepr__', '__hrepr_short__'],
                              self.stdrepr)

        if h.config.postprocess:
            return h.config.postprocess(obj, result, h.H, h) or result
        else:
            return result

Return the HTML representation of an object.

Args:
        obj: The object to represent
        cfg: Configuration to add to the current
            configuration for this operation.

Returns:
        The representation of the object.


def ApprovalRevokeRaw(aff4_path, token):

  try:
    urn = rdf_client.ClientURN(aff4_path)
  except type_info.TypeValueError:
    urn = rdfvalue.RDFURN(aff4_path)

  approval_urn = aff4.ROOT_URN.Add("ACL").Add(urn.Path()).Add(
      token.username).Add(utils.EncodeReasonString(token.reason))

  super_token = access_control.ACLToken(username="raw-approval-superuser")
  super_token.supervisor = True

  approval_request = aff4.FACTORY.Open(
      approval_urn, mode="rw", token=super_token)
  approval_request.DeleteAttribute(approval_request.Schema.APPROVER)
  approval_request.Close()

Revokes an approval for a given token.

    This method requires raw datastore access to manipulate approvals directly.

Args:
        aff4_path: The aff4_path or client id the approval should be created for.
        token: The token that should be revoked.


def save_tiles(tiles, prefix='', directory=os.getcwd(), format='png'):

#    Causes problems in CLI script.
#    if not os.path.exists(directory):
#        os.makedirs(directory)
    for tile in tiles:
        tile.save(filename=tile.generate_filename(prefix=prefix,
                                                  directory=directory,
                                                  format=format),
                                                  format=format)
    return tuple(tiles)

Write image files to disk. Create specified folder(s) if they
    don't exist. Return list of :class:`Tile` instance.

Args:
    tiles (list):  List, tuple or set of :class:`Tile` objects to save.
    prefix (str):  Filename prefix of saved tiles.

    Kwargs:
    directory (str):  Directory to save tiles. Created if non-existant.

Returns:
    Tuple of :class:`Tile` instances.


def __init__(
      self, location=None, parent=None, volume_index=None, **kwargs):

    if not parent:
      raise ValueError('Missing parent value.')

    super(APFSContainerPathSpec, self).__init__(parent=parent, **kwargs)
    self.location = location
    self.volume_index = volume_index

Initializes a path specification.

    Note that an APFS container path specification must have a parent.

Args:
        location (Optional[str]): location.
        parent (Optional[PathSpec]): parent path specification.
        volume_index (Optional[int]): index of the volume within the container.

Raises:
        ValueError: when parent is not set.


def read_structs(fstream):

    struct = read_struct(fstream)
    while struct is not None:
        yield struct
        struct = read_struct(fstream)

Read all structs from likwid's file stream.

Args:
        fstream: Likwid's output file stream.

Returns:
        A generator that can be used to iterate over all structs in the
        fstream.


def Parse(text, message):

  if not isinstance(text, six.text_type): text = text.decode('utf-8')
  try:
    if sys.version_info < (2, 7):
      # object_pair_hook is not supported before python2.7
      js = json.loads(text)
    else:
      js = json.loads(text, object_pairs_hook=_DuplicateChecker)
  except ValueError as e:
    raise ParseError('Failed to load JSON: {0}.'.format(str(e)))
  _ConvertMessage(js, message)
  return message

Parses a JSON representation of a protocol message into a message.

Args:
        text: Message JSON representation.
        message: A protocol beffer message to merge into.

Returns:
        The same message passed as argument.

    Raises::
        ParseError: On JSON parsing problems.


def getRowByIndex(self, index):

        assert isinstance(index, int)
        return Row(self._impl.getRowByIndex(index))

Get row by numeric index.

Args:
        index: Zero-based index of the row to get.

Returns:
        The corresponding row.