Esempio n. 1
0
 def gridFn():
     mask = (self.i3data['c'] == channel)
     self.i3data = i3dtype.maskData(self.i3data, mask)
     return self.i3To2DGridAllChannelsMerged(zmin = zmin,
                                             zmax = zmax,
                                             uncorrected = uncorrected,
                                             verbose = verbose)
Esempio n. 2
0
 def gridFn():
     mask = (self.i3data['c'] == channel)
     self.i3data = i3dtype.maskData(self.i3data, mask)
     return self.i3To2DGridAllChannelsMerged(zmin = zmin,
                                             zmax = zmax,
                                             uncorrected = uncorrected,
                                             verbose = verbose)
Esempio n. 3
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()
Esempio n. 4
0
    def getEmitters(self, frame):
        integrated_on = numpy.zeros(self.n_emitters)

        #
        # This is a little complicated because we are trying to include accurate
        # modeling of emitters that turned on/off more than once in a single frame.
        #
        for i in range(self.n_emitters):
                
            # The easy case, no change in state in the current frame.
            if (self.next_transistion[i] >= (frame + 1.0)):
                if self.am_on[i]:
                    integrated_on[i] = 1.0

            else:
                last_transistion = frame
                while (self.next_transistion[i] < (frame + 1.0)):
                    if self.am_on[i]:
                        integrated_on[i] += self.next_transistion[i] - last_transistion

                    self.am_on[i] = not self.am_on[i]
                    last_transistion = self.next_transistion[i]
                    if self.am_on[i]:
                        self.next_transistion[i] += numpy.random.exponential(self.on_time)
                    else:
                        self.next_transistion[i] += numpy.random.exponential(self.off_time)
                    
                # Turned on and not off again in the current frame.
                if self.am_on[i]:
                    integrated_on[i] += (frame + 1.0) - last_transistion

        # Set height and return only those emitters that have height > 0.
        self.i3_data['a'][:] = integrated_on * self.photons
        return i3dtype.maskData(self.i3_data, (self.i3_data['a'] > 0.0))
Esempio n. 5
0
def render2DImage(i3_reader,
                  shape,
                  category=None,
                  offsets=None,
                  scale=2,
                  sigma=None):
    """
    Create a grayscale image from a Insight3 format binary data.

    i3_reader - A readinsight3.I3Reader object.
    shape - The shape of the output image. This will be multiplied by scale.
    category - Filter for localizations of this category. The default is all categories.
    offsets - X,Y offset of the image origin.  The default is 0.0.
    scale - The 'zoom' level of the output image, i.e. if the original STORM movie was
            256x256 and scale = 2 then the output image will be 512x512.
    sigma - The sigma to use when rendering gaussians (pixels). If this is None then
            the image will be a histogram.
    """
    image = numpy.zeros((shape[0] * scale, shape[1] * scale))

    # Make sure we are starting at the beginning.
    i3_reader.resetFp()

    # Load the first block of data.
    i3_data = i3_reader.nextBlock()
    while (i3_data is not False):
        sys.stdout.write(".")
        sys.stdout.flush()

        # Filter by category, if requested.
        if category is not None:
            i3_data = i3dtype.maskData(i3_data, (i3_data['c'] == category))

        # Adjust by offsets, if specified. Note, not adjusted for scale.
        if offsets is not None:
            i3_data['xc'] -= offsets[0]
            i3_data['yc'] -= offsets[1]

        # Adjust x,y by scale.
        xc = i3_data['xc'] * scale
        yc = i3_data['yc'] * scale

        # Histogram.
        if sigma is None:
            image += gridC.grid2D(numpy.round(xc), numpy.round(yc),
                                  image.shape)
        # Gaussians.
        else:
            dg.drawGaussiansXYOnImage(image, xc, yc, sigma=sigma)

        # Load next block of data.
        i3_data = i3_reader.nextBlock()

    print()
    return image
Esempio n. 6
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()
Esempio n. 7
0
 def getMoleculesInFrameRange(self, start, stop, good_only = True):
     start_mol_num = self.findFrame(start)
     stop_mol_num = self.findFrame(stop)
     if (type(self.localizations) == type(numpy.array([]))):
         data = self.localizations[start_mol_num:stop_mol_num]
     else:
         cur = self.fp.tell()
         self.fp.seek(16 + start_mol_num*self.record_size)
         size = stop_mol_num - start_mol_num
         data = numpy.fromfile(self.fp,
                               dtype=i3dtype.i3DataType(),
                               count=size)
         self.fp.seek(cur)  
     if good_only:
         return i3dtype.maskData(data, (data['c'] != 9))
     else:
         return data
