示例#1
0
def findClusters(mlist_name, clist_name, eps, mc, ignore_z = True, ignore_category = True):
    
    # Load the data.
    pix_to_nm = 160.0

    i3_data_in = readinsight3.loadI3GoodOnly(mlist_name)

    c = i3_data_in['c']
    x = i3_data_in['xc']*pix_to_nm
    y = i3_data_in['yc']*pix_to_nm

    if ignore_z:
        print("Warning! Clustering without using localization z value!")
        z = numpy.zeros(x.size)
    else:
        z = i3_data_in['zc']

    # Perform analysis without regard to category.
    if ignore_category:
        print("Warning! Clustering without regard to category!")
        c = numpy.zeros(c.size)

    # Cluster the data.
    labels = dbscanC.dbscan(x, y, z, c, eps, mc, z_factor=1.0)

    # Save the data.    
    i3_data_out = writeinsight3.I3Writer(clist_name)
    i3dtype.setI3Field(i3_data_in, 'lk', labels)
    i3_data_out.addMolecules(i3_data_in)
    i3_data_out.close()
示例#2
0
def clusterStats(clist_name, min_size):

    # Load the data.
    pix_to_nm = 160.0

    i3_data_in = readinsight3.loadI3GoodOnly(clist_name)
    stats_fp = open(clist_name[:-8] + "stats.txt", "w")
    header = [
        "cluster",
        "cat",
        "size",
        "x-center(nm)",
        "y-center(nm)",
        "z-center(nm)",
        "size-x(nm)",
        "size-y(nm)",
        "size-z(nm)",
        "rg",
    ]
    stats_fp.write(" ".join(header) + "\n")

    # Remove category zero localizations.
    i3_data = i3dtype.maskData(i3_data_in, (i3_data_in["c"] != 0))

    # Calculate cluster stats.
    labels = i3_data["lk"]
    for k in set(labels):
        if k >= 2:
            mask = labels == k
            csize = mask.sum()
            if csize > min_size:
                x = pix_to_nm * i3_data["xc"][mask]
                y = pix_to_nm * i3_data["yc"][mask]
                z = i3_data["zc"][mask]
                c = i3_data["c"][mask]

                # Calculate size in x, y, z.
                sx = numpy.max(x) - numpy.min(x)
                sy = numpy.max(y) - numpy.min(y)
                sz = numpy.max(z) - numpy.min(z)

                # Calculate radius of gyration.
                cx = numpy.mean(x)
                cy = numpy.mean(y)

                rx = x - cx
                ry = y - cy

                if 0:
                    cz = numpy.mean(z)
                    rz = z - cz
                    rg = math.sqrt(numpy.sum(rx * rx + ry * ry + rz * rz) / float(x.size))
                else:
                    rg = math.sqrt(numpy.sum(rx * rx + ry * ry) / float(x.size))

                print("Cluster:", k, x.size, "localizations")
                stats = map(str, [k, c[0], csize, numpy.mean(x), numpy.mean(y), numpy.mean(z), sx, sy, sz, rg])
                stats_fp.write(" ".join(stats) + "\n")

    stats_fp.close()
示例#3
0
def findClusters(mlist_name, clist_name, eps, mc, ignore_z = True, ignore_category = True):
    
    # Load the data.
    pix_to_nm = 160.0

    i3_data_in = readinsight3.loadI3GoodOnly(mlist_name)

    c = i3_data_in['c']
    x = i3_data_in['xc']*pix_to_nm
    y = i3_data_in['yc']*pix_to_nm

    if ignore_z:
        print("Warning! Clustering without using localization z value!")
        z = numpy.zeros(x.size)
    else:
        z = i3_data_in['zc']

    # Perform analysis without regard to category.
    if ignore_category:
        print("Warning! Clustering without regard to category!")
        c = numpy.zeros(c.size)

    # Cluster the data.
    labels = dbscanC.dbscan(x, y, z, c, eps, mc, z_factor=1.0)

    # Save the data.    
    i3_data_out = writeinsight3.I3Writer(clist_name)
    i3dtype.setI3Field(i3_data_in, 'lk', labels)
    i3_data_out.addMolecules(i3_data_in)
    i3_data_out.close()
