| Conditions | 13 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like deepreg.config.parser.config_sanity_check() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import collections.abc |
||
| 74 | def config_sanity_check(config: dict) -> dict: |
||
| 75 | """ |
||
| 76 | Check if the given config satisfies the requirements. |
||
| 77 | |||
| 78 | :param config: entire config. |
||
| 79 | """ |
||
| 80 | |||
| 81 | # check data |
||
| 82 | data_config = config["dataset"] |
||
| 83 | |||
| 84 | if data_config["type"] not in ["paired", "unpaired", "grouped"]: |
||
| 85 | raise ValueError(f"data type must be paired / unpaired / grouped, got {type}.") |
||
| 86 | |||
| 87 | if data_config["format"] not in ["nifti", "h5"]: |
||
| 88 | raise ValueError(f"data format must be nifti / h5, got {format}.") |
||
| 89 | |||
| 90 | assert "dir" in data_config |
||
| 91 | for mode in ["train", "valid", "test"]: |
||
| 92 | assert mode in data_config["dir"].keys() |
||
| 93 | data_dir = data_config["dir"][mode] |
||
| 94 | if data_dir is None: |
||
| 95 | logging.warning(f"Data directory for {mode} is not defined.") |
||
| 96 | if not (isinstance(data_dir, (str, list)) or data_dir is None): |
||
| 97 | raise ValueError( |
||
| 98 | f"data_dir for mode {mode} must be string or list of strings," |
||
| 99 | f"got {data_dir}." |
||
| 100 | ) |
||
| 101 | |||
| 102 | # back compatibility support |
||
| 103 | config = parse_v011(config) |
||
| 104 | |||
| 105 | # check model |
||
| 106 | if config["train"]["method"] == "conditional": |
||
| 107 | if data_config["labeled"] is False: # unlabeled |
||
| 108 | raise ValueError( |
||
| 109 | "For conditional model, data have to be labeled, got unlabeled data." |
||
| 110 | ) |
||
| 111 | |||
| 112 | # loss weights should >= 0 |
||
| 113 | for name in ["image", "label", "regularization"]: |
||
| 114 | loss_config = config["train"]["loss"][name] |
||
| 115 | if not isinstance(loss_config, list): |
||
| 116 | loss_config = [loss_config] |
||
| 117 | |||
| 118 | for loss_i in loss_config: |
||
| 119 | loss_weight = loss_i["weight"] |
||
| 120 | if loss_weight <= 0: |
||
| 121 | logging.warning( |
||
| 122 | "The %s loss weight %.2f is not positive.", name, loss_weight |
||
| 123 | ) |
||
| 124 | |||
| 125 | return config |
||
| 126 |