Esempio n. 1
0
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 = np.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(np.arange(1, int(lmax + 1)))

    if alpha == False:
        carray = np.concatenate(([[0, 0, 0, 1]], carray), axis=0)
    else:
        carray = np.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 = np.repeat(cimage, 3)
        cimage = cimage.reshape(image.shape + (3, ))
        cimage = cimage.astype(carray.dtype)
        cimage += carray
    else:
        cimage = np.repeat(image, 3)
        cimage = cimage.reshape(image.shape + (3, ))
        cimage = cimage.astype(carray.dtype)
        cimage *= carray

    return io.writeData(sink, cimage)
Esempio n. 2
0
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 = np.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 = np.concatenate((data, cimage), axis=3)

    #print cimage.shape
    return io.writeData(sink, cimage)
Esempio n. 3
0
def resampleXY(source,
               dataSizeSink,
               sink=None,
               interpolation='linear',
               out=sys.stdout,
               verbose=True):
    """Resample a 2d image slice
    
    This routine is used for resampling a large stack in parallel in xy or xz direction.
    
    Arguments:
        source (str or array): 2d image source
        dataSizeSink (tuple): size of the resmapled image
        sink (str or None): location for the resmapled image
        interpolation (str): interpolation method to use: 'linear' or None (nearest pixel)
        out (stdout): where to write progress information
        vebose (bool): write progress info if true
    
    Returns:
        array or str: resampled data or file name
    """

    #out.write("Input: %s Output: " % (inputFile, soutputFile))
    data = io.readData(source)
    dataSize = data.shape

    #print dataSize, dataSizeSink

    if data.ndim != 2:
        raise RuntimeError('resampleXY: expects 2d image source, found %dd' %
                           data.ndim)
    #print sagittalImageSize;

    #dataSizeSink = tuple([int(math.ceil(dataSize[i] *  resolutionSource[i]/resolutionSink[i])) for i in range(2)]);
    if verbose:
        out.write(("resampleData: Imagesize: %d, %d " %
                   (dataSize[0], dataSize[1])) +
                  ("Resampled Imagesize: %d, %d" %
                   (dataSizeSink[0], dataSizeSink[1])))
        #out.write(("resampleData: Imagesize: %d, %d " % dataSize) + ("Resampled Imagesize: %d, %d" % (outputSize[1], outputSize[0])))

    # note: cv2.resize reverses x-Y axes
    interpolation = fixInterpolation(interpolation)
    sinkData = cv2.resize(data, (dataSizeSink[1], dataSizeSink[0]),
                          interpolation=interpolation)
    #sinkData = cv2.resize(data,  outputSize);
    #sinkData = scipy.misc.imresize(sagittalImage, outputImageSize, interp = 'bilinear'); #normalizes images -> not usefull for stacks !

    #out.write("resampleData: resized Image size: %d, %d " % sinkData.shape)

    return io.writeData(sink, sinkData)
Esempio n. 4
0
def readImportPreview(importResult):
  """Returns the prestiched preview file to check orientations and coarse alignment
  
  Arguments:
    importResult (str): the base directory of the image data or the import xml file
  
  Returns:
    array: preview image
  """
  
  baseDirectory = baseDirectoryFromXMLImport(importResult) 
  if baseDirectory is None:
    baseDirectory = importResult;

  fn = os.path.join(baseDirectory, 'test_middle_slice.tif');
  return io.readData(fn);
Esempio n. 5
0
def _resampleXYParallel(arg):
    """Resampling helper function to use for parallel resampling of image slices"""
    
    fileSource = arg[0];
    fileSink = arg[1];
    dataSizeSink = arg[2];
    interpolation = arg[3];
    ii = arg[4];
    nn = arg[5];
    verbose = arg[6];
    
    pw = ProcessWriter(ii);
    if verbose:
        pw.write("resampleData: resampling in XY: image %d / %d" % (ii, nn))
    
    data = numpy.squeeze(io.readData(fileSource, z = ii));
    resampleXY(data, sink = fileSink, dataSizeSink = dataSizeSink, interpolation = interpolation, out = pw, verbose = verbose);
Esempio n. 6
0
def _resampleXYParallel(arg):
    """Resampling helper function to use for parallel resampling of image slices"""
    
    fileSource = arg[0];
    fileSink = arg[1];
    dataSizeSink = arg[2];
    interpolation = arg[3];
    ii = arg[4];
    nn = arg[5];
    verbose = arg[6];
    
    pw = ProcessWriter(ii);
    if verbose:
        pw.write("resampleData: resampling in XY: image %d / %d" % (ii, nn))
    
    data = numpy.squeeze(io.readData(fileSource, z = ii));
    resampleXY(data, sink = fileSink, dataSizeSink = dataSizeSink, interpolation = interpolation, out = pw, verbose = verbose);