示例#4
0
def clusterStats(clist_name, min_size):
    
    # Load the data.
    pix_to_nm = 160.0
    
    i3_data_in = readinsight3.loadI3GoodOnly(clist_name)
    stats_fp = open(clist_name[:-8] + "stats.txt", "w")
    header = ["cluster", "cat", "size", "x-center(nm)", "y-center(nm)", "z-center(nm)", "size-x(nm)", "size-y(nm)", "size-z(nm)", "rg"]
    stats_fp.write(" ".join(header) + "\n")
    
    # Remove category zero localizations.
    i3_data = i3dtype.maskData(i3_data_in, (i3_data_in['c'] != 0))

    # Calculate cluster stats.
    labels = i3_data['lk']
    for k in set(labels):
        if (k>=2):
            mask = (labels == k)
            csize = mask.sum()
            if (csize > min_size):
                x = pix_to_nm*i3_data['xc'][mask]
                y = pix_to_nm*i3_data['yc'][mask]
                z = i3_data['zc'][mask]
                c = i3_data['c'][mask]

                # Calculate size in x, y, z.
                sx = numpy.max(x) - numpy.min(x)
                sy = numpy.max(y) - numpy.min(y)
                sz = numpy.max(z) - numpy.min(z)

                # Calculate radius of gyration.
                cx = numpy.mean(x)
                cy = numpy.mean(y)

                rx = x - cx
                ry = y - cy

                if 0:
                    cz = numpy.mean(z)
                    rz = z - cz
                    rg = math.sqrt(numpy.sum(rx*rx + ry*ry + rz*rz) / float(x.size))
                else:
                    rg = math.sqrt(numpy.sum(rx*rx + ry*ry) / float(x.size))

                print("Cluster:", k, x.size, "localizations")
                stats = map(str, [k, c[0], csize, numpy.mean(x), numpy.mean(y), numpy.mean(z), sx, sy, sz, rg])
                stats_fp.write(" ".join(stats) + "\n")

    stats_fp.close()
示例#5
0
    def __init__(self, filename, scale = 4, verbose = True):
        I3GGeneric.__init__(self,
                            filename,
                            scale = scale,
                            verbose = verbose)
        
        self.i3data = readinsight3.loadI3GoodOnly(filename, verbose = verbose)
        self.i3data['fr'] -= 1

        # Determine film size.
        [image_x, image_y, self.film_l] = getFilmSize(filename, self.i3data)
        self.im_size = [image_x, image_y]

        # Determine what channels the image has.
        self.channels = []
        for i in range(10):
            mask = (self.i3data['c'] == i)
            if mask.sum() > 0:
                self.channels.append(i)
示例#6
0
    def __init__(self, filename, scale = 4, verbose = True):
        I3GGeneric.__init__(self,
                            filename,
                            scale = scale,
                            verbose = verbose)
        
        self.i3data = readinsight3.loadI3GoodOnly(filename, verbose = verbose)
        self.i3data['fr'] -= 1

        # Determine film size.
        [image_x, image_y, self.film_l] = getFilmSize(filename, self.i3data)
        self.im_size = [image_x, image_y]

        # Determine what channels the image has.
        self.channels = []
        for i in range(10):
            mask = (self.i3data['c'] == i)
            if mask.sum() > 0:
                self.channels.append(i)
示例#7
0
def clusterSize(clist_name, clist_size_name, remove_cat0 = False):
    
    # Load the data.
    i3_data_in = readinsight3.loadI3GoodOnly(clist_name)

    # Remove category zero localizations.
    if remove_cat0:
        print("warning, removing category zero localizations!")
        i3_data = i3dtype.maskData(i3_data_in, (i3_data_in['c'] != 0))
    else:
        i3_data =  i3_data_in
        
    # Record cluster localization numbers in the fit area field.
    i3_data['a'] = dbscanC.localizationClusterSize(i3_data['lk'])+1

    # Copy cluster id into the frame field.
    i3_data['fr'] = i3_data['lk']

    # Save the data.
    i3_data_out = writeinsight3.I3Writer(clist_size_name)
    i3_data_out.addMolecules(i3_data)
    i3_data_out.close()
