def writeData(filename, data, startIndex=0): """Write image stack to single or multiple image files Arguments: filename (str): file name as regular expression data (array): image data startIndex (int): index of first z-slice Returns: str: file name as regular expression """ #create directory if not exsits io.createDirectory(filename) #check for the \d{xx} part of the regular expression -> if not assume file header (fileheader, fileext, digitfrmt) = splitFileExpression(filename) d = len(data.shape) if d == 2: fname = fileheader + (digitfrmt % startIndex) + fileext io.writeData(fname, data) return fname else: nz = data.shape[2] for i in range(nz): fname = fileheader + (digitfrmt % (i + startIndex)) + fileext io.writeData(fname, data[:, :, i]) return filename
def writeData(filename, data, startIndex=0): """Write image stack to single or multiple image files Arguments: filename (str): file name as regular expression data (array): image data startIndex (int): index of first z-slice Returns: str: file name as regular expression """ # create directory if not exsits io.createDirectory(filename) # check for the \d{xx} part of the regular expression -> if not assume file header (fileheader, fileext, digitfrmt) = splitFileExpression(filename) d = len(data.shape) if d == 2: fname = fileheader + (digitfrmt % startIndex) + fileext io.writeData(fname, data) return fname else: nz = data.shape[2] for i in range(nz): fname = fileheader + (digitfrmt % (i + startIndex)) + fileext io.writeData(fname, data[:, :, i]) return filename
def openData3D(dataSource, x = all, y = all, z = all, cleanUp = True): """Open image in ImageJ Arguments: dataSouce (str or array): volumetric image data x, y, z (all or tuple): sub-range specification inverse (bool):invert image Returns: (object): figure handle """ checkImageJInitialized(); if isinstance(dataSource, numpy.ndarray): filename = tempfile.mktemp(suffix = '.mhd', prefix = 'CM_ImageJ'); io.writeData(filename, dataSource, x = x, y = y, z = z); filepath, imagename = os.path.split(filename); imagename = imagename[:-4] + '.raw'; temp = True; dSize = dataSource.shape; else: filename = dataSource; filepath, imagename = os.path.split(filename); temp = False; if len(dSize) == 4: colorImage = True; else: colorImage = False; if colorImage: macro = ('open("%s"); ' % filename) + \ 'run("Stack to Hyperstack...", "order=xyzct channels=%d slices=%d frames=1 display=Color"); ' % (dSize[3], dSize[2]) + \ 'Stack.setDisplayMode("composite"); ' + \ 'run("3D Viewer"); call("ij3d.ImageJ3DViewer.setCoordinateSystem", "false"); ' + \ 'call("ij3d.ImageJ3DViewer.add", "%s", "None", "%s", "0", "true", "true", "true", "1", "0");' % (imagename, imagename); else: macro = ('open("%s");' % filename) + ' run("RGB Color"); run("3D Viewer"); call("ij3d.ImageJ3DViewer.setCoordinateSystem", "false"); ' + \ 'call("ij3d.ImageJ3DViewer.add", "%s", "None", "%s", "0", "true", "true", "true", "1", "0");' % (imagename, imagename); cmd = ImageJBinary + " -eval '%s'" % macro; print 'running: %s' % cmd res = os.system(cmd); if res != 0: raise RuntimeError('openData3D: failed executing: ' + cmd); if cleanUp and temp: os.remove(filename); os.remove(os.path.join(filepath, imagename)); return macro;
def writeSubStack(filename, img, subStack = None): """Write the non-redundant part of a sub-stack to disk The routine is used to write out images when porcessed in parallel. It assumes that the filename is a patterned file name. Arguments: filename (str or None): file name pattern as described in :mod:`~ClearMap.Io.FileList`, if None return as array img (array): image data of sub-stack subStack (dict or None): sub-stack information, if None write entire image see :ref:`SubStack` Returns: str or array: the file name pattern or image """ if not subStack is None: ii = subStack["zSubStackCenterIndices"][0]; ee = subStack["zSubStackCenterIndices"][1]; si = subStack["zCenterIndices"][0]; else: si = 0; ii = 0; ee = -1; return io.writeData(filename, img[:,:,ii:ee], startIndex = si );
def overlayLabel(dataSource, labelSource, sink=None, alpha=False, labelColorMap='jet', x=all, y=all, z=all): """Overlay a gray scale image with colored labeled image Arguments: dataSouce (str or array): volumetric image data labelSource (str or array): labeled image to be overlayed on the image data sink (str or None): destination for the overlayed image alpha (float or False): transparency labelColorMap (str or object): color map for the labels x, y, z (all or tuple): sub-range specification Returns: (array or str): figure handle See Also: :func:`overlayPoints` """ label = io.readData(labelSource, x=x, y=y, z=z) image = io.readData(dataSource, x=x, y=y, z=z) lmax = labelSource.max() if lmax <= 1: carray = numpy.array([[1, 0, 0, 1]]) else: cm = mpl.cm.get_cmap(labelColorMap) cNorm = mpl.colors.Normalize(vmin=1, vmax=int(lmax)) carray = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm) carray = carray.to_rgba(numpy.arange(1, int(lmax + 1))) if alpha == False: carray = numpy.concatenate(([[0, 0, 0, 1]], carray), axis=0) else: carray = numpy.concatenate(([[1, 1, 1, 1]], carray), axis=0) cm = mpl.colors.ListedColormap(carray) carray = cm(label) carray = carray.take([0, 1, 2], axis=-1) if alpha == False: cimage = (label == 0) * image cimage = numpy.repeat(cimage, 3) cimage = cimage.reshape(image.shape + (3, )) cimage = cimage.astype(carray.dtype) cimage += carray else: cimage = numpy.repeat(image, 3) cimage = cimage.reshape(image.shape + (3, )) cimage = cimage.astype(carray.dtype) cimage *= carray return io.writeData(sink, cimage)
def test(): import os; import ClearMap.IO as io import ClearMap.Settings as settings import ClearMap.ImageProcessing.Ilastik as il; reload(il); ilp = os.path.join(settings.ClearMapPath, 'Test/Ilastik/Test.ilp') src = os.path.join(settings.ClearMapPath, 'Test/Data/ImageAnalysis/cfos-substack.tif'); #out = os.path.join(settings.ClearMapPath, 'Test/Data/Ilastik/image.npy'); out = None; #out = os.path.join(settings.ClearMapPath, 'Test/Data/Ilastik/result\d*.tif'); cls = il.classifyPixel(ilp, src, out); print io.dataSize(src) print cls.shape io.writeData('/home/ckirst/result.raw', cls);
def _cropParallel(arg): """Cropping helper function to use for parallel cropping of image slices""" fileSource = arg[0] fileSink = arg[1] x = arg[2] y = arg[3] ii = arg[4] nn = arg[5] if ii is not None: pw = ProcessWriter(ii) pw.write("cropData: corpping image %d / %d" % (ii, nn)) # pw.write('%s -> %s' % (fileSource, fileSink)); data = io.readData(fileSource, x=x, y=y) io.writeData(fileSink, data)
def test(): """Test Elastix module""" import ClearMap.Alignment.Elastix as self reload(self) from ClearMap.Settings import ClearMapPath; import os, numpy p = ClearMapPath; resultdir = os.path.join(p, 'Test/Elastix/Output'); print 'Searching for transformation parameter file in ' + resultdir; pf = self.getTransformParameterFile(resultdir) print 'Found: ' + pf; #replace path in trasform parameter files: self.setPathTransformParameterFiles(resultdir) #initialize self.initializeElastix('/home/ckirst/programs/elastix') self.printSettings() #transform points pts = numpy.random.rand(5,3); print 'Transforming points: ' tpts = self.transformPoints(pts, transformParameterFile = pf, indices = False); print pts print 'Transformed points: ' print tpts #deformation and distance fields df = self.deformationField(transformParameterFile = pf, resultDirectory = None); #df = '/tmp/elastix_output/deformationField.mhd'; import ClearMap.IO as io data = io.readData('/tmp/elastix_output/deformationField.mhd'); ds = self.deformationDistance(data); io.writeData(os.path.join(p, 'Test/Elastix/Output/distances.raw'), ds);
def test(): import os import ClearMap.IO as io import ClearMap.Settings as settings import ClearMap.ImageProcessing.Ilastik as il reload(il) ilp = os.path.join(settings.ClearMapPath, 'Test/Ilastik/Test.ilp') src = os.path.join(settings.ClearMapPath, 'Test/Data/ImageAnalysis/cfos-substack.tif') #out = os.path.join(settings.ClearMapPath, 'Test/Data/Ilastik/image.npy'); out = None #out = os.path.join(settings.ClearMapPath, 'Test/Data/Ilastik/result\d*.tif'); cls = il.classifyPixel(ilp, src, out) print io.dataSize(src) print cls.shape io.writeData('/home/ckirst/result.raw', cls)
def overlayLabel(dataSource, labelSource, sink = None, alpha = False, labelColorMap = 'jet', x = all, y = all, z = all): """Overlay a gray scale image with colored labeled image Arguments: dataSouce (str or array): volumetric image data labelSource (str or array): labeled image to be overlayed on the image data sink (str or None): destination for the overlayed image alpha (float or False): transparency labelColorMap (str or object): color map for the labels x, y, z (all or tuple): sub-range specification Returns: (array or str): figure handle See Also: :func:`overlayPoints` """ label = io.readData(labelSource, x= x, y = y, z = z); image = io.readData(dataSource, x= x, y = y, z = z); lmax = label.max(); if lmax <= 1: carray = numpy.array([[1,0,0,1]]); else: cm = mpl.cm.get_cmap(labelColorMap); cNorm = mpl.colors.Normalize(vmin=1, vmax = int(lmax)); carray = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm); carray = carray.to_rgba(numpy.arange(1, int(lmax + 1))); if alpha == False: carray = numpy.concatenate(([[0,0,0,1]], carray), axis = 0); else: carray = numpy.concatenate(([[1,1,1,1]], carray), axis = 0); cm = mpl.colors.ListedColormap(carray); carray = cm(label); carray = carray.take([0,1,2], axis = -1); if alpha == False: cimage = (label == 0) * image; cimage = numpy.repeat(cimage, 3); cimage = cimage.reshape(image.shape + (3,)); cimage = cimage.astype(carray.dtype); cimage += carray; else: cimage = numpy.repeat(image, 3); cimage = cimage.reshape(image.shape + (3,)); cimage = cimage.astype(carray.dtype); cimage *= carray; return io.writeData(sink, cimage);
def makeColorAnnotations(filename, labeledImage=None): if labeledImage is None: labeledImage = DefaultLabeledImageFile li = io.readData(labeledImage) dsize = li.shape lr = numpy.zeros(dsize, dtype=numpy.uint8) lg = lr.copy() lb = lr.copy() global Label maxlabel = max(Label.ids) colarray = numpy.zeros((maxlabel, 3)) for i in Label.ids: colarray[i - 1, :] = Label.color(i) for i in Label.ids: ll = li == i lr[ll] = colarray[i - 1, 0] lg[ll] = colarray[i - 1, 1] lb[ll] = colarray[i - 1, 2] io.writeData(filename + "_r.tif", lr) io.writeData(filename + "_g.tif", lg) io.writeData(filename + "_b.tif", lb) return (lr, lg, lb)
def makeColorAnnotations(filename, labeledImage = None): if labeledImage is None: labeledImage = DefaultLabeledImageFile; li = io.readData(labeledImage); dsize = li.shape; lr = numpy.zeros(dsize, dtype = numpy.uint8); lg = lr.copy(); lb = lr.copy(); global Label; maxlabel = max(Label.ids); colarray = numpy.zeros((maxlabel, 3)); for i in Label.ids: colarray[i-1,:] = Label.color(i); for i in Label.ids: ll = li == i; lr[ll] = colarray[i-1,0]; lg[ll] = colarray[i-1,1]; lb[ll] = colarray[i-1,2]; io.writeData(filename + "_r.tif", lr); io.writeData(filename + "_g.tif", lg); io.writeData(filename + "_b.tif", lb); return (lr,lg,lb);
def overlayPoints(dataSource, pointSource, sink=None, pointColor=[1, 0, 0], x=all, y=all, z=all): """Overlay points on 3D data and return as color image Arguments: dataSouce (str or array): volumetric image data pointSource (str or array): point data to be overlayed on the image data pointColor (array): RGB color for the overlayed points x, y, z (all or tuple): sub-range specification Returns: (str or array): image overlayed with points See Also: :func:`overlayLabel` """ data = io.readData(dataSource, x=x, y=y, z=z) points = io.readPoints(pointSource, x=x, y=y, z=z, shift=True) #print data.shape if not pointColor is None: dmax = data.max() dmin = data.min() if dmin == dmax: dmax = dmin + 1 cimage = numpy.repeat((data - dmin) / (dmax - dmin), 3) cimage = cimage.reshape(data.shape + (3, )) if data.ndim == 2: for p in points: # faster version using voxelize ? cimage[p[0], p[1], :] = pointColor elif data.ndim == 3: for p in points: # faster version using voxelize ? cimage[p[0], p[1], p[2], :] = pointColor else: raise RuntimeError( 'overlayPoints: data dimension %d not suported' % data.ndim) else: cimage = vox.voxelize(points, data.shape, voxelizationMethod='Pixel') cimage = cimage.astype(data.dtype) * data.max() data.shape = data.shape + (1, ) cimage.shape = cimage.shape + (1, ) cimage = numpy.concatenate((data, cimage), axis=3) #print cimage.shape return io.writeData(sink, cimage)
def voxelize(points, dataSize = None, sink = None, voxelizeParameter = None, method = 'Spherical', size = (5,5,5), weights = None): """Converts a list of points into an volumetric image array Arguments: points (array): point data array dataSize (tuple): size of final image sink (str, array or None): the location to write or return the resulting voxelization image, if None return array voxelizeParameter (dict): ========== ==================== =========================================================== Name Type Descritption ========== ==================== =========================================================== *method* (str or None) method for voxelization: 'Spherical', 'Rectangular' or 'Pixel' *size* (tuple) size parameter for the voxelization *weights* (array or None) weights for each point, None is uniform weights ========== ==================== =========================================================== Returns: (array): volumetric data of smeared out points """ if dataSize is None: dataSize = tuple(int(math.ceil(points[:,i].max())) for i in range(points.shape[1])); elif isinstance(dataSize, basestring): dataSize = io.dataSize(dataSize); points = io.readPoints(points); if method.lower() == 'spherical': if weights is None: data = vox.voxelizeSphere(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2]); else: data = vox.voxelizeSphereWithWeights(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2], weights); elif method.lower() == 'rectangular': if weights is None: data = vox.voxelizeRectangle(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2]); else: data = vox.voxelizeRectangleWithWeights(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2], weights); elif method.lower() == 'pixel': data = voxelizePixel(points, dataSize, weights); else: raise RuntimeError('voxelize: mode: %s not supported!' % method); return io.writeData(sink, data);
def overlayPoints(dataSource, pointSource, sink = None, pointColor = [1,0,0], x = all, y = all, z = all): """Overlay points on 3D data and return as color image Arguments: dataSouce (str or array): volumetric image data pointSource (str or array): point data to be overlayed on the image data pointColor (array): RGB color for the overlayed points x, y, z (all or tuple): sub-range specification Returns: (str or array): image overlayed with points See Also: :func:`overlayLabel` """ data = io.readData(dataSource, x = x, y = y, z = z); points = io.readPoints(pointSource, x = x, y = y, z = z, shift = True); #print data.shape if not pointColor is None: dmax = data.max(); dmin = data.min(); if dmin == dmax: dmax = dmin + 1; cimage = numpy.repeat( (data - dmin) / (dmax - dmin), 3); cimage = cimage.reshape(data.shape + (3,)); if data.ndim == 2: for p in points: # faster version using voxelize ? cimage[p[0], p[1], :] = pointColor; elif data.ndim == 3: for p in points: # faster version using voxelize ? cimage[p[0], p[1], p[2], :] = pointColor; else: raise RuntimeError('overlayPoints: data dimension %d not suported' % data.ndim); else: cimage = vox.voxelize(points, data.shape, method = 'Pixel'); cimage = cimage.astype(data.dtype) * data.max(); data.shape = data.shape + (1,); cimage.shape = cimage.shape + (1,); cimage = numpy.concatenate((data, cimage), axis = 3); #print cimage.shape return io.writeData(sink, cimage);
def deformationDistance(deformationField, sink = None, scale = None): """Compute the distance field from a deformation vector field Arguments: deformationField (str or array): source of the deformation field determined by :func:`deformationField` sink (str or None): image sink to save the deformation field to scale (tuple or None): scale factor for each dimension, if None = (1,1,1) Returns: array or str: array or file name of the transformed data """ deformationField = io.readData(deformationField); df = numpy.square(deformationField); if not scale is None: for i in range(3): df[:,:,:,i] = df[:,:,:,i] * (scale[i] * scale[i]); return io.writeData(sink, numpy.sqrt(numpy.sum(df, axis = 3)));
def deformationDistance(deformationField, sink=None, scale=None): """Compute the distance field from a deformation vector field Arguments: deformationField (str or array): source of the deformation field determined by :func:`deformationField` sink (str or None): image sink to save the deformation field to scale (tuple or None): scale factor for each dimension, if None = (1,1,1) Returns: array or str: array or file name of the transformed data """ deformationField = io.readData(deformationField) df = numpy.square(deformationField) if not scale is None: for i in range(3): df[:, :, :, i] = df[:, :, :, i] * (scale[i] * scale[i]) return io.writeData(sink, numpy.sqrt(numpy.sum(df, axis=3)))
verbose=True, center_offset=0) plt.subplot(2, 3, 5) rd.plot(ridges, normals, points, image=gray_s) #%% res = wgn.detect_contour(gray, level=140) for r in res: plt.plot(*r.T, c='m') #%% io.writeData('/home/ckirst/test4.tif', gray) #%% Gaussian kernels for derivatives sigma = 1.5 import imageprocessing.ridge_detection as rd reload(rd) n, p, evals, evecs = rd.ridge_points(gray_s, sigma) cplt.plot([gray, evals[:, :, 0], evals[:, :, 1], evals[:, :, 0] > 0], fig=3) cplt.plot([gray, n[:, :, 0], n[:, :, 1]], fig=4) #%% reload(rd)
plt.plotTiling(data, inverse=True, x=(1250, 1350), y=(550, 650), z=(25, 34)) # # background subtraction dataBGR = bgr.removeBackground(data.astype('float'), size=(5, 5), verbose=False, save=None) dataBGR_write = plt.plotTiling(dataBGR, inverse=True, x=(1250, 1350), y=(550, 650), z=(25, 34)) dataBGR_write = plt.overlayPoints(dataBGR, fileRange) mplt.pyplot.savefig(os.path.join(BaseDirectory, 'dataBGR_write.tif')) io.writeData(os.path.join(BaseDirectory, 'background_8.tif'), dataBGR_write) io.writeData(os.path.join(BaseDirectory, 'cells_check.tif'), data) pointSource = os.path.join(BaseDirectory, FilteredCellsFile[0]) data_write = plt.overlayPoints(filename, dataBGR_write, fileRange, pointColor=None) io.writeData(os.path.join(BaseDirectory, 'cells_check.tif'), data) #DoG Filter from ClearMap.ImageProcessing.Filter.DoGFilter import filterDoG dataDoG = filterDoG(dataBGR, size=(7, 7, 9), verbose=False) plt.plotTiling(dataDoG, inverse=True, x=(600, 700), y=(600, 700), z=(1, 15)) #Find Extended Maxima
tPointsFN = os.path.join(resultDir, 'Transformed_Points.npy') transformFN = os.path.join(transformDir, 'outputpoints.txt') tableFN = os.path.join(resultDir, 'ResultsTable.csv') #Atlas and Annotation transformParameterFN = os.path.join(alignDir, 'TransformParameters.1.txt') print(cfos_fn) img = io.readData(cfos_fn) img = img[..., numpy.newaxis] img = img.astype('int16') # converting data to smaller integer types can be more memory efficient! imgB = bgr.removeBackground(img, size=backgroundSize, verbose=True) io.writeData(os.path.join(resultDir, fName + '_BackgroundRemoval.tif'), imgB) ##image filter imgD = filterDoG(img, size=DoGSize, verbose=True) io.writeData(os.path.join(resultDir, fName + '_FilterDoG.tif'), imgD) #Detect Maxima imgMaxima = findExtendedMaxima(img, hMax=None, verbose=True, threshold=maximaThresh) points = findCenterOfMaxima(img, imgMaxima, verbose=True) points = points.astype('int16') #threshold intensities dataShape = detectCellShape(imgD,
tableFN = os.path.join(homeDir, 'ResultsTable.csv') atlasDir = os.path.join('/media/sf_Fred_Data/testClearMap/', orientation + 'Atlas') annoDir = os.path.join('/media/sf_Fred_Data/testClearMap/', orientation + 'Annotation') transformParameterFN = os.path.join(alignResultDir, 'TransformParameters.1.txt') img = io.readData(input_fn) img = img[..., numpy.newaxis] img = img.astype('int16') # converting data to smaller integer types can be more memory efficient! imgB = bgr.removeBackground(img, size=(8, 8), verbose=True) io.writeData(os.path.join(homeDir, 'Results/BackgroundRemoval.tif'), imgB) #image filter imgD = filterDoG(imgB, size=(15, 15, 1), verbose=True) io.writeData(os.path.join(homeDir, 'Results/FilterDoG.tif'), imgD) #Detect Maxima imgMaxima = findExtendedMaxima(imgD, hMax=None, verbose=True, threshold=3) points = findCenterOfMaxima(img, imgMaxima) points = points.astype('int16') #threshold intensities dataShape = detectCellShape(imgD, points, threshold=5) cellSizesPre = findCellSize(dataShape, maxlabel=points.shape[0]) io.writeData(os.path.join(homeDir, 'Results/CellShapes.tif'), 20 * dataShape.astype('int32'))
def transformData(source, sink=[], transformParameterFile=None, transformDirectory=None, resultDirectory=None): """Transform a raw data set to reference using the elastix alignment results If the map determined by elastix is :math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`, transformix on data works as :math:`T^{-1}(\\mathrm{data})`. Arguments: source (str or array): image source to be transformed sink (str, [] or None): image sink to save transformed image to. if [] return the default name of the data file generated by transformix. transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory. transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given. resultDirectory (str or None): the directorty for the transformix results Returns: array or str: array or file name of the transformed data """ global TransformixBinary if isinstance(source, numpy.ndarray): imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif') io.writeData(source, imgname) elif isinstance(source, basestring): if io.dataFileNameToType(source) == "TIF": imgname = source else: imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif') io.transformData(source, imgname) else: raise RuntimeError('transformData: source not a string or array') if resultDirectory == None: resultdirname = os.path.join(tempfile.gettempdir(), 'elastix_output') else: resultdirname = resultDirectory if not os.path.exists(resultdirname): os.makedirs(resultdirname) if transformParameterFile == None: if transformDirectory == None: raise RuntimeError( 'neither alignment directory and transformation parameter file specified!' ) transformparameterdir = transformDirectory transformParameterFile = getTransformParameterFile( transformparameterdir) else: transformparameterdir = os.path.split(transformParameterFile) transformparameterdir = transformparameterdir[0] #transform #make path in parameterfiles absolute setPathTransformParameterFiles(transformparameterdir) #transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt cmd = TransformixBinary + ' -threads 8 -in ' + imgname + ' -out ' + resultdirname + ' -tp ' + transformParameterFile res = os.system(cmd) if res != 0: raise RuntimeError('transformData: failed executing: ' + cmd) if not isinstance(source, basestring): os.remove(imgname) if sink == []: return getResultDataFile(resultdirname) elif sink is None: resultfile = getResultDataFile(resultdirname) return io.readData(resultfile) elif isinstance(sink, basestring): resultfile = getResultDataFile(resultdirname) return io.convertData(resultfile, sink) else: raise RuntimeError('transformData: sink not valid!')
plt.subplot(2,3,5); rd.plot(ridges, normals, points, image = gray_s); #%% res = wgn.detect_contour(gray, level = 140) for r in res: plt.plot(*r.T, c = 'm'); #%% io.writeData('/home/ckirst/test4.tif', gray); #%% Gaussian kernels for derivatives sigma = 1.5; import imageprocessing.ridge_detection as rd; reload(rd); n,p,evals,evecs = rd.ridge_points(gray_s, sigma); cplt.plot([gray, evals[:,:,0], evals[:,:,1], evals[:,:,0]> 0], fig = 3) cplt.plot([gray, n[:,:,0], n[:,:,1]], fig = 4)
def output_analysis_helper(threshold=(20, 900), row=(3, 3), **params): ''' Function to change elastix result directory before running 'step 6' i.e. point transformix to atlas. ''' dct = pth_update(set_parameters_for_clearmap(**params)) dct['RegistrationAlignmentParameter']["resultDirectory"] = os.path.join( params["outputdirectory"], 'clearmap_cluster_output/elastix_auto_to_sim_atlas') points, intensities = io.readPoints( dct['ImageProcessingParameter']["sink"]) #Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" #row = (0,0) : peak intensity from the raw data #row = (1,1) : peak intensity from the DoG filtered data #row = (2,2) : peak intensity from the background subtracted data #row = (3,3) : voxel size from the watershed points, intensities = thresholdPoints(points, intensities, threshold=threshold, row=row) #points, intensities = thresholdPoints(points, intensities, threshold = (20, 900), row = (2,2)); io.writePoints(dct['FilteredCellsFile'], (points, intensities)) # Transform point coordinates ############################# points = io.readPoints( dct['CorrectionResamplingPointsParameter']["pointSource"]) points = resamplePoints(**dct['CorrectionResamplingPointsParameter']) points = transformPoints( points, transformDirectory=dct['CorrectionAlignmentParameter'] ["resultDirectory"], indices=False, resultDirectory=None) dct['CorrectionResamplingPointsInverseParameter']["pointSource"] = points points = resamplePointsInverse( **dct['CorrectionResamplingPointsInverseParameter']) dct['RegistrationResamplingPointParameter']["pointSource"] = points points = resamplePoints(**dct['RegistrationResamplingPointParameter']) points = transformPoints( points, transformDirectory=dct['RegistrationAlignmentParameter'] ["resultDirectory"], indices=False, resultDirectory=None) io.writePoints(dct['TransformedCellsFile'], points) # Heat map generation ##################### points = io.readPoints(dct['TransformedCellsFile']) intensities = io.readPoints(dct['FilteredCellsFile'][1]) #Without weigths: vox = voxelize(points, dct['AtlasFile'], **dct['voxelizeParameter']) if not isinstance(vox, basestring): io.writeData(os.path.join(dct['OutputDirectory'], 'cells_heatmap.tif'), vox.astype('int32')) #With weigths from the intensity file (here raw intensity): dct['voxelizeParameter']["weights"] = intensities[:, 0].astype(float) vox = voxelize(points, dct['AtlasFile'], **dct['voxelizeParameter']) if not isinstance(vox, basestring): io.writeData( os.path.join(dct['OutputDirectory'], 'cells_heatmap_weighted.tif'), vox.astype('int32')) #Table generation: ################## #With integrated weigths from the intensity file (here raw intensity): ids, counts = countPointsInRegions(points, labeledImage=dct['AnnotationFile'], intensities=intensities, intensityRow=0) table = np.zeros(ids.shape, dtype=[('id', 'int64'), ('counts', 'f8'), ('name', 'a256')]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable( os.path.join(dct['OutputDirectory'], 'Annotated_counts_intensities.csv'), table) #Without weigths (pure cell number): ids, counts = countPointsInRegions(points, labeledImage=dct['AnnotationFile'], intensities=None) table = np.zeros(ids.shape, dtype=[('id', 'int64'), ('counts', 'f8'), ('name', 'a256')]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable(os.path.join(dct['OutputDirectory'], 'Annotated_counts.csv'), table) print('Analysis Completed') return
def cropData(source, sink=None, x=all, y=all, z=all, adjustOverlap=False, verbose=True, processes=all): """Crop source from start to stop point Arguments: source (str or array): filename or data array of source sink (str or None): filename or sink x,y,z (tuple or all): the range to crop the data to adjustOverlap (bool): correct overlap meta data if exists Return: str or array: array or filename with cropped data """ if sink is None: return readDataFiles(source, x=x, y=y, z=z) else: # sink assumed to be file expression if not io.isFileExpression(sink): raise RuntimeError("cropping data to different format not supported!") fileheader, fileext, digitfrmt = splitFileExpression(sink) # read first image to get data size and type fp, fl = readFileList(source) nz = len(fl) rz = io.toDataRange(nz, r=z) if adjustOverlap: # change overlap in first file try: fn = os.path.join(fp, fl[0]) info = io.readMetaData(fn, info=["description", "overlap", "resolution"]) description = str(info["description"]) overlap = numpy.array(info["overlap"], dtype=float) resolution = numpy.array(info["resolution"], dtype=float) except: raise RuntimeWarning("could not modify overlap!") fullsize = io.dataSize(fn) data = io.readData(fn, x=x, y=y) # overlap in pixels poverlap = overlap[:2] / resolution[:2] print poverlap # cropped pixel xr = io.toDataRange(fullsize[0], r=x) yr = io.toDataRange(fullsize[1], r=y) print xr print yr print fullsize poverlap[0] = poverlap[0] - xr[0] - (fullsize[0] - xr[1]) poverlap[1] = poverlap[1] - yr[0] - (fullsize[1] - yr[1]) print poverlap # new overlap in microns overlap = poverlap * resolution[:2] # check for consistency if numpy.abs(fullsize[0] - xr[1] - xr[0]) > 1 or numpy.abs(fullsize[1] - yr[1] - yr[0]) > 1: raise RuntimeWarning("cropping is inconsistent with overlap )modification!") # change image description import ClearMap.IO.TIF as CMTIF description = CMTIF.changeOMEMetaDataString(description, {"overlap": overlap}) print len(description) # write first file fnout = fileheader + (digitfrmt % 0) + fileext io.writeData(fnout, data, info=description) zr = range(rz[0] + 1, rz[1]) else: zr = range(rz[0], rz[1]) print zr nZ = len(zr) if processes is None: processes = 1 if processes is all: processes = multiprocessing.cpu_count() if processes > 1: # parallel processing pool = multiprocessing.Pool(processes=processes) argdata = [] for i, z in enumerate(zr): if verbose: argdata.append( (os.path.join(fp, fl[z]), fileheader + (digitfrmt % (i + 1)) + fileext, x, y, (i + 1), (nZ + 1)) ) else: argdata.append( (os.path.join(fp, fl[z]), fileheader + (digitfrmt % (i + 1)) + fileext, x, y, None, None) ) pool.map(_cropParallel, argdata) else: # sequential processing for i, z in enumerate(zr): if verbose: print "cropData: corpping image %d / %d" % (i + 1, nZ + 1) fileSource = os.path.join(fp, fl[z]) data = io.readData(fileSource, x=x, y=y) fileSink = fileheader + (digitfrmt % (i + 1)) + fileext io.writeData(fileSink, data) return sink
def transformData(source, sink = [], transformParameterFile = None, transformDirectory = None, resultDirectory = None): """Transform a raw data set to reference using the elastix alignment results If the map determined by elastix is :math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`, transformix on data works as :math:`T^{-1}(\\mathrm{data})`. Arguments: source (str or array): image source to be transformed sink (str, [] or None): image sink to save transformed image to. if [] return the default name of the data file generated by transformix. transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory. transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given. resultDirectory (str or None): the directorty for the transformix results Returns: array or str: array or file name of the transformed data """ global TransformixBinary; if isinstance(source, numpy.ndarray): imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif'); io.writeData(source, imgname); elif isinstance(source, basestring): if io.dataFileNameToType(source) == "TIF": imgname = source; else: imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif'); io.transformData(source, imgname); else: raise RuntimeError('transformData: source not a string or array'); if resultDirectory == None: resultdirname = os.path.join(tempfile.tempdir, 'elastix_output'); else: resultdirname = resultDirectory; if not os.path.exists(resultdirname): os.makedirs(resultdirname); if transformParameterFile == None: if transformDirectory == None: raise RuntimeError('neither alignment directory and transformation parameter file specified!'); transformparameterdir = transformDirectory transformParameterFile = getTransformParameterFile(transformparameterdir); else: transformparameterdir = os.path.split(transformParameterFile); transformparameterdir = transformparameterdir[0]; #transform #make path in parameterfiles absolute setPathTransformParameterFiles(transformparameterdir); #transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt cmd = TransformixBinary + ' -in ' + imgname + ' -out ' + resultdirname + ' -tp ' + transformParameterFile; res = os.system(cmd); if res != 0: raise RuntimeError('transformData: failed executing: ' + cmd); if not isinstance(source, basestring): os.remove(imgname); if sink == []: return getResultDataFile(resultdirname); elif sink is None: resultfile = getResultDataFile(resultdirname); return io.readData(resultfile); elif isinstance(sink, basestring): resultfile = getResultDataFile(resultdirname); return io.convertData(resultfile, sink); else: raise RuntimeError('transformData: sink not valid!');
def output_analysis( threshold=(20, 900), row=(3, 3), check_cell_detection=False, **params): """Wrapper for analysis: Inputs ------------------- Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" Row: row = (0,0) : peak intensity from the raw data row = (1,1) : peak intensity from the DoG filtered data row = (2,2) : peak intensity from the background subtracted data row = (3,3) : voxel size from the watershed Check Cell detection: (For the testing phase only, remove when running on the full size dataset) """ dct = pth_update(set_parameters_for_clearmap(**params)) points, intensities = io.readPoints( dct["ImageProcessingParameter"]["sink"]) #Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" #row = (0,0) : peak intensity from the raw data #row = (1,1) : peak intensity from the DoG filtered data #row = (2,2) : peak intensity from the background subtracted data #row = (3,3) : voxel size from the watershed points, intensities = thresholdPoints(points, intensities, threshold=threshold, row=row) #points, intensities = thresholdPoints(points, intensities, threshold = (20, 900), row = (2,2)); io.writePoints(dct["FilteredCellsFile"], (points, intensities)) ## Check Cell detection (For the testing phase only, remove when running on the full size dataset) ####################### # if check_cell_detection: # import ClearMap.Visualization.Plot as plt # pointSource= os.path.join(BaseDirectory, FilteredCellsFile[0]); # data = plt.overlayPoints(cFosFile, pointSource, pointColor = None, **cFosFileRange); # io.writeData(os.path.join(BaseDirectory, "cells_check.tif"), data); # Transform point coordinates ############################# points = io.readPoints( dct["CorrectionResamplingPointsParameter"]["pointSource"]) points = resamplePoints(**dct["CorrectionResamplingPointsParameter"]) points = transformPoints( points, transformDirectory=dct["CorrectionAlignmentParameter"] ["resultDirectory"], indices=False, resultDirectory=None) dct["CorrectionResamplingPointsInverseParameter"]["pointSource"] = points points = resamplePointsInverse( **dct["CorrectionResamplingPointsInverseParameter"]) dct["RegistrationResamplingPointParameter"]["pointSource"] = points points = resamplePoints(**dct["RegistrationResamplingPointParameter"]) points = transformPoints( points, transformDirectory=dct["RegistrationAlignmentParameter"] ["resultDirectory"], indices=False, resultDirectory=None) io.writePoints(dct["TransformedCellsFile"], points) # Heat map generation ##################### points = io.readPoints(dct["TransformedCellsFile"]) intensities = io.readPoints(dct["FilteredCellsFile"][1]) #Without weigths: vox = voxelize(points, dct["AtlasFile"], **dct["voxelizeParameter"]) if not isinstance(vox, str): io.writeData(os.path.join(dct["OutputDirectory"], "cells_heatmap.tif"), vox.astype("int32")) #With weigths from the intensity file (here raw intensity): dct["voxelizeParameter"]["weights"] = intensities[:, 0].astype(float) vox = voxelize(points, dct["AtlasFile"], **dct["voxelizeParameter"]) if not isinstance(vox, str): io.writeData( os.path.join(dct["OutputDirectory"], "cells_heatmap_weighted.tif"), vox.astype("int32")) #Table generation: ################## #With integrated weigths from the intensity file (here raw intensity): try: ids, counts = countPointsInRegions(points, labeledImage=dct["AnnotationFile"], intensities=intensities, intensityRow=0) table = numpy.zeros(ids.shape, dtype=[("id", "int64"), ("counts", "f8"), ("name", "a256")]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable( os.path.join(dct["OutputDirectory"], "Annotated_counts_intensities.csv"), table) #Without weigths (pure cell number): ids, counts = countPointsInRegions(points, labeledImage=dct["AnnotationFile"], intensities=None) table = numpy.zeros(ids.shape, dtype=[("id", "int64"), ("counts", "f8"), ("name", "a256")]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable( os.path.join(dct["OutputDirectory"], "Annotated_counts.csv"), table) except: print("Table not generated.\n") print("Analysis Completed") return