Skip to content

Parameter

AbstractParameter

Bases: abc.ABC

Source code in taskchain/parameter.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class AbstractParameter(abc.ABC):

    NO_DEFAULT = NO_DEFAULT
    NO_VALUE = NO_VALUE

    def __init__(self,
                 default: Any = NO_DEFAULT,
                 ignore_persistence: bool = False,
                 dont_persist_default_value: bool = True,
                 ):
        """
        Args:
            default: value used if not provided in config, default to NO_DEFAULT meaning that param is required
            ignore_persistence: do not use this parameter in persistence, useful params without influence on output
            dont_persist_default_value: if value of parameter is same as default, do not use it in persistence,
                useful for adding new parameters without recomputation of data
        """
        self.default = default
        self.ignore_persistence = ignore_persistence
        self.dont_persist_default_value = dont_persist_default_value

    @property
    def required(self) -> bool:
        return self.default == self.NO_DEFAULT

    @property
    @abc.abstractmethod
    def value(self) -> Any:
        pass

    @property
    @abc.abstractmethod
    def name(self) -> str:
        pass

    def value_repr(self):
        if isinstance(self.value, ParameterObject):
            return self.value.repr()
        if isinstance(self.value, Path):
            return repr(self._value)
        return repr(self.value)

    @property
    def repr(self) -> Union[str, None]:
        if self.ignore_persistence:
            return None

        if self.dont_persist_default_value and self.value == self.default:
            return None

        return f'{self.name}={self.value_repr()}'

__init__(default=NO_DEFAULT, ignore_persistence=False, dont_persist_default_value=True)

Parameters:

Name Type Description Default
default Any

value used if not provided in config, default to NO_DEFAULT meaning that param is required

NO_DEFAULT
ignore_persistence bool

do not use this parameter in persistence, useful params without influence on output

False
dont_persist_default_value bool

if value of parameter is same as default, do not use it in persistence, useful for adding new parameters without recomputation of data

True
Source code in taskchain/parameter.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def __init__(self,
             default: Any = NO_DEFAULT,
             ignore_persistence: bool = False,
             dont_persist_default_value: bool = True,
             ):
    """
    Args:
        default: value used if not provided in config, default to NO_DEFAULT meaning that param is required
        ignore_persistence: do not use this parameter in persistence, useful params without influence on output
        dont_persist_default_value: if value of parameter is same as default, do not use it in persistence,
            useful for adding new parameters without recomputation of data
    """
    self.default = default
    self.ignore_persistence = ignore_persistence
    self.dont_persist_default_value = dont_persist_default_value

Parameter

Bases: AbstractParameter

Source code in taskchain/parameter.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class Parameter(AbstractParameter):

    NO_DEFAULT = NO_DEFAULT
    NO_VALUE = NO_VALUE

    def __init__(self,
                 name: str,
                 dtype: Union[type] = None,
                 default: Any = NO_DEFAULT,
                 name_in_config: str = None,
                 ignore_persistence: bool = False,
                 dont_persist_default_value: bool = False,
                 ):
        """
        Args:
            name: name for referencing from task
            dtype: expected datatype
            default: value used if not provided in config, default to NO_DEFAULT meaning that param is required
            name_in_config: name used for search in config, defaults to `name` argument
            ignore_persistence: do not use this parameter in persistence, useful params without influence on output
            dont_persist_default_value: if value of parameter is same as default, do not use it in persistence,
                useful for adding new parameters without recomputation of data
        """
        super().__init__(
            default=default,
            ignore_persistence=ignore_persistence,
            dont_persist_default_value=dont_persist_default_value,
        )
        assert name not in taskchain.config.Config.RESERVED_PARAMETER_NAMES
        self._name = name
        self.dtype = dtype
        self.name_in_config = name if name_in_config is None else name_in_config
        self._value = self.NO_VALUE

    def __str__(self):
        return self.name

    @property
    def name(self) -> str:
        return self._name

    @property
    def value(self) -> Any:
        if self._value == self.NO_VALUE:
            raise ValueError(f'Value not set for parameter `{self}`')

        if self.dtype is Path and self._value is not None:
            return Path(self._value)
        return self._value

    def set_value(self, config) -> Any:
        if self.name_in_config in config:
            value = config[self.name_in_config]
        else:
            if self.required:
                raise ValueError(f'Value for parameter `{self}` not found in config `{config}`')
            value = self.default

        if self.dtype is not None:
            if value is not None and not isinstance(value, self.dtype) and not(self.dtype is Path and isinstance(value, str)):
                raise ValueError(f'Value `{value}` of parameter `{self}` has type {type(value)} instead of `{self.dtype}`')

        self._value = value
        return value

__init__(name, dtype=None, default=NO_DEFAULT, name_in_config=None, ignore_persistence=False, dont_persist_default_value=False)

Parameters:

Name Type Description Default
name str

name for referencing from task

