Python itertools.filterfalse() Examples

The following are 30 code examples of itertools.filterfalse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module itertools , or try the search function .
Example #1
Source File: alb.py    From Particle-Cloud-Framework with Apache License 2.0 6 votes vote down vote up
def _need_update(self, curr_list, desired_list):
        """
        Checks to see if there are any difference in curr_list or desired_list. If they are different this returns True.

        Args:
            curr_list (list): list of dictionaries
            desired_list (list): list of dictionaries

        Returns:
            bool
        """
        #Checks if items need to be added or removed.
        add = list(itertools.filterfalse(lambda x: x in curr_list, desired_list))
        remove = list(itertools.filterfalse(lambda x: x in desired_list, curr_list))
        if add or remove:
            return True
        return False 
Example #2
Source File: utils.py    From virgin-media-hub3 with GNU General Public License v3.0 6 votes vote down vote up
def unique_everseen(iterable, key=None):
    """List unique elements, preserving order.

    Remember all elements ever seen

    >>> list(unique_everseen('AAAABBBCCDAABBB'))
    ['A', 'B', 'C', 'D']
    >>> list(unique_everseen('ABBCcAD', str.lower))
    ['A', 'B', 'C', 'D']

    """
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in itertools.filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element 
Example #3
Source File: lovasz_losses.py    From SegmenTron with Apache License 2.0 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #4
Source File: losses.py    From pytorch-template with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=True, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = filterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #5
Source File: lovasz.py    From catalyst with Apache License 2.0 6 votes vote down vote up
def mean(values, ignore_nan=False, empty=0):
    """
    Nanmean compatible with generators.
    """
    values = iter(values)
    if ignore_nan:
        values = ifilterfalse(isnan, values)
    try:
        n = 1
        acc = next(values)
    except StopIteration:
        if empty == "raise":
            raise ValueError("Empty mean")
        return empty
    for n, v in enumerate(values, 2):  # noqa: B007
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #6
Source File: lovasz_losses.py    From ext_portrait_segmentation with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #7
Source File: itertools.py    From QCElemental with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    # straight from the docs, https://docs.python.org/3/library/itertools.html#itertools-recipes
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in itertools.filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element 
Example #8
Source File: losses.py    From centerpose with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #9
Source File: elb.py    From Particle-Cloud-Framework with Apache License 2.0 6 votes vote down vote up
def _need_update(curr_list, desired_list):
    """
    Checks to see if there are any difference in curr_list or desired_list. If they are different this returns True.

    Args:
        curr_list (list): list of dictionaries
        desired_list (list): list of dictionaries

    Returns:
         bool
    """
    #Checks if items need to be added or removed.
    add = list(itertools.filterfalse(lambda x: x in curr_list, desired_list))
    remove = list(itertools.filterfalse(lambda x: x in desired_list, curr_list))
    if add or remove:
        return True
    return False 
Example #10
Source File: dynamodb_table.py    From Particle-Cloud-Framework with Apache License 2.0 6 votes vote down vote up
def _need_update(self, curr_list, desired_list):
        """
        Checks to see if there are any differences in curr_list or desired_list. If they are different True is returned.

        Args:
            curr_list (list): list of dictionaries
            desired_list (list): list of dictionaries

        Returns:
             bool
        """
        # Checks if items need to be added or removed.
        add = list(itertools.filterfalse(lambda x: x in curr_list, desired_list))
        remove = list(itertools.filterfalse(lambda x: x in desired_list, curr_list))
        if add or remove:
            return True
        return False 
Example #11
Source File: lovasz_losses.py    From Efficient-Segmentation-Networks with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #12
Source File: segment.py    From rich with MIT License 6 votes vote down vote up
def filter_control(
        cls, segments: Iterable["Segment"], is_control=False
    ) -> Iterable["Segment"]:
        """Filter segments by ``is_control`` attribute.

        Args:
            segments (Iterable[Segment]): An iterable of Segment instances.
            is_control (bool, optional): is_control flag to match in search.

        Returns:
            Iterable[Segment]: And iterable of Segment instances.
        
        """
        if is_control:
            return filter(attrgetter("is_control"), segments)
        else:
            return filterfalse(attrgetter("is_control"), segments) 