示例#8
0
def clusterSize(clist_name, clist_size_name, remove_cat0=False):

    # Load the data.
    i3_data_in = readinsight3.loadI3GoodOnly(clist_name)

    # Remove category zero localizations.
    if remove_cat0:
        print("warning, removing category zero localizations!")
        i3_data = i3dtype.maskData(i3_data_in, (i3_data_in['c'] != 0))
    else:
        i3_data = i3_data_in

    # Record cluster localization numbers in the fit area field.
    i3_data['a'] = dbscanC.localizationClusterSize(i3_data['lk']) + 1

    # Copy cluster id into the frame field.
    i3_data['fr'] = i3_data['lk']

    # Save the data.
    i3_data_out = writeinsight3.I3Writer(clist_size_name)
    i3_data_out.addMolecules(i3_data)
    i3_data_out.close()
示例#9
0
#
# Records the cluster size associated with
# a localization in its fit area field.
#
# Hazen 11/11
#

import sys

import storm_analysis.dbscan.dbscan_c as dbscanC
import storm_analysis.sa_library.i3dtype as i3dtype
import storm_analysis.sa_library.readinsight3 as readinsight3
import storm_analysis.sa_library.writeinsight3 as writeinsight3

# Load the data.
i3_data_in = readinsight3.loadI3GoodOnly(sys.argv[1])

# Remove category zero localizations.
if False:
    print "warning, removing category zero localizations!"
    i3_data = i3dtype.maskData(i3_data_in, (i3_data_in['c'] != 0))
else:
    i3_data = i3_data_in

# Record cluster localization numbers in the fit area field.
i3_data['a'] = dbscanC.localizationClusterSize(i3_data['lk']) + 1

# Copy cluster id into the frame field.
i3_data['fr'] = i3_data['lk']

# Save the data.
示例#10
0
def voronoi(mlist_name, clist_name, density_factor, min_size, verbose=True):

    i3_data_in = readinsight3.loadI3GoodOnly(mlist_name)
    n_locs = i3_data_in['xc'].size
    points = numpy.column_stack((i3_data_in['xc'], i3_data_in['yc']))

    print("Creating Voronoi object.")
    vor = Voronoi(points)

    print("Calculating 2D region sizes.")
    for i, region_index in enumerate(vor.point_region):
        if ((i % 10000) == 0):
            print("Processing point", i)

        vertices = []
        for vertex in vor.regions[region_index]:

            # I think these are edge regions?
            if (vertex == -1):
                vertices = []
                break

            vertices.append(vor.vertices[vertex])

        if (len(vertices) > 0):
            area = Polygon(vertices).area
            i3_data_in['a'][i] = 1.0 / area

    # Used median density based threshold.
    ave_density = numpy.median(i3_data_in['a'])
    if verbose:
        print("Min density", numpy.min(i3_data_in['a']))
        print("Max density", numpy.max(i3_data_in['a']))
        print("Median density", ave_density)

    # Record the neighbors of each point.
    max_neighbors = 40
    neighbors = numpy.zeros((n_locs, max_neighbors), dtype=numpy.int32) - 1
    neighbors_counts = numpy.zeros((n_locs), dtype=numpy.int32)

    print("Calculating neighbors")
    for ridge_p in vor.ridge_points:

        p1 = ridge_p[0]
        p2 = ridge_p[1]

        # Add p2 to the list for p1
        neighbors[p1, neighbors_counts[p1]] = p2
        neighbors_counts[p1] += 1

        # Add p1 to the list for p2
        neighbors[p2, neighbors_counts[p2]] = p1
        neighbors_counts[p2] += 1

    if False:
        n1 = neighbors[0, :]
        print(n1)
        print(neighbors[n1[0], :])

    # Mark connected points that meet the minimum density criteria.
    print("Marking connected regions")
    i3_data_in['lk'] = -1
    min_density = density_factor * ave_density
    visited = numpy.zeros((n_locs), dtype=numpy.int32)

    def neighborsList(index):
        nlist = []
        for i in range(neighbors_counts[index]):
            loc_index = neighbors[index, i]
            if (visited[loc_index] == 0):
                nlist.append(neighbors[index, i])
                visited[loc_index] = 1
        return nlist

    cluster_id = 2
    for i in range(n_locs):
        if (visited[i] == 0):
            if (i3_data_in['a'][i] > min_density):
                cluster_elt = [i]
                c_size = 1
                visited[i] = 1
                to_check = neighborsList(i)
                while (len(to_check) > 0):

                    # Remove last localization from the list.
                    loc_index = to_check[-1]
                    to_check = to_check[:-1]

                    # If the localization has sufficient density add to cluster and check neighbors.
                    if (i3_data_in['a'][loc_index] > min_density):
                        to_check += neighborsList(loc_index)
                        cluster_elt.append(loc_index)
                        c_size += 1

                    # Mark as visited.
                    visited[loc_index] = 1

                # Mark the cluster if there are enough localizations in the cluster.
                if (c_size > min_size):
                    print("cluster", cluster_id, "size", c_size)
                    for elt in cluster_elt:
                        i3_data_in['lk'][elt] = cluster_id
                cluster_id += 1
            visited[i] = 1

    print(cluster_id, "clusters")

    # Save the data.
    print("Saving results")
    i3_data_out = writeinsight3.I3Writer(clist_name)
    i3_data_out.addMolecules(i3_data_in)
    i3_data_out.close()
