예제 #1
0
    def restoreState(self, state, rootPath=None):
        if state.get('contents', None) != 'MosaicEditor_save':
            raise TypeError("This does not appear to be MosaicEditor save data.")
        if state['version'][0] > self._saveVersion[0]:
            raise TypeError("Save data has version %d.%d, but this MosaicEditor only supports up to version %d.x." % (state['version'][0], state['version'][1], self._saveVersion[0]))

        if not self.clear():
            return

        root = state['rootPath']
        if root == '':
            # data was stored with no root path; filenames should be absolute
            root = None
        else:
            # data was stored with no root path; filenames should be relative to the loaded file            
            root = DataManager.getHandle(rootPath)
            
        for itemState in state['items']:
            fname = itemState['filename']
            if root is None:
                fh = DataManager.getHandle(fh)
            else:
                fh = root[fname]
            item = self.addFile(fh, name=itemState['name'], inheritTransform=False)
            item.restoreState(itemState)
        
        self.canvas.view.setState(state['view'])
예제 #2
0
def getSiteNWBs(basepath):
    """ Get all experiment NWBs in the slice*/site* folders """

    matches = []

    dirHandleBase = adm.getHandle(basepath)
    dirHandleBase.checkIndex()

    # start in basepath
    for k in dirHandleBase.ls():
        if not dirHandleBase.isManaged(k):
            continue

        slicePath = os.path.join(basepath, k)

        if os.path.isfile(slicePath):
            continue

        # base/slice
        dirHandleSlice = adm.getHandle(slicePath)
        dirHandleSlice.checkIndex()

        for k in dirHandleSlice.ls():
            if not dirHandleSlice.isManaged(k):
                continue

            sitePath = os.path.join(slicePath, k)

            if os.path.isfile(sitePath):
                continue

            # base/slice/site
            all_nwbs = glob.glob(os.path.join(sitePath, '*.nwb'))
            all_pxps = glob.glob(os.path.join(sitePath, '*.pxp'))

            if len(all_nwbs) != 1:
                print "The site folder \"%s\" will be ignored as it holds more than one NWB file." % sitePath
                print all_nwbs
                continue

            if len(all_pxps) != 1:
                print "The site folder \"%s\" will be ignored as it holds more than one PXP file." % sitePath
                print all_pxps
                continue

            siteNWB = all_nwbs[0]
            sitePXP = all_pxps[0]

            if not os.path.isfile(siteNWB):
                print "The site folder \"%s\" will be ignored as it is missing the mandatory NWB file." % sitePath
                continue

            if not os.path.isfile(sitePXP):
                print "The site folder \"%s\" will be ignored as it is missing the mandatory PXP file." % sitePath
                continue

            matches.append(os.path.abspath(siteNWB))

    return matches
예제 #3
0
    def restoreState(self, state, rootPath=None):
        if state.get('contents', None) != 'MosaicEditor_save':
            raise TypeError(
                "This does not appear to be MosaicEditor save data.")
        if state['version'][0] > self._saveVersion[0]:
            raise TypeError(
                "Save data has version %d.%d, but this MosaicEditor only supports up to version %d.x."
                % (state['version'][0], state['version'][1],
                   self._saveVersion[0]))

        if not self.clear():
            return

        root = state['rootPath']
        if root == '':
            # data was stored with no root path; filenames should be absolute
            root = None
        else:
            # data was stored with no root path; filenames should be relative to the loaded file
            root = DataManager.getHandle(rootPath)

        loadfail = []
        for itemState in state['items']:
            fname = itemState.get('filename')

            if fname is None:
                # create item from scratch and restore state
                itemtype = itemState.get('type')
                if itemtype not in items.itemTypes():
                    # warn the user later on that we could not load this item
                    loadfail.append((itemState.get('name'),
                                     'Unknown item type "%s"' % itemtype))
                    continue
                item = self.addItem(type=itemtype, name=itemState['name'])
            else:
                # normalize file name for this OS
                revsep = {'/': '\\', '\\': '/'}[os.path.sep]
                fname = fname.replace(revsep, os.path.sep)

                # create item by loading file and restore state
                if root is None:
                    fh = DataManager.getHandle(fh)
                else:
                    fh = root[fname]
                item = self.addFile(fh,
                                    name=itemState['name'],
                                    inheritTransform=False)
            item.restoreState(itemState)

        self.canvas.view.setState(state['view'])
        if len(loadfail) > 0:
            msg = "\n".join(["%s: %s" % m for m in loadfail])
            raise Exception("Failed to load some items:\n%s" % msg)
