Skip to content

Commit

Permalink
merging remote into local
Browse files Browse the repository at this point in the history
  • Loading branch information
ybouilla committed May 16, 2022
2 parents e4f9171 + 4e33feb commit a9189e7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 45 deletions.
17 changes: 0 additions & 17 deletions fedbiomed/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
from . import constants, exceptions, message, logger, utils
from .metrics import Metrics, MetricTypes
from .messaging import Messaging
from .singleton import SingletonMeta
from .tasks_queue import TasksQueue
from .repository import Repository

__all__ = ["constants",
"exceptions",
"logger",
"message",
"utils",
"Messaging",
"Metrics",
"Repository",
"SingletonMeta",
"TasksQueue"]
4 changes: 0 additions & 4 deletions fedbiomed/common/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ def serialize_msg(msg: dict) -> str:
return json.dumps(msg)


def _deserialize_training_args(msg):
"""TrainingArgs is a class and must be specifically deserialized"""
pass


def _serialize_training_args(msg):
"""TrainingArgs is a class and must be specifically serialized"""
Expand Down
34 changes: 18 additions & 16 deletions fedbiomed/common/training_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from fedbiomed.common.exceptions import FedbiomedUserInputError
from fedbiomed.common.logger import logger
from fedbiomed.common.metrics import MetricTypes
from fedbiomed.common.validator import SchemeValidator, ValidateError, \
RuleError, validator_decorator
from fedbiomed.common.validator import SchemeValidator, ValidatorError, \
ValidateError, RuleError, validator_decorator


class TrainingArgs():
Expand All @@ -32,13 +32,17 @@ def __init__(self, ta: Dict = None, extra_scheme: Dict = None, only_required: bo
update the scheme of the default training args.
Warning: this is a dangerous featuret, provided to
developpers, to ease the test of future Fed-Biomed features
only_required: if True, the object is initialized only with required
values defined in the default_scheme (+ extra_scheme).
If False, then all default values will also be returned
(not only the required key/value pairs).
Raises:
FedbiomedUserInputError: in case of bad value or bad extra_scheme
"""
self._scheme = TrainingArgs.default_scheme()

if extra_scheme is None:
if not isinstance(extra_scheme, dict):
extra_scheme = {}

for k in extra_scheme:
Expand All @@ -60,7 +64,7 @@ def __init__(self, ta: Dict = None, extra_scheme: Dict = None, only_required: bo
try:
self._ta = self._sc.populate_with_defaults( ta,
only_required = only_required)
except RuleError as e:
except ValidatorError as e:
# scheme has required keys without defined default value
msg = ErrorNumbers.FB410.value + f": {e}"
logger.critical(msg)
Expand Down Expand Up @@ -110,7 +114,7 @@ def _test_ratio_hook( v: Any) -> bool:

@staticmethod
@validator_decorator
def _loss_rate_hook( v: Any) -> bool:
def _lr_hook( v: Any):
"""
Test if lr is greater than 0.
"""
Expand All @@ -125,9 +129,9 @@ def default_scheme(cls) -> Dict:
Returns the default (base) scheme for TrainingArgs.
"""
return {
# loss rate
# lr
"lr": {
"rules": [ float, cls._loss_rate_hook ],
"rules": [ float, cls._lr_hook ],
"required": False,
# "default": 0.01
},
Expand Down Expand Up @@ -341,17 +345,15 @@ def default_value(self, key: str) -> Any:
raise FedbiomedUserInputError(msg)


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

if 'test_metric' in self._ta and \
isinstance(self._ta['test_metric'], MetricTypes):
ta = deepcopy(self._ta)
if 'test_metric' in ta and \
isinstance(ta['test_metric'], MetricTypes):
# replace MetricType value by a string
ta = deepcopy(self._ta)
ta['test_metric'] = self._ta['test_metric'].name
return ta

return self._ta
ta['test_metric'] = ta['test_metric'].name
return ta


def get(self, key: str, default: Any = None) -> Any:
Expand Down
12 changes: 9 additions & 3 deletions fedbiomed/common/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def my_validation_funct( value ):
import inspect
import sys

from copy import deepcopy
from enum import Enum
from typing import Any, Callable, Dict, Union

Expand Down Expand Up @@ -243,7 +244,7 @@ def validate(self, value: Dict) -> bool:
Validate a value against the scheme passed at creation time.
Args:
value (dict): value (json) to validate against the scheme passed
value (dict): value (dict) to validate against the scheme passed
at __init__
Returns:
True if value is valid
Expand Down Expand Up @@ -296,13 +297,18 @@ def populate_with_defaults(self, value: Dict, only_required: bool = True) -> Dic
Raises:
RuleError: if scheme provided at init contains a required rules without default value
ValidatorError: if input value was not a dict
"""
if not self.is_valid(): # pragma: no cover
return {}

# check the value against the scheme
result = value
if isinstance(value, dict):
result = deepcopy(value)
else:
raise ValidatorError("input value is not a dict")


for k, v in self._scheme.items():
if 'required' in v and v['required'] is True:

Expand Down
3 changes: 1 addition & 2 deletions tests/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,8 @@ def test_experiment_13_set_training_arguments(self):
# test update of testing_args with argument `reset` set to False
ma_expected_2 = {'lr': 0.2}
train_args_2 = self.test_exp.set_training_args(ma_expected_2, reset=False).dict()
ma_expected_2.update(ma_expected)
ma_expected_2.update(ma_expected_2)
self.assertSubDictInDict(ma_expected_2, train_args_2)
self.assertSubDictInDict(ma_expected, train_args_2)

# test update of testing_args with argument `reset` set to True
ma_expected_3 = {'lr': 1e-4}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_training_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_training_args_01_init(self):

# lr has no default value
with self.assertRaises(FedbiomedUserInputError):
self.assertEqual( t['lr'], t.default_value('lr'))
t.default_value('lr')

# and test_ratio has
self.assertEqual( t['test_ratio'], t.default_value('test_ratio'))
Expand Down
11 changes: 9 additions & 2 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def test_validator_03_registration(self):


def test_validator_04_another_one(self):

"""
provide my own hook
"""
v = Validator()
self.assertTrue(v.register( 'probability', self.hook_probability_check))

Expand All @@ -205,6 +207,9 @@ def test_validator_04_another_one(self):


def test_validator_05_without_decorator(self):
"""
provide a hook without using the @validator_decorator
"""
v = Validator()

rule_name = 'rule_02'
Expand All @@ -222,7 +227,9 @@ def test_validator_05_without_decorator(self):


def test_validator_06_strict_or_not(self):

"""
test the script flag
"""
v = Validator()

rule_name = 'this_rule_is_unknown'
Expand Down

0 comments on commit a9189e7

Please sign in to comment.