def openProjectFile(cls, projectFilePath): """ Class method. Attempt to open the given path to an existing project file. If it doesn't exist, raise a ``ProjectManager.FileMissingError``. If its version is outdated, raise a ``ProjectManager.ProjectVersionError.`` """ logger.info("Opening Project: " + projectFilePath) if not os.path.exists(projectFilePath): raise ProjectManager.FileMissingError() # Open the file as an HDF5 file try: hdf5File = h5py.File(projectFilePath) readOnly = False except IOError: # Maybe the project is read-only hdf5File = h5py.File(projectFilePath, 'r') readOnly = True projectVersion = "0.5" if "ilastikVersion" in hdf5File.keys(): projectVersion = hdf5File["ilastikVersion"].value # FIXME: version comparison if not isVersionCompatible(projectVersion): # Must use _importProject() for old project files. raise ProjectManager.ProjectVersionError(projectVersion, ilastik.__version__) return (hdf5File, readOnly)
def deserializeFromHdf5(self, hdf5File, projectFilePath, headless=False): """Read the the current applet state from the given hdf5File handle, which should already be open. Subclasses should **not** override this method. Instead, subclasses override the 'private' version, *_deserializeFromHdf5* :param hdf5File: An h5py.File handle to the project file, which should already be open :param projectFilePath: The path to the given file handle. (Most serializers do not use this parameter.) :param headless: Are we called in headless mode? (in headless mode corrupted files cannot be fixed via the GUI) """ # Check the overall file version fileVersion = hdf5File["ilastikVersion"].value # Make sure we can find our way around the project tree if not isVersionCompatible(fileVersion): return self.progressSignal.emit(0) # If the top group isn't there, call initWithoutTopGroup try: topGroup = hdf5File[self.topGroupName] groupVersion = topGroup['StorageVersion'][()] except KeyError: topGroup = None groupVersion = None try: if topGroup is not None: inc = self.progressIncrement() for ss in self.serialSlots: ss.deserialize(topGroup) self.progressSignal.emit(inc) # Call the subclass to do remaining work if self.caresOfHeadless: self._deserializeFromHdf5(topGroup, groupVersion, hdf5File, projectFilePath, headless) else: self._deserializeFromHdf5(topGroup, groupVersion, hdf5File, projectFilePath) else: self.initWithoutTopGroup(hdf5File, projectFilePath) finally: self.progressSignal.emit(100)
def deserializeFromHdf5(self, hdf5File, projectFilePath, headless = False): """Read the the current applet state from the given hdf5File handle, which should already be open. Subclasses should **not** override this method. Instead, subclasses override the 'private' version, *_deserializeFromHdf5* :param hdf5File: An h5py.File handle to the project file, which should already be open :param projectFilePath: The path to the given file handle. (Most serializers do not use this parameter.) :param headless: Are we called in headless mode? (in headless mode corrupted files cannot be fixed via the GUI) """ # Check the overall file version fileVersion = hdf5File["ilastikVersion"].value # Make sure we can find our way around the project tree if not isVersionCompatible(fileVersion): return self.progressSignal.emit(0) # If the top group isn't there, call initWithoutTopGroup try: topGroup = hdf5File[self.topGroupName] groupVersion = topGroup['StorageVersion'][()] except KeyError: topGroup = None groupVersion = None try: if topGroup is not None: inc = self.progressIncrement() for ss in self.serialSlots: ss.deserialize(topGroup) self.progressSignal.emit(inc) # Call the subclass to do remaining work if self.caresOfHeadless: self._deserializeFromHdf5(topGroup, groupVersion, hdf5File, projectFilePath, headless) else: self._deserializeFromHdf5(topGroup, groupVersion, hdf5File, projectFilePath) else: self.initWithoutTopGroup(hdf5File, projectFilePath) finally: self.progressSignal.emit(100)
def openProjectFile(cls, projectFilePath, forceReadOnly=False): """ Class method. Attempt to open the given path to an existing project file. If it doesn't exist, raise a ``ProjectManager.FileMissingError``. If its version is outdated, raise a ``ProjectManager.ProjectVersionError.`` """ projectFilePath = os.path.expanduser(projectFilePath) logger.info("Opening Project: " + projectFilePath) if not os.path.exists(projectFilePath): raise ProjectManager.FileMissingError(projectFilePath) # Open the file as an HDF5 file try: if forceReadOnly: mode = "r" else: mode = "r+" hdf5File = h5py.File(projectFilePath, mode) except IOError: # Maybe we tried 'r+', but the project is read-only hdf5File = h5py.File(projectFilePath, "r") readOnly = hdf5File.mode == "r" projectVersion = "0.5" if "ilastikVersion" in list(hdf5File.keys()): projectVersion = hdf5File["ilastikVersion"].value.decode("utf-8") # FIXME: version comparison if not isVersionCompatible(projectVersion): # Must use _importProject() for old project files. raise ProjectManager.ProjectVersionError(projectVersion, ilastik.__version__) workflow_class = None if "workflowName" in list(hdf5File.keys()): # if workflow is found in file, take it workflowName = hdf5File["workflowName"].value.decode("utf-8") workflow_class = getWorkflowFromName(workflowName) return (hdf5File, workflow_class, readOnly)
def openProjectFile(cls, projectFilePath, forceReadOnly=False): """ Class method. Attempt to open the given path to an existing project file. If it doesn't exist, raise a ``ProjectManager.FileMissingError``. If its version is outdated, raise a ``ProjectManager.ProjectVersionError.`` """ projectFilePath = os.path.expanduser(projectFilePath) logger.info("Opening Project: " + projectFilePath) if not os.path.exists(projectFilePath): raise ProjectManager.FileMissingError(projectFilePath) # Open the file as an HDF5 file try: if forceReadOnly: mode = 'r' else: mode = 'r+' hdf5File = h5py.File(projectFilePath, mode) except IOError: # Maybe we tried 'r+', but the project is read-only hdf5File = h5py.File(projectFilePath, 'r') readOnly = (hdf5File.mode == 'r') projectVersion = "0.5" if "ilastikVersion" in hdf5File.keys(): projectVersion = hdf5File["ilastikVersion"].value # FIXME: version comparison if not isVersionCompatible(projectVersion): # Must use _importProject() for old project files. raise ProjectManager.ProjectVersionError(projectVersion, ilastik.__version__) workflow_class = None if "workflowName" in hdf5File.keys(): #if workflow is found in file, take it workflowName = hdf5File["workflowName"].value workflow_class = getWorkflowFromName(workflowName) return (hdf5File, workflow_class, readOnly)
def serializeToHdf5(self, hdf5File, projectFilePath): """Serialize the current applet state to the given hdf5 file. Subclasses should **not** override this method. Instead, subclasses override the 'private' version, *_serializetoHdf5* :param hdf5File: An h5py.File handle to the project file, which should already be open :param projectFilePath: The path to the given file handle. (Most serializers do not use this parameter.) """ # Check the overall file version fileVersion = hdf5File["ilastikVersion"].value # Make sure we can find our way around the project tree if not isVersionCompatible(fileVersion): return topGroup = getOrCreateGroup(hdf5File, self.topGroupName) progress = 0 self.progressSignal.emit(progress) # Set the version key = 'StorageVersion' deleteIfPresent(topGroup, key) topGroup.create_dataset(key, data=self.version) try: inc = self.progressIncrement(topGroup) for ss in self.serialSlots: ss.serialize(topGroup) progress += inc self.progressSignal.emit(progress) # Call the subclass to do remaining work, if any self._serializeToHdf5(topGroup, hdf5File, projectFilePath) finally: self.progressSignal.emit(100)
def openProjectFile(cls, projectFilePath): """ Class method. Attempt to open the given path to an existing project file. If it doesn't exist, raise a ``ProjectManager.FileMissingError``. If its version is outdated, raise a ``ProjectManager.ProjectVersionError.`` """ logger.info("Opening Project: " + projectFilePath) if not os.path.exists(projectFilePath): raise ProjectManager.FileMissingError() # Open the file as an HDF5 file try: hdf5File = h5py.File(projectFilePath) readOnly = False except IOError: # Maybe the project is read-only hdf5File = h5py.File(projectFilePath, 'r') readOnly = True projectVersion = "0.5" if "ilastikVersion" in hdf5File.keys(): projectVersion = hdf5File["ilastikVersion"].value # FIXME: version comparison if not isVersionCompatible(projectVersion): # Must use _importProject() for old project files. raise ProjectManager.ProjectVersionError(projectVersion, ilastik.__version__) workflow_class = None if "workflowName" in hdf5File.keys(): #if workflow is found in file, take it workflowName = hdf5File["workflowName"].value workflow_class = getWorkflowFromName(workflowName) return (hdf5File, workflow_class, readOnly)