Esempio n. 7
0
def resampleXY(source, dataSizeSink, sink = None, interpolation = 'linear', out = sys.stdout, verbose = True):
    """Resample a 2d image slice
    
    This routine is used for resampling a large stack in parallel in xy or xz direction.
    
    Arguments:
        source (str or array): 2d image source
        dataSizeSink (tuple): size of the resmapled image
        sink (str or None): location for the resmapled image
        interpolation (str): interpolation method to use: 'linear' or None (nearest pixel)
        out (stdout): where to write progress information
        vebose (bool): write progress info if true
    
    Returns:
        array or str: resampled data or file name
    """   
    
    #out.write("Input: %s Output: " % (inputFile, soutputFile))
    data = io.readData(source);
    dataSize = data.shape;
    
    #print dataSize, dataSizeSink    
    
    if data.ndim != 2:
        raise RuntimeError('resampleXY: expects 2d image source, found %dd' % data.ndim)
    #print sagittalImageSize;
    
    #dataSizeSink = tuple([int(math.ceil(dataSize[i] *  resolutionSource[i]/resolutionSink[i])) for i in range(2)]);
    if verbose:
        out.write(("resampleData: Imagesize: %d, %d " % (dataSize[0], dataSize[1])) + ("Resampled Imagesize: %d, %d" % (dataSizeSink[0], dataSizeSink[1])))
        #out.write(("resampleData: Imagesize: %d, %d " % dataSize) + ("Resampled Imagesize: %d, %d" % (outputSize[1], outputSize[0])))
    
    # note: cv2.resize reverses x-Y axes
    interpolation = fixInterpolation(interpolation)
    sinkData = cv2.resize(data,  (dataSizeSink[1], dataSizeSink[0]), interpolation = interpolation);
    #sinkData = cv2.resize(data,  outputSize);
    #sinkData = scipy.misc.imresize(sagittalImage, outputImageSize, interp = 'bilinear'); #normalizes images -> not usefull for stacks !
    
    #out.write("resampleData: resized Image size: %d, %d " % sinkData.shape)
    
    return io.writeData(sink, sinkData);
Esempio n. 8
0
def sagittalToCoronalData(source, sink = None):
    """Change from saggital to coronal orientation
     
    Arguments:
        source (str or array): source data to be reoriented
        sink (str or None): destination for reoriented image
    
    Returns:
        str or array: reoriented data
    """
      
    source = io.readData(source);
    d = source.ndim;
    if d < 3:
        raise RuntimeError('sagittalToCoronalData: 3d image required!');
    
    tp = range(d);
    tp[0:3] = [2,0,1];
    source = source.transpose(tp);
    source = source[::-1];
    #source = source[::-1,:,:];
    return io.writeData(sink, source);
Esempio n. 9
0
def sagittalToCoronalData(source, sink=None):
    """Change from saggital to coronal orientation
     
    Arguments:
        source (str or array): source data to be reoriented
        sink (str or None): destination for reoriented image
    
    Returns:
        str or array: reoriented data
    """

    source = io.readData(source)
    d = source.ndim
    if d < 3:
        raise RuntimeError('sagittalToCoronalData: 3d image required!')

    tp = range(d)
    tp[0:3] = [2, 0, 1]
    source = source.transpose(tp)
    source = source[::-1]
    #source = source[::-1,:,:];
    return io.writeData(sink, source)