def getSiteNWBs(basepath):
    """ Get all experiment NWBs in the slice*/site* folders """

    matches = []

    dirHandleBase = adm.getHandle(basepath)
    dirHandleBase.checkIndex()

    # start in basepath
    for k in dirHandleBase.ls():
        if not dirHandleBase.isManaged(k):
            continue

        slicePath = os.path.join(basepath, k)

        if os.path.isfile(slicePath):
            continue

        # base/slice
        dirHandleSlice = adm.getHandle(slicePath)
        dirHandleSlice.checkIndex()

        for k in dirHandleSlice.ls():
            if not dirHandleSlice.isManaged(k):
                continue

            sitePath = os.path.join(slicePath, k)

            if os.path.isfile(sitePath):
                continue

            # base/slice/site
            all_nwbs = glob.glob(os.path.join(sitePath, '*.nwb'))

            if len(all_nwbs) != 1:
                print "The site folder \"%s\" will be ignored as it holds not exactly one NWB file." % sitePath
                print all_nwbs
                continue

            siteNWB = all_nwbs[0]

            if not os.path.isfile(siteNWB):
                print "The site folder \"%s\" will be ignored as it is missing the mandatory NWB file." % sitePath
                continue

            matches.append(os.path.abspath(siteNWB))

    return matches
예제 #5
0
def addSiteContents(siteNWBs, filesToInclude, slicePath, siteName):
    """ Add site specific entries to the NWB file """

    sitePath = os.path.join(slicePath, siteName)
    siteIndex = os.path.join(sitePath, ".index")

    if len(siteNWBs) != 1:
        print "Expected exactly one NWB file belonging to site folder %s, skipping it." % sitePath
        return 1

    sitePXP = glob.glob(os.path.join(sitePath, '*.pxp'))[0]

    addDataSource(siteNWBs, sitePXP)

    dh = adm.getHandle(sitePath)
    dh.checkIndex()

    data = encodeAsJSONString(dh["."].info())
    appendMiscFileToNWB(siteNWBs, "%s_index_meta" % siteName, data)
    appendMiscFileToNWB(siteNWBs, "%s_index" % siteName,
                        getFileContents(siteIndex))

    for k in dh.ls():

        if not dh.isManaged(k):
            continue

        path = os.path.join(sitePath, k)

        if os.path.isdir(path):
            raise NameError("Unexpected folder \"%s\" in \"%s\"." %
                            (k, sitePath))
        elif os.path.isfile(path):  # check if we need to handle it

            if fileShouldBeSkipped(path, filesToInclude):
                continue

            filedesc = encodeAsJSONString(dh[k].info())

            if path.endswith(".tif"):
                appendImageFileToNWB(siteNWBs, path, filedesc)
            elif path.endswith(".ma"):
                appendMAFile(siteNWBs, path, filedesc)
            elif path.endswith(".log"):
                appendPseudoYamlLog(siteNWBs, path, k, filedesc)
            else:
                raise NameError(
                    "Unexpected file type \"%s\" in index \"%s\"." %
                    (k, siteIndex))
        else:
            raise NameError("Unexpected key \"%s\" in index \"%s\"." %
                            (k, siteIndex))
예제 #6
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)
예제 #7
0
def buildCombinedNWBInternal(basepath, siteNWBs, filesToInclude):
    """ NOT FOR PUBLIC USE """

    # we have three types of keys in the main index file
    # ---------------------------------------------------------------------
    # '.'               | common description of the experiment | (unique)
    # '$existingFile'   | log file of the experiment           | (multiple)
    # '$existingFolder' | different slices for each experiment | (multiple)

    dh = adm.getHandle(basepath)
    dh.checkIndex()

    data = encodeAsJSONString(dh["."].info())
    appendMiscFileToNWB(siteNWBs, basename="main_index_meta", content=data)

    logfile = os.path.join(basepath, '.index')
    appendMiscFileToNWB(siteNWBs,
                        basename="main_index",
                        content=getFileContents(logfile))

    for elem in siteNWBs:
        os.remove(deriveOutputNWB(elem))

    for k in dh.ls():
        if not dh.isManaged(k):
            continue

        path = os.path.abspath(os.path.join(basepath, k))

        if os.path.isdir(path):  # slice folder
            addSliceContents(siteNWBs, filesToInclude, basepath, k)
        elif os.path.isfile(path):  # main log file

            data = encodeAsJSONString(dh[k].info())
            appendMiscFileToNWB(siteNWBs,
                                basename="main_logfile_meta",
                                content=data)
            appendMiscFileToNWB(siteNWBs,
                                basename="main_logfile",
                                content=getFileContents(path))
        else:
            raise NameError("Unexpected key \"%s\" in index \"%s\"" %
                            (k, logfile))

    combinedNWBs = []

    for elem in siteNWBs:
        combinedNWBs.append(deriveOutputNWB(elem))

    return combinedNWBs
