Esempio n. 1
0
    def setUp(self):
        self.lattice2D = _np.array([[1]]*4+[[2]]*4+[[0]], dtype=int).reshape(3,3)
        self.fullMask2D = _np.ones_like(self.lattice2D)
        self.indexes2D = lattice_indexes(_np.ones_like(self.lattice2D))

        flat3DLabels = [[1]]*5 + [[2]]*15 + [[0]]*20 + [[2]]*10 + [[0]]*20
        self.lattice3D = _np.array(flat3DLabels).reshape(2,5,7)
        self.indexes3D = lattice_indexes(_np.ones_like(self.lattice3D))
        self.fullMask3D = _np.ones_like(self.lattice3D)

        self.tmp_dir = tempfile.mkdtemp(prefix='pyhrf_tests',
                                        dir=pyhrf.cfg['global']['tmp_path'])
Esempio n. 2
0
    def setUp(self):
        self.lattice2D = _np.array([[1]] * 4 + [[2]] * 4 + [[0]],
                                   dtype=int).reshape(3, 3)
        self.fullMask2D = _np.ones_like(self.lattice2D)
        self.indexes2D = lattice_indexes(_np.ones_like(self.lattice2D))

        flat3DLabels = [[1]] * 5 + [[2]] * 15 + [[0]] * 20 + [[2]] * 10 + [[
            0
        ]] * 20
        self.lattice3D = _np.array(flat3DLabels).reshape(2, 5, 7)
        self.indexes3D = lattice_indexes(_np.ones_like(self.lattice3D))
        self.fullMask3D = _np.ones_like(self.lattice3D)

        self.tmp_dir = tempfile.mkdtemp(prefix='pyhrf_tests',
                                        dir=pyhrf.cfg['global']['tmp_path'])
Esempio n. 3
0
def graph_from_lattice(mask, kerMask=None, depth=1, toroidal=False):
    """
    Creates a graph from a n-dimensional lattice
    'mask' define valid positions to build the graph over.
    'kerMask' is numpy array mask (tuple of arrays) which defines the
    neighbourhood system, ie the relative positions of neighbours for a given
    position in the lattice.
    """
    # print 'size:', latticeIndexes.size
    if kerMask is not None:
        assert mask.ndim == len(kerMask)
    else:
        # Full neighbourhood, ie 8 in 2D, 26 in 3D ...
        ndim = mask.ndim
        neighbourCoords = list(cartesian(*[[0, -1, 1]] * ndim))[1:]
        kerMask = tuple(np.array(neighbourCoords, dtype=int).transpose())

    # loop over valid positions:
    # print mask.shape
    positions = mask_to_coords(mask)
    latticeIndexes = lattice_indexes(mask)
    # print positions
    # print positions.shape
    # Build lists of closest neighbours:
    closestNeighbours = np.empty(len(positions), dtype=object)
    for idx, pos in enumerate(positions):
        # print 'idx :', idx, '- pos:', pos
        # O(n) :
        m = center_mask_at(kerMask, pos, latticeIndexes, toroidal)
        # print 'm:', m
        closestNeighbours[idx] = latticeIndexes[m][latticeIndexes[m] >= 0]

    if depth == 1:
        return closestNeighbours

    neighboursSets = copyModule.deepcopy(closestNeighbours)

    for idx in xrange(nbPositions):
        # Starting positions = closest neighbours of current position
        neighboursToTreat = closestNeighbours[idx]
        visited = set([idx])
        # O(max( sum(lattice shape), kerMask size))*O(kerMask size)*O(d)
        for d in xrange(depth - 1):
            newNeighboursToTreat = set()
            # In the worst case, neighbours to treat are on the perimeter
            # of the lattice, say O(sum(lattice shape))
            # OR
            # If the lattice is kind of flat, the worst case is
            # O(size of kerMask)
            # So complexity is :
            # O(max( sum(lattice shape), kerMask size)) * O(kerMask size)
            for n in neighboursToTreat:
                # O(kerMask size)
                newNeighboursToTreat.update(closestNeighbours[n])
            # Remember what we just saw:
            visited.update(neighboursToTreat)
            # Update neighbours of current position and remove those
            # already seen:
            newNeighboursToTreat.difference_update(visited)
            neighboursSets[idx].update(newNeighboursToTreat)
            # Define new starting positions for next loop:
            neighboursToTreat = newNeighboursToTreat

    return neighboursSets
Esempio n. 4
0
File: graph.py Progetto: Solvi/pyhrf
def graph_from_lattice(mask, kerMask=None, depth=1, toroidal=False):
    """
    Creates a graph from a n-dimensional lattice
    'mask' define valid positions to build the graph over.
    'kerMask' is numpy array mask (tuple of arrays) which defines the
    neighbourhood system, ie the relative positions of neighbours for a given
    position in the lattice.
    """
    #print 'size:', latticeIndexes.size
    if kerMask is not None:
        assert  mask.ndim == len(kerMask)
    else:
        # Full neighbourhood, ie 8 in 2D, 26 in 3D ...
        ndim = mask.ndim
        neighbourCoords = list(cartesian(*[[0,-1,1]]*ndim))[1:]
        kerMask = tuple(np.array(neighbourCoords, dtype=int).transpose())

    # loop over valid positions:
    #print mask.shape
    positions = mask_to_coords(mask)
    latticeIndexes = lattice_indexes(mask)
    #print positions
    #print positions.shape
    # Build lists of closest neighbours:
    closestNeighbours = np.empty(len(positions), dtype=object)
    for idx, pos in enumerate(positions):
        #print 'idx :', idx, '- pos:', pos
        # O(n) :
        m = center_mask_at(kerMask, pos, latticeIndexes, toroidal)
        #print 'm:', m
        closestNeighbours[idx] = latticeIndexes[m][latticeIndexes[m]>=0]

    if depth == 1:
        return closestNeighbours

    neighboursSets = copyModule.deepcopy(closestNeighbours)

    for idx in xrange(nbPositions):
        # Starting positions = closest neighbours of current position
        neighboursToTreat = closestNeighbours[idx]
        visited = set([idx])
        # O(max( sum(lattice shape), kerMask size))*O(kerMask size)*O(d)
        for d in xrange(depth-1):
            newNeighboursToTreat = set()
            # In the worst case, neighbours to treat are on the perimeter
            # of the lattice, say O(sum(lattice shape))
            # OR
            # If the lattice is kind of flat, the worst case is
            # O(size of kerMask)
            # So complexity is :
            # O(max( sum(lattice shape), kerMask size)) * O(kerMask size)
            for n in neighboursToTreat:
                # O(kerMask size)
                newNeighboursToTreat.update(closestNeighbours[n])
            # Remember what we just saw:
            visited.update(neighboursToTreat)
            # Update neighbours of current position and remove those
            # already seen:
            newNeighboursToTreat.difference_update(visited)
            neighboursSets[idx].update(newNeighboursToTreat)
            # Define new starting positions for next loop:
            neighboursToTreat = newNeighboursToTreat

    return neighboursSets