示例#11
0
def clusterImages(mlist_name, title, min_size, image_max, output, image_size):
    
    i3_data = readinsight3.loadI3GoodOnly(mlist_name)

    print("Only coloring clusters with at least", min_size, "localizations.")

    rand_color = randomcolor.RandomColor()

    scale = 4
    image_size[0] = scale * image_size[0]
    image_size[1] = scale * image_size[1]

    red_image = numpy.zeros(image_size)
    grn_image = numpy.zeros(image_size)
    blu_image = numpy.zeros(image_size)
    sum_image = numpy.zeros(image_size)
    
    labels = i3_data['lk']
    start = int(numpy.min(labels))
    stop = int(numpy.max(labels)) + 1
    
    num_clusters = 0
    for k in range(start,stop):
        if ((k%200)==0):
            print("Processing cluster", k)
        
        mask = (labels == k)
        csize = mask.sum()

        x = scale * i3_data['xc'][mask]
        y = scale * i3_data['yc'][mask]

        if (k == -1) or (csize < min_size):
            r = 1.0
            g = 1.0
            b = 1.0
        else:
            num_clusters += 1
            color = rand_color.generate()
            r = int(color[0][1:3], 16)/255.0
            g = int(color[0][3:5], 16)/255.0        
            b = int(color[0][5:7], 16)/255.0

        if False:
            temp = numpy.zeros(image_size)
            dg.drawGaussiansXYOnImage(temp, x, y, sigma = 1.5)

            red_image += r * temp
            grn_image += g * temp
            blu_image += b * temp
            sum_image += temp
        
        else:
            for i in range(x.size):
                yi = int(x[i])
                xi = int(y[i])
                if (xi >= 0) and (xi < image_size[0]) and (yi >= 0) and (yi < image_size[1]):
                    red_image[xi,yi] += r
                    grn_image[xi,yi] += g
                    blu_image[xi,yi] += b
                    sum_image[xi,yi] += 1
        
    # Some wacky normalization scheme..
    mask = (sum_image > image_max)

    red_image[mask] = (red_image[mask]/sum_image[mask]) * image_max
    grn_image[mask] = (grn_image[mask]/sum_image[mask]) * image_max
    blu_image[mask] = (blu_image[mask]/sum_image[mask]) * image_max
    sum_image[mask] = image_max

    rgb_image = numpy.zeros((image_size[0], image_size[1], 3))
    rgb_image[:,:,0] = red_image
    rgb_image[:,:,1] = grn_image
    rgb_image[:,:,2] = blu_image

    rgb_image = (255.0/image_max)*rgb_image
    sum_image = (255.0/image_max)*sum_image

    rgb_image = rgb_image.astype(numpy.uint8)
    sum_image = sum_image.astype(numpy.uint8)

    title += " (" + str(num_clusters) + ")"

    # Save RGB image.
    img1 = Image.fromarray(rgb_image, "RGB")
    try:
        draw1 = ImageDraw.Draw(img1)
        font1 = ImageFont.truetype("FreeMono.ttf", 24)
        draw1.text((2,2), title, (255,255,255), font = font1)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img1.save(output + "_01.png")

    # Save grayscale image.
    img2 = Image.fromarray(sum_image, "L")
    try:
        img2 = img2.convert("RGB")
        draw2 = ImageDraw.Draw(img2)
        font2 = ImageFont.truetype("FreeMono.ttf", 24)
        draw2.text((2,2), title, (255,255,255), font = font2)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img2.save(output + "_02.png")
