check.py 11.7 KB
Newer Older
1
2
3
import functools
import os
import warnings
mibaumgartner's avatar
mibaumgartner committed
4
from pathlib import Path
mibaumgartner's avatar
mibaumgartner committed
5
from typing import Dict, List, Sequence, Optional, Union
6

mibaumgartner's avatar
mibaumgartner committed
7
8
9
10
11
import numpy as np
import SimpleITK as sitk

from nndet.io import load_json, load_sitk
from nndet.io.paths import get_task, get_paths_from_splitted_dir
mibaumgartner's avatar
mibaumgartner committed
12
from nndet.utils.config import load_dataset_info
mibaumgartner's avatar
bugfix  
mibaumgartner committed
13
from nndet.utils.info import maybe_verbose_iterable
mibaumgartner's avatar
mibaumgartner committed
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
def env_guard(func):
    """
    Contextmanager to check nnDetection environment variables
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # we use print here because logging might not be initialized yet and
        # this is intended as a user warning.
        
        # det_data
        if os.environ.get("det_data", None) is None:
            raise RuntimeError(
                "'det_data' environment variable not set. "
                "Please refer to the installation instructions. "
                )

        # det_models
        if os.environ.get("det_models", None) is None:
            raise RuntimeError(
                "'det_models' environment variable not set. "
                "Please refer to the installation instructions. "
                )

        # OMP_NUM_THREADS
        if os.environ.get("OMP_NUM_THREADS", None) is None:
            raise RuntimeError(
                "'OMP_NUM_THREADS' environment variable not set. "
                "Please refer to the installation instructions. "
                )

        # det_num_threads
        if os.environ.get("det_num_threads", None) is None:
            warnings.warn(
                "Warning: 'det_num_threads' environment variable not set. "
                "Please read installation instructions again. "
                "Training will not work properly.")

        # det_verbose
        if os.environ.get("det_verbose", None) is None:
            print("'det_verbose' environment variable not set. "
                  "Continue in verbose mode.")

        return func(*args, **kwargs)
    return wrapper


mibaumgartner's avatar
mibaumgartner committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def _check_key_missing(cfg: dict, key: str, ktype=None):
    if key not in cfg:
        raise ValueError(f"Dataset information did not contain "
                        f"'{key}' key, found {list(cfg.keys())}")
    
    if ktype is not None:
        if not isinstance(cfg[key], ktype):
            raise ValueError(f"Found {key} of type {type(cfg[key])} in "
                             f"dataset information but expected type {ktype}")


def check_dataset_file(task_name: str):
    """
    Run a sequence of checks to confirm correct format of dataset information

    Args:
        task_name: task identifier to check info for
    """
mibaumgartner's avatar
bugfix  
mibaumgartner committed
80
    print("Start dataset info check.")
mibaumgartner's avatar
mibaumgartner committed
81
82
83
84
85
86
87
88
89
90
91
92
    cfg = load_dataset_info(get_task(task_name))
    _check_key_missing(cfg, "task", ktype=str)
    _check_key_missing(cfg, "dim", ktype=int)
    _check_key_missing(cfg, "labels", ktype=dict)
    _check_key_missing(cfg, "modalities", ktype=dict)

    # check dim
    if dim := cfg["dim"] not in [2, 3]:
        raise ValueError(f"Found dim {dim} in dataset info but only support dim=2 or dim=3.")

    # check labels
    for key, item in cfg["labels"].items():
mibaumgartner's avatar
mibaumgartner committed
93
        if not isinstance(key, str):
mibaumgartner's avatar
mibaumgartner committed
94
95
            raise ValueError("Expected key of type string in dataset "
                             f"info labels but found {type(key)} : {key}")
mibaumgartner's avatar
mibaumgartner committed
96
        if not isinstance(item, str):
mibaumgartner's avatar
mibaumgartner committed
97
98
99
100
101
102
103
104
105
106
            raise ValueError("Expected name of type string in dataset "
                             f"info labels but found {type(item)} : {item}")
    found_classes = sorted(list(map(int, cfg["labels"].keys())))
    for ic, idx in enumerate(found_classes):
        if ic != idx:
            raise ValueError("Found wrong order of label classes in dataset info."
                             f"Found {found_classes} but expected {list(range(len(found_classes)))}")

    # check modalities
    for key, item in cfg["modalities"].items():
mibaumgartner's avatar
mibaumgartner committed
107
        if not isinstance(key, str):
mibaumgartner's avatar
mibaumgartner committed
108
109
            raise ValueError("Expected key of type string in dataset "
                             f"info labels but found {type(key)} : {key}")
mibaumgartner's avatar
mibaumgartner committed
110
        if not isinstance(item, str):
mibaumgartner's avatar
mibaumgartner committed
111
112
113
114
115
116
117
            raise ValueError("Expected name of type string in dataset "
                             f"info labels but found {type(item)} : {item}")
    found_mods = sorted(list(map(int, cfg["modalities"].keys())))
    for ic, idx in enumerate(found_classes):
        if ic != idx:
            raise ValueError("Found wrong order of modalities in dataset info."
                             f"Found {found_mods} but expected {list(range(len(found_mods)))}")
mibaumgartner's avatar
mibaumgartner committed
118

mibaumgartner's avatar
mibaumgartner committed
119
120
121
122
123
124
    # check target class
    target_class = cfg.get("target_class", None)
    if target_class is not None and not isinstance(target_class, int):
        raise ValueError("If target class is defined, it needs to be an integer, "
                         f"found {type(target_class)} : {target_class}")

mibaumgartner's avatar
bugfix  
mibaumgartner committed
125
    print("Dataset info check complete.")
mibaumgartner's avatar
mibaumgartner committed
126
127


mibaumgartner's avatar
mibaumgartner committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def check_data_and_label_splitted(
    task_name: str,
    test: bool = False,
    labels: bool = True,
    full_check: bool = True,
    ):
    """
    Perform checks of data and label in raw splitted format

    Args:
        task_name: name of task to check
        test: check test data
        labels: check labels
        full_check: Per default a full check will be performed which needs to
            load all files. If this is disabled, a computationall light check
            will be performed 

    Raises:
        ValueError: if not all raw splitted files were found
        ValueError: missing label info file
        ValueError: instances in label info file need to start at 1
        ValueError: instances in label info file need to be consecutive
    """
mibaumgartner's avatar
mibaumgartner committed
151
    print(f"Start data and label check: test={test}")
mibaumgartner's avatar
mibaumgartner committed
152
153
154
155
156
157
158
159
160
    cfg = load_dataset_info(get_task(task_name))

    splitted_paths = get_paths_from_splitted_dir(
        num_modalities=len(cfg["modalities"]),
        splitted_4d_output_dir=Path(os.getenv('det_data')) / task_name / "raw_splitted",
        labels=labels,
        test=test,
    )

mibaumgartner's avatar
bugfix  
mibaumgartner committed
161
    for case_paths in maybe_verbose_iterable(splitted_paths):
mibaumgartner's avatar
mibaumgartner committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        # check all files exist
        for cp in case_paths:
            if not Path(cp).is_file():
                raise ValueError(f"Expected {cp} to be a raw splitted "
                                 "data path but it does not exist.")

        if labels:
            # check label info (json files)
            mask_path = case_paths[-1]
            mask_info_path = mask_path.parent / f"{mask_path.stem.split('.')[0]}.json"
            if not Path(mask_info_path).is_file():
                raise ValueError(f"Expected {mask_info_path} to be a raw splitted "
                                "mask info path but it does not exist.")
            mask_info = load_json(mask_info_path)
mibaumgartner's avatar
mibaumgartner committed
176

177
            _type_check_instances_json(mask_info, mask_info_path, expected_labels=cfg["labels"])
mibaumgartner's avatar
mibaumgartner committed
178
179

            # check presence / absence of instances in json and mask
mibaumgartner's avatar
bug fix  
mibaumgartner committed
180
181
            if mask_info["instances"]:
                mask_info_instances = list(map(int, mask_info["instances"].keys()))
mibaumgartner's avatar
mibaumgartner committed
182

mibaumgartner's avatar
bug fix  
mibaumgartner committed
183
184
                if j := not min(mask_info_instances) == 1:
                    raise ValueError(f"Instance IDs need to start at 1, found {j} in {mask_info_path}")
mibaumgartner's avatar
mibaumgartner committed
185

mibaumgartner's avatar
bug fix  
mibaumgartner committed
186
187
188
189
                for i in range(1, len(mask_info_instances) + 1):
                    if i not in mask_info_instances:
                        raise ValueError(f"Exptected {i} to be an Instance ID in "
                                        f"{mask_info_path} but only found {mask_info_instances}")
mibaumgartner's avatar
mibaumgartner committed
190
191
192
193
194
        else:
            mask_info_path = None

        if full_check:
            _full_check(case_paths, mask_info_path)
mibaumgartner's avatar
bugfix  
mibaumgartner committed
195
    print("Data and label check complete.")
mibaumgartner's avatar
mibaumgartner committed
196
197


198
def _type_check_instances_json(mask_info: Dict, mask_info_path: Union[str, Path], expected_labels: list):
mibaumgartner's avatar
mibaumgartner committed
199
200
201
202
203
204
205
    """
    Check types of json files

    Args:
        mask_info: contains information loaded from the label json file.
            Specifically the `instances` key is checked for a "str":"int" type
        mask_info_path: path to json file where information was loaded from