Esempio n. 8
0
 def getMoleculesInFrameRange(self, start, stop, good_only=True):
     start_mol_num = self.findFrame(start)
     stop_mol_num = self.findFrame(stop)
     if (type(self.localizations) == type(numpy.array([]))):
         data = self.localizations[start_mol_num:stop_mol_num]
     else:
         cur = self.fp.tell()
         self.fp.seek(16 + start_mol_num * self.record_size)
         size = stop_mol_num - start_mol_num
         data = numpy.fromfile(self.fp,
                               dtype=i3dtype.i3DataType(),
                               count=size)
         self.fp.seek(cur)
     if good_only:
         return i3dtype.maskData(data, (data['c'] != 9))
     else:
         return data
Esempio n. 9
0
def reduceMList(i3_name_in,
                i3_name_out,
                xstart=None,
                xstop=None,
                ystart=None,
                ystop=None,
                min_frame=None,
                max_frame=None):

    meta_data = readinsight3.loadI3Metadata(i3_name_in)

    i3_in = readinsight3.I3Reader(i3_name_in)
    i3_out = writeinsight3.I3Writer(i3_name_out)

    i3_data = i3_in.nextBlock()
    while (i3_data is not False):
        sys.stdout.write(".")
        sys.stdout.flush()

        # Create mask.
        mask = numpy.full(i3_data.size, True, dtype=bool)
        if xstart is not None:
            mask = mask & (i3_data['xc'] > xstart)
        if xstop is not None:
            mask = mask & (i3_data['xc'] < xstop)
        if ystart is not None:
            mask = mask & (i3_data['yc'] > ystart)
        if ystop is not None:
            mask = mask & (i3_data['yc'] < ystop)
        if min_frame is not None:
            mask = mask & (i3_data['fr'] > min_frame)
        if max_frame is not None:
            mask = mask & (i3_data['fr'] < max_frame)

        i3_data = i3dtype.maskData(i3_data, mask)
        if (i3_data.size > 0):
            i3_out.addMolecules(i3_data)

        i3_data = i3_in.nextBlock()

    print()

    if meta_data is not None:
        i3_out.closeWithMetadata(ElementTree.tostring(meta_data, 'ISO-8859-1'))
    else:
        i3_out.close()
Esempio n. 10
0
    def nextBlock(self, block_size=400000, good_only=True):

        # check if we have read all the molecules.
        if (self.cur_molecule >= self.molecules):
            return False

        size = block_size

        # adjust size if we'll read past the end of the file.
        if ((self.cur_molecule + size) > self.molecules):
            size = self.molecules - self.cur_molecule

        self.cur_molecule += size

        # read the data
        data = numpy.fromfile(self.fp, dtype=i3dtype.i3DataType(), count=size)

        if good_only:
            return i3dtype.maskData(data, (data['c'] != 9))
        else:
            return data
Esempio n. 11
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()
Esempio n. 12
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()
Esempio n. 13
0
    def nextBlock(self, block_size = 400000, good_only = True):

        # check if we have read all the molecules.
        if(self.cur_molecule>=self.molecules):
            return False

        size = block_size
        
        # adjust size if we'll read past the end of the file.
        if((self.cur_molecule+size)>self.molecules):
            size = self.molecules - self.cur_molecule

        self.cur_molecule += size

        # read the data
        data = numpy.fromfile(self.fp,
                              dtype=i3dtype.i3DataType(),
                              count=size)

        if good_only:
            return i3dtype.maskData(data, (data['c'] != 9))
        else:
            return data
Esempio n. 14
0
    def getEmitters(self, frame):
        integrated_on = numpy.zeros(self.n_emitters)

        #
        # This is a little complicated because we are trying to include accurate
        # modeling of emitters that turned on/off more than once in a single frame.
        #
        for i in range(self.n_emitters):

            # The easy case, no change in state in the current frame.
            if (self.next_transistion[i] >= (frame + 1.0)):
                if self.am_on[i]:
                    integrated_on[i] = 1.0

            else:
                last_transistion = frame
                while (self.next_transistion[i] < (frame + 1.0)):
                    if self.am_on[i]:
                        integrated_on[
                            i] += self.next_transistion[i] - last_transistion

                    self.am_on[i] = not self.am_on[i]
                    last_transistion = self.next_transistion[i]
                    if self.am_on[i]:
                        self.next_transistion[i] += numpy.random.exponential(
                            self.on_time)
                    else:
                        self.next_transistion[i] += numpy.random.exponential(
                            self.off_time)

                # Turned on and not off again in the current frame.
                if self.am_on[i]:
                    integrated_on[i] += (frame + 1.0) - last_transistion

        # Set height and return only those emitters that have height > 0.
        self.i3_data['a'][:] = integrated_on * self.photons
        return i3dtype.maskData(self.i3_data, (self.i3_data['a'] > 0.0))
Esempio n. 15
0
# 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.
i3_data_out = writeinsight3.I3Writer(sys.argv[2])
i3_data_out.addMolecules(i3_data)
i3_data_out.close()
Esempio n. 16
0
def loadI3NumpyGoodOnly(filename, verbose=1):
    data = loadI3FileNumpy(filename, verbose=verbose)
    return i3dtype.maskData(data, (data['c'] != 9))