required
dtype Union[type]

expected datatype

None
default Any

value used if not provided in config, default to NO_DEFAULT meaning that param is required

NO_DEFAULT
name_in_config str

name used for search in config, defaults to name argument

None
ignore_persistence bool

do not use this parameter in persistence, useful params without influence on output

False
dont_persist_default_value bool

if value of parameter is same as default, do not use it in persistence, useful for adding new parameters without recomputation of data

False
Source code in taskchain/parameter.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def __init__(self,
             name: str,
             dtype: Union[type] = None,
             default: Any = NO_DEFAULT,
             name_in_config: str = None,
             ignore_persistence: bool = False,
             dont_persist_default_value: bool = False,
             ):
    """
    Args:
        name: name for referencing from task
        dtype: expected datatype
        default: value used if not provided in config, default to NO_DEFAULT meaning that param is required
        name_in_config: name used for search in config, defaults to `name` argument
        ignore_persistence: do not use this parameter in persistence, useful params without influence on output
        dont_persist_default_value: if value of parameter is same as default, do not use it in persistence,
            useful for adding new parameters without recomputation of data
    """
    super().__init__(
        default=default,
        ignore_persistence=ignore_persistence,
        dont_persist_default_value=dont_persist_default_value,
    )
    assert name not in taskchain.config.Config.RESERVED_PARAMETER_NAMES
    self._name = name
    self.dtype = dtype
    self.name_in_config = name if name_in_config is None else name_in_config
    self._value = self.NO_VALUE

ParameterObject

Bases: abc.ABC

Every class used in configs has to be inherit from this class.

Source code in taskchain/parameter.py
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
class ParameterObject(abc.ABC):
    """
    Every class used in configs has to be inherit from this class.
    """

    @abc.abstractmethod
    def repr(self) -> str:
        """
        Representation which should uniquely describe object,
        i.e. be based on all arguments of __init__.
        """
        raise NotImplemented

    def __repr__(self):
        return self.repr()

repr() abstractmethod

Representation which should uniquely describe object, i.e. be based on all arguments of init.

Source code in taskchain/parameter.py
222
223
224
225
226
227
228
@abc.abstractmethod
def repr(self) -> str:
    """
    Representation which should uniquely describe object,
    i.e. be based on all arguments of __init__.
    """
    raise NotImplemented

AutoParameterObject

Bases: ParameterObject

ParameterObject with automatic repr method based on arguments of init method.

For correct functionality, is necessary store all init argument values as self.arg_name or self._arg_name

Source code in taskchain/parameter.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
class AutoParameterObject(ParameterObject):
    """
    ParameterObject with automatic `repr` method based on arguments of __init__ method.

    For correct functionality, is necessary store all __init__ argument values as self.arg_name or self._arg_name
    """

    def repr(self) -> str:
        parameters = signature(self.__init__).parameters

        ignore_persistence_args = self.ignore_persistence_args()
        dont_persist_default_value_args = self.dont_persist_default_value_args()
        args = {}
        for i, (arg, parameter) in enumerate(parameters.items()):
            if arg in ignore_persistence_args:
                continue
            if hasattr(self, '_' + arg):
                value = getattr(self, '_' + arg)
            elif hasattr(self, arg):
                value = getattr(self, arg)
            else:
                raise AttributeError(f'Value of __init__ argument `{arg}` not found for class `{fullname(self.__class__)}`, '
                                     f'make sure that value is saved in `self.{arg}` or `self._{arg}`')
            if isinstance(value, IgnoreForPersistence):
                continue
            if arg in dont_persist_default_value_args and value == parameter.default:
                continue
            args[arg] = IgnoreForPersistence.remove(value)
        args_repr = ', '.join(f'{k}={repr(v)}' for k, v in sorted(args.items()))
        assert 'object at 0x' not in args_repr, f'repr for arguments is fragile: {args_repr}'
        return f'{self.__class__.__name__}({args_repr})'

    @staticmethod
    def ignore_persistence_args() -> List[str]:
        """ List of __init__ argument names which are ignored in persistence. """
        return ['verbose', 'debug']

    @staticmethod
    def dont_persist_default_value_args() -> List[str]:
        """
        List of __init__ argument names which are ignored in persistence
        when they take default value.
        """
        return []

ignore_persistence_args() staticmethod

List of init argument names which are ignored in persistence.

Source code in taskchain/parameter.py
292
293
294
295
@staticmethod
def ignore_persistence_args() -> List[str]:
    """ List of __init__ argument names which are ignored in persistence. """
    return ['verbose', 'debug']

dont_persist_default_value_args() staticmethod

List of init argument names which are ignored in persistence when they take default value.

Source code in taskchain/parameter.py
297
298
299
300
301
302
303
@staticmethod
def dont_persist_default_value_args() -> List[str]:
    """
    List of __init__ argument names which are ignored in persistence
    when they take default value.
    """
    return []