示例#1
0
def correlation_torque_axis(points, point_weights, center, data_array,
                            xyz_to_ijk_transform, about_mean):

    # TODO: Exclude points outside data.  Currently treated as zero values.
    import VolumeData as vd
    values, outside = vd.interpolate_volume_data(points,
                                                 xyz_to_ijk_transform,
                                                 data_array)
    gradients, outside = vd.interpolate_volume_gradient(points,
                                                        xyz_to_ijk_transform,
                                                        data_array)
    # t = (|v-vm|^2*sum(wi*rixDvi) - sum(wi*vi)*sum((vi-vm)*rixDvi)) / |w||v-vm|^3
    from numpy import dot
    wv = dot(point_weights, values)
    if about_mean:
        vm = values.sum() / len(values)
        values -= vm
    r = points - center
    from Matrix import cross_products
    rxg = cross_products(r, gradients)
    wrxg = dot(point_weights, rxg)
    vrxg = dot(values, rxg)
    values *= values
    v2 = values.sum()
    ta = v2*wrxg - wv*vrxg
    if v2 != 0:
        ta /= v2 ** 1.5         # Avoid overflow error normalizing ta.
    return ta
示例#2
0
def fit_points_old(fmap, threshold=None):

    f_m = fmap.data.full_matrix()
    size = list(f_m.shape)
    size.reverse()

    points = VolumeData.grid_indices(size, numpy.single)  # i,j,k indices
    _contour.affine_transform_vertices(points, fmap.data.ijk_to_xyz_transform)
    weights = numpy.ravel(f_m).astype(numpy.single)

    threshold = fmap.surface_levels[0]
    #threshold = .3 * max ( numpy.ravel(f_m).astype(numpy.single) )

    ge = numpy.greater_equal(weights, threshold)
    points = numpy.compress(ge, points, 0)
    weights = numpy.compress(ge, weights)
    nz = numpy.nonzero(weights)[0]

    if len(nz) < len(weights):
        points = numpy.take(points, nz, axis=0)
        weights = numpy.take(weights, nz, axis=0)

    #mass = numpy.sum(weights, dtype=numpy.single)
    #fmap.rotation_center = numpy.dot(weights,points) / mass

    if 1:
        print "FitPoints from %s with threshold %.4f, %d nonzero" % (
            fmap.name, threshold, len(nz))

    return points, weights
示例#3
0
def correlation_gradient_direction(points, point_weights, data_array,
                                   xyz_to_ijk_transform, about_mean):

    # TODO: Exclude points outside data.  Currently treated as zero values.
    import VolumeData as vd
    values, outside = vd.interpolate_volume_data(points,
                                                 xyz_to_ijk_transform,
                                                 data_array)
    gradients, outside = vd.interpolate_volume_gradient(points,
                                                        xyz_to_ijk_transform,
                                                        data_array)
    # g = (|v-vm|^2*sum(wi*vi,j) - sum(wi*vi)*sum((vi-vm)*vi,j)) / |w||v-vm|^3
    from numpy import dot
    wv = dot(point_weights, values)
    if about_mean:
        vm = values.sum() / len(values)
        values -= vm
    vg = dot(values, gradients)
    wg = dot(point_weights, gradients)
    values *= values
    v2 = values.sum()
    ga = v2*wg - wv*vg
    return ga
示例#4
0
def sphere_volume(n, r, noise = 1.0):

  from numpy import indices, single as floatc
  o = 0.5*(n-1)
  i = indices((n,n,n), floatc) - o
  a = (i[0,...]*i[0,...] + i[1,...]*i[1,...] + i[2,...]*i[2,...] < r*r).astype(floatc)

  if noise > 0:
    from numpy.random import normal
    a += normal(0, noise, a.shape)

  import VolumeData, VolumeViewer
  g = VolumeData.Array_Grid_Data(a, origin = (-o,-o,-o), step = (1,1,1),
                                 name = 'sphere')
  v = VolumeViewer.volume_from_grid_data(g)
  return v