示例#12
0
def voronoi(mlist_name, clist_name, density_factor, min_size, verbose = True):

    i3_data_in = readinsight3.loadI3GoodOnly(mlist_name)
    n_locs = i3_data_in['xc'].size
    points = numpy.column_stack((i3_data_in['xc'], i3_data_in['yc']))

    print("Creating Voronoi object.")
    vor = Voronoi(points)

    print("Calculating 2D region sizes.")
    for i, region_index in enumerate(vor.point_region):
        if ((i%10000) == 0):
            print("Processing point", i)

        vertices = []
        for vertex in vor.regions[region_index]:
        
            # I think these are edge regions?
            if (vertex == -1):
                vertices = []
                break

            vertices.append(vor.vertices[vertex])
            
        if (len(vertices) > 0):
            area = Polygon(vertices).area
            i3_data_in['a'][i] = 1.0/area

    # Used median density based threshold.
    ave_density = numpy.median(i3_data_in['a'])
    if verbose:
        print("Min density", numpy.min(i3_data_in['a']))
        print("Max density", numpy.max(i3_data_in['a']))
        print("Median density", ave_density)

    # Record the neighbors of each point.
    max_neighbors = 40
    neighbors = numpy.zeros((n_locs, max_neighbors), dtype = numpy.int32) - 1
    neighbors_counts = numpy.zeros((n_locs), dtype = numpy.int32)

    print("Calculating neighbors")
    for ridge_p in vor.ridge_points:

        p1 = ridge_p[0]
        p2 = ridge_p[1]

        # Add p2 to the list for p1
        neighbors[p1,neighbors_counts[p1]] = p2
        neighbors_counts[p1] += 1

        # Add p1 to the list for p2
        neighbors[p2,neighbors_counts[p2]] = p1
        neighbors_counts[p2] += 1

    if False:
        n1 = neighbors[0,:]
        print(n1)
        print(neighbors[n1[0],:])

    # Mark connected points that meet the minimum density criteria.
    print("Marking connected regions")
    i3_data_in['lk'] = -1
    min_density = density_factor * ave_density
    visited = numpy.zeros((n_locs), dtype = numpy.int32)

    def neighborsList(index):
        nlist = []
        for i in range(neighbors_counts[index]):
            loc_index = neighbors[index,i]
            if (visited[loc_index] == 0):
                nlist.append(neighbors[index,i])
                visited[loc_index] = 1
        return nlist

    cluster_id = 2
    for i in range(n_locs):
        if (visited[i] == 0):
            if (i3_data_in['a'][i] > min_density):
                cluster_elt = [i]
                c_size = 1
                visited[i] = 1
                to_check = neighborsList(i)
                while (len(to_check) > 0):

                    # Remove last localization from the list.
                    loc_index = to_check[-1]
                    to_check = to_check[:-1]

                    # If the localization has sufficient density add to cluster and check neighbors.
                    if (i3_data_in['a'][loc_index] > min_density):
                        to_check += neighborsList(loc_index)
                        cluster_elt.append(loc_index)
                        c_size += 1

                    # Mark as visited.
                    visited[loc_index] = 1

                # Mark the cluster if there are enough localizations in the cluster.
                if (c_size > min_size):
                    print("cluster", cluster_id, "size", c_size)
                    for elt in cluster_elt:
                        i3_data_in['lk'][elt] = cluster_id
                cluster_id += 1
            visited[i] = 1

    print(cluster_id, "clusters")
    
    # Save the data.
    print("Saving results")
    i3_data_out = writeinsight3.I3Writer(clist_name)
    i3_data_out.addMolecules(i3_data_in)
    i3_data_out.close()
