Exemplo n.º 1
0
    def iter_colonies(self,
                      filter_for_colonies='from_fset',
                      size=None,
                      shuffle=False):
        """Iterates through (already constructed colonies).

        Parameters
        ----------
        filt : filters.trees.FilterTree instance
           only colonies valid under filt will be yielded
        size : int
           maximal number of colonies before stopping iteration
        shuffle : bool
        """
        if filter_for_colonies is 'from_fset':
            colony_filter = self.exp.fset.colony_filter
        elif filter_for_colonies is None or filter_for_colonies is 'none':
            colony_filter = FilterTRUE()
        elif isinstance(filter_for_colonies, FilterTree):
            colony_filter = filter_for_colonies
        elif isinstance(filter_for_colonies, FilterTRUE):
            colony_filter = filter_for_colonies
        else:
            raise ValueError('"filter_for_colonies" parameter not recognized')
        trees = self.trees[:]
        if shuffle:
            random.shuffle(trees)
        count = 0
        for colony in trees:
            if size is not None and count > size - 1:
                break
            if colony_filter(colony):
                count += 1
                yield colony
        return
Exemplo n.º 2
0
    def iter_lineages(self,
                      filter_for_lineages='from_fset',
                      size=None,
                      shuffle=False):
        """Iterate through valid lineages.

        Parameters
        ----------
        filter_for_lineages : FilterLineage instance or str {'from_fset', 'none'}
            filter lineages
        size : int (default None)
            limit the number of lineages to size. Works only in mode='all'
        shuffle : bool (default False)
            whether to shuffle the ordering of lineages when mode='all'

        Yields
        ------
        lineage : :class:`Lineage` instance
            filtering removed outlier cells, containers, colonies, and lineages
        """
        if filter_for_lineages is 'from_fset':
            lineage_filter = self.fset.lineage_filter
        elif filter_for_lineages is None or filter_for_lineages is 'none':
            lineage_filter = FilterTRUE()
        elif isinstance(filter_for_lineages, FilterLineage):
            lineage_filter = filter_for_lineages
        elif isinstance(filter_for_lineages, FilterTRUE):
            lineage_filter = filter_for_lineages
        else:
            raise ValueError('"filter_for_lineages" parameter not recognized')


#        lin_filt = self.fset.lineage_filter
        if size is not None:
            count = 0
            for colony in self.iter_colonies(shuffle=shuffle):
                for lineage in colony.iter_lineages(
                        filter_for_lineages=lineage_filter, shuffle=shuffle):
                    yield lineage
                    count += 1
                    if count >= size:
                        break
                if count >= size:
                    break
        else:
            for colony in self.iter_colonies(shuffle=shuffle):
                for lineage in colony.iter_lineages(
                        filter_for_lineages=lineage_filter, shuffle=shuffle):
                    yield lineage
        return
Exemplo n.º 3
0
    def iter_lineages(self, independent=True, seed=None,
                      filter_for_lineages='from_fset', size=None, shuffle=False):
        """Iterates through lineages using tree decomposition
        
        When a decomposition has already been performed, call to this method
        will check decomposition parameters(`independent`, `seed`): if they
        are the same, the previous cell sequences are used identically,
        otherwise a new decomposition is computed.
        
        Parameters
        ----------
        independent : bool {True, False}
            whether to use independent decomposition or not (independent:
                intersection of any couple of sequences of cell is empty, or
                a given cell belongs to a unique sequence in the decomposition)
        seed : int (default None)
            use a specified seed to compare results
        filter_for_lineages : FilterLineage instance
        size : int
            iterate up to that number of lineages
        shuffle: bool {False, True}
            shuffle the sequence of lineages

        Yields
        ------
        Lineage instance
        
        See also
        --------
        decompose : tree decomposition
        """
        new = True  # new decomposition
        if self._decomposition is not None:
            a = self._decomposition['independent'] == independent
            b = self._decomposition['seed'] == seed
            if a and b:
                new = False
        if new:
            idseqs = self.decompose(independent=independent, seed=seed)
        else:
            idseqs = self.idseqs[:]
        if shuffle:
            np.random.shuffle(idseqs)  # not sure it is useful...
        if filter_for_lineages is 'from_fset':
            lineage_filter = self.container.exp.fset.lineage_filter
        elif filter_for_lineages is None or filter_for_lineages is 'none':
            lineage_filter = FilterTRUE()
        elif isinstance(filter_for_lineages, FilterLineage):
            lineage_filter = filter_for_lineages
        elif isinstance(filter_for_lineages, FilterTRUE):
            lineage_filter = filter_for_lineages
        else:
            raise ValueError('"filter_for_lineages" parameter not recognized')

        count = 0
        for idseq in idseqs:
            if size is not None and count > size - 1:
                break
            lin = Lineage(self, idseq)
            if lineage_filter(lin):
                count += 1
                yield lin
Exemplo n.º 4
0
    def iter_containers(self, read=True, build=True,
                        filter_for_cells='from_fset',
                        filter_for_containers='from_fset',
                        apply_container_filter=True,
                        extend_observables=False, report_NaNs=True,
                        size=None, shuffle=False):
        """Iterator over containers.

        Parameters
        ----------
        size : int (default None)
            number of containers to be parsed
        read : bool (default True)
            whether to read data and extract Cell instances
        build : bool (default True), called only if `read` is True
            whether to build colonies
        filter_for_cells : FilterCell instance, or str {'from_fset', 'none'}
            filter applied to cells when data files are parsed
        filter_for_containers : FilterContainer instance or str {'from_fset', 'none'}
            filter applied to containers when data files are parsed
        extend_observables : bool (default False)
            whether to construct secondary observables from raw data
        report_NaNs : bool (default True)
            whether to report for NaNs found in data
        shuffle : bool (default False)
            when `size` is set to a number, whether to randomize ordering of
            upcoming containers

        Returns
        -------
        iterator iver Container instances of current Experiment instance.
        """
        if filter_for_cells is 'from_fset':
            cell_filter = self.fset.cell_filter
        elif filter_for_cells is None or filter_for_cells is 'none':
            cell_filter = FilterTRUE()
        elif isinstance(filter_for_cells, FilterCell):
            cell_filter = filter_for_cells
        else:
            raise ValueError('"filter_for_cells" parameter not recognized')
        if filter_for_containers is 'from_fset':
            container_filter = self.fset.container_filter
        elif filter_for_containers is None or filter_for_containers is 'none':
            container_filter = FilterTRUE()
        elif isinstance(filter_for_containers, FilterContainer):
            container_filter = filter_for_containers
        containers = self.containers[:]
        if shuffle:
            random.shuffle(containers)
        if size is None:
            size = len(self.containers)
        for index, path in enumerate(containers, start=1):
            if index > size:
                break
            container = Container(path, exp=self)
            if read:
                container.read_data(build=build, prefilt=cell_filter,
                                    extend_observables=extend_observables,
                                    report_NaNs=report_NaNs)
            # apply filtering on containers
            if container_filter(container):
                yield container
        return