Esempio n. 10
0
def resampleData(source,
                 sink=None,
                 orientation=None,
                 dataSizeSink=None,
                 resolutionSource=(4.0625, 4.0625, 3),
                 resolutionSink=(25, 25, 25),
                 processingDirectory=None,
                 processes=1,
                 cleanup=True,
                 verbose=True,
                 interpolation='linear',
                 **args):
    """Resample data of source in resolution and orientation
    
    Arguments:
        source (str or array): image to be resampled
        sink (str or None): destination of resampled image
        orientation (tuple): orientation specified by permuation and change in sign of (1,2,3)
        dataSizeSink (tuple or None): target size of the resampled image
        resolutionSource (tuple): resolution of the source image (in length per pixel)
        resolutionSink (tuple): resolution of the resampled image (in length per pixel)
        processingDirectory (str or None): directory in which to perform resmapling in parallel, None a temporary directry will be created
        processes (int): number of processes to use for parallel resampling
        cleanup (bool): remove temporary files
        verbose (bool): display progress information
        interpolation (str): method to use for interpolating to the resmapled image
    
    Returns:
        (array or str): data or file name of resampled image

    Notes: 
        * resolutions are assumed to be given for the axes of the intrinsic 
          orientation of the data and reference as when viewed by matplotlib or ImageJ
        * orientation: permuation of 1,2,3 with potential sign, indicating which 
          axes map onto the reference axes, a negative sign indicates reversal 
          of that particular axes
        * only a minimal set of information to detremine the resampling parameter 
          has to be given, e.g. dataSizeSource and dataSizeSink
    """

    orientation = fixOrientation(orientation)

    if isinstance(dataSizeSink, basestring):
        dataSizeSink = io.dataSize(dataSizeSink)

    #orient actual resolutions onto reference resolution
    dataSizeSource = io.dataSize(source)

    dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize(
        dataSizeSource=dataSizeSource,
        dataSizeSink=dataSizeSink,
        resolutionSource=resolutionSource,
        resolutionSink=resolutionSink,
        orientation=orientation)

    dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation)

    #print dataSizeSource, dataSizeSink, resolutionSource, resolutionSink, dataSizeSinkI

    #rescale in x y in parallel
    if processingDirectory == None:
        processingDirectory = tempfile.mkdtemp()

    interpolation = fixInterpolation(interpolation)

    nZ = dataSizeSource[2]
    pool = multiprocessing.Pool(processes=processes)
    argdata = []
    for i in range(nZ):
        argdata.append(
            (source, os.path.join(processingDirectory,
                                  'resample_%04d.tif' % i), dataSizeSinkI,
             interpolation, i, nZ, verbose))
        #print argdata[i]
    pool.map(_resampleXYParallel, argdata)

    #rescale in z
    fn = os.path.join(processingDirectory, 'resample_%04d.tif' % 0)
    data = io.readData(fn)
    zImage = numpy.zeros((dataSizeSinkI[0], dataSizeSinkI[1], nZ),
                         dtype=data.dtype)
    for i in range(nZ):
        if verbose and i % 10 == 0:
            print "resampleData; reading %d/%d" % (i, nZ)
        fn = os.path.join(processingDirectory, 'resample_%04d.tif' % i)
        zImage[:, :, i] = io.readData(fn)

    resampledData = numpy.zeros(dataSizeSinkI, dtype=zImage.dtype)

    for i in range(dataSizeSinkI[0]):
        if verbose and i % 25 == 0:
            print "resampleData: processing %d/%d" % (i, dataSizeSinkI[0])
        #resampledImage[:, iImage ,:] =  scipy.misc.imresize(zImage[:,iImage,:], [resizedZAxisSize, sagittalImageSize[1]] , interp = 'bilinear');
        #cv2.resize takes reverse order of sizes !
        resampledData[i, :, :] = cv2.resize(
            zImage[i, :, :], (dataSizeSinkI[2], dataSizeSinkI[1]),
            interpolation=interpolation)
        #resampledData[i ,:, :] =  cv2.resize(zImage[i,:, :], (dataSize[1], resizedZSize));

    #account for using (z,y,x) array representation -> (y,x,z)
    #resampledData = resampledData.transpose([1,2,0]);
    #resampledData = resampledData.transpose([2,1,0]);

    if cleanup:
        shutil.rmtree(processingDirectory)

    if not orientation is None:

        #reorient
        per = orientationToPermuation(orientation)
        resampledData = resampledData.transpose(per)

        #reverse orientation after permuting e.g. (-2,1) brings axis 2 to first axis and we can reorder there
        if orientation[0] < 0:
            resampledData = resampledData[::-1, :, :]
        if orientation[1] < 0:
            resampledData = resampledData[:, ::-1, :]
        if orientation[2] < 0:
            resampledData = resampledData[:, :, ::-1]

        #bring back from y,x,z to z,y,x
        #resampledImage = resampledImage.transpose([2,0,1]);
    if verbose:
        print "resampleData: resampled data size: " + str(resampledData.shape)

    if sink == []:
        if io.isFileExpression(source):
            sink = os.path.split(source)
            sink = os.path.join(sink[0], 'resample_\d{4}.tif')
        elif isinstance(source, basestring):
            sink = source + '_resample.tif'
        else:
            raise RuntimeError(
                'resampleData: automatic sink naming not supported for non string source!'
            )

    return io.writeData(sink, resampledData)