示例#13
0
def clusterImages(mlist_name, title, min_size, image_max, output, image_size):
    
    i3_data = readinsight3.loadI3GoodOnly(mlist_name)

    print("Only coloring clusters with at least", min_size, "localizations.")

    rand_color = randomcolor.RandomColor()

    scale = 4
    image_size[0] = scale * image_size[0]
    image_size[1] = scale * image_size[1]

    red_image = numpy.zeros(image_size)
    grn_image = numpy.zeros(image_size)
    blu_image = numpy.zeros(image_size)
    sum_image = numpy.zeros(image_size)
    
    labels = i3_data['lk']
    start = int(numpy.min(labels))
    stop = int(numpy.max(labels)) + 1
    
    num_clusters = 0
    for k in range(start,stop):
        if ((k%200)==0):
            print("Processing cluster", k)
        
        mask = (labels == k)
        csize = mask.sum()

        x = scale * i3_data['xc'][mask]
        y = scale * i3_data['yc'][mask]

        if (k == -1) or (csize < min_size):
            r = 1.0
            g = 1.0
            b = 1.0
        else:
            num_clusters += 1
            color = rand_color.generate()
            r = int(color[0][1:3], 16)/255.0
            g = int(color[0][3:5], 16)/255.0        
            b = int(color[0][5:7], 16)/255.0

        if False:
            temp = numpy.zeros(image_size)
            dg.drawGaussiansXYOnImage(temp, x, y, sigma = 1.5)

            red_image += r * temp
            grn_image += g * temp
            blu_image += b * temp
            sum_image += temp
        
        else:
            for i in range(x.size):
                yi = int(x[i])
                xi = int(y[i])
                if (xi >= 0) and (xi < image_size[0]) and (yi >= 0) and (yi < image_size[1]):
                    red_image[xi,yi] += r
                    grn_image[xi,yi] += g
                    blu_image[xi,yi] += b
                    sum_image[xi,yi] += 1
        
    # Some wacky normalization scheme..
    mask = (sum_image > image_max)

    red_image[mask] = (red_image[mask]/sum_image[mask]) * image_max
    grn_image[mask] = (grn_image[mask]/sum_image[mask]) * image_max
    blu_image[mask] = (blu_image[mask]/sum_image[mask]) * image_max
    sum_image[mask] = image_max

    rgb_image = numpy.zeros((image_size[0], image_size[1], 3))
    rgb_image[:,:,0] = red_image
    rgb_image[:,:,1] = grn_image
    rgb_image[:,:,2] = blu_image

    rgb_image = (255.0/image_max)*rgb_image
    sum_image = (255.0/image_max)*sum_image

    rgb_image = rgb_image.astype(numpy.uint8)
    sum_image = sum_image.astype(numpy.uint8)

    title += " (" + str(num_clusters) + ")"

    # Save RGB image.
    img1 = Image.fromarray(rgb_image, "RGB")
    try:
        draw1 = ImageDraw.Draw(img1)
        font1 = ImageFont.truetype("FreeMono.ttf", 24)
        draw1.text((2,2), title, (255,255,255), font = font1)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img1.save(output + "_01.png")

    # Save grayscale image.
    img2 = Image.fromarray(sum_image, "L")
    try:
        img2 = img2.convert("RGB")
        draw2 = ImageDraw.Draw(img2)
        font2 = ImageFont.truetype("FreeMono.ttf", 24)
        draw2.text((2,2), title, (255,255,255), font = font2)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img2.save(output + "_02.png")