Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ybouilla committed May 16, 2022
1 parent 44fca42 commit e4f9171
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
16 changes: 8 additions & 8 deletions fedbiomed/common/training_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self, ta: Dict = None, extra_scheme: Dict = None, only_required: bo
# validators
@staticmethod
@validator_decorator
def _metric_validation_hook( metric: Union[MetricTypes, str, None] ):
def _metric_validation_hook( metric: Union[MetricTypes, str, None] ) -> Union[bool, str]:
"""
Validate the metric argument of test_metric.
"""
Expand All @@ -99,7 +99,7 @@ def _metric_validation_hook( metric: Union[MetricTypes, str, None] ):

@staticmethod
@validator_decorator
def _test_ratio_hook( v: Any):
def _test_ratio_hook( v: Any) -> bool:
"""
Test if in [ 0.0 , 1.0] interval.
"""
Expand All @@ -110,7 +110,7 @@ def _test_ratio_hook( v: Any):

@staticmethod
@validator_decorator
def _loss_rate_hook( v: Any):
def _loss_rate_hook( v: Any) -> bool:
"""
Test if lr is greater than 0.
"""
Expand Down Expand Up @@ -264,7 +264,7 @@ def __getitem__(self, key: str) -> Any:

def update(self, values: Dict) -> TypeVar("TrainingArgs"):
"""
Update multiple keys of the trainig arguments.
Update multiple keys of the training arguments.
Args:
values: a dictionnary of keys to validate/update
Expand All @@ -290,9 +290,9 @@ def __ixor__(self, other: Dict) -> TypeVar("TrainingArgs"):
t ^= { 'epochs': 2 , 'lr': 0.01 }
```
Args:
other: a dictionnary of keys to validate/update
other (Dict): a dictionnary of keys to validate/update
Return:
Returns:
the object itself after modification
Raises:
Expand All @@ -318,7 +318,7 @@ def default_value(self, key: str) -> Any:
Returns the default value for the key.
Args:
key: key
key (str): key
Returns:
value: the default value associated to the key
Expand All @@ -341,7 +341,7 @@ def default_value(self, key: str) -> Any:
raise FedbiomedUserInputError(msg)


def dict(self):
def dict(self) -> Dict:
"""Returns the training_args as a dictionnary."""

if 'test_metric' in self._ta and \
Expand Down
24 changes: 12 additions & 12 deletions fedbiomed/common/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def my_validation_funct( value ):
This class provides json validation against a scheme describing the
expected json content.
The scheme need to follow a specific format, which describe each
The scheme needs to follow a specific format, which describes each
allowed fields and their characteristics:
- a list of associated validators to check against (aka Validator instances)
- the field requirement (required on not)
- a default value (which will be used if the field is required but not provided)
A SchemeValidator is accepted byt the Validator class.
A SchemeValidator is accepted by the Validator class.
**Typical example:**
Expand Down Expand Up @@ -243,7 +243,7 @@ def validate(self, value: Dict) -> bool:
Validate a value against the scheme passed at creation time.
Args:
value: value (json) to validate against the scheme passed
value (dict): value (json) to validate against the scheme passed
at __init__
Returns:
True if value is valid
Expand Down Expand Up @@ -279,20 +279,20 @@ def populate_with_defaults(self, value: Dict, only_required: bool = True) -> Dic
"""
Inject default values defined in the rule to a given dictionary.
Parse the given json value and add default value is key was required
Parse the given json value and add default value if key was required
but not provided.
Of course, the default value must be provided in the scheme.
Of course, the default values must be provided in the scheme.
Warning: this does not parse the result against the scheme. It has
to be done by the user.
Args:
value: a json data to verify/populate
only_required: if True, only force required key. If False, update all
keys with default values in the scheme
value (dict): a json data to verify/populate
only_required (bool): if True, only force required key. If False, update all
keys with default values in the scheme. Defaults to True.
Return:
a json populated with default values, returns an empty dict if something is wrong
(dict) a json populated with default values, returns an empty dict if something is wrong
Raises:
RuleError: if scheme provided at init contains a required rules without default value
Expand Down Expand Up @@ -449,7 +449,7 @@ def validate(self, value: Any, rule: Any, strict: bool = True) -> bool:
True if rule exists and value is compliant.
Raises:
ValidateError: if provided value does not com[ly to the rule
ValidateError: if provided value does not comply to the rule
"""
# rule is in the rulebook -> execute the rule associated function
if isinstance(rule, str) and rule in self._validation_rulebook:
Expand Down Expand Up @@ -605,13 +605,13 @@ def register(self, rule: str, hook: Any, override: bool = False) -> bool:
Add a rule/validation_function to the rulebook.
if the rule (entry of the catalog) was already registered,
it will be rejected, except if ovverride is True
it will be rejected, except if override is True
Args:
rule: registration name (string)
hook: validation hook to register (the hook is checked against
the accepted hook types)
override: if True, still register the rule even if it existed
override: if True, still register the rule even if it existed. Defaults to False.
Returns:
True if rule is accepted, False instead if rule exists and overrride is False
Expand Down
5 changes: 3 additions & 2 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Tuple
import unittest
from fedbiomed.common.validator import Validator, SchemeValidator, \
validator_decorator, _ValidatorHookType
Expand All @@ -20,7 +21,7 @@ def tearDown(self):
# define some validation hooks to add to the validator
@staticmethod
@validator_decorator
def hook_01_positive_integer_check(value):
def hook_01_positive_integer_check(value) -> bool:
"""
value must be a positive integer
"""
Expand All @@ -29,7 +30,7 @@ def hook_01_positive_integer_check(value):
return True

@staticmethod
def hook_02_positive_integer_check(value):
def hook_02_positive_integer_check(value) -> Tuple[bool, str]:
"""
value must be a positive integer
Expand Down

0 comments on commit e4f9171

Please sign in to comment.