Esempio n. 17
0
def loadI3NumpyGoodOnly(filename, verbose = True):
    """
    This filters out molecules with poor z fits (daostorm "3D" fit mode).
    """
    data = loadI3FileNumpy(filename, verbose = verbose)
    return i3dtype.maskData(data, (data['c'] != 9))
Esempio n. 18
0
def loadI3NumpyGoodOnly(filename, verbose = 1):
    data = loadI3FileNumpy(filename, verbose = verbose)
    return i3dtype.maskData(data, (data['c'] != 9))
Esempio n. 19
0
def loadI3NumpyGoodOnly(filename, verbose = True):
    """
    This filters out molecules with poor z fits (daostorm "3D" fit mode).
    """
    data = loadI3FileNumpy(filename, verbose = verbose)
    return i3dtype.maskData(data, (data['c'] != 9))
Esempio n. 20
0
    args.maxi = numpy.max(i3_data['i']) + 1

if (args.maxi > (numpy.max(i3_data['i']) + 1)):
    args.maxi = numpy.max(i3_data['i']) + 1

max_frame = numpy.max(i3_data['fr'])

on_times = []
off_times = []
for i in range(int(args.maxi)):

    if ((i % 100) == 0):
        print("Analyzing emitter", i)

    # We are counting on getting the frames where the emitter was on in order.
    emitter_i3 = i3dtype.maskData(i3_data, (i3_data['i'] == i))
    frames_on = emitter_i3['fr']
    #print(frames_on, frames_on.size)

    # Check if the emitter was never on.
    if (frames_on.size == 0):
        continue

    # Check if off in the first frame.
    if (frames_on[0] != 1):
        off_times.append(frames_on[0])

    # Check if only a single frame.
    if (frames_on.size == 1):
        on_times.append(1)
        continue
    args.maxi = numpy.max(i3_data['i']) + 1

if (args.maxi > (numpy.max(i3_data['i']) + 1)):
    args.maxi = numpy.max(i3_data['i']) + 1

max_frame = numpy.max(i3_data['fr'])

on_times = []
off_times = []
for i in range(int(args.maxi)):

    if ((i % 100) == 0):
        print("Analyzing emitter", i)

    # We are counting on getting the frames where the emitter was on in order.
    emitter_i3 = i3dtype.maskData(i3_data, (i3_data['i'] == i))
    frames_on = emitter_i3['fr']
    #print(frames_on, frames_on.size)

    # Check if the emitter was never on.
    if (frames_on.size == 0):
        continue

    # Check if off in the first frame.
    if (frames_on[0] != 1):
        off_times.append(frames_on[0])

    # Check if only a single frame.
    if (frames_on.size == 1):
        on_times.append(1)
        continue
Esempio n. 22
0
def render3DImage(i3_reader,
                  shape,
                  category=None,
                  offsets=None,
                  scale=2,
                  sigma=None,
                  z_edges=None):
    """
    Create a stack of grayscale images from a Insight3 format binary data.

    i3_reader - A readinsight3.I3Reader object.
    shape - The shape of the output image. This will be multiplied by scale.
    category - Filter for localizations of this category. The default is all categories.
    offsets - X,Y offset of the image origin.  The default is 0.0.
    scale - The 'zoom' level of the output image, i.e. if the original STORM movie was
            256x256 and scale = 2 then the output image will be 512x512.
    sigma - The sigma to use when rendering gaussians (pixels). If this is None then
            the image will be a histogram.
    z_edges - A list of z values specifying the z range for each image. This should be
            in nanometers.
    """
    num_z = len(z_edges) - 1
    images = []
    for i in range(num_z):
        images.append(numpy.zeros((shape[0] * scale, shape[1] * scale)))

    # Make sure we are starting at the beginning.
    i3_reader.resetFp()

    # Load the first block of data.
    i3_data = i3_reader.nextBlock()
    while (i3_data is not False):
        sys.stdout.write(".")
        sys.stdout.flush()

        # Filter by category, if requested.
        if category is not None:
            i3_data = i3dtype.maskData(i3_data, (i3_data['c'] == category))

        # Adjust by offsets, if specified. Note, not adjusted for scale.
        if offsets is not None:
            i3_data['xc'] -= offsets[0]
            i3_data['yc'] -= offsets[1]

        # Adjust x,y by scale.
        xc = i3_data['xc'] * scale
        yc = i3_data['yc'] * scale

        # Iterate through z ranges.
        for i in range(num_z):
            z_mask = (i3_data['zc'] > z_edges[i]) & (i3_data['zc'] <
                                                     z_edges[i + 1])

            xc_z = xc[z_mask]
            yc_z = yc[z_mask]

            # Histogram.
            if sigma is None:
                images[i] += gridC.grid2D(numpy.round(xc_z), numpy.round(yc_z),
                                          images[0].shape)
            # Gaussians.
            else:
                dg.drawGaussiansXYOnImage(images[i], xc_z, yc_z, sigma=sigma)

        # Load next block of data.
        i3_data = i3_reader.nextBlock()

    print()
    return images