示例#1
0
        gtIsECS = gtComps == gt_ECS_label
        gtLbls = np.zeros(gtComps.shape, dtype=np.uint8)
        gtLbls[np.logical_and(gtComps > 0, np.logical_not(gtIsECS))] = 1
        gtLbls[gtIsECS] = 2
        gtComps[gtIsECS] = 0
        n = gtComps.max()
        gtComps[gtComps == n] = gt_ECS_label
        gtnlabels = n - 1

        for i, seg, segp in zip(range(nsegs), segmentations, segpaths):
            fps = os.path.join(segp, seg)
            print("calculating metrics for " + seg + (" chunk %d %d %d" % tuple(chunk)))
            t = time.time()

            # load network output max prob categories (voxelTypes, "labels")
            loadh5 = emVoxelType.readVoxType(srcfile=fps, chunk=chunk, offset=offset, size=size)
            outLbls = loadh5.data_cube

            # calculate the categorization error
            cat_error[i, j] = (gtLbls != outLbls).sum(dtype=np.int64) / float(outLbls.size)

            for k, prm in zip(range(nparams[i]), segparams[i]):
                loadh5 = emLabels.readLabels(
                    srcfile=fps,
                    chunk=chunk,
                    offset=offset,
                    size=size,
                    subgroups=subgroups[i] + ["%.8f" % (prm,)],
                    verbose=False,
                )
                segComps = loadh5.data_cube
