Ejemplo n.º 1
0
from catmaid.control.stack import get_stack_info
from catmaid.control.neurohdf import extract_as_numpy_array

import numpy as np

project_id = 1
stack_id = 3
skeleton_id = 1443

# retrieve all components for a given skeleton id
components = Component.objects.filter(project=project_id,
                                      stack=stack_id,
                                      skeleton_id=skeleton_id).all()

# retrieve stack information
stack_info = get_stack_info(project_id, stack_id)

# compute the skeleton bounding box
minX, minY = int(stack_info['dimension']['x']), int(
    stack_info['dimension']['y'])
maxX, maxY = 0, 0
minZ, maxZ = int(stack_info['dimension']['z']), 0
for comp in components:
    minX = min(minX, comp.min_x)
    minY = min(minY, comp.min_y)
    minZ = min(minZ, comp.z)
    maxX = max(maxX, comp.max_x)
    maxY = max(maxY, comp.max_y)
    maxZ = max(maxZ, comp.z)

print 'found bounding box', minX, minY, maxX, maxY, minZ, maxZ
import numpy as np

project_id = 1
stack_id = 3
skeleton_id = 1443

# retrieve all components for a given skeleton id
components = Component.objects.filter(
        project = project_id,
        stack = stack_id,
        skeleton_id = skeleton_id
    ).all()

# retrieve stack information
stack_info = get_stack_info( project_id, stack_id )

# compute the skeleton bounding box
minX, minY = int(stack_info['dimension']['x']), int(stack_info['dimension']['y'])
maxX, maxY = 0,0
minZ, maxZ = int(stack_info['dimension']['z']), 0
for comp in components:
    minX = min(minX, comp.min_x)
    minY = min(minY, comp.min_y)
    minZ = min(minZ, comp.z)
    maxX = max(maxX, comp.max_x)
    maxY = max(maxY, comp.max_y)
    maxZ = max(maxZ, comp.z)

print 'found bounding box', minX, minY, maxX, maxY, minZ, maxZ
Ejemplo n.º 3
0
def create_segmentation_neurohdf_file(request, project_id, stack_id):
    filename=os.path.join( settings.HDF5_STORAGE_PATH, '{0}_{1}_segmentation.hdf'.format( project_id, stack_id ) )

    print >> sys.stderr, filename
    with closing(h5py.File(filename, 'w')) as hfile:
        hfile.attrs['neurohdf_version'] = '0.1'
        scaleGroup = hfile.create_group("scale")
        scale_zero = scaleGroup.create_group("0")
        sectionGroup = scale_zero.create_group("section")

        # retrieve stack information to transform world coordinates to pixel coordinates
        stack_info = get_stack_info( project_id, stack_id, request.user )

        width=stack_info['dimension']['x']
        height=stack_info['dimension']['y']

        stack = get_object_or_404(Stack, pk=stack_id)
        project = get_object_or_404(Project, pk=project_id)

        whitelist = range( int(stack_info['dimension']['z']) )
        [whitelist.remove( int(k) ) for k,v in stack_info['broken_slices'].items()]

        # retrieve all skeletons that have a component associated with it in order
        # to define a globally consistent colormap from skeleton_id to color
        all_components_with_skeletons = Component.objects.filter(
            project = project,
            stack = stack,
            skeleton_id__isnull=False
        ).distinct('skeleton_id')
        all_skeletons = [ele.skeleton_id for ele in all_components_with_skeletons]
        # TODO: check that no colors are duplicate
        colors = np.random.randint (0,256, (len(all_skeletons),3) )
        colormap = dict(zip(all_skeletons, colors))

        for z in whitelist:
            section = sectionGroup.create_group(str(z))
            shape=(height,width)

            skeletonIdsPixelArray=np.zeros(shape, dtype=np.long)
            skeletonIdsPixelArrayRGB=np.zeros( (height,width, 3), dtype=np.uint8 )

            ### Write all the components
            all_components = Component.objects.filter(
                project = project,
                stack = stack,
                z=z
            ).order_by('creation_time').all()

            for comp in all_components:

                x,y = get_pixellist_for_component(project_id, stack_id, z, comp.component_id)

                skeletonIdsPixelArray[y,x] = comp.skeleton_id
                skeletonIdsPixelArrayRGB[y,x,:] = colormap[comp.skeleton_id]

            all_free_drawings = Drawing.objects.filter(
                stack=stack,
                project=project,
                z = z).exclude(component_id__isnull=False).all()

            for freeDrawing in all_free_drawings:
                typename = DrawingTypesId[freeDrawing.type]
                print >> sys.stderr, 'freedrawing type', freeDrawing.type, typename

                x, y = get_indices_for_drawing(freeDrawing, width, height)

                # TODO: erasor should only delete pixels of its corresponding skeleton_id

                if freeDrawing.type == DrawingTypes['soma']['value']:
                    skeletonIdsPixelArray[y,x] = freeDrawing.skeleton_id
                    skeletonIdsPixelArrayRGB[y,x,:] = colormap[freeDrawing.skeleton_id]
                else:
                    skeletonIdsPixelArray[y,x] = DrawingTypes[typename]['value']
                    skeletonIdsPixelArrayRGB[y,x,:] = DrawingTypesColormap[typename]

            ### Write out
            section.create_dataset("skeletons", data=skeletonIdsPixelArray, compression='gzip', compression_opts=1)
            # map to color
            section.create_dataset("skeletons_rgb", data=skeletonIdsPixelArrayRGB, compression='gzip', compression_opts=1)

        return