예제 #8
0
def addSliceContents(siteNWBs, filesToInclude, basepath, sliceName):
    """ Add entries to the slice specific NWB file """

    slicePath = os.path.join(basepath, sliceName)
    sliceIndex = os.path.join(slicePath, ".index")

    sliceNWBs = [elem for elem in siteNWBs if elem.startswith(slicePath)]

    if len(sliceNWBs) == 0:
        #print "No NWB files belong to slice folder %s, skipping it." % slicePath
        return 1

    dh = adm.getHandle(slicePath)
    dh.checkIndex()

    data = encodeAsJSONString(dh["."].info())
    appendMiscFileToNWB(siteNWBs, "%s_index_meta" % sliceName, data)
    appendMiscFileToNWB(siteNWBs, "%s_index" % sliceName,
                        getFileContents(sliceIndex))

    for k in dh.ls():
        if not dh.isManaged(k):
            continue

        path = os.path.abspath(os.path.join(slicePath, k))

        filedesc = encodeAsJSONString(dh[k].info())

        if os.path.isdir(path):  # site folder
            appendMiscFileToNWB(sliceNWBs, "%s_%s" % (sliceName, k), filedesc)
            addSiteContents(sliceNWBs, filesToInclude, slicePath, k)
        elif os.path.isfile(path):  # check if we need to handle it

            if fileShouldBeSkipped(path, filesToInclude):
                continue

            if path.endswith(".tif"):
                appendImageFileToNWB(sliceNWBs, path, filedesc)
            elif path.endswith(".ma"):
                appendMAFile(sliceNWBs, path, filedesc)
            else:
                raise NameError("Unexpected file type \"%s\" in index \"%s\"" %
                                (k, sliceIndex))
        else:
            raise NameError("Unexpected key \"%s\" in index \"%s\"" %
                            (k, sliceIndex))
def addSiteContents(siteNWBs, filesToInclude, slicePath, siteName):
    """ Add site specific entries to the NWB file """

    sitePath  = os.path.join(slicePath, siteName)
    siteIndex = os.path.join(sitePath, ".index")

    if len(siteNWBs) != 1:
        print "Expected exactly one NWB file belonging to site folder %s, skipping it." % sitePath
        return 1

    addDataSource(siteNWBs)

    dh = adm.getHandle(sitePath)
    dh.checkIndex()

    data = encodeAsJSONString(dh["."].info())
    appendMiscFileToNWB(siteNWBs, "%s_index_meta" % siteName, data)
    appendMiscFileToNWB(siteNWBs, "%s_index" % siteName, getFileContents(siteIndex))

    for k in dh.ls():

        if not dh.isManaged(k):
            continue

        path = os.path.join(sitePath, k)

        if os.path.isdir(path):
            raise NameError("Unexpected folder \"%s\" in \"%s\"." % (k, sitePath))
        elif os.path.isfile(path): # check if we need to handle it

            if fileShouldBeSkipped(path, filesToInclude):
                continue

            filedesc = encodeAsJSONString(dh[k].info())

            if path.endswith(".tif"):
                appendImageFileToNWB(siteNWBs, path, filedesc)
            elif path.endswith(".ma"):
                appendMAFile(siteNWBs, path, filedesc)
            elif path.endswith(".log"):
                appendPseudoYamlLog(siteNWBs, path, k, filedesc)
            else:
                raise NameError("Unexpected file type \"%s\" in index \"%s\"." % (k, siteIndex))
        else:
            raise NameError("Unexpected key \"%s\" in index \"%s\"." % (k, siteIndex))
