Skip to content

Commit

Permalink
Merge pull request #2840 from k-dominik/update-classifier-serialization
Browse files Browse the repository at this point in the history
Update classifier serialization
  • Loading branch information
k-dominik committed May 6, 2024
2 parents 126b3e1 + fb29c44 commit 1df74b8
Show file tree
Hide file tree
Showing 16 changed files with 1,452 additions and 394 deletions.
1 change: 0 additions & 1 deletion ilastik/applets/base/appletSerializer/__init__.py
Expand Up @@ -20,7 +20,6 @@
###############################################################################
from .appletSerializer import AppletSerializer as AppletSerializer
from .serializerUtils import deleteIfPresent as deleteIfPresent
from .serializerUtils import getOrCreateGroup as getOrCreateGroup
from .slotSerializer import JSONSerialSlot as JSONSerialSlot
from .slotSerializer import SerialBlockSlot as SerialBlockSlot
from .slotSerializer import SerialClassifierFactorySlot as SerialClassifierFactorySlot
Expand Down
24 changes: 13 additions & 11 deletions ilastik/applets/base/appletSerializer/appletSerializer.py
Expand Up @@ -19,20 +19,20 @@
# http://ilastik.org/license.html
###############################################################################
import logging
from abc import ABCMeta
from abc import ABC

from future.utils import with_metaclass
import h5py

from ilastik.config import cfg as ilastik_config
from ilastik.utility.maybe import maybe
from lazyflow.utility.orderedSignal import OrderedSignal

from .serializerUtils import deleteIfPresent, getOrCreateGroup
from .serializerUtils import deleteIfPresent

logger = logging.getLogger(__name__)


class AppletSerializer(with_metaclass(ABCMeta, object)):
class AppletSerializer(ABC):
"""
Base class for all AppletSerializers.
"""
Expand All @@ -49,14 +49,16 @@ class IncompatibleProjectVersionError(Exception):
# Semi-abstract methods #
#########################

def _serializeToHdf5(self, topGroup, hdf5File, projectFilePath):
def _serializeToHdf5(self, topGroup: h5py.Group, hdf5File: h5py.File, projectFilePath):
"""Child classes should override this function, if
necessary.
"""
pass

def _deserializeFromHdf5(self, topGroup, groupVersion, hdf5File, projectFilePath, headless=False):
def _deserializeFromHdf5(
self, topGroup: h5py.Group, groupVersion, hdf5File: h5py.File, projectFilePath, headless=False
):
"""Child classes should override this function, if
necessary.
Expand All @@ -67,7 +69,7 @@ def _deserializeFromHdf5(self, topGroup, groupVersion, hdf5File, projectFilePath
# Base class implementation #
#############################

def __init__(self, topGroupName, slots=None, operator=None):
def __init__(self, topGroupName: str, slots=None, operator=None):
"""Constructor. Subclasses must call this method in their own
__init__ functions. If they fail to do so, the shell raises an
exception.
Expand Down Expand Up @@ -95,7 +97,7 @@ def isDirty(self):
"""
return any(list(ss.dirty for ss in self.serialSlots))

def shouldSerialize(self, hdf5File):
def shouldSerialize(self, hdf5File: h5py.File):
"""Whether to serialize or not."""

if self.isDirty():
Expand All @@ -104,7 +106,7 @@ def shouldSerialize(self, hdf5File):
# Need to check if slots should be serialized. First must verify that self.topGroupName is not an empty string
# (as this seems to happen sometimes).
if self.topGroupName:
topGroup = getOrCreateGroup(hdf5File, self.topGroupName)
topGroup = hdf5File.require_group(self.topGroupName)
return any([ss.shouldSerialize(topGroup) for ss in self.serialSlots])

return False
Expand Down Expand Up @@ -135,7 +137,7 @@ def progressIncrement(self, group=None):
return 0
return divmod(100, nslots)[0]

def serializeToHdf5(self, hdf5File, projectFilePath):
def serializeToHdf5(self, hdf5File: h5py.File, projectFilePath):
"""Serialize the current applet state to the given hdf5 file.
Subclasses should **not** override this method. Instead,
Expand All @@ -148,7 +150,7 @@ def serializeToHdf5(self, hdf5File, projectFilePath):
(Most serializers do not use this parameter.)
"""
topGroup = getOrCreateGroup(hdf5File, self.topGroupName)
topGroup = hdf5File.require_group(self.topGroupName)

progress = 0
self.progressSignal(progress)
Expand Down

0 comments on commit 1df74b8

Please sign in to comment.