示例#2
0
    def clean(self):

        # smoothing operates on each label one at a time
        if self.smooth:
            if self.dpCleanLabels_verbose:
                print('Smoothing labels object by object'); t = time.time()

            # threshold sizes to remove empty labels
            self.data_cube, sizes = emLabels.thresholdSizes(self.data_cube, minSize=1)

            # xxx - fix old comments from matlab meshing code, fix this

            # xxx - local parameters, expose if find any need to change these
            rad = 5;                # amount to pad (need greater than one for method 3 because of smoothing
            contour_level = 0.5;   # binary threshold for calculating surface mesh
            smooth_size = [3, 3, 3];

            #emptyLabel = 65535; % should define this in attribs?
            sizes = np.array(self.data_cube.shape); sz = sizes + 2*rad;
            image_with_zeros = np.zeros(sz, dtype=self.data_cube.dtype); # create zeros 3 dimensional array
            image_with_zeros[rad:-rad,rad:-rad,rad:-rad] = self.data_cube  # embed label array into zeros array

            image_with_brd = np.lib.pad(self.data_cube,((rad,rad), (rad,rad), (rad,rad)),'edge');
            nSeeds = self.data_cube.max()

            # do not smooth ECS labels
            sel_ECS, ECS_label = self.getECS(image_with_brd)
            if self.dpCleanLabels_verbose and ECS_label:
                print('\tignoring ECS label %d' % (ECS_label,))

            # get bounding boxes for each supervoxel in zero padded label volume
            svox_bnd = nd.measurements.find_objects(image_with_zeros)

            # iterate over labels
            nSeeds = self.data_cube.max(); lbls = np.zeros(sz, dtype=self.data_cube.dtype)
            assert( nSeeds == len(svox_bnd) )
            for j in range(nSeeds):
                if ECS_label and j+1 == ECS_label: continue

                #if self.dpCleanLabels_verbose:
                #    print('Smoothing label %d / %d' % (j+1,nSeeds)); t = time.time()

                pbnd = tuple([slice(x.start-rad,x.stop+rad) for x in svox_bnd[j]])
                Lcrp = (image_with_brd[pbnd] == j+1).astype(np.double)

                Lfilt = nd.filters.uniform_filter(Lcrp, size=smooth_size, mode='constant')
                # incase smoothing below contour level, use without smoothing
                if not (Lfilt > contour_level).any(): Lfilt = Lcrp

                # assign smoothed output for current label
                lbls[pbnd][Lfilt > contour_level] = j+1

            # put ECS labels back
            if ECS_label: lbls[sel_ECS] = ECS_label

            if self.dpCleanLabels_verbose:
                print('\tdone in %.4f s' % (time.time() - t))

            self.data_cube = lbls[rad:-rad,rad:-rad,rad:-rad]

        if self.remove_adjacencies:
            labels = self.data_cube.astype(np.uint32, copy=True, order='C')
            sel_ECS, ECS_label = self.getECS(labels); labels[sel_ECS] = 0

            if self.dpCleanLabels_verbose:
                print('Removing adjacencies with conn %d%s' % (self.fg_connectivity,
                    ', ignoring ECS label %d' % (ECS_label,) if ECS_label else ''))
                t = time.time()

            self.data_cube = emLabels.remove_adjacencies_nconn(labels, bwconn=self.fgbwconn)
            if ECS_label: self.data_cube[sel_ECS] = ECS_label

            if self.dpCleanLabels_verbose:
                print('\tdone in %.4f s' % (time.time() - t))

        if self.minsize > 0:
            labels = self.data_cube
            sel_ECS, ECS_label = self.getECS(labels); labels[sel_ECS] = 0

            if self.dpCleanLabels_verbose:
                print('Scrubbing labels with minsize %d%s' % (self.minsize,
                    ', ignoring ECS label %d' % (ECS_label,) if ECS_label else ''))
                print('\tnlabels = %d, before re-label' % (labels.max(),))
                t = time.time()

            selbg = np.logical_and((labels == 0), np.logical_not(sel_ECS))
            labels, sizes = emLabels.thresholdSizes(labels, minSize=self.minsize)
            if self.minsize_fill:
                if self.dpCleanLabels_verbose:
                    print('Nearest neighbor fill scrubbed labels')
                labels = emLabels.nearest_neighbor_fill(labels, mask=selbg, sampling=self.data_attrs['scale'])

            nlabels = sizes.size
            labels, nlabels = self.setECS(labels, sel_ECS, ECS_label, nlabels)
            self.data_cube = labels
            # allow this to work before self.get_svox_type or self.write_voxel_type
            self.data_attrs['types_nlabels'] = [nlabels]

            if self.dpCleanLabels_verbose:
                print('\tnlabels = %d after re-label' % (nlabels,))
                print('\tdone in %.4f s' % (time.time() - t))

        if self.cavity_fill:
            if self.dpCleanLabels_verbose:
                print('Removing cavities using conn %d' % (self.bg_connectivity,)); t = time.time()

            selbg = (self.data_cube == 0)
            if self.dpCleanLabels_verbose:
                print('\tnumber bg vox before = %d' % (selbg.sum(dtype=np.int64),))
            labels = np.ones([x + 2 for x in self.data_cube.shape], dtype=np.bool)
            labels[1:-1,1:-1,1:-1] = selbg
            # don't connect the top and bottom xy planes
            labels[1:-1,1:-1,0] = 0; labels[1:-1,1:-1,-1] = 0
            labels, nlabels = nd.measurements.label(labels, self.bgbwconn)
            msk = np.logical_and((labels[1:-1,1:-1,1:-1] != labels[0,0,0]), selbg); del labels
            self.data_cube[msk] = 0; selbg[msk] = 0
            self.data_cube = emLabels.nearest_neighbor_fill(self.data_cube, mask=selbg,
                sampling=self.data_attrs['scale'])

            if self.dpCleanLabels_verbose:
                print('\tdone in %.4f s' % (time.time() - t))
                print('\tnumber bg vox after = %d' % ((self.data_cube==0).sum(dtype=np.int64),))

        if self.relabel:
            labels = self.data_cube
            sel_ECS, ECS_label = self.getECS(labels); labels[sel_ECS] = 0

            if self.dpCleanLabels_verbose:
                print('Relabeling fg components with conn %d%s' % (self.fg_connectivity,
                    ', ignoring ECS label %d' % (ECS_label,) if ECS_label else ''))
                print('\tnlabels = %d, max = %d, before re-label' % (len(np.unique(labels)), labels.max()))
                t = time.time()

            labels, nlabels = nd.measurements.label(labels, self.fgbwconn)

            labels, nlabels = self.setECS(labels, sel_ECS, ECS_label, nlabels)
            self.data_cube = labels

            if self.dpCleanLabels_verbose:
                print('\tnlabels = %d after re-label' % (nlabels,))
                print('\tdone in %.4f s' % (time.time() - t))

        # this step is always last, as writes new voxel_type depending on the cleaning that was done
        if self.get_svox_type or self.write_voxel_type:
            if self.dpCleanLabels_verbose:
                print('Recomputing supervoxel types and re-ordering labels'); t = time.time()

            voxType = emVoxelType.readVoxType(srcfile=self.srcfile, chunk=self.chunk.tolist(),
                offset=self.offset.tolist(), size=self.size.tolist())
            voxel_type = voxType.data_cube.copy(order='C')

            labels = self.data_cube.copy(order='C')
            #nlabels = labels.max(); assert(nlabels == self.data_attrs['types_nlabels'][0])
            nlabels = sum(self.data_attrs['types_nlabels']); ntypes = len(voxType.data_attrs['types'])
            supervoxel_type, voxel_type = emLabels.type_components(labels, voxel_type, nlabels, ntypes)
            assert( supervoxel_type.size == nlabels )
            # reorder labels so that supervoxels are grouped by / in order of supervoxel type
            remap = np.zeros((nlabels+1,), dtype=self.data_cube.dtype)
            remap[np.argsort(supervoxel_type)+1] = np.arange(1,nlabels+1,dtype=self.data_cube.dtype)
            self.data_cube = remap[self.data_cube]
            types_nlabels = [(supervoxel_type==x).sum(dtype=np.int64) for x in range(1,ntypes)]
            assert( sum(types_nlabels) == nlabels ) # indicates voxel type does not match supervoxels
            self.data_attrs['types_nlabels'] = types_nlabels

            if self.write_voxel_type:
                if self.dpCleanLabels_verbose:
                    print('Rewriting voxel type pixel data based on supervoxel types')
                d = voxType.data_attrs.copy(); #d['types_nlabels'] =
                emVoxelType.writeVoxType(outfile=self.outfile, chunk=self.chunk.tolist(),
                    offset=self.offset.tolist(), size=self.size.tolist(), datasize=voxType.datasize.tolist(),
                    chunksize=voxType.chunksize.tolist(), data=voxel_type.astype(emVoxelType.VOXTYPE_DTYPE), attrs=d)

            if self.dpCleanLabels_verbose:
                print('\tdone in %.4f s' % (time.time() - t))