示例#5
0
    def Icos2Map0(self):

        dmap = segmentation_map()
        if dmap == None:
            self.umsg("Select a map in Segment Map dialog")
            return

        mm = dmap.full_matrix()
        #m1 = numpy.zeros_like ( mm )

        # transform to index reference frame of ref_map
        f1 = dmap.data.ijk_to_xyz_transform

        nm = numpy.zeros_like(mm)

        minr, maxr = 300, 400
        pt = numpy.array([[0, 0, 0]], numpy.float32)
        p = pt[0]

        im, jm, km = dmap.data.size[0] / 2, dmap.data.size[
            1] / 2, dmap.data.size[2] / 2

        for i in range(dmap.data.size[0]):
            self.status("Masking %s %.1f->%.1f, %d/%d" %
                        (dmap.name, minr, maxr, i + 1, dmap.data.size[0]))
            di = abs(i - im) * dmap.data.step[0]

            for j in range(dmap.data.size[1]):
                dj = abs(j - jm) * dmap.data.step[1]

                for k in range(dmap.data.size[2]):
                    dk = abs(k - km) * dmap.data.step[2]
                    r = numpy.sqrt(di * di + dj * dj + dk * dk)
                    if dk >= minr and dk < maxr:
                        nm[k, j, i] = mm[k, j, i]

        ndata = VolumeData.Array_Grid_Data(nm, dmap.data.origin,
                                           dmap.data.step,
                                           dmap.data.cell_angles)
        try:
            nvg = VolumeViewer.volume.add_data_set(ndata, None)
        except:
            nvg = VolumeViewer.volume.volume_from_grid_data(ndata)
        nvg.name = dmap.name + "__%.0f--to--%.0f" % (minr, maxr)
示例#6
0
def read_hkl_map(path):

    # Sum values for h,k,l grid points
    f = open(path, 'r')
    m = {}
    c = 0
    while True:
        line = f.readline()

        if c % 1000000 == 0 and c > 0:
            print c
        c += 1
        if line == '':
            break
        fields = line.split()
        ijk = tuple(int(i) for i in fields[:3])
        v = float(fields[3])
        if ijk in m:
            m[ijk] += v
        else:
            m[ijk] = v
    f.close()

    # Find h,k,l bounds
    from numpy import array, int32, zeros, float32
    ijk = array(tuple(m.keys()), int32)
    ijk_min = ijk.min(axis=0)
    ijk_max = ijk.max(axis=0)
    print ijk_min, ijk_max

    # Create numpy array
    isz, jsz, ksz = ijk_max - ijk_min + 1
    a = zeros((ksz, jsz, isz), float32)
    imin, jmin, kmin = ijk_min
    for (i, j, k), v in m.items():
        a[k - kmin, j - jmin, i - imin] = v

    # Create volume model
    import VolumeData, VolumeViewer
    g = VolumeData.Array_Grid_Data(a, origin=tuple(ijk_min))
    v = VolumeViewer.volume_from_grid_data(g)
    from os.path import basename
    v.name = basename(path)