206
        expected_labels: list with the expected labels
mibaumgartner's avatar
mibaumgartner committed
207
208
209
210
211

    Raises:
        ValueError: raised if instance ids are not typed as str
        ValueError: raised if instance classes are not typed as int
    """
212
213
    # transform expected labels to integer
    exp_labs = [int(lab) for lab in expected_labels]
mibaumgartner's avatar
mibaumgartner committed
214
215
216
217
218
219
220
221
    # type check instances key
    for key_instance_id, item_instance_cls in mask_info["instances"].items():
        if not isinstance(key_instance_id, str):
            raise ValueError(f"Instance ids need to be a str, found {type(key_instance_id)} "
                                f"of instance {key_instance_id} in {mask_info_path}")
        if not isinstance(item_instance_cls, int):
            raise ValueError(f"Instance classes needs to be an int, found {type(item_instance_cls)} "
                                f"of instance {key_instance_id} in {mask_info_path}")
222
223
        if not item_instance_cls in exp_labs:
            raise ValueError(f"Instance class {item_instance_cls} not defined in dataset.yml")
mibaumgartner's avatar
mibaumgartner committed
224
225


mibaumgartner's avatar
mibaumgartner committed
226
227
228
229
def _full_check(
    case_paths: List[Path],
    mask_info_path: Optional[Path] = None,
    ) -> None:
mibaumgartner's avatar
mibaumgartner committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    """
    Performas itk and instance chekcs on provided paths

    Args:
        case_paths: paths to all itk images to check properties
            if label is provided it needs to be at the last position
        mask_info_path: optionally check label properties. If None, no
            check of label properties will be performed.

    Raises:
        ValueError: Inconsistent instances in label info and label image

    See also:
        :func:`_check_itk_params`
    """
    img_itk_seq = [load_sitk(cp) for cp in case_paths]
    _check_itk_params(img_itk_seq, case_paths)

    if mask_info_path is not None:
        mask_itk = img_itk_seq[-1]
        mask_info = load_json(mask_info_path)
        info_instances = list(map(int, mask_info["instances"].keys()))
        mask_instances = np.unique(sitk.GetArrayViewFromImage(mask_itk))
        mask_instances = mask_instances[mask_instances > 0]

        for mi in mask_instances:
            if not mi in info_instances:
                raise ValueError(f"Found instance ID {mi} in mask which is "
mibaumgartner's avatar
mibaumgartner committed
258
                                f"not present in info {info_instances} in {mask_info_path}")
mibaumgartner's avatar
mibaumgartner committed
259
260
        if not len(info_instances) == len(mask_instances):
            raise ValueError("Found instances in info which are not present in mask: "
mibaumgartner's avatar
mibaumgartner committed
261
                            f"mask: {mask_instances} info {info_instances} in {mask_info_path}")
mibaumgartner's avatar
mibaumgartner committed
262
263


mibaumgartner's avatar
mibaumgartner committed
264
265
266
267
def _check_itk_params(
    img_seq: Sequence[sitk.Image],
    paths: Sequence[Path],
    ) -> None:
mibaumgartner's avatar
mibaumgartner committed
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
    """
    Check Dimension, Origin, Direction and Spacing of a Sequence of images

    Args:
        img_seq: sequence of images to check
        paths: correcponding paths of images (for error msg)

    Raises:
        ValueError: raised if dimensions do not match
        ValueError: raised if origin does not match
        ValueError: raised if direction does not match
        ValueError: raised if spacing does not match
    """
    for idx, img in enumerate(img_seq[1:], start=1):
        if not (np.asarray(img_seq[0].GetDimension()) == \
            np.asarray(img.GetDimension())).all():
            raise ValueError(f"Expected {paths[idx]} and {paths[0]} to have same dimensions!")
        if not (np.asarray(img_seq[0].GetOrigin()) == \
            np.asarray(img.GetOrigin())).all():
            raise ValueError(f"Expected {paths[idx]} and {paths[0]} to have same origin!")
        if not (np.asarray(img_seq[0].GetDirection()) == \
            np.asarray(img.GetDirection())).all():
            raise ValueError(f"Expected {paths[idx]} and {paths[0]} to have same direction!")
        if not (np.asarray(img_seq[0].GetSpacing()) == \
            np.asarray(img.GetSpacing())).all():
            raise ValueError(f"Expected {paths[idx]} and {paths[0]} to have same spacing!")