예제 #10
0
 def uiChanged(self):
     for item in self.images:
         self.canvas.removeItem(item)
     self.images = []
     
     state = self.stateGroup.state()
     slice = state['slicePlaneCombo']
     hem = state['hemisphereCombo']
     #flip = state['flipCheck']
     
     imgs = self.slicePlanes[slice]
     atlasDir = os.path.split(os.path.abspath(__file__))[0]
     
     for imgFile, scale, pos in imgs:
         fh = DataManager.getHandle(os.path.join(atlasDir, 'images', imgFile))
         item = self.canvas.addFile(fh, pos=pos, scale=[scale,scale], index=0, movable=False, z=10000)
         #item = self.canvas.addFile(fh, index=0, movable=False)
         self.images.append(item)
def buildCombinedNWBInternal(basepath, siteNWBs, filesToInclude):
    """ NOT FOR PUBLIC USE """

    # we have three types of keys in the main index file
    # ---------------------------------------------------------------------
    # '.'               | common description of the experiment | (unique)
    # '$existingFile'   | log file of the experiment           | (multiple)
    # '$existingFolder' | different slices for each experiment | (multiple)

    dh = adm.getHandle(basepath)
    dh.checkIndex()

    data = encodeAsJSONString(dh["."].info())
    appendMiscFileToNWB(siteNWBs, basename = "main_index_meta", content = data)

    logfile = os.path.join(basepath, '.index')
    appendMiscFileToNWB(siteNWBs, basename = "main_index", content = getFileContents(logfile))

    for elem in siteNWBs:
        os.remove(deriveOutputNWB(elem))

    for k in dh.ls():
        if not dh.isManaged(k):
            continue

        path = os.path.abspath(os.path.join(basepath, k))

        if os.path.isdir(path): # slice folder
            addSliceContents(siteNWBs, filesToInclude, basepath, k)
        elif os.path.isfile(path): # main log file

            data = encodeAsJSONString(dh[k].info())
            appendMiscFileToNWB(siteNWBs, basename = "main_logfile_meta", content = data)
            appendMiscFileToNWB(siteNWBs, basename = "main_logfile", content = getFileContents(path))
        else:
            raise NameError("Unexpected key \"%s\" in index \"%s\"" % (k, logfile))

    combinedNWBs = []

    for elem in siteNWBs:
        combinedNWBs.append(deriveOutputNWB(elem))

    return combinedNWBs
def addSliceContents(siteNWBs, filesToInclude, basepath, sliceName):
    """ Add entries to the slice specific NWB file """

    slicePath  = os.path.join(basepath, sliceName)
    sliceIndex = os.path.join(slicePath, ".index")

    sliceNWBs = [elem for elem in siteNWBs if elem.startswith(slicePath)]

    if len(sliceNWBs) == 0:
        #print "No NWB files belong to slice folder %s, skipping it." % slicePath
        return 1

    dh = adm.getHandle(slicePath)
    dh.checkIndex()

    data = encodeAsJSONString(dh["."].info())
    appendMiscFileToNWB(siteNWBs, "%s_index_meta" % sliceName, data)
    appendMiscFileToNWB(siteNWBs, "%s_index" % sliceName, getFileContents(sliceIndex))

    for k in dh.ls():
        if not dh.isManaged(k):
            continue

        path = os.path.abspath(os.path.join(slicePath, k))

        filedesc = encodeAsJSONString(dh[k].info())

        if os.path.isdir(path): # site folder
            appendMiscFileToNWB(sliceNWBs, "%s_%s" % (sliceName, k), filedesc)
            addSiteContents(sliceNWBs, filesToInclude, slicePath, k)
        elif os.path.isfile(path): # check if we need to handle it

            if fileShouldBeSkipped(path, filesToInclude):
                continue

            if path.endswith(".tif"):
                appendImageFileToNWB(sliceNWBs, path, filedesc)
            elif path.endswith(".ma"):
                appendMAFile(sliceNWBs, path, filedesc)
            else:
                raise NameError("Unexpected file type \"%s\" in index \"%s\"" % (k, sliceIndex))
        else:
            raise NameError("Unexpected key \"%s\" in index \"%s\"" % (k, sliceIndex))
예제 #13
0
 def baseDir(self):
     """Return a dirHandle for the base directory used for all file names in the database."""
     if self._baseDir is None:
         dirName = self.ctrlParam('BaseDirectory')
         self._baseDir = DataManager.getHandle(dirName)
     return self._baseDir
예제 #14
0
 def baseDir(self):
     """Return a dirHandle for the base directory used for all file names in the database."""
     if self._baseDir is None:
         dirName = self.ctrlParam('BaseDirectory')
         self._baseDir = DataManager.getHandle(dirName)
     return self._baseDir