Esempio n. 11
0
def resampleDataInverse(sink, source = None, dataSizeSource = None, orientation = None, resolutionSource = (4.0625, 4.0625, 3), resolutionSink = (25, 25, 25), 
                        processingDirectory = None, processes = 1, cleanup = True, verbose = True, interpolation = 'linear', **args):
    """Resample data inversely to :func:`resampleData` routine
    
    Arguments:
        sink (str or None): image to be inversly resampled (=sink in :func:`resampleData`)
        source (str or array): destination for inversly resmapled image (=source in :func:`resampleData`)
        dataSizeSource (tuple or None): target size of the resampled image
        orientation (tuple): orientation specified by permuation and change in sign of (1,2,3)
        resolutionSource (tuple): resolution of the source image (in length per pixel)
        resolutionSink (tuple): resolution of the resampled image (in length per pixel)
        processingDirectory (str or None): directory in which to perform resmapling in parallel, None a temporary directry will be created
        processes (int): number of processes to use for parallel resampling
        cleanup (bool): remove temporary files
        verbose (bool): display progress information
        interpolation (str): method to use for interpolating to the resmapled image
    
    Returns:
        (array or str): data or file name of resampled image

    Notes: 
        * resolutions are assumed to be given for the axes of the intrinsic 
          orientation of the data and reference as when viewed by matplotlib or ImageJ
        * orientation: permuation of 1,2,3 with potential sign, indicating which 
          axes map onto the reference axes, a negative sign indicates reversal 
          of that particular axes
        * only a minimal set of information to detremine the resampling parameter 
          has to be given, e.g. dataSizeSource and dataSizeSink
    """    
    
    
    #orientation
    orientation = fixOrientation(orientation);
    
    #assume we can read data fully into memory
    resampledData = io.readData(sink);

    dataSizeSink = resampledData.shape;
    
    if isinstance(dataSizeSource, basestring):
        dataSizeSource = io.dataSize(dataSizeSource);

    dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize(dataSizeSource = dataSizeSource, dataSizeSink = dataSizeSink, 
                                                                                      resolutionSource = resolutionSource, resolutionSink = resolutionSink, orientation = orientation);

    #print (dataSizeSource, dataSizeSink, resolutionSource, resolutionSink )
    
    dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation);
    
    
    #flip axes back and permute inversely
    if not orientation is None:
        if orientation[0] < 0:
            resampledData = resampledData[::-1, :, :];
        if orientation[1] < 0:
            resampledData = resampledData[:, ::-1, :]; 
        if orientation[2] < 0:
            resampledData = resampledData[:, :, ::-1];

        
        #reorient
        peri = inverseOrientation(orientation);
        peri = orientationToPermuation(peri);
        resampledData = resampledData.transpose(peri);
    
    # upscale in z
    interpolation = fixInterpolation(interpolation);
    
    resampledDataXY = numpy.zeros((dataSizeSinkI[0], dataSizeSinkI[1], dataSizeSource[2]), dtype = resampledData.dtype);    
    
    for i in range(dataSizeSinkI[0]):
        if verbose and i % 25 == 0:
            print "resampleDataInverse: processing %d/%d" % (i, dataSizeSinkI[0])

        #cv2.resize takes reverse order of sizes !
        resampledDataXY[i ,:, :] =  cv2.resize(resampledData[i,:,:], (dataSizeSource[2], dataSizeSinkI[1]), interpolation = interpolation);

    # upscale x, y in parallel
    
    if io.isFileExpression(source):
        files = source;
    else:
        if processingDirectory == None:
            processingDirectory = tempfile.mkdtemp();   
        files = os.path.join(sink[0], 'resample_\d{4}.tif');
    
    io.writeData(files, resampledDataXY);
    
    nZ = dataSizeSource[2];
    pool = multiprocessing.Pool(processes=processes);
    argdata = [];
    for i in range(nZ):
        argdata.append( (source, fl.fileExpressionToFileName(files, i), dataSizeSource, interpolation, i, nZ) );  
    pool.map(_resampleXYParallel, argdata);
    
    if io.isFileExpression(source):
        return source;
    else:
        data = io.convertData(files, source);
        
        if cleanup:
            shutil.rmtree(processingDirectory);
        
        return data;
Esempio n. 12
0
def resampleData(source, sink = None,  orientation = None, dataSizeSink = None, resolutionSource = (4.0625, 4.0625, 3), resolutionSink = (25, 25, 25), 
                 processingDirectory = None, processes = 1, cleanup = True, verbose = True, interpolation = 'linear', **args):
    """Resample data of source in resolution and orientation
    
    Arguments:
        source (str or array): image to be resampled
        sink (str or None): destination of resampled image
        orientation (tuple): orientation specified by permuation and change in sign of (1,2,3)
        dataSizeSink (tuple or None): target size of the resampled image
        resolutionSource (tuple): resolution of the source image (in length per pixel)
        resolutionSink (tuple): resolution of the resampled image (in length per pixel)
        processingDirectory (str or None): directory in which to perform resmapling in parallel, None a temporary directry will be created
        processes (int): number of processes to use for parallel resampling
        cleanup (bool): remove temporary files
        verbose (bool): display progress information
        interpolation (str): method to use for interpolating to the resmapled image
    
    Returns:
        (array or str): data or file name of resampled image

    Notes: 
        * resolutions are assumed to be given for the axes of the intrinsic 
          orientation of the data and reference as when viewed by matplotlib or ImageJ
        * orientation: permuation of 1,2,3 with potential sign, indicating which 
          axes map onto the reference axes, a negative sign indicates reversal 
          of that particular axes
        * only a minimal set of information to detremine the resampling parameter 
          has to be given, e.g. dataSizeSource and dataSizeSink
    """
        
    orientation = fixOrientation(orientation);
    
    if isinstance(dataSizeSink, basestring):
        dataSizeSink = io.dataSize(dataSizeSink);

    #orient actual resolutions onto reference resolution    
    dataSizeSource = io.dataSize(source);
        
    dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize(dataSizeSource = dataSizeSource, dataSizeSink = dataSizeSink, 
                                                                                      resolutionSource = resolutionSource, resolutionSink = resolutionSink, orientation = orientation);
    
    dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation);
    
    #print dataSizeSource, dataSizeSink, resolutionSource, resolutionSink, dataSizeSinkI
    
     
    #rescale in x y in parallel
    if processingDirectory == None:
        processingDirectory = tempfile.mkdtemp();     
        
    interpolation = fixInterpolation(interpolation);
     
    nZ = dataSizeSource[2];
    pool = multiprocessing.Pool(processes=processes);
    argdata = [];
    for i in range(nZ):
        argdata.append( (source, os.path.join(processingDirectory, 'resample_%04d.tif' % i), dataSizeSinkI, interpolation, i, nZ, verbose) );  
        #print argdata[i]
    pool.map(_resampleXYParallel, argdata);
    
    #rescale in z
    fn = os.path.join(processingDirectory, 'resample_%04d.tif' % 0);
    data = io.readData(fn);
    zImage = numpy.zeros((dataSizeSinkI[0], dataSizeSinkI[1], nZ), dtype = data.dtype);    
    for i in range(nZ):
        if verbose and i % 10 == 0:
            print "resampleData; reading %d/%d" % (i, nZ);
        fn = os.path.join(processingDirectory, 'resample_%04d.tif' % i);
        zImage[:,:, i] = io.readData(fn);

    
    resampledData = numpy.zeros(dataSizeSinkI, dtype = zImage.dtype);

    for i in range(dataSizeSinkI[0]):
        if verbose and i % 25 == 0:
            print "resampleData: processing %d/%d" % (i, dataSizeSinkI[0])
        #resampledImage[:, iImage ,:] =  scipy.misc.imresize(zImage[:,iImage,:], [resizedZAxisSize, sagittalImageSize[1]] , interp = 'bilinear'); 
        #cv2.resize takes reverse order of sizes !
        resampledData[i ,:, :] =  cv2.resize(zImage[i,:,:], (dataSizeSinkI[2], dataSizeSinkI[1]), interpolation = interpolation);
        #resampledData[i ,:, :] =  cv2.resize(zImage[i,:, :], (dataSize[1], resizedZSize));
    

    #account for using (z,y,x) array representation -> (y,x,z)
    #resampledData = resampledData.transpose([1,2,0]);
    #resampledData = resampledData.transpose([2,1,0]);
    
    if cleanup:
        shutil.rmtree(processingDirectory);

    if not orientation is None:
        
        #reorient
        per = orientationToPermuation(orientation);
        resampledData = resampledData.transpose(per);
    
        #reverse orientation after permuting e.g. (-2,1) brings axis 2 to first axis and we can reorder there
        if orientation[0] < 0:
            resampledData = resampledData[::-1, :, :];
        if orientation[1] < 0:
            resampledData = resampledData[:, ::-1, :]; 
        if orientation[2] < 0:
            resampledData = resampledData[:, :, ::-1];
        
        #bring back from y,x,z to z,y,x
        #resampledImage = resampledImage.transpose([2,0,1]);
    if verbose:
        print "resampleData: resampled data size: " + str(resampledData.shape)  
    
    if sink == []:
        if io.isFileExpression(source):
            sink = os.path.split(source);
            sink = os.path.join(sink[0], 'resample_\d{4}.tif');
        elif isinstance(source, basestring):
            sink = source + '_resample.tif';
        else:
            raise RuntimeError('resampleData: automatic sink naming not supported for non string source!');
    
    return io.writeData(sink, resampledData);
)
baseDirectory = '/d2/studies/ClearMap/Alex_Acute_iDISCO/NIC_HeatMaps/FiguresForPaper/'
region = 'MSC'

