def getPixels(self, n): # 'n' is 1-based # Target 2D array img to copy data into aimg = ArrayImgs.unsignedShorts(self.dimensions[0:2]) # The number of slices of the 3D volume of a single timepoint nZ = self.img4d.dimension(2) # The slice_index if there was a single channel slice_index = int((n - 1) / 2) # 0-based, of the whole 4D series local_slice_index = slice_index % nZ # 0-based, of the timepoint 3D volume timepoint_index = int(slice_index / nZ) # Z blocks if 1 == n % 2: # Odd slice index: image channel fixedT = Views.hyperSlice(self.img4d, 3, timepoint_index) fixedZ = Views.hyperSlice(fixedT, 2, local_slice_index) w.copy(fixedZ.cursor(), aimg.cursor()) else: # Even slice index: spheres channel sd = SpheresData(self.kdtrees[timepoint_index], radius, inside, outside) volume = Views.interval(Views.raster(sd), self.dimensions3d) plane = Views.hyperSlice(volume, 2, local_slice_index) w.copy(plane.cursor(), aimg.cursor()) # return aimg.update(None).getCurrentStorageArray()
"deltaFoF_somaDiameter%0.2f.csv" % somaDiameter) 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)
def as3DVolume(kdtree, dimensions3d): sd = SpheresData(kdtree, radius, inside, outside) vol3d = Views.interval(Views.raster(sd), dimensions3d) return vol3d
self.kdtree = kdtree self.search = NearestNeighborSearchOnKDTree(kdtree) self.radius = radius self.radius_sq = radius * radius self.inside = inside self.outside = outside def copyRealRandomAccess(self): return RRA(self.numDimensions(), self.kdtree, self.radius, self.inside, self.outside) def get(self): self.search.search(self) # hilariously symmetric if self.search.getSquareDistance() < self.radius_sq: return self.inside # Same: self.kdtree.getSampler().get() return self.outside # Partial implementation, methods not needed were not implemented class Circles(RealRandomAccessible): def realRandomAccess(self): return RRA(2, kdtree, radius, inside, outside) def numDimensions(self): return 2 # 'img' here is used as the Interval within which the RRA is defined circles = Views.interval(Views.raster(Circles()), img) IL.wrap(circles, "Circles").show()
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
# create an empty image phantom = ops.create().img([xSize, ySize, zSize]) # make phantom an ImgPlus phantom = ops.create().imgPlus(phantom) location = Point(phantom.numDimensions()) location.setPosition([xSize / 2, ySize / 2, zSize / 2]) hyperSphere = HyperSphere(phantom, location, 10) for value in hyperSphere: value.setReal(100) phantom.setName("phantom") affine = AffineTransform3D() affine.scale(1, 1, 0.4) interpolatedImg = Views.interpolate(Views.extendZero(phantom), NLinearInterpolatorFactory()) phantom = Views.interval( Views.raster(RealViews.affine(interpolatedImg, affine)), Intervals.createMinMax(0, 0, 18, 255, 255, 82)) # make phantom an ImgPlus phantom = ops.create().imgPlus(ops.copy().iterableInterval( Views.zeroMin(phantom))) phantom.setName('phantom')
self.outside = outside def copyRealRandomAccess(self): return Circles(self.numDimensions(), self.kdtree, self.radius, self.inside, self.outside) def get(self): self.search.search(self) if self.search.getSquareDistance() < self.radius_squared: return self.inside return self.outside # The RealRandomAccessible that wraps the Circles in 2D space, unbounded # NOTE: partial implementation, unneeded methods were left unimplemented class CircleData(RealRandomAccessible): def realRandomAccess(self): return Circles(2, kdtree, radius, inside, outside) def numDimensions(self): return 2 # An unbounded view of the Circles that can be iterated in a grid, with integers raster = Views.raster(CircleData()) # A bounded view of the raster, within the bounds of the original 'img' # I.e. 'img' here is used as the Interval within which the CircleData is defined circles = Views.interval(raster, img) IL.wrap(circles, "Circles").show()
resultFileName = '%s/result.tif' % home.rstrip('/') imp = ImageJFunctions.wrap( result, 'result' ) IJ.saveAsTiff(imp.duplicate(), resultFileName) relativeResult = result.copy() c = relativeResult.cursor() while c.hasNext(): c.fwd() cur = c.get() val = cur.get() cur.set( val - c.getDoublePosition( 2 ) ) relativeResultFileName = '%s/relativeResult.tif' % home.rstrip('/') imp = ImageJFunctions.wrap( relativeResult, 'relative result' ) IJ.saveAsTiff(imp.duplicate(), relativeResultFileName) ratio = [ wrappedImage.dimension( 0 )*1.0/result.dimension( 0 ), wrappedImage.dimension( 1 )*1.0/result.dimension( 1 ) ] shift = [ 0.0, 0.0 ] lutField = SingleDimensionLUTGrid(3, 3, result, 2, ratio, shift ) transformed = Views.interval( Views.raster( RealViews.transformReal( Views.interpolate( Views.extendBorder( wrappedImage ), NLinearInterpolatorFactory() ), lutField ) ), wrappedImage ) imp = ImageJFunctions.wrap( transformed, 'transformed' ) transformedFileName = '%s/transformed.tif' % home.rstrip('/') IJ.saveAsTiff( imp.duplicate(), transformedFileName ) # result = inference.estimateZCoordinates( 0, 0, startingCoordinates, matrixTracker, options )