peaks = peak_map[somaDiameter] # Measure intensity over time, for every peak # by averaging the signal within a radius of each peak. measurement_radius_px = [ (somaDiameter / calibration[d]) * 0.66 for d in xrange(3) ] # NOTE: DIVIDING BY 3, not 2, to make the radius a bit smaller spheres = [ ClosedWritableEllipsoid([peak.getFloatPosition(d) for d in xrange(3)], measurement_radius_px) for peak in peaks ] insides = [ Regions.iterable( Views.interval(Views.raster(Masks.toRealRandomAccessible(sphere)), Intervals.largestContainedInterval(sphere))) for sphere in spheres ] count = float(Regions.countTrue(insides[0])) # same for all def measurePeaks(filename, retry=0): img3D = klb.readFull(os.path.join(srcDir, filename)) """ mean_intensities = [] print Intervals.dimensionsAsLongArray(img3D) for inside, peak in zip(insides, peaks): samples = Regions.sample(inside, img3D) #print type(samples) #print Intervals.dimensionsAsLongArray(samples)
def measureFluorescence(series_name, img4D, mask=None): csv_fluorescence = os.path.join(srcDir, "%s_fluorescence.csv" % series_name) if not os.path.exists(csv_fluorescence): # Generate projection over time (the img3D) and extract peaks with difference of Gaussian using the params # (Will check if file for projection over time exists and just load it) img3D_filepath = os.path.join( srcDir, "%s_4D-to-3D_max_projection.zip" % series_name) img3D, peaks, spheresRAI, impSpheres = findNucleiByMaxProjection( img4D, params, img3D_filepath, show=True) comp = showAsComposite([wrap(img3D), impSpheres]) # Measure intensity over time, for every peak # by averaging the signal within a radius of each peak. measurement_radius = somaDiameter / 3.0 spheres = [ ClosedWritableSphere([peak.getFloatPosition(d) for d in xrange(3)], measurement_radius) for peak in peaks ] insides = [ Regions.iterable( Views.interval( Views.raster(Masks.toRealRandomAccessible(sphere)), Intervals.largestContainedInterval(sphere))) for sphere in spheres ] count = float(Regions.countTrue(insides[0])) # same for all measurements = [] with open(csv_fluorescence, 'w') as csvfile: w = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_NONNUMERIC) # Header: with peak coordinates w.writerow(["timepoint"] + [ "%.2f::%.2f::%.2f" % tuple(peak.getFloatPosition(d) for d in xrange(3)) for peak in peaks ]) # Each time point for t in xrange(img4D.dimension(3)): img3D = Views.hyperSlice(img4D, 3, t) mean_intensities = array( ((sum(t.get() for t in Regions.sample(inside, img3D)) / count) for inside in insides), 'f') w.writerow([t] + mean_intensities.tolist()) measurements.append(mean_intensities) else: # Parse CSV file with open(csv_fluorescence, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"') header = reader.next() # Parse header, containing peak locations peaks = [ RealPoint.wrap(map(float, h.split("::"))) for h in islice(header, 1, None) ] # Parse rows measurements = [map(float, islice(row, 1, None)) for row in reader] return peaks, measurements