points = io.readPoints(TransformedCellsFile)
data = plt.overlayPoints(AnnotationFile, points.astype(int), pointColor=None)
io.writeData(
    os.path.join(BaseDirectory,
                 sampleName + '_Annotations_Points_Overlay_newAtlas2.tif'),
    data)
data = data[:, :, :, 1:]
io.writeData(
    os.path.join(BaseDirectory,
                 sampleName + '_Points_Transformed_newAtlas2.tif'), data)

label = io.readData(AnnotationFile)
label = label.astype('int32')
labelids = np.unique(label)

outside = np.zeros(label.shape, dtype=bool)
"""
In order to find out the level to use, in console input:
>>> lbl.labelAtLevel(r, n)
where r is region ID, and n is level (usually start at 5), if the output is not the
region ID, increase n.
"""
for l in labelids:
    if not (lbl.labelAtLevel(l, 6) == 672):
        outside = np.logical_or(outside, label == l)

heatmap = io.readData(
Esempio n. 14
0
samples = ['IA1_RT', 'IA1_RB', 'IA1_LT', 'IA1_LB', 
           'IA2_NP', 'IA2_RT', 'IA2_RB', 'IA2_LT', 'IA2_LB']


dataList = []
for mouse in samples:
    sampleName = mouse
    baseDirectory = '/d2/studies/ClearMap/IA_iDISCO/' + sampleName


#sampleName = 'IA2_LT'
#baseDirectory = '/d2/studies/ClearMap/IA_iDISCO/' + sampleName
#sampleName = mouse
##IMPORT PREVIOUSLY PRE-PROCESSED POINTS DATA
    hemisphere = '_left'
    data = io.readData(os.path.join(baseDirectory, sampleName + '_Caudoputamen' + '_isolated_points' + hemisphere + '.tif'))
    points = np.nonzero(data)[:3]
    dfPoints = pd.DataFrame(points, index=['x', 'y', 'z']).T
    dfPoints.rename(columns={0: "x", 1: "y", 2: "z"})
    
    ###View Range:
    x_range=dfPoints.x.max() - dfPoints.x.min()
    y_range=dfPoints.y.max() - dfPoints.y.min()
    z_range=dfPoints.z.max() - dfPoints.z.min()
    print(x_range, y_range, z_range)
    
    #Bin Y axis
    dfPoints['y_bins']=pd.cut(dfPoints['y'], bins=2)
    dfPoints['y_bins'].value_counts()
    dfPoints_counts_y = dfPoints['y_bins'].value_counts()
    dfPoints_sorted_y = dfPoints_counts_y.sort_index()
Esempio n. 15
0
def dataViewer(source, axis = None, scale = None):
  source = io.readData(source);
  size = io.dataSize(source);
  size2 = np.array(np.array(size) / 2, dtype = int);
  order = [0,1,2];
  #ImageView expexts z,x,y ordering
  
  #order = np.roll(order, -2)
  source = np.transpose(source, order);
  
  dim = len(size);
  if scale is None:
    scale = np.ones(dim);
  else:
    scale = np.array(scale);
    assert(len(scale) == dim);
  #scale = np.roll(scale,-2);
  
  if axis is None:
    axis = 2;
  orderR  = np.roll(order, -axis);
  sourceR = np.transpose(source, orderR);
  sizeR   = np.roll(size, -axis);
  scaleR  = np.roll(scale,-axis);
  
  print sizeR;
  print scaleR;
    
  #axes = ('t', 'x', 'y');
  axes = None;
  
  percentiles = ([0,5,10,50],[50,90, 95, 100]);
  precent_id = [1, 2];
  
  # create the gui
  pg.mkQApp()  
  
  widget = pg.QtGui.QWidget();
  widget.setWindowTitle('Data Viewer');
  widget.resize(1000,800)  
  
  layout = pg.QtGui.QVBoxLayout();
  layout.setContentsMargins(0,0,0,0)        
  
  splitter = pg.QtGui.QSplitter();
  splitter.setOrientation(pg.QtCore.Qt.Vertical)
  splitter.setSizes([int(widget.height()*0.99), int(widget.height()*0.01)]);
  layout.addWidget(splitter);
  
  #  Image plot
  img = pg.ImageView();
  img.setImage(sourceR, axes = axes);
  img.imageItem.setRect(pg.QtCore.QRect(0, 0, sizeR[1] * scaleR[1],  sizeR[2] * scaleR[2]))
  img.view.setXRange(0, sizeR[1] * scaleR[1]);
  img.view.setYRange(0, sizeR[2] * scaleR[2]);
  img.setCurrentIndex(size2[axis]);
  img.ui.histogram.region.setRegion(np.percentile(sourceR[size2[axis]], [percentiles[0][precent_id[0]], percentiles[1][precent_id[1]]]));
  splitter.addWidget(img);
  
  # Tools
  tools_layout = pg.QtGui.QGridLayout()
  axis_buttons = [];
  for d in range(dim):
    b = pg.QtGui.QPushButton('%d' % (d));
    b.setMaximumWidth(100);
    tools_layout.addWidget(b,0,d);
    axis_buttons.append(b);

  adjust_buttons = [];
  pre = ['Min %d', 'Max %d'];
  iitem = dim;
  for r in range(2):
    adjust_buttons_mm = [];
    for p in percentiles[r]:
      b = pg.QtGui.QPushButton(pre[r] % (p));
      b.setMaximumWidth(120);
      tools_layout.addWidget(b,0,iitem);
      iitem +=1;
      adjust_buttons_mm.append(b);
    adjust_buttons.append(adjust_buttons_mm);
  
  tools_widget = pg.QtGui.QWidget();
  tools_widget.setLayout(tools_layout);
  splitter.addWidget(tools_widget);
  
  widget.setLayout(layout)
  widget.show();
  
  # Callbacks for handling user interaction
  def updateAxis(a):
    axis = a;
    orderR  = np.roll(order, -axis);
    sourceR = np.transpose(source, orderR);
    sizeR   = np.roll(size, -axis);
    scaleR  = np.roll(scale,-axis);
    img.setImage(sourceR, axes = axes);
    img.imageItem.setRect(pg.QtCore.QRect(0, 0, sizeR[1] * scaleR[1],  sizeR[2] * scaleR[2]))
    img.view.setXRange(0, sizeR[1] * scaleR[1]);
    img.view.setYRange(0, sizeR[2] * scaleR[2]);
    img.setCurrentIndex(size2[axis]);
    img.ui.histogram.region.setRegion(np.percentile(sourceR[size2[axis]], [percentiles[0][precent_id[0]], percentiles[1][precent_id[1]]]));
  
  for i,ab in enumerate(axis_buttons):
    ab.clicked.connect(partial(updateAxis, i));
  
  def updateRegion(m,p):
    precent_id[m] = p;
    img.ui.histogram.region.setRegion(np.percentile(sourceR[size2[axis]], [percentiles[0][precent_id[0]], percentiles[1][precent_id[1]]]));
    
  for m,ab in enumerate(adjust_buttons):
    for p, abm in enumerate(ab):
      abm.clicked.connect(partial(updateRegion, m, p));
  
  return widget;
Esempio n. 16
0
def plotTiling2(dataSource,
                tiling="automatic",
                maxtiles=20,
                x=all,
                y=all,
                z=all,
                inverse=False,
                colormap='gray',
                percentile=100):
    """Plot 3d image as 2d tiles
    
    Arguments:
        dataSouce (str or array): volumetric image data
        tiling (str or tuple): tiling specification
        maxtiles: maximalnumber of tiles
        x, y, z (all or tuple): sub-range specification
        inverse (bool):invert image
    
    Returns:
        (object): figure handle
    """

    image = io.readData(dataSource, x=x, y=y, z=z)
    dim = image.ndim

    if dim < 2 or dim > 4:
        raise StandardError('plotTiling: image dimension must be 2 to 4')

    if dim == 2:
        image = image.reshape(image.shape + (1, ))
        dim = 3

    if image.ndim == 3:
        if image.shape[2] == 3:  # 2d color image
            ntiles = 1
            cmap = None
            image = image.reshape((image.shape[0], image.shape[1], 1, 3))
        else:  # 3d gray image
            ntiles = image.shape[2]
            cmap = colormap
            image = image.reshape(image.shape + (1, ))
    else:
        ntiles = image.shape[2]
        # 3d color = 4d
        cmap = None

    if ntiles > maxtiles:
        print "plotTiling: number of tiles %d very big! Clipping at %d!" % (
            ntiles, maxtiles)
        ntiles = maxtiles

    if tiling == "automatic":
        nx = math.floor(math.sqrt(ntiles))
        ny = int(math.ceil(ntiles / nx))
        nx = int(nx)
    else:
        nx = int(tiling[0])
        ny = int(tiling[1])

    #print image.shape
    plt.clf()
    fig = plt.gcf()
    fig.subplots_adjust(wspace=0.05, hspace=0.05)

    for i in range(0, ntiles):
        if i == 0:
            ax = plt.subplot(nx, ny, i + 1)
        else:
            plt.subplot(nx, ny, i + 1, sharex=ax, sharey=ax)

        imgpl = image[:, :, i, :].copy()
        imgpl = imgpl.transpose([1, 0, 2])

        if imgpl.shape[2] == 1:
            imgpl = imgpl.reshape((imgpl.shape[0], imgpl.shape[1]))

        if inverse:
            imgpl = -imgpl.astype('float')

        #a.imshow(imgpl, interpolation='none', cmap = cmap, vmin = imin, vmax = imax);
        vmin, vmax = np.percentile(imgpl, [0, percentile])
        if vmin == vmax:
            vmax = vmin + 1
        imgpl = (imgpl - vmin) / (vmax - vmin)
        plt.imshow(imgpl, interpolation='none', cmap=cmap, vmin=0, vmax=1)

    #fig.canvas.manager.window.activateWindow()
    #fig.canvas.manager.window.raise_()
    plt.tight_layout()

    return fig
Esempio n. 17
0
def resampleDataInverse(sink,
                        source=None,
                        dataSizeSource=None,
                        orientation=None,
                        resolutionSource=(4.0625, 4.0625, 3),
                        resolutionSink=(25, 25, 25),
                        processingDirectory=None,
                        processes=1,
                        cleanup=True,
                        verbose=True,
                        interpolation='linear',
                        **args):
    """Resample data inversely to :func:`resampleData` routine
    
    Arguments:
        sink (str or None): image to be inversly resampled (=sink in :func:`resampleData`)
        source (str or array): destination for inversly resmapled image (=source in :func:`resampleData`)
        dataSizeSource (tuple or None): target size of the resampled image
        orientation (tuple): orientation specified by permuation and change in sign of (1,2,3)
        resolutionSource (tuple): resolution of the source image (in length per pixel)
        resolutionSink (tuple): resolution of the resampled image (in length per pixel)
        processingDirectory (str or None): directory in which to perform resmapling in parallel, None a temporary directry will be created
        processes (int): number of processes to use for parallel resampling
        cleanup (bool): remove temporary files
        verbose (bool): display progress information
        interpolation (str): method to use for interpolating to the resmapled image
    
    Returns:
        (array or str): data or file name of resampled image

    Notes: 
        * resolutions are assumed to be given for the axes of the intrinsic 
          orientation of the data and reference as when viewed by matplotlib or ImageJ
        * orientation: permuation of 1,2,3 with potential sign, indicating which 
          axes map onto the reference axes, a negative sign indicates reversal 
          of that particular axes
        * only a minimal set of information to detremine the resampling parameter 
          has to be given, e.g. dataSizeSource and dataSizeSink
    """

    #orientation
    orientation = fixOrientation(orientation)

    #assume we can read data fully into memory
    resampledData = io.readData(sink)

    dataSizeSink = resampledData.shape

    if isinstance(dataSizeSource, basestring):
        dataSizeSource = io.dataSize(dataSizeSource)

    dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize(
        dataSizeSource=dataSizeSource,
        dataSizeSink=dataSizeSink,
        resolutionSource=resolutionSource,
        resolutionSink=resolutionSink,
        orientation=orientation)

    #print (dataSizeSource, dataSizeSink, resolutionSource, resolutionSink )

    dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation)

    #flip axes back and permute inversely
    if not orientation is None:
        if orientation[0] < 0:
            resampledData = resampledData[::-1, :, :]
        if orientation[1] < 0:
            resampledData = resampledData[:, ::-1, :]
        if orientation[2] < 0:
            resampledData = resampledData[:, :, ::-1]

        #reorient
        peri = inverseOrientation(orientation)
        peri = orientationToPermuation(peri)
        resampledData = resampledData.transpose(peri)

    # upscale in z
    interpolation = fixInterpolation(interpolation)

    resampledDataXY = numpy.zeros(
        (dataSizeSinkI[0], dataSizeSinkI[1], dataSizeSource[2]),
        dtype=resampledData.dtype)

    for i in range(dataSizeSinkI[0]):
        if verbose and i % 25 == 0:
            print "resampleDataInverse: processing %d/%d" % (i,
                                                             dataSizeSinkI[0])

        #cv2.resize takes reverse order of sizes !
        resampledDataXY[i, :, :] = cv2.resize(
            resampledData[i, :, :], (dataSizeSource[2], dataSizeSinkI[1]),
            interpolation=interpolation)

    # upscale x, y in parallel

    if io.isFileExpression(source):
        files = source
    else:
        if processingDirectory == None:
            processingDirectory = tempfile.mkdtemp()
        files = os.path.join(sink[0], 'resample_\d{4}.tif')

    io.writeData(files, resampledDataXY)

    nZ = dataSizeSource[2]
    pool = multiprocessing.Pool(processes=processes)
    argdata = []
    for i in range(nZ):
        argdata.append((source, fl.fileExpressionToFileName(files, i),
                        dataSizeSource, interpolation, i, nZ))
    pool.map(_resampleXYParallel, argdata)

    if io.isFileExpression(source):
        return source
    else:
        data = io.convertData(files, source)

        if cleanup:
            shutil.rmtree(processingDirectory)

        return data