示例#7
0
    def Icos2Map0(self):

        smod = self.GetMod("Icosahedron Faces")
        if smod == None:
            self.umsg("No Icosahedron2 model found")
            return

        dmap = segmentation_map()
        if dmap == None:
            self.umsg("Select a map in Segment Map dialog")
            return

        sepRs = self.segRads.get().split(",")
        print "Sep rads:", sepRs

        if len(sepRs) != 2:
            self.umsg("Enter two radii separated by a comma")
            return

        try:
            start_rad = int(sepRs[0])
        except:
            self.umsg("Invalid start radius: " + sepRs[0])
            return

        try:
            end_rad = int(sepRs[1])
        except:
            self.umsg("Invalid end radius: " + sepRs[1])
            return

        if end_rad <= start_rad:
            self.umsg("End rad should be larger than start rad :) ")
            return

        self.umsg("Mask %s, %d -> %d" % (dmap.name, start_rad, end_rad))

        self.MakeTNorms(smod)

        import time
        start = time.time()

        mm = dmap.full_matrix()
        #m1 = numpy.zeros_like ( mm )

        # transform to index reference frame of ref_map
        f1 = dmap.data.ijk_to_xyz_transform

        from _contour import affine_transform_vertices as transform_vertices
        #f2 = xform_matrix ( mask_map.openState.xform )
        #f3 = xform_matrix ( ref_map.openState.xform.inverse() )
        #f4 = ref_map.data.xyz_to_ijk_transform
        #tf = multiply_matrices( f2, f1 )
        #tf = multiply_matrices( f3, tf )
        #tf = multiply_matrices( f4, tf )

        nm = numpy.zeros_like(mm)

        self.updateIcos2(start_rad)
        minr, maxr = self.MinRad2(smod), self.MaxRad2(smod)
        print " - start rad %d -- min rad %.1f, max rad %.1f" % (
            start_rad, numpy.sqrt(minr), numpy.sqrt(maxr))

        done = time.time()
        elapsed = done - start
        print "Took: ", elapsed

        pt = numpy.array([[0, 0, 0]], numpy.float32)
        p = pt[0]

        for i in range(dmap.data.size[0]):
            self.status("Masking %s, outside radius %d, %d/%d" %
                        (dmap.name, start_rad, i + 1, dmap.data.size[0]))
            p[0] = i * f1[0][0] + f1[0][3]
            for j in range(dmap.data.size[1]):
                p[1] = j * f1[1][1] + f1[1][3]
                for k in range(dmap.data.size[2]):
                    #p[2] = k * f1[2][2] + f1[2][3]
                    #pt = numpy.array ( [[i,j,k]], numpy.float32 )
                    #p[0],p[1],p[2] = ti,tj,tk
                    #transform_vertices ( pt, f1 )
                    p[2] = k * f1[2][2] + f1[2][3]
                    ptr = numpy.sum(p * p)
                    if ptr < minr:
                        pass
                    elif ptr > maxr:
                        nm[k, j, i] = mm[k, j, i]
                    elif self.PIsOutside(pt[0], smod):
                        nm[k, j, i] = mm[k, j, i]

        self.updateIcos2(end_rad)
        minr, maxr = self.MinRad2(smod), self.MaxRad2(smod)
        print " - end rad %d -- min rad %.1f, max rad %.1f" % (
            start_rad, numpy.sqrt(minr), numpy.sqrt(maxr))

        for i in range(dmap.data.size[0]):
            self.status("Masking %s, inside radius %d, %d/%d" %
                        (dmap.name, end_rad, i + 1, dmap.data.size[0]))
            p[0] = i * f1[0][0] + f1[0][3]
            for j in range(dmap.data.size[1]):
                p[1] = j * f1[1][1] + f1[1][3]
                for k in range(dmap.data.size[2]):
                    #pt = numpy.array ( [[i,j,k]], numpy.float32 )
                    #p[0],p[1],p[2] = ti,tj,tk
                    #transform_vertices ( pt, f1 )
                    p[2] = k * f1[2][2] + f1[2][3]
                    ptr = numpy.sum(p * p)
                    if ptr < minr:
                        continue
                    elif ptr > maxr:
                        nm[k, j, i] = 0.0
                    elif self.PIsOutside(p, smod):
                        nm[k, j, i] = 0.0

        ndata = VolumeData.Array_Grid_Data(nm, dmap.data.origin,
                                           dmap.data.step,
                                           dmap.data.cell_angles)
        try:
            nvg = VolumeViewer.volume.add_data_set(ndata, None)
        except:
            nvg = VolumeViewer.volume.volume_from_grid_data(ndata)
        nvg.name = dmap.name + "__%d--to--%d_fast" % (start_rad, end_rad)

        done = time.time()
        elapsed = done - start
        print "Took: ", elapsed