Пример #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
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()