def copyTracking(ref_track_filename, input_filename, output_filename): i3_ref = readinsight3.I3Reader(ref_track_filename) i3_in = readinsight3.I3Reader(input_filename) # Verify that these two localization files are actually pairs. assert (i3_ref.molecules == i3_in.molecules), "Input files do not match." # # Verify that the output file doesn't already exist. # assert not os.path.exists(output_filename), "Output file already exists." i3_out = writeinsight3.I3Writer(output_filename) i3_ref_data = i3_ref.nextBlock() while (i3_ref_data is not False): i3_in_data = i3_in.nextBlock() # Copy tracking information from the reference data file. for field in ["tl", "lk", "fi"]: i3_in_data[field] = i3_ref_data[field] # Save merge of reference (channel 0) and current channel. i3_out.addMolecules(i3_in_data) # Load the next block of reference data. i3_ref_data = i3_ref.nextBlock() # At least for now we are not bothering to copy the meta-data. i3_out.close()
def __init__(self, color=None, locs_name=None, movie_name=None, number=None, **kwds): super().__init__(**kwds) self.color = color self.cur_frame = 0 self.flip_lr = False self.flip_ud = False self.fr_height = None self.fr_width = None self.image = None self.locs = None self.locs_name = locs_name self.movie_name = movie_name self.number = number self.offset_x = 0 self.offset_y = 0 self.pixmap = None self.locs_i3 = readinsight3.I3Reader(locs_name) self.movie_fp = datareader.inferReader(movie_name) self.movie_len = self.movie_fp.filmSize()[2]
def render2DImageFromFile(i3_filename, shape=None, category=None, offsets=None, scale=2, sigma=None): """ Wraps render2DIMage() to make it easier to create an image from a Insight3 format binary file. """ i3_reader = readinsight3.I3Reader(i3_filename) # If not specified, figure out shape. if shape is None: # Load the first block of data (which might be the whole file). i3_reader.resetFp() i3_data = i3_reader.nextBlock() # Try and figure out the original movie size. [x_size, y_size, temp] = i3togrid.getFilmSize(i3_filename, i3_data) shape = (x_size, y_size) return render2DImage(i3_reader, shape, category=category, offsets=offsets, scale=scale, sigma=sigma)
def __init__(self, filename=None, **kwds): super(MoleculeListI3, self).__init__(**kwds) self.fields = [ "x", "y", "z", "background", "error", "height", "sum", "xsigma", "ysigma" ] self.reader = readinsight3.I3Reader(filename) self.n_locs = self.reader.getNumberMolecules()
def __init__(self, filename, frame_sx, frame_sy, mtype): self.i3_bin = readinsight3.I3Reader(filename) self.data = None self.frame_sx = frame_sx self.frame_sy = frame_sy self.last_frame = -1 self.last_i = 0 self.mol_items = [] self.mtype = mtype
def BinToHDF5(bin_name, hdf5_name): if os.path.exists(hdf5_name): os.remove(hdf5_name) with saH5Py.SAH5Py(hdf5_name, is_existing=False) as h5: if True: metadata = readinsight3.loadI3Metadata(bin_name) assert (metadata is not None), "No metadata found for " + bin_name movie_xml = metadata.find("movie") movie_x = int(movie_xml.find("movie_x").text) movie_y = int(movie_xml.find("movie_y").text) movie_l = int(movie_xml.find("movie_l").text) hash_value = movie_xml.find("hash_value") params_xml = metadata.find("settings") nm_per_pixel = float(params_xml.find("pixel_size").text) else: # Fill this in to work around missing metadata. movie_x = 256 movie_y = 256 movie_l = 10 hash_value = "" params_xml = None nm_per_pixel = 100.0 # Set movie properties. h5.setMovieInformation(movie_x, movie_y, movie_l, hash_value) # Set pixel size. h5.setPixelSize(nm_per_pixel) # Set metadata. if params_xml is not None: if (sys.version_info > (3, 0)): h5.addMetadata(ElementTree.tostring(params_xml, 'unicode')) else: h5.addMetadata(ElementTree.tostring(params_xml, 'ISO-8859-1')) # Convert data. i3 = readinsight3.I3Reader(bin_name) for i in range(i3.getNumberFrames()): i3data = i3.getMoleculesInFrame(i + 1) h5_locs = i3dtype.convertToSAHDF5(i3data, i + 1, nm_per_pixel) h5.addLocalizations(h5_locs, i)
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()
def test_write_read_3(): """ Test I3Reader on an empty file. """ bin_name = storm_analysis.getPathOutputTest("test_insight3io.bin") i3_locs = i3dtype.createDefaultI3Data(10) i3dtype.posSet(i3_locs, 'x', 10.0) with writeinsight3.I3Writer(bin_name) as i3: pass i3_reader = readinsight3.I3Reader(bin_name) # Read localizations. i3_in = i3_reader.nextBlock() assert(i3_in is False)
def __init__(self, filename, scale = 4, verbose = True): I3GGeneric.__init__(self, filename, scale = scale, verbose = verbose) self.i3_in = readinsight3.I3Reader(filename) self.i3data = self.i3_in.nextBlock() self.resetFp() # 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)
def test_write_read_2(): """ Test I3Reader. """ bin_name = storm_analysis.getPathOutputTest("test_insight3io.bin") i3_locs = i3dtype.createDefaultI3Data(10) i3dtype.posSet(i3_locs, 'x', 10.0) with writeinsight3.I3Writer(bin_name) as i3: i3.addMolecules(i3_locs) i3_reader = readinsight3.I3Reader(bin_name) # Read localizations. i3_in = i3_reader.nextBlock() assert(numpy.allclose(i3_locs['x'], i3_in['x'])) assert(i3_in is not False) # This should return False because there are no more localizations. i3_in = i3_reader.nextBlock() assert(i3_in is False)
def xyDrift(locs_filename): locs_i3 = readinsight3.I3Reader(locs_filename) # # Load the number of frames and the localizations in # the first and last frame. # n_frames = locs_i3.getNumberFrames() f0_locs = locs_i3.getMoleculesInFrame(1) fn_locs = locs_i3.getMoleculesInFrame(n_frames) assert (f0_locs.size > 0), "No localizations in the first frame." assert (fn_locs.size > 0), "No localizations in the last frame." # # Identify matching beads in the first and last frame and # compute the displacement. # p_index = utilC.peakToPeakIndex(f0_locs['xc'], f0_locs['yc'], fn_locs['xc'], fn_locs['yc']) all_dx = [] all_dy = [] for i in range(f0_locs.size): dx = fn_locs['xc'][p_index[i]] - f0_locs['xc'][i] dy = fn_locs['yc'][p_index[i]] - f0_locs['yc'][i] if ((dx * dx + dy * dy) < 1.0): all_dx.append(dx) all_dy.append(dy) # # Return average per frame. # return [ numpy.mean(numpy.array(all_dx)) / float(n_frames), numpy.mean(numpy.array(all_dy)) / float(n_frames) ]
def MergeBin(bin_files, results_file): assert not os.path.exists(results_file) # Load meta data. metadata = readinsight3.loadI3Metadata(bin_files[0]) # Create I3 writer. i3w = writeinsight3.I3Writer(results_file) # Sequentially read input files and copy into output file. for b_file in bin_files: print("Merging", b_file) i3_reader = readinsight3.I3Reader(b_file) i3_data = i3_reader.nextBlock() while i3_data is not False: print(" working..") i3w.addMolecules(i3_data) i3_data = i3_reader.nextBlock() # Close i3 output file. if metadata is None: i3w.close() else: i3w.closeWithMetadata(ElementTree.tostring(metadata, 'ISO-8859-1'))
def psfLocalizations(i3_filename, mapping_filename, frame = 1, aoi_size = 8, movie_filename = None): # Load localizations. i3_reader = readinsight3.I3Reader(i3_filename) # Load mapping. mappings = {} if os.path.exists(mapping_filename): with open(mapping_filename, 'rb') as fp: mappings = pickle.load(fp) else: print("Mapping file not found, single channel data?") # Try and determine movie frame size. i3_metadata = readinsight3.loadI3Metadata(i3_filename) if i3_metadata is None: if movie_filename is None: raise Exception("I3 metadata not found and movie filename is not specified.") else: movie_fp = datareader.inferReader(movie_filename) [movie_y, movie_x] = movie_fp.filmSize()[:2] else: movie_data = i3_metadata.find("movie") # FIXME: These may be transposed? movie_x = int(movie_data.find("movie_x").text) movie_y = int(movie_data.find("movie_y").text) # Load localizations in the requested frame. locs = i3_reader.getMoleculesInFrame(frame) print("Loaded", locs.size, "localizations.") # Remove localizations that are too close to each other. in_locs = numpy.zeros((locs["x"].size, util_c.getNPeakPar())) in_locs[:,util_c.getXCenterIndex()] = locs["x"] in_locs[:,util_c.getYCenterIndex()] = locs["y"] out_locs = util_c.removeNeighbors(in_locs, 2 * aoi_size) xf = out_locs[:,util_c.getXCenterIndex()] yf = out_locs[:,util_c.getYCenterIndex()] # # Remove localizations that are too close to the edge or # outside of the image in any of the channels. # is_good = numpy.ones(xf.size, dtype = numpy.bool) for i in range(xf.size): # Check in Channel 0. if (xf[i] < aoi_size) or (xf[i] + aoi_size >= movie_x): is_good[i] = False continue if (yf[i] < aoi_size) or (yf[i] + aoi_size >= movie_y): is_good[i] = False continue # Check other channels. for key in mappings: if not is_good[i]: break coeffs = mappings[key] [ch1, ch2, axis] = key.split("_") if (ch1 == "0"): if (axis == "x"): xm = coeffs[0] + coeffs[1]*xf[i] + coeffs[2]*yf[i] if (xm < aoi_size) or (xm + aoi_size >= movie_x): is_good[i] = False break elif (axis == "y"): ym = coeffs[0] + coeffs[1]*xf[i] + coeffs[2]*yf[i] if (ym < aoi_size) or (ym + aoi_size >= movie_y): is_good[i] = False break # # Save localizations for each channel. # gx = xf[is_good] gy = yf[is_good] basename = os.path.splitext(i3_filename)[0] with writeinsight3.I3Writer(basename + "_c1_psf.bin") as w3: w3.addMoleculesWithXY(gx, gy) index = 1 while ("0_" + str(index) + "_x" in mappings): cx = mappings["0_" + str(index) + "_x"] cy = mappings["0_" + str(index) + "_y"] #cx = mappings[str(index) + "_0" + "_x"] #cy = mappings[str(index) + "_0" + "_y"] xm = cx[0] + cx[1] * gx + cx[2] * gy ym = cy[0] + cy[1] * gx + cy[2] * gy with writeinsight3.I3Writer(basename + "_c" + str(index+1) + "_psf.bin") as w3: w3.addMoleculesWithXY(xm, ym) index += 1 # # Print localizations that were kept. # print(gx.size, "localizations were kept:") for i in range(gx.size): print("ch0: {0:.2f} {1:.2f}".format(gx[i], gy[i])) index = 1 while ("0_" + str(index) + "_x" in mappings): cx = mappings["0_" + str(index) + "_x"] cy = mappings["0_" + str(index) + "_y"] xm = cx[0] + cx[1] * gx[i] + cx[2] * gy[i] ym = cy[0] + cy[1] * gx[i] + cy[2] * gy[i] print("ch" + str(index) + ": {0:.2f} {1:.2f}".format(xm, ym)) index += 1 print("") print("")
parser.add_argument('--measured_bin', dest='measured_bin', type=str, required=True, help="Measured localization file.") parser.add_argument('--pixel_size', dest='pixel_size', type=float, required=False, default=160.0, help="Camera pixel size in nanometers.") args = parser.parse_args() # For converting XY units to nanometers. truth_i3 = readinsight3.I3Reader(args.truth_bin) measured_i3 = readinsight3.I3Reader(args.measured_bin) [all_dx, all_dy, all_dz] = findingFittingError(truth_i3, measured_i3, pixel_size=args.pixel_size) print("means and standard deviations (in nm):") print("mean, std (dx)", numpy.mean(all_dx), numpy.std(all_dx)) print("mean, std (dy)", numpy.mean(all_dy), numpy.std(all_dy)) print("mean, std (dz)", numpy.mean(all_dz), numpy.std(all_dz)) print("") h_range_xy = 100.0 h_range_z = 200.0 [hist_dx, bins_xy] = numpy.histogram(all_dx, bins=30,
def __init__(self, filename): super(LocalizationReaderI3, self).__init__() self.reader = readinsight3.I3Reader(filename)
# 07/16 # # Updated for the 2016 challenge. # # Hazen # import sys import storm_analysis.sa_library.readinsight3 as readinsight3 if (len(sys.argv) != 4): print("usage: <bin_file> <smlc_file> <pix_to_nm>") exit() i3_reader = readinsight3.I3Reader(sys.argv[1]) i3_block = i3_reader.nextBlock(block_size=1000, good_only=False) smlc_file_fp = open(sys.argv[2], "w") smlc_file_fp.write("index, frame, xnano, ynano, znano, intensity\n") pix_to_nm = float(sys.argv[3]) print("Saving Localizations") localization_number = 0 index = 0 while (type(i3_block) != type(False)): print(" saving localization", localization_number) for i in range(len(i3_block)):
""" import numpy import sys import storm_analysis.sa_library.readinsight3 as readinsight3 import storm_analysis.sa_library.ia_utilities_c as utilC if (len(sys.argv) != 4): print("usage: <true locations> <measured locations> <tolerance>") exit() # For converting XY units to nanometers. pixel_size = 160.0 truth_i3 = readinsight3.I3Reader(sys.argv[1]) measured_i3 = readinsight3.I3Reader(sys.argv[2]) tolerance = float(sys.argv[3]) recalled_locs = 0 total_locs = 0 for i in range(truth_i3.getNumberFrames()): t_locs = truth_i3.getMoleculesInFrame(i+1) m_locs = measured_i3.getMoleculesInFrame(i+1, good_only = False) dist = utilC.peakToPeakDist(t_locs['xc'], t_locs['yc'], m_locs['xc'], m_locs['yc']) * pixel_size recalled_locs += numpy.count_nonzero((dist < tolerance)) total_locs += dist.size print("Recall fraction", float(recalled_locs)/float(total_locs))
def KMeansClassifier(codebook, input_basename, output_name, extensions = [".bin", "_ch1.bin", "_ch2.bin", "_ch3.bin"], max_distance = 80): """ Note: 1. The default is that there are 4 color channels / cameras. 2. The maximum distance is in percent, so '80' means that the 20% of the localizations that most distant from a cluster center will put in category 9. """ n_channels = codebook.shape[1] assert (n_channels == len(extensions)), "Codebook size does not match data." # Create a reader for each file. i3_readers = [] for ext in extensions: i3_name = input_basename + ext print(i3_name) i3_readers.append(readinsight3.I3Reader(i3_name)) # Create writer for the results. i3_out = writeinsight3.I3Writer(output_name) # Read first block of the first channel data. i3_data = [i3_readers[0].nextBlock()] while (i3_data[0] is not False): print("working..") # Read the data from the other channels. for i in range(1,len(i3_readers)): i3_data.append(i3_readers[i].nextBlock()) # Load height data for each channel. features = numpy.zeros((i3_data[0].size, n_channels)) for i in range(len(i3_readers)): features[:,i] = i3_data[i]['h'] # Normalize by total height. total = numpy.sum(features, axis = 1) for i in range(features.shape[0]): features[i,:] = features[i,:]/total[i] # Whiten the features as recommended by Scipy. features = scipy.cluster.vq.whiten(features) # Classify using codebook. [category, distance] = scipy.cluster.vq.vq(features, codebook) dist_max = numpy.percentile(distance, max_distance) # Put top XX% in distance in category 9 (the discard category). mask = (distance > dist_max) category[mask] = 9 # # Store category and distance in the 'c' and 'i' field respectively. # i3_data[0]['c'] = category i3_data[0]['i'] = distance i3_out.addMolecules(i3_data[0]) # Load the next block of data. i3_data = [i3_readers[0].nextBlock()] # Close output file i3_out.close()