Example #13
Source File: base.py    From pennylane with Apache License 2.0 6 votes vote down vote up
def _op_descendants(self, op, only):
        """Descendants of the given operator in the quantum circuit.

        Args:
            op (Operator): operator in the quantum circuit
            only (str, None): the type of descendants to return.

                - ``'G'``: only return non-observables (default)
                - ``'O'``: only return observables
                - ``None``: return all descendants

        Returns:
            list[Operator]: descendants in a topological order
        """
        succ = self.circuit.descendants_in_order((op,))
        if only == "O":
            return list(filter(_is_observable, succ))
        if only == "G":
            return list(itertools.filterfalse(_is_observable, succ))
        return succ 
Example #14
Source File: utils.py    From LightNet with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = filterfalse(np.isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #15
Source File: lovasz.py    From pytorch-tools with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = filterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == "raise":
            raise ValueError("Empty mean")
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n


# --------------------------- Convinient classes --------------------------- 
Example #16
Source File: utils.py    From hangar-py with Apache License 2.0 6 votes vote down vote up
def unique_everseen(iterable, key=None):
    """List unique elements, preserving order. Remember all elements ever seen.

    >>> list(unique_everseen('AAAABBBCCDAABBB'))
    ['A', 'B', 'C', 'D']
    >>> list(unique_everseen('ABBCcAD', str.lower))
    ['A', 'B', 'C', 'D']
    """
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element 
Example #17
Source File: iterstuff.py    From vgraph with Apache License 2.0 6 votes vote down vote up
def unique_everseen(iterable, key=None):
    """List unique elements, preserving order. Remember all elements ever seen.

    >>> ''.join(unique_everseen('AAAABBBCCDAABBB'))
    'ABCD'
    >>> ''.join(unique_everseen('ABBCcAD', str.lower))
    'ABCD'

    """
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element 
Example #18
Source File: lovasz_losses.py    From PolarSeg with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #19
Source File: lovasz_loss.py    From kaggle-understanding-clouds with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #20
Source File: lovasz_losses.py    From pytorch_segmentation with MIT License 6 votes vote down vote up
def mean(l, ignore_nan=False, empty=0):
    """
    nanmean compatible with generators.
    """
    l = iter(l)
    if ignore_nan:
        l = ifilterfalse(isnan, l)
    try:
        n = 1
        acc = next(l)
    except StopIteration:
        if empty == 'raise':
            raise ValueError('Empty mean')
        return empty
    for n, v in enumerate(l, 2):
        acc += v
    if n == 1:
        return acc
    return acc / n 
Example #21
Source File: test_tools_sync200.py    From synapse with Apache License 2.0 5 votes vote down vote up
def _checkCore(self, core):
        with self.getRegrDir('assets', REGR_VER) as assetdir:
            podesj = getAssetJson(assetdir, 'splicepodes.json')
            podesj = [p for p in podesj if p[0] not in NOMIGR_NDEF]
            tpodes = s_common.tuplify(podesj)

            # check all nodes (removing empty nodedata key)
            nodes = await core.nodes('.created -meta:source:name=test')

            podes = [n.pack(dorepr=True) for n in nodes]
            podes = [(p[0], {k: v for k, v in p[1].items() if k != 'nodedata'}) for p in podes]
            self.gt(len(podes), 0)

            # handle the case where a tag with tagprops was deleted but tag:prop:del splices aren't generated
            for pode in podes:
                tags = pode[1]['tags'].keys()
                pode[1]['tagprops'] = {k: v for k, v in pode[1]['tagprops'].items() if k in tags}
                pode[1]['tagpropreprs'] = {k: v for k, v in pode[1]['tagpropreprs'].items() if k in tags}

            try:
                self.eq(podes, tpodes)
            except AssertionError:
                # print a more useful diff on error
                notincore = list(itertools.filterfalse(lambda x: x in podes, tpodes))
                notintest = list(itertools.filterfalse(lambda x: x in tpodes, podes))
                self.eq(notincore, notintest)  # should be empty, therefore equal
                raise

            # manually check node subset
            self.len(1, await core.nodes('inet:ipv4=1.2.3.4'))
            self.len(2, await core.nodes('inet:dns:a:ipv4=1.2.3.4')) 
Example #22
Source File: DocumentController.py    From nionswift with GNU General Public License v3.0 5 votes vote down vote up
def __change_graphics_role(self, role: typing.Optional[str]) -> bool:
        display_item = self.selected_display_item
        if display_item:
            if display_item.graphic_selection.has_selection:
                graphics = [display_item.graphics[index] for index in display_item.graphic_selection.indexes]
                graphics = itertools.filterfalse(lambda graphic: isinstance(graphic, (Graphics.SpotGraphic, Graphics.WedgeGraphic, Graphics.RingGraphic, Graphics.LatticeGraphic)), graphics)
                if graphics:
                    command = DisplayPanel.ChangeGraphicsCommand(self.document_model, display_item, graphics, command_id="change_role", is_mergeable=True, role=role)
                    command.perform()
                    self.push_undo_command(command)
                    return True
        return False 
Example #23
Source File: filecmp.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def _filter(flist, skip):
    return list(filterfalse(skip.__contains__, flist))


# Demonstration and testing.
# 
Example #24
Source File: find_extended_builds.py    From openshift-tools with Apache License 2.0 5 votes vote down vote up
def summarize_pods(project_name):
    pods = get_namespace_resource("pods", project_name)
    pods = itertools.filterfalse(lambda pod: pod['status']['phase'] != 'Running', pods)
    pods = list(pods)
    print("Number of pods: {0}".format(len(pods)))
    if pods:
        # for pod in pods:
        #     print("Pod:{0}".format(pod))
        cpu = functools.reduce(lambda resource, limit: resource + limit, map(
            lambda pod: normalize_cpu(pod['spec']['containers'][0]['resources']['limits']['cpu']), pods))
        print("Total CPU usage: {0}".format(cpu))

        memory = functools.reduce(lambda resource, limit: resource + limit, map(
            lambda pod: normalize_storage(pod['spec']['containers'][0]['resources']['limits']['memory']), pods))
        print("Total Memory usage: {0}".format(memory)) 
Example #25
Source File: misc.py    From eddy with GNU General Public License v3.0 5 votes vote down vote up
def partition(predicate, iterable):
    """
    Uses the given predicate to partition entries from the given iterable.
    :type predicate: callable
    :type iterable: iterable
    :rtype: tuple
    """
    t1, t2 = itertools.tee(iterable)
    return filter(predicate, t2), itertools.filterfalse(predicate, t1) 
Example #26
Source File: lstm_crf.py    From pytorch-crf with MIT License 5 votes vote down vote up
def get_trainable_params(self,
                             lrs: LRsType = None) -> List[Dict[str, Any]]:
        """Return list of trainable parameter groups."""
        out: List[Dict[str, Any]] = []

        # Add word-embedding parameters if those are trainable.
        if not self._freeze_embeddings:
            out.append({
                "params": self.word_embedding.parameters()
            })

        # Add the parameters from all ther layers modules.
        module_params = [
            self.char_feats_layer.parameters(),
            self.rnn.parameters(),
            self.rnn_to_crf.parameters(),
            self.crf.parameters()
        ]
        if self.sent_context_embedding:
            module_params.append(self.sent_context_embedding.parameters())
        out.append({
            "params": filterfalse(lambda p: not p.requires_grad, chain(*module_params))
        })

        # Optionally add learning rates directly to param groups as well.
        if lrs:
            for param_group, lr in zip(out, lrs):
                param_group["lr"] = lr

        return out 
Example #27
Source File: gitstatus.py    From denite-git with MIT License 5 votes vote down vote up
def __get_preview_window(self):
        return next(filterfalse(lambda x:
                                not x.options['previewwindow'],
                                self.vim.windows), None)

    # diff action 
Example #28
Source File: gitlog.py    From denite-git with MIT License 5 votes vote down vote up
def __get_preview_window(self):
        return next(filterfalse(lambda x:
                                not x.options['previewwindow'],
                                self.vim.windows), None) 
Example #29
Source File: follow_trace.py    From manticore with GNU Affero General Public License v3.0 5 votes vote down vote up
def _partition(pred, iterable):
    t1, t2 = itertools.tee(iterable)
    return list(itertools.filterfalse(pred, t1)), list(filter(pred, t2)) 
Example #30
Source File: utils.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def unique_everseen(iterable, key=None):
    """
    The generator to list unique elements, preserving the order. Remember all
    elements ever seen. This was taken from the itertools recipes.

    Args:
        iterable (:obj:`iter`): An iterable to process.
        key (:obj:`callable`): Optional function to run when checking
            elements (e.g., str.lower)

    Yields:
        The next unique element found.
    """

    seen = set()
    seen_add = seen.add

    if key is None:

        for element in filterfalse(seen.__contains__, iterable):

            seen_add(element)
            yield element

    else:

        for element in iterable:

            k = key(element)

            if k not in seen:

                seen_add(k)
                yield element