Esempio n. 1
0
def statisticOfDistanceMatrix(distanceMatrix):
    """
    statisticOfDistanceMatrix: Determines max, mean and deviation of a distance matrix
    @param distanceMatrix: Must be a square matrix!
    @return: [max,mean,std] of distanceMatrix
    @author: Thomas Hrabe
    """
    from pytom_volume import vol, max
    from pytom.tools.maths import listMean, listStd

    if not distanceMatrix.__class__ == vol:
        raise TypeError('Parameter must be a pytom_volume.vol!')

    if distanceMatrix.sizeZ() > 1:
        raise RuntimeError('Parameter must be a 2D pytom_volume.vol!')

    if distanceMatrix.sizeX() != distanceMatrix.sizeY():
        raise RuntimeError('Matrix must be a square! Size x != y')

    values = []

    for i in range(distanceMatrix.sizeX()):
        if i < distanceMatrix.sizeX():
            for j in range(i + 1, distanceMatrix.sizeY()):
                values.append(distanceMatrix(i, j, 0))

    m = listMean(values)
    s = listStd(values, m)

    return [max(distanceMatrix), m, s]
Esempio n. 2
0
def calculate_difference_map(v1,
                             band1,
                             v2,
                             band2,
                             mask=None,
                             focus_mask=None,
                             align=True,
                             sigma=None,
                             threshold=0.4):
    """mask if for alignment, while focus_mask is for difference map.
    """
    from pytom_volume import vol, power, abs, limit, transformSpline, variance, mean, max, min
    from pytom.basic.normalise import mean0std1
    from pytom.basic.filter import lowpassFilter

    # do lowpass filtering first
    lv1 = lowpassFilter(v1, band1, band1 / 10.)[0]
    lv2 = lowpassFilter(v2, band2, band2 / 10.)[0]

    # do alignment of two volumes, if required. v1 is used as reference.
    if align:
        from sh_alignment.frm import frm_align
        band = int(band1 if band1 < band2 else band2)
        pos, angle, score = frm_align(lv2, None, lv1, None, [4, 64], band,
                                      lv1.sizeX() // 4, mask)
        shift = [
            pos[0] - v1.sizeX() // 2, pos[1] - v1.sizeY() // 2,
            pos[2] - v1.sizeZ() // 2
        ]

        # transform v2
        lvv2 = vol(lv2)
        transformSpline(lv2, lvv2, -angle[1], -angle[0], -angle[2],
                        lv2.sizeX() // 2,
                        lv2.sizeY() // 2,
                        lv2.sizeZ() // 2, -shift[0], -shift[1], -shift[2], 0,
                        0, 0)
    else:
        lvv2 = lv2

    # do normalization
    mean0std1(lv1)
    mean0std1(lvv2)

    # only consider the density beyond certain sigma
    if sigma is None or sigma == 0:
        pass
    elif sigma < 0:  # negative density counts
        assert min(lv1) < sigma
        assert min(lvv2) < sigma
        limit(lv1, 0, 0, sigma, 0, False, True)
        limit(lvv2, 0, 0, sigma, 0, False, True)
    else:  # positive density counts
        assert max(lv1) > sigma
        assert max(lvv2) > sigma
        limit(lv1, sigma, 0, 0, 0, True, False)
        limit(lvv2, sigma, 0, 0, 0, True, False)

    # if we want to focus on specific area only
    if focus_mask:
        lv1 *= focus_mask
        lvv2 *= focus_mask

    # calculate the STD map
    avg = (lv1 + lvv2) / 2
    var1 = avg - lv1
    power(var1, 2)
    var2 = avg - lvv2
    power(var2, 2)

    std_map = var1 + var2
    power(std_map, 0.5)

    # calculate the coefficient of variance map
    # std_map = std_map/abs(avg)

    if focus_mask:
        std_map *= focus_mask

    # threshold the STD map
    mv = mean(std_map)
    threshold = mv + (max(std_map) - mv) * threshold
    limit(std_map, threshold, 0, threshold, 1, True, True)

    # do a lowpass filtering
    std_map1 = lowpassFilter(std_map, v1.sizeX() // 4, v1.sizeX() / 40.)[0]

    if align:
        std_map2 = vol(std_map)
        transformSpline(std_map1, std_map2, angle[0], angle[1], angle[2],
                        v1.sizeX() // 2,
                        v1.sizeY() // 2,
                        v1.sizeZ() // 2, 0, 0, 0, shift[0], shift[1], shift[2])
    else:
        std_map2 = std_map1

    limit(std_map1, 0.5, 0, 1, 1, True, True)
    limit(std_map2, 0.5, 0, 1, 1, True, True)

    # return the respective difference maps
    return (std_map1, std_map2)
Esempio n. 3
0
 def setUpper(self, value=None):
     if value:
         self.upper = value
     else:
         from pytom_volume import max
         self.upper = max(self.vol)
Esempio n. 4
0
def _display(volume, sliceNumber=0, projectionAxis='z'):
    """
    _display: Generate image for volume display
    @author: Thomas Hrabe
    @param volume: The image / volume
    @param sliceNumber: Slice number at which the volume will be cut
    @param projectionAxis: Defines the axis. Default is z, means you look on the xy plane. (x equals to 'yz' plane, y to 'xz') 
    """
    import Image
    from pytom_volume import min, max, mean, variance, limit, subvolume
    from math import sqrt

    if projectionAxis == 'x':
        size1 = volume.sizeY()
        size2 = volume.sizeZ()
    if projectionAxis == 'y':
        size1 = volume.sizeX()
        size2 = volume.sizeZ()
    if projectionAxis == 'z':
        size1 = volume.sizeX()
        size2 = volume.sizeY()

    img = Image.new('L', (size1, size2))

    if sliceNumber > 0:

        if projectionAxis == 'x':
            volume = subvolume(volume, sliceNumber, 0, 0, 1, size1, size2)
        elif projectionAxis == 'y':
            volume = subvolume(volume, 0, sliceNumber, 0, size1, 1, size2)
        elif projectionAxis == 'z':
            volume = subvolume(volume, 0, 0, sliceNumber, size1, size2, 1)

    elif sliceNumber == 0 and volume.sizeZ() > 1:
        sliceNumber = int(volume.sizeZ() / 2)

        if projectionAxis == 'x':
            volume = subvolume(volume, sliceNumber, 0, 0, 1, size1, size2)
        elif projectionAxis == 'y':
            volume = subvolume(volume, 0, sliceNumber, 0, size1, 1, size2)
        elif projectionAxis == 'z':
            volume = subvolume(volume, 0, 0, sliceNumber, size1, size2, 1)

    minValue = min(volume)
    volume.shiftscale(-minValue, 1)
    maxValue = max(volume)
    if maxValue == 0:
        maxValue = 1
    volume.shiftscale(0, 255.0 / maxValue)

    for i in range(size1):
        for j in range(size2):
            if projectionAxis == 'x':
                value = volume(0, i, j)
            elif projectionAxis == 'y':
                value = volume(i, 0, j)
            elif projectionAxis == 'z':
                value = volume(i, j, 0)

            img.putpixel((i, j), int(value))

    return img
Esempio n. 5
0
def _dspcub(volume, sigma=None, projectionAxis='z'):
    """
    _dspcub: Creates a dspcub image
    dspcub2PNG: display z-slices in 2D image
    @param volume: The input volume
    @type volume: L{pytom_volume.vol}
    @parameter sigma: thresholding as multiples of standard deviation sigma
    @type sigma: float
    @param projectionAxis: Defines the axis. Default is z, means you look on the xy plane. (x equals to 'yz' plane, y to 'xz') 
    @return: Image
    @rtype: Image
    @@author: Pia Unverdorben 
    """

    if volume.sizeZ() == 1:
        raise Exception('You can use ')

    import Image
    from pytom_volume import min, max, mean, variance, limit, subvolume
    from math import sqrt, ceil, floor

    sizeX = volume.sizeX()
    sizeY = volume.sizeY()
    sizeZ = volume.sizeZ()

    if projectionAxis == 'x':
        imagesPerRow = int(floor(sqrt(sizeX)))
        size1 = float(sizeX)
        sizeI = sizeY
        sizeJ = sizeZ
    elif projectionAxis == 'y':
        imagesPerRow = int(floor(sqrt(sizeY)))
        size1 = float(sizeY)
        sizeI = sizeX
        sizeJ = sizeZ
    elif projectionAxis == 'z':
        imagesPerRow = int(floor(sqrt(sizeZ)))
        size1 = float(sizeZ)
        sizeI = sizeX
        sizeJ = sizeY

    numberIterations = imagesPerRow * imagesPerRow

    if size1 < numberIterations:
        iterationSteps = float(numberIterations / size1)
    else:
        iterationSteps = float(size1 / numberIterations)

    iterationSteps = int(iterationSteps)

    if projectionAxis == 'x':
        img = Image.new('L', (sizeY * imagesPerRow, sizeZ * imagesPerRow))
    elif projectionAxis == 'y':
        img = Image.new('L', (sizeX * imagesPerRow, sizeZ * imagesPerRow))
    elif projectionAxis == 'z':
        img = Image.new('L', (sizeX * imagesPerRow, sizeY * imagesPerRow))

    # normalize according to standard deviation if sigma is specified
    if sigma:
        meanv = mean(volume)
        stdv = sqrt(variance(volume, False))
        minValue = meanv - float(sigma) * stdv
        maxValue = meanv + float(sigma) * stdv
    else:
        minValue = min(volume)
        maxValue = max(volume)

    for sliceNumber in range(0, numberIterations, iterationSteps):

        if projectionAxis == 'x':
            png = Image.new('L', (sizeY, sizeZ))
        elif projectionAxis == 'y':
            png = Image.new('L', (sizeX, sizeZ))
        elif projectionAxis == 'z':
            png = Image.new('L', (sizeX, sizeY))

        (yvalue, xvalue) = divmod(sliceNumber, imagesPerRow)

        if projectionAxis == 'x':
            vol = subvolume(volume, sliceNumber, 0, 0, 1, sizeY, sizeZ)
        elif projectionAxis == 'y':
            vol = subvolume(volume, 0, sliceNumber, 0, sizeX, 1, sizeZ)
        elif projectionAxis == 'z':
            vol = subvolume(volume, 0, 0, sliceNumber, sizeX, sizeY, 1)

        vol.shiftscale(-minValue, 1)
        vol.shiftscale(0, 255 / maxValue)

        for i in range(sizeI):
            for j in range(sizeJ):

                if projectionAxis == 'x':
                    value = vol(0, i, j)
                elif projectionAxis == 'y':
                    value = vol(i, 0, j)
                elif projectionAxis == 'z':
                    value = vol(i, j, 0)

                png.putpixel((i, j), int(value))

        img.paste(png, (xvalue * sizeI, yvalue * sizeJ))

    return img