Esempio n. 18
0
def plotTiling(dataSource,
               tiling="automatic",
               maxtiles=20,
               x=all,
               y=all,
               z=all,
               inverse=False):
    """Plot 3d image as 2d tiles
    
    Arguments:
        dataSouce (str or array): volumetric image data
        tiling (str or tuple): tiling specification
        maxtiles: maximalnumber of tiles
        x, y, z (all or tuple): sub-range specification
        inverse (bool):invert image
    
    Returns:
        (object): figure handle
    """

    image = io.readData(dataSource, x=x, y=y, z=z)
    dim = image.ndim

    if dim < 2 or dim > 4:
        raise StandardError('plotTiling: image dimension must be 2 to 4')

    if dim == 2:
        image = image.reshape(image.shape + (1, ))
        dim = 3

    if image.ndim == 3:
        if image.shape[2] == 3:  # 2d color image
            ntiles = 1
            cmap = None
            image = image.reshape((image.shape[0], image.shape[1], 1, 3))
        else:  # 3d gray image
            ntiles = image.shape[2]
            cmap = plt.cm.gray
            image = image.reshape(image.shape + (1, ))
    else:
        ntiles = image.shape[2]
        # 3d color = 4d
        cmap = None

    if ntiles > maxtiles:
        print "plotTiling: number of tiles %d very big! Clipping at %d!" % (
            ntiles, maxtiles)
        ntiles = maxtiles

    if tiling == "automatic":
        nx = math.floor(math.sqrt(ntiles))
        ny = int(math.ceil(ntiles / nx))
        nx = int(nx)
    else:
        nx = int(tiling[0])
        ny = int(tiling[1])

    #print image.shape

    fig, axarr = plt.subplots(nx, ny, sharex=True, sharey=True)
    fig.subplots_adjust(wspace=0.05, hspace=0.05)
    axarr = np.array(axarr)
    axarr = axarr.flatten()

    imin = image.min()
    imax = image.max()

    if inverse:
        (imin, imax) = (-float(imax), -float(imin))
    #print imin, imax

    for i in range(0, ntiles):
        a = axarr[i]
        imgpl = image[:, :, i, :].copy()
        imgpl = imgpl.transpose([1, 0, 2])

        if imgpl.shape[2] == 1:
            imgpl = imgpl.reshape((imgpl.shape[0], imgpl.shape[1]))

        if inverse:
            imgpl = -imgpl.astype('float')

        #a.imshow(imgpl, interpolation='none', cmap = cmap, vmin = imin, vmax = imax);
        a.imshow(imgpl, interpolation='none', cmap=cmap, vmin=imin, vmax=imax)

    #fig.canvas.manager.window.activateWindow()
    #fig.canvas.manager.window.raise_()

    return fig
#automated isolation

#import ClearMap.Analysis.Statistics as stat
import ClearMap.Analysis.Label as lbl
import ClearMap.IO.IO as io
import os
import numpy as np
import ClearMap.Alignment.Resampling as rsp

baseDirectory = '/d2/studies/ClearMap/TRAP2_tdTomato_iDISCO/Cohort1_OCIVSA/FtdTom_1RT/'
sampleName = 'FtdTom_1RT'
region = 'NAc'
AnnotationFile = '/d2/studies/ClearMap/TRAP2_tdTomato_iDISCO/Annotations_Horizontal_8-302_Full.tif'

label = io.readData(AnnotationFile)
label = label.astype('int32')
labelids = np.unique(label)

outside = np.zeros(label.shape, dtype=bool)
"""
In order to find out the level to use, in console input:
>>> lbl.labelAtLevel(r, n)
where r is region ID, and n is level (usually start at 5), if the output is not the
region ID, increase n.
"""
for l in labelids:
    if not (lbl.labelAtLevel(l, 6) == 672):
        outside = np.logical_or(outside, label == l)

#VTA = 749 (level 5)