Beispiel #1
0
 def __init__(self, atlas, host):
     Atlas.AtlasCtrlWidget.__init__(self, atlas, host)
     
     self.atlasDir = os.path.split(os.path.abspath(__file__))[0]
     
     ## add ThalamocorticalMarker to canvas
     fh = DataManager.getHandle(os.path.join(self.atlasDir, 'images', 'ThalamocorticalMarker.svg'))
     self.canvas.addFile(fh, pos=(-0.001283, -0.000205), scale=[3.78e-6, 3.78e-6], index=0, movable=False, z=10000)        
  
     ## add CortexROI
     self.roi = CortexROI([-1e-3, 0])
     self.canvas.addGraphicsItem(self.roi, pos=(-1e-3, 1e-3), scale=[1e-3, 1e-3], name='CortexROI', movable=False)
     self.roi.sigRegionChangeFinished.connect(self.roiChanged)
Beispiel #2
0
class A1AtlasCtrlWidget(Atlas.AtlasCtrlWidget):
    
    def __init__(self, atlas, host):
        Atlas.AtlasCtrlWidget.__init__(self, atlas, host)
        
        self.atlasDir = os.path.split(os.path.abspath(__file__))[0]
        
        ## add ThalamocorticalMarker to canvas
        fh = DataManager.getHandle(os.path.join(self.atlasDir, 'images', 'ThalamocorticalMarker.svg'))
        self.canvas.addFile(fh, pos=(-0.001283, -0.000205), scale=[3.78e-6, 3.78e-6], index=0, movable=False, z=10000)        
     
        ## add CortexROI
        self.roi = CortexROI([-1e-3, 0])
        self.canvas.addGraphicsItem(self.roi, pos=(-1e-3, 1e-3), scale=[1e-3, 1e-3], name='CortexROI', movable=False)
        self.roi.sigRegionChangeFinished.connect(self.roiChanged)
        
    def loadState(self):
        if self.sliceDir is None:
            return
            
        state = self.sliceDir.info()['atlas'][self.atlas.name()]      
        self.roi.setState(state['cortexROI'])
        self.atlas.setState(state)
        
    def saveState(self):
        ## Saves the position/configuration of the cortexROI, as well as transforms for mapping to atlas coordinates
        
        if self.sliceDir is None:
            return       
       
        ## get state of ROI
        cortexROI = self.roi.saveState()
        quads = self.roi.getQuadrilaterals()
        newQuads = []
        for q in quads:
            newQuads.append([(p.x(), p.y()) for p in q])
        rects = self.roi.getNormalizedRects()
        matrices = []
        for i, q in enumerate(quads):
            matrix = self.atlas.solveBilinearTransform([pg.Point(x) for x in q], [pg.Point(x) for x in rects[i]])
            matrices.append([list(matrix[0]), list(matrix[1])])
        
        state = {
            'cortexROI':cortexROI,
            'quadrilaterals':newQuads,
            'normalizedRects': rects,
            'transformMatrices': matrices
        }
        
        ## write to slice directory meta-info
        atlasInfo = self.sliceDir.info().get('atlas', {}).deepcopy()
        atlasInfo[self.atlas.name()] = state
        self.sliceDir.setInfo(atlas=atlasInfo)
        
        ## set state for atlas object
        self.atlas.setState(state)       
        
    def generateDataArray(self, positions, dirType):
        prof = Profiler("A1Atlas.generateDataArray", disabled=True)
        
        if self.atlas.state is None:
            self.saveState()
        prof.mark('saved atlas state')
        
        dirColumn = dirType + 'Dir'
        if dirType == 'Protocol':
            data = np.empty(len(positions), dtype=[('SliceDir', object), 
                                                   (dirColumn, object), 
                                                   #('layer', float),
                                                   #('depth', float), 
                                                   ('yPosSlice', float),
                                                   ('yPosCell', float),
                                                   ('percentDepth', float), 
                                                   ('xPosSlice', float), 
                                                   ('xPosCell', float), 
                                                   ('modXPosSlice', float), 
                                                   ('modXPosCell', float)])
            fields = collections.OrderedDict([
                           ('SliceDir', 'directory:Slice'),
                           (dirColumn, 'directory:'+dirType),
                           ('yPosSlice', 'real'),
                           ('yPosCell', 'real'),
                           ('percentDepth', 'real'),
                           ('xPosSlice', 'real'),
                           ('xPosCell', 'real'),
                           ('modXPosSlice', 'real'),
                           ('modXPosCell', 'real')])            
            prof.mark("defined Protocol data array")
            
            for i in range(len(positions)):
                dh, pos = positions[i]
                cellPos = self.dataModel.getCellInfo(dh)['userTransform']['pos']
                mapped = self.atlas.mapToAtlas(pg.Point(pos)) ## needs to return %depth and modXPosSlice 
                #data[i] = (self.sliceDir, dh, mapped.x(), mapped.y(), mapped.z())
                data[i]['SliceDir'] = self.sliceDir
                data[i][dirColumn] = dh
                data[i]['yPosSlice'] = pos[1]
                data[i]['yPosCell'] = pos[1]-cellPos[1]
                data[i]['percentDepth'] = mapped[1]
                data[i]['xPosSlice'] = pos[0]
                data[i]['xPosCell'] = pos[0]-cellPos[0]
                data[i]['modXPosSlice'] = mapped[0]
                data[i]['modXPosCell'] = mapped[0]-self.atlas.mapToAtlas(pg.Point(cellPos))[0]   
            prof.mark("filled protocol data array")
            
        elif dirType == 'Cell':
            data = np.empty(len(positions), dtype=[('SliceDir', object), 
                                                    (dirColumn, object), 
                                                    #('layer', float),
                                                    #('depth', float), 
                                                    ('yPosSlice', float),
                                                    #('yPosCell', float),
                                                    ('percentDepth', float), 
                                                    ('xPosSlice', float), 
                                                    #('xPosCell', float), 
                                                    ('modXPosSlice', float), 
                                                    #('modXPosCell', float)
                                                    ]) 
            fields = collections.OrderedDict([
                ('SliceDir', 'directory:Slice'),
                (dirColumn, 'directory:'+dirType),
                ('yPosSlice', 'real'),
                ('percentDepth', 'real'),
                ('xPosSlice', 'real'),
                ('modXPosSlice', 'real')])
            prof.mark("defined cell data array")
            for i in range(len(positions)):
                dh, pos = positions[i]
                #cellPos = self.dataModel.getCellInfo(dh)['pos']
                mapped = self.atlas.mapToAtlas(pg.Point(pos)) ## needs to return %depth and modXPosSlice 
                #data[i] = (self.sliceDir, dh, mapped.x(), mapped.y(), mapped.z())
                data[i]['SliceDir'] = self.sliceDir
                data[i][dirColumn] = dh
                data[i]['yPosSlice'] = pos[1]
                #data['yPosCell'] = pos[1]-cellPos[1]
                data[i]['percentDepth'] = mapped[1]
                data[i]['xPosSlice'] = pos[0]
                #data['xPosCell'] = pos[0]-cellPos[0]
                data[i]['modXPosSlice'] = mapped[0]
                #data['modXPosCell'] = mapped[0]-self.atlas.mapToAtlas(pg.Point(cellPos))[0]   
            prof.mark("filled cell data array")
        else:
            prof.finish()
            raise Exception("Not sure how to structure data array for dirType=%s"%dirType)
        
        prof.finish()
        return data, fields
                
    def roiChanged(self):
        self.saveState()