Beispiel #1
0
def calculate_area_totals(subject, registrationFolder, ccfSlice, nSliceGroups=1):

    slices = sorted(ccfSlice.keys())
    sliceGroups = []
    for sgInd in range(nSliceGroups):
        sliceGroups.append(slices[sgInd::nSliceGroups])

    groupSliceCount = []

    for sgInd, slices in enumerate(sliceGroups):
        allAreaNames = []
        allSliceCounts = []
        allSliceTotalVoxels = []
        for sliceName in slices:
            ccfZ = ccfSlice[sliceName]
            filenameSVGPost = os.path.join(settings.HISTOLOGY_PATH, subject, registrationFolder, '{}.svg'.format(sliceName))
            (scale, translate, affine) = ha.get_svg_transform(filenameSVGPost, sliceSize=[1388, 1040])
            filenameCSV = os.path.join(settings.HISTOLOGY_PATH, subject, registrationFolder, '{}.csv'.format(sliceName))
            coords = ha.get_coords_from_fiji_csv(filenameCSV, pixelSize=pixelSizes[subject])
            newCoords = ha.apply_svg_transform(scale, translate, affine, coords)

            structIDs = []
            for indCoords in range(np.shape(newCoords)[1]):
                x = newCoords[0,indCoords]
                y = newCoords[1,indCoords]
                thisCoordID = rspAnnotationVolumeRotated[int(x), int(y), ccfZ]
                structIDs.append(thisCoordID)

            # structIDs = annotationVolume.get_structure_id_many_xy(newCoords, ccfSlice[sliceNum])
            # structNames = [annotationVolume.get_structure_from_id(structID) for structID in structIDs]

            structDicts = rsp.structure_tree.get_structures_by_id(structIDs)
            structNames = [d['name'] for d in structDicts]

            structCounts = Counter(structNames)
            allSliceCounts.append(structCounts)
            totalVoxels = annotationVolume.get_total_voxels_per_area(ccfZ)
            totalVoxels = Counter(totalVoxels)
            allSliceTotalVoxels.append(totalVoxels)

        sliceCountSum = reduce(operator.add, allSliceCounts)
        sliceTotalVoxelsSum = reduce(operator.add, allSliceTotalVoxels)
        groupSliceCount.append(sliceCountSum)
        allAreaNames.extend(sliceCountSum.keys())
    #Compress into one dict with lists for each key
    uniqueAreaNames = set(allAreaNames)
    allAreasResults = {}
    for areaName in uniqueAreaNames:
        areaCounts = []
        for sliceCountSum in groupSliceCount:
            areaCounts.append(sliceCountSum.get(areaName, 0))
        allAreasResults.update({areaName:areaCounts})
    return allAreasResults
from jaratoolbox import histologyanalysis as ha
reload(ha)

CREATE_SVG = 0

# -- Create SVG file with images --
if CREATE_SVG:
    filenameAtlas = '/var/tmp/examplesRegistration/atlas.jpg'
    filenameSlice = '/var/tmp/examplesRegistration/slice.jpg'
    filenameSVG = '/tmp/testfile.svg'
    (atlasSize, sliceSize) = ha.save_svg_for_registration(filenameSVG, filenameAtlas, filenameSlice)
    sys.exit()
        
# -- Transform coords --
filenameSVG = '/var/tmp/examplesRegistration/test03.svg'
(scale, translate, affine) = ha.get_svg_transform(filenameSVG)

filenameCSV = '/var/tmp/examplesRegistration/sliceCoords.csv'
coords = ha.get_coords_from_fiji_csv(filenameCSV)

newCoords = ha.apply_svg_transform(scale, translate, affine, coords)
print newCoords.T

filenameAtlas = '/var/tmp/examplesRegistration/atlas.jpg'
atlasIm = mpimg.imread(filenameAtlas)

plt.clf()
plt.imshow(atlasIm,cmap='gray')
plt.plot(newCoords[0,:],newCoords[1,:],'o',mec='r',mfc='none')
plt.axis('image')
plt.show()
Beispiel #3
0
                                     '{}tl.jpg'.format(sliceNum))
        filenameSVG = os.path.join(HISTOLOGY_DIR, subject, registrationFolder,
                                   '{}_pre.svg'.format(sliceNum))
        (atlasSize,
         sliceSize) = ha.save_svg_for_registration(filenameSVG, filenameAtlas,
                                                   filenameSlice)

#Read coords, apply transform, output counts per area
elif CASE == 1:
    annotationVolume = ha.AllenAnnotation()
    allSliceCounts = []
    for sliceNum in slices:
        filenameSVGPost = '/mnt/jarahubdata/jarashare/histology/{}/{}/{}.svg'.format(
            subject, registrationFolder, sliceNum)
        (scale, translate,
         affine) = ha.get_svg_transform(filenameSVGPost,
                                        sliceSize=[1388, 1040])
        filenameCSV = '/mnt/jarahubdata/jarashare/histology/{}/{}/{}.csv'.format(
            subject, registrationFolder, sliceNum)
        coords = ha.get_coords_from_fiji_csv(filenameCSV, pixelSize=4.0048)
        newCoords = ha.apply_svg_transform(scale, translate, affine, coords)
        structIDs = annotationVolume.get_structure_id_many_xy(
            newCoords, ccfSlice[sliceNum])
        structNames = [
            annotationVolume.get_structure_from_id(structID)
            for structID in structIDs
        ]
        structCounts = Counter(structNames)

        allSliceCounts.append(structCounts)

    resultCounts = counter_values(allSliceCounts)
# -- Create SVG file with images --
if CREATE_SVG:
    filenameAtlas = '/var/tmp/examplesRegistration/atlas.jpg'
    filenameSlice = '/var/tmp/examplesRegistration/slice.jpg'
    filenameSVG = '/tmp/testfile.svg'
    (atlasSize,
     sliceSize) = ha.save_svg_for_registration(filenameSVG, filenameAtlas,
                                               filenameSlice)
    sys.exit()
'''Transform coordinates from CSV cell count file to Allen CCF.'''
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from jaratoolbox import histologyanalysis as ha

filenameSVG = '/mnt/jarahubdata/jarashare/histology/anat038/registrationAC/p1d1.svg'
(scale, translate, affine) = ha.get_svg_transform(filenameSVG,
                                                  sliceSize=[1388, 1040])

filenameCSV = '/mnt/jarahubdata/jarashare/histology/anat038/registrationAC/p1d1.csv'
coords = ha.get_coords_from_fiji_csv(filenameCSV, pixelSize=4.0048)

newCoords = ha.apply_svg_transform(scale, translate, affine, coords)
print newCoords.T

filenameAtlas = '/mnt/jarahubdata/atlas/AllenCCF_25/JPEG/allenCCF_Z193.jpg'
atlasIm = mpimg.imread(filenameAtlas)

plt.clf()
plt.imshow(atlasIm, cmap='gray')
plt.plot(newCoords[0, :], newCoords[1, :], 'o', mec='r', mfc=[0, 1, 0], ms=3)
plt.axis('image')
plt.show()